Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,35 @@ properties:
- $ref: /schemas/types.yaml#/definitions/uint32
enum: [0, 1]

apple,aon-pmgr:
$ref: /schemas/types.yaml#/definitions/phandle
description:
The always-on power manager (a syscon) that holds the power state words
of the coprocessor's CPU, of the SRAM its firmware runs from, and of the
rest of its power chain.

apple,aon-power-words:
$ref: /schemas/types.yaml#/definitions/uint32-array
minItems: 1
maxItems: 8
description:
Offsets in apple,aon-pmgr of the power state words of the coprocessor's
power chain, parents first.

apple,aon-cpu-word:
$ref: /schemas/types.yaml#/definitions/uint32
description:
Offset in apple,aon-pmgr of the coprocessor CPU's power state word. Its
presence says the CPU can be reset in place with its power state kept,
so the host may stop the firmware with an RTKit sleep and start it again
from its reset vector.

apple,aon-sram-word:
$ref: /schemas/types.yaml#/definitions/uint32
description:
Offset in apple,aon-pmgr of the power state word of the SRAM holding the
coprocessor's firmware. It has to stay on across a CPU reset.

'#helper-cells':
const: 0
description:
Expand All @@ -131,6 +160,11 @@ required:
- mboxes
- iommus

dependencies:
apple,aon-power-words: [ 'apple,aon-pmgr' ]
apple,aon-cpu-word: [ 'apple,aon-pmgr', 'apple,aon-sram-word' ]
apple,aon-sram-word: [ 'apple,aon-pmgr', 'apple,aon-cpu-word' ]

allOf:
- if:
properties:
Expand Down Expand Up @@ -159,5 +193,9 @@ examples:
reg-names = "asc", "sram";
mboxes = <&mtp_mbox>;
iommus = <&mtp_dart 0>;
apple,aon-pmgr = <&pmgr_mini>;
apple,aon-power-words = <0xc000 0xc018 0xc030 0xc028 0xc048 0xc050>;
apple,aon-cpu-word = <0xc028>;
apple,aon-sram-word = <0xc048>;
};
};
11 changes: 10 additions & 1 deletion arch/arm64/boot/dts/apple/t8140.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@
compatible = "apple,t8140-pmgr", "apple,t8103-pmgr", "syscon", "simple-mfd";
#address-cells = <1>;
#size-cells = <1>;
reg = <0x3 0x8280000 0 0xc000>;
reg = <0x3 0x8280000 0 0x10000>;
/* child nodes are added in t8140-pmgr.dtsi */
};

Expand Down Expand Up @@ -1149,6 +1149,15 @@
mboxes = <&mtp_mbox>;
iommus = <&mtp_dart 0>;
#helper-cells = <0>;
/*
* The always-on PMGR words of its power chain, parents first:
* MTP_FABRIC, MTP_PERIPH, MTP_SCM_FABRIC, MTP_CPU, MTP_SRAM
* (child of both MTP_SCM_FABRIC and MTP_CPU), MTP_DMA.
*/
apple,aon-pmgr = <&pmgr_mini>;
apple,aon-power-words = <0xc000 0xc018 0xc030 0xc028 0xc048 0xc050>;
apple,aon-cpu-word = <0xc028>;
apple,aon-sram-word = <0xc048>;
status = "disabled";
};

