#!/usr/bin/env python3 """Execute unchanged Linux NHI preparation helpers and exact PCI match table. Usage: thunderbolt_controller_oracle.py PINNED_SOURCE_DIRECTORY OUTPUT_DIRECTORY Generated C/binary/trace remain ignored. PCI/MMIO/clock captures model successful reset and firmware startup. This is source effect evidence, not real PCI/ACPI, IRQ handler drain, CM ownership, native IOMMU attachment or physical qualification. """ import hashlib from pathlib import Path import re import subprocess import sys source, out = map(Path, sys.argv[1:]) hashes = { "pci.c": "6a0269d971c416f3021b6b14176c776e81bd0fe42018ae559458007520c1d650", "nhi.c": "5b680a6d921d9f7ec5dd48e5348e645b1bdca66273119cc9b5bdece899a09169", "nhi.h": "f0026407a0005510e34cb331dba9b4a360bb93bdb5c63cd0cec0f2273fdc1e5c", "nhi_regs.h": "e98150dae51b46c1d83d92c0de0fe2b925e61e220a1a14d8cd200a153fc81e45", "include_linux_pci_ids.h": "020fac001871c0f8441c1294f787231fb6cfbf2c55915ca8f918262e2db4487c", } texts = {} for name, digest in hashes.items(): raw = (source / name).read_bytes() assert hashlib.sha256(raw).hexdigest() == digest, name texts[name] = raw.decode() out.mkdir(parents=True, exist_ok=True) def function(filename, name): text = texts[filename] match = re.search(r"^(?:static )?(?:void|int) [^;{}]*?\b" + name + r"\([^;{}]*?\)\n\{", text, re.M) assert match, name at, depth = match.end(), 1 while depth: depth += (text[at] == "{") - (text[at] == "}") at += 1 return text[match.start():at] text = texts["pci.c"] start = text.index("static struct pci_device_id nhi_ids[] = {") at, depth = text.index("{", start) + 1, 1 while depth: depth += (text[at] == "{") - (text[at] == "}") at += 1 ids = text[start:at + 1] names = re.findall(r"PCI_VDEVICE\(INTEL, (PCI_DEVICE_ID_\w+)\)", ids) assert len(names) == 32 nhi_macros = "\n".join(line for line in texts["nhi.h"].splitlines() if line.startswith("#define PCI_") or line.startswith("#define QUIRK_")) register_macros = "\n".join(line for line in texts["nhi_regs.h"].splitlines() if line.startswith("#define ")) prelude = r'''// SPDX-License-Identifier: GPL-2.0-only // Copyright (c) 2014 Andreas Noever // Copyright (C) 2018 Intel Corporation #include #include #include #include #include #include typedef uint8_t u8; typedef uint32_t u32; typedef uint64_t ktime_t; typedef uintptr_t kernel_ulong_t; #define BIT(n) (1U<<(n)) #define GENMASK(h,l) ((~0U>>(31-(h)))&(~0U<<(l))) #define FIELD_GET(mask,value) (((value)&(mask))>>__builtin_ctz(mask)) #define dev_dbg(...) ((void)0) #define dev_warn(...) ((void)0) #define PCI_ANY_ID (~0U) #define PCI_VDEVICE(v,d) .vendor=PCI_VENDOR_ID_##v,.device=(d),.subvendor=PCI_ANY_ID,.subdevice=PCI_ANY_ID #define PCI_DEVICE_CLASS(c,m) .vendor=PCI_ANY_ID,.device=PCI_ANY_ID,.subvendor=PCI_ANY_ID,.subdevice=PCI_ANY_ID,.class=(c),.class_mask=(m) struct tb_nhi_ops { int unused; }; static const struct tb_nhi_ops icl_nhi_ops={0}; struct pci_device_id { u32 vendor,device,subvendor,subdevice,class,class_mask; kernel_ulong_t driver_data; }; struct pci_dev {u32 vendor,device,config[1024];}; struct tb_nhi {void *dev; unsigned char *iobase; unsigned quirks,hop_count;}; struct tb_nhi_pci {struct tb_nhi nhi;}; #define to_pci_dev(dev) ((struct pci_dev *)(dev)) static unsigned char bar[0x40000] __attribute__((aligned(4))); static struct pci_dev pdev; static struct tb_nhi_pci host; static u32 enables[128],pending[128]; static unsigned pc,nm; static struct {unsigned offset;u32 value;} plog[16],mlog[256]; static ktime_t now,reset_clear_at,ready_at; static ktime_t ktime_get(void){return now;} static ktime_t ktime_add_ms(ktime_t a,unsigned b){return a+b;} static bool ktime_before(ktime_t a,ktime_t b){return aconfig[offset/4];if(offset==VS_CAP_9 && now>=ready_at)*value|=VS_CAP_9_FW_READY;return 0;} static int pci_write_config_dword(struct pci_dev *p,unsigned offset,u32 value){if(pc>=16)abort();plog[pc].offset=offset;plog[pc++].value=value;p->config[offset/4]=value;if(offset==VS_CAP_22 && value&VS_CAP_22_FORCE_POWER)ready_at=now+6;return 0;} static u32 ioread32(const void *pointer){ size_t offset=(const unsigned char *)pointer-bar; if(offset>=sizeof(bar))abort(); if(offset>=REG_RING_INTERRUPT_BASE && offset=REG_RING_NOTIFY_BASE && offset=reset_clear_at)*(u32*)(bar+REG_RESET)=0; return *(const u32*)pointer; } static void iowrite32(u32 value,void *pointer){ size_t offset=(unsigned char*)pointer-bar;if(offset>=sizeof(bar)||nm>=256)abort();mlog[nm].offset=offset;mlog[nm++].value=value; if(!(host.nhi.quirks&QUIRK_AUTO_CLEAR_INT) && offset>=REG_RING_INTERRUPT_MASK_CLEAR_BASE && offset=REG_RING_INTERRUPT_BASE && offset=REG_RING_INT_CLEAR && offsetvendor||id->device||id->class;id++)if((id->vendor==vendor||id->vendor==PCI_ANY_ID)&&(id->device==device||id->device==PCI_ANY_ID)&&((id->class^class)&id->class_mask)==0)return id; return NULL; } int main(void){ unsigned devices[]={DEVICE_LIST};unsigned versions[]={0x20,0x40},hops[]={8,16,33,64,1023}; for(unsigned d=0;ddriver_data && icl_nhi_resume(&host.nhi))abort(); unsigned family=id->driver_data?1:(id->class_mask?2:0); printf("C %u %u %u %u %u %u %u",vendor,device,class,versions[v],hops[h],family,pc);for(unsigned i=0;i