drm_pci.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 Jose 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 "drmP.h"
  39. /**********************************************************************/
  40. /** \name PCI memory */
  41. /*@{*/
  42. /**
  43. * \brief Allocate a PCI consistent memory block, for DMA.
  44. */
  45. drm_dma_handle_t *drm_pci_alloc(drm_device_t * dev, size_t size, size_t align,
  46. dma_addr_t maxaddr)
  47. {
  48. drm_dma_handle_t *dmah;
  49. #ifdef DRM_DEBUG_MEMORY
  50. int area = DRM_MEM_DMA;
  51. spin_lock(&drm_mem_lock);
  52. if ((drm_ram_used >> PAGE_SHIFT)
  53. > (DRM_RAM_PERCENT * drm_ram_available) / 100) {
  54. spin_unlock(&drm_mem_lock);
  55. return 0;
  56. }
  57. spin_unlock(&drm_mem_lock);
  58. #endif
  59. /* pci_alloc_consistent only guarantees alignment to the smallest
  60. * PAGE_SIZE order which is greater than or equal to the requested size.
  61. * Return NULL here for now to make sure nobody tries for larger alignment
  62. */
  63. if (align > size)
  64. return NULL;
  65. if (pci_set_dma_mask(dev->pdev, maxaddr) != 0) {
  66. DRM_ERROR("Setting pci dma mask failed\n");
  67. return NULL;
  68. }
  69. dmah = kmalloc(sizeof(drm_dma_handle_t), GFP_KERNEL);
  70. if (!dmah)
  71. return NULL;
  72. dmah->size = size;
  73. dmah->vaddr = pci_alloc_consistent(dev->pdev, size, &dmah->busaddr);
  74. #ifdef DRM_DEBUG_MEMORY
  75. if (dmah->vaddr == NULL) {
  76. spin_lock(&drm_mem_lock);
  77. ++drm_mem_stats[area].fail_count;
  78. spin_unlock(&drm_mem_lock);
  79. kfree(dmah);
  80. return NULL;
  81. }
  82. spin_lock(&drm_mem_lock);
  83. ++drm_mem_stats[area].succeed_count;
  84. drm_mem_stats[area].bytes_allocated += size;
  85. drm_ram_used += size;
  86. spin_unlock(&drm_mem_lock);
  87. #else
  88. if (dmah->vaddr == NULL) {
  89. kfree(dmah);
  90. return NULL;
  91. }
  92. #endif
  93. memset(dmah->vaddr, 0, size);
  94. return dmah;
  95. }
  96. EXPORT_SYMBOL(drm_pci_alloc);
  97. /**
  98. * \brief Free a PCI consistent memory block with freeing its descriptor.
  99. *
  100. * This function is for internal use in the Linux-specific DRM core code.
  101. */
  102. void
  103. __drm_pci_free(drm_device_t * dev, drm_dma_handle_t *dmah)
  104. {
  105. #ifdef DRM_DEBUG_MEMORY
  106. int area = DRM_MEM_DMA;
  107. int alloc_count;
  108. int free_count;
  109. #endif
  110. if (!dmah->vaddr) {
  111. #ifdef DRM_DEBUG_MEMORY
  112. DRM_MEM_ERROR(area, "Attempt to free address 0\n");
  113. #endif
  114. } else {
  115. pci_free_consistent(dev->pdev, dmah->size, dmah->vaddr,
  116. dmah->busaddr);
  117. }
  118. #ifdef DRM_DEBUG_MEMORY
  119. spin_lock(&drm_mem_lock);
  120. free_count = ++drm_mem_stats[area].free_count;
  121. alloc_count = drm_mem_stats[area].succeed_count;
  122. drm_mem_stats[area].bytes_freed += size;
  123. drm_ram_used -= size;
  124. spin_unlock(&drm_mem_lock);
  125. if (free_count > alloc_count) {
  126. DRM_MEM_ERROR(area,
  127. "Excess frees: %d frees, %d allocs\n",
  128. free_count, alloc_count);
  129. }
  130. #endif
  131. }
  132. /**
  133. * \brief Free a PCI consistent memory block
  134. */
  135. void
  136. drm_pci_free(drm_device_t *dev, drm_dma_handle_t *dmah)
  137. {
  138. __drm_pci_free(dev, dmah);
  139. kfree(dmah);
  140. }
  141. EXPORT_SYMBOL(drm_pci_free);
  142. /*@}*/