1 /* 2 * drm_drv.h -- Generic driver template -*- linux-c -*- 3 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com 4 */ 5 /* 6 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas. 7 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 8 * All Rights Reserved. 9 * 10 * Permission is hereby granted, free of charge, to any person obtaining a 11 * copy of this software and associated documentation files (the "Software"), 12 * to deal in the Software without restriction, including without limitation 13 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 * and/or sell copies of the Software, and to permit persons to whom the 15 * Software is furnished to do so, subject to the following conditions: 16 * 17 * The above copyright notice and this permission notice (including the next 18 * paragraph) shall be included in all copies or substantial portions of the 19 * Software. 20 * 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 24 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 * OTHER DEALINGS IN THE SOFTWARE. 28 * 29 * Authors: 30 * Rickard E. (Rik) Faith <faith@valinux.com> 31 * Gareth Hughes <gareth@valinux.com> 32 * 33 */ 34 35 /* 36 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 37 * Use is subject to license terms. 38 */ 39 40 #include "drmP.h" 41 #include "drm.h" 42 #include "drm_sarea.h" 43 44 int drm_debug_flag = 1; 45 46 #define DRIVER_IOCTL_COUNT 256 47 drm_ioctl_desc_t drm_ioctls[DRIVER_IOCTL_COUNT] = { 48 [DRM_IOCTL_NR(DRM_IOCTL_VERSION)] = 49 {drm_version, 0}, 50 [DRM_IOCTL_NR(DRM_IOCTL_GET_UNIQUE)] = 51 {drm_getunique, 0}, 52 [DRM_IOCTL_NR(DRM_IOCTL_GET_MAGIC)] = 53 {drm_getmagic, 0}, 54 [DRM_IOCTL_NR(DRM_IOCTL_IRQ_BUSID)] = 55 {drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY}, 56 [DRM_IOCTL_NR(DRM_IOCTL_GET_MAP)] = 57 {drm_getmap, 0}, 58 [DRM_IOCTL_NR(DRM_IOCTL_GET_CLIENT)] = 59 {drm_getclient, 0}, 60 [DRM_IOCTL_NR(DRM_IOCTL_GET_STATS)] = 61 {drm_getstats, 0}, 62 [DRM_IOCTL_NR(DRM_IOCTL_SET_VERSION)] = 63 {drm_setversion, DRM_MASTER|DRM_ROOT_ONLY}, 64 [DRM_IOCTL_NR(DRM_IOCTL_SET_UNIQUE)] = 65 {drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 66 [DRM_IOCTL_NR(DRM_IOCTL_BLOCK)] = 67 {drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 68 [DRM_IOCTL_NR(DRM_IOCTL_UNBLOCK)] = 69 {drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 70 [DRM_IOCTL_NR(DRM_IOCTL_AUTH_MAGIC)] = 71 {drm_authmagic, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 72 [DRM_IOCTL_NR(DRM_IOCTL_ADD_MAP)] = 73 {drm_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 74 [DRM_IOCTL_NR(DRM_IOCTL_RM_MAP)] = 75 {drm_rmmap_ioctl, DRM_AUTH}, 76 [DRM_IOCTL_NR(DRM_IOCTL_SET_SAREA_CTX)] = 77 {drm_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 78 [DRM_IOCTL_NR(DRM_IOCTL_GET_SAREA_CTX)] = 79 {drm_getsareactx, DRM_AUTH}, 80 [DRM_IOCTL_NR(DRM_IOCTL_ADD_CTX)] = 81 {drm_addctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 82 [DRM_IOCTL_NR(DRM_IOCTL_RM_CTX)] = 83 {drm_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 84 [DRM_IOCTL_NR(DRM_IOCTL_MOD_CTX)] = 85 {drm_modctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 86 [DRM_IOCTL_NR(DRM_IOCTL_GET_CTX)] = 87 {drm_getctx, DRM_AUTH}, 88 [DRM_IOCTL_NR(DRM_IOCTL_SWITCH_CTX)] = 89 {drm_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 90 [DRM_IOCTL_NR(DRM_IOCTL_NEW_CTX)] = 91 {drm_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 92 [DRM_IOCTL_NR(DRM_IOCTL_RES_CTX)] = 93 {drm_resctx, DRM_AUTH}, 94 [DRM_IOCTL_NR(DRM_IOCTL_ADD_DRAW)] = 95 {drm_adddraw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 96 [DRM_IOCTL_NR(DRM_IOCTL_RM_DRAW)] = 97 {drm_rmdraw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 98 [DRM_IOCTL_NR(DRM_IOCTL_LOCK)] = 99 {drm_lock, DRM_AUTH}, 100 [DRM_IOCTL_NR(DRM_IOCTL_UNLOCK)] = 101 {drm_unlock, DRM_AUTH}, 102 [DRM_IOCTL_NR(DRM_IOCTL_FINISH)] = 103 {drm_noop, DRM_AUTH}, 104 [DRM_IOCTL_NR(DRM_IOCTL_ADD_BUFS)] = 105 {drm_addbufs_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 106 [DRM_IOCTL_NR(DRM_IOCTL_MARK_BUFS)] = 107 {drm_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 108 [DRM_IOCTL_NR(DRM_IOCTL_INFO_BUFS)] = 109 {drm_infobufs, DRM_AUTH}, 110 [DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS)] = 111 {drm_mapbufs, DRM_AUTH}, 112 [DRM_IOCTL_NR(DRM_IOCTL_FREE_BUFS)] = 113 {drm_freebufs, DRM_AUTH}, 114 [DRM_IOCTL_NR(DRM_IOCTL_DMA)] = 115 {drm_dma, DRM_AUTH}, 116 [DRM_IOCTL_NR(DRM_IOCTL_CONTROL)] = 117 {drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 118 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ACQUIRE)] = 119 {drm_agp_acquire, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 120 [DRM_IOCTL_NR(DRM_IOCTL_AGP_RELEASE)] = 121 {drm_agp_release, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 122 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ENABLE)] = 123 {drm_agp_enable, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 124 [DRM_IOCTL_NR(DRM_IOCTL_AGP_INFO)] = 125 {drm_agp_info, DRM_AUTH}, 126 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ALLOC)] = 127 {drm_agp_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 128 [DRM_IOCTL_NR(DRM_IOCTL_AGP_FREE)] = 129 {drm_agp_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 130 [DRM_IOCTL_NR(DRM_IOCTL_AGP_BIND)] = 131 {drm_agp_bind, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 132 [DRM_IOCTL_NR(DRM_IOCTL_AGP_UNBIND)] = 133 {drm_agp_unbind, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 134 [DRM_IOCTL_NR(DRM_IOCTL_SG_ALLOC)] = 135 {drm_sg_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 136 [DRM_IOCTL_NR(DRM_IOCTL_SG_FREE)] = 137 {drm_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 138 [DRM_IOCTL_NR(DRM_IOCTL_WAIT_VBLANK)] = 139 {drm_wait_vblank, 0}, 140 [DRM_IOCTL_NR(DRM_IOCTL_UPDATE_DRAW)] = 141 {drm_update_draw, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY}, 142 }; 143 144 const char * 145 drm_find_description(int vendor, int device, drm_pci_id_list_t *idlist) 146 { 147 int i = 0; 148 for (i = 0; idlist[i].vendor != 0; i++) { 149 if ((idlist[i].vendor == vendor) && 150 (idlist[i].device == device)) { 151 return (idlist[i].name); 152 } 153 } 154 return ((char *)NULL); 155 } 156 157 static int 158 drm_firstopen(drm_device_t *dev) 159 { 160 int i; 161 int retval; 162 drm_local_map_t *map; 163 164 /* prebuild the SAREA */ 165 retval = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM, 166 _DRM_CONTAINS_LOCK, &map); 167 if (retval != 0) { 168 DRM_ERROR("firstopen: failed to prebuild SAREA"); 169 return (retval); 170 } 171 172 if (dev->driver->use_agp) { 173 DRM_DEBUG("drm_firstopen: use_agp=%d", dev->driver->use_agp); 174 if (drm_device_is_agp(dev)) 175 dev->agp = drm_agp_init(dev); 176 if (dev->driver->require_agp && dev->agp == NULL) { 177 DRM_ERROR("couldn't initialize AGP"); 178 return (EIO); 179 } 180 } 181 182 if (dev->driver->firstopen) 183 retval = dev->driver->firstopen(dev); 184 185 if (retval != 0) { 186 DRM_ERROR("drm_firstopen: driver-specific firstopen failed"); 187 return (retval); 188 } 189 190 dev->buf_use = 0; 191 192 if (dev->driver->use_dma) { 193 i = drm_dma_setup(dev); 194 if (i != 0) 195 return (i); 196 } 197 dev->counters = 6; 198 dev->types[0] = _DRM_STAT_LOCK; 199 dev->types[1] = _DRM_STAT_OPENS; 200 dev->types[2] = _DRM_STAT_CLOSES; 201 dev->types[3] = _DRM_STAT_IOCTLS; 202 dev->types[4] = _DRM_STAT_LOCKS; 203 dev->types[5] = _DRM_STAT_UNLOCKS; 204 205 for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++) 206 *(&dev->counts[i]) = 0; 207 208 for (i = 0; i < DRM_HASH_SIZE; i++) { 209 dev->magiclist[i].head = NULL; 210 dev->magiclist[i].tail = NULL; 211 } 212 213 dev->irq_enabled = 0; 214 dev->context_flag = 0; 215 dev->last_context = 0; 216 dev->if_version = 0; 217 218 return (0); 219 } 220 221 /* Free resources associated with the DRM on the last close. */ 222 static int 223 drm_lastclose(drm_device_t *dev) 224 { 225 drm_magic_entry_t *pt, *next; 226 drm_local_map_t *map, *mapsave; 227 int i; 228 229 DRM_SPINLOCK_ASSERT(&dev->dev_lock); 230 231 if (dev->driver->lastclose != NULL) 232 dev->driver->lastclose(dev); 233 234 if (dev->irq_enabled) 235 (void) drm_irq_uninstall(dev); 236 237 if (dev->unique) { 238 drm_free(dev->unique, dev->unique_len + 1, DRM_MEM_DRIVER); 239 dev->unique = NULL; 240 dev->unique_len = 0; 241 } 242 243 /* Clear pid list */ 244 for (i = 0; i < DRM_HASH_SIZE; i++) { 245 for (pt = dev->magiclist[i].head; pt; pt = next) { 246 next = pt->next; 247 drm_free(pt, sizeof (*pt), DRM_MEM_MAGIC); 248 } 249 dev->magiclist[i].head = dev->magiclist[i].tail = NULL; 250 } 251 252 /* Clear AGP information */ 253 if (dev->agp) { 254 drm_agp_mem_t *entry; 255 drm_agp_mem_t *nexte; 256 257 /* 258 * Remove AGP resources, but leave dev->agp 259 * intact until drm_cleanup is called. 260 */ 261 for (entry = dev->agp->memory; entry; entry = nexte) { 262 nexte = entry->next; 263 if (entry->bound) 264 (void) drm_agp_unbind_memory( 265 (unsigned long)entry->handle, dev); 266 (void) drm_agp_free_memory(entry->handle); 267 drm_free(entry, sizeof (*entry), DRM_MEM_AGPLISTS); 268 } 269 dev->agp->memory = NULL; 270 271 if (dev->agp->acquired) 272 (void) drm_agp_do_release(dev); 273 274 dev->agp->acquired = 0; 275 dev->agp->enabled = 0; 276 drm_agp_fini(dev); 277 } 278 279 if (dev->sg != NULL) { 280 drm_sg_mem_t *entry; 281 entry = dev->sg; 282 dev->sg = NULL; 283 drm_sg_cleanup(dev, entry); 284 } 285 286 287 /* Clean up maps that weren't set up by the driver. */ 288 TAILQ_FOREACH_SAFE(map, &dev->maplist, link, mapsave) { 289 if (!map->kernel_owned) 290 drm_rmmap(dev, map); 291 } 292 293 drm_dma_takedown(dev); 294 if (dev->lock.hw_lock) { 295 dev->lock.hw_lock = NULL; /* SHM removed */ 296 dev->lock.filp = NULL; 297 298 mutex_enter(&(dev->lock.lock_mutex)); 299 cv_broadcast(&(dev->lock.lock_cv)); 300 mutex_exit(&(dev->lock.lock_mutex)); 301 } 302 303 return (0); 304 } 305 306 static int 307 drm_load(drm_device_t *dev) 308 { 309 int retcode; 310 311 cv_init(&(dev->lock.lock_cv), NULL, CV_DRIVER, NULL); 312 mutex_init(&(dev->lock.lock_mutex), NULL, MUTEX_DRIVER, NULL); 313 mutex_init(&(dev->dev_lock), "drmdev", MUTEX_DRIVER, NULL); 314 mutex_init(&dev->irq_lock, "drmirq", MUTEX_DRIVER, 315 (void *)dev->intr_block); 316 mutex_init(&dev->drw_lock, "drmdrw", MUTEX_DRIVER, NULL); 317 mutex_init(&dev->tasklet_lock, "drmtsk", MUTEX_DRIVER, NULL); 318 319 dev->irq = pci_get_irq(dev); 320 dev->pci_vendor = pci_get_vendor(dev); 321 dev->pci_device = pci_get_device(dev); 322 323 TAILQ_INIT(&dev->maplist); 324 TAILQ_INIT(&dev->minordevs); 325 TAILQ_INIT(&dev->files); 326 if (dev->driver->load != NULL) { 327 retcode = dev->driver->load(dev, 0); 328 if (retcode != 0) { 329 DRM_ERROR("drm_load: failed\n"); 330 goto error; 331 } 332 } 333 334 retcode = drm_ctxbitmap_init(dev); 335 if (retcode != 0) { 336 DRM_ERROR("drm_load: Cannot allocate memory for ctx bitmap"); 337 goto error; 338 } 339 340 if (drm_init_kstats(dev)) { 341 DRM_ERROR("drm_attach => drm_load: init kstats error"); 342 retcode = EFAULT; 343 goto error; 344 } 345 346 DRM_INFO("!drm: Initialized %s %d.%d.%d %s ", 347 dev->driver->driver_name, 348 dev->driver->driver_major, 349 dev->driver->driver_minor, 350 dev->driver->driver_patchlevel, 351 dev->driver->driver_date); 352 return (0); 353 354 error: 355 DRM_LOCK(); 356 (void) drm_lastclose(dev); 357 DRM_UNLOCK(); 358 cv_destroy(&(dev->lock.lock_cv)); 359 mutex_destroy(&(dev->lock.lock_mutex)); 360 mutex_destroy(&dev->irq_lock); 361 mutex_destroy(&(dev->dev_lock)); 362 mutex_destroy(&dev->drw_lock); 363 mutex_destroy(&dev->tasklet_lock); 364 365 return (retcode); 366 } 367 368 /* called when cleanup this module */ 369 static void 370 drm_unload(drm_device_t *dev) 371 { 372 drm_local_map_t *map; 373 374 drm_vblank_cleanup(dev); 375 376 drm_ctxbitmap_cleanup(dev); 377 378 DRM_LOCK(); 379 (void) drm_lastclose(dev); 380 DRM_UNLOCK(); 381 382 while ((map = TAILQ_FIRST(&dev->maplist)) != NULL) { 383 drm_rmmap(dev, map); 384 } 385 386 if (dev->driver->unload != NULL) 387 dev->driver->unload(dev); 388 389 drm_mem_uninit(); 390 cv_destroy(&dev->lock.lock_cv); 391 mutex_destroy(&dev->lock.lock_mutex); 392 mutex_destroy(&dev->irq_lock); 393 mutex_destroy(&dev->dev_lock); 394 mutex_destroy(&dev->drw_lock); 395 mutex_destroy(&dev->tasklet_lock); 396 } 397 398 399 /*ARGSUSED*/ 400 int 401 drm_open(drm_device_t *dev, drm_cminor_t *mp, int openflags, 402 int otyp, cred_t *credp) 403 { 404 int retcode; 405 406 retcode = drm_open_helper(dev, mp, openflags, otyp, credp); 407 408 if (!retcode) { 409 atomic_inc_32(&dev->counts[_DRM_STAT_OPENS]); 410 DRM_LOCK(); 411 if (!dev->open_count ++) 412 retcode = drm_firstopen(dev); 413 DRM_UNLOCK(); 414 } 415 416 return (retcode); 417 } 418 419 /*ARGSUSED*/ 420 int 421 drm_close(drm_device_t *dev, int minor, int flag, int otyp, 422 cred_t *credp) 423 { 424 drm_cminor_t *mp; 425 drm_file_t *fpriv; 426 int retcode = 0; 427 428 DRM_LOCK(); 429 mp = drm_find_file_by_minor(dev, minor); 430 if (!mp) { 431 DRM_UNLOCK(); 432 DRM_ERROR("drm_close: can't find authenticator"); 433 return (EACCES); 434 } 435 436 fpriv = mp->fpriv; 437 ASSERT(fpriv); 438 439 if (--fpriv->refs != 0) 440 goto done; 441 442 if (dev->driver->preclose != NULL) 443 dev->driver->preclose(dev, fpriv); 444 445 /* 446 * Begin inline drm_release 447 */ 448 DRM_DEBUG("drm_close :pid = %d , open_count = %d", 449 DRM_CURRENTPID, dev->open_count); 450 451 if (dev->lock.hw_lock && 452 _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) && 453 dev->lock.filp == fpriv) { 454 DRM_DEBUG("Process %d dead, freeing lock for context %d", 455 DRM_CURRENTPID, 456 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock)); 457 if (dev->driver->reclaim_buffers_locked != NULL) 458 dev->driver->reclaim_buffers_locked(dev, fpriv); 459 (void) drm_lock_free(dev, &dev->lock.hw_lock->lock, 460 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock)); 461 } else if (dev->driver->reclaim_buffers_locked != NULL && 462 dev->lock.hw_lock != NULL) { 463 DRM_ERROR("drm_close: " 464 "retake lock not implemented yet"); 465 } 466 467 if (dev->driver->use_dma) 468 drm_reclaim_buffers(dev, fpriv); 469 470 471 if (dev->driver->postclose != NULL) 472 dev->driver->postclose(dev, fpriv); 473 TAILQ_REMOVE(&dev->files, fpriv, link); 474 drm_free(fpriv, sizeof (*fpriv), DRM_MEM_FILES); 475 476 done: 477 atomic_inc_32(&dev->counts[_DRM_STAT_CLOSES]); 478 479 TAILQ_REMOVE(&dev->minordevs, mp, link); 480 drm_free(mp, sizeof (*mp), DRM_MEM_FILES); 481 482 if (--dev->open_count == 0) { 483 retcode = drm_lastclose(dev); 484 } 485 DRM_UNLOCK(); 486 487 return (retcode); 488 } 489 490 int 491 drm_attach(drm_device_t *dev) 492 { 493 return (drm_load(dev)); 494 } 495 496 int 497 drm_detach(drm_device_t *dev) 498 { 499 drm_unload(dev); 500 drm_fini_kstats(dev); 501 return (DDI_SUCCESS); 502 } 503 504 static int 505 drm_get_businfo(drm_device_t *dev) 506 { 507 dev->irq = pci_get_irq(dev); 508 if (dev->irq == -1) { 509 DRM_ERROR("drm_get_businfo: get irq error"); 510 return (DDI_FAILURE); 511 } 512 /* XXX Fix domain number (alpha hoses) */ 513 dev->pci_domain = 0; 514 if (pci_get_info(dev, &dev->pci_bus, 515 &dev->pci_slot, &dev->pci_func) != DDI_SUCCESS) { 516 DRM_ERROR("drm_get_businfo: get bus slot func error "); 517 return (DDI_FAILURE); 518 } 519 DRM_DEBUG("drm_get_businfo: pci bus: %d, pci slot :%d pci func %d", 520 dev->pci_bus, dev->pci_slot, dev->pci_func); 521 return (DDI_SUCCESS); 522 } 523 524 int 525 drm_probe(drm_device_t *dev, drm_pci_id_list_t *idlist) 526 { 527 const char *s = NULL; 528 int vendor, device; 529 530 vendor = pci_get_vendor(dev); 531 device = pci_get_device(dev); 532 533 s = drm_find_description(vendor, device, idlist); 534 if (s != NULL) { 535 dev->desc = s; 536 if (drm_get_businfo(dev) != DDI_SUCCESS) { 537 DRM_ERROR("drm_probe: drm get bus info error"); 538 return (DDI_FAILURE); 539 } 540 return (DDI_SUCCESS); 541 } 542 return (DDI_FAILURE); 543 } 544