From patchwork Fri Apr 26 11:42:09 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 16421 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ye0-f197.google.com (mail-ye0-f197.google.com [209.85.213.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id C7E362397A for ; Fri, 26 Apr 2013 11:43:09 +0000 (UTC) Received: by mail-ye0-f197.google.com with SMTP id q9sf5993693yen.0 for ; Fri, 26 Apr 2013 04:42:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:mime-version:x-beenthere:x-received:received-spf :x-received:x-forwarded-to:x-forwarded-for:delivered-to:x-received :received-spf:x-received:from:to:cc:subject:date:message-id:x-mailer :x-gm-message-state:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-google-group-id:list-post:list-help:list-archive:list-unsubscribe; bh=l2ujVeKHmZ/S7NNO0fuyJBPdYNiBUnhG3Ztof3u+vV0=; b=U5nOK0RbW3a9BDaff6GuPFhAS/WmTmkEMCrcCbf3eUTLCPsga3OeTvRY+EEFb5QOjJ oiBu/0XoU2mFINg6ZK35KTUc+cFOpu6vqAlj0E+admzwwPPVP41wDmYKtCBkA2kfOO0g stcJr9yIRdNYS3Bnd58WIYfaqIJqfuPBsJtuSj4AUm20RVVY15KYccZNhilNICDP6weI LMcYkkQUkj4Gv4PnE48O9QJ4H+9GqTkZwEm+/VE682aEkde3A5mLznwaEWWlZSvGh8kD e/1oNb22DKRoAng+bArBqv0Ptyy1Fbc7do1iTnxDO9Vx8XdotHTjgS019WB69nTW0KFq jrDw== X-Received: by 10.236.145.131 with SMTP id p3mr21755733yhj.6.1366976534146; Fri, 26 Apr 2013 04:42:14 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.71.141 with SMTP id v13ls1855594qeu.84.gmail; Fri, 26 Apr 2013 04:42:14 -0700 (PDT) X-Received: by 10.52.17.102 with SMTP id n6mr15223071vdd.109.1366976533922; Fri, 26 Apr 2013 04:42:13 -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 gr7si5179491vdc.142.2013.04.26.04.42.13 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 26 Apr 2013 04:42:13 -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 hf12so3672822vcb.21 for ; Fri, 26 Apr 2013 04:42:13 -0700 (PDT) X-Received: by 10.52.228.71 with SMTP id sg7mr23821718vdc.51.1366976533724; Fri, 26 Apr 2013 04:42:13 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.58.127.98 with SMTP id nf2csp64553veb; Fri, 26 Apr 2013 04:42:12 -0700 (PDT) X-Received: by 10.180.205.135 with SMTP id lg7mr3451175wic.11.1366976532419; Fri, 26 Apr 2013 04:42:12 -0700 (PDT) Received: from mail-we0-x232.google.com (mail-we0-x232.google.com [2a00:1450:400c:c03::232]) by mx.google.com with ESMTPS id k10si655460wie.56.2013.04.26.04.42.11 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 26 Apr 2013 04:42:12 -0700 (PDT) Received-SPF: neutral (google.com: 2a00:1450:400c:c03::232 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) client-ip=2a00:1450:400c:c03::232; Received: by mail-we0-f178.google.com with SMTP id t11so240233wey.23 for ; Fri, 26 Apr 2013 04:42:11 -0700 (PDT) X-Received: by 10.194.93.231 with SMTP id cx7mr20382793wjb.33.1366976531782; Fri, 26 Apr 2013 04:42:11 -0700 (PDT) Received: from mai.home (AToulouse-654-1-440-176.w83-205.abo.wanadoo.fr. [83.205.71.176]) by mx.google.com with ESMTPSA id o5sm3290254wix.3.2013.04.26.04.42.09 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 26 Apr 2013 04:42:10 -0700 (PDT) From: Daniel Lezcano To: rjw@sisk.pl, lorenzo.pieralisi@arm.com Cc: linux-pm@vger.kernel.org, linaro-kernel@lists.linaro.org, patches@linaro.org Subject: [PATCH] cpuidle: simplify multiple driver support Date: Fri, 26 Apr 2013 13:42:09 +0200 Message-Id: <1366976529-27731-1-git-send-email-daniel.lezcano@linaro.org> X-Mailer: git-send-email 1.7.9.5 X-Gm-Message-State: ALoCoQmlrqmjGFS3HZGgpeDj9ltJg4jt1vkpIat3HFrdUEJDUBIzPwjZwVXuP6Sad+7//5fHShyD X-Original-Sender: daniel.lezcano@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 Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Commit bf4d1b5ddb78f86078ac6ae0415802d5f0c68f92 brought the multiple driver support. The code added a couple of new API to register the driver per cpu. That led to some code complexity to handle the kernel config options when the multiple driver support is enabled or not, which is not really necessary. The code has to be compatible when the multiple driver support is not enabled, and the multiple driver support has to be compatible with the old api. This patch removes this API, which is not yet used by any driver but needed for the HMP cpuidle drivers which will come soon, and replaces its usage by a cpumask pointer in the cpuidle driver structure telling what cpus are handled by the driver. That let the API cpuidle_[un]register_driver to be used for the multipled driver support. The current code, a bit poor in comments, has been commented and simplified. Signed-off-by: Daniel Lezcano --- drivers/cpuidle/driver.c | 325 ++++++++++++++++++++++++++++------------------ include/linux/cpuidle.h | 21 +-- 2 files changed, 212 insertions(+), 134 deletions(-) diff --git a/drivers/cpuidle/driver.c b/drivers/cpuidle/driver.c index 8dfaaae..2db96b5 100644 --- a/drivers/cpuidle/driver.c +++ b/drivers/cpuidle/driver.c @@ -18,206 +18,267 @@ DEFINE_SPINLOCK(cpuidle_driver_lock); -static void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu); -static struct cpuidle_driver * __cpuidle_get_cpu_driver(int cpu); +#ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS -static void cpuidle_setup_broadcast_timer(void *arg) +static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers); + +/** + * __cpuidle_get_cpu_driver: returns the cpuidle driver tied with the specified + * cpu. + * + * @cpu: an integer specifying the cpu number + * + * Returns a pointer to struct cpuidle_driver, NULL if no driver has been + * registered for this driver + */ +static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu) { - int cpu = smp_processor_id(); - clockevents_notify((long)(arg), &cpu); + return per_cpu(cpuidle_drivers, cpu); } -static void __cpuidle_driver_init(struct cpuidle_driver *drv, int cpu) +/** + * __cpuidle_set_driver: assign to the per cpu variable the driver pointer for + * each cpu the driver is assigned to with the cpumask. + * + * @drv: a pointer to a struct cpuidle_driver + * + * Returns 0 on success, < 0 otherwise + */ +static inline int __cpuidle_set_driver(struct cpuidle_driver *drv) { - int i; + int cpu; - drv->refcnt = 0; + for_each_cpu(cpu, drv->cpumask) { - for (i = drv->state_count - 1; i >= 0 ; i--) { + if (__cpuidle_get_cpu_driver(cpu)) + return -EBUSY; - if (!(drv->states[i].flags & CPUIDLE_FLAG_TIMER_STOP)) - continue; - - drv->bctimer = 1; - on_each_cpu_mask(get_cpu_mask(cpu), cpuidle_setup_broadcast_timer, - (void *)CLOCK_EVT_NOTIFY_BROADCAST_ON, 1); - break; + per_cpu(cpuidle_drivers, cpu) = drv; } + + return 0; } -static int __cpuidle_register_driver(struct cpuidle_driver *drv, int cpu) +/** + * __cpuidle_unset_driver: for each cpu the driver is handling, set the per cpu + * variable driver to NULL. + * + * @drv: a pointer to a struct cpuidle_driver + */ +static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv) { - if (!drv || !drv->state_count) - return -EINVAL; - - if (cpuidle_disabled()) - return -ENODEV; - - if (__cpuidle_get_cpu_driver(cpu)) - return -EBUSY; + int cpu; - __cpuidle_driver_init(drv, cpu); + for_each_cpu(cpu, drv->cpumask) { - __cpuidle_set_cpu_driver(drv, cpu); + if (drv != __cpuidle_get_cpu_driver(cpu)) + continue; - return 0; + per_cpu(cpuidle_drivers, cpu) = NULL; + } } -static void __cpuidle_unregister_driver(struct cpuidle_driver *drv, int cpu) -{ - if (drv != __cpuidle_get_cpu_driver(cpu)) - return; +#else - if (!WARN_ON(drv->refcnt > 0)) - __cpuidle_set_cpu_driver(NULL, cpu); +static struct cpuidle_driver *cpuidle_curr_driver; - if (drv->bctimer) { - drv->bctimer = 0; - on_each_cpu_mask(get_cpu_mask(cpu), cpuidle_setup_broadcast_timer, - (void *)CLOCK_EVT_NOTIFY_BROADCAST_OFF, 1); - } +/** + * __cpuidle_get_cpu_driver: returns the global cpuidle driver pointer. + * + * @cpu: an integer specifying the cpu number, this parameter is ignored + * + * Returns a pointer to a struct cpuidle_driver, NULL if no driver was + * previously registered + */ +static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu) +{ + return cpuidle_curr_driver; } -#ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS +/** + * __cpuidle_set_driver: assign the cpuidle driver pointer to the global cpuidle + * driver variable. + * + * @drv: a pointer to a struct cpuidle_driver + * + * Returns 0 on success, < 0 otherwise + */ +static inline int __cpuidle_set_driver(struct cpuidle_driver *drv) +{ + if (cpuidle_curr_driver) + return -EBUSY; -static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers); + cpuidle_curr_driver = drv; -static void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu) -{ - per_cpu(cpuidle_drivers, cpu) = drv; + return 0; } -static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu) +/** + * __cpuidle_unset_driver: reset the global cpuidle driver variable if the + * cpuidle driver pointer match it. + * + * @drv: a pointer to a struct cpuidle_driver + */ +static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv) { - return per_cpu(cpuidle_drivers, cpu); + if (drv == cpuidle_curr_driver) + cpuidle_curr_driver = NULL; } -static void __cpuidle_unregister_all_cpu_driver(struct cpuidle_driver *drv) +#endif + +/** + * cpuidle_setup_broadcast_timer: set the broadcast timer notification for the + * current cpu. This function is called per cpu context invoked by a smp cross + * call. It is not supposed to be called directly. + * + * @arg: a void pointer, actually used to match the smp cross call api but used + * as a long with two values: + * - CLOCK_EVT_NOTIFY_BROADCAST_ON + * - CLOCK_EVT_NOTIFY_BROADCAST_OFF + */ +static void cpuidle_setup_broadcast_timer(void *arg) { - int cpu; - for_each_present_cpu(cpu) - __cpuidle_unregister_driver(drv, cpu); + int cpu = smp_processor_id(); + clockevents_notify((long)(arg), &cpu); } -static int __cpuidle_register_all_cpu_driver(struct cpuidle_driver *drv) +/** + * __cpuidle_driver_init: initialize the driver internal data. + * + * @drv: a valid pointer to a struct cpuidle_driver + * + * Returns 0 on success, < 0 otherwise + */ +static int __cpuidle_driver_init(struct cpuidle_driver *drv) { - int ret = 0; - int i, cpu; + int i; - for_each_present_cpu(cpu) { - ret = __cpuidle_register_driver(drv, cpu); - if (ret) - break; - } + drv->refcnt = 0; - if (ret) - for_each_present_cpu(i) { - if (i == cpu) - break; - __cpuidle_unregister_driver(drv, i); - } + /* + * we default here to all cpu possible because if the kernel + * boots with some cpus offline and then we online one of them + * the cpu notifier won't know which driver to assign + */ + if (!drv->cpumask) + drv->cpumask = cpu_possible_mask; + + /* + * we look for the timer stop flag in the different states, + * so know we have to setup the broadcast timer. The loop is + * in reverse order, because usually the deeper state has this + * flag set + */ + for (i = drv->state_count - 1; i >= 0 ; i--) { + if (!(drv->states[i].flags & CPUIDLE_FLAG_TIMER_STOP)) + continue; - return ret; + drv->bctimer = 1; + break; + } + + return 0; } -int cpuidle_register_cpu_driver(struct cpuidle_driver *drv, int cpu) +/** + * __cpuidle_register_driver: do some sanity checks, initializes the driver, + * assign the driver to the global cpuidle driver variable(s) and setup the + * broadcast timer if the cpuidle driver has some states which shutdown the + * local timer. + * + * @drv: a valid pointer to a struct cpuidle_driver + * + * Returns 0 on success, < 0 otherwise + */ +static int __cpuidle_register_driver(struct cpuidle_driver *drv) { int ret; - spin_lock(&cpuidle_driver_lock); - ret = __cpuidle_register_driver(drv, cpu); - spin_unlock(&cpuidle_driver_lock); + if (!drv || !drv->state_count) + return -EINVAL; - return ret; -} + if (cpuidle_disabled()) + return -ENODEV; -void cpuidle_unregister_cpu_driver(struct cpuidle_driver *drv, int cpu) -{ - spin_lock(&cpuidle_driver_lock); - __cpuidle_unregister_driver(drv, cpu); - spin_unlock(&cpuidle_driver_lock); -} + ret = __cpuidle_driver_init(drv); + if (ret) + return ret; -/** - * cpuidle_register_driver - registers a driver - * @drv: the driver - */ -int cpuidle_register_driver(struct cpuidle_driver *drv) -{ - int ret; + ret = __cpuidle_set_driver(drv); + if (ret) + return ret; - spin_lock(&cpuidle_driver_lock); - ret = __cpuidle_register_all_cpu_driver(drv); - spin_unlock(&cpuidle_driver_lock); + if (drv->bctimer) + on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer, + (void *)CLOCK_EVT_NOTIFY_BROADCAST_ON, 1); - return ret; + return 0; } -EXPORT_SYMBOL_GPL(cpuidle_register_driver); /** - * cpuidle_unregister_driver - unregisters a driver - * @drv: the driver + * __cpuidle_unregister_driver: checks the driver is no longer in use, reset the + * global cpuidle driver variable(s) and disable the timer broadcast + * notification mechanism if it was in use. + * + * @drv: a valid pointer to a struct cpuidle_driver + * + * Returns 0 on success, < 0 otherwise */ -void cpuidle_unregister_driver(struct cpuidle_driver *drv) +static void __cpuidle_unregister_driver(struct cpuidle_driver *drv) { - spin_lock(&cpuidle_driver_lock); - __cpuidle_unregister_all_cpu_driver(drv); - spin_unlock(&cpuidle_driver_lock); -} -EXPORT_SYMBOL_GPL(cpuidle_unregister_driver); - -#else - -static struct cpuidle_driver *cpuidle_curr_driver; + if (!WARN_ON(drv->refcnt > 0)) + return; -static inline void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu) -{ - cpuidle_curr_driver = drv; -} + __cpuidle_unset_driver(drv); -static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu) -{ - return cpuidle_curr_driver; + if (drv->bctimer) { + drv->bctimer = 0; + on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer, + (void *)CLOCK_EVT_NOTIFY_BROADCAST_OFF, 1); + } } /** - * cpuidle_register_driver - registers a driver - * @drv: the driver + * cpuidle_register_driver: registers a driver by taking a lock to prevent + * multiple callers to [un]register a driver at the same time. + * + * @drv: a pointer to a valid struct cpuidle_driver + * + * Returns 0 on success, < 0 otherwise */ int cpuidle_register_driver(struct cpuidle_driver *drv) { - int ret, cpu; + int ret; - cpu = get_cpu(); spin_lock(&cpuidle_driver_lock); - ret = __cpuidle_register_driver(drv, cpu); + ret = __cpuidle_register_driver(drv); spin_unlock(&cpuidle_driver_lock); - put_cpu(); return ret; } EXPORT_SYMBOL_GPL(cpuidle_register_driver); /** - * cpuidle_unregister_driver - unregisters a driver - * @drv: the driver + * cpuidle_unregister_driver: unregisters a driver by taking a lock to prevent + * multiple callers to [un]register a driver at the same time. The specified + * driver must match the driver currently registered. + * + * @drv: a pointer to a valid struct cpuidle_driver */ void cpuidle_unregister_driver(struct cpuidle_driver *drv) { - int cpu; - - cpu = get_cpu(); spin_lock(&cpuidle_driver_lock); - __cpuidle_unregister_driver(drv, cpu); + __cpuidle_unregister_driver(drv); spin_unlock(&cpuidle_driver_lock); - put_cpu(); } EXPORT_SYMBOL_GPL(cpuidle_unregister_driver); -#endif /** - * cpuidle_get_driver - return the current driver + * cpuidle_get_driver: returns the driver tied with the current cpu. + * + * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered */ struct cpuidle_driver *cpuidle_get_driver(void) { @@ -233,7 +294,12 @@ struct cpuidle_driver *cpuidle_get_driver(void) EXPORT_SYMBOL_GPL(cpuidle_get_driver); /** - * cpuidle_get_cpu_driver - return the driver tied with a cpu + * cpuidle_get_cpu_driver: returns the driver registered with a cpu. + * + * @dev: a valid pointer to a struct cpuidle_device + * + * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered + * for the specified cpu */ struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev) { @@ -244,6 +310,13 @@ struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev) } EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver); +/** + * cpuidle_driver_ref: gets a refcount for the driver. Note this function takes + * a refcount for the driver assigned to the current cpu. + * + * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered + * for the current cpu + */ struct cpuidle_driver *cpuidle_driver_ref(void) { struct cpuidle_driver *drv; @@ -257,6 +330,10 @@ struct cpuidle_driver *cpuidle_driver_ref(void) return drv; } +/** + * cpuidle_driver_unref: puts down the refcount for the driver. Note this + * function decrement the refcount for the driver assigned to the current cpu. + */ void cpuidle_driver_unref(void) { struct cpuidle_driver *drv = cpuidle_get_driver(); diff --git a/include/linux/cpuidle.h b/include/linux/cpuidle.h index 3c86faa..e7a94db 100644 --- a/include/linux/cpuidle.h +++ b/include/linux/cpuidle.h @@ -101,16 +101,20 @@ static inline int cpuidle_get_last_residency(struct cpuidle_device *dev) ****************************/ struct cpuidle_driver { - const char *name; - struct module *owner; - int refcnt; + const char *name; + struct module *owner; + int refcnt; /* used by the cpuidle framework to setup the broadcast timer */ - unsigned int bctimer:1; + unsigned int bctimer:1; + /* states array must be ordered in decreasing power consumption */ - struct cpuidle_state states[CPUIDLE_STATE_MAX]; - int state_count; - int safe_state_index; + struct cpuidle_state states[CPUIDLE_STATE_MAX]; + int state_count; + int safe_state_index; + + /* the driver handles the cpus in cpumask */ + const struct cpumask *cpumask; }; #ifdef CONFIG_CPU_IDLE @@ -135,9 +139,6 @@ extern void cpuidle_disable_device(struct cpuidle_device *dev); extern int cpuidle_play_dead(void); extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev); -extern int cpuidle_register_cpu_driver(struct cpuidle_driver *drv, int cpu); -extern void cpuidle_unregister_cpu_driver(struct cpuidle_driver *drv, int cpu); - #else static inline void disable_cpuidle(void) { } static inline int cpuidle_idle_call(void) { return -ENODEV; }