Blame view

drivers/ps3/ps3-sys-manager.c 20.4 KB
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  /*
   *  PS3 System Manager.
   *
   *  Copyright (C) 2007 Sony Computer Entertainment Inc.
   *  Copyright 2007 Sony Corp.
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; version 2 of the License.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program; if not, write to the Free Software
   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   */
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/workqueue.h>
  #include <linux/reboot.h>
ef596c697   Geert Uytterhoeven   [POWERPC] ps3: al...
25
26
  
  #include <asm/firmware.h>
ca052f792   Geert Uytterhoeven   [POWERPC] PS3: Sa...
27
  #include <asm/lv1call.h>
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
28
  #include <asm/ps3.h>
ef596c697   Geert Uytterhoeven   [POWERPC] ps3: al...
29

fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
30
  #include "vuart.h"
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
31
32
33
  /**
   * ps3_sys_manager - PS3 system manager driver.
   *
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
34
   * The system manager provides an asynchronous system event notification
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
35
36
37
38
39
40
41
42
43
44
45
46
47
   * mechanism for reporting events like thermal alert and button presses to
   * guests.  It also provides support to control system shutdown and startup.
   *
   * The actual system manager is implemented as an application running in the
   * system policy module in lpar_1.  Guests communicate with the system manager
   * through port 2 of the vuart using a simple packet message protocol.
   * Messages are comprised of a fixed field header followed by a message
   * specific payload.
   */
  
  /**
   * struct ps3_sys_manager_header - System manager message header.
   * @version: Header version, currently 1.
af901ca18   AndrĂ© Goddard Rosa   tree-wide: fix as...
48
   * @size: Header size in bytes, currently 16.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
49
50
   * @payload_size: Message payload size in bytes.
   * @service_id: Message type, one of enum ps3_sys_manager_service_id.
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
51
   * @request_tag: Unique number to identify reply.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
52
53
54
55
56
57
58
59
60
   */
  
  struct ps3_sys_manager_header {
  	/* version 1 */
  	u8 version;
  	u8 size;
  	u16 reserved_1;
  	u32 payload_size;
  	u16 service_id;
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
61
62
  	u16 reserved_2;
  	u32 request_tag;
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
63
  };
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #define dump_sm_header(_h) _dump_sm_header(_h, __func__, __LINE__)
  static void __maybe_unused _dump_sm_header(
  	const struct ps3_sys_manager_header *h, const char *func, int line)
  {
  	pr_debug("%s:%d: version:      %xh
  ", func, line, h->version);
  	pr_debug("%s:%d: size:         %xh
  ", func, line, h->size);
  	pr_debug("%s:%d: payload_size: %xh
  ", func, line, h->payload_size);
  	pr_debug("%s:%d: service_id:   %xh
  ", func, line, h->service_id);
  	pr_debug("%s:%d: request_tag:  %xh
  ", func, line, h->request_tag);
  }
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
79
  /**
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
80
81
   * @PS3_SM_RX_MSG_LEN_MIN - Shortest received message length.
   * @PS3_SM_RX_MSG_LEN_MAX - Longest received message length.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
82
   *
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
83
84
   * Currently all messages received from the system manager are either
   * (16 bytes header + 8 bytes payload = 24 bytes) or (16 bytes header
25985edce   Lucas De Marchi   Fix common misspe...
85
   * + 16 bytes payload = 32 bytes).  This knowledge is used to simplify
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
86
   * the logic.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
87
88
89
   */
  
  enum {
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
90
91
  	PS3_SM_RX_MSG_LEN_MIN = 24,
  	PS3_SM_RX_MSG_LEN_MAX = 32,
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
92
93
94
95
  };
  
  /**
   * enum ps3_sys_manager_service_id - Message header service_id.
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
96
97
98
99
100
101
102
103
104
105
106
   * @PS3_SM_SERVICE_ID_REQUEST:       guest --> sys_manager.
   * @PS3_SM_SERVICE_ID_REQUEST_ERROR: guest <-- sys_manager.
   * @PS3_SM_SERVICE_ID_COMMAND:       guest <-- sys_manager.
   * @PS3_SM_SERVICE_ID_RESPONSE:      guest --> sys_manager.
   * @PS3_SM_SERVICE_ID_SET_ATTR:      guest --> sys_manager.
   * @PS3_SM_SERVICE_ID_EXTERN_EVENT:  guest <-- sys_manager.
   * @PS3_SM_SERVICE_ID_SET_NEXT_OP:   guest --> sys_manager.
   *
   * PS3_SM_SERVICE_ID_REQUEST_ERROR is returned for invalid data values in a
   * a PS3_SM_SERVICE_ID_REQUEST message.  It also seems to be returned when
   * a REQUEST message is sent at the wrong time.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
107
108
109
110
111
112
113
114
115
   */
  
  enum ps3_sys_manager_service_id {
  	/* version 1 */
  	PS3_SM_SERVICE_ID_REQUEST = 1,
  	PS3_SM_SERVICE_ID_RESPONSE = 2,
  	PS3_SM_SERVICE_ID_COMMAND = 3,
  	PS3_SM_SERVICE_ID_EXTERN_EVENT = 4,
  	PS3_SM_SERVICE_ID_SET_NEXT_OP = 5,
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
116
  	PS3_SM_SERVICE_ID_REQUEST_ERROR = 6,
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
117
118
119
120
121
122
123
  	PS3_SM_SERVICE_ID_SET_ATTR = 8,
  };
  
  /**
   * enum ps3_sys_manager_attr - Notification attribute (bit position mask).
   * @PS3_SM_ATTR_POWER: Power button.
   * @PS3_SM_ATTR_RESET: Reset button, not available on retail console.
883931612   Thomas Weber   Fix typos in comm...
124
   * @PS3_SM_ATTR_THERMAL: System thermal alert.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
   * @PS3_SM_ATTR_CONTROLLER: Remote controller event.
   * @PS3_SM_ATTR_ALL: Logical OR of all.
   *
   * The guest tells the system manager which events it is interested in receiving
   * notice of by sending the system manager a logical OR of notification
   * attributes via the ps3_sys_manager_send_attr() routine.
   */
  
  enum ps3_sys_manager_attr {
  	/* version 1 */
  	PS3_SM_ATTR_POWER = 1,
  	PS3_SM_ATTR_RESET = 2,
  	PS3_SM_ATTR_THERMAL = 4,
  	PS3_SM_ATTR_CONTROLLER = 8, /* bogus? */
  	PS3_SM_ATTR_ALL = 0x0f,
  };
  
  /**
   * enum ps3_sys_manager_event - External event type, reported by system manager.
ea24608f0   Geoff Levand   [POWERPC] PS3: Up...
144
145
   * @PS3_SM_EVENT_POWER_PRESSED: payload.value =
   *  enum ps3_sys_manager_button_event.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
146
   * @PS3_SM_EVENT_POWER_RELEASED: payload.value = time pressed in millisec.
ea24608f0   Geoff Levand   [POWERPC] PS3: Up...
147
148
   * @PS3_SM_EVENT_RESET_PRESSED: payload.value =
   *  enum ps3_sys_manager_button_event.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
   * @PS3_SM_EVENT_RESET_RELEASED: payload.value = time pressed in millisec.
   * @PS3_SM_EVENT_THERMAL_ALERT: payload.value = thermal zone id.
   * @PS3_SM_EVENT_THERMAL_CLEARED: payload.value = thermal zone id.
   */
  
  enum ps3_sys_manager_event {
  	/* version 1 */
  	PS3_SM_EVENT_POWER_PRESSED = 3,
  	PS3_SM_EVENT_POWER_RELEASED = 4,
  	PS3_SM_EVENT_RESET_PRESSED = 5,
  	PS3_SM_EVENT_RESET_RELEASED = 6,
  	PS3_SM_EVENT_THERMAL_ALERT = 7,
  	PS3_SM_EVENT_THERMAL_CLEARED = 8,
  	/* no info on controller events */
  };
  
  /**
ea24608f0   Geoff Levand   [POWERPC] PS3: Up...
166
167
168
169
170
171
172
173
174
175
176
   * enum ps3_sys_manager_button_event - Button event payload values.
   * @PS3_SM_BUTTON_EVENT_HARD: Hardware generated event.
   * @PS3_SM_BUTTON_EVENT_SOFT: Software generated event.
   */
  
  enum ps3_sys_manager_button_event {
  	PS3_SM_BUTTON_EVENT_HARD = 0,
  	PS3_SM_BUTTON_EVENT_SOFT = 1,
  };
  
  /**
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
177
178
179
180
181
182
183
184
185
186
187
188
   * enum ps3_sys_manager_next_op - Operation to perform after lpar is destroyed.
   */
  
  enum ps3_sys_manager_next_op {
  	/* version 3 */
  	PS3_SM_NEXT_OP_SYS_SHUTDOWN = 1,
  	PS3_SM_NEXT_OP_SYS_REBOOT = 2,
  	PS3_SM_NEXT_OP_LPAR_REBOOT = 0x82,
  };
  
  /**
   * enum ps3_sys_manager_wake_source - Next-op wakeup source (bit position mask).
5442381cd   Geoff Levand   [POWERPC] PS3: Re...
189
   * @PS3_SM_WAKE_DEFAULT: Disk insert, power button, eject button.
1c43d265f   Geoff Levand   [POWERPC] PS3: Sy...
190
   * @PS3_SM_WAKE_W_O_L: Ether or wireless LAN.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
191
192
193
   * @PS3_SM_WAKE_P_O_R: Power on reset.
   *
   * Additional wakeup sources when specifying PS3_SM_NEXT_OP_SYS_SHUTDOWN.
50dad9026   Geoff Levand   [POWERPC] PS3: Sy...
194
195
196
   * The system will always wake from the PS3_SM_WAKE_DEFAULT sources.
   * Sources listed here are the only ones available to guests in the
   * other-os lpar.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
197
198
199
200
201
   */
  
  enum ps3_sys_manager_wake_source {
  	/* version 3 */
  	PS3_SM_WAKE_DEFAULT   = 0,
1c43d265f   Geoff Levand   [POWERPC] PS3: Sy...
202
  	PS3_SM_WAKE_W_O_L     = 0x00000400,
50dad9026   Geoff Levand   [POWERPC] PS3: Sy...
203
  	PS3_SM_WAKE_P_O_R     = 0x80000000,
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
204
205
206
  };
  
  /**
1c43d265f   Geoff Levand   [POWERPC] PS3: Sy...
207
208
209
210
211
212
213
214
   * user_wake_sources - User specified wakeup sources.
   *
   * Logical OR of enum ps3_sys_manager_wake_source types.
   */
  
  static u32 user_wake_sources = PS3_SM_WAKE_DEFAULT;
  
  /**
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
215
216
217
218
219
220
221
222
223
224
225
226
227
228
   * enum ps3_sys_manager_cmd - Command from system manager to guest.
   *
   * The guest completes the actions needed, then acks or naks the command via
   * ps3_sys_manager_send_response().  In the case of @PS3_SM_CMD_SHUTDOWN,
   * the guest must be fully prepared for a system poweroff prior to acking the
   * command.
   */
  
  enum ps3_sys_manager_cmd {
  	/* version 1 */
  	PS3_SM_CMD_SHUTDOWN = 1, /* shutdown guest OS */
  };
  
  /**
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
229
230
231
232
233
234
235
236
237
238
   * ps3_sm_force_power_off - Poweroff helper.
   *
   * A global variable used to force a poweroff when the power button has
   * been pressed irrespective of how init handles the ctrl_alt_del signal.
   *
   */
  
  static unsigned int ps3_sm_force_power_off;
  
  /**
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
239
240
241
   * ps3_sys_manager_write - Helper to write a two part message to the vuart.
   *
   */
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
242
  static int ps3_sys_manager_write(struct ps3_system_bus_device *dev,
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  	const struct ps3_sys_manager_header *header, const void *payload)
  {
  	int result;
  
  	BUG_ON(header->version != 1);
  	BUG_ON(header->size != 16);
  	BUG_ON(header->payload_size != 8 && header->payload_size != 16);
  	BUG_ON(header->service_id > 8);
  
  	result = ps3_vuart_write(dev, header,
  		sizeof(struct ps3_sys_manager_header));
  
  	if (!result)
  		result = ps3_vuart_write(dev, payload, header->payload_size);
  
  	return result;
  }
  
  /**
   * ps3_sys_manager_send_attr - Send a 'set attribute' to the system manager.
   *
   */
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
265
  static int ps3_sys_manager_send_attr(struct ps3_system_bus_device *dev,
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
266
267
  	enum ps3_sys_manager_attr attr)
  {
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
268
  	struct ps3_sys_manager_header header;
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
269
270
271
272
273
274
275
276
277
278
  	struct {
  		u8 version;
  		u8 reserved_1[3];
  		u32 attribute;
  	} payload;
  
  	BUILD_BUG_ON(sizeof(payload) != 8);
  
  	dev_dbg(&dev->core, "%s:%d: %xh
  ", __func__, __LINE__, attr);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
279
280
281
282
283
  	memset(&header, 0, sizeof(header));
  	header.version = 1;
  	header.size = 16;
  	header.payload_size = 16;
  	header.service_id = PS3_SM_SERVICE_ID_SET_ATTR;
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
284
285
286
287
288
289
290
291
292
293
294
295
  	memset(&payload, 0, sizeof(payload));
  	payload.version = 1;
  	payload.attribute = attr;
  
  	return ps3_sys_manager_write(dev, &header, &payload);
  }
  
  /**
   * ps3_sys_manager_send_next_op - Send a 'set next op' to the system manager.
   *
   * Tell the system manager what to do after this lpar is destroyed.
   */
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
296
  static int ps3_sys_manager_send_next_op(struct ps3_system_bus_device *dev,
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
297
298
299
  	enum ps3_sys_manager_next_op op,
  	enum ps3_sys_manager_wake_source wake_source)
  {
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
300
  	struct ps3_sys_manager_header header;
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
301
302
303
304
305
306
307
308
309
310
311
312
313
  	struct {
  		u8 version;
  		u8 type;
  		u8 gos_id;
  		u8 reserved_1;
  		u32 wake_source;
  		u8 reserved_2[8];
  	} payload;
  
  	BUILD_BUG_ON(sizeof(payload) != 16);
  
  	dev_dbg(&dev->core, "%s:%d: (%xh)
  ", __func__, __LINE__, op);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
314
315
316
317
318
  	memset(&header, 0, sizeof(header));
  	header.version = 1;
  	header.size = 16;
  	header.payload_size = 16;
  	header.service_id = PS3_SM_SERVICE_ID_SET_NEXT_OP;
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
  	memset(&payload, 0, sizeof(payload));
  	payload.version = 3;
  	payload.type = op;
  	payload.gos_id = 3; /* other os */
  	payload.wake_source = wake_source;
  
  	return ps3_sys_manager_write(dev, &header, &payload);
  }
  
  /**
   * ps3_sys_manager_send_request_shutdown - Send 'request' to the system manager.
   *
   * The guest sends this message to request an operation or action of the system
   * manager.  The reply is a command message from the system manager.  In the
   * command handler the guest performs the requested operation.  The result of
   * the command is then communicated back to the system manager with a response
   * message.
   *
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
337
   * Currently, the only supported request is the 'shutdown self' request.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
338
   */
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
339
340
  static int ps3_sys_manager_send_request_shutdown(
  	struct ps3_system_bus_device *dev)
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
341
  {
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
342
  	struct ps3_sys_manager_header header;
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
343
344
345
346
347
  	struct {
  		u8 version;
  		u8 type;
  		u8 gos_id;
  		u8 reserved_1[13];
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
348
  	} payload;
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
349
350
351
352
353
  
  	BUILD_BUG_ON(sizeof(payload) != 16);
  
  	dev_dbg(&dev->core, "%s:%d
  ", __func__, __LINE__);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
354
355
356
357
358
359
360
361
362
363
  	memset(&header, 0, sizeof(header));
  	header.version = 1;
  	header.size = 16;
  	header.payload_size = 16;
  	header.service_id = PS3_SM_SERVICE_ID_REQUEST;
  
  	memset(&payload, 0, sizeof(payload));
  	payload.version = 1;
  	payload.type = 1; /* shutdown */
  	payload.gos_id = 0; /* self */
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
364
365
366
367
368
369
370
371
372
373
  	return ps3_sys_manager_write(dev, &header, &payload);
  }
  
  /**
   * ps3_sys_manager_send_response - Send a 'response' to the system manager.
   * @status: zero = success, others fail.
   *
   * The guest sends this message to the system manager to acnowledge success or
   * failure of a command sent by the system manager.
   */
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
374
  static int ps3_sys_manager_send_response(struct ps3_system_bus_device *dev,
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
375
376
  	u64 status)
  {
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
377
  	struct ps3_sys_manager_header header;
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
378
379
380
381
382
383
384
385
386
387
388
389
  	struct {
  		u8 version;
  		u8 reserved_1[3];
  		u8 status;
  		u8 reserved_2[11];
  	} payload;
  
  	BUILD_BUG_ON(sizeof(payload) != 16);
  
  	dev_dbg(&dev->core, "%s:%d: (%s)
  ", __func__, __LINE__,
  		(status ? "nak" : "ack"));
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
390
391
392
393
394
  	memset(&header, 0, sizeof(header));
  	header.version = 1;
  	header.size = 16;
  	header.payload_size = 16;
  	header.service_id = PS3_SM_SERVICE_ID_RESPONSE;
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
395
396
397
398
399
400
401
402
403
404
405
  	memset(&payload, 0, sizeof(payload));
  	payload.version = 1;
  	payload.status = status;
  
  	return ps3_sys_manager_write(dev, &header, &payload);
  }
  
  /**
   * ps3_sys_manager_handle_event - Second stage event msg handler.
   *
   */
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
406
  static int ps3_sys_manager_handle_event(struct ps3_system_bus_device *dev)
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
407
408
409
410
411
412
413
414
415
416
417
418
419
  {
  	int result;
  	struct {
  		u8 version;
  		u8 type;
  		u8 reserved_1[2];
  		u32 value;
  		u8 reserved_2[8];
  	} event;
  
  	BUILD_BUG_ON(sizeof(event) != 16);
  
  	result = ps3_vuart_read(dev, &event, sizeof(event));
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
420
  	BUG_ON(result && "need to retry here");
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
421
422
423
424
425
426
427
428
429
430
  
  	if (event.version != 1) {
  		dev_dbg(&dev->core, "%s:%d: unsupported event version (%u)
  ",
  			__func__, __LINE__, event.version);
  		return -EIO;
  	}
  
  	switch (event.type) {
  	case PS3_SM_EVENT_POWER_PRESSED:
ea24608f0   Geoff Levand   [POWERPC] PS3: Up...
431
432
433
434
435
  		dev_dbg(&dev->core, "%s:%d: POWER_PRESSED (%s)
  ",
  			__func__, __LINE__,
  			(event.value == PS3_SM_BUTTON_EVENT_SOFT ? "soft"
  			: "hard"));
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
436
437
438
439
440
441
442
443
  		ps3_sm_force_power_off = 1;
  		/*
  		 * A memory barrier is use here to sync memory since
  		 * ps3_sys_manager_final_restart() could be called on
  		 * another cpu.
  		 */
  		wmb();
  		kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
444
445
446
447
448
  		break;
  	case PS3_SM_EVENT_POWER_RELEASED:
  		dev_dbg(&dev->core, "%s:%d: POWER_RELEASED (%u ms)
  ",
  			__func__, __LINE__, event.value);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
449
450
  		break;
  	case PS3_SM_EVENT_RESET_PRESSED:
ea24608f0   Geoff Levand   [POWERPC] PS3: Up...
451
452
453
454
455
  		dev_dbg(&dev->core, "%s:%d: RESET_PRESSED (%s)
  ",
  			__func__, __LINE__,
  			(event.value == PS3_SM_BUTTON_EVENT_SOFT ? "soft"
  			: "hard"));
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
456
457
458
459
460
461
462
463
464
465
466
467
468
  		ps3_sm_force_power_off = 0;
  		/*
  		 * A memory barrier is use here to sync memory since
  		 * ps3_sys_manager_final_restart() could be called on
  		 * another cpu.
  		 */
  		wmb();
  		kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */
  		break;
  	case PS3_SM_EVENT_RESET_RELEASED:
  		dev_dbg(&dev->core, "%s:%d: RESET_RELEASED (%u ms)
  ",
  			__func__, __LINE__, event.value);
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
469
470
471
472
473
  		break;
  	case PS3_SM_EVENT_THERMAL_ALERT:
  		dev_dbg(&dev->core, "%s:%d: THERMAL_ALERT (zone %u)
  ",
  			__func__, __LINE__, event.value);
eff56c92a   Geert Uytterhoeven   [POWERPC] PS3: Ch...
474
475
  		pr_info("PS3 Thermal Alert Zone %u
  ", event.value);
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  		break;
  	case PS3_SM_EVENT_THERMAL_CLEARED:
  		dev_dbg(&dev->core, "%s:%d: THERMAL_CLEARED (zone %u)
  ",
  			__func__, __LINE__, event.value);
  		break;
  	default:
  		dev_dbg(&dev->core, "%s:%d: unknown event (%u)
  ",
  			__func__, __LINE__, event.type);
  		return -EIO;
  	}
  
  	return 0;
  }
  /**
   * ps3_sys_manager_handle_cmd - Second stage command msg handler.
   *
   * The system manager sends this in reply to a 'request' message from the guest.
   */
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
496
  static int ps3_sys_manager_handle_cmd(struct ps3_system_bus_device *dev)
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
497
498
499
500
501
502
503
504
505
506
507
508
509
510
  {
  	int result;
  	struct {
  		u8 version;
  		u8 type;
  		u8 reserved_1[14];
  	} cmd;
  
  	BUILD_BUG_ON(sizeof(cmd) != 16);
  
  	dev_dbg(&dev->core, "%s:%d
  ", __func__, __LINE__);
  
  	result = ps3_vuart_read(dev, &cmd, sizeof(cmd));
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
511
  	BUG_ON(result && "need to retry here");
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
512

eff56c92a   Geert Uytterhoeven   [POWERPC] PS3: Ch...
513
  	if (result)
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
  		return result;
  
  	if (cmd.version != 1) {
  		dev_dbg(&dev->core, "%s:%d: unsupported cmd version (%u)
  ",
  			__func__, __LINE__, cmd.version);
  		return -EIO;
  	}
  
  	if (cmd.type != PS3_SM_CMD_SHUTDOWN) {
  		dev_dbg(&dev->core, "%s:%d: unknown cmd (%u)
  ",
  			__func__, __LINE__, cmd.type);
  		return -EIO;
  	}
  
  	ps3_sys_manager_send_response(dev, 0);
  	return 0;
  }
  
  /**
   * ps3_sys_manager_handle_msg - First stage msg handler.
   *
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
537
   * Can be called directly to manually poll vuart and pump message handler.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
538
   */
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
539
  static int ps3_sys_manager_handle_msg(struct ps3_system_bus_device *dev)
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
540
541
542
543
544
545
  {
  	int result;
  	struct ps3_sys_manager_header header;
  
  	result = ps3_vuart_read(dev, &header,
  		sizeof(struct ps3_sys_manager_header));
eff56c92a   Geert Uytterhoeven   [POWERPC] PS3: Ch...
546
  	if (result)
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
547
548
549
550
551
552
  		return result;
  
  	if (header.version != 1) {
  		dev_dbg(&dev->core, "%s:%d: unsupported header version (%u)
  ",
  			__func__, __LINE__, header.version);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
553
  		dump_sm_header(&header);
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
554
555
556
557
  		goto fail_header;
  	}
  
  	BUILD_BUG_ON(sizeof(header) != 16);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
558
559
560
561
562
563
  
  	if (header.size != 16 || (header.payload_size != 8
  		&& header.payload_size != 16)) {
  		dump_sm_header(&header);
  		BUG();
  	}
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
564
565
566
567
568
569
570
571
572
573
  
  	switch (header.service_id) {
  	case PS3_SM_SERVICE_ID_EXTERN_EVENT:
  		dev_dbg(&dev->core, "%s:%d: EVENT
  ", __func__, __LINE__);
  		return ps3_sys_manager_handle_event(dev);
  	case PS3_SM_SERVICE_ID_COMMAND:
  		dev_dbg(&dev->core, "%s:%d: COMMAND
  ", __func__, __LINE__);
  		return ps3_sys_manager_handle_cmd(dev);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
574
575
576
577
578
579
  	case PS3_SM_SERVICE_ID_REQUEST_ERROR:
  		dev_dbg(&dev->core, "%s:%d: REQUEST_ERROR
  ", __func__,
  			__LINE__);
  		dump_sm_header(&header);
  		break;
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
  	default:
  		dev_dbg(&dev->core, "%s:%d: unknown service_id (%u)
  ",
  			__func__, __LINE__, header.service_id);
  		break;
  	}
  	goto fail_id;
  
  fail_header:
  	ps3_vuart_clear_rx_bytes(dev, 0);
  	return -EIO;
  fail_id:
  	ps3_vuart_clear_rx_bytes(dev, header.payload_size);
  	return -EIO;
  }
ca052f792   Geert Uytterhoeven   [POWERPC] PS3: Sa...
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
  static void ps3_sys_manager_fin(struct ps3_system_bus_device *dev)
  {
  	ps3_sys_manager_send_request_shutdown(dev);
  
  	pr_emerg("System Halted, OK to turn off power
  ");
  
  	while (ps3_sys_manager_handle_msg(dev)) {
  		/* pause until next DEC interrupt */
  		lv1_pause(0);
  	}
  
  	while (1) {
  		/* pause, ignoring DEC interrupt */
  		lv1_pause(1);
  	}
  }
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
612
  /**
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
613
   * ps3_sys_manager_final_power_off - The final platform machine_power_off routine.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
614
   *
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
615
   * This routine never returns.  The routine disables asynchronous vuart reads
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
616
617
618
   * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
   * the shutdown command sent from the system manager.  Soon after the
   * acknowledgement is sent the lpar is destroyed by the HV.  This routine
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
619
620
   * should only be called from ps3_power_off() through
   * ps3_sys_manager_ops.power_off.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
621
   */
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
622
  static void ps3_sys_manager_final_power_off(struct ps3_system_bus_device *dev)
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
623
  {
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
624
  	BUG_ON(!dev);
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
625
626
627
628
629
  
  	dev_dbg(&dev->core, "%s:%d
  ", __func__, __LINE__);
  
  	ps3_vuart_cancel_async(dev);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
630
  	ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_SHUTDOWN,
1c43d265f   Geoff Levand   [POWERPC] PS3: Sy...
631
  		user_wake_sources);
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
632

ca052f792   Geert Uytterhoeven   [POWERPC] PS3: Sa...
633
  	ps3_sys_manager_fin(dev);
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
634
635
636
  }
  
  /**
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
637
   * ps3_sys_manager_final_restart - The final platform machine_restart routine.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
638
   *
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
639
   * This routine never returns.  The routine disables asynchronous vuart reads
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
640
641
642
   * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
   * the shutdown command sent from the system manager.  Soon after the
   * acknowledgement is sent the lpar is destroyed by the HV.  This routine
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
643
   * should only be called from ps3_restart() through ps3_sys_manager_ops.restart.
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
644
   */
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
645
  static void ps3_sys_manager_final_restart(struct ps3_system_bus_device *dev)
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
646
  {
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
647
  	BUG_ON(!dev);
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
648
649
650
  
  	dev_dbg(&dev->core, "%s:%d
  ", __func__, __LINE__);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
651
652
653
654
655
656
657
658
  	/* Check if we got here via a power button event. */
  
  	if (ps3_sm_force_power_off) {
  		dev_dbg(&dev->core, "%s:%d: forcing poweroff
  ",
  			__func__, __LINE__);
  		ps3_sys_manager_final_power_off(dev);
  	}
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
659
  	ps3_vuart_cancel_async(dev);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
660
  	ps3_sys_manager_send_attr(dev, 0);
75ffe88d2   Geoff Levand   [POWERPC] PS3: Us...
661
  	ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_REBOOT,
1c43d265f   Geoff Levand   [POWERPC] PS3: Sy...
662
  		user_wake_sources);
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
663

ca052f792   Geert Uytterhoeven   [POWERPC] PS3: Sa...
664
  	ps3_sys_manager_fin(dev);
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
665
  }
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
666
  /**
1c43d265f   Geoff Levand   [POWERPC] PS3: Sy...
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
   * ps3_sys_manager_get_wol - Get wake-on-lan setting.
   */
  
  int ps3_sys_manager_get_wol(void)
  {
  	pr_debug("%s:%d
  ", __func__, __LINE__);
  
  	return (user_wake_sources & PS3_SM_WAKE_W_O_L) != 0;
  }
  EXPORT_SYMBOL_GPL(ps3_sys_manager_get_wol);
  
  /**
   * ps3_sys_manager_set_wol - Set wake-on-lan setting.
   */
  
  void ps3_sys_manager_set_wol(int state)
  {
  	static DEFINE_MUTEX(mutex);
  
  	mutex_lock(&mutex);
  
  	pr_debug("%s:%d: %d
  ", __func__, __LINE__, state);
  
  	if (state)
  		user_wake_sources |= PS3_SM_WAKE_W_O_L;
  	else
  		user_wake_sources &= ~PS3_SM_WAKE_W_O_L;
  	mutex_unlock(&mutex);
  }
  EXPORT_SYMBOL_GPL(ps3_sys_manager_set_wol);
  
  /**
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
701
702
703
704
705
706
707
708
709
710
   * ps3_sys_manager_work - Asynchronous read handler.
   *
   * Signaled when PS3_SM_RX_MSG_LEN_MIN bytes arrive at the vuart port.
   */
  
  static void ps3_sys_manager_work(struct ps3_system_bus_device *dev)
  {
  	ps3_sys_manager_handle_msg(dev);
  	ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN);
  }
0fe763c57   Greg Kroah-Hartman   Drivers: misc: re...
711
  static int ps3_sys_manager_probe(struct ps3_system_bus_device *dev)
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
712
713
  {
  	int result;
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
714
  	struct ps3_sys_manager_ops ops;
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
715
716
717
  
  	dev_dbg(&dev->core, "%s:%d
  ", __func__, __LINE__);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
718
719
720
721
722
723
724
  	ops.power_off = ps3_sys_manager_final_power_off;
  	ops.restart = ps3_sys_manager_final_restart;
  	ops.dev = dev;
  
  	/* ps3_sys_manager_register_ops copies ops. */
  
  	ps3_sys_manager_register_ops(&ops);
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
725
726
727
  
  	result = ps3_sys_manager_send_attr(dev, PS3_SM_ATTR_ALL);
  	BUG_ON(result);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
728
  	result = ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN);
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
729
730
731
732
  	BUG_ON(result);
  
  	return result;
  }
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
733
734
735
736
737
738
739
740
741
742
743
744
  static int ps3_sys_manager_remove(struct ps3_system_bus_device *dev)
  {
  	dev_dbg(&dev->core, "%s:%d
  ", __func__, __LINE__);
  	return 0;
  }
  
  static void ps3_sys_manager_shutdown(struct ps3_system_bus_device *dev)
  {
  	dev_dbg(&dev->core, "%s:%d
  ", __func__, __LINE__);
  }
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
745
  static struct ps3_vuart_port_driver ps3_sys_manager = {
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
746
747
  	.core.match_id = PS3_MATCH_ID_SYSTEM_MANAGER,
  	.core.core.name = "ps3_sys_manager",
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
748
  	.probe = ps3_sys_manager_probe,
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
749
750
751
  	.remove = ps3_sys_manager_remove,
  	.shutdown = ps3_sys_manager_shutdown,
  	.work = ps3_sys_manager_work,
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
752
753
754
755
  };
  
  static int __init ps3_sys_manager_init(void)
  {
ef596c697   Geert Uytterhoeven   [POWERPC] ps3: al...
756
757
  	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  		return -ENODEV;
fde5efd0e   Geoff Levand   [POWERPC] PS3: Sy...
758
759
760
761
  	return ps3_vuart_port_driver_register(&ps3_sys_manager);
  }
  
  module_init(ps3_sys_manager_init);
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
762
  /* Module remove not supported. */
50dad9026   Geoff Levand   [POWERPC] PS3: Sy...
763
764
765
  MODULE_AUTHOR("Sony Corporation");
  MODULE_LICENSE("GPL v2");
  MODULE_DESCRIPTION("PS3 System Manager");
66c63b84b   Geoff Levand   [POWERPC] PS3: Sy...
766
  MODULE_ALIAS(PS3_MODULE_ALIAS_SYSTEM_MANAGER);