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