vgaarbiter.txt 8.1 KB

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