drm_memory.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * \file drm_memory.c
  3. * Memory management wrappers for DRM
  4. *
  5. * \author Rickard E. (Rik) Faith <[email protected]>
  6. * \author Gareth Hughes <[email protected]>
  7. */
  8. /*
  9. * Created: Thu Feb 4 14:00:34 1999 by [email protected]
  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/export.h>
  35. #include <linux/highmem.h>
  36. #include <linux/pci.h>
  37. #include <linux/vmalloc.h>
  38. #include <drm/drm_cache.h>
  39. #include <drm/drm_device.h>
  40. #include "drm_legacy.h"
  41. #if IS_ENABLED(CONFIG_AGP)
  42. #ifdef HAVE_PAGE_AGP
  43. # include <asm/agp.h>
  44. #else
  45. # ifdef __powerpc__
  46. # define PAGE_AGP pgprot_noncached_wc(PAGE_KERNEL)
  47. # else
  48. # define PAGE_AGP PAGE_KERNEL
  49. # endif
  50. #endif
  51. static void *agp_remap(unsigned long offset, unsigned long size,
  52. struct drm_device *dev)
  53. {
  54. unsigned long i, num_pages =
  55. PAGE_ALIGN(size) / PAGE_SIZE;
  56. struct drm_agp_mem *agpmem;
  57. struct page **page_map;
  58. struct page **phys_page_map;
  59. void *addr;
  60. size = PAGE_ALIGN(size);
  61. #ifdef __alpha__
  62. offset -= dev->hose->mem_space->start;
  63. #endif
  64. list_for_each_entry(agpmem, &dev->agp->memory, head)
  65. if (agpmem->bound <= offset
  66. && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >=
  67. (offset + size))
  68. break;
  69. if (&agpmem->head == &dev->agp->memory)
  70. return NULL;
  71. /*
  72. * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
  73. * the CPU do not get remapped by the GART. We fix this by using the kernel's
  74. * page-table instead (that's probably faster anyhow...).
  75. */
  76. /* note: use vmalloc() because num_pages could be large... */
  77. page_map = vmalloc(array_size(num_pages, sizeof(struct page *)));
  78. if (!page_map)
  79. return NULL;
  80. phys_page_map = (agpmem->memory->pages + (offset - agpmem->bound) / PAGE_SIZE);
  81. for (i = 0; i < num_pages; ++i)
  82. page_map[i] = phys_page_map[i];
  83. addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
  84. vfree(page_map);
  85. return addr;
  86. }
  87. #else /* CONFIG_AGP */
  88. static inline void *agp_remap(unsigned long offset, unsigned long size,
  89. struct drm_device *dev)
  90. {
  91. return NULL;
  92. }
  93. #endif /* CONFIG_AGP */
  94. void drm_legacy_ioremap(struct drm_local_map *map, struct drm_device *dev)
  95. {
  96. if (dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
  97. map->handle = agp_remap(map->offset, map->size, dev);
  98. else
  99. map->handle = ioremap(map->offset, map->size);
  100. }
  101. EXPORT_SYMBOL(drm_legacy_ioremap);
  102. void drm_legacy_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
  103. {
  104. if (dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
  105. map->handle = agp_remap(map->offset, map->size, dev);
  106. else
  107. map->handle = ioremap_wc(map->offset, map->size);
  108. }
  109. EXPORT_SYMBOL(drm_legacy_ioremap_wc);
  110. void drm_legacy_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
  111. {
  112. if (!map->handle || !map->size)
  113. return;
  114. if (dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
  115. vunmap(map->handle);
  116. else
  117. iounmap(map->handle);
  118. }
  119. EXPORT_SYMBOL(drm_legacy_ioremapfree);