drm_memory.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * \file drm_memory.h
  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/config.h>
  35. #include <linux/highmem.h>
  36. #include "drmP.h"
  37. #ifdef DEBUG_MEMORY
  38. #include "drm_memory_debug.h"
  39. #else
  40. /** No-op. */
  41. void drm_mem_init(void)
  42. {
  43. }
  44. /**
  45. * Called when "/proc/dri/%dev%/mem" is read.
  46. *
  47. * \param buf output buffer.
  48. * \param start start of output data.
  49. * \param offset requested start offset.
  50. * \param len requested number of bytes.
  51. * \param eof whether there is no more data to return.
  52. * \param data private data.
  53. * \return number of written bytes.
  54. *
  55. * No-op.
  56. */
  57. int drm_mem_info(char *buf, char **start, off_t offset,
  58. int len, int *eof, void *data)
  59. {
  60. return 0;
  61. }
  62. /** Wrapper around kmalloc() and kfree() */
  63. void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
  64. {
  65. void *pt;
  66. if (!(pt = kmalloc(size, GFP_KERNEL))) return NULL;
  67. if (oldpt && oldsize) {
  68. memcpy(pt, oldpt, oldsize);
  69. kfree(oldpt);
  70. }
  71. return pt;
  72. }
  73. /**
  74. * Allocate pages.
  75. *
  76. * \param order size order.
  77. * \param area memory area. (Not used.)
  78. * \return page address on success, or zero on failure.
  79. *
  80. * Allocate and reserve free pages.
  81. */
  82. unsigned long drm_alloc_pages(int order, int area)
  83. {
  84. unsigned long address;
  85. unsigned long bytes = PAGE_SIZE << order;
  86. unsigned long addr;
  87. unsigned int sz;
  88. address = __get_free_pages(GFP_KERNEL, order);
  89. if (!address)
  90. return 0;
  91. /* Zero */
  92. memset((void *)address, 0, bytes);
  93. /* Reserve */
  94. for (addr = address, sz = bytes;
  95. sz > 0;
  96. addr += PAGE_SIZE, sz -= PAGE_SIZE) {
  97. SetPageReserved(virt_to_page(addr));
  98. }
  99. return address;
  100. }
  101. /**
  102. * Free pages.
  103. *
  104. * \param address address of the pages to free.
  105. * \param order size order.
  106. * \param area memory area. (Not used.)
  107. *
  108. * Unreserve and free pages allocated by alloc_pages().
  109. */
  110. void drm_free_pages(unsigned long address, int order, int area)
  111. {
  112. unsigned long bytes = PAGE_SIZE << order;
  113. unsigned long addr;
  114. unsigned int sz;
  115. if (!address)
  116. return;
  117. /* Unreserve */
  118. for (addr = address, sz = bytes;
  119. sz > 0;
  120. addr += PAGE_SIZE, sz -= PAGE_SIZE) {
  121. ClearPageReserved(virt_to_page(addr));
  122. }
  123. free_pages(address, order);
  124. }
  125. #if __OS_HAS_AGP
  126. /** Wrapper around agp_allocate_memory() */
  127. DRM_AGP_MEM *drm_alloc_agp(drm_device_t *dev, int pages, u32 type)
  128. {
  129. return drm_agp_allocate_memory(dev->agp->bridge, pages, type);
  130. }
  131. EXPORT_SYMBOL(drm_alloc_agp);
  132. /** Wrapper around agp_free_memory() */
  133. int drm_free_agp(DRM_AGP_MEM *handle, int pages)
  134. {
  135. return drm_agp_free_memory(handle) ? 0 : -EINVAL;
  136. }
  137. EXPORT_SYMBOL(drm_free_agp);
  138. /** Wrapper around agp_bind_memory() */
  139. int drm_bind_agp(DRM_AGP_MEM *handle, unsigned int start)
  140. {
  141. return drm_agp_bind_memory(handle, start);
  142. }
  143. EXPORT_SYMBOL(drm_bind_agp);
  144. /** Wrapper around agp_unbind_memory() */
  145. int drm_unbind_agp(DRM_AGP_MEM *handle)
  146. {
  147. return drm_agp_unbind_memory(handle);
  148. }
  149. EXPORT_SYMBOL(drm_unbind_agp);
  150. #endif /* agp */
  151. #endif /* debug_memory */