blob: 24ac52e6cd4118f32e46036832949807ef9870e7 [file] [log] [blame]
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001/*
Tiago Vignattic0db9cb2010-05-24 18:24:31 +03002 * vgaarb.c: Implements the VGA arbitration. For details refer to
3 * Documentation/vgaarbiter.txt
4 *
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10005 *
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 *
Tiago Vignattic0db9cb2010-05-24 18:24:31 +030010 * 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 *
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +100030 */
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Bruno Prémont86fd8872014-08-24 23:09:53 +020044#include <linux/screen_info.h>
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +100045
46#include <linux/uaccess.h>
47
48#include <linux/vgaarb.h>
49
50static void vga_arbiter_notify_clients(void);
51/*
52 * We keep a list of all vga devices in the system to speed
53 * up the various operations of the arbiter
54 */
55struct vga_device {
56 struct list_head list;
57 struct pci_dev *pdev;
58 unsigned int decodes; /* what does it decodes */
59 unsigned int owns; /* what does it owns */
60 unsigned int locks; /* what does it locks */
61 unsigned int io_lock_cnt; /* legacy IO lock count */
62 unsigned int mem_lock_cnt; /* legacy MEM lock count */
63 unsigned int io_norm_cnt; /* normal IO count */
64 unsigned int mem_norm_cnt; /* normal MEM count */
Dave Airlie3448a192010-06-01 15:32:24 +100065 bool bridge_has_one_vga;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +100066 /* allow IRQ enable/disable hook */
67 void *cookie;
68 void (*irq_set_state)(void *cookie, bool enable);
69 unsigned int (*set_vga_decode)(void *cookie, bool decode);
70};
71
72static LIST_HEAD(vga_list);
73static int vga_count, vga_decode_count;
74static bool vga_arbiter_used;
75static DEFINE_SPINLOCK(vga_lock);
76static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
77
78
79static const char *vga_iostate_to_str(unsigned int iostate)
80{
81 /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
82 iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
83 switch (iostate) {
84 case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
85 return "io+mem";
86 case VGA_RSRC_LEGACY_IO:
87 return "io";
88 case VGA_RSRC_LEGACY_MEM:
89 return "mem";
90 }
91 return "none";
92}
93
94static int vga_str_to_iostate(char *buf, int str_size, int *io_state)
95{
96 /* we could in theory hand out locks on IO and mem
97 * separately to userspace but it can cause deadlocks */
98 if (strncmp(buf, "none", 4) == 0) {
99 *io_state = VGA_RSRC_NONE;
100 return 1;
101 }
102
103 /* XXX We're not chekcing the str_size! */
104 if (strncmp(buf, "io+mem", 6) == 0)
105 goto both;
106 else if (strncmp(buf, "io", 2) == 0)
107 goto both;
108 else if (strncmp(buf, "mem", 3) == 0)
109 goto both;
110 return 0;
111both:
112 *io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
113 return 1;
114}
115
116#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
117/* this is only used a cookie - it should not be dereferenced */
118static struct pci_dev *vga_default;
119#endif
120
121static void vga_arb_device_card_gone(struct pci_dev *pdev);
122
123/* Find somebody in our list */
124static struct vga_device *vgadev_find(struct pci_dev *pdev)
125{
126 struct vga_device *vgadev;
127
128 list_for_each_entry(vgadev, &vga_list, list)
129 if (pdev == vgadev->pdev)
130 return vgadev;
131 return NULL;
132}
133
134/* Returns the default VGA device (vgacon's babe) */
135#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
136struct pci_dev *vga_default_device(void)
137{
138 return vga_default;
139}
Matthew Garrett1a39b312012-04-16 16:26:02 -0400140
Matthew Garrett1b231702012-04-24 09:31:28 +0100141EXPORT_SYMBOL_GPL(vga_default_device);
142
Matthew Garrett1a39b312012-04-16 16:26:02 -0400143void vga_set_default_device(struct pci_dev *pdev)
144{
Yinghai Lu84544a12012-09-19 11:54:15 -0700145 if (vga_default == pdev)
146 return;
147
148 pci_dev_put(vga_default);
149 vga_default = pci_dev_get(pdev);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400150}
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000151#endif
152
153static inline void vga_irq_set_state(struct vga_device *vgadev, bool state)
154{
155 if (vgadev->irq_set_state)
156 vgadev->irq_set_state(vgadev->cookie, state);
157}
158
159
160/* If we don't ever use VGA arb we should avoid
161 turning off anything anywhere due to old X servers getting
162 confused about the boot device not being VGA */
163static void vga_check_first_use(void)
164{
165 /* we should inform all GPUs in the system that
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300166 * VGA arb has occurred and to try and disable resources
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000167 * if they can */
168 if (!vga_arbiter_used) {
169 vga_arbiter_used = true;
170 vga_arbiter_notify_clients();
171 }
172}
173
174static struct vga_device *__vga_tryget(struct vga_device *vgadev,
175 unsigned int rsrc)
176{
177 unsigned int wants, legacy_wants, match;
178 struct vga_device *conflict;
179 unsigned int pci_bits;
Dave Airlie3448a192010-06-01 15:32:24 +1000180 u32 flags = 0;
181
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000182 /* Account for "normal" resources to lock. If we decode the legacy,
183 * counterpart, we need to request it as well
184 */
185 if ((rsrc & VGA_RSRC_NORMAL_IO) &&
186 (vgadev->decodes & VGA_RSRC_LEGACY_IO))
187 rsrc |= VGA_RSRC_LEGACY_IO;
188 if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
189 (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
190 rsrc |= VGA_RSRC_LEGACY_MEM;
191
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +0300192 pr_debug("%s: %d\n", __func__, rsrc);
193 pr_debug("%s: owns: %d\n", __func__, vgadev->owns);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000194
195 /* Check what resources we need to acquire */
196 wants = rsrc & ~vgadev->owns;
197
198 /* We already own everything, just mark locked & bye bye */
199 if (wants == 0)
200 goto lock_them;
201
202 /* We don't need to request a legacy resource, we just enable
203 * appropriate decoding and go
204 */
205 legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
206 if (legacy_wants == 0)
207 goto enable_them;
208
209 /* Ok, we don't, let's find out how we need to kick off */
210 list_for_each_entry(conflict, &vga_list, list) {
211 unsigned int lwants = legacy_wants;
212 unsigned int change_bridge = 0;
213
214 /* Don't conflict with myself */
215 if (vgadev == conflict)
216 continue;
217
218 /* Check if the architecture allows a conflict between those
219 * 2 devices or if they are on separate domains
220 */
221 if (!vga_conflicts(vgadev->pdev, conflict->pdev))
222 continue;
223
224 /* We have a possible conflict. before we go further, we must
225 * check if we sit on the same bus as the conflicting device.
226 * if we don't, then we must tie both IO and MEM resources
227 * together since there is only a single bit controlling
228 * VGA forwarding on P2P bridges
229 */
230 if (vgadev->pdev->bus != conflict->pdev->bus) {
231 change_bridge = 1;
232 lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
233 }
234
235 /* Check if the guy has a lock on the resource. If he does,
236 * return the conflicting entry
237 */
238 if (conflict->locks & lwants)
239 return conflict;
240
Alex Williamson4e4e7dc2014-07-03 09:59:51 -0600241 /* Ok, now check if it owns the resource we want. We can
242 * lock resources that are not decoded, therefore a device
243 * can own resources it doesn't decode.
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000244 */
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000245 match = lwants & conflict->owns;
246 if (!match)
247 continue;
248
249 /* looks like he doesn't have a lock, we can steal
250 * them from him
251 */
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000252
Dave Airlie3448a192010-06-01 15:32:24 +1000253 flags = 0;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000254 pci_bits = 0;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000255
Alex Williamson4e4e7dc2014-07-03 09:59:51 -0600256 /* If we can't control legacy resources via the bridge, we
257 * also need to disable normal decoding.
258 */
Dave Airlie3448a192010-06-01 15:32:24 +1000259 if (!conflict->bridge_has_one_vga) {
Alex Williamson4e4e7dc2014-07-03 09:59:51 -0600260 if ((match & conflict->decodes) & VGA_RSRC_LEGACY_MEM)
Dave Airlie3448a192010-06-01 15:32:24 +1000261 pci_bits |= PCI_COMMAND_MEMORY;
Alex Williamson4e4e7dc2014-07-03 09:59:51 -0600262 if ((match & conflict->decodes) & VGA_RSRC_LEGACY_IO)
Dave Airlie3448a192010-06-01 15:32:24 +1000263 pci_bits |= PCI_COMMAND_IO;
Alex Williamson4e4e7dc2014-07-03 09:59:51 -0600264
265 if (pci_bits) {
266 vga_irq_set_state(conflict, false);
267 flags |= PCI_VGA_STATE_CHANGE_DECODES;
268 }
Dave Airlie3448a192010-06-01 15:32:24 +1000269 }
270
271 if (change_bridge)
272 flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
273
274 pci_set_vga_state(conflict->pdev, false, pci_bits, flags);
Alex Williamsonf22d7762013-08-15 16:37:53 -0600275 conflict->owns &= ~match;
Alex Williamson4e4e7dc2014-07-03 09:59:51 -0600276
277 /* If we disabled normal decoding, reflect it in owns */
278 if (pci_bits & PCI_COMMAND_MEMORY)
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000279 conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
Alex Williamson4e4e7dc2014-07-03 09:59:51 -0600280 if (pci_bits & PCI_COMMAND_IO)
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000281 conflict->owns &= ~VGA_RSRC_NORMAL_IO;
282 }
283
284enable_them:
285 /* ok dude, we got it, everybody conflicting has been disabled, let's
Alex Williamson4e4e7dc2014-07-03 09:59:51 -0600286 * enable us. Mark any bits in "owns" regardless of whether we
287 * decoded them. We can lock resources we don't decode, therefore
288 * we must track them via "owns".
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000289 */
Dave Airlie3448a192010-06-01 15:32:24 +1000290 flags = 0;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000291 pci_bits = 0;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000292
Dave Airlie3448a192010-06-01 15:32:24 +1000293 if (!vgadev->bridge_has_one_vga) {
294 flags |= PCI_VGA_STATE_CHANGE_DECODES;
295 if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
296 pci_bits |= PCI_COMMAND_MEMORY;
297 if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
298 pci_bits |= PCI_COMMAND_IO;
299 }
Alex Williamson4e4e7dc2014-07-03 09:59:51 -0600300 if (wants & VGA_RSRC_LEGACY_MASK)
Dave Airlie3448a192010-06-01 15:32:24 +1000301 flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
302
303 pci_set_vga_state(vgadev->pdev, true, pci_bits, flags);
304
305 if (!vgadev->bridge_has_one_vga) {
306 vga_irq_set_state(vgadev, true);
307 }
Alex Williamson4e4e7dc2014-07-03 09:59:51 -0600308 vgadev->owns |= wants;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000309lock_them:
310 vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
311 if (rsrc & VGA_RSRC_LEGACY_IO)
312 vgadev->io_lock_cnt++;
313 if (rsrc & VGA_RSRC_LEGACY_MEM)
314 vgadev->mem_lock_cnt++;
315 if (rsrc & VGA_RSRC_NORMAL_IO)
316 vgadev->io_norm_cnt++;
317 if (rsrc & VGA_RSRC_NORMAL_MEM)
318 vgadev->mem_norm_cnt++;
319
320 return NULL;
321}
322
323static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
324{
325 unsigned int old_locks = vgadev->locks;
326
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +0300327 pr_debug("%s\n", __func__);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000328
329 /* Update our counters, and account for equivalent legacy resources
330 * if we decode them
331 */
332 if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
333 vgadev->io_norm_cnt--;
334 if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
335 rsrc |= VGA_RSRC_LEGACY_IO;
336 }
337 if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
338 vgadev->mem_norm_cnt--;
339 if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
340 rsrc |= VGA_RSRC_LEGACY_MEM;
341 }
342 if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
343 vgadev->io_lock_cnt--;
344 if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
345 vgadev->mem_lock_cnt--;
346
347 /* Just clear lock bits, we do lazy operations so we don't really
348 * have to bother about anything else at this point
349 */
350 if (vgadev->io_lock_cnt == 0)
351 vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
352 if (vgadev->mem_lock_cnt == 0)
353 vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
354
355 /* Kick the wait queue in case somebody was waiting if we actually
356 * released something
357 */
358 if (old_locks != vgadev->locks)
359 wake_up_all(&vga_wait_queue);
360}
361
362int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
363{
364 struct vga_device *vgadev, *conflict;
365 unsigned long flags;
366 wait_queue_t wait;
367 int rc = 0;
368
369 vga_check_first_use();
370 /* The one who calls us should check for this, but lets be sure... */
371 if (pdev == NULL)
372 pdev = vga_default_device();
373 if (pdev == NULL)
374 return 0;
375
376 for (;;) {
377 spin_lock_irqsave(&vga_lock, flags);
378 vgadev = vgadev_find(pdev);
379 if (vgadev == NULL) {
380 spin_unlock_irqrestore(&vga_lock, flags);
381 rc = -ENODEV;
382 break;
383 }
384 conflict = __vga_tryget(vgadev, rsrc);
385 spin_unlock_irqrestore(&vga_lock, flags);
386 if (conflict == NULL)
387 break;
388
389
390 /* We have a conflict, we wait until somebody kicks the
391 * work queue. Currently we have one work queue that we
392 * kick each time some resources are released, but it would
393 * be fairly easy to have a per device one so that we only
394 * need to attach to the conflicting device
395 */
396 init_waitqueue_entry(&wait, current);
397 add_wait_queue(&vga_wait_queue, &wait);
398 set_current_state(interruptible ?
399 TASK_INTERRUPTIBLE :
400 TASK_UNINTERRUPTIBLE);
401 if (signal_pending(current)) {
402 rc = -EINTR;
403 break;
404 }
405 schedule();
406 remove_wait_queue(&vga_wait_queue, &wait);
407 set_current_state(TASK_RUNNING);
408 }
409 return rc;
410}
411EXPORT_SYMBOL(vga_get);
412
413int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
414{
415 struct vga_device *vgadev;
416 unsigned long flags;
417 int rc = 0;
418
419 vga_check_first_use();
420
421 /* The one who calls us should check for this, but lets be sure... */
422 if (pdev == NULL)
423 pdev = vga_default_device();
424 if (pdev == NULL)
425 return 0;
426 spin_lock_irqsave(&vga_lock, flags);
427 vgadev = vgadev_find(pdev);
428 if (vgadev == NULL) {
429 rc = -ENODEV;
430 goto bail;
431 }
432 if (__vga_tryget(vgadev, rsrc))
433 rc = -EBUSY;
434bail:
435 spin_unlock_irqrestore(&vga_lock, flags);
436 return rc;
437}
438EXPORT_SYMBOL(vga_tryget);
439
440void vga_put(struct pci_dev *pdev, unsigned int rsrc)
441{
442 struct vga_device *vgadev;
443 unsigned long flags;
444
445 /* The one who calls us should check for this, but lets be sure... */
446 if (pdev == NULL)
447 pdev = vga_default_device();
448 if (pdev == NULL)
449 return;
450 spin_lock_irqsave(&vga_lock, flags);
451 vgadev = vgadev_find(pdev);
452 if (vgadev == NULL)
453 goto bail;
454 __vga_put(vgadev, rsrc);
455bail:
456 spin_unlock_irqrestore(&vga_lock, flags);
457}
458EXPORT_SYMBOL(vga_put);
459
Dave Airlie3448a192010-06-01 15:32:24 +1000460/* Rules for using a bridge to control a VGA descendant decoding:
461 if a bridge has only one VGA descendant then it can be used
462 to control the VGA routing for that device.
463 It should always use the bridge closest to the device to control it.
464 If a bridge has a direct VGA descendant, but also have a sub-bridge
465 VGA descendant then we cannot use that bridge to control the direct VGA descendant.
466 So for every device we register, we need to iterate all its parent bridges
467 so we can invalidate any devices using them properly.
468*/
469static void vga_arbiter_check_bridge_sharing(struct vga_device *vgadev)
470{
471 struct vga_device *same_bridge_vgadev;
472 struct pci_bus *new_bus, *bus;
473 struct pci_dev *new_bridge, *bridge;
474
475 vgadev->bridge_has_one_vga = true;
476
477 if (list_empty(&vga_list))
478 return;
479
480 /* okay iterate the new devices bridge hierarachy */
481 new_bus = vgadev->pdev->bus;
482 while (new_bus) {
483 new_bridge = new_bus->self;
484
Dave Airlief6252112011-10-10 09:29:18 +0100485 /* go through list of devices already registered */
486 list_for_each_entry(same_bridge_vgadev, &vga_list, list) {
487 bus = same_bridge_vgadev->pdev->bus;
488 bridge = bus->self;
489
490 /* see if the share a bridge with this device */
491 if (new_bridge == bridge) {
492 /* if their direct parent bridge is the same
493 as any bridge of this device then it can't be used
494 for that device */
495 same_bridge_vgadev->bridge_has_one_vga = false;
496 }
497
498 /* now iterate the previous devices bridge hierarchy */
499 /* if the new devices parent bridge is in the other devices
500 hierarchy then we can't use it to control this device */
501 while (bus) {
Dave Airlie3448a192010-06-01 15:32:24 +1000502 bridge = bus->self;
Dave Airlief6252112011-10-10 09:29:18 +0100503 if (bridge) {
504 if (bridge == vgadev->pdev->bus->self)
505 vgadev->bridge_has_one_vga = false;
Dave Airlie3448a192010-06-01 15:32:24 +1000506 }
Dave Airlief6252112011-10-10 09:29:18 +0100507 bus = bus->parent;
Dave Airlie3448a192010-06-01 15:32:24 +1000508 }
509 }
510 new_bus = new_bus->parent;
511 }
512}
513
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000514/*
515 * Currently, we assume that the "initial" setup of the system is
516 * not sane, that is we come up with conflicting devices and let
517 * the arbiter's client decides if devices decodes or not legacy
518 * things.
519 */
520static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
521{
522 struct vga_device *vgadev;
523 unsigned long flags;
524 struct pci_bus *bus;
525 struct pci_dev *bridge;
526 u16 cmd;
527
528 /* Only deal with VGA class devices */
529 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
530 return false;
531
532 /* Allocate structure */
533 vgadev = kmalloc(sizeof(struct vga_device), GFP_KERNEL);
534 if (vgadev == NULL) {
535 pr_err("vgaarb: failed to allocate pci device\n");
536 /* What to do on allocation failure ? For now, let's
537 * just do nothing, I'm not sure there is anything saner
538 * to be done
539 */
540 return false;
541 }
542
543 memset(vgadev, 0, sizeof(*vgadev));
544
545 /* Take lock & check for duplicates */
546 spin_lock_irqsave(&vga_lock, flags);
547 if (vgadev_find(pdev) != NULL) {
548 BUG_ON(1);
549 goto fail;
550 }
551 vgadev->pdev = pdev;
552
553 /* By default, assume we decode everything */
554 vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
555 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
556
557 /* by default mark it as decoding */
558 vga_decode_count++;
559 /* Mark that we "own" resources based on our enables, we will
560 * clear that below if the bridge isn't forwarding
561 */
562 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
563 if (cmd & PCI_COMMAND_IO)
564 vgadev->owns |= VGA_RSRC_LEGACY_IO;
565 if (cmd & PCI_COMMAND_MEMORY)
566 vgadev->owns |= VGA_RSRC_LEGACY_MEM;
567
568 /* Check if VGA cycles can get down to us */
569 bus = pdev->bus;
570 while (bus) {
571 bridge = bus->self;
572 if (bridge) {
573 u16 l;
574 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
575 &l);
576 if (!(l & PCI_BRIDGE_CTL_VGA)) {
577 vgadev->owns = 0;
578 break;
579 }
580 }
581 bus = bus->parent;
582 }
583
584 /* Deal with VGA default device. Use first enabled one
585 * by default if arch doesn't have it's own hook
586 */
587#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
588 if (vga_default == NULL &&
Bruno Prémont86fd8872014-08-24 23:09:53 +0200589 ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)) {
590 pr_info("vgaarb: setting as boot device: PCI:%s\n",
591 pci_name(pdev));
Yinghai Lu84544a12012-09-19 11:54:15 -0700592 vga_set_default_device(pdev);
Bruno Prémont86fd8872014-08-24 23:09:53 +0200593 }
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000594#endif
595
Dave Airlie3448a192010-06-01 15:32:24 +1000596 vga_arbiter_check_bridge_sharing(vgadev);
597
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000598 /* Add to the list */
599 list_add(&vgadev->list, &vga_list);
600 vga_count++;
601 pr_info("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
602 pci_name(pdev),
603 vga_iostate_to_str(vgadev->decodes),
604 vga_iostate_to_str(vgadev->owns),
605 vga_iostate_to_str(vgadev->locks));
606
607 spin_unlock_irqrestore(&vga_lock, flags);
608 return true;
609fail:
610 spin_unlock_irqrestore(&vga_lock, flags);
611 kfree(vgadev);
612 return false;
613}
614
615static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
616{
617 struct vga_device *vgadev;
618 unsigned long flags;
619 bool ret = true;
620
621 spin_lock_irqsave(&vga_lock, flags);
622 vgadev = vgadev_find(pdev);
623 if (vgadev == NULL) {
624 ret = false;
625 goto bail;
626 }
627
Matthew Garrett1a39b312012-04-16 16:26:02 -0400628#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
Yinghai Lu84544a12012-09-19 11:54:15 -0700629 if (vga_default == pdev)
630 vga_set_default_device(NULL);
Matthew Garrett1a39b312012-04-16 16:26:02 -0400631#endif
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000632
633 if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
634 vga_decode_count--;
635
636 /* Remove entry from list */
637 list_del(&vgadev->list);
638 vga_count--;
639 /* Notify userland driver that the device is gone so it discards
640 * it's copies of the pci_dev pointer
641 */
642 vga_arb_device_card_gone(pdev);
643
644 /* Wake up all possible waiters */
645 wake_up_all(&vga_wait_queue);
646bail:
647 spin_unlock_irqrestore(&vga_lock, flags);
648 kfree(vgadev);
649 return ret;
650}
651
652/* this is called with the lock */
653static inline void vga_update_device_decodes(struct vga_device *vgadev,
654 int new_decodes)
655{
Alex Williamson5c0f6ee2013-08-15 16:37:59 -0600656 int old_decodes, decodes_removed, decodes_unlocked;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000657
658 old_decodes = vgadev->decodes;
Alex Williamson5c0f6ee2013-08-15 16:37:59 -0600659 decodes_removed = ~new_decodes & old_decodes;
660 decodes_unlocked = vgadev->locks & decodes_removed;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000661 vgadev->decodes = new_decodes;
662
663 pr_info("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
664 pci_name(vgadev->pdev),
665 vga_iostate_to_str(old_decodes),
666 vga_iostate_to_str(vgadev->decodes),
667 vga_iostate_to_str(vgadev->owns));
668
Alex Williamson5c0f6ee2013-08-15 16:37:59 -0600669 /* if we removed locked decodes, lock count goes to zero, and release */
670 if (decodes_unlocked) {
671 if (decodes_unlocked & VGA_RSRC_LEGACY_IO)
672 vgadev->io_lock_cnt = 0;
673 if (decodes_unlocked & VGA_RSRC_LEGACY_MEM)
674 vgadev->mem_lock_cnt = 0;
675 __vga_put(vgadev, decodes_unlocked);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000676 }
677
678 /* change decodes counter */
Alex Williamson5c0f6ee2013-08-15 16:37:59 -0600679 if (old_decodes & VGA_RSRC_LEGACY_MASK &&
680 !(new_decodes & VGA_RSRC_LEGACY_MASK))
681 vga_decode_count--;
682 if (!(old_decodes & VGA_RSRC_LEGACY_MASK) &&
683 new_decodes & VGA_RSRC_LEGACY_MASK)
684 vga_decode_count++;
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +0300685 pr_debug("vgaarb: decoding count now is: %d\n", vga_decode_count);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000686}
687
Daniel J Blueman201ba4c2010-09-22 18:05:35 +0100688static void __vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes, bool userspace)
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000689{
690 struct vga_device *vgadev;
691 unsigned long flags;
692
693 decodes &= VGA_RSRC_LEGACY_MASK;
694
695 spin_lock_irqsave(&vga_lock, flags);
696 vgadev = vgadev_find(pdev);
697 if (vgadev == NULL)
698 goto bail;
699
700 /* don't let userspace futz with kernel driver decodes */
701 if (userspace && vgadev->set_vga_decode)
702 goto bail;
703
704 /* update the device decodes + counter */
705 vga_update_device_decodes(vgadev, decodes);
706
707 /* XXX if somebody is going from "doesn't decode" to "decodes" state
708 * here, additional care must be taken as we may have pending owner
709 * ship of non-legacy region ...
710 */
711bail:
712 spin_unlock_irqrestore(&vga_lock, flags);
713}
714
715void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
716{
717 __vga_set_legacy_decoding(pdev, decodes, false);
718}
719EXPORT_SYMBOL(vga_set_legacy_decoding);
720
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000721/* call with NULL to unregister */
722int vga_client_register(struct pci_dev *pdev, void *cookie,
723 void (*irq_set_state)(void *cookie, bool state),
724 unsigned int (*set_vga_decode)(void *cookie, bool decode))
725{
Chris Wilson934f9922011-01-20 13:09:12 +0000726 int ret = -ENODEV;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000727 struct vga_device *vgadev;
728 unsigned long flags;
729
730 spin_lock_irqsave(&vga_lock, flags);
731 vgadev = vgadev_find(pdev);
732 if (!vgadev)
733 goto bail;
734
735 vgadev->irq_set_state = irq_set_state;
736 vgadev->set_vga_decode = set_vga_decode;
737 vgadev->cookie = cookie;
738 ret = 0;
739
740bail:
741 spin_unlock_irqrestore(&vga_lock, flags);
742 return ret;
743
744}
745EXPORT_SYMBOL(vga_client_register);
746
747/*
748 * Char driver implementation
749 *
750 * Semantics is:
751 *
752 * open : open user instance of the arbitrer. by default, it's
753 * attached to the default VGA device of the system.
754 *
755 * close : close user instance, release locks
756 *
757 * read : return a string indicating the status of the target.
758 * an IO state string is of the form {io,mem,io+mem,none},
759 * mc and ic are respectively mem and io lock counts (for
760 * debugging/diagnostic only). "decodes" indicate what the
761 * card currently decodes, "owns" indicates what is currently
762 * enabled on it, and "locks" indicates what is locked by this
763 * card. If the card is unplugged, we get "invalid" then for
764 * card_ID and an -ENODEV error is returned for any command
765 * until a new card is targeted
766 *
767 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
768 *
769 * write : write a command to the arbiter. List of commands is:
770 *
771 * target <card_ID> : switch target to card <card_ID> (see below)
772 * lock <io_state> : acquires locks on target ("none" is invalid io_state)
773 * trylock <io_state> : non-blocking acquire locks on target
774 * unlock <io_state> : release locks on target
775 * unlock all : release all locks on target held by this user
776 * decodes <io_state> : set the legacy decoding attributes for the card
777 *
778 * poll : event if something change on any card (not just the target)
779 *
780 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
781 * to go back to the system default card (TODO: not implemented yet).
782 * Currently, only PCI is supported as a prefix, but the userland API may
783 * support other bus types in the future, even if the current kernel
784 * implementation doesn't.
785 *
786 * Note about locks:
787 *
788 * The driver keeps track of which user has what locks on which card. It
789 * supports stacking, like the kernel one. This complexifies the implementation
790 * a bit, but makes the arbiter more tolerant to userspace problems and able
791 * to properly cleanup in all cases when a process dies.
792 * Currently, a max of 16 cards simultaneously can have locks issued from
793 * userspace for a given user (file descriptor instance) of the arbiter.
794 *
795 * If the device is hot-unplugged, there is a hook inside the module to notify
796 * they being added/removed in the system and automatically added/removed in
797 * the arbiter.
798 */
799
Mike Travis36028f32010-02-02 17:45:01 -0800800#define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000801#define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
802
803/*
804 * Each user has an array of these, tracking which cards have locks
805 */
806struct vga_arb_user_card {
807 struct pci_dev *pdev;
808 unsigned int mem_cnt;
809 unsigned int io_cnt;
810};
811
812struct vga_arb_private {
813 struct list_head list;
814 struct pci_dev *target;
815 struct vga_arb_user_card cards[MAX_USER_CARDS];
816 spinlock_t lock;
817};
818
819static LIST_HEAD(vga_user_list);
820static DEFINE_SPINLOCK(vga_user_lock);
821
822
823/*
824 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
825 * returns the respective values. If the string is not in this format,
826 * it returns 0.
827 */
828static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
829 unsigned int *bus, unsigned int *devfn)
830{
831 int n;
832 unsigned int slot, func;
833
834
835 n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
836 if (n != 4)
837 return 0;
838
839 *devfn = PCI_DEVFN(slot, func);
840
841 return 1;
842}
843
844static ssize_t vga_arb_read(struct file *file, char __user * buf,
845 size_t count, loff_t *ppos)
846{
847 struct vga_arb_private *priv = file->private_data;
848 struct vga_device *vgadev;
849 struct pci_dev *pdev;
850 unsigned long flags;
851 size_t len;
852 int rc;
853 char *lbuf;
854
855 lbuf = kmalloc(1024, GFP_KERNEL);
856 if (lbuf == NULL)
857 return -ENOMEM;
858
859 /* Shields against vga_arb_device_card_gone (pci_dev going
860 * away), and allows access to vga list
861 */
862 spin_lock_irqsave(&vga_lock, flags);
863
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300864 /* If we are targeting the default, use it */
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000865 pdev = priv->target;
866 if (pdev == NULL || pdev == PCI_INVALID_CARD) {
867 spin_unlock_irqrestore(&vga_lock, flags);
868 len = sprintf(lbuf, "invalid");
869 goto done;
870 }
871
872 /* Find card vgadev structure */
873 vgadev = vgadev_find(pdev);
874 if (vgadev == NULL) {
875 /* Wow, it's not in the list, that shouldn't happen,
876 * let's fix us up and return invalid card
877 */
878 if (pdev == priv->target)
879 vga_arb_device_card_gone(pdev);
880 spin_unlock_irqrestore(&vga_lock, flags);
881 len = sprintf(lbuf, "invalid");
882 goto done;
883 }
884
885 /* Fill the buffer with infos */
886 len = snprintf(lbuf, 1024,
887 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
888 vga_decode_count, pci_name(pdev),
889 vga_iostate_to_str(vgadev->decodes),
890 vga_iostate_to_str(vgadev->owns),
891 vga_iostate_to_str(vgadev->locks),
892 vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
893
894 spin_unlock_irqrestore(&vga_lock, flags);
895done:
896
897 /* Copy that to user */
898 if (len > count)
899 len = count;
900 rc = copy_to_user(buf, lbuf, len);
901 kfree(lbuf);
902 if (rc)
903 return -EFAULT;
904 return len;
905}
906
907/*
908 * TODO: To avoid parsing inside kernel and to improve the speed we may
909 * consider use ioctl here
910 */
911static ssize_t vga_arb_write(struct file *file, const char __user * buf,
912 size_t count, loff_t *ppos)
913{
914 struct vga_arb_private *priv = file->private_data;
915 struct vga_arb_user_card *uc = NULL;
916 struct pci_dev *pdev;
917
918 unsigned int io_state;
919
920 char *kbuf, *curr_pos;
921 size_t remaining = count;
922
923 int ret_val;
924 int i;
925
926
927 kbuf = kmalloc(count + 1, GFP_KERNEL);
928 if (!kbuf)
929 return -ENOMEM;
930
931 if (copy_from_user(kbuf, buf, count)) {
932 kfree(kbuf);
933 return -EFAULT;
934 }
935 curr_pos = kbuf;
936 kbuf[count] = '\0'; /* Just to make sure... */
937
938 if (strncmp(curr_pos, "lock ", 5) == 0) {
939 curr_pos += 5;
940 remaining -= 5;
941
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +0300942 pr_debug("client 0x%p called 'lock'\n", priv);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000943
944 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
945 ret_val = -EPROTO;
946 goto done;
947 }
948 if (io_state == VGA_RSRC_NONE) {
949 ret_val = -EPROTO;
950 goto done;
951 }
952
953 pdev = priv->target;
954 if (priv->target == NULL) {
955 ret_val = -ENODEV;
956 goto done;
957 }
958
959 vga_get_uninterruptible(pdev, io_state);
960
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
972 ret_val = count;
973 goto done;
974 } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
975 curr_pos += 7;
976 remaining -= 7;
977
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +0300978 pr_debug("client 0x%p called 'unlock'\n", priv);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +1000979
980 if (strncmp(curr_pos, "all", 3) == 0)
981 io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
982 else {
983 if (!vga_str_to_iostate
984 (curr_pos, remaining, &io_state)) {
985 ret_val = -EPROTO;
986 goto done;
987 }
988 /* TODO: Add this?
989 if (io_state == VGA_RSRC_NONE) {
990 ret_val = -EPROTO;
991 goto done;
992 }
993 */
994 }
995
996 pdev = priv->target;
997 if (priv->target == NULL) {
998 ret_val = -ENODEV;
999 goto done;
1000 }
1001 for (i = 0; i < MAX_USER_CARDS; i++) {
1002 if (priv->cards[i].pdev == pdev)
1003 uc = &priv->cards[i];
1004 }
1005
Julia Lawallc9168742011-11-15 14:53:11 -08001006 if (!uc) {
1007 ret_val = -EINVAL;
1008 goto done;
1009 }
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001010
Julia Lawallc9168742011-11-15 14:53:11 -08001011 if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
1012 ret_val = -EINVAL;
1013 goto done;
1014 }
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001015
Julia Lawallc9168742011-11-15 14:53:11 -08001016 if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
1017 ret_val = -EINVAL;
1018 goto done;
1019 }
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001020
1021 vga_put(pdev, io_state);
1022
1023 if (io_state & VGA_RSRC_LEGACY_IO)
1024 uc->io_cnt--;
1025 if (io_state & VGA_RSRC_LEGACY_MEM)
1026 uc->mem_cnt--;
1027
1028 ret_val = count;
1029 goto done;
1030 } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
1031 curr_pos += 8;
1032 remaining -= 8;
1033
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +03001034 pr_debug("client 0x%p called 'trylock'\n", priv);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001035
1036 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1037 ret_val = -EPROTO;
1038 goto done;
1039 }
1040 /* TODO: Add this?
1041 if (io_state == VGA_RSRC_NONE) {
1042 ret_val = -EPROTO;
1043 goto done;
1044 }
1045 */
1046
1047 pdev = priv->target;
1048 if (priv->target == NULL) {
1049 ret_val = -ENODEV;
1050 goto done;
1051 }
1052
1053 if (vga_tryget(pdev, io_state)) {
1054 /* Update the client's locks lists... */
1055 for (i = 0; i < MAX_USER_CARDS; i++) {
1056 if (priv->cards[i].pdev == pdev) {
1057 if (io_state & VGA_RSRC_LEGACY_IO)
1058 priv->cards[i].io_cnt++;
1059 if (io_state & VGA_RSRC_LEGACY_MEM)
1060 priv->cards[i].mem_cnt++;
1061 break;
1062 }
1063 }
1064 ret_val = count;
1065 goto done;
1066 } else {
1067 ret_val = -EBUSY;
1068 goto done;
1069 }
1070
1071 } else if (strncmp(curr_pos, "target ", 7) == 0) {
1072 unsigned int domain, bus, devfn;
1073 struct vga_device *vgadev;
1074
1075 curr_pos += 7;
1076 remaining -= 7;
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +03001077 pr_debug("client 0x%p called 'target'\n", priv);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001078 /* if target is default */
Kyle McMartin2cc91162010-02-16 16:18:37 -05001079 if (!strncmp(curr_pos, "default", 7))
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001080 pdev = pci_dev_get(vga_default_device());
1081 else {
1082 if (!vga_pci_str_to_vars(curr_pos, remaining,
1083 &domain, &bus, &devfn)) {
1084 ret_val = -EPROTO;
1085 goto done;
1086 }
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +03001087 pr_debug("vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos,
Mike Travis773a38db2010-02-02 14:38:15 -08001088 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001089
Jiang Liuf85567c2012-08-28 23:43:55 +08001090 pdev = pci_get_domain_bus_and_slot(domain, bus, devfn);
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +03001091 pr_debug("vgaarb: pdev %p\n", pdev);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001092 if (!pdev) {
Jiang Liuf85567c2012-08-28 23:43:55 +08001093 pr_err("vgaarb: invalid PCI address %x:%x:%x\n",
1094 domain, bus, devfn);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001095 ret_val = -ENODEV;
1096 goto done;
1097 }
1098 }
1099
1100 vgadev = vgadev_find(pdev);
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +03001101 pr_debug("vgaarb: vgadev %p\n", vgadev);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001102 if (vgadev == NULL) {
Mike Travis773a38db2010-02-02 14:38:15 -08001103 pr_err("vgaarb: this pci device is not a vga device\n");
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001104 pci_dev_put(pdev);
1105 ret_val = -ENODEV;
1106 goto done;
1107 }
1108
1109 priv->target = pdev;
1110 for (i = 0; i < MAX_USER_CARDS; i++) {
1111 if (priv->cards[i].pdev == pdev)
1112 break;
1113 if (priv->cards[i].pdev == NULL) {
1114 priv->cards[i].pdev = pdev;
1115 priv->cards[i].io_cnt = 0;
1116 priv->cards[i].mem_cnt = 0;
1117 break;
1118 }
1119 }
1120 if (i == MAX_USER_CARDS) {
Mike Travis773a38db2010-02-02 14:38:15 -08001121 pr_err("vgaarb: maximum user cards (%d) number reached!\n",
1122 MAX_USER_CARDS);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001123 pci_dev_put(pdev);
1124 /* XXX: which value to return? */
1125 ret_val = -ENOMEM;
1126 goto done;
1127 }
1128
1129 ret_val = count;
1130 pci_dev_put(pdev);
1131 goto done;
1132
1133
1134 } else if (strncmp(curr_pos, "decodes ", 8) == 0) {
1135 curr_pos += 8;
1136 remaining -= 8;
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +03001137 pr_debug("vgaarb: client 0x%p called 'decodes'\n", priv);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001138
1139 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1140 ret_val = -EPROTO;
1141 goto done;
1142 }
1143 pdev = priv->target;
1144 if (priv->target == NULL) {
1145 ret_val = -ENODEV;
1146 goto done;
1147 }
1148
1149 __vga_set_legacy_decoding(pdev, io_state, true);
1150 ret_val = count;
1151 goto done;
1152 }
1153 /* If we got here, the message written is not part of the protocol! */
1154 kfree(kbuf);
1155 return -EPROTO;
1156
1157done:
1158 kfree(kbuf);
1159 return ret_val;
1160}
1161
1162static unsigned int vga_arb_fpoll(struct file *file, poll_table * wait)
1163{
1164 struct vga_arb_private *priv = file->private_data;
1165
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +03001166 pr_debug("%s\n", __func__);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001167
1168 if (priv == NULL)
1169 return -ENODEV;
1170 poll_wait(file, &vga_wait_queue, wait);
1171 return POLLIN;
1172}
1173
1174static int vga_arb_open(struct inode *inode, struct file *file)
1175{
1176 struct vga_arb_private *priv;
1177 unsigned long flags;
1178
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +03001179 pr_debug("%s\n", __func__);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001180
Rakib Mullickf35119d2011-07-25 17:12:56 -07001181 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001182 if (priv == NULL)
1183 return -ENOMEM;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001184 spin_lock_init(&priv->lock);
1185 file->private_data = priv;
1186
1187 spin_lock_irqsave(&vga_user_lock, flags);
1188 list_add(&priv->list, &vga_user_list);
1189 spin_unlock_irqrestore(&vga_user_lock, flags);
1190
1191 /* Set the client' lists of locks */
1192 priv->target = vga_default_device(); /* Maybe this is still null! */
1193 priv->cards[0].pdev = priv->target;
1194 priv->cards[0].io_cnt = 0;
1195 priv->cards[0].mem_cnt = 0;
1196
1197
1198 return 0;
1199}
1200
1201static int vga_arb_release(struct inode *inode, struct file *file)
1202{
1203 struct vga_arb_private *priv = file->private_data;
1204 struct vga_arb_user_card *uc;
1205 unsigned long flags;
1206 int i;
1207
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +03001208 pr_debug("%s\n", __func__);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001209
1210 if (priv == NULL)
1211 return -ENODEV;
1212
1213 spin_lock_irqsave(&vga_user_lock, flags);
1214 list_del(&priv->list);
1215 for (i = 0; i < MAX_USER_CARDS; i++) {
1216 uc = &priv->cards[i];
1217 if (uc->pdev == NULL)
1218 continue;
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +03001219 pr_debug("uc->io_cnt == %d, uc->mem_cnt == %d\n",
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001220 uc->io_cnt, uc->mem_cnt);
1221 while (uc->io_cnt--)
1222 vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
1223 while (uc->mem_cnt--)
1224 vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
1225 }
1226 spin_unlock_irqrestore(&vga_user_lock, flags);
1227
1228 kfree(priv);
1229
1230 return 0;
1231}
1232
1233static void vga_arb_device_card_gone(struct pci_dev *pdev)
1234{
1235}
1236
1237/*
1238 * callback any registered clients to let them know we have a
1239 * change in VGA cards
1240 */
1241static void vga_arbiter_notify_clients(void)
1242{
1243 struct vga_device *vgadev;
1244 unsigned long flags;
1245 uint32_t new_decodes;
1246 bool new_state;
1247
1248 if (!vga_arbiter_used)
1249 return;
1250
1251 spin_lock_irqsave(&vga_lock, flags);
1252 list_for_each_entry(vgadev, &vga_list, list) {
1253 if (vga_count > 1)
1254 new_state = false;
1255 else
1256 new_state = true;
1257 if (vgadev->set_vga_decode) {
1258 new_decodes = vgadev->set_vga_decode(vgadev->cookie, new_state);
1259 vga_update_device_decodes(vgadev, new_decodes);
1260 }
1261 }
1262 spin_unlock_irqrestore(&vga_lock, flags);
1263}
1264
1265static int pci_notify(struct notifier_block *nb, unsigned long action,
1266 void *data)
1267{
1268 struct device *dev = data;
1269 struct pci_dev *pdev = to_pci_dev(dev);
1270 bool notify = false;
1271
Tiago Vignatti2d6e9b92010-05-24 18:24:30 +03001272 pr_debug("%s\n", __func__);
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001273
1274 /* For now we're only intereted in devices added and removed. I didn't
1275 * test this thing here, so someone needs to double check for the
1276 * cases of hotplugable vga cards. */
1277 if (action == BUS_NOTIFY_ADD_DEVICE)
1278 notify = vga_arbiter_add_pci_device(pdev);
1279 else if (action == BUS_NOTIFY_DEL_DEVICE)
1280 notify = vga_arbiter_del_pci_device(pdev);
1281
1282 if (notify)
1283 vga_arbiter_notify_clients();
1284 return 0;
1285}
1286
1287static struct notifier_block pci_notifier = {
1288 .notifier_call = pci_notify,
1289};
1290
1291static const struct file_operations vga_arb_device_fops = {
1292 .read = vga_arb_read,
1293 .write = vga_arb_write,
1294 .poll = vga_arb_fpoll,
1295 .open = vga_arb_open,
1296 .release = vga_arb_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001297 .llseek = noop_llseek,
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001298};
1299
1300static struct miscdevice vga_arb_device = {
1301 MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
1302};
1303
1304static int __init vga_arb_device_init(void)
1305{
1306 int rc;
1307 struct pci_dev *pdev;
Dave Airlie3448a192010-06-01 15:32:24 +10001308 struct vga_device *vgadev;
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001309
1310 rc = misc_register(&vga_arb_device);
1311 if (rc < 0)
1312 pr_err("vgaarb: error %d registering device\n", rc);
1313
1314 bus_register_notifier(&pci_bus_type, &pci_notifier);
1315
1316 /* We add all pci devices satisfying vga class in the arbiter by
1317 * default */
1318 pdev = NULL;
1319 while ((pdev =
1320 pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
1321 PCI_ANY_ID, pdev)) != NULL)
1322 vga_arbiter_add_pci_device(pdev);
1323
1324 pr_info("vgaarb: loaded\n");
Dave Airlie3448a192010-06-01 15:32:24 +10001325
1326 list_for_each_entry(vgadev, &vga_list, list) {
Bruno Prémont86fd8872014-08-24 23:09:53 +02001327#if defined(CONFIG_X86) || defined(CONFIG_IA64)
1328 /* Override I/O based detection done by vga_arbiter_add_pci_device()
1329 * as it may take the wrong device (e.g. on Apple system under EFI).
1330 *
1331 * Select the device owning the boot framebuffer if there is one.
1332 */
1333 resource_size_t start, end;
1334 int i;
1335
1336 /* Does firmware framebuffer belong to us? */
1337 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1338 if (!(pci_resource_flags(vgadev->pdev, i) & IORESOURCE_MEM))
1339 continue;
1340
1341 start = pci_resource_start(vgadev->pdev, i);
1342 end = pci_resource_end(vgadev->pdev, i);
1343
1344 if (!start || !end)
1345 continue;
1346
1347 if (screen_info.lfb_base < start ||
1348 (screen_info.lfb_base + screen_info.lfb_size) >= end)
1349 continue;
1350 if (!vga_default_device())
1351 pr_info("vgaarb: setting as boot device: PCI:%s\n",
1352 pci_name(vgadev->pdev));
1353 else if (vgadev->pdev != vga_default_device())
1354 pr_info("vgaarb: overriding boot device: PCI:%s\n",
1355 pci_name(vgadev->pdev));
1356 vga_set_default_device(vgadev->pdev);
1357 }
1358#endif
Dave Airlie3448a192010-06-01 15:32:24 +10001359 if (vgadev->bridge_has_one_vga)
1360 pr_info("vgaarb: bridge control possible %s\n", pci_name(vgadev->pdev));
1361 else
1362 pr_info("vgaarb: no bridge control possible %s\n", pci_name(vgadev->pdev));
1363 }
Benjamin Herrenschmidtdeb2d2e2009-08-11 15:52:06 +10001364 return rc;
1365}
1366subsys_initcall(vga_arb_device_init);