blob: 8fc2c82bdedc126bbc0cfd5865c02efa3e0d6df5 [file] [log] [blame]
Shrenuj Bansala419c792016-10-20 14:05:11 -07001/* 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 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
38
Shrenuj Bansala419c792016-10-20 14:05:11 -070039#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 Susheelendra7f66cf72016-09-12 11:04:43 -060045/*
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 Bansala419c792016-10-20 14:05:11 -070053static struct kgsl_mmu_pt_ops iommu_pt_ops;
54static bool need_iommu_sync;
55
56const 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 */
77struct kgsl_iommu_addr_entry {
78 uint64_t base;
79 uint64_t size;
80 struct rb_node node;
81};
82
83static 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
107struct global_pt_entry {
108 struct kgsl_memdesc *memdesc;
109 char name[32];
110};
111
112static struct global_pt_entry global_pt_entries[GLOBAL_PT_ENTRIES];
Harshdeep Dhatt1f408332017-03-27 11:35:13 -0600113static int secure_global_size;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700114static int global_pt_count;
115uint64_t global_pt_alloc;
116static struct kgsl_memdesc gpu_qdss_desc;
Jonathan Wicks4892d8d2017-02-24 16:21:26 -0700117static struct kgsl_memdesc gpu_qtimer_desc;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700118
119void 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 Gundu1fbd9062017-11-01 18:47:45 +0530129 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 Bansala419c792016-10-20 14:05:11 -0700134 }
135}
136
137static 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
148static 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 Dhatt1f408332017-03-27 11:35:13 -0600165void kgsl_iommu_unmap_global_secure_pt_entry(struct kgsl_device *device,
166 struct kgsl_memdesc *entry)
Shrenuj Bansala419c792016-10-20 14:05:11 -0700167{
Harshdeep Dhatt1f408332017-03-27 11:35:13 -0600168 if (!kgsl_mmu_is_secured(&device->mmu))
169 return;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700170
Harshdeep Dhatt1f408332017-03-27 11:35:13 -0600171 if (entry != NULL && entry->pagetable->name == KGSL_MMU_SECURE_PT)
172 kgsl_mmu_unmap(entry->pagetable, entry);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700173
174}
175
Harshdeep Dhatt1f408332017-03-27 11:35:13 -0600176int kgsl_iommu_map_global_secure_pt_entry(struct kgsl_device *device,
177 struct kgsl_memdesc *entry)
Shrenuj Bansala419c792016-10-20 14:05:11 -0700178{
179 int ret = 0;
Harshdeep Dhatt1f408332017-03-27 11:35:13 -0600180
181 if (!kgsl_mmu_is_secured(&device->mmu))
182 return -ENOTSUPP;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700183
184 if (entry != NULL) {
Harshdeep Dhatt1f408332017-03-27 11:35:13 -0600185 struct kgsl_pagetable *pagetable = device->mmu.securepagetable;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700186 entry->pagetable = pagetable;
Harshdeep Dhatt1f408332017-03-27 11:35:13 -0600187 entry->gpuaddr = KGSL_IOMMU_SECURE_BASE + secure_global_size;
188
Shrenuj Bansala419c792016-10-20 14:05:11 -0700189 ret = kgsl_mmu_map(pagetable, entry);
Harshdeep Dhatt1f408332017-03-27 11:35:13 -0600190 if (ret == 0)
191 secure_global_size += entry->size;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700192 }
193 return ret;
194}
195
196static 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
214static 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 Bansala419c792016-10-20 14:05:11 -0700236struct kgsl_memdesc *kgsl_iommu_get_qdss_global_entry(void)
237{
238 return &gpu_qdss_desc;
239}
240
241static 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
275static 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 Wicks4892d8d2017-02-24 16:21:26 -0700281struct kgsl_memdesc *kgsl_iommu_get_qtimer_global_entry(void)
282{
283 return &gpu_qtimer_desc;
284}
285
286static 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
320static 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 Bansala419c792016-10-20 14:05:11 -0700325
326static 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
337static 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
348static 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
366static 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
386static 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
398static 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
427static 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
455static 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
526static 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
561static struct page *kgsl_guard_page;
562static 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 */
569static 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
582struct _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
592static 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
640void __kgsl_get_memory_usage(struct _mem_entry *entry)
641{
642 kgsl_get_memory_usage(entry->name, sizeof(entry->name), entry->flags);
643}
644
645static 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
693static 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
715static 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
726static 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
747static bool
748kgsl_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
774static 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
789static 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;
799 pid_t tid = 0;
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 Vaz1fde74d2017-03-20 18:02:47 +0530805 struct adreno_gpudev *gpudev;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700806 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 Vaz1fde74d2017-03-20 18:02:47 +0530822 gpudev = ADRENO_GPU_DEVICE(adreno_dev);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700823
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);
854 tid = context->tid;
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) ?
875 KGSL_MMU_GLOBAL_PT : tid;
Sunil Khatri86e95682017-01-23 17:10:32 +0530876 /*
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 Bansala419c792016-10-20 14:05:11 -0700883
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 Vaz1fde74d2017-03-20 18:02:47 +0530896 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 Bansala419c792016-10-20 14:05:11 -0700906 /* 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 Bansala419c792016-10-20 14:05:11 -0700929
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 */
963static 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 */
985static 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 */
1003static 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 */
1016static 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
1025static 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 */
1041static 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 */
1056static 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 Bansala419c792016-10-20 14:05:11 -07001073 } 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
1089static void setup_64bit_pagetable(struct kgsl_mmu *mmu,
1090 struct kgsl_pagetable *pagetable,
1091 struct kgsl_iommu_pt *pt)
1092{
Shrenuj Bansala419c792016-10-20 14:05:11 -07001093 if (mmu->secured && pagetable->name == KGSL_MMU_SECURE_PT) {
Harshdeep Dhatt1f408332017-03-27 11:35:13 -06001094 pt->compat_va_start = KGSL_IOMMU_SECURE_BASE;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001095 pt->compat_va_end = KGSL_IOMMU_SECURE_END;
Harshdeep Dhatt1f408332017-03-27 11:35:13 -06001096 pt->va_start = KGSL_IOMMU_SECURE_BASE;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001097 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
1117static void setup_32bit_pagetable(struct kgsl_mmu *mmu,
1118 struct kgsl_pagetable *pagetable,
1119 struct kgsl_iommu_pt *pt)
1120{
Shrenuj Bansala419c792016-10-20 14:05:11 -07001121 if (mmu->secured) {
1122 if (pagetable->name == KGSL_MMU_SECURE_PT) {
Harshdeep Dhatt1f408332017-03-27 11:35:13 -06001123 pt->compat_va_start = KGSL_IOMMU_SECURE_BASE;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001124 pt->compat_va_end = KGSL_IOMMU_SECURE_END;
Harshdeep Dhatt1f408332017-03-27 11:35:13 -06001125 pt->va_start = KGSL_IOMMU_SECURE_BASE;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001126 pt->va_end = KGSL_IOMMU_SECURE_END;
1127 } else {
1128 pt->va_start = KGSL_IOMMU_SVM_BASE32;
Harshdeep Dhatt517a0672017-03-23 10:23:55 -06001129 pt->va_end = KGSL_IOMMU_SECURE_BASE;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001130 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
1148static 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
1181static 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 Susheelendra906564d2017-01-10 15:53:55 -07001198void _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 Bansal9a0563b2017-06-15 14:45:15 -07001220static 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 Bansala419c792016-10-20 14:05:11 -07001233static 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 Susheelendra906564d2017-01-10 15:53:55 -07001256 _enable_gpuhtw_llc(mmu, iommu_pt);
1257
Shrenuj Bansala419c792016-10-20 14:05:11 -07001258 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 Bansalc3b15ce2017-06-15 14:48:05 -07001268 KGSL_CORE_ERR("get DOMAIN_ATTR_CONTEXT_BANK failed: %d\n",
Shrenuj Bansala419c792016-10-20 14:05:11 -07001269 ret);
1270 goto done;
1271 }
1272
Shrenuj Bansal9a0563b2017-06-15 14:45:15 -07001273 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 Bansala419c792016-10-20 14:05:11 -07001282 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
1303done:
1304 if (ret)
1305 _free_pt(ctx, pt);
1306
1307 return ret;
1308}
1309
1310static 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 Susheelendra906564d2017-01-10 15:53:55 -07001339 _enable_gpuhtw_llc(mmu, iommu_pt);
1340
Shrenuj Bansala419c792016-10-20 14:05:11 -07001341 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 Bansala419c792016-10-20 14:05:11 -07001359done:
1360 if (ret)
1361 _free_pt(ctx, pt);
1362 return ret;
1363}
1364
1365static 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 Susheelendra906564d2017-01-10 15:53:55 -07001399 _enable_gpuhtw_llc(mmu, iommu_pt);
1400
Shrenuj Bansala419c792016-10-20 14:05:11 -07001401 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
1422done:
1423 if (ret)
1424 _free_pt(ctx, pt);
1425
1426 return ret;
1427}
1428
1429/* kgsl_iommu_init_pt - Set up an IOMMU pagetable */
1430static 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
1447static 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 */
1473static 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
1482static 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
1496static 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 Wicks4892d8d2017-02-24 16:21:26 -07001528 kgsl_cleanup_qtimer_desc(mmu);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001529}
1530
1531static 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
1548static 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 Wicks4892d8d2017-02-24 16:21:26 -07001600 kgsl_setup_qtimer_desc(device);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001601
Harshdeep Dhatt1f408332017-03-27 11:35:13 -06001602 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 Bansala419c792016-10-20 14:05:11 -07001614done:
1615 if (status)
1616 kgsl_iommu_close(mmu);
1617
1618 return status;
1619}
1620
1621static 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 Vaza2e31112017-04-17 18:29:58 +05301639 } else if (mmu->defaultpagetable == NULL) {
1640 return -ENOMEM;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001641 }
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
1683static 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 Dhatt1f408332017-03-27 11:35:13 -06001695 if (mmu->securepagetable == NULL)
1696 return -ENOMEM;
1697
Shrenuj Bansala419c792016-10-20 14:05:11 -07001698 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;
1713done:
1714 if (ret)
1715 _detach_context(ctx);
1716 return ret;
1717}
1718
1719static int kgsl_iommu_set_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt);
1720
1721static 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
1740static int
1741kgsl_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
1760static int
1761kgsl_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 */
1779static 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
1827static unsigned int _get_protection_flags(struct kgsl_memdesc *memdesc)
1828{
Sushmita Susheelendra7f66cf72016-09-12 11:04:43 -06001829 unsigned int flags = IOMMU_READ | IOMMU_WRITE |
1830 IOMMU_NOEXEC | IOMMU_USE_UPSTREAM_HINT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001831
1832 if (memdesc->flags & KGSL_MEMFLAGS_GPUREADONLY)
1833 flags &= ~IOMMU_WRITE;
1834
1835 if (memdesc->priv & KGSL_MEMDESC_PRIVILEGED)
1836 flags |= IOMMU_PRIV;
1837
1838 return flags;
1839}
1840
1841static int
1842kgsl_iommu_map(struct kgsl_pagetable *pt,
1843 struct kgsl_memdesc *memdesc)
1844{
1845 int ret;
1846 uint64_t addr = memdesc->gpuaddr;
1847 uint64_t size = memdesc->size;
1848 unsigned int flags = _get_protection_flags(memdesc);
1849 struct sg_table *sgt = NULL;
1850
1851 /*
1852 * For paged memory allocated through kgsl, memdesc->pages is not NULL.
1853 * Allocate sgt here just for its map operation. Contiguous memory
1854 * already has its sgt, so no need to allocate it here.
1855 */
1856 if (memdesc->pages != NULL)
1857 sgt = kgsl_alloc_sgt_from_pages(memdesc);
1858 else
1859 sgt = memdesc->sgt;
1860
1861 if (IS_ERR(sgt))
1862 return PTR_ERR(sgt);
1863
1864 ret = _iommu_map_sg_sync_pc(pt, addr, memdesc, sgt->sgl,
1865 sgt->nents, flags);
1866 if (ret)
1867 goto done;
1868
1869 ret = _iommu_map_guard_page(pt, memdesc, addr + size, flags);
1870 if (ret)
1871 _iommu_unmap_sync_pc(pt, memdesc, addr, size);
1872
1873done:
1874 if (memdesc->pages != NULL)
1875 kgsl_free_sgt(sgt);
1876
1877 return ret;
1878}
1879
1880static int kgsl_iommu_sparse_dummy_map(struct kgsl_pagetable *pt,
1881 struct kgsl_memdesc *memdesc, uint64_t offset, uint64_t size)
1882{
1883 int ret = 0, i;
1884 struct page **pages = NULL;
1885 struct sg_table sgt;
1886 int count = size >> PAGE_SHIFT;
1887
1888 /* verify the offset is within our range */
1889 if (size + offset > memdesc->size)
1890 return -EINVAL;
1891
1892 if (kgsl_dummy_page == NULL) {
1893 kgsl_dummy_page = alloc_page(GFP_KERNEL | __GFP_ZERO |
1894 __GFP_HIGHMEM);
1895 if (kgsl_dummy_page == NULL)
1896 return -ENOMEM;
1897 }
1898
1899 pages = kcalloc(count, sizeof(struct page *), GFP_KERNEL);
1900 if (pages == NULL)
1901 return -ENOMEM;
1902
1903 for (i = 0; i < count; i++)
1904 pages[i] = kgsl_dummy_page;
1905
1906 ret = sg_alloc_table_from_pages(&sgt, pages, count,
1907 0, size, GFP_KERNEL);
1908 if (ret == 0) {
1909 ret = _iommu_map_sg_sync_pc(pt, memdesc->gpuaddr + offset,
1910 memdesc, sgt.sgl, sgt.nents,
1911 IOMMU_READ | IOMMU_NOEXEC);
1912 sg_free_table(&sgt);
1913 }
1914
1915 kfree(pages);
1916
1917 return ret;
1918}
1919
1920static int _map_to_one_page(struct kgsl_pagetable *pt, uint64_t addr,
1921 struct kgsl_memdesc *memdesc, uint64_t physoffset,
1922 uint64_t size, unsigned int map_flags)
1923{
1924 int ret = 0, i;
1925 int pg_sz = kgsl_memdesc_get_pagesize(memdesc);
1926 int count = size >> PAGE_SHIFT;
1927 struct page *page = NULL;
1928 struct page **pages = NULL;
1929 struct sg_page_iter sg_iter;
1930 struct sg_table sgt;
1931
1932 /* Find our physaddr offset addr */
1933 if (memdesc->pages != NULL)
1934 page = memdesc->pages[physoffset >> PAGE_SHIFT];
1935 else {
1936 for_each_sg_page(memdesc->sgt->sgl, &sg_iter,
1937 memdesc->sgt->nents, physoffset >> PAGE_SHIFT) {
1938 page = sg_page_iter_page(&sg_iter);
1939 break;
1940 }
1941 }
1942
1943 if (page == NULL)
1944 return -EINVAL;
1945
1946 pages = kcalloc(count, sizeof(struct page *), GFP_KERNEL);
1947 if (pages == NULL)
1948 return -ENOMEM;
1949
1950 for (i = 0; i < count; i++) {
1951 if (pg_sz != PAGE_SIZE) {
1952 struct page *tmp_page = page;
1953 int j;
1954
1955 for (j = 0; j < 16; j++, tmp_page += PAGE_SIZE)
1956 pages[i++] = tmp_page;
1957 } else
1958 pages[i] = page;
1959 }
1960
1961 ret = sg_alloc_table_from_pages(&sgt, pages, count,
1962 0, size, GFP_KERNEL);
1963 if (ret == 0) {
1964 ret = _iommu_map_sg_sync_pc(pt, addr, memdesc, sgt.sgl,
1965 sgt.nents, map_flags);
1966 sg_free_table(&sgt);
1967 }
1968
1969 kfree(pages);
1970
1971 return ret;
1972}
1973
1974static int kgsl_iommu_map_offset(struct kgsl_pagetable *pt,
1975 uint64_t virtaddr, uint64_t virtoffset,
1976 struct kgsl_memdesc *memdesc, uint64_t physoffset,
1977 uint64_t size, uint64_t feature_flag)
1978{
1979 int pg_sz;
1980 unsigned int protflags = _get_protection_flags(memdesc);
1981 int ret;
1982 struct sg_table *sgt = NULL;
1983
1984 pg_sz = kgsl_memdesc_get_pagesize(memdesc);
1985 if (!IS_ALIGNED(virtaddr | virtoffset | physoffset | size, pg_sz))
1986 return -EINVAL;
1987
1988 if (size == 0)
1989 return -EINVAL;
1990
1991 if (!(feature_flag & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS) &&
1992 size + physoffset > kgsl_memdesc_footprint(memdesc))
1993 return -EINVAL;
1994
1995 /*
1996 * For paged memory allocated through kgsl, memdesc->pages is not NULL.
1997 * Allocate sgt here just for its map operation. Contiguous memory
1998 * already has its sgt, so no need to allocate it here.
1999 */
2000 if (memdesc->pages != NULL)
2001 sgt = kgsl_alloc_sgt_from_pages(memdesc);
2002 else
2003 sgt = memdesc->sgt;
2004
2005 if (IS_ERR(sgt))
2006 return PTR_ERR(sgt);
2007
2008 if (feature_flag & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS)
2009 ret = _map_to_one_page(pt, virtaddr + virtoffset,
2010 memdesc, physoffset, size, protflags);
2011 else
2012 ret = _iommu_map_sg_offset_sync_pc(pt, virtaddr + virtoffset,
2013 memdesc, sgt->sgl, sgt->nents,
2014 physoffset, size, protflags);
2015
2016 if (memdesc->pages != NULL)
2017 kgsl_free_sgt(sgt);
2018
2019 return ret;
2020}
2021
2022/* This function must be called with context bank attached */
2023static void kgsl_iommu_clear_fsr(struct kgsl_mmu *mmu)
2024{
2025 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2026 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
2027 unsigned int sctlr_val;
2028
2029 if (ctx->default_pt != NULL) {
2030 kgsl_iommu_enable_clk(mmu);
2031 KGSL_IOMMU_SET_CTX_REG(ctx, FSR, 0xffffffff);
2032 /*
2033 * Re-enable context fault interrupts after clearing
2034 * FSR to prevent the interrupt from firing repeatedly
2035 */
2036 sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR);
2037 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFIE_SHIFT);
2038 KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val);
2039 /*
2040 * Make sure the above register writes
2041 * are not reordered across the barrier
2042 * as we use writel_relaxed to write them
2043 */
2044 wmb();
2045 kgsl_iommu_disable_clk(mmu);
2046 }
2047}
2048
2049static void kgsl_iommu_pagefault_resume(struct kgsl_mmu *mmu)
2050{
2051 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2052 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
2053
2054 if (ctx->default_pt != NULL && ctx->fault) {
2055 /*
2056 * Write 1 to RESUME.TnR to terminate the
2057 * stalled transaction.
2058 */
2059 KGSL_IOMMU_SET_CTX_REG(ctx, RESUME, 1);
2060 /*
2061 * Make sure the above register writes
2062 * are not reordered across the barrier
2063 * as we use writel_relaxed to write them
2064 */
2065 wmb();
2066 ctx->fault = 0;
2067 }
2068}
2069
2070static void kgsl_iommu_stop(struct kgsl_mmu *mmu)
2071{
2072 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2073 int i;
2074
2075 /*
2076 * If the iommu supports retention, we don't need
2077 * to detach when stopping.
2078 */
2079 if (!MMU_FEATURE(mmu, KGSL_MMU_RETENTION)) {
2080 for (i = 0; i < KGSL_IOMMU_CONTEXT_MAX; i++)
2081 _detach_context(&iommu->ctx[i]);
2082 }
2083}
2084
2085static u64
2086kgsl_iommu_get_current_ttbr0(struct kgsl_mmu *mmu)
2087{
2088 u64 val;
2089 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2090 /*
2091 * We cannot enable or disable the clocks in interrupt context, this
2092 * function is called from interrupt context if there is an axi error
2093 */
2094 if (in_interrupt())
2095 return 0;
2096
2097 kgsl_iommu_enable_clk(mmu);
2098 val = KGSL_IOMMU_GET_CTX_REG_Q(&iommu->ctx[KGSL_IOMMU_CONTEXT_USER],
2099 TTBR0);
2100 kgsl_iommu_disable_clk(mmu);
2101 return val;
2102}
2103
2104/*
2105 * kgsl_iommu_set_pt - Change the IOMMU pagetable of the primary context bank
2106 * @mmu - Pointer to mmu structure
2107 * @pt - Pagetable to switch to
2108 *
2109 * Set the new pagetable for the IOMMU by doing direct register writes
2110 * to the IOMMU registers through the cpu
2111 *
2112 * Return - void
2113 */
2114static int kgsl_iommu_set_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt)
2115{
2116 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2117 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
2118 uint64_t ttbr0, temp;
2119 unsigned int contextidr;
2120 unsigned long wait_for_flush;
2121
2122 if ((pt != mmu->defaultpagetable) && !kgsl_mmu_is_perprocess(mmu))
2123 return 0;
2124
2125 kgsl_iommu_enable_clk(mmu);
2126
2127 ttbr0 = kgsl_mmu_pagetable_get_ttbr0(pt);
2128 contextidr = kgsl_mmu_pagetable_get_contextidr(pt);
2129
2130 KGSL_IOMMU_SET_CTX_REG_Q(ctx, TTBR0, ttbr0);
2131 KGSL_IOMMU_SET_CTX_REG(ctx, CONTEXTIDR, contextidr);
2132
2133 /* memory barrier before reading TTBR0 register */
2134 mb();
2135 temp = KGSL_IOMMU_GET_CTX_REG_Q(ctx, TTBR0);
2136
2137 KGSL_IOMMU_SET_CTX_REG(ctx, TLBIALL, 1);
2138 /* make sure the TBLI write completes before we wait */
2139 mb();
2140 /*
2141 * Wait for flush to complete by polling the flush
2142 * status bit of TLBSTATUS register for not more than
2143 * 2 s. After 2s just exit, at that point the SMMU h/w
2144 * may be stuck and will eventually cause GPU to hang
2145 * or bring the system down.
2146 */
2147 wait_for_flush = jiffies + msecs_to_jiffies(2000);
2148 KGSL_IOMMU_SET_CTX_REG(ctx, TLBSYNC, 0);
2149 while (KGSL_IOMMU_GET_CTX_REG(ctx, TLBSTATUS) &
2150 (KGSL_IOMMU_CTX_TLBSTATUS_SACTIVE)) {
2151 if (time_after(jiffies, wait_for_flush)) {
2152 KGSL_DRV_WARN(KGSL_MMU_DEVICE(mmu),
2153 "Wait limit reached for IOMMU tlb flush\n");
2154 break;
2155 }
2156 cpu_relax();
2157 }
2158
2159 kgsl_iommu_disable_clk(mmu);
2160 return 0;
2161}
2162
2163/*
2164 * kgsl_iommu_set_pf_policy() - Set the pagefault policy for IOMMU
2165 * @mmu: Pointer to mmu structure
2166 * @pf_policy: The pagefault polict to set
2167 *
2168 * Check if the new policy indicated by pf_policy is same as current
2169 * policy, if same then return else set the policy
2170 */
2171static int kgsl_iommu_set_pf_policy(struct kgsl_mmu *mmu,
2172 unsigned long pf_policy)
2173{
2174 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2175 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
2176 struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
2177 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2178
2179 if ((adreno_dev->ft_pf_policy &
2180 BIT(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE)) ==
2181 (pf_policy & BIT(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE)))
2182 return 0;
2183
2184 /* If not attached, policy will be updated during the next attach */
2185 if (ctx->default_pt != NULL) {
2186 unsigned int sctlr_val;
2187
2188 kgsl_iommu_enable_clk(mmu);
2189
2190 sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR);
2191
2192 if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, &pf_policy)) {
2193 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
2194 sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
2195 } else {
2196 sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
2197 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
2198 }
2199
2200 KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val);
2201
2202 kgsl_iommu_disable_clk(mmu);
2203 }
2204
2205 return 0;
2206}
2207
2208static struct kgsl_protected_registers *
2209kgsl_iommu_get_prot_regs(struct kgsl_mmu *mmu)
2210{
2211 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2212
2213 return &iommu->protect;
2214}
2215
2216static struct kgsl_iommu_addr_entry *_find_gpuaddr(
2217 struct kgsl_pagetable *pagetable, uint64_t gpuaddr)
2218{
2219 struct kgsl_iommu_pt *pt = pagetable->priv;
2220 struct rb_node *node = pt->rbtree.rb_node;
2221
2222 while (node != NULL) {
2223 struct kgsl_iommu_addr_entry *entry = rb_entry(node,
2224 struct kgsl_iommu_addr_entry, node);
2225
2226 if (gpuaddr < entry->base)
2227 node = node->rb_left;
2228 else if (gpuaddr > entry->base)
2229 node = node->rb_right;
2230 else
2231 return entry;
2232 }
2233
2234 return NULL;
2235}
2236
2237static int _remove_gpuaddr(struct kgsl_pagetable *pagetable,
2238 uint64_t gpuaddr)
2239{
2240 struct kgsl_iommu_pt *pt = pagetable->priv;
2241 struct kgsl_iommu_addr_entry *entry;
2242
2243 entry = _find_gpuaddr(pagetable, gpuaddr);
2244
2245 if (entry != NULL) {
2246 rb_erase(&entry->node, &pt->rbtree);
2247 kmem_cache_free(addr_entry_cache, entry);
2248 return 0;
2249 }
2250
2251 WARN(1, "Couldn't remove gpuaddr: 0x%llx\n", gpuaddr);
2252 return -ENOMEM;
2253}
2254
2255static int _insert_gpuaddr(struct kgsl_pagetable *pagetable,
2256 uint64_t gpuaddr, uint64_t size)
2257{
2258 struct kgsl_iommu_pt *pt = pagetable->priv;
2259 struct rb_node **node, *parent = NULL;
2260 struct kgsl_iommu_addr_entry *new =
2261 kmem_cache_alloc(addr_entry_cache, GFP_ATOMIC);
2262
2263 if (new == NULL)
2264 return -ENOMEM;
2265
2266 new->base = gpuaddr;
2267 new->size = size;
2268
2269 node = &pt->rbtree.rb_node;
2270
2271 while (*node != NULL) {
2272 struct kgsl_iommu_addr_entry *this;
2273
2274 parent = *node;
2275 this = rb_entry(parent, struct kgsl_iommu_addr_entry, node);
2276
2277 if (new->base < this->base)
2278 node = &parent->rb_left;
2279 else if (new->base > this->base)
2280 node = &parent->rb_right;
2281 else {
2282 /* Duplicate entry */
2283 WARN(1, "duplicate gpuaddr: 0x%llx\n", gpuaddr);
2284 return -EEXIST;
2285 }
2286 }
2287
2288 rb_link_node(&new->node, parent, node);
2289 rb_insert_color(&new->node, &pt->rbtree);
2290
2291 return 0;
2292}
2293
2294static uint64_t _get_unmapped_area(struct kgsl_pagetable *pagetable,
2295 uint64_t bottom, uint64_t top, uint64_t size,
2296 uint64_t align)
2297{
2298 struct kgsl_iommu_pt *pt = pagetable->priv;
2299 struct rb_node *node = rb_first(&pt->rbtree);
2300 uint64_t start;
2301
2302 bottom = ALIGN(bottom, align);
2303 start = bottom;
2304
2305 while (node != NULL) {
2306 uint64_t gap;
2307 struct kgsl_iommu_addr_entry *entry = rb_entry(node,
2308 struct kgsl_iommu_addr_entry, node);
2309
2310 /*
2311 * Skip any entries that are outside of the range, but make sure
2312 * to account for some that might straddle the lower bound
2313 */
2314 if (entry->base < bottom) {
2315 if (entry->base + entry->size > bottom)
2316 start = ALIGN(entry->base + entry->size, align);
2317 node = rb_next(node);
2318 continue;
2319 }
2320
2321 /* Stop if we went over the top */
2322 if (entry->base >= top)
2323 break;
2324
2325 /* Make sure there is a gap to consider */
2326 if (start < entry->base) {
2327 gap = entry->base - start;
2328
2329 if (gap >= size)
2330 return start;
2331 }
2332
2333 /* Stop if there is no more room in the region */
2334 if (entry->base + entry->size >= top)
2335 return (uint64_t) -ENOMEM;
2336
2337 /* Start the next cycle at the end of the current entry */
2338 start = ALIGN(entry->base + entry->size, align);
2339 node = rb_next(node);
2340 }
2341
2342 if (start + size <= top)
2343 return start;
2344
2345 return (uint64_t) -ENOMEM;
2346}
2347
2348static uint64_t _get_unmapped_area_topdown(struct kgsl_pagetable *pagetable,
2349 uint64_t bottom, uint64_t top, uint64_t size,
2350 uint64_t align)
2351{
2352 struct kgsl_iommu_pt *pt = pagetable->priv;
2353 struct rb_node *node = rb_last(&pt->rbtree);
2354 uint64_t end = top;
2355 uint64_t mask = ~(align - 1);
2356 struct kgsl_iommu_addr_entry *entry;
2357
2358 /* Make sure that the bottom is correctly aligned */
2359 bottom = ALIGN(bottom, align);
2360
2361 /* Make sure the requested size will fit in the range */
2362 if (size > (top - bottom))
2363 return -ENOMEM;
2364
2365 /* Walk back through the list to find the highest entry in the range */
2366 for (node = rb_last(&pt->rbtree); node != NULL; node = rb_prev(node)) {
2367 entry = rb_entry(node, struct kgsl_iommu_addr_entry, node);
2368 if (entry->base < top)
2369 break;
2370 }
2371
2372 while (node != NULL) {
2373 uint64_t offset;
2374
2375 entry = rb_entry(node, struct kgsl_iommu_addr_entry, node);
2376
2377 /* If the entire entry is below the range the search is over */
2378 if ((entry->base + entry->size) < bottom)
2379 break;
2380
2381 /* Get the top of the entry properly aligned */
2382 offset = ALIGN(entry->base + entry->size, align);
2383
2384 /*
2385 * Try to allocate the memory from the top of the gap,
2386 * making sure that it fits between the top of this entry and
2387 * the bottom of the previous one
2388 */
2389
2390 if ((end > size) && (offset < end)) {
2391 uint64_t chunk = (end - size) & mask;
2392
2393 if (chunk >= offset)
2394 return chunk;
2395 }
2396
2397 /*
2398 * If we get here and the current entry is outside of the range
2399 * then we are officially out of room
2400 */
2401
2402 if (entry->base < bottom)
2403 return (uint64_t) -ENOMEM;
2404
2405 /* Set the top of the gap to the current entry->base */
2406 end = entry->base;
2407
2408 /* And move on to the next lower entry */
2409 node = rb_prev(node);
2410 }
2411
2412 /* If we get here then there are no more entries in the region */
2413 if ((end > size) && (((end - size) & mask) >= bottom))
2414 return (end - size) & mask;
2415
2416 return (uint64_t) -ENOMEM;
2417}
2418
2419static uint64_t kgsl_iommu_find_svm_region(struct kgsl_pagetable *pagetable,
2420 uint64_t start, uint64_t end, uint64_t size,
2421 uint64_t alignment)
2422{
2423 uint64_t addr;
2424
2425 /* Avoid black holes */
2426 if (WARN(end <= start, "Bad search range: 0x%llx-0x%llx", start, end))
2427 return (uint64_t) -EINVAL;
2428
2429 spin_lock(&pagetable->lock);
2430 addr = _get_unmapped_area_topdown(pagetable,
2431 start, end, size, alignment);
2432 spin_unlock(&pagetable->lock);
2433 return addr;
2434}
2435
2436static int kgsl_iommu_set_svm_region(struct kgsl_pagetable *pagetable,
2437 uint64_t gpuaddr, uint64_t size)
2438{
2439 int ret = -ENOMEM;
2440 struct kgsl_iommu_pt *pt = pagetable->priv;
2441 struct rb_node *node;
2442
2443 /* Make sure the requested address doesn't fall in the global range */
2444 if (ADDR_IN_GLOBAL(gpuaddr) || ADDR_IN_GLOBAL(gpuaddr + size))
2445 return -ENOMEM;
2446
2447 spin_lock(&pagetable->lock);
2448 node = pt->rbtree.rb_node;
2449
2450 while (node != NULL) {
2451 uint64_t start, end;
2452 struct kgsl_iommu_addr_entry *entry = rb_entry(node,
2453 struct kgsl_iommu_addr_entry, node);
2454
2455 start = entry->base;
2456 end = entry->base + entry->size;
2457
2458 if (gpuaddr + size <= start)
2459 node = node->rb_left;
2460 else if (end <= gpuaddr)
2461 node = node->rb_right;
2462 else
2463 goto out;
2464 }
2465
2466 ret = _insert_gpuaddr(pagetable, gpuaddr, size);
2467out:
2468 spin_unlock(&pagetable->lock);
2469 return ret;
2470}
2471
2472
2473static int kgsl_iommu_get_gpuaddr(struct kgsl_pagetable *pagetable,
2474 struct kgsl_memdesc *memdesc)
2475{
2476 struct kgsl_iommu_pt *pt = pagetable->priv;
2477 int ret = 0;
2478 uint64_t addr, start, end, size;
2479 unsigned int align;
2480
2481 if (WARN_ON(kgsl_memdesc_use_cpu_map(memdesc)))
2482 return -EINVAL;
2483
2484 if (memdesc->flags & KGSL_MEMFLAGS_SECURE &&
2485 pagetable->name != KGSL_MMU_SECURE_PT)
2486 return -EINVAL;
2487
2488 size = kgsl_memdesc_footprint(memdesc);
2489
2490 align = 1 << kgsl_memdesc_get_align(memdesc);
2491
2492 if (memdesc->flags & KGSL_MEMFLAGS_FORCE_32BIT) {
2493 start = pt->compat_va_start;
2494 end = pt->compat_va_end;
2495 } else {
2496 start = pt->va_start;
2497 end = pt->va_end;
2498 }
2499
Harshdeep Dhatt1f408332017-03-27 11:35:13 -06002500 /*
2501 * When mapping secure buffers, adjust the start of the va range
2502 * to the end of secure global buffers.
2503 */
2504 if (kgsl_memdesc_is_secured(memdesc))
2505 start += secure_global_size;
2506
Shrenuj Bansala419c792016-10-20 14:05:11 -07002507 spin_lock(&pagetable->lock);
2508
2509 addr = _get_unmapped_area(pagetable, start, end, size, align);
2510
2511 if (addr == (uint64_t) -ENOMEM) {
2512 ret = -ENOMEM;
2513 goto out;
2514 }
2515
2516 ret = _insert_gpuaddr(pagetable, addr, size);
2517 if (ret == 0) {
2518 memdesc->gpuaddr = addr;
2519 memdesc->pagetable = pagetable;
2520 }
2521
2522out:
2523 spin_unlock(&pagetable->lock);
2524 return ret;
2525}
2526
2527static void kgsl_iommu_put_gpuaddr(struct kgsl_memdesc *memdesc)
2528{
2529 if (memdesc->pagetable == NULL)
2530 return;
2531
2532 spin_lock(&memdesc->pagetable->lock);
2533
2534 _remove_gpuaddr(memdesc->pagetable, memdesc->gpuaddr);
2535
2536 spin_unlock(&memdesc->pagetable->lock);
2537}
2538
2539static int kgsl_iommu_svm_range(struct kgsl_pagetable *pagetable,
2540 uint64_t *lo, uint64_t *hi, uint64_t memflags)
2541{
2542 struct kgsl_iommu_pt *pt = pagetable->priv;
2543 bool gpu_compat = (memflags & KGSL_MEMFLAGS_FORCE_32BIT) != 0;
2544
2545 if (lo != NULL)
2546 *lo = gpu_compat ? pt->compat_va_start : pt->svm_start;
2547 if (hi != NULL)
2548 *hi = gpu_compat ? pt->compat_va_end : pt->svm_end;
2549
2550 return 0;
2551}
2552
2553static bool kgsl_iommu_addr_in_range(struct kgsl_pagetable *pagetable,
2554 uint64_t gpuaddr)
2555{
2556 struct kgsl_iommu_pt *pt = pagetable->priv;
2557
2558 if (gpuaddr == 0)
2559 return false;
2560
2561 if (gpuaddr >= pt->va_start && gpuaddr < pt->va_end)
2562 return true;
2563
2564 if (gpuaddr >= pt->compat_va_start && gpuaddr < pt->compat_va_end)
2565 return true;
2566
2567 if (gpuaddr >= pt->svm_start && gpuaddr < pt->svm_end)
2568 return true;
2569
2570 return false;
2571}
2572
2573static const struct {
2574 int id;
2575 char *name;
2576} kgsl_iommu_cbs[] = {
2577 { KGSL_IOMMU_CONTEXT_USER, "gfx3d_user", },
2578 { KGSL_IOMMU_CONTEXT_SECURE, "gfx3d_secure" },
2579};
2580
2581static int _kgsl_iommu_cb_probe(struct kgsl_device *device,
2582 struct kgsl_iommu *iommu, struct device_node *node)
2583{
2584 struct platform_device *pdev = of_find_device_by_node(node);
2585 struct kgsl_iommu_context *ctx = NULL;
2586 int i;
2587
2588 for (i = 0; i < ARRAY_SIZE(kgsl_iommu_cbs); i++) {
2589 if (!strcmp(node->name, kgsl_iommu_cbs[i].name)) {
2590 int id = kgsl_iommu_cbs[i].id;
2591
2592 ctx = &iommu->ctx[id];
2593 ctx->id = id;
2594 ctx->cb_num = -1;
2595 ctx->name = kgsl_iommu_cbs[i].name;
2596
2597 break;
2598 }
2599 }
2600
2601 if (ctx == NULL) {
2602 KGSL_CORE_ERR("dt: Unknown context label %s\n", node->name);
2603 return -EINVAL;
2604 }
2605
2606 if (ctx->id == KGSL_IOMMU_CONTEXT_SECURE)
2607 device->mmu.secured = true;
2608
2609 /* this property won't be found for all context banks */
2610 if (of_property_read_u32(node, "qcom,gpu-offset", &ctx->gpu_offset))
2611 ctx->gpu_offset = UINT_MAX;
2612
2613 ctx->kgsldev = device;
2614
2615 /* arm-smmu driver we'll have the right device pointer here. */
2616 if (of_find_property(node, "iommus", NULL)) {
2617 ctx->dev = &pdev->dev;
2618 } else {
2619 ctx->dev = kgsl_mmu_get_ctx(ctx->name);
2620
2621 if (IS_ERR(ctx->dev))
2622 return PTR_ERR(ctx->dev);
2623 }
2624
2625 return 0;
2626}
2627
2628static const struct {
2629 char *feature;
Lynus Vazeb7af682017-04-17 18:36:01 +05302630 unsigned long bit;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002631} kgsl_iommu_features[] = {
2632 { "qcom,retention", KGSL_MMU_RETENTION },
2633 { "qcom,global_pt", KGSL_MMU_GLOBAL_PAGETABLE },
2634 { "qcom,hyp_secure_alloc", KGSL_MMU_HYP_SECURE_ALLOC },
2635 { "qcom,force-32bit", KGSL_MMU_FORCE_32BIT },
2636};
2637
2638static int _kgsl_iommu_probe(struct kgsl_device *device,
2639 struct device_node *node)
2640{
2641 const char *cname;
2642 struct property *prop;
2643 u32 reg_val[2];
2644 int i = 0;
2645 struct kgsl_iommu *iommu = KGSL_IOMMU_PRIV(device);
2646 struct device_node *child;
2647 struct platform_device *pdev = of_find_device_by_node(node);
2648
2649 memset(iommu, 0, sizeof(*iommu));
2650
2651 if (of_device_is_compatible(node, "qcom,kgsl-smmu-v1"))
2652 iommu->version = 1;
2653 else
2654 iommu->version = 2;
2655
2656 if (of_property_read_u32_array(node, "reg", reg_val, 2)) {
2657 KGSL_CORE_ERR("dt: Unable to read KGSL IOMMU register range\n");
2658 return -EINVAL;
2659 }
2660 iommu->regstart = reg_val[0];
2661 iommu->regsize = reg_val[1];
2662
2663 /* Protecting the SMMU registers is mandatory */
2664 if (of_property_read_u32_array(node, "qcom,protect", reg_val, 2)) {
2665 KGSL_CORE_ERR("dt: no iommu protection range specified\n");
2666 return -EINVAL;
2667 }
2668 iommu->protect.base = reg_val[0] / sizeof(u32);
2669 iommu->protect.range = ilog2(reg_val[1] / sizeof(u32));
2670
2671 of_property_for_each_string(node, "clock-names", prop, cname) {
2672 struct clk *c = devm_clk_get(&pdev->dev, cname);
2673
2674 if (IS_ERR(c)) {
2675 KGSL_CORE_ERR("dt: Couldn't get clock: %s\n", cname);
2676 return -ENODEV;
2677 }
2678 if (i >= KGSL_IOMMU_MAX_CLKS) {
2679 KGSL_CORE_ERR("dt: too many clocks defined.\n");
2680 return -EINVAL;
2681 }
2682
2683 iommu->clks[i] = c;
2684 ++i;
2685 }
2686
2687 for (i = 0; i < ARRAY_SIZE(kgsl_iommu_features); i++) {
2688 if (of_property_read_bool(node, kgsl_iommu_features[i].feature))
2689 device->mmu.features |= kgsl_iommu_features[i].bit;
2690 }
2691
2692 if (of_property_read_u32(node, "qcom,micro-mmu-control",
2693 &iommu->micro_mmu_ctrl))
2694 iommu->micro_mmu_ctrl = UINT_MAX;
2695
2696 if (of_property_read_u32(node, "qcom,secure_align_mask",
2697 &device->mmu.secure_align_mask))
2698 device->mmu.secure_align_mask = 0xfff;
2699
2700 /* Fill out the rest of the devices in the node */
2701 of_platform_populate(node, NULL, NULL, &pdev->dev);
2702
2703 for_each_child_of_node(node, child) {
2704 int ret;
2705
2706 if (!of_device_is_compatible(child, "qcom,smmu-kgsl-cb"))
2707 continue;
2708
2709 ret = _kgsl_iommu_cb_probe(device, iommu, child);
2710 if (ret)
2711 return ret;
2712 }
2713
2714 return 0;
2715}
2716
2717static const struct {
2718 char *compat;
2719 int (*probe)(struct kgsl_device *device, struct device_node *node);
2720} kgsl_dt_devices[] = {
2721 { "qcom,kgsl-smmu-v1", _kgsl_iommu_probe },
2722 { "qcom,kgsl-smmu-v2", _kgsl_iommu_probe },
2723};
2724
2725static int kgsl_iommu_probe(struct kgsl_device *device)
2726{
2727 int i;
2728
2729 for (i = 0; i < ARRAY_SIZE(kgsl_dt_devices); i++) {
2730 struct device_node *node;
2731
2732 node = of_find_compatible_node(device->pdev->dev.of_node,
2733 NULL, kgsl_dt_devices[i].compat);
2734
2735 if (node != NULL)
2736 return kgsl_dt_devices[i].probe(device, node);
2737 }
2738
2739 return -ENODEV;
2740}
2741
2742struct kgsl_mmu_ops kgsl_iommu_ops = {
2743 .mmu_init = kgsl_iommu_init,
2744 .mmu_close = kgsl_iommu_close,
2745 .mmu_start = kgsl_iommu_start,
2746 .mmu_stop = kgsl_iommu_stop,
2747 .mmu_set_pt = kgsl_iommu_set_pt,
2748 .mmu_clear_fsr = kgsl_iommu_clear_fsr,
2749 .mmu_get_current_ttbr0 = kgsl_iommu_get_current_ttbr0,
2750 .mmu_enable_clk = kgsl_iommu_enable_clk,
2751 .mmu_disable_clk = kgsl_iommu_disable_clk,
2752 .mmu_get_reg_ahbaddr = kgsl_iommu_get_reg_ahbaddr,
2753 .mmu_pt_equal = kgsl_iommu_pt_equal,
2754 .mmu_set_pf_policy = kgsl_iommu_set_pf_policy,
2755 .mmu_pagefault_resume = kgsl_iommu_pagefault_resume,
2756 .mmu_get_prot_regs = kgsl_iommu_get_prot_regs,
2757 .mmu_init_pt = kgsl_iommu_init_pt,
2758 .mmu_add_global = kgsl_iommu_add_global,
2759 .mmu_remove_global = kgsl_iommu_remove_global,
2760 .mmu_getpagetable = kgsl_iommu_getpagetable,
2761 .mmu_get_qdss_global_entry = kgsl_iommu_get_qdss_global_entry,
Jonathan Wicks4892d8d2017-02-24 16:21:26 -07002762 .mmu_get_qtimer_global_entry = kgsl_iommu_get_qtimer_global_entry,
Shrenuj Bansala419c792016-10-20 14:05:11 -07002763 .probe = kgsl_iommu_probe,
2764};
2765
2766static struct kgsl_mmu_pt_ops iommu_pt_ops = {
2767 .mmu_map = kgsl_iommu_map,
2768 .mmu_unmap = kgsl_iommu_unmap,
2769 .mmu_destroy_pagetable = kgsl_iommu_destroy_pagetable,
2770 .get_ttbr0 = kgsl_iommu_get_ttbr0,
2771 .get_contextidr = kgsl_iommu_get_contextidr,
2772 .get_gpuaddr = kgsl_iommu_get_gpuaddr,
2773 .put_gpuaddr = kgsl_iommu_put_gpuaddr,
2774 .set_svm_region = kgsl_iommu_set_svm_region,
2775 .find_svm_region = kgsl_iommu_find_svm_region,
2776 .svm_range = kgsl_iommu_svm_range,
2777 .addr_in_range = kgsl_iommu_addr_in_range,
2778 .mmu_map_offset = kgsl_iommu_map_offset,
2779 .mmu_unmap_offset = kgsl_iommu_unmap_offset,
2780 .mmu_sparse_dummy_map = kgsl_iommu_sparse_dummy_map,
2781};