iwlwifi: work around bogus active chains detection
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpu / vga / vgaarb.c
blob8827814d0735fbc7c3ff619b6f608476c82a4172
1 /*
2 * vgaarb.c
4 * (C) Copyright 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
5 * (C) Copyright 2007 Paulo R. Zanoni <przanoni@gmail.com>
6 * (C) Copyright 2007, 2009 Tiago Vignatti <vignatti@freedesktop.org>
8 * Implements the VGA arbitration. For details refer to
9 * Documentation/vgaarbiter.txt
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/pci.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/spinlock.h>
21 #include <linux/poll.h>
22 #include <linux/miscdevice.h>
24 #include <linux/uaccess.h>
26 #include <linux/vgaarb.h>
28 static void vga_arbiter_notify_clients(void);
30 * We keep a list of all vga devices in the system to speed
31 * up the various operations of the arbiter
33 struct vga_device {
34 struct list_head list;
35 struct pci_dev *pdev;
36 unsigned int decodes; /* what does it decodes */
37 unsigned int owns; /* what does it owns */
38 unsigned int locks; /* what does it locks */
39 unsigned int io_lock_cnt; /* legacy IO lock count */
40 unsigned int mem_lock_cnt; /* legacy MEM lock count */
41 unsigned int io_norm_cnt; /* normal IO count */
42 unsigned int mem_norm_cnt; /* normal MEM count */
44 /* allow IRQ enable/disable hook */
45 void *cookie;
46 void (*irq_set_state)(void *cookie, bool enable);
47 unsigned int (*set_vga_decode)(void *cookie, bool decode);
50 static LIST_HEAD(vga_list);
51 static int vga_count, vga_decode_count;
52 static bool vga_arbiter_used;
53 static DEFINE_SPINLOCK(vga_lock);
54 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
57 static const char *vga_iostate_to_str(unsigned int iostate)
59 /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
60 iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
61 switch (iostate) {
62 case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
63 return "io+mem";
64 case VGA_RSRC_LEGACY_IO:
65 return "io";
66 case VGA_RSRC_LEGACY_MEM:
67 return "mem";
69 return "none";
72 static int vga_str_to_iostate(char *buf, int str_size, int *io_state)
74 /* we could in theory hand out locks on IO and mem
75 * separately to userspace but it can cause deadlocks */
76 if (strncmp(buf, "none", 4) == 0) {
77 *io_state = VGA_RSRC_NONE;
78 return 1;
81 /* XXX We're not chekcing the str_size! */
82 if (strncmp(buf, "io+mem", 6) == 0)
83 goto both;
84 else if (strncmp(buf, "io", 2) == 0)
85 goto both;
86 else if (strncmp(buf, "mem", 3) == 0)
87 goto both;
88 return 0;
89 both:
90 *io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
91 return 1;
94 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
95 /* this is only used a cookie - it should not be dereferenced */
96 static struct pci_dev *vga_default;
97 #endif
99 static void vga_arb_device_card_gone(struct pci_dev *pdev);
101 /* Find somebody in our list */
102 static struct vga_device *vgadev_find(struct pci_dev *pdev)
104 struct vga_device *vgadev;
106 list_for_each_entry(vgadev, &vga_list, list)
107 if (pdev == vgadev->pdev)
108 return vgadev;
109 return NULL;
112 /* Returns the default VGA device (vgacon's babe) */
113 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
114 struct pci_dev *vga_default_device(void)
116 return vga_default;
118 #endif
120 static inline void vga_irq_set_state(struct vga_device *vgadev, bool state)
122 if (vgadev->irq_set_state)
123 vgadev->irq_set_state(vgadev->cookie, state);
127 /* If we don't ever use VGA arb we should avoid
128 turning off anything anywhere due to old X servers getting
129 confused about the boot device not being VGA */
130 static void vga_check_first_use(void)
132 /* we should inform all GPUs in the system that
133 * VGA arb has occured and to try and disable resources
134 * if they can */
135 if (!vga_arbiter_used) {
136 vga_arbiter_used = true;
137 vga_arbiter_notify_clients();
141 static struct vga_device *__vga_tryget(struct vga_device *vgadev,
142 unsigned int rsrc)
144 unsigned int wants, legacy_wants, match;
145 struct vga_device *conflict;
146 unsigned int pci_bits;
147 /* Account for "normal" resources to lock. If we decode the legacy,
148 * counterpart, we need to request it as well
150 if ((rsrc & VGA_RSRC_NORMAL_IO) &&
151 (vgadev->decodes & VGA_RSRC_LEGACY_IO))
152 rsrc |= VGA_RSRC_LEGACY_IO;
153 if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
154 (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
155 rsrc |= VGA_RSRC_LEGACY_MEM;
157 pr_devel("%s: %d\n", __func__, rsrc);
158 pr_devel("%s: owns: %d\n", __func__, vgadev->owns);
160 /* Check what resources we need to acquire */
161 wants = rsrc & ~vgadev->owns;
163 /* We already own everything, just mark locked & bye bye */
164 if (wants == 0)
165 goto lock_them;
167 /* We don't need to request a legacy resource, we just enable
168 * appropriate decoding and go
170 legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
171 if (legacy_wants == 0)
172 goto enable_them;
174 /* Ok, we don't, let's find out how we need to kick off */
175 list_for_each_entry(conflict, &vga_list, list) {
176 unsigned int lwants = legacy_wants;
177 unsigned int change_bridge = 0;
179 /* Don't conflict with myself */
180 if (vgadev == conflict)
181 continue;
183 /* Check if the architecture allows a conflict between those
184 * 2 devices or if they are on separate domains
186 if (!vga_conflicts(vgadev->pdev, conflict->pdev))
187 continue;
189 /* We have a possible conflict. before we go further, we must
190 * check if we sit on the same bus as the conflicting device.
191 * if we don't, then we must tie both IO and MEM resources
192 * together since there is only a single bit controlling
193 * VGA forwarding on P2P bridges
195 if (vgadev->pdev->bus != conflict->pdev->bus) {
196 change_bridge = 1;
197 lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
200 /* Check if the guy has a lock on the resource. If he does,
201 * return the conflicting entry
203 if (conflict->locks & lwants)
204 return conflict;
206 /* Ok, now check if he owns the resource we want. We don't need
207 * to check "decodes" since it should be impossible to own
208 * own legacy resources you don't decode unless I have a bug
209 * in this code...
211 WARN_ON(conflict->owns & ~conflict->decodes);
212 match = lwants & conflict->owns;
213 if (!match)
214 continue;
216 /* looks like he doesn't have a lock, we can steal
217 * them from him
219 vga_irq_set_state(conflict, false);
221 pci_bits = 0;
222 if (lwants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
223 pci_bits |= PCI_COMMAND_MEMORY;
224 if (lwants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
225 pci_bits |= PCI_COMMAND_IO;
227 pci_set_vga_state(conflict->pdev, false, pci_bits,
228 change_bridge);
229 conflict->owns &= ~lwants;
230 /* If he also owned non-legacy, that is no longer the case */
231 if (lwants & VGA_RSRC_LEGACY_MEM)
232 conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
233 if (lwants & VGA_RSRC_LEGACY_IO)
234 conflict->owns &= ~VGA_RSRC_NORMAL_IO;
237 enable_them:
238 /* ok dude, we got it, everybody conflicting has been disabled, let's
239 * enable us. Make sure we don't mark a bit in "owns" that we don't
240 * also have in "decodes". We can lock resources we don't decode but
241 * not own them.
243 pci_bits = 0;
244 if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
245 pci_bits |= PCI_COMMAND_MEMORY;
246 if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
247 pci_bits |= PCI_COMMAND_IO;
248 pci_set_vga_state(vgadev->pdev, true, pci_bits, !!(wants & VGA_RSRC_LEGACY_MASK));
250 vga_irq_set_state(vgadev, true);
251 vgadev->owns |= (wants & vgadev->decodes);
252 lock_them:
253 vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
254 if (rsrc & VGA_RSRC_LEGACY_IO)
255 vgadev->io_lock_cnt++;
256 if (rsrc & VGA_RSRC_LEGACY_MEM)
257 vgadev->mem_lock_cnt++;
258 if (rsrc & VGA_RSRC_NORMAL_IO)
259 vgadev->io_norm_cnt++;
260 if (rsrc & VGA_RSRC_NORMAL_MEM)
261 vgadev->mem_norm_cnt++;
263 return NULL;
266 static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
268 unsigned int old_locks = vgadev->locks;
270 pr_devel("%s\n", __func__);
272 /* Update our counters, and account for equivalent legacy resources
273 * if we decode them
275 if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
276 vgadev->io_norm_cnt--;
277 if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
278 rsrc |= VGA_RSRC_LEGACY_IO;
280 if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
281 vgadev->mem_norm_cnt--;
282 if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
283 rsrc |= VGA_RSRC_LEGACY_MEM;
285 if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
286 vgadev->io_lock_cnt--;
287 if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
288 vgadev->mem_lock_cnt--;
290 /* Just clear lock bits, we do lazy operations so we don't really
291 * have to bother about anything else at this point
293 if (vgadev->io_lock_cnt == 0)
294 vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
295 if (vgadev->mem_lock_cnt == 0)
296 vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
298 /* Kick the wait queue in case somebody was waiting if we actually
299 * released something
301 if (old_locks != vgadev->locks)
302 wake_up_all(&vga_wait_queue);
305 int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
307 struct vga_device *vgadev, *conflict;
308 unsigned long flags;
309 wait_queue_t wait;
310 int rc = 0;
312 vga_check_first_use();
313 /* The one who calls us should check for this, but lets be sure... */
314 if (pdev == NULL)
315 pdev = vga_default_device();
316 if (pdev == NULL)
317 return 0;
319 for (;;) {
320 spin_lock_irqsave(&vga_lock, flags);
321 vgadev = vgadev_find(pdev);
322 if (vgadev == NULL) {
323 spin_unlock_irqrestore(&vga_lock, flags);
324 rc = -ENODEV;
325 break;
327 conflict = __vga_tryget(vgadev, rsrc);
328 spin_unlock_irqrestore(&vga_lock, flags);
329 if (conflict == NULL)
330 break;
333 /* We have a conflict, we wait until somebody kicks the
334 * work queue. Currently we have one work queue that we
335 * kick each time some resources are released, but it would
336 * be fairly easy to have a per device one so that we only
337 * need to attach to the conflicting device
339 init_waitqueue_entry(&wait, current);
340 add_wait_queue(&vga_wait_queue, &wait);
341 set_current_state(interruptible ?
342 TASK_INTERRUPTIBLE :
343 TASK_UNINTERRUPTIBLE);
344 if (signal_pending(current)) {
345 rc = -EINTR;
346 break;
348 schedule();
349 remove_wait_queue(&vga_wait_queue, &wait);
350 set_current_state(TASK_RUNNING);
352 return rc;
354 EXPORT_SYMBOL(vga_get);
356 int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
358 struct vga_device *vgadev;
359 unsigned long flags;
360 int rc = 0;
362 vga_check_first_use();
364 /* The one who calls us should check for this, but lets be sure... */
365 if (pdev == NULL)
366 pdev = vga_default_device();
367 if (pdev == NULL)
368 return 0;
369 spin_lock_irqsave(&vga_lock, flags);
370 vgadev = vgadev_find(pdev);
371 if (vgadev == NULL) {
372 rc = -ENODEV;
373 goto bail;
375 if (__vga_tryget(vgadev, rsrc))
376 rc = -EBUSY;
377 bail:
378 spin_unlock_irqrestore(&vga_lock, flags);
379 return rc;
381 EXPORT_SYMBOL(vga_tryget);
383 void vga_put(struct pci_dev *pdev, unsigned int rsrc)
385 struct vga_device *vgadev;
386 unsigned long flags;
388 /* The one who calls us should check for this, but lets be sure... */
389 if (pdev == NULL)
390 pdev = vga_default_device();
391 if (pdev == NULL)
392 return;
393 spin_lock_irqsave(&vga_lock, flags);
394 vgadev = vgadev_find(pdev);
395 if (vgadev == NULL)
396 goto bail;
397 __vga_put(vgadev, rsrc);
398 bail:
399 spin_unlock_irqrestore(&vga_lock, flags);
401 EXPORT_SYMBOL(vga_put);
404 * Currently, we assume that the "initial" setup of the system is
405 * not sane, that is we come up with conflicting devices and let
406 * the arbiter's client decides if devices decodes or not legacy
407 * things.
409 static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
411 struct vga_device *vgadev;
412 unsigned long flags;
413 struct pci_bus *bus;
414 struct pci_dev *bridge;
415 u16 cmd;
417 /* Only deal with VGA class devices */
418 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
419 return false;
421 /* Allocate structure */
422 vgadev = kmalloc(sizeof(struct vga_device), GFP_KERNEL);
423 if (vgadev == NULL) {
424 pr_err("vgaarb: failed to allocate pci device\n");
425 /* What to do on allocation failure ? For now, let's
426 * just do nothing, I'm not sure there is anything saner
427 * to be done
429 return false;
432 memset(vgadev, 0, sizeof(*vgadev));
434 /* Take lock & check for duplicates */
435 spin_lock_irqsave(&vga_lock, flags);
436 if (vgadev_find(pdev) != NULL) {
437 BUG_ON(1);
438 goto fail;
440 vgadev->pdev = pdev;
442 /* By default, assume we decode everything */
443 vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
444 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
446 /* by default mark it as decoding */
447 vga_decode_count++;
448 /* Mark that we "own" resources based on our enables, we will
449 * clear that below if the bridge isn't forwarding
451 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
452 if (cmd & PCI_COMMAND_IO)
453 vgadev->owns |= VGA_RSRC_LEGACY_IO;
454 if (cmd & PCI_COMMAND_MEMORY)
455 vgadev->owns |= VGA_RSRC_LEGACY_MEM;
457 /* Check if VGA cycles can get down to us */
458 bus = pdev->bus;
459 while (bus) {
460 bridge = bus->self;
461 if (bridge) {
462 u16 l;
463 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
464 &l);
465 if (!(l & PCI_BRIDGE_CTL_VGA)) {
466 vgadev->owns = 0;
467 break;
470 bus = bus->parent;
473 /* Deal with VGA default device. Use first enabled one
474 * by default if arch doesn't have it's own hook
476 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
477 if (vga_default == NULL &&
478 ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK))
479 vga_default = pci_dev_get(pdev);
480 #endif
482 /* Add to the list */
483 list_add(&vgadev->list, &vga_list);
484 vga_count++;
485 pr_info("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
486 pci_name(pdev),
487 vga_iostate_to_str(vgadev->decodes),
488 vga_iostate_to_str(vgadev->owns),
489 vga_iostate_to_str(vgadev->locks));
491 spin_unlock_irqrestore(&vga_lock, flags);
492 return true;
493 fail:
494 spin_unlock_irqrestore(&vga_lock, flags);
495 kfree(vgadev);
496 return false;
499 static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
501 struct vga_device *vgadev;
502 unsigned long flags;
503 bool ret = true;
505 spin_lock_irqsave(&vga_lock, flags);
506 vgadev = vgadev_find(pdev);
507 if (vgadev == NULL) {
508 ret = false;
509 goto bail;
512 if (vga_default == pdev) {
513 pci_dev_put(vga_default);
514 vga_default = NULL;
517 if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
518 vga_decode_count--;
520 /* Remove entry from list */
521 list_del(&vgadev->list);
522 vga_count--;
523 /* Notify userland driver that the device is gone so it discards
524 * it's copies of the pci_dev pointer
526 vga_arb_device_card_gone(pdev);
528 /* Wake up all possible waiters */
529 wake_up_all(&vga_wait_queue);
530 bail:
531 spin_unlock_irqrestore(&vga_lock, flags);
532 kfree(vgadev);
533 return ret;
536 /* this is called with the lock */
537 static inline void vga_update_device_decodes(struct vga_device *vgadev,
538 int new_decodes)
540 int old_decodes;
541 struct vga_device *new_vgadev, *conflict;
543 old_decodes = vgadev->decodes;
544 vgadev->decodes = new_decodes;
546 pr_info("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
547 pci_name(vgadev->pdev),
548 vga_iostate_to_str(old_decodes),
549 vga_iostate_to_str(vgadev->decodes),
550 vga_iostate_to_str(vgadev->owns));
553 /* if we own the decodes we should move them along to
554 another card */
555 if ((vgadev->owns & old_decodes) && (vga_count > 1)) {
556 /* set us to own nothing */
557 vgadev->owns &= ~old_decodes;
558 list_for_each_entry(new_vgadev, &vga_list, list) {
559 if ((new_vgadev != vgadev) &&
560 (new_vgadev->decodes & VGA_RSRC_LEGACY_MASK)) {
561 pr_info("vgaarb: transferring owner from PCI:%s to PCI:%s\n", pci_name(vgadev->pdev), pci_name(new_vgadev->pdev));
562 conflict = __vga_tryget(new_vgadev, VGA_RSRC_LEGACY_MASK);
563 if (!conflict)
564 __vga_put(new_vgadev, VGA_RSRC_LEGACY_MASK);
565 break;
570 /* change decodes counter */
571 if (old_decodes != new_decodes) {
572 if (new_decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
573 vga_decode_count++;
574 else
575 vga_decode_count--;
579 void __vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes, bool userspace)
581 struct vga_device *vgadev;
582 unsigned long flags;
584 decodes &= VGA_RSRC_LEGACY_MASK;
586 spin_lock_irqsave(&vga_lock, flags);
587 vgadev = vgadev_find(pdev);
588 if (vgadev == NULL)
589 goto bail;
591 /* don't let userspace futz with kernel driver decodes */
592 if (userspace && vgadev->set_vga_decode)
593 goto bail;
595 /* update the device decodes + counter */
596 vga_update_device_decodes(vgadev, decodes);
598 /* XXX if somebody is going from "doesn't decode" to "decodes" state
599 * here, additional care must be taken as we may have pending owner
600 * ship of non-legacy region ...
602 bail:
603 spin_unlock_irqrestore(&vga_lock, flags);
606 void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
608 __vga_set_legacy_decoding(pdev, decodes, false);
610 EXPORT_SYMBOL(vga_set_legacy_decoding);
612 /* call with NULL to unregister */
613 int vga_client_register(struct pci_dev *pdev, void *cookie,
614 void (*irq_set_state)(void *cookie, bool state),
615 unsigned int (*set_vga_decode)(void *cookie, bool decode))
617 int ret = -1;
618 struct vga_device *vgadev;
619 unsigned long flags;
621 spin_lock_irqsave(&vga_lock, flags);
622 vgadev = vgadev_find(pdev);
623 if (!vgadev)
624 goto bail;
626 vgadev->irq_set_state = irq_set_state;
627 vgadev->set_vga_decode = set_vga_decode;
628 vgadev->cookie = cookie;
629 ret = 0;
631 bail:
632 spin_unlock_irqrestore(&vga_lock, flags);
633 return ret;
636 EXPORT_SYMBOL(vga_client_register);
639 * Char driver implementation
641 * Semantics is:
643 * open : open user instance of the arbitrer. by default, it's
644 * attached to the default VGA device of the system.
646 * close : close user instance, release locks
648 * read : return a string indicating the status of the target.
649 * an IO state string is of the form {io,mem,io+mem,none},
650 * mc and ic are respectively mem and io lock counts (for
651 * debugging/diagnostic only). "decodes" indicate what the
652 * card currently decodes, "owns" indicates what is currently
653 * enabled on it, and "locks" indicates what is locked by this
654 * card. If the card is unplugged, we get "invalid" then for
655 * card_ID and an -ENODEV error is returned for any command
656 * until a new card is targeted
658 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
660 * write : write a command to the arbiter. List of commands is:
662 * target <card_ID> : switch target to card <card_ID> (see below)
663 * lock <io_state> : acquires locks on target ("none" is invalid io_state)
664 * trylock <io_state> : non-blocking acquire locks on target
665 * unlock <io_state> : release locks on target
666 * unlock all : release all locks on target held by this user
667 * decodes <io_state> : set the legacy decoding attributes for the card
669 * poll : event if something change on any card (not just the target)
671 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
672 * to go back to the system default card (TODO: not implemented yet).
673 * Currently, only PCI is supported as a prefix, but the userland API may
674 * support other bus types in the future, even if the current kernel
675 * implementation doesn't.
677 * Note about locks:
679 * The driver keeps track of which user has what locks on which card. It
680 * supports stacking, like the kernel one. This complexifies the implementation
681 * a bit, but makes the arbiter more tolerant to userspace problems and able
682 * to properly cleanup in all cases when a process dies.
683 * Currently, a max of 16 cards simultaneously can have locks issued from
684 * userspace for a given user (file descriptor instance) of the arbiter.
686 * If the device is hot-unplugged, there is a hook inside the module to notify
687 * they being added/removed in the system and automatically added/removed in
688 * the arbiter.
691 #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
692 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
695 * Each user has an array of these, tracking which cards have locks
697 struct vga_arb_user_card {
698 struct pci_dev *pdev;
699 unsigned int mem_cnt;
700 unsigned int io_cnt;
703 struct vga_arb_private {
704 struct list_head list;
705 struct pci_dev *target;
706 struct vga_arb_user_card cards[MAX_USER_CARDS];
707 spinlock_t lock;
710 static LIST_HEAD(vga_user_list);
711 static DEFINE_SPINLOCK(vga_user_lock);
715 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
716 * returns the respective values. If the string is not in this format,
717 * it returns 0.
719 static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
720 unsigned int *bus, unsigned int *devfn)
722 int n;
723 unsigned int slot, func;
726 n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
727 if (n != 4)
728 return 0;
730 *devfn = PCI_DEVFN(slot, func);
732 return 1;
735 static ssize_t vga_arb_read(struct file *file, char __user * buf,
736 size_t count, loff_t *ppos)
738 struct vga_arb_private *priv = file->private_data;
739 struct vga_device *vgadev;
740 struct pci_dev *pdev;
741 unsigned long flags;
742 size_t len;
743 int rc;
744 char *lbuf;
746 lbuf = kmalloc(1024, GFP_KERNEL);
747 if (lbuf == NULL)
748 return -ENOMEM;
750 /* Shields against vga_arb_device_card_gone (pci_dev going
751 * away), and allows access to vga list
753 spin_lock_irqsave(&vga_lock, flags);
755 /* If we are targetting the default, use it */
756 pdev = priv->target;
757 if (pdev == NULL || pdev == PCI_INVALID_CARD) {
758 spin_unlock_irqrestore(&vga_lock, flags);
759 len = sprintf(lbuf, "invalid");
760 goto done;
763 /* Find card vgadev structure */
764 vgadev = vgadev_find(pdev);
765 if (vgadev == NULL) {
766 /* Wow, it's not in the list, that shouldn't happen,
767 * let's fix us up and return invalid card
769 if (pdev == priv->target)
770 vga_arb_device_card_gone(pdev);
771 spin_unlock_irqrestore(&vga_lock, flags);
772 len = sprintf(lbuf, "invalid");
773 goto done;
776 /* Fill the buffer with infos */
777 len = snprintf(lbuf, 1024,
778 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
779 vga_decode_count, pci_name(pdev),
780 vga_iostate_to_str(vgadev->decodes),
781 vga_iostate_to_str(vgadev->owns),
782 vga_iostate_to_str(vgadev->locks),
783 vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
785 spin_unlock_irqrestore(&vga_lock, flags);
786 done:
788 /* Copy that to user */
789 if (len > count)
790 len = count;
791 rc = copy_to_user(buf, lbuf, len);
792 kfree(lbuf);
793 if (rc)
794 return -EFAULT;
795 return len;
799 * TODO: To avoid parsing inside kernel and to improve the speed we may
800 * consider use ioctl here
802 static ssize_t vga_arb_write(struct file *file, const char __user * buf,
803 size_t count, loff_t *ppos)
805 struct vga_arb_private *priv = file->private_data;
806 struct vga_arb_user_card *uc = NULL;
807 struct pci_dev *pdev;
809 unsigned int io_state;
811 char *kbuf, *curr_pos;
812 size_t remaining = count;
814 int ret_val;
815 int i;
818 kbuf = kmalloc(count + 1, GFP_KERNEL);
819 if (!kbuf)
820 return -ENOMEM;
822 if (copy_from_user(kbuf, buf, count)) {
823 kfree(kbuf);
824 return -EFAULT;
826 curr_pos = kbuf;
827 kbuf[count] = '\0'; /* Just to make sure... */
829 if (strncmp(curr_pos, "lock ", 5) == 0) {
830 curr_pos += 5;
831 remaining -= 5;
833 pr_devel("client 0x%p called 'lock'\n", priv);
835 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
836 ret_val = -EPROTO;
837 goto done;
839 if (io_state == VGA_RSRC_NONE) {
840 ret_val = -EPROTO;
841 goto done;
844 pdev = priv->target;
845 if (priv->target == NULL) {
846 ret_val = -ENODEV;
847 goto done;
850 vga_get_uninterruptible(pdev, io_state);
852 /* Update the client's locks lists... */
853 for (i = 0; i < MAX_USER_CARDS; i++) {
854 if (priv->cards[i].pdev == pdev) {
855 if (io_state & VGA_RSRC_LEGACY_IO)
856 priv->cards[i].io_cnt++;
857 if (io_state & VGA_RSRC_LEGACY_MEM)
858 priv->cards[i].mem_cnt++;
859 break;
863 ret_val = count;
864 goto done;
865 } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
866 curr_pos += 7;
867 remaining -= 7;
869 pr_devel("client 0x%p called 'unlock'\n", priv);
871 if (strncmp(curr_pos, "all", 3) == 0)
872 io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
873 else {
874 if (!vga_str_to_iostate
875 (curr_pos, remaining, &io_state)) {
876 ret_val = -EPROTO;
877 goto done;
879 /* TODO: Add this?
880 if (io_state == VGA_RSRC_NONE) {
881 ret_val = -EPROTO;
882 goto done;
887 pdev = priv->target;
888 if (priv->target == NULL) {
889 ret_val = -ENODEV;
890 goto done;
892 for (i = 0; i < MAX_USER_CARDS; i++) {
893 if (priv->cards[i].pdev == pdev)
894 uc = &priv->cards[i];
897 if (!uc)
898 return -EINVAL;
900 if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0)
901 return -EINVAL;
903 if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0)
904 return -EINVAL;
906 vga_put(pdev, io_state);
908 if (io_state & VGA_RSRC_LEGACY_IO)
909 uc->io_cnt--;
910 if (io_state & VGA_RSRC_LEGACY_MEM)
911 uc->mem_cnt--;
913 ret_val = count;
914 goto done;
915 } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
916 curr_pos += 8;
917 remaining -= 8;
919 pr_devel("client 0x%p called 'trylock'\n", priv);
921 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
922 ret_val = -EPROTO;
923 goto done;
925 /* TODO: Add this?
926 if (io_state == VGA_RSRC_NONE) {
927 ret_val = -EPROTO;
928 goto done;
932 pdev = priv->target;
933 if (priv->target == NULL) {
934 ret_val = -ENODEV;
935 goto done;
938 if (vga_tryget(pdev, io_state)) {
939 /* Update the client's locks lists... */
940 for (i = 0; i < MAX_USER_CARDS; i++) {
941 if (priv->cards[i].pdev == pdev) {
942 if (io_state & VGA_RSRC_LEGACY_IO)
943 priv->cards[i].io_cnt++;
944 if (io_state & VGA_RSRC_LEGACY_MEM)
945 priv->cards[i].mem_cnt++;
946 break;
949 ret_val = count;
950 goto done;
951 } else {
952 ret_val = -EBUSY;
953 goto done;
956 } else if (strncmp(curr_pos, "target ", 7) == 0) {
957 struct pci_bus *pbus;
958 unsigned int domain, bus, devfn;
959 struct vga_device *vgadev;
961 curr_pos += 7;
962 remaining -= 7;
963 pr_devel("client 0x%p called 'target'\n", priv);
964 /* if target is default */
965 if (!strncmp(curr_pos, "default", 7))
966 pdev = pci_dev_get(vga_default_device());
967 else {
968 if (!vga_pci_str_to_vars(curr_pos, remaining,
969 &domain, &bus, &devfn)) {
970 ret_val = -EPROTO;
971 goto done;
973 pr_devel("vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos,
974 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
976 pbus = pci_find_bus(domain, bus);
977 pr_devel("vgaarb: pbus %p\n", pbus);
978 if (pbus == NULL) {
979 pr_err("vgaarb: invalid PCI domain and/or bus address %x:%x\n",
980 domain, bus);
981 ret_val = -ENODEV;
982 goto done;
984 pdev = pci_get_slot(pbus, devfn);
985 pr_devel("vgaarb: pdev %p\n", pdev);
986 if (!pdev) {
987 pr_err("vgaarb: invalid PCI address %x:%x\n",
988 bus, devfn);
989 ret_val = -ENODEV;
990 goto done;
994 vgadev = vgadev_find(pdev);
995 pr_devel("vgaarb: vgadev %p\n", vgadev);
996 if (vgadev == NULL) {
997 pr_err("vgaarb: this pci device is not a vga device\n");
998 pci_dev_put(pdev);
999 ret_val = -ENODEV;
1000 goto done;
1003 priv->target = pdev;
1004 for (i = 0; i < MAX_USER_CARDS; i++) {
1005 if (priv->cards[i].pdev == pdev)
1006 break;
1007 if (priv->cards[i].pdev == NULL) {
1008 priv->cards[i].pdev = pdev;
1009 priv->cards[i].io_cnt = 0;
1010 priv->cards[i].mem_cnt = 0;
1011 break;
1014 if (i == MAX_USER_CARDS) {
1015 pr_err("vgaarb: maximum user cards (%d) number reached!\n",
1016 MAX_USER_CARDS);
1017 pci_dev_put(pdev);
1018 /* XXX: which value to return? */
1019 ret_val = -ENOMEM;
1020 goto done;
1023 ret_val = count;
1024 pci_dev_put(pdev);
1025 goto done;
1028 } else if (strncmp(curr_pos, "decodes ", 8) == 0) {
1029 curr_pos += 8;
1030 remaining -= 8;
1031 pr_devel("vgaarb: client 0x%p called 'decodes'\n", priv);
1033 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1034 ret_val = -EPROTO;
1035 goto done;
1037 pdev = priv->target;
1038 if (priv->target == NULL) {
1039 ret_val = -ENODEV;
1040 goto done;
1043 __vga_set_legacy_decoding(pdev, io_state, true);
1044 ret_val = count;
1045 goto done;
1047 /* If we got here, the message written is not part of the protocol! */
1048 kfree(kbuf);
1049 return -EPROTO;
1051 done:
1052 kfree(kbuf);
1053 return ret_val;
1056 static unsigned int vga_arb_fpoll(struct file *file, poll_table * wait)
1058 struct vga_arb_private *priv = file->private_data;
1060 pr_devel("%s\n", __func__);
1062 if (priv == NULL)
1063 return -ENODEV;
1064 poll_wait(file, &vga_wait_queue, wait);
1065 return POLLIN;
1068 static int vga_arb_open(struct inode *inode, struct file *file)
1070 struct vga_arb_private *priv;
1071 unsigned long flags;
1073 pr_devel("%s\n", __func__);
1075 priv = kmalloc(sizeof(struct vga_arb_private), GFP_KERNEL);
1076 if (priv == NULL)
1077 return -ENOMEM;
1078 memset(priv, 0, sizeof(*priv));
1079 spin_lock_init(&priv->lock);
1080 file->private_data = priv;
1082 spin_lock_irqsave(&vga_user_lock, flags);
1083 list_add(&priv->list, &vga_user_list);
1084 spin_unlock_irqrestore(&vga_user_lock, flags);
1086 /* Set the client' lists of locks */
1087 priv->target = vga_default_device(); /* Maybe this is still null! */
1088 priv->cards[0].pdev = priv->target;
1089 priv->cards[0].io_cnt = 0;
1090 priv->cards[0].mem_cnt = 0;
1093 return 0;
1096 static int vga_arb_release(struct inode *inode, struct file *file)
1098 struct vga_arb_private *priv = file->private_data;
1099 struct vga_arb_user_card *uc;
1100 unsigned long flags;
1101 int i;
1103 pr_devel("%s\n", __func__);
1105 if (priv == NULL)
1106 return -ENODEV;
1108 spin_lock_irqsave(&vga_user_lock, flags);
1109 list_del(&priv->list);
1110 for (i = 0; i < MAX_USER_CARDS; i++) {
1111 uc = &priv->cards[i];
1112 if (uc->pdev == NULL)
1113 continue;
1114 pr_devel("uc->io_cnt == %d, uc->mem_cnt == %d\n",
1115 uc->io_cnt, uc->mem_cnt);
1116 while (uc->io_cnt--)
1117 vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
1118 while (uc->mem_cnt--)
1119 vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
1121 spin_unlock_irqrestore(&vga_user_lock, flags);
1123 kfree(priv);
1125 return 0;
1128 static void vga_arb_device_card_gone(struct pci_dev *pdev)
1133 * callback any registered clients to let them know we have a
1134 * change in VGA cards
1136 static void vga_arbiter_notify_clients(void)
1138 struct vga_device *vgadev;
1139 unsigned long flags;
1140 uint32_t new_decodes;
1141 bool new_state;
1143 if (!vga_arbiter_used)
1144 return;
1146 spin_lock_irqsave(&vga_lock, flags);
1147 list_for_each_entry(vgadev, &vga_list, list) {
1148 if (vga_count > 1)
1149 new_state = false;
1150 else
1151 new_state = true;
1152 if (vgadev->set_vga_decode) {
1153 new_decodes = vgadev->set_vga_decode(vgadev->cookie, new_state);
1154 vga_update_device_decodes(vgadev, new_decodes);
1157 spin_unlock_irqrestore(&vga_lock, flags);
1160 static int pci_notify(struct notifier_block *nb, unsigned long action,
1161 void *data)
1163 struct device *dev = data;
1164 struct pci_dev *pdev = to_pci_dev(dev);
1165 bool notify = false;
1167 pr_devel("%s\n", __func__);
1169 /* For now we're only intereted in devices added and removed. I didn't
1170 * test this thing here, so someone needs to double check for the
1171 * cases of hotplugable vga cards. */
1172 if (action == BUS_NOTIFY_ADD_DEVICE)
1173 notify = vga_arbiter_add_pci_device(pdev);
1174 else if (action == BUS_NOTIFY_DEL_DEVICE)
1175 notify = vga_arbiter_del_pci_device(pdev);
1177 if (notify)
1178 vga_arbiter_notify_clients();
1179 return 0;
1182 static struct notifier_block pci_notifier = {
1183 .notifier_call = pci_notify,
1186 static const struct file_operations vga_arb_device_fops = {
1187 .read = vga_arb_read,
1188 .write = vga_arb_write,
1189 .poll = vga_arb_fpoll,
1190 .open = vga_arb_open,
1191 .release = vga_arb_release,
1194 static struct miscdevice vga_arb_device = {
1195 MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
1198 static int __init vga_arb_device_init(void)
1200 int rc;
1201 struct pci_dev *pdev;
1203 rc = misc_register(&vga_arb_device);
1204 if (rc < 0)
1205 pr_err("vgaarb: error %d registering device\n", rc);
1207 bus_register_notifier(&pci_bus_type, &pci_notifier);
1209 /* We add all pci devices satisfying vga class in the arbiter by
1210 * default */
1211 pdev = NULL;
1212 while ((pdev =
1213 pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
1214 PCI_ANY_ID, pdev)) != NULL)
1215 vga_arbiter_add_pci_device(pdev);
1217 pr_info("vgaarb: loaded\n");
1218 return rc;
1220 subsys_initcall(vga_arb_device_init);