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(*c), GFP_ATOMIC);
537 c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
538 c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr);
541 BUG_ON(iopte_flag != 0xf800000000000000UL);
542 result = lv1_map_device_dma_region(c->region->dev->bus_id,
543 c->region->dev->dev_id, c->lpar_addr,
544 c->bus_addr, c->len, iopte_flag);
546 DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
547 __func__, __LINE__, ps3_result(result));
551 list_add(&c->link, &r->chunk_list.head);
560 DBG(" <- %s:%d\n", __func__, __LINE__);
564 static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
565 unsigned long len, struct dma_chunk **c_out,
569 struct dma_chunk *c, *last;
571 unsigned long offset;
573 DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__,
574 phys_addr, ps3_mm_phys_to_lpar(phys_addr), len);
575 c = kzalloc(sizeof(*c), GFP_ATOMIC);
583 c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
584 /* allocate IO address */
585 if (list_empty(&r->chunk_list.head)) {
587 c->bus_addr = r->bus_addr;
589 /* derive from last bus addr*/
590 last = list_entry(r->chunk_list.head.next,
591 struct dma_chunk, link);
592 c->bus_addr = last->bus_addr + last->len;
593 DBG("%s: last bus=%#lx, len=%#lx\n", __func__,
594 last->bus_addr, last->len);
597 /* FIXME: check whether length exceeds region size */
599 /* build ioptes for the area */
600 pages = len >> r->page_size;
601 DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__,
602 r->page_size, r->len, pages, iopte_flag);
603 for (iopage = 0; iopage < pages; iopage++) {
604 offset = (1 << r->page_size) * iopage;
605 result = lv1_put_iopte(0,
606 c->bus_addr + offset,
607 c->lpar_addr + offset,
611 pr_warn("%s:%d: lv1_put_iopte failed: %s\n",
612 __func__, __LINE__, ps3_result(result));
615 DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__,
616 iopage, c->bus_addr + offset, c->lpar_addr + offset,
620 /* be sure that last allocated one is inserted at head */
621 list_add(&c->link, &r->chunk_list.head);
624 DBG("%s: end\n", __func__);
628 for (iopage--; 0 <= iopage; iopage--) {
630 c->bus_addr + offset,
631 c->lpar_addr + offset,
642 * dma_sb_region_create - Create a device dma region.
643 * @r: Pointer to a struct ps3_dma_region.
645 * This is the lowest level dma region create routine, and is the one that
646 * will make the HV call to create the region.
649 static int dma_sb_region_create(struct ps3_dma_region *r)
654 DBG(" -> %s:%d:\n", __func__, __LINE__);
658 if (!r->dev->bus_id) {
659 pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
660 r->dev->bus_id, r->dev->dev_id);
664 DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__,
665 __LINE__, r->len, r->page_size, r->offset);
668 BUG_ON(!r->page_size);
669 BUG_ON(!r->region_ops);
671 INIT_LIST_HEAD(&r->chunk_list.head);
672 spin_lock_init(&r->chunk_list.lock);
674 result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id,
675 roundup_pow_of_two(r->len), r->page_size, r->region_type,
677 r->bus_addr = bus_addr;
680 DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
681 __func__, __LINE__, ps3_result(result));
682 r->len = r->bus_addr = 0;
688 static int dma_ioc0_region_create(struct ps3_dma_region *r)
693 INIT_LIST_HEAD(&r->chunk_list.head);
694 spin_lock_init(&r->chunk_list.lock);
696 result = lv1_allocate_io_segment(0,
700 r->bus_addr = bus_addr;
702 DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
703 __func__, __LINE__, ps3_result(result));
704 r->len = r->bus_addr = 0;
706 DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__,
707 r->len, r->page_size, r->bus_addr);
712 * dma_region_free - Free a device dma region.
713 * @r: Pointer to a struct ps3_dma_region.
715 * This is the lowest level dma region free routine, and is the one that
716 * will make the HV call to free the region.
719 static int dma_sb_region_free(struct ps3_dma_region *r)
723 struct dma_chunk *tmp;
727 if (!r->dev->bus_id) {
728 pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__,
729 r->dev->bus_id, r->dev->dev_id);
733 list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
735 dma_sb_free_chunk(c);
738 result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id,
742 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
743 __func__, __LINE__, ps3_result(result));
750 static int dma_ioc0_region_free(struct ps3_dma_region *r)
753 struct dma_chunk *c, *n;
755 DBG("%s: start\n", __func__);
756 list_for_each_entry_safe(c, n, &r->chunk_list.head, link) {
758 dma_ioc0_free_chunk(c);
761 result = lv1_release_io_segment(0, r->bus_addr);
764 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
765 __func__, __LINE__, ps3_result(result));
768 DBG("%s: end\n", __func__);
774 * dma_sb_map_area - Map an area of memory into a device dma region.
775 * @r: Pointer to a struct ps3_dma_region.
776 * @virt_addr: Starting virtual address of the area to map.
777 * @len: Length in bytes of the area to map.
778 * @bus_addr: A pointer to return the starting ioc bus address of the area to
781 * This is the common dma mapping routine.
784 static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
785 unsigned long len, dma_addr_t *bus_addr,
791 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
793 unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
794 unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
796 *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
798 if (!USE_DYNAMIC_DMA) {
799 unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
800 DBG(" -> %s:%d\n", __func__, __LINE__);
801 DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
803 DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
805 DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
807 DBG("%s:%d len %lxh\n", __func__, __LINE__, len);
808 DBG("%s:%d bus_addr %llxh (%lxh)\n", __func__, __LINE__,
812 spin_lock_irqsave(&r->chunk_list.lock, flags);
813 c = dma_find_chunk(r, *bus_addr, len);
816 DBG("%s:%d: reusing mapped chunk", __func__, __LINE__);
819 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
823 result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag);
827 DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
828 __func__, __LINE__, result);
829 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
835 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
839 static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
840 unsigned long len, dma_addr_t *bus_addr,
846 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
848 unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size);
849 unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys,
852 DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__,
854 DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__,
855 phys_addr, aligned_phys, aligned_len);
857 spin_lock_irqsave(&r->chunk_list.lock, flags);
858 c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len);
863 *bus_addr = c->bus_addr + phys_addr - aligned_phys;
865 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
869 result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c,
874 DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
875 __func__, __LINE__, result);
876 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
879 *bus_addr = c->bus_addr + phys_addr - aligned_phys;
880 DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__,
881 virt_addr, phys_addr, aligned_phys, *bus_addr);
884 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
889 * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
890 * @r: Pointer to a struct ps3_dma_region.
891 * @bus_addr: The starting ioc bus address of the area to unmap.
892 * @len: Length in bytes of the area to unmap.
894 * This is the common dma unmap routine.
897 static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr,
903 spin_lock_irqsave(&r->chunk_list.lock, flags);
904 c = dma_find_chunk(r, bus_addr, len);
907 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
909 unsigned long aligned_len = _ALIGN_UP(len + bus_addr
910 - aligned_bus, 1 << r->page_size);
911 DBG("%s:%d: not found: bus_addr %llxh\n",
912 __func__, __LINE__, bus_addr);
913 DBG("%s:%d: not found: len %lxh\n",
914 __func__, __LINE__, len);
915 DBG("%s:%d: not found: aligned_bus %lxh\n",
916 __func__, __LINE__, aligned_bus);
917 DBG("%s:%d: not found: aligned_len %lxh\n",
918 __func__, __LINE__, aligned_len);
924 if (!c->usage_count) {
926 dma_sb_free_chunk(c);
929 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
933 static int dma_ioc0_unmap_area(struct ps3_dma_region *r,
934 dma_addr_t bus_addr, unsigned long len)
939 DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len);
940 spin_lock_irqsave(&r->chunk_list.lock, flags);
941 c = dma_find_chunk(r, bus_addr, len);
944 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
946 unsigned long aligned_len = _ALIGN_UP(len + bus_addr
949 DBG("%s:%d: not found: bus_addr %llxh\n",
950 __func__, __LINE__, bus_addr);
951 DBG("%s:%d: not found: len %lxh\n",
952 __func__, __LINE__, len);
953 DBG("%s:%d: not found: aligned_bus %lxh\n",
954 __func__, __LINE__, aligned_bus);
955 DBG("%s:%d: not found: aligned_len %lxh\n",
956 __func__, __LINE__, aligned_len);
962 if (!c->usage_count) {
964 dma_ioc0_free_chunk(c);
967 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
968 DBG("%s: end\n", __func__);
973 * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
974 * @r: Pointer to a struct ps3_dma_region.
976 * This routine creates an HV dma region for the device and maps all available
977 * ram into the io controller bus address space.
980 static int dma_sb_region_create_linear(struct ps3_dma_region *r)
983 unsigned long virt_addr, len;
986 if (r->len > 16*1024*1024) { /* FIXME: need proper fix */
987 /* force 16M dma pages for linear mapping */
988 if (r->page_size != PS3_DMA_16M) {
989 pr_info("%s:%d: forcing 16M pages for linear map\n",
991 r->page_size = PS3_DMA_16M;
992 r->len = _ALIGN_UP(r->len, 1 << r->page_size);
996 result = dma_sb_region_create(r);
999 if (r->offset < map.rm.size) {
1000 /* Map (part of) 1st RAM chunk */
1001 virt_addr = map.rm.base + r->offset;
1002 len = map.rm.size - r->offset;
1005 result = dma_sb_map_area(r, virt_addr, len, &tmp,
1006 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
1011 if (r->offset + r->len > map.rm.size) {
1012 /* Map (part of) 2nd RAM chunk */
1013 virt_addr = map.rm.size;
1015 if (r->offset >= map.rm.size)
1016 virt_addr += r->offset - map.rm.size;
1018 len -= map.rm.size - r->offset;
1019 result = dma_sb_map_area(r, virt_addr, len, &tmp,
1020 CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW |
1029 * dma_sb_region_free_linear - Free a linear dma mapping for a device.
1030 * @r: Pointer to a struct ps3_dma_region.
1032 * This routine will unmap all mapped areas and free the HV dma region.
1035 static int dma_sb_region_free_linear(struct ps3_dma_region *r)
1038 dma_addr_t bus_addr;
1039 unsigned long len, lpar_addr;
1041 if (r->offset < map.rm.size) {
1042 /* Unmap (part of) 1st RAM chunk */
1043 lpar_addr = map.rm.base + r->offset;
1044 len = map.rm.size - r->offset;
1047 bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1048 result = dma_sb_unmap_area(r, bus_addr, len);
1052 if (r->offset + r->len > map.rm.size) {
1053 /* Unmap (part of) 2nd RAM chunk */
1054 lpar_addr = map.r1.base;
1056 if (r->offset >= map.rm.size)
1057 lpar_addr += r->offset - map.rm.size;
1059 len -= map.rm.size - r->offset;
1060 bus_addr = dma_sb_lpar_to_bus(r, lpar_addr);
1061 result = dma_sb_unmap_area(r, bus_addr, len);
1065 result = dma_sb_region_free(r);
1072 * dma_sb_map_area_linear - Map an area of memory into a device dma region.
1073 * @r: Pointer to a struct ps3_dma_region.
1074 * @virt_addr: Starting virtual address of the area to map.
1075 * @len: Length in bytes of the area to map.
1076 * @bus_addr: A pointer to return the starting ioc bus address of the area to
1079 * This routine just returns the corresponding bus address. Actual mapping
1080 * occurs in dma_region_create_linear().
1083 static int dma_sb_map_area_linear(struct ps3_dma_region *r,
1084 unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr,
1087 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
1089 *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
1094 * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
1095 * @r: Pointer to a struct ps3_dma_region.
1096 * @bus_addr: The starting ioc bus address of the area to unmap.
1097 * @len: Length in bytes of the area to unmap.
1099 * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear().
1102 static int dma_sb_unmap_area_linear(struct ps3_dma_region *r,
1103 dma_addr_t bus_addr, unsigned long len)
1108 static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = {
1109 .create = dma_sb_region_create,
1110 .free = dma_sb_region_free,
1111 .map = dma_sb_map_area,
1112 .unmap = dma_sb_unmap_area
1115 static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = {
1116 .create = dma_sb_region_create_linear,
1117 .free = dma_sb_region_free_linear,
1118 .map = dma_sb_map_area_linear,
1119 .unmap = dma_sb_unmap_area_linear
1122 static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = {
1123 .create = dma_ioc0_region_create,
1124 .free = dma_ioc0_region_free,
1125 .map = dma_ioc0_map_area,
1126 .unmap = dma_ioc0_unmap_area
1129 int ps3_dma_region_init(struct ps3_system_bus_device *dev,
1130 struct ps3_dma_region *r, enum ps3_dma_page_size page_size,
1131 enum ps3_dma_region_type region_type, void *addr, unsigned long len)
1133 unsigned long lpar_addr;
1136 lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0;
1139 r->page_size = page_size;
1140 r->region_type = region_type;
1141 r->offset = lpar_addr;
1142 if (r->offset >= map.rm.size)
1143 r->offset -= map.r1.offset;
1144 r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size);
1146 dev->core.dma_mask = &r->dma_mask;
1148 result = dma_set_mask_and_coherent(&dev->core, DMA_BIT_MASK(32));
1151 dev_err(&dev->core, "%s:%d: dma_set_mask_and_coherent failed: %d\n",
1152 __func__, __LINE__, result);
1156 switch (dev->dev_type) {
1157 case PS3_DEVICE_TYPE_SB:
1158 r->region_ops = (USE_DYNAMIC_DMA)
1159 ? &ps3_dma_sb_region_ops
1160 : &ps3_dma_sb_region_linear_ops;
1162 case PS3_DEVICE_TYPE_IOC0:
1163 r->region_ops = &ps3_dma_ioc0_region_ops;
1171 EXPORT_SYMBOL(ps3_dma_region_init);
1173 int ps3_dma_region_create(struct ps3_dma_region *r)
1176 BUG_ON(!r->region_ops);
1177 BUG_ON(!r->region_ops->create);
1178 return r->region_ops->create(r);
1180 EXPORT_SYMBOL(ps3_dma_region_create);
1182 int ps3_dma_region_free(struct ps3_dma_region *r)
1185 BUG_ON(!r->region_ops);
1186 BUG_ON(!r->region_ops->free);
1187 return r->region_ops->free(r);
1189 EXPORT_SYMBOL(ps3_dma_region_free);
1191 int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
1192 unsigned long len, dma_addr_t *bus_addr,
1195 return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag);
1198 int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr,
1201 return r->region_ops->unmap(r, bus_addr, len);
1204 /*============================================================================*/
1205 /* system startup routines */
1206 /*============================================================================*/
1209 * ps3_mm_init - initialize the address space state variables
1212 void __init ps3_mm_init(void)
1216 DBG(" -> %s:%d\n", __func__, __LINE__);
1218 result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
1222 panic("ps3_repository_read_mm_info() failed");
1224 map.rm.offset = map.rm.base;
1225 map.vas_id = map.htab_size = 0;
1227 /* this implementation assumes map.rm.base is zero */
1229 BUG_ON(map.rm.base);
1230 BUG_ON(!map.rm.size);
1232 /* Check if we got the highmem region from an earlier boot step */
1234 if (ps3_mm_get_repository_highmem(&map.r1)) {
1235 result = ps3_mm_region_create(&map.r1, map.total - map.rm.size);
1238 ps3_mm_set_repository_highmem(&map.r1);
1241 /* correct map.total for the real total amount of memory we use */
1242 map.total = map.rm.size + map.r1.size;
1245 DBG("%s:%d: No highmem region found\n", __func__, __LINE__);
1247 DBG("%s:%d: Adding highmem region: %llxh %llxh\n",
1248 __func__, __LINE__, map.rm.size,
1249 map.total - map.rm.size);
1250 memblock_add(map.rm.size, map.total - map.rm.size);
1253 DBG(" <- %s:%d\n", __func__, __LINE__);
1257 * ps3_mm_shutdown - final cleanup of address space
1260 void ps3_mm_shutdown(void)
1262 ps3_mm_region_destroy(&map.r1);