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