Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  PS3 System Manager.
0004  *
0005  *  Copyright (C) 2007 Sony Computer Entertainment Inc.
0006  *  Copyright 2007 Sony Corp.
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/workqueue.h>
0012 #include <linux/reboot.h>
0013 #include <linux/sched/signal.h>
0014 
0015 #include <asm/firmware.h>
0016 #include <asm/lv1call.h>
0017 #include <asm/ps3.h>
0018 
0019 #include "vuart.h"
0020 
0021 /**
0022  * ps3_sys_manager - PS3 system manager driver.
0023  *
0024  * The system manager provides an asynchronous system event notification
0025  * mechanism for reporting events like thermal alert and button presses to
0026  * guests.  It also provides support to control system shutdown and startup.
0027  *
0028  * The actual system manager is implemented as an application running in the
0029  * system policy module in lpar_1.  Guests communicate with the system manager
0030  * through port 2 of the vuart using a simple packet message protocol.
0031  * Messages are comprised of a fixed field header followed by a message
0032  * specific payload.
0033  */
0034 
0035 /**
0036  * struct ps3_sys_manager_header - System manager message header.
0037  * @version: Header version, currently 1.
0038  * @size: Header size in bytes, currently 16.
0039  * @payload_size: Message payload size in bytes.
0040  * @service_id: Message type, one of enum ps3_sys_manager_service_id.
0041  * @request_tag: Unique number to identify reply.
0042  */
0043 
0044 struct ps3_sys_manager_header {
0045     /* version 1 */
0046     u8 version;
0047     u8 size;
0048     u16 reserved_1;
0049     u32 payload_size;
0050     u16 service_id;
0051     u16 reserved_2;
0052     u32 request_tag;
0053 };
0054 
0055 #define dump_sm_header(_h) _dump_sm_header(_h, __func__, __LINE__)
0056 static void __maybe_unused _dump_sm_header(
0057     const struct ps3_sys_manager_header *h, const char *func, int line)
0058 {
0059     pr_debug("%s:%d: version:      %xh\n", func, line, h->version);
0060     pr_debug("%s:%d: size:         %xh\n", func, line, h->size);
0061     pr_debug("%s:%d: payload_size: %xh\n", func, line, h->payload_size);
0062     pr_debug("%s:%d: service_id:   %xh\n", func, line, h->service_id);
0063     pr_debug("%s:%d: request_tag:  %xh\n", func, line, h->request_tag);
0064 }
0065 
0066 /**
0067  * @PS3_SM_RX_MSG_LEN_MIN - Shortest received message length.
0068  * @PS3_SM_RX_MSG_LEN_MAX - Longest received message length.
0069  *
0070  * Currently all messages received from the system manager are either
0071  * (16 bytes header + 8 bytes payload = 24 bytes) or (16 bytes header
0072  * + 16 bytes payload = 32 bytes).  This knowledge is used to simplify
0073  * the logic.
0074  */
0075 
0076 enum {
0077     PS3_SM_RX_MSG_LEN_MIN = 24,
0078     PS3_SM_RX_MSG_LEN_MAX = 32,
0079 };
0080 
0081 /**
0082  * enum ps3_sys_manager_service_id - Message header service_id.
0083  * @PS3_SM_SERVICE_ID_REQUEST:       guest --> sys_manager.
0084  * @PS3_SM_SERVICE_ID_REQUEST_ERROR: guest <-- sys_manager.
0085  * @PS3_SM_SERVICE_ID_COMMAND:       guest <-- sys_manager.
0086  * @PS3_SM_SERVICE_ID_RESPONSE:      guest --> sys_manager.
0087  * @PS3_SM_SERVICE_ID_SET_ATTR:      guest --> sys_manager.
0088  * @PS3_SM_SERVICE_ID_EXTERN_EVENT:  guest <-- sys_manager.
0089  * @PS3_SM_SERVICE_ID_SET_NEXT_OP:   guest --> sys_manager.
0090  *
0091  * PS3_SM_SERVICE_ID_REQUEST_ERROR is returned for invalid data values in a
0092  * a PS3_SM_SERVICE_ID_REQUEST message.  It also seems to be returned when
0093  * a REQUEST message is sent at the wrong time.
0094  */
0095 
0096 enum ps3_sys_manager_service_id {
0097     /* version 1 */
0098     PS3_SM_SERVICE_ID_REQUEST = 1,
0099     PS3_SM_SERVICE_ID_RESPONSE = 2,
0100     PS3_SM_SERVICE_ID_COMMAND = 3,
0101     PS3_SM_SERVICE_ID_EXTERN_EVENT = 4,
0102     PS3_SM_SERVICE_ID_SET_NEXT_OP = 5,
0103     PS3_SM_SERVICE_ID_REQUEST_ERROR = 6,
0104     PS3_SM_SERVICE_ID_SET_ATTR = 8,
0105 };
0106 
0107 /**
0108  * enum ps3_sys_manager_attr - Notification attribute (bit position mask).
0109  * @PS3_SM_ATTR_POWER: Power button.
0110  * @PS3_SM_ATTR_RESET: Reset button, not available on retail console.
0111  * @PS3_SM_ATTR_THERMAL: System thermal alert.
0112  * @PS3_SM_ATTR_CONTROLLER: Remote controller event.
0113  * @PS3_SM_ATTR_ALL: Logical OR of all.
0114  *
0115  * The guest tells the system manager which events it is interested in receiving
0116  * notice of by sending the system manager a logical OR of notification
0117  * attributes via the ps3_sys_manager_send_attr() routine.
0118  */
0119 
0120 enum ps3_sys_manager_attr {
0121     /* version 1 */
0122     PS3_SM_ATTR_POWER = 1,
0123     PS3_SM_ATTR_RESET = 2,
0124     PS3_SM_ATTR_THERMAL = 4,
0125     PS3_SM_ATTR_CONTROLLER = 8, /* bogus? */
0126     PS3_SM_ATTR_ALL = 0x0f,
0127 };
0128 
0129 /**
0130  * enum ps3_sys_manager_event - External event type, reported by system manager.
0131  * @PS3_SM_EVENT_POWER_PRESSED: payload.value =
0132  *  enum ps3_sys_manager_button_event.
0133  * @PS3_SM_EVENT_POWER_RELEASED: payload.value = time pressed in millisec.
0134  * @PS3_SM_EVENT_RESET_PRESSED: payload.value =
0135  *  enum ps3_sys_manager_button_event.
0136  * @PS3_SM_EVENT_RESET_RELEASED: payload.value = time pressed in millisec.
0137  * @PS3_SM_EVENT_THERMAL_ALERT: payload.value = thermal zone id.
0138  * @PS3_SM_EVENT_THERMAL_CLEARED: payload.value = thermal zone id.
0139  */
0140 
0141 enum ps3_sys_manager_event {
0142     /* version 1 */
0143     PS3_SM_EVENT_POWER_PRESSED = 3,
0144     PS3_SM_EVENT_POWER_RELEASED = 4,
0145     PS3_SM_EVENT_RESET_PRESSED = 5,
0146     PS3_SM_EVENT_RESET_RELEASED = 6,
0147     PS3_SM_EVENT_THERMAL_ALERT = 7,
0148     PS3_SM_EVENT_THERMAL_CLEARED = 8,
0149     /* no info on controller events */
0150 };
0151 
0152 /**
0153  * enum ps3_sys_manager_button_event - Button event payload values.
0154  * @PS3_SM_BUTTON_EVENT_HARD: Hardware generated event.
0155  * @PS3_SM_BUTTON_EVENT_SOFT: Software generated event.
0156  */
0157 
0158 enum ps3_sys_manager_button_event {
0159     PS3_SM_BUTTON_EVENT_HARD = 0,
0160     PS3_SM_BUTTON_EVENT_SOFT = 1,
0161 };
0162 
0163 /**
0164  * enum ps3_sys_manager_next_op - Operation to perform after lpar is destroyed.
0165  */
0166 
0167 enum ps3_sys_manager_next_op {
0168     /* version 3 */
0169     PS3_SM_NEXT_OP_SYS_SHUTDOWN = 1,
0170     PS3_SM_NEXT_OP_SYS_REBOOT = 2,
0171     PS3_SM_NEXT_OP_LPAR_REBOOT = 0x82,
0172 };
0173 
0174 /**
0175  * enum ps3_sys_manager_wake_source - Next-op wakeup source (bit position mask).
0176  * @PS3_SM_WAKE_DEFAULT: Disk insert, power button, eject button.
0177  * @PS3_SM_WAKE_W_O_L: Ether or wireless LAN.
0178  * @PS3_SM_WAKE_P_O_R: Power on reset.
0179  *
0180  * Additional wakeup sources when specifying PS3_SM_NEXT_OP_SYS_SHUTDOWN.
0181  * The system will always wake from the PS3_SM_WAKE_DEFAULT sources.
0182  * Sources listed here are the only ones available to guests in the
0183  * other-os lpar.
0184  */
0185 
0186 enum ps3_sys_manager_wake_source {
0187     /* version 3 */
0188     PS3_SM_WAKE_DEFAULT   = 0,
0189     PS3_SM_WAKE_W_O_L     = 0x00000400,
0190     PS3_SM_WAKE_P_O_R     = 0x80000000,
0191 };
0192 
0193 /**
0194  * user_wake_sources - User specified wakeup sources.
0195  *
0196  * Logical OR of enum ps3_sys_manager_wake_source types.
0197  */
0198 
0199 static u32 user_wake_sources = PS3_SM_WAKE_DEFAULT;
0200 
0201 /**
0202  * enum ps3_sys_manager_cmd - Command from system manager to guest.
0203  *
0204  * The guest completes the actions needed, then acks or naks the command via
0205  * ps3_sys_manager_send_response().  In the case of @PS3_SM_CMD_SHUTDOWN,
0206  * the guest must be fully prepared for a system poweroff prior to acking the
0207  * command.
0208  */
0209 
0210 enum ps3_sys_manager_cmd {
0211     /* version 1 */
0212     PS3_SM_CMD_SHUTDOWN = 1, /* shutdown guest OS */
0213 };
0214 
0215 /**
0216  * ps3_sm_force_power_off - Poweroff helper.
0217  *
0218  * A global variable used to force a poweroff when the power button has
0219  * been pressed irrespective of how init handles the ctrl_alt_del signal.
0220  *
0221  */
0222 
0223 static unsigned int ps3_sm_force_power_off;
0224 
0225 /**
0226  * ps3_sys_manager_write - Helper to write a two part message to the vuart.
0227  *
0228  */
0229 
0230 static int ps3_sys_manager_write(struct ps3_system_bus_device *dev,
0231     const struct ps3_sys_manager_header *header, const void *payload)
0232 {
0233     int result;
0234 
0235     BUG_ON(header->version != 1);
0236     BUG_ON(header->size != 16);
0237     BUG_ON(header->payload_size != 8 && header->payload_size != 16);
0238     BUG_ON(header->service_id > 8);
0239 
0240     result = ps3_vuart_write(dev, header,
0241         sizeof(struct ps3_sys_manager_header));
0242 
0243     if (!result)
0244         result = ps3_vuart_write(dev, payload, header->payload_size);
0245 
0246     return result;
0247 }
0248 
0249 /**
0250  * ps3_sys_manager_send_attr - Send a 'set attribute' to the system manager.
0251  *
0252  */
0253 
0254 static int ps3_sys_manager_send_attr(struct ps3_system_bus_device *dev,
0255     enum ps3_sys_manager_attr attr)
0256 {
0257     struct ps3_sys_manager_header header;
0258     struct {
0259         u8 version;
0260         u8 reserved_1[3];
0261         u32 attribute;
0262     } payload;
0263 
0264     BUILD_BUG_ON(sizeof(payload) != 8);
0265 
0266     dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, attr);
0267 
0268     memset(&header, 0, sizeof(header));
0269     header.version = 1;
0270     header.size = 16;
0271     header.payload_size = 16;
0272     header.service_id = PS3_SM_SERVICE_ID_SET_ATTR;
0273 
0274     memset(&payload, 0, sizeof(payload));
0275     payload.version = 1;
0276     payload.attribute = attr;
0277 
0278     return ps3_sys_manager_write(dev, &header, &payload);
0279 }
0280 
0281 /**
0282  * ps3_sys_manager_send_next_op - Send a 'set next op' to the system manager.
0283  *
0284  * Tell the system manager what to do after this lpar is destroyed.
0285  */
0286 
0287 static int ps3_sys_manager_send_next_op(struct ps3_system_bus_device *dev,
0288     enum ps3_sys_manager_next_op op,
0289     enum ps3_sys_manager_wake_source wake_source)
0290 {
0291     struct ps3_sys_manager_header header;
0292     struct {
0293         u8 version;
0294         u8 type;
0295         u8 gos_id;
0296         u8 reserved_1;
0297         u32 wake_source;
0298         u8 reserved_2[8];
0299     } payload;
0300 
0301     BUILD_BUG_ON(sizeof(payload) != 16);
0302 
0303     dev_dbg(&dev->core, "%s:%d: (%xh)\n", __func__, __LINE__, op);
0304 
0305     memset(&header, 0, sizeof(header));
0306     header.version = 1;
0307     header.size = 16;
0308     header.payload_size = 16;
0309     header.service_id = PS3_SM_SERVICE_ID_SET_NEXT_OP;
0310 
0311     memset(&payload, 0, sizeof(payload));
0312     payload.version = 3;
0313     payload.type = op;
0314     payload.gos_id = 3; /* other os */
0315     payload.wake_source = wake_source;
0316 
0317     return ps3_sys_manager_write(dev, &header, &payload);
0318 }
0319 
0320 /**
0321  * ps3_sys_manager_send_request_shutdown - Send 'request' to the system manager.
0322  *
0323  * The guest sends this message to request an operation or action of the system
0324  * manager.  The reply is a command message from the system manager.  In the
0325  * command handler the guest performs the requested operation.  The result of
0326  * the command is then communicated back to the system manager with a response
0327  * message.
0328  *
0329  * Currently, the only supported request is the 'shutdown self' request.
0330  */
0331 
0332 static int ps3_sys_manager_send_request_shutdown(
0333     struct ps3_system_bus_device *dev)
0334 {
0335     struct ps3_sys_manager_header header;
0336     struct {
0337         u8 version;
0338         u8 type;
0339         u8 gos_id;
0340         u8 reserved_1[13];
0341     } payload;
0342 
0343     BUILD_BUG_ON(sizeof(payload) != 16);
0344 
0345     dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
0346 
0347     memset(&header, 0, sizeof(header));
0348     header.version = 1;
0349     header.size = 16;
0350     header.payload_size = 16;
0351     header.service_id = PS3_SM_SERVICE_ID_REQUEST;
0352 
0353     memset(&payload, 0, sizeof(payload));
0354     payload.version = 1;
0355     payload.type = 1; /* shutdown */
0356     payload.gos_id = 0; /* self */
0357 
0358     return ps3_sys_manager_write(dev, &header, &payload);
0359 }
0360 
0361 /**
0362  * ps3_sys_manager_send_response - Send a 'response' to the system manager.
0363  * @status: zero = success, others fail.
0364  *
0365  * The guest sends this message to the system manager to acnowledge success or
0366  * failure of a command sent by the system manager.
0367  */
0368 
0369 static int ps3_sys_manager_send_response(struct ps3_system_bus_device *dev,
0370     u64 status)
0371 {
0372     struct ps3_sys_manager_header header;
0373     struct {
0374         u8 version;
0375         u8 reserved_1[3];
0376         u8 status;
0377         u8 reserved_2[11];
0378     } payload;
0379 
0380     BUILD_BUG_ON(sizeof(payload) != 16);
0381 
0382     dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
0383         (status ? "nak" : "ack"));
0384 
0385     memset(&header, 0, sizeof(header));
0386     header.version = 1;
0387     header.size = 16;
0388     header.payload_size = 16;
0389     header.service_id = PS3_SM_SERVICE_ID_RESPONSE;
0390 
0391     memset(&payload, 0, sizeof(payload));
0392     payload.version = 1;
0393     payload.status = status;
0394 
0395     return ps3_sys_manager_write(dev, &header, &payload);
0396 }
0397 
0398 /**
0399  * ps3_sys_manager_handle_event - Second stage event msg handler.
0400  *
0401  */
0402 
0403 static int ps3_sys_manager_handle_event(struct ps3_system_bus_device *dev)
0404 {
0405     int result;
0406     struct {
0407         u8 version;
0408         u8 type;
0409         u8 reserved_1[2];
0410         u32 value;
0411         u8 reserved_2[8];
0412     } event;
0413 
0414     BUILD_BUG_ON(sizeof(event) != 16);
0415 
0416     result = ps3_vuart_read(dev, &event, sizeof(event));
0417     BUG_ON(result && "need to retry here");
0418 
0419     if (event.version != 1) {
0420         dev_dbg(&dev->core, "%s:%d: unsupported event version (%u)\n",
0421             __func__, __LINE__, event.version);
0422         return -EIO;
0423     }
0424 
0425     switch (event.type) {
0426     case PS3_SM_EVENT_POWER_PRESSED:
0427         dev_dbg(&dev->core, "%s:%d: POWER_PRESSED (%s)\n",
0428             __func__, __LINE__,
0429             (event.value == PS3_SM_BUTTON_EVENT_SOFT ? "soft"
0430             : "hard"));
0431         ps3_sm_force_power_off = 1;
0432         /*
0433          * A memory barrier is use here to sync memory since
0434          * ps3_sys_manager_final_restart() could be called on
0435          * another cpu.
0436          */
0437         wmb();
0438         kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */
0439         break;
0440     case PS3_SM_EVENT_POWER_RELEASED:
0441         dev_dbg(&dev->core, "%s:%d: POWER_RELEASED (%u ms)\n",
0442             __func__, __LINE__, event.value);
0443         break;
0444     case PS3_SM_EVENT_RESET_PRESSED:
0445         dev_dbg(&dev->core, "%s:%d: RESET_PRESSED (%s)\n",
0446             __func__, __LINE__,
0447             (event.value == PS3_SM_BUTTON_EVENT_SOFT ? "soft"
0448             : "hard"));
0449         ps3_sm_force_power_off = 0;
0450         /*
0451          * A memory barrier is use here to sync memory since
0452          * ps3_sys_manager_final_restart() could be called on
0453          * another cpu.
0454          */
0455         wmb();
0456         kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */
0457         break;
0458     case PS3_SM_EVENT_RESET_RELEASED:
0459         dev_dbg(&dev->core, "%s:%d: RESET_RELEASED (%u ms)\n",
0460             __func__, __LINE__, event.value);
0461         break;
0462     case PS3_SM_EVENT_THERMAL_ALERT:
0463         dev_dbg(&dev->core, "%s:%d: THERMAL_ALERT (zone %u)\n",
0464             __func__, __LINE__, event.value);
0465         pr_info("PS3 Thermal Alert Zone %u\n", event.value);
0466         break;
0467     case PS3_SM_EVENT_THERMAL_CLEARED:
0468         dev_dbg(&dev->core, "%s:%d: THERMAL_CLEARED (zone %u)\n",
0469             __func__, __LINE__, event.value);
0470         break;
0471     default:
0472         dev_dbg(&dev->core, "%s:%d: unknown event (%u)\n",
0473             __func__, __LINE__, event.type);
0474         return -EIO;
0475     }
0476 
0477     return 0;
0478 }
0479 /**
0480  * ps3_sys_manager_handle_cmd - Second stage command msg handler.
0481  *
0482  * The system manager sends this in reply to a 'request' message from the guest.
0483  */
0484 
0485 static int ps3_sys_manager_handle_cmd(struct ps3_system_bus_device *dev)
0486 {
0487     int result;
0488     struct {
0489         u8 version;
0490         u8 type;
0491         u8 reserved_1[14];
0492     } cmd;
0493 
0494     BUILD_BUG_ON(sizeof(cmd) != 16);
0495 
0496     dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
0497 
0498     result = ps3_vuart_read(dev, &cmd, sizeof(cmd));
0499     BUG_ON(result && "need to retry here");
0500 
0501     if (result)
0502         return result;
0503 
0504     if (cmd.version != 1) {
0505         dev_dbg(&dev->core, "%s:%d: unsupported cmd version (%u)\n",
0506             __func__, __LINE__, cmd.version);
0507         return -EIO;
0508     }
0509 
0510     if (cmd.type != PS3_SM_CMD_SHUTDOWN) {
0511         dev_dbg(&dev->core, "%s:%d: unknown cmd (%u)\n",
0512             __func__, __LINE__, cmd.type);
0513         return -EIO;
0514     }
0515 
0516     ps3_sys_manager_send_response(dev, 0);
0517     return 0;
0518 }
0519 
0520 /**
0521  * ps3_sys_manager_handle_msg - First stage msg handler.
0522  *
0523  * Can be called directly to manually poll vuart and pump message handler.
0524  */
0525 
0526 static int ps3_sys_manager_handle_msg(struct ps3_system_bus_device *dev)
0527 {
0528     int result;
0529     struct ps3_sys_manager_header header;
0530 
0531     result = ps3_vuart_read(dev, &header,
0532         sizeof(struct ps3_sys_manager_header));
0533 
0534     if (result)
0535         return result;
0536 
0537     if (header.version != 1) {
0538         dev_dbg(&dev->core, "%s:%d: unsupported header version (%u)\n",
0539             __func__, __LINE__, header.version);
0540         dump_sm_header(&header);
0541         goto fail_header;
0542     }
0543 
0544     BUILD_BUG_ON(sizeof(header) != 16);
0545 
0546     if (header.size != 16 || (header.payload_size != 8
0547         && header.payload_size != 16)) {
0548         dump_sm_header(&header);
0549         BUG();
0550     }
0551 
0552     switch (header.service_id) {
0553     case PS3_SM_SERVICE_ID_EXTERN_EVENT:
0554         dev_dbg(&dev->core, "%s:%d: EVENT\n", __func__, __LINE__);
0555         return ps3_sys_manager_handle_event(dev);
0556     case PS3_SM_SERVICE_ID_COMMAND:
0557         dev_dbg(&dev->core, "%s:%d: COMMAND\n", __func__, __LINE__);
0558         return ps3_sys_manager_handle_cmd(dev);
0559     case PS3_SM_SERVICE_ID_REQUEST_ERROR:
0560         dev_dbg(&dev->core, "%s:%d: REQUEST_ERROR\n", __func__,
0561             __LINE__);
0562         dump_sm_header(&header);
0563         break;
0564     default:
0565         dev_dbg(&dev->core, "%s:%d: unknown service_id (%u)\n",
0566             __func__, __LINE__, header.service_id);
0567         break;
0568     }
0569     goto fail_id;
0570 
0571 fail_header:
0572     ps3_vuart_clear_rx_bytes(dev, 0);
0573     return -EIO;
0574 fail_id:
0575     ps3_vuart_clear_rx_bytes(dev, header.payload_size);
0576     return -EIO;
0577 }
0578 
0579 static void ps3_sys_manager_fin(struct ps3_system_bus_device *dev)
0580 {
0581     ps3_sys_manager_send_request_shutdown(dev);
0582 
0583     pr_emerg("System Halted, OK to turn off power\n");
0584 
0585     while (ps3_sys_manager_handle_msg(dev)) {
0586         /* pause until next DEC interrupt */
0587         lv1_pause(0);
0588     }
0589 
0590     while (1) {
0591         /* pause, ignoring DEC interrupt */
0592         lv1_pause(1);
0593     }
0594 }
0595 
0596 /**
0597  * ps3_sys_manager_final_power_off - The final platform machine_power_off routine.
0598  *
0599  * This routine never returns.  The routine disables asynchronous vuart reads
0600  * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
0601  * the shutdown command sent from the system manager.  Soon after the
0602  * acknowledgement is sent the lpar is destroyed by the HV.  This routine
0603  * should only be called from ps3_power_off() through
0604  * ps3_sys_manager_ops.power_off.
0605  */
0606 
0607 static void ps3_sys_manager_final_power_off(struct ps3_system_bus_device *dev)
0608 {
0609     BUG_ON(!dev);
0610 
0611     dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
0612 
0613     ps3_vuart_cancel_async(dev);
0614 
0615     ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_SHUTDOWN,
0616         user_wake_sources);
0617 
0618     ps3_sys_manager_fin(dev);
0619 }
0620 
0621 /**
0622  * ps3_sys_manager_final_restart - The final platform machine_restart routine.
0623  *
0624  * This routine never returns.  The routine disables asynchronous vuart reads
0625  * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
0626  * the shutdown command sent from the system manager.  Soon after the
0627  * acknowledgement is sent the lpar is destroyed by the HV.  This routine
0628  * should only be called from ps3_restart() through ps3_sys_manager_ops.restart.
0629  */
0630 
0631 static void ps3_sys_manager_final_restart(struct ps3_system_bus_device *dev)
0632 {
0633     BUG_ON(!dev);
0634 
0635     dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
0636 
0637     /* Check if we got here via a power button event. */
0638 
0639     if (ps3_sm_force_power_off) {
0640         dev_dbg(&dev->core, "%s:%d: forcing poweroff\n",
0641             __func__, __LINE__);
0642         ps3_sys_manager_final_power_off(dev);
0643     }
0644 
0645     ps3_vuart_cancel_async(dev);
0646 
0647     ps3_sys_manager_send_attr(dev, 0);
0648     ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_REBOOT,
0649         user_wake_sources);
0650 
0651     ps3_sys_manager_fin(dev);
0652 }
0653 
0654 /**
0655  * ps3_sys_manager_get_wol - Get wake-on-lan setting.
0656  */
0657 
0658 int ps3_sys_manager_get_wol(void)
0659 {
0660     pr_debug("%s:%d\n", __func__, __LINE__);
0661 
0662     return (user_wake_sources & PS3_SM_WAKE_W_O_L) != 0;
0663 }
0664 EXPORT_SYMBOL_GPL(ps3_sys_manager_get_wol);
0665 
0666 /**
0667  * ps3_sys_manager_set_wol - Set wake-on-lan setting.
0668  */
0669 
0670 void ps3_sys_manager_set_wol(int state)
0671 {
0672     static DEFINE_MUTEX(mutex);
0673 
0674     mutex_lock(&mutex);
0675 
0676     pr_debug("%s:%d: %d\n", __func__, __LINE__, state);
0677 
0678     if (state)
0679         user_wake_sources |= PS3_SM_WAKE_W_O_L;
0680     else
0681         user_wake_sources &= ~PS3_SM_WAKE_W_O_L;
0682     mutex_unlock(&mutex);
0683 }
0684 EXPORT_SYMBOL_GPL(ps3_sys_manager_set_wol);
0685 
0686 /**
0687  * ps3_sys_manager_work - Asynchronous read handler.
0688  *
0689  * Signaled when PS3_SM_RX_MSG_LEN_MIN bytes arrive at the vuart port.
0690  */
0691 
0692 static void ps3_sys_manager_work(struct ps3_system_bus_device *dev)
0693 {
0694     ps3_sys_manager_handle_msg(dev);
0695     ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN);
0696 }
0697 
0698 static int ps3_sys_manager_probe(struct ps3_system_bus_device *dev)
0699 {
0700     int result;
0701     struct ps3_sys_manager_ops ops;
0702 
0703     dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
0704 
0705     ops.power_off = ps3_sys_manager_final_power_off;
0706     ops.restart = ps3_sys_manager_final_restart;
0707     ops.dev = dev;
0708 
0709     /* ps3_sys_manager_register_ops copies ops. */
0710 
0711     ps3_sys_manager_register_ops(&ops);
0712 
0713     result = ps3_sys_manager_send_attr(dev, PS3_SM_ATTR_ALL);
0714     BUG_ON(result);
0715 
0716     result = ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN);
0717     BUG_ON(result);
0718 
0719     return result;
0720 }
0721 
0722 static int ps3_sys_manager_remove(struct ps3_system_bus_device *dev)
0723 {
0724     dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
0725     return 0;
0726 }
0727 
0728 static void ps3_sys_manager_shutdown(struct ps3_system_bus_device *dev)
0729 {
0730     dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
0731 }
0732 
0733 static struct ps3_vuart_port_driver ps3_sys_manager = {
0734     .core.match_id = PS3_MATCH_ID_SYSTEM_MANAGER,
0735     .core.core.name = "ps3_sys_manager",
0736     .probe = ps3_sys_manager_probe,
0737     .remove = ps3_sys_manager_remove,
0738     .shutdown = ps3_sys_manager_shutdown,
0739     .work = ps3_sys_manager_work,
0740 };
0741 
0742 static int __init ps3_sys_manager_init(void)
0743 {
0744     if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
0745         return -ENODEV;
0746 
0747     return ps3_vuart_port_driver_register(&ps3_sys_manager);
0748 }
0749 
0750 module_init(ps3_sys_manager_init);
0751 /* Module remove not supported. */
0752 
0753 MODULE_AUTHOR("Sony Corporation");
0754 MODULE_LICENSE("GPL v2");
0755 MODULE_DESCRIPTION("PS3 System Manager");
0756 MODULE_ALIAS(PS3_MODULE_ALIAS_SYSTEM_MANAGER);