drm_memory.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /**
  37. * Called when "/proc/dri/%dev%/mem" is read.
  38. *
  39. * \param buf output buffer.
  40. * \param start start of output data.
  41. * \param offset requested start offset.
  42. * \param len requested number of bytes.
  43. * \param eof whether there is no more data to return.
  44. * \param data private data.
  45. * \return number of written bytes.
  46. *
  47. * No-op.
  48. */
  49. int drm_mem_info(char *buf, char **start, off_t offset,
  50. int len, int *eof, void *data)
  51. {
  52. return 0;
  53. }
  54. #if __OS_HAS_AGP
  55. static void *agp_remap(unsigned long offset, unsigned long size,
  56. struct drm_device * dev)
  57. {
  58. unsigned long i, num_pages =
  59. PAGE_ALIGN(size) / PAGE_SIZE;
  60. struct drm_agp_mem *agpmem;
  61. struct page **page_map;
  62. struct page **phys_page_map;
  63. void *addr;
  64. size = PAGE_ALIGN(size);
  65. #ifdef __alpha__
  66. offset -= dev->hose->mem_space->start;
  67. #endif
  68. list_for_each_entry(agpmem, &dev->agp->memory, head)
  69. if (agpmem->bound <= offset
  70. && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >=
  71. (offset + size))
  72. break;
  73. if (&agpmem->head == &dev->agp->memory)
  74. return NULL;
  75. /*
  76. * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
  77. * the CPU do not get remapped by the GART. We fix this by using the kernel's
  78. * page-table instead (that's probably faster anyhow...).
  79. */
  80. /* note: use vmalloc() because num_pages could be large... */
  81. page_map = vmalloc(num_pages * sizeof(struct page *));
  82. if (!page_map)
  83. return NULL;
  84. phys_page_map = (agpmem->memory->pages + (offset - agpmem->bound) / PAGE_SIZE);
  85. for (i = 0; i < num_pages; ++i)
  86. page_map[i] = phys_page_map[i];
  87. addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
  88. vfree(page_map);
  89. return addr;
  90. }
  91. /** Wrapper around agp_free_memory() */
  92. void drm_free_agp(DRM_AGP_MEM * handle, int pages)
  93. {
  94. agp_free_memory(handle);
  95. }
  96. EXPORT_SYMBOL(drm_free_agp);
  97. /** Wrapper around agp_bind_memory() */
  98. int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start)
  99. {
  100. return agp_bind_memory(handle, start);
  101. }
  102. /** Wrapper around agp_unbind_memory() */
  103. int drm_unbind_agp(DRM_AGP_MEM * handle)
  104. {
  105. return agp_unbind_memory(handle);
  106. }
  107. EXPORT_SYMBOL(drm_unbind_agp);
  108. #else /* __OS_HAS_AGP */
  109. static inline void *agp_remap(unsigned long offset, unsigned long size,
  110. struct drm_device * dev)
  111. {
  112. return NULL;
  113. }
  114. #endif /* agp */
  115. void drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
  116. {
  117. if (drm_core_has_AGP(dev) &&
  118. dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
  119. map->handle = agp_remap(map->offset, map->size, dev);
  120. else
  121. map->handle = ioremap(map->offset, map->size);
  122. }
  123. EXPORT_SYMBOL(drm_core_ioremap);
  124. void drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
  125. {
  126. if (drm_core_has_AGP(dev) &&
  127. dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
  128. map->handle = agp_remap(map->offset, map->size, dev);
  129. else
  130. map->handle = ioremap_wc(map->offset, map->size);
  131. }
  132. EXPORT_SYMBOL(drm_core_ioremap_wc);
  133. void drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
  134. {
  135. if (!map->handle || !map->size)
  136. return;
  137. if (drm_core_has_AGP(dev) &&
  138. dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
  139. vunmap(map->handle);
  140. else
  141. iounmap(map->handle);
  142. }
  143. EXPORT_SYMBOL(drm_core_ioremapfree);