1 // SPDX-License-Identifier: MIT
3 * vgaarb.c: Implements the VGA arbitration. For details refer to
4 * Documentation/gpu/vgaarbiter.rst
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>
11 #define pr_fmt(fmt) "vgaarb: " fmt
13 #define vgaarb_dbg(dev, fmt, arg...) dev_dbg(dev, "vgaarb: " fmt, ##arg)
14 #define vgaarb_info(dev, fmt, arg...) dev_info(dev, "vgaarb: " fmt, ##arg)
15 #define vgaarb_err(dev, fmt, arg...) dev_err(dev, "vgaarb: " fmt, ##arg)
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/pci.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/list.h>
23 #include <linux/sched/signal.h>
24 #include <linux/wait.h>
25 #include <linux/spinlock.h>
26 #include <linux/poll.h>
27 #include <linux/miscdevice.h>
28 #include <linux/slab.h>
29 #include <linux/screen_info.h>
31 #include <linux/console.h>
32 #include <linux/acpi.h>
34 #include <linux/uaccess.h>
36 #include <linux/vgaarb.h>
38 static void vga_arbiter_notify_clients(void);
40 * We keep a list of all vga devices in the system to speed
41 * up the various operations of the arbiter
44 struct list_head list;
46 unsigned int decodes; /* what does it decodes */
47 unsigned int owns; /* what does it owns */
48 unsigned int locks; /* what does it locks */
49 unsigned int io_lock_cnt; /* legacy IO lock count */
50 unsigned int mem_lock_cnt; /* legacy MEM lock count */
51 unsigned int io_norm_cnt; /* normal IO count */
52 unsigned int mem_norm_cnt; /* normal MEM count */
53 bool bridge_has_one_vga;
54 bool is_firmware_default; /* device selected by firmware */
55 unsigned int (*set_decode)(struct pci_dev *pdev, bool decode);
58 static LIST_HEAD(vga_list);
59 static int vga_count, vga_decode_count;
60 static bool vga_arbiter_used;
61 static DEFINE_SPINLOCK(vga_lock);
62 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
65 static const char *vga_iostate_to_str(unsigned int iostate)
67 /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
68 iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
70 case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
72 case VGA_RSRC_LEGACY_IO:
74 case VGA_RSRC_LEGACY_MEM:
80 static int vga_str_to_iostate(char *buf, int str_size, int *io_state)
82 /* we could in theory hand out locks on IO and mem
83 * separately to userspace but it can cause deadlocks */
84 if (strncmp(buf, "none", 4) == 0) {
85 *io_state = VGA_RSRC_NONE;
89 /* XXX We're not chekcing the str_size! */
90 if (strncmp(buf, "io+mem", 6) == 0)
92 else if (strncmp(buf, "io", 2) == 0)
94 else if (strncmp(buf, "mem", 3) == 0)
98 *io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
102 /* this is only used a cookie - it should not be dereferenced */
103 static struct pci_dev *vga_default;
105 /* Find somebody in our list */
106 static struct vga_device *vgadev_find(struct pci_dev *pdev)
108 struct vga_device *vgadev;
110 list_for_each_entry(vgadev, &vga_list, list)
111 if (pdev == vgadev->pdev)
117 * vga_default_device - return the default VGA device, for vgacon
119 * This can be defined by the platform. The default implementation
120 * is rather dumb and will probably only work properly on single
121 * vga card setups and/or x86 platforms.
123 * If your VGA default device is not PCI, you'll have to return
124 * NULL here. In this case, I assume it will not conflict with
125 * any PCI card. If this is not true, I'll have to define two archs
126 * hooks for enabling/disabling the VGA default device if that is
127 * possible. This may be a problem with real _ISA_ VGA cards, in
128 * addition to a PCI one. I don't know at this point how to deal
129 * with that card. Can theirs IOs be disabled at all ? If not, then
130 * I suppose it's a matter of having the proper arch hook telling
131 * us about it, so we basically never allow anybody to succeed a
134 struct pci_dev *vga_default_device(void)
138 EXPORT_SYMBOL_GPL(vga_default_device);
140 void vga_set_default_device(struct pci_dev *pdev)
142 if (vga_default == pdev)
145 pci_dev_put(vga_default);
146 vga_default = pci_dev_get(pdev);
150 * vga_remove_vgacon - deactivete vga console
152 * Unbind and unregister vgacon in case pdev is the default vga
153 * device. Can be called by gpu drivers on initialization to make
154 * sure vga register access done by vgacon will not disturb the
159 #if !defined(CONFIG_VGA_CONSOLE)
160 int vga_remove_vgacon(struct pci_dev *pdev)
164 #elif !defined(CONFIG_DUMMY_CONSOLE)
165 int vga_remove_vgacon(struct pci_dev *pdev)
170 int vga_remove_vgacon(struct pci_dev *pdev)
174 if (pdev != vga_default)
176 vgaarb_info(&pdev->dev, "deactivate vga console\n");
179 if (con_is_bound(&vga_con))
180 ret = do_take_over_console(&dummy_con, 0,
181 MAX_NR_CONSOLES - 1, 1);
183 ret = do_unregister_con_driver(&vga_con);
185 /* Ignore "already unregistered". */
194 EXPORT_SYMBOL(vga_remove_vgacon);
196 /* If we don't ever use VGA arb we should avoid
197 turning off anything anywhere due to old X servers getting
198 confused about the boot device not being VGA */
199 static void vga_check_first_use(void)
201 /* we should inform all GPUs in the system that
202 * VGA arb has occurred and to try and disable resources
204 if (!vga_arbiter_used) {
205 vga_arbiter_used = true;
206 vga_arbiter_notify_clients();
210 static struct vga_device *__vga_tryget(struct vga_device *vgadev,
213 struct device *dev = &vgadev->pdev->dev;
214 unsigned int wants, legacy_wants, match;
215 struct vga_device *conflict;
216 unsigned int pci_bits;
219 /* Account for "normal" resources to lock. If we decode the legacy,
220 * counterpart, we need to request it as well
222 if ((rsrc & VGA_RSRC_NORMAL_IO) &&
223 (vgadev->decodes & VGA_RSRC_LEGACY_IO))
224 rsrc |= VGA_RSRC_LEGACY_IO;
225 if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
226 (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
227 rsrc |= VGA_RSRC_LEGACY_MEM;
229 vgaarb_dbg(dev, "%s: %d\n", __func__, rsrc);
230 vgaarb_dbg(dev, "%s: owns: %d\n", __func__, vgadev->owns);
232 /* Check what resources we need to acquire */
233 wants = rsrc & ~vgadev->owns;
235 /* We already own everything, just mark locked & bye bye */
239 /* We don't need to request a legacy resource, we just enable
240 * appropriate decoding and go
242 legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
243 if (legacy_wants == 0)
246 /* Ok, we don't, let's find out how we need to kick off */
247 list_for_each_entry(conflict, &vga_list, list) {
248 unsigned int lwants = legacy_wants;
249 unsigned int change_bridge = 0;
251 /* Don't conflict with myself */
252 if (vgadev == conflict)
255 /* We have a possible conflict. before we go further, we must
256 * check if we sit on the same bus as the conflicting device.
257 * if we don't, then we must tie both IO and MEM resources
258 * together since there is only a single bit controlling
259 * VGA forwarding on P2P bridges
261 if (vgadev->pdev->bus != conflict->pdev->bus) {
263 lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
266 /* Check if the guy has a lock on the resource. If he does,
267 * return the conflicting entry
269 if (conflict->locks & lwants)
272 /* Ok, now check if it owns the resource we want. We can
273 * lock resources that are not decoded, therefore a device
274 * can own resources it doesn't decode.
276 match = lwants & conflict->owns;
280 /* looks like he doesn't have a lock, we can steal
287 /* If we can't control legacy resources via the bridge, we
288 * also need to disable normal decoding.
290 if (!conflict->bridge_has_one_vga) {
291 if ((match & conflict->decodes) & VGA_RSRC_LEGACY_MEM)
292 pci_bits |= PCI_COMMAND_MEMORY;
293 if ((match & conflict->decodes) & VGA_RSRC_LEGACY_IO)
294 pci_bits |= PCI_COMMAND_IO;
297 flags |= PCI_VGA_STATE_CHANGE_DECODES;
301 flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
303 pci_set_vga_state(conflict->pdev, false, pci_bits, flags);
304 conflict->owns &= ~match;
306 /* If we disabled normal decoding, reflect it in owns */
307 if (pci_bits & PCI_COMMAND_MEMORY)
308 conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
309 if (pci_bits & PCI_COMMAND_IO)
310 conflict->owns &= ~VGA_RSRC_NORMAL_IO;
314 /* ok dude, we got it, everybody conflicting has been disabled, let's
315 * enable us. Mark any bits in "owns" regardless of whether we
316 * decoded them. We can lock resources we don't decode, therefore
317 * we must track them via "owns".
322 if (!vgadev->bridge_has_one_vga) {
323 flags |= PCI_VGA_STATE_CHANGE_DECODES;
324 if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
325 pci_bits |= PCI_COMMAND_MEMORY;
326 if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
327 pci_bits |= PCI_COMMAND_IO;
329 if (wants & VGA_RSRC_LEGACY_MASK)
330 flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
332 pci_set_vga_state(vgadev->pdev, true, pci_bits, flags);
334 vgadev->owns |= wants;
336 vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
337 if (rsrc & VGA_RSRC_LEGACY_IO)
338 vgadev->io_lock_cnt++;
339 if (rsrc & VGA_RSRC_LEGACY_MEM)
340 vgadev->mem_lock_cnt++;
341 if (rsrc & VGA_RSRC_NORMAL_IO)
342 vgadev->io_norm_cnt++;
343 if (rsrc & VGA_RSRC_NORMAL_MEM)
344 vgadev->mem_norm_cnt++;
349 static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
351 struct device *dev = &vgadev->pdev->dev;
352 unsigned int old_locks = vgadev->locks;
354 vgaarb_dbg(dev, "%s\n", __func__);
356 /* Update our counters, and account for equivalent legacy resources
359 if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
360 vgadev->io_norm_cnt--;
361 if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
362 rsrc |= VGA_RSRC_LEGACY_IO;
364 if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
365 vgadev->mem_norm_cnt--;
366 if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
367 rsrc |= VGA_RSRC_LEGACY_MEM;
369 if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
370 vgadev->io_lock_cnt--;
371 if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
372 vgadev->mem_lock_cnt--;
374 /* Just clear lock bits, we do lazy operations so we don't really
375 * have to bother about anything else at this point
377 if (vgadev->io_lock_cnt == 0)
378 vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
379 if (vgadev->mem_lock_cnt == 0)
380 vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
382 /* Kick the wait queue in case somebody was waiting if we actually
385 if (old_locks != vgadev->locks)
386 wake_up_all(&vga_wait_queue);
390 * vga_get - acquire & locks VGA resources
391 * @pdev: pci device of the VGA card or NULL for the system default
392 * @rsrc: bit mask of resources to acquire and lock
393 * @interruptible: blocking should be interruptible by signals ?
395 * This function acquires VGA resources for the given card and mark those
396 * resources locked. If the resource requested are "normal" (and not legacy)
397 * resources, the arbiter will first check whether the card is doing legacy
398 * decoding for that type of resource. If yes, the lock is "converted" into a
399 * legacy resource lock.
401 * The arbiter will first look for all VGA cards that might conflict and disable
402 * their IOs and/or Memory access, including VGA forwarding on P2P bridges if
403 * necessary, so that the requested resources can be used. Then, the card is
404 * marked as locking these resources and the IO and/or Memory accesses are
405 * enabled on the card (including VGA forwarding on parent P2P bridges if any).
407 * This function will block if some conflicting card is already locking one of
408 * the required resources (or any resource on a different bus segment, since P2P
409 * bridges don't differentiate VGA memory and IO afaik). You can indicate
410 * whether this blocking should be interruptible by a signal (for userland
413 * Must not be called at interrupt time or in atomic context. If the card
414 * already owns the resources, the function succeeds. Nested calls are
415 * supported (a per-resource counter is maintained)
417 * On success, release the VGA resource again with vga_put().
421 * 0 on success, negative error code on failure.
423 int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
425 struct vga_device *vgadev, *conflict;
427 wait_queue_entry_t wait;
430 vga_check_first_use();
431 /* The one who calls us should check for this, but lets be sure... */
433 pdev = vga_default_device();
438 spin_lock_irqsave(&vga_lock, flags);
439 vgadev = vgadev_find(pdev);
440 if (vgadev == NULL) {
441 spin_unlock_irqrestore(&vga_lock, flags);
445 conflict = __vga_tryget(vgadev, rsrc);
446 spin_unlock_irqrestore(&vga_lock, flags);
447 if (conflict == NULL)
451 /* We have a conflict, we wait until somebody kicks the
452 * work queue. Currently we have one work queue that we
453 * kick each time some resources are released, but it would
454 * be fairly easy to have a per device one so that we only
455 * need to attach to the conflicting device
457 init_waitqueue_entry(&wait, current);
458 add_wait_queue(&vga_wait_queue, &wait);
459 set_current_state(interruptible ?
461 TASK_UNINTERRUPTIBLE);
462 if (interruptible && signal_pending(current)) {
463 __set_current_state(TASK_RUNNING);
464 remove_wait_queue(&vga_wait_queue, &wait);
469 remove_wait_queue(&vga_wait_queue, &wait);
473 EXPORT_SYMBOL(vga_get);
476 * vga_tryget - try to acquire & lock legacy VGA resources
477 * @pdev: pci devivce of VGA card or NULL for system default
478 * @rsrc: bit mask of resources to acquire and lock
480 * This function performs the same operation as vga_get(), but will return an
481 * error (-EBUSY) instead of blocking if the resources are already locked by
482 * another card. It can be called in any context
484 * On success, release the VGA resource again with vga_put().
488 * 0 on success, negative error code on failure.
490 static int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
492 struct vga_device *vgadev;
496 vga_check_first_use();
498 /* The one who calls us should check for this, but lets be sure... */
500 pdev = vga_default_device();
503 spin_lock_irqsave(&vga_lock, flags);
504 vgadev = vgadev_find(pdev);
505 if (vgadev == NULL) {
509 if (__vga_tryget(vgadev, rsrc))
512 spin_unlock_irqrestore(&vga_lock, flags);
517 * vga_put - release lock on legacy VGA resources
518 * @pdev: pci device of VGA card or NULL for system default
519 * @rsrc: but mask of resource to release
521 * This fuction releases resources previously locked by vga_get() or
522 * vga_tryget(). The resources aren't disabled right away, so that a subsequence
523 * vga_get() on the same card will succeed immediately. Resources have a
524 * counter, so locks are only released if the counter reaches 0.
526 void vga_put(struct pci_dev *pdev, unsigned int rsrc)
528 struct vga_device *vgadev;
531 /* The one who calls us should check for this, but lets be sure... */
533 pdev = vga_default_device();
536 spin_lock_irqsave(&vga_lock, flags);
537 vgadev = vgadev_find(pdev);
540 __vga_put(vgadev, rsrc);
542 spin_unlock_irqrestore(&vga_lock, flags);
544 EXPORT_SYMBOL(vga_put);
546 static bool vga_is_firmware_default(struct pci_dev *pdev)
548 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
549 u64 base = screen_info.lfb_base;
550 u64 size = screen_info.lfb_size;
552 resource_size_t start, end;
556 /* Select the device owning the boot framebuffer if there is one */
558 if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
559 base |= (u64)screen_info.ext_lfb_base << 32;
563 /* Does firmware framebuffer belong to us? */
564 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
565 flags = pci_resource_flags(pdev, i);
567 if ((flags & IORESOURCE_MEM) == 0)
570 start = pci_resource_start(pdev, i);
571 end = pci_resource_end(pdev, i);
576 if (base < start || limit >= end)
585 static bool vga_arb_integrated_gpu(struct device *dev)
587 #if defined(CONFIG_ACPI)
588 struct acpi_device *adev = ACPI_COMPANION(dev);
590 return adev && !strcmp(acpi_device_hid(adev), ACPI_VIDEO_HID);
597 * Return true if vgadev is a better default VGA device than the best one
600 static bool vga_is_boot_device(struct vga_device *vgadev)
602 struct vga_device *boot_vga = vgadev_find(vga_default_device());
603 struct pci_dev *pdev = vgadev->pdev;
607 * We select the default VGA device in this order:
608 * Firmware framebuffer (see vga_arb_select_default_device())
609 * Legacy VGA device (owns VGA_RSRC_LEGACY_MASK)
610 * Non-legacy integrated device (see vga_arb_select_default_device())
611 * Non-legacy discrete device (see vga_arb_select_default_device())
612 * Other device (see vga_arb_select_default_device())
616 * We always prefer a firmware default device, so if we've already
617 * found one, there's no need to consider vgadev.
619 if (boot_vga && boot_vga->is_firmware_default)
622 if (vga_is_firmware_default(pdev)) {
623 vgadev->is_firmware_default = true;
628 * A legacy VGA device has MEM and IO enabled and any bridges
629 * leading to it have PCI_BRIDGE_CTL_VGA enabled so the legacy
630 * resources ([mem 0xa0000-0xbffff], [io 0x3b0-0x3bb], etc) are
633 * We use the first one we find, so if we've already found one,
634 * vgadev is no better.
637 (boot_vga->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)
640 if ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)
644 * If we haven't found a legacy VGA device, accept a non-legacy
645 * device. It may have either IO or MEM enabled, and bridges may
646 * not have PCI_BRIDGE_CTL_VGA enabled, so it may not be able to
647 * use legacy VGA resources. Prefer an integrated GPU over others.
649 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
650 if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
653 * An integrated GPU overrides a previous non-legacy
654 * device. We expect only a single integrated GPU, but if
655 * there are more, we use the *last* because that was the
658 if (vga_arb_integrated_gpu(&pdev->dev))
662 * We prefer the first non-legacy discrete device we find.
663 * If we already found one, vgadev is no better.
666 pci_read_config_word(boot_vga->pdev, PCI_COMMAND,
668 if (boot_cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
675 * vgadev has neither IO nor MEM enabled. If we haven't found any
676 * other VGA devices, it is the best candidate so far.
685 * Rules for using a bridge to control a VGA descendant decoding: if a bridge
686 * has only one VGA descendant then it can be used to control the VGA routing
687 * for that device. It should always use the bridge closest to the device to
688 * control it. If a bridge has a direct VGA descendant, but also have a sub-
689 * bridge VGA descendant then we cannot use that bridge to control the direct
690 * VGA descendant. So for every device we register, we need to iterate all
691 * its parent bridges so we can invalidate any devices using them properly.
693 static void vga_arbiter_check_bridge_sharing(struct vga_device *vgadev)
695 struct vga_device *same_bridge_vgadev;
696 struct pci_bus *new_bus, *bus;
697 struct pci_dev *new_bridge, *bridge;
699 vgadev->bridge_has_one_vga = true;
701 if (list_empty(&vga_list)) {
702 vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n");
706 /* okay iterate the new devices bridge hierarachy */
707 new_bus = vgadev->pdev->bus;
709 new_bridge = new_bus->self;
711 /* go through list of devices already registered */
712 list_for_each_entry(same_bridge_vgadev, &vga_list, list) {
713 bus = same_bridge_vgadev->pdev->bus;
716 /* see if the share a bridge with this device */
717 if (new_bridge == bridge) {
719 * If their direct parent bridge is the same
720 * as any bridge of this device then it can't
721 * be used for that device.
723 same_bridge_vgadev->bridge_has_one_vga = false;
727 * Now iterate the previous devices bridge hierarchy.
728 * If the new devices parent bridge is in the other
729 * devices hierarchy then we can't use it to control
735 if (bridge && bridge == vgadev->pdev->bus->self)
736 vgadev->bridge_has_one_vga = false;
741 new_bus = new_bus->parent;
744 if (vgadev->bridge_has_one_vga)
745 vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n");
747 vgaarb_info(&vgadev->pdev->dev, "no bridge control possible\n");
751 * Currently, we assume that the "initial" setup of the system is
752 * not sane, that is we come up with conflicting devices and let
753 * the arbiter's client decides if devices decodes or not legacy
756 static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
758 struct vga_device *vgadev;
761 struct pci_dev *bridge;
764 /* Only deal with VGA class devices */
765 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
768 /* Allocate structure */
769 vgadev = kzalloc(sizeof(struct vga_device), GFP_KERNEL);
770 if (vgadev == NULL) {
771 vgaarb_err(&pdev->dev, "failed to allocate VGA arbiter data\n");
773 * What to do on allocation failure ? For now, let's just do
774 * nothing, I'm not sure there is anything saner to be done.
779 /* Take lock & check for duplicates */
780 spin_lock_irqsave(&vga_lock, flags);
781 if (vgadev_find(pdev) != NULL) {
787 /* By default, assume we decode everything */
788 vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
789 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
791 /* by default mark it as decoding */
793 /* Mark that we "own" resources based on our enables, we will
794 * clear that below if the bridge isn't forwarding
796 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
797 if (cmd & PCI_COMMAND_IO)
798 vgadev->owns |= VGA_RSRC_LEGACY_IO;
799 if (cmd & PCI_COMMAND_MEMORY)
800 vgadev->owns |= VGA_RSRC_LEGACY_MEM;
802 /* Check if VGA cycles can get down to us */
809 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &l);
810 if (!(l & PCI_BRIDGE_CTL_VGA)) {
818 if (vga_is_boot_device(vgadev)) {
819 vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
820 vga_default_device() ?
821 " (overriding previous)" : "");
822 vga_set_default_device(pdev);
825 vga_arbiter_check_bridge_sharing(vgadev);
827 /* Add to the list */
828 list_add_tail(&vgadev->list, &vga_list);
830 vgaarb_info(&pdev->dev, "VGA device added: decodes=%s,owns=%s,locks=%s\n",
831 vga_iostate_to_str(vgadev->decodes),
832 vga_iostate_to_str(vgadev->owns),
833 vga_iostate_to_str(vgadev->locks));
835 spin_unlock_irqrestore(&vga_lock, flags);
838 spin_unlock_irqrestore(&vga_lock, flags);
843 static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
845 struct vga_device *vgadev;
849 spin_lock_irqsave(&vga_lock, flags);
850 vgadev = vgadev_find(pdev);
851 if (vgadev == NULL) {
856 if (vga_default == pdev)
857 vga_set_default_device(NULL);
859 if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
862 /* Remove entry from list */
863 list_del(&vgadev->list);
866 /* Wake up all possible waiters */
867 wake_up_all(&vga_wait_queue);
869 spin_unlock_irqrestore(&vga_lock, flags);
874 /* this is called with the lock */
875 static inline void vga_update_device_decodes(struct vga_device *vgadev,
878 struct device *dev = &vgadev->pdev->dev;
879 int old_decodes, decodes_removed, decodes_unlocked;
881 old_decodes = vgadev->decodes;
882 decodes_removed = ~new_decodes & old_decodes;
883 decodes_unlocked = vgadev->locks & decodes_removed;
884 vgadev->decodes = new_decodes;
886 vgaarb_info(dev, "changed VGA decodes: olddecodes=%s,decodes=%s:owns=%s\n",
887 vga_iostate_to_str(old_decodes),
888 vga_iostate_to_str(vgadev->decodes),
889 vga_iostate_to_str(vgadev->owns));
891 /* if we removed locked decodes, lock count goes to zero, and release */
892 if (decodes_unlocked) {
893 if (decodes_unlocked & VGA_RSRC_LEGACY_IO)
894 vgadev->io_lock_cnt = 0;
895 if (decodes_unlocked & VGA_RSRC_LEGACY_MEM)
896 vgadev->mem_lock_cnt = 0;
897 __vga_put(vgadev, decodes_unlocked);
900 /* change decodes counter */
901 if (old_decodes & VGA_RSRC_LEGACY_MASK &&
902 !(new_decodes & VGA_RSRC_LEGACY_MASK))
904 if (!(old_decodes & VGA_RSRC_LEGACY_MASK) &&
905 new_decodes & VGA_RSRC_LEGACY_MASK)
907 vgaarb_dbg(dev, "decoding count now is: %d\n", vga_decode_count);
910 static void __vga_set_legacy_decoding(struct pci_dev *pdev,
911 unsigned int decodes,
914 struct vga_device *vgadev;
917 decodes &= VGA_RSRC_LEGACY_MASK;
919 spin_lock_irqsave(&vga_lock, flags);
920 vgadev = vgadev_find(pdev);
924 /* don't let userspace futz with kernel driver decodes */
925 if (userspace && vgadev->set_decode)
928 /* update the device decodes + counter */
929 vga_update_device_decodes(vgadev, decodes);
931 /* XXX if somebody is going from "doesn't decode" to "decodes" state
932 * here, additional care must be taken as we may have pending owner
933 * ship of non-legacy region ...
936 spin_unlock_irqrestore(&vga_lock, flags);
940 * vga_set_legacy_decoding
941 * @pdev: pci device of the VGA card
942 * @decodes: bit mask of what legacy regions the card decodes
944 * Indicates to the arbiter if the card decodes legacy VGA IOs, legacy VGA
945 * Memory, both, or none. All cards default to both, the card driver (fbdev for
946 * example) should tell the arbiter if it has disabled legacy decoding, so the
947 * card can be left out of the arbitration process (and can be safe to take
948 * interrupts at any time.
950 void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
952 __vga_set_legacy_decoding(pdev, decodes, false);
954 EXPORT_SYMBOL(vga_set_legacy_decoding);
957 * vga_client_register - register or unregister a VGA arbitration client
958 * @pdev: pci device of the VGA client
959 * @set_decode: vga decode change callback
961 * Clients have two callback mechanisms they can use.
963 * @set_decode callback: If a client can disable its GPU VGA resource, it
964 * will get a callback from this to set the encode/decode state.
966 * Rationale: we cannot disable VGA decode resources unconditionally some single
967 * GPU laptops seem to require ACPI or BIOS access to the VGA registers to
968 * control things like backlights etc. Hopefully newer multi-GPU laptops do
969 * something saner, and desktops won't have any special ACPI for this. The
970 * driver will get a callback when VGA arbitration is first used by userspace
971 * since some older X servers have issues.
973 * This function does not check whether a client for @pdev has been registered
976 * To unregister just call vga_client_unregister().
978 * Returns: 0 on success, -1 on failure
980 int vga_client_register(struct pci_dev *pdev,
981 unsigned int (*set_decode)(struct pci_dev *pdev, bool decode))
984 struct vga_device *vgadev;
987 spin_lock_irqsave(&vga_lock, flags);
988 vgadev = vgadev_find(pdev);
992 vgadev->set_decode = set_decode;
996 spin_unlock_irqrestore(&vga_lock, flags);
1000 EXPORT_SYMBOL(vga_client_register);
1003 * Char driver implementation
1007 * open : open user instance of the arbitrer. by default, it's
1008 * attached to the default VGA device of the system.
1010 * close : close user instance, release locks
1012 * read : return a string indicating the status of the target.
1013 * an IO state string is of the form {io,mem,io+mem,none},
1014 * mc and ic are respectively mem and io lock counts (for
1015 * debugging/diagnostic only). "decodes" indicate what the
1016 * card currently decodes, "owns" indicates what is currently
1017 * enabled on it, and "locks" indicates what is locked by this
1018 * card. If the card is unplugged, we get "invalid" then for
1019 * card_ID and an -ENODEV error is returned for any command
1020 * until a new card is targeted
1022 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
1024 * write : write a command to the arbiter. List of commands is:
1026 * target <card_ID> : switch target to card <card_ID> (see below)
1027 * lock <io_state> : acquires locks on target ("none" is invalid io_state)
1028 * trylock <io_state> : non-blocking acquire locks on target
1029 * unlock <io_state> : release locks on target
1030 * unlock all : release all locks on target held by this user
1031 * decodes <io_state> : set the legacy decoding attributes for the card
1033 * poll : event if something change on any card (not just the target)
1035 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
1036 * to go back to the system default card (TODO: not implemented yet).
1037 * Currently, only PCI is supported as a prefix, but the userland API may
1038 * support other bus types in the future, even if the current kernel
1039 * implementation doesn't.
1043 * The driver keeps track of which user has what locks on which card. It
1044 * supports stacking, like the kernel one. This complexifies the implementation
1045 * a bit, but makes the arbiter more tolerant to userspace problems and able
1046 * to properly cleanup in all cases when a process dies.
1047 * Currently, a max of 16 cards simultaneously can have locks issued from
1048 * userspace for a given user (file descriptor instance) of the arbiter.
1050 * If the device is hot-unplugged, there is a hook inside the module to notify
1051 * they being added/removed in the system and automatically added/removed in
1055 #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
1056 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
1059 * Each user has an array of these, tracking which cards have locks
1061 struct vga_arb_user_card {
1062 struct pci_dev *pdev;
1063 unsigned int mem_cnt;
1064 unsigned int io_cnt;
1067 struct vga_arb_private {
1068 struct list_head list;
1069 struct pci_dev *target;
1070 struct vga_arb_user_card cards[MAX_USER_CARDS];
1074 static LIST_HEAD(vga_user_list);
1075 static DEFINE_SPINLOCK(vga_user_lock);
1079 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
1080 * returns the respective values. If the string is not in this format,
1083 static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
1084 unsigned int *bus, unsigned int *devfn)
1087 unsigned int slot, func;
1090 n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
1094 *devfn = PCI_DEVFN(slot, func);
1099 static ssize_t vga_arb_read(struct file *file, char __user *buf,
1100 size_t count, loff_t *ppos)
1102 struct vga_arb_private *priv = file->private_data;
1103 struct vga_device *vgadev;
1104 struct pci_dev *pdev;
1105 unsigned long flags;
1110 lbuf = kmalloc(1024, GFP_KERNEL);
1114 /* Protects vga_list */
1115 spin_lock_irqsave(&vga_lock, flags);
1117 /* If we are targeting the default, use it */
1118 pdev = priv->target;
1119 if (pdev == NULL || pdev == PCI_INVALID_CARD) {
1120 spin_unlock_irqrestore(&vga_lock, flags);
1121 len = sprintf(lbuf, "invalid");
1125 /* Find card vgadev structure */
1126 vgadev = vgadev_find(pdev);
1127 if (vgadev == NULL) {
1128 /* Wow, it's not in the list, that shouldn't happen,
1129 * let's fix us up and return invalid card
1131 spin_unlock_irqrestore(&vga_lock, flags);
1132 len = sprintf(lbuf, "invalid");
1136 /* Fill the buffer with infos */
1137 len = snprintf(lbuf, 1024,
1138 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%u:%u)\n",
1139 vga_decode_count, pci_name(pdev),
1140 vga_iostate_to_str(vgadev->decodes),
1141 vga_iostate_to_str(vgadev->owns),
1142 vga_iostate_to_str(vgadev->locks),
1143 vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
1145 spin_unlock_irqrestore(&vga_lock, flags);
1148 /* Copy that to user */
1151 rc = copy_to_user(buf, lbuf, len);
1159 * TODO: To avoid parsing inside kernel and to improve the speed we may
1160 * consider use ioctl here
1162 static ssize_t vga_arb_write(struct file *file, const char __user *buf,
1163 size_t count, loff_t *ppos)
1165 struct vga_arb_private *priv = file->private_data;
1166 struct vga_arb_user_card *uc = NULL;
1167 struct pci_dev *pdev;
1169 unsigned int io_state;
1171 char kbuf[64], *curr_pos;
1172 size_t remaining = count;
1177 if (count >= sizeof(kbuf))
1179 if (copy_from_user(kbuf, buf, count))
1182 kbuf[count] = '\0'; /* Just to make sure... */
1184 if (strncmp(curr_pos, "lock ", 5) == 0) {
1188 pr_debug("client 0x%p called 'lock'\n", priv);
1190 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1194 if (io_state == VGA_RSRC_NONE) {
1199 pdev = priv->target;
1200 if (priv->target == NULL) {
1205 vga_get_uninterruptible(pdev, io_state);
1207 /* Update the client's locks lists... */
1208 for (i = 0; i < MAX_USER_CARDS; i++) {
1209 if (priv->cards[i].pdev == pdev) {
1210 if (io_state & VGA_RSRC_LEGACY_IO)
1211 priv->cards[i].io_cnt++;
1212 if (io_state & VGA_RSRC_LEGACY_MEM)
1213 priv->cards[i].mem_cnt++;
1220 } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
1224 pr_debug("client 0x%p called 'unlock'\n", priv);
1226 if (strncmp(curr_pos, "all", 3) == 0)
1227 io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
1229 if (!vga_str_to_iostate
1230 (curr_pos, remaining, &io_state)) {
1235 if (io_state == VGA_RSRC_NONE) {
1242 pdev = priv->target;
1243 if (priv->target == NULL) {
1247 for (i = 0; i < MAX_USER_CARDS; i++) {
1248 if (priv->cards[i].pdev == pdev)
1249 uc = &priv->cards[i];
1257 if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
1262 if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
1267 vga_put(pdev, io_state);
1269 if (io_state & VGA_RSRC_LEGACY_IO)
1271 if (io_state & VGA_RSRC_LEGACY_MEM)
1276 } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
1280 pr_debug("client 0x%p called 'trylock'\n", priv);
1282 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1287 if (io_state == VGA_RSRC_NONE) {
1293 pdev = priv->target;
1294 if (priv->target == NULL) {
1299 if (vga_tryget(pdev, io_state)) {
1300 /* Update the client's locks lists... */
1301 for (i = 0; i < MAX_USER_CARDS; i++) {
1302 if (priv->cards[i].pdev == pdev) {
1303 if (io_state & VGA_RSRC_LEGACY_IO)
1304 priv->cards[i].io_cnt++;
1305 if (io_state & VGA_RSRC_LEGACY_MEM)
1306 priv->cards[i].mem_cnt++;
1317 } else if (strncmp(curr_pos, "target ", 7) == 0) {
1318 unsigned int domain, bus, devfn;
1319 struct vga_device *vgadev;
1323 pr_debug("client 0x%p called 'target'\n", priv);
1324 /* if target is default */
1325 if (!strncmp(curr_pos, "default", 7))
1326 pdev = pci_dev_get(vga_default_device());
1328 if (!vga_pci_str_to_vars(curr_pos, remaining,
1329 &domain, &bus, &devfn)) {
1333 pdev = pci_get_domain_bus_and_slot(domain, bus, devfn);
1335 pr_debug("invalid PCI address %04x:%02x:%02x.%x\n",
1336 domain, bus, PCI_SLOT(devfn),
1342 pr_debug("%s ==> %04x:%02x:%02x.%x pdev %p\n", curr_pos,
1343 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
1347 vgadev = vgadev_find(pdev);
1348 pr_debug("vgadev %p\n", vgadev);
1349 if (vgadev == NULL) {
1351 vgaarb_dbg(&pdev->dev, "not a VGA device\n");
1359 priv->target = pdev;
1360 for (i = 0; i < MAX_USER_CARDS; i++) {
1361 if (priv->cards[i].pdev == pdev)
1363 if (priv->cards[i].pdev == NULL) {
1364 priv->cards[i].pdev = pdev;
1365 priv->cards[i].io_cnt = 0;
1366 priv->cards[i].mem_cnt = 0;
1370 if (i == MAX_USER_CARDS) {
1371 vgaarb_dbg(&pdev->dev, "maximum user cards (%d) number reached, ignoring this one!\n",
1374 /* XXX: which value to return? */
1384 } else if (strncmp(curr_pos, "decodes ", 8) == 0) {
1387 pr_debug("client 0x%p called 'decodes'\n", priv);
1389 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1393 pdev = priv->target;
1394 if (priv->target == NULL) {
1399 __vga_set_legacy_decoding(pdev, io_state, true);
1403 /* If we got here, the message written is not part of the protocol! */
1410 static __poll_t vga_arb_fpoll(struct file *file, poll_table *wait)
1412 pr_debug("%s\n", __func__);
1414 poll_wait(file, &vga_wait_queue, wait);
1418 static int vga_arb_open(struct inode *inode, struct file *file)
1420 struct vga_arb_private *priv;
1421 unsigned long flags;
1423 pr_debug("%s\n", __func__);
1425 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1428 spin_lock_init(&priv->lock);
1429 file->private_data = priv;
1431 spin_lock_irqsave(&vga_user_lock, flags);
1432 list_add(&priv->list, &vga_user_list);
1433 spin_unlock_irqrestore(&vga_user_lock, flags);
1435 /* Set the client' lists of locks */
1436 priv->target = vga_default_device(); /* Maybe this is still null! */
1437 priv->cards[0].pdev = priv->target;
1438 priv->cards[0].io_cnt = 0;
1439 priv->cards[0].mem_cnt = 0;
1445 static int vga_arb_release(struct inode *inode, struct file *file)
1447 struct vga_arb_private *priv = file->private_data;
1448 struct vga_arb_user_card *uc;
1449 unsigned long flags;
1452 pr_debug("%s\n", __func__);
1454 spin_lock_irqsave(&vga_user_lock, flags);
1455 list_del(&priv->list);
1456 for (i = 0; i < MAX_USER_CARDS; i++) {
1457 uc = &priv->cards[i];
1458 if (uc->pdev == NULL)
1460 vgaarb_dbg(&uc->pdev->dev, "uc->io_cnt == %d, uc->mem_cnt == %d\n",
1461 uc->io_cnt, uc->mem_cnt);
1462 while (uc->io_cnt--)
1463 vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
1464 while (uc->mem_cnt--)
1465 vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
1467 spin_unlock_irqrestore(&vga_user_lock, flags);
1475 * callback any registered clients to let them know we have a
1476 * change in VGA cards
1478 static void vga_arbiter_notify_clients(void)
1480 struct vga_device *vgadev;
1481 unsigned long flags;
1482 uint32_t new_decodes;
1485 if (!vga_arbiter_used)
1488 spin_lock_irqsave(&vga_lock, flags);
1489 list_for_each_entry(vgadev, &vga_list, list) {
1494 if (vgadev->set_decode) {
1495 new_decodes = vgadev->set_decode(vgadev->pdev,
1497 vga_update_device_decodes(vgadev, new_decodes);
1500 spin_unlock_irqrestore(&vga_lock, flags);
1503 static int pci_notify(struct notifier_block *nb, unsigned long action,
1506 struct device *dev = data;
1507 struct pci_dev *pdev = to_pci_dev(dev);
1508 bool notify = false;
1510 vgaarb_dbg(dev, "%s\n", __func__);
1512 /* For now we're only intereted in devices added and removed. I didn't
1513 * test this thing here, so someone needs to double check for the
1514 * cases of hotplugable vga cards. */
1515 if (action == BUS_NOTIFY_ADD_DEVICE)
1516 notify = vga_arbiter_add_pci_device(pdev);
1517 else if (action == BUS_NOTIFY_DEL_DEVICE)
1518 notify = vga_arbiter_del_pci_device(pdev);
1521 vga_arbiter_notify_clients();
1525 static struct notifier_block pci_notifier = {
1526 .notifier_call = pci_notify,
1529 static const struct file_operations vga_arb_device_fops = {
1530 .read = vga_arb_read,
1531 .write = vga_arb_write,
1532 .poll = vga_arb_fpoll,
1533 .open = vga_arb_open,
1534 .release = vga_arb_release,
1535 .llseek = noop_llseek,
1538 static struct miscdevice vga_arb_device = {
1539 MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
1542 static int __init vga_arb_device_init(void)
1545 struct pci_dev *pdev;
1547 rc = misc_register(&vga_arb_device);
1549 pr_err("error %d registering device\n", rc);
1551 bus_register_notifier(&pci_bus_type, &pci_notifier);
1553 /* We add all PCI devices satisfying VGA class in the arbiter by
1557 pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
1558 PCI_ANY_ID, pdev)) != NULL)
1559 vga_arbiter_add_pci_device(pdev);
1561 pr_info("loaded\n");
1564 subsys_initcall_sync(vga_arb_device_init);