ACPI / LPSS: register clock device for Lynxpoint DMA properly
[pandora-kernel.git] / arch / powerpc / platforms / pseries / suspend.c
1 /*
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>
21 #include <linux/stat.h>
22 #include <asm/firmware.h>
23 #include <asm/hvcall.h>
24 #include <asm/machdep.h>
25 #include <asm/mmu.h>
26 #include <asm/rtas.h>
27 #include <asm/topology.h>
28
29 static u64 stream_id;
30 static struct device suspend_dev;
31 static DECLARE_COMPLETION(suspend_work);
32 static struct rtas_suspend_me_data suspend_data;
33 static 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  **/
43 static 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  **/
73 static 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  **/
86 static 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  **/
101 static 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
114  * @dev:                subsys root device
115  * @attr:               device attribute struct
116  * @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  **/
125 static ssize_t store_hibernate(struct device *dev,
126                                struct device_attribute *attr,
127                                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
142         if (!rc) {
143                 stop_topology_update();
144                 rc = pm_suspend(PM_SUSPEND_MEM);
145                 start_topology_update();
146         }
147
148         stream_id = 0;
149
150         if (!rc)
151                 rc = count;
152         return rc;
153 }
154
155 static DEVICE_ATTR(hibernate, S_IWUSR, NULL, store_hibernate);
156
157 static struct bus_type suspend_subsys = {
158         .name = "power",
159         .dev_name = "power",
160 };
161
162 static const struct platform_suspend_ops pseries_suspend_ops = {
163         .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  **/
175 static int pseries_suspend_sysfs_register(struct device *dev)
176 {
177         int rc;
178
179         if ((rc = subsys_system_register(&suspend_subsys, NULL)))
180                 return rc;
181
182         dev->id = 0;
183         dev->bus = &suspend_subsys;
184
185         if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
186                 goto subsys_unregister;
187
188         return 0;
189
190 subsys_unregister:
191         bus_unregister(&suspend_subsys);
192         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  **/
201 static 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
212         if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
213                 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);