stratix10-rsu.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018-2019, Intel Corporation
  4. */
  5. #include <linux/arm-smccc.h>
  6. #include <linux/bitfield.h>
  7. #include <linux/completion.h>
  8. #include <linux/kobject.h>
  9. #include <linux/module.h>
  10. #include <linux/mutex.h>
  11. #include <linux/of.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/firmware/intel/stratix10-svc-client.h>
  15. #include <linux/string.h>
  16. #include <linux/sysfs.h>
  17. #define RSU_STATE_MASK GENMASK_ULL(31, 0)
  18. #define RSU_VERSION_MASK GENMASK_ULL(63, 32)
  19. #define RSU_ERROR_LOCATION_MASK GENMASK_ULL(31, 0)
  20. #define RSU_ERROR_DETAIL_MASK GENMASK_ULL(63, 32)
  21. #define RSU_DCMF0_MASK GENMASK_ULL(31, 0)
  22. #define RSU_DCMF1_MASK GENMASK_ULL(63, 32)
  23. #define RSU_DCMF2_MASK GENMASK_ULL(31, 0)
  24. #define RSU_DCMF3_MASK GENMASK_ULL(63, 32)
  25. #define RSU_TIMEOUT (msecs_to_jiffies(SVC_RSU_REQUEST_TIMEOUT_MS))
  26. #define INVALID_RETRY_COUNTER 0xFF
  27. #define INVALID_DCMF_VERSION 0xFF
  28. typedef void (*rsu_callback)(struct stratix10_svc_client *client,
  29. struct stratix10_svc_cb_data *data);
  30. /**
  31. * struct stratix10_rsu_priv - rsu data structure
  32. * @chan: pointer to the allocated service channel
  33. * @client: active service client
  34. * @completion: state for callback completion
  35. * @lock: a mutex to protect callback completion state
  36. * @status.current_image: address of image currently running in flash
  37. * @status.fail_image: address of failed image in flash
  38. * @status.version: the interface version number of RSU firmware
  39. * @status.state: the state of RSU system
  40. * @status.error_details: error code
  41. * @status.error_location: the error offset inside the image that failed
  42. * @dcmf_version.dcmf0: Quartus dcmf0 version
  43. * @dcmf_version.dcmf1: Quartus dcmf1 version
  44. * @dcmf_version.dcmf2: Quartus dcmf2 version
  45. * @dcmf_version.dcmf3: Quartus dcmf3 version
  46. * @retry_counter: the current image's retry counter
  47. * @max_retry: the preset max retry value
  48. */
  49. struct stratix10_rsu_priv {
  50. struct stratix10_svc_chan *chan;
  51. struct stratix10_svc_client client;
  52. struct completion completion;
  53. struct mutex lock;
  54. struct {
  55. unsigned long current_image;
  56. unsigned long fail_image;
  57. unsigned int version;
  58. unsigned int state;
  59. unsigned int error_details;
  60. unsigned int error_location;
  61. } status;
  62. struct {
  63. unsigned int dcmf0;
  64. unsigned int dcmf1;
  65. unsigned int dcmf2;
  66. unsigned int dcmf3;
  67. } dcmf_version;
  68. unsigned int retry_counter;
  69. unsigned int max_retry;
  70. };
  71. /**
  72. * rsu_status_callback() - Status callback from Intel Service Layer
  73. * @client: pointer to service client
  74. * @data: pointer to callback data structure
  75. *
  76. * Callback from Intel service layer for RSU status request. Status is
  77. * only updated after a system reboot, so a get updated status call is
  78. * made during driver probe.
  79. */
  80. static void rsu_status_callback(struct stratix10_svc_client *client,
  81. struct stratix10_svc_cb_data *data)
  82. {
  83. struct stratix10_rsu_priv *priv = client->priv;
  84. struct arm_smccc_res *res = (struct arm_smccc_res *)data->kaddr1;
  85. if (data->status == BIT(SVC_STATUS_OK)) {
  86. priv->status.version = FIELD_GET(RSU_VERSION_MASK,
  87. res->a2);
  88. priv->status.state = FIELD_GET(RSU_STATE_MASK, res->a2);
  89. priv->status.fail_image = res->a1;
  90. priv->status.current_image = res->a0;
  91. priv->status.error_location =
  92. FIELD_GET(RSU_ERROR_LOCATION_MASK, res->a3);
  93. priv->status.error_details =
  94. FIELD_GET(RSU_ERROR_DETAIL_MASK, res->a3);
  95. } else {
  96. dev_err(client->dev, "COMMAND_RSU_STATUS returned 0x%lX\n",
  97. res->a0);
  98. priv->status.version = 0;
  99. priv->status.state = 0;
  100. priv->status.fail_image = 0;
  101. priv->status.current_image = 0;
  102. priv->status.error_location = 0;
  103. priv->status.error_details = 0;
  104. }
  105. complete(&priv->completion);
  106. }
  107. /**
  108. * rsu_command_callback() - Update callback from Intel Service Layer
  109. * @client: pointer to client
  110. * @data: pointer to callback data structure
  111. *
  112. * Callback from Intel service layer for RSU commands.
  113. */
  114. static void rsu_command_callback(struct stratix10_svc_client *client,
  115. struct stratix10_svc_cb_data *data)
  116. {
  117. struct stratix10_rsu_priv *priv = client->priv;
  118. if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
  119. dev_warn(client->dev, "FW doesn't support notify\n");
  120. else if (data->status == BIT(SVC_STATUS_ERROR))
  121. dev_err(client->dev, "Failure, returned status is %lu\n",
  122. BIT(data->status));
  123. complete(&priv->completion);
  124. }
  125. /**
  126. * rsu_retry_callback() - Callback from Intel service layer for getting
  127. * the current image's retry counter from the firmware
  128. * @client: pointer to client
  129. * @data: pointer to callback data structure
  130. *
  131. * Callback from Intel service layer for retry counter, which is used by
  132. * user to know how many times the images is still allowed to reload
  133. * itself before giving up and starting RSU fail-over flow.
  134. */
  135. static void rsu_retry_callback(struct stratix10_svc_client *client,
  136. struct stratix10_svc_cb_data *data)
  137. {
  138. struct stratix10_rsu_priv *priv = client->priv;
  139. unsigned int *counter = (unsigned int *)data->kaddr1;
  140. if (data->status == BIT(SVC_STATUS_OK))
  141. priv->retry_counter = *counter;
  142. else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
  143. dev_warn(client->dev, "FW doesn't support retry\n");
  144. else
  145. dev_err(client->dev, "Failed to get retry counter %lu\n",
  146. BIT(data->status));
  147. complete(&priv->completion);
  148. }
  149. /**
  150. * rsu_max_retry_callback() - Callback from Intel service layer for getting
  151. * the max retry value from the firmware
  152. * @client: pointer to client
  153. * @data: pointer to callback data structure
  154. *
  155. * Callback from Intel service layer for max retry.
  156. */
  157. static void rsu_max_retry_callback(struct stratix10_svc_client *client,
  158. struct stratix10_svc_cb_data *data)
  159. {
  160. struct stratix10_rsu_priv *priv = client->priv;
  161. unsigned int *max_retry = (unsigned int *)data->kaddr1;
  162. if (data->status == BIT(SVC_STATUS_OK))
  163. priv->max_retry = *max_retry;
  164. else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
  165. dev_warn(client->dev, "FW doesn't support max retry\n");
  166. else
  167. dev_err(client->dev, "Failed to get max retry %lu\n",
  168. BIT(data->status));
  169. complete(&priv->completion);
  170. }
  171. /**
  172. * rsu_dcmf_version_callback() - Callback from Intel service layer for getting
  173. * the DCMF version
  174. * @client: pointer to client
  175. * @data: pointer to callback data structure
  176. *
  177. * Callback from Intel service layer for DCMF version number
  178. */
  179. static void rsu_dcmf_version_callback(struct stratix10_svc_client *client,
  180. struct stratix10_svc_cb_data *data)
  181. {
  182. struct stratix10_rsu_priv *priv = client->priv;
  183. unsigned long long *value1 = (unsigned long long *)data->kaddr1;
  184. unsigned long long *value2 = (unsigned long long *)data->kaddr2;
  185. if (data->status == BIT(SVC_STATUS_OK)) {
  186. priv->dcmf_version.dcmf0 = FIELD_GET(RSU_DCMF0_MASK, *value1);
  187. priv->dcmf_version.dcmf1 = FIELD_GET(RSU_DCMF1_MASK, *value1);
  188. priv->dcmf_version.dcmf2 = FIELD_GET(RSU_DCMF2_MASK, *value2);
  189. priv->dcmf_version.dcmf3 = FIELD_GET(RSU_DCMF3_MASK, *value2);
  190. } else
  191. dev_err(client->dev, "failed to get DCMF version\n");
  192. complete(&priv->completion);
  193. }
  194. /**
  195. * rsu_send_msg() - send a message to Intel service layer
  196. * @priv: pointer to rsu private data
  197. * @command: RSU status or update command
  198. * @arg: the request argument, the bitstream address or notify status
  199. * @callback: function pointer for the callback (status or update)
  200. *
  201. * Start an Intel service layer transaction to perform the SMC call that
  202. * is necessary to get RSU boot log or set the address of bitstream to
  203. * boot after reboot.
  204. *
  205. * Returns 0 on success or -ETIMEDOUT on error.
  206. */
  207. static int rsu_send_msg(struct stratix10_rsu_priv *priv,
  208. enum stratix10_svc_command_code command,
  209. unsigned long arg,
  210. rsu_callback callback)
  211. {
  212. struct stratix10_svc_client_msg msg;
  213. int ret;
  214. mutex_lock(&priv->lock);
  215. reinit_completion(&priv->completion);
  216. priv->client.receive_cb = callback;
  217. msg.command = command;
  218. if (arg)
  219. msg.arg[0] = arg;
  220. ret = stratix10_svc_send(priv->chan, &msg);
  221. if (ret < 0)
  222. goto status_done;
  223. ret = wait_for_completion_interruptible_timeout(&priv->completion,
  224. RSU_TIMEOUT);
  225. if (!ret) {
  226. dev_err(priv->client.dev,
  227. "timeout waiting for SMC call\n");
  228. ret = -ETIMEDOUT;
  229. goto status_done;
  230. } else if (ret < 0) {
  231. dev_err(priv->client.dev,
  232. "error %d waiting for SMC call\n", ret);
  233. goto status_done;
  234. } else {
  235. ret = 0;
  236. }
  237. status_done:
  238. stratix10_svc_done(priv->chan);
  239. mutex_unlock(&priv->lock);
  240. return ret;
  241. }
  242. /*
  243. * This driver exposes some optional features of the Intel Stratix 10 SoC FPGA.
  244. * The sysfs interfaces exposed here are FPGA Remote System Update (RSU)
  245. * related. They allow user space software to query the configuration system
  246. * status and to request optional reboot behavior specific to Intel FPGAs.
  247. */
  248. static ssize_t current_image_show(struct device *dev,
  249. struct device_attribute *attr, char *buf)
  250. {
  251. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  252. if (!priv)
  253. return -ENODEV;
  254. return sprintf(buf, "0x%08lx\n", priv->status.current_image);
  255. }
  256. static ssize_t fail_image_show(struct device *dev,
  257. struct device_attribute *attr, char *buf)
  258. {
  259. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  260. if (!priv)
  261. return -ENODEV;
  262. return sprintf(buf, "0x%08lx\n", priv->status.fail_image);
  263. }
  264. static ssize_t version_show(struct device *dev, struct device_attribute *attr,
  265. char *buf)
  266. {
  267. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  268. if (!priv)
  269. return -ENODEV;
  270. return sprintf(buf, "0x%08x\n", priv->status.version);
  271. }
  272. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  273. char *buf)
  274. {
  275. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  276. if (!priv)
  277. return -ENODEV;
  278. return sprintf(buf, "0x%08x\n", priv->status.state);
  279. }
  280. static ssize_t error_location_show(struct device *dev,
  281. struct device_attribute *attr, char *buf)
  282. {
  283. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  284. if (!priv)
  285. return -ENODEV;
  286. return sprintf(buf, "0x%08x\n", priv->status.error_location);
  287. }
  288. static ssize_t error_details_show(struct device *dev,
  289. struct device_attribute *attr, char *buf)
  290. {
  291. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  292. if (!priv)
  293. return -ENODEV;
  294. return sprintf(buf, "0x%08x\n", priv->status.error_details);
  295. }
  296. static ssize_t retry_counter_show(struct device *dev,
  297. struct device_attribute *attr, char *buf)
  298. {
  299. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  300. if (!priv)
  301. return -ENODEV;
  302. return sprintf(buf, "0x%08x\n", priv->retry_counter);
  303. }
  304. static ssize_t max_retry_show(struct device *dev,
  305. struct device_attribute *attr, char *buf)
  306. {
  307. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  308. if (!priv)
  309. return -ENODEV;
  310. return sprintf(buf, "0x%08x\n", priv->max_retry);
  311. }
  312. static ssize_t dcmf0_show(struct device *dev,
  313. struct device_attribute *attr, char *buf)
  314. {
  315. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  316. if (!priv)
  317. return -ENODEV;
  318. return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf0);
  319. }
  320. static ssize_t dcmf1_show(struct device *dev,
  321. struct device_attribute *attr, char *buf)
  322. {
  323. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  324. if (!priv)
  325. return -ENODEV;
  326. return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf1);
  327. }
  328. static ssize_t dcmf2_show(struct device *dev,
  329. struct device_attribute *attr, char *buf)
  330. {
  331. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  332. if (!priv)
  333. return -ENODEV;
  334. return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf2);
  335. }
  336. static ssize_t dcmf3_show(struct device *dev,
  337. struct device_attribute *attr, char *buf)
  338. {
  339. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  340. if (!priv)
  341. return -ENODEV;
  342. return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf3);
  343. }
  344. static ssize_t reboot_image_store(struct device *dev,
  345. struct device_attribute *attr,
  346. const char *buf, size_t count)
  347. {
  348. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  349. unsigned long address;
  350. int ret;
  351. if (!priv)
  352. return -ENODEV;
  353. ret = kstrtoul(buf, 0, &address);
  354. if (ret)
  355. return ret;
  356. ret = rsu_send_msg(priv, COMMAND_RSU_UPDATE,
  357. address, rsu_command_callback);
  358. if (ret) {
  359. dev_err(dev, "Error, RSU update returned %i\n", ret);
  360. return ret;
  361. }
  362. return count;
  363. }
  364. static ssize_t notify_store(struct device *dev,
  365. struct device_attribute *attr,
  366. const char *buf, size_t count)
  367. {
  368. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  369. unsigned long status;
  370. int ret;
  371. if (!priv)
  372. return -ENODEV;
  373. ret = kstrtoul(buf, 0, &status);
  374. if (ret)
  375. return ret;
  376. ret = rsu_send_msg(priv, COMMAND_RSU_NOTIFY,
  377. status, rsu_command_callback);
  378. if (ret) {
  379. dev_err(dev, "Error, RSU notify returned %i\n", ret);
  380. return ret;
  381. }
  382. /* to get the updated state */
  383. ret = rsu_send_msg(priv, COMMAND_RSU_STATUS,
  384. 0, rsu_status_callback);
  385. if (ret) {
  386. dev_err(dev, "Error, getting RSU status %i\n", ret);
  387. return ret;
  388. }
  389. ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, rsu_retry_callback);
  390. if (ret) {
  391. dev_err(dev, "Error, getting RSU retry %i\n", ret);
  392. return ret;
  393. }
  394. return count;
  395. }
  396. static DEVICE_ATTR_RO(current_image);
  397. static DEVICE_ATTR_RO(fail_image);
  398. static DEVICE_ATTR_RO(state);
  399. static DEVICE_ATTR_RO(version);
  400. static DEVICE_ATTR_RO(error_location);
  401. static DEVICE_ATTR_RO(error_details);
  402. static DEVICE_ATTR_RO(retry_counter);
  403. static DEVICE_ATTR_RO(max_retry);
  404. static DEVICE_ATTR_RO(dcmf0);
  405. static DEVICE_ATTR_RO(dcmf1);
  406. static DEVICE_ATTR_RO(dcmf2);
  407. static DEVICE_ATTR_RO(dcmf3);
  408. static DEVICE_ATTR_WO(reboot_image);
  409. static DEVICE_ATTR_WO(notify);
  410. static struct attribute *rsu_attrs[] = {
  411. &dev_attr_current_image.attr,
  412. &dev_attr_fail_image.attr,
  413. &dev_attr_state.attr,
  414. &dev_attr_version.attr,
  415. &dev_attr_error_location.attr,
  416. &dev_attr_error_details.attr,
  417. &dev_attr_retry_counter.attr,
  418. &dev_attr_max_retry.attr,
  419. &dev_attr_dcmf0.attr,
  420. &dev_attr_dcmf1.attr,
  421. &dev_attr_dcmf2.attr,
  422. &dev_attr_dcmf3.attr,
  423. &dev_attr_reboot_image.attr,
  424. &dev_attr_notify.attr,
  425. NULL
  426. };
  427. ATTRIBUTE_GROUPS(rsu);
  428. static int stratix10_rsu_probe(struct platform_device *pdev)
  429. {
  430. struct device *dev = &pdev->dev;
  431. struct stratix10_rsu_priv *priv;
  432. int ret;
  433. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  434. if (!priv)
  435. return -ENOMEM;
  436. priv->client.dev = dev;
  437. priv->client.receive_cb = NULL;
  438. priv->client.priv = priv;
  439. priv->status.current_image = 0;
  440. priv->status.fail_image = 0;
  441. priv->status.error_location = 0;
  442. priv->status.error_details = 0;
  443. priv->status.version = 0;
  444. priv->status.state = 0;
  445. priv->retry_counter = INVALID_RETRY_COUNTER;
  446. priv->dcmf_version.dcmf0 = INVALID_DCMF_VERSION;
  447. priv->dcmf_version.dcmf1 = INVALID_DCMF_VERSION;
  448. priv->dcmf_version.dcmf2 = INVALID_DCMF_VERSION;
  449. priv->dcmf_version.dcmf3 = INVALID_DCMF_VERSION;
  450. priv->max_retry = INVALID_RETRY_COUNTER;
  451. mutex_init(&priv->lock);
  452. priv->chan = stratix10_svc_request_channel_byname(&priv->client,
  453. SVC_CLIENT_RSU);
  454. if (IS_ERR(priv->chan)) {
  455. dev_err(dev, "couldn't get service channel %s\n",
  456. SVC_CLIENT_RSU);
  457. return PTR_ERR(priv->chan);
  458. }
  459. init_completion(&priv->completion);
  460. platform_set_drvdata(pdev, priv);
  461. /* get the initial state from firmware */
  462. ret = rsu_send_msg(priv, COMMAND_RSU_STATUS,
  463. 0, rsu_status_callback);
  464. if (ret) {
  465. dev_err(dev, "Error, getting RSU status %i\n", ret);
  466. stratix10_svc_free_channel(priv->chan);
  467. }
  468. /* get DCMF version from firmware */
  469. ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_VERSION,
  470. 0, rsu_dcmf_version_callback);
  471. if (ret) {
  472. dev_err(dev, "Error, getting DCMF version %i\n", ret);
  473. stratix10_svc_free_channel(priv->chan);
  474. }
  475. ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, rsu_retry_callback);
  476. if (ret) {
  477. dev_err(dev, "Error, getting RSU retry %i\n", ret);
  478. stratix10_svc_free_channel(priv->chan);
  479. }
  480. ret = rsu_send_msg(priv, COMMAND_RSU_MAX_RETRY, 0,
  481. rsu_max_retry_callback);
  482. if (ret) {
  483. dev_err(dev, "Error, getting RSU max retry %i\n", ret);
  484. stratix10_svc_free_channel(priv->chan);
  485. }
  486. return ret;
  487. }
  488. static int stratix10_rsu_remove(struct platform_device *pdev)
  489. {
  490. struct stratix10_rsu_priv *priv = platform_get_drvdata(pdev);
  491. stratix10_svc_free_channel(priv->chan);
  492. return 0;
  493. }
  494. static struct platform_driver stratix10_rsu_driver = {
  495. .probe = stratix10_rsu_probe,
  496. .remove = stratix10_rsu_remove,
  497. .driver = {
  498. .name = "stratix10-rsu",
  499. .dev_groups = rsu_groups,
  500. },
  501. };
  502. module_platform_driver(stratix10_rsu_driver);
  503. MODULE_LICENSE("GPL v2");
  504. MODULE_DESCRIPTION("Intel Remote System Update Driver");
  505. MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");