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
61 changes: 42 additions & 19 deletions src/kboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -7942,25 +7942,24 @@ static int dt_set_j713_display_maps(const struct adt_segment_ranges *segments, u
return 0;
}

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

/*
* Publish the inherited DCP firmware mapping without starting a display driver: on the J713
* on any firmware as before, and on any other M4 Mac with a panel on 26.6.2.
* Publish the inherited DCP firmware mapping without starting a display driver, on any M4 Mac
* whose ADT describes an internal panel on disp0, whatever its firmware. The kernel picks the
* display backend from the firmware the DCP runs.
*/
static int dt_set_m4_dcp_handoff(void)
{
int dcp_node = fdt_path_offset(dt, "dcp");
if (dcp_node < 0)
return 0; /* Older DT with simple-framebuffer only. */
bool j713 = !fdt_node_check_compatible(dt, 0, "apple,j713");
if (chip_id != T8132 || (!j713 && os_firmware.version != V26_6_2) ||
fdt_node_check_compatible(dt, dcp_node, "apple,t8132-dcp"))
if (chip_id != T8132 || fdt_node_check_compatible(dt, dcp_node, "apple,t8132-dcp"))
bail("FDT: unsupported native DCP handoff target\n");
/* The Mac mini's disp0 is external: its DCP drives a port, not a panel. */
if (!j713) {
int disp0 = adt_path_offset(adt, "/arm-io/disp0");
if (disp0 < 0 || adt_getprop(adt, disp0, "external", NULL))
bail("FDT: no internal panel, no native DCP handoff\n");
}
/* A Mac mini's disp0 is external: its DCP drives a port, not a panel. */
int disp0 = adt_path_offset(adt, "/arm-io/disp0");
if (disp0 < 0 || adt_getprop(adt, disp0, "external", NULL))
bail("FDT: no internal panel, no native DCP handoff\n");

int nub = adt_path_offset(adt, "/arm-io/dcp/iop-dcp-nub");
if (nub < 0)
Expand Down Expand Up @@ -8006,14 +8005,29 @@ static int dt_set_m4_dcp_handoff(void)
if (ret)
return ret;

bool maps = false;
dcp_node = fdt_path_offset(dt, "dcp");
if (fdt_getprop(dt, dcp_node, "apple,j713-native-piodma", NULL)) {
/* The DCP image is per firmware, the same on every M4 Mac. */
if (os_firmware.version != V26_6_2 ||
dt_set_j713_display_maps(segments, count, dram_start, dram_size))
/*
* The walk checks every inherited SID0/SID16 page against the segments of the image
* the DCP runs and publishes nothing unless all of them match, so it needs no list of
* firmware versions.
*/
if (dt_set_j713_display_maps(segments, count, dram_start, dram_size))
printf("FDT: M4 PIODMA handoff unqualified; retaining disabled device\n");
else
maps = true;
}

/*
* Firmware 27 is given its clocks by the OS (/arm-io/disp0 clock-ids); without them it
* does not start, but the rest of the boot does. Nothing is published on 26.6.2.
* The kernel admits a firmware-27 DCP on apple,clock-ids, so they go out only together
* with the inherited display maps: a DCP adopted without them faults on its DART.
*/
if (maps && dt_set_dcp_fw27_clocks("dcp", "/arm-io/disp0"))
printf("FDT: M4 DCP firmware clocks not published\n");

printf("FDT: M4 DCP firmware mappings published; display device status unchanged\n");
return 0;
}
Expand Down Expand Up @@ -8047,17 +8061,26 @@ static int dt_set_dcp_fw27_clocks(const char *alias, const char *adt_engine)
return 0;
}

