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