blob: 86d4d618f2bc749a7093b904f4afe5c4ce945aca [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;
Lynus Vaz1fde74d2017-03-20 18:02:47 +0530800 struct adreno_gpudev *gpudev;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700801 unsigned int no_page_fault_log = 0;
802 unsigned int curr_context_id = 0;
803 struct kgsl_context *context;
804 char *fault_type = "unknown";
805
806 static DEFINE_RATELIMIT_STATE(_rs,
807 DEFAULT_RATELIMIT_INTERVAL,
808 DEFAULT_RATELIMIT_BURST);
809
810 if (mmu == NULL)
811 return ret;
812
813 iommu = _IOMMU_PRIV(mmu);
814 ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
815 device = KGSL_MMU_DEVICE(mmu);
816 adreno_dev = ADRENO_DEVICE(device);
Lynus Vaz1fde74d2017-03-20 18:02:47 +0530817 gpudev = ADRENO_GPU_DEVICE(adreno_dev);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700818
819 if (pt->name == KGSL_MMU_SECURE_PT)
820 ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_SECURE];
821
822 /*
823 * set the fault bits and stuff before any printks so that if fault
824 * handler runs then it will know it's dealing with a pagefault.
825 * Read the global current timestamp because we could be in middle of
826 * RB switch and hence the cur RB may not be reliable but global
827 * one will always be reliable
828 */
829 kgsl_sharedmem_readl(&device->memstore, &curr_context_id,
830 KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL, current_context));
831
832 context = kgsl_context_get(device, curr_context_id);
833
834 write = (flags & IOMMU_FAULT_WRITE) ? 1 : 0;
835 if (flags & IOMMU_FAULT_TRANSLATION)
836 fault_type = "translation";
837 else if (flags & IOMMU_FAULT_PERMISSION)
838 fault_type = "permission";
839
840 if (kgsl_iommu_suppress_pagefault(addr, write, context)) {
841 iommu->pagefault_suppression_count++;
842 kgsl_context_put(context);
843 return ret;
844 }
845
846 if (context != NULL) {
847 /* save pagefault timestamp for GFT */
848 set_bit(KGSL_CONTEXT_PRIV_PAGEFAULT, &context->priv);
849 tid = context->tid;
850 }
851
852 ctx->fault = 1;
853
854 if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE,
855 &adreno_dev->ft_pf_policy) &&
856 (flags & IOMMU_FAULT_TRANSACTION_STALLED)) {
857 /*
858 * Turn off GPU IRQ so we don't get faults from it too.
859 * The device mutex must be held to change power state
860 */
861 mutex_lock(&device->mutex);
862 kgsl_pwrctrl_change_state(device, KGSL_STATE_AWARE);
863 mutex_unlock(&device->mutex);
864 }
865
866 ptbase = KGSL_IOMMU_GET_CTX_REG_Q(ctx, TTBR0);
867 contextidr = KGSL_IOMMU_GET_CTX_REG(ctx, CONTEXTIDR);
868
869 ptname = MMU_FEATURE(mmu, KGSL_MMU_GLOBAL_PAGETABLE) ?
870 KGSL_MMU_GLOBAL_PT : tid;
Sunil Khatri86e95682017-01-23 17:10:32 +0530871 /*
872 * Trace needs to be logged before searching the faulting
873 * address in free list as it takes quite long time in
874 * search and delays the trace unnecessarily.
875 */
876 trace_kgsl_mmu_pagefault(ctx->kgsldev, addr,
877 ptname, write ? "write" : "read");
Shrenuj Bansala419c792016-10-20 14:05:11 -0700878
879 if (test_bit(KGSL_FT_PAGEFAULT_LOG_ONE_PER_PAGE,
880 &adreno_dev->ft_pf_policy))
881 no_page_fault_log = kgsl_mmu_log_fault_addr(mmu, ptbase, addr);
882
883 if (!no_page_fault_log && __ratelimit(&_rs)) {
884 KGSL_MEM_CRIT(ctx->kgsldev,
885 "GPU PAGE FAULT: addr = %lX pid= %d\n", addr, ptname);
886 KGSL_MEM_CRIT(ctx->kgsldev,
887 "context=%s TTBR0=0x%llx CIDR=0x%x (%s %s fault)\n",
888 ctx->name, ptbase, contextidr,
889 write ? "write" : "read", fault_type);
890
Lynus Vaz1fde74d2017-03-20 18:02:47 +0530891 if (gpudev->iommu_fault_block) {
892 unsigned int fsynr1;
893
894 fsynr1 = KGSL_IOMMU_GET_CTX_REG(ctx, FSYNR1);
895 KGSL_MEM_CRIT(ctx->kgsldev,
896 "FAULTING BLOCK: %s\n",
897 gpudev->iommu_fault_block(adreno_dev,
898 fsynr1));
899 }
900
Shrenuj Bansala419c792016-10-20 14:05:11 -0700901 /* Don't print the debug if this is a permissions fault */
902 if (!(flags & IOMMU_FAULT_PERMISSION)) {
903 _check_if_freed(ctx, addr, ptname);
904
905 KGSL_LOG_DUMP(ctx->kgsldev,
906 "---- nearby memory ----\n");
907
908 _find_mem_entries(mmu, addr, &prev, &next, context);
909 if (prev.gpuaddr)
910 _print_entry(ctx->kgsldev, &prev);
911 else
912 KGSL_LOG_DUMP(ctx->kgsldev, "*EMPTY*\n");
913
914 KGSL_LOG_DUMP(ctx->kgsldev, " <- fault @ %8.8lX\n",
915 addr);
916
917 if (next.gpuaddr != (uint64_t) -1)
918 _print_entry(ctx->kgsldev, &next);
919 else
920 KGSL_LOG_DUMP(ctx->kgsldev, "*EMPTY*\n");
921 }
922 }
923
Shrenuj Bansala419c792016-10-20 14:05:11 -0700924
925 /*
926 * We do not want the h/w to resume fetching data from an iommu
927 * that has faulted, this is better for debugging as it will stall
928 * the GPU and trigger a snapshot. Return EBUSY error.
929 */
930 if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE,
931 &adreno_dev->ft_pf_policy) &&
932 (flags & IOMMU_FAULT_TRANSACTION_STALLED)) {
933 uint32_t sctlr_val;
934
935 ret = -EBUSY;
936 /*
937 * Disable context fault interrupts
938 * as we do not clear FSR in the ISR.
939 * Will be re-enabled after FSR is cleared.
940 */
941 sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR);
942 sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_CFIE_SHIFT);
943 KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val);
944
945 adreno_set_gpu_fault(adreno_dev, ADRENO_IOMMU_PAGE_FAULT);
946 /* Go ahead with recovery*/
947 adreno_dispatcher_schedule(device);
948 }
949
950 kgsl_context_put(context);
951 return ret;
952}
953
954/*
955 * kgsl_iommu_disable_clk() - Disable iommu clocks
956 * Disable IOMMU clocks
957 */
958static void kgsl_iommu_disable_clk(struct kgsl_mmu *mmu)
959{
960 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
961 int j;
962
963 atomic_dec(&iommu->clk_enable_count);
964
965 /*
966 * Make sure the clk refcounts are good. An unbalance may
967 * cause the clocks to be off when we need them on.
968 */
969 WARN_ON(atomic_read(&iommu->clk_enable_count) < 0);
970
971 for (j = (KGSL_IOMMU_MAX_CLKS - 1); j >= 0; j--)
972 if (iommu->clks[j])
973 clk_disable_unprepare(iommu->clks[j]);
974}
975
976/*
977 * kgsl_iommu_enable_clk_prepare_enable - Enable the specified IOMMU clock
978 * Try 4 times to enable it and then BUG() for debug
979 */
980static void kgsl_iommu_clk_prepare_enable(struct clk *clk)
981{
982 int num_retries = 4;
983
984 while (num_retries--) {
985 if (!clk_prepare_enable(clk))
986 return;
987 }
988
989 /* Failure is fatal so BUG() to facilitate debug */
990 KGSL_CORE_ERR("IOMMU clock enable failed\n");
991 BUG();
992}
993
994/*
995 * kgsl_iommu_enable_clk - Enable iommu clocks
996 * Enable all the IOMMU clocks
997 */
998static void kgsl_iommu_enable_clk(struct kgsl_mmu *mmu)
999{
1000 int j;
1001 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1002
1003 for (j = 0; j < KGSL_IOMMU_MAX_CLKS; j++) {
1004 if (iommu->clks[j])
1005 kgsl_iommu_clk_prepare_enable(iommu->clks[j]);
1006 }
1007 atomic_inc(&iommu->clk_enable_count);
1008}
1009
1010/* kgsl_iommu_get_ttbr0 - Get TTBR0 setting for a pagetable */
1011static u64 kgsl_iommu_get_ttbr0(struct kgsl_pagetable *pt)
1012{
1013 struct kgsl_iommu_pt *iommu_pt = pt ? pt->priv : NULL;
1014
1015 BUG_ON(iommu_pt == NULL);
1016
1017 return iommu_pt->ttbr0;
1018}
1019
1020static bool kgsl_iommu_pt_equal(struct kgsl_mmu *mmu,
1021 struct kgsl_pagetable *pt,
1022 u64 ttbr0)
1023{
1024 struct kgsl_iommu_pt *iommu_pt = pt ? pt->priv : NULL;
1025 u64 domain_ttbr0;
1026
1027 if (iommu_pt == NULL)
1028 return 0;
1029
1030 domain_ttbr0 = kgsl_iommu_get_ttbr0(pt);
1031
1032 return (domain_ttbr0 == ttbr0);
1033}
1034
1035/* kgsl_iommu_get_contextidr - query CONTEXTIDR setting for a pagetable */
1036static u32 kgsl_iommu_get_contextidr(struct kgsl_pagetable *pt)
1037{
1038 struct kgsl_iommu_pt *iommu_pt = pt ? pt->priv : NULL;
1039
1040 BUG_ON(iommu_pt == NULL);
1041
1042 return iommu_pt->contextidr;
1043}
1044
1045/*
1046 * kgsl_iommu_destroy_pagetable - Free up reaources help by a pagetable
1047 * @mmu_specific_pt - Pointer to pagetable which is to be freed
1048 *
1049 * Return - void
1050 */
1051static void kgsl_iommu_destroy_pagetable(struct kgsl_pagetable *pt)
1052{
1053 struct kgsl_iommu_pt *iommu_pt = pt->priv;
1054 struct kgsl_mmu *mmu = pt->mmu;
1055 struct kgsl_iommu *iommu;
1056 struct kgsl_iommu_context *ctx;
1057
1058 /*
1059 * Make sure all allocations are unmapped before destroying
1060 * the pagetable
1061 */
1062 WARN_ON(!list_empty(&pt->list));
1063
1064 iommu = _IOMMU_PRIV(mmu);
1065
1066 if (pt->name == KGSL_MMU_SECURE_PT) {
1067 ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_SECURE];
1068 kgsl_iommu_unmap_global_secure_pt_entry(pt);
1069 } else {
1070 ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
1071 kgsl_iommu_unmap_globals(pt);
1072 }
1073
1074 if (iommu_pt->domain) {
1075 trace_kgsl_pagetable_destroy(iommu_pt->ttbr0, pt->name);
1076
1077 _detach_pt(iommu_pt, ctx);
1078
1079 iommu_domain_free(iommu_pt->domain);
1080 }
1081
1082 kfree(iommu_pt);
1083}
1084
1085static void setup_64bit_pagetable(struct kgsl_mmu *mmu,
1086 struct kgsl_pagetable *pagetable,
1087 struct kgsl_iommu_pt *pt)
1088{
1089 unsigned int secure_global_size = kgsl_global_secure_pt_entry != NULL ?
1090 kgsl_global_secure_pt_entry->size : 0;
1091 if (mmu->secured && pagetable->name == KGSL_MMU_SECURE_PT) {
1092 pt->compat_va_start = KGSL_IOMMU_SECURE_BASE +
1093 secure_global_size;
1094 pt->compat_va_end = KGSL_IOMMU_SECURE_END;
1095 pt->va_start = KGSL_IOMMU_SECURE_BASE + secure_global_size;
1096 pt->va_end = KGSL_IOMMU_SECURE_END;
1097 } else {
1098 pt->compat_va_start = KGSL_IOMMU_SVM_BASE32;
1099 pt->compat_va_end = KGSL_IOMMU_SVM_END32;
1100 pt->va_start = KGSL_IOMMU_VA_BASE64;
1101 pt->va_end = KGSL_IOMMU_VA_END64;
1102 }
1103
1104 if (pagetable->name != KGSL_MMU_GLOBAL_PT &&
1105 pagetable->name != KGSL_MMU_SECURE_PT) {
1106 if ((BITS_PER_LONG == 32) || is_compat_task()) {
1107 pt->svm_start = KGSL_IOMMU_SVM_BASE32;
1108 pt->svm_end = KGSL_IOMMU_SVM_END32;
1109 } else {
1110 pt->svm_start = KGSL_IOMMU_SVM_BASE64;
1111 pt->svm_end = KGSL_IOMMU_SVM_END64;
1112 }
1113 }
1114}
1115
1116static void setup_32bit_pagetable(struct kgsl_mmu *mmu,
1117 struct kgsl_pagetable *pagetable,
1118 struct kgsl_iommu_pt *pt)
1119{
1120 unsigned int secure_global_size = kgsl_global_secure_pt_entry != NULL ?
1121 kgsl_global_secure_pt_entry->size : 0;
1122 if (mmu->secured) {
1123 if (pagetable->name == KGSL_MMU_SECURE_PT) {
1124 pt->compat_va_start = KGSL_IOMMU_SECURE_BASE +
1125 secure_global_size;
1126 pt->compat_va_end = KGSL_IOMMU_SECURE_END;
1127 pt->va_start = KGSL_IOMMU_SECURE_BASE +
1128 secure_global_size;
1129 pt->va_end = KGSL_IOMMU_SECURE_END;
1130 } else {
1131 pt->va_start = KGSL_IOMMU_SVM_BASE32;
1132 pt->va_end = KGSL_IOMMU_SECURE_BASE +
1133 secure_global_size;
1134 pt->compat_va_start = pt->va_start;
1135 pt->compat_va_end = pt->va_end;
1136 }
1137 } else {
1138 pt->va_start = KGSL_IOMMU_SVM_BASE32;
1139 pt->va_end = KGSL_IOMMU_GLOBAL_MEM_BASE;
1140 pt->compat_va_start = pt->va_start;
1141 pt->compat_va_end = pt->va_end;
1142 }
1143
1144 if (pagetable->name != KGSL_MMU_GLOBAL_PT &&
1145 pagetable->name != KGSL_MMU_SECURE_PT) {
1146 pt->svm_start = KGSL_IOMMU_SVM_BASE32;
1147 pt->svm_end = KGSL_IOMMU_SVM_END32;
1148 }
1149}
1150
1151
1152static struct kgsl_iommu_pt *
1153_alloc_pt(struct device *dev, struct kgsl_mmu *mmu, struct kgsl_pagetable *pt)
1154{
1155 struct kgsl_iommu_pt *iommu_pt;
1156 struct bus_type *bus = kgsl_mmu_get_bus(dev);
1157
1158 if (bus == NULL)
1159 return ERR_PTR(-ENODEV);
1160
1161 iommu_pt = kzalloc(sizeof(struct kgsl_iommu_pt), GFP_KERNEL);
1162 if (iommu_pt == NULL)
1163 return ERR_PTR(-ENOMEM);
1164
1165 iommu_pt->domain = iommu_domain_alloc(bus);
1166 if (iommu_pt->domain == NULL) {
1167 kfree(iommu_pt);
1168 return ERR_PTR(-ENODEV);
1169 }
1170
1171 pt->pt_ops = &iommu_pt_ops;
1172 pt->priv = iommu_pt;
1173 pt->fault_addr = ~0ULL;
1174 iommu_pt->rbtree = RB_ROOT;
1175
1176 if (MMU_FEATURE(mmu, KGSL_MMU_64BIT))
1177 setup_64bit_pagetable(mmu, pt, iommu_pt);
1178 else
1179 setup_32bit_pagetable(mmu, pt, iommu_pt);
1180
1181
1182 return iommu_pt;
1183}
1184
1185static void _free_pt(struct kgsl_iommu_context *ctx, struct kgsl_pagetable *pt)
1186{
1187 struct kgsl_iommu_pt *iommu_pt = pt->priv;
1188
1189 pt->pt_ops = NULL;
1190 pt->priv = NULL;
1191
1192 if (iommu_pt == NULL)
1193 return;
1194
1195 _detach_pt(iommu_pt, ctx);
1196
1197 if (iommu_pt->domain != NULL)
1198 iommu_domain_free(iommu_pt->domain);
1199 kfree(iommu_pt);
1200}
1201
Sushmita Susheelendra906564d2017-01-10 15:53:55 -07001202void _enable_gpuhtw_llc(struct kgsl_mmu *mmu, struct kgsl_iommu_pt *iommu_pt)
1203{
1204 struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
1205 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1206 int gpuhtw_llc_enable = 1;
1207 int ret;
1208
1209 /* GPU pagetable walk LLC slice not enabled */
1210 if (!adreno_dev->gpuhtw_llc_slice)
1211 return;
1212
1213 /* Domain attribute to enable system cache for GPU pagetable walks */
1214 ret = iommu_domain_set_attr(iommu_pt->domain,
1215 DOMAIN_ATTR_USE_UPSTREAM_HINT, &gpuhtw_llc_enable);
1216 /*
1217 * Warn that the system cache will not be used for GPU
1218 * pagetable walks. This is not a fatal error.
1219 */
1220 WARN_ONCE(ret,
1221 "System cache not enabled for GPU pagetable walks: %d\n", ret);
1222}
1223
Shrenuj Bansala419c792016-10-20 14:05:11 -07001224static int _init_global_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt)
1225{
1226 int ret = 0;
1227 struct kgsl_iommu_pt *iommu_pt = NULL;
1228 unsigned int cb_num;
1229 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1230 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
1231
1232 iommu_pt = _alloc_pt(ctx->dev, mmu, pt);
1233
1234 if (IS_ERR(iommu_pt))
1235 return PTR_ERR(iommu_pt);
1236
1237 if (kgsl_mmu_is_perprocess(mmu)) {
1238 ret = iommu_domain_set_attr(iommu_pt->domain,
1239 DOMAIN_ATTR_PROCID, &pt->name);
1240 if (ret) {
1241 KGSL_CORE_ERR("set DOMAIN_ATTR_PROCID failed: %d\n",
1242 ret);
1243 goto done;
1244 }
1245 }
1246
Sushmita Susheelendra906564d2017-01-10 15:53:55 -07001247 _enable_gpuhtw_llc(mmu, iommu_pt);
1248
Shrenuj Bansala419c792016-10-20 14:05:11 -07001249 ret = _attach_pt(iommu_pt, ctx);
1250 if (ret)
1251 goto done;
1252
1253 iommu_set_fault_handler(iommu_pt->domain,
1254 kgsl_iommu_fault_handler, pt);
1255
1256 ret = iommu_domain_get_attr(iommu_pt->domain,
1257 DOMAIN_ATTR_CONTEXT_BANK, &cb_num);
1258 if (ret) {
1259 KGSL_CORE_ERR("get DOMAIN_ATTR_PROCID failed: %d\n",
1260 ret);
1261 goto done;
1262 }
1263
1264 ctx->cb_num = cb_num;
1265 ctx->regbase = iommu->regbase + KGSL_IOMMU_CB0_OFFSET
1266 + (cb_num << KGSL_IOMMU_CB_SHIFT);
1267
1268 ret = iommu_domain_get_attr(iommu_pt->domain,
1269 DOMAIN_ATTR_TTBR0, &iommu_pt->ttbr0);
1270 if (ret) {
1271 KGSL_CORE_ERR("get DOMAIN_ATTR_TTBR0 failed: %d\n",
1272 ret);
1273 goto done;
1274 }
1275 ret = iommu_domain_get_attr(iommu_pt->domain,
1276 DOMAIN_ATTR_CONTEXTIDR, &iommu_pt->contextidr);
1277 if (ret) {
1278 KGSL_CORE_ERR("get DOMAIN_ATTR_CONTEXTIDR failed: %d\n",
1279 ret);
1280 goto done;
1281 }
1282
1283 ret = kgsl_iommu_map_globals(pt);
1284
1285done:
1286 if (ret)
1287 _free_pt(ctx, pt);
1288
1289 return ret;
1290}
1291
1292static int _init_secure_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_SECURE];
1298 int secure_vmid = VMID_CP_PIXEL;
1299 unsigned int cb_num;
1300
1301 if (!mmu->secured)
1302 return -EPERM;
1303
1304 if (!MMU_FEATURE(mmu, KGSL_MMU_HYP_SECURE_ALLOC)) {
1305 if (!kgsl_mmu_bus_secured(ctx->dev))
1306 return -EPERM;
1307 }
1308
1309 iommu_pt = _alloc_pt(ctx->dev, mmu, pt);
1310
1311 if (IS_ERR(iommu_pt))
1312 return PTR_ERR(iommu_pt);
1313
1314 ret = iommu_domain_set_attr(iommu_pt->domain,
1315 DOMAIN_ATTR_SECURE_VMID, &secure_vmid);
1316 if (ret) {
1317 KGSL_CORE_ERR("set DOMAIN_ATTR_SECURE_VMID failed: %d\n", ret);
1318 goto done;
1319 }
1320
Sushmita Susheelendra906564d2017-01-10 15:53:55 -07001321 _enable_gpuhtw_llc(mmu, iommu_pt);
1322
Shrenuj Bansala419c792016-10-20 14:05:11 -07001323 ret = _attach_pt(iommu_pt, ctx);
1324
1325 if (MMU_FEATURE(mmu, KGSL_MMU_HYP_SECURE_ALLOC))
1326 iommu_set_fault_handler(iommu_pt->domain,
1327 kgsl_iommu_fault_handler, pt);
1328
1329 ret = iommu_domain_get_attr(iommu_pt->domain,
1330 DOMAIN_ATTR_CONTEXT_BANK, &cb_num);
1331 if (ret) {
1332 KGSL_CORE_ERR("get DOMAIN_ATTR_PROCID failed: %d\n",
1333 ret);
1334 goto done;
1335 }
1336
1337 ctx->cb_num = cb_num;
1338 ctx->regbase = iommu->regbase + KGSL_IOMMU_CB0_OFFSET
1339 + (cb_num << KGSL_IOMMU_CB_SHIFT);
1340
1341 ret = kgsl_map_global_secure_pt_entry(pt);
1342
1343done:
1344 if (ret)
1345 _free_pt(ctx, pt);
1346 return ret;
1347}
1348
1349static int _init_per_process_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt)
1350{
1351 int ret = 0;
1352 struct kgsl_iommu_pt *iommu_pt = NULL;
1353 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1354 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
1355 int dynamic = 1;
1356 unsigned int cb_num = ctx->cb_num;
1357
1358 iommu_pt = _alloc_pt(ctx->dev, mmu, pt);
1359
1360 if (IS_ERR(iommu_pt))
1361 return PTR_ERR(iommu_pt);
1362
1363 ret = iommu_domain_set_attr(iommu_pt->domain,
1364 DOMAIN_ATTR_DYNAMIC, &dynamic);
1365 if (ret) {
1366 KGSL_CORE_ERR("set DOMAIN_ATTR_DYNAMIC failed: %d\n", ret);
1367 goto done;
1368 }
1369 ret = iommu_domain_set_attr(iommu_pt->domain,
1370 DOMAIN_ATTR_CONTEXT_BANK, &cb_num);
1371 if (ret) {
1372 KGSL_CORE_ERR("set DOMAIN_ATTR_CONTEXT_BANK failed: %d\n", ret);
1373 goto done;
1374 }
1375
1376 ret = iommu_domain_set_attr(iommu_pt->domain,
1377 DOMAIN_ATTR_PROCID, &pt->name);
1378 if (ret) {
1379 KGSL_CORE_ERR("set DOMAIN_ATTR_PROCID failed: %d\n", ret);
1380 goto done;
1381 }
1382
Sushmita Susheelendra906564d2017-01-10 15:53:55 -07001383 _enable_gpuhtw_llc(mmu, iommu_pt);
1384
Shrenuj Bansala419c792016-10-20 14:05:11 -07001385 ret = _attach_pt(iommu_pt, ctx);
1386 if (ret)
1387 goto done;
1388
1389 /* now read back the attributes needed for self programming */
1390 ret = iommu_domain_get_attr(iommu_pt->domain,
1391 DOMAIN_ATTR_TTBR0, &iommu_pt->ttbr0);
1392 if (ret) {
1393 KGSL_CORE_ERR("get DOMAIN_ATTR_TTBR0 failed: %d\n", ret);
1394 goto done;
1395 }
1396
1397 ret = iommu_domain_get_attr(iommu_pt->domain,
1398 DOMAIN_ATTR_CONTEXTIDR, &iommu_pt->contextidr);
1399 if (ret) {
1400 KGSL_CORE_ERR("get DOMAIN_ATTR_CONTEXTIDR failed: %d\n", ret);
1401 goto done;
1402 }
1403
1404 ret = kgsl_iommu_map_globals(pt);
1405
1406done:
1407 if (ret)
1408 _free_pt(ctx, pt);
1409
1410 return ret;
1411}
1412
1413/* kgsl_iommu_init_pt - Set up an IOMMU pagetable */
1414static int kgsl_iommu_init_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt)
1415{
1416 if (pt == NULL)
1417 return -EINVAL;
1418
1419 switch (pt->name) {
1420 case KGSL_MMU_GLOBAL_PT:
1421 return _init_global_pt(mmu, pt);
1422
1423 case KGSL_MMU_SECURE_PT:
1424 return _init_secure_pt(mmu, pt);
1425
1426 default:
1427 return _init_per_process_pt(mmu, pt);
1428 }
1429}
1430
1431static struct kgsl_pagetable *kgsl_iommu_getpagetable(struct kgsl_mmu *mmu,
1432 unsigned long name)
1433{
1434 struct kgsl_pagetable *pt;
1435
1436 if (!kgsl_mmu_is_perprocess(mmu) && (name != KGSL_MMU_SECURE_PT)) {
1437 name = KGSL_MMU_GLOBAL_PT;
1438 if (mmu->defaultpagetable != NULL)
1439 return mmu->defaultpagetable;
1440 }
1441
1442 pt = kgsl_get_pagetable(name);
1443 if (pt == NULL)
1444 pt = kgsl_mmu_createpagetableobject(mmu, name);
1445
1446 return pt;
1447}
1448
1449/*
1450 * kgsl_iommu_get_reg_ahbaddr - Returns the ahb address of the register
1451 * @mmu - Pointer to mmu structure
1452 * @id - The context ID of the IOMMU ctx
1453 * @reg - The register for which address is required
1454 *
1455 * Return - The address of register which can be used in type0 packet
1456 */
1457static unsigned int kgsl_iommu_get_reg_ahbaddr(struct kgsl_mmu *mmu,
1458 int id, unsigned int reg)
1459{
1460 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1461 struct kgsl_iommu_context *ctx = &iommu->ctx[id];
1462
1463 return ctx->gpu_offset + kgsl_iommu_reg_list[reg];
1464}
1465
1466static void _detach_context(struct kgsl_iommu_context *ctx)
1467{
1468 struct kgsl_iommu_pt *iommu_pt;
1469
1470 if (ctx->default_pt == NULL)
1471 return;
1472
1473 iommu_pt = ctx->default_pt->priv;
1474
1475 _detach_pt(iommu_pt, ctx);
1476
1477 ctx->default_pt = NULL;
1478}
1479
1480static void kgsl_iommu_close(struct kgsl_mmu *mmu)
1481{
1482 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1483 int i;
1484
1485 for (i = 0; i < KGSL_IOMMU_CONTEXT_MAX; i++)
1486 _detach_context(&iommu->ctx[i]);
1487
1488 kgsl_mmu_putpagetable(mmu->defaultpagetable);
1489 mmu->defaultpagetable = NULL;
1490
1491 kgsl_mmu_putpagetable(mmu->securepagetable);
1492 mmu->securepagetable = NULL;
1493
1494 if (iommu->regbase != NULL)
1495 iounmap(iommu->regbase);
1496
1497 kgsl_sharedmem_free(&kgsl_secure_guard_page_memdesc);
1498
1499 if (kgsl_guard_page != NULL) {
1500 __free_page(kgsl_guard_page);
1501 kgsl_guard_page = NULL;
1502 }
1503
1504 if (kgsl_dummy_page != NULL) {
1505 __free_page(kgsl_dummy_page);
1506 kgsl_dummy_page = NULL;
1507 }
1508
1509 kgsl_iommu_remove_global(mmu, &iommu->setstate);
1510 kgsl_sharedmem_free(&iommu->setstate);
1511 kgsl_cleanup_qdss_desc(mmu);
Jonathan Wicks4892d8d2017-02-24 16:21:26 -07001512 kgsl_cleanup_qtimer_desc(mmu);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001513}
1514
1515static int _setstate_alloc(struct kgsl_device *device,
1516 struct kgsl_iommu *iommu)
1517{
1518 int ret;
1519
1520 ret = kgsl_sharedmem_alloc_contig(device, &iommu->setstate, PAGE_SIZE);
1521
1522 if (!ret) {
1523 /* Mark the setstate memory as read only */
1524 iommu->setstate.flags |= KGSL_MEMFLAGS_GPUREADONLY;
1525
1526 kgsl_sharedmem_set(device, &iommu->setstate, 0, 0, PAGE_SIZE);
1527 }
1528
1529 return ret;
1530}
1531
1532static int kgsl_iommu_init(struct kgsl_mmu *mmu)
1533{
1534 struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
1535 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1536 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
1537 int status;
1538
1539 mmu->features |= KGSL_MMU_PAGED;
1540
1541 if (ctx->name == NULL) {
1542 KGSL_CORE_ERR("dt: gfx3d0_user context bank not found\n");
1543 return -EINVAL;
1544 }
1545
1546 status = _setstate_alloc(device, iommu);
1547 if (status)
1548 return status;
1549
1550 /* check requirements for per process pagetables */
1551 if (ctx->gpu_offset == UINT_MAX) {
1552 KGSL_CORE_ERR("missing qcom,gpu-offset forces global pt\n");
1553 mmu->features |= KGSL_MMU_GLOBAL_PAGETABLE;
1554 }
1555
1556 if (iommu->version == 1 && iommu->micro_mmu_ctrl == UINT_MAX) {
1557 KGSL_CORE_ERR(
1558 "missing qcom,micro-mmu-control forces global pt\n");
1559 mmu->features |= KGSL_MMU_GLOBAL_PAGETABLE;
1560 }
1561
1562 /* Check to see if we need to do the IOMMU sync dance */
1563 need_iommu_sync = of_property_read_bool(device->pdev->dev.of_node,
1564 "qcom,gpu-quirk-iommu-sync");
1565
1566 iommu->regbase = ioremap(iommu->regstart, iommu->regsize);
1567 if (iommu->regbase == NULL) {
1568 KGSL_CORE_ERR("Could not map IOMMU registers 0x%lx:0x%x\n",
1569 iommu->regstart, iommu->regsize);
1570 status = -ENOMEM;
1571 goto done;
1572 }
1573
1574 if (addr_entry_cache == NULL) {
1575 addr_entry_cache = KMEM_CACHE(kgsl_iommu_addr_entry, 0);
1576 if (addr_entry_cache == NULL) {
1577 status = -ENOMEM;
1578 goto done;
1579 }
1580 }
1581
1582 kgsl_iommu_add_global(mmu, &iommu->setstate, "setstate");
1583 kgsl_setup_qdss_desc(device);
Jonathan Wicks4892d8d2017-02-24 16:21:26 -07001584 kgsl_setup_qtimer_desc(device);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001585
1586done:
1587 if (status)
1588 kgsl_iommu_close(mmu);
1589
1590 return status;
1591}
1592
1593static int _setup_user_context(struct kgsl_mmu *mmu)
1594{
1595 int ret = 0;
1596 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1597 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
1598 struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
1599 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1600 struct kgsl_iommu_pt *iommu_pt = NULL;
1601 unsigned int sctlr_val;
1602
1603 if (mmu->defaultpagetable == NULL) {
1604 mmu->defaultpagetable = kgsl_mmu_getpagetable(mmu,
1605 KGSL_MMU_GLOBAL_PT);
1606 /* if we don't have a default pagetable, nothing will work */
1607 if (IS_ERR(mmu->defaultpagetable)) {
1608 ret = PTR_ERR(mmu->defaultpagetable);
1609 mmu->defaultpagetable = NULL;
1610 return ret;
1611 }
1612 }
1613
1614 iommu_pt = mmu->defaultpagetable->priv;
1615 if (iommu_pt == NULL)
1616 return -ENODEV;
1617
1618 ret = _attach_pt(iommu_pt, ctx);
1619 if (ret)
1620 return ret;
1621
1622 ctx->default_pt = mmu->defaultpagetable;
1623
1624 kgsl_iommu_enable_clk(mmu);
1625
1626 sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR);
1627
1628 /*
1629 * If pagefault policy is GPUHALT_ENABLE,
1630 * 1) Program CFCFG to 1 to enable STALL mode
1631 * 2) Program HUPCF to 0 (Stall or terminate subsequent
1632 * transactions in the presence of an outstanding fault)
1633 * else
1634 * 1) Program CFCFG to 0 to disable STALL mode (0=Terminate)
1635 * 2) Program HUPCF to 1 (Process subsequent transactions
1636 * independently of any outstanding fault)
1637 */
1638
1639 if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE,
1640 &adreno_dev->ft_pf_policy)) {
1641 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
1642 sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
1643 } else {
1644 sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
1645 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
1646 }
1647 KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val);
1648 kgsl_iommu_disable_clk(mmu);
1649
1650 return 0;
1651}
1652
1653static int _setup_secure_context(struct kgsl_mmu *mmu)
1654{
1655 int ret;
1656 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1657 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_SECURE];
1658 unsigned int cb_num;
1659
1660 struct kgsl_iommu_pt *iommu_pt;
1661
1662 if (ctx->dev == NULL || !mmu->secured)
1663 return 0;
1664
1665 if (mmu->securepagetable == NULL) {
1666 mmu->securepagetable = kgsl_mmu_getpagetable(mmu,
1667 KGSL_MMU_SECURE_PT);
1668 if (IS_ERR(mmu->securepagetable)) {
1669 ret = PTR_ERR(mmu->securepagetable);
1670 mmu->securepagetable = NULL;
1671 return ret;
1672 } else if (mmu->securepagetable == NULL) {
1673 return -ENOMEM;
1674 }
1675 }
1676 iommu_pt = mmu->securepagetable->priv;
1677
1678 ret = _attach_pt(iommu_pt, ctx);
1679 if (ret)
1680 goto done;
1681
1682 ctx->default_pt = mmu->securepagetable;
1683
1684 ret = iommu_domain_get_attr(iommu_pt->domain, DOMAIN_ATTR_CONTEXT_BANK,
1685 &cb_num);
1686 if (ret) {
1687 KGSL_CORE_ERR("get CONTEXT_BANK attr, err %d\n", ret);
1688 goto done;
1689 }
1690 ctx->cb_num = cb_num;
1691done:
1692 if (ret)
1693 _detach_context(ctx);
1694 return ret;
1695}
1696
1697static int kgsl_iommu_set_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt);
1698
1699static int kgsl_iommu_start(struct kgsl_mmu *mmu)
1700{
1701 int status;
1702 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
1703
1704 status = _setup_user_context(mmu);
1705 if (status)
1706 return status;
1707
1708 status = _setup_secure_context(mmu);
1709 if (status) {
1710 _detach_context(&iommu->ctx[KGSL_IOMMU_CONTEXT_USER]);
1711 return status;
1712 }
1713
1714 /* Make sure the hardware is programmed to the default pagetable */
1715 return kgsl_iommu_set_pt(mmu, mmu->defaultpagetable);
1716}
1717
1718static int
1719kgsl_iommu_unmap_offset(struct kgsl_pagetable *pt,
1720 struct kgsl_memdesc *memdesc, uint64_t addr,
1721 uint64_t offset, uint64_t size)
1722{
1723 if (size == 0 || (size + offset) > kgsl_memdesc_footprint(memdesc))
1724 return -EINVAL;
1725 /*
1726 * All GPU addresses as assigned are page aligned, but some
1727 * functions perturb the gpuaddr with an offset, so apply the
1728 * mask here to make sure we have the right address.
1729 */
1730
1731 addr = PAGE_ALIGN(addr);
1732 if (addr == 0)
1733 return -EINVAL;
1734
1735 return _iommu_unmap_sync_pc(pt, memdesc, addr + offset, size);
1736}
1737
1738static int
1739kgsl_iommu_unmap(struct kgsl_pagetable *pt, struct kgsl_memdesc *memdesc)
1740{
1741 if (memdesc->size == 0 || memdesc->gpuaddr == 0)
1742 return -EINVAL;
1743
1744 return kgsl_iommu_unmap_offset(pt, memdesc, memdesc->gpuaddr, 0,
1745 kgsl_memdesc_footprint(memdesc));
1746}
1747
1748/**
1749 * _iommu_map_guard_page - Map iommu guard page
1750 * @pt - Pointer to kgsl pagetable structure
1751 * @memdesc - memdesc to add guard page
1752 * @gpuaddr - GPU addr of guard page
1753 * @protflags - flags for mapping
1754 *
1755 * Return 0 on success, error on map fail
1756 */
1757static int _iommu_map_guard_page(struct kgsl_pagetable *pt,
1758 struct kgsl_memdesc *memdesc,
1759 uint64_t gpuaddr,
1760 unsigned int protflags)
1761{
1762 phys_addr_t physaddr;
1763
1764 if (!kgsl_memdesc_has_guard_page(memdesc))
1765 return 0;
1766
1767 /*
1768 * Allocate guard page for secure buffers.
1769 * This has to be done after we attach a smmu pagetable.
1770 * Allocate the guard page when first secure buffer is.
1771 * mapped to save 1MB of memory if CPZ is not used.
1772 */
1773 if (kgsl_memdesc_is_secured(memdesc)) {
1774 struct scatterlist *sg;
1775 unsigned int sgp_size = pt->mmu->secure_align_mask + 1;
1776
1777 if (!kgsl_secure_guard_page_memdesc.sgt) {
1778 if (kgsl_allocate_user(KGSL_MMU_DEVICE(pt->mmu),
1779 &kgsl_secure_guard_page_memdesc,
1780 sgp_size, KGSL_MEMFLAGS_SECURE)) {
1781 KGSL_CORE_ERR(
1782 "Secure guard page alloc failed\n");
1783 return -ENOMEM;
1784 }
1785 }
1786
1787 sg = kgsl_secure_guard_page_memdesc.sgt->sgl;
1788 physaddr = page_to_phys(sg_page(sg));
1789 } else {
1790 if (kgsl_guard_page == NULL) {
1791 kgsl_guard_page = alloc_page(GFP_KERNEL | __GFP_ZERO |
1792 __GFP_NORETRY | __GFP_HIGHMEM);
1793 if (kgsl_guard_page == NULL)
1794 return -ENOMEM;
1795 }
1796
1797 physaddr = page_to_phys(kgsl_guard_page);
1798 }
1799
1800 return _iommu_map_sync_pc(pt, memdesc, gpuaddr, physaddr,
1801 kgsl_memdesc_guard_page_size(memdesc),
1802 protflags & ~IOMMU_WRITE);
1803}
1804
1805static unsigned int _get_protection_flags(struct kgsl_memdesc *memdesc)
1806{
Sushmita Susheelendra7f66cf72016-09-12 11:04:43 -06001807 unsigned int flags = IOMMU_READ | IOMMU_WRITE |
1808 IOMMU_NOEXEC | IOMMU_USE_UPSTREAM_HINT;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001809
1810 if (memdesc->flags & KGSL_MEMFLAGS_GPUREADONLY)
1811 flags &= ~IOMMU_WRITE;
1812
1813 if (memdesc->priv & KGSL_MEMDESC_PRIVILEGED)
1814 flags |= IOMMU_PRIV;
1815
1816 return flags;
1817}
1818
1819static int
1820kgsl_iommu_map(struct kgsl_pagetable *pt,
1821 struct kgsl_memdesc *memdesc)
1822{
1823 int ret;
1824 uint64_t addr = memdesc->gpuaddr;
1825 uint64_t size = memdesc->size;
1826 unsigned int flags = _get_protection_flags(memdesc);
1827 struct sg_table *sgt = NULL;
1828
1829 /*
1830 * For paged memory allocated through kgsl, memdesc->pages is not NULL.
1831 * Allocate sgt here just for its map operation. Contiguous memory
1832 * already has its sgt, so no need to allocate it here.
1833 */
1834 if (memdesc->pages != NULL)
1835 sgt = kgsl_alloc_sgt_from_pages(memdesc);
1836 else
1837 sgt = memdesc->sgt;
1838
1839 if (IS_ERR(sgt))
1840 return PTR_ERR(sgt);
1841
1842 ret = _iommu_map_sg_sync_pc(pt, addr, memdesc, sgt->sgl,
1843 sgt->nents, flags);
1844 if (ret)
1845 goto done;
1846
1847 ret = _iommu_map_guard_page(pt, memdesc, addr + size, flags);
1848 if (ret)
1849 _iommu_unmap_sync_pc(pt, memdesc, addr, size);
1850
1851done:
1852 if (memdesc->pages != NULL)
1853 kgsl_free_sgt(sgt);
1854
1855 return ret;
1856}
1857
1858static int kgsl_iommu_sparse_dummy_map(struct kgsl_pagetable *pt,
1859 struct kgsl_memdesc *memdesc, uint64_t offset, uint64_t size)
1860{
1861 int ret = 0, i;
1862 struct page **pages = NULL;
1863 struct sg_table sgt;
1864 int count = size >> PAGE_SHIFT;
1865
1866 /* verify the offset is within our range */
1867 if (size + offset > memdesc->size)
1868 return -EINVAL;
1869
1870 if (kgsl_dummy_page == NULL) {
1871 kgsl_dummy_page = alloc_page(GFP_KERNEL | __GFP_ZERO |
1872 __GFP_HIGHMEM);
1873 if (kgsl_dummy_page == NULL)
1874 return -ENOMEM;
1875 }
1876
1877 pages = kcalloc(count, sizeof(struct page *), GFP_KERNEL);
1878 if (pages == NULL)
1879 return -ENOMEM;
1880
1881 for (i = 0; i < count; i++)
1882 pages[i] = kgsl_dummy_page;
1883
1884 ret = sg_alloc_table_from_pages(&sgt, pages, count,
1885 0, size, GFP_KERNEL);
1886 if (ret == 0) {
1887 ret = _iommu_map_sg_sync_pc(pt, memdesc->gpuaddr + offset,
1888 memdesc, sgt.sgl, sgt.nents,
1889 IOMMU_READ | IOMMU_NOEXEC);
1890 sg_free_table(&sgt);
1891 }
1892
1893 kfree(pages);
1894
1895 return ret;
1896}
1897
1898static int _map_to_one_page(struct kgsl_pagetable *pt, uint64_t addr,
1899 struct kgsl_memdesc *memdesc, uint64_t physoffset,
1900 uint64_t size, unsigned int map_flags)
1901{
1902 int ret = 0, i;
1903 int pg_sz = kgsl_memdesc_get_pagesize(memdesc);
1904 int count = size >> PAGE_SHIFT;
1905 struct page *page = NULL;
1906 struct page **pages = NULL;
1907 struct sg_page_iter sg_iter;
1908 struct sg_table sgt;
1909
1910 /* Find our physaddr offset addr */
1911 if (memdesc->pages != NULL)
1912 page = memdesc->pages[physoffset >> PAGE_SHIFT];
1913 else {
1914 for_each_sg_page(memdesc->sgt->sgl, &sg_iter,
1915 memdesc->sgt->nents, physoffset >> PAGE_SHIFT) {
1916 page = sg_page_iter_page(&sg_iter);
1917 break;
1918 }
1919 }
1920
1921 if (page == NULL)
1922 return -EINVAL;
1923
1924 pages = kcalloc(count, sizeof(struct page *), GFP_KERNEL);
1925 if (pages == NULL)
1926 return -ENOMEM;
1927
1928 for (i = 0; i < count; i++) {
1929 if (pg_sz != PAGE_SIZE) {
1930 struct page *tmp_page = page;
1931 int j;
1932
1933 for (j = 0; j < 16; j++, tmp_page += PAGE_SIZE)
1934 pages[i++] = tmp_page;
1935 } else
1936 pages[i] = page;
1937 }
1938
1939 ret = sg_alloc_table_from_pages(&sgt, pages, count,
1940 0, size, GFP_KERNEL);
1941 if (ret == 0) {
1942 ret = _iommu_map_sg_sync_pc(pt, addr, memdesc, sgt.sgl,
1943 sgt.nents, map_flags);
1944 sg_free_table(&sgt);
1945 }
1946
1947 kfree(pages);
1948
1949 return ret;
1950}
1951
1952static int kgsl_iommu_map_offset(struct kgsl_pagetable *pt,
1953 uint64_t virtaddr, uint64_t virtoffset,
1954 struct kgsl_memdesc *memdesc, uint64_t physoffset,
1955 uint64_t size, uint64_t feature_flag)
1956{
1957 int pg_sz;
1958 unsigned int protflags = _get_protection_flags(memdesc);
1959 int ret;
1960 struct sg_table *sgt = NULL;
1961
1962 pg_sz = kgsl_memdesc_get_pagesize(memdesc);
1963 if (!IS_ALIGNED(virtaddr | virtoffset | physoffset | size, pg_sz))
1964 return -EINVAL;
1965
1966 if (size == 0)
1967 return -EINVAL;
1968
1969 if (!(feature_flag & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS) &&
1970 size + physoffset > kgsl_memdesc_footprint(memdesc))
1971 return -EINVAL;
1972
1973 /*
1974 * For paged memory allocated through kgsl, memdesc->pages is not NULL.
1975 * Allocate sgt here just for its map operation. Contiguous memory
1976 * already has its sgt, so no need to allocate it here.
1977 */
1978 if (memdesc->pages != NULL)
1979 sgt = kgsl_alloc_sgt_from_pages(memdesc);
1980 else
1981 sgt = memdesc->sgt;
1982
1983 if (IS_ERR(sgt))
1984 return PTR_ERR(sgt);
1985
1986 if (feature_flag & KGSL_SPARSE_BIND_MULTIPLE_TO_PHYS)
1987 ret = _map_to_one_page(pt, virtaddr + virtoffset,
1988 memdesc, physoffset, size, protflags);
1989 else
1990 ret = _iommu_map_sg_offset_sync_pc(pt, virtaddr + virtoffset,
1991 memdesc, sgt->sgl, sgt->nents,
1992 physoffset, size, protflags);
1993
1994 if (memdesc->pages != NULL)
1995 kgsl_free_sgt(sgt);
1996
1997 return ret;
1998}
1999
2000/* This function must be called with context bank attached */
2001static void kgsl_iommu_clear_fsr(struct kgsl_mmu *mmu)
2002{
2003 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2004 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
2005 unsigned int sctlr_val;
2006
2007 if (ctx->default_pt != NULL) {
2008 kgsl_iommu_enable_clk(mmu);
2009 KGSL_IOMMU_SET_CTX_REG(ctx, FSR, 0xffffffff);
2010 /*
2011 * Re-enable context fault interrupts after clearing
2012 * FSR to prevent the interrupt from firing repeatedly
2013 */
2014 sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR);
2015 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFIE_SHIFT);
2016 KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val);
2017 /*
2018 * Make sure the above register writes
2019 * are not reordered across the barrier
2020 * as we use writel_relaxed to write them
2021 */
2022 wmb();
2023 kgsl_iommu_disable_clk(mmu);
2024 }
2025}
2026
2027static void kgsl_iommu_pagefault_resume(struct kgsl_mmu *mmu)
2028{
2029 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2030 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
2031
2032 if (ctx->default_pt != NULL && ctx->fault) {
2033 /*
2034 * Write 1 to RESUME.TnR to terminate the
2035 * stalled transaction.
2036 */
2037 KGSL_IOMMU_SET_CTX_REG(ctx, RESUME, 1);
2038 /*
2039 * Make sure the above register writes
2040 * are not reordered across the barrier
2041 * as we use writel_relaxed to write them
2042 */
2043 wmb();
2044 ctx->fault = 0;
2045 }
2046}
2047
2048static void kgsl_iommu_stop(struct kgsl_mmu *mmu)
2049{
2050 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2051 int i;
2052
2053 /*
2054 * If the iommu supports retention, we don't need
2055 * to detach when stopping.
2056 */
2057 if (!MMU_FEATURE(mmu, KGSL_MMU_RETENTION)) {
2058 for (i = 0; i < KGSL_IOMMU_CONTEXT_MAX; i++)
2059 _detach_context(&iommu->ctx[i]);
2060 }
2061}
2062
2063static u64
2064kgsl_iommu_get_current_ttbr0(struct kgsl_mmu *mmu)
2065{
2066 u64 val;
2067 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2068 /*
2069 * We cannot enable or disable the clocks in interrupt context, this
2070 * function is called from interrupt context if there is an axi error
2071 */
2072 if (in_interrupt())
2073 return 0;
2074
2075 kgsl_iommu_enable_clk(mmu);
2076 val = KGSL_IOMMU_GET_CTX_REG_Q(&iommu->ctx[KGSL_IOMMU_CONTEXT_USER],
2077 TTBR0);
2078 kgsl_iommu_disable_clk(mmu);
2079 return val;
2080}
2081
2082/*
2083 * kgsl_iommu_set_pt - Change the IOMMU pagetable of the primary context bank
2084 * @mmu - Pointer to mmu structure
2085 * @pt - Pagetable to switch to
2086 *
2087 * Set the new pagetable for the IOMMU by doing direct register writes
2088 * to the IOMMU registers through the cpu
2089 *
2090 * Return - void
2091 */
2092static int kgsl_iommu_set_pt(struct kgsl_mmu *mmu, struct kgsl_pagetable *pt)
2093{
2094 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2095 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
2096 uint64_t ttbr0, temp;
2097 unsigned int contextidr;
2098 unsigned long wait_for_flush;
2099
2100 if ((pt != mmu->defaultpagetable) && !kgsl_mmu_is_perprocess(mmu))
2101 return 0;
2102
2103 kgsl_iommu_enable_clk(mmu);
2104
2105 ttbr0 = kgsl_mmu_pagetable_get_ttbr0(pt);
2106 contextidr = kgsl_mmu_pagetable_get_contextidr(pt);
2107
2108 KGSL_IOMMU_SET_CTX_REG_Q(ctx, TTBR0, ttbr0);
2109 KGSL_IOMMU_SET_CTX_REG(ctx, CONTEXTIDR, contextidr);
2110
2111 /* memory barrier before reading TTBR0 register */
2112 mb();
2113 temp = KGSL_IOMMU_GET_CTX_REG_Q(ctx, TTBR0);
2114
2115 KGSL_IOMMU_SET_CTX_REG(ctx, TLBIALL, 1);
2116 /* make sure the TBLI write completes before we wait */
2117 mb();
2118 /*
2119 * Wait for flush to complete by polling the flush
2120 * status bit of TLBSTATUS register for not more than
2121 * 2 s. After 2s just exit, at that point the SMMU h/w
2122 * may be stuck and will eventually cause GPU to hang
2123 * or bring the system down.
2124 */
2125 wait_for_flush = jiffies + msecs_to_jiffies(2000);
2126 KGSL_IOMMU_SET_CTX_REG(ctx, TLBSYNC, 0);
2127 while (KGSL_IOMMU_GET_CTX_REG(ctx, TLBSTATUS) &
2128 (KGSL_IOMMU_CTX_TLBSTATUS_SACTIVE)) {
2129 if (time_after(jiffies, wait_for_flush)) {
2130 KGSL_DRV_WARN(KGSL_MMU_DEVICE(mmu),
2131 "Wait limit reached for IOMMU tlb flush\n");
2132 break;
2133 }
2134 cpu_relax();
2135 }
2136
2137 kgsl_iommu_disable_clk(mmu);
2138 return 0;
2139}
2140
2141/*
2142 * kgsl_iommu_set_pf_policy() - Set the pagefault policy for IOMMU
2143 * @mmu: Pointer to mmu structure
2144 * @pf_policy: The pagefault polict to set
2145 *
2146 * Check if the new policy indicated by pf_policy is same as current
2147 * policy, if same then return else set the policy
2148 */
2149static int kgsl_iommu_set_pf_policy(struct kgsl_mmu *mmu,
2150 unsigned long pf_policy)
2151{
2152 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2153 struct kgsl_iommu_context *ctx = &iommu->ctx[KGSL_IOMMU_CONTEXT_USER];
2154 struct kgsl_device *device = KGSL_MMU_DEVICE(mmu);
2155 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2156
2157 if ((adreno_dev->ft_pf_policy &
2158 BIT(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE)) ==
2159 (pf_policy & BIT(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE)))
2160 return 0;
2161
2162 /* If not attached, policy will be updated during the next attach */
2163 if (ctx->default_pt != NULL) {
2164 unsigned int sctlr_val;
2165
2166 kgsl_iommu_enable_clk(mmu);
2167
2168 sctlr_val = KGSL_IOMMU_GET_CTX_REG(ctx, SCTLR);
2169
2170 if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, &pf_policy)) {
2171 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
2172 sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
2173 } else {
2174 sctlr_val &= ~(0x1 << KGSL_IOMMU_SCTLR_CFCFG_SHIFT);
2175 sctlr_val |= (0x1 << KGSL_IOMMU_SCTLR_HUPCF_SHIFT);
2176 }
2177
2178 KGSL_IOMMU_SET_CTX_REG(ctx, SCTLR, sctlr_val);
2179
2180 kgsl_iommu_disable_clk(mmu);
2181 }
2182
2183 return 0;
2184}
2185
2186static struct kgsl_protected_registers *
2187kgsl_iommu_get_prot_regs(struct kgsl_mmu *mmu)
2188{
2189 struct kgsl_iommu *iommu = _IOMMU_PRIV(mmu);
2190
2191 return &iommu->protect;
2192}
2193
2194static struct kgsl_iommu_addr_entry *_find_gpuaddr(
2195 struct kgsl_pagetable *pagetable, uint64_t gpuaddr)
2196{
2197 struct kgsl_iommu_pt *pt = pagetable->priv;
2198 struct rb_node *node = pt->rbtree.rb_node;
2199
2200 while (node != NULL) {
2201 struct kgsl_iommu_addr_entry *entry = rb_entry(node,
2202 struct kgsl_iommu_addr_entry, node);
2203
2204 if (gpuaddr < entry->base)
2205 node = node->rb_left;
2206 else if (gpuaddr > entry->base)
2207 node = node->rb_right;
2208 else
2209 return entry;
2210 }
2211
2212 return NULL;
2213}
2214
2215static int _remove_gpuaddr(struct kgsl_pagetable *pagetable,
2216 uint64_t gpuaddr)
2217{
2218 struct kgsl_iommu_pt *pt = pagetable->priv;
2219 struct kgsl_iommu_addr_entry *entry;
2220
2221 entry = _find_gpuaddr(pagetable, gpuaddr);
2222
2223 if (entry != NULL) {
2224 rb_erase(&entry->node, &pt->rbtree);
2225 kmem_cache_free(addr_entry_cache, entry);
2226 return 0;
2227 }
2228
2229 WARN(1, "Couldn't remove gpuaddr: 0x%llx\n", gpuaddr);
2230 return -ENOMEM;
2231}
2232
2233static int _insert_gpuaddr(struct kgsl_pagetable *pagetable,
2234 uint64_t gpuaddr, uint64_t size)
2235{
2236 struct kgsl_iommu_pt *pt = pagetable->priv;
2237 struct rb_node **node, *parent = NULL;
2238 struct kgsl_iommu_addr_entry *new =
2239 kmem_cache_alloc(addr_entry_cache, GFP_ATOMIC);
2240
2241 if (new == NULL)
2242 return -ENOMEM;
2243
2244 new->base = gpuaddr;
2245 new->size = size;
2246
2247 node = &pt->rbtree.rb_node;
2248
2249 while (*node != NULL) {
2250 struct kgsl_iommu_addr_entry *this;
2251
2252 parent = *node;
2253 this = rb_entry(parent, struct kgsl_iommu_addr_entry, node);
2254
2255 if (new->base < this->base)
2256 node = &parent->rb_left;
2257 else if (new->base > this->base)
2258 node = &parent->rb_right;
2259 else {
2260 /* Duplicate entry */
2261 WARN(1, "duplicate gpuaddr: 0x%llx\n", gpuaddr);
2262 return -EEXIST;
2263 }
2264 }
2265
2266 rb_link_node(&new->node, parent, node);
2267 rb_insert_color(&new->node, &pt->rbtree);
2268
2269 return 0;
2270}
2271
2272static uint64_t _get_unmapped_area(struct kgsl_pagetable *pagetable,
2273 uint64_t bottom, uint64_t top, uint64_t size,
2274 uint64_t align)
2275{
2276 struct kgsl_iommu_pt *pt = pagetable->priv;
2277 struct rb_node *node = rb_first(&pt->rbtree);
2278 uint64_t start;
2279
2280 bottom = ALIGN(bottom, align);
2281 start = bottom;
2282
2283 while (node != NULL) {
2284 uint64_t gap;
2285 struct kgsl_iommu_addr_entry *entry = rb_entry(node,
2286 struct kgsl_iommu_addr_entry, node);
2287
2288 /*
2289 * Skip any entries that are outside of the range, but make sure
2290 * to account for some that might straddle the lower bound
2291 */
2292 if (entry->base < bottom) {
2293 if (entry->base + entry->size > bottom)
2294 start = ALIGN(entry->base + entry->size, align);
2295 node = rb_next(node);
2296 continue;
2297 }
2298
2299 /* Stop if we went over the top */
2300 if (entry->base >= top)
2301 break;
2302
2303 /* Make sure there is a gap to consider */
2304 if (start < entry->base) {
2305 gap = entry->base - start;
2306
2307 if (gap >= size)
2308 return start;
2309 }
2310
2311 /* Stop if there is no more room in the region */
2312 if (entry->base + entry->size >= top)
2313 return (uint64_t) -ENOMEM;
2314
2315 /* Start the next cycle at the end of the current entry */
2316 start = ALIGN(entry->base + entry->size, align);
2317 node = rb_next(node);
2318 }
2319
2320 if (start + size <= top)
2321 return start;
2322
2323 return (uint64_t) -ENOMEM;
2324}
2325
2326static uint64_t _get_unmapped_area_topdown(struct kgsl_pagetable *pagetable,
2327 uint64_t bottom, uint64_t top, uint64_t size,
2328 uint64_t align)
2329{
2330 struct kgsl_iommu_pt *pt = pagetable->priv;
2331 struct rb_node *node = rb_last(&pt->rbtree);
2332 uint64_t end = top;
2333 uint64_t mask = ~(align - 1);
2334 struct kgsl_iommu_addr_entry *entry;
2335
2336 /* Make sure that the bottom is correctly aligned */
2337 bottom = ALIGN(bottom, align);
2338
2339 /* Make sure the requested size will fit in the range */
2340 if (size > (top - bottom))
2341 return -ENOMEM;
2342
2343 /* Walk back through the list to find the highest entry in the range */
2344 for (node = rb_last(&pt->rbtree); node != NULL; node = rb_prev(node)) {
2345 entry = rb_entry(node, struct kgsl_iommu_addr_entry, node);
2346 if (entry->base < top)
2347 break;
2348 }
2349
2350 while (node != NULL) {
2351 uint64_t offset;
2352
2353 entry = rb_entry(node, struct kgsl_iommu_addr_entry, node);
2354
2355 /* If the entire entry is below the range the search is over */
2356 if ((entry->base + entry->size) < bottom)
2357 break;
2358
2359 /* Get the top of the entry properly aligned */
2360 offset = ALIGN(entry->base + entry->size, align);
2361
2362 /*
2363 * Try to allocate the memory from the top of the gap,
2364 * making sure that it fits between the top of this entry and
2365 * the bottom of the previous one
2366 */
2367
2368 if ((end > size) && (offset < end)) {
2369 uint64_t chunk = (end - size) & mask;
2370
2371 if (chunk >= offset)
2372 return chunk;
2373 }
2374
2375 /*
2376 * If we get here and the current entry is outside of the range
2377 * then we are officially out of room
2378 */
2379
2380 if (entry->base < bottom)
2381 return (uint64_t) -ENOMEM;
2382
2383 /* Set the top of the gap to the current entry->base */
2384 end = entry->base;
2385
2386 /* And move on to the next lower entry */
2387 node = rb_prev(node);
2388 }
2389
2390 /* If we get here then there are no more entries in the region */
2391 if ((end > size) && (((end - size) & mask) >= bottom))
2392 return (end - size) & mask;
2393
2394 return (uint64_t) -ENOMEM;
2395}
2396
2397static uint64_t kgsl_iommu_find_svm_region(struct kgsl_pagetable *pagetable,
2398 uint64_t start, uint64_t end, uint64_t size,
2399 uint64_t alignment)
2400{
2401 uint64_t addr;
2402
2403 /* Avoid black holes */
2404 if (WARN(end <= start, "Bad search range: 0x%llx-0x%llx", start, end))
2405 return (uint64_t) -EINVAL;
2406
2407 spin_lock(&pagetable->lock);
2408 addr = _get_unmapped_area_topdown(pagetable,
2409 start, end, size, alignment);
2410 spin_unlock(&pagetable->lock);
2411 return addr;
2412}
2413
2414static int kgsl_iommu_set_svm_region(struct kgsl_pagetable *pagetable,
2415 uint64_t gpuaddr, uint64_t size)
2416{
2417 int ret = -ENOMEM;
2418 struct kgsl_iommu_pt *pt = pagetable->priv;
2419 struct rb_node *node;
2420
2421 /* Make sure the requested address doesn't fall in the global range */
2422 if (ADDR_IN_GLOBAL(gpuaddr) || ADDR_IN_GLOBAL(gpuaddr + size))
2423 return -ENOMEM;
2424
2425 spin_lock(&pagetable->lock);
2426 node = pt->rbtree.rb_node;
2427
2428 while (node != NULL) {
2429 uint64_t start, end;
2430 struct kgsl_iommu_addr_entry *entry = rb_entry(node,
2431 struct kgsl_iommu_addr_entry, node);
2432
2433 start = entry->base;
2434 end = entry->base + entry->size;
2435
2436 if (gpuaddr + size <= start)
2437 node = node->rb_left;
2438 else if (end <= gpuaddr)
2439 node = node->rb_right;
2440 else
2441 goto out;
2442 }
2443
2444 ret = _insert_gpuaddr(pagetable, gpuaddr, size);
2445out:
2446 spin_unlock(&pagetable->lock);
2447 return ret;
2448}
2449
2450
2451static int kgsl_iommu_get_gpuaddr(struct kgsl_pagetable *pagetable,
2452 struct kgsl_memdesc *memdesc)
2453{
2454 struct kgsl_iommu_pt *pt = pagetable->priv;
2455 int ret = 0;
2456 uint64_t addr, start, end, size;
2457 unsigned int align;
2458
2459 if (WARN_ON(kgsl_memdesc_use_cpu_map(memdesc)))
2460 return -EINVAL;
2461
2462 if (memdesc->flags & KGSL_MEMFLAGS_SECURE &&
2463 pagetable->name != KGSL_MMU_SECURE_PT)
2464 return -EINVAL;
2465
2466 size = kgsl_memdesc_footprint(memdesc);
2467
2468 align = 1 << kgsl_memdesc_get_align(memdesc);
2469
2470 if (memdesc->flags & KGSL_MEMFLAGS_FORCE_32BIT) {
2471 start = pt->compat_va_start;
2472 end = pt->compat_va_end;
2473 } else {
2474 start = pt->va_start;
2475 end = pt->va_end;
2476 }
2477
2478 spin_lock(&pagetable->lock);
2479
2480 addr = _get_unmapped_area(pagetable, start, end, size, align);
2481
2482 if (addr == (uint64_t) -ENOMEM) {
2483 ret = -ENOMEM;
2484 goto out;
2485 }
2486
2487 ret = _insert_gpuaddr(pagetable, addr, size);
2488 if (ret == 0) {
2489 memdesc->gpuaddr = addr;
2490 memdesc->pagetable = pagetable;
2491 }
2492
2493out:
2494 spin_unlock(&pagetable->lock);
2495 return ret;
2496}
2497
2498static void kgsl_iommu_put_gpuaddr(struct kgsl_memdesc *memdesc)
2499{
2500 if (memdesc->pagetable == NULL)
2501 return;
2502
2503 spin_lock(&memdesc->pagetable->lock);
2504
2505 _remove_gpuaddr(memdesc->pagetable, memdesc->gpuaddr);
2506
2507 spin_unlock(&memdesc->pagetable->lock);
2508}
2509
2510static int kgsl_iommu_svm_range(struct kgsl_pagetable *pagetable,
2511 uint64_t *lo, uint64_t *hi, uint64_t memflags)
2512{
2513 struct kgsl_iommu_pt *pt = pagetable->priv;
2514 bool gpu_compat = (memflags & KGSL_MEMFLAGS_FORCE_32BIT) != 0;
2515
2516 if (lo != NULL)
2517 *lo = gpu_compat ? pt->compat_va_start : pt->svm_start;
2518 if (hi != NULL)
2519 *hi = gpu_compat ? pt->compat_va_end : pt->svm_end;
2520
2521 return 0;
2522}
2523
2524static bool kgsl_iommu_addr_in_range(struct kgsl_pagetable *pagetable,
2525 uint64_t gpuaddr)
2526{
2527 struct kgsl_iommu_pt *pt = pagetable->priv;
2528
2529 if (gpuaddr == 0)
2530 return false;
2531
2532 if (gpuaddr >= pt->va_start && gpuaddr < pt->va_end)
2533 return true;
2534
2535 if (gpuaddr >= pt->compat_va_start && gpuaddr < pt->compat_va_end)
2536 return true;
2537
2538 if (gpuaddr >= pt->svm_start && gpuaddr < pt->svm_end)
2539 return true;
2540
2541 return false;
2542}
2543
2544static const struct {
2545 int id;
2546 char *name;
2547} kgsl_iommu_cbs[] = {
2548 { KGSL_IOMMU_CONTEXT_USER, "gfx3d_user", },
2549 { KGSL_IOMMU_CONTEXT_SECURE, "gfx3d_secure" },
2550};
2551
2552static int _kgsl_iommu_cb_probe(struct kgsl_device *device,
2553 struct kgsl_iommu *iommu, struct device_node *node)
2554{
2555 struct platform_device *pdev = of_find_device_by_node(node);
2556 struct kgsl_iommu_context *ctx = NULL;
2557 int i;
2558
2559 for (i = 0; i < ARRAY_SIZE(kgsl_iommu_cbs); i++) {
2560 if (!strcmp(node->name, kgsl_iommu_cbs[i].name)) {
2561 int id = kgsl_iommu_cbs[i].id;
2562
2563 ctx = &iommu->ctx[id];
2564 ctx->id = id;
2565 ctx->cb_num = -1;
2566 ctx->name = kgsl_iommu_cbs[i].name;
2567
2568 break;
2569 }
2570 }
2571
2572 if (ctx == NULL) {
2573 KGSL_CORE_ERR("dt: Unknown context label %s\n", node->name);
2574 return -EINVAL;
2575 }
2576
2577 if (ctx->id == KGSL_IOMMU_CONTEXT_SECURE)
2578 device->mmu.secured = true;
2579
2580 /* this property won't be found for all context banks */
2581 if (of_property_read_u32(node, "qcom,gpu-offset", &ctx->gpu_offset))
2582 ctx->gpu_offset = UINT_MAX;
2583
2584 ctx->kgsldev = device;
2585
2586 /* arm-smmu driver we'll have the right device pointer here. */
2587 if (of_find_property(node, "iommus", NULL)) {
2588 ctx->dev = &pdev->dev;
2589 } else {
2590 ctx->dev = kgsl_mmu_get_ctx(ctx->name);
2591
2592 if (IS_ERR(ctx->dev))
2593 return PTR_ERR(ctx->dev);
2594 }
2595
2596 return 0;
2597}
2598
2599static const struct {
2600 char *feature;
2601 int bit;
2602} kgsl_iommu_features[] = {
2603 { "qcom,retention", KGSL_MMU_RETENTION },
2604 { "qcom,global_pt", KGSL_MMU_GLOBAL_PAGETABLE },
2605 { "qcom,hyp_secure_alloc", KGSL_MMU_HYP_SECURE_ALLOC },
2606 { "qcom,force-32bit", KGSL_MMU_FORCE_32BIT },
2607};
2608
2609static int _kgsl_iommu_probe(struct kgsl_device *device,
2610 struct device_node *node)
2611{
2612 const char *cname;
2613 struct property *prop;
2614 u32 reg_val[2];
2615 int i = 0;
2616 struct kgsl_iommu *iommu = KGSL_IOMMU_PRIV(device);
2617 struct device_node *child;
2618 struct platform_device *pdev = of_find_device_by_node(node);
2619
2620 memset(iommu, 0, sizeof(*iommu));
2621
2622 if (of_device_is_compatible(node, "qcom,kgsl-smmu-v1"))
2623 iommu->version = 1;
2624 else
2625 iommu->version = 2;
2626
2627 if (of_property_read_u32_array(node, "reg", reg_val, 2)) {
2628 KGSL_CORE_ERR("dt: Unable to read KGSL IOMMU register range\n");
2629 return -EINVAL;
2630 }
2631 iommu->regstart = reg_val[0];
2632 iommu->regsize = reg_val[1];
2633
2634 /* Protecting the SMMU registers is mandatory */
2635 if (of_property_read_u32_array(node, "qcom,protect", reg_val, 2)) {
2636 KGSL_CORE_ERR("dt: no iommu protection range specified\n");
2637 return -EINVAL;
2638 }
2639 iommu->protect.base = reg_val[0] / sizeof(u32);
2640 iommu->protect.range = ilog2(reg_val[1] / sizeof(u32));
2641
2642 of_property_for_each_string(node, "clock-names", prop, cname) {
2643 struct clk *c = devm_clk_get(&pdev->dev, cname);
2644
2645 if (IS_ERR(c)) {
2646 KGSL_CORE_ERR("dt: Couldn't get clock: %s\n", cname);
2647 return -ENODEV;
2648 }
2649 if (i >= KGSL_IOMMU_MAX_CLKS) {
2650 KGSL_CORE_ERR("dt: too many clocks defined.\n");
2651 return -EINVAL;
2652 }
2653
2654 iommu->clks[i] = c;
2655 ++i;
2656 }
2657
2658 for (i = 0; i < ARRAY_SIZE(kgsl_iommu_features); i++) {
2659 if (of_property_read_bool(node, kgsl_iommu_features[i].feature))
2660 device->mmu.features |= kgsl_iommu_features[i].bit;
2661 }
2662
2663 if (of_property_read_u32(node, "qcom,micro-mmu-control",
2664 &iommu->micro_mmu_ctrl))
2665 iommu->micro_mmu_ctrl = UINT_MAX;
2666
2667 if (of_property_read_u32(node, "qcom,secure_align_mask",
2668 &device->mmu.secure_align_mask))
2669 device->mmu.secure_align_mask = 0xfff;
2670
2671 /* Fill out the rest of the devices in the node */
2672 of_platform_populate(node, NULL, NULL, &pdev->dev);
2673
2674 for_each_child_of_node(node, child) {
2675 int ret;
2676
2677 if (!of_device_is_compatible(child, "qcom,smmu-kgsl-cb"))
2678 continue;
2679
2680 ret = _kgsl_iommu_cb_probe(device, iommu, child);
2681 if (ret)
2682 return ret;
2683 }
2684
2685 return 0;
2686}
2687
2688static const struct {
2689 char *compat;
2690 int (*probe)(struct kgsl_device *device, struct device_node *node);
2691} kgsl_dt_devices[] = {
2692 { "qcom,kgsl-smmu-v1", _kgsl_iommu_probe },
2693 { "qcom,kgsl-smmu-v2", _kgsl_iommu_probe },
2694};
2695
2696static int kgsl_iommu_probe(struct kgsl_device *device)
2697{
2698 int i;
2699
2700 for (i = 0; i < ARRAY_SIZE(kgsl_dt_devices); i++) {
2701 struct device_node *node;
2702
2703 node = of_find_compatible_node(device->pdev->dev.of_node,
2704 NULL, kgsl_dt_devices[i].compat);
2705
2706 if (node != NULL)
2707 return kgsl_dt_devices[i].probe(device, node);
2708 }
2709
2710 return -ENODEV;
2711}
2712
2713struct kgsl_mmu_ops kgsl_iommu_ops = {
2714 .mmu_init = kgsl_iommu_init,
2715 .mmu_close = kgsl_iommu_close,
2716 .mmu_start = kgsl_iommu_start,
2717 .mmu_stop = kgsl_iommu_stop,
2718 .mmu_set_pt = kgsl_iommu_set_pt,
2719 .mmu_clear_fsr = kgsl_iommu_clear_fsr,
2720 .mmu_get_current_ttbr0 = kgsl_iommu_get_current_ttbr0,
2721 .mmu_enable_clk = kgsl_iommu_enable_clk,
2722 .mmu_disable_clk = kgsl_iommu_disable_clk,
2723 .mmu_get_reg_ahbaddr = kgsl_iommu_get_reg_ahbaddr,
2724 .mmu_pt_equal = kgsl_iommu_pt_equal,
2725 .mmu_set_pf_policy = kgsl_iommu_set_pf_policy,
2726 .mmu_pagefault_resume = kgsl_iommu_pagefault_resume,
2727 .mmu_get_prot_regs = kgsl_iommu_get_prot_regs,
2728 .mmu_init_pt = kgsl_iommu_init_pt,
2729 .mmu_add_global = kgsl_iommu_add_global,
2730 .mmu_remove_global = kgsl_iommu_remove_global,
2731 .mmu_getpagetable = kgsl_iommu_getpagetable,
2732 .mmu_get_qdss_global_entry = kgsl_iommu_get_qdss_global_entry,
Jonathan Wicks4892d8d2017-02-24 16:21:26 -07002733 .mmu_get_qtimer_global_entry = kgsl_iommu_get_qtimer_global_entry,
Shrenuj Bansala419c792016-10-20 14:05:11 -07002734 .probe = kgsl_iommu_probe,
2735};
2736
2737static struct kgsl_mmu_pt_ops iommu_pt_ops = {
2738 .mmu_map = kgsl_iommu_map,
2739 .mmu_unmap = kgsl_iommu_unmap,
2740 .mmu_destroy_pagetable = kgsl_iommu_destroy_pagetable,
2741 .get_ttbr0 = kgsl_iommu_get_ttbr0,
2742 .get_contextidr = kgsl_iommu_get_contextidr,
2743 .get_gpuaddr = kgsl_iommu_get_gpuaddr,
2744 .put_gpuaddr = kgsl_iommu_put_gpuaddr,
2745 .set_svm_region = kgsl_iommu_set_svm_region,
2746 .find_svm_region = kgsl_iommu_find_svm_region,
2747 .svm_range = kgsl_iommu_svm_range,
2748 .addr_in_range = kgsl_iommu_addr_in_range,
2749 .mmu_map_offset = kgsl_iommu_map_offset,
2750 .mmu_unmap_offset = kgsl_iommu_unmap_offset,
2751 .mmu_sparse_dummy_map = kgsl_iommu_sparse_dummy_map,
2752};