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
22 #define DRIVER_NAME "ARM FF-A"
23 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
24
25 #include <linux/acpi.h>
26 #include <linux/arm_ffa.h>
27 #include <linux/bitfield.h>
28 #include <linux/cpuhotplug.h>
29 #include <linux/delay.h>
30 #include <linux/device.h>
31 #include <linux/hashtable.h>
32 #include <linux/interrupt.h>
33 #include <linux/io.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/mm.h>
37 #include <linux/mutex.h>
38 #include <linux/of_irq.h>
39 #include <linux/scatterlist.h>
40 #include <linux/slab.h>
41 #include <linux/smp.h>
42 #include <linux/uuid.h>
43 #include <linux/xarray.h>
44
45 #include "common.h"
46
47 #define FFA_DRIVER_VERSION FFA_VERSION_1_2
48 #define FFA_MIN_VERSION FFA_VERSION_1_0
49
50 #define SENDER_ID_MASK GENMASK(31, 16)
51 #define RECEIVER_ID_MASK GENMASK(15, 0)
52 #define SENDER_ID(x) ((u16)(FIELD_GET(SENDER_ID_MASK, (x))))
53 #define RECEIVER_ID(x) ((u16)(FIELD_GET(RECEIVER_ID_MASK, (x))))
54 #define PACK_TARGET_INFO(s, r) \
55 (FIELD_PREP(SENDER_ID_MASK, (s)) | FIELD_PREP(RECEIVER_ID_MASK, (r)))
56
57 #define RXTX_MAP_MIN_BUFSZ_MASK GENMASK(1, 0)
58 #define RXTX_MAP_MIN_BUFSZ(x) ((x) & RXTX_MAP_MIN_BUFSZ_MASK)
59
60 #define FFA_MAX_NOTIFICATIONS 64
61
62 static ffa_fn *invoke_ffa_fn;
63
64 static const int ffa_linux_errmap[] = {
65 /* better than switch case as long as return value is continuous */
66 0, /* FFA_RET_SUCCESS */
67 -EOPNOTSUPP, /* FFA_RET_NOT_SUPPORTED */
68 -EINVAL, /* FFA_RET_INVALID_PARAMETERS */
69 -ENOMEM, /* FFA_RET_NO_MEMORY */
70 -EBUSY, /* FFA_RET_BUSY */
71 -EINTR, /* FFA_RET_INTERRUPTED */
72 -EACCES, /* FFA_RET_DENIED */
73 -EAGAIN, /* FFA_RET_RETRY */
74 -ECANCELED, /* FFA_RET_ABORTED */
75 -ENODATA, /* FFA_RET_NO_DATA */
76 -EAGAIN, /* FFA_RET_NOT_READY */
77 };
78
ffa_to_linux_errno(int errno)79 static inline int ffa_to_linux_errno(int errno)
80 {
81 int err_idx = -errno;
82
83 if (err_idx >= 0 && err_idx < ARRAY_SIZE(ffa_linux_errmap))
84 return ffa_linux_errmap[err_idx];
85 return -EINVAL;
86 }
87
88 struct ffa_pcpu_irq {
89 struct ffa_drv_info *info;
90 };
91
92 struct ffa_drv_info {
93 u32 version;
94 u16 vm_id;
95 struct mutex rx_lock; /* lock to protect Rx buffer */
96 struct mutex tx_lock; /* lock to protect Tx buffer */
97 void *rx_buffer;
98 void *tx_buffer;
99 size_t rxtx_bufsz;
100 bool mem_ops_native;
101 bool msg_direct_req2_supp;
102 bool bitmap_created;
103 bool notif_enabled;
104 unsigned int sched_recv_irq;
105 unsigned int notif_pend_irq;
106 unsigned int cpuhp_state;
107 struct ffa_pcpu_irq __percpu *irq_pcpu;
108 struct workqueue_struct *notif_pcpu_wq;
109 struct work_struct notif_pcpu_work;
110 struct work_struct sched_recv_irq_work;
111 struct xarray partition_info;
112 DECLARE_HASHTABLE(notifier_hash, ilog2(FFA_MAX_NOTIFICATIONS));
113 rwlock_t notify_lock; /* lock to protect notifier hashtable */
114 };
115
116 static struct ffa_drv_info *drv_info;
117
118 /*
119 * The driver must be able to support all the versions from the earliest
120 * supported FFA_MIN_VERSION to the latest supported FFA_DRIVER_VERSION.
121 * The specification states that if firmware supports a FFA implementation
122 * that is incompatible with and at a greater version number than specified
123 * by the caller(FFA_DRIVER_VERSION passed as parameter to FFA_VERSION),
124 * it must return the NOT_SUPPORTED error code.
125 */
ffa_compatible_version_find(u32 version)126 static u32 ffa_compatible_version_find(u32 version)
127 {
128 u16 major = FFA_MAJOR_VERSION(version), minor = FFA_MINOR_VERSION(version);
129 u16 drv_major = FFA_MAJOR_VERSION(FFA_DRIVER_VERSION);
130 u16 drv_minor = FFA_MINOR_VERSION(FFA_DRIVER_VERSION);
131
132 if ((major < drv_major) || (major == drv_major && minor <= drv_minor))
133 return version;
134
135 pr_info("Firmware version higher than driver version, downgrading\n");
136 return FFA_DRIVER_VERSION;
137 }
138
ffa_version_check(u32 * version)139 static int ffa_version_check(u32 *version)
140 {
141 ffa_value_t ver;
142
143 invoke_ffa_fn((ffa_value_t){
144 .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION,
145 }, &ver);
146
147 if ((s32)ver.a0 == FFA_RET_NOT_SUPPORTED) {
148 pr_info("FFA_VERSION returned not supported\n");
149 return -EOPNOTSUPP;
150 }
151
152 if (FFA_MAJOR_VERSION(ver.a0) > FFA_MAJOR_VERSION(FFA_DRIVER_VERSION)) {
153 pr_err("Incompatible v%d.%d! Latest supported v%d.%d\n",
154 FFA_MAJOR_VERSION(ver.a0), FFA_MINOR_VERSION(ver.a0),
155 FFA_MAJOR_VERSION(FFA_DRIVER_VERSION),
156 FFA_MINOR_VERSION(FFA_DRIVER_VERSION));
157 return -EINVAL;
158 }
159
160 if (ver.a0 < FFA_MIN_VERSION) {
161 pr_err("Incompatible v%d.%d! Earliest supported v%d.%d\n",
162 FFA_MAJOR_VERSION(ver.a0), FFA_MINOR_VERSION(ver.a0),
163 FFA_MAJOR_VERSION(FFA_MIN_VERSION),
164 FFA_MINOR_VERSION(FFA_MIN_VERSION));
165 return -EINVAL;
166 }
167
168 pr_info("Driver version %d.%d\n", FFA_MAJOR_VERSION(FFA_DRIVER_VERSION),
169 FFA_MINOR_VERSION(FFA_DRIVER_VERSION));
170 pr_info("Firmware version %d.%d found\n", FFA_MAJOR_VERSION(ver.a0),
171 FFA_MINOR_VERSION(ver.a0));
172 *version = ffa_compatible_version_find(ver.a0);
173
174 return 0;
175 }
176
ffa_rx_release(void)177 static int ffa_rx_release(void)
178 {
179 ffa_value_t ret;
180
181 invoke_ffa_fn((ffa_value_t){
182 .a0 = FFA_RX_RELEASE,
183 }, &ret);
184
185 if (ret.a0 == FFA_ERROR)
186 return ffa_to_linux_errno((int)ret.a2);
187
188 /* check for ret.a0 == FFA_RX_RELEASE ? */
189
190 return 0;
191 }
192
ffa_rxtx_map(phys_addr_t tx_buf,phys_addr_t rx_buf,u32 pg_cnt)193 static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt)
194 {
195 ffa_value_t ret;
196
197 invoke_ffa_fn((ffa_value_t){
198 .a0 = FFA_FN_NATIVE(RXTX_MAP),
199 .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt,
200 }, &ret);
201
202 if (ret.a0 == FFA_ERROR)
203 return ffa_to_linux_errno((int)ret.a2);
204
205 return 0;
206 }
207
ffa_rxtx_unmap(u16 vm_id)208 static int ffa_rxtx_unmap(u16 vm_id)
209 {
210 ffa_value_t ret;
211
212 invoke_ffa_fn((ffa_value_t){
213 .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0),
214 }, &ret);
215
216 if (ret.a0 == FFA_ERROR)
217 return ffa_to_linux_errno((int)ret.a2);
218
219 return 0;
220 }
221
ffa_features(u32 func_feat_id,u32 input_props,u32 * if_props_1,u32 * if_props_2)222 static int ffa_features(u32 func_feat_id, u32 input_props,
223 u32 *if_props_1, u32 *if_props_2)
224 {
225 ffa_value_t id;
226
227 if (!ARM_SMCCC_IS_FAST_CALL(func_feat_id) && input_props) {
228 pr_err("%s: Invalid Parameters: %x, %x", __func__,
229 func_feat_id, input_props);
230 return ffa_to_linux_errno(FFA_RET_INVALID_PARAMETERS);
231 }
232
233 invoke_ffa_fn((ffa_value_t){
234 .a0 = FFA_FEATURES, .a1 = func_feat_id, .a2 = input_props,
235 }, &id);
236
237 if (id.a0 == FFA_ERROR)
238 return ffa_to_linux_errno((int)id.a2);
239
240 if (if_props_1)
241 *if_props_1 = id.a2;
242 if (if_props_2)
243 *if_props_2 = id.a3;
244
245 return 0;
246 }
247
248 #define PARTITION_INFO_GET_RETURN_COUNT_ONLY BIT(0)
249 #define FFA_SUPPORTS_GET_COUNT_ONLY(version) ((version) > FFA_VERSION_1_0)
250 #define FFA_PART_INFO_HAS_SIZE_IN_RESP(version) ((version) > FFA_VERSION_1_0)
251 #define FFA_PART_INFO_HAS_UUID_IN_RESP(version) ((version) > FFA_VERSION_1_0)
252 #define FFA_PART_INFO_HAS_EXEC_STATE_IN_RESP(version) \
253 ((version) > FFA_VERSION_1_0)
254
255 /* buffer must be sizeof(struct ffa_partition_info) * num_partitions */
256 static int
__ffa_partition_info_get(u32 uuid0,u32 uuid1,u32 uuid2,u32 uuid3,struct ffa_partition_info * buffer,int num_partitions)257 __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
258 struct ffa_partition_info *buffer, int num_partitions)
259 {
260 int idx, count, flags = 0, sz, buf_sz;
261 ffa_value_t partition_info;
262
263 if (FFA_SUPPORTS_GET_COUNT_ONLY(drv_info->version) &&
264 (!buffer || !num_partitions)) /* Just get the count for now */
265 flags = PARTITION_INFO_GET_RETURN_COUNT_ONLY;
266
267 mutex_lock(&drv_info->rx_lock);
268 invoke_ffa_fn((ffa_value_t){
269 .a0 = FFA_PARTITION_INFO_GET,
270 .a1 = uuid0, .a2 = uuid1, .a3 = uuid2, .a4 = uuid3,
271 .a5 = flags,
272 }, &partition_info);
273
274 if (partition_info.a0 == FFA_ERROR) {
275 mutex_unlock(&drv_info->rx_lock);
276 return ffa_to_linux_errno((int)partition_info.a2);
277 }
278
279 count = partition_info.a2;
280
281 if (FFA_PART_INFO_HAS_SIZE_IN_RESP(drv_info->version)) {
282 buf_sz = sz = partition_info.a3;
283 if (sz > sizeof(*buffer))
284 buf_sz = sizeof(*buffer);
285 } else {
286 buf_sz = sz = 8;
287 }
288
289 if (buffer && count <= num_partitions)
290 for (idx = 0; idx < count; idx++) {
291 struct ffa_partition_info_le {
292 __le16 id;
293 __le16 exec_ctxt;
294 __le32 properties;
295 uuid_t uuid;
296 } *rx_buf = drv_info->rx_buffer + idx * sz;
297 struct ffa_partition_info *buf = buffer + idx;
298
299 buf->id = le16_to_cpu(rx_buf->id);
300 buf->exec_ctxt = le16_to_cpu(rx_buf->exec_ctxt);
301 buf->properties = le32_to_cpu(rx_buf->properties);
302 if (buf_sz > 8)
303 import_uuid(&buf->uuid, (u8 *)&rx_buf->uuid);
304 }
305
306 if (!(flags & PARTITION_INFO_GET_RETURN_COUNT_ONLY))
307 ffa_rx_release();
308
309 mutex_unlock(&drv_info->rx_lock);
310
311 return count;
312 }
313
314 #define LAST_INDEX_MASK GENMASK(15, 0)
315 #define CURRENT_INDEX_MASK GENMASK(31, 16)
316 #define UUID_INFO_TAG_MASK GENMASK(47, 32)
317 #define PARTITION_INFO_SZ_MASK GENMASK(63, 48)
318 #define PARTITION_COUNT(x) ((u16)(FIELD_GET(LAST_INDEX_MASK, (x))) + 1)
319 #define CURRENT_INDEX(x) ((u16)(FIELD_GET(CURRENT_INDEX_MASK, (x))))
320 #define UUID_INFO_TAG(x) ((u16)(FIELD_GET(UUID_INFO_TAG_MASK, (x))))
321 #define PARTITION_INFO_SZ(x) ((u16)(FIELD_GET(PARTITION_INFO_SZ_MASK, (x))))
322 #define PART_INFO_ID_MASK GENMASK(15, 0)
323 #define PART_INFO_EXEC_CXT_MASK GENMASK(31, 16)
324 #define PART_INFO_PROPS_MASK GENMASK(63, 32)
325 #define PART_INFO_ID(x) ((u16)(FIELD_GET(PART_INFO_ID_MASK, (x))))
326 #define PART_INFO_EXEC_CXT(x) ((u16)(FIELD_GET(PART_INFO_EXEC_CXT_MASK, (x))))
327 #define PART_INFO_PROPERTIES(x) ((u32)(FIELD_GET(PART_INFO_PROPS_MASK, (x))))
328 static int
__ffa_partition_info_get_regs(u32 uuid0,u32 uuid1,u32 uuid2,u32 uuid3,struct ffa_partition_info * buffer,int num_parts)329 __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
330 struct ffa_partition_info *buffer, int num_parts)
331 {
332 u16 buf_sz, start_idx, cur_idx, count = 0, prev_idx = 0, tag = 0;
333 struct ffa_partition_info *buf = buffer;
334 ffa_value_t partition_info;
335
336 do {
337 __le64 *regs;
338 int idx;
339
340 start_idx = prev_idx ? prev_idx + 1 : 0;
341
342 invoke_ffa_fn((ffa_value_t){
343 .a0 = FFA_PARTITION_INFO_GET_REGS,
344 .a1 = (u64)uuid1 << 32 | uuid0,
345 .a2 = (u64)uuid3 << 32 | uuid2,
346 .a3 = start_idx | tag << 16,
347 }, &partition_info);
348
349 if (partition_info.a0 == FFA_ERROR)
350 return ffa_to_linux_errno((int)partition_info.a2);
351
352 if (!count)
353 count = PARTITION_COUNT(partition_info.a2);
354 if (!buffer || !num_parts) /* count only */
355 return count;
356
357 cur_idx = CURRENT_INDEX(partition_info.a2);
358 tag = UUID_INFO_TAG(partition_info.a2);
359 buf_sz = PARTITION_INFO_SZ(partition_info.a2);
360 if (buf_sz > sizeof(*buffer))
361 buf_sz = sizeof(*buffer);
362
363 regs = (void *)&partition_info.a3;
364 for (idx = 0; idx < cur_idx - start_idx + 1; idx++, buf++) {
365 union {
366 uuid_t uuid;
367 u64 regs[2];
368 } uuid_regs = {
369 .regs = {
370 le64_to_cpu(*(regs + 1)),
371 le64_to_cpu(*(regs + 2)),
372 }
373 };
374 u64 val = *(u64 *)regs;
375
376 buf->id = PART_INFO_ID(val);
377 buf->exec_ctxt = PART_INFO_EXEC_CXT(val);
378 buf->properties = PART_INFO_PROPERTIES(val);
379 uuid_copy(&buf->uuid, &uuid_regs.uuid);
380 regs += 3;
381 }
382 prev_idx = cur_idx;
383
384 } while (cur_idx < (count - 1));
385
386 return count;
387 }
388
389 /* buffer is allocated and caller must free the same if returned count > 0 */
390 static int
ffa_partition_probe(const uuid_t * uuid,struct ffa_partition_info ** buffer)391 ffa_partition_probe(const uuid_t *uuid, struct ffa_partition_info **buffer)
392 {
393 int count;
394 u32 uuid0_4[4];
395 bool reg_mode = false;
396 struct ffa_partition_info *pbuf;
397
398 if (!ffa_features(FFA_PARTITION_INFO_GET_REGS, 0, NULL, NULL))
399 reg_mode = true;
400
401 export_uuid((u8 *)uuid0_4, uuid);
402 if (reg_mode)
403 count = __ffa_partition_info_get_regs(uuid0_4[0], uuid0_4[1],
404 uuid0_4[2], uuid0_4[3],
405 NULL, 0);
406 else
407 count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1],
408 uuid0_4[2], uuid0_4[3],
409 NULL, 0);
410 if (count <= 0)
411 return count;
412
413 pbuf = kzalloc_objs(*pbuf, count);
414 if (!pbuf)
415 return -ENOMEM;
416
417 if (reg_mode)
418 count = __ffa_partition_info_get_regs(uuid0_4[0], uuid0_4[1],
419 uuid0_4[2], uuid0_4[3],
420 pbuf, count);
421 else
422 count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1],
423 uuid0_4[2], uuid0_4[3],
424 pbuf, count);
425 if (count <= 0)
426 kfree(pbuf);
427 else
428 *buffer = pbuf;
429
430 return count;
431 }
432
433 #define VM_ID_MASK GENMASK(15, 0)
ffa_id_get(u16 * vm_id)434 static int ffa_id_get(u16 *vm_id)
435 {
436 ffa_value_t id;
437
438 invoke_ffa_fn((ffa_value_t){
439 .a0 = FFA_ID_GET,
440 }, &id);
441
442 if (id.a0 == FFA_ERROR)
443 return ffa_to_linux_errno((int)id.a2);
444
445 *vm_id = FIELD_GET(VM_ID_MASK, (id.a2));
446
447 return 0;
448 }
449
ffa_msg_send_wait_for_completion(ffa_value_t * ret)450 static inline void ffa_msg_send_wait_for_completion(ffa_value_t *ret)
451 {
452 while (ret->a0 == FFA_INTERRUPT || ret->a0 == FFA_YIELD) {
453 if (ret->a0 == FFA_YIELD)
454 fsleep(1000);
455
456 invoke_ffa_fn((ffa_value_t){
457 .a0 = FFA_RUN, .a1 = ret->a1,
458 }, ret);
459 }
460 }
461
ffa_msg_send_direct_req(u16 src_id,u16 dst_id,bool mode_32bit,struct ffa_send_direct_data * data)462 static int ffa_msg_send_direct_req(u16 src_id, u16 dst_id, bool mode_32bit,
463 struct ffa_send_direct_data *data)
464 {
465 u32 req_id, resp_id, src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
466 ffa_value_t ret;
467
468 if (mode_32bit) {
469 req_id = FFA_MSG_SEND_DIRECT_REQ;
470 resp_id = FFA_MSG_SEND_DIRECT_RESP;
471 } else {
472 req_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_REQ);
473 resp_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_RESP);
474 }
475
476 invoke_ffa_fn((ffa_value_t){
477 .a0 = req_id, .a1 = src_dst_ids, .a2 = 0,
478 .a3 = data->data0, .a4 = data->data1, .a5 = data->data2,
479 .a6 = data->data3, .a7 = data->data4,
480 }, &ret);
481
482 ffa_msg_send_wait_for_completion(&ret);
483
484 if (ret.a0 == FFA_ERROR)
485 return ffa_to_linux_errno((int)ret.a2);
486
487 if (ret.a0 == resp_id) {
488 data->data0 = ret.a3;
489 data->data1 = ret.a4;
490 data->data2 = ret.a5;
491 data->data3 = ret.a6;
492 data->data4 = ret.a7;
493 return 0;
494 }
495
496 return -EINVAL;
497 }
498
ffa_msg_send2(struct ffa_device * dev,u16 src_id,void * buf,size_t sz)499 static int ffa_msg_send2(struct ffa_device *dev, u16 src_id, void *buf, size_t sz)
500 {
501 u32 src_dst_ids = PACK_TARGET_INFO(src_id, dev->vm_id);
502 struct ffa_indirect_msg_hdr *msg;
503 ffa_value_t ret;
504 int retval = 0;
505
506 if (sz > (drv_info->rxtx_bufsz - sizeof(*msg)))
507 return -ERANGE;
508
509 mutex_lock(&drv_info->tx_lock);
510
511 msg = drv_info->tx_buffer;
512 msg->flags = 0;
513 msg->res0 = 0;
514 msg->offset = sizeof(*msg);
515 msg->send_recv_id = src_dst_ids;
516 msg->size = sz;
517 uuid_copy(&msg->uuid, &dev->uuid);
518 memcpy((u8 *)msg + msg->offset, buf, sz);
519
520 /* flags = 0, sender VMID = 0 works for both physical/virtual NS */
521 invoke_ffa_fn((ffa_value_t){
522 .a0 = FFA_MSG_SEND2, .a1 = 0, .a2 = 0
523 }, &ret);
524
525 if (ret.a0 == FFA_ERROR)
526 retval = ffa_to_linux_errno((int)ret.a2);
527
528 mutex_unlock(&drv_info->tx_lock);
529 return retval;
530 }
531
ffa_msg_send_direct_req2(u16 src_id,u16 dst_id,const uuid_t * uuid,struct ffa_send_direct_data2 * data)532 static int ffa_msg_send_direct_req2(u16 src_id, u16 dst_id, const uuid_t *uuid,
533 struct ffa_send_direct_data2 *data)
534 {
535 u32 src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
536 union {
537 uuid_t uuid;
538 __le64 regs[2];
539 } uuid_regs = { .uuid = *uuid };
540 ffa_value_t ret, args = {
541 .a0 = FFA_MSG_SEND_DIRECT_REQ2,
542 .a1 = src_dst_ids,
543 .a2 = le64_to_cpu(uuid_regs.regs[0]),
544 .a3 = le64_to_cpu(uuid_regs.regs[1]),
545 };
546 memcpy((void *)&args + offsetof(ffa_value_t, a4), data, sizeof(*data));
547
548 invoke_ffa_fn(args, &ret);
549
550 ffa_msg_send_wait_for_completion(&ret);
551
552 if (ret.a0 == FFA_ERROR)
553 return ffa_to_linux_errno((int)ret.a2);
554
555 if (ret.a0 == FFA_MSG_SEND_DIRECT_RESP2) {
556 memcpy(data, (void *)&ret + offsetof(ffa_value_t, a4), sizeof(*data));
557 return 0;
558 }
559
560 return -EINVAL;
561 }
562
ffa_mem_first_frag(u32 func_id,phys_addr_t buf,u32 buf_sz,u32 frag_len,u32 len,u64 * handle)563 static int ffa_mem_first_frag(u32 func_id, phys_addr_t buf, u32 buf_sz,
564 u32 frag_len, u32 len, u64 *handle)
565 {
566 ffa_value_t ret;
567
568 invoke_ffa_fn((ffa_value_t){
569 .a0 = func_id, .a1 = len, .a2 = frag_len,
570 .a3 = buf, .a4 = buf_sz,
571 }, &ret);
572
573 while (ret.a0 == FFA_MEM_OP_PAUSE)
574 invoke_ffa_fn((ffa_value_t){
575 .a0 = FFA_MEM_OP_RESUME,
576 .a1 = ret.a1, .a2 = ret.a2,
577 }, &ret);
578
579 if (ret.a0 == FFA_ERROR)
580 return ffa_to_linux_errno((int)ret.a2);
581
582 if (ret.a0 == FFA_SUCCESS) {
583 if (handle)
584 *handle = PACK_HANDLE(ret.a2, ret.a3);
585 } else if (ret.a0 == FFA_MEM_FRAG_RX) {
586 if (handle)
587 *handle = PACK_HANDLE(ret.a1, ret.a2);
588 } else {
589 return -EOPNOTSUPP;
590 }
591
592 return frag_len;
593 }
594
ffa_mem_next_frag(u64 handle,u32 frag_len)595 static int ffa_mem_next_frag(u64 handle, u32 frag_len)
596 {
597 ffa_value_t ret;
598
599 invoke_ffa_fn((ffa_value_t){
600 .a0 = FFA_MEM_FRAG_TX,
601 .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle),
602 .a3 = frag_len,
603 }, &ret);
604
605 while (ret.a0 == FFA_MEM_OP_PAUSE)
606 invoke_ffa_fn((ffa_value_t){
607 .a0 = FFA_MEM_OP_RESUME,
608 .a1 = ret.a1, .a2 = ret.a2,
609 }, &ret);
610
611 if (ret.a0 == FFA_ERROR)
612 return ffa_to_linux_errno((int)ret.a2);
613
614 if (ret.a0 == FFA_MEM_FRAG_RX)
615 return ret.a3;
616 else if (ret.a0 == FFA_SUCCESS)
617 return 0;
618
619 return -EOPNOTSUPP;
620 }
621
622 static int
ffa_transmit_fragment(u32 func_id,phys_addr_t buf,u32 buf_sz,u32 frag_len,u32 len,u64 * handle,bool first)623 ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len,
624 u32 len, u64 *handle, bool first)
625 {
626 if (!first)
627 return ffa_mem_next_frag(*handle, frag_len);
628
629 return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle);
630 }
631
ffa_get_num_pages_sg(struct scatterlist * sg)632 static u32 ffa_get_num_pages_sg(struct scatterlist *sg)
633 {
634 u32 num_pages = 0;
635
636 do {
637 num_pages += sg->length / FFA_PAGE_SIZE;
638 } while ((sg = sg_next(sg)));
639
640 return num_pages;
641 }
642
ffa_memory_attributes_get(u32 func_id)643 static u16 ffa_memory_attributes_get(u32 func_id)
644 {
645 /*
646 * For the memory lend or donate operation, if the receiver is a PE or
647 * a proxy endpoint, the owner/sender must not specify the attributes
648 */
649 if (func_id == FFA_FN_NATIVE(MEM_LEND) ||
650 func_id == FFA_MEM_LEND)
651 return 0;
652
653 return FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK | FFA_MEM_INNER_SHAREABLE;
654 }
655
ffa_emad_impdef_value_init(u32 version,void * dst,void * src)656 static void ffa_emad_impdef_value_init(u32 version, void *dst, void *src)
657 {
658 struct ffa_mem_region_attributes *ep_mem_access;
659
660 if (FFA_EMAD_HAS_IMPDEF_FIELD(version))
661 memcpy(dst, src, sizeof(ep_mem_access->impdef_val));
662 }
663
664 static void
ffa_mem_region_additional_setup(u32 version,struct ffa_mem_region * mem_region)665 ffa_mem_region_additional_setup(u32 version, struct ffa_mem_region *mem_region)
666 {
667 if (!FFA_MEM_REGION_HAS_EP_MEM_OFFSET(version)) {
668 mem_region->ep_mem_size = 0;
669 } else {
670 mem_region->ep_mem_size = ffa_emad_size_get(version);
671 mem_region->ep_mem_offset = sizeof(*mem_region);
672 memset(mem_region->reserved, 0, 12);
673 }
674 }
675
676 static int
ffa_setup_and_transmit(u32 func_id,void * buffer,u32 max_fragsize,struct ffa_mem_ops_args * args)677 ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
678 struct ffa_mem_ops_args *args)
679 {
680 int rc = 0;
681 bool first = true;
682 u32 composite_offset;
683 phys_addr_t addr = 0;
684 struct ffa_mem_region *mem_region = buffer;
685 struct ffa_composite_mem_region *composite;
686 struct ffa_mem_region_addr_range *constituents;
687 struct ffa_mem_region_attributes *ep_mem_access;
688 u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg);
689
690 mem_region->tag = args->tag;
691 mem_region->flags = args->flags;
692 mem_region->sender_id = drv_info->vm_id;
693 mem_region->attributes = ffa_memory_attributes_get(func_id);
694 composite_offset = ffa_mem_desc_offset(buffer, args->nattrs,
695 drv_info->version);
696
697 for (idx = 0; idx < args->nattrs; idx++) {
698 ep_mem_access = buffer +
699 ffa_mem_desc_offset(buffer, idx, drv_info->version);
700 ep_mem_access->receiver = args->attrs[idx].receiver;
701 ep_mem_access->attrs = args->attrs[idx].attrs;
702 ep_mem_access->composite_off = composite_offset;
703 ep_mem_access->flag = 0;
704 ep_mem_access->reserved = 0;
705 ffa_emad_impdef_value_init(drv_info->version,
706 ep_mem_access->impdef_val,
707 args->attrs[idx].impdef_val);
708 }
709 mem_region->handle = 0;
710 mem_region->ep_count = args->nattrs;
711 ffa_mem_region_additional_setup(drv_info->version, mem_region);
712
713 composite = buffer + composite_offset;
714 composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg);
715 composite->addr_range_cnt = num_entries;
716 composite->reserved = 0;
717
718 length = composite_offset + CONSTITUENTS_OFFSET(num_entries);
719 frag_len = composite_offset + CONSTITUENTS_OFFSET(0);
720 if (frag_len > max_fragsize)
721 return -ENXIO;
722
723 if (!args->use_txbuf) {
724 addr = virt_to_phys(buffer);
725 buf_sz = max_fragsize / FFA_PAGE_SIZE;
726 }
727
728 constituents = buffer + frag_len;
729 idx = 0;
730 do {
731 if (frag_len == max_fragsize) {
732 rc = ffa_transmit_fragment(func_id, addr, buf_sz,
733 frag_len, length,
734 &args->g_handle, first);
735 if (rc < 0)
736 return -ENXIO;
737
738 first = false;
739 idx = 0;
740 frag_len = 0;
741 constituents = buffer;
742 }
743
744 if ((void *)constituents - buffer > max_fragsize) {
745 pr_err("Memory Region Fragment > Tx Buffer size\n");
746 return -EFAULT;
747 }
748
749 constituents->address = sg_phys(args->sg);
750 constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE;
751 constituents->reserved = 0;
752 constituents++;
753 frag_len += sizeof(struct ffa_mem_region_addr_range);
754 } while ((args->sg = sg_next(args->sg)));
755
756 return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len,
757 length, &args->g_handle, first);
758 }
759
ffa_memory_ops(u32 func_id,struct ffa_mem_ops_args * args)760 static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args)
761 {
762 int ret;
763 void *buffer;
764 size_t rxtx_bufsz = drv_info->rxtx_bufsz;
765
766 if (!args->use_txbuf) {
767 buffer = alloc_pages_exact(rxtx_bufsz, GFP_KERNEL);
768 if (!buffer)
769 return -ENOMEM;
770 } else {
771 buffer = drv_info->tx_buffer;
772 mutex_lock(&drv_info->tx_lock);
773 }
774
775 ret = ffa_setup_and_transmit(func_id, buffer, rxtx_bufsz, args);
776
777 if (args->use_txbuf)
778 mutex_unlock(&drv_info->tx_lock);
779 else
780 free_pages_exact(buffer, rxtx_bufsz);
781
782 return ret < 0 ? ret : 0;
783 }
784
ffa_memory_reclaim(u64 g_handle,u32 flags)785 static int ffa_memory_reclaim(u64 g_handle, u32 flags)
786 {
787 ffa_value_t ret;
788
789 invoke_ffa_fn((ffa_value_t){
790 .a0 = FFA_MEM_RECLAIM,
791 .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle),
792 .a3 = flags,
793 }, &ret);
794
795 if (ret.a0 == FFA_ERROR)
796 return ffa_to_linux_errno((int)ret.a2);
797
798 return 0;
799 }
800
ffa_notification_bitmap_create(void)801 static int ffa_notification_bitmap_create(void)
802 {
803 ffa_value_t ret;
804 u16 vcpu_count = nr_cpu_ids;
805
806 invoke_ffa_fn((ffa_value_t){
807 .a0 = FFA_NOTIFICATION_BITMAP_CREATE,
808 .a1 = drv_info->vm_id, .a2 = vcpu_count,
809 }, &ret);
810
811 if (ret.a0 == FFA_ERROR)
812 return ffa_to_linux_errno((int)ret.a2);
813
814 return 0;
815 }
816
ffa_notification_bitmap_destroy(void)817 static int ffa_notification_bitmap_destroy(void)
818 {
819 ffa_value_t ret;
820
821 invoke_ffa_fn((ffa_value_t){
822 .a0 = FFA_NOTIFICATION_BITMAP_DESTROY,
823 .a1 = drv_info->vm_id,
824 }, &ret);
825
826 if (ret.a0 == FFA_ERROR)
827 return ffa_to_linux_errno((int)ret.a2);
828
829 return 0;
830 }
831
832 enum notify_type {
833 SECURE_PARTITION,
834 NON_SECURE_VM,
835 SPM_FRAMEWORK,
836 NS_HYP_FRAMEWORK,
837 };
838
839 #define NOTIFICATION_LOW_MASK GENMASK(31, 0)
840 #define NOTIFICATION_HIGH_MASK GENMASK(63, 32)
841 #define NOTIFICATION_BITMAP_HIGH(x) \
842 ((u32)(FIELD_GET(NOTIFICATION_HIGH_MASK, (x))))
843 #define NOTIFICATION_BITMAP_LOW(x) \
844 ((u32)(FIELD_GET(NOTIFICATION_LOW_MASK, (x))))
845 #define PACK_NOTIFICATION_BITMAP(low, high) \
846 (FIELD_PREP(NOTIFICATION_LOW_MASK, (low)) | \
847 FIELD_PREP(NOTIFICATION_HIGH_MASK, (high)))
848
849 #define RECEIVER_VCPU_MASK GENMASK(31, 16)
850 #define PACK_NOTIFICATION_GET_RECEIVER_INFO(vcpu_r, r) \
851 (FIELD_PREP(RECEIVER_VCPU_MASK, (vcpu_r)) | \
852 FIELD_PREP(RECEIVER_ID_MASK, (r)))
853
854 #define NOTIFICATION_INFO_GET_MORE_PEND_MASK BIT(0)
855 #define NOTIFICATION_INFO_GET_ID_COUNT GENMASK(11, 7)
856 #define ID_LIST_MASK_64 GENMASK(51, 12)
857 #define ID_LIST_MASK_32 GENMASK(31, 12)
858 #define MAX_IDS_64 20
859 #define MAX_IDS_32 10
860
861 #define PER_VCPU_NOTIFICATION_FLAG BIT(0)
862 #define SECURE_PARTITION_BITMAP_ENABLE BIT(SECURE_PARTITION)
863 #define NON_SECURE_VM_BITMAP_ENABLE BIT(NON_SECURE_VM)
864 #define SPM_FRAMEWORK_BITMAP_ENABLE BIT(SPM_FRAMEWORK)
865 #define NS_HYP_FRAMEWORK_BITMAP_ENABLE BIT(NS_HYP_FRAMEWORK)
866 #define FFA_BITMAP_SECURE_ENABLE_MASK \
867 (SECURE_PARTITION_BITMAP_ENABLE | SPM_FRAMEWORK_BITMAP_ENABLE)
868 #define FFA_BITMAP_NS_ENABLE_MASK \
869 (NON_SECURE_VM_BITMAP_ENABLE | NS_HYP_FRAMEWORK_BITMAP_ENABLE)
870 #define FFA_BITMAP_ALL_ENABLE_MASK \
871 (FFA_BITMAP_SECURE_ENABLE_MASK | FFA_BITMAP_NS_ENABLE_MASK)
872
873 #define FFA_SECURE_PARTITION_ID_FLAG BIT(15)
874
875 #define SPM_FRAMEWORK_BITMAP(x) NOTIFICATION_BITMAP_LOW(x)
876 #define NS_HYP_FRAMEWORK_BITMAP(x) NOTIFICATION_BITMAP_HIGH(x)
877 #define FRAMEWORK_NOTIFY_RX_BUFFER_FULL BIT(0)
878
ffa_notification_bind_common(u16 dst_id,u64 bitmap,u32 flags,bool is_bind)879 static int ffa_notification_bind_common(u16 dst_id, u64 bitmap,
880 u32 flags, bool is_bind)
881 {
882 ffa_value_t ret;
883 u32 func, src_dst_ids = PACK_TARGET_INFO(dst_id, drv_info->vm_id);
884
885 func = is_bind ? FFA_NOTIFICATION_BIND : FFA_NOTIFICATION_UNBIND;
886
887 invoke_ffa_fn((ffa_value_t){
888 .a0 = func, .a1 = src_dst_ids, .a2 = flags,
889 .a3 = NOTIFICATION_BITMAP_LOW(bitmap),
890 .a4 = NOTIFICATION_BITMAP_HIGH(bitmap),
891 }, &ret);
892
893 if (ret.a0 == FFA_ERROR)
894 return ffa_to_linux_errno((int)ret.a2);
895 else if (ret.a0 != FFA_SUCCESS)
896 return -EINVAL;
897
898 return 0;
899 }
900
901 static
ffa_notification_set(u16 src_id,u16 dst_id,u32 flags,u64 bitmap)902 int ffa_notification_set(u16 src_id, u16 dst_id, u32 flags, u64 bitmap)
903 {
904 ffa_value_t ret;
905 u32 src_dst_ids = PACK_TARGET_INFO(dst_id, src_id);
906
907 invoke_ffa_fn((ffa_value_t) {
908 .a0 = FFA_NOTIFICATION_SET, .a1 = src_dst_ids, .a2 = flags,
909 .a3 = NOTIFICATION_BITMAP_LOW(bitmap),
910 .a4 = NOTIFICATION_BITMAP_HIGH(bitmap),
911 }, &ret);
912
913 if (ret.a0 == FFA_ERROR)
914 return ffa_to_linux_errno((int)ret.a2);
915 else if (ret.a0 != FFA_SUCCESS)
916 return -EINVAL;
917
918 return 0;
919 }
920
921 struct ffa_notify_bitmaps {
922 u64 sp_map;
923 u64 vm_map;
924 u64 arch_map;
925 };
926
ffa_notification_get(u32 flags,struct ffa_notify_bitmaps * notify)927 static int ffa_notification_get(u32 flags, struct ffa_notify_bitmaps *notify)
928 {
929 ffa_value_t ret;
930 u16 src_id = drv_info->vm_id;
931 u16 cpu_id = smp_processor_id();
932 u32 rec_vcpu_ids = PACK_NOTIFICATION_GET_RECEIVER_INFO(cpu_id, src_id);
933
934 invoke_ffa_fn((ffa_value_t){
935 .a0 = FFA_NOTIFICATION_GET, .a1 = rec_vcpu_ids, .a2 = flags,
936 }, &ret);
937
938 if (ret.a0 == FFA_ERROR)
939 return ffa_to_linux_errno((int)ret.a2);
940 else if (ret.a0 != FFA_SUCCESS)
941 return -EINVAL; /* Something else went wrong. */
942
943 if (flags & SECURE_PARTITION_BITMAP_ENABLE)
944 notify->sp_map = PACK_NOTIFICATION_BITMAP(ret.a2, ret.a3);
945 if (flags & NON_SECURE_VM_BITMAP_ENABLE)
946 notify->vm_map = PACK_NOTIFICATION_BITMAP(ret.a4, ret.a5);
947 if (flags & SPM_FRAMEWORK_BITMAP_ENABLE)
948 notify->arch_map = SPM_FRAMEWORK_BITMAP(ret.a6);
949 if (flags & NS_HYP_FRAMEWORK_BITMAP_ENABLE)
950 notify->arch_map = PACK_NOTIFICATION_BITMAP(notify->arch_map,
951 ret.a7);
952
953 return 0;
954 }
955
956 struct ffa_dev_part_info {
957 ffa_sched_recv_cb callback;
958 void *cb_data;
959 rwlock_t rw_lock;
960 struct ffa_device *dev;
961 struct list_head node;
962 };
963
__do_sched_recv_cb(u16 part_id,u16 vcpu,bool is_per_vcpu)964 static void __do_sched_recv_cb(u16 part_id, u16 vcpu, bool is_per_vcpu)
965 {
966 struct ffa_dev_part_info *partition = NULL, *tmp;
967 ffa_sched_recv_cb callback;
968 struct list_head *phead;
969 void *cb_data;
970
971 phead = xa_load(&drv_info->partition_info, part_id);
972 if (!phead) {
973 pr_err("%s: Invalid partition ID 0x%x\n", __func__, part_id);
974 return;
975 }
976
977 list_for_each_entry_safe(partition, tmp, phead, node) {
978 read_lock(&partition->rw_lock);
979 callback = partition->callback;
980 cb_data = partition->cb_data;
981 read_unlock(&partition->rw_lock);
982
983 if (callback)
984 callback(vcpu, is_per_vcpu, cb_data);
985 }
986 }
987
988 /*
989 * Map logical ID index to the u16 index within the packed ID list.
990 *
991 * For native responses (FF-A width == kernel word size), IDs are
992 * tightly packed: idx -> idx.
993 *
994 * For 32-bit responses on a 64-bit kernel, each 64-bit register
995 * contributes 4 x u16 values but only the lower 2 are defined; the
996 * upper 2 are garbage. This mapping skips those upper halves:
997 * 0,1,2,3,4,5,... -> 0,1,4,5,8,9,...
998 */
list_idx_to_u16_idx(int idx,bool is_native_resp)999 static int list_idx_to_u16_idx(int idx, bool is_native_resp)
1000 {
1001 return is_native_resp ? idx : idx + 2 * (idx >> 1);
1002 }
1003
ffa_notification_info_get(void)1004 static void ffa_notification_info_get(void)
1005 {
1006 int ids_processed, ids_count[MAX_IDS_64];
1007 int idx, list, max_ids, lists_cnt;
1008 bool is_64b_resp, is_native_resp;
1009 ffa_value_t ret;
1010 u64 id_list;
1011
1012 do {
1013 invoke_ffa_fn((ffa_value_t){
1014 .a0 = FFA_FN_NATIVE(NOTIFICATION_INFO_GET),
1015 }, &ret);
1016
1017 if (ret.a0 != FFA_FN_NATIVE(SUCCESS) && ret.a0 != FFA_SUCCESS) {
1018 if ((s32)ret.a2 != FFA_RET_NO_DATA)
1019 pr_err("Notification Info fetch failed: 0x%lx (0x%lx)",
1020 ret.a0, ret.a2);
1021 return;
1022 }
1023
1024 is_64b_resp = (ret.a0 == FFA_FN64_SUCCESS);
1025 is_native_resp = (ret.a0 == FFA_FN_NATIVE(SUCCESS));
1026
1027 ids_processed = 0;
1028 lists_cnt = FIELD_GET(NOTIFICATION_INFO_GET_ID_COUNT, ret.a2);
1029 if (is_64b_resp) {
1030 max_ids = MAX_IDS_64;
1031 id_list = FIELD_GET(ID_LIST_MASK_64, ret.a2);
1032 } else {
1033 max_ids = MAX_IDS_32;
1034 id_list = FIELD_GET(ID_LIST_MASK_32, ret.a2);
1035 }
1036
1037 for (idx = 0; idx < lists_cnt; idx++, id_list >>= 2)
1038 ids_count[idx] = (id_list & 0x3) + 1;
1039
1040 /* Process IDs */
1041 for (list = 0; list < lists_cnt; list++) {
1042 int u16_idx;
1043 u16 vcpu_id, part_id, *packed_id_list = (u16 *)&ret.a3;
1044
1045 if (ids_processed >= max_ids - 1)
1046 break;
1047
1048 u16_idx = list_idx_to_u16_idx(ids_processed,
1049 is_native_resp);
1050 part_id = packed_id_list[u16_idx];
1051 ids_processed++;
1052
1053 if (ids_count[list] == 1) { /* Global Notification */
1054 __do_sched_recv_cb(part_id, 0, false);
1055 continue;
1056 }
1057
1058 /* Per vCPU Notification */
1059 for (idx = 1; idx < ids_count[list]; idx++) {
1060 if (ids_processed >= max_ids - 1)
1061 break;
1062
1063 u16_idx = list_idx_to_u16_idx(ids_processed,
1064 is_native_resp);
1065 vcpu_id = packed_id_list[u16_idx];
1066 ids_processed++;
1067
1068 __do_sched_recv_cb(part_id, vcpu_id, true);
1069 }
1070 }
1071 } while (ret.a2 & NOTIFICATION_INFO_GET_MORE_PEND_MASK);
1072 }
1073
ffa_run(struct ffa_device * dev,u16 vcpu)1074 static int ffa_run(struct ffa_device *dev, u16 vcpu)
1075 {
1076 ffa_value_t ret;
1077 u32 target = dev->vm_id << 16 | vcpu;
1078
1079 invoke_ffa_fn((ffa_value_t){ .a0 = FFA_RUN, .a1 = target, }, &ret);
1080
1081 while (ret.a0 == FFA_INTERRUPT)
1082 invoke_ffa_fn((ffa_value_t){ .a0 = FFA_RUN, .a1 = ret.a1, },
1083 &ret);
1084
1085 if (ret.a0 == FFA_ERROR)
1086 return ffa_to_linux_errno((int)ret.a2);
1087
1088 return 0;
1089 }
1090
ffa_drvinfo_flags_init(void)1091 static void ffa_drvinfo_flags_init(void)
1092 {
1093 if (!ffa_features(FFA_FN_NATIVE(MEM_LEND), 0, NULL, NULL) ||
1094 !ffa_features(FFA_FN_NATIVE(MEM_SHARE), 0, NULL, NULL))
1095 drv_info->mem_ops_native = true;
1096
1097 if (!ffa_features(FFA_MSG_SEND_DIRECT_REQ2, 0, NULL, NULL) ||
1098 !ffa_features(FFA_MSG_SEND_DIRECT_RESP2, 0, NULL, NULL))
1099 drv_info->msg_direct_req2_supp = true;
1100 }
1101
ffa_api_version_get(void)1102 static u32 ffa_api_version_get(void)
1103 {
1104 return drv_info->version;
1105 }
1106
ffa_partition_info_get(const char * uuid_str,struct ffa_partition_info * buffer)1107 static int ffa_partition_info_get(const char *uuid_str,
1108 struct ffa_partition_info *buffer)
1109 {
1110 int count;
1111 uuid_t uuid;
1112 struct ffa_partition_info *pbuf;
1113
1114 if (uuid_parse(uuid_str, &uuid)) {
1115 pr_err("invalid uuid (%s)\n", uuid_str);
1116 return -ENODEV;
1117 }
1118
1119 count = ffa_partition_probe(&uuid, &pbuf);
1120 if (count <= 0)
1121 return -ENOENT;
1122
1123 memcpy(buffer, pbuf, sizeof(*pbuf) * count);
1124 kfree(pbuf);
1125 return 0;
1126 }
1127
ffa_mode_32bit_set(struct ffa_device * dev)1128 static void ffa_mode_32bit_set(struct ffa_device *dev)
1129 {
1130 dev->mode_32bit = true;
1131 }
1132
ffa_sync_send_receive(struct ffa_device * dev,struct ffa_send_direct_data * data)1133 static int ffa_sync_send_receive(struct ffa_device *dev,
1134 struct ffa_send_direct_data *data)
1135 {
1136 return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id,
1137 dev->mode_32bit, data);
1138 }
1139
ffa_indirect_msg_send(struct ffa_device * dev,void * buf,size_t sz)1140 static int ffa_indirect_msg_send(struct ffa_device *dev, void *buf, size_t sz)
1141 {
1142 return ffa_msg_send2(dev, drv_info->vm_id, buf, sz);
1143 }
1144
ffa_sync_send_receive2(struct ffa_device * dev,struct ffa_send_direct_data2 * data)1145 static int ffa_sync_send_receive2(struct ffa_device *dev,
1146 struct ffa_send_direct_data2 *data)
1147 {
1148 if (!drv_info->msg_direct_req2_supp)
1149 return -EOPNOTSUPP;
1150
1151 return ffa_msg_send_direct_req2(drv_info->vm_id, dev->vm_id,
1152 &dev->uuid, data);
1153 }
1154
ffa_memory_share(struct ffa_mem_ops_args * args)1155 static int ffa_memory_share(struct ffa_mem_ops_args *args)
1156 {
1157 if (drv_info->mem_ops_native)
1158 return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args);
1159
1160 return ffa_memory_ops(FFA_MEM_SHARE, args);
1161 }
1162
ffa_memory_lend(struct ffa_mem_ops_args * args)1163 static int ffa_memory_lend(struct ffa_mem_ops_args *args)
1164 {
1165 /* Note that upon a successful MEM_LEND request the caller
1166 * must ensure that the memory region specified is not accessed
1167 * until a successful MEM_RECALIM call has been made.
1168 * On systems with a hypervisor present this will been enforced,
1169 * however on systems without a hypervisor the responsibility
1170 * falls to the calling kernel driver to prevent access.
1171 */
1172 if (drv_info->mem_ops_native)
1173 return ffa_memory_ops(FFA_FN_NATIVE(MEM_LEND), args);
1174
1175 return ffa_memory_ops(FFA_MEM_LEND, args);
1176 }
1177
1178 #define ffa_notifications_disabled() (!drv_info->notif_enabled)
1179
1180 struct notifier_cb_info {
1181 struct hlist_node hnode;
1182 struct ffa_device *dev;
1183 ffa_fwk_notifier_cb fwk_cb;
1184 ffa_notifier_cb cb;
1185 void *cb_data;
1186 };
1187
1188 static int
ffa_sched_recv_cb_update(struct ffa_device * dev,ffa_sched_recv_cb callback,void * cb_data,bool is_registration)1189 ffa_sched_recv_cb_update(struct ffa_device *dev, ffa_sched_recv_cb callback,
1190 void *cb_data, bool is_registration)
1191 {
1192 struct ffa_dev_part_info *partition = NULL, *tmp;
1193 struct list_head *phead;
1194 bool cb_valid;
1195
1196 if (ffa_notifications_disabled())
1197 return -EOPNOTSUPP;
1198
1199 phead = xa_load(&drv_info->partition_info, dev->vm_id);
1200 if (!phead) {
1201 pr_err("%s: Invalid partition ID 0x%x\n", __func__, dev->vm_id);
1202 return -EINVAL;
1203 }
1204
1205 list_for_each_entry_safe(partition, tmp, phead, node)
1206 if (partition->dev == dev)
1207 break;
1208
1209 if (!partition) {
1210 pr_err("%s: No such partition ID 0x%x\n", __func__, dev->vm_id);
1211 return -EINVAL;
1212 }
1213
1214 write_lock(&partition->rw_lock);
1215
1216 cb_valid = !!partition->callback;
1217 if (!(is_registration ^ cb_valid)) {
1218 write_unlock(&partition->rw_lock);
1219 return -EINVAL;
1220 }
1221
1222 partition->callback = callback;
1223 partition->cb_data = cb_data;
1224
1225 write_unlock(&partition->rw_lock);
1226 return 0;
1227 }
1228
ffa_sched_recv_cb_register(struct ffa_device * dev,ffa_sched_recv_cb cb,void * cb_data)1229 static int ffa_sched_recv_cb_register(struct ffa_device *dev,
1230 ffa_sched_recv_cb cb, void *cb_data)
1231 {
1232 return ffa_sched_recv_cb_update(dev, cb, cb_data, true);
1233 }
1234
ffa_sched_recv_cb_unregister(struct ffa_device * dev)1235 static int ffa_sched_recv_cb_unregister(struct ffa_device *dev)
1236 {
1237 return ffa_sched_recv_cb_update(dev, NULL, NULL, false);
1238 }
1239
ffa_notification_bind(u16 dst_id,u64 bitmap,u32 flags)1240 static int ffa_notification_bind(u16 dst_id, u64 bitmap, u32 flags)
1241 {
1242 return ffa_notification_bind_common(dst_id, bitmap, flags, true);
1243 }
1244
ffa_notification_unbind(u16 dst_id,u64 bitmap)1245 static int ffa_notification_unbind(u16 dst_id, u64 bitmap)
1246 {
1247 return ffa_notification_bind_common(dst_id, bitmap, 0, false);
1248 }
1249
ffa_notify_type_get(u16 vm_id)1250 static enum notify_type ffa_notify_type_get(u16 vm_id)
1251 {
1252 if (vm_id & FFA_SECURE_PARTITION_ID_FLAG)
1253 return SECURE_PARTITION;
1254 else
1255 return NON_SECURE_VM;
1256 }
1257
1258 /* notifier_hnode_get* should be called with notify_lock held */
1259 static struct notifier_cb_info *
notifier_hnode_get_by_vmid(u16 notify_id,int vmid)1260 notifier_hnode_get_by_vmid(u16 notify_id, int vmid)
1261 {
1262 struct notifier_cb_info *node;
1263
1264 hash_for_each_possible(drv_info->notifier_hash, node, hnode, notify_id)
1265 if (node->fwk_cb && vmid == node->dev->vm_id)
1266 return node;
1267
1268 return NULL;
1269 }
1270
1271 static struct notifier_cb_info *
notifier_hnode_get_by_vmid_uuid(u16 notify_id,int vmid,const uuid_t * uuid)1272 notifier_hnode_get_by_vmid_uuid(u16 notify_id, int vmid, const uuid_t *uuid)
1273 {
1274 struct notifier_cb_info *node;
1275
1276 if (uuid_is_null(uuid))
1277 return notifier_hnode_get_by_vmid(notify_id, vmid);
1278
1279 hash_for_each_possible(drv_info->notifier_hash, node, hnode, notify_id)
1280 if (node->fwk_cb && vmid == node->dev->vm_id &&
1281 uuid_equal(&node->dev->uuid, uuid))
1282 return node;
1283
1284 return NULL;
1285 }
1286
1287 static struct notifier_cb_info *
notifier_hnode_get_by_type(u16 notify_id,enum notify_type type)1288 notifier_hnode_get_by_type(u16 notify_id, enum notify_type type)
1289 {
1290 struct notifier_cb_info *node;
1291
1292 hash_for_each_possible(drv_info->notifier_hash, node, hnode, notify_id)
1293 if (node->cb && type == ffa_notify_type_get(node->dev->vm_id))
1294 return node;
1295
1296 return NULL;
1297 }
1298
update_notifier_cb(struct ffa_device * dev,int notify_id,struct notifier_cb_info * cb,bool is_framework)1299 static int update_notifier_cb(struct ffa_device *dev, int notify_id,
1300 struct notifier_cb_info *cb, bool is_framework)
1301 {
1302 struct notifier_cb_info *cb_info = NULL;
1303 enum notify_type type = ffa_notify_type_get(dev->vm_id);
1304 bool cb_found, is_registration = !!cb;
1305
1306 if (is_framework)
1307 cb_info = notifier_hnode_get_by_vmid_uuid(notify_id, dev->vm_id,
1308 &dev->uuid);
1309 else
1310 cb_info = notifier_hnode_get_by_type(notify_id, type);
1311
1312 cb_found = !!cb_info;
1313
1314 if (!(is_registration ^ cb_found))
1315 return -EINVAL;
1316
1317 if (is_registration) {
1318 hash_add(drv_info->notifier_hash, &cb->hnode, notify_id);
1319 } else {
1320 hash_del(&cb_info->hnode);
1321 kfree(cb_info);
1322 }
1323
1324 return 0;
1325 }
1326
__ffa_notify_relinquish(struct ffa_device * dev,int notify_id,bool is_framework)1327 static int __ffa_notify_relinquish(struct ffa_device *dev, int notify_id,
1328 bool is_framework)
1329 {
1330 int rc;
1331
1332 if (ffa_notifications_disabled())
1333 return -EOPNOTSUPP;
1334
1335 if (notify_id >= FFA_MAX_NOTIFICATIONS)
1336 return -EINVAL;
1337
1338 write_lock(&drv_info->notify_lock);
1339
1340 rc = update_notifier_cb(dev, notify_id, NULL, is_framework);
1341 if (rc) {
1342 pr_err("Could not unregister notification callback\n");
1343 write_unlock(&drv_info->notify_lock);
1344 return rc;
1345 }
1346
1347 if (!is_framework)
1348 rc = ffa_notification_unbind(dev->vm_id, BIT(notify_id));
1349
1350 write_unlock(&drv_info->notify_lock);
1351
1352 return rc;
1353 }
1354
ffa_notify_relinquish(struct ffa_device * dev,int notify_id)1355 static int ffa_notify_relinquish(struct ffa_device *dev, int notify_id)
1356 {
1357 return __ffa_notify_relinquish(dev, notify_id, false);
1358 }
1359
ffa_fwk_notify_relinquish(struct ffa_device * dev,int notify_id)1360 static int ffa_fwk_notify_relinquish(struct ffa_device *dev, int notify_id)
1361 {
1362 return __ffa_notify_relinquish(dev, notify_id, true);
1363 }
1364
__ffa_notify_request(struct ffa_device * dev,bool is_per_vcpu,void * cb,void * cb_data,int notify_id,bool is_framework)1365 static int __ffa_notify_request(struct ffa_device *dev, bool is_per_vcpu,
1366 void *cb, void *cb_data,
1367 int notify_id, bool is_framework)
1368 {
1369 int rc;
1370 u32 flags = 0;
1371 struct notifier_cb_info *cb_info = NULL;
1372
1373 if (ffa_notifications_disabled())
1374 return -EOPNOTSUPP;
1375
1376 if (notify_id >= FFA_MAX_NOTIFICATIONS)
1377 return -EINVAL;
1378
1379 cb_info = kzalloc_obj(*cb_info);
1380 if (!cb_info)
1381 return -ENOMEM;
1382
1383 cb_info->dev = dev;
1384 cb_info->cb_data = cb_data;
1385 if (is_framework)
1386 cb_info->fwk_cb = cb;
1387 else
1388 cb_info->cb = cb;
1389
1390 write_lock(&drv_info->notify_lock);
1391
1392 if (!is_framework) {
1393 if (is_per_vcpu)
1394 flags = PER_VCPU_NOTIFICATION_FLAG;
1395
1396 rc = ffa_notification_bind(dev->vm_id, BIT(notify_id), flags);
1397 if (rc)
1398 goto out_unlock_free;
1399 }
1400
1401 rc = update_notifier_cb(dev, notify_id, cb_info, is_framework);
1402 if (rc) {
1403 pr_err("Failed to register callback for %d - %d\n",
1404 notify_id, rc);
1405 if (!is_framework)
1406 ffa_notification_unbind(dev->vm_id, BIT(notify_id));
1407 }
1408
1409 out_unlock_free:
1410 write_unlock(&drv_info->notify_lock);
1411 if (rc)
1412 kfree(cb_info);
1413
1414 return rc;
1415 }
1416
ffa_notify_request(struct ffa_device * dev,bool is_per_vcpu,ffa_notifier_cb cb,void * cb_data,int notify_id)1417 static int ffa_notify_request(struct ffa_device *dev, bool is_per_vcpu,
1418 ffa_notifier_cb cb, void *cb_data, int notify_id)
1419 {
1420 return __ffa_notify_request(dev, is_per_vcpu, cb, cb_data, notify_id,
1421 false);
1422 }
1423
1424 static int
ffa_fwk_notify_request(struct ffa_device * dev,ffa_fwk_notifier_cb cb,void * cb_data,int notify_id)1425 ffa_fwk_notify_request(struct ffa_device *dev, ffa_fwk_notifier_cb cb,
1426 void *cb_data, int notify_id)
1427 {
1428 return __ffa_notify_request(dev, false, cb, cb_data, notify_id, true);
1429 }
1430
ffa_notify_send(struct ffa_device * dev,int notify_id,bool is_per_vcpu,u16 vcpu)1431 static int ffa_notify_send(struct ffa_device *dev, int notify_id,
1432 bool is_per_vcpu, u16 vcpu)
1433 {
1434 u32 flags = 0;
1435
1436 if (ffa_notifications_disabled())
1437 return -EOPNOTSUPP;
1438
1439 if (is_per_vcpu)
1440 flags |= (PER_VCPU_NOTIFICATION_FLAG | vcpu << 16);
1441
1442 return ffa_notification_set(dev->vm_id, drv_info->vm_id, flags,
1443 BIT(notify_id));
1444 }
1445
handle_notif_callbacks(u64 bitmap,enum notify_type type)1446 static void handle_notif_callbacks(u64 bitmap, enum notify_type type)
1447 {
1448 int notify_id;
1449 struct notifier_cb_info *cb_info = NULL;
1450
1451 for (notify_id = 0; notify_id <= FFA_MAX_NOTIFICATIONS && bitmap;
1452 notify_id++, bitmap >>= 1) {
1453 if (!(bitmap & 1))
1454 continue;
1455
1456 read_lock(&drv_info->notify_lock);
1457 cb_info = notifier_hnode_get_by_type(notify_id, type);
1458 read_unlock(&drv_info->notify_lock);
1459
1460 if (cb_info && cb_info->cb)
1461 cb_info->cb(notify_id, cb_info->cb_data);
1462 }
1463 }
1464
handle_fwk_notif_callbacks(u32 bitmap)1465 static void handle_fwk_notif_callbacks(u32 bitmap)
1466 {
1467 void *buf;
1468 uuid_t uuid;
1469 int notify_id = 0, target;
1470 struct ffa_indirect_msg_hdr *msg;
1471 struct notifier_cb_info *cb_info = NULL;
1472
1473 /* Only one framework notification defined and supported for now */
1474 if (!(bitmap & FRAMEWORK_NOTIFY_RX_BUFFER_FULL))
1475 return;
1476
1477 mutex_lock(&drv_info->rx_lock);
1478
1479 msg = drv_info->rx_buffer;
1480 buf = kmemdup((void *)msg + msg->offset, msg->size, GFP_KERNEL);
1481 if (!buf) {
1482 mutex_unlock(&drv_info->rx_lock);
1483 return;
1484 }
1485
1486 target = SENDER_ID(msg->send_recv_id);
1487 if (msg->offset >= sizeof(*msg))
1488 uuid_copy(&uuid, &msg->uuid);
1489 else
1490 uuid_copy(&uuid, &uuid_null);
1491
1492 mutex_unlock(&drv_info->rx_lock);
1493
1494 ffa_rx_release();
1495
1496 read_lock(&drv_info->notify_lock);
1497 cb_info = notifier_hnode_get_by_vmid_uuid(notify_id, target, &uuid);
1498 read_unlock(&drv_info->notify_lock);
1499
1500 if (cb_info && cb_info->fwk_cb)
1501 cb_info->fwk_cb(notify_id, cb_info->cb_data, buf);
1502 kfree(buf);
1503 }
1504
notif_get_and_handle(void * cb_data)1505 static void notif_get_and_handle(void *cb_data)
1506 {
1507 int rc;
1508 u32 flags;
1509 struct ffa_drv_info *info = cb_data;
1510 struct ffa_notify_bitmaps bitmaps = { 0 };
1511
1512 if (info->vm_id == 0) /* Non secure physical instance */
1513 flags = FFA_BITMAP_SECURE_ENABLE_MASK;
1514 else
1515 flags = FFA_BITMAP_ALL_ENABLE_MASK;
1516
1517 rc = ffa_notification_get(flags, &bitmaps);
1518 if (rc) {
1519 pr_err("Failed to retrieve notifications with %d!\n", rc);
1520 return;
1521 }
1522
1523 handle_fwk_notif_callbacks(SPM_FRAMEWORK_BITMAP(bitmaps.arch_map));
1524 handle_fwk_notif_callbacks(NS_HYP_FRAMEWORK_BITMAP(bitmaps.arch_map));
1525 handle_notif_callbacks(bitmaps.vm_map, NON_SECURE_VM);
1526 handle_notif_callbacks(bitmaps.sp_map, SECURE_PARTITION);
1527 }
1528
1529 static void
ffa_self_notif_handle(u16 vcpu,bool is_per_vcpu,void * cb_data)1530 ffa_self_notif_handle(u16 vcpu, bool is_per_vcpu, void *cb_data)
1531 {
1532 struct ffa_drv_info *info = cb_data;
1533
1534 if (!is_per_vcpu)
1535 notif_get_and_handle(info);
1536 else
1537 smp_call_function_single(vcpu, notif_get_and_handle, info, 0);
1538 }
1539
notif_pcpu_irq_work_fn(struct work_struct * work)1540 static void notif_pcpu_irq_work_fn(struct work_struct *work)
1541 {
1542 struct ffa_drv_info *info = container_of(work, struct ffa_drv_info,
1543 notif_pcpu_work);
1544
1545 ffa_self_notif_handle(smp_processor_id(), true, info);
1546 }
1547
1548 static const struct ffa_info_ops ffa_drv_info_ops = {
1549 .api_version_get = ffa_api_version_get,
1550 .partition_info_get = ffa_partition_info_get,
1551 };
1552
1553 static const struct ffa_msg_ops ffa_drv_msg_ops = {
1554 .mode_32bit_set = ffa_mode_32bit_set,
1555 .sync_send_receive = ffa_sync_send_receive,
1556 .indirect_send = ffa_indirect_msg_send,
1557 .sync_send_receive2 = ffa_sync_send_receive2,
1558 };
1559
1560 static const struct ffa_mem_ops ffa_drv_mem_ops = {
1561 .memory_reclaim = ffa_memory_reclaim,
1562 .memory_share = ffa_memory_share,
1563 .memory_lend = ffa_memory_lend,
1564 };
1565
1566 static const struct ffa_cpu_ops ffa_drv_cpu_ops = {
1567 .run = ffa_run,
1568 };
1569
1570 static const struct ffa_notifier_ops ffa_drv_notifier_ops = {
1571 .sched_recv_cb_register = ffa_sched_recv_cb_register,
1572 .sched_recv_cb_unregister = ffa_sched_recv_cb_unregister,
1573 .notify_request = ffa_notify_request,
1574 .notify_relinquish = ffa_notify_relinquish,
1575 .fwk_notify_request = ffa_fwk_notify_request,
1576 .fwk_notify_relinquish = ffa_fwk_notify_relinquish,
1577 .notify_send = ffa_notify_send,
1578 };
1579
1580 static const struct ffa_ops ffa_drv_ops = {
1581 .info_ops = &ffa_drv_info_ops,
1582 .msg_ops = &ffa_drv_msg_ops,
1583 .mem_ops = &ffa_drv_mem_ops,
1584 .cpu_ops = &ffa_drv_cpu_ops,
1585 .notifier_ops = &ffa_drv_notifier_ops,
1586 };
1587
ffa_device_match_uuid(struct ffa_device * ffa_dev,const uuid_t * uuid)1588 void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid)
1589 {
1590 int count, idx;
1591 struct ffa_partition_info *pbuf, *tpbuf;
1592
1593 count = ffa_partition_probe(uuid, &pbuf);
1594 if (count <= 0)
1595 return;
1596
1597 for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++)
1598 if (tpbuf->id == ffa_dev->vm_id)
1599 uuid_copy(&ffa_dev->uuid, uuid);
1600 kfree(pbuf);
1601 }
1602
1603 static int
ffa_bus_notifier(struct notifier_block * nb,unsigned long action,void * data)1604 ffa_bus_notifier(struct notifier_block *nb, unsigned long action, void *data)
1605 {
1606 struct device *dev = data;
1607 struct ffa_device *fdev = to_ffa_dev(dev);
1608
1609 if (action == BUS_NOTIFY_BIND_DRIVER) {
1610 struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver);
1611 const struct ffa_device_id *id_table = ffa_drv->id_table;
1612
1613 /*
1614 * FF-A v1.1 provides UUID for each partition as part of the
1615 * discovery API, the discovered UUID must be populated in the
1616 * device's UUID and there is no need to workaround by copying
1617 * the same from the driver table.
1618 */
1619 if (uuid_is_null(&fdev->uuid))
1620 ffa_device_match_uuid(fdev, &id_table->uuid);
1621
1622 return NOTIFY_OK;
1623 }
1624
1625 return NOTIFY_DONE;
1626 }
1627
1628 static struct notifier_block ffa_bus_nb = {
1629 .notifier_call = ffa_bus_notifier,
1630 };
1631
ffa_xa_add_partition_info(struct ffa_device * dev)1632 static int ffa_xa_add_partition_info(struct ffa_device *dev)
1633 {
1634 struct ffa_dev_part_info *info;
1635 struct list_head *head, *phead;
1636 int ret = -ENOMEM;
1637
1638 phead = xa_load(&drv_info->partition_info, dev->vm_id);
1639 if (phead) {
1640 head = phead;
1641 list_for_each_entry(info, head, node) {
1642 if (info->dev == dev) {
1643 pr_err("%s: duplicate dev %p part ID 0x%x\n",
1644 __func__, dev, dev->vm_id);
1645 return -EEXIST;
1646 }
1647 }
1648 }
1649
1650 info = kzalloc_obj(*info);
1651 if (!info)
1652 return ret;
1653
1654 rwlock_init(&info->rw_lock);
1655 info->dev = dev;
1656
1657 if (!phead) {
1658 phead = kzalloc_obj(*phead);
1659 if (!phead)
1660 goto free_out;
1661
1662 INIT_LIST_HEAD(phead);
1663
1664 ret = xa_insert(&drv_info->partition_info, dev->vm_id, phead,
1665 GFP_KERNEL);
1666 if (ret) {
1667 pr_err("%s: failed to save part ID 0x%x Ret:%d\n",
1668 __func__, dev->vm_id, ret);
1669 goto free_out;
1670 }
1671 }
1672 list_add(&info->node, phead);
1673 return 0;
1674
1675 free_out:
1676 kfree(phead);
1677 kfree(info);
1678 return ret;
1679 }
1680
ffa_setup_host_partition(int vm_id)1681 static int ffa_setup_host_partition(int vm_id)
1682 {
1683 struct ffa_partition_info buf = { 0 };
1684 struct ffa_device *ffa_dev;
1685 int ret;
1686
1687 buf.id = vm_id;
1688 ffa_dev = ffa_device_register(&buf, &ffa_drv_ops);
1689 if (!ffa_dev) {
1690 pr_err("%s: failed to register host partition ID 0x%x\n",
1691 __func__, vm_id);
1692 return -EINVAL;
1693 }
1694
1695 ret = ffa_xa_add_partition_info(ffa_dev);
1696 if (ret)
1697 return ret;
1698
1699 if (ffa_notifications_disabled())
1700 return 0;
1701
1702 ret = ffa_sched_recv_cb_update(ffa_dev, ffa_self_notif_handle,
1703 drv_info, true);
1704 if (ret)
1705 pr_info("Failed to register driver sched callback %d\n", ret);
1706
1707 return ret;
1708 }
1709
ffa_partitions_cleanup(void)1710 static void ffa_partitions_cleanup(void)
1711 {
1712 struct list_head *phead;
1713 unsigned long idx;
1714
1715 /* Clean up/free all registered devices */
1716 ffa_devices_unregister();
1717
1718 xa_for_each(&drv_info->partition_info, idx, phead) {
1719 struct ffa_dev_part_info *info, *tmp;
1720
1721 xa_erase(&drv_info->partition_info, idx);
1722 list_for_each_entry_safe(info, tmp, phead, node) {
1723 list_del(&info->node);
1724 kfree(info);
1725 }
1726 kfree(phead);
1727 }
1728
1729 xa_destroy(&drv_info->partition_info);
1730 }
1731
ffa_setup_partitions(void)1732 static int ffa_setup_partitions(void)
1733 {
1734 int count, idx, ret;
1735 struct ffa_device *ffa_dev;
1736 struct ffa_partition_info *pbuf, *tpbuf;
1737
1738 if (!FFA_PART_INFO_HAS_UUID_IN_RESP(drv_info->version)) {
1739 ret = bus_register_notifier(&ffa_bus_type, &ffa_bus_nb);
1740 if (ret)
1741 pr_err("Failed to register FF-A bus notifiers\n");
1742 }
1743
1744 count = ffa_partition_probe(&uuid_null, &pbuf);
1745 if (count <= 0) {
1746 pr_info("%s: No partitions found, error %d\n", __func__, count);
1747 return -EINVAL;
1748 }
1749
1750 xa_init(&drv_info->partition_info);
1751 for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) {
1752 /* Note that if the UUID will be uuid_null, that will require
1753 * ffa_bus_notifier() to find the UUID of this partition id
1754 * with help of ffa_device_match_uuid(). FF-A v1.1 and above
1755 * provides UUID here for each partition as part of the
1756 * discovery API and the same is passed.
1757 */
1758 ffa_dev = ffa_device_register(tpbuf, &ffa_drv_ops);
1759 if (!ffa_dev) {
1760 pr_err("%s: failed to register partition ID 0x%x\n",
1761 __func__, tpbuf->id);
1762 continue;
1763 }
1764
1765 if (FFA_PART_INFO_HAS_EXEC_STATE_IN_RESP(drv_info->version) &&
1766 !(tpbuf->properties & FFA_PARTITION_AARCH64_EXEC))
1767 ffa_mode_32bit_set(ffa_dev);
1768
1769 if (ffa_xa_add_partition_info(ffa_dev)) {
1770 ffa_device_unregister(ffa_dev);
1771 continue;
1772 }
1773 }
1774
1775 kfree(pbuf);
1776
1777 /*
1778 * Check if the host is already added as part of partition info
1779 * No multiple UUID possible for the host, so just checking if
1780 * there is an entry will suffice
1781 */
1782 if (xa_load(&drv_info->partition_info, drv_info->vm_id))
1783 return 0;
1784
1785 /* Allocate for the host */
1786 ret = ffa_setup_host_partition(drv_info->vm_id);
1787 if (ret)
1788 ffa_partitions_cleanup();
1789
1790 return ret;
1791 }
1792
1793 /* FFA FEATURE IDs */
1794 #define FFA_FEAT_NOTIFICATION_PENDING_INT (1)
1795 #define FFA_FEAT_SCHEDULE_RECEIVER_INT (2)
1796 #define FFA_FEAT_MANAGED_EXIT_INT (3)
1797
ffa_sched_recv_irq_handler(int irq,void * irq_data)1798 static irqreturn_t ffa_sched_recv_irq_handler(int irq, void *irq_data)
1799 {
1800 struct ffa_pcpu_irq *pcpu = irq_data;
1801 struct ffa_drv_info *info = pcpu->info;
1802
1803 queue_work(info->notif_pcpu_wq, &info->sched_recv_irq_work);
1804
1805 return IRQ_HANDLED;
1806 }
1807
notif_pend_irq_handler(int irq,void * irq_data)1808 static irqreturn_t notif_pend_irq_handler(int irq, void *irq_data)
1809 {
1810 struct ffa_pcpu_irq *pcpu = irq_data;
1811 struct ffa_drv_info *info = pcpu->info;
1812
1813 queue_work_on(smp_processor_id(), info->notif_pcpu_wq,
1814 &info->notif_pcpu_work);
1815
1816 return IRQ_HANDLED;
1817 }
1818
ffa_sched_recv_irq_work_fn(struct work_struct * work)1819 static void ffa_sched_recv_irq_work_fn(struct work_struct *work)
1820 {
1821 ffa_notification_info_get();
1822 }
1823
ffa_irq_map(u32 id)1824 static int ffa_irq_map(u32 id)
1825 {
1826 char *err_str;
1827 int ret, irq, intid;
1828
1829 if (id == FFA_FEAT_NOTIFICATION_PENDING_INT)
1830 err_str = "Notification Pending Interrupt";
1831 else if (id == FFA_FEAT_SCHEDULE_RECEIVER_INT)
1832 err_str = "Schedule Receiver Interrupt";
1833 else
1834 err_str = "Unknown ID";
1835
1836 /* The returned intid is assumed to be SGI donated to NS world */
1837 ret = ffa_features(id, 0, &intid, NULL);
1838 if (ret < 0) {
1839 if (ret != -EOPNOTSUPP)
1840 pr_err("Failed to retrieve FF-A %s %u\n", err_str, id);
1841 return ret;
1842 }
1843
1844 if (acpi_disabled) {
1845 struct of_phandle_args oirq = {};
1846 struct device_node *gic;
1847
1848 /* Only GICv3 supported currently with the device tree */
1849 gic = of_find_compatible_node(NULL, NULL, "arm,gic-v3");
1850 if (!gic)
1851 return -ENXIO;
1852
1853 oirq.np = gic;
1854 oirq.args_count = 1;
1855 oirq.args[0] = intid;
1856 irq = irq_create_of_mapping(&oirq);
1857 of_node_put(gic);
1858 #ifdef CONFIG_ACPI
1859 } else {
1860 irq = acpi_register_gsi(NULL, intid, ACPI_EDGE_SENSITIVE,
1861 ACPI_ACTIVE_HIGH);
1862 #endif
1863 }
1864
1865 if (irq <= 0) {
1866 pr_err("Failed to create IRQ mapping!\n");
1867 return -ENODATA;
1868 }
1869
1870 return irq;
1871 }
1872
ffa_irq_unmap(unsigned int irq)1873 static void ffa_irq_unmap(unsigned int irq)
1874 {
1875 if (!irq)
1876 return;
1877 irq_dispose_mapping(irq);
1878 }
1879
ffa_cpuhp_pcpu_irq_enable(unsigned int cpu)1880 static int ffa_cpuhp_pcpu_irq_enable(unsigned int cpu)
1881 {
1882 if (drv_info->sched_recv_irq)
1883 enable_percpu_irq(drv_info->sched_recv_irq, IRQ_TYPE_NONE);
1884 if (drv_info->notif_pend_irq)
1885 enable_percpu_irq(drv_info->notif_pend_irq, IRQ_TYPE_NONE);
1886 return 0;
1887 }
1888
ffa_cpuhp_pcpu_irq_disable(unsigned int cpu)1889 static int ffa_cpuhp_pcpu_irq_disable(unsigned int cpu)
1890 {
1891 if (drv_info->sched_recv_irq)
1892 disable_percpu_irq(drv_info->sched_recv_irq);
1893 if (drv_info->notif_pend_irq)
1894 disable_percpu_irq(drv_info->notif_pend_irq);
1895 return 0;
1896 }
1897
ffa_uninit_pcpu_irq(void)1898 static void ffa_uninit_pcpu_irq(void)
1899 {
1900 if (drv_info->cpuhp_state) {
1901 cpuhp_remove_state(drv_info->cpuhp_state);
1902 drv_info->cpuhp_state = 0;
1903 }
1904
1905 if (drv_info->notif_pcpu_wq) {
1906 destroy_workqueue(drv_info->notif_pcpu_wq);
1907 drv_info->notif_pcpu_wq = NULL;
1908 }
1909
1910 if (drv_info->sched_recv_irq)
1911 free_percpu_irq(drv_info->sched_recv_irq, drv_info->irq_pcpu);
1912
1913 if (drv_info->notif_pend_irq)
1914 free_percpu_irq(drv_info->notif_pend_irq, drv_info->irq_pcpu);
1915
1916 if (drv_info->irq_pcpu) {
1917 free_percpu(drv_info->irq_pcpu);
1918 drv_info->irq_pcpu = NULL;
1919 }
1920 }
1921
ffa_init_pcpu_irq(void)1922 static int ffa_init_pcpu_irq(void)
1923 {
1924 struct ffa_pcpu_irq __percpu *irq_pcpu;
1925 int ret, cpu;
1926
1927 irq_pcpu = alloc_percpu(struct ffa_pcpu_irq);
1928 if (!irq_pcpu)
1929 return -ENOMEM;
1930
1931 for_each_present_cpu(cpu)
1932 per_cpu_ptr(irq_pcpu, cpu)->info = drv_info;
1933
1934 drv_info->irq_pcpu = irq_pcpu;
1935
1936 if (drv_info->sched_recv_irq) {
1937 ret = request_percpu_irq(drv_info->sched_recv_irq,
1938 ffa_sched_recv_irq_handler,
1939 "ARM-FFA-SRI", irq_pcpu);
1940 if (ret) {
1941 pr_err("Error registering percpu SRI nIRQ %d : %d\n",
1942 drv_info->sched_recv_irq, ret);
1943 drv_info->sched_recv_irq = 0;
1944 return ret;
1945 }
1946 }
1947
1948 if (drv_info->notif_pend_irq) {
1949 ret = request_percpu_irq(drv_info->notif_pend_irq,
1950 notif_pend_irq_handler,
1951 "ARM-FFA-NPI", irq_pcpu);
1952 if (ret) {
1953 pr_err("Error registering percpu NPI nIRQ %d : %d\n",
1954 drv_info->notif_pend_irq, ret);
1955 drv_info->notif_pend_irq = 0;
1956 return ret;
1957 }
1958 }
1959
1960 INIT_WORK(&drv_info->sched_recv_irq_work, ffa_sched_recv_irq_work_fn);
1961 INIT_WORK(&drv_info->notif_pcpu_work, notif_pcpu_irq_work_fn);
1962 drv_info->notif_pcpu_wq = create_workqueue("ffa_pcpu_irq_notification");
1963 if (!drv_info->notif_pcpu_wq)
1964 return -EINVAL;
1965
1966 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "ffa/pcpu-irq:starting",
1967 ffa_cpuhp_pcpu_irq_enable,
1968 ffa_cpuhp_pcpu_irq_disable);
1969
1970 if (ret < 0)
1971 return ret;
1972
1973 drv_info->cpuhp_state = ret;
1974 return 0;
1975 }
1976
ffa_notifications_cleanup(void)1977 static void ffa_notifications_cleanup(void)
1978 {
1979 ffa_uninit_pcpu_irq();
1980 ffa_irq_unmap(drv_info->sched_recv_irq);
1981 drv_info->sched_recv_irq = 0;
1982 ffa_irq_unmap(drv_info->notif_pend_irq);
1983 drv_info->notif_pend_irq = 0;
1984
1985 if (drv_info->bitmap_created) {
1986 ffa_notification_bitmap_destroy();
1987 drv_info->bitmap_created = false;
1988 }
1989 drv_info->notif_enabled = false;
1990 }
1991
ffa_notifications_setup(void)1992 static void ffa_notifications_setup(void)
1993 {
1994 int ret;
1995
1996 ret = ffa_features(FFA_NOTIFICATION_BITMAP_CREATE, 0, NULL, NULL);
1997 if (!ret) {
1998 ret = ffa_notification_bitmap_create();
1999 if (ret) {
2000 pr_err("Notification bitmap create error %d\n", ret);
2001 return;
2002 }
2003
2004 drv_info->bitmap_created = true;
2005 }
2006
2007 ret = ffa_irq_map(FFA_FEAT_SCHEDULE_RECEIVER_INT);
2008 if (ret > 0)
2009 drv_info->sched_recv_irq = ret;
2010
2011 ret = ffa_irq_map(FFA_FEAT_NOTIFICATION_PENDING_INT);
2012 if (ret > 0)
2013 drv_info->notif_pend_irq = ret;
2014
2015 if (!drv_info->sched_recv_irq && !drv_info->notif_pend_irq)
2016 goto cleanup;
2017
2018 ret = ffa_init_pcpu_irq();
2019 if (ret)
2020 goto cleanup;
2021
2022 hash_init(drv_info->notifier_hash);
2023 rwlock_init(&drv_info->notify_lock);
2024
2025 drv_info->notif_enabled = true;
2026 return;
2027 cleanup:
2028 pr_info("Notification setup failed %d, not enabled\n", ret);
2029 ffa_notifications_cleanup();
2030 }
2031
ffa_init(void)2032 static int __init ffa_init(void)
2033 {
2034 int ret;
2035 u32 buf_sz;
2036 size_t rxtx_bufsz = SZ_4K;
2037
2038 ret = ffa_transport_init(&invoke_ffa_fn);
2039 if (ret)
2040 return ret;
2041
2042 drv_info = kzalloc_obj(*drv_info);
2043 if (!drv_info)
2044 return -ENOMEM;
2045
2046 ret = ffa_version_check(&drv_info->version);
2047 if (ret)
2048 goto free_drv_info;
2049
2050 if (ffa_id_get(&drv_info->vm_id)) {
2051 pr_err("failed to obtain VM id for self\n");
2052 ret = -ENODEV;
2053 goto free_drv_info;
2054 }
2055
2056 ret = ffa_features(FFA_FN_NATIVE(RXTX_MAP), 0, &buf_sz, NULL);
2057 if (!ret) {
2058 if (RXTX_MAP_MIN_BUFSZ(buf_sz) == 1)
2059 rxtx_bufsz = SZ_64K;
2060 else if (RXTX_MAP_MIN_BUFSZ(buf_sz) == 2)
2061 rxtx_bufsz = SZ_16K;
2062 else
2063 rxtx_bufsz = SZ_4K;
2064 }
2065
2066 drv_info->rxtx_bufsz = rxtx_bufsz;
2067 drv_info->rx_buffer = alloc_pages_exact(rxtx_bufsz, GFP_KERNEL);
2068 if (!drv_info->rx_buffer) {
2069 ret = -ENOMEM;
2070 goto free_pages;
2071 }
2072
2073 drv_info->tx_buffer = alloc_pages_exact(rxtx_bufsz, GFP_KERNEL);
2074 if (!drv_info->tx_buffer) {
2075 ret = -ENOMEM;
2076 goto free_pages;
2077 }
2078
2079 ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
2080 virt_to_phys(drv_info->rx_buffer),
2081 rxtx_bufsz / FFA_PAGE_SIZE);
2082 if (ret) {
2083 pr_err("failed to register FFA RxTx buffers\n");
2084 goto free_pages;
2085 }
2086
2087 mutex_init(&drv_info->rx_lock);
2088 mutex_init(&drv_info->tx_lock);
2089
2090 ffa_drvinfo_flags_init();
2091
2092 ffa_notifications_setup();
2093
2094 ret = ffa_setup_partitions();
2095 if (!ret)
2096 return ret;
2097
2098 pr_err("failed to setup partitions\n");
2099 ffa_notifications_cleanup();
2100 ffa_rxtx_unmap(drv_info->vm_id);
2101 free_pages:
2102 if (drv_info->tx_buffer)
2103 free_pages_exact(drv_info->tx_buffer, rxtx_bufsz);
2104 free_pages_exact(drv_info->rx_buffer, rxtx_bufsz);
2105 free_drv_info:
2106 kfree(drv_info);
2107 return ret;
2108 }
2109 rootfs_initcall(ffa_init);
2110
ffa_exit(void)2111 static void __exit ffa_exit(void)
2112 {
2113 ffa_notifications_cleanup();
2114 ffa_partitions_cleanup();
2115 ffa_rxtx_unmap(drv_info->vm_id);
2116 free_pages_exact(drv_info->tx_buffer, drv_info->rxtx_bufsz);
2117 free_pages_exact(drv_info->rx_buffer, drv_info->rxtx_bufsz);
2118 kfree(drv_info);
2119 }
2120 module_exit(ffa_exit);
2121
2122 MODULE_ALIAS("arm-ffa");
2123 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
2124 MODULE_DESCRIPTION("Arm FF-A interface driver");
2125 MODULE_LICENSE("GPL v2");
2126