Repository navigation
topology2: validator: add a tool to check topology validity at build time #11344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kv2019i
wants to merge
4
commits into
thesofproject:main
Choose a base branch
from
kv2019i:202610-tplg-validator-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d462341
topology: validator: add topology binary parser and kernel token schema
kv2019i 605e7ec
topology: validator: add validation rules and command line tool
kv2019i 0c5133c
topology: cmake: validate topology2 binaries at build time
kv2019i af80e2f
.github: workflows: run topology validator tests and kernel schema check
kv2019i File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| --- | ||
| name: Topology validator kernel schema | ||
|
|
||
| # Checks that tools/topology/validator/schema/kernel_tokens.py still | ||
| # matches the SOF driver token tables in the kernel. When this fails, | ||
| # rerun schema/gen_kernel_tokens.py against a kernel tree and review the | ||
| # validator rules against the kernel changes. | ||
|
|
||
| # yamllint disable-line rule:truthy | ||
| on: | ||
| schedule: | ||
| - cron: '0 3 * * 1' | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| kernel-schema: | ||
| runs-on: ubuntu-26.04 | ||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| filter: 'tree:0' | ||
| path: sof | ||
|
|
||
| - name: Fetch SOF driver sources | ||
| run: | | ||
| git clone --depth 1 --filter=blob:none --sparse \ | ||
| --branch topic/sof-dev https://github.com/thesofproject/linux.git linux | ||
| git -C linux sparse-checkout set --no-cone \ | ||
| /include/uapi/sound/ /sound/soc/sof/ /sound/soc/soc-topology.c | ||
|
|
||
| - name: Check kernel token schema | ||
| run: python3 sof/tools/topology/validator/schema/gen_kernel_tokens.py | ||
| --kernel linux --check |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Topology validator (`tools/topology/validator`) | ||
|
|
||
| `tplg_validate.py` checks SOF IPC4 topology binaries (`.tplg`) offline, without | ||
| target hardware. It parses the binary and repeats what the Linux SOF driver | ||
| does when it loads the topology, so that problems show up at build time | ||
| instead of on a device. `alsatplg` accepts many topologies that the kernel | ||
| rejects; for instance it prints `undefined sink widget` for a dangling route | ||
| and still exits successfully. | ||
|
|
||
| ```bash | ||
| # check topologies, print errors and warnings | ||
| tplg_validate.py build_tools/topology/topology2/production/sof-mtl-rt711-4ch.tplg | ||
|
|
||
| # also print informational findings, check core IDs against a 5 core DSP | ||
| tplg_validate.py -v --num-cores 5 *.tplg | ||
|
|
||
| # list the rules and the kernel functions they mirror | ||
| tplg_validate.py --list-rules | ||
|
|
||
| # print the decoded topology with kernel token names | ||
| tplg_validate.py --dump foo.tplg | ||
| ``` | ||
|
|
||
| Exit status is 1 if any error is found (`--werror`: any warning). Python 3.8+ | ||
| and the `construct` module (`python3-construct`) are required. | ||
|
|
||
| ## Build integration | ||
|
|
||
| `add_alsatplg2_command` in `tools/topology/CMakeLists.txt` runs the validator | ||
| after `alsatplg` for every topology v2 target, unless configured with | ||
| `-DTPLG_VALIDATE=OFF` or `construct` is missing. A successful check writes a | ||
| `<topology>.tplg.validated` stamp file. An error fails the target and leaves no | ||
| stamp, so the check reruns on the next build. Changes to the validator sources | ||
| or to `known-issues.txt` recheck all topologies without rebuilding them. | ||
|
|
||
| Known errors are listed in `known-issues.txt` as `<topology glob> <rule>` | ||
| pairs and reported as warnings. Do not add entries to silence new errors: | ||
| fix the topology. Remove entries when a topology is fixed. | ||
|
|
||
| ## What is checked | ||
|
|
||
| Rules are grouped in layers by how closely they follow kernel code: | ||
|
|
||
| | Layer | Mirrors | Examples | | ||
| |-------|---------|----------| | ||
| | L1 | `sound/soc/soc-topology.c` | headers, element sizes, names, kcontrol handler IDs, TLV types | | ||
| | L2 | `sound/soc/sof/topology.c` tuple engine | kernel parse errors, tuple types, audio format blocks vs. counts | | ||
| | L3 | `topology.c`, `ipc4-topology.c` setup | routes, pipelines, chain DMA, DAI types, pins, bytes controls, PCM binding | | ||
| | L4 | `sof-audio.c` path walk, format selection | PCM formats vs. host copier formats (advisory) | | ||
|
|
||
| Every finding names the rule and the kernel function it follows, e.g. | ||
|
|
||
| ``` | ||
| foo.tplg: error: G001 route 'mixin.90.1' -> 'mixout.1.1': sink widget 'mixout.1.1' does not exist (kernel: snd_soc_dapm_add_routes, sof_route_load) | ||
| ``` | ||
|
|
||
| Severities: `error` means the kernel fails to load the topology, or fails | ||
| when the affected PCM is used. `warning` means a likely mistake, or a failure | ||
| that depends on runtime state the validator cannot know. `info` lists | ||
| observations (`-v`). | ||
|
|
||
| Not checked, because the data only exists on the target: firmware module | ||
| UUIDs and module types from the firmware manifest, ACPI NHLT blobs, machine | ||
| driver BE links, and the DSP's answers to IPC messages. | ||
|
|
||
| ## Keeping up with the kernel | ||
|
|
||
| The kernel driver is the reference. The validator follows it in two ways: | ||
|
|
||
| - **Generated token schema.** `schema/kernel_tokens.py` is generated from a | ||
| kernel tree by `schema/gen_kernel_tokens.py`. It holds the `SOF_TKN_*` IDs, the | ||
| `struct sof_topology_token` tables with token types, the per widget type | ||
| token lists of `tplg_ipc4_widget_ops`, the DAI type names and the kcontrol | ||
| handler IDs. `tplgparse/kernel_engine.py` emulates the kernel tuple parser | ||
| (`sof_parse_token_sets()`, `sof_copy_tuples()`, `sof_update_ipc_object()`) | ||
| on top of these tables. New tokens or widget types only need a | ||
| regeneration: | ||
|
|
||
| ```bash | ||
| schema/gen_kernel_tokens.py --kernel ~/linux # update kernel_tokens.py | ||
| schema/gen_kernel_tokens.py --kernel ~/linux --check # CI: fail on drift | ||
| ``` | ||
|
|
||
| - **Hand written rules** in `checks/`. Each rule names the kernel function it | ||
| mirrors (`--list-rules`). Changes that add or remove topology checks in | ||
| `sound/soc/sof/ipc4-topology.c` or `topology.c` should get a matching rule | ||
| update here. | ||
|
|
||
| `schema/check_tokens_conf.py` cross-checks `topology2/include/common/tokens.conf` | ||
| and the `token_ref` attributes of the topology2 classes against the kernel schema. | ||
|
|
||
| ## Layout | ||
|
|
||
| ``` | ||
| tplg_validate.py command line tool | ||
| tplgparse/binary.py ALSA topology binary format (ported from sof-test tplgtool2.py) | ||
| tplgparse/model.py typed view, vendor tuples decoded as written | ||
| tplgparse/kernel_engine.py kernel tuple engine emulation | ||
| schema/ generated kernel token schema and its generator | ||
| checks/ rules, one module per layer | ||
| tests/ pytest suite; tplgbuild.py builds test topologies | ||
| known-issues.txt waived errors | ||
| ``` | ||
|
|
||
| Run the tests with `pytest tools/topology/validator/tests`. Set `ALSATPLG` | ||
| to select the `alsatplg` binary for the integration tests and `KERNEL_TREE` | ||
| to include the schema drift check. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in V3.