]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/rpmsg/qcom_smd.c
rpmsg: virtio_rpmsg_bus: Switch to SPDX license identifier
[mirror_ubuntu-jammy-kernel.git] / drivers / rpmsg / qcom_smd.c
CommitLineData
53e2822e
BA
1/*
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>
ab460a2e 17#include <linux/mailbox_client.h>
53e2822e
BA
18#include <linux/mfd/syscon.h>
19#include <linux/module.h>
20#include <linux/of_irq.h>
21#include <linux/of_platform.h>
22#include <linux/platform_device.h>
23#include <linux/regmap.h>
24#include <linux/sched.h>
25#include <linux/slab.h>
26#include <linux/soc/qcom/smem.h>
27#include <linux/wait.h>
28#include <linux/rpmsg.h>
8fc94723 29#include <linux/rpmsg/qcom_smd.h>
53e2822e
BA
30
31#include "rpmsg_internal.h"
32
33/*
34 * The Qualcomm Shared Memory communication solution provides point-to-point
35 * channels for clients to send and receive streaming or packet based data.
36 *
37 * Each channel consists of a control item (channel info) and a ring buffer
38 * pair. The channel info carry information related to channel state, flow
39 * control and the offsets within the ring buffer.
40 *
41 * All allocated channels are listed in an allocation table, identifying the
42 * pair of items by name, type and remote processor.
43 *
44 * Upon creating a new channel the remote processor allocates channel info and
45 * ring buffer items from the smem heap and populate the allocation table. An
46 * interrupt is sent to the other end of the channel and a scan for new
47 * channels should be done. A channel never goes away, it will only change
48 * state.
49 *
50 * The remote processor signals it intent for bring up the communication
51 * channel by setting the state of its end of the channel to "opening" and
52 * sends out an interrupt. We detect this change and register a smd device to
53 * consume the channel. Upon finding a consumer we finish the handshake and the
54 * channel is up.
55 *
56 * Upon closing a channel, the remote processor will update the state of its
57 * end of the channel and signal us, we will then unregister any attached
58 * device and close our end of the channel.
59 *
60 * Devices attached to a channel can use the qcom_smd_send function to push
61 * data to the channel, this is done by copying the data into the tx ring
62 * buffer, updating the pointers in the channel info and signaling the remote
63 * processor.
64 *
65 * The remote processor does the equivalent when it transfer data and upon
66 * receiving the interrupt we check the channel info for new data and delivers
67 * this to the attached device. If the device is not ready to receive the data
68 * we leave it in the ring buffer for now.
69 */
70
71struct smd_channel_info;
72struct smd_channel_info_pair;
73struct smd_channel_info_word;
74struct smd_channel_info_word_pair;
75
76static const struct rpmsg_endpoint_ops qcom_smd_endpoint_ops;
77
78#define SMD_ALLOC_TBL_COUNT 2
79#define SMD_ALLOC_TBL_SIZE 64
80
81/*
82 * This lists the various smem heap items relevant for the allocation table and
83 * smd channel entries.
84 */
85static const struct {
86 unsigned alloc_tbl_id;
87 unsigned info_base_id;
88 unsigned fifo_base_id;
89} smem_items[SMD_ALLOC_TBL_COUNT] = {
90 {
91 .alloc_tbl_id = 13,
92 .info_base_id = 14,
93 .fifo_base_id = 338
94 },
95 {
96 .alloc_tbl_id = 266,
97 .info_base_id = 138,
98 .fifo_base_id = 202,
99 },
100};
101
102/**
103 * struct qcom_smd_edge - representing a remote processor
104 * @of_node: of_node handle for information related to this edge
105 * @edge_id: identifier of this edge
106 * @remote_pid: identifier of remote processor
107 * @irq: interrupt for signals on this edge
108 * @ipc_regmap: regmap handle holding the outgoing ipc register
109 * @ipc_offset: offset within @ipc_regmap of the register for ipc
110 * @ipc_bit: bit in the register at @ipc_offset of @ipc_regmap
ab460a2e
BA
111 * @mbox_client: mailbox client handle
112 * @mbox_chan: apcs ipc mailbox channel handle
53e2822e
BA
113 * @channels: list of all channels detected on this edge
114 * @channels_lock: guard for modifications of @channels
115 * @allocated: array of bitmaps representing already allocated channels
116 * @smem_available: last available amount of smem triggering a channel scan
117 * @scan_work: work item for discovering new channels
118 * @state_work: work item for edge state changes
119 */
120struct qcom_smd_edge {
121 struct device dev;
122
5e53c42c
BA
123 const char *name;
124
53e2822e
BA
125 struct device_node *of_node;
126 unsigned edge_id;
127 unsigned remote_pid;
128
129 int irq;
130
131 struct regmap *ipc_regmap;
132 int ipc_offset;
133 int ipc_bit;
134
ab460a2e
BA
135 struct mbox_client mbox_client;
136 struct mbox_chan *mbox_chan;
137
53e2822e
BA
138 struct list_head channels;
139 spinlock_t channels_lock;
140
141 DECLARE_BITMAP(allocated[SMD_ALLOC_TBL_COUNT], SMD_ALLOC_TBL_SIZE);
142
143 unsigned smem_available;
144
145 wait_queue_head_t new_channel_event;
146
147 struct work_struct scan_work;
148 struct work_struct state_work;
149};
150
151/*
152 * SMD channel states.
153 */
154enum smd_channel_state {
155 SMD_CHANNEL_CLOSED,
156 SMD_CHANNEL_OPENING,
157 SMD_CHANNEL_OPENED,
158 SMD_CHANNEL_FLUSHING,
159 SMD_CHANNEL_CLOSING,
160 SMD_CHANNEL_RESET,
161 SMD_CHANNEL_RESET_OPENING
162};
163
164struct qcom_smd_device {
165 struct rpmsg_device rpdev;
166
167 struct qcom_smd_edge *edge;
168};
169
170struct qcom_smd_endpoint {
171 struct rpmsg_endpoint ept;
172
173 struct qcom_smd_channel *qsch;
174};
175
6ddf12d3 176#define to_smd_device(r) container_of(r, struct qcom_smd_device, rpdev)
53e2822e 177#define to_smd_edge(d) container_of(d, struct qcom_smd_edge, dev)
6ddf12d3 178#define to_smd_endpoint(e) container_of(e, struct qcom_smd_endpoint, ept)
53e2822e
BA
179
180/**
181 * struct qcom_smd_channel - smd channel struct
182 * @edge: qcom_smd_edge this channel is living on
183 * @qsdev: reference to a associated smd client device
184 * @name: name of the channel
185 * @state: local state of the channel
186 * @remote_state: remote state of the channel
187 * @info: byte aligned outgoing/incoming channel info
188 * @info_word: word aligned outgoing/incoming channel info
189 * @tx_lock: lock to make writes to the channel mutually exclusive
190 * @fblockread_event: wakeup event tied to tx fBLOCKREADINTR
191 * @tx_fifo: pointer to the outgoing ring buffer
192 * @rx_fifo: pointer to the incoming ring buffer
193 * @fifo_size: size of each ring buffer
194 * @bounce_buffer: bounce buffer for reading wrapped packets
195 * @cb: callback function registered for this channel
196 * @recv_lock: guard for rx info modifications and cb pointer
197 * @pkt_size: size of the currently handled packet
198 * @list: lite entry for @channels in qcom_smd_edge
199 */
200struct qcom_smd_channel {
201 struct qcom_smd_edge *edge;
202
203 struct qcom_smd_endpoint *qsept;
204 bool registered;
205
206 char *name;
207 enum smd_channel_state state;
208 enum smd_channel_state remote_state;
268105fb 209 wait_queue_head_t state_change_event;
53e2822e
BA
210
211 struct smd_channel_info_pair *info;
212 struct smd_channel_info_word_pair *info_word;
213
33e3820d 214 spinlock_t tx_lock;
53e2822e
BA
215 wait_queue_head_t fblockread_event;
216
217 void *tx_fifo;
218 void *rx_fifo;
219 int fifo_size;
220
221 void *bounce_buffer;
222
223 spinlock_t recv_lock;
224
225 int pkt_size;
226
227 void *drvdata;
228
229 struct list_head list;
230};
231
232/*
233 * Format of the smd_info smem items, for byte aligned channels.
234 */
235struct smd_channel_info {
236 __le32 state;
237 u8 fDSR;
238 u8 fCTS;
239 u8 fCD;
240 u8 fRI;
241 u8 fHEAD;
242 u8 fTAIL;
243 u8 fSTATE;
244 u8 fBLOCKREADINTR;
245 __le32 tail;
246 __le32 head;
247};
248
249struct smd_channel_info_pair {
250 struct smd_channel_info tx;
251 struct smd_channel_info rx;
252};
253
254/*
255 * Format of the smd_info smem items, for word aligned channels.
256 */
257struct smd_channel_info_word {
258 __le32 state;
259 __le32 fDSR;
260 __le32 fCTS;
261 __le32 fCD;
262 __le32 fRI;
263 __le32 fHEAD;
264 __le32 fTAIL;
265 __le32 fSTATE;
266 __le32 fBLOCKREADINTR;
267 __le32 tail;
268 __le32 head;
269};
270
271struct smd_channel_info_word_pair {
272 struct smd_channel_info_word tx;
273 struct smd_channel_info_word rx;
274};
275
276#define GET_RX_CHANNEL_FLAG(channel, param) \
277 ({ \
278 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
279 channel->info_word ? \
280 le32_to_cpu(channel->info_word->rx.param) : \
281 channel->info->rx.param; \
282 })
283
284#define GET_RX_CHANNEL_INFO(channel, param) \
285 ({ \
286 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
287 le32_to_cpu(channel->info_word ? \
288 channel->info_word->rx.param : \
289 channel->info->rx.param); \
290 })
291
292#define SET_RX_CHANNEL_FLAG(channel, param, value) \
293 ({ \
294 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
295 if (channel->info_word) \
296 channel->info_word->rx.param = cpu_to_le32(value); \
297 else \
298 channel->info->rx.param = value; \
299 })
300
301#define SET_RX_CHANNEL_INFO(channel, param, value) \
302 ({ \
303 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
304 if (channel->info_word) \
305 channel->info_word->rx.param = cpu_to_le32(value); \
306 else \
307 channel->info->rx.param = cpu_to_le32(value); \
308 })
309
310#define GET_TX_CHANNEL_FLAG(channel, param) \
311 ({ \
312 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
313 channel->info_word ? \
314 le32_to_cpu(channel->info_word->tx.param) : \
315 channel->info->tx.param; \
316 })
317
318#define GET_TX_CHANNEL_INFO(channel, param) \
319 ({ \
320 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
321 le32_to_cpu(channel->info_word ? \
322 channel->info_word->tx.param : \
323 channel->info->tx.param); \
324 })
325
326#define SET_TX_CHANNEL_FLAG(channel, param, value) \
327 ({ \
328 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
329 if (channel->info_word) \
330 channel->info_word->tx.param = cpu_to_le32(value); \
331 else \
332 channel->info->tx.param = value; \
333 })
334
335#define SET_TX_CHANNEL_INFO(channel, param, value) \
336 ({ \
337 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
338 if (channel->info_word) \
339 channel->info_word->tx.param = cpu_to_le32(value); \
340 else \
341 channel->info->tx.param = cpu_to_le32(value); \
342 })
343
344/**
345 * struct qcom_smd_alloc_entry - channel allocation entry
346 * @name: channel name
347 * @cid: channel index
348 * @flags: channel flags and edge id
349 * @ref_count: reference count of the channel
350 */
351struct qcom_smd_alloc_entry {
352 u8 name[20];
353 __le32 cid;
354 __le32 flags;
355 __le32 ref_count;
356} __packed;
357
358#define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff
359#define SMD_CHANNEL_FLAGS_STREAM BIT(8)
360#define SMD_CHANNEL_FLAGS_PACKET BIT(9)
361
362/*
363 * Each smd packet contains a 20 byte header, with the first 4 being the length
364 * of the packet.
365 */
366#define SMD_PACKET_HEADER_LEN 20
367
368/*
369 * Signal the remote processor associated with 'channel'.
370 */
371static void qcom_smd_signal_channel(struct qcom_smd_channel *channel)
372{
373 struct qcom_smd_edge *edge = channel->edge;
374
ab460a2e
BA
375 if (edge->mbox_chan) {
376 /*
377 * We can ignore a failing mbox_send_message() as the only
378 * possible cause is that the FIFO in the framework is full of
379 * other writes to the same bit.
380 */
381 mbox_send_message(edge->mbox_chan, NULL);
382 mbox_client_txdone(edge->mbox_chan, 0);
383 } else {
384 regmap_write(edge->ipc_regmap, edge->ipc_offset, BIT(edge->ipc_bit));
385 }
53e2822e
BA
386}
387
388/*
389 * Initialize the tx channel info
390 */
391static void qcom_smd_channel_reset(struct qcom_smd_channel *channel)
392{
393 SET_TX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
394 SET_TX_CHANNEL_FLAG(channel, fDSR, 0);
395 SET_TX_CHANNEL_FLAG(channel, fCTS, 0);
396 SET_TX_CHANNEL_FLAG(channel, fCD, 0);
397 SET_TX_CHANNEL_FLAG(channel, fRI, 0);
398 SET_TX_CHANNEL_FLAG(channel, fHEAD, 0);
399 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
400 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
401 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
402 SET_TX_CHANNEL_INFO(channel, head, 0);
403 SET_RX_CHANNEL_INFO(channel, tail, 0);
404
405 qcom_smd_signal_channel(channel);
406
407 channel->state = SMD_CHANNEL_CLOSED;
408 channel->pkt_size = 0;
409}
410
411/*
412 * Set the callback for a channel, with appropriate locking
413 */
414static void qcom_smd_channel_set_callback(struct qcom_smd_channel *channel,
415 rpmsg_rx_cb_t cb)
416{
417 struct rpmsg_endpoint *ept = &channel->qsept->ept;
418 unsigned long flags;
419
420 spin_lock_irqsave(&channel->recv_lock, flags);
421 ept->cb = cb;
422 spin_unlock_irqrestore(&channel->recv_lock, flags);
423};
424
425/*
426 * Calculate the amount of data available in the rx fifo
427 */
428static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel *channel)
429{
430 unsigned head;
431 unsigned tail;
432
433 head = GET_RX_CHANNEL_INFO(channel, head);
434 tail = GET_RX_CHANNEL_INFO(channel, tail);
435
436 return (head - tail) & (channel->fifo_size - 1);
437}
438
439/*
440 * Set tx channel state and inform the remote processor
441 */
442static void qcom_smd_channel_set_state(struct qcom_smd_channel *channel,
443 int state)
444{
445 struct qcom_smd_edge *edge = channel->edge;
446 bool is_open = state == SMD_CHANNEL_OPENED;
447
448 if (channel->state == state)
449 return;
450
451 dev_dbg(&edge->dev, "set_state(%s, %d)\n", channel->name, state);
452
453 SET_TX_CHANNEL_FLAG(channel, fDSR, is_open);
454 SET_TX_CHANNEL_FLAG(channel, fCTS, is_open);
455 SET_TX_CHANNEL_FLAG(channel, fCD, is_open);
456
457 SET_TX_CHANNEL_INFO(channel, state, state);
458 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
459
460 channel->state = state;
461 qcom_smd_signal_channel(channel);
462}
463
464/*
465 * Copy count bytes of data using 32bit accesses, if that's required.
466 */
467static void smd_copy_to_fifo(void __iomem *dst,
468 const void *src,
469 size_t count,
470 bool word_aligned)
471{
472 if (word_aligned) {
473 __iowrite32_copy(dst, src, count / sizeof(u32));
474 } else {
475 memcpy_toio(dst, src, count);
476 }
477}
478
479/*
480 * Copy count bytes of data using 32bit accesses, if that is required.
481 */
482static void smd_copy_from_fifo(void *dst,
483 const void __iomem *src,
484 size_t count,
485 bool word_aligned)
486{
487 if (word_aligned) {
488 __ioread32_copy(dst, src, count / sizeof(u32));
489 } else {
490 memcpy_fromio(dst, src, count);
491 }
492}
493
494/*
495 * Read count bytes of data from the rx fifo into buf, but don't advance the
496 * tail.
497 */
498static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel,
499 void *buf, size_t count)
500{
501 bool word_aligned;
502 unsigned tail;
503 size_t len;
504
505 word_aligned = channel->info_word;
506 tail = GET_RX_CHANNEL_INFO(channel, tail);
507
508 len = min_t(size_t, count, channel->fifo_size - tail);
509 if (len) {
510 smd_copy_from_fifo(buf,
511 channel->rx_fifo + tail,
512 len,
513 word_aligned);
514 }
515
516 if (len != count) {
517 smd_copy_from_fifo(buf + len,
518 channel->rx_fifo,
519 count - len,
520 word_aligned);
521 }
522
523 return count;
524}
525
526/*
527 * Advance the rx tail by count bytes.
528 */
529static void qcom_smd_channel_advance(struct qcom_smd_channel *channel,
530 size_t count)
531{
532 unsigned tail;
533
534 tail = GET_RX_CHANNEL_INFO(channel, tail);
535 tail += count;
536 tail &= (channel->fifo_size - 1);
537 SET_RX_CHANNEL_INFO(channel, tail, tail);
538}
539
540/*
541 * Read out a single packet from the rx fifo and deliver it to the device
542 */
543static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
544{
545 struct rpmsg_endpoint *ept = &channel->qsept->ept;
546 unsigned tail;
547 size_t len;
548 void *ptr;
549 int ret;
550
551 tail = GET_RX_CHANNEL_INFO(channel, tail);
552
553 /* Use bounce buffer if the data wraps */
554 if (tail + channel->pkt_size >= channel->fifo_size) {
555 ptr = channel->bounce_buffer;
556 len = qcom_smd_channel_peek(channel, ptr, channel->pkt_size);
557 } else {
558 ptr = channel->rx_fifo + tail;
559 len = channel->pkt_size;
560 }
561
562 ret = ept->cb(ept->rpdev, ptr, len, ept->priv, RPMSG_ADDR_ANY);
563 if (ret < 0)
564 return ret;
565
566 /* Only forward the tail if the client consumed the data */
567 qcom_smd_channel_advance(channel, len);
568
569 channel->pkt_size = 0;
570
571 return 0;
572}
573
574/*
575 * Per channel interrupt handling
576 */
577static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel)
578{
579 bool need_state_scan = false;
580 int remote_state;
581 __le32 pktlen;
582 int avail;
583 int ret;
584
585 /* Handle state changes */
586 remote_state = GET_RX_CHANNEL_INFO(channel, state);
587 if (remote_state != channel->remote_state) {
588 channel->remote_state = remote_state;
589 need_state_scan = true;
268105fb
BA
590
591 wake_up_interruptible_all(&channel->state_change_event);
53e2822e
BA
592 }
593 /* Indicate that we have seen any state change */
594 SET_RX_CHANNEL_FLAG(channel, fSTATE, 0);
595
596 /* Signal waiting qcom_smd_send() about the interrupt */
597 if (!GET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR))
eb114f27 598 wake_up_interruptible_all(&channel->fblockread_event);
53e2822e
BA
599
600 /* Don't consume any data until we've opened the channel */
601 if (channel->state != SMD_CHANNEL_OPENED)
602 goto out;
603
604 /* Indicate that we've seen the new data */
605 SET_RX_CHANNEL_FLAG(channel, fHEAD, 0);
606
607 /* Consume data */
608 for (;;) {
609 avail = qcom_smd_channel_get_rx_avail(channel);
610
611 if (!channel->pkt_size && avail >= SMD_PACKET_HEADER_LEN) {
612 qcom_smd_channel_peek(channel, &pktlen, sizeof(pktlen));
613 qcom_smd_channel_advance(channel, SMD_PACKET_HEADER_LEN);
614 channel->pkt_size = le32_to_cpu(pktlen);
615 } else if (channel->pkt_size && avail >= channel->pkt_size) {
616 ret = qcom_smd_channel_recv_single(channel);
617 if (ret)
618 break;
619 } else {
620 break;
621 }
622 }
623
624 /* Indicate that we have seen and updated tail */
625 SET_RX_CHANNEL_FLAG(channel, fTAIL, 1);
626
627 /* Signal the remote that we've consumed the data (if requested) */
628 if (!GET_RX_CHANNEL_FLAG(channel, fBLOCKREADINTR)) {
629 /* Ensure ordering of channel info updates */
630 wmb();
631
632 qcom_smd_signal_channel(channel);
633 }
634
635out:
636 return need_state_scan;
637}
638
639/*
640 * The edge interrupts are triggered by the remote processor on state changes,
641 * channel info updates or when new channels are created.
642 */
643static irqreturn_t qcom_smd_edge_intr(int irq, void *data)
644{
645 struct qcom_smd_edge *edge = data;
646 struct qcom_smd_channel *channel;
647 unsigned available;
648 bool kick_scanner = false;
649 bool kick_state = false;
650
651 /*
652 * Handle state changes or data on each of the channels on this edge
653 */
654 spin_lock(&edge->channels_lock);
655 list_for_each_entry(channel, &edge->channels, list) {
656 spin_lock(&channel->recv_lock);
657 kick_state |= qcom_smd_channel_intr(channel);
658 spin_unlock(&channel->recv_lock);
659 }
660 spin_unlock(&edge->channels_lock);
661
662 /*
663 * Creating a new channel requires allocating an smem entry, so we only
664 * have to scan if the amount of available space in smem have changed
665 * since last scan.
666 */
667 available = qcom_smem_get_free_space(edge->remote_pid);
668 if (available != edge->smem_available) {
669 edge->smem_available = available;
670 kick_scanner = true;
671 }
672
673 if (kick_scanner)
674 schedule_work(&edge->scan_work);
675 if (kick_state)
676 schedule_work(&edge->state_work);
677
678 return IRQ_HANDLED;
679}
680
681/*
682 * Calculate how much space is available in the tx fifo.
683 */
684static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel)
685{
686 unsigned head;
687 unsigned tail;
688 unsigned mask = channel->fifo_size - 1;
689
690 head = GET_TX_CHANNEL_INFO(channel, head);
691 tail = GET_TX_CHANNEL_INFO(channel, tail);
692
693 return mask - ((head - tail) & mask);
694}
695
696/*
697 * Write count bytes of data into channel, possibly wrapping in the ring buffer
698 */
699static int qcom_smd_write_fifo(struct qcom_smd_channel *channel,
700 const void *data,
701 size_t count)
702{
703 bool word_aligned;
704 unsigned head;
705 size_t len;
706
707 word_aligned = channel->info_word;
708 head = GET_TX_CHANNEL_INFO(channel, head);
709
710 len = min_t(size_t, count, channel->fifo_size - head);
711 if (len) {
712 smd_copy_to_fifo(channel->tx_fifo + head,
713 data,
714 len,
715 word_aligned);
716 }
717
718 if (len != count) {
719 smd_copy_to_fifo(channel->tx_fifo,
720 data + len,
721 count - len,
722 word_aligned);
723 }
724
725 head += count;
726 head &= (channel->fifo_size - 1);
727 SET_TX_CHANNEL_INFO(channel, head, head);
728
729 return count;
730}
731
732/**
733 * qcom_smd_send - write data to smd channel
734 * @channel: channel handle
735 * @data: buffer of data to write
736 * @len: number of bytes to write
737 *
738 * This is a blocking write of len bytes into the channel's tx ring buffer and
739 * signal the remote end. It will sleep until there is enough space available
740 * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
741 * polling.
742 */
743static int __qcom_smd_send(struct qcom_smd_channel *channel, const void *data,
744 int len, bool wait)
745{
746 __le32 hdr[5] = { cpu_to_le32(len), };
747 int tlen = sizeof(hdr) + len;
33e3820d 748 unsigned long flags;
53e2822e
BA
749 int ret;
750
751 /* Word aligned channels only accept word size aligned data */
752 if (channel->info_word && len % 4)
753 return -EINVAL;
754
755 /* Reject packets that are too big */
756 if (tlen >= channel->fifo_size)
757 return -EINVAL;
758
33e3820d
BA
759 /* Highlight the fact that if we enter the loop below we might sleep */
760 if (wait)
761 might_sleep();
762
763 spin_lock_irqsave(&channel->tx_lock, flags);
53e2822e 764
b2c932e7
BA
765 while (qcom_smd_get_tx_avail(channel) < tlen &&
766 channel->state == SMD_CHANNEL_OPENED) {
53e2822e 767 if (!wait) {
1d74e7ed 768 ret = -EAGAIN;
c3388a07 769 goto out_unlock;
53e2822e
BA
770 }
771
772 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 0);
773
178f3f75 774 /* Wait without holding the tx_lock */
33e3820d 775 spin_unlock_irqrestore(&channel->tx_lock, flags);
178f3f75 776
53e2822e
BA
777 ret = wait_event_interruptible(channel->fblockread_event,
778 qcom_smd_get_tx_avail(channel) >= tlen ||
779 channel->state != SMD_CHANNEL_OPENED);
780 if (ret)
c3388a07 781 return ret;
53e2822e 782
33e3820d 783 spin_lock_irqsave(&channel->tx_lock, flags);
53e2822e
BA
784
785 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
786 }
787
b2c932e7
BA
788 /* Fail if the channel was closed */
789 if (channel->state != SMD_CHANNEL_OPENED) {
790 ret = -EPIPE;
c3388a07 791 goto out_unlock;
b2c932e7
BA
792 }
793
53e2822e
BA
794 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
795
796 qcom_smd_write_fifo(channel, hdr, sizeof(hdr));
797 qcom_smd_write_fifo(channel, data, len);
798
799 SET_TX_CHANNEL_FLAG(channel, fHEAD, 1);
800
801 /* Ensure ordering of channel info updates */
802 wmb();
803
804 qcom_smd_signal_channel(channel);
805
c3388a07 806out_unlock:
33e3820d 807 spin_unlock_irqrestore(&channel->tx_lock, flags);
53e2822e
BA
808
809 return ret;
810}
811
812/*
813 * Helper for opening a channel
814 */
815static int qcom_smd_channel_open(struct qcom_smd_channel *channel,
816 rpmsg_rx_cb_t cb)
817{
268105fb 818 struct qcom_smd_edge *edge = channel->edge;
53e2822e 819 size_t bb_size;
268105fb 820 int ret;
53e2822e
BA
821
822 /*
823 * Packets are maximum 4k, but reduce if the fifo is smaller
824 */
825 bb_size = min(channel->fifo_size, SZ_4K);
826 channel->bounce_buffer = kmalloc(bb_size, GFP_KERNEL);
827 if (!channel->bounce_buffer)
828 return -ENOMEM;
829
830 qcom_smd_channel_set_callback(channel, cb);
831 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENING);
268105fb
BA
832
833 /* Wait for remote to enter opening or opened */
834 ret = wait_event_interruptible_timeout(channel->state_change_event,
835 channel->remote_state == SMD_CHANNEL_OPENING ||
836 channel->remote_state == SMD_CHANNEL_OPENED,
837 HZ);
838 if (!ret) {
839 dev_err(&edge->dev, "remote side did not enter opening state\n");
840 goto out_close_timeout;
841 }
842
53e2822e
BA
843 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED);
844
268105fb
BA
845 /* Wait for remote to enter opened */
846 ret = wait_event_interruptible_timeout(channel->state_change_event,
847 channel->remote_state == SMD_CHANNEL_OPENED,
848 HZ);
849 if (!ret) {
850 dev_err(&edge->dev, "remote side did not enter open state\n");
851 goto out_close_timeout;
852 }
853
53e2822e 854 return 0;
268105fb
BA
855
856out_close_timeout:
857 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
858 return -ETIMEDOUT;
53e2822e
BA
859}
860
861/*
862 * Helper for closing and resetting a channel
863 */
864static void qcom_smd_channel_close(struct qcom_smd_channel *channel)
865{
866 qcom_smd_channel_set_callback(channel, NULL);
867
868 kfree(channel->bounce_buffer);
869 channel->bounce_buffer = NULL;
870
871 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
872 qcom_smd_channel_reset(channel);
873}
874
875static struct qcom_smd_channel *
876qcom_smd_find_channel(struct qcom_smd_edge *edge, const char *name)
877{
878 struct qcom_smd_channel *channel;
879 struct qcom_smd_channel *ret = NULL;
880 unsigned long flags;
53e2822e
BA
881
882 spin_lock_irqsave(&edge->channels_lock, flags);
883 list_for_each_entry(channel, &edge->channels, list) {
66dca399
BA
884 if (!strcmp(channel->name, name)) {
885 ret = channel;
886 break;
887 }
53e2822e
BA
888 }
889 spin_unlock_irqrestore(&edge->channels_lock, flags);
890
891 return ret;
892}
893
894static void __ept_release(struct kref *kref)
895{
896 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
897 refcount);
898 kfree(to_smd_endpoint(ept));
899}
900
901static struct rpmsg_endpoint *qcom_smd_create_ept(struct rpmsg_device *rpdev,
902 rpmsg_rx_cb_t cb, void *priv,
903 struct rpmsg_channel_info chinfo)
904{
905 struct qcom_smd_endpoint *qsept;
906 struct qcom_smd_channel *channel;
907 struct qcom_smd_device *qsdev = to_smd_device(rpdev);
908 struct qcom_smd_edge *edge = qsdev->edge;
909 struct rpmsg_endpoint *ept;
910 const char *name = chinfo.name;
911 int ret;
912
913 /* Wait up to HZ for the channel to appear */
914 ret = wait_event_interruptible_timeout(edge->new_channel_event,
915 (channel = qcom_smd_find_channel(edge, name)) != NULL,
916 HZ);
917 if (!ret)
918 return NULL;
919
920 if (channel->state != SMD_CHANNEL_CLOSED) {
921 dev_err(&rpdev->dev, "channel %s is busy\n", channel->name);
922 return NULL;
923 }
924
925 qsept = kzalloc(sizeof(*qsept), GFP_KERNEL);
926 if (!qsept)
927 return NULL;
928
929 ept = &qsept->ept;
930
931 kref_init(&ept->refcount);
932
933 ept->rpdev = rpdev;
934 ept->cb = cb;
935 ept->priv = priv;
936 ept->ops = &qcom_smd_endpoint_ops;
937
938 channel->qsept = qsept;
939 qsept->qsch = channel;
940
941 ret = qcom_smd_channel_open(channel, cb);
942 if (ret)
943 goto free_ept;
944
945 return ept;
946
947free_ept:
948 channel->qsept = NULL;
949 kref_put(&ept->refcount, __ept_release);
950 return NULL;
951}
952
953static void qcom_smd_destroy_ept(struct rpmsg_endpoint *ept)
954{
955 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
956 struct qcom_smd_channel *ch = qsept->qsch;
957
958 qcom_smd_channel_close(ch);
959 ch->qsept = NULL;
960 kref_put(&ept->refcount, __ept_release);
961}
962
963static int qcom_smd_send(struct rpmsg_endpoint *ept, void *data, int len)
964{
965 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
966
967 return __qcom_smd_send(qsept->qsch, data, len, true);
968}
969
970static int qcom_smd_trysend(struct rpmsg_endpoint *ept, void *data, int len)
971{
972 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
973
974 return __qcom_smd_send(qsept->qsch, data, len, false);
975}
976
afc9a42b 977static __poll_t qcom_smd_poll(struct rpmsg_endpoint *ept,
adaa11b0
BA
978 struct file *filp, poll_table *wait)
979{
980 struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
981 struct qcom_smd_channel *channel = qsept->qsch;
afc9a42b 982 __poll_t mask = 0;
adaa11b0
BA
983
984 poll_wait(filp, &channel->fblockread_event, wait);
985
986 if (qcom_smd_get_tx_avail(channel) > 20)
a9a08845 987 mask |= EPOLLOUT | EPOLLWRNORM;
adaa11b0
BA
988
989 return mask;
990}
991
53e2822e
BA
992/*
993 * Finds the device_node for the smd child interested in this channel.
994 */
995static struct device_node *qcom_smd_match_channel(struct device_node *edge_node,
996 const char *channel)
997{
998 struct device_node *child;
999 const char *name;
1000 const char *key;
1001 int ret;
1002
1003 for_each_available_child_of_node(edge_node, child) {
1004 key = "qcom,smd-channels";
1005 ret = of_property_read_string(child, key, &name);
1006 if (ret)
1007 continue;
1008
1009 if (strcmp(name, channel) == 0)
1010 return child;
1011 }
1012
1013 return NULL;
1014}
1015
0d72038c
BA
1016static int qcom_smd_announce_create(struct rpmsg_device *rpdev)
1017{
1018 struct qcom_smd_endpoint *qept = to_smd_endpoint(rpdev->ept);
1019 struct qcom_smd_channel *channel = qept->qsch;
1020 unsigned long flags;
1021 bool kick_state;
1022
1023 spin_lock_irqsave(&channel->recv_lock, flags);
1024 kick_state = qcom_smd_channel_intr(channel);
1025 spin_unlock_irqrestore(&channel->recv_lock, flags);
1026
1027 if (kick_state)
1028 schedule_work(&channel->edge->state_work);
1029
1030 return 0;
1031}
1032
53e2822e
BA
1033static const struct rpmsg_device_ops qcom_smd_device_ops = {
1034 .create_ept = qcom_smd_create_ept,
0d72038c 1035 .announce_create = qcom_smd_announce_create,
53e2822e
BA
1036};
1037
1038static const struct rpmsg_endpoint_ops qcom_smd_endpoint_ops = {
1039 .destroy_ept = qcom_smd_destroy_ept,
1040 .send = qcom_smd_send,
1041 .trysend = qcom_smd_trysend,
adaa11b0 1042 .poll = qcom_smd_poll,
53e2822e
BA
1043};
1044
b0b03b81
BA
1045static void qcom_smd_release_device(struct device *dev)
1046{
1047 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1048 struct qcom_smd_device *qsdev = to_smd_device(rpdev);
1049
1050 kfree(qsdev);
1051}
1052
53e2822e
BA
1053/*
1054 * Create a smd client device for channel that is being opened.
1055 */
1056static int qcom_smd_create_device(struct qcom_smd_channel *channel)
1057{
1058 struct qcom_smd_device *qsdev;
1059 struct rpmsg_device *rpdev;
1060 struct qcom_smd_edge *edge = channel->edge;
1061
1062 dev_dbg(&edge->dev, "registering '%s'\n", channel->name);
1063
1064 qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
1065 if (!qsdev)
1066 return -ENOMEM;
1067
1068 /* Link qsdev to our SMD edge */
1069 qsdev->edge = edge;
1070
1071 /* Assign callbacks for rpmsg_device */
1072 qsdev->rpdev.ops = &qcom_smd_device_ops;
1073
1074 /* Assign public information to the rpmsg_device */
1075 rpdev = &qsdev->rpdev;
1076 strncpy(rpdev->id.name, channel->name, RPMSG_NAME_SIZE);
1077 rpdev->src = RPMSG_ADDR_ANY;
1078 rpdev->dst = RPMSG_ADDR_ANY;
1079
1080 rpdev->dev.of_node = qcom_smd_match_channel(edge->of_node, channel->name);
1081 rpdev->dev.parent = &edge->dev;
b0b03b81 1082 rpdev->dev.release = qcom_smd_release_device;
53e2822e
BA
1083
1084 return rpmsg_register_device(rpdev);
1085}
1086
0be363bf
BA
1087static int qcom_smd_create_chrdev(struct qcom_smd_edge *edge)
1088{
1089 struct qcom_smd_device *qsdev;
1090
1091 qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
1092 if (!qsdev)
1093 return -ENOMEM;
1094
1095 qsdev->edge = edge;
1096 qsdev->rpdev.ops = &qcom_smd_device_ops;
1097 qsdev->rpdev.dev.parent = &edge->dev;
b0b03b81
BA
1098 qsdev->rpdev.dev.release = qcom_smd_release_device;
1099
0be363bf
BA
1100 return rpmsg_chrdev_register_device(&qsdev->rpdev);
1101}
1102
53e2822e
BA
1103/*
1104 * Allocate the qcom_smd_channel object for a newly found smd channel,
1105 * retrieving and validating the smem items involved.
1106 */
1107static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge,
1108 unsigned smem_info_item,
1109 unsigned smem_fifo_item,
1110 char *name)
1111{
1112 struct qcom_smd_channel *channel;
1113 size_t fifo_size;
1114 size_t info_size;
1115 void *fifo_base;
1116 void *info;
1117 int ret;
1118
1119 channel = devm_kzalloc(&edge->dev, sizeof(*channel), GFP_KERNEL);
1120 if (!channel)
1121 return ERR_PTR(-ENOMEM);
1122
1123 channel->edge = edge;
1124 channel->name = devm_kstrdup(&edge->dev, name, GFP_KERNEL);
1125 if (!channel->name)
1126 return ERR_PTR(-ENOMEM);
1127
33e3820d 1128 spin_lock_init(&channel->tx_lock);
53e2822e
BA
1129 spin_lock_init(&channel->recv_lock);
1130 init_waitqueue_head(&channel->fblockread_event);
268105fb 1131 init_waitqueue_head(&channel->state_change_event);
53e2822e
BA
1132
1133 info = qcom_smem_get(edge->remote_pid, smem_info_item, &info_size);
1134 if (IS_ERR(info)) {
1135 ret = PTR_ERR(info);
1136 goto free_name_and_channel;
1137 }
1138
1139 /*
1140 * Use the size of the item to figure out which channel info struct to
1141 * use.
1142 */
1143 if (info_size == 2 * sizeof(struct smd_channel_info_word)) {
1144 channel->info_word = info;
1145 } else if (info_size == 2 * sizeof(struct smd_channel_info)) {
1146 channel->info = info;
1147 } else {
1148 dev_err(&edge->dev,
1149 "channel info of size %zu not supported\n", info_size);
1150 ret = -EINVAL;
1151 goto free_name_and_channel;
1152 }
1153
1154 fifo_base = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_size);
1155 if (IS_ERR(fifo_base)) {
1156 ret = PTR_ERR(fifo_base);
1157 goto free_name_and_channel;
1158 }
1159
1160 /* The channel consist of a rx and tx fifo of equal size */
1161 fifo_size /= 2;
1162
1163 dev_dbg(&edge->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1164 name, info_size, fifo_size);
1165
1166 channel->tx_fifo = fifo_base;
1167 channel->rx_fifo = fifo_base + fifo_size;
1168 channel->fifo_size = fifo_size;
1169
1170 qcom_smd_channel_reset(channel);
1171
1172 return channel;
1173
1174free_name_and_channel:
1175 devm_kfree(&edge->dev, channel->name);
1176 devm_kfree(&edge->dev, channel);
1177
1178 return ERR_PTR(ret);
1179}
1180
1181/*
1182 * Scans the allocation table for any newly allocated channels, calls
1183 * qcom_smd_create_channel() to create representations of these and add
1184 * them to the edge's list of channels.
1185 */
1186static void qcom_channel_scan_worker(struct work_struct *work)
1187{
1188 struct qcom_smd_edge *edge = container_of(work, struct qcom_smd_edge, scan_work);
1189 struct qcom_smd_alloc_entry *alloc_tbl;
1190 struct qcom_smd_alloc_entry *entry;
1191 struct qcom_smd_channel *channel;
1192 unsigned long flags;
1193 unsigned fifo_id;
1194 unsigned info_id;
1195 int tbl;
1196 int i;
1197 u32 eflags, cid;
1198
1199 for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) {
1200 alloc_tbl = qcom_smem_get(edge->remote_pid,
1201 smem_items[tbl].alloc_tbl_id, NULL);
1202 if (IS_ERR(alloc_tbl))
1203 continue;
1204
1205 for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) {
1206 entry = &alloc_tbl[i];
1207 eflags = le32_to_cpu(entry->flags);
1208 if (test_bit(i, edge->allocated[tbl]))
1209 continue;
1210
1211 if (entry->ref_count == 0)
1212 continue;
1213
1214 if (!entry->name[0])
1215 continue;
1216
1217 if (!(eflags & SMD_CHANNEL_FLAGS_PACKET))
1218 continue;
1219
1220 if ((eflags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id)
1221 continue;
1222
1223 cid = le32_to_cpu(entry->cid);
1224 info_id = smem_items[tbl].info_base_id + cid;
1225 fifo_id = smem_items[tbl].fifo_base_id + cid;
1226
1227 channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
1228 if (IS_ERR(channel))
1229 continue;
1230
1231 spin_lock_irqsave(&edge->channels_lock, flags);
1232 list_add(&channel->list, &edge->channels);
1233 spin_unlock_irqrestore(&edge->channels_lock, flags);
1234
1235 dev_dbg(&edge->dev, "new channel found: '%s'\n", channel->name);
1236 set_bit(i, edge->allocated[tbl]);
1237
eb114f27 1238 wake_up_interruptible_all(&edge->new_channel_event);
53e2822e
BA
1239 }
1240 }
1241
1242 schedule_work(&edge->state_work);
1243}
1244
1245/*
1246 * This per edge worker scans smem for any new channels and register these. It
1247 * then scans all registered channels for state changes that should be handled
1248 * by creating or destroying smd client devices for the registered channels.
1249 *
1250 * LOCKING: edge->channels_lock only needs to cover the list operations, as the
1251 * worker is killed before any channels are deallocated
1252 */
1253static void qcom_channel_state_worker(struct work_struct *work)
1254{
1255 struct qcom_smd_channel *channel;
1256 struct qcom_smd_edge *edge = container_of(work,
1257 struct qcom_smd_edge,
1258 state_work);
1259 struct rpmsg_channel_info chinfo;
1260 unsigned remote_state;
1261 unsigned long flags;
1262
1263 /*
1264 * Register a device for any closed channel where the remote processor
1265 * is showing interest in opening the channel.
1266 */
1267 spin_lock_irqsave(&edge->channels_lock, flags);
1268 list_for_each_entry(channel, &edge->channels, list) {
1269 if (channel->state != SMD_CHANNEL_CLOSED)
1270 continue;
1271
2bd9b438
BA
1272 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1273 if (remote_state != SMD_CHANNEL_OPENING &&
1274 remote_state != SMD_CHANNEL_OPENED)
1275 continue;
1276
53e2822e
BA
1277 if (channel->registered)
1278 continue;
1279
1280 spin_unlock_irqrestore(&edge->channels_lock, flags);
1281 qcom_smd_create_device(channel);
1282 channel->registered = true;
1283 spin_lock_irqsave(&edge->channels_lock, flags);
1284
1285 channel->registered = true;
1286 }
1287
1288 /*
1289 * Unregister the device for any channel that is opened where the
1290 * remote processor is closing the channel.
1291 */
1292 list_for_each_entry(channel, &edge->channels, list) {
1293 if (channel->state != SMD_CHANNEL_OPENING &&
1294 channel->state != SMD_CHANNEL_OPENED)
1295 continue;
1296
1297 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1298 if (remote_state == SMD_CHANNEL_OPENING ||
1299 remote_state == SMD_CHANNEL_OPENED)
1300 continue;
1301
1302 spin_unlock_irqrestore(&edge->channels_lock, flags);
1303
1304 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1305 chinfo.src = RPMSG_ADDR_ANY;
1306 chinfo.dst = RPMSG_ADDR_ANY;
1307 rpmsg_unregister_device(&edge->dev, &chinfo);
1308 channel->registered = false;
1309 spin_lock_irqsave(&edge->channels_lock, flags);
1310 }
1311 spin_unlock_irqrestore(&edge->channels_lock, flags);
1312}
1313
1314/*
1315 * Parses an of_node describing an edge.
1316 */
1317static int qcom_smd_parse_edge(struct device *dev,
1318 struct device_node *node,
1319 struct qcom_smd_edge *edge)
1320{
1321 struct device_node *syscon_np;
1322 const char *key;
1323 int irq;
1324 int ret;
1325
1326 INIT_LIST_HEAD(&edge->channels);
1327 spin_lock_init(&edge->channels_lock);
1328
1329 INIT_WORK(&edge->scan_work, qcom_channel_scan_worker);
1330 INIT_WORK(&edge->state_work, qcom_channel_state_worker);
1331
1332 edge->of_node = of_node_get(node);
1333
1334 key = "qcom,smd-edge";
1335 ret = of_property_read_u32(node, key, &edge->edge_id);
1336 if (ret) {
1337 dev_err(dev, "edge missing %s property\n", key);
1338 return -EINVAL;
1339 }
1340
1341 edge->remote_pid = QCOM_SMEM_HOST_ANY;
1342 key = "qcom,remote-pid";
1343 of_property_read_u32(node, key, &edge->remote_pid);
1344
ab460a2e
BA
1345 edge->mbox_client.dev = dev;
1346 edge->mbox_client.knows_txdone = true;
1347 edge->mbox_chan = mbox_request_channel(&edge->mbox_client, 0);
1348 if (IS_ERR(edge->mbox_chan)) {
1349 if (PTR_ERR(edge->mbox_chan) != -ENODEV)
1350 return PTR_ERR(edge->mbox_chan);
53e2822e 1351
ab460a2e 1352 edge->mbox_chan = NULL;
53e2822e 1353
ab460a2e
BA
1354 syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
1355 if (!syscon_np) {
1356 dev_err(dev, "no qcom,ipc node\n");
1357 return -ENODEV;
1358 }
53e2822e 1359
ab460a2e
BA
1360 edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
1361 if (IS_ERR(edge->ipc_regmap))
1362 return PTR_ERR(edge->ipc_regmap);
1363
1364 key = "qcom,ipc";
1365 ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
1366 if (ret < 0) {
1367 dev_err(dev, "no offset in %s\n", key);
1368 return -EINVAL;
1369 }
1370
1371 ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
1372 if (ret < 0) {
1373 dev_err(dev, "no bit in %s\n", key);
1374 return -EINVAL;
1375 }
53e2822e
BA
1376 }
1377
5e53c42c
BA
1378 ret = of_property_read_string(node, "label", &edge->name);
1379 if (ret < 0)
1380 edge->name = node->name;
1381
53e2822e
BA
1382 irq = irq_of_parse_and_map(node, 0);
1383 if (irq < 0) {
1384 dev_err(dev, "required smd interrupt missing\n");
1385 return -EINVAL;
1386 }
1387
1388 ret = devm_request_irq(dev, irq,
1389 qcom_smd_edge_intr, IRQF_TRIGGER_RISING,
1390 node->name, edge);
1391 if (ret) {
1392 dev_err(dev, "failed to request smd irq\n");
1393 return ret;
1394 }
1395
1396 edge->irq = irq;
1397
1398 return 0;
1399}
1400
1401/*
1402 * Release function for an edge.
1403 * Reset the state of each associated channel and free the edge context.
1404 */
1405static void qcom_smd_edge_release(struct device *dev)
1406{
1407 struct qcom_smd_channel *channel;
1408 struct qcom_smd_edge *edge = to_smd_edge(dev);
1409
1410 list_for_each_entry(channel, &edge->channels, list) {
1411 SET_RX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
1412 SET_RX_CHANNEL_INFO(channel, head, 0);
1413 SET_RX_CHANNEL_INFO(channel, tail, 0);
1414 }
1415
1416 kfree(edge);
1417}
1418
5e53c42c
BA
1419static ssize_t rpmsg_name_show(struct device *dev,
1420 struct device_attribute *attr, char *buf)
1421{
1422 struct qcom_smd_edge *edge = to_smd_edge(dev);
1423
1424 return sprintf(buf, "%s\n", edge->name);
1425}
1426static DEVICE_ATTR_RO(rpmsg_name);
1427
1428static struct attribute *qcom_smd_edge_attrs[] = {
1429 &dev_attr_rpmsg_name.attr,
1430 NULL
1431};
1432ATTRIBUTE_GROUPS(qcom_smd_edge);
1433
53e2822e
BA
1434/**
1435 * qcom_smd_register_edge() - register an edge based on an device_node
1436 * @parent: parent device for the edge
1437 * @node: device_node describing the edge
1438 *
1439 * Returns an edge reference, or negative ERR_PTR() on failure.
1440 */
1441struct qcom_smd_edge *qcom_smd_register_edge(struct device *parent,
1442 struct device_node *node)
1443{
1444 struct qcom_smd_edge *edge;
1445 int ret;
1446
1447 edge = kzalloc(sizeof(*edge), GFP_KERNEL);
1448 if (!edge)
1449 return ERR_PTR(-ENOMEM);
1450
1451 init_waitqueue_head(&edge->new_channel_event);
1452
1453 edge->dev.parent = parent;
1454 edge->dev.release = qcom_smd_edge_release;
aaafb24e 1455 edge->dev.of_node = node;
5e53c42c 1456 edge->dev.groups = qcom_smd_edge_groups;
53e2822e
BA
1457 dev_set_name(&edge->dev, "%s:%s", dev_name(parent), node->name);
1458 ret = device_register(&edge->dev);
1459 if (ret) {
1460 pr_err("failed to register smd edge\n");
be5acd24 1461 put_device(&edge->dev);
53e2822e
BA
1462 return ERR_PTR(ret);
1463 }
1464
1465 ret = qcom_smd_parse_edge(&edge->dev, node, edge);
1466 if (ret) {
1467 dev_err(&edge->dev, "failed to parse smd edge\n");
1468 goto unregister_dev;
1469 }
1470
0be363bf
BA
1471 ret = qcom_smd_create_chrdev(edge);
1472 if (ret) {
1473 dev_err(&edge->dev, "failed to register chrdev for edge\n");
1474 goto unregister_dev;
1475 }
1476
53e2822e
BA
1477 schedule_work(&edge->scan_work);
1478
1479 return edge;
1480
1481unregister_dev:
ab460a2e
BA
1482 if (!IS_ERR_OR_NULL(edge->mbox_chan))
1483 mbox_free_channel(edge->mbox_chan);
1484
be5acd24 1485 device_unregister(&edge->dev);
53e2822e
BA
1486 return ERR_PTR(ret);
1487}
1488EXPORT_SYMBOL(qcom_smd_register_edge);
1489
1490static int qcom_smd_remove_device(struct device *dev, void *data)
1491{
1492 device_unregister(dev);
1493
1494 return 0;
1495}
1496
1497/**
1498 * qcom_smd_unregister_edge() - release an edge and its children
1499 * @edge: edge reference acquired from qcom_smd_register_edge
1500 */
1501int qcom_smd_unregister_edge(struct qcom_smd_edge *edge)
1502{
1503 int ret;
1504
1505 disable_irq(edge->irq);
1506 cancel_work_sync(&edge->scan_work);
1507 cancel_work_sync(&edge->state_work);
1508
1509 ret = device_for_each_child(&edge->dev, NULL, qcom_smd_remove_device);
1510 if (ret)
1511 dev_warn(&edge->dev, "can't remove smd device: %d\n", ret);
1512
ab460a2e 1513 mbox_free_channel(edge->mbox_chan);
53e2822e
BA
1514 device_unregister(&edge->dev);
1515
1516 return 0;
1517}
1518EXPORT_SYMBOL(qcom_smd_unregister_edge);
1519
1520static int qcom_smd_probe(struct platform_device *pdev)
1521{
1522 struct device_node *node;
1523 void *p;
1524
1525 /* Wait for smem */
1526 p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL);
1527 if (PTR_ERR(p) == -EPROBE_DEFER)
1528 return PTR_ERR(p);
1529
1530 for_each_available_child_of_node(pdev->dev.of_node, node)
1531 qcom_smd_register_edge(&pdev->dev, node);
1532
1533 return 0;
1534}
1535
1536static int qcom_smd_remove_edge(struct device *dev, void *data)
1537{
1538 struct qcom_smd_edge *edge = to_smd_edge(dev);
1539
1540 return qcom_smd_unregister_edge(edge);
1541}
1542
1543/*
1544 * Shut down all smd clients by making sure that each edge stops processing
1545 * events and scanning for new channels, then call destroy on the devices.
1546 */
1547static int qcom_smd_remove(struct platform_device *pdev)
1548{
1549 int ret;
1550
1551 ret = device_for_each_child(&pdev->dev, NULL, qcom_smd_remove_edge);
1552 if (ret)
1553 dev_warn(&pdev->dev, "can't remove smd device: %d\n", ret);
1554
1555 return ret;
1556}
1557
1558static const struct of_device_id qcom_smd_of_match[] = {
1559 { .compatible = "qcom,smd" },
1560 {}
1561};
1562MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
1563
1564static struct platform_driver qcom_smd_driver = {
1565 .probe = qcom_smd_probe,
1566 .remove = qcom_smd_remove,
1567 .driver = {
1568 .name = "qcom-smd",
1569 .of_match_table = qcom_smd_of_match,
1570 },
1571};
1572
1573static int __init qcom_smd_init(void)
1574{
1575 return platform_driver_register(&qcom_smd_driver);
1576}
1577subsys_initcall(qcom_smd_init);
1578
1579static void __exit qcom_smd_exit(void)
1580{
1581 platform_driver_unregister(&qcom_smd_driver);
1582}
1583module_exit(qcom_smd_exit);
1584
1585MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
1586MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
1587MODULE_LICENSE("GPL v2");