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