]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/etnaviv/etnaviv_iommu_v2.c
043111a1d60c4f9712c96abb427bbcfb4816d2e3
[linux.git] / drivers / gpu / drm / etnaviv / etnaviv_iommu_v2.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2016-2018 Etnaviv Project
4  */
5
6 #include <linux/bitops.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/platform_device.h>
9 #include <linux/sizes.h>
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
12
13 #include "etnaviv_cmdbuf.h"
14 #include "etnaviv_gpu.h"
15 #include "etnaviv_mmu.h"
16 #include "state.xml.h"
17 #include "state_hi.xml.h"
18
19 #define MMUv2_PTE_PRESENT               BIT(0)
20 #define MMUv2_PTE_EXCEPTION             BIT(1)
21 #define MMUv2_PTE_WRITEABLE             BIT(2)
22
23 #define MMUv2_MTLB_MASK                 0xffc00000
24 #define MMUv2_MTLB_SHIFT                22
25 #define MMUv2_STLB_MASK                 0x003ff000
26 #define MMUv2_STLB_SHIFT                12
27
28 #define MMUv2_MAX_STLB_ENTRIES          1024
29
30 struct etnaviv_iommuv2_context {
31         struct etnaviv_iommu_context base;
32         unsigned short id;
33         /* M(aster) TLB aka first level pagetable */
34         u32 *mtlb_cpu;
35         dma_addr_t mtlb_dma;
36         /* S(lave) TLB aka second level pagetable */
37         u32 *stlb_cpu[MMUv2_MAX_STLB_ENTRIES];
38         dma_addr_t stlb_dma[MMUv2_MAX_STLB_ENTRIES];
39 };
40
41 static struct etnaviv_iommuv2_context *
42 to_v2_context(struct etnaviv_iommu_context *context)
43 {
44         return container_of(context, struct etnaviv_iommuv2_context, base);
45 }
46
47 static void etnaviv_iommuv2_free(struct etnaviv_iommu_context *context)
48 {
49         struct etnaviv_iommuv2_context *v2_context = to_v2_context(context);
50         int i;
51
52         drm_mm_takedown(&context->mm);
53
54         for (i = 0; i < MMUv2_MAX_STLB_ENTRIES; i++) {
55                 if (v2_context->stlb_cpu[i])
56                         dma_free_wc(context->global->dev, SZ_4K,
57                                     v2_context->stlb_cpu[i],
58                                     v2_context->stlb_dma[i]);
59         }
60
61         dma_free_wc(context->global->dev, SZ_4K, v2_context->mtlb_cpu,
62                     v2_context->mtlb_dma);
63
64         clear_bit(v2_context->id, context->global->v2.pta_alloc);
65
66         vfree(v2_context);
67 }
68 static int
69 etnaviv_iommuv2_ensure_stlb(struct etnaviv_iommuv2_context *v2_context,
70                             int stlb)
71 {
72         if (v2_context->stlb_cpu[stlb])
73                 return 0;
74
75         v2_context->stlb_cpu[stlb] =
76                         dma_alloc_wc(v2_context->base.global->dev, SZ_4K,
77                                      &v2_context->stlb_dma[stlb],
78                                      GFP_KERNEL);
79
80         if (!v2_context->stlb_cpu[stlb])
81                 return -ENOMEM;
82
83         memset32(v2_context->stlb_cpu[stlb], MMUv2_PTE_EXCEPTION,
84                  SZ_4K / sizeof(u32));
85
86         v2_context->mtlb_cpu[stlb] =
87                         v2_context->stlb_dma[stlb] | MMUv2_PTE_PRESENT;
88
89         return 0;
90 }
91
92 static int etnaviv_iommuv2_map(struct etnaviv_iommu_context *context,
93                                unsigned long iova, phys_addr_t paddr,
94                                size_t size, int prot)
95 {
96         struct etnaviv_iommuv2_context *v2_context = to_v2_context(context);
97         int mtlb_entry, stlb_entry, ret;
98         u32 entry = lower_32_bits(paddr) | MMUv2_PTE_PRESENT;
99
100         if (size != SZ_4K)
101                 return -EINVAL;
102
103         if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT))
104                 entry |= (upper_32_bits(paddr) & 0xff) << 4;
105
106         if (prot & ETNAVIV_PROT_WRITE)
107                 entry |= MMUv2_PTE_WRITEABLE;
108
109         mtlb_entry = (iova & MMUv2_MTLB_MASK) >> MMUv2_MTLB_SHIFT;
110         stlb_entry = (iova & MMUv2_STLB_MASK) >> MMUv2_STLB_SHIFT;
111
112         ret = etnaviv_iommuv2_ensure_stlb(v2_context, mtlb_entry);
113         if (ret)
114                 return ret;
115
116         v2_context->stlb_cpu[mtlb_entry][stlb_entry] = entry;
117
118         return 0;
119 }
120
121 static size_t etnaviv_iommuv2_unmap(struct etnaviv_iommu_context *context,
122                                     unsigned long iova, size_t size)
123 {
124         struct etnaviv_iommuv2_context *etnaviv_domain = to_v2_context(context);
125         int mtlb_entry, stlb_entry;
126
127         if (size != SZ_4K)
128                 return -EINVAL;
129
130         mtlb_entry = (iova & MMUv2_MTLB_MASK) >> MMUv2_MTLB_SHIFT;
131         stlb_entry = (iova & MMUv2_STLB_MASK) >> MMUv2_STLB_SHIFT;
132
133         etnaviv_domain->stlb_cpu[mtlb_entry][stlb_entry] = MMUv2_PTE_EXCEPTION;
134
135         return SZ_4K;
136 }
137
138 static size_t etnaviv_iommuv2_dump_size(struct etnaviv_iommu_context *context)
139 {
140         struct etnaviv_iommuv2_context *v2_context = to_v2_context(context);
141         size_t dump_size = SZ_4K;
142         int i;
143
144         for (i = 0; i < MMUv2_MAX_STLB_ENTRIES; i++)
145                 if (v2_context->mtlb_cpu[i] & MMUv2_PTE_PRESENT)
146                         dump_size += SZ_4K;
147
148         return dump_size;
149 }
150
151 static void etnaviv_iommuv2_dump(struct etnaviv_iommu_context *context, void *buf)
152 {
153         struct etnaviv_iommuv2_context *v2_context = to_v2_context(context);
154         int i;
155
156         memcpy(buf, v2_context->mtlb_cpu, SZ_4K);
157         buf += SZ_4K;
158         for (i = 0; i < MMUv2_MAX_STLB_ENTRIES; i++, buf += SZ_4K)
159                 if (v2_context->mtlb_cpu[i] & MMUv2_PTE_PRESENT)
160                         memcpy(buf, v2_context->stlb_cpu[i], SZ_4K);
161 }
162
163 static void etnaviv_iommuv2_restore_nonsec(struct etnaviv_gpu *gpu,
164         struct etnaviv_iommu_context *context)
165 {
166         struct etnaviv_iommuv2_context *v2_context = to_v2_context(context);
167         u16 prefetch;
168
169         /* If the MMU is already enabled the state is still there. */
170         if (gpu_read(gpu, VIVS_MMUv2_CONTROL) & VIVS_MMUv2_CONTROL_ENABLE)
171                 return;
172
173         prefetch = etnaviv_buffer_config_mmuv2(gpu,
174                                 (u32)v2_context->mtlb_dma,
175                                 (u32)context->global->bad_page_dma);
176         etnaviv_gpu_start_fe(gpu, (u32)etnaviv_cmdbuf_get_pa(&gpu->buffer),
177                              prefetch);
178         etnaviv_gpu_wait_idle(gpu, 100);
179
180         gpu_write(gpu, VIVS_MMUv2_CONTROL, VIVS_MMUv2_CONTROL_ENABLE);
181 }
182
183 static void etnaviv_iommuv2_restore_sec(struct etnaviv_gpu *gpu,
184         struct etnaviv_iommu_context *context)
185 {
186         struct etnaviv_iommuv2_context *v2_context = to_v2_context(context);
187         u16 prefetch;
188
189         /* If the MMU is already enabled the state is still there. */
190         if (gpu_read(gpu, VIVS_MMUv2_SEC_CONTROL) & VIVS_MMUv2_SEC_CONTROL_ENABLE)
191                 return;
192
193         gpu_write(gpu, VIVS_MMUv2_PTA_ADDRESS_LOW,
194                   lower_32_bits(context->global->v2.pta_dma));
195         gpu_write(gpu, VIVS_MMUv2_PTA_ADDRESS_HIGH,
196                   upper_32_bits(context->global->v2.pta_dma));
197         gpu_write(gpu, VIVS_MMUv2_PTA_CONTROL, VIVS_MMUv2_PTA_CONTROL_ENABLE);
198
199         gpu_write(gpu, VIVS_MMUv2_NONSEC_SAFE_ADDR_LOW,
200                   lower_32_bits(context->global->bad_page_dma));
201         gpu_write(gpu, VIVS_MMUv2_SEC_SAFE_ADDR_LOW,
202                   lower_32_bits(context->global->bad_page_dma));
203         gpu_write(gpu, VIVS_MMUv2_SAFE_ADDRESS_CONFIG,
204                   VIVS_MMUv2_SAFE_ADDRESS_CONFIG_NON_SEC_SAFE_ADDR_HIGH(
205                   upper_32_bits(context->global->bad_page_dma)) |
206                   VIVS_MMUv2_SAFE_ADDRESS_CONFIG_SEC_SAFE_ADDR_HIGH(
207                   upper_32_bits(context->global->bad_page_dma)));
208
209         context->global->v2.pta_cpu[v2_context->id] = v2_context->mtlb_dma |
210                                          VIVS_MMUv2_CONFIGURATION_MODE_MODE4_K;
211
212         /* trigger a PTA load through the FE */
213         prefetch = etnaviv_buffer_config_pta(gpu, v2_context->id);
214         etnaviv_gpu_start_fe(gpu, (u32)etnaviv_cmdbuf_get_pa(&gpu->buffer),
215                              prefetch);
216         etnaviv_gpu_wait_idle(gpu, 100);
217
218         gpu_write(gpu, VIVS_MMUv2_SEC_CONTROL, VIVS_MMUv2_SEC_CONTROL_ENABLE);
219 }
220
221 u32 etnaviv_iommuv2_get_mtlb_addr(struct etnaviv_iommu_context *context)
222 {
223         struct etnaviv_iommuv2_context *v2_context = to_v2_context(context);
224
225         return v2_context->mtlb_dma;
226 }
227
228 unsigned short etnaviv_iommuv2_get_pta_id(struct etnaviv_iommu_context *context)
229 {
230         struct etnaviv_iommuv2_context *v2_context = to_v2_context(context);
231
232         return v2_context->id;
233 }
234 static void etnaviv_iommuv2_restore(struct etnaviv_gpu *gpu,
235                                     struct etnaviv_iommu_context *context)
236 {
237         switch (gpu->sec_mode) {
238         case ETNA_SEC_NONE:
239                 etnaviv_iommuv2_restore_nonsec(gpu, context);
240                 break;
241         case ETNA_SEC_KERNEL:
242                 etnaviv_iommuv2_restore_sec(gpu, context);
243                 break;
244         default:
245                 WARN(1, "unhandled GPU security mode\n");
246                 break;
247         }
248 }
249
250 const struct etnaviv_iommu_ops etnaviv_iommuv2_ops = {
251         .free = etnaviv_iommuv2_free,
252         .map = etnaviv_iommuv2_map,
253         .unmap = etnaviv_iommuv2_unmap,
254         .dump_size = etnaviv_iommuv2_dump_size,
255         .dump = etnaviv_iommuv2_dump,
256         .restore = etnaviv_iommuv2_restore,
257 };
258
259 struct etnaviv_iommu_context *
260 etnaviv_iommuv2_context_alloc(struct etnaviv_iommu_global *global)
261 {
262         struct etnaviv_iommuv2_context *v2_context;
263         struct etnaviv_iommu_context *context;
264
265         v2_context = vzalloc(sizeof(*v2_context));
266         if (!v2_context)
267                 return NULL;
268
269         mutex_lock(&global->lock);
270         v2_context->id = find_first_zero_bit(global->v2.pta_alloc,
271                                              ETNAVIV_PTA_ENTRIES);
272         if (v2_context->id < ETNAVIV_PTA_ENTRIES) {
273                 set_bit(v2_context->id, global->v2.pta_alloc);
274         } else {
275                 mutex_unlock(&global->lock);
276                 goto out_free;
277         }
278         mutex_unlock(&global->lock);
279
280         v2_context->mtlb_cpu = dma_alloc_wc(global->dev, SZ_4K,
281                                             &v2_context->mtlb_dma, GFP_KERNEL);
282         if (!v2_context->mtlb_cpu)
283                 goto out_free_id;
284
285         memset32(v2_context->mtlb_cpu, MMUv2_PTE_EXCEPTION,
286                  MMUv2_MAX_STLB_ENTRIES);
287
288         global->v2.pta_cpu[v2_context->id] = v2_context->mtlb_dma;
289
290         context = &v2_context->base;
291         context->global = global;
292         kref_init(&context->refcount);
293         mutex_init(&context->lock);
294         INIT_LIST_HEAD(&context->mappings);
295         drm_mm_init(&context->mm, SZ_4K, (u64)SZ_1G * 4 - SZ_4K);
296
297         return context;
298
299 out_free_id:
300         clear_bit(v2_context->id, global->v2.pta_alloc);
301 out_free:
302         vfree(v2_context);
303         return NULL;
304 }