Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
$id: http://devicetree.org/schemas/display/apple,t8140-display-power.yaml#
$schema: http://devicetree.org/meta-schemas/core.yaml#

title: Apple T8140 and T8142 display power words
title: Apple T8132, T8140 and T8142 display power words

maintainers:
- Diljit Singh <hey@djdev.me>
Expand All @@ -18,6 +18,7 @@ description:
properties:
compatible:
enum:
- apple,t8132-display-power
- apple,t8140-display-power
- apple,t8142-display-power

Expand All @@ -38,7 +39,9 @@ allOf:
- if:
properties:
compatible:
const: apple,t8140-display-power
enum:
- apple,t8132-display-power
- apple,t8140-display-power
then:
properties:
reg:
Expand Down
23 changes: 23 additions & 0 deletions arch/arm64/boot/dts/apple/t8132-j713.dts
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,31 @@
apple,clock-protection = <&ps_prot 8>;
};

/*
* The words the firmware-27 stop of the internal DCP writes (by default, see
* dcp_s2r_layout_from_dt()), the same ones the deep sleep lowers: pmc-dcp, the PMGR
* power states and its power gate block.
*/
&soc {
dcp_power: display-power@3828a9c14 {
compatible = "apple,t8132-display-power";
reg = <0x3 0x828a9c14 0x0 0x4>,
<0x3 0x80700000 0x0 0x14000>,
<0x3 0x80724000 0x0 0x2000>;
reg-names = "pmc-dcp", "ps", "pwrgate";
};
};

/* Clock-protection slice 42 (ADT dart-dcp): it guards DISP_CPU's power state. */
&dcp_dart {
reg = <0x3 0x0a340000 0x0 0x20000>,
<0x3 0x8079cb80 0x0 0x4>;
reg-names = "dart", "gapf";
};