/*
* A tree written for older firmware has the display clock alone; one written for a
* processor that only ever ran firmware-27-style interfaces (T8132) has no clocks at all.
*/
int clen = 0;
const fdt32_t *display = fdt_getprop(dt, node, "clocks", &clen);
if (!display || clen != sizeof(fdt32_t) || fdt_getprop(dt, node, "clock-names", NULL)) {
if ((display && clen != sizeof(fdt32_t)) || fdt_getprop(dt, node, "clock-names", NULL)) {
printf("FDT: %s: unexpected clock properties; firmware clocks not published\n", alias);
return 0;
}

fdt32_t clocks[9], ids[8];
char names[9 * 12];
int names_len = snprintf(names, sizeof(names), "display") + 1;
clocks[0] = *display;
u32 first = 0;
int names_len = 0;
if (display) {
names_len = snprintf(names, sizeof(names), "display") + 1;
clocks[0] = *display;
first = 1;
}
for (u32 i = 0; i < nclk; i++) {
u32 id, hz;
memcpy(&id, raw_ids + i * sizeof(u32), sizeof(id));
Expand Down Expand Up @@ -8086,15 +8109,15 @@ static int dt_set_dcp_fw27_clocks(const char *alias, const char *adt_engine)
ret = ret ?: fdt_setprop_u32(dt, clk, "phandle", ph);
if (ret)
return ret;
clocks[i + 1] = cpu_to_fdt32(ph);
clocks[i + first] = cpu_to_fdt32(ph);
ids[i] = cpu_to_fdt32(id);
names_len += snprintf(names + names_len, sizeof(names) - names_len, "firmware-%u", i) + 1;
}

node = fdt_path_offset(dt, path);
if (node < 0)
return node;
int ret = fdt_setprop(dt, node, "clocks", clocks, (nclk + 1) * sizeof(*clocks));
int ret = fdt_setprop(dt, node, "clocks", clocks, (nclk + first) * sizeof(*clocks));
node = fdt_path_offset(dt, path);
ret = ret ?: fdt_setprop(dt, node, "clock-names", names, names_len);
node = fdt_path_offset(dt, path);
Expand Down
120 changes: 97 additions & 23 deletions tests/python/test_m4_dcp_handoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path):
header = (ROOT / "src/adt.h").read_text()
start = header.index("struct adt_segment_ranges {")
layout = header[start:header.index("} PACKED;", start) + len("} PACKED;")]
# The clock publisher is declared ahead of the handoff; take its definition.
start = kboot.index("static int dt_set_dcp_fw27_clocks(const char *alias, const char *adt_engine)\n{")
clocks = kboot[start:kboot.index("\n}", start) + 2] + "\n"
start = kboot.index("struct j713_display_maps {")
maps = kboot[start:kboot.index("\n};", start) + 3] + "\n"
production = (
Expand All @@ -31,7 +34,7 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path):
function(kboot, "dt_device_add_mem_region") + function(kboot, "dt_set_dcp_firmware") +
function(kboot, "dt_reserve_asc_segments_at") + function(kboot, "dt_reserve_asc_segments") + function(kboot, "dt_reserve_asc_firmware") +
maps + function(kboot, "j713_display_page") + function(kboot, "dt_set_j713_display_maps") +
function(kboot, "dt_set_m4_dcp_handoff"))
clocks + function(kboot, "dt_set_m4_dcp_handoff"))
production = function(firmware, "firmware_set_fdt", "int ") + production
assert not re.search(r"\bdt_set_j713_dcp_handoff\b", kboot)

Expand All @@ -57,7 +60,7 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path):
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define bail(...) do { printf(__VA_ARGS__); return -1; } while (0)
enum fw_version { V12_3, V12_3_1, V12_4, V13_5B4, V13_5, V13_6_1, V14_7, V26_5, V26_6, V26_6_2,
V27_0 };
V27_0, V27_2 };
struct fw_version_info {
enum fw_version version;
const char *string;
Expand All @@ -73,6 +76,7 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path):
[V26_6] = {V26_6, "26.6", {26, 6, 0}, 3, ""},
[V26_6_2] = {V26_6_2, "26.6.2", {26, 6, 2}, 3, ""},
[V27_0] = {V27_0, "27.0", {27, 0, 0}, 2, ""},
[V27_2] = {V27_2, "27.2", {27, 2, 0}, 3, ""},
};
static struct fw_version_info os_firmware;
''' + layout + r'''
Expand Down Expand Up @@ -126,13 +130,17 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path):

