1873e65bcSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2f58a9d17SGeoff Levand /* 3f58a9d17SGeoff Levand * PS3 address space management. 4f58a9d17SGeoff Levand * 5f58a9d17SGeoff Levand * Copyright (C) 2006 Sony Computer Entertainment Inc. 6f58a9d17SGeoff Levand * Copyright 2006 Sony Corp. 7f58a9d17SGeoff Levand */ 8f58a9d17SGeoff Levand 9f58a9d17SGeoff Levand #include <linux/kernel.h> 104b16f8e2SPaul Gortmaker #include <linux/export.h> 1195f72d1eSYinghai Lu #include <linux/memblock.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 13f58a9d17SGeoff Levand 149413c883SGeert Uytterhoeven #include <asm/cell-regs.h> 15e22ba7e3SArnd Bergmann #include <asm/firmware.h> 16d9b2b2a2SDavid S. Miller #include <asm/prom.h> 17f58a9d17SGeoff Levand #include <asm/udbg.h> 18f58a9d17SGeoff Levand #include <asm/lv1call.h> 19ae3a197eSDavid Howells #include <asm/setup.h> 20f58a9d17SGeoff Levand 21f58a9d17SGeoff Levand #include "platform.h" 22f58a9d17SGeoff Levand 23f58a9d17SGeoff Levand #if defined(DEBUG) 2483bb643dSGeert Uytterhoeven #define DBG udbg_printf 25f58a9d17SGeoff Levand #else 267424639aSMichael Ellerman #define DBG pr_devel 27f58a9d17SGeoff Levand #endif 28f58a9d17SGeoff Levand 29f58a9d17SGeoff Levand enum { 30f58a9d17SGeoff Levand #if defined(CONFIG_PS3_DYNAMIC_DMA) 31f58a9d17SGeoff Levand USE_DYNAMIC_DMA = 1, 32f58a9d17SGeoff Levand #else 33f58a9d17SGeoff Levand USE_DYNAMIC_DMA = 0, 34f58a9d17SGeoff Levand #endif 35f58a9d17SGeoff Levand }; 36f58a9d17SGeoff Levand 37f58a9d17SGeoff Levand enum { 38f58a9d17SGeoff Levand PAGE_SHIFT_4K = 12U, 39f58a9d17SGeoff Levand PAGE_SHIFT_64K = 16U, 40f58a9d17SGeoff Levand PAGE_SHIFT_16M = 24U, 41f58a9d17SGeoff Levand }; 42f58a9d17SGeoff Levand 43f58a9d17SGeoff Levand static unsigned long make_page_sizes(unsigned long a, unsigned long b) 44f58a9d17SGeoff Levand { 45f58a9d17SGeoff Levand return (a << 56) | (b << 48); 46f58a9d17SGeoff Levand } 47f58a9d17SGeoff Levand 48f58a9d17SGeoff Levand enum { 49f58a9d17SGeoff Levand ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04, 50f58a9d17SGeoff Levand ALLOCATE_MEMORY_ADDR_ZERO = 0X08, 51f58a9d17SGeoff Levand }; 52f58a9d17SGeoff Levand 53f58a9d17SGeoff Levand /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */ 54f58a9d17SGeoff Levand 55f58a9d17SGeoff Levand enum { 56f58a9d17SGeoff Levand HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */ 57f58a9d17SGeoff Levand HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */ 58f58a9d17SGeoff Levand }; 59f58a9d17SGeoff Levand 60f58a9d17SGeoff Levand /*============================================================================*/ 61f58a9d17SGeoff Levand /* virtual address space routines */ 62f58a9d17SGeoff Levand /*============================================================================*/ 63f58a9d17SGeoff Levand 64f58a9d17SGeoff Levand /** 65f58a9d17SGeoff Levand * struct mem_region - memory region structure 66f58a9d17SGeoff Levand * @base: base address 67f58a9d17SGeoff Levand * @size: size in bytes 68f58a9d17SGeoff Levand * @offset: difference between base and rm.size 691e755c09SAndre Heider * @destroy: flag if region should be destroyed upon shutdown 70f58a9d17SGeoff Levand */ 71f58a9d17SGeoff Levand 72f58a9d17SGeoff Levand struct mem_region { 73b17b3df1SStephen Rothwell u64 base; 745418b9c6SStephen Rothwell u64 size; 75f58a9d17SGeoff Levand unsigned long offset; 761e755c09SAndre Heider int destroy; 77f58a9d17SGeoff Levand }; 78f58a9d17SGeoff Levand 79f58a9d17SGeoff Levand /** 80f58a9d17SGeoff Levand * struct map - address space state variables holder 81f58a9d17SGeoff Levand * @total: total memory available as reported by HV 82f58a9d17SGeoff Levand * @vas_id - HV virtual address space id 83f58a9d17SGeoff Levand * @htab_size: htab size in bytes 84f58a9d17SGeoff Levand * 85f58a9d17SGeoff Levand * The HV virtual address space (vas) allows for hotplug memory regions. 86f58a9d17SGeoff Levand * Memory regions can be created and destroyed in the vas at runtime. 87f58a9d17SGeoff Levand * @rm: real mode (bootmem) region 888ac5fd11SHector Martin * @r1: highmem region(s) 89f58a9d17SGeoff Levand * 90f58a9d17SGeoff Levand * ps3 addresses 91f58a9d17SGeoff Levand * virt_addr: a cpu 'translated' effective address 92f58a9d17SGeoff Levand * phys_addr: an address in what Linux thinks is the physical address space 93f58a9d17SGeoff Levand * lpar_addr: an address in the HV virtual address space 94f58a9d17SGeoff Levand * bus_addr: an io controller 'translated' address on a device bus 95f58a9d17SGeoff Levand */ 96f58a9d17SGeoff Levand 97f58a9d17SGeoff Levand struct map { 985418b9c6SStephen Rothwell u64 total; 99b17b3df1SStephen Rothwell u64 vas_id; 100b17b3df1SStephen Rothwell u64 htab_size; 101f58a9d17SGeoff Levand struct mem_region rm; 102f58a9d17SGeoff Levand struct mem_region r1; 103f58a9d17SGeoff Levand }; 104f58a9d17SGeoff Levand 105f58a9d17SGeoff Levand #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__) 1066bb5cf10SGeoff Levand static void __maybe_unused _debug_dump_map(const struct map *m, 1076bb5cf10SGeoff Levand const char *func, int line) 108f58a9d17SGeoff Levand { 1095418b9c6SStephen Rothwell DBG("%s:%d: map.total = %llxh\n", func, line, m->total); 1105418b9c6SStephen Rothwell DBG("%s:%d: map.rm.size = %llxh\n", func, line, m->rm.size); 111b17b3df1SStephen Rothwell DBG("%s:%d: map.vas_id = %llu\n", func, line, m->vas_id); 112b17b3df1SStephen Rothwell DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size); 113b17b3df1SStephen Rothwell DBG("%s:%d: map.r1.base = %llxh\n", func, line, m->r1.base); 114f58a9d17SGeoff Levand DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset); 1155418b9c6SStephen Rothwell DBG("%s:%d: map.r1.size = %llxh\n", func, line, m->r1.size); 116f58a9d17SGeoff Levand } 117f58a9d17SGeoff Levand 118f58a9d17SGeoff Levand static struct map map; 119f58a9d17SGeoff Levand 120f58a9d17SGeoff Levand /** 121f58a9d17SGeoff Levand * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address 122f58a9d17SGeoff Levand * @phys_addr: linux physical address 123f58a9d17SGeoff Levand */ 124f58a9d17SGeoff Levand 125f58a9d17SGeoff Levand unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr) 126f58a9d17SGeoff Levand { 127f58a9d17SGeoff Levand BUG_ON(is_kernel_addr(phys_addr)); 128f58a9d17SGeoff Levand return (phys_addr < map.rm.size || phys_addr >= map.total) 129f58a9d17SGeoff Levand ? phys_addr : phys_addr + map.r1.offset; 130f58a9d17SGeoff Levand } 131f58a9d17SGeoff Levand 132f58a9d17SGeoff Levand EXPORT_SYMBOL(ps3_mm_phys_to_lpar); 133f58a9d17SGeoff Levand 134f58a9d17SGeoff Levand /** 135f58a9d17SGeoff Levand * ps3_mm_vas_create - create the virtual address space 136f58a9d17SGeoff Levand */ 137f58a9d17SGeoff Levand 138f58a9d17SGeoff Levand void __init ps3_mm_vas_create(unsigned long* htab_size) 139f58a9d17SGeoff Levand { 140f58a9d17SGeoff Levand int result; 141b17b3df1SStephen Rothwell u64 start_address; 142b17b3df1SStephen Rothwell u64 size; 143b17b3df1SStephen Rothwell u64 access_right; 144b17b3df1SStephen Rothwell u64 max_page_size; 145b17b3df1SStephen Rothwell u64 flags; 146f58a9d17SGeoff Levand 147f58a9d17SGeoff Levand result = lv1_query_logical_partition_address_region_info(0, 148f58a9d17SGeoff Levand &start_address, &size, &access_right, &max_page_size, 149f58a9d17SGeoff Levand &flags); 150f58a9d17SGeoff Levand 151f58a9d17SGeoff Levand if (result) { 152f58a9d17SGeoff Levand DBG("%s:%d: lv1_query_logical_partition_address_region_info " 153f58a9d17SGeoff Levand "failed: %s\n", __func__, __LINE__, 154f58a9d17SGeoff Levand ps3_result(result)); 155f58a9d17SGeoff Levand goto fail; 156f58a9d17SGeoff Levand } 157f58a9d17SGeoff Levand 158f58a9d17SGeoff Levand if (max_page_size < PAGE_SHIFT_16M) { 159b17b3df1SStephen Rothwell DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__, 160f58a9d17SGeoff Levand max_page_size); 161f58a9d17SGeoff Levand goto fail; 162f58a9d17SGeoff Levand } 163f58a9d17SGeoff Levand 164f58a9d17SGeoff Levand BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX); 165f58a9d17SGeoff Levand BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN); 166f58a9d17SGeoff Levand 167f58a9d17SGeoff Levand result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE, 168f58a9d17SGeoff Levand 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K), 169f58a9d17SGeoff Levand &map.vas_id, &map.htab_size); 170f58a9d17SGeoff Levand 171f58a9d17SGeoff Levand if (result) { 172f58a9d17SGeoff Levand DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n", 173f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 174f58a9d17SGeoff Levand goto fail; 175f58a9d17SGeoff Levand } 176f58a9d17SGeoff Levand 177f58a9d17SGeoff Levand result = lv1_select_virtual_address_space(map.vas_id); 178f58a9d17SGeoff Levand 179f58a9d17SGeoff Levand if (result) { 180f58a9d17SGeoff Levand DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n", 181f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 182f58a9d17SGeoff Levand goto fail; 183f58a9d17SGeoff Levand } 184f58a9d17SGeoff Levand 185f58a9d17SGeoff Levand *htab_size = map.htab_size; 186f58a9d17SGeoff Levand 187f58a9d17SGeoff Levand debug_dump_map(&map); 188f58a9d17SGeoff Levand 189f58a9d17SGeoff Levand return; 190f58a9d17SGeoff Levand 191f58a9d17SGeoff Levand fail: 192f58a9d17SGeoff Levand panic("ps3_mm_vas_create failed"); 193f58a9d17SGeoff Levand } 194f58a9d17SGeoff Levand 195f58a9d17SGeoff Levand /** 196f58a9d17SGeoff Levand * ps3_mm_vas_destroy - 197f58a9d17SGeoff Levand */ 198f58a9d17SGeoff Levand 199f58a9d17SGeoff Levand void ps3_mm_vas_destroy(void) 200f58a9d17SGeoff Levand { 2016bb5cf10SGeoff Levand int result; 2026bb5cf10SGeoff Levand 203f58a9d17SGeoff Levand if (map.vas_id) { 2046bb5cf10SGeoff Levand result = lv1_select_virtual_address_space(0); 205*12655446SGeoff Levand result += lv1_destruct_virtual_address_space(map.vas_id); 206*12655446SGeoff Levand 207*12655446SGeoff Levand if (result) { 208*12655446SGeoff Levand lv1_panic(0); 209*12655446SGeoff Levand } 210*12655446SGeoff Levand 211f58a9d17SGeoff Levand map.vas_id = 0; 212f58a9d17SGeoff Levand } 213f58a9d17SGeoff Levand } 214f58a9d17SGeoff Levand 215d4b18bd6SGeoff Levand static int ps3_mm_get_repository_highmem(struct mem_region *r) 216d4b18bd6SGeoff Levand { 217d4b18bd6SGeoff Levand int result; 218d4b18bd6SGeoff Levand 219d4b18bd6SGeoff Levand /* Assume a single highmem region. */ 220d4b18bd6SGeoff Levand 221d4b18bd6SGeoff Levand result = ps3_repository_read_highmem_info(0, &r->base, &r->size); 222d4b18bd6SGeoff Levand 223d4b18bd6SGeoff Levand if (result) 224d4b18bd6SGeoff Levand goto zero_region; 225d4b18bd6SGeoff Levand 226d4b18bd6SGeoff Levand if (!r->base || !r->size) { 227d4b18bd6SGeoff Levand result = -1; 228d4b18bd6SGeoff Levand goto zero_region; 229d4b18bd6SGeoff Levand } 230d4b18bd6SGeoff Levand 231d4b18bd6SGeoff Levand r->offset = r->base - map.rm.size; 232d4b18bd6SGeoff Levand 233d4b18bd6SGeoff Levand DBG("%s:%d: Found high region in repository: %llxh %llxh\n", 234d4b18bd6SGeoff Levand __func__, __LINE__, r->base, r->size); 235d4b18bd6SGeoff Levand 236d4b18bd6SGeoff Levand return 0; 237d4b18bd6SGeoff Levand 238d4b18bd6SGeoff Levand zero_region: 239d4b18bd6SGeoff Levand DBG("%s:%d: No high region in repository.\n", __func__, __LINE__); 240d4b18bd6SGeoff Levand 241d4b18bd6SGeoff Levand r->size = r->base = r->offset = 0; 242d4b18bd6SGeoff Levand return result; 243d4b18bd6SGeoff Levand } 244d4b18bd6SGeoff Levand 245d4b18bd6SGeoff Levand static int ps3_mm_set_repository_highmem(const struct mem_region *r) 246d4b18bd6SGeoff Levand { 247d4b18bd6SGeoff Levand /* Assume a single highmem region. */ 248d4b18bd6SGeoff Levand 249d4b18bd6SGeoff Levand return r ? ps3_repository_write_highmem_info(0, r->base, r->size) : 250d4b18bd6SGeoff Levand ps3_repository_write_highmem_info(0, 0, 0); 251d4b18bd6SGeoff Levand } 252d4b18bd6SGeoff Levand 253f58a9d17SGeoff Levand /** 254f58a9d17SGeoff Levand * ps3_mm_region_create - create a memory region in the vas 255f58a9d17SGeoff Levand * @r: pointer to a struct mem_region to accept initialized values 256f58a9d17SGeoff Levand * @size: requested region size 257f58a9d17SGeoff Levand * 258f58a9d17SGeoff Levand * This implementation creates the region with the vas large page size. 259f58a9d17SGeoff Levand * @size is rounded down to a multiple of the vas large page size. 260f58a9d17SGeoff Levand */ 261f58a9d17SGeoff Levand 26232f44a12SGeert Uytterhoeven static int ps3_mm_region_create(struct mem_region *r, unsigned long size) 263f58a9d17SGeoff Levand { 264f58a9d17SGeoff Levand int result; 265b17b3df1SStephen Rothwell u64 muid; 266f58a9d17SGeoff Levand 267e96d904eSChristophe Leroy r->size = ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M); 268f58a9d17SGeoff Levand 269f58a9d17SGeoff Levand DBG("%s:%d requested %lxh\n", __func__, __LINE__, size); 2705418b9c6SStephen Rothwell DBG("%s:%d actual %llxh\n", __func__, __LINE__, r->size); 2715418b9c6SStephen Rothwell DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__, 2725418b9c6SStephen Rothwell size - r->size, (size - r->size) / 1024 / 1024); 273f58a9d17SGeoff Levand 274f58a9d17SGeoff Levand if (r->size == 0) { 275f58a9d17SGeoff Levand DBG("%s:%d: size == 0\n", __func__, __LINE__); 276f58a9d17SGeoff Levand result = -1; 277f58a9d17SGeoff Levand goto zero_region; 278f58a9d17SGeoff Levand } 279f58a9d17SGeoff Levand 280f58a9d17SGeoff Levand result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0, 281f58a9d17SGeoff Levand ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid); 282f58a9d17SGeoff Levand 283f58a9d17SGeoff Levand if (result || r->base < map.rm.size) { 284f58a9d17SGeoff Levand DBG("%s:%d: lv1_allocate_memory failed: %s\n", 285f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 286f58a9d17SGeoff Levand goto zero_region; 287f58a9d17SGeoff Levand } 288f58a9d17SGeoff Levand 2891e755c09SAndre Heider r->destroy = 1; 290f58a9d17SGeoff Levand r->offset = r->base - map.rm.size; 291f58a9d17SGeoff Levand return result; 292f58a9d17SGeoff Levand 293f58a9d17SGeoff Levand zero_region: 294f58a9d17SGeoff Levand r->size = r->base = r->offset = 0; 295f58a9d17SGeoff Levand return result; 296f58a9d17SGeoff Levand } 297f58a9d17SGeoff Levand 298f58a9d17SGeoff Levand /** 299f58a9d17SGeoff Levand * ps3_mm_region_destroy - destroy a memory region 300f58a9d17SGeoff Levand * @r: pointer to struct mem_region 301f58a9d17SGeoff Levand */ 302f58a9d17SGeoff Levand 30332f44a12SGeert Uytterhoeven static void ps3_mm_region_destroy(struct mem_region *r) 304f58a9d17SGeoff Levand { 3056bb5cf10SGeoff Levand int result; 3066bb5cf10SGeoff Levand 3071e755c09SAndre Heider if (!r->destroy) { 3081e755c09SAndre Heider return; 3091e755c09SAndre Heider } 3101e755c09SAndre Heider 311f58a9d17SGeoff Levand if (r->base) { 3126bb5cf10SGeoff Levand result = lv1_release_memory(r->base); 313*12655446SGeoff Levand 314*12655446SGeoff Levand if (result) { 315*12655446SGeoff Levand lv1_panic(0); 316*12655446SGeoff Levand } 317*12655446SGeoff Levand 318f58a9d17SGeoff Levand r->size = r->base = r->offset = 0; 319f58a9d17SGeoff Levand map.total = map.rm.size; 320f58a9d17SGeoff Levand } 321*12655446SGeoff Levand 3225ae74630SGeoff Levand ps3_mm_set_repository_highmem(NULL); 323f58a9d17SGeoff Levand } 324f58a9d17SGeoff Levand 325f58a9d17SGeoff Levand /*============================================================================*/ 326f58a9d17SGeoff Levand /* dma routines */ 327f58a9d17SGeoff Levand /*============================================================================*/ 328f58a9d17SGeoff Levand 329f58a9d17SGeoff Levand /** 3306bb5cf10SGeoff Levand * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address. 331f58a9d17SGeoff Levand * @r: pointer to dma region structure 332f58a9d17SGeoff Levand * @lpar_addr: HV lpar address 333f58a9d17SGeoff Levand */ 334f58a9d17SGeoff Levand 3356bb5cf10SGeoff Levand static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r, 336f58a9d17SGeoff Levand unsigned long lpar_addr) 337f58a9d17SGeoff Levand { 3386bb5cf10SGeoff Levand if (lpar_addr >= map.rm.size) 3396bb5cf10SGeoff Levand lpar_addr -= map.r1.offset; 3406bb5cf10SGeoff Levand BUG_ON(lpar_addr < r->offset); 3416bb5cf10SGeoff Levand BUG_ON(lpar_addr >= r->offset + r->len); 3426bb5cf10SGeoff Levand return r->bus_addr + lpar_addr - r->offset; 343f58a9d17SGeoff Levand } 344f58a9d17SGeoff Levand 345f58a9d17SGeoff Levand #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__) 3466bb5cf10SGeoff Levand static void __maybe_unused _dma_dump_region(const struct ps3_dma_region *r, 3476bb5cf10SGeoff Levand const char *func, int line) 348f58a9d17SGeoff Levand { 3495c949070SStephen Rothwell DBG("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id, 3506bb5cf10SGeoff Levand r->dev->dev_id); 351f58a9d17SGeoff Levand DBG("%s:%d: page_size %u\n", func, line, r->page_size); 352f58a9d17SGeoff Levand DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr); 353f58a9d17SGeoff Levand DBG("%s:%d: len %lxh\n", func, line, r->len); 3546bb5cf10SGeoff Levand DBG("%s:%d: offset %lxh\n", func, line, r->offset); 355f58a9d17SGeoff Levand } 356f58a9d17SGeoff Levand 357f58a9d17SGeoff Levand /** 358f58a9d17SGeoff Levand * dma_chunk - A chunk of dma pages mapped by the io controller. 359f58a9d17SGeoff Levand * @region - The dma region that owns this chunk. 360f58a9d17SGeoff Levand * @lpar_addr: Starting lpar address of the area to map. 361f58a9d17SGeoff Levand * @bus_addr: Starting ioc bus address of the area to map. 362f58a9d17SGeoff Levand * @len: Length in bytes of the area to map. 363f58a9d17SGeoff Levand * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the 364f58a9d17SGeoff Levand * list of all chuncks owned by the region. 365f58a9d17SGeoff Levand * 366f58a9d17SGeoff Levand * This implementation uses a very simple dma page manager 367f58a9d17SGeoff Levand * based on the dma_chunk structure. This scheme assumes 368f58a9d17SGeoff Levand * that all drivers use very well behaved dma ops. 369f58a9d17SGeoff Levand */ 370f58a9d17SGeoff Levand 371f58a9d17SGeoff Levand struct dma_chunk { 372f58a9d17SGeoff Levand struct ps3_dma_region *region; 373f58a9d17SGeoff Levand unsigned long lpar_addr; 374f58a9d17SGeoff Levand unsigned long bus_addr; 375f58a9d17SGeoff Levand unsigned long len; 376f58a9d17SGeoff Levand struct list_head link; 377f58a9d17SGeoff Levand unsigned int usage_count; 378f58a9d17SGeoff Levand }; 379f58a9d17SGeoff Levand 380f58a9d17SGeoff Levand #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__) 381f58a9d17SGeoff Levand static void _dma_dump_chunk (const struct dma_chunk* c, const char* func, 382f58a9d17SGeoff Levand int line) 383f58a9d17SGeoff Levand { 3845c949070SStephen Rothwell DBG("%s:%d: r.dev %llu:%llu\n", func, line, 3856bb5cf10SGeoff Levand c->region->dev->bus_id, c->region->dev->dev_id); 386f58a9d17SGeoff Levand DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr); 387f58a9d17SGeoff Levand DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size); 388f58a9d17SGeoff Levand DBG("%s:%d: r.len %lxh\n", func, line, c->region->len); 3896bb5cf10SGeoff Levand DBG("%s:%d: r.offset %lxh\n", func, line, c->region->offset); 390f58a9d17SGeoff Levand DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr); 391f58a9d17SGeoff Levand DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr); 392f58a9d17SGeoff Levand DBG("%s:%d: c.len %lxh\n", func, line, c->len); 393f58a9d17SGeoff Levand } 394f58a9d17SGeoff Levand 395f58a9d17SGeoff Levand static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r, 396f58a9d17SGeoff Levand unsigned long bus_addr, unsigned long len) 397f58a9d17SGeoff Levand { 398f58a9d17SGeoff Levand struct dma_chunk *c; 399e96d904eSChristophe Leroy unsigned long aligned_bus = ALIGN_DOWN(bus_addr, 1 << r->page_size); 400b7115316SChristophe Leroy unsigned long aligned_len = ALIGN(len+bus_addr-aligned_bus, 4016bb5cf10SGeoff Levand 1 << r->page_size); 402f58a9d17SGeoff Levand 403f58a9d17SGeoff Levand list_for_each_entry(c, &r->chunk_list.head, link) { 404f58a9d17SGeoff Levand /* intersection */ 4056bb5cf10SGeoff Levand if (aligned_bus >= c->bus_addr && 4066bb5cf10SGeoff Levand aligned_bus + aligned_len <= c->bus_addr + c->len) 407f58a9d17SGeoff Levand return c; 4086bb5cf10SGeoff Levand 409f58a9d17SGeoff Levand /* below */ 4106bb5cf10SGeoff Levand if (aligned_bus + aligned_len <= c->bus_addr) 411f58a9d17SGeoff Levand continue; 4126bb5cf10SGeoff Levand 413f58a9d17SGeoff Levand /* above */ 4146bb5cf10SGeoff Levand if (aligned_bus >= c->bus_addr + c->len) 415f58a9d17SGeoff Levand continue; 416f58a9d17SGeoff Levand 417f58a9d17SGeoff Levand /* we don't handle the multi-chunk case for now */ 418f58a9d17SGeoff Levand dma_dump_chunk(c); 419f58a9d17SGeoff Levand BUG(); 420f58a9d17SGeoff Levand } 421f58a9d17SGeoff Levand return NULL; 422f58a9d17SGeoff Levand } 423f58a9d17SGeoff Levand 4246bb5cf10SGeoff Levand static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r, 4256bb5cf10SGeoff Levand unsigned long lpar_addr, unsigned long len) 4266bb5cf10SGeoff Levand { 4276bb5cf10SGeoff Levand struct dma_chunk *c; 428e96d904eSChristophe Leroy unsigned long aligned_lpar = ALIGN_DOWN(lpar_addr, 1 << r->page_size); 429b7115316SChristophe Leroy unsigned long aligned_len = ALIGN(len + lpar_addr - aligned_lpar, 4306bb5cf10SGeoff Levand 1 << r->page_size); 4316bb5cf10SGeoff Levand 4326bb5cf10SGeoff Levand list_for_each_entry(c, &r->chunk_list.head, link) { 4336bb5cf10SGeoff Levand /* intersection */ 4346bb5cf10SGeoff Levand if (c->lpar_addr <= aligned_lpar && 4356bb5cf10SGeoff Levand aligned_lpar < c->lpar_addr + c->len) { 4366bb5cf10SGeoff Levand if (aligned_lpar + aligned_len <= c->lpar_addr + c->len) 4376bb5cf10SGeoff Levand return c; 4386bb5cf10SGeoff Levand else { 4396bb5cf10SGeoff Levand dma_dump_chunk(c); 4406bb5cf10SGeoff Levand BUG(); 4416bb5cf10SGeoff Levand } 4426bb5cf10SGeoff Levand } 4436bb5cf10SGeoff Levand /* below */ 4446bb5cf10SGeoff Levand if (aligned_lpar + aligned_len <= c->lpar_addr) { 4456bb5cf10SGeoff Levand continue; 4466bb5cf10SGeoff Levand } 4476bb5cf10SGeoff Levand /* above */ 4486bb5cf10SGeoff Levand if (c->lpar_addr + c->len <= aligned_lpar) { 4496bb5cf10SGeoff Levand continue; 4506bb5cf10SGeoff Levand } 4516bb5cf10SGeoff Levand } 4526bb5cf10SGeoff Levand return NULL; 4536bb5cf10SGeoff Levand } 4546bb5cf10SGeoff Levand 4556bb5cf10SGeoff Levand static int dma_sb_free_chunk(struct dma_chunk *c) 456f58a9d17SGeoff Levand { 457f58a9d17SGeoff Levand int result = 0; 458f58a9d17SGeoff Levand 459f58a9d17SGeoff Levand if (c->bus_addr) { 4606bb5cf10SGeoff Levand result = lv1_unmap_device_dma_region(c->region->dev->bus_id, 4616bb5cf10SGeoff Levand c->region->dev->dev_id, c->bus_addr, c->len); 462f58a9d17SGeoff Levand BUG_ON(result); 463f58a9d17SGeoff Levand } 464f58a9d17SGeoff Levand 465f58a9d17SGeoff Levand kfree(c); 466f58a9d17SGeoff Levand return result; 467f58a9d17SGeoff Levand } 468f58a9d17SGeoff Levand 4696bb5cf10SGeoff Levand static int dma_ioc0_free_chunk(struct dma_chunk *c) 4706bb5cf10SGeoff Levand { 4716bb5cf10SGeoff Levand int result = 0; 4726bb5cf10SGeoff Levand int iopage; 4736bb5cf10SGeoff Levand unsigned long offset; 4746bb5cf10SGeoff Levand struct ps3_dma_region *r = c->region; 4756bb5cf10SGeoff Levand 4766bb5cf10SGeoff Levand DBG("%s:start\n", __func__); 4776bb5cf10SGeoff Levand for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) { 4786bb5cf10SGeoff Levand offset = (1 << r->page_size) * iopage; 4796bb5cf10SGeoff Levand /* put INVALID entry */ 4806bb5cf10SGeoff Levand result = lv1_put_iopte(0, 4816bb5cf10SGeoff Levand c->bus_addr + offset, 4826bb5cf10SGeoff Levand c->lpar_addr + offset, 4836bb5cf10SGeoff Levand r->ioid, 4846bb5cf10SGeoff Levand 0); 4856bb5cf10SGeoff Levand DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__, 4866bb5cf10SGeoff Levand c->bus_addr + offset, 4876bb5cf10SGeoff Levand c->lpar_addr + offset, 4886bb5cf10SGeoff Levand r->ioid); 4896bb5cf10SGeoff Levand 4906bb5cf10SGeoff Levand if (result) { 4916bb5cf10SGeoff Levand DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__, 4926bb5cf10SGeoff Levand __LINE__, ps3_result(result)); 4936bb5cf10SGeoff Levand } 4946bb5cf10SGeoff Levand } 4956bb5cf10SGeoff Levand kfree(c); 4966bb5cf10SGeoff Levand DBG("%s:end\n", __func__); 4976bb5cf10SGeoff Levand return result; 4986bb5cf10SGeoff Levand } 4996bb5cf10SGeoff Levand 500f58a9d17SGeoff Levand /** 5016bb5cf10SGeoff Levand * dma_sb_map_pages - Maps dma pages into the io controller bus address space. 502f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 503f58a9d17SGeoff Levand * @phys_addr: Starting physical address of the area to map. 504f58a9d17SGeoff Levand * @len: Length in bytes of the area to map. 505f58a9d17SGeoff Levand * c_out: A pointer to receive an allocated struct dma_chunk for this area. 506f58a9d17SGeoff Levand * 507f58a9d17SGeoff Levand * This is the lowest level dma mapping routine, and is the one that will 508f58a9d17SGeoff Levand * make the HV call to add the pages into the io controller address space. 509f58a9d17SGeoff Levand */ 510f58a9d17SGeoff Levand 5116bb5cf10SGeoff Levand static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr, 5126bb5cf10SGeoff Levand unsigned long len, struct dma_chunk **c_out, u64 iopte_flag) 513f58a9d17SGeoff Levand { 514f58a9d17SGeoff Levand int result; 515f58a9d17SGeoff Levand struct dma_chunk *c; 516f58a9d17SGeoff Levand 517a0828cf5SMarkus Elfring c = kzalloc(sizeof(*c), GFP_ATOMIC); 518f58a9d17SGeoff Levand if (!c) { 519f58a9d17SGeoff Levand result = -ENOMEM; 520f58a9d17SGeoff Levand goto fail_alloc; 521f58a9d17SGeoff Levand } 522f58a9d17SGeoff Levand 523f58a9d17SGeoff Levand c->region = r; 524f58a9d17SGeoff Levand c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr); 5256bb5cf10SGeoff Levand c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr); 526f58a9d17SGeoff Levand c->len = len; 527f58a9d17SGeoff Levand 5286bb5cf10SGeoff Levand BUG_ON(iopte_flag != 0xf800000000000000UL); 5296bb5cf10SGeoff Levand result = lv1_map_device_dma_region(c->region->dev->bus_id, 5306bb5cf10SGeoff Levand c->region->dev->dev_id, c->lpar_addr, 5316bb5cf10SGeoff Levand c->bus_addr, c->len, iopte_flag); 532f58a9d17SGeoff Levand if (result) { 533f58a9d17SGeoff Levand DBG("%s:%d: lv1_map_device_dma_region failed: %s\n", 534f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 535f58a9d17SGeoff Levand goto fail_map; 536f58a9d17SGeoff Levand } 537f58a9d17SGeoff Levand 538f58a9d17SGeoff Levand list_add(&c->link, &r->chunk_list.head); 539f58a9d17SGeoff Levand 540f58a9d17SGeoff Levand *c_out = c; 541f58a9d17SGeoff Levand return 0; 542f58a9d17SGeoff Levand 543f58a9d17SGeoff Levand fail_map: 544f58a9d17SGeoff Levand kfree(c); 545f58a9d17SGeoff Levand fail_alloc: 546f58a9d17SGeoff Levand *c_out = NULL; 547f58a9d17SGeoff Levand DBG(" <- %s:%d\n", __func__, __LINE__); 548f58a9d17SGeoff Levand return result; 549f58a9d17SGeoff Levand } 550f58a9d17SGeoff Levand 5516bb5cf10SGeoff Levand static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr, 5526bb5cf10SGeoff Levand unsigned long len, struct dma_chunk **c_out, 5536bb5cf10SGeoff Levand u64 iopte_flag) 5546bb5cf10SGeoff Levand { 5556bb5cf10SGeoff Levand int result; 5566bb5cf10SGeoff Levand struct dma_chunk *c, *last; 5576bb5cf10SGeoff Levand int iopage, pages; 5586bb5cf10SGeoff Levand unsigned long offset; 5596bb5cf10SGeoff Levand 5606bb5cf10SGeoff Levand DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__, 5616bb5cf10SGeoff Levand phys_addr, ps3_mm_phys_to_lpar(phys_addr), len); 562a0828cf5SMarkus Elfring c = kzalloc(sizeof(*c), GFP_ATOMIC); 5636bb5cf10SGeoff Levand if (!c) { 5646bb5cf10SGeoff Levand result = -ENOMEM; 5656bb5cf10SGeoff Levand goto fail_alloc; 5666bb5cf10SGeoff Levand } 5676bb5cf10SGeoff Levand 5686bb5cf10SGeoff Levand c->region = r; 5696bb5cf10SGeoff Levand c->len = len; 5706bb5cf10SGeoff Levand c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr); 5716bb5cf10SGeoff Levand /* allocate IO address */ 5726bb5cf10SGeoff Levand if (list_empty(&r->chunk_list.head)) { 5736bb5cf10SGeoff Levand /* first one */ 5746bb5cf10SGeoff Levand c->bus_addr = r->bus_addr; 5756bb5cf10SGeoff Levand } else { 5766bb5cf10SGeoff Levand /* derive from last bus addr*/ 5776bb5cf10SGeoff Levand last = list_entry(r->chunk_list.head.next, 5786bb5cf10SGeoff Levand struct dma_chunk, link); 5796bb5cf10SGeoff Levand c->bus_addr = last->bus_addr + last->len; 5806bb5cf10SGeoff Levand DBG("%s: last bus=%#lx, len=%#lx\n", __func__, 5816bb5cf10SGeoff Levand last->bus_addr, last->len); 5826bb5cf10SGeoff Levand } 5836bb5cf10SGeoff Levand 5846bb5cf10SGeoff Levand /* FIXME: check whether length exceeds region size */ 5856bb5cf10SGeoff Levand 5866bb5cf10SGeoff Levand /* build ioptes for the area */ 5876bb5cf10SGeoff Levand pages = len >> r->page_size; 5885c949070SStephen Rothwell DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__, 5896bb5cf10SGeoff Levand r->page_size, r->len, pages, iopte_flag); 5906bb5cf10SGeoff Levand for (iopage = 0; iopage < pages; iopage++) { 5916bb5cf10SGeoff Levand offset = (1 << r->page_size) * iopage; 5926bb5cf10SGeoff Levand result = lv1_put_iopte(0, 5936bb5cf10SGeoff Levand c->bus_addr + offset, 5946bb5cf10SGeoff Levand c->lpar_addr + offset, 5956bb5cf10SGeoff Levand r->ioid, 5966bb5cf10SGeoff Levand iopte_flag); 5976bb5cf10SGeoff Levand if (result) { 598f2c2cbccSJoe Perches pr_warn("%s:%d: lv1_put_iopte failed: %s\n", 5997e28060aSGeert Uytterhoeven __func__, __LINE__, ps3_result(result)); 6006bb5cf10SGeoff Levand goto fail_map; 6016bb5cf10SGeoff Levand } 6026bb5cf10SGeoff Levand DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__, 6036bb5cf10SGeoff Levand iopage, c->bus_addr + offset, c->lpar_addr + offset, 6046bb5cf10SGeoff Levand r->ioid); 6056bb5cf10SGeoff Levand } 6066bb5cf10SGeoff Levand 6076bb5cf10SGeoff Levand /* be sure that last allocated one is inserted at head */ 6086bb5cf10SGeoff Levand list_add(&c->link, &r->chunk_list.head); 6096bb5cf10SGeoff Levand 6106bb5cf10SGeoff Levand *c_out = c; 6116bb5cf10SGeoff Levand DBG("%s: end\n", __func__); 6126bb5cf10SGeoff Levand return 0; 6136bb5cf10SGeoff Levand 6146bb5cf10SGeoff Levand fail_map: 6156bb5cf10SGeoff Levand for (iopage--; 0 <= iopage; iopage--) { 6166bb5cf10SGeoff Levand lv1_put_iopte(0, 6176bb5cf10SGeoff Levand c->bus_addr + offset, 6186bb5cf10SGeoff Levand c->lpar_addr + offset, 6196bb5cf10SGeoff Levand r->ioid, 6206bb5cf10SGeoff Levand 0); 6216bb5cf10SGeoff Levand } 6226bb5cf10SGeoff Levand kfree(c); 6236bb5cf10SGeoff Levand fail_alloc: 6246bb5cf10SGeoff Levand *c_out = NULL; 6256bb5cf10SGeoff Levand return result; 6266bb5cf10SGeoff Levand } 6276bb5cf10SGeoff Levand 628f58a9d17SGeoff Levand /** 6296bb5cf10SGeoff Levand * dma_sb_region_create - Create a device dma region. 630f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 631f58a9d17SGeoff Levand * 632f58a9d17SGeoff Levand * This is the lowest level dma region create routine, and is the one that 633f58a9d17SGeoff Levand * will make the HV call to create the region. 634f58a9d17SGeoff Levand */ 635f58a9d17SGeoff Levand 6366bb5cf10SGeoff Levand static int dma_sb_region_create(struct ps3_dma_region *r) 637f58a9d17SGeoff Levand { 638f58a9d17SGeoff Levand int result; 639b17b3df1SStephen Rothwell u64 bus_addr; 640f58a9d17SGeoff Levand 64162d80749SGeoff Levand DBG(" -> %s:%d:\n", __func__, __LINE__); 6426bb5cf10SGeoff Levand 6436bb5cf10SGeoff Levand BUG_ON(!r); 6446bb5cf10SGeoff Levand 6456bb5cf10SGeoff Levand if (!r->dev->bus_id) { 6465c949070SStephen Rothwell pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__, 6476bb5cf10SGeoff Levand r->dev->bus_id, r->dev->dev_id); 6486bb5cf10SGeoff Levand return 0; 6496bb5cf10SGeoff Levand } 6506bb5cf10SGeoff Levand 6516bb5cf10SGeoff Levand DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__, 6526bb5cf10SGeoff Levand __LINE__, r->len, r->page_size, r->offset); 6536bb5cf10SGeoff Levand 6546bb5cf10SGeoff Levand BUG_ON(!r->len); 6556bb5cf10SGeoff Levand BUG_ON(!r->page_size); 6566bb5cf10SGeoff Levand BUG_ON(!r->region_ops); 6576bb5cf10SGeoff Levand 658f58a9d17SGeoff Levand INIT_LIST_HEAD(&r->chunk_list.head); 659f58a9d17SGeoff Levand spin_lock_init(&r->chunk_list.lock); 660f58a9d17SGeoff Levand 6616bb5cf10SGeoff Levand result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id, 6626bb5cf10SGeoff Levand roundup_pow_of_two(r->len), r->page_size, r->region_type, 663b17b3df1SStephen Rothwell &bus_addr); 664b17b3df1SStephen Rothwell r->bus_addr = bus_addr; 665f58a9d17SGeoff Levand 666f58a9d17SGeoff Levand if (result) { 667f58a9d17SGeoff Levand DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n", 668f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 669f58a9d17SGeoff Levand r->len = r->bus_addr = 0; 670f58a9d17SGeoff Levand } 671f58a9d17SGeoff Levand 672f58a9d17SGeoff Levand return result; 673f58a9d17SGeoff Levand } 674f58a9d17SGeoff Levand 6756bb5cf10SGeoff Levand static int dma_ioc0_region_create(struct ps3_dma_region *r) 6766bb5cf10SGeoff Levand { 6776bb5cf10SGeoff Levand int result; 678b17b3df1SStephen Rothwell u64 bus_addr; 6796bb5cf10SGeoff Levand 6806bb5cf10SGeoff Levand INIT_LIST_HEAD(&r->chunk_list.head); 6816bb5cf10SGeoff Levand spin_lock_init(&r->chunk_list.lock); 6826bb5cf10SGeoff Levand 6836bb5cf10SGeoff Levand result = lv1_allocate_io_segment(0, 6846bb5cf10SGeoff Levand r->len, 6856bb5cf10SGeoff Levand r->page_size, 686b17b3df1SStephen Rothwell &bus_addr); 687b17b3df1SStephen Rothwell r->bus_addr = bus_addr; 6886bb5cf10SGeoff Levand if (result) { 6896bb5cf10SGeoff Levand DBG("%s:%d: lv1_allocate_io_segment failed: %s\n", 6906bb5cf10SGeoff Levand __func__, __LINE__, ps3_result(result)); 6916bb5cf10SGeoff Levand r->len = r->bus_addr = 0; 6926bb5cf10SGeoff Levand } 6936bb5cf10SGeoff Levand DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__, 6946bb5cf10SGeoff Levand r->len, r->page_size, r->bus_addr); 6956bb5cf10SGeoff Levand return result; 6966bb5cf10SGeoff Levand } 6976bb5cf10SGeoff Levand 698f58a9d17SGeoff Levand /** 699f58a9d17SGeoff Levand * dma_region_free - Free a device dma region. 700f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 701f58a9d17SGeoff Levand * 702f58a9d17SGeoff Levand * This is the lowest level dma region free routine, and is the one that 703f58a9d17SGeoff Levand * will make the HV call to free the region. 704f58a9d17SGeoff Levand */ 705f58a9d17SGeoff Levand 7066bb5cf10SGeoff Levand static int dma_sb_region_free(struct ps3_dma_region *r) 707f58a9d17SGeoff Levand { 708f58a9d17SGeoff Levand int result; 709f58a9d17SGeoff Levand struct dma_chunk *c; 710f58a9d17SGeoff Levand struct dma_chunk *tmp; 711f58a9d17SGeoff Levand 7126bb5cf10SGeoff Levand BUG_ON(!r); 7136bb5cf10SGeoff Levand 7146bb5cf10SGeoff Levand if (!r->dev->bus_id) { 7155c949070SStephen Rothwell pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__, 7166bb5cf10SGeoff Levand r->dev->bus_id, r->dev->dev_id); 7176bb5cf10SGeoff Levand return 0; 718f58a9d17SGeoff Levand } 719f58a9d17SGeoff Levand 7206bb5cf10SGeoff Levand list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) { 7216bb5cf10SGeoff Levand list_del(&c->link); 7226bb5cf10SGeoff Levand dma_sb_free_chunk(c); 7236bb5cf10SGeoff Levand } 7246bb5cf10SGeoff Levand 7256bb5cf10SGeoff Levand result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id, 726f58a9d17SGeoff Levand r->bus_addr); 727f58a9d17SGeoff Levand 728f58a9d17SGeoff Levand if (result) 729f58a9d17SGeoff Levand DBG("%s:%d: lv1_free_device_dma_region failed: %s\n", 730f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 731f58a9d17SGeoff Levand 7326bb5cf10SGeoff Levand r->bus_addr = 0; 7336bb5cf10SGeoff Levand 7346bb5cf10SGeoff Levand return result; 7356bb5cf10SGeoff Levand } 7366bb5cf10SGeoff Levand 7376bb5cf10SGeoff Levand static int dma_ioc0_region_free(struct ps3_dma_region *r) 7386bb5cf10SGeoff Levand { 7396bb5cf10SGeoff Levand int result; 7406bb5cf10SGeoff Levand struct dma_chunk *c, *n; 7416bb5cf10SGeoff Levand 7426bb5cf10SGeoff Levand DBG("%s: start\n", __func__); 7436bb5cf10SGeoff Levand list_for_each_entry_safe(c, n, &r->chunk_list.head, link) { 7446bb5cf10SGeoff Levand list_del(&c->link); 7456bb5cf10SGeoff Levand dma_ioc0_free_chunk(c); 7466bb5cf10SGeoff Levand } 7476bb5cf10SGeoff Levand 7486bb5cf10SGeoff Levand result = lv1_release_io_segment(0, r->bus_addr); 7496bb5cf10SGeoff Levand 7506bb5cf10SGeoff Levand if (result) 7516bb5cf10SGeoff Levand DBG("%s:%d: lv1_free_device_dma_region failed: %s\n", 7526bb5cf10SGeoff Levand __func__, __LINE__, ps3_result(result)); 7536bb5cf10SGeoff Levand 7546bb5cf10SGeoff Levand r->bus_addr = 0; 7556bb5cf10SGeoff Levand DBG("%s: end\n", __func__); 756f58a9d17SGeoff Levand 757f58a9d17SGeoff Levand return result; 758f58a9d17SGeoff Levand } 759f58a9d17SGeoff Levand 760f58a9d17SGeoff Levand /** 7616bb5cf10SGeoff Levand * dma_sb_map_area - Map an area of memory into a device dma region. 762f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 763f58a9d17SGeoff Levand * @virt_addr: Starting virtual address of the area to map. 764f58a9d17SGeoff Levand * @len: Length in bytes of the area to map. 765f58a9d17SGeoff Levand * @bus_addr: A pointer to return the starting ioc bus address of the area to 766f58a9d17SGeoff Levand * map. 767f58a9d17SGeoff Levand * 768f58a9d17SGeoff Levand * This is the common dma mapping routine. 769f58a9d17SGeoff Levand */ 770f58a9d17SGeoff Levand 7716bb5cf10SGeoff Levand static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr, 772494fd07aSStephen Rothwell unsigned long len, dma_addr_t *bus_addr, 7736bb5cf10SGeoff Levand u64 iopte_flag) 774f58a9d17SGeoff Levand { 775f58a9d17SGeoff Levand int result; 776f58a9d17SGeoff Levand unsigned long flags; 777f58a9d17SGeoff Levand struct dma_chunk *c; 778f58a9d17SGeoff Levand unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr) 779f58a9d17SGeoff Levand : virt_addr; 780e96d904eSChristophe Leroy unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size); 781b7115316SChristophe Leroy unsigned long aligned_len = ALIGN(len + phys_addr - aligned_phys, 7826bb5cf10SGeoff Levand 1 << r->page_size); 7836bb5cf10SGeoff Levand *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr)); 784f58a9d17SGeoff Levand 785f58a9d17SGeoff Levand if (!USE_DYNAMIC_DMA) { 786f58a9d17SGeoff Levand unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr); 787f58a9d17SGeoff Levand DBG(" -> %s:%d\n", __func__, __LINE__); 788f58a9d17SGeoff Levand DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__, 789f58a9d17SGeoff Levand virt_addr); 790f58a9d17SGeoff Levand DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__, 791f58a9d17SGeoff Levand phys_addr); 792f58a9d17SGeoff Levand DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__, 793f58a9d17SGeoff Levand lpar_addr); 794f58a9d17SGeoff Levand DBG("%s:%d len %lxh\n", __func__, __LINE__, len); 795494fd07aSStephen Rothwell DBG("%s:%d bus_addr %llxh (%lxh)\n", __func__, __LINE__, 796f58a9d17SGeoff Levand *bus_addr, len); 797f58a9d17SGeoff Levand } 798f58a9d17SGeoff Levand 799f58a9d17SGeoff Levand spin_lock_irqsave(&r->chunk_list.lock, flags); 800f58a9d17SGeoff Levand c = dma_find_chunk(r, *bus_addr, len); 801f58a9d17SGeoff Levand 802f58a9d17SGeoff Levand if (c) { 8036bb5cf10SGeoff Levand DBG("%s:%d: reusing mapped chunk", __func__, __LINE__); 8046bb5cf10SGeoff Levand dma_dump_chunk(c); 805f58a9d17SGeoff Levand c->usage_count++; 806f58a9d17SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 807f58a9d17SGeoff Levand return 0; 808f58a9d17SGeoff Levand } 809f58a9d17SGeoff Levand 8106bb5cf10SGeoff Levand result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag); 811f58a9d17SGeoff Levand 812f58a9d17SGeoff Levand if (result) { 813f58a9d17SGeoff Levand *bus_addr = 0; 8146bb5cf10SGeoff Levand DBG("%s:%d: dma_sb_map_pages failed (%d)\n", 815f58a9d17SGeoff Levand __func__, __LINE__, result); 816f58a9d17SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 817f58a9d17SGeoff Levand return result; 818f58a9d17SGeoff Levand } 819f58a9d17SGeoff Levand 820f58a9d17SGeoff Levand c->usage_count = 1; 821f58a9d17SGeoff Levand 822f58a9d17SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 823f58a9d17SGeoff Levand return result; 824f58a9d17SGeoff Levand } 825f58a9d17SGeoff Levand 8266bb5cf10SGeoff Levand static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr, 827494fd07aSStephen Rothwell unsigned long len, dma_addr_t *bus_addr, 8286bb5cf10SGeoff Levand u64 iopte_flag) 8296bb5cf10SGeoff Levand { 8306bb5cf10SGeoff Levand int result; 8316bb5cf10SGeoff Levand unsigned long flags; 8326bb5cf10SGeoff Levand struct dma_chunk *c; 8336bb5cf10SGeoff Levand unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr) 8346bb5cf10SGeoff Levand : virt_addr; 835e96d904eSChristophe Leroy unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size); 836b7115316SChristophe Leroy unsigned long aligned_len = ALIGN(len + phys_addr - aligned_phys, 8376bb5cf10SGeoff Levand 1 << r->page_size); 8386bb5cf10SGeoff Levand 8396bb5cf10SGeoff Levand DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__, 8406bb5cf10SGeoff Levand virt_addr, len); 8416bb5cf10SGeoff Levand DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__, 8426bb5cf10SGeoff Levand phys_addr, aligned_phys, aligned_len); 8436bb5cf10SGeoff Levand 8446bb5cf10SGeoff Levand spin_lock_irqsave(&r->chunk_list.lock, flags); 8456bb5cf10SGeoff Levand c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len); 8466bb5cf10SGeoff Levand 8476bb5cf10SGeoff Levand if (c) { 8486bb5cf10SGeoff Levand /* FIXME */ 8496bb5cf10SGeoff Levand BUG(); 8506bb5cf10SGeoff Levand *bus_addr = c->bus_addr + phys_addr - aligned_phys; 8516bb5cf10SGeoff Levand c->usage_count++; 8526bb5cf10SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 8536bb5cf10SGeoff Levand return 0; 8546bb5cf10SGeoff Levand } 8556bb5cf10SGeoff Levand 8566bb5cf10SGeoff Levand result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c, 8576bb5cf10SGeoff Levand iopte_flag); 8586bb5cf10SGeoff Levand 8596bb5cf10SGeoff Levand if (result) { 8606bb5cf10SGeoff Levand *bus_addr = 0; 8616bb5cf10SGeoff Levand DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n", 8626bb5cf10SGeoff Levand __func__, __LINE__, result); 8636bb5cf10SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 8646bb5cf10SGeoff Levand return result; 8656bb5cf10SGeoff Levand } 8666bb5cf10SGeoff Levand *bus_addr = c->bus_addr + phys_addr - aligned_phys; 867494fd07aSStephen Rothwell DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__, 8686bb5cf10SGeoff Levand virt_addr, phys_addr, aligned_phys, *bus_addr); 8696bb5cf10SGeoff Levand c->usage_count = 1; 8706bb5cf10SGeoff Levand 8716bb5cf10SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 8726bb5cf10SGeoff Levand return result; 8736bb5cf10SGeoff Levand } 8746bb5cf10SGeoff Levand 875f58a9d17SGeoff Levand /** 8766bb5cf10SGeoff Levand * dma_sb_unmap_area - Unmap an area of memory from a device dma region. 877f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 878f58a9d17SGeoff Levand * @bus_addr: The starting ioc bus address of the area to unmap. 879f58a9d17SGeoff Levand * @len: Length in bytes of the area to unmap. 880f58a9d17SGeoff Levand * 881f58a9d17SGeoff Levand * This is the common dma unmap routine. 882f58a9d17SGeoff Levand */ 883f58a9d17SGeoff Levand 884494fd07aSStephen Rothwell static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr, 885f58a9d17SGeoff Levand unsigned long len) 886f58a9d17SGeoff Levand { 887f58a9d17SGeoff Levand unsigned long flags; 888f58a9d17SGeoff Levand struct dma_chunk *c; 889f58a9d17SGeoff Levand 890f58a9d17SGeoff Levand spin_lock_irqsave(&r->chunk_list.lock, flags); 891f58a9d17SGeoff Levand c = dma_find_chunk(r, bus_addr, len); 892f58a9d17SGeoff Levand 893f58a9d17SGeoff Levand if (!c) { 894e96d904eSChristophe Leroy unsigned long aligned_bus = ALIGN_DOWN(bus_addr, 895f58a9d17SGeoff Levand 1 << r->page_size); 896b7115316SChristophe Leroy unsigned long aligned_len = ALIGN(len + bus_addr 8976bb5cf10SGeoff Levand - aligned_bus, 1 << r->page_size); 898494fd07aSStephen Rothwell DBG("%s:%d: not found: bus_addr %llxh\n", 899f58a9d17SGeoff Levand __func__, __LINE__, bus_addr); 900f58a9d17SGeoff Levand DBG("%s:%d: not found: len %lxh\n", 901f58a9d17SGeoff Levand __func__, __LINE__, len); 902f58a9d17SGeoff Levand DBG("%s:%d: not found: aligned_bus %lxh\n", 903f58a9d17SGeoff Levand __func__, __LINE__, aligned_bus); 904f58a9d17SGeoff Levand DBG("%s:%d: not found: aligned_len %lxh\n", 905f58a9d17SGeoff Levand __func__, __LINE__, aligned_len); 906f58a9d17SGeoff Levand BUG(); 907f58a9d17SGeoff Levand } 908f58a9d17SGeoff Levand 909f58a9d17SGeoff Levand c->usage_count--; 910f58a9d17SGeoff Levand 911f58a9d17SGeoff Levand if (!c->usage_count) { 912f58a9d17SGeoff Levand list_del(&c->link); 9136bb5cf10SGeoff Levand dma_sb_free_chunk(c); 914f58a9d17SGeoff Levand } 915f58a9d17SGeoff Levand 916f58a9d17SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 917f58a9d17SGeoff Levand return 0; 918f58a9d17SGeoff Levand } 919f58a9d17SGeoff Levand 92032f44a12SGeert Uytterhoeven static int dma_ioc0_unmap_area(struct ps3_dma_region *r, 921494fd07aSStephen Rothwell dma_addr_t bus_addr, unsigned long len) 9226bb5cf10SGeoff Levand { 9236bb5cf10SGeoff Levand unsigned long flags; 9246bb5cf10SGeoff Levand struct dma_chunk *c; 9256bb5cf10SGeoff Levand 926494fd07aSStephen Rothwell DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len); 9276bb5cf10SGeoff Levand spin_lock_irqsave(&r->chunk_list.lock, flags); 9286bb5cf10SGeoff Levand c = dma_find_chunk(r, bus_addr, len); 9296bb5cf10SGeoff Levand 9306bb5cf10SGeoff Levand if (!c) { 931e96d904eSChristophe Leroy unsigned long aligned_bus = ALIGN_DOWN(bus_addr, 9326bb5cf10SGeoff Levand 1 << r->page_size); 933b7115316SChristophe Leroy unsigned long aligned_len = ALIGN(len + bus_addr 9346bb5cf10SGeoff Levand - aligned_bus, 9356bb5cf10SGeoff Levand 1 << r->page_size); 936494fd07aSStephen Rothwell DBG("%s:%d: not found: bus_addr %llxh\n", 9376bb5cf10SGeoff Levand __func__, __LINE__, bus_addr); 9386bb5cf10SGeoff Levand DBG("%s:%d: not found: len %lxh\n", 9396bb5cf10SGeoff Levand __func__, __LINE__, len); 9406bb5cf10SGeoff Levand DBG("%s:%d: not found: aligned_bus %lxh\n", 9416bb5cf10SGeoff Levand __func__, __LINE__, aligned_bus); 9426bb5cf10SGeoff Levand DBG("%s:%d: not found: aligned_len %lxh\n", 9436bb5cf10SGeoff Levand __func__, __LINE__, aligned_len); 9446bb5cf10SGeoff Levand BUG(); 9456bb5cf10SGeoff Levand } 9466bb5cf10SGeoff Levand 9476bb5cf10SGeoff Levand c->usage_count--; 9486bb5cf10SGeoff Levand 9496bb5cf10SGeoff Levand if (!c->usage_count) { 9506bb5cf10SGeoff Levand list_del(&c->link); 9516bb5cf10SGeoff Levand dma_ioc0_free_chunk(c); 9526bb5cf10SGeoff Levand } 9536bb5cf10SGeoff Levand 9546bb5cf10SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 9556bb5cf10SGeoff Levand DBG("%s: end\n", __func__); 9566bb5cf10SGeoff Levand return 0; 9576bb5cf10SGeoff Levand } 9586bb5cf10SGeoff Levand 959f58a9d17SGeoff Levand /** 9606bb5cf10SGeoff Levand * dma_sb_region_create_linear - Setup a linear dma mapping for a device. 961f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 962f58a9d17SGeoff Levand * 963f58a9d17SGeoff Levand * This routine creates an HV dma region for the device and maps all available 964f58a9d17SGeoff Levand * ram into the io controller bus address space. 965f58a9d17SGeoff Levand */ 966f58a9d17SGeoff Levand 9676bb5cf10SGeoff Levand static int dma_sb_region_create_linear(struct ps3_dma_region *r) 968f58a9d17SGeoff Levand { 969f58a9d17SGeoff Levand int result; 970494fd07aSStephen Rothwell unsigned long virt_addr, len; 971494fd07aSStephen Rothwell dma_addr_t tmp; 972f58a9d17SGeoff Levand 9736bb5cf10SGeoff Levand if (r->len > 16*1024*1024) { /* FIXME: need proper fix */ 974f58a9d17SGeoff Levand /* force 16M dma pages for linear mapping */ 975f58a9d17SGeoff Levand if (r->page_size != PS3_DMA_16M) { 976f58a9d17SGeoff Levand pr_info("%s:%d: forcing 16M pages for linear map\n", 977f58a9d17SGeoff Levand __func__, __LINE__); 978f58a9d17SGeoff Levand r->page_size = PS3_DMA_16M; 979b7115316SChristophe Leroy r->len = ALIGN(r->len, 1 << r->page_size); 9806bb5cf10SGeoff Levand } 981f58a9d17SGeoff Levand } 982f58a9d17SGeoff Levand 9836bb5cf10SGeoff Levand result = dma_sb_region_create(r); 984f58a9d17SGeoff Levand BUG_ON(result); 985f58a9d17SGeoff Levand 9866bb5cf10SGeoff Levand if (r->offset < map.rm.size) { 9876bb5cf10SGeoff Levand /* Map (part of) 1st RAM chunk */ 9886bb5cf10SGeoff Levand virt_addr = map.rm.base + r->offset; 9896bb5cf10SGeoff Levand len = map.rm.size - r->offset; 9906bb5cf10SGeoff Levand if (len > r->len) 9916bb5cf10SGeoff Levand len = r->len; 9926bb5cf10SGeoff Levand result = dma_sb_map_area(r, virt_addr, len, &tmp, 9935c6fc8dbSGeert Uytterhoeven CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW | 9945c6fc8dbSGeert Uytterhoeven CBE_IOPTE_M); 995f58a9d17SGeoff Levand BUG_ON(result); 9966bb5cf10SGeoff Levand } 997f58a9d17SGeoff Levand 9986bb5cf10SGeoff Levand if (r->offset + r->len > map.rm.size) { 9996bb5cf10SGeoff Levand /* Map (part of) 2nd RAM chunk */ 1000a628df1eSGeoff Levand virt_addr = map.rm.size; 10016bb5cf10SGeoff Levand len = r->len; 10026bb5cf10SGeoff Levand if (r->offset >= map.rm.size) 10036bb5cf10SGeoff Levand virt_addr += r->offset - map.rm.size; 1004f58a9d17SGeoff Levand else 10056bb5cf10SGeoff Levand len -= map.rm.size - r->offset; 10066bb5cf10SGeoff Levand result = dma_sb_map_area(r, virt_addr, len, &tmp, 10075c6fc8dbSGeert Uytterhoeven CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW | 10085c6fc8dbSGeert Uytterhoeven CBE_IOPTE_M); 1009f58a9d17SGeoff Levand BUG_ON(result); 10106bb5cf10SGeoff Levand } 1011f58a9d17SGeoff Levand 1012f58a9d17SGeoff Levand return result; 1013f58a9d17SGeoff Levand } 1014f58a9d17SGeoff Levand 1015f58a9d17SGeoff Levand /** 10166bb5cf10SGeoff Levand * dma_sb_region_free_linear - Free a linear dma mapping for a device. 1017f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 1018f58a9d17SGeoff Levand * 1019f58a9d17SGeoff Levand * This routine will unmap all mapped areas and free the HV dma region. 1020f58a9d17SGeoff Levand */ 1021f58a9d17SGeoff Levand 10226bb5cf10SGeoff Levand static int dma_sb_region_free_linear(struct ps3_dma_region *r) 1023f58a9d17SGeoff Levand { 1024f58a9d17SGeoff Levand int result; 1025494fd07aSStephen Rothwell dma_addr_t bus_addr; 1026494fd07aSStephen Rothwell unsigned long len, lpar_addr; 1027f58a9d17SGeoff Levand 10286bb5cf10SGeoff Levand if (r->offset < map.rm.size) { 10296bb5cf10SGeoff Levand /* Unmap (part of) 1st RAM chunk */ 10306bb5cf10SGeoff Levand lpar_addr = map.rm.base + r->offset; 10316bb5cf10SGeoff Levand len = map.rm.size - r->offset; 10326bb5cf10SGeoff Levand if (len > r->len) 10336bb5cf10SGeoff Levand len = r->len; 10346bb5cf10SGeoff Levand bus_addr = dma_sb_lpar_to_bus(r, lpar_addr); 10356bb5cf10SGeoff Levand result = dma_sb_unmap_area(r, bus_addr, len); 1036f58a9d17SGeoff Levand BUG_ON(result); 10376bb5cf10SGeoff Levand } 1038f58a9d17SGeoff Levand 10396bb5cf10SGeoff Levand if (r->offset + r->len > map.rm.size) { 10406bb5cf10SGeoff Levand /* Unmap (part of) 2nd RAM chunk */ 10416bb5cf10SGeoff Levand lpar_addr = map.r1.base; 10426bb5cf10SGeoff Levand len = r->len; 10436bb5cf10SGeoff Levand if (r->offset >= map.rm.size) 10446bb5cf10SGeoff Levand lpar_addr += r->offset - map.rm.size; 10456bb5cf10SGeoff Levand else 10466bb5cf10SGeoff Levand len -= map.rm.size - r->offset; 10476bb5cf10SGeoff Levand bus_addr = dma_sb_lpar_to_bus(r, lpar_addr); 10486bb5cf10SGeoff Levand result = dma_sb_unmap_area(r, bus_addr, len); 1049f58a9d17SGeoff Levand BUG_ON(result); 10506bb5cf10SGeoff Levand } 1051f58a9d17SGeoff Levand 10526bb5cf10SGeoff Levand result = dma_sb_region_free(r); 1053f58a9d17SGeoff Levand BUG_ON(result); 1054f58a9d17SGeoff Levand 1055f58a9d17SGeoff Levand return result; 1056f58a9d17SGeoff Levand } 1057f58a9d17SGeoff Levand 1058f58a9d17SGeoff Levand /** 10596bb5cf10SGeoff Levand * dma_sb_map_area_linear - Map an area of memory into a device dma region. 1060f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 1061f58a9d17SGeoff Levand * @virt_addr: Starting virtual address of the area to map. 1062f58a9d17SGeoff Levand * @len: Length in bytes of the area to map. 1063f58a9d17SGeoff Levand * @bus_addr: A pointer to return the starting ioc bus address of the area to 1064f58a9d17SGeoff Levand * map. 1065f58a9d17SGeoff Levand * 10666bb5cf10SGeoff Levand * This routine just returns the corresponding bus address. Actual mapping 1067f58a9d17SGeoff Levand * occurs in dma_region_create_linear(). 1068f58a9d17SGeoff Levand */ 1069f58a9d17SGeoff Levand 10706bb5cf10SGeoff Levand static int dma_sb_map_area_linear(struct ps3_dma_region *r, 1071494fd07aSStephen Rothwell unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr, 10726bb5cf10SGeoff Levand u64 iopte_flag) 1073f58a9d17SGeoff Levand { 1074f58a9d17SGeoff Levand unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr) 1075f58a9d17SGeoff Levand : virt_addr; 10766bb5cf10SGeoff Levand *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr)); 1077f58a9d17SGeoff Levand return 0; 1078f58a9d17SGeoff Levand } 1079f58a9d17SGeoff Levand 1080f58a9d17SGeoff Levand /** 1081f58a9d17SGeoff Levand * dma_unmap_area_linear - Unmap an area of memory from a device dma region. 1082f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 1083f58a9d17SGeoff Levand * @bus_addr: The starting ioc bus address of the area to unmap. 1084f58a9d17SGeoff Levand * @len: Length in bytes of the area to unmap. 1085f58a9d17SGeoff Levand * 10866bb5cf10SGeoff Levand * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear(). 1087f58a9d17SGeoff Levand */ 1088f58a9d17SGeoff Levand 10896bb5cf10SGeoff Levand static int dma_sb_unmap_area_linear(struct ps3_dma_region *r, 1090494fd07aSStephen Rothwell dma_addr_t bus_addr, unsigned long len) 1091f58a9d17SGeoff Levand { 1092f58a9d17SGeoff Levand return 0; 10936bb5cf10SGeoff Levand }; 10946bb5cf10SGeoff Levand 10956bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = { 10966bb5cf10SGeoff Levand .create = dma_sb_region_create, 10976bb5cf10SGeoff Levand .free = dma_sb_region_free, 10986bb5cf10SGeoff Levand .map = dma_sb_map_area, 10996bb5cf10SGeoff Levand .unmap = dma_sb_unmap_area 11006bb5cf10SGeoff Levand }; 11016bb5cf10SGeoff Levand 11026bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = { 11036bb5cf10SGeoff Levand .create = dma_sb_region_create_linear, 11046bb5cf10SGeoff Levand .free = dma_sb_region_free_linear, 11056bb5cf10SGeoff Levand .map = dma_sb_map_area_linear, 11066bb5cf10SGeoff Levand .unmap = dma_sb_unmap_area_linear 11076bb5cf10SGeoff Levand }; 11086bb5cf10SGeoff Levand 11096bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = { 11106bb5cf10SGeoff Levand .create = dma_ioc0_region_create, 11116bb5cf10SGeoff Levand .free = dma_ioc0_region_free, 11126bb5cf10SGeoff Levand .map = dma_ioc0_map_area, 11136bb5cf10SGeoff Levand .unmap = dma_ioc0_unmap_area 11146bb5cf10SGeoff Levand }; 11156bb5cf10SGeoff Levand 11166bb5cf10SGeoff Levand int ps3_dma_region_init(struct ps3_system_bus_device *dev, 11176bb5cf10SGeoff Levand struct ps3_dma_region *r, enum ps3_dma_page_size page_size, 11186bb5cf10SGeoff Levand enum ps3_dma_region_type region_type, void *addr, unsigned long len) 11196bb5cf10SGeoff Levand { 11206bb5cf10SGeoff Levand unsigned long lpar_addr; 11216bb5cf10SGeoff Levand 11226bb5cf10SGeoff Levand lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0; 11236bb5cf10SGeoff Levand 11246bb5cf10SGeoff Levand r->dev = dev; 11256bb5cf10SGeoff Levand r->page_size = page_size; 11266bb5cf10SGeoff Levand r->region_type = region_type; 11276bb5cf10SGeoff Levand r->offset = lpar_addr; 11286bb5cf10SGeoff Levand if (r->offset >= map.rm.size) 11296bb5cf10SGeoff Levand r->offset -= map.r1.offset; 1130b7115316SChristophe Leroy r->len = len ? len : ALIGN(map.total, 1 << r->page_size); 11316bb5cf10SGeoff Levand 11326bb5cf10SGeoff Levand switch (dev->dev_type) { 11336bb5cf10SGeoff Levand case PS3_DEVICE_TYPE_SB: 11346bb5cf10SGeoff Levand r->region_ops = (USE_DYNAMIC_DMA) 11356bb5cf10SGeoff Levand ? &ps3_dma_sb_region_ops 11366bb5cf10SGeoff Levand : &ps3_dma_sb_region_linear_ops; 11376bb5cf10SGeoff Levand break; 11386bb5cf10SGeoff Levand case PS3_DEVICE_TYPE_IOC0: 11396bb5cf10SGeoff Levand r->region_ops = &ps3_dma_ioc0_region_ops; 11406bb5cf10SGeoff Levand break; 11416bb5cf10SGeoff Levand default: 11426bb5cf10SGeoff Levand BUG(); 11436bb5cf10SGeoff Levand return -EINVAL; 1144f58a9d17SGeoff Levand } 11456bb5cf10SGeoff Levand return 0; 11466bb5cf10SGeoff Levand } 11476bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_init); 1148f58a9d17SGeoff Levand 1149f58a9d17SGeoff Levand int ps3_dma_region_create(struct ps3_dma_region *r) 1150f58a9d17SGeoff Levand { 11516bb5cf10SGeoff Levand BUG_ON(!r); 11526bb5cf10SGeoff Levand BUG_ON(!r->region_ops); 11536bb5cf10SGeoff Levand BUG_ON(!r->region_ops->create); 11546bb5cf10SGeoff Levand return r->region_ops->create(r); 1155f58a9d17SGeoff Levand } 11566bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_create); 1157f58a9d17SGeoff Levand 1158f58a9d17SGeoff Levand int ps3_dma_region_free(struct ps3_dma_region *r) 1159f58a9d17SGeoff Levand { 11606bb5cf10SGeoff Levand BUG_ON(!r); 11616bb5cf10SGeoff Levand BUG_ON(!r->region_ops); 11626bb5cf10SGeoff Levand BUG_ON(!r->region_ops->free); 11636bb5cf10SGeoff Levand return r->region_ops->free(r); 1164f58a9d17SGeoff Levand } 11656bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_free); 1166f58a9d17SGeoff Levand 1167f58a9d17SGeoff Levand int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr, 1168494fd07aSStephen Rothwell unsigned long len, dma_addr_t *bus_addr, 11696bb5cf10SGeoff Levand u64 iopte_flag) 1170f58a9d17SGeoff Levand { 11716bb5cf10SGeoff Levand return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag); 1172f58a9d17SGeoff Levand } 1173f58a9d17SGeoff Levand 1174494fd07aSStephen Rothwell int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr, 1175f58a9d17SGeoff Levand unsigned long len) 1176f58a9d17SGeoff Levand { 11776bb5cf10SGeoff Levand return r->region_ops->unmap(r, bus_addr, len); 1178f58a9d17SGeoff Levand } 1179f58a9d17SGeoff Levand 1180f58a9d17SGeoff Levand /*============================================================================*/ 1181f58a9d17SGeoff Levand /* system startup routines */ 1182f58a9d17SGeoff Levand /*============================================================================*/ 1183f58a9d17SGeoff Levand 1184f58a9d17SGeoff Levand /** 1185f58a9d17SGeoff Levand * ps3_mm_init - initialize the address space state variables 1186f58a9d17SGeoff Levand */ 1187f58a9d17SGeoff Levand 1188f58a9d17SGeoff Levand void __init ps3_mm_init(void) 1189f58a9d17SGeoff Levand { 1190f58a9d17SGeoff Levand int result; 1191f58a9d17SGeoff Levand 1192f58a9d17SGeoff Levand DBG(" -> %s:%d\n", __func__, __LINE__); 1193f58a9d17SGeoff Levand 1194f58a9d17SGeoff Levand result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size, 1195f58a9d17SGeoff Levand &map.total); 1196f58a9d17SGeoff Levand 1197f58a9d17SGeoff Levand if (result) 1198f58a9d17SGeoff Levand panic("ps3_repository_read_mm_info() failed"); 1199f58a9d17SGeoff Levand 1200f58a9d17SGeoff Levand map.rm.offset = map.rm.base; 1201f58a9d17SGeoff Levand map.vas_id = map.htab_size = 0; 1202f58a9d17SGeoff Levand 1203f58a9d17SGeoff Levand /* this implementation assumes map.rm.base is zero */ 1204f58a9d17SGeoff Levand 1205f58a9d17SGeoff Levand BUG_ON(map.rm.base); 1206f58a9d17SGeoff Levand BUG_ON(!map.rm.size); 1207f58a9d17SGeoff Levand 12081e755c09SAndre Heider /* Check if we got the highmem region from an earlier boot step */ 1209f58a9d17SGeoff Levand 12105ae74630SGeoff Levand if (ps3_mm_get_repository_highmem(&map.r1)) { 12115ae74630SGeoff Levand result = ps3_mm_region_create(&map.r1, map.total - map.rm.size); 12125ae74630SGeoff Levand 12135ae74630SGeoff Levand if (!result) 12145ae74630SGeoff Levand ps3_mm_set_repository_highmem(&map.r1); 12155ae74630SGeoff Levand } 1216f58a9d17SGeoff Levand 12176bb5cf10SGeoff Levand /* correct map.total for the real total amount of memory we use */ 12186bb5cf10SGeoff Levand map.total = map.rm.size + map.r1.size; 12196bb5cf10SGeoff Levand 12208ac5fd11SHector Martin if (!map.r1.size) { 12218ac5fd11SHector Martin DBG("%s:%d: No highmem region found\n", __func__, __LINE__); 12228ac5fd11SHector Martin } else { 12238ac5fd11SHector Martin DBG("%s:%d: Adding highmem region: %llxh %llxh\n", 12248ac5fd11SHector Martin __func__, __LINE__, map.rm.size, 12258ac5fd11SHector Martin map.total - map.rm.size); 12268ac5fd11SHector Martin memblock_add(map.rm.size, map.total - map.rm.size); 12278ac5fd11SHector Martin } 12288ac5fd11SHector Martin 1229f58a9d17SGeoff Levand DBG(" <- %s:%d\n", __func__, __LINE__); 1230f58a9d17SGeoff Levand } 1231f58a9d17SGeoff Levand 1232f58a9d17SGeoff Levand /** 1233f58a9d17SGeoff Levand * ps3_mm_shutdown - final cleanup of address space 1234f58a9d17SGeoff Levand */ 1235f58a9d17SGeoff Levand 1236f58a9d17SGeoff Levand void ps3_mm_shutdown(void) 1237f58a9d17SGeoff Levand { 1238f58a9d17SGeoff Levand ps3_mm_region_destroy(&map.r1); 1239f58a9d17SGeoff Levand } 1240