Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 183 additions & 6 deletions src/kboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -5050,13 +5050,12 @@ static int dt_reserve_asc_segments_at(int fdt_node, const char *adt_path, const
uint32_t mem_phandle = fdt_get_phandle(dt, mem_node);

/*
* The OS log segment (flags 0xa) of a T6030 or T8132 display coprocessor,
* internal or external, runs at its physical address and is not reached
* through the DART: reserve its RAM, marked as such, without an IOMMU
* mapping.
* The OS log segment (flags 0xa) of a display coprocessor, internal or
* external, runs at its physical address and is not reached through the
* DART: reserve its RAM, marked as such, without an IOMMU mapping. The
* segment's own flag selects this, on every chip.
*/
if ((chip_id == T6030 || chip_id == T8132) && !strncmp(fdt_path, "dcp", 3) &&
seg->unk == 0xa)
if (!strncmp(fdt_path, "dcp", 3) && seg->unk == 0xa)
ret = fdt_setprop_empty(dt, mem_node, "apple,dcp-os-log");
else
ret = dt_device_set_reserved_mem(mem_node, node_name, dev_phandle, iova, seg_size);
Expand Down Expand Up @@ -9417,6 +9416,181 @@ static int dt_set_j713_display_maps(const struct adt_segment_ranges *segments, u
return 0;
}

/*
* T8152 internal display handoff for Linux's DCP driver, on every T8152 tree whose "dcp" alias is
* an apple,t8152-dcp node (the J804's). If any check or later step fails, nothing is published
* (the tree is edited in a dt_display_transaction()) and the display nodes stay disabled (the
* panel keeps the simple-framebuffer). A tree with the apple,t8152-dcp-iboot contract (J873g) is
* not a target.
*
* iBoot leaves the DCP running with its firmware mapped through locked DART tables. The ADT
* iop-dcp-nub segment-ranges give each segment's physical address and, in "remap", its DVA in the
* DARTs' 1 TiB window, except __OS_LOG (flags 0xa), whose remap repeats the physical address. Every
* dart-dcp DCP-stream page and every dart-disp0 SID 0 / SID 16 page is checked against those
* segments before anything is published: dcp gets one reserved region per segment, display (SID 0)
* and disp0_piodma (SID 16) get the flags-2 segments their tables map, with iommu-addresses. The
* coprocessor and both DARTs are found by their ADT reg.
*
* The DCP stays as iBoot started it, so apple,dcp-rtkit-running stays absent:
* dt_set_dcp_state_t8152() publishes it only for a DCP aura itself started.
*/
/* The DCP's stream on dart-dcp. */
#define T8152_DCP_SID 23
/* remap[63:36]: the DARTs' 1 TiB DVA window (0x10 << 36) the firmware segments run in. */
#define T8152_DCP_DVA_SHIFT 36
#define T8152_DCP_DVA_WINDOW 0x10

static int t8152_dcp_page(u64 iova, u64 physical, void *opaque)
{
struct j713_display_maps *maps = opaque;
for (u32 i = 0; i < maps->count; i++) {
const struct adt_segment_ranges *seg = &maps->segments[i];
u64 start = seg->remap & ((1ULL << T8152_DCP_DVA_SHIFT) - 1);
u64 size = ALIGN_UP((u64)seg->size, SZ_16K);
if (seg->unk == 0xa || iova < start || iova - start >= size)
continue;
if ((seg->remap >> T8152_DCP_DVA_SHIFT) != T8152_DCP_DVA_WINDOW ||
physical != seg->phys + iova - start)
return -1;
maps->bytes[i] += SZ_16K;
return 0;
}
return -1;
}

/* The register window of an ADT DART, by its reg; -1 when the ADT has none. */
static int t8152_dcp_dart(const char *path, u64 *base, u64 *size)
{
int trace[8];

if (adt_path_offset_trace(adt, path, trace) < 0 ||
adt_get_reg(adt, trace, "reg", 0, base, size) < 0 || !*base || !*size)
return -1;
return 0;
}

static int dt_set_status_okay_iommu(const char *alias)
{
int node = fdt_path_offset(dt, alias);
int dart = node < 0 ? -1 : dt_get_iommu_node(node, 0);
if (dart < 0 || fdt_setprop_string(dt, dart, "status", "okay"))
return -1;
node = fdt_path_offset(dt, alias);
if (node < 0 || fdt_setprop_string(dt, node, "status", "okay"))
return -1;
return 0;
}

/* Runs in dt_display_transaction(): a failure at any step discards every edit it made. */
static int dt_set_t8152_dcp_handoff_one(unsigned int unused)
{
(void)unused;
int nub = adt_path_offset(adt, "/arm-io/dcp/iop-dcp-nub");
int chosen = adt_path_offset(adt, "/chosen");
u64 dram_start, dram_size;
u32 len;
if (nub < 0 || chosen < 0 || ADT_GETPROP(adt, chosen, "dram-base", &dram_start) < 0 ||
ADT_GETPROP(adt, chosen, "dram-size", &dram_size) < 0 || !dram_size)
bail("FDT: T8152 DCP: missing ADT nub or DRAM bounds\n");
const struct adt_segment_ranges *segments = adt_getprop(adt, nub, "segment-ranges", &len);
if (!segments || !len || len % sizeof(*segments) || len / sizeof(*segments) > 32)
bail("FDT: T8152 DCP: invalid segment-ranges\n");
u32 count = len / sizeof(*segments);
for (u32 i = 0; i < count; i++) {
u64 size = ALIGN_UP((u64)segments[i].size, SZ_16K);
if (!size || (segments[i].phys | segments[i].remap) & (SZ_16K - 1) ||
segments[i].phys < dram_start || segments[i].phys + size > dram_start + dram_size)
bail("FDT: T8152 DCP: invalid segment %u\n", i);
}

u64 dart_dcp, dart_dcp_size, dart_disp0, dart_disp0_size;
if (t8152_dcp_dart("/arm-io/dart-dcp", &dart_dcp, &dart_dcp_size) ||
t8152_dcp_dart("/arm-io/dart-disp0", &dart_disp0, &dart_disp0_size))
bail("FDT: T8152 DCP: no ADT reg for dart-dcp or dart-disp0\n");

asc_dev_t *asc = asc_init("/arm-io/dcp");
bool running = asc && asc_cpu_running(asc);
asc_free(asc);
if (!running)
bail("FDT: T8152 DCP: coprocessor is not running\n");

/* Every page the running firmware maps must belong to a segment. */
struct j713_display_maps dcpmap = {.segments = segments, .count = count};
if (dart_walk_locked_t8110(dart_dcp, dart_dcp_size, T8152_DCP_SID, dram_start, dram_size,
t8152_dcp_page, &dcpmap))
bail("FDT: T8152 DCP: dart-dcp SID %u maps outside the firmware segments\n", T8152_DCP_SID);

const char *aliases[] = {"disp0", "disp0_piodma"};
const u8 streams[] = {0, 16};
struct j713_display_maps maps[2] = {
{.segments = segments, .count = count},
{.segments = segments, .count = count},
};
for (u32 n = 0; n < 2; n++) {
if (dart_walk_locked_t8110(dart_disp0, dart_disp0_size, streams[n], dram_start, dram_size,
j713_display_page, &maps[n]))
bail("FDT: T8152 DCP: dart-disp0 SID %u maps outside the firmware segments\n",
streams[n]);
for (u32 i = 0; i < count; i++)
if (maps[n].bytes[i] && maps[n].bytes[i] != ALIGN_UP((u64)segments[i].size, SZ_16K))
bail("FDT: T8152 DCP: SID %u maps segment %u partially\n", streams[n], i);
}

const char *uuid = adt_getprop(adt, nub, "uuid", &len);
if (!uuid || len != 37 || uuid[len - 1] ||
fdt_setprop_string(dt, fdt_path_offset(dt, "dcp"), "apple,firmware-uuid", uuid))
bail("FDT: T8152 DCP: cannot publish the firmware UUID\n");

if (dt_reserve_asc_firmware("/arm-io/dcp/iop-dcp-nub", NULL, "dcp", true, 0))
bail("FDT: T8152 DCP: firmware reservation failed\n");

u32 phandles[2];
for (u32 n = 0; n < 2; n++) {
int node = fdt_path_offset(dt, aliases[n]);
if (node < 0)
bail("FDT: T8152 DCP: no %s alias\n", aliases[n]);
phandles[n] = fdt_get_phandle(dt, node);
if (!phandles[n] && (fdt_generate_phandle(dt, &phandles[n]) ||
fdt_setprop_u32(dt, node, "phandle", phandles[n])))
bail("FDT: T8152 DCP: no phandle for %s\n", aliases[n]);
}
for (u32 i = 0; i < count; i++) {
if (!maps[0].bytes[i] && !maps[1].bytes[i])
continue;
char name[64];
snprintf(name, sizeof(name), "asc-firmware@%lx", segments[i].phys);
u64 size = ALIGN_UP((u64)segments[i].size, SZ_16K);
int node = dt_get_or_add_reserved_mem(name, "apple,asc-mem", true, segments[i].phys, size);
if (node < 0)
return -1;
u32 phandle = fdt_get_phandle(dt, node);
for (u32 n = 0; n < 2; n++) {
if (!maps[n].bytes[i])
continue;
node = fdt_subnode_offset(dt, fdt_path_offset(dt, "/reserved-memory"), name);
if (node < 0 ||
dt_device_set_reserved_mem(node, name, phandles[n], segments[i].remap, size) ||
dt_device_add_mem_region(aliases[n], phandle, NULL))
return -1;
}
}

if (dt_set_status_okay_iommu("dcp") || dt_set_status_okay_iommu("disp0") ||
dt_set_status_okay_iommu("disp0_piodma"))
bail("FDT: T8152 DCP: could not enable the display nodes\n");
printf("FDT: T8152 DCP handoff published (%u segments, firmware %s)\n", count, uuid);
return 0;
}

/* The hardware selects the handoff: a "dcp" alias naming an apple,t8152-dcp node. */
static int dt_set_t8152_dcp_handoff(void)
{
int dcp = fdt_path_offset(dt, "dcp");
if (dcp < 0 || fdt_node_check_compatible(dt, dcp, "apple,t8152-dcp"))
return 0;
return dt_display_transaction(dt_set_t8152_dcp_handoff_one, 0);
}

static int dt_set_dcp_fw27_clocks(const char *alias, const char *adt_engine);

/*
Expand Down Expand Up @@ -14159,6 +14333,9 @@ static int dt_prepare_t8152(void)
if (dt_set_serial_number() || dt_set_display_t8152() || dt_set_live_afk_t8152() ||
dt_set_gpu(dt))
return -1;
/* Fail-soft: a refused handoff leaves the display nodes disabled. */
if (dt_set_t8152_dcp_handoff())
printf("FDT: T8152 DCP handoff not published\n");
dt_set_dcpext_t8152();
}
if (dt_set_dcp_state_t8152())
Expand Down
100 changes: 100 additions & 0 deletions tests/python/test_dcp_os_log_flag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# SPDX-License-Identifier: GPL-2.0-only
"""The DCP OS-log rule is keyed on the segment's own flag (0xa), on every chip."""

