FRV: Use generic show_interrupts()
[cris-mirror.git] / drivers / gpu / vga / vgaarb.c
blobace2b1623b214671d6e49e115d4b3c2f3755230e
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>
45 #include <linux/uaccess.h>
47 #include <linux/vgaarb.h>
49 static void vga_arbiter_notify_clients(void);
51 * We keep a list of all vga devices in the system to speed
52 * up the various operations of the arbiter
54 struct vga_device {
55 struct list_head list;
56 struct pci_dev *pdev;
57 unsigned int decodes; /* what does it decodes */
58 unsigned int owns; /* what does it owns */
59 unsigned int locks; /* what does it locks */
60 unsigned int io_lock_cnt; /* legacy IO lock count */
61 unsigned int mem_lock_cnt; /* legacy MEM lock count */
62 unsigned int io_norm_cnt; /* normal IO count */
63 unsigned int mem_norm_cnt; /* normal MEM count */
65 /* allow IRQ enable/disable hook */
66 void *cookie;
67 void (*irq_set_state)(void *cookie, bool enable);
68 unsigned int (*set_vga_decode)(void *cookie, bool decode);
71 static LIST_HEAD(vga_list);
72 static int vga_count, vga_decode_count;
73 static bool vga_arbiter_used;
74 static DEFINE_SPINLOCK(vga_lock);
75 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
78 static const char *vga_iostate_to_str(unsigned int iostate)
80 /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
81 iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
82 switch (iostate) {
83 case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
84 return "io+mem";
85 case VGA_RSRC_LEGACY_IO:
86 return "io";
87 case VGA_RSRC_LEGACY_MEM:
88 return "mem";
90 return "none";
93 static int vga_str_to_iostate(char *buf, int str_size, int *io_state)
95 /* we could in theory hand out locks on IO and mem
96 * separately to userspace but it can cause deadlocks */
97 if (strncmp(buf, "none", 4) == 0) {
98 *io_state = VGA_RSRC_NONE;
99 return 1;
102 /* XXX We're not chekcing the str_size! */
103 if (strncmp(buf, "io+mem", 6) == 0)
104 goto both;
105 else if (strncmp(buf, "io", 2) == 0)
106 goto both;
107 else if (strncmp(buf, "mem", 3) == 0)
108 goto both;
109 return 0;
110 both:
111 *io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
112 return 1;
115 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
116 /* this is only used a cookie - it should not be dereferenced */
117 static struct pci_dev *vga_default;
118 #endif
120 static void vga_arb_device_card_gone(struct pci_dev *pdev);
122 /* Find somebody in our list */
123 static struct vga_device *vgadev_find(struct pci_dev *pdev)
125 struct vga_device *vgadev;
127 list_for_each_entry(vgadev, &vga_list, list)
128 if (pdev == vgadev->pdev)
129 return vgadev;
130 return NULL;
133 /* Returns the default VGA device (vgacon's babe) */
134 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
135 struct pci_dev *vga_default_device(void)
137 return vga_default;
139 #endif
141 static inline void vga_irq_set_state(struct vga_device *vgadev, bool state)
143 if (vgadev->irq_set_state)
144 vgadev->irq_set_state(vgadev->cookie, state);
148 /* If we don't ever use VGA arb we should avoid
149 turning off anything anywhere due to old X servers getting
150 confused about the boot device not being VGA */
151 static void vga_check_first_use(void)
153 /* we should inform all GPUs in the system that
154 * VGA arb has occured and to try and disable resources
155 * if they can */
156 if (!vga_arbiter_used) {
157 vga_arbiter_used = true;
158 vga_arbiter_notify_clients();
162 static struct vga_device *__vga_tryget(struct vga_device *vgadev,
163 unsigned int rsrc)
165 unsigned int wants, legacy_wants, match;
166 struct vga_device *conflict;
167 unsigned int pci_bits;
168 /* Account for "normal" resources to lock. If we decode the legacy,
169 * counterpart, we need to request it as well
171 if ((rsrc & VGA_RSRC_NORMAL_IO) &&
172 (vgadev->decodes & VGA_RSRC_LEGACY_IO))
173 rsrc |= VGA_RSRC_LEGACY_IO;
174 if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
175 (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
176 rsrc |= VGA_RSRC_LEGACY_MEM;
178 pr_debug("%s: %d\n", __func__, rsrc);
179 pr_debug("%s: owns: %d\n", __func__, vgadev->owns);
181 /* Check what resources we need to acquire */
182 wants = rsrc & ~vgadev->owns;
184 /* We already own everything, just mark locked & bye bye */
185 if (wants == 0)
186 goto lock_them;
188 /* We don't need to request a legacy resource, we just enable
189 * appropriate decoding and go
191 legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
192 if (legacy_wants == 0)
193 goto enable_them;
195 /* Ok, we don't, let's find out how we need to kick off */
196 list_for_each_entry(conflict, &vga_list, list) {
197 unsigned int lwants = legacy_wants;
198 unsigned int change_bridge = 0;
200 /* Don't conflict with myself */
201 if (vgadev == conflict)
202 continue;
204 /* Check if the architecture allows a conflict between those
205 * 2 devices or if they are on separate domains
207 if (!vga_conflicts(vgadev->pdev, conflict->pdev))
208 continue;
210 /* We have a possible conflict. before we go further, we must
211 * check if we sit on the same bus as the conflicting device.
212 * if we don't, then we must tie both IO and MEM resources
213 * together since there is only a single bit controlling
214 * VGA forwarding on P2P bridges
216 if (vgadev->pdev->bus != conflict->pdev->bus) {
217 change_bridge = 1;
218 lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
221 /* Check if the guy has a lock on the resource. If he does,
222 * return the conflicting entry
224 if (conflict->locks & lwants)
225 return conflict;
227 /* Ok, now check if he owns the resource we want. We don't need
228 * to check "decodes" since it should be impossible to own
229 * own legacy resources you don't decode unless I have a bug
230 * in this code...
232 WARN_ON(conflict->owns & ~conflict->decodes);
233 match = lwants & conflict->owns;
234 if (!match)
235 continue;
237 /* looks like he doesn't have a lock, we can steal
238 * them from him
240 vga_irq_set_state(conflict, false);
242 pci_bits = 0;
243 if (lwants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
244 pci_bits |= PCI_COMMAND_MEMORY;
245 if (lwants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
246 pci_bits |= PCI_COMMAND_IO;
248 pci_set_vga_state(conflict->pdev, false, pci_bits,
249 change_bridge);
250 conflict->owns &= ~lwants;
251 /* If he also owned non-legacy, that is no longer the case */
252 if (lwants & VGA_RSRC_LEGACY_MEM)
253 conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
254 if (lwants & VGA_RSRC_LEGACY_IO)
255 conflict->owns &= ~VGA_RSRC_NORMAL_IO;
258 enable_them:
259 /* ok dude, we got it, everybody conflicting has been disabled, let's
260 * enable us. Make sure we don't mark a bit in "owns" that we don't
261 * also have in "decodes". We can lock resources we don't decode but
262 * not own them.
264 pci_bits = 0;
265 if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
266 pci_bits |= PCI_COMMAND_MEMORY;
267 if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
268 pci_bits |= PCI_COMMAND_IO;
269 pci_set_vga_state(vgadev->pdev, true, pci_bits, !!(wants & VGA_RSRC_LEGACY_MASK));
271 vga_irq_set_state(vgadev, true);
272 vgadev->owns |= (wants & vgadev->decodes);
273 lock_them:
274 vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
275 if (rsrc & VGA_RSRC_LEGACY_IO)
276 vgadev->io_lock_cnt++;
277 if (rsrc & VGA_RSRC_LEGACY_MEM)
278 vgadev->mem_lock_cnt++;
279 if (rsrc & VGA_RSRC_NORMAL_IO)
280 vgadev->io_norm_cnt++;
281 if (rsrc & VGA_RSRC_NORMAL_MEM)
282 vgadev->mem_norm_cnt++;
284 return NULL;
287 static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
289 unsigned int old_locks = vgadev->locks;
291 pr_debug("%s\n", __func__);
293 /* Update our counters, and account for equivalent legacy resources
294 * if we decode them
296 if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
297 vgadev->io_norm_cnt--;
298 if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
299 rsrc |= VGA_RSRC_LEGACY_IO;
301 if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
302 vgadev->mem_norm_cnt--;
303 if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
304 rsrc |= VGA_RSRC_LEGACY_MEM;
306 if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
307 vgadev->io_lock_cnt--;
308 if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
309 vgadev->mem_lock_cnt--;
311 /* Just clear lock bits, we do lazy operations so we don't really
312 * have to bother about anything else at this point
314 if (vgadev->io_lock_cnt == 0)
315 vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
316 if (vgadev->mem_lock_cnt == 0)
317 vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
319 /* Kick the wait queue in case somebody was waiting if we actually
320 * released something
322 if (old_locks != vgadev->locks)
323 wake_up_all(&vga_wait_queue);
326 int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
328 struct vga_device *vgadev, *conflict;
329 unsigned long flags;
330 wait_queue_t wait;
331 int rc = 0;
333 vga_check_first_use();
334 /* The one who calls us should check for this, but lets be sure... */
335 if (pdev == NULL)
336 pdev = vga_default_device();
337 if (pdev == NULL)
338 return 0;
340 for (;;) {
341 spin_lock_irqsave(&vga_lock, flags);
342 vgadev = vgadev_find(pdev);
343 if (vgadev == NULL) {
344 spin_unlock_irqrestore(&vga_lock, flags);
345 rc = -ENODEV;
346 break;
348 conflict = __vga_tryget(vgadev, rsrc);
349 spin_unlock_irqrestore(&vga_lock, flags);
350 if (conflict == NULL)
351 break;
354 /* We have a conflict, we wait until somebody kicks the
355 * work queue. Currently we have one work queue that we
356 * kick each time some resources are released, but it would
357 * be fairly easy to have a per device one so that we only
358 * need to attach to the conflicting device
360 init_waitqueue_entry(&wait, current);
361 add_wait_queue(&vga_wait_queue, &wait);
362 set_current_state(interruptible ?
363 TASK_INTERRUPTIBLE :
364 TASK_UNINTERRUPTIBLE);
365 if (signal_pending(current)) {
366 rc = -EINTR;
367 break;
369 schedule();
370 remove_wait_queue(&vga_wait_queue, &wait);
371 set_current_state(TASK_RUNNING);
373 return rc;
375 EXPORT_SYMBOL(vga_get);
377 int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
379 struct vga_device *vgadev;
380 unsigned long flags;
381 int rc = 0;
383 vga_check_first_use();
385 /* The one who calls us should check for this, but lets be sure... */
386 if (pdev == NULL)
387 pdev = vga_default_device();
388 if (pdev == NULL)
389 return 0;
390 spin_lock_irqsave(&vga_lock, flags);
391 vgadev = vgadev_find(pdev);
392 if (vgadev == NULL) {
393 rc = -ENODEV;
394 goto bail;
396 if (__vga_tryget(vgadev, rsrc))
397 rc = -EBUSY;
398 bail:
399 spin_unlock_irqrestore(&vga_lock, flags);
400 return rc;
402 EXPORT_SYMBOL(vga_tryget);
404 void vga_put(struct pci_dev *pdev, unsigned int rsrc)
406 struct vga_device *vgadev;
407 unsigned long flags;
409 /* The one who calls us should check for this, but lets be sure... */
410 if (pdev == NULL)
411 pdev = vga_default_device();
412 if (pdev == NULL)
413 return;
414 spin_lock_irqsave(&vga_lock, flags);
415 vgadev = vgadev_find(pdev);
416 if (vgadev == NULL)
417 goto bail;
418 __vga_put(vgadev, rsrc);
419 bail:
420 spin_unlock_irqrestore(&vga_lock, flags);
422 EXPORT_SYMBOL(vga_put);
425 * Currently, we assume that the "initial" setup of the system is
426 * not sane, that is we come up with conflicting devices and let
427 * the arbiter's client decides if devices decodes or not legacy
428 * things.
430 static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
432 struct vga_device *vgadev;
433 unsigned long flags;
434 struct pci_bus *bus;
435 struct pci_dev *bridge;
436 u16 cmd;
438 /* Only deal with VGA class devices */
439 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
440 return false;
442 /* Allocate structure */
443 vgadev = kmalloc(sizeof(struct vga_device), GFP_KERNEL);
444 if (vgadev == NULL) {
445 pr_err("vgaarb: failed to allocate pci device\n");
446 /* What to do on allocation failure ? For now, let's
447 * just do nothing, I'm not sure there is anything saner
448 * to be done
450 return false;
453 memset(vgadev, 0, sizeof(*vgadev));
455 /* Take lock & check for duplicates */
456 spin_lock_irqsave(&vga_lock, flags);
457 if (vgadev_find(pdev) != NULL) {
458 BUG_ON(1);
459 goto fail;
461 vgadev->pdev = pdev;
463 /* By default, assume we decode everything */
464 vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
465 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
467 /* by default mark it as decoding */
468 vga_decode_count++;
469 /* Mark that we "own" resources based on our enables, we will
470 * clear that below if the bridge isn't forwarding
472 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
473 if (cmd & PCI_COMMAND_IO)
474 vgadev->owns |= VGA_RSRC_LEGACY_IO;
475 if (cmd & PCI_COMMAND_MEMORY)
476 vgadev->owns |= VGA_RSRC_LEGACY_MEM;
478 /* Check if VGA cycles can get down to us */
479 bus = pdev->bus;
480 while (bus) {
481 bridge = bus->self;
482 if (bridge) {
483 u16 l;
484 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
485 &l);
486 if (!(l & PCI_BRIDGE_CTL_VGA)) {
487 vgadev->owns = 0;
488 break;
491 bus = bus->parent;
494 /* Deal with VGA default device. Use first enabled one
495 * by default if arch doesn't have it's own hook
497 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
498 if (vga_default == NULL &&
499 ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK))
500 vga_default = pci_dev_get(pdev);
501 #endif
503 /* Add to the list */
504 list_add(&vgadev->list, &vga_list);
505 vga_count++;
506 pr_info("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
507 pci_name(pdev),
508 vga_iostate_to_str(vgadev->decodes),
509 vga_iostate_to_str(vgadev->owns),
510 vga_iostate_to_str(vgadev->locks));
512 spin_unlock_irqrestore(&vga_lock, flags);
513 return true;
514 fail:
515 spin_unlock_irqrestore(&vga_lock, flags);
516 kfree(vgadev);
517 return false;
520 static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
522 struct vga_device *vgadev;
523 unsigned long flags;
524 bool ret = true;
526 spin_lock_irqsave(&vga_lock, flags);
527 vgadev = vgadev_find(pdev);
528 if (vgadev == NULL) {
529 ret = false;
530 goto bail;
533 if (vga_default == pdev) {
534 pci_dev_put(vga_default);
535 vga_default = NULL;
538 if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
539 vga_decode_count--;
541 /* Remove entry from list */
542 list_del(&vgadev->list);
543 vga_count--;
544 /* Notify userland driver that the device is gone so it discards
545 * it's copies of the pci_dev pointer
547 vga_arb_device_card_gone(pdev);
549 /* Wake up all possible waiters */
550 wake_up_all(&vga_wait_queue);
551 bail:
552 spin_unlock_irqrestore(&vga_lock, flags);
553 kfree(vgadev);
554 return ret;
557 /* this is called with the lock */
558 static inline void vga_update_device_decodes(struct vga_device *vgadev,
559 int new_decodes)
561 int old_decodes;
562 struct vga_device *new_vgadev, *conflict;
564 old_decodes = vgadev->decodes;
565 vgadev->decodes = new_decodes;
567 pr_info("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
568 pci_name(vgadev->pdev),
569 vga_iostate_to_str(old_decodes),
570 vga_iostate_to_str(vgadev->decodes),
571 vga_iostate_to_str(vgadev->owns));
574 /* if we own the decodes we should move them along to
575 another card */
576 if ((vgadev->owns & old_decodes) && (vga_count > 1)) {
577 /* set us to own nothing */
578 vgadev->owns &= ~old_decodes;
579 list_for_each_entry(new_vgadev, &vga_list, list) {
580 if ((new_vgadev != vgadev) &&
581 (new_vgadev->decodes & VGA_RSRC_LEGACY_MASK)) {
582 pr_info("vgaarb: transferring owner from PCI:%s to PCI:%s\n", pci_name(vgadev->pdev), pci_name(new_vgadev->pdev));
583 conflict = __vga_tryget(new_vgadev, VGA_RSRC_LEGACY_MASK);
584 if (!conflict)
585 __vga_put(new_vgadev, VGA_RSRC_LEGACY_MASK);
586 break;
591 /* change decodes counter */
592 if (old_decodes != new_decodes) {
593 if (new_decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
594 vga_decode_count++;
595 else
596 vga_decode_count--;
598 pr_debug("vgaarb: decoding count now is: %d\n", vga_decode_count);
601 static void __vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes, bool userspace)
603 struct vga_device *vgadev;
604 unsigned long flags;
606 decodes &= VGA_RSRC_LEGACY_MASK;
608 spin_lock_irqsave(&vga_lock, flags);
609 vgadev = vgadev_find(pdev);
610 if (vgadev == NULL)
611 goto bail;
613 /* don't let userspace futz with kernel driver decodes */
614 if (userspace && vgadev->set_vga_decode)
615 goto bail;
617 /* update the device decodes + counter */
618 vga_update_device_decodes(vgadev, decodes);
620 /* XXX if somebody is going from "doesn't decode" to "decodes" state
621 * here, additional care must be taken as we may have pending owner
622 * ship of non-legacy region ...
624 bail:
625 spin_unlock_irqrestore(&vga_lock, flags);
628 void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
630 __vga_set_legacy_decoding(pdev, decodes, false);
632 EXPORT_SYMBOL(vga_set_legacy_decoding);
634 /* call with NULL to unregister */
635 int vga_client_register(struct pci_dev *pdev, void *cookie,
636 void (*irq_set_state)(void *cookie, bool state),
637 unsigned int (*set_vga_decode)(void *cookie, bool decode))
639 int ret = -ENODEV;
640 struct vga_device *vgadev;
641 unsigned long flags;
643 spin_lock_irqsave(&vga_lock, flags);
644 vgadev = vgadev_find(pdev);
645 if (!vgadev)
646 goto bail;
648 vgadev->irq_set_state = irq_set_state;
649 vgadev->set_vga_decode = set_vga_decode;
650 vgadev->cookie = cookie;
651 ret = 0;
653 bail:
654 spin_unlock_irqrestore(&vga_lock, flags);
655 return ret;
658 EXPORT_SYMBOL(vga_client_register);
661 * Char driver implementation
663 * Semantics is:
665 * open : open user instance of the arbitrer. by default, it's
666 * attached to the default VGA device of the system.
668 * close : close user instance, release locks
670 * read : return a string indicating the status of the target.
671 * an IO state string is of the form {io,mem,io+mem,none},
672 * mc and ic are respectively mem and io lock counts (for
673 * debugging/diagnostic only). "decodes" indicate what the
674 * card currently decodes, "owns" indicates what is currently
675 * enabled on it, and "locks" indicates what is locked by this
676 * card. If the card is unplugged, we get "invalid" then for
677 * card_ID and an -ENODEV error is returned for any command
678 * until a new card is targeted
680 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
682 * write : write a command to the arbiter. List of commands is:
684 * target <card_ID> : switch target to card <card_ID> (see below)
685 * lock <io_state> : acquires locks on target ("none" is invalid io_state)
686 * trylock <io_state> : non-blocking acquire locks on target
687 * unlock <io_state> : release locks on target
688 * unlock all : release all locks on target held by this user
689 * decodes <io_state> : set the legacy decoding attributes for the card
691 * poll : event if something change on any card (not just the target)
693 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
694 * to go back to the system default card (TODO: not implemented yet).
695 * Currently, only PCI is supported as a prefix, but the userland API may
696 * support other bus types in the future, even if the current kernel
697 * implementation doesn't.
699 * Note about locks:
701 * The driver keeps track of which user has what locks on which card. It
702 * supports stacking, like the kernel one. This complexifies the implementation
703 * a bit, but makes the arbiter more tolerant to userspace problems and able
704 * to properly cleanup in all cases when a process dies.
705 * Currently, a max of 16 cards simultaneously can have locks issued from
706 * userspace for a given user (file descriptor instance) of the arbiter.
708 * If the device is hot-unplugged, there is a hook inside the module to notify
709 * they being added/removed in the system and automatically added/removed in
710 * the arbiter.
713 #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
714 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
717 * Each user has an array of these, tracking which cards have locks
719 struct vga_arb_user_card {
720 struct pci_dev *pdev;
721 unsigned int mem_cnt;
722 unsigned int io_cnt;
725 struct vga_arb_private {
726 struct list_head list;
727 struct pci_dev *target;
728 struct vga_arb_user_card cards[MAX_USER_CARDS];
729 spinlock_t lock;
732 static LIST_HEAD(vga_user_list);
733 static DEFINE_SPINLOCK(vga_user_lock);
737 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
738 * returns the respective values. If the string is not in this format,
739 * it returns 0.
741 static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
742 unsigned int *bus, unsigned int *devfn)
744 int n;
745 unsigned int slot, func;
748 n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
749 if (n != 4)
750 return 0;
752 *devfn = PCI_DEVFN(slot, func);
754 return 1;
757 static ssize_t vga_arb_read(struct file *file, char __user * buf,
758 size_t count, loff_t *ppos)
760 struct vga_arb_private *priv = file->private_data;
761 struct vga_device *vgadev;
762 struct pci_dev *pdev;
763 unsigned long flags;
764 size_t len;
765 int rc;
766 char *lbuf;
768 lbuf = kmalloc(1024, GFP_KERNEL);
769 if (lbuf == NULL)
770 return -ENOMEM;
772 /* Shields against vga_arb_device_card_gone (pci_dev going
773 * away), and allows access to vga list
775 spin_lock_irqsave(&vga_lock, flags);
777 /* If we are targetting the default, use it */
778 pdev = priv->target;
779 if (pdev == NULL || pdev == PCI_INVALID_CARD) {
780 spin_unlock_irqrestore(&vga_lock, flags);
781 len = sprintf(lbuf, "invalid");
782 goto done;
785 /* Find card vgadev structure */
786 vgadev = vgadev_find(pdev);
787 if (vgadev == NULL) {
788 /* Wow, it's not in the list, that shouldn't happen,
789 * let's fix us up and return invalid card
791 if (pdev == priv->target)
792 vga_arb_device_card_gone(pdev);
793 spin_unlock_irqrestore(&vga_lock, flags);
794 len = sprintf(lbuf, "invalid");
795 goto done;
798 /* Fill the buffer with infos */
799 len = snprintf(lbuf, 1024,
800 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
801 vga_decode_count, pci_name(pdev),
802 vga_iostate_to_str(vgadev->decodes),
803 vga_iostate_to_str(vgadev->owns),
804 vga_iostate_to_str(vgadev->locks),
805 vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
807 spin_unlock_irqrestore(&vga_lock, flags);
808 done:
810 /* Copy that to user */
811 if (len > count)
812 len = count;
813 rc = copy_to_user(buf, lbuf, len);
814 kfree(lbuf);
815 if (rc)
816 return -EFAULT;
817 return len;
821 * TODO: To avoid parsing inside kernel and to improve the speed we may
822 * consider use ioctl here
824 static ssize_t vga_arb_write(struct file *file, const char __user * buf,
825 size_t count, loff_t *ppos)
827 struct vga_arb_private *priv = file->private_data;
828 struct vga_arb_user_card *uc = NULL;
829 struct pci_dev *pdev;
831 unsigned int io_state;
833 char *kbuf, *curr_pos;
834 size_t remaining = count;
836 int ret_val;
837 int i;
840 kbuf = kmalloc(count + 1, GFP_KERNEL);
841 if (!kbuf)
842 return -ENOMEM;
844 if (copy_from_user(kbuf, buf, count)) {
845 kfree(kbuf);
846 return -EFAULT;
848 curr_pos = kbuf;
849 kbuf[count] = '\0'; /* Just to make sure... */
851 if (strncmp(curr_pos, "lock ", 5) == 0) {
852 curr_pos += 5;
853 remaining -= 5;
855 pr_debug("client 0x%p called 'lock'\n", priv);
857 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
858 ret_val = -EPROTO;
859 goto done;
861 if (io_state == VGA_RSRC_NONE) {
862 ret_val = -EPROTO;
863 goto done;
866 pdev = priv->target;
867 if (priv->target == NULL) {
868 ret_val = -ENODEV;
869 goto done;
872 vga_get_uninterruptible(pdev, io_state);
874 /* Update the client's locks lists... */
875 for (i = 0; i < MAX_USER_CARDS; i++) {
876 if (priv->cards[i].pdev == pdev) {
877 if (io_state & VGA_RSRC_LEGACY_IO)
878 priv->cards[i].io_cnt++;
879 if (io_state & VGA_RSRC_LEGACY_MEM)
880 priv->cards[i].mem_cnt++;
881 break;
885 ret_val = count;
886 goto done;
887 } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
888 curr_pos += 7;
889 remaining -= 7;
891 pr_debug("client 0x%p called 'unlock'\n", priv);
893 if (strncmp(curr_pos, "all", 3) == 0)
894 io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
895 else {
896 if (!vga_str_to_iostate
897 (curr_pos, remaining, &io_state)) {
898 ret_val = -EPROTO;
899 goto done;
901 /* TODO: Add this?
902 if (io_state == VGA_RSRC_NONE) {
903 ret_val = -EPROTO;
904 goto done;
909 pdev = priv->target;
910 if (priv->target == NULL) {
911 ret_val = -ENODEV;
912 goto done;
914 for (i = 0; i < MAX_USER_CARDS; i++) {
915 if (priv->cards[i].pdev == pdev)
916 uc = &priv->cards[i];
919 if (!uc)
920 return -EINVAL;
922 if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0)
923 return -EINVAL;
925 if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0)
926 return -EINVAL;
928 vga_put(pdev, io_state);
930 if (io_state & VGA_RSRC_LEGACY_IO)
931 uc->io_cnt--;
932 if (io_state & VGA_RSRC_LEGACY_MEM)
933 uc->mem_cnt--;
935 ret_val = count;
936 goto done;
937 } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
938 curr_pos += 8;
939 remaining -= 8;
941 pr_debug("client 0x%p called 'trylock'\n", priv);
943 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
944 ret_val = -EPROTO;
945 goto done;
947 /* TODO: Add this?
948 if (io_state == VGA_RSRC_NONE) {
949 ret_val = -EPROTO;
950 goto done;
954 pdev = priv->target;
955 if (priv->target == NULL) {
956 ret_val = -ENODEV;
957 goto done;
960 if (vga_tryget(pdev, io_state)) {
961 /* Update the client's locks lists... */
962 for (i = 0; i < MAX_USER_CARDS; i++) {
963 if (priv->cards[i].pdev == pdev) {
964 if (io_state & VGA_RSRC_LEGACY_IO)
965 priv->cards[i].io_cnt++;
966 if (io_state & VGA_RSRC_LEGACY_MEM)
967 priv->cards[i].mem_cnt++;
968 break;
971 ret_val = count;
972 goto done;
973 } else {
974 ret_val = -EBUSY;
975 goto done;
978 } else if (strncmp(curr_pos, "target ", 7) == 0) {
979 struct pci_bus *pbus;
980 unsigned int domain, bus, devfn;
981 struct vga_device *vgadev;
983 curr_pos += 7;
984 remaining -= 7;
985 pr_debug("client 0x%p called 'target'\n", priv);
986 /* if target is default */
987 if (!strncmp(curr_pos, "default", 7))
988 pdev = pci_dev_get(vga_default_device());
989 else {
990 if (!vga_pci_str_to_vars(curr_pos, remaining,
991 &domain, &bus, &devfn)) {
992 ret_val = -EPROTO;
993 goto done;
995 pr_debug("vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos,
996 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
998 pbus = pci_find_bus(domain, bus);
999 pr_debug("vgaarb: pbus %p\n", pbus);
1000 if (pbus == NULL) {
1001 pr_err("vgaarb: invalid PCI domain and/or bus address %x:%x\n",
1002 domain, bus);
1003 ret_val = -ENODEV;
1004 goto done;
1006 pdev = pci_get_slot(pbus, devfn);
1007 pr_debug("vgaarb: pdev %p\n", pdev);
1008 if (!pdev) {
1009 pr_err("vgaarb: invalid PCI address %x:%x\n",
1010 bus, devfn);
1011 ret_val = -ENODEV;
1012 goto done;
1016 vgadev = vgadev_find(pdev);
1017 pr_debug("vgaarb: vgadev %p\n", vgadev);
1018 if (vgadev == NULL) {
1019 pr_err("vgaarb: this pci device is not a vga device\n");
1020 pci_dev_put(pdev);
1021 ret_val = -ENODEV;
1022 goto done;
1025 priv->target = pdev;
1026 for (i = 0; i < MAX_USER_CARDS; i++) {
1027 if (priv->cards[i].pdev == pdev)
1028 break;
1029 if (priv->cards[i].pdev == NULL) {
1030 priv->cards[i].pdev = pdev;
1031 priv->cards[i].io_cnt = 0;
1032 priv->cards[i].mem_cnt = 0;
1033 break;
1036 if (i == MAX_USER_CARDS) {
1037 pr_err("vgaarb: maximum user cards (%d) number reached!\n",
1038 MAX_USER_CARDS);
1039 pci_dev_put(pdev);
1040 /* XXX: which value to return? */
1041 ret_val = -ENOMEM;
1042 goto done;
1045 ret_val = count;
1046 pci_dev_put(pdev);
1047 goto done;
1050 } else if (strncmp(curr_pos, "decodes ", 8) == 0) {
1051 curr_pos += 8;
1052 remaining -= 8;
1053 pr_debug("vgaarb: client 0x%p called 'decodes'\n", priv);
1055 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1056 ret_val = -EPROTO;
1057 goto done;
1059 pdev = priv->target;
1060 if (priv->target == NULL) {
1061 ret_val = -ENODEV;
1062 goto done;
1065 __vga_set_legacy_decoding(pdev, io_state, true);
1066 ret_val = count;
1067 goto done;
1069 /* If we got here, the message written is not part of the protocol! */
1070 kfree(kbuf);
1071 return -EPROTO;
1073 done:
1074 kfree(kbuf);
1075 return ret_val;
1078 static unsigned int vga_arb_fpoll(struct file *file, poll_table * wait)
1080 struct vga_arb_private *priv = file->private_data;
1082 pr_debug("%s\n", __func__);
1084 if (priv == NULL)
1085 return -ENODEV;
1086 poll_wait(file, &vga_wait_queue, wait);
1087 return POLLIN;
1090 static int vga_arb_open(struct inode *inode, struct file *file)
1092 struct vga_arb_private *priv;
1093 unsigned long flags;
1095 pr_debug("%s\n", __func__);
1097 priv = kmalloc(sizeof(struct vga_arb_private), GFP_KERNEL);
1098 if (priv == NULL)
1099 return -ENOMEM;
1100 memset(priv, 0, sizeof(*priv));
1101 spin_lock_init(&priv->lock);
1102 file->private_data = priv;
1104 spin_lock_irqsave(&vga_user_lock, flags);
1105 list_add(&priv->list, &vga_user_list);
1106 spin_unlock_irqrestore(&vga_user_lock, flags);
1108 /* Set the client' lists of locks */
1109 priv->target = vga_default_device(); /* Maybe this is still null! */
1110 priv->cards[0].pdev = priv->target;
1111 priv->cards[0].io_cnt = 0;
1112 priv->cards[0].mem_cnt = 0;
1115 return 0;
1118 static int vga_arb_release(struct inode *inode, struct file *file)
1120 struct vga_arb_private *priv = file->private_data;
1121 struct vga_arb_user_card *uc;
1122 unsigned long flags;
1123 int i;
1125 pr_debug("%s\n", __func__);
1127 if (priv == NULL)
1128 return -ENODEV;
1130 spin_lock_irqsave(&vga_user_lock, flags);
1131 list_del(&priv->list);
1132 for (i = 0; i < MAX_USER_CARDS; i++) {
1133 uc = &priv->cards[i];
1134 if (uc->pdev == NULL)
1135 continue;
1136 pr_debug("uc->io_cnt == %d, uc->mem_cnt == %d\n",
1137 uc->io_cnt, uc->mem_cnt);
1138 while (uc->io_cnt--)
1139 vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
1140 while (uc->mem_cnt--)
1141 vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
1143 spin_unlock_irqrestore(&vga_user_lock, flags);
1145 kfree(priv);
1147 return 0;
1150 static void vga_arb_device_card_gone(struct pci_dev *pdev)
1155 * callback any registered clients to let them know we have a
1156 * change in VGA cards
1158 static void vga_arbiter_notify_clients(void)
1160 struct vga_device *vgadev;
1161 unsigned long flags;
1162 uint32_t new_decodes;
1163 bool new_state;
1165 if (!vga_arbiter_used)
1166 return;
1168 spin_lock_irqsave(&vga_lock, flags);
1169 list_for_each_entry(vgadev, &vga_list, list) {
1170 if (vga_count > 1)
1171 new_state = false;
1172 else
1173 new_state = true;
1174 if (vgadev->set_vga_decode) {
1175 new_decodes = vgadev->set_vga_decode(vgadev->cookie, new_state);
1176 vga_update_device_decodes(vgadev, new_decodes);
1179 spin_unlock_irqrestore(&vga_lock, flags);
1182 static int pci_notify(struct notifier_block *nb, unsigned long action,
1183 void *data)
1185 struct device *dev = data;
1186 struct pci_dev *pdev = to_pci_dev(dev);
1187 bool notify = false;
1189 pr_debug("%s\n", __func__);
1191 /* For now we're only intereted in devices added and removed. I didn't
1192 * test this thing here, so someone needs to double check for the
1193 * cases of hotplugable vga cards. */
1194 if (action == BUS_NOTIFY_ADD_DEVICE)
1195 notify = vga_arbiter_add_pci_device(pdev);
1196 else if (action == BUS_NOTIFY_DEL_DEVICE)
1197 notify = vga_arbiter_del_pci_device(pdev);
1199 if (notify)
1200 vga_arbiter_notify_clients();
1201 return 0;
1204 static struct notifier_block pci_notifier = {
1205 .notifier_call = pci_notify,
1208 static const struct file_operations vga_arb_device_fops = {
1209 .read = vga_arb_read,
1210 .write = vga_arb_write,
1211 .poll = vga_arb_fpoll,
1212 .open = vga_arb_open,
1213 .release = vga_arb_release,
1214 .llseek = noop_llseek,
1217 static struct miscdevice vga_arb_device = {
1218 MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
1221 static int __init vga_arb_device_init(void)
1223 int rc;
1224 struct pci_dev *pdev;
1226 rc = misc_register(&vga_arb_device);
1227 if (rc < 0)
1228 pr_err("vgaarb: error %d registering device\n", rc);
1230 bus_register_notifier(&pci_bus_type, &pci_notifier);
1232 /* We add all pci devices satisfying vga class in the arbiter by
1233 * default */
1234 pdev = NULL;
1235 while ((pdev =
1236 pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
1237 PCI_ANY_ID, pdev)) != NULL)
1238 vga_arbiter_add_pci_device(pdev);
1240 pr_info("vgaarb: loaded\n");
1241 return rc;
1243 subsys_initcall(vga_arb_device_init);