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>
29 #include <asm/lv1call.h>
34 #define DBG(fmt...) udbg_printf(fmt)
36 #define DBG(fmt...) do{if(0)printk(fmt);}while(0)
40 #if defined(CONFIG_PS3_USE_LPAR_ADDR)
45 #if defined(CONFIG_PS3_DYNAMIC_DMA)
58 static unsigned long make_page_sizes(unsigned long a, unsigned long b)
60 return (a << 56) | (b << 48);
64 ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04,
65 ALLOCATE_MEMORY_ADDR_ZERO = 0X08,
68 /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */
71 HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */
72 HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */
75 /*============================================================================*/
76 /* virtual address space routines */
77 /*============================================================================*/
80 * struct mem_region - memory region structure
82 * @size: size in bytes
83 * @offset: difference between base and rm.size
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: hotplug memory 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
112 unsigned long vas_id;
113 unsigned long htab_size;
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 _debug_dump_map(const struct map* m, 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)
217 lv1_select_virtual_address_space(0);
218 lv1_destruct_virtual_address_space(map.vas_id);
223 /*============================================================================*/
224 /* memory hotplug routines */
225 /*============================================================================*/
228 * ps3_mm_region_create - create a memory region in the vas
229 * @r: pointer to a struct mem_region to accept initialized values
230 * @size: requested region size
232 * This implementation creates the region with the vas large page size.
233 * @size is rounded down to a multiple of the vas large page size.
236 int ps3_mm_region_create(struct mem_region *r, unsigned long size)
241 r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M);
243 DBG("%s:%d requested %lxh\n", __func__, __LINE__, size);
244 DBG("%s:%d actual %lxh\n", __func__, __LINE__, r->size);
245 DBG("%s:%d difference %lxh (%luMB)\n", __func__, __LINE__,
246 (unsigned long)(size - r->size),
247 (size - r->size) / 1024 / 1024);
250 DBG("%s:%d: size == 0\n", __func__, __LINE__);
255 result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0,
256 ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid);
258 if (result || r->base < map.rm.size) {
259 DBG("%s:%d: lv1_allocate_memory failed: %s\n",
260 __func__, __LINE__, ps3_result(result));
264 r->offset = r->base - map.rm.size;
268 r->size = r->base = r->offset = 0;
273 * ps3_mm_region_destroy - destroy a memory region
274 * @r: pointer to struct mem_region
277 void ps3_mm_region_destroy(struct mem_region *r)
280 lv1_release_memory(r->base);
281 r->size = r->base = r->offset = 0;
282 map.total = map.rm.size;
287 * ps3_mm_add_memory - hot add memory
290 static int __init ps3_mm_add_memory(void)
293 unsigned long start_addr;
294 unsigned long start_pfn;
295 unsigned long nr_pages;
297 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
300 BUG_ON(!mem_init_done);
302 start_addr = USE_LPAR_ADDR ? map.r1.base : map.rm.size;
303 start_pfn = start_addr >> PAGE_SHIFT;
304 nr_pages = (map.r1.size + PAGE_SIZE - 1) >> PAGE_SHIFT;
306 DBG("%s:%d: start_addr %lxh, start_pfn %lxh, nr_pages %lxh\n",
307 __func__, __LINE__, start_addr, start_pfn, nr_pages);
309 result = add_memory(0, start_addr, map.r1.size);
312 DBG("%s:%d: add_memory failed: (%d)\n",
313 __func__, __LINE__, result);
317 result = online_pages(start_pfn, nr_pages);
320 DBG("%s:%d: online_pages failed: (%d)\n",
321 __func__, __LINE__, result);
326 core_initcall(ps3_mm_add_memory);
328 /*============================================================================*/
330 /*============================================================================*/
333 * dma_lpar_to_bus - Translate an lpar address to ioc mapped bus address.
334 * @r: pointer to dma region structure
335 * @lpar_addr: HV lpar address
338 static unsigned long dma_lpar_to_bus(struct ps3_dma_region *r,
339 unsigned long lpar_addr)
341 BUG_ON(lpar_addr >= map.r1.base + map.r1.size);
342 return r->bus_addr + (lpar_addr <= map.rm.size ? lpar_addr
343 : lpar_addr - map.r1.offset);
346 #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__)
347 static void _dma_dump_region(const struct ps3_dma_region *r, const char* func,
350 DBG("%s:%d: dev %u:%u\n", func, line, r->did.bus_id,
352 DBG("%s:%d: page_size %u\n", func, line, r->page_size);
353 DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
354 DBG("%s:%d: len %lxh\n", func, line, r->len);
358 * dma_chunk - A chunk of dma pages mapped by the io controller.
359 * @region - The dma region that owns this chunk.
360 * @lpar_addr: Starting lpar address of the area to map.
361 * @bus_addr: Starting ioc bus address of the area to map.
362 * @len: Length in bytes of the area to map.
363 * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the
364 * list of all chuncks owned by the region.
366 * This implementation uses a very simple dma page manager
367 * based on the dma_chunk structure. This scheme assumes
368 * that all drivers use very well behaved dma ops.
372 struct ps3_dma_region *region;
373 unsigned long lpar_addr;
374 unsigned long bus_addr;
376 struct list_head link;
377 unsigned int usage_count;
380 #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__)
381 static void _dma_dump_chunk (const struct dma_chunk* c, const char* func,
384 DBG("%s:%d: r.dev %u:%u\n", func, line,
385 c->region->did.bus_id, c->region->did.dev_id);
386 DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr);
387 DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size);
388 DBG("%s:%d: r.len %lxh\n", func, line, c->region->len);
389 DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr);
390 DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr);
391 DBG("%s:%d: c.len %lxh\n", func, line, c->len);
394 static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r,
395 unsigned long bus_addr, unsigned long len)
398 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 1 << r->page_size);
399 unsigned long aligned_len = _ALIGN_UP(len, 1 << r->page_size);
401 list_for_each_entry(c, &r->chunk_list.head, link) {
403 if (aligned_bus >= c->bus_addr
404 && aligned_bus < c->bus_addr + c->len
405 && aligned_bus + aligned_len <= c->bus_addr + c->len) {
409 if (aligned_bus + aligned_len <= c->bus_addr) {
413 if (aligned_bus >= c->bus_addr + c->len) {
417 /* we don't handle the multi-chunk case for now */
425 static int dma_free_chunk(struct dma_chunk *c)
430 result = lv1_unmap_device_dma_region(c->region->did.bus_id,
431 c->region->did.dev_id, c->bus_addr, c->len);
440 * dma_map_pages - Maps dma pages into the io controller bus address space.
441 * @r: Pointer to a struct ps3_dma_region.
442 * @phys_addr: Starting physical address of the area to map.
443 * @len: Length in bytes of the area to map.
444 * c_out: A pointer to receive an allocated struct dma_chunk for this area.
446 * This is the lowest level dma mapping routine, and is the one that will
447 * make the HV call to add the pages into the io controller address space.
450 static int dma_map_pages(struct ps3_dma_region *r, unsigned long phys_addr,
451 unsigned long len, struct dma_chunk **c_out)
456 c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC);
464 c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
465 c->bus_addr = dma_lpar_to_bus(r, c->lpar_addr);
468 result = lv1_map_device_dma_region(c->region->did.bus_id,
469 c->region->did.dev_id, c->lpar_addr, c->bus_addr, c->len,
470 0xf800000000000000UL);
473 DBG("%s:%d: lv1_map_device_dma_region failed: %s\n",
474 __func__, __LINE__, ps3_result(result));
478 list_add(&c->link, &r->chunk_list.head);
487 DBG(" <- %s:%d\n", __func__, __LINE__);
492 * dma_region_create - Create a device dma region.
493 * @r: Pointer to a struct ps3_dma_region.
495 * This is the lowest level dma region create routine, and is the one that
496 * will make the HV call to create the region.
499 static int dma_region_create(struct ps3_dma_region* r)
503 r->len = _ALIGN_UP(map.total, 1 << r->page_size);
504 INIT_LIST_HEAD(&r->chunk_list.head);
505 spin_lock_init(&r->chunk_list.lock);
507 result = lv1_allocate_device_dma_region(r->did.bus_id, r->did.dev_id,
508 r->len, r->page_size, r->region_type, &r->bus_addr);
513 DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n",
514 __func__, __LINE__, ps3_result(result));
515 r->len = r->bus_addr = 0;
522 * dma_region_free - Free a device dma region.
523 * @r: Pointer to a struct ps3_dma_region.
525 * This is the lowest level dma region free routine, and is the one that
526 * will make the HV call to free the region.
529 static int dma_region_free(struct ps3_dma_region* r)
533 struct dma_chunk *tmp;
535 list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) {
540 result = lv1_free_device_dma_region(r->did.bus_id, r->did.dev_id,
544 DBG("%s:%d: lv1_free_device_dma_region failed: %s\n",
545 __func__, __LINE__, ps3_result(result));
547 r->len = r->bus_addr = 0;
553 * dma_map_area - Map an area of memory into a device dma region.
554 * @r: Pointer to a struct ps3_dma_region.
555 * @virt_addr: Starting virtual address of the area to map.
556 * @len: Length in bytes of the area to map.
557 * @bus_addr: A pointer to return the starting ioc bus address of the area to
560 * This is the common dma mapping routine.
563 static int dma_map_area(struct ps3_dma_region *r, unsigned long virt_addr,
564 unsigned long len, unsigned long *bus_addr)
569 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
572 *bus_addr = dma_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
574 if (!USE_DYNAMIC_DMA) {
575 unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr);
576 DBG(" -> %s:%d\n", __func__, __LINE__);
577 DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__,
579 DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__,
581 DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__,
583 DBG("%s:%d len %lxh\n", __func__, __LINE__, len);
584 DBG("%s:%d bus_addr %lxh (%lxh)\n", __func__, __LINE__,
588 spin_lock_irqsave(&r->chunk_list.lock, flags);
589 c = dma_find_chunk(r, *bus_addr, len);
593 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
597 result = dma_map_pages(r, _ALIGN_DOWN(phys_addr, 1 << r->page_size),
598 _ALIGN_UP(len, 1 << r->page_size), &c);
602 DBG("%s:%d: dma_map_pages failed (%d)\n",
603 __func__, __LINE__, result);
604 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
610 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
615 * dma_unmap_area - Unmap an area of memory from a device dma region.
616 * @r: Pointer to a struct ps3_dma_region.
617 * @bus_addr: The starting ioc bus address of the area to unmap.
618 * @len: Length in bytes of the area to unmap.
620 * This is the common dma unmap routine.
623 int dma_unmap_area(struct ps3_dma_region *r, unsigned long bus_addr,
629 spin_lock_irqsave(&r->chunk_list.lock, flags);
630 c = dma_find_chunk(r, bus_addr, len);
633 unsigned long aligned_bus = _ALIGN_DOWN(bus_addr,
635 unsigned long aligned_len = _ALIGN_UP(len, 1 << r->page_size);
636 DBG("%s:%d: not found: bus_addr %lxh\n",
637 __func__, __LINE__, bus_addr);
638 DBG("%s:%d: not found: len %lxh\n",
639 __func__, __LINE__, len);
640 DBG("%s:%d: not found: aligned_bus %lxh\n",
641 __func__, __LINE__, aligned_bus);
642 DBG("%s:%d: not found: aligned_len %lxh\n",
643 __func__, __LINE__, aligned_len);
649 if (!c->usage_count) {
654 spin_unlock_irqrestore(&r->chunk_list.lock, flags);
659 * dma_region_create_linear - Setup a linear dma maping for a device.
660 * @r: Pointer to a struct ps3_dma_region.
662 * This routine creates an HV dma region for the device and maps all available
663 * ram into the io controller bus address space.
666 static int dma_region_create_linear(struct ps3_dma_region *r)
671 /* force 16M dma pages for linear mapping */
673 if (r->page_size != PS3_DMA_16M) {
674 pr_info("%s:%d: forcing 16M pages for linear map\n",
676 r->page_size = PS3_DMA_16M;
679 result = dma_region_create(r);
682 result = dma_map_area(r, map.rm.base, map.rm.size, &tmp);
686 result = dma_map_area(r, map.r1.base, map.r1.size,
689 result = dma_map_area(r, map.rm.size, map.r1.size,
698 * dma_region_free_linear - Free a linear dma mapping for a device.
699 * @r: Pointer to a struct ps3_dma_region.
701 * This routine will unmap all mapped areas and free the HV dma region.
704 static int dma_region_free_linear(struct ps3_dma_region *r)
708 result = dma_unmap_area(r, dma_lpar_to_bus(r, 0), map.rm.size);
711 result = dma_unmap_area(r, dma_lpar_to_bus(r, map.r1.base),
715 result = dma_region_free(r);
722 * dma_map_area_linear - Map an area of memory into a device dma region.
723 * @r: Pointer to a struct ps3_dma_region.
724 * @virt_addr: Starting virtual address of the area to map.
725 * @len: Length in bytes of the area to map.
726 * @bus_addr: A pointer to return the starting ioc bus address of the area to
729 * This routine just returns the coresponding bus address. Actual mapping
730 * occurs in dma_region_create_linear().
733 static int dma_map_area_linear(struct ps3_dma_region *r,
734 unsigned long virt_addr, unsigned long len, unsigned long *bus_addr)
736 unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr)
738 *bus_addr = dma_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr));
743 * dma_unmap_area_linear - Unmap an area of memory from a device dma region.
744 * @r: Pointer to a struct ps3_dma_region.
745 * @bus_addr: The starting ioc bus address of the area to unmap.
746 * @len: Length in bytes of the area to unmap.
748 * This routine does nothing. Unmapping occurs in dma_region_free_linear().
751 static int dma_unmap_area_linear(struct ps3_dma_region *r,
752 unsigned long bus_addr, unsigned long len)
757 int ps3_dma_region_create(struct ps3_dma_region *r)
759 return (USE_DYNAMIC_DMA)
760 ? dma_region_create(r)
761 : dma_region_create_linear(r);
764 int ps3_dma_region_free(struct ps3_dma_region *r)
766 return (USE_DYNAMIC_DMA)
768 : dma_region_free_linear(r);
771 int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr,
772 unsigned long len, unsigned long *bus_addr)
774 return (USE_DYNAMIC_DMA)
775 ? dma_map_area(r, virt_addr, len, bus_addr)
776 : dma_map_area_linear(r, virt_addr, len, bus_addr);
779 int ps3_dma_unmap(struct ps3_dma_region *r, unsigned long bus_addr,
782 return (USE_DYNAMIC_DMA) ? dma_unmap_area(r, bus_addr, len)
783 : dma_unmap_area_linear(r, bus_addr, len);
786 /*============================================================================*/
787 /* system startup routines */
788 /*============================================================================*/
791 * ps3_mm_init - initialize the address space state variables
794 void __init ps3_mm_init(void)
798 DBG(" -> %s:%d\n", __func__, __LINE__);
800 result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size,
804 panic("ps3_repository_read_mm_info() failed");
806 map.rm.offset = map.rm.base;
807 map.vas_id = map.htab_size = 0;
809 /* this implementation assumes map.rm.base is zero */
812 BUG_ON(!map.rm.size);
814 lmb_add(map.rm.base, map.rm.size);
817 /* arrange to do this in ps3_mm_add_memory */
818 ps3_mm_region_create(&map.r1, map.total - map.rm.size);
820 DBG(" <- %s:%d\n", __func__, __LINE__);
824 * ps3_mm_shutdown - final cleanup of address space
827 void ps3_mm_shutdown(void)
829 ps3_mm_region_destroy(&map.r1);
830 map.total = map.rm.size;