drm_scatter.c 5.4 KB

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