Pull video into test branch
[linux-drm-fsl-dcu.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/ps3.h>
29 #include <asm/lv1call.h>
30
31 #include "platform.h"
32
33 #if defined(DEBUG)
34 #define DBG(fmt...) udbg_printf(fmt)
35 #else
36 #define DBG(fmt...) do{if(0)printk(fmt);}while(0)
37 #endif
38
39 enum {
40 #if defined(CONFIG_PS3_USE_LPAR_ADDR)
41         USE_LPAR_ADDR = 1,
42 #else
43         USE_LPAR_ADDR = 0,
44 #endif
45 #if defined(CONFIG_PS3_DYNAMIC_DMA)
46         USE_DYNAMIC_DMA = 1,
47 #else
48         USE_DYNAMIC_DMA = 0,
49 #endif
50 };
51
52 enum {
53         PAGE_SHIFT_4K = 12U,
54         PAGE_SHIFT_64K = 16U,
55         PAGE_SHIFT_16M = 24U,
56 };
57
58 static unsigned long make_page_sizes(unsigned long a, unsigned long b)
59 {
60         return (a << 56) | (b << 48);
61 }
62
63 enum {
64         ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
65         ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
66 };
67
68 /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
69
70 enum {
71         HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
72         HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
73 };
74
75 /*============================================================================*/
76 /* virtual address space routines                                             */
77 /*============================================================================*/
78
79 /**
80  * struct mem_region - memory region structure
81  * @base: base address
82  * @size: size in bytes
83  * @offset: difference between base and rm.size
84  */
85
86 struct mem_region {
87         unsigned long base;
88         unsigned long size;
89         unsigned long offset;
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: hotplug memory 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         unsigned long total;
112         unsigned long vas_id;
113         unsigned long 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 _debug_dump_map(const struct map* m, 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         if (map.vas_id) {
217                 lv1_select_virtual_address_space(0);
218                 lv1_destruct_virtual_address_space(map.vas_id);
219                 map.vas_id = 0;
220         }
221 }
222
223 /*============================================================================*/
224 /* memory hotplug routines                                                    */
225 /*============================================================================*/
226
227 /**
228  * ps3_mm_region_create - create a memory region in the vas
229  * @r: pointer to a struct mem_region to accept initialized values
230  * @size: requested region size
231  *
232  * This implementation creates the region with the vas large page size.
233  * @size is rounded down to a multiple of the vas large page size.
234  */
235
236 int ps3_mm_region_create(struct mem_region *r, unsigned long size)
237 {
238         int result;
239         unsigned long muid;
240
241         r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
242
243         DBG("%s:%d requested  %lxh\n", __func__, __LINE__, size);
244         DBG("%s:%d actual     %lxh\n", __func__, __LINE__, r->size);
245         DBG("%s:%d difference %lxh (%luMB)\n", __func__, __LINE__,
246                 (unsigned long)(size - r->size),
247                 (size - r->size) / 1024 / 1024);
248
249         if (r->size == 0) {
250                 DBG("%s:%d: size == 0\n", __func__, __LINE__);
251                 result = -1;
252                 goto zero_region;
253         }
254
255         result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
256                 ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
257
258         if (result || r->base < map.rm.size) {
259                 DBG("%s:%d: lv1_allocate_memory failed: %s\n",
260                         __func__, __LINE__, ps3_result(result));
261                 goto zero_region;
262         }
263
264         r->offset = r->base - map.rm.size;
265         return result;
266
267 zero_region:
268         r->size = r->base = r->offset = 0;
269         return result;
270 }
271
272 /**
273  * ps3_mm_region_destroy - destroy a memory region
274  * @r: pointer to struct mem_region
275  */
276
277 void ps3_mm_region_destroy(struct mem_region *r)
278 {
279         if (r->base) {
280                 lv1_release_memory(r->base);
281                 r->size = r->base = r->offset = 0;
282                 map.total = map.rm.size;
283         }
284 }
285
286 /**
287  * ps3_mm_add_memory - hot add memory
288  */
289
290 static int __init ps3_mm_add_memory(void)
291 {
292         int result;
293         unsigned long start_addr;
294         unsigned long start_pfn;
295         unsigned long nr_pages;
296
297         if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
298                 return 0;
299
300         BUG_ON(!mem_init_done);
301
302         start_addr = USE_LPAR_ADDR ? map.r1.base : map.rm.size;
303         start_pfn = start_addr >> PAGE_SHIFT;
304         nr_pages = (map.r1.size + PAGE_SIZE - 1) >> PAGE_SHIFT;
305
306         DBG("%s:%d: start_addr %lxh, start_pfn %lxh, nr_pages %lxh\n",
307                 __func__, __LINE__, start_addr, start_pfn, nr_pages);
308
309         result = add_memory(0, start_addr, map.r1.size);
310
311         if (result) {
312                 DBG("%s:%d: add_memory failed: (%d)\n",
313                         __func__, __LINE__, result);
314                 return result;
315         }
316
317         result = online_pages(start_pfn, nr_pages);
318
319         if (result)
320                 DBG("%s:%d: online_pages failed: (%d)\n",
321                         __func__, __LINE__, result);
322
323         return result;
324 }
325
326 core_initcall(ps3_mm_add_memory);
327
328 /*============================================================================*/
329 /* dma routines                                                               */
330 /*============================================================================*/
331
332 /**
333  * dma_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
334  * @r: pointer to dma region structure
335  * @lpar_addr: HV lpar address
336  */
337
338 static unsigned long dma_lpar_to_bus(struct ps3_dma_region *r,
339         unsigned long lpar_addr)
340 {
341         BUG_ON(lpar_addr >= map.r1.base + map.r1.size);
342         return r->bus_addr + (lpar_addr <= map.rm.size ? lpar_addr
343                 : lpar_addr - map.r1.offset);
344 }
345
346 #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
347 static void _dma_dump_region(const struct ps3_dma_region *r, const char* func,
348         int line)
349 {
350         DBG("%s:%d: dev        %u:%u\n", func, line, r->did.bus_id,
351                 r->did.dev_id);
352         DBG("%s:%d: page_size  %u\n", func, line, r->page_size);
353         DBG("%s:%d: bus_addr   %lxh\n", func, line, r->bus_addr);
354         DBG("%s:%d: len        %lxh\n", func, line, r->len);
355 }
356
357 /**
358  * dma_chunk - A chunk of dma pages mapped by the io controller.
359  * @region - The dma region that owns this chunk.
360  * @lpar_addr: Starting lpar address of the area to map.
361  * @bus_addr: Starting ioc bus address of the area to map.
362  * @len: Length in bytes of the area to map.
363  * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
364  * list of all chuncks owned by the region.
365  *
366  * This implementation uses a very simple dma page manager
367  * based on the dma_chunk structure.  This scheme assumes
368  * that all drivers use very well behaved dma ops.
369  */
370
371 struct dma_chunk {
372         struct ps3_dma_region *region;
373         unsigned long lpar_addr;
374         unsigned long bus_addr;
375         unsigned long len;
376         struct list_head link;
377         unsigned int usage_count;
378 };
379
380 #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
381 static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
382         int line)
383 {
384         DBG("%s:%d: r.dev        %u:%u\n", func, line,
385                 c->region->did.bus_id, c->region->did.dev_id);
386         DBG("%s:%d: r.bus_addr   %lxh\n", func, line, c->region->bus_addr);
387         DBG("%s:%d: r.page_size  %u\n", func, line, c->region->page_size);
388         DBG("%s:%d: r.len        %lxh\n", func, line, c->region->len);
389         DBG("%s:%d: c.lpar_addr  %lxh\n", func, line, c->lpar_addr);
390         DBG("%s:%d: c.bus_addr   %lxh\n", func, line, c->bus_addr);
391         DBG("%s:%d: c.len        %lxh\n", func, line, c->len);
392 }
393
394 static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
395         unsigned long bus_addr, unsigned long len)
396 {
397         struct dma_chunk *c;
398         unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 1 << r->page_size);
399         unsigned long aligned_len = _ALIGN_UP(len, 1 << r->page_size);
400
401         list_for_each_entry(c, &r->chunk_list.head, link) {
402                 /* intersection */
403                 if (aligned_bus >= c->bus_addr
404                         && aligned_bus < c->bus_addr + c->len
405                         && aligned_bus + aligned_len <= c->bus_addr + c->len) {
406                         return c;
407                 }
408                 /* below */
409                 if (aligned_bus + aligned_len <= c->bus_addr) {
410                         continue;
411                 }
412                 /* above */
413                 if (aligned_bus >= c->bus_addr + c->len) {
414                         continue;
415                 }
416
417                 /* we don't handle the multi-chunk case for now */
418
419                 dma_dump_chunk(c);
420                 BUG();
421         }
422         return NULL;
423 }
424
425 static int dma_free_chunk(struct dma_chunk *c)
426 {
427         int result = 0;
428
429         if (c->bus_addr) {
430                 result = lv1_unmap_device_dma_region(c->region->did.bus_id,
431                         c->region->did.dev_id, c->bus_addr, c->len);
432                 BUG_ON(result);
433         }
434
435         kfree(c);
436         return result;
437 }
438
439 /**
440  * dma_map_pages - Maps dma pages into the io controller bus address space.
441  * @r: Pointer to a struct ps3_dma_region.
442  * @phys_addr: Starting physical address of the area to map.
443  * @len: Length in bytes of the area to map.
444  * c_out: A pointer to receive an allocated struct dma_chunk for this area.
445  *
446  * This is the lowest level dma mapping routine, and is the one that will
447  * make the HV call to add the pages into the io controller address space.
448  */
449
450 static int dma_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
451         unsigned long len, struct dma_chunk **c_out)
452 {
453         int result;
454         struct dma_chunk *c;
455
456         c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
457
458         if (!c) {
459                 result = -ENOMEM;
460                 goto fail_alloc;
461         }
462
463         c->region = r;
464         c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
465         c->bus_addr = dma_lpar_to_bus(r, c->lpar_addr);
466         c->len = len;
467
468         result = lv1_map_device_dma_region(c->region->did.bus_id,
469                 c->region->did.dev_id, c->lpar_addr, c->bus_addr, c->len,
470                 0xf800000000000000UL);
471
472         if (result) {
473                 DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
474                         __func__, __LINE__, ps3_result(result));
475                 goto fail_map;
476         }
477
478         list_add(&c->link, &r->chunk_list.head);
479
480         *c_out = c;
481         return 0;
482
483 fail_map:
484         kfree(c);
485 fail_alloc:
486         *c_out = NULL;
487         DBG(" <- %s:%d\n", __func__, __LINE__);
488         return result;
489 }
490
491 /**
492  * dma_region_create - Create a device dma region.
493  * @r: Pointer to a struct ps3_dma_region.
494  *
495  * This is the lowest level dma region create routine, and is the one that
496  * will make the HV call to create the region.
497  */
498
499 static int dma_region_create(struct ps3_dma_region* r)
500 {
501         int result;
502
503         r->len = _ALIGN_UP(map.total, 1 << r->page_size);
504         INIT_LIST_HEAD(&r->chunk_list.head);
505         spin_lock_init(&r->chunk_list.lock);
506
507         result = lv1_allocate_device_dma_region(r->did.bus_id, r->did.dev_id,
508                 r->len, r->page_size, r->region_type, &r->bus_addr);
509
510         dma_dump_region(r);
511
512         if (result) {
513                 DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
514                         __func__, __LINE__, ps3_result(result));
515                 r->len = r->bus_addr = 0;
516         }
517
518         return result;
519 }
520
521 /**
522  * dma_region_free - Free a device dma region.
523  * @r: Pointer to a struct ps3_dma_region.
524  *
525  * This is the lowest level dma region free routine, and is the one that
526  * will make the HV call to free the region.
527  */
528
529 static int dma_region_free(struct ps3_dma_region* r)
530 {
531         int result;
532         struct dma_chunk *c;
533         struct dma_chunk *tmp;
534
535         list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
536                 list_del(&c->link);
537                 dma_free_chunk(c);
538         }
539
540         result = lv1_free_device_dma_region(r->did.bus_id, r->did.dev_id,
541                 r->bus_addr);
542
543         if (result)
544                 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
545                         __func__, __LINE__, ps3_result(result));
546
547         r->len = r->bus_addr = 0;
548
549         return result;
550 }
551
552 /**
553  * dma_map_area - Map an area of memory into a device dma region.
554  * @r: Pointer to a struct ps3_dma_region.
555  * @virt_addr: Starting virtual address of the area to map.
556  * @len: Length in bytes of the area to map.
557  * @bus_addr: A pointer to return the starting ioc bus address of the area to
558  * map.
559  *
560  * This is the common dma mapping routine.
561  */
562
563 static int dma_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
564         unsigned long len, unsigned long *bus_addr)
565 {
566         int result;
567         unsigned long flags;
568         struct dma_chunk *c;
569         unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
570                 : virt_addr;
571
572         *bus_addr = dma_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
573
574         if (!USE_DYNAMIC_DMA) {
575                 unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
576                 DBG(" -> %s:%d\n", __func__, __LINE__);
577                 DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
578                         virt_addr);
579                 DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
580                         phys_addr);
581                 DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
582                         lpar_addr);
583                 DBG("%s:%d len       %lxh\n", __func__, __LINE__, len);
584                 DBG("%s:%d bus_addr  %lxh (%lxh)\n", __func__, __LINE__,
585                 *bus_addr, len);
586         }
587
588         spin_lock_irqsave(&r->chunk_list.lock, flags);
589         c = dma_find_chunk(r, *bus_addr, len);
590
591         if (c) {
592                 c->usage_count++;
593                 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
594                 return 0;
595         }
596
597         result = dma_map_pages(r, _ALIGN_DOWN(phys_addr, 1 << r->page_size),
598                 _ALIGN_UP(len, 1 << r->page_size), &c);
599
600         if (result) {
601                 *bus_addr = 0;
602                 DBG("%s:%d: dma_map_pages failed (%d)\n",
603                         __func__, __LINE__, result);
604                 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
605                 return result;
606         }
607
608         c->usage_count = 1;
609
610         spin_unlock_irqrestore(&r->chunk_list.lock, flags);
611         return result;
612 }
613
614 /**
615  * dma_unmap_area - Unmap an area of memory from a device dma region.
616  * @r: Pointer to a struct ps3_dma_region.
617  * @bus_addr: The starting ioc bus address of the area to unmap.
618  * @len: Length in bytes of the area to unmap.
619  *
620  * This is the common dma unmap routine.
621  */
622
623 int dma_unmap_area(struct ps3_dma_region *r, unsigned long bus_addr,
624         unsigned long len)
625 {
626         unsigned long flags;
627         struct dma_chunk *c;
628
629         spin_lock_irqsave(&r->chunk_list.lock, flags);
630         c = dma_find_chunk(r, bus_addr, len);
631
632         if (!c) {
633                 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
634                         1 << r->page_size);
635                 unsigned long aligned_len = _ALIGN_UP(len, 1 << r->page_size);
636                 DBG("%s:%d: not found: bus_addr %lxh\n",
637                         __func__, __LINE__, bus_addr);
638                 DBG("%s:%d: not found: len %lxh\n",
639                         __func__, __LINE__, len);
640                 DBG("%s:%d: not found: aligned_bus %lxh\n",
641                         __func__, __LINE__, aligned_bus);
642                 DBG("%s:%d: not found: aligned_len %lxh\n",
643                         __func__, __LINE__, aligned_len);
644                 BUG();
645         }
646
647         c->usage_count--;
648
649         if (!c->usage_count) {
650                 list_del(&c->link);
651                 dma_free_chunk(c);
652         }
653
654         spin_unlock_irqrestore(&r->chunk_list.lock, flags);
655         return 0;
656 }
657
658 /**
659  * dma_region_create_linear - Setup a linear dma maping for a device.
660  * @r: Pointer to a struct ps3_dma_region.
661  *
662  * This routine creates an HV dma region for the device and maps all available
663  * ram into the io controller bus address space.
664  */
665
666 static int dma_region_create_linear(struct ps3_dma_region *r)
667 {
668         int result;
669         unsigned long tmp;
670
671         /* force 16M dma pages for linear mapping */
672
673         if (r->page_size != PS3_DMA_16M) {
674                 pr_info("%s:%d: forcing 16M pages for linear map\n",
675                         __func__, __LINE__);
676                 r->page_size = PS3_DMA_16M;
677         }
678
679         result = dma_region_create(r);
680         BUG_ON(result);
681
682         result = dma_map_area(r, map.rm.base, map.rm.size, &tmp);
683         BUG_ON(result);
684
685         if (USE_LPAR_ADDR)
686                 result = dma_map_area(r, map.r1.base, map.r1.size,
687                         &tmp);
688         else
689                 result = dma_map_area(r, map.rm.size, map.r1.size,
690                         &tmp);
691
692         BUG_ON(result);
693
694         return result;
695 }
696
697 /**
698  * dma_region_free_linear - Free a linear dma mapping for a device.
699  * @r: Pointer to a struct ps3_dma_region.
700  *
701  * This routine will unmap all mapped areas and free the HV dma region.
702  */
703
704 static int dma_region_free_linear(struct ps3_dma_region *r)
705 {
706         int result;
707
708         result = dma_unmap_area(r, dma_lpar_to_bus(r, 0), map.rm.size);
709         BUG_ON(result);
710
711         result = dma_unmap_area(r, dma_lpar_to_bus(r, map.r1.base),
712                 map.r1.size);
713         BUG_ON(result);
714
715         result = dma_region_free(r);
716         BUG_ON(result);
717
718         return result;
719 }
720
721 /**
722  * dma_map_area_linear - Map an area of memory into a device dma region.
723  * @r: Pointer to a struct ps3_dma_region.
724  * @virt_addr: Starting virtual address of the area to map.
725  * @len: Length in bytes of the area to map.
726  * @bus_addr: A pointer to return the starting ioc bus address of the area to
727  * map.
728  *
729  * This routine just returns the coresponding bus address.  Actual mapping
730  * occurs in dma_region_create_linear().
731  */
732
733 static int dma_map_area_linear(struct ps3_dma_region *r,
734         unsigned long virt_addr, unsigned long len, unsigned long *bus_addr)
735 {
736         unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
737                 : virt_addr;
738         *bus_addr = dma_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
739         return 0;
740 }
741
742 /**
743  * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
744  * @r: Pointer to a struct ps3_dma_region.
745  * @bus_addr: The starting ioc bus address of the area to unmap.
746  * @len: Length in bytes of the area to unmap.
747  *
748  * This routine does nothing.  Unmapping occurs in dma_region_free_linear().
749  */
750
751 static int dma_unmap_area_linear(struct ps3_dma_region *r,
752         unsigned long bus_addr, unsigned long len)
753 {
754         return 0;
755 }
756
757 int ps3_dma_region_create(struct ps3_dma_region *r)
758 {
759         return (USE_DYNAMIC_DMA)
760                 ? dma_region_create(r)
761                 : dma_region_create_linear(r);
762 }
763
764 int ps3_dma_region_free(struct ps3_dma_region *r)
765 {
766         return (USE_DYNAMIC_DMA)
767                 ? dma_region_free(r)
768                 : dma_region_free_linear(r);
769 }
770
771 int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
772         unsigned long len, unsigned long *bus_addr)
773 {
774         return (USE_DYNAMIC_DMA)
775                 ? dma_map_area(r, virt_addr, len, bus_addr)
776                 : dma_map_area_linear(r, virt_addr, len, bus_addr);
777 }
778
779 int ps3_dma_unmap(struct ps3_dma_region *r, unsigned long bus_addr,
780         unsigned long len)
781 {
782         return (USE_DYNAMIC_DMA) ? dma_unmap_area(r, bus_addr, len)
783                 : dma_unmap_area_linear(r, bus_addr, len);
784 }
785
786 /*============================================================================*/
787 /* system startup routines                                                    */
788 /*============================================================================*/
789
790 /**
791  * ps3_mm_init - initialize the address space state variables
792  */
793
794 void __init ps3_mm_init(void)
795 {
796         int result;
797
798         DBG(" -> %s:%d\n", __func__, __LINE__);
799
800         result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
801                 &map.total);
802
803         if (result)
804                 panic("ps3_repository_read_mm_info() failed");
805
806         map.rm.offset = map.rm.base;
807         map.vas_id = map.htab_size = 0;
808
809         /* this implementation assumes map.rm.base is zero */
810
811         BUG_ON(map.rm.base);
812         BUG_ON(!map.rm.size);
813
814         lmb_add(map.rm.base, map.rm.size);
815         lmb_analyze();
816
817         /* arrange to do this in ps3_mm_add_memory */
818         ps3_mm_region_create(&map.r1, map.total - map.rm.size);
819
820         DBG(" <- %s:%d\n", __func__, __LINE__);
821 }
822
823 /**
824  * ps3_mm_shutdown - final cleanup of address space
825  */
826
827 void ps3_mm_shutdown(void)
828 {
829         ps3_mm_region_destroy(&map.r1);
830         map.total = map.rm.size;
831 }