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