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