drm_memory.c 4.4 KB

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