Skip to content

Commit b754b35

Browse files
committed
vgaarbiter: rst-ifiy and polish kerneldoc
Move the documentation into Documentation/gpu, link it up and pull in the kernel doc. No actual text changes except that I did polish the kerneldoc a bit, especially for vga_client_register(). v2: Remove some rst from vga-switcheroo.rst that I don't understand, but which seems to be the reason why the new vgaarbiter.rst sometimes drops out of the sidebar index. v3: Drop one level of headings and clarify the vgaarb one a bit. v4: Fix some typos (Sean). Cc: Jonathan Corbet <corbet@lwn.net> Cc: linux-doc@vger.kernel.org Cc: Sean Paul <seanpaul@chromium.org> Reviewed-by: Sean Paul <seanpaul@chromium.org> Signed-off-by: Daniel Vetter <daniel.vetter@intel.com> Link: http://patchwork.freedesktop.org/patch/msgid/1471034937-651-20-git-send-email-daniel.vetter@ffwll.ch
1 parent b3c6c8b commit b754b35

5 files changed

Lines changed: 205 additions & 197 deletions

File tree

‎Documentation/gpu/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ Linux GPU Driver Developer's Guide
1212
drm-uapi
1313
i915
1414
vga-switcheroo
15+
vgaarbiter

‎Documentation/gpu/vga-switcheroo.rst‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
.. _vga_switcheroo:
2-
31
==============
42
VGA Switcheroo
53
==============
Lines changed: 80 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
===========
22
VGA Arbiter
33
===========
44

@@ -19,21 +19,8 @@ control bus resources. Therefore an arbitration scheme outside of the X server
1919
is needed to control the sharing of these resources. This document introduces
2020
the operation of the VGA arbiter implemented for the Linux kernel.
2121

22-
----------------------------------------------------------------------------
23-
24-
I. Details and Theory of Operation
25-
I.1 vgaarb
26-
I.2 libpciaccess
27-
I.3 xf86VGAArbiter (X server implementation)
28-
II. Credits
29-
III.References
30-
31-
32-
I. Details and Theory of Operation
33-
==================================
34-
35-
I.1 vgaarb
36-
----------
22+
vgaarb kernel/userspace ABI
23+
---------------------------
3724

3825
The vgaarb is a module of the Linux Kernel. When it is initially loaded, it
3926
scans all PCI devices and adds the VGA ones inside the arbitration. The
@@ -44,42 +31,52 @@ explicitly tell it by calling vga_set_legacy_decoding().
4431
The kernel exports a char device interface (/dev/vga_arbiter) to the clients,
4532
which has the following semantics:
4633

47-
open : open user instance of the arbiter. By default, it's attached to
48-
the default VGA device of the system.
49-
50-
close : close user instance. Release locks made by the user
51-
52-
read : return a string indicating the status of the target like:
53-
54-
"<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
55-
56-
An IO state string is of the form {io,mem,io+mem,none}, mc and
57-
ic are respectively mem and io lock counts (for debugging/
58-
diagnostic only). "decodes" indicate what the card currently
59-
decodes, "owns" indicates what is currently enabled on it, and
60-
"locks" indicates what is locked by this card. If the card is
61-
unplugged, we get "invalid" then for card_ID and an -ENODEV
62-
error is returned for any command until a new card is targeted.
63-
64-
65-
write : write a command to the arbiter. List of commands:
66-
67-
target <card_ID> : switch target to card <card_ID> (see below)
68-
lock <io_state> : acquires locks on target ("none" is an invalid io_state)
69-
trylock <io_state> : non-blocking acquire locks on target (returns EBUSY if
70-
unsuccessful)
71-
unlock <io_state> : release locks on target
72-
unlock all : release all locks on target held by this user (not
73-
implemented yet)
74-
decodes <io_state> : set the legacy decoding attributes for the card
75-
76-
poll : event if something changes on any card (not just the
77-
target)
78-
79-
card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
80-
to go back to the system default card (TODO: not implemented yet). Currently,
81-
only PCI is supported as a prefix, but the userland API may support other bus
82-
types in the future, even if the current kernel implementation doesn't.
34+
open
35+
Opens a user instance of the arbiter. By default, it's attached to the
36+
default VGA device of the system.
37+
38+
close
39+
Close a user instance. Release locks made by the user
40+
41+
read
42+
Return a string indicating the status of the target like:
43+
44+
"<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
45+
46+
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.
53+
54+
55+
write
56+
Write a command to the arbiter. List of commands:
57+
58+
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
72+
73+
poll
74+
event if something changes on any card (not just the target)
75+
76+
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.
8380

