driver.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Arm Firmware Framework for ARMv8-A(FFA) interface driver
  4. *
  5. * The Arm FFA specification[1] describes a software architecture to
  6. * leverages the virtualization extension to isolate software images
  7. * provided by an ecosystem of vendors from each other and describes
  8. * interfaces that standardize communication between the various software
  9. * images including communication between images in the Secure world and
  10. * Normal world. Any Hypervisor could use the FFA interfaces to enable
  11. * communication between VMs it manages.
  12. *
  13. * The Hypervisor a.k.a Partition managers in FFA terminology can assign
  14. * system resources(Memory regions, Devices, CPU cycles) to the partitions
  15. * and manage isolation amongst them.
  16. *
  17. * [1] https://developer.arm.com/docs/den0077/latest
  18. *
  19. * Copyright (C) 2021 ARM Ltd.
  20. */
  21. #define DRIVER_NAME "ARM FF-A"
  22. #define pr_fmt(fmt) DRIVER_NAME ": " fmt
  23. #include <linux/arm_ffa.h>
  24. #include <linux/bitfield.h>
  25. #include <linux/device.h>
  26. #include <linux/io.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/mm.h>
  30. #include <linux/scatterlist.h>
  31. #include <linux/slab.h>
  32. #include <linux/uuid.h>
  33. #include "common.h"
  34. #define FFA_DRIVER_VERSION FFA_VERSION_1_0
  35. #define FFA_MIN_VERSION FFA_VERSION_1_0
  36. #define SENDER_ID_MASK GENMASK(31, 16)
  37. #define RECEIVER_ID_MASK GENMASK(15, 0)
  38. #define SENDER_ID(x) ((u16)(FIELD_GET(SENDER_ID_MASK, (x))))
  39. #define RECEIVER_ID(x) ((u16)(FIELD_GET(RECEIVER_ID_MASK, (x))))
  40. #define PACK_TARGET_INFO(s, r) \
  41. (FIELD_PREP(SENDER_ID_MASK, (s)) | FIELD_PREP(RECEIVER_ID_MASK, (r)))
  42. /*
  43. * Keeping RX TX buffer size as 4K for now
  44. * 64K may be preferred to keep it min a page in 64K PAGE_SIZE config
  45. */
  46. #define RXTX_BUFFER_SIZE SZ_4K
  47. static ffa_fn *invoke_ffa_fn;
  48. static const int ffa_linux_errmap[] = {
  49. /* better than switch case as long as return value is continuous */
  50. 0, /* FFA_RET_SUCCESS */
  51. -EOPNOTSUPP, /* FFA_RET_NOT_SUPPORTED */
  52. -EINVAL, /* FFA_RET_INVALID_PARAMETERS */
  53. -ENOMEM, /* FFA_RET_NO_MEMORY */
  54. -EBUSY, /* FFA_RET_BUSY */
  55. -EINTR, /* FFA_RET_INTERRUPTED */
  56. -EACCES, /* FFA_RET_DENIED */
  57. -EAGAIN, /* FFA_RET_RETRY */
  58. -ECANCELED, /* FFA_RET_ABORTED */
  59. };
  60. static inline int ffa_to_linux_errno(int errno)
  61. {
  62. int err_idx = -errno;
  63. if (err_idx >= 0 && err_idx < ARRAY_SIZE(ffa_linux_errmap))
  64. return ffa_linux_errmap[err_idx];
  65. return -EINVAL;
  66. }
  67. struct ffa_drv_info {
  68. u32 version;
  69. u16 vm_id;
  70. struct mutex rx_lock; /* lock to protect Rx buffer */
  71. struct mutex tx_lock; /* lock to protect Tx buffer */
  72. void *rx_buffer;
  73. void *tx_buffer;
  74. bool mem_ops_native;
  75. };
  76. static struct ffa_drv_info *drv_info;
  77. /*
  78. * The driver must be able to support all the versions from the earliest
  79. * supported FFA_MIN_VERSION to the latest supported FFA_DRIVER_VERSION.
  80. * The specification states that if firmware supports a FFA implementation
  81. * that is incompatible with and at a greater version number than specified
  82. * by the caller(FFA_DRIVER_VERSION passed as parameter to FFA_VERSION),
  83. * it must return the NOT_SUPPORTED error code.
  84. */
  85. static u32 ffa_compatible_version_find(u32 version)
  86. {
  87. u16 major = FFA_MAJOR_VERSION(version), minor = FFA_MINOR_VERSION(version);
  88. u16 drv_major = FFA_MAJOR_VERSION(FFA_DRIVER_VERSION);
  89. u16 drv_minor = FFA_MINOR_VERSION(FFA_DRIVER_VERSION);
  90. if ((major < drv_major) || (major == drv_major && minor <= drv_minor))
  91. return version;
  92. pr_info("Firmware version higher than driver version, downgrading\n");
  93. return FFA_DRIVER_VERSION;
  94. }
  95. static int ffa_version_check(u32 *version)
  96. {
  97. ffa_value_t ver;
  98. invoke_ffa_fn((ffa_value_t){
  99. .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION,
  100. }, &ver);
  101. if (ver.a0 == FFA_RET_NOT_SUPPORTED) {
  102. pr_info("FFA_VERSION returned not supported\n");
  103. return -EOPNOTSUPP;
  104. }
  105. if (ver.a0 < FFA_MIN_VERSION) {
  106. pr_err("Incompatible v%d.%d! Earliest supported v%d.%d\n",
  107. FFA_MAJOR_VERSION(ver.a0), FFA_MINOR_VERSION(ver.a0),
  108. FFA_MAJOR_VERSION(FFA_MIN_VERSION),
  109. FFA_MINOR_VERSION(FFA_MIN_VERSION));
  110. return -EINVAL;
  111. }
  112. pr_info("Driver version %d.%d\n", FFA_MAJOR_VERSION(FFA_DRIVER_VERSION),
  113. FFA_MINOR_VERSION(FFA_DRIVER_VERSION));
  114. pr_info("Firmware version %d.%d found\n", FFA_MAJOR_VERSION(ver.a0),
  115. FFA_MINOR_VERSION(ver.a0));
  116. *version = ffa_compatible_version_find(ver.a0);
  117. return 0;
  118. }
  119. static int ffa_rx_release(void)
  120. {
  121. ffa_value_t ret;
  122. invoke_ffa_fn((ffa_value_t){
  123. .a0 = FFA_RX_RELEASE,
  124. }, &ret);
  125. if (ret.a0 == FFA_ERROR)
  126. return ffa_to_linux_errno((int)ret.a2);
  127. /* check for ret.a0 == FFA_RX_RELEASE ? */
  128. return 0;
  129. }
  130. static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt)
  131. {
  132. ffa_value_t ret;
  133. invoke_ffa_fn((ffa_value_t){
  134. .a0 = FFA_FN_NATIVE(RXTX_MAP),
  135. .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt,
  136. }, &ret);
  137. if (ret.a0 == FFA_ERROR)
  138. return ffa_to_linux_errno((int)ret.a2);
  139. return 0;
  140. }
  141. static int ffa_rxtx_unmap(u16 vm_id)
  142. {
  143. ffa_value_t ret;
  144. invoke_ffa_fn((ffa_value_t){
  145. .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0),
  146. }, &ret);
  147. if (ret.a0 == FFA_ERROR)
  148. return ffa_to_linux_errno((int)ret.a2);
  149. return 0;
  150. }
  151. #define PARTITION_INFO_GET_RETURN_COUNT_ONLY BIT(0)
  152. /* buffer must be sizeof(struct ffa_partition_info) * num_partitions */
  153. static int
  154. __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
  155. struct ffa_partition_info *buffer, int num_partitions)
  156. {
  157. int idx, count, flags = 0, sz, buf_sz;
  158. ffa_value_t partition_info;
  159. if (drv_info->version > FFA_VERSION_1_0 &&
  160. (!buffer || !num_partitions)) /* Just get the count for now */
  161. flags = PARTITION_INFO_GET_RETURN_COUNT_ONLY;
  162. mutex_lock(&drv_info->rx_lock);
  163. invoke_ffa_fn((ffa_value_t){
  164. .a0 = FFA_PARTITION_INFO_GET,
  165. .a1 = uuid0, .a2 = uuid1, .a3 = uuid2, .a4 = uuid3,
  166. .a5 = flags,
  167. }, &partition_info);
  168. if (partition_info.a0 == FFA_ERROR) {
  169. mutex_unlock(&drv_info->rx_lock);
  170. return ffa_to_linux_errno((int)partition_info.a2);
  171. }
  172. count = partition_info.a2;
  173. if (drv_info->version > FFA_VERSION_1_0) {
  174. buf_sz = sz = partition_info.a3;
  175. if (sz > sizeof(*buffer))
  176. buf_sz = sizeof(*buffer);
  177. } else {
  178. /* FFA_VERSION_1_0 lacks size in the response */
  179. buf_sz = sz = 8;
  180. }
  181. if (buffer && count <= num_partitions)
  182. for (idx = 0; idx < count; idx++)
  183. memcpy(buffer + idx, drv_info->rx_buffer + idx * sz,
  184. buf_sz);
  185. ffa_rx_release();
  186. mutex_unlock(&drv_info->rx_lock);
  187. return count;
  188. }
  189. /* buffer is allocated and caller must free the same if returned count > 0 */
  190. static int
  191. ffa_partition_probe(const uuid_t *uuid, struct ffa_partition_info **buffer)
  192. {
  193. int count;
  194. u32 uuid0_4[4];
  195. struct ffa_partition_info *pbuf;
  196. export_uuid((u8 *)uuid0_4, uuid);
  197. count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
  198. uuid0_4[3], NULL, 0);
  199. if (count <= 0)
  200. return count;
  201. pbuf = kcalloc(count, sizeof(*pbuf), GFP_KERNEL);
  202. if (!pbuf)
  203. return -ENOMEM;
  204. count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
  205. uuid0_4[3], pbuf, count);
  206. if (count <= 0)
  207. kfree(pbuf);
  208. else
  209. *buffer = pbuf;
  210. return count;
  211. }
  212. #define VM_ID_MASK GENMASK(15, 0)
  213. static int ffa_id_get(u16 *vm_id)
  214. {
  215. ffa_value_t id;
  216. invoke_ffa_fn((ffa_value_t){
  217. .a0 = FFA_ID_GET,
  218. }, &id);
  219. if (id.a0 == FFA_ERROR)
  220. return ffa_to_linux_errno((int)id.a2);
  221. *vm_id = FIELD_GET(VM_ID_MASK, (id.a2));
  222. return 0;
  223. }
  224. static int ffa_msg_send_direct_req(u16 src_id, u16 dst_id, bool mode_32bit,
  225. struct ffa_send_direct_data *data)
  226. {
  227. u32 req_id, resp_id, src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
  228. ffa_value_t ret;
  229. if (mode_32bit) {
  230. req_id = FFA_MSG_SEND_DIRECT_REQ;
  231. resp_id = FFA_MSG_SEND_DIRECT_RESP;
  232. } else {
  233. req_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_REQ);
  234. resp_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_RESP);
  235. }
  236. invoke_ffa_fn((ffa_value_t){
  237. .a0 = req_id, .a1 = src_dst_ids, .a2 = 0,
  238. .a3 = data->data0, .a4 = data->data1, .a5 = data->data2,
  239. .a6 = data->data3, .a7 = data->data4,
  240. }, &ret);
  241. while (ret.a0 == FFA_INTERRUPT)
  242. invoke_ffa_fn((ffa_value_t){
  243. .a0 = FFA_RUN, .a1 = ret.a1,
  244. }, &ret);
  245. if (ret.a0 == FFA_ERROR)
  246. return ffa_to_linux_errno((int)ret.a2);
  247. if (ret.a0 == resp_id) {
  248. data->data0 = ret.a3;
  249. data->data1 = ret.a4;
  250. data->data2 = ret.a5;
  251. data->data3 = ret.a6;
  252. data->data4 = ret.a7;
  253. return 0;
  254. }
  255. return -EINVAL;
  256. }
  257. static int ffa_mem_first_frag(u32 func_id, phys_addr_t buf, u32 buf_sz,
  258. u32 frag_len, u32 len, u64 *handle)
  259. {
  260. ffa_value_t ret;
  261. invoke_ffa_fn((ffa_value_t){
  262. .a0 = func_id, .a1 = len, .a2 = frag_len,
  263. .a3 = buf, .a4 = buf_sz,
  264. }, &ret);
  265. while (ret.a0 == FFA_MEM_OP_PAUSE)
  266. invoke_ffa_fn((ffa_value_t){
  267. .a0 = FFA_MEM_OP_RESUME,
  268. .a1 = ret.a1, .a2 = ret.a2,
  269. }, &ret);
  270. if (ret.a0 == FFA_ERROR)
  271. return ffa_to_linux_errno((int)ret.a2);
  272. if (ret.a0 == FFA_SUCCESS) {
  273. if (handle)
  274. *handle = PACK_HANDLE(ret.a2, ret.a3);
  275. } else if (ret.a0 == FFA_MEM_FRAG_RX) {
  276. if (handle)
  277. *handle = PACK_HANDLE(ret.a1, ret.a2);
  278. } else {
  279. return -EOPNOTSUPP;
  280. }
  281. return frag_len;
  282. }
  283. static int ffa_mem_next_frag(u64 handle, u32 frag_len)
  284. {
  285. ffa_value_t ret;
  286. invoke_ffa_fn((ffa_value_t){
  287. .a0 = FFA_MEM_FRAG_TX,
  288. .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle),
  289. .a3 = frag_len,
  290. }, &ret);
  291. while (ret.a0 == FFA_MEM_OP_PAUSE)
  292. invoke_ffa_fn((ffa_value_t){
  293. .a0 = FFA_MEM_OP_RESUME,
  294. .a1 = ret.a1, .a2 = ret.a2,
  295. }, &ret);
  296. if (ret.a0 == FFA_ERROR)
  297. return ffa_to_linux_errno((int)ret.a2);
  298. if (ret.a0 == FFA_MEM_FRAG_RX)
  299. return ret.a3;
  300. else if (ret.a0 == FFA_SUCCESS)
  301. return 0;
  302. return -EOPNOTSUPP;
  303. }
  304. static int
  305. ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len,
  306. u32 len, u64 *handle, bool first)
  307. {
  308. if (!first)
  309. return ffa_mem_next_frag(*handle, frag_len);
  310. return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle);
  311. }
  312. static u32 ffa_get_num_pages_sg(struct scatterlist *sg)
  313. {
  314. u32 num_pages = 0;
  315. do {
  316. num_pages += sg->length / FFA_PAGE_SIZE;
  317. } while ((sg = sg_next(sg)));
  318. return num_pages;
  319. }
  320. static u8 ffa_memory_attributes_get(u32 func_id)
  321. {
  322. /*
  323. * For the memory lend or donate operation, if the receiver is a PE or
  324. * a proxy endpoint, the owner/sender must not specify the attributes
  325. */
  326. if (func_id == FFA_FN_NATIVE(MEM_LEND) ||
  327. func_id == FFA_MEM_LEND)
  328. return 0;
  329. return FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK | FFA_MEM_INNER_SHAREABLE;
  330. }
  331. static int
  332. ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
  333. struct ffa_mem_ops_args *args)
  334. {
  335. int rc = 0;
  336. bool first = true;
  337. phys_addr_t addr = 0;
  338. struct ffa_composite_mem_region *composite;
  339. struct ffa_mem_region_addr_range *constituents;
  340. struct ffa_mem_region_attributes *ep_mem_access;
  341. struct ffa_mem_region *mem_region = buffer;
  342. u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg);
  343. mem_region->tag = args->tag;
  344. mem_region->flags = args->flags;
  345. mem_region->sender_id = drv_info->vm_id;
  346. mem_region->attributes = ffa_memory_attributes_get(func_id);
  347. ep_mem_access = &mem_region->ep_mem_access[0];
  348. for (idx = 0; idx < args->nattrs; idx++, ep_mem_access++) {
  349. ep_mem_access->receiver = args->attrs[idx].receiver;
  350. ep_mem_access->attrs = args->attrs[idx].attrs;
  351. ep_mem_access->composite_off = COMPOSITE_OFFSET(args->nattrs);
  352. ep_mem_access->flag = 0;
  353. ep_mem_access->reserved = 0;
  354. }
  355. mem_region->handle = 0;
  356. mem_region->reserved_0 = 0;
  357. mem_region->reserved_1 = 0;
  358. mem_region->ep_count = args->nattrs;
  359. composite = buffer + COMPOSITE_OFFSET(args->nattrs);
  360. composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg);
  361. composite->addr_range_cnt = num_entries;
  362. composite->reserved = 0;
  363. length = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, num_entries);
  364. frag_len = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, 0);
  365. if (frag_len > max_fragsize)
  366. return -ENXIO;
  367. if (!args->use_txbuf) {
  368. addr = virt_to_phys(buffer);
  369. buf_sz = max_fragsize / FFA_PAGE_SIZE;
  370. }
  371. constituents = buffer + frag_len;
  372. idx = 0;
  373. do {
  374. if (frag_len == max_fragsize) {
  375. rc = ffa_transmit_fragment(func_id, addr, buf_sz,
  376. frag_len, length,
  377. &args->g_handle, first);
  378. if (rc < 0)
  379. return -ENXIO;
  380. first = false;
  381. idx = 0;
  382. frag_len = 0;
  383. constituents = buffer;
  384. }
  385. if ((void *)constituents - buffer > max_fragsize) {
  386. pr_err("Memory Region Fragment > Tx Buffer size\n");
  387. return -EFAULT;
  388. }
  389. constituents->address = sg_phys(args->sg);
  390. constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE;
  391. constituents->reserved = 0;
  392. constituents++;
  393. frag_len += sizeof(struct ffa_mem_region_addr_range);
  394. } while ((args->sg = sg_next(args->sg)));
  395. return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len,
  396. length, &args->g_handle, first);
  397. }
  398. static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args)
  399. {
  400. int ret;
  401. void *buffer;
  402. if (!args->use_txbuf) {
  403. buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
  404. if (!buffer)
  405. return -ENOMEM;
  406. } else {
  407. buffer = drv_info->tx_buffer;
  408. mutex_lock(&drv_info->tx_lock);
  409. }
  410. ret = ffa_setup_and_transmit(func_id, buffer, RXTX_BUFFER_SIZE, args);
  411. if (args->use_txbuf)
  412. mutex_unlock(&drv_info->tx_lock);
  413. else
  414. free_pages_exact(buffer, RXTX_BUFFER_SIZE);
  415. return ret < 0 ? ret : 0;
  416. }
  417. static int ffa_memory_reclaim(u64 g_handle, u32 flags)
  418. {
  419. ffa_value_t ret;
  420. invoke_ffa_fn((ffa_value_t){
  421. .a0 = FFA_MEM_RECLAIM,
  422. .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle),
  423. .a3 = flags,
  424. }, &ret);
  425. if (ret.a0 == FFA_ERROR)
  426. return ffa_to_linux_errno((int)ret.a2);
  427. return 0;
  428. }
  429. static int ffa_features(u32 func_feat_id, u32 input_props,
  430. u32 *if_props_1, u32 *if_props_2)
  431. {
  432. ffa_value_t id;
  433. if (!ARM_SMCCC_IS_FAST_CALL(func_feat_id) && input_props) {
  434. pr_err("%s: Invalid Parameters: %x, %x", __func__,
  435. func_feat_id, input_props);
  436. return ffa_to_linux_errno(FFA_RET_INVALID_PARAMETERS);
  437. }
  438. invoke_ffa_fn((ffa_value_t){
  439. .a0 = FFA_FEATURES, .a1 = func_feat_id, .a2 = input_props,
  440. }, &id);
  441. if (id.a0 == FFA_ERROR)
  442. return ffa_to_linux_errno((int)id.a2);
  443. if (if_props_1)
  444. *if_props_1 = id.a2;
  445. if (if_props_2)
  446. *if_props_2 = id.a3;
  447. return 0;
  448. }
  449. static void ffa_set_up_mem_ops_native_flag(void)
  450. {
  451. if (!ffa_features(FFA_FN_NATIVE(MEM_LEND), 0, NULL, NULL) ||
  452. !ffa_features(FFA_FN_NATIVE(MEM_SHARE), 0, NULL, NULL))
  453. drv_info->mem_ops_native = true;
  454. }
  455. static u32 ffa_api_version_get(void)
  456. {
  457. return drv_info->version;
  458. }
  459. static int ffa_partition_info_get(const char *uuid_str,
  460. struct ffa_partition_info *buffer)
  461. {
  462. int count;
  463. uuid_t uuid;
  464. struct ffa_partition_info *pbuf;
  465. if (uuid_parse(uuid_str, &uuid)) {
  466. pr_err("invalid uuid (%s)\n", uuid_str);
  467. return -ENODEV;
  468. }
  469. count = ffa_partition_probe(&uuid, &pbuf);
  470. if (count <= 0)
  471. return -ENOENT;
  472. memcpy(buffer, pbuf, sizeof(*pbuf) * count);
  473. kfree(pbuf);
  474. return 0;
  475. }
  476. static void ffa_mode_32bit_set(struct ffa_device *dev)
  477. {
  478. dev->mode_32bit = true;
  479. }
  480. static int ffa_sync_send_receive(struct ffa_device *dev,
  481. struct ffa_send_direct_data *data)
  482. {
  483. return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id,
  484. dev->mode_32bit, data);
  485. }
  486. static int ffa_memory_share(struct ffa_mem_ops_args *args)
  487. {
  488. if (drv_info->mem_ops_native)
  489. return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args);
  490. return ffa_memory_ops(FFA_MEM_SHARE, args);
  491. }
  492. static int ffa_memory_lend(struct ffa_mem_ops_args *args)
  493. {
  494. /* Note that upon a successful MEM_LEND request the caller
  495. * must ensure that the memory region specified is not accessed
  496. * until a successful MEM_RECALIM call has been made.
  497. * On systems with a hypervisor present this will been enforced,
  498. * however on systems without a hypervisor the responsibility
  499. * falls to the calling kernel driver to prevent access.
  500. */
  501. if (drv_info->mem_ops_native)
  502. return ffa_memory_ops(FFA_FN_NATIVE(MEM_LEND), args);
  503. return ffa_memory_ops(FFA_MEM_LEND, args);
  504. }
  505. static const struct ffa_info_ops ffa_drv_info_ops = {
  506. .api_version_get = ffa_api_version_get,
  507. .partition_info_get = ffa_partition_info_get,
  508. };
  509. static const struct ffa_msg_ops ffa_drv_msg_ops = {
  510. .mode_32bit_set = ffa_mode_32bit_set,
  511. .sync_send_receive = ffa_sync_send_receive,
  512. };
  513. static const struct ffa_mem_ops ffa_drv_mem_ops = {
  514. .memory_reclaim = ffa_memory_reclaim,
  515. .memory_share = ffa_memory_share,
  516. .memory_lend = ffa_memory_lend,
  517. };
  518. static const struct ffa_ops ffa_drv_ops = {
  519. .info_ops = &ffa_drv_info_ops,
  520. .msg_ops = &ffa_drv_msg_ops,
  521. .mem_ops = &ffa_drv_mem_ops,
  522. };
  523. void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid)
  524. {
  525. int count, idx;
  526. struct ffa_partition_info *pbuf, *tpbuf;
  527. /*
  528. * FF-A v1.1 provides UUID for each partition as part of the discovery
  529. * API, the discovered UUID must be populated in the device's UUID and
  530. * there is no need to copy the same from the driver table.
  531. */
  532. if (drv_info->version > FFA_VERSION_1_0)
  533. return;
  534. count = ffa_partition_probe(uuid, &pbuf);
  535. if (count <= 0)
  536. return;
  537. for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++)
  538. if (tpbuf->id == ffa_dev->vm_id)
  539. uuid_copy(&ffa_dev->uuid, uuid);
  540. kfree(pbuf);
  541. }
  542. static void ffa_setup_partitions(void)
  543. {
  544. int count, idx;
  545. uuid_t uuid;
  546. struct ffa_device *ffa_dev;
  547. struct ffa_partition_info *pbuf, *tpbuf;
  548. count = ffa_partition_probe(&uuid_null, &pbuf);
  549. if (count <= 0) {
  550. pr_info("%s: No partitions found, error %d\n", __func__, count);
  551. return;
  552. }
  553. for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) {
  554. import_uuid(&uuid, (u8 *)tpbuf->uuid);
  555. /* Note that if the UUID will be uuid_null, that will require
  556. * ffa_device_match() to find the UUID of this partition id
  557. * with help of ffa_device_match_uuid(). FF-A v1.1 and above
  558. * provides UUID here for each partition as part of the
  559. * discovery API and the same is passed.
  560. */
  561. ffa_dev = ffa_device_register(&uuid, tpbuf->id, &ffa_drv_ops);
  562. if (!ffa_dev) {
  563. pr_err("%s: failed to register partition ID 0x%x\n",
  564. __func__, tpbuf->id);
  565. continue;
  566. }
  567. if (drv_info->version > FFA_VERSION_1_0 &&
  568. !(tpbuf->properties & FFA_PARTITION_AARCH64_EXEC))
  569. ffa_mode_32bit_set(ffa_dev);
  570. }
  571. kfree(pbuf);
  572. }
  573. static int __init ffa_init(void)
  574. {
  575. int ret;
  576. ret = ffa_transport_init(&invoke_ffa_fn);
  577. if (ret)
  578. return ret;
  579. ret = arm_ffa_bus_init();
  580. if (ret)
  581. return ret;
  582. drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL);
  583. if (!drv_info) {
  584. ret = -ENOMEM;
  585. goto ffa_bus_exit;
  586. }
  587. ret = ffa_version_check(&drv_info->version);
  588. if (ret)
  589. goto free_drv_info;
  590. if (ffa_id_get(&drv_info->vm_id)) {
  591. pr_err("failed to obtain VM id for self\n");
  592. ret = -ENODEV;
  593. goto free_drv_info;
  594. }
  595. drv_info->rx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
  596. if (!drv_info->rx_buffer) {
  597. ret = -ENOMEM;
  598. goto free_pages;
  599. }
  600. drv_info->tx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
  601. if (!drv_info->tx_buffer) {
  602. ret = -ENOMEM;
  603. goto free_pages;
  604. }
  605. ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
  606. virt_to_phys(drv_info->rx_buffer),
  607. RXTX_BUFFER_SIZE / FFA_PAGE_SIZE);
  608. if (ret) {
  609. pr_err("failed to register FFA RxTx buffers\n");
  610. goto free_pages;
  611. }
  612. mutex_init(&drv_info->rx_lock);
  613. mutex_init(&drv_info->tx_lock);
  614. ffa_setup_partitions();
  615. ffa_set_up_mem_ops_native_flag();
  616. return 0;
  617. free_pages:
  618. if (drv_info->tx_buffer)
  619. free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
  620. free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
  621. free_drv_info:
  622. kfree(drv_info);
  623. ffa_bus_exit:
  624. arm_ffa_bus_exit();
  625. return ret;
  626. }
  627. subsys_initcall(ffa_init);
  628. static void __exit ffa_exit(void)
  629. {
  630. ffa_rxtx_unmap(drv_info->vm_id);
  631. free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
  632. free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
  633. kfree(drv_info);
  634. arm_ffa_bus_exit();
  635. }
  636. module_exit(ffa_exit);
  637. MODULE_ALIAS("arm-ffa");
  638. MODULE_AUTHOR("Sudeep Holla <[email protected]>");
  639. MODULE_DESCRIPTION("Arm FF-A interface driver");
  640. MODULE_LICENSE("GPL v2");