blob: b6434c4be86a6d8066be9a5fa490adc65a5db619 [file] [log] [blame]
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -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/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;
Stephen Boydf02dc822015-09-02 15:46:46 -070068struct smd_channel_info_pair;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -070069struct smd_channel_info_word;
Stephen Boydf02dc822015-09-02 15:46:46 -070070struct smd_channel_info_word_pair;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -070071
72#define SMD_ALLOC_TBL_COUNT 2
73#define SMD_ALLOC_TBL_SIZE 64
74
75/*
76 * This lists the various smem heap items relevant for the allocation table and
77 * smd channel entries.
78 */
79static const struct {
80 unsigned alloc_tbl_id;
81 unsigned info_base_id;
82 unsigned fifo_base_id;
83} smem_items[SMD_ALLOC_TBL_COUNT] = {
84 {
85 .alloc_tbl_id = 13,
86 .info_base_id = 14,
87 .fifo_base_id = 338
88 },
89 {
Bjorn Anderssonea4683e2015-10-09 22:06:44 -070090 .alloc_tbl_id = 266,
91 .info_base_id = 138,
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -070092 .fifo_base_id = 202,
93 },
94};
95
96/**
97 * struct qcom_smd_edge - representing a remote processor
98 * @smd: handle to qcom_smd
99 * @of_node: of_node handle for information related to this edge
100 * @edge_id: identifier of this edge
Andy Gross93dbed92015-08-26 14:42:45 -0500101 * @remote_pid: identifier of remote processor
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700102 * @irq: interrupt for signals on this edge
103 * @ipc_regmap: regmap handle holding the outgoing ipc register
104 * @ipc_offset: offset within @ipc_regmap of the register for ipc
105 * @ipc_bit: bit in the register at @ipc_offset of @ipc_regmap
106 * @channels: list of all channels detected on this edge
107 * @channels_lock: guard for modifications of @channels
108 * @allocated: array of bitmaps representing already allocated channels
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700109 * @smem_available: last available amount of smem triggering a channel scan
Bjorn Andersson995b1702016-02-17 22:39:03 -0800110 * @scan_work: work item for discovering new channels
111 * @state_work: work item for edge state changes
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700112 */
113struct qcom_smd_edge {
114 struct qcom_smd *smd;
115 struct device_node *of_node;
116 unsigned edge_id;
Andy Gross93dbed92015-08-26 14:42:45 -0500117 unsigned remote_pid;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700118
119 int irq;
120
121 struct regmap *ipc_regmap;
122 int ipc_offset;
123 int ipc_bit;
124
125 struct list_head channels;
126 spinlock_t channels_lock;
127
128 DECLARE_BITMAP(allocated[SMD_ALLOC_TBL_COUNT], SMD_ALLOC_TBL_SIZE);
129
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700130 unsigned smem_available;
131
Bjorn Andersson028021d2016-02-17 22:39:06 -0800132 wait_queue_head_t new_channel_event;
133
Bjorn Andersson995b1702016-02-17 22:39:03 -0800134 struct work_struct scan_work;
135 struct work_struct state_work;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700136};
137
138/*
139 * SMD channel states.
140 */
141enum smd_channel_state {
142 SMD_CHANNEL_CLOSED,
143 SMD_CHANNEL_OPENING,
144 SMD_CHANNEL_OPENED,
145 SMD_CHANNEL_FLUSHING,
146 SMD_CHANNEL_CLOSING,
147 SMD_CHANNEL_RESET,
148 SMD_CHANNEL_RESET_OPENING
149};
150
151/**
152 * struct qcom_smd_channel - smd channel struct
153 * @edge: qcom_smd_edge this channel is living on
154 * @qsdev: reference to a associated smd client device
155 * @name: name of the channel
156 * @state: local state of the channel
157 * @remote_state: remote state of the channel
Stephen Boydf02dc822015-09-02 15:46:46 -0700158 * @info: byte aligned outgoing/incoming channel info
159 * @info_word: word aligned outgoing/incoming channel info
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700160 * @tx_lock: lock to make writes to the channel mutually exclusive
161 * @fblockread_event: wakeup event tied to tx fBLOCKREADINTR
162 * @tx_fifo: pointer to the outgoing ring buffer
163 * @rx_fifo: pointer to the incoming ring buffer
164 * @fifo_size: size of each ring buffer
165 * @bounce_buffer: bounce buffer for reading wrapped packets
166 * @cb: callback function registered for this channel
167 * @recv_lock: guard for rx info modifications and cb pointer
168 * @pkt_size: size of the currently handled packet
169 * @list: lite entry for @channels in qcom_smd_edge
170 */
171struct qcom_smd_channel {
172 struct qcom_smd_edge *edge;
173
174 struct qcom_smd_device *qsdev;
175
176 char *name;
177 enum smd_channel_state state;
178 enum smd_channel_state remote_state;
179
Stephen Boydf02dc822015-09-02 15:46:46 -0700180 struct smd_channel_info_pair *info;
181 struct smd_channel_info_word_pair *info_word;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700182
183 struct mutex tx_lock;
184 wait_queue_head_t fblockread_event;
185
186 void *tx_fifo;
187 void *rx_fifo;
188 int fifo_size;
189
190 void *bounce_buffer;
Bjorn Andersson39f0db22016-02-17 22:39:02 -0800191 qcom_smd_cb_t cb;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700192
193 spinlock_t recv_lock;
194
195 int pkt_size;
196
197 struct list_head list;
Bjorn Anderssond5933852016-02-17 22:39:05 -0800198 struct list_head dev_list;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700199};
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 {
Stephen Boyd24f60e32015-09-17 14:50:53 -0700218 __le32 state;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700219 u8 fDSR;
220 u8 fCTS;
221 u8 fCD;
222 u8 fRI;
223 u8 fHEAD;
224 u8 fTAIL;
225 u8 fSTATE;
226 u8 fBLOCKREADINTR;
Stephen Boyd24f60e32015-09-17 14:50:53 -0700227 __le32 tail;
228 __le32 head;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700229};
230
Stephen Boydf02dc822015-09-02 15:46:46 -0700231struct smd_channel_info_pair {
232 struct smd_channel_info tx;
233 struct smd_channel_info rx;
234};
235
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700236/*
237 * Format of the smd_info smem items, for word aligned channels.
238 */
239struct smd_channel_info_word {
Stephen Boyd24f60e32015-09-17 14:50:53 -0700240 __le32 state;
241 __le32 fDSR;
242 __le32 fCTS;
243 __le32 fCD;
244 __le32 fRI;
245 __le32 fHEAD;
246 __le32 fTAIL;
247 __le32 fSTATE;
248 __le32 fBLOCKREADINTR;
249 __le32 tail;
250 __le32 head;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700251};
252
Stephen Boydf02dc822015-09-02 15:46:46 -0700253struct smd_channel_info_word_pair {
254 struct smd_channel_info_word tx;
255 struct smd_channel_info_word rx;
256};
257
Stephen Boyd24f60e32015-09-17 14:50:53 -0700258#define GET_RX_CHANNEL_FLAG(channel, param) \
259 ({ \
260 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
261 channel->info_word ? \
262 le32_to_cpu(channel->info_word->rx.param) : \
263 channel->info->rx.param; \
264 })
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700265
Stephen Boyd24f60e32015-09-17 14:50:53 -0700266#define GET_RX_CHANNEL_INFO(channel, param) \
267 ({ \
268 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
269 le32_to_cpu(channel->info_word ? \
270 channel->info_word->rx.param : \
271 channel->info->rx.param); \
272 })
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700273
Stephen Boyd24f60e32015-09-17 14:50:53 -0700274#define SET_RX_CHANNEL_FLAG(channel, param, value) \
275 ({ \
276 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
277 if (channel->info_word) \
278 channel->info_word->rx.param = cpu_to_le32(value); \
279 else \
280 channel->info->rx.param = value; \
281 })
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700282
Stephen Boyd24f60e32015-09-17 14:50:53 -0700283#define SET_RX_CHANNEL_INFO(channel, param, value) \
284 ({ \
285 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
286 if (channel->info_word) \
287 channel->info_word->rx.param = cpu_to_le32(value); \
288 else \
289 channel->info->rx.param = cpu_to_le32(value); \
290 })
291
292#define GET_TX_CHANNEL_FLAG(channel, param) \
293 ({ \
294 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
295 channel->info_word ? \
296 le32_to_cpu(channel->info_word->tx.param) : \
297 channel->info->tx.param; \
298 })
299
300#define GET_TX_CHANNEL_INFO(channel, param) \
301 ({ \
302 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
303 le32_to_cpu(channel->info_word ? \
304 channel->info_word->tx.param : \
305 channel->info->tx.param); \
306 })
307
308#define SET_TX_CHANNEL_FLAG(channel, param, value) \
309 ({ \
310 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
311 if (channel->info_word) \
312 channel->info_word->tx.param = cpu_to_le32(value); \
313 else \
314 channel->info->tx.param = value; \
315 })
316
317#define SET_TX_CHANNEL_INFO(channel, param, value) \
318 ({ \
319 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
320 if (channel->info_word) \
321 channel->info_word->tx.param = cpu_to_le32(value); \
322 else \
323 channel->info->tx.param = cpu_to_le32(value); \
324 })
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700325
326/**
327 * struct qcom_smd_alloc_entry - channel allocation entry
328 * @name: channel name
329 * @cid: channel index
330 * @flags: channel flags and edge id
331 * @ref_count: reference count of the channel
332 */
333struct qcom_smd_alloc_entry {
334 u8 name[20];
Stephen Boyd24f60e32015-09-17 14:50:53 -0700335 __le32 cid;
336 __le32 flags;
337 __le32 ref_count;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700338} __packed;
339
340#define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff
341#define SMD_CHANNEL_FLAGS_STREAM BIT(8)
342#define SMD_CHANNEL_FLAGS_PACKET BIT(9)
343
344/*
345 * Each smd packet contains a 20 byte header, with the first 4 being the length
346 * of the packet.
347 */
348#define SMD_PACKET_HEADER_LEN 20
349
350/*
351 * Signal the remote processor associated with 'channel'.
352 */
353static void qcom_smd_signal_channel(struct qcom_smd_channel *channel)
354{
355 struct qcom_smd_edge *edge = channel->edge;
356
357 regmap_write(edge->ipc_regmap, edge->ipc_offset, BIT(edge->ipc_bit));
358}
359
360/*
361 * Initialize the tx channel info
362 */
363static void qcom_smd_channel_reset(struct qcom_smd_channel *channel)
364{
365 SET_TX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
Stephen Boyd24f60e32015-09-17 14:50:53 -0700366 SET_TX_CHANNEL_FLAG(channel, fDSR, 0);
367 SET_TX_CHANNEL_FLAG(channel, fCTS, 0);
368 SET_TX_CHANNEL_FLAG(channel, fCD, 0);
369 SET_TX_CHANNEL_FLAG(channel, fRI, 0);
370 SET_TX_CHANNEL_FLAG(channel, fHEAD, 0);
371 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
372 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
373 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700374 SET_TX_CHANNEL_INFO(channel, head, 0);
375 SET_TX_CHANNEL_INFO(channel, tail, 0);
376
377 qcom_smd_signal_channel(channel);
378
379 channel->state = SMD_CHANNEL_CLOSED;
380 channel->pkt_size = 0;
381}
382
383/*
Bjorn Andersson39f0db22016-02-17 22:39:02 -0800384 * Set the callback for a channel, with appropriate locking
385 */
386static void qcom_smd_channel_set_callback(struct qcom_smd_channel *channel,
387 qcom_smd_cb_t cb)
388{
389 unsigned long flags;
390
391 spin_lock_irqsave(&channel->recv_lock, flags);
392 channel->cb = cb;
393 spin_unlock_irqrestore(&channel->recv_lock, flags);
394};
395
396/*
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700397 * Calculate the amount of data available in the rx fifo
398 */
399static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel *channel)
400{
401 unsigned head;
402 unsigned tail;
403
404 head = GET_RX_CHANNEL_INFO(channel, head);
405 tail = GET_RX_CHANNEL_INFO(channel, tail);
406
407 return (head - tail) & (channel->fifo_size - 1);
408}
409
410/*
411 * Set tx channel state and inform the remote processor
412 */
413static void qcom_smd_channel_set_state(struct qcom_smd_channel *channel,
414 int state)
415{
416 struct qcom_smd_edge *edge = channel->edge;
417 bool is_open = state == SMD_CHANNEL_OPENED;
418
419 if (channel->state == state)
420 return;
421
422 dev_dbg(edge->smd->dev, "set_state(%s, %d)\n", channel->name, state);
423
Stephen Boyd24f60e32015-09-17 14:50:53 -0700424 SET_TX_CHANNEL_FLAG(channel, fDSR, is_open);
425 SET_TX_CHANNEL_FLAG(channel, fCTS, is_open);
426 SET_TX_CHANNEL_FLAG(channel, fCD, is_open);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700427
428 SET_TX_CHANNEL_INFO(channel, state, state);
Stephen Boyd24f60e32015-09-17 14:50:53 -0700429 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700430
431 channel->state = state;
432 qcom_smd_signal_channel(channel);
433}
434
435/*
436 * Copy count bytes of data using 32bit accesses, if that's required.
437 */
Stephen Boyd3b781e52015-09-02 15:46:47 -0700438static void smd_copy_to_fifo(void __iomem *dst,
439 const void *src,
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700440 size_t count,
441 bool word_aligned)
442{
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700443 if (word_aligned) {
Stephen Boyd3b781e52015-09-02 15:46:47 -0700444 __iowrite32_copy(dst, src, count / sizeof(u32));
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700445 } else {
Stephen Boyd3b781e52015-09-02 15:46:47 -0700446 memcpy_toio(dst, src, count);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700447 }
448}
449
450/*
451 * Copy count bytes of data using 32bit accesses, if that is required.
452 */
Stephen Boydc431e672016-01-20 14:58:38 -0800453static void smd_copy_from_fifo(void *dst,
454 const void __iomem *src,
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700455 size_t count,
456 bool word_aligned)
457{
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700458 if (word_aligned) {
Stephen Boydc431e672016-01-20 14:58:38 -0800459 __ioread32_copy(dst, src, count / sizeof(u32));
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700460 } else {
Stephen Boydc431e672016-01-20 14:58:38 -0800461 memcpy_fromio(dst, src, count);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700462 }
463}
464
465/*
466 * Read count bytes of data from the rx fifo into buf, but don't advance the
467 * tail.
468 */
469static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel,
470 void *buf, size_t count)
471{
472 bool word_aligned;
473 unsigned tail;
474 size_t len;
475
Stephen Boydf02dc822015-09-02 15:46:46 -0700476 word_aligned = channel->info_word;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700477 tail = GET_RX_CHANNEL_INFO(channel, tail);
478
479 len = min_t(size_t, count, channel->fifo_size - tail);
480 if (len) {
481 smd_copy_from_fifo(buf,
482 channel->rx_fifo + tail,
483 len,
484 word_aligned);
485 }
486
487 if (len != count) {
488 smd_copy_from_fifo(buf + len,
489 channel->rx_fifo,
490 count - len,
491 word_aligned);
492 }
493
494 return count;
495}
496
497/*
498 * Advance the rx tail by count bytes.
499 */
500static void qcom_smd_channel_advance(struct qcom_smd_channel *channel,
501 size_t count)
502{
503 unsigned tail;
504
505 tail = GET_RX_CHANNEL_INFO(channel, tail);
506 tail += count;
507 tail &= (channel->fifo_size - 1);
508 SET_RX_CHANNEL_INFO(channel, tail, tail);
509}
510
511/*
512 * Read out a single packet from the rx fifo and deliver it to the device
513 */
514static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
515{
516 struct qcom_smd_device *qsdev = channel->qsdev;
517 unsigned tail;
518 size_t len;
519 void *ptr;
520 int ret;
521
522 if (!channel->cb)
523 return 0;
524
525 tail = GET_RX_CHANNEL_INFO(channel, tail);
526
527 /* Use bounce buffer if the data wraps */
528 if (tail + channel->pkt_size >= channel->fifo_size) {
529 ptr = channel->bounce_buffer;
530 len = qcom_smd_channel_peek(channel, ptr, channel->pkt_size);
531 } else {
532 ptr = channel->rx_fifo + tail;
533 len = channel->pkt_size;
534 }
535
536 ret = channel->cb(qsdev, ptr, len);
537 if (ret < 0)
538 return ret;
539
540 /* Only forward the tail if the client consumed the data */
541 qcom_smd_channel_advance(channel, len);
542
543 channel->pkt_size = 0;
544
545 return 0;
546}
547
548/*
549 * Per channel interrupt handling
550 */
551static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel)
552{
553 bool need_state_scan = false;
554 int remote_state;
Stephen Boyd24f60e32015-09-17 14:50:53 -0700555 __le32 pktlen;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700556 int avail;
557 int ret;
558
559 /* Handle state changes */
560 remote_state = GET_RX_CHANNEL_INFO(channel, state);
561 if (remote_state != channel->remote_state) {
562 channel->remote_state = remote_state;
563 need_state_scan = true;
564 }
565 /* Indicate that we have seen any state change */
Stephen Boyd24f60e32015-09-17 14:50:53 -0700566 SET_RX_CHANNEL_FLAG(channel, fSTATE, 0);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700567
568 /* Signal waiting qcom_smd_send() about the interrupt */
Stephen Boyd24f60e32015-09-17 14:50:53 -0700569 if (!GET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR))
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700570 wake_up_interruptible(&channel->fblockread_event);
571
572 /* Don't consume any data until we've opened the channel */
573 if (channel->state != SMD_CHANNEL_OPENED)
574 goto out;
575
576 /* Indicate that we've seen the new data */
Stephen Boyd24f60e32015-09-17 14:50:53 -0700577 SET_RX_CHANNEL_FLAG(channel, fHEAD, 0);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700578
579 /* Consume data */
580 for (;;) {
581 avail = qcom_smd_channel_get_rx_avail(channel);
582
583 if (!channel->pkt_size && avail >= SMD_PACKET_HEADER_LEN) {
584 qcom_smd_channel_peek(channel, &pktlen, sizeof(pktlen));
585 qcom_smd_channel_advance(channel, SMD_PACKET_HEADER_LEN);
Stephen Boyd24f60e32015-09-17 14:50:53 -0700586 channel->pkt_size = le32_to_cpu(pktlen);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700587 } else if (channel->pkt_size && avail >= channel->pkt_size) {
588 ret = qcom_smd_channel_recv_single(channel);
589 if (ret)
590 break;
591 } else {
592 break;
593 }
594 }
595
596 /* Indicate that we have seen and updated tail */
Stephen Boyd24f60e32015-09-17 14:50:53 -0700597 SET_RX_CHANNEL_FLAG(channel, fTAIL, 1);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700598
599 /* Signal the remote that we've consumed the data (if requested) */
Stephen Boyd24f60e32015-09-17 14:50:53 -0700600 if (!GET_RX_CHANNEL_FLAG(channel, fBLOCKREADINTR)) {
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700601 /* Ensure ordering of channel info updates */
602 wmb();
603
604 qcom_smd_signal_channel(channel);
605 }
606
607out:
608 return need_state_scan;
609}
610
611/*
612 * The edge interrupts are triggered by the remote processor on state changes,
613 * channel info updates or when new channels are created.
614 */
615static irqreturn_t qcom_smd_edge_intr(int irq, void *data)
616{
617 struct qcom_smd_edge *edge = data;
618 struct qcom_smd_channel *channel;
619 unsigned available;
Bjorn Andersson995b1702016-02-17 22:39:03 -0800620 bool kick_scanner = false;
621 bool kick_state = false;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700622
623 /*
624 * Handle state changes or data on each of the channels on this edge
625 */
626 spin_lock(&edge->channels_lock);
627 list_for_each_entry(channel, &edge->channels, list) {
628 spin_lock(&channel->recv_lock);
Bjorn Andersson995b1702016-02-17 22:39:03 -0800629 kick_state |= qcom_smd_channel_intr(channel);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700630 spin_unlock(&channel->recv_lock);
631 }
632 spin_unlock(&edge->channels_lock);
633
634 /*
635 * Creating a new channel requires allocating an smem entry, so we only
636 * have to scan if the amount of available space in smem have changed
637 * since last scan.
638 */
Andy Gross93dbed92015-08-26 14:42:45 -0500639 available = qcom_smem_get_free_space(edge->remote_pid);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700640 if (available != edge->smem_available) {
641 edge->smem_available = available;
Bjorn Andersson995b1702016-02-17 22:39:03 -0800642 kick_scanner = true;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700643 }
644
Bjorn Andersson995b1702016-02-17 22:39:03 -0800645 if (kick_scanner)
646 schedule_work(&edge->scan_work);
647 if (kick_state)
648 schedule_work(&edge->state_work);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700649
650 return IRQ_HANDLED;
651}
652
653/*
654 * Delivers any outstanding packets in the rx fifo, can be used after probe of
655 * the clients to deliver any packets that wasn't delivered before the client
656 * was setup.
657 */
658static void qcom_smd_channel_resume(struct qcom_smd_channel *channel)
659{
660 unsigned long flags;
661
662 spin_lock_irqsave(&channel->recv_lock, flags);
663 qcom_smd_channel_intr(channel);
664 spin_unlock_irqrestore(&channel->recv_lock, flags);
665}
666
667/*
668 * Calculate how much space is available in the tx fifo.
669 */
670static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel)
671{
672 unsigned head;
673 unsigned tail;
674 unsigned mask = channel->fifo_size - 1;
675
676 head = GET_TX_CHANNEL_INFO(channel, head);
677 tail = GET_TX_CHANNEL_INFO(channel, tail);
678
679 return mask - ((head - tail) & mask);
680}
681
682/*
683 * Write count bytes of data into channel, possibly wrapping in the ring buffer
684 */
685static int qcom_smd_write_fifo(struct qcom_smd_channel *channel,
686 const void *data,
687 size_t count)
688{
689 bool word_aligned;
690 unsigned head;
691 size_t len;
692
Stephen Boydf02dc822015-09-02 15:46:46 -0700693 word_aligned = channel->info_word;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700694 head = GET_TX_CHANNEL_INFO(channel, head);
695
696 len = min_t(size_t, count, channel->fifo_size - head);
697 if (len) {
698 smd_copy_to_fifo(channel->tx_fifo + head,
699 data,
700 len,
701 word_aligned);
702 }
703
704 if (len != count) {
705 smd_copy_to_fifo(channel->tx_fifo,
706 data + len,
707 count - len,
708 word_aligned);
709 }
710
711 head += count;
712 head &= (channel->fifo_size - 1);
713 SET_TX_CHANNEL_INFO(channel, head, head);
714
715 return count;
716}
717
718/**
719 * qcom_smd_send - write data to smd channel
720 * @channel: channel handle
721 * @data: buffer of data to write
722 * @len: number of bytes to write
723 *
724 * This is a blocking write of len bytes into the channel's tx ring buffer and
725 * signal the remote end. It will sleep until there is enough space available
726 * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
727 * polling.
728 */
729int qcom_smd_send(struct qcom_smd_channel *channel, const void *data, int len)
730{
Stephen Boyd24f60e32015-09-17 14:50:53 -0700731 __le32 hdr[5] = { cpu_to_le32(len), };
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700732 int tlen = sizeof(hdr) + len;
733 int ret;
734
735 /* Word aligned channels only accept word size aligned data */
Stephen Boydf02dc822015-09-02 15:46:46 -0700736 if (channel->info_word && len % 4)
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700737 return -EINVAL;
738
Bjorn Anderssona208ca92015-09-24 18:37:18 -0700739 /* Reject packets that are too big */
740 if (tlen >= channel->fifo_size)
741 return -EINVAL;
742
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700743 ret = mutex_lock_interruptible(&channel->tx_lock);
744 if (ret)
745 return ret;
746
747 while (qcom_smd_get_tx_avail(channel) < tlen) {
748 if (channel->state != SMD_CHANNEL_OPENED) {
749 ret = -EPIPE;
750 goto out;
751 }
752
Stephen Boyd24f60e32015-09-17 14:50:53 -0700753 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 0);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700754
755 ret = wait_event_interruptible(channel->fblockread_event,
756 qcom_smd_get_tx_avail(channel) >= tlen ||
757 channel->state != SMD_CHANNEL_OPENED);
758 if (ret)
759 goto out;
760
Stephen Boyd24f60e32015-09-17 14:50:53 -0700761 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700762 }
763
Stephen Boyd24f60e32015-09-17 14:50:53 -0700764 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700765
766 qcom_smd_write_fifo(channel, hdr, sizeof(hdr));
767 qcom_smd_write_fifo(channel, data, len);
768
Stephen Boyd24f60e32015-09-17 14:50:53 -0700769 SET_TX_CHANNEL_FLAG(channel, fHEAD, 1);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700770
771 /* Ensure ordering of channel info updates */
772 wmb();
773
774 qcom_smd_signal_channel(channel);
775
776out:
777 mutex_unlock(&channel->tx_lock);
778
779 return ret;
780}
781EXPORT_SYMBOL(qcom_smd_send);
782
783static struct qcom_smd_device *to_smd_device(struct device *dev)
784{
785 return container_of(dev, struct qcom_smd_device, dev);
786}
787
788static struct qcom_smd_driver *to_smd_driver(struct device *dev)
789{
790 struct qcom_smd_device *qsdev = to_smd_device(dev);
791
792 return container_of(qsdev->dev.driver, struct qcom_smd_driver, driver);
793}
794
795static int qcom_smd_dev_match(struct device *dev, struct device_driver *drv)
796{
Bjorn Andersson1a7caca2015-08-28 10:39:20 -0700797 struct qcom_smd_device *qsdev = to_smd_device(dev);
798 struct qcom_smd_driver *qsdrv = container_of(drv, struct qcom_smd_driver, driver);
799 const struct qcom_smd_id *match = qsdrv->smd_match_table;
800 const char *name = qsdev->channel->name;
801
802 if (match) {
803 while (match->name[0]) {
804 if (!strcmp(match->name, name))
805 return 1;
806 match++;
807 }
808 }
809
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700810 return of_driver_match_device(dev, drv);
811}
812
813/*
Bjorn Andersson3fd3f2f2016-02-17 22:39:04 -0800814 * Helper for opening a channel
815 */
816static int qcom_smd_channel_open(struct qcom_smd_channel *channel,
817 qcom_smd_cb_t cb)
818{
819 size_t bb_size;
820
821 /*
822 * Packets are maximum 4k, but reduce if the fifo is smaller
823 */
824 bb_size = min(channel->fifo_size, SZ_4K);
825 channel->bounce_buffer = kmalloc(bb_size, GFP_KERNEL);
826 if (!channel->bounce_buffer)
827 return -ENOMEM;
828
829 qcom_smd_channel_set_callback(channel, cb);
830 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENING);
831 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED);
832
833 return 0;
834}
835
836/*
837 * Helper for closing and resetting a channel
838 */
839static void qcom_smd_channel_close(struct qcom_smd_channel *channel)
840{
841 qcom_smd_channel_set_callback(channel, NULL);
842
843 kfree(channel->bounce_buffer);
844 channel->bounce_buffer = NULL;
845
846 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
847 qcom_smd_channel_reset(channel);
848}
849
850/*
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700851 * Probe the smd client.
852 *
853 * The remote side have indicated that it want the channel to be opened, so
854 * complete the state handshake and probe our client driver.
855 */
856static int qcom_smd_dev_probe(struct device *dev)
857{
858 struct qcom_smd_device *qsdev = to_smd_device(dev);
859 struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
860 struct qcom_smd_channel *channel = qsdev->channel;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700861 int ret;
862
Bjorn Andersson3fd3f2f2016-02-17 22:39:04 -0800863 ret = qcom_smd_channel_open(channel, qsdrv->callback);
864 if (ret)
865 return ret;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700866
867 ret = qsdrv->probe(qsdev);
868 if (ret)
869 goto err;
870
871 qcom_smd_channel_resume(channel);
872
873 return 0;
874
875err:
876 dev_err(&qsdev->dev, "probe failed\n");
877
Bjorn Andersson3fd3f2f2016-02-17 22:39:04 -0800878 qcom_smd_channel_close(channel);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700879 return ret;
880}
881
882/*
883 * Remove the smd client.
884 *
885 * The channel is going away, for some reason, so remove the smd client and
886 * reset the channel state.
887 */
888static int qcom_smd_dev_remove(struct device *dev)
889{
890 struct qcom_smd_device *qsdev = to_smd_device(dev);
891 struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
892 struct qcom_smd_channel *channel = qsdev->channel;
Bjorn Anderssond5933852016-02-17 22:39:05 -0800893 struct qcom_smd_channel *tmp;
894 struct qcom_smd_channel *ch;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700895
896 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSING);
897
898 /*
899 * Make sure we don't race with the code receiving data.
900 */
Bjorn Andersson39f0db22016-02-17 22:39:02 -0800901 qcom_smd_channel_set_callback(channel, NULL);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700902
903 /* Wake up any sleepers in qcom_smd_send() */
904 wake_up_interruptible(&channel->fblockread_event);
905
906 /*
907 * We expect that the client might block in remove() waiting for any
908 * outstanding calls to qcom_smd_send() to wake up and finish.
909 */
910 if (qsdrv->remove)
911 qsdrv->remove(qsdev);
912
913 /*
Bjorn Anderssond5933852016-02-17 22:39:05 -0800914 * The client is now gone, close and release all channels associated
915 * with this sdev
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700916 */
Bjorn Anderssond5933852016-02-17 22:39:05 -0800917 list_for_each_entry_safe(ch, tmp, &channel->dev_list, dev_list) {
918 qcom_smd_channel_close(ch);
919 list_del(&ch->dev_list);
920 ch->qsdev = NULL;
921 }
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700922
923 return 0;
924}
925
926static struct bus_type qcom_smd_bus = {
927 .name = "qcom_smd",
928 .match = qcom_smd_dev_match,
929 .probe = qcom_smd_dev_probe,
930 .remove = qcom_smd_dev_remove,
931};
932
933/*
934 * Release function for the qcom_smd_device object.
935 */
936static void qcom_smd_release_device(struct device *dev)
937{
938 struct qcom_smd_device *qsdev = to_smd_device(dev);
939
940 kfree(qsdev);
941}
942
943/*
944 * Finds the device_node for the smd child interested in this channel.
945 */
946static struct device_node *qcom_smd_match_channel(struct device_node *edge_node,
947 const char *channel)
948{
949 struct device_node *child;
950 const char *name;
951 const char *key;
952 int ret;
953
954 for_each_available_child_of_node(edge_node, child) {
955 key = "qcom,smd-channels";
956 ret = of_property_read_string(child, key, &name);
Julia Lawall60830962015-10-12 22:43:15 +0200957 if (ret)
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700958 continue;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700959
960 if (strcmp(name, channel) == 0)
961 return child;
962 }
963
964 return NULL;
965}
966
967/*
968 * Create a smd client device for channel that is being opened.
969 */
970static int qcom_smd_create_device(struct qcom_smd_channel *channel)
971{
972 struct qcom_smd_device *qsdev;
973 struct qcom_smd_edge *edge = channel->edge;
974 struct device_node *node;
975 struct qcom_smd *smd = edge->smd;
976 int ret;
977
978 if (channel->qsdev)
979 return -EEXIST;
980
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700981 dev_dbg(smd->dev, "registering '%s'\n", channel->name);
982
983 qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
984 if (!qsdev)
985 return -ENOMEM;
986
Bjorn Andersson1a7caca2015-08-28 10:39:20 -0700987 node = qcom_smd_match_channel(edge->of_node, channel->name);
988 dev_set_name(&qsdev->dev, "%s.%s",
989 edge->of_node->name,
990 node ? node->name : channel->name);
991
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700992 qsdev->dev.parent = smd->dev;
993 qsdev->dev.bus = &qcom_smd_bus;
994 qsdev->dev.release = qcom_smd_release_device;
995 qsdev->dev.of_node = node;
996
997 qsdev->channel = channel;
998
999 channel->qsdev = qsdev;
1000
1001 ret = device_register(&qsdev->dev);
1002 if (ret) {
1003 dev_err(smd->dev, "device_register failed: %d\n", ret);
1004 put_device(&qsdev->dev);
1005 }
1006
1007 return ret;
1008}
1009
1010/*
1011 * Destroy a smd client device for a channel that's going away.
1012 */
1013static void qcom_smd_destroy_device(struct qcom_smd_channel *channel)
1014{
1015 struct device *dev;
1016
1017 BUG_ON(!channel->qsdev);
1018
1019 dev = &channel->qsdev->dev;
1020
1021 device_unregister(dev);
1022 of_node_put(dev->of_node);
1023 put_device(dev);
1024}
1025
1026/**
1027 * qcom_smd_driver_register - register a smd driver
1028 * @qsdrv: qcom_smd_driver struct
1029 */
1030int qcom_smd_driver_register(struct qcom_smd_driver *qsdrv)
1031{
1032 qsdrv->driver.bus = &qcom_smd_bus;
1033 return driver_register(&qsdrv->driver);
1034}
1035EXPORT_SYMBOL(qcom_smd_driver_register);
1036
1037/**
1038 * qcom_smd_driver_unregister - unregister a smd driver
1039 * @qsdrv: qcom_smd_driver struct
1040 */
1041void qcom_smd_driver_unregister(struct qcom_smd_driver *qsdrv)
1042{
1043 driver_unregister(&qsdrv->driver);
1044}
1045EXPORT_SYMBOL(qcom_smd_driver_unregister);
1046
Bjorn Andersson028021d2016-02-17 22:39:06 -08001047static struct qcom_smd_channel *
1048qcom_smd_find_channel(struct qcom_smd_edge *edge, const char *name)
1049{
1050 struct qcom_smd_channel *channel;
1051 struct qcom_smd_channel *ret = NULL;
1052 unsigned long flags;
1053 unsigned state;
1054
1055 spin_lock_irqsave(&edge->channels_lock, flags);
1056 list_for_each_entry(channel, &edge->channels, list) {
1057 if (strcmp(channel->name, name))
1058 continue;
1059
1060 state = GET_RX_CHANNEL_INFO(channel, state);
1061 if (state != SMD_CHANNEL_OPENING &&
1062 state != SMD_CHANNEL_OPENED)
1063 continue;
1064
1065 ret = channel;
1066 break;
1067 }
1068 spin_unlock_irqrestore(&edge->channels_lock, flags);
1069
1070 return ret;
1071}
1072
1073/**
1074 * qcom_smd_open_channel() - claim additional channels on the same edge
1075 * @sdev: smd_device handle
1076 * @name: channel name
1077 * @cb: callback method to use for incoming data
1078 *
1079 * Returns a channel handle on success, or -EPROBE_DEFER if the channel isn't
1080 * ready.
1081 */
1082struct qcom_smd_channel *qcom_smd_open_channel(struct qcom_smd_device *sdev,
1083 const char *name,
1084 qcom_smd_cb_t cb)
1085{
1086 struct qcom_smd_channel *channel;
1087 struct qcom_smd_edge *edge = sdev->channel->edge;
1088 int ret;
1089
1090 /* Wait up to HZ for the channel to appear */
1091 ret = wait_event_interruptible_timeout(edge->new_channel_event,
1092 (channel = qcom_smd_find_channel(edge, name)) != NULL,
1093 HZ);
1094 if (!ret)
1095 return ERR_PTR(-ETIMEDOUT);
1096
1097 if (channel->state != SMD_CHANNEL_CLOSED) {
1098 dev_err(&sdev->dev, "channel %s is busy\n", channel->name);
1099 return ERR_PTR(-EBUSY);
1100 }
1101
1102 channel->qsdev = sdev;
1103 ret = qcom_smd_channel_open(channel, cb);
1104 if (ret) {
1105 channel->qsdev = NULL;
1106 return ERR_PTR(ret);
1107 }
1108
1109 /*
1110 * Append the list of channel to the channels associated with the sdev
1111 */
1112 list_add_tail(&channel->dev_list, &sdev->channel->dev_list);
1113
1114 return channel;
1115}
1116EXPORT_SYMBOL(qcom_smd_open_channel);
1117
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001118/*
1119 * Allocate the qcom_smd_channel object for a newly found smd channel,
1120 * retrieving and validating the smem items involved.
1121 */
1122static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge,
1123 unsigned smem_info_item,
1124 unsigned smem_fifo_item,
1125 char *name)
1126{
1127 struct qcom_smd_channel *channel;
1128 struct qcom_smd *smd = edge->smd;
1129 size_t fifo_size;
1130 size_t info_size;
1131 void *fifo_base;
1132 void *info;
1133 int ret;
1134
1135 channel = devm_kzalloc(smd->dev, sizeof(*channel), GFP_KERNEL);
1136 if (!channel)
1137 return ERR_PTR(-ENOMEM);
1138
Bjorn Anderssond5933852016-02-17 22:39:05 -08001139 INIT_LIST_HEAD(&channel->dev_list);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001140 channel->edge = edge;
1141 channel->name = devm_kstrdup(smd->dev, name, GFP_KERNEL);
1142 if (!channel->name)
1143 return ERR_PTR(-ENOMEM);
1144
1145 mutex_init(&channel->tx_lock);
1146 spin_lock_init(&channel->recv_lock);
1147 init_waitqueue_head(&channel->fblockread_event);
1148
Stephen Boyd1a039642015-09-02 15:46:44 -07001149 info = qcom_smem_get(edge->remote_pid, smem_info_item, &info_size);
1150 if (IS_ERR(info)) {
1151 ret = PTR_ERR(info);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001152 goto free_name_and_channel;
Stephen Boyd1a039642015-09-02 15:46:44 -07001153 }
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001154
1155 /*
1156 * Use the size of the item to figure out which channel info struct to
1157 * use.
1158 */
1159 if (info_size == 2 * sizeof(struct smd_channel_info_word)) {
Stephen Boydf02dc822015-09-02 15:46:46 -07001160 channel->info_word = info;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001161 } else if (info_size == 2 * sizeof(struct smd_channel_info)) {
Stephen Boydf02dc822015-09-02 15:46:46 -07001162 channel->info = info;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001163 } else {
1164 dev_err(smd->dev,
1165 "channel info of size %zu not supported\n", info_size);
1166 ret = -EINVAL;
1167 goto free_name_and_channel;
1168 }
1169
Stephen Boyd1a039642015-09-02 15:46:44 -07001170 fifo_base = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_size);
1171 if (IS_ERR(fifo_base)) {
1172 ret = PTR_ERR(fifo_base);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001173 goto free_name_and_channel;
Stephen Boyd1a039642015-09-02 15:46:44 -07001174 }
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001175
1176 /* The channel consist of a rx and tx fifo of equal size */
1177 fifo_size /= 2;
1178
1179 dev_dbg(smd->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1180 name, info_size, fifo_size);
1181
1182 channel->tx_fifo = fifo_base;
1183 channel->rx_fifo = fifo_base + fifo_size;
1184 channel->fifo_size = fifo_size;
1185
1186 qcom_smd_channel_reset(channel);
1187
1188 return channel;
1189
1190free_name_and_channel:
1191 devm_kfree(smd->dev, channel->name);
1192 devm_kfree(smd->dev, channel);
1193
1194 return ERR_PTR(ret);
1195}
1196
1197/*
1198 * Scans the allocation table for any newly allocated channels, calls
1199 * qcom_smd_create_channel() to create representations of these and add
1200 * them to the edge's list of channels.
1201 */
Bjorn Andersson995b1702016-02-17 22:39:03 -08001202static void qcom_channel_scan_worker(struct work_struct *work)
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001203{
Bjorn Andersson995b1702016-02-17 22:39:03 -08001204 struct qcom_smd_edge *edge = container_of(work, struct qcom_smd_edge, scan_work);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001205 struct qcom_smd_alloc_entry *alloc_tbl;
1206 struct qcom_smd_alloc_entry *entry;
1207 struct qcom_smd_channel *channel;
1208 struct qcom_smd *smd = edge->smd;
1209 unsigned long flags;
1210 unsigned fifo_id;
1211 unsigned info_id;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001212 int tbl;
1213 int i;
Stephen Boyd24f60e32015-09-17 14:50:53 -07001214 u32 eflags, cid;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001215
1216 for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) {
Stephen Boyd1a039642015-09-02 15:46:44 -07001217 alloc_tbl = qcom_smem_get(edge->remote_pid,
1218 smem_items[tbl].alloc_tbl_id, NULL);
1219 if (IS_ERR(alloc_tbl))
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001220 continue;
1221
1222 for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) {
1223 entry = &alloc_tbl[i];
Stephen Boyd24f60e32015-09-17 14:50:53 -07001224 eflags = le32_to_cpu(entry->flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001225 if (test_bit(i, edge->allocated[tbl]))
1226 continue;
1227
1228 if (entry->ref_count == 0)
1229 continue;
1230
1231 if (!entry->name[0])
1232 continue;
1233
Stephen Boyd24f60e32015-09-17 14:50:53 -07001234 if (!(eflags & SMD_CHANNEL_FLAGS_PACKET))
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001235 continue;
1236
Stephen Boyd24f60e32015-09-17 14:50:53 -07001237 if ((eflags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id)
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001238 continue;
1239
Stephen Boyd24f60e32015-09-17 14:50:53 -07001240 cid = le32_to_cpu(entry->cid);
1241 info_id = smem_items[tbl].info_base_id + cid;
1242 fifo_id = smem_items[tbl].fifo_base_id + cid;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001243
1244 channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
1245 if (IS_ERR(channel))
1246 continue;
1247
1248 spin_lock_irqsave(&edge->channels_lock, flags);
1249 list_add(&channel->list, &edge->channels);
1250 spin_unlock_irqrestore(&edge->channels_lock, flags);
1251
1252 dev_dbg(smd->dev, "new channel found: '%s'\n", channel->name);
1253 set_bit(i, edge->allocated[tbl]);
Bjorn Andersson028021d2016-02-17 22:39:06 -08001254
1255 wake_up_interruptible(&edge->new_channel_event);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001256 }
1257 }
1258
Bjorn Andersson995b1702016-02-17 22:39:03 -08001259 schedule_work(&edge->state_work);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001260}
1261
1262/*
1263 * This per edge worker scans smem for any new channels and register these. It
1264 * then scans all registered channels for state changes that should be handled
1265 * by creating or destroying smd client devices for the registered channels.
1266 *
Bjorn Andersson995b1702016-02-17 22:39:03 -08001267 * LOCKING: edge->channels_lock only needs to cover the list operations, as the
1268 * worker is killed before any channels are deallocated
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001269 */
1270static void qcom_channel_state_worker(struct work_struct *work)
1271{
1272 struct qcom_smd_channel *channel;
1273 struct qcom_smd_edge *edge = container_of(work,
1274 struct qcom_smd_edge,
Bjorn Andersson995b1702016-02-17 22:39:03 -08001275 state_work);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001276 unsigned remote_state;
Bjorn Andersson995b1702016-02-17 22:39:03 -08001277 unsigned long flags;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001278
1279 /*
1280 * Register a device for any closed channel where the remote processor
1281 * is showing interest in opening the channel.
1282 */
Bjorn Andersson995b1702016-02-17 22:39:03 -08001283 spin_lock_irqsave(&edge->channels_lock, flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001284 list_for_each_entry(channel, &edge->channels, list) {
1285 if (channel->state != SMD_CHANNEL_CLOSED)
1286 continue;
1287
1288 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1289 if (remote_state != SMD_CHANNEL_OPENING &&
1290 remote_state != SMD_CHANNEL_OPENED)
1291 continue;
1292
Bjorn Andersson995b1702016-02-17 22:39:03 -08001293 spin_unlock_irqrestore(&edge->channels_lock, flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001294 qcom_smd_create_device(channel);
Bjorn Andersson995b1702016-02-17 22:39:03 -08001295 spin_lock_irqsave(&edge->channels_lock, flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001296 }
1297
1298 /*
1299 * Unregister the device for any channel that is opened where the
1300 * remote processor is closing the channel.
1301 */
1302 list_for_each_entry(channel, &edge->channels, list) {
1303 if (channel->state != SMD_CHANNEL_OPENING &&
1304 channel->state != SMD_CHANNEL_OPENED)
1305 continue;
1306
1307 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1308 if (remote_state == SMD_CHANNEL_OPENING ||
1309 remote_state == SMD_CHANNEL_OPENED)
1310 continue;
1311
Bjorn Andersson995b1702016-02-17 22:39:03 -08001312 spin_unlock_irqrestore(&edge->channels_lock, flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001313 qcom_smd_destroy_device(channel);
Bjorn Andersson995b1702016-02-17 22:39:03 -08001314 spin_lock_irqsave(&edge->channels_lock, flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001315 }
Bjorn Andersson995b1702016-02-17 22:39:03 -08001316 spin_unlock_irqrestore(&edge->channels_lock, flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001317}
1318
1319/*
1320 * Parses an of_node describing an edge.
1321 */
1322static int qcom_smd_parse_edge(struct device *dev,
1323 struct device_node *node,
1324 struct qcom_smd_edge *edge)
1325{
1326 struct device_node *syscon_np;
1327 const char *key;
1328 int irq;
1329 int ret;
1330
1331 INIT_LIST_HEAD(&edge->channels);
1332 spin_lock_init(&edge->channels_lock);
1333
Bjorn Andersson995b1702016-02-17 22:39:03 -08001334 INIT_WORK(&edge->scan_work, qcom_channel_scan_worker);
1335 INIT_WORK(&edge->state_work, qcom_channel_state_worker);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001336
1337 edge->of_node = of_node_get(node);
1338
1339 irq = irq_of_parse_and_map(node, 0);
1340 if (irq < 0) {
1341 dev_err(dev, "required smd interrupt missing\n");
1342 return -EINVAL;
1343 }
1344
1345 ret = devm_request_irq(dev, irq,
1346 qcom_smd_edge_intr, IRQF_TRIGGER_RISING,
1347 node->name, edge);
1348 if (ret) {
1349 dev_err(dev, "failed to request smd irq\n");
1350 return ret;
1351 }
1352
1353 edge->irq = irq;
1354
1355 key = "qcom,smd-edge";
1356 ret = of_property_read_u32(node, key, &edge->edge_id);
1357 if (ret) {
1358 dev_err(dev, "edge missing %s property\n", key);
1359 return -EINVAL;
1360 }
1361
Andy Gross93dbed92015-08-26 14:42:45 -05001362 edge->remote_pid = QCOM_SMEM_HOST_ANY;
1363 key = "qcom,remote-pid";
1364 of_property_read_u32(node, key, &edge->remote_pid);
1365
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001366 syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
1367 if (!syscon_np) {
1368 dev_err(dev, "no qcom,ipc node\n");
1369 return -ENODEV;
1370 }
1371
1372 edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
1373 if (IS_ERR(edge->ipc_regmap))
1374 return PTR_ERR(edge->ipc_regmap);
1375
1376 key = "qcom,ipc";
1377 ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
1378 if (ret < 0) {
1379 dev_err(dev, "no offset in %s\n", key);
1380 return -EINVAL;
1381 }
1382
1383 ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
1384 if (ret < 0) {
1385 dev_err(dev, "no bit in %s\n", key);
1386 return -EINVAL;
1387 }
1388
1389 return 0;
1390}
1391
1392static int qcom_smd_probe(struct platform_device *pdev)
1393{
1394 struct qcom_smd_edge *edge;
1395 struct device_node *node;
1396 struct qcom_smd *smd;
1397 size_t array_size;
1398 int num_edges;
1399 int ret;
1400 int i = 0;
Stephen Boyd1a039642015-09-02 15:46:44 -07001401 void *p;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001402
1403 /* Wait for smem */
Stephen Boyd1a039642015-09-02 15:46:44 -07001404 p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL);
1405 if (PTR_ERR(p) == -EPROBE_DEFER)
1406 return PTR_ERR(p);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001407
1408 num_edges = of_get_available_child_count(pdev->dev.of_node);
1409 array_size = sizeof(*smd) + num_edges * sizeof(struct qcom_smd_edge);
1410 smd = devm_kzalloc(&pdev->dev, array_size, GFP_KERNEL);
1411 if (!smd)
1412 return -ENOMEM;
1413 smd->dev = &pdev->dev;
1414
1415 smd->num_edges = num_edges;
1416 for_each_available_child_of_node(pdev->dev.of_node, node) {
1417 edge = &smd->edges[i++];
1418 edge->smd = smd;
Bjorn Andersson028021d2016-02-17 22:39:06 -08001419 init_waitqueue_head(&edge->new_channel_event);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001420
1421 ret = qcom_smd_parse_edge(&pdev->dev, node, edge);
1422 if (ret)
1423 continue;
1424
Bjorn Andersson995b1702016-02-17 22:39:03 -08001425 schedule_work(&edge->scan_work);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001426 }
1427
1428 platform_set_drvdata(pdev, smd);
1429
1430 return 0;
1431}
1432
1433/*
1434 * Shut down all smd clients by making sure that each edge stops processing
1435 * events and scanning for new channels, then call destroy on the devices.
1436 */
1437static int qcom_smd_remove(struct platform_device *pdev)
1438{
1439 struct qcom_smd_channel *channel;
1440 struct qcom_smd_edge *edge;
1441 struct qcom_smd *smd = platform_get_drvdata(pdev);
1442 int i;
1443
1444 for (i = 0; i < smd->num_edges; i++) {
1445 edge = &smd->edges[i];
1446
1447 disable_irq(edge->irq);
Bjorn Andersson995b1702016-02-17 22:39:03 -08001448 cancel_work_sync(&edge->scan_work);
1449 cancel_work_sync(&edge->state_work);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001450
Bjorn Andersson995b1702016-02-17 22:39:03 -08001451 /* No need to lock here, because the writer is gone */
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001452 list_for_each_entry(channel, &edge->channels, list) {
1453 if (!channel->qsdev)
1454 continue;
1455
1456 qcom_smd_destroy_device(channel);
1457 }
1458 }
1459
1460 return 0;
1461}
1462
1463static const struct of_device_id qcom_smd_of_match[] = {
1464 { .compatible = "qcom,smd" },
1465 {}
1466};
1467MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
1468
1469static struct platform_driver qcom_smd_driver = {
1470 .probe = qcom_smd_probe,
1471 .remove = qcom_smd_remove,
1472 .driver = {
1473 .name = "qcom-smd",
1474 .of_match_table = qcom_smd_of_match,
1475 },
1476};
1477
1478static int __init qcom_smd_init(void)
1479{
1480 int ret;
1481
1482 ret = bus_register(&qcom_smd_bus);
1483 if (ret) {
1484 pr_err("failed to register smd bus: %d\n", ret);
1485 return ret;
1486 }
1487
1488 return platform_driver_register(&qcom_smd_driver);
1489}
1490postcore_initcall(qcom_smd_init);
1491
1492static void __exit qcom_smd_exit(void)
1493{
1494 platform_driver_unregister(&qcom_smd_driver);
1495 bus_unregister(&qcom_smd_bus);
1496}
1497module_exit(qcom_smd_exit);
1498
1499MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
1500MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
1501MODULE_LICENSE("GPL v2");