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