| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 1 | /* | 
| Jesse Barnes | e2d4304 | 2009-09-17 09:40:31 -0700 | [diff] [blame] | 2 |  * The VGA aribiter manages VGA space routing and VGA resource decode to | 
 | 3 |  * allow multiple VGA devices to be used in a system in a safe way. | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 4 |  * | 
 | 5 |  * (C) Copyright 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org> | 
 | 6 |  * (C) Copyright 2007 Paulo R. Zanoni <przanoni@gmail.com> | 
 | 7 |  * (C) Copyright 2007, 2009 Tiago Vignatti <vignatti@freedesktop.org> | 
| Tiago Vignatti | c0db9cb | 2010-05-24 18:24:31 +0300 | [diff] [blame] | 8 |  * | 
 | 9 |  * Permission is hereby granted, free of charge, to any person obtaining a | 
 | 10 |  * copy of this software and associated documentation files (the "Software"), | 
 | 11 |  * to deal in the Software without restriction, including without limitation | 
 | 12 |  * the rights to use, copy, modify, merge, publish, distribute, sublicense, | 
 | 13 |  * and/or sell copies of the Software, and to permit persons to whom the | 
 | 14 |  * Software is furnished to do so, subject to the following conditions: | 
 | 15 |  * | 
 | 16 |  * The above copyright notice and this permission notice (including the next | 
 | 17 |  * paragraph) shall be included in all copies or substantial portions of the | 
 | 18 |  * Software. | 
 | 19 |  * | 
 | 20 |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
 | 21 |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
 | 22 |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL | 
 | 23 |  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
 | 24 |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | 
 | 25 |  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 
 | 26 |  * DEALINGS | 
 | 27 |  * IN THE SOFTWARE. | 
 | 28 |  * | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 29 |  */ | 
 | 30 |  | 
 | 31 | #ifndef LINUX_VGA_H | 
| Doug Goldstein | a6a1a09 | 2010-07-20 15:22:25 -0700 | [diff] [blame] | 32 | #define LINUX_VGA_H | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 33 |  | 
| Matthew Garrett | 1a39b31 | 2012-04-16 16:26:02 -0400 | [diff] [blame] | 34 | #include <video/vga.h> | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 35 |  | 
 | 36 | /* Legacy VGA regions */ | 
 | 37 | #define VGA_RSRC_NONE	       0x00 | 
 | 38 | #define VGA_RSRC_LEGACY_IO     0x01 | 
 | 39 | #define VGA_RSRC_LEGACY_MEM    0x02 | 
 | 40 | #define VGA_RSRC_LEGACY_MASK   (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM) | 
 | 41 | /* Non-legacy access */ | 
 | 42 | #define VGA_RSRC_NORMAL_IO     0x04 | 
 | 43 | #define VGA_RSRC_NORMAL_MEM    0x08 | 
 | 44 |  | 
 | 45 | /* Passing that instead of a pci_dev to use the system "default" | 
 | 46 |  * device, that is the one used by vgacon. Archs will probably | 
 | 47 |  * have to provide their own vga_default_device(); | 
 | 48 |  */ | 
 | 49 | #define VGA_DEFAULT_DEVICE     (NULL) | 
 | 50 |  | 
