2014-06-25 16:19:15

by Tomasz Figa

[permalink] [raw]
Subject: [PATCH 0/2] Firmware-assisted suspend/resume of Exynos SoCs

On Exynos-based boards running secure firmware the sequence of low level
operations to enter and leave system-wide sleep mode is different than
on those without the firmware. Namely:
- CP15 power control and diagnostic registers cannot be written directly,
- the way of setting boot address and boot flag is different,
- different resume handler needs to be used (generic cpu_resume() vs
exynos_cpu_resume()),
- dedicated SMC call needs to be performed instead of letting the CPU enter
WFI.

This series introduces .suspend() and .resume() firmware operations to
perform low level firmware-specific suspend and resume and then leverages
them to provide suspend-resume path meeting the above requirements.

The series is based on Kgene's for-next branch and tested on
Exynos4412-based Trats2 board (with few board-specific fixes that will be
sent separately) and Exynos4210-based Trats board (without any extra
patches).

Tomasz Figa (2):
ARM: firmware: Introduce suspend and resume operations
ARM: EXYNOS: Add support for firmware-assisted suspend/resume

Documentation/arm/firmware.txt | 28 +++++-------------------
arch/arm/include/asm/firmware.h | 8 +++++++
arch/arm/mach-exynos/firmware.c | 47 +++++++++++++++++++++++++++++++++++++++++
arch/arm/mach-exynos/pm.c | 16 +++++++++-----
4 files changed, 71 insertions(+), 28 deletions(-)

--
1.9.3


2014-06-25 16:19:17

by Tomasz Figa

[permalink] [raw]
Subject: [PATCH 2/2] ARM: EXYNOS: Add support for firmware-assisted suspend/resume

On a numer of Exynos-based boards Linux kernel is running in non-secure
mode under a secure firmware. This means that certain operations need to
be handled in special way, with firmware assistance. System-wide
suspend/resume is an example of such operations.

This patch adds support for firmware-assisted suspend/resume by
leveraging recently introduced suspend and resume firmware operations
and modifying existing suspend/resume paths to account for presence of
secure firmware.

Signed-off-by: Tomasz Figa <[email protected]>
---
arch/arm/mach-exynos/firmware.c | 47 +++++++++++++++++++++++++++++++++++++++++
arch/arm/mach-exynos/pm.c | 16 +++++++++-----
2 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/arch/arm/mach-exynos/firmware.c b/arch/arm/mach-exynos/firmware.c
index def7bb4..cd554a1 100644
--- a/arch/arm/mach-exynos/firmware.c
+++ b/arch/arm/mach-exynos/firmware.c
@@ -14,15 +14,21 @@
#include <linux/of.h>
#include <linux/of_address.h>

+#include <asm/cacheflush.h>
#include <asm/cputype.h>
#include <asm/firmware.h>
#include <asm/hardware/cache-l2x0.h>
+#include <asm/suspend.h>

#include <mach/map.h>

#include "common.h"
#include "smc.h"

+#define EXYNOS_SLEEP_MAGIC 0x00000bad
+#define EXYNOS_BOOT_ADDR 0x8
+#define EXYNOS_BOOT_FLAG 0xc
+
static int exynos_do_idle(void)
{
exynos_smc(SMC_CMD_SLEEP, 0, 0, 0);
@@ -66,10 +72,51 @@ static int exynos_set_cpu_boot_addr(int cpu, unsigned long boot_addr)
return 0;
}

