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