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/smd.h>
26 #include <linux/soc/qcom/smem.h>
27 #include <linux/wait.h>
30 * The Qualcomm Shared Memory communication solution provides point-to-point
31 * channels for clients to send and receive streaming or packet based data.
33 * Each channel consists of a control item (channel info) and a ring buffer
34 * pair. The channel info carry information related to channel state, flow
35 * control and the offsets within the ring buffer.
37 * All allocated channels are listed in an allocation table, identifying the
38 * pair of items by name, type and remote processor.
40 * Upon creating a new channel the remote processor allocates channel info and
41 * ring buffer items from the smem heap and populate the allocation table. An
42 * interrupt is sent to the other end of the channel and a scan for new
43 * channels should be done. A channel never goes away, it will only change
46 * The remote processor signals it intent for bring up the communication
47 * channel by setting the state of its end of the channel to "opening" and
48 * sends out an interrupt. We detect this change and register a smd device to
49 * consume the channel. Upon finding a consumer we finish the handshake and the
52 * Upon closing a channel, the remote processor will update the state of its
53 * end of the channel and signal us, we will then unregister any attached
54 * device and close our end of the channel.
56 * Devices attached to a channel can use the qcom_smd_send function to push
57 * data to the channel, this is done by copying the data into the tx ring
58 * buffer, updating the pointers in the channel info and signaling the remote
61 * The remote processor does the equivalent when it transfer data and upon
62 * receiving the interrupt we check the channel info for new data and delivers
63 * this to the attached device. If the device is not ready to receive the data
64 * we leave it in the ring buffer for now.
67 struct smd_channel_info
;
68 struct smd_channel_info_word
;
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 * @smd: handle to qcom_smd
97 * @of_node: of_node handle for information related to this edge
98 * @edge_id: identifier of this edge
99 * @remote_pid: identifier of remote processor
100 * @irq: interrupt for signals on this edge
101 * @ipc_regmap: regmap handle holding the outgoing ipc register
102 * @ipc_offset: offset within @ipc_regmap of the register for ipc
103 * @ipc_bit: bit in the register at @ipc_offset of @ipc_regmap
104 * @channels: list of all channels detected on this edge
105 * @channels_lock: guard for modifications of @channels
106 * @allocated: array of bitmaps representing already allocated channels
107 * @need_rescan: flag that the @work needs to scan smem for new channels
108 * @smem_available: last available amount of smem triggering a channel scan
109 * @work: work item for edge house keeping
111 struct qcom_smd_edge
{
112 struct qcom_smd
*smd
;
113 struct device_node
*of_node
;
119 struct regmap
*ipc_regmap
;
123 struct list_head channels
;
124 spinlock_t channels_lock
;
126 DECLARE_BITMAP(allocated
[SMD_ALLOC_TBL_COUNT
], SMD_ALLOC_TBL_SIZE
);
129 unsigned smem_available
;
131 struct work_struct work
;
135 * SMD channel states.
137 enum smd_channel_state
{
141 SMD_CHANNEL_FLUSHING
,
144 SMD_CHANNEL_RESET_OPENING
148 * struct qcom_smd_channel - smd channel struct
149 * @edge: qcom_smd_edge this channel is living on
150 * @qsdev: reference to a associated smd client device
151 * @name: name of the channel
152 * @state: local state of the channel
153 * @remote_state: remote state of the channel
154 * @tx_info: byte aligned outgoing channel info
155 * @rx_info: byte aligned incoming channel info
156 * @tx_info_word: word aligned outgoing channel info
157 * @rx_info_word: word aligned incoming channel info
158 * @tx_lock: lock to make writes to the channel mutually exclusive
159 * @fblockread_event: wakeup event tied to tx fBLOCKREADINTR
160 * @tx_fifo: pointer to the outgoing ring buffer
161 * @rx_fifo: pointer to the incoming ring buffer
162 * @fifo_size: size of each ring buffer
163 * @bounce_buffer: bounce buffer for reading wrapped packets
164 * @cb: callback function registered for this channel
165 * @recv_lock: guard for rx info modifications and cb pointer
166 * @pkt_size: size of the currently handled packet
167 * @list: lite entry for @channels in qcom_smd_edge
169 struct qcom_smd_channel
{
170 struct qcom_smd_edge
*edge
;
172 struct qcom_smd_device
*qsdev
;
175 enum smd_channel_state state
;
176 enum smd_channel_state remote_state
;
178 struct smd_channel_info
*tx_info
;
179 struct smd_channel_info
*rx_info
;
181 struct smd_channel_info_word
*tx_info_word
;
182 struct smd_channel_info_word
*rx_info_word
;
184 struct mutex tx_lock
;
185 wait_queue_head_t fblockread_event
;
192 int (*cb
)(struct qcom_smd_device
*, const void *, size_t);
194 spinlock_t recv_lock
;
198 struct list_head list
;
202 * struct qcom_smd - smd struct
203 * @dev: device struct
204 * @num_edges: number of entries in @edges
205 * @edges: array of edges to be handled
211 struct qcom_smd_edge edges
[0];
215 * Format of the smd_info smem items, for byte aligned channels.
217 struct smd_channel_info
{
232 * Format of the smd_info smem items, for word aligned channels.
234 struct smd_channel_info_word
{
248 #define GET_RX_CHANNEL_INFO(channel, param) \
249 (channel->rx_info_word ? \
250 channel->rx_info_word->param : \
251 channel->rx_info->param)
253 #define SET_RX_CHANNEL_INFO(channel, param, value) \
254 (channel->rx_info_word ? \
255 (channel->rx_info_word->param = value) : \
256 (channel->rx_info->param = value))
258 #define GET_TX_CHANNEL_INFO(channel, param) \
259 (channel->tx_info_word ? \
260 channel->tx_info_word->param : \
261 channel->tx_info->param)
263 #define SET_TX_CHANNEL_INFO(channel, param, value) \
264 (channel->tx_info_word ? \
265 (channel->tx_info_word->param = value) : \
266 (channel->tx_info->param = value))
269 * struct qcom_smd_alloc_entry - channel allocation entry
270 * @name: channel name
271 * @cid: channel index
272 * @flags: channel flags and edge id
273 * @ref_count: reference count of the channel
275 struct qcom_smd_alloc_entry
{
282 #define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff
283 #define SMD_CHANNEL_FLAGS_STREAM BIT(8)
284 #define SMD_CHANNEL_FLAGS_PACKET BIT(9)
287 * Each smd packet contains a 20 byte header, with the first 4 being the length
290 #define SMD_PACKET_HEADER_LEN 20
293 * Signal the remote processor associated with 'channel'.
295 static void qcom_smd_signal_channel(struct qcom_smd_channel
*channel
)
297 struct qcom_smd_edge
*edge
= channel
->edge
;
299 regmap_write(edge
->ipc_regmap
, edge
->ipc_offset
, BIT(edge
->ipc_bit
));
303 * Initialize the tx channel info
305 static void qcom_smd_channel_reset(struct qcom_smd_channel
*channel
)
307 SET_TX_CHANNEL_INFO(channel
, state
, SMD_CHANNEL_CLOSED
);
308 SET_TX_CHANNEL_INFO(channel
, fDSR
, 0);
309 SET_TX_CHANNEL_INFO(channel
, fCTS
, 0);
310 SET_TX_CHANNEL_INFO(channel
, fCD
, 0);
311 SET_TX_CHANNEL_INFO(channel
, fRI
, 0);
312 SET_TX_CHANNEL_INFO(channel
, fHEAD
, 0);
313 SET_TX_CHANNEL_INFO(channel
, fTAIL
, 0);
314 SET_TX_CHANNEL_INFO(channel
, fSTATE
, 1);
315 SET_TX_CHANNEL_INFO(channel
, fBLOCKREADINTR
, 1);
316 SET_TX_CHANNEL_INFO(channel
, head
, 0);
317 SET_TX_CHANNEL_INFO(channel
, tail
, 0);
319 qcom_smd_signal_channel(channel
);
321 channel
->state
= SMD_CHANNEL_CLOSED
;
322 channel
->pkt_size
= 0;
326 * Calculate the amount of data available in the rx fifo
328 static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel
*channel
)
333 head
= GET_RX_CHANNEL_INFO(channel
, head
);
334 tail
= GET_RX_CHANNEL_INFO(channel
, tail
);
336 return (head
- tail
) & (channel
->fifo_size
- 1);
340 * Set tx channel state and inform the remote processor
342 static void qcom_smd_channel_set_state(struct qcom_smd_channel
*channel
,
345 struct qcom_smd_edge
*edge
= channel
->edge
;
346 bool is_open
= state
== SMD_CHANNEL_OPENED
;
348 if (channel
->state
== state
)
351 dev_dbg(edge
->smd
->dev
, "set_state(%s, %d)\n", channel
->name
, state
);
353 SET_TX_CHANNEL_INFO(channel
, fDSR
, is_open
);
354 SET_TX_CHANNEL_INFO(channel
, fCTS
, is_open
);
355 SET_TX_CHANNEL_INFO(channel
, fCD
, is_open
);
357 SET_TX_CHANNEL_INFO(channel
, state
, state
);
358 SET_TX_CHANNEL_INFO(channel
, fSTATE
, 1);
360 channel
->state
= state
;
361 qcom_smd_signal_channel(channel
);
365 * Copy count bytes of data using 32bit accesses, if that's required.
367 static void smd_copy_to_fifo(void __iomem
*_dst
,
372 u32
*dst
= (u32
*)_dst
;
373 u32
*src
= (u32
*)_src
;
376 count
/= sizeof(u32
);
378 writel_relaxed(*src
++, dst
++);
380 memcpy_toio(_dst
, _src
, count
);
385 * Copy count bytes of data using 32bit accesses, if that is required.
387 static void smd_copy_from_fifo(void *_dst
,
388 const void __iomem
*_src
,
392 u32
*dst
= (u32
*)_dst
;
393 u32
*src
= (u32
*)_src
;
396 count
/= sizeof(u32
);
398 *dst
++ = readl_relaxed(src
++);
400 memcpy_fromio(_dst
, _src
, count
);
405 * Read count bytes of data from the rx fifo into buf, but don't advance the
408 static size_t qcom_smd_channel_peek(struct qcom_smd_channel
*channel
,
409 void *buf
, size_t count
)
415 word_aligned
= channel
->rx_info_word
!= NULL
;
416 tail
= GET_RX_CHANNEL_INFO(channel
, tail
);
418 len
= min_t(size_t, count
, channel
->fifo_size
- tail
);
420 smd_copy_from_fifo(buf
,
421 channel
->rx_fifo
+ tail
,
427 smd_copy_from_fifo(buf
+ len
,
437 * Advance the rx tail by count bytes.
439 static void qcom_smd_channel_advance(struct qcom_smd_channel
*channel
,
444 tail
= GET_RX_CHANNEL_INFO(channel
, tail
);
446 tail
&= (channel
->fifo_size
- 1);
447 SET_RX_CHANNEL_INFO(channel
, tail
, tail
);
451 * Read out a single packet from the rx fifo and deliver it to the device
453 static int qcom_smd_channel_recv_single(struct qcom_smd_channel
*channel
)
455 struct qcom_smd_device
*qsdev
= channel
->qsdev
;
464 tail
= GET_RX_CHANNEL_INFO(channel
, tail
);
466 /* Use bounce buffer if the data wraps */
467 if (tail
+ channel
->pkt_size
>= channel
->fifo_size
) {
468 ptr
= channel
->bounce_buffer
;
469 len
= qcom_smd_channel_peek(channel
, ptr
, channel
->pkt_size
);
471 ptr
= channel
->rx_fifo
+ tail
;
472 len
= channel
->pkt_size
;
475 ret
= channel
->cb(qsdev
, ptr
, len
);
479 /* Only forward the tail if the client consumed the data */
480 qcom_smd_channel_advance(channel
, len
);
482 channel
->pkt_size
= 0;
488 * Per channel interrupt handling
490 static bool qcom_smd_channel_intr(struct qcom_smd_channel
*channel
)
492 bool need_state_scan
= false;
498 /* Handle state changes */
499 remote_state
= GET_RX_CHANNEL_INFO(channel
, state
);
500 if (remote_state
!= channel
->remote_state
) {
501 channel
->remote_state
= remote_state
;
502 need_state_scan
= true;
504 /* Indicate that we have seen any state change */
505 SET_RX_CHANNEL_INFO(channel
, fSTATE
, 0);
507 /* Signal waiting qcom_smd_send() about the interrupt */
508 if (!GET_TX_CHANNEL_INFO(channel
, fBLOCKREADINTR
))
509 wake_up_interruptible(&channel
->fblockread_event
);
511 /* Don't consume any data until we've opened the channel */
512 if (channel
->state
!= SMD_CHANNEL_OPENED
)
515 /* Indicate that we've seen the new data */
516 SET_RX_CHANNEL_INFO(channel
, fHEAD
, 0);
520 avail
= qcom_smd_channel_get_rx_avail(channel
);
522 if (!channel
->pkt_size
&& avail
>= SMD_PACKET_HEADER_LEN
) {
523 qcom_smd_channel_peek(channel
, &pktlen
, sizeof(pktlen
));
524 qcom_smd_channel_advance(channel
, SMD_PACKET_HEADER_LEN
);
525 channel
->pkt_size
= pktlen
;
526 } else if (channel
->pkt_size
&& avail
>= channel
->pkt_size
) {
527 ret
= qcom_smd_channel_recv_single(channel
);
535 /* Indicate that we have seen and updated tail */
536 SET_RX_CHANNEL_INFO(channel
, fTAIL
, 1);
538 /* Signal the remote that we've consumed the data (if requested) */
539 if (!GET_RX_CHANNEL_INFO(channel
, fBLOCKREADINTR
)) {
540 /* Ensure ordering of channel info updates */
543 qcom_smd_signal_channel(channel
);
547 return need_state_scan
;
551 * The edge interrupts are triggered by the remote processor on state changes,
552 * channel info updates or when new channels are created.
554 static irqreturn_t
qcom_smd_edge_intr(int irq
, void *data
)
556 struct qcom_smd_edge
*edge
= data
;
557 struct qcom_smd_channel
*channel
;
559 bool kick_worker
= false;
562 * Handle state changes or data on each of the channels on this edge
564 spin_lock(&edge
->channels_lock
);
565 list_for_each_entry(channel
, &edge
->channels
, list
) {
566 spin_lock(&channel
->recv_lock
);
567 kick_worker
|= qcom_smd_channel_intr(channel
);
568 spin_unlock(&channel
->recv_lock
);
570 spin_unlock(&edge
->channels_lock
);
573 * Creating a new channel requires allocating an smem entry, so we only
574 * have to scan if the amount of available space in smem have changed
577 available
= qcom_smem_get_free_space(edge
->remote_pid
);
578 if (available
!= edge
->smem_available
) {
579 edge
->smem_available
= available
;
580 edge
->need_rescan
= true;
585 schedule_work(&edge
->work
);
591 * Delivers any outstanding packets in the rx fifo, can be used after probe of
592 * the clients to deliver any packets that wasn't delivered before the client
595 static void qcom_smd_channel_resume(struct qcom_smd_channel
*channel
)
599 spin_lock_irqsave(&channel
->recv_lock
, flags
);
600 qcom_smd_channel_intr(channel
);
601 spin_unlock_irqrestore(&channel
->recv_lock
, flags
);
605 * Calculate how much space is available in the tx fifo.
607 static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel
*channel
)
611 unsigned mask
= channel
->fifo_size
- 1;
613 head
= GET_TX_CHANNEL_INFO(channel
, head
);
614 tail
= GET_TX_CHANNEL_INFO(channel
, tail
);
616 return mask
- ((head
- tail
) & mask
);
620 * Write count bytes of data into channel, possibly wrapping in the ring buffer
622 static int qcom_smd_write_fifo(struct qcom_smd_channel
*channel
,
630 word_aligned
= channel
->tx_info_word
!= NULL
;
631 head
= GET_TX_CHANNEL_INFO(channel
, head
);
633 len
= min_t(size_t, count
, channel
->fifo_size
- head
);
635 smd_copy_to_fifo(channel
->tx_fifo
+ head
,
642 smd_copy_to_fifo(channel
->tx_fifo
,
649 head
&= (channel
->fifo_size
- 1);
650 SET_TX_CHANNEL_INFO(channel
, head
, head
);
656 * qcom_smd_send - write data to smd channel
657 * @channel: channel handle
658 * @data: buffer of data to write
659 * @len: number of bytes to write
661 * This is a blocking write of len bytes into the channel's tx ring buffer and
662 * signal the remote end. It will sleep until there is enough space available
663 * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
666 int qcom_smd_send(struct qcom_smd_channel
*channel
, const void *data
, int len
)
669 int tlen
= sizeof(hdr
) + len
;
672 /* Word aligned channels only accept word size aligned data */
673 if (channel
->rx_info_word
!= NULL
&& len
% 4)
676 ret
= mutex_lock_interruptible(&channel
->tx_lock
);
680 while (qcom_smd_get_tx_avail(channel
) < tlen
) {
681 if (channel
->state
!= SMD_CHANNEL_OPENED
) {
686 SET_TX_CHANNEL_INFO(channel
, fBLOCKREADINTR
, 0);
688 ret
= wait_event_interruptible(channel
->fblockread_event
,
689 qcom_smd_get_tx_avail(channel
) >= tlen
||
690 channel
->state
!= SMD_CHANNEL_OPENED
);
694 SET_TX_CHANNEL_INFO(channel
, fBLOCKREADINTR
, 1);
697 SET_TX_CHANNEL_INFO(channel
, fTAIL
, 0);
699 qcom_smd_write_fifo(channel
, hdr
, sizeof(hdr
));
700 qcom_smd_write_fifo(channel
, data
, len
);
702 SET_TX_CHANNEL_INFO(channel
, fHEAD
, 1);
704 /* Ensure ordering of channel info updates */
707 qcom_smd_signal_channel(channel
);
710 mutex_unlock(&channel
->tx_lock
);
714 EXPORT_SYMBOL(qcom_smd_send
);
716 static struct qcom_smd_device
*to_smd_device(struct device
*dev
)
718 return container_of(dev
, struct qcom_smd_device
, dev
);
721 static struct qcom_smd_driver
*to_smd_driver(struct device
*dev
)
723 struct qcom_smd_device
*qsdev
= to_smd_device(dev
);
725 return container_of(qsdev
->dev
.driver
, struct qcom_smd_driver
, driver
);
728 static int qcom_smd_dev_match(struct device
*dev
, struct device_driver
*drv
)
730 struct qcom_smd_device
*qsdev
= to_smd_device(dev
);
731 struct qcom_smd_driver
*qsdrv
= container_of(drv
, struct qcom_smd_driver
, driver
);
732 const struct qcom_smd_id
*match
= qsdrv
->smd_match_table
;
733 const char *name
= qsdev
->channel
->name
;
736 while (match
->name
[0]) {
737 if (!strcmp(match
->name
, name
))
743 return of_driver_match_device(dev
, drv
);
747 * Probe the smd client.
749 * The remote side have indicated that it want the channel to be opened, so
750 * complete the state handshake and probe our client driver.
752 static int qcom_smd_dev_probe(struct device
*dev
)
754 struct qcom_smd_device
*qsdev
= to_smd_device(dev
);
755 struct qcom_smd_driver
*qsdrv
= to_smd_driver(dev
);
756 struct qcom_smd_channel
*channel
= qsdev
->channel
;
761 * Packets are maximum 4k, but reduce if the fifo is smaller
763 bb_size
= min(channel
->fifo_size
, SZ_4K
);
764 channel
->bounce_buffer
= kmalloc(bb_size
, GFP_KERNEL
);
765 if (!channel
->bounce_buffer
)
768 channel
->cb
= qsdrv
->callback
;
770 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_OPENING
);
772 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_OPENED
);
774 ret
= qsdrv
->probe(qsdev
);
778 qcom_smd_channel_resume(channel
);
783 dev_err(&qsdev
->dev
, "probe failed\n");
786 kfree(channel
->bounce_buffer
);
787 channel
->bounce_buffer
= NULL
;
789 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_CLOSED
);
794 * Remove the smd client.
796 * The channel is going away, for some reason, so remove the smd client and
797 * reset the channel state.
799 static int qcom_smd_dev_remove(struct device
*dev
)
801 struct qcom_smd_device
*qsdev
= to_smd_device(dev
);
802 struct qcom_smd_driver
*qsdrv
= to_smd_driver(dev
);
803 struct qcom_smd_channel
*channel
= qsdev
->channel
;
806 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_CLOSING
);
809 * Make sure we don't race with the code receiving data.
811 spin_lock_irqsave(&channel
->recv_lock
, flags
);
813 spin_unlock_irqrestore(&channel
->recv_lock
, flags
);
815 /* Wake up any sleepers in qcom_smd_send() */
816 wake_up_interruptible(&channel
->fblockread_event
);
819 * We expect that the client might block in remove() waiting for any
820 * outstanding calls to qcom_smd_send() to wake up and finish.
823 qsdrv
->remove(qsdev
);
826 * The client is now gone, cleanup and reset the channel state.
828 channel
->qsdev
= NULL
;
829 kfree(channel
->bounce_buffer
);
830 channel
->bounce_buffer
= NULL
;
832 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_CLOSED
);
834 qcom_smd_channel_reset(channel
);
839 static struct bus_type qcom_smd_bus
= {
841 .match
= qcom_smd_dev_match
,
842 .probe
= qcom_smd_dev_probe
,
843 .remove
= qcom_smd_dev_remove
,
847 * Release function for the qcom_smd_device object.
849 static void qcom_smd_release_device(struct device
*dev
)
851 struct qcom_smd_device
*qsdev
= to_smd_device(dev
);
857 * Finds the device_node for the smd child interested in this channel.
859 static struct device_node
*qcom_smd_match_channel(struct device_node
*edge_node
,
862 struct device_node
*child
;
867 for_each_available_child_of_node(edge_node
, child
) {
868 key
= "qcom,smd-channels";
869 ret
= of_property_read_string(child
, key
, &name
);
875 if (strcmp(name
, channel
) == 0)
883 * Create a smd client device for channel that is being opened.
885 static int qcom_smd_create_device(struct qcom_smd_channel
*channel
)
887 struct qcom_smd_device
*qsdev
;
888 struct qcom_smd_edge
*edge
= channel
->edge
;
889 struct device_node
*node
;
890 struct qcom_smd
*smd
= edge
->smd
;
896 dev_dbg(smd
->dev
, "registering '%s'\n", channel
->name
);
898 qsdev
= kzalloc(sizeof(*qsdev
), GFP_KERNEL
);
902 node
= qcom_smd_match_channel(edge
->of_node
, channel
->name
);
903 dev_set_name(&qsdev
->dev
, "%s.%s",
905 node
? node
->name
: channel
->name
);
907 qsdev
->dev
.parent
= smd
->dev
;
908 qsdev
->dev
.bus
= &qcom_smd_bus
;
909 qsdev
->dev
.release
= qcom_smd_release_device
;
910 qsdev
->dev
.of_node
= node
;
912 qsdev
->channel
= channel
;
914 channel
->qsdev
= qsdev
;
916 ret
= device_register(&qsdev
->dev
);
918 dev_err(smd
->dev
, "device_register failed: %d\n", ret
);
919 put_device(&qsdev
->dev
);
926 * Destroy a smd client device for a channel that's going away.
928 static void qcom_smd_destroy_device(struct qcom_smd_channel
*channel
)
932 BUG_ON(!channel
->qsdev
);
934 dev
= &channel
->qsdev
->dev
;
936 device_unregister(dev
);
937 of_node_put(dev
->of_node
);
942 * qcom_smd_driver_register - register a smd driver
943 * @qsdrv: qcom_smd_driver struct
945 int qcom_smd_driver_register(struct qcom_smd_driver
*qsdrv
)
947 qsdrv
->driver
.bus
= &qcom_smd_bus
;
948 return driver_register(&qsdrv
->driver
);
950 EXPORT_SYMBOL(qcom_smd_driver_register
);
953 * qcom_smd_driver_unregister - unregister a smd driver
954 * @qsdrv: qcom_smd_driver struct
956 void qcom_smd_driver_unregister(struct qcom_smd_driver
*qsdrv
)
958 driver_unregister(&qsdrv
->driver
);
960 EXPORT_SYMBOL(qcom_smd_driver_unregister
);
963 * Allocate the qcom_smd_channel object for a newly found smd channel,
964 * retrieving and validating the smem items involved.
966 static struct qcom_smd_channel
*qcom_smd_create_channel(struct qcom_smd_edge
*edge
,
967 unsigned smem_info_item
,
968 unsigned smem_fifo_item
,
971 struct qcom_smd_channel
*channel
;
972 struct qcom_smd
*smd
= edge
->smd
;
979 channel
= devm_kzalloc(smd
->dev
, sizeof(*channel
), GFP_KERNEL
);
981 return ERR_PTR(-ENOMEM
);
983 channel
->edge
= edge
;
984 channel
->name
= devm_kstrdup(smd
->dev
, name
, GFP_KERNEL
);
986 return ERR_PTR(-ENOMEM
);
988 mutex_init(&channel
->tx_lock
);
989 spin_lock_init(&channel
->recv_lock
);
990 init_waitqueue_head(&channel
->fblockread_event
);
992 info
= qcom_smem_get(edge
->remote_pid
, smem_info_item
, &info_size
);
995 goto free_name_and_channel
;
999 * Use the size of the item to figure out which channel info struct to
1002 if (info_size
== 2 * sizeof(struct smd_channel_info_word
)) {
1003 channel
->tx_info_word
= info
;
1004 channel
->rx_info_word
= info
+ sizeof(struct smd_channel_info_word
);
1005 } else if (info_size
== 2 * sizeof(struct smd_channel_info
)) {
1006 channel
->tx_info
= info
;
1007 channel
->rx_info
= info
+ sizeof(struct smd_channel_info
);
1010 "channel info of size %zu not supported\n", info_size
);
1012 goto free_name_and_channel
;
1015 fifo_base
= qcom_smem_get(edge
->remote_pid
, smem_fifo_item
, &fifo_size
);
1016 if (IS_ERR(fifo_base
)) {
1017 ret
= PTR_ERR(fifo_base
);
1018 goto free_name_and_channel
;
1021 /* The channel consist of a rx and tx fifo of equal size */
1024 dev_dbg(smd
->dev
, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1025 name
, info_size
, fifo_size
);
1027 channel
->tx_fifo
= fifo_base
;
1028 channel
->rx_fifo
= fifo_base
+ fifo_size
;
1029 channel
->fifo_size
= fifo_size
;
1031 qcom_smd_channel_reset(channel
);
1035 free_name_and_channel
:
1036 devm_kfree(smd
->dev
, channel
->name
);
1037 devm_kfree(smd
->dev
, channel
);
1039 return ERR_PTR(ret
);
1043 * Scans the allocation table for any newly allocated channels, calls
1044 * qcom_smd_create_channel() to create representations of these and add
1045 * them to the edge's list of channels.
1047 static void qcom_discover_channels(struct qcom_smd_edge
*edge
)
1049 struct qcom_smd_alloc_entry
*alloc_tbl
;
1050 struct qcom_smd_alloc_entry
*entry
;
1051 struct qcom_smd_channel
*channel
;
1052 struct qcom_smd
*smd
= edge
->smd
;
1053 unsigned long flags
;
1059 for (tbl
= 0; tbl
< SMD_ALLOC_TBL_COUNT
; tbl
++) {
1060 alloc_tbl
= qcom_smem_get(edge
->remote_pid
,
1061 smem_items
[tbl
].alloc_tbl_id
, NULL
);
1062 if (IS_ERR(alloc_tbl
))
1065 for (i
= 0; i
< SMD_ALLOC_TBL_SIZE
; i
++) {
1066 entry
= &alloc_tbl
[i
];
1067 if (test_bit(i
, edge
->allocated
[tbl
]))
1070 if (entry
->ref_count
== 0)
1073 if (!entry
->name
[0])
1076 if (!(entry
->flags
& SMD_CHANNEL_FLAGS_PACKET
))
1079 if ((entry
->flags
& SMD_CHANNEL_FLAGS_EDGE_MASK
) != edge
->edge_id
)
1082 info_id
= smem_items
[tbl
].info_base_id
+ entry
->cid
;
1083 fifo_id
= smem_items
[tbl
].fifo_base_id
+ entry
->cid
;
1085 channel
= qcom_smd_create_channel(edge
, info_id
, fifo_id
, entry
->name
);
1086 if (IS_ERR(channel
))
1089 spin_lock_irqsave(&edge
->channels_lock
, flags
);
1090 list_add(&channel
->list
, &edge
->channels
);
1091 spin_unlock_irqrestore(&edge
->channels_lock
, flags
);
1093 dev_dbg(smd
->dev
, "new channel found: '%s'\n", channel
->name
);
1094 set_bit(i
, edge
->allocated
[tbl
]);
1098 schedule_work(&edge
->work
);
1102 * This per edge worker scans smem for any new channels and register these. It
1103 * then scans all registered channels for state changes that should be handled
1104 * by creating or destroying smd client devices for the registered channels.
1106 * LOCKING: edge->channels_lock is not needed to be held during the traversal
1107 * of the channels list as it's done synchronously with the only writer.
1109 static void qcom_channel_state_worker(struct work_struct
*work
)
1111 struct qcom_smd_channel
*channel
;
1112 struct qcom_smd_edge
*edge
= container_of(work
,
1113 struct qcom_smd_edge
,
1115 unsigned remote_state
;
1118 * Rescan smem if we have reason to belive that there are new channels.
1120 if (edge
->need_rescan
) {
1121 edge
->need_rescan
= false;
1122 qcom_discover_channels(edge
);
1126 * Register a device for any closed channel where the remote processor
1127 * is showing interest in opening the channel.
1129 list_for_each_entry(channel
, &edge
->channels
, list
) {
1130 if (channel
->state
!= SMD_CHANNEL_CLOSED
)
1133 remote_state
= GET_RX_CHANNEL_INFO(channel
, state
);
1134 if (remote_state
!= SMD_CHANNEL_OPENING
&&
1135 remote_state
!= SMD_CHANNEL_OPENED
)
1138 qcom_smd_create_device(channel
);
1142 * Unregister the device for any channel that is opened where the
1143 * remote processor is closing the channel.
1145 list_for_each_entry(channel
, &edge
->channels
, list
) {
1146 if (channel
->state
!= SMD_CHANNEL_OPENING
&&
1147 channel
->state
!= SMD_CHANNEL_OPENED
)
1150 remote_state
= GET_RX_CHANNEL_INFO(channel
, state
);
1151 if (remote_state
== SMD_CHANNEL_OPENING
||
1152 remote_state
== SMD_CHANNEL_OPENED
)
1155 qcom_smd_destroy_device(channel
);
1160 * Parses an of_node describing an edge.
1162 static int qcom_smd_parse_edge(struct device
*dev
,
1163 struct device_node
*node
,
1164 struct qcom_smd_edge
*edge
)
1166 struct device_node
*syscon_np
;
1171 INIT_LIST_HEAD(&edge
->channels
);
1172 spin_lock_init(&edge
->channels_lock
);
1174 INIT_WORK(&edge
->work
, qcom_channel_state_worker
);
1176 edge
->of_node
= of_node_get(node
);
1178 irq
= irq_of_parse_and_map(node
, 0);
1180 dev_err(dev
, "required smd interrupt missing\n");
1184 ret
= devm_request_irq(dev
, irq
,
1185 qcom_smd_edge_intr
, IRQF_TRIGGER_RISING
,
1188 dev_err(dev
, "failed to request smd irq\n");
1194 key
= "qcom,smd-edge";
1195 ret
= of_property_read_u32(node
, key
, &edge
->edge_id
);
1197 dev_err(dev
, "edge missing %s property\n", key
);
1201 edge
->remote_pid
= QCOM_SMEM_HOST_ANY
;
1202 key
= "qcom,remote-pid";
1203 of_property_read_u32(node
, key
, &edge
->remote_pid
);
1205 syscon_np
= of_parse_phandle(node
, "qcom,ipc", 0);
1207 dev_err(dev
, "no qcom,ipc node\n");
1211 edge
->ipc_regmap
= syscon_node_to_regmap(syscon_np
);
1212 if (IS_ERR(edge
->ipc_regmap
))
1213 return PTR_ERR(edge
->ipc_regmap
);
1216 ret
= of_property_read_u32_index(node
, key
, 1, &edge
->ipc_offset
);
1218 dev_err(dev
, "no offset in %s\n", key
);
1222 ret
= of_property_read_u32_index(node
, key
, 2, &edge
->ipc_bit
);
1224 dev_err(dev
, "no bit in %s\n", key
);
1231 static int qcom_smd_probe(struct platform_device
*pdev
)
1233 struct qcom_smd_edge
*edge
;
1234 struct device_node
*node
;
1235 struct qcom_smd
*smd
;
1243 p
= qcom_smem_get(QCOM_SMEM_HOST_ANY
, smem_items
[0].alloc_tbl_id
, NULL
);
1244 if (PTR_ERR(p
) == -EPROBE_DEFER
)
1247 num_edges
= of_get_available_child_count(pdev
->dev
.of_node
);
1248 array_size
= sizeof(*smd
) + num_edges
* sizeof(struct qcom_smd_edge
);
1249 smd
= devm_kzalloc(&pdev
->dev
, array_size
, GFP_KERNEL
);
1252 smd
->dev
= &pdev
->dev
;
1254 smd
->num_edges
= num_edges
;
1255 for_each_available_child_of_node(pdev
->dev
.of_node
, node
) {
1256 edge
= &smd
->edges
[i
++];
1259 ret
= qcom_smd_parse_edge(&pdev
->dev
, node
, edge
);
1263 edge
->need_rescan
= true;
1264 schedule_work(&edge
->work
);
1267 platform_set_drvdata(pdev
, smd
);
1273 * Shut down all smd clients by making sure that each edge stops processing
1274 * events and scanning for new channels, then call destroy on the devices.
1276 static int qcom_smd_remove(struct platform_device
*pdev
)
1278 struct qcom_smd_channel
*channel
;
1279 struct qcom_smd_edge
*edge
;
1280 struct qcom_smd
*smd
= platform_get_drvdata(pdev
);
1283 for (i
= 0; i
< smd
->num_edges
; i
++) {
1284 edge
= &smd
->edges
[i
];
1286 disable_irq(edge
->irq
);
1287 cancel_work_sync(&edge
->work
);
1289 list_for_each_entry(channel
, &edge
->channels
, list
) {
1290 if (!channel
->qsdev
)
1293 qcom_smd_destroy_device(channel
);
1300 static const struct of_device_id qcom_smd_of_match
[] = {
1301 { .compatible
= "qcom,smd" },
1304 MODULE_DEVICE_TABLE(of
, qcom_smd_of_match
);
1306 static struct platform_driver qcom_smd_driver
= {
1307 .probe
= qcom_smd_probe
,
1308 .remove
= qcom_smd_remove
,
1311 .of_match_table
= qcom_smd_of_match
,
1315 static int __init
qcom_smd_init(void)
1319 ret
= bus_register(&qcom_smd_bus
);
1321 pr_err("failed to register smd bus: %d\n", ret
);
1325 return platform_driver_register(&qcom_smd_driver
);
1327 postcore_initcall(qcom_smd_init
);
1329 static void __exit
qcom_smd_exit(void)
1331 platform_driver_unregister(&qcom_smd_driver
);
1332 bus_unregister(&qcom_smd_bus
);
1334 module_exit(qcom_smd_exit
);
1336 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
1337 MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
1338 MODULE_LICENSE("GPL v2");