0001 ===========
0002 VGA Arbiter
0003 ===========
0004
0005 Graphic devices are accessed through ranges in I/O or memory space. While most
0006 modern devices allow relocation of such ranges, some "Legacy" VGA devices
0007 implemented on PCI will typically have the same "hard-decoded" addresses as
0008 they did on ISA. For more details see "PCI Bus Binding to IEEE Std 1275-1994
0009 Standard for Boot (Initialization Configuration) Firmware Revision 2.1"
0010 Section 7, Legacy Devices.
0011
0012 The Resource Access Control (RAC) module inside the X server [0] existed for
0013 the legacy VGA arbitration task (besides other bus management tasks) when more
0014 than one legacy device co-exists on the same machine. But the problem happens
0015 when these devices are trying to be accessed by different userspace clients
0016 (e.g. two server in parallel). Their address assignments conflict. Moreover,
0017 ideally, being a userspace application, it is not the role of the X server to
0018 control bus resources. Therefore an arbitration scheme outside of the X server
0019 is needed to control the sharing of these resources. This document introduces
0020 the operation of the VGA arbiter implemented for the Linux kernel.
0021
0022 vgaarb kernel/userspace ABI
0023 ---------------------------
0024
0025 The vgaarb is a module of the Linux Kernel. When it is initially loaded, it
0026 scans all PCI devices and adds the VGA ones inside the arbitration. The
0027 arbiter then enables/disables the decoding on different devices of the VGA
0028 legacy instructions. Devices which do not want/need to use the arbiter may
0029 explicitly tell it by calling vga_set_legacy_decoding().
0030
0031 The kernel exports a char device interface (/dev/vga_arbiter) to the clients,
0032 which has the following semantics:
0033
0034 open
0035 Opens a user instance of the arbiter. By default, it's attached to the
0036 default VGA device of the system.
0037
0038 close
0039 Close a user instance. Release locks made by the user
0040
0041 read
0042 Return a string indicating the status of the target like:
0043
0044 "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
0045
0046 An IO state string is of the form {io,mem,io+mem,none}, mc and
0047 ic are respectively mem and io lock counts (for debugging/
0048 diagnostic only). "decodes" indicate what the card currently
0049 decodes, "owns" indicates what is currently enabled on it, and
0050 "locks" indicates what is locked by this card. If the card is
0051 unplugged, we get "invalid" then for card_ID and an -ENODEV
0052 error is returned for any command until a new card is targeted.
0053
0054
0055 write
0056 Write a command to the arbiter. List of commands:
0057
0058 target <card_ID>
0059 switch target to card <card_ID> (see below)
0060 lock <io_state>
0061 acquires locks on target ("none" is an invalid io_state)
0062 trylock <io_state>
0063 non-blocking acquire locks on target (returns EBUSY if
0064 unsuccessful)
0065 unlock <io_state>
0066 release locks on target
0067 unlock all
0068 release all locks on target held by this user (not implemented
0069 yet)
0070 decodes <io_state>
0071 set the legacy decoding attributes for the card
0072
0073 poll
0074 event if something changes on any card (not just the target)
0075
0076 card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
0077 to go back to the system default card (TODO: not implemented yet). Currently,
0078 only PCI is supported as a prefix, but the userland API may support other bus
0079 types in the future, even if the current kernel implementation doesn't.
0080
0081 Note about locks:
0082
0083 The driver keeps track of which user has which locks on which card. It
0084 supports stacking, like the kernel one. This complexifies the implementation
0085 a bit, but makes the arbiter more tolerant to user space problems and able
0086 to properly cleanup in all cases when a process dies.
0087 Currently, a max of 16 cards can have locks simultaneously issued from
0088 user space for a given user (file descriptor instance) of the arbiter.
0089
0090 In the case of devices hot-{un,}plugged, there is a hook - pci_notify() - to
0091 notify them being added/removed in the system and automatically added/removed
0092 in the arbiter.
0093
0094 There is also an in-kernel API of the arbiter in case DRM, vgacon, or other
0095 drivers want to use it.
0096
0097 In-kernel interface
0098 -------------------
0099
0100 .. kernel-doc:: include/linux/vgaarb.h
0101 :internal:
0102
0103 .. kernel-doc:: drivers/pci/vgaarb.c
0104 :export:
0105
0106 libpciaccess
0107 ------------
0108
0109 To use the vga arbiter char device it was implemented an API inside the
0110 libpciaccess library. One field was added to struct pci_device (each device
0111 on the system)::
0112
0113 /* the type of resource decoded by the device */
0114 int vgaarb_rsrc;
0115
0116 Besides it, in pci_system were added::
0117
0118 int vgaarb_fd;
0119 int vga_count;
0120 struct pci_device *vga_target;
0121 struct pci_device *vga_default_dev;
0122
0123 The vga_count is used to track how many cards are being arbitrated, so for
0124 instance, if there is only one card, then it can completely escape arbitration.
0125
0126 These functions below acquire VGA resources for the given card and mark those
0127 resources as locked. If the resources requested are "normal" (and not legacy)
0128 resources, the arbiter will first check whether the card is doing legacy
0129 decoding for that type of resource. If yes, the lock is "converted" into a
0130 legacy resource lock. The arbiter will first look for all VGA cards that
0131 might conflict and disable their IOs and/or Memory access, including VGA
0132 forwarding on P2P bridges if necessary, so that the requested resources can
0133 be used. Then, the card is marked as locking these resources and the IO and/or
0134 Memory access is enabled on the card (including VGA forwarding on parent
0135 P2P bridges if any). In the case of vga_arb_lock(), the function will block
0136 if some conflicting card is already locking one of the required resources (or
0137 any resource on a different bus segment, since P2P bridges don't differentiate
0138 VGA memory and IO afaik). If the card already owns the resources, the function
0139 succeeds. vga_arb_trylock() will return (-EBUSY) instead of blocking. Nested
0140 calls are supported (a per-resource counter is maintained).
0141
0142 Set the target device of this client. ::
0143
0144 int pci_device_vgaarb_set_target (struct pci_device *dev);
0145
0146 For instance, in x86 if two devices on the same bus want to lock different
0147 resources, both will succeed (lock). If devices are in different buses and
0148 trying to lock different resources, only the first who tried succeeds. ::
0149
0150 int pci_device_vgaarb_lock (void);
0151 int pci_device_vgaarb_trylock (void);
0152
0153 Unlock resources of device. ::
0154
0155 int pci_device_vgaarb_unlock (void);
0156
0157 Indicates to the arbiter if the card decodes legacy VGA IOs, legacy VGA
0158 Memory, both, or none. All cards default to both, the card driver (fbdev for
0159 example) should tell the arbiter if it has disabled legacy decoding, so the
0160 card can be left out of the arbitration process (and can be safe to take
0161 interrupts at any time. ::
0162
0163 int pci_device_vgaarb_decodes (int new_vgaarb_rsrc);
0164
0165 Connects to the arbiter device, allocates the struct ::
0166
0167 int pci_device_vgaarb_init (void);
0168
0169 Close the connection ::
0170
0171 void pci_device_vgaarb_fini (void);
0172
0173 xf86VGAArbiter (X server implementation)
0174 ----------------------------------------
0175
0176 X server basically wraps all the functions that touch VGA registers somehow.
0177
0178 References
0179 ----------
0180
0181 Benjamin Herrenschmidt (IBM?) started this work when he discussed such design
0182 with the Xorg community in 2005 [1, 2]. In the end of 2007, Paulo Zanoni and
0183 Tiago Vignatti (both of C3SL/Federal University of ParanĂ¡) proceeded his work
0184 enhancing the kernel code to adapt as a kernel module and also did the
0185 implementation of the user space side [3]. Now (2009) Tiago Vignatti and Dave
0186 Airlie finally put this work in shape and queued to Jesse Barnes' PCI tree.
0187
0188 0) https://cgit.freedesktop.org/xorg/xserver/commit/?id=4b42448a2388d40f257774fbffdccaea87bd0347
0189 1) https://lists.freedesktop.org/archives/xorg/2005-March/006663.html
0190 2) https://lists.freedesktop.org/archives/xorg/2005-March/006745.html
0191 3) https://lists.freedesktop.org/archives/xorg/2007-October/029507.html