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>
28 MODULE_AUTHOR("Sony Corporation");
29 MODULE_LICENSE("GPL v2");
30 MODULE_DESCRIPTION("PS3 System Manager");
33 * ps3_sys_manager - PS3 system manager driver.
35 * The system manager provides an asyncronous 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, curently 16.
50 * @payload_size: Message payload size in bytes.
51 * @service_id: Message type, one of enum ps3_sys_manager_service_id.
54 struct ps3_sys_manager_header {
65 * @PS3_SM_RX_MSG_LEN - System manager received message length.
67 * Currently all messages received from the system manager are the same length
68 * (16 bytes header + 16 bytes payload = 32 bytes). This knowlege is used to
73 PS3_SM_RX_MSG_LEN = 32,
77 * enum ps3_sys_manager_service_id - Message header service_id.
78 * @PS3_SM_SERVICE_ID_REQUEST: guest --> sys_manager.
79 * @PS3_SM_SERVICE_ID_COMMAND: guest <-- sys_manager.
80 * @PS3_SM_SERVICE_ID_RESPONSE: guest --> sys_manager.
81 * @PS3_SM_SERVICE_ID_SET_ATTR: guest --> sys_manager.
82 * @PS3_SM_SERVICE_ID_EXTERN_EVENT: guest <-- sys_manager.
83 * @PS3_SM_SERVICE_ID_SET_NEXT_OP: guest --> sys_manager.
86 enum ps3_sys_manager_service_id {
88 PS3_SM_SERVICE_ID_REQUEST = 1,
89 PS3_SM_SERVICE_ID_RESPONSE = 2,
90 PS3_SM_SERVICE_ID_COMMAND = 3,
91 PS3_SM_SERVICE_ID_EXTERN_EVENT = 4,
92 PS3_SM_SERVICE_ID_SET_NEXT_OP = 5,
93 PS3_SM_SERVICE_ID_SET_ATTR = 8,
97 * enum ps3_sys_manager_attr - Notification attribute (bit position mask).
98 * @PS3_SM_ATTR_POWER: Power button.
99 * @PS3_SM_ATTR_RESET: Reset button, not available on retail console.
100 * @PS3_SM_ATTR_THERMAL: Sytem thermal alert.
101 * @PS3_SM_ATTR_CONTROLLER: Remote controller event.
102 * @PS3_SM_ATTR_ALL: Logical OR of all.
104 * The guest tells the system manager which events it is interested in receiving
105 * notice of by sending the system manager a logical OR of notification
106 * attributes via the ps3_sys_manager_send_attr() routine.
109 enum ps3_sys_manager_attr {
111 PS3_SM_ATTR_POWER = 1,
112 PS3_SM_ATTR_RESET = 2,
113 PS3_SM_ATTR_THERMAL = 4,
114 PS3_SM_ATTR_CONTROLLER = 8, /* bogus? */
115 PS3_SM_ATTR_ALL = 0x0f,
119 * enum ps3_sys_manager_event - External event type, reported by system manager.
120 * @PS3_SM_EVENT_POWER_PRESSED: payload.value not used.
121 * @PS3_SM_EVENT_POWER_RELEASED: payload.value = time pressed in millisec.
122 * @PS3_SM_EVENT_RESET_PRESSED: payload.value not used.
123 * @PS3_SM_EVENT_RESET_RELEASED: payload.value = time pressed in millisec.
124 * @PS3_SM_EVENT_THERMAL_ALERT: payload.value = thermal zone id.
125 * @PS3_SM_EVENT_THERMAL_CLEARED: payload.value = thermal zone id.
128 enum ps3_sys_manager_event {
130 PS3_SM_EVENT_POWER_PRESSED = 3,
131 PS3_SM_EVENT_POWER_RELEASED = 4,
132 PS3_SM_EVENT_RESET_PRESSED = 5,
133 PS3_SM_EVENT_RESET_RELEASED = 6,
134 PS3_SM_EVENT_THERMAL_ALERT = 7,
135 PS3_SM_EVENT_THERMAL_CLEARED = 8,
136 /* no info on controller events */
140 * enum ps3_sys_manager_next_op - Operation to perform after lpar is destroyed.
143 enum ps3_sys_manager_next_op {
145 PS3_SM_NEXT_OP_SYS_SHUTDOWN = 1,
146 PS3_SM_NEXT_OP_SYS_REBOOT = 2,
147 PS3_SM_NEXT_OP_LPAR_REBOOT = 0x82,
151 * enum ps3_sys_manager_wake_source - Next-op wakeup source (bit position mask).
152 * @PS3_SM_WAKE_DEFAULT: Disk insert, power button, eject button, IR
153 * controller, and bluetooth controller.
155 * @PS3_SM_WAKE_RTC_ERROR:
156 * @PS3_SM_WAKE_P_O_R: Power on reset.
158 * Additional wakeup sources when specifying PS3_SM_NEXT_OP_SYS_SHUTDOWN.
159 * System will always wake from the PS3_SM_WAKE_DEFAULT sources.
162 enum ps3_sys_manager_wake_source {
164 PS3_SM_WAKE_DEFAULT = 0,
165 PS3_SM_WAKE_RTC = 0x00000040,
166 PS3_SM_WAKE_RTC_ERROR = 0x00000080,
167 PS3_SM_WAKE_P_O_R = 0x10000000,
171 * enum ps3_sys_manager_cmd - Command from system manager to guest.
173 * The guest completes the actions needed, then acks or naks the command via
174 * ps3_sys_manager_send_response(). In the case of @PS3_SM_CMD_SHUTDOWN,
175 * the guest must be fully prepared for a system poweroff prior to acking the
179 enum ps3_sys_manager_cmd {
181 PS3_SM_CMD_SHUTDOWN = 1, /* shutdown guest OS */
185 * ps3_sys_manager_write - Helper to write a two part message to the vuart.
189 static int ps3_sys_manager_write(struct ps3_vuart_port_device *dev,
190 const struct ps3_sys_manager_header *header, const void *payload)
194 BUG_ON(header->version != 1);
195 BUG_ON(header->size != 16);
196 BUG_ON(header->payload_size != 8 && header->payload_size != 16);
197 BUG_ON(header->service_id > 8);
199 result = ps3_vuart_write(dev, header,
200 sizeof(struct ps3_sys_manager_header));
203 result = ps3_vuart_write(dev, payload, header->payload_size);
209 * ps3_sys_manager_send_attr - Send a 'set attribute' to the system manager.
213 static int ps3_sys_manager_send_attr(struct ps3_vuart_port_device *dev,
214 enum ps3_sys_manager_attr attr)
216 static const struct ps3_sys_manager_header header = {
220 .service_id = PS3_SM_SERVICE_ID_SET_ATTR,
228 BUILD_BUG_ON(sizeof(payload) != 8);
230 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, attr);
232 memset(&payload, 0, sizeof(payload));
234 payload.attribute = attr;
236 return ps3_sys_manager_write(dev, &header, &payload);
240 * ps3_sys_manager_send_next_op - Send a 'set next op' to the system manager.
242 * Tell the system manager what to do after this lpar is destroyed.
245 static int ps3_sys_manager_send_next_op(struct ps3_vuart_port_device *dev,
246 enum ps3_sys_manager_next_op op,
247 enum ps3_sys_manager_wake_source wake_source)
249 static const struct ps3_sys_manager_header header = {
253 .service_id = PS3_SM_SERVICE_ID_SET_NEXT_OP,
264 BUILD_BUG_ON(sizeof(payload) != 16);
266 dev_dbg(&dev->core, "%s:%d: (%xh)\n", __func__, __LINE__, op);
268 memset(&payload, 0, sizeof(payload));
271 payload.gos_id = 3; /* other os */
272 payload.wake_source = wake_source;
274 return ps3_sys_manager_write(dev, &header, &payload);
278 * ps3_sys_manager_send_request_shutdown - Send 'request' to the system manager.
280 * The guest sends this message to request an operation or action of the system
281 * manager. The reply is a command message from the system manager. In the
282 * command handler the guest performs the requested operation. The result of
283 * the command is then communicated back to the system manager with a response
286 * Currently, the only supported request it the 'shutdown self' request.
289 static int ps3_sys_manager_send_request_shutdown(struct ps3_vuart_port_device *dev)
291 static const struct ps3_sys_manager_header header = {
295 .service_id = PS3_SM_SERVICE_ID_REQUEST,
302 } static const payload = {
304 .type = 1, /* shutdown */
305 .gos_id = 0, /* self */
308 BUILD_BUG_ON(sizeof(payload) != 16);
310 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
312 return ps3_sys_manager_write(dev, &header, &payload);
316 * ps3_sys_manager_send_response - Send a 'response' to the system manager.
317 * @status: zero = success, others fail.
319 * The guest sends this message to the system manager to acnowledge success or
320 * failure of a command sent by the system manager.
323 static int ps3_sys_manager_send_response(struct ps3_vuart_port_device *dev,
326 static const struct ps3_sys_manager_header header = {
330 .service_id = PS3_SM_SERVICE_ID_RESPONSE,
339 BUILD_BUG_ON(sizeof(payload) != 16);
341 dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
342 (status ? "nak" : "ack"));
344 memset(&payload, 0, sizeof(payload));
346 payload.status = status;
348 return ps3_sys_manager_write(dev, &header, &payload);
352 * ps3_sys_manager_handle_event - Second stage event msg handler.
356 static int ps3_sys_manager_handle_event(struct ps3_vuart_port_device *dev)
367 BUILD_BUG_ON(sizeof(event) != 16);
369 result = ps3_vuart_read(dev, &event, sizeof(event));
372 if (event.version != 1) {
373 dev_dbg(&dev->core, "%s:%d: unsupported event version (%u)\n",
374 __func__, __LINE__, event.version);
378 switch (event.type) {
379 case PS3_SM_EVENT_POWER_PRESSED:
380 dev_dbg(&dev->core, "%s:%d: POWER_PRESSED\n",
383 case PS3_SM_EVENT_POWER_RELEASED:
384 dev_dbg(&dev->core, "%s:%d: POWER_RELEASED (%u ms)\n",
385 __func__, __LINE__, event.value);
386 kill_cad_pid(SIGINT, 1);
388 case PS3_SM_EVENT_THERMAL_ALERT:
389 dev_dbg(&dev->core, "%s:%d: THERMAL_ALERT (zone %u)\n",
390 __func__, __LINE__, event.value);
391 printk(KERN_INFO "PS3 Thermal Alert Zone %u\n", event.value);
393 case PS3_SM_EVENT_THERMAL_CLEARED:
394 dev_dbg(&dev->core, "%s:%d: THERMAL_CLEARED (zone %u)\n",
395 __func__, __LINE__, event.value);
398 dev_dbg(&dev->core, "%s:%d: unknown event (%u)\n",
399 __func__, __LINE__, event.type);
406 * ps3_sys_manager_handle_cmd - Second stage command msg handler.
408 * The system manager sends this in reply to a 'request' message from the guest.
411 static int ps3_sys_manager_handle_cmd(struct ps3_vuart_port_device *dev)
420 BUILD_BUG_ON(sizeof(cmd) != 16);
422 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
424 result = ps3_vuart_read(dev, &cmd, sizeof(cmd));
429 if (cmd.version != 1) {
430 dev_dbg(&dev->core, "%s:%d: unsupported cmd version (%u)\n",
431 __func__, __LINE__, cmd.version);
435 if (cmd.type != PS3_SM_CMD_SHUTDOWN) {
436 dev_dbg(&dev->core, "%s:%d: unknown cmd (%u)\n",
437 __func__, __LINE__, cmd.type);
441 ps3_sys_manager_send_response(dev, 0);
446 * ps3_sys_manager_handle_msg - First stage msg handler.
450 static int ps3_sys_manager_handle_msg(struct ps3_vuart_port_device *dev)
453 struct ps3_sys_manager_header header;
455 result = ps3_vuart_read(dev, &header,
456 sizeof(struct ps3_sys_manager_header));
461 if (header.version != 1) {
462 dev_dbg(&dev->core, "%s:%d: unsupported header version (%u)\n",
463 __func__, __LINE__, header.version);
467 BUILD_BUG_ON(sizeof(header) != 16);
468 BUG_ON(header.size != 16);
469 BUG_ON(header.payload_size != 16);
471 switch (header.service_id) {
472 case PS3_SM_SERVICE_ID_EXTERN_EVENT:
473 dev_dbg(&dev->core, "%s:%d: EVENT\n", __func__, __LINE__);
474 return ps3_sys_manager_handle_event(dev);
475 case PS3_SM_SERVICE_ID_COMMAND:
476 dev_dbg(&dev->core, "%s:%d: COMMAND\n", __func__, __LINE__);
477 return ps3_sys_manager_handle_cmd(dev);
479 dev_dbg(&dev->core, "%s:%d: unknown service_id (%u)\n",
480 __func__, __LINE__, header.service_id);
486 ps3_vuart_clear_rx_bytes(dev, 0);
489 ps3_vuart_clear_rx_bytes(dev, header.payload_size);
494 * ps3_sys_manager_work - Asyncronous read handler.
496 * Signaled when a complete message arrives at the vuart port.
499 static void ps3_sys_manager_work(struct work_struct *work)
501 struct ps3_vuart_port_device *dev = ps3_vuart_work_to_port_device(work);
503 ps3_sys_manager_handle_msg(dev);
504 ps3_vuart_read_async(dev, ps3_sys_manager_work, PS3_SM_RX_MSG_LEN);
508 struct ps3_vuart_port_device *dev;
512 * ps3_sys_manager_restart - The final platform machine_restart routine.
514 * This routine never returns. The routine disables asyncronous vuart reads
515 * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
516 * the shutdown command sent from the system manager. Soon after the
517 * acknowledgement is sent the lpar is destroyed by the HV. This routine
518 * should only be called from ps3_restart().
521 void ps3_sys_manager_restart(void)
523 struct ps3_vuart_port_device *dev = drv_priv.dev;
525 BUG_ON(!drv_priv.dev);
527 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
529 ps3_vuart_cancel_async(dev);
531 ps3_sys_manager_send_attr(dev, 0);
532 ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_LPAR_REBOOT,
533 PS3_SM_WAKE_DEFAULT);
534 ps3_sys_manager_send_request_shutdown(dev);
536 printk(KERN_EMERG "System Halted, OK to turn off power\n");
539 ps3_sys_manager_handle_msg(dev);
543 * ps3_sys_manager_power_off - The final platform machine_power_off routine.
545 * This routine never returns. The routine disables asyncronous vuart reads
546 * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
547 * the shutdown command sent from the system manager. Soon after the
548 * acknowledgement is sent the lpar is destroyed by the HV. This routine
549 * should only be called from ps3_power_off().
552 void ps3_sys_manager_power_off(void)
554 struct ps3_vuart_port_device *dev = drv_priv.dev;
556 BUG_ON(!drv_priv.dev);
558 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
560 ps3_vuart_cancel_async(dev);
562 ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_SHUTDOWN,
563 PS3_SM_WAKE_DEFAULT);
564 ps3_sys_manager_send_request_shutdown(dev);
566 printk(KERN_EMERG "System Halted, OK to turn off power\n");
569 ps3_sys_manager_handle_msg(dev);
572 static int ps3_sys_manager_probe(struct ps3_vuart_port_device *dev)
576 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
578 BUG_ON(drv_priv.dev);
581 result = ps3_sys_manager_send_attr(dev, PS3_SM_ATTR_ALL);
584 result = ps3_vuart_read_async(dev, ps3_sys_manager_work,
591 static struct ps3_vuart_port_driver ps3_sys_manager = {
592 .match_id = PS3_MATCH_ID_SYSTEM_MANAGER,
594 .name = "ps3_sys_manager",
596 .probe = ps3_sys_manager_probe,
599 static int __init ps3_sys_manager_init(void)
601 return ps3_vuart_port_driver_register(&ps3_sys_manager);
604 module_init(ps3_sys_manager_init);