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