1f58a9d17SGeoff Levand /* 2f58a9d17SGeoff Levand * PS3 address space management. 3f58a9d17SGeoff Levand * 4f58a9d17SGeoff Levand * Copyright (C) 2006 Sony Computer Entertainment Inc. 5f58a9d17SGeoff Levand * Copyright 2006 Sony Corp. 6f58a9d17SGeoff Levand * 7f58a9d17SGeoff Levand * This program is free software; you can redistribute it and/or modify 8f58a9d17SGeoff Levand * it under the terms of the GNU General Public License as published by 9f58a9d17SGeoff Levand * the Free Software Foundation; version 2 of the License. 10f58a9d17SGeoff Levand * 11f58a9d17SGeoff Levand * This program is distributed in the hope that it will be useful, 12f58a9d17SGeoff Levand * but WITHOUT ANY WARRANTY; without even the implied warranty of 13f58a9d17SGeoff Levand * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14f58a9d17SGeoff Levand * GNU General Public License for more details. 15f58a9d17SGeoff Levand * 16f58a9d17SGeoff Levand * You should have received a copy of the GNU General Public License 17f58a9d17SGeoff Levand * along with this program; if not, write to the Free Software 18f58a9d17SGeoff Levand * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19f58a9d17SGeoff Levand */ 20f58a9d17SGeoff Levand 21f58a9d17SGeoff Levand #include <linux/kernel.h> 224b16f8e2SPaul Gortmaker #include <linux/export.h> 23f58a9d17SGeoff Levand #include <linux/memory_hotplug.h> 2495f72d1eSYinghai Lu #include <linux/memblock.h> 255a0e3ad6STejun Heo #include <linux/slab.h> 26f58a9d17SGeoff Levand 279413c883SGeert Uytterhoeven #include <asm/cell-regs.h> 28e22ba7e3SArnd Bergmann #include <asm/firmware.h> 29d9b2b2a2SDavid S. Miller #include <asm/prom.h> 30f58a9d17SGeoff Levand #include <asm/udbg.h> 31f58a9d17SGeoff Levand #include <asm/lv1call.h> 32ae3a197eSDavid Howells #include <asm/setup.h> 33f58a9d17SGeoff Levand 34f58a9d17SGeoff Levand #include "platform.h" 35f58a9d17SGeoff Levand 36f58a9d17SGeoff Levand #if defined(DEBUG) 3783bb643dSGeert Uytterhoeven #define DBG udbg_printf 38f58a9d17SGeoff Levand #else 397424639aSMichael Ellerman #define DBG pr_devel 40f58a9d17SGeoff Levand #endif 41f58a9d17SGeoff Levand 42f58a9d17SGeoff Levand enum { 43f58a9d17SGeoff Levand #if defined(CONFIG_PS3_DYNAMIC_DMA) 44f58a9d17SGeoff Levand USE_DYNAMIC_DMA = 1, 45f58a9d17SGeoff Levand #else 46f58a9d17SGeoff Levand USE_DYNAMIC_DMA = 0, 47f58a9d17SGeoff Levand #endif 48f58a9d17SGeoff Levand }; 49f58a9d17SGeoff Levand 50f58a9d17SGeoff Levand enum { 51f58a9d17SGeoff Levand PAGE_SHIFT_4K = 12U, 52f58a9d17SGeoff Levand PAGE_SHIFT_64K = 16U, 53f58a9d17SGeoff Levand PAGE_SHIFT_16M = 24U, 54f58a9d17SGeoff Levand }; 55f58a9d17SGeoff Levand 56f58a9d17SGeoff Levand static unsigned long make_page_sizes(unsigned long a, unsigned long b) 57f58a9d17SGeoff Levand { 58f58a9d17SGeoff Levand return (a << 56) | (b << 48); 59f58a9d17SGeoff Levand } 60f58a9d17SGeoff Levand 61f58a9d17SGeoff Levand enum { 62f58a9d17SGeoff Levand ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04, 63f58a9d17SGeoff Levand ALLOCATE_MEMORY_ADDR_ZERO = 0X08, 64f58a9d17SGeoff Levand }; 65f58a9d17SGeoff Levand 66f58a9d17SGeoff Levand /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */ 67f58a9d17SGeoff Levand 68f58a9d17SGeoff Levand enum { 69f58a9d17SGeoff Levand HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */ 70f58a9d17SGeoff Levand HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */ 71f58a9d17SGeoff Levand }; 72f58a9d17SGeoff Levand 73f58a9d17SGeoff Levand /*============================================================================*/ 74f58a9d17SGeoff Levand /* virtual address space routines */ 75f58a9d17SGeoff Levand /*============================================================================*/ 76f58a9d17SGeoff Levand 77f58a9d17SGeoff Levand /** 78f58a9d17SGeoff Levand * struct mem_region - memory region structure 79f58a9d17SGeoff Levand * @base: base address 80f58a9d17SGeoff Levand * @size: size in bytes 81f58a9d17SGeoff Levand * @offset: difference between base and rm.size 82*1e755c09SAndre Heider * @destroy: flag if region should be destroyed upon shutdown 83f58a9d17SGeoff Levand */ 84f58a9d17SGeoff Levand 85f58a9d17SGeoff Levand struct mem_region { 86b17b3df1SStephen Rothwell u64 base; 875418b9c6SStephen Rothwell u64 size; 88f58a9d17SGeoff Levand unsigned long offset; 89*1e755c09SAndre Heider int destroy; 90f58a9d17SGeoff Levand }; 91f58a9d17SGeoff Levand 92f58a9d17SGeoff Levand /** 93f58a9d17SGeoff Levand * struct map - address space state variables holder 94f58a9d17SGeoff Levand * @total: total memory available as reported by HV 95f58a9d17SGeoff Levand * @vas_id - HV virtual address space id 96f58a9d17SGeoff Levand * @htab_size: htab size in bytes 97f58a9d17SGeoff Levand * 98f58a9d17SGeoff Levand * The HV virtual address space (vas) allows for hotplug memory regions. 99f58a9d17SGeoff Levand * Memory regions can be created and destroyed in the vas at runtime. 100f58a9d17SGeoff Levand * @rm: real mode (bootmem) region 101f58a9d17SGeoff Levand * @r1: hotplug memory region(s) 102f58a9d17SGeoff Levand * 103f58a9d17SGeoff Levand * ps3 addresses 104f58a9d17SGeoff Levand * virt_addr: a cpu 'translated' effective address 105f58a9d17SGeoff Levand * phys_addr: an address in what Linux thinks is the physical address space 106f58a9d17SGeoff Levand * lpar_addr: an address in the HV virtual address space 107f58a9d17SGeoff Levand * bus_addr: an io controller 'translated' address on a device bus 108f58a9d17SGeoff Levand */ 109f58a9d17SGeoff Levand 110f58a9d17SGeoff Levand struct map { 1115418b9c6SStephen Rothwell u64 total; 112b17b3df1SStephen Rothwell u64 vas_id; 113b17b3df1SStephen Rothwell u64 htab_size; 114f58a9d17SGeoff Levand struct mem_region rm; 115f58a9d17SGeoff Levand struct mem_region r1; 116f58a9d17SGeoff Levand }; 117f58a9d17SGeoff Levand 118f58a9d17SGeoff Levand #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__) 1196bb5cf10SGeoff Levand static void __maybe_unused _debug_dump_map(const struct map *m, 1206bb5cf10SGeoff Levand const char *func, int line) 121f58a9d17SGeoff Levand { 1225418b9c6SStephen Rothwell DBG("%s:%d: map.total = %llxh\n", func, line, m->total); 1235418b9c6SStephen Rothwell DBG("%s:%d: map.rm.size = %llxh\n", func, line, m->rm.size); 124b17b3df1SStephen Rothwell DBG("%s:%d: map.vas_id = %llu\n", func, line, m->vas_id); 125b17b3df1SStephen Rothwell DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size); 126b17b3df1SStephen Rothwell DBG("%s:%d: map.r1.base = %llxh\n", func, line, m->r1.base); 127f58a9d17SGeoff Levand DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset); 1285418b9c6SStephen Rothwell DBG("%s:%d: map.r1.size = %llxh\n", func, line, m->r1.size); 129f58a9d17SGeoff Levand } 130f58a9d17SGeoff Levand 131f58a9d17SGeoff Levand static struct map map; 132f58a9d17SGeoff Levand 133f58a9d17SGeoff Levand /** 134f58a9d17SGeoff Levand * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address 135f58a9d17SGeoff Levand * @phys_addr: linux physical address 136f58a9d17SGeoff Levand */ 137f58a9d17SGeoff Levand 138f58a9d17SGeoff Levand unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr) 139f58a9d17SGeoff Levand { 140f58a9d17SGeoff Levand BUG_ON(is_kernel_addr(phys_addr)); 141f58a9d17SGeoff Levand return (phys_addr < map.rm.size || phys_addr >= map.total) 142f58a9d17SGeoff Levand ? phys_addr : phys_addr + map.r1.offset; 143f58a9d17SGeoff Levand } 144f58a9d17SGeoff Levand 145f58a9d17SGeoff Levand EXPORT_SYMBOL(ps3_mm_phys_to_lpar); 146f58a9d17SGeoff Levand 147f58a9d17SGeoff Levand /** 148f58a9d17SGeoff Levand * ps3_mm_vas_create - create the virtual address space 149f58a9d17SGeoff Levand */ 150f58a9d17SGeoff Levand 151f58a9d17SGeoff Levand void __init ps3_mm_vas_create(unsigned long* htab_size) 152f58a9d17SGeoff Levand { 153f58a9d17SGeoff Levand int result; 154b17b3df1SStephen Rothwell u64 start_address; 155b17b3df1SStephen Rothwell u64 size; 156b17b3df1SStephen Rothwell u64 access_right; 157b17b3df1SStephen Rothwell u64 max_page_size; 158b17b3df1SStephen Rothwell u64 flags; 159f58a9d17SGeoff Levand 160f58a9d17SGeoff Levand result = lv1_query_logical_partition_address_region_info(0, 161f58a9d17SGeoff Levand &start_address, &size, &access_right, &max_page_size, 162f58a9d17SGeoff Levand &flags); 163f58a9d17SGeoff Levand 164f58a9d17SGeoff Levand if (result) { 165f58a9d17SGeoff Levand DBG("%s:%d: lv1_query_logical_partition_address_region_info " 166f58a9d17SGeoff Levand "failed: %s\n", __func__, __LINE__, 167f58a9d17SGeoff Levand ps3_result(result)); 168f58a9d17SGeoff Levand goto fail; 169f58a9d17SGeoff Levand } 170f58a9d17SGeoff Levand 171f58a9d17SGeoff Levand if (max_page_size < PAGE_SHIFT_16M) { 172b17b3df1SStephen Rothwell DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__, 173f58a9d17SGeoff Levand max_page_size); 174f58a9d17SGeoff Levand goto fail; 175f58a9d17SGeoff Levand } 176f58a9d17SGeoff Levand 177f58a9d17SGeoff Levand BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX); 178f58a9d17SGeoff Levand BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN); 179f58a9d17SGeoff Levand 180f58a9d17SGeoff Levand result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE, 181f58a9d17SGeoff Levand 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K), 182f58a9d17SGeoff Levand &map.vas_id, &map.htab_size); 183f58a9d17SGeoff Levand 184f58a9d17SGeoff Levand if (result) { 185f58a9d17SGeoff Levand DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n", 186f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 187f58a9d17SGeoff Levand goto fail; 188f58a9d17SGeoff Levand } 189f58a9d17SGeoff Levand 190f58a9d17SGeoff Levand result = lv1_select_virtual_address_space(map.vas_id); 191f58a9d17SGeoff Levand 192f58a9d17SGeoff Levand if (result) { 193f58a9d17SGeoff Levand DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n", 194f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 195f58a9d17SGeoff Levand goto fail; 196f58a9d17SGeoff Levand } 197f58a9d17SGeoff Levand 198f58a9d17SGeoff Levand *htab_size = map.htab_size; 199f58a9d17SGeoff Levand 200f58a9d17SGeoff Levand debug_dump_map(&map); 201f58a9d17SGeoff Levand 202f58a9d17SGeoff Levand return; 203f58a9d17SGeoff Levand 204f58a9d17SGeoff Levand fail: 205f58a9d17SGeoff Levand panic("ps3_mm_vas_create failed"); 206f58a9d17SGeoff Levand } 207f58a9d17SGeoff Levand 208f58a9d17SGeoff Levand /** 209f58a9d17SGeoff Levand * ps3_mm_vas_destroy - 210f58a9d17SGeoff Levand */ 211f58a9d17SGeoff Levand 212f58a9d17SGeoff Levand void ps3_mm_vas_destroy(void) 213f58a9d17SGeoff Levand { 2146bb5cf10SGeoff Levand int result; 2156bb5cf10SGeoff Levand 216b17b3df1SStephen Rothwell DBG("%s:%d: map.vas_id = %llu\n", __func__, __LINE__, map.vas_id); 2176bb5cf10SGeoff Levand 218f58a9d17SGeoff Levand if (map.vas_id) { 2196bb5cf10SGeoff Levand result = lv1_select_virtual_address_space(0); 2206bb5cf10SGeoff Levand BUG_ON(result); 2216bb5cf10SGeoff Levand result = lv1_destruct_virtual_address_space(map.vas_id); 2226bb5cf10SGeoff Levand BUG_ON(result); 223f58a9d17SGeoff Levand map.vas_id = 0; 224f58a9d17SGeoff Levand } 225f58a9d17SGeoff Levand } 226f58a9d17SGeoff Levand 227f58a9d17SGeoff Levand /*============================================================================*/ 228f58a9d17SGeoff Levand /* memory hotplug routines */ 229f58a9d17SGeoff Levand /*============================================================================*/ 230f58a9d17SGeoff Levand 231f58a9d17SGeoff Levand /** 232f58a9d17SGeoff Levand * ps3_mm_region_create - create a memory region in the vas 233f58a9d17SGeoff Levand * @r: pointer to a struct mem_region to accept initialized values 234f58a9d17SGeoff Levand * @size: requested region size 235f58a9d17SGeoff Levand * 236f58a9d17SGeoff Levand * This implementation creates the region with the vas large page size. 237f58a9d17SGeoff Levand * @size is rounded down to a multiple of the vas large page size. 238f58a9d17SGeoff Levand */ 239f58a9d17SGeoff Levand 24032f44a12SGeert Uytterhoeven static int ps3_mm_region_create(struct mem_region *r, unsigned long size) 241f58a9d17SGeoff Levand { 242f58a9d17SGeoff Levand int result; 243b17b3df1SStephen Rothwell u64 muid; 244f58a9d17SGeoff Levand 245f58a9d17SGeoff Levand r->size = _ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M); 246f58a9d17SGeoff Levand 247f58a9d17SGeoff Levand DBG("%s:%d requested %lxh\n", __func__, __LINE__, size); 2485418b9c6SStephen Rothwell DBG("%s:%d actual %llxh\n", __func__, __LINE__, r->size); 2495418b9c6SStephen Rothwell DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__, 2505418b9c6SStephen Rothwell size - r->size, (size - r->size) / 1024 / 1024); 251f58a9d17SGeoff Levand 252f58a9d17SGeoff Levand if (r->size == 0) { 253f58a9d17SGeoff Levand DBG("%s:%d: size == 0\n", __func__, __LINE__); 254f58a9d17SGeoff Levand result = -1; 255f58a9d17SGeoff Levand goto zero_region; 256f58a9d17SGeoff Levand } 257f58a9d17SGeoff Levand 258f58a9d17SGeoff Levand result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0, 259f58a9d17SGeoff Levand ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid); 260f58a9d17SGeoff Levand 261f58a9d17SGeoff Levand if (result || r->base < map.rm.size) { 262f58a9d17SGeoff Levand DBG("%s:%d: lv1_allocate_memory failed: %s\n", 263f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 264f58a9d17SGeoff Levand goto zero_region; 265f58a9d17SGeoff Levand } 266f58a9d17SGeoff Levand 267*1e755c09SAndre Heider r->destroy = 1; 268f58a9d17SGeoff Levand r->offset = r->base - map.rm.size; 269f58a9d17SGeoff Levand return result; 270f58a9d17SGeoff Levand 271f58a9d17SGeoff Levand zero_region: 272f58a9d17SGeoff Levand r->size = r->base = r->offset = 0; 273f58a9d17SGeoff Levand return result; 274f58a9d17SGeoff Levand } 275f58a9d17SGeoff Levand 276f58a9d17SGeoff Levand /** 277f58a9d17SGeoff Levand * ps3_mm_region_destroy - destroy a memory region 278f58a9d17SGeoff Levand * @r: pointer to struct mem_region 279f58a9d17SGeoff Levand */ 280f58a9d17SGeoff Levand 28132f44a12SGeert Uytterhoeven static void ps3_mm_region_destroy(struct mem_region *r) 282f58a9d17SGeoff Levand { 2836bb5cf10SGeoff Levand int result; 2846bb5cf10SGeoff Levand 285*1e755c09SAndre Heider if (!r->destroy) { 286*1e755c09SAndre Heider pr_info("%s:%d: Not destroying high region: %llxh %llxh\n", 287*1e755c09SAndre Heider __func__, __LINE__, r->base, r->size); 288*1e755c09SAndre Heider return; 289*1e755c09SAndre Heider } 290*1e755c09SAndre Heider 291b17b3df1SStephen Rothwell DBG("%s:%d: r->base = %llxh\n", __func__, __LINE__, r->base); 292*1e755c09SAndre Heider 293f58a9d17SGeoff Levand if (r->base) { 2946bb5cf10SGeoff Levand result = lv1_release_memory(r->base); 2956bb5cf10SGeoff Levand BUG_ON(result); 296f58a9d17SGeoff Levand r->size = r->base = r->offset = 0; 297f58a9d17SGeoff Levand map.total = map.rm.size; 298f58a9d17SGeoff Levand } 299f58a9d17SGeoff Levand } 300f58a9d17SGeoff Levand 301*1e755c09SAndre Heider static int ps3_mm_get_repository_highmem(struct mem_region *r) 302*1e755c09SAndre Heider { 303*1e755c09SAndre Heider int result; 304*1e755c09SAndre Heider 305*1e755c09SAndre Heider /* Assume a single highmem region. */ 306*1e755c09SAndre Heider 307*1e755c09SAndre Heider result = ps3_repository_read_highmem_info(0, &r->base, &r->size); 308*1e755c09SAndre Heider 309*1e755c09SAndre Heider if (result) 310*1e755c09SAndre Heider goto zero_region; 311*1e755c09SAndre Heider 312*1e755c09SAndre Heider if (!r->base || !r->size) { 313*1e755c09SAndre Heider result = -1; 314*1e755c09SAndre Heider goto zero_region; 315*1e755c09SAndre Heider } 316*1e755c09SAndre Heider 317*1e755c09SAndre Heider r->offset = r->base - map.rm.size; 318*1e755c09SAndre Heider 319*1e755c09SAndre Heider DBG("%s:%d: Found high region in repository: %llxh %llxh\n", 320*1e755c09SAndre Heider __func__, __LINE__, r->base, r->size); 321*1e755c09SAndre Heider 322*1e755c09SAndre Heider return 0; 323*1e755c09SAndre Heider 324*1e755c09SAndre Heider zero_region: 325*1e755c09SAndre Heider DBG("%s:%d: No high region in repository.\n", __func__, __LINE__); 326*1e755c09SAndre Heider 327*1e755c09SAndre Heider r->size = r->base = r->offset = 0; 328*1e755c09SAndre Heider return result; 329*1e755c09SAndre Heider } 330*1e755c09SAndre Heider 331f58a9d17SGeoff Levand /** 332f58a9d17SGeoff Levand * ps3_mm_add_memory - hot add memory 333f58a9d17SGeoff Levand */ 334f58a9d17SGeoff Levand 335f58a9d17SGeoff Levand static int __init ps3_mm_add_memory(void) 336f58a9d17SGeoff Levand { 337f58a9d17SGeoff Levand int result; 338f58a9d17SGeoff Levand unsigned long start_addr; 339f58a9d17SGeoff Levand unsigned long start_pfn; 340f58a9d17SGeoff Levand unsigned long nr_pages; 341f58a9d17SGeoff Levand 342e22ba7e3SArnd Bergmann if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) 343ef596c69SGeert Uytterhoeven return -ENODEV; 344e22ba7e3SArnd Bergmann 345f58a9d17SGeoff Levand BUG_ON(!mem_init_done); 346f58a9d17SGeoff Levand 347*1e755c09SAndre Heider if (!map.r1.size) { 348*1e755c09SAndre Heider DBG("%s:%d: No region 1, not adding memory\n", 349*1e755c09SAndre Heider __func__, __LINE__); 350*1e755c09SAndre Heider return 0; 351*1e755c09SAndre Heider } 352*1e755c09SAndre Heider 353a628df1eSGeoff Levand start_addr = map.rm.size; 354f58a9d17SGeoff Levand start_pfn = start_addr >> PAGE_SHIFT; 355f58a9d17SGeoff Levand nr_pages = (map.r1.size + PAGE_SIZE - 1) >> PAGE_SHIFT; 356f58a9d17SGeoff Levand 357f58a9d17SGeoff Levand DBG("%s:%d: start_addr %lxh, start_pfn %lxh, nr_pages %lxh\n", 358f58a9d17SGeoff Levand __func__, __LINE__, start_addr, start_pfn, nr_pages); 359f58a9d17SGeoff Levand 360f58a9d17SGeoff Levand result = add_memory(0, start_addr, map.r1.size); 361f58a9d17SGeoff Levand 362f58a9d17SGeoff Levand if (result) { 3639146cfc8SGeoff Levand pr_err("%s:%d: add_memory failed: (%d)\n", 364f58a9d17SGeoff Levand __func__, __LINE__, result); 365f58a9d17SGeoff Levand return result; 366f58a9d17SGeoff Levand } 367f58a9d17SGeoff Levand 36895f72d1eSYinghai Lu memblock_add(start_addr, map.r1.size); 369ecc240f9SGeoff Levand 370f58a9d17SGeoff Levand result = online_pages(start_pfn, nr_pages); 371f58a9d17SGeoff Levand 372f58a9d17SGeoff Levand if (result) 3739146cfc8SGeoff Levand pr_err("%s:%d: online_pages failed: (%d)\n", 374f58a9d17SGeoff Levand __func__, __LINE__, result); 375f58a9d17SGeoff Levand 376f58a9d17SGeoff Levand return result; 377f58a9d17SGeoff Levand } 378f58a9d17SGeoff Levand 3790047656eSGeoff Levand device_initcall(ps3_mm_add_memory); 380f58a9d17SGeoff Levand 381f58a9d17SGeoff Levand /*============================================================================*/ 382f58a9d17SGeoff Levand /* dma routines */ 383f58a9d17SGeoff Levand /*============================================================================*/ 384f58a9d17SGeoff Levand 385f58a9d17SGeoff Levand /** 3866bb5cf10SGeoff Levand * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address. 387f58a9d17SGeoff Levand * @r: pointer to dma region structure 388f58a9d17SGeoff Levand * @lpar_addr: HV lpar address 389f58a9d17SGeoff Levand */ 390f58a9d17SGeoff Levand 3916bb5cf10SGeoff Levand static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r, 392f58a9d17SGeoff Levand unsigned long lpar_addr) 393f58a9d17SGeoff Levand { 3946bb5cf10SGeoff Levand if (lpar_addr >= map.rm.size) 3956bb5cf10SGeoff Levand lpar_addr -= map.r1.offset; 3966bb5cf10SGeoff Levand BUG_ON(lpar_addr < r->offset); 3976bb5cf10SGeoff Levand BUG_ON(lpar_addr >= r->offset + r->len); 3986bb5cf10SGeoff Levand return r->bus_addr + lpar_addr - r->offset; 399f58a9d17SGeoff Levand } 400f58a9d17SGeoff Levand 401f58a9d17SGeoff Levand #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__) 4026bb5cf10SGeoff Levand static void __maybe_unused _dma_dump_region(const struct ps3_dma_region *r, 4036bb5cf10SGeoff Levand const char *func, int line) 404f58a9d17SGeoff Levand { 4055c949070SStephen Rothwell DBG("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id, 4066bb5cf10SGeoff Levand r->dev->dev_id); 407f58a9d17SGeoff Levand DBG("%s:%d: page_size %u\n", func, line, r->page_size); 408f58a9d17SGeoff Levand DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr); 409f58a9d17SGeoff Levand DBG("%s:%d: len %lxh\n", func, line, r->len); 4106bb5cf10SGeoff Levand DBG("%s:%d: offset %lxh\n", func, line, r->offset); 411f58a9d17SGeoff Levand } 412f58a9d17SGeoff Levand 413f58a9d17SGeoff Levand /** 414f58a9d17SGeoff Levand * dma_chunk - A chunk of dma pages mapped by the io controller. 415f58a9d17SGeoff Levand * @region - The dma region that owns this chunk. 416f58a9d17SGeoff Levand * @lpar_addr: Starting lpar address of the area to map. 417f58a9d17SGeoff Levand * @bus_addr: Starting ioc bus address of the area to map. 418f58a9d17SGeoff Levand * @len: Length in bytes of the area to map. 419f58a9d17SGeoff Levand * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the 420f58a9d17SGeoff Levand * list of all chuncks owned by the region. 421f58a9d17SGeoff Levand * 422f58a9d17SGeoff Levand * This implementation uses a very simple dma page manager 423f58a9d17SGeoff Levand * based on the dma_chunk structure. This scheme assumes 424f58a9d17SGeoff Levand * that all drivers use very well behaved dma ops. 425f58a9d17SGeoff Levand */ 426f58a9d17SGeoff Levand 427f58a9d17SGeoff Levand struct dma_chunk { 428f58a9d17SGeoff Levand struct ps3_dma_region *region; 429f58a9d17SGeoff Levand unsigned long lpar_addr; 430f58a9d17SGeoff Levand unsigned long bus_addr; 431f58a9d17SGeoff Levand unsigned long len; 432f58a9d17SGeoff Levand struct list_head link; 433f58a9d17SGeoff Levand unsigned int usage_count; 434f58a9d17SGeoff Levand }; 435f58a9d17SGeoff Levand 436f58a9d17SGeoff Levand #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__) 437f58a9d17SGeoff Levand static void _dma_dump_chunk (const struct dma_chunk* c, const char* func, 438f58a9d17SGeoff Levand int line) 439f58a9d17SGeoff Levand { 4405c949070SStephen Rothwell DBG("%s:%d: r.dev %llu:%llu\n", func, line, 4416bb5cf10SGeoff Levand c->region->dev->bus_id, c->region->dev->dev_id); 442f58a9d17SGeoff Levand DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr); 443f58a9d17SGeoff Levand DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size); 444f58a9d17SGeoff Levand DBG("%s:%d: r.len %lxh\n", func, line, c->region->len); 4456bb5cf10SGeoff Levand DBG("%s:%d: r.offset %lxh\n", func, line, c->region->offset); 446f58a9d17SGeoff Levand DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr); 447f58a9d17SGeoff Levand DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr); 448f58a9d17SGeoff Levand DBG("%s:%d: c.len %lxh\n", func, line, c->len); 449f58a9d17SGeoff Levand } 450f58a9d17SGeoff Levand 451f58a9d17SGeoff Levand static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r, 452f58a9d17SGeoff Levand unsigned long bus_addr, unsigned long len) 453f58a9d17SGeoff Levand { 454f58a9d17SGeoff Levand struct dma_chunk *c; 455f58a9d17SGeoff Levand unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 1 << r->page_size); 4566bb5cf10SGeoff Levand unsigned long aligned_len = _ALIGN_UP(len+bus_addr-aligned_bus, 4576bb5cf10SGeoff Levand 1 << r->page_size); 458f58a9d17SGeoff Levand 459f58a9d17SGeoff Levand list_for_each_entry(c, &r->chunk_list.head, link) { 460f58a9d17SGeoff Levand /* intersection */ 4616bb5cf10SGeoff Levand if (aligned_bus >= c->bus_addr && 4626bb5cf10SGeoff Levand aligned_bus + aligned_len <= c->bus_addr + c->len) 463f58a9d17SGeoff Levand return c; 4646bb5cf10SGeoff Levand 465f58a9d17SGeoff Levand /* below */ 4666bb5cf10SGeoff Levand if (aligned_bus + aligned_len <= c->bus_addr) 467f58a9d17SGeoff Levand continue; 4686bb5cf10SGeoff Levand 469f58a9d17SGeoff Levand /* above */ 4706bb5cf10SGeoff Levand if (aligned_bus >= c->bus_addr + c->len) 471f58a9d17SGeoff Levand continue; 472f58a9d17SGeoff Levand 473f58a9d17SGeoff Levand /* we don't handle the multi-chunk case for now */ 474f58a9d17SGeoff Levand dma_dump_chunk(c); 475f58a9d17SGeoff Levand BUG(); 476f58a9d17SGeoff Levand } 477f58a9d17SGeoff Levand return NULL; 478f58a9d17SGeoff Levand } 479f58a9d17SGeoff Levand 4806bb5cf10SGeoff Levand static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r, 4816bb5cf10SGeoff Levand unsigned long lpar_addr, unsigned long len) 4826bb5cf10SGeoff Levand { 4836bb5cf10SGeoff Levand struct dma_chunk *c; 4846bb5cf10SGeoff Levand unsigned long aligned_lpar = _ALIGN_DOWN(lpar_addr, 1 << r->page_size); 4856bb5cf10SGeoff Levand unsigned long aligned_len = _ALIGN_UP(len + lpar_addr - aligned_lpar, 4866bb5cf10SGeoff Levand 1 << r->page_size); 4876bb5cf10SGeoff Levand 4886bb5cf10SGeoff Levand list_for_each_entry(c, &r->chunk_list.head, link) { 4896bb5cf10SGeoff Levand /* intersection */ 4906bb5cf10SGeoff Levand if (c->lpar_addr <= aligned_lpar && 4916bb5cf10SGeoff Levand aligned_lpar < c->lpar_addr + c->len) { 4926bb5cf10SGeoff Levand if (aligned_lpar + aligned_len <= c->lpar_addr + c->len) 4936bb5cf10SGeoff Levand return c; 4946bb5cf10SGeoff Levand else { 4956bb5cf10SGeoff Levand dma_dump_chunk(c); 4966bb5cf10SGeoff Levand BUG(); 4976bb5cf10SGeoff Levand } 4986bb5cf10SGeoff Levand } 4996bb5cf10SGeoff Levand /* below */ 5006bb5cf10SGeoff Levand if (aligned_lpar + aligned_len <= c->lpar_addr) { 5016bb5cf10SGeoff Levand continue; 5026bb5cf10SGeoff Levand } 5036bb5cf10SGeoff Levand /* above */ 5046bb5cf10SGeoff Levand if (c->lpar_addr + c->len <= aligned_lpar) { 5056bb5cf10SGeoff Levand continue; 5066bb5cf10SGeoff Levand } 5076bb5cf10SGeoff Levand } 5086bb5cf10SGeoff Levand return NULL; 5096bb5cf10SGeoff Levand } 5106bb5cf10SGeoff Levand 5116bb5cf10SGeoff Levand static int dma_sb_free_chunk(struct dma_chunk *c) 512f58a9d17SGeoff Levand { 513f58a9d17SGeoff Levand int result = 0; 514f58a9d17SGeoff Levand 515f58a9d17SGeoff Levand if (c->bus_addr) { 5166bb5cf10SGeoff Levand result = lv1_unmap_device_dma_region(c->region->dev->bus_id, 5176bb5cf10SGeoff Levand c->region->dev->dev_id, c->bus_addr, c->len); 518f58a9d17SGeoff Levand BUG_ON(result); 519f58a9d17SGeoff Levand } 520f58a9d17SGeoff Levand 521f58a9d17SGeoff Levand kfree(c); 522f58a9d17SGeoff Levand return result; 523f58a9d17SGeoff Levand } 524f58a9d17SGeoff Levand 5256bb5cf10SGeoff Levand static int dma_ioc0_free_chunk(struct dma_chunk *c) 5266bb5cf10SGeoff Levand { 5276bb5cf10SGeoff Levand int result = 0; 5286bb5cf10SGeoff Levand int iopage; 5296bb5cf10SGeoff Levand unsigned long offset; 5306bb5cf10SGeoff Levand struct ps3_dma_region *r = c->region; 5316bb5cf10SGeoff Levand 5326bb5cf10SGeoff Levand DBG("%s:start\n", __func__); 5336bb5cf10SGeoff Levand for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) { 5346bb5cf10SGeoff Levand offset = (1 << r->page_size) * iopage; 5356bb5cf10SGeoff Levand /* put INVALID entry */ 5366bb5cf10SGeoff Levand result = lv1_put_iopte(0, 5376bb5cf10SGeoff Levand c->bus_addr + offset, 5386bb5cf10SGeoff Levand c->lpar_addr + offset, 5396bb5cf10SGeoff Levand r->ioid, 5406bb5cf10SGeoff Levand 0); 5416bb5cf10SGeoff Levand DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__, 5426bb5cf10SGeoff Levand c->bus_addr + offset, 5436bb5cf10SGeoff Levand c->lpar_addr + offset, 5446bb5cf10SGeoff Levand r->ioid); 5456bb5cf10SGeoff Levand 5466bb5cf10SGeoff Levand if (result) { 5476bb5cf10SGeoff Levand DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__, 5486bb5cf10SGeoff Levand __LINE__, ps3_result(result)); 5496bb5cf10SGeoff Levand } 5506bb5cf10SGeoff Levand } 5516bb5cf10SGeoff Levand kfree(c); 5526bb5cf10SGeoff Levand DBG("%s:end\n", __func__); 5536bb5cf10SGeoff Levand return result; 5546bb5cf10SGeoff Levand } 5556bb5cf10SGeoff Levand 556f58a9d17SGeoff Levand /** 5576bb5cf10SGeoff Levand * dma_sb_map_pages - Maps dma pages into the io controller bus address space. 558f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 559f58a9d17SGeoff Levand * @phys_addr: Starting physical address of the area to map. 560f58a9d17SGeoff Levand * @len: Length in bytes of the area to map. 561f58a9d17SGeoff Levand * c_out: A pointer to receive an allocated struct dma_chunk for this area. 562f58a9d17SGeoff Levand * 563f58a9d17SGeoff Levand * This is the lowest level dma mapping routine, and is the one that will 564f58a9d17SGeoff Levand * make the HV call to add the pages into the io controller address space. 565f58a9d17SGeoff Levand */ 566f58a9d17SGeoff Levand 5676bb5cf10SGeoff Levand static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr, 5686bb5cf10SGeoff Levand unsigned long len, struct dma_chunk **c_out, u64 iopte_flag) 569f58a9d17SGeoff Levand { 570f58a9d17SGeoff Levand int result; 571f58a9d17SGeoff Levand struct dma_chunk *c; 572f58a9d17SGeoff Levand 573f58a9d17SGeoff Levand c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC); 574f58a9d17SGeoff Levand 575f58a9d17SGeoff Levand if (!c) { 576f58a9d17SGeoff Levand result = -ENOMEM; 577f58a9d17SGeoff Levand goto fail_alloc; 578f58a9d17SGeoff Levand } 579f58a9d17SGeoff Levand 580f58a9d17SGeoff Levand c->region = r; 581f58a9d17SGeoff Levand c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr); 5826bb5cf10SGeoff Levand c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr); 583f58a9d17SGeoff Levand c->len = len; 584f58a9d17SGeoff Levand 5856bb5cf10SGeoff Levand BUG_ON(iopte_flag != 0xf800000000000000UL); 5866bb5cf10SGeoff Levand result = lv1_map_device_dma_region(c->region->dev->bus_id, 5876bb5cf10SGeoff Levand c->region->dev->dev_id, c->lpar_addr, 5886bb5cf10SGeoff Levand c->bus_addr, c->len, iopte_flag); 589f58a9d17SGeoff Levand if (result) { 590f58a9d17SGeoff Levand DBG("%s:%d: lv1_map_device_dma_region failed: %s\n", 591f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 592f58a9d17SGeoff Levand goto fail_map; 593f58a9d17SGeoff Levand } 594f58a9d17SGeoff Levand 595f58a9d17SGeoff Levand list_add(&c->link, &r->chunk_list.head); 596f58a9d17SGeoff Levand 597f58a9d17SGeoff Levand *c_out = c; 598f58a9d17SGeoff Levand return 0; 599f58a9d17SGeoff Levand 600f58a9d17SGeoff Levand fail_map: 601f58a9d17SGeoff Levand kfree(c); 602f58a9d17SGeoff Levand fail_alloc: 603f58a9d17SGeoff Levand *c_out = NULL; 604f58a9d17SGeoff Levand DBG(" <- %s:%d\n", __func__, __LINE__); 605f58a9d17SGeoff Levand return result; 606f58a9d17SGeoff Levand } 607f58a9d17SGeoff Levand 6086bb5cf10SGeoff Levand static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr, 6096bb5cf10SGeoff Levand unsigned long len, struct dma_chunk **c_out, 6106bb5cf10SGeoff Levand u64 iopte_flag) 6116bb5cf10SGeoff Levand { 6126bb5cf10SGeoff Levand int result; 6136bb5cf10SGeoff Levand struct dma_chunk *c, *last; 6146bb5cf10SGeoff Levand int iopage, pages; 6156bb5cf10SGeoff Levand unsigned long offset; 6166bb5cf10SGeoff Levand 6176bb5cf10SGeoff Levand DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__, 6186bb5cf10SGeoff Levand phys_addr, ps3_mm_phys_to_lpar(phys_addr), len); 6196bb5cf10SGeoff Levand c = kzalloc(sizeof(struct dma_chunk), GFP_ATOMIC); 6206bb5cf10SGeoff Levand 6216bb5cf10SGeoff Levand if (!c) { 6226bb5cf10SGeoff Levand result = -ENOMEM; 6236bb5cf10SGeoff Levand goto fail_alloc; 6246bb5cf10SGeoff Levand } 6256bb5cf10SGeoff Levand 6266bb5cf10SGeoff Levand c->region = r; 6276bb5cf10SGeoff Levand c->len = len; 6286bb5cf10SGeoff Levand c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr); 6296bb5cf10SGeoff Levand /* allocate IO address */ 6306bb5cf10SGeoff Levand if (list_empty(&r->chunk_list.head)) { 6316bb5cf10SGeoff Levand /* first one */ 6326bb5cf10SGeoff Levand c->bus_addr = r->bus_addr; 6336bb5cf10SGeoff Levand } else { 6346bb5cf10SGeoff Levand /* derive from last bus addr*/ 6356bb5cf10SGeoff Levand last = list_entry(r->chunk_list.head.next, 6366bb5cf10SGeoff Levand struct dma_chunk, link); 6376bb5cf10SGeoff Levand c->bus_addr = last->bus_addr + last->len; 6386bb5cf10SGeoff Levand DBG("%s: last bus=%#lx, len=%#lx\n", __func__, 6396bb5cf10SGeoff Levand last->bus_addr, last->len); 6406bb5cf10SGeoff Levand } 6416bb5cf10SGeoff Levand 6426bb5cf10SGeoff Levand /* FIXME: check whether length exceeds region size */ 6436bb5cf10SGeoff Levand 6446bb5cf10SGeoff Levand /* build ioptes for the area */ 6456bb5cf10SGeoff Levand pages = len >> r->page_size; 6465c949070SStephen Rothwell DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__, 6476bb5cf10SGeoff Levand r->page_size, r->len, pages, iopte_flag); 6486bb5cf10SGeoff Levand for (iopage = 0; iopage < pages; iopage++) { 6496bb5cf10SGeoff Levand offset = (1 << r->page_size) * iopage; 6506bb5cf10SGeoff Levand result = lv1_put_iopte(0, 6516bb5cf10SGeoff Levand c->bus_addr + offset, 6526bb5cf10SGeoff Levand c->lpar_addr + offset, 6536bb5cf10SGeoff Levand r->ioid, 6546bb5cf10SGeoff Levand iopte_flag); 6556bb5cf10SGeoff Levand if (result) { 6567e28060aSGeert Uytterhoeven pr_warning("%s:%d: lv1_put_iopte failed: %s\n", 6577e28060aSGeert Uytterhoeven __func__, __LINE__, ps3_result(result)); 6586bb5cf10SGeoff Levand goto fail_map; 6596bb5cf10SGeoff Levand } 6606bb5cf10SGeoff Levand DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__, 6616bb5cf10SGeoff Levand iopage, c->bus_addr + offset, c->lpar_addr + offset, 6626bb5cf10SGeoff Levand r->ioid); 6636bb5cf10SGeoff Levand } 6646bb5cf10SGeoff Levand 6656bb5cf10SGeoff Levand /* be sure that last allocated one is inserted at head */ 6666bb5cf10SGeoff Levand list_add(&c->link, &r->chunk_list.head); 6676bb5cf10SGeoff Levand 6686bb5cf10SGeoff Levand *c_out = c; 6696bb5cf10SGeoff Levand DBG("%s: end\n", __func__); 6706bb5cf10SGeoff Levand return 0; 6716bb5cf10SGeoff Levand 6726bb5cf10SGeoff Levand fail_map: 6736bb5cf10SGeoff Levand for (iopage--; 0 <= iopage; iopage--) { 6746bb5cf10SGeoff Levand lv1_put_iopte(0, 6756bb5cf10SGeoff Levand c->bus_addr + offset, 6766bb5cf10SGeoff Levand c->lpar_addr + offset, 6776bb5cf10SGeoff Levand r->ioid, 6786bb5cf10SGeoff Levand 0); 6796bb5cf10SGeoff Levand } 6806bb5cf10SGeoff Levand kfree(c); 6816bb5cf10SGeoff Levand fail_alloc: 6826bb5cf10SGeoff Levand *c_out = NULL; 6836bb5cf10SGeoff Levand return result; 6846bb5cf10SGeoff Levand } 6856bb5cf10SGeoff Levand 686f58a9d17SGeoff Levand /** 6876bb5cf10SGeoff Levand * dma_sb_region_create - Create a device dma region. 688f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 689f58a9d17SGeoff Levand * 690f58a9d17SGeoff Levand * This is the lowest level dma region create routine, and is the one that 691f58a9d17SGeoff Levand * will make the HV call to create the region. 692f58a9d17SGeoff Levand */ 693f58a9d17SGeoff Levand 6946bb5cf10SGeoff Levand static int dma_sb_region_create(struct ps3_dma_region *r) 695f58a9d17SGeoff Levand { 696f58a9d17SGeoff Levand int result; 697b17b3df1SStephen Rothwell u64 bus_addr; 698f58a9d17SGeoff Levand 69962d80749SGeoff Levand DBG(" -> %s:%d:\n", __func__, __LINE__); 7006bb5cf10SGeoff Levand 7016bb5cf10SGeoff Levand BUG_ON(!r); 7026bb5cf10SGeoff Levand 7036bb5cf10SGeoff Levand if (!r->dev->bus_id) { 7045c949070SStephen Rothwell pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__, 7056bb5cf10SGeoff Levand r->dev->bus_id, r->dev->dev_id); 7066bb5cf10SGeoff Levand return 0; 7076bb5cf10SGeoff Levand } 7086bb5cf10SGeoff Levand 7096bb5cf10SGeoff Levand DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__, 7106bb5cf10SGeoff Levand __LINE__, r->len, r->page_size, r->offset); 7116bb5cf10SGeoff Levand 7126bb5cf10SGeoff Levand BUG_ON(!r->len); 7136bb5cf10SGeoff Levand BUG_ON(!r->page_size); 7146bb5cf10SGeoff Levand BUG_ON(!r->region_ops); 7156bb5cf10SGeoff Levand 716f58a9d17SGeoff Levand INIT_LIST_HEAD(&r->chunk_list.head); 717f58a9d17SGeoff Levand spin_lock_init(&r->chunk_list.lock); 718f58a9d17SGeoff Levand 7196bb5cf10SGeoff Levand result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id, 7206bb5cf10SGeoff Levand roundup_pow_of_two(r->len), r->page_size, r->region_type, 721b17b3df1SStephen Rothwell &bus_addr); 722b17b3df1SStephen Rothwell r->bus_addr = bus_addr; 723f58a9d17SGeoff Levand 724f58a9d17SGeoff Levand if (result) { 725f58a9d17SGeoff Levand DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n", 726f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 727f58a9d17SGeoff Levand r->len = r->bus_addr = 0; 728f58a9d17SGeoff Levand } 729f58a9d17SGeoff Levand 730f58a9d17SGeoff Levand return result; 731f58a9d17SGeoff Levand } 732f58a9d17SGeoff Levand 7336bb5cf10SGeoff Levand static int dma_ioc0_region_create(struct ps3_dma_region *r) 7346bb5cf10SGeoff Levand { 7356bb5cf10SGeoff Levand int result; 736b17b3df1SStephen Rothwell u64 bus_addr; 7376bb5cf10SGeoff Levand 7386bb5cf10SGeoff Levand INIT_LIST_HEAD(&r->chunk_list.head); 7396bb5cf10SGeoff Levand spin_lock_init(&r->chunk_list.lock); 7406bb5cf10SGeoff Levand 7416bb5cf10SGeoff Levand result = lv1_allocate_io_segment(0, 7426bb5cf10SGeoff Levand r->len, 7436bb5cf10SGeoff Levand r->page_size, 744b17b3df1SStephen Rothwell &bus_addr); 745b17b3df1SStephen Rothwell r->bus_addr = bus_addr; 7466bb5cf10SGeoff Levand if (result) { 7476bb5cf10SGeoff Levand DBG("%s:%d: lv1_allocate_io_segment failed: %s\n", 7486bb5cf10SGeoff Levand __func__, __LINE__, ps3_result(result)); 7496bb5cf10SGeoff Levand r->len = r->bus_addr = 0; 7506bb5cf10SGeoff Levand } 7516bb5cf10SGeoff Levand DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__, 7526bb5cf10SGeoff Levand r->len, r->page_size, r->bus_addr); 7536bb5cf10SGeoff Levand return result; 7546bb5cf10SGeoff Levand } 7556bb5cf10SGeoff Levand 756f58a9d17SGeoff Levand /** 757f58a9d17SGeoff Levand * dma_region_free - Free a device dma region. 758f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 759f58a9d17SGeoff Levand * 760f58a9d17SGeoff Levand * This is the lowest level dma region free routine, and is the one that 761f58a9d17SGeoff Levand * will make the HV call to free the region. 762f58a9d17SGeoff Levand */ 763f58a9d17SGeoff Levand 7646bb5cf10SGeoff Levand static int dma_sb_region_free(struct ps3_dma_region *r) 765f58a9d17SGeoff Levand { 766f58a9d17SGeoff Levand int result; 767f58a9d17SGeoff Levand struct dma_chunk *c; 768f58a9d17SGeoff Levand struct dma_chunk *tmp; 769f58a9d17SGeoff Levand 7706bb5cf10SGeoff Levand BUG_ON(!r); 7716bb5cf10SGeoff Levand 7726bb5cf10SGeoff Levand if (!r->dev->bus_id) { 7735c949070SStephen Rothwell pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__, 7746bb5cf10SGeoff Levand r->dev->bus_id, r->dev->dev_id); 7756bb5cf10SGeoff Levand return 0; 776f58a9d17SGeoff Levand } 777f58a9d17SGeoff Levand 7786bb5cf10SGeoff Levand list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) { 7796bb5cf10SGeoff Levand list_del(&c->link); 7806bb5cf10SGeoff Levand dma_sb_free_chunk(c); 7816bb5cf10SGeoff Levand } 7826bb5cf10SGeoff Levand 7836bb5cf10SGeoff Levand result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id, 784f58a9d17SGeoff Levand r->bus_addr); 785f58a9d17SGeoff Levand 786f58a9d17SGeoff Levand if (result) 787f58a9d17SGeoff Levand DBG("%s:%d: lv1_free_device_dma_region failed: %s\n", 788f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 789f58a9d17SGeoff Levand 7906bb5cf10SGeoff Levand r->bus_addr = 0; 7916bb5cf10SGeoff Levand 7926bb5cf10SGeoff Levand return result; 7936bb5cf10SGeoff Levand } 7946bb5cf10SGeoff Levand 7956bb5cf10SGeoff Levand static int dma_ioc0_region_free(struct ps3_dma_region *r) 7966bb5cf10SGeoff Levand { 7976bb5cf10SGeoff Levand int result; 7986bb5cf10SGeoff Levand struct dma_chunk *c, *n; 7996bb5cf10SGeoff Levand 8006bb5cf10SGeoff Levand DBG("%s: start\n", __func__); 8016bb5cf10SGeoff Levand list_for_each_entry_safe(c, n, &r->chunk_list.head, link) { 8026bb5cf10SGeoff Levand list_del(&c->link); 8036bb5cf10SGeoff Levand dma_ioc0_free_chunk(c); 8046bb5cf10SGeoff Levand } 8056bb5cf10SGeoff Levand 8066bb5cf10SGeoff Levand result = lv1_release_io_segment(0, r->bus_addr); 8076bb5cf10SGeoff Levand 8086bb5cf10SGeoff Levand if (result) 8096bb5cf10SGeoff Levand DBG("%s:%d: lv1_free_device_dma_region failed: %s\n", 8106bb5cf10SGeoff Levand __func__, __LINE__, ps3_result(result)); 8116bb5cf10SGeoff Levand 8126bb5cf10SGeoff Levand r->bus_addr = 0; 8136bb5cf10SGeoff Levand DBG("%s: end\n", __func__); 814f58a9d17SGeoff Levand 815f58a9d17SGeoff Levand return result; 816f58a9d17SGeoff Levand } 817f58a9d17SGeoff Levand 818f58a9d17SGeoff Levand /** 8196bb5cf10SGeoff Levand * dma_sb_map_area - Map an area of memory into a device dma region. 820f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 821f58a9d17SGeoff Levand * @virt_addr: Starting virtual address of the area to map. 822f58a9d17SGeoff Levand * @len: Length in bytes of the area to map. 823f58a9d17SGeoff Levand * @bus_addr: A pointer to return the starting ioc bus address of the area to 824f58a9d17SGeoff Levand * map. 825f58a9d17SGeoff Levand * 826f58a9d17SGeoff Levand * This is the common dma mapping routine. 827f58a9d17SGeoff Levand */ 828f58a9d17SGeoff Levand 8296bb5cf10SGeoff Levand static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr, 830494fd07aSStephen Rothwell unsigned long len, dma_addr_t *bus_addr, 8316bb5cf10SGeoff Levand u64 iopte_flag) 832f58a9d17SGeoff Levand { 833f58a9d17SGeoff Levand int result; 834f58a9d17SGeoff Levand unsigned long flags; 835f58a9d17SGeoff Levand struct dma_chunk *c; 836f58a9d17SGeoff Levand unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr) 837f58a9d17SGeoff Levand : virt_addr; 8386bb5cf10SGeoff Levand unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size); 8396bb5cf10SGeoff Levand unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys, 8406bb5cf10SGeoff Levand 1 << r->page_size); 8416bb5cf10SGeoff Levand *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr)); 842f58a9d17SGeoff Levand 843f58a9d17SGeoff Levand if (!USE_DYNAMIC_DMA) { 844f58a9d17SGeoff Levand unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr); 845f58a9d17SGeoff Levand DBG(" -> %s:%d\n", __func__, __LINE__); 846f58a9d17SGeoff Levand DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__, 847f58a9d17SGeoff Levand virt_addr); 848f58a9d17SGeoff Levand DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__, 849f58a9d17SGeoff Levand phys_addr); 850f58a9d17SGeoff Levand DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__, 851f58a9d17SGeoff Levand lpar_addr); 852f58a9d17SGeoff Levand DBG("%s:%d len %lxh\n", __func__, __LINE__, len); 853494fd07aSStephen Rothwell DBG("%s:%d bus_addr %llxh (%lxh)\n", __func__, __LINE__, 854f58a9d17SGeoff Levand *bus_addr, len); 855f58a9d17SGeoff Levand } 856f58a9d17SGeoff Levand 857f58a9d17SGeoff Levand spin_lock_irqsave(&r->chunk_list.lock, flags); 858f58a9d17SGeoff Levand c = dma_find_chunk(r, *bus_addr, len); 859f58a9d17SGeoff Levand 860f58a9d17SGeoff Levand if (c) { 8616bb5cf10SGeoff Levand DBG("%s:%d: reusing mapped chunk", __func__, __LINE__); 8626bb5cf10SGeoff Levand dma_dump_chunk(c); 863f58a9d17SGeoff Levand c->usage_count++; 864f58a9d17SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 865f58a9d17SGeoff Levand return 0; 866f58a9d17SGeoff Levand } 867f58a9d17SGeoff Levand 8686bb5cf10SGeoff Levand result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag); 869f58a9d17SGeoff Levand 870f58a9d17SGeoff Levand if (result) { 871f58a9d17SGeoff Levand *bus_addr = 0; 8726bb5cf10SGeoff Levand DBG("%s:%d: dma_sb_map_pages failed (%d)\n", 873f58a9d17SGeoff Levand __func__, __LINE__, result); 874f58a9d17SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 875f58a9d17SGeoff Levand return result; 876f58a9d17SGeoff Levand } 877f58a9d17SGeoff Levand 878f58a9d17SGeoff Levand c->usage_count = 1; 879f58a9d17SGeoff Levand 880f58a9d17SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 881f58a9d17SGeoff Levand return result; 882f58a9d17SGeoff Levand } 883f58a9d17SGeoff Levand 8846bb5cf10SGeoff Levand static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr, 885494fd07aSStephen Rothwell unsigned long len, dma_addr_t *bus_addr, 8866bb5cf10SGeoff Levand u64 iopte_flag) 8876bb5cf10SGeoff Levand { 8886bb5cf10SGeoff Levand int result; 8896bb5cf10SGeoff Levand unsigned long flags; 8906bb5cf10SGeoff Levand struct dma_chunk *c; 8916bb5cf10SGeoff Levand unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr) 8926bb5cf10SGeoff Levand : virt_addr; 8936bb5cf10SGeoff Levand unsigned long aligned_phys = _ALIGN_DOWN(phys_addr, 1 << r->page_size); 8946bb5cf10SGeoff Levand unsigned long aligned_len = _ALIGN_UP(len + phys_addr - aligned_phys, 8956bb5cf10SGeoff Levand 1 << r->page_size); 8966bb5cf10SGeoff Levand 8976bb5cf10SGeoff Levand DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__, 8986bb5cf10SGeoff Levand virt_addr, len); 8996bb5cf10SGeoff Levand DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__, 9006bb5cf10SGeoff Levand phys_addr, aligned_phys, aligned_len); 9016bb5cf10SGeoff Levand 9026bb5cf10SGeoff Levand spin_lock_irqsave(&r->chunk_list.lock, flags); 9036bb5cf10SGeoff Levand c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len); 9046bb5cf10SGeoff Levand 9056bb5cf10SGeoff Levand if (c) { 9066bb5cf10SGeoff Levand /* FIXME */ 9076bb5cf10SGeoff Levand BUG(); 9086bb5cf10SGeoff Levand *bus_addr = c->bus_addr + phys_addr - aligned_phys; 9096bb5cf10SGeoff Levand c->usage_count++; 9106bb5cf10SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 9116bb5cf10SGeoff Levand return 0; 9126bb5cf10SGeoff Levand } 9136bb5cf10SGeoff Levand 9146bb5cf10SGeoff Levand result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c, 9156bb5cf10SGeoff Levand iopte_flag); 9166bb5cf10SGeoff Levand 9176bb5cf10SGeoff Levand if (result) { 9186bb5cf10SGeoff Levand *bus_addr = 0; 9196bb5cf10SGeoff Levand DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n", 9206bb5cf10SGeoff Levand __func__, __LINE__, result); 9216bb5cf10SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 9226bb5cf10SGeoff Levand return result; 9236bb5cf10SGeoff Levand } 9246bb5cf10SGeoff Levand *bus_addr = c->bus_addr + phys_addr - aligned_phys; 925494fd07aSStephen Rothwell DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__, 9266bb5cf10SGeoff Levand virt_addr, phys_addr, aligned_phys, *bus_addr); 9276bb5cf10SGeoff Levand c->usage_count = 1; 9286bb5cf10SGeoff Levand 9296bb5cf10SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 9306bb5cf10SGeoff Levand return result; 9316bb5cf10SGeoff Levand } 9326bb5cf10SGeoff Levand 933f58a9d17SGeoff Levand /** 9346bb5cf10SGeoff Levand * dma_sb_unmap_area - Unmap an area of memory from a device dma region. 935f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 936f58a9d17SGeoff Levand * @bus_addr: The starting ioc bus address of the area to unmap. 937f58a9d17SGeoff Levand * @len: Length in bytes of the area to unmap. 938f58a9d17SGeoff Levand * 939f58a9d17SGeoff Levand * This is the common dma unmap routine. 940f58a9d17SGeoff Levand */ 941f58a9d17SGeoff Levand 942494fd07aSStephen Rothwell static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr, 943f58a9d17SGeoff Levand unsigned long len) 944f58a9d17SGeoff Levand { 945f58a9d17SGeoff Levand unsigned long flags; 946f58a9d17SGeoff Levand struct dma_chunk *c; 947f58a9d17SGeoff Levand 948f58a9d17SGeoff Levand spin_lock_irqsave(&r->chunk_list.lock, flags); 949f58a9d17SGeoff Levand c = dma_find_chunk(r, bus_addr, len); 950f58a9d17SGeoff Levand 951f58a9d17SGeoff Levand if (!c) { 952f58a9d17SGeoff Levand unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 953f58a9d17SGeoff Levand 1 << r->page_size); 9546bb5cf10SGeoff Levand unsigned long aligned_len = _ALIGN_UP(len + bus_addr 9556bb5cf10SGeoff Levand - aligned_bus, 1 << r->page_size); 956494fd07aSStephen Rothwell DBG("%s:%d: not found: bus_addr %llxh\n", 957f58a9d17SGeoff Levand __func__, __LINE__, bus_addr); 958f58a9d17SGeoff Levand DBG("%s:%d: not found: len %lxh\n", 959f58a9d17SGeoff Levand __func__, __LINE__, len); 960f58a9d17SGeoff Levand DBG("%s:%d: not found: aligned_bus %lxh\n", 961f58a9d17SGeoff Levand __func__, __LINE__, aligned_bus); 962f58a9d17SGeoff Levand DBG("%s:%d: not found: aligned_len %lxh\n", 963f58a9d17SGeoff Levand __func__, __LINE__, aligned_len); 964f58a9d17SGeoff Levand BUG(); 965f58a9d17SGeoff Levand } 966f58a9d17SGeoff Levand 967f58a9d17SGeoff Levand c->usage_count--; 968f58a9d17SGeoff Levand 969f58a9d17SGeoff Levand if (!c->usage_count) { 970f58a9d17SGeoff Levand list_del(&c->link); 9716bb5cf10SGeoff Levand dma_sb_free_chunk(c); 972f58a9d17SGeoff Levand } 973f58a9d17SGeoff Levand 974f58a9d17SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 975f58a9d17SGeoff Levand return 0; 976f58a9d17SGeoff Levand } 977f58a9d17SGeoff Levand 97832f44a12SGeert Uytterhoeven static int dma_ioc0_unmap_area(struct ps3_dma_region *r, 979494fd07aSStephen Rothwell dma_addr_t bus_addr, unsigned long len) 9806bb5cf10SGeoff Levand { 9816bb5cf10SGeoff Levand unsigned long flags; 9826bb5cf10SGeoff Levand struct dma_chunk *c; 9836bb5cf10SGeoff Levand 984494fd07aSStephen Rothwell DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len); 9856bb5cf10SGeoff Levand spin_lock_irqsave(&r->chunk_list.lock, flags); 9866bb5cf10SGeoff Levand c = dma_find_chunk(r, bus_addr, len); 9876bb5cf10SGeoff Levand 9886bb5cf10SGeoff Levand if (!c) { 9896bb5cf10SGeoff Levand unsigned long aligned_bus = _ALIGN_DOWN(bus_addr, 9906bb5cf10SGeoff Levand 1 << r->page_size); 9916bb5cf10SGeoff Levand unsigned long aligned_len = _ALIGN_UP(len + bus_addr 9926bb5cf10SGeoff Levand - aligned_bus, 9936bb5cf10SGeoff Levand 1 << r->page_size); 994494fd07aSStephen Rothwell DBG("%s:%d: not found: bus_addr %llxh\n", 9956bb5cf10SGeoff Levand __func__, __LINE__, bus_addr); 9966bb5cf10SGeoff Levand DBG("%s:%d: not found: len %lxh\n", 9976bb5cf10SGeoff Levand __func__, __LINE__, len); 9986bb5cf10SGeoff Levand DBG("%s:%d: not found: aligned_bus %lxh\n", 9996bb5cf10SGeoff Levand __func__, __LINE__, aligned_bus); 10006bb5cf10SGeoff Levand DBG("%s:%d: not found: aligned_len %lxh\n", 10016bb5cf10SGeoff Levand __func__, __LINE__, aligned_len); 10026bb5cf10SGeoff Levand BUG(); 10036bb5cf10SGeoff Levand } 10046bb5cf10SGeoff Levand 10056bb5cf10SGeoff Levand c->usage_count--; 10066bb5cf10SGeoff Levand 10076bb5cf10SGeoff Levand if (!c->usage_count) { 10086bb5cf10SGeoff Levand list_del(&c->link); 10096bb5cf10SGeoff Levand dma_ioc0_free_chunk(c); 10106bb5cf10SGeoff Levand } 10116bb5cf10SGeoff Levand 10126bb5cf10SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 10136bb5cf10SGeoff Levand DBG("%s: end\n", __func__); 10146bb5cf10SGeoff Levand return 0; 10156bb5cf10SGeoff Levand } 10166bb5cf10SGeoff Levand 1017f58a9d17SGeoff Levand /** 10186bb5cf10SGeoff Levand * dma_sb_region_create_linear - Setup a linear dma mapping for a device. 1019f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 1020f58a9d17SGeoff Levand * 1021f58a9d17SGeoff Levand * This routine creates an HV dma region for the device and maps all available 1022f58a9d17SGeoff Levand * ram into the io controller bus address space. 1023f58a9d17SGeoff Levand */ 1024f58a9d17SGeoff Levand 10256bb5cf10SGeoff Levand static int dma_sb_region_create_linear(struct ps3_dma_region *r) 1026f58a9d17SGeoff Levand { 1027f58a9d17SGeoff Levand int result; 1028494fd07aSStephen Rothwell unsigned long virt_addr, len; 1029494fd07aSStephen Rothwell dma_addr_t tmp; 1030f58a9d17SGeoff Levand 10316bb5cf10SGeoff Levand if (r->len > 16*1024*1024) { /* FIXME: need proper fix */ 1032f58a9d17SGeoff Levand /* force 16M dma pages for linear mapping */ 1033f58a9d17SGeoff Levand if (r->page_size != PS3_DMA_16M) { 1034f58a9d17SGeoff Levand pr_info("%s:%d: forcing 16M pages for linear map\n", 1035f58a9d17SGeoff Levand __func__, __LINE__); 1036f58a9d17SGeoff Levand r->page_size = PS3_DMA_16M; 10376bb5cf10SGeoff Levand r->len = _ALIGN_UP(r->len, 1 << r->page_size); 10386bb5cf10SGeoff Levand } 1039f58a9d17SGeoff Levand } 1040f58a9d17SGeoff Levand 10416bb5cf10SGeoff Levand result = dma_sb_region_create(r); 1042f58a9d17SGeoff Levand BUG_ON(result); 1043f58a9d17SGeoff Levand 10446bb5cf10SGeoff Levand if (r->offset < map.rm.size) { 10456bb5cf10SGeoff Levand /* Map (part of) 1st RAM chunk */ 10466bb5cf10SGeoff Levand virt_addr = map.rm.base + r->offset; 10476bb5cf10SGeoff Levand len = map.rm.size - r->offset; 10486bb5cf10SGeoff Levand if (len > r->len) 10496bb5cf10SGeoff Levand len = r->len; 10506bb5cf10SGeoff Levand result = dma_sb_map_area(r, virt_addr, len, &tmp, 10515c6fc8dbSGeert Uytterhoeven CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW | 10525c6fc8dbSGeert Uytterhoeven CBE_IOPTE_M); 1053f58a9d17SGeoff Levand BUG_ON(result); 10546bb5cf10SGeoff Levand } 1055f58a9d17SGeoff Levand 10566bb5cf10SGeoff Levand if (r->offset + r->len > map.rm.size) { 10576bb5cf10SGeoff Levand /* Map (part of) 2nd RAM chunk */ 1058a628df1eSGeoff Levand virt_addr = map.rm.size; 10596bb5cf10SGeoff Levand len = r->len; 10606bb5cf10SGeoff Levand if (r->offset >= map.rm.size) 10616bb5cf10SGeoff Levand virt_addr += r->offset - map.rm.size; 1062f58a9d17SGeoff Levand else 10636bb5cf10SGeoff Levand len -= map.rm.size - r->offset; 10646bb5cf10SGeoff Levand result = dma_sb_map_area(r, virt_addr, len, &tmp, 10655c6fc8dbSGeert Uytterhoeven CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW | 10665c6fc8dbSGeert Uytterhoeven CBE_IOPTE_M); 1067f58a9d17SGeoff Levand BUG_ON(result); 10686bb5cf10SGeoff Levand } 1069f58a9d17SGeoff Levand 1070f58a9d17SGeoff Levand return result; 1071f58a9d17SGeoff Levand } 1072f58a9d17SGeoff Levand 1073f58a9d17SGeoff Levand /** 10746bb5cf10SGeoff Levand * dma_sb_region_free_linear - Free a linear dma mapping for a device. 1075f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 1076f58a9d17SGeoff Levand * 1077f58a9d17SGeoff Levand * This routine will unmap all mapped areas and free the HV dma region. 1078f58a9d17SGeoff Levand */ 1079f58a9d17SGeoff Levand 10806bb5cf10SGeoff Levand static int dma_sb_region_free_linear(struct ps3_dma_region *r) 1081f58a9d17SGeoff Levand { 1082f58a9d17SGeoff Levand int result; 1083494fd07aSStephen Rothwell dma_addr_t bus_addr; 1084494fd07aSStephen Rothwell unsigned long len, lpar_addr; 1085f58a9d17SGeoff Levand 10866bb5cf10SGeoff Levand if (r->offset < map.rm.size) { 10876bb5cf10SGeoff Levand /* Unmap (part of) 1st RAM chunk */ 10886bb5cf10SGeoff Levand lpar_addr = map.rm.base + r->offset; 10896bb5cf10SGeoff Levand len = map.rm.size - r->offset; 10906bb5cf10SGeoff Levand if (len > r->len) 10916bb5cf10SGeoff Levand len = r->len; 10926bb5cf10SGeoff Levand bus_addr = dma_sb_lpar_to_bus(r, lpar_addr); 10936bb5cf10SGeoff Levand result = dma_sb_unmap_area(r, bus_addr, len); 1094f58a9d17SGeoff Levand BUG_ON(result); 10956bb5cf10SGeoff Levand } 1096f58a9d17SGeoff Levand 10976bb5cf10SGeoff Levand if (r->offset + r->len > map.rm.size) { 10986bb5cf10SGeoff Levand /* Unmap (part of) 2nd RAM chunk */ 10996bb5cf10SGeoff Levand lpar_addr = map.r1.base; 11006bb5cf10SGeoff Levand len = r->len; 11016bb5cf10SGeoff Levand if (r->offset >= map.rm.size) 11026bb5cf10SGeoff Levand lpar_addr += r->offset - map.rm.size; 11036bb5cf10SGeoff Levand else 11046bb5cf10SGeoff Levand len -= map.rm.size - r->offset; 11056bb5cf10SGeoff Levand bus_addr = dma_sb_lpar_to_bus(r, lpar_addr); 11066bb5cf10SGeoff Levand result = dma_sb_unmap_area(r, bus_addr, len); 1107f58a9d17SGeoff Levand BUG_ON(result); 11086bb5cf10SGeoff Levand } 1109f58a9d17SGeoff Levand 11106bb5cf10SGeoff Levand result = dma_sb_region_free(r); 1111f58a9d17SGeoff Levand BUG_ON(result); 1112f58a9d17SGeoff Levand 1113f58a9d17SGeoff Levand return result; 1114f58a9d17SGeoff Levand } 1115f58a9d17SGeoff Levand 1116f58a9d17SGeoff Levand /** 11176bb5cf10SGeoff Levand * dma_sb_map_area_linear - Map an area of memory into a device dma region. 1118f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 1119f58a9d17SGeoff Levand * @virt_addr: Starting virtual address of the area to map. 1120f58a9d17SGeoff Levand * @len: Length in bytes of the area to map. 1121f58a9d17SGeoff Levand * @bus_addr: A pointer to return the starting ioc bus address of the area to 1122f58a9d17SGeoff Levand * map. 1123f58a9d17SGeoff Levand * 11246bb5cf10SGeoff Levand * This routine just returns the corresponding bus address. Actual mapping 1125f58a9d17SGeoff Levand * occurs in dma_region_create_linear(). 1126f58a9d17SGeoff Levand */ 1127f58a9d17SGeoff Levand 11286bb5cf10SGeoff Levand static int dma_sb_map_area_linear(struct ps3_dma_region *r, 1129494fd07aSStephen Rothwell unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr, 11306bb5cf10SGeoff Levand u64 iopte_flag) 1131f58a9d17SGeoff Levand { 1132f58a9d17SGeoff Levand unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr) 1133f58a9d17SGeoff Levand : virt_addr; 11346bb5cf10SGeoff Levand *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr)); 1135f58a9d17SGeoff Levand return 0; 1136f58a9d17SGeoff Levand } 1137f58a9d17SGeoff Levand 1138f58a9d17SGeoff Levand /** 1139f58a9d17SGeoff Levand * dma_unmap_area_linear - Unmap an area of memory from a device dma region. 1140f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 1141f58a9d17SGeoff Levand * @bus_addr: The starting ioc bus address of the area to unmap. 1142f58a9d17SGeoff Levand * @len: Length in bytes of the area to unmap. 1143f58a9d17SGeoff Levand * 11446bb5cf10SGeoff Levand * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear(). 1145f58a9d17SGeoff Levand */ 1146f58a9d17SGeoff Levand 11476bb5cf10SGeoff Levand static int dma_sb_unmap_area_linear(struct ps3_dma_region *r, 1148494fd07aSStephen Rothwell dma_addr_t bus_addr, unsigned long len) 1149f58a9d17SGeoff Levand { 1150f58a9d17SGeoff Levand return 0; 11516bb5cf10SGeoff Levand }; 11526bb5cf10SGeoff Levand 11536bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = { 11546bb5cf10SGeoff Levand .create = dma_sb_region_create, 11556bb5cf10SGeoff Levand .free = dma_sb_region_free, 11566bb5cf10SGeoff Levand .map = dma_sb_map_area, 11576bb5cf10SGeoff Levand .unmap = dma_sb_unmap_area 11586bb5cf10SGeoff Levand }; 11596bb5cf10SGeoff Levand 11606bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = { 11616bb5cf10SGeoff Levand .create = dma_sb_region_create_linear, 11626bb5cf10SGeoff Levand .free = dma_sb_region_free_linear, 11636bb5cf10SGeoff Levand .map = dma_sb_map_area_linear, 11646bb5cf10SGeoff Levand .unmap = dma_sb_unmap_area_linear 11656bb5cf10SGeoff Levand }; 11666bb5cf10SGeoff Levand 11676bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = { 11686bb5cf10SGeoff Levand .create = dma_ioc0_region_create, 11696bb5cf10SGeoff Levand .free = dma_ioc0_region_free, 11706bb5cf10SGeoff Levand .map = dma_ioc0_map_area, 11716bb5cf10SGeoff Levand .unmap = dma_ioc0_unmap_area 11726bb5cf10SGeoff Levand }; 11736bb5cf10SGeoff Levand 11746bb5cf10SGeoff Levand int ps3_dma_region_init(struct ps3_system_bus_device *dev, 11756bb5cf10SGeoff Levand struct ps3_dma_region *r, enum ps3_dma_page_size page_size, 11766bb5cf10SGeoff Levand enum ps3_dma_region_type region_type, void *addr, unsigned long len) 11776bb5cf10SGeoff Levand { 11786bb5cf10SGeoff Levand unsigned long lpar_addr; 11796bb5cf10SGeoff Levand 11806bb5cf10SGeoff Levand lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0; 11816bb5cf10SGeoff Levand 11826bb5cf10SGeoff Levand r->dev = dev; 11836bb5cf10SGeoff Levand r->page_size = page_size; 11846bb5cf10SGeoff Levand r->region_type = region_type; 11856bb5cf10SGeoff Levand r->offset = lpar_addr; 11866bb5cf10SGeoff Levand if (r->offset >= map.rm.size) 11876bb5cf10SGeoff Levand r->offset -= map.r1.offset; 11886bb5cf10SGeoff Levand r->len = len ? len : _ALIGN_UP(map.total, 1 << r->page_size); 11896bb5cf10SGeoff Levand 11906bb5cf10SGeoff Levand switch (dev->dev_type) { 11916bb5cf10SGeoff Levand case PS3_DEVICE_TYPE_SB: 11926bb5cf10SGeoff Levand r->region_ops = (USE_DYNAMIC_DMA) 11936bb5cf10SGeoff Levand ? &ps3_dma_sb_region_ops 11946bb5cf10SGeoff Levand : &ps3_dma_sb_region_linear_ops; 11956bb5cf10SGeoff Levand break; 11966bb5cf10SGeoff Levand case PS3_DEVICE_TYPE_IOC0: 11976bb5cf10SGeoff Levand r->region_ops = &ps3_dma_ioc0_region_ops; 11986bb5cf10SGeoff Levand break; 11996bb5cf10SGeoff Levand default: 12006bb5cf10SGeoff Levand BUG(); 12016bb5cf10SGeoff Levand return -EINVAL; 1202f58a9d17SGeoff Levand } 12036bb5cf10SGeoff Levand return 0; 12046bb5cf10SGeoff Levand } 12056bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_init); 1206f58a9d17SGeoff Levand 1207f58a9d17SGeoff Levand int ps3_dma_region_create(struct ps3_dma_region *r) 1208f58a9d17SGeoff Levand { 12096bb5cf10SGeoff Levand BUG_ON(!r); 12106bb5cf10SGeoff Levand BUG_ON(!r->region_ops); 12116bb5cf10SGeoff Levand BUG_ON(!r->region_ops->create); 12126bb5cf10SGeoff Levand return r->region_ops->create(r); 1213f58a9d17SGeoff Levand } 12146bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_create); 1215f58a9d17SGeoff Levand 1216f58a9d17SGeoff Levand int ps3_dma_region_free(struct ps3_dma_region *r) 1217f58a9d17SGeoff Levand { 12186bb5cf10SGeoff Levand BUG_ON(!r); 12196bb5cf10SGeoff Levand BUG_ON(!r->region_ops); 12206bb5cf10SGeoff Levand BUG_ON(!r->region_ops->free); 12216bb5cf10SGeoff Levand return r->region_ops->free(r); 1222f58a9d17SGeoff Levand } 12236bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_free); 1224f58a9d17SGeoff Levand 1225f58a9d17SGeoff Levand int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr, 1226494fd07aSStephen Rothwell unsigned long len, dma_addr_t *bus_addr, 12276bb5cf10SGeoff Levand u64 iopte_flag) 1228f58a9d17SGeoff Levand { 12296bb5cf10SGeoff Levand return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag); 1230f58a9d17SGeoff Levand } 1231f58a9d17SGeoff Levand 1232494fd07aSStephen Rothwell int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr, 1233f58a9d17SGeoff Levand unsigned long len) 1234f58a9d17SGeoff Levand { 12356bb5cf10SGeoff Levand return r->region_ops->unmap(r, bus_addr, len); 1236f58a9d17SGeoff Levand } 1237f58a9d17SGeoff Levand 1238f58a9d17SGeoff Levand /*============================================================================*/ 1239f58a9d17SGeoff Levand /* system startup routines */ 1240f58a9d17SGeoff Levand /*============================================================================*/ 1241f58a9d17SGeoff Levand 1242f58a9d17SGeoff Levand /** 1243f58a9d17SGeoff Levand * ps3_mm_init - initialize the address space state variables 1244f58a9d17SGeoff Levand */ 1245f58a9d17SGeoff Levand 1246f58a9d17SGeoff Levand void __init ps3_mm_init(void) 1247f58a9d17SGeoff Levand { 1248f58a9d17SGeoff Levand int result; 1249f58a9d17SGeoff Levand 1250f58a9d17SGeoff Levand DBG(" -> %s:%d\n", __func__, __LINE__); 1251f58a9d17SGeoff Levand 1252f58a9d17SGeoff Levand result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size, 1253f58a9d17SGeoff Levand &map.total); 1254f58a9d17SGeoff Levand 1255f58a9d17SGeoff Levand if (result) 1256f58a9d17SGeoff Levand panic("ps3_repository_read_mm_info() failed"); 1257f58a9d17SGeoff Levand 1258f58a9d17SGeoff Levand map.rm.offset = map.rm.base; 1259f58a9d17SGeoff Levand map.vas_id = map.htab_size = 0; 1260f58a9d17SGeoff Levand 1261f58a9d17SGeoff Levand /* this implementation assumes map.rm.base is zero */ 1262f58a9d17SGeoff Levand 1263f58a9d17SGeoff Levand BUG_ON(map.rm.base); 1264f58a9d17SGeoff Levand BUG_ON(!map.rm.size); 1265f58a9d17SGeoff Levand 1266*1e755c09SAndre Heider /* Check if we got the highmem region from an earlier boot step */ 1267f58a9d17SGeoff Levand 1268*1e755c09SAndre Heider if (ps3_mm_get_repository_highmem(&map.r1)) 1269f58a9d17SGeoff Levand ps3_mm_region_create(&map.r1, map.total - map.rm.size); 1270f58a9d17SGeoff Levand 12716bb5cf10SGeoff Levand /* correct map.total for the real total amount of memory we use */ 12726bb5cf10SGeoff Levand map.total = map.rm.size + map.r1.size; 12736bb5cf10SGeoff Levand 1274f58a9d17SGeoff Levand DBG(" <- %s:%d\n", __func__, __LINE__); 1275f58a9d17SGeoff Levand } 1276f58a9d17SGeoff Levand 1277f58a9d17SGeoff Levand /** 1278f58a9d17SGeoff Levand * ps3_mm_shutdown - final cleanup of address space 1279f58a9d17SGeoff Levand */ 1280f58a9d17SGeoff Levand 1281f58a9d17SGeoff Levand void ps3_mm_shutdown(void) 1282f58a9d17SGeoff Levand { 1283f58a9d17SGeoff Levand ps3_mm_region_destroy(&map.r1); 1284f58a9d17SGeoff Levand } 1285