Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * PS3 System Manager. |
| 3 | * |
| 4 | * Copyright (C) 2007 Sony Computer Entertainment Inc. |
| 5 | * Copyright 2007 Sony Corp. |
| 6 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/workqueue.h> |
| 24 | #include <linux/reboot.h> |
Geert Uytterhoeven | ef596c6 | 2007-03-10 00:05:38 +0100 | [diff] [blame] | 25 | |
| 26 | #include <asm/firmware.h> |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 27 | #include <asm/ps3.h> |
Geert Uytterhoeven | ef596c6 | 2007-03-10 00:05:38 +0100 | [diff] [blame] | 28 | |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 29 | #include "vuart.h" |
| 30 | |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 31 | /** |
| 32 | * ps3_sys_manager - PS3 system manager driver. |
| 33 | * |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 34 | * The system manager provides an asynchronous system event notification |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 35 | * mechanism for reporting events like thermal alert and button presses to |
| 36 | * guests. It also provides support to control system shutdown and startup. |
| 37 | * |
| 38 | * The actual system manager is implemented as an application running in the |
| 39 | * system policy module in lpar_1. Guests communicate with the system manager |
| 40 | * through port 2 of the vuart using a simple packet message protocol. |
| 41 | * Messages are comprised of a fixed field header followed by a message |
| 42 | * specific payload. |
| 43 | */ |
| 44 | |
| 45 | /** |
| 46 | * struct ps3_sys_manager_header - System manager message header. |
| 47 | * @version: Header version, currently 1. |
| 48 | * @size: Header size in bytes, curently 16. |
| 49 | * @payload_size: Message payload size in bytes. |
| 50 | * @service_id: Message type, one of enum ps3_sys_manager_service_id. |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 51 | * @request_tag: Unique number to identify reply. |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 52 | */ |
| 53 | |
| 54 | struct ps3_sys_manager_header { |
| 55 | /* version 1 */ |
| 56 | u8 version; |
| 57 | u8 size; |
| 58 | u16 reserved_1; |
| 59 | u32 payload_size; |
| 60 | u16 service_id; |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 61 | u16 reserved_2; |
| 62 | u32 request_tag; |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 65 | #define dump_sm_header(_h) _dump_sm_header(_h, __func__, __LINE__) |
| 66 | static void __maybe_unused _dump_sm_header( |
| 67 | const struct ps3_sys_manager_header *h, const char *func, int line) |
| 68 | { |
| 69 | pr_debug("%s:%d: version: %xh\n", func, line, h->version); |
| 70 | pr_debug("%s:%d: size: %xh\n", func, line, h->size); |
| 71 | pr_debug("%s:%d: payload_size: %xh\n", func, line, h->payload_size); |
| 72 | pr_debug("%s:%d: service_id: %xh\n", func, line, h->service_id); |
| 73 | pr_debug("%s:%d: request_tag: %xh\n", func, line, h->request_tag); |
| 74 | } |
| 75 | |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 76 | /** |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 77 | * @PS3_SM_RX_MSG_LEN_MIN - Shortest received message length. |
| 78 | * @PS3_SM_RX_MSG_LEN_MAX - Longest received message length. |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 79 | * |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 80 | * Currently all messages received from the system manager are either |
| 81 | * (16 bytes header + 8 bytes payload = 24 bytes) or (16 bytes header |
| 82 | * + 16 bytes payload = 32 bytes). This knowlege is used to simplify |
| 83 | * the logic. |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 84 | */ |
| 85 | |
| 86 | enum { |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 87 | PS3_SM_RX_MSG_LEN_MIN = 24, |
| 88 | PS3_SM_RX_MSG_LEN_MAX = 32, |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /** |
| 92 | * enum ps3_sys_manager_service_id - Message header service_id. |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 93 | * @PS3_SM_SERVICE_ID_REQUEST: guest --> sys_manager. |
| 94 | * @PS3_SM_SERVICE_ID_REQUEST_ERROR: guest <-- sys_manager. |
| 95 | * @PS3_SM_SERVICE_ID_COMMAND: guest <-- sys_manager. |
| 96 | * @PS3_SM_SERVICE_ID_RESPONSE: guest --> sys_manager. |
| 97 | * @PS3_SM_SERVICE_ID_SET_ATTR: guest --> sys_manager. |
| 98 | * @PS3_SM_SERVICE_ID_EXTERN_EVENT: guest <-- sys_manager. |
| 99 | * @PS3_SM_SERVICE_ID_SET_NEXT_OP: guest --> sys_manager. |
| 100 | * |
| 101 | * PS3_SM_SERVICE_ID_REQUEST_ERROR is returned for invalid data values in a |
| 102 | * a PS3_SM_SERVICE_ID_REQUEST message. It also seems to be returned when |
| 103 | * a REQUEST message is sent at the wrong time. |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 104 | */ |
| 105 | |
| 106 | enum ps3_sys_manager_service_id { |
| 107 | /* version 1 */ |
| 108 | PS3_SM_SERVICE_ID_REQUEST = 1, |
| 109 | PS3_SM_SERVICE_ID_RESPONSE = 2, |
| 110 | PS3_SM_SERVICE_ID_COMMAND = 3, |
| 111 | PS3_SM_SERVICE_ID_EXTERN_EVENT = 4, |
| 112 | PS3_SM_SERVICE_ID_SET_NEXT_OP = 5, |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 113 | PS3_SM_SERVICE_ID_REQUEST_ERROR = 6, |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 114 | PS3_SM_SERVICE_ID_SET_ATTR = 8, |
| 115 | }; |
| 116 | |
| 117 | /** |
| 118 | * enum ps3_sys_manager_attr - Notification attribute (bit position mask). |
| 119 | * @PS3_SM_ATTR_POWER: Power button. |
| 120 | * @PS3_SM_ATTR_RESET: Reset button, not available on retail console. |
| 121 | * @PS3_SM_ATTR_THERMAL: Sytem thermal alert. |
| 122 | * @PS3_SM_ATTR_CONTROLLER: Remote controller event. |
| 123 | * @PS3_SM_ATTR_ALL: Logical OR of all. |
| 124 | * |
| 125 | * The guest tells the system manager which events it is interested in receiving |
| 126 | * notice of by sending the system manager a logical OR of notification |
| 127 | * attributes via the ps3_sys_manager_send_attr() routine. |
| 128 | */ |
| 129 | |
| 130 | enum ps3_sys_manager_attr { |
| 131 | /* version 1 */ |
| 132 | PS3_SM_ATTR_POWER = 1, |
| 133 | PS3_SM_ATTR_RESET = 2, |
| 134 | PS3_SM_ATTR_THERMAL = 4, |
| 135 | PS3_SM_ATTR_CONTROLLER = 8, /* bogus? */ |
| 136 | PS3_SM_ATTR_ALL = 0x0f, |
| 137 | }; |
| 138 | |
| 139 | /** |
| 140 | * enum ps3_sys_manager_event - External event type, reported by system manager. |
| 141 | * @PS3_SM_EVENT_POWER_PRESSED: payload.value not used. |
| 142 | * @PS3_SM_EVENT_POWER_RELEASED: payload.value = time pressed in millisec. |
| 143 | * @PS3_SM_EVENT_RESET_PRESSED: payload.value not used. |
| 144 | * @PS3_SM_EVENT_RESET_RELEASED: payload.value = time pressed in millisec. |
| 145 | * @PS3_SM_EVENT_THERMAL_ALERT: payload.value = thermal zone id. |
| 146 | * @PS3_SM_EVENT_THERMAL_CLEARED: payload.value = thermal zone id. |
| 147 | */ |
| 148 | |
| 149 | enum ps3_sys_manager_event { |
| 150 | /* version 1 */ |
| 151 | PS3_SM_EVENT_POWER_PRESSED = 3, |
| 152 | PS3_SM_EVENT_POWER_RELEASED = 4, |
| 153 | PS3_SM_EVENT_RESET_PRESSED = 5, |
| 154 | PS3_SM_EVENT_RESET_RELEASED = 6, |
| 155 | PS3_SM_EVENT_THERMAL_ALERT = 7, |
| 156 | PS3_SM_EVENT_THERMAL_CLEARED = 8, |
| 157 | /* no info on controller events */ |
| 158 | }; |
| 159 | |
| 160 | /** |
| 161 | * enum ps3_sys_manager_next_op - Operation to perform after lpar is destroyed. |
| 162 | */ |
| 163 | |
| 164 | enum ps3_sys_manager_next_op { |
| 165 | /* version 3 */ |
| 166 | PS3_SM_NEXT_OP_SYS_SHUTDOWN = 1, |
| 167 | PS3_SM_NEXT_OP_SYS_REBOOT = 2, |
| 168 | PS3_SM_NEXT_OP_LPAR_REBOOT = 0x82, |
| 169 | }; |
| 170 | |
| 171 | /** |
| 172 | * enum ps3_sys_manager_wake_source - Next-op wakeup source (bit position mask). |
| 173 | * @PS3_SM_WAKE_DEFAULT: Disk insert, power button, eject button, IR |
| 174 | * controller, and bluetooth controller. |
| 175 | * @PS3_SM_WAKE_RTC: |
| 176 | * @PS3_SM_WAKE_RTC_ERROR: |
| 177 | * @PS3_SM_WAKE_P_O_R: Power on reset. |
| 178 | * |
| 179 | * Additional wakeup sources when specifying PS3_SM_NEXT_OP_SYS_SHUTDOWN. |
Geoff Levand | 50dad90 | 2008-02-09 09:53:01 +1100 | [diff] [blame^] | 180 | * The system will always wake from the PS3_SM_WAKE_DEFAULT sources. |
| 181 | * Sources listed here are the only ones available to guests in the |
| 182 | * other-os lpar. |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 183 | */ |
| 184 | |
| 185 | enum ps3_sys_manager_wake_source { |
| 186 | /* version 3 */ |
| 187 | PS3_SM_WAKE_DEFAULT = 0, |
| 188 | PS3_SM_WAKE_RTC = 0x00000040, |
| 189 | PS3_SM_WAKE_RTC_ERROR = 0x00000080, |
Geoff Levand | 50dad90 | 2008-02-09 09:53:01 +1100 | [diff] [blame^] | 190 | PS3_SM_WAKE_P_O_R = 0x80000000, |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | /** |
| 194 | * enum ps3_sys_manager_cmd - Command from system manager to guest. |
| 195 | * |
| 196 | * The guest completes the actions needed, then acks or naks the command via |
| 197 | * ps3_sys_manager_send_response(). In the case of @PS3_SM_CMD_SHUTDOWN, |
| 198 | * the guest must be fully prepared for a system poweroff prior to acking the |
| 199 | * command. |
| 200 | */ |
| 201 | |
| 202 | enum ps3_sys_manager_cmd { |
| 203 | /* version 1 */ |
| 204 | PS3_SM_CMD_SHUTDOWN = 1, /* shutdown guest OS */ |
| 205 | }; |
| 206 | |
| 207 | /** |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 208 | * ps3_sm_force_power_off - Poweroff helper. |
| 209 | * |
| 210 | * A global variable used to force a poweroff when the power button has |
| 211 | * been pressed irrespective of how init handles the ctrl_alt_del signal. |
| 212 | * |
| 213 | */ |
| 214 | |
| 215 | static unsigned int ps3_sm_force_power_off; |
| 216 | |
| 217 | /** |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 218 | * ps3_sys_manager_write - Helper to write a two part message to the vuart. |
| 219 | * |
| 220 | */ |
| 221 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 222 | static int ps3_sys_manager_write(struct ps3_system_bus_device *dev, |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 223 | const struct ps3_sys_manager_header *header, const void *payload) |
| 224 | { |
| 225 | int result; |
| 226 | |
| 227 | BUG_ON(header->version != 1); |
| 228 | BUG_ON(header->size != 16); |
| 229 | BUG_ON(header->payload_size != 8 && header->payload_size != 16); |
| 230 | BUG_ON(header->service_id > 8); |
| 231 | |
| 232 | result = ps3_vuart_write(dev, header, |
| 233 | sizeof(struct ps3_sys_manager_header)); |
| 234 | |
| 235 | if (!result) |
| 236 | result = ps3_vuart_write(dev, payload, header->payload_size); |
| 237 | |
| 238 | return result; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * ps3_sys_manager_send_attr - Send a 'set attribute' to the system manager. |
| 243 | * |
| 244 | */ |
| 245 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 246 | static int ps3_sys_manager_send_attr(struct ps3_system_bus_device *dev, |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 247 | enum ps3_sys_manager_attr attr) |
| 248 | { |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 249 | struct ps3_sys_manager_header header; |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 250 | struct { |
| 251 | u8 version; |
| 252 | u8 reserved_1[3]; |
| 253 | u32 attribute; |
| 254 | } payload; |
| 255 | |
| 256 | BUILD_BUG_ON(sizeof(payload) != 8); |
| 257 | |
| 258 | dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, attr); |
| 259 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 260 | memset(&header, 0, sizeof(header)); |
| 261 | header.version = 1; |
| 262 | header.size = 16; |
| 263 | header.payload_size = 16; |
| 264 | header.service_id = PS3_SM_SERVICE_ID_SET_ATTR; |
| 265 | |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 266 | memset(&payload, 0, sizeof(payload)); |
| 267 | payload.version = 1; |
| 268 | payload.attribute = attr; |
| 269 | |
| 270 | return ps3_sys_manager_write(dev, &header, &payload); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * ps3_sys_manager_send_next_op - Send a 'set next op' to the system manager. |
| 275 | * |
| 276 | * Tell the system manager what to do after this lpar is destroyed. |
| 277 | */ |
| 278 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 279 | static int ps3_sys_manager_send_next_op(struct ps3_system_bus_device *dev, |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 280 | enum ps3_sys_manager_next_op op, |
| 281 | enum ps3_sys_manager_wake_source wake_source) |
| 282 | { |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 283 | struct ps3_sys_manager_header header; |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 284 | struct { |
| 285 | u8 version; |
| 286 | u8 type; |
| 287 | u8 gos_id; |
| 288 | u8 reserved_1; |
| 289 | u32 wake_source; |
| 290 | u8 reserved_2[8]; |
| 291 | } payload; |
| 292 | |
| 293 | BUILD_BUG_ON(sizeof(payload) != 16); |
| 294 | |
| 295 | dev_dbg(&dev->core, "%s:%d: (%xh)\n", __func__, __LINE__, op); |
| 296 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 297 | memset(&header, 0, sizeof(header)); |
| 298 | header.version = 1; |
| 299 | header.size = 16; |
| 300 | header.payload_size = 16; |
| 301 | header.service_id = PS3_SM_SERVICE_ID_SET_NEXT_OP; |
| 302 | |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 303 | memset(&payload, 0, sizeof(payload)); |
| 304 | payload.version = 3; |
| 305 | payload.type = op; |
| 306 | payload.gos_id = 3; /* other os */ |
| 307 | payload.wake_source = wake_source; |
| 308 | |
| 309 | return ps3_sys_manager_write(dev, &header, &payload); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * ps3_sys_manager_send_request_shutdown - Send 'request' to the system manager. |
| 314 | * |
| 315 | * The guest sends this message to request an operation or action of the system |
| 316 | * manager. The reply is a command message from the system manager. In the |
| 317 | * command handler the guest performs the requested operation. The result of |
| 318 | * the command is then communicated back to the system manager with a response |
| 319 | * message. |
| 320 | * |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 321 | * Currently, the only supported request is the 'shutdown self' request. |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 322 | */ |
| 323 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 324 | static int ps3_sys_manager_send_request_shutdown( |
| 325 | struct ps3_system_bus_device *dev) |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 326 | { |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 327 | struct ps3_sys_manager_header header; |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 328 | struct { |
| 329 | u8 version; |
| 330 | u8 type; |
| 331 | u8 gos_id; |
| 332 | u8 reserved_1[13]; |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 333 | } payload; |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 334 | |
| 335 | BUILD_BUG_ON(sizeof(payload) != 16); |
| 336 | |
| 337 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 338 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 339 | memset(&header, 0, sizeof(header)); |
| 340 | header.version = 1; |
| 341 | header.size = 16; |
| 342 | header.payload_size = 16; |
| 343 | header.service_id = PS3_SM_SERVICE_ID_REQUEST; |
| 344 | |
| 345 | memset(&payload, 0, sizeof(payload)); |
| 346 | payload.version = 1; |
| 347 | payload.type = 1; /* shutdown */ |
| 348 | payload.gos_id = 0; /* self */ |
| 349 | |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 350 | return ps3_sys_manager_write(dev, &header, &payload); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * ps3_sys_manager_send_response - Send a 'response' to the system manager. |
| 355 | * @status: zero = success, others fail. |
| 356 | * |
| 357 | * The guest sends this message to the system manager to acnowledge success or |
| 358 | * failure of a command sent by the system manager. |
| 359 | */ |
| 360 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 361 | static int ps3_sys_manager_send_response(struct ps3_system_bus_device *dev, |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 362 | u64 status) |
| 363 | { |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 364 | struct ps3_sys_manager_header header; |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 365 | struct { |
| 366 | u8 version; |
| 367 | u8 reserved_1[3]; |
| 368 | u8 status; |
| 369 | u8 reserved_2[11]; |
| 370 | } payload; |
| 371 | |
| 372 | BUILD_BUG_ON(sizeof(payload) != 16); |
| 373 | |
| 374 | dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__, |
| 375 | (status ? "nak" : "ack")); |
| 376 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 377 | memset(&header, 0, sizeof(header)); |
| 378 | header.version = 1; |
| 379 | header.size = 16; |
| 380 | header.payload_size = 16; |
| 381 | header.service_id = PS3_SM_SERVICE_ID_RESPONSE; |
| 382 | |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 383 | memset(&payload, 0, sizeof(payload)); |
| 384 | payload.version = 1; |
| 385 | payload.status = status; |
| 386 | |
| 387 | return ps3_sys_manager_write(dev, &header, &payload); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * ps3_sys_manager_handle_event - Second stage event msg handler. |
| 392 | * |
| 393 | */ |
| 394 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 395 | static int ps3_sys_manager_handle_event(struct ps3_system_bus_device *dev) |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 396 | { |
| 397 | int result; |
| 398 | struct { |
| 399 | u8 version; |
| 400 | u8 type; |
| 401 | u8 reserved_1[2]; |
| 402 | u32 value; |
| 403 | u8 reserved_2[8]; |
| 404 | } event; |
| 405 | |
| 406 | BUILD_BUG_ON(sizeof(event) != 16); |
| 407 | |
| 408 | result = ps3_vuart_read(dev, &event, sizeof(event)); |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 409 | BUG_ON(result && "need to retry here"); |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 410 | |
| 411 | if (event.version != 1) { |
| 412 | dev_dbg(&dev->core, "%s:%d: unsupported event version (%u)\n", |
| 413 | __func__, __LINE__, event.version); |
| 414 | return -EIO; |
| 415 | } |
| 416 | |
| 417 | switch (event.type) { |
| 418 | case PS3_SM_EVENT_POWER_PRESSED: |
| 419 | dev_dbg(&dev->core, "%s:%d: POWER_PRESSED\n", |
| 420 | __func__, __LINE__); |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 421 | ps3_sm_force_power_off = 1; |
| 422 | /* |
| 423 | * A memory barrier is use here to sync memory since |
| 424 | * ps3_sys_manager_final_restart() could be called on |
| 425 | * another cpu. |
| 426 | */ |
| 427 | wmb(); |
| 428 | kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */ |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 429 | break; |
| 430 | case PS3_SM_EVENT_POWER_RELEASED: |
| 431 | dev_dbg(&dev->core, "%s:%d: POWER_RELEASED (%u ms)\n", |
| 432 | __func__, __LINE__, event.value); |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 433 | break; |
| 434 | case PS3_SM_EVENT_RESET_PRESSED: |
| 435 | dev_dbg(&dev->core, "%s:%d: RESET_PRESSED\n", |
| 436 | __func__, __LINE__); |
| 437 | ps3_sm_force_power_off = 0; |
| 438 | /* |
| 439 | * A memory barrier is use here to sync memory since |
| 440 | * ps3_sys_manager_final_restart() could be called on |
| 441 | * another cpu. |
| 442 | */ |
| 443 | wmb(); |
| 444 | kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */ |
| 445 | break; |
| 446 | case PS3_SM_EVENT_RESET_RELEASED: |
| 447 | dev_dbg(&dev->core, "%s:%d: RESET_RELEASED (%u ms)\n", |
| 448 | __func__, __LINE__, event.value); |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 449 | break; |
| 450 | case PS3_SM_EVENT_THERMAL_ALERT: |
| 451 | dev_dbg(&dev->core, "%s:%d: THERMAL_ALERT (zone %u)\n", |
| 452 | __func__, __LINE__, event.value); |
Geert Uytterhoeven | eff56c9 | 2008-01-19 07:32:14 +1100 | [diff] [blame] | 453 | pr_info("PS3 Thermal Alert Zone %u\n", event.value); |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 454 | break; |
| 455 | case PS3_SM_EVENT_THERMAL_CLEARED: |
| 456 | dev_dbg(&dev->core, "%s:%d: THERMAL_CLEARED (zone %u)\n", |
| 457 | __func__, __LINE__, event.value); |
| 458 | break; |
| 459 | default: |
| 460 | dev_dbg(&dev->core, "%s:%d: unknown event (%u)\n", |
| 461 | __func__, __LINE__, event.type); |
| 462 | return -EIO; |
| 463 | } |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | /** |
| 468 | * ps3_sys_manager_handle_cmd - Second stage command msg handler. |
| 469 | * |
| 470 | * The system manager sends this in reply to a 'request' message from the guest. |
| 471 | */ |
| 472 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 473 | static int ps3_sys_manager_handle_cmd(struct ps3_system_bus_device *dev) |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 474 | { |
| 475 | int result; |
| 476 | struct { |
| 477 | u8 version; |
| 478 | u8 type; |
| 479 | u8 reserved_1[14]; |
| 480 | } cmd; |
| 481 | |
| 482 | BUILD_BUG_ON(sizeof(cmd) != 16); |
| 483 | |
| 484 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 485 | |
| 486 | result = ps3_vuart_read(dev, &cmd, sizeof(cmd)); |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 487 | BUG_ON(result && "need to retry here"); |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 488 | |
Geert Uytterhoeven | eff56c9 | 2008-01-19 07:32:14 +1100 | [diff] [blame] | 489 | if (result) |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 490 | return result; |
| 491 | |
| 492 | if (cmd.version != 1) { |
| 493 | dev_dbg(&dev->core, "%s:%d: unsupported cmd version (%u)\n", |
| 494 | __func__, __LINE__, cmd.version); |
| 495 | return -EIO; |
| 496 | } |
| 497 | |
| 498 | if (cmd.type != PS3_SM_CMD_SHUTDOWN) { |
| 499 | dev_dbg(&dev->core, "%s:%d: unknown cmd (%u)\n", |
| 500 | __func__, __LINE__, cmd.type); |
| 501 | return -EIO; |
| 502 | } |
| 503 | |
| 504 | ps3_sys_manager_send_response(dev, 0); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * ps3_sys_manager_handle_msg - First stage msg handler. |
| 510 | * |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 511 | * Can be called directly to manually poll vuart and pump message handler. |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 512 | */ |
| 513 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 514 | static int ps3_sys_manager_handle_msg(struct ps3_system_bus_device *dev) |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 515 | { |
| 516 | int result; |
| 517 | struct ps3_sys_manager_header header; |
| 518 | |
| 519 | result = ps3_vuart_read(dev, &header, |
| 520 | sizeof(struct ps3_sys_manager_header)); |
| 521 | |
Geert Uytterhoeven | eff56c9 | 2008-01-19 07:32:14 +1100 | [diff] [blame] | 522 | if (result) |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 523 | return result; |
| 524 | |
| 525 | if (header.version != 1) { |
| 526 | dev_dbg(&dev->core, "%s:%d: unsupported header version (%u)\n", |
| 527 | __func__, __LINE__, header.version); |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 528 | dump_sm_header(&header); |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 529 | goto fail_header; |
| 530 | } |
| 531 | |
| 532 | BUILD_BUG_ON(sizeof(header) != 16); |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 533 | |
| 534 | if (header.size != 16 || (header.payload_size != 8 |
| 535 | && header.payload_size != 16)) { |
| 536 | dump_sm_header(&header); |
| 537 | BUG(); |
| 538 | } |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 539 | |
| 540 | switch (header.service_id) { |
| 541 | case PS3_SM_SERVICE_ID_EXTERN_EVENT: |
| 542 | dev_dbg(&dev->core, "%s:%d: EVENT\n", __func__, __LINE__); |
| 543 | return ps3_sys_manager_handle_event(dev); |
| 544 | case PS3_SM_SERVICE_ID_COMMAND: |
| 545 | dev_dbg(&dev->core, "%s:%d: COMMAND\n", __func__, __LINE__); |
| 546 | return ps3_sys_manager_handle_cmd(dev); |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 547 | case PS3_SM_SERVICE_ID_REQUEST_ERROR: |
| 548 | dev_dbg(&dev->core, "%s:%d: REQUEST_ERROR\n", __func__, |
| 549 | __LINE__); |
| 550 | dump_sm_header(&header); |
| 551 | break; |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 552 | default: |
| 553 | dev_dbg(&dev->core, "%s:%d: unknown service_id (%u)\n", |
| 554 | __func__, __LINE__, header.service_id); |
| 555 | break; |
| 556 | } |
| 557 | goto fail_id; |
| 558 | |
| 559 | fail_header: |
| 560 | ps3_vuart_clear_rx_bytes(dev, 0); |
| 561 | return -EIO; |
| 562 | fail_id: |
| 563 | ps3_vuart_clear_rx_bytes(dev, header.payload_size); |
| 564 | return -EIO; |
| 565 | } |
| 566 | |
| 567 | /** |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 568 | * ps3_sys_manager_final_power_off - The final platform machine_power_off routine. |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 569 | * |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 570 | * This routine never returns. The routine disables asynchronous vuart reads |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 571 | * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge |
| 572 | * the shutdown command sent from the system manager. Soon after the |
| 573 | * acknowledgement is sent the lpar is destroyed by the HV. This routine |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 574 | * should only be called from ps3_power_off() through |
| 575 | * ps3_sys_manager_ops.power_off. |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 576 | */ |
| 577 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 578 | static void ps3_sys_manager_final_power_off(struct ps3_system_bus_device *dev) |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 579 | { |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 580 | BUG_ON(!dev); |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 581 | |
| 582 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 583 | |
| 584 | ps3_vuart_cancel_async(dev); |
| 585 | |
| 586 | ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_SHUTDOWN, |
| 587 | PS3_SM_WAKE_DEFAULT); |
| 588 | ps3_sys_manager_send_request_shutdown(dev); |
| 589 | |
Geert Uytterhoeven | eff56c9 | 2008-01-19 07:32:14 +1100 | [diff] [blame] | 590 | pr_emerg("System Halted, OK to turn off power\n"); |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 591 | |
Geert Uytterhoeven | eff56c9 | 2008-01-19 07:32:14 +1100 | [diff] [blame] | 592 | while (1) |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 593 | ps3_sys_manager_handle_msg(dev); |
| 594 | } |
| 595 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 596 | /** |
| 597 | * ps3_sys_manager_final_restart - The final platform machine_restart routine. |
| 598 | * |
| 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_restart() through ps3_sys_manager_ops.restart. |
| 604 | */ |
| 605 | |
| 606 | static void ps3_sys_manager_final_restart(struct ps3_system_bus_device *dev) |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 607 | { |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 608 | BUG_ON(!dev); |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 609 | |
| 610 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 611 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 612 | /* Check if we got here via a power button event. */ |
| 613 | |
| 614 | if (ps3_sm_force_power_off) { |
| 615 | dev_dbg(&dev->core, "%s:%d: forcing poweroff\n", |
| 616 | __func__, __LINE__); |
| 617 | ps3_sys_manager_final_power_off(dev); |
| 618 | } |
| 619 | |
| 620 | ps3_vuart_cancel_async(dev); |
| 621 | |
| 622 | ps3_sys_manager_send_attr(dev, 0); |
Geoff Levand | 75ffe88 | 2008-02-09 09:52:55 +1100 | [diff] [blame] | 623 | ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_REBOOT, |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 624 | PS3_SM_WAKE_DEFAULT); |
| 625 | ps3_sys_manager_send_request_shutdown(dev); |
| 626 | |
Geert Uytterhoeven | eff56c9 | 2008-01-19 07:32:14 +1100 | [diff] [blame] | 627 | pr_emerg("System Halted, OK to turn off power\n"); |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 628 | |
Geert Uytterhoeven | eff56c9 | 2008-01-19 07:32:14 +1100 | [diff] [blame] | 629 | while (1) |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 630 | ps3_sys_manager_handle_msg(dev); |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * ps3_sys_manager_work - Asynchronous read handler. |
| 635 | * |
| 636 | * Signaled when PS3_SM_RX_MSG_LEN_MIN bytes arrive at the vuart port. |
| 637 | */ |
| 638 | |
| 639 | static void ps3_sys_manager_work(struct ps3_system_bus_device *dev) |
| 640 | { |
| 641 | ps3_sys_manager_handle_msg(dev); |
| 642 | ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN); |
| 643 | } |
| 644 | |
| 645 | static int ps3_sys_manager_probe(struct ps3_system_bus_device *dev) |
| 646 | { |
| 647 | int result; |
| 648 | struct ps3_sys_manager_ops ops; |
| 649 | |
| 650 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 651 | |
| 652 | ops.power_off = ps3_sys_manager_final_power_off; |
| 653 | ops.restart = ps3_sys_manager_final_restart; |
| 654 | ops.dev = dev; |
| 655 | |
| 656 | /* ps3_sys_manager_register_ops copies ops. */ |
| 657 | |
| 658 | ps3_sys_manager_register_ops(&ops); |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 659 | |
| 660 | result = ps3_sys_manager_send_attr(dev, PS3_SM_ATTR_ALL); |
| 661 | BUG_ON(result); |
| 662 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 663 | result = ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN); |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 664 | BUG_ON(result); |
| 665 | |
| 666 | return result; |
| 667 | } |
| 668 | |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 669 | static int ps3_sys_manager_remove(struct ps3_system_bus_device *dev) |
| 670 | { |
| 671 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | static void ps3_sys_manager_shutdown(struct ps3_system_bus_device *dev) |
| 676 | { |
| 677 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 678 | } |
| 679 | |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 680 | static struct ps3_vuart_port_driver ps3_sys_manager = { |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 681 | .core.match_id = PS3_MATCH_ID_SYSTEM_MANAGER, |
| 682 | .core.core.name = "ps3_sys_manager", |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 683 | .probe = ps3_sys_manager_probe, |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 684 | .remove = ps3_sys_manager_remove, |
| 685 | .shutdown = ps3_sys_manager_shutdown, |
| 686 | .work = ps3_sys_manager_work, |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 687 | }; |
| 688 | |
| 689 | static int __init ps3_sys_manager_init(void) |
| 690 | { |
Geert Uytterhoeven | ef596c6 | 2007-03-10 00:05:38 +0100 | [diff] [blame] | 691 | if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) |
| 692 | return -ENODEV; |
| 693 | |
Geoff Levand | fde5efd | 2007-02-07 12:20:01 -0800 | [diff] [blame] | 694 | return ps3_vuart_port_driver_register(&ps3_sys_manager); |
| 695 | } |
| 696 | |
| 697 | module_init(ps3_sys_manager_init); |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 698 | /* Module remove not supported. */ |
| 699 | |
Geoff Levand | 50dad90 | 2008-02-09 09:53:01 +1100 | [diff] [blame^] | 700 | MODULE_AUTHOR("Sony Corporation"); |
| 701 | MODULE_LICENSE("GPL v2"); |
| 702 | MODULE_DESCRIPTION("PS3 System Manager"); |
Geoff Levand | 66c63b8 | 2007-06-16 08:03:54 +1000 | [diff] [blame] | 703 | MODULE_ALIAS(PS3_MODULE_ALIAS_SYSTEM_MANAGER); |