| Randy Dunlap | 6069a4c | 2012-01-30 11:43:52 -0800 | [diff] [blame] | 51 | struct pci_dev; | 
 | 52 |  | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 53 | /* For use by clients */ | 
 | 54 |  | 
 | 55 | /** | 
 | 56 |  *     vga_set_legacy_decoding | 
 | 57 |  * | 
 | 58 |  *     @pdev: pci device of the VGA card | 
 | 59 |  *     @decodes: bit mask of what legacy regions the card decodes | 
 | 60 |  * | 
 | 61 |  *     Indicates to the arbiter if the card decodes legacy VGA IOs, | 
 | 62 |  *     legacy VGA Memory, both, or none. All cards default to both, | 
 | 63 |  *     the card driver (fbdev for example) should tell the arbiter | 
 | 64 |  *     if it has disabled legacy decoding, so the card can be left | 
 | 65 |  *     out of the arbitration process (and can be safe to take | 
 | 66 |  *     interrupts at any time. | 
 | 67 |  */ | 
 | 68 | extern void vga_set_legacy_decoding(struct pci_dev *pdev, | 
| Ingo Molnar | 7c32928 | 2009-09-23 09:52:18 +1000 | [diff] [blame] | 69 | 				    unsigned int decodes); | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 70 |  | 
 | 71 | /** | 
 | 72 |  *     vga_get         - acquire & locks VGA resources | 
 | 73 |  * | 
| Dave Airlie | 6ac3bd5 | 2009-08-19 15:21:57 +1000 | [diff] [blame] | 74 |  *     @pdev: pci device of the VGA card or NULL for the system default | 
 | 75 |  *     @rsrc: bit mask of resources to acquire and lock | 
 | 76 |  *     @interruptible: blocking should be interruptible by signals ? | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 77 |  * | 
 | 78 |  *     This function acquires VGA resources for the given | 
 | 79 |  *     card and mark those resources locked. If the resource requested | 
 | 80 |  *     are "normal" (and not legacy) resources, the arbiter will first check | 
| Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 81 |  *     whether the card is doing legacy decoding for that type of resource. If | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 82 |  *     yes, the lock is "converted" into a legacy resource lock. | 
 | 83 |  *     The arbiter will first look for all VGA cards that might conflict | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 84 |  *     and disable their IOs and/or Memory access, including VGA forwarding | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 85 |  *     on P2P bridges if necessary, so that the requested resources can | 
 | 86 |  *     be used. Then, the card is marked as locking these resources and | 
 | 87 |  *     the IO and/or Memory accesse are enabled on the card (including | 
 | 88 |  *     VGA forwarding on parent P2P bridges if any). | 
 | 89 |  *     This function will block if some conflicting card is already locking | 
 | 90 |  *     one of the required resources (or any resource on a different bus | 
 | 91 |  *     segment, since P2P bridges don't differenciate VGA memory and IO | 
| Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 92 |  *     afaik). You can indicate whether this blocking should be interruptible | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 93 |  *     by a signal (for userland interface) or not. | 
 | 94 |  *     Must not be called at interrupt time or in atomic context. | 
 | 95 |  *     If the card already owns the resources, the function succeeds. | 
 | 96 |  *     Nested calls are supported (a per-resource counter is maintained) | 
 | 97 |  */ | 
 | 98 |  | 
| Chris Wilson | 04cbe1d | 2010-08-19 21:29:43 +0100 | [diff] [blame] | 99 | #if defined(CONFIG_VGA_ARB) | 
 | 100 | extern int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible); | 
 | 101 | #else | 
 | 102 | static inline int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible) { return 0; } | 
 | 103 | #endif | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 104 |  | 
 | 105 | /** | 
 | 106 |  *     vga_get_interruptible | 
 | 107 |  * | 
 | 108 |  *     Shortcut to vga_get | 
 | 109 |  */ | 
 | 110 |  | 
 | 111 | static inline int vga_get_interruptible(struct pci_dev *pdev, | 
| Dave Airlie | 6ac3bd5 | 2009-08-19 15:21:57 +1000 | [diff] [blame] | 112 | 					unsigned int rsrc) | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 113 | { | 
 | 114 |        return vga_get(pdev, rsrc, 1); | 
 | 115 | } | 
 | 116 |  | 
 | 117 | /** | 
| Dave Airlie | 6ac3bd5 | 2009-08-19 15:21:57 +1000 | [diff] [blame] | 118 |  *     vga_get_uninterruptible | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 119 |  * | 
 | 120 |  *     Shortcut to vga_get | 
 | 121 |  */ | 
 | 122 |  | 
 | 123 | static inline int vga_get_uninterruptible(struct pci_dev *pdev, | 
| Dave Airlie | 6ac3bd5 | 2009-08-19 15:21:57 +1000 | [diff] [blame] | 124 | 					  unsigned int rsrc) | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 125 | { | 
 | 126 |        return vga_get(pdev, rsrc, 0); | 
 | 127 | } | 
 | 128 |  | 
 | 129 | /** | 
 | 130 |  *     vga_tryget      - try to acquire & lock legacy VGA resources | 
 | 131 |  * | 
 | 132 |  *     @pdev: pci devivce of VGA card or NULL for system default | 
 | 133 |  *     @rsrc: bit mask of resources to acquire and lock | 
 | 134 |  * | 
 | 135 |  *     This function performs the same operation as vga_get(), but | 
 | 136 |  *     will return an error (-EBUSY) instead of blocking if the resources | 
 | 137 |  *     are already locked by another card. It can be called in any context | 
 | 138 |  */ | 
 | 139 |  | 