8481
Note about locks:
8582

@@ -97,29 +94,35 @@ in the arbiter.
9794
There is also an in-kernel API of the arbiter in case DRM, vgacon, or other
9895
drivers want to use it.
9996

97+
In-kernel interface
98+
-------------------
99+
100+
.. kernel-doc:: include/linux/vgaarb.h
101+
:internal:
100102

101-
I.2 libpciaccess
102-
----------------
103+
.. kernel-doc:: drivers/gpu/vga/vgaarb.c
104+
:export:
105+
106+
libpciaccess
107+
------------
103108

104109
To use the vga arbiter char device it was implemented an API inside the
105110
libpciaccess library. One field was added to struct pci_device (each device
106-
on the system):
111+
on the system)::
107112

108113
/* the type of resource decoded by the device */
109114
int vgaarb_rsrc;
110115

111-
Besides it, in pci_system were added:
116+
Besides it, in pci_system were added::
112117

113118
int vgaarb_fd;
114119
int vga_count;
115120
struct pci_device *vga_target;
116121
struct pci_device *vga_default_dev;
117122

118-
119123
The vga_count is used to track how many cards are being arbitrated, so for
120124
instance, if there is only one card, then it can completely escape arbitration.
121125

122-
123126
These functions below acquire VGA resources for the given card and mark those
124127
resources as locked. If the resources requested are "normal" (and not legacy)
125128
resources, the arbiter will first check whether the card is doing legacy
@@ -136,44 +139,44 @@ VGA memory and IO afaik). If the card already owns the resources, the function
136139
succeeds. vga_arb_trylock() will return (-EBUSY) instead of blocking. Nested
137140
calls are supported (a per-resource counter is maintained).
138141

142+
Set the target device of this client. ::
139143

140-
Set the target device of this client.
141144
int pci_device_vgaarb_set_target (struct pci_device *dev);
142145

143-
144146
For instance, in x86 if two devices on the same bus want to lock different
145147
resources, both will succeed (lock). If devices are in different buses and
146-
trying to lock different resources, only the first who tried succeeds.
148+
trying to lock different resources, only the first who tried succeeds. ::
149+
147150
int pci_device_vgaarb_lock (void);
148151
int pci_device_vgaarb_trylock (void);
149152

150-
Unlock resources of device.
153+
Unlock resources of device. ::
154+
151155
int pci_device_vgaarb_unlock (void);
152156

