soc: qcom: smem: Handle big endian CPUs
[deliverable/linux.git] / drivers / soc / qcom / smd.c
CommitLineData
f2ab3298
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>
17#include <linux/mfd/syscon.h>
18#include <linux/module.h>
19#include <linux/of_irq.h>
20#include <linux/of_platform.h>
21#include <linux/platform_device.h>
22#include <linux/regmap.h>
23#include <linux/sched.h>
24#include <linux/slab.h>
25#include <linux/soc/qcom/smd.h>
26#include <linux/soc/qcom/smem.h>
27#include <linux/wait.h>
28
29/*
30 * The Qualcomm Shared Memory communication solution provides point-to-point
31 * channels for clients to send and receive streaming or packet based data.
32 *
33 * Each channel consists of a control item (channel info) and a ring buffer
34 * pair. The channel info carry information related to channel state, flow
35 * control and the offsets within the ring buffer.
36 *
37 * All allocated channels are listed in an allocation table, identifying the
38 * pair of items by name, type and remote processor.
39 *
40 * Upon creating a new channel the remote processor allocates channel info and
41 * ring buffer items from the smem heap and populate the allocation table. An
42 * interrupt is sent to the other end of the channel and a scan for new
43 * channels should be done. A channel never goes away, it will only change
44 * state.
45 *
46 * The remote processor signals it intent for bring up the communication
47 * channel by setting the state of its end of the channel to "opening" and
48 * sends out an interrupt. We detect this change and register a smd device to
49 * consume the channel. Upon finding a consumer we finish the handshake and the
50 * channel is up.
51 *
52 * Upon closing a channel, the remote processor will update the state of its
53 * end of the channel and signal us, we will then unregister any attached
54 * device and close our end of the channel.
55 *
56 * Devices attached to a channel can use the qcom_smd_send function to push
57 * data to the channel, this is done by copying the data into the tx ring
58 * buffer, updating the pointers in the channel info and signaling the remote
59 * processor.
60 *
61 * The remote processor does the equivalent when it transfer data and upon
62 * receiving the interrupt we check the channel info for new data and delivers
63 * this to the attached device. If the device is not ready to receive the data
64 * we leave it in the ring buffer for now.
65 */
66
67struct smd_channel_info;
68struct smd_channel_info_word;
69
70#define SMD_ALLOC_TBL_COUNT 2
71#define SMD_ALLOC_TBL_SIZE 64
72
73/*
74 * This lists the various smem heap items relevant for the allocation table and
75 * smd channel entries.
76 */
77static const struct {
78 unsigned alloc_tbl_id;
79 unsigned info_base_id;
80 unsigned fifo_base_id;
81} smem_items[SMD_ALLOC_TBL_COUNT] = {
82 {
83 .alloc_tbl_id = 13,
84 .info_base_id = 14,
85 .fifo_base_id = 338
86 },
87 {
88 .alloc_tbl_id = 14,
89 .info_base_id = 266,
90 .fifo_base_id = 202,
91 },
92};
93
94/**
95 * struct qcom_smd_edge - representing a remote processor
96 * @smd: handle to qcom_smd
97 * @of_node: of_node handle for information related to this edge
98 * @edge_id: identifier of this edge
93dbed91 99 * @remote_pid: identifier of remote processor
f2ab3298
BA
100 * @irq: interrupt for signals on this edge
101 * @ipc_regmap: regmap handle holding the outgoing ipc register
102 * @ipc_offset: offset within @ipc_regmap of the register for ipc
103 * @ipc_bit: bit in the register at @ipc_offset of @ipc_regmap
104 * @channels: list of all channels detected on this edge
105 * @channels_lock: guard for modifications of @channels
106 * @allocated: array of bitmaps representing already allocated channels
107 * @need_rescan: flag that the @work needs to scan smem for new channels
108 * @smem_available: last available amount of smem triggering a channel scan
109 * @work: work item for edge house keeping
110 */
111struct qcom_smd_edge {
112 struct qcom_smd *smd;
113 struct device_node *of_node;
114 unsigned edge_id;
93dbed91 115 unsigned remote_pid;
f2ab3298
BA
116
117 int irq;
118
119 struct regmap *ipc_regmap;
120 int ipc_offset;
121 int ipc_bit;
122
123 struct list_head channels;
124 spinlock_t channels_lock;
125
126 DECLARE_BITMAP(allocated[SMD_ALLOC_TBL_COUNT], SMD_ALLOC_TBL_SIZE);
127
128 bool need_rescan;
129 unsigned smem_available;
130
131 struct work_struct work;
132};
133
134/*
135 * SMD channel states.
136 */
137enum smd_channel_state {
138 SMD_CHANNEL_CLOSED,
139 SMD_CHANNEL_OPENING,
140 SMD_CHANNEL_OPENED,
141 SMD_CHANNEL_FLUSHING,
142 SMD_CHANNEL_CLOSING,
143 SMD_CHANNEL_RESET,
144 SMD_CHANNEL_RESET_OPENING
145};
146
147/**
148 * struct qcom_smd_channel - smd channel struct
149 * @edge: qcom_smd_edge this channel is living on
150 * @qsdev: reference to a associated smd client device
151 * @name: name of the channel
152 * @state: local state of the channel
153 * @remote_state: remote state of the channel
154 * @tx_info: byte aligned outgoing channel info
155 * @rx_info: byte aligned incoming channel info
156 * @tx_info_word: word aligned outgoing channel info
157 * @rx_info_word: word aligned incoming channel info
158 * @tx_lock: lock to make writes to the channel mutually exclusive
159 * @fblockread_event: wakeup event tied to tx fBLOCKREADINTR
160 * @tx_fifo: pointer to the outgoing ring buffer
161 * @rx_fifo: pointer to the incoming ring buffer
162 * @fifo_size: size of each ring buffer
163 * @bounce_buffer: bounce buffer for reading wrapped packets
164 * @cb: callback function registered for this channel
165 * @recv_lock: guard for rx info modifications and cb pointer
166 * @pkt_size: size of the currently handled packet
167 * @list: lite entry for @channels in qcom_smd_edge
168 */
169struct qcom_smd_channel {
170 struct qcom_smd_edge *edge;
171
172 struct qcom_smd_device *qsdev;
173
174 char *name;
175 enum smd_channel_state state;
176 enum smd_channel_state remote_state;
177
178 struct smd_channel_info *tx_info;
179 struct smd_channel_info *rx_info;
180
181 struct smd_channel_info_word *tx_info_word;
182 struct smd_channel_info_word *rx_info_word;
183
184 struct mutex tx_lock;
185 wait_queue_head_t fblockread_event;
186
187 void *tx_fifo;
188 void *rx_fifo;
189 int fifo_size;
190
191 void *bounce_buffer;
192 int (*cb)(struct qcom_smd_device *, const void *, size_t);
193
194 spinlock_t recv_lock;
195
196 int pkt_size;
197
198 struct list_head list;
199};
200
201/**
202 * struct qcom_smd - smd struct
203 * @dev: device struct
204 * @num_edges: number of entries in @edges
205 * @edges: array of edges to be handled
206 */
207struct qcom_smd {
208 struct device *dev;
209
210 unsigned num_edges;
211 struct qcom_smd_edge edges[0];
212};
213
214/*
215 * Format of the smd_info smem items, for byte aligned channels.
216 */
217struct smd_channel_info {
218 u32 state;
219 u8 fDSR;
220 u8 fCTS;
221 u8 fCD;
222 u8 fRI;
223 u8 fHEAD;
224 u8 fTAIL;
225 u8 fSTATE;
226 u8 fBLOCKREADINTR;
227 u32 tail;
228 u32 head;
229};
230
231/*
232 * Format of the smd_info smem items, for word aligned channels.
233 */
234struct smd_channel_info_word {
235 u32 state;
236 u32 fDSR;
237 u32 fCTS;
238 u32 fCD;
239 u32 fRI;
240 u32 fHEAD;
241 u32 fTAIL;
242 u32 fSTATE;
243 u32 fBLOCKREADINTR;
244 u32 tail;
245 u32 head;
246};
247
248#define GET_RX_CHANNEL_INFO(channel, param) \
249 (channel->rx_info_word ? \
250 channel->rx_info_word->param : \
251 channel->rx_info->param)
252
253#define SET_RX_CHANNEL_INFO(channel, param, value) \
254 (channel->rx_info_word ? \
255 (channel->rx_info_word->param = value) : \
256 (channel->rx_info->param = value))
257
258#define GET_TX_CHANNEL_INFO(channel, param) \
259 (channel->tx_info_word ? \
260 channel->tx_info_word->param : \
261 channel->tx_info->param)
262
263#define SET_TX_CHANNEL_INFO(channel, param, value) \
264 (channel->tx_info_word ? \
265 (channel->tx_info_word->param = value) : \
266 (channel->tx_info->param = value))
267
268/**
269 * struct qcom_smd_alloc_entry - channel allocation entry
270 * @name: channel name
271 * @cid: channel index
272 * @flags: channel flags and edge id
273 * @ref_count: reference count of the channel
274 */
275struct qcom_smd_alloc_entry {
276 u8 name[20];
277 u32 cid;
278 u32 flags;
279 u32 ref_count;
280} __packed;
281
282#define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff
283#define SMD_CHANNEL_FLAGS_STREAM BIT(8)
284#define SMD_CHANNEL_FLAGS_PACKET BIT(9)
285
286/*
287 * Each smd packet contains a 20 byte header, with the first 4 being the length
288 * of the packet.
289 */
290#define SMD_PACKET_HEADER_LEN 20
291
292/*
293 * Signal the remote processor associated with 'channel'.
294 */
295static void qcom_smd_signal_channel(struct qcom_smd_channel *channel)
296{
297 struct qcom_smd_edge *edge = channel->edge;
298
299 regmap_write(edge->ipc_regmap, edge->ipc_offset, BIT(edge->ipc_bit));
300}
301
302/*
303 * Initialize the tx channel info
304 */
305static void qcom_smd_channel_reset(struct qcom_smd_channel *channel)
306{
307 SET_TX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
308 SET_TX_CHANNEL_INFO(channel, fDSR, 0);
309 SET_TX_CHANNEL_INFO(channel, fCTS, 0);
310 SET_TX_CHANNEL_INFO(channel, fCD, 0);
311 SET_TX_CHANNEL_INFO(channel, fRI, 0);
312 SET_TX_CHANNEL_INFO(channel, fHEAD, 0);
313 SET_TX_CHANNEL_INFO(channel, fTAIL, 0);
314 SET_TX_CHANNEL_INFO(channel, fSTATE, 1);
208487a8 315 SET_TX_CHANNEL_INFO(channel, fBLOCKREADINTR, 1);
f2ab3298
BA
316 SET_TX_CHANNEL_INFO(channel, head, 0);
317 SET_TX_CHANNEL_INFO(channel, tail, 0);
318
319 qcom_smd_signal_channel(channel);
320
321 channel->state = SMD_CHANNEL_CLOSED;
322 channel->pkt_size = 0;
323}
324
325/*
326 * Calculate the amount of data available in the rx fifo
327 */
328static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel *channel)
329{
330 unsigned head;
331 unsigned tail;
332
333 head = GET_RX_CHANNEL_INFO(channel, head);
334 tail = GET_RX_CHANNEL_INFO(channel, tail);
335
336 return (head - tail) & (channel->fifo_size - 1);
337}
338
339/*
340 * Set tx channel state and inform the remote processor
341 */
342static void qcom_smd_channel_set_state(struct qcom_smd_channel *channel,
343 int state)
344{
345 struct qcom_smd_edge *edge = channel->edge;
346 bool is_open = state == SMD_CHANNEL_OPENED;
347
348 if (channel->state == state)
349 return;
350
351 dev_dbg(edge->smd->dev, "set_state(%s, %d)\n", channel->name, state);
352
353 SET_TX_CHANNEL_INFO(channel, fDSR, is_open);
354 SET_TX_CHANNEL_INFO(channel, fCTS, is_open);
355 SET_TX_CHANNEL_INFO(channel, fCD, is_open);
356
357 SET_TX_CHANNEL_INFO(channel, state, state);
358 SET_TX_CHANNEL_INFO(channel, fSTATE, 1);
359
360 channel->state = state;
361 qcom_smd_signal_channel(channel);
362}
363
364/*
365 * Copy count bytes of data using 32bit accesses, if that's required.
366 */
367static void smd_copy_to_fifo(void __iomem *_dst,
368 const void *_src,
369 size_t count,
370 bool word_aligned)
371{
372 u32 *dst = (u32 *)_dst;
373 u32 *src = (u32 *)_src;
374
375 if (word_aligned) {
376 count /= sizeof(u32);
377 while (count--)
378 writel_relaxed(*src++, dst++);
379 } else {
380 memcpy_toio(_dst, _src, count);
381 }
382}
383
384/*
385 * Copy count bytes of data using 32bit accesses, if that is required.
386 */
387static void smd_copy_from_fifo(void *_dst,
388 const void __iomem *_src,
389 size_t count,
390 bool word_aligned)
391{
392 u32 *dst = (u32 *)_dst;
393 u32 *src = (u32 *)_src;
394
395 if (word_aligned) {
396 count /= sizeof(u32);
397 while (count--)
398 *dst++ = readl_relaxed(src++);
399 } else {
400 memcpy_fromio(_dst, _src, count);
401 }
402}
403
404/*
405 * Read count bytes of data from the rx fifo into buf, but don't advance the
406 * tail.
407 */
408static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel,
409 void *buf, size_t count)
410{
411 bool word_aligned;
412 unsigned tail;
413 size_t len;
414
415 word_aligned = channel->rx_info_word != NULL;
416 tail = GET_RX_CHANNEL_INFO(channel, tail);
417
418 len = min_t(size_t, count, channel->fifo_size - tail);
419 if (len) {
420 smd_copy_from_fifo(buf,
421 channel->rx_fifo + tail,
422 len,
423 word_aligned);
424 }
425
426 if (len != count) {
427 smd_copy_from_fifo(buf + len,
428 channel->rx_fifo,
429 count - len,
430 word_aligned);
431 }
432
433 return count;
434}
435
436/*
437 * Advance the rx tail by count bytes.
438 */
439static void qcom_smd_channel_advance(struct qcom_smd_channel *channel,
440 size_t count)
441{
442 unsigned tail;
443
444 tail = GET_RX_CHANNEL_INFO(channel, tail);
445 tail += count;
446 tail &= (channel->fifo_size - 1);
447 SET_RX_CHANNEL_INFO(channel, tail, tail);
448}
449
450/*
451 * Read out a single packet from the rx fifo and deliver it to the device
452 */
453static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
454{
455 struct qcom_smd_device *qsdev = channel->qsdev;
456 unsigned tail;
457 size_t len;
458 void *ptr;
459 int ret;
460
461 if (!channel->cb)
462 return 0;
463
464 tail = GET_RX_CHANNEL_INFO(channel, tail);
465
466 /* Use bounce buffer if the data wraps */
467 if (tail + channel->pkt_size >= channel->fifo_size) {
468 ptr = channel->bounce_buffer;
469 len = qcom_smd_channel_peek(channel, ptr, channel->pkt_size);
470 } else {
471 ptr = channel->rx_fifo + tail;
472 len = channel->pkt_size;
473 }
474
475 ret = channel->cb(qsdev, ptr, len);
476 if (ret < 0)
477 return ret;
478
479 /* Only forward the tail if the client consumed the data */
480 qcom_smd_channel_advance(channel, len);
481
482 channel->pkt_size = 0;
483
484 return 0;
485}
486
487/*
488 * Per channel interrupt handling
489 */
490static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel)
491{
492 bool need_state_scan = false;
493 int remote_state;
494 u32 pktlen;
495 int avail;
496 int ret;
497
498 /* Handle state changes */
499 remote_state = GET_RX_CHANNEL_INFO(channel, state);
500 if (remote_state != channel->remote_state) {
501 channel->remote_state = remote_state;
502 need_state_scan = true;
503 }
504 /* Indicate that we have seen any state change */
505 SET_RX_CHANNEL_INFO(channel, fSTATE, 0);
506
507 /* Signal waiting qcom_smd_send() about the interrupt */
508 if (!GET_TX_CHANNEL_INFO(channel, fBLOCKREADINTR))
509 wake_up_interruptible(&channel->fblockread_event);
510
511 /* Don't consume any data until we've opened the channel */
512 if (channel->state != SMD_CHANNEL_OPENED)
513 goto out;
514
515 /* Indicate that we've seen the new data */
516 SET_RX_CHANNEL_INFO(channel, fHEAD, 0);
517
518 /* Consume data */
519 for (;;) {
520 avail = qcom_smd_channel_get_rx_avail(channel);
521
522 if (!channel->pkt_size && avail >= SMD_PACKET_HEADER_LEN) {
523 qcom_smd_channel_peek(channel, &pktlen, sizeof(pktlen));
524 qcom_smd_channel_advance(channel, SMD_PACKET_HEADER_LEN);
525 channel->pkt_size = pktlen;
526 } else if (channel->pkt_size && avail >= channel->pkt_size) {
527 ret = qcom_smd_channel_recv_single(channel);
528 if (ret)
529 break;
530 } else {
531 break;
532 }
533 }
534
535 /* Indicate that we have seen and updated tail */
536 SET_RX_CHANNEL_INFO(channel, fTAIL, 1);
537
538 /* Signal the remote that we've consumed the data (if requested) */
539 if (!GET_RX_CHANNEL_INFO(channel, fBLOCKREADINTR)) {
540 /* Ensure ordering of channel info updates */
541 wmb();
542
543 qcom_smd_signal_channel(channel);
544 }
545
546out:
547 return need_state_scan;
548}
549
550/*
551 * The edge interrupts are triggered by the remote processor on state changes,
552 * channel info updates or when new channels are created.
553 */
554static irqreturn_t qcom_smd_edge_intr(int irq, void *data)
555{
556 struct qcom_smd_edge *edge = data;
557 struct qcom_smd_channel *channel;
558 unsigned available;
559 bool kick_worker = false;
560
561 /*
562 * Handle state changes or data on each of the channels on this edge
563 */
564 spin_lock(&edge->channels_lock);
565 list_for_each_entry(channel, &edge->channels, list) {
566 spin_lock(&channel->recv_lock);
567 kick_worker |= qcom_smd_channel_intr(channel);
568 spin_unlock(&channel->recv_lock);
569 }
570 spin_unlock(&edge->channels_lock);
571
572 /*
573 * Creating a new channel requires allocating an smem entry, so we only
574 * have to scan if the amount of available space in smem have changed
575 * since last scan.
576 */
93dbed91 577 available = qcom_smem_get_free_space(edge->remote_pid);
f2ab3298
BA
578 if (available != edge->smem_available) {
579 edge->smem_available = available;
580 edge->need_rescan = true;
581 kick_worker = true;
582 }
583
584 if (kick_worker)
585 schedule_work(&edge->work);
586
587 return IRQ_HANDLED;
588}
589
590/*
591 * Delivers any outstanding packets in the rx fifo, can be used after probe of
592 * the clients to deliver any packets that wasn't delivered before the client
593 * was setup.
594 */
595static void qcom_smd_channel_resume(struct qcom_smd_channel *channel)
596{
597 unsigned long flags;
598
599 spin_lock_irqsave(&channel->recv_lock, flags);
600 qcom_smd_channel_intr(channel);
601 spin_unlock_irqrestore(&channel->recv_lock, flags);
602}
603
604/*
605 * Calculate how much space is available in the tx fifo.
606 */
607static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel)
608{
609 unsigned head;
610 unsigned tail;
611 unsigned mask = channel->fifo_size - 1;
612
613 head = GET_TX_CHANNEL_INFO(channel, head);
614 tail = GET_TX_CHANNEL_INFO(channel, tail);
615
616 return mask - ((head - tail) & mask);
617}
618
619/*
620 * Write count bytes of data into channel, possibly wrapping in the ring buffer
621 */
622static int qcom_smd_write_fifo(struct qcom_smd_channel *channel,
623 const void *data,
624 size_t count)
625{
626 bool word_aligned;
627 unsigned head;
628 size_t len;
629
630 word_aligned = channel->tx_info_word != NULL;
631 head = GET_TX_CHANNEL_INFO(channel, head);
632
633 len = min_t(size_t, count, channel->fifo_size - head);
634 if (len) {
635 smd_copy_to_fifo(channel->tx_fifo + head,
636 data,
637 len,
638 word_aligned);
639 }
640
641 if (len != count) {
642 smd_copy_to_fifo(channel->tx_fifo,
643 data + len,
644 count - len,
645 word_aligned);
646 }
647
648 head += count;
649 head &= (channel->fifo_size - 1);
650 SET_TX_CHANNEL_INFO(channel, head, head);
651
652 return count;
653}
654
655/**
656 * qcom_smd_send - write data to smd channel
657 * @channel: channel handle
658 * @data: buffer of data to write
659 * @len: number of bytes to write
660 *
661 * This is a blocking write of len bytes into the channel's tx ring buffer and
662 * signal the remote end. It will sleep until there is enough space available
663 * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
664 * polling.
665 */
666int qcom_smd_send(struct qcom_smd_channel *channel, const void *data, int len)
667{
668 u32 hdr[5] = {len,};
669 int tlen = sizeof(hdr) + len;
670 int ret;
671
672 /* Word aligned channels only accept word size aligned data */
673 if (channel->rx_info_word != NULL && len % 4)
674 return -EINVAL;
675
676 ret = mutex_lock_interruptible(&channel->tx_lock);
677 if (ret)
678 return ret;
679
680 while (qcom_smd_get_tx_avail(channel) < tlen) {
681 if (channel->state != SMD_CHANNEL_OPENED) {
682 ret = -EPIPE;
683 goto out;
684 }
685
208487a8 686 SET_TX_CHANNEL_INFO(channel, fBLOCKREADINTR, 0);
f2ab3298
BA
687
688 ret = wait_event_interruptible(channel->fblockread_event,
689 qcom_smd_get_tx_avail(channel) >= tlen ||
690 channel->state != SMD_CHANNEL_OPENED);
691 if (ret)
692 goto out;
693
208487a8 694 SET_TX_CHANNEL_INFO(channel, fBLOCKREADINTR, 1);
f2ab3298
BA
695 }
696
697 SET_TX_CHANNEL_INFO(channel, fTAIL, 0);
698
699 qcom_smd_write_fifo(channel, hdr, sizeof(hdr));
700 qcom_smd_write_fifo(channel, data, len);
701
702 SET_TX_CHANNEL_INFO(channel, fHEAD, 1);
703
704 /* Ensure ordering of channel info updates */
705 wmb();
706
707 qcom_smd_signal_channel(channel);
708
709out:
710 mutex_unlock(&channel->tx_lock);
711
712 return ret;
713}
714EXPORT_SYMBOL(qcom_smd_send);
715
716static struct qcom_smd_device *to_smd_device(struct device *dev)
717{
718 return container_of(dev, struct qcom_smd_device, dev);
719}
720
721static struct qcom_smd_driver *to_smd_driver(struct device *dev)
722{
723 struct qcom_smd_device *qsdev = to_smd_device(dev);
724
725 return container_of(qsdev->dev.driver, struct qcom_smd_driver, driver);
726}
727
728static int qcom_smd_dev_match(struct device *dev, struct device_driver *drv)
729{
1a7caca2
BA
730 struct qcom_smd_device *qsdev = to_smd_device(dev);
731 struct qcom_smd_driver *qsdrv = container_of(drv, struct qcom_smd_driver, driver);
732 const struct qcom_smd_id *match = qsdrv->smd_match_table;
733 const char *name = qsdev->channel->name;
734
735 if (match) {
736 while (match->name[0]) {
737 if (!strcmp(match->name, name))
738 return 1;
739 match++;
740 }
741 }
742
f2ab3298
BA
743 return of_driver_match_device(dev, drv);
744}
745
746/*
747 * Probe the smd client.
748 *
749 * The remote side have indicated that it want the channel to be opened, so
750 * complete the state handshake and probe our client driver.
751 */
752static int qcom_smd_dev_probe(struct device *dev)
753{
754 struct qcom_smd_device *qsdev = to_smd_device(dev);
755 struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
756 struct qcom_smd_channel *channel = qsdev->channel;
757 size_t bb_size;
758 int ret;
759
760 /*
761 * Packets are maximum 4k, but reduce if the fifo is smaller
762 */
763 bb_size = min(channel->fifo_size, SZ_4K);
764 channel->bounce_buffer = kmalloc(bb_size, GFP_KERNEL);
765 if (!channel->bounce_buffer)
766 return -ENOMEM;
767
768 channel->cb = qsdrv->callback;
769
770 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENING);
771
772 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED);
773
774 ret = qsdrv->probe(qsdev);
775 if (ret)
776 goto err;
777
778 qcom_smd_channel_resume(channel);
779
780 return 0;
781
782err:
783 dev_err(&qsdev->dev, "probe failed\n");
784
785 channel->cb = NULL;
786 kfree(channel->bounce_buffer);
787 channel->bounce_buffer = NULL;
788
789 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
790 return ret;
791}
792
793/*
794 * Remove the smd client.
795 *
796 * The channel is going away, for some reason, so remove the smd client and
797 * reset the channel state.
798 */
799static int qcom_smd_dev_remove(struct device *dev)
800{
801 struct qcom_smd_device *qsdev = to_smd_device(dev);
802 struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
803 struct qcom_smd_channel *channel = qsdev->channel;
804 unsigned long flags;
805
806 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSING);
807
808 /*
809 * Make sure we don't race with the code receiving data.
810 */
811 spin_lock_irqsave(&channel->recv_lock, flags);
812 channel->cb = NULL;
813 spin_unlock_irqrestore(&channel->recv_lock, flags);
814
815 /* Wake up any sleepers in qcom_smd_send() */
816 wake_up_interruptible(&channel->fblockread_event);
817
818 /*
819 * We expect that the client might block in remove() waiting for any
820 * outstanding calls to qcom_smd_send() to wake up and finish.
821 */
822 if (qsdrv->remove)
823 qsdrv->remove(qsdev);
824
825 /*
826 * The client is now gone, cleanup and reset the channel state.
827 */
828 channel->qsdev = NULL;
829 kfree(channel->bounce_buffer);
830 channel->bounce_buffer = NULL;
831
832 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
833
834 qcom_smd_channel_reset(channel);
835
836 return 0;
837}
838
839static struct bus_type qcom_smd_bus = {
840 .name = "qcom_smd",
841 .match = qcom_smd_dev_match,
842 .probe = qcom_smd_dev_probe,
843 .remove = qcom_smd_dev_remove,
844};
845
846/*
847 * Release function for the qcom_smd_device object.
848 */
849static void qcom_smd_release_device(struct device *dev)
850{
851 struct qcom_smd_device *qsdev = to_smd_device(dev);
852
853 kfree(qsdev);
854}
855
856/*
857 * Finds the device_node for the smd child interested in this channel.
858 */
859static struct device_node *qcom_smd_match_channel(struct device_node *edge_node,
860 const char *channel)
861{
862 struct device_node *child;
863 const char *name;
864 const char *key;
865 int ret;
866
867 for_each_available_child_of_node(edge_node, child) {
868 key = "qcom,smd-channels";
869 ret = of_property_read_string(child, key, &name);
870 if (ret) {
871 of_node_put(child);
872 continue;
873 }
874
875 if (strcmp(name, channel) == 0)
876 return child;
877 }
878
879 return NULL;
880}
881
882/*
883 * Create a smd client device for channel that is being opened.
884 */
885static int qcom_smd_create_device(struct qcom_smd_channel *channel)
886{
887 struct qcom_smd_device *qsdev;
888 struct qcom_smd_edge *edge = channel->edge;
889 struct device_node *node;
890 struct qcom_smd *smd = edge->smd;
891 int ret;
892
893 if (channel->qsdev)
894 return -EEXIST;
895
f2ab3298
BA
896 dev_dbg(smd->dev, "registering '%s'\n", channel->name);
897
898 qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
899 if (!qsdev)
900 return -ENOMEM;
901
1a7caca2
BA
902 node = qcom_smd_match_channel(edge->of_node, channel->name);
903 dev_set_name(&qsdev->dev, "%s.%s",
904 edge->of_node->name,
905 node ? node->name : channel->name);
906
f2ab3298
BA
907 qsdev->dev.parent = smd->dev;
908 qsdev->dev.bus = &qcom_smd_bus;
909 qsdev->dev.release = qcom_smd_release_device;
910 qsdev->dev.of_node = node;
911
912 qsdev->channel = channel;
913
914 channel->qsdev = qsdev;
915
916 ret = device_register(&qsdev->dev);
917 if (ret) {
918 dev_err(smd->dev, "device_register failed: %d\n", ret);
919 put_device(&qsdev->dev);
920 }
921
922 return ret;
923}
924
925/*
926 * Destroy a smd client device for a channel that's going away.
927 */
928static void qcom_smd_destroy_device(struct qcom_smd_channel *channel)
929{
930 struct device *dev;
931
932 BUG_ON(!channel->qsdev);
933
934 dev = &channel->qsdev->dev;
935
936 device_unregister(dev);
937 of_node_put(dev->of_node);
938 put_device(dev);
939}
940
941/**
942 * qcom_smd_driver_register - register a smd driver
943 * @qsdrv: qcom_smd_driver struct
944 */
945int qcom_smd_driver_register(struct qcom_smd_driver *qsdrv)
946{
947 qsdrv->driver.bus = &qcom_smd_bus;
948 return driver_register(&qsdrv->driver);
949}
950EXPORT_SYMBOL(qcom_smd_driver_register);
951
952/**
953 * qcom_smd_driver_unregister - unregister a smd driver
954 * @qsdrv: qcom_smd_driver struct
955 */
956void qcom_smd_driver_unregister(struct qcom_smd_driver *qsdrv)
957{
958 driver_unregister(&qsdrv->driver);
959}
960EXPORT_SYMBOL(qcom_smd_driver_unregister);
961
962/*
963 * Allocate the qcom_smd_channel object for a newly found smd channel,
964 * retrieving and validating the smem items involved.
965 */
966static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge,
967 unsigned smem_info_item,
968 unsigned smem_fifo_item,
969 char *name)
970{
971 struct qcom_smd_channel *channel;
972 struct qcom_smd *smd = edge->smd;
973 size_t fifo_size;
974 size_t info_size;
975 void *fifo_base;
976 void *info;
977 int ret;
978
979 channel = devm_kzalloc(smd->dev, sizeof(*channel), GFP_KERNEL);
980 if (!channel)
981 return ERR_PTR(-ENOMEM);
982
983 channel->edge = edge;
984 channel->name = devm_kstrdup(smd->dev, name, GFP_KERNEL);
985 if (!channel->name)
986 return ERR_PTR(-ENOMEM);
987
988 mutex_init(&channel->tx_lock);
989 spin_lock_init(&channel->recv_lock);
990 init_waitqueue_head(&channel->fblockread_event);
991
1a03964d
SB
992 info = qcom_smem_get(edge->remote_pid, smem_info_item, &info_size);
993 if (IS_ERR(info)) {
994 ret = PTR_ERR(info);
f2ab3298 995 goto free_name_and_channel;
1a03964d 996 }
f2ab3298
BA
997
998 /*
999 * Use the size of the item to figure out which channel info struct to
1000 * use.
1001 */
1002 if (info_size == 2 * sizeof(struct smd_channel_info_word)) {
1003 channel->tx_info_word = info;
1004 channel->rx_info_word = info + sizeof(struct smd_channel_info_word);
1005 } else if (info_size == 2 * sizeof(struct smd_channel_info)) {
1006 channel->tx_info = info;
1007 channel->rx_info = info + sizeof(struct smd_channel_info);
1008 } else {
1009 dev_err(smd->dev,
1010 "channel info of size %zu not supported\n", info_size);
1011 ret = -EINVAL;
1012 goto free_name_and_channel;
1013 }
1014
1a03964d
SB
1015 fifo_base = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_size);
1016 if (IS_ERR(fifo_base)) {
1017 ret = PTR_ERR(fifo_base);
f2ab3298 1018 goto free_name_and_channel;
1a03964d 1019 }
f2ab3298
BA
1020
1021 /* The channel consist of a rx and tx fifo of equal size */
1022 fifo_size /= 2;
1023
1024 dev_dbg(smd->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1025 name, info_size, fifo_size);
1026
1027 channel->tx_fifo = fifo_base;
1028 channel->rx_fifo = fifo_base + fifo_size;
1029 channel->fifo_size = fifo_size;
1030
1031 qcom_smd_channel_reset(channel);
1032
1033 return channel;
1034
1035free_name_and_channel:
1036 devm_kfree(smd->dev, channel->name);
1037 devm_kfree(smd->dev, channel);
1038
1039 return ERR_PTR(ret);
1040}
1041
1042/*
1043 * Scans the allocation table for any newly allocated channels, calls
1044 * qcom_smd_create_channel() to create representations of these and add
1045 * them to the edge's list of channels.
1046 */
1047static void qcom_discover_channels(struct qcom_smd_edge *edge)
1048{
1049 struct qcom_smd_alloc_entry *alloc_tbl;
1050 struct qcom_smd_alloc_entry *entry;
1051 struct qcom_smd_channel *channel;
1052 struct qcom_smd *smd = edge->smd;
1053 unsigned long flags;
1054 unsigned fifo_id;
1055 unsigned info_id;
f2ab3298
BA
1056 int tbl;
1057 int i;
1058
1059 for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) {
1a03964d
SB
1060 alloc_tbl = qcom_smem_get(edge->remote_pid,
1061 smem_items[tbl].alloc_tbl_id, NULL);
1062 if (IS_ERR(alloc_tbl))
f2ab3298
BA
1063 continue;
1064
1065 for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) {
1066 entry = &alloc_tbl[i];
1067 if (test_bit(i, edge->allocated[tbl]))
1068 continue;
1069
1070 if (entry->ref_count == 0)
1071 continue;
1072
1073 if (!entry->name[0])
1074 continue;
1075
1076 if (!(entry->flags & SMD_CHANNEL_FLAGS_PACKET))
1077 continue;
1078
1079 if ((entry->flags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id)
1080 continue;
1081
1082 info_id = smem_items[tbl].info_base_id + entry->cid;
1083 fifo_id = smem_items[tbl].fifo_base_id + entry->cid;
1084
1085 channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
1086 if (IS_ERR(channel))
1087 continue;
1088
1089 spin_lock_irqsave(&edge->channels_lock, flags);
1090 list_add(&channel->list, &edge->channels);
1091 spin_unlock_irqrestore(&edge->channels_lock, flags);
1092
1093 dev_dbg(smd->dev, "new channel found: '%s'\n", channel->name);
1094 set_bit(i, edge->allocated[tbl]);
1095 }
1096 }
1097
1098 schedule_work(&edge->work);
1099}
1100
1101/*
1102 * This per edge worker scans smem for any new channels and register these. It
1103 * then scans all registered channels for state changes that should be handled
1104 * by creating or destroying smd client devices for the registered channels.
1105 *
1106 * LOCKING: edge->channels_lock is not needed to be held during the traversal
1107 * of the channels list as it's done synchronously with the only writer.
1108 */
1109static void qcom_channel_state_worker(struct work_struct *work)
1110{
1111 struct qcom_smd_channel *channel;
1112 struct qcom_smd_edge *edge = container_of(work,
1113 struct qcom_smd_edge,
1114 work);
1115 unsigned remote_state;
1116
1117 /*
1118 * Rescan smem if we have reason to belive that there are new channels.
1119 */
1120 if (edge->need_rescan) {
1121 edge->need_rescan = false;
1122 qcom_discover_channels(edge);
1123 }
1124
1125 /*
1126 * Register a device for any closed channel where the remote processor
1127 * is showing interest in opening the channel.
1128 */
1129 list_for_each_entry(channel, &edge->channels, list) {
1130 if (channel->state != SMD_CHANNEL_CLOSED)
1131 continue;
1132
1133 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1134 if (remote_state != SMD_CHANNEL_OPENING &&
1135 remote_state != SMD_CHANNEL_OPENED)
1136 continue;
1137
1138 qcom_smd_create_device(channel);
1139 }
1140
1141 /*
1142 * Unregister the device for any channel that is opened where the
1143 * remote processor is closing the channel.
1144 */
1145 list_for_each_entry(channel, &edge->channels, list) {
1146 if (channel->state != SMD_CHANNEL_OPENING &&
1147 channel->state != SMD_CHANNEL_OPENED)
1148 continue;
1149
1150 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1151 if (remote_state == SMD_CHANNEL_OPENING ||
1152 remote_state == SMD_CHANNEL_OPENED)
1153 continue;
1154
1155 qcom_smd_destroy_device(channel);
1156 }
1157}
1158
1159/*
1160 * Parses an of_node describing an edge.
1161 */
1162static int qcom_smd_parse_edge(struct device *dev,
1163 struct device_node *node,
1164 struct qcom_smd_edge *edge)
1165{
1166 struct device_node *syscon_np;
1167 const char *key;
1168 int irq;
1169 int ret;
1170
1171 INIT_LIST_HEAD(&edge->channels);
1172 spin_lock_init(&edge->channels_lock);
1173
1174 INIT_WORK(&edge->work, qcom_channel_state_worker);
1175
1176 edge->of_node = of_node_get(node);
1177
1178 irq = irq_of_parse_and_map(node, 0);
1179 if (irq < 0) {
1180 dev_err(dev, "required smd interrupt missing\n");
1181 return -EINVAL;
1182 }
1183
1184 ret = devm_request_irq(dev, irq,
1185 qcom_smd_edge_intr, IRQF_TRIGGER_RISING,
1186 node->name, edge);
1187 if (ret) {
1188 dev_err(dev, "failed to request smd irq\n");
1189 return ret;
1190 }
1191
1192 edge->irq = irq;
1193
1194 key = "qcom,smd-edge";
1195 ret = of_property_read_u32(node, key, &edge->edge_id);
1196 if (ret) {
1197 dev_err(dev, "edge missing %s property\n", key);
1198 return -EINVAL;
1199 }
1200
93dbed91
AG
1201 edge->remote_pid = QCOM_SMEM_HOST_ANY;
1202 key = "qcom,remote-pid";
1203 of_property_read_u32(node, key, &edge->remote_pid);
1204
f2ab3298
BA
1205 syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
1206 if (!syscon_np) {
1207 dev_err(dev, "no qcom,ipc node\n");
1208 return -ENODEV;
1209 }
1210
1211 edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
1212 if (IS_ERR(edge->ipc_regmap))
1213 return PTR_ERR(edge->ipc_regmap);
1214
1215 key = "qcom,ipc";
1216 ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
1217 if (ret < 0) {
1218 dev_err(dev, "no offset in %s\n", key);
1219 return -EINVAL;
1220 }
1221
1222 ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
1223 if (ret < 0) {
1224 dev_err(dev, "no bit in %s\n", key);
1225 return -EINVAL;
1226 }
1227
1228 return 0;
1229}
1230
1231static int qcom_smd_probe(struct platform_device *pdev)
1232{
1233 struct qcom_smd_edge *edge;
1234 struct device_node *node;
1235 struct qcom_smd *smd;
1236 size_t array_size;
1237 int num_edges;
1238 int ret;
1239 int i = 0;
1a03964d 1240 void *p;
f2ab3298
BA
1241
1242 /* Wait for smem */
1a03964d
SB
1243 p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL);
1244 if (PTR_ERR(p) == -EPROBE_DEFER)
1245 return PTR_ERR(p);
f2ab3298
BA
1246
1247 num_edges = of_get_available_child_count(pdev->dev.of_node);
1248 array_size = sizeof(*smd) + num_edges * sizeof(struct qcom_smd_edge);
1249 smd = devm_kzalloc(&pdev->dev, array_size, GFP_KERNEL);
1250 if (!smd)
1251 return -ENOMEM;
1252 smd->dev = &pdev->dev;
1253
1254 smd->num_edges = num_edges;
1255 for_each_available_child_of_node(pdev->dev.of_node, node) {
1256 edge = &smd->edges[i++];
1257 edge->smd = smd;
1258
1259 ret = qcom_smd_parse_edge(&pdev->dev, node, edge);
1260 if (ret)
1261 continue;
1262
1263 edge->need_rescan = true;
1264 schedule_work(&edge->work);
1265 }
1266
1267 platform_set_drvdata(pdev, smd);
1268
1269 return 0;
1270}
1271
1272/*
1273 * Shut down all smd clients by making sure that each edge stops processing
1274 * events and scanning for new channels, then call destroy on the devices.
1275 */
1276static int qcom_smd_remove(struct platform_device *pdev)
1277{
1278 struct qcom_smd_channel *channel;
1279 struct qcom_smd_edge *edge;
1280 struct qcom_smd *smd = platform_get_drvdata(pdev);
1281 int i;
1282
1283 for (i = 0; i < smd->num_edges; i++) {
1284 edge = &smd->edges[i];
1285
1286 disable_irq(edge->irq);
1287 cancel_work_sync(&edge->work);
1288
1289 list_for_each_entry(channel, &edge->channels, list) {
1290 if (!channel->qsdev)
1291 continue;
1292
1293 qcom_smd_destroy_device(channel);
1294 }
1295 }
1296
1297 return 0;
1298}
1299
1300static const struct of_device_id qcom_smd_of_match[] = {
1301 { .compatible = "qcom,smd" },
1302 {}
1303};
1304MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
1305
1306static struct platform_driver qcom_smd_driver = {
1307 .probe = qcom_smd_probe,
1308 .remove = qcom_smd_remove,
1309 .driver = {
1310 .name = "qcom-smd",
1311 .of_match_table = qcom_smd_of_match,
1312 },
1313};
1314
1315static int __init qcom_smd_init(void)
1316{
1317 int ret;
1318
1319 ret = bus_register(&qcom_smd_bus);
1320 if (ret) {
1321 pr_err("failed to register smd bus: %d\n", ret);
1322 return ret;
1323 }
1324
1325 return platform_driver_register(&qcom_smd_driver);
1326}
1327postcore_initcall(qcom_smd_init);
1328
1329static void __exit qcom_smd_exit(void)
1330{
1331 platform_driver_unregister(&qcom_smd_driver);
1332 bus_unregister(&qcom_smd_bus);
1333}
1334module_exit(qcom_smd_exit);
1335
1336MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
1337MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
1338MODULE_LICENSE("GPL v2");
This page took 0.077548 seconds and 5 git commands to generate.