2 * Copyright (c) 2015, Sony Mobile Communications AB.
3 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/interrupt.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_platform.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/soc/qcom/smem.h>
26 #include <linux/wait.h>
27 #include <linux/rpmsg.h>
28 #include <linux/rpmsg/qcom_smd.h>
30 #include "rpmsg_internal.h"
33 * The Qualcomm Shared Memory communication solution provides point-to-point
34 * channels for clients to send and receive streaming or packet based data.
36 * Each channel consists of a control item (channel info) and a ring buffer
37 * pair. The channel info carry information related to channel state, flow
38 * control and the offsets within the ring buffer.
40 * All allocated channels are listed in an allocation table, identifying the
41 * pair of items by name, type and remote processor.
43 * Upon creating a new channel the remote processor allocates channel info and
44 * ring buffer items from the smem heap and populate the allocation table. An
45 * interrupt is sent to the other end of the channel and a scan for new
46 * channels should be done. A channel never goes away, it will only change
49 * The remote processor signals it intent for bring up the communication
50 * channel by setting the state of its end of the channel to "opening" and
51 * sends out an interrupt. We detect this change and register a smd device to
52 * consume the channel. Upon finding a consumer we finish the handshake and the
55 * Upon closing a channel, the remote processor will update the state of its
56 * end of the channel and signal us, we will then unregister any attached
57 * device and close our end of the channel.
59 * Devices attached to a channel can use the qcom_smd_send function to push
60 * data to the channel, this is done by copying the data into the tx ring
61 * buffer, updating the pointers in the channel info and signaling the remote
64 * The remote processor does the equivalent when it transfer data and upon
65 * receiving the interrupt we check the channel info for new data and delivers
66 * this to the attached device. If the device is not ready to receive the data
67 * we leave it in the ring buffer for now.
70 struct smd_channel_info
;
71 struct smd_channel_info_pair
;
72 struct smd_channel_info_word
;
73 struct smd_channel_info_word_pair
;
75 static const struct rpmsg_endpoint_ops qcom_smd_endpoint_ops
;
77 #define SMD_ALLOC_TBL_COUNT 2
78 #define SMD_ALLOC_TBL_SIZE 64
81 * This lists the various smem heap items relevant for the allocation table and
82 * smd channel entries.
85 unsigned alloc_tbl_id
;
86 unsigned info_base_id
;
87 unsigned fifo_base_id
;
88 } smem_items
[SMD_ALLOC_TBL_COUNT
] = {
102 * struct qcom_smd_edge - representing a remote processor
103 * @of_node: of_node handle for information related to this edge
104 * @edge_id: identifier of this edge
105 * @remote_pid: identifier of remote processor
106 * @irq: interrupt for signals on this edge
107 * @ipc_regmap: regmap handle holding the outgoing ipc register
108 * @ipc_offset: offset within @ipc_regmap of the register for ipc
109 * @ipc_bit: bit in the register at @ipc_offset of @ipc_regmap
110 * @channels: list of all channels detected on this edge
111 * @channels_lock: guard for modifications of @channels
112 * @allocated: array of bitmaps representing already allocated channels
113 * @smem_available: last available amount of smem triggering a channel scan
114 * @scan_work: work item for discovering new channels
115 * @state_work: work item for edge state changes
117 struct qcom_smd_edge
{
122 struct device_node
*of_node
;
128 struct regmap
*ipc_regmap
;
132 struct list_head channels
;
133 spinlock_t channels_lock
;
135 DECLARE_BITMAP(allocated
[SMD_ALLOC_TBL_COUNT
], SMD_ALLOC_TBL_SIZE
);
137 unsigned smem_available
;
139 wait_queue_head_t new_channel_event
;
141 struct work_struct scan_work
;
142 struct work_struct state_work
;
146 * SMD channel states.
148 enum smd_channel_state
{
152 SMD_CHANNEL_FLUSHING
,
155 SMD_CHANNEL_RESET_OPENING
158 struct qcom_smd_device
{
159 struct rpmsg_device rpdev
;
161 struct qcom_smd_edge
*edge
;
164 struct qcom_smd_endpoint
{
165 struct rpmsg_endpoint ept
;
167 struct qcom_smd_channel
*qsch
;
170 #define to_smd_device(_rpdev) container_of(_rpdev, struct qcom_smd_device, rpdev)
171 #define to_smd_edge(d) container_of(d, struct qcom_smd_edge, dev)
172 #define to_smd_endpoint(ept) container_of(ept, struct qcom_smd_endpoint, ept)
175 * struct qcom_smd_channel - smd channel struct
176 * @edge: qcom_smd_edge this channel is living on
177 * @qsdev: reference to a associated smd client device
178 * @name: name of the channel
179 * @state: local state of the channel
180 * @remote_state: remote state of the channel
181 * @info: byte aligned outgoing/incoming channel info
182 * @info_word: word aligned outgoing/incoming channel info
183 * @tx_lock: lock to make writes to the channel mutually exclusive
184 * @fblockread_event: wakeup event tied to tx fBLOCKREADINTR
185 * @tx_fifo: pointer to the outgoing ring buffer
186 * @rx_fifo: pointer to the incoming ring buffer
187 * @fifo_size: size of each ring buffer
188 * @bounce_buffer: bounce buffer for reading wrapped packets
189 * @cb: callback function registered for this channel
190 * @recv_lock: guard for rx info modifications and cb pointer
191 * @pkt_size: size of the currently handled packet
192 * @list: lite entry for @channels in qcom_smd_edge
194 struct qcom_smd_channel
{
195 struct qcom_smd_edge
*edge
;
197 struct qcom_smd_endpoint
*qsept
;
201 enum smd_channel_state state
;
202 enum smd_channel_state remote_state
;
204 struct smd_channel_info_pair
*info
;
205 struct smd_channel_info_word_pair
*info_word
;
207 struct mutex tx_lock
;
208 wait_queue_head_t fblockread_event
;
216 spinlock_t recv_lock
;
222 struct list_head list
;
226 * Format of the smd_info smem items, for byte aligned channels.
228 struct smd_channel_info
{
242 struct smd_channel_info_pair
{
243 struct smd_channel_info tx
;
244 struct smd_channel_info rx
;
248 * Format of the smd_info smem items, for word aligned channels.
250 struct smd_channel_info_word
{
259 __le32 fBLOCKREADINTR
;
264 struct smd_channel_info_word_pair
{
265 struct smd_channel_info_word tx
;
266 struct smd_channel_info_word rx
;
269 #define GET_RX_CHANNEL_FLAG(channel, param) \
271 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
272 channel->info_word ? \
273 le32_to_cpu(channel->info_word->rx.param) : \
274 channel->info->rx.param; \
277 #define GET_RX_CHANNEL_INFO(channel, param) \
279 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
280 le32_to_cpu(channel->info_word ? \
281 channel->info_word->rx.param : \
282 channel->info->rx.param); \
285 #define SET_RX_CHANNEL_FLAG(channel, param, value) \
287 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
288 if (channel->info_word) \
289 channel->info_word->rx.param = cpu_to_le32(value); \
291 channel->info->rx.param = value; \
294 #define SET_RX_CHANNEL_INFO(channel, param, value) \
296 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
297 if (channel->info_word) \
298 channel->info_word->rx.param = cpu_to_le32(value); \
300 channel->info->rx.param = cpu_to_le32(value); \
303 #define GET_TX_CHANNEL_FLAG(channel, param) \
305 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
306 channel->info_word ? \
307 le32_to_cpu(channel->info_word->tx.param) : \
308 channel->info->tx.param; \
311 #define GET_TX_CHANNEL_INFO(channel, param) \
313 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
314 le32_to_cpu(channel->info_word ? \
315 channel->info_word->tx.param : \
316 channel->info->tx.param); \
319 #define SET_TX_CHANNEL_FLAG(channel, param, value) \
321 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
322 if (channel->info_word) \
323 channel->info_word->tx.param = cpu_to_le32(value); \
325 channel->info->tx.param = value; \
328 #define SET_TX_CHANNEL_INFO(channel, param, value) \
330 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
331 if (channel->info_word) \
332 channel->info_word->tx.param = cpu_to_le32(value); \
334 channel->info->tx.param = cpu_to_le32(value); \
338 * struct qcom_smd_alloc_entry - channel allocation entry
339 * @name: channel name
340 * @cid: channel index
341 * @flags: channel flags and edge id
342 * @ref_count: reference count of the channel
344 struct qcom_smd_alloc_entry
{
351 #define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff
352 #define SMD_CHANNEL_FLAGS_STREAM BIT(8)
353 #define SMD_CHANNEL_FLAGS_PACKET BIT(9)
356 * Each smd packet contains a 20 byte header, with the first 4 being the length
359 #define SMD_PACKET_HEADER_LEN 20
362 * Signal the remote processor associated with 'channel'.
364 static void qcom_smd_signal_channel(struct qcom_smd_channel
*channel
)
366 struct qcom_smd_edge
*edge
= channel
->edge
;
368 regmap_write(edge
->ipc_regmap
, edge
->ipc_offset
, BIT(edge
->ipc_bit
));
372 * Initialize the tx channel info
374 static void qcom_smd_channel_reset(struct qcom_smd_channel
*channel
)
376 SET_TX_CHANNEL_INFO(channel
, state
, SMD_CHANNEL_CLOSED
);
377 SET_TX_CHANNEL_FLAG(channel
, fDSR
, 0);
378 SET_TX_CHANNEL_FLAG(channel
, fCTS
, 0);
379 SET_TX_CHANNEL_FLAG(channel
, fCD
, 0);
380 SET_TX_CHANNEL_FLAG(channel
, fRI
, 0);
381 SET_TX_CHANNEL_FLAG(channel
, fHEAD
, 0);
382 SET_TX_CHANNEL_FLAG(channel
, fTAIL
, 0);
383 SET_TX_CHANNEL_FLAG(channel
, fSTATE
, 1);
384 SET_TX_CHANNEL_FLAG(channel
, fBLOCKREADINTR
, 1);
385 SET_TX_CHANNEL_INFO(channel
, head
, 0);
386 SET_RX_CHANNEL_INFO(channel
, tail
, 0);
388 qcom_smd_signal_channel(channel
);
390 channel
->state
= SMD_CHANNEL_CLOSED
;
391 channel
->pkt_size
= 0;
395 * Set the callback for a channel, with appropriate locking
397 static void qcom_smd_channel_set_callback(struct qcom_smd_channel
*channel
,
400 struct rpmsg_endpoint
*ept
= &channel
->qsept
->ept
;
403 spin_lock_irqsave(&channel
->recv_lock
, flags
);
405 spin_unlock_irqrestore(&channel
->recv_lock
, flags
);
409 * Calculate the amount of data available in the rx fifo
411 static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel
*channel
)
416 head
= GET_RX_CHANNEL_INFO(channel
, head
);
417 tail
= GET_RX_CHANNEL_INFO(channel
, tail
);
419 return (head
- tail
) & (channel
->fifo_size
- 1);
423 * Set tx channel state and inform the remote processor
425 static void qcom_smd_channel_set_state(struct qcom_smd_channel
*channel
,
428 struct qcom_smd_edge
*edge
= channel
->edge
;
429 bool is_open
= state
== SMD_CHANNEL_OPENED
;
431 if (channel
->state
== state
)
434 dev_dbg(&edge
->dev
, "set_state(%s, %d)\n", channel
->name
, state
);
436 SET_TX_CHANNEL_FLAG(channel
, fDSR
, is_open
);
437 SET_TX_CHANNEL_FLAG(channel
, fCTS
, is_open
);
438 SET_TX_CHANNEL_FLAG(channel
, fCD
, is_open
);
440 SET_TX_CHANNEL_INFO(channel
, state
, state
);
441 SET_TX_CHANNEL_FLAG(channel
, fSTATE
, 1);
443 channel
->state
= state
;
444 qcom_smd_signal_channel(channel
);
448 * Copy count bytes of data using 32bit accesses, if that's required.
450 static void smd_copy_to_fifo(void __iomem
*dst
,
456 __iowrite32_copy(dst
, src
, count
/ sizeof(u32
));
458 memcpy_toio(dst
, src
, count
);
463 * Copy count bytes of data using 32bit accesses, if that is required.
465 static void smd_copy_from_fifo(void *dst
,
466 const void __iomem
*src
,
471 __ioread32_copy(dst
, src
, count
/ sizeof(u32
));
473 memcpy_fromio(dst
, src
, count
);
478 * Read count bytes of data from the rx fifo into buf, but don't advance the
481 static size_t qcom_smd_channel_peek(struct qcom_smd_channel
*channel
,
482 void *buf
, size_t count
)
488 word_aligned
= channel
->info_word
;
489 tail
= GET_RX_CHANNEL_INFO(channel
, tail
);
491 len
= min_t(size_t, count
, channel
->fifo_size
- tail
);
493 smd_copy_from_fifo(buf
,
494 channel
->rx_fifo
+ tail
,
500 smd_copy_from_fifo(buf
+ len
,
510 * Advance the rx tail by count bytes.
512 static void qcom_smd_channel_advance(struct qcom_smd_channel
*channel
,
517 tail
= GET_RX_CHANNEL_INFO(channel
, tail
);
519 tail
&= (channel
->fifo_size
- 1);
520 SET_RX_CHANNEL_INFO(channel
, tail
, tail
);
524 * Read out a single packet from the rx fifo and deliver it to the device
526 static int qcom_smd_channel_recv_single(struct qcom_smd_channel
*channel
)
528 struct rpmsg_endpoint
*ept
= &channel
->qsept
->ept
;
534 tail
= GET_RX_CHANNEL_INFO(channel
, tail
);
536 /* Use bounce buffer if the data wraps */
537 if (tail
+ channel
->pkt_size
>= channel
->fifo_size
) {
538 ptr
= channel
->bounce_buffer
;
539 len
= qcom_smd_channel_peek(channel
, ptr
, channel
->pkt_size
);
541 ptr
= channel
->rx_fifo
+ tail
;
542 len
= channel
->pkt_size
;
545 ret
= ept
->cb(ept
->rpdev
, ptr
, len
, ept
->priv
, RPMSG_ADDR_ANY
);
549 /* Only forward the tail if the client consumed the data */
550 qcom_smd_channel_advance(channel
, len
);
552 channel
->pkt_size
= 0;
558 * Per channel interrupt handling
560 static bool qcom_smd_channel_intr(struct qcom_smd_channel
*channel
)
562 bool need_state_scan
= false;
568 /* Handle state changes */
569 remote_state
= GET_RX_CHANNEL_INFO(channel
, state
);
570 if (remote_state
!= channel
->remote_state
) {
571 channel
->remote_state
= remote_state
;
572 need_state_scan
= true;
574 /* Indicate that we have seen any state change */
575 SET_RX_CHANNEL_FLAG(channel
, fSTATE
, 0);
577 /* Signal waiting qcom_smd_send() about the interrupt */
578 if (!GET_TX_CHANNEL_FLAG(channel
, fBLOCKREADINTR
))
579 wake_up_interruptible(&channel
->fblockread_event
);
581 /* Don't consume any data until we've opened the channel */
582 if (channel
->state
!= SMD_CHANNEL_OPENED
)
585 /* Indicate that we've seen the new data */
586 SET_RX_CHANNEL_FLAG(channel
, fHEAD
, 0);
590 avail
= qcom_smd_channel_get_rx_avail(channel
);
592 if (!channel
->pkt_size
&& avail
>= SMD_PACKET_HEADER_LEN
) {
593 qcom_smd_channel_peek(channel
, &pktlen
, sizeof(pktlen
));
594 qcom_smd_channel_advance(channel
, SMD_PACKET_HEADER_LEN
);
595 channel
->pkt_size
= le32_to_cpu(pktlen
);
596 } else if (channel
->pkt_size
&& avail
>= channel
->pkt_size
) {
597 ret
= qcom_smd_channel_recv_single(channel
);
605 /* Indicate that we have seen and updated tail */
606 SET_RX_CHANNEL_FLAG(channel
, fTAIL
, 1);
608 /* Signal the remote that we've consumed the data (if requested) */
609 if (!GET_RX_CHANNEL_FLAG(channel
, fBLOCKREADINTR
)) {
610 /* Ensure ordering of channel info updates */
613 qcom_smd_signal_channel(channel
);
617 return need_state_scan
;
621 * The edge interrupts are triggered by the remote processor on state changes,
622 * channel info updates or when new channels are created.
624 static irqreturn_t
qcom_smd_edge_intr(int irq
, void *data
)
626 struct qcom_smd_edge
*edge
= data
;
627 struct qcom_smd_channel
*channel
;
629 bool kick_scanner
= false;
630 bool kick_state
= false;
633 * Handle state changes or data on each of the channels on this edge
635 spin_lock(&edge
->channels_lock
);
636 list_for_each_entry(channel
, &edge
->channels
, list
) {
637 spin_lock(&channel
->recv_lock
);
638 kick_state
|= qcom_smd_channel_intr(channel
);
639 spin_unlock(&channel
->recv_lock
);
641 spin_unlock(&edge
->channels_lock
);
644 * Creating a new channel requires allocating an smem entry, so we only
645 * have to scan if the amount of available space in smem have changed
648 available
= qcom_smem_get_free_space(edge
->remote_pid
);
649 if (available
!= edge
->smem_available
) {
650 edge
->smem_available
= available
;
655 schedule_work(&edge
->scan_work
);
657 schedule_work(&edge
->state_work
);
663 * Calculate how much space is available in the tx fifo.
665 static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel
*channel
)
669 unsigned mask
= channel
->fifo_size
- 1;
671 head
= GET_TX_CHANNEL_INFO(channel
, head
);
672 tail
= GET_TX_CHANNEL_INFO(channel
, tail
);
674 return mask
- ((head
- tail
) & mask
);
678 * Write count bytes of data into channel, possibly wrapping in the ring buffer
680 static int qcom_smd_write_fifo(struct qcom_smd_channel
*channel
,
688 word_aligned
= channel
->info_word
;
689 head
= GET_TX_CHANNEL_INFO(channel
, head
);
691 len
= min_t(size_t, count
, channel
->fifo_size
- head
);
693 smd_copy_to_fifo(channel
->tx_fifo
+ head
,
700 smd_copy_to_fifo(channel
->tx_fifo
,
707 head
&= (channel
->fifo_size
- 1);
708 SET_TX_CHANNEL_INFO(channel
, head
, head
);
714 * qcom_smd_send - write data to smd channel
715 * @channel: channel handle
716 * @data: buffer of data to write
717 * @len: number of bytes to write
719 * This is a blocking write of len bytes into the channel's tx ring buffer and
720 * signal the remote end. It will sleep until there is enough space available
721 * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
724 static int __qcom_smd_send(struct qcom_smd_channel
*channel
, const void *data
,
727 __le32 hdr
[5] = { cpu_to_le32(len
), };
728 int tlen
= sizeof(hdr
) + len
;
731 /* Word aligned channels only accept word size aligned data */
732 if (channel
->info_word
&& len
% 4)
735 /* Reject packets that are too big */
736 if (tlen
>= channel
->fifo_size
)
739 ret
= mutex_lock_interruptible(&channel
->tx_lock
);
743 while (qcom_smd_get_tx_avail(channel
) < tlen
) {
749 if (channel
->state
!= SMD_CHANNEL_OPENED
) {
754 SET_TX_CHANNEL_FLAG(channel
, fBLOCKREADINTR
, 0);
756 ret
= wait_event_interruptible(channel
->fblockread_event
,
757 qcom_smd_get_tx_avail(channel
) >= tlen
||
758 channel
->state
!= SMD_CHANNEL_OPENED
);
762 SET_TX_CHANNEL_FLAG(channel
, fBLOCKREADINTR
, 1);
765 SET_TX_CHANNEL_FLAG(channel
, fTAIL
, 0);
767 qcom_smd_write_fifo(channel
, hdr
, sizeof(hdr
));
768 qcom_smd_write_fifo(channel
, data
, len
);
770 SET_TX_CHANNEL_FLAG(channel
, fHEAD
, 1);
772 /* Ensure ordering of channel info updates */
775 qcom_smd_signal_channel(channel
);
778 mutex_unlock(&channel
->tx_lock
);
784 * Helper for opening a channel
786 static int qcom_smd_channel_open(struct qcom_smd_channel
*channel
,
792 * Packets are maximum 4k, but reduce if the fifo is smaller
794 bb_size
= min(channel
->fifo_size
, SZ_4K
);
795 channel
->bounce_buffer
= kmalloc(bb_size
, GFP_KERNEL
);
796 if (!channel
->bounce_buffer
)
799 qcom_smd_channel_set_callback(channel
, cb
);
800 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_OPENING
);
801 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_OPENED
);
807 * Helper for closing and resetting a channel
809 static void qcom_smd_channel_close(struct qcom_smd_channel
*channel
)
811 qcom_smd_channel_set_callback(channel
, NULL
);
813 kfree(channel
->bounce_buffer
);
814 channel
->bounce_buffer
= NULL
;
816 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_CLOSED
);
817 qcom_smd_channel_reset(channel
);
820 static struct qcom_smd_channel
*
821 qcom_smd_find_channel(struct qcom_smd_edge
*edge
, const char *name
)
823 struct qcom_smd_channel
*channel
;
824 struct qcom_smd_channel
*ret
= NULL
;
827 spin_lock_irqsave(&edge
->channels_lock
, flags
);
828 list_for_each_entry(channel
, &edge
->channels
, list
) {
829 if (!strcmp(channel
->name
, name
)) {
834 spin_unlock_irqrestore(&edge
->channels_lock
, flags
);
839 static void __ept_release(struct kref
*kref
)
841 struct rpmsg_endpoint
*ept
= container_of(kref
, struct rpmsg_endpoint
,
843 kfree(to_smd_endpoint(ept
));
846 static struct rpmsg_endpoint
*qcom_smd_create_ept(struct rpmsg_device
*rpdev
,
847 rpmsg_rx_cb_t cb
, void *priv
,
848 struct rpmsg_channel_info chinfo
)
850 struct qcom_smd_endpoint
*qsept
;
851 struct qcom_smd_channel
*channel
;
852 struct qcom_smd_device
*qsdev
= to_smd_device(rpdev
);
853 struct qcom_smd_edge
*edge
= qsdev
->edge
;
854 struct rpmsg_endpoint
*ept
;
855 const char *name
= chinfo
.name
;
858 /* Wait up to HZ for the channel to appear */
859 ret
= wait_event_interruptible_timeout(edge
->new_channel_event
,
860 (channel
= qcom_smd_find_channel(edge
, name
)) != NULL
,
865 if (channel
->state
!= SMD_CHANNEL_CLOSED
) {
866 dev_err(&rpdev
->dev
, "channel %s is busy\n", channel
->name
);
870 qsept
= kzalloc(sizeof(*qsept
), GFP_KERNEL
);
876 kref_init(&ept
->refcount
);
881 ept
->ops
= &qcom_smd_endpoint_ops
;
883 channel
->qsept
= qsept
;
884 qsept
->qsch
= channel
;
886 ret
= qcom_smd_channel_open(channel
, cb
);
893 channel
->qsept
= NULL
;
894 kref_put(&ept
->refcount
, __ept_release
);
898 static void qcom_smd_destroy_ept(struct rpmsg_endpoint
*ept
)
900 struct qcom_smd_endpoint
*qsept
= to_smd_endpoint(ept
);
901 struct qcom_smd_channel
*ch
= qsept
->qsch
;
903 qcom_smd_channel_close(ch
);
905 kref_put(&ept
->refcount
, __ept_release
);
908 static int qcom_smd_send(struct rpmsg_endpoint
*ept
, void *data
, int len
)
910 struct qcom_smd_endpoint
*qsept
= to_smd_endpoint(ept
);
912 return __qcom_smd_send(qsept
->qsch
, data
, len
, true);
915 static int qcom_smd_trysend(struct rpmsg_endpoint
*ept
, void *data
, int len
)
917 struct qcom_smd_endpoint
*qsept
= to_smd_endpoint(ept
);
919 return __qcom_smd_send(qsept
->qsch
, data
, len
, false);
922 static unsigned int qcom_smd_poll(struct rpmsg_endpoint
*ept
,
923 struct file
*filp
, poll_table
*wait
)
925 struct qcom_smd_endpoint
*qsept
= to_smd_endpoint(ept
);
926 struct qcom_smd_channel
*channel
= qsept
->qsch
;
927 unsigned int mask
= 0;
929 poll_wait(filp
, &channel
->fblockread_event
, wait
);
931 if (qcom_smd_get_tx_avail(channel
) > 20)
932 mask
|= POLLOUT
| POLLWRNORM
;
938 * Finds the device_node for the smd child interested in this channel.
940 static struct device_node
*qcom_smd_match_channel(struct device_node
*edge_node
,
943 struct device_node
*child
;
948 for_each_available_child_of_node(edge_node
, child
) {
949 key
= "qcom,smd-channels";
950 ret
= of_property_read_string(child
, key
, &name
);
954 if (strcmp(name
, channel
) == 0)
961 static const struct rpmsg_device_ops qcom_smd_device_ops
= {
962 .create_ept
= qcom_smd_create_ept
,
965 static const struct rpmsg_endpoint_ops qcom_smd_endpoint_ops
= {
966 .destroy_ept
= qcom_smd_destroy_ept
,
967 .send
= qcom_smd_send
,
968 .trysend
= qcom_smd_trysend
,
969 .poll
= qcom_smd_poll
,
973 * Create a smd client device for channel that is being opened.
975 static int qcom_smd_create_device(struct qcom_smd_channel
*channel
)
977 struct qcom_smd_device
*qsdev
;
978 struct rpmsg_device
*rpdev
;
979 struct qcom_smd_edge
*edge
= channel
->edge
;
981 dev_dbg(&edge
->dev
, "registering '%s'\n", channel
->name
);
983 qsdev
= kzalloc(sizeof(*qsdev
), GFP_KERNEL
);
987 /* Link qsdev to our SMD edge */
990 /* Assign callbacks for rpmsg_device */
991 qsdev
->rpdev
.ops
= &qcom_smd_device_ops
;
993 /* Assign public information to the rpmsg_device */
994 rpdev
= &qsdev
->rpdev
;
995 strncpy(rpdev
->id
.name
, channel
->name
, RPMSG_NAME_SIZE
);
996 rpdev
->src
= RPMSG_ADDR_ANY
;
997 rpdev
->dst
= RPMSG_ADDR_ANY
;
999 rpdev
->dev
.of_node
= qcom_smd_match_channel(edge
->of_node
, channel
->name
);
1000 rpdev
->dev
.parent
= &edge
->dev
;
1002 return rpmsg_register_device(rpdev
);
1005 static int qcom_smd_create_chrdev(struct qcom_smd_edge
*edge
)
1007 struct qcom_smd_device
*qsdev
;
1009 qsdev
= kzalloc(sizeof(*qsdev
), GFP_KERNEL
);
1014 qsdev
->rpdev
.ops
= &qcom_smd_device_ops
;
1015 qsdev
->rpdev
.dev
.parent
= &edge
->dev
;
1016 return rpmsg_chrdev_register_device(&qsdev
->rpdev
);
1020 * Allocate the qcom_smd_channel object for a newly found smd channel,
1021 * retrieving and validating the smem items involved.
1023 static struct qcom_smd_channel
*qcom_smd_create_channel(struct qcom_smd_edge
*edge
,
1024 unsigned smem_info_item
,
1025 unsigned smem_fifo_item
,
1028 struct qcom_smd_channel
*channel
;
1035 channel
= devm_kzalloc(&edge
->dev
, sizeof(*channel
), GFP_KERNEL
);
1037 return ERR_PTR(-ENOMEM
);
1039 channel
->edge
= edge
;
1040 channel
->name
= devm_kstrdup(&edge
->dev
, name
, GFP_KERNEL
);
1042 return ERR_PTR(-ENOMEM
);
1044 mutex_init(&channel
->tx_lock
);
1045 spin_lock_init(&channel
->recv_lock
);
1046 init_waitqueue_head(&channel
->fblockread_event
);
1048 info
= qcom_smem_get(edge
->remote_pid
, smem_info_item
, &info_size
);
1050 ret
= PTR_ERR(info
);
1051 goto free_name_and_channel
;
1055 * Use the size of the item to figure out which channel info struct to
1058 if (info_size
== 2 * sizeof(struct smd_channel_info_word
)) {
1059 channel
->info_word
= info
;
1060 } else if (info_size
== 2 * sizeof(struct smd_channel_info
)) {
1061 channel
->info
= info
;
1064 "channel info of size %zu not supported\n", info_size
);
1066 goto free_name_and_channel
;
1069 fifo_base
= qcom_smem_get(edge
->remote_pid
, smem_fifo_item
, &fifo_size
);
1070 if (IS_ERR(fifo_base
)) {
1071 ret
= PTR_ERR(fifo_base
);
1072 goto free_name_and_channel
;
1075 /* The channel consist of a rx and tx fifo of equal size */
1078 dev_dbg(&edge
->dev
, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1079 name
, info_size
, fifo_size
);
1081 channel
->tx_fifo
= fifo_base
;
1082 channel
->rx_fifo
= fifo_base
+ fifo_size
;
1083 channel
->fifo_size
= fifo_size
;
1085 qcom_smd_channel_reset(channel
);
1089 free_name_and_channel
:
1090 devm_kfree(&edge
->dev
, channel
->name
);
1091 devm_kfree(&edge
->dev
, channel
);
1093 return ERR_PTR(ret
);
1097 * Scans the allocation table for any newly allocated channels, calls
1098 * qcom_smd_create_channel() to create representations of these and add
1099 * them to the edge's list of channels.
1101 static void qcom_channel_scan_worker(struct work_struct
*work
)
1103 struct qcom_smd_edge
*edge
= container_of(work
, struct qcom_smd_edge
, scan_work
);
1104 struct qcom_smd_alloc_entry
*alloc_tbl
;
1105 struct qcom_smd_alloc_entry
*entry
;
1106 struct qcom_smd_channel
*channel
;
1107 unsigned long flags
;
1114 for (tbl
= 0; tbl
< SMD_ALLOC_TBL_COUNT
; tbl
++) {
1115 alloc_tbl
= qcom_smem_get(edge
->remote_pid
,
1116 smem_items
[tbl
].alloc_tbl_id
, NULL
);
1117 if (IS_ERR(alloc_tbl
))
1120 for (i
= 0; i
< SMD_ALLOC_TBL_SIZE
; i
++) {
1121 entry
= &alloc_tbl
[i
];
1122 eflags
= le32_to_cpu(entry
->flags
);
1123 if (test_bit(i
, edge
->allocated
[tbl
]))
1126 if (entry
->ref_count
== 0)
1129 if (!entry
->name
[0])
1132 if (!(eflags
& SMD_CHANNEL_FLAGS_PACKET
))
1135 if ((eflags
& SMD_CHANNEL_FLAGS_EDGE_MASK
) != edge
->edge_id
)
1138 cid
= le32_to_cpu(entry
->cid
);
1139 info_id
= smem_items
[tbl
].info_base_id
+ cid
;
1140 fifo_id
= smem_items
[tbl
].fifo_base_id
+ cid
;
1142 channel
= qcom_smd_create_channel(edge
, info_id
, fifo_id
, entry
->name
);
1143 if (IS_ERR(channel
))
1146 spin_lock_irqsave(&edge
->channels_lock
, flags
);
1147 list_add(&channel
->list
, &edge
->channels
);
1148 spin_unlock_irqrestore(&edge
->channels_lock
, flags
);
1150 dev_dbg(&edge
->dev
, "new channel found: '%s'\n", channel
->name
);
1151 set_bit(i
, edge
->allocated
[tbl
]);
1153 wake_up_interruptible(&edge
->new_channel_event
);
1157 schedule_work(&edge
->state_work
);
1161 * This per edge worker scans smem for any new channels and register these. It
1162 * then scans all registered channels for state changes that should be handled
1163 * by creating or destroying smd client devices for the registered channels.
1165 * LOCKING: edge->channels_lock only needs to cover the list operations, as the
1166 * worker is killed before any channels are deallocated
1168 static void qcom_channel_state_worker(struct work_struct
*work
)
1170 struct qcom_smd_channel
*channel
;
1171 struct qcom_smd_edge
*edge
= container_of(work
,
1172 struct qcom_smd_edge
,
1174 struct rpmsg_channel_info chinfo
;
1175 unsigned remote_state
;
1176 unsigned long flags
;
1179 * Register a device for any closed channel where the remote processor
1180 * is showing interest in opening the channel.
1182 spin_lock_irqsave(&edge
->channels_lock
, flags
);
1183 list_for_each_entry(channel
, &edge
->channels
, list
) {
1184 if (channel
->state
!= SMD_CHANNEL_CLOSED
)
1187 remote_state
= GET_RX_CHANNEL_INFO(channel
, state
);
1188 if (remote_state
!= SMD_CHANNEL_OPENING
&&
1189 remote_state
!= SMD_CHANNEL_OPENED
)
1192 if (channel
->registered
)
1195 spin_unlock_irqrestore(&edge
->channels_lock
, flags
);
1196 qcom_smd_create_device(channel
);
1197 channel
->registered
= true;
1198 spin_lock_irqsave(&edge
->channels_lock
, flags
);
1200 channel
->registered
= true;
1204 * Unregister the device for any channel that is opened where the
1205 * remote processor is closing the channel.
1207 list_for_each_entry(channel
, &edge
->channels
, list
) {
1208 if (channel
->state
!= SMD_CHANNEL_OPENING
&&
1209 channel
->state
!= SMD_CHANNEL_OPENED
)
1212 remote_state
= GET_RX_CHANNEL_INFO(channel
, state
);
1213 if (remote_state
== SMD_CHANNEL_OPENING
||
1214 remote_state
== SMD_CHANNEL_OPENED
)
1217 spin_unlock_irqrestore(&edge
->channels_lock
, flags
);
1219 strncpy(chinfo
.name
, channel
->name
, sizeof(chinfo
.name
));
1220 chinfo
.src
= RPMSG_ADDR_ANY
;
1221 chinfo
.dst
= RPMSG_ADDR_ANY
;
1222 rpmsg_unregister_device(&edge
->dev
, &chinfo
);
1223 channel
->registered
= false;
1224 spin_lock_irqsave(&edge
->channels_lock
, flags
);
1226 spin_unlock_irqrestore(&edge
->channels_lock
, flags
);
1230 * Parses an of_node describing an edge.
1232 static int qcom_smd_parse_edge(struct device
*dev
,
1233 struct device_node
*node
,
1234 struct qcom_smd_edge
*edge
)
1236 struct device_node
*syscon_np
;
1241 INIT_LIST_HEAD(&edge
->channels
);
1242 spin_lock_init(&edge
->channels_lock
);
1244 INIT_WORK(&edge
->scan_work
, qcom_channel_scan_worker
);
1245 INIT_WORK(&edge
->state_work
, qcom_channel_state_worker
);
1247 edge
->of_node
= of_node_get(node
);
1249 key
= "qcom,smd-edge";
1250 ret
= of_property_read_u32(node
, key
, &edge
->edge_id
);
1252 dev_err(dev
, "edge missing %s property\n", key
);
1256 edge
->remote_pid
= QCOM_SMEM_HOST_ANY
;
1257 key
= "qcom,remote-pid";
1258 of_property_read_u32(node
, key
, &edge
->remote_pid
);
1260 syscon_np
= of_parse_phandle(node
, "qcom,ipc", 0);
1262 dev_err(dev
, "no qcom,ipc node\n");
1266 edge
->ipc_regmap
= syscon_node_to_regmap(syscon_np
);
1267 if (IS_ERR(edge
->ipc_regmap
))
1268 return PTR_ERR(edge
->ipc_regmap
);
1271 ret
= of_property_read_u32_index(node
, key
, 1, &edge
->ipc_offset
);
1273 dev_err(dev
, "no offset in %s\n", key
);
1277 ret
= of_property_read_u32_index(node
, key
, 2, &edge
->ipc_bit
);
1279 dev_err(dev
, "no bit in %s\n", key
);
1283 ret
= of_property_read_string(node
, "label", &edge
->name
);
1285 edge
->name
= node
->name
;
1287 irq
= irq_of_parse_and_map(node
, 0);
1289 dev_err(dev
, "required smd interrupt missing\n");
1293 ret
= devm_request_irq(dev
, irq
,
1294 qcom_smd_edge_intr
, IRQF_TRIGGER_RISING
,
1297 dev_err(dev
, "failed to request smd irq\n");
1307 * Release function for an edge.
1308 * Reset the state of each associated channel and free the edge context.
1310 static void qcom_smd_edge_release(struct device
*dev
)
1312 struct qcom_smd_channel
*channel
;
1313 struct qcom_smd_edge
*edge
= to_smd_edge(dev
);
1315 list_for_each_entry(channel
, &edge
->channels
, list
) {
1316 SET_RX_CHANNEL_INFO(channel
, state
, SMD_CHANNEL_CLOSED
);
1317 SET_RX_CHANNEL_INFO(channel
, head
, 0);
1318 SET_RX_CHANNEL_INFO(channel
, tail
, 0);
1324 static ssize_t
rpmsg_name_show(struct device
*dev
,
1325 struct device_attribute
*attr
, char *buf
)
1327 struct qcom_smd_edge
*edge
= to_smd_edge(dev
);
1329 return sprintf(buf
, "%s\n", edge
->name
);
1331 static DEVICE_ATTR_RO(rpmsg_name
);
1333 static struct attribute
*qcom_smd_edge_attrs
[] = {
1334 &dev_attr_rpmsg_name
.attr
,
1337 ATTRIBUTE_GROUPS(qcom_smd_edge
);
1340 * qcom_smd_register_edge() - register an edge based on an device_node
1341 * @parent: parent device for the edge
1342 * @node: device_node describing the edge
1344 * Returns an edge reference, or negative ERR_PTR() on failure.
1346 struct qcom_smd_edge
*qcom_smd_register_edge(struct device
*parent
,
1347 struct device_node
*node
)
1349 struct qcom_smd_edge
*edge
;
1352 edge
= kzalloc(sizeof(*edge
), GFP_KERNEL
);
1354 return ERR_PTR(-ENOMEM
);
1356 init_waitqueue_head(&edge
->new_channel_event
);
1358 edge
->dev
.parent
= parent
;
1359 edge
->dev
.release
= qcom_smd_edge_release
;
1360 edge
->dev
.groups
= qcom_smd_edge_groups
;
1361 dev_set_name(&edge
->dev
, "%s:%s", dev_name(parent
), node
->name
);
1362 ret
= device_register(&edge
->dev
);
1364 pr_err("failed to register smd edge\n");
1365 return ERR_PTR(ret
);
1368 ret
= qcom_smd_parse_edge(&edge
->dev
, node
, edge
);
1370 dev_err(&edge
->dev
, "failed to parse smd edge\n");
1371 goto unregister_dev
;
1374 ret
= qcom_smd_create_chrdev(edge
);
1376 dev_err(&edge
->dev
, "failed to register chrdev for edge\n");
1377 goto unregister_dev
;
1380 schedule_work(&edge
->scan_work
);
1385 put_device(&edge
->dev
);
1386 return ERR_PTR(ret
);
1388 EXPORT_SYMBOL(qcom_smd_register_edge
);
1390 static int qcom_smd_remove_device(struct device
*dev
, void *data
)
1392 device_unregister(dev
);
1398 * qcom_smd_unregister_edge() - release an edge and its children
1399 * @edge: edge reference acquired from qcom_smd_register_edge
1401 int qcom_smd_unregister_edge(struct qcom_smd_edge
*edge
)
1405 disable_irq(edge
->irq
);
1406 cancel_work_sync(&edge
->scan_work
);
1407 cancel_work_sync(&edge
->state_work
);
1409 ret
= device_for_each_child(&edge
->dev
, NULL
, qcom_smd_remove_device
);
1411 dev_warn(&edge
->dev
, "can't remove smd device: %d\n", ret
);
1413 device_unregister(&edge
->dev
);
1417 EXPORT_SYMBOL(qcom_smd_unregister_edge
);
1419 static int qcom_smd_probe(struct platform_device
*pdev
)
1421 struct device_node
*node
;
1425 p
= qcom_smem_get(QCOM_SMEM_HOST_ANY
, smem_items
[0].alloc_tbl_id
, NULL
);
1426 if (PTR_ERR(p
) == -EPROBE_DEFER
)
1429 for_each_available_child_of_node(pdev
->dev
.of_node
, node
)
1430 qcom_smd_register_edge(&pdev
->dev
, node
);
1435 static int qcom_smd_remove_edge(struct device
*dev
, void *data
)
1437 struct qcom_smd_edge
*edge
= to_smd_edge(dev
);
1439 return qcom_smd_unregister_edge(edge
);
1443 * Shut down all smd clients by making sure that each edge stops processing
1444 * events and scanning for new channels, then call destroy on the devices.
1446 static int qcom_smd_remove(struct platform_device
*pdev
)
1450 ret
= device_for_each_child(&pdev
->dev
, NULL
, qcom_smd_remove_edge
);
1452 dev_warn(&pdev
->dev
, "can't remove smd device: %d\n", ret
);
1457 static const struct of_device_id qcom_smd_of_match
[] = {
1458 { .compatible
= "qcom,smd" },
1461 MODULE_DEVICE_TABLE(of
, qcom_smd_of_match
);
1463 static struct platform_driver qcom_smd_driver
= {
1464 .probe
= qcom_smd_probe
,
1465 .remove
= qcom_smd_remove
,
1468 .of_match_table
= qcom_smd_of_match
,
1472 static int __init
qcom_smd_init(void)
1474 return platform_driver_register(&qcom_smd_driver
);
1476 subsys_initcall(qcom_smd_init
);
1478 static void __exit
qcom_smd_exit(void)
1480 platform_driver_unregister(&qcom_smd_driver
);
1482 module_exit(qcom_smd_exit
);
1484 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
1485 MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
1486 MODULE_LICENSE("GPL v2");