cxl: Poll for outstanding IRQs when detaching a context
[linux/fpc-iii.git] / drivers / soc / qcom / smd.c
blob498fd0581a451999b89cbb43805b37f003fdb379
1 /*
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>
16 #include <linux/io.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
44 * state.
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
50 * channel is up.
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
59 * processor.
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.
79 static const struct {
80 unsigned alloc_tbl_id;
81 unsigned info_base_id;
82 unsigned fifo_base_id;
83 } smem_items[SMD_ALLOC_TBL_COUNT] = {
85 .alloc_tbl_id = 13,
86 .info_base_id = 14,
87 .fifo_base_id = 338
90 .alloc_tbl_id = 266,
91 .info_base_id = 138,
92 .fifo_base_id = 202,
96 /**
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;
116 unsigned edge_id;
117 unsigned remote_pid;
119 int irq;
121 struct regmap *ipc_regmap;
122 int ipc_offset;
123 int ipc_bit;
125 struct list_head channels;
126 spinlock_t channels_lock;
128 DECLARE_BITMAP(allocated[SMD_ALLOC_TBL_COUNT], SMD_ALLOC_TBL_SIZE);
130 bool need_rescan;
131 unsigned smem_available;
133 struct work_struct work;
137 * SMD channel states.
139 enum smd_channel_state {
140 SMD_CHANNEL_CLOSED,
141 SMD_CHANNEL_OPENING,
142 SMD_CHANNEL_OPENED,
143 SMD_CHANNEL_FLUSHING,
144 SMD_CHANNEL_CLOSING,
145 SMD_CHANNEL_RESET,
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;
174 char *name;
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;
184 void *tx_fifo;
185 void *rx_fifo;
186 int fifo_size;
188 void *bounce_buffer;
189 int (*cb)(struct qcom_smd_device *, const void *, size_t);
191 spinlock_t recv_lock;
193 int pkt_size;
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
204 struct qcom_smd {
205 struct device *dev;
207 unsigned num_edges;
208 struct qcom_smd_edge edges[0];
212 * Format of the smd_info smem items, for byte aligned channels.
214 struct smd_channel_info {
215 __le32 state;
216 u8 fDSR;
217 u8 fCTS;
218 u8 fCD;
219 u8 fRI;
220 u8 fHEAD;
221 u8 fTAIL;
222 u8 fSTATE;
223 u8 fBLOCKREADINTR;
224 __le32 tail;
225 __le32 head;
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 {
237 __le32 state;
238 __le32 fDSR;
239 __le32 fCTS;
240 __le32 fCD;
241 __le32 fRI;
242 __le32 fHEAD;
243 __le32 fTAIL;
244 __le32 fSTATE;
245 __le32 fBLOCKREADINTR;
246 __le32 tail;
247 __le32 head;
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_FLAG(channel, param) \
256 ({ \
257 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
258 channel->info_word ? \
259 le32_to_cpu(channel->info_word->rx.param) : \
260 channel->info->rx.param; \
263 #define GET_RX_CHANNEL_INFO(channel, param) \
264 ({ \
265 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
266 le32_to_cpu(channel->info_word ? \
267 channel->info_word->rx.param : \
268 channel->info->rx.param); \
271 #define SET_RX_CHANNEL_FLAG(channel, param, value) \
272 ({ \
273 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
274 if (channel->info_word) \
275 channel->info_word->rx.param = cpu_to_le32(value); \
276 else \
277 channel->info->rx.param = value; \
280 #define SET_RX_CHANNEL_INFO(channel, param, value) \
281 ({ \
282 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
283 if (channel->info_word) \
284 channel->info_word->rx.param = cpu_to_le32(value); \
285 else \
286 channel->info->rx.param = cpu_to_le32(value); \
289 #define GET_TX_CHANNEL_FLAG(channel, param) \
290 ({ \
291 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
292 channel->info_word ? \
293 le32_to_cpu(channel->info_word->tx.param) : \
294 channel->info->tx.param; \
297 #define GET_TX_CHANNEL_INFO(channel, param) \
298 ({ \
299 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
300 le32_to_cpu(channel->info_word ? \
301 channel->info_word->tx.param : \
302 channel->info->tx.param); \
305 #define SET_TX_CHANNEL_FLAG(channel, param, value) \
306 ({ \
307 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
308 if (channel->info_word) \
309 channel->info_word->tx.param = cpu_to_le32(value); \
310 else \
311 channel->info->tx.param = value; \
314 #define SET_TX_CHANNEL_INFO(channel, param, value) \
315 ({ \
316 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
317 if (channel->info_word) \
318 channel->info_word->tx.param = cpu_to_le32(value); \
319 else \
320 channel->info->tx.param = cpu_to_le32(value); \
324 * struct qcom_smd_alloc_entry - channel allocation entry
325 * @name: channel name
326 * @cid: channel index
327 * @flags: channel flags and edge id
328 * @ref_count: reference count of the channel
330 struct qcom_smd_alloc_entry {
331 u8 name[20];
332 __le32 cid;
333 __le32 flags;
334 __le32 ref_count;
335 } __packed;
337 #define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff
338 #define SMD_CHANNEL_FLAGS_STREAM BIT(8)
339 #define SMD_CHANNEL_FLAGS_PACKET BIT(9)
342 * Each smd packet contains a 20 byte header, with the first 4 being the length
343 * of the packet.
345 #define SMD_PACKET_HEADER_LEN 20
348 * Signal the remote processor associated with 'channel'.
350 static void qcom_smd_signal_channel(struct qcom_smd_channel *channel)
352 struct qcom_smd_edge *edge = channel->edge;
354 regmap_write(edge->ipc_regmap, edge->ipc_offset, BIT(edge->ipc_bit));
358 * Initialize the tx channel info
360 static void qcom_smd_channel_reset(struct qcom_smd_channel *channel)
362 SET_TX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
363 SET_TX_CHANNEL_FLAG(channel, fDSR, 0);
364 SET_TX_CHANNEL_FLAG(channel, fCTS, 0);
365 SET_TX_CHANNEL_FLAG(channel, fCD, 0);
366 SET_TX_CHANNEL_FLAG(channel, fRI, 0);
367 SET_TX_CHANNEL_FLAG(channel, fHEAD, 0);
368 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
369 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
370 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
371 SET_TX_CHANNEL_INFO(channel, head, 0);
372 SET_TX_CHANNEL_INFO(channel, tail, 0);
374 qcom_smd_signal_channel(channel);
376 channel->state = SMD_CHANNEL_CLOSED;
377 channel->pkt_size = 0;
381 * Calculate the amount of data available in the rx fifo
383 static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel *channel)
385 unsigned head;
386 unsigned tail;
388 head = GET_RX_CHANNEL_INFO(channel, head);
389 tail = GET_RX_CHANNEL_INFO(channel, tail);
391 return (head - tail) & (channel->fifo_size - 1);
395 * Set tx channel state and inform the remote processor
397 static void qcom_smd_channel_set_state(struct qcom_smd_channel *channel,
398 int state)
400 struct qcom_smd_edge *edge = channel->edge;
401 bool is_open = state == SMD_CHANNEL_OPENED;
403 if (channel->state == state)
404 return;
406 dev_dbg(edge->smd->dev, "set_state(%s, %d)\n", channel->name, state);
408 SET_TX_CHANNEL_FLAG(channel, fDSR, is_open);
409 SET_TX_CHANNEL_FLAG(channel, fCTS, is_open);
410 SET_TX_CHANNEL_FLAG(channel, fCD, is_open);
412 SET_TX_CHANNEL_INFO(channel, state, state);
413 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
415 channel->state = state;
416 qcom_smd_signal_channel(channel);
420 * Copy count bytes of data using 32bit accesses, if that's required.
422 static void smd_copy_to_fifo(void __iomem *dst,
423 const void *src,
424 size_t count,
425 bool word_aligned)
427 if (word_aligned) {
428 __iowrite32_copy(dst, src, count / sizeof(u32));
429 } else {
430 memcpy_toio(dst, src, count);
435 * Copy count bytes of data using 32bit accesses, if that is required.
437 static void smd_copy_from_fifo(void *dst,
438 const void __iomem *src,
439 size_t count,
440 bool word_aligned)
442 if (word_aligned) {
443 __ioread32_copy(dst, src, count / sizeof(u32));
444 } else {
445 memcpy_fromio(dst, src, count);
450 * Read count bytes of data from the rx fifo into buf, but don't advance the
451 * tail.
453 static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel,
454 void *buf, size_t count)
456 bool word_aligned;
457 unsigned tail;
458 size_t len;
460 word_aligned = channel->info_word;
461 tail = GET_RX_CHANNEL_INFO(channel, tail);
463 len = min_t(size_t, count, channel->fifo_size - tail);
464 if (len) {
465 smd_copy_from_fifo(buf,
466 channel->rx_fifo + tail,
467 len,
468 word_aligned);
471 if (len != count) {
472 smd_copy_from_fifo(buf + len,
473 channel->rx_fifo,
474 count - len,
475 word_aligned);
478 return count;
482 * Advance the rx tail by count bytes.
484 static void qcom_smd_channel_advance(struct qcom_smd_channel *channel,
485 size_t count)
487 unsigned tail;
489 tail = GET_RX_CHANNEL_INFO(channel, tail);
490 tail += count;
491 tail &= (channel->fifo_size - 1);
492 SET_RX_CHANNEL_INFO(channel, tail, tail);
496 * Read out a single packet from the rx fifo and deliver it to the device
498 static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
500 struct qcom_smd_device *qsdev = channel->qsdev;
501 unsigned tail;
502 size_t len;
503 void *ptr;
504 int ret;
506 if (!channel->cb)
507 return 0;
509 tail = GET_RX_CHANNEL_INFO(channel, tail);
511 /* Use bounce buffer if the data wraps */
512 if (tail + channel->pkt_size >= channel->fifo_size) {
513 ptr = channel->bounce_buffer;
514 len = qcom_smd_channel_peek(channel, ptr, channel->pkt_size);
515 } else {
516 ptr = channel->rx_fifo + tail;
517 len = channel->pkt_size;
520 ret = channel->cb(qsdev, ptr, len);
521 if (ret < 0)
522 return ret;
524 /* Only forward the tail if the client consumed the data */
525 qcom_smd_channel_advance(channel, len);
527 channel->pkt_size = 0;
529 return 0;
533 * Per channel interrupt handling
535 static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel)
537 bool need_state_scan = false;
538 int remote_state;
539 __le32 pktlen;
540 int avail;
541 int ret;
543 /* Handle state changes */
544 remote_state = GET_RX_CHANNEL_INFO(channel, state);
545 if (remote_state != channel->remote_state) {
546 channel->remote_state = remote_state;
547 need_state_scan = true;
549 /* Indicate that we have seen any state change */
550 SET_RX_CHANNEL_FLAG(channel, fSTATE, 0);
552 /* Signal waiting qcom_smd_send() about the interrupt */
553 if (!GET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR))
554 wake_up_interruptible(&channel->fblockread_event);
556 /* Don't consume any data until we've opened the channel */
557 if (channel->state != SMD_CHANNEL_OPENED)
558 goto out;
560 /* Indicate that we've seen the new data */
561 SET_RX_CHANNEL_FLAG(channel, fHEAD, 0);
563 /* Consume data */
564 for (;;) {
565 avail = qcom_smd_channel_get_rx_avail(channel);
567 if (!channel->pkt_size && avail >= SMD_PACKET_HEADER_LEN) {
568 qcom_smd_channel_peek(channel, &pktlen, sizeof(pktlen));
569 qcom_smd_channel_advance(channel, SMD_PACKET_HEADER_LEN);
570 channel->pkt_size = le32_to_cpu(pktlen);
571 } else if (channel->pkt_size && avail >= channel->pkt_size) {
572 ret = qcom_smd_channel_recv_single(channel);
573 if (ret)
574 break;
575 } else {
576 break;
580 /* Indicate that we have seen and updated tail */
581 SET_RX_CHANNEL_FLAG(channel, fTAIL, 1);
583 /* Signal the remote that we've consumed the data (if requested) */
584 if (!GET_RX_CHANNEL_FLAG(channel, fBLOCKREADINTR)) {
585 /* Ensure ordering of channel info updates */
586 wmb();
588 qcom_smd_signal_channel(channel);
591 out:
592 return need_state_scan;
596 * The edge interrupts are triggered by the remote processor on state changes,
597 * channel info updates or when new channels are created.
599 static irqreturn_t qcom_smd_edge_intr(int irq, void *data)
601 struct qcom_smd_edge *edge = data;
602 struct qcom_smd_channel *channel;
603 unsigned available;
604 bool kick_worker = false;
607 * Handle state changes or data on each of the channels on this edge
609 spin_lock(&edge->channels_lock);
610 list_for_each_entry(channel, &edge->channels, list) {
611 spin_lock(&channel->recv_lock);
612 kick_worker |= qcom_smd_channel_intr(channel);
613 spin_unlock(&channel->recv_lock);
615 spin_unlock(&edge->channels_lock);
618 * Creating a new channel requires allocating an smem entry, so we only
619 * have to scan if the amount of available space in smem have changed
620 * since last scan.
622 available = qcom_smem_get_free_space(edge->remote_pid);
623 if (available != edge->smem_available) {
624 edge->smem_available = available;
625 edge->need_rescan = true;
626 kick_worker = true;
629 if (kick_worker)
630 schedule_work(&edge->work);
632 return IRQ_HANDLED;
636 * Delivers any outstanding packets in the rx fifo, can be used after probe of
637 * the clients to deliver any packets that wasn't delivered before the client
638 * was setup.
640 static void qcom_smd_channel_resume(struct qcom_smd_channel *channel)
642 unsigned long flags;
644 spin_lock_irqsave(&channel->recv_lock, flags);
645 qcom_smd_channel_intr(channel);
646 spin_unlock_irqrestore(&channel->recv_lock, flags);
650 * Calculate how much space is available in the tx fifo.
652 static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel)
654 unsigned head;
655 unsigned tail;
656 unsigned mask = channel->fifo_size - 1;
658 head = GET_TX_CHANNEL_INFO(channel, head);
659 tail = GET_TX_CHANNEL_INFO(channel, tail);
661 return mask - ((head - tail) & mask);
665 * Write count bytes of data into channel, possibly wrapping in the ring buffer
667 static int qcom_smd_write_fifo(struct qcom_smd_channel *channel,
668 const void *data,
669 size_t count)
671 bool word_aligned;
672 unsigned head;
673 size_t len;
675 word_aligned = channel->info_word;
676 head = GET_TX_CHANNEL_INFO(channel, head);
678 len = min_t(size_t, count, channel->fifo_size - head);
679 if (len) {
680 smd_copy_to_fifo(channel->tx_fifo + head,
681 data,
682 len,
683 word_aligned);
686 if (len != count) {
687 smd_copy_to_fifo(channel->tx_fifo,
688 data + len,
689 count - len,
690 word_aligned);
693 head += count;
694 head &= (channel->fifo_size - 1);
695 SET_TX_CHANNEL_INFO(channel, head, head);
697 return count;
701 * qcom_smd_send - write data to smd channel
702 * @channel: channel handle
703 * @data: buffer of data to write
704 * @len: number of bytes to write
706 * This is a blocking write of len bytes into the channel's tx ring buffer and
707 * signal the remote end. It will sleep until there is enough space available
708 * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
709 * polling.
711 int qcom_smd_send(struct qcom_smd_channel *channel, const void *data, int len)
713 __le32 hdr[5] = { cpu_to_le32(len), };
714 int tlen = sizeof(hdr) + len;
715 int ret;
717 /* Word aligned channels only accept word size aligned data */
718 if (channel->info_word && len % 4)
719 return -EINVAL;
721 /* Reject packets that are too big */
722 if (tlen >= channel->fifo_size)
723 return -EINVAL;
725 ret = mutex_lock_interruptible(&channel->tx_lock);
726 if (ret)
727 return ret;
729 while (qcom_smd_get_tx_avail(channel) < tlen) {
730 if (channel->state != SMD_CHANNEL_OPENED) {
731 ret = -EPIPE;
732 goto out;
735 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 0);
737 ret = wait_event_interruptible(channel->fblockread_event,
738 qcom_smd_get_tx_avail(channel) >= tlen ||
739 channel->state != SMD_CHANNEL_OPENED);
740 if (ret)
741 goto out;
743 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
746 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
748 qcom_smd_write_fifo(channel, hdr, sizeof(hdr));
749 qcom_smd_write_fifo(channel, data, len);
751 SET_TX_CHANNEL_FLAG(channel, fHEAD, 1);
753 /* Ensure ordering of channel info updates */
754 wmb();
756 qcom_smd_signal_channel(channel);
758 out:
759 mutex_unlock(&channel->tx_lock);
761 return ret;
763 EXPORT_SYMBOL(qcom_smd_send);
765 static struct qcom_smd_device *to_smd_device(struct device *dev)
767 return container_of(dev, struct qcom_smd_device, dev);
770 static struct qcom_smd_driver *to_smd_driver(struct device *dev)
772 struct qcom_smd_device *qsdev = to_smd_device(dev);
774 return container_of(qsdev->dev.driver, struct qcom_smd_driver, driver);
777 static int qcom_smd_dev_match(struct device *dev, struct device_driver *drv)
779 struct qcom_smd_device *qsdev = to_smd_device(dev);
780 struct qcom_smd_driver *qsdrv = container_of(drv, struct qcom_smd_driver, driver);
781 const struct qcom_smd_id *match = qsdrv->smd_match_table;
782 const char *name = qsdev->channel->name;
784 if (match) {
785 while (match->name[0]) {
786 if (!strcmp(match->name, name))
787 return 1;
788 match++;
792 return of_driver_match_device(dev, drv);
796 * Probe the smd client.
798 * The remote side have indicated that it want the channel to be opened, so
799 * complete the state handshake and probe our client driver.
801 static int qcom_smd_dev_probe(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;
806 size_t bb_size;
807 int ret;
810 * Packets are maximum 4k, but reduce if the fifo is smaller
812 bb_size = min(channel->fifo_size, SZ_4K);
813 channel->bounce_buffer = kmalloc(bb_size, GFP_KERNEL);
814 if (!channel->bounce_buffer)
815 return -ENOMEM;
817 channel->cb = qsdrv->callback;
819 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENING);
821 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED);
823 ret = qsdrv->probe(qsdev);
824 if (ret)
825 goto err;
827 qcom_smd_channel_resume(channel);
829 return 0;
831 err:
832 dev_err(&qsdev->dev, "probe failed\n");
834 channel->cb = NULL;
835 kfree(channel->bounce_buffer);
836 channel->bounce_buffer = NULL;
838 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
839 return ret;
843 * Remove the smd client.
845 * The channel is going away, for some reason, so remove the smd client and
846 * reset the channel state.
848 static int qcom_smd_dev_remove(struct device *dev)
850 struct qcom_smd_device *qsdev = to_smd_device(dev);
851 struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
852 struct qcom_smd_channel *channel = qsdev->channel;
853 unsigned long flags;
855 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSING);
858 * Make sure we don't race with the code receiving data.
860 spin_lock_irqsave(&channel->recv_lock, flags);
861 channel->cb = NULL;
862 spin_unlock_irqrestore(&channel->recv_lock, flags);
864 /* Wake up any sleepers in qcom_smd_send() */
865 wake_up_interruptible(&channel->fblockread_event);
868 * We expect that the client might block in remove() waiting for any
869 * outstanding calls to qcom_smd_send() to wake up and finish.
871 if (qsdrv->remove)
872 qsdrv->remove(qsdev);
875 * The client is now gone, cleanup and reset the channel state.
877 channel->qsdev = NULL;
878 kfree(channel->bounce_buffer);
879 channel->bounce_buffer = NULL;
881 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
883 qcom_smd_channel_reset(channel);
885 return 0;
888 static struct bus_type qcom_smd_bus = {
889 .name = "qcom_smd",
890 .match = qcom_smd_dev_match,
891 .probe = qcom_smd_dev_probe,
892 .remove = qcom_smd_dev_remove,
896 * Release function for the qcom_smd_device object.
898 static void qcom_smd_release_device(struct device *dev)
900 struct qcom_smd_device *qsdev = to_smd_device(dev);
902 kfree(qsdev);
906 * Finds the device_node for the smd child interested in this channel.
908 static struct device_node *qcom_smd_match_channel(struct device_node *edge_node,
909 const char *channel)
911 struct device_node *child;
912 const char *name;
913 const char *key;
914 int ret;
916 for_each_available_child_of_node(edge_node, child) {
917 key = "qcom,smd-channels";
918 ret = of_property_read_string(child, key, &name);
919 if (ret)
920 continue;
922 if (strcmp(name, channel) == 0)
923 return child;
926 return NULL;
930 * Create a smd client device for channel that is being opened.
932 static int qcom_smd_create_device(struct qcom_smd_channel *channel)
934 struct qcom_smd_device *qsdev;
935 struct qcom_smd_edge *edge = channel->edge;
936 struct device_node *node;
937 struct qcom_smd *smd = edge->smd;
938 int ret;
940 if (channel->qsdev)
941 return -EEXIST;
943 dev_dbg(smd->dev, "registering '%s'\n", channel->name);
945 qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
946 if (!qsdev)
947 return -ENOMEM;
949 node = qcom_smd_match_channel(edge->of_node, channel->name);
950 dev_set_name(&qsdev->dev, "%s.%s",
951 edge->of_node->name,
952 node ? node->name : channel->name);
954 qsdev->dev.parent = smd->dev;
955 qsdev->dev.bus = &qcom_smd_bus;
956 qsdev->dev.release = qcom_smd_release_device;
957 qsdev->dev.of_node = node;
959 qsdev->channel = channel;
961 channel->qsdev = qsdev;
963 ret = device_register(&qsdev->dev);
964 if (ret) {
965 dev_err(smd->dev, "device_register failed: %d\n", ret);
966 put_device(&qsdev->dev);
969 return ret;
973 * Destroy a smd client device for a channel that's going away.
975 static void qcom_smd_destroy_device(struct qcom_smd_channel *channel)
977 struct device *dev;
979 BUG_ON(!channel->qsdev);
981 dev = &channel->qsdev->dev;
983 device_unregister(dev);
984 of_node_put(dev->of_node);
985 put_device(dev);
989 * qcom_smd_driver_register - register a smd driver
990 * @qsdrv: qcom_smd_driver struct
992 int qcom_smd_driver_register(struct qcom_smd_driver *qsdrv)
994 qsdrv->driver.bus = &qcom_smd_bus;
995 return driver_register(&qsdrv->driver);
997 EXPORT_SYMBOL(qcom_smd_driver_register);
1000 * qcom_smd_driver_unregister - unregister a smd driver
1001 * @qsdrv: qcom_smd_driver struct
1003 void qcom_smd_driver_unregister(struct qcom_smd_driver *qsdrv)
1005 driver_unregister(&qsdrv->driver);
1007 EXPORT_SYMBOL(qcom_smd_driver_unregister);
1010 * Allocate the qcom_smd_channel object for a newly found smd channel,
1011 * retrieving and validating the smem items involved.
1013 static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge,
1014 unsigned smem_info_item,
1015 unsigned smem_fifo_item,
1016 char *name)
1018 struct qcom_smd_channel *channel;
1019 struct qcom_smd *smd = edge->smd;
1020 size_t fifo_size;
1021 size_t info_size;
1022 void *fifo_base;
1023 void *info;
1024 int ret;
1026 channel = devm_kzalloc(smd->dev, sizeof(*channel), GFP_KERNEL);
1027 if (!channel)
1028 return ERR_PTR(-ENOMEM);
1030 channel->edge = edge;
1031 channel->name = devm_kstrdup(smd->dev, name, GFP_KERNEL);
1032 if (!channel->name)
1033 return ERR_PTR(-ENOMEM);
1035 mutex_init(&channel->tx_lock);
1036 spin_lock_init(&channel->recv_lock);
1037 init_waitqueue_head(&channel->fblockread_event);
1039 info = qcom_smem_get(edge->remote_pid, smem_info_item, &info_size);
1040 if (IS_ERR(info)) {
1041 ret = PTR_ERR(info);
1042 goto free_name_and_channel;
1046 * Use the size of the item to figure out which channel info struct to
1047 * use.
1049 if (info_size == 2 * sizeof(struct smd_channel_info_word)) {
1050 channel->info_word = info;
1051 } else if (info_size == 2 * sizeof(struct smd_channel_info)) {
1052 channel->info = info;
1053 } else {
1054 dev_err(smd->dev,
1055 "channel info of size %zu not supported\n", info_size);
1056 ret = -EINVAL;
1057 goto free_name_and_channel;
1060 fifo_base = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_size);
1061 if (IS_ERR(fifo_base)) {
1062 ret = PTR_ERR(fifo_base);
1063 goto free_name_and_channel;
1066 /* The channel consist of a rx and tx fifo of equal size */
1067 fifo_size /= 2;
1069 dev_dbg(smd->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1070 name, info_size, fifo_size);
1072 channel->tx_fifo = fifo_base;
1073 channel->rx_fifo = fifo_base + fifo_size;
1074 channel->fifo_size = fifo_size;
1076 qcom_smd_channel_reset(channel);
1078 return channel;
1080 free_name_and_channel:
1081 devm_kfree(smd->dev, channel->name);
1082 devm_kfree(smd->dev, channel);
1084 return ERR_PTR(ret);
1088 * Scans the allocation table for any newly allocated channels, calls
1089 * qcom_smd_create_channel() to create representations of these and add
1090 * them to the edge's list of channels.
1092 static void qcom_discover_channels(struct qcom_smd_edge *edge)
1094 struct qcom_smd_alloc_entry *alloc_tbl;
1095 struct qcom_smd_alloc_entry *entry;
1096 struct qcom_smd_channel *channel;
1097 struct qcom_smd *smd = edge->smd;
1098 unsigned long flags;
1099 unsigned fifo_id;
1100 unsigned info_id;
1101 int tbl;
1102 int i;
1103 u32 eflags, cid;
1105 for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) {
1106 alloc_tbl = qcom_smem_get(edge->remote_pid,
1107 smem_items[tbl].alloc_tbl_id, NULL);
1108 if (IS_ERR(alloc_tbl))
1109 continue;
1111 for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) {
1112 entry = &alloc_tbl[i];
1113 eflags = le32_to_cpu(entry->flags);
1114 if (test_bit(i, edge->allocated[tbl]))
1115 continue;
1117 if (entry->ref_count == 0)
1118 continue;
1120 if (!entry->name[0])
1121 continue;
1123 if (!(eflags & SMD_CHANNEL_FLAGS_PACKET))
1124 continue;
1126 if ((eflags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id)
1127 continue;
1129 cid = le32_to_cpu(entry->cid);
1130 info_id = smem_items[tbl].info_base_id + cid;
1131 fifo_id = smem_items[tbl].fifo_base_id + cid;
1133 channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
1134 if (IS_ERR(channel))
1135 continue;
1137 spin_lock_irqsave(&edge->channels_lock, flags);
1138 list_add(&channel->list, &edge->channels);
1139 spin_unlock_irqrestore(&edge->channels_lock, flags);
1141 dev_dbg(smd->dev, "new channel found: '%s'\n", channel->name);
1142 set_bit(i, edge->allocated[tbl]);
1146 schedule_work(&edge->work);
1150 * This per edge worker scans smem for any new channels and register these. It
1151 * then scans all registered channels for state changes that should be handled
1152 * by creating or destroying smd client devices for the registered channels.
1154 * LOCKING: edge->channels_lock is not needed to be held during the traversal
1155 * of the channels list as it's done synchronously with the only writer.
1157 static void qcom_channel_state_worker(struct work_struct *work)
1159 struct qcom_smd_channel *channel;
1160 struct qcom_smd_edge *edge = container_of(work,
1161 struct qcom_smd_edge,
1162 work);
1163 unsigned remote_state;
1166 * Rescan smem if we have reason to belive that there are new channels.
1168 if (edge->need_rescan) {
1169 edge->need_rescan = false;
1170 qcom_discover_channels(edge);
1174 * Register a device for any closed channel where the remote processor
1175 * is showing interest in opening the channel.
1177 list_for_each_entry(channel, &edge->channels, list) {
1178 if (channel->state != SMD_CHANNEL_CLOSED)
1179 continue;
1181 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1182 if (remote_state != SMD_CHANNEL_OPENING &&
1183 remote_state != SMD_CHANNEL_OPENED)
1184 continue;
1186 qcom_smd_create_device(channel);
1190 * Unregister the device for any channel that is opened where the
1191 * remote processor is closing the channel.
1193 list_for_each_entry(channel, &edge->channels, list) {
1194 if (channel->state != SMD_CHANNEL_OPENING &&
1195 channel->state != SMD_CHANNEL_OPENED)
1196 continue;
1198 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1199 if (remote_state == SMD_CHANNEL_OPENING ||
1200 remote_state == SMD_CHANNEL_OPENED)
1201 continue;
1203 qcom_smd_destroy_device(channel);
1208 * Parses an of_node describing an edge.
1210 static int qcom_smd_parse_edge(struct device *dev,
1211 struct device_node *node,
1212 struct qcom_smd_edge *edge)
1214 struct device_node *syscon_np;
1215 const char *key;
1216 int irq;
1217 int ret;
1219 INIT_LIST_HEAD(&edge->channels);
1220 spin_lock_init(&edge->channels_lock);
1222 INIT_WORK(&edge->work, qcom_channel_state_worker);
1224 edge->of_node = of_node_get(node);
1226 irq = irq_of_parse_and_map(node, 0);
1227 if (irq < 0) {
1228 dev_err(dev, "required smd interrupt missing\n");
1229 return -EINVAL;
1232 ret = devm_request_irq(dev, irq,
1233 qcom_smd_edge_intr, IRQF_TRIGGER_RISING,
1234 node->name, edge);
1235 if (ret) {
1236 dev_err(dev, "failed to request smd irq\n");
1237 return ret;
1240 edge->irq = irq;
1242 key = "qcom,smd-edge";
1243 ret = of_property_read_u32(node, key, &edge->edge_id);
1244 if (ret) {
1245 dev_err(dev, "edge missing %s property\n", key);
1246 return -EINVAL;
1249 edge->remote_pid = QCOM_SMEM_HOST_ANY;
1250 key = "qcom,remote-pid";
1251 of_property_read_u32(node, key, &edge->remote_pid);
1253 syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
1254 if (!syscon_np) {
1255 dev_err(dev, "no qcom,ipc node\n");
1256 return -ENODEV;
1259 edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
1260 if (IS_ERR(edge->ipc_regmap))
1261 return PTR_ERR(edge->ipc_regmap);
1263 key = "qcom,ipc";
1264 ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
1265 if (ret < 0) {
1266 dev_err(dev, "no offset in %s\n", key);
1267 return -EINVAL;
1270 ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
1271 if (ret < 0) {
1272 dev_err(dev, "no bit in %s\n", key);
1273 return -EINVAL;
1276 return 0;
1279 static int qcom_smd_probe(struct platform_device *pdev)
1281 struct qcom_smd_edge *edge;
1282 struct device_node *node;
1283 struct qcom_smd *smd;
1284 size_t array_size;
1285 int num_edges;
1286 int ret;
1287 int i = 0;
1288 void *p;
1290 /* Wait for smem */
1291 p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL);
1292 if (PTR_ERR(p) == -EPROBE_DEFER)
1293 return PTR_ERR(p);
1295 num_edges = of_get_available_child_count(pdev->dev.of_node);
1296 array_size = sizeof(*smd) + num_edges * sizeof(struct qcom_smd_edge);
1297 smd = devm_kzalloc(&pdev->dev, array_size, GFP_KERNEL);
1298 if (!smd)
1299 return -ENOMEM;
1300 smd->dev = &pdev->dev;
1302 smd->num_edges = num_edges;
1303 for_each_available_child_of_node(pdev->dev.of_node, node) {
1304 edge = &smd->edges[i++];
1305 edge->smd = smd;
1307 ret = qcom_smd_parse_edge(&pdev->dev, node, edge);
1308 if (ret)
1309 continue;
1311 edge->need_rescan = true;
1312 schedule_work(&edge->work);
1315 platform_set_drvdata(pdev, smd);
1317 return 0;
1321 * Shut down all smd clients by making sure that each edge stops processing
1322 * events and scanning for new channels, then call destroy on the devices.
1324 static int qcom_smd_remove(struct platform_device *pdev)
1326 struct qcom_smd_channel *channel;
1327 struct qcom_smd_edge *edge;
1328 struct qcom_smd *smd = platform_get_drvdata(pdev);
1329 int i;
1331 for (i = 0; i < smd->num_edges; i++) {
1332 edge = &smd->edges[i];
1334 disable_irq(edge->irq);
1335 cancel_work_sync(&edge->work);
1337 list_for_each_entry(channel, &edge->channels, list) {
1338 if (!channel->qsdev)
1339 continue;
1341 qcom_smd_destroy_device(channel);
1345 return 0;
1348 static const struct of_device_id qcom_smd_of_match[] = {
1349 { .compatible = "qcom,smd" },
1352 MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
1354 static struct platform_driver qcom_smd_driver = {
1355 .probe = qcom_smd_probe,
1356 .remove = qcom_smd_remove,
1357 .driver = {
1358 .name = "qcom-smd",
1359 .of_match_table = qcom_smd_of_match,
1363 static int __init qcom_smd_init(void)
1365 int ret;
1367 ret = bus_register(&qcom_smd_bus);
1368 if (ret) {
1369 pr_err("failed to register smd bus: %d\n", ret);
1370 return ret;
1373 return platform_driver_register(&qcom_smd_driver);
1375 postcore_initcall(qcom_smd_init);
1377 static void __exit qcom_smd_exit(void)
1379 platform_driver_unregister(&qcom_smd_driver);
1380 bus_unregister(&qcom_smd_bus);
1382 module_exit(qcom_smd_exit);
1384 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
1385 MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
1386 MODULE_LICENSE("GPL v2");