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