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