diff --git a/.github/workflows/codestyle.yml b/.github/workflows/codestyle.yml index 62251271221c..ae35350d911e 100644 --- a/.github/workflows/codestyle.yml +++ b/.github/workflows/codestyle.yml @@ -49,3 +49,14 @@ jobs: - name: no kernel-only headers in user-space code # see scripts/check_kernel_includes.py run: python3 scripts/check_kernel_includes.py --sources --strict + + platform-headers: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + filter: 'tree:0' + + - name: no new unused platform headers or macros + # see scripts/check_platform_headers.py + run: python3 scripts/check_platform_headers.py --strict diff --git a/scripts/check_platform_headers.py b/scripts/check_platform_headers.py new file mode 100755 index 000000000000..ef5bf166acba --- /dev/null +++ b/scripts/check_platform_headers.py @@ -0,0 +1,396 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2026 Intel Corporation. +"""Report platform headers that nothing includes and platform macros that nothing uses. + +The platform headers in src/kernel/platform//include and +src/platform//include give the generic SOF code its view of a +platform. Over time they collected headers and macros that no code reads +any more. This script finds them in the source tree, without building: + +- a header is unused when no code that can be built for its platform can + include it (following #include from the platform's sources, with the + platform's include path; #if is not evaluated); +- a macro is unused when its name appears nowhere in code that can be + built together with its platform's headers: the generic SOF code + (src/ outside the platform and vendor driver directories), the Zephyr + glue (zephyr/, app/) and the platform's own platform and vendor driver + directories. The host library platform sees the host-only code + (posix/, tools/, test/cmocka/, the host arch) instead of the Zephyr + glue, and the posix platform also sees the ztest unit tests. Text + matching counts every #if branch, so the result does not depend on the + configuration. Macros only used by unused macros are unused too. + +Not reported: include guards, names built by token pasting, the _BASE or +_SIZE (_START or _END) partner of a used macro, the other macros of a +mailbox window with a used member, and the vendor-generated Xtensa core +headers in include/arch/. + +The platforms, the directories they share and their vendor driver +directories are listed in scripts/platform_headers.txt. Its [baseline] +section lists the known findings. By default the script only reports. +With --strict it exits with an error on a finding that is not in the +baseline, so new unused headers and macros fail CI; the baseline may +only shrink. Baseline entries that are no longer found are reported so +that they can be dropped. --update-baseline rewrites the baseline with +the current findings. + +--contract prints, for each platform header include name, the macros +that generic code uses from it: what a platform has to provide. + +Usage: + check_platform_headers.py [--strict] [--summary] + check_platform_headers.py --update-baseline + check_platform_headers.py --contract +""" + +import argparse +import fnmatch +import os +import posixpath +import re +import subprocess +import sys +from collections import defaultdict + +SOF_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +DEFAULT_MANIFEST = os.path.join(SOF_ROOT, 'scripts', 'platform_headers.txt') + +KP = 'src/kernel/platform/' +UP = 'src/platform/' +HOST = ('posix/', 'tools/', 'test/cmocka/', 'src/kernel/arch/host/', 'src/kernel/drivers/host/') +CODE_EXTS = ('.c', '.h', '.cpp', '.cc', '.hpp', '.S', '.ld', '.ld.in', '.x.in') +SOURCE_EXTS = ('.c', '.cpp', '.cc', '.S') +# include directories that every build has, besides the platform ones, in the order the +# builds search them: the zephyr/include and posix/include wrappers come first +FW_INCLUDE = ['zephyr/include', 'src/include', 'src/kernel/include', 'third_party/include', + 'src/kernel/arch/xtensa/include', 'tools/rimage/src/include'] +HOST_INCLUDE = ['posix/include', 'src/include', 'src/kernel/include', 'third_party/include', + 'src/kernel/arch/host/include', 'tools/testbench/include'] + +IDENT = re.compile(r'[A-Za-z_]\w*') +DEFINE = re.compile(r'^\s*#\s*define\s+([A-Za-z_]\w*)') +IFNDEF = re.compile(r'^\s*#\s*(?:ifndef|undef)\s+([A-Za-z_]\w*)') +INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]+)[>"]', re.M) +# a name prefix pasted with ## ("PREFIX_ ## x"); a bare parameter ("x ## _suffix") is not one +PASTE = re.compile(r'([A-Za-z_]\w*_)\s*##') +PARTNERS = (('_BASE', '_SIZE'), ('_SIZE', '_BASE'), ('_START', '_END'), ('_END', '_START')) + + +def parse_manifest(path): + """Return {section: [line, ...]} from a '[section]' + one entry per line file.""" + sections = defaultdict(list) + section = None + with open(path, encoding='utf-8') as f: + for line in f: + line = line.split('#', 1)[0].strip() + if not line: + continue + if line.startswith('[') and line.endswith(']'): + section = line[1:-1] + continue + if section is None: + sys.exit(f'{path}: entry "{line}" outside of a [section]') + sections[section].append(line) + return sections + + +def git_files(): + out = subprocess.run(['git', 'ls-files'], cwd=SOF_ROOT, check=True, + capture_output=True, text=True).stdout + return [f for f in out.splitlines() + if f.endswith(CODE_EXTS) and os.path.isfile(os.path.join(SOF_ROOT, f))] + + +class Source: + """Macro definitions, identifier uses and includes of one file, comments removed.""" + + def __init__(self, path): + with open(os.path.join(SOF_ROOT, path), encoding='utf-8', errors='replace') as f: + raw = f.read() + text = re.sub(r'/\*.*?\*/', lambda m: '\n' * m.group(0).count('\n'), raw, flags=re.S) + text = re.sub(r'//[^\n]*', '', text) + self.includes = INCLUDE.findall(text) + self.defines = defaultdict(list) # name -> [line] + self.bodies = defaultdict(str) # name -> body text + self.uses = [] # (name, macro whose body has it, or None) + self.guards = set() + self.pasted = set() # prefixes pasted with ## + lines = text.split('\n') + cur = None + for i, line in enumerate(lines, 1): + m = DEFINE.match(line) + if m: + cur = m.group(1) + self.defines[cur].append(i) + prev = next((l for l in reversed(lines[:i - 1]) if l.strip()), '') + if re.match(r'\s*#\s*ifndef\s+' + cur + r'\b', prev): + self.guards.add(cur) + body = line[m.end():] + else: + body = line + if cur: + self.bodies[cur] += body + '\n' + self.pasted.update(p for p in PASTE.findall(body) if len(p) >= 3) + skip = IFNDEF.match(line) + for t in IDENT.findall(body): + self.uses.append((t, '#ifndef' if skip and t == skip.group(1) else cur)) + if not line.rstrip().endswith('\\'): + cur = None + + +class Tree: + def __init__(self, manifest): + self.m = parse_manifest(manifest) + self.files = git_files() + self.fileset = set(self.files) + self.cache = {} + self.dirs = sorted({re.sub(r'/include/.*', '', f[len(p):]) for f in self.files + for p in (KP, UP) if f.startswith(p) and '/include/' in f}) + self.groups = {} + shared = set() + for line in self.m['groups']: + name, dirs = line.split(':', 1) + self.groups[name.strip()] = dirs.split() + shared.update(dirs.split()) + for d in self.dirs: + if d not in shared and d not in self.groups: + self.groups[d] = [d] + self.vendor = [] # (group name pattern, driver directory) + for line in self.m['vendor']: + pat, d = line.split(':', 1) + self.vendor.append((pat.strip(), d.strip().rstrip('/') + '/')) + self.host = set(self.m['host']) + self.posix = set(self.m['posix']) + self.vendor_dirs = tuple(sorted({d for _, d in self.vendor})) + self.index = None + + def src(self, path): + if path not in self.cache: + self.cache[path] = Source(path) + return self.cache[path] + + def own(self, f, g): + return any(f.startswith((KP + d + '/', UP + d + '/')) for d in self.groups[g]) + + def generic(self, f): + return f.startswith(('src/', 'zephyr/', 'app/')) and \ + not f.startswith((KP, UP) + self.vendor_dirs + HOST) + + def visible(self, f, g): + """Can f be built together with the headers of platform group g?""" + if self.own(f, g): + return True + if any(fnmatch.fnmatchcase(g, pat) and f.startswith(d) for pat, d in self.vendor): + return True + if g in self.host: + return (self.generic(f) and not f.startswith(('zephyr/', 'app/'))) or f.startswith(HOST) + if g in self.posix and f.startswith('test/ztest/'): + return True + return self.generic(f) + + def headers(self, g): + return [f for f in self.files if self.own(f, g) and f.endswith('.h') and '/include/arch/' not in f] + + def include_path(self, g): + dirs = [] + for d in self.groups[g]: + dirs += [KP + d + '/include', UP + d + '/include'] + return dirs + (HOST_INCLUDE if g in self.host else FW_INCLUDE) + + def unreachable(self, g): + path = self.include_path(g) + seen = set() + stack = [f for f in self.files if f.endswith(SOURCE_EXTS) and self.visible(f, g)] + while stack: + f = stack.pop() + if f in seen: + continue + seen.add(f) + for q, name in self.src(f).includes: + cands = [posixpath.normpath(posixpath.join(posixpath.dirname(f), name))] if q == '"' else [] + cands += [posixpath.normpath(posixpath.join(d, name)) for d in path] + for c in cands: + if c in self.fileset: + stack.append(c) + break + return [h for h in self.headers(g) if h not in seen] + + def uses_index(self): + """{platform macro name: [(file, enclosing macro)]} over the whole tree""" + if self.index is None: + names = set() + for f in self.files: + if f.startswith((KP, UP)) and f.endswith('.h'): + names.update(self.src(f).defines) + self.index = defaultdict(list) + for f in self.files: + for t, enc in self.src(f).uses: + if t in names: + self.index[t].append((f, enc)) + return self.index + + def macros(self, g): + """-> ({unused name: [header]}, used names, {name: [header]}) for group g""" + hdrs = self.headers(g) + cand = defaultdict(list) + for h in hdrs: + s = self.src(h) + for n in s.defines: + if n not in s.guards: + cand[n].append(h) + vis = {f for f in self.files if self.visible(f, g)} + pasted = set() + for f in vis: + pasted |= self.src(f).pasted + uses = defaultdict(list) + index = self.uses_index() + for n in cand: + for f, enc in index[n]: + if f not in vis: + continue + if enc == '#ifndef': + if n in self.src(f).defines: + continue # a guard or a duplicate-definition guard + enc = None + uses[n].append(enc) + alive = {n for n in cand if any(n.startswith(p) for p in pasted)} + changed = True + while changed: + changed = False + for n in cand: + if n not in alive and any(e is None or (e != n and (e not in cand or e in alive)) + for e in uses[n]): + alive.add(n) + changed = True + dead = set(cand) - alive + # keep the partner of a used macro, and the rest of a used mailbox window, + # and anything a kept macro needs + kept = set() + for n in dead: + for a, b in PARTNERS: + if n.endswith(a) and n[:-len(a)] + b in alive: + kept.add(n) + if any('/lib/mailbox' in h for h in cand[n]): + w = re.sub(r'_(BASE|SIZE|OFFSET)$', '', n) + if any(re.sub(r'_(BASE|SIZE|OFFSET)$', '', x) == w for x in alive): + kept.add(n) + changed = True + while changed: + changed = False + for k in list(kept): + for h in cand[k]: + for t in IDENT.findall(self.src(h).bodies.get(k, '')): + if t in dead and t not in kept: + kept.add(t) + changed = True + dead -= kept + return {n: cand[n] for n in dead}, alive, cand + + def findings(self): + hdr_groups, unused_in = defaultdict(set), defaultdict(set) + def_groups, dead_in = defaultdict(set), defaultdict(set) + for g in self.groups: + for h in self.headers(g): + hdr_groups[h].add(g) + for h in self.unreachable(g): + unused_in[h].add(g) + dead, _, cand = self.macros(g) + for n, hs in cand.items(): + for h in hs: + def_groups[(h, n)].add(g) + for n, hs in dead.items(): + for h in hs: + dead_in[(h, n)].add(g) + # a header or macro shared by several platforms is only unused if it is in all of them + hdrs = {h for h, gs in unused_in.items() if gs == hdr_groups[h]} + macros = {f'{h}:{n}' for (h, n), gs in dead_in.items() if gs == def_groups[(h, n)] and h not in hdrs} + return hdrs | macros + + def contract(self): + """{include name: (macros firmware code uses, macros only host code uses)}""" + def alive_in(files): + """names used by plain code in files, or by a macro of files that is""" + uses = defaultdict(list) + for f in files: + src = self.src(f) + for t, enc in src.uses: + if enc == '#ifndef': + if t in src.defines: + continue + enc = None + uses[t].append(enc) + alive = {t for t, es in uses.items() if None in es} + changed = True + while changed: + changed = False + for t, es in uses.items(): + if t not in alive and any(e in alive for e in es): + alive.add(t) + changed = True + return alive + + fw = alive_in([f for f in self.files if self.generic(f)]) + host = alive_in([f for f in self.files + if (self.generic(f) and not f.startswith(('zephyr/', 'app/'))) or f.startswith(HOST)]) + res = defaultdict(lambda: (set(), set())) + for g in self.groups: + for h in self.headers(g): + name = h.split('/include/', 1)[1] + src = self.src(h) + for n in src.defines: + if n in src.guards: + continue + if n in fw: + res[name][0].add(n) + elif n in host: + res[name][1].add(n) + return res + + +def write_baseline(manifest, findings): + with open(manifest, encoding='utf-8') as f: + text = f.read() + head = text.split('[baseline]', 1)[0] + body = '\n'.join(sorted(findings)) + with open(manifest, 'w', encoding='utf-8') as f: + f.write(head + '[baseline]\n' + body + ('\n' if body else '')) + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.split('\n\n')[0]) + ap.add_argument('--strict', action='store_true', help='fail on findings that are not in the baseline') + ap.add_argument('--summary', action='store_true', help='only print the counts') + ap.add_argument('--update-baseline', action='store_true', help='rewrite the baseline with the current findings') + ap.add_argument('--contract', action='store_true', help='print the macros generic code uses, per header') + ap.add_argument('--manifest', default=DEFAULT_MANIFEST) + args = ap.parse_args() + + tree = Tree(args.manifest) + if args.contract: + for name, (fw, host) in sorted(tree.contract().items()): + print(f'{name}: {" ".join(sorted(fw)) or "-"}' + (f'; host only: {" ".join(sorted(host))}' if host else '')) + return 0 + + found = tree.findings() + if args.update_baseline: + write_baseline(args.manifest, found) + print(f'{len(found)} findings written to the baseline') + return 0 + + baseline = set(tree.m['baseline']) + new = sorted(found - baseline) + stale = sorted(baseline - found) + if not args.summary: + for f in new: + kind = 'macro' if ':' in f else 'header' + print(f'unused platform {kind}: {f}') + for f in stale: + print(f'no longer found, drop from the baseline: {f}') + print(f'{len(found)} unused platform headers and macros, {len(new)} not in the baseline, ' + f'{len(stale)} baseline entries no longer found') + return 1 if args.strict and new else 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/scripts/platform_headers.txt b/scripts/platform_headers.txt new file mode 100644 index 000000000000..8efdf4006de3 --- /dev/null +++ b/scripts/platform_headers.txt @@ -0,0 +1,145 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Platform layout for scripts/check_platform_headers.py. +# +# A platform is a directory with include/ under src/kernel/platform/ and +# src/platform/ (both trees use the same names). Every such directory is a +# platform of its own, unless it is listed in [groups]. + +# platform: the platform header directories its builds use together, as +# zephyr/CMakeLists.txt puts them on the include path +[groups] +tigerlake: tigerlake intel/cavs +meteorlake: meteorlake intel/ace +lunarlake: lunarlake intel/ace +ace30: ace30 intel/ace +novalake: novalake intel/ace +rembrandt: amd/rembrandt amd/common +acp_7_0: amd/acp_7_0 amd/common +acp_7_x: amd/acp_7_x amd/common + +# platform name pattern: the vendor driver directory its builds compile +[vendor] +imx*: src/kernel/drivers/imx/ +rembrandt: src/kernel/drivers/amd/ +acp_7_*: src/kernel/drivers/amd/ +mtk: src/kernel/drivers/mediatek/ + +# platforms of the host builds (host library, testbench, cmocka): they see +# the host-only code and not the Zephyr glue +[host] +library + +# platforms of the native_sim builds: they also see the ztest unit tests +[posix] +posix + +# Known unused headers ("path") and macros ("path:NAME"). This list may only +# shrink: check_platform_headers.py --strict fails on findings that are not +# here. Regenerate with check_platform_headers.py --update-baseline. +[baseline] +src/kernel/platform/ace30/include/platform/lib/clk.h:CPU_LPRO_FREQ_IDX +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:BT_RX_FIFO_OFFST +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:BT_TX_FIFO_OFFST +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:DAI_BASE +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:DAI_BASE_REM +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:DAI_SIZE +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:DRAM0_BASE +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:DRAM0_SIZE +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:HEAP_BUFFER_BLOCK_SIZE +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:HEAP_SYSTEM_0_BASE +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:HS_RX_FIFO_OFFST +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:HS_TX_FIFO_OFFST +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:PLATFORM_HEAP_BUFFER +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:PLATFORM_HEAP_RUNTIME +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:PLATFORM_HEAP_SYSTEM +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:PLATFORM_HEAP_SYSTEM_RUNTIME +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:SOF_MAILBOX_SIZE +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:SOF_STACK_TOTAL_SIZE +src/kernel/platform/amd/acp_7_0/include/platform/lib/memory.h:SRAM1_SIZE +src/kernel/platform/amd/acp_7_0/include/platform/platform.h:PLATFORM_IPC_INTERRUPT +src/kernel/platform/amd/acp_7_0/include/platform/platform_misc.h:CLK7_CLK2_BYPASS_CNTL +src/kernel/platform/amd/acp_7_0/include/platform/platform_misc.h:CLK7_CLK2_CURRENT_CNT +src/kernel/platform/amd/acp_7_0/include/platform/platform_misc.h:CLK7_CLK2_DFS_STATUS +src/kernel/platform/amd/acp_7_0/include/platform/platform_misc.h:MP1_SMN_C2PMSG_69 +src/kernel/platform/amd/acp_7_0/include/platform/platform_misc.h:MP1_SMN_C2PMSG_85 +src/kernel/platform/amd/acp_7_0/include/platform/platform_misc.h:MP1_SMN_C2PMSG_93 +src/kernel/platform/amd/acp_7_x/include/platform/lib/memory.h:DRAM0_BASE +src/kernel/platform/amd/acp_7_x/include/platform/lib/memory.h:DRAM0_SIZE +src/kernel/platform/amd/acp_7_x/include/platform/lib/memory.h:DRAM1_BASE +src/kernel/platform/amd/acp_7_x/include/platform/lib/memory.h:DRAM1_SIZE +src/kernel/platform/amd/acp_7_x/include/platform/lib/memory.h:HEAP_BUFFER_BLOCK_SIZE +src/kernel/platform/amd/acp_7_x/include/platform/lib/memory.h:HEAP_SYSTEM_0_BASE +src/kernel/platform/amd/acp_7_x/include/platform/lib/memory.h:PLATFORM_HEAP_BUFFER +src/kernel/platform/amd/acp_7_x/include/platform/lib/memory.h:PLATFORM_HEAP_RUNTIME +src/kernel/platform/amd/acp_7_x/include/platform/lib/memory.h:PLATFORM_HEAP_SYSTEM +src/kernel/platform/amd/acp_7_x/include/platform/lib/memory.h:PLATFORM_HEAP_SYSTEM_RUNTIME +src/kernel/platform/amd/acp_7_x/include/platform/lib/memory.h:SOF_MAILBOX_SIZE +src/kernel/platform/amd/acp_7_x/include/platform/lib/memory.h:SRAM1_SIZE +src/kernel/platform/amd/acp_7_x/include/platform/platform.h:PLATFORM_IPC_INTERRUPT +src/kernel/platform/amd/acp_7_x/include/platform/platform_misc.h:ACP_AUDIOPLL_POWER_OFF_REQ +src/kernel/platform/amd/acp_7_x/include/platform/platform_misc.h:ACP_AUDIOPLL_POWER_ON_REQ_WITH_WOV_EN +src/kernel/platform/amd/acp_7_x/include/platform/platform_misc.h:ACP_CLK_BYPASS_SEL_AUDIO_PLL +src/kernel/platform/amd/acp_7_x/include/platform/platform_misc.h:ACP_CLK_BYPASS_SEL_LPPLL +src/kernel/platform/amd/acp_7_x/include/platform/platform_misc.h:ACP_CLK_DIV_BY_1 +src/kernel/platform/amd/acp_7_x/include/platform/platform_misc.h:ACP_CLK_DIV_BY_16 +src/kernel/platform/amd/acp_7_x/include/platform/platform_misc.h:ACP_DEEPSLEEP_ENABLE +src/kernel/platform/amd/acp_7_x/include/platform/platform_misc.h:ACP_FUTURE_REG_ACLK_1 +src/kernel/platform/amd/acp_7_x/include/platform/platform_misc.h:ACP_FUTURE_REG_ACLK_2 +src/kernel/platform/amd/acp_7_x/include/platform/platform_misc.h:ACP_FUTURE_REG_ACLK_3 +src/kernel/platform/amd/acp_7_x/include/platform/platform_misc.h:ACP_FUTURE_REG_ACLK_4 +src/kernel/platform/amd/common/include/platform/lib/dma.h:DMA_ID_DAI_HS_VIRTUAL +src/kernel/platform/amd/common/include/platform/lib/dma.h:DMA_ID_DAI_SW_AUDIO +src/kernel/platform/amd/common/include/platform/lib/dma.h:DMA_ID_HOST +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_BTTDM_RXFRMT +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_BTTDM_TXFRMT +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_DMA_CH_GROUP +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_DMA_CH_RST_STS +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_DMA_CUR_DSCR_0 +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_DMA_CUR_TRANS_CNT_0 +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_EXTERNAL_INTR_CNTL +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_EXTERNAL_INTR_CNTL1 +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_EXTERNAL_INTR_STAT +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_EXTERNAL_INTR_STAT1 +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_I2STDM0_MSTRCLKGEN +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_I2STDM1_MSTRCLKGEN +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_I2STDM_REFCLKGEN +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_I2STDM_RXFRMT +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_I2STDM_TXFRMT +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_P1_BT_RX_LINKPOSITIONCNTR +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_P1_BT_TX_LINKPOSITIONCNTR +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_P1_HS_RX_LINKPOSITIONCNTR +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_P1_HS_TX_LINKPOSITIONCNTR +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_P1_I2S_RX_LINKPOSITIONCNTR +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_P1_I2S_TX_LINKPOSITIONCNTR +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_PDM_CLKDIV +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_PDM_VAD_DYNAMIC_CLK_GATING_EN +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_WOV_BUFFER_STATUS +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_WOV_ERROR_STATUS_REGISTER +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_WOV_PDM_VAD_CTRL +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_WOV_RX_LINEARPOSITIONCNTR_HIGH +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_WOV_RX_LINEARPOSITIONCNTR_LOW +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_WOV_RX_LINKPOSITIONCNTR +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:ACP_WOV_WAKE +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:MP0_C2PMSG_114 +src/kernel/platform/amd/rembrandt/include/platform/chip_offset_byte.h:MP0_C2PMSG_73 +src/kernel/platform/amd/rembrandt/include/platform/lib/memory.h:DRAM1_BASE +src/kernel/platform/amd/rembrandt/include/platform/lib/memory.h:DRAM1_SIZE +src/kernel/platform/amd/rembrandt/include/platform/lib/memory.h:HEAP_BUFFER_BLOCK_SIZE +src/kernel/platform/amd/rembrandt/include/platform/lib/memory.h:HEAP_SYSTEM_0_BASE +src/kernel/platform/amd/rembrandt/include/platform/lib/memory.h:PLATFORM_HEAP_BUFFER +src/kernel/platform/amd/rembrandt/include/platform/lib/memory.h:PLATFORM_HEAP_RUNTIME +src/kernel/platform/amd/rembrandt/include/platform/lib/memory.h:PLATFORM_HEAP_SYSTEM +src/kernel/platform/amd/rembrandt/include/platform/lib/memory.h:PLATFORM_HEAP_SYSTEM_RUNTIME +src/kernel/platform/amd/rembrandt/include/platform/lib/memory.h:SOF_MAILBOX_SIZE +src/kernel/platform/amd/rembrandt/include/platform/lib/memory.h:SOF_STACK_TOTAL_SIZE +src/kernel/platform/amd/rembrandt/include/platform/lib/memory.h:SRAM1_SIZE +src/kernel/platform/amd/rembrandt/include/platform/platform.h:PLATFORM_IPC_INTERRUPT +src/kernel/platform/intel/cavs/include/cavs/drivers/sideband-ipc.h:IPC_DIPCIDR_MSG_MASK +src/kernel/platform/mtk/include/platform/lib/memory.h:PLATFORM_HEAP_BUFFER +src/kernel/platform/mtk/include/platform/lib/memory.h:PLATFORM_HEAP_RUNTIME +src/kernel/platform/mtk/include/platform/lib/memory.h:PLATFORM_HEAP_SYSTEM +src/kernel/platform/mtk/include/platform/lib/memory.h:PLATFORM_HEAP_SYSTEM_RUNTIME +src/kernel/platform/novalake/include/platform/lib/clk.h:CPU_LPRO_FREQ_IDX +src/platform/qemu_xtensa/include/platform/lib/mailbox.h:MAILBOX_TRACE_BASE +src/platform/qemu_xtensa/include/platform/lib/mailbox.h:MAILBOX_TRACE_SIZE diff --git a/src/kernel/platform/README.md b/src/kernel/platform/README.md index 77112340a10e..7304b9dbafd7 100644 --- a/src/kernel/platform/README.md +++ b/src/kernel/platform/README.md @@ -9,3 +9,36 @@ This directory contains SoC/platform-specific integration (e.g. Intel ACE, Media including the Kconfig that selects the build target such as `PANTHERLAKE` (PTL). It configures the hardware the audio application later runs on top of, but is not itself part of the user-space application. + +## Platform header contract + +Generic code reaches a platform through fixed include names. The build puts +`src/kernel/platform//include` and `src/platform//include` on the include +path, plus any shared directories (`intel/ace`, `intel/cavs`, `amd/common`). A platform only +has to provide what the generic code uses: + +| Include name | Included by | What generic code uses | +|---|---|---| +| `platform/platform.h` | `sof/kernel/platform.h` | `HOST_PAGE_SIZE`, `PLATFORM_PAGE_TABLE_SIZE`, `HW_CFG_VERSION`; `platform_wait_for_interrupt()`, `ipc_platform_send_msg()`, `ipc_platform_send_msg_direct()` | +| `platform/lib/memory.h` | `sof/lib/memory.h` | `PLATFORM_DCACHE_ALIGN`, `HOST_PAGE_SIZE`, `SOF_STACK_SIZE`, `host_to_local()`, `local_to_host()`; where the platform has them: the heap sizes (`HEAPMEM_SIZE`, `HEAP_*_SIZE`), `SRAM_BANK_SIZE`, `LP_SRAM_SIZE`, `IMR_BOOT_LDR_MANIFEST_BASE`, `DMA0_BASE`/`DMA0_SIZE`, `MU_BASE` | +| `platform/lib/clk.h` | `rtos/clk.h` | `NUM_CLOCKS`, `NUM_CPU_FREQ`, `CPU_DEFAULT_IDX`, `CPU_LOWEST_FREQ_IDX`, `CLK_MAX_CPU_HZ`, `CLK_SSP`, `PRIMARY_CORE_BASE_CPS_USAGE`, `SECONDARY_CORE_BASE_CPS_USAGE` | +| `platform/lib/mailbox.h`, `platform/lib/mailbox_layout.h` (in `src/platform`) | `sof/lib/mailbox.h` | the host windows `MAILBOX_{DSPBOX,HOSTBOX,STREAM}_{BASE,SIZE}`, `MAILBOX_DEBUG_BASE`, `MAILBOX_SW_REG_{BASE,SIZE}`, and where used `SRAM_OUTBOX_SIZE`, `SRAM_STREAM_BASE`; `mailbox_sw_reg_read()`, `mailbox_sw_reg_read64()`, `mailbox_sw_reg_write()`, `mailbox_sw_regs_write()` | +| `platform/lib/dma.h` | `sof/lib/dma.h`, only for the legacy DMA domain schedulers | `PLATFORM_NUM_DMACS`, `PLATFORM_MAX_DMA_CHAN`, `dma_chan_irq()`, `dma_chan_irq_name()` | +| `platform/lib/dai.h` | `sof/lib/dai-legacy.h`, only without `CONFIG_ZEPHYR_NATIVE_DRIVERS` | nothing | +| `platform/drivers/interrupt.h` | `rtos/interrupt-legacy.h`, only for AMD without native drivers | `PLATFORM_IRQ_HW_NUM`, `PLATFORM_IRQ_CHILDREN`, `PLATFORM_IRQ_FIRST_CHILD` | + +The host builds (host library, testbench, cmocka) use the `library` platform through the +`posix/include` wrappers. Only those builds need `platform/lib/cpu.h` +(`PLATFORM_PRIMARY_CORE_ID`), `platform/drivers/idc.h` (`idc_init()`, `idc_send_msg()`), +`platform/lib/ll_schedule.h` and the host allocator's `PLATFORM_HEAP_*` counts. + +A new platform does not need any other header or macro. In particular it does not need +`platform/trace/trace.h`, `platform/lib/pm_runtime.h`, the DMA trace settings or an XTOS memory +layout. + +`scripts/check_platform_headers.py` lists platform headers that nothing can include and +platform macros that nothing can use, from the source tree. `--contract` prints the table above +from the current code. CI runs it with `--strict`: the known findings are listed in the +`[baseline]` of `scripts/platform_headers.txt`, which may only shrink, so new unused headers and +macros fail. A new platform whose builds use more than its own directory, or that has vendor +drivers outside it, is listed in the `[groups]` and `[vendor]` sections of the same file.