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 * 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 /** @file drm_drv.c 35 * The catch-all file for DRM device support, including module setup/teardown, 36 * open/close, and ioctl dispatch. 37 */ 38 39 #include <sys/limits.h> 40 #include <sys/sysent.h> 41 #include <dev/drm2/drmP.h> 42 #include <dev/drm2/drm.h> 43 #include <dev/drm2/drm_sarea.h> 44 #include <dev/drm2/drm_mode.h> 45 46 #ifdef DRM_DEBUG_DEFAULT_ON 47 int drm_debug_flag = 1; 48 #else 49 int drm_debug_flag = 2; 50 #endif 51 int drm_notyet_flag = 0; 52 53 unsigned int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */ 54 unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */ 55 56 static int drm_load(struct drm_device *dev); 57 static void drm_unload(struct drm_device *dev); 58 static drm_pci_id_list_t *drm_find_description(int vendor, int device, 59 drm_pci_id_list_t *idlist); 60 61 static int 62 drm_modevent(module_t mod, int type, void *data) 63 { 64 65 switch (type) { 66 case MOD_LOAD: 67 TUNABLE_INT_FETCH("drm.debug", &drm_debug_flag); 68 TUNABLE_INT_FETCH("drm.notyet", &drm_notyet_flag); 69 break; 70 } 71 return (0); 72 } 73 74 static moduledata_t drm_mod = { 75 "drmn", 76 drm_modevent, 77 0 78 }; 79 DECLARE_MODULE(drmn, drm_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 80 MODULE_VERSION(drmn, 1); 81 MODULE_DEPEND(drmn, agp, 1, 1, 1); 82 MODULE_DEPEND(drmn, pci, 1, 1, 1); 83 MODULE_DEPEND(drmn, mem, 1, 1, 1); 84 MODULE_DEPEND(drmn, iicbus, 1, 1, 1); 85 86 static drm_ioctl_desc_t drm_ioctls[256] = { 87 DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, 0), 88 DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0), 89 DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0), 90 DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY), 91 DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_getmap, 0), 92 DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, 0), 93 DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, 0), 94 DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_UNLOCKED), 95 DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER|DRM_ROOT_ONLY), 96 97 DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 98 DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 99 DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 100 DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 101 102 DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 103 DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_rmmap_ioctl, DRM_AUTH), 104 105 DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 106 DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_getsareactx, DRM_AUTH), 107 108 DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_ROOT_ONLY), 109 DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_ROOT_ONLY), 110 111 DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_addctx, DRM_AUTH|DRM_ROOT_ONLY), 112 DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 113 DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_modctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 114 DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_getctx, DRM_AUTH), 115 DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 116 DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 117 DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_resctx, DRM_AUTH), 118 119 DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_adddraw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 120 DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_rmdraw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 121 122 DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_lock, DRM_AUTH), 123 DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_unlock, DRM_AUTH), 124 125 DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH), 126 127 DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 128 DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 129 DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_infobufs, DRM_AUTH), 130 DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_mapbufs, DRM_AUTH), 131 DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_freebufs, DRM_AUTH), 132 DRM_IOCTL_DEF(DRM_IOCTL_DMA, drm_dma, DRM_AUTH), 133 134 DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 135 136 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 137 DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 138 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 139 DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH), 140 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 141 DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 142 DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 143 DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 144 145 DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_sg_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 146 DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 147 DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, DRM_UNLOCKED), 148 DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_modeset_ctl, 0), 149 DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_update_draw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 150 151 DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_UNLOCKED), 152 DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH|DRM_UNLOCKED), 153 DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH|DRM_UNLOCKED), 154 155 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 156 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 157 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 158 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 159 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 160 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 161 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 162 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, DRM_MASTER|DRM_UNLOCKED), 163 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER|DRM_UNLOCKED), 164 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 165 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 166 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_mode_attachmode_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 167 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_mode_detachmode_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 168 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, DRM_MASTER | DRM_CONTROL_ALLOW|DRM_UNLOCKED), 169 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_mode_connector_property_set_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 170 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 171 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 172 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 173 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 174 DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 175 DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 176 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 177 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 178 DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 179 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED), 180 }; 181 182 static struct cdevsw drm_cdevsw = { 183 .d_version = D_VERSION, 184 .d_open = drm_open, 185 .d_read = drm_read, 186 .d_ioctl = drm_ioctl, 187 .d_poll = drm_poll, 188 .d_mmap = drm_mmap, 189 .d_mmap_single = drm_gem_mmap_single, 190 .d_name = "drm", 191 .d_flags = D_TRACKCLOSE 192 }; 193 194 static int drm_msi = 1; /* Enable by default. */ 195 TUNABLE_INT("hw.drm.msi", &drm_msi); 196 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device"); 197 SYSCTL_INT(_hw_drm, OID_AUTO, msi, CTLFLAG_RDTUN, &drm_msi, 1, 198 "Enable MSI interrupts for drm devices"); 199 200 static struct drm_msi_blacklist_entry drm_msi_blacklist[] = { 201 {0x8086, 0x2772}, /* Intel i945G */ \ 202 {0x8086, 0x27A2}, /* Intel i945GM */ \ 203 {0x8086, 0x27AE}, /* Intel i945GME */ \ 204 {0, 0} 205 }; 206 207 static int drm_msi_is_blacklisted(int vendor, int device) 208 { 209 int i = 0; 210 211 for (i = 0; drm_msi_blacklist[i].vendor != 0; i++) { 212 if ((drm_msi_blacklist[i].vendor == vendor) && 213 (drm_msi_blacklist[i].device == device)) { 214 return 1; 215 } 216 } 217 218 return 0; 219 } 220 221 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist) 222 { 223 drm_pci_id_list_t *id_entry; 224 int vendor, device; 225 226 vendor = pci_get_vendor(kdev); 227 device = pci_get_device(kdev); 228 229 if (pci_get_class(kdev) != PCIC_DISPLAY 230 || pci_get_subclass(kdev) != PCIS_DISPLAY_VGA) 231 return ENXIO; 232 233 id_entry = drm_find_description(vendor, device, idlist); 234 if (id_entry != NULL) { 235 if (!device_get_desc(kdev)) { 236 DRM_DEBUG("desc : %s\n", device_get_desc(kdev)); 237 device_set_desc(kdev, id_entry->name); 238 } 239 return 0; 240 } 241 242 return ENXIO; 243 } 244 245 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist) 246 { 247 struct drm_device *dev; 248 drm_pci_id_list_t *id_entry; 249 int error, msicount; 250 251 dev = device_get_softc(kdev); 252 253 dev->device = kdev; 254 255 dev->pci_domain = pci_get_domain(dev->device); 256 dev->pci_bus = pci_get_bus(dev->device); 257 dev->pci_slot = pci_get_slot(dev->device); 258 dev->pci_func = pci_get_function(dev->device); 259 260 dev->pci_vendor = pci_get_vendor(dev->device); 261 dev->pci_device = pci_get_device(dev->device); 262 263 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) { 264 if (drm_msi && 265 !drm_msi_is_blacklisted(dev->pci_vendor, dev->pci_device)) { 266 msicount = pci_msi_count(dev->device); 267 DRM_DEBUG("MSI count = %d\n", msicount); 268 if (msicount > 1) 269 msicount = 1; 270 271 if (pci_alloc_msi(dev->device, &msicount) == 0) { 272 DRM_INFO("MSI enabled %d message(s)\n", 273 msicount); 274 dev->msi_enabled = 1; 275 dev->irqrid = 1; 276 } 277 } 278 279 dev->irqr = bus_alloc_resource_any(dev->device, SYS_RES_IRQ, 280 &dev->irqrid, RF_SHAREABLE); 281 if (!dev->irqr) { 282 return (ENOENT); 283 } 284 285 dev->irq = (int) rman_get_start(dev->irqr); 286 } 287 288 mtx_init(&dev->dev_lock, "drmdev", NULL, MTX_DEF); 289 mtx_init(&dev->irq_lock, "drmirq", NULL, MTX_DEF); 290 mtx_init(&dev->vbl_lock, "drmvbl", NULL, MTX_DEF); 291 mtx_init(&dev->drw_lock, "drmdrw", NULL, MTX_DEF); 292 mtx_init(&dev->event_lock, "drmev", NULL, MTX_DEF); 293 sx_init(&dev->dev_struct_lock, "drmslk"); 294 295 id_entry = drm_find_description(dev->pci_vendor, 296 dev->pci_device, idlist); 297 dev->id_entry = id_entry; 298 299 error = drm_load(dev); 300 if (error == 0) 301 error = drm_create_cdevs(kdev); 302 return (error); 303 } 304 305 int 306 drm_create_cdevs(device_t kdev) 307 { 308 struct drm_device *dev; 309 int error, unit; 310 311 unit = device_get_unit(kdev); 312 dev = device_get_softc(kdev); 313 314 error = make_dev_p(MAKEDEV_WAITOK | MAKEDEV_CHECKNAME, &dev->devnode, 315 &drm_cdevsw, 0, DRM_DEV_UID, DRM_DEV_GID, 316 DRM_DEV_MODE, "dri/card%d", unit); 317 if (error == 0) 318 dev->devnode->si_drv1 = dev; 319 return (error); 320 } 321 322 int drm_detach(device_t kdev) 323 { 324 struct drm_device *dev; 325 326 dev = device_get_softc(kdev); 327 drm_unload(dev); 328 if (dev->irqr) { 329 bus_release_resource(dev->device, SYS_RES_IRQ, dev->irqrid, 330 dev->irqr); 331 if (dev->msi_enabled) { 332 pci_release_msi(dev->device); 333 DRM_INFO("MSI released\n"); 334 } 335 } 336 return (0); 337 } 338 339 #ifndef DRM_DEV_NAME 340 #define DRM_DEV_NAME "drm" 341 #endif 342 343 devclass_t drm_devclass; 344 345 drm_pci_id_list_t *drm_find_description(int vendor, int device, 346 drm_pci_id_list_t *idlist) 347 { 348 int i = 0; 349 350 for (i = 0; idlist[i].vendor != 0; i++) { 351 if ((idlist[i].vendor == vendor) && 352 ((idlist[i].device == device) || 353 (idlist[i].device == 0))) { 354 return &idlist[i]; 355 } 356 } 357 return NULL; 358 } 359 360 static int drm_firstopen(struct drm_device *dev) 361 { 362 drm_local_map_t *map; 363 int i; 364 365 DRM_LOCK_ASSERT(dev); 366 367 /* prebuild the SAREA */ 368 i = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM, 369 _DRM_CONTAINS_LOCK, &map); 370 if (i != 0) 371 return i; 372 373 if (dev->driver->firstopen) 374 dev->driver->firstopen(dev); 375 376 dev->buf_use = 0; 377 378 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) { 379 i = drm_dma_setup(dev); 380 if (i != 0) 381 return i; 382 } 383 384 for (i = 0; i < DRM_HASH_SIZE; i++) { 385 dev->magiclist[i].head = NULL; 386 dev->magiclist[i].tail = NULL; 387 } 388 389 dev->lock.lock_queue = 0; 390 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 391 dev->irq_enabled = 0; 392 dev->context_flag = 0; 393 dev->last_context = 0; 394 dev->if_version = 0; 395 396 dev->buf_sigio = NULL; 397 398 DRM_DEBUG("\n"); 399 400 return 0; 401 } 402 403 static int drm_lastclose(struct drm_device *dev) 404 { 405 drm_magic_entry_t *pt, *next; 406 drm_local_map_t *map, *mapsave; 407 int i; 408 409 DRM_LOCK_ASSERT(dev); 410 411 DRM_DEBUG("\n"); 412 413 if (dev->driver->lastclose != NULL) 414 dev->driver->lastclose(dev); 415 416 if (!drm_core_check_feature(dev, DRIVER_MODESET) && dev->irq_enabled) 417 drm_irq_uninstall(dev); 418 419 if (dev->unique) { 420 free(dev->unique, DRM_MEM_DRIVER); 421 dev->unique = NULL; 422 dev->unique_len = 0; 423 } 424 /* Clear pid list */ 425 for (i = 0; i < DRM_HASH_SIZE; i++) { 426 for (pt = dev->magiclist[i].head; pt; pt = next) { 427 next = pt->next; 428 free(pt, DRM_MEM_MAGIC); 429 } 430 dev->magiclist[i].head = dev->magiclist[i].tail = NULL; 431 } 432 433 DRM_UNLOCK(dev); 434 drm_drawable_free_all(dev); 435 DRM_LOCK(dev); 436 437 /* Clear AGP information */ 438 if (dev->agp) { 439 drm_agp_mem_t *entry; 440 drm_agp_mem_t *nexte; 441 442 /* Remove AGP resources, but leave dev->agp intact until 443 * drm_unload is called. 444 */ 445 for (entry = dev->agp->memory; entry; entry = nexte) { 446 nexte = entry->next; 447 if (entry->bound) 448 drm_agp_unbind_memory(entry->handle); 449 drm_agp_free_memory(entry->handle); 450 free(entry, DRM_MEM_AGPLISTS); 451 } 452 dev->agp->memory = NULL; 453 454 if (dev->agp->acquired) 455 drm_agp_release(dev); 456 457 dev->agp->acquired = 0; 458 dev->agp->enabled = 0; 459 } 460 if (dev->sg != NULL) { 461 drm_sg_cleanup(dev->sg); 462 dev->sg = NULL; 463 } 464 465 TAILQ_FOREACH_SAFE(map, &dev->maplist, link, mapsave) { 466 if (!(map->flags & _DRM_DRIVER)) 467 drm_rmmap(dev, map); 468 } 469 470 drm_dma_takedown(dev); 471 if (dev->lock.hw_lock) { 472 dev->lock.hw_lock = NULL; /* SHM removed */ 473 dev->lock.file_priv = NULL; 474 DRM_WAKEUP_INT((void *)&dev->lock.lock_queue); 475 } 476 477 return 0; 478 } 479 480 static int drm_load(struct drm_device *dev) 481 { 482 int i, retcode; 483 484 DRM_DEBUG("\n"); 485 486 TAILQ_INIT(&dev->maplist); 487 dev->map_unrhdr = new_unrhdr(1, ((1 << DRM_MAP_HANDLE_BITS) - 1), NULL); 488 if (dev->map_unrhdr == NULL) { 489 DRM_ERROR("Couldn't allocate map number allocator\n"); 490 return EINVAL; 491 } 492 493 494 drm_mem_init(); 495 drm_sysctl_init(dev); 496 TAILQ_INIT(&dev->files); 497 498 dev->counters = 6; 499 dev->types[0] = _DRM_STAT_LOCK; 500 dev->types[1] = _DRM_STAT_OPENS; 501 dev->types[2] = _DRM_STAT_CLOSES; 502 dev->types[3] = _DRM_STAT_IOCTLS; 503 dev->types[4] = _DRM_STAT_LOCKS; 504 dev->types[5] = _DRM_STAT_UNLOCKS; 505 506 for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++) 507 atomic_set(&dev->counts[i], 0); 508 509 INIT_LIST_HEAD(&dev->vblank_event_list); 510 511 if (drm_core_has_AGP(dev)) { 512 if (drm_device_is_agp(dev)) 513 dev->agp = drm_agp_init(); 514 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP) && 515 dev->agp == NULL) { 516 DRM_ERROR("Card isn't AGP, or couldn't initialize " 517 "AGP.\n"); 518 retcode = ENOMEM; 519 goto error; 520 } 521 if (dev->agp != NULL && dev->agp->info.ai_aperture_base != 0) { 522 if (drm_mtrr_add(dev->agp->info.ai_aperture_base, 523 dev->agp->info.ai_aperture_size, DRM_MTRR_WC) == 0) 524 dev->agp->mtrr = 1; 525 } 526 } 527 528 retcode = drm_ctxbitmap_init(dev); 529 if (retcode != 0) { 530 DRM_ERROR("Cannot allocate memory for context bitmap.\n"); 531 goto error; 532 } 533 534 dev->drw_unrhdr = new_unrhdr(1, INT_MAX, NULL); 535 if (dev->drw_unrhdr == NULL) { 536 DRM_ERROR("Couldn't allocate drawable number allocator\n"); 537 retcode = ENOMEM; 538 goto error; 539 } 540 541 if (dev->driver->driver_features & DRIVER_GEM) { 542 retcode = drm_gem_init(dev); 543 if (retcode != 0) { 544 DRM_ERROR("Cannot initialize graphics execution " 545 "manager (GEM)\n"); 546 goto error1; 547 } 548 } 549 550 if (dev->driver->load != NULL) { 551 DRM_LOCK(dev); 552 /* Shared code returns -errno. */ 553 retcode = -dev->driver->load(dev, 554 dev->id_entry->driver_private); 555 if (pci_enable_busmaster(dev->device)) 556 DRM_ERROR("Request to enable bus-master failed.\n"); 557 DRM_UNLOCK(dev); 558 if (retcode != 0) 559 goto error; 560 } 561 562 DRM_INFO("Initialized %s %d.%d.%d %s\n", 563 dev->driver->name, 564 dev->driver->major, 565 dev->driver->minor, 566 dev->driver->patchlevel, 567 dev->driver->date); 568 569 return 0; 570 571 error1: 572 delete_unrhdr(dev->drw_unrhdr); 573 error: 574 drm_sysctl_cleanup(dev); 575 DRM_LOCK(dev); 576 drm_lastclose(dev); 577 DRM_UNLOCK(dev); 578 if (dev->devnode != NULL) 579 destroy_dev(dev->devnode); 580 581 mtx_destroy(&dev->drw_lock); 582 mtx_destroy(&dev->vbl_lock); 583 mtx_destroy(&dev->irq_lock); 584 mtx_destroy(&dev->dev_lock); 585 mtx_destroy(&dev->event_lock); 586 sx_destroy(&dev->dev_struct_lock); 587 588 return retcode; 589 } 590 591 static void drm_unload(struct drm_device *dev) 592 { 593 int i; 594 595 DRM_DEBUG("\n"); 596 597 drm_sysctl_cleanup(dev); 598 if (dev->devnode != NULL) 599 destroy_dev(dev->devnode); 600 601 drm_ctxbitmap_cleanup(dev); 602 603 if (dev->driver->driver_features & DRIVER_GEM) 604 drm_gem_destroy(dev); 605 606 if (dev->agp && dev->agp->mtrr) { 607 int __unused retcode; 608 609 retcode = drm_mtrr_del(0, dev->agp->info.ai_aperture_base, 610 dev->agp->info.ai_aperture_size, DRM_MTRR_WC); 611 DRM_DEBUG("mtrr_del = %d", retcode); 612 } 613 614 drm_vblank_cleanup(dev); 615 616 DRM_LOCK(dev); 617 drm_lastclose(dev); 618 DRM_UNLOCK(dev); 619 620 /* Clean up PCI resources allocated by drm_bufs.c. We're not really 621 * worried about resource consumption while the DRM is inactive (between 622 * lastclose and firstopen or unload) because these aren't actually 623 * taking up KVA, just keeping the PCI resource allocated. 624 */ 625 for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) { 626 if (dev->pcir[i] == NULL) 627 continue; 628 bus_release_resource(dev->device, SYS_RES_MEMORY, 629 dev->pcirid[i], dev->pcir[i]); 630 dev->pcir[i] = NULL; 631 } 632 633 if (dev->agp) { 634 free(dev->agp, DRM_MEM_AGPLISTS); 635 dev->agp = NULL; 636 } 637 638 if (dev->driver->unload != NULL) { 639 DRM_LOCK(dev); 640 dev->driver->unload(dev); 641 DRM_UNLOCK(dev); 642 } 643 644 delete_unrhdr(dev->drw_unrhdr); 645 delete_unrhdr(dev->map_unrhdr); 646 647 drm_mem_uninit(); 648 649 if (pci_disable_busmaster(dev->device)) 650 DRM_ERROR("Request to disable bus-master failed.\n"); 651 652 mtx_destroy(&dev->drw_lock); 653 mtx_destroy(&dev->vbl_lock); 654 mtx_destroy(&dev->irq_lock); 655 mtx_destroy(&dev->dev_lock); 656 mtx_destroy(&dev->event_lock); 657 sx_destroy(&dev->dev_struct_lock); 658 } 659 660 int drm_version(struct drm_device *dev, void *data, struct drm_file *file_priv) 661 { 662 struct drm_version *version = data; 663 int len; 664 665 #define DRM_COPY( name, value ) \ 666 len = strlen( value ); \ 667 if ( len > name##_len ) len = name##_len; \ 668 name##_len = strlen( value ); \ 669 if ( len && name ) { \ 670 if ( DRM_COPY_TO_USER( name, value, len ) ) \ 671 return EFAULT; \ 672 } 673 674 version->version_major = dev->driver->major; 675 version->version_minor = dev->driver->minor; 676 version->version_patchlevel = dev->driver->patchlevel; 677 678 DRM_COPY(version->name, dev->driver->name); 679 DRM_COPY(version->date, dev->driver->date); 680 DRM_COPY(version->desc, dev->driver->desc); 681 682 return 0; 683 } 684 685 int 686 drm_open(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p) 687 { 688 struct drm_device *dev; 689 int retcode; 690 691 dev = kdev->si_drv1; 692 if (dev == NULL) 693 return (ENXIO); 694 695 DRM_DEBUG("open_count = %d\n", dev->open_count); 696 697 retcode = drm_open_helper(kdev, flags, fmt, p, dev); 698 699 if (retcode == 0) { 700 atomic_inc(&dev->counts[_DRM_STAT_OPENS]); 701 DRM_LOCK(dev); 702 mtx_lock(&Giant); 703 device_busy(dev->device); 704 mtx_unlock(&Giant); 705 if (!dev->open_count++) 706 retcode = drm_firstopen(dev); 707 DRM_UNLOCK(dev); 708 } 709 710 return (retcode); 711 } 712 713 void drm_close(void *data) 714 { 715 struct drm_file *file_priv = data; 716 struct drm_device *dev = file_priv->dev; 717 int retcode = 0; 718 719 DRM_DEBUG("open_count = %d\n", dev->open_count); 720 721 DRM_LOCK(dev); 722 723 if (dev->driver->preclose != NULL) 724 dev->driver->preclose(dev, file_priv); 725 726 /* ======================================================== 727 * Begin inline drm_release 728 */ 729 730 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", 731 DRM_CURRENTPID, (long)dev->device, dev->open_count); 732 733 if (dev->driver->driver_features & DRIVER_GEM) 734 drm_gem_release(dev, file_priv); 735 736 if (dev->lock.hw_lock && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) 737 && dev->lock.file_priv == file_priv) { 738 DRM_DEBUG("Process %d dead, freeing lock for context %d\n", 739 DRM_CURRENTPID, 740 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock)); 741 if (dev->driver->reclaim_buffers_locked != NULL) 742 dev->driver->reclaim_buffers_locked(dev, file_priv); 743 744 drm_lock_free(&dev->lock, 745 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock)); 746 747 /* FIXME: may require heavy-handed reset of 748 hardware at this point, possibly 749 processed via a callback to the X 750 server. */ 751 } else if (dev->driver->reclaim_buffers_locked != NULL && 752 dev->lock.hw_lock != NULL) { 753 /* The lock is required to reclaim buffers */ 754 for (;;) { 755 if (!dev->lock.hw_lock) { 756 /* Device has been unregistered */ 757 retcode = EINTR; 758 break; 759 } 760 if (drm_lock_take(&dev->lock, DRM_KERNEL_CONTEXT)) { 761 dev->lock.file_priv = file_priv; 762 dev->lock.lock_time = jiffies; 763 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]); 764 break; /* Got lock */ 765 } 766 /* Contention */ 767 retcode = DRM_LOCK_SLEEP(dev, &dev->lock.lock_queue, 768 PCATCH, "drmlk2", 0); 769 if (retcode) 770 break; 771 } 772 if (retcode == 0) { 773 dev->driver->reclaim_buffers_locked(dev, file_priv); 774 drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT); 775 } 776 } 777 778 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) && 779 !dev->driver->reclaim_buffers_locked) 780 drm_reclaim_buffers(dev, file_priv); 781 782 funsetown(&dev->buf_sigio); 783 seldrain(&file_priv->event_poll); 784 785 if (dev->driver->postclose != NULL) 786 dev->driver->postclose(dev, file_priv); 787 TAILQ_REMOVE(&dev->files, file_priv, link); 788 free(file_priv, DRM_MEM_FILES); 789 790 /* ======================================================== 791 * End inline drm_release 792 */ 793 794 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]); 795 mtx_lock(&Giant); 796 device_unbusy(dev->device); 797 mtx_unlock(&Giant); 798 if (--dev->open_count == 0) { 799 retcode = drm_lastclose(dev); 800 } 801 802 DRM_UNLOCK(dev); 803 } 804 805 extern drm_ioctl_desc_t drm_compat_ioctls[]; 806 807 /* drm_ioctl is called whenever a process performs an ioctl on /dev/drm. 808 */ 809 int drm_ioctl(struct cdev *kdev, u_long cmd, caddr_t data, int flags, 810 DRM_STRUCTPROC *p) 811 { 812 struct drm_device *dev = drm_get_device_from_kdev(kdev); 813 int retcode = 0; 814 drm_ioctl_desc_t *ioctl; 815 int (*func)(struct drm_device *dev, void *data, struct drm_file *file_priv); 816 int nr = DRM_IOCTL_NR(cmd); 817 int is_driver_ioctl = 0; 818 struct drm_file *file_priv; 819 820 retcode = devfs_get_cdevpriv((void **)&file_priv); 821 if (retcode != 0) { 822 DRM_ERROR("can't find authenticator\n"); 823 return EINVAL; 824 } 825 826 atomic_inc(&dev->counts[_DRM_STAT_IOCTLS]); 827 ++file_priv->ioctl_count; 828 829 DRM_DEBUG("pid=%d, cmd=0x%02lx, nr=0x%02x, dev 0x%lx, auth=%d\n", 830 DRM_CURRENTPID, cmd, nr, (long)dev->device, 831 file_priv->authenticated); 832 833 switch (cmd) { 834 case FIONBIO: 835 case FIOASYNC: 836 return 0; 837 838 case FIOSETOWN: 839 return fsetown(*(int *)data, &dev->buf_sigio); 840 841 case FIOGETOWN: 842 *(int *) data = fgetown(&dev->buf_sigio); 843 return 0; 844 } 845 846 if (IOCGROUP(cmd) != DRM_IOCTL_BASE) { 847 DRM_DEBUG("Bad ioctl group 0x%x\n", (int)IOCGROUP(cmd)); 848 return EINVAL; 849 } 850 851 #ifdef COMPAT_FREEBSD32 852 /* 853 * Called whenever a 32-bit process running under a 64-bit 854 * kernel performs an ioctl on /dev/drm. 855 */ 856 if (SV_CURPROC_FLAG(SV_ILP32) && drm_compat_ioctls[nr].func != NULL) 857 /* 858 * Assume that ioctls without an explicit compat 859 * routine will just work. This may not always be a 860 * good assumption, but it's better than always 861 * failing. 862 */ 863 ioctl = &drm_compat_ioctls[nr]; 864 else 865 #endif 866 ioctl = &drm_ioctls[nr]; 867 /* It's not a core DRM ioctl, try driver-specific. */ 868 if (ioctl->func == NULL && nr >= DRM_COMMAND_BASE) { 869 /* The array entries begin at DRM_COMMAND_BASE ioctl nr */ 870 nr -= DRM_COMMAND_BASE; 871 if (nr > dev->driver->max_ioctl) { 872 DRM_DEBUG("Bad driver ioctl number, 0x%x (of 0x%x)\n", 873 nr, dev->driver->max_ioctl); 874 return EINVAL; 875 } 876 #ifdef COMPAT_FREEBSD32 877 if (SV_CURPROC_FLAG(SV_ILP32) && 878 nr < *dev->driver->compat_ioctls_nr && 879 dev->driver->compat_ioctls[nr].func != NULL) 880 ioctl = &dev->driver->compat_ioctls[nr]; 881 else 882 #endif 883 ioctl = &dev->driver->ioctls[nr]; 884 is_driver_ioctl = 1; 885 } 886 func = ioctl->func; 887 888 if (func == NULL) { 889 DRM_DEBUG("no function\n"); 890 return EINVAL; 891 } 892 893 if (((ioctl->flags & DRM_ROOT_ONLY) && !DRM_SUSER(p)) || 894 ((ioctl->flags & DRM_AUTH) && !file_priv->authenticated) || 895 ((ioctl->flags & DRM_MASTER) && !file_priv->master)) 896 return EACCES; 897 898 if (is_driver_ioctl) { 899 if ((ioctl->flags & DRM_UNLOCKED) == 0) 900 DRM_LOCK(dev); 901 /* shared code returns -errno */ 902 retcode = -func(dev, data, file_priv); 903 if ((ioctl->flags & DRM_UNLOCKED) == 0) 904 DRM_UNLOCK(dev); 905 } else { 906 retcode = func(dev, data, file_priv); 907 } 908 909 if (retcode != 0) 910 DRM_DEBUG(" returning %d\n", retcode); 911 if (retcode != 0 && 912 (drm_debug_flag & DRM_DEBUGBITS_FAILED_IOCTL) != 0) { 913 printf( 914 "pid %d, cmd 0x%02lx, nr 0x%02x/%1d, dev 0x%lx, auth %d, res %d\n", 915 DRM_CURRENTPID, cmd, nr, is_driver_ioctl, (long)dev->device, 916 file_priv->authenticated, retcode); 917 } 918 919 return retcode; 920 } 921 922 drm_local_map_t *drm_getsarea(struct drm_device *dev) 923 { 924 drm_local_map_t *map; 925 926 DRM_LOCK_ASSERT(dev); 927 TAILQ_FOREACH(map, &dev->maplist, link) { 928 if (map->type == _DRM_SHM && (map->flags & _DRM_CONTAINS_LOCK)) 929 return map; 930 } 931 932 return NULL; 933 } 934 935 int 936 drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx, 937 struct sysctl_oid *top) 938 { 939 struct sysctl_oid *oid; 940 941 snprintf(dev->busid_str, sizeof(dev->busid_str), 942 "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus, 943 dev->pci_slot, dev->pci_func); 944 oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid", 945 CTLFLAG_RD, dev->busid_str, 0, NULL); 946 if (oid == NULL) 947 return (ENOMEM); 948 dev->modesetting = (dev->driver->driver_features & DRIVER_MODESET) != 0; 949 oid = SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(top), OID_AUTO, 950 "modesetting", CTLFLAG_RD, &dev->modesetting, 0, NULL); 951 if (oid == NULL) 952 return (ENOMEM); 953 954 return (0); 955 } 956 957 #if DRM_LINUX 958 959 #include <sys/sysproto.h> 960 961 MODULE_DEPEND(DRIVER_NAME, linux, 1, 1, 1); 962 963 #define LINUX_IOCTL_DRM_MIN 0x6400 964 #define LINUX_IOCTL_DRM_MAX 0x64ff 965 966 static linux_ioctl_function_t drm_linux_ioctl; 967 static struct linux_ioctl_handler drm_handler = {drm_linux_ioctl, 968 LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX}; 969 970 SYSINIT(drm_register, SI_SUB_KLD, SI_ORDER_MIDDLE, 971 linux_ioctl_register_handler, &drm_handler); 972 SYSUNINIT(drm_unregister, SI_SUB_KLD, SI_ORDER_MIDDLE, 973 linux_ioctl_unregister_handler, &drm_handler); 974 975 /* The bits for in/out are switched on Linux */ 976 #define LINUX_IOC_IN IOC_OUT 977 #define LINUX_IOC_OUT IOC_IN 978 979 static int 980 drm_linux_ioctl(DRM_STRUCTPROC *p, struct linux_ioctl_args* args) 981 { 982 int error; 983 int cmd = args->cmd; 984 985 args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT); 986 if (cmd & LINUX_IOC_IN) 987 args->cmd |= IOC_IN; 988 if (cmd & LINUX_IOC_OUT) 989 args->cmd |= IOC_OUT; 990 991 error = ioctl(p, (struct ioctl_args *)args); 992 993 return error; 994 } 995 #endif /* DRM_LINUX */ 996 997 bool 998 dmi_check_system(const struct dmi_system_id *sysid) 999 { 1000 1001 /* XXXKIB */ 1002 return (false); 1003 } 1004 1005