xref: /linux/drivers/ps3/ps3-sys-manager.c (revision f7511d5f66f01fc451747b24e79f3ada7a3af9af)
1 /*
2  *  PS3 System Manager.
3  *
4  *  Copyright (C) 2007 Sony Computer Entertainment Inc.
5  *  Copyright 2007 Sony Corp.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; version 2 of the License.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/workqueue.h>
24 #include <linux/reboot.h>
25 
26 #include <asm/firmware.h>
27 #include <asm/lv1call.h>
28 #include <asm/ps3.h>
29 
30 #include "vuart.h"
31 
32 /**
33  * ps3_sys_manager - PS3 system manager driver.
34  *
35  * The system manager provides an asynchronous system event notification
36  * mechanism for reporting events like thermal alert and button presses to
37  * guests.  It also provides support to control system shutdown and startup.
38  *
39  * The actual system manager is implemented as an application running in the
40  * system policy module in lpar_1.  Guests communicate with the system manager
41  * through port 2 of the vuart using a simple packet message protocol.
42  * Messages are comprised of a fixed field header followed by a message
43  * specific payload.
44  */
45 
46 /**
47  * struct ps3_sys_manager_header - System manager message header.
48  * @version: Header version, currently 1.
49  * @size: Header size in bytes, curently 16.
50  * @payload_size: Message payload size in bytes.
51  * @service_id: Message type, one of enum ps3_sys_manager_service_id.
52  * @request_tag: Unique number to identify reply.
53  */
54 
55 struct ps3_sys_manager_header {
56 	/* version 1 */
57 	u8 version;
58 	u8 size;
59 	u16 reserved_1;
60 	u32 payload_size;
61 	u16 service_id;
62 	u16 reserved_2;
63 	u32 request_tag;
64 };
65 
66 #define dump_sm_header(_h) _dump_sm_header(_h, __func__, __LINE__)
67 static void __maybe_unused _dump_sm_header(
68 	const struct ps3_sys_manager_header *h, const char *func, int line)
69 {
70 	pr_debug("%s:%d: version:      %xh\n", func, line, h->version);
71 	pr_debug("%s:%d: size:         %xh\n", func, line, h->size);
72 	pr_debug("%s:%d: payload_size: %xh\n", func, line, h->payload_size);
73 	pr_debug("%s:%d: service_id:   %xh\n", func, line, h->service_id);
74 	pr_debug("%s:%d: request_tag:  %xh\n", func, line, h->request_tag);
75 }
76 
77 /**
78  * @PS3_SM_RX_MSG_LEN_MIN - Shortest received message length.
79  * @PS3_SM_RX_MSG_LEN_MAX - Longest received message length.
80  *
81  * Currently all messages received from the system manager are either
82  * (16 bytes header + 8 bytes payload = 24 bytes) or (16 bytes header
83  * + 16 bytes payload = 32 bytes).  This knowlege is used to simplify
84  * the logic.
85  */
86 
87 enum {
88 	PS3_SM_RX_MSG_LEN_MIN = 24,
89 	PS3_SM_RX_MSG_LEN_MAX = 32,
90 };
91 
92 /**
93  * enum ps3_sys_manager_service_id - Message header service_id.
94  * @PS3_SM_SERVICE_ID_REQUEST:       guest --> sys_manager.
95  * @PS3_SM_SERVICE_ID_REQUEST_ERROR: guest <-- sys_manager.
96  * @PS3_SM_SERVICE_ID_COMMAND:       guest <-- sys_manager.
97  * @PS3_SM_SERVICE_ID_RESPONSE:      guest --> sys_manager.
98  * @PS3_SM_SERVICE_ID_SET_ATTR:      guest --> sys_manager.
99  * @PS3_SM_SERVICE_ID_EXTERN_EVENT:  guest <-- sys_manager.
100  * @PS3_SM_SERVICE_ID_SET_NEXT_OP:   guest --> sys_manager.
101  *
102  * PS3_SM_SERVICE_ID_REQUEST_ERROR is returned for invalid data values in a
103  * a PS3_SM_SERVICE_ID_REQUEST message.  It also seems to be returned when
104  * a REQUEST message is sent at the wrong time.
105  */
106 
107 enum ps3_sys_manager_service_id {
108 	/* version 1 */
109 	PS3_SM_SERVICE_ID_REQUEST = 1,
110 	PS3_SM_SERVICE_ID_RESPONSE = 2,
111 	PS3_SM_SERVICE_ID_COMMAND = 3,
112 	PS3_SM_SERVICE_ID_EXTERN_EVENT = 4,
113 	PS3_SM_SERVICE_ID_SET_NEXT_OP = 5,
114 	PS3_SM_SERVICE_ID_REQUEST_ERROR = 6,
115 	PS3_SM_SERVICE_ID_SET_ATTR = 8,
116 };
117 
118 /**
119  * enum ps3_sys_manager_attr - Notification attribute (bit position mask).
120  * @PS3_SM_ATTR_POWER: Power button.
121  * @PS3_SM_ATTR_RESET: Reset button, not available on retail console.
122  * @PS3_SM_ATTR_THERMAL: Sytem thermal alert.
123  * @PS3_SM_ATTR_CONTROLLER: Remote controller event.
124  * @PS3_SM_ATTR_ALL: Logical OR of all.
125  *
126  * The guest tells the system manager which events it is interested in receiving
127  * notice of by sending the system manager a logical OR of notification
128  * attributes via the ps3_sys_manager_send_attr() routine.
129  */
130 
131 enum ps3_sys_manager_attr {
132 	/* version 1 */
133 	PS3_SM_ATTR_POWER = 1,
134 	PS3_SM_ATTR_RESET = 2,
135 	PS3_SM_ATTR_THERMAL = 4,
136 	PS3_SM_ATTR_CONTROLLER = 8, /* bogus? */
137 	PS3_SM_ATTR_ALL = 0x0f,
138 };
139 
140 /**
141  * enum ps3_sys_manager_event - External event type, reported by system manager.
142  * @PS3_SM_EVENT_POWER_PRESSED: payload.value =
143  *  enum ps3_sys_manager_button_event.
144  * @PS3_SM_EVENT_POWER_RELEASED: payload.value = time pressed in millisec.
145  * @PS3_SM_EVENT_RESET_PRESSED: payload.value =
146  *  enum ps3_sys_manager_button_event.
147  * @PS3_SM_EVENT_RESET_RELEASED: payload.value = time pressed in millisec.
148  * @PS3_SM_EVENT_THERMAL_ALERT: payload.value = thermal zone id.
149  * @PS3_SM_EVENT_THERMAL_CLEARED: payload.value = thermal zone id.
150  */
151 
152 enum ps3_sys_manager_event {
153 	/* version 1 */
154 	PS3_SM_EVENT_POWER_PRESSED = 3,
155 	PS3_SM_EVENT_POWER_RELEASED = 4,
156 	PS3_SM_EVENT_RESET_PRESSED = 5,
157 	PS3_SM_EVENT_RESET_RELEASED = 6,
158 	PS3_SM_EVENT_THERMAL_ALERT = 7,
159 	PS3_SM_EVENT_THERMAL_CLEARED = 8,
160 	/* no info on controller events */
161 };
162 
163 /**
164  * enum ps3_sys_manager_button_event - Button event payload values.
165  * @PS3_SM_BUTTON_EVENT_HARD: Hardware generated event.
166  * @PS3_SM_BUTTON_EVENT_SOFT: Software generated event.
167  */
168 
169 enum ps3_sys_manager_button_event {
170 	PS3_SM_BUTTON_EVENT_HARD = 0,
171 	PS3_SM_BUTTON_EVENT_SOFT = 1,
172 };
173 
174 /**
175  * enum ps3_sys_manager_next_op - Operation to perform after lpar is destroyed.
176  */
177 
178 enum ps3_sys_manager_next_op {
179 	/* version 3 */
180 	PS3_SM_NEXT_OP_SYS_SHUTDOWN = 1,
181 	PS3_SM_NEXT_OP_SYS_REBOOT = 2,
182 	PS3_SM_NEXT_OP_LPAR_REBOOT = 0x82,
183 };
184 
185 /**
186  * enum ps3_sys_manager_wake_source - Next-op wakeup source (bit position mask).
187  * @PS3_SM_WAKE_DEFAULT: Disk insert, power button, eject button, IR
188  * controller, and bluetooth controller.
189  * @PS3_SM_WAKE_RTC:
190  * @PS3_SM_WAKE_RTC_ERROR:
191  * @PS3_SM_WAKE_W_O_L: Ether or wireless LAN.
192  * @PS3_SM_WAKE_P_O_R: Power on reset.
193  *
194  * Additional wakeup sources when specifying PS3_SM_NEXT_OP_SYS_SHUTDOWN.
195  * The system will always wake from the PS3_SM_WAKE_DEFAULT sources.
196  * Sources listed here are the only ones available to guests in the
197  * other-os lpar.
198  */
199 
200 enum ps3_sys_manager_wake_source {
201 	/* version 3 */
202 	PS3_SM_WAKE_DEFAULT   = 0,
203 	PS3_SM_WAKE_RTC       = 0x00000040,
204 	PS3_SM_WAKE_RTC_ERROR = 0x00000080,
205 	PS3_SM_WAKE_W_O_L     = 0x00000400,
206 	PS3_SM_WAKE_P_O_R     = 0x80000000,
207 };
208 
209 /**
210  * user_wake_sources - User specified wakeup sources.
211  *
212  * Logical OR of enum ps3_sys_manager_wake_source types.
213  */
214 
215 static u32 user_wake_sources = PS3_SM_WAKE_DEFAULT;
216 
217 /**
218  * enum ps3_sys_manager_cmd - Command from system manager to guest.
219  *
220  * The guest completes the actions needed, then acks or naks the command via
221  * ps3_sys_manager_send_response().  In the case of @PS3_SM_CMD_SHUTDOWN,
222  * the guest must be fully prepared for a system poweroff prior to acking the
223  * command.
224  */
225 
226 enum ps3_sys_manager_cmd {
227 	/* version 1 */
228 	PS3_SM_CMD_SHUTDOWN = 1, /* shutdown guest OS */
229 };
230 
231 /**
232  * ps3_sm_force_power_off - Poweroff helper.
233  *
234  * A global variable used to force a poweroff when the power button has
235  * been pressed irrespective of how init handles the ctrl_alt_del signal.
236  *
237  */
238 
239 static unsigned int ps3_sm_force_power_off;
240 
241 /**
242  * ps3_sys_manager_write - Helper to write a two part message to the vuart.
243  *
244  */
245 
246 static int ps3_sys_manager_write(struct ps3_system_bus_device *dev,
247 	const struct ps3_sys_manager_header *header, const void *payload)
248 {
249 	int result;
250 
251 	BUG_ON(header->version != 1);
252 	BUG_ON(header->size != 16);
253 	BUG_ON(header->payload_size != 8 && header->payload_size != 16);
254 	BUG_ON(header->service_id > 8);
255 
256 	result = ps3_vuart_write(dev, header,
257 		sizeof(struct ps3_sys_manager_header));
258 
259 	if (!result)
260 		result = ps3_vuart_write(dev, payload, header->payload_size);
261 
262 	return result;
263 }
264 
265 /**
266  * ps3_sys_manager_send_attr - Send a 'set attribute' to the system manager.
267  *
268  */
269 
270 static int ps3_sys_manager_send_attr(struct ps3_system_bus_device *dev,
271 	enum ps3_sys_manager_attr attr)
272 {
273 	struct ps3_sys_manager_header header;
274 	struct {
275 		u8 version;
276 		u8 reserved_1[3];
277 		u32 attribute;
278 	} payload;
279 
280 	BUILD_BUG_ON(sizeof(payload) != 8);
281 
282 	dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, attr);
283 
284 	memset(&header, 0, sizeof(header));
285 	header.version = 1;
286 	header.size = 16;
287 	header.payload_size = 16;
288 	header.service_id = PS3_SM_SERVICE_ID_SET_ATTR;
289 
290 	memset(&payload, 0, sizeof(payload));
291 	payload.version = 1;
292 	payload.attribute = attr;
293 
294 	return ps3_sys_manager_write(dev, &header, &payload);
295 }
296 
297 /**
298  * ps3_sys_manager_send_next_op - Send a 'set next op' to the system manager.
299  *
300  * Tell the system manager what to do after this lpar is destroyed.
301  */
302 
303 static int ps3_sys_manager_send_next_op(struct ps3_system_bus_device *dev,
304 	enum ps3_sys_manager_next_op op,
305 	enum ps3_sys_manager_wake_source wake_source)
306 {
307 	struct ps3_sys_manager_header header;
308 	struct {
309 		u8 version;
310 		u8 type;
311 		u8 gos_id;
312 		u8 reserved_1;
313 		u32 wake_source;
314 		u8 reserved_2[8];
315 	} payload;
316 
317 	BUILD_BUG_ON(sizeof(payload) != 16);
318 
319 	dev_dbg(&dev->core, "%s:%d: (%xh)\n", __func__, __LINE__, op);
320 
321 	memset(&header, 0, sizeof(header));
322 	header.version = 1;
323 	header.size = 16;
324 	header.payload_size = 16;
325 	header.service_id = PS3_SM_SERVICE_ID_SET_NEXT_OP;
326 
327 	memset(&payload, 0, sizeof(payload));
328 	payload.version = 3;
329 	payload.type = op;
330 	payload.gos_id = 3; /* other os */
331 	payload.wake_source = wake_source;
332 
333 	return ps3_sys_manager_write(dev, &header, &payload);
334 }
335 
336 /**
337  * ps3_sys_manager_send_request_shutdown - Send 'request' to the system manager.
338  *
339  * The guest sends this message to request an operation or action of the system
340  * manager.  The reply is a command message from the system manager.  In the
341  * command handler the guest performs the requested operation.  The result of
342  * the command is then communicated back to the system manager with a response
343  * message.
344  *
345  * Currently, the only supported request is the 'shutdown self' request.
346  */
347 
348 static int ps3_sys_manager_send_request_shutdown(
349 	struct ps3_system_bus_device *dev)
350 {
351 	struct ps3_sys_manager_header header;
352 	struct {
353 		u8 version;
354 		u8 type;
355 		u8 gos_id;
356 		u8 reserved_1[13];
357 	} payload;
358 
359 	BUILD_BUG_ON(sizeof(payload) != 16);
360 
361 	dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
362 
363 	memset(&header, 0, sizeof(header));
364 	header.version = 1;
365 	header.size = 16;
366 	header.payload_size = 16;
367 	header.service_id = PS3_SM_SERVICE_ID_REQUEST;
368 
369 	memset(&payload, 0, sizeof(payload));
370 	payload.version = 1;
371 	payload.type = 1; /* shutdown */
372 	payload.gos_id = 0; /* self */
373 
374 	return ps3_sys_manager_write(dev, &header, &payload);
375 }
376 
377 /**
378  * ps3_sys_manager_send_response - Send a 'response' to the system manager.
379  * @status: zero = success, others fail.
380  *
381  * The guest sends this message to the system manager to acnowledge success or
382  * failure of a command sent by the system manager.
383  */
384 
385 static int ps3_sys_manager_send_response(struct ps3_system_bus_device *dev,
386 	u64 status)
387 {
388 	struct ps3_sys_manager_header header;
389 	struct {
390 		u8 version;
391 		u8 reserved_1[3];
392 		u8 status;
393 		u8 reserved_2[11];
394 	} payload;
395 
396 	BUILD_BUG_ON(sizeof(payload) != 16);
397 
398 	dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
399 		(status ? "nak" : "ack"));
400 
401 	memset(&header, 0, sizeof(header));
402 	header.version = 1;
403 	header.size = 16;
404 	header.payload_size = 16;
405 	header.service_id = PS3_SM_SERVICE_ID_RESPONSE;
406 
407 	memset(&payload, 0, sizeof(payload));
408 	payload.version = 1;
409 	payload.status = status;
410 
411 	return ps3_sys_manager_write(dev, &header, &payload);
412 }
413 
414 /**
415  * ps3_sys_manager_handle_event - Second stage event msg handler.
416  *
417  */
418 
419 static int ps3_sys_manager_handle_event(struct ps3_system_bus_device *dev)
420 {
421 	int result;
422 	struct {
423 		u8 version;
424 		u8 type;
425 		u8 reserved_1[2];
426 		u32 value;
427 		u8 reserved_2[8];
428 	} event;
429 
430 	BUILD_BUG_ON(sizeof(event) != 16);
431 
432 	result = ps3_vuart_read(dev, &event, sizeof(event));
433 	BUG_ON(result && "need to retry here");
434 
435 	if (event.version != 1) {
436 		dev_dbg(&dev->core, "%s:%d: unsupported event version (%u)\n",
437 			__func__, __LINE__, event.version);
438 		return -EIO;
439 	}
440 
441 	switch (event.type) {
442 	case PS3_SM_EVENT_POWER_PRESSED:
443 		dev_dbg(&dev->core, "%s:%d: POWER_PRESSED (%s)\n",
444 			__func__, __LINE__,
445 			(event.value == PS3_SM_BUTTON_EVENT_SOFT ? "soft"
446 			: "hard"));
447 		ps3_sm_force_power_off = 1;
448 		/*
449 		 * A memory barrier is use here to sync memory since
450 		 * ps3_sys_manager_final_restart() could be called on
451 		 * another cpu.
452 		 */
453 		wmb();
454 		kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */
455 		break;
456 	case PS3_SM_EVENT_POWER_RELEASED:
457 		dev_dbg(&dev->core, "%s:%d: POWER_RELEASED (%u ms)\n",
458 			__func__, __LINE__, event.value);
459 		break;
460 	case PS3_SM_EVENT_RESET_PRESSED:
461 		dev_dbg(&dev->core, "%s:%d: RESET_PRESSED (%s)\n",
462 			__func__, __LINE__,
463 			(event.value == PS3_SM_BUTTON_EVENT_SOFT ? "soft"
464 			: "hard"));
465 		ps3_sm_force_power_off = 0;
466 		/*
467 		 * A memory barrier is use here to sync memory since
468 		 * ps3_sys_manager_final_restart() could be called on
469 		 * another cpu.
470 		 */
471 		wmb();
472 		kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */
473 		break;
474 	case PS3_SM_EVENT_RESET_RELEASED:
475 		dev_dbg(&dev->core, "%s:%d: RESET_RELEASED (%u ms)\n",
476 			__func__, __LINE__, event.value);
477 		break;
478 	case PS3_SM_EVENT_THERMAL_ALERT:
479 		dev_dbg(&dev->core, "%s:%d: THERMAL_ALERT (zone %u)\n",
480 			__func__, __LINE__, event.value);
481 		pr_info("PS3 Thermal Alert Zone %u\n", event.value);
482 		break;
483 	case PS3_SM_EVENT_THERMAL_CLEARED:
484 		dev_dbg(&dev->core, "%s:%d: THERMAL_CLEARED (zone %u)\n",
485 			__func__, __LINE__, event.value);
486 		break;
487 	default:
488 		dev_dbg(&dev->core, "%s:%d: unknown event (%u)\n",
489 			__func__, __LINE__, event.type);
490 		return -EIO;
491 	}
492 
493 	return 0;
494 }
495 /**
496  * ps3_sys_manager_handle_cmd - Second stage command msg handler.
497  *
498  * The system manager sends this in reply to a 'request' message from the guest.
499  */
500 
501 static int ps3_sys_manager_handle_cmd(struct ps3_system_bus_device *dev)
502 {
503 	int result;
504 	struct {
505 		u8 version;
506 		u8 type;
507 		u8 reserved_1[14];
508 	} cmd;
509 
510 	BUILD_BUG_ON(sizeof(cmd) != 16);
511 
512 	dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
513 
514 	result = ps3_vuart_read(dev, &cmd, sizeof(cmd));
515 	BUG_ON(result && "need to retry here");
516 
517 	if (result)
518 		return result;
519 
520 	if (cmd.version != 1) {
521 		dev_dbg(&dev->core, "%s:%d: unsupported cmd version (%u)\n",
522 			__func__, __LINE__, cmd.version);
523 		return -EIO;
524 	}
525 
526 	if (cmd.type != PS3_SM_CMD_SHUTDOWN) {
527 		dev_dbg(&dev->core, "%s:%d: unknown cmd (%u)\n",
528 			__func__, __LINE__, cmd.type);
529 		return -EIO;
530 	}
531 
532 	ps3_sys_manager_send_response(dev, 0);
533 	return 0;
534 }
535 
536 /**
537  * ps3_sys_manager_handle_msg - First stage msg handler.
538  *
539  * Can be called directly to manually poll vuart and pump message handler.
540  */
541 
542 static int ps3_sys_manager_handle_msg(struct ps3_system_bus_device *dev)
543 {
544 	int result;
545 	struct ps3_sys_manager_header header;
546 
547 	result = ps3_vuart_read(dev, &header,
548 		sizeof(struct ps3_sys_manager_header));
549 
550 	if (result)
551 		return result;
552 
553 	if (header.version != 1) {
554 		dev_dbg(&dev->core, "%s:%d: unsupported header version (%u)\n",
555 			__func__, __LINE__, header.version);
556 		dump_sm_header(&header);
557 		goto fail_header;
558 	}
559 
560 	BUILD_BUG_ON(sizeof(header) != 16);
561 
562 	if (header.size != 16 || (header.payload_size != 8
563 		&& header.payload_size != 16)) {
564 		dump_sm_header(&header);
565 		BUG();
566 	}
567 
568 	switch (header.service_id) {
569 	case PS3_SM_SERVICE_ID_EXTERN_EVENT:
570 		dev_dbg(&dev->core, "%s:%d: EVENT\n", __func__, __LINE__);
571 		return ps3_sys_manager_handle_event(dev);
572 	case PS3_SM_SERVICE_ID_COMMAND:
573 		dev_dbg(&dev->core, "%s:%d: COMMAND\n", __func__, __LINE__);
574 		return ps3_sys_manager_handle_cmd(dev);
575 	case PS3_SM_SERVICE_ID_REQUEST_ERROR:
576 		dev_dbg(&dev->core, "%s:%d: REQUEST_ERROR\n", __func__,
577 			__LINE__);
578 		dump_sm_header(&header);
579 		break;
580 	default:
581 		dev_dbg(&dev->core, "%s:%d: unknown service_id (%u)\n",
582 			__func__, __LINE__, header.service_id);
583 		break;
584 	}
585 	goto fail_id;
586 
587 fail_header:
588 	ps3_vuart_clear_rx_bytes(dev, 0);
589 	return -EIO;
590 fail_id:
591 	ps3_vuart_clear_rx_bytes(dev, header.payload_size);
592 	return -EIO;
593 }
594 
595 static void ps3_sys_manager_fin(struct ps3_system_bus_device *dev)
596 {
597 	ps3_sys_manager_send_request_shutdown(dev);
598 
599 	pr_emerg("System Halted, OK to turn off power\n");
600 
601 	while (ps3_sys_manager_handle_msg(dev)) {
602 		/* pause until next DEC interrupt */
603 		lv1_pause(0);
604 	}
605 
606 	while (1) {
607 		/* pause, ignoring DEC interrupt */
608 		lv1_pause(1);
609 	}
610 }
611 
612 /**
613  * ps3_sys_manager_final_power_off - The final platform machine_power_off routine.
614  *
615  * This routine never returns.  The routine disables asynchronous vuart reads
616  * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
617  * the shutdown command sent from the system manager.  Soon after the
618  * acknowledgement is sent the lpar is destroyed by the HV.  This routine
619  * should only be called from ps3_power_off() through
620  * ps3_sys_manager_ops.power_off.
621  */
622 
623 static void ps3_sys_manager_final_power_off(struct ps3_system_bus_device *dev)
624 {
625 	BUG_ON(!dev);
626 
627 	dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
628 
629 	ps3_vuart_cancel_async(dev);
630 
631 	ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_SHUTDOWN,
632 		user_wake_sources);
633 
634 	ps3_sys_manager_fin(dev);
635 }
636 
637 /**
638  * ps3_sys_manager_final_restart - The final platform machine_restart routine.
639  *
640  * This routine never returns.  The routine disables asynchronous vuart reads
641  * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
642  * the shutdown command sent from the system manager.  Soon after the
643  * acknowledgement is sent the lpar is destroyed by the HV.  This routine
644  * should only be called from ps3_restart() through ps3_sys_manager_ops.restart.
645  */
646 
647 static void ps3_sys_manager_final_restart(struct ps3_system_bus_device *dev)
648 {
649 	BUG_ON(!dev);
650 
651 	dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
652 
653 	/* Check if we got here via a power button event. */
654 
655 	if (ps3_sm_force_power_off) {
656 		dev_dbg(&dev->core, "%s:%d: forcing poweroff\n",
657 			__func__, __LINE__);
658 		ps3_sys_manager_final_power_off(dev);
659 	}
660 
661 	ps3_vuart_cancel_async(dev);
662 
663 	ps3_sys_manager_send_attr(dev, 0);
664 	ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_REBOOT,
665 		user_wake_sources);
666 
667 	ps3_sys_manager_fin(dev);
668 }
669 
670 /**
671  * ps3_sys_manager_get_wol - Get wake-on-lan setting.
672  */
673 
674 int ps3_sys_manager_get_wol(void)
675 {
676 	pr_debug("%s:%d\n", __func__, __LINE__);
677 
678 	return (user_wake_sources & PS3_SM_WAKE_W_O_L) != 0;
679 }
680 EXPORT_SYMBOL_GPL(ps3_sys_manager_get_wol);
681 
682 /**
683  * ps3_sys_manager_set_wol - Set wake-on-lan setting.
684  */
685 
686 void ps3_sys_manager_set_wol(int state)
687 {
688 	static DEFINE_MUTEX(mutex);
689 
690 	mutex_lock(&mutex);
691 
692 	pr_debug("%s:%d: %d\n", __func__, __LINE__, state);
693 
694 	if (state)
695 		user_wake_sources |= PS3_SM_WAKE_W_O_L;
696 	else
697 		user_wake_sources &= ~PS3_SM_WAKE_W_O_L;
698 	mutex_unlock(&mutex);
699 }
700 EXPORT_SYMBOL_GPL(ps3_sys_manager_set_wol);
701 
702 /**
703  * ps3_sys_manager_work - Asynchronous read handler.
704  *
705  * Signaled when PS3_SM_RX_MSG_LEN_MIN bytes arrive at the vuart port.
706  */
707 
708 static void ps3_sys_manager_work(struct ps3_system_bus_device *dev)
709 {
710 	ps3_sys_manager_handle_msg(dev);
711 	ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN);
712 }
713 
714 static int ps3_sys_manager_probe(struct ps3_system_bus_device *dev)
715 {
716 	int result;
717 	struct ps3_sys_manager_ops ops;
718 
719 	dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
720 
721 	ops.power_off = ps3_sys_manager_final_power_off;
722 	ops.restart = ps3_sys_manager_final_restart;
723 	ops.dev = dev;
724 
725 	/* ps3_sys_manager_register_ops copies ops. */
726 
727 	ps3_sys_manager_register_ops(&ops);
728 
729 	result = ps3_sys_manager_send_attr(dev, PS3_SM_ATTR_ALL);
730 	BUG_ON(result);
731 
732 	result = ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN);
733 	BUG_ON(result);
734 
735 	return result;
736 }
737 
738 static int ps3_sys_manager_remove(struct ps3_system_bus_device *dev)
739 {
740 	dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
741 	return 0;
742 }
743 
744 static void ps3_sys_manager_shutdown(struct ps3_system_bus_device *dev)
745 {
746 	dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
747 }
748 
749 static struct ps3_vuart_port_driver ps3_sys_manager = {
750 	.core.match_id = PS3_MATCH_ID_SYSTEM_MANAGER,
751 	.core.core.name = "ps3_sys_manager",
752 	.probe = ps3_sys_manager_probe,
753 	.remove = ps3_sys_manager_remove,
754 	.shutdown = ps3_sys_manager_shutdown,
755 	.work = ps3_sys_manager_work,
756 };
757 
758 static int __init ps3_sys_manager_init(void)
759 {
760 	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
761 		return -ENODEV;
762 
763 	return ps3_vuart_port_driver_register(&ps3_sys_manager);
764 }
765 
766 module_init(ps3_sys_manager_init);
767 /* Module remove not supported. */
768 
769 MODULE_AUTHOR("Sony Corporation");
770 MODULE_LICENSE("GPL v2");
771 MODULE_DESCRIPTION("PS3 System Manager");
772 MODULE_ALIAS(PS3_MODULE_ALIAS_SYSTEM_MANAGER);
773