ffa.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * FF-A v1.0 proxy to filter out invalid memory-sharing SMC calls issued by
  4. * the host. FF-A is a slightly more palatable abbreviation of "Arm Firmware
  5. * Framework for Arm A-profile", which is specified by Arm in document
  6. * number DEN0077.
  7. *
  8. * Copyright (C) 2022 - Google LLC
  9. * Author: Andrew Walbran <[email protected]>
  10. *
  11. * This driver hooks into the SMC trapping logic for the host and intercepts
  12. * all calls falling within the FF-A range. Each call is either:
  13. *
  14. * - Forwarded on unmodified to the SPMD at EL3
  15. * - Rejected as "unsupported"
  16. * - Accompanied by a host stage-2 page-table check/update and reissued
  17. *
  18. * Consequently, any attempts by the host to make guest memory pages
  19. * accessible to the secure world using FF-A will be detected either here
  20. * (in the case that the memory is already owned by the guest) or during
  21. * donation to the guest (in the case that the memory was previously shared
  22. * with the secure world).
  23. *
  24. * To allow the rolling-back of page-table updates and FF-A calls in the
  25. * event of failure, operations involving the RXTX buffers are locked for
  26. * the duration and are therefore serialised.
  27. */
  28. #include <linux/arm_ffa.h>
  29. #include <asm/kvm_pkvm.h>
  30. #include <nvhe/arm-smccc.h>
  31. #include <nvhe/ffa.h>
  32. #include <nvhe/mem_protect.h>
  33. #include <nvhe/memory.h>
  34. #include <nvhe/trap_handler.h>
  35. #include <nvhe/spinlock.h>
  36. /*
  37. * "ID value 0 must be returned at the Non-secure physical FF-A instance"
  38. * We share this ID with the host.
  39. */
  40. #define HOST_FFA_ID 0
  41. /*
  42. * A buffer to hold the maximum descriptor size we can see from the host,
  43. * which is required when the SPMD returns a fragmented FFA_MEM_RETRIEVE_RESP
  44. * when resolving the handle on the reclaim path.
  45. */
  46. struct kvm_ffa_descriptor_buffer {
  47. void *buf;
  48. size_t len;
  49. };
  50. static struct kvm_ffa_descriptor_buffer ffa_desc_buf;
  51. struct kvm_ffa_buffers {
  52. hyp_spinlock_t lock;
  53. void *tx;
  54. void *rx;
  55. };
  56. /*
  57. * Note that we don't currently lock these buffers explicitly, instead
  58. * relying on the locking of the host FFA buffers as we only have one
  59. * client.
  60. */
  61. static struct kvm_ffa_buffers hyp_buffers;
  62. static struct kvm_ffa_buffers host_buffers;
  63. static void ffa_to_smccc_error(struct arm_smccc_res *res, u64 ffa_errno)
  64. {
  65. *res = (struct arm_smccc_res) {
  66. .a0 = FFA_ERROR,
  67. .a2 = ffa_errno,
  68. };
  69. }
  70. static void ffa_to_smccc_res_prop(struct arm_smccc_res *res, int ret, u64 prop)
  71. {
  72. if (ret == FFA_RET_SUCCESS) {
  73. *res = (struct arm_smccc_res) { .a0 = FFA_SUCCESS,
  74. .a2 = prop };
  75. } else {
  76. ffa_to_smccc_error(res, ret);
  77. }
  78. }
  79. static void ffa_to_smccc_res(struct arm_smccc_res *res, int ret)
  80. {
  81. ffa_to_smccc_res_prop(res, ret, 0);
  82. }
  83. static void ffa_set_retval(struct kvm_cpu_context *ctxt,
  84. struct arm_smccc_res *res)
  85. {
  86. cpu_reg(ctxt, 0) = res->a0;
  87. cpu_reg(ctxt, 1) = res->a1;
  88. cpu_reg(ctxt, 2) = res->a2;
  89. cpu_reg(ctxt, 3) = res->a3;
  90. }
  91. static bool is_ffa_call(u64 func_id)
  92. {
  93. return ARM_SMCCC_IS_FAST_CALL(func_id) &&
  94. ARM_SMCCC_OWNER_NUM(func_id) == ARM_SMCCC_OWNER_STANDARD &&
  95. ARM_SMCCC_FUNC_NUM(func_id) >= FFA_MIN_FUNC_NUM &&
  96. ARM_SMCCC_FUNC_NUM(func_id) <= FFA_MAX_FUNC_NUM;
  97. }
  98. static int spmd_map_ffa_buffers(u64 ffa_page_count)
  99. {
  100. struct arm_smccc_res res;
  101. arm_smccc_1_1_smc(FFA_FN64_RXTX_MAP,
  102. hyp_virt_to_phys(hyp_buffers.tx),
  103. hyp_virt_to_phys(hyp_buffers.rx),
  104. ffa_page_count,
  105. 0, 0, 0, 0,
  106. &res);
  107. return res.a0 == FFA_SUCCESS ? FFA_RET_SUCCESS : res.a2;
  108. }
  109. static int spmd_unmap_ffa_buffers(void)
  110. {
  111. struct arm_smccc_res res;
  112. arm_smccc_1_1_smc(FFA_RXTX_UNMAP,
  113. HOST_FFA_ID,
  114. 0, 0, 0, 0, 0, 0,
  115. &res);
  116. return res.a0 == FFA_SUCCESS ? FFA_RET_SUCCESS : res.a2;
  117. }
  118. static void spmd_mem_frag_tx(struct arm_smccc_res *res, u32 handle_lo,
  119. u32 handle_hi, u32 fraglen, u32 endpoint_id)
  120. {
  121. arm_smccc_1_1_smc(FFA_MEM_FRAG_TX,
  122. handle_lo, handle_hi, fraglen, endpoint_id,
  123. 0, 0, 0,
  124. res);
  125. }
  126. static void spmd_mem_frag_rx(struct arm_smccc_res *res, u32 handle_lo,
  127. u32 handle_hi, u32 fragoff)
  128. {
  129. arm_smccc_1_1_smc(FFA_MEM_FRAG_RX,
  130. handle_lo, handle_hi, fragoff, HOST_FFA_ID,
  131. 0, 0, 0,
  132. res);
  133. }
  134. static void spmd_mem_xfer(struct arm_smccc_res *res, u64 func_id, u32 len,
  135. u32 fraglen)
  136. {
  137. arm_smccc_1_1_smc(func_id, len, fraglen,
  138. 0, 0, 0, 0, 0,
  139. res);
  140. }
  141. static void spmd_mem_reclaim(struct arm_smccc_res *res, u32 handle_lo,
  142. u32 handle_hi, u32 flags)
  143. {
  144. arm_smccc_1_1_smc(FFA_MEM_RECLAIM,
  145. handle_lo, handle_hi, flags,
  146. 0, 0, 0, 0,
  147. res);
  148. }
  149. static void spmd_retrieve_req(struct arm_smccc_res *res, u32 len)
  150. {
  151. arm_smccc_1_1_smc(FFA_FN64_MEM_RETRIEVE_REQ,
  152. len, len,
  153. 0, 0, 0, 0, 0,
  154. res);
  155. }
  156. static void do_ffa_rxtx_map(struct arm_smccc_res *res,
  157. struct kvm_cpu_context *ctxt)
  158. {
  159. DECLARE_REG(phys_addr_t, tx, ctxt, 1);
  160. DECLARE_REG(phys_addr_t, rx, ctxt, 2);
  161. DECLARE_REG(u32, npages, ctxt, 3);
  162. int ret = 0;
  163. void *rx_virt, *tx_virt;
  164. if (npages != (KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE) / FFA_PAGE_SIZE) {
  165. ret = FFA_RET_INVALID_PARAMETERS;
  166. goto out;
  167. }
  168. if (!PAGE_ALIGNED(tx) || !PAGE_ALIGNED(rx)) {
  169. ret = FFA_RET_INVALID_PARAMETERS;
  170. goto out;
  171. }
  172. hyp_spin_lock(&host_buffers.lock);
  173. if (host_buffers.tx) {
  174. ret = FFA_RET_DENIED;
  175. goto out_unlock;
  176. }
  177. ret = spmd_map_ffa_buffers(npages);
  178. if (ret)
  179. goto out_unlock;
  180. ret = __pkvm_host_share_hyp(hyp_phys_to_pfn(tx));
  181. if (ret) {
  182. ret = FFA_RET_INVALID_PARAMETERS;
  183. goto err_unmap;
  184. }
  185. ret = __pkvm_host_share_hyp(hyp_phys_to_pfn(rx));
  186. if (ret) {
  187. ret = FFA_RET_INVALID_PARAMETERS;
  188. goto err_unshare_tx;
  189. }
  190. tx_virt = hyp_phys_to_virt(tx);
  191. ret = hyp_pin_shared_mem(tx_virt, tx_virt + 1);
  192. if (ret) {
  193. ret = FFA_RET_INVALID_PARAMETERS;
  194. goto err_unshare_rx;
  195. }
  196. rx_virt = hyp_phys_to_virt(rx);
  197. ret = hyp_pin_shared_mem(rx_virt, rx_virt + 1);
  198. if (ret) {
  199. ret = FFA_RET_INVALID_PARAMETERS;
  200. goto err_unpin_tx;
  201. }
  202. host_buffers.tx = tx_virt;
  203. host_buffers.rx = rx_virt;
  204. out_unlock:
  205. hyp_spin_unlock(&host_buffers.lock);
  206. out:
  207. ffa_to_smccc_res(res, ret);
  208. return;
  209. err_unpin_tx:
  210. hyp_unpin_shared_mem(tx_virt, tx_virt + 1);
  211. err_unshare_rx:
  212. __pkvm_host_unshare_hyp(hyp_phys_to_pfn(rx));
  213. err_unshare_tx:
  214. __pkvm_host_unshare_hyp(hyp_phys_to_pfn(tx));
  215. err_unmap:
  216. spmd_unmap_ffa_buffers();
  217. goto out_unlock;
  218. }
  219. static void do_ffa_rxtx_unmap(struct arm_smccc_res *res,
  220. struct kvm_cpu_context *ctxt)
  221. {
  222. DECLARE_REG(u32, id, ctxt, 1);
  223. int ret = 0;
  224. if (id != HOST_FFA_ID) {
  225. ret = FFA_RET_INVALID_PARAMETERS;
  226. goto out;
  227. }
  228. hyp_spin_lock(&host_buffers.lock);
  229. if (!host_buffers.tx) {
  230. ret = FFA_RET_INVALID_PARAMETERS;
  231. goto out_unlock;
  232. }
  233. hyp_unpin_shared_mem(host_buffers.tx, host_buffers.tx + 1);
  234. WARN_ON(__pkvm_host_unshare_hyp(hyp_virt_to_pfn(host_buffers.tx)));
  235. host_buffers.tx = NULL;
  236. hyp_unpin_shared_mem(host_buffers.rx, host_buffers.rx + 1);
  237. WARN_ON(__pkvm_host_unshare_hyp(hyp_virt_to_pfn(host_buffers.rx)));
  238. host_buffers.rx = NULL;
  239. spmd_unmap_ffa_buffers();
  240. out_unlock:
  241. hyp_spin_unlock(&host_buffers.lock);
  242. out:
  243. ffa_to_smccc_res(res, ret);
  244. }
  245. static u32 __ffa_host_share_ranges(struct ffa_mem_region_addr_range *ranges,
  246. u32 nranges)
  247. {
  248. u32 i;
  249. for (i = 0; i < nranges; ++i) {
  250. struct ffa_mem_region_addr_range *range = &ranges[i];
  251. u64 sz = (u64)range->pg_cnt * FFA_PAGE_SIZE;
  252. u64 pfn = hyp_phys_to_pfn(range->address);
  253. if (!PAGE_ALIGNED(sz))
  254. break;
  255. if (__pkvm_host_share_ffa(pfn, sz / PAGE_SIZE))
  256. break;
  257. }
  258. return i;
  259. }
  260. static u32 __ffa_host_unshare_ranges(struct ffa_mem_region_addr_range *ranges,
  261. u32 nranges)
  262. {
  263. u32 i;
  264. for (i = 0; i < nranges; ++i) {
  265. struct ffa_mem_region_addr_range *range = &ranges[i];
  266. u64 sz = (u64)range->pg_cnt * FFA_PAGE_SIZE;
  267. u64 pfn = hyp_phys_to_pfn(range->address);
  268. if (!PAGE_ALIGNED(sz))
  269. break;
  270. if (__pkvm_host_unshare_ffa(pfn, sz / PAGE_SIZE))
  271. break;
  272. }
  273. return i;
  274. }
  275. static int ffa_host_share_ranges(struct ffa_mem_region_addr_range *ranges,
  276. u32 nranges)
  277. {
  278. u32 nshared = __ffa_host_share_ranges(ranges, nranges);
  279. int ret = 0;
  280. if (nshared != nranges) {
  281. WARN_ON(__ffa_host_unshare_ranges(ranges, nshared) != nshared);
  282. ret = FFA_RET_DENIED;
  283. }
  284. return ret;
  285. }
  286. static int ffa_host_unshare_ranges(struct ffa_mem_region_addr_range *ranges,
  287. u32 nranges)
  288. {
  289. u32 nunshared = __ffa_host_unshare_ranges(ranges, nranges);
  290. int ret = 0;
  291. if (nunshared != nranges) {
  292. WARN_ON(__ffa_host_share_ranges(ranges, nunshared) != nunshared);
  293. ret = FFA_RET_DENIED;
  294. }
  295. return ret;
  296. }
  297. static void do_ffa_mem_frag_tx(struct arm_smccc_res *res,
  298. struct kvm_cpu_context *ctxt)
  299. {
  300. DECLARE_REG(u32, handle_lo, ctxt, 1);
  301. DECLARE_REG(u32, handle_hi, ctxt, 2);
  302. DECLARE_REG(u32, fraglen, ctxt, 3);
  303. DECLARE_REG(u32, endpoint_id, ctxt, 4);
  304. struct ffa_mem_region_addr_range *buf;
  305. int ret = FFA_RET_INVALID_PARAMETERS;
  306. u32 nr_ranges;
  307. if (fraglen > KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE)
  308. goto out;
  309. if (fraglen % sizeof(*buf))
  310. goto out;
  311. hyp_spin_lock(&host_buffers.lock);
  312. if (!host_buffers.tx)
  313. goto out_unlock;
  314. buf = hyp_buffers.tx;
  315. memcpy(buf, host_buffers.tx, fraglen);
  316. nr_ranges = fraglen / sizeof(*buf);
  317. ret = ffa_host_share_ranges(buf, nr_ranges);
  318. if (ret) {
  319. /*
  320. * We're effectively aborting the transaction, so we need
  321. * to restore the global state back to what it was prior to
  322. * transmission of the first fragment.
  323. */
  324. spmd_mem_reclaim(res, handle_lo, handle_hi, 0);
  325. WARN_ON(res->a0 != FFA_SUCCESS);
  326. goto out_unlock;
  327. }
  328. spmd_mem_frag_tx(res, handle_lo, handle_hi, fraglen, endpoint_id);
  329. if (res->a0 != FFA_SUCCESS && res->a0 != FFA_MEM_FRAG_RX)
  330. WARN_ON(ffa_host_unshare_ranges(buf, nr_ranges));
  331. out_unlock:
  332. hyp_spin_unlock(&host_buffers.lock);
  333. out:
  334. if (ret)
  335. ffa_to_smccc_res(res, ret);
  336. /*
  337. * If for any reason this did not succeed, we're in trouble as we have
  338. * now lost the content of the previous fragments and we can't rollback
  339. * the host stage-2 changes. The pages previously marked as shared will
  340. * remain stuck in that state forever, hence preventing the host from
  341. * sharing/donating them again and may possibly lead to subsequent
  342. * failures, but this will not compromise confidentiality.
  343. */
  344. return;
  345. }
  346. static __always_inline void do_ffa_mem_xfer(const u64 func_id,
  347. struct arm_smccc_res *res,
  348. struct kvm_cpu_context *ctxt)
  349. {
  350. DECLARE_REG(u32, len, ctxt, 1);
  351. DECLARE_REG(u32, fraglen, ctxt, 2);
  352. DECLARE_REG(u64, addr_mbz, ctxt, 3);
  353. DECLARE_REG(u32, npages_mbz, ctxt, 4);
  354. struct ffa_composite_mem_region *reg;
  355. struct ffa_mem_region *buf;
  356. u32 offset, nr_ranges;
  357. int ret = 0;
  358. BUILD_BUG_ON(func_id != FFA_FN64_MEM_SHARE &&
  359. func_id != FFA_FN64_MEM_LEND);
  360. if (addr_mbz || npages_mbz || fraglen > len ||
  361. fraglen > KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE) {
  362. ret = FFA_RET_INVALID_PARAMETERS;
  363. goto out;
  364. }
  365. if (fraglen < sizeof(struct ffa_mem_region) +
  366. sizeof(struct ffa_mem_region_attributes)) {
  367. ret = FFA_RET_INVALID_PARAMETERS;
  368. goto out;
  369. }
  370. hyp_spin_lock(&host_buffers.lock);
  371. if (!host_buffers.tx) {
  372. ret = FFA_RET_INVALID_PARAMETERS;
  373. goto out_unlock;
  374. }
  375. buf = hyp_buffers.tx;
  376. memcpy(buf, host_buffers.tx, fraglen);
  377. offset = buf->ep_mem_access[0].composite_off;
  378. if (!offset || buf->ep_count != 1 || buf->sender_id != HOST_FFA_ID) {
  379. ret = FFA_RET_INVALID_PARAMETERS;
  380. goto out_unlock;
  381. }
  382. if (fraglen < offset + sizeof(struct ffa_composite_mem_region)) {
  383. ret = FFA_RET_INVALID_PARAMETERS;
  384. goto out_unlock;
  385. }
  386. reg = (void *)buf + offset;
  387. nr_ranges = ((void *)buf + fraglen) - (void *)reg->constituents;
  388. if (nr_ranges % sizeof(reg->constituents[0])) {
  389. ret = FFA_RET_INVALID_PARAMETERS;
  390. goto out_unlock;
  391. }
  392. nr_ranges /= sizeof(reg->constituents[0]);
  393. ret = ffa_host_share_ranges(reg->constituents, nr_ranges);
  394. if (ret)
  395. goto out_unlock;
  396. spmd_mem_xfer(res, func_id, len, fraglen);
  397. if (fraglen != len) {
  398. if (res->a0 != FFA_MEM_FRAG_RX)
  399. goto err_unshare;
  400. if (res->a3 != fraglen)
  401. goto err_unshare;
  402. } else if (res->a0 != FFA_SUCCESS) {
  403. goto err_unshare;
  404. }
  405. out_unlock:
  406. hyp_spin_unlock(&host_buffers.lock);
  407. out:
  408. if (ret)
  409. ffa_to_smccc_res(res, ret);
  410. return;
  411. err_unshare:
  412. WARN_ON(ffa_host_unshare_ranges(reg->constituents, nr_ranges));
  413. goto out_unlock;
  414. }
  415. static void do_ffa_mem_reclaim(struct arm_smccc_res *res,
  416. struct kvm_cpu_context *ctxt)
  417. {
  418. DECLARE_REG(u32, handle_lo, ctxt, 1);
  419. DECLARE_REG(u32, handle_hi, ctxt, 2);
  420. DECLARE_REG(u32, flags, ctxt, 3);
  421. struct ffa_composite_mem_region *reg;
  422. u32 offset, len, fraglen, fragoff;
  423. struct ffa_mem_region *buf;
  424. int ret = 0;
  425. u64 handle;
  426. handle = PACK_HANDLE(handle_lo, handle_hi);
  427. hyp_spin_lock(&host_buffers.lock);
  428. buf = hyp_buffers.tx;
  429. *buf = (struct ffa_mem_region) {
  430. .sender_id = HOST_FFA_ID,
  431. .handle = handle,
  432. };
  433. spmd_retrieve_req(res, sizeof(*buf));
  434. buf = hyp_buffers.rx;
  435. if (res->a0 != FFA_MEM_RETRIEVE_RESP)
  436. goto out_unlock;
  437. len = res->a1;
  438. fraglen = res->a2;
  439. offset = buf->ep_mem_access[0].composite_off;
  440. /*
  441. * We can trust the SPMD to get this right, but let's at least
  442. * check that we end up with something that doesn't look _completely_
  443. * bogus.
  444. */
  445. if (WARN_ON(offset > len ||
  446. fraglen > KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE)) {
  447. ret = FFA_RET_ABORTED;
  448. goto out_unlock;
  449. }
  450. if (len > ffa_desc_buf.len) {
  451. ret = FFA_RET_NO_MEMORY;
  452. goto out_unlock;
  453. }
  454. buf = ffa_desc_buf.buf;
  455. memcpy(buf, hyp_buffers.rx, fraglen);
  456. for (fragoff = fraglen; fragoff < len; fragoff += fraglen) {
  457. spmd_mem_frag_rx(res, handle_lo, handle_hi, fragoff);
  458. if (res->a0 != FFA_MEM_FRAG_TX) {
  459. ret = FFA_RET_INVALID_PARAMETERS;
  460. goto out_unlock;
  461. }
  462. fraglen = res->a3;
  463. memcpy((void *)buf + fragoff, hyp_buffers.rx, fraglen);
  464. }
  465. spmd_mem_reclaim(res, handle_lo, handle_hi, flags);
  466. if (res->a0 != FFA_SUCCESS)
  467. goto out_unlock;
  468. reg = (void *)buf + offset;
  469. /* If the SPMD was happy, then we should be too. */
  470. WARN_ON(ffa_host_unshare_ranges(reg->constituents,
  471. reg->addr_range_cnt));
  472. out_unlock:
  473. hyp_spin_unlock(&host_buffers.lock);
  474. if (ret)
  475. ffa_to_smccc_res(res, ret);
  476. }
  477. static bool ffa_call_unsupported(u64 func_id)
  478. {
  479. switch (func_id) {
  480. /* Unsupported memory management calls */
  481. case FFA_FN64_MEM_RETRIEVE_REQ:
  482. case FFA_MEM_RETRIEVE_RESP:
  483. case FFA_MEM_RELINQUISH:
  484. case FFA_MEM_OP_PAUSE:
  485. case FFA_MEM_OP_RESUME:
  486. case FFA_MEM_FRAG_RX:
  487. case FFA_FN64_MEM_DONATE:
  488. /* Indirect message passing via RX/TX buffers */
  489. case FFA_MSG_SEND:
  490. case FFA_MSG_POLL:
  491. case FFA_MSG_WAIT:
  492. /* 32-bit variants of 64-bit calls */
  493. case FFA_MSG_SEND_DIRECT_REQ:
  494. case FFA_MSG_SEND_DIRECT_RESP:
  495. case FFA_RXTX_MAP:
  496. case FFA_MEM_DONATE:
  497. case FFA_MEM_RETRIEVE_REQ:
  498. return true;
  499. }
  500. return false;
  501. }
  502. static bool do_ffa_features(struct arm_smccc_res *res,
  503. struct kvm_cpu_context *ctxt)
  504. {
  505. DECLARE_REG(u32, id, ctxt, 1);
  506. u64 prop = 0;
  507. int ret = 0;
  508. if (ffa_call_unsupported(id)) {
  509. ret = FFA_RET_NOT_SUPPORTED;
  510. goto out_handled;
  511. }
  512. switch (id) {
  513. case FFA_MEM_SHARE:
  514. case FFA_FN64_MEM_SHARE:
  515. case FFA_MEM_LEND:
  516. case FFA_FN64_MEM_LEND:
  517. ret = FFA_RET_SUCCESS;
  518. prop = 0; /* No support for dynamic buffers */
  519. goto out_handled;
  520. default:
  521. return false;
  522. }
  523. out_handled:
  524. ffa_to_smccc_res_prop(res, ret, prop);
  525. return true;
  526. }
  527. bool kvm_host_ffa_handler(struct kvm_cpu_context *host_ctxt)
  528. {
  529. DECLARE_REG(u64, func_id, host_ctxt, 0);
  530. struct arm_smccc_res res;
  531. if (!is_ffa_call(func_id))
  532. return false;
  533. switch (func_id) {
  534. case FFA_FEATURES:
  535. if (!do_ffa_features(&res, host_ctxt))
  536. return false;
  537. goto out_handled;
  538. /* Memory management */
  539. case FFA_FN64_RXTX_MAP:
  540. do_ffa_rxtx_map(&res, host_ctxt);
  541. goto out_handled;
  542. case FFA_RXTX_UNMAP:
  543. do_ffa_rxtx_unmap(&res, host_ctxt);
  544. goto out_handled;
  545. case FFA_MEM_SHARE:
  546. case FFA_FN64_MEM_SHARE:
  547. do_ffa_mem_xfer(FFA_FN64_MEM_SHARE, &res, host_ctxt);
  548. goto out_handled;
  549. case FFA_MEM_RECLAIM:
  550. do_ffa_mem_reclaim(&res, host_ctxt);
  551. goto out_handled;
  552. case FFA_MEM_LEND:
  553. case FFA_FN64_MEM_LEND:
  554. do_ffa_mem_xfer(FFA_FN64_MEM_LEND, &res, host_ctxt);
  555. goto out_handled;
  556. case FFA_MEM_FRAG_TX:
  557. do_ffa_mem_frag_tx(&res, host_ctxt);
  558. goto out_handled;
  559. }
  560. if (!ffa_call_unsupported(func_id))
  561. return false; /* Pass through */
  562. ffa_to_smccc_error(&res, FFA_RET_NOT_SUPPORTED);
  563. out_handled:
  564. ffa_set_retval(host_ctxt, &res);
  565. return true;
  566. }
  567. int hyp_ffa_init(void *pages)
  568. {
  569. struct arm_smccc_res res;
  570. size_t min_rxtx_sz;
  571. void *tx, *rx;
  572. if (kvm_host_psci_config.smccc_version < ARM_SMCCC_VERSION_1_1)
  573. return 0;
  574. arm_smccc_1_1_smc(FFA_VERSION, FFA_VERSION_1_0, 0, 0, 0, 0, 0, 0, &res);
  575. if (res.a0 == FFA_RET_NOT_SUPPORTED)
  576. return 0;
  577. if (res.a0 != FFA_VERSION_1_0)
  578. return -EOPNOTSUPP;
  579. arm_smccc_1_1_smc(FFA_ID_GET, 0, 0, 0, 0, 0, 0, 0, &res);
  580. if (res.a0 != FFA_SUCCESS)
  581. return -EOPNOTSUPP;
  582. if (res.a2 != HOST_FFA_ID)
  583. return -EINVAL;
  584. arm_smccc_1_1_smc(FFA_FEATURES, FFA_FN64_RXTX_MAP,
  585. 0, 0, 0, 0, 0, 0, &res);
  586. if (res.a0 != FFA_SUCCESS)
  587. return -EOPNOTSUPP;
  588. switch (res.a2) {
  589. case FFA_FEAT_RXTX_MIN_SZ_4K:
  590. min_rxtx_sz = SZ_4K;
  591. break;
  592. case FFA_FEAT_RXTX_MIN_SZ_16K:
  593. min_rxtx_sz = SZ_16K;
  594. break;
  595. case FFA_FEAT_RXTX_MIN_SZ_64K:
  596. min_rxtx_sz = SZ_64K;
  597. break;
  598. default:
  599. return -EINVAL;
  600. }
  601. if (min_rxtx_sz > PAGE_SIZE)
  602. return -EOPNOTSUPP;
  603. tx = pages;
  604. pages += KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE;
  605. rx = pages;
  606. pages += KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE;
  607. ffa_desc_buf = (struct kvm_ffa_descriptor_buffer) {
  608. .buf = pages,
  609. .len = PAGE_SIZE *
  610. (hyp_ffa_proxy_pages() - (2 * KVM_FFA_MBOX_NR_PAGES)),
  611. };
  612. hyp_buffers = (struct kvm_ffa_buffers) {
  613. .lock = __HYP_SPIN_LOCK_UNLOCKED,
  614. .tx = tx,
  615. .rx = rx,
  616. };
  617. host_buffers = (struct kvm_ffa_buffers) {
  618. .lock = __HYP_SPIN_LOCK_UNLOCKED,
  619. };
  620. return 0;
  621. }