18c2ecf20Sopenharmony_ci===========
28c2ecf20Sopenharmony_ciVGA Arbiter
38c2ecf20Sopenharmony_ci===========
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciGraphic devices are accessed through ranges in I/O or memory space. While most
68c2ecf20Sopenharmony_cimodern devices allow relocation of such ranges, some "Legacy" VGA devices
78c2ecf20Sopenharmony_ciimplemented on PCI will typically have the same "hard-decoded" addresses as
88c2ecf20Sopenharmony_cithey did on ISA. For more details see "PCI Bus Binding to IEEE Std 1275-1994
98c2ecf20Sopenharmony_ciStandard for Boot (Initialization Configuration) Firmware Revision 2.1"
108c2ecf20Sopenharmony_ciSection 7, Legacy Devices.
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ciThe Resource Access Control (RAC) module inside the X server [0] existed for
138c2ecf20Sopenharmony_cithe legacy VGA arbitration task (besides other bus management tasks) when more
148c2ecf20Sopenharmony_cithan one legacy device co-exists on the same machine. But the problem happens
158c2ecf20Sopenharmony_ciwhen these devices are trying to be accessed by different userspace clients
168c2ecf20Sopenharmony_ci(e.g. two server in parallel). Their address assignments conflict. Moreover,
178c2ecf20Sopenharmony_ciideally, being a userspace application, it is not the role of the X server to
188c2ecf20Sopenharmony_cicontrol bus resources. Therefore an arbitration scheme outside of the X server
198c2ecf20Sopenharmony_ciis needed to control the sharing of these resources. This document introduces
208c2ecf20Sopenharmony_cithe operation of the VGA arbiter implemented for the Linux kernel.
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_civgaarb kernel/userspace ABI
238c2ecf20Sopenharmony_ci---------------------------
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciThe vgaarb is a module of the Linux Kernel. When it is initially loaded, it
268c2ecf20Sopenharmony_ciscans all PCI devices and adds the VGA ones inside the arbitration. The
278c2ecf20Sopenharmony_ciarbiter then enables/disables the decoding on different devices of the VGA
288c2ecf20Sopenharmony_cilegacy instructions. Devices which do not want/need to use the arbiter may
298c2ecf20Sopenharmony_ciexplicitly tell it by calling vga_set_legacy_decoding().
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ciThe kernel exports a char device interface (/dev/vga_arbiter) to the clients,
328c2ecf20Sopenharmony_ciwhich has the following semantics:
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciopen
358c2ecf20Sopenharmony_ci        Opens a user instance of the arbiter. By default, it's attached to the
368c2ecf20Sopenharmony_ci        default VGA device of the system.
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciclose
398c2ecf20Sopenharmony_ci        Close a user instance. Release locks made by the user
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciread
428c2ecf20Sopenharmony_ci        Return a string indicating the status of the target like:
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci        "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci        An IO state string is of the form {io,mem,io+mem,none}, mc and
478c2ecf20Sopenharmony_ci        ic are respectively mem and io lock counts (for debugging/
488c2ecf20Sopenharmony_ci        diagnostic only). "decodes" indicate what the card currently
498c2ecf20Sopenharmony_ci        decodes, "owns" indicates what is currently enabled on it, and
508c2ecf20Sopenharmony_ci        "locks" indicates what is locked by this card. If the card is
518c2ecf20Sopenharmony_ci        unplugged, we get "invalid" then for card_ID and an -ENODEV
528c2ecf20Sopenharmony_ci        error is returned for any command until a new card is targeted.
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ciwrite
568c2ecf20Sopenharmony_ci        Write a command to the arbiter. List of commands:
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci        target <card_ID>
598c2ecf20Sopenharmony_ci                switch target to card <card_ID> (see below)
608c2ecf20Sopenharmony_ci        lock <io_state>
618c2ecf20Sopenharmony_ci                acquires locks on target ("none" is an invalid io_state)
628c2ecf20Sopenharmony_ci        trylock <io_state>
638c2ecf20Sopenharmony_ci                non-blocking acquire locks on target (returns EBUSY if
648c2ecf20Sopenharmony_ci                unsuccessful)
658c2ecf20Sopenharmony_ci        unlock <io_state>
668c2ecf20Sopenharmony_ci                release locks on target
678c2ecf20Sopenharmony_ci        unlock all
688c2ecf20Sopenharmony_ci                release all locks on target held by this user (not implemented
698c2ecf20Sopenharmony_ci                yet)
708c2ecf20Sopenharmony_ci        decodes <io_state>
718c2ecf20Sopenharmony_ci                set the legacy decoding attributes for the card
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci        poll
748c2ecf20Sopenharmony_ci                event if something changes on any card (not just the target)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci        card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
778c2ecf20Sopenharmony_ci        to go back to the system default card (TODO: not implemented yet). Currently,
788c2ecf20Sopenharmony_ci        only PCI is supported as a prefix, but the userland API may support other bus
798c2ecf20Sopenharmony_ci        types in the future, even if the current kernel implementation doesn't.
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ciNote about locks:
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciThe driver keeps track of which user has which locks on which card. It
848c2ecf20Sopenharmony_cisupports stacking, like the kernel one. This complexifies the implementation
858c2ecf20Sopenharmony_cia bit, but makes the arbiter more tolerant to user space problems and able
868c2ecf20Sopenharmony_cito properly cleanup in all cases when a process dies.
878c2ecf20Sopenharmony_ciCurrently, a max of 16 cards can have locks simultaneously issued from
888c2ecf20Sopenharmony_ciuser space for a given user (file descriptor instance) of the arbiter.
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciIn the case of devices hot-{un,}plugged, there is a hook - pci_notify() - to
918c2ecf20Sopenharmony_cinotify them being added/removed in the system and automatically added/removed
928c2ecf20Sopenharmony_ciin the arbiter.
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciThere is also an in-kernel API of the arbiter in case DRM, vgacon, or other
958c2ecf20Sopenharmony_cidrivers want to use it.
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ciIn-kernel interface
988c2ecf20Sopenharmony_ci-------------------
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci.. kernel-doc:: include/linux/vgaarb.h
1018c2ecf20Sopenharmony_ci   :internal:
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/gpu/vga/vgaarb.c
1048c2ecf20Sopenharmony_ci   :export:
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cilibpciaccess
1078c2ecf20Sopenharmony_ci------------
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciTo use the vga arbiter char device it was implemented an API inside the
1108c2ecf20Sopenharmony_cilibpciaccess library. One field was added to struct pci_device (each device
1118c2ecf20Sopenharmony_cion the system)::
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci    /* the type of resource decoded by the device */
1148c2ecf20Sopenharmony_ci    int vgaarb_rsrc;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ciBesides it, in pci_system were added::
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci    int vgaarb_fd;
1198c2ecf20Sopenharmony_ci    int vga_count;
1208c2ecf20Sopenharmony_ci    struct pci_device *vga_target;
1218c2ecf20Sopenharmony_ci    struct pci_device *vga_default_dev;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciThe vga_count is used to track how many cards are being arbitrated, so for
1248c2ecf20Sopenharmony_ciinstance, if there is only one card, then it can completely escape arbitration.
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciThese functions below acquire VGA resources for the given card and mark those
1278c2ecf20Sopenharmony_ciresources as locked. If the resources requested are "normal" (and not legacy)
1288c2ecf20Sopenharmony_ciresources, the arbiter will first check whether the card is doing legacy
1298c2ecf20Sopenharmony_cidecoding for that type of resource. If yes, the lock is "converted" into a
1308c2ecf20Sopenharmony_cilegacy resource lock. The arbiter will first look for all VGA cards that
1318c2ecf20Sopenharmony_cimight conflict and disable their IOs and/or Memory access, including VGA
1328c2ecf20Sopenharmony_ciforwarding on P2P bridges if necessary, so that the requested resources can
1338c2ecf20Sopenharmony_cibe used. Then, the card is marked as locking these resources and the IO and/or
1348c2ecf20Sopenharmony_ciMemory access is enabled on the card (including VGA forwarding on parent
1358c2ecf20Sopenharmony_ciP2P bridges if any). In the case of vga_arb_lock(), the function will block
1368c2ecf20Sopenharmony_ciif some conflicting card is already locking one of the required resources (or
1378c2ecf20Sopenharmony_ciany resource on a different bus segment, since P2P bridges don't differentiate
1388c2ecf20Sopenharmony_ciVGA memory and IO afaik). If the card already owns the resources, the function
1398c2ecf20Sopenharmony_cisucceeds.  vga_arb_trylock() will return (-EBUSY) instead of blocking. Nested
1408c2ecf20Sopenharmony_cicalls are supported (a per-resource counter is maintained).
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ciSet the target device of this client. ::
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci    int  pci_device_vgaarb_set_target   (struct pci_device *dev);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ciFor instance, in x86 if two devices on the same bus want to lock different
1478c2ecf20Sopenharmony_ciresources, both will succeed (lock). If devices are in different buses and
1488c2ecf20Sopenharmony_citrying to lock different resources, only the first who tried succeeds. ::
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci    int  pci_device_vgaarb_lock         (void);
1518c2ecf20Sopenharmony_ci    int  pci_device_vgaarb_trylock      (void);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ciUnlock resources of device. ::
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci    int  pci_device_vgaarb_unlock       (void);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ciIndicates to the arbiter if the card decodes legacy VGA IOs, legacy VGA
1588c2ecf20Sopenharmony_ciMemory, both, or none. All cards default to both, the card driver (fbdev for
1598c2ecf20Sopenharmony_ciexample) should tell the arbiter if it has disabled legacy decoding, so the
1608c2ecf20Sopenharmony_cicard can be left out of the arbitration process (and can be safe to take
1618c2ecf20Sopenharmony_ciinterrupts at any time. ::
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci    int  pci_device_vgaarb_decodes      (int new_vgaarb_rsrc);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ciConnects to the arbiter device, allocates the struct ::
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci    int  pci_device_vgaarb_init         (void);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ciClose the connection ::
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci    void pci_device_vgaarb_fini         (void);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cixf86VGAArbiter (X server implementation)
1748c2ecf20Sopenharmony_ci----------------------------------------
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ciX server basically wraps all the functions that touch VGA registers somehow.
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ciReferences
1798c2ecf20Sopenharmony_ci----------
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ciBenjamin Herrenschmidt (IBM?) started this work when he discussed such design
1828c2ecf20Sopenharmony_ciwith the Xorg community in 2005 [1, 2]. In the end of 2007, Paulo Zanoni and
1838c2ecf20Sopenharmony_ciTiago Vignatti (both of C3SL/Federal University of Paraná) proceeded his work
1848c2ecf20Sopenharmony_cienhancing the kernel code to adapt as a kernel module and also did the
1858c2ecf20Sopenharmony_ciimplementation of the user space side [3]. Now (2009) Tiago Vignatti and Dave
1868c2ecf20Sopenharmony_ciAirlie finally put this work in shape and queued to Jesse Barnes' PCI tree.
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci0) https://cgit.freedesktop.org/xorg/xserver/commit/?id=4b42448a2388d40f257774fbffdccaea87bd0347
1898c2ecf20Sopenharmony_ci1) https://lists.freedesktop.org/archives/xorg/2005-March/006663.html
1908c2ecf20Sopenharmony_ci2) https://lists.freedesktop.org/archives/xorg/2005-March/006745.html
1918c2ecf20Sopenharmony_ci3) https://lists.freedesktop.org/archives/xorg/2007-October/029507.html
192