Files
nvdriver/kernel-open/nvidia/ga10x_adapter.c
Eric Hartford 860fe11713 ga10x_gsp: populate bare-metal GspSystemInfo hypervisorType, FHB/chipset IDs, and upstream ASPM (#487, #548) (#660)
* ga10x_gsp: populate GspSystemInfo hypervisorType, FHB/chipset IDs, and upstream ASPM (#487, #548)

* ga10x_gsp: guard upstream ASPM on intervening bridges and keep chipsetIDInfo separate from FHBBusInfo

---------

Co-authored-by: Eric Hartford <eric@quixi.ai>
2026-12-05 19:24:28 -08:00

442 lines
16 KiB
C

// SPDX-License-Identifier: MIT
/* Linux services for the GA10x GSP-RM client (src/nvidia/kernel/ga10x_*.rs): log lines, a sleeping wait, PCI facts
* and config space, a BAR0 fill, the CPU clocks, the caller's credentials, module parameters. No GPU logic lives here. */
#include <linux/acpi.h>
#include <linux/capability.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/ktime.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/timekeeping.h>
#include <linux/vgaarb.h>
#ifdef NV_RS_HAVE_VIDEO_IS_PRIMARY_DEVICE
#include <asm/video.h>
#endif
#ifdef CONFIG_X86
#include <asm/processor.h>
#include <asm/tsc.h>
#endif
#include "pci_adapter.h"
static int gsp_passthru = -1;
module_param(gsp_passthru, int, 0444);
MODULE_PARM_DESC(gsp_passthru, "GspSystemInfo.bIsPassthru: -1 = detect (default; x86 hypervisor flag, 0 on other architectures), 0 or 1 = force");
static bool ga10x_gsp = true;
module_param(ga10x_gsp, bool, 0444);
MODULE_PARM_DESC(ga10x_gsp, "GA10x: boot GSP-RM and run the kernel-side RM in ga10x*.rs; 0 = gsp_bringup's own path (default: 1)");
static bool ga10x_events = true;
module_param(ga10x_events, bool, 0444);
MODULE_PARM_DESC(ga10x_events, "GA10x and GV100: RM events from the engines' non-stall interrupts (default: 1)");
static bool gsp_route_trace;
module_param(gsp_route_trace, bool, 0644);
MODULE_PARM_DESC(gsp_route_trace, "Log GA10x GSP-RM routing: controls, mirrored allocs and frees (default: 0)");
bool nv_rs_ga10x_gsp_enabled(void);
bool nv_rs_ga10x_gsp_enabled(void)
{
return ga10x_gsp;
}
bool nv_rs_ga10x_events_enabled(void);
bool nv_rs_ga10x_events_enabled(void)
{
return ga10x_events;
}
bool nv_rs_ga10x_route_trace(void);
bool nv_rs_ga10x_route_trace(void)
{
return READ_ONCE(gsp_route_trace);
}
/* "ga10x: <what> a b c d" (hex), on the GPU's device when there is one */
void nv_rs_ga10x_note(struct nv_rs_pci *pci, const char *what, u64 a, u64 b, u64 c, u64 d);
void nv_rs_ga10x_note(struct nv_rs_pci *pci, const char *what, u64 a, u64 b, u64 c, u64 d)
{
struct device *dev = pci ? nv_rs_pci_device(pci) : NULL;
if (dev)
dev_info(dev, "ga10x: %s %llx %llx %llx %llx\n", what, a, b, c, d);
else
pr_info("nvidia_rs: ga10x: %s %llx %llx %llx %llx\n", what, a, b, c, d);
}
void nv_rs_ga10x_error(struct nv_rs_pci *pci, const char *what, u64 a, u64 b, u64 c, u64 d);
void nv_rs_ga10x_error(struct nv_rs_pci *pci, const char *what, u64 a, u64 b, u64 c, u64 d)
{
struct device *dev = pci ? nv_rs_pci_device(pci) : NULL;
if (dev)
dev_err(dev, "ga10x: %s %llx %llx %llx %llx\n", what, a, b, c, d);
else
pr_err("nvidia_rs: ga10x: %s %llx %llx %llx %llx\n", what, a, b, c, d);
}
/* a message with three numbers and a text from the GPU (GSP-RM's error log) */
void nv_rs_ga10x_text(struct nv_rs_pci *pci, const char *what, u32 a, u32 b, u32 c, const u8 *text, u32 len);
void nv_rs_ga10x_text(struct nv_rs_pci *pci, const char *what, u32 a, u32 b, u32 c, const u8 *text, u32 len)
{
struct device *dev = pci ? nv_rs_pci_device(pci) : NULL;
if (!text)
len = 0;
if (dev)
dev_err(dev, "ga10x: %s %u %u %u: %.*s\n", what ? what : "", a, b, c,
(int)min_t(u32, len, 256), text ? (const char *)text : "");
}
/* process context only */
void nv_rs_os_sleep_us(u32 us);
void nv_rs_os_sleep_us(u32 us)
{
if (us < 10)
udelay(us);
else if (us <= 20000)
usleep_range(us, us + us / 4);
else
msleep(DIV_ROUND_UP(us, 1000));
}
struct nv_rs_ga10x_pci_info {
u64 bar[6];
u64 bar_len[6];
u32 io_bars;
u16 vendor, device, subsystem_vendor, subsystem_device;
u8 revision, flr_supported, bar0_64b, behind_bridge;
u32 dev_id;
u64 max_user_va;
u64 page_size;
u32 domain;
u32 link_cap;
u32 hypervisor_type;
u8 is_passthru;
u8 oor_arch;
u8 upstream_l0s_unsupported;
u8 upstream_l1_unsupported;
u8 upstream_address_valid;
u8 fhb_revision_id;
u16 fhb_device_id;
u16 fhb_vendor_id;
u16 fhb_subdevice_id;
u16 fhb_subvendor_id;
u8 pad2[6];
};
/* Match 580.178.04 hypervisor.c:_hypervisorDetection_HVM & nv-hypervisor.h:
* 0 = XEN, 1 = VMWARE, 2 = HYPERV, 3 = KVM, 4 = OS_HYPERVISOR_UNKNOWN (bare metal). */
static u32 nv_rs_ga10x_detect_hypervisor(void)
{
#ifdef CONFIG_X86
u32 base, leaf = 0, detected = 4u;
if (!boot_cpu_has(X86_FEATURE_HYPERVISOR))
return 4u;
for (base = 0x40000000u; base < 0x40001000u; base += 0x100u) {
u32 eax = 0, sig[3] = { 0, 0, 0 };
cpuid(base, &eax, &sig[0], &sig[1], &sig[2]);
if (memcmp(sig, "XenVMMXenVMM", 12) == 0 && base > leaf) {
leaf = base;
detected = 0u;
} else if (memcmp(sig, "VMwareVMware", 12) == 0 && base > leaf) {
leaf = base;
detected = 1u;
} else if (memcmp(sig, "Microsoft Hv", 12) == 0 && base > leaf) {
leaf = base;
detected = 2u;
} else if (memcmp(sig, "KVMKVMKVM\0\0\0", 12) == 0 && base > leaf) {
leaf = base;
detected = 3u;
}
}
return detected;
#else
return 4u;
#endif
}
int nv_rs_ga10x_pci_info(struct nv_rs_pci *pci, struct nv_rs_ga10x_pci_info *out);
int nv_rs_ga10x_pci_info(struct nv_rs_pci *pci, struct nv_rs_ga10x_pci_info *out)
{
struct pci_dev *pdev = pci ? nv_rs_pci_pdev(pci) : NULL;
struct pci_dev *fhb = NULL;
u16 cmd = 0;
u32 devcap = 0;
int i;
if (!pdev || !out)
return -ENODEV;
memset(out, 0, sizeof(*out));
pci_read_config_word(pdev, PCI_COMMAND, &cmd);
for (i = 0; i < 6; i++) {
out->bar[i] = pci_resource_start(pdev, i);
out->bar_len[i] = pci_resource_len(pdev, i);
if ((pci_resource_flags(pdev, i) & IORESOURCE_IO) && (cmd & PCI_COMMAND_IO))
out->io_bars |= 1u << i;
}
out->vendor = pdev->vendor;
out->device = pdev->device;
out->subsystem_vendor = pdev->subsystem_vendor;
out->subsystem_device = pdev->subsystem_device;
out->revision = pdev->revision;
if (pcie_capability_read_dword(pdev, PCI_EXP_DEVCAP, &devcap) == 0)
out->flr_supported = (devcap & PCI_EXP_DEVCAP_FLR) ? 1u : 0u;
out->bar0_64b = (pci_resource_flags(pdev, 0) & IORESOURCE_MEM_64) ? 1u : 0u;
if (pdev->bus && pdev->bus->self) {
struct pci_dev *bridge = pdev->bus->self;
bool intervening_bridge =
!pci_is_pcie(bridge) || (pci_pcie_type(bridge) != PCI_EXP_TYPE_ROOT_PORT);
out->behind_bridge = 1u;
out->upstream_address_valid = 1u;
/* Match chipset_pcie.c:1313-1356: only derive upstream ASPM disable flags
* when an intervening bridge/switch separates the root port from the GPU. */
if (intervening_bridge) {
u32 up_lnkcap = 0;
if (pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &up_lnkcap) == 0) {
out->upstream_l0s_unsupported = (up_lnkcap & 0x400u) ? 0u : 1u;
out->upstream_l1_unsupported = (up_lnkcap & 0x800u) ? 0u : 1u;
} else {
out->upstream_l0s_unsupported = 1u;
out->upstream_l1_unsupported = 1u;
}
}
}
fhb = pci_get_domain_bus_and_slot(pci_domain_nr(pdev->bus), 0, PCI_DEVFN(0, 0));
if (fhb) {
out->fhb_device_id = fhb->device;
out->fhb_vendor_id = fhb->vendor;
out->fhb_subdevice_id = fhb->subsystem_device;
out->fhb_subvendor_id = fhb->subsystem_vendor;
out->fhb_revision_id = fhb->revision;
pci_dev_put(fhb);
}
out->dev_id = pci_dev_id(pdev);
out->max_user_va = TASK_SIZE;
out->page_size = PAGE_SIZE;
out->domain = (u32)pci_domain_nr(pdev->bus);
pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &out->link_cap);
#if defined(CONFIG_X86_64)
out->oor_arch = 0u; /* OOR_ARCH_X86_64 */
#elif defined(CONFIG_ARM64)
out->oor_arch = 3u; /* OOR_ARCH_AARCH64 */
#else
out->oor_arch = 4u; /* OOR_ARCH_NONE */
#endif
out->hypervisor_type = nv_rs_ga10x_detect_hypervisor();
#ifdef CONFIG_X86
out->is_passthru = boot_cpu_has(X86_FEATURE_HYPERVISOR) ? 1u : 0u;
#endif
if (gsp_passthru >= 0)
out->is_passthru = gsp_passthru != 0;
return 0;
}
/* Populate GspSystemInfo.acpiMethodData (0xa0..0x344) and bIsPrimary (0x380),
* matching Linux nouveau r570_gsp_acpi_info / r570_gsp_set_system_info for
* mobile/Optimus GPUs. */
static int gsp_primary = -1;
module_param(gsp_primary, int, 0444);
MODULE_PARM_DESC(gsp_primary, "GspSystemInfo.bIsPrimary: -1 = detect (default), 0 or 1 = force");
static bool nv_rs_ga10x_is_primary(struct pci_dev *pdev)
{
if (gsp_primary >= 0)
return gsp_primary != 0;
#ifdef NV_RS_HAVE_VIDEO_IS_PRIMARY_DEVICE
return video_is_primary_device(&pdev->dev);
#else
/* before 6.9 (asm/video.h): the VGA arbiter's default device (NULL without CONFIG_VGA_ARB) */
return vga_default_device() == pdev;
#endif
}
void nv_rs_ga10x_fill_acpi_sysinfo(struct nv_rs_pci *pci, u8 *sysinfo, u32 len);
void nv_rs_ga10x_fill_acpi_sysinfo(struct nv_rs_pci *pci, u8 *sysinfo, u32 len)
{
struct pci_dev *pdev = pci ? nv_rs_pci_pdev(pci) : NULL;
u8 primary;
if (!pdev || !sysinfo || len < 0x3a8u)
return;
primary = nv_rs_ga10x_is_primary(pdev) ? 1u : 0u;
sysinfo[0x380] = primary;
#if defined(CONFIG_ACPI) && defined(CONFIG_X86)
{
acpi_handle handle = ACPI_HANDLE(&pdev->dev);
u32 dod_status = 0xffffu, dod_len = 0;
u32 jt_status = 0xffffu, jt_caps = 0;
u32 opt_status = 0xffffu, opt_caps = 0;
if (handle) {
struct acpi_buffer dod_out = { ACPI_ALLOCATE_BUFFER, NULL };
acpi_status ast;
static const guid_t jt_guid = GUID_INIT(
0xCBECA351u, 0x067Bu, 0x4924u,
0x9Cu, 0xBDu, 0xB4u, 0x6Bu, 0x00u, 0xB8u, 0x6Fu, 0x34u);
static const guid_t nvop_guid = GUID_INIT(
0xA486D8F8u, 0x0BDAu, 0x471Bu,
0xA7u, 0x2Bu, 0x60u, 0x42u, 0xA6u, 0xB5u, 0xBEu, 0xE0u);
union acpi_object argv4;
union acpi_object *obj;
u32 in_buf;
/* acpiMethodData.bValid */
sysinfo[0x0a0] = 1u;
/* dodMethodData at 0x0a4: { status(4), acpiIdListLen(4), acpiIdList[16](64) } */
ast = acpi_evaluate_object(handle, "_DOD", NULL, &dod_out);
if (ACPI_SUCCESS(ast) && dod_out.pointer) {
union acpi_object *dod = dod_out.pointer;
if (dod->type == ACPI_TYPE_PACKAGE && dod->package.count <= 16 &&
(!dod->package.count || dod->package.elements)) {
u32 i;
bool ok = true;
for (i = 0; i < dod->package.count; i++) {
u32 id;
if (dod->package.elements[i].type != ACPI_TYPE_INTEGER) {
ok = false;
break;
}
id = (u32)dod->package.elements[i].integer.value;
memcpy(sysinfo + 0x0ac + i * 4, &id, sizeof(id));
dod_len += 4;
}
if (ok) {
dod_status = 0;
} else {
/* no partial list: status says invalid, and the ids written so far go */
memset(sysinfo + 0x0ac, 0, 16 * 4);
dod_len = 0;
}
}
kfree(dod_out.pointer);
}
memcpy(sysinfo + 0x0a4, &dod_status, sizeof(dod_status));
memcpy(sysinfo + 0x0a8, &dod_len, sizeof(dod_len));
/* jtMethodData at 0x0ec: { status(4), jtCaps(4), jtRevId(2), bSBIOSCaps(1) } */
in_buf = 0;
argv4.buffer.type = ACPI_TYPE_BUFFER;
argv4.buffer.length = sizeof(in_buf);
argv4.buffer.pointer = (u8 *)&in_buf;
obj = acpi_evaluate_dsm(handle, &jt_guid, 0x103u, 0x1u, &argv4);
if (obj) {
if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == 4 &&
obj->buffer.pointer) {
u16 rev;
jt_status = 0;
memcpy(&jt_caps, obj->buffer.pointer, sizeof(jt_caps));
rev = (u16)((jt_caps & 0xfff00000u) >> 20);
memcpy(sysinfo + 0x0f0, &jt_caps, sizeof(jt_caps));
memcpy(sysinfo + 0x0f4, &rev, sizeof(rev));
sysinfo[0x0f6] = 0;
}
ACPI_FREE(obj);
}
memcpy(sysinfo + 0x0ec, &jt_status, sizeof(jt_status));
/* capsMethodData at 0x33c: { status(4), optimusCaps(4) } */
if (acpi_check_dsm(handle, &nvop_guid, 0x100u, BIT_ULL(0x1a))) {
in_buf = 0;
argv4.buffer.type = ACPI_TYPE_BUFFER;
argv4.buffer.length = sizeof(in_buf);
argv4.buffer.pointer = (u8 *)&in_buf;
obj = acpi_evaluate_dsm(handle, &nvop_guid, 0x100u, 0x1au, &argv4);
if (obj) {
if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == 4 &&
obj->buffer.pointer) {
opt_status = 0;
memcpy(&opt_caps, obj->buffer.pointer, sizeof(opt_caps));
memcpy(sysinfo + 0x340, &opt_caps, sizeof(opt_caps));
}
ACPI_FREE(obj);
}
}
memcpy(sysinfo + 0x33c, &opt_status, sizeof(opt_status));
dev_info(&pdev->dev,
"ga10x: ACPI sysinfo: primary=%u dod=%x(n=%u) jt=%x(caps=%x) opt=%x(caps=%x)\n",
primary, dod_status, dod_len / 4, jt_status, jt_caps, opt_status, opt_caps);
}
}
#endif
}
int nv_rs_ga10x_cfg_read32(struct nv_rs_pci *pci, u32 offset, u32 *value);
int nv_rs_ga10x_cfg_read32(struct nv_rs_pci *pci, u32 offset, u32 *value)
{
struct pci_dev *pdev = pci ? nv_rs_pci_pdev(pci) : NULL;
if (!pdev || !value || nv_rs_pci_is_removed(pci))
return -ENODEV;
return pcibios_err_to_errno(pci_read_config_dword(pdev, offset, value));
}
int nv_rs_ga10x_cfg_write32(struct nv_rs_pci *pci, u32 offset, u32 value);
int nv_rs_ga10x_cfg_write32(struct nv_rs_pci *pci, u32 offset, u32 value)
{
struct pci_dev *pdev = pci ? nv_rs_pci_pdev(pci) : NULL;
if (!pdev || nv_rs_pci_is_removed(pci))
return -ENODEV;
return pcibios_err_to_errno(pci_write_config_dword(pdev, offset, value));
}
/* `count` dwords of `value` from BAR0 offset `offset` (zeroing video memory through the BAR0 window) */
int nv_rs_ga10x_bar0_fill32(struct nv_rs_pci *pci, u64 offset, u32 value, u32 count);
int nv_rs_ga10x_bar0_fill32(struct nv_rs_pci *pci, u64 offset, u32 value, u32 count)
{
return nv_rs_pci_bar0_fill32(pci, offset, value, count);
}
int nv_rs_ga10x_bar0_write_vram_ptes(struct nv_rs_pci *pci, u64 offset, u64 phys_start,
u64 step_bytes, u64 flags, u32 count);
int nv_rs_ga10x_bar0_write_vram_ptes(struct nv_rs_pci *pci, u64 offset, u64 phys_start,
u64 step_bytes, u64 flags, u32 count)
{
return nv_rs_pci_bar0_write_vram_ptes(pci, offset, phys_start, step_bytes, flags, count);
}
int nv_rs_ga10x_bar0_write_sysmem_ptes(struct nv_rs_pci *pci, u64 offset, const u64 *dmas,
u64 flags, u32 count);
int nv_rs_ga10x_bar0_write_sysmem_ptes(struct nv_rs_pci *pci, u64 offset, const u64 *dmas,
u64 flags, u32 count)
{
return nv_rs_pci_bar0_write_sysmem_ptes(pci, offset, dmas, flags, count);
}
bool nv_rs_os_capable_admin(void);
bool nv_rs_os_capable_admin(void)
{
return capable(CAP_SYS_ADMIN);
}
u32 nv_rs_os_tgid(void);
u32 nv_rs_os_tgid(void)
{
return task_tgid_nr(current);
}
/* CPU clocks for GPU / CPU time correlation: 1 = wall clock (us since the epoch), 2 = TSC, 3 = monotonic raw (ns) */
u64 nv_rs_os_cpu_clock(u32 which);
u64 nv_rs_os_cpu_clock(u32 which)
{
switch (which) {
case 1:
return ktime_get_real_ns() / 1000;
#ifdef CONFIG_X86
case 2:
return rdtsc_ordered();
#endif
case 3:
return ktime_get_raw_ns();
default:
return 0;
}
}