diff options
author | Ohad Ben-Cohen <ohad@wizery.com> | 2011-01-21 02:03:59 +0200 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2011-06-14 09:05:13 -0700 |
commit | 61ebbf1f0384d0a758a392fde0eae89237f685c1 (patch) | |
tree | f6cdbc8c17c29671543fd313be36cad20c2bbf31 | |
parent | 9a065c694ac7370b0578ae3d1d5aa7d7d2d6d9a5 (diff) | |
download | kernel_samsung_espresso10-61ebbf1f0384d0a758a392fde0eae89237f685c1.zip kernel_samsung_espresso10-61ebbf1f0384d0a758a392fde0eae89237f685c1.tar.gz kernel_samsung_espresso10-61ebbf1f0384d0a758a392fde0eae89237f685c1.tar.bz2 |
drivers: introduce rpmsg, a remote-processor messaging virtio bus
Add a virtio IPC bus, which enables kernel users to communicate
with remote processors over shared memory using a simple messaging
protocol.
Assign a local address for every local endpoint that is created,
and bind it to the user's callback. Invoke that callback when a message
is received carrying the user's address as the destination addr.
Change-Id: I0c6804859c2344bdddd433075d4116d88eea67e4
Signed-off-by: Ohad Ben-Cohen <ohad@wizery.com>
-rw-r--r-- | drivers/Kconfig | 4 | ||||
-rw-r--r-- | drivers/Makefile | 1 | ||||
-rw-r--r-- | drivers/rpmsg/Kconfig | 15 | ||||
-rw-r--r-- | drivers/rpmsg/Makefile | 1 | ||||
-rw-r--r-- | drivers/rpmsg/virtio_rpmsg_bus.c | 789 | ||||
-rw-r--r-- | include/linux/mod_devicetable.h | 10 | ||||
-rw-r--r-- | include/linux/rpmsg.h | 191 | ||||
-rw-r--r-- | include/linux/virtio_ids.h | 1 |
8 files changed, 1012 insertions, 0 deletions
diff --git a/drivers/Kconfig b/drivers/Kconfig index 72175f3..85ac967 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -127,4 +127,8 @@ source "drivers/hwspinlock/Kconfig" source "drivers/clocksource/Kconfig" source "drivers/remoteproc/Kconfig" + +source "drivers/virtio/Kconfig" + +source "drivers/rpmsg/Kconfig" endmenu diff --git a/drivers/Makefile b/drivers/Makefile index 4d53a18..2980a15 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -22,6 +22,7 @@ obj-$(CONFIG_ARM_AMBA) += amba/ obj-$(CONFIG_DMA_ENGINE) += dma/ obj-$(CONFIG_VIRTIO) += virtio/ +obj-$(CONFIG_RPMSG) += rpmsg/ obj-$(CONFIG_XEN) += xen/ # regulators early, since some subsystems rely on them to initialize diff --git a/drivers/rpmsg/Kconfig b/drivers/rpmsg/Kconfig new file mode 100644 index 0000000..d4d39bf --- /dev/null +++ b/drivers/rpmsg/Kconfig @@ -0,0 +1,15 @@ +config RPMSG + tristate "Virtio-based remote processor messaging bus" + select VIRTIO + select VIRTIO_RING + depends on OMAP_RPMSG + ---help--- + This virtio driver provides support for shared-memory-based + remote processor messaging, by registering the RPMSG bus which + in turn enables a handful of IPC drivers. + + Such support is usually required to offload cpu-intensive + or latency-sensitive tasks to specialized remote processors with + dedicated hardware accelerators and/or real-time properties. + + If unsure, say N. diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile new file mode 100644 index 0000000..7617fcb --- /dev/null +++ b/drivers/rpmsg/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_RPMSG) += virtio_rpmsg_bus.o diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c new file mode 100644 index 0000000..5dde5fc --- /dev/null +++ b/drivers/rpmsg/virtio_rpmsg_bus.c @@ -0,0 +1,789 @@ +/* + * Virtio-based remote processor messaging bus + * + * Copyright (C) 2011 Texas Instruments, Inc. + * Copyright (C) 2011 Google, Inc. + * + * Ohad Ben-Cohen <ohad@wizery.com> + * Brian Swetland <swetland@google.com> + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#define pr_fmt(fmt) "%s: " fmt, __func__ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/virtio.h> +#include <linux/virtio_ids.h> +#include <linux/virtio_config.h> +#include <linux/scatterlist.h> +#include <linux/slab.h> +#include <linux/idr.h> +#include <linux/jiffies.h> +#include <linux/sched.h> +#include <linux/wait.h> +#include <linux/rpmsg.h> + +/** + * struct virtproc_info - virtual remote processor info + * + * @vdev: the virtio device + * @rvq: rx virtqueue (from pov of local processor) + * @svq: tx virtqueue (from pov of local processor) + * @rbufs: address of rx buffers + * @sbufs: address of tx buffers + * @last_rbuf: index of last rx buffer used + * @last_sbuf: index of last tx buffer used + * @sim_base: simulated base addr base to make virtio's virt_to_page happy + * @svq_lock: protects the tx virtqueue, to allow several concurrent senders + * @num_bufs: total number of buffers allocated for communicating with this + * virtual remote processor. half is used for rx and half for tx. + * @buf_size: size of buffers allocated for communications + * @endpoints: the set of local endpoints + * @endpoints_lock: lock of the endpoints set + * @sendq: wait queue of sending contexts waiting for free rpmsg buffer + * @ns_ept: the bus's name service endpoint + * + * This structure stores the rpmsg state of a given virtio remote processor + * device (there might be several virtio rproc devices for each physical + * remote processor). + */ +struct virtproc_info { + struct virtio_device *vdev; + struct virtqueue *rvq, *svq; + void *rbufs, *sbufs; + int last_rbuf, last_sbuf; + void *sim_base; + spinlock_t svq_lock; + int num_bufs; + int buf_size; + struct idr endpoints; + spinlock_t endpoints_lock; + wait_queue_head_t sendq; + struct rpmsg_endpoint *ns_ept; +}; + +#define to_rpmsg_channel(d) container_of(d, struct rpmsg_channel, dev) +#define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv) + +/* + * Local addresses are dynamically allocated on-demand. + * We do not dynamically assign addresses from the low 1024 range, + * in order to reserve that address range for predefined services. + */ +#define RPMSG_RESERVED_ADDRESSES (1024) + +/* Address 53 is reserved for advertising remote services */ +#define RPMSG_NS_ADDR (53) + +/* show configuration fields */ +#define rpmsg_show_attr(field, path, format_string) \ +static ssize_t \ +field##_show(struct device *dev, \ + struct device_attribute *attr, char *buf) \ +{ \ + struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); \ + \ + return sprintf(buf, format_string, rpdev->path); \ +} + +rpmsg_show_attr(name, id.name, "%s\n"); +rpmsg_show_attr(dst, dst, "0x%x\n"); +rpmsg_show_attr(src, src, "0x%x\n"); +rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n"); + +/* unique (free running) numbering for rpmsg devices */ +static unsigned int rpmsg_dev_index; + +static ssize_t modalias_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); + + return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name); +} + +static struct device_attribute rpmsg_dev_attrs[] = { + __ATTR_RO(name), + __ATTR_RO(modalias), + __ATTR_RO(dst), + __ATTR_RO(src), + __ATTR_RO(announce), + __ATTR_NULL +}; + +static inline int rpmsg_id_match(const struct rpmsg_channel *rpdev, + const struct rpmsg_device_id *id) +{ + if (strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE)) + return 0; + + return 1; +} + +/* match rpmsg channel and rpmsg driver */ +static int rpmsg_dev_match(struct device *dev, struct device_driver *drv) +{ + struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); + struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv); + const struct rpmsg_device_id *ids = rpdrv->id_table; + unsigned int i; + + for (i = 0; ids[i].name[0]; i++) { + if (rpmsg_id_match(rpdev, &ids[i])) + return 1; + } + + return 0; +} + +static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env) +{ + struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); + + return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT, + rpdev->id.name); +} + +static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp, + struct rpmsg_channel *rpdev, + void (*cb)(struct rpmsg_channel *, void *, int, void *, u32), + void *priv, u32 addr) +{ + int err, tmpaddr, request; + struct rpmsg_endpoint *ept; + struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev; + + if (!idr_pre_get(&vrp->endpoints, GFP_KERNEL)) + return NULL; + + ept = kzalloc(sizeof(*ept), GFP_KERNEL); + if (!ept) { + dev_err(dev, "failed to kzalloc a new ept\n"); + return NULL; + } + + ept->rpdev = rpdev; + ept->cb = cb; + ept->priv = priv; + + /* do we need to allocate a local address ? */ + request = addr == RPMSG_ADDR_ANY ? RPMSG_RESERVED_ADDRESSES : addr; + + spin_lock(&vrp->endpoints_lock); + + /* bind the endpoint to an rpmsg address (and allocate one if needed) */ + err = idr_get_new_above(&vrp->endpoints, ept, request, &tmpaddr); + if (err) { + dev_err(dev, "idr_get_new_above failed: %d\n", err); + goto free_ept; + } + + if (addr != RPMSG_ADDR_ANY && tmpaddr != addr) { + dev_err(dev, "address 0x%x already in use\n", addr); + goto rem_idr; + } + + ept->addr = tmpaddr; + + spin_unlock(&vrp->endpoints_lock); + + return ept; + +rem_idr: + idr_remove(&vrp->endpoints, request); +free_ept: + spin_unlock(&vrp->endpoints_lock); + kfree(ept); + return NULL; +} + +struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *rpdev, + void (*cb)(struct rpmsg_channel *, void *, int, void *, u32), + void *priv, u32 addr) +{ + return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, addr); +} +EXPORT_SYMBOL(rpmsg_create_ept); + +void rpmsg_destroy_ept(struct rpmsg_endpoint *ept) +{ + struct virtproc_info *vrp = ept->rpdev->vrp; + + spin_lock(&vrp->endpoints_lock); + idr_remove(&vrp->endpoints, ept->addr); + spin_unlock(&vrp->endpoints_lock); + + kfree(ept); +} +EXPORT_SYMBOL(rpmsg_destroy_ept); + +static int rpmsg_dev_probe(struct device *dev) +{ + struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); + struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver); + struct virtproc_info *vrp = rpdev->vrp; + struct rpmsg_endpoint *ept; + int err; + + ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, rpdev->src); + if (!ept) { + dev_err(dev, "failed to create endpoint\n"); + err = -ENOMEM; + goto out; + } + + rpdev->ept = ept; + rpdev->src = ept->addr; + + err = rpdrv->probe(rpdev); + if (err) { + dev_err(dev, "%s: failed: %d\n", __func__, err); + rpmsg_destroy_ept(ept); + goto out; + } + + /* need to tell remote processor's name service about this channel ? */ + if (rpdev->announce && + virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) { + struct rpmsg_ns_msg nsm; + + strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE); + nsm.addr = rpdev->src; + nsm.flags = RPMSG_NS_CREATE; + + err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR); + if (err) + dev_err(dev, "failed to announce service %d\n", err); + } + +out: + return err; +} + +static int rpmsg_dev_remove(struct device *dev) +{ + struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); + struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver); + struct virtproc_info *vrp = rpdev->vrp; + int err = 0; + + /* tell remote processor's name service we're removing this channel */ + if (rpdev->announce && + virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) { + struct rpmsg_ns_msg nsm; + + strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE); + nsm.addr = rpdev->src; + nsm.flags = RPMSG_NS_DESTROY; + + err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR); + if (err) + dev_err(dev, "failed to announce service %d\n", err); + } + + rpdrv->remove(rpdev); + + rpmsg_destroy_ept(rpdev->ept); + + return err; +} + +static struct bus_type rpmsg_bus = { + .name = "rpmsg", + .match = rpmsg_dev_match, + .dev_attrs = rpmsg_dev_attrs, + .uevent = rpmsg_uevent, + .probe = rpmsg_dev_probe, + .remove = rpmsg_dev_remove, +}; + +int register_rpmsg_driver(struct rpmsg_driver *rpdrv) +{ + rpdrv->drv.bus = &rpmsg_bus; + return driver_register(&rpdrv->drv); +} +EXPORT_SYMBOL(register_rpmsg_driver); + +void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv) +{ + driver_unregister(&rpdrv->drv); +} +EXPORT_SYMBOL(unregister_rpmsg_driver); + +static void rpmsg_release_device(struct device *dev) +{ + struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); + + kfree(rpdev); +} + +/* match an rpmsg channel with channel info structs */ +static int rpmsg_channel_match(struct device *dev, void *data) +{ + struct rpmsg_channel_info *chinfo = data; + struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); + + if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src) + return 0; + + if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst) + return 0; + + if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE)) + return 0; + + return 1; +} + +static struct rpmsg_channel *rpmsg_create_channel(struct virtproc_info *vrp, + struct rpmsg_channel_info *chinfo) +{ + struct rpmsg_channel *rpdev; + struct device *tmp, *dev = &vrp->vdev->dev; + int ret; + + /* make sure a similar channel doesn't already exist */ + tmp = device_find_child(dev, chinfo, rpmsg_channel_match); + if (tmp) { + dev_err(dev, "channel %s:%x:%x already exist\n", + chinfo->name, chinfo->src, chinfo->dst); + return NULL; + } + + rpdev = kzalloc(sizeof(struct rpmsg_channel), GFP_KERNEL); + if (!rpdev) { + pr_err("kzalloc failed\n"); + return NULL; + } + + rpdev->vrp = vrp; + rpdev->src = chinfo->src; + rpdev->dst = chinfo->dst; + + /* + * rpmsg server channels has predefined local address, and their + * existence needs to be announced remotely + */ + rpdev->announce = rpdev->src != RPMSG_ADDR_ANY ? true : false; + + strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE); + + /* very simple device indexing plumbing which just works (for now) */ + dev_set_name(&rpdev->dev, "rpmsg%d", rpmsg_dev_index++); + + rpdev->dev.parent = &vrp->vdev->dev; + rpdev->dev.bus = &rpmsg_bus; + rpdev->dev.release = rpmsg_release_device; + + ret = device_register(&rpdev->dev); + if (ret) { + dev_err(dev, "device_register failed: %d\n", ret); + kfree(rpdev); + return NULL; + } + + return rpdev; +} + +static void rpmsg_destroy_channel(struct rpmsg_channel *rpdev) +{ + device_unregister(&rpdev->dev); +} + +static int rpmsg_destroy_channel_by_info(struct virtproc_info *vrp, + struct rpmsg_channel_info *chinfo) +{ + struct virtio_device *vdev = vrp->vdev; + struct device *dev; + + dev = device_find_child(&vdev->dev, chinfo, rpmsg_channel_match); + if (!dev) + return -EINVAL; + + rpmsg_destroy_channel(to_rpmsg_channel(dev)); + + return 0; +} + +/* minimal buf "allocator" that is just enough for now */ +static void *get_a_buf(struct virtproc_info *vrp) +{ + unsigned int len; + + /* either pick the next unused buffer */ + if (vrp->last_sbuf < vrp->num_bufs / 2) + return vrp->sbufs + vrp->buf_size * vrp->last_sbuf++; + /* or recycle a used one */ + else + return virtqueue_get_buf(vrp->svq, &len); +} + +/* XXX: the blocking 'wait' mechanism hasn't been tested yet */ +int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst, + void *data, int len, bool wait) +{ + struct virtproc_info *vrp = rpdev->vrp; + struct device *dev = &rpdev->dev; + struct scatterlist sg; + struct rpmsg_hdr *msg; + int err; + unsigned long offset; + void *sim_addr; + + if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) { + dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst); + return -EINVAL; + } + + /* the payload's size is currently limited */ + if (len > vrp->buf_size - sizeof(struct rpmsg_hdr)) { + dev_err(dev, "message is too big (%d)\n", len); + return -EMSGSIZE; + } + + /* grab a buffer */ + msg = get_a_buf(vrp); + if (!msg && !wait) + return -ENOMEM; + + /* no free buffer ? wait for one (but bail after 15 seconds) */ + while (!msg) { + /* enable "tx-complete" interrupts before dozing off */ + virtqueue_enable_cb(vrp->svq); + /* + * sleep until a free buffer is available or 15 secs elapse. + * the timeout period is not configurable because frankly + * i don't see why drivers need to deal with that. + * if later this happens to be required, it'd be easy to add. + */ + if (wait_event_interruptible_timeout(vrp->sendq, + (msg = get_a_buf(vrp)), + msecs_to_jiffies(15000))) + return -ERESTARTSYS; + /* on success, suppress "tx-complete" interrupts again */ + if (msg) + virtqueue_disable_cb(vrp->svq); + } + + msg->len = len; + msg->flags = 0; + msg->src = src; + msg->dst = dst; + msg->unused = 0; + memcpy(msg->data, data, len); + + dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Unused %d\n", + msg->src, msg->dst, msg->len, + msg->flags, msg->unused); + print_hex_dump(KERN_DEBUG, "rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1, + msg, sizeof(*msg) + msg->len, true); + + offset = ((unsigned long) msg) - ((unsigned long) vrp->rbufs); + sim_addr = vrp->sim_base + offset; + sg_init_one(&sg, sim_addr, sizeof(*msg) + len); + + /* protect svq from simultaneous concurrent manipulations */ + spin_lock(&vrp->svq_lock); + + /* add message to the remote processor's virtqueue */ + err = virtqueue_add_buf_gfp(vrp->svq, &sg, 1, 0, msg, GFP_KERNEL); + if (err < 0) { + dev_err(dev, "virtqueue_add_buf_gfp failed: %d\n", err); + goto out; + } + + /* tell the remote processor it has a pending message to read */ + virtqueue_kick(vrp->svq); + + err = 0; +out: + spin_unlock(&vrp->svq_lock); + return err; +} +EXPORT_SYMBOL(rpmsg_send_offchannel_raw); + +static void rpmsg_recv_done(struct virtqueue *rvq) +{ + struct rpmsg_hdr *msg; + unsigned int len; + struct rpmsg_endpoint *ept; + struct scatterlist sg; + unsigned long offset; + void *sim_addr; + struct virtproc_info *vrp = rvq->vdev->priv; + struct device *dev = &rvq->vdev->dev; + int err; + + msg = virtqueue_get_buf(rvq, &len); + if (!msg) { + dev_err(dev, "uhm, incoming signal, but no used buffer ?\n"); + return; + } + + dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Unused: %d\n", + msg->src, msg->dst, msg->len, + msg->flags, msg->unused); + print_hex_dump(KERN_DEBUG, "rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1, + msg, sizeof(*msg) + msg->len, true); + + /* fetch the callback of the appropriate user */ + spin_lock(&vrp->endpoints_lock); + ept = idr_find(&vrp->endpoints, msg->dst); + spin_unlock(&vrp->endpoints_lock); + + if (ept && ept->cb) + ept->cb(ept->rpdev, msg->data, msg->len, ept->priv, msg->src); + else + dev_warn(dev, "msg received with no recepient\n"); + + /* add the buffer back to the remote processor's virtqueue */ + offset = ((unsigned long) msg) - ((unsigned long) vrp->rbufs); + sim_addr = vrp->sim_base + offset; + sg_init_one(&sg, sim_addr, sizeof(*msg) + len); + + err = virtqueue_add_buf_gfp(vrp->rvq, &sg, 0, 1, msg, GFP_KERNEL); + if (err < 0) { + dev_err(dev, "failed to add a virtqueue buffer: %d\n", err); + return; + } + + /* tell the remote processor we added another available rx buffer */ + virtqueue_kick(vrp->rvq); +} + +static void rpmsg_xmit_done(struct virtqueue *svq) +{ + struct virtproc_info *vrp = svq->vdev->priv; + + dev_dbg(&svq->vdev->dev, "%s\n", __func__); + + /* wake up potential processes that are waiting for a buffer */ + wake_up_interruptible(&vrp->sendq); +} + +static void rpmsg_ns_cb(struct rpmsg_channel *rpdev, void *data, int len, + void *priv, u32 src) +{ + struct rpmsg_ns_msg *msg = data; + struct rpmsg_channel *newch; + struct rpmsg_channel_info chinfo; + struct virtproc_info *vrp = priv; + struct device *dev = &vrp->vdev->dev; + int ret; + + print_hex_dump(KERN_DEBUG, __func__, DUMP_PREFIX_NONE, 16, 1, + data, len, true); + + if (len != sizeof(*msg)) { + dev_err(dev, "malformed ns msg (%d)\n", len); + return; + } + + /* + * the name service ept does _not_ belong to a real rpmsg channel, + * and is handled by the rpmsg bus itself. + * for sanity reasons, make sure a valid rpdev has _not_ sneaked + * in somehow. + */ + if (rpdev) { + dev_err(dev, "anomaly: ns ept has an rpdev handle\n"); + return; + } + + /* don't trust the remote processor for null terminating the name */ + msg->name[RPMSG_NAME_SIZE - 1] = '\0'; + + dev_info(dev, "%sing channel %s addr 0x%x\n", + msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat", + msg->name, msg->addr); + + strncpy(chinfo.name, msg->name, sizeof(chinfo.name)); + chinfo.src = RPMSG_ADDR_ANY; + chinfo.dst = msg->addr; + + if (msg->flags & RPMSG_NS_DESTROY) { + ret = rpmsg_destroy_channel_by_info(vrp, &chinfo); + if (ret) + dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret); + } else { + newch = rpmsg_create_channel(vrp, &chinfo); + if (!newch) + dev_err(dev, "rpmsg_create_channel failed\n"); + } +} + +static int rpmsg_probe(struct virtio_device *vdev) +{ + vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done }; + const char *names[] = { "input", "output" }; + struct virtqueue *vqs[2]; + struct virtproc_info *vrp; + void *addr; + int err, i, num_bufs, buf_size, total_buf_size; + struct rpmsg_channel_info *ch; + + vrp = kzalloc(sizeof(*vrp), GFP_KERNEL); + if (!vrp) + return -ENOMEM; + + vrp->vdev = vdev; + + idr_init(&vrp->endpoints); + spin_lock_init(&vrp->endpoints_lock); + spin_lock_init(&vrp->svq_lock); + init_waitqueue_head(&vrp->sendq); + + /* We expect two virtqueues, rx and tx (in this order) */ + err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names); + if (err) + goto free_vi; + + vrp->rvq = vqs[0]; + vrp->svq = vqs[1]; + + /* Platform must supply pre-allocated uncached buffers for now */ + vdev->config->get(vdev, VPROC_BUF_ADDR, &addr, sizeof(addr)); + vdev->config->get(vdev, VPROC_BUF_NUM, &num_bufs, + sizeof(num_bufs)); + vdev->config->get(vdev, VPROC_BUF_SZ, &buf_size, sizeof(buf_size)); + + total_buf_size = num_bufs * buf_size; + + dev_dbg(&vdev->dev, "%d buffers, size %d, addr 0x%x, total 0x%x\n", + num_bufs, buf_size, (unsigned int) addr, total_buf_size); + + vrp->num_bufs = num_bufs; + vrp->buf_size = buf_size; + vrp->rbufs = addr; + vrp->sbufs = addr + total_buf_size / 2; + + /* simulated addr base to make virt_to_page happy */ + vdev->config->get(vdev, VPROC_SIM_BASE, &vrp->sim_base, + sizeof(vrp->sim_base)); + + /* set up the receive buffers */ + for (i = 0; i < num_bufs / 2; i++) { + struct scatterlist sg; + void *tmpaddr = vrp->rbufs + i * buf_size; + void *simaddr = vrp->sim_base + i * buf_size; + + sg_init_one(&sg, simaddr, buf_size); + err = virtqueue_add_buf_gfp(vrp->rvq, &sg, 0, 1, tmpaddr, + GFP_KERNEL); + WARN_ON(err < 0); /* sanity check; this can't really happen */ + } + + /* tell the remote processor it can start sending data */ + virtqueue_kick(vrp->rvq); + + /* suppress "tx-complete" interrupts */ + virtqueue_disable_cb(vrp->svq); + + vdev->priv = vrp; + + dev_info(&vdev->dev, "rpmsg backend virtproc probed successfully\n"); + + /* if supported by the remote processor, enable the name service */ + if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) { + vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb, + vrp, RPMSG_NS_ADDR); + if (!vrp->ns_ept) { + dev_err(&vdev->dev, "failed to create the ns ept\n"); + err = -ENOMEM; + goto vqs_del; + } + } + + /* look for platform-specific static channels */ + vdev->config->get(vdev, VPROC_STATIC_CHANNELS, &ch, sizeof(ch)); + + for (i = 0; ch && ch[i].name[0]; i++) + rpmsg_create_channel(vrp, &ch[i]); + + return 0; + +vqs_del: + vdev->config->del_vqs(vrp->vdev); +free_vi: + kfree(vrp); + return err; +} + +static int rpmsg_remove_device(struct device *dev, void *data) +{ + struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); + + rpmsg_destroy_channel(rpdev); + + return 0; +} + +static void __devexit rpmsg_remove(struct virtio_device *vdev) +{ + struct virtproc_info *vrp = vdev->priv; + int ret; + + ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device); + if (ret) + dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret); + + idr_remove_all(&vrp->endpoints); + idr_destroy(&vrp->endpoints); + + vdev->config->del_vqs(vrp->vdev); + + kfree(vrp); +} + +static struct virtio_device_id id_table[] = { + { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID }, + { 0 }, +}; + +static unsigned int features[] = { + VIRTIO_RPMSG_F_NS, +}; + +static struct virtio_driver virtio_ipc_driver = { + .feature_table = features, + .feature_table_size = ARRAY_SIZE(features), + .driver.name = KBUILD_MODNAME, + .driver.owner = THIS_MODULE, + .id_table = id_table, + .probe = rpmsg_probe, + .remove = __devexit_p(rpmsg_remove), +}; + +static int __init init(void) +{ + int ret; + + ret = bus_register(&rpmsg_bus); + if (ret) { + pr_err("failed to register rpmsg bus: %d\n", ret); + return ret; + } + + return register_virtio_driver(&virtio_ipc_driver); +} +module_init(init); + +static void __exit fini(void) +{ + unregister_virtio_driver(&virtio_ipc_driver); + bus_unregister(&rpmsg_bus); +} +module_exit(fini); + +MODULE_DEVICE_TABLE(virtio, id_table); +MODULE_DESCRIPTION("Virtio-based remote processor messaging bus"); +MODULE_LICENSE("GPL v2"); diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h index ae28e93..561567e 100644 --- a/include/linux/mod_devicetable.h +++ b/include/linux/mod_devicetable.h @@ -533,4 +533,14 @@ struct isapnp_device_id { kernel_ulong_t driver_data; /* data private to the driver */ }; +/* rpmsg */ + +#define RPMSG_NAME_SIZE 32 +#define RPMSG_DEVICE_MODALIAS_FMT "rpmsg:%s" + +struct rpmsg_device_id { + char name[RPMSG_NAME_SIZE]; + kernel_ulong_t driver_data /* Data private to the driver */ + __attribute__((aligned(sizeof(kernel_ulong_t)))); +}; #endif /* LINUX_MOD_DEVICETABLE_H */ diff --git a/include/linux/rpmsg.h b/include/linux/rpmsg.h new file mode 100644 index 0000000..1f7ba09 --- /dev/null +++ b/include/linux/rpmsg.h @@ -0,0 +1,191 @@ +/* + * Remote processor messaging + * + * Copyright(c) 2011 Texas Instruments. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name Texas Instruments nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _LINUX_RPMSG_H +#define _LINUX_RPMSG_H + +#include <linux/types.h> +#include <linux/device.h> +#include <linux/mod_devicetable.h> + +/* The feature bitmap for virtio rpmsg */ +#define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */ + +/** + * struct rpmsg_hdr - + * + * ... keep documenting ... + */ +struct rpmsg_hdr { + u16 len; + u16 flags; + u32 src; + u32 dst; + u32 unused; + u8 data[0]; +} __packed; + +enum rpmsg_ns_flags { + RPMSG_NS_CREATE = 0, + RPMSG_NS_DESTROY = 1, +}; + +struct rpmsg_ns_msg { + char name[RPMSG_NAME_SIZE]; + u32 addr; + u32 flags; +} __packed; + +/* driver requests */ +enum { + VPROC_BUF_ADDR, + VPROC_BUF_NUM, + VPROC_BUF_SZ, + VPROC_SIM_BASE, + VPROC_STATIC_CHANNELS, +}; + +#define RPMSG_ADDR_ANY 0xFFFFFFFF + +struct virtproc_info; + +/** + * rpmsg_channel - rpmsg channels are the devices of the rpmsg bus + * + * @vrp: the remote processor this channel connects to + * @dev: underlying device + * @id: the device type identification (used to match an rpmsg driver) + * @src: local address of this channel + * @dst: destination address of the remote service + * @priv: private pointer for the driver's use. + * @ept: local rpmsg endpoint of this channel + * @announce: need to tell remoteproc about channel creation/removal + */ +struct rpmsg_channel { + struct virtproc_info *vrp; + struct device dev; + struct rpmsg_device_id id; + u32 src; + u32 dst; + void *priv; + struct rpmsg_endpoint *ept; + bool announce; +}; + +struct rpmsg_channel_info { + char name[RPMSG_NAME_SIZE]; + u32 src; + u32 dst; +}; + +/** + * struct rpmsg_endpoint + * + * @rpdev: + * @cb: + * @src: local rpmsg address + * @priv: + */ +struct rpmsg_endpoint { + struct rpmsg_channel *rpdev; + void (*cb)(struct rpmsg_channel *, void *, int, void *, u32); + u32 addr; + void *priv; +}; + +/** + * rpmsg_driver - operations for a rpmsg I/O driver + * @driver: underlying device driver (populate name and owner). + * @id_table: the ids serviced by this driver. + * @probe: the function to call when a device is found. Returns 0 or -errno. + * @remove: the function when a device is removed. + * @callback: invoked when a message is received on the channel + */ +struct rpmsg_driver { + struct device_driver drv; + const struct rpmsg_device_id *id_table; + int (*probe)(struct rpmsg_channel *dev); + void (*remove)(struct rpmsg_channel *dev); + void (*callback)(struct rpmsg_channel *, void *, int, void *, u32); +}; + +int register_rpmsg_device(struct rpmsg_channel *dev); +void unregister_rpmsg_device(struct rpmsg_channel *dev); +int register_rpmsg_driver(struct rpmsg_driver *drv); +void unregister_rpmsg_driver(struct rpmsg_driver *drv); +void rpmsg_destroy_ept(struct rpmsg_endpoint *); +struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *, + void (*cb)(struct rpmsg_channel *, void *, int, void *, u32), + void *priv, u32 addr); + +int +rpmsg_send_offchannel_raw(struct rpmsg_channel *, u32, u32, void *, int, bool); + +static inline +int rpmsg_send_offchannel(struct rpmsg_channel *rpdev, u32 src, u32 dst, + void *data, int len) +{ + return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true); +} + +static inline int rpmsg_send(struct rpmsg_channel *rpdev, void *data, int len) +{ + return rpmsg_send_offchannel(rpdev, rpdev->src, rpdev->dst, data, len); +} + +static inline +int rpmsg_sendto(struct rpmsg_channel *rpdev, void *data, int len, u32 dst) +{ + return rpmsg_send_offchannel(rpdev, rpdev->src, dst, data, len); +} + +static inline +int rpmsg_trysend_offchannel(struct rpmsg_channel *rpdev, u32 src, u32 dst, + void *data, int len) +{ + return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false); +} + +static inline +int rpmsg_trysend(struct rpmsg_channel *rpdev, void *data, int len) +{ + return rpmsg_trysend_offchannel(rpdev, rpdev->src, rpdev->dst, + data, len); +} + +static inline +int rpmsg_trysendto(struct rpmsg_channel *rpdev, void *data, int len, u32 dst) +{ + return rpmsg_trysend_offchannel(rpdev, rpdev->src, dst, data, len); +} + +#endif /* _LINUX_RPMSG_H */ diff --git a/include/linux/virtio_ids.h b/include/linux/virtio_ids.h index 85bb0bb..1520c06 100644 --- a/include/linux/virtio_ids.h +++ b/include/linux/virtio_ids.h @@ -35,5 +35,6 @@ #define VIRTIO_ID_RNG 4 /* virtio ring */ #define VIRTIO_ID_BALLOON 5 /* virtio balloon */ #define VIRTIO_ID_9P 9 /* 9p virtio console */ +#define VIRTIO_ID_RPMSG 10 /* virtio remote processor messaging */ #endif /* _LINUX_VIRTIO_IDS_H */ |