| Chris Wilson | 04cbe1d | 2010-08-19 21:29:43 +0100 | [diff] [blame] | 140 | #if defined(CONFIG_VGA_ARB) | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 141 | extern int vga_tryget(struct pci_dev *pdev, unsigned int rsrc); | 
| Chris Wilson | 04cbe1d | 2010-08-19 21:29:43 +0100 | [diff] [blame] | 142 | #else | 
 | 143 | static inline int vga_tryget(struct pci_dev *pdev, unsigned int rsrc) { return 0; } | 
 | 144 | #endif | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 145 |  | 
 | 146 | /** | 
 | 147 |  *     vga_put         - release lock on legacy VGA resources | 
 | 148 |  * | 
 | 149 |  *     @pdev: pci device of VGA card or NULL for system default | 
 | 150 |  *     @rsrc: but mask of resource to release | 
 | 151 |  * | 
 | 152 |  *     This function releases resources previously locked by vga_get() | 
 | 153 |  *     or vga_tryget(). The resources aren't disabled right away, so | 
 | 154 |  *     that a subsequence vga_get() on the same card will succeed | 
 | 155 |  *     immediately. Resources have a counter, so locks are only | 
 | 156 |  *     released if the counter reaches 0. | 
 | 157 |  */ | 
 | 158 |  | 
| Chris Wilson | 04cbe1d | 2010-08-19 21:29:43 +0100 | [diff] [blame] | 159 | #if defined(CONFIG_VGA_ARB) | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 160 | extern void vga_put(struct pci_dev *pdev, unsigned int rsrc); | 
| Chris Wilson | 04cbe1d | 2010-08-19 21:29:43 +0100 | [diff] [blame] | 161 | #else | 
 | 162 | #define vga_put(pdev, rsrc) | 
 | 163 | #endif | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 164 |  | 
 | 165 |  | 
 | 166 | /** | 
 | 167 |  *     vga_default_device | 
 | 168 |  * | 
 | 169 |  *     This can be defined by the platform. The default implementation | 
 | 170 |  *     is rather dumb and will probably only work properly on single | 
 | 171 |  *     vga card setups and/or x86 platforms. | 
 | 172 |  * | 
 | 173 |  *     If your VGA default device is not PCI, you'll have to return | 
 | 174 |  *     NULL here. In this case, I assume it will not conflict with | 
 | 175 |  *     any PCI card. If this is not true, I'll have to define two archs | 
 | 176 |  *     hooks for enabling/disabling the VGA default device if that is | 
 | 177 |  *     possible. This may be a problem with real _ISA_ VGA cards, in | 
 | 178 |  *     addition to a PCI one. I don't know at this point how to deal | 
 | 179 |  *     with that card. Can theirs IOs be disabled at all ? If not, then | 
 | 180 |  *     I suppose it's a matter of having the proper arch hook telling | 
 | 181 |  *     us about it, so we basically never allow anybody to succeed a | 
 | 182 |  *     vga_get()... | 
 | 183 |  */ | 
 | 184 |  | 
 | 185 | #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE | 
