xref: /linux/drivers/pci/vgaarb.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: MIT
2 /*
3  * vgaarb.c: Implements VGA arbitration. For details refer to
4  * Documentation/gpu/vgaarbiter.rst
5  *
6  * (C) Copyright 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
7  * (C) Copyright 2007 Paulo R. Zanoni <przanoni@gmail.com>
8  * (C) Copyright 2007, 2009 Tiago Vignatti <vignatti@freedesktop.org>
9  */
10 
11 #define pr_fmt(fmt) "vgaarb: " fmt
12 
13 #define vgaarb_dbg(dev, fmt, arg...)	dev_dbg(dev, "vgaarb: " fmt, ##arg)
14 #define vgaarb_info(dev, fmt, arg...)	dev_info(dev, "vgaarb: " fmt, ##arg)
15 #define vgaarb_err(dev, fmt, arg...)	dev_err(dev, "vgaarb: " fmt, ##arg)
16 
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/pci.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/list.h>
23 #include <linux/sched/signal.h>
24 #include <linux/wait.h>
25 #include <linux/spinlock.h>
26 #include <linux/poll.h>
27 #include <linux/miscdevice.h>
28 #include <linux/slab.h>
29 #include <linux/screen_info.h>
30 #include <linux/vt.h>
31 #include <linux/console.h>
32 #include <linux/acpi.h>
33 #include <linux/uaccess.h>
34 #include <linux/vgaarb.h>
35 
36 static void vga_arbiter_notify_clients(void);
37 
38 /*
39  * We keep a list of all VGA devices in the system to speed
40  * up the various operations of the arbiter
41  */
42 struct vga_device {
43 	struct list_head list;
44 	struct pci_dev *pdev;
45 	unsigned int decodes;		/* what it decodes */
46 	unsigned int owns;		/* what it owns */
47 	unsigned int locks;		/* what it locks */
48 	unsigned int io_lock_cnt;	/* legacy IO lock count */
49 	unsigned int mem_lock_cnt;	/* legacy MEM lock count */
50 	unsigned int io_norm_cnt;	/* normal IO count */
51 	unsigned int mem_norm_cnt;	/* normal MEM count */
52 	bool bridge_has_one_vga;
53 	bool is_firmware_default;	/* device selected by firmware */
54 	unsigned int (*set_decode)(struct pci_dev *pdev, bool decode);
55 };
56 
57 static LIST_HEAD(vga_list);
58 static int vga_count, vga_decode_count;
59 static bool vga_arbiter_used;
60 static DEFINE_SPINLOCK(vga_lock);
61 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
62 
63 static const char *vga_iostate_to_str(unsigned int iostate)
64 {
65 	/* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
66 	iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
67 	switch (iostate) {
68 	case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
69 		return "io+mem";
70 	case VGA_RSRC_LEGACY_IO:
71 		return "io";
72 	case VGA_RSRC_LEGACY_MEM:
73 		return "mem";
74 	}
75 	return "none";
76 }
77 
78 static int vga_str_to_iostate(char *buf, int str_size, unsigned int *io_state)
79 {
80 	/*
81 	 * In theory, we could hand out locks on IO and MEM separately to
82 	 * userspace, but this can cause deadlocks.
83 	 */
84 	if (strncmp(buf, "none", 4) == 0) {
85 		*io_state = VGA_RSRC_NONE;
86 		return 1;
87 	}
88 
89 	/* XXX We're not checking the str_size! */
90 	if (strncmp(buf, "io+mem", 6) == 0)
91 		goto both;
92 	else if (strncmp(buf, "io", 2) == 0)
93 		goto both;
94 	else if (strncmp(buf, "mem", 3) == 0)
95 		goto both;
96 	return 0;
97 both:
98 	*io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
99 	return 1;
100 }
101 
102 /* This is only used as a cookie, it should not be dereferenced */
103 static struct pci_dev *vga_default;
104 
105 /* Find somebody in our list */
106 static struct vga_device *vgadev_find(struct pci_dev *pdev)
107 {
108 	struct vga_device *vgadev;
109 
110 	list_for_each_entry(vgadev, &vga_list, list)
111 		if (pdev == vgadev->pdev)
112 			return vgadev;
113 	return NULL;
114 }
115 
116 /**
117  * vga_default_device - return the default VGA device, for vgacon
118  *
119  * This can be defined by the platform. The default implementation is
120  * rather dumb and will probably only work properly on single VGA card
121  * setups and/or x86 platforms.
122  *
123  * If your VGA default device is not PCI, you'll have to return NULL here.
124  * In this case, I assume it will not conflict with any PCI card. If this
125  * is not true, I'll have to define two arch hooks for enabling/disabling
126  * the VGA default device if that is possible. This may be a problem with
127  * real _ISA_ VGA cards, in addition to a PCI one. I don't know at this
128  * point how to deal with that card. Can their IOs be disabled at all? If
129  * not, then I suppose it's a matter of having the proper arch hook telling
130  * us about it, so we basically never allow anybody to succeed a vga_get().
131  */
132 struct pci_dev *vga_default_device(void)
133 {
134 	return vga_default;
135 }
136 EXPORT_SYMBOL_GPL(vga_default_device);
137 
138 void vga_set_default_device(struct pci_dev *pdev)
139 {
140 	if (vga_default == pdev)
141 		return;
142 
143 	pci_dev_put(vga_default);
144 	vga_default = pci_dev_get(pdev);
145 }
146 
147 /**
148  * vga_remove_vgacon - deactivate VGA console
149  *
150  * Unbind and unregister vgacon in case pdev is the default VGA device.
151  * Can be called by GPU drivers on initialization to make sure VGA register
152  * access done by vgacon will not disturb the device.
153  *
154  * @pdev: PCI device.
155  */
156 #if !defined(CONFIG_VGA_CONSOLE)
157 int vga_remove_vgacon(struct pci_dev *pdev)
158 {
159 	return 0;
160 }
161 #elif !defined(CONFIG_DUMMY_CONSOLE)
162 int vga_remove_vgacon(struct pci_dev *pdev)
163 {
164 	return -ENODEV;
165 }
166 #else
167 int vga_remove_vgacon(struct pci_dev *pdev)
168 {
169 	int ret = 0;
170 
171 	if (pdev != vga_default)
172 		return 0;
173 	vgaarb_info(&pdev->dev, "deactivate vga console\n");
174 
175 	console_lock();
176 	if (con_is_bound(&vga_con))
177 		ret = do_take_over_console(&dummy_con, 0,
178 					   MAX_NR_CONSOLES - 1, 1);
179 	if (ret == 0) {
180 		ret = do_unregister_con_driver(&vga_con);
181 
182 		/* Ignore "already unregistered". */
183 		if (ret == -ENODEV)
184 			ret = 0;
185 	}
186 	console_unlock();
187 
188 	return ret;
189 }
190 #endif
191 EXPORT_SYMBOL(vga_remove_vgacon);
192 
193 /*
194  * If we don't ever use VGA arbitration, we should avoid turning off
195  * anything anywhere due to old X servers getting confused about the boot
196  * device not being VGA.
197  */
198 static void vga_check_first_use(void)
199 {
200 	/*
201 	 * Inform all GPUs in the system that VGA arbitration has occurred
202 	 * so they can disable resources if possible.
203 	 */
204 	if (!vga_arbiter_used) {
205 		vga_arbiter_used = true;
206 		vga_arbiter_notify_clients();
207 	}
208 }
209 
210 static struct vga_device *__vga_tryget(struct vga_device *vgadev,
211 				       unsigned int rsrc)
212 {
213 	struct device *dev = &vgadev->pdev->dev;
214 	unsigned int wants, legacy_wants, match;
215 	struct vga_device *conflict;
216 	unsigned int pci_bits;
217 	u32 flags = 0;
218 
219 	/*
220 	 * Account for "normal" resources to lock. If we decode the legacy,
221 	 * counterpart, we need to request it as well
222 	 */
223 	if ((rsrc & VGA_RSRC_NORMAL_IO) &&
224 	    (vgadev->decodes & VGA_RSRC_LEGACY_IO))
225 		rsrc |= VGA_RSRC_LEGACY_IO;
226 	if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
227 	    (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
228 		rsrc |= VGA_RSRC_LEGACY_MEM;
229 
230 	vgaarb_dbg(dev, "%s: %d\n", __func__, rsrc);
231 	vgaarb_dbg(dev, "%s: owns: %d\n", __func__, vgadev->owns);
232 
233 	/* Check what resources we need to acquire */
234 	wants = rsrc & ~vgadev->owns;
235 
236 	/* We already own everything, just mark locked & bye bye */
237 	if (wants == 0)
238 		goto lock_them;
239 
240 	/*
241 	 * We don't need to request a legacy resource, we just enable
242 	 * appropriate decoding and go.
243 	 */
244 	legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
245 	if (legacy_wants == 0)
246 		goto enable_them;
247 
248 	/* Ok, we don't, let's find out who we need to kick off */
249 	list_for_each_entry(conflict, &vga_list, list) {
250 		unsigned int lwants = legacy_wants;
251 		unsigned int change_bridge = 0;
252 
253 		/* Don't conflict with myself */
254 		if (vgadev == conflict)
255 			continue;
256 
257 		/*
258 		 * We have a possible conflict. Before we go further, we must
259 		 * check if we sit on the same bus as the conflicting device.
260 		 * If we don't, then we must tie both IO and MEM resources
261 		 * together since there is only a single bit controlling
262 		 * VGA forwarding on P2P bridges.
263 		 */
264 		if (vgadev->pdev->bus != conflict->pdev->bus) {
265 			change_bridge = 1;
266 			lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
267 		}
268 
269 		/*
270 		 * Check if the guy has a lock on the resource. If he does,
271 		 * return the conflicting entry.
272 		 */
273 		if (conflict->locks & lwants)
274 			return conflict;
275 
276 		/*
277 		 * Ok, now check if it owns the resource we want.  We can
278 		 * lock resources that are not decoded; therefore a device
279 		 * can own resources it doesn't decode.
280 		 */
281 		match = lwants & conflict->owns;
282 		if (!match)
283 			continue;
284 
285 		/*
286 		 * Looks like he doesn't have a lock, we can steal them
287 		 * from him.
288 		 */
289 
290 		flags = 0;
291 		pci_bits = 0;
292 
293 		/*
294 		 * If we can't control legacy resources via the bridge, we
295 		 * also need to disable normal decoding.
296 		 */
297 		if (!conflict->bridge_has_one_vga) {
298 			if ((match & conflict->decodes) & VGA_RSRC_LEGACY_MEM)
299 				pci_bits |= PCI_COMMAND_MEMORY;
300 			if ((match & conflict->decodes) & VGA_RSRC_LEGACY_IO)
301 				pci_bits |= PCI_COMMAND_IO;
302 
303 			if (pci_bits)
304 				flags |= PCI_VGA_STATE_CHANGE_DECODES;
305 		}
306 
307 		if (change_bridge)
308 			flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
309 
310 		pci_set_vga_state(conflict->pdev, false, pci_bits, flags);
311 		conflict->owns &= ~match;
312 
313 		/* If we disabled normal decoding, reflect it in owns */
314 		if (pci_bits & PCI_COMMAND_MEMORY)
315 			conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
316 		if (pci_bits & PCI_COMMAND_IO)
317 			conflict->owns &= ~VGA_RSRC_NORMAL_IO;
318 	}
319 
320 enable_them:
321 	/*
322 	 * Ok, we got it, everybody conflicting has been disabled, let's
323 	 * enable us.  Mark any bits in "owns" regardless of whether we
324 	 * decoded them.  We can lock resources we don't decode, therefore
325 	 * we must track them via "owns".
326 	 */
327 	flags = 0;
328 	pci_bits = 0;
329 
330 	if (!vgadev->bridge_has_one_vga) {
331 		flags |= PCI_VGA_STATE_CHANGE_DECODES;
332 		if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
333 			pci_bits |= PCI_COMMAND_MEMORY;
334 		if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
335 			pci_bits |= PCI_COMMAND_IO;
336 	}
337 	if (wants & VGA_RSRC_LEGACY_MASK)
338 		flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
339 
340 	pci_set_vga_state(vgadev->pdev, true, pci_bits, flags);
341 
342 	vgadev->owns |= wants;
343 lock_them:
344 	vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
345 	if (rsrc & VGA_RSRC_LEGACY_IO)
346 		vgadev->io_lock_cnt++;
347 	if (rsrc & VGA_RSRC_LEGACY_MEM)
348 		vgadev->mem_lock_cnt++;
349 	if (rsrc & VGA_RSRC_NORMAL_IO)
350 		vgadev->io_norm_cnt++;
351 	if (rsrc & VGA_RSRC_NORMAL_MEM)
352 		vgadev->mem_norm_cnt++;
353 
354 	return NULL;
355 }
356 
357 static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
358 {
359 	struct device *dev = &vgadev->pdev->dev;
360 	unsigned int old_locks = vgadev->locks;
361 
362 	vgaarb_dbg(dev, "%s\n", __func__);
363 
364 	/*
365 	 * Update our counters and account for equivalent legacy resources
366 	 * if we decode them.
367 	 */
368 	if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
369 		vgadev->io_norm_cnt--;
370 		if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
371 			rsrc |= VGA_RSRC_LEGACY_IO;
372 	}
373 	if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
374 		vgadev->mem_norm_cnt--;
375 		if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
376 			rsrc |= VGA_RSRC_LEGACY_MEM;
377 	}
378 	if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
379 		vgadev->io_lock_cnt--;
380 	if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
381 		vgadev->mem_lock_cnt--;
382 
383 	/*
384 	 * Just clear lock bits, we do lazy operations so we don't really
385 	 * have to bother about anything else at this point.
386 	 */
387 	if (vgadev->io_lock_cnt == 0)
388 		vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
389 	if (vgadev->mem_lock_cnt == 0)
390 		vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
391 
392 	/*
393 	 * Kick the wait queue in case somebody was waiting if we actually
394 	 * released something.
395 	 */
396 	if (old_locks != vgadev->locks)
397 		wake_up_all(&vga_wait_queue);
398 }
399 
400 /**
401  * vga_get - acquire & lock VGA resources
402  * @pdev: PCI device of the VGA card or NULL for the system default
403  * @rsrc: bit mask of resources to acquire and lock
404  * @interruptible: blocking should be interruptible by signals ?
405  *
406  * Acquire VGA resources for the given card and mark those resources
407  * locked. If the resources requested are "normal" (and not legacy)
408  * resources, the arbiter will first check whether the card is doing legacy
409  * decoding for that type of resource. If yes, the lock is "converted" into
410  * a legacy resource lock.
411  *
412  * The arbiter will first look for all VGA cards that might conflict and disable
413  * their IOs and/or Memory access, including VGA forwarding on P2P bridges if
414  * necessary, so that the requested resources can be used. Then, the card is
415  * marked as locking these resources and the IO and/or Memory accesses are
416  * enabled on the card (including VGA forwarding on parent P2P bridges if any).
417  *
418  * This function will block if some conflicting card is already locking one of
419  * the required resources (or any resource on a different bus segment, since P2P
420  * bridges don't differentiate VGA memory and IO afaik). You can indicate
421  * whether this blocking should be interruptible by a signal (for userland
422  * interface) or not.
423  *
424  * Must not be called at interrupt time or in atomic context.  If the card
425  * already owns the resources, the function succeeds.  Nested calls are
426  * supported (a per-resource counter is maintained)
427  *
428  * On success, release the VGA resource again with vga_put().
429  *
430  * Returns:
431  *
432  * 0 on success, negative error code on failure.
433  */
434 int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
435 {
436 	struct vga_device *vgadev, *conflict;
437 	unsigned long flags;
438 	wait_queue_entry_t wait;
439 	int rc = 0;
440 
441 	vga_check_first_use();
442 	/* The caller should check for this, but let's be sure */
443 	if (pdev == NULL)
444 		pdev = vga_default_device();
445 	if (pdev == NULL)
446 		return 0;
447 
448 	for (;;) {
449 		spin_lock_irqsave(&vga_lock, flags);
450 		vgadev = vgadev_find(pdev);
451 		if (vgadev == NULL) {
452 			spin_unlock_irqrestore(&vga_lock, flags);
453 			rc = -ENODEV;
454 			break;
455 		}
456 		conflict = __vga_tryget(vgadev, rsrc);
457 		spin_unlock_irqrestore(&vga_lock, flags);
458 		if (conflict == NULL)
459 			break;
460 
461 		/*
462 		 * We have a conflict; we wait until somebody kicks the
463 		 * work queue. Currently we have one work queue that we
464 		 * kick each time some resources are released, but it would
465 		 * be fairly easy to have a per-device one so that we only
466 		 * need to attach to the conflicting device.
467 		 */
468 		init_waitqueue_entry(&wait, current);
469 		add_wait_queue(&vga_wait_queue, &wait);
470 		set_current_state(interruptible ?
471 				  TASK_INTERRUPTIBLE :
472 				  TASK_UNINTERRUPTIBLE);
473 		if (interruptible && signal_pending(current)) {
474 			__set_current_state(TASK_RUNNING);
475 			remove_wait_queue(&vga_wait_queue, &wait);
476 			rc = -ERESTARTSYS;
477 			break;
478 		}
479 		schedule();
480 		remove_wait_queue(&vga_wait_queue, &wait);
481 	}
482 	return rc;
483 }
484 EXPORT_SYMBOL(vga_get);
485 
486 /**
487  * vga_tryget - try to acquire & lock legacy VGA resources
488  * @pdev: PCI device of VGA card or NULL for system default
489  * @rsrc: bit mask of resources to acquire and lock
490  *
491  * Perform the same operation as vga_get(), but return an error (-EBUSY)
492  * instead of blocking if the resources are already locked by another card.
493  * Can be called in any context.
494  *
495  * On success, release the VGA resource again with vga_put().
496  *
497  * Returns:
498  *
499  * 0 on success, negative error code on failure.
500  */
501 static int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
502 {
503 	struct vga_device *vgadev;
504 	unsigned long flags;
505 	int rc = 0;
506 
507 	vga_check_first_use();
508 
509 	/* The caller should check for this, but let's be sure */
510 	if (pdev == NULL)
511 		pdev = vga_default_device();
512 	if (pdev == NULL)
513 		return 0;
514 	spin_lock_irqsave(&vga_lock, flags);
515 	vgadev = vgadev_find(pdev);
516 	if (vgadev == NULL) {
517 		rc = -ENODEV;
518 		goto bail;
519 	}
520 	if (__vga_tryget(vgadev, rsrc))
521 		rc = -EBUSY;
522 bail:
523 	spin_unlock_irqrestore(&vga_lock, flags);
524 	return rc;
525 }
526 
527 /**
528  * vga_put - release lock on legacy VGA resources
529  * @pdev: PCI device of VGA card or NULL for system default
530  * @rsrc: bit mask of resource to release
531  *
532  * Release resources previously locked by vga_get() or vga_tryget().  The
533  * resources aren't disabled right away, so that a subsequent vga_get() on
534  * the same card will succeed immediately.  Resources have a counter, so
535  * locks are only released if the counter reaches 0.
536  */
537 void vga_put(struct pci_dev *pdev, unsigned int rsrc)
538 {
539 	struct vga_device *vgadev;
540 	unsigned long flags;
541 
542 	/* The caller should check for this, but let's be sure */
543 	if (pdev == NULL)
544 		pdev = vga_default_device();
545 	if (pdev == NULL)
546 		return;
547 	spin_lock_irqsave(&vga_lock, flags);
548 	vgadev = vgadev_find(pdev);
549 	if (vgadev == NULL)
550 		goto bail;
551 	__vga_put(vgadev, rsrc);
552 bail:
553 	spin_unlock_irqrestore(&vga_lock, flags);
554 }
555 EXPORT_SYMBOL(vga_put);
556 
557 static bool vga_is_firmware_default(struct pci_dev *pdev)
558 {
559 #ifdef CONFIG_SCREEN_INFO
560 	struct screen_info *si = &screen_info;
561 
562 	return pdev == screen_info_pci_dev(si);
563 #else
564 	return false;
565 #endif
566 }
567 
568 static bool vga_arb_integrated_gpu(struct device *dev)
569 {
570 #if defined(CONFIG_ACPI)
571 	struct acpi_device *adev = ACPI_COMPANION(dev);
572 
573 	return adev && !strcmp(acpi_device_hid(adev), ACPI_VIDEO_HID);
574 #else
575 	return false;
576 #endif
577 }
578 
579 /*
580  * Return true if vgadev is a better default VGA device than the best one
581  * we've seen so far.
582  */
583 static bool vga_is_boot_device(struct vga_device *vgadev)
584 {
585 	struct vga_device *boot_vga = vgadev_find(vga_default_device());
586 	struct pci_dev *pdev = vgadev->pdev;
587 	u16 cmd, boot_cmd;
588 
589 	/*
590 	 * We select the default VGA device in this order:
591 	 *   Firmware framebuffer (see vga_arb_select_default_device())
592 	 *   Legacy VGA device (owns VGA_RSRC_LEGACY_MASK)
593 	 *   Non-legacy integrated device (see vga_arb_select_default_device())
594 	 *   Non-legacy discrete device (see vga_arb_select_default_device())
595 	 *   Other device (see vga_arb_select_default_device())
596 	 */
597 
598 	/*
599 	 * We always prefer a firmware default device, so if we've already
600 	 * found one, there's no need to consider vgadev.
601 	 */
602 	if (boot_vga && boot_vga->is_firmware_default)
603 		return false;
604 
605 	if (vga_is_firmware_default(pdev)) {
606 		vgadev->is_firmware_default = true;
607 		return true;
608 	}
609 
610 	/*
611 	 * A legacy VGA device has MEM and IO enabled and any bridges
612 	 * leading to it have PCI_BRIDGE_CTL_VGA enabled so the legacy
613 	 * resources ([mem 0xa0000-0xbffff], [io 0x3b0-0x3bb], etc) are
614 	 * routed to it.
615 	 *
616 	 * We use the first one we find, so if we've already found one,
617 	 * vgadev is no better.
618 	 */
619 	if (boot_vga &&
620 	    (boot_vga->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)
621 		return false;
622 
623 	if ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)
624 		return true;
625 
626 	/*
627 	 * If we haven't found a legacy VGA device, accept a non-legacy
628 	 * device.  It may have either IO or MEM enabled, and bridges may
629 	 * not have PCI_BRIDGE_CTL_VGA enabled, so it may not be able to
630 	 * use legacy VGA resources.  Prefer an integrated GPU over others.
631 	 */
632 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
633 	if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
634 
635 		/*
636 		 * An integrated GPU overrides a previous non-legacy
637 		 * device.  We expect only a single integrated GPU, but if
638 		 * there are more, we use the *last* because that was the
639 		 * previous behavior.
640 		 */
641 		if (vga_arb_integrated_gpu(&pdev->dev))
642 			return true;
643 
644 		/*
645 		 * We prefer the first non-legacy discrete device we find.
646 		 * If we already found one, vgadev is no better.
647 		 */
648 		if (boot_vga) {
649 			pci_read_config_word(boot_vga->pdev, PCI_COMMAND,
650 					     &boot_cmd);
651 			if (boot_cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
652 				return false;
653 		}
654 		return true;
655 	}
656 
657 	/*
658 	 * Vgadev has neither IO nor MEM enabled.  If we haven't found any
659 	 * other VGA devices, it is the best candidate so far.
660 	 */
661 	if (!boot_vga)
662 		return true;
663 
664 	return false;
665 }
666 
667 /*
668  * Rules for using a bridge to control a VGA descendant decoding: if a bridge
669  * has only one VGA descendant then it can be used to control the VGA routing
670  * for that device. It should always use the bridge closest to the device to
671  * control it. If a bridge has a direct VGA descendant, but also have a sub-
672  * bridge VGA descendant then we cannot use that bridge to control the direct
673  * VGA descendant. So for every device we register, we need to iterate all
674  * its parent bridges so we can invalidate any devices using them properly.
675  */
676 static void vga_arbiter_check_bridge_sharing(struct vga_device *vgadev)
677 {
678 	struct vga_device *same_bridge_vgadev;
679 	struct pci_bus *new_bus, *bus;
680 	struct pci_dev *new_bridge, *bridge;
681 
682 	vgadev->bridge_has_one_vga = true;
683 
684 	if (list_empty(&vga_list)) {
685 		vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n");
686 		return;
687 	}
688 
689 	/* Iterate the new device's bridge hierarchy */
690 	new_bus = vgadev->pdev->bus;
691 	while (new_bus) {
692 		new_bridge = new_bus->self;
693 
694 		/* Go through list of devices already registered */
695 		list_for_each_entry(same_bridge_vgadev, &vga_list, list) {
696 			bus = same_bridge_vgadev->pdev->bus;
697 			bridge = bus->self;
698 
699 			/* See if it shares a bridge with this device */
700 			if (new_bridge == bridge) {
701 				/*
702 				 * If its direct parent bridge is the same
703 				 * as any bridge of this device then it can't
704 				 * be used for that device.
705 				 */
706 				same_bridge_vgadev->bridge_has_one_vga = false;
707 			}
708 
709 			/*
710 			 * Now iterate the previous device's bridge hierarchy.
711 			 * If the new device's parent bridge is in the other
712 			 * device's hierarchy, we can't use it to control this
713 			 * device.
714 			 */
715 			while (bus) {
716 				bridge = bus->self;
717 
718 				if (bridge && bridge == vgadev->pdev->bus->self)
719 					vgadev->bridge_has_one_vga = false;
720 
721 				bus = bus->parent;
722 			}
723 		}
724 		new_bus = new_bus->parent;
725 	}
726 
727 	if (vgadev->bridge_has_one_vga)
728 		vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n");
729 	else
730 		vgaarb_info(&vgadev->pdev->dev, "no bridge control possible\n");
731 }
732 
733 /*
734  * Currently, we assume that the "initial" setup of the system is not sane,
735  * that is, we come up with conflicting devices and let the arbiter's
736  * client decide if devices decodes legacy things or not.
737  */
738 static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
739 {
740 	struct vga_device *vgadev;
741 	unsigned long flags;
742 	struct pci_bus *bus;
743 	struct pci_dev *bridge;
744 	u16 cmd;
745 
746 	/* Allocate structure */
747 	vgadev = kzalloc(sizeof(struct vga_device), GFP_KERNEL);
748 	if (vgadev == NULL) {
749 		vgaarb_err(&pdev->dev, "failed to allocate VGA arbiter data\n");
750 		/*
751 		 * What to do on allocation failure? For now, let's just do
752 		 * nothing, I'm not sure there is anything saner to be done.
753 		 */
754 		return false;
755 	}
756 
757 	/* Take lock & check for duplicates */
758 	spin_lock_irqsave(&vga_lock, flags);
759 	if (vgadev_find(pdev) != NULL) {
760 		BUG_ON(1);
761 		goto fail;
762 	}
763 	vgadev->pdev = pdev;
764 
765 	/* By default, assume we decode everything */
766 	vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
767 			  VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
768 
769 	/* By default, mark it as decoding */
770 	vga_decode_count++;
771 
772 	/*
773 	 * Mark that we "own" resources based on our enables, we will
774 	 * clear that below if the bridge isn't forwarding.
775 	 */
776 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
777 	if (cmd & PCI_COMMAND_IO)
778 		vgadev->owns |= VGA_RSRC_LEGACY_IO;
779 	if (cmd & PCI_COMMAND_MEMORY)
780 		vgadev->owns |= VGA_RSRC_LEGACY_MEM;
781 
782 	/* Check if VGA cycles can get down to us */
783 	bus = pdev->bus;
784 	while (bus) {
785 		bridge = bus->self;
786 		if (bridge) {
787 			u16 l;
788 
789 			pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &l);
790 			if (!(l & PCI_BRIDGE_CTL_VGA)) {
791 				vgadev->owns = 0;
792 				break;
793 			}
794 		}
795 		bus = bus->parent;
796 	}
797 
798 	if (vga_is_boot_device(vgadev)) {
799 		vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
800 			    vga_default_device() ?
801 			    " (overriding previous)" : "");
802 		vga_set_default_device(pdev);
803 	}
804 
805 	vga_arbiter_check_bridge_sharing(vgadev);
806 
807 	/* Add to the list */
808 	list_add_tail(&vgadev->list, &vga_list);
809 	vga_count++;
810 	vgaarb_info(&pdev->dev, "VGA device added: decodes=%s,owns=%s,locks=%s\n",
811 		vga_iostate_to_str(vgadev->decodes),
812 		vga_iostate_to_str(vgadev->owns),
813 		vga_iostate_to_str(vgadev->locks));
814 
815 	spin_unlock_irqrestore(&vga_lock, flags);
816 	return true;
817 fail:
818 	spin_unlock_irqrestore(&vga_lock, flags);
819 	kfree(vgadev);
820 	return false;
821 }
822 
823 static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
824 {
825 	struct vga_device *vgadev;
826 	unsigned long flags;
827 	bool ret = true;
828 
829 	spin_lock_irqsave(&vga_lock, flags);
830 	vgadev = vgadev_find(pdev);
831 	if (vgadev == NULL) {
832 		ret = false;
833 		goto bail;
834 	}
835 
836 	if (vga_default == pdev)
837 		vga_set_default_device(NULL);
838 
839 	if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
840 		vga_decode_count--;
841 
842 	/* Remove entry from list */
843 	list_del(&vgadev->list);
844 	vga_count--;
845 
846 	/* Wake up all possible waiters */
847 	wake_up_all(&vga_wait_queue);
848 bail:
849 	spin_unlock_irqrestore(&vga_lock, flags);
850 	kfree(vgadev);
851 	return ret;
852 }
853 
854 /* Called with the lock */
855 static void vga_update_device_decodes(struct vga_device *vgadev,
856 				      unsigned int new_decodes)
857 {
858 	struct device *dev = &vgadev->pdev->dev;
859 	unsigned int old_decodes = vgadev->decodes;
860 	unsigned int decodes_removed = ~new_decodes & old_decodes;
861 	unsigned int decodes_unlocked = vgadev->locks & decodes_removed;
862 
863 	vgadev->decodes = new_decodes;
864 
865 	vgaarb_info(dev, "VGA decodes changed: olddecodes=%s,decodes=%s:owns=%s\n",
866 		    vga_iostate_to_str(old_decodes),
867 		    vga_iostate_to_str(vgadev->decodes),
868 		    vga_iostate_to_str(vgadev->owns));
869 
870 	/* If we removed locked decodes, lock count goes to zero, and release */
871 	if (decodes_unlocked) {
872 		if (decodes_unlocked & VGA_RSRC_LEGACY_IO)
873 			vgadev->io_lock_cnt = 0;
874 		if (decodes_unlocked & VGA_RSRC_LEGACY_MEM)
875 			vgadev->mem_lock_cnt = 0;
876 		__vga_put(vgadev, decodes_unlocked);
877 	}
878 
879 	/* Change decodes counter */
880 	if (old_decodes & VGA_RSRC_LEGACY_MASK &&
881 	    !(new_decodes & VGA_RSRC_LEGACY_MASK))
882 		vga_decode_count--;
883 	if (!(old_decodes & VGA_RSRC_LEGACY_MASK) &&
884 	    new_decodes & VGA_RSRC_LEGACY_MASK)
885 		vga_decode_count++;
886 	vgaarb_dbg(dev, "decoding count now is: %d\n", vga_decode_count);
887 }
888 
889 static void __vga_set_legacy_decoding(struct pci_dev *pdev,
890 				      unsigned int decodes,
891 				      bool userspace)
892 {
893 	struct vga_device *vgadev;
894 	unsigned long flags;
895 
896 	decodes &= VGA_RSRC_LEGACY_MASK;
897 
898 	spin_lock_irqsave(&vga_lock, flags);
899 	vgadev = vgadev_find(pdev);
900 	if (vgadev == NULL)
901 		goto bail;
902 
903 	/* Don't let userspace futz with kernel driver decodes */
904 	if (userspace && vgadev->set_decode)
905 		goto bail;
906 
907 	/* Update the device decodes + counter */
908 	vga_update_device_decodes(vgadev, decodes);
909 
910 	/*
911 	 * XXX If somebody is going from "doesn't decode" to "decodes"
912 	 * state here, additional care must be taken as we may have pending
913 	 * ownership of non-legacy region.
914 	 */
915 bail:
916 	spin_unlock_irqrestore(&vga_lock, flags);
917 }
918 
919 /**
920  * vga_set_legacy_decoding
921  * @pdev: PCI device of the VGA card
922  * @decodes: bit mask of what legacy regions the card decodes
923  *
924  * Indicate to the arbiter if the card decodes legacy VGA IOs, legacy VGA
925  * Memory, both, or none. All cards default to both, the card driver (fbdev for
926  * example) should tell the arbiter if it has disabled legacy decoding, so the
927  * card can be left out of the arbitration process (and can be safe to take
928  * interrupts at any time.
929  */
930 void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
931 {
932 	__vga_set_legacy_decoding(pdev, decodes, false);
933 }
934 EXPORT_SYMBOL(vga_set_legacy_decoding);
935 
936 /**
937  * vga_client_register - register or unregister a VGA arbitration client
938  * @pdev: PCI device of the VGA client
939  * @set_decode: VGA decode change callback
940  *
941  * Clients have two callback mechanisms they can use.
942  *
943  * @set_decode callback: If a client can disable its GPU VGA resource, it
944  * will get a callback from this to set the encode/decode state.
945  *
946  * Rationale: we cannot disable VGA decode resources unconditionally
947  * because some single GPU laptops seem to require ACPI or BIOS access to
948  * the VGA registers to control things like backlights etc. Hopefully newer
949  * multi-GPU laptops do something saner, and desktops won't have any
950  * special ACPI for this. The driver will get a callback when VGA
951  * arbitration is first used by userspace since some older X servers have
952  * issues.
953  *
954  * Does not check whether a client for @pdev has been registered already.
955  *
956  * To unregister, call vga_client_unregister().
957  *
958  * Returns: 0 on success, -ENODEV on failure
959  */
960 int vga_client_register(struct pci_dev *pdev,
961 		unsigned int (*set_decode)(struct pci_dev *pdev, bool decode))
962 {
963 	unsigned long flags;
964 	struct vga_device *vgadev;
965 
966 	spin_lock_irqsave(&vga_lock, flags);
967 	vgadev = vgadev_find(pdev);
968 	if (vgadev)
969 		vgadev->set_decode = set_decode;
970 	spin_unlock_irqrestore(&vga_lock, flags);
971 	if (!vgadev)
972 		return -ENODEV;
973 	return 0;
974 }
975 EXPORT_SYMBOL(vga_client_register);
976 
977 /*
978  * Char driver implementation
979  *
980  * Semantics is:
981  *
982  *  open       : Open user instance of the arbiter. By default, it's
983  *                attached to the default VGA device of the system.
984  *
985  *  close      : Close user instance, release locks
986  *
987  *  read       : Return a string indicating the status of the target.
988  *                An IO state string is of the form {io,mem,io+mem,none},
989  *                mc and ic are respectively mem and io lock counts (for
990  *                debugging/diagnostic only). "decodes" indicate what the
991  *                card currently decodes, "owns" indicates what is currently
992  *                enabled on it, and "locks" indicates what is locked by this
993  *                card. If the card is unplugged, we get "invalid" then for
994  *                card_ID and an -ENODEV error is returned for any command
995  *                until a new card is targeted
996  *
997  *   "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
998  *
999  * write       : write a command to the arbiter. List of commands is:
1000  *
1001  *   target <card_ID>   : switch target to card <card_ID> (see below)
1002  *   lock <io_state>    : acquire locks on target ("none" is invalid io_state)
1003  *   trylock <io_state> : non-blocking acquire locks on target
1004  *   unlock <io_state>  : release locks on target
1005  *   unlock all         : release all locks on target held by this user
1006  *   decodes <io_state> : set the legacy decoding attributes for the card
1007  *
1008  * poll         : event if something change on any card (not just the target)
1009  *
1010  * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
1011  * to go back to the system default card (TODO: not implemented yet).
1012  * Currently, only PCI is supported as a prefix, but the userland API may
1013  * support other bus types in the future, even if the current kernel
1014  * implementation doesn't.
1015  *
1016  * Note about locks:
1017  *
1018  * The driver keeps track of which user has what locks on which card. It
1019  * supports stacking, like the kernel one. This complicates the implementation
1020  * a bit, but makes the arbiter more tolerant to userspace problems and able
1021  * to properly cleanup in all cases when a process dies.
1022  * Currently, a max of 16 cards simultaneously can have locks issued from
1023  * userspace for a given user (file descriptor instance) of the arbiter.
1024  *
1025  * If the device is hot-unplugged, there is a hook inside the module to notify
1026  * it being added/removed in the system and automatically added/removed in
1027  * the arbiter.
1028  */
1029 
1030 #define MAX_USER_CARDS         CONFIG_VGA_ARB_MAX_GPUS
1031 #define PCI_INVALID_CARD       ((struct pci_dev *)-1UL)
1032 
1033 /* Each user has an array of these, tracking which cards have locks */
1034 struct vga_arb_user_card {
1035 	struct pci_dev *pdev;
1036 	unsigned int mem_cnt;
1037 	unsigned int io_cnt;
1038 };
1039 
1040 struct vga_arb_private {
1041 	struct list_head list;
1042 	struct pci_dev *target;
1043 	struct vga_arb_user_card cards[MAX_USER_CARDS];
1044 	spinlock_t lock;
1045 };
1046 
1047 static LIST_HEAD(vga_user_list);
1048 static DEFINE_SPINLOCK(vga_user_lock);
1049 
1050 
1051 /*
1052  * Take a string in the format: "PCI:domain:bus:dev.fn" and return the
1053  * respective values. If the string is not in this format, return 0.
1054  */
1055 static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
1056 			       unsigned int *bus, unsigned int *devfn)
1057 {
1058 	int n;
1059 	unsigned int slot, func;
1060 
1061 	n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
1062 	if (n != 4)
1063 		return 0;
1064 
1065 	*devfn = PCI_DEVFN(slot, func);
1066 
1067 	return 1;
1068 }
1069 
1070 static ssize_t vga_arb_read(struct file *file, char __user *buf,
1071 			    size_t count, loff_t *ppos)
1072 {
1073 	struct vga_arb_private *priv = file->private_data;
1074 	struct vga_device *vgadev;
1075 	struct pci_dev *pdev;
1076 	unsigned long flags;
1077 	size_t len;
1078 	int rc;
1079 	char *lbuf;
1080 
1081 	lbuf = kmalloc(1024, GFP_KERNEL);
1082 	if (lbuf == NULL)
1083 		return -ENOMEM;
1084 
1085 	/* Protect vga_list */
1086 	spin_lock_irqsave(&vga_lock, flags);
1087 
1088 	/* If we are targeting the default, use it */
1089 	pdev = priv->target;
1090 	if (pdev == NULL || pdev == PCI_INVALID_CARD) {
1091 		spin_unlock_irqrestore(&vga_lock, flags);
1092 		len = sprintf(lbuf, "invalid");
1093 		goto done;
1094 	}
1095 
1096 	/* Find card vgadev structure */
1097 	vgadev = vgadev_find(pdev);
1098 	if (vgadev == NULL) {
1099 		/*
1100 		 * Wow, it's not in the list, that shouldn't happen, let's
1101 		 * fix us up and return invalid card.
1102 		 */
1103 		spin_unlock_irqrestore(&vga_lock, flags);
1104 		len = sprintf(lbuf, "invalid");
1105 		goto done;
1106 	}
1107 
1108 	/* Fill the buffer with info */
1109 	len = snprintf(lbuf, 1024,
1110 		       "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%u:%u)\n",
1111 		       vga_decode_count, pci_name(pdev),
1112 		       vga_iostate_to_str(vgadev->decodes),
1113 		       vga_iostate_to_str(vgadev->owns),
1114 		       vga_iostate_to_str(vgadev->locks),
1115 		       vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
1116 
1117 	spin_unlock_irqrestore(&vga_lock, flags);
1118 done:
1119 
1120 	/* Copy that to user */
1121 	if (len > count)
1122 		len = count;
1123 	rc = copy_to_user(buf, lbuf, len);
1124 	kfree(lbuf);
1125 	if (rc)
1126 		return -EFAULT;
1127 	return len;
1128 }
1129 
1130 /*
1131  * TODO: To avoid parsing inside kernel and to improve the speed we may
1132  * consider use ioctl here
1133  */
1134 static ssize_t vga_arb_write(struct file *file, const char __user *buf,
1135 			     size_t count, loff_t *ppos)
1136 {
1137 	struct vga_arb_private *priv = file->private_data;
1138 	struct vga_arb_user_card *uc = NULL;
1139 	struct pci_dev *pdev;
1140 
1141 	unsigned int io_state;
1142 
1143 	char kbuf[64], *curr_pos;
1144 	size_t remaining = count;
1145 
1146 	int ret_val;
1147 	int i;
1148 
1149 	if (count >= sizeof(kbuf))
1150 		return -EINVAL;
1151 	if (copy_from_user(kbuf, buf, count))
1152 		return -EFAULT;
1153 	curr_pos = kbuf;
1154 	kbuf[count] = '\0';
1155 
1156 	if (strncmp(curr_pos, "lock ", 5) == 0) {
1157 		curr_pos += 5;
1158 		remaining -= 5;
1159 
1160 		pr_debug("client 0x%p called 'lock'\n", priv);
1161 
1162 		if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1163 			ret_val = -EPROTO;
1164 			goto done;
1165 		}
1166 		if (io_state == VGA_RSRC_NONE) {
1167 			ret_val = -EPROTO;
1168 			goto done;
1169 		}
1170 
1171 		pdev = priv->target;
1172 		if (priv->target == NULL) {
1173 			ret_val = -ENODEV;
1174 			goto done;
1175 		}
1176 
1177 		vga_get_uninterruptible(pdev, io_state);
1178 
1179 		/* Update the client's locks lists */
1180 		for (i = 0; i < MAX_USER_CARDS; i++) {
1181 			if (priv->cards[i].pdev == pdev) {
1182 				if (io_state & VGA_RSRC_LEGACY_IO)
1183 					priv->cards[i].io_cnt++;
1184 				if (io_state & VGA_RSRC_LEGACY_MEM)
1185 					priv->cards[i].mem_cnt++;
1186 				break;
1187 			}
1188 		}
1189 
1190 		ret_val = count;
1191 		goto done;
1192 	} else if (strncmp(curr_pos, "unlock ", 7) == 0) {
1193 		curr_pos += 7;
1194 		remaining -= 7;
1195 
1196 		pr_debug("client 0x%p called 'unlock'\n", priv);
1197 
1198 		if (strncmp(curr_pos, "all", 3) == 0)
1199 			io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
1200 		else {
1201 			if (!vga_str_to_iostate
1202 			    (curr_pos, remaining, &io_state)) {
1203 				ret_val = -EPROTO;
1204 				goto done;
1205 			}
1206 			/* TODO: Add this?
1207 			   if (io_state == VGA_RSRC_NONE) {
1208 			   ret_val = -EPROTO;
1209 			   goto done;
1210 			   }
1211 			  */
1212 		}
1213 
1214 		pdev = priv->target;
1215 		if (priv->target == NULL) {
1216 			ret_val = -ENODEV;
1217 			goto done;
1218 		}
1219 		for (i = 0; i < MAX_USER_CARDS; i++) {
1220 			if (priv->cards[i].pdev == pdev)
1221 				uc = &priv->cards[i];
1222 		}
1223 
1224 		if (!uc) {
1225 			ret_val = -EINVAL;
1226 			goto done;
1227 		}
1228 
1229 		if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
1230 			ret_val = -EINVAL;
1231 			goto done;
1232 		}
1233 
1234 		if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
1235 			ret_val = -EINVAL;
1236 			goto done;
1237 		}
1238 
1239 		vga_put(pdev, io_state);
1240 
1241 		if (io_state & VGA_RSRC_LEGACY_IO)
1242 			uc->io_cnt--;
1243 		if (io_state & VGA_RSRC_LEGACY_MEM)
1244 			uc->mem_cnt--;
1245 
1246 		ret_val = count;
1247 		goto done;
1248 	} else if (strncmp(curr_pos, "trylock ", 8) == 0) {
1249 		curr_pos += 8;
1250 		remaining -= 8;
1251 
1252 		pr_debug("client 0x%p called 'trylock'\n", priv);
1253 
1254 		if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1255 			ret_val = -EPROTO;
1256 			goto done;
1257 		}
1258 		/* TODO: Add this?
1259 		   if (io_state == VGA_RSRC_NONE) {
1260 		   ret_val = -EPROTO;
1261 		   goto done;
1262 		   }
1263 		 */
1264 
1265 		pdev = priv->target;
1266 		if (priv->target == NULL) {
1267 			ret_val = -ENODEV;
1268 			goto done;
1269 		}
1270 
1271 		if (vga_tryget(pdev, io_state)) {
1272 			/* Update the client's locks lists... */
1273 			for (i = 0; i < MAX_USER_CARDS; i++) {
1274 				if (priv->cards[i].pdev == pdev) {
1275 					if (io_state & VGA_RSRC_LEGACY_IO)
1276 						priv->cards[i].io_cnt++;
1277 					if (io_state & VGA_RSRC_LEGACY_MEM)
1278 						priv->cards[i].mem_cnt++;
1279 					break;
1280 				}
1281 			}
1282 			ret_val = count;
1283 			goto done;
1284 		} else {
1285 			ret_val = -EBUSY;
1286 			goto done;
1287 		}
1288 
1289 	} else if (strncmp(curr_pos, "target ", 7) == 0) {
1290 		unsigned int domain, bus, devfn;
1291 		struct vga_device *vgadev;
1292 
1293 		curr_pos += 7;
1294 		remaining -= 7;
1295 		pr_debug("client 0x%p called 'target'\n", priv);
1296 		/* If target is default */
1297 		if (!strncmp(curr_pos, "default", 7))
1298 			pdev = pci_dev_get(vga_default_device());
1299 		else {
1300 			if (!vga_pci_str_to_vars(curr_pos, remaining,
1301 						 &domain, &bus, &devfn)) {
1302 				ret_val = -EPROTO;
1303 				goto done;
1304 			}
1305 			pdev = pci_get_domain_bus_and_slot(domain, bus, devfn);
1306 			if (!pdev) {
1307 				pr_debug("invalid PCI address %04x:%02x:%02x.%x\n",
1308 					 domain, bus, PCI_SLOT(devfn),
1309 					 PCI_FUNC(devfn));
1310 				ret_val = -ENODEV;
1311 				goto done;
1312 			}
1313 
1314 			pr_debug("%s ==> %04x:%02x:%02x.%x pdev %p\n", curr_pos,
1315 				domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
1316 				pdev);
1317 		}
1318 
1319 		vgadev = vgadev_find(pdev);
1320 		pr_debug("vgadev %p\n", vgadev);
1321 		if (vgadev == NULL) {
1322 			if (pdev) {
1323 				vgaarb_dbg(&pdev->dev, "not a VGA device\n");
1324 				pci_dev_put(pdev);
1325 			}
1326 
1327 			ret_val = -ENODEV;
1328 			goto done;
1329 		}
1330 
1331 		priv->target = pdev;
1332 		for (i = 0; i < MAX_USER_CARDS; i++) {
1333 			if (priv->cards[i].pdev == pdev)
1334 				break;
1335 			if (priv->cards[i].pdev == NULL) {
1336 				priv->cards[i].pdev = pdev;
1337 				priv->cards[i].io_cnt = 0;
1338 				priv->cards[i].mem_cnt = 0;
1339 				break;
1340 			}
1341 		}
1342 		if (i == MAX_USER_CARDS) {
1343 			vgaarb_dbg(&pdev->dev, "maximum user cards (%d) number reached, ignoring this one!\n",
1344 				MAX_USER_CARDS);
1345 			pci_dev_put(pdev);
1346 			/* XXX: Which value to return? */
1347 			ret_val =  -ENOMEM;
1348 			goto done;
1349 		}
1350 
1351 		ret_val = count;
1352 		pci_dev_put(pdev);
1353 		goto done;
1354 
1355 
1356 	} else if (strncmp(curr_pos, "decodes ", 8) == 0) {
1357 		curr_pos += 8;
1358 		remaining -= 8;
1359 		pr_debug("client 0x%p called 'decodes'\n", priv);
1360 
1361 		if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1362 			ret_val = -EPROTO;
1363 			goto done;
1364 		}
1365 		pdev = priv->target;
1366 		if (priv->target == NULL) {
1367 			ret_val = -ENODEV;
1368 			goto done;
1369 		}
1370 
1371 		__vga_set_legacy_decoding(pdev, io_state, true);
1372 		ret_val = count;
1373 		goto done;
1374 	}
1375 	/* If we got here, the message written is not part of the protocol! */
1376 	return -EPROTO;
1377 
1378 done:
1379 	return ret_val;
1380 }
1381 
1382 static __poll_t vga_arb_fpoll(struct file *file, poll_table *wait)
1383 {
1384 	pr_debug("%s\n", __func__);
1385 
1386 	poll_wait(file, &vga_wait_queue, wait);
1387 	return EPOLLIN;
1388 }
1389 
1390 static int vga_arb_open(struct inode *inode, struct file *file)
1391 {
1392 	struct vga_arb_private *priv;
1393 	unsigned long flags;
1394 
1395 	pr_debug("%s\n", __func__);
1396 
1397 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1398 	if (priv == NULL)
1399 		return -ENOMEM;
1400 	spin_lock_init(&priv->lock);
1401 	file->private_data = priv;
1402 
1403 	spin_lock_irqsave(&vga_user_lock, flags);
1404 	list_add(&priv->list, &vga_user_list);
1405 	spin_unlock_irqrestore(&vga_user_lock, flags);
1406 
1407 	/* Set the client's lists of locks */
1408 	priv->target = vga_default_device(); /* Maybe this is still null! */
1409 	priv->cards[0].pdev = priv->target;
1410 	priv->cards[0].io_cnt = 0;
1411 	priv->cards[0].mem_cnt = 0;
1412 
1413 	return 0;
1414 }
1415 
1416 static int vga_arb_release(struct inode *inode, struct file *file)
1417 {
1418 	struct vga_arb_private *priv = file->private_data;
1419 	struct vga_arb_user_card *uc;
1420 	unsigned long flags;
1421 	int i;
1422 
1423 	pr_debug("%s\n", __func__);
1424 
1425 	spin_lock_irqsave(&vga_user_lock, flags);
1426 	list_del(&priv->list);
1427 	for (i = 0; i < MAX_USER_CARDS; i++) {
1428 		uc = &priv->cards[i];
1429 		if (uc->pdev == NULL)
1430 			continue;
1431 		vgaarb_dbg(&uc->pdev->dev, "uc->io_cnt == %d, uc->mem_cnt == %d\n",
1432 			uc->io_cnt, uc->mem_cnt);
1433 		while (uc->io_cnt--)
1434 			vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
1435 		while (uc->mem_cnt--)
1436 			vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
1437 	}
1438 	spin_unlock_irqrestore(&vga_user_lock, flags);
1439 
1440 	kfree(priv);
1441 
1442 	return 0;
1443 }
1444 
1445 /*
1446  * Callback any registered clients to let them know we have a change in VGA
1447  * cards.
1448  */
1449 static void vga_arbiter_notify_clients(void)
1450 {
1451 	struct vga_device *vgadev;
1452 	unsigned long flags;
1453 	unsigned int new_decodes;
1454 	bool new_state;
1455 
1456 	if (!vga_arbiter_used)
1457 		return;
1458 
1459 	new_state = (vga_count > 1) ? false : true;
1460 
1461 	spin_lock_irqsave(&vga_lock, flags);
1462 	list_for_each_entry(vgadev, &vga_list, list) {
1463 		if (vgadev->set_decode) {
1464 			new_decodes = vgadev->set_decode(vgadev->pdev,
1465 							 new_state);
1466 			vga_update_device_decodes(vgadev, new_decodes);
1467 		}
1468 	}
1469 	spin_unlock_irqrestore(&vga_lock, flags);
1470 }
1471 
1472 static int pci_notify(struct notifier_block *nb, unsigned long action,
1473 		      void *data)
1474 {
1475 	struct device *dev = data;
1476 	struct pci_dev *pdev = to_pci_dev(dev);
1477 	bool notify = false;
1478 
1479 	vgaarb_dbg(dev, "%s\n", __func__);
1480 
1481 	/* Only deal with VGA class devices */
1482 	if (!pci_is_vga(pdev))
1483 		return 0;
1484 
1485 	/*
1486 	 * For now, we're only interested in devices added and removed.
1487 	 * I didn't test this thing here, so someone needs to double check
1488 	 * for the cases of hot-pluggable VGA cards.
1489 	 */
1490 	if (action == BUS_NOTIFY_ADD_DEVICE)
1491 		notify = vga_arbiter_add_pci_device(pdev);
1492 	else if (action == BUS_NOTIFY_DEL_DEVICE)
1493 		notify = vga_arbiter_del_pci_device(pdev);
1494 
1495 	if (notify)
1496 		vga_arbiter_notify_clients();
1497 	return 0;
1498 }
1499 
1500 static struct notifier_block pci_notifier = {
1501 	.notifier_call = pci_notify,
1502 };
1503 
1504 static const struct file_operations vga_arb_device_fops = {
1505 	.read = vga_arb_read,
1506 	.write = vga_arb_write,
1507 	.poll = vga_arb_fpoll,
1508 	.open = vga_arb_open,
1509 	.release = vga_arb_release,
1510 	.llseek = noop_llseek,
1511 };
1512 
1513 static struct miscdevice vga_arb_device = {
1514 	MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
1515 };
1516 
1517 static int __init vga_arb_device_init(void)
1518 {
1519 	int rc;
1520 	struct pci_dev *pdev;
1521 
1522 	rc = misc_register(&vga_arb_device);
1523 	if (rc < 0)
1524 		pr_err("error %d registering device\n", rc);
1525 
1526 	bus_register_notifier(&pci_bus_type, &pci_notifier);
1527 
1528 	/* Add all VGA class PCI devices by default */
1529 	pdev = NULL;
1530 	while ((pdev =
1531 		pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
1532 			       PCI_ANY_ID, pdev)) != NULL) {
1533 		if (pci_is_vga(pdev))
1534 			vga_arbiter_add_pci_device(pdev);
1535 	}
1536 
1537 	pr_info("loaded\n");
1538 	return rc;
1539 }
1540 subsys_initcall_sync(vga_arb_device_init);
1541