drm_cache.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. static void
  34. drm_clflush_page(struct page *page)
  35. {
  36. uint8_t *page_virtual;
  37. unsigned int i;
  38. const int size = boot_cpu_data.x86_clflush_size;
  39. if (unlikely(page == NULL))
  40. return;
  41. page_virtual = kmap_atomic(page);
  42. for (i = 0; i < PAGE_SIZE; i += size)
  43. clflush(page_virtual + i);
  44. kunmap_atomic(page_virtual);
  45. }
  46. static void drm_cache_flush_clflush(struct page *pages[],
  47. unsigned long num_pages)
  48. {
  49. unsigned long i;
  50. mb();
  51. for (i = 0; i < num_pages; i++)
  52. drm_clflush_page(*pages++);
  53. mb();
  54. }
  55. static void
  56. drm_clflush_ipi_handler(void *null)
  57. {
  58. wbinvd();
  59. }
  60. #endif
  61. void
  62. drm_clflush_pages(struct page *pages[], unsigned long num_pages)
  63. {
  64. #if defined(CONFIG_X86)
  65. if (cpu_has_clflush) {
  66. drm_cache_flush_clflush(pages, num_pages);
  67. return;
  68. }
  69. if (on_each_cpu(drm_clflush_ipi_handler, NULL, 1) != 0)
  70. printk(KERN_ERR "Timed out waiting for cache flush.\n");
  71. #elif defined(__powerpc__)
  72. unsigned long i;
  73. for (i = 0; i < num_pages; i++) {
  74. struct page *page = pages[i];
  75. void *page_virtual;
  76. if (unlikely(page == NULL))
  77. continue;
  78. page_virtual = kmap_atomic(page);
  79. flush_dcache_range((unsigned long)page_virtual,
  80. (unsigned long)page_virtual + PAGE_SIZE);
  81. kunmap_atomic(page_virtual);
  82. }
  83. #else
  84. printk(KERN_ERR "Architecture has no drm_cache.c support\n");
  85. WARN_ON_ONCE(1);
  86. #endif
  87. }
  88. EXPORT_SYMBOL(drm_clflush_pages);
  89. void
  90. drm_clflush_sg(struct sg_table *st)
  91. {
  92. #if defined(CONFIG_X86)
  93. if (cpu_has_clflush) {
  94. struct scatterlist *sg;
  95. int i;
  96. mb();
  97. for_each_sg(st->sgl, sg, st->nents, i)
  98. drm_clflush_page(sg_page(sg));
  99. mb();
  100. return;
  101. }
  102. if (on_each_cpu(drm_clflush_ipi_handler, NULL, 1) != 0)
  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(char *addr, unsigned long length)
  112. {
  113. #if defined(CONFIG_X86)
  114. if (cpu_has_clflush) {
  115. char *end = addr + length;
  116. mb();
  117. for (; addr < end; addr += boot_cpu_data.x86_clflush_size)
  118. clflush(addr);
  119. clflush(end - 1);
  120. mb();
  121. return;
  122. }
  123. if (on_each_cpu(drm_clflush_ipi_handler, NULL, 1) != 0)
  124. printk(KERN_ERR "Timed out waiting for cache flush.\n");
  125. #else
  126. printk(KERN_ERR "Architecture has no drm_cache.c support\n");
  127. WARN_ON_ONCE(1);
  128. #endif
  129. }
  130. EXPORT_SYMBOL(drm_clflush_virt_range);