20 #define pr_fmt(fmt) "%s: " fmt, __func__
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/virtio.h>
26 #include <linux/virtio_config.h>
29 #include <linux/slab.h>
32 #include <linux/sched.h>
33 #include <linux/wait.h>
85 #define to_rpmsg_channel(d) container_of(d, struct rpmsg_channel, dev)
86 #define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv)
105 #define RPMSG_NUM_BUFS (512)
106 #define RPMSG_BUF_SIZE (512)
107 #define RPMSG_TOTAL_BUF_SPACE (RPMSG_NUM_BUFS * RPMSG_BUF_SIZE)
114 #define RPMSG_RESERVED_ADDRESSES (1024)
117 #define RPMSG_NS_ADDR (53)
120 #define rpmsg_show_attr(field, path, format_string) \
122 field##_show(struct device *dev, \
123 struct device_attribute *attr, char *buf) \
125 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); \
127 return sprintf(buf, format_string, rpdev->path); \
142 static unsigned int rpmsg_dev_index;
162 static inline int rpmsg_id_match(
const struct rpmsg_channel *rpdev,
176 for (i = 0; ids[
i].
name[0]; i++)
177 if (rpmsg_id_match(rpdev, &ids[i]))
200 static void __ept_release(
struct kref *
kref)
225 dev_err(dev,
"failed to kzalloc a new ept\n");
244 dev_err(dev,
"idr_get_new_above failed: %d\n", err);
250 dev_err(dev,
"address 0x%x already in use\n", addr);
264 kref_put(&ept->
refcount, __ept_release);
311 return __rpmsg_create_ept(rpdev->
vrp, rpdev, cb, priv, addr);
338 kref_put(&ept->
refcount, __ept_release);
350 __rpmsg_destroy_ept(ept->
rpdev->vrp, ept);
362 static int rpmsg_dev_probe(
struct device *dev)
372 dev_err(dev,
"failed to create endpoint\n");
380 err = rpdrv->
probe(rpdev);
382 dev_err(dev,
"%s: failed: %d\n", __func__, err);
393 nsm.addr = rpdev->
src;
398 dev_err(dev,
"failed to announce service %d\n", err);
405 static int rpmsg_dev_remove(
struct device *dev)
418 nsm.addr = rpdev->
src;
423 dev_err(dev,
"failed to announce service %d\n", err);
433 static struct bus_type rpmsg_bus = {
435 .match = rpmsg_dev_match,
436 .dev_attrs = rpmsg_dev_attrs,
437 .uevent = rpmsg_uevent,
438 .probe = rpmsg_dev_probe,
439 .remove = rpmsg_dev_remove,
450 rpdrv->
drv.bus = &rpmsg_bus;
467 static void rpmsg_release_device(
struct device *dev)
479 static int rpmsg_channel_match(
struct device *dev,
void *
data)
514 dev_err(dev,
"channel %s:%x:%x already exist\n",
521 pr_err(
"kzalloc failed\n");
540 rpdev->
dev.parent = &vrp->
vdev->dev;
541 rpdev->
dev.bus = &rpmsg_bus;
542 rpdev->
dev.release = rpmsg_release_device;
546 dev_err(dev,
"device_register failed: %d\n", ret);
642 static void rpmsg_downref_sleepers(
struct virtproc_info *vrp)
690 void *data,
int len,
bool wait)
700 dev_err(dev,
"invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
714 dev_err(dev,
"message is too big (%d)\n", len);
719 msg = get_a_tx_buf(vrp);
726 rpmsg_upref_sleepers(vrp);
735 (msg = get_a_tx_buf(vrp)),
739 rpmsg_downref_sleepers(vrp);
743 dev_err(dev,
"timeout waiting for a tx buffer\n");
755 dev_dbg(dev,
"TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
759 msg,
sizeof(*msg) + msg->
len,
true);
773 dev_err(dev,
"virtqueue_add_buf failed: %d\n", err);
788 static void rpmsg_recv_done(
struct virtqueue *rvq)
800 dev_err(dev,
"uhm, incoming signal, but no used buffer ?\n");
804 dev_dbg(dev,
"From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
808 msg,
sizeof(*msg) + msg->
len,
true);
816 dev_warn(dev,
"inbound msg too big: (%d, %d)\n", len, msg->
len);
842 kref_put(&ept->
refcount, __ept_release);
844 dev_warn(dev,
"msg received with no recepient\n");
852 dev_err(dev,
"failed to add a virtqueue buffer: %d\n", err);
867 static void rpmsg_xmit_done(
struct virtqueue *svq)
878 static void rpmsg_ns_cb(
struct rpmsg_channel *rpdev,
void *data,
int len,
888 print_hex_dump(
KERN_DEBUG,
"NS announcement: ",
892 if (len !=
sizeof(*msg)) {
893 dev_err(dev,
"malformed ns msg (%d)\n", len);
904 dev_err(dev,
"anomaly: ns ept has an rpdev handle\n");
911 dev_info(dev,
"%sing channel %s addr 0x%x\n",
920 ret = rpmsg_destroy_channel(vrp, &chinfo);
922 dev_err(dev,
"rpmsg_destroy_channel failed: %d\n", ret);
924 newch = rpmsg_create_channel(vrp, &chinfo);
926 dev_err(dev,
"rpmsg_create_channel failed\n");
932 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
933 const char *names[] = {
"input",
"output" };
951 err = vdev->
config->find_vqs(vdev, 2, vqs, vq_cbs, names);
965 dev_dbg(&vdev->
dev,
"buffers: va %p, dma 0x%llx\n", bufs_va,
969 vrp->
rbufs = bufs_va;
994 vrp->
ns_ept = __rpmsg_create_ept(vrp,
NULL, rpmsg_ns_cb,
997 dev_err(&vdev->
dev,
"failed to create the ns ept\n");
1020 static int rpmsg_remove_device(
struct device *dev,
void *data)
1032 vdev->
config->reset(vdev);
1036 dev_warn(&vdev->
dev,
"can't remove rpmsg device: %d\n", ret);
1039 __rpmsg_destroy_ept(vrp, vrp->
ns_ept);
1064 .driver.name = KBUILD_MODNAME,
1067 .probe = rpmsg_probe,
1071 static int __init rpmsg_init(
void)
1077 pr_err(
"failed to register rpmsg bus: %d\n", ret);
1083 pr_err(
"failed to register virtio driver: %d\n", ret);
1091 static void __exit rpmsg_fini(
void)