12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100 |
- /*
- * 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/dma-mapping.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>
- #include <linux/mutex.h>
- /**
- * struct virtproc_info - virtual remote processor state
- * @vdev: the virtio device
- * @rvq: rx virtqueue
- * @svq: tx virtqueue
- * @rbufs: kernel address of rx buffers
- * @sbufs: kernel address of tx buffers
- * @last_sbuf: index of last tx buffer used
- * @bufs_dma: dma base addr of the buffers
- * @tx_lock: protects svq, sbufs and sleepers, to allow concurrent senders.
- * sending a message might require waking up a dozing remote
- * processor, which involves sleeping, hence the mutex.
- * @endpoints: idr of local endpoints, allows fast retrieval
- * @endpoints_lock: lock of the endpoints set
- * @sendq: wait queue of sending contexts waiting for a tx buffers
- * @sleepers: number of senders that are waiting for a tx 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 proc devices for each physical
- * remote processor).
- */
- struct virtproc_info {
- struct virtio_device *vdev;
- struct virtqueue *rvq, *svq;
- void *rbufs, *sbufs;
- int last_sbuf;
- dma_addr_t bufs_dma;
- struct mutex tx_lock;
- struct idr endpoints;
- struct mutex endpoints_lock;
- wait_queue_head_t sendq;
- atomic_t sleepers;
- struct rpmsg_endpoint *ns_ept;
- };
- /**
- * struct rpmsg_channel_info - internal channel info representation
- * @name: name of service
- * @src: local address
- * @dst: destination address
- */
- struct rpmsg_channel_info {
- char name[RPMSG_NAME_SIZE];
- u32 src;
- u32 dst;
- };
- #define to_rpmsg_channel(d) container_of(d, struct rpmsg_channel, dev)
- #define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv)
- /*
- * We're allocating 512 buffers of 512 bytes for communications, and then
- * using the first 256 buffers for RX, and the last 256 buffers for TX.
- *
- * Each buffer will have 16 bytes for the msg header and 496 bytes for
- * the payload.
- *
- * This will require a total space of 256KB for the buffers.
- *
- * We might also want to add support for user-provided buffers in time.
- * This will allow bigger buffer size flexibility, and can also be used
- * to achieve zero-copy messaging.
- *
- * Note that these numbers are purely a decision of this driver - we
- * can change this without changing anything in the firmware of the remote
- * processor.
- */
- #define RPMSG_NUM_BUFS (512)
- #define RPMSG_BUF_SIZE (512)
- #define RPMSG_TOTAL_BUF_SPACE (RPMSG_NUM_BUFS * RPMSG_BUF_SIZE)
- /*
- * 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)
- /* sysfs 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); \
- }
- /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
- rpmsg_show_attr(name, id.name, "%s\n");
- rpmsg_show_attr(src, src, "0x%x\n");
- rpmsg_show_attr(dst, dst, "0x%x\n");
- rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
- /*
- * Unique (and free running) index for rpmsg devices.
- *
- * Yeah, we're not recycling those numbers (yet?). will be easy
- * to change if/when we want to.
- */
- 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
- };
- /* rpmsg devices and drivers are matched using the service name */
- static inline int rpmsg_id_match(const struct rpmsg_channel *rpdev,
- const struct rpmsg_device_id *id)
- {
- return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
- }
- /* 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);
- }
- /**
- * __ept_release() - deallocate an rpmsg endpoint
- * @kref: the ept's reference count
- *
- * This function deallocates an ept, and is invoked when its @kref refcount
- * drops to zero.
- *
- * Never invoke this function directly!
- */
- static void __ept_release(struct kref *kref)
- {
- struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
- refcount);
- /*
- * At this point no one holds a reference to ept anymore,
- * so we can directly free it
- */
- kfree(ept);
- }
- /* for more info, see below documentation of rpmsg_create_ept() */
- static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
- struct rpmsg_channel *rpdev, rpmsg_rx_cb_t cb,
- 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;
- }
- kref_init(&ept->refcount);
- mutex_init(&ept->cb_lock);
- 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;
- mutex_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;
- }
- /* make sure the user's address request is fulfilled, if relevant */
- if (addr != RPMSG_ADDR_ANY && tmpaddr != addr) {
- dev_err(dev, "address 0x%x already in use\n", addr);
- goto rem_idr;
- }
- ept->addr = tmpaddr;
- mutex_unlock(&vrp->endpoints_lock);
- return ept;
- rem_idr:
- idr_remove(&vrp->endpoints, request);
- free_ept:
- mutex_unlock(&vrp->endpoints_lock);
- kref_put(&ept->refcount, __ept_release);
- return NULL;
- }
- /**
- * rpmsg_create_ept() - create a new rpmsg_endpoint
- * @rpdev: rpmsg channel device
- * @cb: rx callback handler
- * @priv: private data for the driver's use
- * @addr: local rpmsg address to bind with @cb
- *
- * Every rpmsg address in the system is bound to an rx callback (so when
- * inbound messages arrive, they are dispatched by the rpmsg bus using the
- * appropriate callback handler) by means of an rpmsg_endpoint struct.
- *
- * This function allows drivers to create such an endpoint, and by that,
- * bind a callback, and possibly some private data too, to an rpmsg address
- * (either one that is known in advance, or one that will be dynamically
- * assigned for them).
- *
- * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
- * is already created for them when they are probed by the rpmsg bus
- * (using the rx callback provided when they registered to the rpmsg bus).
- *
- * So things should just work for simple drivers: they already have an
- * endpoint, their rx callback is bound to their rpmsg address, and when
- * relevant inbound messages arrive (i.e. messages which their dst address
- * equals to the src address of their rpmsg channel), the driver's handler
- * is invoked to process it.
- *
- * That said, more complicated drivers might do need to allocate
- * additional rpmsg addresses, and bind them to different rx callbacks.
- * To accomplish that, those drivers need to call this function.
- *
- * Drivers should provide their @rpdev channel (so the new endpoint would belong
- * to the same remote processor their channel belongs to), an rx callback
- * function, an optional private data (which is provided back when the
- * rx callback is invoked), and an address they want to bind with the
- * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
- * dynamically assign them an available rpmsg address (drivers should have
- * a very good reason why not to always use RPMSG_ADDR_ANY here).
- *
- * Returns a pointer to the endpoint on success, or NULL on error.
- */
- struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *rpdev,
- rpmsg_rx_cb_t cb, void *priv, u32 addr)
- {
- return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, addr);
- }
- EXPORT_SYMBOL(rpmsg_create_ept);
- /**
- * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
- * @vrp: virtproc which owns this ept
- * @ept: endpoing to destroy
- *
- * An internal function which destroy an ept without assuming it is
- * bound to an rpmsg channel. This is needed for handling the internal
- * name service endpoint, which isn't bound to an rpmsg channel.
- * See also __rpmsg_create_ept().
- */
- static void
- __rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
- {
- /* make sure new inbound messages can't find this ept anymore */
- mutex_lock(&vrp->endpoints_lock);
- idr_remove(&vrp->endpoints, ept->addr);
- mutex_unlock(&vrp->endpoints_lock);
- /* make sure in-flight inbound messages won't invoke cb anymore */
- mutex_lock(&ept->cb_lock);
- ept->cb = NULL;
- mutex_unlock(&ept->cb_lock);
- kref_put(&ept->refcount, __ept_release);
- }
- /**
- * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
- * @ept: endpoing to destroy
- *
- * Should be used by drivers to destroy an rpmsg endpoint previously
- * created with rpmsg_create_ept().
- */
- void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
- {
- __rpmsg_destroy_ept(ept->rpdev->vrp, ept);
- }
- EXPORT_SYMBOL(rpmsg_destroy_ept);
- /*
- * when an rpmsg driver is probed with a channel, we seamlessly create
- * it an endpoint, binding its rx callback to a unique local rpmsg
- * address.
- *
- * if we need to, we also announce about this channel to the remote
- * processor (needed in case the driver is exposing an rpmsg service).
- */
- 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,
- };
- /**
- * register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
- * @rpdrv: pointer to a struct rpmsg_driver
- *
- * Returns 0 on success, and an appropriate error value on failure.
- */
- int register_rpmsg_driver(struct rpmsg_driver *rpdrv)
- {
- rpdrv->drv.bus = &rpmsg_bus;
- return driver_register(&rpdrv->drv);
- }
- EXPORT_SYMBOL(register_rpmsg_driver);
- /**
- * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
- * @rpdrv: pointer to a struct rpmsg_driver
- *
- * Returns 0 on success, and an appropriate error value on failure.
- */
- 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 a channel info struct.
- * this is used to make sure we're not creating rpmsg devices for channels
- * that already exist.
- */
- 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;
- /* found a match ! */
- return 1;
- }
- /*
- * create an rpmsg channel using its name and address info.
- * this function will be used to create both static and dynamic
- * channels.
- */
- 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) {
- /* decrement the matched device's refcount back */
- put_device(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 (for now),
- * 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 is enough 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);
- put_device(&rpdev->dev);
- return NULL;
- }
- return rpdev;
- }
- /*
- * find an existing channel using its name + address properties,
- * and destroy it
- */
- static int rpmsg_destroy_channel(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;
- device_unregister(dev);
- put_device(dev);
- return 0;
- }
- /* super simple buffer "allocator" that is just enough for now */
- static void *get_a_tx_buf(struct virtproc_info *vrp)
- {
- unsigned int len;
- void *ret;
- /* support multiple concurrent senders */
- mutex_lock(&vrp->tx_lock);
- /*
- * either pick the next unused tx buffer
- * (half of our buffers are used for sending messages)
- */
- if (vrp->last_sbuf < RPMSG_NUM_BUFS / 2)
- ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
- /* or recycle a used one */
- else
- ret = virtqueue_get_buf(vrp->svq, &len);
- mutex_unlock(&vrp->tx_lock);
- return ret;
- }
- /**
- * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
- * @vrp: virtual remote processor state
- *
- * This function is called before a sender is blocked, waiting for
- * a tx buffer to become available.
- *
- * If we already have blocking senders, this function merely increases
- * the "sleepers" reference count, and exits.
- *
- * Otherwise, if this is the first sender to block, we also enable
- * virtio's tx callbacks, so we'd be immediately notified when a tx
- * buffer is consumed (we rely on virtio's tx callback in order
- * to wake up sleeping senders as soon as a tx buffer is used by the
- * remote processor).
- */
- static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
- {
- /* support multiple concurrent senders */
- mutex_lock(&vrp->tx_lock);
- /* are we the first sleeping context waiting for tx buffers ? */
- if (atomic_inc_return(&vrp->sleepers) == 1)
- /* enable "tx-complete" interrupts before dozing off */
- virtqueue_enable_cb(vrp->svq);
- mutex_unlock(&vrp->tx_lock);
- }
- /**
- * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
- * @vrp: virtual remote processor state
- *
- * This function is called after a sender, that waited for a tx buffer
- * to become available, is unblocked.
- *
- * If we still have blocking senders, this function merely decreases
- * the "sleepers" reference count, and exits.
- *
- * Otherwise, if there are no more blocking senders, we also disable
- * virtio's tx callbacks, to avoid the overhead incurred with handling
- * those (now redundant) interrupts.
- */
- static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
- {
- /* support multiple concurrent senders */
- mutex_lock(&vrp->tx_lock);
- /* are we the last sleeping context waiting for tx buffers ? */
- if (atomic_dec_and_test(&vrp->sleepers))
- /* disable "tx-complete" interrupts */
- virtqueue_disable_cb(vrp->svq);
- mutex_unlock(&vrp->tx_lock);
- }
- /**
- * rpmsg_send_offchannel_raw() - send a message across to the remote processor
- * @rpdev: the rpmsg channel
- * @src: source address
- * @dst: destination address
- * @data: payload of message
- * @len: length of payload
- * @wait: indicates whether caller should block in case no TX buffers available
- *
- * This function is the base implementation for all of the rpmsg sending API.
- *
- * It will send @data of length @len to @dst, and say it's from @src. The
- * message will be sent to the remote processor which the @rpdev channel
- * belongs to.
- *
- * The message is sent using one of the TX buffers that are available for
- * communication with this remote processor.
- *
- * If @wait is true, the caller will be blocked until either a TX buffer is
- * available, or 15 seconds elapses (we don't want callers to
- * sleep indefinitely due to misbehaving remote processors), and in that
- * case -ERESTARTSYS is returned. The number '15' itself was picked
- * arbitrarily; there's little point in asking drivers to provide a timeout
- * value themselves.
- *
- * Otherwise, if @wait is false, and there are no TX buffers available,
- * the function will immediately fail, and -ENOMEM will be returned.
- *
- * Normally drivers shouldn't use this function directly; instead, drivers
- * should use the appropriate rpmsg_{try}send{to, _offchannel} API
- * (see include/linux/rpmsg.h).
- *
- * Returns 0 on success and an appropriate error value on failure.
- */
- 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;
- /* bcasting isn't allowed */
- 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;
- }
- /*
- * We currently use fixed-sized buffers, and therefore the payload
- * length is limited.
- *
- * One of the possible improvements here is either to support
- * user-provided buffers (and then we can also support zero-copy
- * messaging), or to improve the buffer allocator, to support
- * variable-length buffer sizes.
- */
- if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
- dev_err(dev, "message is too big (%d)\n", len);
- return -EMSGSIZE;
- }
- /* grab a buffer */
- msg = get_a_tx_buf(vrp);
- if (!msg && !wait)
- return -ENOMEM;
- /* no free buffer ? wait for one (but bail after 15 seconds) */
- while (!msg) {
- /* enable "tx-complete" interrupts, if not already enabled */
- rpmsg_upref_sleepers(vrp);
- /*
- * sleep until a free buffer is available or 15 secs elapse.
- * the timeout period is not configurable because there's
- * little point in asking drivers to specify that.
- * if later this happens to be required, it'd be easy to add.
- */
- err = wait_event_interruptible_timeout(vrp->sendq,
- (msg = get_a_tx_buf(vrp)),
- msecs_to_jiffies(15000));
- /* disable "tx-complete" interrupts if we're the last sleeper */
- rpmsg_downref_sleepers(vrp);
- /* timeout ? */
- if (!err) {
- dev_err(dev, "timeout waiting for a tx buffer\n");
- return -ERESTARTSYS;
- }
- }
- msg->len = len;
- msg->flags = 0;
- msg->src = src;
- msg->dst = dst;
- msg->reserved = 0;
- memcpy(msg->data, data, len);
- dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
- msg->src, msg->dst, msg->len,
- msg->flags, msg->reserved);
- print_hex_dump(KERN_DEBUG, "rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
- msg, sizeof(*msg) + msg->len, true);
- sg_init_one(&sg, msg, sizeof(*msg) + len);
- mutex_lock(&vrp->tx_lock);
- /* add message to the remote processor's virtqueue */
- err = virtqueue_add_buf(vrp->svq, &sg, 1, 0, msg, GFP_KERNEL);
- if (err < 0) {
- /*
- * need to reclaim the buffer here, otherwise it's lost
- * (memory won't leak, but rpmsg won't use it again for TX).
- * this will wait for a buffer management overhaul.
- */
- dev_err(dev, "virtqueue_add_buf failed: %d\n", err);
- goto out;
- }
- /* tell the remote processor it has a pending message to read */
- virtqueue_kick(vrp->svq);
- err = 0;
- out:
- mutex_unlock(&vrp->tx_lock);
- return err;
- }
- EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
- /* called when an rx buffer is used, and it's time to digest a message */
- static void rpmsg_recv_done(struct virtqueue *rvq)
- {
- struct rpmsg_hdr *msg;
- unsigned int len;
- struct rpmsg_endpoint *ept;
- struct scatterlist sg;
- 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, Reserved: %d\n",
- msg->src, msg->dst, msg->len,
- msg->flags, msg->reserved);
- print_hex_dump(KERN_DEBUG, "rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
- msg, sizeof(*msg) + msg->len, true);
- /*
- * We currently use fixed-sized buffers, so trivially sanitize
- * the reported payload length.
- */
- if (len > RPMSG_BUF_SIZE ||
- msg->len > (len - sizeof(struct rpmsg_hdr))) {
- dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
- return;
- }
- /* use the dst addr to fetch the callback of the appropriate user */
- mutex_lock(&vrp->endpoints_lock);
- ept = idr_find(&vrp->endpoints, msg->dst);
- /* let's make sure no one deallocates ept while we use it */
- if (ept)
- kref_get(&ept->refcount);
- mutex_unlock(&vrp->endpoints_lock);
- if (ept) {
- /* make sure ept->cb doesn't go away while we use it */
- mutex_lock(&ept->cb_lock);
- if (ept->cb)
- ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
- msg->src);
- mutex_unlock(&ept->cb_lock);
- /* farewell, ept, we don't need you anymore */
- kref_put(&ept->refcount, __ept_release);
- } else
- dev_warn(dev, "msg received with no recipient\n");
- /* publish the real size of the buffer */
- sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
- /* add the buffer back to the remote processor's virtqueue */
- err = virtqueue_add_buf(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);
- }
- /*
- * This is invoked whenever the remote processor completed processing
- * a TX msg we just sent it, and the buffer is put back to the used ring.
- *
- * Normally, though, we suppress this "tx complete" interrupt in order to
- * avoid the incurred overhead.
- */
- 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 senders that are waiting for a tx buffer */
- wake_up_interruptible(&vrp->sendq);
- }
- /* invoked when a name service announcement arrives */
- 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, "NS announcement: ",
- 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(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 *bufs_va;
- int err = 0, i;
- vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
- if (!vrp)
- return -ENOMEM;
- vrp->vdev = vdev;
- idr_init(&vrp->endpoints);
- mutex_init(&vrp->endpoints_lock);
- mutex_init(&vrp->tx_lock);
- init_waitqueue_head(&vrp->sendq);
- /* We expect two virtqueues, rx and tx (and in this order) */
- err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
- if (err)
- goto free_vrp;
- vrp->rvq = vqs[0];
- vrp->svq = vqs[1];
- /* allocate coherent memory for the buffers */
- bufs_va = dma_alloc_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE,
- &vrp->bufs_dma, GFP_KERNEL);
- if (!bufs_va)
- goto vqs_del;
- dev_dbg(&vdev->dev, "buffers: va %p, dma 0x%llx\n", bufs_va,
- (unsigned long long)vrp->bufs_dma);
- /* half of the buffers is dedicated for RX */
- vrp->rbufs = bufs_va;
- /* and half is dedicated for TX */
- vrp->sbufs = bufs_va + RPMSG_TOTAL_BUF_SPACE / 2;
- /* set up the receive buffers */
- for (i = 0; i < RPMSG_NUM_BUFS / 2; i++) {
- struct scatterlist sg;
- void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
- sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
- err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, cpu_addr,
- GFP_KERNEL);
- WARN_ON(err < 0); /* sanity check; this can't really happen */
- }
- /* suppress "tx-complete" interrupts */
- virtqueue_disable_cb(vrp->svq);
- vdev->priv = vrp;
- /* if supported by the remote processor, enable the name service */
- if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
- /* a dedicated endpoint handles the name service msgs */
- 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 free_coherent;
- }
- }
- /* tell the remote processor it can start sending messages */
- virtqueue_kick(vrp->rvq);
- dev_info(&vdev->dev, "rpmsg host is online\n");
- return 0;
- free_coherent:
- dma_free_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE, bufs_va,
- vrp->bufs_dma);
- vqs_del:
- vdev->config->del_vqs(vrp->vdev);
- free_vrp:
- kfree(vrp);
- return err;
- }
- static int rpmsg_remove_device(struct device *dev, void *data)
- {
- device_unregister(dev);
- return 0;
- }
- static void __devexit rpmsg_remove(struct virtio_device *vdev)
- {
- struct virtproc_info *vrp = vdev->priv;
- int ret;
- vdev->config->reset(vdev);
- 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);
- if (vrp->ns_ept)
- __rpmsg_destroy_ept(vrp, vrp->ns_ept);
- idr_remove_all(&vrp->endpoints);
- idr_destroy(&vrp->endpoints);
- vdev->config->del_vqs(vrp->vdev);
- dma_free_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE,
- vrp->rbufs, vrp->bufs_dma);
- 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 rpmsg_init(void)
- {
- int ret;
- ret = bus_register(&rpmsg_bus);
- if (ret) {
- pr_err("failed to register rpmsg bus: %d\n", ret);
- return ret;
- }
- ret = register_virtio_driver(&virtio_ipc_driver);
- if (ret) {
- pr_err("failed to register virtio driver: %d\n", ret);
- bus_unregister(&rpmsg_bus);
- }
- return ret;
- }
- subsys_initcall(rpmsg_init);
- static void __exit rpmsg_fini(void)
- {
- unregister_virtio_driver(&virtio_ipc_driver);
- bus_unregister(&rpmsg_bus);
- }
- module_exit(rpmsg_fini);
- MODULE_DEVICE_TABLE(virtio, id_table);
- MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
- MODULE_LICENSE("GPL v2");
|