diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index c7a693a0f8f58..708566542354d 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -741,6 +741,8 @@ struct apple_pcie { u32 msi_vector_base; }; +#define MAX_AUX_PERST 3 + struct apple_pcie_port { raw_spinlock_t lock; raw_spinlock_t irq_lock; @@ -762,6 +764,8 @@ struct apple_pcie_port { u32 saved_intmask; int sid_map_sz; struct gpio_desc *perst_gpio; + struct gpio_desc *aux_reset_gpios[MAX_AUX_PERST]; + u32 num_aux_resets; struct gpio_desc *pwren_gpio; int idx; /* Conventional port: link up when the sleep began, and found down after it */ @@ -3110,78 +3114,105 @@ static int apple_pcie_link_pwren(struct apple_pcie *pcie, struct gpio_desc *pwre return devm_add_action_or_reset(pcie->dev, apple_pcie_unlink_pwren, link); } -static int apple_pcie_setup_link(struct apple_pcie *pcie, - struct apple_pcie_port *port, - struct device_node *np) +/* + * Acquire the port's PERST# and PWREN GPIOs at probe time and establish the + * PWREN supplier device link before any suspend cycle. Bootloader-trained + * ports pass GPIOD_ASIS so probe never toggles PERST# on a live link; cold + * ports pass GPIOD_OUT_HIGH to assert PERST# before clock setup. + */ +static int apple_pcie_get_port_gpios(struct apple_pcie *pcie, + struct apple_pcie_port *port, + struct device_node *np, + enum gpiod_flags reset_flags) { -#define MAX_AUX_PERST 3 - struct gpio_desc *aux_reset[MAX_AUX_PERST] = { NULL }; + struct gpio_desc *reset, *pwren; u32 num_aux_resets = 0; - struct gpio_desc *reset, *pwren = NULL; - u32 stat; int ret; - /* Bringing the link up again, after a deep sleep. */ - if (port->perst_gpio) { - reset = port->perst_gpio; - pwren = port->pwren_gpio; - goto have_gpios; - } - /* - * Assert PERST# and configure the pin as output. + * On cold ports, assert PERST# and configure the pin as output. * The Aquantia AQC113 10GB nic used desktop macs is sensitive to * deasserting it without prior clock setup. * Observed on M1 Max/Ultra Mac Studios under m1n1's hypervisor. */ reset = devm_fwnode_gpiod_get(pcie->dev, of_fwnode_handle(np), "reset", - GPIOD_OUT_HIGH, "PERST#"); - if (IS_ERR(reset)) - return PTR_ERR(reset); - // HACK: use additional "reset-gpios" until pci-pwrctrl gains PERST# support. - for (u32 idx = 0; idx < MAX_AUX_PERST; idx++) { - aux_reset[idx] = devm_fwnode_gpiod_get_index(pcie->dev, - of_fwnode_handle(np), - "reset", idx + 1, - GPIOD_OUT_HIGH, - "PERST#"); - if (IS_ERR(aux_reset[idx])) { - if (PTR_ERR(aux_reset[idx]) == -ENOENT) - break; - else - return PTR_ERR(aux_reset[idx]); + reset_flags, "PERST#"); + if (IS_ERR(reset)) { + if (reset_flags == GPIOD_ASIS && PTR_ERR(reset) == -ENOENT) + reset = NULL; + else + return PTR_ERR(reset); + } + + if (reset) { + /* HACK: use additional "reset-gpios" until pci-pwrctrl gains PERST# support. */ + for (u32 idx = 0; idx < MAX_AUX_PERST; idx++) { + struct gpio_desc *aux; + + aux = devm_fwnode_gpiod_get_index(pcie->dev, + of_fwnode_handle(np), + "reset", idx + 1, + reset_flags, + "PERST#"); + if (IS_ERR(aux)) { + if (PTR_ERR(aux) == -ENOENT) + break; + return PTR_ERR(aux); + } + port->aux_reset_gpios[idx] = aux; + num_aux_resets++; } - num_aux_resets++; + if (reset_flags != GPIOD_ASIS) + dev_info(pcie->dev, "Using %u auxiliary PERST#\n", + num_aux_resets); + port->perst_gpio = reset; + port->num_aux_resets = num_aux_resets; + if (!num_aux_resets) + port->perst = reset; } - dev_info(pcie->dev, "Using %u auxiliary PERST#\n", num_aux_resets); - if (!num_aux_resets) - port->perst = reset; pwren = devm_fwnode_gpiod_get(pcie->dev, of_fwnode_handle(np), "pwren", - GPIOD_ASIS, "PWREN"); + GPIOD_ASIS, "PWREN"); if (IS_ERR(pwren)) { if (PTR_ERR(pwren) == -ENOENT) pwren = NULL; else return PTR_ERR(pwren); } - if (!num_aux_resets) { - port->perst_gpio = reset; - port->pwren_gpio = pwren; - } + + port->pwren_gpio = pwren; if (pwren && pcie->hw->s2r_noirq) { ret = apple_pcie_link_pwren(pcie, pwren); if (ret) return ret; } -have_gpios: + return 0; +} + +static int apple_pcie_setup_link(struct apple_pcie *pcie, + struct apple_pcie_port *port, + struct device_node *np) +{ + struct gpio_desc *reset = port->perst_gpio; + struct gpio_desc *pwren = port->pwren_gpio; + u32 stat; + int ret; + + if (!reset) + return -ENOENT; + rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK); /* Assert PERST# before setting up the clock */ - gpiod_set_value_cansleep(reset, 1); - for (u32 idx = 0; idx < num_aux_resets; idx++) - gpiod_set_value_cansleep(aux_reset[idx], 1); + ret = gpiod_direction_output(reset, 1); + if (ret) + return ret; + for (u32 idx = 0; idx < port->num_aux_resets; idx++) { + ret = gpiod_direction_output(port->aux_reset_gpios[idx], 1); + if (ret) + return ret; + } /* Power-cycle devices which cannot inherit their firmware state. */ if (pcie->hw->pwren_off_ms) { @@ -3217,8 +3248,11 @@ static int apple_pcie_setup_link(struct apple_pcie *pcie, ret = gpiod_set_value_cansleep(reset, 0); if (ret) return ret; - for (u32 idx = 0; idx < num_aux_resets; idx++) - gpiod_set_value_cansleep(aux_reset[idx], 0); + for (u32 idx = 0; idx < port->num_aux_resets; idx++) { + ret = gpiod_set_value_cansleep(port->aux_reset_gpios[idx], 0); + if (ret) + return ret; + } if (pcie->hw->pipe_rst) { ret = readl_relaxed_poll_timeout(port->base + PORT_T8152_PIPE_RST_STS, @@ -3270,7 +3304,7 @@ static int apple_pcie_port_reset_endpoint(struct apple_pcie *pcie, apple_pcie_port_rmw_clear(port, PORT_LTSSMCTL_START, PORT_LTSSMCTL); rmw_clear(PORT_PERST_OFF, port->base + pcie->hw->port_perst); - ret = gpiod_set_value_cansleep(port->perst, 1); + ret = gpiod_direction_output(port->perst, 1); if (ret) return ret; usleep_range(100, 200); @@ -3500,9 +3534,18 @@ static int apple_pcie_setup_port(struct apple_pcie *pcie, if (ret) goto err_teardown; } + ret = apple_pcie_get_port_gpios(pcie, port, np, + GPIOD_OUT_HIGH); + if (ret) + goto err_teardown; ret = apple_pcie_setup_link(pcie, port, np); if (ret) goto err_teardown; + } else { + ret = apple_pcie_get_port_gpios(pcie, port, np, + GPIOD_ASIS); + if (ret) + goto err_teardown; } } @@ -8303,7 +8346,7 @@ static int apple_pcie_neo_rail_cycle(struct apple_pcie *pcie, struct apple_pcie_ struct pci_dev *fn; int i, ret; - ret = gpiod_set_value_cansleep(port->perst_gpio, 1); + ret = gpiod_direction_output(port->perst_gpio, 1); if (!ret) ret = gpiod_set_value_cansleep(port->pwren_gpio, 0); if (ret) @@ -8439,7 +8482,7 @@ static int apple_pcie_neo_resume_noirq(struct apple_pcie *pcie) list_for_each_entry(port, &pcie->ports, entry) { /* The pin controller just put it back as it was: released, with no clock yet. */ - ret = gpiod_set_value_cansleep(port->perst_gpio, 1); + ret = gpiod_direction_output(port->perst_gpio, 1); if (!ret) ret = apple_pcie_neo_cold_init(pcie, port); if (ret) { @@ -10138,7 +10181,7 @@ static void apple_pcie_hib_ports_phased(struct apple_pcie *pcie) continue; rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK); /* Assert PERST# before setting up the clock */ - ret = gpiod_set_value_cansleep(port->perst_gpio, 1); + ret = gpiod_direction_output(port->perst_gpio, 1); if (!ret && port->pwren_gpio) { ret = gpiod_set_value_cansleep(port->pwren_gpio, 1); powered = true; @@ -10217,7 +10260,7 @@ static bool apple_pcie_hib_phased_ok(struct apple_pcie *pcie) if (pcie->hw->pwren_off_ms || pcie->hw->pipe_rst || pcie->hw->retain_clocks) return false; list_for_each_entry(port, &pcie->ports, entry) - if (port->hib_cold && !port->perst_gpio) + if (port->hib_cold && (!port->perst_gpio || port->num_aux_resets)) return false; return true; } diff --git a/tools/asahi/pcie-apple-pwren-link-tests.py b/tools/asahi/pcie-apple-pwren-link-tests.py new file mode 100755 index 0000000000000..c40ff744311f4 --- /dev/null +++ b/tools/asahi/pcie-apple-pwren-link-tests.py @@ -0,0 +1,467 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Exercise pcie-apple probe-time GPIO acquisition and PWREN device-link setup.""" +import argparse +import re +import resource +import subprocess +import sys +import tempfile +from pathlib import Path + +resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) +ROOT = Path(__file__).resolve().parents[2] +SRC = (ROOT / "drivers/pci/controller/pcie-apple.c").read_text() + + +def extract_function(src: str, name: str) -> str: + m = re.search(rf"^static\s+(?:void|int|bool)\s+{name}\s*\(", src, re.M) + if not m: + raise RuntimeError(f"function {name} not found") + start = m.start() + brace = src.index("{", start) + depth = 1 + pos = brace + 1 + while depth: + ch = src[pos] + if ch == "{": + depth += 1 + elif ch == "}": + depth -= 1 + pos += 1 + return src[start:pos] + + +def extract_setup_port_branch(src: str) -> str: + sp = extract_function(src, "apple_pcie_setup_port") + m = re.search( + r"(\t\tlink_stat = apple_pcie_port_readl\(port, PORT_LINKSTS\);.*?\n\t\t\}(?=\n))", + sp, + re.S, + ) + if not m: + raise RuntimeError("link_stat branch in apple_pcie_setup_port not found") + body = m.group(1) + return ( + "static int test_setup_port_link_branch(struct apple_pcie *pcie, " + "struct apple_pcie_port *port, struct device_node *np)\n" + "{\n" + "\tu32 link_stat;\n" + "\tint ret = 0;\n" + + body + + "\n\treturn 0;\n" + "err_teardown:\n" + "\treturn ret;\n" + "}" + ) + + +def build_production(src: str) -> str: + fns = [ + "apple_pcie_get_port_gpios", + "apple_pcie_setup_link", + "apple_pcie_hib_phased_ok", + ] + return "\n\n".join( + [extract_function(src, f) for f in fns] + [extract_setup_port_branch(src)] + ) + + +FIXTURE = r''' +#include +#include +#include +#include +#include +#include +#include + +typedef uint32_t u32; +typedef int32_t s32; + +#define MAX_AUX_PERST 3 +#define EPROBE_DEFER 517 +#define PORT_APPCLK 0x0 +#define PORT_APPCLK_EN (1U << 0) +#define PORT_PERST_OFF (1U << 0) +#define PORT_T8152_PIPE_RST_STS 0x10 +#define PORT_T8152_PIPE_RST_STS_LANES (1U << 0) +#define PORT_T8152_PIPE_RST (1U << 1) +#define PORT_STATUS 0x20 +#define PORT_STATUS_READY (1U << 0) +#define PORT_LINKSTS 0x24 +#define PORT_LINKSTS_UP (1U << 0) + +#define ERR_PTR(e) ((void *)(intptr_t)(e)) +#define PTR_ERR(p) ((long)(intptr_t)(p)) +#define IS_ERR(p) ((uintptr_t)(p) >= (uintptr_t)-4095) + +enum gpiod_flags { + GPIOD_ASIS = 0, + GPIOD_OUT_HIGH = 1, +}; + +struct device { int dummy; }; +struct device_node { int dummy; }; +struct fwnode_handle { int dummy; }; + +struct gpio_desc { + const char *name; + enum gpiod_flags acquired_flags; + bool is_output; + int val; + int dir_out_calls; + int set_val_calls; +}; + +struct list_head { + struct list_head *next, *prev; +}; + +#define INIT_LIST_HEAD(h) do { (h)->next = (h); (h)->prev = (h); } while (0) +#define list_add_tail(n, h) do { \ + (n)->next = (h); (n)->prev = (h)->prev; \ + (h)->prev->next = (n); (h)->prev = (n); \ +} while (0) +#define container_of(ptr, type, member) \ + ((type *)((char *)(ptr) - offsetof(type, member))) +#define list_for_each_entry(pos, head, member) \ + for (pos = container_of((head)->next, typeof(*pos), member); \ + &pos->member != (head); \ + pos = container_of(pos->member.next, typeof(*pos), member)) + +struct apple_pcie_hw { + bool s2r_noirq; + u32 port_perst; + u32 pwren_off_ms; + u32 pwren_on_ms; + bool retain_clocks; + bool pipe_rst; +}; + +struct apple_pcie { + struct device *dev; + const struct apple_pcie_hw *hw; + struct list_head ports; + bool managed_sids; +}; + +struct apple_pcie_port { + struct list_head entry; + char *base; + struct gpio_desc *perst; + struct gpio_desc *perst_gpio; + struct gpio_desc *aux_reset_gpios[MAX_AUX_PERST]; + u32 num_aux_resets; + struct gpio_desc *pwren_gpio; + bool hib_cold; +}; + +static struct fwnode_handle fake_fwnode; +static struct gpio_desc reset_desc, aux0_desc, pwren_desc; +static int reset_err, aux0_err, pwren_err, link_pwren_err; +static int get_gpiod_calls, link_pwren_calls, refclk_calls, info_calls, err_calls; +static bool forbid_probe_only_calls; +static enum gpiod_flags last_reset_flags, last_pwren_flags; +static u32 reg_space[16]; + +static struct fwnode_handle *of_fwnode_handle(struct device_node *np) +{ + return &fake_fwnode; +} + +static struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev, + struct fwnode_handle *fwnode, + const char *con_id, + unsigned int idx, + enum gpiod_flags flags, + const char *label) +{ + assert(!forbid_probe_only_calls); + get_gpiod_calls++; + if (strcmp(con_id, "reset") == 0 && idx == 0) { + last_reset_flags = flags; + if (reset_err) + return ERR_PTR(reset_err); + reset_desc.acquired_flags = flags; + reset_desc.is_output = (flags == GPIOD_OUT_HIGH); + reset_desc.val = (flags == GPIOD_OUT_HIGH) ? 1 : 0; + return &reset_desc; + } + if (strcmp(con_id, "reset") == 0 && idx == 1) { + if (aux0_err) + return ERR_PTR(aux0_err); + aux0_desc.acquired_flags = flags; + aux0_desc.is_output = (flags == GPIOD_OUT_HIGH); + aux0_desc.val = (flags == GPIOD_OUT_HIGH) ? 1 : 0; + return &aux0_desc; + } + if (strcmp(con_id, "reset") == 0) + return ERR_PTR(-ENOENT); + if (strcmp(con_id, "pwren") == 0) { + last_pwren_flags = flags; + if (pwren_err) + return ERR_PTR(pwren_err); + pwren_desc.acquired_flags = flags; + return &pwren_desc; + } + return ERR_PTR(-ENOENT); +} + +static struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev, + struct fwnode_handle *fwnode, + const char *con_id, + enum gpiod_flags flags, + const char *label) +{ + return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0, flags, label); +} + +static int apple_pcie_link_pwren(struct apple_pcie *pcie, struct gpio_desc *pwren) +{ + assert(!forbid_probe_only_calls); + assert(pwren == &pwren_desc); + link_pwren_calls++; + return link_pwren_err; +} + +static int gpiod_direction_output(struct gpio_desc *desc, int value) +{ + if (!desc) + return 0; + desc->is_output = true; + desc->val = value; + desc->dir_out_calls++; + return 0; +} + +static int gpiod_set_value_cansleep(struct gpio_desc *desc, int value) +{ + if (!desc) + return 0; + desc->val = value; + desc->set_val_calls++; + return 0; +} + +static void rmw_set(u32 set, void *addr) { *(u32 *)addr |= set; } +static void rmw_clear(u32 clear, void *addr) { *(u32 *)addr &= ~clear; } +static u32 fake_linksts; +static u32 apple_pcie_port_readl(struct apple_pcie_port *port, u32 reg) +{ + assert(reg == PORT_LINKSTS); + return fake_linksts; +} +static int apple_pcie_radio_check_cold(struct apple_pcie_port *port) { return 0; } +static int apple_pcie_setup_refclk(struct apple_pcie *pcie, struct apple_pcie_port *port) +{ + refclk_calls++; + return 0; +} +static void msleep(unsigned int ms) {} +static void usleep_range(unsigned long min, unsigned long max) {} + +#define readl_relaxed_poll_timeout(addr, val, cond, delay, timeout) \ + ({ (val) = *(u32 *)(addr); (cond) ? 0 : -ETIMEDOUT; }) + +#define dev_info(...) (info_calls++) +#define dev_err(...) (err_calls++) + +/*PRODUCTION*/ + +static void reset_env(void) +{ + memset(&reset_desc, 0, sizeof(reset_desc)); + memset(&aux0_desc, 0, sizeof(aux0_desc)); + memset(&pwren_desc, 0, sizeof(pwren_desc)); + memset(reg_space, 0, sizeof(reg_space)); + reg_space[PORT_STATUS / 4] = PORT_STATUS_READY; + reset_err = 0; + aux0_err = -ENOENT; + pwren_err = 0; + link_pwren_err = 0; + get_gpiod_calls = 0; + link_pwren_calls = 0; + refclk_calls = 0; + info_calls = 0; + err_calls = 0; + forbid_probe_only_calls = false; + last_reset_flags = (enum gpiod_flags)-1; + last_pwren_flags = (enum gpiod_flags)-1; +} + +static void init_pcie(struct apple_pcie *pcie, struct apple_pcie_hw *hw, + struct device *dev, struct apple_pcie_port *port) +{ + memset(pcie, 0, sizeof(*pcie)); + memset(port, 0, sizeof(*port)); + pcie->dev = dev; + pcie->hw = hw; + INIT_LIST_HEAD(&pcie->ports); + port->base = (char *)reg_space; + list_add_tail(&port->entry, &pcie->ports); +} + +int main(void) +{ + struct device dev = {0}; + struct device_node np = {0}; + struct apple_pcie_hw hw = { .s2r_noirq = true, .port_perst = 0x4 }; + struct apple_pcie pcie; + struct apple_pcie_port port; + + /* 1. Bootloader-trained port acquires GPIOs with GPIOD_ASIS and links PWREN at probe */ + reset_env(); + fake_linksts = PORT_LINKSTS_UP; + init_pcie(&pcie, &hw, &dev, &port); + assert(test_setup_port_link_branch(&pcie, &port, &np) == 0); + assert(last_reset_flags == GPIOD_ASIS); + assert(last_pwren_flags == GPIOD_ASIS); + assert(!reset_desc.is_output && reset_desc.val == 0); + assert(port.perst_gpio == &reset_desc); + assert(port.perst == &reset_desc); + assert(port.pwren_gpio == &pwren_desc); + assert(port.num_aux_resets == 0); + assert(get_gpiod_calls == 3); + assert(link_pwren_calls == 1); + assert(info_calls == 0); + + /* 2. Resume after deep sleep uses cached GPIOs, sets output direction, never links PWREN again */ + forbid_probe_only_calls = true; + assert(apple_pcie_setup_link(&pcie, &port, &np) == 0); + assert(reset_desc.is_output); + assert(reset_desc.dir_out_calls == 1); + assert(reset_desc.val == 0); + assert(pwren_desc.val == 1); + assert(refclk_calls == 1); + assert(link_pwren_calls == 1); + + /* 3. Phased resume is eligible on the first wake for a bootloader-trained single-reset port */ + port.hib_cold = true; + assert(apple_pcie_hib_phased_ok(&pcie) == true); + + /* 4. Auxiliary reset port is NOT eligible for phased resume, and setup_link drives aux PERST# */ + reset_env(); + aux0_err = 0; + init_pcie(&pcie, &hw, &dev, &port); + assert(apple_pcie_get_port_gpios(&pcie, &port, &np, GPIOD_ASIS) == 0); + assert(port.num_aux_resets == 1); + assert(port.perst_gpio == &reset_desc); + assert(port.perst == NULL); + assert(port.aux_reset_gpios[0] == &aux0_desc); + assert(link_pwren_calls == 1); + port.hib_cold = true; + assert(apple_pcie_hib_phased_ok(&pcie) == false); + forbid_probe_only_calls = true; + assert(apple_pcie_setup_link(&pcie, &port, &np) == 0); + assert(reset_desc.is_output && aux0_desc.is_output); + assert(aux0_desc.dir_out_calls == 1 && aux0_desc.val == 0); + + /* 5. Bootloader-trained port without reset-gpios still succeeds and links PWREN */ + reset_env(); + reset_err = -ENOENT; + init_pcie(&pcie, &hw, &dev, &port); + assert(apple_pcie_get_port_gpios(&pcie, &port, &np, GPIOD_ASIS) == 0); + assert(port.perst_gpio == NULL); + assert(port.pwren_gpio == &pwren_desc); + assert(link_pwren_calls == 1); + forbid_probe_only_calls = true; + assert(apple_pcie_setup_link(&pcie, &port, &np) == -ENOENT); + port.hib_cold = true; + assert(apple_pcie_hib_phased_ok(&pcie) == false); + + /* 6. Cold port fails on missing reset-gpios, and both paths propagate -EPROBE_DEFER */ + reset_env(); + reset_err = -ENOENT; + init_pcie(&pcie, &hw, &dev, &port); + assert(apple_pcie_get_port_gpios(&pcie, &port, &np, GPIOD_OUT_HIGH) == -ENOENT); + + reset_env(); + pwren_err = -EPROBE_DEFER; + init_pcie(&pcie, &hw, &dev, &port); + assert(apple_pcie_get_port_gpios(&pcie, &port, &np, GPIOD_ASIS) == -EPROBE_DEFER); + assert(link_pwren_calls == 0); + + /* 7. Non-s2r_noirq host does not register a PWREN device link */ + reset_env(); + hw.s2r_noirq = false; + init_pcie(&pcie, &hw, &dev, &port); + assert(apple_pcie_get_port_gpios(&pcie, &port, &np, GPIOD_OUT_HIGH) == 0); + assert(last_reset_flags == GPIOD_OUT_HIGH); + assert(reset_desc.is_output && reset_desc.val == 1); + assert(link_pwren_calls == 0); + assert(info_calls == 1); + + return 0; +} +''' + + +def check_setup_port_wiring(src: str) -> None: + setup_port = extract_function(src, "apple_pcie_setup_port") + if "apple_pcie_get_port_gpios(pcie, port, np," not in setup_port: + raise AssertionError("apple_pcie_setup_port does not call apple_pcie_get_port_gpios") + + phased = extract_function(src, "apple_pcie_hib_ports_phased") + if "gpiod_direction_output(port->perst_gpio, 1)" not in phased: + raise AssertionError("apple_pcie_hib_ports_phased must assert PERST# via gpiod_direction_output") + reset_ep = extract_function(src, "apple_pcie_port_reset_endpoint") + if "gpiod_direction_output(port->perst, 1)" not in reset_ep: + raise AssertionError("apple_pcie_port_reset_endpoint must assert PERST# via gpiod_direction_output") + + +def apply_mutation(src: str, mutation: str) -> str: + if mutation == "drop-boot-trained-gpios": + old = "\t\t} else {\n\t\t\tret = apple_pcie_get_port_gpios(pcie, port, np,\n\t\t\t\t\t\t\tGPIOD_ASIS);\n\t\t\tif (ret)\n\t\t\t\tgoto err_teardown;\n\t\t}" + assert old in src + return src.replace(old, "\t\t}", 1) + if mutation == "wrong-reset-direction": + old = "\tret = gpiod_direction_output(reset, 1);" + assert old in src + return src.replace(old, "\tret = gpiod_set_value_cansleep(reset, 1);", 1) + if mutation == "drop-aux-phased-guard": + old = "if (port->hib_cold && (!port->perst_gpio || port->num_aux_resets))" + assert old in src + return src.replace(old, "if (port->hib_cold && !port->perst_gpio)", 1) + if mutation == "drop-asis-enoent": + old = "if (reset_flags == GPIOD_ASIS && PTR_ERR(reset) == -ENOENT)" + assert old in src + return src.replace(old, "if (false)", 1) + raise ValueError(f"unknown mutation {mutation}") + + +def main() -> int: + ap = argparse.ArgumentParser() + ap.add_argument("--mutation", choices=[ + "drop-boot-trained-gpios", + "wrong-reset-direction", + "drop-aux-phased-guard", + "drop-asis-enoent", + ]) + args = ap.parse_args() + + src = SRC if not args.mutation else apply_mutation(SRC, args.mutation) + try: + check_setup_port_wiring(src) + prod = build_production(src) + with tempfile.TemporaryDirectory() as td: + c_file = Path(td) / "test.c" + exe = Path(td) / "test" + c_file.write_text(FIXTURE.replace("/*PRODUCTION*/", prod)) + subprocess.run( + ["clang", "-std=gnu11", "-Wall", "-Werror", "-Wno-unused-parameter", str(c_file), "-o", str(exe)], + check=True, + ) + r = subprocess.run([str(exe)], capture_output=True, text=True) + if r.returncode != 0: + raise AssertionError(f"test binary failed: {r.stderr.strip()}") + except AssertionError as e: + print(f"FAIL: {e}", file=sys.stderr) + return 1 + + print("7 pcie-apple PWREN probe-link tests PASS") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index ccce1c105894a..412e12bdb115b 100644 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -655,6 +655,11 @@ def mutations(flag, names, base, expect=KILL): 'usb-rpm', 'usb-epoch-syscore', 'vlp-client', 'six-band'), out('--output')) + [(out('--output') + ['--old-usb'], KILL), (out('--output') + ['--old-regulatory'], KILL)], 'pcie-adopted-clkpm-tests.py': [([], PASS)], + 'pcie-apple-pwren-link-tests.py': [([], PASS)] + + mutations('--mutation', ('drop-boot-trained-gpios', + 'wrong-reset-direction', + 'drop-aux-phased-guard', + 'drop-asis-enoent'), []), 'pciec-policy-tests.py': [(out(), PASS)], 'pmgr-bus-tests.py': [([], PASS)], 'sep-suspend-errors-tests.py': [(out(), PASS)]