from pathlib import Path
import subprocess


def test_dcp_os_log_keyed_on_segment_flag(tmp_path):
repo = Path(__file__).resolve().parents[2]
source = (repo / "src/kboot.c").read_text()
header = (repo / "src/adt.h").read_text()
layout_start = header.index("struct adt_segment_ranges {")
layout = header[layout_start:header.index("} PACKED;", layout_start) + len("} PACKED;")]
def definition(signature):
start = source.index(signature)
return source[start:source.index("\n}", start) + 2]

firmware = definition("static int dt_reserve_asc_segments_at(") + "\n" + \
definition("static int dt_reserve_asc_segments(") + "\n" + \
definition("static int dt_reserve_asc_firmware(")
harness = r'''
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
typedef unsigned long u64;
typedef uint32_t u32;
#define SZ_16K 0x4000
#define T6030 0x6030
#define T6041 0x6041
#define T8132 0x8132
/* Only the dram_only segment filter reads properties this way. */
#define ADT_GETPROP(a, n, k, v) ((size_t)0)
static u32 chip_id;
static unsigned int mappings, links, os_logs;
#define ALIGN_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define bail(...) do { return -1; } while (0)
static int dt_device_dart_va_bits(int fdt_node, int adt_node, u32 *bits) { (void)fdt_node; (void)adt_node; (void)bits; return -1; }
#define PACKED __attribute__((packed))
''' + layout + r'''
static struct adt_segment_ranges segment = {0x100000, 0x200000, 0, 0x4000, 0};
static void *dt, *adt;
static int fdt_path_offset(const void *tree, const char *path)
{ (void)tree; (void)path; return 1; }
static int adt_path_offset(const void *tree, const char *path)
{ (void)tree; (void)path; return 1; }
static uint32_t fdt_get_phandle(const void *tree, int node)
{ (void)tree; (void)node; return 1; }
static int fdt_generate_phandle(const void *tree, u32 *value)
{ (void)tree; *value = 1; return 0; }
static int fdt_setprop_u32(void *tree, int node, const char *name, u32 value)
{ (void)tree; (void)node; (void)name; (void)value; return 0; }
static const void *adt_getprop(const void *tree, int node, const char *name, u32 *len)
{ (void)tree; (void)node; (void)name; *len = sizeof(segment); return &segment; }
static int dt_get_or_add_reserved_mem(const char *name, const char *compat,
bool no_map, u64 phys, u64 size)
{ (void)name; (void)compat; (void)no_map; (void)phys; (void)size; return 2; }
static int dt_device_set_reserved_mem(int node, const char *name, u32 handle,
u64 iova, u64 size)
{ (void)node; (void)name; (void)handle; (void)iova; (void)size; mappings++; return 0; }
static int fdt_node_offset_by_phandle(const void *tree, u32 handle)
{ (void)tree; (void)handle; return 1; }
static int fdt_appendprop_u32(void *tree, int node, const char *name, u32 value)
{ (void)tree; (void)node; (void)name; (void)value; links++; return 0; }
__attribute__((unused)) static const void *fdt_getprop(const void *tree, int node, const char *name, int *len)
{ (void)tree; (void)node; (void)name; (void)len; return "okay"; }
static int fdt_setprop_empty(void *tree, int node, const char *name)
{ (void)tree; (void)node; assert(!strcmp(name, "apple,dcp-os-log")); os_logs++; return 0; }
''' + firmware + r'''
int main(void)
{
/* The flags-0xa segment of any DCP, on a chip no list names: RAM only, marked, no IOVA. */
static const u32 chips[] = {0x8103, 0x8112, 0x6020, 0x8122, T6030, T8132, 0x8152};
for (unsigned int i = 0; i < sizeof(chips) / sizeof(chips[0]); i++) {
chip_id = chips[i];
segment.unk = 0xa;
mappings = links = os_logs = 0;
assert(!dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", false, 0));
assert(os_logs == 1 && !mappings && links == 1);
mappings = links = os_logs = 0;
assert(!dt_reserve_asc_firmware("/arm-io/dcpext0", NULL, "dcpext0", false, 0));
assert(os_logs == 1 && !mappings && links == 1);
/* Any other DCP segment, and a flags-0xa segment of another coprocessor, is mapped. */
segment.unk = 2;
mappings = links = os_logs = 0;
assert(!dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", false, 0));
assert(!os_logs && mappings == 1 && links == 1);
segment.unk = 0xa;
mappings = links = os_logs = 0;
assert(!dt_reserve_asc_firmware("/arm-io/isp", NULL, "isp", false, 0));
assert(!os_logs && mappings == 1 && links == 1);
}
return 0;
}
'''
binary = tmp_path / "dcp-os-log-flag"
subprocess.run(["cc", "-std=c11", "-Wall", "-Wextra", "-Werror", "-x", "c", "-",
"-o", str(binary)], input=harness, text=True, check=True)
subprocess.run([str(binary)], check=True)
2 changes: 1 addition & 1 deletion tests/python/test_isp_reservation_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def definition(signature):
#define T8132 0x8132
/* Only the dram_only segment filter reads properties this way. */
#define ADT_GETPROP(a, n, k, v) ((size_t)0)
static u32 chip_id;
__attribute__((unused)) static u32 chip_id;
static unsigned int mappings, links;
#define ALIGN_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
/* Stub: placeholder-based ISP handoff isn't exercised here. */
Expand Down
1 change: 1 addition & 0 deletions tests/python/test_t8152_audio_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
static int dt_set_display_t8152(void) { return leaf("display"); }
static int dt_set_live_afk_t8152(void) { return leaf("afk"); }
static int dt_set_gpu(void *tree) { assert(tree == dt); return leaf("gpu"); }
static int dt_set_t8152_dcp_handoff(void) { return 0; }
static void dt_set_dcpext_t8152(void) { leaf("dcpext"); }
static int dt_set_dcp_state_t8152(void) { return leaf("dcp_state"); }
static int dt_set_cpus(void) { return leaf("cpus"); }
Expand Down
58 changes: 58 additions & 0 deletions tests/python/test_t8152_dcp_handoff_selector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# SPDX-License-Identifier: GPL-2.0-only
"""T8152 internal DCP handoff: selected by the hardware, bases from the ADT, fail-closed."""

