ps3-sys-manager.c 20 KB

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