blob: 47226e04126d10e930a7a4a1bf42aa1c9abedb7a [file] [log] [blame]
Brian King32d8ad42010-07-07 12:31:02 +00001/*
2 * Copyright (C) 2010 Brian King IBM Corporation
3 *
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.
8 *
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.
13 *
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
17 */
18
19#include <linux/delay.h>
20#include <linux/suspend.h>
Paul Gortmakerb56eade2011-05-27 13:27:45 -040021#include <linux/stat.h>
Brian King32d8ad42010-07-07 12:31:02 +000022#include <asm/firmware.h>
23#include <asm/hvcall.h>
24#include <asm/machdep.h>
25#include <asm/mmu.h>
26#include <asm/rtas.h>
Brian King444080d2012-01-11 06:56:04 +000027#include <asm/topology.h>
Brian King32d8ad42010-07-07 12:31:02 +000028
29static u64 stream_id;
Kay Sievers86ba41d2011-12-21 15:09:52 -080030static struct device suspend_dev;
Brian King32d8ad42010-07-07 12:31:02 +000031static DECLARE_COMPLETION(suspend_work);
32static struct rtas_suspend_me_data suspend_data;
33static atomic_t suspending;
34
35/**
36 * pseries_suspend_begin - First phase of hibernation
37 *
38 * Check to ensure we are in a valid state to hibernate
39 *
40 * Return value:
41 * 0 on success / other on failure
42 **/
43static int pseries_suspend_begin(suspend_state_t state)
44{
45 long vasi_state, rc;
46 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
47
48 /* Make sure the state is valid */
49 rc = plpar_hcall(H_VASI_STATE, retbuf, stream_id);
50
51 vasi_state = retbuf[0];
52
53 if (rc) {
54 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
55 return rc;
56 } else if (vasi_state == H_VASI_ENABLED) {
57 return -EAGAIN;
58 } else if (vasi_state != H_VASI_SUSPENDING) {
59 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
60 vasi_state);
61 return -EIO;
62 }
63
64 return 0;
65}
66
67/**
68 * pseries_suspend_cpu - Suspend a single CPU
69 *
70 * Makes the H_JOIN call to suspend the CPU
71 *
72 **/
73static int pseries_suspend_cpu(void)
74{
75 if (atomic_read(&suspending))
76 return rtas_suspend_cpu(&suspend_data);
77 return 0;
78}
79
80/**
81 * pseries_suspend_enter - Final phase of hibernation
82 *
83 * Return value:
84 * 0 on success / other on failure
85 **/
86static int pseries_suspend_enter(suspend_state_t state)
87{
88 int rc = rtas_suspend_last_cpu(&suspend_data);
89
90 atomic_set(&suspending, 0);
91 atomic_set(&suspend_data.done, 1);
92 return rc;
93}
94
95/**
96 * pseries_prepare_late - Prepare to suspend all other CPUs
97 *
98 * Return value:
99 * 0 on success / other on failure
100 **/
101static int pseries_prepare_late(void)
102{
103 atomic_set(&suspending, 1);
104 atomic_set(&suspend_data.working, 0);
105 atomic_set(&suspend_data.done, 0);
106 atomic_set(&suspend_data.error, 0);
107 suspend_data.complete = &suspend_work;
108 INIT_COMPLETION(suspend_work);
109 return 0;
110}
111
112/**
113 * store_hibernate - Initiate partition hibernation
Kay Sievers86ba41d2011-12-21 15:09:52 -0800114 * @dev: subsys root device
115 * @attr: device attribute struct
Brian King32d8ad42010-07-07 12:31:02 +0000116 * @buf: buffer
117 * @count: buffer size
118 *
119 * Write the stream ID received from the HMC to this file
120 * to trigger hibernating the partition
121 *
122 * Return value:
123 * number of bytes printed to buffer / other on failure
124 **/
Kay Sievers86ba41d2011-12-21 15:09:52 -0800125static ssize_t store_hibernate(struct device *dev,
126 struct device_attribute *attr,
Brian King32d8ad42010-07-07 12:31:02 +0000127 const char *buf, size_t count)
128{
129 int rc;
130
131 if (!capable(CAP_SYS_ADMIN))
132 return -EPERM;
133
134 stream_id = simple_strtoul(buf, NULL, 16);
135
136 do {
137 rc = pseries_suspend_begin(PM_SUSPEND_MEM);
138 if (rc == -EAGAIN)
139 ssleep(1);
140 } while (rc == -EAGAIN);
141
Brian King444080d2012-01-11 06:56:04 +0000142 if (!rc) {
143 stop_topology_update();
Brian King32d8ad42010-07-07 12:31:02 +0000144 rc = pm_suspend(PM_SUSPEND_MEM);
Brian King444080d2012-01-11 06:56:04 +0000145 start_topology_update();
146 }
Brian King32d8ad42010-07-07 12:31:02 +0000147
148 stream_id = 0;
149
150 if (!rc)
151 rc = count;
152 return rc;
153}
154
Kay Sievers86ba41d2011-12-21 15:09:52 -0800155static DEVICE_ATTR(hibernate, S_IWUSR, NULL, store_hibernate);
Brian King32d8ad42010-07-07 12:31:02 +0000156
Kay Sievers86ba41d2011-12-21 15:09:52 -0800157static struct bus_type suspend_subsys = {
Brian King32d8ad42010-07-07 12:31:02 +0000158 .name = "power",
Kay Sievers86ba41d2011-12-21 15:09:52 -0800159 .dev_name = "power",
Brian King32d8ad42010-07-07 12:31:02 +0000160};
161
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100162static const struct platform_suspend_ops pseries_suspend_ops = {
Brian King32d8ad42010-07-07 12:31:02 +0000163 .valid = suspend_valid_only_mem,
164 .begin = pseries_suspend_begin,
165 .prepare_late = pseries_prepare_late,
166 .enter = pseries_suspend_enter,
167};
168
169/**
170 * pseries_suspend_sysfs_register - Register with sysfs
171 *
172 * Return value:
173 * 0 on success / other on failure
174 **/
Kay Sievers86ba41d2011-12-21 15:09:52 -0800175static int pseries_suspend_sysfs_register(struct device *dev)
Brian King32d8ad42010-07-07 12:31:02 +0000176{
177 int rc;
178
Kay Sievers86ba41d2011-12-21 15:09:52 -0800179 if ((rc = subsys_system_register(&suspend_subsys, NULL)))
Brian King32d8ad42010-07-07 12:31:02 +0000180 return rc;
181
Kay Sievers86ba41d2011-12-21 15:09:52 -0800182 dev->id = 0;
183 dev->bus = &suspend_subsys;
Brian King32d8ad42010-07-07 12:31:02 +0000184
Kay Sievers86ba41d2011-12-21 15:09:52 -0800185 if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
186 goto subsys_unregister;
Brian King32d8ad42010-07-07 12:31:02 +0000187
188 return 0;
189
Kay Sievers86ba41d2011-12-21 15:09:52 -0800190subsys_unregister:
191 bus_unregister(&suspend_subsys);
Brian King32d8ad42010-07-07 12:31:02 +0000192 return rc;
193}
194
195/**
196 * pseries_suspend_init - initcall for pSeries suspend
197 *
198 * Return value:
199 * 0 on success / other on failure
200 **/
201static int __init pseries_suspend_init(void)
202{
203 int rc;
204
205 if (!machine_is(pseries) || !firmware_has_feature(FW_FEATURE_LPAR))
206 return 0;
207
208 suspend_data.token = rtas_token("ibm,suspend-me");
209 if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
210 return 0;
211
Kay Sievers86ba41d2011-12-21 15:09:52 -0800212 if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
Brian King32d8ad42010-07-07 12:31:02 +0000213 return rc;
214
215 ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
216 suspend_set_ops(&pseries_suspend_ops);
217 return 0;
218}
219
220__initcall(pseries_suspend_init);