from pathlib import Path
import re

SOURCE = (Path(__file__).resolve().parents[2] / "src/kboot.c").read_text()


def definition(signature):
start = SOURCE.index(signature)
return SOURCE[start:SOURCE.index("\n}\n", start) + 3]


def test_no_boot_argument_or_board_selector():
assert "j804.dcp" not in SOURCE
assert "dt_bootargs_have" not in SOURCE
select = definition("static int dt_set_t8152_dcp_handoff(void)")
assert "board_id" not in select and "bootargs" not in select
# The dcp alias naming an apple,t8152-dcp node selects it, and the edit is one transaction.
assert 'fdt_path_offset(dt, "dcp")' in select
assert 'fdt_node_check_compatible(dt, dcp, "apple,t8152-dcp")' in select
assert "dt_display_transaction(dt_set_t8152_dcp_handoff_one, 0)" in select


def test_called_fail_soft_in_the_minimal_boot_branch():
prepare = definition("static int dt_prepare_t8152(void)")
minimal = prepare[prepare.index('"aurora,minimal-boot"'):prepare.index("dt_set_dcp_state_t8152()")]
call = minimal.index("if (dt_set_t8152_dcp_handoff())")
assert minimal.index("dt_set_gpu(dt)") < call < minimal.index("dt_set_dcpext_t8152();")
assert re.search(r"if \(dt_set_t8152_dcp_handoff\(\)\)\s*printf\(", minimal)


