ps3-sys-manager.c 20 KB

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