static struct board b;
static u64 dram_base = 0x10000000000UL;
static const u32 disp0_clock_ids[] = {364, 431};
static u32 clock_hz[200] = {[364 - 256] = 300000000, [431 - 256] = 0};
static const u32 one = 1;
static int walks;
static u64 dart_skew; /* added to every inherited page's physical address */
static int adt_path_offset(void *tree, const char *path)
{
if (!strcmp(path, "/chosen")) return 1;
if (!strcmp(path, "/arm-io/dcp/iop-dcp-nub")) return 2;
if (!strcmp(path, "/arm-io/disp0")) return b.external < 0 ? -1 : 3;
if (!strcmp(path, "/arm-io")) return 4;
return -1;
}
static const void *adt_getprop(void *tree, int node, const char *name, u32 *len)
Expand All @@ -147,6 +155,9 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path):
}
if (node == 2 && !strcmp(name, "uuid")) { *len = strlen(b.uuid) + 1; return b.uuid; }
if (node == 3 && !strcmp(name, "external") && b.external > 0) { *len = 4; return &one; }
/* /arm-io/disp0 clock-ids index /arm-io clock-frequencies from 256 (every M4 ADT read). */
if (node == 3 && !strcmp(name, "clock-ids")) { *len = sizeof(disp0_clock_ids); return disp0_clock_ids; }
if (node == 4 && !strcmp(name, "clock-frequencies")) { *len = sizeof(clock_hz); return clock_hz; }
return NULL;
}
static int get_value(int node, const char *name, void *out, size_t capacity)
Expand Down Expand Up @@ -174,7 +185,8 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path):
n++;
if (!mapped) continue;
for (u64 at = 0; at < ALIGN_UP((u64)b.segs[i].size, SZ_16K); at += SZ_16K)
if (visit((b.segs[i].remap & ((1UL << 36) - 1)) + at, b.segs[i].phys + at, opaque))
if (visit((b.segs[i].remap & ((1UL << 36) - 1)) + at, b.segs[i].phys + at + dart_skew,
opaque))
return -1;
}
return 0;
Expand All @@ -185,7 +197,7 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path):
static int phandle_of(const char *path) { return fdt_get_phandle(dt, fdt_path_offset(dt, path)); }
static void world(const struct board *board, enum fw_version fw)
{
b = *board; walks = 0; chip_id = T8132;
b = *board; walks = 0; dart_skew = 0; chip_id = T8132;
os_firmware = fw_versions[fw];
dt = blob;
memset(blob, 0, sizeof(blob));
Expand Down Expand Up @@ -285,7 +297,32 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path):
assert(!!fdt_getprop(dt, fdt_path_offset(dt, "disp0"), "apple,j713-inherited-mappings",
NULL) == piodma);
assert(!!fdt_getprop(dt, fdt_path_offset(dt, "disp0"), "memory-region", NULL) == piodma);
assert(walks == (b.native && os_firmware.version == V26_6_2 ? 2 : 0));
/* The walk runs wherever the DT asks for PIODMA, on any firmware. */
assert(walks == (b.native ? 2 : 0));
/*
* Firmware 27 is given /arm-io/disp0's clocks, only with the inherited display maps (the
* kernel admits the DCP on apple,clock-ids); 26.x trees are not touched.
*/
const fdt32_t *ids = fdt_getprop(dt, dcp, "apple,clock-ids", &len);
if (os_firmware.version < V27_0 || !piodma) {
assert(!ids && !fdt_getprop(dt, dcp, "clocks", NULL) &&
fdt_path_offset(dt, "/clock-dcp-firmware-364") < 0);
return;
}
assert(ids && len == 8 && fdt32_ld(&ids[0]) == 364 && fdt32_ld(&ids[1]) == 431);
const char *names = fdt_getprop(dt, dcp, "clock-names", &len);
assert(names && len == 22 && !memcmp(names, "firmware-0\0firmware-1\0", 22));
const fdt32_t *clk = fdt_getprop(dt, dcp, "clocks", &len);
assert(clk && len == 8);
static const char *const clock_path[] = {"/clock-dcp-firmware-364", "/clock-dcp-firmware-431"};
static const u32 rate[] = {300000000, 0};
for (int i = 0; i < 2; i++) {
int node = fdt_path_offset(dt, clock_path[i]);
assert(node >= 0 && fdt32_ld(&clk[i]) == fdt_get_phandle(dt, node));
assert(!fdt_node_check_compatible(dt, node, "fixed-clock"));
const fdt32_t *hz = fdt_getprop(dt, node, "clock-frequency", &len);
assert(hz && len == 4 && fdt32_ld(hz) == rate[i]);
}
}
static void handed_over(const struct board *board, enum fw_version fw, bool piodma)
{
Expand All @@ -309,18 +346,46 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path):
assert(argc == 2);
outdir = argv[1];

