drm_scatter.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "drmP.h"
  34. #define DEBUG_SCATTER 0
  35. static inline void *drm_vmalloc_dma(unsigned long size)
  36. {
  37. #if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
  38. return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL | _PAGE_NO_CACHE);
  39. #else
  40. return vmalloc_32(size);
  41. #endif
  42. }
  43. void drm_sg_cleanup(struct drm_sg_mem * entry)
  44. {
  45. struct page *page;
  46. int i;
  47. for (i = 0; i < entry->pages; i++) {
  48. page = entry->pagelist[i];
  49. if (page)
  50. ClearPageReserved(page);
  51. }
  52. vfree(entry->virtual);
  53. drm_free(entry->busaddr,
  54. entry->pages * sizeof(*entry->busaddr), DRM_MEM_PAGES);
  55. drm_free(entry->pagelist,
  56. entry->pages * sizeof(*entry->pagelist), DRM_MEM_PAGES);
  57. drm_free(entry, sizeof(*entry), DRM_MEM_SGLISTS);
  58. }
  59. #ifdef _LP64
  60. # define ScatterHandle(x) (unsigned int)((x >> 32) + (x & ((1L << 32) - 1)))
  61. #else
  62. # define ScatterHandle(x) (unsigned int)(x)
  63. #endif
  64. int drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request)
  65. {
  66. struct drm_sg_mem *entry;
  67. unsigned long pages, i, j;
  68. DRM_DEBUG("\n");
  69. if (!drm_core_check_feature(dev, DRIVER_SG))
  70. return -EINVAL;
  71. if (dev->sg)
  72. return -EINVAL;
  73. entry = drm_alloc(sizeof(*entry), DRM_MEM_SGLISTS);
  74. if (!entry)
  75. return -ENOMEM;
  76. memset(entry, 0, sizeof(*entry));
  77. pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
  78. DRM_DEBUG("size=%ld pages=%ld\n", request->size, pages);
  79. entry->pages = pages;
  80. entry->pagelist = drm_alloc(pages * sizeof(*entry->pagelist),
  81. DRM_MEM_PAGES);
  82. if (!entry->pagelist) {
  83. drm_free(entry, sizeof(*entry), DRM_MEM_SGLISTS);
  84. return -ENOMEM;
  85. }
  86. memset(entry->pagelist, 0, pages * sizeof(*entry->pagelist));
  87. entry->busaddr = drm_alloc(pages * sizeof(*entry->busaddr),
  88. DRM_MEM_PAGES);
  89. if (!entry->busaddr) {
  90. drm_free(entry->pagelist,
  91. entry->pages * sizeof(*entry->pagelist),
  92. DRM_MEM_PAGES);
  93. drm_free(entry, sizeof(*entry), DRM_MEM_SGLISTS);
  94. return -ENOMEM;
  95. }
  96. memset((void *)entry->busaddr, 0, pages * sizeof(*entry->busaddr));
  97. entry->virtual = drm_vmalloc_dma(pages << PAGE_SHIFT);
  98. if (!entry->virtual) {
  99. drm_free(entry->busaddr,
  100. entry->pages * sizeof(*entry->busaddr), DRM_MEM_PAGES);
  101. drm_free(entry->pagelist,
  102. entry->pages * sizeof(*entry->pagelist),
  103. DRM_MEM_PAGES);
  104. drm_free(entry, sizeof(*entry), DRM_MEM_SGLISTS);
  105. return -ENOMEM;
  106. }
  107. /* This also forces the mapping of COW pages, so our page list
  108. * will be valid. Please don't remove it...
  109. */
  110. memset(entry->virtual, 0, pages << PAGE_SHIFT);
  111. entry->handle = ScatterHandle((unsigned long)entry->virtual);
  112. DRM_DEBUG("handle = %08lx\n", entry->handle);
  113. DRM_DEBUG("virtual = %p\n", entry->virtual);
  114. for (i = (unsigned long)entry->virtual, j = 0; j < pages;
  115. i += PAGE_SIZE, j++) {
  116. entry->pagelist[j] = vmalloc_to_page((void *)i);
  117. if (!entry->pagelist[j])
  118. goto failed;
  119. SetPageReserved(entry->pagelist[j]);
  120. }
  121. request->handle = entry->handle;
  122. dev->sg = entry;
  123. #if DEBUG_SCATTER
  124. /* Verify that each page points to its virtual address, and vice
  125. * versa.
  126. */
  127. {
  128. int error = 0;
  129. for (i = 0; i < pages; i++) {
  130. unsigned long *tmp;
  131. tmp = page_address(entry->pagelist[i]);
  132. for (j = 0;
  133. j < PAGE_SIZE / sizeof(unsigned long);
  134. j++, tmp++) {
  135. *tmp = 0xcafebabe;
  136. }
  137. tmp = (unsigned long *)((u8 *) entry->virtual +
  138. (PAGE_SIZE * i));
  139. for (j = 0;
  140. j < PAGE_SIZE / sizeof(unsigned long);
  141. j++, tmp++) {
  142. if (*tmp != 0xcafebabe && error == 0) {
  143. error = 1;
  144. DRM_ERROR("Scatter allocation error, "
  145. "pagelist does not match "
  146. "virtual mapping\n");
  147. }
  148. }
  149. tmp = page_address(entry->pagelist[i]);
  150. for (j = 0;
  151. j < PAGE_SIZE / sizeof(unsigned long);
  152. j++, tmp++) {
  153. *tmp = 0;
  154. }
  155. }
  156. if (error == 0)
  157. DRM_ERROR("Scatter allocation matches pagelist\n");
  158. }
  159. #endif
  160. return 0;
  161. failed:
  162. drm_sg_cleanup(entry);
  163. return -ENOMEM;
  164. }
  165. EXPORT_SYMBOL(drm_sg_alloc);
  166. int drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
  167. struct drm_file *file_priv)
  168. {
  169. struct drm_scatter_gather *request = data;
  170. return drm_sg_alloc(dev, request);
  171. }
  172. int drm_sg_free(struct drm_device *dev, void *data,
  173. struct drm_file *file_priv)
  174. {
  175. struct drm_scatter_gather *request = data;
  176. struct drm_sg_mem *entry;
  177. if (!drm_core_check_feature(dev, DRIVER_SG))
  178. return -EINVAL;
  179. entry = dev->sg;
  180. dev->sg = NULL;
  181. if (!entry || entry->handle != request->handle)
  182. return -EINVAL;
  183. DRM_DEBUG("virtual = %p\n", entry->virtual);
  184. drm_sg_cleanup(entry);
  185. return 0;
  186. }