Path: blob/master/arch/powerpc/platforms/pseries/suspend.c
10818 views
/*1* Copyright (C) 2010 Brian King IBM Corporation2*3* This program is free software; you can redistribute it and/or modify4* it under the terms of the GNU General Public License as published by5* the Free Software Foundation; either version 2 of the License, or6* (at your option) any later version.7*8* This program is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11* GNU General Public License for more details.12*13* You should have received a copy of the GNU General Public License14* along with this program; if not, write to the Free Software15* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA16*/1718#include <linux/delay.h>19#include <linux/suspend.h>20#include <asm/firmware.h>21#include <asm/hvcall.h>22#include <asm/machdep.h>23#include <asm/mmu.h>24#include <asm/rtas.h>2526static u64 stream_id;27static struct sys_device suspend_sysdev;28static DECLARE_COMPLETION(suspend_work);29static struct rtas_suspend_me_data suspend_data;30static atomic_t suspending;3132/**33* pseries_suspend_begin - First phase of hibernation34*35* Check to ensure we are in a valid state to hibernate36*37* Return value:38* 0 on success / other on failure39**/40static int pseries_suspend_begin(suspend_state_t state)41{42long vasi_state, rc;43unsigned long retbuf[PLPAR_HCALL_BUFSIZE];4445/* Make sure the state is valid */46rc = plpar_hcall(H_VASI_STATE, retbuf, stream_id);4748vasi_state = retbuf[0];4950if (rc) {51pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);52return rc;53} else if (vasi_state == H_VASI_ENABLED) {54return -EAGAIN;55} else if (vasi_state != H_VASI_SUSPENDING) {56pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",57vasi_state);58return -EIO;59}6061return 0;62}6364/**65* pseries_suspend_cpu - Suspend a single CPU66*67* Makes the H_JOIN call to suspend the CPU68*69**/70static int pseries_suspend_cpu(void)71{72if (atomic_read(&suspending))73return rtas_suspend_cpu(&suspend_data);74return 0;75}7677/**78* pseries_suspend_enter - Final phase of hibernation79*80* Return value:81* 0 on success / other on failure82**/83static int pseries_suspend_enter(suspend_state_t state)84{85int rc = rtas_suspend_last_cpu(&suspend_data);8687atomic_set(&suspending, 0);88atomic_set(&suspend_data.done, 1);89return rc;90}9192/**93* pseries_prepare_late - Prepare to suspend all other CPUs94*95* Return value:96* 0 on success / other on failure97**/98static int pseries_prepare_late(void)99{100atomic_set(&suspending, 1);101atomic_set(&suspend_data.working, 0);102atomic_set(&suspend_data.done, 0);103atomic_set(&suspend_data.error, 0);104suspend_data.complete = &suspend_work;105INIT_COMPLETION(suspend_work);106return 0;107}108109/**110* store_hibernate - Initiate partition hibernation111* @classdev: sysdev class struct112* @attr: class device attribute struct113* @buf: buffer114* @count: buffer size115*116* Write the stream ID received from the HMC to this file117* to trigger hibernating the partition118*119* Return value:120* number of bytes printed to buffer / other on failure121**/122static ssize_t store_hibernate(struct sysdev_class *classdev,123struct sysdev_class_attribute *attr,124const char *buf, size_t count)125{126int rc;127128if (!capable(CAP_SYS_ADMIN))129return -EPERM;130131stream_id = simple_strtoul(buf, NULL, 16);132133do {134rc = pseries_suspend_begin(PM_SUSPEND_MEM);135if (rc == -EAGAIN)136ssleep(1);137} while (rc == -EAGAIN);138139if (!rc)140rc = pm_suspend(PM_SUSPEND_MEM);141142stream_id = 0;143144if (!rc)145rc = count;146return rc;147}148149static SYSDEV_CLASS_ATTR(hibernate, S_IWUSR, NULL, store_hibernate);150151static struct sysdev_class suspend_sysdev_class = {152.name = "power",153};154155static const struct platform_suspend_ops pseries_suspend_ops = {156.valid = suspend_valid_only_mem,157.begin = pseries_suspend_begin,158.prepare_late = pseries_prepare_late,159.enter = pseries_suspend_enter,160};161162/**163* pseries_suspend_sysfs_register - Register with sysfs164*165* Return value:166* 0 on success / other on failure167**/168static int pseries_suspend_sysfs_register(struct sys_device *sysdev)169{170int rc;171172if ((rc = sysdev_class_register(&suspend_sysdev_class)))173return rc;174175sysdev->id = 0;176sysdev->cls = &suspend_sysdev_class;177178if ((rc = sysdev_class_create_file(&suspend_sysdev_class, &attr_hibernate)))179goto class_unregister;180181return 0;182183class_unregister:184sysdev_class_unregister(&suspend_sysdev_class);185return rc;186}187188/**189* pseries_suspend_init - initcall for pSeries suspend190*191* Return value:192* 0 on success / other on failure193**/194static int __init pseries_suspend_init(void)195{196int rc;197198if (!machine_is(pseries) || !firmware_has_feature(FW_FEATURE_LPAR))199return 0;200201suspend_data.token = rtas_token("ibm,suspend-me");202if (suspend_data.token == RTAS_UNKNOWN_SERVICE)203return 0;204205if ((rc = pseries_suspend_sysfs_register(&suspend_sysdev)))206return rc;207208ppc_md.suspend_disable_cpu = pseries_suspend_cpu;209suspend_set_ops(&pseries_suspend_ops);210return 0;211}212213__initcall(pseries_suspend_init);214215216