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>
29 #include "../../kernel/cacheinfo.h"
32 static struct device suspend_dev
;
33 static DECLARE_COMPLETION(suspend_work
);
34 static struct rtas_suspend_me_data suspend_data
;
35 static atomic_t suspending
;
38 * pseries_suspend_begin - First phase of hibernation
40 * Check to ensure we are in a valid state to hibernate
43 * 0 on success / other on failure
45 static int pseries_suspend_begin(suspend_state_t state
)
48 unsigned long retbuf
[PLPAR_HCALL_BUFSIZE
];
50 /* Make sure the state is valid */
51 rc
= plpar_hcall(H_VASI_STATE
, retbuf
, stream_id
);
53 vasi_state
= retbuf
[0];
56 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc
);
58 } else if (vasi_state
== H_VASI_ENABLED
) {
60 } else if (vasi_state
!= H_VASI_SUSPENDING
) {
61 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
70 * pseries_suspend_cpu - Suspend a single CPU
72 * Makes the H_JOIN call to suspend the CPU
75 static int pseries_suspend_cpu(void)
77 if (atomic_read(&suspending
))
78 return rtas_suspend_cpu(&suspend_data
);
83 * pseries_suspend_enable_irqs
85 * Post suspend configuration updates
88 static void pseries_suspend_enable_irqs(void)
91 * Update configuration which can be modified based on device tree
92 * changes during resume.
94 cacheinfo_cpu_offline(smp_processor_id());
95 post_mobility_fixup();
96 cacheinfo_cpu_online(smp_processor_id());
100 * pseries_suspend_enter - Final phase of hibernation
103 * 0 on success / other on failure
105 static int pseries_suspend_enter(suspend_state_t state
)
107 int rc
= rtas_suspend_last_cpu(&suspend_data
);
109 atomic_set(&suspending
, 0);
110 atomic_set(&suspend_data
.done
, 1);
115 * pseries_prepare_late - Prepare to suspend all other CPUs
118 * 0 on success / other on failure
120 static int pseries_prepare_late(void)
122 atomic_set(&suspending
, 1);
123 atomic_set(&suspend_data
.working
, 0);
124 atomic_set(&suspend_data
.done
, 0);
125 atomic_set(&suspend_data
.error
, 0);
126 suspend_data
.complete
= &suspend_work
;
127 reinit_completion(&suspend_work
);
132 * store_hibernate - Initiate partition hibernation
133 * @dev: subsys root device
134 * @attr: device attribute struct
136 * @count: buffer size
138 * Write the stream ID received from the HMC to this file
139 * to trigger hibernating the partition
142 * number of bytes printed to buffer / other on failure
144 static ssize_t
store_hibernate(struct device
*dev
,
145 struct device_attribute
*attr
,
146 const char *buf
, size_t count
)
148 cpumask_var_t offline_mask
;
151 if (!capable(CAP_SYS_ADMIN
))
154 if (!alloc_cpumask_var(&offline_mask
, GFP_KERNEL
))
157 stream_id
= simple_strtoul(buf
, NULL
, 16);
160 rc
= pseries_suspend_begin(PM_SUSPEND_MEM
);
163 } while (rc
== -EAGAIN
);
166 /* All present CPUs must be online */
167 cpumask_andnot(offline_mask
, cpu_present_mask
,
169 rc
= rtas_online_cpus_mask(offline_mask
);
171 pr_err("%s: Could not bring present CPUs online.\n",
176 stop_topology_update();
177 rc
= pm_suspend(PM_SUSPEND_MEM
);
178 start_topology_update();
180 /* Take down CPUs not online prior to suspend */
181 if (!rtas_offline_cpus_mask(offline_mask
))
182 pr_warn("%s: Could not restore CPUs to offline "
183 "state.\n", __func__
);
191 free_cpumask_var(offline_mask
);
195 #define USER_DT_UPDATE 0
196 #define KERN_DT_UPDATE 1
199 * show_hibernate - Report device tree update responsibilty
200 * @dev: subsys root device
201 * @attr: device attribute struct
204 * Report whether a device tree update is performed by the kernel after a
205 * resume, or if drmgr must coordinate the update from user space.
208 * 0 if drmgr is to initiate update, and 1 otherwise
210 static ssize_t
show_hibernate(struct device
*dev
,
211 struct device_attribute
*attr
,
214 return sprintf(buf
, "%d\n", KERN_DT_UPDATE
);
217 static DEVICE_ATTR(hibernate
, S_IWUSR
| S_IRUGO
,
218 show_hibernate
, store_hibernate
);
220 static struct bus_type suspend_subsys
= {
225 static const struct platform_suspend_ops pseries_suspend_ops
= {
226 .valid
= suspend_valid_only_mem
,
227 .begin
= pseries_suspend_begin
,
228 .prepare_late
= pseries_prepare_late
,
229 .enter
= pseries_suspend_enter
,
233 * pseries_suspend_sysfs_register - Register with sysfs
236 * 0 on success / other on failure
238 static int pseries_suspend_sysfs_register(struct device
*dev
)
242 if ((rc
= subsys_system_register(&suspend_subsys
, NULL
)))
246 dev
->bus
= &suspend_subsys
;
248 if ((rc
= device_create_file(suspend_subsys
.dev_root
, &dev_attr_hibernate
)))
249 goto subsys_unregister
;
254 bus_unregister(&suspend_subsys
);
259 * pseries_suspend_init - initcall for pSeries suspend
262 * 0 on success / other on failure
264 static int __init
pseries_suspend_init(void)
268 if (!firmware_has_feature(FW_FEATURE_LPAR
))
271 suspend_data
.token
= rtas_token("ibm,suspend-me");
272 if (suspend_data
.token
== RTAS_UNKNOWN_SERVICE
)
275 if ((rc
= pseries_suspend_sysfs_register(&suspend_dev
)))
278 ppc_md
.suspend_disable_cpu
= pseries_suspend_cpu
;
279 ppc_md
.suspend_enable_irqs
= pseries_suspend_enable_irqs
;
280 suspend_set_ops(&pseries_suspend_ops
);
283 machine_device_initcall(pseries
, pseries_suspend_init
);