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