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 <asm/firmware.h>
22 #include <asm/hvcall.h>
23 #include <asm/machdep.h>
28 static struct sys_device suspend_sysdev
;
29 static DECLARE_COMPLETION(suspend_work
);
30 static struct rtas_suspend_me_data suspend_data
;
31 static atomic_t suspending
;
34 * pseries_suspend_begin - First phase of hibernation
36 * Check to ensure we are in a valid state to hibernate
39 * 0 on success / other on failure
41 static int pseries_suspend_begin(suspend_state_t state
)
44 unsigned long retbuf
[PLPAR_HCALL_BUFSIZE
];
46 /* Make sure the state is valid */
47 rc
= plpar_hcall(H_VASI_STATE
, retbuf
, stream_id
);
49 vasi_state
= retbuf
[0];
52 pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc
);
54 } else if (vasi_state
== H_VASI_ENABLED
) {
56 } else if (vasi_state
!= H_VASI_SUSPENDING
) {
57 pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
66 * pseries_suspend_cpu - Suspend a single CPU
68 * Makes the H_JOIN call to suspend the CPU
71 static int pseries_suspend_cpu(void)
73 if (atomic_read(&suspending
))
74 return rtas_suspend_cpu(&suspend_data
);
79 * pseries_suspend_enter - Final phase of hibernation
82 * 0 on success / other on failure
84 static int pseries_suspend_enter(suspend_state_t state
)
86 int rc
= rtas_suspend_last_cpu(&suspend_data
);
88 atomic_set(&suspending
, 0);
89 atomic_set(&suspend_data
.done
, 1);
94 * pseries_prepare_late - Prepare to suspend all other CPUs
97 * 0 on success / other on failure
99 static int pseries_prepare_late(void)
101 atomic_set(&suspending
, 1);
102 atomic_set(&suspend_data
.working
, 0);
103 atomic_set(&suspend_data
.done
, 0);
104 atomic_set(&suspend_data
.error
, 0);
105 suspend_data
.complete
= &suspend_work
;
106 INIT_COMPLETION(suspend_work
);
111 * store_hibernate - Initiate partition hibernation
112 * @classdev: sysdev class struct
113 * @attr: class device attribute struct
115 * @count: buffer size
117 * Write the stream ID received from the HMC to this file
118 * to trigger hibernating the partition
121 * number of bytes printed to buffer / other on failure
123 static ssize_t
store_hibernate(struct sysdev_class
*classdev
,
124 struct sysdev_class_attribute
*attr
,
125 const char *buf
, size_t count
)
129 if (!capable(CAP_SYS_ADMIN
))
132 stream_id
= simple_strtoul(buf
, NULL
, 16);
135 rc
= pseries_suspend_begin(PM_SUSPEND_MEM
);
138 } while (rc
== -EAGAIN
);
141 rc
= pm_suspend(PM_SUSPEND_MEM
);
150 static SYSDEV_CLASS_ATTR(hibernate
, S_IWUSR
, NULL
, store_hibernate
);
152 static struct sysdev_class suspend_sysdev_class
= {
156 static struct platform_suspend_ops pseries_suspend_ops
= {
157 .valid
= suspend_valid_only_mem
,
158 .begin
= pseries_suspend_begin
,
159 .prepare_late
= pseries_prepare_late
,
160 .enter
= pseries_suspend_enter
,
164 * pseries_suspend_sysfs_register - Register with sysfs
167 * 0 on success / other on failure
169 static int pseries_suspend_sysfs_register(struct sys_device
*sysdev
)
173 if ((rc
= sysdev_class_register(&suspend_sysdev_class
)))
177 sysdev
->cls
= &suspend_sysdev_class
;
179 if ((rc
= sysdev_class_create_file(&suspend_sysdev_class
, &attr_hibernate
)))
180 goto class_unregister
;
185 sysdev_class_unregister(&suspend_sysdev_class
);
190 * pseries_suspend_init - initcall for pSeries suspend
193 * 0 on success / other on failure
195 static int __init
pseries_suspend_init(void)
199 if (!machine_is(pseries
) || !firmware_has_feature(FW_FEATURE_LPAR
))
202 suspend_data
.token
= rtas_token("ibm,suspend-me");
203 if (suspend_data
.token
== RTAS_UNKNOWN_SERVICE
)
206 if ((rc
= pseries_suspend_sysfs_register(&suspend_sysdev
)))
209 ppc_md
.suspend_disable_cpu
= pseries_suspend_cpu
;
210 suspend_set_ops(&pseries_suspend_ops
);
214 __initcall(pseries_suspend_init
);