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