DEVFS - remove dev_ops_add(), dev_ops_get(), and get_dev()
[dragonfly.git] / sys / dev / drm / drm_drv.c
CommitLineData
7f3c3d6f
HT
1/*-
2 * Copyright 1999, 2000 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 *
7f3c3d6f
HT
29 */
30
31/** @file drm_drv.c
32 * The catch-all file for DRM device support, including module setup/teardown,
33 * open/close, and ioctl dispatch.
34 */
35
7f3c3d6f 36#include <machine/limits.h>
b3705d71
HT
37#include "dev/drm/drmP.h"
38#include "dev/drm/drm.h"
39#include "dev/drm/drm_sarea.h"
7f3c3d6f
HT
40
41#ifdef DRM_DEBUG_DEFAULT_ON
42int drm_debug_flag = 1;
43#else
44int drm_debug_flag = 0;
45#endif
46
b3705d71
HT
47static int drm_load(struct drm_device *dev);
48static void drm_unload(struct drm_device *dev);
7f3c3d6f
HT
49static drm_pci_id_list_t *drm_find_description(int vendor, int device,
50 drm_pci_id_list_t *idlist);
51
7f3c3d6f 52#define DRIVER_SOFTC(unit) \
b3705d71 53 ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
7f3c3d6f
HT
54
55MODULE_VERSION(drm, 1);
56MODULE_DEPEND(drm, agp, 1, 1, 1);
57MODULE_DEPEND(drm, pci, 1, 1, 1);
7f3c3d6f 58MODULE_DEPEND(drm, mem, 1, 1, 1);
7f3c3d6f
HT
59
60static drm_ioctl_desc_t drm_ioctls[256] = {
61 DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, 0),
62 DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
63 DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
64 DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
65 DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_getmap, 0),
66 DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, 0),
67 DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, 0),
68 DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER|DRM_ROOT_ONLY),
69
70 DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
71 DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
72 DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
73 DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
74
75 DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
76 DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_rmmap_ioctl, DRM_AUTH),
77
78 DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
79 DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_getsareactx, DRM_AUTH),
80
b3705d71 81 DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_addctx, DRM_AUTH|DRM_ROOT_ONLY),
7f3c3d6f
HT
82 DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
83 DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_modctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
84 DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_getctx, DRM_AUTH),
85 DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
86 DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
87 DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_resctx, DRM_AUTH),
88
89 DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_adddraw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
90 DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_rmdraw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
91
92 DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_lock, DRM_AUTH),
93 DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_unlock, DRM_AUTH),
b3705d71 94
7f3c3d6f
HT
95 DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
96
b3705d71
HT
97 DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
98 DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
7f3c3d6f
HT
99 DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_infobufs, DRM_AUTH),
100 DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_mapbufs, DRM_AUTH),
101 DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_freebufs, DRM_AUTH),
102 DRM_IOCTL_DEF(DRM_IOCTL_DMA, drm_dma, DRM_AUTH),
103
104 DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
105
106 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
107 DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
108 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
109 DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
110 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
111 DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
112 DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
113 DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
114
115 DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_sg_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
116 DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
7f3c3d6f 117 DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, 0),
b3705d71 118 DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_modeset_ctl, 0),
7f3c3d6f
HT
119 DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_update_draw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
120};
121
7f3c3d6f
HT
122static struct dev_ops drm_cdevsw = {
123 { "drm", 145, D_TRACKCLOSE },
b3705d71 124 .d_open = drm_open,
2c845b81 125 .d_close = drm_close,
b3705d71
HT
126 .d_read = drm_read,
127 .d_ioctl = drm_ioctl,
128 .d_poll = drm_poll,
129 .d_mmap = drm_mmap
130};
131
bd121a8b 132static int drm_msi = 1; /* Enable by default. */
b3705d71
HT
133TUNABLE_INT("hw.drm.msi", &drm_msi);
134
135static struct drm_msi_blacklist_entry drm_msi_blacklist[] = {
136 {0x8086, 0x2772}, /* Intel i945G */ \
137 {0x8086, 0x27A2}, /* Intel i945GM */ \
138 {0x8086, 0x27AE}, /* Intel i945GME */ \
139 {0, 0}
7f3c3d6f 140};
7f3c3d6f 141
b3705d71
HT
142static int drm_msi_is_blacklisted(int vendor, int device)
143{
144 int i = 0;
145
146 for (i = 0; drm_msi_blacklist[i].vendor != 0; i++) {
147 if ((drm_msi_blacklist[i].vendor == vendor) &&
148 (drm_msi_blacklist[i].device == device)) {
149 return 1;
150 }
151 }
152
153 return 0;
154}
155
156int drm_probe(device_t kdev, drm_pci_id_list_t *idlist)
7f3c3d6f
HT
157{
158 drm_pci_id_list_t *id_entry;
159 int vendor, device;
7f3c3d6f 160
31935f45
SZ
161 vendor = pci_get_vendor(kdev);
162 device = pci_get_device(kdev);
b3705d71 163
b3705d71
HT
164 if (pci_get_class(kdev) != PCIC_DISPLAY
165 || pci_get_subclass(kdev) != PCIS_DISPLAY_VGA)
166 return ENXIO;
7f3c3d6f
HT
167
168 id_entry = drm_find_description(vendor, device, idlist);
169 if (id_entry != NULL) {
b3705d71
HT
170 if (!device_get_desc(kdev)) {
171 DRM_DEBUG("desc : %s\n", device_get_desc(kdev));
172 device_set_desc(kdev, id_entry->name);
173 }
7f3c3d6f
HT
174 return 0;
175 }
176
177 return ENXIO;
178}
179
b3705d71 180int drm_attach(device_t kdev, drm_pci_id_list_t *idlist)
7f3c3d6f 181{
b3705d71 182 struct drm_device *dev;
7f3c3d6f
HT
183 drm_pci_id_list_t *id_entry;
184 int unit;
b3705d71
HT
185#if 0
186 int msicount;
187#endif
7f3c3d6f 188
b3705d71
HT
189 unit = device_get_unit(kdev);
190 dev = device_get_softc(kdev);
7f3c3d6f 191
b3705d71
HT
192 if (!strcmp(device_get_name(kdev), "drmsub"))
193 dev->device = device_get_parent(kdev);
7f3c3d6f 194 else
b3705d71
HT
195 dev->device = kdev;
196
3e82b46c
MD
197 dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID,
198 DRM_DEV_MODE, "dri/card%d", unit);
7f3c3d6f 199
b3705d71
HT
200 dev->pci_domain = 0;
201 dev->pci_bus = pci_get_bus(dev->device);
202 dev->pci_slot = pci_get_slot(dev->device);
203 dev->pci_func = pci_get_function(dev->device);
7f3c3d6f 204
b3705d71
HT
205 dev->pci_vendor = pci_get_vendor(dev->device);
206 dev->pci_device = pci_get_device(dev->device);
7f3c3d6f 207
bd121a8b
HT
208 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) {
209 if (drm_msi &&
210 !drm_msi_is_blacklisted(dev->pci_vendor, dev->pci_device)) {
b3705d71 211#if 0
bd121a8b
HT
212 msicount = pci_msi_count(dev->device);
213 DRM_DEBUG("MSI count = %d\n", msicount);
214 if (msicount > 1)
215 msicount = 1;
216
217 if (pci_alloc_msi(dev->device, &msicount) == 0) {
218 DRM_INFO("MSI enabled %d message(s)\n",
219 msicount);
220 dev->msi_enabled = 1;
221 dev->irqrid = 1;
222 }
7f3c3d6f 223#endif
bd121a8b 224 }
7f3c3d6f 225
bd121a8b
HT
226 dev->irqr = bus_alloc_resource_any(dev->device, SYS_RES_IRQ,
227 &dev->irqrid, RF_SHAREABLE);
228 if (!dev->irqr) {
229 return ENOENT;
230 }
7f3c3d6f 231
bd121a8b
HT
232 dev->irq = (int) rman_get_start(dev->irqr);
233 }
7f3c3d6f 234
b3705d71
HT
235 DRM_SPININIT(&dev->dev_lock, "drmdev");
236 lwkt_serialize_init(&dev->irq_lock);
237 DRM_SPININIT(&dev->vbl_lock, "drmvbl");
238 DRM_SPININIT(&dev->drw_lock, "drmdrw");
7f3c3d6f 239
b3705d71
HT
240 id_entry = drm_find_description(dev->pci_vendor,
241 dev->pci_device, idlist);
242 dev->id_entry = id_entry;
7f3c3d6f 243
b3705d71 244 return drm_load(dev);
7f3c3d6f
HT
245}
246
b3705d71 247int drm_detach(device_t kdev)
7f3c3d6f 248{
b3705d71 249 struct drm_device *dev;
7f3c3d6f 250
b3705d71 251 dev = device_get_softc(kdev);
7f3c3d6f 252
b3705d71 253 drm_unload(dev);
a48a8c19
HT
254
255 if (dev->irqr) {
256 bus_release_resource(dev->device, SYS_RES_IRQ, dev->irqrid,
257 dev->irqr);
7f3c3d6f 258
b3705d71 259#if 0
a48a8c19
HT
260 if (dev->msi_enabled) {
261 pci_release_msi(dev->device);
262 DRM_INFO("MSI released\n");
263 }
b3705d71 264#endif
a48a8c19 265 }
7f3c3d6f
HT
266
267 return 0;
268}
269
b3705d71
HT
270#ifndef DRM_DEV_NAME
271#define DRM_DEV_NAME "drm"
272#endif
7f3c3d6f 273
b3705d71 274devclass_t drm_devclass;
7f3c3d6f
HT
275
276drm_pci_id_list_t *drm_find_description(int vendor, int device,
277 drm_pci_id_list_t *idlist)
278{
279 int i = 0;
280
281 for (i = 0; idlist[i].vendor != 0; i++) {
282 if ((idlist[i].vendor == vendor) &&
b3705d71
HT
283 ((idlist[i].device == device) ||
284 (idlist[i].device == 0))) {
7f3c3d6f
HT
285 return &idlist[i];
286 }
287 }
288 return NULL;
289}
290
b3705d71 291static int drm_firstopen(struct drm_device *dev)
7f3c3d6f
HT
292{
293 drm_local_map_t *map;
294 int i;
295
296 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
297
298 /* prebuild the SAREA */
299 i = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
b3705d71 300 _DRM_CONTAINS_LOCK, &map);
7f3c3d6f
HT
301 if (i != 0)
302 return i;
303
b3705d71
HT
304 if (dev->driver->firstopen)
305 dev->driver->firstopen(dev);
7f3c3d6f
HT
306
307 dev->buf_use = 0;
308
b3705d71 309 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
7f3c3d6f
HT
310 i = drm_dma_setup(dev);
311 if (i != 0)
312 return i;
313 }
314
b3705d71 315 for (i = 0; i < DRM_HASH_SIZE; i++) {
7f3c3d6f
HT
316 dev->magiclist[i].head = NULL;
317 dev->magiclist[i].tail = NULL;
318 }
319
320 dev->lock.lock_queue = 0;
321 dev->irq_enabled = 0;
322 dev->context_flag = 0;
323 dev->last_context = 0;
324 dev->if_version = 0;
325
7f3c3d6f 326 dev->buf_sigio = NULL;
7f3c3d6f 327
b3705d71 328 DRM_DEBUG("\n");
7f3c3d6f
HT
329
330 return 0;
331}
332
b3705d71 333static int drm_lastclose(struct drm_device *dev)
7f3c3d6f
HT
334{
335 drm_magic_entry_t *pt, *next;
336 drm_local_map_t *map, *mapsave;
337 int i;
338
339 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
340
b3705d71 341 DRM_DEBUG("\n");
7f3c3d6f 342
b3705d71
HT
343 if (dev->driver->lastclose != NULL)
344 dev->driver->lastclose(dev);
7f3c3d6f
HT
345
346 if (dev->irq_enabled)
347 drm_irq_uninstall(dev);
348
b3705d71
HT
349 if (dev->unique) {
350 free(dev->unique, DRM_MEM_DRIVER);
7f3c3d6f
HT
351 dev->unique = NULL;
352 dev->unique_len = 0;
353 }
b3705d71
HT
354 /* Clear pid list */
355 for (i = 0; i < DRM_HASH_SIZE; i++) {
356 for (pt = dev->magiclist[i].head; pt; pt = next) {
7f3c3d6f 357 next = pt->next;
b3705d71 358 free(pt, DRM_MEM_MAGIC);
7f3c3d6f
HT
359 }
360 dev->magiclist[i].head = dev->magiclist[i].tail = NULL;
361 }
362
b3705d71
HT
363 DRM_UNLOCK();
364 drm_drawable_free_all(dev);
365 DRM_LOCK();
366
367 /* Clear AGP information */
368 if (dev->agp) {
7f3c3d6f
HT
369 drm_agp_mem_t *entry;
370 drm_agp_mem_t *nexte;
371
372 /* Remove AGP resources, but leave dev->agp intact until
373 * drm_unload is called.
374 */
b3705d71 375 for (entry = dev->agp->memory; entry; entry = nexte) {
7f3c3d6f 376 nexte = entry->next;
b3705d71 377 if (entry->bound)
7f3c3d6f
HT
378 drm_agp_unbind_memory(entry->handle);
379 drm_agp_free_memory(entry->handle);
b3705d71 380 free(entry, DRM_MEM_AGPLISTS);
7f3c3d6f
HT
381 }
382 dev->agp->memory = NULL;
383
384 if (dev->agp->acquired)
385 drm_agp_release(dev);
386
387 dev->agp->acquired = 0;
388 dev->agp->enabled = 0;
389 }
390 if (dev->sg != NULL) {
391 drm_sg_cleanup(dev->sg);
392 dev->sg = NULL;
393 }
394
ecd27a2e 395 TAILQ_FOREACH_MUTABLE(map, &dev->maplist, link, mapsave) {
7f3c3d6f
HT
396 if (!(map->flags & _DRM_DRIVER))
397 drm_rmmap(dev, map);
398 }
399
400 drm_dma_takedown(dev);
b3705d71 401 if (dev->lock.hw_lock) {
7f3c3d6f
HT
402 dev->lock.hw_lock = NULL; /* SHM removed */
403 dev->lock.file_priv = NULL;
404 DRM_WAKEUP_INT((void *)&dev->lock.lock_queue);
405 }
406
407 return 0;
408}
409
b3705d71 410static int drm_load(struct drm_device *dev)
7f3c3d6f
HT
411{
412 int i, retcode;
413
b3705d71 414 DRM_DEBUG("\n");
7f3c3d6f
HT
415
416 TAILQ_INIT(&dev->maplist);
417
418 drm_mem_init();
7f3c3d6f 419 drm_sysctl_init(dev);
7f3c3d6f
HT
420 TAILQ_INIT(&dev->files);
421
422 dev->counters = 6;
423 dev->types[0] = _DRM_STAT_LOCK;
424 dev->types[1] = _DRM_STAT_OPENS;
425 dev->types[2] = _DRM_STAT_CLOSES;
426 dev->types[3] = _DRM_STAT_IOCTLS;
427 dev->types[4] = _DRM_STAT_LOCKS;
428 dev->types[5] = _DRM_STAT_UNLOCKS;
429
b3705d71
HT
430 for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++)
431 atomic_set(&dev->counts[i], 0);
7f3c3d6f 432
b3705d71 433 if (dev->driver->load != NULL) {
7f3c3d6f
HT
434 DRM_LOCK();
435 /* Shared code returns -errno. */
b3705d71 436 retcode = -dev->driver->load(dev,
7f3c3d6f 437 dev->id_entry->driver_private);
b3705d71 438 pci_enable_busmaster(dev->device);
7f3c3d6f
HT
439 DRM_UNLOCK();
440 if (retcode != 0)
441 goto error;
442 }
443
b3705d71 444 if (drm_core_has_AGP(dev)) {
7f3c3d6f
HT
445 if (drm_device_is_agp(dev))
446 dev->agp = drm_agp_init();
b3705d71
HT
447 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP) &&
448 dev->agp == NULL) {
7f3c3d6f
HT
449 DRM_ERROR("Card isn't AGP, or couldn't initialize "
450 "AGP.\n");
451 retcode = ENOMEM;
452 goto error;
453 }
454 if (dev->agp != NULL) {
455 if (drm_mtrr_add(dev->agp->info.ai_aperture_base,
456 dev->agp->info.ai_aperture_size, DRM_MTRR_WC) == 0)
457 dev->agp->mtrr = 1;
458 }
459 }
460
461 retcode = drm_ctxbitmap_init(dev);
462 if (retcode != 0) {
463 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
464 goto error;
465 }
466
7f3c3d6f 467 DRM_INFO("Initialized %s %d.%d.%d %s\n",
b3705d71
HT
468 dev->driver->name,
469 dev->driver->major,
470 dev->driver->minor,
471 dev->driver->patchlevel,
472 dev->driver->date);
7f3c3d6f
HT
473
474 return 0;
475
476error:
7f3c3d6f 477 drm_sysctl_cleanup(dev);
7f3c3d6f
HT
478 DRM_LOCK();
479 drm_lastclose(dev);
480 DRM_UNLOCK();
7f3c3d6f 481 destroy_dev(dev->devnode);
b3705d71
HT
482
483 DRM_SPINUNINIT(&dev->drw_lock);
484 DRM_SPINUNINIT(&dev->vbl_lock);
7f3c3d6f 485 DRM_SPINUNINIT(&dev->dev_lock);
b3705d71 486
7f3c3d6f
HT
487 return retcode;
488}
489
b3705d71 490static void drm_unload(struct drm_device *dev)
7f3c3d6f
HT
491{
492 int i;
493
b3705d71 494 DRM_DEBUG("\n");
7f3c3d6f 495
7f3c3d6f
HT
496 drm_sysctl_cleanup(dev);
497 destroy_dev(dev->devnode);
7f3c3d6f
HT
498
499 drm_ctxbitmap_cleanup(dev);
500
501 if (dev->agp && dev->agp->mtrr) {
502 int __unused retcode;
503
504 retcode = drm_mtrr_del(0, dev->agp->info.ai_aperture_base,
505 dev->agp->info.ai_aperture_size, DRM_MTRR_WC);
506 DRM_DEBUG("mtrr_del = %d", retcode);
507 }
508
b3705d71
HT
509 drm_vblank_cleanup(dev);
510
7f3c3d6f
HT
511 DRM_LOCK();
512 drm_lastclose(dev);
513 DRM_UNLOCK();
514
515 /* Clean up PCI resources allocated by drm_bufs.c. We're not really
516 * worried about resource consumption while the DRM is inactive (between
517 * lastclose and firstopen or unload) because these aren't actually
518 * taking up KVA, just keeping the PCI resource allocated.
519 */
520 for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
521 if (dev->pcir[i] == NULL)
522 continue;
523 bus_release_resource(dev->device, SYS_RES_MEMORY,
524 dev->pcirid[i], dev->pcir[i]);
525 dev->pcir[i] = NULL;
526 }
527
b3705d71
HT
528 if (dev->agp) {
529 free(dev->agp, DRM_MEM_AGPLISTS);
7f3c3d6f
HT
530 dev->agp = NULL;
531 }
532
b3705d71
HT
533 if (dev->driver->unload != NULL) {
534 DRM_LOCK();
535 dev->driver->unload(dev);
536 DRM_UNLOCK();
537 }
7f3c3d6f
HT
538
539 drm_mem_uninit();
b3705d71
HT
540
541 pci_disable_busmaster(dev->device);
542
543 DRM_SPINUNINIT(&dev->drw_lock);
544 DRM_SPINUNINIT(&dev->vbl_lock);
7f3c3d6f 545 DRM_SPINUNINIT(&dev->dev_lock);
7f3c3d6f
HT
546}
547
b3705d71 548int drm_version(struct drm_device *dev, void *data, struct drm_file *file_priv)
7f3c3d6f 549{
b3705d71 550 struct drm_version *version = data;
7f3c3d6f
HT
551 int len;
552
553#define DRM_COPY( name, value ) \
554 len = strlen( value ); \
555 if ( len > name##_len ) len = name##_len; \
556 name##_len = strlen( value ); \
557 if ( len && name ) { \
558 if ( DRM_COPY_TO_USER( name, value, len ) ) \
559 return EFAULT; \
560 }
561
b3705d71
HT
562 version->version_major = dev->driver->major;
563 version->version_minor = dev->driver->minor;
564 version->version_patchlevel = dev->driver->patchlevel;
7f3c3d6f 565
b3705d71
HT
566 DRM_COPY(version->name, dev->driver->name);
567 DRM_COPY(version->date, dev->driver->date);
568 DRM_COPY(version->desc, dev->driver->desc);
7f3c3d6f
HT
569
570 return 0;
571}
572
7f3c3d6f
HT
573int drm_open(struct dev_open_args *ap)
574{
575 struct cdev *kdev = ap->a_head.a_dev;
576 int flags = ap->a_oflags;
577 int fmt = 0;
578 struct thread *p = curthread;
b3705d71 579 struct drm_device *dev = NULL;
7f3c3d6f
HT
580 int retcode = 0;
581
582 dev = DRIVER_SOFTC(minor(kdev));
583
b3705d71 584 DRM_DEBUG("open_count = %d\n", dev->open_count);
7f3c3d6f
HT
585
586 retcode = drm_open_helper(kdev, flags, fmt, p, dev);
587
b3705d71
HT
588 if (!retcode) {
589 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
7f3c3d6f 590 DRM_LOCK();
7f3c3d6f 591 device_busy(dev->device);
b3705d71 592 if (!dev->open_count++)
7f3c3d6f
HT
593 retcode = drm_firstopen(dev);
594 DRM_UNLOCK();
595 }
596
597 return retcode;
598}
599
2c845b81 600int drm_close(struct dev_close_args *ap)
7f3c3d6f 601{
2c845b81
SS
602 struct cdev *kdev = ap->a_head.a_dev;
603 struct drm_file *file_priv;
604 struct drm_device *dev;
7f3c3d6f
HT
605 int retcode = 0;
606
2c845b81
SS
607 dev = DRIVER_SOFTC(minor(kdev));
608 file_priv = drm_find_file_by_proc(dev, curthread);
609
b3705d71 610 DRM_DEBUG("open_count = %d\n", dev->open_count);
7f3c3d6f
HT
611
612 DRM_LOCK();
613
7f3c3d6f
HT
614 if (--file_priv->refs != 0)
615 goto done;
616
b3705d71
HT
617 if (dev->driver->preclose != NULL)
618 dev->driver->preclose(dev, file_priv);
7f3c3d6f
HT
619
620 /* ========================================================
621 * Begin inline drm_release
622 */
623
b3705d71
HT
624 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
625 DRM_CURRENTPID, (long)dev->device, dev->open_count);
7f3c3d6f
HT
626
627 if (dev->lock.hw_lock && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
628 && dev->lock.file_priv == file_priv) {
629 DRM_DEBUG("Process %d dead, freeing lock for context %d\n",
630 DRM_CURRENTPID,
631 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
b3705d71
HT
632 if (dev->driver->reclaim_buffers_locked != NULL)
633 dev->driver->reclaim_buffers_locked(dev, file_priv);
7f3c3d6f 634
b3705d71 635 drm_lock_free(&dev->lock,
7f3c3d6f
HT
636 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
637
638 /* FIXME: may require heavy-handed reset of
639 hardware at this point, possibly
640 processed via a callback to the X
641 server. */
b3705d71 642 } else if (dev->driver->reclaim_buffers_locked != NULL &&
7f3c3d6f
HT
643 dev->lock.hw_lock != NULL) {
644 /* The lock is required to reclaim buffers */
645 for (;;) {
b3705d71 646 if (!dev->lock.hw_lock) {
7f3c3d6f
HT
647 /* Device has been unregistered */
648 retcode = EINTR;
649 break;
650 }
b3705d71 651 if (drm_lock_take(&dev->lock, DRM_KERNEL_CONTEXT)) {
7f3c3d6f
HT
652 dev->lock.file_priv = file_priv;
653 dev->lock.lock_time = jiffies;
b3705d71 654 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
7f3c3d6f
HT
655 break; /* Got lock */
656 }
b3705d71 657 /* Contention */
ae8e83e6 658 tsleep_interlock((void *)&dev->lock.lock_queue, PCATCH);
7f3c3d6f
HT
659 DRM_UNLOCK();
660 retcode = tsleep((void *)&dev->lock.lock_queue,
ae8e83e6 661 PCATCH | PINTERLOCKED, "drmlk2", 0);
7f3c3d6f 662 DRM_LOCK();
7f3c3d6f
HT
663 if (retcode)
664 break;
665 }
666 if (retcode == 0) {
b3705d71
HT
667 dev->driver->reclaim_buffers_locked(dev, file_priv);
668 drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT);
7f3c3d6f
HT
669 }
670 }
671
b3705d71
HT
672 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
673 !dev->driver->reclaim_buffers_locked)
7f3c3d6f
HT
674 drm_reclaim_buffers(dev, file_priv);
675
7f3c3d6f 676 funsetown(dev->buf_sigio);
7f3c3d6f 677
b3705d71
HT
678 if (dev->driver->postclose != NULL)
679 dev->driver->postclose(dev, file_priv);
7f3c3d6f 680 TAILQ_REMOVE(&dev->files, file_priv, link);
b3705d71 681 free(file_priv, DRM_MEM_FILES);
7f3c3d6f
HT
682
683 /* ========================================================
684 * End inline drm_release
685 */
7f3c3d6f 686done:
b3705d71 687 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
7f3c3d6f 688 device_unbusy(dev->device);
7f3c3d6f
HT
689 if (--dev->open_count == 0) {
690 retcode = drm_lastclose(dev);
691 }
692
693 DRM_UNLOCK();
2c845b81
SS
694
695 return (0);
7f3c3d6f
HT
696}
697
698/* drm_ioctl is called whenever a process performs an ioctl on /dev/drm.
699 */
7f3c3d6f
HT
700int drm_ioctl(struct dev_ioctl_args *ap)
701{
702 struct cdev *kdev = ap->a_head.a_dev;
703 u_long cmd = ap->a_cmd;
704 caddr_t data = ap->a_data;
705 struct thread *p = curthread;
b3705d71 706 struct drm_device *dev = drm_get_device_from_kdev(kdev);
7f3c3d6f
HT
707 int retcode = 0;
708 drm_ioctl_desc_t *ioctl;
b3705d71 709 int (*func)(struct drm_device *dev, void *data, struct drm_file *file_priv);
7f3c3d6f
HT
710 int nr = DRM_IOCTL_NR(cmd);
711 int is_driver_ioctl = 0;
b3705d71 712 struct drm_file *file_priv;
7f3c3d6f 713
7f3c3d6f 714 file_priv = drm_find_file_by_proc(dev, p);
b3705d71 715 if (!file_priv) {
7f3c3d6f
HT
716 DRM_ERROR("can't find authenticator\n");
717 return EINVAL;
718 }
719
b3705d71 720 atomic_inc(&dev->counts[_DRM_STAT_IOCTLS]);
7f3c3d6f
HT
721 ++file_priv->ioctl_count;
722
b3705d71 723 DRM_DEBUG("pid=%d, cmd=0x%02lx, nr=0x%02x, dev 0x%lx, auth=%d\n",
7f3c3d6f 724 DRM_CURRENTPID, cmd, nr, (long)dev->device,
b3705d71 725 file_priv->authenticated);
7f3c3d6f
HT
726
727 switch (cmd) {
728 case FIONBIO:
729 case FIOASYNC:
730 return 0;
731
7f3c3d6f
HT
732 case FIOSETOWN:
733 return fsetown(*(int *)data, &dev->buf_sigio);
734
735 case FIOGETOWN:
7f3c3d6f 736 *(int *) data = fgetown(dev->buf_sigio);
7f3c3d6f 737 return 0;
7f3c3d6f
HT
738 }
739
740 if (IOCGROUP(cmd) != DRM_IOCTL_BASE) {
741 DRM_DEBUG("Bad ioctl group 0x%x\n", (int)IOCGROUP(cmd));
742 return EINVAL;
743 }
744
745 ioctl = &drm_ioctls[nr];
746 /* It's not a core DRM ioctl, try driver-specific. */
747 if (ioctl->func == NULL && nr >= DRM_COMMAND_BASE) {
748 /* The array entries begin at DRM_COMMAND_BASE ioctl nr */
749 nr -= DRM_COMMAND_BASE;
b3705d71 750 if (nr > dev->driver->max_ioctl) {
7f3c3d6f 751 DRM_DEBUG("Bad driver ioctl number, 0x%x (of 0x%x)\n",
b3705d71 752 nr, dev->driver->max_ioctl);
7f3c3d6f
HT
753 return EINVAL;
754 }
b3705d71 755 ioctl = &dev->driver->ioctls[nr];
7f3c3d6f
HT
756 is_driver_ioctl = 1;
757 }
758 func = ioctl->func;
759
760 if (func == NULL) {
b3705d71 761 DRM_DEBUG("no function\n");
7f3c3d6f
HT
762 return EINVAL;
763 }
764
765 if (((ioctl->flags & DRM_ROOT_ONLY) && !DRM_SUSER(p)) ||
766 ((ioctl->flags & DRM_AUTH) && !file_priv->authenticated) ||
767 ((ioctl->flags & DRM_MASTER) && !file_priv->master))
768 return EACCES;
769
770 if (is_driver_ioctl) {
771 DRM_LOCK();
772 /* shared code returns -errno */
773 retcode = -func(dev, data, file_priv);
774 DRM_UNLOCK();
775 } else {
776 retcode = func(dev, data, file_priv);
777 }
778
779 if (retcode != 0)
780 DRM_DEBUG(" returning %d\n", retcode);
781
782 return retcode;
783}
784
b3705d71 785drm_local_map_t *drm_getsarea(struct drm_device *dev)
7f3c3d6f
HT
786{
787 drm_local_map_t *map;
788
789 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
790 TAILQ_FOREACH(map, &dev->maplist, link) {
791 if (map->type == _DRM_SHM && (map->flags & _DRM_CONTAINS_LOCK))
792 return map;
793 }
794
795 return NULL;
796}
797
798#if DRM_LINUX
799
800#include <sys/sysproto.h>
801
802MODULE_DEPEND(DRIVER_NAME, linux, 1, 1, 1);
803
804#define LINUX_IOCTL_DRM_MIN 0x6400
805#define LINUX_IOCTL_DRM_MAX 0x64ff
806
807static linux_ioctl_function_t drm_linux_ioctl;
808static struct linux_ioctl_handler drm_handler = {drm_linux_ioctl,
809 LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX};
810
811SYSINIT(drm_register, SI_SUB_KLD, SI_ORDER_MIDDLE,
812 linux_ioctl_register_handler, &drm_handler);
813SYSUNINIT(drm_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE,
814 linux_ioctl_unregister_handler, &drm_handler);
815
816/* The bits for in/out are switched on Linux */
817#define LINUX_IOC_IN IOC_OUT
818#define LINUX_IOC_OUT IOC_IN
819
820static int
821drm_linux_ioctl(DRM_STRUCTPROC *p, struct linux_ioctl_args* args)
822{
823 int error;
824 int cmd = args->cmd;
825
826 args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
827 if (cmd & LINUX_IOC_IN)
828 args->cmd |= IOC_IN;
829 if (cmd & LINUX_IOC_OUT)
830 args->cmd |= IOC_OUT;
831
832 error = ioctl(p, (struct ioctl_args *)args);
833
834 return error;
835}
836#endif /* DRM_LINUX */