sh_eth: R8A7740 supports packet shecksumming
[linux/fpc-iii.git] / drivers / rpmsg / qcom_smd.c
blob0fae48116a0d51d8d78518b48ad871caa2645344
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/smem.h>
26 #include <linux/wait.h>
27 #include <linux/rpmsg.h>
28 #include <linux/rpmsg/qcom_smd.h>
30 #include "rpmsg_internal.h"
33 * The Qualcomm Shared Memory communication solution provides point-to-point
34 * channels for clients to send and receive streaming or packet based data.
36 * Each channel consists of a control item (channel info) and a ring buffer
37 * pair. The channel info carry information related to channel state, flow
38 * control and the offsets within the ring buffer.
40 * All allocated channels are listed in an allocation table, identifying the
41 * pair of items by name, type and remote processor.
43 * Upon creating a new channel the remote processor allocates channel info and
44 * ring buffer items from the smem heap and populate the allocation table. An
45 * interrupt is sent to the other end of the channel and a scan for new
46 * channels should be done. A channel never goes away, it will only change
47 * state.
49 * The remote processor signals it intent for bring up the communication
50 * channel by setting the state of its end of the channel to "opening" and
51 * sends out an interrupt. We detect this change and register a smd device to
52 * consume the channel. Upon finding a consumer we finish the handshake and the
53 * channel is up.
55 * Upon closing a channel, the remote processor will update the state of its
56 * end of the channel and signal us, we will then unregister any attached
57 * device and close our end of the channel.
59 * Devices attached to a channel can use the qcom_smd_send function to push
60 * data to the channel, this is done by copying the data into the tx ring
61 * buffer, updating the pointers in the channel info and signaling the remote
62 * processor.
64 * The remote processor does the equivalent when it transfer data and upon
65 * receiving the interrupt we check the channel info for new data and delivers
66 * this to the attached device. If the device is not ready to receive the data
67 * we leave it in the ring buffer for now.
70 struct smd_channel_info;
71 struct smd_channel_info_pair;
72 struct smd_channel_info_word;
73 struct smd_channel_info_word_pair;
75 static const struct rpmsg_endpoint_ops qcom_smd_endpoint_ops;
77 #define SMD_ALLOC_TBL_COUNT 2
78 #define SMD_ALLOC_TBL_SIZE 64
81 * This lists the various smem heap items relevant for the allocation table and
82 * smd channel entries.
84 static const struct {
85 unsigned alloc_tbl_id;
86 unsigned info_base_id;
87 unsigned fifo_base_id;
88 } smem_items[SMD_ALLOC_TBL_COUNT] = {
90 .alloc_tbl_id = 13,
91 .info_base_id = 14,
92 .fifo_base_id = 338
95 .alloc_tbl_id = 266,
96 .info_base_id = 138,
97 .fifo_base_id = 202,
102 * struct qcom_smd_edge - representing a remote processor
103 * @of_node: of_node handle for information related to this edge
104 * @edge_id: identifier of this edge
105 * @remote_pid: identifier of remote processor
106 * @irq: interrupt for signals on this edge
107 * @ipc_regmap: regmap handle holding the outgoing ipc register
108 * @ipc_offset: offset within @ipc_regmap of the register for ipc
109 * @ipc_bit: bit in the register at @ipc_offset of @ipc_regmap
110 * @channels: list of all channels detected on this edge
111 * @channels_lock: guard for modifications of @channels
112 * @allocated: array of bitmaps representing already allocated channels
113 * @smem_available: last available amount of smem triggering a channel scan
114 * @scan_work: work item for discovering new channels
115 * @state_work: work item for edge state changes
117 struct qcom_smd_edge {
118 struct device dev;
120 struct device_node *of_node;
121 unsigned edge_id;
122 unsigned remote_pid;
124 int irq;
126 struct regmap *ipc_regmap;
127 int ipc_offset;
128 int ipc_bit;
130 struct list_head channels;
131 spinlock_t channels_lock;
133 DECLARE_BITMAP(allocated[SMD_ALLOC_TBL_COUNT], SMD_ALLOC_TBL_SIZE);
135 unsigned smem_available;
137 wait_queue_head_t new_channel_event;
139 struct work_struct scan_work;
140 struct work_struct state_work;
144 * SMD channel states.
146 enum smd_channel_state {
147 SMD_CHANNEL_CLOSED,
148 SMD_CHANNEL_OPENING,
149 SMD_CHANNEL_OPENED,
150 SMD_CHANNEL_FLUSHING,
151 SMD_CHANNEL_CLOSING,
152 SMD_CHANNEL_RESET,
153 SMD_CHANNEL_RESET_OPENING
156 struct qcom_smd_device {
157 struct rpmsg_device rpdev;
159 struct qcom_smd_edge *edge;
162 struct qcom_smd_endpoint {
163 struct rpmsg_endpoint ept;
165 struct qcom_smd_channel *qsch;
168 #define to_smd_device(_rpdev) container_of(_rpdev, struct qcom_smd_device, rpdev)
169 #define to_smd_edge(d) container_of(d, struct qcom_smd_edge, dev)
170 #define to_smd_endpoint(ept) container_of(ept, struct qcom_smd_endpoint, ept)
173 * struct qcom_smd_channel - smd channel struct
174 * @edge: qcom_smd_edge this channel is living on
175 * @qsdev: reference to a associated smd client device
176 * @name: name of the channel
177 * @state: local state of the channel
178 * @remote_state: remote state of the channel
179 * @info: byte aligned outgoing/incoming channel info
180 * @info_word: word aligned outgoing/incoming channel info
181 * @tx_lock: lock to make writes to the channel mutually exclusive
182 * @fblockread_event: wakeup event tied to tx fBLOCKREADINTR
183 * @tx_fifo: pointer to the outgoing ring buffer
184 * @rx_fifo: pointer to the incoming ring buffer
185 * @fifo_size: size of each ring buffer
186 * @bounce_buffer: bounce buffer for reading wrapped packets
187 * @cb: callback function registered for this channel
188 * @recv_lock: guard for rx info modifications and cb pointer
189 * @pkt_size: size of the currently handled packet
190 * @list: lite entry for @channels in qcom_smd_edge
192 struct qcom_smd_channel {
193 struct qcom_smd_edge *edge;
195 struct qcom_smd_endpoint *qsept;
196 bool registered;
198 char *name;
199 enum smd_channel_state state;
200 enum smd_channel_state remote_state;
202 struct smd_channel_info_pair *info;
203 struct smd_channel_info_word_pair *info_word;
205 struct mutex tx_lock;
206 wait_queue_head_t fblockread_event;
208 void *tx_fifo;
209 void *rx_fifo;
210 int fifo_size;
212 void *bounce_buffer;
214 spinlock_t recv_lock;
216 int pkt_size;
218 void *drvdata;
220 struct list_head list;
224 * Format of the smd_info smem items, for byte aligned channels.
226 struct smd_channel_info {
227 __le32 state;
228 u8 fDSR;
229 u8 fCTS;
230 u8 fCD;
231 u8 fRI;
232 u8 fHEAD;
233 u8 fTAIL;
234 u8 fSTATE;
235 u8 fBLOCKREADINTR;
236 __le32 tail;
237 __le32 head;
240 struct smd_channel_info_pair {
241 struct smd_channel_info tx;
242 struct smd_channel_info rx;
246 * Format of the smd_info smem items, for word aligned channels.
248 struct smd_channel_info_word {
249 __le32 state;
250 __le32 fDSR;
251 __le32 fCTS;
252 __le32 fCD;
253 __le32 fRI;
254 __le32 fHEAD;
255 __le32 fTAIL;
256 __le32 fSTATE;
257 __le32 fBLOCKREADINTR;
258 __le32 tail;
259 __le32 head;
262 struct smd_channel_info_word_pair {
263 struct smd_channel_info_word tx;
264 struct smd_channel_info_word rx;
267 #define GET_RX_CHANNEL_FLAG(channel, param) \
268 ({ \
269 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
270 channel->info_word ? \
271 le32_to_cpu(channel->info_word->rx.param) : \
272 channel->info->rx.param; \
275 #define GET_RX_CHANNEL_INFO(channel, param) \
276 ({ \
277 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
278 le32_to_cpu(channel->info_word ? \
279 channel->info_word->rx.param : \
280 channel->info->rx.param); \
283 #define SET_RX_CHANNEL_FLAG(channel, param, value) \
284 ({ \
285 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
286 if (channel->info_word) \
287 channel->info_word->rx.param = cpu_to_le32(value); \
288 else \
289 channel->info->rx.param = value; \
292 #define SET_RX_CHANNEL_INFO(channel, param, value) \
293 ({ \
294 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
295 if (channel->info_word) \
296 channel->info_word->rx.param = cpu_to_le32(value); \
297 else \
298 channel->info->rx.param = cpu_to_le32(value); \
301 #define GET_TX_CHANNEL_FLAG(channel, param) \
302 ({ \
303 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
304 channel->info_word ? \
305 le32_to_cpu(channel->info_word->tx.param) : \
306 channel->info->tx.param; \
309 #define GET_TX_CHANNEL_INFO(channel, param) \
310 ({ \
311 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
312 le32_to_cpu(channel->info_word ? \
313 channel->info_word->tx.param : \
314 channel->info->tx.param); \
317 #define SET_TX_CHANNEL_FLAG(channel, param, value) \
318 ({ \
319 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
320 if (channel->info_word) \
321 channel->info_word->tx.param = cpu_to_le32(value); \
322 else \
323 channel->info->tx.param = value; \
326 #define SET_TX_CHANNEL_INFO(channel, param, value) \
327 ({ \
328 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
329 if (channel->info_word) \
330 channel->info_word->tx.param = cpu_to_le32(value); \
331 else \
332 channel->info->tx.param = cpu_to_le32(value); \
336 * struct qcom_smd_alloc_entry - channel allocation entry
337 * @name: channel name
338 * @cid: channel index
339 * @flags: channel flags and edge id
340 * @ref_count: reference count of the channel
342 struct qcom_smd_alloc_entry {
343 u8 name[20];
344 __le32 cid;
345 __le32 flags;
346 __le32 ref_count;
347 } __packed;
349 #define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff
350 #define SMD_CHANNEL_FLAGS_STREAM BIT(8)
351 #define SMD_CHANNEL_FLAGS_PACKET BIT(9)
354 * Each smd packet contains a 20 byte header, with the first 4 being the length
355 * of the packet.
357 #define SMD_PACKET_HEADER_LEN 20
360 * Signal the remote processor associated with 'channel'.
362 static void qcom_smd_signal_channel(struct qcom_smd_channel *channel)
364 struct qcom_smd_edge *edge = channel->edge;
366 regmap_write(edge->ipc_regmap, edge->ipc_offset, BIT(edge->ipc_bit));
370 * Initialize the tx channel info
372 static void qcom_smd_channel_reset(struct qcom_smd_channel *channel)
374 SET_TX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
375 SET_TX_CHANNEL_FLAG(channel, fDSR, 0);
376 SET_TX_CHANNEL_FLAG(channel, fCTS, 0);
377 SET_TX_CHANNEL_FLAG(channel, fCD, 0);
378 SET_TX_CHANNEL_FLAG(channel, fRI, 0);
379 SET_TX_CHANNEL_FLAG(channel, fHEAD, 0);
380 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
381 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
382 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
383 SET_TX_CHANNEL_INFO(channel, head, 0);
384 SET_RX_CHANNEL_INFO(channel, tail, 0);
386 qcom_smd_signal_channel(channel);
388 channel->state = SMD_CHANNEL_CLOSED;
389 channel->pkt_size = 0;
393 * Set the callback for a channel, with appropriate locking
395 static void qcom_smd_channel_set_callback(struct qcom_smd_channel *channel,
396 rpmsg_rx_cb_t cb)
398 struct rpmsg_endpoint *ept = &channel->qsept->ept;
399 unsigned long flags;
401 spin_lock_irqsave(&channel->recv_lock, flags);
402 ept->cb = cb;
403 spin_unlock_irqrestore(&channel->recv_lock, flags);
407 * Calculate the amount of data available in the rx fifo
409 static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel *channel)
411 unsigned head;
412 unsigned tail;
414 head = GET_RX_CHANNEL_INFO(channel, head);
415 tail = GET_RX_CHANNEL_INFO(channel, tail);
417 return (head - tail) & (channel->fifo_size - 1);
421 * Set tx channel state and inform the remote processor
423 static void qcom_smd_channel_set_state(struct qcom_smd_channel *channel,
424 int state)
426 struct qcom_smd_edge *edge = channel->edge;
427 bool is_open = state == SMD_CHANNEL_OPENED;
429 if (channel->state == state)
430 return;
432 dev_dbg(&edge->dev, "set_state(%s, %d)\n", channel->name, state);
434 SET_TX_CHANNEL_FLAG(channel, fDSR, is_open);
435 SET_TX_CHANNEL_FLAG(channel, fCTS, is_open);
436 SET_TX_CHANNEL_FLAG(channel, fCD, is_open);
438 SET_TX_CHANNEL_INFO(channel, state, state);
439 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
441 channel->state = state;
442 qcom_smd_signal_channel(channel);
446 * Copy count bytes of data using 32bit accesses, if that's required.
448 static void smd_copy_to_fifo(void __iomem *dst,
449 const void *src,
450 size_t count,
451 bool word_aligned)
453 if (word_aligned) {
454 __iowrite32_copy(dst, src, count / sizeof(u32));
455 } else {
456 memcpy_toio(dst, src, count);
461 * Copy count bytes of data using 32bit accesses, if that is required.
463 static void smd_copy_from_fifo(void *dst,
464 const void __iomem *src,
465 size_t count,
466 bool word_aligned)
468 if (word_aligned) {
469 __ioread32_copy(dst, src, count / sizeof(u32));
470 } else {
471 memcpy_fromio(dst, src, count);
476 * Read count bytes of data from the rx fifo into buf, but don't advance the
477 * tail.
479 static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel,
480 void *buf, size_t count)
482 bool word_aligned;
483 unsigned tail;
484 size_t len;
486 word_aligned = channel->info_word;
487 tail = GET_RX_CHANNEL_INFO(channel, tail);
489 len = min_t(size_t, count, channel->fifo_size - tail);
490 if (len) {
491 smd_copy_from_fifo(buf,
492 channel->rx_fifo + tail,
493 len,
494 word_aligned);
497 if (len != count) {
498 smd_copy_from_fifo(buf + len,
499 channel->rx_fifo,
500 count - len,
501 word_aligned);
504 return count;
508 * Advance the rx tail by count bytes.
510 static void qcom_smd_channel_advance(struct qcom_smd_channel *channel,
511 size_t count)
513 unsigned tail;
515 tail = GET_RX_CHANNEL_INFO(channel, tail);
516 tail += count;
517 tail &= (channel->fifo_size - 1);
518 SET_RX_CHANNEL_INFO(channel, tail, tail);
522 * Read out a single packet from the rx fifo and deliver it to the device
524 static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
526 struct rpmsg_endpoint *ept = &channel->qsept->ept;
527 unsigned tail;
528 size_t len;
529 void *ptr;
530 int ret;
532 tail = GET_RX_CHANNEL_INFO(channel, tail);
534 /* Use bounce buffer if the data wraps */
535 if (tail + channel->pkt_size >= channel->fifo_size) {
536 ptr = channel->bounce_buffer;
537 len = qcom_smd_channel_peek(channel, ptr, channel->pkt_size);
538 } else {
539 ptr = channel->rx_fifo + tail;
540 len = channel->pkt_size;
543 ret = ept->cb(ept->rpdev, ptr, len, ept->priv, RPMSG_ADDR_ANY);
544 if (ret < 0)
545 return ret;
547 /* Only forward the tail if the client consumed the data */
548 qcom_smd_channel_advance(channel, len);
550 channel->pkt_size = 0;
552 return 0;
556 * Per channel interrupt handling
558 static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel)
560 bool need_state_scan = false;
561 int remote_state;
562 __le32 pktlen;
563 int avail;
564 int ret;
566 /* Handle state changes */
567 remote_state = GET_RX_CHANNEL_INFO(channel, state);
568 if (remote_state != channel->remote_state) {
569 channel->remote_state = remote_state;
570 need_state_scan = true;
572 /* Indicate that we have seen any state change */
573 SET_RX_CHANNEL_FLAG(channel, fSTATE, 0);
575 /* Signal waiting qcom_smd_send() about the interrupt */
576 if (!GET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR))
577 wake_up_interruptible(&channel->fblockread_event);
579 /* Don't consume any data until we've opened the channel */
580 if (channel->state != SMD_CHANNEL_OPENED)
581 goto out;
583 /* Indicate that we've seen the new data */
584 SET_RX_CHANNEL_FLAG(channel, fHEAD, 0);
586 /* Consume data */
587 for (;;) {
588 avail = qcom_smd_channel_get_rx_avail(channel);
590 if (!channel->pkt_size && avail >= SMD_PACKET_HEADER_LEN) {
591 qcom_smd_channel_peek(channel, &pktlen, sizeof(pktlen));
592 qcom_smd_channel_advance(channel, SMD_PACKET_HEADER_LEN);
593 channel->pkt_size = le32_to_cpu(pktlen);
594 } else if (channel->pkt_size && avail >= channel->pkt_size) {
595 ret = qcom_smd_channel_recv_single(channel);
596 if (ret)
597 break;
598 } else {
599 break;
603 /* Indicate that we have seen and updated tail */
604 SET_RX_CHANNEL_FLAG(channel, fTAIL, 1);
606 /* Signal the remote that we've consumed the data (if requested) */
607 if (!GET_RX_CHANNEL_FLAG(channel, fBLOCKREADINTR)) {
608 /* Ensure ordering of channel info updates */
609 wmb();
611 qcom_smd_signal_channel(channel);
614 out:
615 return need_state_scan;
619 * The edge interrupts are triggered by the remote processor on state changes,
620 * channel info updates or when new channels are created.
622 static irqreturn_t qcom_smd_edge_intr(int irq, void *data)
624 struct qcom_smd_edge *edge = data;
625 struct qcom_smd_channel *channel;
626 unsigned available;
627 bool kick_scanner = false;
628 bool kick_state = false;
631 * Handle state changes or data on each of the channels on this edge
633 spin_lock(&edge->channels_lock);
634 list_for_each_entry(channel, &edge->channels, list) {
635 spin_lock(&channel->recv_lock);
636 kick_state |= qcom_smd_channel_intr(channel);
637 spin_unlock(&channel->recv_lock);
639 spin_unlock(&edge->channels_lock);
642 * Creating a new channel requires allocating an smem entry, so we only
643 * have to scan if the amount of available space in smem have changed
644 * since last scan.
646 available = qcom_smem_get_free_space(edge->remote_pid);
647 if (available != edge->smem_available) {
648 edge->smem_available = available;
649 kick_scanner = true;
652 if (kick_scanner)
653 schedule_work(&edge->scan_work);
654 if (kick_state)
655 schedule_work(&edge->state_work);
657 return IRQ_HANDLED;
661 * Calculate how much space is available in the tx fifo.
663 static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel)
665 unsigned head;
666 unsigned tail;
667 unsigned mask = channel->fifo_size - 1;
669 head = GET_TX_CHANNEL_INFO(channel, head);
670 tail = GET_TX_CHANNEL_INFO(channel, tail);
672 return mask - ((head - tail) & mask);
676 * Write count bytes of data into channel, possibly wrapping in the ring buffer
678 static int qcom_smd_write_fifo(struct qcom_smd_channel *channel,
679 const void *data,
680 size_t count)
682 bool word_aligned;
683 unsigned head;
684 size_t len;
686 word_aligned = channel->info_word;
687 head = GET_TX_CHANNEL_INFO(channel, head);
689 len = min_t(size_t, count, channel->fifo_size - head);
690 if (len) {
691 smd_copy_to_fifo(channel->tx_fifo + head,
692 data,
693 len,
694 word_aligned);
697 if (len != count) {
698 smd_copy_to_fifo(channel->tx_fifo,
699 data + len,
700 count - len,
701 word_aligned);
704 head += count;
705 head &= (channel->fifo_size - 1);
706 SET_TX_CHANNEL_INFO(channel, head, head);
708 return count;
712 * qcom_smd_send - write data to smd channel
713 * @channel: channel handle
714 * @data: buffer of data to write
715 * @len: number of bytes to write
717 * This is a blocking write of len bytes into the channel's tx ring buffer and
718 * signal the remote end. It will sleep until there is enough space available
719 * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
720 * polling.
722 static int __qcom_smd_send(struct qcom_smd_channel *channel, const void *data,
723 int len, bool wait)
725 __le32 hdr[5] = { cpu_to_le32(len), };
726 int tlen = sizeof(hdr) + len;
727 int ret;
729 /* Word aligned channels only accept word size aligned data */
730 if (channel->info_word && len % 4)
731 return -EINVAL;
733 /* Reject packets that are too big */
734 if (tlen >= channel->fifo_size)
735 return -EINVAL;
737 ret = mutex_lock_interruptible(&channel->tx_lock);
738 if (ret)
739 return ret;
741 while (qcom_smd_get_tx_avail(channel) < tlen) {
742 if (!wait) {
743 ret = -EAGAIN;
744 goto out;
747 if (channel->state != SMD_CHANNEL_OPENED) {
748 ret = -EPIPE;
749 goto out;
752 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 0);
754 ret = wait_event_interruptible(channel->fblockread_event,
755 qcom_smd_get_tx_avail(channel) >= tlen ||
756 channel->state != SMD_CHANNEL_OPENED);
757 if (ret)
758 goto out;
760 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
763 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
765 qcom_smd_write_fifo(channel, hdr, sizeof(hdr));
766 qcom_smd_write_fifo(channel, data, len);
768 SET_TX_CHANNEL_FLAG(channel, fHEAD, 1);
770 /* Ensure ordering of channel info updates */
771 wmb();
773 qcom_smd_signal_channel(channel);
775 out:
776 mutex_unlock(&channel->tx_lock);
778 return ret;
782 * Helper for opening a channel
784 static int qcom_smd_channel_open(struct qcom_smd_channel *channel,
785 rpmsg_rx_cb_t cb)
787 size_t bb_size;
790 * Packets are maximum 4k, but reduce if the fifo is smaller
792 bb_size = min(channel->fifo_size, SZ_4K);
793 channel->bounce_buffer = kmalloc(bb_size, GFP_KERNEL);
794 if (!channel->bounce_buffer)
795 return -ENOMEM;
797 qcom_smd_channel_set_callback(channel, cb);
798 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENING);
799 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED);
801 return 0;
805 * Helper for closing and resetting a channel
807 static void qcom_smd_channel_close(struct qcom_smd_channel *channel)
809 qcom_smd_channel_set_callback(channel, NULL);
811 kfree(channel->bounce_buffer);
812 channel->bounce_buffer = NULL;
814 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
815 qcom_smd_channel_reset(channel);
818 static struct qcom_smd_channel *
819 qcom_smd_find_channel(struct qcom_smd_edge *edge, const char *name)
821 struct qcom_smd_channel *channel;
822 struct qcom_smd_channel *ret = NULL;
823 unsigned long flags;
825 spin_lock_irqsave(&edge->channels_lock, flags);
826 list_for_each_entry(channel, &edge->channels, list) {
827 if (!strcmp(channel->name, name)) {
828 ret = channel;
829 break;
832 spin_unlock_irqrestore(&edge->channels_lock, flags);
834 return ret;
837 static void __ept_release(struct kref *kref)
839 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
840 refcount);
841 kfree(to_smd_endpoint(ept));
844 static struct rpmsg_endpoint *qcom_smd_create_ept(struct rpmsg_device *rpdev,
845 rpmsg_rx_cb_t cb, void *priv,
846 struct rpmsg_channel_info chinfo)
848 struct qcom_smd_endpoint *qsept;
849 struct qcom_smd_channel *channel;
850 struct qcom_smd_device *qsdev = to_smd_device(rpdev);
851 struct qcom_smd_edge *edge = qsdev->edge;
852 struct rpmsg_endpoint *ept;
853 const char *name = chinfo.name;
854 int ret;
856 /* Wait up to HZ for the channel to appear */
857 ret = wait_event_interruptible_timeout(edge->new_channel_event,
858 (channel = qcom_smd_find_channel(edge, name)) != NULL,
859 HZ);
860 if (!ret)
861 return NULL;
863 if (channel->state != SMD_CHANNEL_CLOSED) {
864 dev_err(&rpdev->dev, "channel %s is busy\n", channel->name);
865 return NULL;
868 qsept = kzalloc(sizeof(*qsept), GFP_KERNEL);
869 if (!qsept)
870 return NULL;
872 ept = &qsept->ept;
874 kref_init(&ept->refcount);
876 ept->rpdev = rpdev;
877 ept->cb = cb;
878 ept->priv = priv;
879 ept->ops = &qcom_smd_endpoint_ops;
881 channel->qsept = qsept;
882 qsept->qsch = channel;
884 ret = qcom_smd_channel_open(channel, cb);
885 if (ret)
886 goto free_ept;
888 return ept;
890 free_ept:
891 channel->qsept = NULL;
892 kref_put(&ept->refcount, __ept_release);
893 return NULL;
896 static void qcom_smd_destroy_ept(struct rpmsg_endpoint *ept)
898 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
899 struct qcom_smd_channel *ch = qsept->qsch;
901 qcom_smd_channel_close(ch);
902 ch->qsept = NULL;
903 kref_put(&ept->refcount, __ept_release);
906 static int qcom_smd_send(struct rpmsg_endpoint *ept, void *data, int len)
908 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
910 return __qcom_smd_send(qsept->qsch, data, len, true);
913 static int qcom_smd_trysend(struct rpmsg_endpoint *ept, void *data, int len)
915 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
917 return __qcom_smd_send(qsept->qsch, data, len, false);
921 * Finds the device_node for the smd child interested in this channel.
923 static struct device_node *qcom_smd_match_channel(struct device_node *edge_node,
924 const char *channel)
926 struct device_node *child;
927 const char *name;
928 const char *key;
929 int ret;
931 for_each_available_child_of_node(edge_node, child) {
932 key = "qcom,smd-channels";
933 ret = of_property_read_string(child, key, &name);
934 if (ret)
935 continue;
937 if (strcmp(name, channel) == 0)
938 return child;
941 return NULL;
944 static const struct rpmsg_device_ops qcom_smd_device_ops = {
945 .create_ept = qcom_smd_create_ept,
948 static const struct rpmsg_endpoint_ops qcom_smd_endpoint_ops = {
949 .destroy_ept = qcom_smd_destroy_ept,
950 .send = qcom_smd_send,
951 .trysend = qcom_smd_trysend,
955 * Create a smd client device for channel that is being opened.
957 static int qcom_smd_create_device(struct qcom_smd_channel *channel)
959 struct qcom_smd_device *qsdev;
960 struct rpmsg_device *rpdev;
961 struct qcom_smd_edge *edge = channel->edge;
963 dev_dbg(&edge->dev, "registering '%s'\n", channel->name);
965 qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
966 if (!qsdev)
967 return -ENOMEM;
969 /* Link qsdev to our SMD edge */
970 qsdev->edge = edge;
972 /* Assign callbacks for rpmsg_device */
973 qsdev->rpdev.ops = &qcom_smd_device_ops;
975 /* Assign public information to the rpmsg_device */
976 rpdev = &qsdev->rpdev;
977 strncpy(rpdev->id.name, channel->name, RPMSG_NAME_SIZE);
978 rpdev->src = RPMSG_ADDR_ANY;
979 rpdev->dst = RPMSG_ADDR_ANY;
981 rpdev->dev.of_node = qcom_smd_match_channel(edge->of_node, channel->name);
982 rpdev->dev.parent = &edge->dev;
984 return rpmsg_register_device(rpdev);
988 * Allocate the qcom_smd_channel object for a newly found smd channel,
989 * retrieving and validating the smem items involved.
991 static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge,
992 unsigned smem_info_item,
993 unsigned smem_fifo_item,
994 char *name)
996 struct qcom_smd_channel *channel;
997 size_t fifo_size;
998 size_t info_size;
999 void *fifo_base;
1000 void *info;
1001 int ret;
1003 channel = devm_kzalloc(&edge->dev, sizeof(*channel), GFP_KERNEL);
1004 if (!channel)
1005 return ERR_PTR(-ENOMEM);
1007 channel->edge = edge;
1008 channel->name = devm_kstrdup(&edge->dev, name, GFP_KERNEL);
1009 if (!channel->name)
1010 return ERR_PTR(-ENOMEM);
1012 mutex_init(&channel->tx_lock);
1013 spin_lock_init(&channel->recv_lock);
1014 init_waitqueue_head(&channel->fblockread_event);
1016 info = qcom_smem_get(edge->remote_pid, smem_info_item, &info_size);
1017 if (IS_ERR(info)) {
1018 ret = PTR_ERR(info);
1019 goto free_name_and_channel;
1023 * Use the size of the item to figure out which channel info struct to
1024 * use.
1026 if (info_size == 2 * sizeof(struct smd_channel_info_word)) {
1027 channel->info_word = info;
1028 } else if (info_size == 2 * sizeof(struct smd_channel_info)) {
1029 channel->info = info;
1030 } else {
1031 dev_err(&edge->dev,
1032 "channel info of size %zu not supported\n", info_size);
1033 ret = -EINVAL;
1034 goto free_name_and_channel;
1037 fifo_base = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_size);
1038 if (IS_ERR(fifo_base)) {
1039 ret = PTR_ERR(fifo_base);
1040 goto free_name_and_channel;
1043 /* The channel consist of a rx and tx fifo of equal size */
1044 fifo_size /= 2;
1046 dev_dbg(&edge->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1047 name, info_size, fifo_size);
1049 channel->tx_fifo = fifo_base;
1050 channel->rx_fifo = fifo_base + fifo_size;
1051 channel->fifo_size = fifo_size;
1053 qcom_smd_channel_reset(channel);
1055 return channel;
1057 free_name_and_channel:
1058 devm_kfree(&edge->dev, channel->name);
1059 devm_kfree(&edge->dev, channel);
1061 return ERR_PTR(ret);
1065 * Scans the allocation table for any newly allocated channels, calls
1066 * qcom_smd_create_channel() to create representations of these and add
1067 * them to the edge's list of channels.
1069 static void qcom_channel_scan_worker(struct work_struct *work)
1071 struct qcom_smd_edge *edge = container_of(work, struct qcom_smd_edge, scan_work);
1072 struct qcom_smd_alloc_entry *alloc_tbl;
1073 struct qcom_smd_alloc_entry *entry;
1074 struct qcom_smd_channel *channel;
1075 unsigned long flags;
1076 unsigned fifo_id;
1077 unsigned info_id;
1078 int tbl;
1079 int i;
1080 u32 eflags, cid;
1082 for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) {
1083 alloc_tbl = qcom_smem_get(edge->remote_pid,
1084 smem_items[tbl].alloc_tbl_id, NULL);
1085 if (IS_ERR(alloc_tbl))
1086 continue;
1088 for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) {
1089 entry = &alloc_tbl[i];
1090 eflags = le32_to_cpu(entry->flags);
1091 if (test_bit(i, edge->allocated[tbl]))
1092 continue;
1094 if (entry->ref_count == 0)
1095 continue;
1097 if (!entry->name[0])
1098 continue;
1100 if (!(eflags & SMD_CHANNEL_FLAGS_PACKET))
1101 continue;
1103 if ((eflags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id)
1104 continue;
1106 cid = le32_to_cpu(entry->cid);
1107 info_id = smem_items[tbl].info_base_id + cid;
1108 fifo_id = smem_items[tbl].fifo_base_id + cid;
1110 channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
1111 if (IS_ERR(channel))
1112 continue;
1114 spin_lock_irqsave(&edge->channels_lock, flags);
1115 list_add(&channel->list, &edge->channels);
1116 spin_unlock_irqrestore(&edge->channels_lock, flags);
1118 dev_dbg(&edge->dev, "new channel found: '%s'\n", channel->name);
1119 set_bit(i, edge->allocated[tbl]);
1121 wake_up_interruptible(&edge->new_channel_event);
1125 schedule_work(&edge->state_work);
1129 * This per edge worker scans smem for any new channels and register these. It
1130 * then scans all registered channels for state changes that should be handled
1131 * by creating or destroying smd client devices for the registered channels.
1133 * LOCKING: edge->channels_lock only needs to cover the list operations, as the
1134 * worker is killed before any channels are deallocated
1136 static void qcom_channel_state_worker(struct work_struct *work)
1138 struct qcom_smd_channel *channel;
1139 struct qcom_smd_edge *edge = container_of(work,
1140 struct qcom_smd_edge,
1141 state_work);
1142 struct rpmsg_channel_info chinfo;
1143 unsigned remote_state;
1144 unsigned long flags;
1147 * Register a device for any closed channel where the remote processor
1148 * is showing interest in opening the channel.
1150 spin_lock_irqsave(&edge->channels_lock, flags);
1151 list_for_each_entry(channel, &edge->channels, list) {
1152 if (channel->state != SMD_CHANNEL_CLOSED)
1153 continue;
1155 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1156 if (remote_state != SMD_CHANNEL_OPENING &&
1157 remote_state != SMD_CHANNEL_OPENED)
1158 continue;
1160 if (channel->registered)
1161 continue;
1163 spin_unlock_irqrestore(&edge->channels_lock, flags);
1164 qcom_smd_create_device(channel);
1165 channel->registered = true;
1166 spin_lock_irqsave(&edge->channels_lock, flags);
1168 channel->registered = true;
1172 * Unregister the device for any channel that is opened where the
1173 * remote processor is closing the channel.
1175 list_for_each_entry(channel, &edge->channels, list) {
1176 if (channel->state != SMD_CHANNEL_OPENING &&
1177 channel->state != SMD_CHANNEL_OPENED)
1178 continue;
1180 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1181 if (remote_state == SMD_CHANNEL_OPENING ||
1182 remote_state == SMD_CHANNEL_OPENED)
1183 continue;
1185 spin_unlock_irqrestore(&edge->channels_lock, flags);
1187 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1188 chinfo.src = RPMSG_ADDR_ANY;
1189 chinfo.dst = RPMSG_ADDR_ANY;
1190 rpmsg_unregister_device(&edge->dev, &chinfo);
1191 channel->registered = false;
1192 spin_lock_irqsave(&edge->channels_lock, flags);
1194 spin_unlock_irqrestore(&edge->channels_lock, flags);
1198 * Parses an of_node describing an edge.
1200 static int qcom_smd_parse_edge(struct device *dev,
1201 struct device_node *node,
1202 struct qcom_smd_edge *edge)
1204 struct device_node *syscon_np;
1205 const char *key;
1206 int irq;
1207 int ret;
1209 INIT_LIST_HEAD(&edge->channels);
1210 spin_lock_init(&edge->channels_lock);
1212 INIT_WORK(&edge->scan_work, qcom_channel_scan_worker);
1213 INIT_WORK(&edge->state_work, qcom_channel_state_worker);
1215 edge->of_node = of_node_get(node);
1217 key = "qcom,smd-edge";
1218 ret = of_property_read_u32(node, key, &edge->edge_id);
1219 if (ret) {
1220 dev_err(dev, "edge missing %s property\n", key);
1221 return -EINVAL;
1224 edge->remote_pid = QCOM_SMEM_HOST_ANY;
1225 key = "qcom,remote-pid";
1226 of_property_read_u32(node, key, &edge->remote_pid);
1228 syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
1229 if (!syscon_np) {
1230 dev_err(dev, "no qcom,ipc node\n");
1231 return -ENODEV;
1234 edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
1235 if (IS_ERR(edge->ipc_regmap))
1236 return PTR_ERR(edge->ipc_regmap);
1238 key = "qcom,ipc";
1239 ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
1240 if (ret < 0) {
1241 dev_err(dev, "no offset in %s\n", key);
1242 return -EINVAL;
1245 ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
1246 if (ret < 0) {
1247 dev_err(dev, "no bit in %s\n", key);
1248 return -EINVAL;
1251 irq = irq_of_parse_and_map(node, 0);
1252 if (irq < 0) {
1253 dev_err(dev, "required smd interrupt missing\n");
1254 return -EINVAL;
1257 ret = devm_request_irq(dev, irq,
1258 qcom_smd_edge_intr, IRQF_TRIGGER_RISING,
1259 node->name, edge);
1260 if (ret) {
1261 dev_err(dev, "failed to request smd irq\n");
1262 return ret;
1265 edge->irq = irq;
1267 return 0;
1271 * Release function for an edge.
1272 * Reset the state of each associated channel and free the edge context.
1274 static void qcom_smd_edge_release(struct device *dev)
1276 struct qcom_smd_channel *channel;
1277 struct qcom_smd_edge *edge = to_smd_edge(dev);
1279 list_for_each_entry(channel, &edge->channels, list) {
1280 SET_RX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
1281 SET_RX_CHANNEL_INFO(channel, head, 0);
1282 SET_RX_CHANNEL_INFO(channel, tail, 0);
1285 kfree(edge);
1289 * qcom_smd_register_edge() - register an edge based on an device_node
1290 * @parent: parent device for the edge
1291 * @node: device_node describing the edge
1293 * Returns an edge reference, or negative ERR_PTR() on failure.
1295 struct qcom_smd_edge *qcom_smd_register_edge(struct device *parent,
1296 struct device_node *node)
1298 struct qcom_smd_edge *edge;
1299 int ret;
1301 edge = kzalloc(sizeof(*edge), GFP_KERNEL);
1302 if (!edge)
1303 return ERR_PTR(-ENOMEM);
1305 init_waitqueue_head(&edge->new_channel_event);
1307 edge->dev.parent = parent;
1308 edge->dev.release = qcom_smd_edge_release;
1309 dev_set_name(&edge->dev, "%s:%s", dev_name(parent), node->name);
1310 ret = device_register(&edge->dev);
1311 if (ret) {
1312 pr_err("failed to register smd edge\n");
1313 return ERR_PTR(ret);
1316 ret = qcom_smd_parse_edge(&edge->dev, node, edge);
1317 if (ret) {
1318 dev_err(&edge->dev, "failed to parse smd edge\n");
1319 goto unregister_dev;
1322 schedule_work(&edge->scan_work);
1324 return edge;
1326 unregister_dev:
1327 put_device(&edge->dev);
1328 return ERR_PTR(ret);
1330 EXPORT_SYMBOL(qcom_smd_register_edge);
1332 static int qcom_smd_remove_device(struct device *dev, void *data)
1334 device_unregister(dev);
1336 return 0;
1340 * qcom_smd_unregister_edge() - release an edge and its children
1341 * @edge: edge reference acquired from qcom_smd_register_edge
1343 int qcom_smd_unregister_edge(struct qcom_smd_edge *edge)
1345 int ret;
1347 disable_irq(edge->irq);
1348 cancel_work_sync(&edge->scan_work);
1349 cancel_work_sync(&edge->state_work);
1351 ret = device_for_each_child(&edge->dev, NULL, qcom_smd_remove_device);
1352 if (ret)
1353 dev_warn(&edge->dev, "can't remove smd device: %d\n", ret);
1355 device_unregister(&edge->dev);
1357 return 0;
1359 EXPORT_SYMBOL(qcom_smd_unregister_edge);
1361 static int qcom_smd_probe(struct platform_device *pdev)
1363 struct device_node *node;
1364 void *p;
1366 /* Wait for smem */
1367 p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL);
1368 if (PTR_ERR(p) == -EPROBE_DEFER)
1369 return PTR_ERR(p);
1371 for_each_available_child_of_node(pdev->dev.of_node, node)
1372 qcom_smd_register_edge(&pdev->dev, node);
1374 return 0;
1377 static int qcom_smd_remove_edge(struct device *dev, void *data)
1379 struct qcom_smd_edge *edge = to_smd_edge(dev);
1381 return qcom_smd_unregister_edge(edge);
1385 * Shut down all smd clients by making sure that each edge stops processing
1386 * events and scanning for new channels, then call destroy on the devices.
1388 static int qcom_smd_remove(struct platform_device *pdev)
1390 int ret;
1392 ret = device_for_each_child(&pdev->dev, NULL, qcom_smd_remove_edge);
1393 if (ret)
1394 dev_warn(&pdev->dev, "can't remove smd device: %d\n", ret);
1396 return ret;
1399 static const struct of_device_id qcom_smd_of_match[] = {
1400 { .compatible = "qcom,smd" },
1403 MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
1405 static struct platform_driver qcom_smd_driver = {
1406 .probe = qcom_smd_probe,
1407 .remove = qcom_smd_remove,
1408 .driver = {
1409 .name = "qcom-smd",
1410 .of_match_table = qcom_smd_of_match,
1414 static int __init qcom_smd_init(void)
1416 return platform_driver_register(&qcom_smd_driver);
1418 subsys_initcall(qcom_smd_init);
1420 static void __exit qcom_smd_exit(void)
1422 platform_driver_unregister(&qcom_smd_driver);
1424 module_exit(qcom_smd_exit);
1426 MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
1427 MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
1428 MODULE_LICENSE("GPL v2");