drm_memory.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/highmem.h>
  35. #include "drmP.h"
  36. #ifdef DEBUG_MEMORY
  37. #include "drm_memory_debug.h"
  38. #else
  39. /** No-op. */
  40. void drm_mem_init(void)
  41. {
  42. }
  43. /**
  44. * Called when "/proc/dri/%dev%/mem" is read.
  45. *
  46. * \param buf output buffer.
  47. * \param start start of output data.
  48. * \param offset requested start offset.
  49. * \param len requested number of bytes.
  50. * \param eof whether there is no more data to return.
  51. * \param data private data.
  52. * \return number of written bytes.
  53. *
  54. * No-op.
  55. */
  56. int drm_mem_info(char *buf, char **start, off_t offset,
  57. int len, int *eof, void *data)
  58. {
  59. return 0;
  60. }
  61. /** Wrapper around kmalloc() and kfree() */
  62. void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
  63. {
  64. void *pt;
  65. if (!(pt = kmalloc(size, GFP_KERNEL)))
  66. return NULL;
  67. if (oldpt && oldsize) {
  68. memcpy(pt, oldpt, oldsize);
  69. kfree(oldpt);
  70. }
  71. return pt;
  72. }
  73. #if __OS_HAS_AGP
  74. /*
  75. * Find the drm_map that covers the range [offset, offset+size).
  76. */
  77. static drm_map_t *drm_lookup_map(unsigned long offset,
  78. unsigned long size, drm_device_t * dev)
  79. {
  80. struct list_head *list;
  81. drm_map_list_t *r_list;
  82. drm_map_t *map;
  83. list_for_each(list, &dev->maplist->head) {
  84. r_list = (drm_map_list_t *) list;
  85. map = r_list->map;
  86. if (!map)
  87. continue;
  88. if (map->offset <= offset
  89. && (offset + size) <= (map->offset + map->size))
  90. return map;
  91. }
  92. return NULL;
  93. }
  94. static void *agp_remap(unsigned long offset, unsigned long size,
  95. drm_device_t * dev)
  96. {
  97. unsigned long *phys_addr_map, i, num_pages =
  98. PAGE_ALIGN(size) / PAGE_SIZE;
  99. struct drm_agp_mem *agpmem;
  100. struct page **page_map;
  101. void *addr;
  102. size = PAGE_ALIGN(size);
  103. #ifdef __alpha__
  104. offset -= dev->hose->mem_space->start;
  105. #endif
  106. for (agpmem = dev->agp->memory; agpmem; agpmem = agpmem->next)
  107. if (agpmem->bound <= offset
  108. && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >=
  109. (offset + size))
  110. break;
  111. if (!agpmem)
  112. return NULL;
  113. /*
  114. * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
  115. * the CPU do not get remapped by the GART. We fix this by using the kernel's
  116. * page-table instead (that's probably faster anyhow...).
  117. */
  118. /* note: use vmalloc() because num_pages could be large... */
  119. page_map = vmalloc(num_pages * sizeof(struct page *));
  120. if (!page_map)
  121. return NULL;
  122. phys_addr_map =
  123. agpmem->memory->memory + (offset - agpmem->bound) / PAGE_SIZE;
  124. for (i = 0; i < num_pages; ++i)
  125. page_map[i] = pfn_to_page(phys_addr_map[i] >> PAGE_SHIFT);
  126. addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
  127. vfree(page_map);
  128. return addr;
  129. }
  130. /** Wrapper around agp_allocate_memory() */
  131. DRM_AGP_MEM *drm_alloc_agp(drm_device_t * dev, int pages, u32 type)
  132. {
  133. return drm_agp_allocate_memory(dev->agp->bridge, pages, type);
  134. }
  135. /** Wrapper around agp_free_memory() */
  136. int drm_free_agp(DRM_AGP_MEM * handle, int pages)
  137. {
  138. return drm_agp_free_memory(handle) ? 0 : -EINVAL;
  139. }
  140. /** Wrapper around agp_bind_memory() */
  141. int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start)
  142. {
  143. return drm_agp_bind_memory(handle, start);
  144. }
  145. /** Wrapper around agp_unbind_memory() */
  146. int drm_unbind_agp(DRM_AGP_MEM * handle)
  147. {
  148. return drm_agp_unbind_memory(handle);
  149. }
  150. #else /* __OS_HAS_AGP */
  151. static inline drm_map_t *drm_lookup_map(unsigned long offset,
  152. unsigned long size, drm_device_t * dev)
  153. {
  154. return NULL;
  155. }
  156. static inline void *agp_remap(unsigned long offset, unsigned long size,
  157. drm_device_t * dev)
  158. {
  159. return NULL;
  160. }
  161. #endif /* agp */
  162. void *drm_ioremap(unsigned long offset, unsigned long size,
  163. drm_device_t * dev)
  164. {
  165. if (drm_core_has_AGP(dev) && dev->agp && dev->agp->cant_use_aperture) {
  166. drm_map_t *map = drm_lookup_map(offset, size, dev);
  167. if (map && map->type == _DRM_AGP)
  168. return agp_remap(offset, size, dev);
  169. }
  170. return ioremap(offset, size);
  171. }
  172. EXPORT_SYMBOL(drm_ioremap);
  173. #if 0
  174. void *drm_ioremap_nocache(unsigned long offset,
  175. unsigned long size, drm_device_t * dev)
  176. {
  177. if (drm_core_has_AGP(dev) && dev->agp && dev->agp->cant_use_aperture) {
  178. drm_map_t *map = drm_lookup_map(offset, size, dev);
  179. if (map && map->type == _DRM_AGP)
  180. return agp_remap(offset, size, dev);
  181. }
  182. return ioremap_nocache(offset, size);
  183. }
  184. #endif /* 0 */
  185. void drm_ioremapfree(void *pt, unsigned long size,
  186. drm_device_t * dev)
  187. {
  188. /*
  189. * This is a bit ugly. It would be much cleaner if the DRM API would use separate
  190. * routines for handling mappings in the AGP space. Hopefully this can be done in
  191. * a future revision of the interface...
  192. */
  193. if (drm_core_has_AGP(dev) && dev->agp && dev->agp->cant_use_aperture
  194. && ((unsigned long)pt >= VMALLOC_START
  195. && (unsigned long)pt < VMALLOC_END)) {
  196. unsigned long offset;
  197. drm_map_t *map;
  198. offset = drm_follow_page(pt) | ((unsigned long)pt & ~PAGE_MASK);
  199. map = drm_lookup_map(offset, size, dev);
  200. if (map && map->type == _DRM_AGP) {
  201. vunmap(pt);
  202. return;
  203. }
  204. }
  205. iounmap(pt);
  206. }
  207. EXPORT_SYMBOL(drm_ioremapfree);
  208. #endif /* debug_memory */