blob: cfd5cd169c7e78be6ff5ff855dcd6c580478a5d8 [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
Sushmita Susheelendra906564d2017-01-10 15:53:55 -07001145void _enable_gpuhtw_llc(struct kgsl_mmu *mmu, struct kgsl_iommu_pt *iommu_pt)
1146{
1147 struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
1148 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1149 int gpuhtw_llc_enable = 1;
1150 int ret;
1151
1152 /* GPU pagetable walk LLC slice not enabled */
1153 if (!adreno_dev->gpuhtw_llc_slice)
1154 return;
1155
1156 /* Domain attribute to enable system cache for GPU pagetable walks */
1157 ret = iommu_domain_set_attr(iommu_pt->domain,
1158 DOMAIN_ATTR_USE_UPSTREAM_HINT, &gpuhtw_llc_enable);
1159 /*
1160 * Warn that the system cache will not be used for GPU
1161 * pagetable walks. This is not a fatal error.
1162 */
1163 WARN_ONCE(ret,
1164 "System cache not enabled for GPU pagetable walks: %d\n", ret);
1165}
1166
Shrenuj Bansala419c792016-10-20 14:05:11 -07001167static int _init_global_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt)
1168{
1169 int ret = 0;
1170 struct kgsl_iommu_pt *iommu_pt = NULL;
1171 unsigned int cb_num;
1172 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1173 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
1174
1175 iommu_pt = _alloc_pt(ctx->dev, mmu, pt);
1176
1177 if (IS_ERR(iommu_pt))
1178 return PTR_ERR(iommu_pt);
1179
1180 if (kgsl_mmu_is_perprocess(mmu)) {
1181 ret = iommu_domain_set_attr(iommu_pt->domain,
1182 DOMAIN_ATTR_PROCID, &pt->name);
1183 if (ret) {
1184 KGSL_CORE_ERR("set DOMAIN_ATTR_PROCID failed: %d\n",
1185 ret);
1186 goto done;
1187 }
1188 }
1189
Sushmita Susheelendra906564d2017-01-10 15:53:55 -07001190 _enable_gpuhtw_llc(mmu, iommu_pt);
1191
Shrenuj Bansala419c792016-10-20 14:05:11 -07001192 ret = _attach_pt(iommu_pt, ctx);
1193 if (ret)
1194 goto done;
1195
1196 iommu_set_fault_handler(iommu_pt->domain,
1197 kgsl_iommu_fault_handler, pt);
1198
1199 ret = iommu_domain_get_attr(iommu_pt->domain,
1200 DOMAIN_ATTR_CONTEXT_BANK, &cb_num);
1201 if (ret) {
1202 KGSL_CORE_ERR("get DOMAIN_ATTR_PROCID failed: %d\n",
1203 ret);
1204 goto done;
1205 }
1206
1207 ctx->cb_num = cb_num;
1208 ctx->regbase = iommu->regbase + KGSL_IOMMU_CB0_OFFSET
1209 + (cb_num << KGSL_IOMMU_CB_SHIFT);
1210
1211 ret = iommu_domain_get_attr(iommu_pt->domain,
1212 DOMAIN_ATTR_TTBR0, &iommu_pt->ttbr0);
1213 if (ret) {
1214 KGSL_CORE_ERR("get DOMAIN_ATTR_TTBR0 failed: %d\n",
1215 ret);
1216 goto done;
1217 }
1218 ret = iommu_domain_get_attr(iommu_pt->domain,
1219 DOMAIN_ATTR_CONTEXTIDR, &iommu_pt->contextidr);
1220 if (ret) {
1221 KGSL_CORE_ERR("get DOMAIN_ATTR_CONTEXTIDR failed: %d\n",
1222 ret);
1223 goto done;
1224 }
1225
1226 ret = kgsl_iommu_map_globals(pt);
1227
1228done:
1229 if (ret)
1230 _free_pt(ctx, pt);
1231
1232 return ret;
1233}
1234
1235static int _init_secure_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt)
1236{
1237 int ret = 0;
1238 struct kgsl_iommu_pt *iommu_pt = NULL;
1239 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1240 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_SECURE];
1241 int secure_vmid = VMID_CP_PIXEL;
1242 unsigned int cb_num;
1243
1244 if (!mmu->secured)
1245 return -EPERM;
1246
1247 if (!MMU_FEATURE(mmu, KGSL_MMU_HYP_SECURE_ALLOC)) {
1248 if (!kgsl_mmu_bus_secured(ctx->dev))
1249 return -EPERM;
1250 }
1251
1252 iommu_pt = _alloc_pt(ctx->dev, mmu, pt);
1253
1254 if (IS_ERR(iommu_pt))
1255 return PTR_ERR(iommu_pt);
1256
1257 ret = iommu_domain_set_attr(iommu_pt->domain,
1258 DOMAIN_ATTR_SECURE_VMID, &secure_vmid);
1259 if (ret) {
1260 KGSL_CORE_ERR("set DOMAIN_ATTR_SECURE_VMID failed: %d\n", ret);
1261 goto done;
1262 }
1263
Sushmita Susheelendra906564d2017-01-10 15:53:55 -07001264 _enable_gpuhtw_llc(mmu, iommu_pt);
1265
Shrenuj Bansala419c792016-10-20 14:05:11 -07001266 ret = _attach_pt(iommu_pt, ctx);
1267
1268 if (MMU_FEATURE(mmu, KGSL_MMU_HYP_SECURE_ALLOC))
1269 iommu_set_fault_handler(iommu_pt->domain,
1270 kgsl_iommu_fault_handler, pt);
1271
1272 ret = iommu_domain_get_attr(iommu_pt->domain,
1273 DOMAIN_ATTR_CONTEXT_BANK, &cb_num);
1274 if (ret) {
1275 KGSL_CORE_ERR("get DOMAIN_ATTR_PROCID failed: %d\n",
1276 ret);
1277 goto done;
1278 }
1279
1280 ctx->cb_num = cb_num;
1281 ctx->regbase = iommu->regbase + KGSL_IOMMU_CB0_OFFSET
1282 + (cb_num << KGSL_IOMMU_CB_SHIFT);
1283
1284 ret = kgsl_map_global_secure_pt_entry(pt);
1285
1286done:
1287 if (ret)
1288 _free_pt(ctx, pt);
1289 return ret;
1290}
1291
1292static int _init_per_process_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt)
1293{
1294 int ret = 0;
1295 struct kgsl_iommu_pt *iommu_pt = NULL;
1296 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1297 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
1298 int dynamic = 1;
1299 unsigned int cb_num = ctx->cb_num;
1300
1301 iommu_pt = _alloc_pt(ctx->dev, mmu, pt);
1302
1303 if (IS_ERR(iommu_pt))
1304 return PTR_ERR(iommu_pt);
1305
1306 ret = iommu_domain_set_attr(iommu_pt->domain,
1307 DOMAIN_ATTR_DYNAMIC, &dynamic);
1308 if (ret) {
1309 KGSL_CORE_ERR("set DOMAIN_ATTR_DYNAMIC failed: %d\n", ret);
1310 goto done;
1311 }
1312 ret = iommu_domain_set_attr(iommu_pt->domain,
1313 DOMAIN_ATTR_CONTEXT_BANK, &cb_num);
1314 if (ret) {
1315 KGSL_CORE_ERR("set DOMAIN_ATTR_CONTEXT_BANK failed: %d\n", ret);
1316 goto done;
1317 }
1318
1319 ret = iommu_domain_set_attr(iommu_pt->domain,
1320 DOMAIN_ATTR_PROCID, &pt->name);
1321 if (ret) {
1322 KGSL_CORE_ERR("set DOMAIN_ATTR_PROCID failed: %d\n", ret);
1323 goto done;
1324 }
1325
Sushmita Susheelendra906564d2017-01-10 15:53:55 -07001326 _enable_gpuhtw_llc(mmu, iommu_pt);
1327
Shrenuj Bansala419c792016-10-20 14:05:11 -07001328 ret = _attach_pt(iommu_pt, ctx);
1329 if (ret)
1330 goto done;
1331
1332 /* now read back the attributes needed for self programming */
1333 ret = iommu_domain_get_attr(iommu_pt->domain,
1334 DOMAIN_ATTR_TTBR0, &iommu_pt->ttbr0);
1335 if (ret) {
1336 KGSL_CORE_ERR("get DOMAIN_ATTR_TTBR0 failed: %d\n", ret);
1337 goto done;
1338 }
1339
1340 ret = iommu_domain_get_attr(iommu_pt->domain,
1341 DOMAIN_ATTR_CONTEXTIDR, &iommu_pt->contextidr);
1342 if (ret) {
1343 KGSL_CORE_ERR("get DOMAIN_ATTR_CONTEXTIDR failed: %d\n", ret);
1344 goto done;
1345 }
1346
1347 ret = kgsl_iommu_map_globals(pt);
1348
1349done:
1350 if (ret)
1351 _free_pt(ctx, pt);
1352
1353 return ret;
1354}
1355
1356/* kgsl_iommu_init_pt - Set up an IOMMU pagetable */
1357static int kgsl_iommu_init_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt)
1358{
1359 if (pt == NULL)
1360 return -EINVAL;
1361
1362 switch (pt->name) {
1363 case KGSL_MMU_GLOBAL_PT:
1364 return _init_global_pt(mmu, pt);
1365
1366 case KGSL_MMU_SECURE_PT:
1367 return _init_secure_pt(mmu, pt);
1368
1369 default:
1370 return _init_per_process_pt(mmu, pt);
1371 }
1372}
1373
1374static struct kgsl_pagetable *kgsl_iommu_getpagetable(struct kgsl_mmu *mmu,
1375 unsigned long name)
1376{
1377 struct kgsl_pagetable *pt;
1378
1379 if (!kgsl_mmu_is_perprocess(mmu) && (name != KGSL_MMU_SECURE_PT)) {
1380 name = KGSL_MMU_GLOBAL_PT;
1381 if (mmu->defaultpagetable != NULL)
1382 return mmu->defaultpagetable;
1383 }
1384
1385 pt = kgsl_get_pagetable(name);
1386 if (pt == NULL)
1387 pt = kgsl_mmu_createpagetableobject(mmu, name);
1388
1389 return pt;
1390}
1391
1392/*
1393 * kgsl_iommu_get_reg_ahbaddr - Returns the ahb address of the register
1394 * @mmu - Pointer to mmu structure
1395 * @id - The context ID of the IOMMU ctx
1396 * @reg - The register for which address is required
1397 *
1398 * Return - The address of register which can be used in type0 packet
1399 */
1400static unsigned int kgsl_iommu_get_reg_ahbaddr(struct kgsl_mmu *mmu,
1401 int id, unsigned int reg)
1402{
1403 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1404 struct kgsl_iommu_context *ctx = &iommu->ctx[id];
1405
1406 return ctx->gpu_offset + kgsl_iommu_reg_list[reg];
1407}
1408
1409static void _detach_context(struct kgsl_iommu_context *ctx)
1410{
1411 struct kgsl_iommu_pt *iommu_pt;
1412
1413 if (ctx->default_pt == NULL)
1414 return;
1415
1416 iommu_pt = ctx->default_pt->priv;
1417
1418 _detach_pt(iommu_pt, ctx);
1419
1420 ctx->default_pt = NULL;
1421}
1422
1423static void kgsl_iommu_close(struct kgsl_mmu *mmu)
1424{
1425 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1426 int i;
1427
1428 for (i = 0; i < KGSL_IOMMU_CONTEXT_MAX; i++)
1429 _detach_context(&iommu->ctx[i]);
1430
1431 kgsl_mmu_putpagetable(mmu->defaultpagetable);
1432 mmu->defaultpagetable = NULL;
1433
1434 kgsl_mmu_putpagetable(mmu->securepagetable);
1435 mmu->securepagetable = NULL;
1436
1437 if (iommu->regbase != NULL)
1438 iounmap(iommu->regbase);
1439
1440 kgsl_sharedmem_free(&kgsl_secure_guard_page_memdesc);
1441
1442 if (kgsl_guard_page != NULL) {
1443 __free_page(kgsl_guard_page);
1444 kgsl_guard_page = NULL;
1445 }
1446
1447 if (kgsl_dummy_page != NULL) {
1448 __free_page(kgsl_dummy_page);
1449 kgsl_dummy_page = NULL;
1450 }
1451
1452 kgsl_iommu_remove_global(mmu, &iommu->setstate);
1453 kgsl_sharedmem_free(&iommu->setstate);
1454 kgsl_cleanup_qdss_desc(mmu);
1455}
1456
1457static int _setstate_alloc(struct kgsl_device *device,
1458 struct kgsl_iommu *iommu)
1459{
1460 int ret;
1461
1462 ret = kgsl_sharedmem_alloc_contig(device, &iommu->setstate, PAGE_SIZE);
1463
1464 if (!ret) {
1465 /* Mark the setstate memory as read only */
1466 iommu->setstate.flags |= KGSL_MEMFLAGS_GPUREADONLY;
1467
1468 kgsl_sharedmem_set(device, &iommu->setstate, 0, 0, PAGE_SIZE);
1469 }
1470
1471 return ret;
1472}
1473
1474static int kgsl_iommu_init(struct kgsl_mmu *mmu)
1475{
1476 struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
1477 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1478 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
1479 int status;
1480
1481 mmu->features |= KGSL_MMU_PAGED;
1482
1483 if (ctx->name == NULL) {
1484 KGSL_CORE_ERR("dt: gfx3d0_user context bank not found\n");
1485 return -EINVAL;
1486 }
1487
1488 status = _setstate_alloc(device, iommu);
1489 if (status)
1490 return status;
1491
1492 /* check requirements for per process pagetables */
1493 if (ctx->gpu_offset == UINT_MAX) {
1494 KGSL_CORE_ERR("missing qcom,gpu-offset forces global pt\n");
1495 mmu->features |= KGSL_MMU_GLOBAL_PAGETABLE;
1496 }
1497
1498 if (iommu->version == 1 && iommu->micro_mmu_ctrl == UINT_MAX) {
1499 KGSL_CORE_ERR(
1500 "missing qcom,micro-mmu-control forces global pt\n");
1501 mmu->features |= KGSL_MMU_GLOBAL_PAGETABLE;
1502 }
1503
1504 /* Check to see if we need to do the IOMMU sync dance */
1505 need_iommu_sync = of_property_read_bool(device->pdev->dev.of_node,
1506 "qcom,gpu-quirk-iommu-sync");
1507
1508 iommu->regbase = ioremap(iommu->regstart, iommu->regsize);
1509 if (iommu->regbase == NULL) {
1510 KGSL_CORE_ERR("Could not map IOMMU registers 0x%lx:0x%x\n",
1511 iommu->regstart, iommu->regsize);
1512 status = -ENOMEM;
1513 goto done;
1514 }
1515
1516 if (addr_entry_cache == NULL) {
1517 addr_entry_cache = KMEM_CACHE(kgsl_iommu_addr_entry, 0);
1518 if (addr_entry_cache == NULL) {
1519 status = -ENOMEM;
1520 goto done;
1521 }
1522 }
1523
1524 kgsl_iommu_add_global(mmu, &iommu->setstate, "setstate");
1525 kgsl_setup_qdss_desc(device);
1526
1527done:
1528 if (status)
1529 kgsl_iommu_close(mmu);
1530
1531 return status;
1532}
1533
1534static int _setup_user_context(struct kgsl_mmu *mmu)
1535{
1536 int ret = 0;
1537 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1538 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
1539 struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
1540 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1541 struct kgsl_iommu_pt *iommu_pt = NULL;
1542 unsigned int sctlr_val;
1543
1544 if (mmu->defaultpagetable == NULL) {
1545 mmu->defaultpagetable = kgsl_mmu_getpagetable(mmu,
1546 KGSL_MMU_GLOBAL_PT);
1547 /* if we don't have a default pagetable, nothing will work */
1548 if (IS_ERR(mmu->defaultpagetable)) {
1549 ret = PTR_ERR(mmu->defaultpagetable);
1550 mmu->defaultpagetable = NULL;
1551 return ret;
1552 }
1553 }
1554
1555 iommu_pt = mmu->defaultpagetable->priv;
1556 if (iommu_pt == NULL)
1557 return -ENODEV;
1558
1559 ret = _attach_pt(iommu_pt, ctx);
1560 if (ret)
1561 return ret;
1562
1563 ctx->default_pt = mmu->defaultpagetable;
1564
1565 kgsl_iommu_enable_clk(mmu);
1566
1567 sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR);
1568
1569 /*
1570 * If pagefault policy is GPUHALT_ENABLE,
1571 * 1) Program CFCFG to 1 to enable STALL mode
1572 * 2) Program HUPCF to 0 (Stall or terminate subsequent
1573 * transactions in the presence of an outstanding fault)
1574 * else
1575 * 1) Program CFCFG to 0 to disable STALL mode (0=Terminate)
1576 * 2) Program HUPCF to 1 (Process subsequent transactions
1577 * independently of any outstanding fault)
1578 */
1579
1580 if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE,
1581 &adreno_dev->ft_pf_policy)) {
1582 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
1583 sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
1584 } else {
1585 sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
1586 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
1587 }
1588 KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val);
1589 kgsl_iommu_disable_clk(mmu);
1590
1591 return 0;
1592}
1593
1594static int _setup_secure_context(struct kgsl_mmu *mmu)
1595{
1596 int ret;
1597 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1598 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_SECURE];
1599 unsigned int cb_num;
1600
1601 struct kgsl_iommu_pt *iommu_pt;
1602
1603 if (ctx->dev == NULL || !mmu->secured)
1604 return 0;
1605
1606 if (mmu->securepagetable == NULL) {
1607 mmu->securepagetable = kgsl_mmu_getpagetable(mmu,
1608 KGSL_MMU_SECURE_PT);
1609 if (IS_ERR(mmu->securepagetable)) {
1610 ret = PTR_ERR(mmu->securepagetable);
1611 mmu->securepagetable = NULL;
1612 return ret;
1613 } else if (mmu->securepagetable == NULL) {
1614 return -ENOMEM;
1615 }
1616 }
1617 iommu_pt = mmu->securepagetable->priv;
1618
1619 ret = _attach_pt(iommu_pt, ctx);
1620 if (ret)
1621 goto done;
1622
1623 ctx->default_pt = mmu->securepagetable;
1624
1625 ret = iommu_domain_get_attr(iommu_pt->domain, DOMAIN_ATTR_CONTEXT_BANK,
1626 &cb_num);
1627 if (ret) {
1628 KGSL_CORE_ERR("get CONTEXT_BANK attr, err %d\n", ret);
1629 goto done;
1630 }
1631 ctx->cb_num = cb_num;
1632done:
1633 if (ret)
1634 _detach_context(ctx);
1635 return ret;
1636}
1637
1638static int kgsl_iommu_set_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt);
1639
1640static int kgsl_iommu_start(struct kgsl_mmu *mmu)
1641{
1642 int status;
1643 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1644
1645 status = _setup_user_context(mmu);
1646 if (status)
1647 return status;
1648
1649 status = _setup_secure_context(mmu);
1650 if (status) {
1651 _detach_context(&iommu->ctx[KGSL_IOMMU_CONTEXT_USER]);
1652 return status;
1653 }
1654
1655 /* Make sure the hardware is programmed to the default pagetable */
1656 return kgsl_iommu_set_pt(mmu, mmu->defaultpagetable);
1657}
1658
1659static int
1660kgsl_iommu_unmap_offset(struct kgsl_pagetable *pt,
1661 struct kgsl_memdesc *memdesc, uint64_t addr,
1662 uint64_t offset, uint64_t size)
1663{
1664 if (size == 0 || (size + offset) > kgsl_memdesc_footprint(memdesc))
1665 return -EINVAL;
1666 /*
1667 * All GPU addresses as assigned are page aligned, but some
1668 * functions perturb the gpuaddr with an offset, so apply the
1669 * mask here to make sure we have the right address.
1670 */
1671
1672 addr = PAGE_ALIGN(addr);
1673 if (addr == 0)
1674 return -EINVAL;
1675
1676 return _iommu_unmap_sync_pc(pt, memdesc, addr + offset, size);
1677}
1678
1679static int
1680kgsl_iommu_unmap(struct kgsl_pagetable *pt, struct kgsl_memdesc *memdesc)
1681{
1682 if (memdesc->size == 0 || memdesc->gpuaddr == 0)
1683 return -EINVAL;
1684
1685 return kgsl_iommu_unmap_offset(pt, memdesc, memdesc->gpuaddr, 0,
1686 kgsl_memdesc_footprint(memdesc));
1687}
1688
1689/**
1690 * _iommu_map_guard_page - Map iommu guard page
1691 * @pt - Pointer to kgsl pagetable structure
1692 * @memdesc - memdesc to add guard page
1693 * @gpuaddr - GPU addr of guard page
1694 * @protflags - flags for mapping
1695 *
1696 * Return 0 on success, error on map fail
1697 */
1698static int _iommu_map_guard_page(struct kgsl_pagetable *pt,
1699 struct kgsl_memdesc *memdesc,
1700 uint64_t gpuaddr,
1701 unsigned int protflags)
1702{
1703 phys_addr_t physaddr;
1704
1705 if (!kgsl_memdesc_has_guard_page(memdesc))
1706 return 0;
1707
1708 /*
1709 * Allocate guard page for secure buffers.
1710 * This has to be done after we attach a smmu pagetable.
1711 * Allocate the guard page when first secure buffer is.
1712 * mapped to save 1MB of memory if CPZ is not used.
1713 */
1714 if (kgsl_memdesc_is_secured(memdesc)) {
1715 struct scatterlist *sg;
1716 unsigned int sgp_size = pt->mmu->secure_align_mask + 1;
1717
1718 if (!kgsl_secure_guard_page_memdesc.sgt) {
1719 if (kgsl_allocate_user(KGSL_MMU_DEVICE(pt->mmu),
1720 &kgsl_secure_guard_page_memdesc,
1721 sgp_size, KGSL_MEMFLAGS_SECURE)) {
1722 KGSL_CORE_ERR(
1723 "Secure guard page alloc failed\n");
1724 return -ENOMEM;
1725 }
1726 }
1727
1728 sg = kgsl_secure_guard_page_memdesc.sgt->sgl;
1729 physaddr = page_to_phys(sg_page(sg));
1730 } else {
1731 if (kgsl_guard_page == NULL) {
1732 kgsl_guard_page = alloc_page(GFP_KERNEL | __GFP_ZERO |
1733 __GFP_NORETRY | __GFP_HIGHMEM);
1734 if (kgsl_guard_page == NULL)
1735 return -ENOMEM;
1736 }
1737
1738 physaddr = page_to_phys(kgsl_guard_page);
1739 }
1740
1741 return _iommu_map_sync_pc(pt, memdesc, gpuaddr, physaddr,
1742 kgsl_memdesc_guard_page_size(memdesc),
1743 protflags & ~IOMMU_WRITE);
1744}
1745
1746static unsigned int _get_protection_flags(struct kgsl_memdesc *memdesc)
1747{
Sushmita Susheelendra7f66cf72016-09-12 11:04:43 -06001748 unsigned int flags = IOMMU_READ | IOMMU_WRITE |
1749 IOMMU_NOEXEC | IOMMU_USE_UPSTREAM_HINT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001750
1751 if (memdesc->flags & KGSL_MEMFLAGS_GPUREADONLY)
1752 flags &= ~IOMMU_WRITE;
1753
1754 if (memdesc->priv & KGSL_MEMDESC_PRIVILEGED)
1755 flags |= IOMMU_PRIV;
1756
1757 return flags;
1758}
1759
1760static int
1761kgsl_iommu_map(struct kgsl_pagetable *pt,
1762 struct kgsl_memdesc *memdesc)
1763{
1764 int ret;
1765 uint64_t addr = memdesc->gpuaddr;
1766 uint64_t size = memdesc->size;
1767 unsigned int flags = _get_protection_flags(memdesc);
1768 struct sg_table *sgt = NULL;
1769
1770 /*
1771 * For paged memory allocated through kgsl, memdesc->pages is not NULL.
1772 * Allocate sgt here just for its map operation. Contiguous memory
1773 * already has its sgt, so no need to allocate it here.
1774 */
1775 if (memdesc->pages != NULL)
1776 sgt = kgsl_alloc_sgt_from_pages(memdesc);
1777 else
1778 sgt = memdesc->sgt;
1779
1780 if (IS_ERR(sgt))
1781 return PTR_ERR(sgt);
1782
1783 ret = _iommu_map_sg_sync_pc(pt, addr, memdesc, sgt->sgl,
1784 sgt->nents, flags);
1785 if (ret)
1786 goto done;
1787
1788 ret = _iommu_map_guard_page(pt, memdesc, addr + size, flags);
1789 if (ret)
1790 _iommu_unmap_sync_pc(pt, memdesc, addr, size);
1791
1792done:
1793 if (memdesc->pages != NULL)
1794 kgsl_free_sgt(sgt);
1795
1796 return ret;
1797}
1798
1799static int kgsl_iommu_sparse_dummy_map(struct kgsl_pagetable *pt,
1800 struct kgsl_memdesc *memdesc, uint64_t offset, uint64_t size)
1801{
1802 int ret = 0, i;
1803 struct page **pages = NULL;
1804 struct sg_table sgt;
1805 int count = size >> PAGE_SHIFT;
1806
1807 /* verify the offset is within our range */
1808 if (size + offset > memdesc->size)
1809 return -EINVAL;
1810
1811 if (kgsl_dummy_page == NULL) {
1812 kgsl_dummy_page = alloc_page(GFP_KERNEL | __GFP_ZERO |
1813 __GFP_HIGHMEM);
1814 if (kgsl_dummy_page == NULL)
1815 return -ENOMEM;
1816 }
1817
1818 pages = kcalloc(count, sizeof(struct page *), GFP_KERNEL);
1819 if (pages == NULL)
1820 return -ENOMEM;
1821
1822 for (i = 0; i < count; i++)
1823 pages[i] = kgsl_dummy_page;
1824
1825 ret = sg_alloc_table_from_pages(&sgt, pages, count,
1826 0, size, GFP_KERNEL);
1827 if (ret == 0) {
1828 ret = _iommu_map_sg_sync_pc(pt, memdesc->gpuaddr + offset,
1829 memdesc, sgt.sgl, sgt.nents,
1830 IOMMU_READ | IOMMU_NOEXEC);
1831 sg_free_table(&sgt);
1832 }
1833
1834 kfree(pages);
1835
1836 return ret;
1837}
1838
1839static int _map_to_one_page(struct kgsl_pagetable *pt, uint64_t addr,
1840 struct kgsl_memdesc *memdesc, uint64_t physoffset,
1841 uint64_t size, unsigned int map_flags)
1842{
1843 int ret = 0, i;
1844 int pg_sz = kgsl_memdesc_get_pagesize(memdesc);
1845 int count = size >> PAGE_SHIFT;
1846 struct page *page = NULL;
1847 struct page **pages = NULL;
1848 struct sg_page_iter sg_iter;
1849 struct sg_table sgt;
1850
1851 /* Find our physaddr offset addr */
1852 if (memdesc->pages != NULL)
1853 page = memdesc->pages[physoffset >> PAGE_SHIFT];
1854 else {
1855 for_each_sg_page(memdesc->sgt->sgl, &sg_iter,
1856 memdesc->sgt->nents, physoffset >> PAGE_SHIFT) {
1857 page = sg_page_iter_page(&sg_iter);
1858 break;
1859 }
1860 }
1861
1862 if (page == NULL)
1863 return -EINVAL;
1864
1865 pages = kcalloc(count, sizeof(struct page *), GFP_KERNEL);
1866 if (pages == NULL)
1867 return -ENOMEM;
1868
1869 for (i = 0; i < count; i++) {
1870 if (pg_sz != PAGE_SIZE) {
1871 struct page *tmp_page = page;
1872 int j;
1873
1874 for (j = 0; j < 16; j++, tmp_page += PAGE_SIZE)
1875 pages[i++] = tmp_page;
1876 } else
1877 pages[i] = page;
1878 }
1879
1880 ret = sg_alloc_table_from_pages(&sgt, pages, count,
1881 0, size, GFP_KERNEL);
1882 if (ret == 0) {
1883 ret = _iommu_map_sg_sync_pc(pt, addr, memdesc, sgt.sgl,
1884 sgt.nents, map_flags);
1885 sg_free_table(&sgt);
1886 }
1887
1888 kfree(pages);
1889
1890 return ret;
1891}
1892
1893static int kgsl_iommu_map_offset(struct kgsl_pagetable *pt,
1894 uint64_t virtaddr, uint64_t virtoffset,
1895 struct kgsl_memdesc *memdesc, uint64_t physoffset,
1896 uint64_t size, uint64_t feature_flag)
1897{
1898 int pg_sz;
1899 unsigned int protflags = _get_protection_flags(memdesc);
1900 int ret;
1901 struct sg_table *sgt = NULL;
1902
1903 pg_sz = kgsl_memdesc_get_pagesize(memdesc);
1904 if (!IS_ALIGNED(virtaddr | virtoffset | physoffset | size, pg_sz))
1905 return -EINVAL;
1906
1907 if (size == 0)
1908 return -EINVAL;
1909
1910 if (!(feature_flag & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS) &&
1911 size + physoffset > kgsl_memdesc_footprint(memdesc))
1912 return -EINVAL;
1913
1914 /*
1915 * For paged memory allocated through kgsl, memdesc->pages is not NULL.
1916 * Allocate sgt here just for its map operation. Contiguous memory
1917 * already has its sgt, so no need to allocate it here.
1918 */
1919 if (memdesc->pages != NULL)
1920 sgt = kgsl_alloc_sgt_from_pages(memdesc);
1921 else
1922 sgt = memdesc->sgt;
1923
1924 if (IS_ERR(sgt))
1925 return PTR_ERR(sgt);
1926
1927 if (feature_flag & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS)
1928 ret = _map_to_one_page(pt, virtaddr + virtoffset,
1929 memdesc, physoffset, size, protflags);
1930 else
1931 ret = _iommu_map_sg_offset_sync_pc(pt, virtaddr + virtoffset,
1932 memdesc, sgt->sgl, sgt->nents,
1933 physoffset, size, protflags);
1934
1935 if (memdesc->pages != NULL)
1936 kgsl_free_sgt(sgt);
1937
1938 return ret;
1939}
1940
1941/* This function must be called with context bank attached */
1942static void kgsl_iommu_clear_fsr(struct kgsl_mmu *mmu)
1943{
1944 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1945 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
1946 unsigned int sctlr_val;
1947
1948 if (ctx->default_pt != NULL) {
1949 kgsl_iommu_enable_clk(mmu);
1950 KGSL_IOMMU_SET_CTX_REG(ctx, FSR, 0xffffffff);
1951 /*
1952 * Re-enable context fault interrupts after clearing
1953 * FSR to prevent the interrupt from firing repeatedly
1954 */
1955 sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR);
1956 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFIE_SHIFT);
1957 KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val);
1958 /*
1959 * Make sure the above register writes
1960 * are not reordered across the barrier
1961 * as we use writel_relaxed to write them
1962 */
1963 wmb();
1964 kgsl_iommu_disable_clk(mmu);
1965 }
1966}
1967
1968static void kgsl_iommu_pagefault_resume(struct kgsl_mmu *mmu)
1969{
1970 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1971 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
1972
1973 if (ctx->default_pt != NULL && ctx->fault) {
1974 /*
1975 * Write 1 to RESUME.TnR to terminate the
1976 * stalled transaction.
1977 */
1978 KGSL_IOMMU_SET_CTX_REG(ctx, RESUME, 1);
1979 /*
1980 * Make sure the above register writes
1981 * are not reordered across the barrier
1982 * as we use writel_relaxed to write them
1983 */
1984 wmb();
1985 ctx->fault = 0;
1986 }
1987}
1988
1989static void kgsl_iommu_stop(struct kgsl_mmu *mmu)
1990{
1991 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1992 int i;
1993
1994 /*
1995 * If the iommu supports retention, we don't need
1996 * to detach when stopping.
1997 */
1998 if (!MMU_FEATURE(mmu, KGSL_MMU_RETENTION)) {
1999 for (i = 0; i < KGSL_IOMMU_CONTEXT_MAX; i++)
2000 _detach_context(&iommu->ctx[i]);
2001 }
2002}
2003
2004static u64
2005kgsl_iommu_get_current_ttbr0(struct kgsl_mmu *mmu)
2006{
2007 u64 val;
2008 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2009 /*
2010 * We cannot enable or disable the clocks in interrupt context, this
2011 * function is called from interrupt context if there is an axi error
2012 */
2013 if (in_interrupt())
2014 return 0;
2015
2016 kgsl_iommu_enable_clk(mmu);
2017 val = KGSL_IOMMU_GET_CTX_REG_Q(&iommu->ctx[KGSL_IOMMU_CONTEXT_USER],
2018 TTBR0);
2019 kgsl_iommu_disable_clk(mmu);
2020 return val;
2021}
2022
2023/*
2024 * kgsl_iommu_set_pt - Change the IOMMU pagetable of the primary context bank
2025 * @mmu - Pointer to mmu structure
2026 * @pt - Pagetable to switch to
2027 *
2028 * Set the new pagetable for the IOMMU by doing direct register writes
2029 * to the IOMMU registers through the cpu
2030 *
2031 * Return - void
2032 */
2033static int kgsl_iommu_set_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt)
2034{
2035 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2036 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
2037 uint64_t ttbr0, temp;
2038 unsigned int contextidr;
2039 unsigned long wait_for_flush;
2040
2041 if ((pt != mmu->defaultpagetable) && !kgsl_mmu_is_perprocess(mmu))
2042 return 0;
2043
2044 kgsl_iommu_enable_clk(mmu);
2045
2046 ttbr0 = kgsl_mmu_pagetable_get_ttbr0(pt);
2047 contextidr = kgsl_mmu_pagetable_get_contextidr(pt);
2048
2049 KGSL_IOMMU_SET_CTX_REG_Q(ctx, TTBR0, ttbr0);
2050 KGSL_IOMMU_SET_CTX_REG(ctx, CONTEXTIDR, contextidr);
2051
2052 /* memory barrier before reading TTBR0 register */
2053 mb();
2054 temp = KGSL_IOMMU_GET_CTX_REG_Q(ctx, TTBR0);
2055
2056 KGSL_IOMMU_SET_CTX_REG(ctx, TLBIALL, 1);
2057 /* make sure the TBLI write completes before we wait */
2058 mb();
2059 /*
2060 * Wait for flush to complete by polling the flush
2061 * status bit of TLBSTATUS register for not more than
2062 * 2 s. After 2s just exit, at that point the SMMU h/w
2063 * may be stuck and will eventually cause GPU to hang
2064 * or bring the system down.
2065 */
2066 wait_for_flush = jiffies + msecs_to_jiffies(2000);
2067 KGSL_IOMMU_SET_CTX_REG(ctx, TLBSYNC, 0);
2068 while (KGSL_IOMMU_GET_CTX_REG(ctx, TLBSTATUS) &
2069 (KGSL_IOMMU_CTX_TLBSTATUS_SACTIVE)) {
2070 if (time_after(jiffies, wait_for_flush)) {
2071 KGSL_DRV_WARN(KGSL_MMU_DEVICE(mmu),
2072 "Wait limit reached for IOMMU tlb flush\n");
2073 break;
2074 }
2075 cpu_relax();
2076 }
2077
2078 kgsl_iommu_disable_clk(mmu);
2079 return 0;
2080}
2081
2082/*
2083 * kgsl_iommu_set_pf_policy() - Set the pagefault policy for IOMMU
2084 * @mmu: Pointer to mmu structure
2085 * @pf_policy: The pagefault polict to set
2086 *
2087 * Check if the new policy indicated by pf_policy is same as current
2088 * policy, if same then return else set the policy
2089 */
2090static int kgsl_iommu_set_pf_policy(struct kgsl_mmu *mmu,
2091 unsigned long pf_policy)
2092{
2093 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2094 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
2095 struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
2096 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2097
2098 if ((adreno_dev->ft_pf_policy &
2099 BIT(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE)) ==
2100 (pf_policy & BIT(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE)))
2101 return 0;
2102
2103 /* If not attached, policy will be updated during the next attach */
2104 if (ctx->default_pt != NULL) {
2105 unsigned int sctlr_val;
2106
2107 kgsl_iommu_enable_clk(mmu);
2108
2109 sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR);
2110
2111 if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, &pf_policy)) {
2112 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
2113 sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
2114 } else {
2115 sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
2116 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
2117 }
2118
2119 KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val);
2120
2121 kgsl_iommu_disable_clk(mmu);
2122 }
2123
2124 return 0;
2125}
2126
2127static struct kgsl_protected_registers *
2128kgsl_iommu_get_prot_regs(struct kgsl_mmu *mmu)
2129{
2130 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2131
2132 return &iommu->protect;
2133}
2134
2135static struct kgsl_iommu_addr_entry *_find_gpuaddr(
2136 struct kgsl_pagetable *pagetable, uint64_t gpuaddr)
2137{
2138 struct kgsl_iommu_pt *pt = pagetable->priv;
2139 struct rb_node *node = pt->rbtree.rb_node;
2140
2141 while (node != NULL) {
2142 struct kgsl_iommu_addr_entry *entry = rb_entry(node,
2143 struct kgsl_iommu_addr_entry, node);
2144
2145 if (gpuaddr < entry->base)
2146 node = node->rb_left;
2147 else if (gpuaddr > entry->base)
2148 node = node->rb_right;
2149 else
2150 return entry;
2151 }
2152
2153 return NULL;
2154}
2155
2156static int _remove_gpuaddr(struct kgsl_pagetable *pagetable,
2157 uint64_t gpuaddr)
2158{
2159 struct kgsl_iommu_pt *pt = pagetable->priv;
2160 struct kgsl_iommu_addr_entry *entry;
2161
2162 entry = _find_gpuaddr(pagetable, gpuaddr);
2163
2164 if (entry != NULL) {
2165 rb_erase(&entry->node, &pt->rbtree);
2166 kmem_cache_free(addr_entry_cache, entry);
2167 return 0;
2168 }
2169
2170 WARN(1, "Couldn't remove gpuaddr: 0x%llx\n", gpuaddr);
2171 return -ENOMEM;
2172}
2173
2174static int _insert_gpuaddr(struct kgsl_pagetable *pagetable,
2175 uint64_t gpuaddr, uint64_t size)
2176{
2177 struct kgsl_iommu_pt *pt = pagetable->priv;
2178 struct rb_node **node, *parent = NULL;
2179 struct kgsl_iommu_addr_entry *new =
2180 kmem_cache_alloc(addr_entry_cache, GFP_ATOMIC);
2181
2182 if (new == NULL)
2183 return -ENOMEM;
2184
2185 new->base = gpuaddr;
2186 new->size = size;
2187
2188 node = &pt->rbtree.rb_node;
2189
2190 while (*node != NULL) {
2191 struct kgsl_iommu_addr_entry *this;
2192
2193 parent = *node;
2194 this = rb_entry(parent, struct kgsl_iommu_addr_entry, node);
2195
2196 if (new->base < this->base)
2197 node = &parent->rb_left;
2198 else if (new->base > this->base)
2199 node = &parent->rb_right;
2200 else {
2201 /* Duplicate entry */
2202 WARN(1, "duplicate gpuaddr: 0x%llx\n", gpuaddr);
2203 return -EEXIST;
2204 }
2205 }
2206
2207 rb_link_node(&new->node, parent, node);
2208 rb_insert_color(&new->node, &pt->rbtree);
2209
2210 return 0;
2211}
2212
2213static uint64_t _get_unmapped_area(struct kgsl_pagetable *pagetable,
2214 uint64_t bottom, uint64_t top, uint64_t size,
2215 uint64_t align)
2216{
2217 struct kgsl_iommu_pt *pt = pagetable->priv;
2218 struct rb_node *node = rb_first(&pt->rbtree);
2219 uint64_t start;
2220
2221 bottom = ALIGN(bottom, align);
2222 start = bottom;
2223
2224 while (node != NULL) {
2225 uint64_t gap;
2226 struct kgsl_iommu_addr_entry *entry = rb_entry(node,
2227 struct kgsl_iommu_addr_entry, node);
2228
2229 /*
2230 * Skip any entries that are outside of the range, but make sure
2231 * to account for some that might straddle the lower bound
2232 */
2233 if (entry->base < bottom) {
2234 if (entry->base + entry->size > bottom)
2235 start = ALIGN(entry->base + entry->size, align);
2236 node = rb_next(node);
2237 continue;
2238 }
2239
2240 /* Stop if we went over the top */
2241 if (entry->base >= top)
2242 break;
2243
2244 /* Make sure there is a gap to consider */
2245 if (start < entry->base) {
2246 gap = entry->base - start;
2247
2248 if (gap >= size)
2249 return start;
2250 }
2251
2252 /* Stop if there is no more room in the region */
2253 if (entry->base + entry->size >= top)
2254 return (uint64_t) -ENOMEM;
2255
2256 /* Start the next cycle at the end of the current entry */
2257 start = ALIGN(entry->base + entry->size, align);
2258 node = rb_next(node);
2259 }
2260
2261 if (start + size <= top)
2262 return start;
2263
2264 return (uint64_t) -ENOMEM;
2265}
2266
2267static uint64_t _get_unmapped_area_topdown(struct kgsl_pagetable *pagetable,
2268 uint64_t bottom, uint64_t top, uint64_t size,
2269 uint64_t align)
2270{
2271 struct kgsl_iommu_pt *pt = pagetable->priv;
2272 struct rb_node *node = rb_last(&pt->rbtree);
2273 uint64_t end = top;
2274 uint64_t mask = ~(align - 1);
2275 struct kgsl_iommu_addr_entry *entry;
2276
2277 /* Make sure that the bottom is correctly aligned */
2278 bottom = ALIGN(bottom, align);
2279
2280 /* Make sure the requested size will fit in the range */
2281 if (size > (top - bottom))
2282 return -ENOMEM;
2283
2284 /* Walk back through the list to find the highest entry in the range */
2285 for (node = rb_last(&pt->rbtree); node != NULL; node = rb_prev(node)) {
2286 entry = rb_entry(node, struct kgsl_iommu_addr_entry, node);
2287 if (entry->base < top)
2288 break;
2289 }
2290
2291 while (node != NULL) {
2292 uint64_t offset;
2293
2294 entry = rb_entry(node, struct kgsl_iommu_addr_entry, node);
2295
2296 /* If the entire entry is below the range the search is over */
2297 if ((entry->base + entry->size) < bottom)
2298 break;
2299
2300 /* Get the top of the entry properly aligned */
2301 offset = ALIGN(entry->base + entry->size, align);
2302
2303 /*
2304 * Try to allocate the memory from the top of the gap,
2305 * making sure that it fits between the top of this entry and
2306 * the bottom of the previous one
2307 */
2308
2309 if ((end > size) && (offset < end)) {
2310 uint64_t chunk = (end - size) & mask;
2311
2312 if (chunk >= offset)
2313 return chunk;
2314 }
2315
2316 /*
2317 * If we get here and the current entry is outside of the range
2318 * then we are officially out of room
2319 */
2320
2321 if (entry->base < bottom)
2322 return (uint64_t) -ENOMEM;
2323
2324 /* Set the top of the gap to the current entry->base */
2325 end = entry->base;
2326
2327 /* And move on to the next lower entry */
2328 node = rb_prev(node);
2329 }
2330
2331 /* If we get here then there are no more entries in the region */
2332 if ((end > size) && (((end - size) & mask) >= bottom))
2333 return (end - size) & mask;
2334
2335 return (uint64_t) -ENOMEM;
2336}
2337
2338static uint64_t kgsl_iommu_find_svm_region(struct kgsl_pagetable *pagetable,
2339 uint64_t start, uint64_t end, uint64_t size,
2340 uint64_t alignment)
2341{
2342 uint64_t addr;
2343
2344 /* Avoid black holes */
2345 if (WARN(end <= start, "Bad search range: 0x%llx-0x%llx", start, end))
2346 return (uint64_t) -EINVAL;
2347
2348 spin_lock(&pagetable->lock);
2349 addr = _get_unmapped_area_topdown(pagetable,
2350 start, end, size, alignment);
2351 spin_unlock(&pagetable->lock);
2352 return addr;
2353}
2354
2355static int kgsl_iommu_set_svm_region(struct kgsl_pagetable *pagetable,
2356 uint64_t gpuaddr, uint64_t size)
2357{
2358 int ret = -ENOMEM;
2359 struct kgsl_iommu_pt *pt = pagetable->priv;
2360 struct rb_node *node;
2361
2362 /* Make sure the requested address doesn't fall in the global range */
2363 if (ADDR_IN_GLOBAL(gpuaddr) || ADDR_IN_GLOBAL(gpuaddr + size))
2364 return -ENOMEM;
2365
2366 spin_lock(&pagetable->lock);
2367 node = pt->rbtree.rb_node;
2368
2369 while (node != NULL) {
2370 uint64_t start, end;
2371 struct kgsl_iommu_addr_entry *entry = rb_entry(node,
2372 struct kgsl_iommu_addr_entry, node);
2373
2374 start = entry->base;
2375 end = entry->base + entry->size;
2376
2377 if (gpuaddr + size <= start)
2378 node = node->rb_left;
2379 else if (end <= gpuaddr)
2380 node = node->rb_right;
2381 else
2382 goto out;
2383 }
2384
2385 ret = _insert_gpuaddr(pagetable, gpuaddr, size);
2386out:
2387 spin_unlock(&pagetable->lock);
2388 return ret;
2389}
2390
2391
2392static int kgsl_iommu_get_gpuaddr(struct kgsl_pagetable *pagetable,
2393 struct kgsl_memdesc *memdesc)
2394{
2395 struct kgsl_iommu_pt *pt = pagetable->priv;
2396 int ret = 0;
2397 uint64_t addr, start, end, size;
2398 unsigned int align;
2399
2400 if (WARN_ON(kgsl_memdesc_use_cpu_map(memdesc)))
2401 return -EINVAL;
2402
2403 if (memdesc->flags & KGSL_MEMFLAGS_SECURE &&
2404 pagetable->name != KGSL_MMU_SECURE_PT)
2405 return -EINVAL;
2406
2407 size = kgsl_memdesc_footprint(memdesc);
2408
2409 align = 1 << kgsl_memdesc_get_align(memdesc);
2410
2411 if (memdesc->flags & KGSL_MEMFLAGS_FORCE_32BIT) {
2412 start = pt->compat_va_start;
2413 end = pt->compat_va_end;
2414 } else {
2415 start = pt->va_start;
2416 end = pt->va_end;
2417 }
2418
2419 spin_lock(&pagetable->lock);
2420
2421 addr = _get_unmapped_area(pagetable, start, end, size, align);
2422
2423 if (addr == (uint64_t) -ENOMEM) {
2424 ret = -ENOMEM;
2425 goto out;
2426 }
2427
2428 ret = _insert_gpuaddr(pagetable, addr, size);
2429 if (ret == 0) {
2430 memdesc->gpuaddr = addr;
2431 memdesc->pagetable = pagetable;
2432 }
2433
2434out:
2435 spin_unlock(&pagetable->lock);
2436 return ret;
2437}
2438
2439static void kgsl_iommu_put_gpuaddr(struct kgsl_memdesc *memdesc)
2440{
2441 if (memdesc->pagetable == NULL)
2442 return;
2443
2444 spin_lock(&memdesc->pagetable->lock);
2445
2446 _remove_gpuaddr(memdesc->pagetable, memdesc->gpuaddr);
2447
2448 spin_unlock(&memdesc->pagetable->lock);
2449}
2450
2451static int kgsl_iommu_svm_range(struct kgsl_pagetable *pagetable,
2452 uint64_t *lo, uint64_t *hi, uint64_t memflags)
2453{
2454 struct kgsl_iommu_pt *pt = pagetable->priv;
2455 bool gpu_compat = (memflags & KGSL_MEMFLAGS_FORCE_32BIT) != 0;
2456
2457 if (lo != NULL)
2458 *lo = gpu_compat ? pt->compat_va_start : pt->svm_start;
2459 if (hi != NULL)
2460 *hi = gpu_compat ? pt->compat_va_end : pt->svm_end;
2461
2462 return 0;
2463}
2464
2465static bool kgsl_iommu_addr_in_range(struct kgsl_pagetable *pagetable,
2466 uint64_t gpuaddr)
2467{
2468 struct kgsl_iommu_pt *pt = pagetable->priv;
2469
2470 if (gpuaddr == 0)
2471 return false;
2472
2473 if (gpuaddr >= pt->va_start && gpuaddr < pt->va_end)
2474 return true;
2475
2476 if (gpuaddr >= pt->compat_va_start && gpuaddr < pt->compat_va_end)
2477 return true;
2478
2479 if (gpuaddr >= pt->svm_start && gpuaddr < pt->svm_end)
2480 return true;
2481
2482 return false;
2483}
2484
2485static const struct {
2486 int id;
2487 char *name;
2488} kgsl_iommu_cbs[] = {
2489 { KGSL_IOMMU_CONTEXT_USER, "gfx3d_user", },
2490 { KGSL_IOMMU_CONTEXT_SECURE, "gfx3d_secure" },
2491};
2492
2493static int _kgsl_iommu_cb_probe(struct kgsl_device *device,
2494 struct kgsl_iommu *iommu, struct device_node *node)
2495{
2496 struct platform_device *pdev = of_find_device_by_node(node);
2497 struct kgsl_iommu_context *ctx = NULL;
2498 int i;
2499
2500 for (i = 0; i < ARRAY_SIZE(kgsl_iommu_cbs); i++) {
2501 if (!strcmp(node->name, kgsl_iommu_cbs[i].name)) {
2502 int id = kgsl_iommu_cbs[i].id;
2503
2504 ctx = &iommu->ctx[id];
2505 ctx->id = id;
2506 ctx->cb_num = -1;
2507 ctx->name = kgsl_iommu_cbs[i].name;
2508
2509 break;
2510 }
2511 }
2512
2513 if (ctx == NULL) {
2514 KGSL_CORE_ERR("dt: Unknown context label %s\n", node->name);
2515 return -EINVAL;
2516 }
2517
2518 if (ctx->id == KGSL_IOMMU_CONTEXT_SECURE)
2519 device->mmu.secured = true;
2520
2521 /* this property won't be found for all context banks */
2522 if (of_property_read_u32(node, "qcom,gpu-offset", &ctx->gpu_offset))
2523 ctx->gpu_offset = UINT_MAX;
2524
2525 ctx->kgsldev = device;
2526
2527 /* arm-smmu driver we'll have the right device pointer here. */
2528 if (of_find_property(node, "iommus", NULL)) {
2529 ctx->dev = &pdev->dev;
2530 } else {
2531 ctx->dev = kgsl_mmu_get_ctx(ctx->name);
2532
2533 if (IS_ERR(ctx->dev))
2534 return PTR_ERR(ctx->dev);
2535 }
2536
2537 return 0;
2538}
2539
2540static const struct {
2541 char *feature;
2542 int bit;
2543} kgsl_iommu_features[] = {
2544 { "qcom,retention", KGSL_MMU_RETENTION },
2545 { "qcom,global_pt", KGSL_MMU_GLOBAL_PAGETABLE },
2546 { "qcom,hyp_secure_alloc", KGSL_MMU_HYP_SECURE_ALLOC },
2547 { "qcom,force-32bit", KGSL_MMU_FORCE_32BIT },
2548};
2549
2550static int _kgsl_iommu_probe(struct kgsl_device *device,
2551 struct device_node *node)
2552{
2553 const char *cname;
2554 struct property *prop;
2555 u32 reg_val[2];
2556 int i = 0;
2557 struct kgsl_iommu *iommu = KGSL_IOMMU_PRIV(device);
2558 struct device_node *child;
2559 struct platform_device *pdev = of_find_device_by_node(node);
2560
2561 memset(iommu, 0, sizeof(*iommu));
2562
2563 if (of_device_is_compatible(node, "qcom,kgsl-smmu-v1"))
2564 iommu->version = 1;
2565 else
2566 iommu->version = 2;
2567
2568 if (of_property_read_u32_array(node, "reg", reg_val, 2)) {
2569 KGSL_CORE_ERR("dt: Unable to read KGSL IOMMU register range\n");
2570 return -EINVAL;
2571 }
2572 iommu->regstart = reg_val[0];
2573 iommu->regsize = reg_val[1];
2574
2575 /* Protecting the SMMU registers is mandatory */
2576 if (of_property_read_u32_array(node, "qcom,protect", reg_val, 2)) {
2577 KGSL_CORE_ERR("dt: no iommu protection range specified\n");
2578 return -EINVAL;
2579 }
2580 iommu->protect.base = reg_val[0] / sizeof(u32);
2581 iommu->protect.range = ilog2(reg_val[1] / sizeof(u32));
2582
2583 of_property_for_each_string(node, "clock-names", prop, cname) {
2584 struct clk *c = devm_clk_get(&pdev->dev, cname);
2585
2586 if (IS_ERR(c)) {
2587 KGSL_CORE_ERR("dt: Couldn't get clock: %s\n", cname);
2588 return -ENODEV;
2589 }
2590 if (i >= KGSL_IOMMU_MAX_CLKS) {
2591 KGSL_CORE_ERR("dt: too many clocks defined.\n");
2592 return -EINVAL;
2593 }
2594
2595 iommu->clks[i] = c;
2596 ++i;
2597 }
2598
2599 for (i = 0; i < ARRAY_SIZE(kgsl_iommu_features); i++) {
2600 if (of_property_read_bool(node, kgsl_iommu_features[i].feature))
2601 device->mmu.features |= kgsl_iommu_features[i].bit;
2602 }
2603
2604 if (of_property_read_u32(node, "qcom,micro-mmu-control",
2605 &iommu->micro_mmu_ctrl))
2606 iommu->micro_mmu_ctrl = UINT_MAX;
2607
2608 if (of_property_read_u32(node, "qcom,secure_align_mask",
2609 &device->mmu.secure_align_mask))
2610 device->mmu.secure_align_mask = 0xfff;
2611
2612 /* Fill out the rest of the devices in the node */
2613 of_platform_populate(node, NULL, NULL, &pdev->dev);
2614
2615 for_each_child_of_node(node, child) {
2616 int ret;
2617
2618 if (!of_device_is_compatible(child, "qcom,smmu-kgsl-cb"))
2619 continue;
2620
2621 ret = _kgsl_iommu_cb_probe(device, iommu, child);
2622 if (ret)
2623 return ret;
2624 }
2625
2626 return 0;
2627}
2628
2629static const struct {
2630 char *compat;
2631 int (*probe)(struct kgsl_device *device, struct device_node *node);
2632} kgsl_dt_devices[] = {
2633 { "qcom,kgsl-smmu-v1", _kgsl_iommu_probe },
2634 { "qcom,kgsl-smmu-v2", _kgsl_iommu_probe },
2635};
2636
2637static int kgsl_iommu_probe(struct kgsl_device *device)
2638{
2639 int i;
2640
2641 for (i = 0; i < ARRAY_SIZE(kgsl_dt_devices); i++) {
2642 struct device_node *node;
2643
2644 node = of_find_compatible_node(device->pdev->dev.of_node,
2645 NULL, kgsl_dt_devices[i].compat);
2646
2647 if (node != NULL)
2648 return kgsl_dt_devices[i].probe(device, node);
2649 }
2650
2651 return -ENODEV;
2652}
2653
2654struct kgsl_mmu_ops kgsl_iommu_ops = {
2655 .mmu_init = kgsl_iommu_init,
2656 .mmu_close = kgsl_iommu_close,
2657 .mmu_start = kgsl_iommu_start,
2658 .mmu_stop = kgsl_iommu_stop,
2659 .mmu_set_pt = kgsl_iommu_set_pt,
2660 .mmu_clear_fsr = kgsl_iommu_clear_fsr,
2661 .mmu_get_current_ttbr0 = kgsl_iommu_get_current_ttbr0,
2662 .mmu_enable_clk = kgsl_iommu_enable_clk,
2663 .mmu_disable_clk = kgsl_iommu_disable_clk,
2664 .mmu_get_reg_ahbaddr = kgsl_iommu_get_reg_ahbaddr,
2665 .mmu_pt_equal = kgsl_iommu_pt_equal,
2666 .mmu_set_pf_policy = kgsl_iommu_set_pf_policy,
2667 .mmu_pagefault_resume = kgsl_iommu_pagefault_resume,
2668 .mmu_get_prot_regs = kgsl_iommu_get_prot_regs,
2669 .mmu_init_pt = kgsl_iommu_init_pt,
2670 .mmu_add_global = kgsl_iommu_add_global,
2671 .mmu_remove_global = kgsl_iommu_remove_global,
2672 .mmu_getpagetable = kgsl_iommu_getpagetable,
2673 .mmu_get_qdss_global_entry = kgsl_iommu_get_qdss_global_entry,
2674 .probe = kgsl_iommu_probe,
2675};
2676
2677static struct kgsl_mmu_pt_ops iommu_pt_ops = {
2678 .mmu_map = kgsl_iommu_map,
2679 .mmu_unmap = kgsl_iommu_unmap,
2680 .mmu_destroy_pagetable = kgsl_iommu_destroy_pagetable,
2681 .get_ttbr0 = kgsl_iommu_get_ttbr0,
2682 .get_contextidr = kgsl_iommu_get_contextidr,
2683 .get_gpuaddr = kgsl_iommu_get_gpuaddr,
2684 .put_gpuaddr = kgsl_iommu_put_gpuaddr,
2685 .set_svm_region = kgsl_iommu_set_svm_region,
2686 .find_svm_region = kgsl_iommu_find_svm_region,
2687 .svm_range = kgsl_iommu_svm_range,
2688 .addr_in_range = kgsl_iommu_addr_in_range,
2689 .mmu_map_offset = kgsl_iommu_map_offset,
2690 .mmu_unmap_offset = kgsl_iommu_unmap_offset,
2691 .mmu_sparse_dummy_map = kgsl_iommu_sparse_dummy_map,
2692};