From patchwork Tue Jan 24 04:37:28 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob X-Patchwork-Id: 6357 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id EFACB23E0E for ; Tue, 24 Jan 2012 04:37:44 +0000 (UTC) Received: from mail-bk0-f52.google.com (mail-bk0-f52.google.com [209.85.214.52]) by fiordland.canonical.com (Postfix) with ESMTP id C46B6A180D2 for ; Tue, 24 Jan 2012 04:37:44 +0000 (UTC) Received: by bkar19 with SMTP id r19so3637941bka.11 for ; Mon, 23 Jan 2012 20:37:44 -0800 (PST) Received: by 10.204.10.82 with SMTP id o18mr4338439bko.20.1327379864457; Mon, 23 Jan 2012 20:37:44 -0800 (PST) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.204.130.220 with SMTP id u28cs90830bks; Mon, 23 Jan 2012 20:37:44 -0800 (PST) Received: by 10.236.154.137 with SMTP id h9mr13730285yhk.91.1327379862877; Mon, 23 Jan 2012 20:37:42 -0800 (PST) Received: from mail-yx0-f178.google.com (mail-yx0-f178.google.com [209.85.213.178]) by mx.google.com with ESMTPS id n61si15163405yhk.119.2012.01.23.20.37.41 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 23 Jan 2012 20:37:42 -0800 (PST) Received-SPF: neutral (google.com: 209.85.213.178 is neither permitted nor denied by best guess record for domain of rob.lee@linaro.org) client-ip=209.85.213.178; Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.213.178 is neither permitted nor denied by best guess record for domain of rob.lee@linaro.org) smtp.mail=rob.lee@linaro.org Received: by yenm12 with SMTP id m12so1774605yen.37 for ; Mon, 23 Jan 2012 20:37:41 -0800 (PST) MIME-Version: 1.0 Received: by 10.236.131.38 with SMTP id l26mr15639303yhi.70.1327379859770; Mon, 23 Jan 2012 20:37:39 -0800 (PST) Received: from b18647-20 ([23.19.172.97]) by mx.google.com with ESMTPS id h29sm14021831ann.16.2012.01.23.20.37.37 (version=SSLv3 cipher=OTHER); Mon, 23 Jan 2012 20:37:39 -0800 (PST) From: Robert Lee To: len.brown@intel.com, linux-pm@vger.kernel.org, s.hauer@pengutronix.de, amit.kucheria@linaro.org Cc: linux@arm.linux.org.uk, shawn.guo@freescale.com, nicolas.ferre@atmel.com, linux@maxim.org.za, kgene.kim@samsung.com, amit.kachhap@linaro.org, magnus.damm@gmail.com, khilman@ti.com, nsekhar@ti.com, daniel.lezcano@linaro.org, mturquette@linaro.org, vincent.guittot@linaro.org, arnd.bergmann@linaro.org, linux-arm-kernel@lists.infradead.org, patches@linaro.org, deepthi@linux.vnet.ibm.com, broonie@opensource.wolfsonmicro.com, nicolas.pitre@linaro.org, robherring2@gmail.com Subject: [PATCH v3 1/7] cpuidle: Add common init interface and idle functionality Date: Mon, 23 Jan 2012 22:37:28 -0600 Message-Id: <1327379854-12403-2-git-send-email-rob.lee@linaro.org> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1327379854-12403-1-git-send-email-rob.lee@linaro.org> References: <1327379854-12403-1-git-send-email-rob.lee@linaro.org> X-Gm-Message-State: ALoCoQmM9JpxGOyZJ9AnHhxpMAya1lIuC8+PbxyUqNM7RjjXu2fp5GWCBvBW+XfChjhiYAYfesc3 The patch adds some cpuidle initialization functionality commonly duplicated by many platforms. Signed-off-by: Robert Lee Reviewed-by: Rob Herring --- drivers/cpuidle/Makefile | 2 +- drivers/cpuidle/common.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/cpuidle.h | 24 +++++++ 3 files changed, 177 insertions(+), 1 deletions(-) create mode 100644 drivers/cpuidle/common.c diff --git a/drivers/cpuidle/Makefile b/drivers/cpuidle/Makefile index 5634f88..2928d93 100644 --- a/drivers/cpuidle/Makefile +++ b/drivers/cpuidle/Makefile @@ -2,4 +2,4 @@ # Makefile for cpuidle. # -obj-y += cpuidle.o driver.o governor.o sysfs.o governors/ +obj-y += cpuidle.o driver.o governor.o sysfs.o common.o governors/ diff --git a/drivers/cpuidle/common.c b/drivers/cpuidle/common.c new file mode 100644 index 0000000..dafa758 --- /dev/null +++ b/drivers/cpuidle/common.c @@ -0,0 +1,152 @@ +/* + * Copyright 2011 Freescale Semiconductor, Inc. + * Copyright 2011 Linaro Ltd. + * + * The code contained herein is licensed under the GNU General Public + * License. You may obtain a copy of the GNU General Public License + * Version 2 or later at the following locations: + * + * http://www.opensource.org/licenses/gpl-license.html + * http://www.gnu.org/copyleft/gpl.html + */ + +/* + * This code performs provides some commonly used cpuidle setup functionality + * used by many ARM SoC platforms. Providing this functionality here + * reduces the duplication of this code for each ARM platform that uses it. + */ + +#include +#include +#include +#include +#include +#include +#include + +static struct cpuidle_device __percpu * common_cpuidle_devices; + +static int (*do_idle[CPUIDLE_STATE_MAX])(struct cpuidle_device *dev, + struct cpuidle_driver *drv, int index); + +int cpuidle_def_idle(struct cpuidle_device *dev, + struct cpuidle_driver *drv, int index) +{ + cpu_do_idle(); + return index; +} + +static int simple_enter(struct cpuidle_device *dev, + struct cpuidle_driver *drv, int index) +{ + ktime_t time_start, time_end; + + local_irq_disable(); + + time_start = ktime_get(); + + index = do_idle[index](dev, drv, index); + + time_end = ktime_get(); + + local_irq_enable(); + + dev->last_residency = + (int)ktime_to_us(ktime_sub(time_end, time_start)); + + return index; +} + +void common_cpuidle_devices_uninit(void) +{ + int cpu_id; + struct cpuidle_device *dev; + + for_each_possible_cpu(cpu_id) { + dev = per_cpu_ptr(common_cpuidle_devices, cpu_id); + cpuidle_unregister_device(dev); + } + + free_percpu(common_cpuidle_devices); +} + +/** + * common_cpuidle_init() - Provides some commonly used init functionality. + * @pdrv Pointer to your cpuidle_driver object. + * @simple Use the common simple_enter wrapper? + * @driver_data_init Pointer to your platform function to initialize your + * platform specific driver data. Use NULL if platform + * specific data is not needed. + * + * Common cpuidle init interface to provide common cpuidle functionality + * used by many platforms. + */ +int __init common_cpuidle_init(struct cpuidle_driver *pdrv, bool simple, + void (*driver_data_init)(struct cpuidle_device *dev)) +{ + struct cpuidle_device *dev; + struct cpuidle_driver *drv; + int i, cpu_id, ret; + + if (!pdrv || pdrv->state_count > CPUIDLE_STATE_MAX) { + pr_err("%s: Invalid Input\n", __func__); + return -EINVAL; + } + + drv = kmalloc(sizeof(struct cpuidle_driver), GFP_KERNEL); + if (!drv) { + pr_err("%s: no memory for cpuidle driver\n", __func__); + return -ENOMEM; + } + + *drv = *pdrv; + + for (i = 0; simple && (i < drv->state_count); i++) { + do_idle[i] = drv->states[i].enter; + drv->states[i].enter = simple_enter; + } + + ret = cpuidle_register_driver(drv); + if (ret) { + pr_err("%s: Failed to register cpuidle driver\n", __func__); + goto free_drv; + } + + common_cpuidle_devices = alloc_percpu(struct cpuidle_device); + if (common_cpuidle_devices == NULL) { + ret = -ENOMEM; + goto unregister_drv; + } + + /* initialize state data for each cpuidle_device */ + for_each_possible_cpu(cpu_id) { + dev = per_cpu_ptr(common_cpuidle_devices, cpu_id); + dev->cpu = cpu_id; + dev->state_count = drv->state_count; + + if (driver_data_init) + driver_data_init(dev); + + ret = cpuidle_register_device(dev); + if (ret) { + pr_err("%s: Failed to register cpu %u\n", + __func__, cpu_id); + goto uninit; + } + } + + return 0; +uninit: + + common_cpuidle_devices_uninit(); + +unregister_drv: + + cpuidle_unregister_driver(drv); + +free_drv: + + kfree(drv); + + return ret; +} diff --git a/include/linux/cpuidle.h b/include/linux/cpuidle.h index 712abcc..2aa89db 100644 --- a/include/linux/cpuidle.h +++ b/include/linux/cpuidle.h @@ -56,6 +56,16 @@ struct cpuidle_state { #define CPUIDLE_DRIVER_FLAGS_MASK (0xFFFF0000) +/* Common ARM WFI state */ +#define CPUIDLE_ARM_WFI_STATE {\ + .enter = cpuidle_def_idle,\ + .exit_latency = 2,\ + .target_residency = 1,\ + .flags = CPUIDLE_FLAG_TIME_VALID,\ + .name = "WFI",\ + .desc = "ARM core clock gating (WFI)",\ +} + /** * cpuidle_get_statedata - retrieves private driver state data * @st_usage: the state usage statistics @@ -141,6 +151,13 @@ extern void cpuidle_resume_and_unlock(void); extern int cpuidle_enable_device(struct cpuidle_device *dev); extern void cpuidle_disable_device(struct cpuidle_device *dev); +/* provide a default idle function */ +extern int cpuidle_def_idle(struct cpuidle_device *dev, + struct cpuidle_driver *drv, int index); +extern int common_cpuidle_init(struct cpuidle_driver *drv, bool simple, + void (*driver_data_init)(struct cpuidle_device *dev)); +extern void common_cpuidle_devices_uninit(void); + #else static inline void disable_cpuidle(void) { } static inline int cpuidle_idle_call(void) { return -ENODEV; } @@ -157,6 +174,13 @@ static inline void cpuidle_resume_and_unlock(void) { } static inline int cpuidle_enable_device(struct cpuidle_device *dev) {return -ENODEV; } static inline void cpuidle_disable_device(struct cpuidle_device *dev) { } +static inline int cpuidle_def_idle(struct cpuidle_device *dev, + struct cpuidle_driver *drv, int index) +{return -ENODEV; } +static inline int common_cpuidle_init(struct cpuidle_driver *pdrv, + bool simple, void (*driver_data_init)(struct cpuidle_device *dev)) +{return -ENODEV; } +static inline void common_cpuidle_devices_uninit(void) { } #endif