Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions arch/arm64/boot/dts/apple/t6050-die0.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,13 @@
/* ADT info-clock_offset [0x2100, 6]: M1-style 32768 Hz ticks, as on T6040. */
smc_rtc: rtc {
compatible = "apple,smc-rtc";
nvmem-cells = <&rtc_offset>;
nvmem-cell-names = "rtc_offset";
nvmem-cells = <&rtc_offset>, <&rtc_ctrl>, <&rtc_alarm>,
<&rtc_alarm_mask>;
nvmem-cell-names = "rtc_offset", "rtc_ctrl", "rtc_alarm",
"rtc_alarm_mask";
/* The PMIC's RTC alarm, peripheral interrupt 2 of nub-spmi0 */
interrupts-extended = <&nub_spmi0 2 IRQ_TYPE_EDGE_RISING>;
wakeup-source;
};
};

Expand Down Expand Up @@ -222,7 +227,8 @@
};

/*
* /arm-io/nub-spmi0: reg[0] 0x8a014000 +0x4000, generation 4 (no IRQ: the driver refuses one).
* /arm-io/nub-spmi0: reg[0] 0x8a014000 +0x4000, generation 4.
* ADT interrupts[1] is 726, the line the peripheral sources arrive on.
* No power-domains: NUB_SPMI0 is critical and on (Aura talks to the PMU here every boot), and
* binding the pwrstate driver would write the PS register at probe.
*/
Expand All @@ -231,6 +237,9 @@
reg = <0x2 0x8a014000 0x0 0x4000>;
#address-cells = <2>;
#size-cells = <0>;
interrupts = <AIC_IRQ 0 726 IRQ_TYPE_LEVEL_HIGH>;
interrupt-controller;
#interrupt-cells = <2>;

/* spmi-abbeyL1, is-primary: USID 0xe. Read-only use: cells, never a whole-device read. */
pmu: pmic@e {
Expand All @@ -247,6 +256,19 @@
reg = <0x2001 1>;
};

/* ADT spmi-abbeyL1 RTC cells, as on T6030 */
rtc_ctrl: rtc-ctrl@2000 {
reg = <0x2000 1>;
};

rtc_alarm: rtc-alarm@2008 {
reg = <0x2008 4>;
};

rtc_alarm_mask: rtc-alarm-mask@200e {
reg = <0x200e 1>;
};

