drm_scatter.c 5.6 KB

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