surface_acpi_notify.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for the Surface ACPI Notify (SAN) interface/shim.
  4. *
  5. * Translates communication from ACPI to Surface System Aggregator Module
  6. * (SSAM/SAM) requests and back, specifically SAM-over-SSH. Translates SSAM
  7. * events back to ACPI notifications. Allows handling of discrete GPU
  8. * notifications sent from ACPI via the SAN interface by providing them to any
  9. * registered external driver.
  10. *
  11. * Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com>
  12. */
  13. #include <linux/unaligned.h>
  14. #include <linux/acpi.h>
  15. #include <linux/delay.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/notifier.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/rwsem.h>
  22. #include <linux/surface_aggregator/controller.h>
  23. #include <linux/surface_acpi_notify.h>
  24. struct san_data {
  25. struct device *dev;
  26. struct ssam_controller *ctrl;
  27. struct acpi_connection_info info;
  28. struct ssam_event_notifier nf_bat;
  29. struct ssam_event_notifier nf_tmp;
  30. };
  31. #define to_san_data(ptr, member) \
  32. container_of(ptr, struct san_data, member)
  33. static struct workqueue_struct *san_wq;
  34. /* -- dGPU notifier interface. ---------------------------------------------- */
  35. struct san_rqsg_if {
  36. struct rw_semaphore lock;
  37. struct device *dev;
  38. struct blocking_notifier_head nh;
  39. };
  40. static struct san_rqsg_if san_rqsg_if = {
  41. .lock = __RWSEM_INITIALIZER(san_rqsg_if.lock),
  42. .dev = NULL,
  43. .nh = BLOCKING_NOTIFIER_INIT(san_rqsg_if.nh),
  44. };
  45. static int san_set_rqsg_interface_device(struct device *dev)
  46. {
  47. int status = 0;
  48. down_write(&san_rqsg_if.lock);
  49. if (!san_rqsg_if.dev && dev)
  50. san_rqsg_if.dev = dev;
  51. else
  52. status = -EBUSY;
  53. up_write(&san_rqsg_if.lock);
  54. return status;
  55. }
  56. /**
  57. * san_client_link() - Link client as consumer to SAN device.
  58. * @client: The client to link.
  59. *
  60. * Sets up a device link between the provided client device as consumer and
  61. * the SAN device as provider. This function can be used to ensure that the
  62. * SAN interface has been set up and will be set up for as long as the driver
  63. * of the client device is bound. This guarantees that, during that time, all
  64. * dGPU events will be received by any registered notifier.
  65. *
  66. * The link will be automatically removed once the client device's driver is
  67. * unbound.
  68. *
  69. * Return: Returns zero on success, %-ENXIO if the SAN interface has not been
  70. * set up yet, and %-ENOMEM if device link creation failed.
  71. */
  72. int san_client_link(struct device *client)
  73. {
  74. const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
  75. struct device_link *link;
  76. down_read(&san_rqsg_if.lock);
  77. if (!san_rqsg_if.dev) {
  78. up_read(&san_rqsg_if.lock);
  79. return -ENXIO;
  80. }
  81. link = device_link_add(client, san_rqsg_if.dev, flags);
  82. if (!link) {
  83. up_read(&san_rqsg_if.lock);
  84. return -ENOMEM;
  85. }
  86. if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND) {
  87. up_read(&san_rqsg_if.lock);
  88. return -ENXIO;
  89. }
  90. up_read(&san_rqsg_if.lock);
  91. return 0;
  92. }
  93. EXPORT_SYMBOL_GPL(san_client_link);
  94. /**
  95. * san_dgpu_notifier_register() - Register a SAN dGPU notifier.
  96. * @nb: The notifier-block to register.
  97. *
  98. * Registers a SAN dGPU notifier, receiving any new SAN dGPU events sent from
  99. * ACPI. The registered notifier will be called with &struct san_dgpu_event
  100. * as notifier data and the command ID of that event as notifier action.
  101. */
  102. int san_dgpu_notifier_register(struct notifier_block *nb)
  103. {
  104. return blocking_notifier_chain_register(&san_rqsg_if.nh, nb);
  105. }
  106. EXPORT_SYMBOL_GPL(san_dgpu_notifier_register);
  107. /**
  108. * san_dgpu_notifier_unregister() - Unregister a SAN dGPU notifier.
  109. * @nb: The notifier-block to unregister.
  110. */
  111. int san_dgpu_notifier_unregister(struct notifier_block *nb)
  112. {
  113. return blocking_notifier_chain_unregister(&san_rqsg_if.nh, nb);
  114. }
  115. EXPORT_SYMBOL_GPL(san_dgpu_notifier_unregister);
  116. static int san_dgpu_notifier_call(struct san_dgpu_event *evt)
  117. {
  118. int ret;
  119. ret = blocking_notifier_call_chain(&san_rqsg_if.nh, evt->command, evt);
  120. return notifier_to_errno(ret);
  121. }
  122. /* -- ACPI _DSM event relay. ------------------------------------------------ */
  123. #define SAN_DSM_REVISION 0
  124. /* 93b666c5-70c6-469f-a215-3d487c91ab3c */
  125. static const guid_t SAN_DSM_UUID =
  126. GUID_INIT(0x93b666c5, 0x70c6, 0x469f, 0xa2, 0x15, 0x3d,
  127. 0x48, 0x7c, 0x91, 0xab, 0x3c);
  128. enum san_dsm_event_fn {
  129. SAN_DSM_EVENT_FN_BAT1_STAT = 0x03,
  130. SAN_DSM_EVENT_FN_BAT1_INFO = 0x04,
  131. SAN_DSM_EVENT_FN_ADP1_STAT = 0x05,
  132. SAN_DSM_EVENT_FN_ADP1_INFO = 0x06,
  133. SAN_DSM_EVENT_FN_BAT2_STAT = 0x07,
  134. SAN_DSM_EVENT_FN_BAT2_INFO = 0x08,
  135. SAN_DSM_EVENT_FN_THERMAL = 0x09,
  136. SAN_DSM_EVENT_FN_DPTF = 0x0a,
  137. };
  138. enum sam_event_cid_bat {
  139. SAM_EVENT_CID_BAT_BIX = 0x15,
  140. SAM_EVENT_CID_BAT_BST = 0x16,
  141. SAM_EVENT_CID_BAT_ADP = 0x17,
  142. SAM_EVENT_CID_BAT_PROT = 0x18,
  143. SAM_EVENT_CID_BAT_DPTF = 0x4f,
  144. };
  145. enum sam_event_cid_tmp {
  146. SAM_EVENT_CID_TMP_TRIP = 0x0b,
  147. };
  148. struct san_event_work {
  149. struct delayed_work work;
  150. struct device *dev;
  151. struct ssam_event event; /* must be last */
  152. };
  153. static int san_acpi_notify_event(struct device *dev, u64 func,
  154. union acpi_object *param)
  155. {
  156. acpi_handle san = ACPI_HANDLE(dev);
  157. union acpi_object *obj;
  158. int status = 0;
  159. if (!acpi_check_dsm(san, &SAN_DSM_UUID, SAN_DSM_REVISION, BIT_ULL(func)))
  160. return 0;
  161. dev_dbg(dev, "notify event %#04llx\n", func);
  162. obj = acpi_evaluate_dsm_typed(san, &SAN_DSM_UUID, SAN_DSM_REVISION,
  163. func, param, ACPI_TYPE_BUFFER);
  164. if (!obj)
  165. return -EFAULT;
  166. if (obj->buffer.length != 1 || obj->buffer.pointer[0] != 0) {
  167. dev_err(dev, "got unexpected result from _DSM\n");
  168. status = -EPROTO;
  169. }
  170. ACPI_FREE(obj);
  171. return status;
  172. }
  173. static int san_evt_bat_adp(struct device *dev, const struct ssam_event *event)
  174. {
  175. int status;
  176. status = san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_ADP1_STAT, NULL);
  177. if (status)
  178. return status;
  179. /*
  180. * Ensure that the battery states get updated correctly. When the
  181. * battery is fully charged and an adapter is plugged in, it sometimes
  182. * is not updated correctly, instead showing it as charging.
  183. * Explicitly trigger battery updates to fix this.
  184. */
  185. status = san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_BAT1_STAT, NULL);
  186. if (status)
  187. return status;
  188. return san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_BAT2_STAT, NULL);
  189. }
  190. static int san_evt_bat_bix(struct device *dev, const struct ssam_event *event)
  191. {
  192. enum san_dsm_event_fn fn;
  193. if (event->instance_id == 0x02)
  194. fn = SAN_DSM_EVENT_FN_BAT2_INFO;
  195. else
  196. fn = SAN_DSM_EVENT_FN_BAT1_INFO;
  197. return san_acpi_notify_event(dev, fn, NULL);
  198. }
  199. static int san_evt_bat_bst(struct device *dev, const struct ssam_event *event)
  200. {
  201. enum san_dsm_event_fn fn;
  202. if (event->instance_id == 0x02)
  203. fn = SAN_DSM_EVENT_FN_BAT2_STAT;
  204. else
  205. fn = SAN_DSM_EVENT_FN_BAT1_STAT;
  206. return san_acpi_notify_event(dev, fn, NULL);
  207. }
  208. static int san_evt_bat_dptf(struct device *dev, const struct ssam_event *event)
  209. {
  210. union acpi_object payload;
  211. /*
  212. * The Surface ACPI expects a buffer and not a package. It specifically
  213. * checks for ObjectType (Arg3) == 0x03. This will cause a warning in
  214. * acpica/nsarguments.c, but that warning can be safely ignored.
  215. */
  216. payload.type = ACPI_TYPE_BUFFER;
  217. payload.buffer.length = event->length;
  218. payload.buffer.pointer = (u8 *)&event->data[0];
  219. return san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_DPTF, &payload);
  220. }
  221. static unsigned long san_evt_bat_delay(u8 cid)
  222. {
  223. switch (cid) {
  224. case SAM_EVENT_CID_BAT_ADP:
  225. /*
  226. * Wait for battery state to update before signaling adapter
  227. * change.
  228. */
  229. return msecs_to_jiffies(5000);
  230. case SAM_EVENT_CID_BAT_BST:
  231. /* Ensure we do not miss anything important due to caching. */
  232. return msecs_to_jiffies(2000);
  233. default:
  234. return 0;
  235. }
  236. }
  237. static bool san_evt_bat(const struct ssam_event *event, struct device *dev)
  238. {
  239. int status;
  240. switch (event->command_id) {
  241. case SAM_EVENT_CID_BAT_BIX:
  242. status = san_evt_bat_bix(dev, event);
  243. break;
  244. case SAM_EVENT_CID_BAT_BST:
  245. status = san_evt_bat_bst(dev, event);
  246. break;
  247. case SAM_EVENT_CID_BAT_ADP:
  248. status = san_evt_bat_adp(dev, event);
  249. break;
  250. case SAM_EVENT_CID_BAT_PROT:
  251. /*
  252. * TODO: Implement support for battery protection status change
  253. * event.
  254. */
  255. return true;
  256. case SAM_EVENT_CID_BAT_DPTF:
  257. status = san_evt_bat_dptf(dev, event);
  258. break;
  259. default:
  260. return false;
  261. }
  262. if (status) {
  263. dev_err(dev, "error handling power event (cid = %#04x)\n",
  264. event->command_id);
  265. }
  266. return true;
  267. }
  268. static void san_evt_bat_workfn(struct work_struct *work)
  269. {
  270. struct san_event_work *ev;
  271. ev = container_of(work, struct san_event_work, work.work);
  272. san_evt_bat(&ev->event, ev->dev);
  273. kfree(ev);
  274. }
  275. static u32 san_evt_bat_nf(struct ssam_event_notifier *nf,
  276. const struct ssam_event *event)
  277. {
  278. struct san_data *d = to_san_data(nf, nf_bat);
  279. struct san_event_work *work;
  280. unsigned long delay = san_evt_bat_delay(event->command_id);
  281. if (delay == 0)
  282. return san_evt_bat(event, d->dev) ? SSAM_NOTIF_HANDLED : 0;
  283. work = kzalloc(sizeof(*work) + event->length, GFP_KERNEL);
  284. if (!work)
  285. return ssam_notifier_from_errno(-ENOMEM);
  286. INIT_DELAYED_WORK(&work->work, san_evt_bat_workfn);
  287. work->dev = d->dev;
  288. work->event = *event;
  289. memcpy(work->event.data, event->data, event->length);
  290. queue_delayed_work(san_wq, &work->work, delay);
  291. return SSAM_NOTIF_HANDLED;
  292. }
  293. static int san_evt_tmp_trip(struct device *dev, const struct ssam_event *event)
  294. {
  295. union acpi_object param;
  296. /*
  297. * The Surface ACPI expects an integer and not a package. This will
  298. * cause a warning in acpica/nsarguments.c, but that warning can be
  299. * safely ignored.
  300. */
  301. param.type = ACPI_TYPE_INTEGER;
  302. param.integer.value = event->instance_id;
  303. return san_acpi_notify_event(dev, SAN_DSM_EVENT_FN_THERMAL, &param);
  304. }
  305. static bool san_evt_tmp(const struct ssam_event *event, struct device *dev)
  306. {
  307. int status;
  308. switch (event->command_id) {
  309. case SAM_EVENT_CID_TMP_TRIP:
  310. status = san_evt_tmp_trip(dev, event);
  311. break;
  312. default:
  313. return false;
  314. }
  315. if (status) {
  316. dev_err(dev, "error handling thermal event (cid = %#04x)\n",
  317. event->command_id);
  318. }
  319. return true;
  320. }
  321. static u32 san_evt_tmp_nf(struct ssam_event_notifier *nf,
  322. const struct ssam_event *event)
  323. {
  324. struct san_data *d = to_san_data(nf, nf_tmp);
  325. return san_evt_tmp(event, d->dev) ? SSAM_NOTIF_HANDLED : 0;
  326. }
  327. /* -- ACPI GSB OperationRegion handler -------------------------------------- */
  328. struct gsb_data_in {
  329. u8 cv;
  330. } __packed;
  331. struct gsb_data_rqsx {
  332. u8 cv; /* Command value (san_gsb_request_cv). */
  333. u8 tc; /* Target category. */
  334. u8 tid; /* Target ID. */
  335. u8 iid; /* Instance ID. */
  336. u8 snc; /* Expect-response-flag. */
  337. u8 cid; /* Command ID. */
  338. u16 cdl; /* Payload length. */
  339. u8 pld[]; /* Payload. */
  340. } __packed;
  341. struct gsb_data_etwl {
  342. u8 cv; /* Command value (should be 0x02). */
  343. u8 etw3; /* Unknown. */
  344. u8 etw4; /* Unknown. */
  345. u8 msg[]; /* Error message (ASCIIZ). */
  346. } __packed;
  347. struct gsb_data_out {
  348. u8 status; /* _SSH communication status. */
  349. u8 len; /* _SSH payload length. */
  350. u8 pld[]; /* _SSH payload. */
  351. } __packed;
  352. union gsb_buffer_data {
  353. struct gsb_data_in in; /* Common input. */
  354. struct gsb_data_rqsx rqsx; /* RQSX input. */
  355. struct gsb_data_etwl etwl; /* ETWL input. */
  356. struct gsb_data_out out; /* Output. */
  357. };
  358. struct gsb_buffer {
  359. u8 status; /* GSB AttribRawProcess status. */
  360. u8 len; /* GSB AttribRawProcess length. */
  361. union gsb_buffer_data data;
  362. } __packed;
  363. #define SAN_GSB_MAX_RQSX_PAYLOAD (U8_MAX - 2 - sizeof(struct gsb_data_rqsx))
  364. #define SAN_GSB_MAX_RESPONSE (U8_MAX - 2 - sizeof(struct gsb_data_out))
  365. #define SAN_GSB_COMMAND 0
  366. enum san_gsb_request_cv {
  367. SAN_GSB_REQUEST_CV_RQST = 0x01,
  368. SAN_GSB_REQUEST_CV_ETWL = 0x02,
  369. SAN_GSB_REQUEST_CV_RQSG = 0x03,
  370. };
  371. #define SAN_REQUEST_NUM_TRIES 5
  372. static acpi_status san_etwl(struct san_data *d, struct gsb_buffer *b)
  373. {
  374. struct gsb_data_etwl *etwl = &b->data.etwl;
  375. if (b->len < sizeof(struct gsb_data_etwl)) {
  376. dev_err(d->dev, "invalid ETWL package (len = %d)\n", b->len);
  377. return AE_OK;
  378. }
  379. dev_err(d->dev, "ETWL(%#04x, %#04x): %.*s\n", etwl->etw3, etwl->etw4,
  380. (unsigned int)(b->len - sizeof(struct gsb_data_etwl)),
  381. (char *)etwl->msg);
  382. /* Indicate success. */
  383. b->status = 0x00;
  384. b->len = 0x00;
  385. return AE_OK;
  386. }
  387. static
  388. struct gsb_data_rqsx *san_validate_rqsx(struct device *dev, const char *type,
  389. struct gsb_buffer *b)
  390. {
  391. struct gsb_data_rqsx *rqsx = &b->data.rqsx;
  392. if (b->len < sizeof(struct gsb_data_rqsx)) {
  393. dev_err(dev, "invalid %s package (len = %d)\n", type, b->len);
  394. return NULL;
  395. }
  396. if (get_unaligned(&rqsx->cdl) != b->len - sizeof(struct gsb_data_rqsx)) {
  397. dev_err(dev, "bogus %s package (len = %d, cdl = %d)\n",
  398. type, b->len, get_unaligned(&rqsx->cdl));
  399. return NULL;
  400. }
  401. if (get_unaligned(&rqsx->cdl) > SAN_GSB_MAX_RQSX_PAYLOAD) {
  402. dev_err(dev, "payload for %s package too large (cdl = %d)\n",
  403. type, get_unaligned(&rqsx->cdl));
  404. return NULL;
  405. }
  406. return rqsx;
  407. }
  408. static void gsb_rqsx_response_error(struct gsb_buffer *gsb, int status)
  409. {
  410. gsb->status = 0x00;
  411. gsb->len = 0x02;
  412. gsb->data.out.status = (u8)(-status);
  413. gsb->data.out.len = 0x00;
  414. }
  415. static void gsb_rqsx_response_success(struct gsb_buffer *gsb, u8 *ptr, size_t len)
  416. {
  417. gsb->status = 0x00;
  418. gsb->len = len + 2;
  419. gsb->data.out.status = 0x00;
  420. gsb->data.out.len = len;
  421. if (len)
  422. memcpy(&gsb->data.out.pld[0], ptr, len);
  423. }
  424. static acpi_status san_rqst_fixup_suspended(struct san_data *d,
  425. struct ssam_request *rqst,
  426. struct gsb_buffer *gsb)
  427. {
  428. if (rqst->target_category == SSAM_SSH_TC_BAS && rqst->command_id == 0x0D) {
  429. u8 base_state = 1;
  430. /* Base state quirk:
  431. * The base state may be queried from ACPI when the EC is still
  432. * suspended. In this case it will return '-EPERM'. This query
  433. * will only be triggered from the ACPI lid GPE interrupt, thus
  434. * we are either in laptop or studio mode (base status 0x01 or
  435. * 0x02). Furthermore, we will only get here if the device (and
  436. * EC) have been suspended.
  437. *
  438. * We now assume that the device is in laptop mode (0x01). This
  439. * has the drawback that it will wake the device when unfolding
  440. * it in studio mode, but it also allows us to avoid actively
  441. * waiting for the EC to wake up, which may incur a notable
  442. * delay.
  443. */
  444. dev_dbg(d->dev, "rqst: fixup: base-state quirk\n");
  445. gsb_rqsx_response_success(gsb, &base_state, sizeof(base_state));
  446. return AE_OK;
  447. }
  448. gsb_rqsx_response_error(gsb, -ENXIO);
  449. return AE_OK;
  450. }
  451. static acpi_status san_rqst(struct san_data *d, struct gsb_buffer *buffer)
  452. {
  453. u8 rspbuf[SAN_GSB_MAX_RESPONSE];
  454. struct gsb_data_rqsx *gsb_rqst;
  455. struct ssam_request rqst;
  456. struct ssam_response rsp;
  457. int status = 0;
  458. gsb_rqst = san_validate_rqsx(d->dev, "RQST", buffer);
  459. if (!gsb_rqst)
  460. return AE_OK;
  461. rqst.target_category = gsb_rqst->tc;
  462. rqst.target_id = gsb_rqst->tid;
  463. rqst.command_id = gsb_rqst->cid;
  464. rqst.instance_id = gsb_rqst->iid;
  465. rqst.flags = gsb_rqst->snc ? SSAM_REQUEST_HAS_RESPONSE : 0;
  466. rqst.length = get_unaligned(&gsb_rqst->cdl);
  467. rqst.payload = &gsb_rqst->pld[0];
  468. rsp.capacity = ARRAY_SIZE(rspbuf);
  469. rsp.length = 0;
  470. rsp.pointer = &rspbuf[0];
  471. /* Handle suspended device. */
  472. if (d->dev->power.is_suspended) {
  473. dev_warn(d->dev, "rqst: device is suspended, not executing\n");
  474. return san_rqst_fixup_suspended(d, &rqst, buffer);
  475. }
  476. status = __ssam_retry(ssam_request_do_sync_onstack, SAN_REQUEST_NUM_TRIES,
  477. d->ctrl, &rqst, &rsp, SAN_GSB_MAX_RQSX_PAYLOAD);
  478. if (!status) {
  479. gsb_rqsx_response_success(buffer, rsp.pointer, rsp.length);
  480. } else {
  481. dev_err(d->dev, "rqst: failed with error %d\n", status);
  482. gsb_rqsx_response_error(buffer, status);
  483. }
  484. return AE_OK;
  485. }
  486. static acpi_status san_rqsg(struct san_data *d, struct gsb_buffer *buffer)
  487. {
  488. struct gsb_data_rqsx *gsb_rqsg;
  489. struct san_dgpu_event evt;
  490. int status;
  491. gsb_rqsg = san_validate_rqsx(d->dev, "RQSG", buffer);
  492. if (!gsb_rqsg)
  493. return AE_OK;
  494. evt.category = gsb_rqsg->tc;
  495. evt.target = gsb_rqsg->tid;
  496. evt.command = gsb_rqsg->cid;
  497. evt.instance = gsb_rqsg->iid;
  498. evt.length = get_unaligned(&gsb_rqsg->cdl);
  499. evt.payload = &gsb_rqsg->pld[0];
  500. status = san_dgpu_notifier_call(&evt);
  501. if (!status) {
  502. gsb_rqsx_response_success(buffer, NULL, 0);
  503. } else {
  504. dev_err(d->dev, "rqsg: failed with error %d\n", status);
  505. gsb_rqsx_response_error(buffer, status);
  506. }
  507. return AE_OK;
  508. }
  509. static acpi_status san_opreg_handler(u32 function, acpi_physical_address command,
  510. u32 bits, u64 *value64, void *opreg_context,
  511. void *region_context)
  512. {
  513. struct san_data *d = to_san_data(opreg_context, info);
  514. struct gsb_buffer *buffer = (struct gsb_buffer *)value64;
  515. int accessor_type = (function & 0xFFFF0000) >> 16;
  516. if (command != SAN_GSB_COMMAND) {
  517. dev_warn(d->dev, "unsupported command: %#04llx\n", command);
  518. return AE_OK;
  519. }
  520. if (accessor_type != ACPI_GSB_ACCESS_ATTRIB_RAW_PROCESS) {
  521. dev_err(d->dev, "invalid access type: %#04x\n", accessor_type);
  522. return AE_OK;
  523. }
  524. /* Buffer must have at least contain the command-value. */
  525. if (buffer->len == 0) {
  526. dev_err(d->dev, "request-package too small\n");
  527. return AE_OK;
  528. }
  529. switch (buffer->data.in.cv) {
  530. case SAN_GSB_REQUEST_CV_RQST:
  531. return san_rqst(d, buffer);
  532. case SAN_GSB_REQUEST_CV_ETWL:
  533. return san_etwl(d, buffer);
  534. case SAN_GSB_REQUEST_CV_RQSG:
  535. return san_rqsg(d, buffer);
  536. default:
  537. dev_warn(d->dev, "unsupported SAN0 request (cv: %#04x)\n",
  538. buffer->data.in.cv);
  539. return AE_OK;
  540. }
  541. }
  542. /* -- Driver setup. --------------------------------------------------------- */
  543. static int san_events_register(struct platform_device *pdev)
  544. {
  545. struct san_data *d = platform_get_drvdata(pdev);
  546. int status;
  547. d->nf_bat.base.priority = 1;
  548. d->nf_bat.base.fn = san_evt_bat_nf;
  549. d->nf_bat.event.reg = SSAM_EVENT_REGISTRY_SAM;
  550. d->nf_bat.event.id.target_category = SSAM_SSH_TC_BAT;
  551. d->nf_bat.event.id.instance = 0;
  552. d->nf_bat.event.mask = SSAM_EVENT_MASK_TARGET;
  553. d->nf_bat.event.flags = SSAM_EVENT_SEQUENCED;
  554. d->nf_tmp.base.priority = 1;
  555. d->nf_tmp.base.fn = san_evt_tmp_nf;
  556. d->nf_tmp.event.reg = SSAM_EVENT_REGISTRY_SAM;
  557. d->nf_tmp.event.id.target_category = SSAM_SSH_TC_TMP;
  558. d->nf_tmp.event.id.instance = 0;
  559. d->nf_tmp.event.mask = SSAM_EVENT_MASK_TARGET;
  560. d->nf_tmp.event.flags = SSAM_EVENT_SEQUENCED;
  561. status = ssam_notifier_register(d->ctrl, &d->nf_bat);
  562. if (status)
  563. return status;
  564. status = ssam_notifier_register(d->ctrl, &d->nf_tmp);
  565. if (status)
  566. ssam_notifier_unregister(d->ctrl, &d->nf_bat);
  567. return status;
  568. }
  569. static void san_events_unregister(struct platform_device *pdev)
  570. {
  571. struct san_data *d = platform_get_drvdata(pdev);
  572. ssam_notifier_unregister(d->ctrl, &d->nf_bat);
  573. ssam_notifier_unregister(d->ctrl, &d->nf_tmp);
  574. }
  575. #define san_consumer_printk(level, dev, handle, fmt, ...) \
  576. do { \
  577. char *path = "<error getting consumer path>"; \
  578. struct acpi_buffer buffer = { \
  579. .length = ACPI_ALLOCATE_BUFFER, \
  580. .pointer = NULL, \
  581. }; \
  582. \
  583. if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer))) \
  584. path = buffer.pointer; \
  585. \
  586. dev_##level(dev, "[%s]: " fmt, path, ##__VA_ARGS__); \
  587. kfree(buffer.pointer); \
  588. } while (0)
  589. #define san_consumer_dbg(dev, handle, fmt, ...) \
  590. san_consumer_printk(dbg, dev, handle, fmt, ##__VA_ARGS__)
  591. #define san_consumer_warn(dev, handle, fmt, ...) \
  592. san_consumer_printk(warn, dev, handle, fmt, ##__VA_ARGS__)
  593. static acpi_status san_consumer_setup(acpi_handle handle, u32 lvl,
  594. void *context, void **rv)
  595. {
  596. const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER;
  597. struct platform_device *pdev = context;
  598. struct acpi_device *adev;
  599. struct device_link *link;
  600. if (!acpi_device_dep(handle, ACPI_HANDLE(&pdev->dev)))
  601. return AE_OK;
  602. /* Ignore ACPI devices that are not present. */
  603. adev = acpi_fetch_acpi_dev(handle);
  604. if (!adev)
  605. return AE_OK;
  606. san_consumer_dbg(&pdev->dev, handle, "creating device link\n");
  607. /* Try to set up device links, ignore but log errors. */
  608. link = device_link_add(&adev->dev, &pdev->dev, flags);
  609. if (!link) {
  610. san_consumer_warn(&pdev->dev, handle, "failed to create device link\n");
  611. return AE_OK;
  612. }
  613. return AE_OK;
  614. }
  615. static int san_consumer_links_setup(struct platform_device *pdev)
  616. {
  617. acpi_status status;
  618. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  619. ACPI_UINT32_MAX, san_consumer_setup, NULL,
  620. pdev, NULL);
  621. return status ? -EFAULT : 0;
  622. }
  623. static int san_probe(struct platform_device *pdev)
  624. {
  625. struct acpi_device *san = ACPI_COMPANION(&pdev->dev);
  626. struct ssam_controller *ctrl;
  627. struct san_data *data;
  628. acpi_status astatus;
  629. int status;
  630. ctrl = ssam_client_bind(&pdev->dev);
  631. if (IS_ERR(ctrl))
  632. return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
  633. status = san_consumer_links_setup(pdev);
  634. if (status)
  635. return status;
  636. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  637. if (!data)
  638. return -ENOMEM;
  639. data->dev = &pdev->dev;
  640. data->ctrl = ctrl;
  641. platform_set_drvdata(pdev, data);
  642. astatus = acpi_install_address_space_handler(san->handle,
  643. ACPI_ADR_SPACE_GSBUS,
  644. &san_opreg_handler, NULL,
  645. &data->info);
  646. if (ACPI_FAILURE(astatus))
  647. return -ENXIO;
  648. status = san_events_register(pdev);
  649. if (status)
  650. goto err_enable_events;
  651. status = san_set_rqsg_interface_device(&pdev->dev);
  652. if (status)
  653. goto err_install_dev;
  654. acpi_dev_clear_dependencies(san);
  655. return 0;
  656. err_install_dev:
  657. san_events_unregister(pdev);
  658. err_enable_events:
  659. acpi_remove_address_space_handler(san, ACPI_ADR_SPACE_GSBUS,
  660. &san_opreg_handler);
  661. return status;
  662. }
  663. static void san_remove(struct platform_device *pdev)
  664. {
  665. acpi_handle san = ACPI_HANDLE(&pdev->dev);
  666. san_set_rqsg_interface_device(NULL);
  667. acpi_remove_address_space_handler(san, ACPI_ADR_SPACE_GSBUS,
  668. &san_opreg_handler);
  669. san_events_unregister(pdev);
  670. /*
  671. * We have unregistered our event sources. Now we need to ensure that
  672. * all delayed works they may have spawned are run to completion.
  673. */
  674. flush_workqueue(san_wq);
  675. }
  676. static const struct acpi_device_id san_match[] = {
  677. { "MSHW0091" },
  678. { },
  679. };
  680. MODULE_DEVICE_TABLE(acpi, san_match);
  681. static struct platform_driver surface_acpi_notify = {
  682. .probe = san_probe,
  683. .remove = san_remove,
  684. .driver = {
  685. .name = "surface_acpi_notify",
  686. .acpi_match_table = san_match,
  687. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  688. },
  689. };
  690. static int __init san_init(void)
  691. {
  692. int ret;
  693. san_wq = alloc_workqueue("san_wq", WQ_PERCPU, 0);
  694. if (!san_wq)
  695. return -ENOMEM;
  696. ret = platform_driver_register(&surface_acpi_notify);
  697. if (ret)
  698. destroy_workqueue(san_wq);
  699. return ret;
  700. }
  701. module_init(san_init);
  702. static void __exit san_exit(void)
  703. {
  704. platform_driver_unregister(&surface_acpi_notify);
  705. destroy_workqueue(san_wq);
  706. }
  707. module_exit(san_exit);
  708. MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  709. MODULE_DESCRIPTION("Surface ACPI Notify driver for Surface System Aggregator Module");
  710. MODULE_LICENSE("GPL");