Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
211 changes: 205 additions & 6 deletions src/kboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -5050,13 +5050,15 @@ 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, whose descriptor says it runs at its physical address (remap
* equal to phys) is not reached through the DART: reserve its RAM, marked
* as such, without an IOMMU mapping. The segment itself selects this, on
* every chip. A flags-0xa segment with a real remap keeps its mapping (a
* T8142 __OS_LOG can have a live DART alias, see
* dt_reserve_m5_display_segments()).
*/
if ((chip_id == T6030 || chip_id == T8132) && !strncmp(fdt_path, "dcp", 3) &&
seg->unk == 0xa)
if (!strncmp(fdt_path, "dcp", 3) && seg->unk == 0xa && seg->remap == seg->phys)
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 +9419,200 @@ 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;
}

static int dt_t8152_dram(u64 *dram, u64 *size);

/*
* Every firmware segment whole inside DRAM [dram_start, dram_start + dram_size), 16K-aligned at
* both its physical and its remap address, with neither end wrapping. Returns the index of the
* first segment that is not, or -1 when all are.
*/
static int t8152_dcp_bad_segment(const struct adt_segment_ranges *segments, u32 count,
u64 dram_start, u64 dram_size)
{
for (u32 i = 0; i < count; i++) {
u64 phys = segments[i].phys, remap = segments[i].remap;
u64 size = ALIGN_UP((u64)segments[i].size, SZ_16K);
if (!size || (phys | remap) & (SZ_16K - 1) || phys > UINT64_MAX - size ||
remap > UINT64_MAX - size || phys < dram_start || size > dram_size ||
phys - dram_start > dram_size - size)
return i;
}
return -1;
}

/* 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");
u64 dram_start, dram_size;
u32 len;
if (nub < 0)
bail("FDT: T8152 DCP: missing ADT nub\n");
/* Exact u64 dram-base and dram-size, non-zero, not wrapping. */
if (dt_t8152_dram(&dram_start, &dram_size))
return -1;
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);
int bad = t8152_dcp_bad_segment(segments, count, dram_start, dram_size);
if (bad >= 0)
bail("FDT: T8152 DCP: invalid segment %d\n", bad);

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 +14355,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
110 changes: 110 additions & 0 deletions tests/python/test_dcp_os_log_flag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# SPDX-License-Identifier: GPL-2.0-only
"""The DCP OS-log rule is keyed on the segment itself (flag 0xa, remap == phys), 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)
{
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];
/* A flags-0xa DCP segment that runs at its physical address: RAM only, marked, no IOVA. */
segment.unk = 0xa;
segment.remap = segment.phys;
mappings = links = os_logs = 0;
assert(!dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", true, 0));
assert(os_logs == 1 && !mappings && links == 1);
mappings = links = os_logs = 0;
assert(!dt_reserve_asc_firmware("/arm-io/dcpext0", NULL, "dcpext0", true, 0));
assert(os_logs == 1 && !mappings && links == 1);
/* A flags-0xa DCP segment with a real remap keeps its mapping. */
segment.remap = 0x20000000;
mappings = links = os_logs = 0;
assert(!dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", true, 0));
assert(!os_logs && mappings == 1 && links == 1);
mappings = links = os_logs = 0;
assert(!dt_reserve_asc_firmware("/arm-io/dcpext0", NULL, "dcpext0", true, 0));
assert(!os_logs && mappings == 1 && links == 1);
/* Any other DCP segment, and a flags-0xa segment of another coprocessor, is mapped. */
segment.remap = segment.phys;
segment.unk = 2;
mappings = links = os_logs = 0;
assert(!dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", true, 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", true, 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
7 changes: 5 additions & 2 deletions tests/python/test_j713_dcpext_ready.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,12 @@ def define(source, name):
u64 at = dram_base + 0x40000 * n;
segments[n][0] = (struct adt_segment_ranges){.phys = at + 0x4000,
.remap = 0x10000000, .size = 0x4000};
/* The OS log segment, reserved by physical address. */
/*
* The OS log segment, reserved by physical address: as in the real J713 ADTs
* (tests/t8132_dcpext_host.c), its descriptor's remap repeats phys.
*/
segments[n][1] = (struct adt_segment_ranges){.phys = at + 0x10000,
.remap = 0x20000000, .size = 0x8000, .unk = 0xa};
.remap = at + 0x10000, .size = 0x8000, .unk = 0xa};
}
for (unsigned i = 0; i < ARRAY_SIZE(devices); i++)
ps[i] = strstr(devices[i].name, "_CPU") ? GATED : ON;
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
Loading
Loading