stratix10-svc.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017-2018, Intel Corporation
  4. */
  5. #include <linux/completion.h>
  6. #include <linux/delay.h>
  7. #include <linux/genalloc.h>
  8. #include <linux/io.h>
  9. #include <linux/kfifo.h>
  10. #include <linux/kthread.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/firmware/intel/stratix10-smc.h>
  19. #include <linux/firmware/intel/stratix10-svc-client.h>
  20. #include <linux/types.h>
  21. /**
  22. * SVC_NUM_DATA_IN_FIFO - number of struct stratix10_svc_data in the FIFO
  23. *
  24. * SVC_NUM_CHANNEL - number of channel supported by service layer driver
  25. *
  26. * FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS - claim back the submitted buffer(s)
  27. * from the secure world for FPGA manager to reuse, or to free the buffer(s)
  28. * when all bit-stream data had be send.
  29. *
  30. * FPGA_CONFIG_STATUS_TIMEOUT_SEC - poll the FPGA configuration status,
  31. * service layer will return error to FPGA manager when timeout occurs,
  32. * timeout is set to 30 seconds (30 * 1000) at Intel Stratix10 SoC.
  33. */
  34. #define SVC_NUM_DATA_IN_FIFO 32
  35. #define SVC_NUM_CHANNEL 2
  36. #define FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS 200
  37. #define FPGA_CONFIG_STATUS_TIMEOUT_SEC 30
  38. /* stratix10 service layer clients */
  39. #define STRATIX10_RSU "stratix10-rsu"
  40. typedef void (svc_invoke_fn)(unsigned long, unsigned long, unsigned long,
  41. unsigned long, unsigned long, unsigned long,
  42. unsigned long, unsigned long,
  43. struct arm_smccc_res *);
  44. struct stratix10_svc_chan;
  45. /**
  46. * struct stratix10_svc - svc private data
  47. * @stratix10_svc_rsu: pointer to stratix10 RSU device
  48. */
  49. struct stratix10_svc {
  50. struct platform_device *stratix10_svc_rsu;
  51. };
  52. /**
  53. * struct stratix10_svc_sh_memory - service shared memory structure
  54. * @sync_complete: state for a completion
  55. * @addr: physical address of shared memory block
  56. * @size: size of shared memory block
  57. * @invoke_fn: function to issue secure monitor or hypervisor call
  58. *
  59. * This struct is used to save physical address and size of shared memory
  60. * block. The shared memory blocked is allocated by secure monitor software
  61. * at secure world.
  62. *
  63. * Service layer driver uses the physical address and size to create a memory
  64. * pool, then allocates data buffer from that memory pool for service client.
  65. */
  66. struct stratix10_svc_sh_memory {
  67. struct completion sync_complete;
  68. unsigned long addr;
  69. unsigned long size;
  70. svc_invoke_fn *invoke_fn;
  71. };
  72. /**
  73. * struct stratix10_svc_data_mem - service memory structure
  74. * @vaddr: virtual address
  75. * @paddr: physical address
  76. * @size: size of memory
  77. * @node: link list head node
  78. *
  79. * This struct is used in a list that keeps track of buffers which have
  80. * been allocated or freed from the memory pool. Service layer driver also
  81. * uses this struct to transfer physical address to virtual address.
  82. */
  83. struct stratix10_svc_data_mem {
  84. void *vaddr;
  85. phys_addr_t paddr;
  86. size_t size;
  87. struct list_head node;
  88. };
  89. /**
  90. * struct stratix10_svc_data - service data structure
  91. * @chan: service channel
  92. * @paddr: playload physical address
  93. * @size: playload size
  94. * @command: service command requested by client
  95. * @flag: configuration type (full or partial)
  96. * @arg: args to be passed via registers and not physically mapped buffers
  97. *
  98. * This struct is used in service FIFO for inter-process communication.
  99. */
  100. struct stratix10_svc_data {
  101. struct stratix10_svc_chan *chan;
  102. phys_addr_t paddr;
  103. size_t size;
  104. u32 command;
  105. u32 flag;
  106. u64 arg[3];
  107. };
  108. /**
  109. * struct stratix10_svc_controller - service controller
  110. * @dev: device
  111. * @chans: array of service channels
  112. * @num_chans: number of channels in 'chans' array
  113. * @num_active_client: number of active service client
  114. * @node: list management
  115. * @genpool: memory pool pointing to the memory region
  116. * @task: pointer to the thread task which handles SMC or HVC call
  117. * @svc_fifo: a queue for storing service message data
  118. * @complete_status: state for completion
  119. * @svc_fifo_lock: protect access to service message data queue
  120. * @invoke_fn: function to issue secure monitor call or hypervisor call
  121. *
  122. * This struct is used to create communication channels for service clients, to
  123. * handle secure monitor or hypervisor call.
  124. */
  125. struct stratix10_svc_controller {
  126. struct device *dev;
  127. struct stratix10_svc_chan *chans;
  128. int num_chans;
  129. int num_active_client;
  130. struct list_head node;
  131. struct gen_pool *genpool;
  132. struct task_struct *task;
  133. struct kfifo svc_fifo;
  134. struct completion complete_status;
  135. spinlock_t svc_fifo_lock;
  136. svc_invoke_fn *invoke_fn;
  137. };
  138. /**
  139. * struct stratix10_svc_chan - service communication channel
  140. * @ctrl: pointer to service controller which is the provider of this channel
  141. * @scl: pointer to service client which owns the channel
  142. * @name: service client name associated with the channel
  143. * @lock: protect access to the channel
  144. *
  145. * This struct is used by service client to communicate with service layer, each
  146. * service client has its own channel created by service controller.
  147. */
  148. struct stratix10_svc_chan {
  149. struct stratix10_svc_controller *ctrl;
  150. struct stratix10_svc_client *scl;
  151. char *name;
  152. spinlock_t lock;
  153. };
  154. static LIST_HEAD(svc_ctrl);
  155. static LIST_HEAD(svc_data_mem);
  156. /**
  157. * svc_pa_to_va() - translate physical address to virtual address
  158. * @addr: to be translated physical address
  159. *
  160. * Return: valid virtual address or NULL if the provided physical
  161. * address doesn't exist.
  162. */
  163. static void *svc_pa_to_va(unsigned long addr)
  164. {
  165. struct stratix10_svc_data_mem *pmem;
  166. pr_debug("claim back P-addr=0x%016x\n", (unsigned int)addr);
  167. list_for_each_entry(pmem, &svc_data_mem, node)
  168. if (pmem->paddr == addr)
  169. return pmem->vaddr;
  170. /* physical address is not found */
  171. return NULL;
  172. }
  173. /**
  174. * svc_thread_cmd_data_claim() - claim back buffer from the secure world
  175. * @ctrl: pointer to service layer controller
  176. * @p_data: pointer to service data structure
  177. * @cb_data: pointer to callback data structure to service client
  178. *
  179. * Claim back the submitted buffers from the secure world and pass buffer
  180. * back to service client (FPGA manager, etc) for reuse.
  181. */
  182. static void svc_thread_cmd_data_claim(struct stratix10_svc_controller *ctrl,
  183. struct stratix10_svc_data *p_data,
  184. struct stratix10_svc_cb_data *cb_data)
  185. {
  186. struct arm_smccc_res res;
  187. unsigned long timeout;
  188. reinit_completion(&ctrl->complete_status);
  189. timeout = msecs_to_jiffies(FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS);
  190. pr_debug("%s: claim back the submitted buffer\n", __func__);
  191. do {
  192. ctrl->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_COMPLETED_WRITE,
  193. 0, 0, 0, 0, 0, 0, 0, &res);
  194. if (res.a0 == INTEL_SIP_SMC_STATUS_OK) {
  195. if (!res.a1) {
  196. complete(&ctrl->complete_status);
  197. break;
  198. }
  199. cb_data->status = BIT(SVC_STATUS_BUFFER_DONE);
  200. cb_data->kaddr1 = svc_pa_to_va(res.a1);
  201. cb_data->kaddr2 = (res.a2) ?
  202. svc_pa_to_va(res.a2) : NULL;
  203. cb_data->kaddr3 = (res.a3) ?
  204. svc_pa_to_va(res.a3) : NULL;
  205. p_data->chan->scl->receive_cb(p_data->chan->scl,
  206. cb_data);
  207. } else {
  208. pr_debug("%s: secure world busy, polling again\n",
  209. __func__);
  210. }
  211. } while (res.a0 == INTEL_SIP_SMC_STATUS_OK ||
  212. res.a0 == INTEL_SIP_SMC_STATUS_BUSY ||
  213. wait_for_completion_timeout(&ctrl->complete_status, timeout));
  214. }
  215. /**
  216. * svc_thread_cmd_config_status() - check configuration status
  217. * @ctrl: pointer to service layer controller
  218. * @p_data: pointer to service data structure
  219. * @cb_data: pointer to callback data structure to service client
  220. *
  221. * Check whether the secure firmware at secure world has finished the FPGA
  222. * configuration, and then inform FPGA manager the configuration status.
  223. */
  224. static void svc_thread_cmd_config_status(struct stratix10_svc_controller *ctrl,
  225. struct stratix10_svc_data *p_data,
  226. struct stratix10_svc_cb_data *cb_data)
  227. {
  228. struct arm_smccc_res res;
  229. int count_in_sec;
  230. cb_data->kaddr1 = NULL;
  231. cb_data->kaddr2 = NULL;
  232. cb_data->kaddr3 = NULL;
  233. cb_data->status = BIT(SVC_STATUS_ERROR);
  234. pr_debug("%s: polling config status\n", __func__);
  235. count_in_sec = FPGA_CONFIG_STATUS_TIMEOUT_SEC;
  236. while (count_in_sec) {
  237. ctrl->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_ISDONE,
  238. 0, 0, 0, 0, 0, 0, 0, &res);
  239. if ((res.a0 == INTEL_SIP_SMC_STATUS_OK) ||
  240. (res.a0 == INTEL_SIP_SMC_STATUS_ERROR))
  241. break;
  242. /*
  243. * configuration is still in progress, wait one second then
  244. * poll again
  245. */
  246. msleep(1000);
  247. count_in_sec--;
  248. }
  249. if (res.a0 == INTEL_SIP_SMC_STATUS_OK && count_in_sec)
  250. cb_data->status = BIT(SVC_STATUS_COMPLETED);
  251. p_data->chan->scl->receive_cb(p_data->chan->scl, cb_data);
  252. }
  253. /**
  254. * svc_thread_recv_status_ok() - handle the successful status
  255. * @p_data: pointer to service data structure
  256. * @cb_data: pointer to callback data structure to service client
  257. * @res: result from SMC or HVC call
  258. *
  259. * Send back the correspond status to the service clients.
  260. */
  261. static void svc_thread_recv_status_ok(struct stratix10_svc_data *p_data,
  262. struct stratix10_svc_cb_data *cb_data,
  263. struct arm_smccc_res res)
  264. {
  265. cb_data->kaddr1 = NULL;
  266. cb_data->kaddr2 = NULL;
  267. cb_data->kaddr3 = NULL;
  268. switch (p_data->command) {
  269. case COMMAND_RECONFIG:
  270. case COMMAND_RSU_UPDATE:
  271. case COMMAND_RSU_NOTIFY:
  272. cb_data->status = BIT(SVC_STATUS_OK);
  273. break;
  274. case COMMAND_RECONFIG_DATA_SUBMIT:
  275. cb_data->status = BIT(SVC_STATUS_BUFFER_SUBMITTED);
  276. break;
  277. case COMMAND_RECONFIG_STATUS:
  278. cb_data->status = BIT(SVC_STATUS_COMPLETED);
  279. break;
  280. case COMMAND_RSU_RETRY:
  281. case COMMAND_RSU_MAX_RETRY:
  282. cb_data->status = BIT(SVC_STATUS_OK);
  283. cb_data->kaddr1 = &res.a1;
  284. break;
  285. case COMMAND_RSU_DCMF_VERSION:
  286. cb_data->status = BIT(SVC_STATUS_OK);
  287. cb_data->kaddr1 = &res.a1;
  288. cb_data->kaddr2 = &res.a2;
  289. break;
  290. default:
  291. pr_warn("it shouldn't happen\n");
  292. break;
  293. }
  294. pr_debug("%s: call receive_cb\n", __func__);
  295. p_data->chan->scl->receive_cb(p_data->chan->scl, cb_data);
  296. }
  297. /**
  298. * svc_normal_to_secure_thread() - the function to run in the kthread
  299. * @data: data pointer for kthread function
  300. *
  301. * Service layer driver creates stratix10_svc_smc_hvc_call kthread on CPU
  302. * node 0, its function stratix10_svc_secure_call_thread is used to handle
  303. * SMC or HVC calls between kernel driver and secure monitor software.
  304. *
  305. * Return: 0 for success or -ENOMEM on error.
  306. */
  307. static int svc_normal_to_secure_thread(void *data)
  308. {
  309. struct stratix10_svc_controller
  310. *ctrl = (struct stratix10_svc_controller *)data;
  311. struct stratix10_svc_data *pdata;
  312. struct stratix10_svc_cb_data *cbdata;
  313. struct arm_smccc_res res;
  314. unsigned long a0, a1, a2;
  315. int ret_fifo = 0;
  316. pdata = kmalloc(sizeof(*pdata), GFP_KERNEL);
  317. if (!pdata)
  318. return -ENOMEM;
  319. cbdata = kmalloc(sizeof(*cbdata), GFP_KERNEL);
  320. if (!cbdata) {
  321. kfree(pdata);
  322. return -ENOMEM;
  323. }
  324. /* default set, to remove build warning */
  325. a0 = INTEL_SIP_SMC_FPGA_CONFIG_LOOPBACK;
  326. a1 = 0;
  327. a2 = 0;
  328. pr_debug("smc_hvc_shm_thread is running\n");
  329. while (!kthread_should_stop()) {
  330. ret_fifo = kfifo_out_spinlocked(&ctrl->svc_fifo,
  331. pdata, sizeof(*pdata),
  332. &ctrl->svc_fifo_lock);
  333. if (!ret_fifo)
  334. continue;
  335. pr_debug("get from FIFO pa=0x%016x, command=%u, size=%u\n",
  336. (unsigned int)pdata->paddr, pdata->command,
  337. (unsigned int)pdata->size);
  338. switch (pdata->command) {
  339. case COMMAND_RECONFIG_DATA_CLAIM:
  340. svc_thread_cmd_data_claim(ctrl, pdata, cbdata);
  341. continue;
  342. case COMMAND_RECONFIG:
  343. a0 = INTEL_SIP_SMC_FPGA_CONFIG_START;
  344. pr_debug("conf_type=%u\n", (unsigned int)pdata->flag);
  345. a1 = pdata->flag;
  346. a2 = 0;
  347. break;
  348. case COMMAND_RECONFIG_DATA_SUBMIT:
  349. a0 = INTEL_SIP_SMC_FPGA_CONFIG_WRITE;
  350. a1 = (unsigned long)pdata->paddr;
  351. a2 = (unsigned long)pdata->size;
  352. break;
  353. case COMMAND_RECONFIG_STATUS:
  354. a0 = INTEL_SIP_SMC_FPGA_CONFIG_ISDONE;
  355. a1 = 0;
  356. a2 = 0;
  357. break;
  358. case COMMAND_RSU_STATUS:
  359. a0 = INTEL_SIP_SMC_RSU_STATUS;
  360. a1 = 0;
  361. a2 = 0;
  362. break;
  363. case COMMAND_RSU_UPDATE:
  364. a0 = INTEL_SIP_SMC_RSU_UPDATE;
  365. a1 = pdata->arg[0];
  366. a2 = 0;
  367. break;
  368. case COMMAND_RSU_NOTIFY:
  369. a0 = INTEL_SIP_SMC_RSU_NOTIFY;
  370. a1 = pdata->arg[0];
  371. a2 = 0;
  372. break;
  373. case COMMAND_RSU_RETRY:
  374. a0 = INTEL_SIP_SMC_RSU_RETRY_COUNTER;
  375. a1 = 0;
  376. a2 = 0;
  377. break;
  378. case COMMAND_RSU_MAX_RETRY:
  379. a0 = INTEL_SIP_SMC_RSU_MAX_RETRY;
  380. a1 = 0;
  381. a2 = 0;
  382. break;
  383. case COMMAND_RSU_DCMF_VERSION:
  384. a0 = INTEL_SIP_SMC_RSU_DCMF_VERSION;
  385. a1 = 0;
  386. a2 = 0;
  387. break;
  388. default:
  389. pr_warn("it shouldn't happen\n");
  390. break;
  391. }
  392. pr_debug("%s: before SMC call -- a0=0x%016x a1=0x%016x",
  393. __func__, (unsigned int)a0, (unsigned int)a1);
  394. pr_debug(" a2=0x%016x\n", (unsigned int)a2);
  395. ctrl->invoke_fn(a0, a1, a2, 0, 0, 0, 0, 0, &res);
  396. pr_debug("%s: after SMC call -- res.a0=0x%016x",
  397. __func__, (unsigned int)res.a0);
  398. pr_debug(" res.a1=0x%016x, res.a2=0x%016x",
  399. (unsigned int)res.a1, (unsigned int)res.a2);
  400. pr_debug(" res.a3=0x%016x\n", (unsigned int)res.a3);
  401. if (pdata->command == COMMAND_RSU_STATUS) {
  402. if (res.a0 == INTEL_SIP_SMC_RSU_ERROR)
  403. cbdata->status = BIT(SVC_STATUS_ERROR);
  404. else
  405. cbdata->status = BIT(SVC_STATUS_OK);
  406. cbdata->kaddr1 = &res;
  407. cbdata->kaddr2 = NULL;
  408. cbdata->kaddr3 = NULL;
  409. pdata->chan->scl->receive_cb(pdata->chan->scl, cbdata);
  410. continue;
  411. }
  412. switch (res.a0) {
  413. case INTEL_SIP_SMC_STATUS_OK:
  414. svc_thread_recv_status_ok(pdata, cbdata, res);
  415. break;
  416. case INTEL_SIP_SMC_STATUS_BUSY:
  417. switch (pdata->command) {
  418. case COMMAND_RECONFIG_DATA_SUBMIT:
  419. svc_thread_cmd_data_claim(ctrl,
  420. pdata, cbdata);
  421. break;
  422. case COMMAND_RECONFIG_STATUS:
  423. svc_thread_cmd_config_status(ctrl,
  424. pdata, cbdata);
  425. break;
  426. default:
  427. pr_warn("it shouldn't happen\n");
  428. break;
  429. }
  430. break;
  431. case INTEL_SIP_SMC_STATUS_REJECTED:
  432. pr_debug("%s: STATUS_REJECTED\n", __func__);
  433. break;
  434. case INTEL_SIP_SMC_STATUS_ERROR:
  435. case INTEL_SIP_SMC_RSU_ERROR:
  436. pr_err("%s: STATUS_ERROR\n", __func__);
  437. cbdata->status = BIT(SVC_STATUS_ERROR);
  438. cbdata->kaddr1 = &res.a1;
  439. cbdata->kaddr2 = NULL;
  440. cbdata->kaddr3 = NULL;
  441. pdata->chan->scl->receive_cb(pdata->chan->scl, cbdata);
  442. break;
  443. default:
  444. pr_warn("Secure firmware doesn't support...\n");
  445. /*
  446. * be compatible with older version firmware which
  447. * doesn't support RSU notify or retry
  448. */
  449. if ((pdata->command == COMMAND_RSU_RETRY) ||
  450. (pdata->command == COMMAND_RSU_MAX_RETRY) ||
  451. (pdata->command == COMMAND_RSU_NOTIFY)) {
  452. cbdata->status =
  453. BIT(SVC_STATUS_NO_SUPPORT);
  454. cbdata->kaddr1 = NULL;
  455. cbdata->kaddr2 = NULL;
  456. cbdata->kaddr3 = NULL;
  457. pdata->chan->scl->receive_cb(
  458. pdata->chan->scl, cbdata);
  459. }
  460. break;
  461. }
  462. }
  463. kfree(cbdata);
  464. kfree(pdata);
  465. return 0;
  466. }
  467. /**
  468. * svc_normal_to_secure_shm_thread() - the function to run in the kthread
  469. * @data: data pointer for kthread function
  470. *
  471. * Service layer driver creates stratix10_svc_smc_hvc_shm kthread on CPU
  472. * node 0, its function stratix10_svc_secure_shm_thread is used to query the
  473. * physical address of memory block reserved by secure monitor software at
  474. * secure world.
  475. *
  476. * svc_normal_to_secure_shm_thread() calls do_exit() directly since it is a
  477. * standlone thread for which no one will call kthread_stop() or return when
  478. * 'kthread_should_stop()' is true.
  479. */
  480. static int svc_normal_to_secure_shm_thread(void *data)
  481. {
  482. struct stratix10_svc_sh_memory
  483. *sh_mem = (struct stratix10_svc_sh_memory *)data;
  484. struct arm_smccc_res res;
  485. /* SMC or HVC call to get shared memory info from secure world */
  486. sh_mem->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_GET_MEM,
  487. 0, 0, 0, 0, 0, 0, 0, &res);
  488. if (res.a0 == INTEL_SIP_SMC_STATUS_OK) {
  489. sh_mem->addr = res.a1;
  490. sh_mem->size = res.a2;
  491. } else {
  492. pr_err("%s: after SMC call -- res.a0=0x%016x", __func__,
  493. (unsigned int)res.a0);
  494. sh_mem->addr = 0;
  495. sh_mem->size = 0;
  496. }
  497. complete(&sh_mem->sync_complete);
  498. do_exit(0);
  499. }
  500. /**
  501. * svc_get_sh_memory() - get memory block reserved by secure monitor SW
  502. * @pdev: pointer to service layer device
  503. * @sh_memory: pointer to service shared memory structure
  504. *
  505. * Return: zero for successfully getting the physical address of memory block
  506. * reserved by secure monitor software, or negative value on error.
  507. */
  508. static int svc_get_sh_memory(struct platform_device *pdev,
  509. struct stratix10_svc_sh_memory *sh_memory)
  510. {
  511. struct device *dev = &pdev->dev;
  512. struct task_struct *sh_memory_task;
  513. unsigned int cpu = 0;
  514. init_completion(&sh_memory->sync_complete);
  515. /* smc or hvc call happens on cpu 0 bound kthread */
  516. sh_memory_task = kthread_create_on_node(svc_normal_to_secure_shm_thread,
  517. (void *)sh_memory,
  518. cpu_to_node(cpu),
  519. "svc_smc_hvc_shm_thread");
  520. if (IS_ERR(sh_memory_task)) {
  521. dev_err(dev, "fail to create stratix10_svc_smc_shm_thread\n");
  522. return -EINVAL;
  523. }
  524. wake_up_process(sh_memory_task);
  525. if (!wait_for_completion_timeout(&sh_memory->sync_complete, 10 * HZ)) {
  526. dev_err(dev,
  527. "timeout to get sh-memory paras from secure world\n");
  528. return -ETIMEDOUT;
  529. }
  530. if (!sh_memory->addr || !sh_memory->size) {
  531. dev_err(dev,
  532. "failed to get shared memory info from secure world\n");
  533. return -ENOMEM;
  534. }
  535. dev_dbg(dev, "SM software provides paddr: 0x%016x, size: 0x%08x\n",
  536. (unsigned int)sh_memory->addr,
  537. (unsigned int)sh_memory->size);
  538. return 0;
  539. }
  540. /**
  541. * svc_create_memory_pool() - create a memory pool from reserved memory block
  542. * @pdev: pointer to service layer device
  543. * @sh_memory: pointer to service shared memory structure
  544. *
  545. * Return: pool allocated from reserved memory block or ERR_PTR() on error.
  546. */
  547. static struct gen_pool *
  548. svc_create_memory_pool(struct platform_device *pdev,
  549. struct stratix10_svc_sh_memory *sh_memory)
  550. {
  551. struct device *dev = &pdev->dev;
  552. struct gen_pool *genpool;
  553. unsigned long vaddr;
  554. phys_addr_t paddr;
  555. size_t size;
  556. phys_addr_t begin;
  557. phys_addr_t end;
  558. void *va;
  559. size_t page_mask = PAGE_SIZE - 1;
  560. int min_alloc_order = 3;
  561. int ret;
  562. begin = roundup(sh_memory->addr, PAGE_SIZE);
  563. end = rounddown(sh_memory->addr + sh_memory->size, PAGE_SIZE);
  564. paddr = begin;
  565. size = end - begin;
  566. va = memremap(paddr, size, MEMREMAP_WC);
  567. if (!va) {
  568. dev_err(dev, "fail to remap shared memory\n");
  569. return ERR_PTR(-EINVAL);
  570. }
  571. vaddr = (unsigned long)va;
  572. dev_dbg(dev,
  573. "reserved memory vaddr: %p, paddr: 0x%16x size: 0x%8x\n",
  574. va, (unsigned int)paddr, (unsigned int)size);
  575. if ((vaddr & page_mask) || (paddr & page_mask) ||
  576. (size & page_mask)) {
  577. dev_err(dev, "page is not aligned\n");
  578. return ERR_PTR(-EINVAL);
  579. }
  580. genpool = gen_pool_create(min_alloc_order, -1);
  581. if (!genpool) {
  582. dev_err(dev, "fail to create genpool\n");
  583. return ERR_PTR(-ENOMEM);
  584. }
  585. gen_pool_set_algo(genpool, gen_pool_best_fit, NULL);
  586. ret = gen_pool_add_virt(genpool, vaddr, paddr, size, -1);
  587. if (ret) {
  588. dev_err(dev, "fail to add memory chunk to the pool\n");
  589. gen_pool_destroy(genpool);
  590. return ERR_PTR(ret);
  591. }
  592. return genpool;
  593. }
  594. /**
  595. * svc_smccc_smc() - secure monitor call between normal and secure world
  596. * @a0: argument passed in registers 0
  597. * @a1: argument passed in registers 1
  598. * @a2: argument passed in registers 2
  599. * @a3: argument passed in registers 3
  600. * @a4: argument passed in registers 4
  601. * @a5: argument passed in registers 5
  602. * @a6: argument passed in registers 6
  603. * @a7: argument passed in registers 7
  604. * @res: result values from register 0 to 3
  605. */
  606. static void svc_smccc_smc(unsigned long a0, unsigned long a1,
  607. unsigned long a2, unsigned long a3,
  608. unsigned long a4, unsigned long a5,
  609. unsigned long a6, unsigned long a7,
  610. struct arm_smccc_res *res)
  611. {
  612. arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  613. }
  614. /**
  615. * svc_smccc_hvc() - hypervisor call between normal and secure world
  616. * @a0: argument passed in registers 0
  617. * @a1: argument passed in registers 1
  618. * @a2: argument passed in registers 2
  619. * @a3: argument passed in registers 3
  620. * @a4: argument passed in registers 4
  621. * @a5: argument passed in registers 5
  622. * @a6: argument passed in registers 6
  623. * @a7: argument passed in registers 7
  624. * @res: result values from register 0 to 3
  625. */
  626. static void svc_smccc_hvc(unsigned long a0, unsigned long a1,
  627. unsigned long a2, unsigned long a3,
  628. unsigned long a4, unsigned long a5,
  629. unsigned long a6, unsigned long a7,
  630. struct arm_smccc_res *res)
  631. {
  632. arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  633. }
  634. /**
  635. * get_invoke_func() - invoke SMC or HVC call
  636. * @dev: pointer to device
  637. *
  638. * Return: function pointer to svc_smccc_smc or svc_smccc_hvc.
  639. */
  640. static svc_invoke_fn *get_invoke_func(struct device *dev)
  641. {
  642. const char *method;
  643. if (of_property_read_string(dev->of_node, "method", &method)) {
  644. dev_warn(dev, "missing \"method\" property\n");
  645. return ERR_PTR(-ENXIO);
  646. }
  647. if (!strcmp(method, "smc"))
  648. return svc_smccc_smc;
  649. if (!strcmp(method, "hvc"))
  650. return svc_smccc_hvc;
  651. dev_warn(dev, "invalid \"method\" property: %s\n", method);
  652. return ERR_PTR(-EINVAL);
  653. }
  654. /**
  655. * stratix10_svc_request_channel_byname() - request a service channel
  656. * @client: pointer to service client
  657. * @name: service client name
  658. *
  659. * This function is used by service client to request a service channel.
  660. *
  661. * Return: a pointer to channel assigned to the client on success,
  662. * or ERR_PTR() on error.
  663. */
  664. struct stratix10_svc_chan *stratix10_svc_request_channel_byname(
  665. struct stratix10_svc_client *client, const char *name)
  666. {
  667. struct device *dev = client->dev;
  668. struct stratix10_svc_controller *controller;
  669. struct stratix10_svc_chan *chan = NULL;
  670. unsigned long flag;
  671. int i;
  672. /* if probe was called after client's, or error on probe */
  673. if (list_empty(&svc_ctrl))
  674. return ERR_PTR(-EPROBE_DEFER);
  675. controller = list_first_entry(&svc_ctrl,
  676. struct stratix10_svc_controller, node);
  677. for (i = 0; i < SVC_NUM_CHANNEL; i++) {
  678. if (!strcmp(controller->chans[i].name, name)) {
  679. chan = &controller->chans[i];
  680. break;
  681. }
  682. }
  683. /* if there was no channel match */
  684. if (i == SVC_NUM_CHANNEL) {
  685. dev_err(dev, "%s: channel not allocated\n", __func__);
  686. return ERR_PTR(-EINVAL);
  687. }
  688. if (chan->scl || !try_module_get(controller->dev->driver->owner)) {
  689. dev_dbg(dev, "%s: svc not free\n", __func__);
  690. return ERR_PTR(-EBUSY);
  691. }
  692. spin_lock_irqsave(&chan->lock, flag);
  693. chan->scl = client;
  694. chan->ctrl->num_active_client++;
  695. spin_unlock_irqrestore(&chan->lock, flag);
  696. return chan;
  697. }
  698. EXPORT_SYMBOL_GPL(stratix10_svc_request_channel_byname);
  699. /**
  700. * stratix10_svc_free_channel() - free service channel
  701. * @chan: service channel to be freed
  702. *
  703. * This function is used by service client to free a service channel.
  704. */
  705. void stratix10_svc_free_channel(struct stratix10_svc_chan *chan)
  706. {
  707. unsigned long flag;
  708. spin_lock_irqsave(&chan->lock, flag);
  709. chan->scl = NULL;
  710. chan->ctrl->num_active_client--;
  711. module_put(chan->ctrl->dev->driver->owner);
  712. spin_unlock_irqrestore(&chan->lock, flag);
  713. }
  714. EXPORT_SYMBOL_GPL(stratix10_svc_free_channel);
  715. /**
  716. * stratix10_svc_send() - send a message data to the remote
  717. * @chan: service channel assigned to the client
  718. * @msg: message data to be sent, in the format of
  719. * "struct stratix10_svc_client_msg"
  720. *
  721. * This function is used by service client to add a message to the service
  722. * layer driver's queue for being sent to the secure world.
  723. *
  724. * Return: 0 for success, -ENOMEM or -ENOBUFS on error.
  725. */
  726. int stratix10_svc_send(struct stratix10_svc_chan *chan, void *msg)
  727. {
  728. struct stratix10_svc_client_msg
  729. *p_msg = (struct stratix10_svc_client_msg *)msg;
  730. struct stratix10_svc_data_mem *p_mem;
  731. struct stratix10_svc_data *p_data;
  732. int ret = 0;
  733. unsigned int cpu = 0;
  734. p_data = kzalloc(sizeof(*p_data), GFP_KERNEL);
  735. if (!p_data)
  736. return -ENOMEM;
  737. /* first client will create kernel thread */
  738. if (!chan->ctrl->task) {
  739. chan->ctrl->task =
  740. kthread_create_on_node(svc_normal_to_secure_thread,
  741. (void *)chan->ctrl,
  742. cpu_to_node(cpu),
  743. "svc_smc_hvc_thread");
  744. if (IS_ERR(chan->ctrl->task)) {
  745. dev_err(chan->ctrl->dev,
  746. "failed to create svc_smc_hvc_thread\n");
  747. kfree(p_data);
  748. return -EINVAL;
  749. }
  750. kthread_bind(chan->ctrl->task, cpu);
  751. wake_up_process(chan->ctrl->task);
  752. }
  753. pr_debug("%s: sent P-va=%p, P-com=%x, P-size=%u\n", __func__,
  754. p_msg->payload, p_msg->command,
  755. (unsigned int)p_msg->payload_length);
  756. if (list_empty(&svc_data_mem)) {
  757. if (p_msg->command == COMMAND_RECONFIG) {
  758. struct stratix10_svc_command_config_type *ct =
  759. (struct stratix10_svc_command_config_type *)
  760. p_msg->payload;
  761. p_data->flag = ct->flags;
  762. }
  763. } else {
  764. list_for_each_entry(p_mem, &svc_data_mem, node)
  765. if (p_mem->vaddr == p_msg->payload) {
  766. p_data->paddr = p_mem->paddr;
  767. break;
  768. }
  769. }
  770. p_data->command = p_msg->command;
  771. p_data->arg[0] = p_msg->arg[0];
  772. p_data->arg[1] = p_msg->arg[1];
  773. p_data->arg[2] = p_msg->arg[2];
  774. p_data->size = p_msg->payload_length;
  775. p_data->chan = chan;
  776. pr_debug("%s: put to FIFO pa=0x%016x, cmd=%x, size=%u\n", __func__,
  777. (unsigned int)p_data->paddr, p_data->command,
  778. (unsigned int)p_data->size);
  779. ret = kfifo_in_spinlocked(&chan->ctrl->svc_fifo, p_data,
  780. sizeof(*p_data),
  781. &chan->ctrl->svc_fifo_lock);
  782. kfree(p_data);
  783. if (!ret)
  784. return -ENOBUFS;
  785. return 0;
  786. }
  787. EXPORT_SYMBOL_GPL(stratix10_svc_send);
  788. /**
  789. * stratix10_svc_done() - complete service request transactions
  790. * @chan: service channel assigned to the client
  791. *
  792. * This function should be called when client has finished its request
  793. * or there is an error in the request process. It allows the service layer
  794. * to stop the running thread to have maximize savings in kernel resources.
  795. */
  796. void stratix10_svc_done(struct stratix10_svc_chan *chan)
  797. {
  798. /* stop thread when thread is running AND only one active client */
  799. if (chan->ctrl->task && chan->ctrl->num_active_client <= 1) {
  800. pr_debug("svc_smc_hvc_shm_thread is stopped\n");
  801. kthread_stop(chan->ctrl->task);
  802. chan->ctrl->task = NULL;
  803. }
  804. }
  805. EXPORT_SYMBOL_GPL(stratix10_svc_done);
  806. /**
  807. * stratix10_svc_allocate_memory() - allocate memory
  808. * @chan: service channel assigned to the client
  809. * @size: memory size requested by a specific service client
  810. *
  811. * Service layer allocates the requested number of bytes buffer from the
  812. * memory pool, service client uses this function to get allocated buffers.
  813. *
  814. * Return: address of allocated memory on success, or ERR_PTR() on error.
  815. */
  816. void *stratix10_svc_allocate_memory(struct stratix10_svc_chan *chan,
  817. size_t size)
  818. {
  819. struct stratix10_svc_data_mem *pmem;
  820. unsigned long va;
  821. phys_addr_t pa;
  822. struct gen_pool *genpool = chan->ctrl->genpool;
  823. size_t s = roundup(size, 1 << genpool->min_alloc_order);
  824. pmem = devm_kzalloc(chan->ctrl->dev, sizeof(*pmem), GFP_KERNEL);
  825. if (!pmem)
  826. return ERR_PTR(-ENOMEM);
  827. va = gen_pool_alloc(genpool, s);
  828. if (!va)
  829. return ERR_PTR(-ENOMEM);
  830. memset((void *)va, 0, s);
  831. pa = gen_pool_virt_to_phys(genpool, va);
  832. pmem->vaddr = (void *)va;
  833. pmem->paddr = pa;
  834. pmem->size = s;
  835. list_add_tail(&pmem->node, &svc_data_mem);
  836. pr_debug("%s: va=%p, pa=0x%016x\n", __func__,
  837. pmem->vaddr, (unsigned int)pmem->paddr);
  838. return (void *)va;
  839. }
  840. EXPORT_SYMBOL_GPL(stratix10_svc_allocate_memory);
  841. /**
  842. * stratix10_svc_free_memory() - free allocated memory
  843. * @chan: service channel assigned to the client
  844. * @kaddr: memory to be freed
  845. *
  846. * This function is used by service client to free allocated buffers.
  847. */
  848. void stratix10_svc_free_memory(struct stratix10_svc_chan *chan, void *kaddr)
  849. {
  850. struct stratix10_svc_data_mem *pmem;
  851. size_t size = 0;
  852. list_for_each_entry(pmem, &svc_data_mem, node)
  853. if (pmem->vaddr == kaddr) {
  854. size = pmem->size;
  855. break;
  856. }
  857. gen_pool_free(chan->ctrl->genpool, (unsigned long)kaddr, size);
  858. pmem->vaddr = NULL;
  859. list_del(&pmem->node);
  860. }
  861. EXPORT_SYMBOL_GPL(stratix10_svc_free_memory);
  862. static const struct of_device_id stratix10_svc_drv_match[] = {
  863. {.compatible = "intel,stratix10-svc"},
  864. {.compatible = "intel,agilex-svc"},
  865. {},
  866. };
  867. static int stratix10_svc_drv_probe(struct platform_device *pdev)
  868. {
  869. struct device *dev = &pdev->dev;
  870. struct stratix10_svc_controller *controller;
  871. struct stratix10_svc_chan *chans;
  872. struct gen_pool *genpool;
  873. struct stratix10_svc_sh_memory *sh_memory;
  874. struct stratix10_svc *svc;
  875. svc_invoke_fn *invoke_fn;
  876. size_t fifo_size;
  877. int ret;
  878. /* get SMC or HVC function */
  879. invoke_fn = get_invoke_func(dev);
  880. if (IS_ERR(invoke_fn))
  881. return -EINVAL;
  882. sh_memory = devm_kzalloc(dev, sizeof(*sh_memory), GFP_KERNEL);
  883. if (!sh_memory)
  884. return -ENOMEM;
  885. sh_memory->invoke_fn = invoke_fn;
  886. ret = svc_get_sh_memory(pdev, sh_memory);
  887. if (ret)
  888. return ret;
  889. genpool = svc_create_memory_pool(pdev, sh_memory);
  890. if (!genpool)
  891. return -ENOMEM;
  892. /* allocate service controller and supporting channel */
  893. controller = devm_kzalloc(dev, sizeof(*controller), GFP_KERNEL);
  894. if (!controller)
  895. return -ENOMEM;
  896. chans = devm_kmalloc_array(dev, SVC_NUM_CHANNEL,
  897. sizeof(*chans), GFP_KERNEL | __GFP_ZERO);
  898. if (!chans)
  899. return -ENOMEM;
  900. controller->dev = dev;
  901. controller->num_chans = SVC_NUM_CHANNEL;
  902. controller->num_active_client = 0;
  903. controller->chans = chans;
  904. controller->genpool = genpool;
  905. controller->task = NULL;
  906. controller->invoke_fn = invoke_fn;
  907. init_completion(&controller->complete_status);
  908. fifo_size = sizeof(struct stratix10_svc_data) * SVC_NUM_DATA_IN_FIFO;
  909. ret = kfifo_alloc(&controller->svc_fifo, fifo_size, GFP_KERNEL);
  910. if (ret) {
  911. dev_err(dev, "failed to allocate FIFO\n");
  912. return ret;
  913. }
  914. spin_lock_init(&controller->svc_fifo_lock);
  915. chans[0].scl = NULL;
  916. chans[0].ctrl = controller;
  917. chans[0].name = SVC_CLIENT_FPGA;
  918. spin_lock_init(&chans[0].lock);
  919. chans[1].scl = NULL;
  920. chans[1].ctrl = controller;
  921. chans[1].name = SVC_CLIENT_RSU;
  922. spin_lock_init(&chans[1].lock);
  923. list_add_tail(&controller->node, &svc_ctrl);
  924. platform_set_drvdata(pdev, controller);
  925. /* add svc client device(s) */
  926. svc = devm_kzalloc(dev, sizeof(*svc), GFP_KERNEL);
  927. if (!svc) {
  928. ret = -ENOMEM;
  929. goto err_free_kfifo;
  930. }
  931. svc->stratix10_svc_rsu = platform_device_alloc(STRATIX10_RSU, 0);
  932. if (!svc->stratix10_svc_rsu) {
  933. dev_err(dev, "failed to allocate %s device\n", STRATIX10_RSU);
  934. ret = -ENOMEM;
  935. goto err_free_kfifo;
  936. }
  937. ret = platform_device_add(svc->stratix10_svc_rsu);
  938. if (ret)
  939. goto err_put_device;
  940. dev_set_drvdata(dev, svc);
  941. pr_info("Intel Service Layer Driver Initialized\n");
  942. return 0;
  943. err_put_device:
  944. platform_device_put(svc->stratix10_svc_rsu);
  945. err_free_kfifo:
  946. kfifo_free(&controller->svc_fifo);
  947. return ret;
  948. }
  949. static int stratix10_svc_drv_remove(struct platform_device *pdev)
  950. {
  951. struct stratix10_svc *svc = dev_get_drvdata(&pdev->dev);
  952. struct stratix10_svc_controller *ctrl = platform_get_drvdata(pdev);
  953. platform_device_unregister(svc->stratix10_svc_rsu);
  954. kfifo_free(&ctrl->svc_fifo);
  955. if (ctrl->task) {
  956. kthread_stop(ctrl->task);
  957. ctrl->task = NULL;
  958. }
  959. if (ctrl->genpool)
  960. gen_pool_destroy(ctrl->genpool);
  961. list_del(&ctrl->node);
  962. return 0;
  963. }
  964. static struct platform_driver stratix10_svc_driver = {
  965. .probe = stratix10_svc_drv_probe,
  966. .remove = stratix10_svc_drv_remove,
  967. .driver = {
  968. .name = "stratix10-svc",
  969. .of_match_table = stratix10_svc_drv_match,
  970. },
  971. };
  972. static int __init stratix10_svc_init(void)
  973. {
  974. struct device_node *fw_np;
  975. struct device_node *np;
  976. int ret;
  977. fw_np = of_find_node_by_name(NULL, "firmware");
  978. if (!fw_np)
  979. return -ENODEV;
  980. np = of_find_matching_node(fw_np, stratix10_svc_drv_match);
  981. if (!np)
  982. return -ENODEV;
  983. of_node_put(np);
  984. ret = of_platform_populate(fw_np, stratix10_svc_drv_match, NULL, NULL);
  985. if (ret)
  986. return ret;
  987. return platform_driver_register(&stratix10_svc_driver);
  988. }
  989. static void __exit stratix10_svc_exit(void)
  990. {
  991. return platform_driver_unregister(&stratix10_svc_driver);
  992. }
  993. subsys_initcall(stratix10_svc_init);
  994. module_exit(stratix10_svc_exit);
  995. MODULE_LICENSE("GPL v2");
  996. MODULE_DESCRIPTION("Intel Stratix10 Service Layer Driver");
  997. MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");
  998. MODULE_ALIAS("platform:stratix10-svc");