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/kernel.h>
22 #include <linux/module.h>
23 #include <linux/memory_hotplug.h>
25 #include <asm/firmware.h>
28 #include <asm/lv1call.h>
33 #define DBG udbg_printf
39 #if defined(CONFIG_PS3_USE_LPAR_ADDR)
44 #if defined(CONFIG_PS3_DYNAMIC_DMA)
57 static unsigned long make_page_sizes(unsigned long a
, unsigned long b
)
59 return (a
<< 56) | (b
<< 48);
63 ALLOCATE_MEMORY_TRY_ALT_UNIT
= 0X04,
64 ALLOCATE_MEMORY_ADDR_ZERO
= 0X08,
67 /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
70 HTAB_SIZE_MAX
= 20U, /* HV limit of 1MB */
71 HTAB_SIZE_MIN
= 18U, /* CPU limit of 256KB */
74 /*============================================================================*/
75 /* virtual address space routines */
76 /*============================================================================*/
79 * struct mem_region - memory region structure
81 * @size: size in bytes
82 * @offset: difference between base and rm.size
92 * struct map - address space state variables holder
93 * @total: total memory available as reported by HV
94 * @vas_id - HV virtual address space id
95 * @htab_size: htab size in bytes
97 * The HV virtual address space (vas) allows for hotplug memory regions.
98 * Memory regions can be created and destroyed in the vas at runtime.
99 * @rm: real mode (bootmem) region
100 * @r1: hotplug memory region(s)
103 * virt_addr: a cpu 'translated' effective address
104 * phys_addr: an address in what Linux thinks is the physical address space
105 * lpar_addr: an address in the HV virtual address space
106 * bus_addr: an io controller 'translated' address on a device bus
111 unsigned long vas_id
;
112 unsigned long htab_size
;
113 struct mem_region rm
;
114 struct mem_region r1
;
117 #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__)
118 static void __maybe_unused
_debug_dump_map(const struct map
*m
,
119 const char *func
, int line
)
121 DBG("%s:%d: map.total = %lxh\n", func
, line
, m
->total
);
122 DBG("%s:%d: map.rm.size = %lxh\n", func
, line
, m
->rm
.size
);
123 DBG("%s:%d: map.vas_id = %lu\n", func
, line
, m
->vas_id
);
124 DBG("%s:%d: map.htab_size = %lxh\n", func
, line
, m
->htab_size
);
125 DBG("%s:%d: map.r1.base = %lxh\n", func
, line
, m
->r1
.base
);
126 DBG("%s:%d: map.r1.offset = %lxh\n", func
, line
, m
->r1
.offset
);
127 DBG("%s:%d: map.r1.size = %lxh\n", func
, line
, m
->r1
.size
);
130 static struct map map
;
133 * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
134 * @phys_addr: linux physical address
137 unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr
)
139 BUG_ON(is_kernel_addr(phys_addr
));
143 return (phys_addr
< map
.rm
.size
|| phys_addr
>= map
.total
)
144 ? phys_addr
: phys_addr
+ map
.r1
.offset
;
147 EXPORT_SYMBOL(ps3_mm_phys_to_lpar
);
150 * ps3_mm_vas_create - create the virtual address space
153 void __init
ps3_mm_vas_create(unsigned long* htab_size
)
156 unsigned long start_address
;
158 unsigned long access_right
;
159 unsigned long max_page_size
;
162 result
= lv1_query_logical_partition_address_region_info(0,
163 &start_address
, &size
, &access_right
, &max_page_size
,
167 DBG("%s:%d: lv1_query_logical_partition_address_region_info "
168 "failed: %s\n", __func__
, __LINE__
,
173 if (max_page_size
< PAGE_SHIFT_16M
) {
174 DBG("%s:%d: bad max_page_size %lxh\n", __func__
, __LINE__
,
179 BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE
> HTAB_SIZE_MAX
);
180 BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE
< HTAB_SIZE_MIN
);
182 result
= lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE
,
183 2, make_page_sizes(PAGE_SHIFT_16M
, PAGE_SHIFT_64K
),
184 &map
.vas_id
, &map
.htab_size
);
187 DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
188 __func__
, __LINE__
, ps3_result(result
));
192 result
= lv1_select_virtual_address_space(map
.vas_id
);
195 DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
196 __func__
, __LINE__
, ps3_result(result
));
200 *htab_size
= map
.htab_size
;
202 debug_dump_map(&map
);
207 panic("ps3_mm_vas_create failed");
211 * ps3_mm_vas_destroy -
214 void ps3_mm_vas_destroy(void)
218 DBG("%s:%d: map.vas_id = %lu\n", __func__
, __LINE__
, map
.vas_id
);
221 result
= lv1_select_virtual_address_space(0);
223 result
= lv1_destruct_virtual_address_space(map
.vas_id
);
229 /*============================================================================*/
230 /* memory hotplug routines */
231 /*============================================================================*/
234 * ps3_mm_region_create - create a memory region in the vas
235 * @r: pointer to a struct mem_region to accept initialized values
236 * @size: requested region size
238 * This implementation creates the region with the vas large page size.
239 * @size is rounded down to a multiple of the vas large page size.
242 static int ps3_mm_region_create(struct mem_region
*r
, unsigned long size
)
247 r
->size
= _ALIGN_DOWN(size
, 1 << PAGE_SHIFT_16M
);
249 DBG("%s:%d requested %lxh\n", __func__
, __LINE__
, size
);
250 DBG("%s:%d actual %lxh\n", __func__
, __LINE__
, r
->size
);
251 DBG("%s:%d difference %lxh (%luMB)\n", __func__
, __LINE__
,
252 (unsigned long)(size
- r
->size
),
253 (size
- r
->size
) / 1024 / 1024);
256 DBG("%s:%d: size == 0\n", __func__
, __LINE__
);
261 result
= lv1_allocate_memory(r
->size
, PAGE_SHIFT_16M
, 0,
262 ALLOCATE_MEMORY_TRY_ALT_UNIT
, &r
->base
, &muid
);
264 if (result
|| r
->base
< map
.rm
.size
) {
265 DBG("%s:%d: lv1_allocate_memory failed: %s\n",
266 __func__
, __LINE__
, ps3_result(result
));
270 r
->offset
= r
->base
- map
.rm
.size
;
274 r
->size
= r
->base
= r
->offset
= 0;
279 * ps3_mm_region_destroy - destroy a memory region
280 * @r: pointer to struct mem_region
283 static void ps3_mm_region_destroy(struct mem_region
*r
)
287 DBG("%s:%d: r->base = %lxh\n", __func__
, __LINE__
, r
->base
);
289 result
= lv1_release_memory(r
->base
);
291 r
->size
= r
->base
= r
->offset
= 0;
292 map
.total
= map
.rm
.size
;
297 * ps3_mm_add_memory - hot add memory
300 static int __init
ps3_mm_add_memory(void)
303 unsigned long start_addr
;
304 unsigned long start_pfn
;
305 unsigned long nr_pages
;
307 if (!firmware_has_feature(FW_FEATURE_PS3_LV1
))
310 BUG_ON(!mem_init_done
);
312 start_addr
= USE_LPAR_ADDR
? map
.r1
.base
: map
.rm
.size
;
313 start_pfn
= start_addr
>> PAGE_SHIFT
;
314 nr_pages
= (map
.r1
.size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
316 DBG("%s:%d: start_addr %lxh, start_pfn %lxh, nr_pages %lxh\n",
317 __func__
, __LINE__
, start_addr
, start_pfn
, nr_pages
);
319 result
= add_memory(0, start_addr
, map
.r1
.size
);
322 DBG("%s:%d: add_memory failed: (%d)\n",
323 __func__
, __LINE__
, result
);
327 result
= online_pages(start_pfn
, nr_pages
);
330 DBG("%s:%d: online_pages failed: (%d)\n",
331 __func__
, __LINE__
, result
);
336 core_initcall(ps3_mm_add_memory
);
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 %u:%u\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 %u:%u\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=%#lx\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 printk(KERN_WARNING
"%s:%d: lv1_map_device_dma_region "
614 "failed: %s\n", __func__
, __LINE__
,
618 DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__
,
619 iopage
, c
->bus_addr
+ offset
, c
->lpar_addr
+ offset
,
623 /* be sure that last allocated one is inserted at head */
624 list_add(&c
->link
, &r
->chunk_list
.head
);
627 DBG("%s: end\n", __func__
);
631 for (iopage
--; 0 <= iopage
; iopage
--) {
633 c
->bus_addr
+ offset
,
634 c
->lpar_addr
+ offset
,
645 * dma_sb_region_create - Create a device dma region.
646 * @r: Pointer to a struct ps3_dma_region.
648 * This is the lowest level dma region create routine, and is the one that
649 * will make the HV call to create the region.
652 static int dma_sb_region_create(struct ps3_dma_region
*r
)
656 pr_info(" -> %s:%d:\n", __func__
, __LINE__
);
660 if (!r
->dev
->bus_id
) {
661 pr_info("%s:%d: %u:%u 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
,
681 DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
682 __func__
, __LINE__
, ps3_result(result
));
683 r
->len
= r
->bus_addr
= 0;
689 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,
701 DBG("%s:%d: lv1_allocate_io_segment failed: %s\n",
702 __func__
, __LINE__
, ps3_result(result
));
703 r
->len
= r
->bus_addr
= 0;
705 DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__
,
706 r
->len
, r
->page_size
, r
->bus_addr
);
711 * dma_region_free - Free a device dma region.
712 * @r: Pointer to a struct ps3_dma_region.
714 * This is the lowest level dma region free routine, and is the one that
715 * will make the HV call to free the region.
718 static int dma_sb_region_free(struct ps3_dma_region
*r
)
722 struct dma_chunk
*tmp
;
726 if (!r
->dev
->bus_id
) {
727 pr_info("%s:%d: %u:%u no dma\n", __func__
, __LINE__
,
728 r
->dev
->bus_id
, r
->dev
->dev_id
);
732 list_for_each_entry_safe(c
, tmp
, &r
->chunk_list
.head
, link
) {
734 dma_sb_free_chunk(c
);
737 result
= lv1_free_device_dma_region(r
->dev
->bus_id
, r
->dev
->dev_id
,
741 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
742 __func__
, __LINE__
, ps3_result(result
));
749 static int dma_ioc0_region_free(struct ps3_dma_region
*r
)
752 struct dma_chunk
*c
, *n
;
754 DBG("%s: start\n", __func__
);
755 list_for_each_entry_safe(c
, n
, &r
->chunk_list
.head
, link
) {
757 dma_ioc0_free_chunk(c
);
760 result
= lv1_release_io_segment(0, r
->bus_addr
);
763 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
764 __func__
, __LINE__
, ps3_result(result
));
767 DBG("%s: end\n", __func__
);
773 * dma_sb_map_area - Map an area of memory into a device dma region.
774 * @r: Pointer to a struct ps3_dma_region.
775 * @virt_addr: Starting virtual address of the area to map.
776 * @len: Length in bytes of the area to map.
777 * @bus_addr: A pointer to return the starting ioc bus address of the area to
780 * This is the common dma mapping routine.
783 static int dma_sb_map_area(struct ps3_dma_region
*r
, unsigned long virt_addr
,
784 unsigned long len
, unsigned long *bus_addr
,
790 unsigned long phys_addr
= is_kernel_addr(virt_addr
) ? __pa(virt_addr
)
792 unsigned long aligned_phys
= _ALIGN_DOWN(phys_addr
, 1 << r
->page_size
);
793 unsigned long aligned_len
= _ALIGN_UP(len
+ phys_addr
- aligned_phys
,
795 *bus_addr
= dma_sb_lpar_to_bus(r
, ps3_mm_phys_to_lpar(phys_addr
));
797 if (!USE_DYNAMIC_DMA
) {
798 unsigned long lpar_addr
= ps3_mm_phys_to_lpar(phys_addr
);
799 DBG(" -> %s:%d\n", __func__
, __LINE__
);
800 DBG("%s:%d virt_addr %lxh\n", __func__
, __LINE__
,
802 DBG("%s:%d phys_addr %lxh\n", __func__
, __LINE__
,
804 DBG("%s:%d lpar_addr %lxh\n", __func__
, __LINE__
,
806 DBG("%s:%d len %lxh\n", __func__
, __LINE__
, len
);
807 DBG("%s:%d bus_addr %lxh (%lxh)\n", __func__
, __LINE__
,
811 spin_lock_irqsave(&r
->chunk_list
.lock
, flags
);
812 c
= dma_find_chunk(r
, *bus_addr
, len
);
815 DBG("%s:%d: reusing mapped chunk", __func__
, __LINE__
);
818 spin_unlock_irqrestore(&r
->chunk_list
.lock
, flags
);
822 result
= dma_sb_map_pages(r
, aligned_phys
, aligned_len
, &c
, iopte_flag
);
826 DBG("%s:%d: dma_sb_map_pages failed (%d)\n",
827 __func__
, __LINE__
, result
);
828 spin_unlock_irqrestore(&r
->chunk_list
.lock
, flags
);
834 spin_unlock_irqrestore(&r
->chunk_list
.lock
, flags
);
838 static int dma_ioc0_map_area(struct ps3_dma_region
*r
, unsigned long virt_addr
,
839 unsigned long len
, unsigned long *bus_addr
,
845 unsigned long phys_addr
= is_kernel_addr(virt_addr
) ? __pa(virt_addr
)
847 unsigned long aligned_phys
= _ALIGN_DOWN(phys_addr
, 1 << r
->page_size
);
848 unsigned long aligned_len
= _ALIGN_UP(len
+ phys_addr
- aligned_phys
,
851 DBG(KERN_ERR
"%s: vaddr=%#lx, len=%#lx\n", __func__
,
853 DBG(KERN_ERR
"%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__
,
854 phys_addr
, aligned_phys
, aligned_len
);
856 spin_lock_irqsave(&r
->chunk_list
.lock
, flags
);
857 c
= dma_find_chunk_lpar(r
, ps3_mm_phys_to_lpar(phys_addr
), len
);
862 *bus_addr
= c
->bus_addr
+ phys_addr
- aligned_phys
;
864 spin_unlock_irqrestore(&r
->chunk_list
.lock
, flags
);
868 result
= dma_ioc0_map_pages(r
, aligned_phys
, aligned_len
, &c
,
873 DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n",
874 __func__
, __LINE__
, result
);
875 spin_unlock_irqrestore(&r
->chunk_list
.lock
, flags
);
878 *bus_addr
= c
->bus_addr
+ phys_addr
- aligned_phys
;
879 DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#lx\n", __func__
,
880 virt_addr
, phys_addr
, aligned_phys
, *bus_addr
);
883 spin_unlock_irqrestore(&r
->chunk_list
.lock
, flags
);
888 * dma_sb_unmap_area - Unmap an area of memory from a device dma region.
889 * @r: Pointer to a struct ps3_dma_region.
890 * @bus_addr: The starting ioc bus address of the area to unmap.
891 * @len: Length in bytes of the area to unmap.
893 * This is the common dma unmap routine.
896 static int dma_sb_unmap_area(struct ps3_dma_region
*r
, unsigned long bus_addr
,
902 spin_lock_irqsave(&r
->chunk_list
.lock
, flags
);
903 c
= dma_find_chunk(r
, bus_addr
, len
);
906 unsigned long aligned_bus
= _ALIGN_DOWN(bus_addr
,
908 unsigned long aligned_len
= _ALIGN_UP(len
+ bus_addr
909 - aligned_bus
, 1 << r
->page_size
);
910 DBG("%s:%d: not found: bus_addr %lxh\n",
911 __func__
, __LINE__
, bus_addr
);
912 DBG("%s:%d: not found: len %lxh\n",
913 __func__
, __LINE__
, len
);
914 DBG("%s:%d: not found: aligned_bus %lxh\n",
915 __func__
, __LINE__
, aligned_bus
);
916 DBG("%s:%d: not found: aligned_len %lxh\n",
917 __func__
, __LINE__
, aligned_len
);
923 if (!c
->usage_count
) {
925 dma_sb_free_chunk(c
);
928 spin_unlock_irqrestore(&r
->chunk_list
.lock
, flags
);
932 static int dma_ioc0_unmap_area(struct ps3_dma_region
*r
,
933 unsigned long bus_addr
, unsigned long len
)
938 DBG("%s: start a=%#lx l=%#lx\n", __func__
, bus_addr
, len
);
939 spin_lock_irqsave(&r
->chunk_list
.lock
, flags
);
940 c
= dma_find_chunk(r
, bus_addr
, len
);
943 unsigned long aligned_bus
= _ALIGN_DOWN(bus_addr
,
945 unsigned long aligned_len
= _ALIGN_UP(len
+ bus_addr
948 DBG("%s:%d: not found: bus_addr %lxh\n",
949 __func__
, __LINE__
, bus_addr
);
950 DBG("%s:%d: not found: len %lxh\n",
951 __func__
, __LINE__
, len
);
952 DBG("%s:%d: not found: aligned_bus %lxh\n",
953 __func__
, __LINE__
, aligned_bus
);
954 DBG("%s:%d: not found: aligned_len %lxh\n",
955 __func__
, __LINE__
, aligned_len
);
961 if (!c
->usage_count
) {
963 dma_ioc0_free_chunk(c
);
966 spin_unlock_irqrestore(&r
->chunk_list
.lock
, flags
);
967 DBG("%s: end\n", __func__
);
972 * dma_sb_region_create_linear - Setup a linear dma mapping for a device.
973 * @r: Pointer to a struct ps3_dma_region.
975 * This routine creates an HV dma region for the device and maps all available
976 * ram into the io controller bus address space.
979 static int dma_sb_region_create_linear(struct ps3_dma_region
*r
)
982 unsigned long virt_addr
, len
, tmp
;
984 if (r
->len
> 16*1024*1024) { /* FIXME: need proper fix */
985 /* force 16M dma pages for linear mapping */
986 if (r
->page_size
!= PS3_DMA_16M
) {
987 pr_info("%s:%d: forcing 16M pages for linear map\n",
989 r
->page_size
= PS3_DMA_16M
;
990 r
->len
= _ALIGN_UP(r
->len
, 1 << r
->page_size
);
994 result
= dma_sb_region_create(r
);
997 if (r
->offset
< map
.rm
.size
) {
998 /* Map (part of) 1st RAM chunk */
999 virt_addr
= map
.rm
.base
+ r
->offset
;
1000 len
= map
.rm
.size
- r
->offset
;
1003 result
= dma_sb_map_area(r
, virt_addr
, len
, &tmp
,
1004 IOPTE_PP_W
| IOPTE_PP_R
| IOPTE_SO_RW
| IOPTE_M
);
1008 if (r
->offset
+ r
->len
> map
.rm
.size
) {
1009 /* Map (part of) 2nd RAM chunk */
1010 virt_addr
= USE_LPAR_ADDR
? map
.r1
.base
: map
.rm
.size
;
1012 if (r
->offset
>= map
.rm
.size
)
1013 virt_addr
+= r
->offset
- map
.rm
.size
;
1015 len
-= map
.rm
.size
- r
->offset
;
1016 result
= dma_sb_map_area(r
, virt_addr
, len
, &tmp
,
1017 IOPTE_PP_W
| IOPTE_PP_R
| IOPTE_SO_RW
| IOPTE_M
);
1025 * dma_sb_region_free_linear - Free a linear dma mapping for a device.
1026 * @r: Pointer to a struct ps3_dma_region.
1028 * This routine will unmap all mapped areas and free the HV dma region.
1031 static int dma_sb_region_free_linear(struct ps3_dma_region
*r
)
1034 unsigned long bus_addr
, len
, lpar_addr
;
1036 if (r
->offset
< map
.rm
.size
) {
1037 /* Unmap (part of) 1st RAM chunk */
1038 lpar_addr
= map
.rm
.base
+ r
->offset
;
1039 len
= map
.rm
.size
- r
->offset
;
1042 bus_addr
= dma_sb_lpar_to_bus(r
, lpar_addr
);
1043 result
= dma_sb_unmap_area(r
, bus_addr
, len
);
1047 if (r
->offset
+ r
->len
> map
.rm
.size
) {
1048 /* Unmap (part of) 2nd RAM chunk */
1049 lpar_addr
= map
.r1
.base
;
1051 if (r
->offset
>= map
.rm
.size
)
1052 lpar_addr
+= r
->offset
- map
.rm
.size
;
1054 len
-= map
.rm
.size
- r
->offset
;
1055 bus_addr
= dma_sb_lpar_to_bus(r
, lpar_addr
);
1056 result
= dma_sb_unmap_area(r
, bus_addr
, len
);
1060 result
= dma_sb_region_free(r
);
1067 * dma_sb_map_area_linear - Map an area of memory into a device dma region.
1068 * @r: Pointer to a struct ps3_dma_region.
1069 * @virt_addr: Starting virtual address of the area to map.
1070 * @len: Length in bytes of the area to map.
1071 * @bus_addr: A pointer to return the starting ioc bus address of the area to
1074 * This routine just returns the corresponding bus address. Actual mapping
1075 * occurs in dma_region_create_linear().
1078 static int dma_sb_map_area_linear(struct ps3_dma_region
*r
,
1079 unsigned long virt_addr
, unsigned long len
, unsigned long *bus_addr
,
1082 unsigned long phys_addr
= is_kernel_addr(virt_addr
) ? __pa(virt_addr
)
1084 *bus_addr
= dma_sb_lpar_to_bus(r
, ps3_mm_phys_to_lpar(phys_addr
));
1089 * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
1090 * @r: Pointer to a struct ps3_dma_region.
1091 * @bus_addr: The starting ioc bus address of the area to unmap.
1092 * @len: Length in bytes of the area to unmap.
1094 * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear().
1097 static int dma_sb_unmap_area_linear(struct ps3_dma_region
*r
,
1098 unsigned long bus_addr
, unsigned long len
)
1103 static const struct ps3_dma_region_ops ps3_dma_sb_region_ops
= {
1104 .create
= dma_sb_region_create
,
1105 .free
= dma_sb_region_free
,
1106 .map
= dma_sb_map_area
,
1107 .unmap
= dma_sb_unmap_area
1110 static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops
= {
1111 .create
= dma_sb_region_create_linear
,
1112 .free
= dma_sb_region_free_linear
,
1113 .map
= dma_sb_map_area_linear
,
1114 .unmap
= dma_sb_unmap_area_linear
1117 static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops
= {
1118 .create
= dma_ioc0_region_create
,
1119 .free
= dma_ioc0_region_free
,
1120 .map
= dma_ioc0_map_area
,
1121 .unmap
= dma_ioc0_unmap_area
1124 int ps3_dma_region_init(struct ps3_system_bus_device
*dev
,
1125 struct ps3_dma_region
*r
, enum ps3_dma_page_size page_size
,
1126 enum ps3_dma_region_type region_type
, void *addr
, unsigned long len
)
1128 unsigned long lpar_addr
;
1130 lpar_addr
= addr
? ps3_mm_phys_to_lpar(__pa(addr
)) : 0;
1133 r
->page_size
= page_size
;
1134 r
->region_type
= region_type
;
1135 r
->offset
= lpar_addr
;
1136 if (r
->offset
>= map
.rm
.size
)
1137 r
->offset
-= map
.r1
.offset
;
1138 r
->len
= len
? len
: _ALIGN_UP(map
.total
, 1 << r
->page_size
);
1140 switch (dev
->dev_type
) {
1141 case PS3_DEVICE_TYPE_SB
:
1142 r
->region_ops
= (USE_DYNAMIC_DMA
)
1143 ? &ps3_dma_sb_region_ops
1144 : &ps3_dma_sb_region_linear_ops
;
1146 case PS3_DEVICE_TYPE_IOC0
:
1147 r
->region_ops
= &ps3_dma_ioc0_region_ops
;
1155 EXPORT_SYMBOL(ps3_dma_region_init
);
1157 int ps3_dma_region_create(struct ps3_dma_region
*r
)
1160 BUG_ON(!r
->region_ops
);
1161 BUG_ON(!r
->region_ops
->create
);
1162 return r
->region_ops
->create(r
);
1164 EXPORT_SYMBOL(ps3_dma_region_create
);
1166 int ps3_dma_region_free(struct ps3_dma_region
*r
)
1169 BUG_ON(!r
->region_ops
);
1170 BUG_ON(!r
->region_ops
->free
);
1171 return r
->region_ops
->free(r
);
1173 EXPORT_SYMBOL(ps3_dma_region_free
);
1175 int ps3_dma_map(struct ps3_dma_region
*r
, unsigned long virt_addr
,
1176 unsigned long len
, unsigned long *bus_addr
,
1179 return r
->region_ops
->map(r
, virt_addr
, len
, bus_addr
, iopte_flag
);
1182 int ps3_dma_unmap(struct ps3_dma_region
*r
, unsigned long bus_addr
,
1185 return r
->region_ops
->unmap(r
, bus_addr
, len
);
1188 /*============================================================================*/
1189 /* system startup routines */
1190 /*============================================================================*/
1193 * ps3_mm_init - initialize the address space state variables
1196 void __init
ps3_mm_init(void)
1200 DBG(" -> %s:%d\n", __func__
, __LINE__
);
1202 result
= ps3_repository_read_mm_info(&map
.rm
.base
, &map
.rm
.size
,
1206 panic("ps3_repository_read_mm_info() failed");
1208 map
.rm
.offset
= map
.rm
.base
;
1209 map
.vas_id
= map
.htab_size
= 0;
1211 /* this implementation assumes map.rm.base is zero */
1213 BUG_ON(map
.rm
.base
);
1214 BUG_ON(!map
.rm
.size
);
1217 /* arrange to do this in ps3_mm_add_memory */
1218 ps3_mm_region_create(&map
.r1
, map
.total
- map
.rm
.size
);
1220 /* correct map.total for the real total amount of memory we use */
1221 map
.total
= map
.rm
.size
+ map
.r1
.size
;
1223 DBG(" <- %s:%d\n", __func__
, __LINE__
);
1227 * ps3_mm_shutdown - final cleanup of address space
1230 void ps3_mm_shutdown(void)
1232 ps3_mm_region_destroy(&map
.r1
);