Linux Audio

Check our new training course

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