/* The J713 as before: on 26.6.2 with PIODMA and scanout, and on any other firmware. */
/*
* Every M4 Mac whose ADT has an internal panel on disp0, on any firmware: the J713 on
* 26.6.2 with PIODMA and scanout, and the same on older images. PIODMA follows the
* inherited pages, not a firmware version list.
*/
handed_over(&j713, V26_6_2, true);
struct board old = j713;
old.uuid = "0BC97BDD-E2FE-3A1B-96B4-8EA8FC5B8BE8"; /* 26.5.1: PIODMA unqualified */
handed_over(&old, V26_5, false);
handed_over(&old, V27_0, false);
handed_over(&old, V26_6, false);
/* The J713's disp0 is never read. PIODMA follows the firmware version, not the image. */
old = j713; old.external = -1;
handed_over(&old, V26_6, false);
old.uuid = "0BC97BDD-E2FE-3A1B-96B4-8EA8FC5B8BE8"; /* 26.5.1 */
handed_over(&old, V26_5, true);
handed_over(&old, V27_0, true);
handed_over(&old, V26_6, true);
/* The 27.2 stub: the 26.6.2 path's PIODMA and scanout, plus the firmware-27 clocks. */
struct board stub = j713;
stub.uuid = "B3BA603C-3CA7-3E21-B393-44EB20BD2AEF";
handed_over(&stub, V27_2, true);
/*
* Inherited pages that do not match the image's segments: nothing of PIODMA or scanout is
* published (the walk stops at the first page), and neither are the firmware clocks, so
* the kernel does not admit the DCP. It keeps its firmware.
*/
world(&stub, V27_2);
dart_skew = SZ_16K;
assert(!dt_set_m4_dcp_handoff());
assert(walks == 1 && status_is("/soc/display-subsystem", "disabled") &&
status_is("/soc/dcp@30ae00000/piodma", "disabled") &&
status_is("/soc/iommu@30a300000", "disabled"));
assert(!fdt_getprop(dt, fdt_path_offset(dt, "dcp"), "apple,clock-ids", NULL) &&
!fdt_getprop(dt, fdt_path_offset(dt, "dcp"), "clocks", NULL) &&
fdt_path_offset(dt, "/clock-dcp-firmware-364") < 0);
assert(fdt_getprop(dt, fdt_path_offset(dt, "dcp"), "apple,firmware-uuid", NULL));
/* The same pages on 26.6.2 are refused the same way. */
world(&j713, V26_6_2);
dart_skew = SZ_16K;
assert(!dt_set_m4_dcp_handoff());
assert(walks == 1 && status_is("/soc/display-subsystem", "disabled"));

/* Every other M4 Mac with a panel, on 26.6.2. The laptops' DT asks for PIODMA. */
/*
* The other M4 Macs with a panel take the same handoff as the J713, decided by their ADT
* and not their board name. The laptops' DT asks for PIODMA.
*/
handed_over(&j604, V26_6_2, true);
handed_over(&j715, V26_6_2, true);
struct board other = j604;
Expand All @@ -331,22 +396,26 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path):
imac.name = "j624";
imac.native = false;
handed_over(&imac, V26_6_2, false);

/* Any other firmware: nothing, as before. */
const enum fw_version others[] = {V26_5, V26_6, V27_0, V14_7};
struct board pro = j604;
pro.name = "j604-27.2";
pro.uuid = stub.uuid;
handed_over(&pro, V27_2, true);
const enum fw_version others[] = {V26_5, V26_6, V27_0, V27_2, V14_7};
const struct board *panels[] = {&j604, &j715, &imac};
for (unsigned i = 0; i < ARRAY_SIZE(others); i++)
for (unsigned k = 0; k < ARRAY_SIZE(panels); k++) {
world(panels[k], others[i]);
refused();
}
for (unsigned k = 0; k < ARRAY_SIZE(panels); k++)
handed_over(panels[k], others[i], panels[k]->native);

/* No internal panel: the Mac mini, or no disp0 at all. */
/* No internal panel: the Mac mini on any firmware, or no disp0 at all, the J713 too. */
world(&j773g, V26_6_2); refused(); save("j773g-26.6.2");
world(&j773g, V26_6); refused();
world(&j773g, V27_2); refused();
struct board bare = j604;
bare.external = -1;
world(&bare, V26_6_2); refused();
bare = j713;
bare.external = -1;
world(&bare, V27_2); refused();

/* Not a T8132, or not its DCP. */
world(&j713, V26_6_2); chip_id = T8122; refused();
Expand Down Expand Up @@ -376,7 +445,12 @@ def test_m4_dcp_handoff_follows_the_firmware_and_the_panel(tmp_path):
)
result = subprocess.run([str(binary), str(tmp_path)], capture_output=True, text=True)
assert result.returncode == 0, result.stdout + result.stderr
for name in ("j713-26.6.2", "j604-26.6.2", "j715-26.6.2", "j624-26.6.2", "j773g-26.6.2"):
for name in ("j713-27.2", "j713-26.6.2", "j604-26.6.2", "j715-26.6.2", "j624-26.6.2", "j773g-26.6.2",
"j604-27.2-27.2", "j715-27.2"):
assert (tmp_path / f"{name}.dtb").stat().st_size > 0
# The refusals say why.
assert "no internal panel" in result.stdout
# No board name decides the handoff.
handoff = function(kboot, "dt_set_m4_dcp_handoff")
assert not re.search(r"check_compatible\(dt, 0,", handoff)
assert "V26_6_2" not in handoff and "V27_2" not in handoff
Loading