1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#define pr_fmt(fmt) "%s: " fmt, __func__
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/virtio.h>
25#include <linux/virtio_ids.h>
26#include <linux/virtio_config.h>
27#include <linux/scatterlist.h>
28#include <linux/dma-mapping.h>
29#include <linux/slab.h>
30#include <linux/idr.h>
31#include <linux/jiffies.h>
32#include <linux/sched.h>
33#include <linux/wait.h>
34#include <linux/rpmsg.h>
35#include <linux/mutex.h>
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60struct virtproc_info {
61 struct virtio_device *vdev;
62 struct virtqueue *rvq, *svq;
63 void *rbufs, *sbufs;
64 unsigned int num_bufs;
65 int last_sbuf;
66 dma_addr_t bufs_dma;
67 struct mutex tx_lock;
68 struct idr endpoints;
69 struct mutex endpoints_lock;
70 wait_queue_head_t sendq;
71 atomic_t sleepers;
72 struct rpmsg_endpoint *ns_ept;
73};
74
75
76
77
78
79
80
81struct rpmsg_channel_info {
82 char name[RPMSG_NAME_SIZE];
83 u32 src;
84 u32 dst;
85};
86
87#define to_rpmsg_channel(d) container_of(d, struct rpmsg_channel, dev)
88#define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv)
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108#define MAX_RPMSG_NUM_BUFS (512)
109#define RPMSG_BUF_SIZE (512)
110
111
112
113
114
115
116#define RPMSG_RESERVED_ADDRESSES (1024)
117
118
119#define RPMSG_NS_ADDR (53)
120
121
122#define rpmsg_show_attr(field, path, format_string) \
123static ssize_t \
124field##_show(struct device *dev, \
125 struct device_attribute *attr, char *buf) \
126{ \
127 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); \
128 \
129 return sprintf(buf, format_string, rpdev->path); \
130}
131
132
133rpmsg_show_attr(name, id.name, "%s\n");
134rpmsg_show_attr(src, src, "0x%x\n");
135rpmsg_show_attr(dst, dst, "0x%x\n");
136rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
137
138
139
140
141
142
143
144static unsigned int rpmsg_dev_index;
145
146static ssize_t modalias_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
148{
149 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
150
151 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
152}
153
154static struct device_attribute rpmsg_dev_attrs[] = {
155 __ATTR_RO(name),
156 __ATTR_RO(modalias),
157 __ATTR_RO(dst),
158 __ATTR_RO(src),
159 __ATTR_RO(announce),
160 __ATTR_NULL
161};
162
163
164static inline int rpmsg_id_match(const struct rpmsg_channel *rpdev,
165 const struct rpmsg_device_id *id)
166{
167 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
168}
169
170
171static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
172{
173 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
174 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
175 const struct rpmsg_device_id *ids = rpdrv->id_table;
176 unsigned int i;
177
178 for (i = 0; ids[i].name[0]; i++)
179 if (rpmsg_id_match(rpdev, &ids[i]))
180 return 1;
181
182 return 0;
183}
184
185static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
186{
187 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
188
189 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
190 rpdev->id.name);
191}
192
193
194
195
196
197
198
199
200
201
202static void __ept_release(struct kref *kref)
203{
204 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
205 refcount);
206
207
208
209
210 kfree(ept);
211}
212
213
214static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
215 struct rpmsg_channel *rpdev, rpmsg_rx_cb_t cb,
216 void *priv, u32 addr)
217{
218 int id_min, id_max, id;
219 struct rpmsg_endpoint *ept;
220 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
221
222 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
223 if (!ept) {
224 dev_err(dev, "failed to kzalloc a new ept\n");
225 return NULL;
226 }
227
228 kref_init(&ept->refcount);
229 mutex_init(&ept->cb_lock);
230
231 ept->rpdev = rpdev;
232 ept->cb = cb;
233 ept->priv = priv;
234
235
236 if (addr == RPMSG_ADDR_ANY) {
237 id_min = RPMSG_RESERVED_ADDRESSES;
238 id_max = 0;
239 } else {
240 id_min = addr;
241 id_max = addr + 1;
242 }
243
244 mutex_lock(&vrp->endpoints_lock);
245
246
247 id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
248 if (id < 0) {
249 dev_err(dev, "idr_alloc failed: %d\n", id);
250 goto free_ept;
251 }
252 ept->addr = id;
253
254 mutex_unlock(&vrp->endpoints_lock);
255
256 return ept;
257
258free_ept:
259 mutex_unlock(&vrp->endpoints_lock);
260 kref_put(&ept->refcount, __ept_release);
261 return NULL;
262}
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *rpdev,
305 rpmsg_rx_cb_t cb, void *priv, u32 addr)
306{
307 return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, addr);
308}
309EXPORT_SYMBOL(rpmsg_create_ept);
310
311
312
313
314
315
316
317
318
319
320
321static void
322__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
323{
324
325 mutex_lock(&vrp->endpoints_lock);
326 idr_remove(&vrp->endpoints, ept->addr);
327 mutex_unlock(&vrp->endpoints_lock);
328
329
330 mutex_lock(&ept->cb_lock);
331 ept->cb = NULL;
332 mutex_unlock(&ept->cb_lock);
333
334 kref_put(&ept->refcount, __ept_release);
335}
336
337
338
339
340
341
342
343
344void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
345{
346 __rpmsg_destroy_ept(ept->rpdev->vrp, ept);
347}
348EXPORT_SYMBOL(rpmsg_destroy_ept);
349
350
351
352
353
354
355
356
357
358static int rpmsg_dev_probe(struct device *dev)
359{
360 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
361 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
362 struct virtproc_info *vrp = rpdev->vrp;
363 struct rpmsg_endpoint *ept;
364 int err;
365
366 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, rpdev->src);
367 if (!ept) {
368 dev_err(dev, "failed to create endpoint\n");
369 err = -ENOMEM;
370 goto out;
371 }
372
373 rpdev->ept = ept;
374 rpdev->src = ept->addr;
375
376 err = rpdrv->probe(rpdev);
377 if (err) {
378 dev_err(dev, "%s: failed: %d\n", __func__, err);
379 rpmsg_destroy_ept(ept);
380 goto out;
381 }
382
383
384 if (rpdev->announce &&
385 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
386 struct rpmsg_ns_msg nsm;
387
388 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
389 nsm.addr = rpdev->src;
390 nsm.flags = RPMSG_NS_CREATE;
391
392 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
393 if (err)
394 dev_err(dev, "failed to announce service %d\n", err);
395 }
396
397out:
398 return err;
399}
400
401static int rpmsg_dev_remove(struct device *dev)
402{
403 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
404 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
405 struct virtproc_info *vrp = rpdev->vrp;
406 int err = 0;
407
408
409 if (rpdev->announce &&
410 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
411 struct rpmsg_ns_msg nsm;
412
413 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
414 nsm.addr = rpdev->src;
415 nsm.flags = RPMSG_NS_DESTROY;
416
417 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
418 if (err)
419 dev_err(dev, "failed to announce service %d\n", err);
420 }
421
422 rpdrv->remove(rpdev);
423
424 rpmsg_destroy_ept(rpdev->ept);
425
426 return err;
427}
428
429static struct bus_type rpmsg_bus = {
430 .name = "rpmsg",
431 .match = rpmsg_dev_match,
432 .dev_attrs = rpmsg_dev_attrs,
433 .uevent = rpmsg_uevent,
434 .probe = rpmsg_dev_probe,
435 .remove = rpmsg_dev_remove,
436};
437
438
439
440
441
442
443
444int register_rpmsg_driver(struct rpmsg_driver *rpdrv)
445{
446 rpdrv->drv.bus = &rpmsg_bus;
447 return driver_register(&rpdrv->drv);
448}
449EXPORT_SYMBOL(register_rpmsg_driver);
450
451
452
453
454
455
456
457void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
458{
459 driver_unregister(&rpdrv->drv);
460}
461EXPORT_SYMBOL(unregister_rpmsg_driver);
462
463static void rpmsg_release_device(struct device *dev)
464{
465 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
466
467 kfree(rpdev);
468}
469
470
471
472
473
474
475static int rpmsg_channel_match(struct device *dev, void *data)
476{
477 struct rpmsg_channel_info *chinfo = data;
478 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
479
480 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
481 return 0;
482
483 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
484 return 0;
485
486 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
487 return 0;
488
489
490 return 1;
491}
492
493
494
495
496
497
498static struct rpmsg_channel *rpmsg_create_channel(struct virtproc_info *vrp,
499 struct rpmsg_channel_info *chinfo)
500{
501 struct rpmsg_channel *rpdev;
502 struct device *tmp, *dev = &vrp->vdev->dev;
503 int ret;
504
505
506 tmp = device_find_child(dev, chinfo, rpmsg_channel_match);
507 if (tmp) {
508
509 put_device(tmp);
510 dev_err(dev, "channel %s:%x:%x already exist\n",
511 chinfo->name, chinfo->src, chinfo->dst);
512 return NULL;
513 }
514
515 rpdev = kzalloc(sizeof(struct rpmsg_channel), GFP_KERNEL);
516 if (!rpdev) {
517 pr_err("kzalloc failed\n");
518 return NULL;
519 }
520
521 rpdev->vrp = vrp;
522 rpdev->src = chinfo->src;
523 rpdev->dst = chinfo->dst;
524
525
526
527
528
529 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY ? true : false;
530
531 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
532
533
534 dev_set_name(&rpdev->dev, "rpmsg%d", rpmsg_dev_index++);
535
536 rpdev->dev.parent = &vrp->vdev->dev;
537 rpdev->dev.bus = &rpmsg_bus;
538 rpdev->dev.release = rpmsg_release_device;
539
540 ret = device_register(&rpdev->dev);
541 if (ret) {
542 dev_err(dev, "device_register failed: %d\n", ret);
543 put_device(&rpdev->dev);
544 return NULL;
545 }
546
547 return rpdev;
548}
549
550
551
552
553
554static int rpmsg_destroy_channel(struct virtproc_info *vrp,
555 struct rpmsg_channel_info *chinfo)
556{
557 struct virtio_device *vdev = vrp->vdev;
558 struct device *dev;
559
560 dev = device_find_child(&vdev->dev, chinfo, rpmsg_channel_match);
561 if (!dev)
562 return -EINVAL;
563
564 device_unregister(dev);
565
566 put_device(dev);
567
568 return 0;
569}
570
571
572static void *get_a_tx_buf(struct virtproc_info *vrp)
573{
574 unsigned int len;
575 void *ret;
576
577
578 mutex_lock(&vrp->tx_lock);
579
580
581
582
583
584 if (vrp->last_sbuf < vrp->num_bufs / 2)
585 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
586
587 else
588 ret = virtqueue_get_buf(vrp->svq, &len);
589
590 mutex_unlock(&vrp->tx_lock);
591
592 return ret;
593}
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
612{
613
614 mutex_lock(&vrp->tx_lock);
615
616
617 if (atomic_inc_return(&vrp->sleepers) == 1)
618
619 virtqueue_enable_cb(vrp->svq);
620
621 mutex_unlock(&vrp->tx_lock);
622}
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
639{
640
641 mutex_lock(&vrp->tx_lock);
642
643
644 if (atomic_dec_and_test(&vrp->sleepers))
645
646 virtqueue_disable_cb(vrp->svq);
647
648 mutex_unlock(&vrp->tx_lock);
649}
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
686 void *data, int len, bool wait)
687{
688 struct virtproc_info *vrp = rpdev->vrp;
689 struct device *dev = &rpdev->dev;
690 struct scatterlist sg;
691 struct rpmsg_hdr *msg;
692 int err;
693
694
695 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
696 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
697 return -EINVAL;
698 }
699
700
701
702
703
704
705
706
707
708
709 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
710 dev_err(dev, "message is too big (%d)\n", len);
711 return -EMSGSIZE;
712 }
713
714
715 msg = get_a_tx_buf(vrp);
716 if (!msg && !wait)
717 return -ENOMEM;
718
719
720 while (!msg) {
721
722 rpmsg_upref_sleepers(vrp);
723
724
725
726
727
728
729
730 err = wait_event_interruptible_timeout(vrp->sendq,
731 (msg = get_a_tx_buf(vrp)),
732 msecs_to_jiffies(15000));
733
734
735 rpmsg_downref_sleepers(vrp);
736
737
738 if (!err) {
739 dev_err(dev, "timeout waiting for a tx buffer\n");
740 return -ERESTARTSYS;
741 }
742 }
743
744 msg->len = len;
745 msg->flags = 0;
746 msg->src = src;
747 msg->dst = dst;
748 msg->reserved = 0;
749 memcpy(msg->data, data, len);
750
751 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
752 msg->src, msg->dst, msg->len,
753 msg->flags, msg->reserved);
754 print_hex_dump(KERN_DEBUG, "rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
755 msg, sizeof(*msg) + msg->len, true);
756
757 sg_init_one(&sg, msg, sizeof(*msg) + len);
758
759 mutex_lock(&vrp->tx_lock);
760
761
762 err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
763 if (err) {
764
765
766
767
768
769 dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
770 goto out;
771 }
772
773
774 virtqueue_kick(vrp->svq);
775out:
776 mutex_unlock(&vrp->tx_lock);
777 return err;
778}
779EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
780
781static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
782 struct rpmsg_hdr *msg, unsigned int len)
783{
784 struct rpmsg_endpoint *ept;
785 struct scatterlist sg;
786 int err;
787
788 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
789 msg->src, msg->dst, msg->len,
790 msg->flags, msg->reserved);
791 print_hex_dump(KERN_DEBUG, "rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
792 msg, sizeof(*msg) + msg->len, true);
793
794
795
796
797
798 if (len > RPMSG_BUF_SIZE ||
799 msg->len > (len - sizeof(struct rpmsg_hdr))) {
800 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
801 return -EINVAL;
802 }
803
804
805 mutex_lock(&vrp->endpoints_lock);
806
807 ept = idr_find(&vrp->endpoints, msg->dst);
808
809
810 if (ept)
811 kref_get(&ept->refcount);
812
813 mutex_unlock(&vrp->endpoints_lock);
814
815 if (ept) {
816
817 mutex_lock(&ept->cb_lock);
818
819 if (ept->cb)
820 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
821 msg->src);
822
823 mutex_unlock(&ept->cb_lock);
824
825
826 kref_put(&ept->refcount, __ept_release);
827 } else
828 dev_warn(dev, "msg received with no recipient\n");
829
830
831 sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
832
833
834 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
835 if (err < 0) {
836 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
837 return err;
838 }
839
840 return 0;
841}
842
843
844static void rpmsg_recv_done(struct virtqueue *rvq)
845{
846 struct virtproc_info *vrp = rvq->vdev->priv;
847 struct device *dev = &rvq->vdev->dev;
848 struct rpmsg_hdr *msg;
849 unsigned int len, msgs_received = 0;
850 int err;
851
852 msg = virtqueue_get_buf(rvq, &len);
853 if (!msg) {
854 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
855 return;
856 }
857
858 while (msg) {
859 err = rpmsg_recv_single(vrp, dev, msg, len);
860 if (err)
861 break;
862
863 msgs_received++;
864
865 msg = virtqueue_get_buf(rvq, &len);
866 };
867
868 dev_dbg(dev, "Received %u messages\n", msgs_received);
869
870
871 if (msgs_received)
872 virtqueue_kick(vrp->rvq);
873}
874
875
876
877
878
879
880
881
882static void rpmsg_xmit_done(struct virtqueue *svq)
883{
884 struct virtproc_info *vrp = svq->vdev->priv;
885
886 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
887
888
889 wake_up_interruptible(&vrp->sendq);
890}
891
892
893static void rpmsg_ns_cb(struct rpmsg_channel *rpdev, void *data, int len,
894 void *priv, u32 src)
895{
896 struct rpmsg_ns_msg *msg = data;
897 struct rpmsg_channel *newch;
898 struct rpmsg_channel_info chinfo;
899 struct virtproc_info *vrp = priv;
900 struct device *dev = &vrp->vdev->dev;
901 int ret;
902
903 print_hex_dump(KERN_DEBUG, "NS announcement: ",
904 DUMP_PREFIX_NONE, 16, 1,
905 data, len, true);
906
907 if (len != sizeof(*msg)) {
908 dev_err(dev, "malformed ns msg (%d)\n", len);
909 return;
910 }
911
912
913
914
915
916
917
918 if (rpdev) {
919 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
920 return;
921 }
922
923
924 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
925
926 dev_info(dev, "%sing channel %s addr 0x%x\n",
927 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
928 msg->name, msg->addr);
929
930 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
931 chinfo.src = RPMSG_ADDR_ANY;
932 chinfo.dst = msg->addr;
933
934 if (msg->flags & RPMSG_NS_DESTROY) {
935 ret = rpmsg_destroy_channel(vrp, &chinfo);
936 if (ret)
937 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
938 } else {
939 newch = rpmsg_create_channel(vrp, &chinfo);
940 if (!newch)
941 dev_err(dev, "rpmsg_create_channel failed\n");
942 }
943}
944
945static int rpmsg_probe(struct virtio_device *vdev)
946{
947 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
948 const char *names[] = { "input", "output" };
949 struct virtqueue *vqs[2];
950 struct virtproc_info *vrp;
951 void *bufs_va;
952 int err = 0, i;
953 size_t total_buf_space;
954 bool notify;
955
956 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
957 if (!vrp)
958 return -ENOMEM;
959
960 vrp->vdev = vdev;
961
962 idr_init(&vrp->endpoints);
963 mutex_init(&vrp->endpoints_lock);
964 mutex_init(&vrp->tx_lock);
965 init_waitqueue_head(&vrp->sendq);
966
967
968 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
969 if (err)
970 goto free_vrp;
971
972 vrp->rvq = vqs[0];
973 vrp->svq = vqs[1];
974
975
976 WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
977 virtqueue_get_vring_size(vrp->svq));
978
979
980 if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
981 vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
982 else
983 vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
984
985 total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
986
987
988 bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
989 total_buf_space, &vrp->bufs_dma,
990 GFP_KERNEL);
991 if (!bufs_va) {
992 err = -ENOMEM;
993 goto vqs_del;
994 }
995
996 dev_dbg(&vdev->dev, "buffers: va %p, dma 0x%llx\n", bufs_va,
997 (unsigned long long)vrp->bufs_dma);
998
999
1000 vrp->rbufs = bufs_va;
1001
1002
1003 vrp->sbufs = bufs_va + total_buf_space / 2;
1004
1005
1006 for (i = 0; i < vrp->num_bufs / 2; i++) {
1007 struct scatterlist sg;
1008 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
1009
1010 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
1011
1012 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
1013 GFP_KERNEL);
1014 WARN_ON(err);
1015 }
1016
1017
1018 virtqueue_disable_cb(vrp->svq);
1019
1020 vdev->priv = vrp;
1021
1022
1023 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
1024
1025 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
1026 vrp, RPMSG_NS_ADDR);
1027 if (!vrp->ns_ept) {
1028 dev_err(&vdev->dev, "failed to create the ns ept\n");
1029 err = -ENOMEM;
1030 goto free_coherent;
1031 }
1032 }
1033
1034
1035
1036
1037
1038 notify = virtqueue_kick_prepare(vrp->rvq);
1039
1040
1041 virtio_device_ready(vdev);
1042
1043
1044
1045
1046
1047
1048 if (notify)
1049 virtqueue_notify(vrp->rvq);
1050
1051 dev_info(&vdev->dev, "rpmsg host is online\n");
1052
1053 return 0;
1054
1055free_coherent:
1056 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1057 bufs_va, vrp->bufs_dma);
1058vqs_del:
1059 vdev->config->del_vqs(vrp->vdev);
1060free_vrp:
1061 kfree(vrp);
1062 return err;
1063}
1064
1065static int rpmsg_remove_device(struct device *dev, void *data)
1066{
1067 device_unregister(dev);
1068
1069 return 0;
1070}
1071
1072static void rpmsg_remove(struct virtio_device *vdev)
1073{
1074 struct virtproc_info *vrp = vdev->priv;
1075 size_t total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
1076 int ret;
1077
1078 vdev->config->reset(vdev);
1079
1080 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
1081 if (ret)
1082 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
1083
1084 if (vrp->ns_ept)
1085 __rpmsg_destroy_ept(vrp, vrp->ns_ept);
1086
1087 idr_destroy(&vrp->endpoints);
1088
1089 vdev->config->del_vqs(vrp->vdev);
1090
1091 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1092 vrp->rbufs, vrp->bufs_dma);
1093
1094 kfree(vrp);
1095}
1096
1097static struct virtio_device_id id_table[] = {
1098 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1099 { 0 },
1100};
1101
1102static unsigned int features[] = {
1103 VIRTIO_RPMSG_F_NS,
1104};
1105
1106static struct virtio_driver virtio_ipc_driver = {
1107 .feature_table = features,
1108 .feature_table_size = ARRAY_SIZE(features),
1109 .driver.name = KBUILD_MODNAME,
1110 .driver.owner = THIS_MODULE,
1111 .id_table = id_table,
1112 .probe = rpmsg_probe,
1113 .remove = rpmsg_remove,
1114};
1115
1116static int __init rpmsg_init(void)
1117{
1118 int ret;
1119
1120 ret = bus_register(&rpmsg_bus);
1121 if (ret) {
1122 pr_err("failed to register rpmsg bus: %d\n", ret);
1123 return ret;
1124 }
1125
1126 ret = register_virtio_driver(&virtio_ipc_driver);
1127 if (ret) {
1128 pr_err("failed to register virtio driver: %d\n", ret);
1129 bus_unregister(&rpmsg_bus);
1130 }
1131
1132 return ret;
1133}
1134subsys_initcall(rpmsg_init);
1135
1136static void __exit rpmsg_fini(void)
1137{
1138 unregister_virtio_driver(&virtio_ipc_driver);
1139 bus_unregister(&rpmsg_bus);
1140}
1141module_exit(rpmsg_fini);
1142
1143MODULE_DEVICE_TABLE(virtio, id_table);
1144MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1145MODULE_LICENSE("GPL v2");
1146