Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[sfrench/cifs-2.6.git] / arch / powerpc / platforms / ps3 / mm.c
1 /*
2  *  PS3 address space management.
3  *
4  *  Copyright (C) 2006 Sony Computer Entertainment Inc.
5  *  Copyright 2006 Sony Corp.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; version 2 of the License.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/memory_hotplug.h>
24 #include <linux/lmb.h>
25
26 #include <asm/firmware.h>
27 #include <asm/prom.h>
28 #include <asm/udbg.h>
29 #include <asm/lv1call.h>
30
31 #include "platform.h"
32
33 #if defined(DEBUG)
34 #define DBG udbg_printf
35 #else
36 #define DBG pr_debug
37 #endif
38
39 enum {
40 #if defined(CONFIG_PS3_DYNAMIC_DMA)
41         USE_DYNAMIC_DMA = 1,
42 #else
43         USE_DYNAMIC_DMA = 0,
44 #endif
45 };
46
47 enum {
48         PAGE_SHIFT_4K = 12U,
49         PAGE_SHIFT_64K = 16U,
50         PAGE_SHIFT_16M = 24U,
51 };
52
53 static unsigned long make_page_sizes(unsigned long a, unsigned long b)
54 {
55         return (a << 56) | (b << 48);
56 }
57
58 enum {
59         ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
60         ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
61 };
62
63 /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
64
65 enum {
66         HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
67         HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
68 };
69
70 /*============================================================================*/
71 /* virtual address space routines                                             */
72 /*============================================================================*/
73
74 /**
75  * struct mem_region - memory region structure
76  * @base: base address
77  * @size: size in bytes
78  * @offset: difference between base and rm.size
79  */
80
81 struct mem_region {
82         unsigned long base;
83         unsigned long size;
84         unsigned long offset;
85 };
86
87 /**
88  * struct map - address space state variables holder
89  * @total: total memory available as reported by HV
90  * @vas_id - HV virtual address space id
91  * @htab_size: htab size in bytes
92  *
93  * The HV virtual address space (vas) allows for hotplug memory regions.
94  * Memory regions can be created and destroyed in the vas at runtime.
95  * @rm: real mode (bootmem) region
96  * @r1: hotplug memory region(s)
97  *
98  * ps3 addresses
99  * virt_addr: a cpu 'translated' effective address
100  * phys_addr: an address in what Linux thinks is the physical address space
101  * lpar_addr: an address in the HV virtual address space
102  * bus_addr: an io controller 'translated' address on a device bus
103  */
104
105 struct map {
106         unsigned long total;
107         unsigned long vas_id;
108         unsigned long htab_size;
109         struct mem_region rm;
110         struct mem_region r1;
111 };
112
113 #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
114 static void __maybe_unused _debug_dump_map(const struct map *m,
115         const char *func, int line)
116 {
117         DBG("%s:%d: map.total     = %lxh\n", func, line, m->total);
118         DBG("%s:%d: map.rm.size   = %lxh\n", func, line, m->rm.size);
119         DBG("%s:%d: map.vas_id    = %lu\n", func, line, m->vas_id);
120         DBG("%s:%d: map.htab_size = %lxh\n", func, line, m->htab_size);
121         DBG("%s:%d: map.r1.base   = %lxh\n", func, line, m->r1.base);
122         DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
123         DBG("%s:%d: map.r1.size   = %lxh\n", func, line, m->r1.size);
124 }
125
126 static struct map map;
127
128 /**
129  * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
130  * @phys_addr: linux physical address
131  */
132
133 unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
134 {
135         BUG_ON(is_kernel_addr(phys_addr));
136         return (phys_addr < map.rm.size || phys_addr >= map.total)
137                 ? phys_addr : phys_addr + map.r1.offset;
138 }
139
140 EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
141
142 /**
143  * ps3_mm_vas_create - create the virtual address space
144  */
145
146 void __init ps3_mm_vas_create(unsigned long* htab_size)
147 {
148         int result;
149         unsigned long start_address;
150         unsigned long size;
151         unsigned long access_right;
152         unsigned long max_page_size;
153         unsigned long flags;
154
155         result = lv1_query_logical_partition_address_region_info(0,
156                 &start_address, &size, &access_right, &max_page_size,
157                 &flags);
158
159         if (result) {
160                 DBG("%s:%d: lv1_query_logical_partition_address_region_info "
161                         "failed: %s\n", __func__, __LINE__,
162                         ps3_result(result));
163                 goto fail;
164         }
165
166         if (max_page_size < PAGE_SHIFT_16M) {
167                 DBG("%s:%d: bad max_page_size %lxh\n", __func__, __LINE__,
168                         max_page_size);
169                 goto fail;
170         }
171
172         BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
173         BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
174
175         result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
176                         2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
177                         &map.vas_id, &map.htab_size);
178
179         if (result) {
180                 DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
181                         __func__, __LINE__, ps3_result(result));
182                 goto fail;
183         }
184
185         result = lv1_select_virtual_address_space(map.vas_id);
186
187         if (result) {
188                 DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
189                         __func__, __LINE__, ps3_result(result));
190                 goto fail;
191         }
192
193         *htab_size = map.htab_size;
194
195         debug_dump_map(&map);
196
197         return;
198
199 fail:
200         panic("ps3_mm_vas_create failed");
201 }
202
203 /**
204  * ps3_mm_vas_destroy -
205  */
206
207 void ps3_mm_vas_destroy(void)
208 {
209         int result;
210
211         DBG("%s:%d: map.vas_id    = %lu\n", __func__, __LINE__, map.vas_id);
212
213         if (map.vas_id) {
214                 result = lv1_select_virtual_address_space(0);
215                 BUG_ON(result);
216                 result = lv1_destruct_virtual_address_space(map.vas_id);
217                 BUG_ON(result);
218                 map.vas_id = 0;
219         }
220 }
221
222 /*============================================================================*/
223 /* memory hotplug routines                                                    */
224 /*============================================================================*/
225
226 /**
227  * ps3_mm_region_create - create a memory region in the vas
228  * @r: pointer to a struct mem_region to accept initialized values
229  * @size: requested region size
230  *
231  * This implementation creates the region with the vas large page size.
232  * @size is rounded down to a multiple of the vas large page size.
233  */
234
235 static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
236 {
237         int result;
238         unsigned long muid;
239
240         r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
241
242         DBG("%s:%d requested  %lxh\n", __func__, __LINE__, size);
243         DBG("%s:%d actual     %lxh\n", __func__, __LINE__, r->size);
244         DBG("%s:%d difference %lxh (%luMB)\n", __func__, __LINE__,
245                 (unsigned long)(size - r->size),
246                 (size - r->size) / 1024 / 1024);
247
248         if (r->size == 0) {
249                 DBG("%s:%d: size == 0\n", __func__, __LINE__);
250                 result = -1;
251                 goto zero_region;
252         }
253
254         result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
255                 ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
256
257         if (result || r->base < map.rm.size) {
258                 DBG("%s:%d: lv1_allocate_memory failed: %s\n",
259                         __func__, __LINE__, ps3_result(result));
260                 goto zero_region;
261         }
262
263         r->offset = r->base - map.rm.size;
264         return result;
265
266 zero_region:
267         r->size = r->base = r->offset = 0;
268         return result;
269 }
270
271 /**
272  * ps3_mm_region_destroy - destroy a memory region
273  * @r: pointer to struct mem_region
274  */
275
276 static void ps3_mm_region_destroy(struct mem_region *r)
277 {
278         int result;
279
280         DBG("%s:%d: r->base = %lxh\n", __func__, __LINE__, r->base);
281         if (r->base) {
282                 result = lv1_release_memory(r->base);
283                 BUG_ON(result);
284                 r->size = r->base = r->offset = 0;
285                 map.total = map.rm.size;
286         }
287 }
288
289 /**
290  * ps3_mm_add_memory - hot add memory
291  */
292
293 static int __init ps3_mm_add_memory(void)
294 {
295         int result;
296         unsigned long start_addr;
297         unsigned long start_pfn;
298         unsigned long nr_pages;
299
300         if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
301                 return -ENODEV;
302
303         BUG_ON(!mem_init_done);
304
305         start_addr = map.rm.size;
306         start_pfn = start_addr >> PAGE_SHIFT;
307         nr_pages = (map.r1.size + PAGE_SIZE - 1) >> PAGE_SHIFT;
308
309         DBG("%s:%d: start_addr %lxh, start_pfn %lxh, nr_pages %lxh\n",
310                 __func__, __LINE__, start_addr, start_pfn, nr_pages);
311
312         result = add_memory(0, start_addr, map.r1.size);
313
314         if (result) {
315                 DBG("%s:%d: add_memory failed: (%d)\n",
316                         __func__, __LINE__, result);
317                 return result;
318         }
319
320         result = online_pages(start_pfn, nr_pages);
321
322         if (result)
323                 DBG("%s:%d: online_pages failed: (%d)\n",
324                         __func__, __LINE__, result);
325
326         return result;
327 }
328
329 core_initcall(ps3_mm_add_memory);
330
331 /*============================================================================*/
332 /* dma routines                                                               */
333 /*============================================================================*/
334
335 /**
336  * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
337  * @r: pointer to dma region structure
338  * @lpar_addr: HV lpar address
339  */
340
341 static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
342         unsigned long lpar_addr)
343 {
344         if (lpar_addr >= map.rm.size)
345                 lpar_addr -= map.r1.offset;
346         BUG_ON(lpar_addr < r->offset);
347         BUG_ON(lpar_addr >= r->offset + r->len);
348         return r->bus_addr + lpar_addr - r->offset;
349 }
350
351 #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
352 static void  __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
353         const char *func, int line)
354 {
355         DBG("%s:%d: dev        %lu:%lu\n", func, line, r->dev->bus_id,
356                 r->dev->dev_id);
357         DBG("%s:%d: page_size  %u\n", func, line, r->page_size);
358         DBG("%s:%d: bus_addr   %lxh\n", func, line, r->bus_addr);
359         DBG("%s:%d: len        %lxh\n", func, line, r->len);
360         DBG("%s:%d: offset     %lxh\n", func, line, r->offset);
361 }
362
363   /**
364  * dma_chunk - A chunk of dma pages mapped by the io controller.
365  * @region - The dma region that owns this chunk.
366  * @lpar_addr: Starting lpar address of the area to map.
367  * @bus_addr: Starting ioc bus address of the area to map.
368  * @len: Length in bytes of the area to map.
369  * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
370  * list of all chuncks owned by the region.
371  *
372  * This implementation uses a very simple dma page manager
373  * based on the dma_chunk structure.  This scheme assumes
374  * that all drivers use very well behaved dma ops.
375  */
376
377 struct dma_chunk {
378         struct ps3_dma_region *region;
379         unsigned long lpar_addr;
380         unsigned long bus_addr;
381         unsigned long len;
382         struct list_head link;
383         unsigned int usage_count;
384 };
385
386 #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
387 static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
388         int line)
389 {
390         DBG("%s:%d: r.dev        %lu:%lu\n", func, line,
391                 c->region->dev->bus_id, c->region->dev->dev_id);
392         DBG("%s:%d: r.bus_addr   %lxh\n", func, line, c->region->bus_addr);
393         DBG("%s:%d: r.page_size  %u\n", func, line, c->region->page_size);
394         DBG("%s:%d: r.len        %lxh\n", func, line, c->region->len);
395         DBG("%s:%d: r.offset     %lxh\n", func, line, c->region->offset);
396         DBG("%s:%d: c.lpar_addr  %lxh\n", func, line, c->lpar_addr);
397         DBG("%s:%d: c.bus_addr   %lxh\n", func, line, c->bus_addr);
398         DBG("%s:%d: c.len        %lxh\n", func, line, c->len);
399 }
400
401 static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
402         unsigned long bus_addr, unsigned long len)
403 {
404         struct dma_chunk *c;
405         unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 1 << r->page_size);
406         unsigned long aligned_len = _ALIGN_UP(len+bus_addr-aligned_bus,
407                                               1 << r->page_size);
408
409         list_for_each_entry(c, &r->chunk_list.head, link) {
410                 /* intersection */
411                 if (aligned_bus >= c->bus_addr &&
412                     aligned_bus + aligned_len <= c->bus_addr + c->len)
413                         return c;
414
415                 /* below */
416                 if (aligned_bus + aligned_len <= c->bus_addr)
417                         continue;
418
419                 /* above */
420                 if (aligned_bus >= c->bus_addr + c->len)
421                         continue;
422
423                 /* we don't handle the multi-chunk case for now */
424                 dma_dump_chunk(c);
425                 BUG();
426         }
427         return NULL;
428 }
429
430 static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
431         unsigned long lpar_addr, unsigned long len)
432 {
433         struct dma_chunk *c;
434         unsigned long aligned_lpar = _ALIGN_DOWN(lpar_addr, 1 << r->page_size);
435         unsigned long aligned_len = _ALIGN_UP(len + lpar_addr - aligned_lpar,
436                                               1 << r->page_size);
437
438         list_for_each_entry(c, &r->chunk_list.head, link) {
439                 /* intersection */
440                 if (c->lpar_addr <= aligned_lpar &&
441                     aligned_lpar < c->lpar_addr + c->len) {
442                         if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
443                                 return c;
444                         else {
445                                 dma_dump_chunk(c);
446                                 BUG();
447                         }
448                 }
449                 /* below */
450                 if (aligned_lpar + aligned_len <= c->lpar_addr) {
451                         continue;
452                 }
453                 /* above */
454                 if (c->lpar_addr + c->len <= aligned_lpar) {
455                         continue;
456                 }
457         }
458         return NULL;
459 }
460
461 static int dma_sb_free_chunk(struct dma_chunk *c)
462 {
463         int result = 0;
464
465         if (c->bus_addr) {
466                 result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
467                         c->region->dev->dev_id, c->bus_addr, c->len);
468                 BUG_ON(result);
469         }
470
471         kfree(c);
472         return result;
473 }
474
475 static int dma_ioc0_free_chunk(struct dma_chunk *c)
476 {
477         int result = 0;
478         int iopage;
479         unsigned long offset;
480         struct ps3_dma_region *r = c->region;
481
482         DBG("%s:start\n", __func__);
483         for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
484                 offset = (1 << r->page_size) * iopage;
485                 /* put INVALID entry */
486                 result = lv1_put_iopte(0,
487                                        c->bus_addr + offset,
488                                        c->lpar_addr + offset,
489                                        r->ioid,
490                                        0);
491                 DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
492                     c->bus_addr + offset,
493                     c->lpar_addr + offset,
494                     r->ioid);
495
496                 if (result) {
497                         DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
498                             __LINE__, ps3_result(result));
499                 }
500         }
501         kfree(c);
502         DBG("%s:end\n", __func__);
503         return result;
504 }
505
506 /**
507  * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
508  * @r: Pointer to a struct ps3_dma_region.
509  * @phys_addr: Starting physical address of the area to map.
510  * @len: Length in bytes of the area to map.
511  * c_out: A pointer to receive an allocated struct dma_chunk for this area.
512  *
513  * This is the lowest level dma mapping routine, and is the one that will
514  * make the HV call to add the pages into the io controller address space.
515  */
516
517 static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
518             unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
519 {
520         int result;
521         struct dma_chunk *c;
522
523         c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
524
525         if (!c) {
526                 result = -ENOMEM;
527                 goto fail_alloc;
528         }
529
530         c->region = r;
531         c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
532         c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
533         c->len = len;
534
535         BUG_ON(iopte_flag != 0xf800000000000000UL);
536         result = lv1_map_device_dma_region(c->region->dev->bus_id,
537                                            c->region->dev->dev_id, c->lpar_addr,
538                                            c->bus_addr, c->len, iopte_flag);
539         if (result) {
540                 DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
541                         __func__, __LINE__, ps3_result(result));
542                 goto fail_map;
543         }
544
545         list_add(&c->link, &r->chunk_list.head);
546
547         *c_out = c;
548         return 0;
549
550 fail_map:
551         kfree(c);
552 fail_alloc:
553         *c_out = NULL;
554         DBG(" <- %s:%d\n", __func__, __LINE__);
555         return result;
556 }
557
558 static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
559                               unsigned long len, struct dma_chunk **c_out,
560                               u64 iopte_flag)
561 {
562         int result;
563         struct dma_chunk *c, *last;
564         int iopage, pages;
565         unsigned long offset;
566
567         DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
568             phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
569         c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
570
571         if (!c) {
572                 result = -ENOMEM;
573                 goto fail_alloc;
574         }
575
576         c->region = r;
577         c->len = len;
578         c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
579         /* allocate IO address */
580         if (list_empty(&r->chunk_list.head)) {
581                 /* first one */
582                 c->bus_addr = r->bus_addr;
583         } else {
584                 /* derive from last bus addr*/
585                 last  = list_entry(r->chunk_list.head.next,
586                                    struct dma_chunk, link);
587                 c->bus_addr = last->bus_addr + last->len;
588                 DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
589                     last->bus_addr, last->len);
590         }
591
592         /* FIXME: check whether length exceeds region size */
593
594         /* build ioptes for the area */
595         pages = len >> r->page_size;
596         DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#lx\n", __func__,
597             r->page_size, r->len, pages, iopte_flag);
598         for (iopage = 0; iopage < pages; iopage++) {
599                 offset = (1 << r->page_size) * iopage;
600                 result = lv1_put_iopte(0,
601                                        c->bus_addr + offset,
602                                        c->lpar_addr + offset,
603                                        r->ioid,
604                                        iopte_flag);
605                 if (result) {
606                         printk(KERN_WARNING "%s:%d: lv1_map_device_dma_region "
607                                 "failed: %s\n", __func__, __LINE__,
608                                 ps3_result(result));
609                         goto fail_map;
610                 }
611                 DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
612                     iopage, c->bus_addr + offset, c->lpar_addr + offset,
613                     r->ioid);
614         }
615
616         /* be sure that last allocated one is inserted at head */
617         list_add(&c->link, &r->chunk_list.head);
618
619         *c_out = c;
620         DBG("%s: end\n", __func__);
621         return 0;
622
623 fail_map:
624         for (iopage--; 0 <= iopage; iopage--) {
625                 lv1_put_iopte(0,
626                               c->bus_addr + offset,
627                               c->lpar_addr + offset,
628                               r->ioid,
629                               0);
630         }
631         kfree(c);
632 fail_alloc:
633         *c_out = NULL;
634         return result;
635 }
636
637 /**
638  * dma_sb_region_create - Create a device dma region.
639  * @r: Pointer to a struct ps3_dma_region.
640  *
641  * This is the lowest level dma region create routine, and is the one that
642  * will make the HV call to create the region.
643  */
644
645 static int dma_sb_region_create(struct ps3_dma_region *r)
646 {
647         int result;
648
649         pr_info(" -> %s:%d:\n", __func__, __LINE__);
650
651         BUG_ON(!r);
652
653         if (!r->dev->bus_id) {
654                 pr_info("%s:%d: %lu:%lu no dma\n", __func__, __LINE__,
655                         r->dev->bus_id, r->dev->dev_id);
656                 return 0;
657         }
658
659         DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
660             __LINE__, r->len, r->page_size, r->offset);
661
662         BUG_ON(!r->len);
663         BUG_ON(!r->page_size);
664         BUG_ON(!r->region_ops);
665
666         INIT_LIST_HEAD(&r->chunk_list.head);
667         spin_lock_init(&r->chunk_list.lock);
668
669         result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
670                 roundup_pow_of_two(r->len), r->page_size, r->region_type,
671                 &r->bus_addr);
672
673         if (result) {
674                 DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
675                         __func__, __LINE__, ps3_result(result));
676                 r->len = r->bus_addr = 0;
677         }
678
679         return result;
680 }
681
682 static int dma_ioc0_region_create(struct ps3_dma_region *r)
683 {
684         int result;
685
686         INIT_LIST_HEAD(&r->chunk_list.head);
687         spin_lock_init(&r->chunk_list.lock);
688
689         result = lv1_allocate_io_segment(0,
690                                          r->len,
691                                          r->page_size,
692                                          &r->bus_addr);
693         if (result) {
694                 DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
695                         __func__, __LINE__, ps3_result(result));
696                 r->len = r->bus_addr = 0;
697         }
698         DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
699             r->len, r->page_size, r->bus_addr);
700         return result;
701 }
702
703 /**
704  * dma_region_free - Free a device dma region.
705  * @r: Pointer to a struct ps3_dma_region.
706  *
707  * This is the lowest level dma region free routine, and is the one that
708  * will make the HV call to free the region.
709  */
710
711 static int dma_sb_region_free(struct ps3_dma_region *r)
712 {
713         int result;
714         struct dma_chunk *c;
715         struct dma_chunk *tmp;
716
717         BUG_ON(!r);
718
719         if (!r->dev->bus_id) {
720                 pr_info("%s:%d: %lu:%lu no dma\n", __func__, __LINE__,
721                         r->dev->bus_id, r->dev->dev_id);
722                 return 0;
723         }
724
725         list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
726                 list_del(&c->link);
727                 dma_sb_free_chunk(c);
728         }
729
730         result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
731                 r->bus_addr);
732
733         if (result)
734                 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
735                         __func__, __LINE__, ps3_result(result));
736
737         r->bus_addr = 0;
738
739         return result;
740 }
741
742 static int dma_ioc0_region_free(struct ps3_dma_region *r)
743 {
744         int result;
745         struct dma_chunk *c, *n;
746
747         DBG("%s: start\n", __func__);
748         list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
749                 list_del(&c->link);
750                 dma_ioc0_free_chunk(c);
751         }
752
753         result = lv1_release_io_segment(0, r->bus_addr);
754
755         if (result)
756                 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
757                         __func__, __LINE__, ps3_result(result));
758
759         r->bus_addr = 0;
760         DBG("%s: end\n", __func__);
761
762         return result;
763 }
764
765 /**
766  * dma_sb_map_area - Map an area of memory into a device dma region.
767  * @r: Pointer to a struct ps3_dma_region.
768  * @virt_addr: Starting virtual address of the area to map.
769  * @len: Length in bytes of the area to map.
770  * @bus_addr: A pointer to return the starting ioc bus address of the area to
771  * map.
772  *
773  * This is the common dma mapping routine.
774  */
775
776 static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
777            unsigned long len, unsigned long *bus_addr,
778            u64 iopte_flag)
779 {
780         int result;
781         unsigned long flags;
782         struct dma_chunk *c;
783         unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
784                 : virt_addr;
785         unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
786         unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
787                                               1 << r->page_size);
788         *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
789
790         if (!USE_DYNAMIC_DMA) {
791                 unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
792                 DBG(" -> %s:%d\n", __func__, __LINE__);
793                 DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
794                         virt_addr);
795                 DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
796                         phys_addr);
797                 DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
798                         lpar_addr);
799                 DBG("%s:%d len       %lxh\n", __func__, __LINE__, len);
800                 DBG("%s:%d bus_addr  %lxh (%lxh)\n", __func__, __LINE__,
801                 *bus_addr, len);
802         }
803
804         spin_lock_irqsave(&r->chunk_list.lock, flags);
805         c = dma_find_chunk(r, *bus_addr, len);
806
807         if (c) {
808                 DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
809                 dma_dump_chunk(c);
810                 c->usage_count++;
811                 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
812                 return 0;
813         }
814
815         result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
816
817         if (result) {
818                 *bus_addr = 0;
819                 DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
820                         __func__, __LINE__, result);
821                 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
822                 return result;
823         }
824
825         c->usage_count = 1;
826
827         spin_unlock_irqrestore(&r->chunk_list.lock, flags);
828         return result;
829 }
830
831 static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
832              unsigned long len, unsigned long *bus_addr,
833              u64 iopte_flag)
834 {
835         int result;
836         unsigned long flags;
837         struct dma_chunk *c;
838         unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
839                 : virt_addr;
840         unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
841         unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
842                                               1 << r->page_size);
843
844         DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
845             virt_addr, len);
846         DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
847             phys_addr, aligned_phys, aligned_len);
848
849         spin_lock_irqsave(&r->chunk_list.lock, flags);
850         c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
851
852         if (c) {
853                 /* FIXME */
854                 BUG();
855                 *bus_addr = c->bus_addr + phys_addr - aligned_phys;
856                 c->usage_count++;
857                 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
858                 return 0;
859         }
860
861         result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
862                                     iopte_flag);
863
864         if (result) {
865                 *bus_addr = 0;
866                 DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
867                         __func__, __LINE__, result);
868                 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
869                 return result;
870         }
871         *bus_addr = c->bus_addr + phys_addr - aligned_phys;
872         DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#lx\n", __func__,
873             virt_addr, phys_addr, aligned_phys, *bus_addr);
874         c->usage_count = 1;
875
876         spin_unlock_irqrestore(&r->chunk_list.lock, flags);
877         return result;
878 }
879
880 /**
881  * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
882  * @r: Pointer to a struct ps3_dma_region.
883  * @bus_addr: The starting ioc bus address of the area to unmap.
884  * @len: Length in bytes of the area to unmap.
885  *
886  * This is the common dma unmap routine.
887  */
888
889 static int dma_sb_unmap_area(struct ps3_dma_region *r, unsigned long bus_addr,
890         unsigned long len)
891 {
892         unsigned long flags;
893         struct dma_chunk *c;
894
895         spin_lock_irqsave(&r->chunk_list.lock, flags);
896         c = dma_find_chunk(r, bus_addr, len);
897
898         if (!c) {
899                 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
900                         1 << r->page_size);
901                 unsigned long aligned_len = _ALIGN_UP(len + bus_addr
902                         - aligned_bus, 1 << r->page_size);
903                 DBG("%s:%d: not found: bus_addr %lxh\n",
904                         __func__, __LINE__, bus_addr);
905                 DBG("%s:%d: not found: len %lxh\n",
906                         __func__, __LINE__, len);
907                 DBG("%s:%d: not found: aligned_bus %lxh\n",
908                         __func__, __LINE__, aligned_bus);
909                 DBG("%s:%d: not found: aligned_len %lxh\n",
910                         __func__, __LINE__, aligned_len);
911                 BUG();
912         }
913
914         c->usage_count--;
915
916         if (!c->usage_count) {
917                 list_del(&c->link);
918                 dma_sb_free_chunk(c);
919         }
920
921         spin_unlock_irqrestore(&r->chunk_list.lock, flags);
922         return 0;
923 }
924
925 static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
926                         unsigned long bus_addr, unsigned long len)
927 {
928         unsigned long flags;
929         struct dma_chunk *c;
930
931         DBG("%s: start a=%#lx l=%#lx\n", __func__, bus_addr, len);
932         spin_lock_irqsave(&r->chunk_list.lock, flags);
933         c = dma_find_chunk(r, bus_addr, len);
934
935         if (!c) {
936                 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
937                                                         1 << r->page_size);
938                 unsigned long aligned_len = _ALIGN_UP(len + bus_addr
939                                                       - aligned_bus,
940                                                       1 << r->page_size);
941                 DBG("%s:%d: not found: bus_addr %lxh\n",
942                     __func__, __LINE__, bus_addr);
943                 DBG("%s:%d: not found: len %lxh\n",
944                     __func__, __LINE__, len);
945                 DBG("%s:%d: not found: aligned_bus %lxh\n",
946                     __func__, __LINE__, aligned_bus);
947                 DBG("%s:%d: not found: aligned_len %lxh\n",
948                     __func__, __LINE__, aligned_len);
949                 BUG();
950         }
951
952         c->usage_count--;
953
954         if (!c->usage_count) {
955                 list_del(&c->link);
956                 dma_ioc0_free_chunk(c);
957         }
958
959         spin_unlock_irqrestore(&r->chunk_list.lock, flags);
960         DBG("%s: end\n", __func__);
961         return 0;
962 }
963
964 /**
965  * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
966  * @r: Pointer to a struct ps3_dma_region.
967  *
968  * This routine creates an HV dma region for the device and maps all available
969  * ram into the io controller bus address space.
970  */
971
972 static int dma_sb_region_create_linear(struct ps3_dma_region *r)
973 {
974         int result;
975         unsigned long virt_addr, len, tmp;
976
977         if (r->len > 16*1024*1024) {    /* FIXME: need proper fix */
978                 /* force 16M dma pages for linear mapping */
979                 if (r->page_size != PS3_DMA_16M) {
980                         pr_info("%s:%d: forcing 16M pages for linear map\n",
981                                 __func__, __LINE__);
982                         r->page_size = PS3_DMA_16M;
983                         r->len = _ALIGN_UP(r->len, 1 << r->page_size);
984                 }
985         }
986
987         result = dma_sb_region_create(r);
988         BUG_ON(result);
989
990         if (r->offset < map.rm.size) {
991                 /* Map (part of) 1st RAM chunk */
992                 virt_addr = map.rm.base + r->offset;
993                 len = map.rm.size - r->offset;
994                 if (len > r->len)
995                         len = r->len;
996                 result = dma_sb_map_area(r, virt_addr, len, &tmp,
997                         IOPTE_PP_W | IOPTE_PP_R | IOPTE_SO_RW | IOPTE_M);
998                 BUG_ON(result);
999         }
1000
1001         if (r->offset + r->len > map.rm.size) {
1002                 /* Map (part of) 2nd RAM chunk */
1003                 virt_addr = map.rm.size;
1004                 len = r->len;
1005                 if (r->offset >= map.rm.size)
1006                         virt_addr += r->offset - map.rm.size;
1007                 else
1008                         len -= map.rm.size - r->offset;
1009                 result = dma_sb_map_area(r, virt_addr, len, &tmp,
1010                         IOPTE_PP_W | IOPTE_PP_R | IOPTE_SO_RW | IOPTE_M);
1011                 BUG_ON(result);
1012         }
1013
1014         return result;
1015 }
1016
1017 /**
1018  * dma_sb_region_free_linear - Free a linear dma mapping for a device.
1019  * @r: Pointer to a struct ps3_dma_region.
1020  *
1021  * This routine will unmap all mapped areas and free the HV dma region.
1022  */
1023
1024 static int dma_sb_region_free_linear(struct ps3_dma_region *r)
1025 {
1026         int result;
1027         unsigned long bus_addr, len, lpar_addr;
1028
1029         if (r->offset < map.rm.size) {
1030                 /* Unmap (part of) 1st RAM chunk */
1031                 lpar_addr = map.rm.base + r->offset;
1032                 len = map.rm.size - r->offset;
1033                 if (len > r->len)
1034                         len = r->len;
1035                 bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1036                 result = dma_sb_unmap_area(r, bus_addr, len);
1037                 BUG_ON(result);
1038         }
1039
1040         if (r->offset + r->len > map.rm.size) {
1041                 /* Unmap (part of) 2nd RAM chunk */
1042                 lpar_addr = map.r1.base;
1043                 len = r->len;
1044                 if (r->offset >= map.rm.size)
1045                         lpar_addr += r->offset - map.rm.size;
1046                 else
1047                         len -= map.rm.size - r->offset;
1048                 bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1049                 result = dma_sb_unmap_area(r, bus_addr, len);
1050                 BUG_ON(result);
1051         }
1052
1053         result = dma_sb_region_free(r);
1054         BUG_ON(result);
1055
1056         return result;
1057 }
1058
1059 /**
1060  * dma_sb_map_area_linear - Map an area of memory into a device dma region.
1061  * @r: Pointer to a struct ps3_dma_region.
1062  * @virt_addr: Starting virtual address of the area to map.
1063  * @len: Length in bytes of the area to map.
1064  * @bus_addr: A pointer to return the starting ioc bus address of the area to
1065  * map.
1066  *
1067  * This routine just returns the corresponding bus address.  Actual mapping
1068  * occurs in dma_region_create_linear().
1069  */
1070
1071 static int dma_sb_map_area_linear(struct ps3_dma_region *r,
1072         unsigned long virt_addr, unsigned long len, unsigned long *bus_addr,
1073         u64 iopte_flag)
1074 {
1075         unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
1076                 : virt_addr;
1077         *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
1078         return 0;
1079 }
1080
1081 /**
1082  * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
1083  * @r: Pointer to a struct ps3_dma_region.
1084  * @bus_addr: The starting ioc bus address of the area to unmap.
1085  * @len: Length in bytes of the area to unmap.
1086  *
1087  * This routine does nothing.  Unmapping occurs in dma_sb_region_free_linear().
1088  */
1089
1090 static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
1091         unsigned long bus_addr, unsigned long len)
1092 {
1093         return 0;
1094 };
1095
1096 static const struct ps3_dma_region_ops ps3_dma_sb_region_ops =  {
1097         .create = dma_sb_region_create,
1098         .free = dma_sb_region_free,
1099         .map = dma_sb_map_area,
1100         .unmap = dma_sb_unmap_area
1101 };
1102
1103 static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
1104         .create = dma_sb_region_create_linear,
1105         .free = dma_sb_region_free_linear,
1106         .map = dma_sb_map_area_linear,
1107         .unmap = dma_sb_unmap_area_linear
1108 };
1109
1110 static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
1111         .create = dma_ioc0_region_create,
1112         .free = dma_ioc0_region_free,
1113         .map = dma_ioc0_map_area,
1114         .unmap = dma_ioc0_unmap_area
1115 };
1116
1117 int ps3_dma_region_init(struct ps3_system_bus_device *dev,
1118         struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
1119         enum ps3_dma_region_type region_type, void *addr, unsigned long len)
1120 {
1121         unsigned long lpar_addr;
1122
1123         lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
1124
1125         r->dev = dev;
1126         r->page_size = page_size;
1127         r->region_type = region_type;
1128         r->offset = lpar_addr;
1129         if (r->offset >= map.rm.size)
1130                 r->offset -= map.r1.offset;
1131         r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);
1132
1133         switch (dev->dev_type) {
1134         case PS3_DEVICE_TYPE_SB:
1135                 r->region_ops =  (USE_DYNAMIC_DMA)
1136                         ? &ps3_dma_sb_region_ops
1137                         : &ps3_dma_sb_region_linear_ops;
1138                 break;
1139         case PS3_DEVICE_TYPE_IOC0:
1140                 r->region_ops = &ps3_dma_ioc0_region_ops;
1141                 break;
1142         default:
1143                 BUG();
1144                 return -EINVAL;
1145         }
1146         return 0;
1147 }
1148 EXPORT_SYMBOL(ps3_dma_region_init);
1149
1150 int ps3_dma_region_create(struct ps3_dma_region *r)
1151 {
1152         BUG_ON(!r);
1153         BUG_ON(!r->region_ops);
1154         BUG_ON(!r->region_ops->create);
1155         return r->region_ops->create(r);
1156 }
1157 EXPORT_SYMBOL(ps3_dma_region_create);
1158
1159 int ps3_dma_region_free(struct ps3_dma_region *r)
1160 {
1161         BUG_ON(!r);
1162         BUG_ON(!r->region_ops);
1163         BUG_ON(!r->region_ops->free);
1164         return r->region_ops->free(r);
1165 }
1166 EXPORT_SYMBOL(ps3_dma_region_free);
1167
1168 int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
1169         unsigned long len, unsigned long *bus_addr,
1170         u64 iopte_flag)
1171 {
1172         return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
1173 }
1174
1175 int ps3_dma_unmap(struct ps3_dma_region *r, unsigned long bus_addr,
1176         unsigned long len)
1177 {
1178         return r->region_ops->unmap(r, bus_addr, len);
1179 }
1180
1181 /*============================================================================*/
1182 /* system startup routines                                                    */
1183 /*============================================================================*/
1184
1185 /**
1186  * ps3_mm_init - initialize the address space state variables
1187  */
1188
1189 void __init ps3_mm_init(void)
1190 {
1191         int result;
1192
1193         DBG(" -> %s:%d\n", __func__, __LINE__);
1194
1195         result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
1196                 &map.total);
1197
1198         if (result)
1199                 panic("ps3_repository_read_mm_info() failed");
1200
1201         map.rm.offset = map.rm.base;
1202         map.vas_id = map.htab_size = 0;
1203
1204         /* this implementation assumes map.rm.base is zero */
1205
1206         BUG_ON(map.rm.base);
1207         BUG_ON(!map.rm.size);
1208
1209
1210         /* arrange to do this in ps3_mm_add_memory */
1211         ps3_mm_region_create(&map.r1, map.total - map.rm.size);
1212
1213         /* correct map.total for the real total amount of memory we use */
1214         map.total = map.rm.size + map.r1.size;
1215
1216         DBG(" <- %s:%d\n", __func__, __LINE__);
1217 }
1218
1219 /**
1220  * ps3_mm_shutdown - final cleanup of address space
1221  */
1222
1223 void ps3_mm_shutdown(void)
1224 {
1225         ps3_mm_region_destroy(&map.r1);
1226 }