diff --git a/kernel-open/nvidia/ga10x_adapter.c b/kernel-open/nvidia/ga10x_adapter.c index b1d6cc8c..55d7602f 100644 --- a/kernel-open/nvidia/ga10x_adapter.c +++ b/kernel-open/nvidia/ga10x_adapter.c @@ -17,6 +17,7 @@ #include #endif #ifdef CONFIG_X86 +#include #include #endif #include "pci_adapter.h" @@ -112,14 +113,56 @@ struct nv_rs_ga10x_pci_info { u64 page_size; u32 domain; u32 link_cap; + u32 hypervisor_type; u8 is_passthru; - u8 pad2[7]; + 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; @@ -142,12 +185,47 @@ int nv_rs_ga10x_pci_info(struct nv_rs_pci *pci, struct nv_rs_ga10x_pci_info *out 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; - out->behind_bridge = (pdev->bus && pdev->bus->self) ? 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 diff --git a/src/nvidia/kernel/ga10x_gsp.rs b/src/nvidia/kernel/ga10x_gsp.rs index 75b9de3f..88114dc1 100644 --- a/src/nvidia/kernel/ga10x_gsp.rs +++ b/src/nvidia/kernel/ga10x_gsp.rs @@ -163,8 +163,18 @@ pub(crate) struct PciInfo { pub page_size: u64, pub domain: u32, pub link_cap: u32, + pub hypervisor_type: u32, pub is_passthru: u8, - pub pad2: [u8; 7], + pub oor_arch: u8, + pub upstream_l0s_unsupported: u8, + pub upstream_l1_unsupported: u8, + pub upstream_address_valid: u8, + pub fhb_revision_id: u8, + pub fhb_device_id: u16, + pub fhb_vendor_id: u16, + pub fhb_subdevice_id: u16, + pub fhb_subvendor_id: u16, + pub pad2: [u8; 6], } // ---- logging: a static message and up to four values ---- @@ -2435,6 +2445,13 @@ impl Ga10xGsp { let bdf = (u64::from(info.domain) << 32) | u64::from(info.dev_id & 0xff00) | u64::from((info.dev_id >> 3) & 0x1f); + let fhb = fw::BusInfo { + device_id: info.fhb_device_id, + vendor_id: info.fhb_vendor_id, + subdevice_id: info.fhb_subdevice_id, + subvendor_id: info.fhb_subvendor_id, + revision_id: info.fhb_revision_id, + }; let s = fw::SystemInfo { bar0: info.bar[0], bar1: info.bar[1], @@ -2453,13 +2470,29 @@ impl Ga10xGsp { subsystem_vendor: info.subsystem_vendor, subsystem_device: info.subsystem_device, revision: info.revision, + oor_arch: info.oor_arch, behind_bridge: info.behind_bridge != 0, flr_supported: info.flr_supported != 0, bar0_64b: info.bar0_64b != 0, + upstream_l0s_unsupported: info.upstream_l0s_unsupported != 0, + upstream_l1_unsupported: info.upstream_l1_unsupported != 0, + upstream_address_valid: info.upstream_address_valid != 0, + fhb_bus_info: fhb, + chipset_id_info: fw::BusInfo::default(), + hypervisor_type: info.hypervisor_type, is_passthru: info.is_passthru != 0, link_cap: info.link_cap, host_page_size: info.page_size, }; + note( + self.pci, + b"sysinfo (hypervisor, passthru, fhb vid:did, up_aspm_dis)\0", + u64::from(info.hypervisor_type), + u64::from(info.is_passthru), + (u64::from(info.fhb_vendor_id) << 16) | u64::from(info.fhb_device_id), + (u64::from(info.upstream_l1_unsupported) << 1) + | u64::from(info.upstream_l0s_unsupported), + ); let mut b = Bytes::zeroed(fw::SYSTEM_INFO_BYTES)?; fw::write_system_info(b.get(), &s).map_err(fw_errno)?; // acpiMethodData (_DOD, JT and Optimus _DSM) and bIsPrimary, as nouveau and OpenRM fill them on Optimus diff --git a/src/nvidia/src/gpu/ga10x_gsp.rs b/src/nvidia/src/gpu/ga10x_gsp.rs index 2c9d0c09..7f759db7 100644 --- a/src/nvidia/src/gpu/ga10x_gsp.rs +++ b/src/nvidia/src/gpu/ga10x_gsp.rs @@ -1536,9 +1536,28 @@ pub const fn pci_config_mirror_for_family( } } -/// What `GSP_SET_SYSTEM_INFO` carries from the PCI device (nouveau `r535_gsp_rpc_set_system_info` -/// & stock 580.178.04 `kgspPopulateGspRmInitMessage` / `GspSystemInfo`). +/// `HYPERVISOR_TYPE` enum values from 580.178.04 `nv-hypervisor.h:31-38`. +/// Note that `0` is `OS_HYPERVISOR_XEN` — bare metal MUST send `HYPERVISOR_UNKNOWN` (`4`). +pub const HYPERVISOR_XEN: u32 = 0; +pub const HYPERVISOR_VMWARE: u32 = 1; +pub const HYPERVISOR_HYPERV: u32 = 2; +pub const HYPERVISOR_KVM: u32 = 3; +pub const HYPERVISOR_UNKNOWN: u32 = 4; + +/// `BUSINFO` (`g_chipset_nvoc.h:220-227`, 10 bytes with trailing alignment pad): PCI IDs of the +/// first host bridge (`FHBBusInfo` at `+0x8a`) and chipset (`chipsetIDInfo` at `+0x94`). #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct BusInfo { + pub device_id: u16, + pub vendor_id: u16, + pub subdevice_id: u16, + pub subvendor_id: u16, + pub revision_id: u8, +} + +/// What `GSP_SET_SYSTEM_INFO` carries from the PCI device (nouveau `r535_gsp_rpc_set_system_info` +/// & stock 580.178.04 `rpcGspSetSystemInfo_v17_00` / `clSyncWithGsp` / `GspSystemInfo`). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct SystemInfo { pub bar0: u64, pub bar1: u64, @@ -1554,14 +1573,64 @@ pub struct SystemInfo { pub subsystem_vendor: u16, pub subsystem_device: u16, pub revision: u8, + /// `OOR_ARCH` (`g_gpu_nvoc.h:362-366`): 0 = X86_64, 3 = AARCH64, 4 = NONE. + pub oor_arch: u8, pub behind_bridge: bool, pub flr_supported: bool, pub bar0_64b: bool, + pub upstream_l0s_unsupported: bool, + pub upstream_l1_unsupported: bool, + pub upstream_address_valid: bool, + pub fhb_bus_info: BusInfo, + pub chipset_id_info: BusInfo, + /// `HYPERVISOR_TYPE` (`nv-hypervisor.h:31-38`): 4 (`HYPERVISOR_UNKNOWN`) on bare metal. + pub hypervisor_type: u32, pub is_passthru: bool, pub link_cap: u32, pub host_page_size: u64, } +impl Default for SystemInfo { + fn default() -> Self { + Self { + bar0: 0, + bar1: 0, + bar3: 0, + io: 0, + bdf: 0, + max_user_va: 0, + pci_config_mirror_base: 0, + pci_config_mirror_size: 0, + vendor: 0, + device: 0, + subsystem_vendor: 0, + subsystem_device: 0, + revision: 0, + oor_arch: 0, + behind_bridge: false, + flr_supported: false, + bar0_64b: false, + upstream_l0s_unsupported: false, + upstream_l1_unsupported: false, + upstream_address_valid: false, + fhb_bus_info: BusInfo::default(), + chipset_id_info: BusInfo::default(), + hypervisor_type: HYPERVISOR_UNKNOWN, + is_passthru: false, + link_cap: 0, + host_page_size: 0, + } + } +} + +fn write_bus_info(out: &mut [u8], off: usize, b: &BusInfo) -> Result<()> { + wr16(out, off, b.device_id)?; + wr16(out, off + 2, b.vendor_id)?; + wr16(out, off + 4, b.subdevice_id)?; + wr16(out, off + 6, b.subvendor_id)?; + wr8(out, off + 8, b.revision_id) +} + pub fn write_system_info(out: &mut [u8], s: &SystemInfo) -> Result<()> { span(out, 0, SYSTEM_INFO_BYTES)?; wr64(out, 0x00, s.bar0)?; @@ -1579,10 +1648,17 @@ pub fn write_system_info(out: &mut [u8], s: &SystemInfo) -> Result<()> { u32::from(s.subsystem_device) << 16 | u32::from(s.subsystem_vendor), )?; wr32(out, 0x60, u32::from(s.revision))?; - wr64(out, 0x70, 1)?; // clPdbProperties bit 0 (PCIe root port / sysmem access) + wr8(out, 0x68, s.oor_arch)?; // oorArch + wr64(out, 0x70, 1)?; // clPdbProperties bit 0 (PDB_PROP_CL_PCIE_CONFIG_ACCESSIBLE) wr8(out, 0x7c, u8::from(s.behind_bridge))?; // bGpuBehindBridge wr8(out, 0x7d, u8::from(s.flr_supported))?; // bFlrSupported wr8(out, 0x7e, u8::from(s.bar0_64b))?; // b64bBar0Supported + wr8(out, 0x84, u8::from(s.upstream_l0s_unsupported))?; // bUpstreamL0sUnsupported + wr8(out, 0x85, u8::from(s.upstream_l1_unsupported))?; // bUpstreamL1Unsupported + wr8(out, 0x89, u8::from(s.upstream_address_valid))?; // upstreamAddressValid + write_bus_info(out, 0x8a, &s.fhb_bus_info)?; // FHBBusInfo + write_bus_info(out, 0x94, &s.chipset_id_info)?; // chipsetIDInfo + wr32(out, 0x344, s.hypervisor_type)?; // hypervisorType (4 = OS_HYPERVISOR_UNKNOWN) wr8(out, 0x34a, u8::from(s.is_passthru))?; // bIsPassthru wr32(out, 0x384, s.link_cap)?; // PCI_EXP_LNKCAP wr64(out, 0x398, s.host_page_size) @@ -2356,6 +2432,20 @@ mod tests { assert_eq!(ga100.frts_size, 0); let mut buf = vec![0u8; SYSTEM_INFO_BYTES]; + let fhb = BusInfo { + device_id: 0x4668, + vendor_id: 0x8086, + subdevice_id: 0x7d25, + subvendor_id: 0x1462, + revision_id: 0x02, + }; + let chipset = BusInfo { + device_id: 0x1c46, + vendor_id: 0x8086, + subdevice_id: 0x7d25, + subvendor_id: 0x1462, + revision_id: 0x05, + }; let s = SystemInfo { bdf: (1u64 << 32) | (0xb3 << 8) | 2, pci_config_mirror_base: 0x09_2000, @@ -2363,6 +2453,12 @@ mod tests { behind_bridge: true, flr_supported: true, bar0_64b: true, + upstream_l0s_unsupported: true, + upstream_l1_unsupported: false, + upstream_address_valid: true, + fhb_bus_info: fhb, + chipset_id_info: chipset, + hypervisor_type: HYPERVISOR_KVM, is_passthru: true, link_cap: 0x0043_f104, host_page_size: 4096, @@ -2376,8 +2472,26 @@ mod tests { assert_eq!(buf[0x7c], 1); assert_eq!(buf[0x7d], 1); assert_eq!(buf[0x7e], 1); + assert_eq!(buf[0x84], 1); + assert_eq!(buf[0x85], 0); + assert_eq!(buf[0x89], 1); + assert_eq!(rd16(&buf, 0x8a).unwrap(), 0x4668); + assert_eq!(rd16(&buf, 0x8c).unwrap(), 0x8086); + assert_eq!(rd16(&buf, 0x8e).unwrap(), 0x7d25); + assert_eq!(rd16(&buf, 0x90).unwrap(), 0x1462); + assert_eq!(buf[0x92], 0x02); + assert_eq!(rd16(&buf, 0x94).unwrap(), 0x1c46); + assert_eq!(rd16(&buf, 0x96).unwrap(), 0x8086); + assert_eq!(buf[0x9c], 0x05); + assert_eq!(rd32(&buf, 0x344).unwrap(), HYPERVISOR_KVM); assert_eq!(buf[0x34a], 1); assert_eq!(rd32(&buf, 0x384).unwrap(), 0x0043_f104); + + // Bare-metal default must encode OS_HYPERVISOR_UNKNOWN (4), never OS_HYPERVISOR_XEN (0). + let mut bm = vec![0u8; SYSTEM_INFO_BYTES]; + write_system_info(&mut bm, &SystemInfo::default()).unwrap(); + assert_eq!(rd32(&bm, 0x344).unwrap(), HYPERVISOR_UNKNOWN); + assert_eq!(bm[0x34a], 0); } #[test]