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