1 // SPDX-License-Identifier: GPL-2.0-only
3 * PS3 address space management.
5 * Copyright (C) 2006 Sony Computer Entertainment Inc.
6 * Copyright 2006 Sony Corp.
9 #include <linux/dma-mapping.h>
10 #include <linux/kernel.h>
11 #include <linux/export.h>
12 #include <linux/memblock.h>
13 #include <linux/slab.h>
15 #include <asm/cell-regs.h>
16 #include <asm/firmware.h>
19 #include <asm/lv1call.h>
20 #include <asm/setup.h>
25 #define DBG udbg_printf
31 #if defined(CONFIG_PS3_DYNAMIC_DMA)
44 static unsigned long make_page_sizes(unsigned long a, unsigned long b)
46 return (a << 56) | (b << 48);
50 ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
51 ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
54 /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
57 HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
58 HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
61 /*============================================================================*/
62 /* virtual address space routines */
63 /*============================================================================*/
66 * struct mem_region - memory region structure
68 * @size: size in bytes
69 * @offset: difference between base and rm.size
70 * @destroy: flag if region should be destroyed upon shutdown
81 * struct map - address space state variables holder
82 * @total: total memory available as reported by HV
83 * @vas_id - HV virtual address space id
84 * @htab_size: htab size in bytes
86 * The HV virtual address space (vas) allows for hotplug memory regions.
87 * Memory regions can be created and destroyed in the vas at runtime.
88 * @rm: real mode (bootmem) region
89 * @r1: highmem region(s)
92 * virt_addr: a cpu 'translated' effective address
93 * phys_addr: an address in what Linux thinks is the physical address space
94 * lpar_addr: an address in the HV virtual address space
95 * bus_addr: an io controller 'translated' address on a device bus
102 struct mem_region rm;
103 struct mem_region r1;
106 #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
107 static void __maybe_unused _debug_dump_map(const struct map *m,
108 const char *func, int line)
110 DBG("%s:%d: map.total = %llxh\n", func, line, m->total);
111 DBG("%s:%d: map.rm.size = %llxh\n", func, line, m->rm.size);
112 DBG("%s:%d: map.vas_id = %llu\n", func, line, m->vas_id);
113 DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size);
114 DBG("%s:%d: map.r1.base = %llxh\n", func, line, m->r1.base);
115 DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
116 DBG("%s:%d: map.r1.size = %llxh\n", func, line, m->r1.size);
119 static struct map map;
122 * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
123 * @phys_addr: linux physical address
126 unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
128 BUG_ON(is_kernel_addr(phys_addr));
129 return (phys_addr < map.rm.size || phys_addr >= map.total)
130 ? phys_addr : phys_addr + map.r1.offset;
133 EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
136 * ps3_mm_vas_create - create the virtual address space
139 void __init ps3_mm_vas_create(unsigned long* htab_size)
148 result = lv1_query_logical_partition_address_region_info(0,
149 &start_address, &size, &access_right, &max_page_size,
153 DBG("%s:%d: lv1_query_logical_partition_address_region_info "
154 "failed: %s\n", __func__, __LINE__,
159 if (max_page_size < PAGE_SHIFT_16M) {
160 DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__,
165 BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
166 BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
168 result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
169 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
170 &map.vas_id, &map.htab_size);
173 DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
174 __func__, __LINE__, ps3_result(result));
178 result = lv1_select_virtual_address_space(map.vas_id);
181 DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
182 __func__, __LINE__, ps3_result(result));
186 *htab_size = map.htab_size;
188 debug_dump_map(&map);
193 panic("ps3_mm_vas_create failed");
197 * ps3_mm_vas_destroy -
199 * called during kexec sequence with MMU off.
202 notrace void ps3_mm_vas_destroy(void)
207 result = lv1_select_virtual_address_space(0);
208 result += lv1_destruct_virtual_address_space(map.vas_id);
218 static int ps3_mm_get_repository_highmem(struct mem_region *r)
222 /* Assume a single highmem region. */
224 result = ps3_repository_read_highmem_info(0, &r->base, &r->size);
229 if (!r->base || !r->size) {
234 r->offset = r->base - map.rm.size;
236 DBG("%s:%d: Found high region in repository: %llxh %llxh\n",
237 __func__, __LINE__, r->base, r->size);
242 DBG("%s:%d: No high region in repository.\n", __func__, __LINE__);
244 r->size = r->base = r->offset = 0;
248 static int ps3_mm_set_repository_highmem(const struct mem_region *r)
250 /* Assume a single highmem region. */
252 return r ? ps3_repository_write_highmem_info(0, r->base, r->size) :
253 ps3_repository_write_highmem_info(0, 0, 0);
257 * ps3_mm_region_create - create a memory region in the vas
258 * @r: pointer to a struct mem_region to accept initialized values
259 * @size: requested region size
261 * This implementation creates the region with the vas large page size.
262 * @size is rounded down to a multiple of the vas large page size.
265 static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
270 r->size = ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
272 DBG("%s:%d requested %lxh\n", __func__, __LINE__, size);
273 DBG("%s:%d actual %llxh\n", __func__, __LINE__, r->size);
274 DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__,
275 size - r->size, (size - r->size) / 1024 / 1024);
278 DBG("%s:%d: size == 0\n", __func__, __LINE__);
283 result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
284 ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
286 if (result || r->base < map.rm.size) {
287 DBG("%s:%d: lv1_allocate_memory failed: %s\n",
288 __func__, __LINE__, ps3_result(result));
293 r->offset = r->base - map.rm.size;
297 r->size = r->base = r->offset = 0;
302 * ps3_mm_region_destroy - destroy a memory region
303 * @r: pointer to struct mem_region
306 static void ps3_mm_region_destroy(struct mem_region *r)
315 result = lv1_release_memory(r->base);
321 r->size = r->base = r->offset = 0;
322 map.total = map.rm.size;
325 ps3_mm_set_repository_highmem(NULL);
328 /*============================================================================*/
330 /*============================================================================*/
333 * dma_sb_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
338 static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
339 unsigned long lpar_addr)
341 if (lpar_addr >= map.rm.size)
342 lpar_addr -= map.r1.offset;
343 BUG_ON(lpar_addr < r->offset);
344 BUG_ON(lpar_addr >= r->offset + r->len);
345 return r->bus_addr + lpar_addr - r->offset;
348 #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
349 static void __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
350 const char *func, int line)
352 DBG("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id,
354 DBG("%s:%d: page_size %u\n", func, line, r->page_size);
355 DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
356 DBG("%s:%d: len %lxh\n", func, line, r->len);
357 DBG("%s:%d: offset %lxh\n", func, line, r->offset);
361 * dma_chunk - A chunk of dma pages mapped by the io controller.
362 * @region - The dma region that owns this chunk.
363 * @lpar_addr: Starting lpar address of the area to map.
364 * @bus_addr: Starting ioc bus address of the area to map.
365 * @len: Length in bytes of the area to map.
366 * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
367 * list of all chuncks owned by the region.
369 * This implementation uses a very simple dma page manager
370 * based on the dma_chunk structure. This scheme assumes
371 * that all drivers use very well behaved dma ops.
375 struct ps3_dma_region *region;
376 unsigned long lpar_addr;
377 unsigned long bus_addr;
379 struct list_head link;
380 unsigned int usage_count;
383 #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
384 static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
387 DBG("%s:%d: r.dev %llu:%llu\n", func, line,
388 c->region->dev->bus_id, c->region->dev->dev_id);
389 DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr);
390 DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size);
391 DBG("%s:%d: r.len %lxh\n", func, line, c->region->len);
392 DBG("%s:%d: r.offset %lxh\n", func, line, c->region->offset);
393 DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr);
394 DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr);
395 DBG("%s:%d: c.len %lxh\n", func, line, c->len);
398 static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
399 unsigned long bus_addr, unsigned long len)
402 unsigned long aligned_bus = ALIGN_DOWN(bus_addr, 1 << r->page_size);
403 unsigned long aligned_len = ALIGN(len+bus_addr-aligned_bus,
406 list_for_each_entry(c, &r->chunk_list.head, link) {
408 if (aligned_bus >= c->bus_addr &&
409 aligned_bus + aligned_len <= c->bus_addr + c->len)
413 if (aligned_bus + aligned_len <= c->bus_addr)
417 if (aligned_bus >= c->bus_addr + c->len)
420 /* we don't handle the multi-chunk case for now */
427 static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
428 unsigned long lpar_addr, unsigned long len)
431 unsigned long aligned_lpar = ALIGN_DOWN(lpar_addr, 1 << r->page_size);
432 unsigned long aligned_len = ALIGN(len + lpar_addr - aligned_lpar,
435 list_for_each_entry(c, &r->chunk_list.head, link) {
437 if (c->lpar_addr <= aligned_lpar &&
438 aligned_lpar < c->lpar_addr + c->len) {
439 if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
447 if (aligned_lpar + aligned_len <= c->lpar_addr) {
451 if (c->lpar_addr + c->len <= aligned_lpar) {
458 static int dma_sb_free_chunk(struct dma_chunk *c)
463 result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
464 c->region->dev->dev_id, c->bus_addr, c->len);
472 static int dma_ioc0_free_chunk(struct dma_chunk *c)
476 unsigned long offset;
477 struct ps3_dma_region *r = c->region;
479 DBG("%s:start\n", __func__);
480 for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
481 offset = (1 << r->page_size) * iopage;
482 /* put INVALID entry */
483 result = lv1_put_iopte(0,
484 c->bus_addr + offset,
485 c->lpar_addr + offset,
488 DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
489 c->bus_addr + offset,
490 c->lpar_addr + offset,
494 DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
495 __LINE__, ps3_result(result));
499 DBG("%s:end\n", __func__);
504 * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
505 * @r: Pointer to a struct ps3_dma_region.
506 * @phys_addr: Starting physical address of the area to map.
507 * @len: Length in bytes of the area to map.
508 * c_out: A pointer to receive an allocated struct dma_chunk for this area.
510 * This is the lowest level dma mapping routine, and is the one that will
511 * make the HV call to add the pages into the io controller address space.
514 static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
515 unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
520 c = kzalloc(sizeof(*c), GFP_ATOMIC);
527 c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
528 c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
531 BUG_ON(iopte_flag != 0xf800000000000000UL);
532 result = lv1_map_device_dma_region(c->region->dev->bus_id,
533 c->region->dev->dev_id, c->lpar_addr,
534 c->bus_addr, c->len, iopte_flag);
536 DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
537 __func__, __LINE__, ps3_result(result));
541 list_add(&c->link, &r->chunk_list.head);
550 DBG(" <- %s:%d\n", __func__, __LINE__);
554 static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
555 unsigned long len, struct dma_chunk **c_out,
559 struct dma_chunk *c, *last;
561 unsigned long offset;
563 DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
564 phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
565 c = kzalloc(sizeof(*c), GFP_ATOMIC);
573 c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
574 /* allocate IO address */
575 if (list_empty(&r->chunk_list.head)) {
577 c->bus_addr = r->bus_addr;
579 /* derive from last bus addr*/
580 last = list_entry(r->chunk_list.head.next,
581 struct dma_chunk, link);
582 c->bus_addr = last->bus_addr + last->len;
583 DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
584 last->bus_addr, last->len);
587 /* FIXME: check whether length exceeds region size */
589 /* build ioptes for the area */
590 pages = len >> r->page_size;
591 DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__,
592 r->page_size, r->len, pages, iopte_flag);
593 for (iopage = 0; iopage < pages; iopage++) {
594 offset = (1 << r->page_size) * iopage;
595 result = lv1_put_iopte(0,
596 c->bus_addr + offset,
597 c->lpar_addr + offset,
601 pr_warn("%s:%d: lv1_put_iopte failed: %s\n",
602 __func__, __LINE__, ps3_result(result));
605 DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
606 iopage, c->bus_addr + offset, c->lpar_addr + offset,
610 /* be sure that last allocated one is inserted at head */
611 list_add(&c->link, &r->chunk_list.head);
614 DBG("%s: end\n", __func__);
618 for (iopage--; 0 <= iopage; iopage--) {
620 c->bus_addr + offset,
621 c->lpar_addr + offset,
632 * dma_sb_region_create - Create a device dma region.
633 * @r: Pointer to a struct ps3_dma_region.
635 * This is the lowest level dma region create routine, and is the one that
636 * will make the HV call to create the region.
639 static int dma_sb_region_create(struct ps3_dma_region *r)
644 DBG(" -> %s:%d:\n", __func__, __LINE__);
648 if (!r->dev->bus_id) {
649 pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
650 r->dev->bus_id, r->dev->dev_id);
654 DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
655 __LINE__, r->len, r->page_size, r->offset);
658 BUG_ON(!r->page_size);
659 BUG_ON(!r->region_ops);
661 INIT_LIST_HEAD(&r->chunk_list.head);
662 spin_lock_init(&r->chunk_list.lock);
664 result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
665 roundup_pow_of_two(r->len), r->page_size, r->region_type,
667 r->bus_addr = bus_addr;
670 DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
671 __func__, __LINE__, ps3_result(result));
672 r->len = r->bus_addr = 0;
678 static int dma_ioc0_region_create(struct ps3_dma_region *r)
683 INIT_LIST_HEAD(&r->chunk_list.head);
684 spin_lock_init(&r->chunk_list.lock);
686 result = lv1_allocate_io_segment(0,
690 r->bus_addr = bus_addr;
692 DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
693 __func__, __LINE__, ps3_result(result));
694 r->len = r->bus_addr = 0;
696 DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
697 r->len, r->page_size, r->bus_addr);
702 * dma_region_free - Free a device dma region.
703 * @r: Pointer to a struct ps3_dma_region.
705 * This is the lowest level dma region free routine, and is the one that
706 * will make the HV call to free the region.
709 static int dma_sb_region_free(struct ps3_dma_region *r)
713 struct dma_chunk *tmp;
717 if (!r->dev->bus_id) {
718 pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
719 r->dev->bus_id, r->dev->dev_id);
723 list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
725 dma_sb_free_chunk(c);
728 result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
732 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
733 __func__, __LINE__, ps3_result(result));
740 static int dma_ioc0_region_free(struct ps3_dma_region *r)
743 struct dma_chunk *c, *n;
745 DBG("%s: start\n", __func__);
746 list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
748 dma_ioc0_free_chunk(c);
751 result = lv1_release_io_segment(0, r->bus_addr);
754 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
755 __func__, __LINE__, ps3_result(result));
758 DBG("%s: end\n", __func__);
764 * dma_sb_map_area - Map an area of memory into a device dma region.
765 * @r: Pointer to a struct ps3_dma_region.
766 * @virt_addr: Starting virtual address of the area to map.
767 * @len: Length in bytes of the area to map.
768 * @bus_addr: A pointer to return the starting ioc bus address of the area to
771 * This is the common dma mapping routine.
774 static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
775 unsigned long len, dma_addr_t *bus_addr,
781 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
783 unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size);
784 unsigned long aligned_len = ALIGN(len + phys_addr - aligned_phys,
786 *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
788 if (!USE_DYNAMIC_DMA) {
789 unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
790 DBG(" -> %s:%d\n", __func__, __LINE__);
791 DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
793 DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
795 DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
797 DBG("%s:%d len %lxh\n", __func__, __LINE__, len);
798 DBG("%s:%d bus_addr %llxh (%lxh)\n", __func__, __LINE__,
802 spin_lock_irqsave(&r->chunk_list.lock, flags);
803 c = dma_find_chunk(r, *bus_addr, len);
806 DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
809 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
813 result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
817 DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
818 __func__, __LINE__, result);
819 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
825 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
829 static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
830 unsigned long len, dma_addr_t *bus_addr,
836 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
838 unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size);
839 unsigned long aligned_len = ALIGN(len + phys_addr - aligned_phys,
842 DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
844 DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
845 phys_addr, aligned_phys, aligned_len);
847 spin_lock_irqsave(&r->chunk_list.lock, flags);
848 c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
853 *bus_addr = c->bus_addr + phys_addr - aligned_phys;
855 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
859 result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
864 DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
865 __func__, __LINE__, result);
866 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
869 *bus_addr = c->bus_addr + phys_addr - aligned_phys;
870 DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__,
871 virt_addr, phys_addr, aligned_phys, *bus_addr);
874 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
879 * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
880 * @r: Pointer to a struct ps3_dma_region.
881 * @bus_addr: The starting ioc bus address of the area to unmap.
882 * @len: Length in bytes of the area to unmap.
884 * This is the common dma unmap routine.
887 static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr,
893 spin_lock_irqsave(&r->chunk_list.lock, flags);
894 c = dma_find_chunk(r, bus_addr, len);
897 unsigned long aligned_bus = ALIGN_DOWN(bus_addr,
899 unsigned long aligned_len = ALIGN(len + bus_addr
900 - aligned_bus, 1 << r->page_size);
901 DBG("%s:%d: not found: bus_addr %llxh\n",
902 __func__, __LINE__, bus_addr);
903 DBG("%s:%d: not found: len %lxh\n",
904 __func__, __LINE__, len);
905 DBG("%s:%d: not found: aligned_bus %lxh\n",
906 __func__, __LINE__, aligned_bus);
907 DBG("%s:%d: not found: aligned_len %lxh\n",
908 __func__, __LINE__, aligned_len);
914 if (!c->usage_count) {
916 dma_sb_free_chunk(c);
919 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
923 static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
924 dma_addr_t bus_addr, unsigned long len)
929 DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len);
930 spin_lock_irqsave(&r->chunk_list.lock, flags);
931 c = dma_find_chunk(r, bus_addr, len);
934 unsigned long aligned_bus = ALIGN_DOWN(bus_addr,
936 unsigned long aligned_len = ALIGN(len + bus_addr
939 DBG("%s:%d: not found: bus_addr %llxh\n",
940 __func__, __LINE__, bus_addr);
941 DBG("%s:%d: not found: len %lxh\n",
942 __func__, __LINE__, len);
943 DBG("%s:%d: not found: aligned_bus %lxh\n",
944 __func__, __LINE__, aligned_bus);
945 DBG("%s:%d: not found: aligned_len %lxh\n",
946 __func__, __LINE__, aligned_len);
952 if (!c->usage_count) {
954 dma_ioc0_free_chunk(c);
957 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
958 DBG("%s: end\n", __func__);
963 * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
964 * @r: Pointer to a struct ps3_dma_region.
966 * This routine creates an HV dma region for the device and maps all available
967 * ram into the io controller bus address space.
970 static int dma_sb_region_create_linear(struct ps3_dma_region *r)
973 unsigned long virt_addr, len;
976 if (r->len > 16*1024*1024) { /* FIXME: need proper fix */
977 /* force 16M dma pages for linear mapping */
978 if (r->page_size != PS3_DMA_16M) {
979 pr_info("%s:%d: forcing 16M pages for linear map\n",
981 r->page_size = PS3_DMA_16M;
982 r->len = ALIGN(r->len, 1 << r->page_size);
986 result = dma_sb_region_create(r);
989 if (r->offset < map.rm.size) {
990 /* Map (part of) 1st RAM chunk */
991 virt_addr = map.rm.base + r->offset;
992 len = map.rm.size - r->offset;
995 result = dma_sb_map_area(r, virt_addr, len, &tmp,
996 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
1001 if (r->offset + r->len > map.rm.size) {
1002 /* Map (part of) 2nd RAM chunk */
1003 virt_addr = map.rm.size;
1005 if (r->offset >= map.rm.size)
1006 virt_addr += r->offset - map.rm.size;
1008 len -= map.rm.size - r->offset;
1009 result = dma_sb_map_area(r, virt_addr, len, &tmp,
1010 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
1019 * dma_sb_region_free_linear - Free a linear dma mapping for a device.
1020 * @r: Pointer to a struct ps3_dma_region.
1022 * This routine will unmap all mapped areas and free the HV dma region.
1025 static int dma_sb_region_free_linear(struct ps3_dma_region *r)
1028 dma_addr_t bus_addr;
1029 unsigned long len, lpar_addr;
1031 if (r->offset < map.rm.size) {
1032 /* Unmap (part of) 1st RAM chunk */
1033 lpar_addr = map.rm.base + r->offset;
1034 len = map.rm.size - r->offset;
1037 bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1038 result = dma_sb_unmap_area(r, bus_addr, len);
1042 if (r->offset + r->len > map.rm.size) {
1043 /* Unmap (part of) 2nd RAM chunk */
1044 lpar_addr = map.r1.base;
1046 if (r->offset >= map.rm.size)
1047 lpar_addr += r->offset - map.rm.size;
1049 len -= map.rm.size - r->offset;
1050 bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1051 result = dma_sb_unmap_area(r, bus_addr, len);
1055 result = dma_sb_region_free(r);
1062 * dma_sb_map_area_linear - Map an area of memory into a device dma region.
1063 * @r: Pointer to a struct ps3_dma_region.
1064 * @virt_addr: Starting virtual address of the area to map.
1065 * @len: Length in bytes of the area to map.
1066 * @bus_addr: A pointer to return the starting ioc bus address of the area to
1069 * This routine just returns the corresponding bus address. Actual mapping
1070 * occurs in dma_region_create_linear().
1073 static int dma_sb_map_area_linear(struct ps3_dma_region *r,
1074 unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr,
1077 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
1079 *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
1084 * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
1085 * @r: Pointer to a struct ps3_dma_region.
1086 * @bus_addr: The starting ioc bus address of the area to unmap.
1087 * @len: Length in bytes of the area to unmap.
1089 * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear().
1092 static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
1093 dma_addr_t bus_addr, unsigned long len)
1098 static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = {
1099 .create = dma_sb_region_create,
1100 .free = dma_sb_region_free,
1101 .map = dma_sb_map_area,
1102 .unmap = dma_sb_unmap_area
1105 static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
1106 .create = dma_sb_region_create_linear,
1107 .free = dma_sb_region_free_linear,
1108 .map = dma_sb_map_area_linear,
1109 .unmap = dma_sb_unmap_area_linear
1112 static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
1113 .create = dma_ioc0_region_create,
1114 .free = dma_ioc0_region_free,
1115 .map = dma_ioc0_map_area,
1116 .unmap = dma_ioc0_unmap_area
1119 int ps3_dma_region_init(struct ps3_system_bus_device *dev,
1120 struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
1121 enum ps3_dma_region_type region_type, void *addr, unsigned long len)
1123 unsigned long lpar_addr;
1126 lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
1129 r->page_size = page_size;
1130 r->region_type = region_type;
1131 r->offset = lpar_addr;
1132 if (r->offset >= map.rm.size)
1133 r->offset -= map.r1.offset;
1134 r->len = len ? len : ALIGN(map.total, 1 << r->page_size);
1136 dev->core.dma_mask = &r->dma_mask;
1138 result = dma_set_mask_and_coherent(&dev->core, DMA_BIT_MASK(32));
1141 dev_err(&dev->core, "%s:%d: dma_set_mask_and_coherent failed: %d\n",
1142 __func__, __LINE__, result);
1146 switch (dev->dev_type) {
1147 case PS3_DEVICE_TYPE_SB:
1148 r->region_ops = (USE_DYNAMIC_DMA)
1149 ? &ps3_dma_sb_region_ops
1150 : &ps3_dma_sb_region_linear_ops;
1152 case PS3_DEVICE_TYPE_IOC0:
1153 r->region_ops = &ps3_dma_ioc0_region_ops;
1161 EXPORT_SYMBOL(ps3_dma_region_init);
1163 int ps3_dma_region_create(struct ps3_dma_region *r)
1166 BUG_ON(!r->region_ops);
1167 BUG_ON(!r->region_ops->create);
1168 return r->region_ops->create(r);
1170 EXPORT_SYMBOL(ps3_dma_region_create);
1172 int ps3_dma_region_free(struct ps3_dma_region *r)
1175 BUG_ON(!r->region_ops);
1176 BUG_ON(!r->region_ops->free);
1177 return r->region_ops->free(r);
1179 EXPORT_SYMBOL(ps3_dma_region_free);
1181 int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
1182 unsigned long len, dma_addr_t *bus_addr,
1185 return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
1188 int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr,
1191 return r->region_ops->unmap(r, bus_addr, len);
1194 /*============================================================================*/
1195 /* system startup routines */
1196 /*============================================================================*/
1199 * ps3_mm_init - initialize the address space state variables
1202 void __init ps3_mm_init(void)
1206 DBG(" -> %s:%d\n", __func__, __LINE__);
1208 result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
1212 panic("ps3_repository_read_mm_info() failed");
1214 map.rm.offset = map.rm.base;
1215 map.vas_id = map.htab_size = 0;
1217 /* this implementation assumes map.rm.base is zero */
1219 BUG_ON(map.rm.base);
1220 BUG_ON(!map.rm.size);
1222 /* Check if we got the highmem region from an earlier boot step */
1224 if (ps3_mm_get_repository_highmem(&map.r1)) {
1225 result = ps3_mm_region_create(&map.r1, map.total - map.rm.size);
1228 ps3_mm_set_repository_highmem(&map.r1);
1231 /* correct map.total for the real total amount of memory we use */
1232 map.total = map.rm.size + map.r1.size;
1235 DBG("%s:%d: No highmem region found\n", __func__, __LINE__);
1237 DBG("%s:%d: Adding highmem region: %llxh %llxh\n",
1238 __func__, __LINE__, map.rm.size,
1239 map.total - map.rm.size);
1240 memblock_add(map.rm.size, map.total - map.rm.size);
1243 DBG(" <- %s:%d\n", __func__, __LINE__);
1247 * ps3_mm_shutdown - final cleanup of address space
1249 * called during kexec sequence with MMU off.
1252 notrace void ps3_mm_shutdown(void)
1254 ps3_mm_region_destroy(&map.r1);