drm_memory.c 4.3 KB

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