diff --git a/Makefile b/Makefile index 276de4e9..92a17d66 100644 --- a/Makefile +++ b/Makefile @@ -147,6 +147,7 @@ OBJECTS := \ exception.o exception_asm.o \ fb.o font.o font_retina.o \ firmware.o \ + gapf.o \ gxf.o gxf_asm.o \ heapblock.o \ hv.o hv_vm.o hv_exc.o hv_sptm.o \ @@ -158,7 +159,7 @@ OBJECTS := \ iodev.o \ iova.o \ isp.o \ - kboot.o kboot_atc.o \ + kboot.o kboot_atc.o kboot_utils.o \ kboot_t6020_compat.o \ main.o \ mitigations.o \ diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 6fd16fc7..ab2c30e8 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -28,10 +28,17 @@ name = "rust" version = "0.1.0" dependencies = [ "fatfs", + "sha1_smol", "uuid", "versions", ] +[[package]] +name = "sha1_smol" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d" + [[package]] name = "uuid" version = "1.23.4" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index d5f79aac..3b6d65ee 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -12,6 +12,7 @@ crate-type = [ "staticlib" ] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] fatfs = { git = "https://github.com/rafalh/rust-fatfs", rev = "4eccb50d011146fbed20e133d33b22f3c27292e7", default-features = false, features = ["lfn", "alloc"], optional = true } +sha1_smol = { version = "1.0.1" } uuid = { version = "1.23", default-features = false, optional = true } versions = { path = "./versions" } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 91c1c84a..cf0db8a4 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -26,6 +26,7 @@ pub mod gpu; #[cfg(feature = "chainload")] pub mod nvme; pub mod print; +pub mod usb4; #[cfg(not(test))] use crate::dlmalloc::DLMalloc; diff --git a/rust/src/usb4.rs b/rust/src/usb4.rs new file mode 100644 index 00000000..36e5dc29 --- /dev/null +++ b/rust/src/usb4.rs @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: MIT + +use crate::adt::{ADTNode, AdtError}; +use crate::println; +use sha1_smol::Sha1; + +const PLATFORM_UUID_SALT: [u8; 16] = [ + 0xa6, 0xdd, 0x4c, 0xcb, 0xb5, 0xe8, 0x4a, 0xf5, 0xac, 0xdd, 0xb6, 0xdc, 0x6a, 0x05, 0x42, 0xb8, +]; + +const DROM_UUID_SALT: [u8; 16] = [ + 0x06, 0xbf, 0x07, 0x64, 0x05, 0x78, 0x4a, 0xaa, 0x85, 0xb4, 0x79, 0x08, 0x34, 0x8e, 0xe3, 0x43, +]; + +fn usb4_router_uuid() -> Result { + let node = ADTNode::from_path("/chosen")?; + let unique_chip_id = node.named_prop("unique-chip-id")?.u64()?; + let chip_id = node.named_prop("chip-id")?.u32()?; + + let mut hasher = Sha1::new(); + hasher.update(&PLATFORM_UUID_SALT); + hasher.update(&unique_chip_id.to_le_bytes()); + hasher.update(&chip_id.to_le_bytes()); + + let digest = hasher.digest(); + + let mut uuid: [u8; 16] = digest.bytes()[0..16].try_into().unwrap(); + + uuid[6] = 0x50 | (uuid[6] & 0xf); + uuid[8] = 0x80 | (uuid[8] & 0x3f); + + // println!( + // "IOPlatformUUID: \ + // {:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x} \ + // {:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", + // uuid[0], + // uuid[1], + // uuid[2], + // uuid[3], + // uuid[4], + // uuid[5], + // uuid[6], + // uuid[7], + // uuid[8], + // uuid[9], + // uuid[10], + // uuid[11], + // uuid[12], + // uuid[13], + // uuid[14], + // uuid[15] + // ); + + let mut drom_sha1 = Sha1::new(); + drom_sha1.update(&uuid); + drom_sha1.update(&DROM_UUID_SALT); + + let drom_digest = drom_sha1.digest(); + let drom_bytes: [u8; 8] = drom_digest.bytes()[0..8].try_into().unwrap(); + + let mut drom_uuid: u64 = u64::from_le_bytes(drom_bytes); + + drom_uuid = (drom_uuid & 0xffff_ffff_fff0u64) | (0x05ac << 48); + // println!("USB4 router UUID: {:#08x}", drom_uuid); + + Ok(drom_uuid) +} + +#[no_mangle] +pub unsafe extern "C" fn rust_usb4_router_uuid() -> u64 { + match usb4_router_uuid() { + Ok(uuid) => uuid, + Err(_) => 0, + } +} diff --git a/src/atc.c b/src/atc.c index a3107ba5..3eda4034 100644 --- a/src/atc.c +++ b/src/atc.c @@ -83,8 +83,43 @@ const struct atc_tunable_info atc_tunables_t8122[] = { {"tunable_LN1_TX_SHM_CIO_DFLT", "apple,tunable-lane1-cio", 0x14000, 0x1000, true}, }; +const struct atc_tunable_info atc_tunables_t8130[] = { + {"tunable_ATC0AXI2AF", "apple,tunable-axi2af", 0x0, 0x8000, true}, + {"tunable_ATC_FABRIC", "apple,tunable-common-b", 0x44000, 0x4000, true}, + + {"tunable_ACIOPHY_TOP", "apple,tunable-common-b", 0x0, 0x4000, true}, + {"tunable_AUSCMN_DIG", "apple,tunable-common-b", 0x800, 0x200, true}, + {"tunable_AUSCMN_SHM", "apple,tunable-common-b", 0xa00, 0x200, true}, + {"tunable_ACIOPHY_PLL_TOP", "apple,tunable-common-b", 0x1000, 0x4000, true}, + {"tunable_AUSPLL_CORE", "apple,tunable-common-b", 0x2200, 0x4000, true}, + {"tunable_CLKMON_CFG", "apple,tunable-common-b", 0x2600, 0x100, false}, + {"tunable_CIO3PLL_TOP", "apple,tunable-common-b", 0x2800, 0x200, true}, + {"tunable_CIO3PLL_CORE", "apple,tunable-common-b", 0x2a00, 0x200, true}, + {"tunable_CIO_SHIM", "apple,tunable-common-b", 0x4000, 0x4000, true}, + {"tunable_ACIOPHY_LANE_USBC0", "apple,tunable-common-b", 0x5000, 0x1000, true}, + {"tunable_ACIOPHY_LANE_USBC1", "apple,tunable-common-b", 0x7000, 0x1000, true}, + {"tunable_AUX_TOP", "apple,tunable-common-b", 0x16000, 0x4000, true}, + + {"tunable_LN0_RX_TOP_USB_DFLT", "apple,tunable-lane0-usb", 0x9000, 0x1000, true}, + {"tunable_LN0_RX_TOP_USB_EQA", "apple,tunable-lane0-usb", 0x9000, 0x1000, false}, + {"tunable_LN0_RX_EQ_USB_EQA", "apple,tunable-lane0-usb", 0xa000, 0x1000, true}, + {"tunable_LN0_RX_SHM_USB_DFLT", "apple,tunable-lane0-usb", 0xb000, 0x1000, true}, + {"tunable_LN0_TX_TOP_USB_DFLT", "apple,tunable-lane0-usb", 0xc000, 0x1000, true}, + {"tunable_LN0_TX_SHM_USB_DFLT", "apple,tunable-lane0-usb", 0xd000, 0x1000, true}, + + {"tunable_LN1_RX_TOP_USB_DFLT", "apple,tunable-lane1-usb", 0x10000, 0x1000, true}, + {"tunable_LN1_RX_TOP_USB_EQA", "apple,tunable-lane1-usb", 0x10000, 0x1000, false}, + {"tunable_LN1_RX_EQ_USB_EQA", "apple,tunable-lane1-usb", 0x11000, 0x1000, true}, + {"tunable_LN1_RX_SHM_USB_DFLT", "apple,tunable-lane1-usb", 0x12000, 0x1000, true}, + {"tunable_LN1_TX_TOP_USB_DFLT", "apple,tunable-lane1-usb", 0x13000, 0x1000, true}, + {"tunable_LN1_TX_SHM_USB_DFLT", "apple,tunable-lane1-usb", 0x14000, 0x1000, true}, + + {"tunable_ATC_COMMON_CFG", "apple,tunable-lpdptx", 0x0, 0x8000, true}, +}; + const size_t atc_tunables_t8103_count = ARRAY_SIZE(atc_tunables_t8103); const size_t atc_tunables_t8122_count = ARRAY_SIZE(atc_tunables_t8122); +const size_t atc_tunables_t8130_count = ARRAY_SIZE(atc_tunables_t8130); int atc_get_tunables(int adt_node, const struct atc_tunable_info *info, const struct atc_tunable **tunables) diff --git a/src/atc.h b/src/atc.h index f9c3fb96..56a78e90 100644 --- a/src/atc.h +++ b/src/atc.h @@ -87,6 +87,8 @@ extern const struct atc_tunable_info atc_tunables_t8103[]; extern const size_t atc_tunables_t8103_count; extern const struct atc_tunable_info atc_tunables_t8122[]; extern const size_t atc_tunables_t8122_count; +extern const struct atc_tunable_info atc_tunables_t8130[]; +extern const size_t atc_tunables_t8130_count; /* Return the record count, zero for an absent optional table, or -1 on error. */ int atc_get_tunables(int adt_node, const struct atc_tunable_info *info, diff --git a/src/chainload.c b/src/chainload.c index 1520d27c..9216f586 100644 --- a/src/chainload.c +++ b/src/chainload.c @@ -95,10 +95,14 @@ int chainload_image(void *image, size_t size, char **vars, size_t var_cnt) // Keep the next heap above both the inherited reservation and the loaded image. new_boot_args->top_of_kernel_data = max(cur_boot_args.top_of_kernel_data, new_base + image_size); + /* The copy stub reads these buffers after the MMU and caches are disabled. */ + dc_civac_range(new_image, image_size); + dc_civac_range(adt, cur_boot_args.devtree_size); + // Copy chainload stub void *stub = new_image + image_size; memcpy(stub, _chainload_stub_start, stub_size); - dc_cvau_range(stub, stub_size); + dc_civac_range(stub, stub_size); ic_ivau_range(stub, stub_size); // Set up next stage diff --git a/src/chainload_asm.S b/src/chainload_asm.S index 0ff6f609..1ea22466 100644 --- a/src/chainload_asm.S +++ b/src/chainload_asm.S @@ -11,10 +11,13 @@ _chainload_stub_start: ldp x5, x6, [x1], #16 stp x5, x6, [x2] dc cvau, x2 - ic ivau, x2 add x2, x2, #16 sub x3, x3, #16 cbnz x3, 1b + dsb sy + ic iallu + dsb sy + isb br x4 _chainload_stub_end: diff --git a/src/dapf.c b/src/dapf.c index adb40e01..fced9f1c 100644 --- a/src/dapf.c +++ b/src/dapf.c @@ -239,7 +239,9 @@ struct entry dapf_entries[] = { {"/arm-io/dart-pmp", 1}, {"/arm-io/dart-dcp", 1}, {"/arm-io/dart-dcpext0", 1}, {"/arm-io/dart-isp", 5}, {"/arm-io/dart-isp0", 5}, {"/arm-io/dart-ane", 3}, - {"/arm-io/dart-ave", 3}, {NULL, -1}, + {"/arm-io/dart-ane0", 3}, {"/arm-io/dart-ave", 3}, + {"/arm-io/dart-ave0", 3}, {"/arm-io/dart-ave1", 3}, + {NULL, -1}, }; static int dapf_table_index(const char *path) diff --git a/src/dart.c b/src/dart.c index 44c1a108..0776ffd2 100644 --- a/src/dart.c +++ b/src/dart.c @@ -55,6 +55,7 @@ #define DART_PTE_SP_END GENMASK(51, 40) #define DART_T8020_PTE_OFFSET GENMASK(39, 14) #define DART_T6000_PTE_OFFSET GENMASK(39, 10) +#define DART_T8110_PTE_OFFSET GENMASK(37, 10) #define DART_T8020_PTE_DISABLE_SP BIT(1) #define DART_T6000_PTE_REALTIME BIT(1) #define DART_PTE_VALID BIT(0) @@ -67,6 +68,7 @@ #define DART_T8110_TCR_OFF 0x1000 #define DART_T8110_TCR_REMAP GENMASK(11, 8) #define DART_T8110_TCR_REMAP_EN BIT(7) +#define DART_T8110_TCR_FOUR_LEVELS BIT(3) #define DART_T8110_TCR_BYPASS_DAPF BIT(2) #define DART_T8110_TCR_BYPASS_DART BIT(1) #define DART_T8110_TCR_TRANSLATE_ENABLE BIT(0) @@ -181,7 +183,7 @@ const struct dart_params dart_t8110 = { .sid_count = 256, .pte_flags = FIELD_PREP(DART_PTE_SP_END, 0xfff) | FIELD_PREP(DART_PTE_SP_START, 0) | DART_PTE_VALID, - .offset_mask = DART_T6000_PTE_OFFSET, + .offset_mask = DART_T8110_PTE_OFFSET, .tcr_enabled = DART_T8110_TCR_TRANSLATE_ENABLE, .tcr_disabled = DART_T8110_TCR_BYPASS_DAPF | DART_T8110_TCR_BYPASS_DART, .tcr_off = DART_T8110_TCR_OFF, @@ -367,6 +369,38 @@ void dart_lock_adt(const char *path, int instance) } } +static const struct dart_params *dart_params_for_type(enum dart_type_t type) +{ + switch (type) { + case DART_T8020: + return &dart_t8020; + case DART_T8110: + return &dart_t8110; + case DART_T6000: + return &dart_t6000; + } + + return NULL; +} + +static int dart_fdt_type(void *dt, int node, enum dart_type_t *type, const char **type_s) +{ + if (fdt_node_check_compatible(dt, node, "apple,t8103-dart") == 0) { + *type = DART_T8020; + *type_s = "t8020"; + } else if (fdt_node_check_compatible(dt, node, "apple,t6000-dart") == 0) { + *type = DART_T6000; + *type_s = "t6000"; + } else if (fdt_node_check_compatible(dt, node, "apple,t8110-dart") == 0) { + *type = DART_T8110; + *type_s = "t8110"; + } else { + return -1; + } + + return 0; +} + dart_dev_t *dart_init_fdt(void *dt, u32 phandle, int device, bool keep_pts) { int node = fdt_node_offset_by_phandle(dt, phandle); @@ -383,16 +417,7 @@ dart_dev_t *dart_init_fdt(void *dt, u32 phandle, int device, bool keep_pts) const char *type_s; const char *name = fdt_get_name(dt, node, NULL); - if (fdt_node_check_compatible(dt, node, "apple,t8103-dart") == 0) { - type = DART_T8020; - type_s = "t8020"; - } else if (fdt_node_check_compatible(dt, node, "apple,t6000-dart") == 0) { - type = DART_T6000; - type_s = "t6000"; - } else if (fdt_node_check_compatible(dt, node, "apple,t8110-dart") == 0) { - type = DART_T8110; - type_s = "t8110"; - } else { + if (dart_fdt_type(dt, node, &type, &type_s) < 0) { printf("dart: dart %s at 0x%lx is of an unknown type\n", name, base); return NULL; } @@ -408,6 +433,149 @@ dart_dev_t *dart_init_fdt(void *dt, u32 phandle, int device, bool keep_pts) return dart; } +#define DART_WALK_MAX_TABLES 128 + +struct dart_walk_ctx { + const struct dart_params *params; + dart_walk_cb cb; + void *cookie; + u64 tables[DART_WALK_MAX_TABLES]; + unsigned int table_count; + bool has_pending; + struct dart_mapping pending; +}; + +static bool dart_ram_valid(u64 addr, u64 size) +{ + u64 end = ram_base + mem_size_actual; + return size && addr >= ram_base && addr < end && size <= end - addr; +} + +static int dart_walk_flush(struct dart_walk_ctx *ctx) +{ + if (!ctx->has_pending) + return 0; + ctx->has_pending = false; + return ctx->cb(&ctx->pending, ctx->cookie); +} + +static int dart_walk_table(struct dart_walk_ctx *ctx, u64 table, unsigned int shift, u64 base) +{ + if (!dart_ram_valid(table, SZ_16K) || ctx->table_count >= DART_WALK_MAX_TABLES) { + printf("dart: invalid page-table bounds at 0x%lx\n", table); + return -1; + } + for (unsigned int i = 0; i < ctx->table_count; i++) { + if (ctx->tables[i] == table) { + printf("dart: cyclic page table at 0x%lx\n", table); + return -1; + } + } + ctx->tables[ctx->table_count++] = table; + + const u64 *entries = (const u64 *)table; + for (unsigned int i = 0; i < SZ_16K / sizeof(u64); i++) { + u64 entry = entries[i]; + if (!(entry & DART_PTE_VALID)) + continue; + + u64 phys = FIELD_GET(ctx->params->offset_mask, entry) << DART_PTE_OFFSET_SHIFT; + u64 iova = base + ((u64)i << shift); + + if (shift > DART_PTE_OFFSET_SHIFT) { + int ret = dart_walk_table(ctx, phys, shift - 11, iova); + if (ret) + return ret; + continue; + } + + if (!dart_ram_valid(phys, SZ_16K)) { + printf("dart: mapped page 0x%lx outside RAM\n", phys); + return -1; + } + + if (ctx->has_pending && ctx->pending.phys + ctx->pending.size == phys && + ctx->pending.iova + ctx->pending.size == iova) { + ctx->pending.size += SZ_16K; + continue; + } + + int ret = dart_walk_flush(ctx); + if (ret) + return ret; + + ctx->pending = (struct dart_mapping){.iova = iova, .phys = phys, .size = SZ_16K}; + ctx->has_pending = true; + } + + return 0; +} + +/* Walk an existing hardware DART stream without modifying registers or page tables. */ +int dart_walk_fdt_mappings(void *dt, u32 phandle, u32 sid, dart_walk_cb cb, void *cookie) +{ + if (!dt || !cb) + return -1; + + int node = fdt_node_offset_by_phandle(dt, phandle); + if (node < 0) { + printf("FDT: node for phandle %u not found\n", phandle); + return -1; + } + + const char *status = fdt_getprop(dt, node, "status", NULL); + if (status && !strcmp(status, "disabled")) + return 0; + + u64 base = dt_get_address(dt, node); + if (!base) + return -1; + + enum dart_type_t type; + const char *type_s; + const char *name = fdt_get_name(dt, node, NULL); + if (dart_fdt_type(dt, node, &type, &type_s) < 0) { + printf("dart: dart %s at 0x%lx is of an unknown type\n", name, base); + return -1; + } + + const struct dart_params *params = dart_params_for_type(type); + if (!params || sid >= (u32)params->sid_count) { + printf("dart: stream %u out of range for %s\n", sid, name); + return -1; + } + + u32 tcr = read32(base + params->tcr_off + 4 * sid); + if (!(tcr & params->tcr_enabled)) + return 0; + + unsigned int root_shift = + (type == DART_T8110 && (tcr & DART_T8110_TCR_FOUR_LEVELS)) ? 36 : 25; + + struct dart_walk_ctx *ctx = calloc(1, sizeof(*ctx)); + if (!ctx) + return -1; + ctx->params = params; + ctx->cb = cb; + ctx->cookie = cookie; + + int ret = 0; + for (int ttbr_idx = 0; !ret && ttbr_idx < params->ttbr_count; ttbr_idx++) { + u32 ttbr = read32(base + params->ttbr_off + 4 * params->ttbr_count * sid + 4 * ttbr_idx); + if (!(ttbr & params->ttbr_valid)) + continue; + + u64 root = FIELD_GET(params->ttbr_addr, ttbr) << params->ttbr_shift; + ret = dart_walk_table(ctx, root, root_shift, (u64)ttbr_idx << 36); + } + + if (!ret) + ret = dart_walk_flush(ctx); + + free(ctx); + return ret; +} + int dart_setup_pt_region(dart_dev_t *dart, const char *path, int device, u64 vm_base) { int node = adt_path_offset(adt, path); diff --git a/src/dart.h b/src/dart.h index 739d9454..dc9bf291 100644 --- a/src/dart.h +++ b/src/dart.h @@ -29,6 +29,15 @@ void *dart_translate(dart_dev_t *dart, uintptr_t iova); void *dart_translate_silent(dart_dev_t *dart, uintptr_t iova); u64 dart_search(dart_dev_t *dart, void *paddr); u64 dart_find_iova(dart_dev_t *dart, s64 start, size_t len); +struct dart_mapping { + u64 iova; + u64 phys; + u64 size; +}; + +typedef int (*dart_walk_cb)(const struct dart_mapping *mapping, void *cookie); + +int dart_walk_fdt_mappings(void *dt, u32 phandle, u32 sid, dart_walk_cb cb, void *cookie); void dart_shutdown(dart_dev_t *dart); u64 dart_vm_base(dart_dev_t *dart); diff --git a/src/devicetree.c b/src/devicetree.c index afb4638b..9e241bb2 100644 --- a/src/devicetree.c +++ b/src/devicetree.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ #include "devicetree.h" +#include "utils.h" #include "libfdt/libfdt.h" @@ -67,3 +68,29 @@ u64 dt_get_address(void *dt, int node) return dt_translate(ranges, reg); } + +u64 dt_get_address_translated(void *dt, int node, unsigned index, struct dt_ranges_tbl *ranges) +{ + int reg_len = 0; + const fdt64_t *reg = fdt_getprop(dt, node, "reg", ®_len); + // assume #addr-cells == #size-cells == 2 + unsigned addr_idx = index * 2; + + if (!reg || reg_len < (long)((addr_idx + 2) * sizeof(*reg))) { + printf("FDT: failed to get reg[%u] of %s\n", index, fdt_get_name(dt, node, NULL)); + return 0; + } + + return dt_translate(ranges, reg + addr_idx); +} + +bool dt_node_is_compatible(const void *dt, int nodeoffset, const char **compatibles, + size_t num_compatibles) +{ + for (size_t c = 0; c < num_compatibles; c++) { + if (fdt_node_check_compatible(dt, nodeoffset, compatibles[c]) == 0) + return true; + } + + return false; +} diff --git a/src/devicetree.h b/src/devicetree.h index 84e59e72..643b2267 100644 --- a/src/devicetree.h +++ b/src/devicetree.h @@ -18,5 +18,24 @@ struct dt_ranges_tbl { void dt_parse_ranges(void *dt, int node, struct dt_ranges_tbl *ranges); u64 dt_translate(struct dt_ranges_tbl *ranges, const fdt64_t *reg); u64 dt_get_address(void *dt, int node); +u64 dt_get_address_translated(void *dt, int node, unsigned index, struct dt_ranges_tbl *ranges); + +/** + * dt_node_is_compatible: check a node's compatible property against a list of compatibles + * @fdt: pointer to the device tree blob + * @nodeoffset: offset of a tree node + * @compatibles: string list to match against + * @num_compatibles: number of strings in `@compatibles` + * + * dt_node_check_compatible_any() returns `true` if the given node contains a + * 'compatible' property matching one of the given strings as one of its elements, + * it returns `false` otherwise, or on error. + * + * returns: + * true, if the node has a 'compatible' property listing one of the given strings + * false, otherwise + */ +bool dt_node_is_compatible(const void *fdt, int nodeoffset, const char **compatibles, + size_t num_compatibles); #endif diff --git a/src/dockchannel_uart.c b/src/dockchannel_uart.c index e2418567..5df782f1 100644 --- a/src/dockchannel_uart.c +++ b/src/dockchannel_uart.c @@ -144,11 +144,27 @@ static ssize_t dockchannel_uart_iodev_write(void *opaque, const void *buf, size_ return len; } +static ssize_t dockchannel_uart_iodev_write_nonblock(void *opaque, const void *buf, size_t len) +{ + UNUSED(opaque); + const u8 *p = buf; + size_t wrote = 0; + + if (!uart_base) + return 0; + + while (wrote < len && read32(uart_base + DATA_TX_FREE) > 0) + write32(uart_base + DATA_TX8, p[wrote++]); + + return wrote; +} + static struct iodev_ops iodev_dockchannel_uart_ops = { .can_read = dockchannel_uart_iodev_can_read, .can_write = dockchannel_uart_iodev_can_write, .read = dockchannel_uart_iodev_read, .write = dockchannel_uart_iodev_write, + .write_nonblock = dockchannel_uart_iodev_write_nonblock, }; struct iodev iodev_dockchannel_uart = { diff --git a/src/firmware.c b/src/firmware.c index 18465c80..0c7b3f85 100644 --- a/src/firmware.c +++ b/src/firmware.c @@ -236,6 +236,17 @@ void firmware_parse_version(const char *s, u32 *out) static void detect_firmware(struct fw_version_info *info, const char *ver) { + /* + * J700 uses a different mBoot version compared to every other 26.6.2 + * device. For now just patch around it here, but if this becomes more + * common this should be replaced with an alias table. + */ + if (!strcmp(ver, "mBoot-18000.161.9")) { + *info = fw_versions[V26_6_2]; + info->iboot = ver; + return; + } + for (size_t i = 0; i < ARRAY_SIZE(fw_versions); i++) { if (!strcmp(fw_versions[i].iboot, ver)) { *info = fw_versions[i]; diff --git a/src/gapf.c b/src/gapf.c new file mode 100644 index 00000000..da051fe5 --- /dev/null +++ b/src/gapf.c @@ -0,0 +1,100 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "gapf.h" +#include "adt.h" +#include "string.h" +#include "utils.h" + +#define GAPF_PROTECTION_BASE 0x140 +#define GAPF_PROTECTION_STRIDE 0x40 +#define GAPF_CLOCK_PROTECTION BIT(4) +#define GAPF_UNAVAILABLE 0xdeadbeefU + +#define GAPF_MAX_BANKS 8 + +/* + * Clear any leftover DART clock-protection slices before handing control to + * Linux. On SoCs with GAPF/FPAG clock-protection blocks, firmware may leave + * BIT(4) set on active DART slices, blocking OS PMGR power-state writes. + */ +int gapf_disable_all(void) +{ + int path[8]; + int arm_io = adt_path_offset_trace(adt, "/arm-io", path); + if (arm_io < 0) + return -1; + + size_t depth = 0; + while (path[depth]) + depth++; + if (depth + 1 >= ARRAY_SIZE(path)) + return -1; + + int cleared = 0; + int node = arm_io; + + ADT_FOREACH_CHILD(adt, node) + { + const char *type = adt_getprop(adt, node, "device_type", NULL); + if (!type || strcmp(type, "dart")) + continue; + + u32 slices_len = 0; + const u32 *slices = adt_getprop(adt, node, "clock-protection-slice-index", &slices_len); + if (!slices || !slices_len || (slices_len % sizeof(*slices))) + continue; + + u32 inst_len = 0; + const char *instances = adt_getprop(adt, node, "instance", &inst_len); + if (!instances || !inst_len || (inst_len % 16)) + continue; + + path[depth] = node; + path[depth + 1] = 0; + + u64 fpag_base[GAPF_MAX_BANKS]; + u64 fpag_size[GAPF_MAX_BANKS]; + u32 fpag_count = 0; + u32 trad_count = 0; + + for (u32 i = 0; i < inst_len / 16; i++) { + const char *tag = instances + i * 16; + if (!memcmp(tag, "TRAD", 4)) { + trad_count++; + } else if (!memcmp(tag, "FPAG", 4) && fpag_count < GAPF_MAX_BANKS) { + if (adt_get_reg(adt, path, "reg", i, &fpag_base[fpag_count], + &fpag_size[fpag_count]) == 0) + fpag_count++; + } + } + + if (!trad_count || !fpag_count) + continue; + + u32 count = min(slices_len / (u32)sizeof(*slices), fpag_count * trad_count); + for (u32 i = 0; i < count; i++) { + u32 slice = slices[i]; + if (!slice) + continue; + + u32 fpag_idx = i / trad_count; + u64 offset = GAPF_PROTECTION_BASE + (u64)(slice - 1) * GAPF_PROTECTION_STRIDE; + if (offset + sizeof(u32) > fpag_size[fpag_idx]) + continue; + + u64 addr = fpag_base[fpag_idx] + offset; + u32 val = read32(addr); + if (val == GAPF_UNAVAILABLE || val == UINT32_MAX) + continue; + + if (val & GAPF_CLOCK_PROTECTION) { + write32(addr, val & ~GAPF_CLOCK_PROTECTION); + printf("gapf: Cleared clock protection on /arm-io/%s slice %u\n", + adt_get_name(adt, node), slice); + cleared++; + } + } + } + + return cleared; +} diff --git a/src/gapf.h b/src/gapf.h new file mode 100644 index 00000000..17e6ce5b --- /dev/null +++ b/src/gapf.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef GAPF_H +#define GAPF_H + +int gapf_disable_all(void); + +#endif diff --git a/src/iodev.c b/src/iodev.c index 4ac83016..0827b0d2 100644 --- a/src/iodev.c +++ b/src/iodev.c @@ -105,6 +105,19 @@ ssize_t iodev_write(iodev_id_t id, const void *buf, size_t length) return ret; } +ssize_t iodev_write_nonblock(iodev_id_t id, const void *buf, size_t length) +{ + if (!iodevs[id] || !iodevs[id]->ops->write_nonblock) + return iodev_write(id, buf, length); + + if (mmu_active()) + spin_lock(&iodevs[id]->lock); + ssize_t ret = iodevs[id]->ops->write_nonblock(iodevs[id]->opaque, buf, length); + if (mmu_active()) + spin_unlock(&iodevs[id]->lock); + return ret; +} + ssize_t iodev_queue(iodev_id_t id, const void *buf, size_t length) { if (!iodevs[id] || !iodevs[id]->ops->queue) @@ -159,8 +172,15 @@ static void _iodev_console_write_helper(iodev_id_t iod, char prefix, const void if (!(iodevs[iod]->usage & USAGE_CONSOLE)) return; - iodevs[iod]->ops->write(iodevs[iod]->opaque, &prefix, 1); - iodevs[iod]->ops->write(iodevs[iod]->opaque, buf, length); + ssize_t (*write_fn)(void *, const void *, size_t) = + iodevs[iod]->ops->write_nonblock ?: iodevs[iod]->ops->write; + + if (!write_fn) + return; + + if (write_fn(iodevs[iod]->opaque, &prefix, 1) <= 0) + return; + write_fn(iodevs[iod]->opaque, buf, length); } void iodev_console_write(const void *buf, size_t length) @@ -210,7 +230,7 @@ void iodev_console_write(const void *buf, size_t length) size_t block = min(con_wp - con_rp[id], CONSOLE_BUFFER_SIZE - buf_rp); dprintf(" write buf %d\n", block); - ssize_t ret = iodev_write(id, &con_buf[buf_rp], block); + ssize_t ret = iodev_write_nonblock(id, &con_buf[buf_rp], block); if (ret <= 0) goto next_dev; @@ -223,7 +243,7 @@ void iodev_console_write(const void *buf, size_t length) // Write the current buffer while (wrote < length) { - ssize_t ret = iodev_write(id, p, length - wrote); + ssize_t ret = iodev_write_nonblock(id, p, length - wrote); if (ret <= 0) goto next_dev; diff --git a/src/iodev.h b/src/iodev.h index 9259715b..931e018e 100644 --- a/src/iodev.h +++ b/src/iodev.h @@ -30,6 +30,7 @@ struct iodev_ops { bool (*can_write)(void *opaque); ssize_t (*read)(void *opaque, void *buf, size_t length); ssize_t (*write)(void *opaque, const void *buf, size_t length); + ssize_t (*write_nonblock)(void *opaque, const void *buf, size_t length); ssize_t (*queue)(void *opaque, const void *buf, size_t length); void (*flush)(void *opaque); void (*handle_events)(void *opaque); @@ -50,6 +51,7 @@ ssize_t iodev_can_read(iodev_id_t id); bool iodev_can_write(iodev_id_t id); ssize_t iodev_read(iodev_id_t id, void *buf, size_t length); ssize_t iodev_write(iodev_id_t id, const void *buf, size_t length); +ssize_t iodev_write_nonblock(iodev_id_t id, const void *buf, size_t length); ssize_t iodev_queue(iodev_id_t id, const void *buf, size_t length); void iodev_flush(iodev_id_t id); void iodev_handle_events(iodev_id_t id); diff --git a/src/isp.c b/src/isp.c index ddce629f..bc6a98af 100644 --- a/src/isp.c +++ b/src/isp.c @@ -1,28 +1,11 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ #include "adt.h" -#include "dart.h" #include "firmware.h" #include "isp.h" -#include "pmgr.h" #include "soc.h" #include "utils.h" -#define ISP_ASC_VERSION 0x1800000 - -#define ISP_VER_T8103 0xb0090 -#define ISP_VER_T6000 0xb3091 -#define ISP_VER_T8112 0xc1090 -#define ISP_VER_T6020 0xc3091 -#define ISP_VER_T8122 0xf1001 -#define ISP_VER_T603X 0xf3001 - -// PMGR offset to enable to get the version info to work -#define ISP_PMGR_T8103 0x4018 -#define ISP_PMGR_T6000 0x8 -#define ISP_PMGR_T6020 0x4008 -#define ISP_PMGR_T6031 0x4030 - static bool isp_initialized = false; static u64 heap_phys, heap_iova, heap_size, heap_top; @@ -41,8 +24,9 @@ u64 isp_iova_base(void) { switch (chip_id) { case 0x6020 ... 0x6fff: - case 0x8122: + case T8122: case T8132: + case T8140: return 0x10000000000; default: return 0; @@ -51,85 +35,18 @@ u64 isp_iova_base(void) int isp_init(void) { - int err = 0; - - const char *isp_path = "/arm-io/isp"; - const char *dart_path = "/arm-io/dart-isp"; - - int adt_path[8], adt_isp_path[8]; - int isp_node = adt_path_offset_trace(adt, isp_path, adt_isp_path); - int node = adt_path_offset_trace(adt, dart_path, adt_path); + int isp_node = adt_path_offset(adt, "/arm-io/isp"); + int node = adt_path_offset(adt, "/arm-io/dart-isp"); if (node < 0 || isp_node < 0) { - isp_path = "/arm-io/isp0"; - dart_path = "/arm-io/dart-isp0"; - isp_node = adt_path_offset_trace(adt, isp_path, adt_isp_path); - node = adt_path_offset_trace(adt, dart_path, adt_path); + isp_node = adt_path_offset(adt, "/arm-io/isp0"); + node = adt_path_offset(adt, "/arm-io/dart-isp0"); } - if (node < 0) + if (node < 0 || isp_node < 0) return 0; - /* - * T8132 (M4) has the M4 Max's H16 ISP and runs the same macOS 26.6.2 - * firmware, whose shared allocations start at 32 MiB (the M4 Max value); - * the J713 preloaded segments end at 0x13ec000. Leave the ISP in its - * iBoot state, as for the M4 Max: the ADT power-gates also switch the - * sensor domains, and the H16 version register is not known. - */ - if (chip_id == T8132) { - heap_top = 0x2000000; - goto setup_heap; - } - - if (pmgr_adt_power_enable(isp_path) < 0) - return -1; - - u64 isp_base; - u64 pmgr_base; - err = adt_get_reg(adt, adt_isp_path, "reg", 0, &isp_base, NULL); - if (err) - return err; - - err = adt_get_reg(adt, adt_isp_path, "reg", 1, &pmgr_base, NULL); - if (err) - return err; - - u32 pmgr_off; switch (chip_id) { case T8103: case T8112: - case T8122: - pmgr_off = ISP_PMGR_T8103; - break; - case T6000 ... T6002: - pmgr_off = ISP_PMGR_T6000; - break; - case T6020 ... T6022: - case T6030: - pmgr_off = ISP_PMGR_T6020; - break; - case T6031 ... T6034: - pmgr_off = ISP_PMGR_T6031; - break; - default: - printf("isp: Unsupported SoC\n"); - return -1; - } - - err = pmgr_set_mode(pmgr_base + pmgr_off, PMGR_PS_ACTIVE); - if (err) { - printf("isp: Failed to power on\n"); - return err; - } - - u32 ver_rev = read32(isp_base + ISP_ASC_VERSION); - printf("isp: Version 0x%x\n", ver_rev); - - pmgr_set_mode(pmgr_base + pmgr_off, PMGR_PS_PWRGATE); - - /* TODO: confirm versions */ - switch (ver_rev) { - case ISP_VER_T8103: - case ISP_VER_T8112: switch (os_firmware.version) { case V12_3 ... V12_4: heap_top = 0x1800000; @@ -142,7 +59,7 @@ int isp_init(void) return -1; } break; - case ISP_VER_T6000: + case T6000 ... T6002: switch (os_firmware.version) { case V12_3: heap_top = 0xe00000; @@ -156,7 +73,7 @@ int isp_init(void) return -1; } break; - case ISP_VER_T6020: + case T6020 ... T6022: switch (os_firmware.version) { case V13_5: case V13_6_1: @@ -167,8 +84,8 @@ int isp_init(void) return -1; } break; - case ISP_VER_T603X: - case ISP_VER_T8122: + case T6030 ... T6034: + case T8122: switch (os_firmware.version) { case V14_7: heap_top = 0x1000000; @@ -178,12 +95,18 @@ int isp_init(void) return -1; } break; + case T8132: + case T6040 ... T6041: + heap_top = 0x2000000; + break; + case T8140: + heap_top = 0x2200000; + break; default: - printf("isp: unknown revision 0x%x\n", ver_rev); + printf("isp: Unsupported SoC\n"); return -1; } -setup_heap:; const struct adt_segment_ranges *seg; u32 segments_len; @@ -209,8 +132,5 @@ setup_heap:; printf("isp: Heap: 0x%lx..0x%lx (0x%lx @ 0x%lx)\n", heap_iova, heap_top, heap_size, heap_phys); isp_initialized = true; - - if (chip_id != T8132) - pmgr_adt_power_disable(isp_path); - return err; + return 0; } diff --git a/src/kboot.c b/src/kboot.c index f6271198..838bd2b5 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -11,9 +11,11 @@ #include "display.h" #include "exception.h" #include "firmware.h" +#include "gapf.h" #include "iodev.h" #include "isp.h" #include "kboot_atc.h" +#include "kboot_utils.h" #include "malloc.h" #include "mcc.h" #include "memory.h" @@ -313,8 +315,37 @@ static int dt_set_chosen(void) if (fdt_setprop(dt, node, "asahi,m1n1-stage2-version", m1n1_version, strlen(m1n1_version) + 1)) bail("FDT: couldn't set asahi,m1n1-stage2-version\n"); - if (dt_set_rng_seed_sep(node)) - return dt_set_rng_seed_adt(node); + if (dt_set_rng_seed_sep(node)) { + if (dt_set_rng_seed_adt(node)) + bail("FDT: couldn't set random seed"); + } + + if (!cpu_features->apple_sysregs_unlocked) { + /* + * M4 and later lose architectural state in WFI and WFIT. Add the + * bootargs to prevent Linux from crashing in the idle loop and delay + * function. + */ + const char to_append[] = " idle=nop arm64.nowfxt"; + printf("FDT: appending to bootargs:%s\n", to_append); + + /* + * fdt_appendprop_string would create another property value: + * "a", "b". But we want "ab", so manually create or grow the prop, + * then overwrite the \0 of the existing string. + */ + int oldlen, newlen; + void *prop_data; + + if (!fdt_get_property(dt, node, "bootargs", &oldlen) || oldlen < 1) + oldlen = 1; + newlen = oldlen + sizeof(to_append) - 1; + + if (fdt_setprop_placeholder(dt, node, "bootargs", newlen, &prop_data)) + bail("FDT: failed to append to 'bootargs' property\n"); + + strncpy(prop_data + oldlen - 1, to_append, sizeof(to_append)); + } return 0; } @@ -400,6 +431,32 @@ static int dt_set_memory(void) } } + /* iBoot's trust cache may remain write-protected on secondary CPUs. */ + int memory_map = adt_path_offset(adt, "/chosen/memory-map"); + u64 trustcache[2]; + if (memory_map >= 0 && + ADT_GETPROP_ARRAY(adt, memory_map, "TrustCache", trustcache) == sizeof(trustcache) && + trustcache[1]) { + u64 raw_end; + if (__builtin_add_overflow(trustcache[0], trustcache[1], &raw_end) || + raw_end > UINT64_MAX - (get_page_size() - 1)) + bail("FDT: Invalid iBoot trust cache range\n"); + + u64 start = ALIGN_DOWN(trustcache[0], get_page_size()); + u64 end = ALIGN_UP(raw_end, get_page_size()); + + if (end <= dram_min || start >= dram_max) { + printf("FDT: iBoot trust cache 0x%lx..0x%lx outside usable memory, not reserving\n", + start, end); + } else { + if (start < dram_min || end > dram_max) + bail("FDT: iBoot trust cache 0x%lx..0x%lx straddles usable memory\n", start, end); + printf("FDT: Reserving iBoot trust cache 0x%lx..0x%lx\n", start, end); + if (fdt_add_mem_rsv(dt, start, end - start)) + bail("FDT: Could not reserve iBoot trust cache\n"); + } + } + printf("FDT: DRAM at 0x%lx size 0x%lx\n", dram_base, dram_size); printf("FDT: Usable memory is 0x%lx..0x%lx (0x%lx)\n", dram_min, dram_max, dram_max - dram_min); @@ -888,20 +945,20 @@ static int dt_set_bluetooth(void) static int dt_set_multitouch(void) { - const char *path = fdt_get_alias(dt, "touchbar0"); - if (path == NULL) - return 0; - - int node = fdt_path_offset(dt, path); - if (node < 0) - bail("FDT: alias points at nonexistent node\n"); - const char *adt_touchbar; - if (fdt_node_check_compatible(dt, 0, "apple,j293") == 0) + int node; + + if (fdt_node_check_compatible(dt, 0, "apple,j293") == 0) { adt_touchbar = "/arm-io/spi0/multi-touch"; - else if (fdt_node_check_compatible(dt, 0, "apple,j493") == 0) + node = fdt_node_offset_by_compatible(dt, 0, "apple,j293-touchbar"); + } else if (fdt_node_check_compatible(dt, 0, "apple,j493") == 0) { adt_touchbar = "/arm-io/spi3/touch-bar"; - else + node = fdt_node_offset_by_compatible(dt, 0, "apple,j493-touchbar"); + } else { + return 0; + } + + if (node < 0) return 0; int anode = adt_path_offset(adt, adt_touchbar); @@ -1136,13 +1193,22 @@ static const struct adt_tunable_info usb4_nhi_tunables[] = { {"fw_int_ctl_management_tunables", "apple,tunable-nhi", 0x4000, 0x4000, true}, }; +/* CIO v2 integrates the old descriptor/data fabrics into the NHI and + * publishes only the two merge-fabric tables plus interrupt management. + */ +static const struct adt_tunable_info usb4_nhi_tunables_v2[] = { + {"hi_up_merge_fabric_tunables", "apple,tunable-nhi", 0xf8000, 0x4000, true}, + {"hi_dn_merge_fabric_tunables", "apple,tunable-nhi", 0xfc000, 0x4000, true}, + {"fw_int_ctl_management_tunables", "apple,tunable-nhi", 0x4000, 0x4000, true}, +}; + static const struct adt_tunable_info usb4_rc_tunables[] = { {"top_tunables", "apple,tunable-rc", 0x0, 0x4000, true}, {"hbw_fabric_tunables", "apple,tunable-rc", 0x4000, 0x4000, true}, {"lbw_fabric_tunables", "apple,tunable-rc", 0x8000, 0x4000, true}, }; -static const struct adt_tunable_info usb4_pcie_adapter_tunables[] = { +__always_unused static const struct adt_tunable_info usb4_pcie_adapter_tunables[] = { {"pcie_adapter_regs_tunables", "apple,tunable-pcie-adapter", 0x0, 0x4000, true}, }; @@ -1211,43 +1277,42 @@ static int dt_append_acio_tunable(int adt_node, int fdt_node, return 0; } -static int dt_copy_usb4_drom(const char *adt_path, const char *dt_alias) +static u8 crc8(u8 *b, size_t len) { - int adt_node = adt_path_offset(adt, adt_path); - if (adt_node < 0) - return -1; + u8 value = 0xff; - const char *fdt_path = fdt_get_alias(dt, dt_alias); - if (fdt_path == NULL) - bail("FDT: Unable to find alias %s\n", dt_alias); + for (unsigned i = 0; i < len; i++) { + value /*= value*/ ^= b[i]; + for (unsigned j = 0; j < 8; j++) + value = (value << 1) ^ ((value & 0x80) ? 7 : 0); + } - int fdt_node = fdt_path_offset(dt, fdt_path); - if (fdt_node < 0) - bail("FDT: Unable to find path %s for alias %s\n", fdt_path, dt_alias); + return value; +} +static int dt_copy_usb4_drom(int adt_node, int fdt_node, u64 router_uuid) +{ + u8 drom[256]; u32 drom_len; const u8 *drom_blob = adt_getprop(adt, adt_node, "thunderbolt-drom", &drom_len); if (!drom_blob || !drom_len) bail("ADT: Failed to get thunderbolt-drom\n"); - return fdt_setprop(dt, fdt_node, "apple,thunderbolt-drom", drom_blob, drom_len); + if (drom_len > sizeof(drom)) + bail("ADT: thunderbolt-drom exceed buffer size (%u > %zu)\n", drom_len, sizeof(drom)); + + memcpy(drom, drom_blob, drom_len); + memcpy(&drom[1], &router_uuid, sizeof(router_uuid)); + + drom[0] = crc8(&drom[1], 8); + + return fdt_setprop(dt, fdt_node, "apple,thunderbolt-drom", drom, drom_len); } -static int dt_copy_acio_tunables(const char *adt_path, const char *dt_alias, - const struct adt_tunable_info *tunables, size_t n_tunables) +static int __dt_copy_acio_tunables(int adt_node, int fdt_node, + const struct adt_tunable_info *tunables, size_t n_tunables) { int ret; - int adt_node = adt_path_offset(adt, adt_path); - if (adt_node < 0) - return -1; - - const char *fdt_path = fdt_get_alias(dt, dt_alias); - if (fdt_path == NULL) - bail("FDT: Unable to find alias %s\n", dt_alias); - - int fdt_node = fdt_path_offset(dt, fdt_path); - if (fdt_node < 0) - bail("FDT: Unable to find path %s for alias %s\n", fdt_path, dt_alias); for (size_t i = 0; i < n_tunables; ++i) { ret = dt_append_acio_tunable(adt_node, fdt_node, &tunables[i]); @@ -1264,36 +1329,78 @@ static int dt_copy_acio_tunables(const char *adt_path, const char *dt_alias, return -1; } -static int dt_set_acio_tunables(void) +const char *nhi_compatibles[] = { + "apple,t8103-usb4-nhi", +}; + +struct acio_match_data { + u64 router_uuid; + bool v2; +}; + +static int dt_copy_acio_tunables(void *dt, int adt_node, int fdt_node, int index, void *data) { - char adt_path[32]; - char fdt_alias[32]; + const struct acio_match_data *match = data; + bool is_v2 = adt_is_compatible_at(adt, adt_node, "acio,v2", 0); + if (!match || is_v2 != match->v2) + return 0; - for (int i = 0; i < MAX_CIO_DEVS; ++i) { - memset(adt_path, 0, sizeof(adt_path)); - snprintf(adt_path, sizeof(adt_path), "/arm-io/acio%d", i); + const struct adt_tunable_info *nhi_tunables = + is_v2 ? usb4_nhi_tunables_v2 : usb4_nhi_tunables; + size_t n_nhi_tunables = + is_v2 ? ARRAY_SIZE(usb4_nhi_tunables_v2) : ARRAY_SIZE(usb4_nhi_tunables); + int node; + int ret; - memset(fdt_alias, 0, sizeof(fdt_alias)); - snprintf(fdt_alias, sizeof(fdt_alias), "usb4_%d_rc", i); - dt_copy_acio_tunables(adt_path, fdt_alias, usb4_rc_tunables, - sizeof(usb4_rc_tunables) / sizeof(*usb4_rc_tunables)); + ret = + __dt_copy_acio_tunables(adt_node, fdt_node, usb4_rc_tunables, ARRAY_SIZE(usb4_rc_tunables)); + if (ret < 0) + return ret; - memset(fdt_alias, 0, sizeof(fdt_alias)); - snprintf(fdt_alias, sizeof(fdt_alias), "usb4_%d_pcie_adapter", i); - dt_copy_acio_tunables(adt_path, fdt_alias, usb4_pcie_adapter_tunables, - sizeof(usb4_pcie_adapter_tunables) / - sizeof(*usb4_pcie_adapter_tunables)); + fdt_for_each_subnode(node, dt, fdt_node) + { + if (!dt_node_is_compatible(dt, node, nhi_compatibles, ARRAY_SIZE(nhi_compatibles))) + continue; - memset(fdt_alias, 0, sizeof(fdt_alias)); - snprintf(fdt_alias, sizeof(fdt_alias), "usb4_%d_nhi", i); - dt_copy_acio_tunables(adt_path, fdt_alias, usb4_nhi_tunables, - sizeof(usb4_nhi_tunables) / sizeof(*usb4_nhi_tunables)); - dt_copy_usb4_drom(adt_path, fdt_alias); + ret = __dt_copy_acio_tunables(adt_node, node, nhi_tunables, n_nhi_tunables); + if (ret < 0) + return ret; + + if (match->router_uuid) + dt_copy_usb4_drom(adt_node, node, match->router_uuid | index); } return 0; } +#define ADT_T8103_USB4_ACIO_REG_NUM 2 +#define ADT_T6040_USB4_ACIO_REG_NUM 1 +#define FDT_T8103_USB4_ACIO_REG_NUM 0 + +const char *acio_compatibles[] = { + "apple,t8103-usb4-acio", +}; + +static int dt_set_acio_tunables(void) +{ + u64 router_uuid = rust_usb4_router_uuid(); + + if (!router_uuid) + bail("ADT: unable to generate USB4 router UUID\n"); + + struct acio_match_data v1 = {.router_uuid = router_uuid, .v2 = false}; + kboot_setup_matching_nodes(dt, MAX_CIO_DEVS, "acio", ADT_T8103_USB4_ACIO_REG_NUM, + acio_compatibles, ARRAY_SIZE(acio_compatibles), + FDT_T8103_USB4_ACIO_REG_NUM, dt_copy_acio_tunables, &v1); + + struct acio_match_data v2 = {.router_uuid = router_uuid, .v2 = true}; + kboot_setup_matching_nodes(dt, MAX_CIO_DEVS, "acio", ADT_T6040_USB4_ACIO_REG_NUM, + acio_compatibles, ARRAY_SIZE(acio_compatibles), + FDT_T8103_USB4_ACIO_REG_NUM, dt_copy_acio_tunables, &v2); + + return 0; +} + static const struct adt_tunable_info pciec_tunables[] = { {"atc-apcie-debug-tunables", "apple,tunable-debug", 0, 0x2000, true}, {"atc-apcie-fabric-tunables", "apple,tunable-fabric", 0, 0x4000, true}, @@ -1718,6 +1825,34 @@ static int dt_carveout_reserved_regions(const char *dcp_alias, const char *disp_ if (node < 0) bail("ADT: '/chosen/carveout-memory-map' not found\n"); + /* + * On A18 and M5+, iBoot places the coprocessor's __SHARED_DATA_RW firmware + * segment at the tail of a DCP data carveout (such as region-id-50/73/88), + * mapped at a separate DART IOVA from the preceding data prefix. Because + * dt_add_reserved_regions() expects one contiguous DART mapping per + * carveout and the trailing segment is reserved separately from + * segment-ranges, look up the coprocessor's segment-ranges here so we can + * trim any carveout whose tail matches a firmware segment. On earlier SoCs + * where a segment covers the entire carveout (phys == paddr), this is a + * no-op. + */ + const struct adt_segment_ranges *segments = NULL; + u32 seg_count = 0; + char dcp_adt_path[48]; + snprintf(dcp_adt_path, sizeof(dcp_adt_path), "/arm-io/%s", dcp_alias); + int dcp_adt_node = adt_path_offset(adt, dcp_adt_path); + if (dcp_adt_node < 0 && !strcmp(dcp_alias, "dcp")) + dcp_adt_node = adt_path_offset(adt, "/arm-io/dcp0"); + if (dcp_adt_node >= 0) { + u32 seg_len; + const struct adt_segment_ranges *prop = + adt_getprop(adt, dcp_adt_node, "segment-ranges", &seg_len); + if (prop && seg_len >= sizeof(*prop) && !(seg_len % sizeof(*prop))) { + segments = prop; + seg_count = seg_len / sizeof(*prop); + } + } + /* read physical addresses of reserved memory regions */ /* do this up front to avoid errors after modifying the DT */ for (unsigned i = 0; i < num_maps; i++) { @@ -1735,6 +1870,18 @@ static int dt_carveout_reserved_regions(const char *dcp_alias, const char *disp_ region[i].paddr = phys_map[0]; region[i].size = phys_map[1]; + + // Trim a trailing firmware segment if necessary + for (u32 j = 0; j < seg_count; j++) { + if (!segments[j].size) + continue; + u64 seg_end = segments[j].phys + ALIGN_UP(segments[j].size, SZ_16K); + if (segments[j].phys > region[i].paddr && + seg_end == region[i].paddr + region[i].size) { + region[i].size = segments[j].phys - region[i].paddr; + break; + } + } } return dt_add_reserved_regions(dcp_alias, disp_alias, piodma_alias, "apple,asc-mem", maps, @@ -1774,6 +1921,116 @@ static int dt_vram_reserved_region(const char *dcp_alias, const char *disp_alias disp_reserved_regions_vram, ®ion, 1); } +struct dt_dart_reserve_ctx { + const char *alias; + u32 dev_phandle; + u32 sid; + unsigned int count; +}; + +static int dt_reserve_dart_mapping_cb(const struct dart_mapping *m, void *cookie) +{ + struct dt_dart_reserve_ctx *ctx = cookie; + char name[64]; + + snprintf(name, sizeof(name), "%s-dart-%u@%lx", ctx->alias, ctx->sid, m->phys); + int mem = dt_get_or_add_reserved_mem(name, "apple,asc-mem", true, m->phys, m->size); + if (mem < 0) + return mem; + + u32 mem_phandle = fdt_get_phandle(dt, mem); + int ret = dt_device_set_reserved_mem(mem, name, ctx->dev_phandle, m->iova, m->size); + if (ret < 0) + return ret; + + ret = dt_device_add_mem_region(ctx->alias, mem_phandle, NULL); + if (ret < 0) + return ret; + + ctx->count++; + return 0; +} + +static int dt_reserve_dart_mappings(const char *alias) +{ + const char *fdt_path = fdt_get_alias(dt, alias); + if (!fdt_path) + return 0; + + int fdt_node = fdt_path_offset(dt, fdt_path); + if (fdt_node < 0) + return 0; + + const char *status = fdt_getprop(dt, fdt_node, "status", NULL); + if (status && !strcmp(status, "disabled")) + return 0; + + u32 dev_phandle = fdt_get_phandle(dt, fdt_node); + if (!dev_phandle) { + int ret = fdt_generate_phandle(dt, &dev_phandle); + if (!ret) + ret = fdt_setprop_u32(dt, fdt_node, "phandle", dev_phandle); + if (ret) + bail("FDT: couldn't set '%s.phandle' property: %d\n", fdt_path, ret); + fdt_node = fdt_path_offset(dt, fdt_path); + if (fdt_node < 0) + return -1; + } + + int len; + const fdt32_t *iommus = fdt_getprop(dt, fdt_node, "iommus", &len); + if (!iommus || len <= 0 || (len % sizeof(fdt32_t))) + return 0; + + struct { + u32 phandle; + u32 sid; + } streams[16]; + size_t nstreams = 0; + + /* Copy stream specifiers up front; FDT mutations below invalidate pointers. */ + size_t ncells = (size_t)len / sizeof(fdt32_t); + for (size_t idx = 0; idx < ncells;) { + u32 iommu_ph = fdt32_ld(&iommus[idx++]); + int iommu_node = fdt_node_offset_by_phandle(dt, iommu_ph); + if (iommu_node < 0) + bail("FDT: '%s' references unknown iommu phandle %u\n", alias, iommu_ph); + + int clen; + const fdt32_t *cells_prop = fdt_getprop(dt, iommu_node, "#iommu-cells", &clen); + if (!cells_prop || clen != sizeof(fdt32_t)) + bail("FDT: missing #iommu-cells on iommu for '%s'\n", alias); + + u32 cells = fdt32_ld(cells_prop); + if (cells < 1 || idx + cells > ncells || nstreams >= ARRAY_SIZE(streams)) + bail("FDT: invalid iommus specifier on '%s'\n", alias); + + streams[nstreams].phandle = iommu_ph; + streams[nstreams].sid = fdt32_ld(&iommus[idx]); + nstreams++; + idx += cells; + } + + for (size_t i = 0; i < nstreams; i++) { + struct dt_dart_reserve_ctx ctx = { + .alias = alias, + .dev_phandle = dev_phandle, + .sid = streams[i].sid, + .count = 0, + }; + int ret = dart_walk_fdt_mappings(dt, streams[i].phandle, streams[i].sid, + dt_reserve_dart_mapping_cb, &ctx); + if (ret < 0) + bail("FDT: failed to export preloaded DART mappings for '%s' stream %u\n", alias, + streams[i].sid); + if (ctx.count) + printf("FDT: %s: reserved %u preloaded DART mappings (stream %u)\n", alias, ctx.count, + streams[i].sid); + } + + return 0; +} + static int dt_reserve_asc_firmware(const char *adt_path, const char *adt_path_alt, const char *fdt_path, bool remap, u64 base) { @@ -1804,9 +2061,30 @@ static int dt_reserve_asc_firmware(const char *adt_path, const char *adt_path_al u32 segments_len; seg = adt_getprop(adt, node, "segment-ranges", &segments_len); + if (!seg || !segments_len) + return 0; + if (segments_len % sizeof(*seg)) + bail("ADT: invalid ASC segment ranges for '%s'\n", adt_path); unsigned int num_maps = segments_len / sizeof(*seg); - for (unsigned i = 0; i < num_maps; i++) { + u32 names_len = 0; + const char *names = adt_getprop(adt, node, "segment-names", &names_len); + const char *name = names; + + for (unsigned i = 0; i < num_maps; i++, seg++) { + bool is_oslog = false; + if (name && name < names + names_len) { + size_t rem = names + names_len - name; + size_t len = strnlen(name, rem); + const char *sep = memchr(name, ';', len); + size_t tok_len = sep ? (size_t)(sep - name) : len; + is_oslog = tok_len == 8 && !memcmp(name, "__OS_LOG", 8); + name = sep ? sep + 1 : NULL; + } + /* Linux RTKit does not use iBoot's __OS_LOG metadata segment. */ + if (is_oslog) + continue; + u64 iova = (remap ? seg->remap : seg->iova) | base; char node_name[64]; @@ -1821,7 +2099,7 @@ static int dt_reserve_asc_firmware(const char *adt_path, const char *adt_path_al int mem_node = dt_get_or_add_reserved_mem(node_name, "apple,asc-mem", true, seg->phys, seg_size); if (mem_node < 0) - return ret; + return mem_node; uint32_t mem_phandle = fdt_get_phandle(dt, mem_node); /* Identity segments are reserved data, not existing device mappings. */ @@ -1831,11 +2109,12 @@ static int dt_reserve_asc_firmware(const char *adt_path, const char *adt_path_al return ret; } - ret = dt_device_add_mem_region(fdt_path, mem_phandle, NULL); + const char *region_name = NULL; + if (strcmp(fdt_path, "ane") == 0) + region_name = i == 0 ? "text" : (i == 1 ? "data" : NULL); + ret = dt_device_add_mem_region(fdt_path, mem_phandle, region_name); if (ret < 0) return ret; - - seg++; } return 0; @@ -1961,8 +2240,52 @@ static struct disp_mapping disp_reserved_regions_t602x[] = { {"region-id-157", "region157", true, true, false}, }; +/* Export opaque display power-LUT properties from /arm-io/disp0 when present, + * so the DCP driver can serve them back to firmware without embedding per-panel + * calibration tables. */ +static int dt_set_display_provider_properties(void) +{ + int anode = adt_path_offset(adt, "/arm-io/disp0"); + if (anode < 0) + return 0; + + int dcp = fdt_path_offset(dt, "dcp"); + if (dcp < 0) + return 0; + + ADT_FOREACH_PROPERTY(adt, anode, prop) + { + if (strnlen(prop->name, sizeof(prop->name)) == sizeof(prop->name)) + bail("DCP: unterminated disp0 property name\n"); + if (strncmp(prop->name, "power-lut-", 10)) + continue; + + u32 size = prop->size & 0x7fffffff; + if (!size || size > 4096) + bail("DCP: invalid display provider property %s (%u bytes)\n", prop->name, size); + + dcp = fdt_path_offset(dt, "dcp"); + if (dcp < 0) + bail("DCP: lost dcp node\n"); + + int child = fdt_subnode_offset(dt, dcp, "apple,provider-properties"); + if (child == -FDT_ERR_NOTFOUND) + child = fdt_add_subnode(dt, dcp, "apple,provider-properties"); + if (child < 0) + bail("DCP: cannot create provider properties node\n"); + + if (fdt_setprop(dt, child, prop->name, prop->value, size) < 0) + bail("DCP: cannot export display provider property %s\n", prop->name); + } + + return 0; +} + static int dt_set_display(void) { + if (dt_set_display_provider_properties() < 0) + return -1; + /* lock dart-disp0 to prevent old software from resetting it */ dart_lock_adt("/arm-io/dart-disp0", 0); @@ -1984,6 +2307,27 @@ static int dt_set_display(void) ret = dt_carveout_reserved_regions("dcpext", NULL, NULL, dcpext_reserved_regions_t8103, ARRAY_SIZE(dcpext_reserved_regions_t8103)); + } else if (!fdt_node_check_compatible(dt, 0, "apple,t8140")) { + if (fdt_get_alias(dt, "dcp") && + dt_reserve_asc_firmware("/arm-io/dcp", NULL, "dcp", true, 0)) + return -1; + if (fdt_get_alias(dt, "dcpext") && + dt_reserve_asc_firmware("/arm-io/dcpext", NULL, "dcpext", true, 0)) + return -1; + + ret = dt_carveout_reserved_regions("dcp", "disp0", "disp0_piodma", + disp_reserved_regions_t8112, + ARRAY_SIZE(disp_reserved_regions_t8112)); + if (ret) + return ret; + + ret = dt_carveout_reserved_regions("dcpext", NULL, NULL, dcpext_reserved_regions_t8103, + ARRAY_SIZE(dcpext_reserved_regions_t8103)); + if (ret) + return ret; + + if (fdt_get_alias(dt, "dcpext") && dt_set_dcp_firmware("dcpext")) + return -1; } else if (!fdt_node_check_compatible(dt, 0, "apple,t8112") || !fdt_node_check_compatible(dt, 0, "apple,t8132")) { ret = dt_carveout_reserved_regions("dcp", "disp0", "disp0_piodma", @@ -2010,7 +2354,8 @@ static int dt_set_display(void) ARRAY_SIZE(dcpext_reserved_regions_t600x[n])); } } - } else if (!fdt_node_check_compatible(dt, 0, "apple,t6020") || + } else if (!fdt_node_check_compatible(dt, 0, "apple,t6041") || + !fdt_node_check_compatible(dt, 0, "apple,t6020") || !fdt_node_check_compatible(dt, 0, "apple,t6021")) { ret = dt_carveout_reserved_regions("dcp", "disp0", "disp0_piodma", disp_reserved_regions_t602x, @@ -2032,6 +2377,7 @@ static int dt_set_display(void) */ if (!fdt_node_check_compatible(dt, 0, "apple,t8112") || !fdt_node_check_compatible(dt, 0, "apple,t8132") || + !fdt_node_check_compatible(dt, 0, "apple,t6041") || !fdt_node_check_compatible(dt, 0, "apple,t6020") || !fdt_node_check_compatible(dt, 0, "apple,t6021") || !fdt_node_check_compatible(dt, 0, "apple,t6022")) @@ -2298,6 +2644,14 @@ static int dt_set_isp_fwdata(void) if (adt_node < 0) return 0; + u32 fuse0, fuse1; + if (ADT_GETPROP(adt, adt_node, "fuse_aciphy_0", &fuse0) >= 0 && + ADT_GETPROP(adt, adt_node, "fuse_aciphy_1", &fuse1) >= 0) { + fdt32_t fuses[] = {cpu_to_fdt32(fuse0), cpu_to_fdt32(fuse1)}; + if (fdt_setprop(dt, fdt_node, "apple,aciphy-fuses", fuses, sizeof(fuses))) + bail("FDT: failed to pass ISP ACIPHY fuses\n"); + } + uint32_t dev_phandle = fdt_get_phandle(dt, fdt_node); if (!dev_phandle) { ret = fdt_generate_phandle(dt, &dev_phandle); @@ -2320,61 +2674,25 @@ static int dt_set_isp_fwdata(void) static int dt_disable_missing_devs(const char *adt_prefix, const char *dt_prefix, int max_devs) { + int node; int ret = -1; - int adt_prefix_len = strlen(adt_prefix); int dt_prefix_len = strlen(dt_prefix); int acnt = 0, phcnt = 0; - u64 *addrs = calloc(max_devs, sizeof(u64)); + adt_dev_node_address *addrs = calloc(max_devs, sizeof(adt_dev_node_address)); u32 *phandles = calloc(max_devs * 5, sizeof(u32)); // Allow up to 5 extra nodes per device if (!addrs || !phandles) bail_cleanup("FDT: out of memory\n"); - int path[8]; - int node = adt_path_offset_trace(adt, "/arm-io", path); - if (node < 0) - bail_cleanup("ADT: /arm-io not found\n"); + acnt = adt_device_prefix_addrs(adt_prefix, max_devs, 0, addrs); + if (acnt < 0) + goto err; - int pp = 0; - while (path[pp]) - pp++; - path[pp + 1] = 0; - - u32 die_count; - if (ADT_GETPROP(adt, node, "die-count", &die_count) < 0) { - die_count = 1; - } - if (die_count > 8) { - printf("ADT: limiting die-count from %u to 8\n", die_count); - die_count = 8; - } - - /* Find ADT registers */ - ADT_FOREACH_CHILD(adt, node) - { - const char *name = adt_get_name(adt, node); - if (strncmp(name, adt_prefix, adt_prefix_len)) - continue; - - path[pp] = node; - if (adt_get_reg(adt, path, "reg", 0, &addrs[acnt++], NULL) < 0) - bail_cleanup("Error getting /arm-io/%s regs\n", name); - } + u32 die_count = adt_die_count(); for (u32 die = 0; die < die_count; ++die) { - char path[32] = "/soc"; - - if (die_count > 1) { - // pre-linux submission multi-die path - // can probably removed the next time someone read this comment. - snprintf(path, sizeof(path), "/soc/die%u", die); - int die_node = fdt_path_offset(dt, path); - if (die_node < 0) { - /* this should use aliases for the soc nodes */ - u64 die_unit_addr = die * PMGR_DIE_OFFSET + 0x200000000; - snprintf(path, sizeof(path), "/soc@%lx", die_unit_addr); - } - } + char path[32]; + dt_get_soc_path(path, sizeof(path), die, die_count); int soc = fdt_path_offset(dt, path); if (soc < 0) @@ -2399,7 +2717,7 @@ static int dt_disable_missing_devs(const char *adt_prefix, const char *dt_prefix int i; for (i = 0; i < acnt; i++) - if (addrs[i] == addr) + if (addrs[i].address == addr) break; if (i < acnt) continue; @@ -2521,7 +2839,7 @@ static int dt_disable_missing_devs(const char *adt_prefix, const char *dt_prefix return ret; } -__attribute__((unused)) static int dt_transfer_virtios(void) +__maybe_unused static int dt_transfer_virtios(void) { int path[3]; path[0] = adt_path_offset(adt, "/arm-io/"); @@ -2837,6 +3155,10 @@ int kboot_prepare_dt(void *fdt) return -1; if (dt_set_sep()) return -1; + if (dt_reserve_dart_mappings("aop")) + return -1; + if (dt_reserve_dart_mappings("aop1")) + return -1; if (dt_set_pmp()) return -1; if (dt_set_nvram()) @@ -2849,6 +3171,18 @@ int kboot_prepare_dt(void *fdt) return -1; if (dt_setup_sio()) return -1; + if (fdt_get_alias(dt, "ane") && + dt_reserve_asc_firmware("/arm-io/ane", "/arm-io/ane0", "ane", true, 0)) + return -1; + if (fdt_get_alias(dt, "ave") && + dt_reserve_asc_firmware("/arm-io/ave", NULL, "ave", true, 0)) + return -1; + if (fdt_get_alias(dt, "ave0") && + dt_reserve_asc_firmware("/arm-io/ave0", NULL, "ave0", true, 0)) + return -1; + if (fdt_get_alias(dt, "ave1") && + dt_reserve_asc_firmware("/arm-io/ave1", NULL, "ave1", true, 0)) + return -1; if (dt_reserve_asc_firmware("/arm-io/isp", "/arm-io/isp0", "isp", false, isp_iova_base())) return -1; if (dt_set_isp_fwdata()) @@ -2885,6 +3219,7 @@ int kboot_prepare_dt(void *fdt) if (dt_remain < SZ_16K) printf("FDT: free dt buffer space low, %u bytes left\n", dt_remain); + dc_civac_range(dt, fdt_totalsize(dt)); printf("FDT prepared at %p\n", dt); return 0; @@ -2899,6 +3234,7 @@ int kboot_boot(void *kernel) usb_init(); pcie_init(); dapf_init_all(); + gapf_disable_all(); printf("Setting SMP mode to WFE...\n"); smp_set_wfe_mode(true); diff --git a/src/kboot.h b/src/kboot.h index df12f3ad..61deacc9 100644 --- a/src/kboot.h +++ b/src/kboot.h @@ -26,4 +26,7 @@ int kboot_set_uboot(const char *name, const char *value); int kboot_prepare_dt(void *fdt); int kboot_boot(void *kernel); +// from rust/usb4.rs +u64 rust_usb4_router_uuid(void); + #endif diff --git a/src/kboot_atc.c b/src/kboot_atc.c index b27b8eb8..f010fa58 100644 --- a/src/kboot_atc.c +++ b/src/kboot_atc.c @@ -4,6 +4,7 @@ #include "adt.h" #include "atc.h" #include "devicetree.h" +#include "kboot_utils.h" #include "malloc.h" #include "pmgr.h" #include "utils.h" @@ -164,6 +165,7 @@ static const struct atc_fuse_info atc_fuses_t8112_port1[] = { static const struct atc_fuse_hw atc_fuses[] = { {"atc-phy,t8142", -1, NULL, 0}, {"atc-phy,t8132", -1, NULL, 0}, + {"atc-phy,t8130", -1, NULL, 0}, {"atc-phy,t8122", -1, NULL, 0}, {"atc-phy,t6020", -1, NULL, 0}, {"atc-phy,t8112", 0, atc_fuses_t8112_port0, ARRAY_SIZE(atc_fuses_t8112_port0)}, @@ -272,27 +274,13 @@ static int dt_append_atc_tunable(void *dt, int adt_node, int fdt_node, return 0; } -static void dt_copy_atc_tunables(void *dt, const char *adt_path, const char *dt_alias, int port) +static int dt_copy_atc_tunables(void *dt, int adt_node, int fdt_node, int port, + __always_unused void *data) { int ret; - const struct atc_tunable_info *tunables; - size_t tunable_count; - - int adt_node = adt_path_offset(adt, adt_path); - if (adt_node < 0) - return; - - const char *fdt_path = fdt_get_alias(dt, dt_alias); - if (fdt_path == NULL) { - printf("FDT: Unable to find alias %s\n", dt_alias); - return; - } - - int fdt_node = fdt_path_offset(dt, fdt_path); - if (fdt_node < 0) { - printf("FDT: Unable to find path %s for alias %s\n", fdt_path, dt_alias); - return; - } + const struct atc_tunable_info *tunables = NULL; + size_t tunable_count = 0; + const char *adt_path = adt_get_name(adt, adt_node); ret = dt_append_fuses(dt, adt_node, fdt_node, port); if (ret) { @@ -301,9 +289,12 @@ static void dt_copy_atc_tunables(void *dt, const char *adt_path, const char *dt_ goto cleanup; } - if (adt_is_compatible_at(adt, adt_node, "atc-phy,t8132", 0) || - adt_is_compatible_at(adt, adt_node, "atc-phy,t8122", 0) || - adt_is_compatible_at(adt, adt_node, "atc-phy,t8142", 0)) { + if (adt_is_compatible_at(adt, adt_node, "atc-phy,t8130", 0)) { + tunables = atc_tunables_t8130; + tunable_count = atc_tunables_t8130_count; + } else if (adt_is_compatible_at(adt, adt_node, "atc-phy,t8132", 0) || + adt_is_compatible_at(adt, adt_node, "atc-phy,t8122", 0) || + adt_is_compatible_at(adt, adt_node, "atc-phy,t8142", 0)) { tunables = &atc_tunables_t8122[0]; tunable_count = atc_tunables_t8122_count; } else { @@ -346,7 +337,7 @@ static void dt_copy_atc_tunables(void *dt, const char *adt_path, const char *dt_ goto cleanup; } - return; + return 0; cleanup: /* @@ -354,28 +345,77 @@ static void dt_copy_atc_tunables(void *dt, const char *adt_path, const char *dt_ * sure we don't leave half-filled properties around so that we can at least * try to boot with USB2 support only. */ - for (size_t i = 0; i < atc_tunables_t8103_count; ++i) - fdt_delprop(dt, fdt_node, atc_tunables_t8103[i].fdt_name); + for (size_t i = 0; i < tunable_count; ++i) + fdt_delprop(dt, fdt_node, tunables[i].fdt_name); fdt_delprop(dt, fdt_node, "apple,tunable-common-a"); fdt_delprop(dt, fdt_node, "apple,tunable-common"); printf("FDT: Unable to setup ATC tunables for %s - USB3/Thunderbolt will not work\n", adt_path); + return 0; } -int kboot_setup_atc(void *dt) -{ - char adt_path[32]; - char fdt_alias[32]; +/* CIO4 separates the fabric/common controls from the analog register bank. */ +static const struct atc_tunable_info atc_tunables_t6040[] = { + {"tunable_ATC_FABRIC", "apple,tunable-cio4-fabric", 0, 0x4000, true}, + {"tunable_ATC_COMMON_CFG", "apple,tunable-cio4-common", 0, 0x4000, true}, + {"tunable_LN0_RX_CFG_TX_OF_RXCLK", "apple,tunable-cio4-analog", 0xd000, 0x1000, true}, + {"tunable_LN1_RX_CFG_TX_OF_RXCLK", "apple,tunable-cio4-analog", 0x1d000, 0x1000, true}, + {"tunable_AUS40CMN_SHM", "apple,tunable-cio4-analog", 0xa00, 0x200, true}, + {"tunable_AUSPLL_CORE", "apple,tunable-cio4-analog", 0x3200, 0x200, true}, + {"tunable_CIO4PLL_CORE", "apple,tunable-cio4-analog", 0x3a00, 0x200, true}, +}; - for (int i = 0; i < MAX_ATC_DEVS; ++i) { - memset(adt_path, 0, sizeof(adt_path)); - snprintf(adt_path, sizeof(adt_path), "/arm-io/atc-phy%d", i); +static int dt_copy_cio4_tunables(void *dt, int adt_node, int fdt_node, __always_unused int port, + __always_unused void *data) +{ + const char *adt_path = adt_get_name(adt, adt_node); - memset(fdt_alias, 0, sizeof(adt_path)); - snprintf(fdt_alias, sizeof(fdt_alias), "atcphy%d", i); + if (!adt_is_compatible_at(adt, adt_node, "atc-phy,t6040", 0)) + return 0; - dt_copy_atc_tunables(dt, adt_path, fdt_alias, i); + for (size_t i = 0; i < ARRAY_SIZE(atc_tunables_t6040); i++) { + if (dt_append_atc_tunable(dt, adt_node, fdt_node, &atc_tunables_t6040[i])) { + printf("FDT: Unable to copy CIO4 calibration for %s\n", adt_path); + for (size_t j = 0; j < ARRAY_SIZE(atc_tunables_t6040); j++) + fdt_delprop(dt, fdt_node, atc_tunables_t6040[j].fdt_name); + fdt_setprop_string(dt, fdt_node, "status", "disabled"); + return 0; + } } return 0; } + +/* + * These register mapping is valid for: + * - M1 to M4 + * - M1 Pro/Max/Ultra + * - M2 Pro/Max/Ultra + * - M3 Pro/Max + */ +#define ADT_T8103_USB2PHY_REG_NUM 0 +#define FDT_T8103_USB2PHY_REG_NUM 3 + +#define ADT_T6040_ATCPHY_CORE_REG_NUM 2 +#define FDT_T6040_ATCPHY_CORE_REG_NUM 0 + +const char *atcphy_compatibles[] = { + "apple,t8103-atcphy", + "apple,t8122-atcphy", + "apple,t8130-atcphy", +}; + +const char *atcphy_t6040_compatibles[] = { + "apple,t6040-atcphy", +}; + +int kboot_setup_atc(void *dt) +{ + kboot_setup_matching_nodes(dt, MAX_ATC_DEVS, "atc-phy", ADT_T8103_USB2PHY_REG_NUM, + atcphy_compatibles, ARRAY_SIZE(atcphy_compatibles), + FDT_T8103_USB2PHY_REG_NUM, dt_copy_atc_tunables, NULL); + + return kboot_setup_matching_nodes(dt, MAX_ATC_DEVS, "atc-phy", ADT_T6040_ATCPHY_CORE_REG_NUM, + atcphy_t6040_compatibles, ARRAY_SIZE(atcphy_t6040_compatibles), + FDT_T6040_ATCPHY_CORE_REG_NUM, dt_copy_cio4_tunables, NULL); +} diff --git a/src/kboot_gpu.c b/src/kboot_gpu.c index 4f130f5c..98c1ece0 100644 --- a/src/kboot_gpu.c +++ b/src/kboot_gpu.c @@ -490,257 +490,343 @@ static int fdt_set_aux_opp(void *dt, int gpu, const char *prop, const struct aux return 0; } -static int dt_set_gpu_t8132_adt(void *dt, int gpu, int sgx) +static const struct fw_version_info *gpu_fw_compat(const struct fw_version_info *fw) { - ADT_FOREACH_PROPERTY(adt, sgx, prop) - { - char name[64]; - size_t len = strnlen(prop->name, sizeof(prop->name)); - if (len == sizeof(prop->name)) - bail("ADT: GPU: unterminated property name\n"); - snprintf(name, sizeof(name), "apple,sgx-%s", prop->name); - if (fdt_setprop(dt, gpu, name, prop->value, prop->size & 0x7fffffff)) - bail("FDT: GPU: failed to preserve %s\n", prop->name); + switch (fw->version) { + case V12_3_1: + return &fw_versions[V12_3]; + case V13_5B4: + case V13_6_1: + return &fw_versions[V13_5]; + default: + return fw; } +} - int arm_io = adt_path_offset(adt, "/arm-io"); - u32 revision; - if (arm_io < 0 || ADT_GETPROP(adt, arm_io, "chip-revision", &revision) < 0) - bail("ADT: GPU: missing chip revision\n"); - if (fdt_setprop_u32(dt, gpu, "apple,chip-revision", revision) || - fdt_setprop_string(dt, gpu, "apple,firmware-build", os_firmware.iboot)) - return -1; +static int opp_table_check(const void *dt, int gpu, const char *prop, u32 count, u32 *phandle_out, + bool *existing_out) +{ + int len; + const fdt32_t *ph = fdt_getprop(dt, gpu, prop, &len); + if (!ph || len != sizeof(*ph)) + return -FDT_ERR_BADVALUE; + + u32 phandle = fdt32_to_cpu(*ph); + int table = fdt_node_offset_by_phandle(dt, phandle); + if (table < 0) + return table; + + u32 found = 0; + int child; + fdt_for_each_subnode(child, dt, table) + found++; + if (found != 0 && found != count) + return -FDT_ERR_BADVALUE; + + *phandle_out = phandle; + *existing_out = (found == count); return 0; } -/* Per-board G16 inputs the ADT doesn't carry: per-step relative maximum power - * curves (percent, one entry per voltage group including off), the GPU power - * limit and the core mask. Boards without an entry get no GPU. */ -struct t8132_gpu_board { - const char *target; - u32 groups; - u32 pwr_calib_a[MAX_PSTATES]; - u32 pwr_calib_b[MAX_PSTATES]; - u32 max_power_mw; - u32 core_mask; - /* Read the enabled cores from the GPU: binned parts differ per unit. Only for boards - * where that read is validated; on the mini (J773g) it data-aborts. */ - bool read_core_mask; -}; - -static const struct t8132_gpu_board t8132_gpu_boards[] = { - { - .target = "J773g", /* Mac mini (M4) */ - .groups = 11, - .pwr_calib_a = {0, 8, 14, 21, 27, 36, 44, 54, 60, 79, 100}, - .pwr_calib_b = {0, 0, 0, 0, 16, 33, 47, 61, 69, 86, 100}, - .max_power_mw = 37237, - .core_mask = 0x3ff, - .read_core_mask = false, - }, +static int nth_subnode(const void *dt, int parent, u32 index) +{ + u32 i = 0; + int child; + fdt_for_each_subnode(child, dt, parent) { - /* MacBook Air 13" (M4). Observed on this board, like the mini's - * row: the curves and the limit are the values macOS gives the GPU - * firmware in its init data, read from GPU memory with macOS running - * as a guest. The frequency and voltage ladder in that init data - * matches this board's ADT. The core mask is read from the GPU at - * boot; the value here is only the fallback (cores 1 and 6 fused off - * on the first unit, which is also the mask macOS reports there). */ - .target = "J713", - .groups = 10, - .pwr_calib_a = {0, 12, 22, 31, 42, 55, 69, 82, 92, 100}, - .pwr_calib_b = {0, 0, 24, 40, 52, 63, 73, 83, 88, 100}, - .max_power_mw = 26250, - .core_mask = 0x3bd, - .read_core_mask = true, - }, -}; + if (i == index) + return child; + i++; + } + return -FDT_ERR_NOTFOUND; +} -/* Enabled GPU cores, from the GPU itself (as upstream reads G14X's masks at - * the same offset). Binned parts differ per unit, so the board value is only - * a fallback. */ -static u32 t8132_gpu_core_mask(const struct t8132_gpu_board *board) +static int populate_gpu_memory(void *dt, int gpu, int sgx) { - int path[8]; - u64 base, size; + static const struct { + const char *adt_name; + const char *dt_name; + } region_specs[] = { + {"gpu-region", "ttbs"}, + {"gfx-shared-region", "pagetables"}, + {"gfx-shared-l2-region", "l2"}, + {"gfx-handoff", "handoff"}, + {"gfx-data", "firmware"}, + {"gfx1-data", "firmware-secondary"}, + }; - if (!board->read_core_mask) - return board->core_mask; - if (adt_path_offset_trace(adt, "/arm-io/sgx", path) < 0 || - adt_get_reg(adt, path, "reg", 0, &base, &size) < 0) - return board->core_mask; - if (size < 0xe01500 + 4) { - printf("FDT: GPU: core mask register outside sgx reg (size %#lx); using %#x\n", size, - board->core_mask); - return board->core_mask; + size_t region_count = 5; + if (fdt_stringlist_search(dt, gpu, "memory-region-names", "firmware-secondary") >= 0) + region_count = 6; + + int name_count = fdt_stringlist_count(dt, gpu, "memory-region-names"); + if (name_count < 0 || (size_t)name_count != region_count) + bail("FDT: GPU: unexpected memory-region-names count %d (expected %zu)\n", name_count, + region_count); + + int len; + const fdt32_t *handles = fdt_getprop(dt, gpu, "memory-region", &len); + if (!handles || (size_t)len != region_count * sizeof(*handles)) + bail("FDT: GPU: invalid memory-region phandle list\n"); + + int reserved = fdt_path_offset(dt, "/reserved-memory"); + if (reserved < 0 || fdt_address_cells(dt, reserved) != 2 || fdt_size_cells(dt, reserved) != 2) + bail("FDT: GPU: invalid /reserved-memory node\n"); + + u64 bases[ARRAY_SIZE(region_specs)]; + u64 sizes[ARRAY_SIZE(region_specs)]; + u32 phandles[ARRAY_SIZE(region_specs)]; + + for (size_t i = 0; i < region_count; i++) { + char prop[64]; + snprintf(prop, sizeof(prop), "%s-base", region_specs[i].adt_name); + if (ADT_GETPROP(adt, sgx, prop, &bases[i]) < 0) + bail("ADT: GPU: missing %s\n", prop); + snprintf(prop, sizeof(prop), "%s-size", region_specs[i].adt_name); + if (ADT_GETPROP(adt, sgx, prop, &sizes[i]) < 0) + bail("ADT: GPU: missing %s\n", prop); + + u64 base = bases[i], size = sizes[i]; + if (!base || !size || ((base | size) & 0x3fff) || base >= (1ULL << 42) || + size > (1ULL << 42) - base) + bail("ADT: GPU: invalid region %s (%#lx+%#lx)\n", region_specs[i].adt_name, base, size); + + int idx = fdt_stringlist_search(dt, gpu, "memory-region-names", region_specs[i].dt_name); + if (idx < 0 || (size_t)idx >= region_count) + bail("FDT: GPU: missing memory-region-names entry '%s'\n", region_specs[i].dt_name); + + phandles[i] = fdt32_to_cpu(handles[idx]); + for (size_t j = 0; j < i; j++) { + if (phandles[i] == phandles[j] || + (base < bases[j] + sizes[j] && bases[j] < base + size)) + bail("FDT: GPU: duplicate or overlapping reserved region '%s'\n", + region_specs[i].dt_name); + } + + int node = fdt_node_offset_by_phandle(dt, phandles[i]); + if (node < 0 || fdt_parent_offset(dt, node) != reserved) + bail("FDT: GPU: invalid reserved-memory target for '%s'\n", region_specs[i].dt_name); + + const void *nomap = fdt_getprop(dt, node, "no-map", &len); + if (nomap && len != 0) + bail("FDT: GPU: malformed no-map on '%s'\n", region_specs[i].dt_name); } - /* Power sgx and its parent domains before touching its MMIO. Enabling is - * recursive and a no-op for domains already on; sgx alone reading active is - * not enough (J713 aborted here when power-up was skipped for an sgx that - * read active; the earlier unconditional enable read fine). - * The domains stay on, as the earlier Air loader left them. */ - int was_active = pmgr_adt_power_is_active("/arm-io/sgx"); - pmgr_adt_power_enable("/arm-io/sgx"); - if (pmgr_adt_power_is_active("/arm-io/sgx") != 1) { - printf("FDT: GPU: can't power the GPU to read its core mask; using %#x\n", board->core_mask); - return board->core_mask; + + for (size_t i = 0; i < region_count; i++) { + int node = fdt_node_offset_by_phandle(dt, phandles[i]); + if (node < 0) + return -1; + + fdt64_t reg[2] = {cpu_to_fdt64(bases[i]), cpu_to_fdt64(sizes[i])}; + if (fdt_setprop(dt, node, "reg", reg, sizeof(reg))) + bail("FDT: GPU: failed to set reg for '%s'\n", region_specs[i].dt_name); + if (!fdt_getprop(dt, node, "no-map", NULL) && fdt_setprop_empty(dt, node, "no-map")) + bail("FDT: GPU: failed to set no-map for '%s'\n", region_specs[i].dt_name); + if (fdt_setprop_string(dt, node, "status", "okay")) + bail("FDT: GPU: failed to enable '%s'\n", region_specs[i].dt_name); } - u32 mask = read32(base + 0xe01500) & 0x3ff; - /* G16 isn't G14X: only trust a value that looks like a fused subset of T8132's - * ten cores. The board value is one unit's fusing, not a superset. */ - u32 cores = 0; - for (u32 m = mask; m; m &= m - 1) - cores++; - bool sane = !(mask & ~0x3ffu) && cores >= 8; - printf("FDT: GPU: core mask %#x from the GPU (board default %#x, sgx was %s)%s\n", mask, - board->core_mask, was_active == 1 ? "on" : was_active == 0 ? "off" : "unknown", - sane ? "" : "; implausible, using the board default"); - return sane ? mask : board->core_mask; -} -static const struct t8132_gpu_board *t8132_gpu_board(void) -{ - const char *target = adt_getprop(adt, 0, "target-type", NULL); - if (!target) - return NULL; - for (size_t i = 0; i < ARRAY_SIZE(t8132_gpu_boards); i++) - if (!strcmp(target, t8132_gpu_boards[i].target)) - return &t8132_gpu_boards[i]; - return NULL; + return 0; } -static int dt_set_gpu_t8132_perf(void *dt, int gpu, int sgx, - const struct t8132_gpu_board *board) +static int populate_gpu_opps(void *dt, int gpu, int sgx, u32 *clusters_out, u32 *states_out) { - u32 count, tables, core_len, sram_len; - if (ADT_GETPROP(adt, sgx, "perf-state-count", &count) < 0 || - ADT_GETPROP(adt, sgx, "perf-state-table-count", &tables) < 0 || - tables != 1 || count == 0 || count > MAX_PSTATES) - bail("ADT: GPU: unsupported M4 performance table geometry\n"); + u32 raw_states = 0, clusters = 0; + if (ADT_GETPROP(adt, sgx, "perf-state-count", &raw_states) < 0 || raw_states < 2 || + raw_states > MAX_PSTATES || + ADT_GETPROP(adt, sgx, "perf-state-table-count", &clusters) < 0 || clusters < 1 || + clusters > MAX_CLUSTERS) + bail("ADT: GPU: invalid perf state geometry\n"); + + u32 core_len = 0, sram_len = 0; const struct perf_state *core = adt_getprop(adt, sgx, "perf-states", &core_len); const struct perf_state *sram = adt_getprop(adt, sgx, "perf-states-sram", &sram_len); - if (!core || !sram || core_len != count * sizeof(*core) || sram_len != core_len) - bail("ADT: GPU: incomplete M4 performance tables\n"); + size_t expected_len = (size_t)raw_states * clusters * sizeof(*core); + if (!core || !sram || core_len != expected_len || sram_len != expected_len) + bail("ADT: GPU: incomplete perf-states tables\n"); - fdt32_t freq_a[MAX_PSTATES], freq_b[MAX_PSTATES]; - fdt32_t index_a[MAX_PSTATES], index_b[MAX_PSTATES]; - fdt32_t core_voltage[MAX_PSTATES], memory_voltage[MAX_PSTATES]; + u32 firsts[MAX_PSTATES], high[MAX_PSTATES], low[MAX_PSTATES]; u32 groups = 0; - for (u32 first = 0; first < count;) { - u32 last = first, low = first, high = first; - while (last + 1 < count && core[last + 1].volt == core[first].volt) - last++; - for (u32 row = first; row <= last; row++) { - if (core[row].freq != sram[row].freq || - sram[row].volt != sram[first].volt || core[row].freq % 1000000) - bail("ADT: GPU: inconsistent M4 performance row %u\n", row); - if (core[row].freq < core[low].freq) - low = row; - if (core[row].freq > core[high].freq) - high = row; + for (u32 first = 0; first < raw_states;) { + u32 end = first + 1, hi = first, lo = first; + while (end < raw_states && core[end].volt == core[first].volt) + end++; + for (u32 i = first; i < end; i++) { + if (core[i].freq != sram[i].freq || sram[i].volt != sram[first].volt) + bail("ADT: GPU: inconsistent core/SRAM perf row %u\n", i); + if (core[i].freq > core[hi].freq) + hi = i; + if (core[i].freq < core[lo].freq) + lo = i; } - if (first && core[first].volt <= core[first - 1].volt) - bail("ADT: GPU: unordered M4 voltage steps\n"); - freq_a[groups] = cpu_to_fdt32(core[high].freq / 1000000); - freq_b[groups] = cpu_to_fdt32(core[low].freq / 1000000); - index_a[groups] = cpu_to_fdt32(high); - index_b[groups] = cpu_to_fdt32(low); - core_voltage[groups] = cpu_to_fdt32(core[first].volt); - memory_voltage[groups] = cpu_to_fdt32(sram[first].volt); + if ((!groups && (core[hi].freq || core[lo].freq)) || + (groups && (core[hi].freq / 1000000 <= core[high[groups - 1]].freq / 1000000 || + core[lo].freq / 1000000 < core[low[groups - 1]].freq / 1000000))) + bail("ADT: GPU: non-monotonic perf ladder at group %u\n", groups); + firsts[groups] = first; + high[groups] = hi; + low[groups] = lo; groups++; - first = last + 1; + first = end; } - /* Group 0 is the off state; the calibration curves are per group. */ - if (groups < 2 || core[0].freq != 0) - bail("ADT: GPU: M4 performance table has no off state\n"); - if (groups != board->groups) - bail("ADT: GPU: %s has %u voltage steps, its calibration has %u\n", board->target, - groups, board->groups); - - fdt32_t rel_a[MAX_PSTATES], rel_b[MAX_PSTATES]; + if (groups < 2) + bail("ADT: GPU: too few voltage groups (%u)\n", groups); + for (u32 i = 0; i < groups; i++) { - rel_a[i] = cpu_to_fdt32(board->pwr_calib_a[i]); - rel_b[i] = cpu_to_fdt32(board->pwr_calib_b[i]); + for (u32 c = 0; c < clusters; c++) { + u32 row = firsts[i] + c * raw_states; + if (core[row].volt > UINT32_MAX / 1000 || sram[row].volt > UINT32_MAX / 1000) + bail("ADT: GPU: voltage overflow at group %u cluster %u\n", i, c); + } } - size_t size = groups * sizeof(fdt32_t); - if (fdt_setprop(dt, gpu, "apple,m4-freq-a", freq_a, size) || - fdt_setprop(dt, gpu, "apple,m4-freq-b", freq_b, size) || - fdt_setprop(dt, gpu, "apple,m4-index-a", index_a, size) || - fdt_setprop(dt, gpu, "apple,m4-index-b", index_b, size) || - fdt_setprop(dt, gpu, "apple,m4-core-voltage", core_voltage, size) || - fdt_setprop(dt, gpu, "apple,m4-memory-voltage", memory_voltage, size) || - fdt_setprop(dt, gpu, "apple,m4-relative-a", rel_a, size) || - fdt_setprop(dt, gpu, "apple,m4-relative-b", rel_b, size) || - fdt_setprop_u32(dt, gpu, "apple,m4-max-power-mw", board->max_power_mw) || - fdt_setprop_u32(dt, gpu, "apple,m4-core-mask", t8132_gpu_core_mask(board))) - return -1; + + u32 core_opp_ph = 0; + bool core_opp_existing = false; + if (opp_table_check(dt, gpu, "operating-points-v2", groups, &core_opp_ph, &core_opp_existing)) + bail("FDT: GPU: invalid operating-points-v2 table (expected 0 or %u entries)\n", groups); + + u32 cs_len = 0; + const struct aux_perf_states *cs = adt_getprop(adt, sgx, "cs-perf-states", &cs_len); + bool populate_cs = cs && fdt_getprop(dt, gpu, "apple,cs-opp", NULL); + u32 cs_opp_ph = 0, cs_min_uv = 0; + bool cs_opp_existing = false; + if (populate_cs) { + if (cs_len < 24 || cs->dies != 1 || !cs->count || cs->count > MAX_PSTATES || + cs_len != 24 + cs->count * sizeof(struct aux_perf_state)) + bail("ADT: GPU: invalid cs-perf-states table\n"); + u64 minimum; + memcpy(&minimum, &cs->states[cs->count], sizeof(minimum)); + if (minimum > UINT32_MAX) + bail("ADT: GPU: invalid cs-perf-states min SRAM voltage\n"); + cs_min_uv = (u32)minimum; + for (u32 i = 0; i < cs->count; i++) { + if (cs->states[i].volt > UINT32_MAX) + bail("ADT: GPU: CS OPP voltage overflow at %u\n", i); + } + if (opp_table_check(dt, gpu, "apple,cs-opp", cs->count, &cs_opp_ph, &cs_opp_existing)) + bail("FDT: GPU: invalid apple,cs-opp table (expected 0 or %lu entries)\n", cs->count); + } + + /* Write per-SKU scalars on gpu before inserting OPP subnodes. */ + u32 mcc_mask = 0; + if (ADT_GETPROP(adt, sgx, "active-mcc-bit-vector", &mcc_mask) >= 0 && mcc_mask && + fdt_setprop_u32(dt, gpu, "apple,active-mcc-mask", mcc_mask)) + bail("FDT: GPU: failed to set apple,active-mcc-mask\n"); + if (populate_cs && fdt_setprop_u32(dt, gpu, "apple,csafr-min-sram-microvolt", cs_min_uv)) + bail("FDT: GPU: failed to set apple,csafr-min-sram-microvolt\n"); + + for (u32 step = 0; step < groups; step++) { + u32 i = core_opp_existing ? step : (groups - 1 - step); + int table = fdt_node_offset_by_phandle(dt, core_opp_ph); + if (table < 0) + return -1; + + int child; + if (core_opp_existing) { + child = nth_subnode(dt, table, i); + } else { + char name[8]; + snprintf(name, sizeof(name), "opp%02u", i); + child = fdt_add_subnode(dt, table, name); + } + if (child < 0) + bail("FDT: GPU: failed to locate/create core OPP %u\n", i); + + fdt32_t core_uv[MAX_CLUSTERS], sram_uv[MAX_CLUSTERS]; + for (u32 c = 0; c < clusters; c++) { + u32 row = firsts[i] + c * raw_states; + core_uv[c] = cpu_to_fdt32(core[row].volt * 1000); + sram_uv[c] = cpu_to_fdt32(sram[row].volt * 1000); + } + fdt32_t idx[2] = {cpu_to_fdt32(high[i]), cpu_to_fdt32(low[i])}; + + if (fdt_setprop_u64(dt, child, "opp-hz", core[high[i]].freq) || + fdt_setprop_u64(dt, child, "apple,alternate-opp-hz", core[low[i]].freq) || + fdt_setprop(dt, child, "opp-microvolt", core_uv, clusters * sizeof(*core_uv)) || + fdt_setprop(dt, child, "apple,sram-microvolt", sram_uv, clusters * sizeof(*sram_uv)) || + fdt_setprop(dt, child, "apple,perf-index", idx, sizeof(idx))) + bail("FDT: GPU: failed to populate core OPP %u\n", i); + } + + if (populate_cs) { + for (u32 step = 0; step < cs->count; step++) { + u32 i = cs_opp_existing ? step : (cs->count - 1 - step); + int table = fdt_node_offset_by_phandle(dt, cs_opp_ph); + if (table < 0) + return -1; + + int child; + if (cs_opp_existing) { + child = nth_subnode(dt, table, i); + } else { + char name[8]; + snprintf(name, sizeof(name), "opp%02u", i); + child = fdt_add_subnode(dt, table, name); + } + if (child < 0) + bail("FDT: GPU: failed to locate/create CS OPP %u\n", i); + + if (fdt_setprop_u64(dt, child, "opp-hz", cs->states[i].freq) || + fdt_setprop_u32(dt, child, "opp-microvolt", (u32)cs->states[i].volt)) + bail("FDT: GPU: failed to populate CS OPP %u\n", i); + } + } + + *clusters_out = clusters; + *states_out = groups; return 0; } -/* G16 owns a different initialization graph from G13/G14. Pass the boot - * firmware's memory contract through without running the older calibration - * serializer. Linux constructs the G16 graph in its own allocations. */ -static int dt_set_gpu_t8132(void *dt) +/* Generic GPU DT handoff for modern Apple Silicon GPUs (G16+ / resident + * firmware carveout model). Forwards only boot-time and per-unit fused ADT + * state: firmware carveout reservations, fused core/CS OPP tables, optional + * per-SKU MCC mask, and runtime firmware version metadata. Static SoC + * constants live in Linux. */ +static int dt_set_gpu_generic(void *dt) { int gpu = fdt_path_offset(dt, "gpu"); if (gpu < 0) return 0; - const struct t8132_gpu_board *board = t8132_gpu_board(); - if (!board) { - printf("FDT: GPU: no power calibration for %s; disabling the GPU\n", - (const char *)adt_getprop(adt, 0, "target-type", NULL) ?: "this board"); - return fdt_setprop_string(dt, gpu, "status", "disabled") ? -1 : 0; - } - int sgx = adt_path_offset(adt, "/arm-io/sgx"); if (sgx < 0) - bail("ADT: GPU: missing sgx node\n"); + bail("ADT: GPU: missing /arm-io/sgx node\n"); - static const struct { - const char *adt_name; - const char *dt_path; - } regions[] = { - {"gpu-region", "/reserved-memory/uat-ttbs"}, - {"gfx-shared-region", "/reserved-memory/uat-pagetables"}, - {"gfx-shared-l2-region", "/reserved-memory/uat-l2"}, - {"gfx-handoff", "/reserved-memory/uat-handoff"}, - {"gfx-data", "/reserved-memory/gpu-firmware"}, - }; - for (size_t i = 0; i < ARRAY_SIZE(regions); i++) { - if (dt_set_region(dt, sgx, regions[i].adt_name, regions[i].dt_path)) - return -1; + if (!adt_getprop(adt, sgx, "gfx-data-base", NULL)) { + printf("ADT: GPU: unsupported chip T%x\n", chip_id); + return 0; } - /* Adding properties can move the node. Resolve it after all reservations. */ + if (populate_gpu_memory(dt, gpu, sgx)) + return -1; + gpu = fdt_path_offset(dt, "gpu"); - u64 private_base, private_size; - if (ADT_GETPROP(adt, sgx, "rtkit-private-vm-region-base", &private_base) < 0 || - ADT_GETPROP(adt, sgx, "rtkit-private-vm-region-size", &private_size) < 0 || - !private_size) - bail("ADT: GPU: missing private VM region\n"); - - fdt64_t private_vm[2] = {cpu_to_fdt64(private_base), cpu_to_fdt64(private_size)}; - if (fdt_setprop(dt, gpu, "apple,rtkit-private-vm-region", private_vm, sizeof(private_vm))) + if (gpu < 0) + bail("FDT: GPU: lost gpu node after memory reservations\n"); + + u32 clusters = 0, groups = 0; + if (populate_gpu_opps(dt, gpu, sgx, &clusters, &groups)) return -1; + + gpu = fdt_path_offset(dt, "gpu"); + if (gpu < 0) + bail("FDT: GPU: lost gpu node after OPP population\n"); + + const struct fw_version_info *compat = gpu_fw_compat(&os_firmware); if (firmware_set_fdt(dt, gpu, "apple,firmware-version", &os_firmware) || - firmware_set_fdt(dt, gpu, "apple,firmware-compat", &os_firmware) || - firmware_set_fdt(dt, gpu, "apple,firmware-abi", &os_firmware)) + firmware_set_fdt(dt, gpu, "apple,firmware-compat", compat)) return -1; - if (dt_set_gpu_t8132_adt(dt, gpu, sgx) || dt_set_gpu_t8132_perf(dt, gpu, sgx, board)) + if (os_firmware.iboot && fdt_setprop_string(dt, gpu, "apple,firmware-build", os_firmware.iboot)) return -1; if (fdt_setprop_string(dt, gpu, "status", "okay")) - return -1; + bail("FDT: GPU: failed to enable gpu node\n"); - printf("FDT: GPU: T8132 firmware memory handoff ready\n"); + printf("FDT: GPU: T%x DT ready (%u clusters, %u OPP groups)\n", chip_id, clusters, groups); return 0; } int dt_set_gpu(void *dt) { - if (chip_id == T8132) - return dt_set_gpu_t8132(dt); - bool has_cs_afr = false; int (*calc_power)(u32 count, u32 table_count, const struct perf_state *core, const struct perf_state *sram, const struct aux_perf_states *cs, u32 *max_pwr, @@ -748,8 +834,6 @@ int dt_set_gpu(void *dt) u32 dies = 1; - printf("FDT: GPU: Initializing GPU info\n"); - switch (chip_id) { case T8103: calc_power = calc_power_t8103; @@ -771,10 +855,11 @@ int dt_set_gpu(void *dt) calc_power = calc_power_t600x; break; default: - printf("ADT: GPU: unsupported chip!\n"); - return 0; + return dt_set_gpu_generic(dt); } + printf("FDT: GPU: Initializing GPU info\n"); + int gpu = fdt_path_offset(dt, "gpu"); if (gpu < 0) { printf("FDT: GPU: gpu alias not found in device tree\n"); @@ -949,20 +1034,7 @@ int dt_set_gpu(void *dt) return 0; } - const struct fw_version_info *compat; - - switch (os_firmware.version) { - case V12_3_1: - compat = &fw_versions[V12_3]; - break; - case V13_5B4: - case V13_6_1: - compat = &fw_versions[V13_5]; - break; - default: - compat = &os_firmware; - break; - } + const struct fw_version_info *compat = gpu_fw_compat(&os_firmware); if (downstream_dtb) { if (firmware_set_fdt(dt, gpu, "apple,firmware-version", &os_firmware)) diff --git a/src/kboot_utils.c b/src/kboot_utils.c new file mode 100644 index 00000000..ebbe38be --- /dev/null +++ b/src/kboot_utils.c @@ -0,0 +1,130 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include + +#include "kboot_utils.h" +#include "adt.h" +#include "devicetree.h" +#include "pmgr.h" +#include "utils.h" + +#include "libfdt/libfdt.h" + +#define bail(...) \ + do { \ + printf(__VA_ARGS__); \ + return -1; \ + } while (0) + +int adt_device_prefix_addrs(const char *adt_prefix, int max_devs, int regnum, + adt_dev_node_address devs[ADT_MAX_PREFIX_DEVS]) +{ + int adt_prefix_len = strlen(adt_prefix); + int count = 0; + int path[8]; + + if (max_devs > ADT_MAX_PREFIX_DEVS) + bail("ADT: max_devs exceeds ADT_MAX_PREFIX_DEVS (%d)\n", ADT_MAX_PREFIX_DEVS); + + int node = adt_path_offset_trace(adt, "/arm-io", path); + if (node < 0) + bail("ADT: /arm-io not found\n"); + + int pp = 0; + while (path[pp]) + pp++; + path[pp + 1] = 0; + + /* Find ADT registers */ + ADT_FOREACH_CHILD(adt, node) + { + const char *name = adt_get_name(adt, node); + if (strncmp(name, adt_prefix, adt_prefix_len)) + continue; + + char suffix = name[adt_prefix_len]; + if (suffix != '\0' && (suffix < '0' || suffix > '9')) + continue; + + path[pp] = node; + devs[count].node = node; + devs[count].port = suffix ? suffix - '0' : 0; + if (adt_get_reg(adt, path, "reg", regnum, &devs[count++].address, NULL) < 0) + bail("Error getting /arm-io/%s regs\n", name); + } + + return count; +} + +int adt_die_count(void) +{ + int node = adt_path_offset(adt, "/arm-io"); + int die_count; + + if (ADT_GETPROP(adt, node, "die-count", &die_count) < 0) { + die_count = 1; + } + if (die_count > 8) { + printf("ADT: limiting die-count from %u to 8\n", die_count); + die_count = 8; + } + + return die_count; +} + +int dt_get_soc_path(char *path, size_t size, int die, int die_count) +{ + if (die_count > 1) { + u64 die_unit_addr = die * PMGR_DIE_OFFSET + 0x200000000; + return snprintf(path, size, "/soc@%lx", die_unit_addr); + } + + return snprintf(path, size, "/soc"); +} + +int kboot_setup_matching_nodes(void *dt, int max_devices, const char *adt_prefix, int adt_reg_idx, + const char **compatibles, size_t num_compatibles, int fdt_reg_idx, + kboot_match_t *match_action, void *action_data) +{ + adt_dev_node_address adt_devices[ADT_MAX_PREFIX_DEVS] = {0}; + int node = 0; + + int devs = adt_device_prefix_addrs(adt_prefix, max_devices, adt_reg_idx, adt_devices); + if (devs < 0) { + printf("ADT: Failed to collect %s.reg[%d]\n", adt_prefix, adt_reg_idx); + return 0; + } + + int die_count = adt_die_count(); + for (int die = 0; die < die_count; die++) { + char path[32]; + dt_get_soc_path(path, sizeof(path), die, die_count); + + int soc = fdt_path_offset(dt, path); + if (soc < 0) { + printf("FDT: %s node not found in devtree\n", path); + continue; + } + + // parse ranges for address translation + struct dt_ranges_tbl ranges[DT_MAX_RANGES] = {0}; + dt_parse_ranges(dt, soc, ranges); + + /* Find compatible devices and their ADT match */ + fdt_for_each_subnode(node, dt, soc) + { + if (!dt_node_is_compatible(dt, node, compatibles, num_compatibles)) + continue; + u64 addr = dt_get_address_translated(dt, node, fdt_reg_idx, ranges); + + for (int i = 0; i < devs; ++i) { + if ((adt_devices[i].address) == addr) { + match_action(dt, adt_devices[i].node, node, adt_devices[i].port, action_data); + break; + } + } + } + } + + return 0; +} diff --git a/src/kboot_utils.h b/src/kboot_utils.h new file mode 100644 index 00000000..3849f646 --- /dev/null +++ b/src/kboot_utils.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef KBOOT_UTILS_H +#define KBOOT_UTILS_H + +#include "types.h" + +#define ADT_MAX_PREFIX_DEVS 8 + +typedef struct adt_dev_node_address { + u64 address; + int node; + u8 port; +} adt_dev_node_address; + +int adt_device_prefix_addrs(const char *adt_prefix, int max_devs, int regnum, + adt_dev_node_address devs[ADT_MAX_PREFIX_DEVS]); +int adt_die_count(void); + +int dt_get_soc_path(char *path, size_t size, int die, int die_count); + +typedef int(kboot_match_t)(void *dt, int adt_node, int node_offset, int index, void *data); + +int kboot_setup_matching_nodes(void *dt, int max_devices, const char *adt_prefix, int adt_reg_idx, + const char **compatibles, size_t num_compatibles, int fdt_reg_idx, + kboot_match_t *match_action, void *action_data); + +#endif /* KBOOT_UTILS_H */ diff --git a/src/main.c b/src/main.c index 04e71411..577a3834 100644 --- a/src/main.c +++ b/src/main.c @@ -207,6 +207,7 @@ void m1n1_main(void) #ifdef USE_FB fb_shutdown(next_stage.restore_logo); #endif + dc_civac_range(&next_stage, sizeof(next_stage)); mmu_shutdown(); #endif diff --git a/src/memory.c b/src/memory.c index fe780faa..e8f134ee 100644 --- a/src/memory.c +++ b/src/memory.c @@ -19,12 +19,14 @@ #define CACHE_RANGE_OP(func, op) \ void func(void *addr, size_t length) \ { \ + if (!length) \ + return; \ if (func == dc_civac_range && cpu_features->workaround_cyclone_cache) { \ reg_clr(SYS_IMP_APL_HID4, HID4_DISABLE_DC_MVA); \ sysop("isb"); \ } \ - u64 p = (u64)addr; \ - u64 end = p + length; \ + u64 p = ALIGN_DOWN((u64)addr, CACHE_LINE_SIZE); \ + u64 end = (u64)addr + length; \ while (p < end) { \ cacheop(op, p); \ p += CACHE_LINE_SIZE; \ diff --git a/src/payload.c b/src/payload.c index 954be568..7747fdcb 100644 --- a/src/payload.c +++ b/src/payload.c @@ -12,6 +12,7 @@ #include "heapblock.h" #include "kboot.h" #include "mitigations.h" +#include "memory.h" #include "smp.h" #include "utils.h" @@ -61,6 +62,10 @@ static void finalize_uncompression(void *dest, size_t dest_len) // Explicitly *un*aligned or it'll fail this assert, since 64b alignment is the default assert(end == heapblock_alloc_aligned((u8 *)next - (u8 *)end, 1)); } + + /* Publish newly decompressed instructions before the uncached handoff. */ + dc_civac_range(dest, dest_len); + ic_ivau_range(dest, dest_len); } static void *decompress_gz(void *p, size_t size) diff --git a/src/pcie.c b/src/pcie.c index a5b3bcab..5bfcfbf3 100644 --- a/src/pcie.c +++ b/src/pcie.c @@ -141,7 +141,8 @@ enum apcie_type { APCIE_T8122 = 2, APCIE_T6031 = 3, APCIE_T8132 = 4, - APCIE_T8142 = 5, + APCIE_T8140 = 5, + APCIE_T8142 = 6, }; struct reg_info { @@ -216,6 +217,18 @@ static const struct reg_info regs_t8132 = { .fuse_idx = 5, }; +static const struct reg_info regs_t8140 = { + .type = APCIE_T8140, + .compat = APCIE_T8140, + .shared_reg_count = 7, + .config_idx = 0, + .rc_idx = 1, + .phy_common_idx = 2, + .phy_idx = 2, + .phy_ip_idx = 3, + .axi_idx = 4, +}; + static const struct reg_info regs_t8142 = { .type = APCIE_T8142, .compat = APCIE_T8122, @@ -323,6 +336,10 @@ static int pcie_init_controller(int controller, const char *path) fuse_bits = NULL; state->pcie_regs = ®s_t8132; printf("pcie: Initializing t8132 PCIe controller\n"); + } else if (adt_is_compatible(adt, adt_offset, "apcie,t8140")) { + fuse_bits = NULL; + state->pcie_regs = ®s_t8140; + printf("pcie: Initializing t8140 PCIe controller\n"); } else if (adt_is_compatible(adt, adt_offset, "apcie,t8142")) { fuse_bits = NULL; state->pcie_regs = ®s_t8142; @@ -406,7 +423,7 @@ static int pcie_init_controller(int controller, const char *path) return -1; } - if (state->pcie_regs->compat == APCIE_T8122) { + if (state->pcie_regs->compat == APCIE_T8122 || state->pcie_regs->type == APCIE_T8140) { // The T8122 init seems very similar to the T602X, with different offsets, // and with reg[2], [3] and [4] coalesced to reg[2] in the ADT. state->phy_base[0] += state->pcie_regs->type == APCIE_T8142 ? 0x20000 : 0x8000; @@ -505,7 +522,8 @@ static int pcie_init_controller(int controller, const char *path) set32(state->rc_base + APCIE_PHYIF_CTRL, APCIE_PHYIF_CTRL_RUN); udelay(1); } else if (state->pcie_regs->type == APCIE_T602X || - state->pcie_regs->compat == APCIE_T8122) { + state->pcie_regs->compat == APCIE_T8122 || + state->pcie_regs->type == APCIE_T8140) { set32(state->phy_base[phy] + 4, 0x01); } @@ -519,24 +537,27 @@ static int pcie_init_controller(int controller, const char *path) fuse << fuse_bits[i].tgt_bit); } - char pll_prop[64]; - char auspma_prop[64]; + if (state->pcie_regs->type == APCIE_T81XX || state->pcie_regs->type == APCIE_T602X || + state->pcie_regs->compat == APCIE_T8122) { + char pll_prop[64]; + char auspma_prop[64]; - if (state->num_phys == 1) { - strcpy(pll_prop, "apcie-phy-ip-pll-tunables"); - strcpy(auspma_prop, "apcie-phy-ip-auspma-tunables"); - } else { - snprintf(pll_prop, sizeof(pll_prop), "apcie-phy-%d-ip-pll-tunables", phy); - snprintf(auspma_prop, sizeof(auspma_prop), "apcie-phy-%d-ip-auspma-tunables", phy); - } + if (state->num_phys == 1) { + strcpy(pll_prop, "apcie-phy-ip-pll-tunables"); + strcpy(auspma_prop, "apcie-phy-ip-auspma-tunables"); + } else { + snprintf(pll_prop, sizeof(pll_prop), "apcie-phy-%d-ip-pll-tunables", phy); + snprintf(auspma_prop, sizeof(auspma_prop), "apcie-phy-%d-ip-auspma-tunables", phy); + } - if (tunables_apply_local_addr(path, pll_prop, state->phy_ip_base[phy])) { - printf("pcie: Error applying %s for %s\n", pll_prop, path); - return -1; - } - if (tunables_apply_local_addr(path, auspma_prop, state->phy_ip_base[phy])) { - printf("pcie: Error applying %s for %s\n", auspma_prop, path); - return -1; + if (tunables_apply_local_addr(path, pll_prop, state->phy_ip_base[phy])) { + printf("pcie: Error applying %s for %s\n", pll_prop, path); + return -1; + } + if (tunables_apply_local_addr(path, auspma_prop, state->phy_ip_base[phy])) { + printf("pcie: Error applying %s for %s\n", auspma_prop, path); + return -1; + } } /* T8142 PHY-IP post-tuning write; the bit's function is unknown. */ @@ -548,10 +569,13 @@ static int pcie_init_controller(int controller, const char *path) } } - if (state->pcie_regs->type == APCIE_T602X || state->pcie_regs->compat == APCIE_T8122) { + if (state->pcie_regs->type == APCIE_T602X || state->pcie_regs->compat == APCIE_T8122 || + state->pcie_regs->type == APCIE_T8140) { mask32(state->phy_common_base + APCIE_PHYCMN_CLK, APCIE_PHYCMN_CLK_MODE, FIELD_PREP(APCIE_PHYCMN_CLK_MODE, 1)); + } + if (state->pcie_regs->type == APCIE_T602X || state->pcie_regs->compat == APCIE_T8122) { // Why always PHY 1 in this case? u32 off = state->num_phys > 1 ? PHY_STRIDE : 0; if (poll32(state->phy_base[0] + off + 0x8, 1, 1, 250000)) { @@ -567,6 +591,10 @@ static int pcie_init_controller(int controller, const char *path) set32(state->phy_base[phy] + APCIE_PHY_CTRL, 0x200); } } + } + + if (state->pcie_regs->type == APCIE_T602X || state->pcie_regs->compat == APCIE_T8122 || + state->pcie_regs->type == APCIE_T8140) { write32(state->rc_base + 0x54, 0x140); write32(state->rc_base + 0x50, 0x1); if (poll32(state->rc_base + 0x58, 1, 1, 250000)) { @@ -653,7 +681,8 @@ static int pcie_init_controller(int controller, const char *path) clear32(state->axi_base + 0x600, BIT(16)); } - if (state->pcie_regs->type == APCIE_T602X || state->pcie_regs->compat == APCIE_T8122) { + if (state->pcie_regs->type == APCIE_T602X || state->pcie_regs->compat == APCIE_T8122 || + state->pcie_regs->type == APCIE_T8140) { write32(state->port_base[port] + 0x88, 0x110); write32(state->port_base[port] + 0x100, 0xffffffff); write32(state->port_base[port] + 0x148, 0xffffffff); @@ -662,7 +691,8 @@ static int pcie_init_controller(int controller, const char *path) write32(state->port_base[port] + 0x84, 0x0); if (state->pcie_regs->type == APCIE_T602X) { write32(state->port_base[port] + 0x104, 0x7fffffff); - } else if (state->pcie_regs->compat == APCIE_T8122) { + } else if (state->pcie_regs->compat == APCIE_T8122 || + state->pcie_regs->type == APCIE_T8140) { write32(state->port_base[port] + 0x104, 0xfffffff0); } write32(state->port_base[port] + 0x124, 0x100); @@ -671,7 +701,7 @@ static int pcie_init_controller(int controller, const char *path) write32(state->port_base[port] + 0x800, 0x100100); write32(state->port_base[port] + 0x808, 0x1000ff); write32(state->port_base[port] + 0x82c, 0x0); - if (state->pcie_regs->compat == APCIE_T8122) { + if (state->pcie_regs->compat == APCIE_T8122 || state->pcie_regs->type == APCIE_T8140) { for (int i = 0; i < 16; i++) { write32(state->port_base[port] + 0x3000 + 4 * i, 0); } @@ -691,7 +721,8 @@ static int pcie_init_controller(int controller, const char *path) write32(state->port_base[port] + 0x144, 0x253770); write32(state->port_base[port] + 0x21c, 0x0); write32(state->port_base[port] + 0x834, 0x0); - if (controller != APCIE || state->pcie_regs->compat == APCIE_T8122) + if (controller != APCIE || state->pcie_regs->compat == APCIE_T8122 || + state->pcie_regs->type == APCIE_T8140) write32(state->port_base[port] + 0x83c, 0x0); } @@ -702,7 +733,8 @@ static int pcie_init_controller(int controller, const char *path) set32(state->port_base[port] + APCIE_PORT_APPCLK, APCIE_PORT_APPCLK_EN); - if (state->pcie_regs->type == APCIE_T602X || state->pcie_regs->compat == APCIE_T8122) { + if (state->pcie_regs->type == APCIE_T602X || state->pcie_regs->compat == APCIE_T8122 || + state->pcie_regs->type == APCIE_T8140) { clear32(state->port_phy_base[port] + APCIE_PHY_CTRL, APCIE_PHY_CTRL_CLK0REQ | APCIE_PHY_CTRL_CLK1REQ); @@ -722,7 +754,8 @@ static int pcie_init_controller(int controller, const char *path) if (state->pcie_regs->type == APCIE_T602X) { clear32(state->port_phy_base[port] + APCIE_PHY_CTRL, 0x4000); - } else if (state->pcie_regs->compat == APCIE_T8122) { + } else if (state->pcie_regs->compat == APCIE_T8122 || + state->pcie_regs->type == APCIE_T8140) { clear32(state->port_phy_base[port] + APCIE_PHY_CTRL, 0x10); } set32(state->port_phy_base[port] + APCIE_PHY_CTRL, 0x200); diff --git a/src/types.h b/src/types.h index b17e54e9..a85738b9 100644 --- a/src/types.h +++ b/src/types.h @@ -27,6 +27,20 @@ typedef s64 ssize_t; #endif +// clang-format off +/* + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-unused-type-attribute + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-unused-variable-attribute + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-unused-label-attribute + * clang: https://clang.llvm.org/docs/AttributeReference.html#maybe-unused-unused + * + * Copied from Linux' include/linux/compiler_attributes.h + */ +// clang-format on +#define __always_unused __attribute__((__unused__)) +#define __maybe_unused __attribute__((__unused__)) + #define UNUSED(x) (void)(x) #define ALIGNED(x) __attribute__((aligned(x))) #define PACKED __attribute__((packed))