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

2014-06-26 Thread Daniel Drake
Hi Tomasz,

On Wed, Jun 25, 2014 at 5:18 PM, Tomasz Figa t.f...@samsung.com 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 1007.rtc: rtc disabled, re-enabling
usb usb1: root hub lost power or was reset
s3c-i2c 1386.i2c: slave address 0x00
s3c-i2c 1386.i2c: bus frequency set to 214 KHz
s3c-i2c 1387.i2c: slave address 0x00
s3c-i2c 1387.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:   ffed 
9f60: ffed  c080048c c05bba78   c07f4f10 c08401aa
9f80: c07f8000  0001 c07f9fa0 c000f5c4 c000f5c8 6153 
[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:
CPU0: hi:  186, btch:  31 usd:  94
CPU1: hi:  186, btch:  31 usd:  30
CPU2: hi:  186, btch:  31 usd:   0
CPU3: hi:  186, btch:  31 usd:   0
HighMem per-cpu:
CPU0: hi:  186, btch:  31 usd:  98
CPU1: hi:  186, btch:  31 usd:   0
CPU2: hi:  186, btch:  31 usd:   0
CPU3: 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 

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

2014-06-25 Thread Tomasz Figa
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 t.f...@samsung.com
---
 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 0x0bad
+#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

--
To unsubscribe from this list: send the line unsubscribe linux-samsung-soc in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


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

2014-06-25 Thread Russell King - ARM Linux
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.
--
To unsubscribe from this list: send the line unsubscribe linux-samsung-soc in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html