Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / arch / powerpc / platforms / pseries / suspend.c
blobd3de0849f29645520e9537569156d6b022f732a4
1 /*
2 * Copyright (C) 2010 Brian King IBM Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/delay.h>
20 #include <linux/suspend.h>
21 #include <linux/stat.h>
22 #include <asm/firmware.h>
23 #include <asm/hvcall.h>
24 #include <asm/machdep.h>
25 #include <asm/mmu.h>
26 #include <asm/rtas.h>
28 static u64 stream_id;
29 static struct sys_device suspend_sysdev;
30 static DECLARE_COMPLETION(suspend_work);
31 static struct rtas_suspend_me_data suspend_data;
32 static atomic_t suspending;
34 /**
35 * pseries_suspend_begin - First phase of hibernation
37 * Check to ensure we are in a valid state to hibernate
39 * Return value:
40 * 0 on success / other on failure
41 **/
42 static int pseries_suspend_begin(suspend_state_t state)
44 long vasi_state, rc;
45 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
47 /* Make sure the state is valid */
48 rc = plpar_hcall(H_VASI_STATE, retbuf, stream_id);
50 vasi_state = retbuf[0];
52 if (rc) {
53 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
54 return rc;
55 } else if (vasi_state == H_VASI_ENABLED) {
56 return -EAGAIN;
57 } else if (vasi_state != H_VASI_SUSPENDING) {
58 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
59 vasi_state);
60 return -EIO;
63 return 0;
66 /**
67 * pseries_suspend_cpu - Suspend a single CPU
69 * Makes the H_JOIN call to suspend the CPU
71 **/
72 static int pseries_suspend_cpu(void)
74 if (atomic_read(&suspending))
75 return rtas_suspend_cpu(&suspend_data);
76 return 0;
79 /**
80 * pseries_suspend_enter - Final phase of hibernation
82 * Return value:
83 * 0 on success / other on failure
84 **/
85 static int pseries_suspend_enter(suspend_state_t state)
87 int rc = rtas_suspend_last_cpu(&suspend_data);
89 atomic_set(&suspending, 0);
90 atomic_set(&suspend_data.done, 1);
91 return rc;
94 /**
95 * pseries_prepare_late - Prepare to suspend all other CPUs
97 * Return value:
98 * 0 on success / other on failure
99 **/
100 static int pseries_prepare_late(void)
102 atomic_set(&suspending, 1);
103 atomic_set(&suspend_data.working, 0);
104 atomic_set(&suspend_data.done, 0);
105 atomic_set(&suspend_data.error, 0);
106 suspend_data.complete = &suspend_work;
107 INIT_COMPLETION(suspend_work);
108 return 0;
112 * store_hibernate - Initiate partition hibernation
113 * @classdev: sysdev class struct
114 * @attr: class device attribute struct
115 * @buf: buffer
116 * @count: buffer size
118 * Write the stream ID received from the HMC to this file
119 * to trigger hibernating the partition
121 * Return value:
122 * number of bytes printed to buffer / other on failure
124 static ssize_t store_hibernate(struct sysdev_class *classdev,
125 struct sysdev_class_attribute *attr,
126 const char *buf, size_t count)
128 int rc;
130 if (!capable(CAP_SYS_ADMIN))
131 return -EPERM;
133 stream_id = simple_strtoul(buf, NULL, 16);
135 do {
136 rc = pseries_suspend_begin(PM_SUSPEND_MEM);
137 if (rc == -EAGAIN)
138 ssleep(1);
139 } while (rc == -EAGAIN);
141 if (!rc)
142 rc = pm_suspend(PM_SUSPEND_MEM);
144 stream_id = 0;
146 if (!rc)
147 rc = count;
148 return rc;
151 static SYSDEV_CLASS_ATTR(hibernate, S_IWUSR, NULL, store_hibernate);
153 static struct sysdev_class suspend_sysdev_class = {
154 .name = "power",
157 static const struct platform_suspend_ops pseries_suspend_ops = {
158 .valid = suspend_valid_only_mem,
159 .begin = pseries_suspend_begin,
160 .prepare_late = pseries_prepare_late,
161 .enter = pseries_suspend_enter,
165 * pseries_suspend_sysfs_register - Register with sysfs
167 * Return value:
168 * 0 on success / other on failure
170 static int pseries_suspend_sysfs_register(struct sys_device *sysdev)
172 int rc;
174 if ((rc = sysdev_class_register(&suspend_sysdev_class)))
175 return rc;
177 sysdev->id = 0;
178 sysdev->cls = &suspend_sysdev_class;
180 if ((rc = sysdev_class_create_file(&suspend_sysdev_class, &attr_hibernate)))
181 goto class_unregister;
183 return 0;
185 class_unregister:
186 sysdev_class_unregister(&suspend_sysdev_class);
187 return rc;
191 * pseries_suspend_init - initcall for pSeries suspend
193 * Return value:
194 * 0 on success / other on failure
196 static int __init pseries_suspend_init(void)
198 int rc;
200 if (!machine_is(pseries) || !firmware_has_feature(FW_FEATURE_LPAR))
201 return 0;
203 suspend_data.token = rtas_token("ibm,suspend-me");
204 if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
205 return 0;
207 if ((rc = pseries_suspend_sysfs_register(&suspend_sysdev)))
208 return rc;
210 ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
211 suspend_set_ops(&pseries_suspend_ops);
212 return 0;
215 __initcall(pseries_suspend_init);