From patchwork Tue Mar 18 21:40:58 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sebastian Capella X-Patchwork-Id: 26524 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-vc0-f199.google.com (mail-vc0-f199.google.com [209.85.220.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 0CFF8202FA for ; Tue, 18 Mar 2014 21:41:45 +0000 (UTC) Received: by mail-vc0-f199.google.com with SMTP id lh14sf18495026vcb.6 for ; Tue, 18 Mar 2014 14:41:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe:content-type :content-transfer-encoding; bh=fDw5Jwr31rMoEx7Zejd+lUMZFN7AD0j/zfWHk6MHEOw=; b=QGOmHXwVm8FQHH6Y0ZuopPuA4ibF2zD8tjt4pHuEKJwjEO3uYNKOJI0cwSdSnxJolc 1bQXqiXPiSN3riSxlJ7nuUXTY6SoiR59zAPQ4/UWK4xAkd0+bh/VhaITSWgGO5ZNPoGA liQwesJNejPPWGw/nyZQDvRWi2hH1Ze5ult8ZDm9TTeGnsBtxqcX/iAbHcwb7i4FjGTq TCRNha0X5CnSYfC1KZRJYJDhl07eS8kA4QjHjogzQz3pE/ea/t+oetod4eq0Z7tQFyGR wiZ/CiYD5D/ARFNkXVs8tPKGnYT/LHDgnzZJ5sV+rDgpSFOWAMtEUxn6RdFJwTYPolqa tIVg== X-Gm-Message-State: ALoCoQl88KN0/jhQ44opkuTp3qPT5spJgh9o49INL8fldr802ssvfkGvvfEsttiExeALIZC+r0cH X-Received: by 10.224.113.202 with SMTP id b10mr12869020qaq.3.1395178905790; Tue, 18 Mar 2014 14:41:45 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.102.1 with SMTP id v1ls2237583qge.50.gmail; Tue, 18 Mar 2014 14:41:45 -0700 (PDT) X-Received: by 10.58.55.170 with SMTP id t10mr5638193vep.29.1395178905647; Tue, 18 Mar 2014 14:41:45 -0700 (PDT) Received: from mail-vc0-f176.google.com (mail-vc0-f176.google.com [209.85.220.176]) by mx.google.com with ESMTPS id vr5si784340vec.39.2014.03.18.14.41.45 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 18 Mar 2014 14:41:45 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.176 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.176; Received: by mail-vc0-f176.google.com with SMTP id lc6so8133681vcb.35 for ; Tue, 18 Mar 2014 14:41:45 -0700 (PDT) X-Received: by 10.58.207.74 with SMTP id lu10mr26316003vec.15.1395178905521; Tue, 18 Mar 2014 14:41:45 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.78.9 with SMTP id i9csp248073vck; Tue, 18 Mar 2014 14:41:44 -0700 (PDT) X-Received: by 10.68.254.5 with SMTP id ae5mr35952089pbd.83.1395178903295; Tue, 18 Mar 2014 14:41:43 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id qe9si19290886pbb.102.2014.03.18.14.41.42; Tue, 18 Mar 2014 14:41:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933261AbaCRVlf (ORCPT + 26 others); Tue, 18 Mar 2014 17:41:35 -0400 Received: from mail-pb0-f47.google.com ([209.85.160.47]:56203 "EHLO mail-pb0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757619AbaCRVlI (ORCPT ); Tue, 18 Mar 2014 17:41:08 -0400 Received: by mail-pb0-f47.google.com with SMTP id up15so7885919pbc.34 for ; Tue, 18 Mar 2014 14:41:08 -0700 (PDT) X-Received: by 10.66.189.226 with SMTP id gl2mr36139748pac.65.1395178868375; Tue, 18 Mar 2014 14:41:08 -0700 (PDT) Received: from localhost (cpe-76-93-135-111.san.res.rr.com. [76.93.135.111]) by mx.google.com with ESMTPSA id fk4sm93181277pab.23.2014.03.18.14.41.04 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 18 Mar 2014 14:41:06 -0700 (PDT) From: Sebastian Capella To: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, linaro-kernel@lists.linaro.org, linux-arm-kernel@lists.infradead.org Cc: Russ Dill , "Rafael J. Wysocki" , Sebastian Capella , Russell King , Len Brown , Nicolas Pitre , Santosh Shilimkar , Will Deacon , Jonathan Austin , Catalin Marinas , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Stephen Boyd , Victor Kamensky , Sricharan R , Jiang Liu , Laura Abbott Subject: [PATCH v9 2/2] ARM hibernation / suspend-to-disk Date: Tue, 18 Mar 2014 14:40:58 -0700 Message-Id: <1395178858-27343-3-git-send-email-sebastian.capella@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1395178858-27343-1-git-send-email-sebastian.capella@linaro.org> References: <1395178858-27343-1-git-send-email-sebastian.capella@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: sebastian.capella@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.220.176 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Russ Dill Enable hibernation for ARM architectures and provide ARM architecture specific calls used during hibernation. The swsusp hibernation framework depends on the platform first having functional suspend/resume. Then, in order to enable hibernation on a given platform, a platform_hibernation_ops structure may need to be registered with the system in order to save/restore any SoC-specific / cpu specific state needing (re)init over a suspend-to-disk/resume-from-disk cycle. For example: - "secure" SoCs that have different sets of control registers and/or different CR reg access patterns. - SoCs with L2 caches as the activation sequence there is SoC-dependent; a full off-on cycle for L2 is not done by the hibernation support code. - SoCs requiring steps on wakeup _before_ the "generic" parts done by cpu_suspend / cpu_resume can work correctly. - SoCs having persistent state which is maintained during suspend and resume, but will be lost during the power off cycle after suspend-to-disk. This is a rebase/rework of Frank Hofmann's v5 hibernation patchset. Acked-by: Russ Dill Cc: "Rafael J. Wysocki" Signed-off-by: Sebastian Capella Acked-by: Pavel Machek Reviewed-by: Lorenzo Pieralisi Cc: Russell King Cc: Len Brown Cc: Nicolas Pitre Cc: Santosh Shilimkar Cc: Will Deacon Cc: Jonathan Austin Cc: Catalin Marinas Cc: "Uwe Kleine-König" Cc: Stephen Boyd Cc: Stefano Stabellini Cc: Sricharan R Cc: Jiang Liu Cc: Laura Abbott --- arch/arm/Kconfig | 5 ++ arch/arm/include/asm/memory.h | 1 + arch/arm/kernel/Makefile | 1 + arch/arm/kernel/hibernate.c | 107 +++++++++++++++++++++++++++++++++++++++++ include/linux/suspend.h | 2 + 5 files changed, 116 insertions(+) create mode 100644 arch/arm/kernel/hibernate.c diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index 1594945..d204231 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -2279,6 +2279,11 @@ config ARCH_SUSPEND_POSSIBLE config ARM_CPU_SUSPEND def_bool PM_SLEEP +config ARCH_HIBERNATION_POSSIBLE + bool + depends on MMU + default y if ARCH_SUSPEND_POSSIBLE + endmenu source "net/Kconfig" diff --git a/arch/arm/include/asm/memory.h b/arch/arm/include/asm/memory.h index 4afb376..47347dd 100644 --- a/arch/arm/include/asm/memory.h +++ b/arch/arm/include/asm/memory.h @@ -289,6 +289,7 @@ static inline void *phys_to_virt(phys_addr_t x) #define __pa(x) __virt_to_phys((unsigned long)(x)) #define __va(x) ((void *)__phys_to_virt((phys_addr_t)(x))) #define pfn_to_kaddr(pfn) __va((pfn) << PAGE_SHIFT) +#define virt_to_pfn(kaddr) (__pa(kaddr) >> PAGE_SHIFT) extern phys_addr_t (*arch_virt_to_idmap)(unsigned long x); diff --git a/arch/arm/kernel/Makefile b/arch/arm/kernel/Makefile index a30fc9b..8afa848 100644 --- a/arch/arm/kernel/Makefile +++ b/arch/arm/kernel/Makefile @@ -39,6 +39,7 @@ obj-$(CONFIG_ARTHUR) += arthur.o obj-$(CONFIG_ISA_DMA) += dma-isa.o obj-$(CONFIG_PCI) += bios32.o isa.o obj-$(CONFIG_ARM_CPU_SUSPEND) += sleep.o suspend.o +obj-$(CONFIG_HIBERNATION) += hibernate.o obj-$(CONFIG_SMP) += smp.o ifdef CONFIG_MMU obj-$(CONFIG_SMP) += smp_tlb.o diff --git a/arch/arm/kernel/hibernate.c b/arch/arm/kernel/hibernate.c new file mode 100644 index 0000000..bb8b796 --- /dev/null +++ b/arch/arm/kernel/hibernate.c @@ -0,0 +1,107 @@ +/* + * Hibernation support specific for ARM + * + * Derived from work on ARM hibernation support by: + * + * Ubuntu project, hibernation support for mach-dove + * Copyright (C) 2010 Nokia Corporation (Hiroshi Doyu) + * Copyright (C) 2010 Texas Instruments, Inc. (Teerth Reddy et al.) + * https://lkml.org/lkml/2010/6/18/4 + * https://lists.linux-foundation.org/pipermail/linux-pm/2010-June/027422.html + * https://patchwork.kernel.org/patch/96442/ + * + * Copyright (C) 2006 Rafael J. Wysocki + * + * License terms: GNU General Public License (GPL) version 2 + */ + +#include +#include +#include +#include +#include +#include + +extern const void __nosave_begin, __nosave_end; + +int pfn_is_nosave(unsigned long pfn) +{ + unsigned long nosave_begin_pfn = virt_to_pfn(&__nosave_begin); + unsigned long nosave_end_pfn = virt_to_pfn(&__nosave_end - 1); + + return (pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn); +} + +void notrace save_processor_state(void) +{ + WARN_ON(num_online_cpus() != 1); + local_fiq_disable(); +} + +void notrace restore_processor_state(void) +{ + local_fiq_enable(); +} + +/* + * Snapshot kernel memory and reset the system. + * + * swsusp_save() is executed in the suspend finisher so that the CPU + * context pointer and memory are part of the saved image, which is + * required by the resume kernel image to restart execution from + * swsusp_arch_suspend(). + * + * soft_restart is not technically needed, but is used to get success + * returned from cpu_suspend. + * + * When soft reboot completes, the hibernation snapshot is written out. + */ +static int notrace arch_save_image(unsigned long unused) +{ + int ret; + + ret = swsusp_save(); + if (ret == 0) + soft_restart(virt_to_phys(cpu_resume)); + return ret; +} + +/* + * Save the current CPU state before suspend / poweroff. + */ +int notrace swsusp_arch_suspend(void) +{ + return cpu_suspend(0, arch_save_image); +} + +/* + * Restore page contents for physical pages that were in use during loading + * hibernation image. Switch to idmap_pgd so the physical page tables + * are overwritten with the same contents. + */ +static void notrace arch_restore_image(void *unused) +{ + struct pbe *pbe; + + cpu_switch_mm(idmap_pgd, &init_mm); + for (pbe = restore_pblist; pbe; pbe = pbe->next) + copy_page(pbe->orig_address, pbe->address); + + soft_restart(virt_to_phys(cpu_resume)); +} + +static u64 resume_stack[PAGE_SIZE/2/sizeof(u64)] __nosavedata; + +/* + * Resume from the hibernation image. + * Due to the kernel heap / data restore, stack contents change underneath + * and that would make function calls impossible; switch to a temporary + * stack within the nosave region to avoid that problem. + */ +int swsusp_arch_resume(void) +{ + extern void call_with_stack(void (*fn)(void *), void *arg, void *sp); + call_with_stack(arch_restore_image, 0, + resume_stack + ARRAY_SIZE(resume_stack)); + return 0; +} diff --git a/include/linux/suspend.h b/include/linux/suspend.h index f73cabf..38bbf95 100644 --- a/include/linux/suspend.h +++ b/include/linux/suspend.h @@ -320,6 +320,8 @@ extern unsigned long get_safe_page(gfp_t gfp_mask); extern void hibernation_set_ops(const struct platform_hibernation_ops *ops); extern int hibernate(void); extern bool system_entering_hibernation(void); +asmlinkage int swsusp_save(void); +extern struct pbe *restore_pblist; #else /* CONFIG_HIBERNATION */ static inline void register_nosave_region(unsigned long b, unsigned long e) {} static inline void register_nosave_region_late(unsigned long b, unsigned long e) {}