drm_cache.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. void
  63. drm_clflush_pages(struct page *pages[], unsigned long num_pages)
  64. {
  65. #if defined(CONFIG_X86)
  66. if (cpu_has_clflush) {
  67. drm_cache_flush_clflush(pages, num_pages);
  68. return;
  69. }
  70. if (wbinvd_on_all_cpus())
  71. printk(KERN_ERR "Timed out waiting for cache flush.\n");
  72. #elif defined(__powerpc__)
  73. unsigned long i;
  74. for (i = 0; i < num_pages; i++) {
  75. struct page *page = pages[i];
  76. void *page_virtual;
  77. if (unlikely(page == NULL))
  78. continue;
  79. page_virtual = kmap_atomic(page);
  80. flush_dcache_range((unsigned long)page_virtual,
  81. (unsigned long)page_virtual + PAGE_SIZE);
  82. kunmap_atomic(page_virtual);
  83. }
  84. #else
  85. printk(KERN_ERR "Architecture has no drm_cache.c support\n");
  86. WARN_ON_ONCE(1);
  87. #endif
  88. }
  89. EXPORT_SYMBOL(drm_clflush_pages);
  90. void
  91. drm_clflush_sg(struct sg_table *st)
  92. {
  93. #if defined(CONFIG_X86)
  94. if (cpu_has_clflush) {
  95. struct sg_page_iter sg_iter;
  96. mb();
  97. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  98. drm_clflush_page(sg_page_iter_page(&sg_iter));
  99. mb();
  100. return;
  101. }
  102. if (wbinvd_on_all_cpus())
  103. printk(KERN_ERR "Timed out waiting for cache flush.\n");
  104. #else
  105. printk(KERN_ERR "Architecture has no drm_cache.c support\n");
  106. WARN_ON_ONCE(1);
  107. #endif
  108. }
  109. EXPORT_SYMBOL(drm_clflush_sg);
  110. void
  111. drm_clflush_virt_range(void *addr, unsigned long length)
  112. {
  113. #if defined(CONFIG_X86)
  114. if (cpu_has_clflush) {
  115. const int size = boot_cpu_data.x86_clflush_size;
  116. void *end = addr + length;
  117. addr = (void *)(((unsigned long)addr) & -size);
  118. mb();
  119. for (; addr < end; addr += size)
  120. clflushopt(addr);
  121. clflushopt(end - 1); /* force serialisation */
  122. mb();
  123. return;
  124. }
  125. if (wbinvd_on_all_cpus())
  126. printk(KERN_ERR "Timed out waiting for cache flush.\n");
  127. #else
  128. printk(KERN_ERR "Architecture has no drm_cache.c support\n");
  129. WARN_ON_ONCE(1);
  130. #endif
  131. }
  132. EXPORT_SYMBOL(drm_clflush_virt_range);