rtc_offset: rtc-offset@2100 {
reg = <0x2100 6>; /* ADT info-clock_offset */
};
Expand Down
56 changes: 35 additions & 21 deletions drivers/soc/apple/sleep-t6050.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,10 @@ struct apple_sleep_region_state {
u32 spmi_mask[T6050_SPMI_CTRLS];
bool spmi_masked;
bool spmi_post_timer;
/*
* nub-spmi0's and each of apple,spmi-buses's peripheral sources as the last acknowledgement
* found them (t6050_spmi_src_ack): latched, enabled, and those it acknowledged
*/
/* Peripheral sources seen by t6050_spmi_src_ack(), before any W1C. */
u32 spmi_src_latched[T6050_SPMI_CTRLS][T6050_SPMI_SRC_WORDS];
u32 spmi_src_enabled[T6050_SPMI_CTRLS][T6050_SPMI_SRC_WORDS];
u32 spmi_src_acked[T6050_SPMI_CTRLS][T6050_SPMI_SRC_WORDS];
u32 spmi_src_events[T6050_SPMI_CTRLS][T6050_SPMI_SRC_WORDS];
bool spmi_src_valid;
/* This wake's status words are read and not yet in its events (t6050_wake_events_report) */
bool wake_status_fresh;
Expand Down Expand Up @@ -2455,24 +2452,38 @@ static int t6050_sleep_entry_lower(struct apple_sleep *s)
* nub-spmi-a1.0x0B rtc"). A latched source that isn't enabled stays set: nub-spmi-a0's +0x600
* reads 0x12051c00 through every macOS sleep.
*
* Linux takes no SPMI interrupt, and acknowledged only nub-spmi0's PMU sources. Do to the
* controllers what macOS's handler does: at the entry, before the sleep's masks, and at the
* wake, before the run-time masks are back, acknowledge each latched source that is enabled
* (and on nub-spmi0 the DT's PMU sources) on nub-spmi0 and each of apple,spmi-buses. The
* enables are left as they are. What the wake found is kept for its events. An event after the
* entry's acknowledgement wakes the sleep, as it should.
* Acknowledge enabled latched sources and the DT's PMU sources at entry and at wake. On
* wake, leave nub-spmi0 sources armed by the Linux SPMI IRQ domain pending for its
* dispatcher. Record the candidates before filtering W1C so the wake log includes
* deferred IRQs. An event after the entry's clear still wakes the sleep.
*/
#define T6050_SPMI_SRC_EN 0x400

static void t6050_spmi_src_ack(struct apple_sleep *s)
static u32 t6050_spmi_ack_mask(u32 sources, unsigned int word, struct device *wake_bus,
bool (*wake_armed)(struct device *dev, unsigned int hwirq))
{
unsigned long pending = sources;
unsigned int bit;

if (!wake_bus)
return sources;

for_each_set_bit(bit, &pending, 32)
if (wake_armed(wake_bus, 32 * word + bit))
sources &= ~BIT(bit);

return sources;
}

static void t6050_spmi_src_ack(struct apple_sleep *s, bool resume)
{
struct apple_sleep_region_state *r = s->region;
unsigned int b, i, pmu_word = (r->spmi_pmu_status - M5_SPMI_SRC_STATUS) / 4;
void __iomem *bus;
u32 ack;

BUILD_BUG_ON(ARRAY_SIZE(r->spmi_src_acked) != 1 + ARRAY_SIZE(r->spmi_bus));
for (b = 0; b < ARRAY_SIZE(r->spmi_src_acked); b++) {
BUILD_BUG_ON(ARRAY_SIZE(r->spmi_src_events) != 1 + ARRAY_SIZE(r->spmi_bus));
for (b = 0; b < ARRAY_SIZE(r->spmi_src_events); b++) {
bus = b ? r->spmi_bus[b - 1] : s->spmi;
for (i = 0; i < T6050_SPMI_SRC_WORDS; i++) {
r->spmi_src_latched[b][i] = 0;
Expand All @@ -2484,7 +2495,10 @@ static void t6050_spmi_src_ack(struct apple_sleep *s)
ack = r->spmi_src_latched[b][i] & r->spmi_src_enabled[b][i];
if (!b && r->spmi_pmu_bits && i == pmu_word)
ack |= r->spmi_src_latched[b][i] & r->spmi_pmu_bits;
r->spmi_src_acked[b][i] = ack;
r->spmi_src_events[b][i] = ack;
if (resume && bus == s->spmi)
ack = t6050_spmi_ack_mask(ack, i, r->pmu_bus,
apple_spmi_irq_wake_armed);
if (ack)
writel(ack, bus + M5_SPMI_SRC_STATUS + 4 * i);
}
Expand Down Expand Up @@ -2530,7 +2544,7 @@ static void t6050_sleep_after_ans(struct apple_sleep *s)
t6050_late_lower(s, i);
r->spmi_mask[0] = readl(s->spmi + M5_SPMI_IRQ_EN);
/* Every controller's pending sources, as macOS's handler leaves them for the sleep */
t6050_spmi_src_ack(s);
t6050_spmi_src_ack(s, false);
r->spmi_src_valid = false;
/* nub-spmi0's mask first: its interrupt outputs are AP wake sources. */
writel(readl(s->spmi + M5_SPMI_IRQ_STATUS), s->spmi + M5_SPMI_IRQ_STATUS);
Expand Down Expand Up @@ -3789,7 +3803,7 @@ static int t6050_wake_words(char *buf, int len, int size, const u32 *w, unsigned
/*
* After each wake, one line in the form of macOS's console's ("system wake events: NUB.SPMI0Sw3IRQ
* nub-spmi0.0x02 nub-spmi-a1.0x0B rtc", wm2): the AP wake sources whose status is set, by their ADT
* names; the SPMI sources the wake acknowledged (latched and enabled), as <controller>.0x<source>
* names; the SPMI sources seen at wake, as <controller>.0x<source>
* with nub-spmi0 by its ADT name and the others by their nodes; and rtc if the PMU's alarm had
* fired. The SMC's reasons are its wake record's. Like macOS's, these are latched events, not
* proof that one of them alone raised the wake. The words as read go to the debug log.
Expand Down Expand Up @@ -3822,10 +3836,10 @@ static void t6050_wake_events_report(struct apple_sleep *s)
if (r->wake_status[T6050_WAKE_WORDS] & BIT(b))
n += scnprintf(ev + n, lim - n, " GFX%u", b);
}
for (b = 0; r->spmi_src_valid && b < ARRAY_SIZE(r->spmi_src_acked); b++) {
for (b = 0; r->spmi_src_valid && b < ARRAY_SIZE(r->spmi_src_events); b++) {
np = b ? of_parse_phandle(s->dev->of_node, "apple,spmi-buses", b - 1) : NULL;
for (i = 0; i < 32 * T6050_SPMI_SRC_WORDS; i++) {
if (!(r->spmi_src_acked[b][i / 32] & BIT(i % 32)))
if (!(r->spmi_src_events[b][i / 32] & BIT(i % 32)))
continue;
if (np)
n += scnprintf(ev + n, lim - n, " %pOFP.0x%02X", np, i);
Expand All @@ -3846,7 +3860,7 @@ static void t6050_wake_events_report(struct apple_sleep *s)
m = t6050_wake_words(raw, m, sizeof(raw), r->wake_status, T6050_WAKE_WORDS + 1);
dev_dbg(s->dev, "wake events: %s\n", raw);
}
for (b = 0; r->spmi_src_valid && b < ARRAY_SIZE(r->spmi_src_acked); b++) {
for (b = 0; r->spmi_src_valid && b < ARRAY_SIZE(r->spmi_src_events); b++) {
for (i = 0; i < T6050_SPMI_SRC_WORDS; i++)
if (r->spmi_src_latched[b][i] | r->spmi_src_enabled[b][i])
break;
Expand Down Expand Up @@ -3877,7 +3891,7 @@ static void t6050_sleep_wake_regs(struct apple_sleep *s)

if (r->spmi_masked) {
/* What the controllers latched while the SoC slept, before the run-time masks */
t6050_spmi_src_ack(s);
t6050_spmi_src_ack(s, true);
writel(r->spmi_mask[0], s->spmi + M5_SPMI_IRQ_EN);
for (i = 0; i < ARRAY_SIZE(r->spmi_bus); i++)
if (r->spmi_bus[i])
Expand Down
31 changes: 31 additions & 0 deletions drivers/soc/apple/sleep-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,36 @@ static void apple_sleep_test_wdt_handoff(struct kunit *test)
apple_sleep_wdt_unregister(&st->dev2);
}

static bool apple_sleep_test_spmi_wake_armed(struct device *dev, unsigned int hwirq)
{
const unsigned long *armed = dev_get_drvdata(dev);

return test_bit(hwirq, armed);
}

static void apple_sleep_test_t6050_spmi_ack(struct kunit *test)
{
DECLARE_BITMAP(armed, 32 * T6050_SPMI_SRC_WORDS) = {};
struct device bus = { .driver_data = armed };
u32 first = BIT(2) | BIT(11) | BIT(15);
u32 later = BIT(4) | BIT(31);

set_bit(2, armed);
set_bit(15, armed);
set_bit(127, armed);
KUNIT_EXPECT_EQ(test, t6050_spmi_ack_mask(first, 0, &bus,
apple_sleep_test_spmi_wake_armed), (u32)BIT(11));
KUNIT_EXPECT_EQ(test, t6050_spmi_ack_mask(later, 3, &bus,
apple_sleep_test_spmi_wake_armed), (u32)BIT(4));
KUNIT_EXPECT_EQ(test, t6050_spmi_ack_mask(later, 2, &bus,
apple_sleep_test_spmi_wake_armed), later);
KUNIT_EXPECT_EQ(test, t6050_spmi_ack_mask(first, 0, NULL,
apple_sleep_test_spmi_wake_armed), first);
bitmap_zero(armed, 32 * T6050_SPMI_SRC_WORDS);
KUNIT_EXPECT_EQ(test, t6050_spmi_ack_mask(first, 0, &bus,
apple_sleep_test_spmi_wake_armed), first);
}

static void apple_sleep_test_t6040_wdt_span(struct kunit *test)
{
struct resource full = DEFINE_RES_MEM(T6040_WDT_PA, 0x4000);
Expand Down Expand Up @@ -539,6 +569,7 @@ static struct kunit_case apple_sleep_test_cases[] = {
KUNIT_CASE(apple_sleep_test_books),
KUNIT_CASE(apple_sleep_test_wake_names),
KUNIT_CASE(apple_sleep_test_wdt_handoff),
KUNIT_CASE(apple_sleep_test_t6050_spmi_ack),
KUNIT_CASE(apple_sleep_test_t6040_wdt_span),
{}
};
Expand Down
20 changes: 20 additions & 0 deletions drivers/spmi/spmi-apple-controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,26 @@ void apple_spmi_fifo_unlock(struct device *dev)
}
EXPORT_SYMBOL_GPL(apple_spmi_fifo_unlock);

/* Only called during the platform's noirq resume with secondary CPUs offline. */
bool apple_spmi_irq_wake_armed(struct device *dev, unsigned int hwirq)
{
struct apple_spmi *spmi;
struct irq_data *data;
unsigned int irq;

if (!dev || dev->driver != &apple_spmi_driver.driver)
return false;
spmi = dev_get_drvdata(dev);
if (!spmi->irqd || hwirq >= spmi->hw->num_irqs ||
!test_bit(hwirq, spmi->irq_mask_cache))
return false;

irq = irq_find_mapping(spmi->irqd, hwirq);
data = irq ? irq_get_irq_data(irq) : NULL;
return data && irqd_is_wakeup_armed(data) && !irqd_irq_disabled(data);
}
EXPORT_SYMBOL_GPL(apple_spmi_irq_wake_armed);

module_platform_driver(apple_spmi_driver);

MODULE_AUTHOR("Jean-Francois Bortolotti <jeff@borto.fr>");
Expand Down
7 changes: 7 additions & 0 deletions include/linux/soc/apple/spmi-fifo.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <linux/errno.h>
#include <linux/kconfig.h>
#include <linux/types.h>

struct device;

Expand All @@ -18,6 +19,7 @@ struct device;
*/
int apple_spmi_fifo_trylock(struct device *dev);
void apple_spmi_fifo_unlock(struct device *dev);
bool apple_spmi_irq_wake_armed(struct device *dev, unsigned int hwirq);
#else
static inline int apple_spmi_fifo_trylock(struct device *dev)
{
Expand All @@ -27,6 +29,11 @@ static inline int apple_spmi_fifo_trylock(struct device *dev)
static inline void apple_spmi_fifo_unlock(struct device *dev)
{
}

static inline bool apple_spmi_irq_wake_armed(struct device *dev, unsigned int hwirq)
{
return false;
}
#endif

#endif
Loading