Thomas Gleixner | 873e65b | 2019-05-27 08:55:15 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 2 | /* |
| 3 | * PS3 address space management. |
| 4 | * |
| 5 | * Copyright (C) 2006 Sony Computer Entertainment Inc. |
| 6 | * Copyright 2006 Sony Corp. |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Geoff Levand | 9733862 | 2021-06-03 19:17:02 +0000 | [diff] [blame] | 9 | #include <linux/dma-mapping.h> |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Paul Gortmaker | 4b16f8e | 2011-07-22 18:24:23 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 12 | #include <linux/memblock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 14 | |
Geert Uytterhoeven | 9413c88 | 2009-07-29 02:06:42 +0000 | [diff] [blame] | 15 | #include <asm/cell-regs.h> |
Arnd Bergmann | e22ba7e | 2006-11-27 19:18:57 +0100 | [diff] [blame] | 16 | #include <asm/firmware.h> |
David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 17 | #include <asm/prom.h> |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 18 | #include <asm/udbg.h> |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 19 | #include <asm/lv1call.h> |
David Howells | ae3a197 | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 20 | #include <asm/setup.h> |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 21 | |
| 22 | #include "platform.h" |
| 23 | |
| 24 | #if defined(DEBUG) |
Geert Uytterhoeven | 83bb643 | 2007-06-16 07:19:23 +1000 | [diff] [blame] | 25 | #define DBG udbg_printf |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 26 | #else |
Michael Ellerman | 7424639 | 2009-06-22 15:56:39 +0000 | [diff] [blame] | 27 | #define DBG pr_devel |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 28 | #endif |
| 29 | |
| 30 | enum { |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 31 | #if defined(CONFIG_PS3_DYNAMIC_DMA) |
| 32 | USE_DYNAMIC_DMA = 1, |
| 33 | #else |
| 34 | USE_DYNAMIC_DMA = 0, |
| 35 | #endif |
| 36 | }; |
| 37 | |
| 38 | enum { |
| 39 | PAGE_SHIFT_4K = 12U, |
| 40 | PAGE_SHIFT_64K = 16U, |
| 41 | PAGE_SHIFT_16M = 24U, |
| 42 | }; |
| 43 | |
Nick Child | f1ba9b9 | 2021-12-16 17:00:28 -0500 | [diff] [blame] | 44 | static unsigned long __init make_page_sizes(unsigned long a, unsigned long b) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 45 | { |
| 46 | return (a << 56) | (b << 48); |
| 47 | } |
| 48 | |
| 49 | enum { |
| 50 | ALLOCATE_MEMORY_TRY_ALT_UNIT = 0X04, |
| 51 | ALLOCATE_MEMORY_ADDR_ZERO = 0X08, |
| 52 | }; |
| 53 | |
| 54 | /* valid htab sizes are {18,19,20} = 256K, 512K, 1M */ |
| 55 | |
| 56 | enum { |
| 57 | HTAB_SIZE_MAX = 20U, /* HV limit of 1MB */ |
| 58 | HTAB_SIZE_MIN = 18U, /* CPU limit of 256KB */ |
| 59 | }; |
| 60 | |
| 61 | /*============================================================================*/ |
| 62 | /* virtual address space routines */ |
| 63 | /*============================================================================*/ |
| 64 | |
| 65 | /** |
| 66 | * struct mem_region - memory region structure |
| 67 | * @base: base address |
| 68 | * @size: size in bytes |
| 69 | * @offset: difference between base and rm.size |
Andre Heider | 1e755c0 | 2011-08-11 21:31:07 +0200 | [diff] [blame] | 70 | * @destroy: flag if region should be destroyed upon shutdown |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 71 | */ |
| 72 | |
| 73 | struct mem_region { |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 74 | u64 base; |
Stephen Rothwell | 5418b9c | 2009-01-13 20:01:28 +0000 | [diff] [blame] | 75 | u64 size; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 76 | unsigned long offset; |
Andre Heider | 1e755c0 | 2011-08-11 21:31:07 +0200 | [diff] [blame] | 77 | int destroy; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /** |
| 81 | * struct map - address space state variables holder |
| 82 | * @total: total memory available as reported by HV |
| 83 | * @vas_id - HV virtual address space id |
| 84 | * @htab_size: htab size in bytes |
| 85 | * |
| 86 | * The HV virtual address space (vas) allows for hotplug memory regions. |
| 87 | * Memory regions can be created and destroyed in the vas at runtime. |
| 88 | * @rm: real mode (bootmem) region |
Hector Martin | 8ac5fd1 | 2011-08-11 21:31:08 +0200 | [diff] [blame] | 89 | * @r1: highmem region(s) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 90 | * |
| 91 | * ps3 addresses |
| 92 | * virt_addr: a cpu 'translated' effective address |
| 93 | * phys_addr: an address in what Linux thinks is the physical address space |
| 94 | * lpar_addr: an address in the HV virtual address space |
| 95 | * bus_addr: an io controller 'translated' address on a device bus |
| 96 | */ |
| 97 | |
| 98 | struct map { |
Stephen Rothwell | 5418b9c | 2009-01-13 20:01:28 +0000 | [diff] [blame] | 99 | u64 total; |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 100 | u64 vas_id; |
| 101 | u64 htab_size; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 102 | struct mem_region rm; |
| 103 | struct mem_region r1; |
| 104 | }; |
| 105 | |
| 106 | #define debug_dump_map(x) _debug_dump_map(x, __func__, __LINE__) |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 107 | static void __maybe_unused _debug_dump_map(const struct map *m, |
| 108 | const char *func, int line) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 109 | { |
Stephen Rothwell | 5418b9c | 2009-01-13 20:01:28 +0000 | [diff] [blame] | 110 | DBG("%s:%d: map.total = %llxh\n", func, line, m->total); |
| 111 | DBG("%s:%d: map.rm.size = %llxh\n", func, line, m->rm.size); |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 112 | DBG("%s:%d: map.vas_id = %llu\n", func, line, m->vas_id); |
| 113 | DBG("%s:%d: map.htab_size = %llxh\n", func, line, m->htab_size); |
| 114 | DBG("%s:%d: map.r1.base = %llxh\n", func, line, m->r1.base); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 115 | DBG("%s:%d: map.r1.offset = %lxh\n", func, line, m->r1.offset); |
Stephen Rothwell | 5418b9c | 2009-01-13 20:01:28 +0000 | [diff] [blame] | 116 | DBG("%s:%d: map.r1.size = %llxh\n", func, line, m->r1.size); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static struct map map; |
| 120 | |
| 121 | /** |
| 122 | * ps3_mm_phys_to_lpar - translate a linux physical address to lpar address |
| 123 | * @phys_addr: linux physical address |
| 124 | */ |
| 125 | |
| 126 | unsigned long ps3_mm_phys_to_lpar(unsigned long phys_addr) |
| 127 | { |
| 128 | BUG_ON(is_kernel_addr(phys_addr)); |
Geoff Levand | a628df1 | 2008-01-19 07:32:59 +1100 | [diff] [blame] | 129 | return (phys_addr < map.rm.size || phys_addr >= map.total) |
| 130 | ? phys_addr : phys_addr + map.r1.offset; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | EXPORT_SYMBOL(ps3_mm_phys_to_lpar); |
| 134 | |
| 135 | /** |
| 136 | * ps3_mm_vas_create - create the virtual address space |
| 137 | */ |
| 138 | |
| 139 | void __init ps3_mm_vas_create(unsigned long* htab_size) |
| 140 | { |
| 141 | int result; |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 142 | u64 start_address; |
| 143 | u64 size; |
| 144 | u64 access_right; |
| 145 | u64 max_page_size; |
| 146 | u64 flags; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 147 | |
| 148 | result = lv1_query_logical_partition_address_region_info(0, |
| 149 | &start_address, &size, &access_right, &max_page_size, |
| 150 | &flags); |
| 151 | |
| 152 | if (result) { |
| 153 | DBG("%s:%d: lv1_query_logical_partition_address_region_info " |
| 154 | "failed: %s\n", __func__, __LINE__, |
| 155 | ps3_result(result)); |
| 156 | goto fail; |
| 157 | } |
| 158 | |
| 159 | if (max_page_size < PAGE_SHIFT_16M) { |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 160 | DBG("%s:%d: bad max_page_size %llxh\n", __func__, __LINE__, |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 161 | max_page_size); |
| 162 | goto fail; |
| 163 | } |
| 164 | |
| 165 | BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE > HTAB_SIZE_MAX); |
| 166 | BUILD_BUG_ON(CONFIG_PS3_HTAB_SIZE < HTAB_SIZE_MIN); |
| 167 | |
| 168 | result = lv1_construct_virtual_address_space(CONFIG_PS3_HTAB_SIZE, |
| 169 | 2, make_page_sizes(PAGE_SHIFT_16M, PAGE_SHIFT_64K), |
| 170 | &map.vas_id, &map.htab_size); |
| 171 | |
| 172 | if (result) { |
| 173 | DBG("%s:%d: lv1_construct_virtual_address_space failed: %s\n", |
| 174 | __func__, __LINE__, ps3_result(result)); |
| 175 | goto fail; |
| 176 | } |
| 177 | |
| 178 | result = lv1_select_virtual_address_space(map.vas_id); |
| 179 | |
| 180 | if (result) { |
| 181 | DBG("%s:%d: lv1_select_virtual_address_space failed: %s\n", |
| 182 | __func__, __LINE__, ps3_result(result)); |
| 183 | goto fail; |
| 184 | } |
| 185 | |
| 186 | *htab_size = map.htab_size; |
| 187 | |
| 188 | debug_dump_map(&map); |
| 189 | |
| 190 | return; |
| 191 | |
| 192 | fail: |
| 193 | panic("ps3_mm_vas_create failed"); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * ps3_mm_vas_destroy - |
Hari Bathini | 8119cef | 2021-07-14 18:17:58 +0530 | [diff] [blame] | 198 | * |
| 199 | * called during kexec sequence with MMU off. |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 200 | */ |
| 201 | |
Hari Bathini | 8119cef | 2021-07-14 18:17:58 +0530 | [diff] [blame] | 202 | notrace void ps3_mm_vas_destroy(void) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 203 | { |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 204 | int result; |
| 205 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 206 | if (map.vas_id) { |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 207 | result = lv1_select_virtual_address_space(0); |
Geoff Levand | 12655446 | 2020-05-09 18:58:32 +0000 | [diff] [blame] | 208 | result += lv1_destruct_virtual_address_space(map.vas_id); |
| 209 | |
| 210 | if (result) { |
| 211 | lv1_panic(0); |
| 212 | } |
| 213 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 214 | map.vas_id = 0; |
| 215 | } |
| 216 | } |
| 217 | |
Nick Child | f1ba9b9 | 2021-12-16 17:00:28 -0500 | [diff] [blame] | 218 | static int __init ps3_mm_get_repository_highmem(struct mem_region *r) |
Geoff Levand | d4b18bd | 2015-01-13 01:00:20 +0000 | [diff] [blame] | 219 | { |
| 220 | int result; |
| 221 | |
| 222 | /* Assume a single highmem region. */ |
| 223 | |
| 224 | result = ps3_repository_read_highmem_info(0, &r->base, &r->size); |
| 225 | |
| 226 | if (result) |
| 227 | goto zero_region; |
| 228 | |
| 229 | if (!r->base || !r->size) { |
| 230 | result = -1; |
| 231 | goto zero_region; |
| 232 | } |
| 233 | |
| 234 | r->offset = r->base - map.rm.size; |
| 235 | |
| 236 | DBG("%s:%d: Found high region in repository: %llxh %llxh\n", |
| 237 | __func__, __LINE__, r->base, r->size); |
| 238 | |
| 239 | return 0; |
| 240 | |
| 241 | zero_region: |
| 242 | DBG("%s:%d: No high region in repository.\n", __func__, __LINE__); |
| 243 | |
| 244 | r->size = r->base = r->offset = 0; |
| 245 | return result; |
| 246 | } |
| 247 | |
| 248 | static int ps3_mm_set_repository_highmem(const struct mem_region *r) |
| 249 | { |
| 250 | /* Assume a single highmem region. */ |
| 251 | |
| 252 | return r ? ps3_repository_write_highmem_info(0, r->base, r->size) : |
| 253 | ps3_repository_write_highmem_info(0, 0, 0); |
| 254 | } |
| 255 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 256 | /** |
| 257 | * ps3_mm_region_create - create a memory region in the vas |
| 258 | * @r: pointer to a struct mem_region to accept initialized values |
| 259 | * @size: requested region size |
| 260 | * |
| 261 | * This implementation creates the region with the vas large page size. |
| 262 | * @size is rounded down to a multiple of the vas large page size. |
| 263 | */ |
| 264 | |
Geert Uytterhoeven | 32f44a1 | 2007-06-16 08:07:23 +1000 | [diff] [blame] | 265 | static int ps3_mm_region_create(struct mem_region *r, unsigned long size) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 266 | { |
| 267 | int result; |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 268 | u64 muid; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 269 | |
Christophe Leroy | e96d904 | 2020-04-20 18:36:35 +0000 | [diff] [blame] | 270 | r->size = ALIGN_DOWN(size, 1 << PAGE_SHIFT_16M); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 271 | |
| 272 | DBG("%s:%d requested %lxh\n", __func__, __LINE__, size); |
Stephen Rothwell | 5418b9c | 2009-01-13 20:01:28 +0000 | [diff] [blame] | 273 | DBG("%s:%d actual %llxh\n", __func__, __LINE__, r->size); |
| 274 | DBG("%s:%d difference %llxh (%lluMB)\n", __func__, __LINE__, |
| 275 | size - r->size, (size - r->size) / 1024 / 1024); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 276 | |
| 277 | if (r->size == 0) { |
| 278 | DBG("%s:%d: size == 0\n", __func__, __LINE__); |
| 279 | result = -1; |
| 280 | goto zero_region; |
| 281 | } |
| 282 | |
| 283 | result = lv1_allocate_memory(r->size, PAGE_SHIFT_16M, 0, |
| 284 | ALLOCATE_MEMORY_TRY_ALT_UNIT, &r->base, &muid); |
| 285 | |
| 286 | if (result || r->base < map.rm.size) { |
| 287 | DBG("%s:%d: lv1_allocate_memory failed: %s\n", |
| 288 | __func__, __LINE__, ps3_result(result)); |
| 289 | goto zero_region; |
| 290 | } |
| 291 | |
Andre Heider | 1e755c0 | 2011-08-11 21:31:07 +0200 | [diff] [blame] | 292 | r->destroy = 1; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 293 | r->offset = r->base - map.rm.size; |
| 294 | return result; |
| 295 | |
| 296 | zero_region: |
| 297 | r->size = r->base = r->offset = 0; |
| 298 | return result; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * ps3_mm_region_destroy - destroy a memory region |
| 303 | * @r: pointer to struct mem_region |
| 304 | */ |
| 305 | |
Geert Uytterhoeven | 32f44a1 | 2007-06-16 08:07:23 +1000 | [diff] [blame] | 306 | static void ps3_mm_region_destroy(struct mem_region *r) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 307 | { |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 308 | int result; |
| 309 | |
Andre Heider | 1e755c0 | 2011-08-11 21:31:07 +0200 | [diff] [blame] | 310 | if (!r->destroy) { |
Andre Heider | 1e755c0 | 2011-08-11 21:31:07 +0200 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 314 | if (r->base) { |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 315 | result = lv1_release_memory(r->base); |
Geoff Levand | 12655446 | 2020-05-09 18:58:32 +0000 | [diff] [blame] | 316 | |
| 317 | if (result) { |
| 318 | lv1_panic(0); |
| 319 | } |
| 320 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 321 | r->size = r->base = r->offset = 0; |
| 322 | map.total = map.rm.size; |
| 323 | } |
Geoff Levand | 12655446 | 2020-05-09 18:58:32 +0000 | [diff] [blame] | 324 | |
Geoff Levand | 5ae7463 | 2015-01-13 01:00:20 +0000 | [diff] [blame] | 325 | ps3_mm_set_repository_highmem(NULL); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 326 | } |
| 327 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 328 | /*============================================================================*/ |
| 329 | /* dma routines */ |
| 330 | /*============================================================================*/ |
| 331 | |
| 332 | /** |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 333 | * dma_sb_lpar_to_bus - Translate an lpar address to ioc mapped bus address. |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 334 | * @r: pointer to dma region structure |
| 335 | * @lpar_addr: HV lpar address |
| 336 | */ |
| 337 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 338 | static unsigned long dma_sb_lpar_to_bus(struct ps3_dma_region *r, |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 339 | unsigned long lpar_addr) |
| 340 | { |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 341 | if (lpar_addr >= map.rm.size) |
| 342 | lpar_addr -= map.r1.offset; |
| 343 | BUG_ON(lpar_addr < r->offset); |
| 344 | BUG_ON(lpar_addr >= r->offset + r->len); |
| 345 | return r->bus_addr + lpar_addr - r->offset; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | #define dma_dump_region(_a) _dma_dump_region(_a, __func__, __LINE__) |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 349 | static void __maybe_unused _dma_dump_region(const struct ps3_dma_region *r, |
| 350 | const char *func, int line) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 351 | { |
Stephen Rothwell | 5c94907 | 2009-01-13 20:02:39 +0000 | [diff] [blame] | 352 | DBG("%s:%d: dev %llu:%llu\n", func, line, r->dev->bus_id, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 353 | r->dev->dev_id); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 354 | DBG("%s:%d: page_size %u\n", func, line, r->page_size); |
| 355 | DBG("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr); |
| 356 | DBG("%s:%d: len %lxh\n", func, line, r->len); |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 357 | DBG("%s:%d: offset %lxh\n", func, line, r->offset); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 360 | /** |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 361 | * dma_chunk - A chunk of dma pages mapped by the io controller. |
| 362 | * @region - The dma region that owns this chunk. |
| 363 | * @lpar_addr: Starting lpar address of the area to map. |
| 364 | * @bus_addr: Starting ioc bus address of the area to map. |
| 365 | * @len: Length in bytes of the area to map. |
| 366 | * @link: A struct list_head used with struct ps3_dma_region.chunk_list, the |
| 367 | * list of all chuncks owned by the region. |
| 368 | * |
| 369 | * This implementation uses a very simple dma page manager |
| 370 | * based on the dma_chunk structure. This scheme assumes |
| 371 | * that all drivers use very well behaved dma ops. |
| 372 | */ |
| 373 | |
| 374 | struct dma_chunk { |
| 375 | struct ps3_dma_region *region; |
| 376 | unsigned long lpar_addr; |
| 377 | unsigned long bus_addr; |
| 378 | unsigned long len; |
| 379 | struct list_head link; |
| 380 | unsigned int usage_count; |
| 381 | }; |
| 382 | |
| 383 | #define dma_dump_chunk(_a) _dma_dump_chunk(_a, __func__, __LINE__) |
| 384 | static void _dma_dump_chunk (const struct dma_chunk* c, const char* func, |
| 385 | int line) |
| 386 | { |
Stephen Rothwell | 5c94907 | 2009-01-13 20:02:39 +0000 | [diff] [blame] | 387 | DBG("%s:%d: r.dev %llu:%llu\n", func, line, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 388 | c->region->dev->bus_id, c->region->dev->dev_id); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 389 | DBG("%s:%d: r.bus_addr %lxh\n", func, line, c->region->bus_addr); |
| 390 | DBG("%s:%d: r.page_size %u\n", func, line, c->region->page_size); |
| 391 | DBG("%s:%d: r.len %lxh\n", func, line, c->region->len); |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 392 | DBG("%s:%d: r.offset %lxh\n", func, line, c->region->offset); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 393 | DBG("%s:%d: c.lpar_addr %lxh\n", func, line, c->lpar_addr); |
| 394 | DBG("%s:%d: c.bus_addr %lxh\n", func, line, c->bus_addr); |
| 395 | DBG("%s:%d: c.len %lxh\n", func, line, c->len); |
| 396 | } |
| 397 | |
| 398 | static struct dma_chunk * dma_find_chunk(struct ps3_dma_region *r, |
| 399 | unsigned long bus_addr, unsigned long len) |
| 400 | { |
| 401 | struct dma_chunk *c; |
Christophe Leroy | e96d904 | 2020-04-20 18:36:35 +0000 | [diff] [blame] | 402 | unsigned long aligned_bus = ALIGN_DOWN(bus_addr, 1 << r->page_size); |
Christophe Leroy | b711531 | 2020-04-20 18:36:36 +0000 | [diff] [blame] | 403 | unsigned long aligned_len = ALIGN(len+bus_addr-aligned_bus, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 404 | 1 << r->page_size); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 405 | |
| 406 | list_for_each_entry(c, &r->chunk_list.head, link) { |
| 407 | /* intersection */ |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 408 | if (aligned_bus >= c->bus_addr && |
| 409 | aligned_bus + aligned_len <= c->bus_addr + c->len) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 410 | return c; |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 411 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 412 | /* below */ |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 413 | if (aligned_bus + aligned_len <= c->bus_addr) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 414 | continue; |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 415 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 416 | /* above */ |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 417 | if (aligned_bus >= c->bus_addr + c->len) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 418 | continue; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 419 | |
| 420 | /* we don't handle the multi-chunk case for now */ |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 421 | dma_dump_chunk(c); |
| 422 | BUG(); |
| 423 | } |
| 424 | return NULL; |
| 425 | } |
| 426 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 427 | static struct dma_chunk *dma_find_chunk_lpar(struct ps3_dma_region *r, |
| 428 | unsigned long lpar_addr, unsigned long len) |
| 429 | { |
| 430 | struct dma_chunk *c; |
Christophe Leroy | e96d904 | 2020-04-20 18:36:35 +0000 | [diff] [blame] | 431 | unsigned long aligned_lpar = ALIGN_DOWN(lpar_addr, 1 << r->page_size); |
Christophe Leroy | b711531 | 2020-04-20 18:36:36 +0000 | [diff] [blame] | 432 | unsigned long aligned_len = ALIGN(len + lpar_addr - aligned_lpar, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 433 | 1 << r->page_size); |
| 434 | |
| 435 | list_for_each_entry(c, &r->chunk_list.head, link) { |
| 436 | /* intersection */ |
| 437 | if (c->lpar_addr <= aligned_lpar && |
| 438 | aligned_lpar < c->lpar_addr + c->len) { |
| 439 | if (aligned_lpar + aligned_len <= c->lpar_addr + c->len) |
| 440 | return c; |
| 441 | else { |
| 442 | dma_dump_chunk(c); |
| 443 | BUG(); |
| 444 | } |
| 445 | } |
| 446 | /* below */ |
| 447 | if (aligned_lpar + aligned_len <= c->lpar_addr) { |
| 448 | continue; |
| 449 | } |
| 450 | /* above */ |
| 451 | if (c->lpar_addr + c->len <= aligned_lpar) { |
| 452 | continue; |
| 453 | } |
| 454 | } |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | static int dma_sb_free_chunk(struct dma_chunk *c) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 459 | { |
| 460 | int result = 0; |
| 461 | |
| 462 | if (c->bus_addr) { |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 463 | result = lv1_unmap_device_dma_region(c->region->dev->bus_id, |
| 464 | c->region->dev->dev_id, c->bus_addr, c->len); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 465 | BUG_ON(result); |
| 466 | } |
| 467 | |
| 468 | kfree(c); |
| 469 | return result; |
| 470 | } |
| 471 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 472 | static int dma_ioc0_free_chunk(struct dma_chunk *c) |
| 473 | { |
| 474 | int result = 0; |
| 475 | int iopage; |
| 476 | unsigned long offset; |
| 477 | struct ps3_dma_region *r = c->region; |
| 478 | |
| 479 | DBG("%s:start\n", __func__); |
| 480 | for (iopage = 0; iopage < (c->len >> r->page_size); iopage++) { |
| 481 | offset = (1 << r->page_size) * iopage; |
| 482 | /* put INVALID entry */ |
| 483 | result = lv1_put_iopte(0, |
| 484 | c->bus_addr + offset, |
| 485 | c->lpar_addr + offset, |
| 486 | r->ioid, |
| 487 | 0); |
| 488 | DBG("%s: bus=%#lx, lpar=%#lx, ioid=%d\n", __func__, |
| 489 | c->bus_addr + offset, |
| 490 | c->lpar_addr + offset, |
| 491 | r->ioid); |
| 492 | |
| 493 | if (result) { |
| 494 | DBG("%s:%d: lv1_put_iopte failed: %s\n", __func__, |
| 495 | __LINE__, ps3_result(result)); |
| 496 | } |
| 497 | } |
| 498 | kfree(c); |
| 499 | DBG("%s:end\n", __func__); |
| 500 | return result; |
| 501 | } |
| 502 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 503 | /** |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 504 | * dma_sb_map_pages - Maps dma pages into the io controller bus address space. |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 505 | * @r: Pointer to a struct ps3_dma_region. |
| 506 | * @phys_addr: Starting physical address of the area to map. |
| 507 | * @len: Length in bytes of the area to map. |
| 508 | * c_out: A pointer to receive an allocated struct dma_chunk for this area. |
| 509 | * |
| 510 | * This is the lowest level dma mapping routine, and is the one that will |
| 511 | * make the HV call to add the pages into the io controller address space. |
| 512 | */ |
| 513 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 514 | static int dma_sb_map_pages(struct ps3_dma_region *r, unsigned long phys_addr, |
| 515 | unsigned long len, struct dma_chunk **c_out, u64 iopte_flag) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 516 | { |
| 517 | int result; |
| 518 | struct dma_chunk *c; |
| 519 | |
Markus Elfring | a0828cf | 2017-01-19 17:15:30 +0100 | [diff] [blame] | 520 | c = kzalloc(sizeof(*c), GFP_ATOMIC); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 521 | if (!c) { |
| 522 | result = -ENOMEM; |
| 523 | goto fail_alloc; |
| 524 | } |
| 525 | |
| 526 | c->region = r; |
| 527 | c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr); |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 528 | c->bus_addr = dma_sb_lpar_to_bus(r, c->lpar_addr); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 529 | c->len = len; |
| 530 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 531 | BUG_ON(iopte_flag != 0xf800000000000000UL); |
| 532 | result = lv1_map_device_dma_region(c->region->dev->bus_id, |
| 533 | c->region->dev->dev_id, c->lpar_addr, |
| 534 | c->bus_addr, c->len, iopte_flag); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 535 | if (result) { |
| 536 | DBG("%s:%d: lv1_map_device_dma_region failed: %s\n", |
| 537 | __func__, __LINE__, ps3_result(result)); |
| 538 | goto fail_map; |
| 539 | } |
| 540 | |
| 541 | list_add(&c->link, &r->chunk_list.head); |
| 542 | |
| 543 | *c_out = c; |
| 544 | return 0; |
| 545 | |
| 546 | fail_map: |
| 547 | kfree(c); |
| 548 | fail_alloc: |
| 549 | *c_out = NULL; |
| 550 | DBG(" <- %s:%d\n", __func__, __LINE__); |
| 551 | return result; |
| 552 | } |
| 553 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 554 | static int dma_ioc0_map_pages(struct ps3_dma_region *r, unsigned long phys_addr, |
| 555 | unsigned long len, struct dma_chunk **c_out, |
| 556 | u64 iopte_flag) |
| 557 | { |
| 558 | int result; |
| 559 | struct dma_chunk *c, *last; |
| 560 | int iopage, pages; |
| 561 | unsigned long offset; |
| 562 | |
| 563 | DBG(KERN_ERR "%s: phy=%#lx, lpar%#lx, len=%#lx\n", __func__, |
| 564 | phys_addr, ps3_mm_phys_to_lpar(phys_addr), len); |
Markus Elfring | a0828cf | 2017-01-19 17:15:30 +0100 | [diff] [blame] | 565 | c = kzalloc(sizeof(*c), GFP_ATOMIC); |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 566 | if (!c) { |
| 567 | result = -ENOMEM; |
| 568 | goto fail_alloc; |
| 569 | } |
| 570 | |
| 571 | c->region = r; |
| 572 | c->len = len; |
| 573 | c->lpar_addr = ps3_mm_phys_to_lpar(phys_addr); |
| 574 | /* allocate IO address */ |
| 575 | if (list_empty(&r->chunk_list.head)) { |
| 576 | /* first one */ |
| 577 | c->bus_addr = r->bus_addr; |
| 578 | } else { |
| 579 | /* derive from last bus addr*/ |
| 580 | last = list_entry(r->chunk_list.head.next, |
| 581 | struct dma_chunk, link); |
| 582 | c->bus_addr = last->bus_addr + last->len; |
| 583 | DBG("%s: last bus=%#lx, len=%#lx\n", __func__, |
| 584 | last->bus_addr, last->len); |
| 585 | } |
| 586 | |
| 587 | /* FIXME: check whether length exceeds region size */ |
| 588 | |
| 589 | /* build ioptes for the area */ |
| 590 | pages = len >> r->page_size; |
Stephen Rothwell | 5c94907 | 2009-01-13 20:02:39 +0000 | [diff] [blame] | 591 | DBG("%s: pgsize=%#x len=%#lx pages=%#x iopteflag=%#llx\n", __func__, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 592 | r->page_size, r->len, pages, iopte_flag); |
| 593 | for (iopage = 0; iopage < pages; iopage++) { |
| 594 | offset = (1 << r->page_size) * iopage; |
| 595 | result = lv1_put_iopte(0, |
| 596 | c->bus_addr + offset, |
| 597 | c->lpar_addr + offset, |
| 598 | r->ioid, |
| 599 | iopte_flag); |
| 600 | if (result) { |
Joe Perches | f2c2cbc | 2016-10-24 21:00:08 -0700 | [diff] [blame] | 601 | pr_warn("%s:%d: lv1_put_iopte failed: %s\n", |
| 602 | __func__, __LINE__, ps3_result(result)); |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 603 | goto fail_map; |
| 604 | } |
| 605 | DBG("%s: pg=%d bus=%#lx, lpar=%#lx, ioid=%#x\n", __func__, |
| 606 | iopage, c->bus_addr + offset, c->lpar_addr + offset, |
| 607 | r->ioid); |
| 608 | } |
| 609 | |
| 610 | /* be sure that last allocated one is inserted at head */ |
| 611 | list_add(&c->link, &r->chunk_list.head); |
| 612 | |
| 613 | *c_out = c; |
| 614 | DBG("%s: end\n", __func__); |
| 615 | return 0; |
| 616 | |
| 617 | fail_map: |
| 618 | for (iopage--; 0 <= iopage; iopage--) { |
| 619 | lv1_put_iopte(0, |
| 620 | c->bus_addr + offset, |
| 621 | c->lpar_addr + offset, |
| 622 | r->ioid, |
| 623 | 0); |
| 624 | } |
| 625 | kfree(c); |
| 626 | fail_alloc: |
| 627 | *c_out = NULL; |
| 628 | return result; |
| 629 | } |
| 630 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 631 | /** |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 632 | * dma_sb_region_create - Create a device dma region. |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 633 | * @r: Pointer to a struct ps3_dma_region. |
| 634 | * |
| 635 | * This is the lowest level dma region create routine, and is the one that |
| 636 | * will make the HV call to create the region. |
| 637 | */ |
| 638 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 639 | static int dma_sb_region_create(struct ps3_dma_region *r) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 640 | { |
| 641 | int result; |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 642 | u64 bus_addr; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 643 | |
Geoff Levand | 62d8074 | 2008-12-03 13:52:15 +0000 | [diff] [blame] | 644 | DBG(" -> %s:%d:\n", __func__, __LINE__); |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 645 | |
| 646 | BUG_ON(!r); |
| 647 | |
| 648 | if (!r->dev->bus_id) { |
Stephen Rothwell | 5c94907 | 2009-01-13 20:02:39 +0000 | [diff] [blame] | 649 | pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 650 | r->dev->bus_id, r->dev->dev_id); |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | DBG("%s:%u: len = 0x%lx, page_size = %u, offset = 0x%lx\n", __func__, |
| 655 | __LINE__, r->len, r->page_size, r->offset); |
| 656 | |
| 657 | BUG_ON(!r->len); |
| 658 | BUG_ON(!r->page_size); |
| 659 | BUG_ON(!r->region_ops); |
| 660 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 661 | INIT_LIST_HEAD(&r->chunk_list.head); |
| 662 | spin_lock_init(&r->chunk_list.lock); |
| 663 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 664 | result = lv1_allocate_device_dma_region(r->dev->bus_id, r->dev->dev_id, |
| 665 | roundup_pow_of_two(r->len), r->page_size, r->region_type, |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 666 | &bus_addr); |
| 667 | r->bus_addr = bus_addr; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 668 | |
| 669 | if (result) { |
| 670 | DBG("%s:%d: lv1_allocate_device_dma_region failed: %s\n", |
| 671 | __func__, __LINE__, ps3_result(result)); |
| 672 | r->len = r->bus_addr = 0; |
| 673 | } |
| 674 | |
| 675 | return result; |
| 676 | } |
| 677 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 678 | static int dma_ioc0_region_create(struct ps3_dma_region *r) |
| 679 | { |
| 680 | int result; |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 681 | u64 bus_addr; |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 682 | |
| 683 | INIT_LIST_HEAD(&r->chunk_list.head); |
| 684 | spin_lock_init(&r->chunk_list.lock); |
| 685 | |
| 686 | result = lv1_allocate_io_segment(0, |
| 687 | r->len, |
| 688 | r->page_size, |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 689 | &bus_addr); |
| 690 | r->bus_addr = bus_addr; |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 691 | if (result) { |
| 692 | DBG("%s:%d: lv1_allocate_io_segment failed: %s\n", |
| 693 | __func__, __LINE__, ps3_result(result)); |
| 694 | r->len = r->bus_addr = 0; |
| 695 | } |
| 696 | DBG("%s: len=%#lx, pg=%d, bus=%#lx\n", __func__, |
| 697 | r->len, r->page_size, r->bus_addr); |
| 698 | return result; |
| 699 | } |
| 700 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 701 | /** |
| 702 | * dma_region_free - Free a device dma region. |
| 703 | * @r: Pointer to a struct ps3_dma_region. |
| 704 | * |
| 705 | * This is the lowest level dma region free routine, and is the one that |
| 706 | * will make the HV call to free the region. |
| 707 | */ |
| 708 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 709 | static int dma_sb_region_free(struct ps3_dma_region *r) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 710 | { |
| 711 | int result; |
| 712 | struct dma_chunk *c; |
| 713 | struct dma_chunk *tmp; |
| 714 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 715 | BUG_ON(!r); |
| 716 | |
| 717 | if (!r->dev->bus_id) { |
Stephen Rothwell | 5c94907 | 2009-01-13 20:02:39 +0000 | [diff] [blame] | 718 | pr_info("%s:%d: %llu:%llu no dma\n", __func__, __LINE__, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 719 | r->dev->bus_id, r->dev->dev_id); |
| 720 | return 0; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 721 | } |
| 722 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 723 | list_for_each_entry_safe(c, tmp, &r->chunk_list.head, link) { |
| 724 | list_del(&c->link); |
| 725 | dma_sb_free_chunk(c); |
| 726 | } |
| 727 | |
| 728 | result = lv1_free_device_dma_region(r->dev->bus_id, r->dev->dev_id, |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 729 | r->bus_addr); |
| 730 | |
| 731 | if (result) |
| 732 | DBG("%s:%d: lv1_free_device_dma_region failed: %s\n", |
| 733 | __func__, __LINE__, ps3_result(result)); |
| 734 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 735 | r->bus_addr = 0; |
| 736 | |
| 737 | return result; |
| 738 | } |
| 739 | |
| 740 | static int dma_ioc0_region_free(struct ps3_dma_region *r) |
| 741 | { |
| 742 | int result; |
| 743 | struct dma_chunk *c, *n; |
| 744 | |
| 745 | DBG("%s: start\n", __func__); |
| 746 | list_for_each_entry_safe(c, n, &r->chunk_list.head, link) { |
| 747 | list_del(&c->link); |
| 748 | dma_ioc0_free_chunk(c); |
| 749 | } |
| 750 | |
| 751 | result = lv1_release_io_segment(0, r->bus_addr); |
| 752 | |
| 753 | if (result) |
| 754 | DBG("%s:%d: lv1_free_device_dma_region failed: %s\n", |
| 755 | __func__, __LINE__, ps3_result(result)); |
| 756 | |
| 757 | r->bus_addr = 0; |
| 758 | DBG("%s: end\n", __func__); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 759 | |
| 760 | return result; |
| 761 | } |
| 762 | |
| 763 | /** |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 764 | * dma_sb_map_area - Map an area of memory into a device dma region. |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 765 | * @r: Pointer to a struct ps3_dma_region. |
| 766 | * @virt_addr: Starting virtual address of the area to map. |
| 767 | * @len: Length in bytes of the area to map. |
| 768 | * @bus_addr: A pointer to return the starting ioc bus address of the area to |
| 769 | * map. |
| 770 | * |
| 771 | * This is the common dma mapping routine. |
| 772 | */ |
| 773 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 774 | static int dma_sb_map_area(struct ps3_dma_region *r, unsigned long virt_addr, |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 775 | unsigned long len, dma_addr_t *bus_addr, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 776 | u64 iopte_flag) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 777 | { |
| 778 | int result; |
| 779 | unsigned long flags; |
| 780 | struct dma_chunk *c; |
| 781 | unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr) |
| 782 | : virt_addr; |
Christophe Leroy | e96d904 | 2020-04-20 18:36:35 +0000 | [diff] [blame] | 783 | unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size); |
Christophe Leroy | b711531 | 2020-04-20 18:36:36 +0000 | [diff] [blame] | 784 | unsigned long aligned_len = ALIGN(len + phys_addr - aligned_phys, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 785 | 1 << r->page_size); |
| 786 | *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr)); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 787 | |
| 788 | if (!USE_DYNAMIC_DMA) { |
| 789 | unsigned long lpar_addr = ps3_mm_phys_to_lpar(phys_addr); |
| 790 | DBG(" -> %s:%d\n", __func__, __LINE__); |
| 791 | DBG("%s:%d virt_addr %lxh\n", __func__, __LINE__, |
| 792 | virt_addr); |
| 793 | DBG("%s:%d phys_addr %lxh\n", __func__, __LINE__, |
| 794 | phys_addr); |
| 795 | DBG("%s:%d lpar_addr %lxh\n", __func__, __LINE__, |
| 796 | lpar_addr); |
| 797 | DBG("%s:%d len %lxh\n", __func__, __LINE__, len); |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 798 | DBG("%s:%d bus_addr %llxh (%lxh)\n", __func__, __LINE__, |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 799 | *bus_addr, len); |
| 800 | } |
| 801 | |
| 802 | spin_lock_irqsave(&r->chunk_list.lock, flags); |
| 803 | c = dma_find_chunk(r, *bus_addr, len); |
| 804 | |
| 805 | if (c) { |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 806 | DBG("%s:%d: reusing mapped chunk", __func__, __LINE__); |
| 807 | dma_dump_chunk(c); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 808 | c->usage_count++; |
| 809 | spin_unlock_irqrestore(&r->chunk_list.lock, flags); |
| 810 | return 0; |
| 811 | } |
| 812 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 813 | result = dma_sb_map_pages(r, aligned_phys, aligned_len, &c, iopte_flag); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 814 | |
| 815 | if (result) { |
| 816 | *bus_addr = 0; |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 817 | DBG("%s:%d: dma_sb_map_pages failed (%d)\n", |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 818 | __func__, __LINE__, result); |
| 819 | spin_unlock_irqrestore(&r->chunk_list.lock, flags); |
| 820 | return result; |
| 821 | } |
| 822 | |
| 823 | c->usage_count = 1; |
| 824 | |
| 825 | spin_unlock_irqrestore(&r->chunk_list.lock, flags); |
| 826 | return result; |
| 827 | } |
| 828 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 829 | static int dma_ioc0_map_area(struct ps3_dma_region *r, unsigned long virt_addr, |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 830 | unsigned long len, dma_addr_t *bus_addr, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 831 | u64 iopte_flag) |
| 832 | { |
| 833 | int result; |
| 834 | unsigned long flags; |
| 835 | struct dma_chunk *c; |
| 836 | unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr) |
| 837 | : virt_addr; |
Christophe Leroy | e96d904 | 2020-04-20 18:36:35 +0000 | [diff] [blame] | 838 | unsigned long aligned_phys = ALIGN_DOWN(phys_addr, 1 << r->page_size); |
Christophe Leroy | b711531 | 2020-04-20 18:36:36 +0000 | [diff] [blame] | 839 | unsigned long aligned_len = ALIGN(len + phys_addr - aligned_phys, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 840 | 1 << r->page_size); |
| 841 | |
| 842 | DBG(KERN_ERR "%s: vaddr=%#lx, len=%#lx\n", __func__, |
| 843 | virt_addr, len); |
| 844 | DBG(KERN_ERR "%s: ph=%#lx a_ph=%#lx a_l=%#lx\n", __func__, |
| 845 | phys_addr, aligned_phys, aligned_len); |
| 846 | |
| 847 | spin_lock_irqsave(&r->chunk_list.lock, flags); |
| 848 | c = dma_find_chunk_lpar(r, ps3_mm_phys_to_lpar(phys_addr), len); |
| 849 | |
| 850 | if (c) { |
| 851 | /* FIXME */ |
| 852 | BUG(); |
| 853 | *bus_addr = c->bus_addr + phys_addr - aligned_phys; |
| 854 | c->usage_count++; |
| 855 | spin_unlock_irqrestore(&r->chunk_list.lock, flags); |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | result = dma_ioc0_map_pages(r, aligned_phys, aligned_len, &c, |
| 860 | iopte_flag); |
| 861 | |
| 862 | if (result) { |
| 863 | *bus_addr = 0; |
| 864 | DBG("%s:%d: dma_ioc0_map_pages failed (%d)\n", |
| 865 | __func__, __LINE__, result); |
| 866 | spin_unlock_irqrestore(&r->chunk_list.lock, flags); |
| 867 | return result; |
| 868 | } |
| 869 | *bus_addr = c->bus_addr + phys_addr - aligned_phys; |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 870 | DBG("%s: va=%#lx pa=%#lx a_pa=%#lx bus=%#llx\n", __func__, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 871 | virt_addr, phys_addr, aligned_phys, *bus_addr); |
| 872 | c->usage_count = 1; |
| 873 | |
| 874 | spin_unlock_irqrestore(&r->chunk_list.lock, flags); |
| 875 | return result; |
| 876 | } |
| 877 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 878 | /** |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 879 | * dma_sb_unmap_area - Unmap an area of memory from a device dma region. |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 880 | * @r: Pointer to a struct ps3_dma_region. |
| 881 | * @bus_addr: The starting ioc bus address of the area to unmap. |
| 882 | * @len: Length in bytes of the area to unmap. |
| 883 | * |
| 884 | * This is the common dma unmap routine. |
| 885 | */ |
| 886 | |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 887 | static int dma_sb_unmap_area(struct ps3_dma_region *r, dma_addr_t bus_addr, |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 888 | unsigned long len) |
| 889 | { |
| 890 | unsigned long flags; |
| 891 | struct dma_chunk *c; |
| 892 | |
| 893 | spin_lock_irqsave(&r->chunk_list.lock, flags); |
| 894 | c = dma_find_chunk(r, bus_addr, len); |
| 895 | |
| 896 | if (!c) { |
Christophe Leroy | e96d904 | 2020-04-20 18:36:35 +0000 | [diff] [blame] | 897 | unsigned long aligned_bus = ALIGN_DOWN(bus_addr, |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 898 | 1 << r->page_size); |
Christophe Leroy | b711531 | 2020-04-20 18:36:36 +0000 | [diff] [blame] | 899 | unsigned long aligned_len = ALIGN(len + bus_addr |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 900 | - aligned_bus, 1 << r->page_size); |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 901 | DBG("%s:%d: not found: bus_addr %llxh\n", |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 902 | __func__, __LINE__, bus_addr); |
| 903 | DBG("%s:%d: not found: len %lxh\n", |
| 904 | __func__, __LINE__, len); |
| 905 | DBG("%s:%d: not found: aligned_bus %lxh\n", |
| 906 | __func__, __LINE__, aligned_bus); |
| 907 | DBG("%s:%d: not found: aligned_len %lxh\n", |
| 908 | __func__, __LINE__, aligned_len); |
| 909 | BUG(); |
| 910 | } |
| 911 | |
| 912 | c->usage_count--; |
| 913 | |
| 914 | if (!c->usage_count) { |
| 915 | list_del(&c->link); |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 916 | dma_sb_free_chunk(c); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | spin_unlock_irqrestore(&r->chunk_list.lock, flags); |
| 920 | return 0; |
| 921 | } |
| 922 | |
Geert Uytterhoeven | 32f44a1 | 2007-06-16 08:07:23 +1000 | [diff] [blame] | 923 | static int dma_ioc0_unmap_area(struct ps3_dma_region *r, |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 924 | dma_addr_t bus_addr, unsigned long len) |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 925 | { |
| 926 | unsigned long flags; |
| 927 | struct dma_chunk *c; |
| 928 | |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 929 | DBG("%s: start a=%#llx l=%#lx\n", __func__, bus_addr, len); |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 930 | spin_lock_irqsave(&r->chunk_list.lock, flags); |
| 931 | c = dma_find_chunk(r, bus_addr, len); |
| 932 | |
| 933 | if (!c) { |
Christophe Leroy | e96d904 | 2020-04-20 18:36:35 +0000 | [diff] [blame] | 934 | unsigned long aligned_bus = ALIGN_DOWN(bus_addr, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 935 | 1 << r->page_size); |
Christophe Leroy | b711531 | 2020-04-20 18:36:36 +0000 | [diff] [blame] | 936 | unsigned long aligned_len = ALIGN(len + bus_addr |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 937 | - aligned_bus, |
| 938 | 1 << r->page_size); |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 939 | DBG("%s:%d: not found: bus_addr %llxh\n", |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 940 | __func__, __LINE__, bus_addr); |
| 941 | DBG("%s:%d: not found: len %lxh\n", |
| 942 | __func__, __LINE__, len); |
| 943 | DBG("%s:%d: not found: aligned_bus %lxh\n", |
| 944 | __func__, __LINE__, aligned_bus); |
| 945 | DBG("%s:%d: not found: aligned_len %lxh\n", |
| 946 | __func__, __LINE__, aligned_len); |
| 947 | BUG(); |
| 948 | } |
| 949 | |
| 950 | c->usage_count--; |
| 951 | |
| 952 | if (!c->usage_count) { |
| 953 | list_del(&c->link); |
| 954 | dma_ioc0_free_chunk(c); |
| 955 | } |
| 956 | |
| 957 | spin_unlock_irqrestore(&r->chunk_list.lock, flags); |
| 958 | DBG("%s: end\n", __func__); |
| 959 | return 0; |
| 960 | } |
| 961 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 962 | /** |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 963 | * dma_sb_region_create_linear - Setup a linear dma mapping for a device. |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 964 | * @r: Pointer to a struct ps3_dma_region. |
| 965 | * |
| 966 | * This routine creates an HV dma region for the device and maps all available |
| 967 | * ram into the io controller bus address space. |
| 968 | */ |
| 969 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 970 | static int dma_sb_region_create_linear(struct ps3_dma_region *r) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 971 | { |
| 972 | int result; |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 973 | unsigned long virt_addr, len; |
| 974 | dma_addr_t tmp; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 975 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 976 | if (r->len > 16*1024*1024) { /* FIXME: need proper fix */ |
| 977 | /* force 16M dma pages for linear mapping */ |
| 978 | if (r->page_size != PS3_DMA_16M) { |
| 979 | pr_info("%s:%d: forcing 16M pages for linear map\n", |
| 980 | __func__, __LINE__); |
| 981 | r->page_size = PS3_DMA_16M; |
Christophe Leroy | b711531 | 2020-04-20 18:36:36 +0000 | [diff] [blame] | 982 | r->len = ALIGN(r->len, 1 << r->page_size); |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 983 | } |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 984 | } |
| 985 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 986 | result = dma_sb_region_create(r); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 987 | BUG_ON(result); |
| 988 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 989 | if (r->offset < map.rm.size) { |
| 990 | /* Map (part of) 1st RAM chunk */ |
| 991 | virt_addr = map.rm.base + r->offset; |
| 992 | len = map.rm.size - r->offset; |
| 993 | if (len > r->len) |
| 994 | len = r->len; |
| 995 | result = dma_sb_map_area(r, virt_addr, len, &tmp, |
Geert Uytterhoeven | 5c6fc8d | 2009-06-10 04:38:45 +0000 | [diff] [blame] | 996 | CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW | |
| 997 | CBE_IOPTE_M); |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 998 | BUG_ON(result); |
| 999 | } |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1000 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1001 | if (r->offset + r->len > map.rm.size) { |
| 1002 | /* Map (part of) 2nd RAM chunk */ |
Geoff Levand | a628df1 | 2008-01-19 07:32:59 +1100 | [diff] [blame] | 1003 | virt_addr = map.rm.size; |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1004 | len = r->len; |
| 1005 | if (r->offset >= map.rm.size) |
| 1006 | virt_addr += r->offset - map.rm.size; |
| 1007 | else |
| 1008 | len -= map.rm.size - r->offset; |
| 1009 | result = dma_sb_map_area(r, virt_addr, len, &tmp, |
Geert Uytterhoeven | 5c6fc8d | 2009-06-10 04:38:45 +0000 | [diff] [blame] | 1010 | CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_SO_RW | |
| 1011 | CBE_IOPTE_M); |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1012 | BUG_ON(result); |
| 1013 | } |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1014 | |
| 1015 | return result; |
| 1016 | } |
| 1017 | |
| 1018 | /** |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1019 | * dma_sb_region_free_linear - Free a linear dma mapping for a device. |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1020 | * @r: Pointer to a struct ps3_dma_region. |
| 1021 | * |
| 1022 | * This routine will unmap all mapped areas and free the HV dma region. |
| 1023 | */ |
| 1024 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1025 | static int dma_sb_region_free_linear(struct ps3_dma_region *r) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1026 | { |
| 1027 | int result; |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 1028 | dma_addr_t bus_addr; |
| 1029 | unsigned long len, lpar_addr; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1030 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1031 | if (r->offset < map.rm.size) { |
| 1032 | /* Unmap (part of) 1st RAM chunk */ |
| 1033 | lpar_addr = map.rm.base + r->offset; |
| 1034 | len = map.rm.size - r->offset; |
| 1035 | if (len > r->len) |
| 1036 | len = r->len; |
| 1037 | bus_addr = dma_sb_lpar_to_bus(r, lpar_addr); |
| 1038 | result = dma_sb_unmap_area(r, bus_addr, len); |
| 1039 | BUG_ON(result); |
| 1040 | } |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1041 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1042 | if (r->offset + r->len > map.rm.size) { |
| 1043 | /* Unmap (part of) 2nd RAM chunk */ |
| 1044 | lpar_addr = map.r1.base; |
| 1045 | len = r->len; |
| 1046 | if (r->offset >= map.rm.size) |
| 1047 | lpar_addr += r->offset - map.rm.size; |
| 1048 | else |
| 1049 | len -= map.rm.size - r->offset; |
| 1050 | bus_addr = dma_sb_lpar_to_bus(r, lpar_addr); |
| 1051 | result = dma_sb_unmap_area(r, bus_addr, len); |
| 1052 | BUG_ON(result); |
| 1053 | } |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1054 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1055 | result = dma_sb_region_free(r); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1056 | BUG_ON(result); |
| 1057 | |
| 1058 | return result; |
| 1059 | } |
| 1060 | |
| 1061 | /** |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1062 | * dma_sb_map_area_linear - Map an area of memory into a device dma region. |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1063 | * @r: Pointer to a struct ps3_dma_region. |
| 1064 | * @virt_addr: Starting virtual address of the area to map. |
| 1065 | * @len: Length in bytes of the area to map. |
| 1066 | * @bus_addr: A pointer to return the starting ioc bus address of the area to |
| 1067 | * map. |
| 1068 | * |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1069 | * This routine just returns the corresponding bus address. Actual mapping |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1070 | * occurs in dma_region_create_linear(). |
| 1071 | */ |
| 1072 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1073 | static int dma_sb_map_area_linear(struct ps3_dma_region *r, |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 1074 | unsigned long virt_addr, unsigned long len, dma_addr_t *bus_addr, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1075 | u64 iopte_flag) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1076 | { |
| 1077 | unsigned long phys_addr = is_kernel_addr(virt_addr) ? __pa(virt_addr) |
| 1078 | : virt_addr; |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1079 | *bus_addr = dma_sb_lpar_to_bus(r, ps3_mm_phys_to_lpar(phys_addr)); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1080 | return 0; |
| 1081 | } |
| 1082 | |
| 1083 | /** |
| 1084 | * dma_unmap_area_linear - Unmap an area of memory from a device dma region. |
| 1085 | * @r: Pointer to a struct ps3_dma_region. |
| 1086 | * @bus_addr: The starting ioc bus address of the area to unmap. |
| 1087 | * @len: Length in bytes of the area to unmap. |
| 1088 | * |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1089 | * This routine does nothing. Unmapping occurs in dma_sb_region_free_linear(). |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1090 | */ |
| 1091 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1092 | static int dma_sb_unmap_area_linear(struct ps3_dma_region *r, |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 1093 | dma_addr_t bus_addr, unsigned long len) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1094 | { |
| 1095 | return 0; |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1096 | }; |
| 1097 | |
| 1098 | static const struct ps3_dma_region_ops ps3_dma_sb_region_ops = { |
| 1099 | .create = dma_sb_region_create, |
| 1100 | .free = dma_sb_region_free, |
| 1101 | .map = dma_sb_map_area, |
| 1102 | .unmap = dma_sb_unmap_area |
| 1103 | }; |
| 1104 | |
| 1105 | static const struct ps3_dma_region_ops ps3_dma_sb_region_linear_ops = { |
| 1106 | .create = dma_sb_region_create_linear, |
| 1107 | .free = dma_sb_region_free_linear, |
| 1108 | .map = dma_sb_map_area_linear, |
| 1109 | .unmap = dma_sb_unmap_area_linear |
| 1110 | }; |
| 1111 | |
| 1112 | static const struct ps3_dma_region_ops ps3_dma_ioc0_region_ops = { |
| 1113 | .create = dma_ioc0_region_create, |
| 1114 | .free = dma_ioc0_region_free, |
| 1115 | .map = dma_ioc0_map_area, |
| 1116 | .unmap = dma_ioc0_unmap_area |
| 1117 | }; |
| 1118 | |
| 1119 | int ps3_dma_region_init(struct ps3_system_bus_device *dev, |
| 1120 | struct ps3_dma_region *r, enum ps3_dma_page_size page_size, |
| 1121 | enum ps3_dma_region_type region_type, void *addr, unsigned long len) |
| 1122 | { |
| 1123 | unsigned long lpar_addr; |
Geoff Levand | 9733862 | 2021-06-03 19:17:02 +0000 | [diff] [blame] | 1124 | int result; |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1125 | |
| 1126 | lpar_addr = addr ? ps3_mm_phys_to_lpar(__pa(addr)) : 0; |
| 1127 | |
| 1128 | r->dev = dev; |
| 1129 | r->page_size = page_size; |
| 1130 | r->region_type = region_type; |
| 1131 | r->offset = lpar_addr; |
| 1132 | if (r->offset >= map.rm.size) |
| 1133 | r->offset -= map.r1.offset; |
Christophe Leroy | b711531 | 2020-04-20 18:36:36 +0000 | [diff] [blame] | 1134 | r->len = len ? len : ALIGN(map.total, 1 << r->page_size); |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1135 | |
Geoff Levand | 9733862 | 2021-06-03 19:17:02 +0000 | [diff] [blame] | 1136 | dev->core.dma_mask = &r->dma_mask; |
| 1137 | |
| 1138 | result = dma_set_mask_and_coherent(&dev->core, DMA_BIT_MASK(32)); |
| 1139 | |
| 1140 | if (result < 0) { |
| 1141 | dev_err(&dev->core, "%s:%d: dma_set_mask_and_coherent failed: %d\n", |
| 1142 | __func__, __LINE__, result); |
| 1143 | return result; |
| 1144 | } |
| 1145 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1146 | switch (dev->dev_type) { |
| 1147 | case PS3_DEVICE_TYPE_SB: |
| 1148 | r->region_ops = (USE_DYNAMIC_DMA) |
| 1149 | ? &ps3_dma_sb_region_ops |
| 1150 | : &ps3_dma_sb_region_linear_ops; |
| 1151 | break; |
| 1152 | case PS3_DEVICE_TYPE_IOC0: |
| 1153 | r->region_ops = &ps3_dma_ioc0_region_ops; |
| 1154 | break; |
| 1155 | default: |
| 1156 | BUG(); |
| 1157 | return -EINVAL; |
| 1158 | } |
| 1159 | return 0; |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1160 | } |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1161 | EXPORT_SYMBOL(ps3_dma_region_init); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1162 | |
| 1163 | int ps3_dma_region_create(struct ps3_dma_region *r) |
| 1164 | { |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1165 | BUG_ON(!r); |
| 1166 | BUG_ON(!r->region_ops); |
| 1167 | BUG_ON(!r->region_ops->create); |
| 1168 | return r->region_ops->create(r); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1169 | } |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1170 | EXPORT_SYMBOL(ps3_dma_region_create); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1171 | |
| 1172 | int ps3_dma_region_free(struct ps3_dma_region *r) |
| 1173 | { |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1174 | BUG_ON(!r); |
| 1175 | BUG_ON(!r->region_ops); |
| 1176 | BUG_ON(!r->region_ops->free); |
| 1177 | return r->region_ops->free(r); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1178 | } |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1179 | EXPORT_SYMBOL(ps3_dma_region_free); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1180 | |
| 1181 | int ps3_dma_map(struct ps3_dma_region *r, unsigned long virt_addr, |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 1182 | unsigned long len, dma_addr_t *bus_addr, |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1183 | u64 iopte_flag) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1184 | { |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1185 | return r->region_ops->map(r, virt_addr, len, bus_addr, iopte_flag); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1186 | } |
| 1187 | |
Stephen Rothwell | 494fd07a | 2009-01-13 19:58:10 +0000 | [diff] [blame] | 1188 | int ps3_dma_unmap(struct ps3_dma_region *r, dma_addr_t bus_addr, |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1189 | unsigned long len) |
| 1190 | { |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1191 | return r->region_ops->unmap(r, bus_addr, len); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /*============================================================================*/ |
| 1195 | /* system startup routines */ |
| 1196 | /*============================================================================*/ |
| 1197 | |
| 1198 | /** |
| 1199 | * ps3_mm_init - initialize the address space state variables |
| 1200 | */ |
| 1201 | |
| 1202 | void __init ps3_mm_init(void) |
| 1203 | { |
| 1204 | int result; |
| 1205 | |
| 1206 | DBG(" -> %s:%d\n", __func__, __LINE__); |
| 1207 | |
| 1208 | result = ps3_repository_read_mm_info(&map.rm.base, &map.rm.size, |
| 1209 | &map.total); |
| 1210 | |
| 1211 | if (result) |
| 1212 | panic("ps3_repository_read_mm_info() failed"); |
| 1213 | |
| 1214 | map.rm.offset = map.rm.base; |
| 1215 | map.vas_id = map.htab_size = 0; |
| 1216 | |
| 1217 | /* this implementation assumes map.rm.base is zero */ |
| 1218 | |
| 1219 | BUG_ON(map.rm.base); |
| 1220 | BUG_ON(!map.rm.size); |
| 1221 | |
Andre Heider | 1e755c0 | 2011-08-11 21:31:07 +0200 | [diff] [blame] | 1222 | /* Check if we got the highmem region from an earlier boot step */ |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1223 | |
Geoff Levand | 5ae7463 | 2015-01-13 01:00:20 +0000 | [diff] [blame] | 1224 | if (ps3_mm_get_repository_highmem(&map.r1)) { |
| 1225 | result = ps3_mm_region_create(&map.r1, map.total - map.rm.size); |
| 1226 | |
| 1227 | if (!result) |
| 1228 | ps3_mm_set_repository_highmem(&map.r1); |
| 1229 | } |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1230 | |
Geoff Levand | 6bb5cf1 | 2007-06-16 07:52:02 +1000 | [diff] [blame] | 1231 | /* correct map.total for the real total amount of memory we use */ |
| 1232 | map.total = map.rm.size + map.r1.size; |
| 1233 | |
Hector Martin | 8ac5fd1 | 2011-08-11 21:31:08 +0200 | [diff] [blame] | 1234 | if (!map.r1.size) { |
| 1235 | DBG("%s:%d: No highmem region found\n", __func__, __LINE__); |
| 1236 | } else { |
| 1237 | DBG("%s:%d: Adding highmem region: %llxh %llxh\n", |
| 1238 | __func__, __LINE__, map.rm.size, |
| 1239 | map.total - map.rm.size); |
| 1240 | memblock_add(map.rm.size, map.total - map.rm.size); |
| 1241 | } |
| 1242 | |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1243 | DBG(" <- %s:%d\n", __func__, __LINE__); |
| 1244 | } |
| 1245 | |
| 1246 | /** |
| 1247 | * ps3_mm_shutdown - final cleanup of address space |
Hari Bathini | 8119cef | 2021-07-14 18:17:58 +0530 | [diff] [blame] | 1248 | * |
| 1249 | * called during kexec sequence with MMU off. |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1250 | */ |
| 1251 | |
Hari Bathini | 8119cef | 2021-07-14 18:17:58 +0530 | [diff] [blame] | 1252 | notrace void ps3_mm_shutdown(void) |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1253 | { |
| 1254 | ps3_mm_region_destroy(&map.r1); |
Geoff Levand | f58a9d1 | 2006-11-23 00:46:51 +0100 | [diff] [blame] | 1255 | } |