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/kernel.h>
10 #include <linux/export.h>
11 #include <linux/memblock.h>
12 #include <linux/slab.h>
14 #include <asm/cell-regs.h>
15 #include <asm/firmware.h>
18 #include <asm/lv1call.h>
19 #include <asm/setup.h>
24 #define DBG udbg_printf
30 #if defined(CONFIG_PS3_DYNAMIC_DMA)
43 static unsigned long make_page_sizes(unsigned long a, unsigned long b)
45 return (a << 56) | (b << 48);
49 ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
50 ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
53 /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
56 HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
57 HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
60 /*============================================================================*/
61 /* virtual address space routines */
62 /*============================================================================*/
65 * struct mem_region - memory region structure
67 * @size: size in bytes
68 * @offset: difference between base and rm.size
69 * @destroy: flag if region should be destroyed upon shutdown
80 * struct map - address space state variables holder
81 * @total: total memory available as reported by HV
82 * @vas_id - HV virtual address space id
83 * @htab_size: htab size in bytes
85 * The HV virtual address space (vas) allows for hotplug memory regions.
86 * Memory regions can be created and destroyed in the vas at runtime.
87 * @rm: real mode (bootmem) region
88 * @r1: highmem region(s)
91 * virt_addr: a cpu 'translated' effective address
92 * phys_addr: an address in what Linux thinks is the physical address space
93 * lpar_addr: an address in the HV virtual address space
94 * bus_addr: an io controller 'translated' address on a device bus
101 struct mem_region rm;
102 struct mem_region r1;
105 #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
106 static void __maybe_unused _debug_dump_map(const struct map *m,
107 const char *func, int line)
109 DBG("%s:%d: map.total = %llxh\n", func, line, m->total);
110 DBG("%s:%d: map.rm.size = %llxh\n", func, line, m->rm.size);
111 DBG("%s:%d: map.vas_id = %llu\n", func, line, m->vas_id);
112 DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size);
113 DBG("%s:%d: map.r1.base = %llxh\n", func, line, m->r1.base);
114 DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
115 DBG("%s:%d: map.r1.size = %llxh\n", func, line, m->r1.size);
118 static struct map map;
121 * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
122 * @phys_addr: linux physical address
125 unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
127 BUG_ON(is_kernel_addr(phys_addr));
128 return (phys_addr < map.rm.size || phys_addr >= map.total)
129 ? phys_addr : phys_addr + map.r1.offset;
132 EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
135 * ps3_mm_vas_create - create the virtual address space
138 void __init ps3_mm_vas_create(unsigned long* htab_size)
147 result = lv1_query_logical_partition_address_region_info(0,
148 &start_address, &size, &access_right, &max_page_size,
152 DBG("%s:%d: lv1_query_logical_partition_address_region_info "
153 "failed: %s\n", __func__, __LINE__,
158 if (max_page_size < PAGE_SHIFT_16M) {
159 DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__,
164 BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
165 BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
167 result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
168 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
169 &map.vas_id, &map.htab_size);
172 DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
173 __func__, __LINE__, ps3_result(result));
177 result = lv1_select_virtual_address_space(map.vas_id);
180 DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
181 __func__, __LINE__, ps3_result(result));
185 *htab_size = map.htab_size;
187 debug_dump_map(&map);
192 panic("ps3_mm_vas_create failed");
196 * ps3_mm_vas_destroy -
199 void ps3_mm_vas_destroy(void)
203 DBG("%s:%d: map.vas_id = %llu\n", __func__, __LINE__, map.vas_id);
206 result = lv1_select_virtual_address_space(0);
208 result = lv1_destruct_virtual_address_space(map.vas_id);
214 static int ps3_mm_get_repository_highmem(struct mem_region *r)
218 /* Assume a single highmem region. */
220 result = ps3_repository_read_highmem_info(0, &r->base, &r->size);
225 if (!r->base || !r->size) {
230 r->offset = r->base - map.rm.size;
232 DBG("%s:%d: Found high region in repository: %llxh %llxh\n",
233 __func__, __LINE__, r->base, r->size);
238 DBG("%s:%d: No high region in repository.\n", __func__, __LINE__);
240 r->size = r->base = r->offset = 0;
244 static int ps3_mm_set_repository_highmem(const struct mem_region *r)
246 /* Assume a single highmem region. */
248 return r ? ps3_repository_write_highmem_info(0, r->base, r->size) :
249 ps3_repository_write_highmem_info(0, 0, 0);
253 * ps3_mm_region_create - create a memory region in the vas
254 * @r: pointer to a struct mem_region to accept initialized values
255 * @size: requested region size
257 * This implementation creates the region with the vas large page size.
258 * @size is rounded down to a multiple of the vas large page size.
261 static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
266 r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
268 DBG("%s:%d requested %lxh\n", __func__, __LINE__, size);
269 DBG("%s:%d actual %llxh\n", __func__, __LINE__, r->size);
270 DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__,
271 size - r->size, (size - r->size) / 1024 / 1024);
274 DBG("%s:%d: size == 0\n", __func__, __LINE__);
279 result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
280 ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
282 if (result || r->base < map.rm.size) {
283 DBG("%s:%d: lv1_allocate_memory failed: %s\n",
284 __func__, __LINE__, ps3_result(result));
289 r->offset = r->base - map.rm.size;
293 r->size = r->base = r->offset = 0;
298 * ps3_mm_region_destroy - destroy a memory region
299 * @r: pointer to struct mem_region
302 static void ps3_mm_region_destroy(struct mem_region *r)
307 pr_info("%s:%d: Not destroying high region: %llxh %llxh\n",
308 __func__, __LINE__, r->base, r->size);
312 DBG("%s:%d: r->base = %llxh\n", __func__, __LINE__, r->base);
315 result = lv1_release_memory(r->base);
317 r->size = r->base = r->offset = 0;
318 map.total = map.rm.size;
320 ps3_mm_set_repository_highmem(NULL);
323 /*============================================================================*/
325 /*============================================================================*/
328 * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
329 * @r: pointer to dma region structure
330 * @lpar_addr: HV lpar address
333 static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
334 unsigned long lpar_addr)
336 if (lpar_addr >= map.rm.size)
337 lpar_addr -= map.r1.offset;
338 BUG_ON(lpar_addr < r->offset);
339 BUG_ON(lpar_addr >= r->offset + r->len);
340 return r->bus_addr + lpar_addr - r->offset;
343 #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
344 static void __maybe_unused _dma_dump_region(const struct ps3_dma_region *r,
345 const char *func, int line)
347 DBG("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id,
349 DBG("%s:%d: page_size %u\n", func, line, r->page_size);
350 DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
351 DBG("%s:%d: len %lxh\n", func, line, r->len);
352 DBG("%s:%d: offset %lxh\n", func, line, r->offset);
356 * dma_chunk - A chunk of dma pages mapped by the io controller.
357 * @region - The dma region that owns this chunk.
358 * @lpar_addr: Starting lpar address of the area to map.
359 * @bus_addr: Starting ioc bus address of the area to map.
360 * @len: Length in bytes of the area to map.
361 * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
362 * list of all chuncks owned by the region.
364 * This implementation uses a very simple dma page manager
365 * based on the dma_chunk structure. This scheme assumes
366 * that all drivers use very well behaved dma ops.
370 struct ps3_dma_region *region;
371 unsigned long lpar_addr;
372 unsigned long bus_addr;
374 struct list_head link;
375 unsigned int usage_count;
378 #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
379 static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
382 DBG("%s:%d: r.dev %llu:%llu\n", func, line,
383 c->region->dev->bus_id, c->region->dev->dev_id);
384 DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr);
385 DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size);
386 DBG("%s:%d: r.len %lxh\n", func, line, c->region->len);
387 DBG("%s:%d: r.offset %lxh\n", func, line, c->region->offset);
388 DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr);
389 DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr);
390 DBG("%s:%d: c.len %lxh\n", func, line, c->len);
393 static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
394 unsigned long bus_addr, unsigned long len)
397 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 1 << r->page_size);
398 unsigned long aligned_len = _ALIGN_UP(len+bus_addr-aligned_bus,
401 list_for_each_entry(c, &r->chunk_list.head, link) {
403 if (aligned_bus >= c->bus_addr &&
404 aligned_bus + aligned_len <= c->bus_addr + c->len)
408 if (aligned_bus + aligned_len <= c->bus_addr)
412 if (aligned_bus >= c->bus_addr + c->len)
415 /* we don't handle the multi-chunk case for now */
422 static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
423 unsigned long lpar_addr, unsigned long len)
426 unsigned long aligned_lpar = _ALIGN_DOWN(lpar_addr, 1 << r->page_size);
427 unsigned long aligned_len = _ALIGN_UP(len + lpar_addr - aligned_lpar,
430 list_for_each_entry(c, &r->chunk_list.head, link) {
432 if (c->lpar_addr <= aligned_lpar &&
433 aligned_lpar < c->lpar_addr + c->len) {
434 if (aligned_lpar + aligned_len <= c->lpar_addr + c->len)
442 if (aligned_lpar + aligned_len <= c->lpar_addr) {
446 if (c->lpar_addr + c->len <= aligned_lpar) {
453 static int dma_sb_free_chunk(struct dma_chunk *c)
458 result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
459 c->region->dev->dev_id, c->bus_addr, c->len);
467 static int dma_ioc0_free_chunk(struct dma_chunk *c)
471 unsigned long offset;
472 struct ps3_dma_region *r = c->region;
474 DBG("%s:start\n", __func__);
475 for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) {
476 offset = (1 << r->page_size) * iopage;
477 /* put INVALID entry */
478 result = lv1_put_iopte(0,
479 c->bus_addr + offset,
480 c->lpar_addr + offset,
483 DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
484 c->bus_addr + offset,
485 c->lpar_addr + offset,
489 DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
490 __LINE__, ps3_result(result));
494 DBG("%s:end\n", __func__);
499 * dma_sb_map_pages - Maps dma pages into the io controller bus address space.
500 * @r: Pointer to a struct ps3_dma_region.
501 * @phys_addr: Starting physical address of the area to map.
502 * @len: Length in bytes of the area to map.
503 * c_out: A pointer to receive an allocated struct dma_chunk for this area.
505 * This is the lowest level dma mapping routine, and is the one that will
506 * make the HV call to add the pages into the io controller address space.
509 static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
510 unsigned long len, struct dma_chunk **c_out, u64 iopte_flag)
515 c = kzalloc(sizeof(*c), GFP_ATOMIC);
522 c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
523 c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
526 BUG_ON(iopte_flag != 0xf800000000000000UL);
527 result = lv1_map_device_dma_region(c->region->dev->bus_id,
528 c->region->dev->dev_id, c->lpar_addr,
529 c->bus_addr, c->len, iopte_flag);
531 DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
532 __func__, __LINE__, ps3_result(result));
536 list_add(&c->link, &r->chunk_list.head);
545 DBG(" <- %s:%d\n", __func__, __LINE__);
549 static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
550 unsigned long len, struct dma_chunk **c_out,
554 struct dma_chunk *c, *last;
556 unsigned long offset;
558 DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
559 phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
560 c = kzalloc(sizeof(*c), GFP_ATOMIC);
568 c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
569 /* allocate IO address */
570 if (list_empty(&r->chunk_list.head)) {
572 c->bus_addr = r->bus_addr;
574 /* derive from last bus addr*/
575 last = list_entry(r->chunk_list.head.next,
576 struct dma_chunk, link);
577 c->bus_addr = last->bus_addr + last->len;
578 DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
579 last->bus_addr, last->len);
582 /* FIXME: check whether length exceeds region size */
584 /* build ioptes for the area */
585 pages = len >> r->page_size;
586 DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__,
587 r->page_size, r->len, pages, iopte_flag);
588 for (iopage = 0; iopage < pages; iopage++) {
589 offset = (1 << r->page_size) * iopage;
590 result = lv1_put_iopte(0,
591 c->bus_addr + offset,
592 c->lpar_addr + offset,
596 pr_warn("%s:%d: lv1_put_iopte failed: %s\n",
597 __func__, __LINE__, ps3_result(result));
600 DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
601 iopage, c->bus_addr + offset, c->lpar_addr + offset,
605 /* be sure that last allocated one is inserted at head */
606 list_add(&c->link, &r->chunk_list.head);
609 DBG("%s: end\n", __func__);
613 for (iopage--; 0 <= iopage; iopage--) {
615 c->bus_addr + offset,
616 c->lpar_addr + offset,
627 * dma_sb_region_create - Create a device dma region.
628 * @r: Pointer to a struct ps3_dma_region.
630 * This is the lowest level dma region create routine, and is the one that
631 * will make the HV call to create the region.
634 static int dma_sb_region_create(struct ps3_dma_region *r)
639 DBG(" -> %s:%d:\n", __func__, __LINE__);
643 if (!r->dev->bus_id) {
644 pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
645 r->dev->bus_id, r->dev->dev_id);
649 DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
650 __LINE__, r->len, r->page_size, r->offset);
653 BUG_ON(!r->page_size);
654 BUG_ON(!r->region_ops);
656 INIT_LIST_HEAD(&r->chunk_list.head);
657 spin_lock_init(&r->chunk_list.lock);
659 result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
660 roundup_pow_of_two(r->len), r->page_size, r->region_type,
662 r->bus_addr = bus_addr;
665 DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
666 __func__, __LINE__, ps3_result(result));
667 r->len = r->bus_addr = 0;
673 static int dma_ioc0_region_create(struct ps3_dma_region *r)
678 INIT_LIST_HEAD(&r->chunk_list.head);
679 spin_lock_init(&r->chunk_list.lock);
681 result = lv1_allocate_io_segment(0,
685 r->bus_addr = bus_addr;
687 DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
688 __func__, __LINE__, ps3_result(result));
689 r->len = r->bus_addr = 0;
691 DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
692 r->len, r->page_size, r->bus_addr);
697 * dma_region_free - Free a device dma region.
698 * @r: Pointer to a struct ps3_dma_region.
700 * This is the lowest level dma region free routine, and is the one that
701 * will make the HV call to free the region.
704 static int dma_sb_region_free(struct ps3_dma_region *r)
708 struct dma_chunk *tmp;
712 if (!r->dev->bus_id) {
713 pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
714 r->dev->bus_id, r->dev->dev_id);
718 list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
720 dma_sb_free_chunk(c);
723 result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
727 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
728 __func__, __LINE__, ps3_result(result));
735 static int dma_ioc0_region_free(struct ps3_dma_region *r)
738 struct dma_chunk *c, *n;
740 DBG("%s: start\n", __func__);
741 list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
743 dma_ioc0_free_chunk(c);
746 result = lv1_release_io_segment(0, r->bus_addr);
749 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
750 __func__, __LINE__, ps3_result(result));
753 DBG("%s: end\n", __func__);
759 * dma_sb_map_area - Map an area of memory into a device dma region.
760 * @r: Pointer to a struct ps3_dma_region.
761 * @virt_addr: Starting virtual address of the area to map.
762 * @len: Length in bytes of the area to map.
763 * @bus_addr: A pointer to return the starting ioc bus address of the area to
766 * This is the common dma mapping routine.
769 static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
770 unsigned long len, dma_addr_t *bus_addr,
776 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
778 unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
779 unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
781 *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
783 if (!USE_DYNAMIC_DMA) {
784 unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
785 DBG(" -> %s:%d\n", __func__, __LINE__);
786 DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
788 DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
790 DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
792 DBG("%s:%d len %lxh\n", __func__, __LINE__, len);
793 DBG("%s:%d bus_addr %llxh (%lxh)\n", __func__, __LINE__,
797 spin_lock_irqsave(&r->chunk_list.lock, flags);
798 c = dma_find_chunk(r, *bus_addr, len);
801 DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
804 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
808 result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
812 DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
813 __func__, __LINE__, result);
814 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
820 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
824 static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
825 unsigned long len, dma_addr_t *bus_addr,
831 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
833 unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
834 unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
837 DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
839 DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
840 phys_addr, aligned_phys, aligned_len);
842 spin_lock_irqsave(&r->chunk_list.lock, flags);
843 c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
848 *bus_addr = c->bus_addr + phys_addr - aligned_phys;
850 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
854 result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
859 DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
860 __func__, __LINE__, result);
861 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
864 *bus_addr = c->bus_addr + phys_addr - aligned_phys;
865 DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__,
866 virt_addr, phys_addr, aligned_phys, *bus_addr);
869 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
874 * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
875 * @r: Pointer to a struct ps3_dma_region.
876 * @bus_addr: The starting ioc bus address of the area to unmap.
877 * @len: Length in bytes of the area to unmap.
879 * This is the common dma unmap routine.
882 static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr,
888 spin_lock_irqsave(&r->chunk_list.lock, flags);
889 c = dma_find_chunk(r, bus_addr, len);
892 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
894 unsigned long aligned_len = _ALIGN_UP(len + bus_addr
895 - aligned_bus, 1 << r->page_size);
896 DBG("%s:%d: not found: bus_addr %llxh\n",
897 __func__, __LINE__, bus_addr);
898 DBG("%s:%d: not found: len %lxh\n",
899 __func__, __LINE__, len);
900 DBG("%s:%d: not found: aligned_bus %lxh\n",
901 __func__, __LINE__, aligned_bus);
902 DBG("%s:%d: not found: aligned_len %lxh\n",
903 __func__, __LINE__, aligned_len);
909 if (!c->usage_count) {
911 dma_sb_free_chunk(c);
914 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
918 static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
919 dma_addr_t bus_addr, unsigned long len)
924 DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len);
925 spin_lock_irqsave(&r->chunk_list.lock, flags);
926 c = dma_find_chunk(r, bus_addr, len);
929 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
931 unsigned long aligned_len = _ALIGN_UP(len + bus_addr
934 DBG("%s:%d: not found: bus_addr %llxh\n",
935 __func__, __LINE__, bus_addr);
936 DBG("%s:%d: not found: len %lxh\n",
937 __func__, __LINE__, len);
938 DBG("%s:%d: not found: aligned_bus %lxh\n",
939 __func__, __LINE__, aligned_bus);
940 DBG("%s:%d: not found: aligned_len %lxh\n",
941 __func__, __LINE__, aligned_len);
947 if (!c->usage_count) {
949 dma_ioc0_free_chunk(c);
952 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
953 DBG("%s: end\n", __func__);
958 * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
959 * @r: Pointer to a struct ps3_dma_region.
961 * This routine creates an HV dma region for the device and maps all available
962 * ram into the io controller bus address space.
965 static int dma_sb_region_create_linear(struct ps3_dma_region *r)
968 unsigned long virt_addr, len;
971 if (r->len > 16*1024*1024) { /* FIXME: need proper fix */
972 /* force 16M dma pages for linear mapping */
973 if (r->page_size != PS3_DMA_16M) {
974 pr_info("%s:%d: forcing 16M pages for linear map\n",
976 r->page_size = PS3_DMA_16M;
977 r->len = _ALIGN_UP(r->len, 1 << r->page_size);
981 result = dma_sb_region_create(r);
984 if (r->offset < map.rm.size) {
985 /* Map (part of) 1st RAM chunk */
986 virt_addr = map.rm.base + r->offset;
987 len = map.rm.size - r->offset;
990 result = dma_sb_map_area(r, virt_addr, len, &tmp,
991 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
996 if (r->offset + r->len > map.rm.size) {
997 /* Map (part of) 2nd RAM chunk */
998 virt_addr = map.rm.size;
1000 if (r->offset >= map.rm.size)
1001 virt_addr += r->offset - map.rm.size;
1003 len -= map.rm.size - r->offset;
1004 result = dma_sb_map_area(r, virt_addr, len, &tmp,
1005 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
1014 * dma_sb_region_free_linear - Free a linear dma mapping for a device.
1015 * @r: Pointer to a struct ps3_dma_region.
1017 * This routine will unmap all mapped areas and free the HV dma region.
1020 static int dma_sb_region_free_linear(struct ps3_dma_region *r)
1023 dma_addr_t bus_addr;
1024 unsigned long len, lpar_addr;
1026 if (r->offset < map.rm.size) {
1027 /* Unmap (part of) 1st RAM chunk */
1028 lpar_addr = map.rm.base + r->offset;
1029 len = map.rm.size - r->offset;
1032 bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1033 result = dma_sb_unmap_area(r, bus_addr, len);
1037 if (r->offset + r->len > map.rm.size) {
1038 /* Unmap (part of) 2nd RAM chunk */
1039 lpar_addr = map.r1.base;
1041 if (r->offset >= map.rm.size)
1042 lpar_addr += r->offset - map.rm.size;
1044 len -= map.rm.size - r->offset;
1045 bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1046 result = dma_sb_unmap_area(r, bus_addr, len);
1050 result = dma_sb_region_free(r);
1057 * dma_sb_map_area_linear - Map an area of memory into a device dma region.
1058 * @r: Pointer to a struct ps3_dma_region.
1059 * @virt_addr: Starting virtual address of the area to map.
1060 * @len: Length in bytes of the area to map.
1061 * @bus_addr: A pointer to return the starting ioc bus address of the area to
1064 * This routine just returns the corresponding bus address. Actual mapping
1065 * occurs in dma_region_create_linear().
1068 static int dma_sb_map_area_linear(struct ps3_dma_region *r,
1069 unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr,
1072 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
1074 *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
1079 * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
1080 * @r: Pointer to a struct ps3_dma_region.
1081 * @bus_addr: The starting ioc bus address of the area to unmap.
1082 * @len: Length in bytes of the area to unmap.
1084 * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear().
1087 static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
1088 dma_addr_t bus_addr, unsigned long len)
1093 static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = {
1094 .create = dma_sb_region_create,
1095 .free = dma_sb_region_free,
1096 .map = dma_sb_map_area,
1097 .unmap = dma_sb_unmap_area
1100 static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
1101 .create = dma_sb_region_create_linear,
1102 .free = dma_sb_region_free_linear,
1103 .map = dma_sb_map_area_linear,
1104 .unmap = dma_sb_unmap_area_linear
1107 static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
1108 .create = dma_ioc0_region_create,
1109 .free = dma_ioc0_region_free,
1110 .map = dma_ioc0_map_area,
1111 .unmap = dma_ioc0_unmap_area
1114 int ps3_dma_region_init(struct ps3_system_bus_device *dev,
1115 struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
1116 enum ps3_dma_region_type region_type, void *addr, unsigned long len)
1118 unsigned long lpar_addr;
1120 lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
1123 r->page_size = page_size;
1124 r->region_type = region_type;
1125 r->offset = lpar_addr;
1126 if (r->offset >= map.rm.size)
1127 r->offset -= map.r1.offset;
1128 r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);
1130 switch (dev->dev_type) {
1131 case PS3_DEVICE_TYPE_SB:
1132 r->region_ops = (USE_DYNAMIC_DMA)
1133 ? &ps3_dma_sb_region_ops
1134 : &ps3_dma_sb_region_linear_ops;
1136 case PS3_DEVICE_TYPE_IOC0:
1137 r->region_ops = &ps3_dma_ioc0_region_ops;
1145 EXPORT_SYMBOL(ps3_dma_region_init);
1147 int ps3_dma_region_create(struct ps3_dma_region *r)
1150 BUG_ON(!r->region_ops);
1151 BUG_ON(!r->region_ops->create);
1152 return r->region_ops->create(r);
1154 EXPORT_SYMBOL(ps3_dma_region_create);
1156 int ps3_dma_region_free(struct ps3_dma_region *r)
1159 BUG_ON(!r->region_ops);
1160 BUG_ON(!r->region_ops->free);
1161 return r->region_ops->free(r);
1163 EXPORT_SYMBOL(ps3_dma_region_free);
1165 int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
1166 unsigned long len, dma_addr_t *bus_addr,
1169 return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
1172 int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr,
1175 return r->region_ops->unmap(r, bus_addr, len);
1178 /*============================================================================*/
1179 /* system startup routines */
1180 /*============================================================================*/
1183 * ps3_mm_init - initialize the address space state variables
1186 void __init ps3_mm_init(void)
1190 DBG(" -> %s:%d\n", __func__, __LINE__);
1192 result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
1196 panic("ps3_repository_read_mm_info() failed");
1198 map.rm.offset = map.rm.base;
1199 map.vas_id = map.htab_size = 0;
1201 /* this implementation assumes map.rm.base is zero */
1203 BUG_ON(map.rm.base);
1204 BUG_ON(!map.rm.size);
1206 /* Check if we got the highmem region from an earlier boot step */
1208 if (ps3_mm_get_repository_highmem(&map.r1)) {
1209 result = ps3_mm_region_create(&map.r1, map.total - map.rm.size);
1212 ps3_mm_set_repository_highmem(&map.r1);
1215 /* correct map.total for the real total amount of memory we use */
1216 map.total = map.rm.size + map.r1.size;
1219 DBG("%s:%d: No highmem region found\n", __func__, __LINE__);
1221 DBG("%s:%d: Adding highmem region: %llxh %llxh\n",
1222 __func__, __LINE__, map.rm.size,
1223 map.total - map.rm.size);
1224 memblock_add(map.rm.size, map.total - map.rm.size);
1227 DBG(" <- %s:%d\n", __func__, __LINE__);
1231 * ps3_mm_shutdown - final cleanup of address space
1234 void ps3_mm_shutdown(void)
1236 ps3_mm_region_destroy(&map.r1);