drm_dma.c 4.5 KB

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