153157
Indicates to the arbiter if the card decodes legacy VGA IOs, legacy VGA
154158
Memory, both, or none. All cards default to both, the card driver (fbdev for
155159
example) should tell the arbiter if it has disabled legacy decoding, so the
156160
card can be left out of the arbitration process (and can be safe to take
157-
interrupts at any time.
161+
interrupts at any time. ::
162+
158163
int pci_device_vgaarb_decodes (int new_vgaarb_rsrc);
159164

160-
Connects to the arbiter device, allocates the struct
161-
int pci_device_vgaarb_init (void);
165+
Connects to the arbiter device, allocates the struct ::
162166

163-
Close the connection
164-
void pci_device_vgaarb_fini (void);
167+
int pci_device_vgaarb_init (void);
165168

169+
Close the connection ::
166170

167-
I.3 xf86VGAArbiter (X server implementation)
168-
--------------------------------------------
171+
void pci_device_vgaarb_fini (void);
169172

170-
(TODO)
173+
xf86VGAArbiter (X server implementation)
174+
----------------------------------------
171175

172176
X server basically wraps all the functions that touch VGA registers somehow.
173177

174-
175-
II. Credits
176-
===========
178+
References
179+
----------
177180

178181
Benjamin Herrenschmidt (IBM?) started this work when he discussed such design
179182
with the Xorg community in 2005 [1, 2]. In the end of 2007, Paulo Zanoni and
@@ -182,11 +185,7 @@ enhancing the kernel code to adapt as a kernel module and also did the
182185
implementation of the user space side [3]. Now (2009) Tiago Vignatti and Dave
183186
Airlie finally put this work in shape and queued to Jesse Barnes' PCI tree.
184187

185-
186-
III. References
187-
==============
188-
189-
[0] http://cgit.freedesktop.org/xorg/xserver/commit/?id=4b42448a2388d40f257774fbffdccaea87bd0347
190-
[1] http://lists.freedesktop.org/archives/xorg/2005-March/006663.html
191-
[2] http://lists.freedesktop.org/archives/xorg/2005-March/006745.html
192-
[3] http://lists.freedesktop.org/archives/xorg/2007-October/029507.html
188+
0) http://cgit.freedesktop.org/xorg/xserver/commit/?id=4b42448a2388d40f257774fbffdccaea87bd0347
189+
1) http://lists.freedesktop.org/archives/xorg/2005-March/006663.html
190+
2) http://lists.freedesktop.org/archives/xorg/2005-March/006745.html
191+
3) http://lists.freedesktop.org/archives/xorg/2007-October/029507.html

‎drivers/gpu/vga/vgaarb.c‎

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,24 @@ static struct vga_device *vgadev_find(struct pci_dev *pdev)
131131
return NULL;
132132
}
133133

134-
/* Returns the default VGA device (vgacon's babe) */
134+
/**
135+
* vga_default_device - return the default VGA device, for vgacon
136+
*
137+
* This can be defined by the platform. The default implementation
138+
* is rather dumb and will probably only work properly on single
139+
* vga card setups and/or x86 platforms.
140+
*
141+
* If your VGA default device is not PCI, you'll have to return
142+
* NULL here. In this case, I assume it will not conflict with
143+
* any PCI card. If this is not true, I'll have to define two archs
144+
* hooks for enabling/disabling the VGA default device if that is
145+
* possible. This may be a problem with real _ISA_ VGA cards, in
146+
* addition to a PCI one. I don't know at this point how to deal
147+
* with that card. Can theirs IOs be disabled at all ? If not, then
148+
* I suppose it's a matter of having the proper arch hook telling
149+
* us about it, so we basically never allow anybody to succeed a
150+
* vga_get()...
151+
*/
135152
struct pci_dev *vga_default_device(void)
136153
{
137154
return vga_default;
@@ -356,6 +373,40 @@ static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
356373
wake_up_all(&vga_wait_queue);
357374
}
358375

376+
/**
377+
* vga_get - acquire & locks VGA resources
378+
* @pdev: pci device of the VGA card or NULL for the system default
379+
* @rsrc: bit mask of resources to acquire and lock
380+
* @interruptible: blocking should be interruptible by signals ?
381+
*
382+
* This function acquires VGA resources for the given card and mark those
383+
* resources locked. If the resource requested are "normal" (and not legacy)
384+
* resources, the arbiter will first check whether the card is doing legacy
385+
* decoding for that type of resource. If yes, the lock is "converted" into a
386+
* legacy resource lock.
387+
*
388+
* The arbiter will first look for all VGA cards that might conflict and disable
389+
* their IOs and/or Memory access, including VGA forwarding on P2P bridges if
390+
* necessary, so that the requested resources can be used. Then, the card is
391+
* marked as locking these resources and the IO and/or Memory accesses are
392+
* enabled on the card (including VGA forwarding on parent P2P bridges if any).
393+
*
394+
* This function will block if some conflicting card is already locking one of
395+
* the required resources (or any resource on a different bus segment, since P2P
396+
* bridges don't differentiate VGA memory and IO afaik). You can indicate
397+
* whether this blocking should be interruptible by a signal (for userland
398+
* interface) or not.
399+
*
400+
* Must not be called at interrupt time or in atomic context. If the card
401+
* already owns the resources, the function succeeds. Nested calls are
402+
* supported (a per-resource counter is maintained)
403+
*
404+
* On success, release the VGA resource again with vga_put().
405+
*
406+
* Returns:
407+
*
408+
* 0 on success, negative error code on failure.
409+
*/
359410
int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
360411
{
361412
struct vga_device *vgadev, *conflict;
@@ -408,6 +459,21 @@ int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
408459
}
409460
EXPORT_SYMBOL(vga_get);
410461

