1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016-2017, Linaro Ltd
7 #include <linux/interrupt.h>
9 #include <linux/list.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
13 #include <linux/of_address.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/rpmsg.h>
17 #include <linux/sizes.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
20 #include <linux/mailbox_client.h>
22 #include "rpmsg_internal.h"
23 #include "qcom_glink_native.h"
25 #define GLINK_NAME_SIZE 32
26 #define GLINK_VERSION_1 1
28 #define RPM_GLINK_CID_MIN 1
29 #define RPM_GLINK_CID_MAX 65536
39 * struct glink_defer_cmd - deferred incoming control message
41 * @msg: message header
42 * @data: payload of the message
44 * Copy of a received control message, to be added to @rx_queue and processed
45 * by @rx_work of @qcom_glink.
47 struct glink_defer_cmd {
48 struct list_head node;
55 * struct glink_core_rx_intent - RX intent
58 * @data: pointer to the data (may be NULL for zero-copy)
59 * @id: remote or local intent ID
60 * @size: size of the original intent (do not modify)
61 * @reuse: To mark if the intent can be reused after first use
62 * @in_use: To mark if intent is already in use for the channel
63 * @offset: next write offset (initially 0)
66 struct glink_core_rx_intent {
74 struct list_head node;
78 * struct qcom_glink - driver context, relates to one remote subsystem
79 * @dev: reference to the associated struct device
80 * @rx_pipe: pipe object for receive FIFO
81 * @tx_pipe: pipe object for transmit FIFO
82 * @rx_work: worker for handling received control messages
83 * @rx_lock: protects the @rx_queue
84 * @rx_queue: queue of received control messages to be processed in @rx_work
85 * @tx_lock: synchronizes operations on the tx fifo
86 * @idr_lock: synchronizes @lcids and @rcids modifications
87 * @lcids: idr of all channels with a known local channel id
88 * @rcids: idr of all channels with a known remote channel id
89 * @features: remote features
90 * @intentless: flag to indicate that there is no intent
91 * @tx_avail_notify: Waitqueue for pending tx tasks
92 * @sent_read_notify: flag to check cmd sent or not
93 * @abort_tx: flag indicating that all tx attempts should fail
98 struct qcom_glink_pipe *rx_pipe;
99 struct qcom_glink_pipe *tx_pipe;
101 struct work_struct rx_work;
103 struct list_head rx_queue;
110 unsigned long features;
113 wait_queue_head_t tx_avail_notify;
114 bool sent_read_notify;
127 * struct glink_channel - internal representation of a channel
128 * @rpdev: rpdev reference, only used for primary endpoints
129 * @ept: rpmsg endpoint this channel is associated with
130 * @glink: qcom_glink context handle
131 * @refcount: refcount for the channel object
132 * @recv_lock: guard for @ept.cb
133 * @name: unique channel name/identifier
134 * @lcid: channel id, in local space
135 * @rcid: channel id, in remote space
136 * @intent_lock: lock for protection of @liids, @riids
137 * @liids: idr of all local intents
138 * @riids: idr of all remote intents
139 * @intent_work: worker responsible for transmitting rx_done packets
140 * @done_intents: list of intents that needs to be announced rx_done
141 * @buf: receive buffer, for gathering fragments
142 * @buf_offset: write offset in @buf
143 * @buf_size: size of current @buf
144 * @open_ack: completed once remote has acked the open-request
145 * @open_req: completed once open-request has been received
146 * @intent_req_lock: Synchronises multiple intent requests
147 * @intent_req_result: Result of intent request
148 * @intent_req_comp: Completion for intent_req signalling
150 struct glink_channel {
151 struct rpmsg_endpoint ept;
153 struct rpmsg_device *rpdev;
154 struct qcom_glink *glink;
156 struct kref refcount;
158 spinlock_t recv_lock;
164 spinlock_t intent_lock;
167 struct work_struct intent_work;
168 struct list_head done_intents;
170 struct glink_core_rx_intent *buf;
174 struct completion open_ack;
175 struct completion open_req;
177 struct mutex intent_req_lock;
178 bool intent_req_result;
179 struct completion intent_req_comp;
182 #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
184 static const struct rpmsg_endpoint_ops glink_endpoint_ops;
186 #define RPM_CMD_VERSION 0
187 #define RPM_CMD_VERSION_ACK 1
188 #define RPM_CMD_OPEN 2
189 #define RPM_CMD_CLOSE 3
190 #define RPM_CMD_OPEN_ACK 4
191 #define RPM_CMD_INTENT 5
192 #define RPM_CMD_RX_DONE 6
193 #define RPM_CMD_RX_INTENT_REQ 7
194 #define RPM_CMD_RX_INTENT_REQ_ACK 8
195 #define RPM_CMD_TX_DATA 9
196 #define RPM_CMD_CLOSE_ACK 11
197 #define RPM_CMD_TX_DATA_CONT 12
198 #define RPM_CMD_READ_NOTIF 13
199 #define RPM_CMD_RX_DONE_W_REUSE 14
201 #define GLINK_FEATURE_INTENTLESS BIT(1)
203 static void qcom_glink_rx_done_work(struct work_struct *work);
205 static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
208 struct glink_channel *channel;
210 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
212 return ERR_PTR(-ENOMEM);
214 /* Setup glink internal glink_channel data */
215 spin_lock_init(&channel->recv_lock);
216 spin_lock_init(&channel->intent_lock);
217 mutex_init(&channel->intent_req_lock);
219 channel->glink = glink;
220 channel->name = kstrdup(name, GFP_KERNEL);
222 init_completion(&channel->open_req);
223 init_completion(&channel->open_ack);
224 init_completion(&channel->intent_req_comp);
226 INIT_LIST_HEAD(&channel->done_intents);
227 INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
229 idr_init(&channel->liids);
230 idr_init(&channel->riids);
231 kref_init(&channel->refcount);
236 static void qcom_glink_channel_release(struct kref *ref)
238 struct glink_channel *channel = container_of(ref, struct glink_channel,
240 struct glink_core_rx_intent *intent;
241 struct glink_core_rx_intent *tmp;
245 /* cancel pending rx_done work */
246 cancel_work_sync(&channel->intent_work);
248 spin_lock_irqsave(&channel->intent_lock, flags);
249 /* Free all non-reuse intents pending rx_done work */
250 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
251 if (!intent->reuse) {
257 idr_for_each_entry(&channel->liids, tmp, iid) {
261 idr_destroy(&channel->liids);
263 idr_for_each_entry(&channel->riids, tmp, iid)
265 idr_destroy(&channel->riids);
266 spin_unlock_irqrestore(&channel->intent_lock, flags);
268 kfree(channel->name);
272 static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
274 return glink->rx_pipe->avail(glink->rx_pipe);
277 static void qcom_glink_rx_peek(struct qcom_glink *glink,
278 void *data, unsigned int offset, size_t count)
280 glink->rx_pipe->peek(glink->rx_pipe, data, offset, count);
283 static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
285 glink->rx_pipe->advance(glink->rx_pipe, count);
288 static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
290 return glink->tx_pipe->avail(glink->tx_pipe);
293 static void qcom_glink_tx_write(struct qcom_glink *glink,
294 const void *hdr, size_t hlen,
295 const void *data, size_t dlen)
297 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
300 static void qcom_glink_tx_kick(struct qcom_glink *glink)
302 glink->tx_pipe->kick(glink->tx_pipe);
305 static void qcom_glink_send_read_notify(struct qcom_glink *glink)
307 struct glink_msg msg;
309 msg.cmd = cpu_to_le16(RPM_CMD_READ_NOTIF);
313 qcom_glink_tx_write(glink, &msg, sizeof(msg), NULL, 0);
315 qcom_glink_tx_kick(glink);
318 static int qcom_glink_tx(struct qcom_glink *glink,
319 const void *hdr, size_t hlen,
320 const void *data, size_t dlen, bool wait)
322 unsigned int tlen = hlen + dlen;
326 /* Reject packets that are too big */
327 if (tlen >= glink->tx_pipe->length)
330 spin_lock_irqsave(&glink->tx_lock, flags);
332 if (glink->abort_tx) {
337 while (qcom_glink_tx_avail(glink) < tlen) {
343 if (glink->abort_tx) {
348 if (!glink->sent_read_notify) {
349 glink->sent_read_notify = true;
350 qcom_glink_send_read_notify(glink);
353 /* Wait without holding the tx_lock */
354 spin_unlock_irqrestore(&glink->tx_lock, flags);
356 wait_event_timeout(glink->tx_avail_notify,
357 qcom_glink_tx_avail(glink) >= tlen, 10 * HZ);
359 spin_lock_irqsave(&glink->tx_lock, flags);
361 if (qcom_glink_tx_avail(glink) >= tlen)
362 glink->sent_read_notify = false;
365 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
366 qcom_glink_tx_kick(glink);
369 spin_unlock_irqrestore(&glink->tx_lock, flags);
374 static int qcom_glink_send_version(struct qcom_glink *glink)
376 struct glink_msg msg;
378 msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
379 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
380 msg.param2 = cpu_to_le32(glink->features);
382 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
385 static void qcom_glink_send_version_ack(struct qcom_glink *glink)
387 struct glink_msg msg;
389 msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
390 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
391 msg.param2 = cpu_to_le32(glink->features);
393 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
396 static void qcom_glink_send_open_ack(struct qcom_glink *glink,
397 struct glink_channel *channel)
399 struct glink_msg msg;
401 msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
402 msg.param1 = cpu_to_le16(channel->rcid);
403 msg.param2 = cpu_to_le32(0);
405 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
408 static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
409 unsigned int cid, bool granted)
411 struct glink_channel *channel;
414 spin_lock_irqsave(&glink->idr_lock, flags);
415 channel = idr_find(&glink->rcids, cid);
416 spin_unlock_irqrestore(&glink->idr_lock, flags);
418 dev_err(glink->dev, "unable to find channel\n");
422 channel->intent_req_result = granted;
423 complete(&channel->intent_req_comp);
426 static void qcom_glink_intent_req_abort(struct glink_channel *channel)
428 channel->intent_req_result = 0;
429 complete(&channel->intent_req_comp);
433 * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
434 * @glink: Ptr to the glink edge
435 * @channel: Ptr to the channel that the open req is sent
437 * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
438 * Will return with refcount held, regardless of outcome.
440 * Return: 0 on success, negative errno otherwise.
442 static int qcom_glink_send_open_req(struct qcom_glink *glink,
443 struct glink_channel *channel)
446 struct glink_msg msg;
447 u8 name[GLINK_NAME_SIZE];
449 int name_len = strlen(channel->name) + 1;
450 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
454 kref_get(&channel->refcount);
456 spin_lock_irqsave(&glink->idr_lock, flags);
457 ret = idr_alloc_cyclic(&glink->lcids, channel,
458 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
460 spin_unlock_irqrestore(&glink->idr_lock, flags);
466 req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
467 req.msg.param1 = cpu_to_le16(channel->lcid);
468 req.msg.param2 = cpu_to_le32(name_len);
469 strcpy(req.name, channel->name);
471 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
478 spin_lock_irqsave(&glink->idr_lock, flags);
479 idr_remove(&glink->lcids, channel->lcid);
481 spin_unlock_irqrestore(&glink->idr_lock, flags);
486 static void qcom_glink_send_close_req(struct qcom_glink *glink,
487 struct glink_channel *channel)
489 struct glink_msg req;
491 req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
492 req.param1 = cpu_to_le16(channel->lcid);
495 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
498 static void qcom_glink_send_close_ack(struct qcom_glink *glink,
501 struct glink_msg req;
503 req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
504 req.param1 = cpu_to_le16(rcid);
507 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
510 static void qcom_glink_rx_done_work(struct work_struct *work)
512 struct glink_channel *channel = container_of(work, struct glink_channel,
514 struct qcom_glink *glink = channel->glink;
515 struct glink_core_rx_intent *intent, *tmp;
522 unsigned int cid = channel->lcid;
527 spin_lock_irqsave(&channel->intent_lock, flags);
528 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
529 list_del(&intent->node);
530 spin_unlock_irqrestore(&channel->intent_lock, flags);
532 reuse = intent->reuse;
534 cmd.id = reuse ? RPM_CMD_RX_DONE_W_REUSE : RPM_CMD_RX_DONE;
538 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
543 spin_lock_irqsave(&channel->intent_lock, flags);
545 spin_unlock_irqrestore(&channel->intent_lock, flags);
548 static void qcom_glink_rx_done(struct qcom_glink *glink,
549 struct glink_channel *channel,
550 struct glink_core_rx_intent *intent)
552 /* We don't send RX_DONE to intentless systems */
553 if (glink->intentless) {
559 /* Take it off the tree of receive intents */
560 if (!intent->reuse) {
561 spin_lock(&channel->intent_lock);
562 idr_remove(&channel->liids, intent->id);
563 spin_unlock(&channel->intent_lock);
566 /* Schedule the sending of a rx_done indication */
567 spin_lock(&channel->intent_lock);
568 list_add_tail(&intent->node, &channel->done_intents);
569 spin_unlock(&channel->intent_lock);
571 schedule_work(&channel->intent_work);
575 * qcom_glink_receive_version() - receive version/features from remote system
577 * @glink: pointer to transport interface
578 * @version: remote version
579 * @features: remote features
581 * This function is called in response to a remote-initiated version/feature
582 * negotiation sequence.
584 static void qcom_glink_receive_version(struct qcom_glink *glink,
591 case GLINK_VERSION_1:
592 glink->features &= features;
595 qcom_glink_send_version_ack(glink);
601 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
603 * @glink: pointer to transport interface
604 * @version: remote version response
605 * @features: remote features response
607 * This function is called in response to a local-initiated version/feature
608 * negotiation sequence and is the counter-offer from the remote side based
609 * upon the initial version and feature set requested.
611 static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
617 /* Version negotiation failed */
619 case GLINK_VERSION_1:
620 if (features == glink->features)
623 glink->features &= features;
626 qcom_glink_send_version(glink);
632 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
633 * wire format and transmit
634 * @glink: The transport to transmit on.
635 * @channel: The glink channel
636 * @granted: The request response to encode.
638 * Return: 0 on success or standard Linux error code.
640 static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
641 struct glink_channel *channel,
644 struct glink_msg msg;
646 msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
647 msg.param1 = cpu_to_le16(channel->lcid);
648 msg.param2 = cpu_to_le32(granted);
650 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
656 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
658 * @glink: The transport to transmit on.
659 * @channel: The local channel
660 * @intent: The intent to pass on to remote.
662 * Return: 0 on success or standard Linux error code.
664 static int qcom_glink_advertise_intent(struct qcom_glink *glink,
665 struct glink_channel *channel,
666 struct glink_core_rx_intent *intent)
677 cmd.id = cpu_to_le16(RPM_CMD_INTENT);
678 cmd.lcid = cpu_to_le16(channel->lcid);
679 cmd.count = cpu_to_le32(1);
680 cmd.size = cpu_to_le32(intent->size);
681 cmd.liid = cpu_to_le32(intent->id);
683 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
688 static struct glink_core_rx_intent *
689 qcom_glink_alloc_intent(struct qcom_glink *glink,
690 struct glink_channel *channel,
694 struct glink_core_rx_intent *intent;
698 intent = kzalloc(sizeof(*intent), GFP_KERNEL);
702 intent->data = kzalloc(size, GFP_KERNEL);
706 spin_lock_irqsave(&channel->intent_lock, flags);
707 ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
709 spin_unlock_irqrestore(&channel->intent_lock, flags);
712 spin_unlock_irqrestore(&channel->intent_lock, flags);
716 intent->reuse = reuseable;
727 static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
728 u32 cid, uint32_t iid,
731 struct glink_core_rx_intent *intent;
732 struct glink_channel *channel;
735 spin_lock_irqsave(&glink->idr_lock, flags);
736 channel = idr_find(&glink->rcids, cid);
737 spin_unlock_irqrestore(&glink->idr_lock, flags);
739 dev_err(glink->dev, "invalid channel id received\n");
743 spin_lock_irqsave(&channel->intent_lock, flags);
744 intent = idr_find(&channel->riids, iid);
747 spin_unlock_irqrestore(&channel->intent_lock, flags);
748 dev_err(glink->dev, "invalid intent id received\n");
752 intent->in_use = false;
755 idr_remove(&channel->riids, intent->id);
758 spin_unlock_irqrestore(&channel->intent_lock, flags);
762 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
764 * @glink: Pointer to the transport interface
765 * @cid: Remote channel ID
766 * @size: size of the intent
768 * The function searches for the local channel to which the request for
769 * rx_intent has arrived and allocates and notifies the remote back
771 static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
772 u32 cid, size_t size)
774 struct glink_core_rx_intent *intent;
775 struct glink_channel *channel;
778 spin_lock_irqsave(&glink->idr_lock, flags);
779 channel = idr_find(&glink->rcids, cid);
780 spin_unlock_irqrestore(&glink->idr_lock, flags);
783 pr_err("%s channel not found for cid %d\n", __func__, cid);
787 intent = qcom_glink_alloc_intent(glink, channel, size, false);
789 qcom_glink_advertise_intent(glink, channel, intent);
791 qcom_glink_send_intent_req_ack(glink, channel, !!intent);
794 static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
796 struct glink_defer_cmd *dcmd;
798 extra = ALIGN(extra, 8);
800 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
801 dev_dbg(glink->dev, "Insufficient data in rx fifo");
805 dcmd = kzalloc(struct_size(dcmd, data, extra), GFP_ATOMIC);
809 INIT_LIST_HEAD(&dcmd->node);
811 qcom_glink_rx_peek(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
813 spin_lock(&glink->rx_lock);
814 list_add_tail(&dcmd->node, &glink->rx_queue);
815 spin_unlock(&glink->rx_lock);
817 schedule_work(&glink->rx_work);
818 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
823 static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
825 struct glink_core_rx_intent *intent;
826 struct glink_channel *channel;
828 struct glink_msg msg;
832 unsigned int chunk_size;
833 unsigned int left_size;
839 if (avail < sizeof(hdr)) {
840 dev_dbg(glink->dev, "Not enough data in fifo\n");
844 qcom_glink_rx_peek(glink, &hdr, 0, sizeof(hdr));
845 chunk_size = le32_to_cpu(hdr.chunk_size);
846 left_size = le32_to_cpu(hdr.left_size);
848 if (avail < sizeof(hdr) + chunk_size) {
849 dev_dbg(glink->dev, "Payload not yet in fifo\n");
853 rcid = le16_to_cpu(hdr.msg.param1);
854 spin_lock_irqsave(&glink->idr_lock, flags);
855 channel = idr_find(&glink->rcids, rcid);
856 spin_unlock_irqrestore(&glink->idr_lock, flags);
858 dev_dbg(glink->dev, "Data on non-existing channel\n");
860 /* Drop the message */
864 if (glink->intentless) {
865 /* Might have an ongoing, fragmented, message to append */
867 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
871 intent->data = kmalloc(chunk_size + left_size,
878 intent->id = 0xbabababa;
879 intent->size = chunk_size + left_size;
882 channel->buf = intent;
884 intent = channel->buf;
887 liid = le32_to_cpu(hdr.msg.param2);
889 spin_lock_irqsave(&channel->intent_lock, flags);
890 intent = idr_find(&channel->liids, liid);
891 spin_unlock_irqrestore(&channel->intent_lock, flags);
895 "no intent found for channel %s intent %d",
896 channel->name, liid);
902 if (intent->size - intent->offset < chunk_size) {
903 dev_err(glink->dev, "Insufficient space in intent\n");
905 /* The packet header lied, drop payload */
909 qcom_glink_rx_peek(glink, intent->data + intent->offset,
910 sizeof(hdr), chunk_size);
911 intent->offset += chunk_size;
913 /* Handle message when no fragments remain to be received */
915 spin_lock(&channel->recv_lock);
916 if (channel->ept.cb) {
917 channel->ept.cb(channel->ept.rpdev,
923 spin_unlock(&channel->recv_lock);
928 qcom_glink_rx_done(glink, channel, intent);
932 qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
937 static void qcom_glink_handle_intent(struct qcom_glink *glink,
942 struct glink_core_rx_intent *intent;
943 struct glink_channel *channel;
950 struct glink_msg msg;
951 struct intent_pair intents[];
954 const size_t msglen = struct_size(msg, intents, count);
959 if (avail < msglen) {
960 dev_dbg(glink->dev, "Not enough data in fifo\n");
964 spin_lock_irqsave(&glink->idr_lock, flags);
965 channel = idr_find(&glink->rcids, cid);
966 spin_unlock_irqrestore(&glink->idr_lock, flags);
968 dev_err(glink->dev, "intents for non-existing channel\n");
972 msg = kmalloc(msglen, GFP_ATOMIC);
976 qcom_glink_rx_peek(glink, msg, 0, msglen);
978 for (i = 0; i < count; ++i) {
979 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
983 intent->id = le32_to_cpu(msg->intents[i].iid);
984 intent->size = le32_to_cpu(msg->intents[i].size);
986 spin_lock_irqsave(&channel->intent_lock, flags);
987 ret = idr_alloc(&channel->riids, intent,
988 intent->id, intent->id + 1, GFP_ATOMIC);
989 spin_unlock_irqrestore(&channel->intent_lock, flags);
992 dev_err(glink->dev, "failed to store remote intent\n");
996 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
999 static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
1001 struct glink_channel *channel;
1003 spin_lock(&glink->idr_lock);
1004 channel = idr_find(&glink->lcids, lcid);
1005 spin_unlock(&glink->idr_lock);
1007 dev_err(glink->dev, "Invalid open ack packet\n");
1011 complete_all(&channel->open_ack);
1016 void qcom_glink_native_rx(struct qcom_glink *glink)
1018 struct glink_msg msg;
1019 unsigned int param1;
1020 unsigned int param2;
1025 /* To wakeup any blocking writers */
1026 wake_up_all(&glink->tx_avail_notify);
1029 avail = qcom_glink_rx_avail(glink);
1030 if (avail < sizeof(msg))
1033 qcom_glink_rx_peek(glink, &msg, 0, sizeof(msg));
1035 cmd = le16_to_cpu(msg.cmd);
1036 param1 = le16_to_cpu(msg.param1);
1037 param2 = le32_to_cpu(msg.param2);
1040 case RPM_CMD_VERSION:
1041 case RPM_CMD_VERSION_ACK:
1043 case RPM_CMD_CLOSE_ACK:
1044 case RPM_CMD_RX_INTENT_REQ:
1045 ret = qcom_glink_rx_defer(glink, 0);
1047 case RPM_CMD_OPEN_ACK:
1048 ret = qcom_glink_rx_open_ack(glink, param1);
1049 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1052 ret = qcom_glink_rx_defer(glink, param2);
1054 case RPM_CMD_TX_DATA:
1055 case RPM_CMD_TX_DATA_CONT:
1056 ret = qcom_glink_rx_data(glink, avail);
1058 case RPM_CMD_READ_NOTIF:
1059 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1060 qcom_glink_tx_kick(glink);
1062 case RPM_CMD_INTENT:
1063 qcom_glink_handle_intent(glink, param1, param2, avail);
1065 case RPM_CMD_RX_DONE:
1066 qcom_glink_handle_rx_done(glink, param1, param2, false);
1067 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1069 case RPM_CMD_RX_DONE_W_REUSE:
1070 qcom_glink_handle_rx_done(glink, param1, param2, true);
1071 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1073 case RPM_CMD_RX_INTENT_REQ_ACK:
1074 qcom_glink_handle_intent_req_ack(glink, param1, param2);
1075 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1078 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1087 EXPORT_SYMBOL(qcom_glink_native_rx);
1089 /* Locally initiated rpmsg_create_ept */
1090 static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1093 struct glink_channel *channel;
1095 unsigned long flags;
1097 channel = qcom_glink_alloc_channel(glink, name);
1098 if (IS_ERR(channel))
1099 return ERR_CAST(channel);
1101 ret = qcom_glink_send_open_req(glink, channel);
1103 goto release_channel;
1105 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1109 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1113 qcom_glink_send_open_ack(glink, channel);
1118 /* qcom_glink_send_open_req() did register the channel in lcids*/
1119 spin_lock_irqsave(&glink->idr_lock, flags);
1120 idr_remove(&glink->lcids, channel->lcid);
1121 spin_unlock_irqrestore(&glink->idr_lock, flags);
1124 /* Release qcom_glink_send_open_req() reference */
1125 kref_put(&channel->refcount, qcom_glink_channel_release);
1126 /* Release qcom_glink_alloc_channel() reference */
1127 kref_put(&channel->refcount, qcom_glink_channel_release);
1129 return ERR_PTR(-ETIMEDOUT);
1132 /* Remote initiated rpmsg_create_ept */
1133 static int qcom_glink_create_remote(struct qcom_glink *glink,
1134 struct glink_channel *channel)
1138 qcom_glink_send_open_ack(glink, channel);
1140 ret = qcom_glink_send_open_req(glink, channel);
1144 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1154 * Send a close request to "undo" our open-ack. The close-ack will
1155 * release qcom_glink_send_open_req() reference and the last reference
1156 * will be relesed after receiving remote_close or transport unregister
1157 * by calling qcom_glink_native_remove().
1159 qcom_glink_send_close_req(glink, channel);
1164 static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1167 struct rpmsg_channel_info
1170 struct glink_channel *parent = to_glink_channel(rpdev->ept);
1171 struct glink_channel *channel;
1172 struct qcom_glink *glink = parent->glink;
1173 struct rpmsg_endpoint *ept;
1174 const char *name = chinfo.name;
1177 unsigned long flags;
1179 spin_lock_irqsave(&glink->idr_lock, flags);
1180 idr_for_each_entry(&glink->rcids, channel, cid) {
1181 if (!strcmp(channel->name, name))
1184 spin_unlock_irqrestore(&glink->idr_lock, flags);
1187 channel = qcom_glink_create_local(glink, name);
1188 if (IS_ERR(channel))
1191 ret = qcom_glink_create_remote(glink, channel);
1196 ept = &channel->ept;
1200 ept->ops = &glink_endpoint_ops;
1205 static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1207 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1208 struct device_node *np = rpdev->dev.of_node;
1209 struct qcom_glink *glink = channel->glink;
1210 struct glink_core_rx_intent *intent;
1211 const struct property *prop = NULL;
1212 __be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
1215 __be32 *val = defaults;
1218 if (glink->intentless || !completion_done(&channel->open_ack))
1221 prop = of_find_property(np, "qcom,intents", NULL);
1224 num_groups = prop->length / sizeof(u32) / 2;
1227 /* Channel is now open, advertise base set of intents */
1228 while (num_groups--) {
1229 size = be32_to_cpup(val++);
1230 num_intents = be32_to_cpup(val++);
1231 while (num_intents--) {
1232 intent = qcom_glink_alloc_intent(glink, channel, size,
1237 qcom_glink_advertise_intent(glink, channel, intent);
1243 static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1245 struct glink_channel *channel = to_glink_channel(ept);
1246 struct qcom_glink *glink = channel->glink;
1247 unsigned long flags;
1249 spin_lock_irqsave(&channel->recv_lock, flags);
1250 channel->ept.cb = NULL;
1251 spin_unlock_irqrestore(&channel->recv_lock, flags);
1253 /* Decouple the potential rpdev from the channel */
1254 channel->rpdev = NULL;
1256 qcom_glink_send_close_req(glink, channel);
1259 static int qcom_glink_request_intent(struct qcom_glink *glink,
1260 struct glink_channel *channel,
1271 mutex_lock(&channel->intent_req_lock);
1273 reinit_completion(&channel->intent_req_comp);
1275 cmd.id = RPM_CMD_RX_INTENT_REQ;
1276 cmd.cid = channel->lcid;
1279 ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1283 ret = wait_for_completion_timeout(&channel->intent_req_comp, 10 * HZ);
1285 dev_err(glink->dev, "intent request timed out\n");
1288 ret = channel->intent_req_result ? 0 : -ECANCELED;
1292 mutex_unlock(&channel->intent_req_lock);
1296 static int __qcom_glink_send(struct glink_channel *channel,
1297 void *data, int len, bool wait)
1299 struct qcom_glink *glink = channel->glink;
1300 struct glink_core_rx_intent *intent = NULL;
1301 struct glink_core_rx_intent *tmp;
1304 struct glink_msg msg;
1309 unsigned long flags;
1310 int chunk_size = len;
1313 if (!glink->intentless) {
1315 spin_lock_irqsave(&channel->intent_lock, flags);
1316 idr_for_each_entry(&channel->riids, tmp, iid) {
1317 if (tmp->size >= len && !tmp->in_use) {
1320 else if (intent->size > tmp->size)
1322 if (intent->size == len)
1327 intent->in_use = true;
1328 spin_unlock_irqrestore(&channel->intent_lock, flags);
1330 /* We found an available intent */
1337 ret = qcom_glink_request_intent(glink, channel, len);
1345 if (wait && chunk_size > SZ_8K) {
1347 left_size = len - chunk_size;
1349 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
1350 req.msg.param1 = cpu_to_le16(channel->lcid);
1351 req.msg.param2 = cpu_to_le32(iid);
1352 req.chunk_size = cpu_to_le32(chunk_size);
1353 req.left_size = cpu_to_le32(left_size);
1355 ret = qcom_glink_tx(glink, &req, sizeof(req), data, chunk_size, wait);
1357 /* Mark intent available if we failed */
1358 if (ret && intent) {
1359 intent->in_use = false;
1363 while (left_size > 0) {
1364 data = (void *)((char *)data + chunk_size);
1365 chunk_size = left_size;
1366 if (chunk_size > SZ_8K)
1368 left_size -= chunk_size;
1370 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA_CONT);
1371 req.msg.param1 = cpu_to_le16(channel->lcid);
1372 req.msg.param2 = cpu_to_le32(iid);
1373 req.chunk_size = cpu_to_le32(chunk_size);
1374 req.left_size = cpu_to_le32(left_size);
1376 ret = qcom_glink_tx(glink, &req, sizeof(req), data,
1379 /* Mark intent available if we failed */
1380 if (ret && intent) {
1381 intent->in_use = false;
1388 static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1390 struct glink_channel *channel = to_glink_channel(ept);
1392 return __qcom_glink_send(channel, data, len, true);
1395 static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1397 struct glink_channel *channel = to_glink_channel(ept);
1399 return __qcom_glink_send(channel, data, len, false);
1402 static int qcom_glink_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1404 struct glink_channel *channel = to_glink_channel(ept);
1406 return __qcom_glink_send(channel, data, len, true);
1409 static int qcom_glink_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1411 struct glink_channel *channel = to_glink_channel(ept);
1413 return __qcom_glink_send(channel, data, len, false);
1417 * Finds the device_node for the glink child interested in this channel.
1419 static struct device_node *qcom_glink_match_channel(struct device_node *node,
1420 const char *channel)
1422 struct device_node *child;
1427 for_each_available_child_of_node(node, child) {
1428 key = "qcom,glink-channels";
1429 ret = of_property_read_string(child, key, &name);
1433 if (strcmp(name, channel) == 0)
1440 static const struct rpmsg_device_ops glink_device_ops = {
1441 .create_ept = qcom_glink_create_ept,
1442 .announce_create = qcom_glink_announce_create,
1445 static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1446 .destroy_ept = qcom_glink_destroy_ept,
1447 .send = qcom_glink_send,
1448 .sendto = qcom_glink_sendto,
1449 .trysend = qcom_glink_trysend,
1450 .trysendto = qcom_glink_trysendto,
1453 static void qcom_glink_rpdev_release(struct device *dev)
1455 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1460 static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1463 struct glink_channel *channel;
1464 struct rpmsg_device *rpdev;
1465 bool create_device = false;
1466 struct device_node *node;
1469 unsigned long flags;
1471 spin_lock_irqsave(&glink->idr_lock, flags);
1472 idr_for_each_entry(&glink->lcids, channel, lcid) {
1473 if (!strcmp(channel->name, name))
1476 spin_unlock_irqrestore(&glink->idr_lock, flags);
1479 channel = qcom_glink_alloc_channel(glink, name);
1480 if (IS_ERR(channel))
1481 return PTR_ERR(channel);
1483 /* The opening dance was initiated by the remote */
1484 create_device = true;
1487 spin_lock_irqsave(&glink->idr_lock, flags);
1488 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
1490 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
1491 spin_unlock_irqrestore(&glink->idr_lock, flags);
1494 channel->rcid = ret;
1495 spin_unlock_irqrestore(&glink->idr_lock, flags);
1497 complete_all(&channel->open_req);
1499 if (create_device) {
1500 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1506 rpdev->ept = &channel->ept;
1507 strscpy_pad(rpdev->id.name, name, RPMSG_NAME_SIZE);
1508 rpdev->src = RPMSG_ADDR_ANY;
1509 rpdev->dst = RPMSG_ADDR_ANY;
1510 rpdev->ops = &glink_device_ops;
1512 node = qcom_glink_match_channel(glink->dev->of_node, name);
1513 rpdev->dev.of_node = node;
1514 rpdev->dev.parent = glink->dev;
1515 rpdev->dev.release = qcom_glink_rpdev_release;
1517 ret = rpmsg_register_device(rpdev);
1521 channel->rpdev = rpdev;
1527 spin_lock_irqsave(&glink->idr_lock, flags);
1528 idr_remove(&glink->rcids, channel->rcid);
1530 spin_unlock_irqrestore(&glink->idr_lock, flags);
1532 /* Release the reference, iff we took it */
1534 kref_put(&channel->refcount, qcom_glink_channel_release);
1539 static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1541 struct rpmsg_channel_info chinfo;
1542 struct glink_channel *channel;
1543 unsigned long flags;
1545 spin_lock_irqsave(&glink->idr_lock, flags);
1546 channel = idr_find(&glink->rcids, rcid);
1547 spin_unlock_irqrestore(&glink->idr_lock, flags);
1548 if (WARN(!channel, "close request on unknown channel\n"))
1551 /* cancel pending rx_done work */
1552 cancel_work_sync(&channel->intent_work);
1554 if (channel->rpdev) {
1555 strscpy_pad(chinfo.name, channel->name, sizeof(chinfo.name));
1556 chinfo.src = RPMSG_ADDR_ANY;
1557 chinfo.dst = RPMSG_ADDR_ANY;
1559 rpmsg_unregister_device(glink->dev, &chinfo);
1561 channel->rpdev = NULL;
1563 qcom_glink_send_close_ack(glink, channel->rcid);
1565 spin_lock_irqsave(&glink->idr_lock, flags);
1566 idr_remove(&glink->rcids, channel->rcid);
1568 spin_unlock_irqrestore(&glink->idr_lock, flags);
1570 kref_put(&channel->refcount, qcom_glink_channel_release);
1573 static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1575 struct rpmsg_channel_info chinfo;
1576 struct glink_channel *channel;
1577 unsigned long flags;
1579 /* To wakeup any blocking writers */
1580 wake_up_all(&glink->tx_avail_notify);
1582 spin_lock_irqsave(&glink->idr_lock, flags);
1583 channel = idr_find(&glink->lcids, lcid);
1584 if (WARN(!channel, "close ack on unknown channel\n")) {
1585 spin_unlock_irqrestore(&glink->idr_lock, flags);
1589 idr_remove(&glink->lcids, channel->lcid);
1591 spin_unlock_irqrestore(&glink->idr_lock, flags);
1593 /* Decouple the potential rpdev from the channel */
1594 if (channel->rpdev) {
1595 strscpy(chinfo.name, channel->name, sizeof(chinfo.name));
1596 chinfo.src = RPMSG_ADDR_ANY;
1597 chinfo.dst = RPMSG_ADDR_ANY;
1599 rpmsg_unregister_device(glink->dev, &chinfo);
1601 channel->rpdev = NULL;
1603 kref_put(&channel->refcount, qcom_glink_channel_release);
1606 static void qcom_glink_work(struct work_struct *work)
1608 struct qcom_glink *glink = container_of(work, struct qcom_glink,
1610 struct glink_defer_cmd *dcmd;
1611 struct glink_msg *msg;
1612 unsigned long flags;
1613 unsigned int param1;
1614 unsigned int param2;
1618 spin_lock_irqsave(&glink->rx_lock, flags);
1619 if (list_empty(&glink->rx_queue)) {
1620 spin_unlock_irqrestore(&glink->rx_lock, flags);
1623 dcmd = list_first_entry(&glink->rx_queue,
1624 struct glink_defer_cmd, node);
1625 list_del(&dcmd->node);
1626 spin_unlock_irqrestore(&glink->rx_lock, flags);
1629 cmd = le16_to_cpu(msg->cmd);
1630 param1 = le16_to_cpu(msg->param1);
1631 param2 = le32_to_cpu(msg->param2);
1634 case RPM_CMD_VERSION:
1635 qcom_glink_receive_version(glink, param1, param2);
1637 case RPM_CMD_VERSION_ACK:
1638 qcom_glink_receive_version_ack(glink, param1, param2);
1641 qcom_glink_rx_open(glink, param1, msg->data);
1644 qcom_glink_rx_close(glink, param1);
1646 case RPM_CMD_CLOSE_ACK:
1647 qcom_glink_rx_close_ack(glink, param1);
1649 case RPM_CMD_RX_INTENT_REQ:
1650 qcom_glink_handle_intent_req(glink, param1, param2);
1653 WARN(1, "Unknown defer object %d\n", cmd);
1661 static void qcom_glink_cancel_rx_work(struct qcom_glink *glink)
1663 struct glink_defer_cmd *dcmd;
1664 struct glink_defer_cmd *tmp;
1666 /* cancel any pending deferred rx_work */
1667 cancel_work_sync(&glink->rx_work);
1669 list_for_each_entry_safe(dcmd, tmp, &glink->rx_queue, node)
1673 static ssize_t rpmsg_name_show(struct device *dev,
1674 struct device_attribute *attr, char *buf)
1679 ret = of_property_read_string(dev->of_node, "label", &name);
1681 name = dev->of_node->name;
1683 return sysfs_emit(buf, "%s\n", name);
1685 static DEVICE_ATTR_RO(rpmsg_name);
1687 static struct attribute *qcom_glink_attrs[] = {
1688 &dev_attr_rpmsg_name.attr,
1691 ATTRIBUTE_GROUPS(qcom_glink);
1693 static void qcom_glink_device_release(struct device *dev)
1695 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1696 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1698 /* Release qcom_glink_alloc_channel() reference */
1699 kref_put(&channel->refcount, qcom_glink_channel_release);
1703 static int qcom_glink_create_chrdev(struct qcom_glink *glink)
1705 struct rpmsg_device *rpdev;
1706 struct glink_channel *channel;
1708 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1712 channel = qcom_glink_alloc_channel(glink, "rpmsg_chrdev");
1713 if (IS_ERR(channel)) {
1715 return PTR_ERR(channel);
1717 channel->rpdev = rpdev;
1719 rpdev->ept = &channel->ept;
1720 rpdev->ops = &glink_device_ops;
1721 rpdev->dev.parent = glink->dev;
1722 rpdev->dev.release = qcom_glink_device_release;
1724 return rpmsg_ctrldev_register_device(rpdev);
1727 struct qcom_glink *qcom_glink_native_probe(struct device *dev,
1728 unsigned long features,
1729 struct qcom_glink_pipe *rx,
1730 struct qcom_glink_pipe *tx,
1734 struct qcom_glink *glink;
1736 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1738 return ERR_PTR(-ENOMEM);
1741 glink->tx_pipe = tx;
1742 glink->rx_pipe = rx;
1744 glink->features = features;
1745 glink->intentless = intentless;
1747 spin_lock_init(&glink->tx_lock);
1748 spin_lock_init(&glink->rx_lock);
1749 INIT_LIST_HEAD(&glink->rx_queue);
1750 INIT_WORK(&glink->rx_work, qcom_glink_work);
1751 init_waitqueue_head(&glink->tx_avail_notify);
1753 spin_lock_init(&glink->idr_lock);
1754 idr_init(&glink->lcids);
1755 idr_init(&glink->rcids);
1757 glink->dev->groups = qcom_glink_groups;
1759 ret = device_add_groups(dev, qcom_glink_groups);
1761 dev_err(dev, "failed to add groups\n");
1763 ret = qcom_glink_send_version(glink);
1765 return ERR_PTR(ret);
1767 ret = qcom_glink_create_chrdev(glink);
1769 dev_err(glink->dev, "failed to register chrdev\n");
1773 EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
1775 static int qcom_glink_remove_device(struct device *dev, void *data)
1777 device_unregister(dev);
1782 void qcom_glink_native_remove(struct qcom_glink *glink)
1784 struct glink_channel *channel;
1785 unsigned long flags;
1789 qcom_glink_cancel_rx_work(glink);
1791 /* Fail all attempts at sending messages */
1792 spin_lock_irqsave(&glink->tx_lock, flags);
1793 glink->abort_tx = true;
1794 wake_up_all(&glink->tx_avail_notify);
1795 spin_unlock_irqrestore(&glink->tx_lock, flags);
1797 /* Abort any senders waiting for intent requests */
1798 spin_lock_irqsave(&glink->idr_lock, flags);
1799 idr_for_each_entry(&glink->lcids, channel, cid)
1800 qcom_glink_intent_req_abort(channel);
1801 spin_unlock_irqrestore(&glink->idr_lock, flags);
1803 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1805 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1807 /* Release any defunct local channels, waiting for close-ack */
1808 idr_for_each_entry(&glink->lcids, channel, cid)
1809 kref_put(&channel->refcount, qcom_glink_channel_release);
1811 /* Release any defunct local channels, waiting for close-req */
1812 idr_for_each_entry(&glink->rcids, channel, cid)
1813 kref_put(&channel->refcount, qcom_glink_channel_release);
1815 idr_destroy(&glink->lcids);
1816 idr_destroy(&glink->rcids);
1818 EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
1820 MODULE_DESCRIPTION("Qualcomm GLINK driver");
1821 MODULE_LICENSE("GPL v2");