ps3-sys-manager.c 20 KB

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