drm_memory.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 *
  57. drm_lookup_map (unsigned long offset, 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 && (offset + size) <= (map->offset + map->size))
  68. return map;
  69. }
  70. return NULL;
  71. }
  72. static inline void *
  73. agp_remap (unsigned long offset, unsigned long size, drm_device_t *dev)
  74. {
  75. unsigned long *phys_addr_map, i, num_pages = PAGE_ALIGN(size) / PAGE_SIZE;
  76. struct drm_agp_mem *agpmem;
  77. struct page **page_map;
  78. void *addr;
  79. size = PAGE_ALIGN(size);
  80. #ifdef __alpha__
  81. offset -= dev->hose->mem_space->start;
  82. #endif
  83. for (agpmem = dev->agp->memory; agpmem; agpmem = agpmem->next)
  84. if (agpmem->bound <= offset
  85. && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >= (offset + size))
  86. break;
  87. if (!agpmem)
  88. return NULL;
  89. /*
  90. * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
  91. * the CPU do not get remapped by the GART. We fix this by using the kernel's
  92. * page-table instead (that's probably faster anyhow...).
  93. */
  94. /* note: use vmalloc() because num_pages could be large... */
  95. page_map = vmalloc(num_pages * sizeof(struct page *));
  96. if (!page_map)
  97. return NULL;
  98. phys_addr_map = agpmem->memory->memory + (offset - agpmem->bound) / PAGE_SIZE;
  99. for (i = 0; i < num_pages; ++i)
  100. page_map[i] = pfn_to_page(phys_addr_map[i] >> PAGE_SHIFT);
  101. addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
  102. vfree(page_map);
  103. return addr;
  104. }
  105. static inline unsigned long
  106. drm_follow_page (void *vaddr)
  107. {
  108. pgd_t *pgd = pgd_offset_k((unsigned long) vaddr);
  109. pud_t *pud = pud_offset(pgd, (unsigned long) vaddr);
  110. pmd_t *pmd = pmd_offset(pud, (unsigned long) vaddr);
  111. pte_t *ptep = pte_offset_kernel(pmd, (unsigned long) vaddr);
  112. return pte_pfn(*ptep) << PAGE_SHIFT;
  113. }
  114. #else /* __OS_HAS_AGP */
  115. static inline drm_map_t *drm_lookup_map(unsigned long offset, unsigned long size, drm_device_t *dev)
  116. {
  117. return NULL;
  118. }
  119. static inline void *agp_remap(unsigned long offset, unsigned long size, drm_device_t *dev)
  120. {
  121. return NULL;
  122. }
  123. static inline unsigned long drm_follow_page (void *vaddr)
  124. {
  125. return 0;
  126. }
  127. #endif
  128. static inline void *drm_ioremap(unsigned long offset, unsigned long size, drm_device_t *dev)
  129. {
  130. if (drm_core_has_AGP(dev) && dev->agp && dev->agp->cant_use_aperture) {
  131. drm_map_t *map = drm_lookup_map(offset, size, dev);
  132. if (map && map->type == _DRM_AGP)
  133. return agp_remap(offset, size, dev);
  134. }
  135. return ioremap(offset, size);
  136. }
  137. static inline void *drm_ioremap_nocache(unsigned long offset, unsigned long size,
  138. drm_device_t *dev)
  139. {
  140. if (drm_core_has_AGP(dev) && dev->agp && dev->agp->cant_use_aperture) {
  141. drm_map_t *map = drm_lookup_map(offset, size, dev);
  142. if (map && map->type == _DRM_AGP)
  143. return agp_remap(offset, size, dev);
  144. }
  145. return ioremap_nocache(offset, size);
  146. }
  147. static inline void drm_ioremapfree(void *pt, unsigned long size, drm_device_t *dev)
  148. {
  149. /*
  150. * This is a bit ugly. It would be much cleaner if the DRM API would use separate
  151. * routines for handling mappings in the AGP space. Hopefully this can be done in
  152. * a future revision of the interface...
  153. */
  154. if (drm_core_has_AGP(dev) && dev->agp && dev->agp->cant_use_aperture
  155. && ((unsigned long) pt >= VMALLOC_START && (unsigned long) pt < VMALLOC_END))
  156. {
  157. unsigned long offset;
  158. drm_map_t *map;
  159. offset = drm_follow_page(pt) | ((unsigned long) pt & ~PAGE_MASK);
  160. map = drm_lookup_map(offset, size, dev);
  161. if (map && map->type == _DRM_AGP) {
  162. vunmap(pt);
  163. return;
  164. }
  165. }
  166. iounmap(pt);
  167. }