Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011-2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/genalloc.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/iommu.h> |
| 20 | #include <linux/msm_kgsl.h> |
| 21 | #include <linux/ratelimit.h> |
| 22 | #include <linux/of_platform.h> |
| 23 | #include <soc/qcom/scm.h> |
| 24 | #include <soc/qcom/secure_buffer.h> |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 25 | #include <linux/compat.h> |
| 26 | |
| 27 | #include "kgsl.h" |
| 28 | #include "kgsl_device.h" |
| 29 | #include "kgsl_mmu.h" |
| 30 | #include "kgsl_sharedmem.h" |
| 31 | #include "kgsl_iommu.h" |
| 32 | #include "adreno_pm4types.h" |
| 33 | #include "adreno.h" |
| 34 | #include "kgsl_trace.h" |
| 35 | #include "kgsl_pwrctrl.h" |
| 36 | |
Shrenuj Bansal | 9a0563b | 2017-06-15 14:45:15 -0700 | [diff] [blame] | 37 | #define CP_APERTURE_REG 0 |
| 38 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 39 | #define _IOMMU_PRIV(_mmu) (&((_mmu)->priv.iommu)) |
| 40 | |
| 41 | #define ADDR_IN_GLOBAL(_a) \ |
| 42 | (((_a) >= KGSL_IOMMU_GLOBAL_MEM_BASE) && \ |
| 43 | ((_a) < (KGSL_IOMMU_GLOBAL_MEM_BASE + KGSL_IOMMU_GLOBAL_MEM_SIZE))) |
| 44 | |
Sushmita Susheelendra | 7f66cf7 | 2016-09-12 11:04:43 -0600 | [diff] [blame] | 45 | /* |
| 46 | * Flag to set SMMU memory attributes required to |
| 47 | * enable system cache for GPU transactions. |
| 48 | */ |
| 49 | #ifndef IOMMU_USE_UPSTREAM_HINT |
| 50 | #define IOMMU_USE_UPSTREAM_HINT 0 |
| 51 | #endif |
| 52 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 53 | static struct kgsl_mmu_pt_ops iommu_pt_ops; |
| 54 | static bool need_iommu_sync; |
| 55 | |
| 56 | const unsigned int kgsl_iommu_reg_list[KGSL_IOMMU_REG_MAX] = { |
| 57 | 0x0,/* SCTLR */ |
| 58 | 0x20,/* TTBR0 */ |
| 59 | 0x34,/* CONTEXTIDR */ |
| 60 | 0x58,/* FSR */ |
| 61 | 0x60,/* FAR_0 */ |
| 62 | 0x618,/* TLBIALL */ |
| 63 | 0x008,/* RESUME */ |
| 64 | 0x68,/* FSYNR0 */ |
| 65 | 0x6C,/* FSYNR1 */ |
| 66 | 0x7F0,/* TLBSYNC */ |
| 67 | 0x7F4,/* TLBSTATUS */ |
| 68 | }; |
| 69 | |
| 70 | /* |
| 71 | * struct kgsl_iommu_addr_entry - entry in the kgsl_iommu_pt rbtree. |
| 72 | * @base: starting virtual address of the entry |
| 73 | * @size: size of the entry |
| 74 | * @node: the rbtree node |
| 75 | * |
| 76 | */ |
| 77 | struct kgsl_iommu_addr_entry { |
| 78 | uint64_t base; |
| 79 | uint64_t size; |
| 80 | struct rb_node node; |
| 81 | }; |
| 82 | |
| 83 | static struct kmem_cache *addr_entry_cache; |
| 84 | |
| 85 | /* |
| 86 | * There are certain memory allocations (ringbuffer, memstore, etc) that need to |
| 87 | * be present at the same address in every pagetable. We call these "global" |
| 88 | * pagetable entries. There are relatively few of these and they are mostly |
| 89 | * stable (defined at init time) but the actual number of globals can differ |
| 90 | * slight depending on the target and implementation. |
| 91 | * |
| 92 | * Here we define an array and a simple allocator to keep track of the currently |
| 93 | * active global entries. Each entry is assigned a unique address inside of a |
| 94 | * MMU implementation specific "global" region. The addresses are assigned |
| 95 | * sequentially and never re-used to avoid having to go back and reprogram |
| 96 | * existing pagetables. The entire list of active entries are mapped and |
| 97 | * unmapped into every new pagetable as it is created and destroyed. |
| 98 | * |
| 99 | * Because there are relatively few entries and they are defined at boot time we |
| 100 | * don't need to go over the top to define a dynamic allocation scheme. It will |
| 101 | * be less wasteful to pick a static number with a little bit of growth |
| 102 | * potential. |
| 103 | */ |
| 104 | |
| 105 | #define GLOBAL_PT_ENTRIES 32 |
| 106 | |
| 107 | struct global_pt_entry { |
| 108 | struct kgsl_memdesc *memdesc; |
| 109 | char name[32]; |
| 110 | }; |
| 111 | |
| 112 | static struct global_pt_entry global_pt_entries[GLOBAL_PT_ENTRIES]; |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 113 | static int secure_global_size; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 114 | static int global_pt_count; |
| 115 | uint64_t global_pt_alloc; |
| 116 | static struct kgsl_memdesc gpu_qdss_desc; |
Jonathan Wicks | 4892d8d | 2017-02-24 16:21:26 -0700 | [diff] [blame] | 117 | static struct kgsl_memdesc gpu_qtimer_desc; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 118 | |
| 119 | void kgsl_print_global_pt_entries(struct seq_file *s) |
| 120 | { |
| 121 | int i; |
| 122 | |
| 123 | for (i = 0; i < global_pt_count; i++) { |
| 124 | struct kgsl_memdesc *memdesc = global_pt_entries[i].memdesc; |
| 125 | |
| 126 | if (memdesc == NULL) |
| 127 | continue; |
| 128 | |
Hareesh Gundu | 1fbd906 | 2017-11-01 18:47:45 +0530 | [diff] [blame] | 129 | seq_printf(s, "0x%pK-0x%pK %16llu %s\n", |
| 130 | (uint64_t *)(uintptr_t) memdesc->gpuaddr, |
| 131 | (uint64_t *)(uintptr_t) (memdesc->gpuaddr + |
| 132 | memdesc->size - 1), memdesc->size, |
| 133 | global_pt_entries[i].name); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | static void kgsl_iommu_unmap_globals(struct kgsl_pagetable *pagetable) |
| 138 | { |
| 139 | unsigned int i; |
| 140 | |
| 141 | for (i = 0; i < global_pt_count; i++) { |
| 142 | if (global_pt_entries[i].memdesc != NULL) |
| 143 | kgsl_mmu_unmap(pagetable, |
| 144 | global_pt_entries[i].memdesc); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static int kgsl_iommu_map_globals(struct kgsl_pagetable *pagetable) |
| 149 | { |
| 150 | unsigned int i; |
| 151 | |
| 152 | for (i = 0; i < global_pt_count; i++) { |
| 153 | if (global_pt_entries[i].memdesc != NULL) { |
| 154 | int ret = kgsl_mmu_map(pagetable, |
| 155 | global_pt_entries[i].memdesc); |
| 156 | |
| 157 | if (ret) |
| 158 | return ret; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 165 | void kgsl_iommu_unmap_global_secure_pt_entry(struct kgsl_device *device, |
| 166 | struct kgsl_memdesc *entry) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 167 | { |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 168 | if (!kgsl_mmu_is_secured(&device->mmu)) |
| 169 | return; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 170 | |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 171 | if (entry != NULL && entry->pagetable->name == KGSL_MMU_SECURE_PT) |
| 172 | kgsl_mmu_unmap(entry->pagetable, entry); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 173 | |
| 174 | } |
| 175 | |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 176 | int kgsl_iommu_map_global_secure_pt_entry(struct kgsl_device *device, |
| 177 | struct kgsl_memdesc *entry) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 178 | { |
| 179 | int ret = 0; |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 180 | |
| 181 | if (!kgsl_mmu_is_secured(&device->mmu)) |
| 182 | return -ENOTSUPP; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 183 | |
| 184 | if (entry != NULL) { |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 185 | struct kgsl_pagetable *pagetable = device->mmu.securepagetable; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 186 | entry->pagetable = pagetable; |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 187 | entry->gpuaddr = KGSL_IOMMU_SECURE_BASE + secure_global_size; |
| 188 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 189 | ret = kgsl_mmu_map(pagetable, entry); |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 190 | if (ret == 0) |
| 191 | secure_global_size += entry->size; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 192 | } |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static void kgsl_iommu_remove_global(struct kgsl_mmu *mmu, |
| 197 | struct kgsl_memdesc *memdesc) |
| 198 | { |
| 199 | int i; |
| 200 | |
| 201 | if (memdesc->gpuaddr == 0 || !(memdesc->priv & KGSL_MEMDESC_GLOBAL)) |
| 202 | return; |
| 203 | |
| 204 | for (i = 0; i < global_pt_count; i++) { |
| 205 | if (global_pt_entries[i].memdesc == memdesc) { |
| 206 | memdesc->gpuaddr = 0; |
| 207 | memdesc->priv &= ~KGSL_MEMDESC_GLOBAL; |
| 208 | global_pt_entries[i].memdesc = NULL; |
| 209 | return; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | static void kgsl_iommu_add_global(struct kgsl_mmu *mmu, |
| 215 | struct kgsl_memdesc *memdesc, const char *name) |
| 216 | { |
| 217 | if (memdesc->gpuaddr != 0) |
| 218 | return; |
| 219 | |
| 220 | /*Check that we can fit the global allocations */ |
| 221 | if (WARN_ON(global_pt_count >= GLOBAL_PT_ENTRIES) || |
| 222 | WARN_ON((global_pt_alloc + memdesc->size) >= |
| 223 | KGSL_IOMMU_GLOBAL_MEM_SIZE)) |
| 224 | return; |
| 225 | |
| 226 | memdesc->gpuaddr = KGSL_IOMMU_GLOBAL_MEM_BASE + global_pt_alloc; |
| 227 | memdesc->priv |= KGSL_MEMDESC_GLOBAL; |
| 228 | global_pt_alloc += memdesc->size; |
| 229 | |
| 230 | global_pt_entries[global_pt_count].memdesc = memdesc; |
| 231 | strlcpy(global_pt_entries[global_pt_count].name, name, |
| 232 | sizeof(global_pt_entries[global_pt_count].name)); |
| 233 | global_pt_count++; |
| 234 | } |
| 235 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 236 | struct kgsl_memdesc *kgsl_iommu_get_qdss_global_entry(void) |
| 237 | { |
| 238 | return &gpu_qdss_desc; |
| 239 | } |
| 240 | |
| 241 | static void kgsl_setup_qdss_desc(struct kgsl_device *device) |
| 242 | { |
| 243 | int result = 0; |
| 244 | uint32_t gpu_qdss_entry[2]; |
| 245 | |
| 246 | if (!of_find_property(device->pdev->dev.of_node, |
| 247 | "qcom,gpu-qdss-stm", NULL)) |
| 248 | return; |
| 249 | |
| 250 | if (of_property_read_u32_array(device->pdev->dev.of_node, |
| 251 | "qcom,gpu-qdss-stm", gpu_qdss_entry, 2)) { |
| 252 | KGSL_CORE_ERR("Failed to read gpu qdss dts entry\n"); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | gpu_qdss_desc.flags = 0; |
| 257 | gpu_qdss_desc.priv = 0; |
| 258 | gpu_qdss_desc.physaddr = gpu_qdss_entry[0]; |
| 259 | gpu_qdss_desc.size = gpu_qdss_entry[1]; |
| 260 | gpu_qdss_desc.pagetable = NULL; |
| 261 | gpu_qdss_desc.ops = NULL; |
| 262 | gpu_qdss_desc.dev = device->dev->parent; |
| 263 | gpu_qdss_desc.hostptr = NULL; |
| 264 | |
| 265 | result = memdesc_sg_dma(&gpu_qdss_desc, gpu_qdss_desc.physaddr, |
| 266 | gpu_qdss_desc.size); |
| 267 | if (result) { |
| 268 | KGSL_CORE_ERR("memdesc_sg_dma failed: %d\n", result); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | kgsl_mmu_add_global(device, &gpu_qdss_desc, "gpu-qdss"); |
| 273 | } |
| 274 | |
| 275 | static inline void kgsl_cleanup_qdss_desc(struct kgsl_mmu *mmu) |
| 276 | { |
| 277 | kgsl_iommu_remove_global(mmu, &gpu_qdss_desc); |
| 278 | kgsl_sharedmem_free(&gpu_qdss_desc); |
| 279 | } |
| 280 | |
Jonathan Wicks | 4892d8d | 2017-02-24 16:21:26 -0700 | [diff] [blame] | 281 | struct kgsl_memdesc *kgsl_iommu_get_qtimer_global_entry(void) |
| 282 | { |
| 283 | return &gpu_qtimer_desc; |
| 284 | } |
| 285 | |
| 286 | static void kgsl_setup_qtimer_desc(struct kgsl_device *device) |
| 287 | { |
| 288 | int result = 0; |
| 289 | uint32_t gpu_qtimer_entry[2]; |
| 290 | |
| 291 | if (!of_find_property(device->pdev->dev.of_node, |
| 292 | "qcom,gpu-qtimer", NULL)) |
| 293 | return; |
| 294 | |
| 295 | if (of_property_read_u32_array(device->pdev->dev.of_node, |
| 296 | "qcom,gpu-qtimer", gpu_qtimer_entry, 2)) { |
| 297 | KGSL_CORE_ERR("Failed to read gpu qtimer dts entry\n"); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | gpu_qtimer_desc.flags = 0; |
| 302 | gpu_qtimer_desc.priv = 0; |
| 303 | gpu_qtimer_desc.physaddr = gpu_qtimer_entry[0]; |
| 304 | gpu_qtimer_desc.size = gpu_qtimer_entry[1]; |
| 305 | gpu_qtimer_desc.pagetable = NULL; |
| 306 | gpu_qtimer_desc.ops = NULL; |
| 307 | gpu_qtimer_desc.dev = device->dev->parent; |
| 308 | gpu_qtimer_desc.hostptr = NULL; |
| 309 | |
| 310 | result = memdesc_sg_dma(&gpu_qtimer_desc, gpu_qtimer_desc.physaddr, |
| 311 | gpu_qtimer_desc.size); |
| 312 | if (result) { |
| 313 | KGSL_CORE_ERR("memdesc_sg_dma failed: %d\n", result); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | kgsl_mmu_add_global(device, &gpu_qtimer_desc, "gpu-qtimer"); |
| 318 | } |
| 319 | |
| 320 | static inline void kgsl_cleanup_qtimer_desc(struct kgsl_mmu *mmu) |
| 321 | { |
| 322 | kgsl_iommu_remove_global(mmu, &gpu_qtimer_desc); |
| 323 | kgsl_sharedmem_free(&gpu_qtimer_desc); |
| 324 | } |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 325 | |
| 326 | static inline void _iommu_sync_mmu_pc(bool lock) |
| 327 | { |
| 328 | if (need_iommu_sync == false) |
| 329 | return; |
| 330 | |
| 331 | if (lock) |
| 332 | mutex_lock(&kgsl_mmu_sync); |
| 333 | else |
| 334 | mutex_unlock(&kgsl_mmu_sync); |
| 335 | } |
| 336 | |
| 337 | static void _detach_pt(struct kgsl_iommu_pt *iommu_pt, |
| 338 | struct kgsl_iommu_context *ctx) |
| 339 | { |
| 340 | if (iommu_pt->attached) { |
| 341 | _iommu_sync_mmu_pc(true); |
| 342 | iommu_detach_device(iommu_pt->domain, ctx->dev); |
| 343 | _iommu_sync_mmu_pc(false); |
| 344 | iommu_pt->attached = false; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | static int _attach_pt(struct kgsl_iommu_pt *iommu_pt, |
| 349 | struct kgsl_iommu_context *ctx) |
| 350 | { |
| 351 | int ret; |
| 352 | |
| 353 | if (iommu_pt->attached) |
| 354 | return 0; |
| 355 | |
| 356 | _iommu_sync_mmu_pc(true); |
| 357 | ret = iommu_attach_device(iommu_pt->domain, ctx->dev); |
| 358 | _iommu_sync_mmu_pc(false); |
| 359 | |
| 360 | if (ret == 0) |
| 361 | iommu_pt->attached = true; |
| 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | static int _lock_if_secure_mmu(struct kgsl_memdesc *memdesc, |
| 367 | struct kgsl_mmu *mmu) |
| 368 | { |
| 369 | struct kgsl_device *device = KGSL_MMU_DEVICE(mmu); |
| 370 | |
| 371 | if (!kgsl_memdesc_is_secured(memdesc)) |
| 372 | return 0; |
| 373 | |
| 374 | if (!kgsl_mmu_is_secured(mmu)) |
| 375 | return -EINVAL; |
| 376 | |
| 377 | mutex_lock(&device->mutex); |
| 378 | if (kgsl_active_count_get(device)) { |
| 379 | mutex_unlock(&device->mutex); |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static void _unlock_if_secure_mmu(struct kgsl_memdesc *memdesc, |
| 387 | struct kgsl_mmu *mmu) |
| 388 | { |
| 389 | struct kgsl_device *device = KGSL_MMU_DEVICE(mmu); |
| 390 | |
| 391 | if (!kgsl_memdesc_is_secured(memdesc) || !kgsl_mmu_is_secured(mmu)) |
| 392 | return; |
| 393 | |
| 394 | kgsl_active_count_put(device); |
| 395 | mutex_unlock(&device->mutex); |
| 396 | } |
| 397 | |
| 398 | static int _iommu_map_sync_pc(struct kgsl_pagetable *pt, |
| 399 | struct kgsl_memdesc *memdesc, |
| 400 | uint64_t gpuaddr, phys_addr_t physaddr, |
| 401 | uint64_t size, unsigned int flags) |
| 402 | { |
| 403 | struct kgsl_iommu_pt *iommu_pt = pt->priv; |
| 404 | int ret; |
| 405 | |
| 406 | ret = _lock_if_secure_mmu(memdesc, pt->mmu); |
| 407 | if (ret) |
| 408 | return ret; |
| 409 | |
| 410 | _iommu_sync_mmu_pc(true); |
| 411 | |
| 412 | ret = iommu_map(iommu_pt->domain, gpuaddr, physaddr, size, flags); |
| 413 | |
| 414 | _iommu_sync_mmu_pc(false); |
| 415 | |
| 416 | _unlock_if_secure_mmu(memdesc, pt->mmu); |
| 417 | |
| 418 | if (ret) { |
| 419 | KGSL_CORE_ERR("map err: 0x%016llX, 0x%llx, 0x%x, %d\n", |
| 420 | gpuaddr, size, flags, ret); |
| 421 | return -ENODEV; |
| 422 | } |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static int _iommu_unmap_sync_pc(struct kgsl_pagetable *pt, |
| 428 | struct kgsl_memdesc *memdesc, uint64_t addr, uint64_t size) |
| 429 | { |
| 430 | struct kgsl_iommu_pt *iommu_pt = pt->priv; |
| 431 | size_t unmapped = 0; |
| 432 | int ret; |
| 433 | |
| 434 | ret = _lock_if_secure_mmu(memdesc, pt->mmu); |
| 435 | if (ret) |
| 436 | return ret; |
| 437 | |
| 438 | _iommu_sync_mmu_pc(true); |
| 439 | |
| 440 | unmapped = iommu_unmap(iommu_pt->domain, addr, size); |
| 441 | |
| 442 | _iommu_sync_mmu_pc(false); |
| 443 | |
| 444 | _unlock_if_secure_mmu(memdesc, pt->mmu); |
| 445 | |
| 446 | if (unmapped != size) { |
| 447 | KGSL_CORE_ERR("unmap err: 0x%016llx, 0x%llx, %zd\n", |
| 448 | addr, size, unmapped); |
| 449 | return -ENODEV; |
| 450 | } |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int _iommu_map_sg_offset_sync_pc(struct kgsl_pagetable *pt, |
| 456 | uint64_t addr, struct kgsl_memdesc *memdesc, |
| 457 | struct scatterlist *sg, int nents, |
| 458 | uint64_t offset, uint64_t size, unsigned int flags) |
| 459 | { |
| 460 | struct kgsl_iommu_pt *iommu_pt = pt->priv; |
| 461 | uint64_t offset_tmp = offset; |
| 462 | uint64_t size_tmp = size; |
| 463 | size_t mapped = 0; |
| 464 | unsigned int i; |
| 465 | struct scatterlist *s; |
| 466 | phys_addr_t physaddr; |
| 467 | int ret; |
| 468 | |
| 469 | ret = _lock_if_secure_mmu(memdesc, pt->mmu); |
| 470 | if (ret) |
| 471 | return ret; |
| 472 | |
| 473 | _iommu_sync_mmu_pc(true); |
| 474 | |
| 475 | for_each_sg(sg, s, nents, i) { |
| 476 | /* Iterate until we find the offset */ |
| 477 | if (offset_tmp >= s->length) { |
| 478 | offset_tmp -= s->length; |
| 479 | continue; |
| 480 | } |
| 481 | |
| 482 | /* How much mapping is needed in this sg? */ |
| 483 | if (size < s->length - offset_tmp) |
| 484 | size_tmp = size; |
| 485 | else |
| 486 | size_tmp = s->length - offset_tmp; |
| 487 | |
| 488 | /* Get the phys addr for the offset page */ |
| 489 | if (offset_tmp != 0) { |
| 490 | physaddr = page_to_phys(nth_page(sg_page(s), |
| 491 | offset_tmp >> PAGE_SHIFT)); |
| 492 | /* Reset offset_tmp */ |
| 493 | offset_tmp = 0; |
| 494 | } else |
| 495 | physaddr = page_to_phys(sg_page(s)); |
| 496 | |
| 497 | /* Do the map for this sg */ |
| 498 | ret = iommu_map(iommu_pt->domain, addr + mapped, |
| 499 | physaddr, size_tmp, flags); |
| 500 | if (ret) |
| 501 | break; |
| 502 | |
| 503 | mapped += size_tmp; |
| 504 | size -= size_tmp; |
| 505 | |
| 506 | if (size == 0) |
| 507 | break; |
| 508 | } |
| 509 | |
| 510 | _iommu_sync_mmu_pc(false); |
| 511 | |
| 512 | _unlock_if_secure_mmu(memdesc, pt->mmu); |
| 513 | |
| 514 | if (size != 0) { |
| 515 | /* Cleanup on error */ |
| 516 | _iommu_unmap_sync_pc(pt, memdesc, addr, mapped); |
| 517 | KGSL_CORE_ERR( |
| 518 | "map sg offset err: 0x%016llX, %d, %x, %zd\n", |
| 519 | addr, nents, flags, mapped); |
| 520 | return -ENODEV; |
| 521 | } |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static int _iommu_map_sg_sync_pc(struct kgsl_pagetable *pt, |
| 527 | uint64_t addr, struct kgsl_memdesc *memdesc, |
| 528 | struct scatterlist *sg, int nents, |
| 529 | unsigned int flags) |
| 530 | { |
| 531 | struct kgsl_iommu_pt *iommu_pt = pt->priv; |
| 532 | size_t mapped; |
| 533 | int ret; |
| 534 | |
| 535 | ret = _lock_if_secure_mmu(memdesc, pt->mmu); |
| 536 | if (ret) |
| 537 | return ret; |
| 538 | |
| 539 | _iommu_sync_mmu_pc(true); |
| 540 | |
| 541 | mapped = iommu_map_sg(iommu_pt->domain, addr, sg, nents, flags); |
| 542 | |
| 543 | _iommu_sync_mmu_pc(false); |
| 544 | |
| 545 | _unlock_if_secure_mmu(memdesc, pt->mmu); |
| 546 | |
| 547 | if (mapped == 0) { |
| 548 | KGSL_CORE_ERR("map sg err: 0x%016llX, %d, %x, %zd\n", |
| 549 | addr, nents, flags, mapped); |
| 550 | return -ENODEV; |
| 551 | } |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * One page allocation for a guard region to protect against over-zealous |
| 558 | * GPU pre-fetch |
| 559 | */ |
| 560 | |
| 561 | static struct page *kgsl_guard_page; |
| 562 | static struct kgsl_memdesc kgsl_secure_guard_page_memdesc; |
| 563 | |
| 564 | /* |
| 565 | * The dummy page is a placeholder/extra page to be used for sparse mappings. |
| 566 | * This page will be mapped to all virtual sparse bindings that are not |
| 567 | * physically backed. |
| 568 | */ |
| 569 | static struct page *kgsl_dummy_page; |
| 570 | |
| 571 | /* These functions help find the nearest allocated memory entries on either side |
| 572 | * of a faulting address. If we know the nearby allocations memory we can |
| 573 | * get a better determination of what we think should have been located in the |
| 574 | * faulting region |
| 575 | */ |
| 576 | |
| 577 | /* |
| 578 | * A local structure to make it easy to store the interesting bits for the |
| 579 | * memory entries on either side of the faulting address |
| 580 | */ |
| 581 | |
| 582 | struct _mem_entry { |
| 583 | uint64_t gpuaddr; |
| 584 | uint64_t size; |
| 585 | uint64_t flags; |
| 586 | unsigned int priv; |
| 587 | int pending_free; |
| 588 | pid_t pid; |
| 589 | char name[32]; |
| 590 | }; |
| 591 | |
| 592 | static void _get_global_entries(uint64_t faultaddr, |
| 593 | struct _mem_entry *prev, |
| 594 | struct _mem_entry *next) |
| 595 | { |
| 596 | int i; |
| 597 | uint64_t prevaddr = 0; |
| 598 | struct global_pt_entry *p = NULL; |
| 599 | |
| 600 | uint64_t nextaddr = (uint64_t) -1; |
| 601 | struct global_pt_entry *n = NULL; |
| 602 | |
| 603 | for (i = 0; i < global_pt_count; i++) { |
| 604 | uint64_t addr; |
| 605 | |
| 606 | if (global_pt_entries[i].memdesc == NULL) |
| 607 | continue; |
| 608 | |
| 609 | addr = global_pt_entries[i].memdesc->gpuaddr; |
| 610 | if ((addr < faultaddr) && (addr > prevaddr)) { |
| 611 | prevaddr = addr; |
| 612 | p = &global_pt_entries[i]; |
| 613 | } |
| 614 | |
| 615 | if ((addr > faultaddr) && (addr < nextaddr)) { |
| 616 | nextaddr = addr; |
| 617 | n = &global_pt_entries[i]; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | if (p != NULL) { |
| 622 | prev->gpuaddr = p->memdesc->gpuaddr; |
| 623 | prev->size = p->memdesc->size; |
| 624 | prev->flags = p->memdesc->flags; |
| 625 | prev->priv = p->memdesc->priv; |
| 626 | prev->pid = 0; |
| 627 | strlcpy(prev->name, p->name, sizeof(prev->name)); |
| 628 | } |
| 629 | |
| 630 | if (n != NULL) { |
| 631 | next->gpuaddr = n->memdesc->gpuaddr; |
| 632 | next->size = n->memdesc->size; |
| 633 | next->flags = n->memdesc->flags; |
| 634 | next->priv = n->memdesc->priv; |
| 635 | next->pid = 0; |
| 636 | strlcpy(next->name, n->name, sizeof(next->name)); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | void __kgsl_get_memory_usage(struct _mem_entry *entry) |
| 641 | { |
| 642 | kgsl_get_memory_usage(entry->name, sizeof(entry->name), entry->flags); |
| 643 | } |
| 644 | |
| 645 | static void _get_entries(struct kgsl_process_private *private, |
| 646 | uint64_t faultaddr, struct _mem_entry *prev, |
| 647 | struct _mem_entry *next) |
| 648 | { |
| 649 | int id; |
| 650 | struct kgsl_mem_entry *entry; |
| 651 | |
| 652 | uint64_t prevaddr = 0; |
| 653 | struct kgsl_mem_entry *p = NULL; |
| 654 | |
| 655 | uint64_t nextaddr = (uint64_t) -1; |
| 656 | struct kgsl_mem_entry *n = NULL; |
| 657 | |
| 658 | idr_for_each_entry(&private->mem_idr, entry, id) { |
| 659 | uint64_t addr = entry->memdesc.gpuaddr; |
| 660 | |
| 661 | if ((addr < faultaddr) && (addr > prevaddr)) { |
| 662 | prevaddr = addr; |
| 663 | p = entry; |
| 664 | } |
| 665 | |
| 666 | if ((addr > faultaddr) && (addr < nextaddr)) { |
| 667 | nextaddr = addr; |
| 668 | n = entry; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | if (p != NULL) { |
| 673 | prev->gpuaddr = p->memdesc.gpuaddr; |
| 674 | prev->size = p->memdesc.size; |
| 675 | prev->flags = p->memdesc.flags; |
| 676 | prev->priv = p->memdesc.priv; |
| 677 | prev->pending_free = p->pending_free; |
| 678 | prev->pid = private->pid; |
| 679 | __kgsl_get_memory_usage(prev); |
| 680 | } |
| 681 | |
| 682 | if (n != NULL) { |
| 683 | next->gpuaddr = n->memdesc.gpuaddr; |
| 684 | next->size = n->memdesc.size; |
| 685 | next->flags = n->memdesc.flags; |
| 686 | next->priv = n->memdesc.priv; |
| 687 | next->pending_free = n->pending_free; |
| 688 | next->pid = private->pid; |
| 689 | __kgsl_get_memory_usage(next); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | static void _find_mem_entries(struct kgsl_mmu *mmu, uint64_t faultaddr, |
| 694 | struct _mem_entry *preventry, struct _mem_entry *nextentry, |
| 695 | struct kgsl_context *context) |
| 696 | { |
| 697 | struct kgsl_process_private *private; |
| 698 | |
| 699 | memset(preventry, 0, sizeof(*preventry)); |
| 700 | memset(nextentry, 0, sizeof(*nextentry)); |
| 701 | |
| 702 | /* Set the maximum possible size as an initial value */ |
| 703 | nextentry->gpuaddr = (uint64_t) -1; |
| 704 | |
| 705 | if (ADDR_IN_GLOBAL(faultaddr)) { |
| 706 | _get_global_entries(faultaddr, preventry, nextentry); |
| 707 | } else if (context) { |
| 708 | private = context->proc_priv; |
| 709 | spin_lock(&private->mem_lock); |
| 710 | _get_entries(private, faultaddr, preventry, nextentry); |
| 711 | spin_unlock(&private->mem_lock); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | static void _print_entry(struct kgsl_device *device, struct _mem_entry *entry) |
| 716 | { |
| 717 | KGSL_LOG_DUMP(device, |
| 718 | "[%016llX - %016llX] %s %s (pid = %d) (%s)\n", |
| 719 | entry->gpuaddr, |
| 720 | entry->gpuaddr + entry->size, |
| 721 | entry->priv & KGSL_MEMDESC_GUARD_PAGE ? "(+guard)" : "", |
| 722 | entry->pending_free ? "(pending free)" : "", |
| 723 | entry->pid, entry->name); |
| 724 | } |
| 725 | |
| 726 | static void _check_if_freed(struct kgsl_iommu_context *ctx, |
| 727 | uint64_t addr, pid_t ptname) |
| 728 | { |
| 729 | uint64_t gpuaddr = addr; |
| 730 | uint64_t size = 0; |
| 731 | uint64_t flags = 0; |
| 732 | pid_t pid; |
| 733 | |
| 734 | char name[32]; |
| 735 | |
| 736 | memset(name, 0, sizeof(name)); |
| 737 | |
| 738 | if (kgsl_memfree_find_entry(ptname, &gpuaddr, &size, &flags, &pid)) { |
| 739 | kgsl_get_memory_usage(name, sizeof(name) - 1, flags); |
| 740 | KGSL_LOG_DUMP(ctx->kgsldev, "---- premature free ----\n"); |
| 741 | KGSL_LOG_DUMP(ctx->kgsldev, |
| 742 | "[%8.8llX-%8.8llX] (%s) was already freed by pid %d\n", |
| 743 | gpuaddr, gpuaddr + size, name, pid); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | static bool |
| 748 | kgsl_iommu_uche_overfetch(struct kgsl_process_private *private, |
| 749 | uint64_t faultaddr) |
| 750 | { |
| 751 | int id; |
| 752 | struct kgsl_mem_entry *entry = NULL; |
| 753 | |
| 754 | spin_lock(&private->mem_lock); |
| 755 | idr_for_each_entry(&private->mem_idr, entry, id) { |
| 756 | struct kgsl_memdesc *m = &entry->memdesc; |
| 757 | |
| 758 | if ((faultaddr >= (m->gpuaddr + m->size)) |
| 759 | && (faultaddr < (m->gpuaddr + m->size + 64))) { |
| 760 | spin_unlock(&private->mem_lock); |
| 761 | return true; |
| 762 | } |
| 763 | } |
| 764 | spin_unlock(&private->mem_lock); |
| 765 | return false; |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * Read pagefaults where the faulting address lies within the first 64 bytes |
| 770 | * of a page (UCHE line size is 64 bytes) and the fault page is preceded by a |
| 771 | * valid allocation are considered likely due to UCHE overfetch and suppressed. |
| 772 | */ |
| 773 | |
| 774 | static bool kgsl_iommu_suppress_pagefault(uint64_t faultaddr, int write, |
| 775 | struct kgsl_context *context) |
| 776 | { |
| 777 | /* |
| 778 | * If there is no context associated with the pagefault then this |
| 779 | * could be a fault on a global buffer. We do not suppress faults |
| 780 | * on global buffers as they are mainly accessed by the CP bypassing |
| 781 | * the UCHE. Also, write pagefaults are never suppressed. |
| 782 | */ |
| 783 | if (!context || write) |
| 784 | return false; |
| 785 | |
| 786 | return kgsl_iommu_uche_overfetch(context->proc_priv, faultaddr); |
| 787 | } |
| 788 | |
| 789 | static int kgsl_iommu_fault_handler(struct iommu_domain *domain, |
| 790 | struct device *dev, unsigned long addr, int flags, void *token) |
| 791 | { |
| 792 | int ret = 0; |
| 793 | struct kgsl_pagetable *pt = token; |
| 794 | struct kgsl_mmu *mmu = pt->mmu; |
| 795 | struct kgsl_iommu *iommu; |
| 796 | struct kgsl_iommu_context *ctx; |
| 797 | u64 ptbase; |
| 798 | u32 contextidr; |
Lynus Vaz | e0a0131 | 2017-11-08 19:39:31 +0530 | [diff] [blame] | 799 | pid_t pid = 0; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 800 | pid_t ptname; |
| 801 | struct _mem_entry prev, next; |
| 802 | int write; |
| 803 | struct kgsl_device *device; |
| 804 | struct adreno_device *adreno_dev; |
Lynus Vaz | 1fde74d | 2017-03-20 18:02:47 +0530 | [diff] [blame] | 805 | struct adreno_gpudev *gpudev; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 806 | unsigned int no_page_fault_log = 0; |
| 807 | unsigned int curr_context_id = 0; |
| 808 | struct kgsl_context *context; |
| 809 | char *fault_type = "unknown"; |
| 810 | |
| 811 | static DEFINE_RATELIMIT_STATE(_rs, |
| 812 | DEFAULT_RATELIMIT_INTERVAL, |
| 813 | DEFAULT_RATELIMIT_BURST); |
| 814 | |
| 815 | if (mmu == NULL) |
| 816 | return ret; |
| 817 | |
| 818 | iommu = _IOMMU_PRIV(mmu); |
| 819 | ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER]; |
| 820 | device = KGSL_MMU_DEVICE(mmu); |
| 821 | adreno_dev = ADRENO_DEVICE(device); |
Lynus Vaz | 1fde74d | 2017-03-20 18:02:47 +0530 | [diff] [blame] | 822 | gpudev = ADRENO_GPU_DEVICE(adreno_dev); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 823 | |
| 824 | if (pt->name == KGSL_MMU_SECURE_PT) |
| 825 | ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_SECURE]; |
| 826 | |
| 827 | /* |
| 828 | * set the fault bits and stuff before any printks so that if fault |
| 829 | * handler runs then it will know it's dealing with a pagefault. |
| 830 | * Read the global current timestamp because we could be in middle of |
| 831 | * RB switch and hence the cur RB may not be reliable but global |
| 832 | * one will always be reliable |
| 833 | */ |
| 834 | kgsl_sharedmem_readl(&device->memstore, &curr_context_id, |
| 835 | KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL, current_context)); |
| 836 | |
| 837 | context = kgsl_context_get(device, curr_context_id); |
| 838 | |
| 839 | write = (flags & IOMMU_FAULT_WRITE) ? 1 : 0; |
| 840 | if (flags & IOMMU_FAULT_TRANSLATION) |
| 841 | fault_type = "translation"; |
| 842 | else if (flags & IOMMU_FAULT_PERMISSION) |
| 843 | fault_type = "permission"; |
| 844 | |
| 845 | if (kgsl_iommu_suppress_pagefault(addr, write, context)) { |
| 846 | iommu->pagefault_suppression_count++; |
| 847 | kgsl_context_put(context); |
| 848 | return ret; |
| 849 | } |
| 850 | |
| 851 | if (context != NULL) { |
| 852 | /* save pagefault timestamp for GFT */ |
| 853 | set_bit(KGSL_CONTEXT_PRIV_PAGEFAULT, &context->priv); |
Lynus Vaz | e0a0131 | 2017-11-08 19:39:31 +0530 | [diff] [blame] | 854 | pid = context->proc_priv->pid; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | ctx->fault = 1; |
| 858 | |
| 859 | if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, |
| 860 | &adreno_dev->ft_pf_policy) && |
| 861 | (flags & IOMMU_FAULT_TRANSACTION_STALLED)) { |
| 862 | /* |
| 863 | * Turn off GPU IRQ so we don't get faults from it too. |
| 864 | * The device mutex must be held to change power state |
| 865 | */ |
| 866 | mutex_lock(&device->mutex); |
| 867 | kgsl_pwrctrl_change_state(device, KGSL_STATE_AWARE); |
| 868 | mutex_unlock(&device->mutex); |
| 869 | } |
| 870 | |
| 871 | ptbase = KGSL_IOMMU_GET_CTX_REG_Q(ctx, TTBR0); |
| 872 | contextidr = KGSL_IOMMU_GET_CTX_REG(ctx, CONTEXTIDR); |
| 873 | |
| 874 | ptname = MMU_FEATURE(mmu, KGSL_MMU_GLOBAL_PAGETABLE) ? |
Lynus Vaz | e0a0131 | 2017-11-08 19:39:31 +0530 | [diff] [blame] | 875 | KGSL_MMU_GLOBAL_PT : pid; |
Sunil Khatri | 86e9568 | 2017-01-23 17:10:32 +0530 | [diff] [blame] | 876 | /* |
| 877 | * Trace needs to be logged before searching the faulting |
| 878 | * address in free list as it takes quite long time in |
| 879 | * search and delays the trace unnecessarily. |
| 880 | */ |
| 881 | trace_kgsl_mmu_pagefault(ctx->kgsldev, addr, |
| 882 | ptname, write ? "write" : "read"); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 883 | |
| 884 | if (test_bit(KGSL_FT_PAGEFAULT_LOG_ONE_PER_PAGE, |
| 885 | &adreno_dev->ft_pf_policy)) |
| 886 | no_page_fault_log = kgsl_mmu_log_fault_addr(mmu, ptbase, addr); |
| 887 | |
| 888 | if (!no_page_fault_log && __ratelimit(&_rs)) { |
| 889 | KGSL_MEM_CRIT(ctx->kgsldev, |
| 890 | "GPU PAGE FAULT: addr = %lX pid= %d\n", addr, ptname); |
| 891 | KGSL_MEM_CRIT(ctx->kgsldev, |
| 892 | "context=%s TTBR0=0x%llx CIDR=0x%x (%s %s fault)\n", |
| 893 | ctx->name, ptbase, contextidr, |
| 894 | write ? "write" : "read", fault_type); |
| 895 | |
Lynus Vaz | 1fde74d | 2017-03-20 18:02:47 +0530 | [diff] [blame] | 896 | if (gpudev->iommu_fault_block) { |
| 897 | unsigned int fsynr1; |
| 898 | |
| 899 | fsynr1 = KGSL_IOMMU_GET_CTX_REG(ctx, FSYNR1); |
| 900 | KGSL_MEM_CRIT(ctx->kgsldev, |
| 901 | "FAULTING BLOCK: %s\n", |
| 902 | gpudev->iommu_fault_block(adreno_dev, |
| 903 | fsynr1)); |
| 904 | } |
| 905 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 906 | /* Don't print the debug if this is a permissions fault */ |
| 907 | if (!(flags & IOMMU_FAULT_PERMISSION)) { |
| 908 | _check_if_freed(ctx, addr, ptname); |
| 909 | |
| 910 | KGSL_LOG_DUMP(ctx->kgsldev, |
| 911 | "---- nearby memory ----\n"); |
| 912 | |
| 913 | _find_mem_entries(mmu, addr, &prev, &next, context); |
| 914 | if (prev.gpuaddr) |
| 915 | _print_entry(ctx->kgsldev, &prev); |
| 916 | else |
| 917 | KGSL_LOG_DUMP(ctx->kgsldev, "*EMPTY*\n"); |
| 918 | |
| 919 | KGSL_LOG_DUMP(ctx->kgsldev, " <- fault @ %8.8lX\n", |
| 920 | addr); |
| 921 | |
| 922 | if (next.gpuaddr != (uint64_t) -1) |
| 923 | _print_entry(ctx->kgsldev, &next); |
| 924 | else |
| 925 | KGSL_LOG_DUMP(ctx->kgsldev, "*EMPTY*\n"); |
| 926 | } |
| 927 | } |
| 928 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 929 | |
| 930 | /* |
| 931 | * We do not want the h/w to resume fetching data from an iommu |
| 932 | * that has faulted, this is better for debugging as it will stall |
| 933 | * the GPU and trigger a snapshot. Return EBUSY error. |
| 934 | */ |
| 935 | if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, |
| 936 | &adreno_dev->ft_pf_policy) && |
| 937 | (flags & IOMMU_FAULT_TRANSACTION_STALLED)) { |
| 938 | uint32_t sctlr_val; |
| 939 | |
| 940 | ret = -EBUSY; |
| 941 | /* |
| 942 | * Disable context fault interrupts |
| 943 | * as we do not clear FSR in the ISR. |
| 944 | * Will be re-enabled after FSR is cleared. |
| 945 | */ |
| 946 | sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR); |
| 947 | sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_CFIE_SHIFT); |
| 948 | KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val); |
| 949 | |
| 950 | adreno_set_gpu_fault(adreno_dev, ADRENO_IOMMU_PAGE_FAULT); |
| 951 | /* Go ahead with recovery*/ |
| 952 | adreno_dispatcher_schedule(device); |
| 953 | } |
| 954 | |
| 955 | kgsl_context_put(context); |
| 956 | return ret; |
| 957 | } |
| 958 | |
| 959 | /* |
| 960 | * kgsl_iommu_disable_clk() - Disable iommu clocks |
| 961 | * Disable IOMMU clocks |
| 962 | */ |
| 963 | static void kgsl_iommu_disable_clk(struct kgsl_mmu *mmu) |
| 964 | { |
| 965 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 966 | int j; |
| 967 | |
| 968 | atomic_dec(&iommu->clk_enable_count); |
| 969 | |
| 970 | /* |
| 971 | * Make sure the clk refcounts are good. An unbalance may |
| 972 | * cause the clocks to be off when we need them on. |
| 973 | */ |
| 974 | WARN_ON(atomic_read(&iommu->clk_enable_count) < 0); |
| 975 | |
| 976 | for (j = (KGSL_IOMMU_MAX_CLKS - 1); j >= 0; j--) |
| 977 | if (iommu->clks[j]) |
| 978 | clk_disable_unprepare(iommu->clks[j]); |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * kgsl_iommu_enable_clk_prepare_enable - Enable the specified IOMMU clock |
| 983 | * Try 4 times to enable it and then BUG() for debug |
| 984 | */ |
| 985 | static void kgsl_iommu_clk_prepare_enable(struct clk *clk) |
| 986 | { |
| 987 | int num_retries = 4; |
| 988 | |
| 989 | while (num_retries--) { |
| 990 | if (!clk_prepare_enable(clk)) |
| 991 | return; |
| 992 | } |
| 993 | |
| 994 | /* Failure is fatal so BUG() to facilitate debug */ |
| 995 | KGSL_CORE_ERR("IOMMU clock enable failed\n"); |
| 996 | BUG(); |
| 997 | } |
| 998 | |
| 999 | /* |
| 1000 | * kgsl_iommu_enable_clk - Enable iommu clocks |
| 1001 | * Enable all the IOMMU clocks |
| 1002 | */ |
| 1003 | static void kgsl_iommu_enable_clk(struct kgsl_mmu *mmu) |
| 1004 | { |
| 1005 | int j; |
| 1006 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 1007 | |
| 1008 | for (j = 0; j < KGSL_IOMMU_MAX_CLKS; j++) { |
| 1009 | if (iommu->clks[j]) |
| 1010 | kgsl_iommu_clk_prepare_enable(iommu->clks[j]); |
| 1011 | } |
| 1012 | atomic_inc(&iommu->clk_enable_count); |
| 1013 | } |
| 1014 | |
| 1015 | /* kgsl_iommu_get_ttbr0 - Get TTBR0 setting for a pagetable */ |
| 1016 | static u64 kgsl_iommu_get_ttbr0(struct kgsl_pagetable *pt) |
| 1017 | { |
| 1018 | struct kgsl_iommu_pt *iommu_pt = pt ? pt->priv : NULL; |
| 1019 | |
| 1020 | BUG_ON(iommu_pt == NULL); |
| 1021 | |
| 1022 | return iommu_pt->ttbr0; |
| 1023 | } |
| 1024 | |
| 1025 | static bool kgsl_iommu_pt_equal(struct kgsl_mmu *mmu, |
| 1026 | struct kgsl_pagetable *pt, |
| 1027 | u64 ttbr0) |
| 1028 | { |
| 1029 | struct kgsl_iommu_pt *iommu_pt = pt ? pt->priv : NULL; |
| 1030 | u64 domain_ttbr0; |
| 1031 | |
| 1032 | if (iommu_pt == NULL) |
| 1033 | return 0; |
| 1034 | |
| 1035 | domain_ttbr0 = kgsl_iommu_get_ttbr0(pt); |
| 1036 | |
| 1037 | return (domain_ttbr0 == ttbr0); |
| 1038 | } |
| 1039 | |
| 1040 | /* kgsl_iommu_get_contextidr - query CONTEXTIDR setting for a pagetable */ |
| 1041 | static u32 kgsl_iommu_get_contextidr(struct kgsl_pagetable *pt) |
| 1042 | { |
| 1043 | struct kgsl_iommu_pt *iommu_pt = pt ? pt->priv : NULL; |
| 1044 | |
| 1045 | BUG_ON(iommu_pt == NULL); |
| 1046 | |
| 1047 | return iommu_pt->contextidr; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * kgsl_iommu_destroy_pagetable - Free up reaources help by a pagetable |
| 1052 | * @mmu_specific_pt - Pointer to pagetable which is to be freed |
| 1053 | * |
| 1054 | * Return - void |
| 1055 | */ |
| 1056 | static void kgsl_iommu_destroy_pagetable(struct kgsl_pagetable *pt) |
| 1057 | { |
| 1058 | struct kgsl_iommu_pt *iommu_pt = pt->priv; |
| 1059 | struct kgsl_mmu *mmu = pt->mmu; |
| 1060 | struct kgsl_iommu *iommu; |
| 1061 | struct kgsl_iommu_context *ctx; |
| 1062 | |
| 1063 | /* |
| 1064 | * Make sure all allocations are unmapped before destroying |
| 1065 | * the pagetable |
| 1066 | */ |
| 1067 | WARN_ON(!list_empty(&pt->list)); |
| 1068 | |
| 1069 | iommu = _IOMMU_PRIV(mmu); |
| 1070 | |
| 1071 | if (pt->name == KGSL_MMU_SECURE_PT) { |
| 1072 | ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_SECURE]; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1073 | } else { |
| 1074 | ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER]; |
| 1075 | kgsl_iommu_unmap_globals(pt); |
| 1076 | } |
| 1077 | |
| 1078 | if (iommu_pt->domain) { |
| 1079 | trace_kgsl_pagetable_destroy(iommu_pt->ttbr0, pt->name); |
| 1080 | |
| 1081 | _detach_pt(iommu_pt, ctx); |
| 1082 | |
| 1083 | iommu_domain_free(iommu_pt->domain); |
| 1084 | } |
| 1085 | |
| 1086 | kfree(iommu_pt); |
| 1087 | } |
| 1088 | |
| 1089 | static void setup_64bit_pagetable(struct kgsl_mmu *mmu, |
| 1090 | struct kgsl_pagetable *pagetable, |
| 1091 | struct kgsl_iommu_pt *pt) |
| 1092 | { |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1093 | if (mmu->secured && pagetable->name == KGSL_MMU_SECURE_PT) { |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 1094 | pt->compat_va_start = KGSL_IOMMU_SECURE_BASE; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1095 | pt->compat_va_end = KGSL_IOMMU_SECURE_END; |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 1096 | pt->va_start = KGSL_IOMMU_SECURE_BASE; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1097 | pt->va_end = KGSL_IOMMU_SECURE_END; |
| 1098 | } else { |
| 1099 | pt->compat_va_start = KGSL_IOMMU_SVM_BASE32; |
| 1100 | pt->compat_va_end = KGSL_IOMMU_SVM_END32; |
| 1101 | pt->va_start = KGSL_IOMMU_VA_BASE64; |
| 1102 | pt->va_end = KGSL_IOMMU_VA_END64; |
| 1103 | } |
| 1104 | |
| 1105 | if (pagetable->name != KGSL_MMU_GLOBAL_PT && |
| 1106 | pagetable->name != KGSL_MMU_SECURE_PT) { |
| 1107 | if ((BITS_PER_LONG == 32) || is_compat_task()) { |
| 1108 | pt->svm_start = KGSL_IOMMU_SVM_BASE32; |
| 1109 | pt->svm_end = KGSL_IOMMU_SVM_END32; |
| 1110 | } else { |
| 1111 | pt->svm_start = KGSL_IOMMU_SVM_BASE64; |
| 1112 | pt->svm_end = KGSL_IOMMU_SVM_END64; |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | static void setup_32bit_pagetable(struct kgsl_mmu *mmu, |
| 1118 | struct kgsl_pagetable *pagetable, |
| 1119 | struct kgsl_iommu_pt *pt) |
| 1120 | { |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1121 | if (mmu->secured) { |
| 1122 | if (pagetable->name == KGSL_MMU_SECURE_PT) { |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 1123 | pt->compat_va_start = KGSL_IOMMU_SECURE_BASE; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1124 | pt->compat_va_end = KGSL_IOMMU_SECURE_END; |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 1125 | pt->va_start = KGSL_IOMMU_SECURE_BASE; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1126 | pt->va_end = KGSL_IOMMU_SECURE_END; |
| 1127 | } else { |
| 1128 | pt->va_start = KGSL_IOMMU_SVM_BASE32; |
Harshdeep Dhatt | 517a067 | 2017-03-23 10:23:55 -0600 | [diff] [blame] | 1129 | pt->va_end = KGSL_IOMMU_SECURE_BASE; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1130 | pt->compat_va_start = pt->va_start; |
| 1131 | pt->compat_va_end = pt->va_end; |
| 1132 | } |
| 1133 | } else { |
| 1134 | pt->va_start = KGSL_IOMMU_SVM_BASE32; |
| 1135 | pt->va_end = KGSL_IOMMU_GLOBAL_MEM_BASE; |
| 1136 | pt->compat_va_start = pt->va_start; |
| 1137 | pt->compat_va_end = pt->va_end; |
| 1138 | } |
| 1139 | |
| 1140 | if (pagetable->name != KGSL_MMU_GLOBAL_PT && |
| 1141 | pagetable->name != KGSL_MMU_SECURE_PT) { |
| 1142 | pt->svm_start = KGSL_IOMMU_SVM_BASE32; |
| 1143 | pt->svm_end = KGSL_IOMMU_SVM_END32; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | |
| 1148 | static struct kgsl_iommu_pt * |
| 1149 | _alloc_pt(struct device *dev, struct kgsl_mmu *mmu, struct kgsl_pagetable *pt) |
| 1150 | { |
| 1151 | struct kgsl_iommu_pt *iommu_pt; |
| 1152 | struct bus_type *bus = kgsl_mmu_get_bus(dev); |
| 1153 | |
| 1154 | if (bus == NULL) |
| 1155 | return ERR_PTR(-ENODEV); |
| 1156 | |
| 1157 | iommu_pt = kzalloc(sizeof(struct kgsl_iommu_pt), GFP_KERNEL); |
| 1158 | if (iommu_pt == NULL) |
| 1159 | return ERR_PTR(-ENOMEM); |
| 1160 | |
| 1161 | iommu_pt->domain = iommu_domain_alloc(bus); |
| 1162 | if (iommu_pt->domain == NULL) { |
| 1163 | kfree(iommu_pt); |
| 1164 | return ERR_PTR(-ENODEV); |
| 1165 | } |
| 1166 | |
| 1167 | pt->pt_ops = &iommu_pt_ops; |
| 1168 | pt->priv = iommu_pt; |
| 1169 | pt->fault_addr = ~0ULL; |
| 1170 | iommu_pt->rbtree = RB_ROOT; |
| 1171 | |
| 1172 | if (MMU_FEATURE(mmu, KGSL_MMU_64BIT)) |
| 1173 | setup_64bit_pagetable(mmu, pt, iommu_pt); |
| 1174 | else |
| 1175 | setup_32bit_pagetable(mmu, pt, iommu_pt); |
| 1176 | |
| 1177 | |
| 1178 | return iommu_pt; |
| 1179 | } |
| 1180 | |
| 1181 | static void _free_pt(struct kgsl_iommu_context *ctx, struct kgsl_pagetable *pt) |
| 1182 | { |
| 1183 | struct kgsl_iommu_pt *iommu_pt = pt->priv; |
| 1184 | |
| 1185 | pt->pt_ops = NULL; |
| 1186 | pt->priv = NULL; |
| 1187 | |
| 1188 | if (iommu_pt == NULL) |
| 1189 | return; |
| 1190 | |
| 1191 | _detach_pt(iommu_pt, ctx); |
| 1192 | |
| 1193 | if (iommu_pt->domain != NULL) |
| 1194 | iommu_domain_free(iommu_pt->domain); |
| 1195 | kfree(iommu_pt); |
| 1196 | } |
| 1197 | |
Sushmita Susheelendra | 906564d | 2017-01-10 15:53:55 -0700 | [diff] [blame] | 1198 | void _enable_gpuhtw_llc(struct kgsl_mmu *mmu, struct kgsl_iommu_pt *iommu_pt) |
| 1199 | { |
| 1200 | struct kgsl_device *device = KGSL_MMU_DEVICE(mmu); |
| 1201 | struct adreno_device *adreno_dev = ADRENO_DEVICE(device); |
| 1202 | int gpuhtw_llc_enable = 1; |
| 1203 | int ret; |
| 1204 | |
| 1205 | /* GPU pagetable walk LLC slice not enabled */ |
| 1206 | if (!adreno_dev->gpuhtw_llc_slice) |
| 1207 | return; |
| 1208 | |
| 1209 | /* Domain attribute to enable system cache for GPU pagetable walks */ |
| 1210 | ret = iommu_domain_set_attr(iommu_pt->domain, |
| 1211 | DOMAIN_ATTR_USE_UPSTREAM_HINT, &gpuhtw_llc_enable); |
| 1212 | /* |
| 1213 | * Warn that the system cache will not be used for GPU |
| 1214 | * pagetable walks. This is not a fatal error. |
| 1215 | */ |
| 1216 | WARN_ONCE(ret, |
| 1217 | "System cache not enabled for GPU pagetable walks: %d\n", ret); |
| 1218 | } |
| 1219 | |
Shrenuj Bansal | 9a0563b | 2017-06-15 14:45:15 -0700 | [diff] [blame] | 1220 | static int program_smmu_aperture(unsigned int cb, unsigned int aperture_reg) |
| 1221 | { |
| 1222 | struct scm_desc desc = {0}; |
| 1223 | |
| 1224 | desc.args[0] = 0xFFFF0000 | ((aperture_reg & 0xff) << 8) | (cb & 0xff); |
| 1225 | desc.args[1] = 0xFFFFFFFF; |
| 1226 | desc.args[2] = 0xFFFFFFFF; |
| 1227 | desc.args[3] = 0xFFFFFFFF; |
| 1228 | desc.arginfo = SCM_ARGS(4); |
| 1229 | |
| 1230 | return scm_call2(SCM_SIP_FNID(SCM_SVC_MP, 0x1B), &desc); |
| 1231 | } |
| 1232 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1233 | static int _init_global_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt) |
| 1234 | { |
| 1235 | int ret = 0; |
| 1236 | struct kgsl_iommu_pt *iommu_pt = NULL; |
| 1237 | unsigned int cb_num; |
| 1238 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 1239 | struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER]; |
| 1240 | |
| 1241 | iommu_pt = _alloc_pt(ctx->dev, mmu, pt); |
| 1242 | |
| 1243 | if (IS_ERR(iommu_pt)) |
| 1244 | return PTR_ERR(iommu_pt); |
| 1245 | |
| 1246 | if (kgsl_mmu_is_perprocess(mmu)) { |
| 1247 | ret = iommu_domain_set_attr(iommu_pt->domain, |
| 1248 | DOMAIN_ATTR_PROCID, &pt->name); |
| 1249 | if (ret) { |
| 1250 | KGSL_CORE_ERR("set DOMAIN_ATTR_PROCID failed: %d\n", |
| 1251 | ret); |
| 1252 | goto done; |
| 1253 | } |
| 1254 | } |
| 1255 | |
Sushmita Susheelendra | 906564d | 2017-01-10 15:53:55 -0700 | [diff] [blame] | 1256 | _enable_gpuhtw_llc(mmu, iommu_pt); |
| 1257 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1258 | ret = _attach_pt(iommu_pt, ctx); |
| 1259 | if (ret) |
| 1260 | goto done; |
| 1261 | |
| 1262 | iommu_set_fault_handler(iommu_pt->domain, |
| 1263 | kgsl_iommu_fault_handler, pt); |
| 1264 | |
| 1265 | ret = iommu_domain_get_attr(iommu_pt->domain, |
| 1266 | DOMAIN_ATTR_CONTEXT_BANK, &cb_num); |
| 1267 | if (ret) { |
Shrenuj Bansal | c3b15ce | 2017-06-15 14:48:05 -0700 | [diff] [blame] | 1268 | KGSL_CORE_ERR("get DOMAIN_ATTR_CONTEXT_BANK failed: %d\n", |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1269 | ret); |
| 1270 | goto done; |
| 1271 | } |
| 1272 | |
Shrenuj Bansal | 9a0563b | 2017-06-15 14:45:15 -0700 | [diff] [blame] | 1273 | if (!MMU_FEATURE(mmu, KGSL_MMU_GLOBAL_PAGETABLE)) { |
| 1274 | ret = program_smmu_aperture(cb_num, CP_APERTURE_REG); |
| 1275 | if (ret) { |
| 1276 | pr_err("SMMU aperture programming call failed with error %d\n", |
| 1277 | ret); |
| 1278 | return ret; |
| 1279 | } |
| 1280 | } |
| 1281 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1282 | ctx->cb_num = cb_num; |
| 1283 | ctx->regbase = iommu->regbase + KGSL_IOMMU_CB0_OFFSET |
| 1284 | + (cb_num << KGSL_IOMMU_CB_SHIFT); |
| 1285 | |
| 1286 | ret = iommu_domain_get_attr(iommu_pt->domain, |
| 1287 | DOMAIN_ATTR_TTBR0, &iommu_pt->ttbr0); |
| 1288 | if (ret) { |
| 1289 | KGSL_CORE_ERR("get DOMAIN_ATTR_TTBR0 failed: %d\n", |
| 1290 | ret); |
| 1291 | goto done; |
| 1292 | } |
| 1293 | ret = iommu_domain_get_attr(iommu_pt->domain, |
| 1294 | DOMAIN_ATTR_CONTEXTIDR, &iommu_pt->contextidr); |
| 1295 | if (ret) { |
| 1296 | KGSL_CORE_ERR("get DOMAIN_ATTR_CONTEXTIDR failed: %d\n", |
| 1297 | ret); |
| 1298 | goto done; |
| 1299 | } |
| 1300 | |
| 1301 | ret = kgsl_iommu_map_globals(pt); |
| 1302 | |
| 1303 | done: |
| 1304 | if (ret) |
| 1305 | _free_pt(ctx, pt); |
| 1306 | |
| 1307 | return ret; |
| 1308 | } |
| 1309 | |
| 1310 | static int _init_secure_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt) |
| 1311 | { |
| 1312 | int ret = 0; |
| 1313 | struct kgsl_iommu_pt *iommu_pt = NULL; |
| 1314 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 1315 | struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_SECURE]; |
| 1316 | int secure_vmid = VMID_CP_PIXEL; |
| 1317 | unsigned int cb_num; |
| 1318 | |
| 1319 | if (!mmu->secured) |
| 1320 | return -EPERM; |
| 1321 | |
| 1322 | if (!MMU_FEATURE(mmu, KGSL_MMU_HYP_SECURE_ALLOC)) { |
| 1323 | if (!kgsl_mmu_bus_secured(ctx->dev)) |
| 1324 | return -EPERM; |
| 1325 | } |
| 1326 | |
| 1327 | iommu_pt = _alloc_pt(ctx->dev, mmu, pt); |
| 1328 | |
| 1329 | if (IS_ERR(iommu_pt)) |
| 1330 | return PTR_ERR(iommu_pt); |
| 1331 | |
| 1332 | ret = iommu_domain_set_attr(iommu_pt->domain, |
| 1333 | DOMAIN_ATTR_SECURE_VMID, &secure_vmid); |
| 1334 | if (ret) { |
| 1335 | KGSL_CORE_ERR("set DOMAIN_ATTR_SECURE_VMID failed: %d\n", ret); |
| 1336 | goto done; |
| 1337 | } |
| 1338 | |
Sushmita Susheelendra | 906564d | 2017-01-10 15:53:55 -0700 | [diff] [blame] | 1339 | _enable_gpuhtw_llc(mmu, iommu_pt); |
| 1340 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1341 | ret = _attach_pt(iommu_pt, ctx); |
| 1342 | |
| 1343 | if (MMU_FEATURE(mmu, KGSL_MMU_HYP_SECURE_ALLOC)) |
| 1344 | iommu_set_fault_handler(iommu_pt->domain, |
| 1345 | kgsl_iommu_fault_handler, pt); |
| 1346 | |
| 1347 | ret = iommu_domain_get_attr(iommu_pt->domain, |
| 1348 | DOMAIN_ATTR_CONTEXT_BANK, &cb_num); |
| 1349 | if (ret) { |
| 1350 | KGSL_CORE_ERR("get DOMAIN_ATTR_PROCID failed: %d\n", |
| 1351 | ret); |
| 1352 | goto done; |
| 1353 | } |
| 1354 | |
| 1355 | ctx->cb_num = cb_num; |
| 1356 | ctx->regbase = iommu->regbase + KGSL_IOMMU_CB0_OFFSET |
| 1357 | + (cb_num << KGSL_IOMMU_CB_SHIFT); |
| 1358 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1359 | done: |
| 1360 | if (ret) |
| 1361 | _free_pt(ctx, pt); |
| 1362 | return ret; |
| 1363 | } |
| 1364 | |
| 1365 | static int _init_per_process_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt) |
| 1366 | { |
| 1367 | int ret = 0; |
| 1368 | struct kgsl_iommu_pt *iommu_pt = NULL; |
| 1369 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 1370 | struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER]; |
| 1371 | int dynamic = 1; |
| 1372 | unsigned int cb_num = ctx->cb_num; |
| 1373 | |
| 1374 | iommu_pt = _alloc_pt(ctx->dev, mmu, pt); |
| 1375 | |
| 1376 | if (IS_ERR(iommu_pt)) |
| 1377 | return PTR_ERR(iommu_pt); |
| 1378 | |
| 1379 | ret = iommu_domain_set_attr(iommu_pt->domain, |
| 1380 | DOMAIN_ATTR_DYNAMIC, &dynamic); |
| 1381 | if (ret) { |
| 1382 | KGSL_CORE_ERR("set DOMAIN_ATTR_DYNAMIC failed: %d\n", ret); |
| 1383 | goto done; |
| 1384 | } |
| 1385 | ret = iommu_domain_set_attr(iommu_pt->domain, |
| 1386 | DOMAIN_ATTR_CONTEXT_BANK, &cb_num); |
| 1387 | if (ret) { |
| 1388 | KGSL_CORE_ERR("set DOMAIN_ATTR_CONTEXT_BANK failed: %d\n", ret); |
| 1389 | goto done; |
| 1390 | } |
| 1391 | |
| 1392 | ret = iommu_domain_set_attr(iommu_pt->domain, |
| 1393 | DOMAIN_ATTR_PROCID, &pt->name); |
| 1394 | if (ret) { |
| 1395 | KGSL_CORE_ERR("set DOMAIN_ATTR_PROCID failed: %d\n", ret); |
| 1396 | goto done; |
| 1397 | } |
| 1398 | |
Sushmita Susheelendra | 906564d | 2017-01-10 15:53:55 -0700 | [diff] [blame] | 1399 | _enable_gpuhtw_llc(mmu, iommu_pt); |
| 1400 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1401 | ret = _attach_pt(iommu_pt, ctx); |
| 1402 | if (ret) |
| 1403 | goto done; |
| 1404 | |
| 1405 | /* now read back the attributes needed for self programming */ |
| 1406 | ret = iommu_domain_get_attr(iommu_pt->domain, |
| 1407 | DOMAIN_ATTR_TTBR0, &iommu_pt->ttbr0); |
| 1408 | if (ret) { |
| 1409 | KGSL_CORE_ERR("get DOMAIN_ATTR_TTBR0 failed: %d\n", ret); |
| 1410 | goto done; |
| 1411 | } |
| 1412 | |
| 1413 | ret = iommu_domain_get_attr(iommu_pt->domain, |
| 1414 | DOMAIN_ATTR_CONTEXTIDR, &iommu_pt->contextidr); |
| 1415 | if (ret) { |
| 1416 | KGSL_CORE_ERR("get DOMAIN_ATTR_CONTEXTIDR failed: %d\n", ret); |
| 1417 | goto done; |
| 1418 | } |
| 1419 | |
| 1420 | ret = kgsl_iommu_map_globals(pt); |
| 1421 | |
| 1422 | done: |
| 1423 | if (ret) |
| 1424 | _free_pt(ctx, pt); |
| 1425 | |
| 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | /* kgsl_iommu_init_pt - Set up an IOMMU pagetable */ |
| 1430 | static int kgsl_iommu_init_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt) |
| 1431 | { |
| 1432 | if (pt == NULL) |
| 1433 | return -EINVAL; |
| 1434 | |
| 1435 | switch (pt->name) { |
| 1436 | case KGSL_MMU_GLOBAL_PT: |
| 1437 | return _init_global_pt(mmu, pt); |
| 1438 | |
| 1439 | case KGSL_MMU_SECURE_PT: |
| 1440 | return _init_secure_pt(mmu, pt); |
| 1441 | |
| 1442 | default: |
| 1443 | return _init_per_process_pt(mmu, pt); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | static struct kgsl_pagetable *kgsl_iommu_getpagetable(struct kgsl_mmu *mmu, |
| 1448 | unsigned long name) |
| 1449 | { |
| 1450 | struct kgsl_pagetable *pt; |
| 1451 | |
| 1452 | if (!kgsl_mmu_is_perprocess(mmu) && (name != KGSL_MMU_SECURE_PT)) { |
| 1453 | name = KGSL_MMU_GLOBAL_PT; |
| 1454 | if (mmu->defaultpagetable != NULL) |
| 1455 | return mmu->defaultpagetable; |
| 1456 | } |
| 1457 | |
| 1458 | pt = kgsl_get_pagetable(name); |
| 1459 | if (pt == NULL) |
| 1460 | pt = kgsl_mmu_createpagetableobject(mmu, name); |
| 1461 | |
| 1462 | return pt; |
| 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * kgsl_iommu_get_reg_ahbaddr - Returns the ahb address of the register |
| 1467 | * @mmu - Pointer to mmu structure |
| 1468 | * @id - The context ID of the IOMMU ctx |
| 1469 | * @reg - The register for which address is required |
| 1470 | * |
| 1471 | * Return - The address of register which can be used in type0 packet |
| 1472 | */ |
| 1473 | static unsigned int kgsl_iommu_get_reg_ahbaddr(struct kgsl_mmu *mmu, |
| 1474 | int id, unsigned int reg) |
| 1475 | { |
| 1476 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 1477 | struct kgsl_iommu_context *ctx = &iommu->ctx[id]; |
| 1478 | |
| 1479 | return ctx->gpu_offset + kgsl_iommu_reg_list[reg]; |
| 1480 | } |
| 1481 | |
| 1482 | static void _detach_context(struct kgsl_iommu_context *ctx) |
| 1483 | { |
| 1484 | struct kgsl_iommu_pt *iommu_pt; |
| 1485 | |
| 1486 | if (ctx->default_pt == NULL) |
| 1487 | return; |
| 1488 | |
| 1489 | iommu_pt = ctx->default_pt->priv; |
| 1490 | |
| 1491 | _detach_pt(iommu_pt, ctx); |
| 1492 | |
| 1493 | ctx->default_pt = NULL; |
| 1494 | } |
| 1495 | |
| 1496 | static void kgsl_iommu_close(struct kgsl_mmu *mmu) |
| 1497 | { |
| 1498 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 1499 | int i; |
| 1500 | |
| 1501 | for (i = 0; i < KGSL_IOMMU_CONTEXT_MAX; i++) |
| 1502 | _detach_context(&iommu->ctx[i]); |
| 1503 | |
| 1504 | kgsl_mmu_putpagetable(mmu->defaultpagetable); |
| 1505 | mmu->defaultpagetable = NULL; |
| 1506 | |
| 1507 | kgsl_mmu_putpagetable(mmu->securepagetable); |
| 1508 | mmu->securepagetable = NULL; |
| 1509 | |
| 1510 | if (iommu->regbase != NULL) |
| 1511 | iounmap(iommu->regbase); |
| 1512 | |
| 1513 | kgsl_sharedmem_free(&kgsl_secure_guard_page_memdesc); |
| 1514 | |
| 1515 | if (kgsl_guard_page != NULL) { |
| 1516 | __free_page(kgsl_guard_page); |
| 1517 | kgsl_guard_page = NULL; |
| 1518 | } |
| 1519 | |
| 1520 | if (kgsl_dummy_page != NULL) { |
| 1521 | __free_page(kgsl_dummy_page); |
| 1522 | kgsl_dummy_page = NULL; |
| 1523 | } |
| 1524 | |
| 1525 | kgsl_iommu_remove_global(mmu, &iommu->setstate); |
| 1526 | kgsl_sharedmem_free(&iommu->setstate); |
| 1527 | kgsl_cleanup_qdss_desc(mmu); |
Jonathan Wicks | 4892d8d | 2017-02-24 16:21:26 -0700 | [diff] [blame] | 1528 | kgsl_cleanup_qtimer_desc(mmu); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | static int _setstate_alloc(struct kgsl_device *device, |
| 1532 | struct kgsl_iommu *iommu) |
| 1533 | { |
| 1534 | int ret; |
| 1535 | |
| 1536 | ret = kgsl_sharedmem_alloc_contig(device, &iommu->setstate, PAGE_SIZE); |
| 1537 | |
| 1538 | if (!ret) { |
| 1539 | /* Mark the setstate memory as read only */ |
| 1540 | iommu->setstate.flags |= KGSL_MEMFLAGS_GPUREADONLY; |
| 1541 | |
| 1542 | kgsl_sharedmem_set(device, &iommu->setstate, 0, 0, PAGE_SIZE); |
| 1543 | } |
| 1544 | |
| 1545 | return ret; |
| 1546 | } |
| 1547 | |
| 1548 | static int kgsl_iommu_init(struct kgsl_mmu *mmu) |
| 1549 | { |
| 1550 | struct kgsl_device *device = KGSL_MMU_DEVICE(mmu); |
| 1551 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 1552 | struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER]; |
| 1553 | int status; |
| 1554 | |
| 1555 | mmu->features |= KGSL_MMU_PAGED; |
| 1556 | |
| 1557 | if (ctx->name == NULL) { |
| 1558 | KGSL_CORE_ERR("dt: gfx3d0_user context bank not found\n"); |
| 1559 | return -EINVAL; |
| 1560 | } |
| 1561 | |
| 1562 | status = _setstate_alloc(device, iommu); |
| 1563 | if (status) |
| 1564 | return status; |
| 1565 | |
| 1566 | /* check requirements for per process pagetables */ |
| 1567 | if (ctx->gpu_offset == UINT_MAX) { |
| 1568 | KGSL_CORE_ERR("missing qcom,gpu-offset forces global pt\n"); |
| 1569 | mmu->features |= KGSL_MMU_GLOBAL_PAGETABLE; |
| 1570 | } |
| 1571 | |
| 1572 | if (iommu->version == 1 && iommu->micro_mmu_ctrl == UINT_MAX) { |
| 1573 | KGSL_CORE_ERR( |
| 1574 | "missing qcom,micro-mmu-control forces global pt\n"); |
| 1575 | mmu->features |= KGSL_MMU_GLOBAL_PAGETABLE; |
| 1576 | } |
| 1577 | |
| 1578 | /* Check to see if we need to do the IOMMU sync dance */ |
| 1579 | need_iommu_sync = of_property_read_bool(device->pdev->dev.of_node, |
| 1580 | "qcom,gpu-quirk-iommu-sync"); |
| 1581 | |
| 1582 | iommu->regbase = ioremap(iommu->regstart, iommu->regsize); |
| 1583 | if (iommu->regbase == NULL) { |
| 1584 | KGSL_CORE_ERR("Could not map IOMMU registers 0x%lx:0x%x\n", |
| 1585 | iommu->regstart, iommu->regsize); |
| 1586 | status = -ENOMEM; |
| 1587 | goto done; |
| 1588 | } |
| 1589 | |
| 1590 | if (addr_entry_cache == NULL) { |
| 1591 | addr_entry_cache = KMEM_CACHE(kgsl_iommu_addr_entry, 0); |
| 1592 | if (addr_entry_cache == NULL) { |
| 1593 | status = -ENOMEM; |
| 1594 | goto done; |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | kgsl_iommu_add_global(mmu, &iommu->setstate, "setstate"); |
| 1599 | kgsl_setup_qdss_desc(device); |
Jonathan Wicks | 4892d8d | 2017-02-24 16:21:26 -0700 | [diff] [blame] | 1600 | kgsl_setup_qtimer_desc(device); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1601 | |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 1602 | if (!mmu->secured) |
| 1603 | goto done; |
| 1604 | |
| 1605 | mmu->securepagetable = kgsl_mmu_getpagetable(mmu, |
| 1606 | KGSL_MMU_SECURE_PT); |
| 1607 | if (IS_ERR(mmu->securepagetable)) { |
| 1608 | status = PTR_ERR(mmu->securepagetable); |
| 1609 | mmu->securepagetable = NULL; |
| 1610 | } else if (mmu->securepagetable == NULL) { |
| 1611 | status = -ENOMEM; |
| 1612 | } |
| 1613 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1614 | done: |
| 1615 | if (status) |
| 1616 | kgsl_iommu_close(mmu); |
| 1617 | |
| 1618 | return status; |
| 1619 | } |
| 1620 | |
| 1621 | static int _setup_user_context(struct kgsl_mmu *mmu) |
| 1622 | { |
| 1623 | int ret = 0; |
| 1624 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 1625 | struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER]; |
| 1626 | struct kgsl_device *device = KGSL_MMU_DEVICE(mmu); |
| 1627 | struct adreno_device *adreno_dev = ADRENO_DEVICE(device); |
| 1628 | struct kgsl_iommu_pt *iommu_pt = NULL; |
| 1629 | unsigned int sctlr_val; |
| 1630 | |
| 1631 | if (mmu->defaultpagetable == NULL) { |
| 1632 | mmu->defaultpagetable = kgsl_mmu_getpagetable(mmu, |
| 1633 | KGSL_MMU_GLOBAL_PT); |
| 1634 | /* if we don't have a default pagetable, nothing will work */ |
| 1635 | if (IS_ERR(mmu->defaultpagetable)) { |
| 1636 | ret = PTR_ERR(mmu->defaultpagetable); |
| 1637 | mmu->defaultpagetable = NULL; |
| 1638 | return ret; |
Lynus Vaz | a2e3111 | 2017-04-17 18:29:58 +0530 | [diff] [blame] | 1639 | } else if (mmu->defaultpagetable == NULL) { |
| 1640 | return -ENOMEM; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | iommu_pt = mmu->defaultpagetable->priv; |
| 1645 | if (iommu_pt == NULL) |
| 1646 | return -ENODEV; |
| 1647 | |
| 1648 | ret = _attach_pt(iommu_pt, ctx); |
| 1649 | if (ret) |
| 1650 | return ret; |
| 1651 | |
| 1652 | ctx->default_pt = mmu->defaultpagetable; |
| 1653 | |
| 1654 | kgsl_iommu_enable_clk(mmu); |
| 1655 | |
| 1656 | sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR); |
| 1657 | |
| 1658 | /* |
| 1659 | * If pagefault policy is GPUHALT_ENABLE, |
| 1660 | * 1) Program CFCFG to 1 to enable STALL mode |
| 1661 | * 2) Program HUPCF to 0 (Stall or terminate subsequent |
| 1662 | * transactions in the presence of an outstanding fault) |
| 1663 | * else |
| 1664 | * 1) Program CFCFG to 0 to disable STALL mode (0=Terminate) |
| 1665 | * 2) Program HUPCF to 1 (Process subsequent transactions |
| 1666 | * independently of any outstanding fault) |
| 1667 | */ |
| 1668 | |
| 1669 | if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, |
| 1670 | &adreno_dev->ft_pf_policy)) { |
| 1671 | sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT); |
| 1672 | sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT); |
| 1673 | } else { |
| 1674 | sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT); |
| 1675 | sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT); |
| 1676 | } |
| 1677 | KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val); |
| 1678 | kgsl_iommu_disable_clk(mmu); |
| 1679 | |
| 1680 | return 0; |
| 1681 | } |
| 1682 | |
| 1683 | static int _setup_secure_context(struct kgsl_mmu *mmu) |
| 1684 | { |
| 1685 | int ret; |
| 1686 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 1687 | struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_SECURE]; |
| 1688 | unsigned int cb_num; |
| 1689 | |
| 1690 | struct kgsl_iommu_pt *iommu_pt; |
| 1691 | |
| 1692 | if (ctx->dev == NULL || !mmu->secured) |
| 1693 | return 0; |
| 1694 | |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 1695 | if (mmu->securepagetable == NULL) |
| 1696 | return -ENOMEM; |
| 1697 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1698 | iommu_pt = mmu->securepagetable->priv; |
| 1699 | |
| 1700 | ret = _attach_pt(iommu_pt, ctx); |
| 1701 | if (ret) |
| 1702 | goto done; |
| 1703 | |
| 1704 | ctx->default_pt = mmu->securepagetable; |
| 1705 | |
| 1706 | ret = iommu_domain_get_attr(iommu_pt->domain, DOMAIN_ATTR_CONTEXT_BANK, |
| 1707 | &cb_num); |
| 1708 | if (ret) { |
| 1709 | KGSL_CORE_ERR("get CONTEXT_BANK attr, err %d\n", ret); |
| 1710 | goto done; |
| 1711 | } |
| 1712 | ctx->cb_num = cb_num; |
| 1713 | done: |
| 1714 | if (ret) |
| 1715 | _detach_context(ctx); |
| 1716 | return ret; |
| 1717 | } |
| 1718 | |
| 1719 | static int kgsl_iommu_set_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt); |
| 1720 | |
| 1721 | static int kgsl_iommu_start(struct kgsl_mmu *mmu) |
| 1722 | { |
| 1723 | int status; |
| 1724 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 1725 | |
| 1726 | status = _setup_user_context(mmu); |
| 1727 | if (status) |
| 1728 | return status; |
| 1729 | |
| 1730 | status = _setup_secure_context(mmu); |
| 1731 | if (status) { |
| 1732 | _detach_context(&iommu->ctx[KGSL_IOMMU_CONTEXT_USER]); |
| 1733 | return status; |
| 1734 | } |
| 1735 | |
| 1736 | /* Make sure the hardware is programmed to the default pagetable */ |
| 1737 | return kgsl_iommu_set_pt(mmu, mmu->defaultpagetable); |
| 1738 | } |
| 1739 | |
| 1740 | static int |
| 1741 | kgsl_iommu_unmap_offset(struct kgsl_pagetable *pt, |
| 1742 | struct kgsl_memdesc *memdesc, uint64_t addr, |
| 1743 | uint64_t offset, uint64_t size) |
| 1744 | { |
| 1745 | if (size == 0 || (size + offset) > kgsl_memdesc_footprint(memdesc)) |
| 1746 | return -EINVAL; |
| 1747 | /* |
| 1748 | * All GPU addresses as assigned are page aligned, but some |
| 1749 | * functions perturb the gpuaddr with an offset, so apply the |
| 1750 | * mask here to make sure we have the right address. |
| 1751 | */ |
| 1752 | |
| 1753 | addr = PAGE_ALIGN(addr); |
| 1754 | if (addr == 0) |
| 1755 | return -EINVAL; |
| 1756 | |
| 1757 | return _iommu_unmap_sync_pc(pt, memdesc, addr + offset, size); |
| 1758 | } |
| 1759 | |
| 1760 | static int |
| 1761 | kgsl_iommu_unmap(struct kgsl_pagetable *pt, struct kgsl_memdesc *memdesc) |
| 1762 | { |
| 1763 | if (memdesc->size == 0 || memdesc->gpuaddr == 0) |
| 1764 | return -EINVAL; |
| 1765 | |
| 1766 | return kgsl_iommu_unmap_offset(pt, memdesc, memdesc->gpuaddr, 0, |
| 1767 | kgsl_memdesc_footprint(memdesc)); |
| 1768 | } |
| 1769 | |
| 1770 | /** |
| 1771 | * _iommu_map_guard_page - Map iommu guard page |
| 1772 | * @pt - Pointer to kgsl pagetable structure |
| 1773 | * @memdesc - memdesc to add guard page |
| 1774 | * @gpuaddr - GPU addr of guard page |
| 1775 | * @protflags - flags for mapping |
| 1776 | * |
| 1777 | * Return 0 on success, error on map fail |
| 1778 | */ |
| 1779 | static int _iommu_map_guard_page(struct kgsl_pagetable *pt, |
| 1780 | struct kgsl_memdesc *memdesc, |
| 1781 | uint64_t gpuaddr, |
| 1782 | unsigned int protflags) |
| 1783 | { |
| 1784 | phys_addr_t physaddr; |
| 1785 | |
| 1786 | if (!kgsl_memdesc_has_guard_page(memdesc)) |
| 1787 | return 0; |
| 1788 | |
| 1789 | /* |
| 1790 | * Allocate guard page for secure buffers. |
| 1791 | * This has to be done after we attach a smmu pagetable. |
| 1792 | * Allocate the guard page when first secure buffer is. |
| 1793 | * mapped to save 1MB of memory if CPZ is not used. |
| 1794 | */ |
| 1795 | if (kgsl_memdesc_is_secured(memdesc)) { |
| 1796 | struct scatterlist *sg; |
| 1797 | unsigned int sgp_size = pt->mmu->secure_align_mask + 1; |
| 1798 | |
| 1799 | if (!kgsl_secure_guard_page_memdesc.sgt) { |
| 1800 | if (kgsl_allocate_user(KGSL_MMU_DEVICE(pt->mmu), |
| 1801 | &kgsl_secure_guard_page_memdesc, |
| 1802 | sgp_size, KGSL_MEMFLAGS_SECURE)) { |
| 1803 | KGSL_CORE_ERR( |
| 1804 | "Secure guard page alloc failed\n"); |
| 1805 | return -ENOMEM; |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | sg = kgsl_secure_guard_page_memdesc.sgt->sgl; |
| 1810 | physaddr = page_to_phys(sg_page(sg)); |
| 1811 | } else { |
| 1812 | if (kgsl_guard_page == NULL) { |
| 1813 | kgsl_guard_page = alloc_page(GFP_KERNEL | __GFP_ZERO | |
| 1814 | __GFP_NORETRY | __GFP_HIGHMEM); |
| 1815 | if (kgsl_guard_page == NULL) |
| 1816 | return -ENOMEM; |
| 1817 | } |
| 1818 | |
| 1819 | physaddr = page_to_phys(kgsl_guard_page); |
| 1820 | } |
| 1821 | |
| 1822 | return _iommu_map_sync_pc(pt, memdesc, gpuaddr, physaddr, |
| 1823 | kgsl_memdesc_guard_page_size(memdesc), |
| 1824 | protflags & ~IOMMU_WRITE); |
| 1825 | } |
| 1826 | |
| 1827 | static unsigned int _get_protection_flags(struct kgsl_memdesc *memdesc) |
| 1828 | { |
Sushmita Susheelendra | 7f66cf7 | 2016-09-12 11:04:43 -0600 | [diff] [blame] | 1829 | unsigned int flags = IOMMU_READ | IOMMU_WRITE | |
| 1830 | IOMMU_NOEXEC | IOMMU_USE_UPSTREAM_HINT; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1831 | |
| 1832 | if (memdesc->flags & KGSL_MEMFLAGS_GPUREADONLY) |
| 1833 | flags &= ~IOMMU_WRITE; |
| 1834 | |
| 1835 | if (memdesc->priv & KGSL_MEMDESC_PRIVILEGED) |
| 1836 | flags |= IOMMU_PRIV; |
| 1837 | |
Shrenuj Bansal | 4fd6a56 | 2017-08-07 15:12:54 -0700 | [diff] [blame] | 1838 | if (memdesc->flags & KGSL_MEMFLAGS_IOCOHERENT) |
| 1839 | flags |= IOMMU_CACHE; |
| 1840 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1841 | return flags; |
| 1842 | } |
| 1843 | |
| 1844 | static int |
| 1845 | kgsl_iommu_map(struct kgsl_pagetable *pt, |
| 1846 | struct kgsl_memdesc *memdesc) |
| 1847 | { |
| 1848 | int ret; |
| 1849 | uint64_t addr = memdesc->gpuaddr; |
| 1850 | uint64_t size = memdesc->size; |
| 1851 | unsigned int flags = _get_protection_flags(memdesc); |
| 1852 | struct sg_table *sgt = NULL; |
| 1853 | |
| 1854 | /* |
| 1855 | * For paged memory allocated through kgsl, memdesc->pages is not NULL. |
| 1856 | * Allocate sgt here just for its map operation. Contiguous memory |
| 1857 | * already has its sgt, so no need to allocate it here. |
| 1858 | */ |
| 1859 | if (memdesc->pages != NULL) |
| 1860 | sgt = kgsl_alloc_sgt_from_pages(memdesc); |
| 1861 | else |
| 1862 | sgt = memdesc->sgt; |
| 1863 | |
| 1864 | if (IS_ERR(sgt)) |
| 1865 | return PTR_ERR(sgt); |
| 1866 | |
| 1867 | ret = _iommu_map_sg_sync_pc(pt, addr, memdesc, sgt->sgl, |
| 1868 | sgt->nents, flags); |
| 1869 | if (ret) |
| 1870 | goto done; |
| 1871 | |
| 1872 | ret = _iommu_map_guard_page(pt, memdesc, addr + size, flags); |
| 1873 | if (ret) |
| 1874 | _iommu_unmap_sync_pc(pt, memdesc, addr, size); |
| 1875 | |
| 1876 | done: |
| 1877 | if (memdesc->pages != NULL) |
| 1878 | kgsl_free_sgt(sgt); |
| 1879 | |
| 1880 | return ret; |
| 1881 | } |
| 1882 | |
| 1883 | static int kgsl_iommu_sparse_dummy_map(struct kgsl_pagetable *pt, |
| 1884 | struct kgsl_memdesc *memdesc, uint64_t offset, uint64_t size) |
| 1885 | { |
| 1886 | int ret = 0, i; |
| 1887 | struct page **pages = NULL; |
| 1888 | struct sg_table sgt; |
| 1889 | int count = size >> PAGE_SHIFT; |
| 1890 | |
| 1891 | /* verify the offset is within our range */ |
| 1892 | if (size + offset > memdesc->size) |
| 1893 | return -EINVAL; |
| 1894 | |
| 1895 | if (kgsl_dummy_page == NULL) { |
| 1896 | kgsl_dummy_page = alloc_page(GFP_KERNEL | __GFP_ZERO | |
| 1897 | __GFP_HIGHMEM); |
| 1898 | if (kgsl_dummy_page == NULL) |
| 1899 | return -ENOMEM; |
| 1900 | } |
| 1901 | |
| 1902 | pages = kcalloc(count, sizeof(struct page *), GFP_KERNEL); |
| 1903 | if (pages == NULL) |
| 1904 | return -ENOMEM; |
| 1905 | |
| 1906 | for (i = 0; i < count; i++) |
| 1907 | pages[i] = kgsl_dummy_page; |
| 1908 | |
| 1909 | ret = sg_alloc_table_from_pages(&sgt, pages, count, |
| 1910 | 0, size, GFP_KERNEL); |
| 1911 | if (ret == 0) { |
| 1912 | ret = _iommu_map_sg_sync_pc(pt, memdesc->gpuaddr + offset, |
| 1913 | memdesc, sgt.sgl, sgt.nents, |
| 1914 | IOMMU_READ | IOMMU_NOEXEC); |
| 1915 | sg_free_table(&sgt); |
| 1916 | } |
| 1917 | |
| 1918 | kfree(pages); |
| 1919 | |
| 1920 | return ret; |
| 1921 | } |
| 1922 | |
| 1923 | static int _map_to_one_page(struct kgsl_pagetable *pt, uint64_t addr, |
| 1924 | struct kgsl_memdesc *memdesc, uint64_t physoffset, |
| 1925 | uint64_t size, unsigned int map_flags) |
| 1926 | { |
| 1927 | int ret = 0, i; |
| 1928 | int pg_sz = kgsl_memdesc_get_pagesize(memdesc); |
| 1929 | int count = size >> PAGE_SHIFT; |
| 1930 | struct page *page = NULL; |
| 1931 | struct page **pages = NULL; |
| 1932 | struct sg_page_iter sg_iter; |
| 1933 | struct sg_table sgt; |
| 1934 | |
| 1935 | /* Find our physaddr offset addr */ |
| 1936 | if (memdesc->pages != NULL) |
| 1937 | page = memdesc->pages[physoffset >> PAGE_SHIFT]; |
| 1938 | else { |
| 1939 | for_each_sg_page(memdesc->sgt->sgl, &sg_iter, |
| 1940 | memdesc->sgt->nents, physoffset >> PAGE_SHIFT) { |
| 1941 | page = sg_page_iter_page(&sg_iter); |
| 1942 | break; |
| 1943 | } |
| 1944 | } |
| 1945 | |
| 1946 | if (page == NULL) |
| 1947 | return -EINVAL; |
| 1948 | |
| 1949 | pages = kcalloc(count, sizeof(struct page *), GFP_KERNEL); |
| 1950 | if (pages == NULL) |
| 1951 | return -ENOMEM; |
| 1952 | |
| 1953 | for (i = 0; i < count; i++) { |
| 1954 | if (pg_sz != PAGE_SIZE) { |
| 1955 | struct page *tmp_page = page; |
| 1956 | int j; |
| 1957 | |
| 1958 | for (j = 0; j < 16; j++, tmp_page += PAGE_SIZE) |
| 1959 | pages[i++] = tmp_page; |
| 1960 | } else |
| 1961 | pages[i] = page; |
| 1962 | } |
| 1963 | |
| 1964 | ret = sg_alloc_table_from_pages(&sgt, pages, count, |
| 1965 | 0, size, GFP_KERNEL); |
| 1966 | if (ret == 0) { |
| 1967 | ret = _iommu_map_sg_sync_pc(pt, addr, memdesc, sgt.sgl, |
| 1968 | sgt.nents, map_flags); |
| 1969 | sg_free_table(&sgt); |
| 1970 | } |
| 1971 | |
| 1972 | kfree(pages); |
| 1973 | |
| 1974 | return ret; |
| 1975 | } |
| 1976 | |
| 1977 | static int kgsl_iommu_map_offset(struct kgsl_pagetable *pt, |
| 1978 | uint64_t virtaddr, uint64_t virtoffset, |
| 1979 | struct kgsl_memdesc *memdesc, uint64_t physoffset, |
| 1980 | uint64_t size, uint64_t feature_flag) |
| 1981 | { |
| 1982 | int pg_sz; |
| 1983 | unsigned int protflags = _get_protection_flags(memdesc); |
| 1984 | int ret; |
| 1985 | struct sg_table *sgt = NULL; |
| 1986 | |
| 1987 | pg_sz = kgsl_memdesc_get_pagesize(memdesc); |
| 1988 | if (!IS_ALIGNED(virtaddr | virtoffset | physoffset | size, pg_sz)) |
| 1989 | return -EINVAL; |
| 1990 | |
| 1991 | if (size == 0) |
| 1992 | return -EINVAL; |
| 1993 | |
| 1994 | if (!(feature_flag & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS) && |
| 1995 | size + physoffset > kgsl_memdesc_footprint(memdesc)) |
| 1996 | return -EINVAL; |
| 1997 | |
| 1998 | /* |
| 1999 | * For paged memory allocated through kgsl, memdesc->pages is not NULL. |
| 2000 | * Allocate sgt here just for its map operation. Contiguous memory |
| 2001 | * already has its sgt, so no need to allocate it here. |
| 2002 | */ |
| 2003 | if (memdesc->pages != NULL) |
| 2004 | sgt = kgsl_alloc_sgt_from_pages(memdesc); |
| 2005 | else |
| 2006 | sgt = memdesc->sgt; |
| 2007 | |
| 2008 | if (IS_ERR(sgt)) |
| 2009 | return PTR_ERR(sgt); |
| 2010 | |
| 2011 | if (feature_flag & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS) |
| 2012 | ret = _map_to_one_page(pt, virtaddr + virtoffset, |
| 2013 | memdesc, physoffset, size, protflags); |
| 2014 | else |
| 2015 | ret = _iommu_map_sg_offset_sync_pc(pt, virtaddr + virtoffset, |
| 2016 | memdesc, sgt->sgl, sgt->nents, |
| 2017 | physoffset, size, protflags); |
| 2018 | |
| 2019 | if (memdesc->pages != NULL) |
| 2020 | kgsl_free_sgt(sgt); |
| 2021 | |
| 2022 | return ret; |
| 2023 | } |
| 2024 | |
| 2025 | /* This function must be called with context bank attached */ |
| 2026 | static void kgsl_iommu_clear_fsr(struct kgsl_mmu *mmu) |
| 2027 | { |
| 2028 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 2029 | struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER]; |
| 2030 | unsigned int sctlr_val; |
| 2031 | |
| 2032 | if (ctx->default_pt != NULL) { |
| 2033 | kgsl_iommu_enable_clk(mmu); |
| 2034 | KGSL_IOMMU_SET_CTX_REG(ctx, FSR, 0xffffffff); |
| 2035 | /* |
| 2036 | * Re-enable context fault interrupts after clearing |
| 2037 | * FSR to prevent the interrupt from firing repeatedly |
| 2038 | */ |
| 2039 | sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR); |
| 2040 | sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFIE_SHIFT); |
| 2041 | KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val); |
| 2042 | /* |
| 2043 | * Make sure the above register writes |
| 2044 | * are not reordered across the barrier |
| 2045 | * as we use writel_relaxed to write them |
| 2046 | */ |
| 2047 | wmb(); |
| 2048 | kgsl_iommu_disable_clk(mmu); |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | static void kgsl_iommu_pagefault_resume(struct kgsl_mmu *mmu) |
| 2053 | { |
| 2054 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 2055 | struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER]; |
| 2056 | |
| 2057 | if (ctx->default_pt != NULL && ctx->fault) { |
| 2058 | /* |
| 2059 | * Write 1 to RESUME.TnR to terminate the |
| 2060 | * stalled transaction. |
| 2061 | */ |
| 2062 | KGSL_IOMMU_SET_CTX_REG(ctx, RESUME, 1); |
| 2063 | /* |
| 2064 | * Make sure the above register writes |
| 2065 | * are not reordered across the barrier |
| 2066 | * as we use writel_relaxed to write them |
| 2067 | */ |
| 2068 | wmb(); |
| 2069 | ctx->fault = 0; |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | static void kgsl_iommu_stop(struct kgsl_mmu *mmu) |
| 2074 | { |
| 2075 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 2076 | int i; |
| 2077 | |
| 2078 | /* |
| 2079 | * If the iommu supports retention, we don't need |
| 2080 | * to detach when stopping. |
| 2081 | */ |
| 2082 | if (!MMU_FEATURE(mmu, KGSL_MMU_RETENTION)) { |
| 2083 | for (i = 0; i < KGSL_IOMMU_CONTEXT_MAX; i++) |
| 2084 | _detach_context(&iommu->ctx[i]); |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | static u64 |
| 2089 | kgsl_iommu_get_current_ttbr0(struct kgsl_mmu *mmu) |
| 2090 | { |
| 2091 | u64 val; |
| 2092 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 2093 | /* |
| 2094 | * We cannot enable or disable the clocks in interrupt context, this |
| 2095 | * function is called from interrupt context if there is an axi error |
| 2096 | */ |
| 2097 | if (in_interrupt()) |
| 2098 | return 0; |
| 2099 | |
| 2100 | kgsl_iommu_enable_clk(mmu); |
| 2101 | val = KGSL_IOMMU_GET_CTX_REG_Q(&iommu->ctx[KGSL_IOMMU_CONTEXT_USER], |
| 2102 | TTBR0); |
| 2103 | kgsl_iommu_disable_clk(mmu); |
| 2104 | return val; |
| 2105 | } |
| 2106 | |
| 2107 | /* |
| 2108 | * kgsl_iommu_set_pt - Change the IOMMU pagetable of the primary context bank |
| 2109 | * @mmu - Pointer to mmu structure |
| 2110 | * @pt - Pagetable to switch to |
| 2111 | * |
| 2112 | * Set the new pagetable for the IOMMU by doing direct register writes |
| 2113 | * to the IOMMU registers through the cpu |
| 2114 | * |
| 2115 | * Return - void |
| 2116 | */ |
| 2117 | static int kgsl_iommu_set_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt) |
| 2118 | { |
| 2119 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 2120 | struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER]; |
| 2121 | uint64_t ttbr0, temp; |
| 2122 | unsigned int contextidr; |
| 2123 | unsigned long wait_for_flush; |
| 2124 | |
| 2125 | if ((pt != mmu->defaultpagetable) && !kgsl_mmu_is_perprocess(mmu)) |
| 2126 | return 0; |
| 2127 | |
| 2128 | kgsl_iommu_enable_clk(mmu); |
| 2129 | |
| 2130 | ttbr0 = kgsl_mmu_pagetable_get_ttbr0(pt); |
| 2131 | contextidr = kgsl_mmu_pagetable_get_contextidr(pt); |
| 2132 | |
| 2133 | KGSL_IOMMU_SET_CTX_REG_Q(ctx, TTBR0, ttbr0); |
| 2134 | KGSL_IOMMU_SET_CTX_REG(ctx, CONTEXTIDR, contextidr); |
| 2135 | |
| 2136 | /* memory barrier before reading TTBR0 register */ |
| 2137 | mb(); |
| 2138 | temp = KGSL_IOMMU_GET_CTX_REG_Q(ctx, TTBR0); |
| 2139 | |
| 2140 | KGSL_IOMMU_SET_CTX_REG(ctx, TLBIALL, 1); |
| 2141 | /* make sure the TBLI write completes before we wait */ |
| 2142 | mb(); |
| 2143 | /* |
| 2144 | * Wait for flush to complete by polling the flush |
| 2145 | * status bit of TLBSTATUS register for not more than |
| 2146 | * 2 s. After 2s just exit, at that point the SMMU h/w |
| 2147 | * may be stuck and will eventually cause GPU to hang |
| 2148 | * or bring the system down. |
| 2149 | */ |
| 2150 | wait_for_flush = jiffies + msecs_to_jiffies(2000); |
| 2151 | KGSL_IOMMU_SET_CTX_REG(ctx, TLBSYNC, 0); |
| 2152 | while (KGSL_IOMMU_GET_CTX_REG(ctx, TLBSTATUS) & |
| 2153 | (KGSL_IOMMU_CTX_TLBSTATUS_SACTIVE)) { |
| 2154 | if (time_after(jiffies, wait_for_flush)) { |
| 2155 | KGSL_DRV_WARN(KGSL_MMU_DEVICE(mmu), |
| 2156 | "Wait limit reached for IOMMU tlb flush\n"); |
| 2157 | break; |
| 2158 | } |
| 2159 | cpu_relax(); |
| 2160 | } |
| 2161 | |
| 2162 | kgsl_iommu_disable_clk(mmu); |
| 2163 | return 0; |
| 2164 | } |
| 2165 | |
| 2166 | /* |
| 2167 | * kgsl_iommu_set_pf_policy() - Set the pagefault policy for IOMMU |
| 2168 | * @mmu: Pointer to mmu structure |
| 2169 | * @pf_policy: The pagefault polict to set |
| 2170 | * |
| 2171 | * Check if the new policy indicated by pf_policy is same as current |
| 2172 | * policy, if same then return else set the policy |
| 2173 | */ |
| 2174 | static int kgsl_iommu_set_pf_policy(struct kgsl_mmu *mmu, |
| 2175 | unsigned long pf_policy) |
| 2176 | { |
| 2177 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 2178 | struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER]; |
| 2179 | struct kgsl_device *device = KGSL_MMU_DEVICE(mmu); |
| 2180 | struct adreno_device *adreno_dev = ADRENO_DEVICE(device); |
| 2181 | |
| 2182 | if ((adreno_dev->ft_pf_policy & |
| 2183 | BIT(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE)) == |
| 2184 | (pf_policy & BIT(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE))) |
| 2185 | return 0; |
| 2186 | |
| 2187 | /* If not attached, policy will be updated during the next attach */ |
| 2188 | if (ctx->default_pt != NULL) { |
| 2189 | unsigned int sctlr_val; |
| 2190 | |
| 2191 | kgsl_iommu_enable_clk(mmu); |
| 2192 | |
| 2193 | sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR); |
| 2194 | |
| 2195 | if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, &pf_policy)) { |
| 2196 | sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT); |
| 2197 | sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT); |
| 2198 | } else { |
| 2199 | sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT); |
| 2200 | sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT); |
| 2201 | } |
| 2202 | |
| 2203 | KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val); |
| 2204 | |
| 2205 | kgsl_iommu_disable_clk(mmu); |
| 2206 | } |
| 2207 | |
| 2208 | return 0; |
| 2209 | } |
| 2210 | |
| 2211 | static struct kgsl_protected_registers * |
| 2212 | kgsl_iommu_get_prot_regs(struct kgsl_mmu *mmu) |
| 2213 | { |
| 2214 | struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu); |
| 2215 | |
| 2216 | return &iommu->protect; |
| 2217 | } |
| 2218 | |
| 2219 | static struct kgsl_iommu_addr_entry *_find_gpuaddr( |
| 2220 | struct kgsl_pagetable *pagetable, uint64_t gpuaddr) |
| 2221 | { |
| 2222 | struct kgsl_iommu_pt *pt = pagetable->priv; |
| 2223 | struct rb_node *node = pt->rbtree.rb_node; |
| 2224 | |
| 2225 | while (node != NULL) { |
| 2226 | struct kgsl_iommu_addr_entry *entry = rb_entry(node, |
| 2227 | struct kgsl_iommu_addr_entry, node); |
| 2228 | |
| 2229 | if (gpuaddr < entry->base) |
| 2230 | node = node->rb_left; |
| 2231 | else if (gpuaddr > entry->base) |
| 2232 | node = node->rb_right; |
| 2233 | else |
| 2234 | return entry; |
| 2235 | } |
| 2236 | |
| 2237 | return NULL; |
| 2238 | } |
| 2239 | |
| 2240 | static int _remove_gpuaddr(struct kgsl_pagetable *pagetable, |
| 2241 | uint64_t gpuaddr) |
| 2242 | { |
| 2243 | struct kgsl_iommu_pt *pt = pagetable->priv; |
| 2244 | struct kgsl_iommu_addr_entry *entry; |
| 2245 | |
| 2246 | entry = _find_gpuaddr(pagetable, gpuaddr); |
| 2247 | |
| 2248 | if (entry != NULL) { |
| 2249 | rb_erase(&entry->node, &pt->rbtree); |
| 2250 | kmem_cache_free(addr_entry_cache, entry); |
| 2251 | return 0; |
| 2252 | } |
| 2253 | |
| 2254 | WARN(1, "Couldn't remove gpuaddr: 0x%llx\n", gpuaddr); |
| 2255 | return -ENOMEM; |
| 2256 | } |
| 2257 | |
| 2258 | static int _insert_gpuaddr(struct kgsl_pagetable *pagetable, |
| 2259 | uint64_t gpuaddr, uint64_t size) |
| 2260 | { |
| 2261 | struct kgsl_iommu_pt *pt = pagetable->priv; |
| 2262 | struct rb_node **node, *parent = NULL; |
| 2263 | struct kgsl_iommu_addr_entry *new = |
| 2264 | kmem_cache_alloc(addr_entry_cache, GFP_ATOMIC); |
| 2265 | |
| 2266 | if (new == NULL) |
| 2267 | return -ENOMEM; |
| 2268 | |
| 2269 | new->base = gpuaddr; |
| 2270 | new->size = size; |
| 2271 | |
| 2272 | node = &pt->rbtree.rb_node; |
| 2273 | |
| 2274 | while (*node != NULL) { |
| 2275 | struct kgsl_iommu_addr_entry *this; |
| 2276 | |
| 2277 | parent = *node; |
| 2278 | this = rb_entry(parent, struct kgsl_iommu_addr_entry, node); |
| 2279 | |
| 2280 | if (new->base < this->base) |
| 2281 | node = &parent->rb_left; |
| 2282 | else if (new->base > this->base) |
| 2283 | node = &parent->rb_right; |
| 2284 | else { |
| 2285 | /* Duplicate entry */ |
| 2286 | WARN(1, "duplicate gpuaddr: 0x%llx\n", gpuaddr); |
| 2287 | return -EEXIST; |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | rb_link_node(&new->node, parent, node); |
| 2292 | rb_insert_color(&new->node, &pt->rbtree); |
| 2293 | |
| 2294 | return 0; |
| 2295 | } |
| 2296 | |
| 2297 | static uint64_t _get_unmapped_area(struct kgsl_pagetable *pagetable, |
| 2298 | uint64_t bottom, uint64_t top, uint64_t size, |
| 2299 | uint64_t align) |
| 2300 | { |
| 2301 | struct kgsl_iommu_pt *pt = pagetable->priv; |
| 2302 | struct rb_node *node = rb_first(&pt->rbtree); |
| 2303 | uint64_t start; |
| 2304 | |
| 2305 | bottom = ALIGN(bottom, align); |
| 2306 | start = bottom; |
| 2307 | |
| 2308 | while (node != NULL) { |
| 2309 | uint64_t gap; |
| 2310 | struct kgsl_iommu_addr_entry *entry = rb_entry(node, |
| 2311 | struct kgsl_iommu_addr_entry, node); |
| 2312 | |
| 2313 | /* |
| 2314 | * Skip any entries that are outside of the range, but make sure |
| 2315 | * to account for some that might straddle the lower bound |
| 2316 | */ |
| 2317 | if (entry->base < bottom) { |
| 2318 | if (entry->base + entry->size > bottom) |
| 2319 | start = ALIGN(entry->base + entry->size, align); |
| 2320 | node = rb_next(node); |
| 2321 | continue; |
| 2322 | } |
| 2323 | |
| 2324 | /* Stop if we went over the top */ |
| 2325 | if (entry->base >= top) |
| 2326 | break; |
| 2327 | |
| 2328 | /* Make sure there is a gap to consider */ |
| 2329 | if (start < entry->base) { |
| 2330 | gap = entry->base - start; |
| 2331 | |
| 2332 | if (gap >= size) |
| 2333 | return start; |
| 2334 | } |
| 2335 | |
| 2336 | /* Stop if there is no more room in the region */ |
| 2337 | if (entry->base + entry->size >= top) |
| 2338 | return (uint64_t) -ENOMEM; |
| 2339 | |
| 2340 | /* Start the next cycle at the end of the current entry */ |
| 2341 | start = ALIGN(entry->base + entry->size, align); |
| 2342 | node = rb_next(node); |
| 2343 | } |
| 2344 | |
| 2345 | if (start + size <= top) |
| 2346 | return start; |
| 2347 | |
| 2348 | return (uint64_t) -ENOMEM; |
| 2349 | } |
| 2350 | |
| 2351 | static uint64_t _get_unmapped_area_topdown(struct kgsl_pagetable *pagetable, |
| 2352 | uint64_t bottom, uint64_t top, uint64_t size, |
| 2353 | uint64_t align) |
| 2354 | { |
| 2355 | struct kgsl_iommu_pt *pt = pagetable->priv; |
| 2356 | struct rb_node *node = rb_last(&pt->rbtree); |
| 2357 | uint64_t end = top; |
| 2358 | uint64_t mask = ~(align - 1); |
| 2359 | struct kgsl_iommu_addr_entry *entry; |
| 2360 | |
| 2361 | /* Make sure that the bottom is correctly aligned */ |
| 2362 | bottom = ALIGN(bottom, align); |
| 2363 | |
| 2364 | /* Make sure the requested size will fit in the range */ |
| 2365 | if (size > (top - bottom)) |
| 2366 | return -ENOMEM; |
| 2367 | |
| 2368 | /* Walk back through the list to find the highest entry in the range */ |
| 2369 | for (node = rb_last(&pt->rbtree); node != NULL; node = rb_prev(node)) { |
| 2370 | entry = rb_entry(node, struct kgsl_iommu_addr_entry, node); |
| 2371 | if (entry->base < top) |
| 2372 | break; |
| 2373 | } |
| 2374 | |
| 2375 | while (node != NULL) { |
| 2376 | uint64_t offset; |
| 2377 | |
| 2378 | entry = rb_entry(node, struct kgsl_iommu_addr_entry, node); |
| 2379 | |
| 2380 | /* If the entire entry is below the range the search is over */ |
| 2381 | if ((entry->base + entry->size) < bottom) |
| 2382 | break; |
| 2383 | |
| 2384 | /* Get the top of the entry properly aligned */ |
| 2385 | offset = ALIGN(entry->base + entry->size, align); |
| 2386 | |
| 2387 | /* |
| 2388 | * Try to allocate the memory from the top of the gap, |
| 2389 | * making sure that it fits between the top of this entry and |
| 2390 | * the bottom of the previous one |
| 2391 | */ |
| 2392 | |
| 2393 | if ((end > size) && (offset < end)) { |
| 2394 | uint64_t chunk = (end - size) & mask; |
| 2395 | |
| 2396 | if (chunk >= offset) |
| 2397 | return chunk; |
| 2398 | } |
| 2399 | |
| 2400 | /* |
| 2401 | * If we get here and the current entry is outside of the range |
| 2402 | * then we are officially out of room |
| 2403 | */ |
| 2404 | |
| 2405 | if (entry->base < bottom) |
| 2406 | return (uint64_t) -ENOMEM; |
| 2407 | |
| 2408 | /* Set the top of the gap to the current entry->base */ |
| 2409 | end = entry->base; |
| 2410 | |
| 2411 | /* And move on to the next lower entry */ |
| 2412 | node = rb_prev(node); |
| 2413 | } |
| 2414 | |
| 2415 | /* If we get here then there are no more entries in the region */ |
| 2416 | if ((end > size) && (((end - size) & mask) >= bottom)) |
| 2417 | return (end - size) & mask; |
| 2418 | |
| 2419 | return (uint64_t) -ENOMEM; |
| 2420 | } |
| 2421 | |
| 2422 | static uint64_t kgsl_iommu_find_svm_region(struct kgsl_pagetable *pagetable, |
| 2423 | uint64_t start, uint64_t end, uint64_t size, |
| 2424 | uint64_t alignment) |
| 2425 | { |
| 2426 | uint64_t addr; |
| 2427 | |
| 2428 | /* Avoid black holes */ |
| 2429 | if (WARN(end <= start, "Bad search range: 0x%llx-0x%llx", start, end)) |
| 2430 | return (uint64_t) -EINVAL; |
| 2431 | |
| 2432 | spin_lock(&pagetable->lock); |
| 2433 | addr = _get_unmapped_area_topdown(pagetable, |
| 2434 | start, end, size, alignment); |
| 2435 | spin_unlock(&pagetable->lock); |
| 2436 | return addr; |
| 2437 | } |
| 2438 | |
| 2439 | static int kgsl_iommu_set_svm_region(struct kgsl_pagetable *pagetable, |
| 2440 | uint64_t gpuaddr, uint64_t size) |
| 2441 | { |
| 2442 | int ret = -ENOMEM; |
| 2443 | struct kgsl_iommu_pt *pt = pagetable->priv; |
| 2444 | struct rb_node *node; |
| 2445 | |
| 2446 | /* Make sure the requested address doesn't fall in the global range */ |
| 2447 | if (ADDR_IN_GLOBAL(gpuaddr) || ADDR_IN_GLOBAL(gpuaddr + size)) |
| 2448 | return -ENOMEM; |
| 2449 | |
| 2450 | spin_lock(&pagetable->lock); |
| 2451 | node = pt->rbtree.rb_node; |
| 2452 | |
| 2453 | while (node != NULL) { |
| 2454 | uint64_t start, end; |
| 2455 | struct kgsl_iommu_addr_entry *entry = rb_entry(node, |
| 2456 | struct kgsl_iommu_addr_entry, node); |
| 2457 | |
| 2458 | start = entry->base; |
| 2459 | end = entry->base + entry->size; |
| 2460 | |
| 2461 | if (gpuaddr + size <= start) |
| 2462 | node = node->rb_left; |
| 2463 | else if (end <= gpuaddr) |
| 2464 | node = node->rb_right; |
| 2465 | else |
| 2466 | goto out; |
| 2467 | } |
| 2468 | |
| 2469 | ret = _insert_gpuaddr(pagetable, gpuaddr, size); |
| 2470 | out: |
| 2471 | spin_unlock(&pagetable->lock); |
| 2472 | return ret; |
| 2473 | } |
| 2474 | |
| 2475 | |
| 2476 | static int kgsl_iommu_get_gpuaddr(struct kgsl_pagetable *pagetable, |
| 2477 | struct kgsl_memdesc *memdesc) |
| 2478 | { |
| 2479 | struct kgsl_iommu_pt *pt = pagetable->priv; |
| 2480 | int ret = 0; |
| 2481 | uint64_t addr, start, end, size; |
| 2482 | unsigned int align; |
| 2483 | |
| 2484 | if (WARN_ON(kgsl_memdesc_use_cpu_map(memdesc))) |
| 2485 | return -EINVAL; |
| 2486 | |
| 2487 | if (memdesc->flags & KGSL_MEMFLAGS_SECURE && |
| 2488 | pagetable->name != KGSL_MMU_SECURE_PT) |
| 2489 | return -EINVAL; |
| 2490 | |
| 2491 | size = kgsl_memdesc_footprint(memdesc); |
| 2492 | |
| 2493 | align = 1 << kgsl_memdesc_get_align(memdesc); |
| 2494 | |
| 2495 | if (memdesc->flags & KGSL_MEMFLAGS_FORCE_32BIT) { |
| 2496 | start = pt->compat_va_start; |
| 2497 | end = pt->compat_va_end; |
| 2498 | } else { |
| 2499 | start = pt->va_start; |
| 2500 | end = pt->va_end; |
| 2501 | } |
| 2502 | |
Harshdeep Dhatt | 1f40833 | 2017-03-27 11:35:13 -0600 | [diff] [blame] | 2503 | /* |
| 2504 | * When mapping secure buffers, adjust the start of the va range |
| 2505 | * to the end of secure global buffers. |
| 2506 | */ |
| 2507 | if (kgsl_memdesc_is_secured(memdesc)) |
| 2508 | start += secure_global_size; |
| 2509 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 2510 | spin_lock(&pagetable->lock); |
| 2511 | |
| 2512 | addr = _get_unmapped_area(pagetable, start, end, size, align); |
| 2513 | |
| 2514 | if (addr == (uint64_t) -ENOMEM) { |
| 2515 | ret = -ENOMEM; |
| 2516 | goto out; |
| 2517 | } |
| 2518 | |
| 2519 | ret = _insert_gpuaddr(pagetable, addr, size); |
| 2520 | if (ret == 0) { |
| 2521 | memdesc->gpuaddr = addr; |
| 2522 | memdesc->pagetable = pagetable; |
| 2523 | } |
| 2524 | |
| 2525 | out: |
| 2526 | spin_unlock(&pagetable->lock); |
| 2527 | return ret; |
| 2528 | } |
| 2529 | |
| 2530 | static void kgsl_iommu_put_gpuaddr(struct kgsl_memdesc *memdesc) |
| 2531 | { |
| 2532 | if (memdesc->pagetable == NULL) |
| 2533 | return; |
| 2534 | |
| 2535 | spin_lock(&memdesc->pagetable->lock); |
| 2536 | |
| 2537 | _remove_gpuaddr(memdesc->pagetable, memdesc->gpuaddr); |
| 2538 | |
| 2539 | spin_unlock(&memdesc->pagetable->lock); |
| 2540 | } |
| 2541 | |
| 2542 | static int kgsl_iommu_svm_range(struct kgsl_pagetable *pagetable, |
| 2543 | uint64_t *lo, uint64_t *hi, uint64_t memflags) |
| 2544 | { |
| 2545 | struct kgsl_iommu_pt *pt = pagetable->priv; |
| 2546 | bool gpu_compat = (memflags & KGSL_MEMFLAGS_FORCE_32BIT) != 0; |
| 2547 | |
| 2548 | if (lo != NULL) |
| 2549 | *lo = gpu_compat ? pt->compat_va_start : pt->svm_start; |
| 2550 | if (hi != NULL) |
| 2551 | *hi = gpu_compat ? pt->compat_va_end : pt->svm_end; |
| 2552 | |
| 2553 | return 0; |
| 2554 | } |
| 2555 | |
| 2556 | static bool kgsl_iommu_addr_in_range(struct kgsl_pagetable *pagetable, |
| 2557 | uint64_t gpuaddr) |
| 2558 | { |
| 2559 | struct kgsl_iommu_pt *pt = pagetable->priv; |
| 2560 | |
| 2561 | if (gpuaddr == 0) |
| 2562 | return false; |
| 2563 | |
| 2564 | if (gpuaddr >= pt->va_start && gpuaddr < pt->va_end) |
| 2565 | return true; |
| 2566 | |
| 2567 | if (gpuaddr >= pt->compat_va_start && gpuaddr < pt->compat_va_end) |
| 2568 | return true; |
| 2569 | |
| 2570 | if (gpuaddr >= pt->svm_start && gpuaddr < pt->svm_end) |
| 2571 | return true; |
| 2572 | |
| 2573 | return false; |
| 2574 | } |
| 2575 | |
| 2576 | static const struct { |
| 2577 | int id; |
| 2578 | char *name; |
| 2579 | } kgsl_iommu_cbs[] = { |
| 2580 | { KGSL_IOMMU_CONTEXT_USER, "gfx3d_user", }, |
| 2581 | { KGSL_IOMMU_CONTEXT_SECURE, "gfx3d_secure" }, |
| 2582 | }; |
| 2583 | |
| 2584 | static int _kgsl_iommu_cb_probe(struct kgsl_device *device, |
| 2585 | struct kgsl_iommu *iommu, struct device_node *node) |
| 2586 | { |
| 2587 | struct platform_device *pdev = of_find_device_by_node(node); |
| 2588 | struct kgsl_iommu_context *ctx = NULL; |
| 2589 | int i; |
| 2590 | |
| 2591 | for (i = 0; i < ARRAY_SIZE(kgsl_iommu_cbs); i++) { |
| 2592 | if (!strcmp(node->name, kgsl_iommu_cbs[i].name)) { |
| 2593 | int id = kgsl_iommu_cbs[i].id; |
| 2594 | |
| 2595 | ctx = &iommu->ctx[id]; |
| 2596 | ctx->id = id; |
| 2597 | ctx->cb_num = -1; |
| 2598 | ctx->name = kgsl_iommu_cbs[i].name; |
| 2599 | |
| 2600 | break; |
| 2601 | } |
| 2602 | } |
| 2603 | |
| 2604 | if (ctx == NULL) { |
| 2605 | KGSL_CORE_ERR("dt: Unknown context label %s\n", node->name); |
| 2606 | return -EINVAL; |
| 2607 | } |
| 2608 | |
| 2609 | if (ctx->id == KGSL_IOMMU_CONTEXT_SECURE) |
| 2610 | device->mmu.secured = true; |
| 2611 | |
| 2612 | /* this property won't be found for all context banks */ |
| 2613 | if (of_property_read_u32(node, "qcom,gpu-offset", &ctx->gpu_offset)) |
| 2614 | ctx->gpu_offset = UINT_MAX; |
| 2615 | |
| 2616 | ctx->kgsldev = device; |
| 2617 | |
| 2618 | /* arm-smmu driver we'll have the right device pointer here. */ |
| 2619 | if (of_find_property(node, "iommus", NULL)) { |
| 2620 | ctx->dev = &pdev->dev; |
| 2621 | } else { |
| 2622 | ctx->dev = kgsl_mmu_get_ctx(ctx->name); |
| 2623 | |
| 2624 | if (IS_ERR(ctx->dev)) |
| 2625 | return PTR_ERR(ctx->dev); |
| 2626 | } |
| 2627 | |
| 2628 | return 0; |
| 2629 | } |
| 2630 | |
| 2631 | static const struct { |
| 2632 | char *feature; |
Lynus Vaz | eb7af68 | 2017-04-17 18:36:01 +0530 | [diff] [blame] | 2633 | unsigned long bit; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 2634 | } kgsl_iommu_features[] = { |
| 2635 | { "qcom,retention", KGSL_MMU_RETENTION }, |
| 2636 | { "qcom,global_pt", KGSL_MMU_GLOBAL_PAGETABLE }, |
| 2637 | { "qcom,hyp_secure_alloc", KGSL_MMU_HYP_SECURE_ALLOC }, |
| 2638 | { "qcom,force-32bit", KGSL_MMU_FORCE_32BIT }, |
| 2639 | }; |
| 2640 | |
| 2641 | static int _kgsl_iommu_probe(struct kgsl_device *device, |
| 2642 | struct device_node *node) |
| 2643 | { |
| 2644 | const char *cname; |
| 2645 | struct property *prop; |
| 2646 | u32 reg_val[2]; |
| 2647 | int i = 0; |
| 2648 | struct kgsl_iommu *iommu = KGSL_IOMMU_PRIV(device); |
| 2649 | struct device_node *child; |
| 2650 | struct platform_device *pdev = of_find_device_by_node(node); |
| 2651 | |
| 2652 | memset(iommu, 0, sizeof(*iommu)); |
| 2653 | |
| 2654 | if (of_device_is_compatible(node, "qcom,kgsl-smmu-v1")) |
| 2655 | iommu->version = 1; |
| 2656 | else |
| 2657 | iommu->version = 2; |
| 2658 | |
| 2659 | if (of_property_read_u32_array(node, "reg", reg_val, 2)) { |
| 2660 | KGSL_CORE_ERR("dt: Unable to read KGSL IOMMU register range\n"); |
| 2661 | return -EINVAL; |
| 2662 | } |
| 2663 | iommu->regstart = reg_val[0]; |
| 2664 | iommu->regsize = reg_val[1]; |
| 2665 | |
| 2666 | /* Protecting the SMMU registers is mandatory */ |
| 2667 | if (of_property_read_u32_array(node, "qcom,protect", reg_val, 2)) { |
| 2668 | KGSL_CORE_ERR("dt: no iommu protection range specified\n"); |
| 2669 | return -EINVAL; |
| 2670 | } |
| 2671 | iommu->protect.base = reg_val[0] / sizeof(u32); |
| 2672 | iommu->protect.range = ilog2(reg_val[1] / sizeof(u32)); |
| 2673 | |
| 2674 | of_property_for_each_string(node, "clock-names", prop, cname) { |
| 2675 | struct clk *c = devm_clk_get(&pdev->dev, cname); |
| 2676 | |
| 2677 | if (IS_ERR(c)) { |
| 2678 | KGSL_CORE_ERR("dt: Couldn't get clock: %s\n", cname); |
| 2679 | return -ENODEV; |
| 2680 | } |
| 2681 | if (i >= KGSL_IOMMU_MAX_CLKS) { |
| 2682 | KGSL_CORE_ERR("dt: too many clocks defined.\n"); |
| 2683 | return -EINVAL; |
| 2684 | } |
| 2685 | |
| 2686 | iommu->clks[i] = c; |
| 2687 | ++i; |
| 2688 | } |
| 2689 | |
| 2690 | for (i = 0; i < ARRAY_SIZE(kgsl_iommu_features); i++) { |
| 2691 | if (of_property_read_bool(node, kgsl_iommu_features[i].feature)) |
| 2692 | device->mmu.features |= kgsl_iommu_features[i].bit; |
| 2693 | } |
| 2694 | |
| 2695 | if (of_property_read_u32(node, "qcom,micro-mmu-control", |
| 2696 | &iommu->micro_mmu_ctrl)) |
| 2697 | iommu->micro_mmu_ctrl = UINT_MAX; |
| 2698 | |
| 2699 | if (of_property_read_u32(node, "qcom,secure_align_mask", |
| 2700 | &device->mmu.secure_align_mask)) |
| 2701 | device->mmu.secure_align_mask = 0xfff; |
| 2702 | |
| 2703 | /* Fill out the rest of the devices in the node */ |
| 2704 | of_platform_populate(node, NULL, NULL, &pdev->dev); |
| 2705 | |
| 2706 | for_each_child_of_node(node, child) { |
| 2707 | int ret; |
| 2708 | |
| 2709 | if (!of_device_is_compatible(child, "qcom,smmu-kgsl-cb")) |
| 2710 | continue; |
| 2711 | |
| 2712 | ret = _kgsl_iommu_cb_probe(device, iommu, child); |
| 2713 | if (ret) |
| 2714 | return ret; |
| 2715 | } |
| 2716 | |
| 2717 | return 0; |
| 2718 | } |
| 2719 | |
| 2720 | static const struct { |
| 2721 | char *compat; |
| 2722 | int (*probe)(struct kgsl_device *device, struct device_node *node); |
| 2723 | } kgsl_dt_devices[] = { |
| 2724 | { "qcom,kgsl-smmu-v1", _kgsl_iommu_probe }, |
| 2725 | { "qcom,kgsl-smmu-v2", _kgsl_iommu_probe }, |
| 2726 | }; |
| 2727 | |
| 2728 | static int kgsl_iommu_probe(struct kgsl_device *device) |
| 2729 | { |
| 2730 | int i; |
| 2731 | |
| 2732 | for (i = 0; i < ARRAY_SIZE(kgsl_dt_devices); i++) { |
| 2733 | struct device_node *node; |
| 2734 | |
| 2735 | node = of_find_compatible_node(device->pdev->dev.of_node, |
| 2736 | NULL, kgsl_dt_devices[i].compat); |
| 2737 | |
| 2738 | if (node != NULL) |
| 2739 | return kgsl_dt_devices[i].probe(device, node); |
| 2740 | } |
| 2741 | |
| 2742 | return -ENODEV; |
| 2743 | } |
| 2744 | |
| 2745 | struct kgsl_mmu_ops kgsl_iommu_ops = { |
| 2746 | .mmu_init = kgsl_iommu_init, |
| 2747 | .mmu_close = kgsl_iommu_close, |
| 2748 | .mmu_start = kgsl_iommu_start, |
| 2749 | .mmu_stop = kgsl_iommu_stop, |
| 2750 | .mmu_set_pt = kgsl_iommu_set_pt, |
| 2751 | .mmu_clear_fsr = kgsl_iommu_clear_fsr, |
| 2752 | .mmu_get_current_ttbr0 = kgsl_iommu_get_current_ttbr0, |
| 2753 | .mmu_enable_clk = kgsl_iommu_enable_clk, |
| 2754 | .mmu_disable_clk = kgsl_iommu_disable_clk, |
| 2755 | .mmu_get_reg_ahbaddr = kgsl_iommu_get_reg_ahbaddr, |
| 2756 | .mmu_pt_equal = kgsl_iommu_pt_equal, |
| 2757 | .mmu_set_pf_policy = kgsl_iommu_set_pf_policy, |
| 2758 | .mmu_pagefault_resume = kgsl_iommu_pagefault_resume, |
| 2759 | .mmu_get_prot_regs = kgsl_iommu_get_prot_regs, |
| 2760 | .mmu_init_pt = kgsl_iommu_init_pt, |
| 2761 | .mmu_add_global = kgsl_iommu_add_global, |
| 2762 | .mmu_remove_global = kgsl_iommu_remove_global, |
| 2763 | .mmu_getpagetable = kgsl_iommu_getpagetable, |
| 2764 | .mmu_get_qdss_global_entry = kgsl_iommu_get_qdss_global_entry, |
Jonathan Wicks | 4892d8d | 2017-02-24 16:21:26 -0700 | [diff] [blame] | 2765 | .mmu_get_qtimer_global_entry = kgsl_iommu_get_qtimer_global_entry, |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 2766 | .probe = kgsl_iommu_probe, |
| 2767 | }; |
| 2768 | |
| 2769 | static struct kgsl_mmu_pt_ops iommu_pt_ops = { |
| 2770 | .mmu_map = kgsl_iommu_map, |
| 2771 | .mmu_unmap = kgsl_iommu_unmap, |
| 2772 | .mmu_destroy_pagetable = kgsl_iommu_destroy_pagetable, |
| 2773 | .get_ttbr0 = kgsl_iommu_get_ttbr0, |
| 2774 | .get_contextidr = kgsl_iommu_get_contextidr, |
| 2775 | .get_gpuaddr = kgsl_iommu_get_gpuaddr, |
| 2776 | .put_gpuaddr = kgsl_iommu_put_gpuaddr, |
| 2777 | .set_svm_region = kgsl_iommu_set_svm_region, |
| 2778 | .find_svm_region = kgsl_iommu_find_svm_region, |
| 2779 | .svm_range = kgsl_iommu_svm_range, |
| 2780 | .addr_in_range = kgsl_iommu_addr_in_range, |
| 2781 | .mmu_map_offset = kgsl_iommu_map_offset, |
| 2782 | .mmu_unmap_offset = kgsl_iommu_unmap_offset, |
| 2783 | .mmu_sparse_dummy_map = kgsl_iommu_sparse_dummy_map, |
| 2784 | }; |