diff --git a/drivers/soc/apple/sleep-t6050.h b/drivers/soc/apple/sleep-t6050.h index dd825710ed755..2913afd35e4cc 100644 --- a/drivers/soc/apple/sleep-t6050.h +++ b/drivers/soc/apple/sleep-t6050.h @@ -168,6 +168,8 @@ struct apple_sleep_region_state { /* The AP wake-enable words, on this die and the GFX die */ void __iomem *wake, *wake_gfx, *aic_gfx; u32 wake_enabled[11], wake_saved[11]; + u32 wake_pending[11], wake_masks_at_return[11]; + bool wake_snapshot_valid; bool wake_armed, wake_sources; u32 spmi_mask[6]; bool spmi_masked; @@ -1593,6 +1595,7 @@ static int t6050_sleep_begin(struct apple_sleep *s) unsigned int i; int ret; + r->wake_snapshot_valid = false; t6050_sleep_pins_low(s); r->n1_up_ret = 0; r->n1_host = t6050_n1_host_bound(s); @@ -1980,6 +1983,44 @@ static void t6050_floor_steps(struct apple_sleep *s) */ #define T6050_WAKE_ENABLE 0x0c #define T6050_WAKE_WORDS 10 +#define T6050_WAKE_STATUS 0x34 +#define T6050_WAKE_GFX_STATUS 0x10 + +/* Read-only: snapshot before PMU/SPMI acknowledgment can remove a wake level. */ +static void t6050_wake_snapshot(struct apple_sleep *s) +{ + struct apple_sleep_region_state *r = s->region; + unsigned int i; + + r->wake_snapshot_valid = false; + if (!r->wake_armed || !r->wake || !r->wake_gfx) + return; + for (i = 0; i < T6050_WAKE_WORDS; i++) { + r->wake_pending[i] = readl(r->wake + T6050_WAKE_STATUS + 4 * i); + r->wake_masks_at_return[i] = readl(r->wake + T6050_WAKE_ENABLE + 4 * i); + } + r->wake_pending[T6050_WAKE_WORDS] = readl(r->wake_gfx + T6050_WAKE_GFX_STATUS); + r->wake_masks_at_return[T6050_WAKE_WORDS] = readl(r->wake_gfx + T6050_WAKE_ENABLE); + r->wake_snapshot_valid = true; +} + +static void t6050_report_wake_snapshot(struct apple_sleep *s) +{ + struct apple_sleep_region_state *r = s->region; + + if (!s->returned) + return; + if (!r->wake_snapshot_valid) { + dev_warn(s->dev, "wake register snapshot unavailable\n"); + return; + } + dev_info(s->dev, "wake pending AP %*ph GFX %#x\n", + (int)(T6050_WAKE_WORDS * sizeof(u32)), r->wake_pending, + r->wake_pending[T6050_WAKE_WORDS]); + dev_info(s->dev, "wake masks AP %*ph GFX %#x\n", + (int)(T6050_WAKE_WORDS * sizeof(u32)), r->wake_masks_at_return, + r->wake_masks_at_return[T6050_WAKE_WORDS]); +} static void t6050_wake_masks(struct apple_sleep *s) { @@ -3259,6 +3300,7 @@ static void t6050_sleep_wake_regs(struct apple_sleep *s) unsigned int i; u32 value; + t6050_wake_snapshot(s); if (r->spmi_masked) { /* What the PMU sent while the SoC slept (its wake), before the run-time mask */ t6050_spmi_pmu_ack(s); @@ -3340,6 +3382,7 @@ static void t6050_sleep_finish(struct apple_sleep *s) r->boot_stage_written = false; t6050_sleep_ptd_ownership(s, false); r->usb_aon_kept = 0; + t6050_report_wake_snapshot(s); dev_dbg(s->dev, "entry %s: replay %u, timeouts %u, late %u, PMU %u/%d, DART stops %#x\n", r->committed ? "returned" : "refused", s->ps_rewoken, s->ps_rewake_timeouts, r->late_errors, r->pmu_errors, ret, r->dart_done); diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 30e443d34270d..30b7ed474a613 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -263,6 +263,8 @@ def mutations(flag, names, base, expect=KILL): 't6040-calibration-tests.py': [([], PASS)], 't604x-cpu-order-tests.py': [(out('--output') + ['--dtc', '{dtc}'], PASS)], 't8132-usb4-policy-tests.py': [(out('--output') + ['--dtc', '{dtc}'], PASS)], + 't6050-wake-reporting-tests.py': [(out('--output'), PASS), + (out('--output') + ['--negative-controls'], PASS)], 'tb-cio-sleep-tests.py': [(out('--output'), PASS)], 'tb-dock-pm-tests.py': [(out('--output'), PASS)], 'tb-dp-repair-tests.py': [(out('--output'), PASS), (out('--output') + ['--mutations'], PASS)], diff --git a/tools/asahi/t6050-wake-report.py b/tools/asahi/t6050-wake-report.py new file mode 100755 index 0000000000000..a2924c68714fc --- /dev/null +++ b/tools/asahi/t6050-wake-report.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Decode one production pending/mask snapshot; no hardware or hold qualification.""" +import argparse +import json +from pathlib import Path +import re +import struct + + +def parse(text): + if 'wake register snapshot unavailable' in text: + raise ValueError('explicitly unavailable wake snapshot') + words = {} + for label in ('pending', 'masks'): + matches = re.findall(r'\bwake ' + label + r' AP ([0-9a-fA-F ]+) GFX (0x[0-9a-fA-F]+|0)\s*$', text, re.M) + if len(matches) != 1: + raise ValueError('require exactly one ' + label + ' record in this row interval') + raw = bytes.fromhex(matches[0][0]) + gfx = int(matches[0][1], 0) + if len(raw) != 40 or not 0 <= gfx <= 0xffffffff: + raise ValueError('incomplete AP10/GFX1 word snapshot') + # The reporting candidate is J714s little-endian ARM64, like the retained TEST dump. + words[label] = list(struct.unpack('<10I', raw)) + [gfx] + + def ids(values): + return [32 * word + bit for word, value in enumerate(values) + for bit in range(32) if value & (1 << bit)] + + pending, masks = words['pending'], words['masks'] + return {'pending_words': pending, 'mask_words': masks, + 'raw_pending_ids': ids(pending), + 'enabled_pending_ids': ids([value & ~mask for value, mask in zip(pending, masks)]), + 'masked_pending_ids': ids([value & mask for value, mask in zip(pending, masks)]), + 'scope': 'sequential early-wake pending/mask observation; not cause, final-commit, GPIO, SMC or hold proof'} + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('row_kernel_log', type=Path) + args = parser.parse_args() + print(json.dumps(parse(args.row_kernel_log.read_text()), indent=2)) diff --git a/tools/asahi/t6050-wake-reporting-tests.py b/tools/asahi/t6050-wake-reporting-tests.py new file mode 100755 index 0000000000000..8ebcdbd60df97 --- /dev/null +++ b/tools/asahi/t6050-wake-reporting-tests.py @@ -0,0 +1,253 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Compile this repository's T6050 wake-report callbacks with controlled MMIO. + +The test reads drivers/soc/apple/sleep-t6050.h from its own checkout, never +from a frozen proposal. Tests are source/host checks, not hardware qualification. +""" +import argparse +import hashlib +import importlib.util +import json +import os +from pathlib import Path +import re +import resource +import signal +import shlex +import subprocess +import tempfile + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parents[1] +spec = importlib.util.spec_from_file_location('wake_report', HERE / 't6050-wake-report.py') +module = importlib.util.module_from_spec(spec) +spec.loader.exec_module(module) +parse = module.parse + + +def block(source, name): + match = re.search(r'^static (?:void|int|bool) ' + name + r'\([^;]*?\)\n\{', source, re.M) + if not match: + raise RuntimeError('production function missing: ' + name) + brace = source.index('{', match.start()) + depth, end = 1, brace + 1 + while depth: + depth += (source[end] == '{') - (source[end] == '}') + end += 1 + return source[match.start():end] + + +def program(source): + constants = '\n'.join(re.findall(r'^#define T6050_WAKE_(?:ENABLE|WORDS|STATUS|GFX_STATUS)\s+[^\n]+', source, re.M)) + fields = '\n'.join(re.findall(r'^\s*(?:u32 wake_pending\[[^\n]+|bool wake_snapshot_valid;)', source, re.M)) + helper = block(source, 't6050_wake_snapshot') + '\n' + block(source, 't6050_report_wake_snapshot') + begin = block(source, 't6050_sleep_begin').split('{', 1)[1].split('\tt6050_sleep_pins_low(s);', 1)[0] + wake = block(source, 't6050_sleep_wake_regs').split('{', 1)[1].split('\tif (r->spmi_masked)', 1)[0] + finish = block(source, 't6050_sleep_finish') + assert finish.index('t6050_report_wake_snapshot(s);') > finish.index('t6050_sleep_undo_regs(s);') + suffix = finish[finish.index('\tr->usb_aon_kept = 0;'):finish.index('\tdev_dbg(s->dev, "entry')] + return PRELUDE.replace('PRODUCTION_FIELDS', fields) + constants + '\n' + helper + ''' +static void begin_prefix(struct apple_sleep *s) {''' + begin + ''' + (void)r; (void)i; (void)ret; +} +static void wake_prefix(struct apple_sleep *s) {''' + wake + ''' + (void)r; (void)i; (void)value; + mock_ack(); +} +static void finish_suffix(struct apple_sleep *s) { + struct apple_sleep_region_state *r = s->region; +''' + suffix + '\n}\n' + TESTS + + +PRELUDE = r''' +#include +#include +#include +#include +#include +#include +typedef uint32_t u32; +struct apple_sleep_region_state { + void *wake, *wake_gfx; + bool wake_armed; + unsigned usb_aon_kept; +PRODUCTION_FIELDS +}; +struct apple_sleep { struct apple_sleep_region_state *region; bool returned; void *dev; }; +static u32 ap[1024], gfx[1024], observed_pending[11], observed_masks[11]; +static struct apple_sleep_region_state *active; +static int reads, reports, warnings; +static bool acknowledged; +static u32 mock_readl(const void *ptr) +{ + uintptr_t address=(uintptr_t)ptr, ab=(uintptr_t)ap, gb=(uintptr_t)gfx; + assert(!active->wake_snapshot_valid && !acknowledged); + if (address>=ab && address=0xc && offset<=0x30 && !(offset&3)) || + (offset>=0x34 && offset<=0x58 && !(offset&3))); + } else { + assert(address>=gb && addresswake=ap; r->wake_gfx=gfx; r->wake_armed=true; + *s=(struct apple_sleep){.region=r,.returned=false}; active=r; + reads=reports=warnings=0; acknowledged=false; + for(unsigned i=0;i<10;i++) ap[0xc/4+i]=expected_masks[i]; + gfx[0xc/4]=expected_masks[10]; +} +static void dump(const char *label, const u32 values[11]) +{ + printf("wake %s AP",label); + for(unsigned i=0;i<40;i++) printf(" %02x",((const unsigned char *)values)[i]); + printf(" GFX %#x\n",values[10]); +} +int main(void) +{ + struct apple_sleep s; struct apple_sleep_region_state r; + setup(&s,&r); r.wake_snapshot_valid=true; begin_prefix(&s); + assert(!r.wake_snapshot_valid); + for(unsigned id=0;id<352;id++) { + setup(&s,&r); + if(id<320) ap[0x34/4+id/32]=1U<<(id%32); else gfx[0x10/4]=1U<<(id-320); + wake_prefix(&s); + assert(reads==22 && r.wake_snapshot_valid && acknowledged); + /* apple_sleep_return sets this only after the SoC wake callback. */ + s.returned=true; + finish_suffix(&s); + assert(reads==22 && reports==2 && warnings==0); + for(unsigned word=0;word<11;word++) { + assert(observed_pending[word]==(word==id/32 ? 1U<<(id%32) : 0)); + assert(observed_masks[word]==expected_masks[word]); + } + dump("pending",observed_pending); dump("masks",observed_masks); + } + for(unsigned missing=0;missing<3;missing++) { + setup(&s,&r); r.wake_snapshot_valid=true; + if(missing==0) r.wake=NULL; + if(missing==1) r.wake_gfx=NULL; + if(missing==2) r.wake_armed=false; + t6050_wake_snapshot(&s); + assert(!r.wake_snapshot_valid && reads==0); + mock_ack(); s.returned=true; finish_suffix(&s); assert(reports==0 && warnings==1); + } + setup(&s,&r); ap[0x34/4+76/32]=1U<<(76%32); wake_prefix(&s); + s.returned=false; finish_suffix(&s); assert(reports==0 && warnings==0); + return 0; +} +''' + + +def controls(source): + return { + 'truncate-ap-to-nine': source.replace('i < T6050_WAKE_WORDS; i++', 'i < T6050_WAKE_WORDS - 1; i++', 1), + 'wrong-ap-status-offset': source.replace('#define T6050_WAKE_STATUS\t0x34', '#define T6050_WAKE_STATUS\t0x28', 1), + 'gfx-reads-mask-as-pending': source.replace('#define T6050_WAKE_GFX_STATUS\t0x10', '#define T6050_WAKE_GFX_STATUS\t0x0c', 1), + 'gfx-overwrites-ap-word-nine': source.replace('r->wake_pending[T6050_WAKE_WORDS] =', 'r->wake_pending[T6050_WAKE_WORDS - 1] =', 1), + 'invert-mask-readback': source.replace('r->wake_masks_at_return[i] = readl(', 'r->wake_masks_at_return[i] = ~readl(', 1), + 'capture-after-ack': source.replace('\tt6050_wake_snapshot(s);\n\tif (r->spmi_masked)', '\tif (r->spmi_masked)', 1).replace('\t\tt6050_spmi_pmu_ack(s);', '\t\tt6050_spmi_pmu_ack(s);\n\t\tt6050_wake_snapshot(s);', 1), + 'omit-begin-invalidation': source.replace('\tr->wake_snapshot_valid = false;\n\tt6050_sleep_pins_low(s);', '\tt6050_sleep_pins_low(s);', 1), + 'report-on-refusal': source.replace('\tif (!s->returned)\n\t\treturn;\n', '', 1), + 'capture-gated-on-later-returned-flag': source.replace( + '\tr->wake_snapshot_valid = false;\n\tif (!r->wake_armed', + '\tr->wake_snapshot_valid = false;\n\tif (!s->returned)\n\t\treturn;\n\tif (!r->wake_armed', 1), + } + + +def no_core(): + resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--cc', default=os.environ.get('CC', 'cc')) + parser.add_argument('--tree', type=Path, default=ROOT, help='checkout root (default: this repository)') + parser.add_argument('--output', '--out', dest='out', type=Path) + parser.add_argument('--negative-controls', action='store_true') + args = parser.parse_args() + args.out = args.out or Path(tempfile.mkdtemp(prefix='t6050-wake-reporting-')) / 'out' + args.out.mkdir(parents=True, exist_ok=False) + source_path = args.tree / 'drivers/soc/apple/sleep-t6050.h' + source = source_path.read_text() + results = [] + variants = {'production-current': source} + if args.negative_controls: + variants.update(controls(source)) + for name, source in variants.items(): + assert name == 'production-current' or source != source_path.read_text(), name + c = args.out / (name + '.c'); binary = args.out / name + c.write_text(program(source)) + command = shlex.split(args.cc) + ['-std=gnu11', '-O1', '-Wall', '-Wextra', '-Werror', str(c), '-o', str(binary)] + built = subprocess.run(command, capture_output=True, text=True, timeout=30) + (args.out / (name + '-compile.log')).write_text(built.stdout + built.stderr) + assert built.returncode == 0, (name, built.stderr) + ran = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10, preexec_fn=no_core) + (args.out / (name + '.log')).write_text(ran.stdout + ran.stderr) + if name == 'production-current': + assert ran.returncode == 0, ran.stderr + lines = ran.stdout.splitlines() + assert len(lines) == 704 + for bit in range(352): + decoded = parse('\n'.join(lines[2*bit:2*bit+2])) + assert decoded['raw_pending_ids'] == [bit], (bit, decoded) + masked = bool(decoded['mask_words'][bit // 32] & (1 << (bit % 32))) + assert decoded['enabled_pending_ids'] == ([] if masked else [bit]), (bit, decoded) + assert decoded['masked_pending_ids'] == ([bit] if masked else []), (bit, decoded) + # The retained WM2 RTC76 bit must not become AP12 or lose GFX indexing. + assert parse('\n'.join(lines[152:154]))['raw_pending_ids'] == [76] + else: + assert ran.returncode == -signal.SIGABRT and 'Assertion' in ran.stderr, (name, ran.returncode, ran.stderr) + results.append({'variant': name, 'exit_code': ran.returncode, + 'source_sha256': hashlib.sha256(c.read_bytes()).hexdigest()}) + for invalid in ('', 'wake register snapshot unavailable', + 'wake pending AP 00 GFX 0\nwake masks AP 00 GFX 0', + '\n'.join(lines[:2] + lines[:2])): + try: + parse(invalid) + except ValueError: + pass + else: + raise AssertionError('missing/malformed/duplicate observation accepted') + (args.out / 'result.json').write_text(json.dumps({'compiler': args.cc, 'variants': results, + 'all_352_global_bits_checked': True, 'negative_controls': len(results)-1, + 'input_path': str(source_path.resolve()), 'input_sha256': hashlib.sha256(source_path.read_bytes()).hexdigest(), + 'scope': 'source-extracted actual repository callbacks only; no kernel build or hardware'}, indent=2) + '\n') + print(f'{args.cc}: actual repository callbacks, all 352 bit positions, {len(results)-1} compiled assertion controls, parser refusals PASS') + + +if __name__ == '__main__': + main()