18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Virtio-based remote processor messaging bus 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2011 Texas Instruments, Inc. 68c2ecf20Sopenharmony_ci * Copyright (C) 2011 Google, Inc. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Ohad Ben-Cohen <ohad@wizery.com> 98c2ecf20Sopenharmony_ci * Brian Swetland <swetland@google.com> 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "%s: " fmt, __func__ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h> 158c2ecf20Sopenharmony_ci#include <linux/idr.h> 168c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 178c2ecf20Sopenharmony_ci#include <linux/kernel.h> 188c2ecf20Sopenharmony_ci#include <linux/module.h> 198c2ecf20Sopenharmony_ci#include <linux/mutex.h> 208c2ecf20Sopenharmony_ci#include <linux/of_device.h> 218c2ecf20Sopenharmony_ci#include <linux/rpmsg.h> 228c2ecf20Sopenharmony_ci#include <linux/scatterlist.h> 238c2ecf20Sopenharmony_ci#include <linux/slab.h> 248c2ecf20Sopenharmony_ci#include <linux/sched.h> 258c2ecf20Sopenharmony_ci#include <linux/virtio.h> 268c2ecf20Sopenharmony_ci#include <linux/virtio_byteorder.h> 278c2ecf20Sopenharmony_ci#include <linux/virtio_ids.h> 288c2ecf20Sopenharmony_ci#include <linux/virtio_config.h> 298c2ecf20Sopenharmony_ci#include <linux/wait.h> 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#include "rpmsg_internal.h" 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci/** 348c2ecf20Sopenharmony_ci * struct virtproc_info - virtual remote processor state 358c2ecf20Sopenharmony_ci * @vdev: the virtio device 368c2ecf20Sopenharmony_ci * @rvq: rx virtqueue 378c2ecf20Sopenharmony_ci * @svq: tx virtqueue 388c2ecf20Sopenharmony_ci * @rbufs: kernel address of rx buffers 398c2ecf20Sopenharmony_ci * @sbufs: kernel address of tx buffers 408c2ecf20Sopenharmony_ci * @num_bufs: total number of buffers for rx and tx 418c2ecf20Sopenharmony_ci * @buf_size: size of one rx or tx buffer 428c2ecf20Sopenharmony_ci * @last_sbuf: index of last tx buffer used 438c2ecf20Sopenharmony_ci * @bufs_dma: dma base addr of the buffers 448c2ecf20Sopenharmony_ci * @tx_lock: protects svq, sbufs and sleepers, to allow concurrent senders. 458c2ecf20Sopenharmony_ci * sending a message might require waking up a dozing remote 468c2ecf20Sopenharmony_ci * processor, which involves sleeping, hence the mutex. 478c2ecf20Sopenharmony_ci * @endpoints: idr of local endpoints, allows fast retrieval 488c2ecf20Sopenharmony_ci * @endpoints_lock: lock of the endpoints set 498c2ecf20Sopenharmony_ci * @sendq: wait queue of sending contexts waiting for a tx buffers 508c2ecf20Sopenharmony_ci * @sleepers: number of senders that are waiting for a tx buffer 518c2ecf20Sopenharmony_ci * @ns_ept: the bus's name service endpoint 528c2ecf20Sopenharmony_ci * 538c2ecf20Sopenharmony_ci * This structure stores the rpmsg state of a given virtio remote processor 548c2ecf20Sopenharmony_ci * device (there might be several virtio proc devices for each physical 558c2ecf20Sopenharmony_ci * remote processor). 568c2ecf20Sopenharmony_ci */ 578c2ecf20Sopenharmony_cistruct virtproc_info { 588c2ecf20Sopenharmony_ci struct virtio_device *vdev; 598c2ecf20Sopenharmony_ci struct virtqueue *rvq, *svq; 608c2ecf20Sopenharmony_ci void *rbufs, *sbufs; 618c2ecf20Sopenharmony_ci unsigned int num_bufs; 628c2ecf20Sopenharmony_ci unsigned int buf_size; 638c2ecf20Sopenharmony_ci int last_sbuf; 648c2ecf20Sopenharmony_ci dma_addr_t bufs_dma; 658c2ecf20Sopenharmony_ci struct mutex tx_lock; 668c2ecf20Sopenharmony_ci struct idr endpoints; 678c2ecf20Sopenharmony_ci struct mutex endpoints_lock; 688c2ecf20Sopenharmony_ci wait_queue_head_t sendq; 698c2ecf20Sopenharmony_ci atomic_t sleepers; 708c2ecf20Sopenharmony_ci struct rpmsg_endpoint *ns_ept; 718c2ecf20Sopenharmony_ci}; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci/* The feature bitmap for virtio rpmsg */ 748c2ecf20Sopenharmony_ci#define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */ 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci/** 778c2ecf20Sopenharmony_ci * struct rpmsg_hdr - common header for all rpmsg messages 788c2ecf20Sopenharmony_ci * @src: source address 798c2ecf20Sopenharmony_ci * @dst: destination address 808c2ecf20Sopenharmony_ci * @reserved: reserved for future use 818c2ecf20Sopenharmony_ci * @len: length of payload (in bytes) 828c2ecf20Sopenharmony_ci * @flags: message flags 838c2ecf20Sopenharmony_ci * @data: @len bytes of message payload data 848c2ecf20Sopenharmony_ci * 858c2ecf20Sopenharmony_ci * Every message sent(/received) on the rpmsg bus begins with this header. 868c2ecf20Sopenharmony_ci */ 878c2ecf20Sopenharmony_cistruct rpmsg_hdr { 888c2ecf20Sopenharmony_ci __virtio32 src; 898c2ecf20Sopenharmony_ci __virtio32 dst; 908c2ecf20Sopenharmony_ci __virtio32 reserved; 918c2ecf20Sopenharmony_ci __virtio16 len; 928c2ecf20Sopenharmony_ci __virtio16 flags; 938c2ecf20Sopenharmony_ci u8 data[]; 948c2ecf20Sopenharmony_ci} __packed; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci/** 978c2ecf20Sopenharmony_ci * struct rpmsg_ns_msg - dynamic name service announcement message 988c2ecf20Sopenharmony_ci * @name: name of remote service that is published 998c2ecf20Sopenharmony_ci * @addr: address of remote service that is published 1008c2ecf20Sopenharmony_ci * @flags: indicates whether service is created or destroyed 1018c2ecf20Sopenharmony_ci * 1028c2ecf20Sopenharmony_ci * This message is sent across to publish a new service, or announce 1038c2ecf20Sopenharmony_ci * about its removal. When we receive these messages, an appropriate 1048c2ecf20Sopenharmony_ci * rpmsg channel (i.e device) is created/destroyed. In turn, the ->probe() 1058c2ecf20Sopenharmony_ci * or ->remove() handler of the appropriate rpmsg driver will be invoked 1068c2ecf20Sopenharmony_ci * (if/as-soon-as one is registered). 1078c2ecf20Sopenharmony_ci */ 1088c2ecf20Sopenharmony_cistruct rpmsg_ns_msg { 1098c2ecf20Sopenharmony_ci char name[RPMSG_NAME_SIZE]; 1108c2ecf20Sopenharmony_ci __virtio32 addr; 1118c2ecf20Sopenharmony_ci __virtio32 flags; 1128c2ecf20Sopenharmony_ci} __packed; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci/** 1158c2ecf20Sopenharmony_ci * enum rpmsg_ns_flags - dynamic name service announcement flags 1168c2ecf20Sopenharmony_ci * 1178c2ecf20Sopenharmony_ci * @RPMSG_NS_CREATE: a new remote service was just created 1188c2ecf20Sopenharmony_ci * @RPMSG_NS_DESTROY: a known remote service was just destroyed 1198c2ecf20Sopenharmony_ci */ 1208c2ecf20Sopenharmony_cienum rpmsg_ns_flags { 1218c2ecf20Sopenharmony_ci RPMSG_NS_CREATE = 0, 1228c2ecf20Sopenharmony_ci RPMSG_NS_DESTROY = 1, 1238c2ecf20Sopenharmony_ci}; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci/** 1268c2ecf20Sopenharmony_ci * struct virtio_rpmsg_channel - rpmsg channel descriptor 1278c2ecf20Sopenharmony_ci * @rpdev: the rpmsg channel device 1288c2ecf20Sopenharmony_ci * @vrp: the virtio remote processor device this channel belongs to 1298c2ecf20Sopenharmony_ci * 1308c2ecf20Sopenharmony_ci * This structure stores the channel that links the rpmsg device to the virtio 1318c2ecf20Sopenharmony_ci * remote processor device. 1328c2ecf20Sopenharmony_ci */ 1338c2ecf20Sopenharmony_cistruct virtio_rpmsg_channel { 1348c2ecf20Sopenharmony_ci struct rpmsg_device rpdev; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci struct virtproc_info *vrp; 1378c2ecf20Sopenharmony_ci}; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci#define to_virtio_rpmsg_channel(_rpdev) \ 1408c2ecf20Sopenharmony_ci container_of(_rpdev, struct virtio_rpmsg_channel, rpdev) 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci/* 1438c2ecf20Sopenharmony_ci * We're allocating buffers of 512 bytes each for communications. The 1448c2ecf20Sopenharmony_ci * number of buffers will be computed from the number of buffers supported 1458c2ecf20Sopenharmony_ci * by the vring, upto a maximum of 512 buffers (256 in each direction). 1468c2ecf20Sopenharmony_ci * 1478c2ecf20Sopenharmony_ci * Each buffer will have 16 bytes for the msg header and 496 bytes for 1488c2ecf20Sopenharmony_ci * the payload. 1498c2ecf20Sopenharmony_ci * 1508c2ecf20Sopenharmony_ci * This will utilize a maximum total space of 256KB for the buffers. 1518c2ecf20Sopenharmony_ci * 1528c2ecf20Sopenharmony_ci * We might also want to add support for user-provided buffers in time. 1538c2ecf20Sopenharmony_ci * This will allow bigger buffer size flexibility, and can also be used 1548c2ecf20Sopenharmony_ci * to achieve zero-copy messaging. 1558c2ecf20Sopenharmony_ci * 1568c2ecf20Sopenharmony_ci * Note that these numbers are purely a decision of this driver - we 1578c2ecf20Sopenharmony_ci * can change this without changing anything in the firmware of the remote 1588c2ecf20Sopenharmony_ci * processor. 1598c2ecf20Sopenharmony_ci */ 1608c2ecf20Sopenharmony_ci#define MAX_RPMSG_NUM_BUFS (512) 1618c2ecf20Sopenharmony_ci#define MAX_RPMSG_BUF_SIZE (512) 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci/* 1648c2ecf20Sopenharmony_ci * Local addresses are dynamically allocated on-demand. 1658c2ecf20Sopenharmony_ci * We do not dynamically assign addresses from the low 1024 range, 1668c2ecf20Sopenharmony_ci * in order to reserve that address range for predefined services. 1678c2ecf20Sopenharmony_ci */ 1688c2ecf20Sopenharmony_ci#define RPMSG_RESERVED_ADDRESSES (1024) 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci/* Address 53 is reserved for advertising remote services */ 1718c2ecf20Sopenharmony_ci#define RPMSG_NS_ADDR (53) 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_cistatic void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept); 1748c2ecf20Sopenharmony_cistatic int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len); 1758c2ecf20Sopenharmony_cistatic int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, 1768c2ecf20Sopenharmony_ci u32 dst); 1778c2ecf20Sopenharmony_cistatic int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, 1788c2ecf20Sopenharmony_ci u32 dst, void *data, int len); 1798c2ecf20Sopenharmony_cistatic int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len); 1808c2ecf20Sopenharmony_cistatic int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, 1818c2ecf20Sopenharmony_ci int len, u32 dst); 1828c2ecf20Sopenharmony_cistatic int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, 1838c2ecf20Sopenharmony_ci u32 dst, void *data, int len); 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_cistatic const struct rpmsg_endpoint_ops virtio_endpoint_ops = { 1868c2ecf20Sopenharmony_ci .destroy_ept = virtio_rpmsg_destroy_ept, 1878c2ecf20Sopenharmony_ci .send = virtio_rpmsg_send, 1888c2ecf20Sopenharmony_ci .sendto = virtio_rpmsg_sendto, 1898c2ecf20Sopenharmony_ci .send_offchannel = virtio_rpmsg_send_offchannel, 1908c2ecf20Sopenharmony_ci .trysend = virtio_rpmsg_trysend, 1918c2ecf20Sopenharmony_ci .trysendto = virtio_rpmsg_trysendto, 1928c2ecf20Sopenharmony_ci .trysend_offchannel = virtio_rpmsg_trysend_offchannel, 1938c2ecf20Sopenharmony_ci}; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci/** 1968c2ecf20Sopenharmony_ci * rpmsg_sg_init - initialize scatterlist according to cpu address location 1978c2ecf20Sopenharmony_ci * @sg: scatterlist to fill 1988c2ecf20Sopenharmony_ci * @cpu_addr: virtual address of the buffer 1998c2ecf20Sopenharmony_ci * @len: buffer length 2008c2ecf20Sopenharmony_ci * 2018c2ecf20Sopenharmony_ci * An internal function filling scatterlist according to virtual address 2028c2ecf20Sopenharmony_ci * location (in vmalloc or in kernel). 2038c2ecf20Sopenharmony_ci */ 2048c2ecf20Sopenharmony_cistatic void 2058c2ecf20Sopenharmony_cirpmsg_sg_init(struct scatterlist *sg, void *cpu_addr, unsigned int len) 2068c2ecf20Sopenharmony_ci{ 2078c2ecf20Sopenharmony_ci if (is_vmalloc_addr(cpu_addr)) { 2088c2ecf20Sopenharmony_ci sg_init_table(sg, 1); 2098c2ecf20Sopenharmony_ci sg_set_page(sg, vmalloc_to_page(cpu_addr), len, 2108c2ecf20Sopenharmony_ci offset_in_page(cpu_addr)); 2118c2ecf20Sopenharmony_ci } else { 2128c2ecf20Sopenharmony_ci WARN_ON(!virt_addr_valid(cpu_addr)); 2138c2ecf20Sopenharmony_ci sg_init_one(sg, cpu_addr, len); 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci/** 2188c2ecf20Sopenharmony_ci * __ept_release() - deallocate an rpmsg endpoint 2198c2ecf20Sopenharmony_ci * @kref: the ept's reference count 2208c2ecf20Sopenharmony_ci * 2218c2ecf20Sopenharmony_ci * This function deallocates an ept, and is invoked when its @kref refcount 2228c2ecf20Sopenharmony_ci * drops to zero. 2238c2ecf20Sopenharmony_ci * 2248c2ecf20Sopenharmony_ci * Never invoke this function directly! 2258c2ecf20Sopenharmony_ci */ 2268c2ecf20Sopenharmony_cistatic void __ept_release(struct kref *kref) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint, 2298c2ecf20Sopenharmony_ci refcount); 2308c2ecf20Sopenharmony_ci /* 2318c2ecf20Sopenharmony_ci * At this point no one holds a reference to ept anymore, 2328c2ecf20Sopenharmony_ci * so we can directly free it 2338c2ecf20Sopenharmony_ci */ 2348c2ecf20Sopenharmony_ci kfree(ept); 2358c2ecf20Sopenharmony_ci} 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci/* for more info, see below documentation of rpmsg_create_ept() */ 2388c2ecf20Sopenharmony_cistatic struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp, 2398c2ecf20Sopenharmony_ci struct rpmsg_device *rpdev, 2408c2ecf20Sopenharmony_ci rpmsg_rx_cb_t cb, 2418c2ecf20Sopenharmony_ci void *priv, u32 addr) 2428c2ecf20Sopenharmony_ci{ 2438c2ecf20Sopenharmony_ci int id_min, id_max, id; 2448c2ecf20Sopenharmony_ci struct rpmsg_endpoint *ept; 2458c2ecf20Sopenharmony_ci struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev; 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci ept = kzalloc(sizeof(*ept), GFP_KERNEL); 2488c2ecf20Sopenharmony_ci if (!ept) 2498c2ecf20Sopenharmony_ci return NULL; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci kref_init(&ept->refcount); 2528c2ecf20Sopenharmony_ci mutex_init(&ept->cb_lock); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci ept->rpdev = rpdev; 2558c2ecf20Sopenharmony_ci ept->cb = cb; 2568c2ecf20Sopenharmony_ci ept->priv = priv; 2578c2ecf20Sopenharmony_ci ept->ops = &virtio_endpoint_ops; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci /* do we need to allocate a local address ? */ 2608c2ecf20Sopenharmony_ci if (addr == RPMSG_ADDR_ANY) { 2618c2ecf20Sopenharmony_ci id_min = RPMSG_RESERVED_ADDRESSES; 2628c2ecf20Sopenharmony_ci id_max = 0; 2638c2ecf20Sopenharmony_ci } else { 2648c2ecf20Sopenharmony_ci id_min = addr; 2658c2ecf20Sopenharmony_ci id_max = addr + 1; 2668c2ecf20Sopenharmony_ci } 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci mutex_lock(&vrp->endpoints_lock); 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci /* bind the endpoint to an rpmsg address (and allocate one if needed) */ 2718c2ecf20Sopenharmony_ci id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL); 2728c2ecf20Sopenharmony_ci if (id < 0) { 2738c2ecf20Sopenharmony_ci dev_err(dev, "idr_alloc failed: %d\n", id); 2748c2ecf20Sopenharmony_ci goto free_ept; 2758c2ecf20Sopenharmony_ci } 2768c2ecf20Sopenharmony_ci ept->addr = id; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci mutex_unlock(&vrp->endpoints_lock); 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci return ept; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_cifree_ept: 2838c2ecf20Sopenharmony_ci mutex_unlock(&vrp->endpoints_lock); 2848c2ecf20Sopenharmony_ci kref_put(&ept->refcount, __ept_release); 2858c2ecf20Sopenharmony_ci return NULL; 2868c2ecf20Sopenharmony_ci} 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_cistatic struct rpmsg_endpoint *virtio_rpmsg_create_ept(struct rpmsg_device *rpdev, 2898c2ecf20Sopenharmony_ci rpmsg_rx_cb_t cb, 2908c2ecf20Sopenharmony_ci void *priv, 2918c2ecf20Sopenharmony_ci struct rpmsg_channel_info chinfo) 2928c2ecf20Sopenharmony_ci{ 2938c2ecf20Sopenharmony_ci struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev); 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci return __rpmsg_create_ept(vch->vrp, rpdev, cb, priv, chinfo.src); 2968c2ecf20Sopenharmony_ci} 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci/** 2998c2ecf20Sopenharmony_ci * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint 3008c2ecf20Sopenharmony_ci * @vrp: virtproc which owns this ept 3018c2ecf20Sopenharmony_ci * @ept: endpoing to destroy 3028c2ecf20Sopenharmony_ci * 3038c2ecf20Sopenharmony_ci * An internal function which destroy an ept without assuming it is 3048c2ecf20Sopenharmony_ci * bound to an rpmsg channel. This is needed for handling the internal 3058c2ecf20Sopenharmony_ci * name service endpoint, which isn't bound to an rpmsg channel. 3068c2ecf20Sopenharmony_ci * See also __rpmsg_create_ept(). 3078c2ecf20Sopenharmony_ci */ 3088c2ecf20Sopenharmony_cistatic void 3098c2ecf20Sopenharmony_ci__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept) 3108c2ecf20Sopenharmony_ci{ 3118c2ecf20Sopenharmony_ci /* make sure new inbound messages can't find this ept anymore */ 3128c2ecf20Sopenharmony_ci mutex_lock(&vrp->endpoints_lock); 3138c2ecf20Sopenharmony_ci idr_remove(&vrp->endpoints, ept->addr); 3148c2ecf20Sopenharmony_ci mutex_unlock(&vrp->endpoints_lock); 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci /* make sure in-flight inbound messages won't invoke cb anymore */ 3178c2ecf20Sopenharmony_ci mutex_lock(&ept->cb_lock); 3188c2ecf20Sopenharmony_ci ept->cb = NULL; 3198c2ecf20Sopenharmony_ci mutex_unlock(&ept->cb_lock); 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci kref_put(&ept->refcount, __ept_release); 3228c2ecf20Sopenharmony_ci} 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_cistatic void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept) 3258c2ecf20Sopenharmony_ci{ 3268c2ecf20Sopenharmony_ci struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(ept->rpdev); 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci __rpmsg_destroy_ept(vch->vrp, ept); 3298c2ecf20Sopenharmony_ci} 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_cistatic int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev) 3328c2ecf20Sopenharmony_ci{ 3338c2ecf20Sopenharmony_ci struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev); 3348c2ecf20Sopenharmony_ci struct virtproc_info *vrp = vch->vrp; 3358c2ecf20Sopenharmony_ci struct device *dev = &rpdev->dev; 3368c2ecf20Sopenharmony_ci int err = 0; 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci /* need to tell remote processor's name service about this channel ? */ 3398c2ecf20Sopenharmony_ci if (rpdev->announce && rpdev->ept && 3408c2ecf20Sopenharmony_ci virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) { 3418c2ecf20Sopenharmony_ci struct rpmsg_ns_msg nsm; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE); 3448c2ecf20Sopenharmony_ci nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr); 3458c2ecf20Sopenharmony_ci nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE); 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR); 3488c2ecf20Sopenharmony_ci if (err) 3498c2ecf20Sopenharmony_ci dev_err(dev, "failed to announce service %d\n", err); 3508c2ecf20Sopenharmony_ci } 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci return err; 3538c2ecf20Sopenharmony_ci} 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_cistatic int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev) 3568c2ecf20Sopenharmony_ci{ 3578c2ecf20Sopenharmony_ci struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev); 3588c2ecf20Sopenharmony_ci struct virtproc_info *vrp = vch->vrp; 3598c2ecf20Sopenharmony_ci struct device *dev = &rpdev->dev; 3608c2ecf20Sopenharmony_ci int err = 0; 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci /* tell remote processor's name service we're removing this channel */ 3638c2ecf20Sopenharmony_ci if (rpdev->announce && rpdev->ept && 3648c2ecf20Sopenharmony_ci virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) { 3658c2ecf20Sopenharmony_ci struct rpmsg_ns_msg nsm; 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE); 3688c2ecf20Sopenharmony_ci nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr); 3698c2ecf20Sopenharmony_ci nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY); 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR); 3728c2ecf20Sopenharmony_ci if (err) 3738c2ecf20Sopenharmony_ci dev_err(dev, "failed to announce service %d\n", err); 3748c2ecf20Sopenharmony_ci } 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci return err; 3778c2ecf20Sopenharmony_ci} 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_cistatic const struct rpmsg_device_ops virtio_rpmsg_ops = { 3808c2ecf20Sopenharmony_ci .create_ept = virtio_rpmsg_create_ept, 3818c2ecf20Sopenharmony_ci .announce_create = virtio_rpmsg_announce_create, 3828c2ecf20Sopenharmony_ci .announce_destroy = virtio_rpmsg_announce_destroy, 3838c2ecf20Sopenharmony_ci}; 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_cistatic void virtio_rpmsg_release_device(struct device *dev) 3868c2ecf20Sopenharmony_ci{ 3878c2ecf20Sopenharmony_ci struct rpmsg_device *rpdev = to_rpmsg_device(dev); 3888c2ecf20Sopenharmony_ci struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev); 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci kfree(rpdev->driver_override); 3918c2ecf20Sopenharmony_ci kfree(vch); 3928c2ecf20Sopenharmony_ci} 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci/* 3958c2ecf20Sopenharmony_ci * create an rpmsg channel using its name and address info. 3968c2ecf20Sopenharmony_ci * this function will be used to create both static and dynamic 3978c2ecf20Sopenharmony_ci * channels. 3988c2ecf20Sopenharmony_ci */ 3998c2ecf20Sopenharmony_cistatic struct rpmsg_device *rpmsg_create_channel(struct virtproc_info *vrp, 4008c2ecf20Sopenharmony_ci struct rpmsg_channel_info *chinfo) 4018c2ecf20Sopenharmony_ci{ 4028c2ecf20Sopenharmony_ci struct virtio_rpmsg_channel *vch; 4038c2ecf20Sopenharmony_ci struct rpmsg_device *rpdev; 4048c2ecf20Sopenharmony_ci struct device *tmp, *dev = &vrp->vdev->dev; 4058c2ecf20Sopenharmony_ci int ret; 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci /* make sure a similar channel doesn't already exist */ 4088c2ecf20Sopenharmony_ci tmp = rpmsg_find_device(dev, chinfo); 4098c2ecf20Sopenharmony_ci if (tmp) { 4108c2ecf20Sopenharmony_ci /* decrement the matched device's refcount back */ 4118c2ecf20Sopenharmony_ci put_device(tmp); 4128c2ecf20Sopenharmony_ci dev_err(dev, "channel %s:%x:%x already exist\n", 4138c2ecf20Sopenharmony_ci chinfo->name, chinfo->src, chinfo->dst); 4148c2ecf20Sopenharmony_ci return NULL; 4158c2ecf20Sopenharmony_ci } 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci vch = kzalloc(sizeof(*vch), GFP_KERNEL); 4188c2ecf20Sopenharmony_ci if (!vch) 4198c2ecf20Sopenharmony_ci return NULL; 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci /* Link the channel to our vrp */ 4228c2ecf20Sopenharmony_ci vch->vrp = vrp; 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci /* Assign public information to the rpmsg_device */ 4258c2ecf20Sopenharmony_ci rpdev = &vch->rpdev; 4268c2ecf20Sopenharmony_ci rpdev->src = chinfo->src; 4278c2ecf20Sopenharmony_ci rpdev->dst = chinfo->dst; 4288c2ecf20Sopenharmony_ci rpdev->ops = &virtio_rpmsg_ops; 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci /* 4318c2ecf20Sopenharmony_ci * rpmsg server channels has predefined local address (for now), 4328c2ecf20Sopenharmony_ci * and their existence needs to be announced remotely 4338c2ecf20Sopenharmony_ci */ 4348c2ecf20Sopenharmony_ci rpdev->announce = rpdev->src != RPMSG_ADDR_ANY; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE); 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci rpdev->dev.parent = &vrp->vdev->dev; 4398c2ecf20Sopenharmony_ci rpdev->dev.release = virtio_rpmsg_release_device; 4408c2ecf20Sopenharmony_ci ret = rpmsg_register_device(rpdev); 4418c2ecf20Sopenharmony_ci if (ret) 4428c2ecf20Sopenharmony_ci return NULL; 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci return rpdev; 4458c2ecf20Sopenharmony_ci} 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci/* super simple buffer "allocator" that is just enough for now */ 4488c2ecf20Sopenharmony_cistatic void *get_a_tx_buf(struct virtproc_info *vrp) 4498c2ecf20Sopenharmony_ci{ 4508c2ecf20Sopenharmony_ci unsigned int len; 4518c2ecf20Sopenharmony_ci void *ret; 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci /* support multiple concurrent senders */ 4548c2ecf20Sopenharmony_ci mutex_lock(&vrp->tx_lock); 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci /* 4578c2ecf20Sopenharmony_ci * either pick the next unused tx buffer 4588c2ecf20Sopenharmony_ci * (half of our buffers are used for sending messages) 4598c2ecf20Sopenharmony_ci */ 4608c2ecf20Sopenharmony_ci if (vrp->last_sbuf < vrp->num_bufs / 2) 4618c2ecf20Sopenharmony_ci ret = vrp->sbufs + vrp->buf_size * vrp->last_sbuf++; 4628c2ecf20Sopenharmony_ci /* or recycle a used one */ 4638c2ecf20Sopenharmony_ci else 4648c2ecf20Sopenharmony_ci ret = virtqueue_get_buf(vrp->svq, &len); 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci mutex_unlock(&vrp->tx_lock); 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci return ret; 4698c2ecf20Sopenharmony_ci} 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci/** 4728c2ecf20Sopenharmony_ci * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed 4738c2ecf20Sopenharmony_ci * @vrp: virtual remote processor state 4748c2ecf20Sopenharmony_ci * 4758c2ecf20Sopenharmony_ci * This function is called before a sender is blocked, waiting for 4768c2ecf20Sopenharmony_ci * a tx buffer to become available. 4778c2ecf20Sopenharmony_ci * 4788c2ecf20Sopenharmony_ci * If we already have blocking senders, this function merely increases 4798c2ecf20Sopenharmony_ci * the "sleepers" reference count, and exits. 4808c2ecf20Sopenharmony_ci * 4818c2ecf20Sopenharmony_ci * Otherwise, if this is the first sender to block, we also enable 4828c2ecf20Sopenharmony_ci * virtio's tx callbacks, so we'd be immediately notified when a tx 4838c2ecf20Sopenharmony_ci * buffer is consumed (we rely on virtio's tx callback in order 4848c2ecf20Sopenharmony_ci * to wake up sleeping senders as soon as a tx buffer is used by the 4858c2ecf20Sopenharmony_ci * remote processor). 4868c2ecf20Sopenharmony_ci */ 4878c2ecf20Sopenharmony_cistatic void rpmsg_upref_sleepers(struct virtproc_info *vrp) 4888c2ecf20Sopenharmony_ci{ 4898c2ecf20Sopenharmony_ci /* support multiple concurrent senders */ 4908c2ecf20Sopenharmony_ci mutex_lock(&vrp->tx_lock); 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci /* are we the first sleeping context waiting for tx buffers ? */ 4938c2ecf20Sopenharmony_ci if (atomic_inc_return(&vrp->sleepers) == 1) 4948c2ecf20Sopenharmony_ci /* enable "tx-complete" interrupts before dozing off */ 4958c2ecf20Sopenharmony_ci virtqueue_enable_cb(vrp->svq); 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci mutex_unlock(&vrp->tx_lock); 4988c2ecf20Sopenharmony_ci} 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci/** 5018c2ecf20Sopenharmony_ci * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed 5028c2ecf20Sopenharmony_ci * @vrp: virtual remote processor state 5038c2ecf20Sopenharmony_ci * 5048c2ecf20Sopenharmony_ci * This function is called after a sender, that waited for a tx buffer 5058c2ecf20Sopenharmony_ci * to become available, is unblocked. 5068c2ecf20Sopenharmony_ci * 5078c2ecf20Sopenharmony_ci * If we still have blocking senders, this function merely decreases 5088c2ecf20Sopenharmony_ci * the "sleepers" reference count, and exits. 5098c2ecf20Sopenharmony_ci * 5108c2ecf20Sopenharmony_ci * Otherwise, if there are no more blocking senders, we also disable 5118c2ecf20Sopenharmony_ci * virtio's tx callbacks, to avoid the overhead incurred with handling 5128c2ecf20Sopenharmony_ci * those (now redundant) interrupts. 5138c2ecf20Sopenharmony_ci */ 5148c2ecf20Sopenharmony_cistatic void rpmsg_downref_sleepers(struct virtproc_info *vrp) 5158c2ecf20Sopenharmony_ci{ 5168c2ecf20Sopenharmony_ci /* support multiple concurrent senders */ 5178c2ecf20Sopenharmony_ci mutex_lock(&vrp->tx_lock); 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci /* are we the last sleeping context waiting for tx buffers ? */ 5208c2ecf20Sopenharmony_ci if (atomic_dec_and_test(&vrp->sleepers)) 5218c2ecf20Sopenharmony_ci /* disable "tx-complete" interrupts */ 5228c2ecf20Sopenharmony_ci virtqueue_disable_cb(vrp->svq); 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci mutex_unlock(&vrp->tx_lock); 5258c2ecf20Sopenharmony_ci} 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci/** 5288c2ecf20Sopenharmony_ci * rpmsg_send_offchannel_raw() - send a message across to the remote processor 5298c2ecf20Sopenharmony_ci * @rpdev: the rpmsg channel 5308c2ecf20Sopenharmony_ci * @src: source address 5318c2ecf20Sopenharmony_ci * @dst: destination address 5328c2ecf20Sopenharmony_ci * @data: payload of message 5338c2ecf20Sopenharmony_ci * @len: length of payload 5348c2ecf20Sopenharmony_ci * @wait: indicates whether caller should block in case no TX buffers available 5358c2ecf20Sopenharmony_ci * 5368c2ecf20Sopenharmony_ci * This function is the base implementation for all of the rpmsg sending API. 5378c2ecf20Sopenharmony_ci * 5388c2ecf20Sopenharmony_ci * It will send @data of length @len to @dst, and say it's from @src. The 5398c2ecf20Sopenharmony_ci * message will be sent to the remote processor which the @rpdev channel 5408c2ecf20Sopenharmony_ci * belongs to. 5418c2ecf20Sopenharmony_ci * 5428c2ecf20Sopenharmony_ci * The message is sent using one of the TX buffers that are available for 5438c2ecf20Sopenharmony_ci * communication with this remote processor. 5448c2ecf20Sopenharmony_ci * 5458c2ecf20Sopenharmony_ci * If @wait is true, the caller will be blocked until either a TX buffer is 5468c2ecf20Sopenharmony_ci * available, or 15 seconds elapses (we don't want callers to 5478c2ecf20Sopenharmony_ci * sleep indefinitely due to misbehaving remote processors), and in that 5488c2ecf20Sopenharmony_ci * case -ERESTARTSYS is returned. The number '15' itself was picked 5498c2ecf20Sopenharmony_ci * arbitrarily; there's little point in asking drivers to provide a timeout 5508c2ecf20Sopenharmony_ci * value themselves. 5518c2ecf20Sopenharmony_ci * 5528c2ecf20Sopenharmony_ci * Otherwise, if @wait is false, and there are no TX buffers available, 5538c2ecf20Sopenharmony_ci * the function will immediately fail, and -ENOMEM will be returned. 5548c2ecf20Sopenharmony_ci * 5558c2ecf20Sopenharmony_ci * Normally drivers shouldn't use this function directly; instead, drivers 5568c2ecf20Sopenharmony_ci * should use the appropriate rpmsg_{try}send{to, _offchannel} API 5578c2ecf20Sopenharmony_ci * (see include/linux/rpmsg.h). 5588c2ecf20Sopenharmony_ci * 5598c2ecf20Sopenharmony_ci * Returns 0 on success and an appropriate error value on failure. 5608c2ecf20Sopenharmony_ci */ 5618c2ecf20Sopenharmony_cistatic int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev, 5628c2ecf20Sopenharmony_ci u32 src, u32 dst, 5638c2ecf20Sopenharmony_ci void *data, int len, bool wait) 5648c2ecf20Sopenharmony_ci{ 5658c2ecf20Sopenharmony_ci struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev); 5668c2ecf20Sopenharmony_ci struct virtproc_info *vrp = vch->vrp; 5678c2ecf20Sopenharmony_ci struct device *dev = &rpdev->dev; 5688c2ecf20Sopenharmony_ci struct scatterlist sg; 5698c2ecf20Sopenharmony_ci struct rpmsg_hdr *msg; 5708c2ecf20Sopenharmony_ci int err; 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ci /* bcasting isn't allowed */ 5738c2ecf20Sopenharmony_ci if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) { 5748c2ecf20Sopenharmony_ci dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst); 5758c2ecf20Sopenharmony_ci return -EINVAL; 5768c2ecf20Sopenharmony_ci } 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_ci /* 5798c2ecf20Sopenharmony_ci * We currently use fixed-sized buffers, and therefore the payload 5808c2ecf20Sopenharmony_ci * length is limited. 5818c2ecf20Sopenharmony_ci * 5828c2ecf20Sopenharmony_ci * One of the possible improvements here is either to support 5838c2ecf20Sopenharmony_ci * user-provided buffers (and then we can also support zero-copy 5848c2ecf20Sopenharmony_ci * messaging), or to improve the buffer allocator, to support 5858c2ecf20Sopenharmony_ci * variable-length buffer sizes. 5868c2ecf20Sopenharmony_ci */ 5878c2ecf20Sopenharmony_ci if (len > vrp->buf_size - sizeof(struct rpmsg_hdr)) { 5888c2ecf20Sopenharmony_ci dev_err(dev, "message is too big (%d)\n", len); 5898c2ecf20Sopenharmony_ci return -EMSGSIZE; 5908c2ecf20Sopenharmony_ci } 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci /* grab a buffer */ 5938c2ecf20Sopenharmony_ci msg = get_a_tx_buf(vrp); 5948c2ecf20Sopenharmony_ci if (!msg && !wait) 5958c2ecf20Sopenharmony_ci return -ENOMEM; 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci /* no free buffer ? wait for one (but bail after 15 seconds) */ 5988c2ecf20Sopenharmony_ci while (!msg) { 5998c2ecf20Sopenharmony_ci /* enable "tx-complete" interrupts, if not already enabled */ 6008c2ecf20Sopenharmony_ci rpmsg_upref_sleepers(vrp); 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ci /* 6038c2ecf20Sopenharmony_ci * sleep until a free buffer is available or 15 secs elapse. 6048c2ecf20Sopenharmony_ci * the timeout period is not configurable because there's 6058c2ecf20Sopenharmony_ci * little point in asking drivers to specify that. 6068c2ecf20Sopenharmony_ci * if later this happens to be required, it'd be easy to add. 6078c2ecf20Sopenharmony_ci */ 6088c2ecf20Sopenharmony_ci err = wait_event_interruptible_timeout(vrp->sendq, 6098c2ecf20Sopenharmony_ci (msg = get_a_tx_buf(vrp)), 6108c2ecf20Sopenharmony_ci msecs_to_jiffies(15000)); 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_ci /* disable "tx-complete" interrupts if we're the last sleeper */ 6138c2ecf20Sopenharmony_ci rpmsg_downref_sleepers(vrp); 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci /* timeout ? */ 6168c2ecf20Sopenharmony_ci if (!err) { 6178c2ecf20Sopenharmony_ci dev_err(dev, "timeout waiting for a tx buffer\n"); 6188c2ecf20Sopenharmony_ci return -ERESTARTSYS; 6198c2ecf20Sopenharmony_ci } 6208c2ecf20Sopenharmony_ci } 6218c2ecf20Sopenharmony_ci 6228c2ecf20Sopenharmony_ci msg->len = cpu_to_virtio16(vrp->vdev, len); 6238c2ecf20Sopenharmony_ci msg->flags = 0; 6248c2ecf20Sopenharmony_ci msg->src = cpu_to_virtio32(vrp->vdev, src); 6258c2ecf20Sopenharmony_ci msg->dst = cpu_to_virtio32(vrp->vdev, dst); 6268c2ecf20Sopenharmony_ci msg->reserved = 0; 6278c2ecf20Sopenharmony_ci memcpy(msg->data, data, len); 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n", 6308c2ecf20Sopenharmony_ci src, dst, len, msg->flags, msg->reserved); 6318c2ecf20Sopenharmony_ci#if defined(CONFIG_DYNAMIC_DEBUG) 6328c2ecf20Sopenharmony_ci dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1, 6338c2ecf20Sopenharmony_ci msg, sizeof(*msg) + len, true); 6348c2ecf20Sopenharmony_ci#endif 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_ci rpmsg_sg_init(&sg, msg, sizeof(*msg) + len); 6378c2ecf20Sopenharmony_ci 6388c2ecf20Sopenharmony_ci mutex_lock(&vrp->tx_lock); 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci /* add message to the remote processor's virtqueue */ 6418c2ecf20Sopenharmony_ci err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL); 6428c2ecf20Sopenharmony_ci if (err) { 6438c2ecf20Sopenharmony_ci /* 6448c2ecf20Sopenharmony_ci * need to reclaim the buffer here, otherwise it's lost 6458c2ecf20Sopenharmony_ci * (memory won't leak, but rpmsg won't use it again for TX). 6468c2ecf20Sopenharmony_ci * this will wait for a buffer management overhaul. 6478c2ecf20Sopenharmony_ci */ 6488c2ecf20Sopenharmony_ci dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err); 6498c2ecf20Sopenharmony_ci goto out; 6508c2ecf20Sopenharmony_ci } 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_ci /* tell the remote processor it has a pending message to read */ 6538c2ecf20Sopenharmony_ci virtqueue_kick(vrp->svq); 6548c2ecf20Sopenharmony_ciout: 6558c2ecf20Sopenharmony_ci mutex_unlock(&vrp->tx_lock); 6568c2ecf20Sopenharmony_ci return err; 6578c2ecf20Sopenharmony_ci} 6588c2ecf20Sopenharmony_ci 6598c2ecf20Sopenharmony_cistatic int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len) 6608c2ecf20Sopenharmony_ci{ 6618c2ecf20Sopenharmony_ci struct rpmsg_device *rpdev = ept->rpdev; 6628c2ecf20Sopenharmony_ci u32 src = ept->addr, dst = rpdev->dst; 6638c2ecf20Sopenharmony_ci 6648c2ecf20Sopenharmony_ci return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true); 6658c2ecf20Sopenharmony_ci} 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_cistatic int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, 6688c2ecf20Sopenharmony_ci u32 dst) 6698c2ecf20Sopenharmony_ci{ 6708c2ecf20Sopenharmony_ci struct rpmsg_device *rpdev = ept->rpdev; 6718c2ecf20Sopenharmony_ci u32 src = ept->addr; 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_ci return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true); 6748c2ecf20Sopenharmony_ci} 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_cistatic int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, 6778c2ecf20Sopenharmony_ci u32 dst, void *data, int len) 6788c2ecf20Sopenharmony_ci{ 6798c2ecf20Sopenharmony_ci struct rpmsg_device *rpdev = ept->rpdev; 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true); 6828c2ecf20Sopenharmony_ci} 6838c2ecf20Sopenharmony_ci 6848c2ecf20Sopenharmony_cistatic int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len) 6858c2ecf20Sopenharmony_ci{ 6868c2ecf20Sopenharmony_ci struct rpmsg_device *rpdev = ept->rpdev; 6878c2ecf20Sopenharmony_ci u32 src = ept->addr, dst = rpdev->dst; 6888c2ecf20Sopenharmony_ci 6898c2ecf20Sopenharmony_ci return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false); 6908c2ecf20Sopenharmony_ci} 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_cistatic int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, 6938c2ecf20Sopenharmony_ci int len, u32 dst) 6948c2ecf20Sopenharmony_ci{ 6958c2ecf20Sopenharmony_ci struct rpmsg_device *rpdev = ept->rpdev; 6968c2ecf20Sopenharmony_ci u32 src = ept->addr; 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_ci return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false); 6998c2ecf20Sopenharmony_ci} 7008c2ecf20Sopenharmony_ci 7018c2ecf20Sopenharmony_cistatic int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, 7028c2ecf20Sopenharmony_ci u32 dst, void *data, int len) 7038c2ecf20Sopenharmony_ci{ 7048c2ecf20Sopenharmony_ci struct rpmsg_device *rpdev = ept->rpdev; 7058c2ecf20Sopenharmony_ci 7068c2ecf20Sopenharmony_ci return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false); 7078c2ecf20Sopenharmony_ci} 7088c2ecf20Sopenharmony_ci 7098c2ecf20Sopenharmony_cistatic int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev, 7108c2ecf20Sopenharmony_ci struct rpmsg_hdr *msg, unsigned int len) 7118c2ecf20Sopenharmony_ci{ 7128c2ecf20Sopenharmony_ci struct rpmsg_endpoint *ept; 7138c2ecf20Sopenharmony_ci struct scatterlist sg; 7148c2ecf20Sopenharmony_ci unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len); 7158c2ecf20Sopenharmony_ci int err; 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n", 7188c2ecf20Sopenharmony_ci virtio32_to_cpu(vrp->vdev, msg->src), 7198c2ecf20Sopenharmony_ci virtio32_to_cpu(vrp->vdev, msg->dst), msg_len, 7208c2ecf20Sopenharmony_ci virtio16_to_cpu(vrp->vdev, msg->flags), 7218c2ecf20Sopenharmony_ci virtio32_to_cpu(vrp->vdev, msg->reserved)); 7228c2ecf20Sopenharmony_ci#if defined(CONFIG_DYNAMIC_DEBUG) 7238c2ecf20Sopenharmony_ci dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1, 7248c2ecf20Sopenharmony_ci msg, sizeof(*msg) + msg_len, true); 7258c2ecf20Sopenharmony_ci#endif 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ci /* 7288c2ecf20Sopenharmony_ci * We currently use fixed-sized buffers, so trivially sanitize 7298c2ecf20Sopenharmony_ci * the reported payload length. 7308c2ecf20Sopenharmony_ci */ 7318c2ecf20Sopenharmony_ci if (len > vrp->buf_size || 7328c2ecf20Sopenharmony_ci msg_len > (len - sizeof(struct rpmsg_hdr))) { 7338c2ecf20Sopenharmony_ci dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len); 7348c2ecf20Sopenharmony_ci return -EINVAL; 7358c2ecf20Sopenharmony_ci } 7368c2ecf20Sopenharmony_ci 7378c2ecf20Sopenharmony_ci /* use the dst addr to fetch the callback of the appropriate user */ 7388c2ecf20Sopenharmony_ci mutex_lock(&vrp->endpoints_lock); 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_ci ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst)); 7418c2ecf20Sopenharmony_ci 7428c2ecf20Sopenharmony_ci /* let's make sure no one deallocates ept while we use it */ 7438c2ecf20Sopenharmony_ci if (ept) 7448c2ecf20Sopenharmony_ci kref_get(&ept->refcount); 7458c2ecf20Sopenharmony_ci 7468c2ecf20Sopenharmony_ci mutex_unlock(&vrp->endpoints_lock); 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci if (ept) { 7498c2ecf20Sopenharmony_ci /* make sure ept->cb doesn't go away while we use it */ 7508c2ecf20Sopenharmony_ci mutex_lock(&ept->cb_lock); 7518c2ecf20Sopenharmony_ci 7528c2ecf20Sopenharmony_ci if (ept->cb) 7538c2ecf20Sopenharmony_ci ept->cb(ept->rpdev, msg->data, msg_len, ept->priv, 7548c2ecf20Sopenharmony_ci virtio32_to_cpu(vrp->vdev, msg->src)); 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_ci mutex_unlock(&ept->cb_lock); 7578c2ecf20Sopenharmony_ci 7588c2ecf20Sopenharmony_ci /* farewell, ept, we don't need you anymore */ 7598c2ecf20Sopenharmony_ci kref_put(&ept->refcount, __ept_release); 7608c2ecf20Sopenharmony_ci } else 7618c2ecf20Sopenharmony_ci dev_warn(dev, "msg received with no recipient\n"); 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_ci /* publish the real size of the buffer */ 7648c2ecf20Sopenharmony_ci rpmsg_sg_init(&sg, msg, vrp->buf_size); 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci /* add the buffer back to the remote processor's virtqueue */ 7678c2ecf20Sopenharmony_ci err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL); 7688c2ecf20Sopenharmony_ci if (err < 0) { 7698c2ecf20Sopenharmony_ci dev_err(dev, "failed to add a virtqueue buffer: %d\n", err); 7708c2ecf20Sopenharmony_ci return err; 7718c2ecf20Sopenharmony_ci } 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_ci return 0; 7748c2ecf20Sopenharmony_ci} 7758c2ecf20Sopenharmony_ci 7768c2ecf20Sopenharmony_ci/* called when an rx buffer is used, and it's time to digest a message */ 7778c2ecf20Sopenharmony_cistatic void rpmsg_recv_done(struct virtqueue *rvq) 7788c2ecf20Sopenharmony_ci{ 7798c2ecf20Sopenharmony_ci struct virtproc_info *vrp = rvq->vdev->priv; 7808c2ecf20Sopenharmony_ci struct device *dev = &rvq->vdev->dev; 7818c2ecf20Sopenharmony_ci struct rpmsg_hdr *msg; 7828c2ecf20Sopenharmony_ci unsigned int len, msgs_received = 0; 7838c2ecf20Sopenharmony_ci int err; 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_ci msg = virtqueue_get_buf(rvq, &len); 7868c2ecf20Sopenharmony_ci if (!msg) { 7878c2ecf20Sopenharmony_ci dev_err(dev, "uhm, incoming signal, but no used buffer ?\n"); 7888c2ecf20Sopenharmony_ci return; 7898c2ecf20Sopenharmony_ci } 7908c2ecf20Sopenharmony_ci 7918c2ecf20Sopenharmony_ci while (msg) { 7928c2ecf20Sopenharmony_ci err = rpmsg_recv_single(vrp, dev, msg, len); 7938c2ecf20Sopenharmony_ci if (err) 7948c2ecf20Sopenharmony_ci break; 7958c2ecf20Sopenharmony_ci 7968c2ecf20Sopenharmony_ci msgs_received++; 7978c2ecf20Sopenharmony_ci 7988c2ecf20Sopenharmony_ci msg = virtqueue_get_buf(rvq, &len); 7998c2ecf20Sopenharmony_ci } 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_ci dev_dbg(dev, "Received %u messages\n", msgs_received); 8028c2ecf20Sopenharmony_ci 8038c2ecf20Sopenharmony_ci /* tell the remote processor we added another available rx buffer */ 8048c2ecf20Sopenharmony_ci if (msgs_received) 8058c2ecf20Sopenharmony_ci virtqueue_kick(vrp->rvq); 8068c2ecf20Sopenharmony_ci} 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_ci/* 8098c2ecf20Sopenharmony_ci * This is invoked whenever the remote processor completed processing 8108c2ecf20Sopenharmony_ci * a TX msg we just sent it, and the buffer is put back to the used ring. 8118c2ecf20Sopenharmony_ci * 8128c2ecf20Sopenharmony_ci * Normally, though, we suppress this "tx complete" interrupt in order to 8138c2ecf20Sopenharmony_ci * avoid the incurred overhead. 8148c2ecf20Sopenharmony_ci */ 8158c2ecf20Sopenharmony_cistatic void rpmsg_xmit_done(struct virtqueue *svq) 8168c2ecf20Sopenharmony_ci{ 8178c2ecf20Sopenharmony_ci struct virtproc_info *vrp = svq->vdev->priv; 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci dev_dbg(&svq->vdev->dev, "%s\n", __func__); 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci /* wake up potential senders that are waiting for a tx buffer */ 8228c2ecf20Sopenharmony_ci wake_up_interruptible(&vrp->sendq); 8238c2ecf20Sopenharmony_ci} 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_ci/* invoked when a name service announcement arrives */ 8268c2ecf20Sopenharmony_cistatic int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len, 8278c2ecf20Sopenharmony_ci void *priv, u32 src) 8288c2ecf20Sopenharmony_ci{ 8298c2ecf20Sopenharmony_ci struct rpmsg_ns_msg *msg = data; 8308c2ecf20Sopenharmony_ci struct rpmsg_device *newch; 8318c2ecf20Sopenharmony_ci struct rpmsg_channel_info chinfo; 8328c2ecf20Sopenharmony_ci struct virtproc_info *vrp = priv; 8338c2ecf20Sopenharmony_ci struct device *dev = &vrp->vdev->dev; 8348c2ecf20Sopenharmony_ci int ret; 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_ci#if defined(CONFIG_DYNAMIC_DEBUG) 8378c2ecf20Sopenharmony_ci dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1, 8388c2ecf20Sopenharmony_ci data, len, true); 8398c2ecf20Sopenharmony_ci#endif 8408c2ecf20Sopenharmony_ci 8418c2ecf20Sopenharmony_ci if (len != sizeof(*msg)) { 8428c2ecf20Sopenharmony_ci dev_err(dev, "malformed ns msg (%d)\n", len); 8438c2ecf20Sopenharmony_ci return -EINVAL; 8448c2ecf20Sopenharmony_ci } 8458c2ecf20Sopenharmony_ci 8468c2ecf20Sopenharmony_ci /* 8478c2ecf20Sopenharmony_ci * the name service ept does _not_ belong to a real rpmsg channel, 8488c2ecf20Sopenharmony_ci * and is handled by the rpmsg bus itself. 8498c2ecf20Sopenharmony_ci * for sanity reasons, make sure a valid rpdev has _not_ sneaked 8508c2ecf20Sopenharmony_ci * in somehow. 8518c2ecf20Sopenharmony_ci */ 8528c2ecf20Sopenharmony_ci if (rpdev) { 8538c2ecf20Sopenharmony_ci dev_err(dev, "anomaly: ns ept has an rpdev handle\n"); 8548c2ecf20Sopenharmony_ci return -EINVAL; 8558c2ecf20Sopenharmony_ci } 8568c2ecf20Sopenharmony_ci 8578c2ecf20Sopenharmony_ci /* don't trust the remote processor for null terminating the name */ 8588c2ecf20Sopenharmony_ci msg->name[RPMSG_NAME_SIZE - 1] = '\0'; 8598c2ecf20Sopenharmony_ci 8608c2ecf20Sopenharmony_ci strncpy(chinfo.name, msg->name, sizeof(chinfo.name)); 8618c2ecf20Sopenharmony_ci chinfo.src = RPMSG_ADDR_ANY; 8628c2ecf20Sopenharmony_ci chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr); 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_ci dev_info(dev, "%sing channel %s addr 0x%x\n", 8658c2ecf20Sopenharmony_ci virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ? 8668c2ecf20Sopenharmony_ci "destroy" : "creat", msg->name, chinfo.dst); 8678c2ecf20Sopenharmony_ci 8688c2ecf20Sopenharmony_ci if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) { 8698c2ecf20Sopenharmony_ci ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo); 8708c2ecf20Sopenharmony_ci if (ret) 8718c2ecf20Sopenharmony_ci dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret); 8728c2ecf20Sopenharmony_ci } else { 8738c2ecf20Sopenharmony_ci newch = rpmsg_create_channel(vrp, &chinfo); 8748c2ecf20Sopenharmony_ci if (!newch) 8758c2ecf20Sopenharmony_ci dev_err(dev, "rpmsg_create_channel failed\n"); 8768c2ecf20Sopenharmony_ci } 8778c2ecf20Sopenharmony_ci 8788c2ecf20Sopenharmony_ci return 0; 8798c2ecf20Sopenharmony_ci} 8808c2ecf20Sopenharmony_ci 8818c2ecf20Sopenharmony_cistatic int rpmsg_probe(struct virtio_device *vdev) 8828c2ecf20Sopenharmony_ci{ 8838c2ecf20Sopenharmony_ci vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done }; 8848c2ecf20Sopenharmony_ci static const char * const names[] = { "input", "output" }; 8858c2ecf20Sopenharmony_ci struct virtqueue *vqs[2]; 8868c2ecf20Sopenharmony_ci struct virtproc_info *vrp; 8878c2ecf20Sopenharmony_ci void *bufs_va; 8888c2ecf20Sopenharmony_ci int err = 0, i; 8898c2ecf20Sopenharmony_ci size_t total_buf_space; 8908c2ecf20Sopenharmony_ci bool notify; 8918c2ecf20Sopenharmony_ci 8928c2ecf20Sopenharmony_ci vrp = kzalloc(sizeof(*vrp), GFP_KERNEL); 8938c2ecf20Sopenharmony_ci if (!vrp) 8948c2ecf20Sopenharmony_ci return -ENOMEM; 8958c2ecf20Sopenharmony_ci 8968c2ecf20Sopenharmony_ci vrp->vdev = vdev; 8978c2ecf20Sopenharmony_ci 8988c2ecf20Sopenharmony_ci idr_init(&vrp->endpoints); 8998c2ecf20Sopenharmony_ci mutex_init(&vrp->endpoints_lock); 9008c2ecf20Sopenharmony_ci mutex_init(&vrp->tx_lock); 9018c2ecf20Sopenharmony_ci init_waitqueue_head(&vrp->sendq); 9028c2ecf20Sopenharmony_ci 9038c2ecf20Sopenharmony_ci /* We expect two virtqueues, rx and tx (and in this order) */ 9048c2ecf20Sopenharmony_ci err = virtio_find_vqs(vdev, 2, vqs, vq_cbs, names, NULL); 9058c2ecf20Sopenharmony_ci if (err) 9068c2ecf20Sopenharmony_ci goto free_vrp; 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_ci vrp->rvq = vqs[0]; 9098c2ecf20Sopenharmony_ci vrp->svq = vqs[1]; 9108c2ecf20Sopenharmony_ci 9118c2ecf20Sopenharmony_ci /* we expect symmetric tx/rx vrings */ 9128c2ecf20Sopenharmony_ci WARN_ON(virtqueue_get_vring_size(vrp->rvq) != 9138c2ecf20Sopenharmony_ci virtqueue_get_vring_size(vrp->svq)); 9148c2ecf20Sopenharmony_ci 9158c2ecf20Sopenharmony_ci /* we need less buffers if vrings are small */ 9168c2ecf20Sopenharmony_ci if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2) 9178c2ecf20Sopenharmony_ci vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2; 9188c2ecf20Sopenharmony_ci else 9198c2ecf20Sopenharmony_ci vrp->num_bufs = MAX_RPMSG_NUM_BUFS; 9208c2ecf20Sopenharmony_ci 9218c2ecf20Sopenharmony_ci vrp->buf_size = MAX_RPMSG_BUF_SIZE; 9228c2ecf20Sopenharmony_ci 9238c2ecf20Sopenharmony_ci total_buf_space = vrp->num_bufs * vrp->buf_size; 9248c2ecf20Sopenharmony_ci 9258c2ecf20Sopenharmony_ci /* allocate coherent memory for the buffers */ 9268c2ecf20Sopenharmony_ci bufs_va = dma_alloc_coherent(vdev->dev.parent, 9278c2ecf20Sopenharmony_ci total_buf_space, &vrp->bufs_dma, 9288c2ecf20Sopenharmony_ci GFP_KERNEL); 9298c2ecf20Sopenharmony_ci if (!bufs_va) { 9308c2ecf20Sopenharmony_ci err = -ENOMEM; 9318c2ecf20Sopenharmony_ci goto vqs_del; 9328c2ecf20Sopenharmony_ci } 9338c2ecf20Sopenharmony_ci 9348c2ecf20Sopenharmony_ci dev_dbg(&vdev->dev, "buffers: va %pK, dma %pad\n", 9358c2ecf20Sopenharmony_ci bufs_va, &vrp->bufs_dma); 9368c2ecf20Sopenharmony_ci 9378c2ecf20Sopenharmony_ci /* half of the buffers is dedicated for RX */ 9388c2ecf20Sopenharmony_ci vrp->rbufs = bufs_va; 9398c2ecf20Sopenharmony_ci 9408c2ecf20Sopenharmony_ci /* and half is dedicated for TX */ 9418c2ecf20Sopenharmony_ci vrp->sbufs = bufs_va + total_buf_space / 2; 9428c2ecf20Sopenharmony_ci 9438c2ecf20Sopenharmony_ci /* set up the receive buffers */ 9448c2ecf20Sopenharmony_ci for (i = 0; i < vrp->num_bufs / 2; i++) { 9458c2ecf20Sopenharmony_ci struct scatterlist sg; 9468c2ecf20Sopenharmony_ci void *cpu_addr = vrp->rbufs + i * vrp->buf_size; 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci rpmsg_sg_init(&sg, cpu_addr, vrp->buf_size); 9498c2ecf20Sopenharmony_ci 9508c2ecf20Sopenharmony_ci err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr, 9518c2ecf20Sopenharmony_ci GFP_KERNEL); 9528c2ecf20Sopenharmony_ci WARN_ON(err); /* sanity check; this can't really happen */ 9538c2ecf20Sopenharmony_ci } 9548c2ecf20Sopenharmony_ci 9558c2ecf20Sopenharmony_ci /* suppress "tx-complete" interrupts */ 9568c2ecf20Sopenharmony_ci virtqueue_disable_cb(vrp->svq); 9578c2ecf20Sopenharmony_ci 9588c2ecf20Sopenharmony_ci vdev->priv = vrp; 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci /* if supported by the remote processor, enable the name service */ 9618c2ecf20Sopenharmony_ci if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) { 9628c2ecf20Sopenharmony_ci /* a dedicated endpoint handles the name service msgs */ 9638c2ecf20Sopenharmony_ci vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb, 9648c2ecf20Sopenharmony_ci vrp, RPMSG_NS_ADDR); 9658c2ecf20Sopenharmony_ci if (!vrp->ns_ept) { 9668c2ecf20Sopenharmony_ci dev_err(&vdev->dev, "failed to create the ns ept\n"); 9678c2ecf20Sopenharmony_ci err = -ENOMEM; 9688c2ecf20Sopenharmony_ci goto free_coherent; 9698c2ecf20Sopenharmony_ci } 9708c2ecf20Sopenharmony_ci } 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_ci /* 9738c2ecf20Sopenharmony_ci * Prepare to kick but don't notify yet - we can't do this before 9748c2ecf20Sopenharmony_ci * device is ready. 9758c2ecf20Sopenharmony_ci */ 9768c2ecf20Sopenharmony_ci notify = virtqueue_kick_prepare(vrp->rvq); 9778c2ecf20Sopenharmony_ci 9788c2ecf20Sopenharmony_ci /* From this point on, we can notify and get callbacks. */ 9798c2ecf20Sopenharmony_ci virtio_device_ready(vdev); 9808c2ecf20Sopenharmony_ci 9818c2ecf20Sopenharmony_ci /* tell the remote processor it can start sending messages */ 9828c2ecf20Sopenharmony_ci /* 9838c2ecf20Sopenharmony_ci * this might be concurrent with callbacks, but we are only 9848c2ecf20Sopenharmony_ci * doing notify, not a full kick here, so that's ok. 9858c2ecf20Sopenharmony_ci */ 9868c2ecf20Sopenharmony_ci if (notify) 9878c2ecf20Sopenharmony_ci virtqueue_notify(vrp->rvq); 9888c2ecf20Sopenharmony_ci 9898c2ecf20Sopenharmony_ci dev_info(&vdev->dev, "rpmsg host is online\n"); 9908c2ecf20Sopenharmony_ci 9918c2ecf20Sopenharmony_ci return 0; 9928c2ecf20Sopenharmony_ci 9938c2ecf20Sopenharmony_cifree_coherent: 9948c2ecf20Sopenharmony_ci dma_free_coherent(vdev->dev.parent, total_buf_space, 9958c2ecf20Sopenharmony_ci bufs_va, vrp->bufs_dma); 9968c2ecf20Sopenharmony_civqs_del: 9978c2ecf20Sopenharmony_ci vdev->config->del_vqs(vrp->vdev); 9988c2ecf20Sopenharmony_cifree_vrp: 9998c2ecf20Sopenharmony_ci kfree(vrp); 10008c2ecf20Sopenharmony_ci return err; 10018c2ecf20Sopenharmony_ci} 10028c2ecf20Sopenharmony_ci 10038c2ecf20Sopenharmony_cistatic int rpmsg_remove_device(struct device *dev, void *data) 10048c2ecf20Sopenharmony_ci{ 10058c2ecf20Sopenharmony_ci device_unregister(dev); 10068c2ecf20Sopenharmony_ci 10078c2ecf20Sopenharmony_ci return 0; 10088c2ecf20Sopenharmony_ci} 10098c2ecf20Sopenharmony_ci 10108c2ecf20Sopenharmony_cistatic void rpmsg_remove(struct virtio_device *vdev) 10118c2ecf20Sopenharmony_ci{ 10128c2ecf20Sopenharmony_ci struct virtproc_info *vrp = vdev->priv; 10138c2ecf20Sopenharmony_ci size_t total_buf_space = vrp->num_bufs * vrp->buf_size; 10148c2ecf20Sopenharmony_ci int ret; 10158c2ecf20Sopenharmony_ci 10168c2ecf20Sopenharmony_ci vdev->config->reset(vdev); 10178c2ecf20Sopenharmony_ci 10188c2ecf20Sopenharmony_ci ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device); 10198c2ecf20Sopenharmony_ci if (ret) 10208c2ecf20Sopenharmony_ci dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret); 10218c2ecf20Sopenharmony_ci 10228c2ecf20Sopenharmony_ci if (vrp->ns_ept) 10238c2ecf20Sopenharmony_ci __rpmsg_destroy_ept(vrp, vrp->ns_ept); 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_ci idr_destroy(&vrp->endpoints); 10268c2ecf20Sopenharmony_ci 10278c2ecf20Sopenharmony_ci vdev->config->del_vqs(vrp->vdev); 10288c2ecf20Sopenharmony_ci 10298c2ecf20Sopenharmony_ci dma_free_coherent(vdev->dev.parent, total_buf_space, 10308c2ecf20Sopenharmony_ci vrp->rbufs, vrp->bufs_dma); 10318c2ecf20Sopenharmony_ci 10328c2ecf20Sopenharmony_ci kfree(vrp); 10338c2ecf20Sopenharmony_ci} 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_cistatic struct virtio_device_id id_table[] = { 10368c2ecf20Sopenharmony_ci { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID }, 10378c2ecf20Sopenharmony_ci { 0 }, 10388c2ecf20Sopenharmony_ci}; 10398c2ecf20Sopenharmony_ci 10408c2ecf20Sopenharmony_cistatic unsigned int features[] = { 10418c2ecf20Sopenharmony_ci VIRTIO_RPMSG_F_NS, 10428c2ecf20Sopenharmony_ci}; 10438c2ecf20Sopenharmony_ci 10448c2ecf20Sopenharmony_cistatic struct virtio_driver virtio_ipc_driver = { 10458c2ecf20Sopenharmony_ci .feature_table = features, 10468c2ecf20Sopenharmony_ci .feature_table_size = ARRAY_SIZE(features), 10478c2ecf20Sopenharmony_ci .driver.name = KBUILD_MODNAME, 10488c2ecf20Sopenharmony_ci .driver.owner = THIS_MODULE, 10498c2ecf20Sopenharmony_ci .id_table = id_table, 10508c2ecf20Sopenharmony_ci .probe = rpmsg_probe, 10518c2ecf20Sopenharmony_ci .remove = rpmsg_remove, 10528c2ecf20Sopenharmony_ci}; 10538c2ecf20Sopenharmony_ci 10548c2ecf20Sopenharmony_cistatic int __init rpmsg_init(void) 10558c2ecf20Sopenharmony_ci{ 10568c2ecf20Sopenharmony_ci int ret; 10578c2ecf20Sopenharmony_ci 10588c2ecf20Sopenharmony_ci ret = register_virtio_driver(&virtio_ipc_driver); 10598c2ecf20Sopenharmony_ci if (ret) 10608c2ecf20Sopenharmony_ci pr_err("failed to register virtio driver: %d\n", ret); 10618c2ecf20Sopenharmony_ci 10628c2ecf20Sopenharmony_ci return ret; 10638c2ecf20Sopenharmony_ci} 10648c2ecf20Sopenharmony_cisubsys_initcall(rpmsg_init); 10658c2ecf20Sopenharmony_ci 10668c2ecf20Sopenharmony_cistatic void __exit rpmsg_fini(void) 10678c2ecf20Sopenharmony_ci{ 10688c2ecf20Sopenharmony_ci unregister_virtio_driver(&virtio_ipc_driver); 10698c2ecf20Sopenharmony_ci} 10708c2ecf20Sopenharmony_cimodule_exit(rpmsg_fini); 10718c2ecf20Sopenharmony_ci 10728c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(virtio, id_table); 10738c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Virtio-based remote processor messaging bus"); 10748c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 1075