drm_memory.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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)
  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_allocate_memory() */
  92. DRM_AGP_MEM *drm_alloc_agp(struct drm_device * dev, int pages, u32 type)
  93. {
  94. return drm_agp_allocate_memory(dev->agp->bridge, pages, type);
  95. }
  96. /** Wrapper around agp_free_memory() */
  97. int drm_free_agp(DRM_AGP_MEM * handle, int pages)
  98. {
  99. return drm_agp_free_memory(handle) ? 0 : -EINVAL;
  100. }
  101. EXPORT_SYMBOL(drm_free_agp);
  102. /** Wrapper around agp_bind_memory() */
  103. int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start)
  104. {
  105. return drm_agp_bind_memory(handle, start);
  106. }
  107. /** Wrapper around agp_unbind_memory() */
  108. int drm_unbind_agp(DRM_AGP_MEM * handle)
  109. {
  110. return drm_agp_unbind_memory(handle);
  111. }
  112. EXPORT_SYMBOL(drm_unbind_agp);
  113. #else /* __OS_HAS_AGP */
  114. static inline void *agp_remap(unsigned long offset, unsigned long size,
  115. struct drm_device * dev)
  116. {
  117. return NULL;
  118. }
  119. #endif /* agp */
  120. void drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
  121. {
  122. if (drm_core_has_AGP(dev) &&
  123. dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
  124. map->handle = agp_remap(map->offset, map->size, dev);
  125. else
  126. map->handle = ioremap(map->offset, map->size);
  127. }
  128. EXPORT_SYMBOL(drm_core_ioremap);
  129. void drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
  130. {
  131. if (drm_core_has_AGP(dev) &&
  132. dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
  133. map->handle = agp_remap(map->offset, map->size, dev);
  134. else
  135. map->handle = ioremap_wc(map->offset, map->size);
  136. }
  137. EXPORT_SYMBOL(drm_core_ioremap_wc);
  138. void drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
  139. {
  140. if (!map->handle || !map->size)
  141. return;
  142. if (drm_core_has_AGP(dev) &&
  143. dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
  144. vunmap(map->handle);
  145. else
  146. iounmap(map->handle);
  147. }
  148. EXPORT_SYMBOL(drm_core_ioremapfree);