smc_abi.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, 2023 Linaro Limited
  4. * Copyright (c) 2016, EPAM Systems
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/arm-smccc.h>
  8. #include <linux/cpuhotplug.h>
  9. #include <linux/errno.h>
  10. #include <linux/firmware.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/rpmb.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <linux/tee_core.h>
  26. #include <linux/types.h>
  27. #include <linux/workqueue.h>
  28. #include "optee_private.h"
  29. #include "optee_smc.h"
  30. #include "optee_rpc_cmd.h"
  31. #include <linux/kmemleak.h>
  32. #define CREATE_TRACE_POINTS
  33. #include "optee_trace.h"
  34. /*
  35. * This file implement the SMC ABI used when communicating with secure world
  36. * OP-TEE OS via raw SMCs.
  37. * This file is divided into the following sections:
  38. * 1. Convert between struct tee_param and struct optee_msg_param
  39. * 2. Low level support functions to register shared memory in secure world
  40. * 3. Dynamic shared memory pool based on alloc_pages()
  41. * 4. Do a normal scheduled call into secure world
  42. * 5. Asynchronous notification
  43. * 6. Driver initialization.
  44. */
  45. /*
  46. * A typical OP-TEE private shm allocation is 224 bytes (argument struct
  47. * with 6 parameters, needed for open session). So with an alignment of 512
  48. * we'll waste a bit more than 50%. However, it's only expected that we'll
  49. * have a handful of these structs allocated at a time. Most memory will
  50. * be allocated aligned to the page size, So all in all this should scale
  51. * up and down quite well.
  52. */
  53. #define OPTEE_MIN_STATIC_POOL_ALIGN 9 /* 512 bytes aligned */
  54. /* SMC ABI considers at most a single TEE firmware */
  55. static unsigned int pcpu_irq_num;
  56. static int optee_cpuhp_enable_pcpu_irq(unsigned int cpu)
  57. {
  58. enable_percpu_irq(pcpu_irq_num, IRQ_TYPE_NONE);
  59. return 0;
  60. }
  61. static int optee_cpuhp_disable_pcpu_irq(unsigned int cpu)
  62. {
  63. disable_percpu_irq(pcpu_irq_num);
  64. return 0;
  65. }
  66. /*
  67. * 1. Convert between struct tee_param and struct optee_msg_param
  68. *
  69. * optee_from_msg_param() and optee_to_msg_param() are the main
  70. * functions.
  71. */
  72. static int from_msg_param_tmp_mem(struct tee_param *p, u32 attr,
  73. const struct optee_msg_param *mp)
  74. {
  75. struct tee_shm *shm;
  76. phys_addr_t pa;
  77. int rc;
  78. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
  79. attr - OPTEE_MSG_ATTR_TYPE_TMEM_INPUT;
  80. p->u.memref.size = mp->u.tmem.size;
  81. shm = (struct tee_shm *)(unsigned long)mp->u.tmem.shm_ref;
  82. if (!shm) {
  83. p->u.memref.shm_offs = 0;
  84. p->u.memref.shm = NULL;
  85. return 0;
  86. }
  87. rc = tee_shm_get_pa(shm, 0, &pa);
  88. if (rc)
  89. return rc;
  90. p->u.memref.shm_offs = mp->u.tmem.buf_ptr - pa;
  91. p->u.memref.shm = shm;
  92. return 0;
  93. }
  94. static void from_msg_param_reg_mem(struct tee_param *p, u32 attr,
  95. const struct optee_msg_param *mp)
  96. {
  97. struct tee_shm *shm;
  98. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
  99. attr - OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
  100. p->u.memref.size = mp->u.rmem.size;
  101. shm = (struct tee_shm *)(unsigned long)mp->u.rmem.shm_ref;
  102. if (shm) {
  103. p->u.memref.shm_offs = mp->u.rmem.offs;
  104. p->u.memref.shm = shm;
  105. } else {
  106. p->u.memref.shm_offs = 0;
  107. p->u.memref.shm = NULL;
  108. }
  109. }
  110. /**
  111. * optee_from_msg_param() - convert from OPTEE_MSG parameters to
  112. * struct tee_param
  113. * @optee: main service struct
  114. * @params: subsystem internal parameter representation
  115. * @num_params: number of elements in the parameter arrays
  116. * @msg_params: OPTEE_MSG parameters
  117. * Returns 0 on success or <0 on failure
  118. */
  119. static int optee_from_msg_param(struct optee *optee, struct tee_param *params,
  120. size_t num_params,
  121. const struct optee_msg_param *msg_params)
  122. {
  123. int rc;
  124. size_t n;
  125. for (n = 0; n < num_params; n++) {
  126. struct tee_param *p = params + n;
  127. const struct optee_msg_param *mp = msg_params + n;
  128. u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK;
  129. switch (attr) {
  130. case OPTEE_MSG_ATTR_TYPE_NONE:
  131. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
  132. memset(&p->u, 0, sizeof(p->u));
  133. break;
  134. case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT:
  135. case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT:
  136. case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT:
  137. optee_from_msg_param_value(p, attr, mp);
  138. break;
  139. case OPTEE_MSG_ATTR_TYPE_TMEM_INPUT:
  140. case OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT:
  141. case OPTEE_MSG_ATTR_TYPE_TMEM_INOUT:
  142. rc = from_msg_param_tmp_mem(p, attr, mp);
  143. if (rc)
  144. return rc;
  145. break;
  146. case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT:
  147. case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT:
  148. case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT:
  149. from_msg_param_reg_mem(p, attr, mp);
  150. break;
  151. default:
  152. return -EINVAL;
  153. }
  154. }
  155. return 0;
  156. }
  157. static int to_msg_param_tmp_mem(struct optee_msg_param *mp,
  158. const struct tee_param *p)
  159. {
  160. int rc;
  161. phys_addr_t pa;
  162. mp->attr = OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + p->attr -
  163. TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
  164. mp->u.tmem.shm_ref = (unsigned long)p->u.memref.shm;
  165. mp->u.tmem.size = p->u.memref.size;
  166. if (!p->u.memref.shm) {
  167. mp->u.tmem.buf_ptr = 0;
  168. return 0;
  169. }
  170. rc = tee_shm_get_pa(p->u.memref.shm, p->u.memref.shm_offs, &pa);
  171. if (rc)
  172. return rc;
  173. mp->u.tmem.buf_ptr = pa;
  174. mp->attr |= OPTEE_MSG_ATTR_CACHE_PREDEFINED <<
  175. OPTEE_MSG_ATTR_CACHE_SHIFT;
  176. return 0;
  177. }
  178. static int to_msg_param_reg_mem(struct optee_msg_param *mp,
  179. const struct tee_param *p)
  180. {
  181. mp->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr -
  182. TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
  183. mp->u.rmem.shm_ref = (unsigned long)p->u.memref.shm;
  184. mp->u.rmem.size = p->u.memref.size;
  185. mp->u.rmem.offs = p->u.memref.shm_offs;
  186. return 0;
  187. }
  188. /**
  189. * optee_to_msg_param() - convert from struct tee_params to OPTEE_MSG parameters
  190. * @optee: main service struct
  191. * @msg_params: OPTEE_MSG parameters
  192. * @num_params: number of elements in the parameter arrays
  193. * @params: subsystem itnernal parameter representation
  194. * Returns 0 on success or <0 on failure
  195. */
  196. static int optee_to_msg_param(struct optee *optee,
  197. struct optee_msg_param *msg_params,
  198. size_t num_params, const struct tee_param *params)
  199. {
  200. int rc;
  201. size_t n;
  202. for (n = 0; n < num_params; n++) {
  203. const struct tee_param *p = params + n;
  204. struct optee_msg_param *mp = msg_params + n;
  205. switch (p->attr) {
  206. case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
  207. mp->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
  208. memset(&mp->u, 0, sizeof(mp->u));
  209. break;
  210. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
  211. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
  212. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
  213. optee_to_msg_param_value(mp, p);
  214. break;
  215. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
  216. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
  217. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
  218. if (tee_shm_is_dynamic(p->u.memref.shm))
  219. rc = to_msg_param_reg_mem(mp, p);
  220. else
  221. rc = to_msg_param_tmp_mem(mp, p);
  222. if (rc)
  223. return rc;
  224. break;
  225. default:
  226. return -EINVAL;
  227. }
  228. }
  229. return 0;
  230. }
  231. /*
  232. * 2. Low level support functions to register shared memory in secure world
  233. *
  234. * Functions to enable/disable shared memory caching in secure world, that
  235. * is, lazy freeing of previously allocated shared memory. Freeing is
  236. * performed when a request has been compled.
  237. *
  238. * Functions to register and unregister shared memory both for normal
  239. * clients and for tee-supplicant.
  240. */
  241. /**
  242. * optee_enable_shm_cache() - Enables caching of some shared memory allocation
  243. * in OP-TEE
  244. * @optee: main service struct
  245. */
  246. static void optee_enable_shm_cache(struct optee *optee)
  247. {
  248. struct optee_call_waiter w;
  249. /* We need to retry until secure world isn't busy. */
  250. optee_cq_wait_init(&optee->call_queue, &w, false);
  251. while (true) {
  252. struct arm_smccc_res res;
  253. optee->smc.invoke_fn(OPTEE_SMC_ENABLE_SHM_CACHE,
  254. 0, 0, 0, 0, 0, 0, 0, &res);
  255. if (res.a0 == OPTEE_SMC_RETURN_OK)
  256. break;
  257. optee_cq_wait_for_completion(&optee->call_queue, &w);
  258. }
  259. optee_cq_wait_final(&optee->call_queue, &w);
  260. }
  261. /**
  262. * __optee_disable_shm_cache() - Disables caching of some shared memory
  263. * allocation in OP-TEE
  264. * @optee: main service struct
  265. * @is_mapped: true if the cached shared memory addresses were mapped by this
  266. * kernel, are safe to dereference, and should be freed
  267. */
  268. static void __optee_disable_shm_cache(struct optee *optee, bool is_mapped)
  269. {
  270. struct optee_call_waiter w;
  271. /* We need to retry until secure world isn't busy. */
  272. optee_cq_wait_init(&optee->call_queue, &w, false);
  273. while (true) {
  274. union {
  275. struct arm_smccc_res smccc;
  276. struct optee_smc_disable_shm_cache_result result;
  277. } res;
  278. optee->smc.invoke_fn(OPTEE_SMC_DISABLE_SHM_CACHE,
  279. 0, 0, 0, 0, 0, 0, 0, &res.smccc);
  280. if (res.result.status == OPTEE_SMC_RETURN_ENOTAVAIL)
  281. break; /* All shm's freed */
  282. if (res.result.status == OPTEE_SMC_RETURN_OK) {
  283. struct tee_shm *shm;
  284. /*
  285. * Shared memory references that were not mapped by
  286. * this kernel must be ignored to prevent a crash.
  287. */
  288. if (!is_mapped)
  289. continue;
  290. shm = reg_pair_to_ptr(res.result.shm_upper32,
  291. res.result.shm_lower32);
  292. tee_shm_free(shm);
  293. } else {
  294. optee_cq_wait_for_completion(&optee->call_queue, &w);
  295. }
  296. }
  297. optee_cq_wait_final(&optee->call_queue, &w);
  298. }
  299. /**
  300. * optee_disable_shm_cache() - Disables caching of mapped shared memory
  301. * allocations in OP-TEE
  302. * @optee: main service struct
  303. */
  304. static void optee_disable_shm_cache(struct optee *optee)
  305. {
  306. return __optee_disable_shm_cache(optee, true);
  307. }
  308. /**
  309. * optee_disable_unmapped_shm_cache() - Disables caching of shared memory
  310. * allocations in OP-TEE which are not
  311. * currently mapped
  312. * @optee: main service struct
  313. */
  314. static void optee_disable_unmapped_shm_cache(struct optee *optee)
  315. {
  316. return __optee_disable_shm_cache(optee, false);
  317. }
  318. #define PAGELIST_ENTRIES_PER_PAGE \
  319. ((OPTEE_MSG_NONCONTIG_PAGE_SIZE / sizeof(u64)) - 1)
  320. /*
  321. * The final entry in each pagelist page is a pointer to the next
  322. * pagelist page.
  323. */
  324. static size_t get_pages_list_size(size_t num_entries)
  325. {
  326. int pages = DIV_ROUND_UP(num_entries, PAGELIST_ENTRIES_PER_PAGE);
  327. return pages * OPTEE_MSG_NONCONTIG_PAGE_SIZE;
  328. }
  329. static u64 *optee_allocate_pages_list(size_t num_entries)
  330. {
  331. return alloc_pages_exact(get_pages_list_size(num_entries), GFP_KERNEL);
  332. }
  333. static void optee_free_pages_list(void *list, size_t num_entries)
  334. {
  335. free_pages_exact(list, get_pages_list_size(num_entries));
  336. }
  337. /**
  338. * optee_fill_pages_list() - write list of user pages to given shared
  339. * buffer.
  340. *
  341. * @dst: page-aligned buffer where list of pages will be stored
  342. * @pages: array of pages that represents shared buffer
  343. * @num_pages: number of entries in @pages
  344. * @page_offset: offset of user buffer from page start
  345. *
  346. * @dst should be big enough to hold list of user page addresses and
  347. * links to the next pages of buffer
  348. */
  349. static void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
  350. size_t page_offset)
  351. {
  352. int n = 0;
  353. phys_addr_t optee_page;
  354. /*
  355. * Refer to OPTEE_MSG_ATTR_NONCONTIG description in optee_msg.h
  356. * for details.
  357. */
  358. struct {
  359. u64 pages_list[PAGELIST_ENTRIES_PER_PAGE];
  360. u64 next_page_data;
  361. } *pages_data;
  362. /*
  363. * Currently OP-TEE uses 4k page size and it does not looks
  364. * like this will change in the future. On other hand, there are
  365. * no know ARM architectures with page size < 4k.
  366. * Thus the next built assert looks redundant. But the following
  367. * code heavily relies on this assumption, so it is better be
  368. * safe than sorry.
  369. */
  370. BUILD_BUG_ON(PAGE_SIZE < OPTEE_MSG_NONCONTIG_PAGE_SIZE);
  371. pages_data = (void *)dst;
  372. /*
  373. * If linux page is bigger than 4k, and user buffer offset is
  374. * larger than 4k/8k/12k/etc this will skip first 4k pages,
  375. * because they bear no value data for OP-TEE.
  376. */
  377. optee_page = page_to_phys(*pages) +
  378. round_down(page_offset, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
  379. while (true) {
  380. pages_data->pages_list[n++] = optee_page;
  381. if (n == PAGELIST_ENTRIES_PER_PAGE) {
  382. pages_data->next_page_data =
  383. virt_to_phys(pages_data + 1);
  384. pages_data++;
  385. n = 0;
  386. }
  387. optee_page += OPTEE_MSG_NONCONTIG_PAGE_SIZE;
  388. if (!(optee_page & ~PAGE_MASK)) {
  389. if (!--num_pages)
  390. break;
  391. pages++;
  392. optee_page = page_to_phys(*pages);
  393. }
  394. }
  395. }
  396. static int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
  397. struct page **pages, size_t num_pages,
  398. unsigned long start)
  399. {
  400. struct optee *optee = tee_get_drvdata(ctx->teedev);
  401. struct optee_msg_arg *msg_arg;
  402. struct tee_shm *shm_arg;
  403. u64 *pages_list;
  404. size_t sz;
  405. int rc;
  406. if (!num_pages)
  407. return -EINVAL;
  408. rc = optee_check_mem_type(start, num_pages);
  409. if (rc)
  410. return rc;
  411. pages_list = optee_allocate_pages_list(num_pages);
  412. if (!pages_list)
  413. return -ENOMEM;
  414. /*
  415. * We're about to register shared memory we can't register shared
  416. * memory for this request or there's a catch-22.
  417. *
  418. * So in this we'll have to do the good old temporary private
  419. * allocation instead of using optee_get_msg_arg().
  420. */
  421. sz = optee_msg_arg_size(optee->rpc_param_count);
  422. shm_arg = tee_shm_alloc_priv_buf(ctx, sz);
  423. if (IS_ERR(shm_arg)) {
  424. rc = PTR_ERR(shm_arg);
  425. goto out;
  426. }
  427. msg_arg = tee_shm_get_va(shm_arg, 0);
  428. if (IS_ERR(msg_arg)) {
  429. rc = PTR_ERR(msg_arg);
  430. goto out;
  431. }
  432. optee_fill_pages_list(pages_list, pages, num_pages,
  433. tee_shm_get_page_offset(shm));
  434. memset(msg_arg, 0, OPTEE_MSG_GET_ARG_SIZE(1));
  435. msg_arg->num_params = 1;
  436. msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM;
  437. msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
  438. OPTEE_MSG_ATTR_NONCONTIG;
  439. msg_arg->params->u.tmem.shm_ref = (unsigned long)shm;
  440. msg_arg->params->u.tmem.size = tee_shm_get_size(shm);
  441. /*
  442. * In the least bits of msg_arg->params->u.tmem.buf_ptr we
  443. * store buffer offset from 4k page, as described in OP-TEE ABI.
  444. */
  445. msg_arg->params->u.tmem.buf_ptr = virt_to_phys(pages_list) |
  446. (tee_shm_get_page_offset(shm) & (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
  447. if (optee->ops->do_call_with_arg(ctx, shm_arg, 0, false) ||
  448. msg_arg->ret != TEEC_SUCCESS)
  449. rc = -EINVAL;
  450. tee_shm_free(shm_arg);
  451. out:
  452. optee_free_pages_list(pages_list, num_pages);
  453. return rc;
  454. }
  455. static int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm)
  456. {
  457. struct optee *optee = tee_get_drvdata(ctx->teedev);
  458. struct optee_msg_arg *msg_arg;
  459. struct tee_shm *shm_arg;
  460. int rc = 0;
  461. size_t sz;
  462. /*
  463. * We're about to unregister shared memory and we may not be able
  464. * register shared memory for this request in case we're called
  465. * from optee_shm_arg_cache_uninit().
  466. *
  467. * So in order to keep things simple in this function just as in
  468. * optee_shm_register() we'll use temporary private allocation
  469. * instead of using optee_get_msg_arg().
  470. */
  471. sz = optee_msg_arg_size(optee->rpc_param_count);
  472. shm_arg = tee_shm_alloc_priv_buf(ctx, sz);
  473. if (IS_ERR(shm_arg))
  474. return PTR_ERR(shm_arg);
  475. msg_arg = tee_shm_get_va(shm_arg, 0);
  476. if (IS_ERR(msg_arg)) {
  477. rc = PTR_ERR(msg_arg);
  478. goto out;
  479. }
  480. memset(msg_arg, 0, sz);
  481. msg_arg->num_params = 1;
  482. msg_arg->cmd = OPTEE_MSG_CMD_UNREGISTER_SHM;
  483. msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
  484. msg_arg->params[0].u.rmem.shm_ref = (unsigned long)shm;
  485. if (optee->ops->do_call_with_arg(ctx, shm_arg, 0, false) ||
  486. msg_arg->ret != TEEC_SUCCESS)
  487. rc = -EINVAL;
  488. out:
  489. tee_shm_free(shm_arg);
  490. return rc;
  491. }
  492. static int optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm,
  493. struct page **pages, size_t num_pages,
  494. unsigned long start)
  495. {
  496. /*
  497. * We don't want to register supplicant memory in OP-TEE.
  498. * Instead information about it will be passed in RPC code.
  499. */
  500. return optee_check_mem_type(start, num_pages);
  501. }
  502. static int optee_shm_unregister_supp(struct tee_context *ctx,
  503. struct tee_shm *shm)
  504. {
  505. return 0;
  506. }
  507. /*
  508. * 3. Dynamic shared memory pool based on alloc_pages()
  509. *
  510. * Implements an OP-TEE specific shared memory pool which is used
  511. * when dynamic shared memory is supported by secure world.
  512. *
  513. * The main function is optee_shm_pool_alloc_pages().
  514. */
  515. static int pool_op_alloc(struct tee_shm_pool *pool,
  516. struct tee_shm *shm, size_t size, size_t align)
  517. {
  518. /*
  519. * Shared memory private to the OP-TEE driver doesn't need
  520. * to be registered with OP-TEE.
  521. */
  522. if (shm->flags & TEE_SHM_PRIV)
  523. return tee_dyn_shm_alloc_helper(shm, size, align, NULL);
  524. return tee_dyn_shm_alloc_helper(shm, size, align, optee_shm_register);
  525. }
  526. static void pool_op_free(struct tee_shm_pool *pool,
  527. struct tee_shm *shm)
  528. {
  529. if (!(shm->flags & TEE_SHM_PRIV))
  530. tee_dyn_shm_free_helper(shm, optee_shm_unregister);
  531. else
  532. tee_dyn_shm_free_helper(shm, NULL);
  533. }
  534. static void pool_op_destroy_pool(struct tee_shm_pool *pool)
  535. {
  536. kfree(pool);
  537. }
  538. static const struct tee_shm_pool_ops pool_ops = {
  539. .alloc = pool_op_alloc,
  540. .free = pool_op_free,
  541. .destroy_pool = pool_op_destroy_pool,
  542. };
  543. /**
  544. * optee_shm_pool_alloc_pages() - create page-based allocator pool
  545. *
  546. * This pool is used when OP-TEE supports dymanic SHM. In this case
  547. * command buffers and such are allocated from kernel's own memory.
  548. */
  549. static struct tee_shm_pool *optee_shm_pool_alloc_pages(void)
  550. {
  551. struct tee_shm_pool *pool = kzalloc_obj(*pool);
  552. if (!pool)
  553. return ERR_PTR(-ENOMEM);
  554. pool->ops = &pool_ops;
  555. return pool;
  556. }
  557. /*
  558. * 4. Do a normal scheduled call into secure world
  559. *
  560. * The function optee_smc_do_call_with_arg() performs a normal scheduled
  561. * call into secure world. During this call may normal world request help
  562. * from normal world using RPCs, Remote Procedure Calls. This includes
  563. * delivery of non-secure interrupts to for instance allow rescheduling of
  564. * the current task.
  565. */
  566. static void handle_rpc_func_cmd_shm_free(struct tee_context *ctx,
  567. struct optee_msg_arg *arg)
  568. {
  569. struct tee_shm *shm;
  570. arg->ret_origin = TEEC_ORIGIN_COMMS;
  571. if (arg->num_params != 1 ||
  572. arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
  573. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  574. return;
  575. }
  576. shm = (struct tee_shm *)(unsigned long)arg->params[0].u.value.b;
  577. switch (arg->params[0].u.value.a) {
  578. case OPTEE_RPC_SHM_TYPE_APPL:
  579. optee_rpc_cmd_free_suppl(ctx, shm);
  580. break;
  581. case OPTEE_RPC_SHM_TYPE_KERNEL:
  582. tee_shm_free(shm);
  583. break;
  584. default:
  585. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  586. }
  587. arg->ret = TEEC_SUCCESS;
  588. }
  589. static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
  590. struct optee *optee,
  591. struct optee_msg_arg *arg,
  592. struct optee_call_ctx *call_ctx)
  593. {
  594. struct tee_shm *shm;
  595. size_t sz;
  596. size_t n;
  597. struct page **pages;
  598. size_t page_count;
  599. arg->ret_origin = TEEC_ORIGIN_COMMS;
  600. if (!arg->num_params ||
  601. arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
  602. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  603. return;
  604. }
  605. for (n = 1; n < arg->num_params; n++) {
  606. if (arg->params[n].attr != OPTEE_MSG_ATTR_TYPE_NONE) {
  607. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  608. return;
  609. }
  610. }
  611. sz = arg->params[0].u.value.b;
  612. switch (arg->params[0].u.value.a) {
  613. case OPTEE_RPC_SHM_TYPE_APPL:
  614. shm = optee_rpc_cmd_alloc_suppl(ctx, sz);
  615. break;
  616. case OPTEE_RPC_SHM_TYPE_KERNEL:
  617. shm = tee_shm_alloc_priv_buf(optee->ctx, sz);
  618. break;
  619. default:
  620. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  621. return;
  622. }
  623. if (IS_ERR(shm)) {
  624. arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
  625. return;
  626. }
  627. /*
  628. * If there are pages it's dynamically allocated shared memory (not
  629. * from the reserved shared memory pool) and needs to be
  630. * registered.
  631. */
  632. pages = tee_shm_get_pages(shm, &page_count);
  633. if (pages) {
  634. u64 *pages_list;
  635. pages_list = optee_allocate_pages_list(page_count);
  636. if (!pages_list) {
  637. arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
  638. goto bad;
  639. }
  640. call_ctx->pages_list = pages_list;
  641. call_ctx->num_entries = page_count;
  642. arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
  643. OPTEE_MSG_ATTR_NONCONTIG;
  644. /*
  645. * In the least bits of u.tmem.buf_ptr we store buffer offset
  646. * from 4k page, as described in OP-TEE ABI.
  647. */
  648. arg->params[0].u.tmem.buf_ptr = virt_to_phys(pages_list) |
  649. (tee_shm_get_page_offset(shm) &
  650. (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
  651. optee_fill_pages_list(pages_list, pages, page_count,
  652. tee_shm_get_page_offset(shm));
  653. } else {
  654. phys_addr_t pa;
  655. if (tee_shm_get_pa(shm, 0, &pa)) {
  656. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  657. goto bad;
  658. }
  659. arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT;
  660. arg->params[0].u.tmem.buf_ptr = pa;
  661. }
  662. arg->params[0].u.tmem.size = tee_shm_get_size(shm);
  663. arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
  664. arg->ret = TEEC_SUCCESS;
  665. return;
  666. bad:
  667. tee_shm_free(shm);
  668. }
  669. static void free_pages_list(struct optee_call_ctx *call_ctx)
  670. {
  671. if (call_ctx->pages_list) {
  672. optee_free_pages_list(call_ctx->pages_list,
  673. call_ctx->num_entries);
  674. call_ctx->pages_list = NULL;
  675. call_ctx->num_entries = 0;
  676. }
  677. }
  678. static void optee_rpc_finalize_call(struct optee_call_ctx *call_ctx)
  679. {
  680. free_pages_list(call_ctx);
  681. }
  682. static void handle_rpc_func_cmd(struct tee_context *ctx, struct optee *optee,
  683. struct optee_msg_arg *arg,
  684. struct optee_call_ctx *call_ctx)
  685. {
  686. switch (arg->cmd) {
  687. case OPTEE_RPC_CMD_SHM_ALLOC:
  688. free_pages_list(call_ctx);
  689. handle_rpc_func_cmd_shm_alloc(ctx, optee, arg, call_ctx);
  690. break;
  691. case OPTEE_RPC_CMD_SHM_FREE:
  692. handle_rpc_func_cmd_shm_free(ctx, arg);
  693. break;
  694. default:
  695. optee_rpc_cmd(ctx, optee, arg);
  696. }
  697. }
  698. /**
  699. * optee_handle_rpc() - handle RPC from secure world
  700. * @ctx: context doing the RPC
  701. * @rpc_arg: pointer to RPC arguments if any, or NULL if none
  702. * @param: value of registers for the RPC
  703. * @call_ctx: call context. Preserved during one OP-TEE invocation
  704. *
  705. * Result of RPC is written back into @param.
  706. */
  707. static void optee_handle_rpc(struct tee_context *ctx,
  708. struct optee_msg_arg *rpc_arg,
  709. struct optee_rpc_param *param,
  710. struct optee_call_ctx *call_ctx)
  711. {
  712. struct tee_device *teedev = ctx->teedev;
  713. struct optee *optee = tee_get_drvdata(teedev);
  714. struct optee_msg_arg *arg;
  715. struct tee_shm *shm;
  716. phys_addr_t pa;
  717. switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) {
  718. case OPTEE_SMC_RPC_FUNC_ALLOC:
  719. shm = tee_shm_alloc_priv_buf(optee->ctx, param->a1);
  720. if (!IS_ERR(shm) && !tee_shm_get_pa(shm, 0, &pa)) {
  721. reg_pair_from_64(&param->a1, &param->a2, pa);
  722. reg_pair_from_64(&param->a4, &param->a5,
  723. (unsigned long)shm);
  724. } else {
  725. param->a1 = 0;
  726. param->a2 = 0;
  727. param->a4 = 0;
  728. param->a5 = 0;
  729. }
  730. kmemleak_not_leak(shm);
  731. break;
  732. case OPTEE_SMC_RPC_FUNC_FREE:
  733. shm = reg_pair_to_ptr(param->a1, param->a2);
  734. tee_shm_free(shm);
  735. break;
  736. case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR:
  737. /*
  738. * A foreign interrupt was raised while secure world was
  739. * executing, since they are handled in Linux a dummy RPC is
  740. * performed to let Linux take the interrupt through the normal
  741. * vector.
  742. */
  743. break;
  744. case OPTEE_SMC_RPC_FUNC_CMD:
  745. if (rpc_arg) {
  746. arg = rpc_arg;
  747. } else {
  748. shm = reg_pair_to_ptr(param->a1, param->a2);
  749. arg = tee_shm_get_va(shm, 0);
  750. if (IS_ERR(arg)) {
  751. pr_err("%s: tee_shm_get_va %p failed\n",
  752. __func__, shm);
  753. break;
  754. }
  755. }
  756. handle_rpc_func_cmd(ctx, optee, arg, call_ctx);
  757. break;
  758. default:
  759. pr_warn("Unknown RPC func 0x%x\n",
  760. (u32)OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0));
  761. break;
  762. }
  763. param->a0 = OPTEE_SMC_CALL_RETURN_FROM_RPC;
  764. }
  765. /**
  766. * optee_smc_do_call_with_arg() - Do an SMC to OP-TEE in secure world
  767. * @ctx: calling context
  768. * @shm: shared memory holding the message to pass to secure world
  769. * @offs: offset of the message in @shm
  770. * @system_thread: true if caller requests TEE system thread support
  771. *
  772. * Does and SMC to OP-TEE in secure world and handles eventual resulting
  773. * Remote Procedure Calls (RPC) from OP-TEE.
  774. *
  775. * Returns return code from secure world, 0 is OK
  776. */
  777. static int optee_smc_do_call_with_arg(struct tee_context *ctx,
  778. struct tee_shm *shm, u_int offs,
  779. bool system_thread)
  780. {
  781. struct optee *optee = tee_get_drvdata(ctx->teedev);
  782. struct optee_call_waiter w;
  783. struct optee_rpc_param param = { };
  784. struct optee_call_ctx call_ctx = { };
  785. struct optee_msg_arg *rpc_arg = NULL;
  786. int rc;
  787. if (optee->rpc_param_count) {
  788. struct optee_msg_arg *arg;
  789. unsigned int rpc_arg_offs;
  790. arg = tee_shm_get_va(shm, offs);
  791. if (IS_ERR(arg))
  792. return PTR_ERR(arg);
  793. rpc_arg_offs = OPTEE_MSG_GET_ARG_SIZE(arg->num_params);
  794. rpc_arg = tee_shm_get_va(shm, offs + rpc_arg_offs);
  795. if (IS_ERR(rpc_arg))
  796. return PTR_ERR(rpc_arg);
  797. }
  798. if (rpc_arg && tee_shm_is_dynamic(shm)) {
  799. param.a0 = OPTEE_SMC_CALL_WITH_REGD_ARG;
  800. reg_pair_from_64(&param.a1, &param.a2, (u_long)shm);
  801. param.a3 = offs;
  802. } else {
  803. phys_addr_t parg;
  804. rc = tee_shm_get_pa(shm, offs, &parg);
  805. if (rc)
  806. return rc;
  807. if (rpc_arg)
  808. param.a0 = OPTEE_SMC_CALL_WITH_RPC_ARG;
  809. else
  810. param.a0 = OPTEE_SMC_CALL_WITH_ARG;
  811. reg_pair_from_64(&param.a1, &param.a2, parg);
  812. }
  813. /* Initialize waiter */
  814. optee_cq_wait_init(&optee->call_queue, &w, system_thread);
  815. while (true) {
  816. struct arm_smccc_res res;
  817. trace_optee_invoke_fn_begin(&param);
  818. optee->smc.invoke_fn(param.a0, param.a1, param.a2, param.a3,
  819. param.a4, param.a5, param.a6, param.a7,
  820. &res);
  821. trace_optee_invoke_fn_end(&param, &res);
  822. if (res.a0 == OPTEE_SMC_RETURN_ETHREAD_LIMIT) {
  823. /*
  824. * Out of threads in secure world, wait for a thread
  825. * become available.
  826. */
  827. optee_cq_wait_for_completion(&optee->call_queue, &w);
  828. } else if (OPTEE_SMC_RETURN_IS_RPC(res.a0)) {
  829. cond_resched();
  830. param.a0 = res.a0;
  831. param.a1 = res.a1;
  832. param.a2 = res.a2;
  833. param.a3 = res.a3;
  834. optee_handle_rpc(ctx, rpc_arg, &param, &call_ctx);
  835. } else {
  836. rc = res.a0;
  837. break;
  838. }
  839. }
  840. optee_rpc_finalize_call(&call_ctx);
  841. /*
  842. * We're done with our thread in secure world, if there's any
  843. * thread waiters wake up one.
  844. */
  845. optee_cq_wait_final(&optee->call_queue, &w);
  846. return rc;
  847. }
  848. static int optee_smc_lend_protmem(struct optee *optee, struct tee_shm *protmem,
  849. u32 *mem_attrs, unsigned int ma_count,
  850. u32 use_case)
  851. {
  852. struct optee_shm_arg_entry *entry;
  853. struct optee_msg_arg *msg_arg;
  854. struct tee_shm *shm;
  855. u_int offs;
  856. int rc;
  857. msg_arg = optee_get_msg_arg(optee->ctx, 2, &entry, &shm, &offs);
  858. if (IS_ERR(msg_arg))
  859. return PTR_ERR(msg_arg);
  860. msg_arg->cmd = OPTEE_MSG_CMD_LEND_PROTMEM;
  861. msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
  862. msg_arg->params[0].u.value.a = use_case;
  863. msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_TMEM_INPUT;
  864. msg_arg->params[1].u.tmem.buf_ptr = protmem->paddr;
  865. msg_arg->params[1].u.tmem.size = protmem->size;
  866. msg_arg->params[1].u.tmem.shm_ref = (u_long)protmem;
  867. rc = optee->ops->do_call_with_arg(optee->ctx, shm, offs, false);
  868. if (rc)
  869. goto out;
  870. if (msg_arg->ret != TEEC_SUCCESS) {
  871. rc = -EINVAL;
  872. goto out;
  873. }
  874. protmem->sec_world_id = (u_long)protmem;
  875. out:
  876. optee_free_msg_arg(optee->ctx, entry, offs);
  877. return rc;
  878. }
  879. static int optee_smc_reclaim_protmem(struct optee *optee,
  880. struct tee_shm *protmem)
  881. {
  882. struct optee_shm_arg_entry *entry;
  883. struct optee_msg_arg *msg_arg;
  884. struct tee_shm *shm;
  885. u_int offs;
  886. int rc;
  887. msg_arg = optee_get_msg_arg(optee->ctx, 1, &entry, &shm, &offs);
  888. if (IS_ERR(msg_arg))
  889. return PTR_ERR(msg_arg);
  890. msg_arg->cmd = OPTEE_MSG_CMD_RECLAIM_PROTMEM;
  891. msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
  892. msg_arg->params[0].u.rmem.shm_ref = (u_long)protmem;
  893. rc = optee->ops->do_call_with_arg(optee->ctx, shm, offs, false);
  894. if (rc)
  895. goto out;
  896. if (msg_arg->ret != TEEC_SUCCESS)
  897. rc = -EINVAL;
  898. out:
  899. optee_free_msg_arg(optee->ctx, entry, offs);
  900. return rc;
  901. }
  902. /*
  903. * 5. Asynchronous notification
  904. */
  905. static u32 get_async_notif_value(optee_invoke_fn *invoke_fn, bool *value_valid,
  906. bool *value_pending)
  907. {
  908. struct arm_smccc_res res;
  909. invoke_fn(OPTEE_SMC_GET_ASYNC_NOTIF_VALUE, 0, 0, 0, 0, 0, 0, 0, &res);
  910. if (res.a0) {
  911. *value_valid = false;
  912. return 0;
  913. }
  914. *value_valid = (res.a2 & OPTEE_SMC_ASYNC_NOTIF_VALUE_VALID);
  915. *value_pending = (res.a2 & OPTEE_SMC_ASYNC_NOTIF_VALUE_PENDING);
  916. return res.a1;
  917. }
  918. static irqreturn_t irq_handler(struct optee *optee)
  919. {
  920. bool do_bottom_half = false;
  921. bool value_valid;
  922. bool value_pending;
  923. u32 value;
  924. do {
  925. value = get_async_notif_value(optee->smc.invoke_fn,
  926. &value_valid, &value_pending);
  927. if (!value_valid)
  928. break;
  929. if (value == OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_BOTTOM_HALF)
  930. do_bottom_half = true;
  931. else
  932. optee_notif_send(optee, value);
  933. } while (value_pending);
  934. if (do_bottom_half)
  935. return IRQ_WAKE_THREAD;
  936. return IRQ_HANDLED;
  937. }
  938. static irqreturn_t notif_irq_handler(int irq, void *dev_id)
  939. {
  940. struct optee *optee = dev_id;
  941. return irq_handler(optee);
  942. }
  943. static irqreturn_t notif_irq_thread_fn(int irq, void *dev_id)
  944. {
  945. struct optee *optee = dev_id;
  946. optee_do_bottom_half(optee->ctx);
  947. return IRQ_HANDLED;
  948. }
  949. static int init_irq(struct optee *optee, u_int irq)
  950. {
  951. int rc;
  952. rc = request_threaded_irq(irq, notif_irq_handler,
  953. notif_irq_thread_fn,
  954. 0, "optee_notification", optee);
  955. if (rc)
  956. return rc;
  957. optee->smc.notif_irq = irq;
  958. return 0;
  959. }
  960. static irqreturn_t notif_pcpu_irq_handler(int irq, void *dev_id)
  961. {
  962. struct optee_pcpu *pcpu = dev_id;
  963. struct optee *optee = pcpu->optee;
  964. if (irq_handler(optee) == IRQ_WAKE_THREAD)
  965. queue_work(optee->smc.notif_pcpu_wq,
  966. &optee->smc.notif_pcpu_work);
  967. return IRQ_HANDLED;
  968. }
  969. static void notif_pcpu_irq_work_fn(struct work_struct *work)
  970. {
  971. struct optee_smc *optee_smc = container_of(work, struct optee_smc,
  972. notif_pcpu_work);
  973. struct optee *optee = container_of(optee_smc, struct optee, smc);
  974. optee_do_bottom_half(optee->ctx);
  975. }
  976. static int init_pcpu_irq(struct optee *optee, u_int irq)
  977. {
  978. struct optee_pcpu __percpu *optee_pcpu;
  979. int cpu, rc;
  980. optee_pcpu = alloc_percpu(struct optee_pcpu);
  981. if (!optee_pcpu)
  982. return -ENOMEM;
  983. for_each_present_cpu(cpu)
  984. per_cpu_ptr(optee_pcpu, cpu)->optee = optee;
  985. rc = request_percpu_irq(irq, notif_pcpu_irq_handler,
  986. "optee_pcpu_notification", optee_pcpu);
  987. if (rc)
  988. goto err_free_pcpu;
  989. INIT_WORK(&optee->smc.notif_pcpu_work, notif_pcpu_irq_work_fn);
  990. optee->smc.notif_pcpu_wq = create_workqueue("optee_pcpu_notification");
  991. if (!optee->smc.notif_pcpu_wq) {
  992. rc = -EINVAL;
  993. goto err_free_pcpu_irq;
  994. }
  995. optee->smc.optee_pcpu = optee_pcpu;
  996. optee->smc.notif_irq = irq;
  997. pcpu_irq_num = irq;
  998. rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "optee/pcpu-notif:starting",
  999. optee_cpuhp_enable_pcpu_irq,
  1000. optee_cpuhp_disable_pcpu_irq);
  1001. if (!rc)
  1002. rc = -EINVAL;
  1003. if (rc < 0)
  1004. goto err_free_pcpu_irq;
  1005. optee->smc.notif_cpuhp_state = rc;
  1006. return 0;
  1007. err_free_pcpu_irq:
  1008. free_percpu_irq(irq, optee_pcpu);
  1009. err_free_pcpu:
  1010. free_percpu(optee_pcpu);
  1011. return rc;
  1012. }
  1013. static int optee_smc_notif_init_irq(struct optee *optee, u_int irq)
  1014. {
  1015. if (irq_is_percpu_devid(irq))
  1016. return init_pcpu_irq(optee, irq);
  1017. else
  1018. return init_irq(optee, irq);
  1019. }
  1020. static void uninit_pcpu_irq(struct optee *optee)
  1021. {
  1022. cpuhp_remove_state(optee->smc.notif_cpuhp_state);
  1023. destroy_workqueue(optee->smc.notif_pcpu_wq);
  1024. free_percpu_irq(optee->smc.notif_irq, optee->smc.optee_pcpu);
  1025. free_percpu(optee->smc.optee_pcpu);
  1026. }
  1027. static void optee_smc_notif_uninit_irq(struct optee *optee)
  1028. {
  1029. if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_ASYNC_NOTIF) {
  1030. optee_stop_async_notif(optee->ctx);
  1031. if (optee->smc.notif_irq) {
  1032. if (irq_is_percpu_devid(optee->smc.notif_irq))
  1033. uninit_pcpu_irq(optee);
  1034. else
  1035. free_irq(optee->smc.notif_irq, optee);
  1036. irq_dispose_mapping(optee->smc.notif_irq);
  1037. }
  1038. }
  1039. }
  1040. /*
  1041. * 6. Driver initialization
  1042. *
  1043. * During driver initialization is secure world probed to find out which
  1044. * features it supports so the driver can be initialized with a matching
  1045. * configuration. This involves for instance support for dynamic shared
  1046. * memory instead of a static memory carvout.
  1047. */
  1048. static void optee_get_version(struct tee_device *teedev,
  1049. struct tee_ioctl_version_data *vers)
  1050. {
  1051. struct tee_ioctl_version_data v = {
  1052. .impl_id = TEE_IMPL_ID_OPTEE,
  1053. .impl_caps = TEE_OPTEE_CAP_TZ,
  1054. .gen_caps = TEE_GEN_CAP_GP,
  1055. };
  1056. struct optee *optee = tee_get_drvdata(teedev);
  1057. if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
  1058. v.gen_caps |= TEE_GEN_CAP_REG_MEM;
  1059. if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_MEMREF_NULL)
  1060. v.gen_caps |= TEE_GEN_CAP_MEMREF_NULL;
  1061. *vers = v;
  1062. }
  1063. static int optee_smc_open(struct tee_context *ctx)
  1064. {
  1065. struct optee *optee = tee_get_drvdata(ctx->teedev);
  1066. u32 sec_caps = optee->smc.sec_caps;
  1067. return optee_open(ctx, sec_caps & OPTEE_SMC_SEC_CAP_MEMREF_NULL);
  1068. }
  1069. static const struct tee_driver_ops optee_clnt_ops = {
  1070. .get_version = optee_get_version,
  1071. .get_tee_revision = optee_get_revision,
  1072. .open = optee_smc_open,
  1073. .release = optee_release,
  1074. .open_session = optee_open_session,
  1075. .close_session = optee_close_session,
  1076. .system_session = optee_system_session,
  1077. .invoke_func = optee_invoke_func,
  1078. .cancel_req = optee_cancel_req,
  1079. .shm_register = optee_shm_register,
  1080. .shm_unregister = optee_shm_unregister,
  1081. };
  1082. static const struct tee_desc optee_clnt_desc = {
  1083. .name = DRIVER_NAME "-clnt",
  1084. .ops = &optee_clnt_ops,
  1085. .owner = THIS_MODULE,
  1086. };
  1087. static const struct tee_driver_ops optee_supp_ops = {
  1088. .get_version = optee_get_version,
  1089. .get_tee_revision = optee_get_revision,
  1090. .open = optee_smc_open,
  1091. .release = optee_release_supp,
  1092. .supp_recv = optee_supp_recv,
  1093. .supp_send = optee_supp_send,
  1094. .shm_register = optee_shm_register_supp,
  1095. .shm_unregister = optee_shm_unregister_supp,
  1096. };
  1097. static const struct tee_desc optee_supp_desc = {
  1098. .name = DRIVER_NAME "-supp",
  1099. .ops = &optee_supp_ops,
  1100. .owner = THIS_MODULE,
  1101. .flags = TEE_DESC_PRIVILEGED,
  1102. };
  1103. static const struct optee_ops optee_ops = {
  1104. .do_call_with_arg = optee_smc_do_call_with_arg,
  1105. .to_msg_param = optee_to_msg_param,
  1106. .from_msg_param = optee_from_msg_param,
  1107. .lend_protmem = optee_smc_lend_protmem,
  1108. .reclaim_protmem = optee_smc_reclaim_protmem,
  1109. };
  1110. static int enable_async_notif(optee_invoke_fn *invoke_fn)
  1111. {
  1112. struct arm_smccc_res res;
  1113. invoke_fn(OPTEE_SMC_ENABLE_ASYNC_NOTIF, 0, 0, 0, 0, 0, 0, 0, &res);
  1114. if (res.a0)
  1115. return -EINVAL;
  1116. return 0;
  1117. }
  1118. static bool optee_msg_api_uid_is_optee_api(optee_invoke_fn *invoke_fn)
  1119. {
  1120. struct arm_smccc_res res;
  1121. invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res);
  1122. if (res.a0 == OPTEE_MSG_UID_0 && res.a1 == OPTEE_MSG_UID_1 &&
  1123. res.a2 == OPTEE_MSG_UID_2 && res.a3 == OPTEE_MSG_UID_3)
  1124. return true;
  1125. return false;
  1126. }
  1127. #ifdef CONFIG_OPTEE_INSECURE_LOAD_IMAGE
  1128. static bool optee_msg_api_uid_is_optee_image_load(optee_invoke_fn *invoke_fn)
  1129. {
  1130. struct arm_smccc_res res;
  1131. invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res);
  1132. if (res.a0 == OPTEE_MSG_IMAGE_LOAD_UID_0 &&
  1133. res.a1 == OPTEE_MSG_IMAGE_LOAD_UID_1 &&
  1134. res.a2 == OPTEE_MSG_IMAGE_LOAD_UID_2 &&
  1135. res.a3 == OPTEE_MSG_IMAGE_LOAD_UID_3)
  1136. return true;
  1137. return false;
  1138. }
  1139. #endif
  1140. static void optee_msg_get_os_revision(optee_invoke_fn *invoke_fn,
  1141. struct optee_revision *revision)
  1142. {
  1143. union {
  1144. struct arm_smccc_res smccc;
  1145. struct optee_smc_call_get_os_revision_result result;
  1146. } res = {
  1147. .result = {
  1148. .build_id = 0
  1149. }
  1150. };
  1151. invoke_fn(OPTEE_SMC_CALL_GET_OS_REVISION, 0, 0, 0, 0, 0, 0, 0,
  1152. &res.smccc);
  1153. if (revision) {
  1154. revision->os_major = res.result.major;
  1155. revision->os_minor = res.result.minor;
  1156. revision->os_build_id = res.result.build_id;
  1157. }
  1158. if (res.result.build_id)
  1159. pr_info("revision %lu.%lu (%0*lx)", res.result.major,
  1160. res.result.minor, (int)sizeof(res.result.build_id) * 2,
  1161. res.result.build_id);
  1162. else
  1163. pr_info("revision %lu.%lu", res.result.major, res.result.minor);
  1164. }
  1165. static bool optee_msg_api_revision_is_compatible(optee_invoke_fn *invoke_fn)
  1166. {
  1167. union {
  1168. struct arm_smccc_res smccc;
  1169. struct optee_smc_calls_revision_result result;
  1170. } res;
  1171. invoke_fn(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
  1172. if (res.result.major == OPTEE_MSG_REVISION_MAJOR &&
  1173. (int)res.result.minor >= OPTEE_MSG_REVISION_MINOR)
  1174. return true;
  1175. return false;
  1176. }
  1177. static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
  1178. u32 *sec_caps, u32 *max_notif_value,
  1179. unsigned int *rpc_param_count)
  1180. {
  1181. union {
  1182. struct arm_smccc_res smccc;
  1183. struct optee_smc_exchange_capabilities_result result;
  1184. } res;
  1185. u32 a1 = 0;
  1186. /*
  1187. * TODO This isn't enough to tell if it's UP system (from kernel
  1188. * point of view) or not, is_smp() returns the information
  1189. * needed, but can't be called directly from here.
  1190. */
  1191. if (!IS_ENABLED(CONFIG_SMP) || nr_cpu_ids == 1)
  1192. a1 |= OPTEE_SMC_NSEC_CAP_UNIPROCESSOR;
  1193. invoke_fn(OPTEE_SMC_EXCHANGE_CAPABILITIES, a1, 0, 0, 0, 0, 0, 0,
  1194. &res.smccc);
  1195. if (res.result.status != OPTEE_SMC_RETURN_OK)
  1196. return false;
  1197. *sec_caps = res.result.capabilities;
  1198. if (*sec_caps & OPTEE_SMC_SEC_CAP_ASYNC_NOTIF)
  1199. *max_notif_value = res.result.max_notif_value;
  1200. else
  1201. *max_notif_value = OPTEE_DEFAULT_MAX_NOTIF_VALUE;
  1202. if (*sec_caps & OPTEE_SMC_SEC_CAP_RPC_ARG)
  1203. *rpc_param_count = (u8)res.result.data;
  1204. else
  1205. *rpc_param_count = 0;
  1206. return true;
  1207. }
  1208. static unsigned int optee_msg_get_thread_count(optee_invoke_fn *invoke_fn)
  1209. {
  1210. struct arm_smccc_res res;
  1211. invoke_fn(OPTEE_SMC_GET_THREAD_COUNT, 0, 0, 0, 0, 0, 0, 0, &res);
  1212. if (res.a0)
  1213. return 0;
  1214. return res.a1;
  1215. }
  1216. static struct tee_shm_pool *
  1217. optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm)
  1218. {
  1219. union {
  1220. struct arm_smccc_res smccc;
  1221. struct optee_smc_get_shm_config_result result;
  1222. } res;
  1223. unsigned long vaddr;
  1224. phys_addr_t paddr;
  1225. size_t size;
  1226. phys_addr_t begin;
  1227. phys_addr_t end;
  1228. void *va;
  1229. void *rc;
  1230. invoke_fn(OPTEE_SMC_GET_SHM_CONFIG, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
  1231. if (res.result.status != OPTEE_SMC_RETURN_OK) {
  1232. pr_err("static shm service not available\n");
  1233. return ERR_PTR(-ENOENT);
  1234. }
  1235. if (res.result.settings != OPTEE_SMC_SHM_CACHED) {
  1236. pr_err("only normal cached shared memory supported\n");
  1237. return ERR_PTR(-EINVAL);
  1238. }
  1239. begin = roundup(res.result.start, PAGE_SIZE);
  1240. end = rounddown(res.result.start + res.result.size, PAGE_SIZE);
  1241. paddr = begin;
  1242. size = end - begin;
  1243. va = memremap(paddr, size, MEMREMAP_WB);
  1244. if (!va) {
  1245. pr_err("shared memory ioremap failed\n");
  1246. return ERR_PTR(-EINVAL);
  1247. }
  1248. vaddr = (unsigned long)va;
  1249. rc = tee_shm_pool_alloc_res_mem(vaddr, paddr, size,
  1250. OPTEE_MIN_STATIC_POOL_ALIGN);
  1251. if (IS_ERR(rc))
  1252. memunmap(va);
  1253. else
  1254. *memremaped_shm = va;
  1255. return rc;
  1256. }
  1257. /* Simple wrapper functions to be able to use a function pointer */
  1258. static void optee_smccc_smc(unsigned long a0, unsigned long a1,
  1259. unsigned long a2, unsigned long a3,
  1260. unsigned long a4, unsigned long a5,
  1261. unsigned long a6, unsigned long a7,
  1262. struct arm_smccc_res *res)
  1263. {
  1264. arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  1265. }
  1266. static void optee_smccc_hvc(unsigned long a0, unsigned long a1,
  1267. unsigned long a2, unsigned long a3,
  1268. unsigned long a4, unsigned long a5,
  1269. unsigned long a6, unsigned long a7,
  1270. struct arm_smccc_res *res)
  1271. {
  1272. arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  1273. }
  1274. static optee_invoke_fn *get_invoke_func(struct device *dev)
  1275. {
  1276. const char *method;
  1277. pr_info("probing for conduit method.\n");
  1278. if (device_property_read_string(dev, "method", &method)) {
  1279. pr_warn("missing \"method\" property\n");
  1280. return ERR_PTR(-ENXIO);
  1281. }
  1282. if (!strcmp("hvc", method))
  1283. return optee_smccc_hvc;
  1284. else if (!strcmp("smc", method))
  1285. return optee_smccc_smc;
  1286. pr_warn("invalid \"method\" property: %s\n", method);
  1287. return ERR_PTR(-EINVAL);
  1288. }
  1289. /* optee_remove - Device Removal Routine
  1290. * @pdev: platform device information struct
  1291. *
  1292. * optee_remove is called by platform subsystem to alert the driver
  1293. * that it should release the device
  1294. */
  1295. static void optee_smc_remove(struct platform_device *pdev)
  1296. {
  1297. struct optee *optee = platform_get_drvdata(pdev);
  1298. /*
  1299. * Ask OP-TEE to free all cached shared memory objects to decrease
  1300. * reference counters and also avoid wild pointers in secure world
  1301. * into the old shared memory range.
  1302. */
  1303. if (!optee->rpc_param_count)
  1304. optee_disable_shm_cache(optee);
  1305. optee_smc_notif_uninit_irq(optee);
  1306. optee_remove_common(optee);
  1307. if (optee->smc.memremaped_shm)
  1308. memunmap(optee->smc.memremaped_shm);
  1309. kfree(optee);
  1310. }
  1311. /* optee_shutdown - Device Removal Routine
  1312. * @pdev: platform device information struct
  1313. *
  1314. * platform_shutdown is called by the platform subsystem to alert
  1315. * the driver that a shutdown, reboot, or kexec is happening and
  1316. * device must be disabled.
  1317. */
  1318. static void optee_shutdown(struct platform_device *pdev)
  1319. {
  1320. struct optee *optee = platform_get_drvdata(pdev);
  1321. if (!optee->rpc_param_count)
  1322. optee_disable_shm_cache(optee);
  1323. }
  1324. #ifdef CONFIG_OPTEE_INSECURE_LOAD_IMAGE
  1325. #define OPTEE_FW_IMAGE "optee/tee.bin"
  1326. static optee_invoke_fn *cpuhp_invoke_fn;
  1327. static int optee_cpuhp_probe(unsigned int cpu)
  1328. {
  1329. /*
  1330. * Invoking a call on a CPU will cause OP-TEE to perform the required
  1331. * setup for that CPU. Just invoke the call to get the UID since that
  1332. * has no side effects.
  1333. */
  1334. if (optee_msg_api_uid_is_optee_api(cpuhp_invoke_fn))
  1335. return 0;
  1336. else
  1337. return -EINVAL;
  1338. }
  1339. static int optee_load_fw(struct platform_device *pdev,
  1340. optee_invoke_fn *invoke_fn)
  1341. {
  1342. const struct firmware *fw = NULL;
  1343. struct arm_smccc_res res;
  1344. phys_addr_t data_pa;
  1345. u8 *data_buf = NULL;
  1346. u64 data_size;
  1347. u32 data_pa_high, data_pa_low;
  1348. u32 data_size_high, data_size_low;
  1349. int rc;
  1350. int hp_state;
  1351. if (!optee_msg_api_uid_is_optee_image_load(invoke_fn))
  1352. return 0;
  1353. rc = request_firmware(&fw, OPTEE_FW_IMAGE, &pdev->dev);
  1354. if (rc) {
  1355. /*
  1356. * The firmware in the rootfs will not be accessible until we
  1357. * are in the SYSTEM_RUNNING state, so return EPROBE_DEFER until
  1358. * that point.
  1359. */
  1360. if (system_state < SYSTEM_RUNNING)
  1361. return -EPROBE_DEFER;
  1362. goto fw_err;
  1363. }
  1364. data_size = fw->size;
  1365. /*
  1366. * This uses the GFP_DMA flag to ensure we are allocated memory in the
  1367. * 32-bit space since TF-A cannot map memory beyond the 32-bit boundary.
  1368. */
  1369. data_buf = kmemdup(fw->data, fw->size, GFP_KERNEL | GFP_DMA);
  1370. if (!data_buf) {
  1371. rc = -ENOMEM;
  1372. goto fw_err;
  1373. }
  1374. data_pa = virt_to_phys(data_buf);
  1375. reg_pair_from_64(&data_pa_high, &data_pa_low, data_pa);
  1376. reg_pair_from_64(&data_size_high, &data_size_low, data_size);
  1377. goto fw_load;
  1378. fw_err:
  1379. pr_warn("image loading failed\n");
  1380. data_pa_high = 0;
  1381. data_pa_low = 0;
  1382. data_size_high = 0;
  1383. data_size_low = 0;
  1384. fw_load:
  1385. /*
  1386. * Always invoke the SMC, even if loading the image fails, to indicate
  1387. * to EL3 that we have passed the point where it should allow invoking
  1388. * this SMC.
  1389. */
  1390. pr_warn("OP-TEE image loaded from kernel, this can be insecure");
  1391. invoke_fn(OPTEE_SMC_CALL_LOAD_IMAGE, data_size_high, data_size_low,
  1392. data_pa_high, data_pa_low, 0, 0, 0, &res);
  1393. if (!rc)
  1394. rc = res.a0;
  1395. release_firmware(fw);
  1396. kfree(data_buf);
  1397. if (!rc) {
  1398. /*
  1399. * We need to initialize OP-TEE on all other running cores as
  1400. * well. Any cores that aren't running yet will get initialized
  1401. * when they are brought up by the power management functions in
  1402. * TF-A which are registered by the OP-TEE SPD. Due to that we
  1403. * can un-register the callback right after registering it.
  1404. */
  1405. cpuhp_invoke_fn = invoke_fn;
  1406. hp_state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "optee:probe",
  1407. optee_cpuhp_probe, NULL);
  1408. if (hp_state < 0) {
  1409. pr_warn("Failed with CPU hotplug setup for OP-TEE");
  1410. return -EINVAL;
  1411. }
  1412. cpuhp_remove_state(hp_state);
  1413. cpuhp_invoke_fn = NULL;
  1414. }
  1415. return rc;
  1416. }
  1417. #else
  1418. static inline int optee_load_fw(struct platform_device *pdev,
  1419. optee_invoke_fn *invoke_fn)
  1420. {
  1421. return 0;
  1422. }
  1423. #endif
  1424. static struct tee_protmem_pool *static_protmem_pool_init(struct optee *optee)
  1425. {
  1426. #if IS_ENABLED(CONFIG_OPTEE_STATIC_PROTMEM_POOL)
  1427. union {
  1428. struct arm_smccc_res smccc;
  1429. struct optee_smc_get_protmem_config_result result;
  1430. } res;
  1431. struct tee_protmem_pool *pool;
  1432. void *p;
  1433. int rc;
  1434. optee->smc.invoke_fn(OPTEE_SMC_GET_PROTMEM_CONFIG, 0, 0, 0, 0,
  1435. 0, 0, 0, &res.smccc);
  1436. if (res.result.status != OPTEE_SMC_RETURN_OK)
  1437. return ERR_PTR(-EINVAL);
  1438. rc = optee_set_dma_mask(optee, res.result.pa_width);
  1439. if (rc)
  1440. return ERR_PTR(rc);
  1441. /*
  1442. * Map the memory as uncached to make sure the kernel can work with
  1443. * __pfn_to_page() and friends since that's needed when passing the
  1444. * protected DMA-buf to a device. The memory should otherwise not
  1445. * be touched by the kernel since it's likely to cause an external
  1446. * abort due to the protection status.
  1447. */
  1448. p = devm_memremap(&optee->teedev->dev, res.result.start,
  1449. res.result.size, MEMREMAP_WC);
  1450. if (IS_ERR(p))
  1451. return p;
  1452. pool = tee_protmem_static_pool_alloc(res.result.start, res.result.size);
  1453. if (IS_ERR(pool))
  1454. devm_memunmap(&optee->teedev->dev, p);
  1455. return pool;
  1456. #else
  1457. return ERR_PTR(-EINVAL);
  1458. #endif
  1459. }
  1460. static int optee_protmem_pool_init(struct optee *optee)
  1461. {
  1462. bool protm = optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_PROTMEM;
  1463. bool dyn_protm = optee->smc.sec_caps &
  1464. OPTEE_SMC_SEC_CAP_DYNAMIC_PROTMEM;
  1465. enum tee_dma_heap_id heap_id = TEE_DMA_HEAP_SECURE_VIDEO_PLAY;
  1466. struct tee_protmem_pool *pool = ERR_PTR(-EINVAL);
  1467. int rc = -EINVAL;
  1468. if (!protm && !dyn_protm)
  1469. return 0;
  1470. if (protm)
  1471. pool = static_protmem_pool_init(optee);
  1472. if (dyn_protm && IS_ERR(pool))
  1473. pool = optee_protmem_alloc_dyn_pool(optee, heap_id);
  1474. if (IS_ERR(pool))
  1475. return PTR_ERR(pool);
  1476. rc = tee_device_register_dma_heap(optee->teedev, heap_id, pool);
  1477. if (rc)
  1478. pool->ops->destroy_pool(pool);
  1479. return rc;
  1480. }
  1481. static int optee_probe(struct platform_device *pdev)
  1482. {
  1483. optee_invoke_fn *invoke_fn;
  1484. struct tee_shm_pool *pool = ERR_PTR(-EINVAL);
  1485. struct optee *optee = NULL;
  1486. void *memremaped_shm = NULL;
  1487. unsigned int rpc_param_count;
  1488. unsigned int thread_count;
  1489. struct tee_device *teedev;
  1490. struct tee_context *ctx;
  1491. u32 max_notif_value;
  1492. u32 arg_cache_flags;
  1493. u32 sec_caps;
  1494. int rc;
  1495. invoke_fn = get_invoke_func(&pdev->dev);
  1496. if (IS_ERR(invoke_fn))
  1497. return PTR_ERR(invoke_fn);
  1498. rc = optee_load_fw(pdev, invoke_fn);
  1499. if (rc)
  1500. return rc;
  1501. if (!optee_msg_api_uid_is_optee_api(invoke_fn)) {
  1502. pr_warn("api uid mismatch\n");
  1503. return -EINVAL;
  1504. }
  1505. if (!optee_msg_api_revision_is_compatible(invoke_fn)) {
  1506. pr_warn("api revision mismatch\n");
  1507. return -EINVAL;
  1508. }
  1509. thread_count = optee_msg_get_thread_count(invoke_fn);
  1510. if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps,
  1511. &max_notif_value,
  1512. &rpc_param_count)) {
  1513. pr_warn("capabilities mismatch\n");
  1514. return -EINVAL;
  1515. }
  1516. /*
  1517. * Try to use dynamic shared memory if possible
  1518. */
  1519. if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) {
  1520. /*
  1521. * If we have OPTEE_SMC_SEC_CAP_RPC_ARG we can ask
  1522. * optee_get_msg_arg() to pre-register (by having
  1523. * OPTEE_SHM_ARG_ALLOC_PRIV cleared) the page used to pass
  1524. * an argument struct.
  1525. *
  1526. * With the page is pre-registered we can use a non-zero
  1527. * offset for argument struct, this is indicated with
  1528. * OPTEE_SHM_ARG_SHARED.
  1529. *
  1530. * This means that optee_smc_do_call_with_arg() will use
  1531. * OPTEE_SMC_CALL_WITH_REGD_ARG for pre-registered pages.
  1532. */
  1533. if (sec_caps & OPTEE_SMC_SEC_CAP_RPC_ARG)
  1534. arg_cache_flags = OPTEE_SHM_ARG_SHARED;
  1535. else
  1536. arg_cache_flags = OPTEE_SHM_ARG_ALLOC_PRIV;
  1537. pool = optee_shm_pool_alloc_pages();
  1538. }
  1539. /*
  1540. * If dynamic shared memory is not available or failed - try static one
  1541. */
  1542. if (IS_ERR(pool) && (sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM)) {
  1543. /*
  1544. * The static memory pool can use non-zero page offsets so
  1545. * let optee_get_msg_arg() know that with OPTEE_SHM_ARG_SHARED.
  1546. *
  1547. * optee_get_msg_arg() should not pre-register the
  1548. * allocated page used to pass an argument struct, this is
  1549. * indicated with OPTEE_SHM_ARG_ALLOC_PRIV.
  1550. *
  1551. * This means that optee_smc_do_call_with_arg() will use
  1552. * OPTEE_SMC_CALL_WITH_ARG if rpc_param_count is 0, else
  1553. * OPTEE_SMC_CALL_WITH_RPC_ARG.
  1554. */
  1555. arg_cache_flags = OPTEE_SHM_ARG_SHARED |
  1556. OPTEE_SHM_ARG_ALLOC_PRIV;
  1557. pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm);
  1558. }
  1559. if (IS_ERR(pool))
  1560. return PTR_ERR(pool);
  1561. optee = kzalloc_obj(*optee);
  1562. if (!optee) {
  1563. rc = -ENOMEM;
  1564. goto err_free_shm_pool;
  1565. }
  1566. optee_msg_get_os_revision(invoke_fn, &optee->revision);
  1567. optee->ops = &optee_ops;
  1568. optee->smc.invoke_fn = invoke_fn;
  1569. optee->smc.sec_caps = sec_caps;
  1570. optee->rpc_param_count = rpc_param_count;
  1571. if (IS_REACHABLE(CONFIG_RPMB) &&
  1572. (sec_caps & OPTEE_SMC_SEC_CAP_RPMB_PROBE))
  1573. optee->in_kernel_rpmb_routing = true;
  1574. teedev = tee_device_alloc(&optee_clnt_desc, NULL, pool, optee);
  1575. if (IS_ERR(teedev)) {
  1576. rc = PTR_ERR(teedev);
  1577. goto err_free_optee;
  1578. }
  1579. optee->teedev = teedev;
  1580. teedev = tee_device_alloc(&optee_supp_desc, NULL, pool, optee);
  1581. if (IS_ERR(teedev)) {
  1582. rc = PTR_ERR(teedev);
  1583. goto err_unreg_teedev;
  1584. }
  1585. optee->supp_teedev = teedev;
  1586. optee_set_dev_group(optee);
  1587. rc = tee_device_register(optee->teedev);
  1588. if (rc)
  1589. goto err_unreg_supp_teedev;
  1590. rc = tee_device_register(optee->supp_teedev);
  1591. if (rc)
  1592. goto err_unreg_supp_teedev;
  1593. optee_cq_init(&optee->call_queue, thread_count);
  1594. optee_supp_init(&optee->supp);
  1595. optee->smc.memremaped_shm = memremaped_shm;
  1596. optee->pool = pool;
  1597. optee_shm_arg_cache_init(optee, arg_cache_flags);
  1598. mutex_init(&optee->rpmb_dev_mutex);
  1599. platform_set_drvdata(pdev, optee);
  1600. ctx = teedev_open(optee->teedev);
  1601. if (IS_ERR(ctx)) {
  1602. rc = PTR_ERR(ctx);
  1603. goto err_supp_uninit;
  1604. }
  1605. optee->ctx = ctx;
  1606. rc = optee_notif_init(optee, max_notif_value);
  1607. if (rc)
  1608. goto err_close_ctx;
  1609. if (sec_caps & OPTEE_SMC_SEC_CAP_ASYNC_NOTIF) {
  1610. unsigned int irq;
  1611. rc = platform_get_irq(pdev, 0);
  1612. if (rc < 0) {
  1613. pr_err("platform_get_irq: ret %d\n", rc);
  1614. goto err_notif_uninit;
  1615. }
  1616. irq = rc;
  1617. rc = optee_smc_notif_init_irq(optee, irq);
  1618. if (rc) {
  1619. irq_dispose_mapping(irq);
  1620. goto err_notif_uninit;
  1621. }
  1622. enable_async_notif(optee->smc.invoke_fn);
  1623. pr_info("Asynchronous notifications enabled\n");
  1624. }
  1625. if (optee_protmem_pool_init(optee))
  1626. pr_info("Protected memory service not available\n");
  1627. /*
  1628. * Ensure that there are no pre-existing shm objects before enabling
  1629. * the shm cache so that there's no chance of receiving an invalid
  1630. * address during shutdown. This could occur, for example, if we're
  1631. * kexec booting from an older kernel that did not properly cleanup the
  1632. * shm cache.
  1633. */
  1634. optee_disable_unmapped_shm_cache(optee);
  1635. /*
  1636. * Only enable the shm cache in case we're not able to pass the RPC
  1637. * arg struct right after the normal arg struct.
  1638. */
  1639. if (!optee->rpc_param_count)
  1640. optee_enable_shm_cache(optee);
  1641. if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
  1642. pr_info("dynamic shared memory is enabled\n");
  1643. rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);
  1644. if (rc)
  1645. goto err_disable_shm_cache;
  1646. INIT_WORK(&optee->rpmb_scan_bus_work, optee_bus_scan_rpmb);
  1647. optee->rpmb_intf.notifier_call = optee_rpmb_intf_rdev;
  1648. blocking_notifier_chain_register(&optee_rpmb_intf_added,
  1649. &optee->rpmb_intf);
  1650. pr_info("initialized driver\n");
  1651. return 0;
  1652. err_disable_shm_cache:
  1653. if (!optee->rpc_param_count)
  1654. optee_disable_shm_cache(optee);
  1655. optee_smc_notif_uninit_irq(optee);
  1656. optee_unregister_devices();
  1657. err_notif_uninit:
  1658. optee_notif_uninit(optee);
  1659. err_close_ctx:
  1660. teedev_close_context(ctx);
  1661. err_supp_uninit:
  1662. rpmb_dev_put(optee->rpmb_dev);
  1663. mutex_destroy(&optee->rpmb_dev_mutex);
  1664. optee_shm_arg_cache_uninit(optee);
  1665. optee_supp_uninit(&optee->supp);
  1666. mutex_destroy(&optee->call_queue.mutex);
  1667. err_unreg_supp_teedev:
  1668. tee_device_unregister(optee->supp_teedev);
  1669. err_unreg_teedev:
  1670. tee_device_unregister(optee->teedev);
  1671. err_free_optee:
  1672. kfree(optee);
  1673. err_free_shm_pool:
  1674. tee_shm_pool_free(pool);
  1675. if (memremaped_shm)
  1676. memunmap(memremaped_shm);
  1677. return rc;
  1678. }
  1679. static const struct of_device_id optee_dt_match[] = {
  1680. { .compatible = "linaro,optee-tz" },
  1681. {},
  1682. };
  1683. MODULE_DEVICE_TABLE(of, optee_dt_match);
  1684. static struct platform_driver optee_driver = {
  1685. .probe = optee_probe,
  1686. .remove = optee_smc_remove,
  1687. .shutdown = optee_shutdown,
  1688. .driver = {
  1689. .name = "optee",
  1690. .of_match_table = optee_dt_match,
  1691. },
  1692. };
  1693. int optee_smc_abi_register(void)
  1694. {
  1695. return platform_driver_register(&optee_driver);
  1696. }
  1697. void optee_smc_abi_unregister(void)
  1698. {
  1699. platform_driver_unregister(&optee_driver);
  1700. }