ps3-sys-manager.c 20 KB

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