]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drm_ioctl.c
This commit was generated by cvs2svn to compensate for changes in r147001,
[FreeBSD/FreeBSD.git] / sys / dev / drm / drm_ioctl.c
1 /* drm_ioctl.h -- IOCTL processing for DRM -*- linux-c -*-
2  * Created: Fri Jan  8 09:01:26 1999 by faith@valinux.com
3  */
4 /*-
5  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
7  * All Rights Reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  *
28  * Authors:
29  *    Rickard E. (Rik) Faith <faith@valinux.com>
30  *    Gareth Hughes <gareth@valinux.com>
31  *
32  * $FreeBSD$
33  */
34
35 #include "dev/drm/drmP.h"
36
37 /*
38  * Beginning in revision 1.1 of the DRM interface, getunique will return
39  * a unique in the form pci:oooo:bb:dd.f (o=domain, b=bus, d=device, f=function)
40  * before setunique has been called.  The format for the bus-specific part of
41  * the unique is not defined for any other bus.
42  */
43 int drm_getunique(DRM_IOCTL_ARGS)
44 {
45         DRM_DEVICE;
46         drm_unique_t     u;
47
48         DRM_COPY_FROM_USER_IOCTL( u, (drm_unique_t *)data, sizeof(u) );
49
50         if (u.unique_len >= dev->unique_len) {
51                 if (DRM_COPY_TO_USER(u.unique, dev->unique, dev->unique_len))
52                         return DRM_ERR(EFAULT);
53         }
54         u.unique_len = dev->unique_len;
55
56         DRM_COPY_TO_USER_IOCTL( (drm_unique_t *)data, u, sizeof(u) );
57
58         return 0;
59 }
60
61 /* Deprecated in DRM version 1.1, and will return EBUSY when setversion has
62  * requested version 1.1 or greater.
63  */
64 int drm_setunique(DRM_IOCTL_ARGS)
65 {
66         DRM_DEVICE;
67         drm_unique_t u;
68         int domain, bus, slot, func, ret;
69         char *busid;
70
71         DRM_COPY_FROM_USER_IOCTL( u, (drm_unique_t *)data, sizeof(u) );
72
73         /* Check and copy in the submitted Bus ID */
74         if (!u.unique_len || u.unique_len > 1024)
75                 return DRM_ERR(EINVAL);
76
77         busid = malloc(u.unique_len + 1, M_DRM, M_WAITOK);
78         if (busid == NULL)
79                 return DRM_ERR(ENOMEM);
80
81         if (DRM_COPY_FROM_USER(busid, u.unique, u.unique_len)) {
82                 free(busid, M_DRM);
83                 return DRM_ERR(EFAULT);
84         }
85         busid[u.unique_len] = '\0';
86
87         /* Return error if the busid submitted doesn't match the device's actual
88          * busid.
89          */
90         ret = sscanf(busid, "PCI:%d:%d:%d", &bus, &slot, &func);
91         if (ret != 3) {
92                 free(busid, M_DRM);
93                 return DRM_ERR(EINVAL);
94         }
95         domain = bus >> 8;
96         bus &= 0xff;
97         
98         if ((domain != dev->pci_domain) ||
99             (bus != dev->pci_bus) ||
100             (slot != dev->pci_slot) ||
101             (func != dev->pci_func)) {
102                 free(busid, M_DRM);
103                 return DRM_ERR(EINVAL);
104         }
105
106         /* Actually set the device's busid now. */
107         DRM_LOCK();
108         if (dev->unique_len || dev->unique) {
109                 DRM_UNLOCK();
110                 return DRM_ERR(EBUSY);
111         }
112
113         dev->unique_len = u.unique_len;
114         dev->unique = busid;
115         DRM_UNLOCK();
116
117         return 0;
118 }
119
120
121 static int
122 drm_set_busid(drm_device_t *dev)
123 {
124
125         DRM_LOCK();
126
127         if (dev->unique != NULL) {
128                 DRM_UNLOCK();
129                 return EBUSY;
130         }
131
132         dev->unique_len = 20;
133         dev->unique = malloc(dev->unique_len + 1, M_DRM, M_NOWAIT);
134         if (dev->unique == NULL) {
135                 DRM_UNLOCK();
136                 return ENOMEM;
137         }
138
139         snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%1x",
140             dev->pci_domain, dev->pci_bus, dev->pci_slot, dev->pci_func);
141
142         DRM_UNLOCK();
143
144         return 0;
145 }
146
147 int drm_getmap(DRM_IOCTL_ARGS)
148 {
149         DRM_DEVICE;
150         drm_map_t    map;
151         drm_local_map_t    *mapinlist;
152         int          idx;
153         int          i = 0;
154
155         DRM_COPY_FROM_USER_IOCTL( map, (drm_map_t *)data, sizeof(map) );
156
157         idx = map.offset;
158
159         DRM_LOCK();
160         if (idx < 0) {
161                 DRM_UNLOCK();
162                 return DRM_ERR(EINVAL);
163         }
164
165         TAILQ_FOREACH(mapinlist, &dev->maplist, link) {
166                 if (i==idx) {
167                         map.offset = mapinlist->offset;
168                         map.size   = mapinlist->size;
169                         map.type   = mapinlist->type;
170                         map.flags  = mapinlist->flags;
171                         map.handle = mapinlist->handle;
172                         map.mtrr   = mapinlist->mtrr;
173                         break;
174                 }
175                 i++;
176         }
177
178         DRM_UNLOCK();
179
180         if (mapinlist == NULL)
181                 return EINVAL;
182
183         DRM_COPY_TO_USER_IOCTL( (drm_map_t *)data, map, sizeof(map) );
184
185         return 0;
186 }
187
188 int drm_getclient(DRM_IOCTL_ARGS)
189 {
190         DRM_DEVICE;
191         drm_client_t client;
192         drm_file_t   *pt;
193         int          idx;
194         int          i = 0;
195
196         DRM_COPY_FROM_USER_IOCTL( client, (drm_client_t *)data, sizeof(client) );
197
198         idx = client.idx;
199         DRM_LOCK();
200         TAILQ_FOREACH(pt, &dev->files, link) {
201                 if (i==idx)
202                 {
203                         client.auth  = pt->authenticated;
204                         client.pid   = pt->pid;
205                         client.uid   = pt->uid;
206                         client.magic = pt->magic;
207                         client.iocs  = pt->ioctl_count;
208                         DRM_UNLOCK();
209
210                         *(drm_client_t *)data = client;
211                         return 0;
212                 }
213                 i++;
214         }
215         DRM_UNLOCK();
216
217         DRM_COPY_TO_USER_IOCTL( (drm_client_t *)data, client, sizeof(client) );
218
219         return 0;
220 }
221
222 int drm_getstats(DRM_IOCTL_ARGS)
223 {
224         DRM_DEVICE;
225         drm_stats_t  stats;
226         int          i;
227
228         memset(&stats, 0, sizeof(stats));
229         
230         DRM_LOCK();
231
232         for (i = 0; i < dev->counters; i++) {
233                 if (dev->types[i] == _DRM_STAT_LOCK)
234                         stats.data[i].value
235                                 = (dev->lock.hw_lock
236                                    ? dev->lock.hw_lock->lock : 0);
237                 else 
238                         stats.data[i].value = atomic_read(&dev->counts[i]);
239                 stats.data[i].type  = dev->types[i];
240         }
241         
242         stats.count = dev->counters;
243
244         DRM_UNLOCK();
245
246         DRM_COPY_TO_USER_IOCTL( (drm_stats_t *)data, stats, sizeof(stats) );
247
248         return 0;
249 }
250
251 #define DRM_IF_MAJOR    1
252 #define DRM_IF_MINOR    2
253
254 int drm_setversion(DRM_IOCTL_ARGS)
255 {
256         DRM_DEVICE;
257         drm_set_version_t sv;
258         drm_set_version_t retv;
259         int if_version;
260
261         DRM_COPY_FROM_USER_IOCTL(sv, (drm_set_version_t *)data, sizeof(sv));
262
263         retv.drm_di_major = DRM_IF_MAJOR;
264         retv.drm_di_minor = DRM_IF_MINOR;
265         retv.drm_dd_major = dev->driver_major;
266         retv.drm_dd_minor = dev->driver_minor;
267
268         DRM_COPY_TO_USER_IOCTL((drm_set_version_t *)data, retv, sizeof(sv));
269
270         if (sv.drm_di_major != -1) {
271                 if (sv.drm_di_major != DRM_IF_MAJOR ||
272                     sv.drm_di_minor < 0 || sv.drm_di_minor > DRM_IF_MINOR)
273                         return EINVAL;
274                 if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_dd_minor);
275                 dev->if_version = DRM_MAX(if_version, dev->if_version);
276                 if (sv.drm_di_minor >= 1) {
277                         /*
278                          * Version 1.1 includes tying of DRM to specific device
279                          */
280                         drm_set_busid(dev);
281                 }
282         }
283
284         if (sv.drm_dd_major != -1) {
285                 if (sv.drm_dd_major != dev->driver_major ||
286                     sv.drm_dd_minor < 0 || sv.drm_dd_minor > dev->driver_minor)
287                         return EINVAL;
288         }
289         return 0;
290 }
291
292
293 int drm_noop(DRM_IOCTL_ARGS)
294 {
295         DRM_DEBUG("\n");
296         return 0;
297 }