Pull asus into release branch
[pandora-kernel.git] / drivers / ps3 / sys-manager.c
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>
25
26 #include <asm/firmware.h>
27 #include <asm/ps3.h>
28
29 #include "vuart.h"
30
31 MODULE_AUTHOR("Sony Corporation");
32 MODULE_LICENSE("GPL v2");
33 MODULE_DESCRIPTION("PS3 System Manager");
34
35 /**
36  * ps3_sys_manager - PS3 system manager driver.
37  *
38  * The system manager provides an asyncronous system event notification
39  * mechanism for reporting events like thermal alert and button presses to
40  * guests.  It also provides support to control system shutdown and startup.
41  *
42  * The actual system manager is implemented as an application running in the
43  * system policy module in lpar_1.  Guests communicate with the system manager
44  * through port 2 of the vuart using a simple packet message protocol.
45  * Messages are comprised of a fixed field header followed by a message
46  * specific payload.
47  */
48
49 /**
50  * struct ps3_sys_manager_header - System manager message header.
51  * @version: Header version, currently 1.
52  * @size: Header size in bytes, curently 16.
53  * @payload_size: Message payload size in bytes.
54  * @service_id: Message type, one of enum ps3_sys_manager_service_id.
55  */
56
57 struct ps3_sys_manager_header {
58         /* version 1 */
59         u8 version;
60         u8 size;
61         u16 reserved_1;
62         u32 payload_size;
63         u16 service_id;
64         u16 reserved_2[3];
65 };
66
67 /**
68  * @PS3_SM_RX_MSG_LEN - System manager received message length.
69  *
70  * Currently all messages received from the system manager are the same length
71  * (16 bytes header + 16 bytes payload = 32 bytes).  This knowlege is used to
72  * simplify the logic.
73  */
74
75 enum {
76         PS3_SM_RX_MSG_LEN = 32,
77 };
78
79 /**
80  * enum ps3_sys_manager_service_id - Message header service_id.
81  * @PS3_SM_SERVICE_ID_REQUEST:      guest --> sys_manager.
82  * @PS3_SM_SERVICE_ID_COMMAND:      guest <-- sys_manager.
83  * @PS3_SM_SERVICE_ID_RESPONSE:     guest --> sys_manager.
84  * @PS3_SM_SERVICE_ID_SET_ATTR:     guest --> sys_manager.
85  * @PS3_SM_SERVICE_ID_EXTERN_EVENT: guest <-- sys_manager.
86  * @PS3_SM_SERVICE_ID_SET_NEXT_OP:  guest --> sys_manager.
87  */
88
89 enum ps3_sys_manager_service_id {
90         /* version 1 */
91         PS3_SM_SERVICE_ID_REQUEST = 1,
92         PS3_SM_SERVICE_ID_RESPONSE = 2,
93         PS3_SM_SERVICE_ID_COMMAND = 3,
94         PS3_SM_SERVICE_ID_EXTERN_EVENT = 4,
95         PS3_SM_SERVICE_ID_SET_NEXT_OP = 5,
96         PS3_SM_SERVICE_ID_SET_ATTR = 8,
97 };
98
99 /**
100  * enum ps3_sys_manager_attr - Notification attribute (bit position mask).
101  * @PS3_SM_ATTR_POWER: Power button.
102  * @PS3_SM_ATTR_RESET: Reset button, not available on retail console.
103  * @PS3_SM_ATTR_THERMAL: Sytem thermal alert.
104  * @PS3_SM_ATTR_CONTROLLER: Remote controller event.
105  * @PS3_SM_ATTR_ALL: Logical OR of all.
106  *
107  * The guest tells the system manager which events it is interested in receiving
108  * notice of by sending the system manager a logical OR of notification
109  * attributes via the ps3_sys_manager_send_attr() routine.
110  */
111
112 enum ps3_sys_manager_attr {
113         /* version 1 */
114         PS3_SM_ATTR_POWER = 1,
115         PS3_SM_ATTR_RESET = 2,
116         PS3_SM_ATTR_THERMAL = 4,
117         PS3_SM_ATTR_CONTROLLER = 8, /* bogus? */
118         PS3_SM_ATTR_ALL = 0x0f,
119 };
120
121 /**
122  * enum ps3_sys_manager_event - External event type, reported by system manager.
123  * @PS3_SM_EVENT_POWER_PRESSED: payload.value not used.
124  * @PS3_SM_EVENT_POWER_RELEASED: payload.value = time pressed in millisec.
125  * @PS3_SM_EVENT_RESET_PRESSED: payload.value not used.
126  * @PS3_SM_EVENT_RESET_RELEASED: payload.value = time pressed in millisec.
127  * @PS3_SM_EVENT_THERMAL_ALERT: payload.value = thermal zone id.
128  * @PS3_SM_EVENT_THERMAL_CLEARED: payload.value = thermal zone id.
129  */
130
131 enum ps3_sys_manager_event {
132         /* version 1 */
133         PS3_SM_EVENT_POWER_PRESSED = 3,
134         PS3_SM_EVENT_POWER_RELEASED = 4,
135         PS3_SM_EVENT_RESET_PRESSED = 5,
136         PS3_SM_EVENT_RESET_RELEASED = 6,
137         PS3_SM_EVENT_THERMAL_ALERT = 7,
138         PS3_SM_EVENT_THERMAL_CLEARED = 8,
139         /* no info on controller events */
140 };
141
142 /**
143  * enum ps3_sys_manager_next_op - Operation to perform after lpar is destroyed.
144  */
145
146 enum ps3_sys_manager_next_op {
147         /* version 3 */
148         PS3_SM_NEXT_OP_SYS_SHUTDOWN = 1,
149         PS3_SM_NEXT_OP_SYS_REBOOT = 2,
150         PS3_SM_NEXT_OP_LPAR_REBOOT = 0x82,
151 };
152
153 /**
154  * enum ps3_sys_manager_wake_source - Next-op wakeup source (bit position mask).
155  * @PS3_SM_WAKE_DEFAULT: Disk insert, power button, eject button, IR
156  * controller, and bluetooth controller.
157  * @PS3_SM_WAKE_RTC:
158  * @PS3_SM_WAKE_RTC_ERROR:
159  * @PS3_SM_WAKE_P_O_R: Power on reset.
160  *
161  * Additional wakeup sources when specifying PS3_SM_NEXT_OP_SYS_SHUTDOWN.
162  * System will always wake from the PS3_SM_WAKE_DEFAULT sources.
163  */
164
165 enum ps3_sys_manager_wake_source {
166         /* version 3 */
167         PS3_SM_WAKE_DEFAULT   = 0,
168         PS3_SM_WAKE_RTC       = 0x00000040,
169         PS3_SM_WAKE_RTC_ERROR = 0x00000080,
170         PS3_SM_WAKE_P_O_R     = 0x10000000,
171 };
172
173 /**
174  * enum ps3_sys_manager_cmd - Command from system manager to guest.
175  *
176  * The guest completes the actions needed, then acks or naks the command via
177  * ps3_sys_manager_send_response().  In the case of @PS3_SM_CMD_SHUTDOWN,
178  * the guest must be fully prepared for a system poweroff prior to acking the
179  * command.
180  */
181
182 enum ps3_sys_manager_cmd {
183         /* version 1 */
184         PS3_SM_CMD_SHUTDOWN = 1, /* shutdown guest OS */
185 };
186
187 /**
188  * ps3_sys_manager_write - Helper to write a two part message to the vuart.
189  *
190  */
191
192 static int ps3_sys_manager_write(struct ps3_vuart_port_device *dev,
193         const struct ps3_sys_manager_header *header, const void *payload)
194 {
195         int result;
196
197         BUG_ON(header->version != 1);
198         BUG_ON(header->size != 16);
199         BUG_ON(header->payload_size != 8 && header->payload_size != 16);
200         BUG_ON(header->service_id > 8);
201
202         result = ps3_vuart_write(dev, header,
203                 sizeof(struct ps3_sys_manager_header));
204
205         if (!result)
206                 result = ps3_vuart_write(dev, payload, header->payload_size);
207
208         return result;
209 }
210
211 /**
212  * ps3_sys_manager_send_attr - Send a 'set attribute' to the system manager.
213  *
214  */
215
216 static int ps3_sys_manager_send_attr(struct ps3_vuart_port_device *dev,
217         enum ps3_sys_manager_attr attr)
218 {
219         static const struct ps3_sys_manager_header header = {
220                 .version = 1,
221                 .size = 16,
222                 .payload_size = 16,
223                 .service_id = PS3_SM_SERVICE_ID_SET_ATTR,
224         };
225         struct {
226                 u8 version;
227                 u8 reserved_1[3];
228                 u32 attribute;
229         } payload;
230
231         BUILD_BUG_ON(sizeof(payload) != 8);
232
233         dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, attr);
234
235         memset(&payload, 0, sizeof(payload));
236         payload.version = 1;
237         payload.attribute = attr;
238
239         return ps3_sys_manager_write(dev, &header, &payload);
240 }
241
242 /**
243  * ps3_sys_manager_send_next_op - Send a 'set next op' to the system manager.
244  *
245  * Tell the system manager what to do after this lpar is destroyed.
246  */
247
248 static int ps3_sys_manager_send_next_op(struct ps3_vuart_port_device *dev,
249         enum ps3_sys_manager_next_op op,
250         enum ps3_sys_manager_wake_source wake_source)
251 {
252         static const struct ps3_sys_manager_header header = {
253                 .version = 1,
254                 .size = 16,
255                 .payload_size = 16,
256                 .service_id = PS3_SM_SERVICE_ID_SET_NEXT_OP,
257         };
258         struct {
259                 u8 version;
260                 u8 type;
261                 u8 gos_id;
262                 u8 reserved_1;
263                 u32 wake_source;
264                 u8 reserved_2[8];
265         } payload;
266
267         BUILD_BUG_ON(sizeof(payload) != 16);
268
269         dev_dbg(&dev->core, "%s:%d: (%xh)\n", __func__, __LINE__, op);
270
271         memset(&payload, 0, sizeof(payload));
272         payload.version = 3;
273         payload.type = op;
274         payload.gos_id = 3; /* other os */
275         payload.wake_source = wake_source;
276
277         return ps3_sys_manager_write(dev, &header, &payload);
278 }
279
280 /**
281  * ps3_sys_manager_send_request_shutdown - Send 'request' to the system manager.
282  *
283  * The guest sends this message to request an operation or action of the system
284  * manager.  The reply is a command message from the system manager.  In the
285  * command handler the guest performs the requested operation.  The result of
286  * the command is then communicated back to the system manager with a response
287  * message.
288  *
289  * Currently, the only supported request it the 'shutdown self' request.
290  */
291
292 static int ps3_sys_manager_send_request_shutdown(struct ps3_vuart_port_device *dev)
293 {
294         static const struct ps3_sys_manager_header header = {
295                 .version = 1,
296                 .size = 16,
297                 .payload_size = 16,
298                 .service_id = PS3_SM_SERVICE_ID_REQUEST,
299         };
300         struct {
301                 u8 version;
302                 u8 type;
303                 u8 gos_id;
304                 u8 reserved_1[13];
305         } static const payload = {
306                 .version = 1,
307                 .type = 1, /* shutdown */
308                 .gos_id = 0, /* self */
309         };
310
311         BUILD_BUG_ON(sizeof(payload) != 16);
312
313         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
314
315         return ps3_sys_manager_write(dev, &header, &payload);
316 }
317
318 /**
319  * ps3_sys_manager_send_response - Send a 'response' to the system manager.
320  * @status: zero = success, others fail.
321  *
322  * The guest sends this message to the system manager to acnowledge success or
323  * failure of a command sent by the system manager.
324  */
325
326 static int ps3_sys_manager_send_response(struct ps3_vuart_port_device *dev,
327         u64 status)
328 {
329         static const struct ps3_sys_manager_header header = {
330                 .version = 1,
331                 .size = 16,
332                 .payload_size = 16,
333                 .service_id = PS3_SM_SERVICE_ID_RESPONSE,
334         };
335         struct {
336                 u8 version;
337                 u8 reserved_1[3];
338                 u8 status;
339                 u8 reserved_2[11];
340         } payload;
341
342         BUILD_BUG_ON(sizeof(payload) != 16);
343
344         dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
345                 (status ? "nak" : "ack"));
346
347         memset(&payload, 0, sizeof(payload));
348         payload.version = 1;
349         payload.status = status;
350
351         return ps3_sys_manager_write(dev, &header, &payload);
352 }
353
354 /**
355  * ps3_sys_manager_handle_event - Second stage event msg handler.
356  *
357  */
358
359 static int ps3_sys_manager_handle_event(struct ps3_vuart_port_device *dev)
360 {
361         int result;
362         struct {
363                 u8 version;
364                 u8 type;
365                 u8 reserved_1[2];
366                 u32 value;
367                 u8 reserved_2[8];
368         } event;
369
370         BUILD_BUG_ON(sizeof(event) != 16);
371
372         result = ps3_vuart_read(dev, &event, sizeof(event));
373         BUG_ON(result);
374
375         if (event.version != 1) {
376                 dev_dbg(&dev->core, "%s:%d: unsupported event version (%u)\n",
377                         __func__, __LINE__, event.version);
378                 return -EIO;
379         }
380
381         switch (event.type) {
382         case PS3_SM_EVENT_POWER_PRESSED:
383                 dev_dbg(&dev->core, "%s:%d: POWER_PRESSED\n",
384                         __func__, __LINE__);
385                 break;
386         case PS3_SM_EVENT_POWER_RELEASED:
387                 dev_dbg(&dev->core, "%s:%d: POWER_RELEASED (%u ms)\n",
388                         __func__, __LINE__, event.value);
389                 kill_cad_pid(SIGINT, 1);
390                 break;
391         case PS3_SM_EVENT_THERMAL_ALERT:
392                 dev_dbg(&dev->core, "%s:%d: THERMAL_ALERT (zone %u)\n",
393                         __func__, __LINE__, event.value);
394                 printk(KERN_INFO "PS3 Thermal Alert Zone %u\n", event.value);
395                 break;
396         case PS3_SM_EVENT_THERMAL_CLEARED:
397                 dev_dbg(&dev->core, "%s:%d: THERMAL_CLEARED (zone %u)\n",
398                         __func__, __LINE__, event.value);
399                 break;
400         default:
401                 dev_dbg(&dev->core, "%s:%d: unknown event (%u)\n",
402                         __func__, __LINE__, event.type);
403                 return -EIO;
404         }
405
406         return 0;
407 }
408 /**
409  * ps3_sys_manager_handle_cmd - Second stage command msg handler.
410  *
411  * The system manager sends this in reply to a 'request' message from the guest.
412  */
413
414 static int ps3_sys_manager_handle_cmd(struct ps3_vuart_port_device *dev)
415 {
416         int result;
417         struct {
418                 u8 version;
419                 u8 type;
420                 u8 reserved_1[14];
421         } cmd;
422
423         BUILD_BUG_ON(sizeof(cmd) != 16);
424
425         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
426
427         result = ps3_vuart_read(dev, &cmd, sizeof(cmd));
428
429         if(result)
430                 return result;
431
432         if (cmd.version != 1) {
433                 dev_dbg(&dev->core, "%s:%d: unsupported cmd version (%u)\n",
434                         __func__, __LINE__, cmd.version);
435                 return -EIO;
436         }
437
438         if (cmd.type != PS3_SM_CMD_SHUTDOWN) {
439                 dev_dbg(&dev->core, "%s:%d: unknown cmd (%u)\n",
440                         __func__, __LINE__, cmd.type);
441                 return -EIO;
442         }
443
444         ps3_sys_manager_send_response(dev, 0);
445         return 0;
446 }
447
448 /**
449  * ps3_sys_manager_handle_msg - First stage msg handler.
450  *
451  */
452
453 static int ps3_sys_manager_handle_msg(struct ps3_vuart_port_device *dev)
454 {
455         int result;
456         struct ps3_sys_manager_header header;
457
458         result = ps3_vuart_read(dev, &header,
459                 sizeof(struct ps3_sys_manager_header));
460
461         if(result)
462                 return result;
463
464         if (header.version != 1) {
465                 dev_dbg(&dev->core, "%s:%d: unsupported header version (%u)\n",
466                         __func__, __LINE__, header.version);
467                 goto fail_header;
468         }
469
470         BUILD_BUG_ON(sizeof(header) != 16);
471         BUG_ON(header.size != 16);
472         BUG_ON(header.payload_size != 16);
473
474         switch (header.service_id) {
475         case PS3_SM_SERVICE_ID_EXTERN_EVENT:
476                 dev_dbg(&dev->core, "%s:%d: EVENT\n", __func__, __LINE__);
477                 return ps3_sys_manager_handle_event(dev);
478         case PS3_SM_SERVICE_ID_COMMAND:
479                 dev_dbg(&dev->core, "%s:%d: COMMAND\n", __func__, __LINE__);
480                 return ps3_sys_manager_handle_cmd(dev);
481         default:
482                 dev_dbg(&dev->core, "%s:%d: unknown service_id (%u)\n",
483                         __func__, __LINE__, header.service_id);
484                 break;
485         }
486         goto fail_id;
487
488 fail_header:
489         ps3_vuart_clear_rx_bytes(dev, 0);
490         return -EIO;
491 fail_id:
492         ps3_vuart_clear_rx_bytes(dev, header.payload_size);
493         return -EIO;
494 }
495
496 /**
497  * ps3_sys_manager_work - Asyncronous read handler.
498  *
499  * Signaled when a complete message arrives at the vuart port.
500  */
501
502 static void ps3_sys_manager_work(struct work_struct *work)
503 {
504         struct ps3_vuart_port_device *dev = ps3_vuart_work_to_port_device(work);
505
506         ps3_sys_manager_handle_msg(dev);
507         ps3_vuart_read_async(dev, ps3_sys_manager_work, PS3_SM_RX_MSG_LEN);
508 }
509
510 struct {
511         struct ps3_vuart_port_device *dev;
512 } static drv_priv;
513
514 /**
515  * ps3_sys_manager_restart - The final platform machine_restart routine.
516  *
517  * This routine never returns.  The routine disables asyncronous vuart reads
518  * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
519  * the shutdown command sent from the system manager.  Soon after the
520  * acknowledgement is sent the lpar is destroyed by the HV.  This routine
521  * should only be called from ps3_restart().
522  */
523
524 void ps3_sys_manager_restart(void)
525 {
526         struct ps3_vuart_port_device *dev = drv_priv.dev;
527
528         BUG_ON(!drv_priv.dev);
529
530         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
531
532         ps3_vuart_cancel_async(dev);
533
534         ps3_sys_manager_send_attr(dev, 0);
535         ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_LPAR_REBOOT,
536                 PS3_SM_WAKE_DEFAULT);
537         ps3_sys_manager_send_request_shutdown(dev);
538
539         printk(KERN_EMERG "System Halted, OK to turn off power\n");
540
541         while(1)
542                 ps3_sys_manager_handle_msg(dev);
543 }
544
545 /**
546  * ps3_sys_manager_power_off - The final platform machine_power_off routine.
547  *
548  * This routine never returns.  The routine disables asyncronous vuart reads
549  * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
550  * the shutdown command sent from the system manager.  Soon after the
551  * acknowledgement is sent the lpar is destroyed by the HV.  This routine
552  * should only be called from ps3_power_off().
553  */
554
555 void ps3_sys_manager_power_off(void)
556 {
557         struct ps3_vuart_port_device *dev = drv_priv.dev;
558
559         BUG_ON(!drv_priv.dev);
560
561         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
562
563         ps3_vuart_cancel_async(dev);
564
565         ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_SHUTDOWN,
566                 PS3_SM_WAKE_DEFAULT);
567         ps3_sys_manager_send_request_shutdown(dev);
568
569         printk(KERN_EMERG "System Halted, OK to turn off power\n");
570
571         while(1)
572                 ps3_sys_manager_handle_msg(dev);
573 }
574
575 static int ps3_sys_manager_probe(struct ps3_vuart_port_device *dev)
576 {
577         int result;
578
579         dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
580
581         BUG_ON(drv_priv.dev);
582         drv_priv.dev = dev;
583
584         result = ps3_sys_manager_send_attr(dev, PS3_SM_ATTR_ALL);
585         BUG_ON(result);
586
587         result = ps3_vuart_read_async(dev, ps3_sys_manager_work,
588                 PS3_SM_RX_MSG_LEN);
589         BUG_ON(result);
590
591         return result;
592 }
593
594 static struct ps3_vuart_port_driver ps3_sys_manager = {
595         .match_id = PS3_MATCH_ID_SYSTEM_MANAGER,
596         .core = {
597                 .name = "ps3_sys_manager",
598         },
599         .probe = ps3_sys_manager_probe,
600 };
601
602 static int __init ps3_sys_manager_init(void)
603 {
604         if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
605                 return -ENODEV;
606
607         return ps3_vuart_port_driver_register(&ps3_sys_manager);
608 }
609
610 module_init(ps3_sys_manager_init);