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