def test_bases_come_from_the_adt():
one = definition("static int dt_set_t8152_dcp_handoff_one(unsigned int unused)")
# No coprocessor or DART address is built in.
assert not re.search(r"0x2[0-9a-f]{8}", one)
assert 'asc_init("/arm-io/dcp")' in one and "asc_cpu_running(asc)" in one
assert "asc_free(asc)" in one
assert 't8152_dcp_dart("/arm-io/dart-dcp", &dart_dcp, &dart_dcp_size)' in one
assert 't8152_dcp_dart("/arm-io/dart-disp0", &dart_disp0, &dart_disp0_size)' in one
assert "dart_walk_locked_t8110(dart_dcp, dart_dcp_size, T8152_DCP_SID" in one
assert "dart_walk_locked_t8110(dart_disp0, dart_disp0_size, streams[n]" in one
dart = definition("static int t8152_dcp_dart(const char *path, u64 *base, u64 *size)")
assert "adt_path_offset_trace(adt, path, trace)" in dart
assert 'adt_get_reg(adt, trace, "reg", 0, base, size)' in dart
# Named stream and DVA window.
assert "#define T8152_DCP_SID 23" in SOURCE
page = definition("static int t8152_dcp_page(u64 iova, u64 physical, void *opaque)")
assert "(seg->remap >> T8152_DCP_DVA_SHIFT) != T8152_DCP_DVA_WINDOW" in page
assert ">> 36" not in page


def test_os_log_rule_keyed_on_the_segment_flag():
at = definition("static int dt_reserve_asc_segments_at(")
rule = at[at.rindex("if (", 0, at.index('"apple,dcp-os-log"')):at.index('"apple,dcp-os-log"')]
assert "chip_id" not in rule
assert '!strncmp(fdt_path, "dcp", 3) && seg->unk == 0xa' in rule
Loading