1 /************************************************************************** 2 * 3 * Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 24 * USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 /* 28 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com> 29 */ 30 #include <linux/cc_platform.h> 31 #include <linux/export.h> 32 #include <linux/highmem.h> 33 #include <linux/ioport.h> 34 #include <linux/iosys-map.h> 35 #include <xen/xen.h> 36 37 #include <drm/drm_cache.h> 38 39 /* A small bounce buffer that fits on the stack. */ 40 #define MEMCPY_BOUNCE_SIZE 128 41 42 #if defined(CONFIG_X86) 43 #include <asm/smp.h> 44 45 /* 46 * clflushopt is an unordered instruction which needs fencing with mfence or 47 * sfence to avoid ordering issues. For drm_clflush_page this fencing happens 48 * in the caller. 49 */ 50 static void 51 drm_clflush_page(struct vm_page *page) 52 { 53 uint8_t *page_virtual; 54 unsigned int i; 55 const int size = curcpu()->ci_cflushsz; 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 66 static void drm_cache_flush_clflush(struct vm_page *pages[], 67 unsigned long num_pages) 68 { 69 unsigned long i; 70 71 mb(); /*Full memory barrier used before so that CLFLUSH is ordered*/ 72 for (i = 0; i < num_pages; i++) 73 drm_clflush_page(*pages++); 74 mb(); /*Also used after CLFLUSH so that all cache is flushed*/ 75 } 76 #endif 77 78 /** 79 * drm_clflush_pages - Flush dcache lines of a set of pages. 80 * @pages: List of pages to be flushed. 81 * @num_pages: Number of pages in the array. 82 * 83 * Flush every data cache line entry that points to an address belonging 84 * to a page in the array. 85 */ 86 void 87 drm_clflush_pages(struct vm_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__) && defined(__linux__) 100 unsigned long i; 101 102 for (i = 0; i < num_pages; i++) { 103 struct vm_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 WARN_ONCE(1, "Architecture has no drm_cache.c support\n"); 116 #endif 117 } 118 EXPORT_SYMBOL(drm_clflush_pages); 119 120 /** 121 * drm_clflush_sg - Flush dcache lines pointing to a scather-gather. 122 * @st: struct sg_table. 123 * 124 * Flush every data cache line entry that points to an address in the 125 * sg. 126 */ 127 void 128 drm_clflush_sg(struct sg_table *st) 129 { 130 #if defined(CONFIG_X86) 131 if (static_cpu_has(X86_FEATURE_CLFLUSH)) { 132 struct sg_page_iter sg_iter; 133 134 mb(); /*CLFLUSH is ordered only by using memory barriers*/ 135 for_each_sgtable_page(st, &sg_iter, 0) 136 drm_clflush_page(sg_page_iter_page(&sg_iter)); 137 mb(); /*Make sure that all cache line entry is flushed*/ 138 139 return; 140 } 141 142 if (wbinvd_on_all_cpus()) 143 pr_err("Timed out waiting for cache flush\n"); 144 #else 145 WARN_ONCE(1, "Architecture has no drm_cache.c support\n"); 146 #endif 147 } 148 EXPORT_SYMBOL(drm_clflush_sg); 149 150 /** 151 * drm_clflush_virt_range - Flush dcache lines of a region 152 * @addr: Initial kernel memory address. 153 * @length: Region size. 154 * 155 * Flush every data cache line entry that points to an address in the 156 * region requested. 157 */ 158 void 159 drm_clflush_virt_range(void *addr, unsigned long length) 160 { 161 #if defined(CONFIG_X86) 162 if (static_cpu_has(X86_FEATURE_CLFLUSH)) { 163 const int size = curcpu()->ci_cflushsz; 164 void *end = addr + length; 165 166 addr = (void *)(((unsigned long)addr) & -size); 167 mb(); /*CLFLUSH is only ordered with a full memory barrier*/ 168 for (; addr < end; addr += size) 169 clflushopt(addr); 170 clflushopt(end - 1); /* force serialisation */ 171 mb(); /*Ensure that every data cache line entry is flushed*/ 172 return; 173 } 174 175 if (wbinvd_on_all_cpus()) 176 pr_err("Timed out waiting for cache flush\n"); 177 #else 178 WARN_ONCE(1, "Architecture has no drm_cache.c support\n"); 179 #endif 180 } 181 EXPORT_SYMBOL(drm_clflush_virt_range); 182 183 bool drm_need_swiotlb(int dma_bits) 184 { 185 return false; 186 #ifdef notyet 187 struct resource *tmp; 188 resource_size_t max_iomem = 0; 189 190 /* 191 * Xen paravirtual hosts require swiotlb regardless of requested dma 192 * transfer size. 193 * 194 * NOTE: Really, what it requires is use of the dma_alloc_coherent 195 * allocator used in ttm_dma_populate() instead of 196 * ttm_populate_and_map_pages(), which bounce buffers so much in 197 * Xen it leads to swiotlb buffer exhaustion. 198 */ 199 if (xen_pv_domain()) 200 return true; 201 202 /* 203 * Enforce dma_alloc_coherent when memory encryption is active as well 204 * for the same reasons as for Xen paravirtual hosts. 205 */ 206 if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) 207 return true; 208 209 for (tmp = iomem_resource.child; tmp; tmp = tmp->sibling) 210 max_iomem = max(max_iomem, tmp->end); 211 212 return max_iomem > ((u64)1 << dma_bits); 213 #endif 214 } 215 EXPORT_SYMBOL(drm_need_swiotlb); 216 217 static void memcpy_fallback(struct iosys_map *dst, 218 const struct iosys_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 iosys_map_memcpy_to(dst, 0, src->vaddr, len); 225 } else if (!dst->is_iomem) { 226 memcpy_fromio(dst->vaddr, src->vaddr_iomem, len); 227 } else { 228 /* 229 * Bounce size is not performance tuned, but using a 230 * bounce buffer like this is significantly faster than 231 * resorting to ioreadxx() + iowritexx(). 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 253 #ifdef __linux__ 254 static DEFINE_STATIC_KEY_FALSE(has_movntdqa); 255 #else 256 static int has_movntdqa; 257 258 #include <asm/fpu/api.h> 259 260 static inline void 261 static_branch_enable(int *x) 262 { 263 *x = 1; 264 } 265 266 static inline int 267 static_branch_likely(int *x) 268 { 269 return (likely(*x == 1)); 270 } 271 272 #endif 273 274 static void __memcpy_ntdqa(void *dst, const void *src, unsigned long len) 275 { 276 kernel_fpu_begin(); 277 278 while (len >= 4) { 279 asm("movntdqa (%0), %%xmm0\n" 280 "movntdqa 16(%0), %%xmm1\n" 281 "movntdqa 32(%0), %%xmm2\n" 282 "movntdqa 48(%0), %%xmm3\n" 283 "movaps %%xmm0, (%1)\n" 284 "movaps %%xmm1, 16(%1)\n" 285 "movaps %%xmm2, 32(%1)\n" 286 "movaps %%xmm3, 48(%1)\n" 287 :: "r" (src), "r" (dst) : "memory"); 288 src += 64; 289 dst += 64; 290 len -= 4; 291 } 292 while (len--) { 293 asm("movntdqa (%0), %%xmm0\n" 294 "movaps %%xmm0, (%1)\n" 295 :: "r" (src), "r" (dst) : "memory"); 296 src += 16; 297 dst += 16; 298 } 299 300 kernel_fpu_end(); 301 } 302 303 /* 304 * __drm_memcpy_from_wc copies @len bytes from @src to @dst using 305 * non-temporal instructions where available. Note that all arguments 306 * (@src, @dst) must be aligned to 16 bytes and @len must be a multiple 307 * of 16. 308 */ 309 static void __drm_memcpy_from_wc(void *dst, const void *src, unsigned long len) 310 { 311 if (unlikely(((unsigned long)dst | (unsigned long)src | len) & 15)) 312 memcpy(dst, src, len); 313 else if (likely(len)) 314 __memcpy_ntdqa(dst, src, len >> 4); 315 } 316 317 /** 318 * drm_memcpy_from_wc - Perform the fastest available memcpy from a source 319 * that may be WC. 320 * @dst: The destination pointer 321 * @src: The source pointer 322 * @len: The size of the area o transfer in bytes 323 * 324 * Tries an arch optimized memcpy for prefetching reading out of a WC region, 325 * and if no such beast is available, falls back to a normal memcpy. 326 */ 327 void drm_memcpy_from_wc(struct iosys_map *dst, 328 const struct iosys_map *src, 329 unsigned long len) 330 { 331 if (WARN_ON(in_interrupt())) { 332 memcpy_fallback(dst, src, len); 333 return; 334 } 335 336 if (static_branch_likely(&has_movntdqa)) { 337 __drm_memcpy_from_wc(dst->is_iomem ? 338 (void __force *)dst->vaddr_iomem : 339 dst->vaddr, 340 src->is_iomem ? 341 (void const __force *)src->vaddr_iomem : 342 src->vaddr, 343 len); 344 return; 345 } 346 347 memcpy_fallback(dst, src, len); 348 } 349 EXPORT_SYMBOL(drm_memcpy_from_wc); 350 351 /* 352 * drm_memcpy_init_early - One time initialization of the WC memcpy code 353 */ 354 void drm_memcpy_init_early(void) 355 { 356 /* 357 * Some hypervisors (e.g. KVM) don't support VEX-prefix instructions 358 * emulation. So don't enable movntdqa in hypervisor guest. 359 */ 360 if (static_cpu_has(X86_FEATURE_XMM4_1) && 361 !boot_cpu_has(X86_FEATURE_HYPERVISOR)) 362 static_branch_enable(&has_movntdqa); 363 } 364 #else 365 void drm_memcpy_from_wc(struct iosys_map *dst, 366 const struct iosys_map *src, 367 unsigned long len) 368 { 369 WARN_ON(in_interrupt()); 370 371 memcpy_fallback(dst, src, len); 372 } 373 EXPORT_SYMBOL(drm_memcpy_from_wc); 374 375 void drm_memcpy_init_early(void) 376 { 377 } 378 #endif /* CONFIG_X86 */ 379