This source file includes following definitions.
- scmi_to_linux_errno
- scmi_dump_header_dbg
- scmi_fetch_response
- pack_scmi_header
- unpack_scmi_header
- scmi_tx_prepare
- scmi_xfer_get
- __scmi_xfer_put
- scmi_rx_callback
- scmi_xfer_put
- scmi_xfer_poll_done
- scmi_xfer_done_no_timeout
- scmi_do_xfer
- scmi_do_xfer_with_response
- scmi_xfer_get_init
- scmi_version_get
- scmi_setup_protocol_implemented
- scmi_is_protocol_implemented
- scmi_handle_get
- scmi_handle_put
- scmi_xfer_info_init
- scmi_mailbox_check
- scmi_mbox_chan_setup
- scmi_mbox_txrx_setup
- scmi_create_protocol_device
- scmi_probe
- scmi_mbox_free_channel
- scmi_remove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <linux/bitmap.h>
18 #include <linux/export.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/ktime.h>
22 #include <linux/mailbox_client.h>
23 #include <linux/module.h>
24 #include <linux/of_address.h>
25 #include <linux/of_device.h>
26 #include <linux/processor.h>
27 #include <linux/semaphore.h>
28 #include <linux/slab.h>
29
30 #include "common.h"
31
32 #define MSG_ID_MASK GENMASK(7, 0)
33 #define MSG_XTRACT_ID(hdr) FIELD_GET(MSG_ID_MASK, (hdr))
34 #define MSG_TYPE_MASK GENMASK(9, 8)
35 #define MSG_XTRACT_TYPE(hdr) FIELD_GET(MSG_TYPE_MASK, (hdr))
36 #define MSG_TYPE_COMMAND 0
37 #define MSG_TYPE_DELAYED_RESP 2
38 #define MSG_TYPE_NOTIFICATION 3
39 #define MSG_PROTOCOL_ID_MASK GENMASK(17, 10)
40 #define MSG_XTRACT_PROT_ID(hdr) FIELD_GET(MSG_PROTOCOL_ID_MASK, (hdr))
41 #define MSG_TOKEN_ID_MASK GENMASK(27, 18)
42 #define MSG_XTRACT_TOKEN(hdr) FIELD_GET(MSG_TOKEN_ID_MASK, (hdr))
43 #define MSG_TOKEN_MAX (MSG_XTRACT_TOKEN(MSG_TOKEN_ID_MASK) + 1)
44
45 enum scmi_error_codes {
46 SCMI_SUCCESS = 0,
47 SCMI_ERR_SUPPORT = -1,
48 SCMI_ERR_PARAMS = -2,
49 SCMI_ERR_ACCESS = -3,
50 SCMI_ERR_ENTRY = -4,
51 SCMI_ERR_RANGE = -5,
52 SCMI_ERR_BUSY = -6,
53 SCMI_ERR_COMMS = -7,
54 SCMI_ERR_GENERIC = -8,
55 SCMI_ERR_HARDWARE = -9,
56 SCMI_ERR_PROTOCOL = -10,
57 SCMI_ERR_MAX
58 };
59
60
61 static LIST_HEAD(scmi_list);
62
63 static DEFINE_MUTEX(scmi_list_mutex);
64
65
66
67
68
69
70
71
72
73
74 struct scmi_xfers_info {
75 struct scmi_xfer *xfer_block;
76 unsigned long *xfer_alloc_table;
77 spinlock_t xfer_lock;
78 };
79
80
81
82
83
84
85
86
87
88 struct scmi_desc {
89 int max_rx_timeout_ms;
90 int max_msg;
91 int max_msg_size;
92 };
93
94
95
96
97
98
99
100
101
102
103
104 struct scmi_chan_info {
105 struct mbox_client cl;
106 struct mbox_chan *chan;
107 void __iomem *payload;
108 struct device *dev;
109 struct scmi_handle *handle;
110 };
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 struct scmi_info {
129 struct device *dev;
130 const struct scmi_desc *desc;
131 struct scmi_revision_info version;
132 struct scmi_handle handle;
133 struct scmi_xfers_info tx_minfo;
134 struct idr tx_idr;
135 struct idr rx_idr;
136 u8 *protocols_imp;
137 struct list_head node;
138 int users;
139 };
140
141 #define client_to_scmi_chan_info(c) container_of(c, struct scmi_chan_info, cl)
142 #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
143
144
145
146
147
148
149 struct scmi_shared_mem {
150 __le32 reserved;
151 __le32 channel_status;
152 #define SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR BIT(1)
153 #define SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE BIT(0)
154 __le32 reserved1[2];
155 __le32 flags;
156 #define SCMI_SHMEM_FLAG_INTR_ENABLED BIT(0)
157 __le32 length;
158 __le32 msg_header;
159 u8 msg_payload[0];
160 };
161
162 static const int scmi_linux_errmap[] = {
163
164 0,
165 -EOPNOTSUPP,
166 -EINVAL,
167 -EACCES,
168 -ENOENT,
169 -ERANGE,
170 -EBUSY,
171 -ECOMM,
172 -EIO,
173 -EREMOTEIO,
174 -EPROTO,
175 };
176
177 static inline int scmi_to_linux_errno(int errno)
178 {
179 if (errno < SCMI_SUCCESS && errno > SCMI_ERR_MAX)
180 return scmi_linux_errmap[-errno];
181 return -EIO;
182 }
183
184
185
186
187
188
189
190 static inline void scmi_dump_header_dbg(struct device *dev,
191 struct scmi_msg_hdr *hdr)
192 {
193 dev_dbg(dev, "Message ID: %x Sequence ID: %x Protocol: %x\n",
194 hdr->id, hdr->seq, hdr->protocol_id);
195 }
196
197 static void scmi_fetch_response(struct scmi_xfer *xfer,
198 struct scmi_shared_mem __iomem *mem)
199 {
200 xfer->hdr.status = ioread32(mem->msg_payload);
201
202 xfer->rx.len = min_t(size_t, xfer->rx.len, ioread32(&mem->length) - 8);
203
204
205 memcpy_fromio(xfer->rx.buf, mem->msg_payload + 4, xfer->rx.len);
206 }
207
208
209
210
211
212
213
214
215
216 static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr)
217 {
218 return FIELD_PREP(MSG_ID_MASK, hdr->id) |
219 FIELD_PREP(MSG_TOKEN_ID_MASK, hdr->seq) |
220 FIELD_PREP(MSG_PROTOCOL_ID_MASK, hdr->protocol_id);
221 }
222
223
224
225
226
227
228
229 static inline void unpack_scmi_header(u32 msg_hdr, struct scmi_msg_hdr *hdr)
230 {
231 hdr->id = MSG_XTRACT_ID(msg_hdr);
232 hdr->protocol_id = MSG_XTRACT_PROT_ID(msg_hdr);
233 }
234
235
236
237
238
239
240
241
242
243
244 static void scmi_tx_prepare(struct mbox_client *cl, void *m)
245 {
246 struct scmi_xfer *t = m;
247 struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl);
248 struct scmi_shared_mem __iomem *mem = cinfo->payload;
249
250
251
252
253
254
255
256 spin_until_cond(ioread32(&mem->channel_status) &
257 SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
258
259 iowrite32(0x0, &mem->channel_status);
260 iowrite32(t->hdr.poll_completion ? 0 : SCMI_SHMEM_FLAG_INTR_ENABLED,
261 &mem->flags);
262 iowrite32(sizeof(mem->msg_header) + t->tx.len, &mem->length);
263 iowrite32(pack_scmi_header(&t->hdr), &mem->msg_header);
264 if (t->tx.buf)
265 memcpy_toio(mem->msg_payload, t->tx.buf, t->tx.len);
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
284 struct scmi_xfers_info *minfo)
285 {
286 u16 xfer_id;
287 struct scmi_xfer *xfer;
288 unsigned long flags, bit_pos;
289 struct scmi_info *info = handle_to_scmi_info(handle);
290
291
292 spin_lock_irqsave(&minfo->xfer_lock, flags);
293 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
294 info->desc->max_msg);
295 if (bit_pos == info->desc->max_msg) {
296 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
297 return ERR_PTR(-ENOMEM);
298 }
299 set_bit(bit_pos, minfo->xfer_alloc_table);
300 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
301
302 xfer_id = bit_pos;
303
304 xfer = &minfo->xfer_block[xfer_id];
305 xfer->hdr.seq = xfer_id;
306 reinit_completion(&xfer->done);
307
308 return xfer;
309 }
310
311
312
313
314
315
316
317
318
319 static void
320 __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
321 {
322 unsigned long flags;
323
324
325
326
327
328
329 spin_lock_irqsave(&minfo->xfer_lock, flags);
330 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
331 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
332 }
333
334
335
336
337
338
339
340
341
342
343
344
345
346 static void scmi_rx_callback(struct mbox_client *cl, void *m)
347 {
348 u8 msg_type;
349 u32 msg_hdr;
350 u16 xfer_id;
351 struct scmi_xfer *xfer;
352 struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl);
353 struct device *dev = cinfo->dev;
354 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
355 struct scmi_xfers_info *minfo = &info->tx_minfo;
356 struct scmi_shared_mem __iomem *mem = cinfo->payload;
357
358 msg_hdr = ioread32(&mem->msg_header);
359 msg_type = MSG_XTRACT_TYPE(msg_hdr);
360 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
361
362 if (msg_type == MSG_TYPE_NOTIFICATION)
363 return;
364
365
366 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
367 dev_err(dev, "message for %d is not expected!\n", xfer_id);
368 return;
369 }
370
371 xfer = &minfo->xfer_block[xfer_id];
372
373 scmi_dump_header_dbg(dev, &xfer->hdr);
374
375 scmi_fetch_response(xfer, mem);
376
377 if (msg_type == MSG_TYPE_DELAYED_RESP)
378 complete(xfer->async_done);
379 else
380 complete(&xfer->done);
381 }
382
383
384
385
386
387
388
389 void scmi_xfer_put(const struct scmi_handle *handle, struct scmi_xfer *xfer)
390 {
391 struct scmi_info *info = handle_to_scmi_info(handle);
392
393 __scmi_xfer_put(&info->tx_minfo, xfer);
394 }
395
396 static bool
397 scmi_xfer_poll_done(const struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
398 {
399 struct scmi_shared_mem __iomem *mem = cinfo->payload;
400 u16 xfer_id = MSG_XTRACT_TOKEN(ioread32(&mem->msg_header));
401
402 if (xfer->hdr.seq != xfer_id)
403 return false;
404
405 return ioread32(&mem->channel_status) &
406 (SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR |
407 SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
408 }
409
410 #define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC)
411
412 static bool scmi_xfer_done_no_timeout(const struct scmi_chan_info *cinfo,
413 struct scmi_xfer *xfer, ktime_t stop)
414 {
415 ktime_t __cur = ktime_get();
416
417 return scmi_xfer_poll_done(cinfo, xfer) || ktime_after(__cur, stop);
418 }
419
420
421
422
423
424
425
426
427
428
429
430 int scmi_do_xfer(const struct scmi_handle *handle, struct scmi_xfer *xfer)
431 {
432 int ret;
433 int timeout;
434 struct scmi_info *info = handle_to_scmi_info(handle);
435 struct device *dev = info->dev;
436 struct scmi_chan_info *cinfo;
437
438 cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id);
439 if (unlikely(!cinfo))
440 return -EINVAL;
441
442 ret = mbox_send_message(cinfo->chan, xfer);
443 if (ret < 0) {
444 dev_dbg(dev, "mbox send fail %d\n", ret);
445 return ret;
446 }
447
448
449 ret = 0;
450
451 if (xfer->hdr.poll_completion) {
452 ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS);
453
454 spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop));
455
456 if (ktime_before(ktime_get(), stop))
457 scmi_fetch_response(xfer, cinfo->payload);
458 else
459 ret = -ETIMEDOUT;
460 } else {
461
462 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
463 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
464 dev_err(dev, "mbox timed out in resp(caller: %pS)\n",
465 (void *)_RET_IP_);
466 ret = -ETIMEDOUT;
467 }
468 }
469
470 if (!ret && xfer->hdr.status)
471 ret = scmi_to_linux_errno(xfer->hdr.status);
472
473
474
475
476
477
478
479 mbox_client_txdone(cinfo->chan, ret);
480
481 return ret;
482 }
483
484 #define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC)
485
486
487
488
489
490
491
492
493
494
495
496 int scmi_do_xfer_with_response(const struct scmi_handle *handle,
497 struct scmi_xfer *xfer)
498 {
499 int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
500 DECLARE_COMPLETION_ONSTACK(async_response);
501
502 xfer->async_done = &async_response;
503
504 ret = scmi_do_xfer(handle, xfer);
505 if (!ret && !wait_for_completion_timeout(xfer->async_done, timeout))
506 ret = -ETIMEDOUT;
507
508 xfer->async_done = NULL;
509 return ret;
510 }
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528 int scmi_xfer_get_init(const struct scmi_handle *handle, u8 msg_id, u8 prot_id,
529 size_t tx_size, size_t rx_size, struct scmi_xfer **p)
530 {
531 int ret;
532 struct scmi_xfer *xfer;
533 struct scmi_info *info = handle_to_scmi_info(handle);
534 struct scmi_xfers_info *minfo = &info->tx_minfo;
535 struct device *dev = info->dev;
536
537
538 if (rx_size > info->desc->max_msg_size ||
539 tx_size > info->desc->max_msg_size)
540 return -ERANGE;
541
542 xfer = scmi_xfer_get(handle, minfo);
543 if (IS_ERR(xfer)) {
544 ret = PTR_ERR(xfer);
545 dev_err(dev, "failed to get free message slot(%d)\n", ret);
546 return ret;
547 }
548
549 xfer->tx.len = tx_size;
550 xfer->rx.len = rx_size ? : info->desc->max_msg_size;
551 xfer->hdr.id = msg_id;
552 xfer->hdr.protocol_id = prot_id;
553 xfer->hdr.poll_completion = false;
554
555 *p = xfer;
556
557 return 0;
558 }
559
560
561
562
563
564
565
566
567
568
569
570
571 int scmi_version_get(const struct scmi_handle *handle, u8 protocol,
572 u32 *version)
573 {
574 int ret;
575 __le32 *rev_info;
576 struct scmi_xfer *t;
577
578 ret = scmi_xfer_get_init(handle, PROTOCOL_VERSION, protocol, 0,
579 sizeof(*version), &t);
580 if (ret)
581 return ret;
582
583 ret = scmi_do_xfer(handle, t);
584 if (!ret) {
585 rev_info = t->rx.buf;
586 *version = le32_to_cpu(*rev_info);
587 }
588
589 scmi_xfer_put(handle, t);
590 return ret;
591 }
592
593 void scmi_setup_protocol_implemented(const struct scmi_handle *handle,
594 u8 *prot_imp)
595 {
596 struct scmi_info *info = handle_to_scmi_info(handle);
597
598 info->protocols_imp = prot_imp;
599 }
600
601 static bool
602 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
603 {
604 int i;
605 struct scmi_info *info = handle_to_scmi_info(handle);
606
607 if (!info->protocols_imp)
608 return false;
609
610 for (i = 0; i < MAX_PROTOCOLS_IMP; i++)
611 if (info->protocols_imp[i] == prot_id)
612 return true;
613 return false;
614 }
615
616
617
618
619
620
621
622
623
624
625
626
627 struct scmi_handle *scmi_handle_get(struct device *dev)
628 {
629 struct list_head *p;
630 struct scmi_info *info;
631 struct scmi_handle *handle = NULL;
632
633 mutex_lock(&scmi_list_mutex);
634 list_for_each(p, &scmi_list) {
635 info = list_entry(p, struct scmi_info, node);
636 if (dev->parent == info->dev) {
637 handle = &info->handle;
638 info->users++;
639 break;
640 }
641 }
642 mutex_unlock(&scmi_list_mutex);
643
644 return handle;
645 }
646
647
648
649
650
651
652
653
654
655
656
657
658
659 int scmi_handle_put(const struct scmi_handle *handle)
660 {
661 struct scmi_info *info;
662
663 if (!handle)
664 return -EINVAL;
665
666 info = handle_to_scmi_info(handle);
667 mutex_lock(&scmi_list_mutex);
668 if (!WARN_ON(!info->users))
669 info->users--;
670 mutex_unlock(&scmi_list_mutex);
671
672 return 0;
673 }
674
675 static int scmi_xfer_info_init(struct scmi_info *sinfo)
676 {
677 int i;
678 struct scmi_xfer *xfer;
679 struct device *dev = sinfo->dev;
680 const struct scmi_desc *desc = sinfo->desc;
681 struct scmi_xfers_info *info = &sinfo->tx_minfo;
682
683
684 if (WARN_ON(desc->max_msg >= MSG_TOKEN_MAX)) {
685 dev_err(dev, "Maximum message of %d exceeds supported %ld\n",
686 desc->max_msg, MSG_TOKEN_MAX);
687 return -EINVAL;
688 }
689
690 info->xfer_block = devm_kcalloc(dev, desc->max_msg,
691 sizeof(*info->xfer_block), GFP_KERNEL);
692 if (!info->xfer_block)
693 return -ENOMEM;
694
695 info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(desc->max_msg),
696 sizeof(long), GFP_KERNEL);
697 if (!info->xfer_alloc_table)
698 return -ENOMEM;
699
700
701 for (i = 0, xfer = info->xfer_block; i < desc->max_msg; i++, xfer++) {
702 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
703 GFP_KERNEL);
704 if (!xfer->rx.buf)
705 return -ENOMEM;
706
707 xfer->tx.buf = xfer->rx.buf;
708 init_completion(&xfer->done);
709 }
710
711 spin_lock_init(&info->xfer_lock);
712
713 return 0;
714 }
715
716 static int scmi_mailbox_check(struct device_node *np, int idx)
717 {
718 return of_parse_phandle_with_args(np, "mboxes", "#mbox-cells",
719 idx, NULL);
720 }
721
722 static int scmi_mbox_chan_setup(struct scmi_info *info, struct device *dev,
723 int prot_id, bool tx)
724 {
725 int ret, idx;
726 struct resource res;
727 resource_size_t size;
728 struct device_node *shmem, *np = dev->of_node;
729 struct scmi_chan_info *cinfo;
730 struct mbox_client *cl;
731 struct idr *idr;
732 const char *desc = tx ? "Tx" : "Rx";
733
734
735 idx = tx ? 0 : 1;
736 idr = tx ? &info->tx_idr : &info->rx_idr;
737
738 if (scmi_mailbox_check(np, idx)) {
739 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
740 if (unlikely(!cinfo))
741 return -EINVAL;
742 goto idr_alloc;
743 }
744
745 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
746 if (!cinfo)
747 return -ENOMEM;
748
749 cinfo->dev = dev;
750
751 cl = &cinfo->cl;
752 cl->dev = dev;
753 cl->rx_callback = scmi_rx_callback;
754 cl->tx_prepare = tx ? scmi_tx_prepare : NULL;
755 cl->tx_block = false;
756 cl->knows_txdone = tx;
757
758 shmem = of_parse_phandle(np, "shmem", idx);
759 ret = of_address_to_resource(shmem, 0, &res);
760 of_node_put(shmem);
761 if (ret) {
762 dev_err(dev, "failed to get SCMI %s payload memory\n", desc);
763 return ret;
764 }
765
766 size = resource_size(&res);
767 cinfo->payload = devm_ioremap(info->dev, res.start, size);
768 if (!cinfo->payload) {
769 dev_err(dev, "failed to ioremap SCMI %s payload\n", desc);
770 return -EADDRNOTAVAIL;
771 }
772
773 cinfo->chan = mbox_request_channel(cl, idx);
774 if (IS_ERR(cinfo->chan)) {
775 ret = PTR_ERR(cinfo->chan);
776 if (ret != -EPROBE_DEFER)
777 dev_err(dev, "failed to request SCMI %s mailbox\n",
778 desc);
779 return ret;
780 }
781
782 idr_alloc:
783 ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
784 if (ret != prot_id) {
785 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
786 return ret;
787 }
788
789 cinfo->handle = &info->handle;
790 return 0;
791 }
792
793 static inline int
794 scmi_mbox_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id)
795 {
796 int ret = scmi_mbox_chan_setup(info, dev, prot_id, true);
797
798 if (!ret)
799 scmi_mbox_chan_setup(info, dev, prot_id, false);
800
801 return ret;
802 }
803
804 static inline void
805 scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
806 int prot_id)
807 {
808 struct scmi_device *sdev;
809
810 sdev = scmi_device_create(np, info->dev, prot_id);
811 if (!sdev) {
812 dev_err(info->dev, "failed to create %d protocol device\n",
813 prot_id);
814 return;
815 }
816
817 if (scmi_mbox_txrx_setup(info, &sdev->dev, prot_id)) {
818 dev_err(&sdev->dev, "failed to setup transport\n");
819 scmi_device_destroy(sdev);
820 return;
821 }
822
823
824 scmi_set_handle(sdev);
825 }
826
827 static int scmi_probe(struct platform_device *pdev)
828 {
829 int ret;
830 struct scmi_handle *handle;
831 const struct scmi_desc *desc;
832 struct scmi_info *info;
833 struct device *dev = &pdev->dev;
834 struct device_node *child, *np = dev->of_node;
835
836
837 if (scmi_mailbox_check(np, 0)) {
838 dev_err(dev, "no mailbox found in %pOF\n", np);
839 return -EINVAL;
840 }
841
842 desc = of_device_get_match_data(dev);
843 if (!desc)
844 return -EINVAL;
845
846 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
847 if (!info)
848 return -ENOMEM;
849
850 info->dev = dev;
851 info->desc = desc;
852 INIT_LIST_HEAD(&info->node);
853
854 ret = scmi_xfer_info_init(info);
855 if (ret)
856 return ret;
857
858 platform_set_drvdata(pdev, info);
859 idr_init(&info->tx_idr);
860 idr_init(&info->rx_idr);
861
862 handle = &info->handle;
863 handle->dev = info->dev;
864 handle->version = &info->version;
865
866 ret = scmi_mbox_txrx_setup(info, dev, SCMI_PROTOCOL_BASE);
867 if (ret)
868 return ret;
869
870 ret = scmi_base_protocol_init(handle);
871 if (ret) {
872 dev_err(dev, "unable to communicate with SCMI(%d)\n", ret);
873 return ret;
874 }
875
876 mutex_lock(&scmi_list_mutex);
877 list_add_tail(&info->node, &scmi_list);
878 mutex_unlock(&scmi_list_mutex);
879
880 for_each_available_child_of_node(np, child) {
881 u32 prot_id;
882
883 if (of_property_read_u32(child, "reg", &prot_id))
884 continue;
885
886 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
887 dev_err(dev, "Out of range protocol %d\n", prot_id);
888
889 if (!scmi_is_protocol_implemented(handle, prot_id)) {
890 dev_err(dev, "SCMI protocol %d not implemented\n",
891 prot_id);
892 continue;
893 }
894
895 scmi_create_protocol_device(child, info, prot_id);
896 }
897
898 return 0;
899 }
900
901 static int scmi_mbox_free_channel(int id, void *p, void *data)
902 {
903 struct scmi_chan_info *cinfo = p;
904 struct idr *idr = data;
905
906 if (!IS_ERR_OR_NULL(cinfo->chan)) {
907 mbox_free_channel(cinfo->chan);
908 cinfo->chan = NULL;
909 }
910
911 idr_remove(idr, id);
912
913 return 0;
914 }
915
916 static int scmi_remove(struct platform_device *pdev)
917 {
918 int ret = 0;
919 struct scmi_info *info = platform_get_drvdata(pdev);
920 struct idr *idr = &info->tx_idr;
921
922 mutex_lock(&scmi_list_mutex);
923 if (info->users)
924 ret = -EBUSY;
925 else
926 list_del(&info->node);
927 mutex_unlock(&scmi_list_mutex);
928
929 if (ret)
930 return ret;
931
932
933 ret = idr_for_each(idr, scmi_mbox_free_channel, idr);
934 idr_destroy(&info->tx_idr);
935
936 idr = &info->rx_idr;
937 ret = idr_for_each(idr, scmi_mbox_free_channel, idr);
938 idr_destroy(&info->rx_idr);
939
940 return ret;
941 }
942
943 static const struct scmi_desc scmi_generic_desc = {
944 .max_rx_timeout_ms = 30,
945 .max_msg = 20,
946 .max_msg_size = 128,
947 };
948
949
950 static const struct of_device_id scmi_of_match[] = {
951 { .compatible = "arm,scmi", .data = &scmi_generic_desc },
952 { },
953 };
954
955 MODULE_DEVICE_TABLE(of, scmi_of_match);
956
957 static struct platform_driver scmi_driver = {
958 .driver = {
959 .name = "arm-scmi",
960 .of_match_table = scmi_of_match,
961 },
962 .probe = scmi_probe,
963 .remove = scmi_remove,
964 };
965
966 module_platform_driver(scmi_driver);
967
968 MODULE_ALIAS("platform: arm-scmi");
969 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
970 MODULE_DESCRIPTION("ARM SCMI protocol driver");
971 MODULE_LICENSE("GPL v2");