Files
mi8-platina-linux/scripts/phone-cellular-bam-overlay.py
T
2026-11-18 01:39:26 +09:00

101 lines
3.8 KiB
Python

#!/usr/bin/env python3
"""Generate a temporary AP DMA test overlay from this boot's live device tree.
No DT change, module loading or compilation is performed by this program.
The generated phandles are valid only for the inspected tree/boot.
"""
import argparse
from pathlib import Path
import struct
import sys
def cells(path):
data = path.read_bytes()
if not data or len(data) % 4:
raise ValueError(f"invalid cells: {path}")
return struct.unpack(f">{len(data) // 4}I", data)
def generate(root):
if b"xiaomi,platina" not in (root / "compatible").read_bytes().split(b"\0"):
raise ValueError("the device is not xiaomi,platina")
soc = root / "soc@0"
if cells(soc / "#address-cells") != (1,) or cells(soc / "#size-cells") != (1,):
raise ValueError("unsupported SoC address/size cells")
providers = {}
for prop in root.rglob("phandle"):
value, = cells(prop)
if not value or value in providers:
raise ValueError("invalid or duplicate phandle")
providers[value] = prop.parent
smmu = soc / "iommu@16c0000"
if b"qcom,smmu-v2" not in (smmu / "compatible").read_bytes().split(b"\0"):
raise ValueError("ANOC2 SMMU is not the expected controller")
if cells(smmu / "#iommu-cells") != (1,):
raise ValueError("unsupported ANOC2 IOMMU argument layout")
smmu_id, = cells(smmu / "phandle")
for prop in root.rglob("iommus"):
values = cells(prop)
offset = 0
while offset < len(values):
provider = providers.get(values[offset])
if provider is None:
raise ValueError(f"unresolved IOMMU provider: {prop}")
nargs, = cells(provider / "#iommu-cells")
if nargs < 1 or offset + nargs >= len(values):
raise ValueError(f"invalid IOMMU arguments: {prop}")
if provider == smmu and values[offset + 1] == 0x19C0:
raise ValueError(f"AP stream 0x19c0 already declared: {prop.parent}")
offset += nargs + 1
ipa_nodes = [p.parent for p in soc.glob("*/compatible")
if b"qcom,sdm660-ipa" in p.read_bytes().split(b"\0")]
if len(ipa_nodes) != 1:
raise ValueError("expected exactly one original IPA node")
ipa = ipa_nodes[0]
if (ipa / "clock-names").read_bytes() != b"core\0":
raise ValueError("unsupported IPA clocks")
clock = cells(ipa / "clocks")
if len(clock) != 2 or clock[1] != 0x44:
raise ValueError("expected RPM IPA core clock 0x44")
if clock[0] not in providers or cells(providers[clock[0]] / "#clock-cells") != (1,):
raise ValueError("invalid IPA clock provider")
if (soc / "platina-ipa-dma-test@147c0000").exists():
raise ValueError("the temporary test node already exists")
return f'''/dts-v1/;
/plugin/;
/* Generated from the inspected live DT. Do not reuse across different DTBs. */
/ {{
fragment@0 {{
target-path = "/soc@0";
__overlay__ {{
platina-ipa-dma-test@147c0000 {{
compatible = "xiaomi,platina-ipa-v2-dma-test";
reg = <0x147c0000 0x1000>, <0x14784000 0x26934>;
reg-names = "ipa", "bam";
iommus = <0x{smmu_id:x} 0x19c0>;
clocks = <0x{clock[0]:x} 0x44>;
clock-names = "core";
status = "okay";
}};
}};
}};
}};
'''
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--dt-root", type=Path, default=Path("/sys/firmware/devicetree/base"))
args = parser.parse_args()
try:
sys.stdout.write(generate(args.dt_root))
except (OSError, ValueError, struct.error) as error:
parser.exit(1, f"overlay generation refused: {error}\n")
if __name__ == "__main__":
main()