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