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/cpu.h>
20 #include <linux/delay.h>
21 #include <linux/suspend.h>
22 #include <linux/stat.h>
23 #include <asm/firmware.h>
24 #include <asm/hvcall.h>
25 #include <asm/machdep.h>
28 #include <asm/topology.h>
31 static struct device suspend_dev
;
32 static DECLARE_COMPLETION(suspend_work
);
33 static struct rtas_suspend_me_data suspend_data
;
34 static atomic_t suspending
;
37 * pseries_suspend_begin - First phase of hibernation
39 * Check to ensure we are in a valid state to hibernate
42 * 0 on success / other on failure
44 static int pseries_suspend_begin(suspend_state_t state
)
47 unsigned long retbuf
[PLPAR_HCALL_BUFSIZE
];
49 /* Make sure the state is valid */
50 rc
= plpar_hcall(H_VASI_STATE
, retbuf
, stream_id
);
52 vasi_state
= retbuf
[0];
55 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc
);
57 } else if (vasi_state
== H_VASI_ENABLED
) {
59 } else if (vasi_state
!= H_VASI_SUSPENDING
) {
60 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
69 * pseries_suspend_cpu - Suspend a single CPU
71 * Makes the H_JOIN call to suspend the CPU
74 static int pseries_suspend_cpu(void)
76 if (atomic_read(&suspending
))
77 return rtas_suspend_cpu(&suspend_data
);
82 * pseries_suspend_enter - Final phase of hibernation
85 * 0 on success / other on failure
87 static int pseries_suspend_enter(suspend_state_t state
)
89 int rc
= rtas_suspend_last_cpu(&suspend_data
);
91 atomic_set(&suspending
, 0);
92 atomic_set(&suspend_data
.done
, 1);
97 * pseries_prepare_late - Prepare to suspend all other CPUs
100 * 0 on success / other on failure
102 static int pseries_prepare_late(void)
104 atomic_set(&suspending
, 1);
105 atomic_set(&suspend_data
.working
, 0);
106 atomic_set(&suspend_data
.done
, 0);
107 atomic_set(&suspend_data
.error
, 0);
108 suspend_data
.complete
= &suspend_work
;
109 reinit_completion(&suspend_work
);
114 * store_hibernate - Initiate partition hibernation
115 * @dev: subsys root device
116 * @attr: device attribute struct
118 * @count: buffer size
120 * Write the stream ID received from the HMC to this file
121 * to trigger hibernating the partition
124 * number of bytes printed to buffer / other on failure
126 static ssize_t
store_hibernate(struct device
*dev
,
127 struct device_attribute
*attr
,
128 const char *buf
, size_t count
)
130 cpumask_var_t offline_mask
;
133 if (!capable(CAP_SYS_ADMIN
))
136 if (!alloc_cpumask_var(&offline_mask
, GFP_TEMPORARY
))
139 stream_id
= simple_strtoul(buf
, NULL
, 16);
142 rc
= pseries_suspend_begin(PM_SUSPEND_MEM
);
145 } while (rc
== -EAGAIN
);
148 /* All present CPUs must be online */
149 cpumask_andnot(offline_mask
, cpu_present_mask
,
151 rc
= rtas_online_cpus_mask(offline_mask
);
153 pr_err("%s: Could not bring present CPUs online.\n",
158 stop_topology_update();
159 rc
= pm_suspend(PM_SUSPEND_MEM
);
160 start_topology_update();
162 /* Take down CPUs not online prior to suspend */
163 if (!rtas_offline_cpus_mask(offline_mask
))
164 pr_warn("%s: Could not restore CPUs to offline "
165 "state.\n", __func__
);
173 free_cpumask_var(offline_mask
);
177 static DEVICE_ATTR(hibernate
, S_IWUSR
, NULL
, store_hibernate
);
179 static struct bus_type suspend_subsys
= {
184 static const struct platform_suspend_ops pseries_suspend_ops
= {
185 .valid
= suspend_valid_only_mem
,
186 .begin
= pseries_suspend_begin
,
187 .prepare_late
= pseries_prepare_late
,
188 .enter
= pseries_suspend_enter
,
192 * pseries_suspend_sysfs_register - Register with sysfs
195 * 0 on success / other on failure
197 static int pseries_suspend_sysfs_register(struct device
*dev
)
201 if ((rc
= subsys_system_register(&suspend_subsys
, NULL
)))
205 dev
->bus
= &suspend_subsys
;
207 if ((rc
= device_create_file(suspend_subsys
.dev_root
, &dev_attr_hibernate
)))
208 goto subsys_unregister
;
213 bus_unregister(&suspend_subsys
);
218 * pseries_suspend_init - initcall for pSeries suspend
221 * 0 on success / other on failure
223 static int __init
pseries_suspend_init(void)
227 if (!machine_is(pseries
) || !firmware_has_feature(FW_FEATURE_LPAR
))
230 suspend_data
.token
= rtas_token("ibm,suspend-me");
231 if (suspend_data
.token
== RTAS_UNKNOWN_SERVICE
)
234 if ((rc
= pseries_suspend_sysfs_register(&suspend_dev
)))
237 ppc_md
.suspend_disable_cpu
= pseries_suspend_cpu
;
238 suspend_set_ops(&pseries_suspend_ops
);
242 __initcall(pseries_suspend_init
);