drm_dma.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * \file drm_dma.c
  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 <linux/export.h>
  35. #include <drm/drmP.h>
  36. #include "drm_legacy.h"
  37. /**
  38. * Initialize the DMA data.
  39. *
  40. * \param dev DRM device.
  41. * \return zero on success or a negative value on failure.
  42. *
  43. * Allocate and initialize a drm_device_dma structure.
  44. */
  45. int drm_legacy_dma_setup(struct drm_device *dev)
  46. {
  47. int i;
  48. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA) ||
  49. drm_core_check_feature(dev, DRIVER_MODESET)) {
  50. return 0;
  51. }
  52. dev->buf_use = 0;
  53. atomic_set(&dev->buf_alloc, 0);
  54. dev->dma = kzalloc(sizeof(*dev->dma), GFP_KERNEL);
  55. if (!dev->dma)
  56. return -ENOMEM;
  57. for (i = 0; i <= DRM_MAX_ORDER; i++)
  58. memset(&dev->dma->bufs[i], 0, sizeof(dev->dma->bufs[0]));
  59. return 0;
  60. }
  61. /**
  62. * Cleanup the DMA resources.
  63. *
  64. * \param dev DRM device.
  65. *
  66. * Free all pages associated with DMA buffers, the buffers and pages lists, and
  67. * finally the drm_device::dma structure itself.
  68. */
  69. void drm_legacy_dma_takedown(struct drm_device *dev)
  70. {
  71. struct drm_device_dma *dma = dev->dma;
  72. int i, j;
  73. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA) ||
  74. drm_core_check_feature(dev, DRIVER_MODESET)) {
  75. return;
  76. }
  77. if (!dma)
  78. return;
  79. /* Clear dma buffers */
  80. for (i = 0; i <= DRM_MAX_ORDER; i++) {
  81. if (dma->bufs[i].seg_count) {
  82. DRM_DEBUG("order %d: buf_count = %d,"
  83. " seg_count = %d\n",
  84. i,
  85. dma->bufs[i].buf_count,
  86. dma->bufs[i].seg_count);
  87. for (j = 0; j < dma->bufs[i].seg_count; j++) {
  88. if (dma->bufs[i].seglist[j]) {
  89. drm_pci_free(dev, dma->bufs[i].seglist[j]);
  90. }
  91. }
  92. kfree(dma->bufs[i].seglist);
  93. }
  94. if (dma->bufs[i].buf_count) {
  95. for (j = 0; j < dma->bufs[i].buf_count; j++) {
  96. kfree(dma->bufs[i].buflist[j].dev_private);
  97. }
  98. kfree(dma->bufs[i].buflist);
  99. }
  100. }
  101. kfree(dma->buflist);
  102. kfree(dma->pagelist);
  103. kfree(dev->dma);
  104. dev->dma = NULL;
  105. }
  106. /**
  107. * Free a buffer.
  108. *
  109. * \param dev DRM device.
  110. * \param buf buffer to free.
  111. *
  112. * Resets the fields of \p buf.
  113. */
  114. void drm_legacy_free_buffer(struct drm_device *dev, struct drm_buf * buf)
  115. {
  116. if (!buf)
  117. return;
  118. buf->waiting = 0;
  119. buf->pending = 0;
  120. buf->file_priv = NULL;
  121. buf->used = 0;
  122. }
  123. /**
  124. * Reclaim the buffers.
  125. *
  126. * \param file_priv DRM file private.
  127. *
  128. * Frees each buffer associated with \p file_priv not already on the hardware.
  129. */
  130. void drm_legacy_reclaim_buffers(struct drm_device *dev,
  131. struct drm_file *file_priv)
  132. {
  133. struct drm_device_dma *dma = dev->dma;
  134. int i;
  135. if (!dma)
  136. return;
  137. for (i = 0; i < dma->buf_count; i++) {
  138. if (dma->buflist[i]->file_priv == file_priv) {
  139. switch (dma->buflist[i]->list) {
  140. case DRM_LIST_NONE:
  141. drm_legacy_free_buffer(dev, dma->buflist[i]);
  142. break;
  143. case DRM_LIST_WAIT:
  144. dma->buflist[i]->list = DRM_LIST_RECLAIM;
  145. break;
  146. default:
  147. /* Buffer already on hardware. */
  148. break;
  149. }
  150. }
  151. }
  152. }