Expand Down
1 change: 1 addition & 0 deletions drivers/soc/apple/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ config APPLE_SOC_SLEEP
depends on ARM64 && (ARCH_APPLE || COMPILE_TEST)
depends on HOTPLUG_CPU && SUSPEND
select APPLE_TUNABLE
select MFD_SYSCON
help
Powers the secondary CPUs off and suspends the system to RAM on an
Apple SoC whose kernel runs directly at EL2, with no monitor to ask.
Expand Down
94 changes: 80 additions & 14 deletions drivers/soc/apple/rtkit-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <linux/pm_wakeup.h>
#include <linux/soc/apple/rtkit.h>
#include <linux/soc/apple/rtkit-helper.h>
#include <linux/soc/apple/sleep.h>
#include <linux/soc/apple/t8132.h>
#include <linux/string.h>
#include <linux/suspend.h>
Expand Down Expand Up @@ -44,6 +45,13 @@ struct apple_rtkit_helper {
bool ap_idle;
/* It was told, gave no answer, and wasn't told otherwise. */
bool idle_unanswered;
/*
* Its CPU can be reset in place (apple,aon-cpu-word, as on the T8140 MTP): started after
* a CPU reset and stopped with an RTKit sleep, to restart.
*/
bool restartable;
/* The firmware is stopping for a sleep, so RTKit may free the buffers given to it. */
bool free_buffers;
/* Ends a sleep whose .prepare failed, which gets no .complete. */
struct notifier_block pm_nb;

Expand Down Expand Up @@ -109,7 +117,11 @@ static int apple_rtkit_helper_shmem_setup(void *cookie, struct apple_rtkit_shmem

static void apple_rtkit_helper_shmem_destroy(void *cookie, struct apple_rtkit_shmem *bfr)
{
// no-op
struct apple_rtkit_helper *helper = cookie;

/* Others keep theirs: a quiesced firmware may still write its log. */
if (helper->free_buffers && bfr->buffer && !bfr->is_mapped)
dma_free_coherent(helper->dev, bfr->size, bfr->buffer, bfr->iova);
}

static const struct apple_rtkit_ops apple_rtkit_helper_ops = {
Expand Down Expand Up @@ -244,6 +256,67 @@ static int apple_rtkit_helper_inherit_buffers(
return count;
}

/*
* A restartable firmware (the T8140 MTP) that ran and was stopped for a sleep, as U-Boot leaves
* it, only comes back from its reset vector, and the run bit alone doesn't take it there. So its
* CPU is reset first. An image iBoot loaded and nobody started boots cold either way.
*/
static int apple_rtkit_helper_start(struct apple_rtkit_helper *helper)
{
struct device *dev = helper->dev;
void __iomem *ctrl = helper->asc_base + APPLE_ASC_CPU_CONTROL;
int ret;

if (helper->restartable) {
if (readl(ctrl) & APPLE_ASC_CPU_CONTROL_RUN) {
dev_warn(dev, "CPU already running with no handoff; not resetting it\n");
} else {
ret = apple_sleep_aon_cpu_reset(dev);
if (ret)
dev_warn(dev, "no CPU reset (%d); only a never-started firmware boots\n",
ret);
}
}

writel_relaxed(APPLE_ASC_CPU_CONTROL_RUN, ctrl);

/* Works for both wake and boot */
if (of_device_is_compatible(dev->of_node, "apple,t8142-mtp"))
return apple_rtkit_wake_early_ap(helper->rtk);
return apple_rtkit_wake(helper->rtk);
}

/*
* A restartable helper gets an RTKit sleep, so apple_rtkit_helper_start() can bring it back, and
* is left running with its buffers if it doesn't answer. Others are quiesced.
*/
static void apple_rtkit_helper_stop(struct apple_rtkit_helper *helper)
{
void __iomem *ctrl = helper->asc_base + APPLE_ASC_CPU_CONTROL;
int ret;

if (!helper->restartable) {
if (apple_rtkit_is_running(helper->rtk))
apple_rtkit_quiesce(helper->rtk);
writel_relaxed(0, ctrl);
return;
}

if (apple_rtkit_is_running(helper->rtk)) {
helper->free_buffers = true;
ret = apple_rtkit_shutdown(helper->rtk);
if (ret) {
helper->free_buffers = false;
dev_err(helper->dev, "RTKit sleep not answered (%d); left running\n", ret);
return;
}
}

/* Twice, each time from what it reads back, as for a sleep. */
writel(readl(ctrl) & ~APPLE_ASC_CPU_CONTROL_RUN, ctrl);
writel(readl(ctrl) & ~APPLE_ASC_CPU_CONTROL_RUN, ctrl);
}

static int apple_rtkit_helper_pm_register(struct apple_rtkit_helper *helper);

static int apple_rtkit_helper_probe(struct platform_device *pdev)
Expand All @@ -263,6 +336,7 @@ static int apple_rtkit_helper_probe(struct platform_device *pdev)
return -ENOMEM;

helper->dev = dev;
helper->restartable = of_property_present(dev->of_node, "apple,aon-cpu-word");
platform_set_drvdata(pdev, helper);

helper->asc_base = devm_platform_ioremap_resource_byname(pdev, "asc");
Expand Down Expand Up @@ -364,17 +438,12 @@ static int apple_rtkit_helper_probe(struct platform_device *pdev)
return dev_err_probe(dev, PTR_ERR(helper->rtk),
"Failed to initialize RTKit\n");

writel_relaxed(APPLE_ASC_CPU_CONTROL_RUN,
helper->asc_base + APPLE_ASC_CPU_CONTROL);

/* Works for both wake and boot */
if (of_device_is_compatible(dev->of_node, "apple,t8142-mtp"))
ret = apple_rtkit_wake_early_ap(helper->rtk);
else
ret = apple_rtkit_wake(helper->rtk);
ret = apple_rtkit_helper_start(helper);
if (ret != 0)
return dev_err_probe(dev, ret, "Failed to wake up coprocessor\n");

if (helper->restartable)
dev_info(dev, "started the MTP without a handoff\n");
return apple_rtkit_helper_pm_register(helper);
}

Expand All @@ -386,10 +455,7 @@ static void apple_rtkit_helper_remove(struct platform_device *pdev)
if (helper->adopted || helper->preserved)
return;

if (apple_rtkit_is_running(helper->rtk))
apple_rtkit_quiesce(helper->rtk);

writel_relaxed(0, helper->asc_base + APPLE_ASC_CPU_CONTROL);
apple_rtkit_helper_stop(helper);
}

static int apple_rtkit_helper_ap_power(struct apple_rtkit_helper *helper, bool on)
Expand Down Expand Up @@ -536,7 +602,7 @@ static int __maybe_unused apple_rtkit_helper_freeze(struct device *dev)
struct apple_rtkit_helper *helper = dev_get_drvdata(dev);

/* A hibernation image cannot recreate the firmware's live DMA ownership. */
return helper->adopted ? -EBUSY : 0;
return helper->adopted || helper->restartable ? -EBUSY : 0;
}

