drm_scatter.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * \file drm_scatter.c
  3. * IOCTLs to manage scatter/gather memory
  4. *
  5. * \author Gareth Hughes <gareth@valinux.com>
  6. */
  7. /*
  8. * Created: Mon Dec 18 23:20:54 2000 by gareth@valinux.com
  9. *
  10. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  11. * All Rights Reserved.
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice (including the next
  21. * paragraph) shall be included in all copies or substantial portions of the
  22. * Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  28. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  29. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. * DEALINGS IN THE SOFTWARE.
  31. */
  32. #include <linux/vmalloc.h>
  33. #include <linux/slab.h>
  34. #include <drm/drmP.h>
  35. #define DEBUG_SCATTER 0
  36. static inline void *drm_vmalloc_dma(unsigned long size)
  37. {
  38. #if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
  39. return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL | _PAGE_NO_CACHE);
  40. #else
  41. return vmalloc_32(size);
  42. #endif
  43. }
  44. static void drm_sg_cleanup(struct drm_sg_mem * entry)
  45. {
  46. struct page *page;
  47. int i;
  48. for (i = 0; i < entry->pages; i++) {
  49. page = entry->pagelist[i];
  50. if (page)
  51. ClearPageReserved(page);
  52. }
  53. vfree(entry->virtual);
  54. kfree(entry->busaddr);
  55. kfree(entry->pagelist);
  56. kfree(entry);
  57. }
  58. void drm_legacy_sg_cleanup(struct drm_device *dev)
  59. {
  60. if (drm_core_check_feature(dev, DRIVER_SG) && dev->sg &&
  61. !drm_core_check_feature(dev, DRIVER_MODESET)) {
  62. drm_sg_cleanup(dev->sg);
  63. dev->sg = NULL;
  64. }
  65. }
  66. #ifdef _LP64
  67. # define ScatterHandle(x) (unsigned int)((x >> 32) + (x & ((1L << 32) - 1)))
  68. #else
  69. # define ScatterHandle(x) (unsigned int)(x)
  70. #endif
  71. int drm_sg_alloc(struct drm_device *dev, void *data,
  72. struct drm_file *file_priv)
  73. {
  74. struct drm_scatter_gather *request = data;
  75. struct drm_sg_mem *entry;
  76. unsigned long pages, i, j;
  77. DRM_DEBUG("\n");
  78. if (drm_core_check_feature(dev, DRIVER_MODESET))
  79. return -EINVAL;
  80. if (!drm_core_check_feature(dev, DRIVER_SG))
  81. return -EINVAL;
  82. if (dev->sg)
  83. return -EINVAL;
  84. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  85. if (!entry)
  86. return -ENOMEM;
  87. pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
  88. DRM_DEBUG("size=%ld pages=%ld\n", request->size, pages);
  89. entry->pages = pages;
  90. entry->pagelist = kcalloc(pages, sizeof(*entry->pagelist), GFP_KERNEL);
  91. if (!entry->pagelist) {
  92. kfree(entry);
  93. return -ENOMEM;
  94. }
  95. entry->busaddr = kcalloc(pages, sizeof(*entry->busaddr), GFP_KERNEL);
  96. if (!entry->busaddr) {
  97. kfree(entry->pagelist);
  98. kfree(entry);
  99. return -ENOMEM;
  100. }
  101. entry->virtual = drm_vmalloc_dma(pages << PAGE_SHIFT);
  102. if (!entry->virtual) {
  103. kfree(entry->busaddr);
  104. kfree(entry->pagelist);
  105. kfree(entry);
  106. return -ENOMEM;
  107. }
  108. /* This also forces the mapping of COW pages, so our page list
  109. * will be valid. Please don't remove it...
  110. */
  111. memset(entry->virtual, 0, pages << PAGE_SHIFT);
  112. entry->handle = ScatterHandle((unsigned long)entry->virtual);
  113. DRM_DEBUG("handle = %08lx\n", entry->handle);
  114. DRM_DEBUG("virtual = %p\n", entry->virtual);
  115. for (i = (unsigned long)entry->virtual, j = 0; j < pages;
  116. i += PAGE_SIZE, j++) {
  117. entry->pagelist[j] = vmalloc_to_page((void *)i);
  118. if (!entry->pagelist[j])
  119. goto failed;
  120. SetPageReserved(entry->pagelist[j]);
  121. }
  122. request->handle = entry->handle;
  123. dev->sg = entry;
  124. #if DEBUG_SCATTER
  125. /* Verify that each page points to its virtual address, and vice
  126. * versa.
  127. */
  128. {
  129. int error = 0;
  130. for (i = 0; i < pages; i++) {
  131. unsigned long *tmp;
  132. tmp = page_address(entry->pagelist[i]);
  133. for (j = 0;
  134. j < PAGE_SIZE / sizeof(unsigned long);
  135. j++, tmp++) {
  136. *tmp = 0xcafebabe;
  137. }
  138. tmp = (unsigned long *)((u8 *) entry->virtual +
  139. (PAGE_SIZE * i));
  140. for (j = 0;
  141. j < PAGE_SIZE / sizeof(unsigned long);
  142. j++, tmp++) {
  143. if (*tmp != 0xcafebabe && error == 0) {
  144. error = 1;
  145. DRM_ERROR("Scatter allocation error, "
  146. "pagelist does not match "
  147. "virtual mapping\n");
  148. }
  149. }
  150. tmp = page_address(entry->pagelist[i]);
  151. for (j = 0;
  152. j < PAGE_SIZE / sizeof(unsigned long);
  153. j++, tmp++) {
  154. *tmp = 0;
  155. }
  156. }
  157. if (error == 0)
  158. DRM_ERROR("Scatter allocation matches pagelist\n");
  159. }
  160. #endif
  161. return 0;
  162. failed:
  163. drm_sg_cleanup(entry);
  164. return -ENOMEM;
  165. }
  166. int drm_sg_free(struct drm_device *dev, void *data,
  167. struct drm_file *file_priv)
  168. {
  169. struct drm_scatter_gather *request = data;
  170. struct drm_sg_mem *entry;
  171. if (drm_core_check_feature(dev, DRIVER_MODESET))
  172. return -EINVAL;
  173. if (!drm_core_check_feature(dev, DRIVER_SG))
  174. return -EINVAL;
  175. entry = dev->sg;
  176. dev->sg = NULL;
  177. if (!entry || entry->handle != request->handle)
  178. return -EINVAL;
  179. DRM_DEBUG("virtual = %p\n", entry->virtual);
  180. drm_sg_cleanup(entry);
  181. return 0;
  182. }