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