drm_memory.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * \file drm_memory.c
  3. * Memory management wrappers for DRM
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Created: Thu Feb 4 14:00:34 1999 by faith@valinux.com
  10. *
  11. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * All Rights Reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a
  16. * copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice (including the next
  23. * paragraph) shall be included in all copies or substantial portions of the
  24. * Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  29. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #include <linux/config.h>
  35. #include <linux/highmem.h>
  36. #include "drmP.h"
  37. #ifdef DEBUG_MEMORY
  38. #include "drm_memory_debug.h"
  39. #else
  40. /** No-op. */
  41. void drm_mem_init(void)
  42. {
  43. }
  44. /**
  45. * Called when "/proc/dri/%dev%/mem" is read.
  46. *
  47. * \param buf output buffer.
  48. * \param start start of output data.
  49. * \param offset requested start offset.
  50. * \param len requested number of bytes.
  51. * \param eof whether there is no more data to return.
  52. * \param data private data.
  53. * \return number of written bytes.
  54. *
  55. * No-op.
  56. */
  57. int drm_mem_info(char *buf, char **start, off_t offset,
  58. int len, int *eof, void *data)
  59. {
  60. return 0;
  61. }
  62. /** Wrapper around kmalloc() and kfree() */
  63. void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
  64. {
  65. void *pt;
  66. if (!(pt = kmalloc(size, GFP_KERNEL)))
  67. return NULL;
  68. if (oldpt && oldsize) {
  69. memcpy(pt, oldpt, oldsize);
  70. kfree(oldpt);
  71. }
  72. return pt;
  73. }
  74. #if __OS_HAS_AGP
  75. /*
  76. * Find the drm_map that covers the range [offset, offset+size).
  77. */
  78. static drm_map_t *drm_lookup_map(unsigned long offset,
  79. unsigned long size, drm_device_t * dev)
  80. {
  81. struct list_head *list;
  82. drm_map_list_t *r_list;
  83. drm_map_t *map;
  84. list_for_each(list, &dev->maplist->head) {
  85. r_list = (drm_map_list_t *) list;
  86. map = r_list->map;
  87. if (!map)
  88. continue;
  89. if (map->offset <= offset
  90. && (offset + size) <= (map->offset + map->size))
  91. return map;
  92. }
  93. return NULL;
  94. }
  95. static void *agp_remap(unsigned long offset, unsigned long size,
  96. drm_device_t * dev)
  97. {
  98. unsigned long *phys_addr_map, i, num_pages =
  99. PAGE_ALIGN(size) / PAGE_SIZE;
  100. struct drm_agp_mem *agpmem;
  101. struct page **page_map;
  102. void *addr;
  103. size = PAGE_ALIGN(size);
  104. #ifdef __alpha__
  105. offset -= dev->hose->mem_space->start;
  106. #endif
  107. for (agpmem = dev->agp->memory; agpmem; agpmem = agpmem->next)
  108. if (agpmem->bound <= offset
  109. && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >=
  110. (offset + size))
  111. break;
  112. if (!agpmem)
  113. return NULL;
  114. /*
  115. * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
  116. * the CPU do not get remapped by the GART. We fix this by using the kernel's
  117. * page-table instead (that's probably faster anyhow...).
  118. */
  119. /* note: use vmalloc() because num_pages could be large... */
  120. page_map = vmalloc(num_pages * sizeof(struct page *));
  121. if (!page_map)
  122. return NULL;
  123. phys_addr_map =
  124. agpmem->memory->memory + (offset - agpmem->bound) / PAGE_SIZE;
  125. for (i = 0; i < num_pages; ++i)
  126. page_map[i] = pfn_to_page(phys_addr_map[i] >> PAGE_SHIFT);
  127. addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
  128. vfree(page_map);
  129. return addr;
  130. }
  131. /** Wrapper around agp_allocate_memory() */
  132. DRM_AGP_MEM *drm_alloc_agp(drm_device_t * dev, int pages, u32 type)
  133. {
  134. return drm_agp_allocate_memory(dev->agp->bridge, pages, type);
  135. }
  136. /** Wrapper around agp_free_memory() */
  137. int drm_free_agp(DRM_AGP_MEM * handle, int pages)
  138. {
  139. return drm_agp_free_memory(handle) ? 0 : -EINVAL;
  140. }
  141. /** Wrapper around agp_bind_memory() */
  142. int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start)
  143. {
  144. return drm_agp_bind_memory(handle, start);
  145. }
  146. /** Wrapper around agp_unbind_memory() */
  147. int drm_unbind_agp(DRM_AGP_MEM * handle)
  148. {
  149. return drm_agp_unbind_memory(handle);
  150. }
  151. #else /* __OS_HAS_AGP */
  152. static inline drm_map_t *drm_lookup_map(unsigned long offset,
  153. unsigned long size, drm_device_t * dev)
  154. {
  155. return NULL;
  156. }
  157. static inline void *agp_remap(unsigned long offset, unsigned long size,
  158. drm_device_t * dev)
  159. {
  160. return NULL;
  161. }
  162. #endif /* agp */
  163. void *drm_ioremap(unsigned long offset, unsigned long size,
  164. drm_device_t * dev)
  165. {
  166. if (drm_core_has_AGP(dev) && dev->agp && dev->agp->cant_use_aperture) {
  167. drm_map_t *map = drm_lookup_map(offset, size, dev);
  168. if (map && map->type == _DRM_AGP)
  169. return agp_remap(offset, size, dev);
  170. }
  171. return ioremap(offset, size);
  172. }
  173. EXPORT_SYMBOL(drm_ioremap);
  174. #if 0
  175. void *drm_ioremap_nocache(unsigned long offset,
  176. unsigned long size, drm_device_t * dev)
  177. {
  178. if (drm_core_has_AGP(dev) && dev->agp && dev->agp->cant_use_aperture) {
  179. drm_map_t *map = drm_lookup_map(offset, size, dev);
  180. if (map && map->type == _DRM_AGP)
  181. return agp_remap(offset, size, dev);
  182. }
  183. return ioremap_nocache(offset, size);
  184. }
  185. #endif /* 0 */
  186. void drm_ioremapfree(void *pt, unsigned long size,
  187. drm_device_t * dev)
  188. {
  189. /*
  190. * This is a bit ugly. It would be much cleaner if the DRM API would use separate
  191. * routines for handling mappings in the AGP space. Hopefully this can be done in
  192. * a future revision of the interface...
  193. */
  194. if (drm_core_has_AGP(dev) && dev->agp && dev->agp->cant_use_aperture
  195. && ((unsigned long)pt >= VMALLOC_START
  196. && (unsigned long)pt < VMALLOC_END)) {
  197. unsigned long offset;
  198. drm_map_t *map;
  199. offset = drm_follow_page(pt) | ((unsigned long)pt & ~PAGE_MASK);
  200. map = drm_lookup_map(offset, size, dev);
  201. if (map && map->type == _DRM_AGP) {
  202. vunmap(pt);
  203. return;
  204. }
  205. }
  206. iounmap(pt);
  207. }
  208. EXPORT_SYMBOL(drm_ioremapfree);
  209. #endif /* debug_memory */