Only release irq resources if we were actually using them.
[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
HT
160 device_t realdev;
161
b3705d71
HT
162 if (!strcmp(device_get_name(kdev), "drmsub"))
163 realdev = device_get_parent(kdev);
7f3c3d6f 164 else
b3705d71 165 realdev = kdev;
7f3c3d6f
HT
166 vendor = pci_get_vendor(realdev);
167 device = pci_get_device(realdev);
b3705d71
HT
168
169#if 0 /* XXX */
170 if (pci_get_class(kdev) != PCIC_DISPLAY
171 || pci_get_subclass(kdev) != PCIS_DISPLAY_VGA)
172 return ENXIO;
7f3c3d6f
HT
173#endif
174
175 id_entry = drm_find_description(vendor, device, idlist);
176 if (id_entry != NULL) {
b3705d71
HT
177 if (!device_get_desc(kdev)) {
178 DRM_DEBUG("desc : %s\n", device_get_desc(kdev));
179 device_set_desc(kdev, id_entry->name);
180 }
7f3c3d6f
HT
181 return 0;
182 }
183
184 return ENXIO;
185}
186
b3705d71 187int drm_attach(device_t kdev, drm_pci_id_list_t *idlist)
7f3c3d6f 188{
b3705d71 189 struct drm_device *dev;
7f3c3d6f
HT
190 drm_pci_id_list_t *id_entry;
191 int unit;
b3705d71
HT
192#if 0
193 int msicount;
194#endif
7f3c3d6f 195
b3705d71
HT
196 unit = device_get_unit(kdev);
197 dev = device_get_softc(kdev);
7f3c3d6f 198
b3705d71
HT
199 if (!strcmp(device_get_name(kdev), "drmsub"))
200 dev->device = device_get_parent(kdev);
7f3c3d6f 201 else
b3705d71
HT
202 dev->device = kdev;
203
7f3c3d6f 204 dev_ops_add(&drm_cdevsw, -1, unit);
7f3c3d6f
HT
205 dev->devnode = make_dev(&drm_cdevsw,
206 unit,
207 DRM_DEV_UID,
208 DRM_DEV_GID,
209 DRM_DEV_MODE,
210 "dri/card%d", unit);
7f3c3d6f 211
b3705d71
HT
212 dev->pci_domain = 0;
213 dev->pci_bus = pci_get_bus(dev->device);
214 dev->pci_slot = pci_get_slot(dev->device);
215 dev->pci_func = pci_get_function(dev->device);
7f3c3d6f 216
b3705d71
HT
217 dev->pci_vendor = pci_get_vendor(dev->device);
218 dev->pci_device = pci_get_device(dev->device);
7f3c3d6f 219
bd121a8b
HT
220 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) {
221 if (drm_msi &&
222 !drm_msi_is_blacklisted(dev->pci_vendor, dev->pci_device)) {
b3705d71 223#if 0
bd121a8b
HT
224 msicount = pci_msi_count(dev->device);
225 DRM_DEBUG("MSI count = %d\n", msicount);
226 if (msicount > 1)
227 msicount = 1;
228
229 if (pci_alloc_msi(dev->device, &msicount) == 0) {
230 DRM_INFO("MSI enabled %d message(s)\n",
231 msicount);
232 dev->msi_enabled = 1;
233 dev->irqrid = 1;
234 }
7f3c3d6f 235#endif
bd121a8b 236 }
7f3c3d6f 237
bd121a8b
HT
238 dev->irqr = bus_alloc_resource_any(dev->device, SYS_RES_IRQ,
239 &dev->irqrid, RF_SHAREABLE);
240 if (!dev->irqr) {
241 return ENOENT;
242 }
7f3c3d6f 243
bd121a8b
HT
244 dev->irq = (int) rman_get_start(dev->irqr);
245 }
7f3c3d6f 246
b3705d71
HT
247 DRM_SPININIT(&dev->dev_lock, "drmdev");
248 lwkt_serialize_init(&dev->irq_lock);
249 DRM_SPININIT(&dev->vbl_lock, "drmvbl");
250 DRM_SPININIT(&dev->drw_lock, "drmdrw");
7f3c3d6f 251
b3705d71
HT
252 id_entry = drm_find_description(dev->pci_vendor,
253 dev->pci_device, idlist);
254 dev->id_entry = id_entry;
7f3c3d6f 255
b3705d71 256 return drm_load(dev);
7f3c3d6f
HT
257}
258
b3705d71 259int drm_detach(device_t kdev)
7f3c3d6f 260{
b3705d71 261 struct drm_device *dev;
7f3c3d6f 262
b3705d71 263 dev = device_get_softc(kdev);
7f3c3d6f 264
b3705d71 265 drm_unload(dev);
a48a8c19
HT
266
267 if (dev->irqr) {
268 bus_release_resource(dev->device, SYS_RES_IRQ, dev->irqrid,
269 dev->irqr);
7f3c3d6f 270
b3705d71 271#if 0
a48a8c19
HT
272 if (dev->msi_enabled) {
273 pci_release_msi(dev->device);
274 DRM_INFO("MSI released\n");
275 }
b3705d71 276#endif
a48a8c19 277 }
7f3c3d6f
HT
278
279 return 0;
280}
281
b3705d71
HT
282#ifndef DRM_DEV_NAME
283#define DRM_DEV_NAME "drm"
284#endif
7f3c3d6f 285
b3705d71 286devclass_t drm_devclass;
7f3c3d6f
HT
287
288drm_pci_id_list_t *drm_find_description(int vendor, int device,
289 drm_pci_id_list_t *idlist)
290{
291 int i = 0;
292
293 for (i = 0; idlist[i].vendor != 0; i++) {
294 if ((idlist[i].vendor == vendor) &&
b3705d71
HT
295 ((idlist[i].device == device) ||
296 (idlist[i].device == 0))) {
7f3c3d6f
HT
297 return &idlist[i];
298 }
299 }
300 return NULL;
301}
302
b3705d71 303static int drm_firstopen(struct drm_device *dev)
7f3c3d6f
HT
304{
305 drm_local_map_t *map;
306 int i;
307
308 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
309
310 /* prebuild the SAREA */
311 i = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
b3705d71 312 _DRM_CONTAINS_LOCK, &map);
7f3c3d6f
HT
313 if (i != 0)
314 return i;
315
b3705d71
HT
316 if (dev->driver->firstopen)
317 dev->driver->firstopen(dev);
7f3c3d6f
HT
318
319 dev->buf_use = 0;
320
b3705d71 321 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
7f3c3d6f
HT
322 i = drm_dma_setup(dev);
323 if (i != 0)
324 return i;
325 }
326
b3705d71 327 for (i = 0; i < DRM_HASH_SIZE; i++) {
7f3c3d6f
HT
328 dev->magiclist[i].head = NULL;
329 dev->magiclist[i].tail = NULL;
330 }
331
332 dev->lock.lock_queue = 0;
333 dev->irq_enabled = 0;
334 dev->context_flag = 0;
335 dev->last_context = 0;
336 dev->if_version = 0;
337
7f3c3d6f 338 dev->buf_sigio = NULL;
7f3c3d6f 339
b3705d71 340 DRM_DEBUG("\n");
7f3c3d6f
HT
341
342 return 0;
343}
344
b3705d71 345static int drm_lastclose(struct drm_device *dev)
7f3c3d6f
HT
346{
347 drm_magic_entry_t *pt, *next;
348 drm_local_map_t *map, *mapsave;
349 int i;
350
351 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
352
b3705d71 353 DRM_DEBUG("\n");
7f3c3d6f 354
b3705d71
HT
355 if (dev->driver->lastclose != NULL)
356 dev->driver->lastclose(dev);
7f3c3d6f
HT
357
358 if (dev->irq_enabled)
359 drm_irq_uninstall(dev);
360
b3705d71
HT
361 if (dev->unique) {
362 free(dev->unique, DRM_MEM_DRIVER);
7f3c3d6f
HT
363 dev->unique = NULL;
364 dev->unique_len = 0;
365 }
b3705d71
HT
366 /* Clear pid list */
367 for (i = 0; i < DRM_HASH_SIZE; i++) {
368 for (pt = dev->magiclist[i].head; pt; pt = next) {
7f3c3d6f 369 next = pt->next;
b3705d71 370 free(pt, DRM_MEM_MAGIC);
7f3c3d6f
HT
371 }
372 dev->magiclist[i].head = dev->magiclist[i].tail = NULL;
373 }
374
b3705d71
HT
375 DRM_UNLOCK();
376 drm_drawable_free_all(dev);
377 DRM_LOCK();
378
379 /* Clear AGP information */
380 if (dev->agp) {
7f3c3d6f
HT
381 drm_agp_mem_t *entry;
382 drm_agp_mem_t *nexte;
383
384 /* Remove AGP resources, but leave dev->agp intact until
385 * drm_unload is called.
386 */
b3705d71 387 for (entry = dev->agp->memory; entry; entry = nexte) {
7f3c3d6f 388 nexte = entry->next;
b3705d71 389 if (entry->bound)
7f3c3d6f
HT
390 drm_agp_unbind_memory(entry->handle);
391 drm_agp_free_memory(entry->handle);
b3705d71 392 free(entry, DRM_MEM_AGPLISTS);
7f3c3d6f
HT
393 }
394 dev->agp->memory = NULL;
395
396 if (dev->agp->acquired)
397 drm_agp_release(dev);
398
399 dev->agp->acquired = 0;
400 dev->agp->enabled = 0;
401 }
402 if (dev->sg != NULL) {
403 drm_sg_cleanup(dev->sg);
404 dev->sg = NULL;
405 }
406
ecd27a2e 407 TAILQ_FOREACH_MUTABLE(map, &dev->maplist, link, mapsave) {
7f3c3d6f
HT
408 if (!(map->flags & _DRM_DRIVER))
409 drm_rmmap(dev, map);
410 }
411
412 drm_dma_takedown(dev);
b3705d71 413 if (dev->lock.hw_lock) {
7f3c3d6f
HT
414 dev->lock.hw_lock = NULL; /* SHM removed */
415 dev->lock.file_priv = NULL;
416 DRM_WAKEUP_INT((void *)&dev->lock.lock_queue);
417 }
418
419 return 0;
420}
421
b3705d71 422static int drm_load(struct drm_device *dev)
7f3c3d6f
HT
423{
424 int i, retcode;
425
b3705d71 426 DRM_DEBUG("\n");
7f3c3d6f
HT
427
428 TAILQ_INIT(&dev->maplist);
429
430 drm_mem_init();
7f3c3d6f 431 drm_sysctl_init(dev);
7f3c3d6f
HT
432 TAILQ_INIT(&dev->files);
433
434 dev->counters = 6;
435 dev->types[0] = _DRM_STAT_LOCK;
436 dev->types[1] = _DRM_STAT_OPENS;
437 dev->types[2] = _DRM_STAT_CLOSES;
438 dev->types[3] = _DRM_STAT_IOCTLS;
439 dev->types[4] = _DRM_STAT_LOCKS;
440 dev->types[5] = _DRM_STAT_UNLOCKS;
441
b3705d71
HT
442 for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++)
443 atomic_set(&dev->counts[i], 0);
7f3c3d6f 444
b3705d71 445 if (dev->driver->load != NULL) {
7f3c3d6f
HT
446 DRM_LOCK();
447 /* Shared code returns -errno. */
b3705d71 448 retcode = -dev->driver->load(dev,
7f3c3d6f 449 dev->id_entry->driver_private);
b3705d71 450 pci_enable_busmaster(dev->device);
7f3c3d6f
HT
451 DRM_UNLOCK();
452 if (retcode != 0)
453 goto error;
454 }
455
b3705d71 456 if (drm_core_has_AGP(dev)) {
7f3c3d6f
HT
457 if (drm_device_is_agp(dev))
458 dev->agp = drm_agp_init();
b3705d71
HT
459 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP) &&
460 dev->agp == NULL) {
7f3c3d6f
HT
461 DRM_ERROR("Card isn't AGP, or couldn't initialize "
462 "AGP.\n");
463 retcode = ENOMEM;
464 goto error;
465 }
466 if (dev->agp != NULL) {
467 if (drm_mtrr_add(dev->agp->info.ai_aperture_base,
468 dev->agp->info.ai_aperture_size, DRM_MTRR_WC) == 0)
469 dev->agp->mtrr = 1;
470 }
471 }
472
473 retcode = drm_ctxbitmap_init(dev);
474 if (retcode != 0) {
475 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
476 goto error;
477 }
478
7f3c3d6f 479 DRM_INFO("Initialized %s %d.%d.%d %s\n",
b3705d71
HT
480 dev->driver->name,
481 dev->driver->major,
482 dev->driver->minor,
483 dev->driver->patchlevel,
484 dev->driver->date);
7f3c3d6f
HT
485
486 return 0;
487
488error:
7f3c3d6f 489 drm_sysctl_cleanup(dev);
7f3c3d6f
HT
490 DRM_LOCK();
491 drm_lastclose(dev);
492 DRM_UNLOCK();
7f3c3d6f 493 destroy_dev(dev->devnode);
b3705d71
HT
494
495 DRM_SPINUNINIT(&dev->drw_lock);
496 DRM_SPINUNINIT(&dev->vbl_lock);
7f3c3d6f 497 DRM_SPINUNINIT(&dev->dev_lock);
b3705d71 498
7f3c3d6f
HT
499 return retcode;
500}
501
b3705d71 502static void drm_unload(struct drm_device *dev)
7f3c3d6f
HT
503{
504 int i;
505
b3705d71 506 DRM_DEBUG("\n");
7f3c3d6f 507
7f3c3d6f
HT
508 drm_sysctl_cleanup(dev);
509 destroy_dev(dev->devnode);
7f3c3d6f
HT
510
511 drm_ctxbitmap_cleanup(dev);
512
513 if (dev->agp && dev->agp->mtrr) {
514 int __unused retcode;
515
516 retcode = drm_mtrr_del(0, dev->agp->info.ai_aperture_base,
517 dev->agp->info.ai_aperture_size, DRM_MTRR_WC);
518 DRM_DEBUG("mtrr_del = %d", retcode);
519 }
520
b3705d71
HT
521 drm_vblank_cleanup(dev);
522
7f3c3d6f
HT
523 DRM_LOCK();
524 drm_lastclose(dev);
525 DRM_UNLOCK();
526
527 /* Clean up PCI resources allocated by drm_bufs.c. We're not really
528 * worried about resource consumption while the DRM is inactive (between
529 * lastclose and firstopen or unload) because these aren't actually
530 * taking up KVA, just keeping the PCI resource allocated.
531 */
532 for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
533 if (dev->pcir[i] == NULL)
534 continue;
535 bus_release_resource(dev->device, SYS_RES_MEMORY,
536 dev->pcirid[i], dev->pcir[i]);
537 dev->pcir[i] = NULL;
538 }
539
b3705d71
HT
540 if (dev->agp) {
541 free(dev->agp, DRM_MEM_AGPLISTS);
7f3c3d6f
HT
542 dev->agp = NULL;
543 }
544
b3705d71
HT
545 if (dev->driver->unload != NULL) {
546 DRM_LOCK();
547 dev->driver->unload(dev);
548 DRM_UNLOCK();
549 }
7f3c3d6f
HT
550
551 drm_mem_uninit();
b3705d71
HT
552
553 pci_disable_busmaster(dev->device);
554
555 DRM_SPINUNINIT(&dev->drw_lock);
556 DRM_SPINUNINIT(&dev->vbl_lock);
7f3c3d6f 557 DRM_SPINUNINIT(&dev->dev_lock);
7f3c3d6f
HT
558}
559
b3705d71 560int drm_version(struct drm_device *dev, void *data, struct drm_file *file_priv)
7f3c3d6f 561{
b3705d71 562 struct drm_version *version = data;
7f3c3d6f
HT
563 int len;
564
565#define DRM_COPY( name, value ) \
566 len = strlen( value ); \
567 if ( len > name##_len ) len = name##_len; \
568 name##_len = strlen( value ); \
569 if ( len && name ) { \
570 if ( DRM_COPY_TO_USER( name, value, len ) ) \
571 return EFAULT; \
572 }
573
b3705d71
HT
574 version->version_major = dev->driver->major;
575 version->version_minor = dev->driver->minor;
576 version->version_patchlevel = dev->driver->patchlevel;
7f3c3d6f 577
b3705d71
HT
578 DRM_COPY(version->name, dev->driver->name);
579 DRM_COPY(version->date, dev->driver->date);
580 DRM_COPY(version->desc, dev->driver->desc);
7f3c3d6f
HT
581
582 return 0;
583}
584
7f3c3d6f
HT
585int drm_open(struct dev_open_args *ap)
586{
587 struct cdev *kdev = ap->a_head.a_dev;
588 int flags = ap->a_oflags;
589 int fmt = 0;
590 struct thread *p = curthread;
b3705d71 591 struct drm_device *dev = NULL;
7f3c3d6f
HT
592 int retcode = 0;
593
594 dev = DRIVER_SOFTC(minor(kdev));
595
b3705d71 596 DRM_DEBUG("open_count = %d\n", dev->open_count);
7f3c3d6f
HT
597
598 retcode = drm_open_helper(kdev, flags, fmt, p, dev);
599
b3705d71
HT
600 if (!retcode) {
601 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
7f3c3d6f 602 DRM_LOCK();
7f3c3d6f 603 device_busy(dev->device);
b3705d71 604 if (!dev->open_count++)
7f3c3d6f
HT
605 retcode = drm_firstopen(dev);
606 DRM_UNLOCK();
607 }
608
609 return retcode;
610}
611
2c845b81 612int drm_close(struct dev_close_args *ap)
7f3c3d6f 613{
2c845b81
SS
614 struct cdev *kdev = ap->a_head.a_dev;
615 struct drm_file *file_priv;
616 struct drm_device *dev;
7f3c3d6f
HT
617 int retcode = 0;
618
2c845b81
SS
619 dev = DRIVER_SOFTC(minor(kdev));
620 file_priv = drm_find_file_by_proc(dev, curthread);
621
b3705d71 622 DRM_DEBUG("open_count = %d\n", dev->open_count);
7f3c3d6f
HT
623
624 DRM_LOCK();
625
7f3c3d6f
HT
626 if (--file_priv->refs != 0)
627 goto done;
628
b3705d71
HT
629 if (dev->driver->preclose != NULL)
630 dev->driver->preclose(dev, file_priv);
7f3c3d6f
HT
631
632 /* ========================================================
633 * Begin inline drm_release
634 */
635
b3705d71
HT
636 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
637 DRM_CURRENTPID, (long)dev->device, dev->open_count);
7f3c3d6f
HT
638
639 if (dev->lock.hw_lock && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
640 && dev->lock.file_priv == file_priv) {
641 DRM_DEBUG("Process %d dead, freeing lock for context %d\n",
642 DRM_CURRENTPID,
643 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
b3705d71
HT
644 if (dev->driver->reclaim_buffers_locked != NULL)
645 dev->driver->reclaim_buffers_locked(dev, file_priv);
7f3c3d6f 646
b3705d71 647 drm_lock_free(&dev->lock,
7f3c3d6f
HT
648 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
649
650 /* FIXME: may require heavy-handed reset of
651 hardware at this point, possibly
652 processed via a callback to the X
653 server. */
b3705d71 654 } else if (dev->driver->reclaim_buffers_locked != NULL &&
7f3c3d6f
HT
655 dev->lock.hw_lock != NULL) {
656 /* The lock is required to reclaim buffers */
657 for (;;) {
b3705d71 658 if (!dev->lock.hw_lock) {
7f3c3d6f
HT
659 /* Device has been unregistered */
660 retcode = EINTR;
661 break;
662 }
b3705d71 663 if (drm_lock_take(&dev->lock, DRM_KERNEL_CONTEXT)) {
7f3c3d6f
HT
664 dev->lock.file_priv = file_priv;
665 dev->lock.lock_time = jiffies;
b3705d71 666 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
7f3c3d6f
HT
667 break; /* Got lock */
668 }
b3705d71 669 /* Contention */
7f3c3d6f
HT
670 crit_enter();
671 tsleep_interlock((void *)&dev->lock.lock_queue);
672 DRM_UNLOCK();
673 retcode = tsleep((void *)&dev->lock.lock_queue,
674 PCATCH, "drmlk2", 0);
675 crit_exit();
676 DRM_LOCK();
7f3c3d6f
HT
677 if (retcode)
678 break;
679 }
680 if (retcode == 0) {
b3705d71
HT
681 dev->driver->reclaim_buffers_locked(dev, file_priv);
682 drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT);
7f3c3d6f
HT
683 }
684 }
685
b3705d71
HT
686 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
687 !dev->driver->reclaim_buffers_locked)
7f3c3d6f
HT
688 drm_reclaim_buffers(dev, file_priv);
689
7f3c3d6f 690 funsetown(dev->buf_sigio);
7f3c3d6f 691
b3705d71
HT
692 if (dev->driver->postclose != NULL)
693 dev->driver->postclose(dev, file_priv);
7f3c3d6f 694 TAILQ_REMOVE(&dev->files, file_priv, link);
b3705d71 695 free(file_priv, DRM_MEM_FILES);
7f3c3d6f
HT
696
697 /* ========================================================
698 * End inline drm_release
699 */
7f3c3d6f 700done:
b3705d71 701 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
7f3c3d6f 702 device_unbusy(dev->device);
7f3c3d6f
HT
703 if (--dev->open_count == 0) {
704 retcode = drm_lastclose(dev);
705 }
706
707 DRM_UNLOCK();
2c845b81
SS
708
709 return (0);
7f3c3d6f
HT
710}
711
712/* drm_ioctl is called whenever a process performs an ioctl on /dev/drm.
713 */
7f3c3d6f
HT
714int drm_ioctl(struct dev_ioctl_args *ap)
715{
716 struct cdev *kdev = ap->a_head.a_dev;
717 u_long cmd = ap->a_cmd;
718 caddr_t data = ap->a_data;
719 struct thread *p = curthread;
b3705d71 720 struct drm_device *dev = drm_get_device_from_kdev(kdev);
7f3c3d6f
HT
721 int retcode = 0;
722 drm_ioctl_desc_t *ioctl;
b3705d71 723 int (*func)(struct drm_device *dev, void *data, struct drm_file *file_priv);
7f3c3d6f
HT
724 int nr = DRM_IOCTL_NR(cmd);
725 int is_driver_ioctl = 0;
b3705d71 726 struct drm_file *file_priv;
7f3c3d6f 727
7f3c3d6f 728 file_priv = drm_find_file_by_proc(dev, p);
b3705d71 729 if (!file_priv) {
7f3c3d6f
HT
730 DRM_ERROR("can't find authenticator\n");
731 return EINVAL;
732 }
733
b3705d71 734 atomic_inc(&dev->counts[_DRM_STAT_IOCTLS]);
7f3c3d6f
HT
735 ++file_priv->ioctl_count;
736
b3705d71 737 DRM_DEBUG("pid=%d, cmd=0x%02lx, nr=0x%02x, dev 0x%lx, auth=%d\n",
7f3c3d6f 738 DRM_CURRENTPID, cmd, nr, (long)dev->device,
b3705d71 739 file_priv->authenticated);
7f3c3d6f
HT
740
741 switch (cmd) {
742 case FIONBIO:
743 case FIOASYNC:
744 return 0;
745
7f3c3d6f
HT
746 case FIOSETOWN:
747 return fsetown(*(int *)data, &dev->buf_sigio);
748
749 case FIOGETOWN:
7f3c3d6f 750 *(int *) data = fgetown(dev->buf_sigio);
7f3c3d6f 751 return 0;
7f3c3d6f
HT
752 }
753
754 if (IOCGROUP(cmd) != DRM_IOCTL_BASE) {
755 DRM_DEBUG("Bad ioctl group 0x%x\n", (int)IOCGROUP(cmd));
756 return EINVAL;
757 }
758
759 ioctl = &drm_ioctls[nr];
760 /* It's not a core DRM ioctl, try driver-specific. */
761 if (ioctl->func == NULL && nr >= DRM_COMMAND_BASE) {
762 /* The array entries begin at DRM_COMMAND_BASE ioctl nr */
763 nr -= DRM_COMMAND_BASE;
b3705d71 764 if (nr > dev->driver->max_ioctl) {
7f3c3d6f 765 DRM_DEBUG("Bad driver ioctl number, 0x%x (of 0x%x)\n",
b3705d71 766 nr, dev->driver->max_ioctl);
7f3c3d6f
HT
767 return EINVAL;
768 }
b3705d71 769 ioctl = &dev->driver->ioctls[nr];
7f3c3d6f
HT
770 is_driver_ioctl = 1;
771 }
772 func = ioctl->func;
773
774 if (func == NULL) {
b3705d71 775 DRM_DEBUG("no function\n");
7f3c3d6f
HT
776 return EINVAL;
777 }
778
779 if (((ioctl->flags & DRM_ROOT_ONLY) && !DRM_SUSER(p)) ||
780 ((ioctl->flags & DRM_AUTH) && !file_priv->authenticated) ||
781 ((ioctl->flags & DRM_MASTER) && !file_priv->master))
782 return EACCES;
783
784 if (is_driver_ioctl) {
785 DRM_LOCK();
786 /* shared code returns -errno */
787 retcode = -func(dev, data, file_priv);
788 DRM_UNLOCK();
789 } else {
790 retcode = func(dev, data, file_priv);
791 }
792
793 if (retcode != 0)
794 DRM_DEBUG(" returning %d\n", retcode);
795
796 return retcode;
797}
798
b3705d71 799drm_local_map_t *drm_getsarea(struct drm_device *dev)
7f3c3d6f
HT
800{
801 drm_local_map_t *map;
802
803 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
804 TAILQ_FOREACH(map, &dev->maplist, link) {
805 if (map->type == _DRM_SHM && (map->flags & _DRM_CONTAINS_LOCK))
806 return map;
807 }
808
809 return NULL;
810}
811
812#if DRM_LINUX
813
814#include <sys/sysproto.h>
815
816MODULE_DEPEND(DRIVER_NAME, linux, 1, 1, 1);
817
818#define LINUX_IOCTL_DRM_MIN 0x6400
819#define LINUX_IOCTL_DRM_MAX 0x64ff
820
821static linux_ioctl_function_t drm_linux_ioctl;
822static struct linux_ioctl_handler drm_handler = {drm_linux_ioctl,
823 LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX};
824
825SYSINIT(drm_register, SI_SUB_KLD, SI_ORDER_MIDDLE,
826 linux_ioctl_register_handler, &drm_handler);
827SYSUNINIT(drm_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE,
828 linux_ioctl_unregister_handler, &drm_handler);
829
830/* The bits for in/out are switched on Linux */
831#define LINUX_IOC_IN IOC_OUT
832#define LINUX_IOC_OUT IOC_IN
833
834static int
835drm_linux_ioctl(DRM_STRUCTPROC *p, struct linux_ioctl_args* args)
836{
837 int error;
838 int cmd = args->cmd;
839
840 args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
841 if (cmd & LINUX_IOC_IN)
842 args->cmd |= IOC_IN;
843 if (cmd & LINUX_IOC_OUT)
844 args->cmd |= IOC_OUT;
845
846 error = ioctl(p, (struct ioctl_args *)args);
847
848 return error;
849}
850#endif /* DRM_LINUX */