1/* 2 * vgaarb.c: Implements the VGA arbitration. For details refer to 3 * Documentation/vgaarbiter.txt 4 * 5 * 6 * (C) Copyright 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org> 7 * (C) Copyright 2007 Paulo R. Zanoni <przanoni@gmail.com> 8 * (C) Copyright 2007, 2009 Tiago Vignatti <vignatti@freedesktop.org> 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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 * DEALINGS 28 * IN THE SOFTWARE. 29 * 30 */ 31 32#include <linux/module.h> 33#include <linux/kernel.h> 34#include <linux/pci.h> 35#include <linux/errno.h> 36#include <linux/init.h> 37#include <linux/list.h> 38#include <linux/sched.h> 39#include <linux/wait.h> 40#include <linux/spinlock.h> 41#include <linux/poll.h> 42#include <linux/miscdevice.h> 43#include <linux/slab.h> 44 45#include <linux/uaccess.h> 46 47#include <linux/vgaarb.h> 48 49static void vga_arbiter_notify_clients(void); 50/* 51 * We keep a list of all vga devices in the system to speed 52 * up the various operations of the arbiter 53 */ 54struct vga_device { 55 struct list_head list; 56 struct pci_dev *pdev; 57 unsigned int decodes; /* what does it decodes */ 58 unsigned int owns; /* what does it owns */ 59 unsigned int locks; /* what does it locks */ 60 unsigned int io_lock_cnt; /* legacy IO lock count */ 61 unsigned int mem_lock_cnt; /* legacy MEM lock count */ 62 unsigned int io_norm_cnt; /* normal IO count */ 63 unsigned int mem_norm_cnt; /* normal MEM count */ 64 65 /* allow IRQ enable/disable hook */ 66 void *cookie; 67 void (*irq_set_state)(void *cookie, bool enable); 68 unsigned int (*set_vga_decode)(void *cookie, bool decode); 69}; 70 71static LIST_HEAD(vga_list); 72static int vga_count, vga_decode_count; 73static bool vga_arbiter_used; 74static DEFINE_SPINLOCK(vga_lock); 75static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue); 76 77 78static const char *vga_iostate_to_str(unsigned int iostate) 79{ 80 /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */ 81 iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; 82 switch (iostate) { 83 case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM: 84 return "io+mem"; 85 case VGA_RSRC_LEGACY_IO: 86 return "io"; 87 case VGA_RSRC_LEGACY_MEM: 88 return "mem"; 89 } 90 return "none"; 91} 92 93static int vga_str_to_iostate(char *buf, int str_size, int *io_state) 94{ 95 /* we could in theory hand out locks on IO and mem 96 * separately to userspace but it can cause deadlocks */ 97 if (strncmp(buf, "none", 4) == 0) { 98 *io_state = VGA_RSRC_NONE; 99 return 1; 100 } 101 102 if (strncmp(buf, "io+mem", 6) == 0) 103 goto both; 104 else if (strncmp(buf, "io", 2) == 0) 105 goto both; 106 else if (strncmp(buf, "mem", 3) == 0) 107 goto both; 108 return 0; 109both: 110 *io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; 111 return 1; 112} 113 114#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE 115/* this is only used a cookie - it should not be dereferenced */ 116static struct pci_dev *vga_default; 117#endif 118 119static void vga_arb_device_card_gone(struct pci_dev *pdev); 120 121/* Find somebody in our list */ 122static struct vga_device *vgadev_find(struct pci_dev *pdev) 123{ 124 struct vga_device *vgadev; 125 126 list_for_each_entry(vgadev, &vga_list, list) 127 if (pdev == vgadev->pdev) 128 return vgadev; 129 return NULL; 130} 131 132/* Returns the default VGA device (vgacon's babe) */ 133#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE 134struct pci_dev *vga_default_device(void) 135{ 136 return vga_default; 137} 138#endif 139 140static inline void vga_irq_set_state(struct vga_device *vgadev, bool state) 141{ 142 if (vgadev->irq_set_state) 143 vgadev->irq_set_state(vgadev->cookie, state); 144} 145 146 147/* If we don't ever use VGA arb we should avoid 148 turning off anything anywhere due to old X servers getting 149 confused about the boot device not being VGA */ 150static void vga_check_first_use(void) 151{ 152 /* we should inform all GPUs in the system that 153 * VGA arb has occured and to try and disable resources 154 * if they can */ 155 if (!vga_arbiter_used) { 156 vga_arbiter_used = true; 157 vga_arbiter_notify_clients(); 158 } 159} 160 161static struct vga_device *__vga_tryget(struct vga_device *vgadev, 162 unsigned int rsrc) 163{ 164 unsigned int wants, legacy_wants, match; 165 struct vga_device *conflict; 166 unsigned int pci_bits; 167 /* Account for "normal" resources to lock. If we decode the legacy, 168 * counterpart, we need to request it as well 169 */ 170 if ((rsrc & VGA_RSRC_NORMAL_IO) && 171 (vgadev->decodes & VGA_RSRC_LEGACY_IO)) 172 rsrc |= VGA_RSRC_LEGACY_IO; 173 if ((rsrc & VGA_RSRC_NORMAL_MEM) && 174 (vgadev->decodes & VGA_RSRC_LEGACY_MEM)) 175 rsrc |= VGA_RSRC_LEGACY_MEM; 176 177 pr_debug("%s: %d\n", __func__, rsrc); 178 pr_debug("%s: owns: %d\n", __func__, vgadev->owns); 179 180 /* Check what resources we need to acquire */ 181 wants = rsrc & ~vgadev->owns; 182 183 /* We already own everything, just mark locked & bye bye */ 184 if (wants == 0) 185 goto lock_them; 186 187 /* We don't need to request a legacy resource, we just enable 188 * appropriate decoding and go 189 */ 190 legacy_wants = wants & VGA_RSRC_LEGACY_MASK; 191 if (legacy_wants == 0) 192 goto enable_them; 193 194 /* Ok, we don't, let's find out how we need to kick off */ 195 list_for_each_entry(conflict, &vga_list, list) { 196 unsigned int lwants = legacy_wants; 197 unsigned int change_bridge = 0; 198 199 /* Don't conflict with myself */ 200 if (vgadev == conflict) 201 continue; 202 203 /* Check if the architecture allows a conflict between those 204 * 2 devices or if they are on separate domains 205 */ 206 if (!vga_conflicts(vgadev->pdev, conflict->pdev)) 207 continue; 208 209 /* We have a possible conflict. before we go further, we must 210 * check if we sit on the same bus as the conflicting device. 211 * if we don't, then we must tie both IO and MEM resources 212 * together since there is only a single bit controlling 213 * VGA forwarding on P2P bridges 214 */ 215 if (vgadev->pdev->bus != conflict->pdev->bus) { 216 change_bridge = 1; 217 lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; 218 } 219 220 /* Check if the guy has a lock on the resource. If he does, 221 * return the conflicting entry 222 */ 223 if (conflict->locks & lwants) 224 return conflict; 225 226 /* Ok, now check if he owns the resource we want. We don't need 227 * to check "decodes" since it should be impossible to own 228 * own legacy resources you don't decode unless I have a bug 229 * in this code... 230 */ 231 WARN_ON(conflict->owns & ~conflict->decodes); 232 match = lwants & conflict->owns; 233 if (!match) 234 continue; 235 236 /* looks like he doesn't have a lock, we can steal 237 * them from him 238 */ 239 vga_irq_set_state(conflict, false); 240 241 pci_bits = 0; 242 if (lwants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM)) 243 pci_bits |= PCI_COMMAND_MEMORY; 244 if (lwants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO)) 245 pci_bits |= PCI_COMMAND_IO; 246 247 pci_set_vga_state(conflict->pdev, false, pci_bits, 248 change_bridge); 249 conflict->owns &= ~lwants; 250 /* If he also owned non-legacy, that is no longer the case */ 251 if (lwants & VGA_RSRC_LEGACY_MEM) 252 conflict->owns &= ~VGA_RSRC_NORMAL_MEM; 253 if (lwants & VGA_RSRC_LEGACY_IO) 254 conflict->owns &= ~VGA_RSRC_NORMAL_IO; 255 } 256 257enable_them: 258 /* ok dude, we got it, everybody conflicting has been disabled, let's 259 * enable us. Make sure we don't mark a bit in "owns" that we don't 260 * also have in "decodes". We can lock resources we don't decode but 261 * not own them. 262 */ 263 pci_bits = 0; 264 if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM)) 265 pci_bits |= PCI_COMMAND_MEMORY; 266 if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO)) 267 pci_bits |= PCI_COMMAND_IO; 268 pci_set_vga_state(vgadev->pdev, true, pci_bits, !!(wants & VGA_RSRC_LEGACY_MASK)); 269 270 vga_irq_set_state(vgadev, true); 271 vgadev->owns |= (wants & vgadev->decodes); 272lock_them: 273 vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK); 274 if (rsrc & VGA_RSRC_LEGACY_IO) 275 vgadev->io_lock_cnt++; 276 if (rsrc & VGA_RSRC_LEGACY_MEM) 277 vgadev->mem_lock_cnt++; 278 if (rsrc & VGA_RSRC_NORMAL_IO) 279 vgadev->io_norm_cnt++; 280 if (rsrc & VGA_RSRC_NORMAL_MEM) 281 vgadev->mem_norm_cnt++; 282 283 return NULL; 284} 285 286static void __vga_put(struct vga_device *vgadev, unsigned int rsrc) 287{ 288 unsigned int old_locks = vgadev->locks; 289 290 pr_debug("%s\n", __func__); 291 292 /* Update our counters, and account for equivalent legacy resources 293 * if we decode them 294 */ 295 if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) { 296 vgadev->io_norm_cnt--; 297 if (vgadev->decodes & VGA_RSRC_LEGACY_IO) 298 rsrc |= VGA_RSRC_LEGACY_IO; 299 } 300 if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) { 301 vgadev->mem_norm_cnt--; 302 if (vgadev->decodes & VGA_RSRC_LEGACY_MEM) 303 rsrc |= VGA_RSRC_LEGACY_MEM; 304 } 305 if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0) 306 vgadev->io_lock_cnt--; 307 if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0) 308 vgadev->mem_lock_cnt--; 309 310 /* Just clear lock bits, we do lazy operations so we don't really 311 * have to bother about anything else at this point 312 */ 313 if (vgadev->io_lock_cnt == 0) 314 vgadev->locks &= ~VGA_RSRC_LEGACY_IO; 315 if (vgadev->mem_lock_cnt == 0) 316 vgadev->locks &= ~VGA_RSRC_LEGACY_MEM; 317 318 /* Kick the wait queue in case somebody was waiting if we actually 319 * released something 320 */ 321 if (old_locks != vgadev->locks) 322 wake_up_all(&vga_wait_queue); 323} 324 325int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible) 326{ 327 struct vga_device *vgadev, *conflict; 328 unsigned long flags; 329 wait_queue_t wait; 330 int rc = 0; 331 332 vga_check_first_use(); 333 /* The one who calls us should check for this, but lets be sure... */ 334 if (pdev == NULL) 335 pdev = vga_default_device(); 336 if (pdev == NULL) 337 return 0; 338 339 for (;;) { 340 spin_lock_irqsave(&vga_lock, flags); 341 vgadev = vgadev_find(pdev); 342 if (vgadev == NULL) { 343 spin_unlock_irqrestore(&vga_lock, flags); 344 rc = -ENODEV; 345 break; 346 } 347 conflict = __vga_tryget(vgadev, rsrc); 348 spin_unlock_irqrestore(&vga_lock, flags); 349 if (conflict == NULL) 350 break; 351 352 353 /* We have a conflict, we wait until somebody kicks the 354 * work queue. Currently we have one work queue that we 355 * kick each time some resources are released, but it would 356 * be fairly easy to have a per device one so that we only 357 * need to attach to the conflicting device 358 */ 359 init_waitqueue_entry(&wait, current); 360 add_wait_queue(&vga_wait_queue, &wait); 361 set_current_state(interruptible ? 362 TASK_INTERRUPTIBLE : 363 TASK_UNINTERRUPTIBLE); 364 if (signal_pending(current)) { 365 rc = -EINTR; 366 break; 367 } 368 schedule(); 369 remove_wait_queue(&vga_wait_queue, &wait); 370 set_current_state(TASK_RUNNING); 371 } 372 return rc; 373} 374EXPORT_SYMBOL(vga_get); 375 376int vga_tryget(struct pci_dev *pdev, unsigned int rsrc) 377{ 378 struct vga_device *vgadev; 379 unsigned long flags; 380 int rc = 0; 381 382 vga_check_first_use(); 383 384 /* The one who calls us should check for this, but lets be sure... */ 385 if (pdev == NULL) 386 pdev = vga_default_device(); 387 if (pdev == NULL) 388 return 0; 389 spin_lock_irqsave(&vga_lock, flags); 390 vgadev = vgadev_find(pdev); 391 if (vgadev == NULL) { 392 rc = -ENODEV; 393 goto bail; 394 } 395 if (__vga_tryget(vgadev, rsrc)) 396 rc = -EBUSY; 397bail: 398 spin_unlock_irqrestore(&vga_lock, flags); 399 return rc; 400} 401EXPORT_SYMBOL(vga_tryget); 402 403void vga_put(struct pci_dev *pdev, unsigned int rsrc) 404{ 405 struct vga_device *vgadev; 406 unsigned long flags; 407 408 /* The one who calls us should check for this, but lets be sure... */ 409 if (pdev == NULL) 410 pdev = vga_default_device(); 411 if (pdev == NULL) 412 return; 413 spin_lock_irqsave(&vga_lock, flags); 414 vgadev = vgadev_find(pdev); 415 if (vgadev == NULL) 416 goto bail; 417 __vga_put(vgadev, rsrc); 418bail: 419 spin_unlock_irqrestore(&vga_lock, flags); 420} 421EXPORT_SYMBOL(vga_put); 422 423/* 424 * Currently, we assume that the "initial" setup of the system is 425 * not sane, that is we come up with conflicting devices and let 426 * the arbiter's client decides if devices decodes or not legacy 427 * things. 428 */ 429static bool vga_arbiter_add_pci_device(struct pci_dev *pdev) 430{ 431 struct vga_device *vgadev; 432 unsigned long flags; 433 struct pci_bus *bus; 434 struct pci_dev *bridge; 435 u16 cmd; 436 437 /* Only deal with VGA class devices */ 438 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA) 439 return false; 440 441 /* Allocate structure */ 442 vgadev = kmalloc(sizeof(struct vga_device), GFP_KERNEL); 443 if (vgadev == NULL) { 444 pr_err("vgaarb: failed to allocate pci device\n"); 445 /* What to do on allocation failure ? For now, let's 446 * just do nothing, I'm not sure there is anything saner 447 * to be done 448 */ 449 return false; 450 } 451 452 memset(vgadev, 0, sizeof(*vgadev)); 453 454 /* Take lock & check for duplicates */ 455 spin_lock_irqsave(&vga_lock, flags); 456 if (vgadev_find(pdev) != NULL) { 457 BUG_ON(1); 458 goto fail; 459 } 460 vgadev->pdev = pdev; 461 462 /* By default, assume we decode everything */ 463 vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | 464 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 465 466 /* by default mark it as decoding */ 467 vga_decode_count++; 468 /* Mark that we "own" resources based on our enables, we will 469 * clear that below if the bridge isn't forwarding 470 */ 471 pci_read_config_word(pdev, PCI_COMMAND, &cmd); 472 if (cmd & PCI_COMMAND_IO) 473 vgadev->owns |= VGA_RSRC_LEGACY_IO; 474 if (cmd & PCI_COMMAND_MEMORY) 475 vgadev->owns |= VGA_RSRC_LEGACY_MEM; 476 477 /* Check if VGA cycles can get down to us */ 478 bus = pdev->bus; 479 while (bus) { 480 bridge = bus->self; 481 if (bridge) { 482 u16 l; 483 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, 484 &l); 485 if (!(l & PCI_BRIDGE_CTL_VGA)) { 486 vgadev->owns = 0; 487 break; 488 } 489 } 490 bus = bus->parent; 491 } 492 493 /* Deal with VGA default device. Use first enabled one 494 * by default if arch doesn't have it's own hook 495 */ 496#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE 497 if (vga_default == NULL && 498 ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)) 499 vga_default = pci_dev_get(pdev); 500#endif 501 502 /* Add to the list */ 503 list_add(&vgadev->list, &vga_list); 504 vga_count++; 505 pr_info("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n", 506 pci_name(pdev), 507 vga_iostate_to_str(vgadev->decodes), 508 vga_iostate_to_str(vgadev->owns), 509 vga_iostate_to_str(vgadev->locks)); 510 511 spin_unlock_irqrestore(&vga_lock, flags); 512 return true; 513fail: 514 spin_unlock_irqrestore(&vga_lock, flags); 515 kfree(vgadev); 516 return false; 517} 518 519static bool vga_arbiter_del_pci_device(struct pci_dev *pdev) 520{ 521 struct vga_device *vgadev; 522 unsigned long flags; 523 bool ret = true; 524 525 spin_lock_irqsave(&vga_lock, flags); 526 vgadev = vgadev_find(pdev); 527 if (vgadev == NULL) { 528 ret = false; 529 goto bail; 530 } 531 532 if (vga_default == pdev) { 533 pci_dev_put(vga_default); 534 vga_default = NULL; 535 } 536 537 if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM)) 538 vga_decode_count--; 539 540 /* Remove entry from list */ 541 list_del(&vgadev->list); 542 vga_count--; 543 /* Notify userland driver that the device is gone so it discards 544 * it's copies of the pci_dev pointer 545 */ 546 vga_arb_device_card_gone(pdev); 547 548 /* Wake up all possible waiters */ 549 wake_up_all(&vga_wait_queue); 550bail: 551 spin_unlock_irqrestore(&vga_lock, flags); 552 kfree(vgadev); 553 return ret; 554} 555 556/* this is called with the lock */ 557static inline void vga_update_device_decodes(struct vga_device *vgadev, 558 int new_decodes) 559{ 560 int old_decodes; 561 struct vga_device *new_vgadev, *conflict; 562 563 old_decodes = vgadev->decodes; 564 vgadev->decodes = new_decodes; 565 566 pr_info("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n", 567 pci_name(vgadev->pdev), 568 vga_iostate_to_str(old_decodes), 569 vga_iostate_to_str(vgadev->decodes), 570 vga_iostate_to_str(vgadev->owns)); 571 572 573 /* if we own the decodes we should move them along to 574 another card */ 575 if ((vgadev->owns & old_decodes) && (vga_count > 1)) { 576 /* set us to own nothing */ 577 vgadev->owns &= ~old_decodes; 578 list_for_each_entry(new_vgadev, &vga_list, list) { 579 if ((new_vgadev != vgadev) && 580 (new_vgadev->decodes & VGA_RSRC_LEGACY_MASK)) { 581 pr_info("vgaarb: transferring owner from PCI:%s to PCI:%s\n", pci_name(vgadev->pdev), pci_name(new_vgadev->pdev)); 582 conflict = __vga_tryget(new_vgadev, VGA_RSRC_LEGACY_MASK); 583 if (!conflict) 584 __vga_put(new_vgadev, VGA_RSRC_LEGACY_MASK); 585 break; 586 } 587 } 588 } 589 590 /* change decodes counter */ 591 if (old_decodes != new_decodes) { 592 if (new_decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM)) 593 vga_decode_count++; 594 else 595 vga_decode_count--; 596 } 597 pr_debug("vgaarb: decoding count now is: %d\n", vga_decode_count); 598} 599 600static void __vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes, bool userspace) 601{ 602 struct vga_device *vgadev; 603 unsigned long flags; 604 605 decodes &= VGA_RSRC_LEGACY_MASK; 606 607 spin_lock_irqsave(&vga_lock, flags); 608 vgadev = vgadev_find(pdev); 609 if (vgadev == NULL) 610 goto bail; 611 612 /* don't let userspace futz with kernel driver decodes */ 613 if (userspace && vgadev->set_vga_decode) 614 goto bail; 615 616 /* update the device decodes + counter */ 617 vga_update_device_decodes(vgadev, decodes); 618 619bail: 620 spin_unlock_irqrestore(&vga_lock, flags); 621} 622 623void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes) 624{ 625 __vga_set_legacy_decoding(pdev, decodes, false); 626} 627EXPORT_SYMBOL(vga_set_legacy_decoding); 628 629/* call with NULL to unregister */ 630int vga_client_register(struct pci_dev *pdev, void *cookie, 631 void (*irq_set_state)(void *cookie, bool state), 632 unsigned int (*set_vga_decode)(void *cookie, bool decode)) 633{ 634 int ret = -ENODEV; 635 struct vga_device *vgadev; 636 unsigned long flags; 637 638 spin_lock_irqsave(&vga_lock, flags); 639 vgadev = vgadev_find(pdev); 640 if (!vgadev) 641 goto bail; 642 643 vgadev->irq_set_state = irq_set_state; 644 vgadev->set_vga_decode = set_vga_decode; 645 vgadev->cookie = cookie; 646 ret = 0; 647 648bail: 649 spin_unlock_irqrestore(&vga_lock, flags); 650 return ret; 651 652} 653EXPORT_SYMBOL(vga_client_register); 654 655/* 656 * Char driver implementation 657 * 658 * Semantics is: 659 * 660 * open : open user instance of the arbitrer. by default, it's 661 * attached to the default VGA device of the system. 662 * 663 * close : close user instance, release locks 664 * 665 * read : return a string indicating the status of the target. 666 * an IO state string is of the form {io,mem,io+mem,none}, 667 * mc and ic are respectively mem and io lock counts (for 668 * debugging/diagnostic only). "decodes" indicate what the 669 * card currently decodes, "owns" indicates what is currently 670 * enabled on it, and "locks" indicates what is locked by this 671 * card. If the card is unplugged, we get "invalid" then for 672 * card_ID and an -ENODEV error is returned for any command 673 * until a new card is targeted 674 * 675 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)" 676 * 677 * write : write a command to the arbiter. List of commands is: 678 * 679 * target <card_ID> : switch target to card <card_ID> (see below) 680 * lock <io_state> : acquires locks on target ("none" is invalid io_state) 681 * trylock <io_state> : non-blocking acquire locks on target 682 * unlock <io_state> : release locks on target 683 * unlock all : release all locks on target held by this user 684 * decodes <io_state> : set the legacy decoding attributes for the card 685 * 686 * poll : event if something change on any card (not just the target) 687 * 688 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default" 689 * to go back to the system default card (TODO: not implemented yet). 690 * Currently, only PCI is supported as a prefix, but the userland API may 691 * support other bus types in the future, even if the current kernel 692 * implementation doesn't. 693 * 694 * Note about locks: 695 * 696 * The driver keeps track of which user has what locks on which card. It 697 * supports stacking, like the kernel one. This complexifies the implementation 698 * a bit, but makes the arbiter more tolerant to userspace problems and able 699 * to properly cleanup in all cases when a process dies. 700 * Currently, a max of 16 cards simultaneously can have locks issued from 701 * userspace for a given user (file descriptor instance) of the arbiter. 702 * 703 * If the device is hot-unplugged, there is a hook inside the module to notify 704 * they being added/removed in the system and automatically added/removed in 705 * the arbiter. 706 */ 707 708#define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS 709#define PCI_INVALID_CARD ((struct pci_dev *)-1UL) 710 711/* 712 * Each user has an array of these, tracking which cards have locks 713 */ 714struct vga_arb_user_card { 715 struct pci_dev *pdev; 716 unsigned int mem_cnt; 717 unsigned int io_cnt; 718}; 719 720struct vga_arb_private { 721 struct list_head list; 722 struct pci_dev *target; 723 struct vga_arb_user_card cards[MAX_USER_CARDS]; 724 spinlock_t lock; 725}; 726 727static LIST_HEAD(vga_user_list); 728static DEFINE_SPINLOCK(vga_user_lock); 729 730 731/* 732 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and 733 * returns the respective values. If the string is not in this format, 734 * it returns 0. 735 */ 736static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain, 737 unsigned int *bus, unsigned int *devfn) 738{ 739 int n; 740 unsigned int slot, func; 741 742 743 n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func); 744 if (n != 4) 745 return 0; 746 747 *devfn = PCI_DEVFN(slot, func); 748 749 return 1; 750} 751 752static ssize_t vga_arb_read(struct file *file, char __user * buf, 753 size_t count, loff_t *ppos) 754{ 755 struct vga_arb_private *priv = file->private_data; 756 struct vga_device *vgadev; 757 struct pci_dev *pdev; 758 unsigned long flags; 759 size_t len; 760 int rc; 761 char *lbuf; 762 763 lbuf = kmalloc(1024, GFP_KERNEL); 764 if (lbuf == NULL) 765 return -ENOMEM; 766 767 /* Shields against vga_arb_device_card_gone (pci_dev going 768 * away), and allows access to vga list 769 */ 770 spin_lock_irqsave(&vga_lock, flags); 771 772 /* If we are targetting the default, use it */ 773 pdev = priv->target; 774 if (pdev == NULL || pdev == PCI_INVALID_CARD) { 775 spin_unlock_irqrestore(&vga_lock, flags); 776 len = sprintf(lbuf, "invalid"); 777 goto done; 778 } 779 780 /* Find card vgadev structure */ 781 vgadev = vgadev_find(pdev); 782 if (vgadev == NULL) { 783 /* Wow, it's not in the list, that shouldn't happen, 784 * let's fix us up and return invalid card 785 */ 786 if (pdev == priv->target) 787 vga_arb_device_card_gone(pdev); 788 spin_unlock_irqrestore(&vga_lock, flags); 789 len = sprintf(lbuf, "invalid"); 790 goto done; 791 } 792 793 /* Fill the buffer with infos */ 794 len = snprintf(lbuf, 1024, 795 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n", 796 vga_decode_count, pci_name(pdev), 797 vga_iostate_to_str(vgadev->decodes), 798 vga_iostate_to_str(vgadev->owns), 799 vga_iostate_to_str(vgadev->locks), 800 vgadev->io_lock_cnt, vgadev->mem_lock_cnt); 801 802 spin_unlock_irqrestore(&vga_lock, flags); 803done: 804 805 /* Copy that to user */ 806 if (len > count) 807 len = count; 808 rc = copy_to_user(buf, lbuf, len); 809 kfree(lbuf); 810 if (rc) 811 return -EFAULT; 812 return len; 813} 814 815/* 816 * TODO: To avoid parsing inside kernel and to improve the speed we may 817 * consider use ioctl here 818 */ 819static ssize_t vga_arb_write(struct file *file, const char __user * buf, 820 size_t count, loff_t *ppos) 821{ 822 struct vga_arb_private *priv = file->private_data; 823 struct vga_arb_user_card *uc = NULL; 824 struct pci_dev *pdev; 825 826 unsigned int io_state; 827 828 char *kbuf, *curr_pos; 829 size_t remaining = count; 830 831 int ret_val; 832 int i; 833 834 835 kbuf = kmalloc(count + 1, GFP_KERNEL); 836 if (!kbuf) 837 return -ENOMEM; 838 839 if (copy_from_user(kbuf, buf, count)) { 840 kfree(kbuf); 841 return -EFAULT; 842 } 843 curr_pos = kbuf; 844 kbuf[count] = '\0'; /* Just to make sure... */ 845 846 if (strncmp(curr_pos, "lock ", 5) == 0) { 847 curr_pos += 5; 848 remaining -= 5; 849 850 pr_debug("client 0x%p called 'lock'\n", priv); 851 852 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) { 853 ret_val = -EPROTO; 854 goto done; 855 } 856 if (io_state == VGA_RSRC_NONE) { 857 ret_val = -EPROTO; 858 goto done; 859 } 860 861 pdev = priv->target; 862 if (priv->target == NULL) { 863 ret_val = -ENODEV; 864 goto done; 865 } 866 867 vga_get_uninterruptible(pdev, io_state); 868 869 /* Update the client's locks lists... */ 870 for (i = 0; i < MAX_USER_CARDS; i++) { 871 if (priv->cards[i].pdev == pdev) { 872 if (io_state & VGA_RSRC_LEGACY_IO) 873 priv->cards[i].io_cnt++; 874 if (io_state & VGA_RSRC_LEGACY_MEM) 875 priv->cards[i].mem_cnt++; 876 break; 877 } 878 } 879 880 ret_val = count; 881 goto done; 882 } else if (strncmp(curr_pos, "unlock ", 7) == 0) { 883 curr_pos += 7; 884 remaining -= 7; 885 886 pr_debug("client 0x%p called 'unlock'\n", priv); 887 888 if (strncmp(curr_pos, "all", 3) == 0) 889 io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; 890 else { 891 if (!vga_str_to_iostate 892 (curr_pos, remaining, &io_state)) { 893 ret_val = -EPROTO; 894 goto done; 895 } 896 /* TODO: Add this? 897 if (io_state == VGA_RSRC_NONE) { 898 ret_val = -EPROTO; 899 goto done; 900 } 901 */ 902 } 903 904 pdev = priv->target; 905 if (priv->target == NULL) { 906 ret_val = -ENODEV; 907 goto done; 908 } 909 for (i = 0; i < MAX_USER_CARDS; i++) { 910 if (priv->cards[i].pdev == pdev) 911 uc = &priv->cards[i]; 912 } 913 914 if (!uc) 915 return -EINVAL; 916 917 if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) 918 return -EINVAL; 919 920 if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) 921 return -EINVAL; 922 923 vga_put(pdev, io_state); 924 925 if (io_state & VGA_RSRC_LEGACY_IO) 926 uc->io_cnt--; 927 if (io_state & VGA_RSRC_LEGACY_MEM) 928 uc->mem_cnt--; 929 930 ret_val = count; 931 goto done; 932 } else if (strncmp(curr_pos, "trylock ", 8) == 0) { 933 curr_pos += 8; 934 remaining -= 8; 935 936 pr_debug("client 0x%p called 'trylock'\n", priv); 937 938 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) { 939 ret_val = -EPROTO; 940 goto done; 941 } 942 /* TODO: Add this? 943 if (io_state == VGA_RSRC_NONE) { 944 ret_val = -EPROTO; 945 goto done; 946 } 947 */ 948 949 pdev = priv->target; 950 if (priv->target == NULL) { 951 ret_val = -ENODEV; 952 goto done; 953 } 954 955 if (vga_tryget(pdev, io_state)) { 956 /* Update the client's locks lists... */ 957 for (i = 0; i < MAX_USER_CARDS; i++) { 958 if (priv->cards[i].pdev == pdev) { 959 if (io_state & VGA_RSRC_LEGACY_IO) 960 priv->cards[i].io_cnt++; 961 if (io_state & VGA_RSRC_LEGACY_MEM) 962 priv->cards[i].mem_cnt++; 963 break; 964 } 965 } 966 ret_val = count; 967 goto done; 968 } else { 969 ret_val = -EBUSY; 970 goto done; 971 } 972 973 } else if (strncmp(curr_pos, "target ", 7) == 0) { 974 struct pci_bus *pbus; 975 unsigned int domain, bus, devfn; 976 struct vga_device *vgadev; 977 978 curr_pos += 7; 979 remaining -= 7; 980 pr_debug("client 0x%p called 'target'\n", priv); 981 /* if target is default */ 982 if (!strncmp(curr_pos, "default", 7)) 983 pdev = pci_dev_get(vga_default_device()); 984 else { 985 if (!vga_pci_str_to_vars(curr_pos, remaining, 986 &domain, &bus, &devfn)) { 987 ret_val = -EPROTO; 988 goto done; 989 } 990 pr_debug("vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos, 991 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn)); 992 993 pbus = pci_find_bus(domain, bus); 994 pr_debug("vgaarb: pbus %p\n", pbus); 995 if (pbus == NULL) { 996 pr_err("vgaarb: invalid PCI domain and/or bus address %x:%x\n", 997 domain, bus); 998 ret_val = -ENODEV; 999 goto done; 1000 } 1001 pdev = pci_get_slot(pbus, devfn); 1002 pr_debug("vgaarb: pdev %p\n", pdev); 1003 if (!pdev) { 1004 pr_err("vgaarb: invalid PCI address %x:%x\n", 1005 bus, devfn); 1006 ret_val = -ENODEV; 1007 goto done; 1008 } 1009 } 1010 1011 vgadev = vgadev_find(pdev); 1012 pr_debug("vgaarb: vgadev %p\n", vgadev); 1013 if (vgadev == NULL) { 1014 pr_err("vgaarb: this pci device is not a vga device\n"); 1015 pci_dev_put(pdev); 1016 ret_val = -ENODEV; 1017 goto done; 1018 } 1019 1020 priv->target = pdev; 1021 for (i = 0; i < MAX_USER_CARDS; i++) { 1022 if (priv->cards[i].pdev == pdev) 1023 break; 1024 if (priv->cards[i].pdev == NULL) { 1025 priv->cards[i].pdev = pdev; 1026 priv->cards[i].io_cnt = 0; 1027 priv->cards[i].mem_cnt = 0; 1028 break; 1029 } 1030 } 1031 if (i == MAX_USER_CARDS) { 1032 pr_err("vgaarb: maximum user cards (%d) number reached!\n", 1033 MAX_USER_CARDS); 1034 pci_dev_put(pdev); 1035 ret_val = -ENOMEM; 1036 goto done; 1037 } 1038 1039 ret_val = count; 1040 pci_dev_put(pdev); 1041 goto done; 1042 1043 1044 } else if (strncmp(curr_pos, "decodes ", 8) == 0) { 1045 curr_pos += 8; 1046 remaining -= 8; 1047 pr_debug("vgaarb: client 0x%p called 'decodes'\n", priv); 1048 1049 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) { 1050 ret_val = -EPROTO; 1051 goto done; 1052 } 1053 pdev = priv->target; 1054 if (priv->target == NULL) { 1055 ret_val = -ENODEV; 1056 goto done; 1057 } 1058 1059 __vga_set_legacy_decoding(pdev, io_state, true); 1060 ret_val = count; 1061 goto done; 1062 } 1063 /* If we got here, the message written is not part of the protocol! */ 1064 kfree(kbuf); 1065 return -EPROTO; 1066 1067done: 1068 kfree(kbuf); 1069 return ret_val; 1070} 1071 1072static unsigned int vga_arb_fpoll(struct file *file, poll_table * wait) 1073{ 1074 struct vga_arb_private *priv = file->private_data; 1075 1076 pr_debug("%s\n", __func__); 1077 1078 if (priv == NULL) 1079 return -ENODEV; 1080 poll_wait(file, &vga_wait_queue, wait); 1081 return POLLIN; 1082} 1083 1084static int vga_arb_open(struct inode *inode, struct file *file) 1085{ 1086 struct vga_arb_private *priv; 1087 unsigned long flags; 1088 1089 pr_debug("%s\n", __func__); 1090 1091 priv = kmalloc(sizeof(struct vga_arb_private), GFP_KERNEL); 1092 if (priv == NULL) 1093 return -ENOMEM; 1094 memset(priv, 0, sizeof(*priv)); 1095 spin_lock_init(&priv->lock); 1096 file->private_data = priv; 1097 1098 spin_lock_irqsave(&vga_user_lock, flags); 1099 list_add(&priv->list, &vga_user_list); 1100 spin_unlock_irqrestore(&vga_user_lock, flags); 1101 1102 /* Set the client' lists of locks */ 1103 priv->target = vga_default_device(); /* Maybe this is still null! */ 1104 priv->cards[0].pdev = priv->target; 1105 priv->cards[0].io_cnt = 0; 1106 priv->cards[0].mem_cnt = 0; 1107 1108 1109 return 0; 1110} 1111 1112static int vga_arb_release(struct inode *inode, struct file *file) 1113{ 1114 struct vga_arb_private *priv = file->private_data; 1115 struct vga_arb_user_card *uc; 1116 unsigned long flags; 1117 int i; 1118 1119 pr_debug("%s\n", __func__); 1120 1121 if (priv == NULL) 1122 return -ENODEV; 1123 1124 spin_lock_irqsave(&vga_user_lock, flags); 1125 list_del(&priv->list); 1126 for (i = 0; i < MAX_USER_CARDS; i++) { 1127 uc = &priv->cards[i]; 1128 if (uc->pdev == NULL) 1129 continue; 1130 pr_debug("uc->io_cnt == %d, uc->mem_cnt == %d\n", 1131 uc->io_cnt, uc->mem_cnt); 1132 while (uc->io_cnt--) 1133 vga_put(uc->pdev, VGA_RSRC_LEGACY_IO); 1134 while (uc->mem_cnt--) 1135 vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM); 1136 } 1137 spin_unlock_irqrestore(&vga_user_lock, flags); 1138 1139 kfree(priv); 1140 1141 return 0; 1142} 1143 1144static void vga_arb_device_card_gone(struct pci_dev *pdev) 1145{ 1146} 1147 1148/* 1149 * callback any registered clients to let them know we have a 1150 * change in VGA cards 1151 */ 1152static void vga_arbiter_notify_clients(void) 1153{ 1154 struct vga_device *vgadev; 1155 unsigned long flags; 1156 uint32_t new_decodes; 1157 bool new_state; 1158 1159 if (!vga_arbiter_used) 1160 return; 1161 1162 spin_lock_irqsave(&vga_lock, flags); 1163 list_for_each_entry(vgadev, &vga_list, list) { 1164 if (vga_count > 1) 1165 new_state = false; 1166 else 1167 new_state = true; 1168 if (vgadev->set_vga_decode) { 1169 new_decodes = vgadev->set_vga_decode(vgadev->cookie, new_state); 1170 vga_update_device_decodes(vgadev, new_decodes); 1171 } 1172 } 1173 spin_unlock_irqrestore(&vga_lock, flags); 1174} 1175 1176static int pci_notify(struct notifier_block *nb, unsigned long action, 1177 void *data) 1178{ 1179 struct device *dev = data; 1180 struct pci_dev *pdev = to_pci_dev(dev); 1181 bool notify = false; 1182 1183 pr_debug("%s\n", __func__); 1184 1185 /* For now we're only intereted in devices added and removed. I didn't 1186 * test this thing here, so someone needs to double check for the 1187 * cases of hotplugable vga cards. */ 1188 if (action == BUS_NOTIFY_ADD_DEVICE) 1189 notify = vga_arbiter_add_pci_device(pdev); 1190 else if (action == BUS_NOTIFY_DEL_DEVICE) 1191 notify = vga_arbiter_del_pci_device(pdev); 1192 1193 if (notify) 1194 vga_arbiter_notify_clients(); 1195 return 0; 1196} 1197 1198static struct notifier_block pci_notifier = { 1199 .notifier_call = pci_notify, 1200}; 1201 1202static const struct file_operations vga_arb_device_fops = { 1203 .read = vga_arb_read, 1204 .write = vga_arb_write, 1205 .poll = vga_arb_fpoll, 1206 .open = vga_arb_open, 1207 .release = vga_arb_release, 1208}; 1209 1210static struct miscdevice vga_arb_device = { 1211 MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops 1212}; 1213 1214static int __init vga_arb_device_init(void) 1215{ 1216 int rc; 1217 struct pci_dev *pdev; 1218 1219 rc = misc_register(&vga_arb_device); 1220 if (rc < 0) 1221 pr_err("vgaarb: error %d registering device\n", rc); 1222 1223 bus_register_notifier(&pci_bus_type, &pci_notifier); 1224 1225 /* We add all pci devices satisfying vga class in the arbiter by 1226 * default */ 1227 pdev = NULL; 1228 while ((pdev = 1229 pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 1230 PCI_ANY_ID, pdev)) != NULL) 1231 vga_arbiter_add_pci_device(pdev); 1232 1233 pr_info("vgaarb: loaded\n"); 1234 return rc; 1235} 1236subsys_initcall(vga_arb_device_init); 1237