drm_memory.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. static void *agp_remap(unsigned long offset, unsigned long size,
  75. struct drm_device * dev)
  76. {
  77. unsigned long *phys_addr_map, i, num_pages =
  78. PAGE_ALIGN(size) / PAGE_SIZE;
  79. struct drm_agp_mem *agpmem;
  80. struct page **page_map;
  81. void *addr;
  82. size = PAGE_ALIGN(size);
  83. #ifdef __alpha__
  84. offset -= dev->hose->mem_space->start;
  85. #endif
  86. list_for_each_entry(agpmem, &dev->agp->memory, head)
  87. if (agpmem->bound <= offset
  88. && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >=
  89. (offset + size))
  90. break;
  91. if (!agpmem)
  92. return NULL;
  93. /*
  94. * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
  95. * the CPU do not get remapped by the GART. We fix this by using the kernel's
  96. * page-table instead (that's probably faster anyhow...).
  97. */
  98. /* note: use vmalloc() because num_pages could be large... */
  99. page_map = vmalloc(num_pages * sizeof(struct page *));
  100. if (!page_map)
  101. return NULL;
  102. phys_addr_map =
  103. agpmem->memory->memory + (offset - agpmem->bound) / PAGE_SIZE;
  104. for (i = 0; i < num_pages; ++i)
  105. page_map[i] = pfn_to_page(phys_addr_map[i] >> PAGE_SHIFT);
  106. addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
  107. vfree(page_map);
  108. return addr;
  109. }
  110. /** Wrapper around agp_allocate_memory() */
  111. DRM_AGP_MEM *drm_alloc_agp(struct drm_device * dev, int pages, u32 type)
  112. {
  113. return drm_agp_allocate_memory(dev->agp->bridge, pages, type);
  114. }
  115. /** Wrapper around agp_free_memory() */
  116. int drm_free_agp(DRM_AGP_MEM * handle, int pages)
  117. {
  118. return drm_agp_free_memory(handle) ? 0 : -EINVAL;
  119. }
  120. EXPORT_SYMBOL(drm_free_agp);
  121. /** Wrapper around agp_bind_memory() */
  122. int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start)
  123. {
  124. return drm_agp_bind_memory(handle, start);
  125. }
  126. /** Wrapper around agp_unbind_memory() */
  127. int drm_unbind_agp(DRM_AGP_MEM * handle)
  128. {
  129. return drm_agp_unbind_memory(handle);
  130. }
  131. EXPORT_SYMBOL(drm_unbind_agp);
  132. #else /* __OS_HAS_AGP */
  133. static inline void *agp_remap(unsigned long offset, unsigned long size,
  134. struct drm_device * dev)
  135. {
  136. return NULL;
  137. }
  138. #endif /* agp */
  139. #endif /* debug_memory */
  140. void drm_core_ioremap(struct drm_map *map, struct drm_device *dev)
  141. {
  142. if (drm_core_has_AGP(dev) &&
  143. dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
  144. map->handle = agp_remap(map->offset, map->size, dev);
  145. else
  146. map->handle = ioremap(map->offset, map->size);
  147. }
  148. EXPORT_SYMBOL(drm_core_ioremap);
  149. void drm_core_ioremap_wc(struct drm_map *map, struct drm_device *dev)
  150. {
  151. map->handle = ioremap_wc(map->offset, map->size);
  152. }
  153. EXPORT_SYMBOL(drm_core_ioremap_wc);
  154. void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev)
  155. {
  156. if (!map->handle || !map->size)
  157. return;
  158. if (drm_core_has_AGP(dev) &&
  159. dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
  160. vunmap(map->handle);
  161. else
  162. iounmap(map->handle);
  163. }
  164. EXPORT_SYMBOL(drm_core_ioremapfree);