static const struct dev_pm_ops apple_rtkit_helper_pm_ops = {
Expand Down
55 changes: 55 additions & 0 deletions drivers/soc/apple/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <linux/iopoll.h>
#include <linux/irqchip/irq-apple-aic.h>
#include <linux/ktime.h>
#include <linux/mfd/syscon.h>
#include <linux/moduleparam.h>
#include <linux/mutex.h>
#include <linux/of.h>
Expand All @@ -50,6 +51,7 @@
#include <linux/platform_device.h>
#include <linux/panic.h>
#include <linux/pm_domain.h>
#include <linux/regmap.h>
#include <linux/smp.h>
#include <linux/soc/apple/dart.h>
#include <linux/soc/apple/sleep.h>
Expand Down Expand Up @@ -1410,6 +1412,59 @@ int apple_sleep_engine_reset(struct device *dev)
}
EXPORT_SYMBOL_GPL(apple_sleep_engine_reset);

/*
* Resets a stopped always-on coprocessor CPU in place, as the PMGR's device reset does, so the
* run bit starts it from its reset vector. The power state stays, so the SRAM keeps the firmware.
* The device tree names the always-on PMGR (apple,aon-pmgr) and, in it, the CPU's word
* (apple,aon-cpu-word) and that of the SRAM holding its firmware (apple,aon-sram-word).
*/
int apple_sleep_aon_cpu_reset(struct device *dev)
{
struct device_node *np = dev->of_node;
struct regmap *aon;
u32 cpu, sram, sval, before, held, after;
int ret;

if (!apple_sleep)
return -ENODEV;
aon = syscon_regmap_lookup_by_phandle(np, "apple,aon-pmgr");
if (IS_ERR(aon) || of_property_read_u32(np, "apple,aon-cpu-word", &cpu) ||
of_property_read_u32(np, "apple,aon-sram-word", &sram))
return -ENODEV;

/* Check the targets: the PMGR's own idling can lower the actual states. */
ret = regmap_read(aon, cpu, &before) ?: regmap_read(aon, sram, &sval);
if (ret)
return ret;
if (FIELD_GET(PS_TARGET, before) != 0xf || FIELD_GET(PS_TARGET, sval) != 0xf) {
dev_err(dev, "CPU reset refused: CPU %#x, SRAM %#x\n", before, sval);
return -EIO;
}

regmap_write(aon, cpu, (before & ~PS_FLAGS) | PS_DEV_DISABLE);
ret = regmap_read_poll_timeout_atomic(aon, cpu, held, !(held & PS_BUSY), 1, 100);
if (ret) {
regmap_write_bits(aon, cpu, PS_FLAGS | PS_DEV_DISABLE, 0);
dev_err(dev, "CPU reset refused: CPU %#x stayed busy\n", held);
return ret;
}
regmap_write(aon, cpu, (held & ~PS_FLAGS) | PS_RESET);
usleep_range(10, 20);
regmap_read(aon, cpu, &held);
regmap_write(aon, cpu, held & ~(PS_FLAGS | PS_RESET));
regmap_write_bits(aon, cpu, PS_FLAGS | PS_DEV_DISABLE, 0);
regmap_read(aon, cpu, &after);
regmap_read(aon, sram, &sval);

dev_info(dev, "CPU reset in place: %#x before, %#x held, %#x after, SRAM %#x\n", before,
held, after, sval);
if (FIELD_GET(PS_TARGET, after) != 0xf || FIELD_GET(PS_TARGET, sval) != 0xf ||
(after & (PS_RESET | PS_DEV_DISABLE)))
return -EIO;
return 0;
}
EXPORT_SYMBOL_GPL(apple_sleep_aon_cpu_reset);

/* The order macOS does it in, across clusters. */
static void sleep_dvfm_park(struct apple_sleep *s)
{
Expand Down
12 changes: 12 additions & 0 deletions include/linux/soc/apple/sleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ void apple_sleep_restore_pins(void);
* sleep engine's PMGR reset of its own domains, its DARTs kept. The caller starts it after.
*/
int apple_sleep_engine_reset(struct device *dev);

/*
* Resets the stopped CPU of an always-on coprocessor (the MTP) with its power state kept.
* -ENODEV when the device tree describes no such CPU or there's no sleep driver. The caller
* starts it after.
*/
int apple_sleep_aon_cpu_reset(struct device *dev);
#else
static inline int apple_sleep_coproc_register(struct device *dev, void __iomem *asc,
bool pcie_ans, int (*last_stop)(void *data),
Expand Down Expand Up @@ -317,6 +324,11 @@ static inline int apple_sleep_engine_reset(struct device *dev)
{
return -EOPNOTSUPP;
}

static inline int apple_sleep_aon_cpu_reset(struct device *dev)
{
return -ENODEV;
}
#endif

#endif