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