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_pair
;
69 struct smd_channel_info_word
;
70 struct smd_channel_info_word_pair
;
72 #define SMD_ALLOC_TBL_COUNT 2
73 #define SMD_ALLOC_TBL_SIZE 64
76 * This lists the various smem heap items relevant for the allocation table and
77 * smd channel entries.
80 unsigned alloc_tbl_id
;
81 unsigned info_base_id
;
82 unsigned fifo_base_id
;
83 } smem_items
[SMD_ALLOC_TBL_COUNT
] = {
97 * struct qcom_smd_edge - representing a remote processor
98 * @smd: handle to qcom_smd
99 * @of_node: of_node handle for information related to this edge
100 * @edge_id: identifier of this edge
101 * @remote_pid: identifier of remote processor
102 * @irq: interrupt for signals on this edge
103 * @ipc_regmap: regmap handle holding the outgoing ipc register
104 * @ipc_offset: offset within @ipc_regmap of the register for ipc
105 * @ipc_bit: bit in the register at @ipc_offset of @ipc_regmap
106 * @channels: list of all channels detected on this edge
107 * @channels_lock: guard for modifications of @channels
108 * @allocated: array of bitmaps representing already allocated channels
109 * @need_rescan: flag that the @work needs to scan smem for new channels
110 * @smem_available: last available amount of smem triggering a channel scan
111 * @work: work item for edge house keeping
113 struct qcom_smd_edge
{
114 struct qcom_smd
*smd
;
115 struct device_node
*of_node
;
121 struct regmap
*ipc_regmap
;
125 struct list_head channels
;
126 spinlock_t channels_lock
;
128 DECLARE_BITMAP(allocated
[SMD_ALLOC_TBL_COUNT
], SMD_ALLOC_TBL_SIZE
);
131 unsigned smem_available
;
133 struct work_struct work
;
137 * SMD channel states.
139 enum smd_channel_state
{
143 SMD_CHANNEL_FLUSHING
,
146 SMD_CHANNEL_RESET_OPENING
150 * struct qcom_smd_channel - smd channel struct
151 * @edge: qcom_smd_edge this channel is living on
152 * @qsdev: reference to a associated smd client device
153 * @name: name of the channel
154 * @state: local state of the channel
155 * @remote_state: remote state of the channel
156 * @info: byte aligned outgoing/incoming channel info
157 * @info_word: word aligned outgoing/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_pair
*info
;
179 struct smd_channel_info_word_pair
*info_word
;
181 struct mutex tx_lock
;
182 wait_queue_head_t fblockread_event
;
189 int (*cb
)(struct qcom_smd_device
*, const void *, size_t);
191 spinlock_t recv_lock
;
195 struct list_head list
;
199 * struct qcom_smd - smd struct
200 * @dev: device struct
201 * @num_edges: number of entries in @edges
202 * @edges: array of edges to be handled
208 struct qcom_smd_edge edges
[0];
212 * Format of the smd_info smem items, for byte aligned channels.
214 struct smd_channel_info
{
228 struct smd_channel_info_pair
{
229 struct smd_channel_info tx
;
230 struct smd_channel_info rx
;
234 * Format of the smd_info smem items, for word aligned channels.
236 struct smd_channel_info_word
{
250 struct smd_channel_info_word_pair
{
251 struct smd_channel_info_word tx
;
252 struct smd_channel_info_word rx
;
255 #define GET_RX_CHANNEL_INFO(channel, param) \
256 (channel->info_word ? \
257 channel->info_word->rx.param : \
258 channel->info->rx.param)
260 #define SET_RX_CHANNEL_INFO(channel, param, value) \
261 (channel->info_word ? \
262 (channel->info_word->rx.param = value) : \
263 (channel->info->rx.param = value))
265 #define GET_TX_CHANNEL_INFO(channel, param) \
266 (channel->info_word ? \
267 channel->info_word->tx.param : \
268 channel->info->tx.param)
270 #define SET_TX_CHANNEL_INFO(channel, param, value) \
271 (channel->info_word ? \
272 (channel->info_word->tx.param = value) : \
273 (channel->info->tx.param = value))
276 * struct qcom_smd_alloc_entry - channel allocation entry
277 * @name: channel name
278 * @cid: channel index
279 * @flags: channel flags and edge id
280 * @ref_count: reference count of the channel
282 struct qcom_smd_alloc_entry
{
289 #define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff
290 #define SMD_CHANNEL_FLAGS_STREAM BIT(8)
291 #define SMD_CHANNEL_FLAGS_PACKET BIT(9)
294 * Each smd packet contains a 20 byte header, with the first 4 being the length
297 #define SMD_PACKET_HEADER_LEN 20
300 * Signal the remote processor associated with 'channel'.
302 static void qcom_smd_signal_channel(struct qcom_smd_channel
*channel
)
304 struct qcom_smd_edge
*edge
= channel
->edge
;
306 regmap_write(edge
->ipc_regmap
, edge
->ipc_offset
, BIT(edge
->ipc_bit
));
310 * Initialize the tx channel info
312 static void qcom_smd_channel_reset(struct qcom_smd_channel
*channel
)
314 SET_TX_CHANNEL_INFO(channel
, state
, SMD_CHANNEL_CLOSED
);
315 SET_TX_CHANNEL_INFO(channel
, fDSR
, 0);
316 SET_TX_CHANNEL_INFO(channel
, fCTS
, 0);
317 SET_TX_CHANNEL_INFO(channel
, fCD
, 0);
318 SET_TX_CHANNEL_INFO(channel
, fRI
, 0);
319 SET_TX_CHANNEL_INFO(channel
, fHEAD
, 0);
320 SET_TX_CHANNEL_INFO(channel
, fTAIL
, 0);
321 SET_TX_CHANNEL_INFO(channel
, fSTATE
, 1);
322 SET_TX_CHANNEL_INFO(channel
, fBLOCKREADINTR
, 1);
323 SET_TX_CHANNEL_INFO(channel
, head
, 0);
324 SET_TX_CHANNEL_INFO(channel
, tail
, 0);
326 qcom_smd_signal_channel(channel
);
328 channel
->state
= SMD_CHANNEL_CLOSED
;
329 channel
->pkt_size
= 0;
333 * Calculate the amount of data available in the rx fifo
335 static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel
*channel
)
340 head
= GET_RX_CHANNEL_INFO(channel
, head
);
341 tail
= GET_RX_CHANNEL_INFO(channel
, tail
);
343 return (head
- tail
) & (channel
->fifo_size
- 1);
347 * Set tx channel state and inform the remote processor
349 static void qcom_smd_channel_set_state(struct qcom_smd_channel
*channel
,
352 struct qcom_smd_edge
*edge
= channel
->edge
;
353 bool is_open
= state
== SMD_CHANNEL_OPENED
;
355 if (channel
->state
== state
)
358 dev_dbg(edge
->smd
->dev
, "set_state(%s, %d)\n", channel
->name
, state
);
360 SET_TX_CHANNEL_INFO(channel
, fDSR
, is_open
);
361 SET_TX_CHANNEL_INFO(channel
, fCTS
, is_open
);
362 SET_TX_CHANNEL_INFO(channel
, fCD
, is_open
);
364 SET_TX_CHANNEL_INFO(channel
, state
, state
);
365 SET_TX_CHANNEL_INFO(channel
, fSTATE
, 1);
367 channel
->state
= state
;
368 qcom_smd_signal_channel(channel
);
372 * Copy count bytes of data using 32bit accesses, if that's required.
374 static void smd_copy_to_fifo(void __iomem
*dst
,
380 __iowrite32_copy(dst
, src
, count
/ sizeof(u32
));
382 memcpy_toio(dst
, src
, count
);
387 * Copy count bytes of data using 32bit accesses, if that is required.
389 static void smd_copy_from_fifo(void *_dst
,
390 const void __iomem
*_src
,
394 u32
*dst
= (u32
*)_dst
;
395 u32
*src
= (u32
*)_src
;
398 count
/= sizeof(u32
);
400 *dst
++ = readl_relaxed(src
++);
402 memcpy_fromio(_dst
, _src
, count
);
407 * Read count bytes of data from the rx fifo into buf, but don't advance the
410 static size_t qcom_smd_channel_peek(struct qcom_smd_channel
*channel
,
411 void *buf
, size_t count
)
417 word_aligned
= channel
->info_word
;
418 tail
= GET_RX_CHANNEL_INFO(channel
, tail
);
420 len
= min_t(size_t, count
, channel
->fifo_size
- tail
);
422 smd_copy_from_fifo(buf
,
423 channel
->rx_fifo
+ tail
,
429 smd_copy_from_fifo(buf
+ len
,
439 * Advance the rx tail by count bytes.
441 static void qcom_smd_channel_advance(struct qcom_smd_channel
*channel
,
446 tail
= GET_RX_CHANNEL_INFO(channel
, tail
);
448 tail
&= (channel
->fifo_size
- 1);
449 SET_RX_CHANNEL_INFO(channel
, tail
, tail
);
453 * Read out a single packet from the rx fifo and deliver it to the device
455 static int qcom_smd_channel_recv_single(struct qcom_smd_channel
*channel
)
457 struct qcom_smd_device
*qsdev
= channel
->qsdev
;
466 tail
= GET_RX_CHANNEL_INFO(channel
, tail
);
468 /* Use bounce buffer if the data wraps */
469 if (tail
+ channel
->pkt_size
>= channel
->fifo_size
) {
470 ptr
= channel
->bounce_buffer
;
471 len
= qcom_smd_channel_peek(channel
, ptr
, channel
->pkt_size
);
473 ptr
= channel
->rx_fifo
+ tail
;
474 len
= channel
->pkt_size
;
477 ret
= channel
->cb(qsdev
, ptr
, len
);
481 /* Only forward the tail if the client consumed the data */
482 qcom_smd_channel_advance(channel
, len
);
484 channel
->pkt_size
= 0;
490 * Per channel interrupt handling
492 static bool qcom_smd_channel_intr(struct qcom_smd_channel
*channel
)
494 bool need_state_scan
= false;
500 /* Handle state changes */
501 remote_state
= GET_RX_CHANNEL_INFO(channel
, state
);
502 if (remote_state
!= channel
->remote_state
) {
503 channel
->remote_state
= remote_state
;
504 need_state_scan
= true;
506 /* Indicate that we have seen any state change */
507 SET_RX_CHANNEL_INFO(channel
, fSTATE
, 0);
509 /* Signal waiting qcom_smd_send() about the interrupt */
510 if (!GET_TX_CHANNEL_INFO(channel
, fBLOCKREADINTR
))
511 wake_up_interruptible(&channel
->fblockread_event
);
513 /* Don't consume any data until we've opened the channel */
514 if (channel
->state
!= SMD_CHANNEL_OPENED
)
517 /* Indicate that we've seen the new data */
518 SET_RX_CHANNEL_INFO(channel
, fHEAD
, 0);
522 avail
= qcom_smd_channel_get_rx_avail(channel
);
524 if (!channel
->pkt_size
&& avail
>= SMD_PACKET_HEADER_LEN
) {
525 qcom_smd_channel_peek(channel
, &pktlen
, sizeof(pktlen
));
526 qcom_smd_channel_advance(channel
, SMD_PACKET_HEADER_LEN
);
527 channel
->pkt_size
= pktlen
;
528 } else if (channel
->pkt_size
&& avail
>= channel
->pkt_size
) {
529 ret
= qcom_smd_channel_recv_single(channel
);
537 /* Indicate that we have seen and updated tail */
538 SET_RX_CHANNEL_INFO(channel
, fTAIL
, 1);
540 /* Signal the remote that we've consumed the data (if requested) */
541 if (!GET_RX_CHANNEL_INFO(channel
, fBLOCKREADINTR
)) {
542 /* Ensure ordering of channel info updates */
545 qcom_smd_signal_channel(channel
);
549 return need_state_scan
;
553 * The edge interrupts are triggered by the remote processor on state changes,
554 * channel info updates or when new channels are created.
556 static irqreturn_t
qcom_smd_edge_intr(int irq
, void *data
)
558 struct qcom_smd_edge
*edge
= data
;
559 struct qcom_smd_channel
*channel
;
561 bool kick_worker
= false;
564 * Handle state changes or data on each of the channels on this edge
566 spin_lock(&edge
->channels_lock
);
567 list_for_each_entry(channel
, &edge
->channels
, list
) {
568 spin_lock(&channel
->recv_lock
);
569 kick_worker
|= qcom_smd_channel_intr(channel
);
570 spin_unlock(&channel
->recv_lock
);
572 spin_unlock(&edge
->channels_lock
);
575 * Creating a new channel requires allocating an smem entry, so we only
576 * have to scan if the amount of available space in smem have changed
579 available
= qcom_smem_get_free_space(edge
->remote_pid
);
580 if (available
!= edge
->smem_available
) {
581 edge
->smem_available
= available
;
582 edge
->need_rescan
= true;
587 schedule_work(&edge
->work
);
593 * Delivers any outstanding packets in the rx fifo, can be used after probe of
594 * the clients to deliver any packets that wasn't delivered before the client
597 static void qcom_smd_channel_resume(struct qcom_smd_channel
*channel
)
601 spin_lock_irqsave(&channel
->recv_lock
, flags
);
602 qcom_smd_channel_intr(channel
);
603 spin_unlock_irqrestore(&channel
->recv_lock
, flags
);
607 * Calculate how much space is available in the tx fifo.
609 static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel
*channel
)
613 unsigned mask
= channel
->fifo_size
- 1;
615 head
= GET_TX_CHANNEL_INFO(channel
, head
);
616 tail
= GET_TX_CHANNEL_INFO(channel
, tail
);
618 return mask
- ((head
- tail
) & mask
);
622 * Write count bytes of data into channel, possibly wrapping in the ring buffer
624 static int qcom_smd_write_fifo(struct qcom_smd_channel
*channel
,
632 word_aligned
= channel
->info_word
;
633 head
= GET_TX_CHANNEL_INFO(channel
, head
);
635 len
= min_t(size_t, count
, channel
->fifo_size
- head
);
637 smd_copy_to_fifo(channel
->tx_fifo
+ head
,
644 smd_copy_to_fifo(channel
->tx_fifo
,
651 head
&= (channel
->fifo_size
- 1);
652 SET_TX_CHANNEL_INFO(channel
, head
, head
);
658 * qcom_smd_send - write data to smd channel
659 * @channel: channel handle
660 * @data: buffer of data to write
661 * @len: number of bytes to write
663 * This is a blocking write of len bytes into the channel's tx ring buffer and
664 * signal the remote end. It will sleep until there is enough space available
665 * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
668 int qcom_smd_send(struct qcom_smd_channel
*channel
, const void *data
, int len
)
671 int tlen
= sizeof(hdr
) + len
;
674 /* Word aligned channels only accept word size aligned data */
675 if (channel
->info_word
&& len
% 4)
678 ret
= mutex_lock_interruptible(&channel
->tx_lock
);
682 while (qcom_smd_get_tx_avail(channel
) < tlen
) {
683 if (channel
->state
!= SMD_CHANNEL_OPENED
) {
688 SET_TX_CHANNEL_INFO(channel
, fBLOCKREADINTR
, 0);
690 ret
= wait_event_interruptible(channel
->fblockread_event
,
691 qcom_smd_get_tx_avail(channel
) >= tlen
||
692 channel
->state
!= SMD_CHANNEL_OPENED
);
696 SET_TX_CHANNEL_INFO(channel
, fBLOCKREADINTR
, 1);
699 SET_TX_CHANNEL_INFO(channel
, fTAIL
, 0);
701 qcom_smd_write_fifo(channel
, hdr
, sizeof(hdr
));
702 qcom_smd_write_fifo(channel
, data
, len
);
704 SET_TX_CHANNEL_INFO(channel
, fHEAD
, 1);
706 /* Ensure ordering of channel info updates */
709 qcom_smd_signal_channel(channel
);
712 mutex_unlock(&channel
->tx_lock
);
716 EXPORT_SYMBOL(qcom_smd_send
);
718 static struct qcom_smd_device
*to_smd_device(struct device
*dev
)
720 return container_of(dev
, struct qcom_smd_device
, dev
);
723 static struct qcom_smd_driver
*to_smd_driver(struct device
*dev
)
725 struct qcom_smd_device
*qsdev
= to_smd_device(dev
);
727 return container_of(qsdev
->dev
.driver
, struct qcom_smd_driver
, driver
);
730 static int qcom_smd_dev_match(struct device
*dev
, struct device_driver
*drv
)
732 struct qcom_smd_device
*qsdev
= to_smd_device(dev
);
733 struct qcom_smd_driver
*qsdrv
= container_of(drv
, struct qcom_smd_driver
, driver
);
734 const struct qcom_smd_id
*match
= qsdrv
->smd_match_table
;
735 const char *name
= qsdev
->channel
->name
;
738 while (match
->name
[0]) {
739 if (!strcmp(match
->name
, name
))
745 return of_driver_match_device(dev
, drv
);
749 * Probe the smd client.
751 * The remote side have indicated that it want the channel to be opened, so
752 * complete the state handshake and probe our client driver.
754 static int qcom_smd_dev_probe(struct device
*dev
)
756 struct qcom_smd_device
*qsdev
= to_smd_device(dev
);
757 struct qcom_smd_driver
*qsdrv
= to_smd_driver(dev
);
758 struct qcom_smd_channel
*channel
= qsdev
->channel
;
763 * Packets are maximum 4k, but reduce if the fifo is smaller
765 bb_size
= min(channel
->fifo_size
, SZ_4K
);
766 channel
->bounce_buffer
= kmalloc(bb_size
, GFP_KERNEL
);
767 if (!channel
->bounce_buffer
)
770 channel
->cb
= qsdrv
->callback
;
772 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_OPENING
);
774 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_OPENED
);
776 ret
= qsdrv
->probe(qsdev
);
780 qcom_smd_channel_resume(channel
);
785 dev_err(&qsdev
->dev
, "probe failed\n");
788 kfree(channel
->bounce_buffer
);
789 channel
->bounce_buffer
= NULL
;
791 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_CLOSED
);
796 * Remove the smd client.
798 * The channel is going away, for some reason, so remove the smd client and
799 * reset the channel state.
801 static int qcom_smd_dev_remove(struct device
*dev
)
803 struct qcom_smd_device
*qsdev
= to_smd_device(dev
);
804 struct qcom_smd_driver
*qsdrv
= to_smd_driver(dev
);
805 struct qcom_smd_channel
*channel
= qsdev
->channel
;
808 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_CLOSING
);
811 * Make sure we don't race with the code receiving data.
813 spin_lock_irqsave(&channel
->recv_lock
, flags
);
815 spin_unlock_irqrestore(&channel
->recv_lock
, flags
);
817 /* Wake up any sleepers in qcom_smd_send() */
818 wake_up_interruptible(&channel
->fblockread_event
);
821 * We expect that the client might block in remove() waiting for any
822 * outstanding calls to qcom_smd_send() to wake up and finish.
825 qsdrv
->remove(qsdev
);
828 * The client is now gone, cleanup and reset the channel state.
830 channel
->qsdev
= NULL
;
831 kfree(channel
->bounce_buffer
);
832 channel
->bounce_buffer
= NULL
;
834 qcom_smd_channel_set_state(channel
, SMD_CHANNEL_CLOSED
);
836 qcom_smd_channel_reset(channel
);
841 static struct bus_type qcom_smd_bus
= {
843 .match
= qcom_smd_dev_match
,
844 .probe
= qcom_smd_dev_probe
,
845 .remove
= qcom_smd_dev_remove
,
849 * Release function for the qcom_smd_device object.
851 static void qcom_smd_release_device(struct device
*dev
)
853 struct qcom_smd_device
*qsdev
= to_smd_device(dev
);
859 * Finds the device_node for the smd child interested in this channel.
861 static struct device_node
*qcom_smd_match_channel(struct device_node
*edge_node
,
864 struct device_node
*child
;
869 for_each_available_child_of_node(edge_node
, child
) {
870 key
= "qcom,smd-channels";
871 ret
= of_property_read_string(child
, key
, &name
);
877 if (strcmp(name
, channel
) == 0)
885 * Create a smd client device for channel that is being opened.
887 static int qcom_smd_create_device(struct qcom_smd_channel
*channel
)
889 struct qcom_smd_device
*qsdev
;
890 struct qcom_smd_edge
*edge
= channel
->edge
;
891 struct device_node
*node
;
892 struct qcom_smd
*smd
= edge
->smd
;
898 dev_dbg(smd
->dev
, "registering '%s'\n", channel
->name
);
900 qsdev
= kzalloc(sizeof(*qsdev
), GFP_KERNEL
);
904 node
= qcom_smd_match_channel(edge
->of_node
, channel
->name
);
905 dev_set_name(&qsdev
->dev
, "%s.%s",
907 node
? node
->name
: channel
->name
);
909 qsdev
->dev
.parent
= smd
->dev
;
910 qsdev
->dev
.bus
= &qcom_smd_bus
;
911 qsdev
->dev
.release
= qcom_smd_release_device
;
912 qsdev
->dev
.of_node
= node
;
914 qsdev
->channel
= channel
;
916 channel
->qsdev
= qsdev
;
918 ret
= device_register(&qsdev
->dev
);
920 dev_err(smd
->dev
, "device_register failed: %d\n", ret
);
921 put_device(&qsdev
->dev
);
928 * Destroy a smd client device for a channel that's going away.
930 static void qcom_smd_destroy_device(struct qcom_smd_channel
*channel
)
934 BUG_ON(!channel
->qsdev
);
936 dev
= &channel
->qsdev
->dev
;
938 device_unregister(dev
);
939 of_node_put(dev
->of_node
);
944 * qcom_smd_driver_register - register a smd driver
945 * @qsdrv: qcom_smd_driver struct
947 int qcom_smd_driver_register(struct qcom_smd_driver
*qsdrv
)
949 qsdrv
->driver
.bus
= &qcom_smd_bus
;
950 return driver_register(&qsdrv
->driver
);
952 EXPORT_SYMBOL(qcom_smd_driver_register
);
955 * qcom_smd_driver_unregister - unregister a smd driver
956 * @qsdrv: qcom_smd_driver struct
958 void qcom_smd_driver_unregister(struct qcom_smd_driver
*qsdrv
)
960 driver_unregister(&qsdrv
->driver
);
962 EXPORT_SYMBOL(qcom_smd_driver_unregister
);
965 * Allocate the qcom_smd_channel object for a newly found smd channel,
966 * retrieving and validating the smem items involved.
968 static struct qcom_smd_channel
*qcom_smd_create_channel(struct qcom_smd_edge
*edge
,
969 unsigned smem_info_item
,
970 unsigned smem_fifo_item
,
973 struct qcom_smd_channel
*channel
;
974 struct qcom_smd
*smd
= edge
->smd
;
981 channel
= devm_kzalloc(smd
->dev
, sizeof(*channel
), GFP_KERNEL
);
983 return ERR_PTR(-ENOMEM
);
985 channel
->edge
= edge
;
986 channel
->name
= devm_kstrdup(smd
->dev
, name
, GFP_KERNEL
);
988 return ERR_PTR(-ENOMEM
);
990 mutex_init(&channel
->tx_lock
);
991 spin_lock_init(&channel
->recv_lock
);
992 init_waitqueue_head(&channel
->fblockread_event
);
994 info
= qcom_smem_get(edge
->remote_pid
, smem_info_item
, &info_size
);
997 goto free_name_and_channel
;
1001 * Use the size of the item to figure out which channel info struct to
1004 if (info_size
== 2 * sizeof(struct smd_channel_info_word
)) {
1005 channel
->info_word
= info
;
1006 } else if (info_size
== 2 * sizeof(struct smd_channel_info
)) {
1007 channel
->info
= 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");