1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015, Sony Mobile Communications AB.
4 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
7 #include <linux/interrupt.h>
9 #include <linux/mailbox_client.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
12 #include <linux/of_irq.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/soc/qcom/smem.h>
19 #include <linux/wait.h>
20 #include <linux/rpmsg.h>
21 #include <linux/rpmsg/qcom_smd.h>
23 #include "rpmsg_internal.h"
26 * The Qualcomm Shared Memory communication solution provides point-to-point
27 * channels for clients to send and receive streaming or packet based data.
29 * Each channel consists of a control item (channel info) and a ring buffer
30 * pair. The channel info carry information related to channel state, flow
31 * control and the offsets within the ring buffer.
33 * All allocated channels are listed in an allocation table, identifying the
34 * pair of items by name, type and remote processor.
36 * Upon creating a new channel the remote processor allocates channel info and
37 * ring buffer items from the smem heap and populate the allocation table. An
38 * interrupt is sent to the other end of the channel and a scan for new
39 * channels should be done. A channel never goes away, it will only change
42 * The remote processor signals it intent for bring up the communication
43 * channel by setting the state of its end of the channel to "opening" and
44 * sends out an interrupt. We detect this change and register a smd device to
45 * consume the channel. Upon finding a consumer we finish the handshake and the
48 * Upon closing a channel, the remote processor will update the state of its
49 * end of the channel and signal us, we will then unregister any attached
50 * device and close our end of the channel.
52 * Devices attached to a channel can use the qcom_smd_send function to push
53 * data to the channel, this is done by copying the data into the tx ring
54 * buffer, updating the pointers in the channel info and signaling the remote
57 * The remote processor does the equivalent when it transfer data and upon
58 * receiving the interrupt we check the channel info for new data and delivers
59 * this to the attached device. If the device is not ready to receive the data
60 * we leave it in the ring buffer for now.
63 struct smd_channel_info;
64 struct smd_channel_info_pair;
65 struct smd_channel_info_word;
66 struct smd_channel_info_word_pair;
68 static const struct rpmsg_endpoint_ops qcom_smd_endpoint_ops;
70 #define SMD_ALLOC_TBL_COUNT 2
71 #define SMD_ALLOC_TBL_SIZE 64
74 * This lists the various smem heap items relevant for the allocation table and
75 * smd channel entries.
78 unsigned alloc_tbl_id;
79 unsigned info_base_id;
80 unsigned fifo_base_id;
81 } smem_items[SMD_ALLOC_TBL_COUNT] = {
95 * struct qcom_smd_edge - representing a remote processor
96 * @of_node: of_node handle for information related to this edge
97 * @edge_id: identifier of this edge
98 * @remote_pid: identifier of remote processor
99 * @irq: interrupt for signals on this edge
100 * @ipc_regmap: regmap handle holding the outgoing ipc register
101 * @ipc_offset: offset within @ipc_regmap of the register for ipc
102 * @ipc_bit: bit in the register at @ipc_offset of @ipc_regmap
103 * @mbox_client: mailbox client handle
104 * @mbox_chan: apcs ipc mailbox channel handle
105 * @channels: list of all channels detected on this edge
106 * @channels_lock: guard for modifications of @channels
107 * @allocated: array of bitmaps representing already allocated channels
108 * @smem_available: last available amount of smem triggering a channel scan
109 * @scan_work: work item for discovering new channels
110 * @state_work: work item for edge state changes
112 struct qcom_smd_edge {
117 struct device_node *of_node;
123 struct regmap *ipc_regmap;
127 struct mbox_client mbox_client;
128 struct mbox_chan *mbox_chan;
130 struct list_head channels;
131 spinlock_t channels_lock;
133 DECLARE_BITMAP(allocated[SMD_ALLOC_TBL_COUNT], SMD_ALLOC_TBL_SIZE);
135 unsigned smem_available;
137 wait_queue_head_t new_channel_event;
139 struct work_struct scan_work;
140 struct work_struct state_work;
144 * SMD channel states.
146 enum smd_channel_state {
150 SMD_CHANNEL_FLUSHING,
153 SMD_CHANNEL_RESET_OPENING
156 struct qcom_smd_device {
157 struct rpmsg_device rpdev;
159 struct qcom_smd_edge *edge;
162 struct qcom_smd_endpoint {
163 struct rpmsg_endpoint ept;
165 struct qcom_smd_channel *qsch;
168 #define to_smd_device(r) container_of(r, struct qcom_smd_device, rpdev)
169 #define to_smd_edge(d) container_of(d, struct qcom_smd_edge, dev)
170 #define to_smd_endpoint(e) container_of(e, struct qcom_smd_endpoint, ept)
173 * struct qcom_smd_channel - smd channel struct
174 * @edge: qcom_smd_edge this channel is living on
175 * @qsdev: reference to a associated smd client device
176 * @name: name of the channel
177 * @state: local state of the channel
178 * @remote_state: remote state of the channel
179 * @info: byte aligned outgoing/incoming channel info
180 * @info_word: word aligned outgoing/incoming channel info
181 * @tx_lock: lock to make writes to the channel mutually exclusive
182 * @fblockread_event: wakeup event tied to tx fBLOCKREADINTR
183 * @tx_fifo: pointer to the outgoing ring buffer
184 * @rx_fifo: pointer to the incoming ring buffer
185 * @fifo_size: size of each ring buffer
186 * @bounce_buffer: bounce buffer for reading wrapped packets
187 * @cb: callback function registered for this channel
188 * @recv_lock: guard for rx info modifications and cb pointer
189 * @pkt_size: size of the currently handled packet
190 * @list: lite entry for @channels in qcom_smd_edge
192 struct qcom_smd_channel {
193 struct qcom_smd_edge *edge;
195 struct qcom_smd_endpoint *qsept;
199 enum smd_channel_state state;
200 enum smd_channel_state remote_state;
201 wait_queue_head_t state_change_event;
203 struct smd_channel_info_pair *info;
204 struct smd_channel_info_word_pair *info_word;
207 wait_queue_head_t fblockread_event;
215 spinlock_t recv_lock;
221 struct list_head list;
225 * Format of the smd_info smem items, for byte aligned channels.
227 struct smd_channel_info {
241 struct smd_channel_info_pair {
242 struct smd_channel_info tx;
243 struct smd_channel_info rx;
247 * Format of the smd_info smem items, for word aligned channels.
249 struct smd_channel_info_word {
258 __le32 fBLOCKREADINTR;
263 struct smd_channel_info_word_pair {
264 struct smd_channel_info_word tx;
265 struct smd_channel_info_word rx;
268 #define GET_RX_CHANNEL_FLAG(channel, param) \
270 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
271 channel->info_word ? \
272 le32_to_cpu(channel->info_word->rx.param) : \
273 channel->info->rx.param; \
276 #define GET_RX_CHANNEL_INFO(channel, param) \
278 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
279 le32_to_cpu(channel->info_word ? \
280 channel->info_word->rx.param : \
281 channel->info->rx.param); \
284 #define SET_RX_CHANNEL_FLAG(channel, param, value) \
286 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
287 if (channel->info_word) \
288 channel->info_word->rx.param = cpu_to_le32(value); \
290 channel->info->rx.param = value; \
293 #define SET_RX_CHANNEL_INFO(channel, param, value) \
295 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
296 if (channel->info_word) \
297 channel->info_word->rx.param = cpu_to_le32(value); \
299 channel->info->rx.param = cpu_to_le32(value); \
302 #define GET_TX_CHANNEL_FLAG(channel, param) \
304 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
305 channel->info_word ? \
306 le32_to_cpu(channel->info_word->tx.param) : \
307 channel->info->tx.param; \
310 #define GET_TX_CHANNEL_INFO(channel, param) \
312 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
313 le32_to_cpu(channel->info_word ? \
314 channel->info_word->tx.param : \
315 channel->info->tx.param); \
318 #define SET_TX_CHANNEL_FLAG(channel, param, value) \
320 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
321 if (channel->info_word) \
322 channel->info_word->tx.param = cpu_to_le32(value); \
324 channel->info->tx.param = value; \
327 #define SET_TX_CHANNEL_INFO(channel, param, value) \
329 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
330 if (channel->info_word) \
331 channel->info_word->tx.param = cpu_to_le32(value); \
333 channel->info->tx.param = cpu_to_le32(value); \
337 * struct qcom_smd_alloc_entry - channel allocation entry
338 * @name: channel name
339 * @cid: channel index
340 * @flags: channel flags and edge id
341 * @ref_count: reference count of the channel
343 struct qcom_smd_alloc_entry {
350 #define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff
351 #define SMD_CHANNEL_FLAGS_STREAM BIT(8)
352 #define SMD_CHANNEL_FLAGS_PACKET BIT(9)
355 * Each smd packet contains a 20 byte header, with the first 4 being the length
358 #define SMD_PACKET_HEADER_LEN 20
361 * Signal the remote processor associated with 'channel'.
363 static void qcom_smd_signal_channel(struct qcom_smd_channel *channel)
365 struct qcom_smd_edge *edge = channel->edge;
367 if (edge->mbox_chan) {
369 * We can ignore a failing mbox_send_message() as the only
370 * possible cause is that the FIFO in the framework is full of
371 * other writes to the same bit.
373 mbox_send_message(edge->mbox_chan, NULL);
374 mbox_client_txdone(edge->mbox_chan, 0);
376 regmap_write(edge->ipc_regmap, edge->ipc_offset, BIT(edge->ipc_bit));
381 * Initialize the tx channel info
383 static void qcom_smd_channel_reset(struct qcom_smd_channel *channel)
385 SET_TX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
386 SET_TX_CHANNEL_FLAG(channel, fDSR, 0);
387 SET_TX_CHANNEL_FLAG(channel, fCTS, 0);
388 SET_TX_CHANNEL_FLAG(channel, fCD, 0);
389 SET_TX_CHANNEL_FLAG(channel, fRI, 0);
390 SET_TX_CHANNEL_FLAG(channel, fHEAD, 0);
391 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
392 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
393 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
394 SET_TX_CHANNEL_INFO(channel, head, 0);
395 SET_RX_CHANNEL_INFO(channel, tail, 0);
397 qcom_smd_signal_channel(channel);
399 channel->state = SMD_CHANNEL_CLOSED;
400 channel->pkt_size = 0;
404 * Set the callback for a channel, with appropriate locking
406 static void qcom_smd_channel_set_callback(struct qcom_smd_channel *channel,
409 struct rpmsg_endpoint *ept = &channel->qsept->ept;
412 spin_lock_irqsave(&channel->recv_lock, flags);
414 spin_unlock_irqrestore(&channel->recv_lock, flags);
418 * Calculate the amount of data available in the rx fifo
420 static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel *channel)
425 head = GET_RX_CHANNEL_INFO(channel, head);
426 tail = GET_RX_CHANNEL_INFO(channel, tail);
428 return (head - tail) & (channel->fifo_size - 1);
432 * Set tx channel state and inform the remote processor
434 static void qcom_smd_channel_set_state(struct qcom_smd_channel *channel,
437 struct qcom_smd_edge *edge = channel->edge;
438 bool is_open = state == SMD_CHANNEL_OPENED;
440 if (channel->state == state)
443 dev_dbg(&edge->dev, "set_state(%s, %d)\n", channel->name, state);
445 SET_TX_CHANNEL_FLAG(channel, fDSR, is_open);
446 SET_TX_CHANNEL_FLAG(channel, fCTS, is_open);
447 SET_TX_CHANNEL_FLAG(channel, fCD, is_open);
449 SET_TX_CHANNEL_INFO(channel, state, state);
450 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
452 channel->state = state;
453 qcom_smd_signal_channel(channel);
457 * Copy count bytes of data using 32bit accesses, if that's required.
459 static void smd_copy_to_fifo(void __iomem *dst,
465 __iowrite32_copy(dst, src, count / sizeof(u32));
467 memcpy_toio(dst, src, count);
472 * Copy count bytes of data using 32bit accesses, if that is required.
474 static void smd_copy_from_fifo(void *dst,
475 const void __iomem *src,
480 __ioread32_copy(dst, src, count / sizeof(u32));
482 memcpy_fromio(dst, src, count);
487 * Read count bytes of data from the rx fifo into buf, but don't advance the
490 static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel,
491 void *buf, size_t count)
497 word_aligned = channel->info_word;
498 tail = GET_RX_CHANNEL_INFO(channel, tail);
500 len = min_t(size_t, count, channel->fifo_size - tail);
502 smd_copy_from_fifo(buf,
503 channel->rx_fifo + tail,
509 smd_copy_from_fifo(buf + len,
519 * Advance the rx tail by count bytes.
521 static void qcom_smd_channel_advance(struct qcom_smd_channel *channel,
526 tail = GET_RX_CHANNEL_INFO(channel, tail);
528 tail &= (channel->fifo_size - 1);
529 SET_RX_CHANNEL_INFO(channel, tail, tail);
533 * Read out a single packet from the rx fifo and deliver it to the device
535 static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
537 struct rpmsg_endpoint *ept = &channel->qsept->ept;
543 tail = GET_RX_CHANNEL_INFO(channel, tail);
545 /* Use bounce buffer if the data wraps */
546 if (tail + channel->pkt_size >= channel->fifo_size) {
547 ptr = channel->bounce_buffer;
548 len = qcom_smd_channel_peek(channel, ptr, channel->pkt_size);
550 ptr = channel->rx_fifo + tail;
551 len = channel->pkt_size;
554 ret = ept->cb(ept->rpdev, ptr, len, ept->priv, RPMSG_ADDR_ANY);
558 /* Only forward the tail if the client consumed the data */
559 qcom_smd_channel_advance(channel, len);
561 channel->pkt_size = 0;
567 * Per channel interrupt handling
569 static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel)
571 bool need_state_scan = false;
577 /* Handle state changes */
578 remote_state = GET_RX_CHANNEL_INFO(channel, state);
579 if (remote_state != channel->remote_state) {
580 channel->remote_state = remote_state;
581 need_state_scan = true;
583 wake_up_interruptible_all(&channel->state_change_event);
585 /* Indicate that we have seen any state change */
586 SET_RX_CHANNEL_FLAG(channel, fSTATE, 0);
588 /* Signal waiting qcom_smd_send() about the interrupt */
589 if (!GET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR))
590 wake_up_interruptible_all(&channel->fblockread_event);
592 /* Don't consume any data until we've opened the channel */
593 if (channel->state != SMD_CHANNEL_OPENED)
596 /* Indicate that we've seen the new data */
597 SET_RX_CHANNEL_FLAG(channel, fHEAD, 0);
601 avail = qcom_smd_channel_get_rx_avail(channel);
603 if (!channel->pkt_size && avail >= SMD_PACKET_HEADER_LEN) {
604 qcom_smd_channel_peek(channel, &pktlen, sizeof(pktlen));
605 qcom_smd_channel_advance(channel, SMD_PACKET_HEADER_LEN);
606 channel->pkt_size = le32_to_cpu(pktlen);
607 } else if (channel->pkt_size && avail >= channel->pkt_size) {
608 ret = qcom_smd_channel_recv_single(channel);
616 /* Indicate that we have seen and updated tail */
617 SET_RX_CHANNEL_FLAG(channel, fTAIL, 1);
619 /* Signal the remote that we've consumed the data (if requested) */
620 if (!GET_RX_CHANNEL_FLAG(channel, fBLOCKREADINTR)) {
621 /* Ensure ordering of channel info updates */
624 qcom_smd_signal_channel(channel);
628 return need_state_scan;
632 * The edge interrupts are triggered by the remote processor on state changes,
633 * channel info updates or when new channels are created.
635 static irqreturn_t qcom_smd_edge_intr(int irq, void *data)
637 struct qcom_smd_edge *edge = data;
638 struct qcom_smd_channel *channel;
640 bool kick_scanner = false;
641 bool kick_state = false;
644 * Handle state changes or data on each of the channels on this edge
646 spin_lock(&edge->channels_lock);
647 list_for_each_entry(channel, &edge->channels, list) {
648 spin_lock(&channel->recv_lock);
649 kick_state |= qcom_smd_channel_intr(channel);
650 spin_unlock(&channel->recv_lock);
652 spin_unlock(&edge->channels_lock);
655 * Creating a new channel requires allocating an smem entry, so we only
656 * have to scan if the amount of available space in smem have changed
659 available = qcom_smem_get_free_space(edge->remote_pid);
660 if (available != edge->smem_available) {
661 edge->smem_available = available;
666 schedule_work(&edge->scan_work);
668 schedule_work(&edge->state_work);
674 * Calculate how much space is available in the tx fifo.
676 static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel)
680 unsigned mask = channel->fifo_size - 1;
682 head = GET_TX_CHANNEL_INFO(channel, head);
683 tail = GET_TX_CHANNEL_INFO(channel, tail);
685 return mask - ((head - tail) & mask);
689 * Write count bytes of data into channel, possibly wrapping in the ring buffer
691 static int qcom_smd_write_fifo(struct qcom_smd_channel *channel,
699 word_aligned = channel->info_word;
700 head = GET_TX_CHANNEL_INFO(channel, head);
702 len = min_t(size_t, count, channel->fifo_size - head);
704 smd_copy_to_fifo(channel->tx_fifo + head,
711 smd_copy_to_fifo(channel->tx_fifo,
718 head &= (channel->fifo_size - 1);
719 SET_TX_CHANNEL_INFO(channel, head, head);
725 * qcom_smd_send - write data to smd channel
726 * @channel: channel handle
727 * @data: buffer of data to write
728 * @len: number of bytes to write
730 * This is a blocking write of len bytes into the channel's tx ring buffer and
731 * signal the remote end. It will sleep until there is enough space available
732 * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
735 static int __qcom_smd_send(struct qcom_smd_channel *channel, const void *data,
738 __le32 hdr[5] = { cpu_to_le32(len), };
739 int tlen = sizeof(hdr) + len;
743 /* Word aligned channels only accept word size aligned data */
744 if (channel->info_word && len % 4)
747 /* Reject packets that are too big */
748 if (tlen >= channel->fifo_size)
751 /* Highlight the fact that if we enter the loop below we might sleep */
755 spin_lock_irqsave(&channel->tx_lock, flags);
757 while (qcom_smd_get_tx_avail(channel) < tlen &&
758 channel->state == SMD_CHANNEL_OPENED) {
764 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 0);
766 /* Wait without holding the tx_lock */
767 spin_unlock_irqrestore(&channel->tx_lock, flags);
769 ret = wait_event_interruptible(channel->fblockread_event,
770 qcom_smd_get_tx_avail(channel) >= tlen ||
771 channel->state != SMD_CHANNEL_OPENED);
775 spin_lock_irqsave(&channel->tx_lock, flags);
777 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
780 /* Fail if the channel was closed */
781 if (channel->state != SMD_CHANNEL_OPENED) {
786 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
788 qcom_smd_write_fifo(channel, hdr, sizeof(hdr));
789 qcom_smd_write_fifo(channel, data, len);
791 SET_TX_CHANNEL_FLAG(channel, fHEAD, 1);
793 /* Ensure ordering of channel info updates */
796 qcom_smd_signal_channel(channel);
799 spin_unlock_irqrestore(&channel->tx_lock, flags);
805 * Helper for opening a channel
807 static int qcom_smd_channel_open(struct qcom_smd_channel *channel,
810 struct qcom_smd_edge *edge = channel->edge;
815 * Packets are maximum 4k, but reduce if the fifo is smaller
817 bb_size = min(channel->fifo_size, SZ_4K);
818 channel->bounce_buffer = kmalloc(bb_size, GFP_KERNEL);
819 if (!channel->bounce_buffer)
822 qcom_smd_channel_set_callback(channel, cb);
823 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENING);
825 /* Wait for remote to enter opening or opened */
826 ret = wait_event_interruptible_timeout(channel->state_change_event,
827 channel->remote_state == SMD_CHANNEL_OPENING ||
828 channel->remote_state == SMD_CHANNEL_OPENED,
831 dev_err(&edge->dev, "remote side did not enter opening state\n");
832 goto out_close_timeout;
835 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED);
837 /* Wait for remote to enter opened */
838 ret = wait_event_interruptible_timeout(channel->state_change_event,
839 channel->remote_state == SMD_CHANNEL_OPENED,
842 dev_err(&edge->dev, "remote side did not enter open state\n");
843 goto out_close_timeout;
849 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
854 * Helper for closing and resetting a channel
856 static void qcom_smd_channel_close(struct qcom_smd_channel *channel)
858 qcom_smd_channel_set_callback(channel, NULL);
860 kfree(channel->bounce_buffer);
861 channel->bounce_buffer = NULL;
863 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
864 qcom_smd_channel_reset(channel);
867 static struct qcom_smd_channel *
868 qcom_smd_find_channel(struct qcom_smd_edge *edge, const char *name)
870 struct qcom_smd_channel *channel;
871 struct qcom_smd_channel *ret = NULL;
874 spin_lock_irqsave(&edge->channels_lock, flags);
875 list_for_each_entry(channel, &edge->channels, list) {
876 if (!strcmp(channel->name, name)) {
881 spin_unlock_irqrestore(&edge->channels_lock, flags);
886 static void __ept_release(struct kref *kref)
888 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
890 kfree(to_smd_endpoint(ept));
893 static struct rpmsg_endpoint *qcom_smd_create_ept(struct rpmsg_device *rpdev,
894 rpmsg_rx_cb_t cb, void *priv,
895 struct rpmsg_channel_info chinfo)
897 struct qcom_smd_endpoint *qsept;
898 struct qcom_smd_channel *channel;
899 struct qcom_smd_device *qsdev = to_smd_device(rpdev);
900 struct qcom_smd_edge *edge = qsdev->edge;
901 struct rpmsg_endpoint *ept;
902 const char *name = chinfo.name;
905 /* Wait up to HZ for the channel to appear */
906 ret = wait_event_interruptible_timeout(edge->new_channel_event,
907 (channel = qcom_smd_find_channel(edge, name)) != NULL,
912 if (channel->state != SMD_CHANNEL_CLOSED) {
913 dev_err(&rpdev->dev, "channel %s is busy\n", channel->name);
917 qsept = kzalloc(sizeof(*qsept), GFP_KERNEL);
923 kref_init(&ept->refcount);
928 ept->ops = &qcom_smd_endpoint_ops;
930 channel->qsept = qsept;
931 qsept->qsch = channel;
933 ret = qcom_smd_channel_open(channel, cb);
940 channel->qsept = NULL;
941 kref_put(&ept->refcount, __ept_release);
945 static void qcom_smd_destroy_ept(struct rpmsg_endpoint *ept)
947 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
948 struct qcom_smd_channel *ch = qsept->qsch;
950 qcom_smd_channel_close(ch);
952 kref_put(&ept->refcount, __ept_release);
955 static int qcom_smd_send(struct rpmsg_endpoint *ept, void *data, int len)
957 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
959 return __qcom_smd_send(qsept->qsch, data, len, true);
962 static int qcom_smd_trysend(struct rpmsg_endpoint *ept, void *data, int len)
964 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
966 return __qcom_smd_send(qsept->qsch, data, len, false);
969 static __poll_t qcom_smd_poll(struct rpmsg_endpoint *ept,
970 struct file *filp, poll_table *wait)
972 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
973 struct qcom_smd_channel *channel = qsept->qsch;
976 poll_wait(filp, &channel->fblockread_event, wait);
978 if (qcom_smd_get_tx_avail(channel) > 20)
979 mask |= EPOLLOUT | EPOLLWRNORM;
985 * Finds the device_node for the smd child interested in this channel.
987 static struct device_node *qcom_smd_match_channel(struct device_node *edge_node,
990 struct device_node *child;
995 for_each_available_child_of_node(edge_node, child) {
996 key = "qcom,smd-channels";
997 ret = of_property_read_string(child, key, &name);
1001 if (strcmp(name, channel) == 0)
1008 static int qcom_smd_announce_create(struct rpmsg_device *rpdev)
1010 struct qcom_smd_endpoint *qept = to_smd_endpoint(rpdev->ept);
1011 struct qcom_smd_channel *channel = qept->qsch;
1012 unsigned long flags;
1015 spin_lock_irqsave(&channel->recv_lock, flags);
1016 kick_state = qcom_smd_channel_intr(channel);
1017 spin_unlock_irqrestore(&channel->recv_lock, flags);
1020 schedule_work(&channel->edge->state_work);
1025 static const struct rpmsg_device_ops qcom_smd_device_ops = {
1026 .create_ept = qcom_smd_create_ept,
1027 .announce_create = qcom_smd_announce_create,
1030 static const struct rpmsg_endpoint_ops qcom_smd_endpoint_ops = {
1031 .destroy_ept = qcom_smd_destroy_ept,
1032 .send = qcom_smd_send,
1033 .trysend = qcom_smd_trysend,
1034 .poll = qcom_smd_poll,
1037 static void qcom_smd_release_device(struct device *dev)
1039 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1040 struct qcom_smd_device *qsdev = to_smd_device(rpdev);
1046 * Create a smd client device for channel that is being opened.
1048 static int qcom_smd_create_device(struct qcom_smd_channel *channel)
1050 struct qcom_smd_device *qsdev;
1051 struct rpmsg_device *rpdev;
1052 struct qcom_smd_edge *edge = channel->edge;
1054 dev_dbg(&edge->dev, "registering '%s'\n", channel->name);
1056 qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
1060 /* Link qsdev to our SMD edge */
1063 /* Assign callbacks for rpmsg_device */
1064 qsdev->rpdev.ops = &qcom_smd_device_ops;
1066 /* Assign public information to the rpmsg_device */
1067 rpdev = &qsdev->rpdev;
1068 strncpy(rpdev->id.name, channel->name, RPMSG_NAME_SIZE);
1069 rpdev->src = RPMSG_ADDR_ANY;
1070 rpdev->dst = RPMSG_ADDR_ANY;
1072 rpdev->dev.of_node = qcom_smd_match_channel(edge->of_node, channel->name);
1073 rpdev->dev.parent = &edge->dev;
1074 rpdev->dev.release = qcom_smd_release_device;
1076 return rpmsg_register_device(rpdev);
1079 static int qcom_smd_create_chrdev(struct qcom_smd_edge *edge)
1081 struct qcom_smd_device *qsdev;
1083 qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
1088 qsdev->rpdev.ops = &qcom_smd_device_ops;
1089 qsdev->rpdev.dev.parent = &edge->dev;
1090 qsdev->rpdev.dev.release = qcom_smd_release_device;
1092 return rpmsg_chrdev_register_device(&qsdev->rpdev);
1096 * Allocate the qcom_smd_channel object for a newly found smd channel,
1097 * retrieving and validating the smem items involved.
1099 static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge,
1100 unsigned smem_info_item,
1101 unsigned smem_fifo_item,
1104 struct qcom_smd_channel *channel;
1111 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
1113 return ERR_PTR(-ENOMEM);
1115 channel->edge = edge;
1116 channel->name = kstrdup(name, GFP_KERNEL);
1118 return ERR_PTR(-ENOMEM);
1120 spin_lock_init(&channel->tx_lock);
1121 spin_lock_init(&channel->recv_lock);
1122 init_waitqueue_head(&channel->fblockread_event);
1123 init_waitqueue_head(&channel->state_change_event);
1125 info = qcom_smem_get(edge->remote_pid, smem_info_item, &info_size);
1127 ret = PTR_ERR(info);
1128 goto free_name_and_channel;
1132 * Use the size of the item to figure out which channel info struct to
1135 if (info_size == 2 * sizeof(struct smd_channel_info_word)) {
1136 channel->info_word = info;
1137 } else if (info_size == 2 * sizeof(struct smd_channel_info)) {
1138 channel->info = info;
1141 "channel info of size %zu not supported\n", info_size);
1143 goto free_name_and_channel;
1146 fifo_base = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_size);
1147 if (IS_ERR(fifo_base)) {
1148 ret = PTR_ERR(fifo_base);
1149 goto free_name_and_channel;
1152 /* The channel consist of a rx and tx fifo of equal size */
1155 dev_dbg(&edge->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1156 name, info_size, fifo_size);
1158 channel->tx_fifo = fifo_base;
1159 channel->rx_fifo = fifo_base + fifo_size;
1160 channel->fifo_size = fifo_size;
1162 qcom_smd_channel_reset(channel);
1166 free_name_and_channel:
1167 kfree(channel->name);
1170 return ERR_PTR(ret);
1174 * Scans the allocation table for any newly allocated channels, calls
1175 * qcom_smd_create_channel() to create representations of these and add
1176 * them to the edge's list of channels.
1178 static void qcom_channel_scan_worker(struct work_struct *work)
1180 struct qcom_smd_edge *edge = container_of(work, struct qcom_smd_edge, scan_work);
1181 struct qcom_smd_alloc_entry *alloc_tbl;
1182 struct qcom_smd_alloc_entry *entry;
1183 struct qcom_smd_channel *channel;
1184 unsigned long flags;
1191 for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) {
1192 alloc_tbl = qcom_smem_get(edge->remote_pid,
1193 smem_items[tbl].alloc_tbl_id, NULL);
1194 if (IS_ERR(alloc_tbl))
1197 for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) {
1198 entry = &alloc_tbl[i];
1199 eflags = le32_to_cpu(entry->flags);
1200 if (test_bit(i, edge->allocated[tbl]))
1203 if (entry->ref_count == 0)
1206 if (!entry->name[0])
1209 if (!(eflags & SMD_CHANNEL_FLAGS_PACKET))
1212 if ((eflags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id)
1215 cid = le32_to_cpu(entry->cid);
1216 info_id = smem_items[tbl].info_base_id + cid;
1217 fifo_id = smem_items[tbl].fifo_base_id + cid;
1219 channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
1220 if (IS_ERR(channel))
1223 spin_lock_irqsave(&edge->channels_lock, flags);
1224 list_add(&channel->list, &edge->channels);
1225 spin_unlock_irqrestore(&edge->channels_lock, flags);
1227 dev_dbg(&edge->dev, "new channel found: '%s'\n", channel->name);
1228 set_bit(i, edge->allocated[tbl]);
1230 wake_up_interruptible_all(&edge->new_channel_event);
1234 schedule_work(&edge->state_work);
1238 * This per edge worker scans smem for any new channels and register these. It
1239 * then scans all registered channels for state changes that should be handled
1240 * by creating or destroying smd client devices for the registered channels.
1242 * LOCKING: edge->channels_lock only needs to cover the list operations, as the
1243 * worker is killed before any channels are deallocated
1245 static void qcom_channel_state_worker(struct work_struct *work)
1247 struct qcom_smd_channel *channel;
1248 struct qcom_smd_edge *edge = container_of(work,
1249 struct qcom_smd_edge,
1251 struct rpmsg_channel_info chinfo;
1252 unsigned remote_state;
1253 unsigned long flags;
1256 * Register a device for any closed channel where the remote processor
1257 * is showing interest in opening the channel.
1259 spin_lock_irqsave(&edge->channels_lock, flags);
1260 list_for_each_entry(channel, &edge->channels, list) {
1261 if (channel->state != SMD_CHANNEL_CLOSED)
1264 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1265 if (remote_state != SMD_CHANNEL_OPENING &&
1266 remote_state != SMD_CHANNEL_OPENED)
1269 if (channel->registered)
1272 spin_unlock_irqrestore(&edge->channels_lock, flags);
1273 qcom_smd_create_device(channel);
1274 channel->registered = true;
1275 spin_lock_irqsave(&edge->channels_lock, flags);
1277 channel->registered = true;
1281 * Unregister the device for any channel that is opened where the
1282 * remote processor is closing the channel.
1284 list_for_each_entry(channel, &edge->channels, list) {
1285 if (channel->state != SMD_CHANNEL_OPENING &&
1286 channel->state != SMD_CHANNEL_OPENED)
1289 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1290 if (remote_state == SMD_CHANNEL_OPENING ||
1291 remote_state == SMD_CHANNEL_OPENED)
1294 spin_unlock_irqrestore(&edge->channels_lock, flags);
1296 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1297 chinfo.src = RPMSG_ADDR_ANY;
1298 chinfo.dst = RPMSG_ADDR_ANY;
1299 rpmsg_unregister_device(&edge->dev, &chinfo);
1300 channel->registered = false;
1301 spin_lock_irqsave(&edge->channels_lock, flags);
1303 spin_unlock_irqrestore(&edge->channels_lock, flags);
1307 * Parses an of_node describing an edge.
1309 static int qcom_smd_parse_edge(struct device *dev,
1310 struct device_node *node,
1311 struct qcom_smd_edge *edge)
1313 struct device_node *syscon_np;
1318 INIT_LIST_HEAD(&edge->channels);
1319 spin_lock_init(&edge->channels_lock);
1321 INIT_WORK(&edge->scan_work, qcom_channel_scan_worker);
1322 INIT_WORK(&edge->state_work, qcom_channel_state_worker);
1324 edge->of_node = of_node_get(node);
1326 key = "qcom,smd-edge";
1327 ret = of_property_read_u32(node, key, &edge->edge_id);
1329 dev_err(dev, "edge missing %s property\n", key);
1333 edge->remote_pid = QCOM_SMEM_HOST_ANY;
1334 key = "qcom,remote-pid";
1335 of_property_read_u32(node, key, &edge->remote_pid);
1337 edge->mbox_client.dev = dev;
1338 edge->mbox_client.knows_txdone = true;
1339 edge->mbox_chan = mbox_request_channel(&edge->mbox_client, 0);
1340 if (IS_ERR(edge->mbox_chan)) {
1341 if (PTR_ERR(edge->mbox_chan) != -ENODEV)
1342 return PTR_ERR(edge->mbox_chan);
1344 edge->mbox_chan = NULL;
1346 syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
1348 dev_err(dev, "no qcom,ipc node\n");
1352 edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
1353 if (IS_ERR(edge->ipc_regmap))
1354 return PTR_ERR(edge->ipc_regmap);
1357 ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
1359 dev_err(dev, "no offset in %s\n", key);
1363 ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
1365 dev_err(dev, "no bit in %s\n", key);
1370 ret = of_property_read_string(node, "label", &edge->name);
1372 edge->name = node->name;
1374 irq = irq_of_parse_and_map(node, 0);
1376 dev_err(dev, "required smd interrupt missing\n");
1380 ret = devm_request_irq(dev, irq,
1381 qcom_smd_edge_intr, IRQF_TRIGGER_RISING,
1384 dev_err(dev, "failed to request smd irq\n");
1394 * Release function for an edge.
1395 * Reset the state of each associated channel and free the edge context.
1397 static void qcom_smd_edge_release(struct device *dev)
1399 struct qcom_smd_channel *channel, *tmp;
1400 struct qcom_smd_edge *edge = to_smd_edge(dev);
1402 list_for_each_entry_safe(channel, tmp, &edge->channels, list) {
1403 list_del(&channel->list);
1404 kfree(channel->name);
1411 static ssize_t rpmsg_name_show(struct device *dev,
1412 struct device_attribute *attr, char *buf)
1414 struct qcom_smd_edge *edge = to_smd_edge(dev);
1416 return sprintf(buf, "%s\n", edge->name);
1418 static DEVICE_ATTR_RO(rpmsg_name);
1420 static struct attribute *qcom_smd_edge_attrs[] = {
1421 &dev_attr_rpmsg_name.attr,
1424 ATTRIBUTE_GROUPS(qcom_smd_edge);
1427 * qcom_smd_register_edge() - register an edge based on an device_node
1428 * @parent: parent device for the edge
1429 * @node: device_node describing the edge
1431 * Returns an edge reference, or negative ERR_PTR() on failure.
1433 struct qcom_smd_edge *qcom_smd_register_edge(struct device *parent,
1434 struct device_node *node)
1436 struct qcom_smd_edge *edge;
1439 edge = kzalloc(sizeof(*edge), GFP_KERNEL);
1441 return ERR_PTR(-ENOMEM);
1443 init_waitqueue_head(&edge->new_channel_event);
1445 edge->dev.parent = parent;
1446 edge->dev.release = qcom_smd_edge_release;
1447 edge->dev.of_node = node;
1448 edge->dev.groups = qcom_smd_edge_groups;
1449 dev_set_name(&edge->dev, "%s:%s", dev_name(parent), node->name);
1450 ret = device_register(&edge->dev);
1452 pr_err("failed to register smd edge\n");
1453 put_device(&edge->dev);
1454 return ERR_PTR(ret);
1457 ret = qcom_smd_parse_edge(&edge->dev, node, edge);
1459 dev_err(&edge->dev, "failed to parse smd edge\n");
1460 goto unregister_dev;
1463 ret = qcom_smd_create_chrdev(edge);
1465 dev_err(&edge->dev, "failed to register chrdev for edge\n");
1466 goto unregister_dev;
1469 schedule_work(&edge->scan_work);
1474 if (!IS_ERR_OR_NULL(edge->mbox_chan))
1475 mbox_free_channel(edge->mbox_chan);
1477 device_unregister(&edge->dev);
1478 return ERR_PTR(ret);
1480 EXPORT_SYMBOL(qcom_smd_register_edge);
1482 static int qcom_smd_remove_device(struct device *dev, void *data)
1484 device_unregister(dev);
1490 * qcom_smd_unregister_edge() - release an edge and its children
1491 * @edge: edge reference acquired from qcom_smd_register_edge
1493 int qcom_smd_unregister_edge(struct qcom_smd_edge *edge)
1497 disable_irq(edge->irq);
1498 cancel_work_sync(&edge->scan_work);
1499 cancel_work_sync(&edge->state_work);
1501 ret = device_for_each_child(&edge->dev, NULL, qcom_smd_remove_device);
1503 dev_warn(&edge->dev, "can't remove smd device: %d\n", ret);
1505 mbox_free_channel(edge->mbox_chan);
1506 device_unregister(&edge->dev);
1510 EXPORT_SYMBOL(qcom_smd_unregister_edge);
1512 static int qcom_smd_probe(struct platform_device *pdev)
1514 struct device_node *node;
1518 p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL);
1519 if (PTR_ERR(p) == -EPROBE_DEFER)
1522 for_each_available_child_of_node(pdev->dev.of_node, node)
1523 qcom_smd_register_edge(&pdev->dev, node);
1528 static int qcom_smd_remove_edge(struct device *dev, void *data)
1530 struct qcom_smd_edge *edge = to_smd_edge(dev);
1532 return qcom_smd_unregister_edge(edge);
1536 * Shut down all smd clients by making sure that each edge stops processing
1537 * events and scanning for new channels, then call destroy on the devices.
1539 static int qcom_smd_remove(struct platform_device *pdev)
1543 ret = device_for_each_child(&pdev->dev, NULL, qcom_smd_remove_edge);
1545 dev_warn(&pdev->dev, "can't remove smd device: %d\n", ret);
1550 static const struct of_device_id qcom_smd_of_match[] = {
1551 { .compatible = "qcom,smd" },
1554 MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
1556 static struct platform_driver qcom_smd_driver = {
1557 .probe = qcom_smd_probe,
1558 .remove = qcom_smd_remove,
1561 .of_match_table = qcom_smd_of_match,
1565 static int __init qcom_smd_init(void)
1567 return platform_driver_register(&qcom_smd_driver);
1569 subsys_initcall(qcom_smd_init);
1571 static void __exit qcom_smd_exit(void)
1573 platform_driver_unregister(&qcom_smd_driver);
1575 module_exit(qcom_smd_exit);
1578 MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
1579 MODULE_LICENSE("GPL v2");