stratix10-svc.c 36 KB

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