drm_cache.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #include <drm/drm_cache.h>
  37. /* A small bounce buffer that fits on the stack. */
  38. #define MEMCPY_BOUNCE_SIZE 128
  39. #if defined(CONFIG_X86)
  40. #include <asm/smp.h>
  41. /*
  42. * clflushopt is an unordered instruction which needs fencing with mfence or
  43. * sfence to avoid ordering issues. For drm_clflush_page this fencing happens
  44. * in the caller.
  45. */
  46. static void
  47. drm_clflush_page(struct page *page)
  48. {
  49. uint8_t *page_virtual;
  50. unsigned int i;
  51. const int size = boot_cpu_data.x86_clflush_size;
  52. if (unlikely(page == NULL))
  53. return;
  54. page_virtual = kmap_atomic(page);
  55. for (i = 0; i < PAGE_SIZE; i += size)
  56. clflushopt(page_virtual + i);
  57. kunmap_atomic(page_virtual);
  58. }
  59. static void drm_cache_flush_clflush(struct page *pages[],
  60. unsigned long num_pages)
  61. {
  62. unsigned long i;
  63. mb(); /*Full memory barrier used before so that CLFLUSH is ordered*/
  64. for (i = 0; i < num_pages; i++)
  65. drm_clflush_page(*pages++);
  66. mb(); /*Also used after CLFLUSH so that all cache is flushed*/
  67. }
  68. #endif
  69. /**
  70. * drm_clflush_pages - Flush dcache lines of a set of pages.
  71. * @pages: List of pages to be flushed.
  72. * @num_pages: Number of pages in the array.
  73. *
  74. * Flush every data cache line entry that points to an address belonging
  75. * to a page in the array.
  76. */
  77. void
  78. drm_clflush_pages(struct page *pages[], unsigned long num_pages)
  79. {
  80. #if defined(CONFIG_X86)
  81. if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
  82. drm_cache_flush_clflush(pages, num_pages);
  83. return;
  84. }
  85. wbinvd_on_all_cpus();
  86. #elif defined(__powerpc__)
  87. unsigned long i;
  88. for (i = 0; i < num_pages; i++) {
  89. struct page *page = pages[i];
  90. void *page_virtual;
  91. if (unlikely(page == NULL))
  92. continue;
  93. page_virtual = kmap_atomic(page);
  94. flush_dcache_range((unsigned long)page_virtual,
  95. (unsigned long)page_virtual + PAGE_SIZE);
  96. kunmap_atomic(page_virtual);
  97. }
  98. #else
  99. WARN_ONCE(1, "Architecture has no drm_cache.c support\n");
  100. #endif
  101. }
  102. EXPORT_SYMBOL(drm_clflush_pages);
  103. /**
  104. * drm_clflush_sg - Flush dcache lines pointing to a scather-gather.
  105. * @st: struct sg_table.
  106. *
  107. * Flush every data cache line entry that points to an address in the
  108. * sg.
  109. */
  110. void
  111. drm_clflush_sg(struct sg_table *st)
  112. {
  113. #if defined(CONFIG_X86)
  114. if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
  115. struct sg_page_iter sg_iter;
  116. mb(); /*CLFLUSH is ordered only by using memory barriers*/
  117. for_each_sgtable_page(st, &sg_iter, 0)
  118. drm_clflush_page(sg_page_iter_page(&sg_iter));
  119. mb(); /*Make sure that all cache line entry is flushed*/
  120. return;
  121. }
  122. wbinvd_on_all_cpus();
  123. #else
  124. WARN_ONCE(1, "Architecture has no drm_cache.c support\n");
  125. #endif
  126. }
  127. EXPORT_SYMBOL(drm_clflush_sg);
  128. /**
  129. * drm_clflush_virt_range - Flush dcache lines of a region
  130. * @addr: Initial kernel memory address.
  131. * @length: Region size.
  132. *
  133. * Flush every data cache line entry that points to an address in the
  134. * region requested.
  135. */
  136. void
  137. drm_clflush_virt_range(void *addr, unsigned long length)
  138. {
  139. #if defined(CONFIG_X86)
  140. if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
  141. const int size = boot_cpu_data.x86_clflush_size;
  142. void *end = addr + length;
  143. addr = (void *)(((unsigned long)addr) & -size);
  144. mb(); /*CLFLUSH is only ordered with a full memory barrier*/
  145. for (; addr < end; addr += size)
  146. clflushopt(addr);
  147. clflushopt(end - 1); /* force serialisation */
  148. mb(); /*Ensure that every data cache line entry is flushed*/
  149. return;
  150. }
  151. wbinvd_on_all_cpus();
  152. #else
  153. WARN_ONCE(1, "Architecture has no drm_cache.c support\n");
  154. #endif
  155. }
  156. EXPORT_SYMBOL(drm_clflush_virt_range);
  157. bool drm_need_swiotlb(int dma_bits)
  158. {
  159. struct resource *tmp;
  160. resource_size_t max_iomem = 0;
  161. /*
  162. * Xen paravirtual hosts require swiotlb regardless of requested dma
  163. * transfer size.
  164. *
  165. * NOTE: Really, what it requires is use of the dma_alloc_coherent
  166. * allocator used in ttm_dma_populate() instead of
  167. * ttm_populate_and_map_pages(), which bounce buffers so much in
  168. * Xen it leads to swiotlb buffer exhaustion.
  169. */
  170. if (xen_pv_domain())
  171. return true;
  172. /*
  173. * Enforce dma_alloc_coherent when memory encryption is active as well
  174. * for the same reasons as for Xen paravirtual hosts.
  175. */
  176. if (cc_platform_has(CC_ATTR_MEM_ENCRYPT))
  177. return true;
  178. for (tmp = iomem_resource.child; tmp; tmp = tmp->sibling)
  179. max_iomem = max(max_iomem, tmp->end);
  180. return max_iomem > ((u64)1 << dma_bits);
  181. }
  182. EXPORT_SYMBOL(drm_need_swiotlb);
  183. static void memcpy_fallback(struct iosys_map *dst,
  184. const struct iosys_map *src,
  185. unsigned long len)
  186. {
  187. if (!dst->is_iomem && !src->is_iomem) {
  188. memcpy(dst->vaddr, src->vaddr, len);
  189. } else if (!src->is_iomem) {
  190. iosys_map_memcpy_to(dst, 0, src->vaddr, len);
  191. } else if (!dst->is_iomem) {
  192. memcpy_fromio(dst->vaddr, src->vaddr_iomem, len);
  193. } else {
  194. /*
  195. * Bounce size is not performance tuned, but using a
  196. * bounce buffer like this is significantly faster than
  197. * resorting to ioreadxx() + iowritexx().
  198. */
  199. char bounce[MEMCPY_BOUNCE_SIZE];
  200. void __iomem *_src = src->vaddr_iomem;
  201. void __iomem *_dst = dst->vaddr_iomem;
  202. while (len >= MEMCPY_BOUNCE_SIZE) {
  203. memcpy_fromio(bounce, _src, MEMCPY_BOUNCE_SIZE);
  204. memcpy_toio(_dst, bounce, MEMCPY_BOUNCE_SIZE);
  205. _src += MEMCPY_BOUNCE_SIZE;
  206. _dst += MEMCPY_BOUNCE_SIZE;
  207. len -= MEMCPY_BOUNCE_SIZE;
  208. }
  209. if (len) {
  210. memcpy_fromio(bounce, _src, MEMCPY_BOUNCE_SIZE);
  211. memcpy_toio(_dst, bounce, MEMCPY_BOUNCE_SIZE);
  212. }
  213. }
  214. }
  215. #ifdef CONFIG_X86
  216. static DEFINE_STATIC_KEY_FALSE(has_movntdqa);
  217. static void __memcpy_ntdqa(void *dst, const void *src, unsigned long len)
  218. {
  219. kernel_fpu_begin();
  220. while (len >= 4) {
  221. asm("movntdqa (%0), %%xmm0\n"
  222. "movntdqa 16(%0), %%xmm1\n"
  223. "movntdqa 32(%0), %%xmm2\n"
  224. "movntdqa 48(%0), %%xmm3\n"
  225. "movaps %%xmm0, (%1)\n"
  226. "movaps %%xmm1, 16(%1)\n"
  227. "movaps %%xmm2, 32(%1)\n"
  228. "movaps %%xmm3, 48(%1)\n"
  229. :: "r" (src), "r" (dst) : "memory");
  230. src += 64;
  231. dst += 64;
  232. len -= 4;
  233. }
  234. while (len--) {
  235. asm("movntdqa (%0), %%xmm0\n"
  236. "movaps %%xmm0, (%1)\n"
  237. :: "r" (src), "r" (dst) : "memory");
  238. src += 16;
  239. dst += 16;
  240. }
  241. kernel_fpu_end();
  242. }
  243. /*
  244. * __drm_memcpy_from_wc copies @len bytes from @src to @dst using
  245. * non-temporal instructions where available. Note that all arguments
  246. * (@src, @dst) must be aligned to 16 bytes and @len must be a multiple
  247. * of 16.
  248. */
  249. static void __drm_memcpy_from_wc(void *dst, const void *src, unsigned long len)
  250. {
  251. if (unlikely(((unsigned long)dst | (unsigned long)src | len) & 15))
  252. memcpy(dst, src, len);
  253. else if (likely(len))
  254. __memcpy_ntdqa(dst, src, len >> 4);
  255. }
  256. /**
  257. * drm_memcpy_from_wc - Perform the fastest available memcpy from a source
  258. * that may be WC.
  259. * @dst: The destination pointer
  260. * @src: The source pointer
  261. * @len: The size of the area o transfer in bytes
  262. *
  263. * Tries an arch optimized memcpy for prefetching reading out of a WC region,
  264. * and if no such beast is available, falls back to a normal memcpy.
  265. */
  266. void drm_memcpy_from_wc(struct iosys_map *dst,
  267. const struct iosys_map *src,
  268. unsigned long len)
  269. {
  270. if (WARN_ON(in_interrupt())) {
  271. memcpy_fallback(dst, src, len);
  272. return;
  273. }
  274. if (static_branch_likely(&has_movntdqa)) {
  275. __drm_memcpy_from_wc(dst->is_iomem ?
  276. (void __force *)dst->vaddr_iomem :
  277. dst->vaddr,
  278. src->is_iomem ?
  279. (void const __force *)src->vaddr_iomem :
  280. src->vaddr,
  281. len);
  282. return;
  283. }
  284. memcpy_fallback(dst, src, len);
  285. }
  286. EXPORT_SYMBOL(drm_memcpy_from_wc);
  287. /*
  288. * drm_memcpy_init_early - One time initialization of the WC memcpy code
  289. */
  290. void drm_memcpy_init_early(void)
  291. {
  292. /*
  293. * Some hypervisors (e.g. KVM) don't support VEX-prefix instructions
  294. * emulation. So don't enable movntdqa in hypervisor guest.
  295. */
  296. if (static_cpu_has(X86_FEATURE_XMM4_1) &&
  297. !boot_cpu_has(X86_FEATURE_HYPERVISOR))
  298. static_branch_enable(&has_movntdqa);
  299. }
  300. #else
  301. void drm_memcpy_from_wc(struct iosys_map *dst,
  302. const struct iosys_map *src,
  303. unsigned long len)
  304. {
  305. WARN_ON(in_interrupt());
  306. memcpy_fallback(dst, src, len);
  307. }
  308. EXPORT_SYMBOL(drm_memcpy_from_wc);
  309. void drm_memcpy_init_early(void)
  310. {
  311. }
  312. #endif /* CONFIG_X86 */