&dcp {
apple,pmc-interface = <&pmc_disp0>;
apple,display-power = <&dcp_power>;

panel: panel {
compatible = "apple,panel-j713", "apple,panel";
Expand Down
10 changes: 10 additions & 0 deletions drivers/gpu/drm/apple/dcp-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ struct dcp_s2r_layout {
u32 disp_streams;
/* "pmc-dcp" counts the powered DCPs: T8132 and T8140 only */
bool pmc_dcp;
/*
* From the device tree (dcp_s2r_layout_from_dt()): only the @*_mask bits of a
* retention word are written, @*_off while off and the value read at probe back on.
* Zero masks: the whole words, as above.
*/
u32 ret_ff_mask, ret_ff_off, ret_ff_on;
u32 ret_mem_mask, ret_mem_off, ret_mem_on;
};

/*
Expand Down Expand Up @@ -861,6 +868,9 @@ struct apple_dcp {
struct completion s2r_dart_done;
struct device *s2r_disp_dart;
struct device *s2r_dcp_dart;
/* hw.s2r without a layout in its hw data: the device tree's, see dcp_s2r_layout_from_dt() */
struct dcp_s2r_layout s2r_dt_layout;
bool s2r_dt_layout_ok;

/* eDP display without DP-HDMI conversion */
bool main_display;
Expand Down
179 changes: 163 additions & 16 deletions drivers/gpu/drm/apple/dcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,132 @@ static int dcp_secure_port_init(struct apple_dcp *dcp)
* display domains through apple,display-power, and the internal DCP only takes IOP idle
* once its secure port has gone idle. A device tree without those keeps the firmware
* running: DPMS is the backlight alone and the display stays up across a sleep. A SoC
* without a secure mailbox says so (hw.s2r_no_secure_port), and one behind
* appledrm.s2r_untested stops its firmware for DPMS off and J813 system sleep.
* without a secure mailbox says so (hw.s2r_no_secure_port). Power words from the device
* tree stop the firmware by default; a SoC whose static words are only derived (T8142) does
* so only with appledrm.s2r_untested.
*/
/*
* hw.s2r without a layout in its hw data takes its words from the device tree, the ones its
* deep sleep lowers: DISP_CPU is the first state of the DCP's power-domains, DISP_FE that
* state's parent and DISP_SYS theirs, each at its reg in the power manager whose block the
* display-power node's "ps" starts. The retention words are DISP_CPU's two
* apple,retention-workaround entries (power manager offset, mask, value while off), in the
* node's "pwrgate" block, and the value each reads now, with the DCP running, is the one it
* gets back at power-on. It counts in pmc-dcp where the node has that word. The DART streams
* are the ones its DARTs have enabled at probe, see dcp_s2r_layout_streams().
*/
static int dcp_s2r_layout_from_dt(struct apple_dcp *dcp)
{
struct dcp_s2r_layout *l = &dcp->s2r_dt_layout;
struct device_node *power, *pmgr = NULL, *ps[3] = {};
struct resource ps_res, gate_res, pmgr_res;
u32 off[3], ret[6];
void __iomem *gate;
int i, err = -EINVAL;

power = of_parse_phandle(dcp->dev->of_node, "apple,display-power", 0);
if (!power)
return -ENODEV;
if (of_address_to_resource(power, of_property_match_string(power, "reg-names", "ps"),
&ps_res) ||
of_address_to_resource(power, of_property_match_string(power, "reg-names", "pwrgate"),
&gate_res))
goto out;
ps[0] = of_parse_phandle(dcp->dev->of_node, "power-domains", 0);
for (i = 1; i < 3 && ps[i - 1]; i++)
ps[i] = of_parse_phandle(ps[i - 1], "power-domains", 0);
if (!ps[2])
goto out;
pmgr = of_get_parent(ps[0]);
if (!pmgr || of_address_to_resource(pmgr, 0, &pmgr_res) || pmgr_res.start != ps_res.start)
goto out;
for (i = 0; i < 3; i++) {
struct device_node *parent = of_get_parent(ps[i]);

of_node_put(parent);
if (parent != pmgr || of_property_read_u32(ps[i], "reg", &off[i]) ||
resource_size(&ps_res) < 4 || off[i] > resource_size(&ps_res) - 4 ||
off[i] % 4)
goto out;
}
if (of_property_read_u32_array(ps[0], "apple,retention-workaround", ret, 6) ||
of_property_count_u32_elems(ps[0], "apple,retention-workaround") != 6)
goto out;
for (i = 0; i < 6; i += 3) {
u64 at = pmgr_res.start + ret[i];

if (at <= gate_res.start || at + 4 > gate_res.end + 1 || at % 4 || !ret[i + 1] ||
ret[i + 2] & ~ret[i + 1])
goto out;
}
gate = ioremap(gate_res.start, resource_size(&gate_res));
if (!gate) {
err = -ENOMEM;
goto out;
}
*l = (struct dcp_s2r_layout){
.disp_cpu = off[0],
.disp_fe = off[1],
.disp_sys = off[2],
.ret_ff = pmgr_res.start + ret[0] - gate_res.start,
.ret_ff_mask = ret[1],
.ret_ff_off = ret[2],
.ret_mem = pmgr_res.start + ret[3] - gate_res.start,
.ret_mem_mask = ret[4],
.ret_mem_off = ret[5],
.pmc_dcp = of_property_match_string(power, "reg-names", "pmc-dcp") >= 0,
};
l->ret_ff_on = readl(gate + l->ret_ff);
l->ret_mem_on = readl(gate + l->ret_mem);
iounmap(gate);
dcp->s2r_dt_layout_ok = true;
dev_dbg(dcp->dev, "display power from the device tree: CPU %#x FE %#x SYS %#x, retention %#x/%#x on %#x/%#x\n",
l->disp_cpu, l->disp_fe, l->disp_sys, l->ret_ff, l->ret_mem, l->ret_ff_on,
l->ret_mem_on);
err = 0;
out:
for (i = 0; i < 3; i++)
of_node_put(ps[i]);
of_node_put(pmgr);
of_node_put(power);
return err;
}

static struct device *dcp_s2r_find_dart(struct device_node *np);

/*
* A device-tree layout's DART streams: the ones its DARTs have enabled at probe, with the
* firmware running. A DCP DART, or a piodma DART where the DCP has a piodma node, that is
* missing or reads none leaves nothing for the stop to restore, so the layout is refused.
*/
static int dcp_s2r_layout_streams(struct apple_dcp *dcp)
{
struct device_node *piodma;
struct device *dart;

dart = dcp_s2r_find_dart(dcp->dev->of_node);
if (!dart)
return -ENODEV;
dcp->s2r_dt_layout.dcp_streams = apple_dart_enabled_streams(dart);
put_device(dart);
if (!dcp->s2r_dt_layout.dcp_streams)
return -ENODATA;
piodma = of_get_child_by_name(dcp->dev->of_node, "piodma");
if (piodma) {
dart = dcp_s2r_find_dart(piodma);
of_node_put(piodma);
if (!dart)
return -ENODEV;
dcp->s2r_dt_layout.disp_streams = apple_dart_enabled_streams(dart);
put_device(dart);
if (!dcp->s2r_dt_layout.disp_streams)
return -ENODATA;
}
dev_dbg(dcp->dev, "DART streams for the stop: DCP %#x, display %#x\n",
dcp->s2r_dt_layout.dcp_streams, dcp->s2r_dt_layout.disp_streams);
return 0;
}

static void dcp_s2r_check_dt(struct apple_dcp *dcp)
{
struct device_node *np = dcp->dev->of_node;
Expand All @@ -308,6 +431,16 @@ static void dcp_s2r_check_dt(struct apple_dcp *dcp)
!power ? "apple,display-power" : "secure mailbox");
dcp->hw.s2r = false;
}
if (dcp->hw.s2r && !dcp->hw.s2r_layout) {
int ret = dcp_s2r_layout_from_dt(dcp) ?: dcp_s2r_layout_streams(dcp);

if (ret) {
dev_info(dcp->dev, "no display power layout (%d), the firmware keeps running\n",
ret);
dcp->s2r_dt_layout_ok = false;
dcp->hw.s2r = false;
}
}
if (dcp->hw.s2r_ext && !power) {
dev_info(dcp->dev, "no apple,display-power in the device tree, the firmware keeps running\n");
dcp->hw.s2r_ext = false;
Expand Down Expand Up @@ -484,7 +617,7 @@ static bool dcp_j714s_ext(struct device *dev)
static bool s2r_untested;
module_param(s2r_untested, bool, 0444);
MODULE_PARM_DESC(s2r_untested,
"Stop the DCP firmware on DPMS off and J813 system sleep using derived T8142 power words (default 0)");
"Stop the DCP firmware on DPMS off and system sleep using derived T8142 power words (default 0)");

bool dcp_t6041_ext_enabled(void)
{
Expand Down Expand Up @@ -7122,13 +7255,17 @@ static int dcp_s2r_pm_register(struct apple_dcp *dcp);
* - num_dptx_ports 0: it drives the panel only.
* - adopt_live_session: iBoot leaves it running with the 27.2 image and the
* panel lit; Linux never starts its CPU.
* No sleep support: no firmware-27 sleep has run on this processor.
* - s2r: the firmware-27 stop (iomfb_v27.c) for DPMS off and deep sleep,
* with the 27.2 table's calls and the power words its device tree names
* (dcp_s2r_layout_from_dt()). It has no secure mailbox.
*/
static const struct apple_dcp_hw_data apple_dcp_hw_t8132_v27 = {
.firmware_dma_bits = 41,
.num_dptx_ports = 0,
.adopt_live_session = true,
.firmware_compat = DCP_FIRMWARE_V_27_0,
.s2r = true,
.s2r_no_secure_port = true,
};

static const struct apple_dcp_hw_data apple_dcp_hw_t8132_dcpext_v27 = {
Expand Down Expand Up @@ -7310,10 +7447,13 @@ static int dcp_platform_probe_pipe(struct platform_device *pdev)
of_device_is_compatible(dev->of_node, "apple,t8132-dcp"))
dcp->hw = apple_dcp_hw_t8132_v27;
/*
* A SoC with derived power words is offered the stop only when asked for, and only on
* A stop needs power words: a static layout, or the device tree's (see
* dcp_s2r_layout_from_dt(), offered by default). A SoC whose static words are only
* derived (hw.s2r_untested, T8142) is offered it only when asked for, and only on
* firmware 27.0, whose calls are the ones that ran with them.
*/
if (dcp->hw.s2r && (!dcp->hw.s2r_layout ||
if (dcp->hw.s2r && ((!dcp->hw.s2r_layout &&
!of_property_present(dev->of_node, "apple,display-power")) ||
(dcp->hw.s2r_untested &&
(!s2r_untested || fw_compat != DCP_FIRMWARE_V_27_0))))
dcp->hw.s2r = false;
Expand Down Expand Up @@ -8282,7 +8422,9 @@ static const struct dcp_s2r_layout *dcp_s2r_layout(struct apple_dcp *dcp)
{
if (dcp_s2r_is_t8142_ext(dcp))
return &dcp_s2r_t8142_ext[dcp->index - 1];
return dcp->hw.s2r_layout ?: &dcp_s2r_t8140;
if (dcp->hw.s2r_layout)
return dcp->hw.s2r_layout;
return dcp->s2r_dt_layout_ok ? &dcp->s2r_dt_layout : &dcp_s2r_t8140;
}
#define DCP_PS_MIN GENMASK(19, 16)

Expand Down Expand Up @@ -8791,6 +8933,12 @@ static void dcp_s2r_ps_lower(void __iomem *reg)
writel((val & ~GENMASK(3, 0)) | GENMASK(9, 8), reg);
}

/* A retention word: the whole of it from a hw-data layout, only its masked bits from the DT's. */
static void dcp_s2r_ret_write(void __iomem *reg, u32 mask, u32 val, u32 word)
{
writel(mask ? (readl(reg) & ~mask) | (val & mask) : word, reg);
}

/* Its display-power words, pmc-dcp only where the layout has one. */
static int dcp_s2r_map_power(struct apple_dcp *dcp, void __iomem **ps, void __iomem **pwrgate,
void __iomem **pmc)
Expand Down Expand Up @@ -8829,8 +8977,8 @@ static int dcp_s2r_domains_off(struct apple_dcp *dcp)
if (val & BIT(28))
writel(val & ~(BIT(28) | GENMASK(9, 8)), ps + l->disp_cpu);
if (l->ret_ff) {
writel(3, pwrgate + l->ret_ff);
writel(0, pwrgate + l->ret_mem);
dcp_s2r_ret_write(pwrgate + l->ret_ff, l->ret_ff_mask, l->ret_ff_off, 3);
dcp_s2r_ret_write(pwrgate + l->ret_mem, l->ret_mem_mask, l->ret_mem_off, 0);
}
dcp_s2r_ps_lower(ps + l->disp_cpu);
dcp_s2r_ps_lower(ps + l->disp_fe);
Expand Down Expand Up @@ -8881,8 +9029,8 @@ static int dcp_s2r_domains_on(struct apple_dcp *dcp)
}
writel(val | 0xf, ps + l->disp_cpu);
if (l->ret_ff) {
writel(0, pwrgate + l->ret_ff);
writel(1, pwrgate + l->ret_mem);
dcp_s2r_ret_write(pwrgate + l->ret_ff, l->ret_ff_mask, l->ret_ff_on, 0);
dcp_s2r_ret_write(pwrgate + l->ret_mem, l->ret_mem_mask, l->ret_mem_on, 1);
}
writel(val | 0xf | BIT(28), ps + l->disp_cpu);
if (pmc)
Expand Down Expand Up @@ -9629,12 +9777,11 @@ static int dcp_platform_suspend(struct device *dev)
return dcp_v27_sleep_suspend(dcp);

/*
* J813 uses the same qualified stop as DPMS off. Probe already gates
* its derived power layout on appledrm.s2r_untested; without this
* system-suspend path an active panel leaves the DCP running.
* The same stop as DPMS off; without this system-suspend path an active
* panel leaves the DCP running. Probe already gates a derived power
* layout (T8142) on appledrm.s2r_untested.
*/
if (pm_suspend_via_firmware() && dcp_s2r_capable(dcp) &&
(!dcp->hw.s2r_untested || of_machine_is_compatible("apple,j813"))) {
if (pm_suspend_via_firmware() && dcp_s2r_capable(dcp)) {
int ret = dcp_s2r_suspend(dcp);

/* Lit by the last resume with its CRTC off: the next resume finds it off by DPMS. */
Expand Down
23 changes: 23 additions & 0 deletions drivers/iommu/apple-dart.c
Original file line number Diff line number Diff line change
Expand Up @@ -7761,6 +7761,29 @@ int apple_dart_display_streams(struct device *dev, u32 streams, bool on, bool fl
}
EXPORT_SYMBOL_GPL(apple_dart_display_streams);

/*
* The streams a T8110 display DART has enabled, read while its domain is up: what a DCP
* whose device tree names no streams stops and enables again around its power cycle. The
* mask holds streams 0 to 31; a DART with any stream above them enabled can't be described
* by it and reads 0, which its caller takes as no streams to stop.
*/
u32 apple_dart_enabled_streams(struct device *dev)
{
struct apple_dart *dart = dev_get_drvdata(dev);
unsigned int i;

if (!dart || dart->hw->type != DART_T8110)
return 0;
for (i = 1; i < DIV_ROUND_UP(dart->num_streams, 32); i++) {
if (readl(dart->regs + DART_T8110_ENABLE_STREAMS + 4 * i)) {
dev_warn_once(dart->dev, "streams above 31 enabled: not reported\n");
return 0;
}
}
return readl(dart->regs + DART_T8110_ENABLE_STREAMS);
}
EXPORT_SYMBOL_GPL(apple_dart_enabled_streams);

/*
* A DART whose domain goes off with its consumer's, at runtime, while other users still map
* into it: dark, maps reach memory only and their invalidations wait; lit, those run. Its
Expand Down
7 changes: 7 additions & 0 deletions include/linux/soc/apple/dart.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ int apple_dart_sleep_prepare(struct device *dev);
void apple_dart_sleep_restore(struct device *dev);
/* A display DART's streams around a display power cycle, see the DART driver. */
int apple_dart_display_streams(struct device *dev, u32 streams, bool on, bool flush);
/* The streams 0 to 31 a display DART has enabled; 0 if any stream above 31 is on. */
u32 apple_dart_enabled_streams(struct device *dev);
/* A GAPF slice released before its coprocessor's CPU domain comes up after a wake. */
int apple_dart_gapf_ready(struct device *dev);
/* A display DART whose domain goes off at runtime while others still map into it. */
Expand Down Expand Up @@ -221,6 +223,11 @@ static inline int apple_dart_display_streams(struct device *dev, u32 streams, bo
return -ENODEV;
}

static inline u32 apple_dart_enabled_streams(struct device *dev)
{
return 0;
}

static inline int apple_dart_gapf_ready(struct device *dev)
{
return 0;
Expand Down