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/*-
10 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
11 *
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
36#include <sys/cdefs.h>
37#include <dev/drm2/drmP.h>
38
39/**
40 * Initialize the DMA data.
41 *
42 * \param 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 */
47int drm_dma_setup(struct drm_device *dev)
48{
49	int i;
50
51	dev->dma = malloc(sizeof(*dev->dma), DRM_MEM_DRIVER, M_NOWAIT | M_ZERO);
52	if (!dev->dma)
53		return -ENOMEM;
54
55	for (i = 0; i <= DRM_MAX_ORDER; i++)
56		memset(&dev->dma->bufs[i], 0, sizeof(dev->dma->bufs[0]));
57
58	return 0;
59}
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 */
69void drm_dma_takedown(struct drm_device *dev)
70{
71	struct drm_device_dma *dma = dev->dma;
72	int i, j;
73
74	if (!dma)
75		return;
76
77	/* Clear dma buffers */
78	for (i = 0; i <= DRM_MAX_ORDER; i++) {
79		if (dma->bufs[i].seg_count) {
80			DRM_DEBUG("order %d: buf_count = %d,"
81				  " seg_count = %d\n",
82				  i,
83				  dma->bufs[i].buf_count,
84				  dma->bufs[i].seg_count);
85			for (j = 0; j < dma->bufs[i].seg_count; j++) {
86				if (dma->bufs[i].seglist[j]) {
87					drm_pci_free(dev, dma->bufs[i].seglist[j]);
88				}
89			}
90			free(dma->bufs[i].seglist, DRM_MEM_SEGS);
91		}
92		if (dma->bufs[i].buf_count) {
93			for (j = 0; j < dma->bufs[i].buf_count; j++) {
94				free(dma->bufs[i].buflist[j].dev_private,
95				    DRM_MEM_BUFS);
96			}
97			free(dma->bufs[i].buflist, DRM_MEM_BUFS);
98		}
99	}
100
101	free(dma->buflist, DRM_MEM_BUFS);
102	free(dma->pagelist, DRM_MEM_PAGES);
103	free(dev->dma, DRM_MEM_DRIVER);
104	dev->dma = NULL;
105}
106
107/**
108 * Free a buffer.
109 *
110 * \param dev DRM device.
111 * \param buf buffer to free.
112 *
113 * Resets the fields of \p buf.
114 */
115void drm_free_buffer(struct drm_device *dev, struct drm_buf * buf)
116{
117	if (!buf)
118		return;
119
120	buf->waiting = 0;
121	buf->pending = 0;
122	buf->file_priv = NULL;
123	buf->used = 0;
124}
125
126/**
127 * Reclaim the buffers.
128 *
129 * \param file_priv DRM file private.
130 *
131 * Frees each buffer associated with \p file_priv not already on the hardware.
132 */
133void drm_core_reclaim_buffers(struct drm_device *dev,
134			      struct drm_file *file_priv)
135{
136	struct drm_device_dma *dma = dev->dma;
137	int i;
138
139	if (!dma)
140		return;
141	for (i = 0; i < dma->buf_count; i++) {
142		if (dma->buflist[i]->file_priv == file_priv) {
143			switch (dma->buflist[i]->list) {
144			case DRM_LIST_NONE:
145				drm_free_buffer(dev, dma->buflist[i]);
146				break;
147			case DRM_LIST_WAIT:
148				dma->buflist[i]->list = DRM_LIST_RECLAIM;
149				break;
150			default:
151				/* Buffer already on hardware. */
152				break;
153			}
154		}
155	}
156}
157
158EXPORT_SYMBOL(drm_core_reclaim_buffers);
159