drm_dma.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * \file drm_dma.h
  3. * DMA IOCTL and function support
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
  10. *
  11. * Copyright 1999, 2000 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 "drmP.h"
  35. /**
  36. * Initialize the DMA data.
  37. *
  38. * \param dev DRM device.
  39. * \return zero on success or a negative value on failure.
  40. *
  41. * Allocate and initialize a drm_device_dma structure.
  42. */
  43. int drm_dma_setup( drm_device_t *dev )
  44. {
  45. int i;
  46. dev->dma = drm_alloc( sizeof(*dev->dma), DRM_MEM_DRIVER );
  47. if ( !dev->dma )
  48. return -ENOMEM;
  49. memset( dev->dma, 0, sizeof(*dev->dma) );
  50. for ( i = 0 ; i <= DRM_MAX_ORDER ; i++ )
  51. memset(&dev->dma->bufs[i], 0, sizeof(dev->dma->bufs[0]));
  52. return 0;
  53. }
  54. /**
  55. * Cleanup the DMA resources.
  56. *
  57. * \param dev DRM device.
  58. *
  59. * Free all pages associated with DMA buffers, the buffers and pages lists, and
  60. * finally the the drm_device::dma structure itself.
  61. */
  62. void drm_dma_takedown(drm_device_t *dev)
  63. {
  64. drm_device_dma_t *dma = dev->dma;
  65. int i, j;
  66. if (!dma) return;
  67. /* Clear dma buffers */
  68. for (i = 0; i <= DRM_MAX_ORDER; i++) {
  69. if (dma->bufs[i].seg_count) {
  70. DRM_DEBUG("order %d: buf_count = %d,"
  71. " seg_count = %d\n",
  72. i,
  73. dma->bufs[i].buf_count,
  74. dma->bufs[i].seg_count);
  75. for (j = 0; j < dma->bufs[i].seg_count; j++) {
  76. if (dma->bufs[i].seglist[j]) {
  77. drm_free_pages(dma->bufs[i].seglist[j],
  78. dma->bufs[i].page_order,
  79. DRM_MEM_DMA);
  80. }
  81. }
  82. drm_free(dma->bufs[i].seglist,
  83. dma->bufs[i].seg_count
  84. * sizeof(*dma->bufs[0].seglist),
  85. DRM_MEM_SEGS);
  86. }
  87. if (dma->bufs[i].buf_count) {
  88. for (j = 0; j < dma->bufs[i].buf_count; j++) {
  89. if (dma->bufs[i].buflist[j].dev_private) {
  90. drm_free(dma->bufs[i].buflist[j].dev_private,
  91. dma->bufs[i].buflist[j].dev_priv_size,
  92. DRM_MEM_BUFS);
  93. }
  94. }
  95. drm_free(dma->bufs[i].buflist,
  96. dma->bufs[i].buf_count *
  97. sizeof(*dma->bufs[0].buflist),
  98. DRM_MEM_BUFS);
  99. }
  100. }
  101. if (dma->buflist) {
  102. drm_free(dma->buflist,
  103. dma->buf_count * sizeof(*dma->buflist),
  104. DRM_MEM_BUFS);
  105. }
  106. if (dma->pagelist) {
  107. drm_free(dma->pagelist,
  108. dma->page_count * sizeof(*dma->pagelist),
  109. DRM_MEM_PAGES);
  110. }
  111. drm_free(dev->dma, sizeof(*dev->dma), DRM_MEM_DRIVER);
  112. dev->dma = NULL;
  113. }
  114. /**
  115. * Free a buffer.
  116. *
  117. * \param dev DRM device.
  118. * \param buf buffer to free.
  119. *
  120. * Resets the fields of \p buf.
  121. */
  122. void drm_free_buffer(drm_device_t *dev, drm_buf_t *buf)
  123. {
  124. if (!buf) return;
  125. buf->waiting = 0;
  126. buf->pending = 0;
  127. buf->filp = NULL;
  128. buf->used = 0;
  129. if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE) && waitqueue_active(&buf->dma_wait)) {
  130. wake_up_interruptible(&buf->dma_wait);
  131. }
  132. }
  133. /**
  134. * Reclaim the buffers.
  135. *
  136. * \param filp file pointer.
  137. *
  138. * Frees each buffer associated with \p filp not already on the hardware.
  139. */
  140. void drm_core_reclaim_buffers(drm_device_t *dev, struct file *filp)
  141. {
  142. drm_device_dma_t *dma = dev->dma;
  143. int i;
  144. if (!dma) return;
  145. for (i = 0; i < dma->buf_count; i++) {
  146. if (dma->buflist[i]->filp == filp) {
  147. switch (dma->buflist[i]->list) {
  148. case DRM_LIST_NONE:
  149. drm_free_buffer(dev, dma->buflist[i]);
  150. break;
  151. case DRM_LIST_WAIT:
  152. dma->buflist[i]->list = DRM_LIST_RECLAIM;
  153. break;
  154. default:
  155. /* Buffer already on hardware. */
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. EXPORT_SYMBOL(drm_core_reclaim_buffers);