• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * vgaarb.c: Implements the VGA arbitration. For details refer to
3  * Documentation/vgaarbiter.txt
4  *
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  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice (including the next
18  * paragraph) shall be included in all copies or substantial portions of the
19  * Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS
28  * IN THE SOFTWARE.
29  *
30  */
31 
32 #define pr_fmt(fmt) "vgaarb: " fmt
33 
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/pci.h>
37 #include <linux/errno.h>
38 #include <linux/init.h>
39 #include <linux/list.h>
40 #include <linux/sched.h>
41 #include <linux/wait.h>
42 #include <linux/spinlock.h>
43 #include <linux/poll.h>
44 #include <linux/miscdevice.h>
45 #include <linux/slab.h>
46 #include <linux/screen_info.h>
47 
48 #include <linux/uaccess.h>
49 
50 #include <linux/vgaarb.h>
51 
52 static void vga_arbiter_notify_clients(void);
53 /*
54  * We keep a list of all vga devices in the system to speed
55  * up the various operations of the arbiter
56  */
57 struct vga_device {
58 	struct list_head list;
59 	struct pci_dev *pdev;
60 	unsigned int decodes;	/* what does it decodes */
61 	unsigned int owns;	/* what does it owns */
62 	unsigned int locks;	/* what does it locks */
63 	unsigned int io_lock_cnt;	/* legacy IO lock count */
64 	unsigned int mem_lock_cnt;	/* legacy MEM lock count */
65 	unsigned int io_norm_cnt;	/* normal IO count */
66 	unsigned int mem_norm_cnt;	/* normal MEM count */
67 	bool bridge_has_one_vga;
68 	/* allow IRQ enable/disable hook */
69 	void *cookie;
70 	void (*irq_set_state)(void *cookie, bool enable);
71 	unsigned int (*set_vga_decode)(void *cookie, bool decode);
72 };
73 
74 static LIST_HEAD(vga_list);
75 static int vga_count, vga_decode_count;
76 static bool vga_arbiter_used;
77 static DEFINE_SPINLOCK(vga_lock);
78 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
79 
80 
vga_iostate_to_str(unsigned int iostate)81 static const char *vga_iostate_to_str(unsigned int iostate)
82 {
83 	/* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
84 	iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
85 	switch (iostate) {
86 	case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
87 		return "io+mem";
88 	case VGA_RSRC_LEGACY_IO:
89 		return "io";
90 	case VGA_RSRC_LEGACY_MEM:
91 		return "mem";
92 	}
93 	return "none";
94 }
95 
vga_str_to_iostate(char * buf,int str_size,int * io_state)96 static int vga_str_to_iostate(char *buf, int str_size, int *io_state)
97 {
98 	/* we could in theory hand out locks on IO and mem
99 	 * separately to userspace but it can cause deadlocks */
100 	if (strncmp(buf, "none", 4) == 0) {
101 		*io_state = VGA_RSRC_NONE;
102 		return 1;
103 	}
104 
105 	/* XXX We're not chekcing the str_size! */
106 	if (strncmp(buf, "io+mem", 6) == 0)
107 		goto both;
108 	else if (strncmp(buf, "io", 2) == 0)
109 		goto both;
110 	else if (strncmp(buf, "mem", 3) == 0)
111 		goto both;
112 	return 0;
113 both:
114 	*io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
115 	return 1;
116 }
117 
118 /* this is only used a cookie - it should not be dereferenced */
119 static struct pci_dev *vga_default;
120 
121 static void vga_arb_device_card_gone(struct pci_dev *pdev);
122 
123 /* Find somebody in our list */
vgadev_find(struct pci_dev * pdev)124 static struct vga_device *vgadev_find(struct pci_dev *pdev)
125 {
126 	struct vga_device *vgadev;
127 
128 	list_for_each_entry(vgadev, &vga_list, list)
129 		if (pdev == vgadev->pdev)
130 			return vgadev;
131 	return NULL;
132 }
133 
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  */
vga_default_device(void)152 struct pci_dev *vga_default_device(void)
153 {
154 	return vga_default;
155 }
156 EXPORT_SYMBOL_GPL(vga_default_device);
157 
vga_set_default_device(struct pci_dev * pdev)158 void vga_set_default_device(struct pci_dev *pdev)
159 {
160 	if (vga_default == pdev)
161 		return;
162 
163 	pci_dev_put(vga_default);
164 	vga_default = pci_dev_get(pdev);
165 }
166 
vga_irq_set_state(struct vga_device * vgadev,bool state)167 static inline void vga_irq_set_state(struct vga_device *vgadev, bool state)
168 {
169 	if (vgadev->irq_set_state)
170 		vgadev->irq_set_state(vgadev->cookie, state);
171 }
172 
173 
174 /* If we don't ever use VGA arb we should avoid
175    turning off anything anywhere due to old X servers getting
176    confused about the boot device not being VGA */
vga_check_first_use(void)177 static void vga_check_first_use(void)
178 {
179 	/* we should inform all GPUs in the system that
180 	 * VGA arb has occurred and to try and disable resources
181 	 * if they can */
182 	if (!vga_arbiter_used) {
183 		vga_arbiter_used = true;
184 		vga_arbiter_notify_clients();
185 	}
186 }
187 
__vga_tryget(struct vga_device * vgadev,unsigned int rsrc)188 static struct vga_device *__vga_tryget(struct vga_device *vgadev,
189 				       unsigned int rsrc)
190 {
191 	unsigned int wants, legacy_wants, match;
192 	struct vga_device *conflict;
193 	unsigned int pci_bits;
194 	u32 flags = 0;
195 
196 	/* Account for "normal" resources to lock. If we decode the legacy,
197 	 * counterpart, we need to request it as well
198 	 */
199 	if ((rsrc & VGA_RSRC_NORMAL_IO) &&
200 	    (vgadev->decodes & VGA_RSRC_LEGACY_IO))
201 		rsrc |= VGA_RSRC_LEGACY_IO;
202 	if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
203 	    (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
204 		rsrc |= VGA_RSRC_LEGACY_MEM;
205 
206 	pr_debug("%s: %d\n", __func__, rsrc);
207 	pr_debug("%s: owns: %d\n", __func__, vgadev->owns);
208 
209 	/* Check what resources we need to acquire */
210 	wants = rsrc & ~vgadev->owns;
211 
212 	/* We already own everything, just mark locked & bye bye */
213 	if (wants == 0)
214 		goto lock_them;
215 
216 	/* We don't need to request a legacy resource, we just enable
217 	 * appropriate decoding and go
218 	 */
219 	legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
220 	if (legacy_wants == 0)
221 		goto enable_them;
222 
223 	/* Ok, we don't, let's find out how we need to kick off */
224 	list_for_each_entry(conflict, &vga_list, list) {
225 		unsigned int lwants = legacy_wants;
226 		unsigned int change_bridge = 0;
227 
228 		/* Don't conflict with myself */
229 		if (vgadev == conflict)
230 			continue;
231 
232 		/* Check if the architecture allows a conflict between those
233 		 * 2 devices or if they are on separate domains
234 		 */
235 		if (!vga_conflicts(vgadev->pdev, conflict->pdev))
236 			continue;
237 
238 		/* We have a possible conflict. before we go further, we must
239 		 * check if we sit on the same bus as the conflicting device.
240 		 * if we don't, then we must tie both IO and MEM resources
241 		 * together since there is only a single bit controlling
242 		 * VGA forwarding on P2P bridges
243 		 */
244 		if (vgadev->pdev->bus != conflict->pdev->bus) {
245 			change_bridge = 1;
246 			lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
247 		}
248 
249 		/* Check if the guy has a lock on the resource. If he does,
250 		 * return the conflicting entry
251 		 */
252 		if (conflict->locks & lwants)
253 			return conflict;
254 
255 		/* Ok, now check if it owns the resource we want.  We can
256 		 * lock resources that are not decoded, therefore a device
257 		 * can own resources it doesn't decode.
258 		 */
259 		match = lwants & conflict->owns;
260 		if (!match)
261 			continue;
262 
263 		/* looks like he doesn't have a lock, we can steal
264 		 * them from him
265 		 */
266 
267 		flags = 0;
268 		pci_bits = 0;
269 
270 		/* If we can't control legacy resources via the bridge, we
271 		 * also need to disable normal decoding.
272 		 */
273 		if (!conflict->bridge_has_one_vga) {
274 			if ((match & conflict->decodes) & VGA_RSRC_LEGACY_MEM)
275 				pci_bits |= PCI_COMMAND_MEMORY;
276 			if ((match & conflict->decodes) & VGA_RSRC_LEGACY_IO)
277 				pci_bits |= PCI_COMMAND_IO;
278 
279 			if (pci_bits) {
280 				vga_irq_set_state(conflict, false);
281 				flags |= PCI_VGA_STATE_CHANGE_DECODES;
282 			}
283 		}
284 
285 		if (change_bridge)
286 			flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
287 
288 		pci_set_vga_state(conflict->pdev, false, pci_bits, flags);
289 		conflict->owns &= ~match;
290 
291 		/* If we disabled normal decoding, reflect it in owns */
292 		if (pci_bits & PCI_COMMAND_MEMORY)
293 			conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
294 		if (pci_bits & PCI_COMMAND_IO)
295 			conflict->owns &= ~VGA_RSRC_NORMAL_IO;
296 	}
297 
298 enable_them:
299 	/* ok dude, we got it, everybody conflicting has been disabled, let's
300 	 * enable us.  Mark any bits in "owns" regardless of whether we
301 	 * decoded them.  We can lock resources we don't decode, therefore
302 	 * we must track them via "owns".
303 	 */
304 	flags = 0;
305 	pci_bits = 0;
306 
307 	if (!vgadev->bridge_has_one_vga) {
308 		flags |= PCI_VGA_STATE_CHANGE_DECODES;
309 		if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
310 			pci_bits |= PCI_COMMAND_MEMORY;
311 		if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
312 			pci_bits |= PCI_COMMAND_IO;
313 	}
314 	if (wants & VGA_RSRC_LEGACY_MASK)
315 		flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
316 
317 	pci_set_vga_state(vgadev->pdev, true, pci_bits, flags);
318 
319 	if (!vgadev->bridge_has_one_vga)
320 		vga_irq_set_state(vgadev, true);
321 
322 	vgadev->owns |= wants;
323 lock_them:
324 	vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
325 	if (rsrc & VGA_RSRC_LEGACY_IO)
326 		vgadev->io_lock_cnt++;
327 	if (rsrc & VGA_RSRC_LEGACY_MEM)
328 		vgadev->mem_lock_cnt++;
329 	if (rsrc & VGA_RSRC_NORMAL_IO)
330 		vgadev->io_norm_cnt++;
331 	if (rsrc & VGA_RSRC_NORMAL_MEM)
332 		vgadev->mem_norm_cnt++;
333 
334 	return NULL;
335 }
336 
__vga_put(struct vga_device * vgadev,unsigned int rsrc)337 static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
338 {
339 	unsigned int old_locks = vgadev->locks;
340 
341 	pr_debug("%s\n", __func__);
342 
343 	/* Update our counters, and account for equivalent legacy resources
344 	 * if we decode them
345 	 */
346 	if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
347 		vgadev->io_norm_cnt--;
348 		if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
349 			rsrc |= VGA_RSRC_LEGACY_IO;
350 	}
351 	if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
352 		vgadev->mem_norm_cnt--;
353 		if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
354 			rsrc |= VGA_RSRC_LEGACY_MEM;
355 	}
356 	if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
357 		vgadev->io_lock_cnt--;
358 	if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
359 		vgadev->mem_lock_cnt--;
360 
361 	/* Just clear lock bits, we do lazy operations so we don't really
362 	 * have to bother about anything else at this point
363 	 */
364 	if (vgadev->io_lock_cnt == 0)
365 		vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
366 	if (vgadev->mem_lock_cnt == 0)
367 		vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
368 
369 	/* Kick the wait queue in case somebody was waiting if we actually
370 	 * released something
371 	 */
372 	if (old_locks != vgadev->locks)
373 		wake_up_all(&vga_wait_queue);
374 }
375 
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  */
vga_get(struct pci_dev * pdev,unsigned int rsrc,int interruptible)410 int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
411 {
412 	struct vga_device *vgadev, *conflict;
413 	unsigned long flags;
414 	wait_queue_t wait;
415 	int rc = 0;
416 
417 	vga_check_first_use();
418 	/* The one who calls us should check for this, but lets be sure... */
419 	if (pdev == NULL)
420 		pdev = vga_default_device();
421 	if (pdev == NULL)
422 		return 0;
423 
424 	for (;;) {
425 		spin_lock_irqsave(&vga_lock, flags);
426 		vgadev = vgadev_find(pdev);
427 		if (vgadev == NULL) {
428 			spin_unlock_irqrestore(&vga_lock, flags);
429 			rc = -ENODEV;
430 			break;
431 		}
432 		conflict = __vga_tryget(vgadev, rsrc);
433 		spin_unlock_irqrestore(&vga_lock, flags);
434 		if (conflict == NULL)
435 			break;
436 
437 
438 		/* We have a conflict, we wait until somebody kicks the
439 		 * work queue. Currently we have one work queue that we
440 		 * kick each time some resources are released, but it would
441 		 * be fairly easy to have a per device one so that we only
442 		 * need to attach to the conflicting device
443 		 */
444 		init_waitqueue_entry(&wait, current);
445 		add_wait_queue(&vga_wait_queue, &wait);
446 		set_current_state(interruptible ?
447 				  TASK_INTERRUPTIBLE :
448 				  TASK_UNINTERRUPTIBLE);
449 		if (interruptible && signal_pending(current)) {
450 			__set_current_state(TASK_RUNNING);
451 			remove_wait_queue(&vga_wait_queue, &wait);
452 			rc = -ERESTARTSYS;
453 			break;
454 		}
455 		schedule();
456 		remove_wait_queue(&vga_wait_queue, &wait);
457 	}
458 	return rc;
459 }
460 EXPORT_SYMBOL(vga_get);
461 
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  */
vga_tryget(struct pci_dev * pdev,unsigned int rsrc)477 int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
478 {
479 	struct vga_device *vgadev;
480 	unsigned long flags;
481 	int rc = 0;
482 
483 	vga_check_first_use();
484 
485 	/* The one who calls us should check for this, but lets be sure... */
486 	if (pdev == NULL)
487 		pdev = vga_default_device();
488 	if (pdev == NULL)
489 		return 0;
490 	spin_lock_irqsave(&vga_lock, flags);
491 	vgadev = vgadev_find(pdev);
492 	if (vgadev == NULL) {
493 		rc = -ENODEV;
494 		goto bail;
495 	}
496 	if (__vga_tryget(vgadev, rsrc))
497 		rc = -EBUSY;
498 bail:
499 	spin_unlock_irqrestore(&vga_lock, flags);
500 	return rc;
501 }
502 EXPORT_SYMBOL(vga_tryget);
503 
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  */
vga_put(struct pci_dev * pdev,unsigned int rsrc)514 void vga_put(struct pci_dev *pdev, unsigned int rsrc)
515 {
516 	struct vga_device *vgadev;
517 	unsigned long flags;
518 
519 	/* The one who calls us should check for this, but lets be sure... */
520 	if (pdev == NULL)
521 		pdev = vga_default_device();
522 	if (pdev == NULL)
523 		return;
524 	spin_lock_irqsave(&vga_lock, flags);
525 	vgadev = vgadev_find(pdev);
526 	if (vgadev == NULL)
527 		goto bail;
528 	__vga_put(vgadev, rsrc);
529 bail:
530 	spin_unlock_irqrestore(&vga_lock, flags);
531 }
532 EXPORT_SYMBOL(vga_put);
533 
534 /*
535  * Rules for using a bridge to control a VGA descendant decoding: if a bridge
536  * has only one VGA descendant then it can be used to control the VGA routing
537  * for that device. It should always use the bridge closest to the device to
538  * control it. If a bridge has a direct VGA descendant, but also have a sub-
539  * bridge VGA descendant then we cannot use that bridge to control the direct
540  * VGA descendant. So for every device we register, we need to iterate all
541  * its parent bridges so we can invalidate any devices using them properly.
542  */
vga_arbiter_check_bridge_sharing(struct vga_device * vgadev)543 static void vga_arbiter_check_bridge_sharing(struct vga_device *vgadev)
544 {
545 	struct vga_device *same_bridge_vgadev;
546 	struct pci_bus *new_bus, *bus;
547 	struct pci_dev *new_bridge, *bridge;
548 
549 	vgadev->bridge_has_one_vga = true;
550 
551 	if (list_empty(&vga_list))
552 		return;
553 
554 	/* okay iterate the new devices bridge hierarachy */
555 	new_bus = vgadev->pdev->bus;
556 	while (new_bus) {
557 		new_bridge = new_bus->self;
558 
559 		/* go through list of devices already registered */
560 		list_for_each_entry(same_bridge_vgadev, &vga_list, list) {
561 			bus = same_bridge_vgadev->pdev->bus;
562 			bridge = bus->self;
563 
564 			/* see if the share a bridge with this device */
565 			if (new_bridge == bridge) {
566 				/*
567 				 * If their direct parent bridge is the same
568 				 * as any bridge of this device then it can't
569 				 * be used for that device.
570 				 */
571 				same_bridge_vgadev->bridge_has_one_vga = false;
572 			}
573 
574 			/*
575 			 * Now iterate the previous devices bridge hierarchy.
576 			 * If the new devices parent bridge is in the other
577 			 * devices hierarchy then we can't use it to control
578 			 * this device
579 			 */
580 			while (bus) {
581 				bridge = bus->self;
582 
583 				if (bridge && bridge == vgadev->pdev->bus->self)
584 					vgadev->bridge_has_one_vga = false;
585 
586 				bus = bus->parent;
587 			}
588 		}
589 		new_bus = new_bus->parent;
590 	}
591 }
592 
593 /*
594  * Currently, we assume that the "initial" setup of the system is
595  * not sane, that is we come up with conflicting devices and let
596  * the arbiter's client decides if devices decodes or not legacy
597  * things.
598  */
vga_arbiter_add_pci_device(struct pci_dev * pdev)599 static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
600 {
601 	struct vga_device *vgadev;
602 	unsigned long flags;
603 	struct pci_bus *bus;
604 	struct pci_dev *bridge;
605 	u16 cmd;
606 
607 	/* Only deal with VGA class devices */
608 	if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
609 		return false;
610 
611 	/* Allocate structure */
612 	vgadev = kzalloc(sizeof(struct vga_device), GFP_KERNEL);
613 	if (vgadev == NULL) {
614 		pr_err("failed to allocate pci device\n");
615 		/*
616 		 * What to do on allocation failure ? For now, let's just do
617 		 * nothing, I'm not sure there is anything saner to be done.
618 		 */
619 		return false;
620 	}
621 
622 	/* Take lock & check for duplicates */
623 	spin_lock_irqsave(&vga_lock, flags);
624 	if (vgadev_find(pdev) != NULL) {
625 		BUG_ON(1);
626 		goto fail;
627 	}
628 	vgadev->pdev = pdev;
629 
630 	/* By default, assume we decode everything */
631 	vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
632 			  VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
633 
634 	/* by default mark it as decoding */
635 	vga_decode_count++;
636 	/* Mark that we "own" resources based on our enables, we will
637 	 * clear that below if the bridge isn't forwarding
638 	 */
639 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
640 	if (cmd & PCI_COMMAND_IO)
641 		vgadev->owns |= VGA_RSRC_LEGACY_IO;
642 	if (cmd & PCI_COMMAND_MEMORY)
643 		vgadev->owns |= VGA_RSRC_LEGACY_MEM;
644 
645 	/* Check if VGA cycles can get down to us */
646 	bus = pdev->bus;
647 	while (bus) {
648 		bridge = bus->self;
649 		if (bridge) {
650 			u16 l;
651 
652 			pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &l);
653 			if (!(l & PCI_BRIDGE_CTL_VGA)) {
654 				vgadev->owns = 0;
655 				break;
656 			}
657 		}
658 		bus = bus->parent;
659 	}
660 
661 	/* Deal with VGA default device. Use first enabled one
662 	 * by default if arch doesn't have it's own hook
663 	 */
664 	if (vga_default == NULL &&
665 	    ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)) {
666 		pr_info("setting as boot device: PCI:%s\n", pci_name(pdev));
667 		vga_set_default_device(pdev);
668 	}
669 
670 	vga_arbiter_check_bridge_sharing(vgadev);
671 
672 	/* Add to the list */
673 	list_add(&vgadev->list, &vga_list);
674 	vga_count++;
675 	pr_info("device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
676 		pci_name(pdev),
677 		vga_iostate_to_str(vgadev->decodes),
678 		vga_iostate_to_str(vgadev->owns),
679 		vga_iostate_to_str(vgadev->locks));
680 
681 	spin_unlock_irqrestore(&vga_lock, flags);
682 	return true;
683 fail:
684 	spin_unlock_irqrestore(&vga_lock, flags);
685 	kfree(vgadev);
686 	return false;
687 }
688 
vga_arbiter_del_pci_device(struct pci_dev * pdev)689 static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
690 {
691 	struct vga_device *vgadev;
692 	unsigned long flags;
693 	bool ret = true;
694 
695 	spin_lock_irqsave(&vga_lock, flags);
696 	vgadev = vgadev_find(pdev);
697 	if (vgadev == NULL) {
698 		ret = false;
699 		goto bail;
700 	}
701 
702 	if (vga_default == pdev)
703 		vga_set_default_device(NULL);
704 
705 	if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
706 		vga_decode_count--;
707 
708 	/* Remove entry from list */
709 	list_del(&vgadev->list);
710 	vga_count--;
711 	/* Notify userland driver that the device is gone so it discards
712 	 * it's copies of the pci_dev pointer
713 	 */
714 	vga_arb_device_card_gone(pdev);
715 
716 	/* Wake up all possible waiters */
717 	wake_up_all(&vga_wait_queue);
718 bail:
719 	spin_unlock_irqrestore(&vga_lock, flags);
720 	kfree(vgadev);
721 	return ret;
722 }
723 
724 /* this is called with the lock */
vga_update_device_decodes(struct vga_device * vgadev,int new_decodes)725 static inline void vga_update_device_decodes(struct vga_device *vgadev,
726 					     int new_decodes)
727 {
728 	int old_decodes, decodes_removed, decodes_unlocked;
729 
730 	old_decodes = vgadev->decodes;
731 	decodes_removed = ~new_decodes & old_decodes;
732 	decodes_unlocked = vgadev->locks & decodes_removed;
733 	vgadev->decodes = new_decodes;
734 
735 	pr_info("device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
736 		pci_name(vgadev->pdev),
737 		vga_iostate_to_str(old_decodes),
738 		vga_iostate_to_str(vgadev->decodes),
739 		vga_iostate_to_str(vgadev->owns));
740 
741 	/* if we removed locked decodes, lock count goes to zero, and release */
742 	if (decodes_unlocked) {
743 		if (decodes_unlocked & VGA_RSRC_LEGACY_IO)
744 			vgadev->io_lock_cnt = 0;
745 		if (decodes_unlocked & VGA_RSRC_LEGACY_MEM)
746 			vgadev->mem_lock_cnt = 0;
747 		__vga_put(vgadev, decodes_unlocked);
748 	}
749 
750 	/* change decodes counter */
751 	if (old_decodes & VGA_RSRC_LEGACY_MASK &&
752 	    !(new_decodes & VGA_RSRC_LEGACY_MASK))
753 		vga_decode_count--;
754 	if (!(old_decodes & VGA_RSRC_LEGACY_MASK) &&
755 	    new_decodes & VGA_RSRC_LEGACY_MASK)
756 		vga_decode_count++;
757 	pr_debug("decoding count now is: %d\n", vga_decode_count);
758 }
759 
__vga_set_legacy_decoding(struct pci_dev * pdev,unsigned int decodes,bool userspace)760 static void __vga_set_legacy_decoding(struct pci_dev *pdev,
761 				      unsigned int decodes,
762 				      bool userspace)
763 {
764 	struct vga_device *vgadev;
765 	unsigned long flags;
766 
767 	decodes &= VGA_RSRC_LEGACY_MASK;
768 
769 	spin_lock_irqsave(&vga_lock, flags);
770 	vgadev = vgadev_find(pdev);
771 	if (vgadev == NULL)
772 		goto bail;
773 
774 	/* don't let userspace futz with kernel driver decodes */
775 	if (userspace && vgadev->set_vga_decode)
776 		goto bail;
777 
778 	/* update the device decodes + counter */
779 	vga_update_device_decodes(vgadev, decodes);
780 
781 	/* XXX if somebody is going from "doesn't decode" to "decodes" state
782 	 * here, additional care must be taken as we may have pending owner
783 	 * ship of non-legacy region ...
784 	 */
785 bail:
786 	spin_unlock_irqrestore(&vga_lock, flags);
787 }
788 
vga_set_legacy_decoding(struct pci_dev * pdev,unsigned int decodes)789 void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
790 {
791 	__vga_set_legacy_decoding(pdev, decodes, false);
792 }
793 EXPORT_SYMBOL(vga_set_legacy_decoding);
794 
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  */
vga_client_register(struct pci_dev * pdev,void * cookie,void (* irq_set_state)(void * cookie,bool state),unsigned int (* set_vga_decode)(void * cookie,bool decode))826 int vga_client_register(struct pci_dev *pdev, void *cookie,
827 			void (*irq_set_state)(void *cookie, bool state),
828 			unsigned int (*set_vga_decode)(void *cookie,
829 						       bool decode))
830 {
831 	int ret = -ENODEV;
832 	struct vga_device *vgadev;
833 	unsigned long flags;
834 
835 	spin_lock_irqsave(&vga_lock, flags);
836 	vgadev = vgadev_find(pdev);
837 	if (!vgadev)
838 		goto bail;
839 
840 	vgadev->irq_set_state = irq_set_state;
841 	vgadev->set_vga_decode = set_vga_decode;
842 	vgadev->cookie = cookie;
843 	ret = 0;
844 
845 bail:
846 	spin_unlock_irqrestore(&vga_lock, flags);
847 	return ret;
848 
849 }
850 EXPORT_SYMBOL(vga_client_register);
851 
852 /*
853  * Char driver implementation
854  *
855  * Semantics is:
856  *
857  *  open       : open user instance of the arbitrer. by default, it's
858  *                attached to the default VGA device of the system.
859  *
860  *  close      : close user instance, release locks
861  *
862  *  read       : return a string indicating the status of the target.
863  *                an IO state string is of the form {io,mem,io+mem,none},
864  *                mc and ic are respectively mem and io lock counts (for
865  *                debugging/diagnostic only). "decodes" indicate what the
866  *                card currently decodes, "owns" indicates what is currently
867  *                enabled on it, and "locks" indicates what is locked by this
868  *                card. If the card is unplugged, we get "invalid" then for
869  *                card_ID and an -ENODEV error is returned for any command
870  *                until a new card is targeted
871  *
872  *   "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
873  *
874  * write       : write a command to the arbiter. List of commands is:
875  *
876  *   target <card_ID>   : switch target to card <card_ID> (see below)
877  *   lock <io_state>    : acquires locks on target ("none" is invalid io_state)
878  *   trylock <io_state> : non-blocking acquire locks on target
879  *   unlock <io_state>  : release locks on target
880  *   unlock all         : release all locks on target held by this user
881  *   decodes <io_state> : set the legacy decoding attributes for the card
882  *
883  * poll         : event if something change on any card (not just the target)
884  *
885  * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
886  * to go back to the system default card (TODO: not implemented yet).
887  * Currently, only PCI is supported as a prefix, but the userland API may
888  * support other bus types in the future, even if the current kernel
889  * implementation doesn't.
890  *
891  * Note about locks:
892  *
893  * The driver keeps track of which user has what locks on which card. It
894  * supports stacking, like the kernel one. This complexifies the implementation
895  * a bit, but makes the arbiter more tolerant to userspace problems and able
896  * to properly cleanup in all cases when a process dies.
897  * Currently, a max of 16 cards simultaneously can have locks issued from
898  * userspace for a given user (file descriptor instance) of the arbiter.
899  *
900  * If the device is hot-unplugged, there is a hook inside the module to notify
901  * they being added/removed in the system and automatically added/removed in
902  * the arbiter.
903  */
904 
905 #define MAX_USER_CARDS         CONFIG_VGA_ARB_MAX_GPUS
906 #define PCI_INVALID_CARD       ((struct pci_dev *)-1UL)
907 
908 /*
909  * Each user has an array of these, tracking which cards have locks
910  */
911 struct vga_arb_user_card {
912 	struct pci_dev *pdev;
913 	unsigned int mem_cnt;
914 	unsigned int io_cnt;
915 };
916 
917 struct vga_arb_private {
918 	struct list_head list;
919 	struct pci_dev *target;
920 	struct vga_arb_user_card cards[MAX_USER_CARDS];
921 	spinlock_t lock;
922 };
923 
924 static LIST_HEAD(vga_user_list);
925 static DEFINE_SPINLOCK(vga_user_lock);
926 
927 
928 /*
929  * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
930  * returns the respective values. If the string is not in this format,
931  * it returns 0.
932  */
vga_pci_str_to_vars(char * buf,int count,unsigned int * domain,unsigned int * bus,unsigned int * devfn)933 static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
934 			       unsigned int *bus, unsigned int *devfn)
935 {
936 	int n;
937 	unsigned int slot, func;
938 
939 
940 	n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
941 	if (n != 4)
942 		return 0;
943 
944 	*devfn = PCI_DEVFN(slot, func);
945 
946 	return 1;
947 }
948 
vga_arb_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)949 static ssize_t vga_arb_read(struct file *file, char __user *buf,
950 			    size_t count, loff_t *ppos)
951 {
952 	struct vga_arb_private *priv = file->private_data;
953 	struct vga_device *vgadev;
954 	struct pci_dev *pdev;
955 	unsigned long flags;
956 	size_t len;
957 	int rc;
958 	char *lbuf;
959 
960 	lbuf = kmalloc(1024, GFP_KERNEL);
961 	if (lbuf == NULL)
962 		return -ENOMEM;
963 
964 	/* Shields against vga_arb_device_card_gone (pci_dev going
965 	 * away), and allows access to vga list
966 	 */
967 	spin_lock_irqsave(&vga_lock, flags);
968 
969 	/* If we are targeting the default, use it */
970 	pdev = priv->target;
971 	if (pdev == NULL || pdev == PCI_INVALID_CARD) {
972 		spin_unlock_irqrestore(&vga_lock, flags);
973 		len = sprintf(lbuf, "invalid");
974 		goto done;
975 	}
976 
977 	/* Find card vgadev structure */
978 	vgadev = vgadev_find(pdev);
979 	if (vgadev == NULL) {
980 		/* Wow, it's not in the list, that shouldn't happen,
981 		 * let's fix us up and return invalid card
982 		 */
983 		if (pdev == priv->target)
984 			vga_arb_device_card_gone(pdev);
985 		spin_unlock_irqrestore(&vga_lock, flags);
986 		len = sprintf(lbuf, "invalid");
987 		goto done;
988 	}
989 
990 	/* Fill the buffer with infos */
991 	len = snprintf(lbuf, 1024,
992 		       "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
993 		       vga_decode_count, pci_name(pdev),
994 		       vga_iostate_to_str(vgadev->decodes),
995 		       vga_iostate_to_str(vgadev->owns),
996 		       vga_iostate_to_str(vgadev->locks),
997 		       vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
998 
999 	spin_unlock_irqrestore(&vga_lock, flags);
1000 done:
1001 
1002 	/* Copy that to user */
1003 	if (len > count)
1004 		len = count;
1005 	rc = copy_to_user(buf, lbuf, len);
1006 	kfree(lbuf);
1007 	if (rc)
1008 		return -EFAULT;
1009 	return len;
1010 }
1011 
1012 /*
1013  * TODO: To avoid parsing inside kernel and to improve the speed we may
1014  * consider use ioctl here
1015  */
vga_arb_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1016 static ssize_t vga_arb_write(struct file *file, const char __user *buf,
1017 			     size_t count, loff_t *ppos)
1018 {
1019 	struct vga_arb_private *priv = file->private_data;
1020 	struct vga_arb_user_card *uc = NULL;
1021 	struct pci_dev *pdev;
1022 
1023 	unsigned int io_state;
1024 
1025 	char *kbuf, *curr_pos;
1026 	size_t remaining = count;
1027 
1028 	int ret_val;
1029 	int i;
1030 
1031 
1032 	kbuf = kmalloc(count + 1, GFP_KERNEL);
1033 	if (!kbuf)
1034 		return -ENOMEM;
1035 
1036 	if (copy_from_user(kbuf, buf, count)) {
1037 		kfree(kbuf);
1038 		return -EFAULT;
1039 	}
1040 	curr_pos = kbuf;
1041 	kbuf[count] = '\0';	/* Just to make sure... */
1042 
1043 	if (strncmp(curr_pos, "lock ", 5) == 0) {
1044 		curr_pos += 5;
1045 		remaining -= 5;
1046 
1047 		pr_debug("client 0x%p called 'lock'\n", priv);
1048 
1049 		if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1050 			ret_val = -EPROTO;
1051 			goto done;
1052 		}
1053 		if (io_state == VGA_RSRC_NONE) {
1054 			ret_val = -EPROTO;
1055 			goto done;
1056 		}
1057 
1058 		pdev = priv->target;
1059 		if (priv->target == NULL) {
1060 			ret_val = -ENODEV;
1061 			goto done;
1062 		}
1063 
1064 		vga_get_uninterruptible(pdev, io_state);
1065 
1066 		/* Update the client's locks lists... */
1067 		for (i = 0; i < MAX_USER_CARDS; i++) {
1068 			if (priv->cards[i].pdev == pdev) {
1069 				if (io_state & VGA_RSRC_LEGACY_IO)
1070 					priv->cards[i].io_cnt++;
1071 				if (io_state & VGA_RSRC_LEGACY_MEM)
1072 					priv->cards[i].mem_cnt++;
1073 				break;
1074 			}
1075 		}
1076 
1077 		ret_val = count;
1078 		goto done;
1079 	} else if (strncmp(curr_pos, "unlock ", 7) == 0) {
1080 		curr_pos += 7;
1081 		remaining -= 7;
1082 
1083 		pr_debug("client 0x%p called 'unlock'\n", priv);
1084 
1085 		if (strncmp(curr_pos, "all", 3) == 0)
1086 			io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
1087 		else {
1088 			if (!vga_str_to_iostate
1089 			    (curr_pos, remaining, &io_state)) {
1090 				ret_val = -EPROTO;
1091 				goto done;
1092 			}
1093 			/* TODO: Add this?
1094 			   if (io_state == VGA_RSRC_NONE) {
1095 			   ret_val = -EPROTO;
1096 			   goto done;
1097 			   }
1098 			  */
1099 		}
1100 
1101 		pdev = priv->target;
1102 		if (priv->target == NULL) {
1103 			ret_val = -ENODEV;
1104 			goto done;
1105 		}
1106 		for (i = 0; i < MAX_USER_CARDS; i++) {
1107 			if (priv->cards[i].pdev == pdev)
1108 				uc = &priv->cards[i];
1109 		}
1110 
1111 		if (!uc) {
1112 			ret_val = -EINVAL;
1113 			goto done;
1114 		}
1115 
1116 		if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
1117 			ret_val = -EINVAL;
1118 			goto done;
1119 		}
1120 
1121 		if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
1122 			ret_val = -EINVAL;
1123 			goto done;
1124 		}
1125 
1126 		vga_put(pdev, io_state);
1127 
1128 		if (io_state & VGA_RSRC_LEGACY_IO)
1129 			uc->io_cnt--;
1130 		if (io_state & VGA_RSRC_LEGACY_MEM)
1131 			uc->mem_cnt--;
1132 
1133 		ret_val = count;
1134 		goto done;
1135 	} else if (strncmp(curr_pos, "trylock ", 8) == 0) {
1136 		curr_pos += 8;
1137 		remaining -= 8;
1138 
1139 		pr_debug("client 0x%p called 'trylock'\n", priv);
1140 
1141 		if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1142 			ret_val = -EPROTO;
1143 			goto done;
1144 		}
1145 		/* TODO: Add this?
1146 		   if (io_state == VGA_RSRC_NONE) {
1147 		   ret_val = -EPROTO;
1148 		   goto done;
1149 		   }
1150 		 */
1151 
1152 		pdev = priv->target;
1153 		if (priv->target == NULL) {
1154 			ret_val = -ENODEV;
1155 			goto done;
1156 		}
1157 
1158 		if (vga_tryget(pdev, io_state)) {
1159 			/* Update the client's locks lists... */
1160 			for (i = 0; i < MAX_USER_CARDS; i++) {
1161 				if (priv->cards[i].pdev == pdev) {
1162 					if (io_state & VGA_RSRC_LEGACY_IO)
1163 						priv->cards[i].io_cnt++;
1164 					if (io_state & VGA_RSRC_LEGACY_MEM)
1165 						priv->cards[i].mem_cnt++;
1166 					break;
1167 				}
1168 			}
1169 			ret_val = count;
1170 			goto done;
1171 		} else {
1172 			ret_val = -EBUSY;
1173 			goto done;
1174 		}
1175 
1176 	} else if (strncmp(curr_pos, "target ", 7) == 0) {
1177 		unsigned int domain, bus, devfn;
1178 		struct vga_device *vgadev;
1179 
1180 		curr_pos += 7;
1181 		remaining -= 7;
1182 		pr_debug("client 0x%p called 'target'\n", priv);
1183 		/* if target is default */
1184 		if (!strncmp(curr_pos, "default", 7))
1185 			pdev = pci_dev_get(vga_default_device());
1186 		else {
1187 			if (!vga_pci_str_to_vars(curr_pos, remaining,
1188 						 &domain, &bus, &devfn)) {
1189 				ret_val = -EPROTO;
1190 				goto done;
1191 			}
1192 			pr_debug("%s ==> %x:%x:%x.%x\n", curr_pos,
1193 				domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
1194 
1195 			pdev = pci_get_domain_bus_and_slot(domain, bus, devfn);
1196 			pr_debug("pdev %p\n", pdev);
1197 			if (!pdev) {
1198 				pr_err("invalid PCI address %x:%x:%x\n",
1199 					domain, bus, devfn);
1200 				ret_val = -ENODEV;
1201 				goto done;
1202 			}
1203 		}
1204 
1205 		vgadev = vgadev_find(pdev);
1206 		pr_debug("vgadev %p\n", vgadev);
1207 		if (vgadev == NULL) {
1208 			if (pdev) {
1209 				pr_err("this pci device is not a vga device\n");
1210 				pci_dev_put(pdev);
1211 			}
1212 
1213 			ret_val = -ENODEV;
1214 			goto done;
1215 		}
1216 
1217 		priv->target = pdev;
1218 		for (i = 0; i < MAX_USER_CARDS; i++) {
1219 			if (priv->cards[i].pdev == pdev)
1220 				break;
1221 			if (priv->cards[i].pdev == NULL) {
1222 				priv->cards[i].pdev = pdev;
1223 				priv->cards[i].io_cnt = 0;
1224 				priv->cards[i].mem_cnt = 0;
1225 				break;
1226 			}
1227 		}
1228 		if (i == MAX_USER_CARDS) {
1229 			pr_err("maximum user cards (%d) number reached!\n",
1230 				MAX_USER_CARDS);
1231 			pci_dev_put(pdev);
1232 			/* XXX: which value to return? */
1233 			ret_val =  -ENOMEM;
1234 			goto done;
1235 		}
1236 
1237 		ret_val = count;
1238 		pci_dev_put(pdev);
1239 		goto done;
1240 
1241 
1242 	} else if (strncmp(curr_pos, "decodes ", 8) == 0) {
1243 		curr_pos += 8;
1244 		remaining -= 8;
1245 		pr_debug("client 0x%p called 'decodes'\n", priv);
1246 
1247 		if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1248 			ret_val = -EPROTO;
1249 			goto done;
1250 		}
1251 		pdev = priv->target;
1252 		if (priv->target == NULL) {
1253 			ret_val = -ENODEV;
1254 			goto done;
1255 		}
1256 
1257 		__vga_set_legacy_decoding(pdev, io_state, true);
1258 		ret_val = count;
1259 		goto done;
1260 	}
1261 	/* If we got here, the message written is not part of the protocol! */
1262 	kfree(kbuf);
1263 	return -EPROTO;
1264 
1265 done:
1266 	kfree(kbuf);
1267 	return ret_val;
1268 }
1269 
vga_arb_fpoll(struct file * file,poll_table * wait)1270 static unsigned int vga_arb_fpoll(struct file *file, poll_table *wait)
1271 {
1272 	pr_debug("%s\n", __func__);
1273 
1274 	poll_wait(file, &vga_wait_queue, wait);
1275 	return POLLIN;
1276 }
1277 
vga_arb_open(struct inode * inode,struct file * file)1278 static int vga_arb_open(struct inode *inode, struct file *file)
1279 {
1280 	struct vga_arb_private *priv;
1281 	unsigned long flags;
1282 
1283 	pr_debug("%s\n", __func__);
1284 
1285 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1286 	if (priv == NULL)
1287 		return -ENOMEM;
1288 	spin_lock_init(&priv->lock);
1289 	file->private_data = priv;
1290 
1291 	spin_lock_irqsave(&vga_user_lock, flags);
1292 	list_add(&priv->list, &vga_user_list);
1293 	spin_unlock_irqrestore(&vga_user_lock, flags);
1294 
1295 	/* Set the client' lists of locks */
1296 	priv->target = vga_default_device(); /* Maybe this is still null! */
1297 	priv->cards[0].pdev = priv->target;
1298 	priv->cards[0].io_cnt = 0;
1299 	priv->cards[0].mem_cnt = 0;
1300 
1301 
1302 	return 0;
1303 }
1304 
vga_arb_release(struct inode * inode,struct file * file)1305 static int vga_arb_release(struct inode *inode, struct file *file)
1306 {
1307 	struct vga_arb_private *priv = file->private_data;
1308 	struct vga_arb_user_card *uc;
1309 	unsigned long flags;
1310 	int i;
1311 
1312 	pr_debug("%s\n", __func__);
1313 
1314 	spin_lock_irqsave(&vga_user_lock, flags);
1315 	list_del(&priv->list);
1316 	for (i = 0; i < MAX_USER_CARDS; i++) {
1317 		uc = &priv->cards[i];
1318 		if (uc->pdev == NULL)
1319 			continue;
1320 		pr_debug("uc->io_cnt == %d, uc->mem_cnt == %d\n",
1321 			 uc->io_cnt, uc->mem_cnt);
1322 		while (uc->io_cnt--)
1323 			vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
1324 		while (uc->mem_cnt--)
1325 			vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
1326 	}
1327 	spin_unlock_irqrestore(&vga_user_lock, flags);
1328 
1329 	kfree(priv);
1330 
1331 	return 0;
1332 }
1333 
vga_arb_device_card_gone(struct pci_dev * pdev)1334 static void vga_arb_device_card_gone(struct pci_dev *pdev)
1335 {
1336 }
1337 
1338 /*
1339  * callback any registered clients to let them know we have a
1340  * change in VGA cards
1341  */
vga_arbiter_notify_clients(void)1342 static void vga_arbiter_notify_clients(void)
1343 {
1344 	struct vga_device *vgadev;
1345 	unsigned long flags;
1346 	uint32_t new_decodes;
1347 	bool new_state;
1348 
1349 	if (!vga_arbiter_used)
1350 		return;
1351 
1352 	spin_lock_irqsave(&vga_lock, flags);
1353 	list_for_each_entry(vgadev, &vga_list, list) {
1354 		if (vga_count > 1)
1355 			new_state = false;
1356 		else
1357 			new_state = true;
1358 		if (vgadev->set_vga_decode) {
1359 			new_decodes = vgadev->set_vga_decode(vgadev->cookie,
1360 							     new_state);
1361 			vga_update_device_decodes(vgadev, new_decodes);
1362 		}
1363 	}
1364 	spin_unlock_irqrestore(&vga_lock, flags);
1365 }
1366 
pci_notify(struct notifier_block * nb,unsigned long action,void * data)1367 static int pci_notify(struct notifier_block *nb, unsigned long action,
1368 		      void *data)
1369 {
1370 	struct device *dev = data;
1371 	struct pci_dev *pdev = to_pci_dev(dev);
1372 	bool notify = false;
1373 
1374 	pr_debug("%s\n", __func__);
1375 
1376 	/* For now we're only intereted in devices added and removed. I didn't
1377 	 * test this thing here, so someone needs to double check for the
1378 	 * cases of hotplugable vga cards. */
1379 	if (action == BUS_NOTIFY_ADD_DEVICE)
1380 		notify = vga_arbiter_add_pci_device(pdev);
1381 	else if (action == BUS_NOTIFY_DEL_DEVICE)
1382 		notify = vga_arbiter_del_pci_device(pdev);
1383 
1384 	if (notify)
1385 		vga_arbiter_notify_clients();
1386 	return 0;
1387 }
1388 
1389 static struct notifier_block pci_notifier = {
1390 	.notifier_call = pci_notify,
1391 };
1392 
1393 static const struct file_operations vga_arb_device_fops = {
1394 	.read = vga_arb_read,
1395 	.write = vga_arb_write,
1396 	.poll = vga_arb_fpoll,
1397 	.open = vga_arb_open,
1398 	.release = vga_arb_release,
1399 	.llseek = noop_llseek,
1400 };
1401 
1402 static struct miscdevice vga_arb_device = {
1403 	MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
1404 };
1405 
vga_arb_device_init(void)1406 static int __init vga_arb_device_init(void)
1407 {
1408 	int rc;
1409 	struct pci_dev *pdev;
1410 	struct vga_device *vgadev;
1411 
1412 	rc = misc_register(&vga_arb_device);
1413 	if (rc < 0)
1414 		pr_err("error %d registering device\n", rc);
1415 
1416 	bus_register_notifier(&pci_bus_type, &pci_notifier);
1417 
1418 	/* We add all pci devices satisfying vga class in the arbiter by
1419 	 * default */
1420 	pdev = NULL;
1421 	while ((pdev =
1422 		pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
1423 			       PCI_ANY_ID, pdev)) != NULL)
1424 		vga_arbiter_add_pci_device(pdev);
1425 
1426 	pr_info("loaded\n");
1427 
1428 	list_for_each_entry(vgadev, &vga_list, list) {
1429 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
1430 		/*
1431 		 * Override vga_arbiter_add_pci_device()'s I/O based detection
1432 		 * as it may take the wrong device (e.g. on Apple system under
1433 		 * EFI).
1434 		 *
1435 		 * Select the device owning the boot framebuffer if there is
1436 		 * one.
1437 		 */
1438 		resource_size_t start, end, limit;
1439 		unsigned long flags;
1440 		int i;
1441 
1442 		limit = screen_info.lfb_base + screen_info.lfb_size;
1443 
1444 		/* Does firmware framebuffer belong to us? */
1445 		for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1446 			flags = pci_resource_flags(vgadev->pdev, i);
1447 
1448 			if ((flags & IORESOURCE_MEM) == 0)
1449 				continue;
1450 
1451 			start = pci_resource_start(vgadev->pdev, i);
1452 			end  = pci_resource_end(vgadev->pdev, i);
1453 
1454 			if (!start || !end)
1455 				continue;
1456 
1457 			if (screen_info.lfb_base < start || limit >= end)
1458 				continue;
1459 
1460 			if (!vga_default_device())
1461 				pr_info("setting as boot device: PCI:%s\n",
1462 					pci_name(vgadev->pdev));
1463 			else if (vgadev->pdev != vga_default_device())
1464 				pr_info("overriding boot device: PCI:%s\n",
1465 					pci_name(vgadev->pdev));
1466 			vga_set_default_device(vgadev->pdev);
1467 		}
1468 #endif
1469 		if (vgadev->bridge_has_one_vga)
1470 			pr_info("bridge control possible %s\n",
1471 				pci_name(vgadev->pdev));
1472 		else
1473 			pr_info("no bridge control possible %s\n",
1474 				pci_name(vgadev->pdev));
1475 	}
1476 	return rc;
1477 }
1478 subsys_initcall(vga_arb_device_init);
1479