drm_cache.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/export.h>
  31. #include <drm/drmP.h>
  32. #if defined(CONFIG_X86)
  33. #include <asm/smp.h>
  34. /*
  35. * clflushopt is an unordered instruction which needs fencing with mfence or
  36. * sfence to avoid ordering issues. For drm_clflush_page this fencing happens
  37. * in the caller.
  38. */
  39. static void
  40. drm_clflush_page(struct page *page)
  41. {
  42. uint8_t *page_virtual;
  43. unsigned int i;
  44. const int size = boot_cpu_data.x86_clflush_size;
  45. if (unlikely(page == NULL))
  46. return;
  47. page_virtual = kmap_atomic(page);
  48. for (i = 0; i < PAGE_SIZE; i += size)
  49. clflushopt(page_virtual + i);
  50. kunmap_atomic(page_virtual);
  51. }
  52. static void drm_cache_flush_clflush(struct page *pages[],
  53. unsigned long num_pages)
  54. {
  55. unsigned long i;
  56. mb();
  57. for (i = 0; i < num_pages; i++)
  58. drm_clflush_page(*pages++);
  59. mb();
  60. }
  61. #endif
  62. /**
  63. * drm_clflush_pages - Flush dcache lines of a set of pages.
  64. * @pages: List of pages to be flushed.
  65. * @num_pages: Number of pages in the array.
  66. *
  67. * Flush every data cache line entry that points to an address belonging
  68. * to a page in the array.
  69. */
  70. void
  71. drm_clflush_pages(struct page *pages[], unsigned long num_pages)
  72. {
  73. #if defined(CONFIG_X86)
  74. if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
  75. drm_cache_flush_clflush(pages, num_pages);
  76. return;
  77. }
  78. if (wbinvd_on_all_cpus())
  79. printk(KERN_ERR "Timed out waiting for cache flush.\n");
  80. #elif defined(__powerpc__)
  81. unsigned long i;
  82. for (i = 0; i < num_pages; i++) {
  83. struct page *page = pages[i];
  84. void *page_virtual;
  85. if (unlikely(page == NULL))
  86. continue;
  87. page_virtual = kmap_atomic(page);
  88. flush_dcache_range((unsigned long)page_virtual,
  89. (unsigned long)page_virtual + PAGE_SIZE);
  90. kunmap_atomic(page_virtual);
  91. }
  92. #else
  93. printk(KERN_ERR "Architecture has no drm_cache.c support\n");
  94. WARN_ON_ONCE(1);
  95. #endif
  96. }
  97. EXPORT_SYMBOL(drm_clflush_pages);
  98. /**
  99. * drm_clflush_sg - Flush dcache lines pointing to a scather-gather.
  100. * @st: struct sg_table.
  101. *
  102. * Flush every data cache line entry that points to an address in the
  103. * sg.
  104. */
  105. void
  106. drm_clflush_sg(struct sg_table *st)
  107. {
  108. #if defined(CONFIG_X86)
  109. if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
  110. struct sg_page_iter sg_iter;
  111. mb();
  112. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  113. drm_clflush_page(sg_page_iter_page(&sg_iter));
  114. mb();
  115. return;
  116. }
  117. if (wbinvd_on_all_cpus())
  118. printk(KERN_ERR "Timed out waiting for cache flush.\n");
  119. #else
  120. printk(KERN_ERR "Architecture has no drm_cache.c support\n");
  121. WARN_ON_ONCE(1);
  122. #endif
  123. }
  124. EXPORT_SYMBOL(drm_clflush_sg);
  125. /**
  126. * drm_clflush_virt_range - Flush dcache lines of a region
  127. * @addr: Initial kernel memory address.
  128. * @length: Region size.
  129. *
  130. * Flush every data cache line entry that points to an address in the
  131. * region requested.
  132. */
  133. void
  134. drm_clflush_virt_range(void *addr, unsigned long length)
  135. {
  136. #if defined(CONFIG_X86)
  137. if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
  138. const int size = boot_cpu_data.x86_clflush_size;
  139. void *end = addr + length;
  140. addr = (void *)(((unsigned long)addr) & -size);
  141. mb();
  142. for (; addr < end; addr += size)
  143. clflushopt(addr);
  144. clflushopt(end - 1); /* force serialisation */
  145. mb();
  146. return;
  147. }
  148. if (wbinvd_on_all_cpus())
  149. printk(KERN_ERR "Timed out waiting for cache flush.\n");
  150. #else
  151. printk(KERN_ERR "Architecture has no drm_cache.c support\n");
  152. WARN_ON_ONCE(1);
  153. #endif
  154. }
  155. EXPORT_SYMBOL(drm_clflush_virt_range);