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