blob: 4d1b54e862851022c52ca9318e6bc2c231ed29c3 [file] [log] [blame]
kz15163460405de2006-09-28 01:41:18 -07001/*
cg149915d0538f62008-01-09 19:45:15 -08002 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
kz15163460405de2006-09-28 01:41:18 -07003 * Use is subject to license terms.
4 */
5
6/* BEGIN CSTYLED */
7
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.
13 *
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:
20 *
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.
24 *
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.
32 *
33 * Authors:
34 * Gareth Hughes <gareth@valinux.com>
35 * Eric Anholt <anholt@FreeBSD.org>
36 *
37 */
38/* END CSTYLED */
39
kz15163460405de2006-09-28 01:41:18 -070040#include "drmP.h"
Gordon Rossdc1b2692016-10-09 11:32:43 -040041#include <sys/gfx_private.h>
cg149915d0538f62008-01-09 19:45:15 -080042#include "drm_io32.h"
kz15163460405de2006-09-28 01:41:18 -070043
44#define DEBUG_SCATTER 0
45
cg149915d0538f62008-01-09 19:45:15 -080046#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
51
kz15163460405de2006-09-28 01:41:18 -070052void
cg149915d0538f62008-01-09 19:45:15 -080053drm_sg_cleanup(drm_device_t *dev, drm_sg_mem_t *entry)
kz15163460405de2006-09-28 01:41:18 -070054{
cg149915d0538f62008-01-09 19:45:15 -080055 int pages = entry->pages;
56
kz15163460405de2006-09-28 01:41:18 -070057 if (entry->busaddr) {
cg149915d0538f62008-01-09 19:45:15 -080058 kmem_free(entry->busaddr, sizeof (*entry->busaddr) * pages);
kz15163460405de2006-09-28 01:41:18 -070059 entry->busaddr = NULL;
60 }
cg149915d0538f62008-01-09 19:45:15 -080061
62 ASSERT(entry->umem_cookie == NULL);
63
64 if (entry->dmah_sg) {
65 drm_pci_free(dev, entry->dmah_sg);
66 entry->dmah_sg = NULL;
kz15163460405de2006-09-28 01:41:18 -070067 }
68
cg149915d0538f62008-01-09 19:45:15 -080069 if (entry->dmah_gart) {
70 drm_pci_free(dev, entry->dmah_gart);
71 entry->dmah_gart = NULL;
72 }
73
74 if (entry) {
75 drm_free(entry, sizeof (drm_sg_mem_t), DRM_MEM_SGLISTS);
76 entry = NULL;
77 }
kz15163460405de2006-09-28 01:41:18 -070078}
79
80/*ARGSUSED*/
81int
82drm_sg_alloc(DRM_IOCTL_ARGS)
83{
84 DRM_DEVICE;
kz15163460405de2006-09-28 01:41:18 -070085 unsigned long pages;
cg149915d0538f62008-01-09 19:45:15 -080086 drm_sg_mem_t *entry;
87 drm_dma_handle_t *dmah;
88 drm_scatter_gather_t request;
kz15163460405de2006-09-28 01:41:18 -070089
90 DRM_DEBUG("%s\n", "drm_sg_alloc");
91
92 if (dev->sg)
cg149915d0538f62008-01-09 19:45:15 -080093 return (EINVAL);
kz15163460405de2006-09-28 01:41:18 -070094
cg149915d0538f62008-01-09 19:45:15 -080095#ifdef _MULTI_DATAMODEL
kz15163460405de2006-09-28 01:41:18 -070096 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
cg149915d0538f62008-01-09 19:45:15 -080097 drm_scatter_gather_32_t request32;
kz15163460405de2006-09-28 01:41:18 -070098
cg149915d0538f62008-01-09 19:45:15 -080099 DRM_COPYFROM_WITH_RETURN(&request32, (void *)data,
100 sizeof (request32));
kz15163460405de2006-09-28 01:41:18 -0700101 request.size = request32.size;
102 request.handle = request32.handle;
103 } else
cg149915d0538f62008-01-09 19:45:15 -0800104#endif
105 DRM_COPYFROM_WITH_RETURN(&request, (void *)data,
106 sizeof (request));
kz15163460405de2006-09-28 01:41:18 -0700107
cg149915d0538f62008-01-09 19:45:15 -0800108 pages = btopr(request.size);
kz15163460405de2006-09-28 01:41:18 -0700109 DRM_DEBUG("sg size=%ld pages=%ld\n", request.size, pages);
cg149915d0538f62008-01-09 19:45:15 -0800110 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);
kz15163460405de2006-09-28 01:41:18 -0700117
cg149915d0538f62008-01-09 19:45:15 -0800118 entry->handle = ScatterHandle((unsigned long)dmah->vaddr);
119 entry->virtual = (void *)dmah->vaddr;
kz15163460405de2006-09-28 01:41:18 -0700120 request.handle = entry->handle;
cg149915d0538f62008-01-09 19:45:15 -0800121 entry->dmah_sg = dmah;
122#ifdef _MULTI_DATAMODEL
kz15163460405de2006-09-28 01:41:18 -0700123 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
cg149915d0538f62008-01-09 19:45:15 -0800124 drm_scatter_gather_32_t data32;
kz15163460405de2006-09-28 01:41:18 -0700125
cg149915d0538f62008-01-09 19:45:15 -0800126 data32.size = (uint32_t)request.size;
127 data32.handle = (uint32_t)request.handle;
kz15163460405de2006-09-28 01:41:18 -0700128
cg149915d0538f62008-01-09 19:45:15 -0800129 DRM_COPYTO_WITH_RETURN((void *)data, &data32,
130 sizeof (data32));
kz15163460405de2006-09-28 01:41:18 -0700131 } else
cg149915d0538f62008-01-09 19:45:15 -0800132#endif
133 DRM_COPYTO_WITH_RETURN((void *)data, &request,
134 sizeof (request));
kz15163460405de2006-09-28 01:41:18 -0700135
136 DRM_LOCK();
137 if (dev->sg) {
138 DRM_UNLOCK();
cg149915d0538f62008-01-09 19:45:15 -0800139 drm_sg_cleanup(dev, entry);
140 return (EINVAL);
kz15163460405de2006-09-28 01:41:18 -0700141 }
142 dev->sg = entry;
143 DRM_UNLOCK();
144
145 return (0);
cg149915d0538f62008-01-09 19:45:15 -0800146
147err_exit:
148 drm_sg_cleanup(dev, entry);
149 return (ENOMEM);
kz15163460405de2006-09-28 01:41:18 -0700150}
151
152/*ARGSUSED*/
153int
154drm_sg_free(DRM_IOCTL_ARGS)
155{
156 DRM_DEVICE;
157 drm_scatter_gather_t request;
158 drm_sg_mem_t *entry;
159
cg149915d0538f62008-01-09 19:45:15 -0800160#ifdef _MULTI_DATAMODEL
kz15163460405de2006-09-28 01:41:18 -0700161 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
cg149915d0538f62008-01-09 19:45:15 -0800162 drm_scatter_gather_32_t request32;
kz15163460405de2006-09-28 01:41:18 -0700163
cg149915d0538f62008-01-09 19:45:15 -0800164 DRM_COPYFROM_WITH_RETURN(&request32, (void *)data,
165 sizeof (request32));
kz15163460405de2006-09-28 01:41:18 -0700166 request.size = request32.size;
167 request.handle = request32.handle;
168 } else
cg149915d0538f62008-01-09 19:45:15 -0800169#endif
170 DRM_COPYFROM_WITH_RETURN(&request, (void *)data,
171 sizeof (request));
kz15163460405de2006-09-28 01:41:18 -0700172
173 DRM_LOCK();
174 entry = dev->sg;
175 dev->sg = NULL;
176 DRM_UNLOCK();
177
178 if (!entry || entry->handle != request.handle)
cg149915d0538f62008-01-09 19:45:15 -0800179 return (EINVAL);
kz15163460405de2006-09-28 01:41:18 -0700180
cg149915d0538f62008-01-09 19:45:15 -0800181 drm_sg_cleanup(dev, entry);
kz15163460405de2006-09-28 01:41:18 -0700182
183 return (0);
184}