2 * PS3 address space management.
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006 Sony Corp.
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.
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.
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
21 #include <linux/dma-mapping.h>
22 #include <linux/kernel.h>
23 #include <linux/export.h>
24 #include <linux/memblock.h>
25 #include <linux/slab.h>
27 #include <asm/cell-regs.h>
28 #include <asm/firmware.h>
31 #include <asm/lv1call.h>
32 #include <asm/setup.h>
37 #define DBG udbg_printf
43 #if defined(CONFIG_PS3_DYNAMIC_DMA)
56 static unsigned long make_page_sizes(unsigned long a, unsigned long b)
58 return (a << 56) | (b << 48);
62 ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
63 ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
66 /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
69 HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
70 HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
73 /*============================================================================*/
74 /* virtual address space routines */
75 /*============================================================================*/
78 * struct mem_region - memory region structure
80 * @size: size in bytes
81 * @offset: difference between base and rm.size
82 * @destroy: flag if region should be destroyed upon shutdown
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
98 * The HV virtual address space (vas) allows for hotplug memory regions.
99 * Memory regions can be created and destroyed in the vas at runtime.
100 * @rm: real mode (bootmem) region
101 * @r1: highmem region(s)
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
114 struct mem_region rm;
115 struct mem_region r1;
118 #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
119 static void __maybe_unused _debug_dump_map(const struct map *m,
120 const char *func, int line)
122 DBG("%s:%d: map.total = %llxh\n", func, line, m->total);
123 DBG("%s:%d: map.rm.size = %llxh\n", func, line, m->rm.size);
124 DBG("%s:%d: map.vas_id = %llu\n", func, line, m->vas_id);
125 DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size);
126 DBG("%s:%d: map.r1.base = %llxh\n", func, line, m->r1.base);
127 DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
128 DBG("%s:%d: map.r1.size = %llxh\n", func, line, m->r1.size);
131 static struct map map;
134 * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
135 * @phys_addr: linux physical address
138 unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
140 BUG_ON(is_kernel_addr(phys_addr));
141 return (phys_addr < map.rm.size || phys_addr >= map.total)
142 ? phys_addr : phys_addr + map.r1.offset;
145 EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
148 * ps3_mm_vas_create - create the virtual address space
151 void __init ps3_mm_vas_create(unsigned long* htab_size)
160 result = lv1_query_logical_partition_address_region_info(0,
161 &start_address, &size, &access_right, &max_page_size,
165 DBG("%s:%d: lv1_query_logical_partition_address_region_info "
166 "failed: %s\n", __func__, __LINE__,
171 if (max_page_size < PAGE_SHIFT_16M) {
172 DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__,
177 BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
178 BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
180 result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
181 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
182 &map.vas_id, &map.htab_size);
185 DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
186 __func__, __LINE__, ps3_result(result));
190 result = lv1_select_virtual_address_space(map.vas_id);
193 DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
194 __func__, __LINE__, ps3_result(result));
198 *htab_size = map.htab_size;
200 debug_dump_map(&map);
205 panic("ps3_mm_vas_create failed");
209 * ps3_mm_vas_destroy -
212 void ps3_mm_vas_destroy(void)
217 result = lv1_select_virtual_address_space(0);
218 result += lv1_destruct_virtual_address_space(map.vas_id);
228 static int ps3_mm_get_repository_highmem(struct mem_region *r)
232 /* Assume a single highmem region. */
234 result = ps3_repository_read_highmem_info(0, &r->base, &r->size);
239 if (!r->base || !r->size) {
244 r->offset = r->base - map.rm.size;
246 DBG("%s:%d: Found high region in repository: %llxh %llxh\n",
247 __func__, __LINE__, r->base, r->size);
252 DBG("%s:%d: No high region in repository.\n", __func__, __LINE__);
254 r->size = r->base = r->offset = 0;
258 static int ps3_mm_set_repository_highmem(const struct mem_region *r)
260 /* Assume a single highmem region. */
262 return r ? ps3_repository_write_highmem_info(0, r->base, r->size) :
263 ps3_repository_write_highmem_info(0, 0, 0);
267 * ps3_mm_region_create - create a memory region in the vas
268 * @r: pointer to a struct mem_region to accept initialized values
269 * @size: requested region size
271 * This implementation creates the region with the vas large page size.
272 * @size is rounded down to a multiple of the vas large page size.
275 static int ps3_mm_region_create(struct mem_region *r, unsigned long size)
280 r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
282 DBG("%s:%d requested %lxh\n", __func__, __LINE__, size);
283 DBG("%s:%d actual %llxh\n", __func__, __LINE__, r->size);
284 DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__,
285 size - r->size, (size - r->size) / 1024 / 1024);
288 DBG("%s:%d: size == 0\n", __func__, __LINE__);
293 result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
294 ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
296 if (result || r->base < map.rm.size) {
297 DBG("%s:%d: lv1_allocate_memory failed: %s\n",
298 __func__, __LINE__, ps3_result(result));
303 r->offset = r->base - map.rm.size;
307 r->size = r->base = r->offset = 0;
312 * ps3_mm_region_destroy - destroy a memory region
313 * @r: pointer to struct mem_region
316 static void ps3_mm_region_destroy(struct mem_region *r)
325 result = lv1_release_memory(r->base);
331 r->size = r->base = r->offset = 0;
332 map.total = map.rm.size;
335 ps3_mm_set_repository_highmem(NULL);
338 /*============================================================================*/
340 /*============================================================================*/
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
348 static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r,
349 unsigned long lpar_addr)
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;
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)
362 DBG("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_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);
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.
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.
385 struct ps3_dma_region *region;
386 unsigned long lpar_addr;
387 unsigned long bus_addr;
389 struct list_head link;
390 unsigned int usage_count;
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,
397 DBG("%s:%d: r.dev %llu:%llu\n", func, line,
398 c->region->dev->bus_id, c->region->dev->dev_id);
399 DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr);
400 DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size);
401 DBG("%s:%d: r.len %lxh\n", func, line, c->region->len);
402 DBG("%s:%d: r.offset %lxh\n", func, line, c->region->offset);
403 DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr);
404 DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr);
405 DBG("%s:%d: c.len %lxh\n", func, line, c->len);
408 static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
409 unsigned long bus_addr, unsigned long len)
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,
416 list_for_each_entry(c, &r->chunk_list.head, link) {
418 if (aligned_bus >= c->bus_addr &&
419 aligned_bus + aligned_len <= c->bus_addr + c->len)
423 if (aligned_bus + aligned_len <= c->bus_addr)
427 if (aligned_bus >= c->bus_addr + c->len)
430 /* we don't handle the multi-chunk case for now */
437 static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r,
438 unsigned long lpar_addr, unsigned long len)
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,
445 list_for_each_entry(c, &r->chunk_list.head, link) {
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)
457 if (aligned_lpar + aligned_len <= c->lpar_addr) {
461 if (c->lpar_addr + c->len <= aligned_lpar) {
468 static int dma_sb_free_chunk(struct dma_chunk *c)
473 result = lv1_unmap_device_dma_region(c->region->dev->bus_id,
474 c->region->dev->dev_id, c->bus_addr, c->len);
482 static int dma_ioc0_free_chunk(struct dma_chunk *c)
486 unsigned long offset;
487 struct ps3_dma_region *r = c->region;
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,
498 DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__,
499 c->bus_addr + offset,
500 c->lpar_addr + offset,
504 DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__,
505 __LINE__, ps3_result(result));
509 DBG("%s:end\n", __func__);
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.
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.
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)
530 c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
538 c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
539 c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
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);
547 DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
548 __func__, __LINE__, ps3_result(result));
552 list_add(&c->link, &r->chunk_list.head);
561 DBG(" <- %s:%d\n", __func__, __LINE__);
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,
570 struct dma_chunk *c, *last;
572 unsigned long offset;
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);
585 c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
586 /* allocate IO address */
587 if (list_empty(&r->chunk_list.head)) {
589 c->bus_addr = r->bus_addr;
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);
599 /* FIXME: check whether length exceeds region size */
601 /* build ioptes for the area */
602 pages = len >> r->page_size;
603 DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\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,
613 pr_warning("%s:%d: lv1_put_iopte failed: %s\n",
614 __func__, __LINE__, ps3_result(result));
617 DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
618 iopage, c->bus_addr + offset, c->lpar_addr + offset,
622 /* be sure that last allocated one is inserted at head */
623 list_add(&c->link, &r->chunk_list.head);
626 DBG("%s: end\n", __func__);
630 for (iopage--; 0 <= iopage; iopage--) {
632 c->bus_addr + offset,
633 c->lpar_addr + offset,
644 * dma_sb_region_create - Create a device dma region.
645 * @r: Pointer to a struct ps3_dma_region.
647 * This is the lowest level dma region create routine, and is the one that
648 * will make the HV call to create the region.
651 static int dma_sb_region_create(struct ps3_dma_region *r)
656 DBG(" -> %s:%d:\n", __func__, __LINE__);
660 if (!r->dev->bus_id) {
661 pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
662 r->dev->bus_id, r->dev->dev_id);
666 DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
667 __LINE__, r->len, r->page_size, r->offset);
670 BUG_ON(!r->page_size);
671 BUG_ON(!r->region_ops);
673 INIT_LIST_HEAD(&r->chunk_list.head);
674 spin_lock_init(&r->chunk_list.lock);
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,
679 r->bus_addr = bus_addr;
682 DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
683 __func__, __LINE__, ps3_result(result));
684 r->len = r->bus_addr = 0;
690 static int dma_ioc0_region_create(struct ps3_dma_region *r)
695 INIT_LIST_HEAD(&r->chunk_list.head);
696 spin_lock_init(&r->chunk_list.lock);
698 result = lv1_allocate_io_segment(0,
702 r->bus_addr = bus_addr;
704 DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
705 __func__, __LINE__, ps3_result(result));
706 r->len = r->bus_addr = 0;
708 DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
709 r->len, r->page_size, r->bus_addr);
714 * dma_region_free - Free a device dma region.
715 * @r: Pointer to a struct ps3_dma_region.
717 * This is the lowest level dma region free routine, and is the one that
718 * will make the HV call to free the region.
721 static int dma_sb_region_free(struct ps3_dma_region *r)
725 struct dma_chunk *tmp;
729 if (!r->dev->bus_id) {
730 pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
731 r->dev->bus_id, r->dev->dev_id);
735 list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
737 dma_sb_free_chunk(c);
740 result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
744 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
745 __func__, __LINE__, ps3_result(result));
752 static int dma_ioc0_region_free(struct ps3_dma_region *r)
755 struct dma_chunk *c, *n;
757 DBG("%s: start\n", __func__);
758 list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
760 dma_ioc0_free_chunk(c);
763 result = lv1_release_io_segment(0, r->bus_addr);
766 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
767 __func__, __LINE__, ps3_result(result));
770 DBG("%s: end\n", __func__);
776 * dma_sb_map_area - Map an area of memory into a device dma region.
777 * @r: Pointer to a struct ps3_dma_region.
778 * @virt_addr: Starting virtual address of the area to map.
779 * @len: Length in bytes of the area to map.
780 * @bus_addr: A pointer to return the starting ioc bus address of the area to
783 * This is the common dma mapping routine.
786 static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
787 unsigned long len, dma_addr_t *bus_addr,
793 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
795 unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
796 unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
798 *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
800 if (!USE_DYNAMIC_DMA) {
801 unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
802 DBG(" -> %s:%d\n", __func__, __LINE__);
803 DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
805 DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
807 DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
809 DBG("%s:%d len %lxh\n", __func__, __LINE__, len);
810 DBG("%s:%d bus_addr %llxh (%lxh)\n", __func__, __LINE__,
814 spin_lock_irqsave(&r->chunk_list.lock, flags);
815 c = dma_find_chunk(r, *bus_addr, len);
818 DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
821 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
825 result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
829 DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
830 __func__, __LINE__, result);
831 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
837 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
841 static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
842 unsigned long len, dma_addr_t *bus_addr,
848 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
850 unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
851 unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
854 DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
856 DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
857 phys_addr, aligned_phys, aligned_len);
859 spin_lock_irqsave(&r->chunk_list.lock, flags);
860 c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
865 *bus_addr = c->bus_addr + phys_addr - aligned_phys;
867 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
871 result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
876 DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
877 __func__, __LINE__, result);
878 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
881 *bus_addr = c->bus_addr + phys_addr - aligned_phys;
882 DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__,
883 virt_addr, phys_addr, aligned_phys, *bus_addr);
886 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
891 * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
892 * @r: Pointer to a struct ps3_dma_region.
893 * @bus_addr: The starting ioc bus address of the area to unmap.
894 * @len: Length in bytes of the area to unmap.
896 * This is the common dma unmap routine.
899 static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr,
905 spin_lock_irqsave(&r->chunk_list.lock, flags);
906 c = dma_find_chunk(r, bus_addr, len);
909 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
911 unsigned long aligned_len = _ALIGN_UP(len + bus_addr
912 - aligned_bus, 1 << r->page_size);
913 DBG("%s:%d: not found: bus_addr %llxh\n",
914 __func__, __LINE__, bus_addr);
915 DBG("%s:%d: not found: len %lxh\n",
916 __func__, __LINE__, len);
917 DBG("%s:%d: not found: aligned_bus %lxh\n",
918 __func__, __LINE__, aligned_bus);
919 DBG("%s:%d: not found: aligned_len %lxh\n",
920 __func__, __LINE__, aligned_len);
926 if (!c->usage_count) {
928 dma_sb_free_chunk(c);
931 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
935 static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
936 dma_addr_t bus_addr, unsigned long len)
941 DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len);
942 spin_lock_irqsave(&r->chunk_list.lock, flags);
943 c = dma_find_chunk(r, bus_addr, len);
946 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
948 unsigned long aligned_len = _ALIGN_UP(len + bus_addr
951 DBG("%s:%d: not found: bus_addr %llxh\n",
952 __func__, __LINE__, bus_addr);
953 DBG("%s:%d: not found: len %lxh\n",
954 __func__, __LINE__, len);
955 DBG("%s:%d: not found: aligned_bus %lxh\n",
956 __func__, __LINE__, aligned_bus);
957 DBG("%s:%d: not found: aligned_len %lxh\n",
958 __func__, __LINE__, aligned_len);
964 if (!c->usage_count) {
966 dma_ioc0_free_chunk(c);
969 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
970 DBG("%s: end\n", __func__);
975 * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
976 * @r: Pointer to a struct ps3_dma_region.
978 * This routine creates an HV dma region for the device and maps all available
979 * ram into the io controller bus address space.
982 static int dma_sb_region_create_linear(struct ps3_dma_region *r)
985 unsigned long virt_addr, len;
988 if (r->len > 16*1024*1024) { /* FIXME: need proper fix */
989 /* force 16M dma pages for linear mapping */
990 if (r->page_size != PS3_DMA_16M) {
991 pr_info("%s:%d: forcing 16M pages for linear map\n",
993 r->page_size = PS3_DMA_16M;
994 r->len = _ALIGN_UP(r->len, 1 << r->page_size);
998 result = dma_sb_region_create(r);
1001 if (r->offset < map.rm.size) {
1002 /* Map (part of) 1st RAM chunk */
1003 virt_addr = map.rm.base + r->offset;
1004 len = map.rm.size - r->offset;
1007 result = dma_sb_map_area(r, virt_addr, len, &tmp,
1008 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
1013 if (r->offset + r->len > map.rm.size) {
1014 /* Map (part of) 2nd RAM chunk */
1015 virt_addr = map.rm.size;
1017 if (r->offset >= map.rm.size)
1018 virt_addr += r->offset - map.rm.size;
1020 len -= map.rm.size - r->offset;
1021 result = dma_sb_map_area(r, virt_addr, len, &tmp,
1022 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
1031 * dma_sb_region_free_linear - Free a linear dma mapping for a device.
1032 * @r: Pointer to a struct ps3_dma_region.
1034 * This routine will unmap all mapped areas and free the HV dma region.
1037 static int dma_sb_region_free_linear(struct ps3_dma_region *r)
1040 dma_addr_t bus_addr;
1041 unsigned long len, lpar_addr;
1043 if (r->offset < map.rm.size) {
1044 /* Unmap (part of) 1st RAM chunk */
1045 lpar_addr = map.rm.base + r->offset;
1046 len = map.rm.size - r->offset;
1049 bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1050 result = dma_sb_unmap_area(r, bus_addr, len);
1054 if (r->offset + r->len > map.rm.size) {
1055 /* Unmap (part of) 2nd RAM chunk */
1056 lpar_addr = map.r1.base;
1058 if (r->offset >= map.rm.size)
1059 lpar_addr += r->offset - map.rm.size;
1061 len -= map.rm.size - r->offset;
1062 bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1063 result = dma_sb_unmap_area(r, bus_addr, len);
1067 result = dma_sb_region_free(r);
1074 * dma_sb_map_area_linear - Map an area of memory into a device dma region.
1075 * @r: Pointer to a struct ps3_dma_region.
1076 * @virt_addr: Starting virtual address of the area to map.
1077 * @len: Length in bytes of the area to map.
1078 * @bus_addr: A pointer to return the starting ioc bus address of the area to
1081 * This routine just returns the corresponding bus address. Actual mapping
1082 * occurs in dma_region_create_linear().
1085 static int dma_sb_map_area_linear(struct ps3_dma_region *r,
1086 unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr,
1089 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
1091 *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
1096 * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
1097 * @r: Pointer to a struct ps3_dma_region.
1098 * @bus_addr: The starting ioc bus address of the area to unmap.
1099 * @len: Length in bytes of the area to unmap.
1101 * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear().
1104 static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
1105 dma_addr_t bus_addr, unsigned long len)
1110 static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = {
1111 .create = dma_sb_region_create,
1112 .free = dma_sb_region_free,
1113 .map = dma_sb_map_area,
1114 .unmap = dma_sb_unmap_area
1117 static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
1118 .create = dma_sb_region_create_linear,
1119 .free = dma_sb_region_free_linear,
1120 .map = dma_sb_map_area_linear,
1121 .unmap = dma_sb_unmap_area_linear
1124 static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
1125 .create = dma_ioc0_region_create,
1126 .free = dma_ioc0_region_free,
1127 .map = dma_ioc0_map_area,
1128 .unmap = dma_ioc0_unmap_area
1131 int ps3_dma_region_init(struct ps3_system_bus_device *dev,
1132 struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
1133 enum ps3_dma_region_type region_type, void *addr, unsigned long len)
1135 unsigned long lpar_addr;
1138 lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
1141 r->page_size = page_size;
1142 r->region_type = region_type;
1143 r->offset = lpar_addr;
1144 if (r->offset >= map.rm.size)
1145 r->offset -= map.r1.offset;
1146 r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);
1148 dev->core.dma_mask = &r->dma_mask;
1150 result = dma_set_mask_and_coherent(&dev->core, DMA_BIT_MASK(32));
1153 dev_err(&dev->core, "%s:%d: dma_set_mask_and_coherent failed: %d\n",
1154 __func__, __LINE__, result);
1158 switch (dev->dev_type) {
1159 case PS3_DEVICE_TYPE_SB:
1160 r->region_ops = (USE_DYNAMIC_DMA)
1161 ? &ps3_dma_sb_region_ops
1162 : &ps3_dma_sb_region_linear_ops;
1164 case PS3_DEVICE_TYPE_IOC0:
1165 r->region_ops = &ps3_dma_ioc0_region_ops;
1173 EXPORT_SYMBOL(ps3_dma_region_init);
1175 int ps3_dma_region_create(struct ps3_dma_region *r)
1178 BUG_ON(!r->region_ops);
1179 BUG_ON(!r->region_ops->create);
1180 return r->region_ops->create(r);
1182 EXPORT_SYMBOL(ps3_dma_region_create);
1184 int ps3_dma_region_free(struct ps3_dma_region *r)
1187 BUG_ON(!r->region_ops);
1188 BUG_ON(!r->region_ops->free);
1189 return r->region_ops->free(r);
1191 EXPORT_SYMBOL(ps3_dma_region_free);
1193 int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
1194 unsigned long len, dma_addr_t *bus_addr,
1197 return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
1200 int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr,
1203 return r->region_ops->unmap(r, bus_addr, len);
1206 /*============================================================================*/
1207 /* system startup routines */
1208 /*============================================================================*/
1211 * ps3_mm_init - initialize the address space state variables
1214 void __init ps3_mm_init(void)
1218 DBG(" -> %s:%d\n", __func__, __LINE__);
1220 result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
1224 panic("ps3_repository_read_mm_info() failed");
1226 map.rm.offset = map.rm.base;
1227 map.vas_id = map.htab_size = 0;
1229 /* this implementation assumes map.rm.base is zero */
1231 BUG_ON(map.rm.base);
1232 BUG_ON(!map.rm.size);
1234 /* Check if we got the highmem region from an earlier boot step */
1236 if (ps3_mm_get_repository_highmem(&map.r1)) {
1237 result = ps3_mm_region_create(&map.r1, map.total - map.rm.size);
1240 ps3_mm_set_repository_highmem(&map.r1);
1243 /* correct map.total for the real total amount of memory we use */
1244 map.total = map.rm.size + map.r1.size;
1247 DBG("%s:%d: No highmem region found\n", __func__, __LINE__);
1249 DBG("%s:%d: Adding highmem region: %llxh %llxh\n",
1250 __func__, __LINE__, map.rm.size,
1251 map.total - map.rm.size);
1252 memblock_add(map.rm.size, map.total - map.rm.size);
1255 DBG(" <- %s:%d\n", __func__, __LINE__);
1259 * ps3_mm_shutdown - final cleanup of address space
1262 void ps3_mm_shutdown(void)
1264 ps3_mm_region_destroy(&map.r1);