Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: MIT
0002 /*
0003  * vgaarb.c: Implements the VGA arbitration. For details refer to
0004  * Documentation/gpu/vgaarbiter.rst
0005  *
0006  * (C) Copyright 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
0007  * (C) Copyright 2007 Paulo R. Zanoni <przanoni@gmail.com>
0008  * (C) Copyright 2007, 2009 Tiago Vignatti <vignatti@freedesktop.org>
0009  */
0010 
0011 #define pr_fmt(fmt) "vgaarb: " fmt
0012 
0013 #define vgaarb_dbg(dev, fmt, arg...)    dev_dbg(dev, "vgaarb: " fmt, ##arg)
0014 #define vgaarb_info(dev, fmt, arg...)   dev_info(dev, "vgaarb: " fmt, ##arg)
0015 #define vgaarb_err(dev, fmt, arg...)    dev_err(dev, "vgaarb: " fmt, ##arg)
0016 
0017 #include <linux/module.h>
0018 #include <linux/kernel.h>
0019 #include <linux/pci.h>
0020 #include <linux/errno.h>
0021 #include <linux/init.h>
0022 #include <linux/list.h>
0023 #include <linux/sched/signal.h>
0024 #include <linux/wait.h>
0025 #include <linux/spinlock.h>
0026 #include <linux/poll.h>
0027 #include <linux/miscdevice.h>
0028 #include <linux/slab.h>
0029 #include <linux/screen_info.h>
0030 #include <linux/vt.h>
0031 #include <linux/console.h>
0032 #include <linux/acpi.h>
0033 
0034 #include <linux/uaccess.h>
0035 
0036 #include <linux/vgaarb.h>
0037 
0038 static void vga_arbiter_notify_clients(void);
0039 /*
0040  * We keep a list of all vga devices in the system to speed
0041  * up the various operations of the arbiter
0042  */
0043 struct vga_device {
0044     struct list_head list;
0045     struct pci_dev *pdev;
0046     unsigned int decodes;   /* what does it decodes */
0047     unsigned int owns;  /* what does it owns */
0048     unsigned int locks; /* what does it locks */
0049     unsigned int io_lock_cnt;   /* legacy IO lock count */
0050     unsigned int mem_lock_cnt;  /* legacy MEM lock count */
0051     unsigned int io_norm_cnt;   /* normal IO count */
0052     unsigned int mem_norm_cnt;  /* normal MEM count */
0053     bool bridge_has_one_vga;
0054     bool is_firmware_default;   /* device selected by firmware */
0055     unsigned int (*set_decode)(struct pci_dev *pdev, bool decode);
0056 };
0057 
0058 static LIST_HEAD(vga_list);
0059 static int vga_count, vga_decode_count;
0060 static bool vga_arbiter_used;
0061 static DEFINE_SPINLOCK(vga_lock);
0062 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
0063 
0064 
0065 static const char *vga_iostate_to_str(unsigned int iostate)
0066 {
0067     /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
0068     iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
0069     switch (iostate) {
0070     case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
0071         return "io+mem";
0072     case VGA_RSRC_LEGACY_IO:
0073         return "io";
0074     case VGA_RSRC_LEGACY_MEM:
0075         return "mem";
0076     }
0077     return "none";
0078 }
0079 
0080 static int vga_str_to_iostate(char *buf, int str_size, int *io_state)
0081 {
0082     /* we could in theory hand out locks on IO and mem
0083      * separately to userspace but it can cause deadlocks */
0084     if (strncmp(buf, "none", 4) == 0) {
0085         *io_state = VGA_RSRC_NONE;
0086         return 1;
0087     }
0088 
0089     /* XXX We're not chekcing the str_size! */
0090     if (strncmp(buf, "io+mem", 6) == 0)
0091         goto both;
0092     else if (strncmp(buf, "io", 2) == 0)
0093         goto both;
0094     else if (strncmp(buf, "mem", 3) == 0)
0095         goto both;
0096     return 0;
0097 both:
0098     *io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
0099     return 1;
0100 }
0101 
0102 /* this is only used a cookie - it should not be dereferenced */
0103 static struct pci_dev *vga_default;
0104 
0105 /* Find somebody in our list */
0106 static struct vga_device *vgadev_find(struct pci_dev *pdev)
0107 {
0108     struct vga_device *vgadev;
0109 
0110     list_for_each_entry(vgadev, &vga_list, list)
0111         if (pdev == vgadev->pdev)
0112             return vgadev;
0113     return NULL;
0114 }
0115 
0116 /**
0117  * vga_default_device - return the default VGA device, for vgacon
0118  *
0119  * This can be defined by the platform. The default implementation
0120  * is rather dumb and will probably only work properly on single
0121  * vga card setups and/or x86 platforms.
0122  *
0123  * If your VGA default device is not PCI, you'll have to return
0124  * NULL here. In this case, I assume it will not conflict with
0125  * any PCI card. If this is not true, I'll have to define two archs
0126  * hooks for enabling/disabling the VGA default device if that is
0127  * possible. This may be a problem with real _ISA_ VGA cards, in
0128  * addition to a PCI one. I don't know at this point how to deal
0129  * with that card. Can theirs IOs be disabled at all ? If not, then
0130  * I suppose it's a matter of having the proper arch hook telling
0131  * us about it, so we basically never allow anybody to succeed a
0132  * vga_get()...
0133  */
0134 struct pci_dev *vga_default_device(void)
0135 {
0136     return vga_default;
0137 }
0138 EXPORT_SYMBOL_GPL(vga_default_device);
0139 
0140 void vga_set_default_device(struct pci_dev *pdev)
0141 {
0142     if (vga_default == pdev)
0143         return;
0144 
0145     pci_dev_put(vga_default);
0146     vga_default = pci_dev_get(pdev);
0147 }
0148 
0149 /**
0150  * vga_remove_vgacon - deactivete vga console
0151  *
0152  * Unbind and unregister vgacon in case pdev is the default vga
0153  * device.  Can be called by gpu drivers on initialization to make
0154  * sure vga register access done by vgacon will not disturb the
0155  * device.
0156  *
0157  * @pdev: pci device.
0158  */
0159 #if !defined(CONFIG_VGA_CONSOLE)
0160 int vga_remove_vgacon(struct pci_dev *pdev)
0161 {
0162     return 0;
0163 }
0164 #elif !defined(CONFIG_DUMMY_CONSOLE)
0165 int vga_remove_vgacon(struct pci_dev *pdev)
0166 {
0167     return -ENODEV;
0168 }
0169 #else
0170 int vga_remove_vgacon(struct pci_dev *pdev)
0171 {
0172     int ret = 0;
0173 
0174     if (pdev != vga_default)
0175         return 0;
0176     vgaarb_info(&pdev->dev, "deactivate vga console\n");
0177 
0178     console_lock();
0179     if (con_is_bound(&vga_con))
0180         ret = do_take_over_console(&dummy_con, 0,
0181                        MAX_NR_CONSOLES - 1, 1);
0182     if (ret == 0) {
0183         ret = do_unregister_con_driver(&vga_con);
0184 
0185         /* Ignore "already unregistered". */
0186         if (ret == -ENODEV)
0187             ret = 0;
0188     }
0189     console_unlock();
0190 
0191     return ret;
0192 }
0193 #endif
0194 EXPORT_SYMBOL(vga_remove_vgacon);
0195 
0196 /* If we don't ever use VGA arb we should avoid
0197    turning off anything anywhere due to old X servers getting
0198    confused about the boot device not being VGA */
0199 static void vga_check_first_use(void)
0200 {
0201     /* we should inform all GPUs in the system that
0202      * VGA arb has occurred and to try and disable resources
0203      * if they can */
0204     if (!vga_arbiter_used) {
0205         vga_arbiter_used = true;
0206         vga_arbiter_notify_clients();
0207     }
0208 }
0209 
0210 static struct vga_device *__vga_tryget(struct vga_device *vgadev,
0211                        unsigned int rsrc)
0212 {
0213     struct device *dev = &vgadev->pdev->dev;
0214     unsigned int wants, legacy_wants, match;
0215     struct vga_device *conflict;
0216     unsigned int pci_bits;
0217     u32 flags = 0;
0218 
0219     /* Account for "normal" resources to lock. If we decode the legacy,
0220      * counterpart, we need to request it as well
0221      */
0222     if ((rsrc & VGA_RSRC_NORMAL_IO) &&
0223         (vgadev->decodes & VGA_RSRC_LEGACY_IO))
0224         rsrc |= VGA_RSRC_LEGACY_IO;
0225     if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
0226         (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
0227         rsrc |= VGA_RSRC_LEGACY_MEM;
0228 
0229     vgaarb_dbg(dev, "%s: %d\n", __func__, rsrc);
0230     vgaarb_dbg(dev, "%s: owns: %d\n", __func__, vgadev->owns);
0231 
0232     /* Check what resources we need to acquire */
0233     wants = rsrc & ~vgadev->owns;
0234 
0235     /* We already own everything, just mark locked & bye bye */
0236     if (wants == 0)
0237         goto lock_them;
0238 
0239     /* We don't need to request a legacy resource, we just enable
0240      * appropriate decoding and go
0241      */
0242     legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
0243     if (legacy_wants == 0)
0244         goto enable_them;
0245 
0246     /* Ok, we don't, let's find out how we need to kick off */
0247     list_for_each_entry(conflict, &vga_list, list) {
0248         unsigned int lwants = legacy_wants;
0249         unsigned int change_bridge = 0;
0250 
0251         /* Don't conflict with myself */
0252         if (vgadev == conflict)
0253             continue;
0254 
0255         /* We have a possible conflict. before we go further, we must
0256          * check if we sit on the same bus as the conflicting device.
0257          * if we don't, then we must tie both IO and MEM resources
0258          * together since there is only a single bit controlling
0259          * VGA forwarding on P2P bridges
0260          */
0261         if (vgadev->pdev->bus != conflict->pdev->bus) {
0262             change_bridge = 1;
0263             lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
0264         }
0265 
0266         /* Check if the guy has a lock on the resource. If he does,
0267          * return the conflicting entry
0268          */
0269         if (conflict->locks & lwants)
0270             return conflict;
0271 
0272         /* Ok, now check if it owns the resource we want.  We can
0273          * lock resources that are not decoded, therefore a device
0274          * can own resources it doesn't decode.
0275          */
0276         match = lwants & conflict->owns;
0277         if (!match)
0278             continue;
0279 
0280         /* looks like he doesn't have a lock, we can steal
0281          * them from him
0282          */
0283 
0284         flags = 0;
0285         pci_bits = 0;
0286 
0287         /* If we can't control legacy resources via the bridge, we
0288          * also need to disable normal decoding.
0289          */
0290         if (!conflict->bridge_has_one_vga) {
0291             if ((match & conflict->decodes) & VGA_RSRC_LEGACY_MEM)
0292                 pci_bits |= PCI_COMMAND_MEMORY;
0293             if ((match & conflict->decodes) & VGA_RSRC_LEGACY_IO)
0294                 pci_bits |= PCI_COMMAND_IO;
0295 
0296             if (pci_bits)
0297                 flags |= PCI_VGA_STATE_CHANGE_DECODES;
0298         }
0299 
0300         if (change_bridge)
0301             flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
0302 
0303         pci_set_vga_state(conflict->pdev, false, pci_bits, flags);
0304         conflict->owns &= ~match;
0305 
0306         /* If we disabled normal decoding, reflect it in owns */
0307         if (pci_bits & PCI_COMMAND_MEMORY)
0308             conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
0309         if (pci_bits & PCI_COMMAND_IO)
0310             conflict->owns &= ~VGA_RSRC_NORMAL_IO;
0311     }
0312 
0313 enable_them:
0314     /* ok dude, we got it, everybody conflicting has been disabled, let's
0315      * enable us.  Mark any bits in "owns" regardless of whether we
0316      * decoded them.  We can lock resources we don't decode, therefore
0317      * we must track them via "owns".
0318      */
0319     flags = 0;
0320     pci_bits = 0;
0321 
0322     if (!vgadev->bridge_has_one_vga) {
0323         flags |= PCI_VGA_STATE_CHANGE_DECODES;
0324         if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
0325             pci_bits |= PCI_COMMAND_MEMORY;
0326         if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
0327             pci_bits |= PCI_COMMAND_IO;
0328     }
0329     if (wants & VGA_RSRC_LEGACY_MASK)
0330         flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
0331 
0332     pci_set_vga_state(vgadev->pdev, true, pci_bits, flags);
0333 
0334     vgadev->owns |= wants;
0335 lock_them:
0336     vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
0337     if (rsrc & VGA_RSRC_LEGACY_IO)
0338         vgadev->io_lock_cnt++;
0339     if (rsrc & VGA_RSRC_LEGACY_MEM)
0340         vgadev->mem_lock_cnt++;
0341     if (rsrc & VGA_RSRC_NORMAL_IO)
0342         vgadev->io_norm_cnt++;
0343     if (rsrc & VGA_RSRC_NORMAL_MEM)
0344         vgadev->mem_norm_cnt++;
0345 
0346     return NULL;
0347 }
0348 
0349 static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
0350 {
0351     struct device *dev = &vgadev->pdev->dev;
0352     unsigned int old_locks = vgadev->locks;
0353 
0354     vgaarb_dbg(dev, "%s\n", __func__);
0355 
0356     /* Update our counters, and account for equivalent legacy resources
0357      * if we decode them
0358      */
0359     if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
0360         vgadev->io_norm_cnt--;
0361         if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
0362             rsrc |= VGA_RSRC_LEGACY_IO;
0363     }
0364     if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
0365         vgadev->mem_norm_cnt--;
0366         if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
0367             rsrc |= VGA_RSRC_LEGACY_MEM;
0368     }
0369     if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
0370         vgadev->io_lock_cnt--;
0371     if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
0372         vgadev->mem_lock_cnt--;
0373 
0374     /* Just clear lock bits, we do lazy operations so we don't really
0375      * have to bother about anything else at this point
0376      */
0377     if (vgadev->io_lock_cnt == 0)
0378         vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
0379     if (vgadev->mem_lock_cnt == 0)
0380         vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
0381 
0382     /* Kick the wait queue in case somebody was waiting if we actually
0383      * released something
0384      */
0385     if (old_locks != vgadev->locks)
0386         wake_up_all(&vga_wait_queue);
0387 }
0388 
0389 /**
0390  * vga_get - acquire & locks VGA resources
0391  * @pdev: pci device of the VGA card or NULL for the system default
0392  * @rsrc: bit mask of resources to acquire and lock
0393  * @interruptible: blocking should be interruptible by signals ?
0394  *
0395  * This function acquires VGA resources for the given card and mark those
0396  * resources locked. If the resource requested are "normal" (and not legacy)
0397  * resources, the arbiter will first check whether the card is doing legacy
0398  * decoding for that type of resource. If yes, the lock is "converted" into a
0399  * legacy resource lock.
0400  *
0401  * The arbiter will first look for all VGA cards that might conflict and disable
0402  * their IOs and/or Memory access, including VGA forwarding on P2P bridges if
0403  * necessary, so that the requested resources can be used. Then, the card is
0404  * marked as locking these resources and the IO and/or Memory accesses are
0405  * enabled on the card (including VGA forwarding on parent P2P bridges if any).
0406  *
0407  * This function will block if some conflicting card is already locking one of
0408  * the required resources (or any resource on a different bus segment, since P2P
0409  * bridges don't differentiate VGA memory and IO afaik). You can indicate
0410  * whether this blocking should be interruptible by a signal (for userland
0411  * interface) or not.
0412  *
0413  * Must not be called at interrupt time or in atomic context.  If the card
0414  * already owns the resources, the function succeeds.  Nested calls are
0415  * supported (a per-resource counter is maintained)
0416  *
0417  * On success, release the VGA resource again with vga_put().
0418  *
0419  * Returns:
0420  *
0421  * 0 on success, negative error code on failure.
0422  */
0423 int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
0424 {
0425     struct vga_device *vgadev, *conflict;
0426     unsigned long flags;
0427     wait_queue_entry_t wait;
0428     int rc = 0;
0429 
0430     vga_check_first_use();
0431     /* The one who calls us should check for this, but lets be sure... */
0432     if (pdev == NULL)
0433         pdev = vga_default_device();
0434     if (pdev == NULL)
0435         return 0;
0436 
0437     for (;;) {
0438         spin_lock_irqsave(&vga_lock, flags);
0439         vgadev = vgadev_find(pdev);
0440         if (vgadev == NULL) {
0441             spin_unlock_irqrestore(&vga_lock, flags);
0442             rc = -ENODEV;
0443             break;
0444         }
0445         conflict = __vga_tryget(vgadev, rsrc);
0446         spin_unlock_irqrestore(&vga_lock, flags);
0447         if (conflict == NULL)
0448             break;
0449 
0450 
0451         /* We have a conflict, we wait until somebody kicks the
0452          * work queue. Currently we have one work queue that we
0453          * kick each time some resources are released, but it would
0454          * be fairly easy to have a per device one so that we only
0455          * need to attach to the conflicting device
0456          */
0457         init_waitqueue_entry(&wait, current);
0458         add_wait_queue(&vga_wait_queue, &wait);
0459         set_current_state(interruptible ?
0460                   TASK_INTERRUPTIBLE :
0461                   TASK_UNINTERRUPTIBLE);
0462         if (interruptible && signal_pending(current)) {
0463             __set_current_state(TASK_RUNNING);
0464             remove_wait_queue(&vga_wait_queue, &wait);
0465             rc = -ERESTARTSYS;
0466             break;
0467         }
0468         schedule();
0469         remove_wait_queue(&vga_wait_queue, &wait);
0470     }
0471     return rc;
0472 }
0473 EXPORT_SYMBOL(vga_get);
0474 
0475 /**
0476  * vga_tryget - try to acquire & lock legacy VGA resources
0477  * @pdev: pci devivce of VGA card or NULL for system default
0478  * @rsrc: bit mask of resources to acquire and lock
0479  *
0480  * This function performs the same operation as vga_get(), but will return an
0481  * error (-EBUSY) instead of blocking if the resources are already locked by
0482  * another card. It can be called in any context
0483  *
0484  * On success, release the VGA resource again with vga_put().
0485  *
0486  * Returns:
0487  *
0488  * 0 on success, negative error code on failure.
0489  */
0490 static int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
0491 {
0492     struct vga_device *vgadev;
0493     unsigned long flags;
0494     int rc = 0;
0495 
0496     vga_check_first_use();
0497 
0498     /* The one who calls us should check for this, but lets be sure... */
0499     if (pdev == NULL)
0500         pdev = vga_default_device();
0501     if (pdev == NULL)
0502         return 0;
0503     spin_lock_irqsave(&vga_lock, flags);
0504     vgadev = vgadev_find(pdev);
0505     if (vgadev == NULL) {
0506         rc = -ENODEV;
0507         goto bail;
0508     }
0509     if (__vga_tryget(vgadev, rsrc))
0510         rc = -EBUSY;
0511 bail:
0512     spin_unlock_irqrestore(&vga_lock, flags);
0513     return rc;
0514 }
0515 
0516 /**
0517  * vga_put - release lock on legacy VGA resources
0518  * @pdev: pci device of VGA card or NULL for system default
0519  * @rsrc: but mask of resource to release
0520  *
0521  * This fuction releases resources previously locked by vga_get() or
0522  * vga_tryget(). The resources aren't disabled right away, so that a subsequence
0523  * vga_get() on the same card will succeed immediately. Resources have a
0524  * counter, so locks are only released if the counter reaches 0.
0525  */
0526 void vga_put(struct pci_dev *pdev, unsigned int rsrc)
0527 {
0528     struct vga_device *vgadev;
0529     unsigned long flags;
0530 
0531     /* The one who calls us should check for this, but lets be sure... */
0532     if (pdev == NULL)
0533         pdev = vga_default_device();
0534     if (pdev == NULL)
0535         return;
0536     spin_lock_irqsave(&vga_lock, flags);
0537     vgadev = vgadev_find(pdev);
0538     if (vgadev == NULL)
0539         goto bail;
0540     __vga_put(vgadev, rsrc);
0541 bail:
0542     spin_unlock_irqrestore(&vga_lock, flags);
0543 }
0544 EXPORT_SYMBOL(vga_put);
0545 
0546 static bool vga_is_firmware_default(struct pci_dev *pdev)
0547 {
0548 #if defined(CONFIG_X86) || defined(CONFIG_IA64)
0549     u64 base = screen_info.lfb_base;
0550     u64 size = screen_info.lfb_size;
0551     u64 limit;
0552     resource_size_t start, end;
0553     unsigned long flags;
0554     int i;
0555 
0556     /* Select the device owning the boot framebuffer if there is one */
0557 
0558     if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
0559         base |= (u64)screen_info.ext_lfb_base << 32;
0560 
0561     limit = base + size;
0562 
0563     /* Does firmware framebuffer belong to us? */
0564     for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
0565         flags = pci_resource_flags(pdev, i);
0566 
0567         if ((flags & IORESOURCE_MEM) == 0)
0568             continue;
0569 
0570         start = pci_resource_start(pdev, i);
0571         end  = pci_resource_end(pdev, i);
0572 
0573         if (!start || !end)
0574             continue;
0575 
0576         if (base < start || limit >= end)
0577             continue;
0578 
0579         return true;
0580     }
0581 #endif
0582     return false;
0583 }
0584 
0585 static bool vga_arb_integrated_gpu(struct device *dev)
0586 {
0587 #if defined(CONFIG_ACPI)
0588     struct acpi_device *adev = ACPI_COMPANION(dev);
0589 
0590     return adev && !strcmp(acpi_device_hid(adev), ACPI_VIDEO_HID);
0591 #else
0592     return false;
0593 #endif
0594 }
0595 
0596 /*
0597  * Return true if vgadev is a better default VGA device than the best one
0598  * we've seen so far.
0599  */
0600 static bool vga_is_boot_device(struct vga_device *vgadev)
0601 {
0602     struct vga_device *boot_vga = vgadev_find(vga_default_device());
0603     struct pci_dev *pdev = vgadev->pdev;
0604     u16 cmd, boot_cmd;
0605 
0606     /*
0607      * We select the default VGA device in this order:
0608      *   Firmware framebuffer (see vga_arb_select_default_device())
0609      *   Legacy VGA device (owns VGA_RSRC_LEGACY_MASK)
0610      *   Non-legacy integrated device (see vga_arb_select_default_device())
0611      *   Non-legacy discrete device (see vga_arb_select_default_device())
0612      *   Other device (see vga_arb_select_default_device())
0613      */
0614 
0615     /*
0616      * We always prefer a firmware default device, so if we've already
0617      * found one, there's no need to consider vgadev.
0618      */
0619     if (boot_vga && boot_vga->is_firmware_default)
0620         return false;
0621 
0622     if (vga_is_firmware_default(pdev)) {
0623         vgadev->is_firmware_default = true;
0624         return true;
0625     }
0626 
0627     /*
0628      * A legacy VGA device has MEM and IO enabled and any bridges
0629      * leading to it have PCI_BRIDGE_CTL_VGA enabled so the legacy
0630      * resources ([mem 0xa0000-0xbffff], [io 0x3b0-0x3bb], etc) are
0631      * routed to it.
0632      *
0633      * We use the first one we find, so if we've already found one,
0634      * vgadev is no better.
0635      */
0636     if (boot_vga &&
0637         (boot_vga->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)
0638         return false;
0639 
0640     if ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)
0641         return true;
0642 
0643     /*
0644      * If we haven't found a legacy VGA device, accept a non-legacy
0645      * device.  It may have either IO or MEM enabled, and bridges may
0646      * not have PCI_BRIDGE_CTL_VGA enabled, so it may not be able to
0647      * use legacy VGA resources.  Prefer an integrated GPU over others.
0648      */
0649     pci_read_config_word(pdev, PCI_COMMAND, &cmd);
0650     if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
0651 
0652         /*
0653          * An integrated GPU overrides a previous non-legacy
0654          * device.  We expect only a single integrated GPU, but if
0655          * there are more, we use the *last* because that was the
0656          * previous behavior.
0657          */
0658         if (vga_arb_integrated_gpu(&pdev->dev))
0659             return true;
0660 
0661         /*
0662          * We prefer the first non-legacy discrete device we find.
0663          * If we already found one, vgadev is no better.
0664          */
0665         if (boot_vga) {
0666             pci_read_config_word(boot_vga->pdev, PCI_COMMAND,
0667                          &boot_cmd);
0668             if (boot_cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
0669                 return false;
0670         }
0671         return true;
0672     }
0673 
0674     /*
0675      * vgadev has neither IO nor MEM enabled.  If we haven't found any
0676      * other VGA devices, it is the best candidate so far.
0677      */
0678     if (!boot_vga)
0679         return true;
0680 
0681     return false;
0682 }
0683 
0684 /*
0685  * Rules for using a bridge to control a VGA descendant decoding: if a bridge
0686  * has only one VGA descendant then it can be used to control the VGA routing
0687  * for that device. It should always use the bridge closest to the device to
0688  * control it. If a bridge has a direct VGA descendant, but also have a sub-
0689  * bridge VGA descendant then we cannot use that bridge to control the direct
0690  * VGA descendant. So for every device we register, we need to iterate all
0691  * its parent bridges so we can invalidate any devices using them properly.
0692  */
0693 static void vga_arbiter_check_bridge_sharing(struct vga_device *vgadev)
0694 {
0695     struct vga_device *same_bridge_vgadev;
0696     struct pci_bus *new_bus, *bus;
0697     struct pci_dev *new_bridge, *bridge;
0698 
0699     vgadev->bridge_has_one_vga = true;
0700 
0701     if (list_empty(&vga_list)) {
0702         vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n");
0703         return;
0704     }
0705 
0706     /* okay iterate the new devices bridge hierarachy */
0707     new_bus = vgadev->pdev->bus;
0708     while (new_bus) {
0709         new_bridge = new_bus->self;
0710 
0711         /* go through list of devices already registered */
0712         list_for_each_entry(same_bridge_vgadev, &vga_list, list) {
0713             bus = same_bridge_vgadev->pdev->bus;
0714             bridge = bus->self;
0715 
0716             /* see if the share a bridge with this device */
0717             if (new_bridge == bridge) {
0718                 /*
0719                  * If their direct parent bridge is the same
0720                  * as any bridge of this device then it can't
0721                  * be used for that device.
0722                  */
0723                 same_bridge_vgadev->bridge_has_one_vga = false;
0724             }
0725 
0726             /*
0727              * Now iterate the previous devices bridge hierarchy.
0728              * If the new devices parent bridge is in the other
0729              * devices hierarchy then we can't use it to control
0730              * this device
0731              */
0732             while (bus) {
0733                 bridge = bus->self;
0734 
0735                 if (bridge && bridge == vgadev->pdev->bus->self)
0736                     vgadev->bridge_has_one_vga = false;
0737 
0738                 bus = bus->parent;
0739             }
0740         }
0741         new_bus = new_bus->parent;
0742     }
0743 
0744     if (vgadev->bridge_has_one_vga)
0745         vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n");
0746     else
0747         vgaarb_info(&vgadev->pdev->dev, "no bridge control possible\n");
0748 }
0749 
0750 /*
0751  * Currently, we assume that the "initial" setup of the system is
0752  * not sane, that is we come up with conflicting devices and let
0753  * the arbiter's client decides if devices decodes or not legacy
0754  * things.
0755  */
0756 static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
0757 {
0758     struct vga_device *vgadev;
0759     unsigned long flags;
0760     struct pci_bus *bus;
0761     struct pci_dev *bridge;
0762     u16 cmd;
0763 
0764     /* Only deal with VGA class devices */
0765     if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
0766         return false;
0767 
0768     /* Allocate structure */
0769     vgadev = kzalloc(sizeof(struct vga_device), GFP_KERNEL);
0770     if (vgadev == NULL) {
0771         vgaarb_err(&pdev->dev, "failed to allocate VGA arbiter data\n");
0772         /*
0773          * What to do on allocation failure ? For now, let's just do
0774          * nothing, I'm not sure there is anything saner to be done.
0775          */
0776         return false;
0777     }
0778 
0779     /* Take lock & check for duplicates */
0780     spin_lock_irqsave(&vga_lock, flags);
0781     if (vgadev_find(pdev) != NULL) {
0782         BUG_ON(1);
0783         goto fail;
0784     }
0785     vgadev->pdev = pdev;
0786 
0787     /* By default, assume we decode everything */
0788     vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
0789               VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
0790 
0791     /* by default mark it as decoding */
0792     vga_decode_count++;
0793     /* Mark that we "own" resources based on our enables, we will
0794      * clear that below if the bridge isn't forwarding
0795      */
0796     pci_read_config_word(pdev, PCI_COMMAND, &cmd);
0797     if (cmd & PCI_COMMAND_IO)
0798         vgadev->owns |= VGA_RSRC_LEGACY_IO;
0799     if (cmd & PCI_COMMAND_MEMORY)
0800         vgadev->owns |= VGA_RSRC_LEGACY_MEM;
0801 
0802     /* Check if VGA cycles can get down to us */
0803     bus = pdev->bus;
0804     while (bus) {
0805         bridge = bus->self;
0806         if (bridge) {
0807             u16 l;
0808 
0809             pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &l);
0810             if (!(l & PCI_BRIDGE_CTL_VGA)) {
0811                 vgadev->owns = 0;
0812                 break;
0813             }
0814         }
0815         bus = bus->parent;
0816     }
0817 
0818     if (vga_is_boot_device(vgadev)) {
0819         vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
0820                 vga_default_device() ?
0821                 " (overriding previous)" : "");
0822         vga_set_default_device(pdev);
0823     }
0824 
0825     vga_arbiter_check_bridge_sharing(vgadev);
0826 
0827     /* Add to the list */
0828     list_add_tail(&vgadev->list, &vga_list);
0829     vga_count++;
0830     vgaarb_info(&pdev->dev, "VGA device added: decodes=%s,owns=%s,locks=%s\n",
0831         vga_iostate_to_str(vgadev->decodes),
0832         vga_iostate_to_str(vgadev->owns),
0833         vga_iostate_to_str(vgadev->locks));
0834 
0835     spin_unlock_irqrestore(&vga_lock, flags);
0836     return true;
0837 fail:
0838     spin_unlock_irqrestore(&vga_lock, flags);
0839     kfree(vgadev);
0840     return false;
0841 }
0842 
0843 static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
0844 {
0845     struct vga_device *vgadev;
0846     unsigned long flags;
0847     bool ret = true;
0848 
0849     spin_lock_irqsave(&vga_lock, flags);
0850     vgadev = vgadev_find(pdev);
0851     if (vgadev == NULL) {
0852         ret = false;
0853         goto bail;
0854     }
0855 
0856     if (vga_default == pdev)
0857         vga_set_default_device(NULL);
0858 
0859     if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
0860         vga_decode_count--;
0861 
0862     /* Remove entry from list */
0863     list_del(&vgadev->list);
0864     vga_count--;
0865 
0866     /* Wake up all possible waiters */
0867     wake_up_all(&vga_wait_queue);
0868 bail:
0869     spin_unlock_irqrestore(&vga_lock, flags);
0870     kfree(vgadev);
0871     return ret;
0872 }
0873 
0874 /* this is called with the lock */
0875 static inline void vga_update_device_decodes(struct vga_device *vgadev,
0876                          int new_decodes)
0877 {
0878     struct device *dev = &vgadev->pdev->dev;
0879     int old_decodes, decodes_removed, decodes_unlocked;
0880 
0881     old_decodes = vgadev->decodes;
0882     decodes_removed = ~new_decodes & old_decodes;
0883     decodes_unlocked = vgadev->locks & decodes_removed;
0884     vgadev->decodes = new_decodes;
0885 
0886     vgaarb_info(dev, "changed VGA decodes: olddecodes=%s,decodes=%s:owns=%s\n",
0887         vga_iostate_to_str(old_decodes),
0888         vga_iostate_to_str(vgadev->decodes),
0889         vga_iostate_to_str(vgadev->owns));
0890 
0891     /* if we removed locked decodes, lock count goes to zero, and release */
0892     if (decodes_unlocked) {
0893         if (decodes_unlocked & VGA_RSRC_LEGACY_IO)
0894             vgadev->io_lock_cnt = 0;
0895         if (decodes_unlocked & VGA_RSRC_LEGACY_MEM)
0896             vgadev->mem_lock_cnt = 0;
0897         __vga_put(vgadev, decodes_unlocked);
0898     }
0899 
0900     /* change decodes counter */
0901     if (old_decodes & VGA_RSRC_LEGACY_MASK &&
0902         !(new_decodes & VGA_RSRC_LEGACY_MASK))
0903         vga_decode_count--;
0904     if (!(old_decodes & VGA_RSRC_LEGACY_MASK) &&
0905         new_decodes & VGA_RSRC_LEGACY_MASK)
0906         vga_decode_count++;
0907     vgaarb_dbg(dev, "decoding count now is: %d\n", vga_decode_count);
0908 }
0909 
0910 static void __vga_set_legacy_decoding(struct pci_dev *pdev,
0911                       unsigned int decodes,
0912                       bool userspace)
0913 {
0914     struct vga_device *vgadev;
0915     unsigned long flags;
0916 
0917     decodes &= VGA_RSRC_LEGACY_MASK;
0918 
0919     spin_lock_irqsave(&vga_lock, flags);
0920     vgadev = vgadev_find(pdev);
0921     if (vgadev == NULL)
0922         goto bail;
0923 
0924     /* don't let userspace futz with kernel driver decodes */
0925     if (userspace && vgadev->set_decode)
0926         goto bail;
0927 
0928     /* update the device decodes + counter */
0929     vga_update_device_decodes(vgadev, decodes);
0930 
0931     /* XXX if somebody is going from "doesn't decode" to "decodes" state
0932      * here, additional care must be taken as we may have pending owner
0933      * ship of non-legacy region ...
0934      */
0935 bail:
0936     spin_unlock_irqrestore(&vga_lock, flags);
0937 }
0938 
0939 /**
0940  * vga_set_legacy_decoding
0941  * @pdev: pci device of the VGA card
0942  * @decodes: bit mask of what legacy regions the card decodes
0943  *
0944  * Indicates to the arbiter if the card decodes legacy VGA IOs, legacy VGA
0945  * Memory, both, or none. All cards default to both, the card driver (fbdev for
0946  * example) should tell the arbiter if it has disabled legacy decoding, so the
0947  * card can be left out of the arbitration process (and can be safe to take
0948  * interrupts at any time.
0949  */
0950 void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
0951 {
0952     __vga_set_legacy_decoding(pdev, decodes, false);
0953 }
0954 EXPORT_SYMBOL(vga_set_legacy_decoding);
0955 
0956 /**
0957  * vga_client_register - register or unregister a VGA arbitration client
0958  * @pdev: pci device of the VGA client
0959  * @set_decode: vga decode change callback
0960  *
0961  * Clients have two callback mechanisms they can use.
0962  *
0963  * @set_decode callback: If a client can disable its GPU VGA resource, it
0964  * will get a callback from this to set the encode/decode state.
0965  *
0966  * Rationale: we cannot disable VGA decode resources unconditionally some single
0967  * GPU laptops seem to require ACPI or BIOS access to the VGA registers to
0968  * control things like backlights etc.  Hopefully newer multi-GPU laptops do
0969  * something saner, and desktops won't have any special ACPI for this. The
0970  * driver will get a callback when VGA arbitration is first used by userspace
0971  * since some older X servers have issues.
0972  *
0973  * This function does not check whether a client for @pdev has been registered
0974  * already.
0975  *
0976  * To unregister just call vga_client_unregister().
0977  *
0978  * Returns: 0 on success, -1 on failure
0979  */
0980 int vga_client_register(struct pci_dev *pdev,
0981         unsigned int (*set_decode)(struct pci_dev *pdev, bool decode))
0982 {
0983     int ret = -ENODEV;
0984     struct vga_device *vgadev;
0985     unsigned long flags;
0986 
0987     spin_lock_irqsave(&vga_lock, flags);
0988     vgadev = vgadev_find(pdev);
0989     if (!vgadev)
0990         goto bail;
0991 
0992     vgadev->set_decode = set_decode;
0993     ret = 0;
0994 
0995 bail:
0996     spin_unlock_irqrestore(&vga_lock, flags);
0997     return ret;
0998 
0999 }
1000 EXPORT_SYMBOL(vga_client_register);
1001 
1002 /*
1003  * Char driver implementation
1004  *
1005  * Semantics is:
1006  *
1007  *  open       : open user instance of the arbitrer. by default, it's
1008  *                attached to the default VGA device of the system.
1009  *
1010  *  close      : close user instance, release locks
1011  *
1012  *  read       : return a string indicating the status of the target.
1013  *                an IO state string is of the form {io,mem,io+mem,none},
1014  *                mc and ic are respectively mem and io lock counts (for
1015  *                debugging/diagnostic only). "decodes" indicate what the
1016  *                card currently decodes, "owns" indicates what is currently
1017  *                enabled on it, and "locks" indicates what is locked by this
1018  *                card. If the card is unplugged, we get "invalid" then for
1019  *                card_ID and an -ENODEV error is returned for any command
1020  *                until a new card is targeted
1021  *
1022  *   "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
1023  *
1024  * write       : write a command to the arbiter. List of commands is:
1025  *
1026  *   target <card_ID>   : switch target to card <card_ID> (see below)
1027  *   lock <io_state>    : acquires locks on target ("none" is invalid io_state)
1028  *   trylock <io_state> : non-blocking acquire locks on target
1029  *   unlock <io_state>  : release locks on target
1030  *   unlock all         : release all locks on target held by this user
1031  *   decodes <io_state> : set the legacy decoding attributes for the card
1032  *
1033  * poll         : event if something change on any card (not just the target)
1034  *
1035  * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
1036  * to go back to the system default card (TODO: not implemented yet).
1037  * Currently, only PCI is supported as a prefix, but the userland API may
1038  * support other bus types in the future, even if the current kernel
1039  * implementation doesn't.
1040  *
1041  * Note about locks:
1042  *
1043  * The driver keeps track of which user has what locks on which card. It
1044  * supports stacking, like the kernel one. This complexifies the implementation
1045  * a bit, but makes the arbiter more tolerant to userspace problems and able
1046  * to properly cleanup in all cases when a process dies.
1047  * Currently, a max of 16 cards simultaneously can have locks issued from
1048  * userspace for a given user (file descriptor instance) of the arbiter.
1049  *
1050  * If the device is hot-unplugged, there is a hook inside the module to notify
1051  * they being added/removed in the system and automatically added/removed in
1052  * the arbiter.
1053  */
1054 
1055 #define MAX_USER_CARDS         CONFIG_VGA_ARB_MAX_GPUS
1056 #define PCI_INVALID_CARD       ((struct pci_dev *)-1UL)
1057 
1058 /*
1059  * Each user has an array of these, tracking which cards have locks
1060  */
1061 struct vga_arb_user_card {
1062     struct pci_dev *pdev;
1063     unsigned int mem_cnt;
1064     unsigned int io_cnt;
1065 };
1066 
1067 struct vga_arb_private {
1068     struct list_head list;
1069     struct pci_dev *target;
1070     struct vga_arb_user_card cards[MAX_USER_CARDS];
1071     spinlock_t lock;
1072 };
1073 
1074 static LIST_HEAD(vga_user_list);
1075 static DEFINE_SPINLOCK(vga_user_lock);
1076 
1077 
1078 /*
1079  * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
1080  * returns the respective values. If the string is not in this format,
1081  * it returns 0.
1082  */
1083 static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
1084                    unsigned int *bus, unsigned int *devfn)
1085 {
1086     int n;
1087     unsigned int slot, func;
1088 
1089 
1090     n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
1091     if (n != 4)
1092         return 0;
1093 
1094     *devfn = PCI_DEVFN(slot, func);
1095 
1096     return 1;
1097 }
1098 
1099 static ssize_t vga_arb_read(struct file *file, char __user *buf,
1100                 size_t count, loff_t *ppos)
1101 {
1102     struct vga_arb_private *priv = file->private_data;
1103     struct vga_device *vgadev;
1104     struct pci_dev *pdev;
1105     unsigned long flags;
1106     size_t len;
1107     int rc;
1108     char *lbuf;
1109 
1110     lbuf = kmalloc(1024, GFP_KERNEL);
1111     if (lbuf == NULL)
1112         return -ENOMEM;
1113 
1114     /* Protects vga_list */
1115     spin_lock_irqsave(&vga_lock, flags);
1116 
1117     /* If we are targeting the default, use it */
1118     pdev = priv->target;
1119     if (pdev == NULL || pdev == PCI_INVALID_CARD) {
1120         spin_unlock_irqrestore(&vga_lock, flags);
1121         len = sprintf(lbuf, "invalid");
1122         goto done;
1123     }
1124 
1125     /* Find card vgadev structure */
1126     vgadev = vgadev_find(pdev);
1127     if (vgadev == NULL) {
1128         /* Wow, it's not in the list, that shouldn't happen,
1129          * let's fix us up and return invalid card
1130          */
1131         spin_unlock_irqrestore(&vga_lock, flags);
1132         len = sprintf(lbuf, "invalid");
1133         goto done;
1134     }
1135 
1136     /* Fill the buffer with infos */
1137     len = snprintf(lbuf, 1024,
1138                "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%u:%u)\n",
1139                vga_decode_count, pci_name(pdev),
1140                vga_iostate_to_str(vgadev->decodes),
1141                vga_iostate_to_str(vgadev->owns),
1142                vga_iostate_to_str(vgadev->locks),
1143                vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
1144 
1145     spin_unlock_irqrestore(&vga_lock, flags);
1146 done:
1147 
1148     /* Copy that to user */
1149     if (len > count)
1150         len = count;
1151     rc = copy_to_user(buf, lbuf, len);
1152     kfree(lbuf);
1153     if (rc)
1154         return -EFAULT;
1155     return len;
1156 }
1157 
1158 /*
1159  * TODO: To avoid parsing inside kernel and to improve the speed we may
1160  * consider use ioctl here
1161  */
1162 static ssize_t vga_arb_write(struct file *file, const char __user *buf,
1163                  size_t count, loff_t *ppos)
1164 {
1165     struct vga_arb_private *priv = file->private_data;
1166     struct vga_arb_user_card *uc = NULL;
1167     struct pci_dev *pdev;
1168 
1169     unsigned int io_state;
1170 
1171     char kbuf[64], *curr_pos;
1172     size_t remaining = count;
1173 
1174     int ret_val;
1175     int i;
1176 
1177     if (count >= sizeof(kbuf))
1178         return -EINVAL;
1179     if (copy_from_user(kbuf, buf, count))
1180         return -EFAULT;
1181     curr_pos = kbuf;
1182     kbuf[count] = '\0'; /* Just to make sure... */
1183 
1184     if (strncmp(curr_pos, "lock ", 5) == 0) {
1185         curr_pos += 5;
1186         remaining -= 5;
1187 
1188         pr_debug("client 0x%p called 'lock'\n", priv);
1189 
1190         if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1191             ret_val = -EPROTO;
1192             goto done;
1193         }
1194         if (io_state == VGA_RSRC_NONE) {
1195             ret_val = -EPROTO;
1196             goto done;
1197         }
1198 
1199         pdev = priv->target;
1200         if (priv->target == NULL) {
1201             ret_val = -ENODEV;
1202             goto done;
1203         }
1204 
1205         vga_get_uninterruptible(pdev, io_state);
1206 
1207         /* Update the client's locks lists... */
1208         for (i = 0; i < MAX_USER_CARDS; i++) {
1209             if (priv->cards[i].pdev == pdev) {
1210                 if (io_state & VGA_RSRC_LEGACY_IO)
1211                     priv->cards[i].io_cnt++;
1212                 if (io_state & VGA_RSRC_LEGACY_MEM)
1213                     priv->cards[i].mem_cnt++;
1214                 break;
1215             }
1216         }
1217 
1218         ret_val = count;
1219         goto done;
1220     } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
1221         curr_pos += 7;
1222         remaining -= 7;
1223 
1224         pr_debug("client 0x%p called 'unlock'\n", priv);
1225 
1226         if (strncmp(curr_pos, "all", 3) == 0)
1227             io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
1228         else {
1229             if (!vga_str_to_iostate
1230                 (curr_pos, remaining, &io_state)) {
1231                 ret_val = -EPROTO;
1232                 goto done;
1233             }
1234             /* TODO: Add this?
1235                if (io_state == VGA_RSRC_NONE) {
1236                ret_val = -EPROTO;
1237                goto done;
1238                }
1239               */
1240         }
1241 
1242         pdev = priv->target;
1243         if (priv->target == NULL) {
1244             ret_val = -ENODEV;
1245             goto done;
1246         }
1247         for (i = 0; i < MAX_USER_CARDS; i++) {
1248             if (priv->cards[i].pdev == pdev)
1249                 uc = &priv->cards[i];
1250         }
1251 
1252         if (!uc) {
1253             ret_val = -EINVAL;
1254             goto done;
1255         }
1256 
1257         if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
1258             ret_val = -EINVAL;
1259             goto done;
1260         }
1261 
1262         if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
1263             ret_val = -EINVAL;
1264             goto done;
1265         }
1266 
1267         vga_put(pdev, io_state);
1268 
1269         if (io_state & VGA_RSRC_LEGACY_IO)
1270             uc->io_cnt--;
1271         if (io_state & VGA_RSRC_LEGACY_MEM)
1272             uc->mem_cnt--;
1273 
1274         ret_val = count;
1275         goto done;
1276     } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
1277         curr_pos += 8;
1278         remaining -= 8;
1279 
1280         pr_debug("client 0x%p called 'trylock'\n", priv);
1281 
1282         if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1283             ret_val = -EPROTO;
1284             goto done;
1285         }
1286         /* TODO: Add this?
1287            if (io_state == VGA_RSRC_NONE) {
1288            ret_val = -EPROTO;
1289            goto done;
1290            }
1291          */
1292 
1293         pdev = priv->target;
1294         if (priv->target == NULL) {
1295             ret_val = -ENODEV;
1296             goto done;
1297         }
1298 
1299         if (vga_tryget(pdev, io_state)) {
1300             /* Update the client's locks lists... */
1301             for (i = 0; i < MAX_USER_CARDS; i++) {
1302                 if (priv->cards[i].pdev == pdev) {
1303                     if (io_state & VGA_RSRC_LEGACY_IO)
1304                         priv->cards[i].io_cnt++;
1305                     if (io_state & VGA_RSRC_LEGACY_MEM)
1306                         priv->cards[i].mem_cnt++;
1307                     break;
1308                 }
1309             }
1310             ret_val = count;
1311             goto done;
1312         } else {
1313             ret_val = -EBUSY;
1314             goto done;
1315         }
1316 
1317     } else if (strncmp(curr_pos, "target ", 7) == 0) {
1318         unsigned int domain, bus, devfn;
1319         struct vga_device *vgadev;
1320 
1321         curr_pos += 7;
1322         remaining -= 7;
1323         pr_debug("client 0x%p called 'target'\n", priv);
1324         /* if target is default */
1325         if (!strncmp(curr_pos, "default", 7))
1326             pdev = pci_dev_get(vga_default_device());
1327         else {
1328             if (!vga_pci_str_to_vars(curr_pos, remaining,
1329                          &domain, &bus, &devfn)) {
1330                 ret_val = -EPROTO;
1331                 goto done;
1332             }
1333             pdev = pci_get_domain_bus_and_slot(domain, bus, devfn);
1334             if (!pdev) {
1335                 pr_debug("invalid PCI address %04x:%02x:%02x.%x\n",
1336                      domain, bus, PCI_SLOT(devfn),
1337                      PCI_FUNC(devfn));
1338                 ret_val = -ENODEV;
1339                 goto done;
1340             }
1341 
1342             pr_debug("%s ==> %04x:%02x:%02x.%x pdev %p\n", curr_pos,
1343                 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
1344                 pdev);
1345         }
1346 
1347         vgadev = vgadev_find(pdev);
1348         pr_debug("vgadev %p\n", vgadev);
1349         if (vgadev == NULL) {
1350             if (pdev) {
1351                 vgaarb_dbg(&pdev->dev, "not a VGA device\n");
1352                 pci_dev_put(pdev);
1353             }
1354 
1355             ret_val = -ENODEV;
1356             goto done;
1357         }
1358 
1359         priv->target = pdev;
1360         for (i = 0; i < MAX_USER_CARDS; i++) {
1361             if (priv->cards[i].pdev == pdev)
1362                 break;
1363             if (priv->cards[i].pdev == NULL) {
1364                 priv->cards[i].pdev = pdev;
1365                 priv->cards[i].io_cnt = 0;
1366                 priv->cards[i].mem_cnt = 0;
1367                 break;
1368             }
1369         }
1370         if (i == MAX_USER_CARDS) {
1371             vgaarb_dbg(&pdev->dev, "maximum user cards (%d) number reached, ignoring this one!\n",
1372                 MAX_USER_CARDS);
1373             pci_dev_put(pdev);
1374             /* XXX: which value to return? */
1375             ret_val =  -ENOMEM;
1376             goto done;
1377         }
1378 
1379         ret_val = count;
1380         pci_dev_put(pdev);
1381         goto done;
1382 
1383 
1384     } else if (strncmp(curr_pos, "decodes ", 8) == 0) {
1385         curr_pos += 8;
1386         remaining -= 8;
1387         pr_debug("client 0x%p called 'decodes'\n", priv);
1388 
1389         if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1390             ret_val = -EPROTO;
1391             goto done;
1392         }
1393         pdev = priv->target;
1394         if (priv->target == NULL) {
1395             ret_val = -ENODEV;
1396             goto done;
1397         }
1398 
1399         __vga_set_legacy_decoding(pdev, io_state, true);
1400         ret_val = count;
1401         goto done;
1402     }
1403     /* If we got here, the message written is not part of the protocol! */
1404     return -EPROTO;
1405 
1406 done:
1407     return ret_val;
1408 }
1409 
1410 static __poll_t vga_arb_fpoll(struct file *file, poll_table *wait)
1411 {
1412     pr_debug("%s\n", __func__);
1413 
1414     poll_wait(file, &vga_wait_queue, wait);
1415     return EPOLLIN;
1416 }
1417 
1418 static int vga_arb_open(struct inode *inode, struct file *file)
1419 {
1420     struct vga_arb_private *priv;
1421     unsigned long flags;
1422 
1423     pr_debug("%s\n", __func__);
1424 
1425     priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1426     if (priv == NULL)
1427         return -ENOMEM;
1428     spin_lock_init(&priv->lock);
1429     file->private_data = priv;
1430 
1431     spin_lock_irqsave(&vga_user_lock, flags);
1432     list_add(&priv->list, &vga_user_list);
1433     spin_unlock_irqrestore(&vga_user_lock, flags);
1434 
1435     /* Set the client' lists of locks */
1436     priv->target = vga_default_device(); /* Maybe this is still null! */
1437     priv->cards[0].pdev = priv->target;
1438     priv->cards[0].io_cnt = 0;
1439     priv->cards[0].mem_cnt = 0;
1440 
1441 
1442     return 0;
1443 }
1444 
1445 static int vga_arb_release(struct inode *inode, struct file *file)
1446 {
1447     struct vga_arb_private *priv = file->private_data;
1448     struct vga_arb_user_card *uc;
1449     unsigned long flags;
1450     int i;
1451 
1452     pr_debug("%s\n", __func__);
1453 
1454     spin_lock_irqsave(&vga_user_lock, flags);
1455     list_del(&priv->list);
1456     for (i = 0; i < MAX_USER_CARDS; i++) {
1457         uc = &priv->cards[i];
1458         if (uc->pdev == NULL)
1459             continue;
1460         vgaarb_dbg(&uc->pdev->dev, "uc->io_cnt == %d, uc->mem_cnt == %d\n",
1461             uc->io_cnt, uc->mem_cnt);
1462         while (uc->io_cnt--)
1463             vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
1464         while (uc->mem_cnt--)
1465             vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
1466     }
1467     spin_unlock_irqrestore(&vga_user_lock, flags);
1468 
1469     kfree(priv);
1470 
1471     return 0;
1472 }
1473 
1474 /*
1475  * callback any registered clients to let them know we have a
1476  * change in VGA cards
1477  */
1478 static void vga_arbiter_notify_clients(void)
1479 {
1480     struct vga_device *vgadev;
1481     unsigned long flags;
1482     uint32_t new_decodes;
1483     bool new_state;
1484 
1485     if (!vga_arbiter_used)
1486         return;
1487 
1488     spin_lock_irqsave(&vga_lock, flags);
1489     list_for_each_entry(vgadev, &vga_list, list) {
1490         if (vga_count > 1)
1491             new_state = false;
1492         else
1493             new_state = true;
1494         if (vgadev->set_decode) {
1495             new_decodes = vgadev->set_decode(vgadev->pdev,
1496                              new_state);
1497             vga_update_device_decodes(vgadev, new_decodes);
1498         }
1499     }
1500     spin_unlock_irqrestore(&vga_lock, flags);
1501 }
1502 
1503 static int pci_notify(struct notifier_block *nb, unsigned long action,
1504               void *data)
1505 {
1506     struct device *dev = data;
1507     struct pci_dev *pdev = to_pci_dev(dev);
1508     bool notify = false;
1509 
1510     vgaarb_dbg(dev, "%s\n", __func__);
1511 
1512     /* For now we're only intereted in devices added and removed. I didn't
1513      * test this thing here, so someone needs to double check for the
1514      * cases of hotplugable vga cards. */
1515     if (action == BUS_NOTIFY_ADD_DEVICE)
1516         notify = vga_arbiter_add_pci_device(pdev);
1517     else if (action == BUS_NOTIFY_DEL_DEVICE)
1518         notify = vga_arbiter_del_pci_device(pdev);
1519 
1520     if (notify)
1521         vga_arbiter_notify_clients();
1522     return 0;
1523 }
1524 
1525 static struct notifier_block pci_notifier = {
1526     .notifier_call = pci_notify,
1527 };
1528 
1529 static const struct file_operations vga_arb_device_fops = {
1530     .read = vga_arb_read,
1531     .write = vga_arb_write,
1532     .poll = vga_arb_fpoll,
1533     .open = vga_arb_open,
1534     .release = vga_arb_release,
1535     .llseek = noop_llseek,
1536 };
1537 
1538 static struct miscdevice vga_arb_device = {
1539     MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
1540 };
1541 
1542 static int __init vga_arb_device_init(void)
1543 {
1544     int rc;
1545     struct pci_dev *pdev;
1546 
1547     rc = misc_register(&vga_arb_device);
1548     if (rc < 0)
1549         pr_err("error %d registering device\n", rc);
1550 
1551     bus_register_notifier(&pci_bus_type, &pci_notifier);
1552 
1553     /* We add all PCI devices satisfying VGA class in the arbiter by
1554      * default */
1555     pdev = NULL;
1556     while ((pdev =
1557         pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
1558                    PCI_ANY_ID, pdev)) != NULL)
1559         vga_arbiter_add_pci_device(pdev);
1560 
1561     pr_info("loaded\n");
1562     return rc;
1563 }
1564 subsys_initcall_sync(vga_arb_device_init);