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