462+
/**
463+
* vga_tryget - try to acquire & lock legacy VGA resources
464+
* @pdev: pci devivce of VGA card or NULL for system default
465+
* @rsrc: bit mask of resources to acquire and lock
466+
*
467+
* This function performs the same operation as vga_get(), but will return an
468+
* error (-EBUSY) instead of blocking if the resources are already locked by
469+
* another card. It can be called in any context
470+
*
471+
* On success, release the VGA resource again with vga_put().
472+
*
473+
* Returns:
474+
*
475+
* 0 on success, negative error code on failure.
476+
*/
411477
int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
412478
{
413479
struct vga_device *vgadev;
@@ -435,6 +501,16 @@ int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
435501
}
436502
EXPORT_SYMBOL(vga_tryget);
437503

504+
/**
505+
* vga_put - release lock on legacy VGA resources
506+
* @pdev: pci device of VGA card or NULL for system default
507+
* @rsrc: but mask of resource to release
508+
*
509+
* This fuction releases resources previously locked by vga_get() or
510+
* vga_tryget(). The resources aren't disabled right away, so that a subsequence
511+
* vga_get() on the same card will succeed immediately. Resources have a
512+
* counter, so locks are only released if the counter reaches 0.
513+
*/
438514
void vga_put(struct pci_dev *pdev, unsigned int rsrc)
439515
{
440516
struct vga_device *vgadev;
@@ -716,7 +792,37 @@ void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
716792
}
717793
EXPORT_SYMBOL(vga_set_legacy_decoding);
718794

719-
/* call with NULL to unregister */
795+
/**
796+
* vga_client_register - register or unregister a VGA arbitration client
797+
* @pdev: pci device of the VGA client
798+
* @cookie: client cookie to be used in callbacks
799+
* @irq_set_state: irq state change callback
800+
* @set_vga_decode: vga decode change callback
801+
*
802+
* Clients have two callback mechanisms they can use.
803+
*
804+
* @irq_set_state callback: If a client can't disable its GPUs VGA
805+
* resources, then we need to be able to ask it to turn off its irqs when we
806+
* turn off its mem and io decoding.
807+
*
808+
* @set_vga_decode callback: If a client can disable its GPU VGA resource, it
809+
* will get a callback from this to set the encode/decode state.
810+
*
811+
* Rationale: we cannot disable VGA decode resources unconditionally some single
812+
* GPU laptops seem to require ACPI or BIOS access to the VGA registers to
813+
* control things like backlights etc. Hopefully newer multi-GPU laptops do
814+
* something saner, and desktops won't have any special ACPI for this. The
815+
* driver will get a callback when VGA arbitration is first used by userspace
816+
* since some older X servers have issues.
817+
*
818+
* This function does not check whether a client for @pdev has been registered
819+
* already.
820+
*
821+
* To unregister just call this function with @irq_set_state and @set_vga_decode
822+
* both set to NULL for the same @pdev as originally used to register them.
823+
*
824+
* Returns: 0 on success, -1 on failure
825+
*/
720826
int vga_client_register(struct pci_dev *pdev, void *cookie,
721827
void (*irq_set_state)(void *cookie, bool state),
722828
unsigned int (*set_vga_decode)(void *cookie,

0 commit comments

Comments
 (0)