blob: ac1957dfdf24f313c5daaee942d62374a63fb0cf [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
Bjorn Anderssonb853cb92016-03-28 21:35:22 -0700197 void *drvdata;
198
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700199 struct list_head list;
Bjorn Anderssond5933852016-02-17 22:39:05 -0800200 struct list_head dev_list;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700201};
202
203/**
204 * struct qcom_smd - smd struct
205 * @dev: device struct
206 * @num_edges: number of entries in @edges
207 * @edges: array of edges to be handled
208 */
209struct qcom_smd {
210 struct device *dev;
211
212 unsigned num_edges;
213 struct qcom_smd_edge edges[0];
214};
215
216/*
217 * Format of the smd_info smem items, for byte aligned channels.
218 */
219struct smd_channel_info {
Stephen Boyd24f60e32015-09-17 14:50:53 -0700220 __le32 state;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700221 u8 fDSR;
222 u8 fCTS;
223 u8 fCD;
224 u8 fRI;
225 u8 fHEAD;
226 u8 fTAIL;
227 u8 fSTATE;
228 u8 fBLOCKREADINTR;
Stephen Boyd24f60e32015-09-17 14:50:53 -0700229 __le32 tail;
230 __le32 head;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700231};
232
Stephen Boydf02dc822015-09-02 15:46:46 -0700233struct smd_channel_info_pair {
234 struct smd_channel_info tx;
235 struct smd_channel_info rx;
236};
237
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700238/*
239 * Format of the smd_info smem items, for word aligned channels.
240 */
241struct smd_channel_info_word {
Stephen Boyd24f60e32015-09-17 14:50:53 -0700242 __le32 state;
243 __le32 fDSR;
244 __le32 fCTS;
245 __le32 fCD;
246 __le32 fRI;
247 __le32 fHEAD;
248 __le32 fTAIL;
249 __le32 fSTATE;
250 __le32 fBLOCKREADINTR;
251 __le32 tail;
252 __le32 head;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700253};
254
Stephen Boydf02dc822015-09-02 15:46:46 -0700255struct smd_channel_info_word_pair {
256 struct smd_channel_info_word tx;
257 struct smd_channel_info_word rx;
258};
259
Stephen Boyd24f60e32015-09-17 14:50:53 -0700260#define GET_RX_CHANNEL_FLAG(channel, param) \
261 ({ \
262 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
263 channel->info_word ? \
264 le32_to_cpu(channel->info_word->rx.param) : \
265 channel->info->rx.param; \
266 })
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700267
Stephen Boyd24f60e32015-09-17 14:50:53 -0700268#define GET_RX_CHANNEL_INFO(channel, param) \
269 ({ \
270 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
271 le32_to_cpu(channel->info_word ? \
272 channel->info_word->rx.param : \
273 channel->info->rx.param); \
274 })
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700275
Stephen Boyd24f60e32015-09-17 14:50:53 -0700276#define SET_RX_CHANNEL_FLAG(channel, param, value) \
277 ({ \
278 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \
279 if (channel->info_word) \
280 channel->info_word->rx.param = cpu_to_le32(value); \
281 else \
282 channel->info->rx.param = value; \
283 })
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700284
Stephen Boyd24f60e32015-09-17 14:50:53 -0700285#define SET_RX_CHANNEL_INFO(channel, param, value) \
286 ({ \
287 BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \
288 if (channel->info_word) \
289 channel->info_word->rx.param = cpu_to_le32(value); \
290 else \
291 channel->info->rx.param = cpu_to_le32(value); \
292 })
293
294#define GET_TX_CHANNEL_FLAG(channel, param) \
295 ({ \
296 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
297 channel->info_word ? \
298 le32_to_cpu(channel->info_word->tx.param) : \
299 channel->info->tx.param; \
300 })
301
302#define GET_TX_CHANNEL_INFO(channel, param) \
303 ({ \
304 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
305 le32_to_cpu(channel->info_word ? \
306 channel->info_word->tx.param : \
307 channel->info->tx.param); \
308 })
309
310#define SET_TX_CHANNEL_FLAG(channel, param, value) \
311 ({ \
312 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \
313 if (channel->info_word) \
314 channel->info_word->tx.param = cpu_to_le32(value); \
315 else \
316 channel->info->tx.param = value; \
317 })
318
319#define SET_TX_CHANNEL_INFO(channel, param, value) \
320 ({ \
321 BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \
322 if (channel->info_word) \
323 channel->info_word->tx.param = cpu_to_le32(value); \
324 else \
325 channel->info->tx.param = cpu_to_le32(value); \
326 })
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700327
328/**
329 * struct qcom_smd_alloc_entry - channel allocation entry
330 * @name: channel name
331 * @cid: channel index
332 * @flags: channel flags and edge id
333 * @ref_count: reference count of the channel
334 */
335struct qcom_smd_alloc_entry {
336 u8 name[20];
Stephen Boyd24f60e32015-09-17 14:50:53 -0700337 __le32 cid;
338 __le32 flags;
339 __le32 ref_count;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700340} __packed;
341
342#define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff
343#define SMD_CHANNEL_FLAGS_STREAM BIT(8)
344#define SMD_CHANNEL_FLAGS_PACKET BIT(9)
345
346/*
347 * Each smd packet contains a 20 byte header, with the first 4 being the length
348 * of the packet.
349 */
350#define SMD_PACKET_HEADER_LEN 20
351
352/*
353 * Signal the remote processor associated with 'channel'.
354 */
355static void qcom_smd_signal_channel(struct qcom_smd_channel *channel)
356{
357 struct qcom_smd_edge *edge = channel->edge;
358
359 regmap_write(edge->ipc_regmap, edge->ipc_offset, BIT(edge->ipc_bit));
360}
361
362/*
363 * Initialize the tx channel info
364 */
365static void qcom_smd_channel_reset(struct qcom_smd_channel *channel)
366{
367 SET_TX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED);
Stephen Boyd24f60e32015-09-17 14:50:53 -0700368 SET_TX_CHANNEL_FLAG(channel, fDSR, 0);
369 SET_TX_CHANNEL_FLAG(channel, fCTS, 0);
370 SET_TX_CHANNEL_FLAG(channel, fCD, 0);
371 SET_TX_CHANNEL_FLAG(channel, fRI, 0);
372 SET_TX_CHANNEL_FLAG(channel, fHEAD, 0);
373 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
374 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
375 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700376 SET_TX_CHANNEL_INFO(channel, head, 0);
377 SET_TX_CHANNEL_INFO(channel, tail, 0);
378
379 qcom_smd_signal_channel(channel);
380
381 channel->state = SMD_CHANNEL_CLOSED;
382 channel->pkt_size = 0;
383}
384
385/*
Bjorn Andersson39f0db22016-02-17 22:39:02 -0800386 * Set the callback for a channel, with appropriate locking
387 */
388static void qcom_smd_channel_set_callback(struct qcom_smd_channel *channel,
389 qcom_smd_cb_t cb)
390{
391 unsigned long flags;
392
393 spin_lock_irqsave(&channel->recv_lock, flags);
394 channel->cb = cb;
395 spin_unlock_irqrestore(&channel->recv_lock, flags);
396};
397
398/*
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700399 * Calculate the amount of data available in the rx fifo
400 */
401static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel *channel)
402{
403 unsigned head;
404 unsigned tail;
405
406 head = GET_RX_CHANNEL_INFO(channel, head);
407 tail = GET_RX_CHANNEL_INFO(channel, tail);
408
409 return (head - tail) & (channel->fifo_size - 1);
410}
411
412/*
413 * Set tx channel state and inform the remote processor
414 */
415static void qcom_smd_channel_set_state(struct qcom_smd_channel *channel,
416 int state)
417{
418 struct qcom_smd_edge *edge = channel->edge;
419 bool is_open = state == SMD_CHANNEL_OPENED;
420
421 if (channel->state == state)
422 return;
423
424 dev_dbg(edge->smd->dev, "set_state(%s, %d)\n", channel->name, state);
425
Stephen Boyd24f60e32015-09-17 14:50:53 -0700426 SET_TX_CHANNEL_FLAG(channel, fDSR, is_open);
427 SET_TX_CHANNEL_FLAG(channel, fCTS, is_open);
428 SET_TX_CHANNEL_FLAG(channel, fCD, is_open);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700429
430 SET_TX_CHANNEL_INFO(channel, state, state);
Stephen Boyd24f60e32015-09-17 14:50:53 -0700431 SET_TX_CHANNEL_FLAG(channel, fSTATE, 1);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700432
433 channel->state = state;
434 qcom_smd_signal_channel(channel);
435}
436
437/*
438 * Copy count bytes of data using 32bit accesses, if that's required.
439 */
Stephen Boyd3b781e52015-09-02 15:46:47 -0700440static void smd_copy_to_fifo(void __iomem *dst,
441 const void *src,
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700442 size_t count,
443 bool word_aligned)
444{
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700445 if (word_aligned) {
Stephen Boyd3b781e52015-09-02 15:46:47 -0700446 __iowrite32_copy(dst, src, count / sizeof(u32));
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700447 } else {
Stephen Boyd3b781e52015-09-02 15:46:47 -0700448 memcpy_toio(dst, src, count);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700449 }
450}
451
452/*
453 * Copy count bytes of data using 32bit accesses, if that is required.
454 */
Stephen Boydc431e672016-01-20 14:58:38 -0800455static void smd_copy_from_fifo(void *dst,
456 const void __iomem *src,
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700457 size_t count,
458 bool word_aligned)
459{
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700460 if (word_aligned) {
Stephen Boydc431e672016-01-20 14:58:38 -0800461 __ioread32_copy(dst, src, count / sizeof(u32));
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700462 } else {
Stephen Boydc431e672016-01-20 14:58:38 -0800463 memcpy_fromio(dst, src, count);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700464 }
465}
466
467/*
468 * Read count bytes of data from the rx fifo into buf, but don't advance the
469 * tail.
470 */
471static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel,
472 void *buf, size_t count)
473{
474 bool word_aligned;
475 unsigned tail;
476 size_t len;
477
Stephen Boydf02dc822015-09-02 15:46:46 -0700478 word_aligned = channel->info_word;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700479 tail = GET_RX_CHANNEL_INFO(channel, tail);
480
481 len = min_t(size_t, count, channel->fifo_size - tail);
482 if (len) {
483 smd_copy_from_fifo(buf,
484 channel->rx_fifo + tail,
485 len,
486 word_aligned);
487 }
488
489 if (len != count) {
490 smd_copy_from_fifo(buf + len,
491 channel->rx_fifo,
492 count - len,
493 word_aligned);
494 }
495
496 return count;
497}
498
499/*
500 * Advance the rx tail by count bytes.
501 */
502static void qcom_smd_channel_advance(struct qcom_smd_channel *channel,
503 size_t count)
504{
505 unsigned tail;
506
507 tail = GET_RX_CHANNEL_INFO(channel, tail);
508 tail += count;
509 tail &= (channel->fifo_size - 1);
510 SET_RX_CHANNEL_INFO(channel, tail, tail);
511}
512
513/*
514 * Read out a single packet from the rx fifo and deliver it to the device
515 */
516static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
517{
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700518 unsigned tail;
519 size_t len;
520 void *ptr;
521 int ret;
522
523 if (!channel->cb)
524 return 0;
525
526 tail = GET_RX_CHANNEL_INFO(channel, tail);
527
528 /* Use bounce buffer if the data wraps */
529 if (tail + channel->pkt_size >= channel->fifo_size) {
530 ptr = channel->bounce_buffer;
531 len = qcom_smd_channel_peek(channel, ptr, channel->pkt_size);
532 } else {
533 ptr = channel->rx_fifo + tail;
534 len = channel->pkt_size;
535 }
536
Bjorn Anderssonb853cb92016-03-28 21:35:22 -0700537 ret = channel->cb(channel, ptr, len);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700538 if (ret < 0)
539 return ret;
540
541 /* Only forward the tail if the client consumed the data */
542 qcom_smd_channel_advance(channel, len);
543
544 channel->pkt_size = 0;
545
546 return 0;
547}
548
549/*
550 * Per channel interrupt handling
551 */
552static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel)
553{
554 bool need_state_scan = false;
555 int remote_state;
Stephen Boyd24f60e32015-09-17 14:50:53 -0700556 __le32 pktlen;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700557 int avail;
558 int ret;
559
560 /* Handle state changes */
561 remote_state = GET_RX_CHANNEL_INFO(channel, state);
562 if (remote_state != channel->remote_state) {
563 channel->remote_state = remote_state;
564 need_state_scan = true;
565 }
566 /* Indicate that we have seen any state change */
Stephen Boyd24f60e32015-09-17 14:50:53 -0700567 SET_RX_CHANNEL_FLAG(channel, fSTATE, 0);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700568
569 /* Signal waiting qcom_smd_send() about the interrupt */
Stephen Boyd24f60e32015-09-17 14:50:53 -0700570 if (!GET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR))
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700571 wake_up_interruptible(&channel->fblockread_event);
572
573 /* Don't consume any data until we've opened the channel */
574 if (channel->state != SMD_CHANNEL_OPENED)
575 goto out;
576
577 /* Indicate that we've seen the new data */
Stephen Boyd24f60e32015-09-17 14:50:53 -0700578 SET_RX_CHANNEL_FLAG(channel, fHEAD, 0);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700579
580 /* Consume data */
581 for (;;) {
582 avail = qcom_smd_channel_get_rx_avail(channel);
583
584 if (!channel->pkt_size && avail >= SMD_PACKET_HEADER_LEN) {
585 qcom_smd_channel_peek(channel, &pktlen, sizeof(pktlen));
586 qcom_smd_channel_advance(channel, SMD_PACKET_HEADER_LEN);
Stephen Boyd24f60e32015-09-17 14:50:53 -0700587 channel->pkt_size = le32_to_cpu(pktlen);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700588 } else if (channel->pkt_size && avail >= channel->pkt_size) {
589 ret = qcom_smd_channel_recv_single(channel);
590 if (ret)
591 break;
592 } else {
593 break;
594 }
595 }
596
597 /* Indicate that we have seen and updated tail */
Stephen Boyd24f60e32015-09-17 14:50:53 -0700598 SET_RX_CHANNEL_FLAG(channel, fTAIL, 1);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700599
600 /* Signal the remote that we've consumed the data (if requested) */
Stephen Boyd24f60e32015-09-17 14:50:53 -0700601 if (!GET_RX_CHANNEL_FLAG(channel, fBLOCKREADINTR)) {
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700602 /* Ensure ordering of channel info updates */
603 wmb();
604
605 qcom_smd_signal_channel(channel);
606 }
607
608out:
609 return need_state_scan;
610}
611
612/*
613 * The edge interrupts are triggered by the remote processor on state changes,
614 * channel info updates or when new channels are created.
615 */
616static irqreturn_t qcom_smd_edge_intr(int irq, void *data)
617{
618 struct qcom_smd_edge *edge = data;
619 struct qcom_smd_channel *channel;
620 unsigned available;
Bjorn Andersson995b1702016-02-17 22:39:03 -0800621 bool kick_scanner = false;
622 bool kick_state = false;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700623
624 /*
625 * Handle state changes or data on each of the channels on this edge
626 */
627 spin_lock(&edge->channels_lock);
628 list_for_each_entry(channel, &edge->channels, list) {
629 spin_lock(&channel->recv_lock);
Bjorn Andersson995b1702016-02-17 22:39:03 -0800630 kick_state |= qcom_smd_channel_intr(channel);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700631 spin_unlock(&channel->recv_lock);
632 }
633 spin_unlock(&edge->channels_lock);
634
635 /*
636 * Creating a new channel requires allocating an smem entry, so we only
637 * have to scan if the amount of available space in smem have changed
638 * since last scan.
639 */
Andy Gross93dbed92015-08-26 14:42:45 -0500640 available = qcom_smem_get_free_space(edge->remote_pid);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700641 if (available != edge->smem_available) {
642 edge->smem_available = available;
Bjorn Andersson995b1702016-02-17 22:39:03 -0800643 kick_scanner = true;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700644 }
645
Bjorn Andersson995b1702016-02-17 22:39:03 -0800646 if (kick_scanner)
647 schedule_work(&edge->scan_work);
648 if (kick_state)
649 schedule_work(&edge->state_work);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700650
651 return IRQ_HANDLED;
652}
653
654/*
655 * Delivers any outstanding packets in the rx fifo, can be used after probe of
656 * the clients to deliver any packets that wasn't delivered before the client
657 * was setup.
658 */
659static void qcom_smd_channel_resume(struct qcom_smd_channel *channel)
660{
661 unsigned long flags;
662
663 spin_lock_irqsave(&channel->recv_lock, flags);
664 qcom_smd_channel_intr(channel);
665 spin_unlock_irqrestore(&channel->recv_lock, flags);
666}
667
668/*
669 * Calculate how much space is available in the tx fifo.
670 */
671static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel)
672{
673 unsigned head;
674 unsigned tail;
675 unsigned mask = channel->fifo_size - 1;
676
677 head = GET_TX_CHANNEL_INFO(channel, head);
678 tail = GET_TX_CHANNEL_INFO(channel, tail);
679
680 return mask - ((head - tail) & mask);
681}
682
683/*
684 * Write count bytes of data into channel, possibly wrapping in the ring buffer
685 */
686static int qcom_smd_write_fifo(struct qcom_smd_channel *channel,
687 const void *data,
688 size_t count)
689{
690 bool word_aligned;
691 unsigned head;
692 size_t len;
693
Stephen Boydf02dc822015-09-02 15:46:46 -0700694 word_aligned = channel->info_word;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700695 head = GET_TX_CHANNEL_INFO(channel, head);
696
697 len = min_t(size_t, count, channel->fifo_size - head);
698 if (len) {
699 smd_copy_to_fifo(channel->tx_fifo + head,
700 data,
701 len,
702 word_aligned);
703 }
704
705 if (len != count) {
706 smd_copy_to_fifo(channel->tx_fifo,
707 data + len,
708 count - len,
709 word_aligned);
710 }
711
712 head += count;
713 head &= (channel->fifo_size - 1);
714 SET_TX_CHANNEL_INFO(channel, head, head);
715
716 return count;
717}
718
719/**
720 * qcom_smd_send - write data to smd channel
721 * @channel: channel handle
722 * @data: buffer of data to write
723 * @len: number of bytes to write
724 *
725 * This is a blocking write of len bytes into the channel's tx ring buffer and
726 * signal the remote end. It will sleep until there is enough space available
727 * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid
728 * polling.
729 */
730int qcom_smd_send(struct qcom_smd_channel *channel, const void *data, int len)
731{
Stephen Boyd24f60e32015-09-17 14:50:53 -0700732 __le32 hdr[5] = { cpu_to_le32(len), };
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700733 int tlen = sizeof(hdr) + len;
734 int ret;
735
736 /* Word aligned channels only accept word size aligned data */
Stephen Boydf02dc822015-09-02 15:46:46 -0700737 if (channel->info_word && len % 4)
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700738 return -EINVAL;
739
Bjorn Anderssona208ca92015-09-24 18:37:18 -0700740 /* Reject packets that are too big */
741 if (tlen >= channel->fifo_size)
742 return -EINVAL;
743
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700744 ret = mutex_lock_interruptible(&channel->tx_lock);
745 if (ret)
746 return ret;
747
748 while (qcom_smd_get_tx_avail(channel) < tlen) {
749 if (channel->state != SMD_CHANNEL_OPENED) {
750 ret = -EPIPE;
751 goto out;
752 }
753
Stephen Boyd24f60e32015-09-17 14:50:53 -0700754 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 0);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700755
756 ret = wait_event_interruptible(channel->fblockread_event,
757 qcom_smd_get_tx_avail(channel) >= tlen ||
758 channel->state != SMD_CHANNEL_OPENED);
759 if (ret)
760 goto out;
761
Stephen Boyd24f60e32015-09-17 14:50:53 -0700762 SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700763 }
764
Stephen Boyd24f60e32015-09-17 14:50:53 -0700765 SET_TX_CHANNEL_FLAG(channel, fTAIL, 0);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700766
767 qcom_smd_write_fifo(channel, hdr, sizeof(hdr));
768 qcom_smd_write_fifo(channel, data, len);
769
Stephen Boyd24f60e32015-09-17 14:50:53 -0700770 SET_TX_CHANNEL_FLAG(channel, fHEAD, 1);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700771
772 /* Ensure ordering of channel info updates */
773 wmb();
774
775 qcom_smd_signal_channel(channel);
776
777out:
778 mutex_unlock(&channel->tx_lock);
779
780 return ret;
781}
782EXPORT_SYMBOL(qcom_smd_send);
783
784static struct qcom_smd_device *to_smd_device(struct device *dev)
785{
786 return container_of(dev, struct qcom_smd_device, dev);
787}
788
789static struct qcom_smd_driver *to_smd_driver(struct device *dev)
790{
791 struct qcom_smd_device *qsdev = to_smd_device(dev);
792
793 return container_of(qsdev->dev.driver, struct qcom_smd_driver, driver);
794}
795
796static int qcom_smd_dev_match(struct device *dev, struct device_driver *drv)
797{
Bjorn Andersson1a7caca2015-08-28 10:39:20 -0700798 struct qcom_smd_device *qsdev = to_smd_device(dev);
799 struct qcom_smd_driver *qsdrv = container_of(drv, struct qcom_smd_driver, driver);
800 const struct qcom_smd_id *match = qsdrv->smd_match_table;
801 const char *name = qsdev->channel->name;
802
803 if (match) {
804 while (match->name[0]) {
805 if (!strcmp(match->name, name))
806 return 1;
807 match++;
808 }
809 }
810
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700811 return of_driver_match_device(dev, drv);
812}
813
814/*
Bjorn Andersson3fd3f2f2016-02-17 22:39:04 -0800815 * Helper for opening a channel
816 */
817static int qcom_smd_channel_open(struct qcom_smd_channel *channel,
818 qcom_smd_cb_t cb)
819{
820 size_t bb_size;
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);
832 qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED);
833
834 return 0;
835}
836
837/*
838 * Helper for closing and resetting a channel
839 */
840static void qcom_smd_channel_close(struct qcom_smd_channel *channel)
841{
842 qcom_smd_channel_set_callback(channel, NULL);
843
844 kfree(channel->bounce_buffer);
845 channel->bounce_buffer = NULL;
846
847 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED);
848 qcom_smd_channel_reset(channel);
849}
850
851/*
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700852 * Probe the smd client.
853 *
854 * The remote side have indicated that it want the channel to be opened, so
855 * complete the state handshake and probe our client driver.
856 */
857static int qcom_smd_dev_probe(struct device *dev)
858{
859 struct qcom_smd_device *qsdev = to_smd_device(dev);
860 struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
861 struct qcom_smd_channel *channel = qsdev->channel;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700862 int ret;
863
Bjorn Andersson3fd3f2f2016-02-17 22:39:04 -0800864 ret = qcom_smd_channel_open(channel, qsdrv->callback);
865 if (ret)
866 return ret;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700867
868 ret = qsdrv->probe(qsdev);
869 if (ret)
870 goto err;
871
872 qcom_smd_channel_resume(channel);
873
874 return 0;
875
876err:
877 dev_err(&qsdev->dev, "probe failed\n");
878
Bjorn Andersson3fd3f2f2016-02-17 22:39:04 -0800879 qcom_smd_channel_close(channel);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700880 return ret;
881}
882
883/*
884 * Remove the smd client.
885 *
886 * The channel is going away, for some reason, so remove the smd client and
887 * reset the channel state.
888 */
889static int qcom_smd_dev_remove(struct device *dev)
890{
891 struct qcom_smd_device *qsdev = to_smd_device(dev);
892 struct qcom_smd_driver *qsdrv = to_smd_driver(dev);
893 struct qcom_smd_channel *channel = qsdev->channel;
Bjorn Anderssond5933852016-02-17 22:39:05 -0800894 struct qcom_smd_channel *tmp;
895 struct qcom_smd_channel *ch;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700896
897 qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSING);
898
899 /*
900 * Make sure we don't race with the code receiving data.
901 */
Bjorn Andersson39f0db22016-02-17 22:39:02 -0800902 qcom_smd_channel_set_callback(channel, NULL);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700903
904 /* Wake up any sleepers in qcom_smd_send() */
905 wake_up_interruptible(&channel->fblockread_event);
906
907 /*
908 * We expect that the client might block in remove() waiting for any
909 * outstanding calls to qcom_smd_send() to wake up and finish.
910 */
911 if (qsdrv->remove)
912 qsdrv->remove(qsdev);
913
914 /*
Bjorn Anderssond5933852016-02-17 22:39:05 -0800915 * The client is now gone, close and release all channels associated
916 * with this sdev
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700917 */
Bjorn Anderssond5933852016-02-17 22:39:05 -0800918 list_for_each_entry_safe(ch, tmp, &channel->dev_list, dev_list) {
919 qcom_smd_channel_close(ch);
920 list_del(&ch->dev_list);
921 ch->qsdev = NULL;
922 }
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700923
924 return 0;
925}
926
927static struct bus_type qcom_smd_bus = {
928 .name = "qcom_smd",
929 .match = qcom_smd_dev_match,
930 .probe = qcom_smd_dev_probe,
931 .remove = qcom_smd_dev_remove,
932};
933
934/*
935 * Release function for the qcom_smd_device object.
936 */
937static void qcom_smd_release_device(struct device *dev)
938{
939 struct qcom_smd_device *qsdev = to_smd_device(dev);
940
941 kfree(qsdev);
942}
943
944/*
945 * Finds the device_node for the smd child interested in this channel.
946 */
947static struct device_node *qcom_smd_match_channel(struct device_node *edge_node,
948 const char *channel)
949{
950 struct device_node *child;
951 const char *name;
952 const char *key;
953 int ret;
954
955 for_each_available_child_of_node(edge_node, child) {
956 key = "qcom,smd-channels";
957 ret = of_property_read_string(child, key, &name);
Julia Lawall60830962015-10-12 22:43:15 +0200958 if (ret)
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700959 continue;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700960
961 if (strcmp(name, channel) == 0)
962 return child;
963 }
964
965 return NULL;
966}
967
968/*
969 * Create a smd client device for channel that is being opened.
970 */
971static int qcom_smd_create_device(struct qcom_smd_channel *channel)
972{
973 struct qcom_smd_device *qsdev;
974 struct qcom_smd_edge *edge = channel->edge;
975 struct device_node *node;
976 struct qcom_smd *smd = edge->smd;
977 int ret;
978
979 if (channel->qsdev)
980 return -EEXIST;
981
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700982 dev_dbg(smd->dev, "registering '%s'\n", channel->name);
983
984 qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL);
985 if (!qsdev)
986 return -ENOMEM;
987
Bjorn Andersson1a7caca2015-08-28 10:39:20 -0700988 node = qcom_smd_match_channel(edge->of_node, channel->name);
989 dev_set_name(&qsdev->dev, "%s.%s",
990 edge->of_node->name,
991 node ? node->name : channel->name);
992
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -0700993 qsdev->dev.parent = smd->dev;
994 qsdev->dev.bus = &qcom_smd_bus;
995 qsdev->dev.release = qcom_smd_release_device;
996 qsdev->dev.of_node = node;
997
998 qsdev->channel = channel;
999
1000 channel->qsdev = qsdev;
1001
1002 ret = device_register(&qsdev->dev);
1003 if (ret) {
1004 dev_err(smd->dev, "device_register failed: %d\n", ret);
1005 put_device(&qsdev->dev);
1006 }
1007
1008 return ret;
1009}
1010
1011/*
1012 * Destroy a smd client device for a channel that's going away.
1013 */
1014static void qcom_smd_destroy_device(struct qcom_smd_channel *channel)
1015{
1016 struct device *dev;
1017
1018 BUG_ON(!channel->qsdev);
1019
1020 dev = &channel->qsdev->dev;
1021
1022 device_unregister(dev);
1023 of_node_put(dev->of_node);
1024 put_device(dev);
1025}
1026
1027/**
1028 * qcom_smd_driver_register - register a smd driver
1029 * @qsdrv: qcom_smd_driver struct
1030 */
1031int qcom_smd_driver_register(struct qcom_smd_driver *qsdrv)
1032{
1033 qsdrv->driver.bus = &qcom_smd_bus;
1034 return driver_register(&qsdrv->driver);
1035}
1036EXPORT_SYMBOL(qcom_smd_driver_register);
1037
Bjorn Anderssonb853cb92016-03-28 21:35:22 -07001038void *qcom_smd_get_drvdata(struct qcom_smd_channel *channel)
1039{
1040 return channel->drvdata;
1041}
1042EXPORT_SYMBOL(qcom_smd_get_drvdata);
1043
1044void qcom_smd_set_drvdata(struct qcom_smd_channel *channel, void *data)
1045{
1046 channel->drvdata = data;
1047}
1048EXPORT_SYMBOL(qcom_smd_set_drvdata);
1049
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001050/**
1051 * qcom_smd_driver_unregister - unregister a smd driver
1052 * @qsdrv: qcom_smd_driver struct
1053 */
1054void qcom_smd_driver_unregister(struct qcom_smd_driver *qsdrv)
1055{
1056 driver_unregister(&qsdrv->driver);
1057}
1058EXPORT_SYMBOL(qcom_smd_driver_unregister);
1059
Bjorn Andersson028021d2016-02-17 22:39:06 -08001060static struct qcom_smd_channel *
1061qcom_smd_find_channel(struct qcom_smd_edge *edge, const char *name)
1062{
1063 struct qcom_smd_channel *channel;
1064 struct qcom_smd_channel *ret = NULL;
1065 unsigned long flags;
1066 unsigned state;
1067
1068 spin_lock_irqsave(&edge->channels_lock, flags);
1069 list_for_each_entry(channel, &edge->channels, list) {
1070 if (strcmp(channel->name, name))
1071 continue;
1072
1073 state = GET_RX_CHANNEL_INFO(channel, state);
1074 if (state != SMD_CHANNEL_OPENING &&
1075 state != SMD_CHANNEL_OPENED)
1076 continue;
1077
1078 ret = channel;
1079 break;
1080 }
1081 spin_unlock_irqrestore(&edge->channels_lock, flags);
1082
1083 return ret;
1084}
1085
1086/**
1087 * qcom_smd_open_channel() - claim additional channels on the same edge
1088 * @sdev: smd_device handle
1089 * @name: channel name
1090 * @cb: callback method to use for incoming data
1091 *
1092 * Returns a channel handle on success, or -EPROBE_DEFER if the channel isn't
1093 * ready.
1094 */
Bjorn Anderssonb853cb92016-03-28 21:35:22 -07001095struct qcom_smd_channel *qcom_smd_open_channel(struct qcom_smd_channel *parent,
Bjorn Andersson028021d2016-02-17 22:39:06 -08001096 const char *name,
1097 qcom_smd_cb_t cb)
1098{
1099 struct qcom_smd_channel *channel;
Bjorn Anderssonb853cb92016-03-28 21:35:22 -07001100 struct qcom_smd_device *sdev = parent->qsdev;
1101 struct qcom_smd_edge *edge = parent->edge;
Bjorn Andersson028021d2016-02-17 22:39:06 -08001102 int ret;
1103
1104 /* Wait up to HZ for the channel to appear */
1105 ret = wait_event_interruptible_timeout(edge->new_channel_event,
1106 (channel = qcom_smd_find_channel(edge, name)) != NULL,
1107 HZ);
1108 if (!ret)
1109 return ERR_PTR(-ETIMEDOUT);
1110
1111 if (channel->state != SMD_CHANNEL_CLOSED) {
1112 dev_err(&sdev->dev, "channel %s is busy\n", channel->name);
1113 return ERR_PTR(-EBUSY);
1114 }
1115
1116 channel->qsdev = sdev;
1117 ret = qcom_smd_channel_open(channel, cb);
1118 if (ret) {
1119 channel->qsdev = NULL;
1120 return ERR_PTR(ret);
1121 }
1122
1123 /*
1124 * Append the list of channel to the channels associated with the sdev
1125 */
1126 list_add_tail(&channel->dev_list, &sdev->channel->dev_list);
1127
1128 return channel;
1129}
1130EXPORT_SYMBOL(qcom_smd_open_channel);
1131
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001132/*
1133 * Allocate the qcom_smd_channel object for a newly found smd channel,
1134 * retrieving and validating the smem items involved.
1135 */
1136static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge,
1137 unsigned smem_info_item,
1138 unsigned smem_fifo_item,
1139 char *name)
1140{
1141 struct qcom_smd_channel *channel;
1142 struct qcom_smd *smd = edge->smd;
1143 size_t fifo_size;
1144 size_t info_size;
1145 void *fifo_base;
1146 void *info;
1147 int ret;
1148
1149 channel = devm_kzalloc(smd->dev, sizeof(*channel), GFP_KERNEL);
1150 if (!channel)
1151 return ERR_PTR(-ENOMEM);
1152
Bjorn Anderssond5933852016-02-17 22:39:05 -08001153 INIT_LIST_HEAD(&channel->dev_list);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001154 channel->edge = edge;
1155 channel->name = devm_kstrdup(smd->dev, name, GFP_KERNEL);
1156 if (!channel->name)
1157 return ERR_PTR(-ENOMEM);
1158
1159 mutex_init(&channel->tx_lock);
1160 spin_lock_init(&channel->recv_lock);
1161 init_waitqueue_head(&channel->fblockread_event);
1162
Stephen Boyd1a039642015-09-02 15:46:44 -07001163 info = qcom_smem_get(edge->remote_pid, smem_info_item, &info_size);
1164 if (IS_ERR(info)) {
1165 ret = PTR_ERR(info);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001166 goto free_name_and_channel;
Stephen Boyd1a039642015-09-02 15:46:44 -07001167 }
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001168
1169 /*
1170 * Use the size of the item to figure out which channel info struct to
1171 * use.
1172 */
1173 if (info_size == 2 * sizeof(struct smd_channel_info_word)) {
Stephen Boydf02dc822015-09-02 15:46:46 -07001174 channel->info_word = info;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001175 } else if (info_size == 2 * sizeof(struct smd_channel_info)) {
Stephen Boydf02dc822015-09-02 15:46:46 -07001176 channel->info = info;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001177 } else {
1178 dev_err(smd->dev,
1179 "channel info of size %zu not supported\n", info_size);
1180 ret = -EINVAL;
1181 goto free_name_and_channel;
1182 }
1183
Stephen Boyd1a039642015-09-02 15:46:44 -07001184 fifo_base = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_size);
1185 if (IS_ERR(fifo_base)) {
1186 ret = PTR_ERR(fifo_base);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001187 goto free_name_and_channel;
Stephen Boyd1a039642015-09-02 15:46:44 -07001188 }
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001189
1190 /* The channel consist of a rx and tx fifo of equal size */
1191 fifo_size /= 2;
1192
1193 dev_dbg(smd->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n",
1194 name, info_size, fifo_size);
1195
1196 channel->tx_fifo = fifo_base;
1197 channel->rx_fifo = fifo_base + fifo_size;
1198 channel->fifo_size = fifo_size;
1199
1200 qcom_smd_channel_reset(channel);
1201
1202 return channel;
1203
1204free_name_and_channel:
1205 devm_kfree(smd->dev, channel->name);
1206 devm_kfree(smd->dev, channel);
1207
1208 return ERR_PTR(ret);
1209}
1210
1211/*
1212 * Scans the allocation table for any newly allocated channels, calls
1213 * qcom_smd_create_channel() to create representations of these and add
1214 * them to the edge's list of channels.
1215 */
Bjorn Andersson995b1702016-02-17 22:39:03 -08001216static void qcom_channel_scan_worker(struct work_struct *work)
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001217{
Bjorn Andersson995b1702016-02-17 22:39:03 -08001218 struct qcom_smd_edge *edge = container_of(work, struct qcom_smd_edge, scan_work);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001219 struct qcom_smd_alloc_entry *alloc_tbl;
1220 struct qcom_smd_alloc_entry *entry;
1221 struct qcom_smd_channel *channel;
1222 struct qcom_smd *smd = edge->smd;
1223 unsigned long flags;
1224 unsigned fifo_id;
1225 unsigned info_id;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001226 int tbl;
1227 int i;
Stephen Boyd24f60e32015-09-17 14:50:53 -07001228 u32 eflags, cid;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001229
1230 for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) {
Stephen Boyd1a039642015-09-02 15:46:44 -07001231 alloc_tbl = qcom_smem_get(edge->remote_pid,
1232 smem_items[tbl].alloc_tbl_id, NULL);
1233 if (IS_ERR(alloc_tbl))
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001234 continue;
1235
1236 for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) {
1237 entry = &alloc_tbl[i];
Stephen Boyd24f60e32015-09-17 14:50:53 -07001238 eflags = le32_to_cpu(entry->flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001239 if (test_bit(i, edge->allocated[tbl]))
1240 continue;
1241
1242 if (entry->ref_count == 0)
1243 continue;
1244
1245 if (!entry->name[0])
1246 continue;
1247
Stephen Boyd24f60e32015-09-17 14:50:53 -07001248 if (!(eflags & SMD_CHANNEL_FLAGS_PACKET))
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001249 continue;
1250
Stephen Boyd24f60e32015-09-17 14:50:53 -07001251 if ((eflags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id)
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001252 continue;
1253
Stephen Boyd24f60e32015-09-17 14:50:53 -07001254 cid = le32_to_cpu(entry->cid);
1255 info_id = smem_items[tbl].info_base_id + cid;
1256 fifo_id = smem_items[tbl].fifo_base_id + cid;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001257
1258 channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name);
1259 if (IS_ERR(channel))
1260 continue;
1261
1262 spin_lock_irqsave(&edge->channels_lock, flags);
1263 list_add(&channel->list, &edge->channels);
1264 spin_unlock_irqrestore(&edge->channels_lock, flags);
1265
1266 dev_dbg(smd->dev, "new channel found: '%s'\n", channel->name);
1267 set_bit(i, edge->allocated[tbl]);
Bjorn Andersson028021d2016-02-17 22:39:06 -08001268
1269 wake_up_interruptible(&edge->new_channel_event);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001270 }
1271 }
1272
Bjorn Andersson995b1702016-02-17 22:39:03 -08001273 schedule_work(&edge->state_work);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001274}
1275
1276/*
1277 * This per edge worker scans smem for any new channels and register these. It
1278 * then scans all registered channels for state changes that should be handled
1279 * by creating or destroying smd client devices for the registered channels.
1280 *
Bjorn Andersson995b1702016-02-17 22:39:03 -08001281 * LOCKING: edge->channels_lock only needs to cover the list operations, as the
1282 * worker is killed before any channels are deallocated
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001283 */
1284static void qcom_channel_state_worker(struct work_struct *work)
1285{
1286 struct qcom_smd_channel *channel;
1287 struct qcom_smd_edge *edge = container_of(work,
1288 struct qcom_smd_edge,
Bjorn Andersson995b1702016-02-17 22:39:03 -08001289 state_work);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001290 unsigned remote_state;
Bjorn Andersson995b1702016-02-17 22:39:03 -08001291 unsigned long flags;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001292
1293 /*
1294 * Register a device for any closed channel where the remote processor
1295 * is showing interest in opening the channel.
1296 */
Bjorn Andersson995b1702016-02-17 22:39:03 -08001297 spin_lock_irqsave(&edge->channels_lock, flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001298 list_for_each_entry(channel, &edge->channels, list) {
1299 if (channel->state != SMD_CHANNEL_CLOSED)
1300 continue;
1301
1302 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1303 if (remote_state != SMD_CHANNEL_OPENING &&
1304 remote_state != SMD_CHANNEL_OPENED)
1305 continue;
1306
Bjorn Andersson995b1702016-02-17 22:39:03 -08001307 spin_unlock_irqrestore(&edge->channels_lock, flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001308 qcom_smd_create_device(channel);
Bjorn Andersson995b1702016-02-17 22:39:03 -08001309 spin_lock_irqsave(&edge->channels_lock, flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001310 }
1311
1312 /*
1313 * Unregister the device for any channel that is opened where the
1314 * remote processor is closing the channel.
1315 */
1316 list_for_each_entry(channel, &edge->channels, list) {
1317 if (channel->state != SMD_CHANNEL_OPENING &&
1318 channel->state != SMD_CHANNEL_OPENED)
1319 continue;
1320
1321 remote_state = GET_RX_CHANNEL_INFO(channel, state);
1322 if (remote_state == SMD_CHANNEL_OPENING ||
1323 remote_state == SMD_CHANNEL_OPENED)
1324 continue;
1325
Bjorn Andersson995b1702016-02-17 22:39:03 -08001326 spin_unlock_irqrestore(&edge->channels_lock, flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001327 qcom_smd_destroy_device(channel);
Bjorn Andersson995b1702016-02-17 22:39:03 -08001328 spin_lock_irqsave(&edge->channels_lock, flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001329 }
Bjorn Andersson995b1702016-02-17 22:39:03 -08001330 spin_unlock_irqrestore(&edge->channels_lock, flags);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001331}
1332
1333/*
1334 * Parses an of_node describing an edge.
1335 */
1336static int qcom_smd_parse_edge(struct device *dev,
1337 struct device_node *node,
1338 struct qcom_smd_edge *edge)
1339{
1340 struct device_node *syscon_np;
1341 const char *key;
1342 int irq;
1343 int ret;
1344
1345 INIT_LIST_HEAD(&edge->channels);
1346 spin_lock_init(&edge->channels_lock);
1347
Bjorn Andersson995b1702016-02-17 22:39:03 -08001348 INIT_WORK(&edge->scan_work, qcom_channel_scan_worker);
1349 INIT_WORK(&edge->state_work, qcom_channel_state_worker);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001350
1351 edge->of_node = of_node_get(node);
1352
1353 irq = irq_of_parse_and_map(node, 0);
1354 if (irq < 0) {
1355 dev_err(dev, "required smd interrupt missing\n");
1356 return -EINVAL;
1357 }
1358
1359 ret = devm_request_irq(dev, irq,
1360 qcom_smd_edge_intr, IRQF_TRIGGER_RISING,
1361 node->name, edge);
1362 if (ret) {
1363 dev_err(dev, "failed to request smd irq\n");
1364 return ret;
1365 }
1366
1367 edge->irq = irq;
1368
1369 key = "qcom,smd-edge";
1370 ret = of_property_read_u32(node, key, &edge->edge_id);
1371 if (ret) {
1372 dev_err(dev, "edge missing %s property\n", key);
1373 return -EINVAL;
1374 }
1375
Andy Gross93dbed92015-08-26 14:42:45 -05001376 edge->remote_pid = QCOM_SMEM_HOST_ANY;
1377 key = "qcom,remote-pid";
1378 of_property_read_u32(node, key, &edge->remote_pid);
1379
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001380 syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
1381 if (!syscon_np) {
1382 dev_err(dev, "no qcom,ipc node\n");
1383 return -ENODEV;
1384 }
1385
1386 edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
1387 if (IS_ERR(edge->ipc_regmap))
1388 return PTR_ERR(edge->ipc_regmap);
1389
1390 key = "qcom,ipc";
1391 ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
1392 if (ret < 0) {
1393 dev_err(dev, "no offset in %s\n", key);
1394 return -EINVAL;
1395 }
1396
1397 ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
1398 if (ret < 0) {
1399 dev_err(dev, "no bit in %s\n", key);
1400 return -EINVAL;
1401 }
1402
1403 return 0;
1404}
1405
1406static int qcom_smd_probe(struct platform_device *pdev)
1407{
1408 struct qcom_smd_edge *edge;
1409 struct device_node *node;
1410 struct qcom_smd *smd;
1411 size_t array_size;
1412 int num_edges;
1413 int ret;
1414 int i = 0;
Stephen Boyd1a039642015-09-02 15:46:44 -07001415 void *p;
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001416
1417 /* Wait for smem */
Stephen Boyd1a039642015-09-02 15:46:44 -07001418 p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL);
1419 if (PTR_ERR(p) == -EPROBE_DEFER)
1420 return PTR_ERR(p);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001421
1422 num_edges = of_get_available_child_count(pdev->dev.of_node);
1423 array_size = sizeof(*smd) + num_edges * sizeof(struct qcom_smd_edge);
1424 smd = devm_kzalloc(&pdev->dev, array_size, GFP_KERNEL);
1425 if (!smd)
1426 return -ENOMEM;
1427 smd->dev = &pdev->dev;
1428
1429 smd->num_edges = num_edges;
1430 for_each_available_child_of_node(pdev->dev.of_node, node) {
1431 edge = &smd->edges[i++];
1432 edge->smd = smd;
Bjorn Andersson028021d2016-02-17 22:39:06 -08001433 init_waitqueue_head(&edge->new_channel_event);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001434
1435 ret = qcom_smd_parse_edge(&pdev->dev, node, edge);
1436 if (ret)
1437 continue;
1438
Bjorn Andersson995b1702016-02-17 22:39:03 -08001439 schedule_work(&edge->scan_work);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001440 }
1441
1442 platform_set_drvdata(pdev, smd);
1443
1444 return 0;
1445}
1446
1447/*
1448 * Shut down all smd clients by making sure that each edge stops processing
1449 * events and scanning for new channels, then call destroy on the devices.
1450 */
1451static int qcom_smd_remove(struct platform_device *pdev)
1452{
1453 struct qcom_smd_channel *channel;
1454 struct qcom_smd_edge *edge;
1455 struct qcom_smd *smd = platform_get_drvdata(pdev);
1456 int i;
1457
1458 for (i = 0; i < smd->num_edges; i++) {
1459 edge = &smd->edges[i];
1460
1461 disable_irq(edge->irq);
Bjorn Andersson995b1702016-02-17 22:39:03 -08001462 cancel_work_sync(&edge->scan_work);
1463 cancel_work_sync(&edge->state_work);
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001464
Bjorn Andersson995b1702016-02-17 22:39:03 -08001465 /* No need to lock here, because the writer is gone */
Bjorn Anderssonf2ab3292015-07-27 20:20:30 -07001466 list_for_each_entry(channel, &edge->channels, list) {
1467 if (!channel->qsdev)
1468 continue;
1469
1470 qcom_smd_destroy_device(channel);
1471 }
1472 }
1473
1474 return 0;
1475}
1476
1477static const struct of_device_id qcom_smd_of_match[] = {
1478 { .compatible = "qcom,smd" },
1479 {}
1480};
1481MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
1482
1483static struct platform_driver qcom_smd_driver = {
1484 .probe = qcom_smd_probe,
1485 .remove = qcom_smd_remove,
1486 .driver = {
1487 .name = "qcom-smd",
1488 .of_match_table = qcom_smd_of_match,
1489 },
1490};
1491
1492static int __init qcom_smd_init(void)
1493{
1494 int ret;
1495
1496 ret = bus_register(&qcom_smd_bus);
1497 if (ret) {
1498 pr_err("failed to register smd bus: %d\n", ret);
1499 return ret;
1500 }
1501
1502 return platform_driver_register(&qcom_smd_driver);
1503}
1504postcore_initcall(qcom_smd_init);
1505
1506static void __exit qcom_smd_exit(void)
1507{
1508 platform_driver_unregister(&qcom_smd_driver);
1509 bus_unregister(&qcom_smd_bus);
1510}
1511module_exit(qcom_smd_exit);
1512
1513MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
1514MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
1515MODULE_LICENSE("GPL v2");