bna: remove oper_state_cbfn from struct bna_rxf
[linux/fpc-iii.git] / drivers / gpu / vga / vgaarb.c
blob7bcbf863656ec6f2c1e76d6156365fc0548230ff
1 /*
2 * vgaarb.c: Implements the VGA arbitration. For details refer to
3 * Documentation/vgaarbiter.txt
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>
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:
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.
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.
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>
46 #include <linux/uaccess.h>
48 #include <linux/vgaarb.h>
50 static void vga_arbiter_notify_clients(void);
52 * We keep a list of all vga devices in the system to speed
53 * up the various operations of the arbiter
55 struct 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);
72 static LIST_HEAD(vga_list);
73 static int vga_count, vga_decode_count;
74 static bool vga_arbiter_used;
75 static DEFINE_SPINLOCK(vga_lock);
76 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
79 static const char *vga_iostate_to_str(unsigned int iostate)
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";
91 return "none";
94 static int vga_str_to_iostate(char *buf, int str_size, int *io_state)
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;
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;
111 both:
112 *io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
113 return 1;
116 /* this is only used a cookie - it should not be dereferenced */
117 static struct pci_dev *vga_default;
119 static void vga_arb_device_card_gone(struct pci_dev *pdev);
121 /* Find somebody in our list */
122 static struct vga_device *vgadev_find(struct pci_dev *pdev)
124 struct vga_device *vgadev;
126 list_for_each_entry(vgadev, &vga_list, list)
127 if (pdev == vgadev->pdev)
128 return vgadev;
129 return NULL;
132 /* Returns the default VGA device (vgacon's babe) */
133 struct pci_dev *vga_default_device(void)
135 return vga_default;
138 EXPORT_SYMBOL_GPL(vga_default_device);
140 void vga_set_default_device(struct pci_dev *pdev)
142 if (vga_default == pdev)
143 return;
145 pci_dev_put(vga_default);
146 vga_default = pci_dev_get(pdev);
149 static inline void vga_irq_set_state(struct vga_device *vgadev, bool state)
151 if (vgadev->irq_set_state)
152 vgadev->irq_set_state(vgadev->cookie, state);
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 */
159 static void vga_check_first_use(void)
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();
170 static struct vga_device *__vga_tryget(struct vga_device *vgadev,
171 unsigned int rsrc)
173 unsigned int wants, legacy_wants, match;
174 struct vga_device *conflict;
175 unsigned int pci_bits;
176 u32 flags = 0;
178 /* Account for "normal" resources to lock. If we decode the legacy,
179 * counterpart, we need to request it as well
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;
188 pr_debug("%s: %d\n", __func__, rsrc);
189 pr_debug("%s: owns: %d\n", __func__, vgadev->owns);
191 /* Check what resources we need to acquire */
192 wants = rsrc & ~vgadev->owns;
194 /* We already own everything, just mark locked & bye bye */
195 if (wants == 0)
196 goto lock_them;
198 /* We don't need to request a legacy resource, we just enable
199 * appropriate decoding and go
201 legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
202 if (legacy_wants == 0)
203 goto enable_them;
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;
210 /* Don't conflict with myself */
211 if (vgadev == conflict)
212 continue;
214 /* Check if the architecture allows a conflict between those
215 * 2 devices or if they are on separate domains
217 if (!vga_conflicts(vgadev->pdev, conflict->pdev))
218 continue;
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
226 if (vgadev->pdev->bus != conflict->pdev->bus) {
227 change_bridge = 1;
228 lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
231 /* Check if the guy has a lock on the resource. If he does,
232 * return the conflicting entry
234 if (conflict->locks & lwants)
235 return conflict;
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.
241 match = lwants & conflict->owns;
242 if (!match)
243 continue;
245 /* looks like he doesn't have a lock, we can steal
246 * them from him
249 flags = 0;
250 pci_bits = 0;
252 /* If we can't control legacy resources via the bridge, we
253 * also need to disable normal decoding.
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;
261 if (pci_bits) {
262 vga_irq_set_state(conflict, false);
263 flags |= PCI_VGA_STATE_CHANGE_DECODES;
267 if (change_bridge)
268 flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
270 pci_set_vga_state(conflict->pdev, false, pci_bits, flags);
271 conflict->owns &= ~match;
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;
280 enable_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".
286 flags = 0;
287 pci_bits = 0;
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;
296 if (wants & VGA_RSRC_LEGACY_MASK)
297 flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
299 pci_set_vga_state(vgadev->pdev, true, pci_bits, flags);
301 if (!vgadev->bridge_has_one_vga) {
302 vga_irq_set_state(vgadev, true);
304 vgadev->owns |= wants;
305 lock_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++;
316 return NULL;
319 static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
321 unsigned int old_locks = vgadev->locks;
323 pr_debug("%s\n", __func__);
325 /* Update our counters, and account for equivalent legacy resources
326 * if we decode them
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;
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;
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--;
343 /* Just clear lock bits, we do lazy operations so we don't really
344 * have to bother about anything else at this point
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;
351 /* Kick the wait queue in case somebody was waiting if we actually
352 * released something
354 if (old_locks != vgadev->locks)
355 wake_up_all(&vga_wait_queue);
358 int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
360 struct vga_device *vgadev, *conflict;
361 unsigned long flags;
362 wait_queue_t wait;
363 int rc = 0;
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;
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;
380 conflict = __vga_tryget(vgadev, rsrc);
381 spin_unlock_irqrestore(&vga_lock, flags);
382 if (conflict == NULL)
383 break;
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
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;
401 schedule();
402 remove_wait_queue(&vga_wait_queue, &wait);
404 return rc;
406 EXPORT_SYMBOL(vga_get);
408 int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
410 struct vga_device *vgadev;
411 unsigned long flags;
412 int rc = 0;
414 vga_check_first_use();
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;
427 if (__vga_tryget(vgadev, rsrc))
428 rc = -EBUSY;
429 bail:
430 spin_unlock_irqrestore(&vga_lock, flags);
431 return rc;
433 EXPORT_SYMBOL(vga_tryget);
435 void vga_put(struct pci_dev *pdev, unsigned int rsrc)
437 struct vga_device *vgadev;
438 unsigned long flags;
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);
450 bail:
451 spin_unlock_irqrestore(&vga_lock, flags);
453 EXPORT_SYMBOL(vga_put);
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.
464 static void vga_arbiter_check_bridge_sharing(struct vga_device *vgadev)
466 struct vga_device *same_bridge_vgadev;
467 struct pci_bus *new_bus, *bus;
468 struct pci_dev *new_bridge, *bridge;
470 vgadev->bridge_has_one_vga = true;
472 if (list_empty(&vga_list))
473 return;
475 /* okay iterate the new devices bridge hierarachy */
476 new_bus = vgadev->pdev->bus;
477 while (new_bus) {
478 new_bridge = new_bus->self;
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;
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;
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;
502 bus = bus->parent;
505 new_bus = new_bus->parent;
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.
515 static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
517 struct vga_device *vgadev;
518 unsigned long flags;
519 struct pci_bus *bus;
520 struct pci_dev *bridge;
521 u16 cmd;
523 /* Only deal with VGA class devices */
524 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
525 return false;
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
535 return false;
538 memset(vgadev, 0, sizeof(*vgadev));
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;
546 vgadev->pdev = pdev;
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;
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
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;
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;
576 bus = bus->parent;
579 /* Deal with VGA default device. Use first enabled one
580 * by default if arch doesn't have it's own hook
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);
589 vga_arbiter_check_bridge_sharing(vgadev);
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));
600 spin_unlock_irqrestore(&vga_lock, flags);
601 return true;
602 fail:
603 spin_unlock_irqrestore(&vga_lock, flags);
604 kfree(vgadev);
605 return false;
608 static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
610 struct vga_device *vgadev;
611 unsigned long flags;
612 bool ret = true;
614 spin_lock_irqsave(&vga_lock, flags);
615 vgadev = vgadev_find(pdev);
616 if (vgadev == NULL) {
617 ret = false;
618 goto bail;
621 if (vga_default == pdev)
622 vga_set_default_device(NULL);
624 if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
625 vga_decode_count--;
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
633 vga_arb_device_card_gone(pdev);
635 /* Wake up all possible waiters */
636 wake_up_all(&vga_wait_queue);
637 bail:
638 spin_unlock_irqrestore(&vga_lock, flags);
639 kfree(vgadev);
640 return ret;
643 /* this is called with the lock */
644 static inline void vga_update_device_decodes(struct vga_device *vgadev,
645 int new_decodes)
647 int old_decodes, decodes_removed, decodes_unlocked;
649 old_decodes = vgadev->decodes;
650 decodes_removed = ~new_decodes & old_decodes;
651 decodes_unlocked = vgadev->locks & decodes_removed;
652 vgadev->decodes = new_decodes;
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));
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);
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);
679 static void __vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes, bool userspace)
681 struct vga_device *vgadev;
682 unsigned long flags;
684 decodes &= VGA_RSRC_LEGACY_MASK;
686 spin_lock_irqsave(&vga_lock, flags);
687 vgadev = vgadev_find(pdev);
688 if (vgadev == NULL)
689 goto bail;
691 /* don't let userspace futz with kernel driver decodes */
692 if (userspace && vgadev->set_vga_decode)
693 goto bail;
695 /* update the device decodes + counter */
696 vga_update_device_decodes(vgadev, decodes);
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 ...
702 bail:
703 spin_unlock_irqrestore(&vga_lock, flags);
706 void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
708 __vga_set_legacy_decoding(pdev, decodes, false);
710 EXPORT_SYMBOL(vga_set_legacy_decoding);
712 /* call with NULL to unregister */
713 int 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))
717 int ret = -ENODEV;
718 struct vga_device *vgadev;
719 unsigned long flags;
721 spin_lock_irqsave(&vga_lock, flags);
722 vgadev = vgadev_find(pdev);
723 if (!vgadev)
724 goto bail;
726 vgadev->irq_set_state = irq_set_state;
727 vgadev->set_vga_decode = set_vga_decode;
728 vgadev->cookie = cookie;
729 ret = 0;
731 bail:
732 spin_unlock_irqrestore(&vga_lock, flags);
733 return ret;
736 EXPORT_SYMBOL(vga_client_register);
739 * Char driver implementation
741 * Semantics is:
743 * open : open user instance of the arbitrer. by default, it's
744 * attached to the default VGA device of the system.
746 * close : close user instance, release locks
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
758 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
760 * write : write a command to the arbiter. List of commands is:
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
769 * poll : event if something change on any card (not just the target)
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.
777 * Note about locks:
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.
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.
791 #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
792 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
795 * Each user has an array of these, tracking which cards have locks
797 struct vga_arb_user_card {
798 struct pci_dev *pdev;
799 unsigned int mem_cnt;
800 unsigned int io_cnt;
803 struct 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;
810 static LIST_HEAD(vga_user_list);
811 static DEFINE_SPINLOCK(vga_user_lock);
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.
819 static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
820 unsigned int *bus, unsigned int *devfn)
822 int n;
823 unsigned int slot, func;
826 n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
827 if (n != 4)
828 return 0;
830 *devfn = PCI_DEVFN(slot, func);
832 return 1;
835 static ssize_t vga_arb_read(struct file *file, char __user * buf,
836 size_t count, loff_t *ppos)
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;
846 lbuf = kmalloc(1024, GFP_KERNEL);
847 if (lbuf == NULL)
848 return -ENOMEM;
850 /* Shields against vga_arb_device_card_gone (pci_dev going
851 * away), and allows access to vga list
853 spin_lock_irqsave(&vga_lock, flags);
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;
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
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;
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);
885 spin_unlock_irqrestore(&vga_lock, flags);
886 done:
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;
899 * TODO: To avoid parsing inside kernel and to improve the speed we may
900 * consider use ioctl here
902 static ssize_t vga_arb_write(struct file *file, const char __user * buf,
903 size_t count, loff_t *ppos)
905 struct vga_arb_private *priv = file->private_data;
906 struct vga_arb_user_card *uc = NULL;
907 struct pci_dev *pdev;
909 unsigned int io_state;
911 char *kbuf, *curr_pos;
912 size_t remaining = count;
914 int ret_val;
915 int i;
918 kbuf = kmalloc(count + 1, GFP_KERNEL);
919 if (!kbuf)
920 return -ENOMEM;
922 if (copy_from_user(kbuf, buf, count)) {
923 kfree(kbuf);
924 return -EFAULT;
926 curr_pos = kbuf;
927 kbuf[count] = '\0'; /* Just to make sure... */
929 if (strncmp(curr_pos, "lock ", 5) == 0) {
930 curr_pos += 5;
931 remaining -= 5;
933 pr_debug("client 0x%p called 'lock'\n", priv);
935 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
936 ret_val = -EPROTO;
937 goto done;
939 if (io_state == VGA_RSRC_NONE) {
940 ret_val = -EPROTO;
941 goto done;
944 pdev = priv->target;
945 if (priv->target == NULL) {
946 ret_val = -ENODEV;
947 goto done;
950 vga_get_uninterruptible(pdev, io_state);
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;
963 ret_val = count;
964 goto done;
965 } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
966 curr_pos += 7;
967 remaining -= 7;
969 pr_debug("client 0x%p called 'unlock'\n", priv);
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;
979 /* TODO: Add this?
980 if (io_state == VGA_RSRC_NONE) {
981 ret_val = -EPROTO;
982 goto done;
987 pdev = priv->target;
988 if (priv->target == NULL) {
989 ret_val = -ENODEV;
990 goto done;
992 for (i = 0; i < MAX_USER_CARDS; i++) {
993 if (priv->cards[i].pdev == pdev)
994 uc = &priv->cards[i];
997 if (!uc) {
998 ret_val = -EINVAL;
999 goto done;
1002 if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
1003 ret_val = -EINVAL;
1004 goto done;
1007 if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
1008 ret_val = -EINVAL;
1009 goto done;
1012 vga_put(pdev, io_state);
1014 if (io_state & VGA_RSRC_LEGACY_IO)
1015 uc->io_cnt--;
1016 if (io_state & VGA_RSRC_LEGACY_MEM)
1017 uc->mem_cnt--;
1019 ret_val = count;
1020 goto done;
1021 } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
1022 curr_pos += 8;
1023 remaining -= 8;
1025 pr_debug("client 0x%p called 'trylock'\n", priv);
1027 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1028 ret_val = -EPROTO;
1029 goto done;
1031 /* TODO: Add this?
1032 if (io_state == VGA_RSRC_NONE) {
1033 ret_val = -EPROTO;
1034 goto done;
1038 pdev = priv->target;
1039 if (priv->target == NULL) {
1040 ret_val = -ENODEV;
1041 goto done;
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;
1055 ret_val = count;
1056 goto done;
1057 } else {
1058 ret_val = -EBUSY;
1059 goto done;
1062 } else if (strncmp(curr_pos, "target ", 7) == 0) {
1063 unsigned int domain, bus, devfn;
1064 struct vga_device *vgadev;
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;
1078 pr_debug("vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos,
1079 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
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;
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;
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;
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;
1120 ret_val = count;
1121 pci_dev_put(pdev);
1122 goto done;
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);
1130 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1131 ret_val = -EPROTO;
1132 goto done;
1134 pdev = priv->target;
1135 if (priv->target == NULL) {
1136 ret_val = -ENODEV;
1137 goto done;
1140 __vga_set_legacy_decoding(pdev, io_state, true);
1141 ret_val = count;
1142 goto done;
1144 /* If we got here, the message written is not part of the protocol! */
1145 kfree(kbuf);
1146 return -EPROTO;
1148 done:
1149 kfree(kbuf);
1150 return ret_val;
1153 static unsigned int vga_arb_fpoll(struct file *file, poll_table * wait)
1155 struct vga_arb_private *priv = file->private_data;
1157 pr_debug("%s\n", __func__);
1159 if (priv == NULL)
1160 return -ENODEV;
1161 poll_wait(file, &vga_wait_queue, wait);
1162 return POLLIN;
1165 static int vga_arb_open(struct inode *inode, struct file *file)
1167 struct vga_arb_private *priv;
1168 unsigned long flags;
1170 pr_debug("%s\n", __func__);
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;
1178 spin_lock_irqsave(&vga_user_lock, flags);
1179 list_add(&priv->list, &vga_user_list);
1180 spin_unlock_irqrestore(&vga_user_lock, flags);
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;
1189 return 0;
1192 static int vga_arb_release(struct inode *inode, struct file *file)
1194 struct vga_arb_private *priv = file->private_data;
1195 struct vga_arb_user_card *uc;
1196 unsigned long flags;
1197 int i;
1199 pr_debug("%s\n", __func__);
1201 if (priv == NULL)
1202 return -ENODEV;
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);
1217 spin_unlock_irqrestore(&vga_user_lock, flags);
1219 kfree(priv);
1221 return 0;
1224 static void vga_arb_device_card_gone(struct pci_dev *pdev)
1229 * callback any registered clients to let them know we have a
1230 * change in VGA cards
1232 static void vga_arbiter_notify_clients(void)
1234 struct vga_device *vgadev;
1235 unsigned long flags;
1236 uint32_t new_decodes;
1237 bool new_state;
1239 if (!vga_arbiter_used)
1240 return;
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);
1253 spin_unlock_irqrestore(&vga_lock, flags);
1256 static int pci_notify(struct notifier_block *nb, unsigned long action,
1257 void *data)
1259 struct device *dev = data;
1260 struct pci_dev *pdev = to_pci_dev(dev);
1261 bool notify = false;
1263 pr_debug("%s\n", __func__);
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);
1273 if (notify)
1274 vga_arbiter_notify_clients();
1275 return 0;
1278 static struct notifier_block pci_notifier = {
1279 .notifier_call = pci_notify,
1282 static 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,
1291 static struct miscdevice vga_arb_device = {
1292 MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
1295 static int __init vga_arb_device_init(void)
1297 int rc;
1298 struct pci_dev *pdev;
1299 struct vga_device *vgadev;
1301 rc = misc_register(&vga_arb_device);
1302 if (rc < 0)
1303 pr_err("vgaarb: error %d registering device\n", rc);
1305 bus_register_notifier(&pci_bus_type, &pci_notifier);
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);
1315 pr_info("vgaarb: loaded\n");
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).
1322 * Select the device owning the boot framebuffer if there is one.
1324 resource_size_t start, end;
1325 int i;
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;
1332 start = pci_resource_start(vgadev->pdev, i);
1333 end = pci_resource_end(vgadev->pdev, i);
1335 if (!start || !end)
1336 continue;
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);
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));
1355 return rc;
1357 subsys_initcall(vga_arb_device_init);