stratix10-soc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * FPGA Manager Driver for Intel Stratix10 SoC
  4. *
  5. * Copyright (C) 2018 Intel Corporation
  6. */
  7. #include <linux/completion.h>
  8. #include <linux/fpga/fpga-mgr.h>
  9. #include <linux/firmware/intel/stratix10-svc-client.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. /*
  15. * FPGA programming requires a higher level of privilege (EL3), per the SoC
  16. * design.
  17. */
  18. #define NUM_SVC_BUFS 4
  19. #define SVC_BUF_SIZE SZ_512K
  20. /* Indicates buffer is in use if set */
  21. #define SVC_BUF_LOCK 0
  22. #define S10_BUFFER_TIMEOUT (msecs_to_jiffies(SVC_RECONFIG_BUFFER_TIMEOUT_MS))
  23. #define S10_RECONFIG_TIMEOUT (msecs_to_jiffies(SVC_RECONFIG_REQUEST_TIMEOUT_MS))
  24. /*
  25. * struct s10_svc_buf
  26. * buf: virtual address of buf provided by service layer
  27. * lock: locked if buffer is in use
  28. */
  29. struct s10_svc_buf {
  30. char *buf;
  31. unsigned long lock;
  32. };
  33. struct s10_priv {
  34. struct stratix10_svc_chan *chan;
  35. struct stratix10_svc_client client;
  36. struct completion status_return_completion;
  37. struct s10_svc_buf svc_bufs[NUM_SVC_BUFS];
  38. unsigned long status;
  39. };
  40. static int s10_svc_send_msg(struct s10_priv *priv,
  41. enum stratix10_svc_command_code command,
  42. void *payload, u32 payload_length)
  43. {
  44. struct stratix10_svc_chan *chan = priv->chan;
  45. struct device *dev = priv->client.dev;
  46. struct stratix10_svc_client_msg msg;
  47. int ret;
  48. dev_dbg(dev, "%s cmd=%d payload=%p length=%d\n",
  49. __func__, command, payload, payload_length);
  50. msg.command = command;
  51. msg.payload = payload;
  52. msg.payload_length = payload_length;
  53. ret = stratix10_svc_send(chan, &msg);
  54. dev_dbg(dev, "stratix10_svc_send returned status %d\n", ret);
  55. return ret;
  56. }
  57. /*
  58. * Free buffers allocated from the service layer's pool that are not in use.
  59. * Return true when all buffers are freed.
  60. */
  61. static bool s10_free_buffers(struct fpga_manager *mgr)
  62. {
  63. struct s10_priv *priv = mgr->priv;
  64. uint num_free = 0;
  65. uint i;
  66. for (i = 0; i < NUM_SVC_BUFS; i++) {
  67. if (!priv->svc_bufs[i].buf) {
  68. num_free++;
  69. continue;
  70. }
  71. if (!test_and_set_bit_lock(SVC_BUF_LOCK,
  72. &priv->svc_bufs[i].lock)) {
  73. stratix10_svc_free_memory(priv->chan,
  74. priv->svc_bufs[i].buf);
  75. priv->svc_bufs[i].buf = NULL;
  76. num_free++;
  77. }
  78. }
  79. return num_free == NUM_SVC_BUFS;
  80. }
  81. /*
  82. * Returns count of how many buffers are not in use.
  83. */
  84. static uint s10_free_buffer_count(struct fpga_manager *mgr)
  85. {
  86. struct s10_priv *priv = mgr->priv;
  87. uint num_free = 0;
  88. uint i;
  89. for (i = 0; i < NUM_SVC_BUFS; i++)
  90. if (!priv->svc_bufs[i].buf)
  91. num_free++;
  92. return num_free;
  93. }
  94. /*
  95. * s10_unlock_bufs
  96. * Given the returned buffer address, match that address to our buffer struct
  97. * and unlock that buffer. This marks it as available to be refilled and sent
  98. * (or freed).
  99. * priv: private data
  100. * kaddr: kernel address of buffer that was returned from service layer
  101. */
  102. static void s10_unlock_bufs(struct s10_priv *priv, void *kaddr)
  103. {
  104. uint i;
  105. if (!kaddr)
  106. return;
  107. for (i = 0; i < NUM_SVC_BUFS; i++)
  108. if (priv->svc_bufs[i].buf == kaddr) {
  109. clear_bit_unlock(SVC_BUF_LOCK,
  110. &priv->svc_bufs[i].lock);
  111. return;
  112. }
  113. WARN(1, "Unknown buffer returned from service layer %p\n", kaddr);
  114. }
  115. /*
  116. * s10_receive_callback - callback for service layer to use to provide client
  117. * (this driver) messages received through the mailbox.
  118. * client: service layer client struct
  119. * data: message from service layer
  120. */
  121. static void s10_receive_callback(struct stratix10_svc_client *client,
  122. struct stratix10_svc_cb_data *data)
  123. {
  124. struct s10_priv *priv = client->priv;
  125. u32 status;
  126. int i;
  127. WARN_ONCE(!data, "%s: stratix10_svc_rc_data = NULL", __func__);
  128. status = data->status;
  129. /*
  130. * Here we set status bits as we receive them. Elsewhere, we always use
  131. * test_and_clear_bit() to check status in priv->status
  132. */
  133. for (i = 0; i <= SVC_STATUS_ERROR; i++)
  134. if (status & (1 << i))
  135. set_bit(i, &priv->status);
  136. if (status & BIT(SVC_STATUS_BUFFER_DONE)) {
  137. s10_unlock_bufs(priv, data->kaddr1);
  138. s10_unlock_bufs(priv, data->kaddr2);
  139. s10_unlock_bufs(priv, data->kaddr3);
  140. }
  141. complete(&priv->status_return_completion);
  142. }
  143. /*
  144. * s10_ops_write_init - prepare for FPGA reconfiguration by requesting
  145. * partial reconfig and allocating buffers from the service layer.
  146. */
  147. static int s10_ops_write_init(struct fpga_manager *mgr,
  148. struct fpga_image_info *info,
  149. const char *buf, size_t count)
  150. {
  151. struct s10_priv *priv = mgr->priv;
  152. struct device *dev = priv->client.dev;
  153. struct stratix10_svc_command_config_type ctype;
  154. char *kbuf;
  155. uint i;
  156. int ret;
  157. ctype.flags = 0;
  158. if (info->flags & FPGA_MGR_PARTIAL_RECONFIG) {
  159. dev_dbg(dev, "Requesting partial reconfiguration.\n");
  160. ctype.flags |= BIT(COMMAND_RECONFIG_FLAG_PARTIAL);
  161. } else {
  162. dev_dbg(dev, "Requesting full reconfiguration.\n");
  163. }
  164. reinit_completion(&priv->status_return_completion);
  165. ret = s10_svc_send_msg(priv, COMMAND_RECONFIG,
  166. &ctype, sizeof(ctype));
  167. if (ret < 0)
  168. goto init_done;
  169. ret = wait_for_completion_timeout(
  170. &priv->status_return_completion, S10_RECONFIG_TIMEOUT);
  171. if (!ret) {
  172. dev_err(dev, "timeout waiting for RECONFIG_REQUEST\n");
  173. ret = -ETIMEDOUT;
  174. goto init_done;
  175. }
  176. ret = 0;
  177. if (!test_and_clear_bit(SVC_STATUS_OK, &priv->status)) {
  178. ret = -ETIMEDOUT;
  179. goto init_done;
  180. }
  181. /* Allocate buffers from the service layer's pool. */
  182. for (i = 0; i < NUM_SVC_BUFS; i++) {
  183. kbuf = stratix10_svc_allocate_memory(priv->chan, SVC_BUF_SIZE);
  184. if (IS_ERR(kbuf)) {
  185. s10_free_buffers(mgr);
  186. ret = PTR_ERR(kbuf);
  187. goto init_done;
  188. }
  189. priv->svc_bufs[i].buf = kbuf;
  190. priv->svc_bufs[i].lock = 0;
  191. }
  192. init_done:
  193. stratix10_svc_done(priv->chan);
  194. return ret;
  195. }
  196. /*
  197. * s10_send_buf - send a buffer to the service layer queue
  198. * mgr: fpga manager struct
  199. * buf: fpga image buffer
  200. * count: size of buf in bytes
  201. * Returns # of bytes transferred or -ENOBUFS if the all the buffers are in use
  202. * or if the service queue is full. Never returns 0.
  203. */
  204. static int s10_send_buf(struct fpga_manager *mgr, const char *buf, size_t count)
  205. {
  206. struct s10_priv *priv = mgr->priv;
  207. struct device *dev = priv->client.dev;
  208. void *svc_buf;
  209. size_t xfer_sz;
  210. int ret;
  211. uint i;
  212. /* get/lock a buffer that that's not being used */
  213. for (i = 0; i < NUM_SVC_BUFS; i++)
  214. if (!test_and_set_bit_lock(SVC_BUF_LOCK,
  215. &priv->svc_bufs[i].lock))
  216. break;
  217. if (i == NUM_SVC_BUFS)
  218. return -ENOBUFS;
  219. xfer_sz = count < SVC_BUF_SIZE ? count : SVC_BUF_SIZE;
  220. svc_buf = priv->svc_bufs[i].buf;
  221. memcpy(svc_buf, buf, xfer_sz);
  222. ret = s10_svc_send_msg(priv, COMMAND_RECONFIG_DATA_SUBMIT,
  223. svc_buf, xfer_sz);
  224. if (ret < 0) {
  225. dev_err(dev,
  226. "Error while sending data to service layer (%d)", ret);
  227. clear_bit_unlock(SVC_BUF_LOCK, &priv->svc_bufs[i].lock);
  228. return ret;
  229. }
  230. return xfer_sz;
  231. }
  232. /*
  233. * Send an FPGA image to privileged layers to write to the FPGA. When done
  234. * sending, free all service layer buffers we allocated in write_init.
  235. */
  236. static int s10_ops_write(struct fpga_manager *mgr, const char *buf,
  237. size_t count)
  238. {
  239. struct s10_priv *priv = mgr->priv;
  240. struct device *dev = priv->client.dev;
  241. long wait_status;
  242. int sent = 0;
  243. int ret = 0;
  244. /*
  245. * Loop waiting for buffers to be returned. When a buffer is returned,
  246. * reuse it to send more data or free if if all data has been sent.
  247. */
  248. while (count > 0 || s10_free_buffer_count(mgr) != NUM_SVC_BUFS) {
  249. reinit_completion(&priv->status_return_completion);
  250. if (count > 0) {
  251. sent = s10_send_buf(mgr, buf, count);
  252. if (sent < 0)
  253. continue;
  254. count -= sent;
  255. buf += sent;
  256. } else {
  257. if (s10_free_buffers(mgr))
  258. return 0;
  259. ret = s10_svc_send_msg(
  260. priv, COMMAND_RECONFIG_DATA_CLAIM,
  261. NULL, 0);
  262. if (ret < 0)
  263. break;
  264. }
  265. /*
  266. * If callback hasn't already happened, wait for buffers to be
  267. * returned from service layer
  268. */
  269. wait_status = 1; /* not timed out */
  270. if (!priv->status)
  271. wait_status = wait_for_completion_timeout(
  272. &priv->status_return_completion,
  273. S10_BUFFER_TIMEOUT);
  274. if (test_and_clear_bit(SVC_STATUS_BUFFER_DONE, &priv->status) ||
  275. test_and_clear_bit(SVC_STATUS_BUFFER_SUBMITTED,
  276. &priv->status)) {
  277. ret = 0;
  278. continue;
  279. }
  280. if (test_and_clear_bit(SVC_STATUS_ERROR, &priv->status)) {
  281. dev_err(dev, "ERROR - giving up - SVC_STATUS_ERROR\n");
  282. ret = -EFAULT;
  283. break;
  284. }
  285. if (!wait_status) {
  286. dev_err(dev, "timeout waiting for svc layer buffers\n");
  287. ret = -ETIMEDOUT;
  288. break;
  289. }
  290. }
  291. if (!s10_free_buffers(mgr))
  292. dev_err(dev, "%s not all buffers were freed\n", __func__);
  293. return ret;
  294. }
  295. static int s10_ops_write_complete(struct fpga_manager *mgr,
  296. struct fpga_image_info *info)
  297. {
  298. struct s10_priv *priv = mgr->priv;
  299. struct device *dev = priv->client.dev;
  300. unsigned long timeout;
  301. int ret;
  302. timeout = usecs_to_jiffies(info->config_complete_timeout_us);
  303. do {
  304. reinit_completion(&priv->status_return_completion);
  305. ret = s10_svc_send_msg(priv, COMMAND_RECONFIG_STATUS, NULL, 0);
  306. if (ret < 0)
  307. break;
  308. ret = wait_for_completion_timeout(
  309. &priv->status_return_completion, timeout);
  310. if (!ret) {
  311. dev_err(dev,
  312. "timeout waiting for RECONFIG_COMPLETED\n");
  313. ret = -ETIMEDOUT;
  314. break;
  315. }
  316. /* Not error or timeout, so ret is # of jiffies until timeout */
  317. timeout = ret;
  318. ret = 0;
  319. if (test_and_clear_bit(SVC_STATUS_COMPLETED, &priv->status))
  320. break;
  321. if (test_and_clear_bit(SVC_STATUS_ERROR, &priv->status)) {
  322. dev_err(dev, "ERROR - giving up - SVC_STATUS_ERROR\n");
  323. ret = -EFAULT;
  324. break;
  325. }
  326. } while (1);
  327. stratix10_svc_done(priv->chan);
  328. return ret;
  329. }
  330. static const struct fpga_manager_ops s10_ops = {
  331. .write_init = s10_ops_write_init,
  332. .write = s10_ops_write,
  333. .write_complete = s10_ops_write_complete,
  334. };
  335. static int s10_probe(struct platform_device *pdev)
  336. {
  337. struct device *dev = &pdev->dev;
  338. struct s10_priv *priv;
  339. struct fpga_manager *mgr;
  340. int ret;
  341. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  342. if (!priv)
  343. return -ENOMEM;
  344. priv->client.dev = dev;
  345. priv->client.receive_cb = s10_receive_callback;
  346. priv->client.priv = priv;
  347. priv->chan = stratix10_svc_request_channel_byname(&priv->client,
  348. SVC_CLIENT_FPGA);
  349. if (IS_ERR(priv->chan)) {
  350. dev_err(dev, "couldn't get service channel (%s)\n",
  351. SVC_CLIENT_FPGA);
  352. return PTR_ERR(priv->chan);
  353. }
  354. init_completion(&priv->status_return_completion);
  355. mgr = fpga_mgr_register(dev, "Stratix10 SOC FPGA Manager",
  356. &s10_ops, priv);
  357. if (IS_ERR(mgr)) {
  358. dev_err(dev, "unable to register FPGA manager\n");
  359. ret = PTR_ERR(mgr);
  360. goto probe_err;
  361. }
  362. platform_set_drvdata(pdev, mgr);
  363. return 0;
  364. probe_err:
  365. stratix10_svc_free_channel(priv->chan);
  366. return ret;
  367. }
  368. static void s10_remove(struct platform_device *pdev)
  369. {
  370. struct fpga_manager *mgr = platform_get_drvdata(pdev);
  371. struct s10_priv *priv = mgr->priv;
  372. fpga_mgr_unregister(mgr);
  373. stratix10_svc_free_channel(priv->chan);
  374. }
  375. static const struct of_device_id s10_of_match[] = {
  376. {.compatible = "intel,stratix10-soc-fpga-mgr"},
  377. {.compatible = "intel,agilex-soc-fpga-mgr"},
  378. {},
  379. };
  380. MODULE_DEVICE_TABLE(of, s10_of_match);
  381. static struct platform_driver s10_driver = {
  382. .probe = s10_probe,
  383. .remove_new = s10_remove,
  384. .driver = {
  385. .name = "Stratix10 SoC FPGA manager",
  386. .of_match_table = of_match_ptr(s10_of_match),
  387. },
  388. };
  389. static int __init s10_init(void)
  390. {
  391. struct device_node *fw_np;
  392. struct device_node *np;
  393. int ret;
  394. fw_np = of_find_node_by_name(NULL, "svc");
  395. if (!fw_np)
  396. return -ENODEV;
  397. of_node_get(fw_np);
  398. np = of_find_matching_node(fw_np, s10_of_match);
  399. if (!np) {
  400. of_node_put(fw_np);
  401. return -ENODEV;
  402. }
  403. of_node_put(np);
  404. ret = of_platform_populate(fw_np, s10_of_match, NULL, NULL);
  405. of_node_put(fw_np);
  406. if (ret)
  407. return ret;
  408. return platform_driver_register(&s10_driver);
  409. }
  410. static void __exit s10_exit(void)
  411. {
  412. return platform_driver_unregister(&s10_driver);
  413. }
  414. module_init(s10_init);
  415. module_exit(s10_exit);
  416. MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
  417. MODULE_DESCRIPTION("Intel Stratix 10 SOC FPGA Manager");
  418. MODULE_LICENSE("GPL v2");