ps3-sys-manager.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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, curently 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 knowlege 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: Sytem 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, IR
  168. * controller, and bluetooth controller.
  169. * @PS3_SM_WAKE_RTC:
  170. * @PS3_SM_WAKE_RTC_ERROR:
  171. * @PS3_SM_WAKE_W_O_L: Ether or wireless LAN.
  172. * @PS3_SM_WAKE_P_O_R: Power on reset.
  173. *
  174. * Additional wakeup sources when specifying PS3_SM_NEXT_OP_SYS_SHUTDOWN.
  175. * The system will always wake from the PS3_SM_WAKE_DEFAULT sources.
  176. * Sources listed here are the only ones available to guests in the
  177. * other-os lpar.
  178. */
  179. enum ps3_sys_manager_wake_source {
  180. /* version 3 */
  181. PS3_SM_WAKE_DEFAULT = 0,
  182. PS3_SM_WAKE_RTC = 0x00000040,
  183. PS3_SM_WAKE_RTC_ERROR = 0x00000080,
  184. PS3_SM_WAKE_W_O_L = 0x00000400,
  185. PS3_SM_WAKE_P_O_R = 0x80000000,
  186. };
  187. /**
  188. * user_wake_sources - User specified wakeup sources.
  189. *
  190. * Logical OR of enum ps3_sys_manager_wake_source types.
  191. */
  192. static u32 user_wake_sources = PS3_SM_WAKE_DEFAULT;
  193. /**
  194. * enum ps3_sys_manager_cmd - Command from system manager to guest.
  195. *
  196. * The guest completes the actions needed, then acks or naks the command via
  197. * ps3_sys_manager_send_response(). In the case of @PS3_SM_CMD_SHUTDOWN,
  198. * the guest must be fully prepared for a system poweroff prior to acking the
  199. * command.
  200. */
  201. enum ps3_sys_manager_cmd {
  202. /* version 1 */
  203. PS3_SM_CMD_SHUTDOWN = 1, /* shutdown guest OS */
  204. };
  205. /**
  206. * ps3_sm_force_power_off - Poweroff helper.
  207. *
  208. * A global variable used to force a poweroff when the power button has
  209. * been pressed irrespective of how init handles the ctrl_alt_del signal.
  210. *
  211. */
  212. static unsigned int ps3_sm_force_power_off;
  213. /**
  214. * ps3_sys_manager_write - Helper to write a two part message to the vuart.
  215. *
  216. */
  217. static int ps3_sys_manager_write(struct ps3_system_bus_device *dev,
  218. const struct ps3_sys_manager_header *header, const void *payload)
  219. {
  220. int result;
  221. BUG_ON(header->version != 1);
  222. BUG_ON(header->size != 16);
  223. BUG_ON(header->payload_size != 8 && header->payload_size != 16);
  224. BUG_ON(header->service_id > 8);
  225. result = ps3_vuart_write(dev, header,
  226. sizeof(struct ps3_sys_manager_header));
  227. if (!result)
  228. result = ps3_vuart_write(dev, payload, header->payload_size);
  229. return result;
  230. }
  231. /**
  232. * ps3_sys_manager_send_attr - Send a 'set attribute' to the system manager.
  233. *
  234. */
  235. static int ps3_sys_manager_send_attr(struct ps3_system_bus_device *dev,
  236. enum ps3_sys_manager_attr attr)
  237. {
  238. struct ps3_sys_manager_header header;
  239. struct {
  240. u8 version;
  241. u8 reserved_1[3];
  242. u32 attribute;
  243. } payload;
  244. BUILD_BUG_ON(sizeof(payload) != 8);
  245. dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, attr);
  246. memset(&header, 0, sizeof(header));
  247. header.version = 1;
  248. header.size = 16;
  249. header.payload_size = 16;
  250. header.service_id = PS3_SM_SERVICE_ID_SET_ATTR;
  251. memset(&payload, 0, sizeof(payload));
  252. payload.version = 1;
  253. payload.attribute = attr;
  254. return ps3_sys_manager_write(dev, &header, &payload);
  255. }
  256. /**
  257. * ps3_sys_manager_send_next_op - Send a 'set next op' to the system manager.
  258. *
  259. * Tell the system manager what to do after this lpar is destroyed.
  260. */
  261. static int ps3_sys_manager_send_next_op(struct ps3_system_bus_device *dev,
  262. enum ps3_sys_manager_next_op op,
  263. enum ps3_sys_manager_wake_source wake_source)
  264. {
  265. struct ps3_sys_manager_header header;
  266. struct {
  267. u8 version;
  268. u8 type;
  269. u8 gos_id;
  270. u8 reserved_1;
  271. u32 wake_source;
  272. u8 reserved_2[8];
  273. } payload;
  274. BUILD_BUG_ON(sizeof(payload) != 16);
  275. dev_dbg(&dev->core, "%s:%d: (%xh)\n", __func__, __LINE__, op);
  276. memset(&header, 0, sizeof(header));
  277. header.version = 1;
  278. header.size = 16;
  279. header.payload_size = 16;
  280. header.service_id = PS3_SM_SERVICE_ID_SET_NEXT_OP;
  281. memset(&payload, 0, sizeof(payload));
  282. payload.version = 3;
  283. payload.type = op;
  284. payload.gos_id = 3; /* other os */
  285. payload.wake_source = wake_source;
  286. return ps3_sys_manager_write(dev, &header, &payload);
  287. }
  288. /**
  289. * ps3_sys_manager_send_request_shutdown - Send 'request' to the system manager.
  290. *
  291. * The guest sends this message to request an operation or action of the system
  292. * manager. The reply is a command message from the system manager. In the
  293. * command handler the guest performs the requested operation. The result of
  294. * the command is then communicated back to the system manager with a response
  295. * message.
  296. *
  297. * Currently, the only supported request is the 'shutdown self' request.
  298. */
  299. static int ps3_sys_manager_send_request_shutdown(
  300. struct ps3_system_bus_device *dev)
  301. {
  302. struct ps3_sys_manager_header header;
  303. struct {
  304. u8 version;
  305. u8 type;
  306. u8 gos_id;
  307. u8 reserved_1[13];
  308. } payload;
  309. BUILD_BUG_ON(sizeof(payload) != 16);
  310. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  311. memset(&header, 0, sizeof(header));
  312. header.version = 1;
  313. header.size = 16;
  314. header.payload_size = 16;
  315. header.service_id = PS3_SM_SERVICE_ID_REQUEST;
  316. memset(&payload, 0, sizeof(payload));
  317. payload.version = 1;
  318. payload.type = 1; /* shutdown */
  319. payload.gos_id = 0; /* self */
  320. return ps3_sys_manager_write(dev, &header, &payload);
  321. }
  322. /**
  323. * ps3_sys_manager_send_response - Send a 'response' to the system manager.
  324. * @status: zero = success, others fail.
  325. *
  326. * The guest sends this message to the system manager to acnowledge success or
  327. * failure of a command sent by the system manager.
  328. */
  329. static int ps3_sys_manager_send_response(struct ps3_system_bus_device *dev,
  330. u64 status)
  331. {
  332. struct ps3_sys_manager_header header;
  333. struct {
  334. u8 version;
  335. u8 reserved_1[3];
  336. u8 status;
  337. u8 reserved_2[11];
  338. } payload;
  339. BUILD_BUG_ON(sizeof(payload) != 16);
  340. dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
  341. (status ? "nak" : "ack"));
  342. memset(&header, 0, sizeof(header));
  343. header.version = 1;
  344. header.size = 16;
  345. header.payload_size = 16;
  346. header.service_id = PS3_SM_SERVICE_ID_RESPONSE;
  347. memset(&payload, 0, sizeof(payload));
  348. payload.version = 1;
  349. payload.status = status;
  350. return ps3_sys_manager_write(dev, &header, &payload);
  351. }
  352. /**
  353. * ps3_sys_manager_handle_event - Second stage event msg handler.
  354. *
  355. */
  356. static int ps3_sys_manager_handle_event(struct ps3_system_bus_device *dev)
  357. {
  358. int result;
  359. struct {
  360. u8 version;
  361. u8 type;
  362. u8 reserved_1[2];
  363. u32 value;
  364. u8 reserved_2[8];
  365. } event;
  366. BUILD_BUG_ON(sizeof(event) != 16);
  367. result = ps3_vuart_read(dev, &event, sizeof(event));
  368. BUG_ON(result && "need to retry here");
  369. if (event.version != 1) {
  370. dev_dbg(&dev->core, "%s:%d: unsupported event version (%u)\n",
  371. __func__, __LINE__, event.version);
  372. return -EIO;
  373. }
  374. switch (event.type) {
  375. case PS3_SM_EVENT_POWER_PRESSED:
  376. dev_dbg(&dev->core, "%s:%d: POWER_PRESSED (%s)\n",
  377. __func__, __LINE__,
  378. (event.value == PS3_SM_BUTTON_EVENT_SOFT ? "soft"
  379. : "hard"));
  380. ps3_sm_force_power_off = 1;
  381. /*
  382. * A memory barrier is use here to sync memory since
  383. * ps3_sys_manager_final_restart() could be called on
  384. * another cpu.
  385. */
  386. wmb();
  387. kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */
  388. break;
  389. case PS3_SM_EVENT_POWER_RELEASED:
  390. dev_dbg(&dev->core, "%s:%d: POWER_RELEASED (%u ms)\n",
  391. __func__, __LINE__, event.value);
  392. break;
  393. case PS3_SM_EVENT_RESET_PRESSED:
  394. dev_dbg(&dev->core, "%s:%d: RESET_PRESSED (%s)\n",
  395. __func__, __LINE__,
  396. (event.value == PS3_SM_BUTTON_EVENT_SOFT ? "soft"
  397. : "hard"));
  398. ps3_sm_force_power_off = 0;
  399. /*
  400. * A memory barrier is use here to sync memory since
  401. * ps3_sys_manager_final_restart() could be called on
  402. * another cpu.
  403. */
  404. wmb();
  405. kill_cad_pid(SIGINT, 1); /* ctrl_alt_del */
  406. break;
  407. case PS3_SM_EVENT_RESET_RELEASED:
  408. dev_dbg(&dev->core, "%s:%d: RESET_RELEASED (%u ms)\n",
  409. __func__, __LINE__, event.value);
  410. break;
  411. case PS3_SM_EVENT_THERMAL_ALERT:
  412. dev_dbg(&dev->core, "%s:%d: THERMAL_ALERT (zone %u)\n",
  413. __func__, __LINE__, event.value);
  414. pr_info("PS3 Thermal Alert Zone %u\n", event.value);
  415. break;
  416. case PS3_SM_EVENT_THERMAL_CLEARED:
  417. dev_dbg(&dev->core, "%s:%d: THERMAL_CLEARED (zone %u)\n",
  418. __func__, __LINE__, event.value);
  419. break;
  420. default:
  421. dev_dbg(&dev->core, "%s:%d: unknown event (%u)\n",
  422. __func__, __LINE__, event.type);
  423. return -EIO;
  424. }
  425. return 0;
  426. }
  427. /**
  428. * ps3_sys_manager_handle_cmd - Second stage command msg handler.
  429. *
  430. * The system manager sends this in reply to a 'request' message from the guest.
  431. */
  432. static int ps3_sys_manager_handle_cmd(struct ps3_system_bus_device *dev)
  433. {
  434. int result;
  435. struct {
  436. u8 version;
  437. u8 type;
  438. u8 reserved_1[14];
  439. } cmd;
  440. BUILD_BUG_ON(sizeof(cmd) != 16);
  441. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  442. result = ps3_vuart_read(dev, &cmd, sizeof(cmd));
  443. BUG_ON(result && "need to retry here");
  444. if (result)
  445. return result;
  446. if (cmd.version != 1) {
  447. dev_dbg(&dev->core, "%s:%d: unsupported cmd version (%u)\n",
  448. __func__, __LINE__, cmd.version);
  449. return -EIO;
  450. }
  451. if (cmd.type != PS3_SM_CMD_SHUTDOWN) {
  452. dev_dbg(&dev->core, "%s:%d: unknown cmd (%u)\n",
  453. __func__, __LINE__, cmd.type);
  454. return -EIO;
  455. }
  456. ps3_sys_manager_send_response(dev, 0);
  457. return 0;
  458. }
  459. /**
  460. * ps3_sys_manager_handle_msg - First stage msg handler.
  461. *
  462. * Can be called directly to manually poll vuart and pump message handler.
  463. */
  464. static int ps3_sys_manager_handle_msg(struct ps3_system_bus_device *dev)
  465. {
  466. int result;
  467. struct ps3_sys_manager_header header;
  468. result = ps3_vuart_read(dev, &header,
  469. sizeof(struct ps3_sys_manager_header));
  470. if (result)
  471. return result;
  472. if (header.version != 1) {
  473. dev_dbg(&dev->core, "%s:%d: unsupported header version (%u)\n",
  474. __func__, __LINE__, header.version);
  475. dump_sm_header(&header);
  476. goto fail_header;
  477. }
  478. BUILD_BUG_ON(sizeof(header) != 16);
  479. if (header.size != 16 || (header.payload_size != 8
  480. && header.payload_size != 16)) {
  481. dump_sm_header(&header);
  482. BUG();
  483. }
  484. switch (header.service_id) {
  485. case PS3_SM_SERVICE_ID_EXTERN_EVENT:
  486. dev_dbg(&dev->core, "%s:%d: EVENT\n", __func__, __LINE__);
  487. return ps3_sys_manager_handle_event(dev);
  488. case PS3_SM_SERVICE_ID_COMMAND:
  489. dev_dbg(&dev->core, "%s:%d: COMMAND\n", __func__, __LINE__);
  490. return ps3_sys_manager_handle_cmd(dev);
  491. case PS3_SM_SERVICE_ID_REQUEST_ERROR:
  492. dev_dbg(&dev->core, "%s:%d: REQUEST_ERROR\n", __func__,
  493. __LINE__);
  494. dump_sm_header(&header);
  495. break;
  496. default:
  497. dev_dbg(&dev->core, "%s:%d: unknown service_id (%u)\n",
  498. __func__, __LINE__, header.service_id);
  499. break;
  500. }
  501. goto fail_id;
  502. fail_header:
  503. ps3_vuart_clear_rx_bytes(dev, 0);
  504. return -EIO;
  505. fail_id:
  506. ps3_vuart_clear_rx_bytes(dev, header.payload_size);
  507. return -EIO;
  508. }
  509. static void ps3_sys_manager_fin(struct ps3_system_bus_device *dev)
  510. {
  511. ps3_sys_manager_send_request_shutdown(dev);
  512. pr_emerg("System Halted, OK to turn off power\n");
  513. while (ps3_sys_manager_handle_msg(dev)) {
  514. /* pause until next DEC interrupt */
  515. lv1_pause(0);
  516. }
  517. while (1) {
  518. /* pause, ignoring DEC interrupt */
  519. lv1_pause(1);
  520. }
  521. }
  522. /**
  523. * ps3_sys_manager_final_power_off - The final platform machine_power_off 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_power_off() through
  530. * ps3_sys_manager_ops.power_off.
  531. */
  532. static void ps3_sys_manager_final_power_off(struct ps3_system_bus_device *dev)
  533. {
  534. BUG_ON(!dev);
  535. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  536. ps3_vuart_cancel_async(dev);
  537. ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_SHUTDOWN,
  538. user_wake_sources);
  539. ps3_sys_manager_fin(dev);
  540. }
  541. /**
  542. * ps3_sys_manager_final_restart - The final platform machine_restart routine.
  543. *
  544. * This routine never returns. The routine disables asynchronous vuart reads
  545. * then spins calling ps3_sys_manager_handle_msg() to receive and acknowledge
  546. * the shutdown command sent from the system manager. Soon after the
  547. * acknowledgement is sent the lpar is destroyed by the HV. This routine
  548. * should only be called from ps3_restart() through ps3_sys_manager_ops.restart.
  549. */
  550. static void ps3_sys_manager_final_restart(struct ps3_system_bus_device *dev)
  551. {
  552. BUG_ON(!dev);
  553. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  554. /* Check if we got here via a power button event. */
  555. if (ps3_sm_force_power_off) {
  556. dev_dbg(&dev->core, "%s:%d: forcing poweroff\n",
  557. __func__, __LINE__);
  558. ps3_sys_manager_final_power_off(dev);
  559. }
  560. ps3_vuart_cancel_async(dev);
  561. ps3_sys_manager_send_attr(dev, 0);
  562. ps3_sys_manager_send_next_op(dev, PS3_SM_NEXT_OP_SYS_REBOOT,
  563. user_wake_sources);
  564. ps3_sys_manager_fin(dev);
  565. }
  566. /**
  567. * ps3_sys_manager_get_wol - Get wake-on-lan setting.
  568. */
  569. int ps3_sys_manager_get_wol(void)
  570. {
  571. pr_debug("%s:%d\n", __func__, __LINE__);
  572. return (user_wake_sources & PS3_SM_WAKE_W_O_L) != 0;
  573. }
  574. EXPORT_SYMBOL_GPL(ps3_sys_manager_get_wol);
  575. /**
  576. * ps3_sys_manager_set_wol - Set wake-on-lan setting.
  577. */
  578. void ps3_sys_manager_set_wol(int state)
  579. {
  580. static DEFINE_MUTEX(mutex);
  581. mutex_lock(&mutex);
  582. pr_debug("%s:%d: %d\n", __func__, __LINE__, state);
  583. if (state)
  584. user_wake_sources |= PS3_SM_WAKE_W_O_L;
  585. else
  586. user_wake_sources &= ~PS3_SM_WAKE_W_O_L;
  587. mutex_unlock(&mutex);
  588. }
  589. EXPORT_SYMBOL_GPL(ps3_sys_manager_set_wol);
  590. /**
  591. * ps3_sys_manager_work - Asynchronous read handler.
  592. *
  593. * Signaled when PS3_SM_RX_MSG_LEN_MIN bytes arrive at the vuart port.
  594. */
  595. static void ps3_sys_manager_work(struct ps3_system_bus_device *dev)
  596. {
  597. ps3_sys_manager_handle_msg(dev);
  598. ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN);
  599. }
  600. static int ps3_sys_manager_probe(struct ps3_system_bus_device *dev)
  601. {
  602. int result;
  603. struct ps3_sys_manager_ops ops;
  604. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  605. ops.power_off = ps3_sys_manager_final_power_off;
  606. ops.restart = ps3_sys_manager_final_restart;
  607. ops.dev = dev;
  608. /* ps3_sys_manager_register_ops copies ops. */
  609. ps3_sys_manager_register_ops(&ops);
  610. result = ps3_sys_manager_send_attr(dev, PS3_SM_ATTR_ALL);
  611. BUG_ON(result);
  612. result = ps3_vuart_read_async(dev, PS3_SM_RX_MSG_LEN_MIN);
  613. BUG_ON(result);
  614. return result;
  615. }
  616. static int ps3_sys_manager_remove(struct ps3_system_bus_device *dev)
  617. {
  618. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  619. return 0;
  620. }
  621. static void ps3_sys_manager_shutdown(struct ps3_system_bus_device *dev)
  622. {
  623. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  624. }
  625. static struct ps3_vuart_port_driver ps3_sys_manager = {
  626. .core.match_id = PS3_MATCH_ID_SYSTEM_MANAGER,
  627. .core.core.name = "ps3_sys_manager",
  628. .probe = ps3_sys_manager_probe,
  629. .remove = ps3_sys_manager_remove,
  630. .shutdown = ps3_sys_manager_shutdown,
  631. .work = ps3_sys_manager_work,
  632. };
  633. static int __init ps3_sys_manager_init(void)
  634. {
  635. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  636. return -ENODEV;
  637. return ps3_vuart_port_driver_register(&ps3_sys_manager);
  638. }
  639. module_init(ps3_sys_manager_init);
  640. /* Module remove not supported. */
  641. MODULE_AUTHOR("Sony Corporation");
  642. MODULE_LICENSE("GPL v2");
  643. MODULE_DESCRIPTION("PS3 System Manager");
  644. MODULE_ALIAS(PS3_MODULE_ALIAS_SYSTEM_MANAGER);