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