+/* For Cortex-A9 Diagnostic and Power control register */
+static unsigned int cp15_power;
+static unsigned int cp15_diag;
+
+static int exynos_cpu_suspend(unsigned long arg)
+{
+ flush_cache_all();
+ outer_flush_all();
+
+ exynos_smc(SMC_CMD_SLEEP, 0, 0, 0);
+
+ pr_info("Failed to suspend the system\n");
+ writel(0, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
+ return 1;
+}
+
+static int exynos_suspend(void)
+{
+ /* Save Power control and Diagnostic registers */
+ asm ("mrc p15, 0, %0, c15, c0, 0\n"
+ "mrc p15, 0, %1, c15, c0, 1\n"
+ : "=r" (cp15_power), "=r" (cp15_diag) : : "cc");
+
+ writel(EXYNOS_SLEEP_MAGIC, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
+ writel(virt_to_phys(cpu_resume),
+ sysram_ns_base_addr + EXYNOS_BOOT_ADDR);
+
+ return cpu_suspend(0, exynos_cpu_suspend);
+}
+
+static int exynos_resume(void)
+{
+ exynos_smc(SMC_CMD_C15RESUME, cp15_power, cp15_diag, 0);
+ writel(0, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
+ outer_resume();
+
+ return 0;
+}
+
static const struct firmware_ops exynos_firmware_ops = {
.do_idle = exynos_do_idle,
.set_cpu_boot_addr = exynos_set_cpu_boot_addr,
.cpu_boot = exynos_cpu_boot,
+ .suspend = exynos_suspend,
+ .resume = exynos_resume,
};

static void exynos_l2_write_sec(unsigned long val, void __iomem *base,
diff --git a/arch/arm/mach-exynos/pm.c b/arch/arm/mach-exynos/pm.c
index f23cc77..ed20318 100644
--- a/arch/arm/mach-exynos/pm.c
+++ b/arch/arm/mach-exynos/pm.c
@@ -23,6 +23,7 @@
#include <linux/clk.h>

#include <asm/cacheflush.h>
+#include <asm/firmware.h>
#include <asm/hardware/cache-l2x0.h>
#include <asm/smp_scu.h>
#include <asm/suspend.h>
@@ -331,12 +332,11 @@ static int exynos_pm_central_resume(void)

static void exynos_pm_resume(void)
{
+ u32 cpuid = read_cpuid_part_number();
+
if (exynos_pm_central_resume())
goto early_wakeup;

- if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A9)
- exynos_cpu_restore_register();
-
/* For release retention */

__raw_writel((1 << 28), S5P_PAD_RET_MAUDIO_OPTION);
@@ -353,9 +353,13 @@ static void exynos_pm_resume(void)

s3c_pm_do_restore_core(exynos_core_save, ARRAY_SIZE(exynos_core_save));

- if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A9)
+ if (cpuid == ARM_CPU_PART_CORTEX_A9)
scu_enable(S5P_VA_SCU);

+ if (call_firmware_op(resume) == -ENOSYS
+ && cpuid == ARM_CPU_PART_CORTEX_A9)
+ exynos_cpu_restore_register();
+
early_wakeup:

/* Clear SLEEP mode set in INFORM1 */
@@ -391,7 +395,9 @@ static int exynos_suspend_enter(suspend_state_t state)
flush_cache_all();
s3c_pm_check_store();

- ret = cpu_suspend(0, exynos_cpu_suspend);
+ ret = call_firmware_op(suspend);
+ if (ret == -ENOSYS)
+ ret = cpu_suspend(0, exynos_cpu_suspend);
if (ret)
return ret;

--
1.9.3

2014-06-25 16:19:33

by Tomasz Figa

[permalink] [raw]
Subject: [PATCH 1/2] ARM: firmware: Introduce suspend and resume operations

This patch extends the firmware_ops structure with two new callbacks:
.suspend() and .resume(). The former is intended to ask the firmware to
save all its volatile state and suspend the system, without returning
back to the kernel in between. The latter is to be called early by
very low level platform suspend code after waking up to restore low
level hardware state, which can't be restored in non-secure mode.

While at it, outdated version of the structure is removed from the
documentation and replaced with a reference to the header file.

Signed-off-by: Tomasz Figa <[email protected]>
---
Documentation/arm/firmware.txt | 28 +++++-----------------------
arch/arm/include/asm/firmware.h | 8 ++++++++
2 files changed, 13 insertions(+), 23 deletions(-)

diff --git a/Documentation/arm/firmware.txt b/Documentation/arm/firmware.txt
index c2e468f..da6713a 100644
--- a/Documentation/arm/firmware.txt
+++ b/Documentation/arm/firmware.txt
@@ -7,32 +7,14 @@ world, which changes the way some things have to be initialized. This makes
a need to provide an interface for such platforms to specify available firmware
operations and call them when needed.

-Firmware operations can be specified using struct firmware_ops
-
- struct firmware_ops {
- /*
- * Enters CPU idle mode
- */
- int (*do_idle)(void);
- /*
- * Sets boot address of specified physical CPU
- */
- int (*set_cpu_boot_addr)(int cpu, unsigned long boot_addr);
- /*
- * Boots specified physical CPU
- */
- int (*cpu_boot)(int cpu);
- /*
- * Initializes L2 cache
- */
- int (*l2x0_init)(void);
- };
-
-and then registered with register_firmware_ops function
+Firmware operations can be specified by filling in a struct firmware_ops
+with appropriate callbacks and then registering it with register_firmware_ops()
+function.

void register_firmware_ops(const struct firmware_ops *ops)

-the ops pointer must be non-NULL.
+The ops pointer must be non-NULL. More information about struct firmware_ops
+and its members can be found in arch/arm/include/asm/firmware.h header.

There is a default, empty set of operations provided, so there is no need to
set anything if platform does not require firmware operations.
diff --git a/arch/arm/include/asm/firmware.h b/arch/arm/include/asm/firmware.h
index 2c9f10d..5904f59 100644
--- a/arch/arm/include/asm/firmware.h
+++ b/arch/arm/include/asm/firmware.h
@@ -41,6 +41,14 @@ struct firmware_ops {
* Initializes L2 cache
*/
int (*l2x0_init)(void);
+ /*
+ * Enter system-wide suspend.
+ */
+ int (*suspend)(void);
+ /*
+ * Restore state of privileged hardware after system-wide suspend.
+ */
+ int (*resume)(void);
};

/* Global pointer for current firmware_ops structure, can't be NULL. */
--
1.9.3

2014-06-25 22:54:57

by Russell King - ARM Linux

[permalink] [raw]
Subject: Re: [PATCH 2/2] ARM: EXYNOS: Add support for firmware-assisted suspend/resume

On Wed, Jun 25, 2014 at 06:18:34PM +0200, Tomasz Figa wrote:
> +static int exynos_suspend(void)
> +{
> + /* Save Power control and Diagnostic registers */
> + asm ("mrc p15, 0, %0, c15, c0, 0\n"
> + "mrc p15, 0, %1, c15, c0, 1\n"
> + : "=r" (cp15_power), "=r" (cp15_diag) : : "cc");
> +
> + writel(EXYNOS_SLEEP_MAGIC, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
> + writel(virt_to_phys(cpu_resume),
> + sysram_ns_base_addr + EXYNOS_BOOT_ADDR);
> +
> + return cpu_suspend(0, exynos_cpu_suspend);
> +}
> +
> +static int exynos_resume(void)
> +{
> + exynos_smc(SMC_CMD_C15RESUME, cp15_power, cp15_diag, 0);

I am told that these two registers are not expected to change value
once the MMU is on. This presents something of a problem where the
secure monitor is involved, because what that means is that this
really needs to be done before we get to C code.

OMAP has similar issues where it needs to restore the L2 cache
setup via SMC calls before the MMU is enabled, and they deal with
this via some hand-crafted assembly code which runs prior to calling
into cpu_resume.

--
FTTC broadband for 0.8mile line: now at 9.7Mbps down 460kbps up... slowly
improving, and getting towards what was expected from it.

2014-06-26 02:33:36

by Alexandre Courbot

[permalink] [raw]
Subject: Re: [PATCH 1/2] ARM: firmware: Introduce suspend and resume operations

On 06/26/2014 01:18 AM, Tomasz Figa wrote:
> This patch extends the firmware_ops structure with two new callbacks:
> .suspend() and .resume(). The former is intended to ask the firmware to
> save all its volatile state and suspend the system, without returning
> back to the kernel in between. The latter is to be called early by
> very low level platform suspend code after waking up to restore low
> level hardware state, which can't be restored in non-secure mode.
>
> While at it, outdated version of the structure is removed from the
> documentation and replaced with a reference to the header file.

Acked-by: Alexandre Courbot <[email protected]>

2014-06-26 11:33:10

by Daniel Drake

[permalink] [raw]
Subject: Re: [PATCH 2/2] ARM: EXYNOS: Add support for firmware-assisted suspend/resume

Hi Tomasz,

On Wed, Jun 25, 2014 at 5:18 PM, Tomasz Figa <[email protected]> wrote:
> On a numer of Exynos-based boards Linux kernel is running in non-secure
> mode under a secure firmware. This means that certain operations need to
> be handled in special way, with firmware assistance. System-wide
> suspend/resume is an example of such operations.

Thanks for working on this. Tested on ODROID-U2, it almost works! Now
instead of resetting upon resume, the system does go back into Linux.

However things go wrong at that point - maybe some memory corruption?
A memory allocation error from the USB stack, and then an unhandled
abort about 5 seconds which usually kills my bash process.

I'll try to find some time to investigate a bit more. In the mean
time, the crash log is below.
I'm testing with "rtcwake -m mem -s 10". That's using the internal RTC
with the max77686 RTC driver not built to avoid confusion. Also the
max77686 clk driver must be disabled otherwise it disables the
upstream clock source of the exynos's RTC.

Enabling non-boot CPUs ...
CPU1: Booted secondary processor
CPU1 is up
CPU2: Booted secondary processor
CPU2 is up
CPU3: Booted secondary processor
CPU3 is up
PM: noirq resume of devices complete after 1.460 msecs
PM: early resume of devices complete after 1.603 msecs
s3c-rtc 10070000.rtc: rtc disabled, re-enabling
usb usb1: root hub lost power or was reset
s3c-i2c 13860000.i2c: slave address 0x00
s3c-i2c 13860000.i2c: bus frequency set to 214 KHz
s3c-i2c 13870000.i2c: slave address 0x00
s3c-i2c 13870000.i2c: bus frequency set to 71 KHz
usb 1-2: reset high-speed USB device number 2 using exynos-ehci
swapper/0: page allocation failure: order:0, mode:0x284000
CPU: 0 PID: 0 Comm: swapper/0 Not tainted 3.16.0-rc2-00034-g8f86926 #118
[<c0016140>] (unwind_backtrace) from [<c0011e14>] (show_stack+0x10/0x14)
[<c0011e14>] (show_stack) from [<c05b27f4>] (dump_stack+0x84/0xc4)
[<c05b27f4>] (dump_stack) from [<c00b5398>] (warn_alloc_failed+0x108/0x128)
[<c00b5398>] (warn_alloc_failed) from [<c00b8858>]
(__alloc_pages_nodemask+0x924/0xa3c)
[<c00b8858>] (__alloc_pages_nodemask) from [<c00ea824>] (new_slab+0xd8/0x340)
[<c00ea824>] (new_slab) from [<c05b0abc>]
(__slab_alloc.isra.53.constprop.58+0x280/0x3b0)
[<c05b0abc>] (__slab_alloc.isra.53.constprop.58) from [<c00ec7dc>]
(kmem_cache_alloc+0x118/0x204)
[<c00ec7dc>] (kmem_cache_alloc) from [<c02ba680>]
(radix_tree_node_alloc+0x8c/0x98)
[<c02ba680>] (radix_tree_node_alloc) from [<c02baed4>]
(__radix_tree_create+0x128/0x1c4)
[<c02baed4>] (__radix_tree_create) from [<c02baf9c>]
(radix_tree_insert+0x2c/0xd4)
[<c02baf9c>] (radix_tree_insert) from [<c02db86c>] (add_dma_entry+0x84/0x144)
[<c02db86c>] (add_dma_entry) from [<c03ccd08>]
(usb_hcd_map_urb_for_dma+0x450/0x540)
[<c03ccd08>] (usb_hcd_map_urb_for_dma) from [<c03cd660>]
(usb_hcd_submit_urb+0x6cc/0x7f8)
[<c03cd660>] (usb_hcd_submit_urb) from [<c03c1300>] (rx_submit+0xe4/0x210)
[<c03c1300>] (rx_submit) from [<c03c1630>] (rx_complete+0x19c/0x20c)
[<c03c1630>] (rx_complete) from [<c03cbd48>] (__usb_hcd_giveback_urb+0x6c/0xd0)
[<c03cbd48>] (__usb_hcd_giveback_urb) from [<c03cc88c>]
(usb_giveback_urb_bh+0xa0/0xcc)
[<c03cc88c>] (usb_giveback_urb_bh) from [<c00289e8>] (tasklet_action+0xcc/0x14c)
[<c00289e8>] (tasklet_action) from [<c0028b70>] (__do_softirq+0x108/0x24c)
[<c0028b70>] (__do_softirq) from [<c0028f20>] (irq_exit+0x98/0xe4)
[<c0028f20>] (irq_exit) from [<c000f31c>] (handle_IRQ+0xa0/0xc4)
[<c000f31c>] (handle_IRQ) from [<c0008628>] (gic_handle_irq+0x4c/0x68)
[<c0008628>] (gic_handle_irq) from [<c00129c0>] (__irq_svc+0x40/0x70)
Exception stack(0xc07f9f58 to 0xc07f9fa0)
9f40: ffffffed 00000000
9f60: ffffffed 00000000 c080048c c05bba78 00000000 00000000 c07f4f10 c08401aa
9f80: c07f8000 00000000 00000001 c07f9fa0 c000f5c4 c000f5c8 60000153 ffffffff
[<c00129c0>] (__irq_svc) from [<c000f5c8>] (arch_cpu_idle+0x28/0x2c)
[<c000f5c8>] (arch_cpu_idle) from [<c005db24>] (cpu_startup_entry+0x138/0x238)
[<c005db24>] (cpu_startup_entry) from [<c07abbd4>] (start_kernel+0x3ac/0x3b8)
Mem-info:
Normal per-cpu:
CPU 0: hi: 186, btch: 31 usd: 94
CPU 1: hi: 186, btch: 31 usd: 30
CPU 2: hi: 186, btch: 31 usd: 0
CPU 3: hi: 186, btch: 31 usd: 0
HighMem per-cpu:
CPU 0: hi: 186, btch: 31 usd: 98
CPU 1: hi: 186, btch: 31 usd: 0
CPU 2: hi: 186, btch: 31 usd: 0
CPU 3: hi: 186, btch: 31 usd: 0
active_anon:4564 inactive_anon:267 isolated_anon:0
active_file:9994 inactive_file:5258 isolated_file:0
unevictable:0 dirty:0 writeback:0 unstable:0
free:347626 slab_reclaimable:4068 slab_unreclaimable:5438
mapped:4418 shmem:913 pagetables:174 bounce:0
free_cma:32441
Normal free:131792kB min:3444kB low:4304kB high:5164kB
active_anon:4372kB inactive_anon:272kB active_file:33896kB
inactive_file:4512kB unevictable:0kB isolated(anon):0kB
isolated(file):0kB present:778240kB managed:743296kB mlocked:0kB
dirty:0kB writeback:0kB mapped:5208kB shmem:908kB
slab_reclaimable:16272kB slab_unreclaimable:21752kB kernel_stack:928kB
pagetables:696kB unstable:0kB bounce:0kB free_cma:129764kB
writeback_tmp:0kB pages_scanned:0 all_unreclaimable? no
lowmem_reserve[]: 0 10296 10296
HighMem free:1258712kB min:512kB low:2036kB high:3564kB
active_anon:13884kB inactive_anon:796kB active_file:6080kB
inactive_file:16520kB unevictable:0kB isolated(anon):0kB
isolated(file):0kB present:1317888kB managed:1317888kB mlocked:0kB
dirty:0kB writeback:20kB mapped:12464kB shmem:2744kB
slab_reclaimable:0kB slab_unreclaimable:0kB kernel_stack:0kB
pagetables:0kB unstable:0kB bounce:0kB free_cma:0kB writeback_tmp:0kB
pages_scanned:0 all_unreclaimable? no
lowmem_reserve[]: 0 0 0
Normal: 68*4kB (UEMC) 15*8kB (UEM) 6*16kB (EM) 2*32kB (RC) 2*64kB (RC)
1*128kB (C) 1*256kB (R) 1*512kB (C) 1*1024kB (R) 1*2048kB (C)
31*4096kB (C) = 131624kB
HighMem: 173*4kB (UM) 52*8kB (UM) 21*16kB (UM) 14*32kB (UM) 11*64kB
(UM) 6*128kB (UM) 6*256kB (U) 3*512kB (UM) 3*1024kB (U) 0*2048kB
305*4096kB (UMR) = 1258788kB
16166 total pagecache pages
0 pages in swap cache
Swap cache stats: add 0, delete 0, find 0/0
Free swap = 0kB
Total swap = 0kB
524032 pages of RAM
348040 free pages
8736 reserved pages
6850 slab pages
27050 pages shared
0 pages swap cached
SLUB: Unable to allocate memory on node -1 (gfp=0x0)
cache: radix_tree_node, object size: 304, buffer size: 352, default
order: 1, min order: 0
node 0: slabs: 820, objs: 18860, free: 0
DMA-API: cacheline tracking ENOMEM, dma-debug disabled
smsc95xx 1-2:1.0 eth0: kevent 2 may have been dropped
smsc95xx 1-2:1.0 eth0: kevent 2 may have been dropped
smsc95xx 1-2:1.0 eth0: kevent 2 may have been dropped
[sched_delayed] sched: RT throttling activated
PM: resume of devices complete after 3978.631 msecs
PM: resume devices took 3.980 seconds
PM: Finishing wakeup.
Restarting tasks ...
usb 1-3: USB disconnect, device number 3
done.
Unhandled fault: imprecise external abort (0xc06) at 0xb6f28000