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 #define pr_fmt(fmt) "vgaarb: " fmt
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/pci.h>
37 #include <linux/errno.h>
38 #include <linux/init.h>
39 #include <linux/list.h>
40 #include <linux/sched.h>
41 #include <linux/wait.h>
42 #include <linux/spinlock.h>
43 #include <linux/poll.h>
44 #include <linux/miscdevice.h>
45 #include <linux/slab.h>
46 #include <linux/screen_info.h>
48 #include <linux/uaccess.h>
50 #include <linux/vgaarb.h>
52 static void vga_arbiter_notify_clients(void);
54 * We keep a list of all vga devices in the system to speed
55 * up the various operations of the arbiter
58 struct list_head list
;
60 unsigned int decodes
; /* what does it decodes */
61 unsigned int owns
; /* what does it owns */
62 unsigned int locks
; /* what does it locks */
63 unsigned int io_lock_cnt
; /* legacy IO lock count */
64 unsigned int mem_lock_cnt
; /* legacy MEM lock count */
65 unsigned int io_norm_cnt
; /* normal IO count */
66 unsigned int mem_norm_cnt
; /* normal MEM count */
67 bool bridge_has_one_vga
;
68 /* allow IRQ enable/disable hook */
70 void (*irq_set_state
)(void *cookie
, bool enable
);
71 unsigned int (*set_vga_decode
)(void *cookie
, bool decode
);
74 static LIST_HEAD(vga_list
);
75 static int vga_count
, vga_decode_count
;
76 static bool vga_arbiter_used
;
77 static DEFINE_SPINLOCK(vga_lock
);
78 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue
);
81 static const char *vga_iostate_to_str(unsigned int iostate
)
83 /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
84 iostate
&= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
86 case VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
:
88 case VGA_RSRC_LEGACY_IO
:
90 case VGA_RSRC_LEGACY_MEM
:
96 static int vga_str_to_iostate(char *buf
, int str_size
, int *io_state
)
98 /* we could in theory hand out locks on IO and mem
99 * separately to userspace but it can cause deadlocks */
100 if (strncmp(buf
, "none", 4) == 0) {
101 *io_state
= VGA_RSRC_NONE
;
105 /* XXX We're not chekcing the str_size! */
106 if (strncmp(buf
, "io+mem", 6) == 0)
108 else if (strncmp(buf
, "io", 2) == 0)
110 else if (strncmp(buf
, "mem", 3) == 0)
114 *io_state
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
118 /* this is only used a cookie - it should not be dereferenced */
119 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
)
135 * vga_default_device - return the default VGA device, for vgacon
137 * This can be defined by the platform. The default implementation
138 * is rather dumb and will probably only work properly on single
139 * vga card setups and/or x86 platforms.
141 * If your VGA default device is not PCI, you'll have to return
142 * NULL here. In this case, I assume it will not conflict with
143 * any PCI card. If this is not true, I'll have to define two archs
144 * hooks for enabling/disabling the VGA default device if that is
145 * possible. This may be a problem with real _ISA_ VGA cards, in
146 * addition to a PCI one. I don't know at this point how to deal
147 * with that card. Can theirs IOs be disabled at all ? If not, then
148 * I suppose it's a matter of having the proper arch hook telling
149 * us about it, so we basically never allow anybody to succeed a
152 struct pci_dev
*vga_default_device(void)
156 EXPORT_SYMBOL_GPL(vga_default_device
);
158 void vga_set_default_device(struct pci_dev
*pdev
)
160 if (vga_default
== pdev
)
163 pci_dev_put(vga_default
);
164 vga_default
= pci_dev_get(pdev
);
167 static inline void vga_irq_set_state(struct vga_device
*vgadev
, bool state
)
169 if (vgadev
->irq_set_state
)
170 vgadev
->irq_set_state(vgadev
->cookie
, state
);
174 /* If we don't ever use VGA arb we should avoid
175 turning off anything anywhere due to old X servers getting
176 confused about the boot device not being VGA */
177 static void vga_check_first_use(void)
179 /* we should inform all GPUs in the system that
180 * VGA arb has occurred and to try and disable resources
182 if (!vga_arbiter_used
) {
183 vga_arbiter_used
= true;
184 vga_arbiter_notify_clients();
188 static struct vga_device
*__vga_tryget(struct vga_device
*vgadev
,
191 unsigned int wants
, legacy_wants
, match
;
192 struct vga_device
*conflict
;
193 unsigned int pci_bits
;
196 /* Account for "normal" resources to lock. If we decode the legacy,
197 * counterpart, we need to request it as well
199 if ((rsrc
& VGA_RSRC_NORMAL_IO
) &&
200 (vgadev
->decodes
& VGA_RSRC_LEGACY_IO
))
201 rsrc
|= VGA_RSRC_LEGACY_IO
;
202 if ((rsrc
& VGA_RSRC_NORMAL_MEM
) &&
203 (vgadev
->decodes
& VGA_RSRC_LEGACY_MEM
))
204 rsrc
|= VGA_RSRC_LEGACY_MEM
;
206 pr_debug("%s: %d\n", __func__
, rsrc
);
207 pr_debug("%s: owns: %d\n", __func__
, vgadev
->owns
);
209 /* Check what resources we need to acquire */
210 wants
= rsrc
& ~vgadev
->owns
;
212 /* We already own everything, just mark locked & bye bye */
216 /* We don't need to request a legacy resource, we just enable
217 * appropriate decoding and go
219 legacy_wants
= wants
& VGA_RSRC_LEGACY_MASK
;
220 if (legacy_wants
== 0)
223 /* Ok, we don't, let's find out how we need to kick off */
224 list_for_each_entry(conflict
, &vga_list
, list
) {
225 unsigned int lwants
= legacy_wants
;
226 unsigned int change_bridge
= 0;
228 /* Don't conflict with myself */
229 if (vgadev
== conflict
)
232 /* Check if the architecture allows a conflict between those
233 * 2 devices or if they are on separate domains
235 if (!vga_conflicts(vgadev
->pdev
, conflict
->pdev
))
238 /* We have a possible conflict. before we go further, we must
239 * check if we sit on the same bus as the conflicting device.
240 * if we don't, then we must tie both IO and MEM resources
241 * together since there is only a single bit controlling
242 * VGA forwarding on P2P bridges
244 if (vgadev
->pdev
->bus
!= conflict
->pdev
->bus
) {
246 lwants
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
249 /* Check if the guy has a lock on the resource. If he does,
250 * return the conflicting entry
252 if (conflict
->locks
& lwants
)
255 /* Ok, now check if it owns the resource we want. We can
256 * lock resources that are not decoded, therefore a device
257 * can own resources it doesn't decode.
259 match
= lwants
& conflict
->owns
;
263 /* looks like he doesn't have a lock, we can steal
270 /* If we can't control legacy resources via the bridge, we
271 * also need to disable normal decoding.
273 if (!conflict
->bridge_has_one_vga
) {
274 if ((match
& conflict
->decodes
) & VGA_RSRC_LEGACY_MEM
)
275 pci_bits
|= PCI_COMMAND_MEMORY
;
276 if ((match
& conflict
->decodes
) & VGA_RSRC_LEGACY_IO
)
277 pci_bits
|= PCI_COMMAND_IO
;
280 vga_irq_set_state(conflict
, false);
281 flags
|= PCI_VGA_STATE_CHANGE_DECODES
;
286 flags
|= PCI_VGA_STATE_CHANGE_BRIDGE
;
288 pci_set_vga_state(conflict
->pdev
, false, pci_bits
, flags
);
289 conflict
->owns
&= ~match
;
291 /* If we disabled normal decoding, reflect it in owns */
292 if (pci_bits
& PCI_COMMAND_MEMORY
)
293 conflict
->owns
&= ~VGA_RSRC_NORMAL_MEM
;
294 if (pci_bits
& PCI_COMMAND_IO
)
295 conflict
->owns
&= ~VGA_RSRC_NORMAL_IO
;
299 /* ok dude, we got it, everybody conflicting has been disabled, let's
300 * enable us. Mark any bits in "owns" regardless of whether we
301 * decoded them. We can lock resources we don't decode, therefore
302 * we must track them via "owns".
307 if (!vgadev
->bridge_has_one_vga
) {
308 flags
|= PCI_VGA_STATE_CHANGE_DECODES
;
309 if (wants
& (VGA_RSRC_LEGACY_MEM
|VGA_RSRC_NORMAL_MEM
))
310 pci_bits
|= PCI_COMMAND_MEMORY
;
311 if (wants
& (VGA_RSRC_LEGACY_IO
|VGA_RSRC_NORMAL_IO
))
312 pci_bits
|= PCI_COMMAND_IO
;
314 if (wants
& VGA_RSRC_LEGACY_MASK
)
315 flags
|= PCI_VGA_STATE_CHANGE_BRIDGE
;
317 pci_set_vga_state(vgadev
->pdev
, true, pci_bits
, flags
);
319 if (!vgadev
->bridge_has_one_vga
)
320 vga_irq_set_state(vgadev
, true);
322 vgadev
->owns
|= wants
;
324 vgadev
->locks
|= (rsrc
& VGA_RSRC_LEGACY_MASK
);
325 if (rsrc
& VGA_RSRC_LEGACY_IO
)
326 vgadev
->io_lock_cnt
++;
327 if (rsrc
& VGA_RSRC_LEGACY_MEM
)
328 vgadev
->mem_lock_cnt
++;
329 if (rsrc
& VGA_RSRC_NORMAL_IO
)
330 vgadev
->io_norm_cnt
++;
331 if (rsrc
& VGA_RSRC_NORMAL_MEM
)
332 vgadev
->mem_norm_cnt
++;
337 static void __vga_put(struct vga_device
*vgadev
, unsigned int rsrc
)
339 unsigned int old_locks
= vgadev
->locks
;
341 pr_debug("%s\n", __func__
);
343 /* Update our counters, and account for equivalent legacy resources
346 if ((rsrc
& VGA_RSRC_NORMAL_IO
) && vgadev
->io_norm_cnt
> 0) {
347 vgadev
->io_norm_cnt
--;
348 if (vgadev
->decodes
& VGA_RSRC_LEGACY_IO
)
349 rsrc
|= VGA_RSRC_LEGACY_IO
;
351 if ((rsrc
& VGA_RSRC_NORMAL_MEM
) && vgadev
->mem_norm_cnt
> 0) {
352 vgadev
->mem_norm_cnt
--;
353 if (vgadev
->decodes
& VGA_RSRC_LEGACY_MEM
)
354 rsrc
|= VGA_RSRC_LEGACY_MEM
;
356 if ((rsrc
& VGA_RSRC_LEGACY_IO
) && vgadev
->io_lock_cnt
> 0)
357 vgadev
->io_lock_cnt
--;
358 if ((rsrc
& VGA_RSRC_LEGACY_MEM
) && vgadev
->mem_lock_cnt
> 0)
359 vgadev
->mem_lock_cnt
--;
361 /* Just clear lock bits, we do lazy operations so we don't really
362 * have to bother about anything else at this point
364 if (vgadev
->io_lock_cnt
== 0)
365 vgadev
->locks
&= ~VGA_RSRC_LEGACY_IO
;
366 if (vgadev
->mem_lock_cnt
== 0)
367 vgadev
->locks
&= ~VGA_RSRC_LEGACY_MEM
;
369 /* Kick the wait queue in case somebody was waiting if we actually
372 if (old_locks
!= vgadev
->locks
)
373 wake_up_all(&vga_wait_queue
);
377 * vga_get - acquire & locks VGA resources
378 * @pdev: pci device of the VGA card or NULL for the system default
379 * @rsrc: bit mask of resources to acquire and lock
380 * @interruptible: blocking should be interruptible by signals ?
382 * This function acquires VGA resources for the given card and mark those
383 * resources locked. If the resource requested are "normal" (and not legacy)
384 * resources, the arbiter will first check whether the card is doing legacy
385 * decoding for that type of resource. If yes, the lock is "converted" into a
386 * legacy resource lock.
388 * The arbiter will first look for all VGA cards that might conflict and disable
389 * their IOs and/or Memory access, including VGA forwarding on P2P bridges if
390 * necessary, so that the requested resources can be used. Then, the card is
391 * marked as locking these resources and the IO and/or Memory accesses are
392 * enabled on the card (including VGA forwarding on parent P2P bridges if any).
394 * This function will block if some conflicting card is already locking one of
395 * the required resources (or any resource on a different bus segment, since P2P
396 * bridges don't differentiate VGA memory and IO afaik). You can indicate
397 * whether this blocking should be interruptible by a signal (for userland
400 * Must not be called at interrupt time or in atomic context. If the card
401 * already owns the resources, the function succeeds. Nested calls are
402 * supported (a per-resource counter is maintained)
404 * On success, release the VGA resource again with vga_put().
408 * 0 on success, negative error code on failure.
410 int vga_get(struct pci_dev
*pdev
, unsigned int rsrc
, int interruptible
)
412 struct vga_device
*vgadev
, *conflict
;
417 vga_check_first_use();
418 /* The one who calls us should check for this, but lets be sure... */
420 pdev
= vga_default_device();
425 spin_lock_irqsave(&vga_lock
, flags
);
426 vgadev
= vgadev_find(pdev
);
427 if (vgadev
== NULL
) {
428 spin_unlock_irqrestore(&vga_lock
, flags
);
432 conflict
= __vga_tryget(vgadev
, rsrc
);
433 spin_unlock_irqrestore(&vga_lock
, flags
);
434 if (conflict
== NULL
)
438 /* We have a conflict, we wait until somebody kicks the
439 * work queue. Currently we have one work queue that we
440 * kick each time some resources are released, but it would
441 * be fairly easy to have a per device one so that we only
442 * need to attach to the conflicting device
444 init_waitqueue_entry(&wait
, current
);
445 add_wait_queue(&vga_wait_queue
, &wait
);
446 set_current_state(interruptible
?
448 TASK_UNINTERRUPTIBLE
);
449 if (interruptible
&& signal_pending(current
)) {
450 __set_current_state(TASK_RUNNING
);
451 remove_wait_queue(&vga_wait_queue
, &wait
);
456 remove_wait_queue(&vga_wait_queue
, &wait
);
460 EXPORT_SYMBOL(vga_get
);
463 * vga_tryget - try to acquire & lock legacy VGA resources
464 * @pdev: pci devivce of VGA card or NULL for system default
465 * @rsrc: bit mask of resources to acquire and lock
467 * This function performs the same operation as vga_get(), but will return an
468 * error (-EBUSY) instead of blocking if the resources are already locked by
469 * another card. It can be called in any context
471 * On success, release the VGA resource again with vga_put().
475 * 0 on success, negative error code on failure.
477 int vga_tryget(struct pci_dev
*pdev
, unsigned int rsrc
)
479 struct vga_device
*vgadev
;
483 vga_check_first_use();
485 /* The one who calls us should check for this, but lets be sure... */
487 pdev
= vga_default_device();
490 spin_lock_irqsave(&vga_lock
, flags
);
491 vgadev
= vgadev_find(pdev
);
492 if (vgadev
== NULL
) {
496 if (__vga_tryget(vgadev
, rsrc
))
499 spin_unlock_irqrestore(&vga_lock
, flags
);
502 EXPORT_SYMBOL(vga_tryget
);
505 * vga_put - release lock on legacy VGA resources
506 * @pdev: pci device of VGA card or NULL for system default
507 * @rsrc: but mask of resource to release
509 * This fuction releases resources previously locked by vga_get() or
510 * vga_tryget(). The resources aren't disabled right away, so that a subsequence
511 * vga_get() on the same card will succeed immediately. Resources have a
512 * counter, so locks are only released if the counter reaches 0.
514 void vga_put(struct pci_dev
*pdev
, unsigned int rsrc
)
516 struct vga_device
*vgadev
;
519 /* The one who calls us should check for this, but lets be sure... */
521 pdev
= vga_default_device();
524 spin_lock_irqsave(&vga_lock
, flags
);
525 vgadev
= vgadev_find(pdev
);
528 __vga_put(vgadev
, rsrc
);
530 spin_unlock_irqrestore(&vga_lock
, flags
);
532 EXPORT_SYMBOL(vga_put
);
535 * Rules for using a bridge to control a VGA descendant decoding: if a bridge
536 * has only one VGA descendant then it can be used to control the VGA routing
537 * for that device. It should always use the bridge closest to the device to
538 * control it. If a bridge has a direct VGA descendant, but also have a sub-
539 * bridge VGA descendant then we cannot use that bridge to control the direct
540 * VGA descendant. So for every device we register, we need to iterate all
541 * its parent bridges so we can invalidate any devices using them properly.
543 static void vga_arbiter_check_bridge_sharing(struct vga_device
*vgadev
)
545 struct vga_device
*same_bridge_vgadev
;
546 struct pci_bus
*new_bus
, *bus
;
547 struct pci_dev
*new_bridge
, *bridge
;
549 vgadev
->bridge_has_one_vga
= true;
551 if (list_empty(&vga_list
))
554 /* okay iterate the new devices bridge hierarachy */
555 new_bus
= vgadev
->pdev
->bus
;
557 new_bridge
= new_bus
->self
;
559 /* go through list of devices already registered */
560 list_for_each_entry(same_bridge_vgadev
, &vga_list
, list
) {
561 bus
= same_bridge_vgadev
->pdev
->bus
;
564 /* see if the share a bridge with this device */
565 if (new_bridge
== bridge
) {
567 * If their direct parent bridge is the same
568 * as any bridge of this device then it can't
569 * be used for that device.
571 same_bridge_vgadev
->bridge_has_one_vga
= false;
575 * Now iterate the previous devices bridge hierarchy.
576 * If the new devices parent bridge is in the other
577 * devices hierarchy then we can't use it to control
583 if (bridge
&& bridge
== vgadev
->pdev
->bus
->self
)
584 vgadev
->bridge_has_one_vga
= false;
589 new_bus
= new_bus
->parent
;
594 * Currently, we assume that the "initial" setup of the system is
595 * not sane, that is we come up with conflicting devices and let
596 * the arbiter's client decides if devices decodes or not legacy
599 static bool vga_arbiter_add_pci_device(struct pci_dev
*pdev
)
601 struct vga_device
*vgadev
;
604 struct pci_dev
*bridge
;
607 /* Only deal with VGA class devices */
608 if ((pdev
->class >> 8) != PCI_CLASS_DISPLAY_VGA
)
611 /* Allocate structure */
612 vgadev
= kzalloc(sizeof(struct vga_device
), GFP_KERNEL
);
613 if (vgadev
== NULL
) {
614 pr_err("failed to allocate pci device\n");
616 * What to do on allocation failure ? For now, let's just do
617 * nothing, I'm not sure there is anything saner to be done.
622 /* Take lock & check for duplicates */
623 spin_lock_irqsave(&vga_lock
, flags
);
624 if (vgadev_find(pdev
) != NULL
) {
630 /* By default, assume we decode everything */
631 vgadev
->decodes
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
|
632 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
634 /* by default mark it as decoding */
636 /* Mark that we "own" resources based on our enables, we will
637 * clear that below if the bridge isn't forwarding
639 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
);
640 if (cmd
& PCI_COMMAND_IO
)
641 vgadev
->owns
|= VGA_RSRC_LEGACY_IO
;
642 if (cmd
& PCI_COMMAND_MEMORY
)
643 vgadev
->owns
|= VGA_RSRC_LEGACY_MEM
;
645 /* Check if VGA cycles can get down to us */
652 pci_read_config_word(bridge
, PCI_BRIDGE_CONTROL
, &l
);
653 if (!(l
& PCI_BRIDGE_CTL_VGA
)) {
661 /* Deal with VGA default device. Use first enabled one
662 * by default if arch doesn't have it's own hook
664 if (vga_default
== NULL
&&
665 ((vgadev
->owns
& VGA_RSRC_LEGACY_MASK
) == VGA_RSRC_LEGACY_MASK
)) {
666 pr_info("setting as boot device: PCI:%s\n", pci_name(pdev
));
667 vga_set_default_device(pdev
);
670 vga_arbiter_check_bridge_sharing(vgadev
);
672 /* Add to the list */
673 list_add(&vgadev
->list
, &vga_list
);
675 pr_info("device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
677 vga_iostate_to_str(vgadev
->decodes
),
678 vga_iostate_to_str(vgadev
->owns
),
679 vga_iostate_to_str(vgadev
->locks
));
681 spin_unlock_irqrestore(&vga_lock
, flags
);
684 spin_unlock_irqrestore(&vga_lock
, flags
);
689 static bool vga_arbiter_del_pci_device(struct pci_dev
*pdev
)
691 struct vga_device
*vgadev
;
695 spin_lock_irqsave(&vga_lock
, flags
);
696 vgadev
= vgadev_find(pdev
);
697 if (vgadev
== NULL
) {
702 if (vga_default
== pdev
)
703 vga_set_default_device(NULL
);
705 if (vgadev
->decodes
& (VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
))
708 /* Remove entry from list */
709 list_del(&vgadev
->list
);
711 /* Notify userland driver that the device is gone so it discards
712 * it's copies of the pci_dev pointer
714 vga_arb_device_card_gone(pdev
);
716 /* Wake up all possible waiters */
717 wake_up_all(&vga_wait_queue
);
719 spin_unlock_irqrestore(&vga_lock
, flags
);
724 /* this is called with the lock */
725 static inline void vga_update_device_decodes(struct vga_device
*vgadev
,
728 int old_decodes
, decodes_removed
, decodes_unlocked
;
730 old_decodes
= vgadev
->decodes
;
731 decodes_removed
= ~new_decodes
& old_decodes
;
732 decodes_unlocked
= vgadev
->locks
& decodes_removed
;
733 vgadev
->decodes
= new_decodes
;
735 pr_info("device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
736 pci_name(vgadev
->pdev
),
737 vga_iostate_to_str(old_decodes
),
738 vga_iostate_to_str(vgadev
->decodes
),
739 vga_iostate_to_str(vgadev
->owns
));
741 /* if we removed locked decodes, lock count goes to zero, and release */
742 if (decodes_unlocked
) {
743 if (decodes_unlocked
& VGA_RSRC_LEGACY_IO
)
744 vgadev
->io_lock_cnt
= 0;
745 if (decodes_unlocked
& VGA_RSRC_LEGACY_MEM
)
746 vgadev
->mem_lock_cnt
= 0;
747 __vga_put(vgadev
, decodes_unlocked
);
750 /* change decodes counter */
751 if (old_decodes
& VGA_RSRC_LEGACY_MASK
&&
752 !(new_decodes
& VGA_RSRC_LEGACY_MASK
))
754 if (!(old_decodes
& VGA_RSRC_LEGACY_MASK
) &&
755 new_decodes
& VGA_RSRC_LEGACY_MASK
)
757 pr_debug("decoding count now is: %d\n", vga_decode_count
);
760 static void __vga_set_legacy_decoding(struct pci_dev
*pdev
,
761 unsigned int decodes
,
764 struct vga_device
*vgadev
;
767 decodes
&= VGA_RSRC_LEGACY_MASK
;
769 spin_lock_irqsave(&vga_lock
, flags
);
770 vgadev
= vgadev_find(pdev
);
774 /* don't let userspace futz with kernel driver decodes */
775 if (userspace
&& vgadev
->set_vga_decode
)
778 /* update the device decodes + counter */
779 vga_update_device_decodes(vgadev
, decodes
);
781 /* XXX if somebody is going from "doesn't decode" to "decodes" state
782 * here, additional care must be taken as we may have pending owner
783 * ship of non-legacy region ...
786 spin_unlock_irqrestore(&vga_lock
, flags
);
789 void vga_set_legacy_decoding(struct pci_dev
*pdev
, unsigned int decodes
)
791 __vga_set_legacy_decoding(pdev
, decodes
, false);
793 EXPORT_SYMBOL(vga_set_legacy_decoding
);
796 * vga_client_register - register or unregister a VGA arbitration client
797 * @pdev: pci device of the VGA client
798 * @cookie: client cookie to be used in callbacks
799 * @irq_set_state: irq state change callback
800 * @set_vga_decode: vga decode change callback
802 * Clients have two callback mechanisms they can use.
804 * @irq_set_state callback: If a client can't disable its GPUs VGA
805 * resources, then we need to be able to ask it to turn off its irqs when we
806 * turn off its mem and io decoding.
808 * @set_vga_decode callback: If a client can disable its GPU VGA resource, it
809 * will get a callback from this to set the encode/decode state.
811 * Rationale: we cannot disable VGA decode resources unconditionally some single
812 * GPU laptops seem to require ACPI or BIOS access to the VGA registers to
813 * control things like backlights etc. Hopefully newer multi-GPU laptops do
814 * something saner, and desktops won't have any special ACPI for this. The
815 * driver will get a callback when VGA arbitration is first used by userspace
816 * since some older X servers have issues.
818 * This function does not check whether a client for @pdev has been registered
821 * To unregister just call this function with @irq_set_state and @set_vga_decode
822 * both set to NULL for the same @pdev as originally used to register them.
824 * Returns: 0 on success, -1 on failure
826 int vga_client_register(struct pci_dev
*pdev
, void *cookie
,
827 void (*irq_set_state
)(void *cookie
, bool state
),
828 unsigned int (*set_vga_decode
)(void *cookie
,
832 struct vga_device
*vgadev
;
835 spin_lock_irqsave(&vga_lock
, flags
);
836 vgadev
= vgadev_find(pdev
);
840 vgadev
->irq_set_state
= irq_set_state
;
841 vgadev
->set_vga_decode
= set_vga_decode
;
842 vgadev
->cookie
= cookie
;
846 spin_unlock_irqrestore(&vga_lock
, flags
);
850 EXPORT_SYMBOL(vga_client_register
);
853 * Char driver implementation
857 * open : open user instance of the arbitrer. by default, it's
858 * attached to the default VGA device of the system.
860 * close : close user instance, release locks
862 * read : return a string indicating the status of the target.
863 * an IO state string is of the form {io,mem,io+mem,none},
864 * mc and ic are respectively mem and io lock counts (for
865 * debugging/diagnostic only). "decodes" indicate what the
866 * card currently decodes, "owns" indicates what is currently
867 * enabled on it, and "locks" indicates what is locked by this
868 * card. If the card is unplugged, we get "invalid" then for
869 * card_ID and an -ENODEV error is returned for any command
870 * until a new card is targeted
872 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
874 * write : write a command to the arbiter. List of commands is:
876 * target <card_ID> : switch target to card <card_ID> (see below)
877 * lock <io_state> : acquires locks on target ("none" is invalid io_state)
878 * trylock <io_state> : non-blocking acquire locks on target
879 * unlock <io_state> : release locks on target
880 * unlock all : release all locks on target held by this user
881 * decodes <io_state> : set the legacy decoding attributes for the card
883 * poll : event if something change on any card (not just the target)
885 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
886 * to go back to the system default card (TODO: not implemented yet).
887 * Currently, only PCI is supported as a prefix, but the userland API may
888 * support other bus types in the future, even if the current kernel
889 * implementation doesn't.
893 * The driver keeps track of which user has what locks on which card. It
894 * supports stacking, like the kernel one. This complexifies the implementation
895 * a bit, but makes the arbiter more tolerant to userspace problems and able
896 * to properly cleanup in all cases when a process dies.
897 * Currently, a max of 16 cards simultaneously can have locks issued from
898 * userspace for a given user (file descriptor instance) of the arbiter.
900 * If the device is hot-unplugged, there is a hook inside the module to notify
901 * they being added/removed in the system and automatically added/removed in
905 #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
906 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
909 * Each user has an array of these, tracking which cards have locks
911 struct vga_arb_user_card
{
912 struct pci_dev
*pdev
;
913 unsigned int mem_cnt
;
917 struct vga_arb_private
{
918 struct list_head list
;
919 struct pci_dev
*target
;
920 struct vga_arb_user_card cards
[MAX_USER_CARDS
];
924 static LIST_HEAD(vga_user_list
);
925 static DEFINE_SPINLOCK(vga_user_lock
);
929 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
930 * returns the respective values. If the string is not in this format,
933 static int vga_pci_str_to_vars(char *buf
, int count
, unsigned int *domain
,
934 unsigned int *bus
, unsigned int *devfn
)
937 unsigned int slot
, func
;
940 n
= sscanf(buf
, "PCI:%x:%x:%x.%x", domain
, bus
, &slot
, &func
);
944 *devfn
= PCI_DEVFN(slot
, func
);
949 static ssize_t
vga_arb_read(struct file
*file
, char __user
*buf
,
950 size_t count
, loff_t
*ppos
)
952 struct vga_arb_private
*priv
= file
->private_data
;
953 struct vga_device
*vgadev
;
954 struct pci_dev
*pdev
;
960 lbuf
= kmalloc(1024, GFP_KERNEL
);
964 /* Shields against vga_arb_device_card_gone (pci_dev going
965 * away), and allows access to vga list
967 spin_lock_irqsave(&vga_lock
, flags
);
969 /* If we are targeting the default, use it */
971 if (pdev
== NULL
|| pdev
== PCI_INVALID_CARD
) {
972 spin_unlock_irqrestore(&vga_lock
, flags
);
973 len
= sprintf(lbuf
, "invalid");
977 /* Find card vgadev structure */
978 vgadev
= vgadev_find(pdev
);
979 if (vgadev
== NULL
) {
980 /* Wow, it's not in the list, that shouldn't happen,
981 * let's fix us up and return invalid card
983 if (pdev
== priv
->target
)
984 vga_arb_device_card_gone(pdev
);
985 spin_unlock_irqrestore(&vga_lock
, flags
);
986 len
= sprintf(lbuf
, "invalid");
990 /* Fill the buffer with infos */
991 len
= snprintf(lbuf
, 1024,
992 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
993 vga_decode_count
, pci_name(pdev
),
994 vga_iostate_to_str(vgadev
->decodes
),
995 vga_iostate_to_str(vgadev
->owns
),
996 vga_iostate_to_str(vgadev
->locks
),
997 vgadev
->io_lock_cnt
, vgadev
->mem_lock_cnt
);
999 spin_unlock_irqrestore(&vga_lock
, flags
);
1002 /* Copy that to user */
1005 rc
= copy_to_user(buf
, lbuf
, len
);
1013 * TODO: To avoid parsing inside kernel and to improve the speed we may
1014 * consider use ioctl here
1016 static ssize_t
vga_arb_write(struct file
*file
, const char __user
*buf
,
1017 size_t count
, loff_t
*ppos
)
1019 struct vga_arb_private
*priv
= file
->private_data
;
1020 struct vga_arb_user_card
*uc
= NULL
;
1021 struct pci_dev
*pdev
;
1023 unsigned int io_state
;
1025 char *kbuf
, *curr_pos
;
1026 size_t remaining
= count
;
1032 kbuf
= kmalloc(count
+ 1, GFP_KERNEL
);
1036 if (copy_from_user(kbuf
, buf
, count
)) {
1041 kbuf
[count
] = '\0'; /* Just to make sure... */
1043 if (strncmp(curr_pos
, "lock ", 5) == 0) {
1047 pr_debug("client 0x%p called 'lock'\n", priv
);
1049 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
1053 if (io_state
== VGA_RSRC_NONE
) {
1058 pdev
= priv
->target
;
1059 if (priv
->target
== NULL
) {
1064 vga_get_uninterruptible(pdev
, io_state
);
1066 /* Update the client's locks lists... */
1067 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1068 if (priv
->cards
[i
].pdev
== pdev
) {
1069 if (io_state
& VGA_RSRC_LEGACY_IO
)
1070 priv
->cards
[i
].io_cnt
++;
1071 if (io_state
& VGA_RSRC_LEGACY_MEM
)
1072 priv
->cards
[i
].mem_cnt
++;
1079 } else if (strncmp(curr_pos
, "unlock ", 7) == 0) {
1083 pr_debug("client 0x%p called 'unlock'\n", priv
);
1085 if (strncmp(curr_pos
, "all", 3) == 0)
1086 io_state
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
1088 if (!vga_str_to_iostate
1089 (curr_pos
, remaining
, &io_state
)) {
1094 if (io_state == VGA_RSRC_NONE) {
1101 pdev
= priv
->target
;
1102 if (priv
->target
== NULL
) {
1106 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1107 if (priv
->cards
[i
].pdev
== pdev
)
1108 uc
= &priv
->cards
[i
];
1116 if (io_state
& VGA_RSRC_LEGACY_IO
&& uc
->io_cnt
== 0) {
1121 if (io_state
& VGA_RSRC_LEGACY_MEM
&& uc
->mem_cnt
== 0) {
1126 vga_put(pdev
, io_state
);
1128 if (io_state
& VGA_RSRC_LEGACY_IO
)
1130 if (io_state
& VGA_RSRC_LEGACY_MEM
)
1135 } else if (strncmp(curr_pos
, "trylock ", 8) == 0) {
1139 pr_debug("client 0x%p called 'trylock'\n", priv
);
1141 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
1146 if (io_state == VGA_RSRC_NONE) {
1152 pdev
= priv
->target
;
1153 if (priv
->target
== NULL
) {
1158 if (vga_tryget(pdev
, io_state
)) {
1159 /* Update the client's locks lists... */
1160 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1161 if (priv
->cards
[i
].pdev
== pdev
) {
1162 if (io_state
& VGA_RSRC_LEGACY_IO
)
1163 priv
->cards
[i
].io_cnt
++;
1164 if (io_state
& VGA_RSRC_LEGACY_MEM
)
1165 priv
->cards
[i
].mem_cnt
++;
1176 } else if (strncmp(curr_pos
, "target ", 7) == 0) {
1177 unsigned int domain
, bus
, devfn
;
1178 struct vga_device
*vgadev
;
1182 pr_debug("client 0x%p called 'target'\n", priv
);
1183 /* if target is default */
1184 if (!strncmp(curr_pos
, "default", 7))
1185 pdev
= pci_dev_get(vga_default_device());
1187 if (!vga_pci_str_to_vars(curr_pos
, remaining
,
1188 &domain
, &bus
, &devfn
)) {
1192 pr_debug("%s ==> %x:%x:%x.%x\n", curr_pos
,
1193 domain
, bus
, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
1195 pdev
= pci_get_domain_bus_and_slot(domain
, bus
, devfn
);
1196 pr_debug("pdev %p\n", pdev
);
1198 pr_err("invalid PCI address %x:%x:%x\n",
1199 domain
, bus
, devfn
);
1205 vgadev
= vgadev_find(pdev
);
1206 pr_debug("vgadev %p\n", vgadev
);
1207 if (vgadev
== NULL
) {
1209 pr_err("this pci device is not a vga device\n");
1217 priv
->target
= pdev
;
1218 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1219 if (priv
->cards
[i
].pdev
== pdev
)
1221 if (priv
->cards
[i
].pdev
== NULL
) {
1222 priv
->cards
[i
].pdev
= pdev
;
1223 priv
->cards
[i
].io_cnt
= 0;
1224 priv
->cards
[i
].mem_cnt
= 0;
1228 if (i
== MAX_USER_CARDS
) {
1229 pr_err("maximum user cards (%d) number reached!\n",
1232 /* XXX: which value to return? */
1242 } else if (strncmp(curr_pos
, "decodes ", 8) == 0) {
1245 pr_debug("client 0x%p called 'decodes'\n", priv
);
1247 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
1251 pdev
= priv
->target
;
1252 if (priv
->target
== NULL
) {
1257 __vga_set_legacy_decoding(pdev
, io_state
, true);
1261 /* If we got here, the message written is not part of the protocol! */
1270 static unsigned int vga_arb_fpoll(struct file
*file
, poll_table
*wait
)
1272 pr_debug("%s\n", __func__
);
1274 poll_wait(file
, &vga_wait_queue
, wait
);
1278 static int vga_arb_open(struct inode
*inode
, struct file
*file
)
1280 struct vga_arb_private
*priv
;
1281 unsigned long flags
;
1283 pr_debug("%s\n", __func__
);
1285 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1288 spin_lock_init(&priv
->lock
);
1289 file
->private_data
= priv
;
1291 spin_lock_irqsave(&vga_user_lock
, flags
);
1292 list_add(&priv
->list
, &vga_user_list
);
1293 spin_unlock_irqrestore(&vga_user_lock
, flags
);
1295 /* Set the client' lists of locks */
1296 priv
->target
= vga_default_device(); /* Maybe this is still null! */
1297 priv
->cards
[0].pdev
= priv
->target
;
1298 priv
->cards
[0].io_cnt
= 0;
1299 priv
->cards
[0].mem_cnt
= 0;
1305 static int vga_arb_release(struct inode
*inode
, struct file
*file
)
1307 struct vga_arb_private
*priv
= file
->private_data
;
1308 struct vga_arb_user_card
*uc
;
1309 unsigned long flags
;
1312 pr_debug("%s\n", __func__
);
1314 spin_lock_irqsave(&vga_user_lock
, flags
);
1315 list_del(&priv
->list
);
1316 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1317 uc
= &priv
->cards
[i
];
1318 if (uc
->pdev
== NULL
)
1320 pr_debug("uc->io_cnt == %d, uc->mem_cnt == %d\n",
1321 uc
->io_cnt
, uc
->mem_cnt
);
1322 while (uc
->io_cnt
--)
1323 vga_put(uc
->pdev
, VGA_RSRC_LEGACY_IO
);
1324 while (uc
->mem_cnt
--)
1325 vga_put(uc
->pdev
, VGA_RSRC_LEGACY_MEM
);
1327 spin_unlock_irqrestore(&vga_user_lock
, flags
);
1334 static void vga_arb_device_card_gone(struct pci_dev
*pdev
)
1339 * callback any registered clients to let them know we have a
1340 * change in VGA cards
1342 static void vga_arbiter_notify_clients(void)
1344 struct vga_device
*vgadev
;
1345 unsigned long flags
;
1346 uint32_t new_decodes
;
1349 if (!vga_arbiter_used
)
1352 spin_lock_irqsave(&vga_lock
, flags
);
1353 list_for_each_entry(vgadev
, &vga_list
, list
) {
1358 if (vgadev
->set_vga_decode
) {
1359 new_decodes
= vgadev
->set_vga_decode(vgadev
->cookie
,
1361 vga_update_device_decodes(vgadev
, new_decodes
);
1364 spin_unlock_irqrestore(&vga_lock
, flags
);
1367 static int pci_notify(struct notifier_block
*nb
, unsigned long action
,
1370 struct device
*dev
= data
;
1371 struct pci_dev
*pdev
= to_pci_dev(dev
);
1372 bool notify
= false;
1374 pr_debug("%s\n", __func__
);
1376 /* For now we're only intereted in devices added and removed. I didn't
1377 * test this thing here, so someone needs to double check for the
1378 * cases of hotplugable vga cards. */
1379 if (action
== BUS_NOTIFY_ADD_DEVICE
)
1380 notify
= vga_arbiter_add_pci_device(pdev
);
1381 else if (action
== BUS_NOTIFY_DEL_DEVICE
)
1382 notify
= vga_arbiter_del_pci_device(pdev
);
1385 vga_arbiter_notify_clients();
1389 static struct notifier_block pci_notifier
= {
1390 .notifier_call
= pci_notify
,
1393 static const struct file_operations vga_arb_device_fops
= {
1394 .read
= vga_arb_read
,
1395 .write
= vga_arb_write
,
1396 .poll
= vga_arb_fpoll
,
1397 .open
= vga_arb_open
,
1398 .release
= vga_arb_release
,
1399 .llseek
= noop_llseek
,
1402 static struct miscdevice vga_arb_device
= {
1403 MISC_DYNAMIC_MINOR
, "vga_arbiter", &vga_arb_device_fops
1406 static int __init
vga_arb_device_init(void)
1409 struct pci_dev
*pdev
;
1410 struct vga_device
*vgadev
;
1412 rc
= misc_register(&vga_arb_device
);
1414 pr_err("error %d registering device\n", rc
);
1416 bus_register_notifier(&pci_bus_type
, &pci_notifier
);
1418 /* We add all pci devices satisfying vga class in the arbiter by
1422 pci_get_subsys(PCI_ANY_ID
, PCI_ANY_ID
, PCI_ANY_ID
,
1423 PCI_ANY_ID
, pdev
)) != NULL
)
1424 vga_arbiter_add_pci_device(pdev
);
1426 pr_info("loaded\n");
1428 list_for_each_entry(vgadev
, &vga_list
, list
) {
1429 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
1431 * Override vga_arbiter_add_pci_device()'s I/O based detection
1432 * as it may take the wrong device (e.g. on Apple system under
1435 * Select the device owning the boot framebuffer if there is
1438 resource_size_t start
, end
, limit
;
1439 unsigned long flags
;
1442 limit
= screen_info
.lfb_base
+ screen_info
.lfb_size
;
1444 /* Does firmware framebuffer belong to us? */
1445 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++) {
1446 flags
= pci_resource_flags(vgadev
->pdev
, i
);
1448 if ((flags
& IORESOURCE_MEM
) == 0)
1451 start
= pci_resource_start(vgadev
->pdev
, i
);
1452 end
= pci_resource_end(vgadev
->pdev
, i
);
1457 if (screen_info
.lfb_base
< start
|| limit
>= end
)
1460 if (!vga_default_device())
1461 pr_info("setting as boot device: PCI:%s\n",
1462 pci_name(vgadev
->pdev
));
1463 else if (vgadev
->pdev
!= vga_default_device())
1464 pr_info("overriding boot device: PCI:%s\n",
1465 pci_name(vgadev
->pdev
));
1466 vga_set_default_device(vgadev
->pdev
);
1469 if (vgadev
->bridge_has_one_vga
)
1470 pr_info("bridge control possible %s\n",
1471 pci_name(vgadev
->pdev
));
1473 pr_info("no bridge control possible %s\n",
1474 pci_name(vgadev
->pdev
));
1478 subsys_initcall(vga_arb_device_init
);