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>
27 #include <asm/lv1call.h>
33 * ps3_sys_manager - PS3 system manager driver.
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.
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
47 * struct ps3_sys_manager_header - System manager message header.
48 * @version: Header version, currently 1.
49 * @size: Header size in bytes, currently 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.
55 struct ps3_sys_manager_header {
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)
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);
78 * @PS3_SM_RX_MSG_LEN_MIN - Shortest received message length.
79 * @PS3_SM_RX_MSG_LEN_MAX - Longest received message length.
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
88 PS3_SM_RX_MSG_LEN_MIN = 24,
89 PS3_SM_RX_MSG_LEN_MAX = 32,
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.
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.
107 enum ps3_sys_manager_service_id {
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,
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.
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.
131 enum ps3_sys_manager_attr {
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,
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.
152 enum ps3_sys_manager_event {
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 */
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.
169 enum ps3_sys_manager_button_event {
170 PS3_SM_BUTTON_EVENT_HARD = 0,
171 PS3_SM_BUTTON_EVENT_SOFT = 1,
175 * enum ps3_sys_manager_next_op - Operation to perform after lpar is destroyed.
178 enum ps3_sys_manager_next_op {
180 PS3_SM_NEXT_OP_SYS_SHUTDOWN = 1,
181 PS3_SM_NEXT_OP_SYS_REBOOT = 2,
182 PS3_SM_NEXT_OP_LPAR_REBOOT = 0x82,
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.
188 * @PS3_SM_WAKE_W_O_L: Ether or wireless LAN.
189 * @PS3_SM_WAKE_P_O_R: Power on reset.
191 * Additional wakeup sources when specifying PS3_SM_NEXT_OP_SYS_SHUTDOWN.
192 * The system will always wake from the PS3_SM_WAKE_DEFAULT sources.
193 * Sources listed here are the only ones available to guests in the
197 enum ps3_sys_manager_wake_source {
199 PS3_SM_WAKE_DEFAULT = 0,
200 PS3_SM_WAKE_W_O_L = 0x00000400,
201 PS3_SM_WAKE_P_O_R = 0x80000000,
205 * user_wake_sources - User specified wakeup sources.
207 * Logical OR of enum ps3_sys_manager_wake_source types.
210 static u32 user_wake_sources = PS3_SM_WAKE_DEFAULT;
213 * enum ps3_sys_manager_cmd - Command from system manager to guest.
215 * The guest completes the actions needed, then acks or naks the command via
216 * ps3_sys_manager_send_response(). In the case of @PS3_SM_CMD_SHUTDOWN,
217 * the guest must be fully prepared for a system poweroff prior to acking the
221 enum ps3_sys_manager_cmd {
223 PS3_SM_CMD_SHUTDOWN = 1, /* shutdown guest OS */
227 * ps3_sm_force_power_off - Poweroff helper.
229 * A global variable used to force a poweroff when the power button has
230 * been pressed irrespective of how init handles the ctrl_alt_del signal.
234 static unsigned int ps3_sm_force_power_off;
237 * ps3_sys_manager_write - Helper to write a two part message to the vuart.
241 static int ps3_sys_manager_write(struct ps3_system_bus_device *dev,
242 const struct ps3_sys_manager_header *header, const void *payload)
246 BUG_ON(header->version != 1);
247 BUG_ON(header->size != 16);
248 BUG_ON(header->payload_size != 8 && header->payload_size != 16);
249 BUG_ON(header->service_id > 8);
251 result = ps3_vuart_write(dev, header,
252 sizeof(struct ps3_sys_manager_header));
255 result = ps3_vuart_write(dev, payload, header->payload_size);
261 * ps3_sys_manager_send_attr - Send a 'set attribute' to the system manager.
265 static int ps3_sys_manager_send_attr(struct ps3_system_bus_device *dev,
266 enum ps3_sys_manager_attr attr)
268 struct ps3_sys_manager_header header;
275 BUILD_BUG_ON(sizeof(payload) != 8);
277 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, attr);
279 memset(&header, 0, sizeof(header));
282 header.payload_size = 16;
283 header.service_id = PS3_SM_SERVICE_ID_SET_ATTR;
285 memset(&payload, 0, sizeof(payload));
287 payload.attribute = attr;
289 return ps3_sys_manager_write(dev, &header, &payload);
293 * ps3_sys_manager_send_next_op - Send a 'set next op' to the system manager.
295 * Tell the system manager what to do after this lpar is destroyed.
298 static int ps3_sys_manager_send_next_op(struct ps3_system_bus_device *dev,
299 enum ps3_sys_manager_next_op op,
300 enum ps3_sys_manager_wake_source wake_source)
302 struct ps3_sys_manager_header header;
312 BUILD_BUG_ON(sizeof(payload) != 16);
314 dev_dbg(&dev->core, "%s:%d: (%xh)\n", __func__, __LINE__, op);
316 memset(&header, 0, sizeof(header));
319 header.payload_size = 16;
320 header.service_id = PS3_SM_SERVICE_ID_SET_NEXT_OP;
322 memset(&payload, 0, sizeof(payload));
325 payload.gos_id = 3; /* other os */
326 payload.wake_source = wake_source;
328 return ps3_sys_manager_write(dev, &header, &payload);
332 * ps3_sys_manager_send_request_shutdown - Send 'request' to the system manager.
334 * The guest sends this message to request an operation or action of the system
335 * manager. The reply is a command message from the system manager. In the
336 * command handler the guest performs the requested operation. The result of
337 * the command is then communicated back to the system manager with a response
340 * Currently, the only supported request is the 'shutdown self' request.
343 static int ps3_sys_manager_send_request_shutdown(
344 struct ps3_system_bus_device *dev)
346 struct ps3_sys_manager_header header;
354 BUILD_BUG_ON(sizeof(payload) != 16);
356 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
358 memset(&header, 0, sizeof(header));
361 header.payload_size = 16;
362 header.service_id = PS3_SM_SERVICE_ID_REQUEST;
364 memset(&payload, 0, sizeof(payload));
366 payload.type = 1; /* shutdown */
367 payload.gos_id = 0; /* self */
369 return ps3_sys_manager_write(dev, &header, &payload);
373 * ps3_sys_manager_send_response - Send a 'response' to the system manager.
374 * @status: zero = success, others fail.
376 * The guest sends this message to the system manager to acnowledge success or
377 * failure of a command sent by the system manager.
380 static int ps3_sys_manager_send_response(struct ps3_system_bus_device *dev,
383 struct ps3_sys_manager_header header;
391 BUILD_BUG_ON(sizeof(payload) != 16);
393 dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
394 (status ? "nak" : "ack"));
396 memset(&header, 0, sizeof(header));
399 header.payload_size = 16;
400 header.service_id = PS3_SM_SERVICE_ID_RESPONSE;
402 memset(&payload, 0, sizeof(payload));
404 payload.status = status;
406 return ps3_sys_manager_write(dev, &header, &payload);
410 * ps3_sys_manager_handle_event - Second stage event msg handler.
414 static int ps3_sys_manager_handle_event(struct ps3_system_bus_device *dev)
425 BUILD_BUG_ON(sizeof(event) != 16);
427 result = ps3_vuart_read(dev, &event, sizeof(event));
428 BUG_ON(result && "need to retry here");
430 if (event.version != 1) {
431 dev_dbg(&dev->core, "%s:%d: unsupported event version (%u)\n",
432 __func__, __LINE__, event.version);
436 switch (event.type) {
437 case PS3_SM_EVENT_POWER_PRESSED:
438 dev_dbg(&dev->core, "%s:%d: POWER_PRESSED (%s)\n",
440 (event.value == PS3_SM_BUTTON_EVENT_SOFT ? "soft"
442 ps3_sm_force_power_off = 1;
444 * A memory barrier is use here to sync memory since
445 * ps3_sys_manager_final_restart() could be called on
449 kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */
451 case PS3_SM_EVENT_POWER_RELEASED:
452 dev_dbg(&dev->core, "%s:%d: POWER_RELEASED (%u ms)\n",
453 __func__, __LINE__, event.value);
455 case PS3_SM_EVENT_RESET_PRESSED:
456 dev_dbg(&dev->core, "%s:%d: RESET_PRESSED (%s)\n",
458 (event.value == PS3_SM_BUTTON_EVENT_SOFT ? "soft"
460 ps3_sm_force_power_off = 0;
462 * A memory barrier is use here to sync memory since
463 * ps3_sys_manager_final_restart() could be called on
467 kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */
469 case PS3_SM_EVENT_RESET_RELEASED:
470 dev_dbg(&dev->core, "%s:%d: RESET_RELEASED (%u ms)\n",
471 __func__, __LINE__, event.value);
473 case PS3_SM_EVENT_THERMAL_ALERT:
474 dev_dbg(&dev->core, "%s:%d: THERMAL_ALERT (zone %u)\n",
475 __func__, __LINE__, event.value);
476 pr_info("PS3 Thermal Alert Zone %u\n", event.value);
478 case PS3_SM_EVENT_THERMAL_CLEARED:
479 dev_dbg(&dev->core, "%s:%d: THERMAL_CLEARED (zone %u)\n",
480 __func__, __LINE__, event.value);
483 dev_dbg(&dev->core, "%s:%d: unknown event (%u)\n",
484 __func__, __LINE__, event.type);
491 * ps3_sys_manager_handle_cmd - Second stage command msg handler.
493 * The system manager sends this in reply to a 'request' message from the guest.
496 static int ps3_sys_manager_handle_cmd(struct ps3_system_bus_device *dev)
505 BUILD_BUG_ON(sizeof(cmd) != 16);
507 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
509 result = ps3_vuart_read(dev, &cmd, sizeof(cmd));
510 BUG_ON(result && "need to retry here");
515 if (cmd.version != 1) {
516 dev_dbg(&dev->core, "%s:%d: unsupported cmd version (%u)\n",
517 __func__, __LINE__, cmd.version);
521 if (cmd.type != PS3_SM_CMD_SHUTDOWN) {
522 dev_dbg(&dev->core, "%s:%d: unknown cmd (%u)\n",
523 __func__, __LINE__, cmd.type);
527 ps3_sys_manager_send_response(dev, 0);
532 * ps3_sys_manager_handle_msg - First stage msg handler.
534 * Can be called directly to manually poll vuart and pump message handler.
537 static int ps3_sys_manager_handle_msg(struct ps3_system_bus_device *dev)
540 struct ps3_sys_manager_header header;
542 result = ps3_vuart_read(dev, &header,
543 sizeof(struct ps3_sys_manager_header));
548 if (header.version != 1) {
549 dev_dbg(&dev->core, "%s:%d: unsupported header version (%u)\n",
550 __func__, __LINE__, header.version);
551 dump_sm_header(&header);
555 BUILD_BUG_ON(sizeof(header) != 16);
557 if (header.size != 16 || (header.payload_size != 8
558 && header.payload_size != 16)) {
559 dump_sm_header(&header);
563 switch (header.service_id) {
564 case PS3_SM_SERVICE_ID_EXTERN_EVENT:
565 dev_dbg(&dev->core, "%s:%d: EVENT\n", __func__, __LINE__);
566 return ps3_sys_manager_handle_event(dev);
567 case PS3_SM_SERVICE_ID_COMMAND:
568 dev_dbg(&dev->core, "%s:%d: COMMAND\n", __func__, __LINE__);
569 return ps3_sys_manager_handle_cmd(dev);
570 case PS3_SM_SERVICE_ID_REQUEST_ERROR:
571 dev_dbg(&dev->core, "%s:%d: REQUEST_ERROR\n", __func__,
573 dump_sm_header(&header);
576 dev_dbg(&dev->core, "%s:%d: unknown service_id (%u)\n",
577 __func__, __LINE__, header.service_id);
583 ps3_vuart_clear_rx_bytes(dev, 0);
586 ps3_vuart_clear_rx_bytes(dev, header.payload_size);
590 static void ps3_sys_manager_fin(struct ps3_system_bus_device *dev)
592 ps3_sys_manager_send_request_shutdown(dev);
594 pr_emerg("System Halted, OK to turn off power\n");
596 while (ps3_sys_manager_handle_msg(dev)) {
597 /* pause until next DEC interrupt */
602 /* pause, ignoring DEC interrupt */
608 * ps3_sys_manager_final_power_off - The final platform machine_power_off routine.
610 * This routine never returns. The routine disables asynchronous vuart reads
611 * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
612 * the shutdown command sent from the system manager. Soon after the
613 * acknowledgement is sent the lpar is destroyed by the HV. This routine
614 * should only be called from ps3_power_off() through
615 * ps3_sys_manager_ops.power_off.
618 static void ps3_sys_manager_final_power_off(struct ps3_system_bus_device *dev)
622 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
624 ps3_vuart_cancel_async(dev);
626 ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_SHUTDOWN,
629 ps3_sys_manager_fin(dev);
633 * ps3_sys_manager_final_restart - The final platform machine_restart routine.
635 * This routine never returns. The routine disables asynchronous vuart reads
636 * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
637 * the shutdown command sent from the system manager. Soon after the
638 * acknowledgement is sent the lpar is destroyed by the HV. This routine
639 * should only be called from ps3_restart() through ps3_sys_manager_ops.restart.
642 static void ps3_sys_manager_final_restart(struct ps3_system_bus_device *dev)
646 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
648 /* Check if we got here via a power button event. */
650 if (ps3_sm_force_power_off) {
651 dev_dbg(&dev->core, "%s:%d: forcing poweroff\n",
653 ps3_sys_manager_final_power_off(dev);
656 ps3_vuart_cancel_async(dev);
658 ps3_sys_manager_send_attr(dev, 0);
659 ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_REBOOT,
662 ps3_sys_manager_fin(dev);
666 * ps3_sys_manager_get_wol - Get wake-on-lan setting.
669 int ps3_sys_manager_get_wol(void)
671 pr_debug("%s:%d\n", __func__, __LINE__);
673 return (user_wake_sources & PS3_SM_WAKE_W_O_L) != 0;
675 EXPORT_SYMBOL_GPL(ps3_sys_manager_get_wol);
678 * ps3_sys_manager_set_wol - Set wake-on-lan setting.
681 void ps3_sys_manager_set_wol(int state)
683 static DEFINE_MUTEX(mutex);
687 pr_debug("%s:%d: %d\n", __func__, __LINE__, state);
690 user_wake_sources |= PS3_SM_WAKE_W_O_L;
692 user_wake_sources &= ~PS3_SM_WAKE_W_O_L;
693 mutex_unlock(&mutex);
695 EXPORT_SYMBOL_GPL(ps3_sys_manager_set_wol);
698 * ps3_sys_manager_work - Asynchronous read handler.
700 * Signaled when PS3_SM_RX_MSG_LEN_MIN bytes arrive at the vuart port.
703 static void ps3_sys_manager_work(struct ps3_system_bus_device *dev)
705 ps3_sys_manager_handle_msg(dev);
706 ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN);
709 static int __devinit ps3_sys_manager_probe(struct ps3_system_bus_device *dev)
712 struct ps3_sys_manager_ops ops;
714 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
716 ops.power_off = ps3_sys_manager_final_power_off;
717 ops.restart = ps3_sys_manager_final_restart;
720 /* ps3_sys_manager_register_ops copies ops. */
722 ps3_sys_manager_register_ops(&ops);
724 result = ps3_sys_manager_send_attr(dev, PS3_SM_ATTR_ALL);
727 result = ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN);
733 static int ps3_sys_manager_remove(struct ps3_system_bus_device *dev)
735 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
739 static void ps3_sys_manager_shutdown(struct ps3_system_bus_device *dev)
741 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
744 static struct ps3_vuart_port_driver ps3_sys_manager = {
745 .core.match_id = PS3_MATCH_ID_SYSTEM_MANAGER,
746 .core.core.name = "ps3_sys_manager",
747 .probe = ps3_sys_manager_probe,
748 .remove = ps3_sys_manager_remove,
749 .shutdown = ps3_sys_manager_shutdown,
750 .work = ps3_sys_manager_work,
753 static int __init ps3_sys_manager_init(void)
755 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
758 return ps3_vuart_port_driver_register(&ps3_sys_manager);
761 module_init(ps3_sys_manager_init);
762 /* Module remove not supported. */
764 MODULE_AUTHOR("Sony Corporation");
765 MODULE_LICENSE("GPL v2");
766 MODULE_DESCRIPTION("PS3 System Manager");
767 MODULE_ALIAS(PS3_MODULE_ALIAS_SYSTEM_MANAGER);