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 99733862eSGeoff Levand #include <linux/dma-mapping.h> 10f58a9d17SGeoff Levand #include <linux/kernel.h> 114b16f8e2SPaul Gortmaker #include <linux/export.h> 1295f72d1eSYinghai Lu #include <linux/memblock.h> 135a0e3ad6STejun Heo #include <linux/slab.h> 14f58a9d17SGeoff Levand 159413c883SGeert Uytterhoeven #include <asm/cell-regs.h> 16e22ba7e3SArnd Bergmann #include <asm/firmware.h> 17d9b2b2a2SDavid S. Miller #include <asm/prom.h> 18f58a9d17SGeoff Levand #include <asm/udbg.h> 19f58a9d17SGeoff Levand #include <asm/lv1call.h> 20ae3a197eSDavid Howells #include <asm/setup.h> 21f58a9d17SGeoff Levand 22f58a9d17SGeoff Levand #include "platform.h" 23f58a9d17SGeoff Levand 24f58a9d17SGeoff Levand #if defined(DEBUG) 2583bb643dSGeert Uytterhoeven #define DBG udbg_printf 26f58a9d17SGeoff Levand #else 277424639aSMichael Ellerman #define DBG pr_devel 28f58a9d17SGeoff Levand #endif 29f58a9d17SGeoff Levand 30f58a9d17SGeoff Levand enum { 31f58a9d17SGeoff Levand #if defined(CONFIG_PS3_DYNAMIC_DMA) 32f58a9d17SGeoff Levand USE_DYNAMIC_DMA = 1, 33f58a9d17SGeoff Levand #else 34f58a9d17SGeoff Levand USE_DYNAMIC_DMA = 0, 35f58a9d17SGeoff Levand #endif 36f58a9d17SGeoff Levand }; 37f58a9d17SGeoff Levand 38f58a9d17SGeoff Levand enum { 39f58a9d17SGeoff Levand PAGE_SHIFT_4K = 12U, 40f58a9d17SGeoff Levand PAGE_SHIFT_64K = 16U, 41f58a9d17SGeoff Levand PAGE_SHIFT_16M = 24U, 42f58a9d17SGeoff Levand }; 43f58a9d17SGeoff Levand 44*f1ba9b94SNick Child static unsigned long __init make_page_sizes(unsigned long a, unsigned long b) 45f58a9d17SGeoff Levand { 46f58a9d17SGeoff Levand return (a << 56) | (b << 48); 47f58a9d17SGeoff Levand } 48f58a9d17SGeoff Levand 49f58a9d17SGeoff Levand enum { 50f58a9d17SGeoff Levand ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04, 51f58a9d17SGeoff Levand ALLOCATE_MEMORY_ADDR_ZERO = 0X08, 52f58a9d17SGeoff Levand }; 53f58a9d17SGeoff Levand 54f58a9d17SGeoff Levand /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */ 55f58a9d17SGeoff Levand 56f58a9d17SGeoff Levand enum { 57f58a9d17SGeoff Levand HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */ 58f58a9d17SGeoff Levand HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */ 59f58a9d17SGeoff Levand }; 60f58a9d17SGeoff Levand 61f58a9d17SGeoff Levand /*============================================================================*/ 62f58a9d17SGeoff Levand /* virtual address space routines */ 63f58a9d17SGeoff Levand /*============================================================================*/ 64f58a9d17SGeoff Levand 65f58a9d17SGeoff Levand /** 66f58a9d17SGeoff Levand * struct mem_region - memory region structure 67f58a9d17SGeoff Levand * @base: base address 68f58a9d17SGeoff Levand * @size: size in bytes 69f58a9d17SGeoff Levand * @offset: difference between base and rm.size 701e755c09SAndre Heider * @destroy: flag if region should be destroyed upon shutdown 71f58a9d17SGeoff Levand */ 72f58a9d17SGeoff Levand 73f58a9d17SGeoff Levand struct mem_region { 74b17b3df1SStephen Rothwell u64 base; 755418b9c6SStephen Rothwell u64 size; 76f58a9d17SGeoff Levand unsigned long offset; 771e755c09SAndre Heider int destroy; 78f58a9d17SGeoff Levand }; 79f58a9d17SGeoff Levand 80f58a9d17SGeoff Levand /** 81f58a9d17SGeoff Levand * struct map - address space state variables holder 82f58a9d17SGeoff Levand * @total: total memory available as reported by HV 83f58a9d17SGeoff Levand * @vas_id - HV virtual address space id 84f58a9d17SGeoff Levand * @htab_size: htab size in bytes 85f58a9d17SGeoff Levand * 86f58a9d17SGeoff Levand * The HV virtual address space (vas) allows for hotplug memory regions. 87f58a9d17SGeoff Levand * Memory regions can be created and destroyed in the vas at runtime. 88f58a9d17SGeoff Levand * @rm: real mode (bootmem) region 898ac5fd11SHector Martin * @r1: highmem region(s) 90f58a9d17SGeoff Levand * 91f58a9d17SGeoff Levand * ps3 addresses 92f58a9d17SGeoff Levand * virt_addr: a cpu 'translated' effective address 93f58a9d17SGeoff Levand * phys_addr: an address in what Linux thinks is the physical address space 94f58a9d17SGeoff Levand * lpar_addr: an address in the HV virtual address space 95f58a9d17SGeoff Levand * bus_addr: an io controller 'translated' address on a device bus 96f58a9d17SGeoff Levand */ 97f58a9d17SGeoff Levand 98f58a9d17SGeoff Levand struct map { 995418b9c6SStephen Rothwell u64 total; 100b17b3df1SStephen Rothwell u64 vas_id; 101b17b3df1SStephen Rothwell u64 htab_size; 102f58a9d17SGeoff Levand struct mem_region rm; 103f58a9d17SGeoff Levand struct mem_region r1; 104f58a9d17SGeoff Levand }; 105f58a9d17SGeoff Levand 106f58a9d17SGeoff Levand #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__) 1076bb5cf10SGeoff Levand static void __maybe_unused _debug_dump_map(const struct map *m, 1086bb5cf10SGeoff Levand const char *func, int line) 109f58a9d17SGeoff Levand { 1105418b9c6SStephen Rothwell DBG("%s:%d: map.total = %llxh\n", func, line, m->total); 1115418b9c6SStephen Rothwell DBG("%s:%d: map.rm.size = %llxh\n", func, line, m->rm.size); 112b17b3df1SStephen Rothwell DBG("%s:%d: map.vas_id = %llu\n", func, line, m->vas_id); 113b17b3df1SStephen Rothwell DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size); 114b17b3df1SStephen Rothwell DBG("%s:%d: map.r1.base = %llxh\n", func, line, m->r1.base); 115f58a9d17SGeoff Levand DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset); 1165418b9c6SStephen Rothwell DBG("%s:%d: map.r1.size = %llxh\n", func, line, m->r1.size); 117f58a9d17SGeoff Levand } 118f58a9d17SGeoff Levand 119f58a9d17SGeoff Levand static struct map map; 120f58a9d17SGeoff Levand 121f58a9d17SGeoff Levand /** 122f58a9d17SGeoff Levand * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address 123f58a9d17SGeoff Levand * @phys_addr: linux physical address 124f58a9d17SGeoff Levand */ 125f58a9d17SGeoff Levand 126f58a9d17SGeoff Levand unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr) 127f58a9d17SGeoff Levand { 128f58a9d17SGeoff Levand BUG_ON(is_kernel_addr(phys_addr)); 129f58a9d17SGeoff Levand return (phys_addr < map.rm.size || phys_addr >= map.total) 130f58a9d17SGeoff Levand ? phys_addr : phys_addr + map.r1.offset; 131f58a9d17SGeoff Levand } 132f58a9d17SGeoff Levand 133f58a9d17SGeoff Levand EXPORT_SYMBOL(ps3_mm_phys_to_lpar); 134f58a9d17SGeoff Levand 135f58a9d17SGeoff Levand /** 136f58a9d17SGeoff Levand * ps3_mm_vas_create - create the virtual address space 137f58a9d17SGeoff Levand */ 138f58a9d17SGeoff Levand 139f58a9d17SGeoff Levand void __init ps3_mm_vas_create(unsigned long* htab_size) 140f58a9d17SGeoff Levand { 141f58a9d17SGeoff Levand int result; 142b17b3df1SStephen Rothwell u64 start_address; 143b17b3df1SStephen Rothwell u64 size; 144b17b3df1SStephen Rothwell u64 access_right; 145b17b3df1SStephen Rothwell u64 max_page_size; 146b17b3df1SStephen Rothwell u64 flags; 147f58a9d17SGeoff Levand 148f58a9d17SGeoff Levand result = lv1_query_logical_partition_address_region_info(0, 149f58a9d17SGeoff Levand &start_address, &size, &access_right, &max_page_size, 150f58a9d17SGeoff Levand &flags); 151f58a9d17SGeoff Levand 152f58a9d17SGeoff Levand if (result) { 153f58a9d17SGeoff Levand DBG("%s:%d: lv1_query_logical_partition_address_region_info " 154f58a9d17SGeoff Levand "failed: %s\n", __func__, __LINE__, 155f58a9d17SGeoff Levand ps3_result(result)); 156f58a9d17SGeoff Levand goto fail; 157f58a9d17SGeoff Levand } 158f58a9d17SGeoff Levand 159f58a9d17SGeoff Levand if (max_page_size < PAGE_SHIFT_16M) { 160b17b3df1SStephen Rothwell DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__, 161f58a9d17SGeoff Levand max_page_size); 162f58a9d17SGeoff Levand goto fail; 163f58a9d17SGeoff Levand } 164f58a9d17SGeoff Levand 165f58a9d17SGeoff Levand BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX); 166f58a9d17SGeoff Levand BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN); 167f58a9d17SGeoff Levand 168f58a9d17SGeoff Levand result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE, 169f58a9d17SGeoff Levand 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K), 170f58a9d17SGeoff Levand &map.vas_id, &map.htab_size); 171f58a9d17SGeoff Levand 172f58a9d17SGeoff Levand if (result) { 173f58a9d17SGeoff Levand DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n", 174f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 175f58a9d17SGeoff Levand goto fail; 176f58a9d17SGeoff Levand } 177f58a9d17SGeoff Levand 178f58a9d17SGeoff Levand result = lv1_select_virtual_address_space(map.vas_id); 179f58a9d17SGeoff Levand 180f58a9d17SGeoff Levand if (result) { 181f58a9d17SGeoff Levand DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n", 182f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 183f58a9d17SGeoff Levand goto fail; 184f58a9d17SGeoff Levand } 185f58a9d17SGeoff Levand 186f58a9d17SGeoff Levand *htab_size = map.htab_size; 187f58a9d17SGeoff Levand 188f58a9d17SGeoff Levand debug_dump_map(&map); 189f58a9d17SGeoff Levand 190f58a9d17SGeoff Levand return; 191f58a9d17SGeoff Levand 192f58a9d17SGeoff Levand fail: 193f58a9d17SGeoff Levand panic("ps3_mm_vas_create failed"); 194f58a9d17SGeoff Levand } 195f58a9d17SGeoff Levand 196f58a9d17SGeoff Levand /** 197f58a9d17SGeoff Levand * ps3_mm_vas_destroy - 1988119cefdSHari Bathini * 1998119cefdSHari Bathini * called during kexec sequence with MMU off. 200f58a9d17SGeoff Levand */ 201f58a9d17SGeoff Levand 2028119cefdSHari Bathini notrace void ps3_mm_vas_destroy(void) 203f58a9d17SGeoff Levand { 2046bb5cf10SGeoff Levand int result; 2056bb5cf10SGeoff Levand 206f58a9d17SGeoff Levand if (map.vas_id) { 2076bb5cf10SGeoff Levand result = lv1_select_virtual_address_space(0); 20812655446SGeoff Levand result += lv1_destruct_virtual_address_space(map.vas_id); 20912655446SGeoff Levand 21012655446SGeoff Levand if (result) { 21112655446SGeoff Levand lv1_panic(0); 21212655446SGeoff Levand } 21312655446SGeoff Levand 214f58a9d17SGeoff Levand map.vas_id = 0; 215f58a9d17SGeoff Levand } 216f58a9d17SGeoff Levand } 217f58a9d17SGeoff Levand 218*f1ba9b94SNick Child static int __init ps3_mm_get_repository_highmem(struct mem_region *r) 219d4b18bd6SGeoff Levand { 220d4b18bd6SGeoff Levand int result; 221d4b18bd6SGeoff Levand 222d4b18bd6SGeoff Levand /* Assume a single highmem region. */ 223d4b18bd6SGeoff Levand 224d4b18bd6SGeoff Levand result = ps3_repository_read_highmem_info(0, &r->base, &r->size); 225d4b18bd6SGeoff Levand 226d4b18bd6SGeoff Levand if (result) 227d4b18bd6SGeoff Levand goto zero_region; 228d4b18bd6SGeoff Levand 229d4b18bd6SGeoff Levand if (!r->base || !r->size) { 230d4b18bd6SGeoff Levand result = -1; 231d4b18bd6SGeoff Levand goto zero_region; 232d4b18bd6SGeoff Levand } 233d4b18bd6SGeoff Levand 234d4b18bd6SGeoff Levand r->offset = r->base - map.rm.size; 235d4b18bd6SGeoff Levand 236d4b18bd6SGeoff Levand DBG("%s:%d: Found high region in repository: %llxh %llxh\n", 237d4b18bd6SGeoff Levand __func__, __LINE__, r->base, r->size); 238d4b18bd6SGeoff Levand 239d4b18bd6SGeoff Levand return 0; 240d4b18bd6SGeoff Levand 241d4b18bd6SGeoff Levand zero_region: 242d4b18bd6SGeoff Levand DBG("%s:%d: No high region in repository.\n", __func__, __LINE__); 243d4b18bd6SGeoff Levand 244d4b18bd6SGeoff Levand r->size = r->base = r->offset = 0; 245d4b18bd6SGeoff Levand return result; 246d4b18bd6SGeoff Levand } 247d4b18bd6SGeoff Levand 248d4b18bd6SGeoff Levand static int ps3_mm_set_repository_highmem(const struct mem_region *r) 249d4b18bd6SGeoff Levand { 250d4b18bd6SGeoff Levand /* Assume a single highmem region. */ 251d4b18bd6SGeoff Levand 252d4b18bd6SGeoff Levand return r ? ps3_repository_write_highmem_info(0, r->base, r->size) : 253d4b18bd6SGeoff Levand ps3_repository_write_highmem_info(0, 0, 0); 254d4b18bd6SGeoff Levand } 255d4b18bd6SGeoff Levand 256f58a9d17SGeoff Levand /** 257f58a9d17SGeoff Levand * ps3_mm_region_create - create a memory region in the vas 258f58a9d17SGeoff Levand * @r: pointer to a struct mem_region to accept initialized values 259f58a9d17SGeoff Levand * @size: requested region size 260f58a9d17SGeoff Levand * 261f58a9d17SGeoff Levand * This implementation creates the region with the vas large page size. 262f58a9d17SGeoff Levand * @size is rounded down to a multiple of the vas large page size. 263f58a9d17SGeoff Levand */ 264f58a9d17SGeoff Levand 26532f44a12SGeert Uytterhoeven static int ps3_mm_region_create(struct mem_region *r, unsigned long size) 266f58a9d17SGeoff Levand { 267f58a9d17SGeoff Levand int result; 268b17b3df1SStephen Rothwell u64 muid; 269f58a9d17SGeoff Levand 270e96d904eSChristophe Leroy r->size = ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M); 271f58a9d17SGeoff Levand 272f58a9d17SGeoff Levand DBG("%s:%d requested %lxh\n", __func__, __LINE__, size); 2735418b9c6SStephen Rothwell DBG("%s:%d actual %llxh\n", __func__, __LINE__, r->size); 2745418b9c6SStephen Rothwell DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__, 2755418b9c6SStephen Rothwell size - r->size, (size - r->size) / 1024 / 1024); 276f58a9d17SGeoff Levand 277f58a9d17SGeoff Levand if (r->size == 0) { 278f58a9d17SGeoff Levand DBG("%s:%d: size == 0\n", __func__, __LINE__); 279f58a9d17SGeoff Levand result = -1; 280f58a9d17SGeoff Levand goto zero_region; 281f58a9d17SGeoff Levand } 282f58a9d17SGeoff Levand 283f58a9d17SGeoff Levand result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0, 284f58a9d17SGeoff Levand ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid); 285f58a9d17SGeoff Levand 286f58a9d17SGeoff Levand if (result || r->base < map.rm.size) { 287f58a9d17SGeoff Levand DBG("%s:%d: lv1_allocate_memory failed: %s\n", 288f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 289f58a9d17SGeoff Levand goto zero_region; 290f58a9d17SGeoff Levand } 291f58a9d17SGeoff Levand 2921e755c09SAndre Heider r->destroy = 1; 293f58a9d17SGeoff Levand r->offset = r->base - map.rm.size; 294f58a9d17SGeoff Levand return result; 295f58a9d17SGeoff Levand 296f58a9d17SGeoff Levand zero_region: 297f58a9d17SGeoff Levand r->size = r->base = r->offset = 0; 298f58a9d17SGeoff Levand return result; 299f58a9d17SGeoff Levand } 300f58a9d17SGeoff Levand 301f58a9d17SGeoff Levand /** 302f58a9d17SGeoff Levand * ps3_mm_region_destroy - destroy a memory region 303f58a9d17SGeoff Levand * @r: pointer to struct mem_region 304f58a9d17SGeoff Levand */ 305f58a9d17SGeoff Levand 30632f44a12SGeert Uytterhoeven static void ps3_mm_region_destroy(struct mem_region *r) 307f58a9d17SGeoff Levand { 3086bb5cf10SGeoff Levand int result; 3096bb5cf10SGeoff Levand 3101e755c09SAndre Heider if (!r->destroy) { 3111e755c09SAndre Heider return; 3121e755c09SAndre Heider } 3131e755c09SAndre Heider 314f58a9d17SGeoff Levand if (r->base) { 3156bb5cf10SGeoff Levand result = lv1_release_memory(r->base); 31612655446SGeoff Levand 31712655446SGeoff Levand if (result) { 31812655446SGeoff Levand lv1_panic(0); 31912655446SGeoff Levand } 32012655446SGeoff Levand 321f58a9d17SGeoff Levand r->size = r->base = r->offset = 0; 322f58a9d17SGeoff Levand map.total = map.rm.size; 323f58a9d17SGeoff Levand } 32412655446SGeoff Levand 3255ae74630SGeoff Levand ps3_mm_set_repository_highmem(NULL); 326f58a9d17SGeoff Levand } 327f58a9d17SGeoff Levand 328f58a9d17SGeoff Levand /*============================================================================*/ 329f58a9d17SGeoff Levand /* dma routines */ 330f58a9d17SGeoff Levand /*============================================================================*/ 331f58a9d17SGeoff Levand 332f58a9d17SGeoff Levand /** 3336bb5cf10SGeoff Levand * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address. 334f58a9d17SGeoff Levand * @r: pointer to dma region structure 335f58a9d17SGeoff Levand * @lpar_addr: HV lpar address 336f58a9d17SGeoff Levand */ 337f58a9d17SGeoff Levand 3386bb5cf10SGeoff Levand static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r, 339f58a9d17SGeoff Levand unsigned long lpar_addr) 340f58a9d17SGeoff Levand { 3416bb5cf10SGeoff Levand if (lpar_addr >= map.rm.size) 3426bb5cf10SGeoff Levand lpar_addr -= map.r1.offset; 3436bb5cf10SGeoff Levand BUG_ON(lpar_addr < r->offset); 3446bb5cf10SGeoff Levand BUG_ON(lpar_addr >= r->offset + r->len); 3456bb5cf10SGeoff Levand return r->bus_addr + lpar_addr - r->offset; 346f58a9d17SGeoff Levand } 347f58a9d17SGeoff Levand 348f58a9d17SGeoff Levand #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__) 3496bb5cf10SGeoff Levand static void __maybe_unused _dma_dump_region(const struct ps3_dma_region *r, 3506bb5cf10SGeoff Levand const char *func, int line) 351f58a9d17SGeoff Levand { 3525c949070SStephen Rothwell DBG("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id, 3536bb5cf10SGeoff Levand r->dev->dev_id); 354f58a9d17SGeoff Levand DBG("%s:%d: page_size %u\n", func, line, r->page_size); 355f58a9d17SGeoff Levand DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr); 356f58a9d17SGeoff Levand DBG("%s:%d: len %lxh\n", func, line, r->len); 3576bb5cf10SGeoff Levand DBG("%s:%d: offset %lxh\n", func, line, r->offset); 358f58a9d17SGeoff Levand } 359f58a9d17SGeoff Levand 360f58a9d17SGeoff Levand /** 361f58a9d17SGeoff Levand * dma_chunk - A chunk of dma pages mapped by the io controller. 362f58a9d17SGeoff Levand * @region - The dma region that owns this chunk. 363f58a9d17SGeoff Levand * @lpar_addr: Starting lpar address of the area to map. 364f58a9d17SGeoff Levand * @bus_addr: Starting ioc bus address of the area to map. 365f58a9d17SGeoff Levand * @len: Length in bytes of the area to map. 366f58a9d17SGeoff Levand * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the 367f58a9d17SGeoff Levand * list of all chuncks owned by the region. 368f58a9d17SGeoff Levand * 369f58a9d17SGeoff Levand * This implementation uses a very simple dma page manager 370f58a9d17SGeoff Levand * based on the dma_chunk structure. This scheme assumes 371f58a9d17SGeoff Levand * that all drivers use very well behaved dma ops. 372f58a9d17SGeoff Levand */ 373f58a9d17SGeoff Levand 374f58a9d17SGeoff Levand struct dma_chunk { 375f58a9d17SGeoff Levand struct ps3_dma_region *region; 376f58a9d17SGeoff Levand unsigned long lpar_addr; 377f58a9d17SGeoff Levand unsigned long bus_addr; 378f58a9d17SGeoff Levand unsigned long len; 379f58a9d17SGeoff Levand struct list_head link; 380f58a9d17SGeoff Levand unsigned int usage_count; 381f58a9d17SGeoff Levand }; 382f58a9d17SGeoff Levand 383f58a9d17SGeoff Levand #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__) 384f58a9d17SGeoff Levand static void _dma_dump_chunk (const struct dma_chunk* c, const char* func, 385f58a9d17SGeoff Levand int line) 386f58a9d17SGeoff Levand { 3875c949070SStephen Rothwell DBG("%s:%d: r.dev %llu:%llu\n", func, line, 3886bb5cf10SGeoff Levand c->region->dev->bus_id, c->region->dev->dev_id); 389f58a9d17SGeoff Levand DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr); 390f58a9d17SGeoff Levand DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size); 391f58a9d17SGeoff Levand DBG("%s:%d: r.len %lxh\n", func, line, c->region->len); 3926bb5cf10SGeoff Levand DBG("%s:%d: r.offset %lxh\n", func, line, c->region->offset); 393f58a9d17SGeoff Levand DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr); 394f58a9d17SGeoff Levand DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr); 395f58a9d17SGeoff Levand DBG("%s:%d: c.len %lxh\n", func, line, c->len); 396f58a9d17SGeoff Levand } 397f58a9d17SGeoff Levand 398f58a9d17SGeoff Levand static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r, 399f58a9d17SGeoff Levand unsigned long bus_addr, unsigned long len) 400f58a9d17SGeoff Levand { 401f58a9d17SGeoff Levand struct dma_chunk *c; 402e96d904eSChristophe Leroy unsigned long aligned_bus = ALIGN_DOWN(bus_addr, 1 << r->page_size); 403b7115316SChristophe Leroy unsigned long aligned_len = ALIGN(len+bus_addr-aligned_bus, 4046bb5cf10SGeoff Levand 1 << r->page_size); 405f58a9d17SGeoff Levand 406f58a9d17SGeoff Levand list_for_each_entry(c, &r->chunk_list.head, link) { 407f58a9d17SGeoff Levand /* intersection */ 4086bb5cf10SGeoff Levand if (aligned_bus >= c->bus_addr && 4096bb5cf10SGeoff Levand aligned_bus + aligned_len <= c->bus_addr + c->len) 410f58a9d17SGeoff Levand return c; 4116bb5cf10SGeoff Levand 412f58a9d17SGeoff Levand /* below */ 4136bb5cf10SGeoff Levand if (aligned_bus + aligned_len <= c->bus_addr) 414f58a9d17SGeoff Levand continue; 4156bb5cf10SGeoff Levand 416f58a9d17SGeoff Levand /* above */ 4176bb5cf10SGeoff Levand if (aligned_bus >= c->bus_addr + c->len) 418f58a9d17SGeoff Levand continue; 419f58a9d17SGeoff Levand 420f58a9d17SGeoff Levand /* we don't handle the multi-chunk case for now */ 421f58a9d17SGeoff Levand dma_dump_chunk(c); 422f58a9d17SGeoff Levand BUG(); 423f58a9d17SGeoff Levand } 424f58a9d17SGeoff Levand return NULL; 425f58a9d17SGeoff Levand } 426f58a9d17SGeoff Levand 4276bb5cf10SGeoff Levand static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r, 4286bb5cf10SGeoff Levand unsigned long lpar_addr, unsigned long len) 4296bb5cf10SGeoff Levand { 4306bb5cf10SGeoff Levand struct dma_chunk *c; 431e96d904eSChristophe Leroy unsigned long aligned_lpar = ALIGN_DOWN(lpar_addr, 1 << r->page_size); 432b7115316SChristophe Leroy unsigned long aligned_len = ALIGN(len + lpar_addr - aligned_lpar, 4336bb5cf10SGeoff Levand 1 << r->page_size); 4346bb5cf10SGeoff Levand 4356bb5cf10SGeoff Levand list_for_each_entry(c, &r->chunk_list.head, link) { 4366bb5cf10SGeoff Levand /* intersection */ 4376bb5cf10SGeoff Levand if (c->lpar_addr <= aligned_lpar && 4386bb5cf10SGeoff Levand aligned_lpar < c->lpar_addr + c->len) { 4396bb5cf10SGeoff Levand if (aligned_lpar + aligned_len <= c->lpar_addr + c->len) 4406bb5cf10SGeoff Levand return c; 4416bb5cf10SGeoff Levand else { 4426bb5cf10SGeoff Levand dma_dump_chunk(c); 4436bb5cf10SGeoff Levand BUG(); 4446bb5cf10SGeoff Levand } 4456bb5cf10SGeoff Levand } 4466bb5cf10SGeoff Levand /* below */ 4476bb5cf10SGeoff Levand if (aligned_lpar + aligned_len <= c->lpar_addr) { 4486bb5cf10SGeoff Levand continue; 4496bb5cf10SGeoff Levand } 4506bb5cf10SGeoff Levand /* above */ 4516bb5cf10SGeoff Levand if (c->lpar_addr + c->len <= aligned_lpar) { 4526bb5cf10SGeoff Levand continue; 4536bb5cf10SGeoff Levand } 4546bb5cf10SGeoff Levand } 4556bb5cf10SGeoff Levand return NULL; 4566bb5cf10SGeoff Levand } 4576bb5cf10SGeoff Levand 4586bb5cf10SGeoff Levand static int dma_sb_free_chunk(struct dma_chunk *c) 459f58a9d17SGeoff Levand { 460f58a9d17SGeoff Levand int result = 0; 461f58a9d17SGeoff Levand 462f58a9d17SGeoff Levand if (c->bus_addr) { 4636bb5cf10SGeoff Levand result = lv1_unmap_device_dma_region(c->region->dev->bus_id, 4646bb5cf10SGeoff Levand c->region->dev->dev_id, c->bus_addr, c->len); 465f58a9d17SGeoff Levand BUG_ON(result); 466f58a9d17SGeoff Levand } 467f58a9d17SGeoff Levand 468f58a9d17SGeoff Levand kfree(c); 469f58a9d17SGeoff Levand return result; 470f58a9d17SGeoff Levand } 471f58a9d17SGeoff Levand 4726bb5cf10SGeoff Levand static int dma_ioc0_free_chunk(struct dma_chunk *c) 4736bb5cf10SGeoff Levand { 4746bb5cf10SGeoff Levand int result = 0; 4756bb5cf10SGeoff Levand int iopage; 4766bb5cf10SGeoff Levand unsigned long offset; 4776bb5cf10SGeoff Levand struct ps3_dma_region *r = c->region; 4786bb5cf10SGeoff Levand 4796bb5cf10SGeoff Levand DBG("%s:start\n", __func__); 4806bb5cf10SGeoff Levand for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) { 4816bb5cf10SGeoff Levand offset = (1 << r->page_size) * iopage; 4826bb5cf10SGeoff Levand /* put INVALID entry */ 4836bb5cf10SGeoff Levand result = lv1_put_iopte(0, 4846bb5cf10SGeoff Levand c->bus_addr + offset, 4856bb5cf10SGeoff Levand c->lpar_addr + offset, 4866bb5cf10SGeoff Levand r->ioid, 4876bb5cf10SGeoff Levand 0); 4886bb5cf10SGeoff Levand DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__, 4896bb5cf10SGeoff Levand c->bus_addr + offset, 4906bb5cf10SGeoff Levand c->lpar_addr + offset, 4916bb5cf10SGeoff Levand r->ioid); 4926bb5cf10SGeoff Levand 4936bb5cf10SGeoff Levand if (result) { 4946bb5cf10SGeoff Levand DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__, 4956bb5cf10SGeoff Levand __LINE__, ps3_result(result)); 4966bb5cf10SGeoff Levand } 4976bb5cf10SGeoff Levand } 4986bb5cf10SGeoff Levand kfree(c); 4996bb5cf10SGeoff Levand DBG("%s:end\n", __func__); 5006bb5cf10SGeoff Levand return result; 5016bb5cf10SGeoff Levand } 5026bb5cf10SGeoff Levand 503f58a9d17SGeoff Levand /** 5046bb5cf10SGeoff Levand * dma_sb_map_pages - Maps dma pages into the io controller bus address space. 505f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 506f58a9d17SGeoff Levand * @phys_addr: Starting physical address of the area to map. 507f58a9d17SGeoff Levand * @len: Length in bytes of the area to map. 508f58a9d17SGeoff Levand * c_out: A pointer to receive an allocated struct dma_chunk for this area. 509f58a9d17SGeoff Levand * 510f58a9d17SGeoff Levand * This is the lowest level dma mapping routine, and is the one that will 511f58a9d17SGeoff Levand * make the HV call to add the pages into the io controller address space. 512f58a9d17SGeoff Levand */ 513f58a9d17SGeoff Levand 5146bb5cf10SGeoff Levand static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr, 5156bb5cf10SGeoff Levand unsigned long len, struct dma_chunk **c_out, u64 iopte_flag) 516f58a9d17SGeoff Levand { 517f58a9d17SGeoff Levand int result; 518f58a9d17SGeoff Levand struct dma_chunk *c; 519f58a9d17SGeoff Levand 520a0828cf5SMarkus Elfring c = kzalloc(sizeof(*c), GFP_ATOMIC); 521f58a9d17SGeoff Levand if (!c) { 522f58a9d17SGeoff Levand result = -ENOMEM; 523f58a9d17SGeoff Levand goto fail_alloc; 524f58a9d17SGeoff Levand } 525f58a9d17SGeoff Levand 526f58a9d17SGeoff Levand c->region = r; 527f58a9d17SGeoff Levand c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr); 5286bb5cf10SGeoff Levand c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr); 529f58a9d17SGeoff Levand c->len = len; 530f58a9d17SGeoff Levand 5316bb5cf10SGeoff Levand BUG_ON(iopte_flag != 0xf800000000000000UL); 5326bb5cf10SGeoff Levand result = lv1_map_device_dma_region(c->region->dev->bus_id, 5336bb5cf10SGeoff Levand c->region->dev->dev_id, c->lpar_addr, 5346bb5cf10SGeoff Levand c->bus_addr, c->len, iopte_flag); 535f58a9d17SGeoff Levand if (result) { 536f58a9d17SGeoff Levand DBG("%s:%d: lv1_map_device_dma_region failed: %s\n", 537f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 538f58a9d17SGeoff Levand goto fail_map; 539f58a9d17SGeoff Levand } 540f58a9d17SGeoff Levand 541f58a9d17SGeoff Levand list_add(&c->link, &r->chunk_list.head); 542f58a9d17SGeoff Levand 543f58a9d17SGeoff Levand *c_out = c; 544f58a9d17SGeoff Levand return 0; 545f58a9d17SGeoff Levand 546f58a9d17SGeoff Levand fail_map: 547f58a9d17SGeoff Levand kfree(c); 548f58a9d17SGeoff Levand fail_alloc: 549f58a9d17SGeoff Levand *c_out = NULL; 550f58a9d17SGeoff Levand DBG(" <- %s:%d\n", __func__, __LINE__); 551f58a9d17SGeoff Levand return result; 552f58a9d17SGeoff Levand } 553f58a9d17SGeoff Levand 5546bb5cf10SGeoff Levand static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr, 5556bb5cf10SGeoff Levand unsigned long len, struct dma_chunk **c_out, 5566bb5cf10SGeoff Levand u64 iopte_flag) 5576bb5cf10SGeoff Levand { 5586bb5cf10SGeoff Levand int result; 5596bb5cf10SGeoff Levand struct dma_chunk *c, *last; 5606bb5cf10SGeoff Levand int iopage, pages; 5616bb5cf10SGeoff Levand unsigned long offset; 5626bb5cf10SGeoff Levand 5636bb5cf10SGeoff Levand DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__, 5646bb5cf10SGeoff Levand phys_addr, ps3_mm_phys_to_lpar(phys_addr), len); 565a0828cf5SMarkus Elfring c = kzalloc(sizeof(*c), GFP_ATOMIC); 5666bb5cf10SGeoff Levand if (!c) { 5676bb5cf10SGeoff Levand result = -ENOMEM; 5686bb5cf10SGeoff Levand goto fail_alloc; 5696bb5cf10SGeoff Levand } 5706bb5cf10SGeoff Levand 5716bb5cf10SGeoff Levand c->region = r; 5726bb5cf10SGeoff Levand c->len = len; 5736bb5cf10SGeoff Levand c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr); 5746bb5cf10SGeoff Levand /* allocate IO address */ 5756bb5cf10SGeoff Levand if (list_empty(&r->chunk_list.head)) { 5766bb5cf10SGeoff Levand /* first one */ 5776bb5cf10SGeoff Levand c->bus_addr = r->bus_addr; 5786bb5cf10SGeoff Levand } else { 5796bb5cf10SGeoff Levand /* derive from last bus addr*/ 5806bb5cf10SGeoff Levand last = list_entry(r->chunk_list.head.next, 5816bb5cf10SGeoff Levand struct dma_chunk, link); 5826bb5cf10SGeoff Levand c->bus_addr = last->bus_addr + last->len; 5836bb5cf10SGeoff Levand DBG("%s: last bus=%#lx, len=%#lx\n", __func__, 5846bb5cf10SGeoff Levand last->bus_addr, last->len); 5856bb5cf10SGeoff Levand } 5866bb5cf10SGeoff Levand 5876bb5cf10SGeoff Levand /* FIXME: check whether length exceeds region size */ 5886bb5cf10SGeoff Levand 5896bb5cf10SGeoff Levand /* build ioptes for the area */ 5906bb5cf10SGeoff Levand pages = len >> r->page_size; 5915c949070SStephen Rothwell DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__, 5926bb5cf10SGeoff Levand r->page_size, r->len, pages, iopte_flag); 5936bb5cf10SGeoff Levand for (iopage = 0; iopage < pages; iopage++) { 5946bb5cf10SGeoff Levand offset = (1 << r->page_size) * iopage; 5956bb5cf10SGeoff Levand result = lv1_put_iopte(0, 5966bb5cf10SGeoff Levand c->bus_addr + offset, 5976bb5cf10SGeoff Levand c->lpar_addr + offset, 5986bb5cf10SGeoff Levand r->ioid, 5996bb5cf10SGeoff Levand iopte_flag); 6006bb5cf10SGeoff Levand if (result) { 601f2c2cbccSJoe Perches pr_warn("%s:%d: lv1_put_iopte failed: %s\n", 6027e28060aSGeert Uytterhoeven __func__, __LINE__, ps3_result(result)); 6036bb5cf10SGeoff Levand goto fail_map; 6046bb5cf10SGeoff Levand } 6056bb5cf10SGeoff Levand DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__, 6066bb5cf10SGeoff Levand iopage, c->bus_addr + offset, c->lpar_addr + offset, 6076bb5cf10SGeoff Levand r->ioid); 6086bb5cf10SGeoff Levand } 6096bb5cf10SGeoff Levand 6106bb5cf10SGeoff Levand /* be sure that last allocated one is inserted at head */ 6116bb5cf10SGeoff Levand list_add(&c->link, &r->chunk_list.head); 6126bb5cf10SGeoff Levand 6136bb5cf10SGeoff Levand *c_out = c; 6146bb5cf10SGeoff Levand DBG("%s: end\n", __func__); 6156bb5cf10SGeoff Levand return 0; 6166bb5cf10SGeoff Levand 6176bb5cf10SGeoff Levand fail_map: 6186bb5cf10SGeoff Levand for (iopage--; 0 <= iopage; iopage--) { 6196bb5cf10SGeoff Levand lv1_put_iopte(0, 6206bb5cf10SGeoff Levand c->bus_addr + offset, 6216bb5cf10SGeoff Levand c->lpar_addr + offset, 6226bb5cf10SGeoff Levand r->ioid, 6236bb5cf10SGeoff Levand 0); 6246bb5cf10SGeoff Levand } 6256bb5cf10SGeoff Levand kfree(c); 6266bb5cf10SGeoff Levand fail_alloc: 6276bb5cf10SGeoff Levand *c_out = NULL; 6286bb5cf10SGeoff Levand return result; 6296bb5cf10SGeoff Levand } 6306bb5cf10SGeoff Levand 631f58a9d17SGeoff Levand /** 6326bb5cf10SGeoff Levand * dma_sb_region_create - Create a device dma region. 633f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 634f58a9d17SGeoff Levand * 635f58a9d17SGeoff Levand * This is the lowest level dma region create routine, and is the one that 636f58a9d17SGeoff Levand * will make the HV call to create the region. 637f58a9d17SGeoff Levand */ 638f58a9d17SGeoff Levand 6396bb5cf10SGeoff Levand static int dma_sb_region_create(struct ps3_dma_region *r) 640f58a9d17SGeoff Levand { 641f58a9d17SGeoff Levand int result; 642b17b3df1SStephen Rothwell u64 bus_addr; 643f58a9d17SGeoff Levand 64462d80749SGeoff Levand DBG(" -> %s:%d:\n", __func__, __LINE__); 6456bb5cf10SGeoff Levand 6466bb5cf10SGeoff Levand BUG_ON(!r); 6476bb5cf10SGeoff Levand 6486bb5cf10SGeoff Levand if (!r->dev->bus_id) { 6495c949070SStephen Rothwell pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__, 6506bb5cf10SGeoff Levand r->dev->bus_id, r->dev->dev_id); 6516bb5cf10SGeoff Levand return 0; 6526bb5cf10SGeoff Levand } 6536bb5cf10SGeoff Levand 6546bb5cf10SGeoff Levand DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__, 6556bb5cf10SGeoff Levand __LINE__, r->len, r->page_size, r->offset); 6566bb5cf10SGeoff Levand 6576bb5cf10SGeoff Levand BUG_ON(!r->len); 6586bb5cf10SGeoff Levand BUG_ON(!r->page_size); 6596bb5cf10SGeoff Levand BUG_ON(!r->region_ops); 6606bb5cf10SGeoff Levand 661f58a9d17SGeoff Levand INIT_LIST_HEAD(&r->chunk_list.head); 662f58a9d17SGeoff Levand spin_lock_init(&r->chunk_list.lock); 663f58a9d17SGeoff Levand 6646bb5cf10SGeoff Levand result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id, 6656bb5cf10SGeoff Levand roundup_pow_of_two(r->len), r->page_size, r->region_type, 666b17b3df1SStephen Rothwell &bus_addr); 667b17b3df1SStephen Rothwell r->bus_addr = bus_addr; 668f58a9d17SGeoff Levand 669f58a9d17SGeoff Levand if (result) { 670f58a9d17SGeoff Levand DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n", 671f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 672f58a9d17SGeoff Levand r->len = r->bus_addr = 0; 673f58a9d17SGeoff Levand } 674f58a9d17SGeoff Levand 675f58a9d17SGeoff Levand return result; 676f58a9d17SGeoff Levand } 677f58a9d17SGeoff Levand 6786bb5cf10SGeoff Levand static int dma_ioc0_region_create(struct ps3_dma_region *r) 6796bb5cf10SGeoff Levand { 6806bb5cf10SGeoff Levand int result; 681b17b3df1SStephen Rothwell u64 bus_addr; 6826bb5cf10SGeoff Levand 6836bb5cf10SGeoff Levand INIT_LIST_HEAD(&r->chunk_list.head); 6846bb5cf10SGeoff Levand spin_lock_init(&r->chunk_list.lock); 6856bb5cf10SGeoff Levand 6866bb5cf10SGeoff Levand result = lv1_allocate_io_segment(0, 6876bb5cf10SGeoff Levand r->len, 6886bb5cf10SGeoff Levand r->page_size, 689b17b3df1SStephen Rothwell &bus_addr); 690b17b3df1SStephen Rothwell r->bus_addr = bus_addr; 6916bb5cf10SGeoff Levand if (result) { 6926bb5cf10SGeoff Levand DBG("%s:%d: lv1_allocate_io_segment failed: %s\n", 6936bb5cf10SGeoff Levand __func__, __LINE__, ps3_result(result)); 6946bb5cf10SGeoff Levand r->len = r->bus_addr = 0; 6956bb5cf10SGeoff Levand } 6966bb5cf10SGeoff Levand DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__, 6976bb5cf10SGeoff Levand r->len, r->page_size, r->bus_addr); 6986bb5cf10SGeoff Levand return result; 6996bb5cf10SGeoff Levand } 7006bb5cf10SGeoff Levand 701f58a9d17SGeoff Levand /** 702f58a9d17SGeoff Levand * dma_region_free - Free a device dma region. 703f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 704f58a9d17SGeoff Levand * 705f58a9d17SGeoff Levand * This is the lowest level dma region free routine, and is the one that 706f58a9d17SGeoff Levand * will make the HV call to free the region. 707f58a9d17SGeoff Levand */ 708f58a9d17SGeoff Levand 7096bb5cf10SGeoff Levand static int dma_sb_region_free(struct ps3_dma_region *r) 710f58a9d17SGeoff Levand { 711f58a9d17SGeoff Levand int result; 712f58a9d17SGeoff Levand struct dma_chunk *c; 713f58a9d17SGeoff Levand struct dma_chunk *tmp; 714f58a9d17SGeoff Levand 7156bb5cf10SGeoff Levand BUG_ON(!r); 7166bb5cf10SGeoff Levand 7176bb5cf10SGeoff Levand if (!r->dev->bus_id) { 7185c949070SStephen Rothwell pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__, 7196bb5cf10SGeoff Levand r->dev->bus_id, r->dev->dev_id); 7206bb5cf10SGeoff Levand return 0; 721f58a9d17SGeoff Levand } 722f58a9d17SGeoff Levand 7236bb5cf10SGeoff Levand list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) { 7246bb5cf10SGeoff Levand list_del(&c->link); 7256bb5cf10SGeoff Levand dma_sb_free_chunk(c); 7266bb5cf10SGeoff Levand } 7276bb5cf10SGeoff Levand 7286bb5cf10SGeoff Levand result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id, 729f58a9d17SGeoff Levand r->bus_addr); 730f58a9d17SGeoff Levand 731f58a9d17SGeoff Levand if (result) 732f58a9d17SGeoff Levand DBG("%s:%d: lv1_free_device_dma_region failed: %s\n", 733f58a9d17SGeoff Levand __func__, __LINE__, ps3_result(result)); 734f58a9d17SGeoff Levand 7356bb5cf10SGeoff Levand r->bus_addr = 0; 7366bb5cf10SGeoff Levand 7376bb5cf10SGeoff Levand return result; 7386bb5cf10SGeoff Levand } 7396bb5cf10SGeoff Levand 7406bb5cf10SGeoff Levand static int dma_ioc0_region_free(struct ps3_dma_region *r) 7416bb5cf10SGeoff Levand { 7426bb5cf10SGeoff Levand int result; 7436bb5cf10SGeoff Levand struct dma_chunk *c, *n; 7446bb5cf10SGeoff Levand 7456bb5cf10SGeoff Levand DBG("%s: start\n", __func__); 7466bb5cf10SGeoff Levand list_for_each_entry_safe(c, n, &r->chunk_list.head, link) { 7476bb5cf10SGeoff Levand list_del(&c->link); 7486bb5cf10SGeoff Levand dma_ioc0_free_chunk(c); 7496bb5cf10SGeoff Levand } 7506bb5cf10SGeoff Levand 7516bb5cf10SGeoff Levand result = lv1_release_io_segment(0, r->bus_addr); 7526bb5cf10SGeoff Levand 7536bb5cf10SGeoff Levand if (result) 7546bb5cf10SGeoff Levand DBG("%s:%d: lv1_free_device_dma_region failed: %s\n", 7556bb5cf10SGeoff Levand __func__, __LINE__, ps3_result(result)); 7566bb5cf10SGeoff Levand 7576bb5cf10SGeoff Levand r->bus_addr = 0; 7586bb5cf10SGeoff Levand DBG("%s: end\n", __func__); 759f58a9d17SGeoff Levand 760f58a9d17SGeoff Levand return result; 761f58a9d17SGeoff Levand } 762f58a9d17SGeoff Levand 763f58a9d17SGeoff Levand /** 7646bb5cf10SGeoff Levand * dma_sb_map_area - Map an area of memory into a device dma region. 765f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 766f58a9d17SGeoff Levand * @virt_addr: Starting virtual address of the area to map. 767f58a9d17SGeoff Levand * @len: Length in bytes of the area to map. 768f58a9d17SGeoff Levand * @bus_addr: A pointer to return the starting ioc bus address of the area to 769f58a9d17SGeoff Levand * map. 770f58a9d17SGeoff Levand * 771f58a9d17SGeoff Levand * This is the common dma mapping routine. 772f58a9d17SGeoff Levand */ 773f58a9d17SGeoff Levand 7746bb5cf10SGeoff Levand static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr, 775494fd07aSStephen Rothwell unsigned long len, dma_addr_t *bus_addr, 7766bb5cf10SGeoff Levand u64 iopte_flag) 777f58a9d17SGeoff Levand { 778f58a9d17SGeoff Levand int result; 779f58a9d17SGeoff Levand unsigned long flags; 780f58a9d17SGeoff Levand struct dma_chunk *c; 781f58a9d17SGeoff Levand unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr) 782f58a9d17SGeoff Levand : virt_addr; 783e96d904eSChristophe Leroy unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size); 784b7115316SChristophe Leroy unsigned long aligned_len = ALIGN(len + phys_addr - aligned_phys, 7856bb5cf10SGeoff Levand 1 << r->page_size); 7866bb5cf10SGeoff Levand *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr)); 787f58a9d17SGeoff Levand 788f58a9d17SGeoff Levand if (!USE_DYNAMIC_DMA) { 789f58a9d17SGeoff Levand unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr); 790f58a9d17SGeoff Levand DBG(" -> %s:%d\n", __func__, __LINE__); 791f58a9d17SGeoff Levand DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__, 792f58a9d17SGeoff Levand virt_addr); 793f58a9d17SGeoff Levand DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__, 794f58a9d17SGeoff Levand phys_addr); 795f58a9d17SGeoff Levand DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__, 796f58a9d17SGeoff Levand lpar_addr); 797f58a9d17SGeoff Levand DBG("%s:%d len %lxh\n", __func__, __LINE__, len); 798494fd07aSStephen Rothwell DBG("%s:%d bus_addr %llxh (%lxh)\n", __func__, __LINE__, 799f58a9d17SGeoff Levand *bus_addr, len); 800f58a9d17SGeoff Levand } 801f58a9d17SGeoff Levand 802f58a9d17SGeoff Levand spin_lock_irqsave(&r->chunk_list.lock, flags); 803f58a9d17SGeoff Levand c = dma_find_chunk(r, *bus_addr, len); 804f58a9d17SGeoff Levand 805f58a9d17SGeoff Levand if (c) { 8066bb5cf10SGeoff Levand DBG("%s:%d: reusing mapped chunk", __func__, __LINE__); 8076bb5cf10SGeoff Levand dma_dump_chunk(c); 808f58a9d17SGeoff Levand c->usage_count++; 809f58a9d17SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 810f58a9d17SGeoff Levand return 0; 811f58a9d17SGeoff Levand } 812f58a9d17SGeoff Levand 8136bb5cf10SGeoff Levand result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag); 814f58a9d17SGeoff Levand 815f58a9d17SGeoff Levand if (result) { 816f58a9d17SGeoff Levand *bus_addr = 0; 8176bb5cf10SGeoff Levand DBG("%s:%d: dma_sb_map_pages failed (%d)\n", 818f58a9d17SGeoff Levand __func__, __LINE__, result); 819f58a9d17SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 820f58a9d17SGeoff Levand return result; 821f58a9d17SGeoff Levand } 822f58a9d17SGeoff Levand 823f58a9d17SGeoff Levand c->usage_count = 1; 824f58a9d17SGeoff Levand 825f58a9d17SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 826f58a9d17SGeoff Levand return result; 827f58a9d17SGeoff Levand } 828f58a9d17SGeoff Levand 8296bb5cf10SGeoff Levand static int dma_ioc0_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) 8326bb5cf10SGeoff Levand { 8336bb5cf10SGeoff Levand int result; 8346bb5cf10SGeoff Levand unsigned long flags; 8356bb5cf10SGeoff Levand struct dma_chunk *c; 8366bb5cf10SGeoff Levand unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr) 8376bb5cf10SGeoff Levand : virt_addr; 838e96d904eSChristophe Leroy unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size); 839b7115316SChristophe Leroy unsigned long aligned_len = ALIGN(len + phys_addr - aligned_phys, 8406bb5cf10SGeoff Levand 1 << r->page_size); 8416bb5cf10SGeoff Levand 8426bb5cf10SGeoff Levand DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__, 8436bb5cf10SGeoff Levand virt_addr, len); 8446bb5cf10SGeoff Levand DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__, 8456bb5cf10SGeoff Levand phys_addr, aligned_phys, aligned_len); 8466bb5cf10SGeoff Levand 8476bb5cf10SGeoff Levand spin_lock_irqsave(&r->chunk_list.lock, flags); 8486bb5cf10SGeoff Levand c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len); 8496bb5cf10SGeoff Levand 8506bb5cf10SGeoff Levand if (c) { 8516bb5cf10SGeoff Levand /* FIXME */ 8526bb5cf10SGeoff Levand BUG(); 8536bb5cf10SGeoff Levand *bus_addr = c->bus_addr + phys_addr - aligned_phys; 8546bb5cf10SGeoff Levand c->usage_count++; 8556bb5cf10SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 8566bb5cf10SGeoff Levand return 0; 8576bb5cf10SGeoff Levand } 8586bb5cf10SGeoff Levand 8596bb5cf10SGeoff Levand result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c, 8606bb5cf10SGeoff Levand iopte_flag); 8616bb5cf10SGeoff Levand 8626bb5cf10SGeoff Levand if (result) { 8636bb5cf10SGeoff Levand *bus_addr = 0; 8646bb5cf10SGeoff Levand DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n", 8656bb5cf10SGeoff Levand __func__, __LINE__, result); 8666bb5cf10SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 8676bb5cf10SGeoff Levand return result; 8686bb5cf10SGeoff Levand } 8696bb5cf10SGeoff Levand *bus_addr = c->bus_addr + phys_addr - aligned_phys; 870494fd07aSStephen Rothwell DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__, 8716bb5cf10SGeoff Levand virt_addr, phys_addr, aligned_phys, *bus_addr); 8726bb5cf10SGeoff Levand c->usage_count = 1; 8736bb5cf10SGeoff Levand 8746bb5cf10SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 8756bb5cf10SGeoff Levand return result; 8766bb5cf10SGeoff Levand } 8776bb5cf10SGeoff Levand 878f58a9d17SGeoff Levand /** 8796bb5cf10SGeoff Levand * dma_sb_unmap_area - Unmap an area of memory from a device dma region. 880f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 881f58a9d17SGeoff Levand * @bus_addr: The starting ioc bus address of the area to unmap. 882f58a9d17SGeoff Levand * @len: Length in bytes of the area to unmap. 883f58a9d17SGeoff Levand * 884f58a9d17SGeoff Levand * This is the common dma unmap routine. 885f58a9d17SGeoff Levand */ 886f58a9d17SGeoff Levand 887494fd07aSStephen Rothwell static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr, 888f58a9d17SGeoff Levand unsigned long len) 889f58a9d17SGeoff Levand { 890f58a9d17SGeoff Levand unsigned long flags; 891f58a9d17SGeoff Levand struct dma_chunk *c; 892f58a9d17SGeoff Levand 893f58a9d17SGeoff Levand spin_lock_irqsave(&r->chunk_list.lock, flags); 894f58a9d17SGeoff Levand c = dma_find_chunk(r, bus_addr, len); 895f58a9d17SGeoff Levand 896f58a9d17SGeoff Levand if (!c) { 897e96d904eSChristophe Leroy unsigned long aligned_bus = ALIGN_DOWN(bus_addr, 898f58a9d17SGeoff Levand 1 << r->page_size); 899b7115316SChristophe Leroy unsigned long aligned_len = ALIGN(len + bus_addr 9006bb5cf10SGeoff Levand - aligned_bus, 1 << r->page_size); 901494fd07aSStephen Rothwell DBG("%s:%d: not found: bus_addr %llxh\n", 902f58a9d17SGeoff Levand __func__, __LINE__, bus_addr); 903f58a9d17SGeoff Levand DBG("%s:%d: not found: len %lxh\n", 904f58a9d17SGeoff Levand __func__, __LINE__, len); 905f58a9d17SGeoff Levand DBG("%s:%d: not found: aligned_bus %lxh\n", 906f58a9d17SGeoff Levand __func__, __LINE__, aligned_bus); 907f58a9d17SGeoff Levand DBG("%s:%d: not found: aligned_len %lxh\n", 908f58a9d17SGeoff Levand __func__, __LINE__, aligned_len); 909f58a9d17SGeoff Levand BUG(); 910f58a9d17SGeoff Levand } 911f58a9d17SGeoff Levand 912f58a9d17SGeoff Levand c->usage_count--; 913f58a9d17SGeoff Levand 914f58a9d17SGeoff Levand if (!c->usage_count) { 915f58a9d17SGeoff Levand list_del(&c->link); 9166bb5cf10SGeoff Levand dma_sb_free_chunk(c); 917f58a9d17SGeoff Levand } 918f58a9d17SGeoff Levand 919f58a9d17SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 920f58a9d17SGeoff Levand return 0; 921f58a9d17SGeoff Levand } 922f58a9d17SGeoff Levand 92332f44a12SGeert Uytterhoeven static int dma_ioc0_unmap_area(struct ps3_dma_region *r, 924494fd07aSStephen Rothwell dma_addr_t bus_addr, unsigned long len) 9256bb5cf10SGeoff Levand { 9266bb5cf10SGeoff Levand unsigned long flags; 9276bb5cf10SGeoff Levand struct dma_chunk *c; 9286bb5cf10SGeoff Levand 929494fd07aSStephen Rothwell DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len); 9306bb5cf10SGeoff Levand spin_lock_irqsave(&r->chunk_list.lock, flags); 9316bb5cf10SGeoff Levand c = dma_find_chunk(r, bus_addr, len); 9326bb5cf10SGeoff Levand 9336bb5cf10SGeoff Levand if (!c) { 934e96d904eSChristophe Leroy unsigned long aligned_bus = ALIGN_DOWN(bus_addr, 9356bb5cf10SGeoff Levand 1 << r->page_size); 936b7115316SChristophe Leroy unsigned long aligned_len = ALIGN(len + bus_addr 9376bb5cf10SGeoff Levand - aligned_bus, 9386bb5cf10SGeoff Levand 1 << r->page_size); 939494fd07aSStephen Rothwell DBG("%s:%d: not found: bus_addr %llxh\n", 9406bb5cf10SGeoff Levand __func__, __LINE__, bus_addr); 9416bb5cf10SGeoff Levand DBG("%s:%d: not found: len %lxh\n", 9426bb5cf10SGeoff Levand __func__, __LINE__, len); 9436bb5cf10SGeoff Levand DBG("%s:%d: not found: aligned_bus %lxh\n", 9446bb5cf10SGeoff Levand __func__, __LINE__, aligned_bus); 9456bb5cf10SGeoff Levand DBG("%s:%d: not found: aligned_len %lxh\n", 9466bb5cf10SGeoff Levand __func__, __LINE__, aligned_len); 9476bb5cf10SGeoff Levand BUG(); 9486bb5cf10SGeoff Levand } 9496bb5cf10SGeoff Levand 9506bb5cf10SGeoff Levand c->usage_count--; 9516bb5cf10SGeoff Levand 9526bb5cf10SGeoff Levand if (!c->usage_count) { 9536bb5cf10SGeoff Levand list_del(&c->link); 9546bb5cf10SGeoff Levand dma_ioc0_free_chunk(c); 9556bb5cf10SGeoff Levand } 9566bb5cf10SGeoff Levand 9576bb5cf10SGeoff Levand spin_unlock_irqrestore(&r->chunk_list.lock, flags); 9586bb5cf10SGeoff Levand DBG("%s: end\n", __func__); 9596bb5cf10SGeoff Levand return 0; 9606bb5cf10SGeoff Levand } 9616bb5cf10SGeoff Levand 962f58a9d17SGeoff Levand /** 9636bb5cf10SGeoff Levand * dma_sb_region_create_linear - Setup a linear dma mapping for a device. 964f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 965f58a9d17SGeoff Levand * 966f58a9d17SGeoff Levand * This routine creates an HV dma region for the device and maps all available 967f58a9d17SGeoff Levand * ram into the io controller bus address space. 968f58a9d17SGeoff Levand */ 969f58a9d17SGeoff Levand 9706bb5cf10SGeoff Levand static int dma_sb_region_create_linear(struct ps3_dma_region *r) 971f58a9d17SGeoff Levand { 972f58a9d17SGeoff Levand int result; 973494fd07aSStephen Rothwell unsigned long virt_addr, len; 974494fd07aSStephen Rothwell dma_addr_t tmp; 975f58a9d17SGeoff Levand 9766bb5cf10SGeoff Levand if (r->len > 16*1024*1024) { /* FIXME: need proper fix */ 977f58a9d17SGeoff Levand /* force 16M dma pages for linear mapping */ 978f58a9d17SGeoff Levand if (r->page_size != PS3_DMA_16M) { 979f58a9d17SGeoff Levand pr_info("%s:%d: forcing 16M pages for linear map\n", 980f58a9d17SGeoff Levand __func__, __LINE__); 981f58a9d17SGeoff Levand r->page_size = PS3_DMA_16M; 982b7115316SChristophe Leroy r->len = ALIGN(r->len, 1 << r->page_size); 9836bb5cf10SGeoff Levand } 984f58a9d17SGeoff Levand } 985f58a9d17SGeoff Levand 9866bb5cf10SGeoff Levand result = dma_sb_region_create(r); 987f58a9d17SGeoff Levand BUG_ON(result); 988f58a9d17SGeoff Levand 9896bb5cf10SGeoff Levand if (r->offset < map.rm.size) { 9906bb5cf10SGeoff Levand /* Map (part of) 1st RAM chunk */ 9916bb5cf10SGeoff Levand virt_addr = map.rm.base + r->offset; 9926bb5cf10SGeoff Levand len = map.rm.size - r->offset; 9936bb5cf10SGeoff Levand if (len > r->len) 9946bb5cf10SGeoff Levand len = r->len; 9956bb5cf10SGeoff Levand result = dma_sb_map_area(r, virt_addr, len, &tmp, 9965c6fc8dbSGeert Uytterhoeven CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW | 9975c6fc8dbSGeert Uytterhoeven CBE_IOPTE_M); 998f58a9d17SGeoff Levand BUG_ON(result); 9996bb5cf10SGeoff Levand } 1000f58a9d17SGeoff Levand 10016bb5cf10SGeoff Levand if (r->offset + r->len > map.rm.size) { 10026bb5cf10SGeoff Levand /* Map (part of) 2nd RAM chunk */ 1003a628df1eSGeoff Levand virt_addr = map.rm.size; 10046bb5cf10SGeoff Levand len = r->len; 10056bb5cf10SGeoff Levand if (r->offset >= map.rm.size) 10066bb5cf10SGeoff Levand virt_addr += r->offset - map.rm.size; 1007f58a9d17SGeoff Levand else 10086bb5cf10SGeoff Levand len -= map.rm.size - r->offset; 10096bb5cf10SGeoff Levand result = dma_sb_map_area(r, virt_addr, len, &tmp, 10105c6fc8dbSGeert Uytterhoeven CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW | 10115c6fc8dbSGeert Uytterhoeven CBE_IOPTE_M); 1012f58a9d17SGeoff Levand BUG_ON(result); 10136bb5cf10SGeoff Levand } 1014f58a9d17SGeoff Levand 1015f58a9d17SGeoff Levand return result; 1016f58a9d17SGeoff Levand } 1017f58a9d17SGeoff Levand 1018f58a9d17SGeoff Levand /** 10196bb5cf10SGeoff Levand * dma_sb_region_free_linear - Free a linear dma mapping for a device. 1020f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 1021f58a9d17SGeoff Levand * 1022f58a9d17SGeoff Levand * This routine will unmap all mapped areas and free the HV dma region. 1023f58a9d17SGeoff Levand */ 1024f58a9d17SGeoff Levand 10256bb5cf10SGeoff Levand static int dma_sb_region_free_linear(struct ps3_dma_region *r) 1026f58a9d17SGeoff Levand { 1027f58a9d17SGeoff Levand int result; 1028494fd07aSStephen Rothwell dma_addr_t bus_addr; 1029494fd07aSStephen Rothwell unsigned long len, lpar_addr; 1030f58a9d17SGeoff Levand 10316bb5cf10SGeoff Levand if (r->offset < map.rm.size) { 10326bb5cf10SGeoff Levand /* Unmap (part of) 1st RAM chunk */ 10336bb5cf10SGeoff Levand lpar_addr = map.rm.base + r->offset; 10346bb5cf10SGeoff Levand len = map.rm.size - r->offset; 10356bb5cf10SGeoff Levand if (len > r->len) 10366bb5cf10SGeoff Levand len = r->len; 10376bb5cf10SGeoff Levand bus_addr = dma_sb_lpar_to_bus(r, lpar_addr); 10386bb5cf10SGeoff Levand result = dma_sb_unmap_area(r, bus_addr, len); 1039f58a9d17SGeoff Levand BUG_ON(result); 10406bb5cf10SGeoff Levand } 1041f58a9d17SGeoff Levand 10426bb5cf10SGeoff Levand if (r->offset + r->len > map.rm.size) { 10436bb5cf10SGeoff Levand /* Unmap (part of) 2nd RAM chunk */ 10446bb5cf10SGeoff Levand lpar_addr = map.r1.base; 10456bb5cf10SGeoff Levand len = r->len; 10466bb5cf10SGeoff Levand if (r->offset >= map.rm.size) 10476bb5cf10SGeoff Levand lpar_addr += r->offset - map.rm.size; 10486bb5cf10SGeoff Levand else 10496bb5cf10SGeoff Levand len -= map.rm.size - r->offset; 10506bb5cf10SGeoff Levand bus_addr = dma_sb_lpar_to_bus(r, lpar_addr); 10516bb5cf10SGeoff Levand result = dma_sb_unmap_area(r, bus_addr, len); 1052f58a9d17SGeoff Levand BUG_ON(result); 10536bb5cf10SGeoff Levand } 1054f58a9d17SGeoff Levand 10556bb5cf10SGeoff Levand result = dma_sb_region_free(r); 1056f58a9d17SGeoff Levand BUG_ON(result); 1057f58a9d17SGeoff Levand 1058f58a9d17SGeoff Levand return result; 1059f58a9d17SGeoff Levand } 1060f58a9d17SGeoff Levand 1061f58a9d17SGeoff Levand /** 10626bb5cf10SGeoff Levand * dma_sb_map_area_linear - Map an area of memory into a device dma region. 1063f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 1064f58a9d17SGeoff Levand * @virt_addr: Starting virtual address of the area to map. 1065f58a9d17SGeoff Levand * @len: Length in bytes of the area to map. 1066f58a9d17SGeoff Levand * @bus_addr: A pointer to return the starting ioc bus address of the area to 1067f58a9d17SGeoff Levand * map. 1068f58a9d17SGeoff Levand * 10696bb5cf10SGeoff Levand * This routine just returns the corresponding bus address. Actual mapping 1070f58a9d17SGeoff Levand * occurs in dma_region_create_linear(). 1071f58a9d17SGeoff Levand */ 1072f58a9d17SGeoff Levand 10736bb5cf10SGeoff Levand static int dma_sb_map_area_linear(struct ps3_dma_region *r, 1074494fd07aSStephen Rothwell unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr, 10756bb5cf10SGeoff Levand u64 iopte_flag) 1076f58a9d17SGeoff Levand { 1077f58a9d17SGeoff Levand unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr) 1078f58a9d17SGeoff Levand : virt_addr; 10796bb5cf10SGeoff Levand *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr)); 1080f58a9d17SGeoff Levand return 0; 1081f58a9d17SGeoff Levand } 1082f58a9d17SGeoff Levand 1083f58a9d17SGeoff Levand /** 1084f58a9d17SGeoff Levand * dma_unmap_area_linear - Unmap an area of memory from a device dma region. 1085f58a9d17SGeoff Levand * @r: Pointer to a struct ps3_dma_region. 1086f58a9d17SGeoff Levand * @bus_addr: The starting ioc bus address of the area to unmap. 1087f58a9d17SGeoff Levand * @len: Length in bytes of the area to unmap. 1088f58a9d17SGeoff Levand * 10896bb5cf10SGeoff Levand * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear(). 1090f58a9d17SGeoff Levand */ 1091f58a9d17SGeoff Levand 10926bb5cf10SGeoff Levand static int dma_sb_unmap_area_linear(struct ps3_dma_region *r, 1093494fd07aSStephen Rothwell dma_addr_t bus_addr, unsigned long len) 1094f58a9d17SGeoff Levand { 1095f58a9d17SGeoff Levand return 0; 10966bb5cf10SGeoff Levand }; 10976bb5cf10SGeoff Levand 10986bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = { 10996bb5cf10SGeoff Levand .create = dma_sb_region_create, 11006bb5cf10SGeoff Levand .free = dma_sb_region_free, 11016bb5cf10SGeoff Levand .map = dma_sb_map_area, 11026bb5cf10SGeoff Levand .unmap = dma_sb_unmap_area 11036bb5cf10SGeoff Levand }; 11046bb5cf10SGeoff Levand 11056bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = { 11066bb5cf10SGeoff Levand .create = dma_sb_region_create_linear, 11076bb5cf10SGeoff Levand .free = dma_sb_region_free_linear, 11086bb5cf10SGeoff Levand .map = dma_sb_map_area_linear, 11096bb5cf10SGeoff Levand .unmap = dma_sb_unmap_area_linear 11106bb5cf10SGeoff Levand }; 11116bb5cf10SGeoff Levand 11126bb5cf10SGeoff Levand static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = { 11136bb5cf10SGeoff Levand .create = dma_ioc0_region_create, 11146bb5cf10SGeoff Levand .free = dma_ioc0_region_free, 11156bb5cf10SGeoff Levand .map = dma_ioc0_map_area, 11166bb5cf10SGeoff Levand .unmap = dma_ioc0_unmap_area 11176bb5cf10SGeoff Levand }; 11186bb5cf10SGeoff Levand 11196bb5cf10SGeoff Levand int ps3_dma_region_init(struct ps3_system_bus_device *dev, 11206bb5cf10SGeoff Levand struct ps3_dma_region *r, enum ps3_dma_page_size page_size, 11216bb5cf10SGeoff Levand enum ps3_dma_region_type region_type, void *addr, unsigned long len) 11226bb5cf10SGeoff Levand { 11236bb5cf10SGeoff Levand unsigned long lpar_addr; 11249733862eSGeoff Levand int result; 11256bb5cf10SGeoff Levand 11266bb5cf10SGeoff Levand lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0; 11276bb5cf10SGeoff Levand 11286bb5cf10SGeoff Levand r->dev = dev; 11296bb5cf10SGeoff Levand r->page_size = page_size; 11306bb5cf10SGeoff Levand r->region_type = region_type; 11316bb5cf10SGeoff Levand r->offset = lpar_addr; 11326bb5cf10SGeoff Levand if (r->offset >= map.rm.size) 11336bb5cf10SGeoff Levand r->offset -= map.r1.offset; 1134b7115316SChristophe Leroy r->len = len ? len : ALIGN(map.total, 1 << r->page_size); 11356bb5cf10SGeoff Levand 11369733862eSGeoff Levand dev->core.dma_mask = &r->dma_mask; 11379733862eSGeoff Levand 11389733862eSGeoff Levand result = dma_set_mask_and_coherent(&dev->core, DMA_BIT_MASK(32)); 11399733862eSGeoff Levand 11409733862eSGeoff Levand if (result < 0) { 11419733862eSGeoff Levand dev_err(&dev->core, "%s:%d: dma_set_mask_and_coherent failed: %d\n", 11429733862eSGeoff Levand __func__, __LINE__, result); 11439733862eSGeoff Levand return result; 11449733862eSGeoff Levand } 11459733862eSGeoff Levand 11466bb5cf10SGeoff Levand switch (dev->dev_type) { 11476bb5cf10SGeoff Levand case PS3_DEVICE_TYPE_SB: 11486bb5cf10SGeoff Levand r->region_ops = (USE_DYNAMIC_DMA) 11496bb5cf10SGeoff Levand ? &ps3_dma_sb_region_ops 11506bb5cf10SGeoff Levand : &ps3_dma_sb_region_linear_ops; 11516bb5cf10SGeoff Levand break; 11526bb5cf10SGeoff Levand case PS3_DEVICE_TYPE_IOC0: 11536bb5cf10SGeoff Levand r->region_ops = &ps3_dma_ioc0_region_ops; 11546bb5cf10SGeoff Levand break; 11556bb5cf10SGeoff Levand default: 11566bb5cf10SGeoff Levand BUG(); 11576bb5cf10SGeoff Levand return -EINVAL; 1158f58a9d17SGeoff Levand } 11596bb5cf10SGeoff Levand return 0; 11606bb5cf10SGeoff Levand } 11616bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_init); 1162f58a9d17SGeoff Levand 1163f58a9d17SGeoff Levand int ps3_dma_region_create(struct ps3_dma_region *r) 1164f58a9d17SGeoff Levand { 11656bb5cf10SGeoff Levand BUG_ON(!r); 11666bb5cf10SGeoff Levand BUG_ON(!r->region_ops); 11676bb5cf10SGeoff Levand BUG_ON(!r->region_ops->create); 11686bb5cf10SGeoff Levand return r->region_ops->create(r); 1169f58a9d17SGeoff Levand } 11706bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_create); 1171f58a9d17SGeoff Levand 1172f58a9d17SGeoff Levand int ps3_dma_region_free(struct ps3_dma_region *r) 1173f58a9d17SGeoff Levand { 11746bb5cf10SGeoff Levand BUG_ON(!r); 11756bb5cf10SGeoff Levand BUG_ON(!r->region_ops); 11766bb5cf10SGeoff Levand BUG_ON(!r->region_ops->free); 11776bb5cf10SGeoff Levand return r->region_ops->free(r); 1178f58a9d17SGeoff Levand } 11796bb5cf10SGeoff Levand EXPORT_SYMBOL(ps3_dma_region_free); 1180f58a9d17SGeoff Levand 1181f58a9d17SGeoff Levand int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr, 1182494fd07aSStephen Rothwell unsigned long len, dma_addr_t *bus_addr, 11836bb5cf10SGeoff Levand u64 iopte_flag) 1184f58a9d17SGeoff Levand { 11856bb5cf10SGeoff Levand return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag); 1186f58a9d17SGeoff Levand } 1187f58a9d17SGeoff Levand 1188494fd07aSStephen Rothwell int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr, 1189f58a9d17SGeoff Levand unsigned long len) 1190f58a9d17SGeoff Levand { 11916bb5cf10SGeoff Levand return r->region_ops->unmap(r, bus_addr, len); 1192f58a9d17SGeoff Levand } 1193f58a9d17SGeoff Levand 1194f58a9d17SGeoff Levand /*============================================================================*/ 1195f58a9d17SGeoff Levand /* system startup routines */ 1196f58a9d17SGeoff Levand /*============================================================================*/ 1197f58a9d17SGeoff Levand 1198f58a9d17SGeoff Levand /** 1199f58a9d17SGeoff Levand * ps3_mm_init - initialize the address space state variables 1200f58a9d17SGeoff Levand */ 1201f58a9d17SGeoff Levand 1202f58a9d17SGeoff Levand void __init ps3_mm_init(void) 1203f58a9d17SGeoff Levand { 1204f58a9d17SGeoff Levand int result; 1205f58a9d17SGeoff Levand 1206f58a9d17SGeoff Levand DBG(" -> %s:%d\n", __func__, __LINE__); 1207f58a9d17SGeoff Levand 1208f58a9d17SGeoff Levand result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size, 1209f58a9d17SGeoff Levand &map.total); 1210f58a9d17SGeoff Levand 1211f58a9d17SGeoff Levand if (result) 1212f58a9d17SGeoff Levand panic("ps3_repository_read_mm_info() failed"); 1213f58a9d17SGeoff Levand 1214f58a9d17SGeoff Levand map.rm.offset = map.rm.base; 1215f58a9d17SGeoff Levand map.vas_id = map.htab_size = 0; 1216f58a9d17SGeoff Levand 1217f58a9d17SGeoff Levand /* this implementation assumes map.rm.base is zero */ 1218f58a9d17SGeoff Levand 1219f58a9d17SGeoff Levand BUG_ON(map.rm.base); 1220f58a9d17SGeoff Levand BUG_ON(!map.rm.size); 1221f58a9d17SGeoff Levand 12221e755c09SAndre Heider /* Check if we got the highmem region from an earlier boot step */ 1223f58a9d17SGeoff Levand 12245ae74630SGeoff Levand if (ps3_mm_get_repository_highmem(&map.r1)) { 12255ae74630SGeoff Levand result = ps3_mm_region_create(&map.r1, map.total - map.rm.size); 12265ae74630SGeoff Levand 12275ae74630SGeoff Levand if (!result) 12285ae74630SGeoff Levand ps3_mm_set_repository_highmem(&map.r1); 12295ae74630SGeoff Levand } 1230f58a9d17SGeoff Levand 12316bb5cf10SGeoff Levand /* correct map.total for the real total amount of memory we use */ 12326bb5cf10SGeoff Levand map.total = map.rm.size + map.r1.size; 12336bb5cf10SGeoff Levand 12348ac5fd11SHector Martin if (!map.r1.size) { 12358ac5fd11SHector Martin DBG("%s:%d: No highmem region found\n", __func__, __LINE__); 12368ac5fd11SHector Martin } else { 12378ac5fd11SHector Martin DBG("%s:%d: Adding highmem region: %llxh %llxh\n", 12388ac5fd11SHector Martin __func__, __LINE__, map.rm.size, 12398ac5fd11SHector Martin map.total - map.rm.size); 12408ac5fd11SHector Martin memblock_add(map.rm.size, map.total - map.rm.size); 12418ac5fd11SHector Martin } 12428ac5fd11SHector Martin 1243f58a9d17SGeoff Levand DBG(" <- %s:%d\n", __func__, __LINE__); 1244f58a9d17SGeoff Levand } 1245f58a9d17SGeoff Levand 1246f58a9d17SGeoff Levand /** 1247f58a9d17SGeoff Levand * ps3_mm_shutdown - final cleanup of address space 12488119cefdSHari Bathini * 12498119cefdSHari Bathini * called during kexec sequence with MMU off. 1250f58a9d17SGeoff Levand */ 1251f58a9d17SGeoff Levand 12528119cefdSHari Bathini notrace void ps3_mm_shutdown(void) 1253f58a9d17SGeoff Levand { 1254f58a9d17SGeoff Levand ps3_mm_region_destroy(&map.r1); 1255f58a9d17SGeoff Levand } 1256