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