1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2007 Sony Computer Entertainment Inc.
6 * Copyright 2007 Sony Corp.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/workqueue.h>
12 #include <linux/reboot.h>
13 #include <linux/sched/signal.h>
15 #include <asm/firmware.h>
16 #include <asm/lv1call.h>
22 * ps3_sys_manager - PS3 system manager driver.
24 * The system manager provides an asynchronous system event notification
25 * mechanism for reporting events like thermal alert and button presses to
26 * guests. It also provides support to control system shutdown and startup.
28 * The actual system manager is implemented as an application running in the
29 * system policy module in lpar_1. Guests communicate with the system manager
30 * through port 2 of the vuart using a simple packet message protocol.
31 * Messages are comprised of a fixed field header followed by a message
36 * struct ps3_sys_manager_header - System manager message header.
37 * @version: Header version, currently 1.
38 * @size: Header size in bytes, currently 16.
39 * @payload_size: Message payload size in bytes.
40 * @service_id: Message type, one of enum ps3_sys_manager_service_id.
41 * @request_tag: Unique number to identify reply.
44 struct ps3_sys_manager_header {
55 #define dump_sm_header(_h) _dump_sm_header(_h, __func__, __LINE__)
56 static void __maybe_unused _dump_sm_header(
57 const struct ps3_sys_manager_header *h, const char *func, int line)
59 pr_debug("%s:%d: version: %xh\n", func, line, h->version);
60 pr_debug("%s:%d: size: %xh\n", func, line, h->size);
61 pr_debug("%s:%d: payload_size: %xh\n", func, line, h->payload_size);
62 pr_debug("%s:%d: service_id: %xh\n", func, line, h->service_id);
63 pr_debug("%s:%d: request_tag: %xh\n", func, line, h->request_tag);
67 * @PS3_SM_RX_MSG_LEN_MIN - Shortest received message length.
68 * @PS3_SM_RX_MSG_LEN_MAX - Longest received message length.
70 * Currently all messages received from the system manager are either
71 * (16 bytes header + 8 bytes payload = 24 bytes) or (16 bytes header
72 * + 16 bytes payload = 32 bytes). This knowledge is used to simplify
77 PS3_SM_RX_MSG_LEN_MIN = 24,
78 PS3_SM_RX_MSG_LEN_MAX = 32,
82 * enum ps3_sys_manager_service_id - Message header service_id.
83 * @PS3_SM_SERVICE_ID_REQUEST: guest --> sys_manager.
84 * @PS3_SM_SERVICE_ID_REQUEST_ERROR: guest <-- sys_manager.
85 * @PS3_SM_SERVICE_ID_COMMAND: guest <-- sys_manager.
86 * @PS3_SM_SERVICE_ID_RESPONSE: guest --> sys_manager.
87 * @PS3_SM_SERVICE_ID_SET_ATTR: guest --> sys_manager.
88 * @PS3_SM_SERVICE_ID_EXTERN_EVENT: guest <-- sys_manager.
89 * @PS3_SM_SERVICE_ID_SET_NEXT_OP: guest --> sys_manager.
91 * PS3_SM_SERVICE_ID_REQUEST_ERROR is returned for invalid data values in a
92 * a PS3_SM_SERVICE_ID_REQUEST message. It also seems to be returned when
93 * a REQUEST message is sent at the wrong time.
96 enum ps3_sys_manager_service_id {
98 PS3_SM_SERVICE_ID_REQUEST = 1,
99 PS3_SM_SERVICE_ID_RESPONSE = 2,
100 PS3_SM_SERVICE_ID_COMMAND = 3,
101 PS3_SM_SERVICE_ID_EXTERN_EVENT = 4,
102 PS3_SM_SERVICE_ID_SET_NEXT_OP = 5,
103 PS3_SM_SERVICE_ID_REQUEST_ERROR = 6,
104 PS3_SM_SERVICE_ID_SET_ATTR = 8,
108 * enum ps3_sys_manager_attr - Notification attribute (bit position mask).
109 * @PS3_SM_ATTR_POWER: Power button.
110 * @PS3_SM_ATTR_RESET: Reset button, not available on retail console.
111 * @PS3_SM_ATTR_THERMAL: System thermal alert.
112 * @PS3_SM_ATTR_CONTROLLER: Remote controller event.
113 * @PS3_SM_ATTR_ALL: Logical OR of all.
115 * The guest tells the system manager which events it is interested in receiving
116 * notice of by sending the system manager a logical OR of notification
117 * attributes via the ps3_sys_manager_send_attr() routine.
120 enum ps3_sys_manager_attr {
122 PS3_SM_ATTR_POWER = 1,
123 PS3_SM_ATTR_RESET = 2,
124 PS3_SM_ATTR_THERMAL = 4,
125 PS3_SM_ATTR_CONTROLLER = 8, /* bogus? */
126 PS3_SM_ATTR_ALL = 0x0f,
130 * enum ps3_sys_manager_event - External event type, reported by system manager.
131 * @PS3_SM_EVENT_POWER_PRESSED: payload.value =
132 * enum ps3_sys_manager_button_event.
133 * @PS3_SM_EVENT_POWER_RELEASED: payload.value = time pressed in millisec.
134 * @PS3_SM_EVENT_RESET_PRESSED: payload.value =
135 * enum ps3_sys_manager_button_event.
136 * @PS3_SM_EVENT_RESET_RELEASED: payload.value = time pressed in millisec.
137 * @PS3_SM_EVENT_THERMAL_ALERT: payload.value = thermal zone id.
138 * @PS3_SM_EVENT_THERMAL_CLEARED: payload.value = thermal zone id.
141 enum ps3_sys_manager_event {
143 PS3_SM_EVENT_POWER_PRESSED = 3,
144 PS3_SM_EVENT_POWER_RELEASED = 4,
145 PS3_SM_EVENT_RESET_PRESSED = 5,
146 PS3_SM_EVENT_RESET_RELEASED = 6,
147 PS3_SM_EVENT_THERMAL_ALERT = 7,
148 PS3_SM_EVENT_THERMAL_CLEARED = 8,
149 /* no info on controller events */
153 * enum ps3_sys_manager_button_event - Button event payload values.
154 * @PS3_SM_BUTTON_EVENT_HARD: Hardware generated event.
155 * @PS3_SM_BUTTON_EVENT_SOFT: Software generated event.
158 enum ps3_sys_manager_button_event {
159 PS3_SM_BUTTON_EVENT_HARD = 0,
160 PS3_SM_BUTTON_EVENT_SOFT = 1,
164 * enum ps3_sys_manager_next_op - Operation to perform after lpar is destroyed.
167 enum ps3_sys_manager_next_op {
169 PS3_SM_NEXT_OP_SYS_SHUTDOWN = 1,
170 PS3_SM_NEXT_OP_SYS_REBOOT = 2,
171 PS3_SM_NEXT_OP_LPAR_REBOOT = 0x82,
175 * enum ps3_sys_manager_wake_source - Next-op wakeup source (bit position mask).
176 * @PS3_SM_WAKE_DEFAULT: Disk insert, power button, eject button.
177 * @PS3_SM_WAKE_W_O_L: Ether or wireless LAN.
178 * @PS3_SM_WAKE_P_O_R: Power on reset.
180 * Additional wakeup sources when specifying PS3_SM_NEXT_OP_SYS_SHUTDOWN.
181 * The system will always wake from the PS3_SM_WAKE_DEFAULT sources.
182 * Sources listed here are the only ones available to guests in the
186 enum ps3_sys_manager_wake_source {
188 PS3_SM_WAKE_DEFAULT = 0,
189 PS3_SM_WAKE_W_O_L = 0x00000400,
190 PS3_SM_WAKE_P_O_R = 0x80000000,
194 * user_wake_sources - User specified wakeup sources.
196 * Logical OR of enum ps3_sys_manager_wake_source types.
199 static u32 user_wake_sources = PS3_SM_WAKE_DEFAULT;
202 * enum ps3_sys_manager_cmd - Command from system manager to guest.
204 * The guest completes the actions needed, then acks or naks the command via
205 * ps3_sys_manager_send_response(). In the case of @PS3_SM_CMD_SHUTDOWN,
206 * the guest must be fully prepared for a system poweroff prior to acking the
210 enum ps3_sys_manager_cmd {
212 PS3_SM_CMD_SHUTDOWN = 1, /* shutdown guest OS */
216 * ps3_sm_force_power_off - Poweroff helper.
218 * A global variable used to force a poweroff when the power button has
219 * been pressed irrespective of how init handles the ctrl_alt_del signal.
223 static unsigned int ps3_sm_force_power_off;
226 * ps3_sys_manager_write - Helper to write a two part message to the vuart.
230 static int ps3_sys_manager_write(struct ps3_system_bus_device *dev,
231 const struct ps3_sys_manager_header *header, const void *payload)
235 BUG_ON(header->version != 1);
236 BUG_ON(header->size != 16);
237 BUG_ON(header->payload_size != 8 && header->payload_size != 16);
238 BUG_ON(header->service_id > 8);
240 result = ps3_vuart_write(dev, header,
241 sizeof(struct ps3_sys_manager_header));
244 result = ps3_vuart_write(dev, payload, header->payload_size);
250 * ps3_sys_manager_send_attr - Send a 'set attribute' to the system manager.
254 static int ps3_sys_manager_send_attr(struct ps3_system_bus_device *dev,
255 enum ps3_sys_manager_attr attr)
257 struct ps3_sys_manager_header header;
264 BUILD_BUG_ON(sizeof(payload) != 8);
266 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, attr);
268 memset(&header, 0, sizeof(header));
271 header.payload_size = 16;
272 header.service_id = PS3_SM_SERVICE_ID_SET_ATTR;
274 memset(&payload, 0, sizeof(payload));
276 payload.attribute = attr;
278 return ps3_sys_manager_write(dev, &header, &payload);
282 * ps3_sys_manager_send_next_op - Send a 'set next op' to the system manager.
284 * Tell the system manager what to do after this lpar is destroyed.
287 static int ps3_sys_manager_send_next_op(struct ps3_system_bus_device *dev,
288 enum ps3_sys_manager_next_op op,
289 enum ps3_sys_manager_wake_source wake_source)
291 struct ps3_sys_manager_header header;
301 BUILD_BUG_ON(sizeof(payload) != 16);
303 dev_dbg(&dev->core, "%s:%d: (%xh)\n", __func__, __LINE__, op);
305 memset(&header, 0, sizeof(header));
308 header.payload_size = 16;
309 header.service_id = PS3_SM_SERVICE_ID_SET_NEXT_OP;
311 memset(&payload, 0, sizeof(payload));
314 payload.gos_id = 3; /* other os */
315 payload.wake_source = wake_source;
317 return ps3_sys_manager_write(dev, &header, &payload);
321 * ps3_sys_manager_send_request_shutdown - Send 'request' to the system manager.
323 * The guest sends this message to request an operation or action of the system
324 * manager. The reply is a command message from the system manager. In the
325 * command handler the guest performs the requested operation. The result of
326 * the command is then communicated back to the system manager with a response
329 * Currently, the only supported request is the 'shutdown self' request.
332 static int ps3_sys_manager_send_request_shutdown(
333 struct ps3_system_bus_device *dev)
335 struct ps3_sys_manager_header header;
343 BUILD_BUG_ON(sizeof(payload) != 16);
345 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
347 memset(&header, 0, sizeof(header));
350 header.payload_size = 16;
351 header.service_id = PS3_SM_SERVICE_ID_REQUEST;
353 memset(&payload, 0, sizeof(payload));
355 payload.type = 1; /* shutdown */
356 payload.gos_id = 0; /* self */
358 return ps3_sys_manager_write(dev, &header, &payload);
362 * ps3_sys_manager_send_response - Send a 'response' to the system manager.
363 * @status: zero = success, others fail.
365 * The guest sends this message to the system manager to acnowledge success or
366 * failure of a command sent by the system manager.
369 static int ps3_sys_manager_send_response(struct ps3_system_bus_device *dev,
372 struct ps3_sys_manager_header header;
380 BUILD_BUG_ON(sizeof(payload) != 16);
382 dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
383 (status ? "nak" : "ack"));
385 memset(&header, 0, sizeof(header));
388 header.payload_size = 16;
389 header.service_id = PS3_SM_SERVICE_ID_RESPONSE;
391 memset(&payload, 0, sizeof(payload));
393 payload.status = status;
395 return ps3_sys_manager_write(dev, &header, &payload);
399 * ps3_sys_manager_handle_event - Second stage event msg handler.
403 static int ps3_sys_manager_handle_event(struct ps3_system_bus_device *dev)
414 BUILD_BUG_ON(sizeof(event) != 16);
416 result = ps3_vuart_read(dev, &event, sizeof(event));
417 BUG_ON(result && "need to retry here");
419 if (event.version != 1) {
420 dev_dbg(&dev->core, "%s:%d: unsupported event version (%u)\n",
421 __func__, __LINE__, event.version);
425 switch (event.type) {
426 case PS3_SM_EVENT_POWER_PRESSED:
427 dev_dbg(&dev->core, "%s:%d: POWER_PRESSED (%s)\n",
429 (event.value == PS3_SM_BUTTON_EVENT_SOFT ? "soft"
431 ps3_sm_force_power_off = 1;
433 * A memory barrier is use here to sync memory since
434 * ps3_sys_manager_final_restart() could be called on
438 kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */
440 case PS3_SM_EVENT_POWER_RELEASED:
441 dev_dbg(&dev->core, "%s:%d: POWER_RELEASED (%u ms)\n",
442 __func__, __LINE__, event.value);
444 case PS3_SM_EVENT_RESET_PRESSED:
445 dev_dbg(&dev->core, "%s:%d: RESET_PRESSED (%s)\n",
447 (event.value == PS3_SM_BUTTON_EVENT_SOFT ? "soft"
449 ps3_sm_force_power_off = 0;
451 * A memory barrier is use here to sync memory since
452 * ps3_sys_manager_final_restart() could be called on
456 kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */
458 case PS3_SM_EVENT_RESET_RELEASED:
459 dev_dbg(&dev->core, "%s:%d: RESET_RELEASED (%u ms)\n",
460 __func__, __LINE__, event.value);
462 case PS3_SM_EVENT_THERMAL_ALERT:
463 dev_dbg(&dev->core, "%s:%d: THERMAL_ALERT (zone %u)\n",
464 __func__, __LINE__, event.value);
465 pr_info("PS3 Thermal Alert Zone %u\n", event.value);
467 case PS3_SM_EVENT_THERMAL_CLEARED:
468 dev_dbg(&dev->core, "%s:%d: THERMAL_CLEARED (zone %u)\n",
469 __func__, __LINE__, event.value);
472 dev_dbg(&dev->core, "%s:%d: unknown event (%u)\n",
473 __func__, __LINE__, event.type);
480 * ps3_sys_manager_handle_cmd - Second stage command msg handler.
482 * The system manager sends this in reply to a 'request' message from the guest.
485 static int ps3_sys_manager_handle_cmd(struct ps3_system_bus_device *dev)
494 BUILD_BUG_ON(sizeof(cmd) != 16);
496 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
498 result = ps3_vuart_read(dev, &cmd, sizeof(cmd));
499 BUG_ON(result && "need to retry here");
504 if (cmd.version != 1) {
505 dev_dbg(&dev->core, "%s:%d: unsupported cmd version (%u)\n",
506 __func__, __LINE__, cmd.version);
510 if (cmd.type != PS3_SM_CMD_SHUTDOWN) {
511 dev_dbg(&dev->core, "%s:%d: unknown cmd (%u)\n",
512 __func__, __LINE__, cmd.type);
516 ps3_sys_manager_send_response(dev, 0);
521 * ps3_sys_manager_handle_msg - First stage msg handler.
523 * Can be called directly to manually poll vuart and pump message handler.
526 static int ps3_sys_manager_handle_msg(struct ps3_system_bus_device *dev)
529 struct ps3_sys_manager_header header;
531 result = ps3_vuart_read(dev, &header,
532 sizeof(struct ps3_sys_manager_header));
537 if (header.version != 1) {
538 dev_dbg(&dev->core, "%s:%d: unsupported header version (%u)\n",
539 __func__, __LINE__, header.version);
540 dump_sm_header(&header);
544 BUILD_BUG_ON(sizeof(header) != 16);
546 if (header.size != 16 || (header.payload_size != 8
547 && header.payload_size != 16)) {
548 dump_sm_header(&header);
552 switch (header.service_id) {
553 case PS3_SM_SERVICE_ID_EXTERN_EVENT:
554 dev_dbg(&dev->core, "%s:%d: EVENT\n", __func__, __LINE__);
555 return ps3_sys_manager_handle_event(dev);
556 case PS3_SM_SERVICE_ID_COMMAND:
557 dev_dbg(&dev->core, "%s:%d: COMMAND\n", __func__, __LINE__);
558 return ps3_sys_manager_handle_cmd(dev);
559 case PS3_SM_SERVICE_ID_REQUEST_ERROR:
560 dev_dbg(&dev->core, "%s:%d: REQUEST_ERROR\n", __func__,
562 dump_sm_header(&header);
565 dev_dbg(&dev->core, "%s:%d: unknown service_id (%u)\n",
566 __func__, __LINE__, header.service_id);
572 ps3_vuart_clear_rx_bytes(dev, 0);
575 ps3_vuart_clear_rx_bytes(dev, header.payload_size);
579 static void ps3_sys_manager_fin(struct ps3_system_bus_device *dev)
581 ps3_sys_manager_send_request_shutdown(dev);
583 pr_emerg("System Halted, OK to turn off power\n");
585 while (ps3_sys_manager_handle_msg(dev)) {
586 /* pause until next DEC interrupt */
591 /* pause, ignoring DEC interrupt */
597 * ps3_sys_manager_final_power_off - The final platform machine_power_off routine.
599 * This routine never returns. The routine disables asynchronous vuart reads
600 * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
601 * the shutdown command sent from the system manager. Soon after the
602 * acknowledgement is sent the lpar is destroyed by the HV. This routine
603 * should only be called from ps3_power_off() through
604 * ps3_sys_manager_ops.power_off.
607 static void ps3_sys_manager_final_power_off(struct ps3_system_bus_device *dev)
611 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
613 ps3_vuart_cancel_async(dev);
615 ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_SHUTDOWN,
618 ps3_sys_manager_fin(dev);
622 * ps3_sys_manager_final_restart - The final platform machine_restart routine.
624 * This routine never returns. The routine disables asynchronous vuart reads
625 * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
626 * the shutdown command sent from the system manager. Soon after the
627 * acknowledgement is sent the lpar is destroyed by the HV. This routine
628 * should only be called from ps3_restart() through ps3_sys_manager_ops.restart.
631 static void ps3_sys_manager_final_restart(struct ps3_system_bus_device *dev)
635 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
637 /* Check if we got here via a power button event. */
639 if (ps3_sm_force_power_off) {
640 dev_dbg(&dev->core, "%s:%d: forcing poweroff\n",
642 ps3_sys_manager_final_power_off(dev);
645 ps3_vuart_cancel_async(dev);
647 ps3_sys_manager_send_attr(dev, 0);
648 ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_REBOOT,
651 ps3_sys_manager_fin(dev);
655 * ps3_sys_manager_get_wol - Get wake-on-lan setting.
658 int ps3_sys_manager_get_wol(void)
660 pr_debug("%s:%d\n", __func__, __LINE__);
662 return (user_wake_sources & PS3_SM_WAKE_W_O_L) != 0;
664 EXPORT_SYMBOL_GPL(ps3_sys_manager_get_wol);
667 * ps3_sys_manager_set_wol - Set wake-on-lan setting.
670 void ps3_sys_manager_set_wol(int state)
672 static DEFINE_MUTEX(mutex);
676 pr_debug("%s:%d: %d\n", __func__, __LINE__, state);
679 user_wake_sources |= PS3_SM_WAKE_W_O_L;
681 user_wake_sources &= ~PS3_SM_WAKE_W_O_L;
682 mutex_unlock(&mutex);
684 EXPORT_SYMBOL_GPL(ps3_sys_manager_set_wol);
687 * ps3_sys_manager_work - Asynchronous read handler.
689 * Signaled when PS3_SM_RX_MSG_LEN_MIN bytes arrive at the vuart port.
692 static void ps3_sys_manager_work(struct ps3_system_bus_device *dev)
694 ps3_sys_manager_handle_msg(dev);
695 ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN);
698 static int ps3_sys_manager_probe(struct ps3_system_bus_device *dev)
701 struct ps3_sys_manager_ops ops;
703 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
705 ops.power_off = ps3_sys_manager_final_power_off;
706 ops.restart = ps3_sys_manager_final_restart;
709 /* ps3_sys_manager_register_ops copies ops. */
711 ps3_sys_manager_register_ops(&ops);
713 result = ps3_sys_manager_send_attr(dev, PS3_SM_ATTR_ALL);
716 result = ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN);
722 static int ps3_sys_manager_remove(struct ps3_system_bus_device *dev)
724 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
728 static void ps3_sys_manager_shutdown(struct ps3_system_bus_device *dev)
730 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
733 static struct ps3_vuart_port_driver ps3_sys_manager = {
734 .core.match_id = PS3_MATCH_ID_SYSTEM_MANAGER,
735 .core.core.name = "ps3_sys_manager",
736 .probe = ps3_sys_manager_probe,
737 .remove = ps3_sys_manager_remove,
738 .shutdown = ps3_sys_manager_shutdown,
739 .work = ps3_sys_manager_work,
742 static int __init ps3_sys_manager_init(void)
744 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
747 return ps3_vuart_port_driver_register(&ps3_sys_manager);
750 module_init(ps3_sys_manager_init);
751 /* Module remove not supported. */
753 MODULE_AUTHOR("Sony Corporation");
754 MODULE_LICENSE("GPL v2");
755 MODULE_DESCRIPTION("PS3 System Manager");
756 MODULE_ALIAS(PS3_MODULE_ALIAS_SYSTEM_MANAGER);