stratix10-svc.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017-2018, Intel Corporation
  4. * Copyright (C) 2025, Altera Corporation
  5. */
  6. #include <linux/atomic.h>
  7. #include <linux/completion.h>
  8. #include <linux/delay.h>
  9. #include <linux/genalloc.h>
  10. #include <linux/hashtable.h>
  11. #include <linux/idr.h>
  12. #include <linux/io.h>
  13. #include <linux/kfifo.h>
  14. #include <linux/kthread.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/firmware/intel/stratix10-smc.h>
  23. #include <linux/firmware/intel/stratix10-svc-client.h>
  24. #include <linux/types.h>
  25. /**
  26. * SVC_NUM_DATA_IN_FIFO - number of struct stratix10_svc_data in the FIFO
  27. *
  28. * SVC_NUM_CHANNEL - number of channel supported by service layer driver
  29. *
  30. * FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS - claim back the submitted buffer(s)
  31. * from the secure world for FPGA manager to reuse, or to free the buffer(s)
  32. * when all bit-stream data had be send.
  33. *
  34. * FPGA_CONFIG_STATUS_TIMEOUT_SEC - poll the FPGA configuration status,
  35. * service layer will return error to FPGA manager when timeout occurs,
  36. * timeout is set to 30 seconds (30 * 1000) at Intel Stratix10 SoC.
  37. */
  38. #define SVC_NUM_DATA_IN_FIFO 8
  39. #define SVC_NUM_CHANNEL 4
  40. #define FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS 2000
  41. #define FPGA_CONFIG_STATUS_TIMEOUT_SEC 30
  42. #define BYTE_TO_WORD_SIZE 4
  43. /* stratix10 service layer clients */
  44. #define STRATIX10_RSU "stratix10-rsu"
  45. /* Maximum number of SDM client IDs. */
  46. #define MAX_SDM_CLIENT_IDS 16
  47. /* Client ID for SIP Service Version 1. */
  48. #define SIP_SVC_V1_CLIENT_ID 0x1
  49. /* Maximum number of SDM job IDs. */
  50. #define MAX_SDM_JOB_IDS 16
  51. /* Number of bits used for asynchronous transaction hashing. */
  52. #define ASYNC_TRX_HASH_BITS 3
  53. /*
  54. * Total number of transaction IDs, which is a combination of
  55. * client ID and job ID.
  56. */
  57. #define TOTAL_TRANSACTION_IDS \
  58. (MAX_SDM_CLIENT_IDS * MAX_SDM_JOB_IDS)
  59. /* Minimum major version of the ATF for Asynchronous transactions. */
  60. #define ASYNC_ATF_MINIMUM_MAJOR_VERSION 0x3
  61. /* Minimum minor version of the ATF for Asynchronous transactions.*/
  62. #define ASYNC_ATF_MINIMUM_MINOR_VERSION 0x0
  63. /* Job ID field in the transaction ID */
  64. #define STRATIX10_JOB_FIELD GENMASK(3, 0)
  65. /* Client ID field in the transaction ID */
  66. #define STRATIX10_CLIENT_FIELD GENMASK(7, 4)
  67. /* Transaction ID mask for Stratix10 service layer */
  68. #define STRATIX10_TRANS_ID_FIELD GENMASK(7, 0)
  69. /* Macro to extract the job ID from a transaction ID. */
  70. #define STRATIX10_GET_JOBID(transaction_id) \
  71. (FIELD_GET(STRATIX10_JOB_FIELD, transaction_id))
  72. /* Macro to set the job ID in a transaction ID. */
  73. #define STRATIX10_SET_JOBID(jobid) \
  74. (FIELD_PREP(STRATIX10_JOB_FIELD, jobid))
  75. /* Macro to set the client ID in a transaction ID. */
  76. #define STRATIX10_SET_CLIENTID(clientid) \
  77. (FIELD_PREP(STRATIX10_CLIENT_FIELD, clientid))
  78. /* Macro to set a transaction ID using a client ID and a job ID. */
  79. #define STRATIX10_SET_TRANSACTIONID(clientid, jobid) \
  80. (STRATIX10_SET_CLIENTID(clientid) | STRATIX10_SET_JOBID(jobid))
  81. /* Macro to set a transaction ID for SIP SMC Async transactions */
  82. #define STRATIX10_SIP_SMC_SET_TRANSACTIONID_X1(transaction_id) \
  83. (FIELD_PREP(STRATIX10_TRANS_ID_FIELD, transaction_id))
  84. /* 10-bit mask for extracting the SDM status code */
  85. #define STRATIX10_SDM_STATUS_MASK GENMASK(9, 0)
  86. /* Macro to get the SDM mailbox error status */
  87. #define STRATIX10_GET_SDM_STATUS_CODE(status) \
  88. (FIELD_GET(STRATIX10_SDM_STATUS_MASK, status))
  89. typedef void (svc_invoke_fn)(unsigned long, unsigned long, unsigned long,
  90. unsigned long, unsigned long, unsigned long,
  91. unsigned long, unsigned long,
  92. struct arm_smccc_res *);
  93. struct stratix10_svc_chan;
  94. /**
  95. * struct stratix10_svc - svc private data
  96. * @stratix10_svc_rsu: pointer to stratix10 RSU device
  97. */
  98. struct stratix10_svc {
  99. struct platform_device *stratix10_svc_rsu;
  100. };
  101. /**
  102. * struct stratix10_svc_sh_memory - service shared memory structure
  103. * @sync_complete: state for a completion
  104. * @addr: physical address of shared memory block
  105. * @size: size of shared memory block
  106. * @invoke_fn: service clients to handle secure monitor or hypervisor calls
  107. *
  108. * This struct is used to save physical address and size of shared memory
  109. * block. The shared memory blocked is allocated by secure monitor software
  110. * at secure world.
  111. *
  112. * Service layer driver uses the physical address and size to create a memory
  113. * pool, then allocates data buffer from that memory pool for service client.
  114. */
  115. struct stratix10_svc_sh_memory {
  116. struct completion sync_complete;
  117. unsigned long addr;
  118. unsigned long size;
  119. svc_invoke_fn *invoke_fn;
  120. };
  121. /**
  122. * struct stratix10_svc_data_mem - service memory structure
  123. * @vaddr: virtual address
  124. * @paddr: physical address
  125. * @size: size of memory
  126. * @node: link list head node
  127. *
  128. * This struct is used in a list that keeps track of buffers which have
  129. * been allocated or freed from the memory pool. Service layer driver also
  130. * uses this struct to transfer physical address to virtual address.
  131. */
  132. struct stratix10_svc_data_mem {
  133. void *vaddr;
  134. phys_addr_t paddr;
  135. size_t size;
  136. struct list_head node;
  137. };
  138. /**
  139. * struct stratix10_svc_data - service data structure
  140. * @chan: service channel
  141. * @paddr: physical address of to be processed payload
  142. * @size: to be processed playload size
  143. * @paddr_output: physical address of processed payload
  144. * @size_output: processed payload size
  145. * @command: service command requested by client
  146. * @flag: configuration type (full or partial)
  147. * @arg: args to be passed via registers and not physically mapped buffers
  148. *
  149. * This struct is used in service FIFO for inter-process communication.
  150. */
  151. struct stratix10_svc_data {
  152. struct stratix10_svc_chan *chan;
  153. phys_addr_t paddr;
  154. size_t size;
  155. phys_addr_t paddr_output;
  156. size_t size_output;
  157. u32 command;
  158. u32 flag;
  159. u64 arg[3];
  160. };
  161. /**
  162. * struct stratix10_svc_async_handler - Asynchronous handler for Stratix10
  163. * service layer
  164. * @transaction_id: Unique identifier for the transaction
  165. * @achan: Pointer to the asynchronous channel structure
  166. * @cb_arg: Argument to be passed to the callback function
  167. * @cb: Callback function to be called upon completion
  168. * @msg: Pointer to the client message structure
  169. * @next: Node in the hash list
  170. * @res: Response structure to store result from the secure firmware
  171. *
  172. * This structure is used to handle asynchronous transactions in the
  173. * Stratix10 service layer. It maintains the necessary information
  174. * for processing and completing asynchronous requests.
  175. */
  176. struct stratix10_svc_async_handler {
  177. u8 transaction_id;
  178. struct stratix10_async_chan *achan;
  179. void *cb_arg;
  180. async_callback_t cb;
  181. struct stratix10_svc_client_msg *msg;
  182. struct hlist_node next;
  183. struct arm_smccc_1_2_regs res;
  184. };
  185. /**
  186. * struct stratix10_async_chan - Structure representing an asynchronous channel
  187. * @async_client_id: Unique client identifier for the asynchronous operation
  188. * @job_id_pool: Pointer to the job ID pool associated with this channel
  189. */
  190. struct stratix10_async_chan {
  191. unsigned long async_client_id;
  192. struct ida job_id_pool;
  193. };
  194. /**
  195. * struct stratix10_async_ctrl - Control structure for Stratix10
  196. * asynchronous operations
  197. * @initialized: Flag indicating whether the control structure has
  198. * been initialized
  199. * @invoke_fn: Function pointer for invoking Stratix10 service calls
  200. * to EL3 secure firmware
  201. * @async_id_pool: Pointer to the ID pool used for asynchronous
  202. * operations
  203. * @common_achan_refcount: Atomic reference count for the common
  204. * asynchronous channel usage
  205. * @common_async_chan: Pointer to the common asynchronous channel
  206. * structure
  207. * @trx_list_lock: Spinlock for protecting the transaction list
  208. * operations
  209. * @trx_list: Hash table for managing asynchronous transactions
  210. */
  211. struct stratix10_async_ctrl {
  212. bool initialized;
  213. void (*invoke_fn)(struct stratix10_async_ctrl *actrl,
  214. const struct arm_smccc_1_2_regs *args,
  215. struct arm_smccc_1_2_regs *res);
  216. struct ida async_id_pool;
  217. atomic_t common_achan_refcount;
  218. struct stratix10_async_chan *common_async_chan;
  219. /* spinlock to protect trx_list hash table */
  220. spinlock_t trx_list_lock;
  221. DECLARE_HASHTABLE(trx_list, ASYNC_TRX_HASH_BITS);
  222. };
  223. /**
  224. * struct stratix10_svc_controller - service controller
  225. * @dev: device
  226. * @chans: array of service channels
  227. * @num_chans: number of channels in 'chans' array
  228. * @num_active_client: number of active service client
  229. * @node: list management
  230. * @genpool: memory pool pointing to the memory region
  231. * @complete_status: state for completion
  232. * @invoke_fn: function to issue secure monitor call or hypervisor call
  233. * @svc: manages the list of client svc drivers
  234. * @sdm_lock: only allows a single command single response to SDM
  235. * @actrl: async control structure
  236. *
  237. * This struct is used to create communication channels for service clients, to
  238. * handle secure monitor or hypervisor call.
  239. */
  240. struct stratix10_svc_controller {
  241. struct device *dev;
  242. struct stratix10_svc_chan *chans;
  243. int num_chans;
  244. int num_active_client;
  245. struct list_head node;
  246. struct gen_pool *genpool;
  247. struct completion complete_status;
  248. svc_invoke_fn *invoke_fn;
  249. struct stratix10_svc *svc;
  250. struct mutex sdm_lock;
  251. struct stratix10_async_ctrl actrl;
  252. };
  253. /**
  254. * struct stratix10_svc_chan - service communication channel
  255. * @ctrl: pointer to service controller which is the provider of this channel
  256. * @scl: pointer to service client which owns the channel
  257. * @name: service client name associated with the channel
  258. * @task: pointer to the thread task which handles SMC or HVC call
  259. * @svc_fifo: a queue for storing service message data (separate fifo for every channel)
  260. * @svc_fifo_lock: protect access to service message data queue (locking pending fifo)
  261. * @lock: protect access to the channel
  262. * @async_chan: reference to asynchronous channel object for this channel
  263. *
  264. * This struct is used by service client to communicate with service layer.
  265. * Each service client has its own channel created by service controller.
  266. */
  267. struct stratix10_svc_chan {
  268. struct stratix10_svc_controller *ctrl;
  269. struct stratix10_svc_client *scl;
  270. char *name;
  271. struct task_struct *task;
  272. struct kfifo svc_fifo;
  273. spinlock_t svc_fifo_lock;
  274. spinlock_t lock;
  275. struct stratix10_async_chan *async_chan;
  276. };
  277. static LIST_HEAD(svc_ctrl);
  278. static LIST_HEAD(svc_data_mem);
  279. /*
  280. * svc_mem_lock protects access to the svc_data_mem list for
  281. * concurrent multi-client operations
  282. */
  283. static DEFINE_MUTEX(svc_mem_lock);
  284. /**
  285. * svc_pa_to_va() - translate physical address to virtual address
  286. * @addr: to be translated physical address
  287. *
  288. * Return: valid virtual address or NULL if the provided physical
  289. * address doesn't exist.
  290. */
  291. static void *svc_pa_to_va(unsigned long addr)
  292. {
  293. struct stratix10_svc_data_mem *pmem;
  294. pr_debug("claim back P-addr=0x%016x\n", (unsigned int)addr);
  295. guard(mutex)(&svc_mem_lock);
  296. list_for_each_entry(pmem, &svc_data_mem, node)
  297. if (pmem->paddr == addr)
  298. return pmem->vaddr;
  299. /* physical address is not found */
  300. return NULL;
  301. }
  302. /**
  303. * svc_thread_cmd_data_claim() - claim back buffer from the secure world
  304. * @ctrl: pointer to service layer controller
  305. * @p_data: pointer to service data structure
  306. * @cb_data: pointer to callback data structure to service client
  307. *
  308. * Claim back the submitted buffers from the secure world and pass buffer
  309. * back to service client (FPGA manager, etc) for reuse.
  310. */
  311. static void svc_thread_cmd_data_claim(struct stratix10_svc_controller *ctrl,
  312. struct stratix10_svc_data *p_data,
  313. struct stratix10_svc_cb_data *cb_data)
  314. {
  315. struct arm_smccc_res res;
  316. unsigned long timeout;
  317. reinit_completion(&ctrl->complete_status);
  318. timeout = msecs_to_jiffies(FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS);
  319. pr_debug("%s: claim back the submitted buffer\n", __func__);
  320. do {
  321. ctrl->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_COMPLETED_WRITE,
  322. 0, 0, 0, 0, 0, 0, 0, &res);
  323. if (res.a0 == INTEL_SIP_SMC_STATUS_OK) {
  324. if (!res.a1) {
  325. complete(&ctrl->complete_status);
  326. break;
  327. }
  328. cb_data->status = BIT(SVC_STATUS_BUFFER_DONE);
  329. cb_data->kaddr1 = svc_pa_to_va(res.a1);
  330. cb_data->kaddr2 = (res.a2) ?
  331. svc_pa_to_va(res.a2) : NULL;
  332. cb_data->kaddr3 = (res.a3) ?
  333. svc_pa_to_va(res.a3) : NULL;
  334. p_data->chan->scl->receive_cb(p_data->chan->scl,
  335. cb_data);
  336. } else {
  337. pr_debug("%s: secure world busy, polling again\n",
  338. __func__);
  339. }
  340. } while (res.a0 == INTEL_SIP_SMC_STATUS_OK ||
  341. res.a0 == INTEL_SIP_SMC_STATUS_BUSY ||
  342. wait_for_completion_timeout(&ctrl->complete_status, timeout));
  343. }
  344. /**
  345. * svc_thread_cmd_config_status() - check configuration status
  346. * @ctrl: pointer to service layer controller
  347. * @p_data: pointer to service data structure
  348. * @cb_data: pointer to callback data structure to service client
  349. *
  350. * Check whether the secure firmware at secure world has finished the FPGA
  351. * configuration, and then inform FPGA manager the configuration status.
  352. */
  353. static void svc_thread_cmd_config_status(struct stratix10_svc_controller *ctrl,
  354. struct stratix10_svc_data *p_data,
  355. struct stratix10_svc_cb_data *cb_data)
  356. {
  357. struct arm_smccc_res res;
  358. int count_in_sec;
  359. unsigned long a0, a1, a2;
  360. cb_data->kaddr1 = NULL;
  361. cb_data->kaddr2 = NULL;
  362. cb_data->kaddr3 = NULL;
  363. cb_data->status = BIT(SVC_STATUS_ERROR);
  364. pr_debug("%s: polling config status\n", __func__);
  365. a0 = INTEL_SIP_SMC_FPGA_CONFIG_ISDONE;
  366. a1 = (unsigned long)p_data->paddr;
  367. a2 = (unsigned long)p_data->size;
  368. if (p_data->command == COMMAND_POLL_SERVICE_STATUS)
  369. a0 = INTEL_SIP_SMC_SERVICE_COMPLETED;
  370. count_in_sec = FPGA_CONFIG_STATUS_TIMEOUT_SEC;
  371. while (count_in_sec) {
  372. ctrl->invoke_fn(a0, a1, a2, 0, 0, 0, 0, 0, &res);
  373. if ((res.a0 == INTEL_SIP_SMC_STATUS_OK) ||
  374. (res.a0 == INTEL_SIP_SMC_STATUS_ERROR) ||
  375. (res.a0 == INTEL_SIP_SMC_STATUS_REJECTED))
  376. break;
  377. /*
  378. * request is still in progress, wait one second then
  379. * poll again
  380. */
  381. msleep(1000);
  382. count_in_sec--;
  383. }
  384. if (!count_in_sec) {
  385. pr_err("%s: poll status timeout\n", __func__);
  386. cb_data->status = BIT(SVC_STATUS_BUSY);
  387. } else if (res.a0 == INTEL_SIP_SMC_STATUS_OK) {
  388. cb_data->status = BIT(SVC_STATUS_COMPLETED);
  389. cb_data->kaddr2 = (res.a2) ?
  390. svc_pa_to_va(res.a2) : NULL;
  391. cb_data->kaddr3 = (res.a3) ? &res.a3 : NULL;
  392. } else {
  393. pr_err("%s: poll status error\n", __func__);
  394. cb_data->kaddr1 = &res.a1;
  395. cb_data->kaddr2 = (res.a2) ?
  396. svc_pa_to_va(res.a2) : NULL;
  397. cb_data->kaddr3 = (res.a3) ? &res.a3 : NULL;
  398. cb_data->status = BIT(SVC_STATUS_ERROR);
  399. }
  400. p_data->chan->scl->receive_cb(p_data->chan->scl, cb_data);
  401. }
  402. /**
  403. * svc_thread_recv_status_ok() - handle the successful status
  404. * @p_data: pointer to service data structure
  405. * @cb_data: pointer to callback data structure to service client
  406. * @res: result from SMC or HVC call
  407. *
  408. * Send back the correspond status to the service clients.
  409. */
  410. static void svc_thread_recv_status_ok(struct stratix10_svc_data *p_data,
  411. struct stratix10_svc_cb_data *cb_data,
  412. struct arm_smccc_res res)
  413. {
  414. cb_data->kaddr1 = NULL;
  415. cb_data->kaddr2 = NULL;
  416. cb_data->kaddr3 = NULL;
  417. switch (p_data->command) {
  418. case COMMAND_RECONFIG:
  419. case COMMAND_RSU_UPDATE:
  420. case COMMAND_RSU_NOTIFY:
  421. case COMMAND_FCS_REQUEST_SERVICE:
  422. case COMMAND_FCS_SEND_CERTIFICATE:
  423. case COMMAND_FCS_DATA_ENCRYPTION:
  424. case COMMAND_FCS_DATA_DECRYPTION:
  425. cb_data->status = BIT(SVC_STATUS_OK);
  426. break;
  427. case COMMAND_RECONFIG_DATA_SUBMIT:
  428. cb_data->status = BIT(SVC_STATUS_BUFFER_SUBMITTED);
  429. break;
  430. case COMMAND_RECONFIG_STATUS:
  431. cb_data->status = BIT(SVC_STATUS_COMPLETED);
  432. break;
  433. case COMMAND_RSU_RETRY:
  434. case COMMAND_RSU_MAX_RETRY:
  435. case COMMAND_RSU_DCMF_STATUS:
  436. case COMMAND_FIRMWARE_VERSION:
  437. case COMMAND_HWMON_READTEMP:
  438. case COMMAND_HWMON_READVOLT:
  439. cb_data->status = BIT(SVC_STATUS_OK);
  440. cb_data->kaddr1 = &res.a1;
  441. break;
  442. case COMMAND_SMC_SVC_VERSION:
  443. cb_data->status = BIT(SVC_STATUS_OK);
  444. cb_data->kaddr1 = &res.a1;
  445. cb_data->kaddr2 = &res.a2;
  446. break;
  447. case COMMAND_RSU_DCMF_VERSION:
  448. cb_data->status = BIT(SVC_STATUS_OK);
  449. cb_data->kaddr1 = &res.a1;
  450. cb_data->kaddr2 = &res.a2;
  451. break;
  452. case COMMAND_FCS_RANDOM_NUMBER_GEN:
  453. case COMMAND_FCS_GET_PROVISION_DATA:
  454. case COMMAND_POLL_SERVICE_STATUS:
  455. cb_data->status = BIT(SVC_STATUS_OK);
  456. cb_data->kaddr1 = &res.a1;
  457. cb_data->kaddr2 = svc_pa_to_va(res.a2);
  458. cb_data->kaddr3 = &res.a3;
  459. break;
  460. case COMMAND_MBOX_SEND_CMD:
  461. cb_data->status = BIT(SVC_STATUS_OK);
  462. cb_data->kaddr1 = &res.a1;
  463. /* SDM return size in u8. Convert size to u32 word */
  464. res.a2 = res.a2 * BYTE_TO_WORD_SIZE;
  465. cb_data->kaddr2 = &res.a2;
  466. break;
  467. default:
  468. pr_warn("it shouldn't happen\n");
  469. break;
  470. }
  471. pr_debug("%s: call receive_cb\n", __func__);
  472. p_data->chan->scl->receive_cb(p_data->chan->scl, cb_data);
  473. }
  474. /**
  475. * svc_normal_to_secure_thread() - the function to run in the kthread
  476. * @data: data pointer for kthread function
  477. *
  478. * Service layer driver creates stratix10_svc_smc_hvc_call kthread on CPU
  479. * node 0, its function stratix10_svc_secure_call_thread is used to handle
  480. * SMC or HVC calls between kernel driver and secure monitor software.
  481. *
  482. * Return: 0 for success or -ENOMEM on error.
  483. */
  484. static int svc_normal_to_secure_thread(void *data)
  485. {
  486. struct stratix10_svc_chan *chan = (struct stratix10_svc_chan *)data;
  487. struct stratix10_svc_controller *ctrl = chan->ctrl;
  488. struct stratix10_svc_data *pdata = NULL;
  489. struct stratix10_svc_cb_data *cbdata = NULL;
  490. struct arm_smccc_res res;
  491. unsigned long a0, a1, a2, a3, a4, a5, a6, a7;
  492. int ret_fifo = 0;
  493. pdata = kmalloc_obj(*pdata);
  494. if (!pdata)
  495. return -ENOMEM;
  496. cbdata = kmalloc_obj(*cbdata);
  497. if (!cbdata) {
  498. kfree(pdata);
  499. return -ENOMEM;
  500. }
  501. /* default set, to remove build warning */
  502. a0 = INTEL_SIP_SMC_FPGA_CONFIG_LOOPBACK;
  503. a1 = 0;
  504. a2 = 0;
  505. a3 = 0;
  506. a4 = 0;
  507. a5 = 0;
  508. a6 = 0;
  509. a7 = 0;
  510. pr_debug("%s: %s: Thread is running!\n", __func__, chan->name);
  511. while (!kthread_should_stop()) {
  512. ret_fifo = kfifo_out_spinlocked(&chan->svc_fifo,
  513. pdata, sizeof(*pdata),
  514. &chan->svc_fifo_lock);
  515. if (!ret_fifo)
  516. continue;
  517. pr_debug("get from FIFO pa=0x%016x, command=%u, size=%u\n",
  518. (unsigned int)pdata->paddr, pdata->command,
  519. (unsigned int)pdata->size);
  520. /* SDM can only process one command at a time */
  521. pr_debug("%s: %s: Thread is waiting for mutex!\n",
  522. __func__, chan->name);
  523. if (mutex_lock_interruptible(&ctrl->sdm_lock)) {
  524. /* item already dequeued; notify client to unblock it */
  525. cbdata->status = BIT(SVC_STATUS_ERROR);
  526. cbdata->kaddr1 = NULL;
  527. cbdata->kaddr2 = NULL;
  528. cbdata->kaddr3 = NULL;
  529. if (pdata->chan->scl)
  530. pdata->chan->scl->receive_cb(pdata->chan->scl,
  531. cbdata);
  532. break;
  533. }
  534. switch (pdata->command) {
  535. case COMMAND_RECONFIG_DATA_CLAIM:
  536. svc_thread_cmd_data_claim(ctrl, pdata, cbdata);
  537. mutex_unlock(&ctrl->sdm_lock);
  538. continue;
  539. case COMMAND_RECONFIG:
  540. a0 = INTEL_SIP_SMC_FPGA_CONFIG_START;
  541. pr_debug("conf_type=%u\n", (unsigned int)pdata->flag);
  542. a1 = pdata->flag;
  543. a2 = 0;
  544. break;
  545. case COMMAND_RECONFIG_DATA_SUBMIT:
  546. a0 = INTEL_SIP_SMC_FPGA_CONFIG_WRITE;
  547. a1 = (unsigned long)pdata->paddr;
  548. a2 = (unsigned long)pdata->size;
  549. break;
  550. case COMMAND_RECONFIG_STATUS:
  551. a0 = INTEL_SIP_SMC_FPGA_CONFIG_ISDONE;
  552. a1 = 0;
  553. a2 = 0;
  554. break;
  555. case COMMAND_RSU_STATUS:
  556. a0 = INTEL_SIP_SMC_RSU_STATUS;
  557. a1 = 0;
  558. a2 = 0;
  559. break;
  560. case COMMAND_RSU_UPDATE:
  561. a0 = INTEL_SIP_SMC_RSU_UPDATE;
  562. a1 = pdata->arg[0];
  563. a2 = 0;
  564. break;
  565. case COMMAND_RSU_NOTIFY:
  566. a0 = INTEL_SIP_SMC_RSU_NOTIFY;
  567. a1 = pdata->arg[0];
  568. a2 = 0;
  569. break;
  570. case COMMAND_RSU_RETRY:
  571. a0 = INTEL_SIP_SMC_RSU_RETRY_COUNTER;
  572. a1 = 0;
  573. a2 = 0;
  574. break;
  575. case COMMAND_RSU_MAX_RETRY:
  576. a0 = INTEL_SIP_SMC_RSU_MAX_RETRY;
  577. a1 = 0;
  578. a2 = 0;
  579. break;
  580. case COMMAND_RSU_DCMF_VERSION:
  581. a0 = INTEL_SIP_SMC_RSU_DCMF_VERSION;
  582. a1 = 0;
  583. a2 = 0;
  584. break;
  585. case COMMAND_FIRMWARE_VERSION:
  586. a0 = INTEL_SIP_SMC_FIRMWARE_VERSION;
  587. a1 = 0;
  588. a2 = 0;
  589. break;
  590. /* for FCS */
  591. case COMMAND_FCS_DATA_ENCRYPTION:
  592. a0 = INTEL_SIP_SMC_FCS_CRYPTION;
  593. a1 = 1;
  594. a2 = (unsigned long)pdata->paddr;
  595. a3 = (unsigned long)pdata->size;
  596. a4 = (unsigned long)pdata->paddr_output;
  597. a5 = (unsigned long)pdata->size_output;
  598. break;
  599. case COMMAND_FCS_DATA_DECRYPTION:
  600. a0 = INTEL_SIP_SMC_FCS_CRYPTION;
  601. a1 = 0;
  602. a2 = (unsigned long)pdata->paddr;
  603. a3 = (unsigned long)pdata->size;
  604. a4 = (unsigned long)pdata->paddr_output;
  605. a5 = (unsigned long)pdata->size_output;
  606. break;
  607. case COMMAND_FCS_RANDOM_NUMBER_GEN:
  608. a0 = INTEL_SIP_SMC_FCS_RANDOM_NUMBER;
  609. a1 = (unsigned long)pdata->paddr;
  610. a2 = 0;
  611. break;
  612. case COMMAND_FCS_REQUEST_SERVICE:
  613. a0 = INTEL_SIP_SMC_FCS_SERVICE_REQUEST;
  614. a1 = (unsigned long)pdata->paddr;
  615. a2 = (unsigned long)pdata->size;
  616. break;
  617. case COMMAND_FCS_SEND_CERTIFICATE:
  618. a0 = INTEL_SIP_SMC_FCS_SEND_CERTIFICATE;
  619. a1 = (unsigned long)pdata->paddr;
  620. a2 = (unsigned long)pdata->size;
  621. break;
  622. case COMMAND_FCS_GET_PROVISION_DATA:
  623. a0 = INTEL_SIP_SMC_FCS_GET_PROVISION_DATA;
  624. a1 = (unsigned long)pdata->paddr;
  625. a2 = 0;
  626. break;
  627. /* for HWMON */
  628. case COMMAND_HWMON_READTEMP:
  629. a0 = INTEL_SIP_SMC_HWMON_READTEMP;
  630. a1 = pdata->arg[0];
  631. a2 = 0;
  632. break;
  633. case COMMAND_HWMON_READVOLT:
  634. a0 = INTEL_SIP_SMC_HWMON_READVOLT;
  635. a1 = pdata->arg[0];
  636. a2 = 0;
  637. break;
  638. /* for polling */
  639. case COMMAND_POLL_SERVICE_STATUS:
  640. a0 = INTEL_SIP_SMC_SERVICE_COMPLETED;
  641. a1 = (unsigned long)pdata->paddr;
  642. a2 = (unsigned long)pdata->size;
  643. break;
  644. case COMMAND_RSU_DCMF_STATUS:
  645. a0 = INTEL_SIP_SMC_RSU_DCMF_STATUS;
  646. a1 = 0;
  647. a2 = 0;
  648. break;
  649. case COMMAND_SMC_SVC_VERSION:
  650. a0 = INTEL_SIP_SMC_SVC_VERSION;
  651. a1 = 0;
  652. a2 = 0;
  653. break;
  654. case COMMAND_MBOX_SEND_CMD:
  655. a0 = INTEL_SIP_SMC_MBOX_SEND_CMD;
  656. a1 = pdata->arg[0];
  657. a2 = (unsigned long)pdata->paddr;
  658. a3 = (unsigned long)pdata->size / BYTE_TO_WORD_SIZE;
  659. a4 = pdata->arg[1];
  660. a5 = (unsigned long)pdata->paddr_output;
  661. a6 = (unsigned long)pdata->size_output / BYTE_TO_WORD_SIZE;
  662. break;
  663. default:
  664. pr_warn("it shouldn't happen\n");
  665. mutex_unlock(&ctrl->sdm_lock);
  666. continue;
  667. }
  668. pr_debug("%s: %s: before SMC call -- a0=0x%016x a1=0x%016x",
  669. __func__, chan->name,
  670. (unsigned int)a0,
  671. (unsigned int)a1);
  672. pr_debug(" a2=0x%016x\n", (unsigned int)a2);
  673. pr_debug(" a3=0x%016x\n", (unsigned int)a3);
  674. pr_debug(" a4=0x%016x\n", (unsigned int)a4);
  675. pr_debug(" a5=0x%016x\n", (unsigned int)a5);
  676. ctrl->invoke_fn(a0, a1, a2, a3, a4, a5, a6, a7, &res);
  677. pr_debug("%s: %s: after SMC call -- res.a0=0x%016x",
  678. __func__, chan->name, (unsigned int)res.a0);
  679. pr_debug(" res.a1=0x%016x, res.a2=0x%016x",
  680. (unsigned int)res.a1, (unsigned int)res.a2);
  681. pr_debug(" res.a3=0x%016x\n", (unsigned int)res.a3);
  682. if (pdata->command == COMMAND_RSU_STATUS) {
  683. if (res.a0 == INTEL_SIP_SMC_RSU_ERROR)
  684. cbdata->status = BIT(SVC_STATUS_ERROR);
  685. else
  686. cbdata->status = BIT(SVC_STATUS_OK);
  687. cbdata->kaddr1 = &res;
  688. cbdata->kaddr2 = NULL;
  689. cbdata->kaddr3 = NULL;
  690. pdata->chan->scl->receive_cb(pdata->chan->scl, cbdata);
  691. mutex_unlock(&ctrl->sdm_lock);
  692. continue;
  693. }
  694. switch (res.a0) {
  695. case INTEL_SIP_SMC_STATUS_OK:
  696. svc_thread_recv_status_ok(pdata, cbdata, res);
  697. break;
  698. case INTEL_SIP_SMC_STATUS_BUSY:
  699. switch (pdata->command) {
  700. case COMMAND_RECONFIG_DATA_SUBMIT:
  701. svc_thread_cmd_data_claim(ctrl,
  702. pdata, cbdata);
  703. break;
  704. case COMMAND_RECONFIG_STATUS:
  705. case COMMAND_POLL_SERVICE_STATUS:
  706. svc_thread_cmd_config_status(ctrl,
  707. pdata, cbdata);
  708. break;
  709. default:
  710. pr_warn("it shouldn't happen\n");
  711. break;
  712. }
  713. break;
  714. case INTEL_SIP_SMC_STATUS_REJECTED:
  715. pr_debug("%s: STATUS_REJECTED\n", __func__);
  716. /* for FCS */
  717. switch (pdata->command) {
  718. case COMMAND_FCS_REQUEST_SERVICE:
  719. case COMMAND_FCS_SEND_CERTIFICATE:
  720. case COMMAND_FCS_GET_PROVISION_DATA:
  721. case COMMAND_FCS_DATA_ENCRYPTION:
  722. case COMMAND_FCS_DATA_DECRYPTION:
  723. case COMMAND_FCS_RANDOM_NUMBER_GEN:
  724. case COMMAND_MBOX_SEND_CMD:
  725. cbdata->status = BIT(SVC_STATUS_INVALID_PARAM);
  726. cbdata->kaddr1 = NULL;
  727. cbdata->kaddr2 = NULL;
  728. cbdata->kaddr3 = NULL;
  729. pdata->chan->scl->receive_cb(pdata->chan->scl,
  730. cbdata);
  731. break;
  732. }
  733. break;
  734. case INTEL_SIP_SMC_STATUS_ERROR:
  735. case INTEL_SIP_SMC_RSU_ERROR:
  736. pr_err("%s: STATUS_ERROR\n", __func__);
  737. cbdata->status = BIT(SVC_STATUS_ERROR);
  738. cbdata->kaddr1 = &res.a1;
  739. cbdata->kaddr2 = (res.a2) ?
  740. svc_pa_to_va(res.a2) : NULL;
  741. cbdata->kaddr3 = (res.a3) ? &res.a3 : NULL;
  742. pdata->chan->scl->receive_cb(pdata->chan->scl, cbdata);
  743. break;
  744. default:
  745. pr_warn("Secure firmware doesn't support...\n");
  746. /*
  747. * be compatible with older version firmware which
  748. * doesn't support newer RSU commands
  749. */
  750. if ((pdata->command != COMMAND_RSU_UPDATE) &&
  751. (pdata->command != COMMAND_RSU_STATUS)) {
  752. cbdata->status =
  753. BIT(SVC_STATUS_NO_SUPPORT);
  754. cbdata->kaddr1 = NULL;
  755. cbdata->kaddr2 = NULL;
  756. cbdata->kaddr3 = NULL;
  757. pdata->chan->scl->receive_cb(
  758. pdata->chan->scl, cbdata);
  759. }
  760. break;
  761. }
  762. mutex_unlock(&ctrl->sdm_lock);
  763. }
  764. kfree(cbdata);
  765. kfree(pdata);
  766. return 0;
  767. }
  768. /**
  769. * svc_normal_to_secure_shm_thread() - the function to run in the kthread
  770. * @data: data pointer for kthread function
  771. *
  772. * Service layer driver creates stratix10_svc_smc_hvc_shm kthread on CPU
  773. * node 0, its function stratix10_svc_secure_shm_thread is used to query the
  774. * physical address of memory block reserved by secure monitor software at
  775. * secure world.
  776. *
  777. * svc_normal_to_secure_shm_thread() terminates directly since it is a
  778. * standlone thread for which no one will call kthread_stop() or return when
  779. * 'kthread_should_stop()' is true.
  780. */
  781. static int svc_normal_to_secure_shm_thread(void *data)
  782. {
  783. struct stratix10_svc_sh_memory
  784. *sh_mem = (struct stratix10_svc_sh_memory *)data;
  785. struct arm_smccc_res res;
  786. /* SMC or HVC call to get shared memory info from secure world */
  787. sh_mem->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_GET_MEM,
  788. 0, 0, 0, 0, 0, 0, 0, &res);
  789. if (res.a0 == INTEL_SIP_SMC_STATUS_OK) {
  790. sh_mem->addr = res.a1;
  791. sh_mem->size = res.a2;
  792. } else {
  793. pr_err("%s: after SMC call -- res.a0=0x%016x", __func__,
  794. (unsigned int)res.a0);
  795. sh_mem->addr = 0;
  796. sh_mem->size = 0;
  797. }
  798. complete(&sh_mem->sync_complete);
  799. return 0;
  800. }
  801. /**
  802. * svc_get_sh_memory() - get memory block reserved by secure monitor SW
  803. * @pdev: pointer to service layer device
  804. * @sh_memory: pointer to service shared memory structure
  805. *
  806. * Return: zero for successfully getting the physical address of memory block
  807. * reserved by secure monitor software, or negative value on error.
  808. */
  809. static int svc_get_sh_memory(struct platform_device *pdev,
  810. struct stratix10_svc_sh_memory *sh_memory)
  811. {
  812. struct device *dev = &pdev->dev;
  813. struct task_struct *sh_memory_task;
  814. unsigned int cpu = 0;
  815. init_completion(&sh_memory->sync_complete);
  816. /* smc or hvc call happens on cpu 0 bound kthread */
  817. sh_memory_task = kthread_create_on_node(svc_normal_to_secure_shm_thread,
  818. (void *)sh_memory,
  819. cpu_to_node(cpu),
  820. "svc_smc_hvc_shm_thread");
  821. if (IS_ERR(sh_memory_task)) {
  822. dev_err(dev, "fail to create stratix10_svc_smc_shm_thread\n");
  823. return -EINVAL;
  824. }
  825. wake_up_process(sh_memory_task);
  826. if (!wait_for_completion_timeout(&sh_memory->sync_complete, 10 * HZ)) {
  827. dev_err(dev,
  828. "timeout to get sh-memory paras from secure world\n");
  829. return -ETIMEDOUT;
  830. }
  831. if (!sh_memory->addr || !sh_memory->size) {
  832. dev_err(dev,
  833. "failed to get shared memory info from secure world\n");
  834. return -ENOMEM;
  835. }
  836. dev_dbg(dev, "SM software provides paddr: 0x%016x, size: 0x%08x\n",
  837. (unsigned int)sh_memory->addr,
  838. (unsigned int)sh_memory->size);
  839. return 0;
  840. }
  841. /**
  842. * svc_create_memory_pool() - create a memory pool from reserved memory block
  843. * @pdev: pointer to service layer device
  844. * @sh_memory: pointer to service shared memory structure
  845. *
  846. * Return: pool allocated from reserved memory block or ERR_PTR() on error.
  847. */
  848. static struct gen_pool *
  849. svc_create_memory_pool(struct platform_device *pdev,
  850. struct stratix10_svc_sh_memory *sh_memory)
  851. {
  852. struct device *dev = &pdev->dev;
  853. struct gen_pool *genpool;
  854. unsigned long vaddr;
  855. phys_addr_t paddr;
  856. size_t size;
  857. phys_addr_t begin;
  858. phys_addr_t end;
  859. void *va;
  860. size_t page_mask = PAGE_SIZE - 1;
  861. int min_alloc_order = 3;
  862. int ret;
  863. begin = roundup(sh_memory->addr, PAGE_SIZE);
  864. end = rounddown(sh_memory->addr + sh_memory->size, PAGE_SIZE);
  865. paddr = begin;
  866. size = end - begin;
  867. va = devm_memremap(dev, paddr, size, MEMREMAP_WC);
  868. if (IS_ERR(va)) {
  869. dev_err(dev, "fail to remap shared memory\n");
  870. return ERR_PTR(-EINVAL);
  871. }
  872. vaddr = (unsigned long)va;
  873. dev_dbg(dev,
  874. "reserved memory vaddr: %p, paddr: 0x%16x size: 0x%8x\n",
  875. va, (unsigned int)paddr, (unsigned int)size);
  876. if ((vaddr & page_mask) || (paddr & page_mask) ||
  877. (size & page_mask)) {
  878. dev_err(dev, "page is not aligned\n");
  879. return ERR_PTR(-EINVAL);
  880. }
  881. genpool = gen_pool_create(min_alloc_order, -1);
  882. if (!genpool) {
  883. dev_err(dev, "fail to create genpool\n");
  884. return ERR_PTR(-ENOMEM);
  885. }
  886. gen_pool_set_algo(genpool, gen_pool_best_fit, NULL);
  887. ret = gen_pool_add_virt(genpool, vaddr, paddr, size, -1);
  888. if (ret) {
  889. dev_err(dev, "fail to add memory chunk to the pool\n");
  890. gen_pool_destroy(genpool);
  891. return ERR_PTR(ret);
  892. }
  893. return genpool;
  894. }
  895. /**
  896. * svc_smccc_smc() - secure monitor call between normal and secure world
  897. * @a0: argument passed in registers 0
  898. * @a1: argument passed in registers 1
  899. * @a2: argument passed in registers 2
  900. * @a3: argument passed in registers 3
  901. * @a4: argument passed in registers 4
  902. * @a5: argument passed in registers 5
  903. * @a6: argument passed in registers 6
  904. * @a7: argument passed in registers 7
  905. * @res: result values from register 0 to 3
  906. */
  907. static void svc_smccc_smc(unsigned long a0, unsigned long a1,
  908. unsigned long a2, unsigned long a3,
  909. unsigned long a4, unsigned long a5,
  910. unsigned long a6, unsigned long a7,
  911. struct arm_smccc_res *res)
  912. {
  913. arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  914. }
  915. /**
  916. * svc_smccc_hvc() - hypervisor call between normal and secure world
  917. * @a0: argument passed in registers 0
  918. * @a1: argument passed in registers 1
  919. * @a2: argument passed in registers 2
  920. * @a3: argument passed in registers 3
  921. * @a4: argument passed in registers 4
  922. * @a5: argument passed in registers 5
  923. * @a6: argument passed in registers 6
  924. * @a7: argument passed in registers 7
  925. * @res: result values from register 0 to 3
  926. */
  927. static void svc_smccc_hvc(unsigned long a0, unsigned long a1,
  928. unsigned long a2, unsigned long a3,
  929. unsigned long a4, unsigned long a5,
  930. unsigned long a6, unsigned long a7,
  931. struct arm_smccc_res *res)
  932. {
  933. arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  934. }
  935. /**
  936. * get_invoke_func() - invoke SMC or HVC call
  937. * @dev: pointer to device
  938. *
  939. * Return: function pointer to svc_smccc_smc or svc_smccc_hvc.
  940. */
  941. static svc_invoke_fn *get_invoke_func(struct device *dev)
  942. {
  943. const char *method;
  944. if (of_property_read_string(dev->of_node, "method", &method)) {
  945. dev_warn(dev, "missing \"method\" property\n");
  946. return ERR_PTR(-ENXIO);
  947. }
  948. if (!strcmp(method, "smc"))
  949. return svc_smccc_smc;
  950. if (!strcmp(method, "hvc"))
  951. return svc_smccc_hvc;
  952. dev_warn(dev, "invalid \"method\" property: %s\n", method);
  953. return ERR_PTR(-EINVAL);
  954. }
  955. /**
  956. * stratix10_svc_request_channel_byname() - request a service channel
  957. * @client: pointer to service client
  958. * @name: service client name
  959. *
  960. * This function is used by service client to request a service channel.
  961. *
  962. * Return: a pointer to channel assigned to the client on success,
  963. * or ERR_PTR() on error.
  964. */
  965. struct stratix10_svc_chan *stratix10_svc_request_channel_byname(
  966. struct stratix10_svc_client *client, const char *name)
  967. {
  968. struct device *dev = client->dev;
  969. struct stratix10_svc_controller *controller;
  970. struct stratix10_svc_chan *chan = NULL;
  971. unsigned long flag;
  972. int i;
  973. /* if probe was called after client's, or error on probe */
  974. if (list_empty(&svc_ctrl))
  975. return ERR_PTR(-EPROBE_DEFER);
  976. controller = list_first_entry(&svc_ctrl,
  977. struct stratix10_svc_controller, node);
  978. for (i = 0; i < SVC_NUM_CHANNEL; i++) {
  979. if (!strcmp(controller->chans[i].name, name)) {
  980. chan = &controller->chans[i];
  981. break;
  982. }
  983. }
  984. /* if there was no channel match */
  985. if (i == SVC_NUM_CHANNEL) {
  986. dev_err(dev, "%s: channel not allocated\n", __func__);
  987. return ERR_PTR(-EINVAL);
  988. }
  989. if (chan->scl || !try_module_get(controller->dev->driver->owner)) {
  990. dev_dbg(dev, "%s: svc not free\n", __func__);
  991. return ERR_PTR(-EBUSY);
  992. }
  993. spin_lock_irqsave(&chan->lock, flag);
  994. chan->scl = client;
  995. chan->ctrl->num_active_client++;
  996. spin_unlock_irqrestore(&chan->lock, flag);
  997. return chan;
  998. }
  999. EXPORT_SYMBOL_GPL(stratix10_svc_request_channel_byname);
  1000. /**
  1001. * stratix10_svc_add_async_client - Add an asynchronous client to the
  1002. * Stratix10 service channel.
  1003. * @chan: Pointer to the Stratix10 service channel structure.
  1004. * @use_unique_clientid: Boolean flag indicating whether to use a
  1005. * unique client ID.
  1006. *
  1007. * This function adds an asynchronous client to the specified
  1008. * Stratix10 service channel. If the `use_unique_clientid` flag is
  1009. * set to true, a unique client ID is allocated for the asynchronous
  1010. * channel. Otherwise, a common asynchronous channel is used.
  1011. *
  1012. * Return: 0 on success, or a negative error code on failure:
  1013. * -EINVAL if the channel is NULL or the async controller is
  1014. * not initialized.
  1015. * -EALREADY if the async channel is already allocated.
  1016. * -ENOMEM if memory allocation fails.
  1017. * Other negative values if ID allocation fails.
  1018. */
  1019. int stratix10_svc_add_async_client(struct stratix10_svc_chan *chan,
  1020. bool use_unique_clientid)
  1021. {
  1022. struct stratix10_svc_controller *ctrl;
  1023. struct stratix10_async_ctrl *actrl;
  1024. struct stratix10_async_chan *achan;
  1025. int ret = 0;
  1026. if (!chan)
  1027. return -EINVAL;
  1028. ctrl = chan->ctrl;
  1029. actrl = &ctrl->actrl;
  1030. if (!actrl->initialized) {
  1031. dev_err(ctrl->dev, "Async controller not initialized\n");
  1032. return -EINVAL;
  1033. }
  1034. if (chan->async_chan) {
  1035. dev_err(ctrl->dev, "async channel already allocated\n");
  1036. return -EALREADY;
  1037. }
  1038. if (use_unique_clientid &&
  1039. atomic_read(&actrl->common_achan_refcount) > 0) {
  1040. chan->async_chan = actrl->common_async_chan;
  1041. atomic_inc(&actrl->common_achan_refcount);
  1042. return 0;
  1043. }
  1044. achan = kzalloc_obj(*achan);
  1045. if (!achan)
  1046. return -ENOMEM;
  1047. ida_init(&achan->job_id_pool);
  1048. ret = ida_alloc_max(&actrl->async_id_pool, MAX_SDM_CLIENT_IDS,
  1049. GFP_KERNEL);
  1050. if (ret < 0) {
  1051. dev_err(ctrl->dev,
  1052. "Failed to allocate async client id\n");
  1053. ida_destroy(&achan->job_id_pool);
  1054. kfree(achan);
  1055. return ret;
  1056. }
  1057. achan->async_client_id = ret;
  1058. chan->async_chan = achan;
  1059. if (use_unique_clientid &&
  1060. atomic_read(&actrl->common_achan_refcount) == 0) {
  1061. actrl->common_async_chan = achan;
  1062. atomic_inc(&actrl->common_achan_refcount);
  1063. }
  1064. return 0;
  1065. }
  1066. EXPORT_SYMBOL_GPL(stratix10_svc_add_async_client);
  1067. /**
  1068. * stratix10_svc_remove_async_client - Remove an asynchronous client
  1069. * from the Stratix10 service
  1070. * channel.
  1071. * @chan: Pointer to the Stratix10 service channel structure.
  1072. *
  1073. * This function removes an asynchronous client associated with the
  1074. * given service channel. It checks if the channel and the
  1075. * asynchronous channel are valid, and then proceeds to decrement
  1076. * the reference count for the common asynchronous channel if
  1077. * applicable. If the reference count reaches zero, it destroys the
  1078. * job ID pool and deallocates the asynchronous client ID. For
  1079. * non-common asynchronous channels, it directly destroys the job ID
  1080. * pool, deallocates the asynchronous client ID, and frees the
  1081. * memory allocated for the asynchronous channel.
  1082. *
  1083. * Return: 0 on success, -EINVAL if the channel or asynchronous
  1084. * channel is invalid.
  1085. */
  1086. int stratix10_svc_remove_async_client(struct stratix10_svc_chan *chan)
  1087. {
  1088. struct stratix10_svc_controller *ctrl;
  1089. struct stratix10_async_ctrl *actrl;
  1090. struct stratix10_async_chan *achan;
  1091. if (!chan)
  1092. return -EINVAL;
  1093. ctrl = chan->ctrl;
  1094. actrl = &ctrl->actrl;
  1095. achan = chan->async_chan;
  1096. if (!achan) {
  1097. dev_err(ctrl->dev, "async channel not allocated\n");
  1098. return -EINVAL;
  1099. }
  1100. if (achan == actrl->common_async_chan) {
  1101. atomic_dec(&actrl->common_achan_refcount);
  1102. if (atomic_read(&actrl->common_achan_refcount) == 0) {
  1103. ida_destroy(&achan->job_id_pool);
  1104. ida_free(&actrl->async_id_pool,
  1105. achan->async_client_id);
  1106. kfree(achan);
  1107. actrl->common_async_chan = NULL;
  1108. }
  1109. } else {
  1110. ida_destroy(&achan->job_id_pool);
  1111. ida_free(&actrl->async_id_pool, achan->async_client_id);
  1112. kfree(achan);
  1113. }
  1114. chan->async_chan = NULL;
  1115. return 0;
  1116. }
  1117. EXPORT_SYMBOL_GPL(stratix10_svc_remove_async_client);
  1118. /**
  1119. * stratix10_svc_async_send - Send an asynchronous message to the
  1120. * Stratix10 service
  1121. * @chan: Pointer to the service channel structure
  1122. * @msg: Pointer to the message to be sent
  1123. * @handler: Pointer to the handler for the asynchronous message
  1124. * used by caller for later reference.
  1125. * @cb: Callback function to be called upon completion
  1126. * @cb_arg: Argument to be passed to the callback function
  1127. *
  1128. * This function sends an asynchronous message to the SDM mailbox in
  1129. * EL3 secure firmware. It performs various checks and setups,
  1130. * including allocating a job ID, setting up the transaction ID and
  1131. * packaging it to El3 firmware. The function handles different
  1132. * commands by setting up the appropriate arguments for the SMC call.
  1133. * If the SMC call is successful, the handler is set up and the
  1134. * function returns 0. If the SMC call fails, appropriate error
  1135. * handling is performed along with cleanup of resources.
  1136. *
  1137. * Return: 0 on success, -EINVAL for invalid argument, -ENOMEM if
  1138. * memory is not available, -EAGAIN if EL3 firmware is busy, -EBADF
  1139. * if the message is rejected by EL3 firmware and -EIO on other
  1140. * errors from EL3 firmware.
  1141. */
  1142. int stratix10_svc_async_send(struct stratix10_svc_chan *chan, void *msg,
  1143. void **handler, async_callback_t cb, void *cb_arg)
  1144. {
  1145. struct arm_smccc_1_2_regs args = { 0 }, res = { 0 };
  1146. struct stratix10_svc_async_handler *handle = NULL;
  1147. struct stratix10_svc_client_msg *p_msg =
  1148. (struct stratix10_svc_client_msg *)msg;
  1149. struct stratix10_svc_controller *ctrl;
  1150. struct stratix10_async_ctrl *actrl;
  1151. struct stratix10_async_chan *achan;
  1152. int ret = 0;
  1153. if (!chan || !msg || !handler)
  1154. return -EINVAL;
  1155. achan = chan->async_chan;
  1156. ctrl = chan->ctrl;
  1157. actrl = &ctrl->actrl;
  1158. if (!actrl->initialized) {
  1159. dev_err(ctrl->dev, "Async controller not initialized\n");
  1160. return -EINVAL;
  1161. }
  1162. if (!achan) {
  1163. dev_err(ctrl->dev, "Async channel not allocated\n");
  1164. return -EINVAL;
  1165. }
  1166. handle = kzalloc(sizeof(*handle), GFP_KERNEL);
  1167. if (!handle)
  1168. return -ENOMEM;
  1169. ret = ida_alloc_max(&achan->job_id_pool, MAX_SDM_JOB_IDS,
  1170. GFP_KERNEL);
  1171. if (ret < 0) {
  1172. dev_err(ctrl->dev, "Failed to allocate job id\n");
  1173. kfree(handle);
  1174. return -ENOMEM;
  1175. }
  1176. handle->transaction_id =
  1177. STRATIX10_SET_TRANSACTIONID(achan->async_client_id, ret);
  1178. handle->cb = cb;
  1179. handle->msg = p_msg;
  1180. handle->cb_arg = cb_arg;
  1181. handle->achan = achan;
  1182. /*set the transaction jobid in args.a1*/
  1183. args.a1 =
  1184. STRATIX10_SIP_SMC_SET_TRANSACTIONID_X1(handle->transaction_id);
  1185. switch (p_msg->command) {
  1186. case COMMAND_RSU_GET_SPT_TABLE:
  1187. args.a0 = INTEL_SIP_SMC_ASYNC_RSU_GET_SPT;
  1188. break;
  1189. case COMMAND_RSU_STATUS:
  1190. args.a0 = INTEL_SIP_SMC_ASYNC_RSU_GET_ERROR_STATUS;
  1191. break;
  1192. case COMMAND_RSU_NOTIFY:
  1193. args.a0 = INTEL_SIP_SMC_ASYNC_RSU_NOTIFY;
  1194. args.a2 = p_msg->arg[0];
  1195. break;
  1196. default:
  1197. dev_err(ctrl->dev, "Invalid command ,%d\n", p_msg->command);
  1198. ret = -EINVAL;
  1199. goto deallocate_id;
  1200. }
  1201. /**
  1202. * There is a chance that during the execution of async_send()
  1203. * in one core, an interrupt might be received in another core;
  1204. * to mitigate this we are adding the handle to the DB and then
  1205. * send the smc call. If the smc call is rejected or busy then
  1206. * we will deallocate the handle for the client to retry again.
  1207. */
  1208. scoped_guard(spinlock_bh, &actrl->trx_list_lock) {
  1209. hash_add(actrl->trx_list, &handle->next,
  1210. handle->transaction_id);
  1211. }
  1212. actrl->invoke_fn(actrl, &args, &res);
  1213. switch (res.a0) {
  1214. case INTEL_SIP_SMC_STATUS_OK:
  1215. dev_dbg(ctrl->dev,
  1216. "Async message sent with transaction_id 0x%02x\n",
  1217. handle->transaction_id);
  1218. *handler = handle;
  1219. return 0;
  1220. case INTEL_SIP_SMC_STATUS_BUSY:
  1221. dev_warn(ctrl->dev, "Mailbox is busy, try after some time\n");
  1222. ret = -EAGAIN;
  1223. break;
  1224. case INTEL_SIP_SMC_STATUS_REJECTED:
  1225. dev_err(ctrl->dev, "Async message rejected\n");
  1226. ret = -EBADF;
  1227. break;
  1228. default:
  1229. dev_err(ctrl->dev,
  1230. "Failed to send async message ,got status as %ld\n",
  1231. res.a0);
  1232. ret = -EIO;
  1233. }
  1234. scoped_guard(spinlock_bh, &actrl->trx_list_lock) {
  1235. hash_del(&handle->next);
  1236. }
  1237. deallocate_id:
  1238. ida_free(&achan->job_id_pool,
  1239. STRATIX10_GET_JOBID(handle->transaction_id));
  1240. kfree(handle);
  1241. return ret;
  1242. }
  1243. EXPORT_SYMBOL_GPL(stratix10_svc_async_send);
  1244. /**
  1245. * stratix10_svc_async_prepare_response - Prepare the response data for
  1246. * an asynchronous transaction.
  1247. * @chan: Pointer to the service channel structure.
  1248. * @handle: Pointer to the asynchronous handler structure.
  1249. * @data: Pointer to the callback data structure.
  1250. *
  1251. * This function prepares the response data for an asynchronous transaction. It
  1252. * extracts the response data from the SMC response structure and stores it in
  1253. * the callback data structure. The function also logs the completion of the
  1254. * asynchronous transaction.
  1255. *
  1256. * Return: 0 on success, -ENOENT if the command is invalid
  1257. */
  1258. static int stratix10_svc_async_prepare_response(struct stratix10_svc_chan *chan,
  1259. struct stratix10_svc_async_handler *handle,
  1260. struct stratix10_svc_cb_data *data)
  1261. {
  1262. struct stratix10_svc_client_msg *p_msg =
  1263. (struct stratix10_svc_client_msg *)handle->msg;
  1264. struct stratix10_svc_controller *ctrl = chan->ctrl;
  1265. data->status = STRATIX10_GET_SDM_STATUS_CODE(handle->res.a1);
  1266. switch (p_msg->command) {
  1267. case COMMAND_RSU_NOTIFY:
  1268. break;
  1269. case COMMAND_RSU_GET_SPT_TABLE:
  1270. data->kaddr1 = (void *)&handle->res.a2;
  1271. data->kaddr2 = (void *)&handle->res.a3;
  1272. break;
  1273. case COMMAND_RSU_STATUS:
  1274. /* COMMAND_RSU_STATUS has more elements than the cb_data
  1275. * can acomodate, so passing the response structure to the
  1276. * response function to be handled before done command is
  1277. * executed by the client.
  1278. */
  1279. data->kaddr1 = (void *)&handle->res;
  1280. break;
  1281. default:
  1282. dev_alert(ctrl->dev, "Invalid command\n ,%d", p_msg->command);
  1283. return -ENOENT;
  1284. }
  1285. dev_dbg(ctrl->dev, "Async message completed transaction_id 0x%02x\n",
  1286. handle->transaction_id);
  1287. return 0;
  1288. }
  1289. /**
  1290. * stratix10_svc_async_poll - Polls the status of an asynchronous
  1291. * transaction.
  1292. * @chan: Pointer to the service channel structure.
  1293. * @tx_handle: Handle to the transaction being polled.
  1294. * @data: Pointer to the callback data structure.
  1295. *
  1296. * This function polls the status of an asynchronous transaction
  1297. * identified by the given transaction handle. It ensures that the
  1298. * necessary structures are initialized and valid before proceeding
  1299. * with the poll operation. The function sets up the necessary
  1300. * arguments for the SMC call, invokes the call, and prepares the
  1301. * response data if the call is successful. If the call fails, the
  1302. * function returns the error mapped to the SVC status error.
  1303. *
  1304. * Return: 0 on success, -EINVAL if any input parameter is invalid,
  1305. * -EAGAIN if the transaction is still in progress,
  1306. * -EPERM if the command is invalid, or other negative
  1307. * error codes on failure.
  1308. */
  1309. int stratix10_svc_async_poll(struct stratix10_svc_chan *chan,
  1310. void *tx_handle,
  1311. struct stratix10_svc_cb_data *data)
  1312. {
  1313. struct stratix10_svc_async_handler *handle;
  1314. struct arm_smccc_1_2_regs args = { 0 };
  1315. struct stratix10_svc_controller *ctrl;
  1316. struct stratix10_async_ctrl *actrl;
  1317. struct stratix10_async_chan *achan;
  1318. int ret;
  1319. if (!chan || !tx_handle || !data)
  1320. return -EINVAL;
  1321. ctrl = chan->ctrl;
  1322. actrl = &ctrl->actrl;
  1323. achan = chan->async_chan;
  1324. if (!achan) {
  1325. dev_err(ctrl->dev, "Async channel not allocated\n");
  1326. return -EINVAL;
  1327. }
  1328. handle = (struct stratix10_svc_async_handler *)tx_handle;
  1329. scoped_guard(spinlock_bh, &actrl->trx_list_lock) {
  1330. if (!hash_hashed(&handle->next)) {
  1331. dev_err(ctrl->dev, "Invalid transaction handler");
  1332. return -EINVAL;
  1333. }
  1334. }
  1335. args.a0 = INTEL_SIP_SMC_ASYNC_POLL;
  1336. args.a1 =
  1337. STRATIX10_SIP_SMC_SET_TRANSACTIONID_X1(handle->transaction_id);
  1338. actrl->invoke_fn(actrl, &args, &handle->res);
  1339. /*clear data for response*/
  1340. memset(data, 0, sizeof(*data));
  1341. if (handle->res.a0 == INTEL_SIP_SMC_STATUS_OK) {
  1342. ret = stratix10_svc_async_prepare_response(chan, handle, data);
  1343. if (ret) {
  1344. dev_err(ctrl->dev, "Error in preparation of response,%d\n", ret);
  1345. WARN_ON_ONCE(1);
  1346. }
  1347. return 0;
  1348. } else if (handle->res.a0 == INTEL_SIP_SMC_STATUS_BUSY) {
  1349. dev_dbg(ctrl->dev, "async message is still in progress\n");
  1350. return -EAGAIN;
  1351. }
  1352. dev_err(ctrl->dev,
  1353. "Failed to poll async message ,got status as %ld\n",
  1354. handle->res.a0);
  1355. return -EINVAL;
  1356. }
  1357. EXPORT_SYMBOL_GPL(stratix10_svc_async_poll);
  1358. /**
  1359. * stratix10_svc_async_done - Completes an asynchronous transaction.
  1360. * @chan: Pointer to the service channel structure.
  1361. * @tx_handle: Handle to the transaction being completed.
  1362. *
  1363. * This function completes an asynchronous transaction identified by
  1364. * the given transaction handle. It ensures that the necessary
  1365. * structures are initialized and valid before proceeding with the
  1366. * completion operation. The function deallocates the transaction ID,
  1367. * frees the memory allocated for the handler, and removes the handler
  1368. * from the transaction list.
  1369. *
  1370. * Return: 0 on success, -EINVAL if any input parameter is invalid,
  1371. * or other negative error codes on failure.
  1372. */
  1373. int stratix10_svc_async_done(struct stratix10_svc_chan *chan, void *tx_handle)
  1374. {
  1375. struct stratix10_svc_async_handler *handle;
  1376. struct stratix10_svc_controller *ctrl;
  1377. struct stratix10_async_chan *achan;
  1378. struct stratix10_async_ctrl *actrl;
  1379. if (!chan || !tx_handle)
  1380. return -EINVAL;
  1381. ctrl = chan->ctrl;
  1382. achan = chan->async_chan;
  1383. actrl = &ctrl->actrl;
  1384. if (!achan) {
  1385. dev_err(ctrl->dev, "async channel not allocated\n");
  1386. return -EINVAL;
  1387. }
  1388. handle = (struct stratix10_svc_async_handler *)tx_handle;
  1389. scoped_guard(spinlock_bh, &actrl->trx_list_lock) {
  1390. if (!hash_hashed(&handle->next)) {
  1391. dev_err(ctrl->dev, "Invalid transaction handle");
  1392. return -EINVAL;
  1393. }
  1394. hash_del(&handle->next);
  1395. }
  1396. ida_free(&achan->job_id_pool,
  1397. STRATIX10_GET_JOBID(handle->transaction_id));
  1398. kfree(handle);
  1399. return 0;
  1400. }
  1401. EXPORT_SYMBOL_GPL(stratix10_svc_async_done);
  1402. static inline void stratix10_smc_1_2(struct stratix10_async_ctrl *actrl,
  1403. const struct arm_smccc_1_2_regs *args,
  1404. struct arm_smccc_1_2_regs *res)
  1405. {
  1406. arm_smccc_1_2_smc(args, res);
  1407. }
  1408. /**
  1409. * stratix10_svc_async_init - Initialize the Stratix10 service
  1410. * controller for asynchronous operations.
  1411. * @controller: Pointer to the Stratix10 service controller structure.
  1412. *
  1413. * This function initializes the asynchronous service controller by
  1414. * setting up the necessary data structures and initializing the
  1415. * transaction list.
  1416. *
  1417. * Return: 0 on success, -EINVAL if the controller is NULL or already
  1418. * initialized, -ENOMEM if memory allocation fails,
  1419. * -EADDRINUSE if the client ID is already reserved, or other
  1420. * negative error codes on failure.
  1421. */
  1422. static int stratix10_svc_async_init(struct stratix10_svc_controller *controller)
  1423. {
  1424. struct stratix10_async_ctrl *actrl;
  1425. struct arm_smccc_res res;
  1426. struct device *dev;
  1427. int ret;
  1428. if (!controller)
  1429. return -EINVAL;
  1430. actrl = &controller->actrl;
  1431. if (actrl->initialized)
  1432. return -EINVAL;
  1433. dev = controller->dev;
  1434. controller->invoke_fn(INTEL_SIP_SMC_SVC_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
  1435. if (res.a0 != INTEL_SIP_SMC_STATUS_OK ||
  1436. !(res.a1 > ASYNC_ATF_MINIMUM_MAJOR_VERSION ||
  1437. (res.a1 == ASYNC_ATF_MINIMUM_MAJOR_VERSION &&
  1438. res.a2 >= ASYNC_ATF_MINIMUM_MINOR_VERSION))) {
  1439. dev_err(dev,
  1440. "Intel Service Layer Driver: ATF version is not compatible for async operation\n");
  1441. return -EINVAL;
  1442. }
  1443. actrl->invoke_fn = stratix10_smc_1_2;
  1444. ida_init(&actrl->async_id_pool);
  1445. /**
  1446. * SIP_SVC_V1_CLIENT_ID is used by V1/stratix10_svc_send() clients
  1447. * for communicating with SDM synchronously. We need to restrict
  1448. * this in V3/stratix10_svc_async_send() usage to distinguish
  1449. * between V1 and V3 messages in El3 firmware.
  1450. */
  1451. ret = ida_alloc_range(&actrl->async_id_pool, SIP_SVC_V1_CLIENT_ID,
  1452. SIP_SVC_V1_CLIENT_ID, GFP_KERNEL);
  1453. if (ret < 0) {
  1454. dev_err(dev,
  1455. "Intel Service Layer Driver: Error on reserving SIP_SVC_V1_CLIENT_ID\n");
  1456. ida_destroy(&actrl->async_id_pool);
  1457. actrl->invoke_fn = NULL;
  1458. return -EADDRINUSE;
  1459. }
  1460. spin_lock_init(&actrl->trx_list_lock);
  1461. hash_init(actrl->trx_list);
  1462. atomic_set(&actrl->common_achan_refcount, 0);
  1463. actrl->initialized = true;
  1464. return 0;
  1465. }
  1466. /**
  1467. * stratix10_svc_async_exit - Clean up and exit the asynchronous
  1468. * service controller
  1469. * @ctrl: Pointer to the stratix10_svc_controller structure
  1470. *
  1471. * This function performs the necessary cleanup for the asynchronous
  1472. * service controller. It checks if the controller is valid and if it
  1473. * has been initialized. It then locks the transaction list and safely
  1474. * removes and deallocates each handler in the list. The function also
  1475. * removes any asynchronous clients associated with the controller's
  1476. * channels and destroys the asynchronous ID pool. Finally, it resets
  1477. * the asynchronous ID pool and invoke function pointers to NULL.
  1478. *
  1479. * Return: 0 on success, -EINVAL if the controller is invalid or not
  1480. * initialized.
  1481. */
  1482. static int stratix10_svc_async_exit(struct stratix10_svc_controller *ctrl)
  1483. {
  1484. struct stratix10_svc_async_handler *handler;
  1485. struct stratix10_async_ctrl *actrl;
  1486. struct hlist_node *tmp;
  1487. int i;
  1488. if (!ctrl)
  1489. return -EINVAL;
  1490. actrl = &ctrl->actrl;
  1491. if (!actrl->initialized)
  1492. return -EINVAL;
  1493. actrl->initialized = false;
  1494. scoped_guard(spinlock_bh, &actrl->trx_list_lock) {
  1495. hash_for_each_safe(actrl->trx_list, i, tmp, handler, next) {
  1496. ida_free(&handler->achan->job_id_pool,
  1497. STRATIX10_GET_JOBID(handler->transaction_id));
  1498. hash_del(&handler->next);
  1499. kfree(handler);
  1500. }
  1501. }
  1502. for (i = 0; i < SVC_NUM_CHANNEL; i++) {
  1503. if (ctrl->chans[i].async_chan) {
  1504. stratix10_svc_remove_async_client(&ctrl->chans[i]);
  1505. ctrl->chans[i].async_chan = NULL;
  1506. }
  1507. }
  1508. ida_destroy(&actrl->async_id_pool);
  1509. actrl->invoke_fn = NULL;
  1510. return 0;
  1511. }
  1512. /**
  1513. * stratix10_svc_free_channel() - free service channel
  1514. * @chan: service channel to be freed
  1515. *
  1516. * This function is used by service client to free a service channel.
  1517. */
  1518. void stratix10_svc_free_channel(struct stratix10_svc_chan *chan)
  1519. {
  1520. unsigned long flag;
  1521. spin_lock_irqsave(&chan->lock, flag);
  1522. chan->scl = NULL;
  1523. chan->ctrl->num_active_client--;
  1524. module_put(chan->ctrl->dev->driver->owner);
  1525. spin_unlock_irqrestore(&chan->lock, flag);
  1526. }
  1527. EXPORT_SYMBOL_GPL(stratix10_svc_free_channel);
  1528. /**
  1529. * stratix10_svc_send() - send a message data to the remote
  1530. * @chan: service channel assigned to the client
  1531. * @msg: message data to be sent, in the format of
  1532. * "struct stratix10_svc_client_msg"
  1533. *
  1534. * This function is used by service client to add a message to the service
  1535. * layer driver's queue for being sent to the secure world.
  1536. *
  1537. * Return: 0 for success, -ENOMEM or -ENOBUFS on error.
  1538. */
  1539. int stratix10_svc_send(struct stratix10_svc_chan *chan, void *msg)
  1540. {
  1541. struct stratix10_svc_client_msg
  1542. *p_msg = (struct stratix10_svc_client_msg *)msg;
  1543. struct stratix10_svc_data_mem *p_mem;
  1544. struct stratix10_svc_data *p_data;
  1545. int ret = 0;
  1546. unsigned int cpu = 0;
  1547. p_data = kzalloc_obj(*p_data);
  1548. if (!p_data)
  1549. return -ENOMEM;
  1550. /* first caller creates the per-channel kthread */
  1551. if (!chan->task) {
  1552. struct task_struct *task;
  1553. task = kthread_run_on_cpu(svc_normal_to_secure_thread,
  1554. (void *)chan,
  1555. cpu, "svc_smc_hvc_thread");
  1556. if (IS_ERR(task)) {
  1557. dev_err(chan->ctrl->dev,
  1558. "failed to create svc_smc_hvc_thread\n");
  1559. kfree(p_data);
  1560. return -EINVAL;
  1561. }
  1562. spin_lock(&chan->lock);
  1563. if (chan->task) {
  1564. /* another caller won the race; discard our thread */
  1565. spin_unlock(&chan->lock);
  1566. kthread_stop(task);
  1567. } else {
  1568. chan->task = task;
  1569. spin_unlock(&chan->lock);
  1570. }
  1571. }
  1572. pr_debug("%s: %s: sent P-va=%p, P-com=%x, P-size=%u\n", __func__,
  1573. chan->name, p_msg->payload, p_msg->command,
  1574. (unsigned int)p_msg->payload_length);
  1575. if (list_empty(&svc_data_mem)) {
  1576. if (p_msg->command == COMMAND_RECONFIG) {
  1577. struct stratix10_svc_command_config_type *ct =
  1578. (struct stratix10_svc_command_config_type *)
  1579. p_msg->payload;
  1580. p_data->flag = ct->flags;
  1581. }
  1582. } else {
  1583. guard(mutex)(&svc_mem_lock);
  1584. list_for_each_entry(p_mem, &svc_data_mem, node)
  1585. if (p_mem->vaddr == p_msg->payload) {
  1586. p_data->paddr = p_mem->paddr;
  1587. p_data->size = p_msg->payload_length;
  1588. break;
  1589. }
  1590. if (p_msg->payload_output) {
  1591. list_for_each_entry(p_mem, &svc_data_mem, node)
  1592. if (p_mem->vaddr == p_msg->payload_output) {
  1593. p_data->paddr_output =
  1594. p_mem->paddr;
  1595. p_data->size_output =
  1596. p_msg->payload_length_output;
  1597. break;
  1598. }
  1599. }
  1600. }
  1601. p_data->command = p_msg->command;
  1602. p_data->arg[0] = p_msg->arg[0];
  1603. p_data->arg[1] = p_msg->arg[1];
  1604. p_data->arg[2] = p_msg->arg[2];
  1605. p_data->size = p_msg->payload_length;
  1606. p_data->chan = chan;
  1607. pr_debug("%s: %s: put to FIFO pa=0x%016x, cmd=%x, size=%u\n",
  1608. __func__,
  1609. chan->name,
  1610. (unsigned int)p_data->paddr,
  1611. p_data->command,
  1612. (unsigned int)p_data->size);
  1613. ret = kfifo_in_spinlocked(&chan->svc_fifo, p_data,
  1614. sizeof(*p_data),
  1615. &chan->svc_fifo_lock);
  1616. kfree(p_data);
  1617. if (!ret)
  1618. return -ENOBUFS;
  1619. return 0;
  1620. }
  1621. EXPORT_SYMBOL_GPL(stratix10_svc_send);
  1622. /**
  1623. * stratix10_svc_done() - complete service request transactions
  1624. * @chan: service channel assigned to the client
  1625. *
  1626. * This function should be called when client has finished its request
  1627. * or there is an error in the request process. It allows the service layer
  1628. * to stop the running thread to have maximize savings in kernel resources.
  1629. */
  1630. void stratix10_svc_done(struct stratix10_svc_chan *chan)
  1631. {
  1632. /* stop thread when thread is running */
  1633. if (chan->task) {
  1634. pr_debug("%s: %s: svc_smc_hvc_shm_thread is stopping\n",
  1635. __func__, chan->name);
  1636. kthread_stop(chan->task);
  1637. chan->task = NULL;
  1638. }
  1639. }
  1640. EXPORT_SYMBOL_GPL(stratix10_svc_done);
  1641. /**
  1642. * stratix10_svc_allocate_memory() - allocate memory
  1643. * @chan: service channel assigned to the client
  1644. * @size: memory size requested by a specific service client
  1645. *
  1646. * Service layer allocates the requested number of bytes buffer from the
  1647. * memory pool, service client uses this function to get allocated buffers.
  1648. *
  1649. * Return: address of allocated memory on success, or ERR_PTR() on error.
  1650. */
  1651. void *stratix10_svc_allocate_memory(struct stratix10_svc_chan *chan,
  1652. size_t size)
  1653. {
  1654. struct stratix10_svc_data_mem *pmem;
  1655. unsigned long va;
  1656. phys_addr_t pa;
  1657. struct gen_pool *genpool = chan->ctrl->genpool;
  1658. size_t s = roundup(size, 1 << genpool->min_alloc_order);
  1659. pmem = devm_kzalloc(chan->ctrl->dev, sizeof(*pmem), GFP_KERNEL);
  1660. if (!pmem)
  1661. return ERR_PTR(-ENOMEM);
  1662. guard(mutex)(&svc_mem_lock);
  1663. va = gen_pool_alloc(genpool, s);
  1664. if (!va)
  1665. return ERR_PTR(-ENOMEM);
  1666. memset((void *)va, 0, s);
  1667. pa = gen_pool_virt_to_phys(genpool, va);
  1668. pmem->vaddr = (void *)va;
  1669. pmem->paddr = pa;
  1670. pmem->size = s;
  1671. list_add_tail(&pmem->node, &svc_data_mem);
  1672. pr_debug("%s: %s: va=%p, pa=0x%016x\n", __func__,
  1673. chan->name, pmem->vaddr, (unsigned int)pmem->paddr);
  1674. return (void *)va;
  1675. }
  1676. EXPORT_SYMBOL_GPL(stratix10_svc_allocate_memory);
  1677. /**
  1678. * stratix10_svc_free_memory() - free allocated memory
  1679. * @chan: service channel assigned to the client
  1680. * @kaddr: memory to be freed
  1681. *
  1682. * This function is used by service client to free allocated buffers.
  1683. */
  1684. void stratix10_svc_free_memory(struct stratix10_svc_chan *chan, void *kaddr)
  1685. {
  1686. struct stratix10_svc_data_mem *pmem;
  1687. guard(mutex)(&svc_mem_lock);
  1688. list_for_each_entry(pmem, &svc_data_mem, node)
  1689. if (pmem->vaddr == kaddr) {
  1690. gen_pool_free(chan->ctrl->genpool,
  1691. (unsigned long)kaddr, pmem->size);
  1692. pmem->vaddr = NULL;
  1693. list_del(&pmem->node);
  1694. return;
  1695. }
  1696. list_del(&svc_data_mem);
  1697. }
  1698. EXPORT_SYMBOL_GPL(stratix10_svc_free_memory);
  1699. static const struct of_device_id stratix10_svc_drv_match[] = {
  1700. {.compatible = "intel,stratix10-svc"},
  1701. {.compatible = "intel,agilex-svc"},
  1702. {},
  1703. };
  1704. static const char * const chan_names[SVC_NUM_CHANNEL] = {
  1705. SVC_CLIENT_FPGA,
  1706. SVC_CLIENT_RSU,
  1707. SVC_CLIENT_FCS,
  1708. SVC_CLIENT_HWMON
  1709. };
  1710. static int stratix10_svc_drv_probe(struct platform_device *pdev)
  1711. {
  1712. struct device *dev = &pdev->dev;
  1713. struct stratix10_svc_controller *controller;
  1714. struct stratix10_svc_chan *chans;
  1715. struct gen_pool *genpool;
  1716. struct stratix10_svc_sh_memory *sh_memory;
  1717. struct stratix10_svc *svc = NULL;
  1718. svc_invoke_fn *invoke_fn;
  1719. size_t fifo_size;
  1720. int ret, i = 0;
  1721. /* get SMC or HVC function */
  1722. invoke_fn = get_invoke_func(dev);
  1723. if (IS_ERR(invoke_fn))
  1724. return -EINVAL;
  1725. sh_memory = devm_kzalloc(dev, sizeof(*sh_memory), GFP_KERNEL);
  1726. if (!sh_memory)
  1727. return -ENOMEM;
  1728. sh_memory->invoke_fn = invoke_fn;
  1729. ret = svc_get_sh_memory(pdev, sh_memory);
  1730. if (ret)
  1731. return ret;
  1732. genpool = svc_create_memory_pool(pdev, sh_memory);
  1733. if (IS_ERR(genpool))
  1734. return PTR_ERR(genpool);
  1735. /* allocate service controller and supporting channel */
  1736. controller = devm_kzalloc(dev, sizeof(*controller), GFP_KERNEL);
  1737. if (!controller) {
  1738. ret = -ENOMEM;
  1739. goto err_destroy_pool;
  1740. }
  1741. chans = devm_kmalloc_array(dev, SVC_NUM_CHANNEL,
  1742. sizeof(*chans), GFP_KERNEL | __GFP_ZERO);
  1743. if (!chans) {
  1744. ret = -ENOMEM;
  1745. goto err_destroy_pool;
  1746. }
  1747. controller->dev = dev;
  1748. controller->num_chans = SVC_NUM_CHANNEL;
  1749. controller->num_active_client = 0;
  1750. controller->chans = chans;
  1751. controller->genpool = genpool;
  1752. controller->invoke_fn = invoke_fn;
  1753. INIT_LIST_HEAD(&controller->node);
  1754. init_completion(&controller->complete_status);
  1755. ret = stratix10_svc_async_init(controller);
  1756. if (ret) {
  1757. dev_dbg(dev, "Intel Service Layer Driver: Error on stratix10_svc_async_init %d\n",
  1758. ret);
  1759. goto err_destroy_pool;
  1760. }
  1761. fifo_size = sizeof(struct stratix10_svc_data) * SVC_NUM_DATA_IN_FIFO;
  1762. mutex_init(&controller->sdm_lock);
  1763. for (i = 0; i < SVC_NUM_CHANNEL; i++) {
  1764. chans[i].scl = NULL;
  1765. chans[i].ctrl = controller;
  1766. chans[i].name = (char *)chan_names[i];
  1767. spin_lock_init(&chans[i].lock);
  1768. ret = kfifo_alloc(&chans[i].svc_fifo, fifo_size, GFP_KERNEL);
  1769. if (ret) {
  1770. dev_err(dev, "failed to allocate FIFO %d\n", i);
  1771. goto err_free_fifos;
  1772. }
  1773. spin_lock_init(&chans[i].svc_fifo_lock);
  1774. }
  1775. list_add_tail(&controller->node, &svc_ctrl);
  1776. platform_set_drvdata(pdev, controller);
  1777. /* add svc client device(s) */
  1778. svc = devm_kzalloc(dev, sizeof(*svc), GFP_KERNEL);
  1779. if (!svc) {
  1780. ret = -ENOMEM;
  1781. goto err_free_fifos;
  1782. }
  1783. controller->svc = svc;
  1784. svc->stratix10_svc_rsu = platform_device_alloc(STRATIX10_RSU, 0);
  1785. if (!svc->stratix10_svc_rsu) {
  1786. dev_err(dev, "failed to allocate %s device\n", STRATIX10_RSU);
  1787. ret = -ENOMEM;
  1788. goto err_free_fifos;
  1789. }
  1790. ret = platform_device_add(svc->stratix10_svc_rsu);
  1791. if (ret)
  1792. goto err_put_device;
  1793. ret = of_platform_default_populate(dev_of_node(dev), NULL, dev);
  1794. if (ret)
  1795. goto err_unregister_rsu_dev;
  1796. pr_info("Intel Service Layer Driver Initialized\n");
  1797. return 0;
  1798. err_unregister_rsu_dev:
  1799. platform_device_unregister(svc->stratix10_svc_rsu);
  1800. goto err_free_fifos;
  1801. err_put_device:
  1802. platform_device_put(svc->stratix10_svc_rsu);
  1803. err_free_fifos:
  1804. /* only remove from list if list_add_tail() was reached */
  1805. if (!list_empty(&controller->node))
  1806. list_del(&controller->node);
  1807. /* free only the FIFOs that were successfully allocated */
  1808. while (i--)
  1809. kfifo_free(&chans[i].svc_fifo);
  1810. stratix10_svc_async_exit(controller);
  1811. err_destroy_pool:
  1812. gen_pool_destroy(genpool);
  1813. return ret;
  1814. }
  1815. static void stratix10_svc_drv_remove(struct platform_device *pdev)
  1816. {
  1817. int i;
  1818. struct stratix10_svc_controller *ctrl = platform_get_drvdata(pdev);
  1819. struct stratix10_svc *svc = ctrl->svc;
  1820. stratix10_svc_async_exit(ctrl);
  1821. of_platform_depopulate(ctrl->dev);
  1822. platform_device_unregister(svc->stratix10_svc_rsu);
  1823. for (i = 0; i < SVC_NUM_CHANNEL; i++) {
  1824. if (ctrl->chans[i].task) {
  1825. kthread_stop(ctrl->chans[i].task);
  1826. ctrl->chans[i].task = NULL;
  1827. }
  1828. kfifo_free(&ctrl->chans[i].svc_fifo);
  1829. }
  1830. if (ctrl->genpool)
  1831. gen_pool_destroy(ctrl->genpool);
  1832. list_del(&ctrl->node);
  1833. }
  1834. static struct platform_driver stratix10_svc_driver = {
  1835. .probe = stratix10_svc_drv_probe,
  1836. .remove = stratix10_svc_drv_remove,
  1837. .driver = {
  1838. .name = "stratix10-svc",
  1839. .of_match_table = stratix10_svc_drv_match,
  1840. },
  1841. };
  1842. static int __init stratix10_svc_init(void)
  1843. {
  1844. struct device_node *fw_np;
  1845. struct device_node *np;
  1846. int ret;
  1847. fw_np = of_find_node_by_name(NULL, "firmware");
  1848. if (!fw_np)
  1849. return -ENODEV;
  1850. np = of_find_matching_node(fw_np, stratix10_svc_drv_match);
  1851. if (!np)
  1852. return -ENODEV;
  1853. of_node_put(np);
  1854. ret = of_platform_populate(fw_np, stratix10_svc_drv_match, NULL, NULL);
  1855. if (ret)
  1856. return ret;
  1857. return platform_driver_register(&stratix10_svc_driver);
  1858. }
  1859. static void __exit stratix10_svc_exit(void)
  1860. {
  1861. return platform_driver_unregister(&stratix10_svc_driver);
  1862. }
  1863. subsys_initcall(stratix10_svc_init);
  1864. module_exit(stratix10_svc_exit);
  1865. MODULE_LICENSE("GPL v2");
  1866. MODULE_DESCRIPTION("Intel Stratix10 Service Layer Driver");
  1867. MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");
  1868. MODULE_ALIAS("platform:stratix10-svc");