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_ctxbitmap_cleanup(dev); 375 376 DRM_LOCK(); 377 (void) drm_lastclose(dev); 378 DRM_UNLOCK(); 379 380 while ((map = TAILQ_FIRST(&dev->maplist)) != NULL) { 381 drm_rmmap(dev, map); 382 } 383 384 if (dev->driver->unload != NULL) 385 dev->driver->unload(dev); 386 387 drm_mem_uninit(); 388 cv_destroy(&dev->lock.lock_cv); 389 mutex_destroy(&dev->lock.lock_mutex); 390 mutex_destroy(&dev->irq_lock); 391 mutex_destroy(&dev->dev_lock); 392 mutex_destroy(&dev->drw_lock); 393 mutex_destroy(&dev->tasklet_lock); 394 } 395 396 397 /*ARGSUSED*/ 398 int 399 drm_open(drm_device_t *dev, drm_cminor_t *mp, int openflags, 400 int otyp, cred_t *credp) 401 { 402 int retcode; 403 404 retcode = drm_open_helper(dev, mp, openflags, otyp, credp); 405 406 if (!retcode) { 407 atomic_inc_32(&dev->counts[_DRM_STAT_OPENS]); 408 DRM_LOCK(); 409 if (!dev->open_count ++) 410 retcode = drm_firstopen(dev); 411 DRM_UNLOCK(); 412 } 413 414 return (retcode); 415 } 416 417 /*ARGSUSED*/ 418 int 419 drm_close(drm_device_t *dev, int minor, int flag, int otyp, 420 cred_t *credp) 421 { 422 drm_cminor_t *mp; 423 drm_file_t *fpriv; 424 int retcode = 0; 425 426 DRM_LOCK(); 427 mp = drm_find_file_by_minor(dev, minor); 428 if (!mp) { 429 DRM_UNLOCK(); 430 DRM_ERROR("drm_close: can't find authenticator"); 431 return (EACCES); 432 } 433 434 fpriv = mp->fpriv; 435 ASSERT(fpriv); 436 437 if (--fpriv->refs != 0) 438 goto done; 439 440 if (dev->driver->preclose != NULL) 441 dev->driver->preclose(dev, fpriv); 442 443 /* 444 * Begin inline drm_release 445 */ 446 DRM_DEBUG("drm_close :pid = %d , open_count = %d", 447 DRM_CURRENTPID, dev->open_count); 448 449 if (dev->lock.hw_lock && 450 _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) && 451 dev->lock.filp == fpriv) { 452 DRM_DEBUG("Process %d dead, freeing lock for context %d", 453 DRM_CURRENTPID, 454 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock)); 455 if (dev->driver->reclaim_buffers_locked != NULL) 456 dev->driver->reclaim_buffers_locked(dev, fpriv); 457 (void) drm_lock_free(dev, &dev->lock.hw_lock->lock, 458 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock)); 459 } else if (dev->driver->reclaim_buffers_locked != NULL && 460 dev->lock.hw_lock != NULL) { 461 DRM_ERROR("drm_close: " 462 "retake lock not implemented yet"); 463 } 464 465 if (dev->driver->use_dma) 466 drm_reclaim_buffers(dev, fpriv); 467 468 469 if (dev->driver->postclose != NULL) 470 dev->driver->postclose(dev, fpriv); 471 TAILQ_REMOVE(&dev->files, fpriv, link); 472 drm_free(fpriv, sizeof (*fpriv), DRM_MEM_FILES); 473 474 done: 475 atomic_inc_32(&dev->counts[_DRM_STAT_CLOSES]); 476 477 TAILQ_REMOVE(&dev->minordevs, mp, link); 478 drm_free(mp, sizeof (*mp), DRM_MEM_FILES); 479 480 if (--dev->open_count == 0) { 481 retcode = drm_lastclose(dev); 482 } 483 DRM_UNLOCK(); 484 485 return (retcode); 486 } 487 488 int 489 drm_attach(drm_device_t *dev) 490 { 491 return (drm_load(dev)); 492 } 493 494 int 495 drm_detach(drm_device_t *dev) 496 { 497 drm_unload(dev); 498 drm_fini_kstats(dev); 499 return (DDI_SUCCESS); 500 } 501 502 static int 503 drm_get_businfo(drm_device_t *dev) 504 { 505 dev->irq = pci_get_irq(dev); 506 if (dev->irq == -1) { 507 DRM_ERROR("drm_get_businfo: get irq error"); 508 return (DDI_FAILURE); 509 } 510 /* XXX Fix domain number (alpha hoses) */ 511 dev->pci_domain = 0; 512 if (pci_get_info(dev, &dev->pci_bus, 513 &dev->pci_slot, &dev->pci_func) != DDI_SUCCESS) { 514 DRM_ERROR("drm_get_businfo: get bus slot func error "); 515 return (DDI_FAILURE); 516 } 517 DRM_DEBUG("drm_get_businfo: pci bus: %d, pci slot :%d pci func %d", 518 dev->pci_bus, dev->pci_slot, dev->pci_func); 519 return (DDI_SUCCESS); 520 } 521 522 int 523 drm_probe(drm_device_t *dev, drm_pci_id_list_t *idlist) 524 { 525 const char *s = NULL; 526 int vendor, device; 527 528 vendor = pci_get_vendor(dev); 529 device = pci_get_device(dev); 530 531 s = drm_find_description(vendor, device, idlist); 532 if (s != NULL) { 533 dev->desc = s; 534 if (drm_get_businfo(dev) != DDI_SUCCESS) { 535 DRM_ERROR("drm_probe: drm get bus info error"); 536 return (DDI_FAILURE); 537 } 538 return (DDI_SUCCESS); 539 } 540 return (DDI_FAILURE); 541 } 542