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