Home | History | Annotate | Line # | Download | only in drm
drm_dma.c revision 1.1.1.2.4.2
      1  1.1.1.2.4.2  tls /**
      2  1.1.1.2.4.2  tls  * \file drm_dma.c
      3  1.1.1.2.4.2  tls  * DMA IOCTL and function support
      4  1.1.1.2.4.2  tls  *
      5  1.1.1.2.4.2  tls  * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
      6  1.1.1.2.4.2  tls  * \author Gareth Hughes <gareth (at) valinux.com>
      7  1.1.1.2.4.2  tls  */
      8  1.1.1.2.4.2  tls 
      9  1.1.1.2.4.2  tls /*
     10  1.1.1.2.4.2  tls  * Created: Fri Mar 19 14:30:16 1999 by faith (at) valinux.com
     11  1.1.1.2.4.2  tls  *
     12  1.1.1.2.4.2  tls  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
     13  1.1.1.2.4.2  tls  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
     14  1.1.1.2.4.2  tls  * All Rights Reserved.
     15  1.1.1.2.4.2  tls  *
     16  1.1.1.2.4.2  tls  * Permission is hereby granted, free of charge, to any person obtaining a
     17  1.1.1.2.4.2  tls  * copy of this software and associated documentation files (the "Software"),
     18  1.1.1.2.4.2  tls  * to deal in the Software without restriction, including without limitation
     19  1.1.1.2.4.2  tls  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     20  1.1.1.2.4.2  tls  * and/or sell copies of the Software, and to permit persons to whom the
     21  1.1.1.2.4.2  tls  * Software is furnished to do so, subject to the following conditions:
     22  1.1.1.2.4.2  tls  *
     23  1.1.1.2.4.2  tls  * The above copyright notice and this permission notice (including the next
     24  1.1.1.2.4.2  tls  * paragraph) shall be included in all copies or substantial portions of the
     25  1.1.1.2.4.2  tls  * Software.
     26  1.1.1.2.4.2  tls  *
     27  1.1.1.2.4.2  tls  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     28  1.1.1.2.4.2  tls  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     29  1.1.1.2.4.2  tls  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     30  1.1.1.2.4.2  tls  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     31  1.1.1.2.4.2  tls  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     32  1.1.1.2.4.2  tls  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     33  1.1.1.2.4.2  tls  * OTHER DEALINGS IN THE SOFTWARE.
     34  1.1.1.2.4.2  tls  */
     35  1.1.1.2.4.2  tls 
     36  1.1.1.2.4.2  tls #include <linux/export.h>
     37  1.1.1.2.4.2  tls #include <drm/drmP.h>
     38  1.1.1.2.4.2  tls 
     39  1.1.1.2.4.2  tls /**
     40  1.1.1.2.4.2  tls  * Initialize the DMA data.
     41  1.1.1.2.4.2  tls  *
     42  1.1.1.2.4.2  tls  * \param dev DRM device.
     43  1.1.1.2.4.2  tls  * \return zero on success or a negative value on failure.
     44  1.1.1.2.4.2  tls  *
     45  1.1.1.2.4.2  tls  * Allocate and initialize a drm_device_dma structure.
     46  1.1.1.2.4.2  tls  */
     47  1.1.1.2.4.2  tls int drm_legacy_dma_setup(struct drm_device *dev)
     48  1.1.1.2.4.2  tls {
     49  1.1.1.2.4.2  tls 	int i;
     50  1.1.1.2.4.2  tls 
     51  1.1.1.2.4.2  tls 	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA) ||
     52  1.1.1.2.4.2  tls 	    drm_core_check_feature(dev, DRIVER_MODESET)) {
     53  1.1.1.2.4.2  tls 		return 0;
     54  1.1.1.2.4.2  tls 	}
     55  1.1.1.2.4.2  tls 
     56  1.1.1.2.4.2  tls 	dev->buf_use = 0;
     57  1.1.1.2.4.2  tls 	atomic_set(&dev->buf_alloc, 0);
     58  1.1.1.2.4.2  tls 
     59  1.1.1.2.4.2  tls 	dev->dma = kzalloc(sizeof(*dev->dma), GFP_KERNEL);
     60  1.1.1.2.4.2  tls 	if (!dev->dma)
     61  1.1.1.2.4.2  tls 		return -ENOMEM;
     62  1.1.1.2.4.2  tls 
     63  1.1.1.2.4.2  tls 	for (i = 0; i <= DRM_MAX_ORDER; i++)
     64  1.1.1.2.4.2  tls 		memset(&dev->dma->bufs[i], 0, sizeof(dev->dma->bufs[0]));
     65  1.1.1.2.4.2  tls 
     66  1.1.1.2.4.2  tls 	return 0;
     67  1.1.1.2.4.2  tls }
     68  1.1.1.2.4.2  tls 
     69  1.1.1.2.4.2  tls /**
     70  1.1.1.2.4.2  tls  * Cleanup the DMA resources.
     71  1.1.1.2.4.2  tls  *
     72  1.1.1.2.4.2  tls  * \param dev DRM device.
     73  1.1.1.2.4.2  tls  *
     74  1.1.1.2.4.2  tls  * Free all pages associated with DMA buffers, the buffers and pages lists, and
     75  1.1.1.2.4.2  tls  * finally the drm_device::dma structure itself.
     76  1.1.1.2.4.2  tls  */
     77  1.1.1.2.4.2  tls void drm_legacy_dma_takedown(struct drm_device *dev)
     78  1.1.1.2.4.2  tls {
     79  1.1.1.2.4.2  tls 	struct drm_device_dma *dma = dev->dma;
     80  1.1.1.2.4.2  tls 	int i, j;
     81  1.1.1.2.4.2  tls 
     82  1.1.1.2.4.2  tls 	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA) ||
     83  1.1.1.2.4.2  tls 	    drm_core_check_feature(dev, DRIVER_MODESET)) {
     84  1.1.1.2.4.2  tls 		return;
     85  1.1.1.2.4.2  tls 	}
     86  1.1.1.2.4.2  tls 
     87  1.1.1.2.4.2  tls 	if (!dma)
     88  1.1.1.2.4.2  tls 		return;
     89  1.1.1.2.4.2  tls 
     90  1.1.1.2.4.2  tls 	/* Clear dma buffers */
     91  1.1.1.2.4.2  tls 	for (i = 0; i <= DRM_MAX_ORDER; i++) {
     92  1.1.1.2.4.2  tls 		if (dma->bufs[i].seg_count) {
     93  1.1.1.2.4.2  tls 			DRM_DEBUG("order %d: buf_count = %d,"
     94  1.1.1.2.4.2  tls 				  " seg_count = %d\n",
     95  1.1.1.2.4.2  tls 				  i,
     96  1.1.1.2.4.2  tls 				  dma->bufs[i].buf_count,
     97  1.1.1.2.4.2  tls 				  dma->bufs[i].seg_count);
     98  1.1.1.2.4.2  tls 			for (j = 0; j < dma->bufs[i].seg_count; j++) {
     99  1.1.1.2.4.2  tls 				if (dma->bufs[i].seglist[j]) {
    100  1.1.1.2.4.2  tls 					drm_pci_free(dev, dma->bufs[i].seglist[j]);
    101  1.1.1.2.4.2  tls 				}
    102  1.1.1.2.4.2  tls 			}
    103  1.1.1.2.4.2  tls 			kfree(dma->bufs[i].seglist);
    104  1.1.1.2.4.2  tls 		}
    105  1.1.1.2.4.2  tls 		if (dma->bufs[i].buf_count) {
    106  1.1.1.2.4.2  tls 			for (j = 0; j < dma->bufs[i].buf_count; j++) {
    107  1.1.1.2.4.2  tls 				kfree(dma->bufs[i].buflist[j].dev_private);
    108  1.1.1.2.4.2  tls 			}
    109  1.1.1.2.4.2  tls 			kfree(dma->bufs[i].buflist);
    110  1.1.1.2.4.2  tls 		}
    111  1.1.1.2.4.2  tls 	}
    112  1.1.1.2.4.2  tls 
    113  1.1.1.2.4.2  tls 	kfree(dma->buflist);
    114  1.1.1.2.4.2  tls 	kfree(dma->pagelist);
    115  1.1.1.2.4.2  tls 	kfree(dev->dma);
    116  1.1.1.2.4.2  tls 	dev->dma = NULL;
    117  1.1.1.2.4.2  tls }
    118  1.1.1.2.4.2  tls 
    119  1.1.1.2.4.2  tls /**
    120  1.1.1.2.4.2  tls  * Free a buffer.
    121  1.1.1.2.4.2  tls  *
    122  1.1.1.2.4.2  tls  * \param dev DRM device.
    123  1.1.1.2.4.2  tls  * \param buf buffer to free.
    124  1.1.1.2.4.2  tls  *
    125  1.1.1.2.4.2  tls  * Resets the fields of \p buf.
    126  1.1.1.2.4.2  tls  */
    127  1.1.1.2.4.2  tls void drm_free_buffer(struct drm_device *dev, struct drm_buf * buf)
    128  1.1.1.2.4.2  tls {
    129  1.1.1.2.4.2  tls 	if (!buf)
    130  1.1.1.2.4.2  tls 		return;
    131  1.1.1.2.4.2  tls 
    132  1.1.1.2.4.2  tls 	buf->waiting = 0;
    133  1.1.1.2.4.2  tls 	buf->pending = 0;
    134  1.1.1.2.4.2  tls 	buf->file_priv = NULL;
    135  1.1.1.2.4.2  tls 	buf->used = 0;
    136  1.1.1.2.4.2  tls }
    137  1.1.1.2.4.2  tls 
    138  1.1.1.2.4.2  tls /**
    139  1.1.1.2.4.2  tls  * Reclaim the buffers.
    140  1.1.1.2.4.2  tls  *
    141  1.1.1.2.4.2  tls  * \param file_priv DRM file private.
    142  1.1.1.2.4.2  tls  *
    143  1.1.1.2.4.2  tls  * Frees each buffer associated with \p file_priv not already on the hardware.
    144  1.1.1.2.4.2  tls  */
    145  1.1.1.2.4.2  tls void drm_core_reclaim_buffers(struct drm_device *dev,
    146  1.1.1.2.4.2  tls 			      struct drm_file *file_priv)
    147  1.1.1.2.4.2  tls {
    148  1.1.1.2.4.2  tls 	struct drm_device_dma *dma = dev->dma;
    149  1.1.1.2.4.2  tls 	int i;
    150  1.1.1.2.4.2  tls 
    151  1.1.1.2.4.2  tls 	if (!dma)
    152  1.1.1.2.4.2  tls 		return;
    153  1.1.1.2.4.2  tls 	for (i = 0; i < dma->buf_count; i++) {
    154  1.1.1.2.4.2  tls 		if (dma->buflist[i]->file_priv == file_priv) {
    155  1.1.1.2.4.2  tls 			switch (dma->buflist[i]->list) {
    156  1.1.1.2.4.2  tls 			case DRM_LIST_NONE:
    157  1.1.1.2.4.2  tls 				drm_free_buffer(dev, dma->buflist[i]);
    158  1.1.1.2.4.2  tls 				break;
    159  1.1.1.2.4.2  tls 			case DRM_LIST_WAIT:
    160  1.1.1.2.4.2  tls 				dma->buflist[i]->list = DRM_LIST_RECLAIM;
    161  1.1.1.2.4.2  tls 				break;
    162  1.1.1.2.4.2  tls 			default:
    163  1.1.1.2.4.2  tls 				/* Buffer already on hardware. */
    164  1.1.1.2.4.2  tls 				break;
    165  1.1.1.2.4.2  tls 			}
    166  1.1.1.2.4.2  tls 		}
    167  1.1.1.2.4.2  tls 	}
    168  1.1.1.2.4.2  tls }
    169  1.1.1.2.4.2  tls 
    170  1.1.1.2.4.2  tls EXPORT_SYMBOL(drm_core_reclaim_buffers);
    171