Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / uts / common / io / drm / drm_scatter.c
blob4d1b54e862851022c52ca9318e6bc2c231ed29c3
1 /*
2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 /* BEGIN CSTYLED */
8 /* drm_scatter.h -- IOCTLs to manage scatter/gather memory -*- linux-c -*-
9 * Created: Mon Dec 18 23:20:54 2000 by gareth@valinux.com */
10 /*-
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
23 * Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
33 * Authors:
34 * Gareth Hughes <gareth@valinux.com>
35 * Eric Anholt <anholt@FreeBSD.org>
38 /* END CSTYLED */
40 #include "drmP.h"
41 #include <sys/gfx_private.h>
42 #include "drm_io32.h"
44 #define DEBUG_SCATTER 0
46 #ifdef _LP64
47 #define ScatterHandle(x) (unsigned int)((x >> 32) + (x & ((1L << 32) - 1)))
48 #else
49 #define ScatterHandle(x) (unsigned int)(x)
50 #endif
52 void
53 drm_sg_cleanup(drm_device_t *dev, drm_sg_mem_t *entry)
55 int pages = entry->pages;
57 if (entry->busaddr) {
58 kmem_free(entry->busaddr, sizeof (*entry->busaddr) * pages);
59 entry->busaddr = NULL;
62 ASSERT(entry->umem_cookie == NULL);
64 if (entry->dmah_sg) {
65 drm_pci_free(dev, entry->dmah_sg);
66 entry->dmah_sg = NULL;
69 if (entry->dmah_gart) {
70 drm_pci_free(dev, entry->dmah_gart);
71 entry->dmah_gart = NULL;
74 if (entry) {
75 drm_free(entry, sizeof (drm_sg_mem_t), DRM_MEM_SGLISTS);
76 entry = NULL;
80 /*ARGSUSED*/
81 int
82 drm_sg_alloc(DRM_IOCTL_ARGS)
84 DRM_DEVICE;
85 unsigned long pages;
86 drm_sg_mem_t *entry;
87 drm_dma_handle_t *dmah;
88 drm_scatter_gather_t request;
90 DRM_DEBUG("%s\n", "drm_sg_alloc");
92 if (dev->sg)
93 return (EINVAL);
95 #ifdef _MULTI_DATAMODEL
96 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
97 drm_scatter_gather_32_t request32;
99 DRM_COPYFROM_WITH_RETURN(&request32, (void *)data,
100 sizeof (request32));
101 request.size = request32.size;
102 request.handle = request32.handle;
103 } else
104 #endif
105 DRM_COPYFROM_WITH_RETURN(&request, (void *)data,
106 sizeof (request));
108 pages = btopr(request.size);
109 DRM_DEBUG("sg size=%ld pages=%ld\n", request.size, pages);
110 entry = kmem_zalloc(sizeof (*entry), KM_SLEEP);
111 entry->pages = (int)pages;
112 dmah = drm_pci_alloc(dev, ptob(pages), 4096, 0xfffffffful, pages);
113 if (dmah == NULL)
114 goto err_exit;
115 entry->busaddr = (void *)kmem_zalloc(sizeof (*entry->busaddr) *
116 pages, KM_SLEEP);
118 entry->handle = ScatterHandle((unsigned long)dmah->vaddr);
119 entry->virtual = (void *)dmah->vaddr;
120 request.handle = entry->handle;
121 entry->dmah_sg = dmah;
122 #ifdef _MULTI_DATAMODEL
123 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
124 drm_scatter_gather_32_t data32;
126 data32.size = (uint32_t)request.size;
127 data32.handle = (uint32_t)request.handle;
129 DRM_COPYTO_WITH_RETURN((void *)data, &data32,
130 sizeof (data32));
131 } else
132 #endif
133 DRM_COPYTO_WITH_RETURN((void *)data, &request,
134 sizeof (request));
136 DRM_LOCK();
137 if (dev->sg) {
138 DRM_UNLOCK();
139 drm_sg_cleanup(dev, entry);
140 return (EINVAL);
142 dev->sg = entry;
143 DRM_UNLOCK();
145 return (0);
147 err_exit:
148 drm_sg_cleanup(dev, entry);
149 return (ENOMEM);
152 /*ARGSUSED*/
154 drm_sg_free(DRM_IOCTL_ARGS)
156 DRM_DEVICE;
157 drm_scatter_gather_t request;
158 drm_sg_mem_t *entry;
160 #ifdef _MULTI_DATAMODEL
161 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
162 drm_scatter_gather_32_t request32;
164 DRM_COPYFROM_WITH_RETURN(&request32, (void *)data,
165 sizeof (request32));
166 request.size = request32.size;
167 request.handle = request32.handle;
168 } else
169 #endif
170 DRM_COPYFROM_WITH_RETURN(&request, (void *)data,
171 sizeof (request));
173 DRM_LOCK();
174 entry = dev->sg;
175 dev->sg = NULL;
176 DRM_UNLOCK();
178 if (!entry || entry->handle != request.handle)
179 return (EINVAL);
181 drm_sg_cleanup(dev, entry);
183 return (0);