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