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