drm_memory.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * \file drm_memory.h
  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 <linux/vmalloc.h>
  37. #include "drmP.h"
  38. /**
  39. * Cut down version of drm_memory_debug.h, which used to be called
  40. * drm_memory.h.
  41. */
  42. #if __OS_HAS_AGP
  43. #include <linux/vmalloc.h>
  44. #ifdef HAVE_PAGE_AGP
  45. #include <asm/agp.h>
  46. #else
  47. # ifdef __powerpc__
  48. # define PAGE_AGP __pgprot(_PAGE_KERNEL | _PAGE_NO_CACHE)
  49. # else
  50. # define PAGE_AGP PAGE_KERNEL
  51. # endif
  52. #endif
  53. /*
  54. * Find the drm_map that covers the range [offset, offset+size).
  55. */
  56. static inline drm_map_t *drm_lookup_map(unsigned long offset,
  57. unsigned long size, drm_device_t * dev)
  58. {
  59. struct list_head *list;
  60. drm_map_list_t *r_list;
  61. drm_map_t *map;
  62. list_for_each(list, &dev->maplist->head) {
  63. r_list = (drm_map_list_t *) list;
  64. map = r_list->map;
  65. if (!map)
  66. continue;
  67. if (map->offset <= offset
  68. && (offset + size) <= (map->offset + map->size))
  69. return map;
  70. }
  71. return NULL;
  72. }
  73. static inline void *agp_remap(unsigned long offset, unsigned long size,
  74. drm_device_t * dev)
  75. {
  76. unsigned long *phys_addr_map, i, num_pages =
  77. PAGE_ALIGN(size) / PAGE_SIZE;
  78. struct drm_agp_mem *agpmem;
  79. struct page **page_map;
  80. void *addr;
  81. size = PAGE_ALIGN(size);
  82. #ifdef __alpha__
  83. offset -= dev->hose->mem_space->start;
  84. #endif
  85. for (agpmem = dev->agp->memory; agpmem; agpmem = agpmem->next)
  86. if (agpmem->bound <= offset
  87. && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >=
  88. (offset + size))
  89. break;
  90. if (!agpmem)
  91. return NULL;
  92. /*
  93. * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
  94. * the CPU do not get remapped by the GART. We fix this by using the kernel's
  95. * page-table instead (that's probably faster anyhow...).
  96. */
  97. /* note: use vmalloc() because num_pages could be large... */
  98. page_map = vmalloc(num_pages * sizeof(struct page *));
  99. if (!page_map)
  100. return NULL;
  101. phys_addr_map =
  102. agpmem->memory->memory + (offset - agpmem->bound) / PAGE_SIZE;
  103. for (i = 0; i < num_pages; ++i)
  104. page_map[i] = pfn_to_page(phys_addr_map[i] >> PAGE_SHIFT);
  105. addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
  106. vfree(page_map);
  107. return addr;
  108. }
  109. static inline unsigned long drm_follow_page(void *vaddr)
  110. {
  111. pgd_t *pgd = pgd_offset_k((unsigned long)vaddr);
  112. pud_t *pud = pud_offset(pgd, (unsigned long)vaddr);
  113. pmd_t *pmd = pmd_offset(pud, (unsigned long)vaddr);
  114. pte_t *ptep = pte_offset_kernel(pmd, (unsigned long)vaddr);
  115. return pte_pfn(*ptep) << PAGE_SHIFT;
  116. }
  117. #else /* __OS_HAS_AGP */
  118. static inline drm_map_t *drm_lookup_map(unsigned long offset,
  119. unsigned long size, drm_device_t * dev)
  120. {
  121. return NULL;
  122. }
  123. static inline void *agp_remap(unsigned long offset, unsigned long size,
  124. drm_device_t * dev)
  125. {
  126. return NULL;
  127. }
  128. static inline unsigned long drm_follow_page(void *vaddr)
  129. {
  130. return 0;
  131. }
  132. #endif
  133. static inline void *drm_ioremap(unsigned long offset, unsigned long size,
  134. drm_device_t * dev)
  135. {
  136. if (drm_core_has_AGP(dev) && dev->agp && dev->agp->cant_use_aperture) {
  137. drm_map_t *map = drm_lookup_map(offset, size, dev);
  138. if (map && map->type == _DRM_AGP)
  139. return agp_remap(offset, size, dev);
  140. }
  141. return ioremap(offset, size);
  142. }
  143. static inline void *drm_ioremap_nocache(unsigned long offset,
  144. unsigned long size, drm_device_t * dev)
  145. {
  146. if (drm_core_has_AGP(dev) && dev->agp && dev->agp->cant_use_aperture) {
  147. drm_map_t *map = drm_lookup_map(offset, size, dev);
  148. if (map && map->type == _DRM_AGP)
  149. return agp_remap(offset, size, dev);
  150. }
  151. return ioremap_nocache(offset, size);
  152. }
  153. static inline void drm_ioremapfree(void *pt, unsigned long size,
  154. drm_device_t * dev)
  155. {
  156. /*
  157. * This is a bit ugly. It would be much cleaner if the DRM API would use separate
  158. * routines for handling mappings in the AGP space. Hopefully this can be done in
  159. * a future revision of the interface...
  160. */
  161. if (drm_core_has_AGP(dev) && dev->agp && dev->agp->cant_use_aperture
  162. && ((unsigned long)pt >= VMALLOC_START
  163. && (unsigned long)pt < VMALLOC_END)) {
  164. unsigned long offset;
  165. drm_map_t *map;
  166. offset = drm_follow_page(pt) | ((unsigned long)pt & ~PAGE_MASK);
  167. map = drm_lookup_map(offset, size, dev);
  168. if (map && map->type == _DRM_AGP) {
  169. vunmap(pt);
  170. return;
  171. }
  172. }
  173. iounmap(pt);
  174. }