2 * vgaarb.c: Implements the VGA arbitration. For details refer to
3 * Documentation/vgaarbiter.txt
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>
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:
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
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
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 #include <linux/screen_info.h>
46 #include <linux/uaccess.h>
48 #include <linux/vgaarb.h>
50 static void vga_arbiter_notify_clients(void);
52 * We keep a list of all vga devices in the system to speed
53 * up the various operations of the arbiter
56 struct list_head list;
58 unsigned int decodes; /* what does it decodes */
59 unsigned int owns; /* what does it owns */
60 unsigned int locks; /* what does it locks */
61 unsigned int io_lock_cnt; /* legacy IO lock count */
62 unsigned int mem_lock_cnt; /* legacy MEM lock count */
63 unsigned int io_norm_cnt; /* normal IO count */
64 unsigned int mem_norm_cnt; /* normal MEM count */
65 bool bridge_has_one_vga;
66 /* allow IRQ enable/disable hook */
68 void (*irq_set_state)(void *cookie, bool enable);
69 unsigned int (*set_vga_decode)(void *cookie, bool decode);
72 static LIST_HEAD(vga_list);
73 static int vga_count, vga_decode_count;
74 static bool vga_arbiter_used;
75 static DEFINE_SPINLOCK(vga_lock);
76 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
79 static const char *vga_iostate_to_str(unsigned int iostate)
81 /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
82 iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
84 case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
86 case VGA_RSRC_LEGACY_IO:
88 case VGA_RSRC_LEGACY_MEM:
94 static int vga_str_to_iostate(char *buf, int str_size, int *io_state)
96 /* we could in theory hand out locks on IO and mem
97 * separately to userspace but it can cause deadlocks */
98 if (strncmp(buf, "none", 4) == 0) {
99 *io_state = VGA_RSRC_NONE;
103 /* XXX We're not chekcing the str_size! */
104 if (strncmp(buf, "io+mem", 6) == 0)
106 else if (strncmp(buf, "io", 2) == 0)
108 else if (strncmp(buf, "mem", 3) == 0)
112 *io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
116 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
117 /* this is only used a cookie - it should not be dereferenced */
118 static struct pci_dev *vga_default;
121 static void vga_arb_device_card_gone(struct pci_dev *pdev);
123 /* Find somebody in our list */
124 static struct vga_device *vgadev_find(struct pci_dev *pdev)
126 struct vga_device *vgadev;
128 list_for_each_entry(vgadev, &vga_list, list)
129 if (pdev == vgadev->pdev)
134 /* Returns the default VGA device (vgacon's babe) */
135 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
136 struct pci_dev *vga_default_device(void)
141 EXPORT_SYMBOL_GPL(vga_default_device);
143 void vga_set_default_device(struct pci_dev *pdev)
145 if (vga_default == pdev)
148 pci_dev_put(vga_default);
149 vga_default = pci_dev_get(pdev);
153 static inline void vga_irq_set_state(struct vga_device *vgadev, bool state)
155 if (vgadev->irq_set_state)
156 vgadev->irq_set_state(vgadev->cookie, state);
160 /* If we don't ever use VGA arb we should avoid
161 turning off anything anywhere due to old X servers getting
162 confused about the boot device not being VGA */
163 static void vga_check_first_use(void)
165 /* we should inform all GPUs in the system that
166 * VGA arb has occurred and to try and disable resources
168 if (!vga_arbiter_used) {
169 vga_arbiter_used = true;
170 vga_arbiter_notify_clients();
174 static struct vga_device *__vga_tryget(struct vga_device *vgadev,
177 unsigned int wants, legacy_wants, match;
178 struct vga_device *conflict;
179 unsigned int pci_bits;
182 /* Account for "normal" resources to lock. If we decode the legacy,
183 * counterpart, we need to request it as well
185 if ((rsrc & VGA_RSRC_NORMAL_IO) &&
186 (vgadev->decodes & VGA_RSRC_LEGACY_IO))
187 rsrc |= VGA_RSRC_LEGACY_IO;
188 if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
189 (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
190 rsrc |= VGA_RSRC_LEGACY_MEM;
192 pr_debug("%s: %d\n", __func__, rsrc);
193 pr_debug("%s: owns: %d\n", __func__, vgadev->owns);
195 /* Check what resources we need to acquire */
196 wants = rsrc & ~vgadev->owns;
198 /* We already own everything, just mark locked & bye bye */
202 /* We don't need to request a legacy resource, we just enable
203 * appropriate decoding and go
205 legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
206 if (legacy_wants == 0)
209 /* Ok, we don't, let's find out how we need to kick off */
210 list_for_each_entry(conflict, &vga_list, list) {
211 unsigned int lwants = legacy_wants;
212 unsigned int change_bridge = 0;
214 /* Don't conflict with myself */
215 if (vgadev == conflict)
218 /* Check if the architecture allows a conflict between those
219 * 2 devices or if they are on separate domains
221 if (!vga_conflicts(vgadev->pdev, conflict->pdev))
224 /* We have a possible conflict. before we go further, we must
225 * check if we sit on the same bus as the conflicting device.
226 * if we don't, then we must tie both IO and MEM resources
227 * together since there is only a single bit controlling
228 * VGA forwarding on P2P bridges
230 if (vgadev->pdev->bus != conflict->pdev->bus) {
232 lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
235 /* Check if the guy has a lock on the resource. If he does,
236 * return the conflicting entry
238 if (conflict->locks & lwants)
241 /* Ok, now check if it owns the resource we want. We can
242 * lock resources that are not decoded, therefore a device
243 * can own resources it doesn't decode.
245 match = lwants & conflict->owns;
249 /* looks like he doesn't have a lock, we can steal
256 /* If we can't control legacy resources via the bridge, we
257 * also need to disable normal decoding.
259 if (!conflict->bridge_has_one_vga) {
260 if ((match & conflict->decodes) & VGA_RSRC_LEGACY_MEM)
261 pci_bits |= PCI_COMMAND_MEMORY;
262 if ((match & conflict->decodes) & VGA_RSRC_LEGACY_IO)
263 pci_bits |= PCI_COMMAND_IO;
266 vga_irq_set_state(conflict, false);
267 flags |= PCI_VGA_STATE_CHANGE_DECODES;
272 flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
274 pci_set_vga_state(conflict->pdev, false, pci_bits, flags);
275 conflict->owns &= ~match;
277 /* If we disabled normal decoding, reflect it in owns */
278 if (pci_bits & PCI_COMMAND_MEMORY)
279 conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
280 if (pci_bits & PCI_COMMAND_IO)
281 conflict->owns &= ~VGA_RSRC_NORMAL_IO;
285 /* ok dude, we got it, everybody conflicting has been disabled, let's
286 * enable us. Mark any bits in "owns" regardless of whether we
287 * decoded them. We can lock resources we don't decode, therefore
288 * we must track them via "owns".
293 if (!vgadev->bridge_has_one_vga) {
294 flags |= PCI_VGA_STATE_CHANGE_DECODES;
295 if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
296 pci_bits |= PCI_COMMAND_MEMORY;
297 if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
298 pci_bits |= PCI_COMMAND_IO;
300 if (wants & VGA_RSRC_LEGACY_MASK)
301 flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
303 pci_set_vga_state(vgadev->pdev, true, pci_bits, flags);
305 if (!vgadev->bridge_has_one_vga) {
306 vga_irq_set_state(vgadev, true);
308 vgadev->owns |= wants;
310 vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
311 if (rsrc & VGA_RSRC_LEGACY_IO)
312 vgadev->io_lock_cnt++;
313 if (rsrc & VGA_RSRC_LEGACY_MEM)
314 vgadev->mem_lock_cnt++;
315 if (rsrc & VGA_RSRC_NORMAL_IO)
316 vgadev->io_norm_cnt++;
317 if (rsrc & VGA_RSRC_NORMAL_MEM)
318 vgadev->mem_norm_cnt++;
323 static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
325 unsigned int old_locks = vgadev->locks;
327 pr_debug("%s\n", __func__);
329 /* Update our counters, and account for equivalent legacy resources
332 if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
333 vgadev->io_norm_cnt--;
334 if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
335 rsrc |= VGA_RSRC_LEGACY_IO;
337 if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
338 vgadev->mem_norm_cnt--;
339 if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
340 rsrc |= VGA_RSRC_LEGACY_MEM;
342 if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
343 vgadev->io_lock_cnt--;
344 if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
345 vgadev->mem_lock_cnt--;
347 /* Just clear lock bits, we do lazy operations so we don't really
348 * have to bother about anything else at this point
350 if (vgadev->io_lock_cnt == 0)
351 vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
352 if (vgadev->mem_lock_cnt == 0)
353 vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
355 /* Kick the wait queue in case somebody was waiting if we actually
358 if (old_locks != vgadev->locks)
359 wake_up_all(&vga_wait_queue);
362 int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
364 struct vga_device *vgadev, *conflict;
369 vga_check_first_use();
370 /* The one who calls us should check for this, but lets be sure... */
372 pdev = vga_default_device();
377 spin_lock_irqsave(&vga_lock, flags);
378 vgadev = vgadev_find(pdev);
379 if (vgadev == NULL) {
380 spin_unlock_irqrestore(&vga_lock, flags);
384 conflict = __vga_tryget(vgadev, rsrc);
385 spin_unlock_irqrestore(&vga_lock, flags);
386 if (conflict == NULL)
390 /* We have a conflict, we wait until somebody kicks the
391 * work queue. Currently we have one work queue that we
392 * kick each time some resources are released, but it would
393 * be fairly easy to have a per device one so that we only
394 * need to attach to the conflicting device
396 init_waitqueue_entry(&wait, current);
397 add_wait_queue(&vga_wait_queue, &wait);
398 set_current_state(interruptible ?
400 TASK_UNINTERRUPTIBLE);
401 if (signal_pending(current)) {
406 remove_wait_queue(&vga_wait_queue, &wait);
407 set_current_state(TASK_RUNNING);
411 EXPORT_SYMBOL(vga_get);
413 int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
415 struct vga_device *vgadev;
419 vga_check_first_use();
421 /* The one who calls us should check for this, but lets be sure... */
423 pdev = vga_default_device();
426 spin_lock_irqsave(&vga_lock, flags);
427 vgadev = vgadev_find(pdev);
428 if (vgadev == NULL) {
432 if (__vga_tryget(vgadev, rsrc))
435 spin_unlock_irqrestore(&vga_lock, flags);
438 EXPORT_SYMBOL(vga_tryget);
440 void vga_put(struct pci_dev *pdev, unsigned int rsrc)
442 struct vga_device *vgadev;
445 /* The one who calls us should check for this, but lets be sure... */
447 pdev = vga_default_device();
450 spin_lock_irqsave(&vga_lock, flags);
451 vgadev = vgadev_find(pdev);
454 __vga_put(vgadev, rsrc);
456 spin_unlock_irqrestore(&vga_lock, flags);
458 EXPORT_SYMBOL(vga_put);
460 /* Rules for using a bridge to control a VGA descendant decoding:
461 if a bridge has only one VGA descendant then it can be used
462 to control the VGA routing for that device.
463 It should always use the bridge closest to the device to control it.
464 If a bridge has a direct VGA descendant, but also have a sub-bridge
465 VGA descendant then we cannot use that bridge to control the direct VGA descendant.
466 So for every device we register, we need to iterate all its parent bridges
467 so we can invalidate any devices using them properly.
469 static void vga_arbiter_check_bridge_sharing(struct vga_device *vgadev)
471 struct vga_device *same_bridge_vgadev;
472 struct pci_bus *new_bus, *bus;
473 struct pci_dev *new_bridge, *bridge;
475 vgadev->bridge_has_one_vga = true;
477 if (list_empty(&vga_list))
480 /* okay iterate the new devices bridge hierarachy */
481 new_bus = vgadev->pdev->bus;
483 new_bridge = new_bus->self;
485 /* go through list of devices already registered */
486 list_for_each_entry(same_bridge_vgadev, &vga_list, list) {
487 bus = same_bridge_vgadev->pdev->bus;
490 /* see if the share a bridge with this device */
491 if (new_bridge == bridge) {
492 /* if their direct parent bridge is the same
493 as any bridge of this device then it can't be used
495 same_bridge_vgadev->bridge_has_one_vga = false;
498 /* now iterate the previous devices bridge hierarchy */
499 /* if the new devices parent bridge is in the other devices
500 hierarchy then we can't use it to control this device */
504 if (bridge == vgadev->pdev->bus->self)
505 vgadev->bridge_has_one_vga = false;
510 new_bus = new_bus->parent;
515 * Currently, we assume that the "initial" setup of the system is
516 * not sane, that is we come up with conflicting devices and let
517 * the arbiter's client decides if devices decodes or not legacy
520 static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
522 struct vga_device *vgadev;
525 struct pci_dev *bridge;
528 /* Only deal with VGA class devices */
529 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
532 /* Allocate structure */
533 vgadev = kmalloc(sizeof(struct vga_device), GFP_KERNEL);
534 if (vgadev == NULL) {
535 pr_err("vgaarb: failed to allocate pci device\n");
536 /* What to do on allocation failure ? For now, let's
537 * just do nothing, I'm not sure there is anything saner
543 memset(vgadev, 0, sizeof(*vgadev));
545 /* Take lock & check for duplicates */
546 spin_lock_irqsave(&vga_lock, flags);
547 if (vgadev_find(pdev) != NULL) {
553 /* By default, assume we decode everything */
554 vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
555 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
557 /* by default mark it as decoding */
559 /* Mark that we "own" resources based on our enables, we will
560 * clear that below if the bridge isn't forwarding
562 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
563 if (cmd & PCI_COMMAND_IO)
564 vgadev->owns |= VGA_RSRC_LEGACY_IO;
565 if (cmd & PCI_COMMAND_MEMORY)
566 vgadev->owns |= VGA_RSRC_LEGACY_MEM;
568 /* Check if VGA cycles can get down to us */
574 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
576 if (!(l & PCI_BRIDGE_CTL_VGA)) {
584 /* Deal with VGA default device. Use first enabled one
585 * by default if arch doesn't have it's own hook
587 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
588 if (vga_default == NULL &&
589 ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)) {
590 pr_info("vgaarb: setting as boot device: PCI:%s\n",
592 vga_set_default_device(pdev);
596 vga_arbiter_check_bridge_sharing(vgadev);
598 /* Add to the list */
599 list_add(&vgadev->list, &vga_list);
601 pr_info("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
603 vga_iostate_to_str(vgadev->decodes),
604 vga_iostate_to_str(vgadev->owns),
605 vga_iostate_to_str(vgadev->locks));
607 spin_unlock_irqrestore(&vga_lock, flags);
610 spin_unlock_irqrestore(&vga_lock, flags);
615 static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
617 struct vga_device *vgadev;
621 spin_lock_irqsave(&vga_lock, flags);
622 vgadev = vgadev_find(pdev);
623 if (vgadev == NULL) {
628 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
629 if (vga_default == pdev)
630 vga_set_default_device(NULL);
633 if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
636 /* Remove entry from list */
637 list_del(&vgadev->list);
639 /* Notify userland driver that the device is gone so it discards
640 * it's copies of the pci_dev pointer
642 vga_arb_device_card_gone(pdev);
644 /* Wake up all possible waiters */
645 wake_up_all(&vga_wait_queue);
647 spin_unlock_irqrestore(&vga_lock, flags);
652 /* this is called with the lock */
653 static inline void vga_update_device_decodes(struct vga_device *vgadev,
656 int old_decodes, decodes_removed, decodes_unlocked;
658 old_decodes = vgadev->decodes;
659 decodes_removed = ~new_decodes & old_decodes;
660 decodes_unlocked = vgadev->locks & decodes_removed;
661 vgadev->decodes = new_decodes;
663 pr_info("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
664 pci_name(vgadev->pdev),
665 vga_iostate_to_str(old_decodes),
666 vga_iostate_to_str(vgadev->decodes),
667 vga_iostate_to_str(vgadev->owns));
669 /* if we removed locked decodes, lock count goes to zero, and release */
670 if (decodes_unlocked) {
671 if (decodes_unlocked & VGA_RSRC_LEGACY_IO)
672 vgadev->io_lock_cnt = 0;
673 if (decodes_unlocked & VGA_RSRC_LEGACY_MEM)
674 vgadev->mem_lock_cnt = 0;
675 __vga_put(vgadev, decodes_unlocked);
678 /* change decodes counter */
679 if (old_decodes & VGA_RSRC_LEGACY_MASK &&
680 !(new_decodes & VGA_RSRC_LEGACY_MASK))
682 if (!(old_decodes & VGA_RSRC_LEGACY_MASK) &&
683 new_decodes & VGA_RSRC_LEGACY_MASK)
685 pr_debug("vgaarb: decoding count now is: %d\n", vga_decode_count);
688 static void __vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes, bool userspace)
690 struct vga_device *vgadev;
693 decodes &= VGA_RSRC_LEGACY_MASK;
695 spin_lock_irqsave(&vga_lock, flags);
696 vgadev = vgadev_find(pdev);
700 /* don't let userspace futz with kernel driver decodes */
701 if (userspace && vgadev->set_vga_decode)
704 /* update the device decodes + counter */
705 vga_update_device_decodes(vgadev, decodes);
707 /* XXX if somebody is going from "doesn't decode" to "decodes" state
708 * here, additional care must be taken as we may have pending owner
709 * ship of non-legacy region ...
712 spin_unlock_irqrestore(&vga_lock, flags);
715 void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
717 __vga_set_legacy_decoding(pdev, decodes, false);
719 EXPORT_SYMBOL(vga_set_legacy_decoding);
721 /* call with NULL to unregister */
722 int vga_client_register(struct pci_dev *pdev, void *cookie,
723 void (*irq_set_state)(void *cookie, bool state),
724 unsigned int (*set_vga_decode)(void *cookie, bool decode))
727 struct vga_device *vgadev;
730 spin_lock_irqsave(&vga_lock, flags);
731 vgadev = vgadev_find(pdev);
735 vgadev->irq_set_state = irq_set_state;
736 vgadev->set_vga_decode = set_vga_decode;
737 vgadev->cookie = cookie;
741 spin_unlock_irqrestore(&vga_lock, flags);
745 EXPORT_SYMBOL(vga_client_register);
748 * Char driver implementation
752 * open : open user instance of the arbitrer. by default, it's
753 * attached to the default VGA device of the system.
755 * close : close user instance, release locks
757 * read : return a string indicating the status of the target.
758 * an IO state string is of the form {io,mem,io+mem,none},
759 * mc and ic are respectively mem and io lock counts (for
760 * debugging/diagnostic only). "decodes" indicate what the
761 * card currently decodes, "owns" indicates what is currently
762 * enabled on it, and "locks" indicates what is locked by this
763 * card. If the card is unplugged, we get "invalid" then for
764 * card_ID and an -ENODEV error is returned for any command
765 * until a new card is targeted
767 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
769 * write : write a command to the arbiter. List of commands is:
771 * target <card_ID> : switch target to card <card_ID> (see below)
772 * lock <io_state> : acquires locks on target ("none" is invalid io_state)
773 * trylock <io_state> : non-blocking acquire locks on target
774 * unlock <io_state> : release locks on target
775 * unlock all : release all locks on target held by this user
776 * decodes <io_state> : set the legacy decoding attributes for the card
778 * poll : event if something change on any card (not just the target)
780 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
781 * to go back to the system default card (TODO: not implemented yet).
782 * Currently, only PCI is supported as a prefix, but the userland API may
783 * support other bus types in the future, even if the current kernel
784 * implementation doesn't.
788 * The driver keeps track of which user has what locks on which card. It
789 * supports stacking, like the kernel one. This complexifies the implementation
790 * a bit, but makes the arbiter more tolerant to userspace problems and able
791 * to properly cleanup in all cases when a process dies.
792 * Currently, a max of 16 cards simultaneously can have locks issued from
793 * userspace for a given user (file descriptor instance) of the arbiter.
795 * If the device is hot-unplugged, there is a hook inside the module to notify
796 * they being added/removed in the system and automatically added/removed in
800 #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
801 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
804 * Each user has an array of these, tracking which cards have locks
806 struct vga_arb_user_card {
807 struct pci_dev *pdev;
808 unsigned int mem_cnt;
812 struct vga_arb_private {
813 struct list_head list;
814 struct pci_dev *target;
815 struct vga_arb_user_card cards[MAX_USER_CARDS];
819 static LIST_HEAD(vga_user_list);
820 static DEFINE_SPINLOCK(vga_user_lock);
824 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
825 * returns the respective values. If the string is not in this format,
828 static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
829 unsigned int *bus, unsigned int *devfn)
832 unsigned int slot, func;
835 n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
839 *devfn = PCI_DEVFN(slot, func);
844 static ssize_t vga_arb_read(struct file *file, char __user * buf,
845 size_t count, loff_t *ppos)
847 struct vga_arb_private *priv = file->private_data;
848 struct vga_device *vgadev;
849 struct pci_dev *pdev;
855 lbuf = kmalloc(1024, GFP_KERNEL);
859 /* Shields against vga_arb_device_card_gone (pci_dev going
860 * away), and allows access to vga list
862 spin_lock_irqsave(&vga_lock, flags);
864 /* If we are targeting the default, use it */
866 if (pdev == NULL || pdev == PCI_INVALID_CARD) {
867 spin_unlock_irqrestore(&vga_lock, flags);
868 len = sprintf(lbuf, "invalid");
872 /* Find card vgadev structure */
873 vgadev = vgadev_find(pdev);
874 if (vgadev == NULL) {
875 /* Wow, it's not in the list, that shouldn't happen,
876 * let's fix us up and return invalid card
878 if (pdev == priv->target)
879 vga_arb_device_card_gone(pdev);
880 spin_unlock_irqrestore(&vga_lock, flags);
881 len = sprintf(lbuf, "invalid");
885 /* Fill the buffer with infos */
886 len = snprintf(lbuf, 1024,
887 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
888 vga_decode_count, pci_name(pdev),
889 vga_iostate_to_str(vgadev->decodes),
890 vga_iostate_to_str(vgadev->owns),
891 vga_iostate_to_str(vgadev->locks),
892 vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
894 spin_unlock_irqrestore(&vga_lock, flags);
897 /* Copy that to user */
900 rc = copy_to_user(buf, lbuf, len);
908 * TODO: To avoid parsing inside kernel and to improve the speed we may
909 * consider use ioctl here
911 static ssize_t vga_arb_write(struct file *file, const char __user * buf,
912 size_t count, loff_t *ppos)
914 struct vga_arb_private *priv = file->private_data;
915 struct vga_arb_user_card *uc = NULL;
916 struct pci_dev *pdev;
918 unsigned int io_state;
920 char *kbuf, *curr_pos;
921 size_t remaining = count;
927 kbuf = kmalloc(count + 1, GFP_KERNEL);
931 if (copy_from_user(kbuf, buf, count)) {
936 kbuf[count] = '\0'; /* Just to make sure... */
938 if (strncmp(curr_pos, "lock ", 5) == 0) {
942 pr_debug("client 0x%p called 'lock'\n", priv);
944 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
948 if (io_state == VGA_RSRC_NONE) {
954 if (priv->target == NULL) {
959 vga_get_uninterruptible(pdev, io_state);
961 /* Update the client's locks lists... */
962 for (i = 0; i < MAX_USER_CARDS; i++) {
963 if (priv->cards[i].pdev == pdev) {
964 if (io_state & VGA_RSRC_LEGACY_IO)
965 priv->cards[i].io_cnt++;
966 if (io_state & VGA_RSRC_LEGACY_MEM)
967 priv->cards[i].mem_cnt++;
974 } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
978 pr_debug("client 0x%p called 'unlock'\n", priv);
980 if (strncmp(curr_pos, "all", 3) == 0)
981 io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
983 if (!vga_str_to_iostate
984 (curr_pos, remaining, &io_state)) {
989 if (io_state == VGA_RSRC_NONE) {
997 if (priv->target == NULL) {
1001 for (i = 0; i < MAX_USER_CARDS; i++) {
1002 if (priv->cards[i].pdev == pdev)
1003 uc = &priv->cards[i];
1011 if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
1016 if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
1021 vga_put(pdev, io_state);
1023 if (io_state & VGA_RSRC_LEGACY_IO)
1025 if (io_state & VGA_RSRC_LEGACY_MEM)
1030 } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
1034 pr_debug("client 0x%p called 'trylock'\n", priv);
1036 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1041 if (io_state == VGA_RSRC_NONE) {
1047 pdev = priv->target;
1048 if (priv->target == NULL) {
1053 if (vga_tryget(pdev, io_state)) {
1054 /* Update the client's locks lists... */
1055 for (i = 0; i < MAX_USER_CARDS; i++) {
1056 if (priv->cards[i].pdev == pdev) {
1057 if (io_state & VGA_RSRC_LEGACY_IO)
1058 priv->cards[i].io_cnt++;
1059 if (io_state & VGA_RSRC_LEGACY_MEM)
1060 priv->cards[i].mem_cnt++;
1071 } else if (strncmp(curr_pos, "target ", 7) == 0) {
1072 unsigned int domain, bus, devfn;
1073 struct vga_device *vgadev;
1077 pr_debug("client 0x%p called 'target'\n", priv);
1078 /* if target is default */
1079 if (!strncmp(curr_pos, "default", 7))
1080 pdev = pci_dev_get(vga_default_device());
1082 if (!vga_pci_str_to_vars(curr_pos, remaining,
1083 &domain, &bus, &devfn)) {
1087 pr_debug("vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos,
1088 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
1090 pdev = pci_get_domain_bus_and_slot(domain, bus, devfn);
1091 pr_debug("vgaarb: pdev %p\n", pdev);
1093 pr_err("vgaarb: invalid PCI address %x:%x:%x\n",
1094 domain, bus, devfn);
1100 vgadev = vgadev_find(pdev);
1101 pr_debug("vgaarb: vgadev %p\n", vgadev);
1102 if (vgadev == NULL) {
1103 pr_err("vgaarb: this pci device is not a vga device\n");
1109 priv->target = pdev;
1110 for (i = 0; i < MAX_USER_CARDS; i++) {
1111 if (priv->cards[i].pdev == pdev)
1113 if (priv->cards[i].pdev == NULL) {
1114 priv->cards[i].pdev = pdev;
1115 priv->cards[i].io_cnt = 0;
1116 priv->cards[i].mem_cnt = 0;
1120 if (i == MAX_USER_CARDS) {
1121 pr_err("vgaarb: maximum user cards (%d) number reached!\n",
1124 /* XXX: which value to return? */
1134 } else if (strncmp(curr_pos, "decodes ", 8) == 0) {
1137 pr_debug("vgaarb: client 0x%p called 'decodes'\n", priv);
1139 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1143 pdev = priv->target;
1144 if (priv->target == NULL) {
1149 __vga_set_legacy_decoding(pdev, io_state, true);
1153 /* If we got here, the message written is not part of the protocol! */
1162 static unsigned int vga_arb_fpoll(struct file *file, poll_table * wait)
1164 struct vga_arb_private *priv = file->private_data;
1166 pr_debug("%s\n", __func__);
1170 poll_wait(file, &vga_wait_queue, wait);
1174 static int vga_arb_open(struct inode *inode, struct file *file)
1176 struct vga_arb_private *priv;
1177 unsigned long flags;
1179 pr_debug("%s\n", __func__);
1181 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1184 spin_lock_init(&priv->lock);
1185 file->private_data = priv;
1187 spin_lock_irqsave(&vga_user_lock, flags);
1188 list_add(&priv->list, &vga_user_list);
1189 spin_unlock_irqrestore(&vga_user_lock, flags);
1191 /* Set the client' lists of locks */
1192 priv->target = vga_default_device(); /* Maybe this is still null! */
1193 priv->cards[0].pdev = priv->target;
1194 priv->cards[0].io_cnt = 0;
1195 priv->cards[0].mem_cnt = 0;
1201 static int vga_arb_release(struct inode *inode, struct file *file)
1203 struct vga_arb_private *priv = file->private_data;
1204 struct vga_arb_user_card *uc;
1205 unsigned long flags;
1208 pr_debug("%s\n", __func__);
1213 spin_lock_irqsave(&vga_user_lock, flags);
1214 list_del(&priv->list);
1215 for (i = 0; i < MAX_USER_CARDS; i++) {
1216 uc = &priv->cards[i];
1217 if (uc->pdev == NULL)
1219 pr_debug("uc->io_cnt == %d, uc->mem_cnt == %d\n",
1220 uc->io_cnt, uc->mem_cnt);
1221 while (uc->io_cnt--)
1222 vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
1223 while (uc->mem_cnt--)
1224 vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
1226 spin_unlock_irqrestore(&vga_user_lock, flags);
1233 static void vga_arb_device_card_gone(struct pci_dev *pdev)
1238 * callback any registered clients to let them know we have a
1239 * change in VGA cards
1241 static void vga_arbiter_notify_clients(void)
1243 struct vga_device *vgadev;
1244 unsigned long flags;
1245 uint32_t new_decodes;
1248 if (!vga_arbiter_used)
1251 spin_lock_irqsave(&vga_lock, flags);
1252 list_for_each_entry(vgadev, &vga_list, list) {
1257 if (vgadev->set_vga_decode) {
1258 new_decodes = vgadev->set_vga_decode(vgadev->cookie, new_state);
1259 vga_update_device_decodes(vgadev, new_decodes);
1262 spin_unlock_irqrestore(&vga_lock, flags);
1265 static int pci_notify(struct notifier_block *nb, unsigned long action,
1268 struct device *dev = data;
1269 struct pci_dev *pdev = to_pci_dev(dev);
1270 bool notify = false;
1272 pr_debug("%s\n", __func__);
1274 /* For now we're only intereted in devices added and removed. I didn't
1275 * test this thing here, so someone needs to double check for the
1276 * cases of hotplugable vga cards. */
1277 if (action == BUS_NOTIFY_ADD_DEVICE)
1278 notify = vga_arbiter_add_pci_device(pdev);
1279 else if (action == BUS_NOTIFY_DEL_DEVICE)
1280 notify = vga_arbiter_del_pci_device(pdev);
1283 vga_arbiter_notify_clients();
1287 static struct notifier_block pci_notifier = {
1288 .notifier_call = pci_notify,
1291 static const struct file_operations vga_arb_device_fops = {
1292 .read = vga_arb_read,
1293 .write = vga_arb_write,
1294 .poll = vga_arb_fpoll,
1295 .open = vga_arb_open,
1296 .release = vga_arb_release,
1297 .llseek = noop_llseek,
1300 static struct miscdevice vga_arb_device = {
1301 MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
1304 static int __init vga_arb_device_init(void)
1307 struct pci_dev *pdev;
1308 struct vga_device *vgadev;
1310 rc = misc_register(&vga_arb_device);
1312 pr_err("vgaarb: error %d registering device\n", rc);
1314 bus_register_notifier(&pci_bus_type, &pci_notifier);
1316 /* We add all pci devices satisfying vga class in the arbiter by
1320 pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
1321 PCI_ANY_ID, pdev)) != NULL)
1322 vga_arbiter_add_pci_device(pdev);
1324 pr_info("vgaarb: loaded\n");
1326 list_for_each_entry(vgadev, &vga_list, list) {
1327 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
1328 /* Override I/O based detection done by vga_arbiter_add_pci_device()
1329 * as it may take the wrong device (e.g. on Apple system under EFI).
1331 * Select the device owning the boot framebuffer if there is one.
1333 resource_size_t start, end;
1336 /* Does firmware framebuffer belong to us? */
1337 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1338 if (!(pci_resource_flags(vgadev->pdev, i) & IORESOURCE_MEM))
1341 start = pci_resource_start(vgadev->pdev, i);
1342 end = pci_resource_end(vgadev->pdev, i);
1347 if (screen_info.lfb_base < start ||
1348 (screen_info.lfb_base + screen_info.lfb_size) >= end)
1350 if (!vga_default_device())
1351 pr_info("vgaarb: setting as boot device: PCI:%s\n",
1352 pci_name(vgadev->pdev));
1353 else if (vgadev->pdev != vga_default_device())
1354 pr_info("vgaarb: overriding boot device: PCI:%s\n",
1355 pci_name(vgadev->pdev));
1356 vga_set_default_device(vgadev->pdev);
1359 if (vgadev->bridge_has_one_vga)
1360 pr_info("vgaarb: bridge control possible %s\n", pci_name(vgadev->pdev));
1362 pr_info("vgaarb: no bridge control possible %s\n", pci_name(vgadev->pdev));
1366 subsys_initcall(vga_arb_device_init);