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