drm_pci.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* drm_pci.h -- PCI DMA memory management wrappers for DRM -*- linux-c -*- */
  2. /**
  3. * \file drm_pci.c
  4. * \brief Functions and ioctls to manage PCI memory
  5. *
  6. * \warning These interfaces aren't stable yet.
  7. *
  8. * \todo Implement the remaining ioctl's for the PCI pools.
  9. * \todo The wrappers here are so thin that they would be better off inlined..
  10. *
  11. * \author José Fonseca <jrfonseca@tungstengraphics.com>
  12. * \author Leif Delgass <ldelgass@retinalburn.net>
  13. */
  14. /*
  15. * Copyright 2003 José Fonseca.
  16. * Copyright 2003 Leif Delgass.
  17. * All Rights Reserved.
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a
  20. * copy of this software and associated documentation files (the "Software"),
  21. * to deal in the Software without restriction, including without limitation
  22. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  23. * and/or sell copies of the Software, and to permit persons to whom the
  24. * Software is furnished to do so, subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice (including the next
  27. * paragraph) shall be included in all copies or substantial portions of the
  28. * Software.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  31. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  32. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  33. * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  34. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  35. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. */
  37. #include <linux/pci.h>
  38. #include <linux/dma-mapping.h>
  39. #include "drmP.h"
  40. /**********************************************************************/
  41. /** \name PCI memory */
  42. /*@{*/
  43. /**
  44. * \brief Allocate a PCI consistent memory block, for DMA.
  45. */
  46. drm_dma_handle_t *drm_pci_alloc(struct drm_device * dev, size_t size, size_t align,
  47. dma_addr_t maxaddr)
  48. {
  49. drm_dma_handle_t *dmah;
  50. #if 1
  51. unsigned long addr;
  52. size_t sz;
  53. #endif
  54. #ifdef DRM_DEBUG_MEMORY
  55. int area = DRM_MEM_DMA;
  56. spin_lock(&drm_mem_lock);
  57. if ((drm_ram_used >> PAGE_SHIFT)
  58. > (DRM_RAM_PERCENT * drm_ram_available) / 100) {
  59. spin_unlock(&drm_mem_lock);
  60. return 0;
  61. }
  62. spin_unlock(&drm_mem_lock);
  63. #endif
  64. /* pci_alloc_consistent only guarantees alignment to the smallest
  65. * PAGE_SIZE order which is greater than or equal to the requested size.
  66. * Return NULL here for now to make sure nobody tries for larger alignment
  67. */
  68. if (align > size)
  69. return NULL;
  70. if (pci_set_dma_mask(dev->pdev, maxaddr) != 0) {
  71. DRM_ERROR("Setting pci dma mask failed\n");
  72. return NULL;
  73. }
  74. dmah = kmalloc(sizeof(drm_dma_handle_t), GFP_KERNEL);
  75. if (!dmah)
  76. return NULL;
  77. dmah->size = size;
  78. dmah->vaddr = dma_alloc_coherent(&dev->pdev->dev, size, &dmah->busaddr, GFP_KERNEL | __GFP_COMP);
  79. #ifdef DRM_DEBUG_MEMORY
  80. if (dmah->vaddr == NULL) {
  81. spin_lock(&drm_mem_lock);
  82. ++drm_mem_stats[area].fail_count;
  83. spin_unlock(&drm_mem_lock);
  84. kfree(dmah);
  85. return NULL;
  86. }
  87. spin_lock(&drm_mem_lock);
  88. ++drm_mem_stats[area].succeed_count;
  89. drm_mem_stats[area].bytes_allocated += size;
  90. drm_ram_used += size;
  91. spin_unlock(&drm_mem_lock);
  92. #else
  93. if (dmah->vaddr == NULL) {
  94. kfree(dmah);
  95. return NULL;
  96. }
  97. #endif
  98. memset(dmah->vaddr, 0, size);
  99. /* XXX - Is virt_to_page() legal for consistent mem? */
  100. /* Reserve */
  101. for (addr = (unsigned long)dmah->vaddr, sz = size;
  102. sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
  103. SetPageReserved(virt_to_page(addr));
  104. }
  105. return dmah;
  106. }
  107. EXPORT_SYMBOL(drm_pci_alloc);
  108. /**
  109. * \brief Free a PCI consistent memory block without freeing its descriptor.
  110. *
  111. * This function is for internal use in the Linux-specific DRM core code.
  112. */
  113. void __drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
  114. {
  115. #if 1
  116. unsigned long addr;
  117. size_t sz;
  118. #endif
  119. #ifdef DRM_DEBUG_MEMORY
  120. int area = DRM_MEM_DMA;
  121. int alloc_count;
  122. int free_count;
  123. #endif
  124. if (!dmah->vaddr) {
  125. #ifdef DRM_DEBUG_MEMORY
  126. DRM_MEM_ERROR(area, "Attempt to free address 0\n");
  127. #endif
  128. } else {
  129. /* XXX - Is virt_to_page() legal for consistent mem? */
  130. /* Unreserve */
  131. for (addr = (unsigned long)dmah->vaddr, sz = dmah->size;
  132. sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
  133. ClearPageReserved(virt_to_page(addr));
  134. }
  135. dma_free_coherent(&dev->pdev->dev, dmah->size, dmah->vaddr,
  136. dmah->busaddr);
  137. }
  138. #ifdef DRM_DEBUG_MEMORY
  139. spin_lock(&drm_mem_lock);
  140. free_count = ++drm_mem_stats[area].free_count;
  141. alloc_count = drm_mem_stats[area].succeed_count;
  142. drm_mem_stats[area].bytes_freed += size;
  143. drm_ram_used -= size;
  144. spin_unlock(&drm_mem_lock);
  145. if (free_count > alloc_count) {
  146. DRM_MEM_ERROR(area,
  147. "Excess frees: %d frees, %d allocs\n",
  148. free_count, alloc_count);
  149. }
  150. #endif
  151. }
  152. /**
  153. * \brief Free a PCI consistent memory block
  154. */
  155. void drm_pci_free(struct drm_device * dev, drm_dma_handle_t * dmah)
  156. {
  157. __drm_pci_free(dev, dmah);
  158. kfree(dmah);
  159. }
  160. EXPORT_SYMBOL(drm_pci_free);
  161. /*@}*/