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