4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2007 Sony Corp.
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.
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.
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
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/workqueue.h>
24 #include <linux/reboot.h>
26 #include <asm/firmware.h>
31 MODULE_AUTHOR("Sony Corporation");
32 MODULE_LICENSE("GPL v2");
33 MODULE_DESCRIPTION("PS3 System Manager");
36 * ps3_sys_manager - PS3 system manager driver.
38 * The system manager provides an asynchronous system event notification
39 * mechanism for reporting events like thermal alert and button presses to
40 * guests. It also provides support to control system shutdown and startup.
42 * The actual system manager is implemented as an application running in the
43 * system policy module in lpar_1. Guests communicate with the system manager
44 * through port 2 of the vuart using a simple packet message protocol.
45 * Messages are comprised of a fixed field header followed by a message
50 * struct ps3_sys_manager_header - System manager message header.
51 * @version: Header version, currently 1.
52 * @size: Header size in bytes, curently 16.
53 * @payload_size: Message payload size in bytes.
54 * @service_id: Message type, one of enum ps3_sys_manager_service_id.
55 * @request_tag: Unique number to identify reply.
58 struct ps3_sys_manager_header
{
69 #define dump_sm_header(_h) _dump_sm_header(_h, __func__, __LINE__)
70 static void __maybe_unused
_dump_sm_header(
71 const struct ps3_sys_manager_header
*h
, const char *func
, int line
)
73 pr_debug("%s:%d: version: %xh\n", func
, line
, h
->version
);
74 pr_debug("%s:%d: size: %xh\n", func
, line
, h
->size
);
75 pr_debug("%s:%d: payload_size: %xh\n", func
, line
, h
->payload_size
);
76 pr_debug("%s:%d: service_id: %xh\n", func
, line
, h
->service_id
);
77 pr_debug("%s:%d: request_tag: %xh\n", func
, line
, h
->request_tag
);
81 * @PS3_SM_RX_MSG_LEN_MIN - Shortest received message length.
82 * @PS3_SM_RX_MSG_LEN_MAX - Longest received message length.
84 * Currently all messages received from the system manager are either
85 * (16 bytes header + 8 bytes payload = 24 bytes) or (16 bytes header
86 * + 16 bytes payload = 32 bytes). This knowlege is used to simplify
91 PS3_SM_RX_MSG_LEN_MIN
= 24,
92 PS3_SM_RX_MSG_LEN_MAX
= 32,
96 * enum ps3_sys_manager_service_id - Message header service_id.
97 * @PS3_SM_SERVICE_ID_REQUEST: guest --> sys_manager.
98 * @PS3_SM_SERVICE_ID_REQUEST_ERROR: guest <-- sys_manager.
99 * @PS3_SM_SERVICE_ID_COMMAND: guest <-- sys_manager.
100 * @PS3_SM_SERVICE_ID_RESPONSE: guest --> sys_manager.
101 * @PS3_SM_SERVICE_ID_SET_ATTR: guest --> sys_manager.
102 * @PS3_SM_SERVICE_ID_EXTERN_EVENT: guest <-- sys_manager.
103 * @PS3_SM_SERVICE_ID_SET_NEXT_OP: guest --> sys_manager.
105 * PS3_SM_SERVICE_ID_REQUEST_ERROR is returned for invalid data values in a
106 * a PS3_SM_SERVICE_ID_REQUEST message. It also seems to be returned when
107 * a REQUEST message is sent at the wrong time.
110 enum ps3_sys_manager_service_id
{
112 PS3_SM_SERVICE_ID_REQUEST
= 1,
113 PS3_SM_SERVICE_ID_RESPONSE
= 2,
114 PS3_SM_SERVICE_ID_COMMAND
= 3,
115 PS3_SM_SERVICE_ID_EXTERN_EVENT
= 4,
116 PS3_SM_SERVICE_ID_SET_NEXT_OP
= 5,
117 PS3_SM_SERVICE_ID_REQUEST_ERROR
= 6,
118 PS3_SM_SERVICE_ID_SET_ATTR
= 8,
122 * enum ps3_sys_manager_attr - Notification attribute (bit position mask).
123 * @PS3_SM_ATTR_POWER: Power button.
124 * @PS3_SM_ATTR_RESET: Reset button, not available on retail console.
125 * @PS3_SM_ATTR_THERMAL: Sytem thermal alert.
126 * @PS3_SM_ATTR_CONTROLLER: Remote controller event.
127 * @PS3_SM_ATTR_ALL: Logical OR of all.
129 * The guest tells the system manager which events it is interested in receiving
130 * notice of by sending the system manager a logical OR of notification
131 * attributes via the ps3_sys_manager_send_attr() routine.
134 enum ps3_sys_manager_attr
{
136 PS3_SM_ATTR_POWER
= 1,
137 PS3_SM_ATTR_RESET
= 2,
138 PS3_SM_ATTR_THERMAL
= 4,
139 PS3_SM_ATTR_CONTROLLER
= 8, /* bogus? */
140 PS3_SM_ATTR_ALL
= 0x0f,
144 * enum ps3_sys_manager_event - External event type, reported by system manager.
145 * @PS3_SM_EVENT_POWER_PRESSED: payload.value not used.
146 * @PS3_SM_EVENT_POWER_RELEASED: payload.value = time pressed in millisec.
147 * @PS3_SM_EVENT_RESET_PRESSED: payload.value not used.
148 * @PS3_SM_EVENT_RESET_RELEASED: payload.value = time pressed in millisec.
149 * @PS3_SM_EVENT_THERMAL_ALERT: payload.value = thermal zone id.
150 * @PS3_SM_EVENT_THERMAL_CLEARED: payload.value = thermal zone id.
153 enum ps3_sys_manager_event
{
155 PS3_SM_EVENT_POWER_PRESSED
= 3,
156 PS3_SM_EVENT_POWER_RELEASED
= 4,
157 PS3_SM_EVENT_RESET_PRESSED
= 5,
158 PS3_SM_EVENT_RESET_RELEASED
= 6,
159 PS3_SM_EVENT_THERMAL_ALERT
= 7,
160 PS3_SM_EVENT_THERMAL_CLEARED
= 8,
161 /* no info on controller events */
165 * enum ps3_sys_manager_next_op - Operation to perform after lpar is destroyed.
168 enum ps3_sys_manager_next_op
{
170 PS3_SM_NEXT_OP_SYS_SHUTDOWN
= 1,
171 PS3_SM_NEXT_OP_SYS_REBOOT
= 2,
172 PS3_SM_NEXT_OP_LPAR_REBOOT
= 0x82,
176 * enum ps3_sys_manager_wake_source - Next-op wakeup source (bit position mask).
177 * @PS3_SM_WAKE_DEFAULT: Disk insert, power button, eject button, IR
178 * controller, and bluetooth controller.
180 * @PS3_SM_WAKE_RTC_ERROR:
181 * @PS3_SM_WAKE_P_O_R: Power on reset.
183 * Additional wakeup sources when specifying PS3_SM_NEXT_OP_SYS_SHUTDOWN.
184 * System will always wake from the PS3_SM_WAKE_DEFAULT sources.
187 enum ps3_sys_manager_wake_source
{
189 PS3_SM_WAKE_DEFAULT
= 0,
190 PS3_SM_WAKE_RTC
= 0x00000040,
191 PS3_SM_WAKE_RTC_ERROR
= 0x00000080,
192 PS3_SM_WAKE_P_O_R
= 0x10000000,
196 * enum ps3_sys_manager_cmd - Command from system manager to guest.
198 * The guest completes the actions needed, then acks or naks the command via
199 * ps3_sys_manager_send_response(). In the case of @PS3_SM_CMD_SHUTDOWN,
200 * the guest must be fully prepared for a system poweroff prior to acking the
204 enum ps3_sys_manager_cmd
{
206 PS3_SM_CMD_SHUTDOWN
= 1, /* shutdown guest OS */
210 * ps3_sm_force_power_off - Poweroff helper.
212 * A global variable used to force a poweroff when the power button has
213 * been pressed irrespective of how init handles the ctrl_alt_del signal.
217 static unsigned int ps3_sm_force_power_off
;
220 * ps3_sys_manager_write - Helper to write a two part message to the vuart.
224 static int ps3_sys_manager_write(struct ps3_system_bus_device
*dev
,
225 const struct ps3_sys_manager_header
*header
, const void *payload
)
229 BUG_ON(header
->version
!= 1);
230 BUG_ON(header
->size
!= 16);
231 BUG_ON(header
->payload_size
!= 8 && header
->payload_size
!= 16);
232 BUG_ON(header
->service_id
> 8);
234 result
= ps3_vuart_write(dev
, header
,
235 sizeof(struct ps3_sys_manager_header
));
238 result
= ps3_vuart_write(dev
, payload
, header
->payload_size
);
244 * ps3_sys_manager_send_attr - Send a 'set attribute' to the system manager.
248 static int ps3_sys_manager_send_attr(struct ps3_system_bus_device
*dev
,
249 enum ps3_sys_manager_attr attr
)
251 struct ps3_sys_manager_header header
;
258 BUILD_BUG_ON(sizeof(payload
) != 8);
260 dev_dbg(&dev
->core
, "%s:%d: %xh\n", __func__
, __LINE__
, attr
);
262 memset(&header
, 0, sizeof(header
));
265 header
.payload_size
= 16;
266 header
.service_id
= PS3_SM_SERVICE_ID_SET_ATTR
;
268 memset(&payload
, 0, sizeof(payload
));
270 payload
.attribute
= attr
;
272 return ps3_sys_manager_write(dev
, &header
, &payload
);
276 * ps3_sys_manager_send_next_op - Send a 'set next op' to the system manager.
278 * Tell the system manager what to do after this lpar is destroyed.
281 static int ps3_sys_manager_send_next_op(struct ps3_system_bus_device
*dev
,
282 enum ps3_sys_manager_next_op op
,
283 enum ps3_sys_manager_wake_source wake_source
)
285 struct ps3_sys_manager_header header
;
295 BUILD_BUG_ON(sizeof(payload
) != 16);
297 dev_dbg(&dev
->core
, "%s:%d: (%xh)\n", __func__
, __LINE__
, op
);
299 memset(&header
, 0, sizeof(header
));
302 header
.payload_size
= 16;
303 header
.service_id
= PS3_SM_SERVICE_ID_SET_NEXT_OP
;
305 memset(&payload
, 0, sizeof(payload
));
308 payload
.gos_id
= 3; /* other os */
309 payload
.wake_source
= wake_source
;
311 return ps3_sys_manager_write(dev
, &header
, &payload
);
315 * ps3_sys_manager_send_request_shutdown - Send 'request' to the system manager.
317 * The guest sends this message to request an operation or action of the system
318 * manager. The reply is a command message from the system manager. In the
319 * command handler the guest performs the requested operation. The result of
320 * the command is then communicated back to the system manager with a response
323 * Currently, the only supported request is the 'shutdown self' request.
326 static int ps3_sys_manager_send_request_shutdown(
327 struct ps3_system_bus_device
*dev
)
329 struct ps3_sys_manager_header header
;
337 BUILD_BUG_ON(sizeof(payload
) != 16);
339 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
341 memset(&header
, 0, sizeof(header
));
344 header
.payload_size
= 16;
345 header
.service_id
= PS3_SM_SERVICE_ID_REQUEST
;
347 memset(&payload
, 0, sizeof(payload
));
349 payload
.type
= 1; /* shutdown */
350 payload
.gos_id
= 0; /* self */
352 return ps3_sys_manager_write(dev
, &header
, &payload
);
356 * ps3_sys_manager_send_response - Send a 'response' to the system manager.
357 * @status: zero = success, others fail.
359 * The guest sends this message to the system manager to acnowledge success or
360 * failure of a command sent by the system manager.
363 static int ps3_sys_manager_send_response(struct ps3_system_bus_device
*dev
,
366 struct ps3_sys_manager_header header
;
374 BUILD_BUG_ON(sizeof(payload
) != 16);
376 dev_dbg(&dev
->core
, "%s:%d: (%s)\n", __func__
, __LINE__
,
377 (status
? "nak" : "ack"));
379 memset(&header
, 0, sizeof(header
));
382 header
.payload_size
= 16;
383 header
.service_id
= PS3_SM_SERVICE_ID_RESPONSE
;
385 memset(&payload
, 0, sizeof(payload
));
387 payload
.status
= status
;
389 return ps3_sys_manager_write(dev
, &header
, &payload
);
393 * ps3_sys_manager_handle_event - Second stage event msg handler.
397 static int ps3_sys_manager_handle_event(struct ps3_system_bus_device
*dev
)
408 BUILD_BUG_ON(sizeof(event
) != 16);
410 result
= ps3_vuart_read(dev
, &event
, sizeof(event
));
411 BUG_ON(result
&& "need to retry here");
413 if (event
.version
!= 1) {
414 dev_dbg(&dev
->core
, "%s:%d: unsupported event version (%u)\n",
415 __func__
, __LINE__
, event
.version
);
419 switch (event
.type
) {
420 case PS3_SM_EVENT_POWER_PRESSED
:
421 dev_dbg(&dev
->core
, "%s:%d: POWER_PRESSED\n",
423 ps3_sm_force_power_off
= 1;
425 * A memory barrier is use here to sync memory since
426 * ps3_sys_manager_final_restart() could be called on
430 kill_cad_pid(SIGINT
, 1); /* ctrl_alt_del */
432 case PS3_SM_EVENT_POWER_RELEASED
:
433 dev_dbg(&dev
->core
, "%s:%d: POWER_RELEASED (%u ms)\n",
434 __func__
, __LINE__
, event
.value
);
436 case PS3_SM_EVENT_RESET_PRESSED
:
437 dev_dbg(&dev
->core
, "%s:%d: RESET_PRESSED\n",
439 ps3_sm_force_power_off
= 0;
441 * A memory barrier is use here to sync memory since
442 * ps3_sys_manager_final_restart() could be called on
446 kill_cad_pid(SIGINT
, 1); /* ctrl_alt_del */
448 case PS3_SM_EVENT_RESET_RELEASED
:
449 dev_dbg(&dev
->core
, "%s:%d: RESET_RELEASED (%u ms)\n",
450 __func__
, __LINE__
, event
.value
);
452 case PS3_SM_EVENT_THERMAL_ALERT
:
453 dev_dbg(&dev
->core
, "%s:%d: THERMAL_ALERT (zone %u)\n",
454 __func__
, __LINE__
, event
.value
);
455 printk(KERN_INFO
"PS3 Thermal Alert Zone %u\n", event
.value
);
457 case PS3_SM_EVENT_THERMAL_CLEARED
:
458 dev_dbg(&dev
->core
, "%s:%d: THERMAL_CLEARED (zone %u)\n",
459 __func__
, __LINE__
, event
.value
);
462 dev_dbg(&dev
->core
, "%s:%d: unknown event (%u)\n",
463 __func__
, __LINE__
, event
.type
);
470 * ps3_sys_manager_handle_cmd - Second stage command msg handler.
472 * The system manager sends this in reply to a 'request' message from the guest.
475 static int ps3_sys_manager_handle_cmd(struct ps3_system_bus_device
*dev
)
484 BUILD_BUG_ON(sizeof(cmd
) != 16);
486 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
488 result
= ps3_vuart_read(dev
, &cmd
, sizeof(cmd
));
489 BUG_ON(result
&& "need to retry here");
494 if (cmd
.version
!= 1) {
495 dev_dbg(&dev
->core
, "%s:%d: unsupported cmd version (%u)\n",
496 __func__
, __LINE__
, cmd
.version
);
500 if (cmd
.type
!= PS3_SM_CMD_SHUTDOWN
) {
501 dev_dbg(&dev
->core
, "%s:%d: unknown cmd (%u)\n",
502 __func__
, __LINE__
, cmd
.type
);
506 ps3_sys_manager_send_response(dev
, 0);
511 * ps3_sys_manager_handle_msg - First stage msg handler.
513 * Can be called directly to manually poll vuart and pump message handler.
516 static int ps3_sys_manager_handle_msg(struct ps3_system_bus_device
*dev
)
519 struct ps3_sys_manager_header header
;
521 result
= ps3_vuart_read(dev
, &header
,
522 sizeof(struct ps3_sys_manager_header
));
527 if (header
.version
!= 1) {
528 dev_dbg(&dev
->core
, "%s:%d: unsupported header version (%u)\n",
529 __func__
, __LINE__
, header
.version
);
530 dump_sm_header(&header
);
534 BUILD_BUG_ON(sizeof(header
) != 16);
536 if (header
.size
!= 16 || (header
.payload_size
!= 8
537 && header
.payload_size
!= 16)) {
538 dump_sm_header(&header
);
542 switch (header
.service_id
) {
543 case PS3_SM_SERVICE_ID_EXTERN_EVENT
:
544 dev_dbg(&dev
->core
, "%s:%d: EVENT\n", __func__
, __LINE__
);
545 return ps3_sys_manager_handle_event(dev
);
546 case PS3_SM_SERVICE_ID_COMMAND
:
547 dev_dbg(&dev
->core
, "%s:%d: COMMAND\n", __func__
, __LINE__
);
548 return ps3_sys_manager_handle_cmd(dev
);
549 case PS3_SM_SERVICE_ID_REQUEST_ERROR
:
550 dev_dbg(&dev
->core
, "%s:%d: REQUEST_ERROR\n", __func__
,
552 dump_sm_header(&header
);
555 dev_dbg(&dev
->core
, "%s:%d: unknown service_id (%u)\n",
556 __func__
, __LINE__
, header
.service_id
);
562 ps3_vuart_clear_rx_bytes(dev
, 0);
565 ps3_vuart_clear_rx_bytes(dev
, header
.payload_size
);
570 * ps3_sys_manager_final_power_off - The final platform machine_power_off routine.
572 * This routine never returns. The routine disables asynchronous vuart reads
573 * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
574 * the shutdown command sent from the system manager. Soon after the
575 * acknowledgement is sent the lpar is destroyed by the HV. This routine
576 * should only be called from ps3_power_off() through
577 * ps3_sys_manager_ops.power_off.
580 static void ps3_sys_manager_final_power_off(struct ps3_system_bus_device
*dev
)
584 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
586 ps3_vuart_cancel_async(dev
);
588 ps3_sys_manager_send_next_op(dev
, PS3_SM_NEXT_OP_SYS_SHUTDOWN
,
589 PS3_SM_WAKE_DEFAULT
);
590 ps3_sys_manager_send_request_shutdown(dev
);
592 printk(KERN_EMERG
"System Halted, OK to turn off power\n");
595 ps3_sys_manager_handle_msg(dev
);
599 * ps3_sys_manager_final_restart - The final platform machine_restart routine.
601 * This routine never returns. The routine disables asynchronous vuart reads
602 * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
603 * the shutdown command sent from the system manager. Soon after the
604 * acknowledgement is sent the lpar is destroyed by the HV. This routine
605 * should only be called from ps3_restart() through ps3_sys_manager_ops.restart.
608 static void ps3_sys_manager_final_restart(struct ps3_system_bus_device
*dev
)
612 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
614 /* Check if we got here via a power button event. */
616 if (ps3_sm_force_power_off
) {
617 dev_dbg(&dev
->core
, "%s:%d: forcing poweroff\n",
619 ps3_sys_manager_final_power_off(dev
);
622 ps3_vuart_cancel_async(dev
);
624 ps3_sys_manager_send_attr(dev
, 0);
625 ps3_sys_manager_send_next_op(dev
, PS3_SM_NEXT_OP_LPAR_REBOOT
,
626 PS3_SM_WAKE_DEFAULT
);
627 ps3_sys_manager_send_request_shutdown(dev
);
629 printk(KERN_EMERG
"System Halted, OK to turn off power\n");
632 ps3_sys_manager_handle_msg(dev
);
636 * ps3_sys_manager_work - Asynchronous read handler.
638 * Signaled when PS3_SM_RX_MSG_LEN_MIN bytes arrive at the vuart port.
641 static void ps3_sys_manager_work(struct ps3_system_bus_device
*dev
)
643 ps3_sys_manager_handle_msg(dev
);
644 ps3_vuart_read_async(dev
, PS3_SM_RX_MSG_LEN_MIN
);
647 static int ps3_sys_manager_probe(struct ps3_system_bus_device
*dev
)
650 struct ps3_sys_manager_ops ops
;
652 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
654 ops
.power_off
= ps3_sys_manager_final_power_off
;
655 ops
.restart
= ps3_sys_manager_final_restart
;
658 /* ps3_sys_manager_register_ops copies ops. */
660 ps3_sys_manager_register_ops(&ops
);
662 result
= ps3_sys_manager_send_attr(dev
, PS3_SM_ATTR_ALL
);
665 result
= ps3_vuart_read_async(dev
, PS3_SM_RX_MSG_LEN_MIN
);
671 static int ps3_sys_manager_remove(struct ps3_system_bus_device
*dev
)
673 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
677 static void ps3_sys_manager_shutdown(struct ps3_system_bus_device
*dev
)
679 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
682 static struct ps3_vuart_port_driver ps3_sys_manager
= {
683 .core
.match_id
= PS3_MATCH_ID_SYSTEM_MANAGER
,
684 .core
.core
.name
= "ps3_sys_manager",
685 .probe
= ps3_sys_manager_probe
,
686 .remove
= ps3_sys_manager_remove
,
687 .shutdown
= ps3_sys_manager_shutdown
,
688 .work
= ps3_sys_manager_work
,
691 static int __init
ps3_sys_manager_init(void)
693 if (!firmware_has_feature(FW_FEATURE_PS3_LV1
))
696 return ps3_vuart_port_driver_register(&ps3_sys_manager
);
699 module_init(ps3_sys_manager_init
);
700 /* Module remove not supported. */
702 MODULE_ALIAS(PS3_MODULE_ALIAS_SYSTEM_MANAGER
);