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>
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
;
35 * pseries_suspend_begin - First phase of hibernation
37 * Check to ensure we are in a valid state to hibernate
40 * 0 on success / other on failure
42 static int pseries_suspend_begin(suspend_state_t state
)
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];
53 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc
);
55 } else if (vasi_state
== H_VASI_ENABLED
) {
57 } else if (vasi_state
!= H_VASI_SUSPENDING
) {
58 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
67 * pseries_suspend_cpu - Suspend a single CPU
69 * Makes the H_JOIN call to suspend the CPU
72 static int pseries_suspend_cpu(void)
74 if (atomic_read(&suspending
))
75 return rtas_suspend_cpu(&suspend_data
);
80 * pseries_suspend_enter - Final phase of hibernation
83 * 0 on success / other on failure
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);
95 * pseries_prepare_late - Prepare to suspend all other CPUs
98 * 0 on success / other on failure
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
);
112 * store_hibernate - Initiate partition hibernation
113 * @classdev: sysdev class struct
114 * @attr: class device attribute struct
116 * @count: buffer size
118 * Write the stream ID received from the HMC to this file
119 * to trigger hibernating the partition
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
)
130 if (!capable(CAP_SYS_ADMIN
))
133 stream_id
= simple_strtoul(buf
, NULL
, 16);
136 rc
= pseries_suspend_begin(PM_SUSPEND_MEM
);
139 } while (rc
== -EAGAIN
);
142 rc
= pm_suspend(PM_SUSPEND_MEM
);
151 static SYSDEV_CLASS_ATTR(hibernate
, S_IWUSR
, NULL
, store_hibernate
);
153 static struct sysdev_class suspend_sysdev_class
= {
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
168 * 0 on success / other on failure
170 static int pseries_suspend_sysfs_register(struct sys_device
*sysdev
)
174 if ((rc
= sysdev_class_register(&suspend_sysdev_class
)))
178 sysdev
->cls
= &suspend_sysdev_class
;
180 if ((rc
= sysdev_class_create_file(&suspend_sysdev_class
, &attr_hibernate
)))
181 goto class_unregister
;
186 sysdev_class_unregister(&suspend_sysdev_class
);
191 * pseries_suspend_init - initcall for pSeries suspend
194 * 0 on success / other on failure
196 static int __init
pseries_suspend_init(void)
200 if (!machine_is(pseries
) || !firmware_has_feature(FW_FEATURE_LPAR
))
203 suspend_data
.token
= rtas_token("ibm,suspend-me");
204 if (suspend_data
.token
== RTAS_UNKNOWN_SERVICE
)
207 if ((rc
= pseries_suspend_sysfs_register(&suspend_sysdev
)))
210 ppc_md
.suspend_disable_cpu
= pseries_suspend_cpu
;
211 suspend_set_ops(&pseries_suspend_ops
);
215 __initcall(pseries_suspend_init
);