blob: 394a1b52e4712989090f7f27a79292a2ce43e3e7 [file] [log] [blame]
Bjorn Andersson53e28222016-09-01 15:28:09 -07001/*
2 * Copyright (c) 2015, Sony Mobile Communications AB.
3 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
4 *
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.
8 *
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.
13 */
14
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>
Bjorn Andersson8fc94722016-10-19 19:40:03 -070028#include <linux/rpmsg/qcom_smd.h>
Bjorn Andersson53e28222016-09-01 15:28:09 -070029
30#include "rpmsg_internal.h"
31
32/*
33 * The Qualcomm Shared Memory communication solution provides point-to-point
34 * channels for clients to send and receive streaming or packet based data.
35 *
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.
39 *
40 * All allocated channels are listed in an allocation table, identifying the
41 * pair of items by name, type and remote processor.
42 *
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.
48 *
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.
54 *
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.
58 *
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.
63 *
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.
68 */
69
70struct smd_channel_info;
71struct smd_channel_info_pair;
72struct smd_channel_info_word;
73struct smd_channel_info_word_pair;
74
75static const struct rpmsg_endpoint_ops qcom_smd_endpoint_ops;
76
77#define SMD_ALLOC_TBL_COUNT 2
78#define SMD_ALLOC_TBL_SIZE 64
79
80/*
81 * This lists the various smem heap items relevant for the allocation table and
82 * smd channel entries.
83 */
84static const struct {
85 unsigned alloc_tbl_id;
86 unsigned info_base_id;
87 unsigned fifo_base_id;
88} smem_items[SMD_ALLOC_TBL_COUNT] = {
89 {
90 .alloc_tbl_id = 13,
91 .info_base_id = 14,
92 .fifo_base_id = 338
93 },
94 {
95 .alloc_tbl_id = 266,
96 .info_base_id = 138,
97 .fifo_base_id = 202,
98 },
99};
100
101/**
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
116 */
117struct qcom_smd_edge {
118 struct device dev;
119
120 struct device_node *of_node;
121 unsigned edge_id;
122 unsigned remote_pid;
123
124 int irq;
125
126 struct regmap *ipc_regmap;
127 int ipc_offset;
128 int ipc_bit;
129
130 struct list_head channels;
131 spinlock_t channels_lock;
132
133 DECLARE_BITMAP(allocated[SMD_ALLOC_TBL_COUNT], SMD_ALLOC_TBL_SIZE);
134
135 unsigned smem_available;
136
137 wait_queue_head_t new_channel_event;
138
139 struct work_struct scan_work;
140 struct work_struct state_work;
141};
142
143/*
144 * SMD channel states.
145 */
146enum 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
154};
155
156struct qcom_smd_device {
157 struct rpmsg_device rpdev;
158
159 struct qcom_smd_edge *edge;
160};
161
162struct qcom_smd_endpoint {
163 struct rpmsg_endpoint ept;
164
165 struct qcom_smd_channel *qsch;
166};
167
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)
171
172/**
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
191 */
192struct qcom_smd_channel {
193 struct qcom_smd_edge *edge;
194
195 struct qcom_smd_endpoint *qsept;
196 bool registered;
197
198 char *name;
199 enum smd_channel_state state;
200 enum smd_channel_state remote_state;
201
202 struct smd_channel_info_pair *info;
203 struct smd_channel_info_word_pair *info_word;
204
205 struct mutex tx_lock;
206 wait_queue_head_t fblockread_event;
207
208 void *tx_fifo;
209 void *rx_fifo;
210 int fifo_size;
211
212 void *bounce_buffer;
213
214 spinlock_t recv_lock;
215
216 int pkt_size;
217
218 void *drvdata;
219
220 struct list_head list;
221};
222
223/*
224 * Format of the smd_info smem items, for byte aligned channels.
225 */
226struct 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;
238};
239
240struct smd_channel_info_pair {
241 struct smd_channel_info tx;
242 struct smd_channel_info rx;
243};
244
245/*
246 * Format of the smd_info smem items, for word aligned channels.
247 */
248struct 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;
260};
261
262struct smd_channel_info_word_pair {
263 struct smd_channel_info_word tx;
264 struct smd_channel_info_word rx;
265};
266
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; \
273 })
274
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); \
281 })
282
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; \
290 })
291
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); \
299 })
300
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; \
307 })
308
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); \
315 })
316
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; \
324 })
325
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); \
333 })
334
335/**
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
341 */
342struct qcom_smd_alloc_entry {
343 u8 name[20];
344 __le32 cid;
345 __le32 flags;
346 __le32 ref_count;
347} __packed;
348
349#define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff
350#define SMD_CHANNEL_FLAGS_STREAM BIT(8)
351#define SMD_CHANNEL_FLAGS_PACKET BIT(9)
352
353/*
354 * Each smd packet contains a 20 byte header, with the first 4 being the length
355 * of the packet.
356 */
357#define SMD_PACKET_HEADER_LEN 20
358
359/*
360 * Signal the remote processor associated with 'channel'.
361 */
362static void qcom_smd_signal_channel(struct qcom_smd_channel *channel)
363{
364 struct qcom_smd_edge *edge = channel->edge;
365
366 regmap_write(edge->ipc_regmap, edge->ipc_offset, BIT(edge->ipc_bit));
367}
368
369/*
370 * Initialize the tx channel info
371 */
372static void qcom_smd_channel_reset(struct qcom_smd_channel *channel)
373{
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);
385
386 qcom_smd_signal_channel(channel);
387
388 channel->state = SMD_CHANNEL_CLOSED;
389 channel->pkt_size = 0;
390}
391
392/*
393 * Set the callback for a channel, with appropriate locking
394 */
395static void qcom_smd_channel_set_callback(struct qcom_smd_channel *channel,
396 rpmsg_rx_cb_t cb)
397{
398 struct rpmsg_endpoint *ept = &channel->qsept->ept;
399 unsigned long flags;
400
401 spin_lock_irqsave(&channel->recv_lock, flags);
402 ept->cb = cb;
403 spin_unlock_irqrestore(&channel->recv_lock, flags);
404};
405
406/*
407 * Calculate the amount of data available in the rx fifo
408 */
409static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel *channel)
410{
411 unsigned head;
412 unsigned tail;
413
414 head = GET_RX_CHANNEL_INFO(channel, head);
415 tail = GET_RX_CHANNEL_INFO(channel, tail);
416
417 return (head - tail) & (channel->fifo_size - 1);
418}
419
420/*
421 * Set tx channel state and inform the remote processor
422 */
423static void qcom_smd_channel_set_state(struct qcom_smd_channel *channel,
424 int state)
425{
426 struct qcom_smd_edge *edge = channel->edge;
427 bool is_open = state == SMD_CHANNEL_OPENED;
428
429 if (channel->state == state)
430 return;
431
432 dev_dbg(&edge->dev, "set_state(%s, %d)\n", channel->name, state);
433
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);
437
438 SET_TX_CHANNEL_INFO(channel, state, state);
439 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
440
441 channel->state = state;
442 qcom_smd_signal_channel(channel);
443}
444
445/*
446 * Copy count bytes of data using 32bit accesses, if that's required.
447 */
448static void smd_copy_to_fifo(void __iomem *dst,
449 const void *src,
450 size_t count,
451 bool word_aligned)
452{
453 if (word_aligned) {
454 __iowrite32_copy(dst, src, count / sizeof(u32));
455 } else {
456 memcpy_toio(dst, src, count);
457 }
458}
459
460/*
461 * Copy count bytes of data using 32bit accesses, if that is required.
462 */
463static void smd_copy_from_fifo(void *dst,
464 const void __iomem *src,
465 size_t count,
466 bool word_aligned)
467{
468 if (word_aligned) {
469 __ioread32_copy(dst, src, count / sizeof(u32));
470 } else {
471 memcpy_fromio(dst, src, count);
472 }
473}
474
475/*
476 * Read count bytes of data from the rx fifo into buf, but don't advance the
477 * tail.
478 */
479static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel,
480 void *buf, size_t count)
481{
482 bool word_aligned;
483 unsigned tail;
484 size_t len;
485
486 word_aligned = channel->info_word;
487 tail = GET_RX_CHANNEL_INFO(channel, tail);
488
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);
495 }
496
497 if (len != count) {
498 smd_copy_from_fifo(buf + len,
499 channel->rx_fifo,
500 count - len,
501 word_aligned);
502 }
503
504 return count;
505}
506
507/*
508 * Advance the rx tail by count bytes.
509 */
510static void qcom_smd_channel_advance(struct qcom_smd_channel *channel,
511 size_t count)
512{
513 unsigned tail;
514
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);
519}
520
521/*
522 * Read out a single packet from the rx fifo and deliver it to the device
523 */
524static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
525{
526 struct rpmsg_endpoint *ept = &channel->qsept->ept;
527 unsigned tail;
528 size_t len;
529 void *ptr;
530 int ret;
531
532 tail = GET_RX_CHANNEL_INFO(channel, tail);
533
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;
541 }
542
543 ret = ept->cb(ept->rpdev, ptr, len, ept->priv, RPMSG_ADDR_ANY);
544 if (ret < 0)
545 return ret;
546
547 /* Only forward the tail if the client consumed the data */
548 qcom_smd_channel_advance(channel, len);
549
550 channel->pkt_size = 0;
551
552 return 0;
553}
554
555/*
556 * Per channel interrupt handling
557 */
558static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel)
559{
560 bool need_state_scan = false;
561 int remote_state;
562 __le32 pktlen;
563 int avail;
564 int ret;
565
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;
571 }
572 /* Indicate that we have seen any state change */
573 SET_RX_CHANNEL_FLAG(channel, fSTATE, 0);
574
575 /* Signal waiting qcom_smd_send() about the interrupt */
576 if (!GET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR))
577 wake_up_interruptible(&channel->fblockread_event);
578
579 /* Don't consume any data until we've opened the channel */
580 if (channel->state != SMD_CHANNEL_OPENED)
581 goto out;
582
583 /* Indicate that we've seen the new data */
584 SET_RX_CHANNEL_FLAG(channel, fHEAD, 0);
585
586 /* Consume data */
587 for (;;) {
588 avail = qcom_smd_channel_get_rx_avail(channel);
589
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;
600 }
601 }
602
603 /* Indicate that we have seen and updated tail */
604 SET_RX_CHANNEL_FLAG(channel, fTAIL, 1);
605
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();
610
611 qcom_smd_signal_channel(channel);
612 }
613
614out:
615 return need_state_scan;
616}
617
618/*
619 * The edge interrupts are triggered by the remote processor on state changes,
620 * channel info updates or when new channels are created.
621 */
622static irqreturn_t qcom_smd_edge_intr(int irq, void *data)
623{
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;
629
630 /*
631 * Handle state changes or data on each of the channels on this edge
632 */
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);
638 }
639 spin_unlock(&edge->channels_lock);
640
641 /*
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.
645 */
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;
650 }
651
652 if (kick_scanner)
653 schedule_work(&edge->scan_work);
654 if (kick_state)
655 schedule_work(&edge->state_work);
656
657 return IRQ_HANDLED;
658}
659
660/*
661 * Calculate how much space is available in the tx fifo.
662 */
663static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel)
664{
665 unsigned head;
666 unsigned tail;
667 unsigned mask = channel->fifo_size - 1;
668
669 head = GET_TX_CHANNEL_INFO(channel, head);
670 tail = GET_TX_CHANNEL_INFO(channel, tail);
671
672 return mask - ((head - tail) & mask);
673}
674
675/*
676 * Write count bytes of data into channel, possibly wrapping in the ring buffer
677 */
678static int qcom_smd_write_fifo(struct qcom_smd_channel *channel,
679 const void *data,
680 size_t count)
681{
682 bool word_aligned;
683 unsigned head;
684 size_t len;
685
686 word_aligned = channel->info_word;
687 head = GET_TX_CHANNEL_INFO(channel, head);
688
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);
695 }
696
697 if (len != count) {
698 smd_copy_to_fifo(channel->tx_fifo,
699 data + len,
700 count - len,
701 word_aligned);
702 }
703
704 head += count;
705 head &= (channel->fifo_size - 1);
706 SET_TX_CHANNEL_INFO(channel, head, head);
707
708 return count;
709}
710
711/**
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
716 *
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.
721 */
722static int __qcom_smd_send(struct qcom_smd_channel *channel, const void *data,
723 int len, bool wait)
724{
725 __le32 hdr[5] = { cpu_to_le32(len), };
726 int tlen = sizeof(hdr) + len;
727 int ret;
728
729 /* Word aligned channels only accept word size aligned data */
730 if (channel->info_word && len % 4)
731 return -EINVAL;
732
733 /* Reject packets that are too big */
734 if (tlen >= channel->fifo_size)
735 return -EINVAL;
736
737 ret = mutex_lock_interruptible(&channel->tx_lock);
738 if (ret)
739 return ret;
740
741 while (qcom_smd_get_tx_avail(channel) < tlen) {
742 if (!wait) {
743 ret = -ENOMEM;
744 goto out;
745 }
746
747 if (channel->state != SMD_CHANNEL_OPENED) {
748 ret = -EPIPE;
749 goto out;
750 }
751
752 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 0);
753
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;
759
760 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
761 }
762
763 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
764
765 qcom_smd_write_fifo(channel, hdr, sizeof(hdr));
766 qcom_smd_write_fifo(channel, data, len);
767
768 SET_TX_CHANNEL_FLAG(channel, fHEAD, 1);
769
770 /* Ensure ordering of channel info updates */
771 wmb();
772
773 qcom_smd_signal_channel(channel);
774
775out:
776 mutex_unlock(&channel->tx_lock);
777
778 return ret;
779}
780
781/*
782 * Helper for opening a channel
783 */
784static int qcom_smd_channel_open(struct qcom_smd_channel *channel,
785 rpmsg_rx_cb_t cb)
786{
787 size_t bb_size;
788
789 /*
790 * Packets are maximum 4k, but reduce if the fifo is smaller
791 */
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;
796
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);
800
801 return 0;
802}
803
804/*
805 * Helper for closing and resetting a channel
806 */
807static void qcom_smd_channel_close(struct qcom_smd_channel *channel)
808{
809 qcom_smd_channel_set_callback(channel, NULL);
810
811 kfree(channel->bounce_buffer);
812 channel->bounce_buffer = NULL;
813
814 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
815 qcom_smd_channel_reset(channel);
816}
817
818static struct qcom_smd_channel *
819qcom_smd_find_channel(struct qcom_smd_edge *edge, const char *name)
820{
821 struct qcom_smd_channel *channel;
822 struct qcom_smd_channel *ret = NULL;
823 unsigned long flags;
824 unsigned state;
825
826 spin_lock_irqsave(&edge->channels_lock, flags);
827 list_for_each_entry(channel, &edge->channels, list) {
828 if (strcmp(channel->name, name))
829 continue;
830
831 state = GET_RX_CHANNEL_INFO(channel, state);
832 if (state != SMD_CHANNEL_OPENING &&
833 state != SMD_CHANNEL_OPENED)
834 continue;
835
836 ret = channel;
837 break;
838 }
839 spin_unlock_irqrestore(&edge->channels_lock, flags);
840
841 return ret;
842}
843
844static void __ept_release(struct kref *kref)
845{
846 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
847 refcount);
848 kfree(to_smd_endpoint(ept));
849}
850
851static struct rpmsg_endpoint *qcom_smd_create_ept(struct rpmsg_device *rpdev,
852 rpmsg_rx_cb_t cb, void *priv,
853 struct rpmsg_channel_info chinfo)
854{
855 struct qcom_smd_endpoint *qsept;
856 struct qcom_smd_channel *channel;
857 struct qcom_smd_device *qsdev = to_smd_device(rpdev);
858 struct qcom_smd_edge *edge = qsdev->edge;
859 struct rpmsg_endpoint *ept;
860 const char *name = chinfo.name;
861 int ret;
862
863 /* Wait up to HZ for the channel to appear */
864 ret = wait_event_interruptible_timeout(edge->new_channel_event,
865 (channel = qcom_smd_find_channel(edge, name)) != NULL,
866 HZ);
867 if (!ret)
868 return NULL;
869
870 if (channel->state != SMD_CHANNEL_CLOSED) {
871 dev_err(&rpdev->dev, "channel %s is busy\n", channel->name);
872 return NULL;
873 }
874
875 qsept = kzalloc(sizeof(*qsept), GFP_KERNEL);
876 if (!qsept)
877 return NULL;
878
879 ept = &qsept->ept;
880
881 kref_init(&ept->refcount);
882
883 ept->rpdev = rpdev;
884 ept->cb = cb;
885 ept->priv = priv;
886 ept->ops = &qcom_smd_endpoint_ops;
887
888 channel->qsept = qsept;
889 qsept->qsch = channel;
890
891 ret = qcom_smd_channel_open(channel, cb);
892 if (ret)
893 goto free_ept;
894
895 return ept;
896
897free_ept:
898 channel->qsept = NULL;
899 kref_put(&ept->refcount, __ept_release);
900 return NULL;
901}
902
903static void qcom_smd_destroy_ept(struct rpmsg_endpoint *ept)
904{
905 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
906 struct qcom_smd_channel *ch = qsept->qsch;
907
908 qcom_smd_channel_close(ch);
909 ch->qsept = NULL;
910 kref_put(&ept->refcount, __ept_release);
911}
912
913static int qcom_smd_send(struct rpmsg_endpoint *ept, void *data, int len)
914{
915 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
916
917 return __qcom_smd_send(qsept->qsch, data, len, true);
918}
919
920static int qcom_smd_trysend(struct rpmsg_endpoint *ept, void *data, int len)
921{
922 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
923
924 return __qcom_smd_send(qsept->qsch, data, len, false);
925}
926
927/*
928 * Finds the device_node for the smd child interested in this channel.
929 */
930static struct device_node *qcom_smd_match_channel(struct device_node *edge_node,
931 const char *channel)
932{
933 struct device_node *child;
934 const char *name;
935 const char *key;
936 int ret;
937
938 for_each_available_child_of_node(edge_node, child) {
939 key = "qcom,smd-channels";
940 ret = of_property_read_string(child, key, &name);
941 if (ret)
942 continue;
943
944 if (strcmp(name, channel) == 0)
945 return child;
946 }
947
948 return NULL;
949}
950
951static const struct rpmsg_device_ops qcom_smd_device_ops = {
952 .create_ept = qcom_smd_create_ept,
953};
954
955static const struct rpmsg_endpoint_ops qcom_smd_endpoint_ops = {
956 .destroy_ept = qcom_smd_destroy_ept,
957 .send = qcom_smd_send,
958 .trysend = qcom_smd_trysend,
959};
960
961/*
962 * Create a smd client device for channel that is being opened.
963 */
964static int qcom_smd_create_device(struct qcom_smd_channel *channel)
965{
966 struct qcom_smd_device *qsdev;
967 struct rpmsg_device *rpdev;
968 struct qcom_smd_edge *edge = channel->edge;
969
970 dev_dbg(&edge->dev, "registering '%s'\n", channel->name);
971
972 qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
973 if (!qsdev)
974 return -ENOMEM;
975
976 /* Link qsdev to our SMD edge */
977 qsdev->edge = edge;
978
979 /* Assign callbacks for rpmsg_device */
980 qsdev->rpdev.ops = &qcom_smd_device_ops;
981
982 /* Assign public information to the rpmsg_device */
983 rpdev = &qsdev->rpdev;
984 strncpy(rpdev->id.name, channel->name, RPMSG_NAME_SIZE);
985 rpdev->src = RPMSG_ADDR_ANY;
986 rpdev->dst = RPMSG_ADDR_ANY;
987
988 rpdev->dev.of_node = qcom_smd_match_channel(edge->of_node, channel->name);
989 rpdev->dev.parent = &edge->dev;
990
991 return rpmsg_register_device(rpdev);
992}
993
994/*
995 * Allocate the qcom_smd_channel object for a newly found smd channel,
996 * retrieving and validating the smem items involved.
997 */
998static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge,
999 unsigned smem_info_item,
1000 unsigned smem_fifo_item,
1001 char *name)
1002{
1003 struct qcom_smd_channel *channel;
1004 size_t fifo_size;
1005 size_t info_size;
1006 void *fifo_base;
1007 void *info;
1008 int ret;
1009
1010 channel = devm_kzalloc(&edge->dev, sizeof(*channel), GFP_KERNEL);
1011 if (!channel)
1012 return ERR_PTR(-ENOMEM);
1013
1014 channel->edge = edge;
1015 channel->name = devm_kstrdup(&edge->dev, name, GFP_KERNEL);
1016 if (!channel->name)
1017 return ERR_PTR(-ENOMEM);
1018
1019 mutex_init(&channel->tx_lock);
1020 spin_lock_init(&channel->recv_lock);
1021 init_waitqueue_head(&channel->fblockread_event);
1022
1023 info = qcom_smem_get(edge->remote_pid, smem_info_item, &info_size);
1024 if (IS_ERR(info)) {
1025 ret = PTR_ERR(info);
1026 goto free_name_and_channel;
1027 }
1028
1029 /*
1030 * Use the size of the item to figure out which channel info struct to
1031 * use.
1032 */
1033 if (info_size == 2 * sizeof(struct smd_channel_info_word)) {
1034 channel->info_word = info;
1035 } else if (info_size == 2 * sizeof(struct smd_channel_info)) {
1036 channel->info = info;
1037 } else {
1038 dev_err(&edge->dev,
1039 "channel info of size %zu not supported\n", info_size);
1040 ret = -EINVAL;
1041 goto free_name_and_channel;
1042 }
1043
1044 fifo_base = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_size);
1045 if (IS_ERR(fifo_base)) {
1046 ret = PTR_ERR(fifo_base);
1047 goto free_name_and_channel;
1048 }
1049
1050 /* The channel consist of a rx and tx fifo of equal size */
1051 fifo_size /= 2;
1052
1053 dev_dbg(&edge->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1054 name, info_size, fifo_size);
1055
1056 channel->tx_fifo = fifo_base;
1057 channel->rx_fifo = fifo_base + fifo_size;
1058 channel->fifo_size = fifo_size;
1059
1060 qcom_smd_channel_reset(channel);
1061
1062 return channel;
1063
1064free_name_and_channel:
1065 devm_kfree(&edge->dev, channel->name);
1066 devm_kfree(&edge->dev, channel);
1067
1068 return ERR_PTR(ret);
1069}
1070
1071/*
1072 * Scans the allocation table for any newly allocated channels, calls
1073 * qcom_smd_create_channel() to create representations of these and add
1074 * them to the edge's list of channels.
1075 */
1076static void qcom_channel_scan_worker(struct work_struct *work)
1077{
1078 struct qcom_smd_edge *edge = container_of(work, struct qcom_smd_edge, scan_work);
1079 struct qcom_smd_alloc_entry *alloc_tbl;
1080 struct qcom_smd_alloc_entry *entry;
1081 struct qcom_smd_channel *channel;
1082 unsigned long flags;
1083 unsigned fifo_id;
1084 unsigned info_id;
1085 int tbl;
1086 int i;
1087 u32 eflags, cid;
1088
1089 for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) {
1090 alloc_tbl = qcom_smem_get(edge->remote_pid,
1091 smem_items[tbl].alloc_tbl_id, NULL);
1092 if (IS_ERR(alloc_tbl))
1093 continue;
1094
1095 for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) {
1096 entry = &alloc_tbl[i];
1097 eflags = le32_to_cpu(entry->flags);
1098 if (test_bit(i, edge->allocated[tbl]))
1099 continue;
1100
1101 if (entry->ref_count == 0)
1102 continue;
1103
1104 if (!entry->name[0])
1105 continue;
1106
1107 if (!(eflags & SMD_CHANNEL_FLAGS_PACKET))
1108 continue;
1109
1110 if ((eflags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id)
1111 continue;
1112
1113 cid = le32_to_cpu(entry->cid);
1114 info_id = smem_items[tbl].info_base_id + cid;
1115 fifo_id = smem_items[tbl].fifo_base_id + cid;
1116
1117 channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
1118 if (IS_ERR(channel))
1119 continue;
1120
1121 spin_lock_irqsave(&edge->channels_lock, flags);
1122 list_add(&channel->list, &edge->channels);
1123 spin_unlock_irqrestore(&edge->channels_lock, flags);
1124
1125 dev_dbg(&edge->dev, "new channel found: '%s'\n", channel->name);
1126 set_bit(i, edge->allocated[tbl]);
1127
1128 wake_up_interruptible(&edge->new_channel_event);
1129 }
1130 }
1131
1132 schedule_work(&edge->state_work);
1133}
1134
1135/*
1136 * This per edge worker scans smem for any new channels and register these. It
1137 * then scans all registered channels for state changes that should be handled
1138 * by creating or destroying smd client devices for the registered channels.
1139 *
1140 * LOCKING: edge->channels_lock only needs to cover the list operations, as the
1141 * worker is killed before any channels are deallocated
1142 */
1143static void qcom_channel_state_worker(struct work_struct *work)
1144{
1145 struct qcom_smd_channel *channel;
1146 struct qcom_smd_edge *edge = container_of(work,
1147 struct qcom_smd_edge,
1148 state_work);
1149 struct rpmsg_channel_info chinfo;
1150 unsigned remote_state;
1151 unsigned long flags;
1152
1153 /*
1154 * Register a device for any closed channel where the remote processor
1155 * is showing interest in opening the channel.
1156 */
1157 spin_lock_irqsave(&edge->channels_lock, flags);
1158 list_for_each_entry(channel, &edge->channels, list) {
1159 if (channel->state != SMD_CHANNEL_CLOSED)
1160 continue;
1161
1162 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1163 if (remote_state != SMD_CHANNEL_OPENING &&
1164 remote_state != SMD_CHANNEL_OPENED)
1165 continue;
1166
1167 if (channel->registered)
1168 continue;
1169
1170 spin_unlock_irqrestore(&edge->channels_lock, flags);
1171 qcom_smd_create_device(channel);
1172 channel->registered = true;
1173 spin_lock_irqsave(&edge->channels_lock, flags);
1174
1175 channel->registered = true;
1176 }
1177
1178 /*
1179 * Unregister the device for any channel that is opened where the
1180 * remote processor is closing the channel.
1181 */
1182 list_for_each_entry(channel, &edge->channels, list) {
1183 if (channel->state != SMD_CHANNEL_OPENING &&
1184 channel->state != SMD_CHANNEL_OPENED)
1185 continue;
1186
1187 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1188 if (remote_state == SMD_CHANNEL_OPENING ||
1189 remote_state == SMD_CHANNEL_OPENED)
1190 continue;
1191
1192 spin_unlock_irqrestore(&edge->channels_lock, flags);
1193
1194 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1195 chinfo.src = RPMSG_ADDR_ANY;
1196 chinfo.dst = RPMSG_ADDR_ANY;
1197 rpmsg_unregister_device(&edge->dev, &chinfo);
1198 channel->registered = false;
1199 spin_lock_irqsave(&edge->channels_lock, flags);
1200 }
1201 spin_unlock_irqrestore(&edge->channels_lock, flags);
1202}
1203
1204/*
1205 * Parses an of_node describing an edge.
1206 */
1207static int qcom_smd_parse_edge(struct device *dev,
1208 struct device_node *node,
1209 struct qcom_smd_edge *edge)
1210{
1211 struct device_node *syscon_np;
1212 const char *key;
1213 int irq;
1214 int ret;
1215
1216 INIT_LIST_HEAD(&edge->channels);
1217 spin_lock_init(&edge->channels_lock);
1218
1219 INIT_WORK(&edge->scan_work, qcom_channel_scan_worker);
1220 INIT_WORK(&edge->state_work, qcom_channel_state_worker);
1221
1222 edge->of_node = of_node_get(node);
1223
1224 key = "qcom,smd-edge";
1225 ret = of_property_read_u32(node, key, &edge->edge_id);
1226 if (ret) {
1227 dev_err(dev, "edge missing %s property\n", key);
1228 return -EINVAL;
1229 }
1230
1231 edge->remote_pid = QCOM_SMEM_HOST_ANY;
1232 key = "qcom,remote-pid";
1233 of_property_read_u32(node, key, &edge->remote_pid);
1234
1235 syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
1236 if (!syscon_np) {
1237 dev_err(dev, "no qcom,ipc node\n");
1238 return -ENODEV;
1239 }
1240
1241 edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
1242 if (IS_ERR(edge->ipc_regmap))
1243 return PTR_ERR(edge->ipc_regmap);
1244
1245 key = "qcom,ipc";
1246 ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
1247 if (ret < 0) {
1248 dev_err(dev, "no offset in %s\n", key);
1249 return -EINVAL;
1250 }
1251
1252 ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
1253 if (ret < 0) {
1254 dev_err(dev, "no bit in %s\n", key);
1255 return -EINVAL;
1256 }
1257
1258 irq = irq_of_parse_and_map(node, 0);
1259 if (irq < 0) {
1260 dev_err(dev, "required smd interrupt missing\n");
1261 return -EINVAL;
1262 }
1263
1264 ret = devm_request_irq(dev, irq,
1265 qcom_smd_edge_intr, IRQF_TRIGGER_RISING,
1266 node->name, edge);
1267 if (ret) {
1268 dev_err(dev, "failed to request smd irq\n");
1269 return ret;
1270 }
1271
1272 edge->irq = irq;
1273
1274 return 0;
1275}
1276
1277/*
1278 * Release function for an edge.
1279 * Reset the state of each associated channel and free the edge context.
1280 */
1281static void qcom_smd_edge_release(struct device *dev)
1282{
1283 struct qcom_smd_channel *channel;
1284 struct qcom_smd_edge *edge = to_smd_edge(dev);
1285
1286 list_for_each_entry(channel, &edge->channels, list) {
1287 SET_RX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
1288 SET_RX_CHANNEL_INFO(channel, head, 0);
1289 SET_RX_CHANNEL_INFO(channel, tail, 0);
1290 }
1291
1292 kfree(edge);
1293}
1294
1295/**
1296 * qcom_smd_register_edge() - register an edge based on an device_node
1297 * @parent: parent device for the edge
1298 * @node: device_node describing the edge
1299 *
1300 * Returns an edge reference, or negative ERR_PTR() on failure.
1301 */
1302struct qcom_smd_edge *qcom_smd_register_edge(struct device *parent,
1303 struct device_node *node)
1304{
1305 struct qcom_smd_edge *edge;
1306 int ret;
1307
1308 edge = kzalloc(sizeof(*edge), GFP_KERNEL);
1309 if (!edge)
1310 return ERR_PTR(-ENOMEM);
1311
1312 init_waitqueue_head(&edge->new_channel_event);
1313
1314 edge->dev.parent = parent;
1315 edge->dev.release = qcom_smd_edge_release;
1316 dev_set_name(&edge->dev, "%s:%s", dev_name(parent), node->name);
1317 ret = device_register(&edge->dev);
1318 if (ret) {
1319 pr_err("failed to register smd edge\n");
1320 return ERR_PTR(ret);
1321 }
1322
1323 ret = qcom_smd_parse_edge(&edge->dev, node, edge);
1324 if (ret) {
1325 dev_err(&edge->dev, "failed to parse smd edge\n");
1326 goto unregister_dev;
1327 }
1328
1329 schedule_work(&edge->scan_work);
1330
1331 return edge;
1332
1333unregister_dev:
1334 put_device(&edge->dev);
1335 return ERR_PTR(ret);
1336}
1337EXPORT_SYMBOL(qcom_smd_register_edge);
1338
1339static int qcom_smd_remove_device(struct device *dev, void *data)
1340{
1341 device_unregister(dev);
1342
1343 return 0;
1344}
1345
1346/**
1347 * qcom_smd_unregister_edge() - release an edge and its children
1348 * @edge: edge reference acquired from qcom_smd_register_edge
1349 */
1350int qcom_smd_unregister_edge(struct qcom_smd_edge *edge)
1351{
1352 int ret;
1353
1354 disable_irq(edge->irq);
1355 cancel_work_sync(&edge->scan_work);
1356 cancel_work_sync(&edge->state_work);
1357
1358 ret = device_for_each_child(&edge->dev, NULL, qcom_smd_remove_device);
1359 if (ret)
1360 dev_warn(&edge->dev, "can't remove smd device: %d\n", ret);
1361
1362 device_unregister(&edge->dev);
1363
1364 return 0;
1365}
1366EXPORT_SYMBOL(qcom_smd_unregister_edge);
1367
1368static int qcom_smd_probe(struct platform_device *pdev)
1369{
1370 struct device_node *node;
1371 void *p;
1372
1373 /* Wait for smem */
1374 p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL);
1375 if (PTR_ERR(p) == -EPROBE_DEFER)
1376 return PTR_ERR(p);
1377
1378 for_each_available_child_of_node(pdev->dev.of_node, node)
1379 qcom_smd_register_edge(&pdev->dev, node);
1380
1381 return 0;
1382}
1383
1384static int qcom_smd_remove_edge(struct device *dev, void *data)
1385{
1386 struct qcom_smd_edge *edge = to_smd_edge(dev);
1387
1388 return qcom_smd_unregister_edge(edge);
1389}
1390
1391/*
1392 * Shut down all smd clients by making sure that each edge stops processing
1393 * events and scanning for new channels, then call destroy on the devices.
1394 */
1395static int qcom_smd_remove(struct platform_device *pdev)
1396{
1397 int ret;
1398
1399 ret = device_for_each_child(&pdev->dev, NULL, qcom_smd_remove_edge);
1400 if (ret)
1401 dev_warn(&pdev->dev, "can't remove smd device: %d\n", ret);
1402
1403 return ret;
1404}
1405
1406static const struct of_device_id qcom_smd_of_match[] = {
1407 { .compatible = "qcom,smd" },
1408 {}
1409};
1410MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
1411
1412static struct platform_driver qcom_smd_driver = {
1413 .probe = qcom_smd_probe,
1414 .remove = qcom_smd_remove,
1415 .driver = {
1416 .name = "qcom-smd",
1417 .of_match_table = qcom_smd_of_match,
1418 },
1419};
1420
1421static int __init qcom_smd_init(void)
1422{
1423 return platform_driver_register(&qcom_smd_driver);
1424}
1425subsys_initcall(qcom_smd_init);
1426
1427static void __exit qcom_smd_exit(void)
1428{
1429 platform_driver_unregister(&qcom_smd_driver);
1430}
1431module_exit(qcom_smd_exit);
1432
1433MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
1434MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
1435MODULE_LICENSE("GPL v2");