blob: 0b41b051d0211c3b058cfd37d8ba22ed3ade8ff1 [file] [log] [blame]
Daniel Vetterb754b352016-08-12 22:48:56 +02001===========
Tiago Vignatti0737c4e2009-08-16 18:09:36 +03002VGA Arbiter
3===========
4
5Graphic devices are accessed through ranges in I/O or memory space. While most
6modern devices allow relocation of such ranges, some "Legacy" VGA devices
7implemented on PCI will typically have the same "hard-decoded" addresses as
8they did on ISA. For more details see "PCI Bus Binding to IEEE Std 1275-1994
9Standard for Boot (Initialization Configuration) Firmware Revision 2.1"
10Section 7, Legacy Devices.
11
12The Resource Access Control (RAC) module inside the X server [0] existed for
13the legacy VGA arbitration task (besides other bus management tasks) when more
14than one legacy device co-exists on the same machine. But the problem happens
15when these devices are trying to be accessed by different userspace clients
16(e.g. two server in parallel). Their address assignments conflict. Moreover,
Nicolas Kaiserf14d8d32011-05-23 11:59:56 -070017ideally, being a userspace application, it is not the role of the X server to
18control bus resources. Therefore an arbitration scheme outside of the X server
19is needed to control the sharing of these resources. This document introduces
20the operation of the VGA arbiter implemented for the Linux kernel.
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030021
Daniel Vetterb754b352016-08-12 22:48:56 +020022vgaarb kernel/userspace ABI
23---------------------------
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030024
25The vgaarb is a module of the Linux Kernel. When it is initially loaded, it
26scans all PCI devices and adds the VGA ones inside the arbitration. The
27arbiter then enables/disables the decoding on different devices of the VGA
Nicolas Kaiserf14d8d32011-05-23 11:59:56 -070028legacy instructions. Devices which do not want/need to use the arbiter may
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030029explicitly tell it by calling vga_set_legacy_decoding().
30
31The kernel exports a char device interface (/dev/vga_arbiter) to the clients,
32which has the following semantics:
33
Daniel Vetterb754b352016-08-12 22:48:56 +020034open
35 Opens a user instance of the arbiter. By default, it's attached to the
36 default VGA device of the system.
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030037
Daniel Vetterb754b352016-08-12 22:48:56 +020038close
39 Close a user instance. Release locks made by the user
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030040
Daniel Vetterb754b352016-08-12 22:48:56 +020041read
42 Return a string indicating the status of the target like:
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030043
Daniel Vetterb754b352016-08-12 22:48:56 +020044 "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030045
Daniel Vetterb754b352016-08-12 22:48:56 +020046 An IO state string is of the form {io,mem,io+mem,none}, mc and
47 ic are respectively mem and io lock counts (for debugging/
48 diagnostic only). "decodes" indicate what the card currently
49 decodes, "owns" indicates what is currently enabled on it, and
50 "locks" indicates what is locked by this card. If the card is
51 unplugged, we get "invalid" then for card_ID and an -ENODEV
52 error is returned for any command until a new card is targeted.
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030053
54
Daniel Vetterb754b352016-08-12 22:48:56 +020055write
56 Write a command to the arbiter. List of commands:
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030057
Daniel Vetterb754b352016-08-12 22:48:56 +020058 target <card_ID>
59 switch target to card <card_ID> (see below)
60 lock <io_state>
61 acquires locks on target ("none" is an invalid io_state)
62 trylock <io_state>
63 non-blocking acquire locks on target (returns EBUSY if
64 unsuccessful)
65 unlock <io_state>
66 release locks on target
67 unlock all
68 release all locks on target held by this user (not implemented
69 yet)
70 decodes <io_state>
71 set the legacy decoding attributes for the card
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030072
Daniel Vetterb754b352016-08-12 22:48:56 +020073 poll
74 event if something changes on any card (not just the target)
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030075
Daniel Vetterb754b352016-08-12 22:48:56 +020076 card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
77 to go back to the system default card (TODO: not implemented yet). Currently,
78 only PCI is supported as a prefix, but the userland API may support other bus
79 types in the future, even if the current kernel implementation doesn't.
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030080
81Note about locks:
82
83The driver keeps track of which user has which locks on which card. It
84supports stacking, like the kernel one. This complexifies the implementation
85a bit, but makes the arbiter more tolerant to user space problems and able
86to properly cleanup in all cases when a process dies.
87Currently, a max of 16 cards can have locks simultaneously issued from
88user space for a given user (file descriptor instance) of the arbiter.
89
90In the case of devices hot-{un,}plugged, there is a hook - pci_notify() - to
91notify them being added/removed in the system and automatically added/removed
92in the arbiter.
93
Nicolas Kaiserf14d8d32011-05-23 11:59:56 -070094There is also an in-kernel API of the arbiter in case DRM, vgacon, or other
95drivers want to use it.
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030096
Daniel Vetterb754b352016-08-12 22:48:56 +020097In-kernel interface
98-------------------
Tiago Vignatti0737c4e2009-08-16 18:09:36 +030099
Daniel Vetterb754b352016-08-12 22:48:56 +0200100.. kernel-doc:: include/linux/vgaarb.h
101 :internal:
102
103.. kernel-doc:: drivers/gpu/vga/vgaarb.c
104 :export:
105
106libpciaccess
107------------
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300108
109To use the vga arbiter char device it was implemented an API inside the
Detlef Riekenberg15293df2009-12-10 13:55:48 +0100110libpciaccess library. One field was added to struct pci_device (each device
Daniel Vetterb754b352016-08-12 22:48:56 +0200111on the system)::
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300112
113 /* the type of resource decoded by the device */
114 int vgaarb_rsrc;
115
Daniel Vetterb754b352016-08-12 22:48:56 +0200116Besides it, in pci_system were added::
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300117
118 int vgaarb_fd;
119 int vga_count;
120 struct pci_device *vga_target;
121 struct pci_device *vga_default_dev;
122
Nicolas Kaiserf14d8d32011-05-23 11:59:56 -0700123The vga_count is used to track how many cards are being arbitrated, so for
124instance, if there is only one card, then it can completely escape arbitration.
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300125
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300126These functions below acquire VGA resources for the given card and mark those
127resources as locked. If the resources requested are "normal" (and not legacy)
128resources, the arbiter will first check whether the card is doing legacy
129decoding for that type of resource. If yes, the lock is "converted" into a
130legacy resource lock. The arbiter will first look for all VGA cards that
131might conflict and disable their IOs and/or Memory access, including VGA
132forwarding on P2P bridges if necessary, so that the requested resources can
133be used. Then, the card is marked as locking these resources and the IO and/or
134Memory access is enabled on the card (including VGA forwarding on parent
135P2P bridges if any). In the case of vga_arb_lock(), the function will block
136if some conflicting card is already locking one of the required resources (or
137any resource on a different bus segment, since P2P bridges don't differentiate
138VGA memory and IO afaik). If the card already owns the resources, the function
139succeeds. vga_arb_trylock() will return (-EBUSY) instead of blocking. Nested
140calls are supported (a per-resource counter is maintained).
141
Daniel Vetterb754b352016-08-12 22:48:56 +0200142Set the target device of this client. ::
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300143
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300144 int pci_device_vgaarb_set_target (struct pci_device *dev);
145
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300146For instance, in x86 if two devices on the same bus want to lock different
147resources, both will succeed (lock). If devices are in different buses and
Daniel Vetterb754b352016-08-12 22:48:56 +0200148trying to lock different resources, only the first who tried succeeds. ::
149
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300150 int pci_device_vgaarb_lock (void);
151 int pci_device_vgaarb_trylock (void);
152
Daniel Vetterb754b352016-08-12 22:48:56 +0200153Unlock resources of device. ::
154
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300155 int pci_device_vgaarb_unlock (void);
156
157Indicates to the arbiter if the card decodes legacy VGA IOs, legacy VGA
158Memory, both, or none. All cards default to both, the card driver (fbdev for
159example) should tell the arbiter if it has disabled legacy decoding, so the
160card can be left out of the arbitration process (and can be safe to take
Daniel Vetterb754b352016-08-12 22:48:56 +0200161interrupts at any time. ::
162
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300163 int pci_device_vgaarb_decodes (int new_vgaarb_rsrc);
164
Daniel Vetterb754b352016-08-12 22:48:56 +0200165Connects to the arbiter device, allocates the struct ::
166
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300167 int pci_device_vgaarb_init (void);
168
Daniel Vetterb754b352016-08-12 22:48:56 +0200169Close the connection ::
170
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300171 void pci_device_vgaarb_fini (void);
172
Daniel Vetterb754b352016-08-12 22:48:56 +0200173xf86VGAArbiter (X server implementation)
174----------------------------------------
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300175
176X server basically wraps all the functions that touch VGA registers somehow.
177
Daniel Vetterb754b352016-08-12 22:48:56 +0200178References
179----------
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300180
181Benjamin Herrenschmidt (IBM?) started this work when he discussed such design
182with the Xorg community in 2005 [1, 2]. In the end of 2007, Paulo Zanoni and
Al Virod36b6912011-12-29 17:09:01 -0500183Tiago Vignatti (both of C3SL/Federal University of ParanĂ¡) proceeded his work
Tiago Vignatti0737c4e2009-08-16 18:09:36 +0300184enhancing the kernel code to adapt as a kernel module and also did the
185implementation of the user space side [3]. Now (2009) Tiago Vignatti and Dave
186Airlie finally put this work in shape and queued to Jesse Barnes' PCI tree.
187
Daniel Vetterb754b352016-08-12 22:48:56 +02001880) http://cgit.freedesktop.org/xorg/xserver/commit/?id=4b42448a2388d40f257774fbffdccaea87bd0347
1891) http://lists.freedesktop.org/archives/xorg/2005-March/006663.html
1902) http://lists.freedesktop.org/archives/xorg/2005-March/006745.html
1913) http://lists.freedesktop.org/archives/xorg/2007-October/029507.html