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 return of_driver_match_device(dev, drv);
734 * Probe the smd client.
736 * The remote side have indicated that it want the channel to be opened, so
737 * complete the state handshake and probe our client driver.
739 static int qcom_smd_dev_probe(struct device *dev)
741 struct qcom_smd_device *qsdev = to_smd_device(dev);
742 struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
743 struct qcom_smd_channel *channel = qsdev->channel;
748 * Packets are maximum 4k, but reduce if the fifo is smaller
750 bb_size = min(channel->fifo_size, SZ_4K);
751 channel->bounce_buffer = kmalloc(bb_size, GFP_KERNEL);
752 if (!channel->bounce_buffer)
755 channel->cb = qsdrv->callback;
757 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENING);
759 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED);
761 ret = qsdrv->probe(qsdev);
765 qcom_smd_channel_resume(channel);
770 dev_err(&qsdev->dev, "probe failed\n");
773 kfree(channel->bounce_buffer);
774 channel->bounce_buffer = NULL;
776 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
781 * Remove the smd client.
783 * The channel is going away, for some reason, so remove the smd client and
784 * reset the channel state.
786 static int qcom_smd_dev_remove(struct device *dev)
788 struct qcom_smd_device *qsdev = to_smd_device(dev);
789 struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
790 struct qcom_smd_channel *channel = qsdev->channel;
793 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSING);
796 * Make sure we don't race with the code receiving data.
798 spin_lock_irqsave(&channel->recv_lock, flags);
800 spin_unlock_irqrestore(&channel->recv_lock, flags);
802 /* Wake up any sleepers in qcom_smd_send() */
803 wake_up_interruptible(&channel->fblockread_event);
806 * We expect that the client might block in remove() waiting for any
807 * outstanding calls to qcom_smd_send() to wake up and finish.
810 qsdrv->remove(qsdev);
813 * The client is now gone, cleanup and reset the channel state.
815 channel->qsdev = NULL;
816 kfree(channel->bounce_buffer);
817 channel->bounce_buffer = NULL;
819 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
821 qcom_smd_channel_reset(channel);
826 static struct bus_type qcom_smd_bus = {
828 .match = qcom_smd_dev_match,
829 .probe = qcom_smd_dev_probe,
830 .remove = qcom_smd_dev_remove,
834 * Release function for the qcom_smd_device object.
836 static void qcom_smd_release_device(struct device *dev)
838 struct qcom_smd_device *qsdev = to_smd_device(dev);
844 * Finds the device_node for the smd child interested in this channel.
846 static struct device_node *qcom_smd_match_channel(struct device_node *edge_node,
849 struct device_node *child;
854 for_each_available_child_of_node(edge_node, child) {
855 key = "qcom,smd-channels";
856 ret = of_property_read_string(child, key, &name);
862 if (strcmp(name, channel) == 0)
870 * Create a smd client device for channel that is being opened.
872 static int qcom_smd_create_device(struct qcom_smd_channel *channel)
874 struct qcom_smd_device *qsdev;
875 struct qcom_smd_edge *edge = channel->edge;
876 struct device_node *node;
877 struct qcom_smd *smd = edge->smd;
883 node = qcom_smd_match_channel(edge->of_node, channel->name);
885 dev_dbg(smd->dev, "no match for '%s'\n", channel->name);
889 dev_dbg(smd->dev, "registering '%s'\n", channel->name);
891 qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
895 dev_set_name(&qsdev->dev, "%s.%s", edge->of_node->name, node->name);
896 qsdev->dev.parent = smd->dev;
897 qsdev->dev.bus = &qcom_smd_bus;
898 qsdev->dev.release = qcom_smd_release_device;
899 qsdev->dev.of_node = node;
901 qsdev->channel = channel;
903 channel->qsdev = qsdev;
905 ret = device_register(&qsdev->dev);
907 dev_err(smd->dev, "device_register failed: %d\n", ret);
908 put_device(&qsdev->dev);
915 * Destroy a smd client device for a channel that's going away.
917 static void qcom_smd_destroy_device(struct qcom_smd_channel *channel)
921 BUG_ON(!channel->qsdev);
923 dev = &channel->qsdev->dev;
925 device_unregister(dev);
926 of_node_put(dev->of_node);
931 * qcom_smd_driver_register - register a smd driver
932 * @qsdrv: qcom_smd_driver struct
934 int qcom_smd_driver_register(struct qcom_smd_driver *qsdrv)
936 qsdrv->driver.bus = &qcom_smd_bus;
937 return driver_register(&qsdrv->driver);
939 EXPORT_SYMBOL(qcom_smd_driver_register);
942 * qcom_smd_driver_unregister - unregister a smd driver
943 * @qsdrv: qcom_smd_driver struct
945 void qcom_smd_driver_unregister(struct qcom_smd_driver *qsdrv)
947 driver_unregister(&qsdrv->driver);
949 EXPORT_SYMBOL(qcom_smd_driver_unregister);
952 * Allocate the qcom_smd_channel object for a newly found smd channel,
953 * retrieving and validating the smem items involved.
955 static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge,
956 unsigned smem_info_item,
957 unsigned smem_fifo_item,
960 struct qcom_smd_channel *channel;
961 struct qcom_smd *smd = edge->smd;
968 channel = devm_kzalloc(smd->dev, sizeof(*channel), GFP_KERNEL);
970 return ERR_PTR(-ENOMEM);
972 channel->edge = edge;
973 channel->name = devm_kstrdup(smd->dev, name, GFP_KERNEL);
975 return ERR_PTR(-ENOMEM);
977 mutex_init(&channel->tx_lock);
978 spin_lock_init(&channel->recv_lock);
979 init_waitqueue_head(&channel->fblockread_event);
981 ret = qcom_smem_get(edge->remote_pid, smem_info_item, (void **)&info,
984 goto free_name_and_channel;
987 * Use the size of the item to figure out which channel info struct to
990 if (info_size == 2 * sizeof(struct smd_channel_info_word)) {
991 channel->tx_info_word = info;
992 channel->rx_info_word = info + sizeof(struct smd_channel_info_word);
993 } else if (info_size == 2 * sizeof(struct smd_channel_info)) {
994 channel->tx_info = info;
995 channel->rx_info = info + sizeof(struct smd_channel_info);
998 "channel info of size %zu not supported\n", info_size);
1000 goto free_name_and_channel;
1003 ret = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_base,
1006 goto free_name_and_channel;
1008 /* The channel consist of a rx and tx fifo of equal size */
1011 dev_dbg(smd->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1012 name, info_size, fifo_size);
1014 channel->tx_fifo = fifo_base;
1015 channel->rx_fifo = fifo_base + fifo_size;
1016 channel->fifo_size = fifo_size;
1018 qcom_smd_channel_reset(channel);
1022 free_name_and_channel:
1023 devm_kfree(smd->dev, channel->name);
1024 devm_kfree(smd->dev, channel);
1026 return ERR_PTR(ret);
1030 * Scans the allocation table for any newly allocated channels, calls
1031 * qcom_smd_create_channel() to create representations of these and add
1032 * them to the edge's list of channels.
1034 static void qcom_discover_channels(struct qcom_smd_edge *edge)
1036 struct qcom_smd_alloc_entry *alloc_tbl;
1037 struct qcom_smd_alloc_entry *entry;
1038 struct qcom_smd_channel *channel;
1039 struct qcom_smd *smd = edge->smd;
1040 unsigned long flags;
1047 for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) {
1048 ret = qcom_smem_get(edge->remote_pid,
1049 smem_items[tbl].alloc_tbl_id,
1050 (void **)&alloc_tbl,
1055 for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) {
1056 entry = &alloc_tbl[i];
1057 if (test_bit(i, edge->allocated[tbl]))
1060 if (entry->ref_count == 0)
1063 if (!entry->name[0])
1066 if (!(entry->flags & SMD_CHANNEL_FLAGS_PACKET))
1069 if ((entry->flags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id)
1072 info_id = smem_items[tbl].info_base_id + entry->cid;
1073 fifo_id = smem_items[tbl].fifo_base_id + entry->cid;
1075 channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
1076 if (IS_ERR(channel))
1079 spin_lock_irqsave(&edge->channels_lock, flags);
1080 list_add(&channel->list, &edge->channels);
1081 spin_unlock_irqrestore(&edge->channels_lock, flags);
1083 dev_dbg(smd->dev, "new channel found: '%s'\n", channel->name);
1084 set_bit(i, edge->allocated[tbl]);
1088 schedule_work(&edge->work);
1092 * This per edge worker scans smem for any new channels and register these. It
1093 * then scans all registered channels for state changes that should be handled
1094 * by creating or destroying smd client devices for the registered channels.
1096 * LOCKING: edge->channels_lock is not needed to be held during the traversal
1097 * of the channels list as it's done synchronously with the only writer.
1099 static void qcom_channel_state_worker(struct work_struct *work)
1101 struct qcom_smd_channel *channel;
1102 struct qcom_smd_edge *edge = container_of(work,
1103 struct qcom_smd_edge,
1105 unsigned remote_state;
1108 * Rescan smem if we have reason to belive that there are new channels.
1110 if (edge->need_rescan) {
1111 edge->need_rescan = false;
1112 qcom_discover_channels(edge);
1116 * Register a device for any closed channel where the remote processor
1117 * is showing interest in opening the channel.
1119 list_for_each_entry(channel, &edge->channels, list) {
1120 if (channel->state != SMD_CHANNEL_CLOSED)
1123 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1124 if (remote_state != SMD_CHANNEL_OPENING &&
1125 remote_state != SMD_CHANNEL_OPENED)
1128 qcom_smd_create_device(channel);
1132 * Unregister the device for any channel that is opened where the
1133 * remote processor is closing the channel.
1135 list_for_each_entry(channel, &edge->channels, list) {
1136 if (channel->state != SMD_CHANNEL_OPENING &&
1137 channel->state != SMD_CHANNEL_OPENED)
1140 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1141 if (remote_state == SMD_CHANNEL_OPENING ||
1142 remote_state == SMD_CHANNEL_OPENED)
1145 qcom_smd_destroy_device(channel);
1150 * Parses an of_node describing an edge.
1152 static int qcom_smd_parse_edge(struct device *dev,
1153 struct device_node *node,
1154 struct qcom_smd_edge *edge)
1156 struct device_node *syscon_np;
1161 INIT_LIST_HEAD(&edge->channels);
1162 spin_lock_init(&edge->channels_lock);
1164 INIT_WORK(&edge->work, qcom_channel_state_worker);
1166 edge->of_node = of_node_get(node);
1168 irq = irq_of_parse_and_map(node, 0);
1170 dev_err(dev, "required smd interrupt missing\n");
1174 ret = devm_request_irq(dev, irq,
1175 qcom_smd_edge_intr, IRQF_TRIGGER_RISING,
1178 dev_err(dev, "failed to request smd irq\n");
1184 key = "qcom,smd-edge";
1185 ret = of_property_read_u32(node, key, &edge->edge_id);
1187 dev_err(dev, "edge missing %s property\n", key);
1191 edge->remote_pid = QCOM_SMEM_HOST_ANY;
1192 key = "qcom,remote-pid";
1193 of_property_read_u32(node, key, &edge->remote_pid);
1195 syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
1197 dev_err(dev, "no qcom,ipc node\n");
1201 edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
1202 if (IS_ERR(edge->ipc_regmap))
1203 return PTR_ERR(edge->ipc_regmap);
1206 ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
1208 dev_err(dev, "no offset in %s\n", key);
1212 ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
1214 dev_err(dev, "no bit in %s\n", key);
1221 static int qcom_smd_probe(struct platform_device *pdev)
1223 struct qcom_smd_edge *edge;
1224 struct device_node *node;
1225 struct qcom_smd *smd;
1232 ret = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL, NULL);
1233 if (ret == -EPROBE_DEFER)
1236 num_edges = of_get_available_child_count(pdev->dev.of_node);
1237 array_size = sizeof(*smd) + num_edges * sizeof(struct qcom_smd_edge);
1238 smd = devm_kzalloc(&pdev->dev, array_size, GFP_KERNEL);
1241 smd->dev = &pdev->dev;
1243 smd->num_edges = num_edges;
1244 for_each_available_child_of_node(pdev->dev.of_node, node) {
1245 edge = &smd->edges[i++];
1248 ret = qcom_smd_parse_edge(&pdev->dev, node, edge);
1252 edge->need_rescan = true;
1253 schedule_work(&edge->work);
1256 platform_set_drvdata(pdev, smd);
1262 * Shut down all smd clients by making sure that each edge stops processing
1263 * events and scanning for new channels, then call destroy on the devices.
1265 static int qcom_smd_remove(struct platform_device *pdev)
1267 struct qcom_smd_channel *channel;
1268 struct qcom_smd_edge *edge;
1269 struct qcom_smd *smd = platform_get_drvdata(pdev);
1272 for (i = 0; i < smd->num_edges; i++) {
1273 edge = &smd->edges[i];
1275 disable_irq(edge->irq);
1276 cancel_work_sync(&edge->work);
1278 list_for_each_entry(channel, &edge->channels, list) {
1279 if (!channel->qsdev)
1282 qcom_smd_destroy_device(channel);
1289 static const struct of_device_id qcom_smd_of_match[] = {
1290 { .compatible = "qcom,smd" },
1293 MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
1295 static struct platform_driver qcom_smd_driver = {
1296 .probe = qcom_smd_probe,
1297 .remove = qcom_smd_remove,
1300 .of_match_table = qcom_smd_of_match,
1304 static int __init qcom_smd_init(void)
1308 ret = bus_register(&qcom_smd_bus);
1310 pr_err("failed to register smd bus: %d\n", ret);
1314 return platform_driver_register(&qcom_smd_driver);
1316 postcore_initcall(qcom_smd_init);
1318 static void __exit qcom_smd_exit(void)
1320 platform_driver_unregister(&qcom_smd_driver);
1321 bus_unregister(&qcom_smd_bus);
1323 module_exit(qcom_smd_exit);
1325 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
1326 MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
1327 MODULE_LICENSE("GPL v2");