1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/dma-buf-map.h>
32#include <linux/export.h>
33#include <linux/highmem.h>
34#include <linux/mem_encrypt.h>
35#include <xen/xen.h>
36
37#include <drm/drm_cache.h>
38
39
40#define MEMCPY_BOUNCE_SIZE 128
41
42#if defined(CONFIG_X86)
43#include <asm/smp.h>
44
45
46
47
48
49
50static void
51drm_clflush_page(struct page *page)
52{
53 uint8_t *page_virtual;
54 unsigned int i;
55 const int size = boot_cpu_data.x86_clflush_size;
56
57 if (unlikely(page == NULL))
58 return;
59
60 page_virtual = kmap_atomic(page);
61 for (i = 0; i < PAGE_SIZE; i += size)
62 clflushopt(page_virtual + i);
63 kunmap_atomic(page_virtual);
64}
65
66static void drm_cache_flush_clflush(struct page *pages[],
67 unsigned long num_pages)
68{
69 unsigned long i;
70
71 mb();
72 for (i = 0; i < num_pages; i++)
73 drm_clflush_page(*pages++);
74 mb();
75}
76#endif
77
78
79
80
81
82
83
84
85
86void
87drm_clflush_pages(struct page *pages[], unsigned long num_pages)
88{
89
90#if defined(CONFIG_X86)
91 if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
92 drm_cache_flush_clflush(pages, num_pages);
93 return;
94 }
95
96 if (wbinvd_on_all_cpus())
97 pr_err("Timed out waiting for cache flush\n");
98
99#elif defined(__powerpc__)
100 unsigned long i;
101
102 for (i = 0; i < num_pages; i++) {
103 struct page *page = pages[i];
104 void *page_virtual;
105
106 if (unlikely(page == NULL))
107 continue;
108
109 page_virtual = kmap_atomic(page);
110 flush_dcache_range((unsigned long)page_virtual,
111 (unsigned long)page_virtual + PAGE_SIZE);
112 kunmap_atomic(page_virtual);
113 }
114#else
115 pr_err("Architecture has no drm_cache.c support\n");
116 WARN_ON_ONCE(1);
117#endif
118}
119EXPORT_SYMBOL(drm_clflush_pages);
120
121
122
123
124
125
126
127
128void
129drm_clflush_sg(struct sg_table *st)
130{
131#if defined(CONFIG_X86)
132 if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
133 struct sg_page_iter sg_iter;
134
135 mb();
136 for_each_sgtable_page(st, &sg_iter, 0)
137 drm_clflush_page(sg_page_iter_page(&sg_iter));
138 mb();
139
140 return;
141 }
142
143 if (wbinvd_on_all_cpus())
144 pr_err("Timed out waiting for cache flush\n");
145#else
146 pr_err("Architecture has no drm_cache.c support\n");
147 WARN_ON_ONCE(1);
148#endif
149}
150EXPORT_SYMBOL(drm_clflush_sg);
151
152
153
154
155
156
157
158
159
160void
161drm_clflush_virt_range(void *addr, unsigned long length)
162{
163#if defined(CONFIG_X86)
164 if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
165 const int size = boot_cpu_data.x86_clflush_size;
166 void *end = addr + length;
167
168 addr = (void *)(((unsigned long)addr) & -size);
169 mb();
170 for (; addr < end; addr += size)
171 clflushopt(addr);
172 clflushopt(end - 1);
173 mb();
174 return;
175 }
176
177 if (wbinvd_on_all_cpus())
178 pr_err("Timed out waiting for cache flush\n");
179#else
180 pr_err("Architecture has no drm_cache.c support\n");
181 WARN_ON_ONCE(1);
182#endif
183}
184EXPORT_SYMBOL(drm_clflush_virt_range);
185
186bool drm_need_swiotlb(int dma_bits)
187{
188 struct resource *tmp;
189 resource_size_t max_iomem = 0;
190
191
192
193
194
195
196
197
198
199
200 if (xen_pv_domain())
201 return true;
202
203
204
205
206
207 if (mem_encrypt_active())
208 return true;
209
210 for (tmp = iomem_resource.child; tmp; tmp = tmp->sibling)
211 max_iomem = max(max_iomem, tmp->end);
212
213 return max_iomem > ((u64)1 << dma_bits);
214}
215EXPORT_SYMBOL(drm_need_swiotlb);
216
217static void memcpy_fallback(struct dma_buf_map *dst,
218 const struct dma_buf_map *src,
219 unsigned long len)
220{
221 if (!dst->is_iomem && !src->is_iomem) {
222 memcpy(dst->vaddr, src->vaddr, len);
223 } else if (!src->is_iomem) {
224 dma_buf_map_memcpy_to(dst, src->vaddr, len);
225 } else if (!dst->is_iomem) {
226 memcpy_fromio(dst->vaddr, src->vaddr_iomem, len);
227 } else {
228
229
230
231
232
233 char bounce[MEMCPY_BOUNCE_SIZE];
234 void __iomem *_src = src->vaddr_iomem;
235 void __iomem *_dst = dst->vaddr_iomem;
236
237 while (len >= MEMCPY_BOUNCE_SIZE) {
238 memcpy_fromio(bounce, _src, MEMCPY_BOUNCE_SIZE);
239 memcpy_toio(_dst, bounce, MEMCPY_BOUNCE_SIZE);
240 _src += MEMCPY_BOUNCE_SIZE;
241 _dst += MEMCPY_BOUNCE_SIZE;
242 len -= MEMCPY_BOUNCE_SIZE;
243 }
244 if (len) {
245 memcpy_fromio(bounce, _src, MEMCPY_BOUNCE_SIZE);
246 memcpy_toio(_dst, bounce, MEMCPY_BOUNCE_SIZE);
247 }
248 }
249}
250
251#ifdef CONFIG_X86
252
253static DEFINE_STATIC_KEY_FALSE(has_movntdqa);
254
255static void __memcpy_ntdqa(void *dst, const void *src, unsigned long len)
256{
257 kernel_fpu_begin();
258
259 while (len >= 4) {
260 asm("movntdqa (%0), %%xmm0\n"
261 "movntdqa 16(%0), %%xmm1\n"
262 "movntdqa 32(%0), %%xmm2\n"
263 "movntdqa 48(%0), %%xmm3\n"
264 "movaps %%xmm0, (%1)\n"
265 "movaps %%xmm1, 16(%1)\n"
266 "movaps %%xmm2, 32(%1)\n"
267 "movaps %%xmm3, 48(%1)\n"
268 :: "r" (src), "r" (dst) : "memory");
269 src += 64;
270 dst += 64;
271 len -= 4;
272 }
273 while (len--) {
274 asm("movntdqa (%0), %%xmm0\n"
275 "movaps %%xmm0, (%1)\n"
276 :: "r" (src), "r" (dst) : "memory");
277 src += 16;
278 dst += 16;
279 }
280
281 kernel_fpu_end();
282}
283
284
285
286
287
288
289
290static void __drm_memcpy_from_wc(void *dst, const void *src, unsigned long len)
291{
292 if (unlikely(((unsigned long)dst | (unsigned long)src | len) & 15))
293 memcpy(dst, src, len);
294 else if (likely(len))
295 __memcpy_ntdqa(dst, src, len >> 4);
296}
297
298
299
300
301
302
303
304
305
306
307
308void drm_memcpy_from_wc(struct dma_buf_map *dst,
309 const struct dma_buf_map *src,
310 unsigned long len)
311{
312 if (WARN_ON(in_interrupt())) {
313 memcpy_fallback(dst, src, len);
314 return;
315 }
316
317 if (static_branch_likely(&has_movntdqa)) {
318 __drm_memcpy_from_wc(dst->is_iomem ?
319 (void __force *)dst->vaddr_iomem :
320 dst->vaddr,
321 src->is_iomem ?
322 (void const __force *)src->vaddr_iomem :
323 src->vaddr,
324 len);
325 return;
326 }
327
328 memcpy_fallback(dst, src, len);
329}
330EXPORT_SYMBOL(drm_memcpy_from_wc);
331
332
333
334
335void drm_memcpy_init_early(void)
336{
337
338
339
340
341 if (static_cpu_has(X86_FEATURE_XMM4_1) &&
342 !boot_cpu_has(X86_FEATURE_HYPERVISOR))
343 static_branch_enable(&has_movntdqa);
344}
345#else
346void drm_memcpy_from_wc(struct dma_buf_map *dst,
347 const struct dma_buf_map *src,
348 unsigned long len)
349{
350 WARN_ON(in_interrupt());
351
352 memcpy_fallback(dst, src, len);
353}
354EXPORT_SYMBOL(drm_memcpy_from_wc);
355
356void drm_memcpy_init_early(void)
357{
358}
359#endif
360