2 * Copyright (c) 2016-2017, Linaro Ltd
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/idr.h>
15 #include <linux/interrupt.h>
17 #include <linux/list.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/module.h>
21 #include <linux/of_address.h>
22 #include <linux/of_irq.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/rpmsg.h>
26 #include <linux/sizes.h>
27 #include <linux/slab.h>
28 #include <linux/workqueue.h>
29 #include <linux/mailbox_client.h>
31 #include "rpmsg_internal.h"
32 #include "qcom_glink_native.h"
34 #define GLINK_NAME_SIZE 32
35 #define GLINK_VERSION_1 1
37 #define RPM_GLINK_CID_MIN 1
38 #define RPM_GLINK_CID_MAX 65536
48 * struct glink_defer_cmd - deferred incoming control message
50 * @msg: message header
51 * data: payload of the message
53 * Copy of a received control message, to be added to @rx_queue and processed
54 * by @rx_work of @qcom_glink.
56 struct glink_defer_cmd
{
57 struct list_head node
;
64 * struct glink_core_rx_intent - RX intent
67 * data: pointer to the data (may be NULL for zero-copy)
68 * id: remote or local intent ID
69 * size: size of the original intent (do not modify)
70 * reuse: To mark if the intent can be reused after first use
71 * in_use: To mark if intent is already in use for the channel
72 * offset: next write offset (initially 0)
74 struct glink_core_rx_intent
{
82 struct list_head node
;
86 * struct qcom_glink - driver context, relates to one remote subsystem
87 * @dev: reference to the associated struct device
88 * @mbox_client: mailbox client
89 * @mbox_chan: mailbox channel
90 * @rx_pipe: pipe object for receive FIFO
91 * @tx_pipe: pipe object for transmit FIFO
92 * @irq: IRQ for signaling incoming events
93 * @rx_work: worker for handling received control messages
94 * @rx_lock: protects the @rx_queue
95 * @rx_queue: queue of received control messages to be processed in @rx_work
96 * @tx_lock: synchronizes operations on the tx fifo
97 * @idr_lock: synchronizes @lcids and @rcids modifications
98 * @lcids: idr of all channels with a known local channel id
99 * @rcids: idr of all channels with a known remote channel id
104 struct mbox_client mbox_client
;
105 struct mbox_chan
*mbox_chan
;
107 struct qcom_glink_pipe
*rx_pipe
;
108 struct qcom_glink_pipe
*tx_pipe
;
112 struct work_struct rx_work
;
114 struct list_head rx_queue
;
116 struct mutex tx_lock
;
121 unsigned long features
;
134 * struct glink_channel - internal representation of a channel
135 * @rpdev: rpdev reference, only used for primary endpoints
136 * @ept: rpmsg endpoint this channel is associated with
137 * @glink: qcom_glink context handle
138 * @refcount: refcount for the channel object
139 * @recv_lock: guard for @ept.cb
140 * @name: unique channel name/identifier
141 * @lcid: channel id, in local space
142 * @rcid: channel id, in remote space
143 * @intent_lock: lock for protection of @liids, @riids
144 * @liids: idr of all local intents
145 * @riids: idr of all remote intents
146 * @intent_work: worker responsible for transmitting rx_done packets
147 * @done_intents: list of intents that needs to be announced rx_done
148 * @buf: receive buffer, for gathering fragments
149 * @buf_offset: write offset in @buf
150 * @buf_size: size of current @buf
151 * @open_ack: completed once remote has acked the open-request
152 * @open_req: completed once open-request has been received
153 * @intent_req_lock: Synchronises multiple intent requests
154 * @intent_req_result: Result of intent request
155 * @intent_req_comp: Completion for intent_req signalling
157 struct glink_channel
{
158 struct rpmsg_endpoint ept
;
160 struct rpmsg_device
*rpdev
;
161 struct qcom_glink
*glink
;
163 struct kref refcount
;
165 spinlock_t recv_lock
;
171 spinlock_t intent_lock
;
174 struct work_struct intent_work
;
175 struct list_head done_intents
;
177 struct glink_core_rx_intent
*buf
;
181 struct completion open_ack
;
182 struct completion open_req
;
184 struct mutex intent_req_lock
;
185 bool intent_req_result
;
186 struct completion intent_req_comp
;
189 #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
191 static const struct rpmsg_endpoint_ops glink_endpoint_ops
;
193 #define RPM_CMD_VERSION 0
194 #define RPM_CMD_VERSION_ACK 1
195 #define RPM_CMD_OPEN 2
196 #define RPM_CMD_CLOSE 3
197 #define RPM_CMD_OPEN_ACK 4
198 #define RPM_CMD_INTENT 5
199 #define RPM_CMD_RX_DONE 6
200 #define RPM_CMD_RX_INTENT_REQ 7
201 #define RPM_CMD_RX_INTENT_REQ_ACK 8
202 #define RPM_CMD_TX_DATA 9
203 #define RPM_CMD_CLOSE_ACK 11
204 #define RPM_CMD_TX_DATA_CONT 12
205 #define RPM_CMD_READ_NOTIF 13
206 #define RPM_CMD_RX_DONE_W_REUSE 14
208 #define GLINK_FEATURE_INTENTLESS BIT(1)
210 static void qcom_glink_rx_done_work(struct work_struct
*work
);
212 static struct glink_channel
*qcom_glink_alloc_channel(struct qcom_glink
*glink
,
215 struct glink_channel
*channel
;
217 channel
= kzalloc(sizeof(*channel
), GFP_KERNEL
);
219 return ERR_PTR(-ENOMEM
);
221 /* Setup glink internal glink_channel data */
222 spin_lock_init(&channel
->recv_lock
);
223 spin_lock_init(&channel
->intent_lock
);
225 channel
->glink
= glink
;
226 channel
->name
= kstrdup(name
, GFP_KERNEL
);
228 init_completion(&channel
->open_req
);
229 init_completion(&channel
->open_ack
);
231 INIT_LIST_HEAD(&channel
->done_intents
);
232 INIT_WORK(&channel
->intent_work
, qcom_glink_rx_done_work
);
234 idr_init(&channel
->liids
);
235 idr_init(&channel
->riids
);
236 kref_init(&channel
->refcount
);
241 static void qcom_glink_channel_release(struct kref
*ref
)
243 struct glink_channel
*channel
= container_of(ref
, struct glink_channel
,
247 spin_lock_irqsave(&channel
->intent_lock
, flags
);
248 idr_destroy(&channel
->liids
);
249 idr_destroy(&channel
->riids
);
250 spin_unlock_irqrestore(&channel
->intent_lock
, flags
);
252 kfree(channel
->name
);
256 static size_t qcom_glink_rx_avail(struct qcom_glink
*glink
)
258 return glink
->rx_pipe
->avail(glink
->rx_pipe
);
261 static void qcom_glink_rx_peak(struct qcom_glink
*glink
,
262 void *data
, unsigned int offset
, size_t count
)
264 glink
->rx_pipe
->peak(glink
->rx_pipe
, data
, offset
, count
);
267 static void qcom_glink_rx_advance(struct qcom_glink
*glink
, size_t count
)
269 glink
->rx_pipe
->advance(glink
->rx_pipe
, count
);
272 static size_t qcom_glink_tx_avail(struct qcom_glink
*glink
)
274 return glink
->tx_pipe
->avail(glink
->tx_pipe
);
277 static void qcom_glink_tx_write(struct qcom_glink
*glink
,
278 const void *hdr
, size_t hlen
,
279 const void *data
, size_t dlen
)
281 glink
->tx_pipe
->write(glink
->tx_pipe
, hdr
, hlen
, data
, dlen
);
284 static int qcom_glink_tx(struct qcom_glink
*glink
,
285 const void *hdr
, size_t hlen
,
286 const void *data
, size_t dlen
, bool wait
)
288 unsigned int tlen
= hlen
+ dlen
;
291 /* Reject packets that are too big */
292 if (tlen
>= glink
->tx_pipe
->length
)
295 ret
= mutex_lock_interruptible(&glink
->tx_lock
);
299 while (qcom_glink_tx_avail(glink
) < tlen
) {
305 usleep_range(10000, 15000);
308 qcom_glink_tx_write(glink
, hdr
, hlen
, data
, dlen
);
310 mbox_send_message(glink
->mbox_chan
, NULL
);
311 mbox_client_txdone(glink
->mbox_chan
, 0);
314 mutex_unlock(&glink
->tx_lock
);
319 static int qcom_glink_send_version(struct qcom_glink
*glink
)
321 struct glink_msg msg
;
323 msg
.cmd
= cpu_to_le16(RPM_CMD_VERSION
);
324 msg
.param1
= cpu_to_le16(GLINK_VERSION_1
);
325 msg
.param2
= cpu_to_le32(glink
->features
);
327 return qcom_glink_tx(glink
, &msg
, sizeof(msg
), NULL
, 0, true);
330 static void qcom_glink_send_version_ack(struct qcom_glink
*glink
)
332 struct glink_msg msg
;
334 msg
.cmd
= cpu_to_le16(RPM_CMD_VERSION_ACK
);
335 msg
.param1
= cpu_to_le16(GLINK_VERSION_1
);
336 msg
.param2
= cpu_to_le32(glink
->features
);
338 qcom_glink_tx(glink
, &msg
, sizeof(msg
), NULL
, 0, true);
341 static void qcom_glink_send_open_ack(struct qcom_glink
*glink
,
342 struct glink_channel
*channel
)
344 struct glink_msg msg
;
346 msg
.cmd
= cpu_to_le16(RPM_CMD_OPEN_ACK
);
347 msg
.param1
= cpu_to_le16(channel
->rcid
);
348 msg
.param2
= cpu_to_le32(0);
350 qcom_glink_tx(glink
, &msg
, sizeof(msg
), NULL
, 0, true);
353 static void qcom_glink_handle_intent_req_ack(struct qcom_glink
*glink
,
354 unsigned int cid
, bool granted
)
356 struct glink_channel
*channel
;
359 spin_lock_irqsave(&glink
->idr_lock
, flags
);
360 channel
= idr_find(&glink
->rcids
, cid
);
361 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
363 dev_err(glink
->dev
, "unable to find channel\n");
367 channel
->intent_req_result
= granted
;
368 complete(&channel
->intent_req_comp
);
372 * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
373 * @glink: Ptr to the glink edge
374 * @channel: Ptr to the channel that the open req is sent
376 * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
377 * Will return with refcount held, regardless of outcome.
379 * Returns 0 on success, negative errno otherwise.
381 static int qcom_glink_send_open_req(struct qcom_glink
*glink
,
382 struct glink_channel
*channel
)
385 struct glink_msg msg
;
386 u8 name
[GLINK_NAME_SIZE
];
388 int name_len
= strlen(channel
->name
) + 1;
389 int req_len
= ALIGN(sizeof(req
.msg
) + name_len
, 8);
393 kref_get(&channel
->refcount
);
395 spin_lock_irqsave(&glink
->idr_lock
, flags
);
396 ret
= idr_alloc_cyclic(&glink
->lcids
, channel
,
397 RPM_GLINK_CID_MIN
, RPM_GLINK_CID_MAX
,
399 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
405 req
.msg
.cmd
= cpu_to_le16(RPM_CMD_OPEN
);
406 req
.msg
.param1
= cpu_to_le16(channel
->lcid
);
407 req
.msg
.param2
= cpu_to_le32(name_len
);
408 strcpy(req
.name
, channel
->name
);
410 ret
= qcom_glink_tx(glink
, &req
, req_len
, NULL
, 0, true);
417 spin_lock_irqsave(&glink
->idr_lock
, flags
);
418 idr_remove(&glink
->lcids
, channel
->lcid
);
420 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
425 static void qcom_glink_send_close_req(struct qcom_glink
*glink
,
426 struct glink_channel
*channel
)
428 struct glink_msg req
;
430 req
.cmd
= cpu_to_le16(RPM_CMD_CLOSE
);
431 req
.param1
= cpu_to_le16(channel
->lcid
);
434 qcom_glink_tx(glink
, &req
, sizeof(req
), NULL
, 0, true);
437 static void qcom_glink_send_close_ack(struct qcom_glink
*glink
,
440 struct glink_msg req
;
442 req
.cmd
= cpu_to_le16(RPM_CMD_CLOSE_ACK
);
443 req
.param1
= cpu_to_le16(rcid
);
446 qcom_glink_tx(glink
, &req
, sizeof(req
), NULL
, 0, true);
449 static void qcom_glink_rx_done_work(struct work_struct
*work
)
451 struct glink_channel
*channel
= container_of(work
, struct glink_channel
,
453 struct qcom_glink
*glink
= channel
->glink
;
454 struct glink_core_rx_intent
*intent
, *tmp
;
461 unsigned int cid
= channel
->lcid
;
466 spin_lock_irqsave(&channel
->intent_lock
, flags
);
467 list_for_each_entry_safe(intent
, tmp
, &channel
->done_intents
, node
) {
468 list_del(&intent
->node
);
469 spin_unlock_irqrestore(&channel
->intent_lock
, flags
);
471 reuse
= intent
->reuse
;
473 cmd
.id
= reuse
? RPM_CMD_RX_DONE_W_REUSE
: RPM_CMD_RX_DONE
;
477 qcom_glink_tx(glink
, &cmd
, sizeof(cmd
), NULL
, 0, true);
482 spin_lock_irqsave(&channel
->intent_lock
, flags
);
484 spin_unlock_irqrestore(&channel
->intent_lock
, flags
);
487 static void qcom_glink_rx_done(struct qcom_glink
*glink
,
488 struct glink_channel
*channel
,
489 struct glink_core_rx_intent
*intent
)
491 /* We don't send RX_DONE to intentless systems */
492 if (glink
->intentless
) {
498 /* Take it off the tree of receive intents */
499 if (!intent
->reuse
) {
500 spin_lock(&channel
->intent_lock
);
501 idr_remove(&channel
->liids
, intent
->id
);
502 spin_unlock(&channel
->intent_lock
);
505 /* Schedule the sending of a rx_done indication */
506 spin_lock(&channel
->intent_lock
);
507 list_add_tail(&intent
->node
, &channel
->done_intents
);
508 spin_unlock(&channel
->intent_lock
);
510 schedule_work(&channel
->intent_work
);
514 * qcom_glink_receive_version() - receive version/features from remote system
516 * @glink: pointer to transport interface
517 * @r_version: remote version
518 * @r_features: remote features
520 * This function is called in response to a remote-initiated version/feature
521 * negotiation sequence.
523 static void qcom_glink_receive_version(struct qcom_glink
*glink
,
530 case GLINK_VERSION_1
:
531 glink
->features
&= features
;
534 qcom_glink_send_version_ack(glink
);
540 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
542 * @glink: pointer to transport interface
543 * @r_version: remote version response
544 * @r_features: remote features response
546 * This function is called in response to a local-initiated version/feature
547 * negotiation sequence and is the counter-offer from the remote side based
548 * upon the initial version and feature set requested.
550 static void qcom_glink_receive_version_ack(struct qcom_glink
*glink
,
556 /* Version negotiation failed */
558 case GLINK_VERSION_1
:
559 if (features
== glink
->features
)
562 glink
->features
&= features
;
565 qcom_glink_send_version(glink
);
571 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
572 wire format and transmit
573 * @glink: The transport to transmit on.
574 * @channel: The glink channel
575 * @granted: The request response to encode.
577 * Return: 0 on success or standard Linux error code.
579 static int qcom_glink_send_intent_req_ack(struct qcom_glink
*glink
,
580 struct glink_channel
*channel
,
583 struct glink_msg msg
;
585 msg
.cmd
= cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK
);
586 msg
.param1
= cpu_to_le16(channel
->lcid
);
587 msg
.param2
= cpu_to_le32(granted
);
589 qcom_glink_tx(glink
, &msg
, sizeof(msg
), NULL
, 0, true);
595 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
597 * @glink: The transport to transmit on.
598 * @channel: The local channel
599 * @size: The intent to pass on to remote.
601 * Return: 0 on success or standard Linux error code.
603 static int qcom_glink_advertise_intent(struct qcom_glink
*glink
,
604 struct glink_channel
*channel
,
605 struct glink_core_rx_intent
*intent
)
616 cmd
.id
= cpu_to_le16(RPM_CMD_INTENT
);
617 cmd
.lcid
= cpu_to_le16(channel
->lcid
);
618 cmd
.count
= cpu_to_le32(1);
619 cmd
.size
= cpu_to_le32(intent
->size
);
620 cmd
.liid
= cpu_to_le32(intent
->id
);
622 qcom_glink_tx(glink
, &cmd
, sizeof(cmd
), NULL
, 0, true);
627 static struct glink_core_rx_intent
*
628 qcom_glink_alloc_intent(struct qcom_glink
*glink
,
629 struct glink_channel
*channel
,
633 struct glink_core_rx_intent
*intent
;
637 intent
= kzalloc(sizeof(*intent
), GFP_KERNEL
);
642 intent
->data
= kzalloc(size
, GFP_KERNEL
);
646 spin_lock_irqsave(&channel
->intent_lock
, flags
);
647 ret
= idr_alloc_cyclic(&channel
->liids
, intent
, 1, -1, GFP_ATOMIC
);
649 spin_unlock_irqrestore(&channel
->intent_lock
, flags
);
652 spin_unlock_irqrestore(&channel
->intent_lock
, flags
);
656 intent
->reuse
= reuseable
;
661 static void qcom_glink_handle_rx_done(struct qcom_glink
*glink
,
662 u32 cid
, uint32_t iid
,
665 struct glink_core_rx_intent
*intent
;
666 struct glink_channel
*channel
;
669 spin_lock_irqsave(&glink
->idr_lock
, flags
);
670 channel
= idr_find(&glink
->rcids
, cid
);
671 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
673 dev_err(glink
->dev
, "invalid channel id received\n");
677 spin_lock_irqsave(&channel
->intent_lock
, flags
);
678 intent
= idr_find(&channel
->riids
, iid
);
681 spin_unlock_irqrestore(&channel
->intent_lock
, flags
);
682 dev_err(glink
->dev
, "invalid intent id received\n");
686 intent
->in_use
= false;
689 idr_remove(&channel
->riids
, intent
->id
);
692 spin_unlock_irqrestore(&channel
->intent_lock
, flags
);
696 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
698 * if_ptr: Pointer to the transport interface
699 * rcid: Remote channel ID
700 * size: size of the intent
702 * The function searches for the local channel to which the request for
703 * rx_intent has arrived and allocates and notifies the remote back
705 static void qcom_glink_handle_intent_req(struct qcom_glink
*glink
,
706 u32 cid
, size_t size
)
708 struct glink_core_rx_intent
*intent
;
709 struct glink_channel
*channel
;
712 spin_lock_irqsave(&glink
->idr_lock
, flags
);
713 channel
= idr_find(&glink
->rcids
, cid
);
714 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
717 pr_err("%s channel not found for cid %d\n", __func__
, cid
);
721 intent
= qcom_glink_alloc_intent(glink
, channel
, size
, false);
723 qcom_glink_advertise_intent(glink
, channel
, intent
);
725 qcom_glink_send_intent_req_ack(glink
, channel
, !!intent
);
728 static int qcom_glink_rx_defer(struct qcom_glink
*glink
, size_t extra
)
730 struct glink_defer_cmd
*dcmd
;
732 extra
= ALIGN(extra
, 8);
734 if (qcom_glink_rx_avail(glink
) < sizeof(struct glink_msg
) + extra
) {
735 dev_dbg(glink
->dev
, "Insufficient data in rx fifo");
739 dcmd
= kzalloc(sizeof(*dcmd
) + extra
, GFP_ATOMIC
);
743 INIT_LIST_HEAD(&dcmd
->node
);
745 qcom_glink_rx_peak(glink
, &dcmd
->msg
, 0, sizeof(dcmd
->msg
) + extra
);
747 spin_lock(&glink
->rx_lock
);
748 list_add_tail(&dcmd
->node
, &glink
->rx_queue
);
749 spin_unlock(&glink
->rx_lock
);
751 schedule_work(&glink
->rx_work
);
752 qcom_glink_rx_advance(glink
, sizeof(dcmd
->msg
) + extra
);
757 static int qcom_glink_rx_data(struct qcom_glink
*glink
, size_t avail
)
759 struct glink_core_rx_intent
*intent
;
760 struct glink_channel
*channel
;
762 struct glink_msg msg
;
766 unsigned int chunk_size
;
767 unsigned int left_size
;
773 if (avail
< sizeof(hdr
)) {
774 dev_dbg(glink
->dev
, "Not enough data in fifo\n");
778 qcom_glink_rx_peak(glink
, &hdr
, 0, sizeof(hdr
));
779 chunk_size
= le32_to_cpu(hdr
.chunk_size
);
780 left_size
= le32_to_cpu(hdr
.left_size
);
782 if (avail
< sizeof(hdr
) + chunk_size
) {
783 dev_dbg(glink
->dev
, "Payload not yet in fifo\n");
787 if (WARN(chunk_size
% 4, "Incoming data must be word aligned\n"))
790 rcid
= le16_to_cpu(hdr
.msg
.param1
);
791 spin_lock_irqsave(&glink
->idr_lock
, flags
);
792 channel
= idr_find(&glink
->rcids
, rcid
);
793 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
795 dev_dbg(glink
->dev
, "Data on non-existing channel\n");
797 /* Drop the message */
801 if (glink
->intentless
) {
802 /* Might have an ongoing, fragmented, message to append */
804 intent
= kzalloc(sizeof(*intent
), GFP_ATOMIC
);
808 intent
->data
= kmalloc(chunk_size
+ left_size
,
815 intent
->id
= 0xbabababa;
816 intent
->size
= chunk_size
+ left_size
;
819 channel
->buf
= intent
;
821 intent
= channel
->buf
;
824 liid
= le32_to_cpu(hdr
.msg
.param2
);
826 spin_lock_irqsave(&channel
->intent_lock
, flags
);
827 intent
= idr_find(&channel
->liids
, liid
);
828 spin_unlock_irqrestore(&channel
->intent_lock
, flags
);
832 "no intent found for channel %s intent %d",
833 channel
->name
, liid
);
838 if (intent
->size
- intent
->offset
< chunk_size
) {
839 dev_err(glink
->dev
, "Insufficient space in intent\n");
841 /* The packet header lied, drop payload */
845 qcom_glink_rx_peak(glink
, intent
->data
+ intent
->offset
,
846 sizeof(hdr
), chunk_size
);
847 intent
->offset
+= chunk_size
;
849 /* Handle message when no fragments remain to be received */
851 spin_lock(&channel
->recv_lock
);
852 if (channel
->ept
.cb
) {
853 channel
->ept
.cb(channel
->ept
.rpdev
,
859 spin_unlock(&channel
->recv_lock
);
864 qcom_glink_rx_done(glink
, channel
, intent
);
868 qcom_glink_rx_advance(glink
, ALIGN(sizeof(hdr
) + chunk_size
, 8));
873 static void qcom_glink_handle_intent(struct qcom_glink
*glink
,
878 struct glink_core_rx_intent
*intent
;
879 struct glink_channel
*channel
;
886 struct glink_msg msg
;
887 struct intent_pair intents
[];
890 const size_t msglen
= sizeof(*msg
) + sizeof(struct intent_pair
) * count
;
895 if (avail
< msglen
) {
896 dev_dbg(glink
->dev
, "Not enough data in fifo\n");
900 spin_lock_irqsave(&glink
->idr_lock
, flags
);
901 channel
= idr_find(&glink
->rcids
, cid
);
902 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
904 dev_err(glink
->dev
, "intents for non-existing channel\n");
908 msg
= kmalloc(msglen
, GFP_ATOMIC
);
912 qcom_glink_rx_peak(glink
, msg
, 0, msglen
);
914 for (i
= 0; i
< count
; ++i
) {
915 intent
= kzalloc(sizeof(*intent
), GFP_ATOMIC
);
919 intent
->id
= le32_to_cpu(msg
->intents
[i
].iid
);
920 intent
->size
= le32_to_cpu(msg
->intents
[i
].size
);
922 spin_lock_irqsave(&channel
->intent_lock
, flags
);
923 ret
= idr_alloc(&channel
->riids
, intent
,
924 intent
->id
, intent
->id
+ 1, GFP_ATOMIC
);
925 spin_unlock_irqrestore(&channel
->intent_lock
, flags
);
928 dev_err(glink
->dev
, "failed to store remote intent\n");
932 qcom_glink_rx_advance(glink
, ALIGN(msglen
, 8));
935 static int qcom_glink_rx_open_ack(struct qcom_glink
*glink
, unsigned int lcid
)
937 struct glink_channel
*channel
;
939 spin_lock(&glink
->idr_lock
);
940 channel
= idr_find(&glink
->lcids
, lcid
);
941 spin_unlock(&glink
->idr_lock
);
943 dev_err(glink
->dev
, "Invalid open ack packet\n");
947 complete(&channel
->open_ack
);
952 static irqreturn_t
qcom_glink_native_intr(int irq
, void *data
)
954 struct qcom_glink
*glink
= data
;
955 struct glink_msg msg
;
963 avail
= qcom_glink_rx_avail(glink
);
964 if (avail
< sizeof(msg
))
967 qcom_glink_rx_peak(glink
, &msg
, 0, sizeof(msg
));
969 cmd
= le16_to_cpu(msg
.cmd
);
970 param1
= le16_to_cpu(msg
.param1
);
971 param2
= le32_to_cpu(msg
.param2
);
974 case RPM_CMD_VERSION
:
975 case RPM_CMD_VERSION_ACK
:
977 case RPM_CMD_CLOSE_ACK
:
978 case RPM_CMD_RX_INTENT_REQ
:
979 ret
= qcom_glink_rx_defer(glink
, 0);
981 case RPM_CMD_OPEN_ACK
:
982 ret
= qcom_glink_rx_open_ack(glink
, param1
);
983 qcom_glink_rx_advance(glink
, ALIGN(sizeof(msg
), 8));
986 ret
= qcom_glink_rx_defer(glink
, param2
);
988 case RPM_CMD_TX_DATA
:
989 case RPM_CMD_TX_DATA_CONT
:
990 ret
= qcom_glink_rx_data(glink
, avail
);
992 case RPM_CMD_READ_NOTIF
:
993 qcom_glink_rx_advance(glink
, ALIGN(sizeof(msg
), 8));
995 mbox_send_message(glink
->mbox_chan
, NULL
);
996 mbox_client_txdone(glink
->mbox_chan
, 0);
999 qcom_glink_handle_intent(glink
, param1
, param2
, avail
);
1001 case RPM_CMD_RX_DONE
:
1002 qcom_glink_handle_rx_done(glink
, param1
, param2
, false);
1003 qcom_glink_rx_advance(glink
, ALIGN(sizeof(msg
), 8));
1005 case RPM_CMD_RX_DONE_W_REUSE
:
1006 qcom_glink_handle_rx_done(glink
, param1
, param2
, true);
1007 qcom_glink_rx_advance(glink
, ALIGN(sizeof(msg
), 8));
1009 case RPM_CMD_RX_INTENT_REQ_ACK
:
1010 qcom_glink_handle_intent_req_ack(glink
, param1
, param2
);
1011 qcom_glink_rx_advance(glink
, ALIGN(sizeof(msg
), 8));
1014 dev_err(glink
->dev
, "unhandled rx cmd: %d\n", cmd
);
1026 /* Locally initiated rpmsg_create_ept */
1027 static struct glink_channel
*qcom_glink_create_local(struct qcom_glink
*glink
,
1030 struct glink_channel
*channel
;
1032 unsigned long flags
;
1034 channel
= qcom_glink_alloc_channel(glink
, name
);
1035 if (IS_ERR(channel
))
1036 return ERR_CAST(channel
);
1038 ret
= qcom_glink_send_open_req(glink
, channel
);
1040 goto release_channel
;
1042 ret
= wait_for_completion_timeout(&channel
->open_ack
, 5 * HZ
);
1046 ret
= wait_for_completion_timeout(&channel
->open_req
, 5 * HZ
);
1050 qcom_glink_send_open_ack(glink
, channel
);
1055 /* qcom_glink_send_open_req() did register the channel in lcids*/
1056 spin_lock_irqsave(&glink
->idr_lock
, flags
);
1057 idr_remove(&glink
->lcids
, channel
->lcid
);
1058 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
1061 /* Release qcom_glink_send_open_req() reference */
1062 kref_put(&channel
->refcount
, qcom_glink_channel_release
);
1063 /* Release qcom_glink_alloc_channel() reference */
1064 kref_put(&channel
->refcount
, qcom_glink_channel_release
);
1066 return ERR_PTR(-ETIMEDOUT
);
1069 /* Remote initiated rpmsg_create_ept */
1070 static int qcom_glink_create_remote(struct qcom_glink
*glink
,
1071 struct glink_channel
*channel
)
1075 qcom_glink_send_open_ack(glink
, channel
);
1077 ret
= qcom_glink_send_open_req(glink
, channel
);
1081 ret
= wait_for_completion_timeout(&channel
->open_ack
, 5 * HZ
);
1091 * Send a close request to "undo" our open-ack. The close-ack will
1092 * release the last reference.
1094 qcom_glink_send_close_req(glink
, channel
);
1096 /* Release qcom_glink_send_open_req() reference */
1097 kref_put(&channel
->refcount
, qcom_glink_channel_release
);
1102 static struct rpmsg_endpoint
*qcom_glink_create_ept(struct rpmsg_device
*rpdev
,
1105 struct rpmsg_channel_info
1108 struct glink_channel
*parent
= to_glink_channel(rpdev
->ept
);
1109 struct glink_channel
*channel
;
1110 struct qcom_glink
*glink
= parent
->glink
;
1111 struct rpmsg_endpoint
*ept
;
1112 const char *name
= chinfo
.name
;
1115 unsigned long flags
;
1117 spin_lock_irqsave(&glink
->idr_lock
, flags
);
1118 idr_for_each_entry(&glink
->rcids
, channel
, cid
) {
1119 if (!strcmp(channel
->name
, name
))
1122 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
1125 channel
= qcom_glink_create_local(glink
, name
);
1126 if (IS_ERR(channel
))
1129 ret
= qcom_glink_create_remote(glink
, channel
);
1134 ept
= &channel
->ept
;
1138 ept
->ops
= &glink_endpoint_ops
;
1143 static int qcom_glink_announce_create(struct rpmsg_device
*rpdev
)
1145 struct glink_channel
*channel
= to_glink_channel(rpdev
->ept
);
1146 struct glink_core_rx_intent
*intent
;
1147 struct qcom_glink
*glink
= channel
->glink
;
1148 int num_intents
= glink
->intentless
? 0 : 5;
1150 /* Channel is now open, advertise base set of intents */
1151 while (num_intents
--) {
1152 intent
= qcom_glink_alloc_intent(glink
, channel
, SZ_1K
, true);
1156 qcom_glink_advertise_intent(glink
, channel
, intent
);
1162 static void qcom_glink_destroy_ept(struct rpmsg_endpoint
*ept
)
1164 struct glink_channel
*channel
= to_glink_channel(ept
);
1165 struct qcom_glink
*glink
= channel
->glink
;
1166 unsigned long flags
;
1168 spin_lock_irqsave(&channel
->recv_lock
, flags
);
1169 channel
->ept
.cb
= NULL
;
1170 spin_unlock_irqrestore(&channel
->recv_lock
, flags
);
1172 /* Decouple the potential rpdev from the channel */
1173 channel
->rpdev
= NULL
;
1175 qcom_glink_send_close_req(glink
, channel
);
1178 static int qcom_glink_request_intent(struct qcom_glink
*glink
,
1179 struct glink_channel
*channel
,
1190 mutex_lock(&channel
->intent_req_lock
);
1192 reinit_completion(&channel
->intent_req_comp
);
1194 cmd
.id
= RPM_CMD_RX_INTENT_REQ
;
1195 cmd
.cid
= channel
->lcid
;
1198 ret
= qcom_glink_tx(glink
, &cmd
, sizeof(cmd
), NULL
, 0, true);
1202 ret
= wait_for_completion_timeout(&channel
->intent_req_comp
, 10 * HZ
);
1204 dev_err(glink
->dev
, "intent request timed out\n");
1207 ret
= channel
->intent_req_result
? 0 : -ECANCELED
;
1210 mutex_unlock(&channel
->intent_req_lock
);
1214 static int __qcom_glink_send(struct glink_channel
*channel
,
1215 void *data
, int len
, bool wait
)
1217 struct qcom_glink
*glink
= channel
->glink
;
1218 struct glink_core_rx_intent
*intent
= NULL
;
1219 struct glink_core_rx_intent
*tmp
;
1222 struct glink_msg msg
;
1227 unsigned long flags
;
1229 if (!glink
->intentless
) {
1231 spin_lock_irqsave(&channel
->intent_lock
, flags
);
1232 idr_for_each_entry(&channel
->riids
, tmp
, iid
) {
1233 if (tmp
->size
>= len
&& !tmp
->in_use
) {
1239 spin_unlock_irqrestore(&channel
->intent_lock
, flags
);
1241 /* We found an available intent */
1248 ret
= qcom_glink_request_intent(glink
, channel
, len
);
1256 req
.msg
.cmd
= cpu_to_le16(RPM_CMD_TX_DATA
);
1257 req
.msg
.param1
= cpu_to_le16(channel
->lcid
);
1258 req
.msg
.param2
= cpu_to_le32(iid
);
1259 req
.chunk_size
= cpu_to_le32(len
);
1260 req
.left_size
= cpu_to_le32(0);
1262 ret
= qcom_glink_tx(glink
, &req
, sizeof(req
), data
, len
, wait
);
1264 /* Mark intent available if we failed */
1266 intent
->in_use
= false;
1271 static int qcom_glink_send(struct rpmsg_endpoint
*ept
, void *data
, int len
)
1273 struct glink_channel
*channel
= to_glink_channel(ept
);
1275 return __qcom_glink_send(channel
, data
, len
, true);
1278 static int qcom_glink_trysend(struct rpmsg_endpoint
*ept
, void *data
, int len
)
1280 struct glink_channel
*channel
= to_glink_channel(ept
);
1282 return __qcom_glink_send(channel
, data
, len
, false);
1286 * Finds the device_node for the glink child interested in this channel.
1288 static struct device_node
*qcom_glink_match_channel(struct device_node
*node
,
1289 const char *channel
)
1291 struct device_node
*child
;
1296 for_each_available_child_of_node(node
, child
) {
1297 key
= "qcom,glink-channels";
1298 ret
= of_property_read_string(child
, key
, &name
);
1302 if (strcmp(name
, channel
) == 0)
1309 static const struct rpmsg_device_ops glink_device_ops
= {
1310 .create_ept
= qcom_glink_create_ept
,
1311 .announce_create
= qcom_glink_announce_create
,
1314 static const struct rpmsg_endpoint_ops glink_endpoint_ops
= {
1315 .destroy_ept
= qcom_glink_destroy_ept
,
1316 .send
= qcom_glink_send
,
1317 .trysend
= qcom_glink_trysend
,
1320 static void qcom_glink_rpdev_release(struct device
*dev
)
1322 struct rpmsg_device
*rpdev
= to_rpmsg_device(dev
);
1323 struct glink_channel
*channel
= to_glink_channel(rpdev
->ept
);
1325 channel
->rpdev
= NULL
;
1329 static int qcom_glink_rx_open(struct qcom_glink
*glink
, unsigned int rcid
,
1332 struct glink_channel
*channel
;
1333 struct rpmsg_device
*rpdev
;
1334 bool create_device
= false;
1335 struct device_node
*node
;
1338 unsigned long flags
;
1340 spin_lock_irqsave(&glink
->idr_lock
, flags
);
1341 idr_for_each_entry(&glink
->lcids
, channel
, lcid
) {
1342 if (!strcmp(channel
->name
, name
))
1345 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
1348 channel
= qcom_glink_alloc_channel(glink
, name
);
1349 if (IS_ERR(channel
))
1350 return PTR_ERR(channel
);
1352 /* The opening dance was initiated by the remote */
1353 create_device
= true;
1356 spin_lock_irqsave(&glink
->idr_lock
, flags
);
1357 ret
= idr_alloc(&glink
->rcids
, channel
, rcid
, rcid
+ 1, GFP_ATOMIC
);
1359 dev_err(glink
->dev
, "Unable to insert channel into rcid list\n");
1360 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
1363 channel
->rcid
= ret
;
1364 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
1366 complete(&channel
->open_req
);
1368 if (create_device
) {
1369 rpdev
= kzalloc(sizeof(*rpdev
), GFP_KERNEL
);
1375 rpdev
->ept
= &channel
->ept
;
1376 strncpy(rpdev
->id
.name
, name
, RPMSG_NAME_SIZE
);
1377 rpdev
->src
= RPMSG_ADDR_ANY
;
1378 rpdev
->dst
= RPMSG_ADDR_ANY
;
1379 rpdev
->ops
= &glink_device_ops
;
1381 node
= qcom_glink_match_channel(glink
->dev
->of_node
, name
);
1382 rpdev
->dev
.of_node
= node
;
1383 rpdev
->dev
.parent
= glink
->dev
;
1384 rpdev
->dev
.release
= qcom_glink_rpdev_release
;
1386 ret
= rpmsg_register_device(rpdev
);
1390 channel
->rpdev
= rpdev
;
1398 spin_lock_irqsave(&glink
->idr_lock
, flags
);
1399 idr_remove(&glink
->rcids
, channel
->rcid
);
1401 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
1403 /* Release the reference, iff we took it */
1405 kref_put(&channel
->refcount
, qcom_glink_channel_release
);
1410 static void qcom_glink_rx_close(struct qcom_glink
*glink
, unsigned int rcid
)
1412 struct rpmsg_channel_info chinfo
;
1413 struct glink_channel
*channel
;
1414 unsigned long flags
;
1416 spin_lock_irqsave(&glink
->idr_lock
, flags
);
1417 channel
= idr_find(&glink
->rcids
, rcid
);
1418 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
1419 if (WARN(!channel
, "close request on unknown channel\n"))
1422 /* cancel pending rx_done work */
1423 cancel_work_sync(&channel
->intent_work
);
1425 if (channel
->rpdev
) {
1426 strncpy(chinfo
.name
, channel
->name
, sizeof(chinfo
.name
));
1427 chinfo
.src
= RPMSG_ADDR_ANY
;
1428 chinfo
.dst
= RPMSG_ADDR_ANY
;
1430 rpmsg_unregister_device(glink
->dev
, &chinfo
);
1433 qcom_glink_send_close_ack(glink
, channel
->rcid
);
1435 spin_lock_irqsave(&glink
->idr_lock
, flags
);
1436 idr_remove(&glink
->rcids
, channel
->rcid
);
1438 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
1440 kref_put(&channel
->refcount
, qcom_glink_channel_release
);
1443 static void qcom_glink_rx_close_ack(struct qcom_glink
*glink
, unsigned int lcid
)
1445 struct glink_channel
*channel
;
1446 unsigned long flags
;
1448 spin_lock_irqsave(&glink
->idr_lock
, flags
);
1449 channel
= idr_find(&glink
->lcids
, lcid
);
1450 if (WARN(!channel
, "close ack on unknown channel\n")) {
1451 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
1455 idr_remove(&glink
->lcids
, channel
->lcid
);
1457 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
1459 kref_put(&channel
->refcount
, qcom_glink_channel_release
);
1462 static void qcom_glink_work(struct work_struct
*work
)
1464 struct qcom_glink
*glink
= container_of(work
, struct qcom_glink
,
1466 struct glink_defer_cmd
*dcmd
;
1467 struct glink_msg
*msg
;
1468 unsigned long flags
;
1469 unsigned int param1
;
1470 unsigned int param2
;
1474 spin_lock_irqsave(&glink
->rx_lock
, flags
);
1475 if (list_empty(&glink
->rx_queue
)) {
1476 spin_unlock_irqrestore(&glink
->rx_lock
, flags
);
1479 dcmd
= list_first_entry(&glink
->rx_queue
,
1480 struct glink_defer_cmd
, node
);
1481 list_del(&dcmd
->node
);
1482 spin_unlock_irqrestore(&glink
->rx_lock
, flags
);
1485 cmd
= le16_to_cpu(msg
->cmd
);
1486 param1
= le16_to_cpu(msg
->param1
);
1487 param2
= le32_to_cpu(msg
->param2
);
1490 case RPM_CMD_VERSION
:
1491 qcom_glink_receive_version(glink
, param1
, param2
);
1493 case RPM_CMD_VERSION_ACK
:
1494 qcom_glink_receive_version_ack(glink
, param1
, param2
);
1497 qcom_glink_rx_open(glink
, param1
, msg
->data
);
1500 qcom_glink_rx_close(glink
, param1
);
1502 case RPM_CMD_CLOSE_ACK
:
1503 qcom_glink_rx_close_ack(glink
, param1
);
1505 case RPM_CMD_RX_INTENT_REQ
:
1506 qcom_glink_handle_intent_req(glink
, param1
, param2
);
1509 WARN(1, "Unknown defer object %d\n", cmd
);
1517 struct qcom_glink
*qcom_glink_native_probe(struct device
*dev
,
1518 unsigned long features
,
1519 struct qcom_glink_pipe
*rx
,
1520 struct qcom_glink_pipe
*tx
,
1525 struct qcom_glink
*glink
;
1527 glink
= devm_kzalloc(dev
, sizeof(*glink
), GFP_KERNEL
);
1529 return ERR_PTR(-ENOMEM
);
1532 glink
->tx_pipe
= tx
;
1533 glink
->rx_pipe
= rx
;
1535 glink
->features
= features
;
1536 glink
->intentless
= intentless
;
1538 mutex_init(&glink
->tx_lock
);
1539 spin_lock_init(&glink
->rx_lock
);
1540 INIT_LIST_HEAD(&glink
->rx_queue
);
1541 INIT_WORK(&glink
->rx_work
, qcom_glink_work
);
1543 spin_lock_init(&glink
->idr_lock
);
1544 idr_init(&glink
->lcids
);
1545 idr_init(&glink
->rcids
);
1547 glink
->mbox_client
.dev
= dev
;
1548 glink
->mbox_chan
= mbox_request_channel(&glink
->mbox_client
, 0);
1549 if (IS_ERR(glink
->mbox_chan
)) {
1550 if (PTR_ERR(glink
->mbox_chan
) != -EPROBE_DEFER
)
1551 dev_err(dev
, "failed to acquire IPC channel\n");
1552 return ERR_CAST(glink
->mbox_chan
);
1555 irq
= of_irq_get(dev
->of_node
, 0);
1556 ret
= devm_request_irq(dev
, irq
,
1557 qcom_glink_native_intr
,
1558 IRQF_NO_SUSPEND
| IRQF_SHARED
,
1559 "glink-native", glink
);
1561 dev_err(dev
, "failed to request IRQ\n");
1562 return ERR_PTR(ret
);
1567 ret
= qcom_glink_send_version(glink
);
1569 return ERR_PTR(ret
);
1573 EXPORT_SYMBOL_GPL(qcom_glink_native_probe
);
1575 static int qcom_glink_remove_device(struct device
*dev
, void *data
)
1577 device_unregister(dev
);
1582 void qcom_glink_native_remove(struct qcom_glink
*glink
)
1584 struct glink_channel
*channel
;
1587 unsigned long flags
;
1589 disable_irq(glink
->irq
);
1590 cancel_work_sync(&glink
->rx_work
);
1592 ret
= device_for_each_child(glink
->dev
, NULL
, qcom_glink_remove_device
);
1594 dev_warn(glink
->dev
, "Can't remove GLINK devices: %d\n", ret
);
1596 spin_lock_irqsave(&glink
->idr_lock
, flags
);
1597 /* Release any defunct local channels, waiting for close-ack */
1598 idr_for_each_entry(&glink
->lcids
, channel
, cid
)
1599 kref_put(&channel
->refcount
, qcom_glink_channel_release
);
1601 idr_destroy(&glink
->lcids
);
1602 idr_destroy(&glink
->rcids
);
1603 spin_unlock_irqrestore(&glink
->idr_lock
, flags
);
1604 mbox_free_channel(glink
->mbox_chan
);
1606 EXPORT_SYMBOL_GPL(qcom_glink_native_remove
);
1608 void qcom_glink_native_unregister(struct qcom_glink
*glink
)
1610 device_unregister(glink
->dev
);
1612 EXPORT_SYMBOL_GPL(qcom_glink_native_unregister
);