Skip to content
103 changes: 100 additions & 3 deletions drivers/edac/apple_soc_edac.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Copyright The Aurora Silicon Contributors
*/

#include <linux/bitops.h>
#include <linux/edac.h>
#include <linux/io.h>
#include <linux/interrupt.h>
Expand Down Expand Up @@ -36,9 +37,23 @@ struct apple_soc_edac_range {
const char *name;
};

/*
* A fabric NI whose error status sits in its aperture: a block of status words,
* one per error type, whose set bits name the port. They're acknowledged by
* writing the set bits back.
*/
struct apple_soc_edac_ni {
u8 index; /* interrupt index */
u8 reg; /* reg index of the NI's aperture */
u16 status; /* offset of the first status word */
u32 words; /* bit n: a status word at status + 4 * n */
};

struct apple_soc_edac_hw {
const struct apple_soc_edac_range *ranges;
size_t nranges;
const struct apple_soc_edac_ni *ni;
size_t nni;
unsigned int num_irqs;
unsigned int num_status_regs;
bool event_log;
Expand All @@ -51,6 +66,8 @@ struct apple_soc_edac_irq {
const char *source;
unsigned int index;
int irq;
const struct apple_soc_edac_ni *ni;
void __iomem *ni_base;
unsigned int retries;
unsigned int backoff_ms;
unsigned long armed_jiffies;
Expand Down Expand Up @@ -126,6 +143,16 @@ static const struct apple_soc_edac_range t8140_error_ranges[] = {
{ 25, 1, "pio" },
};

/* From the ADT's niN-metadata and macOS's NI decoder tables */
static const struct apple_soc_edac_ni t8140_ni[] = {
{ 18, 9, 0x1900, 0x1ffd7ff }, /* SoC NI 0 */
{ 19, 10, 0x1b00, 0x1ffd7ff }, /* SoC NI 1 */
{ 20, 11, 0x1500, 0x1ffd7ff }, /* SoC NI 2 */
{ 21, 12, 0x2500, 0x1ffd7ff }, /* MCC NI */
{ 22, 13, 0x2100, 0x7fefff }, /* AFC MCC NI */
{ 23, 14, 0x1d00, 0x3ff7ff }, /* GFX NI */
};

static const struct apple_soc_edac_hw t6000_error_hw = {
.ranges = t6000_error_ranges,
.nranges = ARRAY_SIZE(t6000_error_ranges),
Expand All @@ -148,6 +175,8 @@ static const struct apple_soc_edac_hw t8112_error_hw = {
static const struct apple_soc_edac_hw t8140_error_hw = {
.ranges = t8140_error_ranges,
.nranges = ARRAY_SIZE(t8140_error_ranges),
.ni = t8140_ni,
.nni = ARRAY_SIZE(t8140_ni),
.num_irqs = 26,
};

Expand Down Expand Up @@ -214,6 +243,38 @@ static bool apple_soc_edac_t6041_status(struct apple_soc_edac_irq *irq_data,
return false;
}

/*
* Log and acknowledge an NI's set status words. Returns whether any was set;
* @cleared says whether all of them read back clear.
*/
static bool apple_soc_edac_ni_status(struct apple_soc_edac_irq *irq_data,
bool *cleared)
{
const struct apple_soc_edac_ni *ni = irq_data->ni;
unsigned long words = ni->words;
unsigned int bit, offset;
bool found = false;
u32 status, left;

*cleared = true;
for_each_set_bit(bit, &words, 32) {
offset = ni->status + 4 * bit;
status = readl(irq_data->ni_base + offset);
if (!status)
continue;
writel(status, irq_data->ni_base + offset);
left = readl(irq_data->ni_base + offset);
dev_err_ratelimited(irq_data->edac->dev,
"%s: status %#x at %#x, %#x after the acknowledge\n",
irq_data->source, status, offset, left);
found = true;
if (left)
*cleared = false;
}

return found;
}

static void apple_soc_edac_rearm(struct work_struct *work)
{
struct apple_soc_edac_irq *irq_data =
Expand Down Expand Up @@ -244,12 +305,27 @@ static irqreturn_t apple_soc_edac_irq_thread(int irq, void *data)
return IRQ_HANDLED;
}

snprintf(msg, sizeof(msg), "%s source %u", irq_data->source,
irq_data->index);
if (irq_data->ni) {
bool cleared;

if (apple_soc_edac_ni_status(irq_data, &cleared)) {
edac_device_handle_ue(edac->dci, 0, 0, msg);
dev_err_ratelimited(edac->dev, "hardware error: %s (IRQ %d)\n",
msg, irq);
if (cleared)
return IRQ_HANDLED;
} else {
dev_err_ratelimited(edac->dev, "%s asserted with no status set\n",
msg);
}
}

/* No status was pending, or this source has no clear contract. */
disable_irq_nosync(irq);

snprintf(msg, sizeof(msg), "%s source %u", irq_data->source,
irq_data->index);
if (!edac->hw->num_status_regs || irq_data->index < 45) {
if (!irq_data->ni && (!edac->hw->num_status_regs || irq_data->index < 45)) {
edac_device_handle_ue(edac->dci, 0, 0, msg);
dev_err_ratelimited(edac->dev,
"hardware error: %s (IRQ %d)\n", msg, irq);
Expand Down Expand Up @@ -280,6 +356,24 @@ static irqreturn_t apple_soc_edac_irq_thread(int irq, void *data)
return IRQ_HANDLED;
}

static int apple_soc_edac_map_ni(struct platform_device *pdev,
struct apple_soc_edac_irq *irq_data)
{
const struct apple_soc_edac_hw *hw = irq_data->edac->hw;
size_t i;

for (i = 0; i < hw->nni; i++) {
if (hw->ni[i].index != irq_data->index)
continue;
irq_data->ni_base = devm_platform_ioremap_resource(pdev, hw->ni[i].reg);
if (IS_ERR(irq_data->ni_base))
return PTR_ERR(irq_data->ni_base);
irq_data->ni = &hw->ni[i];
}

return 0;
}

static int apple_soc_edac_probe(struct platform_device *pdev)
{
const struct apple_soc_edac_hw *hw;
Expand Down Expand Up @@ -356,6 +450,9 @@ static int apple_soc_edac_probe(struct platform_device *pdev)
irq_data->irq = irq;
irq_data->backoff_ms = APPLE_SOC_EDAC_BACKOFF_MS;
irq_data->armed_jiffies = jiffies;
ret = apple_soc_edac_map_ni(pdev, irq_data);
if (ret)
goto err_del_device;
INIT_DELAYED_WORK(&irq_data->rearm, apple_soc_edac_rearm);

ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
Expand Down
42 changes: 10 additions & 32 deletions drivers/phy/apple/atc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6652,10 +6652,11 @@ static int atcphy_probe(struct platform_device *pdev)
* controller is brought to D0, and xHCI restores its own state.
*
* USB2 and USB3 qualify, without DisplayPort, whose link the common block's power-down
* would end. With USB3 the halted controller's PIPE is parked on the dummy backend just
* before the hibernation, as macOS locks and parks it when its host goes to Sleep
* (setUSB3Mode, static), and the lanes are left as they are: the commit takes them down.
* At the wake the lanes come back in USB3 and the PIPE is routed to them when the controller's
* would end. With USB3 the PIPE stays routed to the USB3 PHY, as macOS leaves it (measured):
* the halted controller doesn't acknowledge a PIPE lock, and the power control acknowledges
* its PHY lock at the wake only with the PIPE on the PHY. The lanes are left as they are: the
* commit takes them down. At the wake the common block is set up again under that lock, with
* the PIPE parked, and the PIPE is routed to the lanes when the controller's
* core is set up, before xHCI starts, with no Type-C mode change and no SuperSpeed port power
* cycle. macOS's raise from Sleep to its saved USB3 mode is also just a handshake and the USB3
* PHY's power level (static, setUSB3Mode). macOS also stops its PHY's fabric traffic before
Expand Down Expand Up @@ -6689,26 +6690,6 @@ static bool atcphy_can_sleep_in_place(const struct apple_atcphy *atcphy)
!atcphy->dp_owned_pmas && !atcphy->aux_power_requested && !atcphy->tunnel_users;
}

/*
* The halted controller's PIPE goes to the dummy backend for the hibernation. A lock it
* doesn't acknowledge is only logged, as macOS's sleep only logs it too; the PIPE is then
* parked unlocked, as for a stopped controller.
*/
static int atcphy_sleep_park_pipe(struct apple_atcphy *atcphy)
{
bool dirty = atcphy->mode_dirty;
int ret;

ret = atcphy_park_pipehandler(atcphy, false);
if (!ret)
return 0;

dev_warn(atcphy->dev, "PIPE lock not acknowledged for the sleep (%d): parking unlocked\n",
ret);
atcphy->mode_dirty = dirty;
return atcphy_park_pipehandler(atcphy, true);
}

static void atcphy_usb2_sleep_enter(struct apple_atcphy *atcphy)
{
set32(atcphy->regs.usb2phy + USB2PHY_MISCTUNE, USB2PHY_MISCTUNE_REFCLK_GATE_OFF);
Expand Down Expand Up @@ -6904,13 +6885,17 @@ static int atcphy_sleep_power_down(struct apple_atcphy *atcphy)
* USB3, without the controller's reset or a USB2 PHY power cycle: atcphy_configure() powers
* the USB2 PHY on over the running one, which rewrites only what it holds, so the hub's link
* stays. The host role goes in again as at probe, for a USB2 PHY that lost its registers
* too. The PIPE stays parked until the controller's core is set up and routes it.
* too. The PIPE is parked first, unlocked as the controller's core is in reset, and stays
* parked until the core is set up and routes it.
*/
static int atcphy_common_restore(struct apple_atcphy *atcphy)
{
enum atcphy_mode mode = atcphy->mode;
int ret;

ret = atcphy_park_pipehandler(atcphy, true);
if (ret)
dev_warn(atcphy->dev, "PIPE not parked after the sleep: %d\n", ret);
atcphy_forget_common_state(atcphy);
ret = atcphy_power_off(atcphy);
if (ret)
Expand Down Expand Up @@ -6987,13 +6972,6 @@ static int atcphy_suspend_noirq(struct device *dev)
atcphy->sleep_ret = -EBUSY;
return -EBUSY;
}
ret = atcphy_sleep_park_pipe(atcphy);
if (ret) {
dev_err(dev, "PIPE not parked for the sleep: %d, refusing it\n", ret);
atcphy->sleep_ret = -EBUSY;
return -EBUSY;
}

ret = atcphy_controller_hibernate(atcphy);
if (ret)
return ret;
Expand Down