Linux Audio

Check our new training course

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