2022-02-24 22:47:53 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2009-08-11 05:52:06 +00:00
|
|
|
/*
|
2023-08-08 22:34:07 +00:00
|
|
|
* vgaarb.c: Implements VGA arbitration. For details refer to
|
2018-05-08 18:14:57 +00:00
|
|
|
* Documentation/gpu/vgaarbiter.rst
|
2010-05-24 15:24:31 +00:00
|
|
|
*
|
2009-08-11 05:52:06 +00:00
|
|
|
* (C) Copyright 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
|
|
|
|
* (C) Copyright 2007 Paulo R. Zanoni <przanoni@gmail.com>
|
|
|
|
* (C) Copyright 2007, 2009 Tiago Vignatti <vignatti@freedesktop.org>
|
|
|
|
*/
|
|
|
|
|
2015-08-12 14:32:06 +00:00
|
|
|
#define pr_fmt(fmt) "vgaarb: " fmt
|
|
|
|
|
2016-11-17 17:47:58 +00:00
|
|
|
#define vgaarb_dbg(dev, fmt, arg...) dev_dbg(dev, "vgaarb: " fmt, ##arg)
|
|
|
|
#define vgaarb_info(dev, fmt, arg...) dev_info(dev, "vgaarb: " fmt, ##arg)
|
|
|
|
#define vgaarb_err(dev, fmt, arg...) dev_err(dev, "vgaarb: " fmt, ##arg)
|
|
|
|
|
2009-08-11 05:52:06 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/list.h>
|
2017-02-02 18:15:33 +00:00
|
|
|
#include <linux/sched/signal.h>
|
2009-08-11 05:52:06 +00:00
|
|
|
#include <linux/wait.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/miscdevice.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2014-08-24 21:09:53 +00:00
|
|
|
#include <linux/screen_info.h>
|
2019-03-01 09:24:59 +00:00
|
|
|
#include <linux/vt.h>
|
|
|
|
#include <linux/console.h>
|
2021-05-19 13:57:23 +00:00
|
|
|
#include <linux/acpi.h>
|
2009-08-11 05:52:06 +00:00
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/vgaarb.h>
|
|
|
|
|
|
|
|
static void vga_arbiter_notify_clients(void);
|
2023-08-08 22:34:07 +00:00
|
|
|
|
2009-08-11 05:52:06 +00:00
|
|
|
/*
|
2023-08-08 22:34:07 +00:00
|
|
|
* We keep a list of all VGA devices in the system to speed
|
2009-08-11 05:52:06 +00:00
|
|
|
* up the various operations of the arbiter
|
|
|
|
*/
|
|
|
|
struct vga_device {
|
|
|
|
struct list_head list;
|
|
|
|
struct pci_dev *pdev;
|
2023-08-08 22:34:07 +00:00
|
|
|
unsigned int decodes; /* what it decodes */
|
|
|
|
unsigned int owns; /* what it owns */
|
|
|
|
unsigned int locks; /* what it locks */
|
2009-08-11 05:52:06 +00:00
|
|
|
unsigned int io_lock_cnt; /* legacy IO lock count */
|
|
|
|
unsigned int mem_lock_cnt; /* legacy MEM lock count */
|
|
|
|
unsigned int io_norm_cnt; /* normal IO count */
|
|
|
|
unsigned int mem_norm_cnt; /* normal MEM count */
|
2010-06-01 05:32:24 +00:00
|
|
|
bool bridge_has_one_vga;
|
PCI/VGA: Move firmware default device detection to ADD_DEVICE path
Previously we selected the firmware default device, i.e., one that owns the
boot framebuffer, as the default device in vga_arb_select_default_device().
This was only done in the vga_arb_device_init() subsys_initcall, so devices
enumerated later, e.g., by pcibios_init(), were not eligible.
Fix this by moving the firmware default device selection from
vga_arb_select_default_device() to vga_arbiter_add_pci_device(), which is
called after every PCI device is enumerated, either by the
vga_arb_device_init() subsys_initcall or as an ADD_DEVICE notifier.
Note that if vga_arb_select_default_device() previously found a device
owning the boot framebuffer, it unconditionally set it to be the default
VGA device, and no subsequent device could replace it.
[bhelgaas: commit log, restructure slightly]
Link: https://lore.kernel.org/r/20211015061512.2941859-7-chenhuacai@loongson.cn
Link: https://lore.kernel.org/r/20220224224753.297579-6-helgaas@kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Cc: Bruno Prémont <bonbons@linux-vserver.org>
2022-02-24 22:47:47 +00:00
|
|
|
bool is_firmware_default; /* device selected by firmware */
|
2021-07-16 06:16:34 +00:00
|
|
|
unsigned int (*set_decode)(struct pci_dev *pdev, bool decode);
|
2009-08-11 05:52:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(vga_list);
|
|
|
|
static int vga_count, vga_decode_count;
|
|
|
|
static bool vga_arbiter_used;
|
|
|
|
static DEFINE_SPINLOCK(vga_lock);
|
|
|
|
static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
|
|
|
|
|
|
|
|
static const char *vga_iostate_to_str(unsigned int iostate)
|
|
|
|
{
|
|
|
|
/* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
|
|
|
|
iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
|
|
|
|
switch (iostate) {
|
|
|
|
case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
|
|
|
|
return "io+mem";
|
|
|
|
case VGA_RSRC_LEGACY_IO:
|
|
|
|
return "io";
|
|
|
|
case VGA_RSRC_LEGACY_MEM:
|
|
|
|
return "mem";
|
|
|
|
}
|
|
|
|
return "none";
|
|
|
|
}
|
|
|
|
|
2023-08-08 22:34:02 +00:00
|
|
|
static int vga_str_to_iostate(char *buf, int str_size, unsigned int *io_state)
|
2009-08-11 05:52:06 +00:00
|
|
|
{
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* In theory, we could hand out locks on IO and MEM separately to
|
|
|
|
* userspace, but this can cause deadlocks.
|
|
|
|
*/
|
2009-08-11 05:52:06 +00:00
|
|
|
if (strncmp(buf, "none", 4) == 0) {
|
|
|
|
*io_state = VGA_RSRC_NONE;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* XXX We're not checking the str_size! */
|
2009-08-11 05:52:06 +00:00
|
|
|
if (strncmp(buf, "io+mem", 6) == 0)
|
|
|
|
goto both;
|
|
|
|
else if (strncmp(buf, "io", 2) == 0)
|
|
|
|
goto both;
|
|
|
|
else if (strncmp(buf, "mem", 3) == 0)
|
|
|
|
goto both;
|
|
|
|
return 0;
|
|
|
|
both:
|
|
|
|
*io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* This is only used as a cookie, it should not be dereferenced */
|
2009-08-11 05:52:06 +00:00
|
|
|
static struct pci_dev *vga_default;
|
|
|
|
|
|
|
|
/* Find somebody in our list */
|
|
|
|
static struct vga_device *vgadev_find(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
struct vga_device *vgadev;
|
|
|
|
|
|
|
|
list_for_each_entry(vgadev, &vga_list, list)
|
|
|
|
if (pdev == vgadev->pdev)
|
|
|
|
return vgadev;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-12 20:48:56 +00:00
|
|
|
/**
|
|
|
|
* vga_default_device - return the default VGA device, for vgacon
|
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* This can be defined by the platform. The default implementation is
|
|
|
|
* rather dumb and will probably only work properly on single VGA card
|
|
|
|
* setups and/or x86 platforms.
|
2016-08-12 20:48:56 +00:00
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* If your VGA default device is not PCI, you'll have to return NULL here.
|
|
|
|
* In this case, I assume it will not conflict with any PCI card. If this
|
|
|
|
* is not true, I'll have to define two arch hooks for enabling/disabling
|
|
|
|
* the VGA default device if that is possible. This may be a problem with
|
|
|
|
* real _ISA_ VGA cards, in addition to a PCI one. I don't know at this
|
|
|
|
* point how to deal with that card. Can their IOs be disabled at all? If
|
|
|
|
* not, then I suppose it's a matter of having the proper arch hook telling
|
|
|
|
* us about it, so we basically never allow anybody to succeed a vga_get().
|
2016-08-12 20:48:56 +00:00
|
|
|
*/
|
2009-08-11 05:52:06 +00:00
|
|
|
struct pci_dev *vga_default_device(void)
|
|
|
|
{
|
|
|
|
return vga_default;
|
|
|
|
}
|
2012-04-24 08:31:28 +00:00
|
|
|
EXPORT_SYMBOL_GPL(vga_default_device);
|
|
|
|
|
2012-04-16 20:26:02 +00:00
|
|
|
void vga_set_default_device(struct pci_dev *pdev)
|
|
|
|
{
|
2012-09-19 18:54:15 +00:00
|
|
|
if (vga_default == pdev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pci_dev_put(vga_default);
|
|
|
|
vga_default = pci_dev_get(pdev);
|
2012-04-16 20:26:02 +00:00
|
|
|
}
|
2009-08-11 05:52:06 +00:00
|
|
|
|
2019-03-01 09:24:59 +00:00
|
|
|
/**
|
2023-08-08 22:34:07 +00:00
|
|
|
* vga_remove_vgacon - deactivate VGA console
|
2019-03-01 09:24:59 +00:00
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* Unbind and unregister vgacon in case pdev is the default VGA device.
|
|
|
|
* Can be called by GPU drivers on initialization to make sure VGA register
|
|
|
|
* access done by vgacon will not disturb the device.
|
2019-03-01 09:24:59 +00:00
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* @pdev: PCI device.
|
2019-03-01 09:24:59 +00:00
|
|
|
*/
|
|
|
|
#if !defined(CONFIG_VGA_CONSOLE)
|
|
|
|
int vga_remove_vgacon(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#elif !defined(CONFIG_DUMMY_CONSOLE)
|
|
|
|
int vga_remove_vgacon(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int vga_remove_vgacon(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (pdev != vga_default)
|
|
|
|
return 0;
|
|
|
|
vgaarb_info(&pdev->dev, "deactivate vga console\n");
|
|
|
|
|
|
|
|
console_lock();
|
|
|
|
if (con_is_bound(&vga_con))
|
|
|
|
ret = do_take_over_console(&dummy_con, 0,
|
|
|
|
MAX_NR_CONSOLES - 1, 1);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = do_unregister_con_driver(&vga_con);
|
|
|
|
|
|
|
|
/* Ignore "already unregistered". */
|
|
|
|
if (ret == -ENODEV)
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
console_unlock();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
EXPORT_SYMBOL(vga_remove_vgacon);
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* If we don't ever use VGA arbitration, we should avoid turning off
|
|
|
|
* anything anywhere due to old X servers getting confused about the boot
|
|
|
|
* device not being VGA.
|
|
|
|
*/
|
2009-08-11 05:52:06 +00:00
|
|
|
static void vga_check_first_use(void)
|
|
|
|
{
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* Inform all GPUs in the system that VGA arbitration has occurred
|
|
|
|
* so they can disable resources if possible.
|
|
|
|
*/
|
2009-08-11 05:52:06 +00:00
|
|
|
if (!vga_arbiter_used) {
|
|
|
|
vga_arbiter_used = true;
|
|
|
|
vga_arbiter_notify_clients();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct vga_device *__vga_tryget(struct vga_device *vgadev,
|
|
|
|
unsigned int rsrc)
|
|
|
|
{
|
2016-11-17 17:47:58 +00:00
|
|
|
struct device *dev = &vgadev->pdev->dev;
|
2009-08-11 05:52:06 +00:00
|
|
|
unsigned int wants, legacy_wants, match;
|
|
|
|
struct vga_device *conflict;
|
|
|
|
unsigned int pci_bits;
|
2010-06-01 05:32:24 +00:00
|
|
|
u32 flags = 0;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* Account for "normal" resources to lock. If we decode the legacy,
|
2009-08-11 05:52:06 +00:00
|
|
|
* counterpart, we need to request it as well
|
|
|
|
*/
|
|
|
|
if ((rsrc & VGA_RSRC_NORMAL_IO) &&
|
|
|
|
(vgadev->decodes & VGA_RSRC_LEGACY_IO))
|
|
|
|
rsrc |= VGA_RSRC_LEGACY_IO;
|
|
|
|
if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
|
|
|
|
(vgadev->decodes & VGA_RSRC_LEGACY_MEM))
|
|
|
|
rsrc |= VGA_RSRC_LEGACY_MEM;
|
|
|
|
|
2016-11-17 17:47:58 +00:00
|
|
|
vgaarb_dbg(dev, "%s: %d\n", __func__, rsrc);
|
|
|
|
vgaarb_dbg(dev, "%s: owns: %d\n", __func__, vgadev->owns);
|
2009-08-11 05:52:06 +00:00
|
|
|
|
|
|
|
/* Check what resources we need to acquire */
|
|
|
|
wants = rsrc & ~vgadev->owns;
|
|
|
|
|
|
|
|
/* We already own everything, just mark locked & bye bye */
|
|
|
|
if (wants == 0)
|
|
|
|
goto lock_them;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* We don't need to request a legacy resource, we just enable
|
|
|
|
* appropriate decoding and go.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
|
|
|
|
if (legacy_wants == 0)
|
|
|
|
goto enable_them;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Ok, we don't, let's find out who we need to kick off */
|
2009-08-11 05:52:06 +00:00
|
|
|
list_for_each_entry(conflict, &vga_list, list) {
|
|
|
|
unsigned int lwants = legacy_wants;
|
|
|
|
unsigned int change_bridge = 0;
|
|
|
|
|
|
|
|
/* Don't conflict with myself */
|
|
|
|
if (vgadev == conflict)
|
|
|
|
continue;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* We have a possible conflict. Before we go further, we must
|
2009-08-11 05:52:06 +00:00
|
|
|
* check if we sit on the same bus as the conflicting device.
|
2023-08-08 22:34:07 +00:00
|
|
|
* If we don't, then we must tie both IO and MEM resources
|
2009-08-11 05:52:06 +00:00
|
|
|
* together since there is only a single bit controlling
|
2023-08-08 22:34:07 +00:00
|
|
|
* VGA forwarding on P2P bridges.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
if (vgadev->pdev->bus != conflict->pdev->bus) {
|
|
|
|
change_bridge = 1;
|
|
|
|
lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
|
|
|
|
}
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* Check if the guy has a lock on the resource. If he does,
|
|
|
|
* return the conflicting entry.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
if (conflict->locks & lwants)
|
|
|
|
return conflict;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* Ok, now check if it owns the resource we want. We can
|
|
|
|
* lock resources that are not decoded; therefore a device
|
2014-07-03 15:59:51 +00:00
|
|
|
* can own resources it doesn't decode.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
match = lwants & conflict->owns;
|
|
|
|
if (!match)
|
|
|
|
continue;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* Looks like he doesn't have a lock, we can steal them
|
|
|
|
* from him.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
|
2010-06-01 05:32:24 +00:00
|
|
|
flags = 0;
|
2009-08-11 05:52:06 +00:00
|
|
|
pci_bits = 0;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* If we can't control legacy resources via the bridge, we
|
2014-07-03 15:59:51 +00:00
|
|
|
* also need to disable normal decoding.
|
|
|
|
*/
|
2010-06-01 05:32:24 +00:00
|
|
|
if (!conflict->bridge_has_one_vga) {
|
2014-07-03 15:59:51 +00:00
|
|
|
if ((match & conflict->decodes) & VGA_RSRC_LEGACY_MEM)
|
2010-06-01 05:32:24 +00:00
|
|
|
pci_bits |= PCI_COMMAND_MEMORY;
|
2014-07-03 15:59:51 +00:00
|
|
|
if ((match & conflict->decodes) & VGA_RSRC_LEGACY_IO)
|
2010-06-01 05:32:24 +00:00
|
|
|
pci_bits |= PCI_COMMAND_IO;
|
2014-07-03 15:59:51 +00:00
|
|
|
|
2021-07-16 06:16:33 +00:00
|
|
|
if (pci_bits)
|
2014-07-03 15:59:51 +00:00
|
|
|
flags |= PCI_VGA_STATE_CHANGE_DECODES;
|
2010-06-01 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (change_bridge)
|
|
|
|
flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
|
|
|
|
|
|
|
|
pci_set_vga_state(conflict->pdev, false, pci_bits, flags);
|
2013-08-15 22:37:53 +00:00
|
|
|
conflict->owns &= ~match;
|
2014-07-03 15:59:51 +00:00
|
|
|
|
|
|
|
/* If we disabled normal decoding, reflect it in owns */
|
|
|
|
if (pci_bits & PCI_COMMAND_MEMORY)
|
2009-08-11 05:52:06 +00:00
|
|
|
conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
|
2014-07-03 15:59:51 +00:00
|
|
|
if (pci_bits & PCI_COMMAND_IO)
|
2009-08-11 05:52:06 +00:00
|
|
|
conflict->owns &= ~VGA_RSRC_NORMAL_IO;
|
|
|
|
}
|
|
|
|
|
|
|
|
enable_them:
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* Ok, we got it, everybody conflicting has been disabled, let's
|
2014-07-03 15:59:51 +00:00
|
|
|
* enable us. Mark any bits in "owns" regardless of whether we
|
|
|
|
* decoded them. We can lock resources we don't decode, therefore
|
|
|
|
* we must track them via "owns".
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
2010-06-01 05:32:24 +00:00
|
|
|
flags = 0;
|
2009-08-11 05:52:06 +00:00
|
|
|
pci_bits = 0;
|
|
|
|
|
2010-06-01 05:32:24 +00:00
|
|
|
if (!vgadev->bridge_has_one_vga) {
|
|
|
|
flags |= PCI_VGA_STATE_CHANGE_DECODES;
|
|
|
|
if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
|
|
|
|
pci_bits |= PCI_COMMAND_MEMORY;
|
|
|
|
if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
|
|
|
|
pci_bits |= PCI_COMMAND_IO;
|
|
|
|
}
|
2014-07-03 15:59:51 +00:00
|
|
|
if (wants & VGA_RSRC_LEGACY_MASK)
|
2010-06-01 05:32:24 +00:00
|
|
|
flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
|
|
|
|
|
|
|
|
pci_set_vga_state(vgadev->pdev, true, pci_bits, flags);
|
|
|
|
|
2014-07-03 15:59:51 +00:00
|
|
|
vgadev->owns |= wants;
|
2009-08-11 05:52:06 +00:00
|
|
|
lock_them:
|
|
|
|
vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
|
|
|
|
if (rsrc & VGA_RSRC_LEGACY_IO)
|
|
|
|
vgadev->io_lock_cnt++;
|
|
|
|
if (rsrc & VGA_RSRC_LEGACY_MEM)
|
|
|
|
vgadev->mem_lock_cnt++;
|
|
|
|
if (rsrc & VGA_RSRC_NORMAL_IO)
|
|
|
|
vgadev->io_norm_cnt++;
|
|
|
|
if (rsrc & VGA_RSRC_NORMAL_MEM)
|
|
|
|
vgadev->mem_norm_cnt++;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
|
|
|
|
{
|
2016-11-17 17:47:58 +00:00
|
|
|
struct device *dev = &vgadev->pdev->dev;
|
2009-08-11 05:52:06 +00:00
|
|
|
unsigned int old_locks = vgadev->locks;
|
|
|
|
|
2016-11-17 17:47:58 +00:00
|
|
|
vgaarb_dbg(dev, "%s\n", __func__);
|
2009-08-11 05:52:06 +00:00
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* Update our counters and account for equivalent legacy resources
|
|
|
|
* if we decode them.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
|
|
|
|
vgadev->io_norm_cnt--;
|
|
|
|
if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
|
|
|
|
rsrc |= VGA_RSRC_LEGACY_IO;
|
|
|
|
}
|
|
|
|
if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
|
|
|
|
vgadev->mem_norm_cnt--;
|
|
|
|
if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
|
|
|
|
rsrc |= VGA_RSRC_LEGACY_MEM;
|
|
|
|
}
|
|
|
|
if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
|
|
|
|
vgadev->io_lock_cnt--;
|
|
|
|
if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
|
|
|
|
vgadev->mem_lock_cnt--;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* Just clear lock bits, we do lazy operations so we don't really
|
|
|
|
* have to bother about anything else at this point.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
if (vgadev->io_lock_cnt == 0)
|
|
|
|
vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
|
|
|
|
if (vgadev->mem_lock_cnt == 0)
|
|
|
|
vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* Kick the wait queue in case somebody was waiting if we actually
|
|
|
|
* released something.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
if (old_locks != vgadev->locks)
|
|
|
|
wake_up_all(&vga_wait_queue);
|
|
|
|
}
|
|
|
|
|
2016-08-12 20:48:56 +00:00
|
|
|
/**
|
2023-08-08 22:34:07 +00:00
|
|
|
* vga_get - acquire & lock VGA resources
|
|
|
|
* @pdev: PCI device of the VGA card or NULL for the system default
|
2016-08-12 20:48:56 +00:00
|
|
|
* @rsrc: bit mask of resources to acquire and lock
|
|
|
|
* @interruptible: blocking should be interruptible by signals ?
|
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* Acquire VGA resources for the given card and mark those resources
|
|
|
|
* locked. If the resources requested are "normal" (and not legacy)
|
2016-08-12 20:48:56 +00:00
|
|
|
* resources, the arbiter will first check whether the card is doing legacy
|
2023-08-08 22:34:07 +00:00
|
|
|
* decoding for that type of resource. If yes, the lock is "converted" into
|
|
|
|
* a legacy resource lock.
|
2016-08-12 20:48:56 +00:00
|
|
|
*
|
|
|
|
* The arbiter will first look for all VGA cards that might conflict and disable
|
|
|
|
* their IOs and/or Memory access, including VGA forwarding on P2P bridges if
|
|
|
|
* necessary, so that the requested resources can be used. Then, the card is
|
|
|
|
* marked as locking these resources and the IO and/or Memory accesses are
|
|
|
|
* enabled on the card (including VGA forwarding on parent P2P bridges if any).
|
|
|
|
*
|
|
|
|
* This function will block if some conflicting card is already locking one of
|
|
|
|
* the required resources (or any resource on a different bus segment, since P2P
|
|
|
|
* bridges don't differentiate VGA memory and IO afaik). You can indicate
|
|
|
|
* whether this blocking should be interruptible by a signal (for userland
|
|
|
|
* interface) or not.
|
|
|
|
*
|
|
|
|
* Must not be called at interrupt time or in atomic context. If the card
|
|
|
|
* already owns the resources, the function succeeds. Nested calls are
|
|
|
|
* supported (a per-resource counter is maintained)
|
|
|
|
*
|
|
|
|
* On success, release the VGA resource again with vga_put().
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
*
|
|
|
|
* 0 on success, negative error code on failure.
|
|
|
|
*/
|
2009-08-11 05:52:06 +00:00
|
|
|
int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
|
|
|
|
{
|
|
|
|
struct vga_device *vgadev, *conflict;
|
|
|
|
unsigned long flags;
|
2017-06-20 10:06:13 +00:00
|
|
|
wait_queue_entry_t wait;
|
2009-08-11 05:52:06 +00:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
vga_check_first_use();
|
2023-08-08 22:34:07 +00:00
|
|
|
/* The caller should check for this, but let's be sure */
|
2009-08-11 05:52:06 +00:00
|
|
|
if (pdev == NULL)
|
|
|
|
pdev = vga_default_device();
|
|
|
|
if (pdev == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
spin_lock_irqsave(&vga_lock, flags);
|
|
|
|
vgadev = vgadev_find(pdev);
|
|
|
|
if (vgadev == NULL) {
|
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
|
|
|
rc = -ENODEV;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
conflict = __vga_tryget(vgadev, rsrc);
|
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
|
|
|
if (conflict == NULL)
|
|
|
|
break;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* We have a conflict; we wait until somebody kicks the
|
2009-08-11 05:52:06 +00:00
|
|
|
* work queue. Currently we have one work queue that we
|
|
|
|
* kick each time some resources are released, but it would
|
2023-08-08 22:34:07 +00:00
|
|
|
* be fairly easy to have a per-device one so that we only
|
|
|
|
* need to attach to the conflicting device.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
init_waitqueue_entry(&wait, current);
|
|
|
|
add_wait_queue(&vga_wait_queue, &wait);
|
|
|
|
set_current_state(interruptible ?
|
|
|
|
TASK_INTERRUPTIBLE :
|
|
|
|
TASK_UNINTERRUPTIBLE);
|
2015-11-30 02:17:31 +00:00
|
|
|
if (interruptible && signal_pending(current)) {
|
|
|
|
__set_current_state(TASK_RUNNING);
|
|
|
|
remove_wait_queue(&vga_wait_queue, &wait);
|
|
|
|
rc = -ERESTARTSYS;
|
2009-08-11 05:52:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
schedule();
|
|
|
|
remove_wait_queue(&vga_wait_queue, &wait);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_get);
|
|
|
|
|
2016-08-12 20:48:56 +00:00
|
|
|
/**
|
|
|
|
* vga_tryget - try to acquire & lock legacy VGA resources
|
2023-08-08 22:34:07 +00:00
|
|
|
* @pdev: PCI device of VGA card or NULL for system default
|
2016-08-12 20:48:56 +00:00
|
|
|
* @rsrc: bit mask of resources to acquire and lock
|
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* Perform the same operation as vga_get(), but return an error (-EBUSY)
|
|
|
|
* instead of blocking if the resources are already locked by another card.
|
|
|
|
* Can be called in any context.
|
2016-08-12 20:48:56 +00:00
|
|
|
*
|
|
|
|
* On success, release the VGA resource again with vga_put().
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
*
|
|
|
|
* 0 on success, negative error code on failure.
|
|
|
|
*/
|
2020-08-01 06:17:13 +00:00
|
|
|
static int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
|
2009-08-11 05:52:06 +00:00
|
|
|
{
|
|
|
|
struct vga_device *vgadev;
|
|
|
|
unsigned long flags;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
vga_check_first_use();
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* The caller should check for this, but let's be sure */
|
2009-08-11 05:52:06 +00:00
|
|
|
if (pdev == NULL)
|
|
|
|
pdev = vga_default_device();
|
|
|
|
if (pdev == NULL)
|
|
|
|
return 0;
|
|
|
|
spin_lock_irqsave(&vga_lock, flags);
|
|
|
|
vgadev = vgadev_find(pdev);
|
|
|
|
if (vgadev == NULL) {
|
|
|
|
rc = -ENODEV;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
if (__vga_tryget(vgadev, rsrc))
|
|
|
|
rc = -EBUSY;
|
|
|
|
bail:
|
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-08-12 20:48:56 +00:00
|
|
|
/**
|
|
|
|
* vga_put - release lock on legacy VGA resources
|
2023-08-08 22:34:07 +00:00
|
|
|
* @pdev: PCI device of VGA card or NULL for system default
|
|
|
|
* @rsrc: bit mask of resource to release
|
2016-08-12 20:48:56 +00:00
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* Release resources previously locked by vga_get() or vga_tryget(). The
|
|
|
|
* resources aren't disabled right away, so that a subsequent vga_get() on
|
|
|
|
* the same card will succeed immediately. Resources have a counter, so
|
|
|
|
* locks are only released if the counter reaches 0.
|
2016-08-12 20:48:56 +00:00
|
|
|
*/
|
2009-08-11 05:52:06 +00:00
|
|
|
void vga_put(struct pci_dev *pdev, unsigned int rsrc)
|
|
|
|
{
|
|
|
|
struct vga_device *vgadev;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* The caller should check for this, but let's be sure */
|
2009-08-11 05:52:06 +00:00
|
|
|
if (pdev == NULL)
|
|
|
|
pdev = vga_default_device();
|
|
|
|
if (pdev == NULL)
|
|
|
|
return;
|
|
|
|
spin_lock_irqsave(&vga_lock, flags);
|
|
|
|
vgadev = vgadev_find(pdev);
|
|
|
|
if (vgadev == NULL)
|
|
|
|
goto bail;
|
|
|
|
__vga_put(vgadev, rsrc);
|
|
|
|
bail:
|
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_put);
|
|
|
|
|
PCI/VGA: Move firmware default device detection to ADD_DEVICE path
Previously we selected the firmware default device, i.e., one that owns the
boot framebuffer, as the default device in vga_arb_select_default_device().
This was only done in the vga_arb_device_init() subsys_initcall, so devices
enumerated later, e.g., by pcibios_init(), were not eligible.
Fix this by moving the firmware default device selection from
vga_arb_select_default_device() to vga_arbiter_add_pci_device(), which is
called after every PCI device is enumerated, either by the
vga_arb_device_init() subsys_initcall or as an ADD_DEVICE notifier.
Note that if vga_arb_select_default_device() previously found a device
owning the boot framebuffer, it unconditionally set it to be the default
VGA device, and no subsequent device could replace it.
[bhelgaas: commit log, restructure slightly]
Link: https://lore.kernel.org/r/20211015061512.2941859-7-chenhuacai@loongson.cn
Link: https://lore.kernel.org/r/20220224224753.297579-6-helgaas@kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Cc: Bruno Prémont <bonbons@linux-vserver.org>
2022-02-24 22:47:47 +00:00
|
|
|
static bool vga_is_firmware_default(struct pci_dev *pdev)
|
2022-02-24 22:47:45 +00:00
|
|
|
{
|
arch: Remove Itanium (IA-64) architecture
The Itanium architecture is obsolete, and an informal survey [0] reveals
that any residual use of Itanium hardware in production is mostly HP-UX
or OpenVMS based. The use of Linux on Itanium appears to be limited to
enthusiasts that occasionally boot a fresh Linux kernel to see whether
things are still working as intended, and perhaps to churn out some
distro packages that are rarely used in practice.
None of the original companies behind Itanium still produce or support
any hardware or software for the architecture, and it is listed as
'Orphaned' in the MAINTAINERS file, as apparently, none of the engineers
that contributed on behalf of those companies (nor anyone else, for that
matter) have been willing to support or maintain the architecture
upstream or even be responsible for applying the odd fix. The Intel
firmware team removed all IA-64 support from the Tianocore/EDK2
reference implementation of EFI in 2018. (Itanium is the original
architecture for which EFI was developed, and the way Linux supports it
deviates significantly from other architectures.) Some distros, such as
Debian and Gentoo, still maintain [unofficial] ia64 ports, but many have
dropped support years ago.
While the argument is being made [1] that there is a 'for the common
good' angle to being able to build and run existing projects such as the
Grid Community Toolkit [2] on Itanium for interoperability testing, the
fact remains that none of those projects are known to be deployed on
Linux/ia64, and very few people actually have access to such a system in
the first place. Even if there were ways imaginable in which Linux/ia64
could be put to good use today, what matters is whether anyone is
actually doing that, and this does not appear to be the case.
There are no emulators widely available, and so boot testing Itanium is
generally infeasible for ordinary contributors. GCC still supports IA-64
but its compile farm [3] no longer has any IA-64 machines. GLIBC would
like to get rid of IA-64 [4] too because it would permit some overdue
code cleanups. In summary, the benefits to the ecosystem of having IA-64
be part of it are mostly theoretical, whereas the maintenance overhead
of keeping it supported is real.
So let's rip off the band aid, and remove the IA-64 arch code entirely.
This follows the timeline proposed by the Debian/ia64 maintainer [5],
which removes support in a controlled manner, leaving IA-64 in a known
good state in the most recent LTS release. Other projects will follow
once the kernel support is removed.
[0] https://lore.kernel.org/all/CAMj1kXFCMh_578jniKpUtx_j8ByHnt=s7S+yQ+vGbKt9ud7+kQ@mail.gmail.com/
[1] https://lore.kernel.org/all/0075883c-7c51-00f5-2c2d-5119c1820410@web.de/
[2] https://gridcf.org/gct-docs/latest/index.html
[3] https://cfarm.tetaneutral.net/machines/list/
[4] https://lore.kernel.org/all/87bkiilpc4.fsf@mid.deneb.enyo.de/
[5] https://lore.kernel.org/all/ff58a3e76e5102c94bb5946d99187b358def688a.camel@physik.fu-berlin.de/
Acked-by: Tony Luck <tony.luck@intel.com>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2022-10-20 13:54:33 +00:00
|
|
|
#if defined(CONFIG_X86)
|
2022-02-24 22:47:45 +00:00
|
|
|
u64 base = screen_info.lfb_base;
|
|
|
|
u64 size = screen_info.lfb_size;
|
2023-03-30 16:24:30 +00:00
|
|
|
struct resource *r;
|
2022-02-24 22:47:45 +00:00
|
|
|
u64 limit;
|
|
|
|
|
|
|
|
/* Select the device owning the boot framebuffer if there is one */
|
|
|
|
|
|
|
|
if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
|
|
|
|
base |= (u64)screen_info.ext_lfb_base << 32;
|
|
|
|
|
|
|
|
limit = base + size;
|
|
|
|
|
|
|
|
/* Does firmware framebuffer belong to us? */
|
2023-03-30 16:24:30 +00:00
|
|
|
pci_dev_for_each_resource(pdev, r) {
|
|
|
|
if (resource_type(r) != IORESOURCE_MEM)
|
2022-02-24 22:47:45 +00:00
|
|
|
continue;
|
|
|
|
|
2023-03-30 16:24:30 +00:00
|
|
|
if (!r->start || !r->end)
|
2022-02-24 22:47:45 +00:00
|
|
|
continue;
|
|
|
|
|
2023-03-30 16:24:30 +00:00
|
|
|
if (base < r->start || limit >= r->end)
|
2022-02-24 22:47:45 +00:00
|
|
|
continue;
|
|
|
|
|
PCI/VGA: Move firmware default device detection to ADD_DEVICE path
Previously we selected the firmware default device, i.e., one that owns the
boot framebuffer, as the default device in vga_arb_select_default_device().
This was only done in the vga_arb_device_init() subsys_initcall, so devices
enumerated later, e.g., by pcibios_init(), were not eligible.
Fix this by moving the firmware default device selection from
vga_arb_select_default_device() to vga_arbiter_add_pci_device(), which is
called after every PCI device is enumerated, either by the
vga_arb_device_init() subsys_initcall or as an ADD_DEVICE notifier.
Note that if vga_arb_select_default_device() previously found a device
owning the boot framebuffer, it unconditionally set it to be the default
VGA device, and no subsequent device could replace it.
[bhelgaas: commit log, restructure slightly]
Link: https://lore.kernel.org/r/20211015061512.2941859-7-chenhuacai@loongson.cn
Link: https://lore.kernel.org/r/20220224224753.297579-6-helgaas@kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Cc: Bruno Prémont <bonbons@linux-vserver.org>
2022-02-24 22:47:47 +00:00
|
|
|
return true;
|
2022-02-24 22:47:45 +00:00
|
|
|
}
|
|
|
|
#endif
|
PCI/VGA: Move firmware default device detection to ADD_DEVICE path
Previously we selected the firmware default device, i.e., one that owns the
boot framebuffer, as the default device in vga_arb_select_default_device().
This was only done in the vga_arb_device_init() subsys_initcall, so devices
enumerated later, e.g., by pcibios_init(), were not eligible.
Fix this by moving the firmware default device selection from
vga_arb_select_default_device() to vga_arbiter_add_pci_device(), which is
called after every PCI device is enumerated, either by the
vga_arb_device_init() subsys_initcall or as an ADD_DEVICE notifier.
Note that if vga_arb_select_default_device() previously found a device
owning the boot framebuffer, it unconditionally set it to be the default
VGA device, and no subsequent device could replace it.
[bhelgaas: commit log, restructure slightly]
Link: https://lore.kernel.org/r/20211015061512.2941859-7-chenhuacai@loongson.cn
Link: https://lore.kernel.org/r/20220224224753.297579-6-helgaas@kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Cc: Bruno Prémont <bonbons@linux-vserver.org>
2022-02-24 22:47:47 +00:00
|
|
|
return false;
|
2022-02-24 22:47:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 22:47:44 +00:00
|
|
|
static bool vga_arb_integrated_gpu(struct device *dev)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_ACPI)
|
|
|
|
struct acpi_device *adev = ACPI_COMPANION(dev);
|
|
|
|
|
|
|
|
return adev && !strcmp(acpi_device_hid(adev), ACPI_VIDEO_HID);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
PCI/VGA: Factor out default VGA device selection
Default VGA device selection fails when PCI devices are enumerated after
the vga_arb_device_init() subsys_initcall.
vga_arbiter_add_pci_device() selects the first fully enabled device to
which legacy VGA resources are routed as the default VGA device. This is
an ADD_DEVICE notifier, so it runs after every PCI device is enumerated.
vga_arb_select_default_device() may select framebuffer devices, partially
enabled GPUs, or non-legacy devices that don't have legacy VGA resources
routed to them as the default VGA device. But this only happens once, from
the vga_arb_device_init() subsys_initcall, so it doesn't consider devices
enumerated after that:
acpi_init
acpi_scan_init
acpi_pci_root_init # PCI device enumeration (ACPI systems)
vga_arb_device_init
for_each_pci_device
vga_arbiter_add_pci_device # ADD_DEVICE notifier
if (VGA-owner)
vga_set_default_device <-- set default VGA
vga_arb_select_default_device # only called ONCE
for_each_vga_device
if (framebuffer)
vga_set_default_device <-- set default VGA to framebuffer
if (!vga_default_device())
if (non-legacy, integrated GPU, etc)
vga_set_default_device <-- set default VGA
if (!vga_default_device())
vga_set_default_device <-- set default VGA
pcibios_init
pcibios_scanbus # PCI device enumeration (non-ACPI systems)
...
vga_arbiter_add_pci_device # ADD_DEVICE notification
if (VGA-owner)
vga_set_default_device <-- set default VGA
Note that on non-ACPI systems, vga_arb_select_default_device() runs before
pcibios_init(), so it sees no VGA devices and can never set a framebuffer
device, a non-legacy integrated GPU, etc., as the default device.
Factor out the default VGA device selection to vga_is_boot_device(), called
from vga_arbiter_add_pci_device().
Then we can migrate the default device selection from
vga_arb_select_default_device() to the vga_arbiter_add_pci_device() path.
[bhelgaas: commit log, split to separate patch]
Link: https://lore.kernel.org/r/20211015061512.2941859-4-chenhuacai@loongson.cn
Link: https://lore.kernel.org/r/20220224224753.297579-5-helgaas@kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
2022-02-24 22:47:46 +00:00
|
|
|
/*
|
|
|
|
* Return true if vgadev is a better default VGA device than the best one
|
|
|
|
* we've seen so far.
|
|
|
|
*/
|
|
|
|
static bool vga_is_boot_device(struct vga_device *vgadev)
|
|
|
|
{
|
|
|
|
struct vga_device *boot_vga = vgadev_find(vga_default_device());
|
PCI/VGA: Move firmware default device detection to ADD_DEVICE path
Previously we selected the firmware default device, i.e., one that owns the
boot framebuffer, as the default device in vga_arb_select_default_device().
This was only done in the vga_arb_device_init() subsys_initcall, so devices
enumerated later, e.g., by pcibios_init(), were not eligible.
Fix this by moving the firmware default device selection from
vga_arb_select_default_device() to vga_arbiter_add_pci_device(), which is
called after every PCI device is enumerated, either by the
vga_arb_device_init() subsys_initcall or as an ADD_DEVICE notifier.
Note that if vga_arb_select_default_device() previously found a device
owning the boot framebuffer, it unconditionally set it to be the default
VGA device, and no subsequent device could replace it.
[bhelgaas: commit log, restructure slightly]
Link: https://lore.kernel.org/r/20211015061512.2941859-7-chenhuacai@loongson.cn
Link: https://lore.kernel.org/r/20220224224753.297579-6-helgaas@kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Cc: Bruno Prémont <bonbons@linux-vserver.org>
2022-02-24 22:47:47 +00:00
|
|
|
struct pci_dev *pdev = vgadev->pdev;
|
2022-02-24 22:47:48 +00:00
|
|
|
u16 cmd, boot_cmd;
|
PCI/VGA: Factor out default VGA device selection
Default VGA device selection fails when PCI devices are enumerated after
the vga_arb_device_init() subsys_initcall.
vga_arbiter_add_pci_device() selects the first fully enabled device to
which legacy VGA resources are routed as the default VGA device. This is
an ADD_DEVICE notifier, so it runs after every PCI device is enumerated.
vga_arb_select_default_device() may select framebuffer devices, partially
enabled GPUs, or non-legacy devices that don't have legacy VGA resources
routed to them as the default VGA device. But this only happens once, from
the vga_arb_device_init() subsys_initcall, so it doesn't consider devices
enumerated after that:
acpi_init
acpi_scan_init
acpi_pci_root_init # PCI device enumeration (ACPI systems)
vga_arb_device_init
for_each_pci_device
vga_arbiter_add_pci_device # ADD_DEVICE notifier
if (VGA-owner)
vga_set_default_device <-- set default VGA
vga_arb_select_default_device # only called ONCE
for_each_vga_device
if (framebuffer)
vga_set_default_device <-- set default VGA to framebuffer
if (!vga_default_device())
if (non-legacy, integrated GPU, etc)
vga_set_default_device <-- set default VGA
if (!vga_default_device())
vga_set_default_device <-- set default VGA
pcibios_init
pcibios_scanbus # PCI device enumeration (non-ACPI systems)
...
vga_arbiter_add_pci_device # ADD_DEVICE notification
if (VGA-owner)
vga_set_default_device <-- set default VGA
Note that on non-ACPI systems, vga_arb_select_default_device() runs before
pcibios_init(), so it sees no VGA devices and can never set a framebuffer
device, a non-legacy integrated GPU, etc., as the default device.
Factor out the default VGA device selection to vga_is_boot_device(), called
from vga_arbiter_add_pci_device().
Then we can migrate the default device selection from
vga_arb_select_default_device() to the vga_arbiter_add_pci_device() path.
[bhelgaas: commit log, split to separate patch]
Link: https://lore.kernel.org/r/20211015061512.2941859-4-chenhuacai@loongson.cn
Link: https://lore.kernel.org/r/20220224224753.297579-5-helgaas@kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
2022-02-24 22:47:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We select the default VGA device in this order:
|
|
|
|
* Firmware framebuffer (see vga_arb_select_default_device())
|
|
|
|
* Legacy VGA device (owns VGA_RSRC_LEGACY_MASK)
|
|
|
|
* Non-legacy integrated device (see vga_arb_select_default_device())
|
|
|
|
* Non-legacy discrete device (see vga_arb_select_default_device())
|
|
|
|
* Other device (see vga_arb_select_default_device())
|
|
|
|
*/
|
|
|
|
|
PCI/VGA: Move firmware default device detection to ADD_DEVICE path
Previously we selected the firmware default device, i.e., one that owns the
boot framebuffer, as the default device in vga_arb_select_default_device().
This was only done in the vga_arb_device_init() subsys_initcall, so devices
enumerated later, e.g., by pcibios_init(), were not eligible.
Fix this by moving the firmware default device selection from
vga_arb_select_default_device() to vga_arbiter_add_pci_device(), which is
called after every PCI device is enumerated, either by the
vga_arb_device_init() subsys_initcall or as an ADD_DEVICE notifier.
Note that if vga_arb_select_default_device() previously found a device
owning the boot framebuffer, it unconditionally set it to be the default
VGA device, and no subsequent device could replace it.
[bhelgaas: commit log, restructure slightly]
Link: https://lore.kernel.org/r/20211015061512.2941859-7-chenhuacai@loongson.cn
Link: https://lore.kernel.org/r/20220224224753.297579-6-helgaas@kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Cc: Bruno Prémont <bonbons@linux-vserver.org>
2022-02-24 22:47:47 +00:00
|
|
|
/*
|
|
|
|
* We always prefer a firmware default device, so if we've already
|
|
|
|
* found one, there's no need to consider vgadev.
|
|
|
|
*/
|
|
|
|
if (boot_vga && boot_vga->is_firmware_default)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (vga_is_firmware_default(pdev)) {
|
|
|
|
vgadev->is_firmware_default = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
PCI/VGA: Factor out default VGA device selection
Default VGA device selection fails when PCI devices are enumerated after
the vga_arb_device_init() subsys_initcall.
vga_arbiter_add_pci_device() selects the first fully enabled device to
which legacy VGA resources are routed as the default VGA device. This is
an ADD_DEVICE notifier, so it runs after every PCI device is enumerated.
vga_arb_select_default_device() may select framebuffer devices, partially
enabled GPUs, or non-legacy devices that don't have legacy VGA resources
routed to them as the default VGA device. But this only happens once, from
the vga_arb_device_init() subsys_initcall, so it doesn't consider devices
enumerated after that:
acpi_init
acpi_scan_init
acpi_pci_root_init # PCI device enumeration (ACPI systems)
vga_arb_device_init
for_each_pci_device
vga_arbiter_add_pci_device # ADD_DEVICE notifier
if (VGA-owner)
vga_set_default_device <-- set default VGA
vga_arb_select_default_device # only called ONCE
for_each_vga_device
if (framebuffer)
vga_set_default_device <-- set default VGA to framebuffer
if (!vga_default_device())
if (non-legacy, integrated GPU, etc)
vga_set_default_device <-- set default VGA
if (!vga_default_device())
vga_set_default_device <-- set default VGA
pcibios_init
pcibios_scanbus # PCI device enumeration (non-ACPI systems)
...
vga_arbiter_add_pci_device # ADD_DEVICE notification
if (VGA-owner)
vga_set_default_device <-- set default VGA
Note that on non-ACPI systems, vga_arb_select_default_device() runs before
pcibios_init(), so it sees no VGA devices and can never set a framebuffer
device, a non-legacy integrated GPU, etc., as the default device.
Factor out the default VGA device selection to vga_is_boot_device(), called
from vga_arbiter_add_pci_device().
Then we can migrate the default device selection from
vga_arb_select_default_device() to the vga_arbiter_add_pci_device() path.
[bhelgaas: commit log, split to separate patch]
Link: https://lore.kernel.org/r/20211015061512.2941859-4-chenhuacai@loongson.cn
Link: https://lore.kernel.org/r/20220224224753.297579-5-helgaas@kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
2022-02-24 22:47:46 +00:00
|
|
|
/*
|
|
|
|
* A legacy VGA device has MEM and IO enabled and any bridges
|
|
|
|
* leading to it have PCI_BRIDGE_CTL_VGA enabled so the legacy
|
|
|
|
* resources ([mem 0xa0000-0xbffff], [io 0x3b0-0x3bb], etc) are
|
|
|
|
* routed to it.
|
|
|
|
*
|
|
|
|
* We use the first one we find, so if we've already found one,
|
|
|
|
* vgadev is no better.
|
|
|
|
*/
|
2022-02-24 22:47:49 +00:00
|
|
|
if (boot_vga &&
|
|
|
|
(boot_vga->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)
|
PCI/VGA: Factor out default VGA device selection
Default VGA device selection fails when PCI devices are enumerated after
the vga_arb_device_init() subsys_initcall.
vga_arbiter_add_pci_device() selects the first fully enabled device to
which legacy VGA resources are routed as the default VGA device. This is
an ADD_DEVICE notifier, so it runs after every PCI device is enumerated.
vga_arb_select_default_device() may select framebuffer devices, partially
enabled GPUs, or non-legacy devices that don't have legacy VGA resources
routed to them as the default VGA device. But this only happens once, from
the vga_arb_device_init() subsys_initcall, so it doesn't consider devices
enumerated after that:
acpi_init
acpi_scan_init
acpi_pci_root_init # PCI device enumeration (ACPI systems)
vga_arb_device_init
for_each_pci_device
vga_arbiter_add_pci_device # ADD_DEVICE notifier
if (VGA-owner)
vga_set_default_device <-- set default VGA
vga_arb_select_default_device # only called ONCE
for_each_vga_device
if (framebuffer)
vga_set_default_device <-- set default VGA to framebuffer
if (!vga_default_device())
if (non-legacy, integrated GPU, etc)
vga_set_default_device <-- set default VGA
if (!vga_default_device())
vga_set_default_device <-- set default VGA
pcibios_init
pcibios_scanbus # PCI device enumeration (non-ACPI systems)
...
vga_arbiter_add_pci_device # ADD_DEVICE notification
if (VGA-owner)
vga_set_default_device <-- set default VGA
Note that on non-ACPI systems, vga_arb_select_default_device() runs before
pcibios_init(), so it sees no VGA devices and can never set a framebuffer
device, a non-legacy integrated GPU, etc., as the default device.
Factor out the default VGA device selection to vga_is_boot_device(), called
from vga_arbiter_add_pci_device().
Then we can migrate the default device selection from
vga_arb_select_default_device() to the vga_arbiter_add_pci_device() path.
[bhelgaas: commit log, split to separate patch]
Link: https://lore.kernel.org/r/20211015061512.2941859-4-chenhuacai@loongson.cn
Link: https://lore.kernel.org/r/20220224224753.297579-5-helgaas@kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
2022-02-24 22:47:46 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK)
|
|
|
|
return true;
|
|
|
|
|
2022-02-24 22:47:48 +00:00
|
|
|
/*
|
|
|
|
* If we haven't found a legacy VGA device, accept a non-legacy
|
|
|
|
* device. It may have either IO or MEM enabled, and bridges may
|
|
|
|
* not have PCI_BRIDGE_CTL_VGA enabled, so it may not be able to
|
|
|
|
* use legacy VGA resources. Prefer an integrated GPU over others.
|
|
|
|
*/
|
|
|
|
pci_read_config_word(pdev, PCI_COMMAND, &cmd);
|
|
|
|
if (cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An integrated GPU overrides a previous non-legacy
|
|
|
|
* device. We expect only a single integrated GPU, but if
|
|
|
|
* there are more, we use the *last* because that was the
|
|
|
|
* previous behavior.
|
|
|
|
*/
|
|
|
|
if (vga_arb_integrated_gpu(&pdev->dev))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We prefer the first non-legacy discrete device we find.
|
|
|
|
* If we already found one, vgadev is no better.
|
|
|
|
*/
|
|
|
|
if (boot_vga) {
|
|
|
|
pci_read_config_word(boot_vga->pdev, PCI_COMMAND,
|
|
|
|
&boot_cmd);
|
|
|
|
if (boot_cmd & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-24 22:47:49 +00:00
|
|
|
/*
|
2023-08-08 22:34:07 +00:00
|
|
|
* Vgadev has neither IO nor MEM enabled. If we haven't found any
|
2022-02-24 22:47:49 +00:00
|
|
|
* other VGA devices, it is the best candidate so far.
|
|
|
|
*/
|
|
|
|
if (!boot_vga)
|
|
|
|
return true;
|
|
|
|
|
PCI/VGA: Factor out default VGA device selection
Default VGA device selection fails when PCI devices are enumerated after
the vga_arb_device_init() subsys_initcall.
vga_arbiter_add_pci_device() selects the first fully enabled device to
which legacy VGA resources are routed as the default VGA device. This is
an ADD_DEVICE notifier, so it runs after every PCI device is enumerated.
vga_arb_select_default_device() may select framebuffer devices, partially
enabled GPUs, or non-legacy devices that don't have legacy VGA resources
routed to them as the default VGA device. But this only happens once, from
the vga_arb_device_init() subsys_initcall, so it doesn't consider devices
enumerated after that:
acpi_init
acpi_scan_init
acpi_pci_root_init # PCI device enumeration (ACPI systems)
vga_arb_device_init
for_each_pci_device
vga_arbiter_add_pci_device # ADD_DEVICE notifier
if (VGA-owner)
vga_set_default_device <-- set default VGA
vga_arb_select_default_device # only called ONCE
for_each_vga_device
if (framebuffer)
vga_set_default_device <-- set default VGA to framebuffer
if (!vga_default_device())
if (non-legacy, integrated GPU, etc)
vga_set_default_device <-- set default VGA
if (!vga_default_device())
vga_set_default_device <-- set default VGA
pcibios_init
pcibios_scanbus # PCI device enumeration (non-ACPI systems)
...
vga_arbiter_add_pci_device # ADD_DEVICE notification
if (VGA-owner)
vga_set_default_device <-- set default VGA
Note that on non-ACPI systems, vga_arb_select_default_device() runs before
pcibios_init(), so it sees no VGA devices and can never set a framebuffer
device, a non-legacy integrated GPU, etc., as the default device.
Factor out the default VGA device selection to vga_is_boot_device(), called
from vga_arbiter_add_pci_device().
Then we can migrate the default device selection from
vga_arb_select_default_device() to the vga_arbiter_add_pci_device() path.
[bhelgaas: commit log, split to separate patch]
Link: https://lore.kernel.org/r/20211015061512.2941859-4-chenhuacai@loongson.cn
Link: https://lore.kernel.org/r/20220224224753.297579-5-helgaas@kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
2022-02-24 22:47:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-12 14:32:07 +00:00
|
|
|
/*
|
|
|
|
* Rules for using a bridge to control a VGA descendant decoding: if a bridge
|
|
|
|
* has only one VGA descendant then it can be used to control the VGA routing
|
|
|
|
* for that device. It should always use the bridge closest to the device to
|
|
|
|
* control it. If a bridge has a direct VGA descendant, but also have a sub-
|
|
|
|
* bridge VGA descendant then we cannot use that bridge to control the direct
|
|
|
|
* VGA descendant. So for every device we register, we need to iterate all
|
|
|
|
* its parent bridges so we can invalidate any devices using them properly.
|
|
|
|
*/
|
2010-06-01 05:32:24 +00:00
|
|
|
static void vga_arbiter_check_bridge_sharing(struct vga_device *vgadev)
|
|
|
|
{
|
|
|
|
struct vga_device *same_bridge_vgadev;
|
|
|
|
struct pci_bus *new_bus, *bus;
|
|
|
|
struct pci_dev *new_bridge, *bridge;
|
|
|
|
|
|
|
|
vgadev->bridge_has_one_vga = true;
|
|
|
|
|
2022-02-24 22:47:51 +00:00
|
|
|
if (list_empty(&vga_list)) {
|
|
|
|
vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n");
|
2010-06-01 05:32:24 +00:00
|
|
|
return;
|
2022-02-24 22:47:51 +00:00
|
|
|
}
|
2010-06-01 05:32:24 +00:00
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Iterate the new device's bridge hierarchy */
|
2010-06-01 05:32:24 +00:00
|
|
|
new_bus = vgadev->pdev->bus;
|
|
|
|
while (new_bus) {
|
|
|
|
new_bridge = new_bus->self;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Go through list of devices already registered */
|
2011-10-10 08:29:18 +00:00
|
|
|
list_for_each_entry(same_bridge_vgadev, &vga_list, list) {
|
|
|
|
bus = same_bridge_vgadev->pdev->bus;
|
|
|
|
bridge = bus->self;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* See if it shares a bridge with this device */
|
2011-10-10 08:29:18 +00:00
|
|
|
if (new_bridge == bridge) {
|
2015-08-12 14:32:07 +00:00
|
|
|
/*
|
2023-08-08 22:34:07 +00:00
|
|
|
* If its direct parent bridge is the same
|
2015-08-12 14:32:07 +00:00
|
|
|
* as any bridge of this device then it can't
|
|
|
|
* be used for that device.
|
|
|
|
*/
|
2011-10-10 08:29:18 +00:00
|
|
|
same_bridge_vgadev->bridge_has_one_vga = false;
|
|
|
|
}
|
2010-06-01 05:32:24 +00:00
|
|
|
|
2015-08-12 14:32:07 +00:00
|
|
|
/*
|
2023-08-08 22:34:07 +00:00
|
|
|
* Now iterate the previous device's bridge hierarchy.
|
|
|
|
* If the new device's parent bridge is in the other
|
|
|
|
* device's hierarchy, we can't use it to control this
|
|
|
|
* device.
|
2015-08-12 14:32:07 +00:00
|
|
|
*/
|
2011-10-10 08:29:18 +00:00
|
|
|
while (bus) {
|
|
|
|
bridge = bus->self;
|
2015-08-12 14:32:07 +00:00
|
|
|
|
|
|
|
if (bridge && bridge == vgadev->pdev->bus->self)
|
|
|
|
vgadev->bridge_has_one_vga = false;
|
|
|
|
|
2011-10-10 08:29:18 +00:00
|
|
|
bus = bus->parent;
|
2010-06-01 05:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
new_bus = new_bus->parent;
|
|
|
|
}
|
2022-02-24 22:47:51 +00:00
|
|
|
|
|
|
|
if (vgadev->bridge_has_one_vga)
|
|
|
|
vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n");
|
|
|
|
else
|
|
|
|
vgaarb_info(&vgadev->pdev->dev, "no bridge control possible\n");
|
2010-06-01 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2009-08-11 05:52:06 +00:00
|
|
|
/*
|
2023-08-08 22:34:07 +00:00
|
|
|
* Currently, we assume that the "initial" setup of the system is not sane,
|
|
|
|
* that is, we come up with conflicting devices and let the arbiter's
|
|
|
|
* client decide if devices decodes legacy things or not.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
struct vga_device *vgadev;
|
|
|
|
unsigned long flags;
|
|
|
|
struct pci_bus *bus;
|
|
|
|
struct pci_dev *bridge;
|
|
|
|
u16 cmd;
|
|
|
|
|
|
|
|
/* Allocate structure */
|
2015-09-30 19:57:06 +00:00
|
|
|
vgadev = kzalloc(sizeof(struct vga_device), GFP_KERNEL);
|
2009-08-11 05:52:06 +00:00
|
|
|
if (vgadev == NULL) {
|
2016-11-17 17:47:58 +00:00
|
|
|
vgaarb_err(&pdev->dev, "failed to allocate VGA arbiter data\n");
|
2015-08-12 14:32:07 +00:00
|
|
|
/*
|
2023-08-08 22:34:07 +00:00
|
|
|
* What to do on allocation failure? For now, let's just do
|
2015-08-12 14:32:07 +00:00
|
|
|
* nothing, I'm not sure there is anything saner to be done.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Take lock & check for duplicates */
|
|
|
|
spin_lock_irqsave(&vga_lock, flags);
|
|
|
|
if (vgadev_find(pdev) != NULL) {
|
|
|
|
BUG_ON(1);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
vgadev->pdev = pdev;
|
|
|
|
|
|
|
|
/* By default, assume we decode everything */
|
|
|
|
vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
|
|
|
|
VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* By default, mark it as decoding */
|
2009-08-11 05:52:06 +00:00
|
|
|
vga_decode_count++;
|
2023-08-08 22:34:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark that we "own" resources based on our enables, we will
|
|
|
|
* clear that below if the bridge isn't forwarding.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
pci_read_config_word(pdev, PCI_COMMAND, &cmd);
|
|
|
|
if (cmd & PCI_COMMAND_IO)
|
|
|
|
vgadev->owns |= VGA_RSRC_LEGACY_IO;
|
|
|
|
if (cmd & PCI_COMMAND_MEMORY)
|
|
|
|
vgadev->owns |= VGA_RSRC_LEGACY_MEM;
|
|
|
|
|
|
|
|
/* Check if VGA cycles can get down to us */
|
|
|
|
bus = pdev->bus;
|
|
|
|
while (bus) {
|
|
|
|
bridge = bus->self;
|
|
|
|
if (bridge) {
|
|
|
|
u16 l;
|
2015-08-12 14:32:07 +00:00
|
|
|
|
|
|
|
pci_read_config_word(bridge, PCI_BRIDGE_CONTROL, &l);
|
2009-08-11 05:52:06 +00:00
|
|
|
if (!(l & PCI_BRIDGE_CTL_VGA)) {
|
|
|
|
vgadev->owns = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bus = bus->parent;
|
|
|
|
}
|
|
|
|
|
PCI/VGA: Factor out default VGA device selection
Default VGA device selection fails when PCI devices are enumerated after
the vga_arb_device_init() subsys_initcall.
vga_arbiter_add_pci_device() selects the first fully enabled device to
which legacy VGA resources are routed as the default VGA device. This is
an ADD_DEVICE notifier, so it runs after every PCI device is enumerated.
vga_arb_select_default_device() may select framebuffer devices, partially
enabled GPUs, or non-legacy devices that don't have legacy VGA resources
routed to them as the default VGA device. But this only happens once, from
the vga_arb_device_init() subsys_initcall, so it doesn't consider devices
enumerated after that:
acpi_init
acpi_scan_init
acpi_pci_root_init # PCI device enumeration (ACPI systems)
vga_arb_device_init
for_each_pci_device
vga_arbiter_add_pci_device # ADD_DEVICE notifier
if (VGA-owner)
vga_set_default_device <-- set default VGA
vga_arb_select_default_device # only called ONCE
for_each_vga_device
if (framebuffer)
vga_set_default_device <-- set default VGA to framebuffer
if (!vga_default_device())
if (non-legacy, integrated GPU, etc)
vga_set_default_device <-- set default VGA
if (!vga_default_device())
vga_set_default_device <-- set default VGA
pcibios_init
pcibios_scanbus # PCI device enumeration (non-ACPI systems)
...
vga_arbiter_add_pci_device # ADD_DEVICE notification
if (VGA-owner)
vga_set_default_device <-- set default VGA
Note that on non-ACPI systems, vga_arb_select_default_device() runs before
pcibios_init(), so it sees no VGA devices and can never set a framebuffer
device, a non-legacy integrated GPU, etc., as the default device.
Factor out the default VGA device selection to vga_is_boot_device(), called
from vga_arbiter_add_pci_device().
Then we can migrate the default device selection from
vga_arb_select_default_device() to the vga_arbiter_add_pci_device() path.
[bhelgaas: commit log, split to separate patch]
Link: https://lore.kernel.org/r/20211015061512.2941859-4-chenhuacai@loongson.cn
Link: https://lore.kernel.org/r/20220224224753.297579-5-helgaas@kernel.org
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
2022-02-24 22:47:46 +00:00
|
|
|
if (vga_is_boot_device(vgadev)) {
|
|
|
|
vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n",
|
|
|
|
vga_default_device() ?
|
|
|
|
" (overriding previous)" : "");
|
2012-09-19 18:54:15 +00:00
|
|
|
vga_set_default_device(pdev);
|
2014-08-24 21:09:53 +00:00
|
|
|
}
|
2009-08-11 05:52:06 +00:00
|
|
|
|
2010-06-01 05:32:24 +00:00
|
|
|
vga_arbiter_check_bridge_sharing(vgadev);
|
|
|
|
|
2009-08-11 05:52:06 +00:00
|
|
|
/* Add to the list */
|
2018-08-31 18:20:00 +00:00
|
|
|
list_add_tail(&vgadev->list, &vga_list);
|
2009-08-11 05:52:06 +00:00
|
|
|
vga_count++;
|
2016-11-17 17:47:58 +00:00
|
|
|
vgaarb_info(&pdev->dev, "VGA device added: decodes=%s,owns=%s,locks=%s\n",
|
2009-08-11 05:52:06 +00:00
|
|
|
vga_iostate_to_str(vgadev->decodes),
|
|
|
|
vga_iostate_to_str(vgadev->owns),
|
|
|
|
vga_iostate_to_str(vgadev->locks));
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
|
|
|
return true;
|
|
|
|
fail:
|
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
|
|
|
kfree(vgadev);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
struct vga_device *vgadev;
|
|
|
|
unsigned long flags;
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&vga_lock, flags);
|
|
|
|
vgadev = vgadev_find(pdev);
|
|
|
|
if (vgadev == NULL) {
|
|
|
|
ret = false;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2012-09-19 18:54:15 +00:00
|
|
|
if (vga_default == pdev)
|
|
|
|
vga_set_default_device(NULL);
|
2009-08-11 05:52:06 +00:00
|
|
|
|
|
|
|
if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
|
|
|
|
vga_decode_count--;
|
|
|
|
|
|
|
|
/* Remove entry from list */
|
|
|
|
list_del(&vgadev->list);
|
|
|
|
vga_count--;
|
|
|
|
|
|
|
|
/* Wake up all possible waiters */
|
|
|
|
wake_up_all(&vga_wait_queue);
|
|
|
|
bail:
|
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
|
|
|
kfree(vgadev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Called with the lock */
|
2023-08-08 22:34:05 +00:00
|
|
|
static void vga_update_device_decodes(struct vga_device *vgadev,
|
|
|
|
unsigned int new_decodes)
|
2009-08-11 05:52:06 +00:00
|
|
|
{
|
2016-11-17 17:47:58 +00:00
|
|
|
struct device *dev = &vgadev->pdev->dev;
|
2023-08-08 22:34:05 +00:00
|
|
|
unsigned int old_decodes = vgadev->decodes;
|
|
|
|
unsigned int decodes_removed = ~new_decodes & old_decodes;
|
|
|
|
unsigned int decodes_unlocked = vgadev->locks & decodes_removed;
|
2009-08-11 05:52:06 +00:00
|
|
|
|
|
|
|
vgadev->decodes = new_decodes;
|
|
|
|
|
2023-08-08 22:34:05 +00:00
|
|
|
vgaarb_info(dev, "VGA decodes changed: olddecodes=%s,decodes=%s:owns=%s\n",
|
|
|
|
vga_iostate_to_str(old_decodes),
|
|
|
|
vga_iostate_to_str(vgadev->decodes),
|
|
|
|
vga_iostate_to_str(vgadev->owns));
|
2009-08-11 05:52:06 +00:00
|
|
|
|
2023-08-08 22:34:05 +00:00
|
|
|
/* If we removed locked decodes, lock count goes to zero, and release */
|
vgaarb: Fix VGA decodes changes
When VGA decodes change we need to do a bit more evaluation of exactly what
has changed. We don't necessarily give up all the old owns resources and
we need to account for resources with locks. The new algorithm is: If
something is added, update decodes. If legacy resources were added and
none were there before, we have a new participant. If something is
removed, update decodes. If we previously owned it, we no longer own it.
If it was previously locked, invalidate all locks and release it. If
legacy resources were removed and none are left, remove the participant
from VGA arbitration.
Previously we updated decodes, released ownership of everything that was
previously decoded, ignored all locks, and went off looking for another
device to transfer VGA to. In a test case where Intel IGD removes only
legacy VGA memory decoding, this left the arbiter switching to discrete
graphics without actually disabling legacy VGA IO from the IGD. As a
bonus, we bumped up the count of VGA arbitration participants for no
good reason.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Cc: Dave Airlie <airlied@redhat.com>
Acked-by: Dave Airlie <airlied@redhat.com>
Reviewed-by: Ville Syrjälä <ville.syrjala@linux.intel.com>
[danvet: Kill now unused variables, reported by the 0-day kernel
builtbot.]
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
2013-08-15 22:37:59 +00:00
|
|
|
if (decodes_unlocked) {
|
|
|
|
if (decodes_unlocked & VGA_RSRC_LEGACY_IO)
|
|
|
|
vgadev->io_lock_cnt = 0;
|
|
|
|
if (decodes_unlocked & VGA_RSRC_LEGACY_MEM)
|
|
|
|
vgadev->mem_lock_cnt = 0;
|
|
|
|
__vga_put(vgadev, decodes_unlocked);
|
2009-08-11 05:52:06 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Change decodes counter */
|
vgaarb: Fix VGA decodes changes
When VGA decodes change we need to do a bit more evaluation of exactly what
has changed. We don't necessarily give up all the old owns resources and
we need to account for resources with locks. The new algorithm is: If
something is added, update decodes. If legacy resources were added and
none were there before, we have a new participant. If something is
removed, update decodes. If we previously owned it, we no longer own it.
If it was previously locked, invalidate all locks and release it. If
legacy resources were removed and none are left, remove the participant
from VGA arbitration.
Previously we updated decodes, released ownership of everything that was
previously decoded, ignored all locks, and went off looking for another
device to transfer VGA to. In a test case where Intel IGD removes only
legacy VGA memory decoding, this left the arbiter switching to discrete
graphics without actually disabling legacy VGA IO from the IGD. As a
bonus, we bumped up the count of VGA arbitration participants for no
good reason.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Cc: Dave Airlie <airlied@redhat.com>
Acked-by: Dave Airlie <airlied@redhat.com>
Reviewed-by: Ville Syrjälä <ville.syrjala@linux.intel.com>
[danvet: Kill now unused variables, reported by the 0-day kernel
builtbot.]
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
2013-08-15 22:37:59 +00:00
|
|
|
if (old_decodes & VGA_RSRC_LEGACY_MASK &&
|
|
|
|
!(new_decodes & VGA_RSRC_LEGACY_MASK))
|
|
|
|
vga_decode_count--;
|
|
|
|
if (!(old_decodes & VGA_RSRC_LEGACY_MASK) &&
|
|
|
|
new_decodes & VGA_RSRC_LEGACY_MASK)
|
|
|
|
vga_decode_count++;
|
2016-11-17 17:47:58 +00:00
|
|
|
vgaarb_dbg(dev, "decoding count now is: %d\n", vga_decode_count);
|
2009-08-11 05:52:06 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 14:32:07 +00:00
|
|
|
static void __vga_set_legacy_decoding(struct pci_dev *pdev,
|
|
|
|
unsigned int decodes,
|
|
|
|
bool userspace)
|
2009-08-11 05:52:06 +00:00
|
|
|
{
|
|
|
|
struct vga_device *vgadev;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
decodes &= VGA_RSRC_LEGACY_MASK;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&vga_lock, flags);
|
|
|
|
vgadev = vgadev_find(pdev);
|
|
|
|
if (vgadev == NULL)
|
|
|
|
goto bail;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Don't let userspace futz with kernel driver decodes */
|
2021-07-16 06:16:34 +00:00
|
|
|
if (userspace && vgadev->set_decode)
|
2009-08-11 05:52:06 +00:00
|
|
|
goto bail;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Update the device decodes + counter */
|
2009-08-11 05:52:06 +00:00
|
|
|
vga_update_device_decodes(vgadev, decodes);
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* XXX If somebody is going from "doesn't decode" to "decodes"
|
|
|
|
* state here, additional care must be taken as we may have pending
|
|
|
|
* ownership of non-legacy region.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
bail:
|
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
|
|
|
}
|
|
|
|
|
2021-07-16 06:16:30 +00:00
|
|
|
/**
|
|
|
|
* vga_set_legacy_decoding
|
2023-08-08 22:34:07 +00:00
|
|
|
* @pdev: PCI device of the VGA card
|
2021-07-16 06:16:30 +00:00
|
|
|
* @decodes: bit mask of what legacy regions the card decodes
|
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* Indicate to the arbiter if the card decodes legacy VGA IOs, legacy VGA
|
2021-07-16 06:16:30 +00:00
|
|
|
* Memory, both, or none. All cards default to both, the card driver (fbdev for
|
|
|
|
* example) should tell the arbiter if it has disabled legacy decoding, so the
|
|
|
|
* card can be left out of the arbitration process (and can be safe to take
|
|
|
|
* interrupts at any time.
|
|
|
|
*/
|
2009-08-11 05:52:06 +00:00
|
|
|
void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
|
|
|
|
{
|
|
|
|
__vga_set_legacy_decoding(pdev, decodes, false);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_set_legacy_decoding);
|
|
|
|
|
2016-08-12 20:48:56 +00:00
|
|
|
/**
|
|
|
|
* vga_client_register - register or unregister a VGA arbitration client
|
2023-08-08 22:34:07 +00:00
|
|
|
* @pdev: PCI device of the VGA client
|
|
|
|
* @set_decode: VGA decode change callback
|
2016-08-12 20:48:56 +00:00
|
|
|
*
|
|
|
|
* Clients have two callback mechanisms they can use.
|
|
|
|
*
|
2021-07-16 06:16:34 +00:00
|
|
|
* @set_decode callback: If a client can disable its GPU VGA resource, it
|
2016-08-12 20:48:56 +00:00
|
|
|
* will get a callback from this to set the encode/decode state.
|
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* Rationale: we cannot disable VGA decode resources unconditionally
|
|
|
|
* because some single GPU laptops seem to require ACPI or BIOS access to
|
|
|
|
* the VGA registers to control things like backlights etc. Hopefully newer
|
|
|
|
* multi-GPU laptops do something saner, and desktops won't have any
|
|
|
|
* special ACPI for this. The driver will get a callback when VGA
|
|
|
|
* arbitration is first used by userspace since some older X servers have
|
|
|
|
* issues.
|
2016-08-12 20:48:56 +00:00
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* Does not check whether a client for @pdev has been registered already.
|
2016-08-12 20:48:56 +00:00
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* To unregister, call vga_client_unregister().
|
2016-08-12 20:48:56 +00:00
|
|
|
*
|
2023-08-08 22:34:08 +00:00
|
|
|
* Returns: 0 on success, -ENODEV on failure
|
2016-08-12 20:48:56 +00:00
|
|
|
*/
|
2021-07-16 06:16:34 +00:00
|
|
|
int vga_client_register(struct pci_dev *pdev,
|
|
|
|
unsigned int (*set_decode)(struct pci_dev *pdev, bool decode))
|
2009-08-11 05:52:06 +00:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
2023-08-08 22:34:08 +00:00
|
|
|
struct vga_device *vgadev;
|
2009-08-11 05:52:06 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&vga_lock, flags);
|
|
|
|
vgadev = vgadev_find(pdev);
|
2023-08-08 22:34:08 +00:00
|
|
|
if (vgadev)
|
|
|
|
vgadev->set_decode = set_decode;
|
2009-08-11 05:52:06 +00:00
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
2023-08-08 22:34:08 +00:00
|
|
|
if (!vgadev)
|
|
|
|
return -ENODEV;
|
|
|
|
return 0;
|
2009-08-11 05:52:06 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_client_register);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Char driver implementation
|
|
|
|
*
|
|
|
|
* Semantics is:
|
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* open : Open user instance of the arbiter. By default, it's
|
2009-08-11 05:52:06 +00:00
|
|
|
* attached to the default VGA device of the system.
|
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* close : Close user instance, release locks
|
2009-08-11 05:52:06 +00:00
|
|
|
*
|
2023-08-08 22:34:07 +00:00
|
|
|
* read : Return a string indicating the status of the target.
|
|
|
|
* An IO state string is of the form {io,mem,io+mem,none},
|
2009-08-11 05:52:06 +00:00
|
|
|
* mc and ic are respectively mem and io lock counts (for
|
|
|
|
* debugging/diagnostic only). "decodes" indicate what the
|
|
|
|
* card currently decodes, "owns" indicates what is currently
|
|
|
|
* enabled on it, and "locks" indicates what is locked by this
|
|
|
|
* card. If the card is unplugged, we get "invalid" then for
|
|
|
|
* card_ID and an -ENODEV error is returned for any command
|
|
|
|
* until a new card is targeted
|
|
|
|
*
|
|
|
|
* "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
|
|
|
|
*
|
|
|
|
* write : write a command to the arbiter. List of commands is:
|
|
|
|
*
|
|
|
|
* target <card_ID> : switch target to card <card_ID> (see below)
|
2023-08-08 22:34:07 +00:00
|
|
|
* lock <io_state> : acquire locks on target ("none" is invalid io_state)
|
2009-08-11 05:52:06 +00:00
|
|
|
* trylock <io_state> : non-blocking acquire locks on target
|
|
|
|
* unlock <io_state> : release locks on target
|
|
|
|
* unlock all : release all locks on target held by this user
|
|
|
|
* decodes <io_state> : set the legacy decoding attributes for the card
|
|
|
|
*
|
|
|
|
* poll : event if something change on any card (not just the target)
|
|
|
|
*
|
|
|
|
* card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
|
|
|
|
* to go back to the system default card (TODO: not implemented yet).
|
|
|
|
* Currently, only PCI is supported as a prefix, but the userland API may
|
|
|
|
* support other bus types in the future, even if the current kernel
|
|
|
|
* implementation doesn't.
|
|
|
|
*
|
|
|
|
* Note about locks:
|
|
|
|
*
|
|
|
|
* The driver keeps track of which user has what locks on which card. It
|
2023-08-08 22:34:07 +00:00
|
|
|
* supports stacking, like the kernel one. This complicates the implementation
|
2009-08-11 05:52:06 +00:00
|
|
|
* a bit, but makes the arbiter more tolerant to userspace problems and able
|
|
|
|
* to properly cleanup in all cases when a process dies.
|
|
|
|
* Currently, a max of 16 cards simultaneously can have locks issued from
|
|
|
|
* userspace for a given user (file descriptor instance) of the arbiter.
|
|
|
|
*
|
|
|
|
* If the device is hot-unplugged, there is a hook inside the module to notify
|
2023-08-08 22:34:07 +00:00
|
|
|
* it being added/removed in the system and automatically added/removed in
|
2009-08-11 05:52:06 +00:00
|
|
|
* the arbiter.
|
|
|
|
*/
|
|
|
|
|
2010-02-03 01:45:01 +00:00
|
|
|
#define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
|
2009-08-11 05:52:06 +00:00
|
|
|
#define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Each user has an array of these, tracking which cards have locks */
|
2009-08-11 05:52:06 +00:00
|
|
|
struct vga_arb_user_card {
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
unsigned int mem_cnt;
|
|
|
|
unsigned int io_cnt;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct vga_arb_private {
|
|
|
|
struct list_head list;
|
|
|
|
struct pci_dev *target;
|
|
|
|
struct vga_arb_user_card cards[MAX_USER_CARDS];
|
|
|
|
spinlock_t lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(vga_user_list);
|
|
|
|
static DEFINE_SPINLOCK(vga_user_lock);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2023-08-08 22:34:07 +00:00
|
|
|
* Take a string in the format: "PCI:domain:bus:dev.fn" and return the
|
|
|
|
* respective values. If the string is not in this format, return 0.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
|
|
|
|
unsigned int *bus, unsigned int *devfn)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
unsigned int slot, func;
|
|
|
|
|
|
|
|
n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
|
|
|
|
if (n != 4)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*devfn = PCI_DEVFN(slot, func);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-08-12 14:32:07 +00:00
|
|
|
static ssize_t vga_arb_read(struct file *file, char __user *buf,
|
2009-08-11 05:52:06 +00:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct vga_arb_private *priv = file->private_data;
|
|
|
|
struct vga_device *vgadev;
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
unsigned long flags;
|
|
|
|
size_t len;
|
|
|
|
int rc;
|
|
|
|
char *lbuf;
|
|
|
|
|
|
|
|
lbuf = kmalloc(1024, GFP_KERNEL);
|
|
|
|
if (lbuf == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Protect vga_list */
|
2009-08-11 05:52:06 +00:00
|
|
|
spin_lock_irqsave(&vga_lock, flags);
|
|
|
|
|
2011-03-31 01:57:33 +00:00
|
|
|
/* If we are targeting the default, use it */
|
2009-08-11 05:52:06 +00:00
|
|
|
pdev = priv->target;
|
|
|
|
if (pdev == NULL || pdev == PCI_INVALID_CARD) {
|
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
|
|
|
len = sprintf(lbuf, "invalid");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find card vgadev structure */
|
|
|
|
vgadev = vgadev_find(pdev);
|
|
|
|
if (vgadev == NULL) {
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* Wow, it's not in the list, that shouldn't happen, let's
|
|
|
|
* fix us up and return invalid card.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
|
|
|
len = sprintf(lbuf, "invalid");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Fill the buffer with info */
|
2009-08-11 05:52:06 +00:00
|
|
|
len = snprintf(lbuf, 1024,
|
2022-02-24 22:47:52 +00:00
|
|
|
"count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%u:%u)\n",
|
2009-08-11 05:52:06 +00:00
|
|
|
vga_decode_count, pci_name(pdev),
|
|
|
|
vga_iostate_to_str(vgadev->decodes),
|
|
|
|
vga_iostate_to_str(vgadev->owns),
|
|
|
|
vga_iostate_to_str(vgadev->locks),
|
|
|
|
vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
|
|
|
done:
|
|
|
|
|
|
|
|
/* Copy that to user */
|
|
|
|
if (len > count)
|
|
|
|
len = count;
|
|
|
|
rc = copy_to_user(buf, lbuf, len);
|
|
|
|
kfree(lbuf);
|
|
|
|
if (rc)
|
|
|
|
return -EFAULT;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: To avoid parsing inside kernel and to improve the speed we may
|
|
|
|
* consider use ioctl here
|
|
|
|
*/
|
2015-08-12 14:32:07 +00:00
|
|
|
static ssize_t vga_arb_write(struct file *file, const char __user *buf,
|
2009-08-11 05:52:06 +00:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct vga_arb_private *priv = file->private_data;
|
|
|
|
struct vga_arb_user_card *uc = NULL;
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
|
|
|
|
unsigned int io_state;
|
|
|
|
|
2016-10-14 13:22:22 +00:00
|
|
|
char kbuf[64], *curr_pos;
|
2009-08-11 05:52:06 +00:00
|
|
|
size_t remaining = count;
|
|
|
|
|
|
|
|
int ret_val;
|
|
|
|
int i;
|
|
|
|
|
2016-10-14 13:22:22 +00:00
|
|
|
if (count >= sizeof(kbuf))
|
|
|
|
return -EINVAL;
|
|
|
|
if (copy_from_user(kbuf, buf, count))
|
2009-08-11 05:52:06 +00:00
|
|
|
return -EFAULT;
|
|
|
|
curr_pos = kbuf;
|
2023-08-08 22:34:07 +00:00
|
|
|
kbuf[count] = '\0';
|
2009-08-11 05:52:06 +00:00
|
|
|
|
|
|
|
if (strncmp(curr_pos, "lock ", 5) == 0) {
|
|
|
|
curr_pos += 5;
|
|
|
|
remaining -= 5;
|
|
|
|
|
2010-05-24 15:24:30 +00:00
|
|
|
pr_debug("client 0x%p called 'lock'\n", priv);
|
2009-08-11 05:52:06 +00:00
|
|
|
|
|
|
|
if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
|
|
|
|
ret_val = -EPROTO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (io_state == VGA_RSRC_NONE) {
|
|
|
|
ret_val = -EPROTO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
pdev = priv->target;
|
|
|
|
if (priv->target == NULL) {
|
|
|
|
ret_val = -ENODEV;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
vga_get_uninterruptible(pdev, io_state);
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Update the client's locks lists */
|
2009-08-11 05:52:06 +00:00
|
|
|
for (i = 0; i < MAX_USER_CARDS; i++) {
|
|
|
|
if (priv->cards[i].pdev == pdev) {
|
|
|
|
if (io_state & VGA_RSRC_LEGACY_IO)
|
|
|
|
priv->cards[i].io_cnt++;
|
|
|
|
if (io_state & VGA_RSRC_LEGACY_MEM)
|
|
|
|
priv->cards[i].mem_cnt++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret_val = count;
|
|
|
|
goto done;
|
|
|
|
} else if (strncmp(curr_pos, "unlock ", 7) == 0) {
|
|
|
|
curr_pos += 7;
|
|
|
|
remaining -= 7;
|
|
|
|
|
2010-05-24 15:24:30 +00:00
|
|
|
pr_debug("client 0x%p called 'unlock'\n", priv);
|
2009-08-11 05:52:06 +00:00
|
|
|
|
|
|
|
if (strncmp(curr_pos, "all", 3) == 0)
|
|
|
|
io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
|
|
|
|
else {
|
|
|
|
if (!vga_str_to_iostate
|
|
|
|
(curr_pos, remaining, &io_state)) {
|
|
|
|
ret_val = -EPROTO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/* TODO: Add this?
|
|
|
|
if (io_state == VGA_RSRC_NONE) {
|
|
|
|
ret_val = -EPROTO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
pdev = priv->target;
|
|
|
|
if (priv->target == NULL) {
|
|
|
|
ret_val = -ENODEV;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
for (i = 0; i < MAX_USER_CARDS; i++) {
|
|
|
|
if (priv->cards[i].pdev == pdev)
|
|
|
|
uc = &priv->cards[i];
|
|
|
|
}
|
|
|
|
|
2011-11-15 22:53:11 +00:00
|
|
|
if (!uc) {
|
|
|
|
ret_val = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2009-08-11 05:52:06 +00:00
|
|
|
|
2011-11-15 22:53:11 +00:00
|
|
|
if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
|
|
|
|
ret_val = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2009-08-11 05:52:06 +00:00
|
|
|
|
2011-11-15 22:53:11 +00:00
|
|
|
if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
|
|
|
|
ret_val = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2009-08-11 05:52:06 +00:00
|
|
|
|
|
|
|
vga_put(pdev, io_state);
|
|
|
|
|
|
|
|
if (io_state & VGA_RSRC_LEGACY_IO)
|
|
|
|
uc->io_cnt--;
|
|
|
|
if (io_state & VGA_RSRC_LEGACY_MEM)
|
|
|
|
uc->mem_cnt--;
|
|
|
|
|
|
|
|
ret_val = count;
|
|
|
|
goto done;
|
|
|
|
} else if (strncmp(curr_pos, "trylock ", 8) == 0) {
|
|
|
|
curr_pos += 8;
|
|
|
|
remaining -= 8;
|
|
|
|
|
2010-05-24 15:24:30 +00:00
|
|
|
pr_debug("client 0x%p called 'trylock'\n", priv);
|
2009-08-11 05:52:06 +00:00
|
|
|
|
|
|
|
if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
|
|
|
|
ret_val = -EPROTO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/* TODO: Add this?
|
|
|
|
if (io_state == VGA_RSRC_NONE) {
|
|
|
|
ret_val = -EPROTO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
pdev = priv->target;
|
|
|
|
if (priv->target == NULL) {
|
|
|
|
ret_val = -ENODEV;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vga_tryget(pdev, io_state)) {
|
|
|
|
/* Update the client's locks lists... */
|
|
|
|
for (i = 0; i < MAX_USER_CARDS; i++) {
|
|
|
|
if (priv->cards[i].pdev == pdev) {
|
|
|
|
if (io_state & VGA_RSRC_LEGACY_IO)
|
|
|
|
priv->cards[i].io_cnt++;
|
|
|
|
if (io_state & VGA_RSRC_LEGACY_MEM)
|
|
|
|
priv->cards[i].mem_cnt++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret_val = count;
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
ret_val = -EBUSY;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (strncmp(curr_pos, "target ", 7) == 0) {
|
|
|
|
unsigned int domain, bus, devfn;
|
|
|
|
struct vga_device *vgadev;
|
|
|
|
|
|
|
|
curr_pos += 7;
|
|
|
|
remaining -= 7;
|
2010-05-24 15:24:30 +00:00
|
|
|
pr_debug("client 0x%p called 'target'\n", priv);
|
2023-08-08 22:34:07 +00:00
|
|
|
/* If target is default */
|
2010-02-16 21:18:37 +00:00
|
|
|
if (!strncmp(curr_pos, "default", 7))
|
2009-08-11 05:52:06 +00:00
|
|
|
pdev = pci_dev_get(vga_default_device());
|
|
|
|
else {
|
|
|
|
if (!vga_pci_str_to_vars(curr_pos, remaining,
|
|
|
|
&domain, &bus, &devfn)) {
|
|
|
|
ret_val = -EPROTO;
|
|
|
|
goto done;
|
|
|
|
}
|
2012-08-28 15:43:55 +00:00
|
|
|
pdev = pci_get_domain_bus_and_slot(domain, bus, devfn);
|
2009-08-11 05:52:06 +00:00
|
|
|
if (!pdev) {
|
2016-11-18 14:11:58 +00:00
|
|
|
pr_debug("invalid PCI address %04x:%02x:%02x.%x\n",
|
|
|
|
domain, bus, PCI_SLOT(devfn),
|
|
|
|
PCI_FUNC(devfn));
|
2009-08-11 05:52:06 +00:00
|
|
|
ret_val = -ENODEV;
|
|
|
|
goto done;
|
|
|
|
}
|
2016-11-17 17:47:58 +00:00
|
|
|
|
|
|
|
pr_debug("%s ==> %04x:%02x:%02x.%x pdev %p\n", curr_pos,
|
|
|
|
domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
|
|
|
|
pdev);
|
2009-08-11 05:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vgadev = vgadev_find(pdev);
|
2015-08-12 14:32:06 +00:00
|
|
|
pr_debug("vgadev %p\n", vgadev);
|
2009-08-11 05:52:06 +00:00
|
|
|
if (vgadev == NULL) {
|
2015-08-12 14:32:05 +00:00
|
|
|
if (pdev) {
|
2016-11-18 14:11:58 +00:00
|
|
|
vgaarb_dbg(&pdev->dev, "not a VGA device\n");
|
2015-08-12 14:32:05 +00:00
|
|
|
pci_dev_put(pdev);
|
|
|
|
}
|
|
|
|
|
2009-08-11 05:52:06 +00:00
|
|
|
ret_val = -ENODEV;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->target = pdev;
|
|
|
|
for (i = 0; i < MAX_USER_CARDS; i++) {
|
|
|
|
if (priv->cards[i].pdev == pdev)
|
|
|
|
break;
|
|
|
|
if (priv->cards[i].pdev == NULL) {
|
|
|
|
priv->cards[i].pdev = pdev;
|
|
|
|
priv->cards[i].io_cnt = 0;
|
|
|
|
priv->cards[i].mem_cnt = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == MAX_USER_CARDS) {
|
2016-11-18 14:11:58 +00:00
|
|
|
vgaarb_dbg(&pdev->dev, "maximum user cards (%d) number reached, ignoring this one!\n",
|
2010-02-02 22:38:15 +00:00
|
|
|
MAX_USER_CARDS);
|
2009-08-11 05:52:06 +00:00
|
|
|
pci_dev_put(pdev);
|
2023-08-08 22:34:07 +00:00
|
|
|
/* XXX: Which value to return? */
|
2009-08-11 05:52:06 +00:00
|
|
|
ret_val = -ENOMEM;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret_val = count;
|
|
|
|
pci_dev_put(pdev);
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
|
|
|
|
} else if (strncmp(curr_pos, "decodes ", 8) == 0) {
|
|
|
|
curr_pos += 8;
|
|
|
|
remaining -= 8;
|
2015-08-12 14:32:06 +00:00
|
|
|
pr_debug("client 0x%p called 'decodes'\n", priv);
|
2009-08-11 05:52:06 +00:00
|
|
|
|
|
|
|
if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
|
|
|
|
ret_val = -EPROTO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
pdev = priv->target;
|
|
|
|
if (priv->target == NULL) {
|
|
|
|
ret_val = -ENODEV;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
__vga_set_legacy_decoding(pdev, io_state, true);
|
|
|
|
ret_val = count;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/* If we got here, the message written is not part of the protocol! */
|
|
|
|
return -EPROTO;
|
|
|
|
|
|
|
|
done:
|
|
|
|
return ret_val;
|
|
|
|
}
|
|
|
|
|
2017-07-03 10:39:46 +00:00
|
|
|
static __poll_t vga_arb_fpoll(struct file *file, poll_table *wait)
|
2009-08-11 05:52:06 +00:00
|
|
|
{
|
2010-05-24 15:24:30 +00:00
|
|
|
pr_debug("%s\n", __func__);
|
2009-08-11 05:52:06 +00:00
|
|
|
|
|
|
|
poll_wait(file, &vga_wait_queue, wait);
|
2018-02-11 22:34:03 +00:00
|
|
|
return EPOLLIN;
|
2009-08-11 05:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int vga_arb_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct vga_arb_private *priv;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2010-05-24 15:24:30 +00:00
|
|
|
pr_debug("%s\n", __func__);
|
2009-08-11 05:52:06 +00:00
|
|
|
|
2011-07-26 00:12:56 +00:00
|
|
|
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
|
2009-08-11 05:52:06 +00:00
|
|
|
if (priv == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
spin_lock_init(&priv->lock);
|
|
|
|
file->private_data = priv;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&vga_user_lock, flags);
|
|
|
|
list_add(&priv->list, &vga_user_list);
|
|
|
|
spin_unlock_irqrestore(&vga_user_lock, flags);
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Set the client's lists of locks */
|
2009-08-11 05:52:06 +00:00
|
|
|
priv->target = vga_default_device(); /* Maybe this is still null! */
|
|
|
|
priv->cards[0].pdev = priv->target;
|
|
|
|
priv->cards[0].io_cnt = 0;
|
|
|
|
priv->cards[0].mem_cnt = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vga_arb_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct vga_arb_private *priv = file->private_data;
|
|
|
|
struct vga_arb_user_card *uc;
|
|
|
|
unsigned long flags;
|
|
|
|
int i;
|
|
|
|
|
2010-05-24 15:24:30 +00:00
|
|
|
pr_debug("%s\n", __func__);
|
2009-08-11 05:52:06 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&vga_user_lock, flags);
|
|
|
|
list_del(&priv->list);
|
|
|
|
for (i = 0; i < MAX_USER_CARDS; i++) {
|
|
|
|
uc = &priv->cards[i];
|
|
|
|
if (uc->pdev == NULL)
|
|
|
|
continue;
|
2016-11-17 17:47:58 +00:00
|
|
|
vgaarb_dbg(&uc->pdev->dev, "uc->io_cnt == %d, uc->mem_cnt == %d\n",
|
|
|
|
uc->io_cnt, uc->mem_cnt);
|
2009-08-11 05:52:06 +00:00
|
|
|
while (uc->io_cnt--)
|
|
|
|
vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
|
|
|
|
while (uc->mem_cnt--)
|
|
|
|
vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&vga_user_lock, flags);
|
|
|
|
|
|
|
|
kfree(priv);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-08-08 22:34:07 +00:00
|
|
|
* Callback any registered clients to let them know we have a change in VGA
|
|
|
|
* cards.
|
2009-08-11 05:52:06 +00:00
|
|
|
*/
|
|
|
|
static void vga_arbiter_notify_clients(void)
|
|
|
|
{
|
|
|
|
struct vga_device *vgadev;
|
|
|
|
unsigned long flags;
|
2023-08-08 22:34:05 +00:00
|
|
|
unsigned int new_decodes;
|
2009-08-11 05:52:06 +00:00
|
|
|
bool new_state;
|
|
|
|
|
|
|
|
if (!vga_arbiter_used)
|
|
|
|
return;
|
|
|
|
|
2023-08-08 22:34:06 +00:00
|
|
|
new_state = (vga_count > 1) ? false : true;
|
|
|
|
|
2009-08-11 05:52:06 +00:00
|
|
|
spin_lock_irqsave(&vga_lock, flags);
|
|
|
|
list_for_each_entry(vgadev, &vga_list, list) {
|
2021-07-16 06:16:34 +00:00
|
|
|
if (vgadev->set_decode) {
|
|
|
|
new_decodes = vgadev->set_decode(vgadev->pdev,
|
|
|
|
new_state);
|
2009-08-11 05:52:06 +00:00
|
|
|
vga_update_device_decodes(vgadev, new_decodes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&vga_lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pci_notify(struct notifier_block *nb, unsigned long action,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct device *dev = data;
|
|
|
|
struct pci_dev *pdev = to_pci_dev(dev);
|
|
|
|
bool notify = false;
|
|
|
|
|
2016-11-17 17:47:58 +00:00
|
|
|
vgaarb_dbg(dev, "%s\n", __func__);
|
2009-08-11 05:52:06 +00:00
|
|
|
|
2023-10-06 21:48:38 +00:00
|
|
|
/* Only deal with VGA class devices */
|
|
|
|
if (!pci_is_vga(pdev))
|
|
|
|
return 0;
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/*
|
|
|
|
* For now, we're only interested in devices added and removed.
|
|
|
|
* I didn't test this thing here, so someone needs to double check
|
|
|
|
* for the cases of hot-pluggable VGA cards.
|
|
|
|
*/
|
2009-08-11 05:52:06 +00:00
|
|
|
if (action == BUS_NOTIFY_ADD_DEVICE)
|
|
|
|
notify = vga_arbiter_add_pci_device(pdev);
|
|
|
|
else if (action == BUS_NOTIFY_DEL_DEVICE)
|
|
|
|
notify = vga_arbiter_del_pci_device(pdev);
|
|
|
|
|
|
|
|
if (notify)
|
|
|
|
vga_arbiter_notify_clients();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block pci_notifier = {
|
|
|
|
.notifier_call = pci_notify,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct file_operations vga_arb_device_fops = {
|
|
|
|
.read = vga_arb_read,
|
|
|
|
.write = vga_arb_write,
|
|
|
|
.poll = vga_arb_fpoll,
|
|
|
|
.open = vga_arb_open,
|
|
|
|
.release = vga_arb_release,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 16:52:59 +00:00
|
|
|
.llseek = noop_llseek,
|
2009-08-11 05:52:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct miscdevice vga_arb_device = {
|
|
|
|
MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
|
|
|
|
};
|
|
|
|
|
2017-10-13 03:47:29 +00:00
|
|
|
static int __init vga_arb_device_init(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
|
|
|
|
rc = misc_register(&vga_arb_device);
|
|
|
|
if (rc < 0)
|
|
|
|
pr_err("error %d registering device\n", rc);
|
|
|
|
|
|
|
|
bus_register_notifier(&pci_bus_type, &pci_notifier);
|
|
|
|
|
2023-08-08 22:34:07 +00:00
|
|
|
/* Add all VGA class PCI devices by default */
|
2017-10-13 03:47:29 +00:00
|
|
|
pdev = NULL;
|
|
|
|
while ((pdev =
|
|
|
|
pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
|
2023-10-06 21:48:38 +00:00
|
|
|
PCI_ANY_ID, pdev)) != NULL) {
|
|
|
|
if (pci_is_vga(pdev))
|
|
|
|
vga_arbiter_add_pci_device(pdev);
|
|
|
|
}
|
2017-10-13 03:47:29 +00:00
|
|
|
|
2016-11-17 17:47:58 +00:00
|
|
|
pr_info("loaded\n");
|
2009-08-11 05:52:06 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2022-02-24 22:47:43 +00:00
|
|
|
subsys_initcall_sync(vga_arb_device_init);
|