xref: /titanic_50/usr/src/uts/common/io/drm/drm_scatter.c (revision d0538f66491267879b7418b21ad78e3dcc2dcc83)
160405de4Skz151634 /*
2*d0538f66Scg149915  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
360405de4Skz151634  * Use is subject to license terms.
460405de4Skz151634  */
560405de4Skz151634 
660405de4Skz151634 /* BEGIN CSTYLED */
760405de4Skz151634 
860405de4Skz151634 /* drm_scatter.h -- IOCTLs to manage scatter/gather memory -*- linux-c -*-
960405de4Skz151634  * Created: Mon Dec 18 23:20:54 2000 by gareth@valinux.com */
1060405de4Skz151634 /*-
1160405de4Skz151634  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
1260405de4Skz151634  * All Rights Reserved.
1360405de4Skz151634  *
1460405de4Skz151634  * Permission is hereby granted, free of charge, to any person obtaining a
1560405de4Skz151634  * copy of this software and associated documentation files (the "Software"),
1660405de4Skz151634  * to deal in the Software without restriction, including without limitation
1760405de4Skz151634  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1860405de4Skz151634  * and/or sell copies of the Software, and to permit persons to whom the
1960405de4Skz151634  * Software is furnished to do so, subject to the following conditions:
2060405de4Skz151634  *
2160405de4Skz151634  * The above copyright notice and this permission notice (including the next
2260405de4Skz151634  * paragraph) shall be included in all copies or substantial portions of the
2360405de4Skz151634  * Software.
2460405de4Skz151634  *
2560405de4Skz151634  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2660405de4Skz151634  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2760405de4Skz151634  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
2860405de4Skz151634  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2960405de4Skz151634  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
3060405de4Skz151634  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
3160405de4Skz151634  * DEALINGS IN THE SOFTWARE.
3260405de4Skz151634  *
3360405de4Skz151634  * Authors:
3460405de4Skz151634  *   Gareth Hughes <gareth@valinux.com>
3560405de4Skz151634  *   Eric Anholt <anholt@FreeBSD.org>
3660405de4Skz151634  *
3760405de4Skz151634  */
3860405de4Skz151634 /* END CSTYLED */
3960405de4Skz151634 
4060405de4Skz151634 #pragma ident	"%Z%%M%	%I%	%E% SMI"
4160405de4Skz151634 
4260405de4Skz151634 #include "drmP.h"
43*d0538f66Scg149915 #include <gfx_private.h>
44*d0538f66Scg149915 #include "drm_io32.h"
4560405de4Skz151634 
4660405de4Skz151634 #define	DEBUG_SCATTER 0
4760405de4Skz151634 
48*d0538f66Scg149915 #ifdef	_LP64
49*d0538f66Scg149915 #define	ScatterHandle(x) (unsigned int)((x >> 32) + (x & ((1L << 32) - 1)))
50*d0538f66Scg149915 #else
51*d0538f66Scg149915 #define	ScatterHandle(x) (unsigned int)(x)
52*d0538f66Scg149915 #endif
53*d0538f66Scg149915 
5460405de4Skz151634 void
drm_sg_cleanup(drm_device_t * dev,drm_sg_mem_t * entry)55*d0538f66Scg149915 drm_sg_cleanup(drm_device_t *dev, drm_sg_mem_t *entry)
5660405de4Skz151634 {
57*d0538f66Scg149915 	int	pages = entry->pages;
58*d0538f66Scg149915 
5960405de4Skz151634 	if (entry->busaddr) {
60*d0538f66Scg149915 		kmem_free(entry->busaddr, sizeof (*entry->busaddr) * pages);
6160405de4Skz151634 		entry->busaddr = NULL;
6260405de4Skz151634 	}
63*d0538f66Scg149915 
64*d0538f66Scg149915 	ASSERT(entry->umem_cookie == NULL);
65*d0538f66Scg149915 
66*d0538f66Scg149915 	if (entry->dmah_sg) {
67*d0538f66Scg149915 		drm_pci_free(dev, entry->dmah_sg);
68*d0538f66Scg149915 		entry->dmah_sg = NULL;
6960405de4Skz151634 	}
7060405de4Skz151634 
71*d0538f66Scg149915 	if (entry->dmah_gart) {
72*d0538f66Scg149915 		drm_pci_free(dev, entry->dmah_gart);
73*d0538f66Scg149915 		entry->dmah_gart = NULL;
74*d0538f66Scg149915 	}
75*d0538f66Scg149915 
76*d0538f66Scg149915 	if (entry) {
77*d0538f66Scg149915 		drm_free(entry, sizeof (drm_sg_mem_t), DRM_MEM_SGLISTS);
78*d0538f66Scg149915 		entry = NULL;
79*d0538f66Scg149915 	}
8060405de4Skz151634 }
8160405de4Skz151634 
8260405de4Skz151634 /*ARGSUSED*/
8360405de4Skz151634 int
drm_sg_alloc(DRM_IOCTL_ARGS)8460405de4Skz151634 drm_sg_alloc(DRM_IOCTL_ARGS)
8560405de4Skz151634 {
8660405de4Skz151634 	DRM_DEVICE;
8760405de4Skz151634 	unsigned long pages;
88*d0538f66Scg149915 	drm_sg_mem_t		*entry;
89*d0538f66Scg149915 	drm_dma_handle_t	*dmah;
90*d0538f66Scg149915 	drm_scatter_gather_t request;
9160405de4Skz151634 
9260405de4Skz151634 	DRM_DEBUG("%s\n", "drm_sg_alloc");
9360405de4Skz151634 
9460405de4Skz151634 	if (dev->sg)
95*d0538f66Scg149915 		return (EINVAL);
9660405de4Skz151634 
97*d0538f66Scg149915 #ifdef	_MULTI_DATAMODEL
9860405de4Skz151634 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
99*d0538f66Scg149915 		drm_scatter_gather_32_t request32;
10060405de4Skz151634 
101*d0538f66Scg149915 		DRM_COPYFROM_WITH_RETURN(&request32, (void *)data,
102*d0538f66Scg149915 		    sizeof (request32));
10360405de4Skz151634 		request.size = request32.size;
10460405de4Skz151634 		request.handle = request32.handle;
10560405de4Skz151634 	} else
106*d0538f66Scg149915 #endif
107*d0538f66Scg149915 		DRM_COPYFROM_WITH_RETURN(&request, (void *)data,
10860405de4Skz151634 		    sizeof (request));
10960405de4Skz151634 
110*d0538f66Scg149915 	pages = btopr(request.size);
11160405de4Skz151634 	DRM_DEBUG("sg size=%ld pages=%ld\n", request.size, pages);
112*d0538f66Scg149915 	entry = kmem_zalloc(sizeof (*entry), KM_SLEEP);
113*d0538f66Scg149915 	entry->pages = (int)pages;
114*d0538f66Scg149915 	dmah = drm_pci_alloc(dev, ptob(pages), 4096, 0xfffffffful, pages);
115*d0538f66Scg149915 	if (dmah == NULL)
116*d0538f66Scg149915 		goto err_exit;
117*d0538f66Scg149915 	entry->busaddr = (void *)kmem_zalloc(sizeof (*entry->busaddr) *
118*d0538f66Scg149915 	    pages, KM_SLEEP);
11960405de4Skz151634 
120*d0538f66Scg149915 	entry->handle = ScatterHandle((unsigned long)dmah->vaddr);
121*d0538f66Scg149915 	entry->virtual = (void *)dmah->vaddr;
12260405de4Skz151634 	request.handle = entry->handle;
123*d0538f66Scg149915 	entry->dmah_sg = dmah;
124*d0538f66Scg149915 #ifdef	_MULTI_DATAMODEL
12560405de4Skz151634 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
126*d0538f66Scg149915 		drm_scatter_gather_32_t data32;
12760405de4Skz151634 
128*d0538f66Scg149915 		data32.size = (uint32_t)request.size;
129*d0538f66Scg149915 		data32.handle = (uint32_t)request.handle;
13060405de4Skz151634 
131*d0538f66Scg149915 		DRM_COPYTO_WITH_RETURN((void *)data, &data32,
132*d0538f66Scg149915 		    sizeof (data32));
13360405de4Skz151634 	} else
134*d0538f66Scg149915 #endif
135*d0538f66Scg149915 		DRM_COPYTO_WITH_RETURN((void *)data, &request,
13660405de4Skz151634 		    sizeof (request));
13760405de4Skz151634 
13860405de4Skz151634 	DRM_LOCK();
13960405de4Skz151634 	if (dev->sg) {
14060405de4Skz151634 		DRM_UNLOCK();
141*d0538f66Scg149915 		drm_sg_cleanup(dev, entry);
142*d0538f66Scg149915 		return (EINVAL);
14360405de4Skz151634 	}
14460405de4Skz151634 	dev->sg = entry;
14560405de4Skz151634 	DRM_UNLOCK();
14660405de4Skz151634 
14760405de4Skz151634 	return (0);
148*d0538f66Scg149915 
149*d0538f66Scg149915 err_exit:
150*d0538f66Scg149915 	drm_sg_cleanup(dev, entry);
151*d0538f66Scg149915 	return (ENOMEM);
15260405de4Skz151634 }
15360405de4Skz151634 
15460405de4Skz151634 /*ARGSUSED*/
15560405de4Skz151634 int
drm_sg_free(DRM_IOCTL_ARGS)15660405de4Skz151634 drm_sg_free(DRM_IOCTL_ARGS)
15760405de4Skz151634 {
15860405de4Skz151634 	DRM_DEVICE;
15960405de4Skz151634 	drm_scatter_gather_t request;
16060405de4Skz151634 	drm_sg_mem_t *entry;
16160405de4Skz151634 
162*d0538f66Scg149915 #ifdef	_MULTI_DATAMODEL
16360405de4Skz151634 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
164*d0538f66Scg149915 		drm_scatter_gather_32_t request32;
16560405de4Skz151634 
166*d0538f66Scg149915 		DRM_COPYFROM_WITH_RETURN(&request32, (void *)data,
167*d0538f66Scg149915 		    sizeof (request32));
16860405de4Skz151634 		request.size = request32.size;
16960405de4Skz151634 		request.handle = request32.handle;
17060405de4Skz151634 	} else
171*d0538f66Scg149915 #endif
172*d0538f66Scg149915 		DRM_COPYFROM_WITH_RETURN(&request, (void *)data,
17360405de4Skz151634 		    sizeof (request));
17460405de4Skz151634 
17560405de4Skz151634 	DRM_LOCK();
17660405de4Skz151634 	entry = dev->sg;
17760405de4Skz151634 	dev->sg = NULL;
17860405de4Skz151634 	DRM_UNLOCK();
17960405de4Skz151634 
18060405de4Skz151634 	if (!entry || entry->handle != request.handle)
181*d0538f66Scg149915 		return (EINVAL);
18260405de4Skz151634 
183*d0538f66Scg149915 	drm_sg_cleanup(dev, entry);
18460405de4Skz151634 
18560405de4Skz151634 	return (0);
18660405de4Skz151634 }
187