OSDN Git Service

Merge branch 'nouveau-1'
[android-x86/external-libdrm.git] / bsd-core / 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  */
33
34 #include "drmP.h"
35
36 /*
37  * Beginning in revision 1.1 of the DRM interface, getunique will return
38  * a unique in the form pci:oooo:bb:dd.f (o=domain, b=bus, d=device, f=function)
39  * before setunique has been called.  The format for the bus-specific part of
40  * the unique is not defined for any other bus.
41  */
42 int drm_getunique(DRM_IOCTL_ARGS)
43 {
44         DRM_DEVICE;
45         drm_unique_t     u;
46
47         DRM_COPY_FROM_USER_IOCTL( u, (drm_unique_t *)data, sizeof(u) );
48
49         if (u.unique_len >= dev->unique_len) {
50                 if (DRM_COPY_TO_USER(u.unique, dev->unique, dev->unique_len))
51                         return DRM_ERR(EFAULT);
52         }
53         u.unique_len = dev->unique_len;
54
55         DRM_COPY_TO_USER_IOCTL( (drm_unique_t *)data, u, sizeof(u) );
56
57         return 0;
58 }
59
60 /* Deprecated in DRM version 1.1, and will return EBUSY when setversion has
61  * requested version 1.1 or greater.
62  */
63 int drm_setunique(DRM_IOCTL_ARGS)
64 {
65         DRM_DEVICE;
66         drm_unique_t u;
67         int domain, bus, slot, func, ret;
68         char *busid;
69
70         DRM_COPY_FROM_USER_IOCTL( u, (drm_unique_t *)data, sizeof(u) );
71
72         /* Check and copy in the submitted Bus ID */
73         if (!u.unique_len || u.unique_len > 1024)
74                 return DRM_ERR(EINVAL);
75
76         busid = malloc(u.unique_len + 1, M_DRM, M_WAITOK);
77         if (busid == NULL)
78                 return DRM_ERR(ENOMEM);
79
80         if (DRM_COPY_FROM_USER(busid, u.unique, u.unique_len)) {
81                 free(busid, M_DRM);
82                 return DRM_ERR(EFAULT);
83         }
84         busid[u.unique_len] = '\0';
85
86         /* Return error if the busid submitted doesn't match the device's actual
87          * busid.
88          */
89         ret = sscanf(busid, "PCI:%d:%d:%d", &bus, &slot, &func);
90         if (ret != 3) {
91                 free(busid, M_DRM);
92                 return DRM_ERR(EINVAL);
93         }
94         domain = bus >> 8;
95         bus &= 0xff;
96         
97         if ((domain != dev->pci_domain) ||
98             (bus != dev->pci_bus) ||
99             (slot != dev->pci_slot) ||
100             (func != dev->pci_func)) {
101                 free(busid, M_DRM);
102                 return DRM_ERR(EINVAL);
103         }
104
105         /* Actually set the device's busid now. */
106         DRM_LOCK();
107         if (dev->unique_len || dev->unique) {
108                 DRM_UNLOCK();
109                 return DRM_ERR(EBUSY);
110         }
111
112         dev->unique_len = u.unique_len;
113         dev->unique = busid;
114         DRM_UNLOCK();
115
116         return 0;
117 }
118
119
120 static int
121 drm_set_busid(drm_device_t *dev)
122 {
123
124         DRM_LOCK();
125
126         if (dev->unique != NULL) {
127                 DRM_UNLOCK();
128                 return EBUSY;
129         }
130
131         dev->unique_len = 20;
132         dev->unique = malloc(dev->unique_len + 1, M_DRM, M_NOWAIT);
133         if (dev->unique == NULL) {
134                 DRM_UNLOCK();
135                 return ENOMEM;
136         }
137
138         snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%1x",
139             dev->pci_domain, dev->pci_bus, dev->pci_slot, dev->pci_func);
140
141         DRM_UNLOCK();
142
143         return 0;
144 }
145
146 int drm_getmap(DRM_IOCTL_ARGS)
147 {
148         DRM_DEVICE;
149         drm_map_t    map;
150         drm_local_map_t    *mapinlist;
151         int          idx;
152         int          i = 0;
153
154         DRM_COPY_FROM_USER_IOCTL( map, (drm_map_t *)data, sizeof(map) );
155
156         idx = map.offset;
157
158         DRM_LOCK();
159         if (idx < 0) {
160                 DRM_UNLOCK();
161                 return DRM_ERR(EINVAL);
162         }
163
164         TAILQ_FOREACH(mapinlist, &dev->maplist, link) {
165                 if (i==idx) {
166                         map.offset = mapinlist->offset;
167                         map.size   = mapinlist->size;
168                         map.type   = mapinlist->type;
169                         map.flags  = mapinlist->flags;
170                         map.handle = mapinlist->handle;
171                         map.mtrr   = mapinlist->mtrr;
172                         break;
173                 }
174                 i++;
175         }
176
177         DRM_UNLOCK();
178
179         if (mapinlist == NULL)
180                 return EINVAL;
181
182         DRM_COPY_TO_USER_IOCTL( (drm_map_t *)data, map, sizeof(map) );
183
184         return 0;
185 }
186
187 int drm_getclient(DRM_IOCTL_ARGS)
188 {
189         DRM_DEVICE;
190         drm_client_t client;
191         drm_file_t   *pt;
192         int          idx;
193         int          i = 0;
194
195         DRM_COPY_FROM_USER_IOCTL( client, (drm_client_t *)data, sizeof(client) );
196
197         idx = client.idx;
198         DRM_LOCK();
199         TAILQ_FOREACH(pt, &dev->files, link) {
200                 if (i==idx)
201                 {
202                         client.auth  = pt->authenticated;
203                         client.pid   = pt->pid;
204                         client.uid   = pt->uid;
205                         client.magic = pt->magic;
206                         client.iocs  = pt->ioctl_count;
207                         DRM_UNLOCK();
208
209                         *(drm_client_t *)data = client;
210                         return 0;
211                 }
212                 i++;
213         }
214         DRM_UNLOCK();
215
216         DRM_COPY_TO_USER_IOCTL( (drm_client_t *)data, client, sizeof(client) );
217
218         return 0;
219 }
220
221 int drm_getstats(DRM_IOCTL_ARGS)
222 {
223         DRM_DEVICE;
224         drm_stats_t  stats;
225         int          i;
226
227         memset(&stats, 0, sizeof(stats));
228         
229         DRM_LOCK();
230
231         for (i = 0; i < dev->counters; i++) {
232                 if (dev->types[i] == _DRM_STAT_LOCK)
233                         stats.data[i].value
234                                 = (dev->lock.hw_lock
235                                    ? dev->lock.hw_lock->lock : 0);
236                 else 
237                         stats.data[i].value = atomic_read(&dev->counts[i]);
238                 stats.data[i].type  = dev->types[i];
239         }
240         
241         stats.count = dev->counters;
242
243         DRM_UNLOCK();
244
245         DRM_COPY_TO_USER_IOCTL( (drm_stats_t *)data, stats, sizeof(stats) );
246
247         return 0;
248 }
249
250 #define DRM_IF_MAJOR    1
251 #define DRM_IF_MINOR    2
252
253 int drm_setversion(DRM_IOCTL_ARGS)
254 {
255         DRM_DEVICE;
256         drm_set_version_t sv;
257         drm_set_version_t retv;
258         int if_version;
259
260         DRM_COPY_FROM_USER_IOCTL(sv, (drm_set_version_t *)data, sizeof(sv));
261
262         retv.drm_di_major = DRM_IF_MAJOR;
263         retv.drm_di_minor = DRM_IF_MINOR;
264         retv.drm_dd_major = dev->driver.major;
265         retv.drm_dd_minor = dev->driver.minor;
266
267         DRM_COPY_TO_USER_IOCTL((drm_set_version_t *)data, retv, sizeof(sv));
268
269         if (sv.drm_di_major != -1) {
270                 if (sv.drm_di_major != DRM_IF_MAJOR ||
271                     sv.drm_di_minor < 0 || sv.drm_di_minor > DRM_IF_MINOR)
272                         return EINVAL;
273                 if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_dd_minor);
274                 dev->if_version = DRM_MAX(if_version, dev->if_version);
275                 if (sv.drm_di_minor >= 1) {
276                         /*
277                          * Version 1.1 includes tying of DRM to specific device
278                          */
279                         drm_set_busid(dev);
280                 }
281         }
282
283         if (sv.drm_dd_major != -1) {
284                 if (sv.drm_dd_major != dev->driver.major ||
285                     sv.drm_dd_minor < 0 || sv.drm_dd_minor > dev->driver.minor)
286                         return EINVAL;
287         }
288         return 0;
289 }
290
291
292 int drm_noop(DRM_IOCTL_ARGS)
293 {
294         DRM_DEBUG("\n");
295         return 0;
296 }