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 asyncronous 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.
57 struct ps3_sys_manager_header
{
68 * @PS3_SM_RX_MSG_LEN - System manager received message length.
70 * Currently all messages received from the system manager are the same length
71 * (16 bytes header + 16 bytes payload = 32 bytes). This knowlege is used to
76 PS3_SM_RX_MSG_LEN
= 32,
80 * enum ps3_sys_manager_service_id - Message header service_id.
81 * @PS3_SM_SERVICE_ID_REQUEST: guest --> sys_manager.
82 * @PS3_SM_SERVICE_ID_COMMAND: guest <-- sys_manager.
83 * @PS3_SM_SERVICE_ID_RESPONSE: guest --> sys_manager.
84 * @PS3_SM_SERVICE_ID_SET_ATTR: guest --> sys_manager.
85 * @PS3_SM_SERVICE_ID_EXTERN_EVENT: guest <-- sys_manager.
86 * @PS3_SM_SERVICE_ID_SET_NEXT_OP: guest --> sys_manager.
89 enum ps3_sys_manager_service_id
{
91 PS3_SM_SERVICE_ID_REQUEST
= 1,
92 PS3_SM_SERVICE_ID_RESPONSE
= 2,
93 PS3_SM_SERVICE_ID_COMMAND
= 3,
94 PS3_SM_SERVICE_ID_EXTERN_EVENT
= 4,
95 PS3_SM_SERVICE_ID_SET_NEXT_OP
= 5,
96 PS3_SM_SERVICE_ID_SET_ATTR
= 8,
100 * enum ps3_sys_manager_attr - Notification attribute (bit position mask).
101 * @PS3_SM_ATTR_POWER: Power button.
102 * @PS3_SM_ATTR_RESET: Reset button, not available on retail console.
103 * @PS3_SM_ATTR_THERMAL: Sytem thermal alert.
104 * @PS3_SM_ATTR_CONTROLLER: Remote controller event.
105 * @PS3_SM_ATTR_ALL: Logical OR of all.
107 * The guest tells the system manager which events it is interested in receiving
108 * notice of by sending the system manager a logical OR of notification
109 * attributes via the ps3_sys_manager_send_attr() routine.
112 enum ps3_sys_manager_attr
{
114 PS3_SM_ATTR_POWER
= 1,
115 PS3_SM_ATTR_RESET
= 2,
116 PS3_SM_ATTR_THERMAL
= 4,
117 PS3_SM_ATTR_CONTROLLER
= 8, /* bogus? */
118 PS3_SM_ATTR_ALL
= 0x0f,
122 * enum ps3_sys_manager_event - External event type, reported by system manager.
123 * @PS3_SM_EVENT_POWER_PRESSED: payload.value not used.
124 * @PS3_SM_EVENT_POWER_RELEASED: payload.value = time pressed in millisec.
125 * @PS3_SM_EVENT_RESET_PRESSED: payload.value not used.
126 * @PS3_SM_EVENT_RESET_RELEASED: payload.value = time pressed in millisec.
127 * @PS3_SM_EVENT_THERMAL_ALERT: payload.value = thermal zone id.
128 * @PS3_SM_EVENT_THERMAL_CLEARED: payload.value = thermal zone id.
131 enum ps3_sys_manager_event
{
133 PS3_SM_EVENT_POWER_PRESSED
= 3,
134 PS3_SM_EVENT_POWER_RELEASED
= 4,
135 PS3_SM_EVENT_RESET_PRESSED
= 5,
136 PS3_SM_EVENT_RESET_RELEASED
= 6,
137 PS3_SM_EVENT_THERMAL_ALERT
= 7,
138 PS3_SM_EVENT_THERMAL_CLEARED
= 8,
139 /* no info on controller events */
143 * enum ps3_sys_manager_next_op - Operation to perform after lpar is destroyed.
146 enum ps3_sys_manager_next_op
{
148 PS3_SM_NEXT_OP_SYS_SHUTDOWN
= 1,
149 PS3_SM_NEXT_OP_SYS_REBOOT
= 2,
150 PS3_SM_NEXT_OP_LPAR_REBOOT
= 0x82,
154 * enum ps3_sys_manager_wake_source - Next-op wakeup source (bit position mask).
155 * @PS3_SM_WAKE_DEFAULT: Disk insert, power button, eject button, IR
156 * controller, and bluetooth controller.
158 * @PS3_SM_WAKE_RTC_ERROR:
159 * @PS3_SM_WAKE_P_O_R: Power on reset.
161 * Additional wakeup sources when specifying PS3_SM_NEXT_OP_SYS_SHUTDOWN.
162 * System will always wake from the PS3_SM_WAKE_DEFAULT sources.
165 enum ps3_sys_manager_wake_source
{
167 PS3_SM_WAKE_DEFAULT
= 0,
168 PS3_SM_WAKE_RTC
= 0x00000040,
169 PS3_SM_WAKE_RTC_ERROR
= 0x00000080,
170 PS3_SM_WAKE_P_O_R
= 0x10000000,
174 * enum ps3_sys_manager_cmd - Command from system manager to guest.
176 * The guest completes the actions needed, then acks or naks the command via
177 * ps3_sys_manager_send_response(). In the case of @PS3_SM_CMD_SHUTDOWN,
178 * the guest must be fully prepared for a system poweroff prior to acking the
182 enum ps3_sys_manager_cmd
{
184 PS3_SM_CMD_SHUTDOWN
= 1, /* shutdown guest OS */
188 * ps3_sys_manager_write - Helper to write a two part message to the vuart.
192 static int ps3_sys_manager_write(struct ps3_vuart_port_device
*dev
,
193 const struct ps3_sys_manager_header
*header
, const void *payload
)
197 BUG_ON(header
->version
!= 1);
198 BUG_ON(header
->size
!= 16);
199 BUG_ON(header
->payload_size
!= 8 && header
->payload_size
!= 16);
200 BUG_ON(header
->service_id
> 8);
202 result
= ps3_vuart_write(dev
, header
,
203 sizeof(struct ps3_sys_manager_header
));
206 result
= ps3_vuart_write(dev
, payload
, header
->payload_size
);
212 * ps3_sys_manager_send_attr - Send a 'set attribute' to the system manager.
216 static int ps3_sys_manager_send_attr(struct ps3_vuart_port_device
*dev
,
217 enum ps3_sys_manager_attr attr
)
219 static const struct ps3_sys_manager_header header
= {
223 .service_id
= PS3_SM_SERVICE_ID_SET_ATTR
,
231 BUILD_BUG_ON(sizeof(payload
) != 8);
233 dev_dbg(&dev
->core
, "%s:%d: %xh\n", __func__
, __LINE__
, attr
);
235 memset(&payload
, 0, sizeof(payload
));
237 payload
.attribute
= attr
;
239 return ps3_sys_manager_write(dev
, &header
, &payload
);
243 * ps3_sys_manager_send_next_op - Send a 'set next op' to the system manager.
245 * Tell the system manager what to do after this lpar is destroyed.
248 static int ps3_sys_manager_send_next_op(struct ps3_vuart_port_device
*dev
,
249 enum ps3_sys_manager_next_op op
,
250 enum ps3_sys_manager_wake_source wake_source
)
252 static const struct ps3_sys_manager_header header
= {
256 .service_id
= PS3_SM_SERVICE_ID_SET_NEXT_OP
,
267 BUILD_BUG_ON(sizeof(payload
) != 16);
269 dev_dbg(&dev
->core
, "%s:%d: (%xh)\n", __func__
, __LINE__
, op
);
271 memset(&payload
, 0, sizeof(payload
));
274 payload
.gos_id
= 3; /* other os */
275 payload
.wake_source
= wake_source
;
277 return ps3_sys_manager_write(dev
, &header
, &payload
);
281 * ps3_sys_manager_send_request_shutdown - Send 'request' to the system manager.
283 * The guest sends this message to request an operation or action of the system
284 * manager. The reply is a command message from the system manager. In the
285 * command handler the guest performs the requested operation. The result of
286 * the command is then communicated back to the system manager with a response
289 * Currently, the only supported request it the 'shutdown self' request.
292 static int ps3_sys_manager_send_request_shutdown(struct ps3_vuart_port_device
*dev
)
294 static const struct ps3_sys_manager_header header
= {
298 .service_id
= PS3_SM_SERVICE_ID_REQUEST
,
305 } static const payload
= {
307 .type
= 1, /* shutdown */
308 .gos_id
= 0, /* self */
311 BUILD_BUG_ON(sizeof(payload
) != 16);
313 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
315 return ps3_sys_manager_write(dev
, &header
, &payload
);
319 * ps3_sys_manager_send_response - Send a 'response' to the system manager.
320 * @status: zero = success, others fail.
322 * The guest sends this message to the system manager to acnowledge success or
323 * failure of a command sent by the system manager.
326 static int ps3_sys_manager_send_response(struct ps3_vuart_port_device
*dev
,
329 static const struct ps3_sys_manager_header header
= {
333 .service_id
= PS3_SM_SERVICE_ID_RESPONSE
,
342 BUILD_BUG_ON(sizeof(payload
) != 16);
344 dev_dbg(&dev
->core
, "%s:%d: (%s)\n", __func__
, __LINE__
,
345 (status
? "nak" : "ack"));
347 memset(&payload
, 0, sizeof(payload
));
349 payload
.status
= status
;
351 return ps3_sys_manager_write(dev
, &header
, &payload
);
355 * ps3_sys_manager_handle_event - Second stage event msg handler.
359 static int ps3_sys_manager_handle_event(struct ps3_vuart_port_device
*dev
)
370 BUILD_BUG_ON(sizeof(event
) != 16);
372 result
= ps3_vuart_read(dev
, &event
, sizeof(event
));
375 if (event
.version
!= 1) {
376 dev_dbg(&dev
->core
, "%s:%d: unsupported event version (%u)\n",
377 __func__
, __LINE__
, event
.version
);
381 switch (event
.type
) {
382 case PS3_SM_EVENT_POWER_PRESSED
:
383 dev_dbg(&dev
->core
, "%s:%d: POWER_PRESSED\n",
386 case PS3_SM_EVENT_POWER_RELEASED
:
387 dev_dbg(&dev
->core
, "%s:%d: POWER_RELEASED (%u ms)\n",
388 __func__
, __LINE__
, event
.value
);
389 kill_cad_pid(SIGINT
, 1);
391 case PS3_SM_EVENT_THERMAL_ALERT
:
392 dev_dbg(&dev
->core
, "%s:%d: THERMAL_ALERT (zone %u)\n",
393 __func__
, __LINE__
, event
.value
);
394 printk(KERN_INFO
"PS3 Thermal Alert Zone %u\n", event
.value
);
396 case PS3_SM_EVENT_THERMAL_CLEARED
:
397 dev_dbg(&dev
->core
, "%s:%d: THERMAL_CLEARED (zone %u)\n",
398 __func__
, __LINE__
, event
.value
);
401 dev_dbg(&dev
->core
, "%s:%d: unknown event (%u)\n",
402 __func__
, __LINE__
, event
.type
);
409 * ps3_sys_manager_handle_cmd - Second stage command msg handler.
411 * The system manager sends this in reply to a 'request' message from the guest.
414 static int ps3_sys_manager_handle_cmd(struct ps3_vuart_port_device
*dev
)
423 BUILD_BUG_ON(sizeof(cmd
) != 16);
425 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
427 result
= ps3_vuart_read(dev
, &cmd
, sizeof(cmd
));
432 if (cmd
.version
!= 1) {
433 dev_dbg(&dev
->core
, "%s:%d: unsupported cmd version (%u)\n",
434 __func__
, __LINE__
, cmd
.version
);
438 if (cmd
.type
!= PS3_SM_CMD_SHUTDOWN
) {
439 dev_dbg(&dev
->core
, "%s:%d: unknown cmd (%u)\n",
440 __func__
, __LINE__
, cmd
.type
);
444 ps3_sys_manager_send_response(dev
, 0);
449 * ps3_sys_manager_handle_msg - First stage msg handler.
453 static int ps3_sys_manager_handle_msg(struct ps3_vuart_port_device
*dev
)
456 struct ps3_sys_manager_header header
;
458 result
= ps3_vuart_read(dev
, &header
,
459 sizeof(struct ps3_sys_manager_header
));
464 if (header
.version
!= 1) {
465 dev_dbg(&dev
->core
, "%s:%d: unsupported header version (%u)\n",
466 __func__
, __LINE__
, header
.version
);
470 BUILD_BUG_ON(sizeof(header
) != 16);
471 BUG_ON(header
.size
!= 16);
472 BUG_ON(header
.payload_size
!= 16);
474 switch (header
.service_id
) {
475 case PS3_SM_SERVICE_ID_EXTERN_EVENT
:
476 dev_dbg(&dev
->core
, "%s:%d: EVENT\n", __func__
, __LINE__
);
477 return ps3_sys_manager_handle_event(dev
);
478 case PS3_SM_SERVICE_ID_COMMAND
:
479 dev_dbg(&dev
->core
, "%s:%d: COMMAND\n", __func__
, __LINE__
);
480 return ps3_sys_manager_handle_cmd(dev
);
482 dev_dbg(&dev
->core
, "%s:%d: unknown service_id (%u)\n",
483 __func__
, __LINE__
, header
.service_id
);
489 ps3_vuart_clear_rx_bytes(dev
, 0);
492 ps3_vuart_clear_rx_bytes(dev
, header
.payload_size
);
497 * ps3_sys_manager_work - Asyncronous read handler.
499 * Signaled when a complete message arrives at the vuart port.
502 static void ps3_sys_manager_work(struct work_struct
*work
)
504 struct ps3_vuart_port_device
*dev
= ps3_vuart_work_to_port_device(work
);
506 ps3_sys_manager_handle_msg(dev
);
507 ps3_vuart_read_async(dev
, ps3_sys_manager_work
, PS3_SM_RX_MSG_LEN
);
511 struct ps3_vuart_port_device
*dev
;
515 * ps3_sys_manager_restart - The final platform machine_restart routine.
517 * This routine never returns. The routine disables asyncronous vuart reads
518 * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
519 * the shutdown command sent from the system manager. Soon after the
520 * acknowledgement is sent the lpar is destroyed by the HV. This routine
521 * should only be called from ps3_restart().
524 void ps3_sys_manager_restart(void)
526 struct ps3_vuart_port_device
*dev
= drv_priv
.dev
;
528 BUG_ON(!drv_priv
.dev
);
530 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
532 ps3_vuart_cancel_async(dev
);
534 ps3_sys_manager_send_attr(dev
, 0);
535 ps3_sys_manager_send_next_op(dev
, PS3_SM_NEXT_OP_LPAR_REBOOT
,
536 PS3_SM_WAKE_DEFAULT
);
537 ps3_sys_manager_send_request_shutdown(dev
);
539 printk(KERN_EMERG
"System Halted, OK to turn off power\n");
542 ps3_sys_manager_handle_msg(dev
);
546 * ps3_sys_manager_power_off - The final platform machine_power_off routine.
548 * This routine never returns. The routine disables asyncronous vuart reads
549 * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
550 * the shutdown command sent from the system manager. Soon after the
551 * acknowledgement is sent the lpar is destroyed by the HV. This routine
552 * should only be called from ps3_power_off().
555 void ps3_sys_manager_power_off(void)
557 struct ps3_vuart_port_device
*dev
= drv_priv
.dev
;
559 BUG_ON(!drv_priv
.dev
);
561 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
563 ps3_vuart_cancel_async(dev
);
565 ps3_sys_manager_send_next_op(dev
, PS3_SM_NEXT_OP_SYS_SHUTDOWN
,
566 PS3_SM_WAKE_DEFAULT
);
567 ps3_sys_manager_send_request_shutdown(dev
);
569 printk(KERN_EMERG
"System Halted, OK to turn off power\n");
572 ps3_sys_manager_handle_msg(dev
);
575 static int ps3_sys_manager_probe(struct ps3_vuart_port_device
*dev
)
579 dev_dbg(&dev
->core
, "%s:%d\n", __func__
, __LINE__
);
581 BUG_ON(drv_priv
.dev
);
584 result
= ps3_sys_manager_send_attr(dev
, PS3_SM_ATTR_ALL
);
587 result
= ps3_vuart_read_async(dev
, ps3_sys_manager_work
,
594 static struct ps3_vuart_port_driver ps3_sys_manager
= {
595 .match_id
= PS3_MATCH_ID_SYSTEM_MANAGER
,
597 .name
= "ps3_sys_manager",
599 .probe
= ps3_sys_manager_probe
,
602 static int __init
ps3_sys_manager_init(void)
604 if (!firmware_has_feature(FW_FEATURE_PS3_LV1
))
607 return ps3_vuart_port_driver_register(&ps3_sys_manager
);
610 module_init(ps3_sys_manager_init
);