1 // SPDX-License-Identifier: MIT
3 * vgaarb.c: Implements 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>
33 #include <linux/uaccess.h>
34 #include <linux/vgaarb.h>
36 static void vga_arbiter_notify_clients(void);
39 * We keep a list of all VGA devices in the system to speed
40 * up the various operations of the arbiter
43 struct list_head list
;
45 unsigned int decodes
; /* what it decodes */
46 unsigned int owns
; /* what it owns */
47 unsigned int locks
; /* what it locks */
48 unsigned int io_lock_cnt
; /* legacy IO lock count */
49 unsigned int mem_lock_cnt
; /* legacy MEM lock count */
50 unsigned int io_norm_cnt
; /* normal IO count */
51 unsigned int mem_norm_cnt
; /* normal MEM count */
52 bool bridge_has_one_vga
;
53 bool is_firmware_default
; /* device selected by firmware */
54 unsigned int (*set_decode
)(struct pci_dev
*pdev
, bool decode
);
57 static LIST_HEAD(vga_list
);
58 static int vga_count
, vga_decode_count
;
59 static bool vga_arbiter_used
;
60 static DEFINE_SPINLOCK(vga_lock
);
61 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue
);
63 static const char *vga_iostate_to_str(unsigned int iostate
)
65 /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
66 iostate
&= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
68 case VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
:
70 case VGA_RSRC_LEGACY_IO
:
72 case VGA_RSRC_LEGACY_MEM
:
78 static int vga_str_to_iostate(char *buf
, int str_size
, unsigned int *io_state
)
81 * In theory, we could hand out locks on IO and MEM separately to
82 * userspace, but this can cause deadlocks.
84 if (strncmp(buf
, "none", 4) == 0) {
85 *io_state
= VGA_RSRC_NONE
;
89 /* XXX We're not checking 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 as 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 is
120 * rather dumb and will probably only work properly on single VGA card
121 * setups and/or x86 platforms.
123 * If your VGA default device is not PCI, you'll have to return NULL here.
124 * In this case, I assume it will not conflict with any PCI card. If this
125 * is not true, I'll have to define two arch hooks for enabling/disabling
126 * the VGA default device if that is possible. This may be a problem with
127 * real _ISA_ VGA cards, in addition to a PCI one. I don't know at this
128 * point how to deal with that card. Can their IOs be disabled at all? If
129 * not, then I suppose it's a matter of having the proper arch hook telling
130 * us about it, so we basically never allow anybody to succeed a vga_get().
132 struct pci_dev
*vga_default_device(void)
136 EXPORT_SYMBOL_GPL(vga_default_device
);
138 void vga_set_default_device(struct pci_dev
*pdev
)
140 if (vga_default
== pdev
)
143 pci_dev_put(vga_default
);
144 vga_default
= pci_dev_get(pdev
);
148 * vga_remove_vgacon - deactivate VGA console
150 * Unbind and unregister vgacon in case pdev is the default VGA device.
151 * Can be called by GPU drivers on initialization to make sure VGA register
152 * access done by vgacon will not disturb the device.
156 #if !defined(CONFIG_VGA_CONSOLE)
157 int vga_remove_vgacon(struct pci_dev
*pdev
)
161 #elif !defined(CONFIG_DUMMY_CONSOLE)
162 int vga_remove_vgacon(struct pci_dev
*pdev
)
167 int vga_remove_vgacon(struct pci_dev
*pdev
)
171 if (pdev
!= vga_default
)
173 vgaarb_info(&pdev
->dev
, "deactivate vga console\n");
176 if (con_is_bound(&vga_con
))
177 ret
= do_take_over_console(&dummy_con
, 0,
178 MAX_NR_CONSOLES
- 1, 1);
180 ret
= do_unregister_con_driver(&vga_con
);
182 /* Ignore "already unregistered". */
191 EXPORT_SYMBOL(vga_remove_vgacon
);
194 * If we don't ever use VGA arbitration, we should avoid turning off
195 * anything anywhere due to old X servers getting confused about the boot
196 * device not being VGA.
198 static void vga_check_first_use(void)
201 * Inform all GPUs in the system that VGA arbitration has occurred
202 * so they can disable resources if possible.
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
;
220 * Account for "normal" resources to lock. If we decode the legacy,
221 * counterpart, we need to request it as well
223 if ((rsrc
& VGA_RSRC_NORMAL_IO
) &&
224 (vgadev
->decodes
& VGA_RSRC_LEGACY_IO
))
225 rsrc
|= VGA_RSRC_LEGACY_IO
;
226 if ((rsrc
& VGA_RSRC_NORMAL_MEM
) &&
227 (vgadev
->decodes
& VGA_RSRC_LEGACY_MEM
))
228 rsrc
|= VGA_RSRC_LEGACY_MEM
;
230 vgaarb_dbg(dev
, "%s: %d\n", __func__
, rsrc
);
231 vgaarb_dbg(dev
, "%s: owns: %d\n", __func__
, vgadev
->owns
);
233 /* Check what resources we need to acquire */
234 wants
= rsrc
& ~vgadev
->owns
;
236 /* We already own everything, just mark locked & bye bye */
241 * We don't need to request a legacy resource, we just enable
242 * appropriate decoding and go.
244 legacy_wants
= wants
& VGA_RSRC_LEGACY_MASK
;
245 if (legacy_wants
== 0)
248 /* Ok, we don't, let's find out who we need to kick off */
249 list_for_each_entry(conflict
, &vga_list
, list
) {
250 unsigned int lwants
= legacy_wants
;
251 unsigned int change_bridge
= 0;
253 /* Don't conflict with myself */
254 if (vgadev
== conflict
)
258 * We have a possible conflict. Before we go further, we must
259 * check if we sit on the same bus as the conflicting device.
260 * If we don't, then we must tie both IO and MEM resources
261 * together since there is only a single bit controlling
262 * VGA forwarding on P2P bridges.
264 if (vgadev
->pdev
->bus
!= conflict
->pdev
->bus
) {
266 lwants
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
270 * Check if the guy has a lock on the resource. If he does,
271 * return the conflicting entry.
273 if (conflict
->locks
& lwants
)
277 * Ok, now check if it owns the resource we want. We can
278 * lock resources that are not decoded; therefore a device
279 * can own resources it doesn't decode.
281 match
= lwants
& conflict
->owns
;
286 * Looks like he doesn't have a lock, we can steal them
294 * If we can't control legacy resources via the bridge, we
295 * also need to disable normal decoding.
297 if (!conflict
->bridge_has_one_vga
) {
298 if ((match
& conflict
->decodes
) & VGA_RSRC_LEGACY_MEM
)
299 pci_bits
|= PCI_COMMAND_MEMORY
;
300 if ((match
& conflict
->decodes
) & VGA_RSRC_LEGACY_IO
)
301 pci_bits
|= PCI_COMMAND_IO
;
304 flags
|= PCI_VGA_STATE_CHANGE_DECODES
;
308 flags
|= PCI_VGA_STATE_CHANGE_BRIDGE
;
310 pci_set_vga_state(conflict
->pdev
, false, pci_bits
, flags
);
311 conflict
->owns
&= ~match
;
313 /* If we disabled normal decoding, reflect it in owns */
314 if (pci_bits
& PCI_COMMAND_MEMORY
)
315 conflict
->owns
&= ~VGA_RSRC_NORMAL_MEM
;
316 if (pci_bits
& PCI_COMMAND_IO
)
317 conflict
->owns
&= ~VGA_RSRC_NORMAL_IO
;
322 * Ok, we got it, everybody conflicting has been disabled, let's
323 * enable us. Mark any bits in "owns" regardless of whether we
324 * decoded them. We can lock resources we don't decode, therefore
325 * we must track them via "owns".
330 if (!vgadev
->bridge_has_one_vga
) {
331 flags
|= PCI_VGA_STATE_CHANGE_DECODES
;
332 if (wants
& (VGA_RSRC_LEGACY_MEM
|VGA_RSRC_NORMAL_MEM
))
333 pci_bits
|= PCI_COMMAND_MEMORY
;
334 if (wants
& (VGA_RSRC_LEGACY_IO
|VGA_RSRC_NORMAL_IO
))
335 pci_bits
|= PCI_COMMAND_IO
;
337 if (wants
& VGA_RSRC_LEGACY_MASK
)
338 flags
|= PCI_VGA_STATE_CHANGE_BRIDGE
;
340 pci_set_vga_state(vgadev
->pdev
, true, pci_bits
, flags
);
342 vgadev
->owns
|= wants
;
344 vgadev
->locks
|= (rsrc
& VGA_RSRC_LEGACY_MASK
);
345 if (rsrc
& VGA_RSRC_LEGACY_IO
)
346 vgadev
->io_lock_cnt
++;
347 if (rsrc
& VGA_RSRC_LEGACY_MEM
)
348 vgadev
->mem_lock_cnt
++;
349 if (rsrc
& VGA_RSRC_NORMAL_IO
)
350 vgadev
->io_norm_cnt
++;
351 if (rsrc
& VGA_RSRC_NORMAL_MEM
)
352 vgadev
->mem_norm_cnt
++;
357 static void __vga_put(struct vga_device
*vgadev
, unsigned int rsrc
)
359 struct device
*dev
= &vgadev
->pdev
->dev
;
360 unsigned int old_locks
= vgadev
->locks
;
362 vgaarb_dbg(dev
, "%s\n", __func__
);
365 * Update our counters and account for equivalent legacy resources
368 if ((rsrc
& VGA_RSRC_NORMAL_IO
) && vgadev
->io_norm_cnt
> 0) {
369 vgadev
->io_norm_cnt
--;
370 if (vgadev
->decodes
& VGA_RSRC_LEGACY_IO
)
371 rsrc
|= VGA_RSRC_LEGACY_IO
;
373 if ((rsrc
& VGA_RSRC_NORMAL_MEM
) && vgadev
->mem_norm_cnt
> 0) {
374 vgadev
->mem_norm_cnt
--;
375 if (vgadev
->decodes
& VGA_RSRC_LEGACY_MEM
)
376 rsrc
|= VGA_RSRC_LEGACY_MEM
;
378 if ((rsrc
& VGA_RSRC_LEGACY_IO
) && vgadev
->io_lock_cnt
> 0)
379 vgadev
->io_lock_cnt
--;
380 if ((rsrc
& VGA_RSRC_LEGACY_MEM
) && vgadev
->mem_lock_cnt
> 0)
381 vgadev
->mem_lock_cnt
--;
384 * Just clear lock bits, we do lazy operations so we don't really
385 * have to bother about anything else at this point.
387 if (vgadev
->io_lock_cnt
== 0)
388 vgadev
->locks
&= ~VGA_RSRC_LEGACY_IO
;
389 if (vgadev
->mem_lock_cnt
== 0)
390 vgadev
->locks
&= ~VGA_RSRC_LEGACY_MEM
;
393 * Kick the wait queue in case somebody was waiting if we actually
394 * released something.
396 if (old_locks
!= vgadev
->locks
)
397 wake_up_all(&vga_wait_queue
);
401 * vga_get - acquire & lock VGA resources
402 * @pdev: PCI device of the VGA card or NULL for the system default
403 * @rsrc: bit mask of resources to acquire and lock
404 * @interruptible: blocking should be interruptible by signals ?
406 * Acquire VGA resources for the given card and mark those resources
407 * locked. If the resources requested are "normal" (and not legacy)
408 * resources, the arbiter will first check whether the card is doing legacy
409 * decoding for that type of resource. If yes, the lock is "converted" into
410 * a legacy resource lock.
412 * The arbiter will first look for all VGA cards that might conflict and disable
413 * their IOs and/or Memory access, including VGA forwarding on P2P bridges if
414 * necessary, so that the requested resources can be used. Then, the card is
415 * marked as locking these resources and the IO and/or Memory accesses are
416 * enabled on the card (including VGA forwarding on parent P2P bridges if any).
418 * This function will block if some conflicting card is already locking one of
419 * the required resources (or any resource on a different bus segment, since P2P
420 * bridges don't differentiate VGA memory and IO afaik). You can indicate
421 * whether this blocking should be interruptible by a signal (for userland
424 * Must not be called at interrupt time or in atomic context. If the card
425 * already owns the resources, the function succeeds. Nested calls are
426 * supported (a per-resource counter is maintained)
428 * On success, release the VGA resource again with vga_put().
432 * 0 on success, negative error code on failure.
434 int vga_get(struct pci_dev
*pdev
, unsigned int rsrc
, int interruptible
)
436 struct vga_device
*vgadev
, *conflict
;
438 wait_queue_entry_t wait
;
441 vga_check_first_use();
442 /* The caller should check for this, but let's be sure */
444 pdev
= vga_default_device();
449 spin_lock_irqsave(&vga_lock
, flags
);
450 vgadev
= vgadev_find(pdev
);
451 if (vgadev
== NULL
) {
452 spin_unlock_irqrestore(&vga_lock
, flags
);
456 conflict
= __vga_tryget(vgadev
, rsrc
);
457 spin_unlock_irqrestore(&vga_lock
, flags
);
458 if (conflict
== NULL
)
462 * We have a conflict; we wait until somebody kicks the
463 * work queue. Currently we have one work queue that we
464 * kick each time some resources are released, but it would
465 * be fairly easy to have a per-device one so that we only
466 * need to attach to the conflicting device.
468 init_waitqueue_entry(&wait
, current
);
469 add_wait_queue(&vga_wait_queue
, &wait
);
470 set_current_state(interruptible
?
472 TASK_UNINTERRUPTIBLE
);
473 if (interruptible
&& signal_pending(current
)) {
474 __set_current_state(TASK_RUNNING
);
475 remove_wait_queue(&vga_wait_queue
, &wait
);
480 remove_wait_queue(&vga_wait_queue
, &wait
);
484 EXPORT_SYMBOL(vga_get
);
487 * vga_tryget - try to acquire & lock legacy VGA resources
488 * @pdev: PCI device of VGA card or NULL for system default
489 * @rsrc: bit mask of resources to acquire and lock
491 * Perform the same operation as vga_get(), but return an error (-EBUSY)
492 * instead of blocking if the resources are already locked by another card.
493 * Can be called in any context.
495 * On success, release the VGA resource again with vga_put().
499 * 0 on success, negative error code on failure.
501 static int vga_tryget(struct pci_dev
*pdev
, unsigned int rsrc
)
503 struct vga_device
*vgadev
;
507 vga_check_first_use();
509 /* The caller should check for this, but let's be sure */
511 pdev
= vga_default_device();
514 spin_lock_irqsave(&vga_lock
, flags
);
515 vgadev
= vgadev_find(pdev
);
516 if (vgadev
== NULL
) {
520 if (__vga_tryget(vgadev
, rsrc
))
523 spin_unlock_irqrestore(&vga_lock
, flags
);
528 * vga_put - release lock on legacy VGA resources
529 * @pdev: PCI device of VGA card or NULL for system default
530 * @rsrc: bit mask of resource to release
532 * Release resources previously locked by vga_get() or vga_tryget(). The
533 * resources aren't disabled right away, so that a subsequent vga_get() on
534 * the same card will succeed immediately. Resources have a counter, so
535 * locks are only released if the counter reaches 0.
537 void vga_put(struct pci_dev
*pdev
, unsigned int rsrc
)
539 struct vga_device
*vgadev
;
542 /* The caller should check for this, but let's be sure */
544 pdev
= vga_default_device();
547 spin_lock_irqsave(&vga_lock
, flags
);
548 vgadev
= vgadev_find(pdev
);
551 __vga_put(vgadev
, rsrc
);
553 spin_unlock_irqrestore(&vga_lock
, flags
);
555 EXPORT_SYMBOL(vga_put
);
557 static bool vga_is_firmware_default(struct pci_dev
*pdev
)
559 #if defined(CONFIG_X86)
560 u64 base
= screen_info
.lfb_base
;
561 u64 size
= screen_info
.lfb_size
;
565 /* Select the device owning the boot framebuffer if there is one */
567 if (screen_info
.capabilities
& VIDEO_CAPABILITY_64BIT_BASE
)
568 base
|= (u64
)screen_info
.ext_lfb_base
<< 32;
572 /* Does firmware framebuffer belong to us? */
573 pci_dev_for_each_resource(pdev
, r
) {
574 if (resource_type(r
) != IORESOURCE_MEM
)
577 if (!r
->start
|| !r
->end
)
580 if (base
< r
->start
|| limit
>= r
->end
)
589 static bool vga_arb_integrated_gpu(struct device
*dev
)
591 #if defined(CONFIG_ACPI)
592 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
594 return adev
&& !strcmp(acpi_device_hid(adev
), ACPI_VIDEO_HID
);
601 * Return true if vgadev is a better default VGA device than the best one
604 static bool vga_is_boot_device(struct vga_device
*vgadev
)
606 struct vga_device
*boot_vga
= vgadev_find(vga_default_device());
607 struct pci_dev
*pdev
= vgadev
->pdev
;
611 * We select the default VGA device in this order:
612 * Firmware framebuffer (see vga_arb_select_default_device())
613 * Legacy VGA device (owns VGA_RSRC_LEGACY_MASK)
614 * Non-legacy integrated device (see vga_arb_select_default_device())
615 * Non-legacy discrete device (see vga_arb_select_default_device())
616 * Other device (see vga_arb_select_default_device())
620 * We always prefer a firmware default device, so if we've already
621 * found one, there's no need to consider vgadev.
623 if (boot_vga
&& boot_vga
->is_firmware_default
)
626 if (vga_is_firmware_default(pdev
)) {
627 vgadev
->is_firmware_default
= true;
632 * A legacy VGA device has MEM and IO enabled and any bridges
633 * leading to it have PCI_BRIDGE_CTL_VGA enabled so the legacy
634 * resources ([mem 0xa0000-0xbffff], [io 0x3b0-0x3bb], etc) are
637 * We use the first one we find, so if we've already found one,
638 * vgadev is no better.
641 (boot_vga
->owns
& VGA_RSRC_LEGACY_MASK
) == VGA_RSRC_LEGACY_MASK
)
644 if ((vgadev
->owns
& VGA_RSRC_LEGACY_MASK
) == VGA_RSRC_LEGACY_MASK
)
648 * If we haven't found a legacy VGA device, accept a non-legacy
649 * device. It may have either IO or MEM enabled, and bridges may
650 * not have PCI_BRIDGE_CTL_VGA enabled, so it may not be able to
651 * use legacy VGA resources. Prefer an integrated GPU over others.
653 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
);
654 if (cmd
& (PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
)) {
657 * An integrated GPU overrides a previous non-legacy
658 * device. We expect only a single integrated GPU, but if
659 * there are more, we use the *last* because that was the
662 if (vga_arb_integrated_gpu(&pdev
->dev
))
666 * We prefer the first non-legacy discrete device we find.
667 * If we already found one, vgadev is no better.
670 pci_read_config_word(boot_vga
->pdev
, PCI_COMMAND
,
672 if (boot_cmd
& (PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
))
679 * Vgadev has neither IO nor MEM enabled. If we haven't found any
680 * other VGA devices, it is the best candidate so far.
689 * Rules for using a bridge to control a VGA descendant decoding: if a bridge
690 * has only one VGA descendant then it can be used to control the VGA routing
691 * for that device. It should always use the bridge closest to the device to
692 * control it. If a bridge has a direct VGA descendant, but also have a sub-
693 * bridge VGA descendant then we cannot use that bridge to control the direct
694 * VGA descendant. So for every device we register, we need to iterate all
695 * its parent bridges so we can invalidate any devices using them properly.
697 static void vga_arbiter_check_bridge_sharing(struct vga_device
*vgadev
)
699 struct vga_device
*same_bridge_vgadev
;
700 struct pci_bus
*new_bus
, *bus
;
701 struct pci_dev
*new_bridge
, *bridge
;
703 vgadev
->bridge_has_one_vga
= true;
705 if (list_empty(&vga_list
)) {
706 vgaarb_info(&vgadev
->pdev
->dev
, "bridge control possible\n");
710 /* Iterate the new device's bridge hierarchy */
711 new_bus
= vgadev
->pdev
->bus
;
713 new_bridge
= new_bus
->self
;
715 /* Go through list of devices already registered */
716 list_for_each_entry(same_bridge_vgadev
, &vga_list
, list
) {
717 bus
= same_bridge_vgadev
->pdev
->bus
;
720 /* See if it shares a bridge with this device */
721 if (new_bridge
== bridge
) {
723 * If its direct parent bridge is the same
724 * as any bridge of this device then it can't
725 * be used for that device.
727 same_bridge_vgadev
->bridge_has_one_vga
= false;
731 * Now iterate the previous device's bridge hierarchy.
732 * If the new device's parent bridge is in the other
733 * device's hierarchy, we can't use it to control this
739 if (bridge
&& bridge
== vgadev
->pdev
->bus
->self
)
740 vgadev
->bridge_has_one_vga
= false;
745 new_bus
= new_bus
->parent
;
748 if (vgadev
->bridge_has_one_vga
)
749 vgaarb_info(&vgadev
->pdev
->dev
, "bridge control possible\n");
751 vgaarb_info(&vgadev
->pdev
->dev
, "no bridge control possible\n");
755 * Currently, we assume that the "initial" setup of the system is not sane,
756 * that is, we come up with conflicting devices and let the arbiter's
757 * client decide if devices decodes legacy things or not.
759 static bool vga_arbiter_add_pci_device(struct pci_dev
*pdev
)
761 struct vga_device
*vgadev
;
764 struct pci_dev
*bridge
;
767 /* Allocate structure */
768 vgadev
= kzalloc(sizeof(struct vga_device
), GFP_KERNEL
);
769 if (vgadev
== NULL
) {
770 vgaarb_err(&pdev
->dev
, "failed to allocate VGA arbiter data\n");
772 * What to do on allocation failure? For now, let's just do
773 * nothing, I'm not sure there is anything saner to be done.
778 /* Take lock & check for duplicates */
779 spin_lock_irqsave(&vga_lock
, flags
);
780 if (vgadev_find(pdev
) != NULL
) {
786 /* By default, assume we decode everything */
787 vgadev
->decodes
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
|
788 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
790 /* By default, mark it as decoding */
794 * Mark that we "own" resources based on our enables, we will
795 * clear that below if the bridge isn't forwarding.
797 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
);
798 if (cmd
& PCI_COMMAND_IO
)
799 vgadev
->owns
|= VGA_RSRC_LEGACY_IO
;
800 if (cmd
& PCI_COMMAND_MEMORY
)
801 vgadev
->owns
|= VGA_RSRC_LEGACY_MEM
;
803 /* Check if VGA cycles can get down to us */
810 pci_read_config_word(bridge
, PCI_BRIDGE_CONTROL
, &l
);
811 if (!(l
& PCI_BRIDGE_CTL_VGA
)) {
819 if (vga_is_boot_device(vgadev
)) {
820 vgaarb_info(&pdev
->dev
, "setting as boot VGA device%s\n",
821 vga_default_device() ?
822 " (overriding previous)" : "");
823 vga_set_default_device(pdev
);
826 vga_arbiter_check_bridge_sharing(vgadev
);
828 /* Add to the list */
829 list_add_tail(&vgadev
->list
, &vga_list
);
831 vgaarb_info(&pdev
->dev
, "VGA device added: decodes=%s,owns=%s,locks=%s\n",
832 vga_iostate_to_str(vgadev
->decodes
),
833 vga_iostate_to_str(vgadev
->owns
),
834 vga_iostate_to_str(vgadev
->locks
));
836 spin_unlock_irqrestore(&vga_lock
, flags
);
839 spin_unlock_irqrestore(&vga_lock
, flags
);
844 static bool vga_arbiter_del_pci_device(struct pci_dev
*pdev
)
846 struct vga_device
*vgadev
;
850 spin_lock_irqsave(&vga_lock
, flags
);
851 vgadev
= vgadev_find(pdev
);
852 if (vgadev
== NULL
) {
857 if (vga_default
== pdev
)
858 vga_set_default_device(NULL
);
860 if (vgadev
->decodes
& (VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
))
863 /* Remove entry from list */
864 list_del(&vgadev
->list
);
867 /* Wake up all possible waiters */
868 wake_up_all(&vga_wait_queue
);
870 spin_unlock_irqrestore(&vga_lock
, flags
);
875 /* Called with the lock */
876 static void vga_update_device_decodes(struct vga_device
*vgadev
,
877 unsigned int new_decodes
)
879 struct device
*dev
= &vgadev
->pdev
->dev
;
880 unsigned int old_decodes
= vgadev
->decodes
;
881 unsigned int decodes_removed
= ~new_decodes
& old_decodes
;
882 unsigned int decodes_unlocked
= vgadev
->locks
& decodes_removed
;
884 vgadev
->decodes
= new_decodes
;
886 vgaarb_info(dev
, "VGA decodes changed: 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
);
932 * XXX If somebody is going from "doesn't decode" to "decodes"
933 * state here, additional care must be taken as we may have pending
934 * ownership of non-legacy region.
937 spin_unlock_irqrestore(&vga_lock
, flags
);
941 * vga_set_legacy_decoding
942 * @pdev: PCI device of the VGA card
943 * @decodes: bit mask of what legacy regions the card decodes
945 * Indicate to the arbiter if the card decodes legacy VGA IOs, legacy VGA
946 * Memory, both, or none. All cards default to both, the card driver (fbdev for
947 * example) should tell the arbiter if it has disabled legacy decoding, so the
948 * card can be left out of the arbitration process (and can be safe to take
949 * interrupts at any time.
951 void vga_set_legacy_decoding(struct pci_dev
*pdev
, unsigned int decodes
)
953 __vga_set_legacy_decoding(pdev
, decodes
, false);
955 EXPORT_SYMBOL(vga_set_legacy_decoding
);
958 * vga_client_register - register or unregister a VGA arbitration client
959 * @pdev: PCI device of the VGA client
960 * @set_decode: VGA decode change callback
962 * Clients have two callback mechanisms they can use.
964 * @set_decode callback: If a client can disable its GPU VGA resource, it
965 * will get a callback from this to set the encode/decode state.
967 * Rationale: we cannot disable VGA decode resources unconditionally
968 * because some single GPU laptops seem to require ACPI or BIOS access to
969 * the VGA registers to control things like backlights etc. Hopefully newer
970 * multi-GPU laptops do something saner, and desktops won't have any
971 * special ACPI for this. The driver will get a callback when VGA
972 * arbitration is first used by userspace since some older X servers have
975 * Does not check whether a client for @pdev has been registered already.
977 * To unregister, call vga_client_unregister().
979 * Returns: 0 on success, -ENODEV on failure
981 int vga_client_register(struct pci_dev
*pdev
,
982 unsigned int (*set_decode
)(struct pci_dev
*pdev
, bool decode
))
985 struct vga_device
*vgadev
;
987 spin_lock_irqsave(&vga_lock
, flags
);
988 vgadev
= vgadev_find(pdev
);
990 vgadev
->set_decode
= set_decode
;
991 spin_unlock_irqrestore(&vga_lock
, flags
);
996 EXPORT_SYMBOL(vga_client_register
);
999 * Char driver implementation
1003 * open : Open user instance of the arbiter. By default, it's
1004 * attached to the default VGA device of the system.
1006 * close : Close user instance, release locks
1008 * read : Return a string indicating the status of the target.
1009 * An IO state string is of the form {io,mem,io+mem,none},
1010 * mc and ic are respectively mem and io lock counts (for
1011 * debugging/diagnostic only). "decodes" indicate what the
1012 * card currently decodes, "owns" indicates what is currently
1013 * enabled on it, and "locks" indicates what is locked by this
1014 * card. If the card is unplugged, we get "invalid" then for
1015 * card_ID and an -ENODEV error is returned for any command
1016 * until a new card is targeted
1018 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
1020 * write : write a command to the arbiter. List of commands is:
1022 * target <card_ID> : switch target to card <card_ID> (see below)
1023 * lock <io_state> : acquire locks on target ("none" is invalid io_state)
1024 * trylock <io_state> : non-blocking acquire locks on target
1025 * unlock <io_state> : release locks on target
1026 * unlock all : release all locks on target held by this user
1027 * decodes <io_state> : set the legacy decoding attributes for the card
1029 * poll : event if something change on any card (not just the target)
1031 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
1032 * to go back to the system default card (TODO: not implemented yet).
1033 * Currently, only PCI is supported as a prefix, but the userland API may
1034 * support other bus types in the future, even if the current kernel
1035 * implementation doesn't.
1039 * The driver keeps track of which user has what locks on which card. It
1040 * supports stacking, like the kernel one. This complicates the implementation
1041 * a bit, but makes the arbiter more tolerant to userspace problems and able
1042 * to properly cleanup in all cases when a process dies.
1043 * Currently, a max of 16 cards simultaneously can have locks issued from
1044 * userspace for a given user (file descriptor instance) of the arbiter.
1046 * If the device is hot-unplugged, there is a hook inside the module to notify
1047 * it being added/removed in the system and automatically added/removed in
1051 #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
1052 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
1054 /* Each user has an array of these, tracking which cards have locks */
1055 struct vga_arb_user_card
{
1056 struct pci_dev
*pdev
;
1057 unsigned int mem_cnt
;
1058 unsigned int io_cnt
;
1061 struct vga_arb_private
{
1062 struct list_head list
;
1063 struct pci_dev
*target
;
1064 struct vga_arb_user_card cards
[MAX_USER_CARDS
];
1068 static LIST_HEAD(vga_user_list
);
1069 static DEFINE_SPINLOCK(vga_user_lock
);
1073 * Take a string in the format: "PCI:domain:bus:dev.fn" and return the
1074 * respective values. If the string is not in this format, return 0.
1076 static int vga_pci_str_to_vars(char *buf
, int count
, unsigned int *domain
,
1077 unsigned int *bus
, unsigned int *devfn
)
1080 unsigned int slot
, func
;
1082 n
= sscanf(buf
, "PCI:%x:%x:%x.%x", domain
, bus
, &slot
, &func
);
1086 *devfn
= PCI_DEVFN(slot
, func
);
1091 static ssize_t
vga_arb_read(struct file
*file
, char __user
*buf
,
1092 size_t count
, loff_t
*ppos
)
1094 struct vga_arb_private
*priv
= file
->private_data
;
1095 struct vga_device
*vgadev
;
1096 struct pci_dev
*pdev
;
1097 unsigned long flags
;
1102 lbuf
= kmalloc(1024, GFP_KERNEL
);
1106 /* Protect vga_list */
1107 spin_lock_irqsave(&vga_lock
, flags
);
1109 /* If we are targeting the default, use it */
1110 pdev
= priv
->target
;
1111 if (pdev
== NULL
|| pdev
== PCI_INVALID_CARD
) {
1112 spin_unlock_irqrestore(&vga_lock
, flags
);
1113 len
= sprintf(lbuf
, "invalid");
1117 /* Find card vgadev structure */
1118 vgadev
= vgadev_find(pdev
);
1119 if (vgadev
== NULL
) {
1121 * Wow, it's not in the list, that shouldn't happen, let's
1122 * fix us up and return invalid card.
1124 spin_unlock_irqrestore(&vga_lock
, flags
);
1125 len
= sprintf(lbuf
, "invalid");
1129 /* Fill the buffer with info */
1130 len
= snprintf(lbuf
, 1024,
1131 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%u:%u)\n",
1132 vga_decode_count
, pci_name(pdev
),
1133 vga_iostate_to_str(vgadev
->decodes
),
1134 vga_iostate_to_str(vgadev
->owns
),
1135 vga_iostate_to_str(vgadev
->locks
),
1136 vgadev
->io_lock_cnt
, vgadev
->mem_lock_cnt
);
1138 spin_unlock_irqrestore(&vga_lock
, flags
);
1141 /* Copy that to user */
1144 rc
= copy_to_user(buf
, lbuf
, len
);
1152 * TODO: To avoid parsing inside kernel and to improve the speed we may
1153 * consider use ioctl here
1155 static ssize_t
vga_arb_write(struct file
*file
, const char __user
*buf
,
1156 size_t count
, loff_t
*ppos
)
1158 struct vga_arb_private
*priv
= file
->private_data
;
1159 struct vga_arb_user_card
*uc
= NULL
;
1160 struct pci_dev
*pdev
;
1162 unsigned int io_state
;
1164 char kbuf
[64], *curr_pos
;
1165 size_t remaining
= count
;
1170 if (count
>= sizeof(kbuf
))
1172 if (copy_from_user(kbuf
, buf
, count
))
1177 if (strncmp(curr_pos
, "lock ", 5) == 0) {
1181 pr_debug("client 0x%p called 'lock'\n", priv
);
1183 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
1187 if (io_state
== VGA_RSRC_NONE
) {
1192 pdev
= priv
->target
;
1193 if (priv
->target
== NULL
) {
1198 vga_get_uninterruptible(pdev
, io_state
);
1200 /* Update the client's locks lists */
1201 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1202 if (priv
->cards
[i
].pdev
== pdev
) {
1203 if (io_state
& VGA_RSRC_LEGACY_IO
)
1204 priv
->cards
[i
].io_cnt
++;
1205 if (io_state
& VGA_RSRC_LEGACY_MEM
)
1206 priv
->cards
[i
].mem_cnt
++;
1213 } else if (strncmp(curr_pos
, "unlock ", 7) == 0) {
1217 pr_debug("client 0x%p called 'unlock'\n", priv
);
1219 if (strncmp(curr_pos
, "all", 3) == 0)
1220 io_state
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
1222 if (!vga_str_to_iostate
1223 (curr_pos
, remaining
, &io_state
)) {
1228 if (io_state == VGA_RSRC_NONE) {
1235 pdev
= priv
->target
;
1236 if (priv
->target
== NULL
) {
1240 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1241 if (priv
->cards
[i
].pdev
== pdev
)
1242 uc
= &priv
->cards
[i
];
1250 if (io_state
& VGA_RSRC_LEGACY_IO
&& uc
->io_cnt
== 0) {
1255 if (io_state
& VGA_RSRC_LEGACY_MEM
&& uc
->mem_cnt
== 0) {
1260 vga_put(pdev
, io_state
);
1262 if (io_state
& VGA_RSRC_LEGACY_IO
)
1264 if (io_state
& VGA_RSRC_LEGACY_MEM
)
1269 } else if (strncmp(curr_pos
, "trylock ", 8) == 0) {
1273 pr_debug("client 0x%p called 'trylock'\n", priv
);
1275 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
1280 if (io_state == VGA_RSRC_NONE) {
1286 pdev
= priv
->target
;
1287 if (priv
->target
== NULL
) {
1292 if (vga_tryget(pdev
, io_state
)) {
1293 /* Update the client's locks lists... */
1294 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1295 if (priv
->cards
[i
].pdev
== pdev
) {
1296 if (io_state
& VGA_RSRC_LEGACY_IO
)
1297 priv
->cards
[i
].io_cnt
++;
1298 if (io_state
& VGA_RSRC_LEGACY_MEM
)
1299 priv
->cards
[i
].mem_cnt
++;
1310 } else if (strncmp(curr_pos
, "target ", 7) == 0) {
1311 unsigned int domain
, bus
, devfn
;
1312 struct vga_device
*vgadev
;
1316 pr_debug("client 0x%p called 'target'\n", priv
);
1317 /* If target is default */
1318 if (!strncmp(curr_pos
, "default", 7))
1319 pdev
= pci_dev_get(vga_default_device());
1321 if (!vga_pci_str_to_vars(curr_pos
, remaining
,
1322 &domain
, &bus
, &devfn
)) {
1326 pdev
= pci_get_domain_bus_and_slot(domain
, bus
, devfn
);
1328 pr_debug("invalid PCI address %04x:%02x:%02x.%x\n",
1329 domain
, bus
, PCI_SLOT(devfn
),
1335 pr_debug("%s ==> %04x:%02x:%02x.%x pdev %p\n", curr_pos
,
1336 domain
, bus
, PCI_SLOT(devfn
), PCI_FUNC(devfn
),
1340 vgadev
= vgadev_find(pdev
);
1341 pr_debug("vgadev %p\n", vgadev
);
1342 if (vgadev
== NULL
) {
1344 vgaarb_dbg(&pdev
->dev
, "not a VGA device\n");
1352 priv
->target
= pdev
;
1353 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1354 if (priv
->cards
[i
].pdev
== pdev
)
1356 if (priv
->cards
[i
].pdev
== NULL
) {
1357 priv
->cards
[i
].pdev
= pdev
;
1358 priv
->cards
[i
].io_cnt
= 0;
1359 priv
->cards
[i
].mem_cnt
= 0;
1363 if (i
== MAX_USER_CARDS
) {
1364 vgaarb_dbg(&pdev
->dev
, "maximum user cards (%d) number reached, ignoring this one!\n",
1367 /* XXX: Which value to return? */
1377 } else if (strncmp(curr_pos
, "decodes ", 8) == 0) {
1380 pr_debug("client 0x%p called 'decodes'\n", priv
);
1382 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
1386 pdev
= priv
->target
;
1387 if (priv
->target
== NULL
) {
1392 __vga_set_legacy_decoding(pdev
, io_state
, true);
1396 /* If we got here, the message written is not part of the protocol! */
1403 static __poll_t
vga_arb_fpoll(struct file
*file
, poll_table
*wait
)
1405 pr_debug("%s\n", __func__
);
1407 poll_wait(file
, &vga_wait_queue
, wait
);
1411 static int vga_arb_open(struct inode
*inode
, struct file
*file
)
1413 struct vga_arb_private
*priv
;
1414 unsigned long flags
;
1416 pr_debug("%s\n", __func__
);
1418 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1421 spin_lock_init(&priv
->lock
);
1422 file
->private_data
= priv
;
1424 spin_lock_irqsave(&vga_user_lock
, flags
);
1425 list_add(&priv
->list
, &vga_user_list
);
1426 spin_unlock_irqrestore(&vga_user_lock
, flags
);
1428 /* Set the client's lists of locks */
1429 priv
->target
= vga_default_device(); /* Maybe this is still null! */
1430 priv
->cards
[0].pdev
= priv
->target
;
1431 priv
->cards
[0].io_cnt
= 0;
1432 priv
->cards
[0].mem_cnt
= 0;
1437 static int vga_arb_release(struct inode
*inode
, struct file
*file
)
1439 struct vga_arb_private
*priv
= file
->private_data
;
1440 struct vga_arb_user_card
*uc
;
1441 unsigned long flags
;
1444 pr_debug("%s\n", __func__
);
1446 spin_lock_irqsave(&vga_user_lock
, flags
);
1447 list_del(&priv
->list
);
1448 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1449 uc
= &priv
->cards
[i
];
1450 if (uc
->pdev
== NULL
)
1452 vgaarb_dbg(&uc
->pdev
->dev
, "uc->io_cnt == %d, uc->mem_cnt == %d\n",
1453 uc
->io_cnt
, uc
->mem_cnt
);
1454 while (uc
->io_cnt
--)
1455 vga_put(uc
->pdev
, VGA_RSRC_LEGACY_IO
);
1456 while (uc
->mem_cnt
--)
1457 vga_put(uc
->pdev
, VGA_RSRC_LEGACY_MEM
);
1459 spin_unlock_irqrestore(&vga_user_lock
, flags
);
1467 * Callback any registered clients to let them know we have a change in VGA
1470 static void vga_arbiter_notify_clients(void)
1472 struct vga_device
*vgadev
;
1473 unsigned long flags
;
1474 unsigned int new_decodes
;
1477 if (!vga_arbiter_used
)
1480 new_state
= (vga_count
> 1) ? false : true;
1482 spin_lock_irqsave(&vga_lock
, flags
);
1483 list_for_each_entry(vgadev
, &vga_list
, list
) {
1484 if (vgadev
->set_decode
) {
1485 new_decodes
= vgadev
->set_decode(vgadev
->pdev
,
1487 vga_update_device_decodes(vgadev
, new_decodes
);
1490 spin_unlock_irqrestore(&vga_lock
, flags
);
1493 static int pci_notify(struct notifier_block
*nb
, unsigned long action
,
1496 struct device
*dev
= data
;
1497 struct pci_dev
*pdev
= to_pci_dev(dev
);
1498 bool notify
= false;
1500 vgaarb_dbg(dev
, "%s\n", __func__
);
1502 /* Only deal with VGA class devices */
1503 if (!pci_is_vga(pdev
))
1507 * For now, we're only interested in devices added and removed.
1508 * I didn't test this thing here, so someone needs to double check
1509 * for the cases of hot-pluggable VGA cards.
1511 if (action
== BUS_NOTIFY_ADD_DEVICE
)
1512 notify
= vga_arbiter_add_pci_device(pdev
);
1513 else if (action
== BUS_NOTIFY_DEL_DEVICE
)
1514 notify
= vga_arbiter_del_pci_device(pdev
);
1517 vga_arbiter_notify_clients();
1521 static struct notifier_block pci_notifier
= {
1522 .notifier_call
= pci_notify
,
1525 static const struct file_operations vga_arb_device_fops
= {
1526 .read
= vga_arb_read
,
1527 .write
= vga_arb_write
,
1528 .poll
= vga_arb_fpoll
,
1529 .open
= vga_arb_open
,
1530 .release
= vga_arb_release
,
1531 .llseek
= noop_llseek
,
1534 static struct miscdevice vga_arb_device
= {
1535 MISC_DYNAMIC_MINOR
, "vga_arbiter", &vga_arb_device_fops
1538 static int __init
vga_arb_device_init(void)
1541 struct pci_dev
*pdev
;
1543 rc
= misc_register(&vga_arb_device
);
1545 pr_err("error %d registering device\n", rc
);
1547 bus_register_notifier(&pci_bus_type
, &pci_notifier
);
1549 /* Add all VGA class PCI devices by default */
1552 pci_get_subsys(PCI_ANY_ID
, PCI_ANY_ID
, PCI_ANY_ID
,
1553 PCI_ANY_ID
, pdev
)) != NULL
) {
1554 if (pci_is_vga(pdev
))
1555 vga_arbiter_add_pci_device(pdev
);
1558 pr_info("loaded\n");
1561 subsys_initcall_sync(vga_arb_device_init
);