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