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(fmt...) udbg_printf(fmt)
35 #define DBG(fmt...) do{if(0)printk(fmt);}while(0)
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 _debug_dump_map(const struct map* m, const char* func, int line)
120 DBG("%s:%d: map.total = %lxh\n", func, line, m->total);
121 DBG("%s:%d: map.rm.size = %lxh\n", func, line, m->rm.size);
122 DBG("%s:%d: map.vas_id = %lu\n", func, line, m->vas_id);
123 DBG("%s:%d: map.htab_size = %lxh\n", func, line, m->htab_size);
124 DBG("%s:%d: map.r1.base = %lxh\n", func, line, m->r1.base);
125 DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset);
126 DBG("%s:%d: map.r1.size = %lxh\n", func, line, m->r1.size);
129 static struct map map;
132 * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address
133 * @phys_addr: linux physical address
136 unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr)
138 BUG_ON(is_kernel_addr(phys_addr));
142 return (phys_addr < map.rm.size || phys_addr >= map.total)
143 ? phys_addr : phys_addr + map.r1.offset;
146 EXPORT_SYMBOL(ps3_mm_phys_to_lpar);
149 * ps3_mm_vas_create - create the virtual address space
152 void __init ps3_mm_vas_create(unsigned long* htab_size)
155 unsigned long start_address;
157 unsigned long access_right;
158 unsigned long max_page_size;
161 result = lv1_query_logical_partition_address_region_info(0,
162 &start_address, &size, &access_right, &max_page_size,
166 DBG("%s:%d: lv1_query_logical_partition_address_region_info "
167 "failed: %s\n", __func__, __LINE__,
172 if (max_page_size < PAGE_SHIFT_16M) {
173 DBG("%s:%d: bad max_page_size %lxh\n", __func__, __LINE__,
178 BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX);
179 BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN);
181 result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE,
182 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K),
183 &map.vas_id, &map.htab_size);
186 DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n",
187 __func__, __LINE__, ps3_result(result));
191 result = lv1_select_virtual_address_space(map.vas_id);
194 DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n",
195 __func__, __LINE__, ps3_result(result));
199 *htab_size = map.htab_size;
201 debug_dump_map(&map);
206 panic("ps3_mm_vas_create failed");
210 * ps3_mm_vas_destroy -
213 void ps3_mm_vas_destroy(void)
216 lv1_select_virtual_address_space(0);
217 lv1_destruct_virtual_address_space(map.vas_id);
222 /*============================================================================*/
223 /* memory hotplug routines */
224 /*============================================================================*/
227 * ps3_mm_region_create - create a memory region in the vas
228 * @r: pointer to a struct mem_region to accept initialized values
229 * @size: requested region size
231 * This implementation creates the region with the vas large page size.
232 * @size is rounded down to a multiple of the vas large page size.
235 int ps3_mm_region_create(struct mem_region *r, unsigned long size)
240 r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
242 DBG("%s:%d requested %lxh\n", __func__, __LINE__, size);
243 DBG("%s:%d actual %lxh\n", __func__, __LINE__, r->size);
244 DBG("%s:%d difference %lxh (%luMB)\n", __func__, __LINE__,
245 (unsigned long)(size - r->size),
246 (size - r->size) / 1024 / 1024);
249 DBG("%s:%d: size == 0\n", __func__, __LINE__);
254 result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
255 ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
257 if (result || r->base < map.rm.size) {
258 DBG("%s:%d: lv1_allocate_memory failed: %s\n",
259 __func__, __LINE__, ps3_result(result));
263 r->offset = r->base - map.rm.size;
267 r->size = r->base = r->offset = 0;
272 * ps3_mm_region_destroy - destroy a memory region
273 * @r: pointer to struct mem_region
276 void ps3_mm_region_destroy(struct mem_region *r)
279 lv1_release_memory(r->base);
280 r->size = r->base = r->offset = 0;
281 map.total = map.rm.size;
286 * ps3_mm_add_memory - hot add memory
289 static int __init ps3_mm_add_memory(void)
292 unsigned long start_addr;
293 unsigned long start_pfn;
294 unsigned long nr_pages;
296 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
299 BUG_ON(!mem_init_done);
301 start_addr = USE_LPAR_ADDR ? map.r1.base : map.rm.size;
302 start_pfn = start_addr >> PAGE_SHIFT;
303 nr_pages = (map.r1.size + PAGE_SIZE - 1) >> PAGE_SHIFT;
305 DBG("%s:%d: start_addr %lxh, start_pfn %lxh, nr_pages %lxh\n",
306 __func__, __LINE__, start_addr, start_pfn, nr_pages);
308 result = add_memory(0, start_addr, map.r1.size);
311 DBG("%s:%d: add_memory failed: (%d)\n",
312 __func__, __LINE__, result);
316 result = online_pages(start_pfn, nr_pages);
319 DBG("%s:%d: online_pages failed: (%d)\n",
320 __func__, __LINE__, result);
325 core_initcall(ps3_mm_add_memory);
327 /*============================================================================*/
329 /*============================================================================*/
332 * dma_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
333 * @r: pointer to dma region structure
334 * @lpar_addr: HV lpar address
337 static unsigned long dma_lpar_to_bus(struct ps3_dma_region *r,
338 unsigned long lpar_addr)
340 BUG_ON(lpar_addr >= map.r1.base + map.r1.size);
341 return r->bus_addr + (lpar_addr <= map.rm.size ? lpar_addr
342 : lpar_addr - map.r1.offset);
345 #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
346 static void _dma_dump_region(const struct ps3_dma_region *r, const char* func,
349 DBG("%s:%d: dev %u:%u\n", func, line, r->did.bus_id,
351 DBG("%s:%d: page_size %u\n", func, line, r->page_size);
352 DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
353 DBG("%s:%d: len %lxh\n", func, line, r->len);
357 * dma_chunk - A chunk of dma pages mapped by the io controller.
358 * @region - The dma region that owns this chunk.
359 * @lpar_addr: Starting lpar address of the area to map.
360 * @bus_addr: Starting ioc bus address of the area to map.
361 * @len: Length in bytes of the area to map.
362 * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
363 * list of all chuncks owned by the region.
365 * This implementation uses a very simple dma page manager
366 * based on the dma_chunk structure. This scheme assumes
367 * that all drivers use very well behaved dma ops.
371 struct ps3_dma_region *region;
372 unsigned long lpar_addr;
373 unsigned long bus_addr;
375 struct list_head link;
376 unsigned int usage_count;
379 #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
380 static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
383 DBG("%s:%d: r.dev %u:%u\n", func, line,
384 c->region->did.bus_id, c->region->did.dev_id);
385 DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr);
386 DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size);
387 DBG("%s:%d: r.len %lxh\n", func, line, c->region->len);
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, 1 << r->page_size);
400 list_for_each_entry(c, &r->chunk_list.head, link) {
402 if (aligned_bus >= c->bus_addr
403 && aligned_bus < c->bus_addr + c->len
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) {
416 /* we don't handle the multi-chunk case for now */
424 static int dma_free_chunk(struct dma_chunk *c)
429 result = lv1_unmap_device_dma_region(c->region->did.bus_id,
430 c->region->did.dev_id, c->bus_addr, c->len);
439 * dma_map_pages - Maps dma pages into the io controller bus address space.
440 * @r: Pointer to a struct ps3_dma_region.
441 * @phys_addr: Starting physical address of the area to map.
442 * @len: Length in bytes of the area to map.
443 * c_out: A pointer to receive an allocated struct dma_chunk for this area.
445 * This is the lowest level dma mapping routine, and is the one that will
446 * make the HV call to add the pages into the io controller address space.
449 static int dma_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
450 unsigned long len, struct dma_chunk **c_out)
455 c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
463 c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
464 c->bus_addr = dma_lpar_to_bus(r, c->lpar_addr);
467 result = lv1_map_device_dma_region(c->region->did.bus_id,
468 c->region->did.dev_id, c->lpar_addr, c->bus_addr, c->len,
469 0xf800000000000000UL);
472 DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
473 __func__, __LINE__, ps3_result(result));
477 list_add(&c->link, &r->chunk_list.head);
486 DBG(" <- %s:%d\n", __func__, __LINE__);
491 * dma_region_create - Create a device dma region.
492 * @r: Pointer to a struct ps3_dma_region.
494 * This is the lowest level dma region create routine, and is the one that
495 * will make the HV call to create the region.
498 static int dma_region_create(struct ps3_dma_region* r)
502 r->len = _ALIGN_UP(map.total, 1 << r->page_size);
503 INIT_LIST_HEAD(&r->chunk_list.head);
504 spin_lock_init(&r->chunk_list.lock);
506 result = lv1_allocate_device_dma_region(r->did.bus_id, r->did.dev_id,
507 r->len, r->page_size, r->region_type, &r->bus_addr);
512 DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
513 __func__, __LINE__, ps3_result(result));
514 r->len = r->bus_addr = 0;
521 * dma_region_free - Free a device dma region.
522 * @r: Pointer to a struct ps3_dma_region.
524 * This is the lowest level dma region free routine, and is the one that
525 * will make the HV call to free the region.
528 static int dma_region_free(struct ps3_dma_region* r)
532 struct dma_chunk *tmp;
534 list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
539 result = lv1_free_device_dma_region(r->did.bus_id, r->did.dev_id,
543 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
544 __func__, __LINE__, ps3_result(result));
546 r->len = r->bus_addr = 0;
552 * dma_map_area - Map an area of memory into a device dma region.
553 * @r: Pointer to a struct ps3_dma_region.
554 * @virt_addr: Starting virtual address of the area to map.
555 * @len: Length in bytes of the area to map.
556 * @bus_addr: A pointer to return the starting ioc bus address of the area to
559 * This is the common dma mapping routine.
562 static int dma_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
563 unsigned long len, unsigned long *bus_addr)
568 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
571 *bus_addr = dma_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
573 if (!USE_DYNAMIC_DMA) {
574 unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
575 DBG(" -> %s:%d\n", __func__, __LINE__);
576 DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
578 DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
580 DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
582 DBG("%s:%d len %lxh\n", __func__, __LINE__, len);
583 DBG("%s:%d bus_addr %lxh (%lxh)\n", __func__, __LINE__,
587 spin_lock_irqsave(&r->chunk_list.lock, flags);
588 c = dma_find_chunk(r, *bus_addr, len);
592 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
596 result = dma_map_pages(r, _ALIGN_DOWN(phys_addr, 1 << r->page_size),
597 _ALIGN_UP(len, 1 << r->page_size), &c);
601 DBG("%s:%d: dma_map_pages failed (%d)\n",
602 __func__, __LINE__, result);
603 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
609 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
614 * dma_unmap_area - Unmap an area of memory from a device dma region.
615 * @r: Pointer to a struct ps3_dma_region.
616 * @bus_addr: The starting ioc bus address of the area to unmap.
617 * @len: Length in bytes of the area to unmap.
619 * This is the common dma unmap routine.
622 int dma_unmap_area(struct ps3_dma_region *r, unsigned long bus_addr,
628 spin_lock_irqsave(&r->chunk_list.lock, flags);
629 c = dma_find_chunk(r, bus_addr, len);
632 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
634 unsigned long aligned_len = _ALIGN_UP(len, 1 << r->page_size);
635 DBG("%s:%d: not found: bus_addr %lxh\n",
636 __func__, __LINE__, bus_addr);
637 DBG("%s:%d: not found: len %lxh\n",
638 __func__, __LINE__, len);
639 DBG("%s:%d: not found: aligned_bus %lxh\n",
640 __func__, __LINE__, aligned_bus);
641 DBG("%s:%d: not found: aligned_len %lxh\n",
642 __func__, __LINE__, aligned_len);
648 if (!c->usage_count) {
653 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
658 * dma_region_create_linear - Setup a linear dma maping for a device.
659 * @r: Pointer to a struct ps3_dma_region.
661 * This routine creates an HV dma region for the device and maps all available
662 * ram into the io controller bus address space.
665 static int dma_region_create_linear(struct ps3_dma_region *r)
670 /* force 16M dma pages for linear mapping */
672 if (r->page_size != PS3_DMA_16M) {
673 pr_info("%s:%d: forcing 16M pages for linear map\n",
675 r->page_size = PS3_DMA_16M;
678 result = dma_region_create(r);
681 result = dma_map_area(r, map.rm.base, map.rm.size, &tmp);
685 result = dma_map_area(r, map.r1.base, map.r1.size,
688 result = dma_map_area(r, map.rm.size, map.r1.size,
697 * dma_region_free_linear - Free a linear dma mapping for a device.
698 * @r: Pointer to a struct ps3_dma_region.
700 * This routine will unmap all mapped areas and free the HV dma region.
703 static int dma_region_free_linear(struct ps3_dma_region *r)
707 result = dma_unmap_area(r, dma_lpar_to_bus(r, 0), map.rm.size);
710 result = dma_unmap_area(r, dma_lpar_to_bus(r, map.r1.base),
714 result = dma_region_free(r);
721 * dma_map_area_linear - Map an area of memory into a device dma region.
722 * @r: Pointer to a struct ps3_dma_region.
723 * @virt_addr: Starting virtual address of the area to map.
724 * @len: Length in bytes of the area to map.
725 * @bus_addr: A pointer to return the starting ioc bus address of the area to
728 * This routine just returns the coresponding bus address. Actual mapping
729 * occurs in dma_region_create_linear().
732 static int dma_map_area_linear(struct ps3_dma_region *r,
733 unsigned long virt_addr, unsigned long len, unsigned long *bus_addr)
735 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
737 *bus_addr = dma_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
742 * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
743 * @r: Pointer to a struct ps3_dma_region.
744 * @bus_addr: The starting ioc bus address of the area to unmap.
745 * @len: Length in bytes of the area to unmap.
747 * This routine does nothing. Unmapping occurs in dma_region_free_linear().
750 static int dma_unmap_area_linear(struct ps3_dma_region *r,
751 unsigned long bus_addr, unsigned long len)
756 int ps3_dma_region_create(struct ps3_dma_region *r)
758 return (USE_DYNAMIC_DMA)
759 ? dma_region_create(r)
760 : dma_region_create_linear(r);
763 int ps3_dma_region_free(struct ps3_dma_region *r)
765 return (USE_DYNAMIC_DMA)
767 : dma_region_free_linear(r);
770 int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
771 unsigned long len, unsigned long *bus_addr)
773 return (USE_DYNAMIC_DMA)
774 ? dma_map_area(r, virt_addr, len, bus_addr)
775 : dma_map_area_linear(r, virt_addr, len, bus_addr);
778 int ps3_dma_unmap(struct ps3_dma_region *r, unsigned long bus_addr,
781 return (USE_DYNAMIC_DMA) ? dma_unmap_area(r, bus_addr, len)
782 : dma_unmap_area_linear(r, bus_addr, len);
785 /*============================================================================*/
786 /* system startup routines */
787 /*============================================================================*/
790 * ps3_mm_init - initialize the address space state variables
793 void __init ps3_mm_init(void)
797 DBG(" -> %s:%d\n", __func__, __LINE__);
799 result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
803 panic("ps3_repository_read_mm_info() failed");
805 map.rm.offset = map.rm.base;
806 map.vas_id = map.htab_size = 0;
808 /* this implementation assumes map.rm.base is zero */
811 BUG_ON(!map.rm.size);
813 lmb_add(map.rm.base, map.rm.size);
816 /* arrange to do this in ps3_mm_add_memory */
817 ps3_mm_region_create(&map.r1, map.total - map.rm.size);
819 DBG(" <- %s:%d\n", __func__, __LINE__);
823 * ps3_mm_shutdown - final cleanup of address space
826 void ps3_mm_shutdown(void)
828 ps3_mm_region_destroy(&map.r1);
829 map.total = map.rm.size;