drm_memory.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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() */
  63. void *drm_calloc(size_t nmemb, size_t size, int area)
  64. {
  65. void *addr;
  66. addr = kmalloc(size * nmemb, GFP_KERNEL);
  67. if (addr != NULL)
  68. memset((void *)addr, 0, size * nmemb);
  69. return addr;
  70. }
  71. EXPORT_SYMBOL(drm_calloc);
  72. /** Wrapper around kmalloc() and kfree() */
  73. void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
  74. {
  75. void *pt;
  76. if (!(pt = kmalloc(size, GFP_KERNEL))) return NULL;
  77. if (oldpt && oldsize) {
  78. memcpy(pt, oldpt, oldsize);
  79. kfree(oldpt);
  80. }
  81. return pt;
  82. }
  83. /**
  84. * Allocate pages.
  85. *
  86. * \param order size order.
  87. * \param area memory area. (Not used.)
  88. * \return page address on success, or zero on failure.
  89. *
  90. * Allocate and reserve free pages.
  91. */
  92. unsigned long drm_alloc_pages(int order, int area)
  93. {
  94. unsigned long address;
  95. unsigned long bytes = PAGE_SIZE << order;
  96. unsigned long addr;
  97. unsigned int sz;
  98. address = __get_free_pages(GFP_KERNEL, order);
  99. if (!address)
  100. return 0;
  101. /* Zero */
  102. memset((void *)address, 0, bytes);
  103. /* Reserve */
  104. for (addr = address, sz = bytes;
  105. sz > 0;
  106. addr += PAGE_SIZE, sz -= PAGE_SIZE) {
  107. SetPageReserved(virt_to_page(addr));
  108. }
  109. return address;
  110. }
  111. /**
  112. * Free pages.
  113. *
  114. * \param address address of the pages to free.
  115. * \param order size order.
  116. * \param area memory area. (Not used.)
  117. *
  118. * Unreserve and free pages allocated by alloc_pages().
  119. */
  120. void drm_free_pages(unsigned long address, int order, int area)
  121. {
  122. unsigned long bytes = PAGE_SIZE << order;
  123. unsigned long addr;
  124. unsigned int sz;
  125. if (!address)
  126. return;
  127. /* Unreserve */
  128. for (addr = address, sz = bytes;
  129. sz > 0;
  130. addr += PAGE_SIZE, sz -= PAGE_SIZE) {
  131. ClearPageReserved(virt_to_page(addr));
  132. }
  133. free_pages(address, order);
  134. }
  135. #if __OS_HAS_AGP
  136. /** Wrapper around agp_allocate_memory() */
  137. DRM_AGP_MEM *drm_alloc_agp(struct agp_bridge_data *bridge, int pages, u32 type)
  138. {
  139. return drm_agp_allocate_memory(bridge, pages, type);
  140. }
  141. /** Wrapper around agp_free_memory() */
  142. int drm_free_agp(DRM_AGP_MEM *handle, int pages)
  143. {
  144. return drm_agp_free_memory(handle) ? 0 : -EINVAL;
  145. }
  146. /** Wrapper around agp_bind_memory() */
  147. int drm_bind_agp(DRM_AGP_MEM *handle, unsigned int start)
  148. {
  149. return drm_agp_bind_memory(handle, start);
  150. }
  151. /** Wrapper around agp_unbind_memory() */
  152. int drm_unbind_agp(DRM_AGP_MEM *handle)
  153. {
  154. return drm_agp_unbind_memory(handle);
  155. }
  156. #endif /* agp */
  157. #endif /* debug_memory */