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