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