clk: x86: Rename clk-lpt to more specific clk-lpss-atom
[platform/kernel/linux-rpi.git] / drivers / firmware / arm_ffa / driver.c
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/arm_ffa.h>
26 #include <linux/bitfield.h>
27 #include <linux/device.h>
28 #include <linux/io.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/mm.h>
32 #include <linux/scatterlist.h>
33 #include <linux/slab.h>
34 #include <linux/uuid.h>
35
36 #include "common.h"
37
38 #define FFA_DRIVER_VERSION      FFA_VERSION_1_0
39
40 #define FFA_SMC(calling_convention, func_num)                           \
41         ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, (calling_convention),   \
42                            ARM_SMCCC_OWNER_STANDARD, (func_num))
43
44 #define FFA_SMC_32(func_num)    FFA_SMC(ARM_SMCCC_SMC_32, (func_num))
45 #define FFA_SMC_64(func_num)    FFA_SMC(ARM_SMCCC_SMC_64, (func_num))
46
47 #define FFA_ERROR                       FFA_SMC_32(0x60)
48 #define FFA_SUCCESS                     FFA_SMC_32(0x61)
49 #define FFA_INTERRUPT                   FFA_SMC_32(0x62)
50 #define FFA_VERSION                     FFA_SMC_32(0x63)
51 #define FFA_FEATURES                    FFA_SMC_32(0x64)
52 #define FFA_RX_RELEASE                  FFA_SMC_32(0x65)
53 #define FFA_RXTX_MAP                    FFA_SMC_32(0x66)
54 #define FFA_FN64_RXTX_MAP               FFA_SMC_64(0x66)
55 #define FFA_RXTX_UNMAP                  FFA_SMC_32(0x67)
56 #define FFA_PARTITION_INFO_GET          FFA_SMC_32(0x68)
57 #define FFA_ID_GET                      FFA_SMC_32(0x69)
58 #define FFA_MSG_POLL                    FFA_SMC_32(0x6A)
59 #define FFA_MSG_WAIT                    FFA_SMC_32(0x6B)
60 #define FFA_YIELD                       FFA_SMC_32(0x6C)
61 #define FFA_RUN                         FFA_SMC_32(0x6D)
62 #define FFA_MSG_SEND                    FFA_SMC_32(0x6E)
63 #define FFA_MSG_SEND_DIRECT_REQ         FFA_SMC_32(0x6F)
64 #define FFA_FN64_MSG_SEND_DIRECT_REQ    FFA_SMC_64(0x6F)
65 #define FFA_MSG_SEND_DIRECT_RESP        FFA_SMC_32(0x70)
66 #define FFA_FN64_MSG_SEND_DIRECT_RESP   FFA_SMC_64(0x70)
67 #define FFA_MEM_DONATE                  FFA_SMC_32(0x71)
68 #define FFA_FN64_MEM_DONATE             FFA_SMC_64(0x71)
69 #define FFA_MEM_LEND                    FFA_SMC_32(0x72)
70 #define FFA_FN64_MEM_LEND               FFA_SMC_64(0x72)
71 #define FFA_MEM_SHARE                   FFA_SMC_32(0x73)
72 #define FFA_FN64_MEM_SHARE              FFA_SMC_64(0x73)
73 #define FFA_MEM_RETRIEVE_REQ            FFA_SMC_32(0x74)
74 #define FFA_FN64_MEM_RETRIEVE_REQ       FFA_SMC_64(0x74)
75 #define FFA_MEM_RETRIEVE_RESP           FFA_SMC_32(0x75)
76 #define FFA_MEM_RELINQUISH              FFA_SMC_32(0x76)
77 #define FFA_MEM_RECLAIM                 FFA_SMC_32(0x77)
78 #define FFA_MEM_OP_PAUSE                FFA_SMC_32(0x78)
79 #define FFA_MEM_OP_RESUME               FFA_SMC_32(0x79)
80 #define FFA_MEM_FRAG_RX                 FFA_SMC_32(0x7A)
81 #define FFA_MEM_FRAG_TX                 FFA_SMC_32(0x7B)
82 #define FFA_NORMAL_WORLD_RESUME         FFA_SMC_32(0x7C)
83
84 /*
85  * For some calls it is necessary to use SMC64 to pass or return 64-bit values.
86  * For such calls FFA_FN_NATIVE(name) will choose the appropriate
87  * (native-width) function ID.
88  */
89 #ifdef CONFIG_64BIT
90 #define FFA_FN_NATIVE(name)     FFA_FN64_##name
91 #else
92 #define FFA_FN_NATIVE(name)     FFA_##name
93 #endif
94
95 /* FFA error codes. */
96 #define FFA_RET_SUCCESS            (0)
97 #define FFA_RET_NOT_SUPPORTED      (-1)
98 #define FFA_RET_INVALID_PARAMETERS (-2)
99 #define FFA_RET_NO_MEMORY          (-3)
100 #define FFA_RET_BUSY               (-4)
101 #define FFA_RET_INTERRUPTED        (-5)
102 #define FFA_RET_DENIED             (-6)
103 #define FFA_RET_RETRY              (-7)
104 #define FFA_RET_ABORTED            (-8)
105
106 #define MAJOR_VERSION_MASK      GENMASK(30, 16)
107 #define MINOR_VERSION_MASK      GENMASK(15, 0)
108 #define MAJOR_VERSION(x)        ((u16)(FIELD_GET(MAJOR_VERSION_MASK, (x))))
109 #define MINOR_VERSION(x)        ((u16)(FIELD_GET(MINOR_VERSION_MASK, (x))))
110 #define PACK_VERSION_INFO(major, minor)                 \
111         (FIELD_PREP(MAJOR_VERSION_MASK, (major)) |      \
112          FIELD_PREP(MINOR_VERSION_MASK, (minor)))
113 #define FFA_VERSION_1_0         PACK_VERSION_INFO(1, 0)
114 #define FFA_MIN_VERSION         FFA_VERSION_1_0
115
116 #define SENDER_ID_MASK          GENMASK(31, 16)
117 #define RECEIVER_ID_MASK        GENMASK(15, 0)
118 #define SENDER_ID(x)            ((u16)(FIELD_GET(SENDER_ID_MASK, (x))))
119 #define RECEIVER_ID(x)          ((u16)(FIELD_GET(RECEIVER_ID_MASK, (x))))
120 #define PACK_TARGET_INFO(s, r)          \
121         (FIELD_PREP(SENDER_ID_MASK, (s)) | FIELD_PREP(RECEIVER_ID_MASK, (r)))
122
123 /**
124  * FF-A specification mentions explicitly about '4K pages'. This should
125  * not be confused with the kernel PAGE_SIZE, which is the translation
126  * granule kernel is configured and may be one among 4K, 16K and 64K.
127  */
128 #define FFA_PAGE_SIZE           SZ_4K
129 /*
130  * Keeping RX TX buffer size as 4K for now
131  * 64K may be preferred to keep it min a page in 64K PAGE_SIZE config
132  */
133 #define RXTX_BUFFER_SIZE        SZ_4K
134
135 static ffa_fn *invoke_ffa_fn;
136
137 static const int ffa_linux_errmap[] = {
138         /* better than switch case as long as return value is continuous */
139         0,              /* FFA_RET_SUCCESS */
140         -EOPNOTSUPP,    /* FFA_RET_NOT_SUPPORTED */
141         -EINVAL,        /* FFA_RET_INVALID_PARAMETERS */
142         -ENOMEM,        /* FFA_RET_NO_MEMORY */
143         -EBUSY,         /* FFA_RET_BUSY */
144         -EINTR,         /* FFA_RET_INTERRUPTED */
145         -EACCES,        /* FFA_RET_DENIED */
146         -EAGAIN,        /* FFA_RET_RETRY */
147         -ECANCELED,     /* FFA_RET_ABORTED */
148 };
149
150 static inline int ffa_to_linux_errno(int errno)
151 {
152         if (errno < FFA_RET_SUCCESS && errno >= -ARRAY_SIZE(ffa_linux_errmap))
153                 return ffa_linux_errmap[-errno];
154         return -EINVAL;
155 }
156
157 struct ffa_drv_info {
158         u32 version;
159         u16 vm_id;
160         struct mutex rx_lock; /* lock to protect Rx buffer */
161         struct mutex tx_lock; /* lock to protect Tx buffer */
162         void *rx_buffer;
163         void *tx_buffer;
164 };
165
166 static struct ffa_drv_info *drv_info;
167
168 static int ffa_version_check(u32 *version)
169 {
170         ffa_value_t ver;
171
172         invoke_ffa_fn((ffa_value_t){
173                       .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION,
174                       }, &ver);
175
176         if (ver.a0 == FFA_RET_NOT_SUPPORTED) {
177                 pr_info("FFA_VERSION returned not supported\n");
178                 return -EOPNOTSUPP;
179         }
180
181         if (ver.a0 < FFA_MIN_VERSION || ver.a0 > FFA_DRIVER_VERSION) {
182                 pr_err("Incompatible version %d.%d found\n",
183                        MAJOR_VERSION(ver.a0), MINOR_VERSION(ver.a0));
184                 return -EINVAL;
185         }
186
187         *version = ver.a0;
188         pr_info("Version %d.%d found\n", MAJOR_VERSION(ver.a0),
189                 MINOR_VERSION(ver.a0));
190         return 0;
191 }
192
193 static int ffa_rx_release(void)
194 {
195         ffa_value_t ret;
196
197         invoke_ffa_fn((ffa_value_t){
198                       .a0 = FFA_RX_RELEASE,
199                       }, &ret);
200
201         if (ret.a0 == FFA_ERROR)
202                 return ffa_to_linux_errno((int)ret.a2);
203
204         /* check for ret.a0 == FFA_RX_RELEASE ? */
205
206         return 0;
207 }
208
209 static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt)
210 {
211         ffa_value_t ret;
212
213         invoke_ffa_fn((ffa_value_t){
214                       .a0 = FFA_FN_NATIVE(RXTX_MAP),
215                       .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt,
216                       }, &ret);
217
218         if (ret.a0 == FFA_ERROR)
219                 return ffa_to_linux_errno((int)ret.a2);
220
221         return 0;
222 }
223
224 static int ffa_rxtx_unmap(u16 vm_id)
225 {
226         ffa_value_t ret;
227
228         invoke_ffa_fn((ffa_value_t){
229                       .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0),
230                       }, &ret);
231
232         if (ret.a0 == FFA_ERROR)
233                 return ffa_to_linux_errno((int)ret.a2);
234
235         return 0;
236 }
237
238 /* buffer must be sizeof(struct ffa_partition_info) * num_partitions */
239 static int
240 __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
241                          struct ffa_partition_info *buffer, int num_partitions)
242 {
243         int count;
244         ffa_value_t partition_info;
245
246         mutex_lock(&drv_info->rx_lock);
247         invoke_ffa_fn((ffa_value_t){
248                       .a0 = FFA_PARTITION_INFO_GET,
249                       .a1 = uuid0, .a2 = uuid1, .a3 = uuid2, .a4 = uuid3,
250                       }, &partition_info);
251
252         if (partition_info.a0 == FFA_ERROR) {
253                 mutex_unlock(&drv_info->rx_lock);
254                 return ffa_to_linux_errno((int)partition_info.a2);
255         }
256
257         count = partition_info.a2;
258
259         if (buffer && count <= num_partitions)
260                 memcpy(buffer, drv_info->rx_buffer, sizeof(*buffer) * count);
261
262         ffa_rx_release();
263
264         mutex_unlock(&drv_info->rx_lock);
265
266         return count;
267 }
268
269 /* buffer is allocated and caller must free the same if returned count > 0 */
270 static int
271 ffa_partition_probe(const uuid_t *uuid, struct ffa_partition_info **buffer)
272 {
273         int count;
274         u32 uuid0_4[4];
275         struct ffa_partition_info *pbuf;
276
277         export_uuid((u8 *)uuid0_4, uuid);
278         count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
279                                          uuid0_4[3], NULL, 0);
280         if (count <= 0)
281                 return count;
282
283         pbuf = kcalloc(count, sizeof(*pbuf), GFP_KERNEL);
284         if (!pbuf)
285                 return -ENOMEM;
286
287         count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
288                                          uuid0_4[3], pbuf, count);
289         if (count <= 0)
290                 kfree(pbuf);
291         else
292                 *buffer = pbuf;
293
294         return count;
295 }
296
297 #define VM_ID_MASK      GENMASK(15, 0)
298 static int ffa_id_get(u16 *vm_id)
299 {
300         ffa_value_t id;
301
302         invoke_ffa_fn((ffa_value_t){
303                       .a0 = FFA_ID_GET,
304                       }, &id);
305
306         if (id.a0 == FFA_ERROR)
307                 return ffa_to_linux_errno((int)id.a2);
308
309         *vm_id = FIELD_GET(VM_ID_MASK, (id.a2));
310
311         return 0;
312 }
313
314 static int ffa_msg_send_direct_req(u16 src_id, u16 dst_id, bool mode_32bit,
315                                    struct ffa_send_direct_data *data)
316 {
317         u32 req_id, resp_id, src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
318         ffa_value_t ret;
319
320         if (mode_32bit) {
321                 req_id = FFA_MSG_SEND_DIRECT_REQ;
322                 resp_id = FFA_MSG_SEND_DIRECT_RESP;
323         } else {
324                 req_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_REQ);
325                 resp_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_RESP);
326         }
327
328         invoke_ffa_fn((ffa_value_t){
329                       .a0 = req_id, .a1 = src_dst_ids, .a2 = 0,
330                       .a3 = data->data0, .a4 = data->data1, .a5 = data->data2,
331                       .a6 = data->data3, .a7 = data->data4,
332                       }, &ret);
333
334         while (ret.a0 == FFA_INTERRUPT)
335                 invoke_ffa_fn((ffa_value_t){
336                               .a0 = FFA_RUN, .a1 = ret.a1,
337                               }, &ret);
338
339         if (ret.a0 == FFA_ERROR)
340                 return ffa_to_linux_errno((int)ret.a2);
341
342         if (ret.a0 == resp_id) {
343                 data->data0 = ret.a3;
344                 data->data1 = ret.a4;
345                 data->data2 = ret.a5;
346                 data->data3 = ret.a6;
347                 data->data4 = ret.a7;
348                 return 0;
349         }
350
351         return -EINVAL;
352 }
353
354 static int ffa_mem_first_frag(u32 func_id, phys_addr_t buf, u32 buf_sz,
355                               u32 frag_len, u32 len, u64 *handle)
356 {
357         ffa_value_t ret;
358
359         invoke_ffa_fn((ffa_value_t){
360                       .a0 = func_id, .a1 = len, .a2 = frag_len,
361                       .a3 = buf, .a4 = buf_sz,
362                       }, &ret);
363
364         while (ret.a0 == FFA_MEM_OP_PAUSE)
365                 invoke_ffa_fn((ffa_value_t){
366                               .a0 = FFA_MEM_OP_RESUME,
367                               .a1 = ret.a1, .a2 = ret.a2,
368                               }, &ret);
369
370         if (ret.a0 == FFA_ERROR)
371                 return ffa_to_linux_errno((int)ret.a2);
372
373         if (ret.a0 != FFA_SUCCESS)
374                 return -EOPNOTSUPP;
375
376         if (handle)
377                 *handle = PACK_HANDLE(ret.a2, ret.a3);
378
379         return frag_len;
380 }
381
382 static int ffa_mem_next_frag(u64 handle, u32 frag_len)
383 {
384         ffa_value_t ret;
385
386         invoke_ffa_fn((ffa_value_t){
387                       .a0 = FFA_MEM_FRAG_TX,
388                       .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle),
389                       .a3 = frag_len,
390                       }, &ret);
391
392         while (ret.a0 == FFA_MEM_OP_PAUSE)
393                 invoke_ffa_fn((ffa_value_t){
394                               .a0 = FFA_MEM_OP_RESUME,
395                               .a1 = ret.a1, .a2 = ret.a2,
396                               }, &ret);
397
398         if (ret.a0 == FFA_ERROR)
399                 return ffa_to_linux_errno((int)ret.a2);
400
401         if (ret.a0 != FFA_MEM_FRAG_RX)
402                 return -EOPNOTSUPP;
403
404         return ret.a3;
405 }
406
407 static int
408 ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len,
409                       u32 len, u64 *handle, bool first)
410 {
411         if (!first)
412                 return ffa_mem_next_frag(*handle, frag_len);
413
414         return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle);
415 }
416
417 static u32 ffa_get_num_pages_sg(struct scatterlist *sg)
418 {
419         u32 num_pages = 0;
420
421         do {
422                 num_pages += sg->length / FFA_PAGE_SIZE;
423         } while ((sg = sg_next(sg)));
424
425         return num_pages;
426 }
427
428 static int
429 ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
430                        struct ffa_mem_ops_args *args)
431 {
432         int rc = 0;
433         bool first = true;
434         phys_addr_t addr = 0;
435         struct ffa_composite_mem_region *composite;
436         struct ffa_mem_region_addr_range *constituents;
437         struct ffa_mem_region_attributes *ep_mem_access;
438         struct ffa_mem_region *mem_region = buffer;
439         u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg);
440
441         mem_region->tag = args->tag;
442         mem_region->flags = args->flags;
443         mem_region->sender_id = drv_info->vm_id;
444         mem_region->attributes = FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK |
445                                  FFA_MEM_INNER_SHAREABLE;
446         ep_mem_access = &mem_region->ep_mem_access[0];
447
448         for (idx = 0; idx < args->nattrs; idx++, ep_mem_access++) {
449                 ep_mem_access->receiver = args->attrs[idx].receiver;
450                 ep_mem_access->attrs = args->attrs[idx].attrs;
451                 ep_mem_access->composite_off = COMPOSITE_OFFSET(args->nattrs);
452         }
453         mem_region->ep_count = args->nattrs;
454
455         composite = buffer + COMPOSITE_OFFSET(args->nattrs);
456         composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg);
457         composite->addr_range_cnt = num_entries;
458
459         length = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, num_entries);
460         frag_len = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, 0);
461         if (frag_len > max_fragsize)
462                 return -ENXIO;
463
464         if (!args->use_txbuf) {
465                 addr = virt_to_phys(buffer);
466                 buf_sz = max_fragsize / FFA_PAGE_SIZE;
467         }
468
469         constituents = buffer + frag_len;
470         idx = 0;
471         do {
472                 if (frag_len == max_fragsize) {
473                         rc = ffa_transmit_fragment(func_id, addr, buf_sz,
474                                                    frag_len, length,
475                                                    &args->g_handle, first);
476                         if (rc < 0)
477                                 return -ENXIO;
478
479                         first = false;
480                         idx = 0;
481                         frag_len = 0;
482                         constituents = buffer;
483                 }
484
485                 if ((void *)constituents - buffer > max_fragsize) {
486                         pr_err("Memory Region Fragment > Tx Buffer size\n");
487                         return -EFAULT;
488                 }
489
490                 constituents->address = sg_phys(args->sg);
491                 constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE;
492                 constituents++;
493                 frag_len += sizeof(struct ffa_mem_region_addr_range);
494         } while ((args->sg = sg_next(args->sg)));
495
496         return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len,
497                                      length, &args->g_handle, first);
498 }
499
500 static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args)
501 {
502         int ret;
503         void *buffer;
504
505         if (!args->use_txbuf) {
506                 buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
507                 if (!buffer)
508                         return -ENOMEM;
509         } else {
510                 buffer = drv_info->tx_buffer;
511                 mutex_lock(&drv_info->tx_lock);
512         }
513
514         ret = ffa_setup_and_transmit(func_id, buffer, RXTX_BUFFER_SIZE, args);
515
516         if (args->use_txbuf)
517                 mutex_unlock(&drv_info->tx_lock);
518         else
519                 free_pages_exact(buffer, RXTX_BUFFER_SIZE);
520
521         return ret < 0 ? ret : 0;
522 }
523
524 static int ffa_memory_reclaim(u64 g_handle, u32 flags)
525 {
526         ffa_value_t ret;
527
528         invoke_ffa_fn((ffa_value_t){
529                       .a0 = FFA_MEM_RECLAIM,
530                       .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle),
531                       .a3 = flags,
532                       }, &ret);
533
534         if (ret.a0 == FFA_ERROR)
535                 return ffa_to_linux_errno((int)ret.a2);
536
537         return 0;
538 }
539
540 static u32 ffa_api_version_get(void)
541 {
542         return drv_info->version;
543 }
544
545 static int ffa_partition_info_get(const char *uuid_str,
546                                   struct ffa_partition_info *buffer)
547 {
548         int count;
549         uuid_t uuid;
550         struct ffa_partition_info *pbuf;
551
552         if (uuid_parse(uuid_str, &uuid)) {
553                 pr_err("invalid uuid (%s)\n", uuid_str);
554                 return -ENODEV;
555         }
556
557         count = ffa_partition_probe(&uuid_null, &pbuf);
558         if (count <= 0)
559                 return -ENOENT;
560
561         memcpy(buffer, pbuf, sizeof(*pbuf) * count);
562         kfree(pbuf);
563         return 0;
564 }
565
566 static void ffa_mode_32bit_set(struct ffa_device *dev)
567 {
568         dev->mode_32bit = true;
569 }
570
571 static int ffa_sync_send_receive(struct ffa_device *dev,
572                                  struct ffa_send_direct_data *data)
573 {
574         return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id,
575                                        dev->mode_32bit, data);
576 }
577
578 static int
579 ffa_memory_share(struct ffa_device *dev, struct ffa_mem_ops_args *args)
580 {
581         if (dev->mode_32bit)
582                 return ffa_memory_ops(FFA_MEM_SHARE, args);
583
584         return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args);
585 }
586
587 static const struct ffa_dev_ops ffa_ops = {
588         .api_version_get = ffa_api_version_get,
589         .partition_info_get = ffa_partition_info_get,
590         .mode_32bit_set = ffa_mode_32bit_set,
591         .sync_send_receive = ffa_sync_send_receive,
592         .memory_reclaim = ffa_memory_reclaim,
593         .memory_share = ffa_memory_share,
594 };
595
596 const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev)
597 {
598         if (ffa_device_is_valid(dev))
599                 return &ffa_ops;
600
601         return NULL;
602 }
603 EXPORT_SYMBOL_GPL(ffa_dev_ops_get);
604
605 void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid)
606 {
607         int count, idx;
608         struct ffa_partition_info *pbuf, *tpbuf;
609
610         count = ffa_partition_probe(uuid, &pbuf);
611         if (count <= 0)
612                 return;
613
614         for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++)
615                 if (tpbuf->id == ffa_dev->vm_id)
616                         uuid_copy(&ffa_dev->uuid, uuid);
617         kfree(pbuf);
618 }
619
620 static void ffa_setup_partitions(void)
621 {
622         int count, idx;
623         struct ffa_device *ffa_dev;
624         struct ffa_partition_info *pbuf, *tpbuf;
625
626         count = ffa_partition_probe(&uuid_null, &pbuf);
627         if (count <= 0) {
628                 pr_info("%s: No partitions found, error %d\n", __func__, count);
629                 return;
630         }
631
632         for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) {
633                 /* Note that the &uuid_null parameter will require
634                  * ffa_device_match() to find the UUID of this partition id
635                  * with help of ffa_device_match_uuid(). Once the FF-A spec
636                  * is updated to provide correct UUID here for each partition
637                  * as part of the discovery API, we need to pass the
638                  * discovered UUID here instead.
639                  */
640                 ffa_dev = ffa_device_register(&uuid_null, tpbuf->id);
641                 if (!ffa_dev) {
642                         pr_err("%s: failed to register partition ID 0x%x\n",
643                                __func__, tpbuf->id);
644                         continue;
645                 }
646
647                 ffa_dev_set_drvdata(ffa_dev, drv_info);
648         }
649         kfree(pbuf);
650 }
651
652 static int __init ffa_init(void)
653 {
654         int ret;
655
656         ret = ffa_transport_init(&invoke_ffa_fn);
657         if (ret)
658                 return ret;
659
660         ret = arm_ffa_bus_init();
661         if (ret)
662                 return ret;
663
664         drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL);
665         if (!drv_info) {
666                 ret = -ENOMEM;
667                 goto ffa_bus_exit;
668         }
669
670         ret = ffa_version_check(&drv_info->version);
671         if (ret)
672                 goto free_drv_info;
673
674         if (ffa_id_get(&drv_info->vm_id)) {
675                 pr_err("failed to obtain VM id for self\n");
676                 ret = -ENODEV;
677                 goto free_drv_info;
678         }
679
680         drv_info->rx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
681         if (!drv_info->rx_buffer) {
682                 ret = -ENOMEM;
683                 goto free_pages;
684         }
685
686         drv_info->tx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
687         if (!drv_info->tx_buffer) {
688                 ret = -ENOMEM;
689                 goto free_pages;
690         }
691
692         ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
693                            virt_to_phys(drv_info->rx_buffer),
694                            RXTX_BUFFER_SIZE / FFA_PAGE_SIZE);
695         if (ret) {
696                 pr_err("failed to register FFA RxTx buffers\n");
697                 goto free_pages;
698         }
699
700         mutex_init(&drv_info->rx_lock);
701         mutex_init(&drv_info->tx_lock);
702
703         ffa_setup_partitions();
704
705         return 0;
706 free_pages:
707         if (drv_info->tx_buffer)
708                 free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
709         free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
710 free_drv_info:
711         kfree(drv_info);
712 ffa_bus_exit:
713         arm_ffa_bus_exit();
714         return ret;
715 }
716 subsys_initcall(ffa_init);
717
718 static void __exit ffa_exit(void)
719 {
720         ffa_rxtx_unmap(drv_info->vm_id);
721         free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
722         free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
723         kfree(drv_info);
724         arm_ffa_bus_exit();
725 }
726 module_exit(ffa_exit);
727
728 MODULE_ALIAS("arm-ffa");
729 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
730 MODULE_DESCRIPTION("Arm FF-A interface driver");
731 MODULE_LICENSE("GPL v2");