| Matthew Garrett | 6e079d5 | 2012-05-03 09:29:01 +0100 | [diff] [blame] | 186 | #ifdef CONFIG_VGA_ARB | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 187 | extern struct pci_dev *vga_default_device(void); | 
| Matthew Garrett | 1a39b31 | 2012-04-16 16:26:02 -0400 | [diff] [blame] | 188 | extern void vga_set_default_device(struct pci_dev *pdev); | 
| Matthew Garrett | 6e079d5 | 2012-05-03 09:29:01 +0100 | [diff] [blame] | 189 | #else | 
 | 190 | static inline struct pci_dev *vga_default_device(void) { return NULL; }; | 
 | 191 | static inline void vga_set_default_device(struct pci_dev *pdev) { }; | 
 | 192 | #endif | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 193 | #endif | 
 | 194 |  | 
 | 195 | /** | 
 | 196 |  *     vga_conflicts | 
 | 197 |  * | 
 | 198 |  *     Architectures should define this if they have several | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 199 |  *     independent PCI domains that can afford concurrent VGA | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 200 |  *     decoding | 
 | 201 |  */ | 
 | 202 |  | 
 | 203 | #ifndef __ARCH_HAS_VGA_CONFLICT | 
 | 204 | static inline int vga_conflicts(struct pci_dev *p1, struct pci_dev *p2) | 
 | 205 | { | 
 | 206 |        return 1; | 
 | 207 | } | 
 | 208 | #endif | 
 | 209 |  | 
| Dave Airlie | 6ac3bd5 | 2009-08-19 15:21:57 +1000 | [diff] [blame] | 210 | /** | 
 | 211 |  *	vga_client_register | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 212 |  * | 
| Dave Airlie | 6ac3bd5 | 2009-08-19 15:21:57 +1000 | [diff] [blame] | 213 |  *	@pdev: pci device of the VGA client | 
 | 214 |  *	@cookie: client cookie to be used in callbacks | 
 | 215 |  *	@irq_set_state: irq state change callback | 
 | 216 |  *	@set_vga_decode: vga decode change callback | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 217 |  * | 
| Dave Airlie | 6ac3bd5 | 2009-08-19 15:21:57 +1000 | [diff] [blame] | 218 |  * 	return value: 0 on success, -1 on failure | 
 | 219 |  * 	Register a client with the VGA arbitration logic | 
 | 220 |  * | 
 | 221 |  *	Clients have two callback mechanisms they can use. | 
 | 222 |  *	irq enable/disable callback - | 
 | 223 |  *		If a client can't disable its GPUs VGA resources, then we | 
 | 224 |  *		need to be able to ask it to turn off its irqs when we | 
 | 225 |  *		turn off its mem and io decoding. | 
 | 226 |  *	set_vga_decode | 
 | 227 |  *		If a client can disable its GPU VGA resource, it will | 
 | 228 |  *		get a callback from this to set the encode/decode state | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 229 |  * | 
 | 230 |  * Rationale: we cannot disable VGA decode resources unconditionally | 
 | 231 |  * some single GPU laptops seem to require ACPI or BIOS access to the | 
 | 232 |  * VGA registers to control things like backlights etc. | 
 | 233 |  * Hopefully newer multi-GPU laptops do something saner, and desktops | 
 | 234 |  * won't have any special ACPI for this. | 
| Dave Airlie | 6ac3bd5 | 2009-08-19 15:21:57 +1000 | [diff] [blame] | 235 |  * They driver will get a callback when VGA arbitration is first used | 
 | 236 |  * by userspace since we some older X servers have issues. | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 237 |  */ | 
| Ingo Molnar | 7c32928 | 2009-09-23 09:52:18 +1000 | [diff] [blame] | 238 | #if defined(CONFIG_VGA_ARB) | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 239 | int vga_client_register(struct pci_dev *pdev, void *cookie, | 
 | 240 | 			void (*irq_set_state)(void *cookie, bool state), | 
 | 241 | 			unsigned int (*set_vga_decode)(void *cookie, bool state)); | 
| Ingo Molnar | 7c32928 | 2009-09-23 09:52:18 +1000 | [diff] [blame] | 242 | #else | 
 | 243 | static inline int vga_client_register(struct pci_dev *pdev, void *cookie, | 
 | 244 | 				      void (*irq_set_state)(void *cookie, bool state), | 
 | 245 | 				      unsigned int (*set_vga_decode)(void *cookie, bool state)) | 
 | 246 | { | 
 | 247 | 	return 0; | 
 | 248 | } | 
 | 249 | #endif | 
| Benjamin Herrenschmidt | deb2d2e | 2009-08-11 15:52:06 +1000 | [diff] [blame] | 250 |  | 
 | 251 | #endif /* LINUX_VGA_H */ |