driver.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  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/acpi.h>
  24. #include <linux/arm_ffa.h>
  25. #include <linux/bitfield.h>
  26. #include <linux/cpuhotplug.h>
  27. #include <linux/delay.h>
  28. #include <linux/device.h>
  29. #include <linux/hashtable.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/io.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/mm.h>
  35. #include <linux/mutex.h>
  36. #include <linux/of_irq.h>
  37. #include <linux/scatterlist.h>
  38. #include <linux/slab.h>
  39. #include <linux/smp.h>
  40. #include <linux/uuid.h>
  41. #include <linux/xarray.h>
  42. #include "common.h"
  43. #define FFA_DRIVER_VERSION FFA_VERSION_1_1
  44. #define FFA_MIN_VERSION FFA_VERSION_1_0
  45. #define SENDER_ID_MASK GENMASK(31, 16)
  46. #define RECEIVER_ID_MASK GENMASK(15, 0)
  47. #define SENDER_ID(x) ((u16)(FIELD_GET(SENDER_ID_MASK, (x))))
  48. #define RECEIVER_ID(x) ((u16)(FIELD_GET(RECEIVER_ID_MASK, (x))))
  49. #define PACK_TARGET_INFO(s, r) \
  50. (FIELD_PREP(SENDER_ID_MASK, (s)) | FIELD_PREP(RECEIVER_ID_MASK, (r)))
  51. #define RXTX_MAP_MIN_BUFSZ_MASK GENMASK(1, 0)
  52. #define RXTX_MAP_MIN_BUFSZ(x) ((x) & RXTX_MAP_MIN_BUFSZ_MASK)
  53. #define FFA_MAX_NOTIFICATIONS 64
  54. static ffa_fn *invoke_ffa_fn;
  55. static const int ffa_linux_errmap[] = {
  56. /* better than switch case as long as return value is continuous */
  57. 0, /* FFA_RET_SUCCESS */
  58. -EOPNOTSUPP, /* FFA_RET_NOT_SUPPORTED */
  59. -EINVAL, /* FFA_RET_INVALID_PARAMETERS */
  60. -ENOMEM, /* FFA_RET_NO_MEMORY */
  61. -EBUSY, /* FFA_RET_BUSY */
  62. -EINTR, /* FFA_RET_INTERRUPTED */
  63. -EACCES, /* FFA_RET_DENIED */
  64. -EAGAIN, /* FFA_RET_RETRY */
  65. -ECANCELED, /* FFA_RET_ABORTED */
  66. -ENODATA, /* FFA_RET_NO_DATA */
  67. -EAGAIN, /* FFA_RET_NOT_READY */
  68. };
  69. static inline int ffa_to_linux_errno(int errno)
  70. {
  71. int err_idx = -errno;
  72. if (err_idx >= 0 && err_idx < ARRAY_SIZE(ffa_linux_errmap))
  73. return ffa_linux_errmap[err_idx];
  74. return -EINVAL;
  75. }
  76. struct ffa_pcpu_irq {
  77. struct ffa_drv_info *info;
  78. };
  79. struct ffa_drv_info {
  80. u32 version;
  81. u16 vm_id;
  82. struct mutex rx_lock; /* lock to protect Rx buffer */
  83. struct mutex tx_lock; /* lock to protect Tx buffer */
  84. void *rx_buffer;
  85. void *tx_buffer;
  86. size_t rxtx_bufsz;
  87. bool mem_ops_native;
  88. bool msg_direct_req2_supp;
  89. bool bitmap_created;
  90. bool notif_enabled;
  91. unsigned int sched_recv_irq;
  92. unsigned int notif_pend_irq;
  93. unsigned int cpuhp_state;
  94. struct ffa_pcpu_irq __percpu *irq_pcpu;
  95. struct workqueue_struct *notif_pcpu_wq;
  96. struct work_struct notif_pcpu_work;
  97. struct work_struct sched_recv_irq_work;
  98. struct xarray partition_info;
  99. DECLARE_HASHTABLE(notifier_hash, ilog2(FFA_MAX_NOTIFICATIONS));
  100. rwlock_t notify_lock; /* lock to protect notifier hashtable */
  101. };
  102. static struct ffa_drv_info *drv_info;
  103. static void ffa_partitions_cleanup(void);
  104. /*
  105. * The driver must be able to support all the versions from the earliest
  106. * supported FFA_MIN_VERSION to the latest supported FFA_DRIVER_VERSION.
  107. * The specification states that if firmware supports a FFA implementation
  108. * that is incompatible with and at a greater version number than specified
  109. * by the caller(FFA_DRIVER_VERSION passed as parameter to FFA_VERSION),
  110. * it must return the NOT_SUPPORTED error code.
  111. */
  112. static u32 ffa_compatible_version_find(u32 version)
  113. {
  114. u16 major = FFA_MAJOR_VERSION(version), minor = FFA_MINOR_VERSION(version);
  115. u16 drv_major = FFA_MAJOR_VERSION(FFA_DRIVER_VERSION);
  116. u16 drv_minor = FFA_MINOR_VERSION(FFA_DRIVER_VERSION);
  117. if ((major < drv_major) || (major == drv_major && minor <= drv_minor))
  118. return version;
  119. pr_info("Firmware version higher than driver version, downgrading\n");
  120. return FFA_DRIVER_VERSION;
  121. }
  122. static int ffa_version_check(u32 *version)
  123. {
  124. ffa_value_t ver;
  125. invoke_ffa_fn((ffa_value_t){
  126. .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION,
  127. }, &ver);
  128. if (ver.a0 == FFA_RET_NOT_SUPPORTED) {
  129. pr_info("FFA_VERSION returned not supported\n");
  130. return -EOPNOTSUPP;
  131. }
  132. if (FFA_MAJOR_VERSION(ver.a0) > FFA_MAJOR_VERSION(FFA_DRIVER_VERSION)) {
  133. pr_err("Incompatible v%d.%d! Latest supported v%d.%d\n",
  134. FFA_MAJOR_VERSION(ver.a0), FFA_MINOR_VERSION(ver.a0),
  135. FFA_MAJOR_VERSION(FFA_DRIVER_VERSION),
  136. FFA_MINOR_VERSION(FFA_DRIVER_VERSION));
  137. return -EINVAL;
  138. }
  139. if (ver.a0 < FFA_MIN_VERSION) {
  140. pr_err("Incompatible v%d.%d! Earliest supported v%d.%d\n",
  141. FFA_MAJOR_VERSION(ver.a0), FFA_MINOR_VERSION(ver.a0),
  142. FFA_MAJOR_VERSION(FFA_MIN_VERSION),
  143. FFA_MINOR_VERSION(FFA_MIN_VERSION));
  144. return -EINVAL;
  145. }
  146. pr_info("Driver version %d.%d\n", FFA_MAJOR_VERSION(FFA_DRIVER_VERSION),
  147. FFA_MINOR_VERSION(FFA_DRIVER_VERSION));
  148. pr_info("Firmware version %d.%d found\n", FFA_MAJOR_VERSION(ver.a0),
  149. FFA_MINOR_VERSION(ver.a0));
  150. *version = ffa_compatible_version_find(ver.a0);
  151. return 0;
  152. }
  153. static int ffa_rx_release(void)
  154. {
  155. ffa_value_t ret;
  156. invoke_ffa_fn((ffa_value_t){
  157. .a0 = FFA_RX_RELEASE,
  158. }, &ret);
  159. if (ret.a0 == FFA_ERROR)
  160. return ffa_to_linux_errno((int)ret.a2);
  161. /* check for ret.a0 == FFA_RX_RELEASE ? */
  162. return 0;
  163. }
  164. static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt)
  165. {
  166. ffa_value_t ret;
  167. invoke_ffa_fn((ffa_value_t){
  168. .a0 = FFA_FN_NATIVE(RXTX_MAP),
  169. .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt,
  170. }, &ret);
  171. if (ret.a0 == FFA_ERROR)
  172. return ffa_to_linux_errno((int)ret.a2);
  173. return 0;
  174. }
  175. static int ffa_rxtx_unmap(u16 vm_id)
  176. {
  177. ffa_value_t ret;
  178. invoke_ffa_fn((ffa_value_t){
  179. .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0),
  180. }, &ret);
  181. if (ret.a0 == FFA_ERROR)
  182. return ffa_to_linux_errno((int)ret.a2);
  183. return 0;
  184. }
  185. static int ffa_features(u32 func_feat_id, u32 input_props,
  186. u32 *if_props_1, u32 *if_props_2)
  187. {
  188. ffa_value_t id;
  189. if (!ARM_SMCCC_IS_FAST_CALL(func_feat_id) && input_props) {
  190. pr_err("%s: Invalid Parameters: %x, %x", __func__,
  191. func_feat_id, input_props);
  192. return ffa_to_linux_errno(FFA_RET_INVALID_PARAMETERS);
  193. }
  194. invoke_ffa_fn((ffa_value_t){
  195. .a0 = FFA_FEATURES, .a1 = func_feat_id, .a2 = input_props,
  196. }, &id);
  197. if (id.a0 == FFA_ERROR)
  198. return ffa_to_linux_errno((int)id.a2);
  199. if (if_props_1)
  200. *if_props_1 = id.a2;
  201. if (if_props_2)
  202. *if_props_2 = id.a3;
  203. return 0;
  204. }
  205. #define PARTITION_INFO_GET_RETURN_COUNT_ONLY BIT(0)
  206. /* buffer must be sizeof(struct ffa_partition_info) * num_partitions */
  207. static int
  208. __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
  209. struct ffa_partition_info *buffer, int num_partitions)
  210. {
  211. int idx, count, flags = 0, sz, buf_sz;
  212. ffa_value_t partition_info;
  213. if (drv_info->version > FFA_VERSION_1_0 &&
  214. (!buffer || !num_partitions)) /* Just get the count for now */
  215. flags = PARTITION_INFO_GET_RETURN_COUNT_ONLY;
  216. mutex_lock(&drv_info->rx_lock);
  217. invoke_ffa_fn((ffa_value_t){
  218. .a0 = FFA_PARTITION_INFO_GET,
  219. .a1 = uuid0, .a2 = uuid1, .a3 = uuid2, .a4 = uuid3,
  220. .a5 = flags,
  221. }, &partition_info);
  222. if (partition_info.a0 == FFA_ERROR) {
  223. mutex_unlock(&drv_info->rx_lock);
  224. return ffa_to_linux_errno((int)partition_info.a2);
  225. }
  226. count = partition_info.a2;
  227. if (drv_info->version > FFA_VERSION_1_0) {
  228. buf_sz = sz = partition_info.a3;
  229. if (sz > sizeof(*buffer))
  230. buf_sz = sizeof(*buffer);
  231. } else {
  232. /* FFA_VERSION_1_0 lacks size in the response */
  233. buf_sz = sz = 8;
  234. }
  235. if (buffer && count <= num_partitions)
  236. for (idx = 0; idx < count; idx++)
  237. memcpy(buffer + idx, drv_info->rx_buffer + idx * sz,
  238. buf_sz);
  239. if (!(flags & PARTITION_INFO_GET_RETURN_COUNT_ONLY))
  240. ffa_rx_release();
  241. mutex_unlock(&drv_info->rx_lock);
  242. return count;
  243. }
  244. #define LAST_INDEX_MASK GENMASK(15, 0)
  245. #define CURRENT_INDEX_MASK GENMASK(31, 16)
  246. #define UUID_INFO_TAG_MASK GENMASK(47, 32)
  247. #define PARTITION_INFO_SZ_MASK GENMASK(63, 48)
  248. #define PARTITION_COUNT(x) ((u16)(FIELD_GET(LAST_INDEX_MASK, (x))) + 1)
  249. #define CURRENT_INDEX(x) ((u16)(FIELD_GET(CURRENT_INDEX_MASK, (x))))
  250. #define UUID_INFO_TAG(x) ((u16)(FIELD_GET(UUID_INFO_TAG_MASK, (x))))
  251. #define PARTITION_INFO_SZ(x) ((u16)(FIELD_GET(PARTITION_INFO_SZ_MASK, (x))))
  252. static int
  253. __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
  254. struct ffa_partition_info *buffer, int num_parts)
  255. {
  256. u16 buf_sz, start_idx, cur_idx, count = 0, prev_idx = 0, tag = 0;
  257. ffa_value_t partition_info;
  258. do {
  259. start_idx = prev_idx ? prev_idx + 1 : 0;
  260. invoke_ffa_fn((ffa_value_t){
  261. .a0 = FFA_PARTITION_INFO_GET_REGS,
  262. .a1 = (u64)uuid1 << 32 | uuid0,
  263. .a2 = (u64)uuid3 << 32 | uuid2,
  264. .a3 = start_idx | tag << 16,
  265. }, &partition_info);
  266. if (partition_info.a0 == FFA_ERROR)
  267. return ffa_to_linux_errno((int)partition_info.a2);
  268. if (!count)
  269. count = PARTITION_COUNT(partition_info.a2);
  270. if (!buffer || !num_parts) /* count only */
  271. return count;
  272. cur_idx = CURRENT_INDEX(partition_info.a2);
  273. tag = UUID_INFO_TAG(partition_info.a2);
  274. buf_sz = PARTITION_INFO_SZ(partition_info.a2);
  275. if (buf_sz > sizeof(*buffer))
  276. buf_sz = sizeof(*buffer);
  277. memcpy(buffer + prev_idx * buf_sz, &partition_info.a3,
  278. (cur_idx - start_idx + 1) * buf_sz);
  279. prev_idx = cur_idx;
  280. } while (cur_idx < (count - 1));
  281. return count;
  282. }
  283. /* buffer is allocated and caller must free the same if returned count > 0 */
  284. static int
  285. ffa_partition_probe(const uuid_t *uuid, struct ffa_partition_info **buffer)
  286. {
  287. int count;
  288. u32 uuid0_4[4];
  289. bool reg_mode = false;
  290. struct ffa_partition_info *pbuf;
  291. if (!ffa_features(FFA_PARTITION_INFO_GET_REGS, 0, NULL, NULL))
  292. reg_mode = true;
  293. export_uuid((u8 *)uuid0_4, uuid);
  294. if (reg_mode)
  295. count = __ffa_partition_info_get_regs(uuid0_4[0], uuid0_4[1],
  296. uuid0_4[2], uuid0_4[3],
  297. NULL, 0);
  298. else
  299. count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1],
  300. uuid0_4[2], uuid0_4[3],
  301. NULL, 0);
  302. if (count <= 0)
  303. return count;
  304. pbuf = kcalloc(count, sizeof(*pbuf), GFP_KERNEL);
  305. if (!pbuf)
  306. return -ENOMEM;
  307. if (reg_mode)
  308. count = __ffa_partition_info_get_regs(uuid0_4[0], uuid0_4[1],
  309. uuid0_4[2], uuid0_4[3],
  310. pbuf, count);
  311. else
  312. count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1],
  313. uuid0_4[2], uuid0_4[3],
  314. pbuf, count);
  315. if (count <= 0)
  316. kfree(pbuf);
  317. else
  318. *buffer = pbuf;
  319. return count;
  320. }
  321. #define VM_ID_MASK GENMASK(15, 0)
  322. static int ffa_id_get(u16 *vm_id)
  323. {
  324. ffa_value_t id;
  325. invoke_ffa_fn((ffa_value_t){
  326. .a0 = FFA_ID_GET,
  327. }, &id);
  328. if (id.a0 == FFA_ERROR)
  329. return ffa_to_linux_errno((int)id.a2);
  330. *vm_id = FIELD_GET(VM_ID_MASK, (id.a2));
  331. return 0;
  332. }
  333. static inline void ffa_msg_send_wait_for_completion(ffa_value_t *ret)
  334. {
  335. while (ret->a0 == FFA_INTERRUPT || ret->a0 == FFA_YIELD) {
  336. if (ret->a0 == FFA_YIELD)
  337. fsleep(1000);
  338. invoke_ffa_fn((ffa_value_t){
  339. .a0 = FFA_RUN, .a1 = ret->a1,
  340. }, ret);
  341. }
  342. }
  343. static int ffa_msg_send_direct_req(u16 src_id, u16 dst_id, bool mode_32bit,
  344. struct ffa_send_direct_data *data)
  345. {
  346. u32 req_id, resp_id, src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
  347. ffa_value_t ret;
  348. if (mode_32bit) {
  349. req_id = FFA_MSG_SEND_DIRECT_REQ;
  350. resp_id = FFA_MSG_SEND_DIRECT_RESP;
  351. } else {
  352. req_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_REQ);
  353. resp_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_RESP);
  354. }
  355. invoke_ffa_fn((ffa_value_t){
  356. .a0 = req_id, .a1 = src_dst_ids, .a2 = 0,
  357. .a3 = data->data0, .a4 = data->data1, .a5 = data->data2,
  358. .a6 = data->data3, .a7 = data->data4,
  359. }, &ret);
  360. ffa_msg_send_wait_for_completion(&ret);
  361. if (ret.a0 == FFA_ERROR)
  362. return ffa_to_linux_errno((int)ret.a2);
  363. if (ret.a0 == resp_id) {
  364. data->data0 = ret.a3;
  365. data->data1 = ret.a4;
  366. data->data2 = ret.a5;
  367. data->data3 = ret.a6;
  368. data->data4 = ret.a7;
  369. return 0;
  370. }
  371. return -EINVAL;
  372. }
  373. static int ffa_msg_send2(u16 src_id, u16 dst_id, void *buf, size_t sz)
  374. {
  375. u32 src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
  376. struct ffa_indirect_msg_hdr *msg;
  377. ffa_value_t ret;
  378. int retval = 0;
  379. if (sz > (drv_info->rxtx_bufsz - sizeof(*msg)))
  380. return -ERANGE;
  381. mutex_lock(&drv_info->tx_lock);
  382. msg = drv_info->tx_buffer;
  383. msg->flags = 0;
  384. msg->res0 = 0;
  385. msg->offset = sizeof(*msg);
  386. msg->send_recv_id = src_dst_ids;
  387. msg->size = sz;
  388. memcpy((u8 *)msg + msg->offset, buf, sz);
  389. /* flags = 0, sender VMID = 0 works for both physical/virtual NS */
  390. invoke_ffa_fn((ffa_value_t){
  391. .a0 = FFA_MSG_SEND2, .a1 = 0, .a2 = 0
  392. }, &ret);
  393. if (ret.a0 == FFA_ERROR)
  394. retval = ffa_to_linux_errno((int)ret.a2);
  395. mutex_unlock(&drv_info->tx_lock);
  396. return retval;
  397. }
  398. static int ffa_msg_send_direct_req2(u16 src_id, u16 dst_id, const uuid_t *uuid,
  399. struct ffa_send_direct_data2 *data)
  400. {
  401. u32 src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
  402. union {
  403. uuid_t uuid;
  404. __le64 regs[2];
  405. } uuid_regs = { .uuid = *uuid };
  406. ffa_value_t ret, args = {
  407. .a0 = FFA_MSG_SEND_DIRECT_REQ2,
  408. .a1 = src_dst_ids,
  409. .a2 = le64_to_cpu(uuid_regs.regs[0]),
  410. .a3 = le64_to_cpu(uuid_regs.regs[1]),
  411. };
  412. memcpy((void *)&args + offsetof(ffa_value_t, a4), data, sizeof(*data));
  413. invoke_ffa_fn(args, &ret);
  414. ffa_msg_send_wait_for_completion(&ret);
  415. if (ret.a0 == FFA_ERROR)
  416. return ffa_to_linux_errno((int)ret.a2);
  417. if (ret.a0 == FFA_MSG_SEND_DIRECT_RESP2) {
  418. memcpy(data, (void *)&ret + offsetof(ffa_value_t, a4), sizeof(*data));
  419. return 0;
  420. }
  421. return -EINVAL;
  422. }
  423. static int ffa_mem_first_frag(u32 func_id, phys_addr_t buf, u32 buf_sz,
  424. u32 frag_len, u32 len, u64 *handle)
  425. {
  426. ffa_value_t ret;
  427. invoke_ffa_fn((ffa_value_t){
  428. .a0 = func_id, .a1 = len, .a2 = frag_len,
  429. .a3 = buf, .a4 = buf_sz,
  430. }, &ret);
  431. while (ret.a0 == FFA_MEM_OP_PAUSE)
  432. invoke_ffa_fn((ffa_value_t){
  433. .a0 = FFA_MEM_OP_RESUME,
  434. .a1 = ret.a1, .a2 = ret.a2,
  435. }, &ret);
  436. if (ret.a0 == FFA_ERROR)
  437. return ffa_to_linux_errno((int)ret.a2);
  438. if (ret.a0 == FFA_SUCCESS) {
  439. if (handle)
  440. *handle = PACK_HANDLE(ret.a2, ret.a3);
  441. } else if (ret.a0 == FFA_MEM_FRAG_RX) {
  442. if (handle)
  443. *handle = PACK_HANDLE(ret.a1, ret.a2);
  444. } else {
  445. return -EOPNOTSUPP;
  446. }
  447. return frag_len;
  448. }
  449. static int ffa_mem_next_frag(u64 handle, u32 frag_len)
  450. {
  451. ffa_value_t ret;
  452. invoke_ffa_fn((ffa_value_t){
  453. .a0 = FFA_MEM_FRAG_TX,
  454. .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle),
  455. .a3 = frag_len,
  456. }, &ret);
  457. while (ret.a0 == FFA_MEM_OP_PAUSE)
  458. invoke_ffa_fn((ffa_value_t){
  459. .a0 = FFA_MEM_OP_RESUME,
  460. .a1 = ret.a1, .a2 = ret.a2,
  461. }, &ret);
  462. if (ret.a0 == FFA_ERROR)
  463. return ffa_to_linux_errno((int)ret.a2);
  464. if (ret.a0 == FFA_MEM_FRAG_RX)
  465. return ret.a3;
  466. else if (ret.a0 == FFA_SUCCESS)
  467. return 0;
  468. return -EOPNOTSUPP;
  469. }
  470. static int
  471. ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len,
  472. u32 len, u64 *handle, bool first)
  473. {
  474. if (!first)
  475. return ffa_mem_next_frag(*handle, frag_len);
  476. return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle);
  477. }
  478. static u32 ffa_get_num_pages_sg(struct scatterlist *sg)
  479. {
  480. u32 num_pages = 0;
  481. do {
  482. num_pages += sg->length / FFA_PAGE_SIZE;
  483. } while ((sg = sg_next(sg)));
  484. return num_pages;
  485. }
  486. static u16 ffa_memory_attributes_get(u32 func_id)
  487. {
  488. /*
  489. * For the memory lend or donate operation, if the receiver is a PE or
  490. * a proxy endpoint, the owner/sender must not specify the attributes
  491. */
  492. if (func_id == FFA_FN_NATIVE(MEM_LEND) ||
  493. func_id == FFA_MEM_LEND)
  494. return 0;
  495. return FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK | FFA_MEM_INNER_SHAREABLE;
  496. }
  497. static int
  498. ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
  499. struct ffa_mem_ops_args *args)
  500. {
  501. int rc = 0;
  502. bool first = true;
  503. u32 composite_offset;
  504. phys_addr_t addr = 0;
  505. struct ffa_mem_region *mem_region = buffer;
  506. struct ffa_composite_mem_region *composite;
  507. struct ffa_mem_region_addr_range *constituents;
  508. struct ffa_mem_region_attributes *ep_mem_access;
  509. u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg);
  510. mem_region->tag = args->tag;
  511. mem_region->flags = args->flags;
  512. mem_region->sender_id = drv_info->vm_id;
  513. mem_region->attributes = ffa_memory_attributes_get(func_id);
  514. ep_mem_access = buffer +
  515. ffa_mem_desc_offset(buffer, 0, drv_info->version);
  516. composite_offset = ffa_mem_desc_offset(buffer, args->nattrs,
  517. drv_info->version);
  518. for (idx = 0; idx < args->nattrs; idx++, ep_mem_access++) {
  519. ep_mem_access->receiver = args->attrs[idx].receiver;
  520. ep_mem_access->attrs = args->attrs[idx].attrs;
  521. ep_mem_access->composite_off = composite_offset;
  522. ep_mem_access->flag = 0;
  523. ep_mem_access->reserved = 0;
  524. }
  525. mem_region->handle = 0;
  526. mem_region->ep_count = args->nattrs;
  527. if (drv_info->version <= FFA_VERSION_1_0) {
  528. mem_region->ep_mem_size = 0;
  529. } else {
  530. mem_region->ep_mem_size = sizeof(*ep_mem_access);
  531. mem_region->ep_mem_offset = sizeof(*mem_region);
  532. memset(mem_region->reserved, 0, 12);
  533. }
  534. composite = buffer + composite_offset;
  535. composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg);
  536. composite->addr_range_cnt = num_entries;
  537. composite->reserved = 0;
  538. length = composite_offset + CONSTITUENTS_OFFSET(num_entries);
  539. frag_len = composite_offset + CONSTITUENTS_OFFSET(0);
  540. if (frag_len > max_fragsize)
  541. return -ENXIO;
  542. if (!args->use_txbuf) {
  543. addr = virt_to_phys(buffer);
  544. buf_sz = max_fragsize / FFA_PAGE_SIZE;
  545. }
  546. constituents = buffer + frag_len;
  547. idx = 0;
  548. do {
  549. if (frag_len == max_fragsize) {
  550. rc = ffa_transmit_fragment(func_id, addr, buf_sz,
  551. frag_len, length,
  552. &args->g_handle, first);
  553. if (rc < 0)
  554. return -ENXIO;
  555. first = false;
  556. idx = 0;
  557. frag_len = 0;
  558. constituents = buffer;
  559. }
  560. if ((void *)constituents - buffer > max_fragsize) {
  561. pr_err("Memory Region Fragment > Tx Buffer size\n");
  562. return -EFAULT;
  563. }
  564. constituents->address = sg_phys(args->sg);
  565. constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE;
  566. constituents->reserved = 0;
  567. constituents++;
  568. frag_len += sizeof(struct ffa_mem_region_addr_range);
  569. } while ((args->sg = sg_next(args->sg)));
  570. return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len,
  571. length, &args->g_handle, first);
  572. }
  573. static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args)
  574. {
  575. int ret;
  576. void *buffer;
  577. size_t rxtx_bufsz = drv_info->rxtx_bufsz;
  578. if (!args->use_txbuf) {
  579. buffer = alloc_pages_exact(rxtx_bufsz, GFP_KERNEL);
  580. if (!buffer)
  581. return -ENOMEM;
  582. } else {
  583. buffer = drv_info->tx_buffer;
  584. mutex_lock(&drv_info->tx_lock);
  585. }
  586. ret = ffa_setup_and_transmit(func_id, buffer, rxtx_bufsz, args);
  587. if (args->use_txbuf)
  588. mutex_unlock(&drv_info->tx_lock);
  589. else
  590. free_pages_exact(buffer, rxtx_bufsz);
  591. return ret < 0 ? ret : 0;
  592. }
  593. static int ffa_memory_reclaim(u64 g_handle, u32 flags)
  594. {
  595. ffa_value_t ret;
  596. invoke_ffa_fn((ffa_value_t){
  597. .a0 = FFA_MEM_RECLAIM,
  598. .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle),
  599. .a3 = flags,
  600. }, &ret);
  601. if (ret.a0 == FFA_ERROR)
  602. return ffa_to_linux_errno((int)ret.a2);
  603. return 0;
  604. }
  605. static int ffa_notification_bitmap_create(void)
  606. {
  607. ffa_value_t ret;
  608. u16 vcpu_count = nr_cpu_ids;
  609. invoke_ffa_fn((ffa_value_t){
  610. .a0 = FFA_NOTIFICATION_BITMAP_CREATE,
  611. .a1 = drv_info->vm_id, .a2 = vcpu_count,
  612. }, &ret);
  613. if (ret.a0 == FFA_ERROR)
  614. return ffa_to_linux_errno((int)ret.a2);
  615. return 0;
  616. }
  617. static int ffa_notification_bitmap_destroy(void)
  618. {
  619. ffa_value_t ret;
  620. invoke_ffa_fn((ffa_value_t){
  621. .a0 = FFA_NOTIFICATION_BITMAP_DESTROY,
  622. .a1 = drv_info->vm_id,
  623. }, &ret);
  624. if (ret.a0 == FFA_ERROR)
  625. return ffa_to_linux_errno((int)ret.a2);
  626. return 0;
  627. }
  628. #define NOTIFICATION_LOW_MASK GENMASK(31, 0)
  629. #define NOTIFICATION_HIGH_MASK GENMASK(63, 32)
  630. #define NOTIFICATION_BITMAP_HIGH(x) \
  631. ((u32)(FIELD_GET(NOTIFICATION_HIGH_MASK, (x))))
  632. #define NOTIFICATION_BITMAP_LOW(x) \
  633. ((u32)(FIELD_GET(NOTIFICATION_LOW_MASK, (x))))
  634. #define PACK_NOTIFICATION_BITMAP(low, high) \
  635. (FIELD_PREP(NOTIFICATION_LOW_MASK, (low)) | \
  636. FIELD_PREP(NOTIFICATION_HIGH_MASK, (high)))
  637. #define RECEIVER_VCPU_MASK GENMASK(31, 16)
  638. #define PACK_NOTIFICATION_GET_RECEIVER_INFO(vcpu_r, r) \
  639. (FIELD_PREP(RECEIVER_VCPU_MASK, (vcpu_r)) | \
  640. FIELD_PREP(RECEIVER_ID_MASK, (r)))
  641. #define NOTIFICATION_INFO_GET_MORE_PEND_MASK BIT(0)
  642. #define NOTIFICATION_INFO_GET_ID_COUNT GENMASK(11, 7)
  643. #define ID_LIST_MASK_64 GENMASK(51, 12)
  644. #define ID_LIST_MASK_32 GENMASK(31, 12)
  645. #define MAX_IDS_64 20
  646. #define MAX_IDS_32 10
  647. #define PER_VCPU_NOTIFICATION_FLAG BIT(0)
  648. #define SECURE_PARTITION_BITMAP BIT(0)
  649. #define NON_SECURE_VM_BITMAP BIT(1)
  650. #define SPM_FRAMEWORK_BITMAP BIT(2)
  651. #define NS_HYP_FRAMEWORK_BITMAP BIT(3)
  652. static int ffa_notification_bind_common(u16 dst_id, u64 bitmap,
  653. u32 flags, bool is_bind)
  654. {
  655. ffa_value_t ret;
  656. u32 func, src_dst_ids = PACK_TARGET_INFO(dst_id, drv_info->vm_id);
  657. func = is_bind ? FFA_NOTIFICATION_BIND : FFA_NOTIFICATION_UNBIND;
  658. invoke_ffa_fn((ffa_value_t){
  659. .a0 = func, .a1 = src_dst_ids, .a2 = flags,
  660. .a3 = NOTIFICATION_BITMAP_LOW(bitmap),
  661. .a4 = NOTIFICATION_BITMAP_HIGH(bitmap),
  662. }, &ret);
  663. if (ret.a0 == FFA_ERROR)
  664. return ffa_to_linux_errno((int)ret.a2);
  665. else if (ret.a0 != FFA_SUCCESS)
  666. return -EINVAL;
  667. return 0;
  668. }
  669. static
  670. int ffa_notification_set(u16 src_id, u16 dst_id, u32 flags, u64 bitmap)
  671. {
  672. ffa_value_t ret;
  673. u32 src_dst_ids = PACK_TARGET_INFO(dst_id, src_id);
  674. invoke_ffa_fn((ffa_value_t) {
  675. .a0 = FFA_NOTIFICATION_SET, .a1 = src_dst_ids, .a2 = flags,
  676. .a3 = NOTIFICATION_BITMAP_LOW(bitmap),
  677. .a4 = NOTIFICATION_BITMAP_HIGH(bitmap),
  678. }, &ret);
  679. if (ret.a0 == FFA_ERROR)
  680. return ffa_to_linux_errno((int)ret.a2);
  681. else if (ret.a0 != FFA_SUCCESS)
  682. return -EINVAL;
  683. return 0;
  684. }
  685. struct ffa_notify_bitmaps {
  686. u64 sp_map;
  687. u64 vm_map;
  688. u64 arch_map;
  689. };
  690. static int ffa_notification_get(u32 flags, struct ffa_notify_bitmaps *notify)
  691. {
  692. ffa_value_t ret;
  693. u16 src_id = drv_info->vm_id;
  694. u16 cpu_id = smp_processor_id();
  695. u32 rec_vcpu_ids = PACK_NOTIFICATION_GET_RECEIVER_INFO(cpu_id, src_id);
  696. invoke_ffa_fn((ffa_value_t){
  697. .a0 = FFA_NOTIFICATION_GET, .a1 = rec_vcpu_ids, .a2 = flags,
  698. }, &ret);
  699. if (ret.a0 == FFA_ERROR)
  700. return ffa_to_linux_errno((int)ret.a2);
  701. else if (ret.a0 != FFA_SUCCESS)
  702. return -EINVAL; /* Something else went wrong. */
  703. notify->sp_map = PACK_NOTIFICATION_BITMAP(ret.a2, ret.a3);
  704. notify->vm_map = PACK_NOTIFICATION_BITMAP(ret.a4, ret.a5);
  705. notify->arch_map = PACK_NOTIFICATION_BITMAP(ret.a6, ret.a7);
  706. return 0;
  707. }
  708. struct ffa_dev_part_info {
  709. ffa_sched_recv_cb callback;
  710. void *cb_data;
  711. rwlock_t rw_lock;
  712. };
  713. static void __do_sched_recv_cb(u16 part_id, u16 vcpu, bool is_per_vcpu)
  714. {
  715. struct ffa_dev_part_info *partition;
  716. ffa_sched_recv_cb callback;
  717. void *cb_data;
  718. partition = xa_load(&drv_info->partition_info, part_id);
  719. if (!partition) {
  720. pr_err("%s: Invalid partition ID 0x%x\n", __func__, part_id);
  721. return;
  722. }
  723. read_lock(&partition->rw_lock);
  724. callback = partition->callback;
  725. cb_data = partition->cb_data;
  726. read_unlock(&partition->rw_lock);
  727. if (callback)
  728. callback(vcpu, is_per_vcpu, cb_data);
  729. }
  730. static void ffa_notification_info_get(void)
  731. {
  732. int idx, list, max_ids, lists_cnt, ids_processed, ids_count[MAX_IDS_64];
  733. bool is_64b_resp;
  734. ffa_value_t ret;
  735. u64 id_list;
  736. do {
  737. invoke_ffa_fn((ffa_value_t){
  738. .a0 = FFA_FN_NATIVE(NOTIFICATION_INFO_GET),
  739. }, &ret);
  740. if (ret.a0 != FFA_FN_NATIVE(SUCCESS) && ret.a0 != FFA_SUCCESS) {
  741. if (ret.a2 != FFA_RET_NO_DATA)
  742. pr_err("Notification Info fetch failed: 0x%lx (0x%lx)",
  743. ret.a0, ret.a2);
  744. return;
  745. }
  746. is_64b_resp = (ret.a0 == FFA_FN64_SUCCESS);
  747. ids_processed = 0;
  748. lists_cnt = FIELD_GET(NOTIFICATION_INFO_GET_ID_COUNT, ret.a2);
  749. if (is_64b_resp) {
  750. max_ids = MAX_IDS_64;
  751. id_list = FIELD_GET(ID_LIST_MASK_64, ret.a2);
  752. } else {
  753. max_ids = MAX_IDS_32;
  754. id_list = FIELD_GET(ID_LIST_MASK_32, ret.a2);
  755. }
  756. for (idx = 0; idx < lists_cnt; idx++, id_list >>= 2)
  757. ids_count[idx] = (id_list & 0x3) + 1;
  758. /* Process IDs */
  759. for (list = 0; list < lists_cnt; list++) {
  760. u16 vcpu_id, part_id, *packed_id_list = (u16 *)&ret.a3;
  761. if (ids_processed >= max_ids - 1)
  762. break;
  763. part_id = packed_id_list[ids_processed++];
  764. if (ids_count[list] == 1) { /* Global Notification */
  765. __do_sched_recv_cb(part_id, 0, false);
  766. continue;
  767. }
  768. /* Per vCPU Notification */
  769. for (idx = 0; idx < ids_count[list]; idx++) {
  770. if (ids_processed >= max_ids - 1)
  771. break;
  772. vcpu_id = packed_id_list[ids_processed++];
  773. __do_sched_recv_cb(part_id, vcpu_id, true);
  774. }
  775. }
  776. } while (ret.a2 & NOTIFICATION_INFO_GET_MORE_PEND_MASK);
  777. }
  778. static int ffa_run(struct ffa_device *dev, u16 vcpu)
  779. {
  780. ffa_value_t ret;
  781. u32 target = dev->vm_id << 16 | vcpu;
  782. invoke_ffa_fn((ffa_value_t){ .a0 = FFA_RUN, .a1 = target, }, &ret);
  783. while (ret.a0 == FFA_INTERRUPT)
  784. invoke_ffa_fn((ffa_value_t){ .a0 = FFA_RUN, .a1 = ret.a1, },
  785. &ret);
  786. if (ret.a0 == FFA_ERROR)
  787. return ffa_to_linux_errno((int)ret.a2);
  788. return 0;
  789. }
  790. static void ffa_drvinfo_flags_init(void)
  791. {
  792. if (!ffa_features(FFA_FN_NATIVE(MEM_LEND), 0, NULL, NULL) ||
  793. !ffa_features(FFA_FN_NATIVE(MEM_SHARE), 0, NULL, NULL))
  794. drv_info->mem_ops_native = true;
  795. if (!ffa_features(FFA_MSG_SEND_DIRECT_REQ2, 0, NULL, NULL) ||
  796. !ffa_features(FFA_MSG_SEND_DIRECT_RESP2, 0, NULL, NULL))
  797. drv_info->msg_direct_req2_supp = true;
  798. }
  799. static u32 ffa_api_version_get(void)
  800. {
  801. return drv_info->version;
  802. }
  803. static int ffa_partition_info_get(const char *uuid_str,
  804. struct ffa_partition_info *buffer)
  805. {
  806. int count;
  807. uuid_t uuid;
  808. struct ffa_partition_info *pbuf;
  809. if (uuid_parse(uuid_str, &uuid)) {
  810. pr_err("invalid uuid (%s)\n", uuid_str);
  811. return -ENODEV;
  812. }
  813. count = ffa_partition_probe(&uuid, &pbuf);
  814. if (count <= 0)
  815. return -ENOENT;
  816. memcpy(buffer, pbuf, sizeof(*pbuf) * count);
  817. kfree(pbuf);
  818. return 0;
  819. }
  820. static void ffa_mode_32bit_set(struct ffa_device *dev)
  821. {
  822. dev->mode_32bit = true;
  823. }
  824. static int ffa_sync_send_receive(struct ffa_device *dev,
  825. struct ffa_send_direct_data *data)
  826. {
  827. return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id,
  828. dev->mode_32bit, data);
  829. }
  830. static int ffa_indirect_msg_send(struct ffa_device *dev, void *buf, size_t sz)
  831. {
  832. return ffa_msg_send2(drv_info->vm_id, dev->vm_id, buf, sz);
  833. }
  834. static int ffa_sync_send_receive2(struct ffa_device *dev, const uuid_t *uuid,
  835. struct ffa_send_direct_data2 *data)
  836. {
  837. if (!drv_info->msg_direct_req2_supp)
  838. return -EOPNOTSUPP;
  839. return ffa_msg_send_direct_req2(drv_info->vm_id, dev->vm_id,
  840. uuid, data);
  841. }
  842. static int ffa_memory_share(struct ffa_mem_ops_args *args)
  843. {
  844. if (drv_info->mem_ops_native)
  845. return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args);
  846. return ffa_memory_ops(FFA_MEM_SHARE, args);
  847. }
  848. static int ffa_memory_lend(struct ffa_mem_ops_args *args)
  849. {
  850. /* Note that upon a successful MEM_LEND request the caller
  851. * must ensure that the memory region specified is not accessed
  852. * until a successful MEM_RECALIM call has been made.
  853. * On systems with a hypervisor present this will been enforced,
  854. * however on systems without a hypervisor the responsibility
  855. * falls to the calling kernel driver to prevent access.
  856. */
  857. if (drv_info->mem_ops_native)
  858. return ffa_memory_ops(FFA_FN_NATIVE(MEM_LEND), args);
  859. return ffa_memory_ops(FFA_MEM_LEND, args);
  860. }
  861. #define FFA_SECURE_PARTITION_ID_FLAG BIT(15)
  862. #define ffa_notifications_disabled() (!drv_info->notif_enabled)
  863. enum notify_type {
  864. NON_SECURE_VM,
  865. SECURE_PARTITION,
  866. FRAMEWORK,
  867. };
  868. struct notifier_cb_info {
  869. struct hlist_node hnode;
  870. ffa_notifier_cb cb;
  871. void *cb_data;
  872. enum notify_type type;
  873. };
  874. static int ffa_sched_recv_cb_update(u16 part_id, ffa_sched_recv_cb callback,
  875. void *cb_data, bool is_registration)
  876. {
  877. struct ffa_dev_part_info *partition;
  878. bool cb_valid;
  879. if (ffa_notifications_disabled())
  880. return -EOPNOTSUPP;
  881. partition = xa_load(&drv_info->partition_info, part_id);
  882. if (!partition) {
  883. pr_err("%s: Invalid partition ID 0x%x\n", __func__, part_id);
  884. return -EINVAL;
  885. }
  886. write_lock(&partition->rw_lock);
  887. cb_valid = !!partition->callback;
  888. if (!(is_registration ^ cb_valid)) {
  889. write_unlock(&partition->rw_lock);
  890. return -EINVAL;
  891. }
  892. partition->callback = callback;
  893. partition->cb_data = cb_data;
  894. write_unlock(&partition->rw_lock);
  895. return 0;
  896. }
  897. static int ffa_sched_recv_cb_register(struct ffa_device *dev,
  898. ffa_sched_recv_cb cb, void *cb_data)
  899. {
  900. return ffa_sched_recv_cb_update(dev->vm_id, cb, cb_data, true);
  901. }
  902. static int ffa_sched_recv_cb_unregister(struct ffa_device *dev)
  903. {
  904. return ffa_sched_recv_cb_update(dev->vm_id, NULL, NULL, false);
  905. }
  906. static int ffa_notification_bind(u16 dst_id, u64 bitmap, u32 flags)
  907. {
  908. return ffa_notification_bind_common(dst_id, bitmap, flags, true);
  909. }
  910. static int ffa_notification_unbind(u16 dst_id, u64 bitmap)
  911. {
  912. return ffa_notification_bind_common(dst_id, bitmap, 0, false);
  913. }
  914. /* Should be called while the notify_lock is taken */
  915. static struct notifier_cb_info *
  916. notifier_hash_node_get(u16 notify_id, enum notify_type type)
  917. {
  918. struct notifier_cb_info *node;
  919. hash_for_each_possible(drv_info->notifier_hash, node, hnode, notify_id)
  920. if (type == node->type)
  921. return node;
  922. return NULL;
  923. }
  924. static int update_notifier_cb(int notify_id, enum notify_type type,
  925. struct notifier_cb_info *cb)
  926. {
  927. struct notifier_cb_info *cb_info = NULL;
  928. bool cb_found, is_registration = !!cb;
  929. cb_info = notifier_hash_node_get(notify_id, type);
  930. cb_found = !!cb_info;
  931. if (!(is_registration ^ cb_found))
  932. return -EINVAL;
  933. if (is_registration) {
  934. hash_add(drv_info->notifier_hash, &cb->hnode, notify_id);
  935. } else {
  936. hash_del(&cb_info->hnode);
  937. kfree(cb_info);
  938. }
  939. return 0;
  940. }
  941. static enum notify_type ffa_notify_type_get(u16 vm_id)
  942. {
  943. if (vm_id & FFA_SECURE_PARTITION_ID_FLAG)
  944. return SECURE_PARTITION;
  945. else
  946. return NON_SECURE_VM;
  947. }
  948. static int ffa_notify_relinquish(struct ffa_device *dev, int notify_id)
  949. {
  950. int rc;
  951. enum notify_type type = ffa_notify_type_get(dev->vm_id);
  952. if (ffa_notifications_disabled())
  953. return -EOPNOTSUPP;
  954. if (notify_id >= FFA_MAX_NOTIFICATIONS)
  955. return -EINVAL;
  956. write_lock(&drv_info->notify_lock);
  957. rc = update_notifier_cb(notify_id, type, NULL);
  958. if (rc) {
  959. pr_err("Could not unregister notification callback\n");
  960. write_unlock(&drv_info->notify_lock);
  961. return rc;
  962. }
  963. rc = ffa_notification_unbind(dev->vm_id, BIT(notify_id));
  964. write_unlock(&drv_info->notify_lock);
  965. return rc;
  966. }
  967. static int ffa_notify_request(struct ffa_device *dev, bool is_per_vcpu,
  968. ffa_notifier_cb cb, void *cb_data, int notify_id)
  969. {
  970. int rc;
  971. u32 flags = 0;
  972. struct notifier_cb_info *cb_info = NULL;
  973. enum notify_type type = ffa_notify_type_get(dev->vm_id);
  974. if (ffa_notifications_disabled())
  975. return -EOPNOTSUPP;
  976. if (notify_id >= FFA_MAX_NOTIFICATIONS)
  977. return -EINVAL;
  978. cb_info = kzalloc(sizeof(*cb_info), GFP_KERNEL);
  979. if (!cb_info)
  980. return -ENOMEM;
  981. cb_info->type = type;
  982. cb_info->cb_data = cb_data;
  983. cb_info->cb = cb;
  984. write_lock(&drv_info->notify_lock);
  985. if (is_per_vcpu)
  986. flags = PER_VCPU_NOTIFICATION_FLAG;
  987. rc = ffa_notification_bind(dev->vm_id, BIT(notify_id), flags);
  988. if (rc)
  989. goto out_unlock_free;
  990. rc = update_notifier_cb(notify_id, type, cb_info);
  991. if (rc) {
  992. pr_err("Failed to register callback for %d - %d\n",
  993. notify_id, rc);
  994. ffa_notification_unbind(dev->vm_id, BIT(notify_id));
  995. }
  996. out_unlock_free:
  997. write_unlock(&drv_info->notify_lock);
  998. if (rc)
  999. kfree(cb_info);
  1000. return rc;
  1001. }
  1002. static int ffa_notify_send(struct ffa_device *dev, int notify_id,
  1003. bool is_per_vcpu, u16 vcpu)
  1004. {
  1005. u32 flags = 0;
  1006. if (ffa_notifications_disabled())
  1007. return -EOPNOTSUPP;
  1008. if (is_per_vcpu)
  1009. flags |= (PER_VCPU_NOTIFICATION_FLAG | vcpu << 16);
  1010. return ffa_notification_set(dev->vm_id, drv_info->vm_id, flags,
  1011. BIT(notify_id));
  1012. }
  1013. static void handle_notif_callbacks(u64 bitmap, enum notify_type type)
  1014. {
  1015. int notify_id;
  1016. struct notifier_cb_info *cb_info = NULL;
  1017. for (notify_id = 0; notify_id <= FFA_MAX_NOTIFICATIONS && bitmap;
  1018. notify_id++, bitmap >>= 1) {
  1019. if (!(bitmap & 1))
  1020. continue;
  1021. read_lock(&drv_info->notify_lock);
  1022. cb_info = notifier_hash_node_get(notify_id, type);
  1023. read_unlock(&drv_info->notify_lock);
  1024. if (cb_info && cb_info->cb)
  1025. cb_info->cb(notify_id, cb_info->cb_data);
  1026. }
  1027. }
  1028. static void notif_get_and_handle(void *unused)
  1029. {
  1030. int rc;
  1031. struct ffa_notify_bitmaps bitmaps;
  1032. rc = ffa_notification_get(SECURE_PARTITION_BITMAP |
  1033. SPM_FRAMEWORK_BITMAP, &bitmaps);
  1034. if (rc) {
  1035. pr_err("Failed to retrieve notifications with %d!\n", rc);
  1036. return;
  1037. }
  1038. handle_notif_callbacks(bitmaps.vm_map, NON_SECURE_VM);
  1039. handle_notif_callbacks(bitmaps.sp_map, SECURE_PARTITION);
  1040. handle_notif_callbacks(bitmaps.arch_map, FRAMEWORK);
  1041. }
  1042. static void
  1043. ffa_self_notif_handle(u16 vcpu, bool is_per_vcpu, void *cb_data)
  1044. {
  1045. struct ffa_drv_info *info = cb_data;
  1046. if (!is_per_vcpu)
  1047. notif_get_and_handle(info);
  1048. else
  1049. smp_call_function_single(vcpu, notif_get_and_handle, info, 0);
  1050. }
  1051. static void notif_pcpu_irq_work_fn(struct work_struct *work)
  1052. {
  1053. struct ffa_drv_info *info = container_of(work, struct ffa_drv_info,
  1054. notif_pcpu_work);
  1055. ffa_self_notif_handle(smp_processor_id(), true, info);
  1056. }
  1057. static const struct ffa_info_ops ffa_drv_info_ops = {
  1058. .api_version_get = ffa_api_version_get,
  1059. .partition_info_get = ffa_partition_info_get,
  1060. };
  1061. static const struct ffa_msg_ops ffa_drv_msg_ops = {
  1062. .mode_32bit_set = ffa_mode_32bit_set,
  1063. .sync_send_receive = ffa_sync_send_receive,
  1064. .indirect_send = ffa_indirect_msg_send,
  1065. .sync_send_receive2 = ffa_sync_send_receive2,
  1066. };
  1067. static const struct ffa_mem_ops ffa_drv_mem_ops = {
  1068. .memory_reclaim = ffa_memory_reclaim,
  1069. .memory_share = ffa_memory_share,
  1070. .memory_lend = ffa_memory_lend,
  1071. };
  1072. static const struct ffa_cpu_ops ffa_drv_cpu_ops = {
  1073. .run = ffa_run,
  1074. };
  1075. static const struct ffa_notifier_ops ffa_drv_notifier_ops = {
  1076. .sched_recv_cb_register = ffa_sched_recv_cb_register,
  1077. .sched_recv_cb_unregister = ffa_sched_recv_cb_unregister,
  1078. .notify_request = ffa_notify_request,
  1079. .notify_relinquish = ffa_notify_relinquish,
  1080. .notify_send = ffa_notify_send,
  1081. };
  1082. static const struct ffa_ops ffa_drv_ops = {
  1083. .info_ops = &ffa_drv_info_ops,
  1084. .msg_ops = &ffa_drv_msg_ops,
  1085. .mem_ops = &ffa_drv_mem_ops,
  1086. .cpu_ops = &ffa_drv_cpu_ops,
  1087. .notifier_ops = &ffa_drv_notifier_ops,
  1088. };
  1089. void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid)
  1090. {
  1091. int count, idx;
  1092. struct ffa_partition_info *pbuf, *tpbuf;
  1093. count = ffa_partition_probe(uuid, &pbuf);
  1094. if (count <= 0)
  1095. return;
  1096. for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++)
  1097. if (tpbuf->id == ffa_dev->vm_id)
  1098. uuid_copy(&ffa_dev->uuid, uuid);
  1099. kfree(pbuf);
  1100. }
  1101. static int
  1102. ffa_bus_notifier(struct notifier_block *nb, unsigned long action, void *data)
  1103. {
  1104. struct device *dev = data;
  1105. struct ffa_device *fdev = to_ffa_dev(dev);
  1106. if (action == BUS_NOTIFY_BIND_DRIVER) {
  1107. struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver);
  1108. const struct ffa_device_id *id_table = ffa_drv->id_table;
  1109. /*
  1110. * FF-A v1.1 provides UUID for each partition as part of the
  1111. * discovery API, the discovered UUID must be populated in the
  1112. * device's UUID and there is no need to workaround by copying
  1113. * the same from the driver table.
  1114. */
  1115. if (uuid_is_null(&fdev->uuid))
  1116. ffa_device_match_uuid(fdev, &id_table->uuid);
  1117. return NOTIFY_OK;
  1118. }
  1119. return NOTIFY_DONE;
  1120. }
  1121. static struct notifier_block ffa_bus_nb = {
  1122. .notifier_call = ffa_bus_notifier,
  1123. };
  1124. static int ffa_setup_partitions(void)
  1125. {
  1126. int count, idx, ret;
  1127. struct ffa_device *ffa_dev;
  1128. struct ffa_dev_part_info *info;
  1129. struct ffa_partition_info *pbuf, *tpbuf;
  1130. if (drv_info->version == FFA_VERSION_1_0) {
  1131. ret = bus_register_notifier(&ffa_bus_type, &ffa_bus_nb);
  1132. if (ret)
  1133. pr_err("Failed to register FF-A bus notifiers\n");
  1134. }
  1135. count = ffa_partition_probe(&uuid_null, &pbuf);
  1136. if (count <= 0) {
  1137. pr_info("%s: No partitions found, error %d\n", __func__, count);
  1138. return -EINVAL;
  1139. }
  1140. xa_init(&drv_info->partition_info);
  1141. for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) {
  1142. /* Note that if the UUID will be uuid_null, that will require
  1143. * ffa_bus_notifier() to find the UUID of this partition id
  1144. * with help of ffa_device_match_uuid(). FF-A v1.1 and above
  1145. * provides UUID here for each partition as part of the
  1146. * discovery API and the same is passed.
  1147. */
  1148. ffa_dev = ffa_device_register(tpbuf, &ffa_drv_ops);
  1149. if (!ffa_dev) {
  1150. pr_err("%s: failed to register partition ID 0x%x\n",
  1151. __func__, tpbuf->id);
  1152. continue;
  1153. }
  1154. if (drv_info->version > FFA_VERSION_1_0 &&
  1155. !(tpbuf->properties & FFA_PARTITION_AARCH64_EXEC))
  1156. ffa_mode_32bit_set(ffa_dev);
  1157. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1158. if (!info) {
  1159. ffa_device_unregister(ffa_dev);
  1160. continue;
  1161. }
  1162. rwlock_init(&info->rw_lock);
  1163. ret = xa_insert(&drv_info->partition_info, tpbuf->id,
  1164. info, GFP_KERNEL);
  1165. if (ret) {
  1166. pr_err("%s: failed to save partition ID 0x%x - ret:%d\n",
  1167. __func__, tpbuf->id, ret);
  1168. ffa_device_unregister(ffa_dev);
  1169. kfree(info);
  1170. }
  1171. }
  1172. kfree(pbuf);
  1173. /* Check if the host is already added as part of partition info */
  1174. if (xa_load(&drv_info->partition_info, drv_info->vm_id))
  1175. return 0;
  1176. /* Allocate for the host */
  1177. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1178. if (!info) {
  1179. /* Already registered devices are freed on bus_exit */
  1180. ffa_partitions_cleanup();
  1181. return -ENOMEM;
  1182. }
  1183. rwlock_init(&info->rw_lock);
  1184. ret = xa_insert(&drv_info->partition_info, drv_info->vm_id,
  1185. info, GFP_KERNEL);
  1186. if (ret) {
  1187. pr_err("%s: failed to save Host partition ID 0x%x - ret:%d. Abort.\n",
  1188. __func__, drv_info->vm_id, ret);
  1189. kfree(info);
  1190. /* Already registered devices are freed on bus_exit */
  1191. ffa_partitions_cleanup();
  1192. }
  1193. return ret;
  1194. }
  1195. static void ffa_partitions_cleanup(void)
  1196. {
  1197. struct ffa_dev_part_info *info;
  1198. unsigned long idx;
  1199. xa_for_each(&drv_info->partition_info, idx, info) {
  1200. xa_erase(&drv_info->partition_info, idx);
  1201. kfree(info);
  1202. }
  1203. xa_destroy(&drv_info->partition_info);
  1204. }
  1205. /* FFA FEATURE IDs */
  1206. #define FFA_FEAT_NOTIFICATION_PENDING_INT (1)
  1207. #define FFA_FEAT_SCHEDULE_RECEIVER_INT (2)
  1208. #define FFA_FEAT_MANAGED_EXIT_INT (3)
  1209. static irqreturn_t ffa_sched_recv_irq_handler(int irq, void *irq_data)
  1210. {
  1211. struct ffa_pcpu_irq *pcpu = irq_data;
  1212. struct ffa_drv_info *info = pcpu->info;
  1213. queue_work(info->notif_pcpu_wq, &info->sched_recv_irq_work);
  1214. return IRQ_HANDLED;
  1215. }
  1216. static irqreturn_t notif_pend_irq_handler(int irq, void *irq_data)
  1217. {
  1218. struct ffa_pcpu_irq *pcpu = irq_data;
  1219. struct ffa_drv_info *info = pcpu->info;
  1220. queue_work_on(smp_processor_id(), info->notif_pcpu_wq,
  1221. &info->notif_pcpu_work);
  1222. return IRQ_HANDLED;
  1223. }
  1224. static void ffa_sched_recv_irq_work_fn(struct work_struct *work)
  1225. {
  1226. ffa_notification_info_get();
  1227. }
  1228. static int ffa_irq_map(u32 id)
  1229. {
  1230. char *err_str;
  1231. int ret, irq, intid;
  1232. if (id == FFA_FEAT_NOTIFICATION_PENDING_INT)
  1233. err_str = "Notification Pending Interrupt";
  1234. else if (id == FFA_FEAT_SCHEDULE_RECEIVER_INT)
  1235. err_str = "Schedule Receiver Interrupt";
  1236. else
  1237. err_str = "Unknown ID";
  1238. /* The returned intid is assumed to be SGI donated to NS world */
  1239. ret = ffa_features(id, 0, &intid, NULL);
  1240. if (ret < 0) {
  1241. if (ret != -EOPNOTSUPP)
  1242. pr_err("Failed to retrieve FF-A %s %u\n", err_str, id);
  1243. return ret;
  1244. }
  1245. if (acpi_disabled) {
  1246. struct of_phandle_args oirq = {};
  1247. struct device_node *gic;
  1248. /* Only GICv3 supported currently with the device tree */
  1249. gic = of_find_compatible_node(NULL, NULL, "arm,gic-v3");
  1250. if (!gic)
  1251. return -ENXIO;
  1252. oirq.np = gic;
  1253. oirq.args_count = 1;
  1254. oirq.args[0] = intid;
  1255. irq = irq_create_of_mapping(&oirq);
  1256. of_node_put(gic);
  1257. #ifdef CONFIG_ACPI
  1258. } else {
  1259. irq = acpi_register_gsi(NULL, intid, ACPI_EDGE_SENSITIVE,
  1260. ACPI_ACTIVE_HIGH);
  1261. #endif
  1262. }
  1263. if (irq <= 0) {
  1264. pr_err("Failed to create IRQ mapping!\n");
  1265. return -ENODATA;
  1266. }
  1267. return irq;
  1268. }
  1269. static void ffa_irq_unmap(unsigned int irq)
  1270. {
  1271. if (!irq)
  1272. return;
  1273. irq_dispose_mapping(irq);
  1274. }
  1275. static int ffa_cpuhp_pcpu_irq_enable(unsigned int cpu)
  1276. {
  1277. if (drv_info->sched_recv_irq)
  1278. enable_percpu_irq(drv_info->sched_recv_irq, IRQ_TYPE_NONE);
  1279. if (drv_info->notif_pend_irq)
  1280. enable_percpu_irq(drv_info->notif_pend_irq, IRQ_TYPE_NONE);
  1281. return 0;
  1282. }
  1283. static int ffa_cpuhp_pcpu_irq_disable(unsigned int cpu)
  1284. {
  1285. if (drv_info->sched_recv_irq)
  1286. disable_percpu_irq(drv_info->sched_recv_irq);
  1287. if (drv_info->notif_pend_irq)
  1288. disable_percpu_irq(drv_info->notif_pend_irq);
  1289. return 0;
  1290. }
  1291. static void ffa_uninit_pcpu_irq(void)
  1292. {
  1293. if (drv_info->cpuhp_state) {
  1294. cpuhp_remove_state(drv_info->cpuhp_state);
  1295. drv_info->cpuhp_state = 0;
  1296. }
  1297. if (drv_info->notif_pcpu_wq) {
  1298. destroy_workqueue(drv_info->notif_pcpu_wq);
  1299. drv_info->notif_pcpu_wq = NULL;
  1300. }
  1301. if (drv_info->sched_recv_irq)
  1302. free_percpu_irq(drv_info->sched_recv_irq, drv_info->irq_pcpu);
  1303. if (drv_info->notif_pend_irq)
  1304. free_percpu_irq(drv_info->notif_pend_irq, drv_info->irq_pcpu);
  1305. if (drv_info->irq_pcpu) {
  1306. free_percpu(drv_info->irq_pcpu);
  1307. drv_info->irq_pcpu = NULL;
  1308. }
  1309. }
  1310. static int ffa_init_pcpu_irq(void)
  1311. {
  1312. struct ffa_pcpu_irq __percpu *irq_pcpu;
  1313. int ret, cpu;
  1314. irq_pcpu = alloc_percpu(struct ffa_pcpu_irq);
  1315. if (!irq_pcpu)
  1316. return -ENOMEM;
  1317. for_each_present_cpu(cpu)
  1318. per_cpu_ptr(irq_pcpu, cpu)->info = drv_info;
  1319. drv_info->irq_pcpu = irq_pcpu;
  1320. if (drv_info->sched_recv_irq) {
  1321. ret = request_percpu_irq(drv_info->sched_recv_irq,
  1322. ffa_sched_recv_irq_handler,
  1323. "ARM-FFA-SRI", irq_pcpu);
  1324. if (ret) {
  1325. pr_err("Error registering percpu SRI nIRQ %d : %d\n",
  1326. drv_info->sched_recv_irq, ret);
  1327. drv_info->sched_recv_irq = 0;
  1328. return ret;
  1329. }
  1330. }
  1331. if (drv_info->notif_pend_irq) {
  1332. ret = request_percpu_irq(drv_info->notif_pend_irq,
  1333. notif_pend_irq_handler,
  1334. "ARM-FFA-NPI", irq_pcpu);
  1335. if (ret) {
  1336. pr_err("Error registering percpu NPI nIRQ %d : %d\n",
  1337. drv_info->notif_pend_irq, ret);
  1338. drv_info->notif_pend_irq = 0;
  1339. return ret;
  1340. }
  1341. }
  1342. INIT_WORK(&drv_info->sched_recv_irq_work, ffa_sched_recv_irq_work_fn);
  1343. INIT_WORK(&drv_info->notif_pcpu_work, notif_pcpu_irq_work_fn);
  1344. drv_info->notif_pcpu_wq = create_workqueue("ffa_pcpu_irq_notification");
  1345. if (!drv_info->notif_pcpu_wq)
  1346. return -EINVAL;
  1347. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "ffa/pcpu-irq:starting",
  1348. ffa_cpuhp_pcpu_irq_enable,
  1349. ffa_cpuhp_pcpu_irq_disable);
  1350. if (ret < 0)
  1351. return ret;
  1352. drv_info->cpuhp_state = ret;
  1353. return 0;
  1354. }
  1355. static void ffa_notifications_cleanup(void)
  1356. {
  1357. ffa_uninit_pcpu_irq();
  1358. ffa_irq_unmap(drv_info->sched_recv_irq);
  1359. drv_info->sched_recv_irq = 0;
  1360. ffa_irq_unmap(drv_info->notif_pend_irq);
  1361. drv_info->notif_pend_irq = 0;
  1362. if (drv_info->bitmap_created) {
  1363. ffa_notification_bitmap_destroy();
  1364. drv_info->bitmap_created = false;
  1365. }
  1366. drv_info->notif_enabled = false;
  1367. }
  1368. static void ffa_notifications_setup(void)
  1369. {
  1370. int ret;
  1371. ret = ffa_features(FFA_NOTIFICATION_BITMAP_CREATE, 0, NULL, NULL);
  1372. if (!ret) {
  1373. ret = ffa_notification_bitmap_create();
  1374. if (ret) {
  1375. pr_err("Notification bitmap create error %d\n", ret);
  1376. return;
  1377. }
  1378. drv_info->bitmap_created = true;
  1379. }
  1380. ret = ffa_irq_map(FFA_FEAT_SCHEDULE_RECEIVER_INT);
  1381. if (ret > 0)
  1382. drv_info->sched_recv_irq = ret;
  1383. ret = ffa_irq_map(FFA_FEAT_NOTIFICATION_PENDING_INT);
  1384. if (ret > 0)
  1385. drv_info->notif_pend_irq = ret;
  1386. if (!drv_info->sched_recv_irq && !drv_info->notif_pend_irq)
  1387. goto cleanup;
  1388. ret = ffa_init_pcpu_irq();
  1389. if (ret)
  1390. goto cleanup;
  1391. hash_init(drv_info->notifier_hash);
  1392. rwlock_init(&drv_info->notify_lock);
  1393. drv_info->notif_enabled = true;
  1394. return;
  1395. cleanup:
  1396. pr_info("Notification setup failed %d, not enabled\n", ret);
  1397. ffa_notifications_cleanup();
  1398. }
  1399. static int __init ffa_init(void)
  1400. {
  1401. int ret;
  1402. u32 buf_sz;
  1403. size_t rxtx_bufsz = SZ_4K;
  1404. ret = ffa_transport_init(&invoke_ffa_fn);
  1405. if (ret)
  1406. return ret;
  1407. drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL);
  1408. if (!drv_info)
  1409. return -ENOMEM;
  1410. ret = ffa_version_check(&drv_info->version);
  1411. if (ret)
  1412. goto free_drv_info;
  1413. if (ffa_id_get(&drv_info->vm_id)) {
  1414. pr_err("failed to obtain VM id for self\n");
  1415. ret = -ENODEV;
  1416. goto free_drv_info;
  1417. }
  1418. ret = ffa_features(FFA_FN_NATIVE(RXTX_MAP), 0, &buf_sz, NULL);
  1419. if (!ret) {
  1420. if (RXTX_MAP_MIN_BUFSZ(buf_sz) == 1)
  1421. rxtx_bufsz = SZ_64K;
  1422. else if (RXTX_MAP_MIN_BUFSZ(buf_sz) == 2)
  1423. rxtx_bufsz = SZ_16K;
  1424. else
  1425. rxtx_bufsz = SZ_4K;
  1426. }
  1427. drv_info->rxtx_bufsz = rxtx_bufsz;
  1428. drv_info->rx_buffer = alloc_pages_exact(rxtx_bufsz, GFP_KERNEL);
  1429. if (!drv_info->rx_buffer) {
  1430. ret = -ENOMEM;
  1431. goto free_pages;
  1432. }
  1433. drv_info->tx_buffer = alloc_pages_exact(rxtx_bufsz, GFP_KERNEL);
  1434. if (!drv_info->tx_buffer) {
  1435. ret = -ENOMEM;
  1436. goto free_pages;
  1437. }
  1438. ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
  1439. virt_to_phys(drv_info->rx_buffer),
  1440. rxtx_bufsz / FFA_PAGE_SIZE);
  1441. if (ret) {
  1442. pr_err("failed to register FFA RxTx buffers\n");
  1443. goto free_pages;
  1444. }
  1445. mutex_init(&drv_info->rx_lock);
  1446. mutex_init(&drv_info->tx_lock);
  1447. ffa_drvinfo_flags_init();
  1448. ffa_notifications_setup();
  1449. ret = ffa_setup_partitions();
  1450. if (ret) {
  1451. pr_err("failed to setup partitions\n");
  1452. goto cleanup_notifs;
  1453. }
  1454. ret = ffa_sched_recv_cb_update(drv_info->vm_id, ffa_self_notif_handle,
  1455. drv_info, true);
  1456. if (ret)
  1457. pr_info("Failed to register driver sched callback %d\n", ret);
  1458. return 0;
  1459. cleanup_notifs:
  1460. ffa_notifications_cleanup();
  1461. free_pages:
  1462. if (drv_info->tx_buffer)
  1463. free_pages_exact(drv_info->tx_buffer, rxtx_bufsz);
  1464. free_pages_exact(drv_info->rx_buffer, rxtx_bufsz);
  1465. free_drv_info:
  1466. kfree(drv_info);
  1467. return ret;
  1468. }
  1469. rootfs_initcall(ffa_init);
  1470. static void __exit ffa_exit(void)
  1471. {
  1472. ffa_notifications_cleanup();
  1473. ffa_partitions_cleanup();
  1474. ffa_rxtx_unmap(drv_info->vm_id);
  1475. free_pages_exact(drv_info->tx_buffer, drv_info->rxtx_bufsz);
  1476. free_pages_exact(drv_info->rx_buffer, drv_info->rxtx_bufsz);
  1477. kfree(drv_info);
  1478. }
  1479. module_exit(ffa_exit);
  1480. MODULE_ALIAS("arm-ffa");
  1481. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  1482. MODULE_DESCRIPTION("Arm FF-A interface driver");
  1483. MODULE_LICENSE("GPL v2");