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
97 struct qcom_glink_pipe *rx_pipe;
98 struct qcom_glink_pipe *tx_pipe;
100 struct work_struct rx_work;
102 struct list_head rx_queue;
109 unsigned long features;
112 wait_queue_head_t tx_avail_notify;
113 bool sent_read_notify;
124 * struct glink_channel - internal representation of a channel
125 * @rpdev: rpdev reference, only used for primary endpoints
126 * @ept: rpmsg endpoint this channel is associated with
127 * @glink: qcom_glink context handle
128 * @refcount: refcount for the channel object
129 * @recv_lock: guard for @ept.cb
130 * @name: unique channel name/identifier
131 * @lcid: channel id, in local space
132 * @rcid: channel id, in remote space
133 * @intent_lock: lock for protection of @liids, @riids
134 * @liids: idr of all local intents
135 * @riids: idr of all remote intents
136 * @intent_work: worker responsible for transmitting rx_done packets
137 * @done_intents: list of intents that needs to be announced rx_done
138 * @buf: receive buffer, for gathering fragments
139 * @buf_offset: write offset in @buf
140 * @buf_size: size of current @buf
141 * @open_ack: completed once remote has acked the open-request
142 * @open_req: completed once open-request has been received
143 * @intent_req_lock: Synchronises multiple intent requests
144 * @intent_req_result: Result of intent request
145 * @intent_req_comp: Completion for intent_req signalling
147 struct glink_channel {
148 struct rpmsg_endpoint ept;
150 struct rpmsg_device *rpdev;
151 struct qcom_glink *glink;
153 struct kref refcount;
155 spinlock_t recv_lock;
161 spinlock_t intent_lock;
164 struct work_struct intent_work;
165 struct list_head done_intents;
167 struct glink_core_rx_intent *buf;
171 struct completion open_ack;
172 struct completion open_req;
174 struct mutex intent_req_lock;
175 bool intent_req_result;
176 struct completion intent_req_comp;
179 #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
181 static const struct rpmsg_endpoint_ops glink_endpoint_ops;
183 #define RPM_CMD_VERSION 0
184 #define RPM_CMD_VERSION_ACK 1
185 #define RPM_CMD_OPEN 2
186 #define RPM_CMD_CLOSE 3
187 #define RPM_CMD_OPEN_ACK 4
188 #define RPM_CMD_INTENT 5
189 #define RPM_CMD_RX_DONE 6
190 #define RPM_CMD_RX_INTENT_REQ 7
191 #define RPM_CMD_RX_INTENT_REQ_ACK 8
192 #define RPM_CMD_TX_DATA 9
193 #define RPM_CMD_CLOSE_ACK 11
194 #define RPM_CMD_TX_DATA_CONT 12
195 #define RPM_CMD_READ_NOTIF 13
196 #define RPM_CMD_RX_DONE_W_REUSE 14
198 #define GLINK_FEATURE_INTENTLESS BIT(1)
200 static void qcom_glink_rx_done_work(struct work_struct *work);
202 static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
205 struct glink_channel *channel;
207 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
209 return ERR_PTR(-ENOMEM);
211 /* Setup glink internal glink_channel data */
212 spin_lock_init(&channel->recv_lock);
213 spin_lock_init(&channel->intent_lock);
214 mutex_init(&channel->intent_req_lock);
216 channel->glink = glink;
217 channel->name = kstrdup(name, GFP_KERNEL);
219 init_completion(&channel->open_req);
220 init_completion(&channel->open_ack);
221 init_completion(&channel->intent_req_comp);
223 INIT_LIST_HEAD(&channel->done_intents);
224 INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
226 idr_init(&channel->liids);
227 idr_init(&channel->riids);
228 kref_init(&channel->refcount);
233 static void qcom_glink_channel_release(struct kref *ref)
235 struct glink_channel *channel = container_of(ref, struct glink_channel,
237 struct glink_core_rx_intent *intent;
238 struct glink_core_rx_intent *tmp;
242 /* cancel pending rx_done work */
243 cancel_work_sync(&channel->intent_work);
245 spin_lock_irqsave(&channel->intent_lock, flags);
246 /* Free all non-reuse intents pending rx_done work */
247 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
248 if (!intent->reuse) {
254 idr_for_each_entry(&channel->liids, tmp, iid) {
258 idr_destroy(&channel->liids);
260 idr_for_each_entry(&channel->riids, tmp, iid)
262 idr_destroy(&channel->riids);
263 spin_unlock_irqrestore(&channel->intent_lock, flags);
265 kfree(channel->name);
269 static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
271 return glink->rx_pipe->avail(glink->rx_pipe);
274 static void qcom_glink_rx_peak(struct qcom_glink *glink,
275 void *data, unsigned int offset, size_t count)
277 glink->rx_pipe->peak(glink->rx_pipe, data, offset, count);
280 static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
282 glink->rx_pipe->advance(glink->rx_pipe, count);
285 static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
287 return glink->tx_pipe->avail(glink->tx_pipe);
290 static void qcom_glink_tx_write(struct qcom_glink *glink,
291 const void *hdr, size_t hlen,
292 const void *data, size_t dlen)
294 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
297 static void qcom_glink_tx_kick(struct qcom_glink *glink)
299 glink->tx_pipe->kick(glink->tx_pipe);
302 static void qcom_glink_send_read_notify(struct qcom_glink *glink)
304 struct glink_msg msg;
306 msg.cmd = cpu_to_le16(RPM_CMD_READ_NOTIF);
310 qcom_glink_tx_write(glink, &msg, sizeof(msg), NULL, 0);
312 qcom_glink_tx_kick(glink);
315 static int qcom_glink_tx(struct qcom_glink *glink,
316 const void *hdr, size_t hlen,
317 const void *data, size_t dlen, bool wait)
319 unsigned int tlen = hlen + dlen;
323 /* Reject packets that are too big */
324 if (tlen >= glink->tx_pipe->length)
327 spin_lock_irqsave(&glink->tx_lock, flags);
329 while (qcom_glink_tx_avail(glink) < tlen) {
335 if (!glink->sent_read_notify) {
336 glink->sent_read_notify = true;
337 qcom_glink_send_read_notify(glink);
340 /* Wait without holding the tx_lock */
341 spin_unlock_irqrestore(&glink->tx_lock, flags);
343 wait_event_timeout(glink->tx_avail_notify,
344 qcom_glink_tx_avail(glink) >= tlen, 10 * HZ);
346 spin_lock_irqsave(&glink->tx_lock, flags);
348 if (qcom_glink_tx_avail(glink) >= tlen)
349 glink->sent_read_notify = false;
352 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
353 qcom_glink_tx_kick(glink);
356 spin_unlock_irqrestore(&glink->tx_lock, flags);
361 static int qcom_glink_send_version(struct qcom_glink *glink)
363 struct glink_msg msg;
365 msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
366 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
367 msg.param2 = cpu_to_le32(glink->features);
369 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
372 static void qcom_glink_send_version_ack(struct qcom_glink *glink)
374 struct glink_msg msg;
376 msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
377 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
378 msg.param2 = cpu_to_le32(glink->features);
380 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
383 static void qcom_glink_send_open_ack(struct qcom_glink *glink,
384 struct glink_channel *channel)
386 struct glink_msg msg;
388 msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
389 msg.param1 = cpu_to_le16(channel->rcid);
390 msg.param2 = cpu_to_le32(0);
392 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
395 static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
396 unsigned int cid, bool granted)
398 struct glink_channel *channel;
401 spin_lock_irqsave(&glink->idr_lock, flags);
402 channel = idr_find(&glink->rcids, cid);
403 spin_unlock_irqrestore(&glink->idr_lock, flags);
405 dev_err(glink->dev, "unable to find channel\n");
409 channel->intent_req_result = granted;
410 complete(&channel->intent_req_comp);
414 * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
415 * @glink: Ptr to the glink edge
416 * @channel: Ptr to the channel that the open req is sent
418 * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
419 * Will return with refcount held, regardless of outcome.
421 * Return: 0 on success, negative errno otherwise.
423 static int qcom_glink_send_open_req(struct qcom_glink *glink,
424 struct glink_channel *channel)
427 struct glink_msg msg;
428 u8 name[GLINK_NAME_SIZE];
430 int name_len = strlen(channel->name) + 1;
431 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
435 kref_get(&channel->refcount);
437 spin_lock_irqsave(&glink->idr_lock, flags);
438 ret = idr_alloc_cyclic(&glink->lcids, channel,
439 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
441 spin_unlock_irqrestore(&glink->idr_lock, flags);
447 req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
448 req.msg.param1 = cpu_to_le16(channel->lcid);
449 req.msg.param2 = cpu_to_le32(name_len);
450 strcpy(req.name, channel->name);
452 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
459 spin_lock_irqsave(&glink->idr_lock, flags);
460 idr_remove(&glink->lcids, channel->lcid);
462 spin_unlock_irqrestore(&glink->idr_lock, flags);
467 static void qcom_glink_send_close_req(struct qcom_glink *glink,
468 struct glink_channel *channel)
470 struct glink_msg req;
472 req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
473 req.param1 = cpu_to_le16(channel->lcid);
476 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
479 static void qcom_glink_send_close_ack(struct qcom_glink *glink,
482 struct glink_msg req;
484 req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
485 req.param1 = cpu_to_le16(rcid);
488 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
491 static void qcom_glink_rx_done_work(struct work_struct *work)
493 struct glink_channel *channel = container_of(work, struct glink_channel,
495 struct qcom_glink *glink = channel->glink;
496 struct glink_core_rx_intent *intent, *tmp;
503 unsigned int cid = channel->lcid;
508 spin_lock_irqsave(&channel->intent_lock, flags);
509 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
510 list_del(&intent->node);
511 spin_unlock_irqrestore(&channel->intent_lock, flags);
513 reuse = intent->reuse;
515 cmd.id = reuse ? RPM_CMD_RX_DONE_W_REUSE : RPM_CMD_RX_DONE;
519 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
524 spin_lock_irqsave(&channel->intent_lock, flags);
526 spin_unlock_irqrestore(&channel->intent_lock, flags);
529 static void qcom_glink_rx_done(struct qcom_glink *glink,
530 struct glink_channel *channel,
531 struct glink_core_rx_intent *intent)
533 /* We don't send RX_DONE to intentless systems */
534 if (glink->intentless) {
540 /* Take it off the tree of receive intents */
541 if (!intent->reuse) {
542 spin_lock(&channel->intent_lock);
543 idr_remove(&channel->liids, intent->id);
544 spin_unlock(&channel->intent_lock);
547 /* Schedule the sending of a rx_done indication */
548 spin_lock(&channel->intent_lock);
549 list_add_tail(&intent->node, &channel->done_intents);
550 spin_unlock(&channel->intent_lock);
552 schedule_work(&channel->intent_work);
556 * qcom_glink_receive_version() - receive version/features from remote system
558 * @glink: pointer to transport interface
559 * @version: remote version
560 * @features: remote features
562 * This function is called in response to a remote-initiated version/feature
563 * negotiation sequence.
565 static void qcom_glink_receive_version(struct qcom_glink *glink,
572 case GLINK_VERSION_1:
573 glink->features &= features;
576 qcom_glink_send_version_ack(glink);
582 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
584 * @glink: pointer to transport interface
585 * @version: remote version response
586 * @features: remote features response
588 * This function is called in response to a local-initiated version/feature
589 * negotiation sequence and is the counter-offer from the remote side based
590 * upon the initial version and feature set requested.
592 static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
598 /* Version negotiation failed */
600 case GLINK_VERSION_1:
601 if (features == glink->features)
604 glink->features &= features;
607 qcom_glink_send_version(glink);
613 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
614 * wire format and transmit
615 * @glink: The transport to transmit on.
616 * @channel: The glink channel
617 * @granted: The request response to encode.
619 * Return: 0 on success or standard Linux error code.
621 static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
622 struct glink_channel *channel,
625 struct glink_msg msg;
627 msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
628 msg.param1 = cpu_to_le16(channel->lcid);
629 msg.param2 = cpu_to_le32(granted);
631 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
637 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
639 * @glink: The transport to transmit on.
640 * @channel: The local channel
641 * @intent: The intent to pass on to remote.
643 * Return: 0 on success or standard Linux error code.
645 static int qcom_glink_advertise_intent(struct qcom_glink *glink,
646 struct glink_channel *channel,
647 struct glink_core_rx_intent *intent)
658 cmd.id = cpu_to_le16(RPM_CMD_INTENT);
659 cmd.lcid = cpu_to_le16(channel->lcid);
660 cmd.count = cpu_to_le32(1);
661 cmd.size = cpu_to_le32(intent->size);
662 cmd.liid = cpu_to_le32(intent->id);
664 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
669 static struct glink_core_rx_intent *
670 qcom_glink_alloc_intent(struct qcom_glink *glink,
671 struct glink_channel *channel,
675 struct glink_core_rx_intent *intent;
679 intent = kzalloc(sizeof(*intent), GFP_KERNEL);
683 intent->data = kzalloc(size, GFP_KERNEL);
687 spin_lock_irqsave(&channel->intent_lock, flags);
688 ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
690 spin_unlock_irqrestore(&channel->intent_lock, flags);
693 spin_unlock_irqrestore(&channel->intent_lock, flags);
697 intent->reuse = reuseable;
708 static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
709 u32 cid, uint32_t iid,
712 struct glink_core_rx_intent *intent;
713 struct glink_channel *channel;
716 spin_lock_irqsave(&glink->idr_lock, flags);
717 channel = idr_find(&glink->rcids, cid);
718 spin_unlock_irqrestore(&glink->idr_lock, flags);
720 dev_err(glink->dev, "invalid channel id received\n");
724 spin_lock_irqsave(&channel->intent_lock, flags);
725 intent = idr_find(&channel->riids, iid);
728 spin_unlock_irqrestore(&channel->intent_lock, flags);
729 dev_err(glink->dev, "invalid intent id received\n");
733 intent->in_use = false;
736 idr_remove(&channel->riids, intent->id);
739 spin_unlock_irqrestore(&channel->intent_lock, flags);
743 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
745 * @glink: Pointer to the transport interface
746 * @cid: Remote channel ID
747 * @size: size of the intent
749 * The function searches for the local channel to which the request for
750 * rx_intent has arrived and allocates and notifies the remote back
752 static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
753 u32 cid, size_t size)
755 struct glink_core_rx_intent *intent;
756 struct glink_channel *channel;
759 spin_lock_irqsave(&glink->idr_lock, flags);
760 channel = idr_find(&glink->rcids, cid);
761 spin_unlock_irqrestore(&glink->idr_lock, flags);
764 pr_err("%s channel not found for cid %d\n", __func__, cid);
768 intent = qcom_glink_alloc_intent(glink, channel, size, false);
770 qcom_glink_advertise_intent(glink, channel, intent);
772 qcom_glink_send_intent_req_ack(glink, channel, !!intent);
775 static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
777 struct glink_defer_cmd *dcmd;
779 extra = ALIGN(extra, 8);
781 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
782 dev_dbg(glink->dev, "Insufficient data in rx fifo");
786 dcmd = kzalloc(struct_size(dcmd, data, extra), GFP_ATOMIC);
790 INIT_LIST_HEAD(&dcmd->node);
792 qcom_glink_rx_peak(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
794 spin_lock(&glink->rx_lock);
795 list_add_tail(&dcmd->node, &glink->rx_queue);
796 spin_unlock(&glink->rx_lock);
798 schedule_work(&glink->rx_work);
799 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
804 static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
806 struct glink_core_rx_intent *intent;
807 struct glink_channel *channel;
809 struct glink_msg msg;
813 unsigned int chunk_size;
814 unsigned int left_size;
820 if (avail < sizeof(hdr)) {
821 dev_dbg(glink->dev, "Not enough data in fifo\n");
825 qcom_glink_rx_peak(glink, &hdr, 0, sizeof(hdr));
826 chunk_size = le32_to_cpu(hdr.chunk_size);
827 left_size = le32_to_cpu(hdr.left_size);
829 if (avail < sizeof(hdr) + chunk_size) {
830 dev_dbg(glink->dev, "Payload not yet in fifo\n");
834 rcid = le16_to_cpu(hdr.msg.param1);
835 spin_lock_irqsave(&glink->idr_lock, flags);
836 channel = idr_find(&glink->rcids, rcid);
837 spin_unlock_irqrestore(&glink->idr_lock, flags);
839 dev_dbg(glink->dev, "Data on non-existing channel\n");
841 /* Drop the message */
845 if (glink->intentless) {
846 /* Might have an ongoing, fragmented, message to append */
848 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
852 intent->data = kmalloc(chunk_size + left_size,
859 intent->id = 0xbabababa;
860 intent->size = chunk_size + left_size;
863 channel->buf = intent;
865 intent = channel->buf;
868 liid = le32_to_cpu(hdr.msg.param2);
870 spin_lock_irqsave(&channel->intent_lock, flags);
871 intent = idr_find(&channel->liids, liid);
872 spin_unlock_irqrestore(&channel->intent_lock, flags);
876 "no intent found for channel %s intent %d",
877 channel->name, liid);
883 if (intent->size - intent->offset < chunk_size) {
884 dev_err(glink->dev, "Insufficient space in intent\n");
886 /* The packet header lied, drop payload */
890 qcom_glink_rx_peak(glink, intent->data + intent->offset,
891 sizeof(hdr), chunk_size);
892 intent->offset += chunk_size;
894 /* Handle message when no fragments remain to be received */
896 spin_lock(&channel->recv_lock);
897 if (channel->ept.cb) {
898 channel->ept.cb(channel->ept.rpdev,
904 spin_unlock(&channel->recv_lock);
909 qcom_glink_rx_done(glink, channel, intent);
913 qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
918 static void qcom_glink_handle_intent(struct qcom_glink *glink,
923 struct glink_core_rx_intent *intent;
924 struct glink_channel *channel;
931 struct glink_msg msg;
932 struct intent_pair intents[];
935 const size_t msglen = struct_size(msg, intents, count);
940 if (avail < msglen) {
941 dev_dbg(glink->dev, "Not enough data in fifo\n");
945 spin_lock_irqsave(&glink->idr_lock, flags);
946 channel = idr_find(&glink->rcids, cid);
947 spin_unlock_irqrestore(&glink->idr_lock, flags);
949 dev_err(glink->dev, "intents for non-existing channel\n");
953 msg = kmalloc(msglen, GFP_ATOMIC);
957 qcom_glink_rx_peak(glink, msg, 0, msglen);
959 for (i = 0; i < count; ++i) {
960 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
964 intent->id = le32_to_cpu(msg->intents[i].iid);
965 intent->size = le32_to_cpu(msg->intents[i].size);
967 spin_lock_irqsave(&channel->intent_lock, flags);
968 ret = idr_alloc(&channel->riids, intent,
969 intent->id, intent->id + 1, GFP_ATOMIC);
970 spin_unlock_irqrestore(&channel->intent_lock, flags);
973 dev_err(glink->dev, "failed to store remote intent\n");
977 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
980 static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
982 struct glink_channel *channel;
984 spin_lock(&glink->idr_lock);
985 channel = idr_find(&glink->lcids, lcid);
986 spin_unlock(&glink->idr_lock);
988 dev_err(glink->dev, "Invalid open ack packet\n");
992 complete_all(&channel->open_ack);
997 void qcom_glink_native_rx(struct qcom_glink *glink)
999 struct glink_msg msg;
1000 unsigned int param1;
1001 unsigned int param2;
1006 /* To wakeup any blocking writers */
1007 wake_up_all(&glink->tx_avail_notify);
1010 avail = qcom_glink_rx_avail(glink);
1011 if (avail < sizeof(msg))
1014 qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
1016 cmd = le16_to_cpu(msg.cmd);
1017 param1 = le16_to_cpu(msg.param1);
1018 param2 = le32_to_cpu(msg.param2);
1021 case RPM_CMD_VERSION:
1022 case RPM_CMD_VERSION_ACK:
1024 case RPM_CMD_CLOSE_ACK:
1025 case RPM_CMD_RX_INTENT_REQ:
1026 ret = qcom_glink_rx_defer(glink, 0);
1028 case RPM_CMD_OPEN_ACK:
1029 ret = qcom_glink_rx_open_ack(glink, param1);
1030 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1033 ret = qcom_glink_rx_defer(glink, param2);
1035 case RPM_CMD_TX_DATA:
1036 case RPM_CMD_TX_DATA_CONT:
1037 ret = qcom_glink_rx_data(glink, avail);
1039 case RPM_CMD_READ_NOTIF:
1040 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1041 qcom_glink_tx_kick(glink);
1043 case RPM_CMD_INTENT:
1044 qcom_glink_handle_intent(glink, param1, param2, avail);
1046 case RPM_CMD_RX_DONE:
1047 qcom_glink_handle_rx_done(glink, param1, param2, false);
1048 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1050 case RPM_CMD_RX_DONE_W_REUSE:
1051 qcom_glink_handle_rx_done(glink, param1, param2, true);
1052 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1054 case RPM_CMD_RX_INTENT_REQ_ACK:
1055 qcom_glink_handle_intent_req_ack(glink, param1, param2);
1056 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1059 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1068 EXPORT_SYMBOL(qcom_glink_native_rx);
1070 /* Locally initiated rpmsg_create_ept */
1071 static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1074 struct glink_channel *channel;
1076 unsigned long flags;
1078 channel = qcom_glink_alloc_channel(glink, name);
1079 if (IS_ERR(channel))
1080 return ERR_CAST(channel);
1082 ret = qcom_glink_send_open_req(glink, channel);
1084 goto release_channel;
1086 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1090 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1094 qcom_glink_send_open_ack(glink, channel);
1099 /* qcom_glink_send_open_req() did register the channel in lcids*/
1100 spin_lock_irqsave(&glink->idr_lock, flags);
1101 idr_remove(&glink->lcids, channel->lcid);
1102 spin_unlock_irqrestore(&glink->idr_lock, flags);
1105 /* Release qcom_glink_send_open_req() reference */
1106 kref_put(&channel->refcount, qcom_glink_channel_release);
1107 /* Release qcom_glink_alloc_channel() reference */
1108 kref_put(&channel->refcount, qcom_glink_channel_release);
1110 return ERR_PTR(-ETIMEDOUT);
1113 /* Remote initiated rpmsg_create_ept */
1114 static int qcom_glink_create_remote(struct qcom_glink *glink,
1115 struct glink_channel *channel)
1119 qcom_glink_send_open_ack(glink, channel);
1121 ret = qcom_glink_send_open_req(glink, channel);
1125 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1135 * Send a close request to "undo" our open-ack. The close-ack will
1136 * release qcom_glink_send_open_req() reference and the last reference
1137 * will be relesed after receiving remote_close or transport unregister
1138 * by calling qcom_glink_native_remove().
1140 qcom_glink_send_close_req(glink, channel);
1145 static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1148 struct rpmsg_channel_info
1151 struct glink_channel *parent = to_glink_channel(rpdev->ept);
1152 struct glink_channel *channel;
1153 struct qcom_glink *glink = parent->glink;
1154 struct rpmsg_endpoint *ept;
1155 const char *name = chinfo.name;
1158 unsigned long flags;
1160 spin_lock_irqsave(&glink->idr_lock, flags);
1161 idr_for_each_entry(&glink->rcids, channel, cid) {
1162 if (!strcmp(channel->name, name))
1165 spin_unlock_irqrestore(&glink->idr_lock, flags);
1168 channel = qcom_glink_create_local(glink, name);
1169 if (IS_ERR(channel))
1172 ret = qcom_glink_create_remote(glink, channel);
1177 ept = &channel->ept;
1181 ept->ops = &glink_endpoint_ops;
1186 static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1188 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1189 struct device_node *np = rpdev->dev.of_node;
1190 struct qcom_glink *glink = channel->glink;
1191 struct glink_core_rx_intent *intent;
1192 const struct property *prop = NULL;
1193 __be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
1196 __be32 *val = defaults;
1199 if (glink->intentless || !completion_done(&channel->open_ack))
1202 prop = of_find_property(np, "qcom,intents", NULL);
1205 num_groups = prop->length / sizeof(u32) / 2;
1208 /* Channel is now open, advertise base set of intents */
1209 while (num_groups--) {
1210 size = be32_to_cpup(val++);
1211 num_intents = be32_to_cpup(val++);
1212 while (num_intents--) {
1213 intent = qcom_glink_alloc_intent(glink, channel, size,
1218 qcom_glink_advertise_intent(glink, channel, intent);
1224 static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1226 struct glink_channel *channel = to_glink_channel(ept);
1227 struct qcom_glink *glink = channel->glink;
1228 unsigned long flags;
1230 spin_lock_irqsave(&channel->recv_lock, flags);
1231 channel->ept.cb = NULL;
1232 spin_unlock_irqrestore(&channel->recv_lock, flags);
1234 /* Decouple the potential rpdev from the channel */
1235 channel->rpdev = NULL;
1237 qcom_glink_send_close_req(glink, channel);
1240 static int qcom_glink_request_intent(struct qcom_glink *glink,
1241 struct glink_channel *channel,
1252 mutex_lock(&channel->intent_req_lock);
1254 reinit_completion(&channel->intent_req_comp);
1256 cmd.id = RPM_CMD_RX_INTENT_REQ;
1257 cmd.cid = channel->lcid;
1260 ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1264 ret = wait_for_completion_timeout(&channel->intent_req_comp, 10 * HZ);
1266 dev_err(glink->dev, "intent request timed out\n");
1269 ret = channel->intent_req_result ? 0 : -ECANCELED;
1273 mutex_unlock(&channel->intent_req_lock);
1277 static int __qcom_glink_send(struct glink_channel *channel,
1278 void *data, int len, bool wait)
1280 struct qcom_glink *glink = channel->glink;
1281 struct glink_core_rx_intent *intent = NULL;
1282 struct glink_core_rx_intent *tmp;
1285 struct glink_msg msg;
1290 unsigned long flags;
1291 int chunk_size = len;
1294 if (!glink->intentless) {
1296 spin_lock_irqsave(&channel->intent_lock, flags);
1297 idr_for_each_entry(&channel->riids, tmp, iid) {
1298 if (tmp->size >= len && !tmp->in_use) {
1301 else if (intent->size > tmp->size)
1303 if (intent->size == len)
1308 intent->in_use = true;
1309 spin_unlock_irqrestore(&channel->intent_lock, flags);
1311 /* We found an available intent */
1318 ret = qcom_glink_request_intent(glink, channel, len);
1326 if (wait && chunk_size > SZ_8K) {
1328 left_size = len - chunk_size;
1330 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
1331 req.msg.param1 = cpu_to_le16(channel->lcid);
1332 req.msg.param2 = cpu_to_le32(iid);
1333 req.chunk_size = cpu_to_le32(chunk_size);
1334 req.left_size = cpu_to_le32(left_size);
1336 ret = qcom_glink_tx(glink, &req, sizeof(req), data, chunk_size, wait);
1338 /* Mark intent available if we failed */
1339 if (ret && intent) {
1340 intent->in_use = false;
1344 while (left_size > 0) {
1345 data = (void *)((char *)data + chunk_size);
1346 chunk_size = left_size;
1347 if (chunk_size > SZ_8K)
1349 left_size -= chunk_size;
1351 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA_CONT);
1352 req.msg.param1 = cpu_to_le16(channel->lcid);
1353 req.msg.param2 = cpu_to_le32(iid);
1354 req.chunk_size = cpu_to_le32(chunk_size);
1355 req.left_size = cpu_to_le32(left_size);
1357 ret = qcom_glink_tx(glink, &req, sizeof(req), data,
1360 /* Mark intent available if we failed */
1361 if (ret && intent) {
1362 intent->in_use = false;
1369 static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1371 struct glink_channel *channel = to_glink_channel(ept);
1373 return __qcom_glink_send(channel, data, len, true);
1376 static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1378 struct glink_channel *channel = to_glink_channel(ept);
1380 return __qcom_glink_send(channel, data, len, false);
1383 static int qcom_glink_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1385 struct glink_channel *channel = to_glink_channel(ept);
1387 return __qcom_glink_send(channel, data, len, true);
1390 static int qcom_glink_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1392 struct glink_channel *channel = to_glink_channel(ept);
1394 return __qcom_glink_send(channel, data, len, false);
1398 * Finds the device_node for the glink child interested in this channel.
1400 static struct device_node *qcom_glink_match_channel(struct device_node *node,
1401 const char *channel)
1403 struct device_node *child;
1408 for_each_available_child_of_node(node, child) {
1409 key = "qcom,glink-channels";
1410 ret = of_property_read_string(child, key, &name);
1414 if (strcmp(name, channel) == 0)
1421 static const struct rpmsg_device_ops glink_device_ops = {
1422 .create_ept = qcom_glink_create_ept,
1423 .announce_create = qcom_glink_announce_create,
1426 static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1427 .destroy_ept = qcom_glink_destroy_ept,
1428 .send = qcom_glink_send,
1429 .sendto = qcom_glink_sendto,
1430 .trysend = qcom_glink_trysend,
1431 .trysendto = qcom_glink_trysendto,
1434 static void qcom_glink_rpdev_release(struct device *dev)
1436 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1441 static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1444 struct glink_channel *channel;
1445 struct rpmsg_device *rpdev;
1446 bool create_device = false;
1447 struct device_node *node;
1450 unsigned long flags;
1452 spin_lock_irqsave(&glink->idr_lock, flags);
1453 idr_for_each_entry(&glink->lcids, channel, lcid) {
1454 if (!strcmp(channel->name, name))
1457 spin_unlock_irqrestore(&glink->idr_lock, flags);
1460 channel = qcom_glink_alloc_channel(glink, name);
1461 if (IS_ERR(channel))
1462 return PTR_ERR(channel);
1464 /* The opening dance was initiated by the remote */
1465 create_device = true;
1468 spin_lock_irqsave(&glink->idr_lock, flags);
1469 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
1471 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
1472 spin_unlock_irqrestore(&glink->idr_lock, flags);
1475 channel->rcid = ret;
1476 spin_unlock_irqrestore(&glink->idr_lock, flags);
1478 complete_all(&channel->open_req);
1480 if (create_device) {
1481 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1487 rpdev->ept = &channel->ept;
1488 strscpy_pad(rpdev->id.name, name, RPMSG_NAME_SIZE);
1489 rpdev->src = RPMSG_ADDR_ANY;
1490 rpdev->dst = RPMSG_ADDR_ANY;
1491 rpdev->ops = &glink_device_ops;
1493 node = qcom_glink_match_channel(glink->dev->of_node, name);
1494 rpdev->dev.of_node = node;
1495 rpdev->dev.parent = glink->dev;
1496 rpdev->dev.release = qcom_glink_rpdev_release;
1498 ret = rpmsg_register_device(rpdev);
1502 channel->rpdev = rpdev;
1508 spin_lock_irqsave(&glink->idr_lock, flags);
1509 idr_remove(&glink->rcids, channel->rcid);
1511 spin_unlock_irqrestore(&glink->idr_lock, flags);
1513 /* Release the reference, iff we took it */
1515 kref_put(&channel->refcount, qcom_glink_channel_release);
1520 static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1522 struct rpmsg_channel_info chinfo;
1523 struct glink_channel *channel;
1524 unsigned long flags;
1526 spin_lock_irqsave(&glink->idr_lock, flags);
1527 channel = idr_find(&glink->rcids, rcid);
1528 spin_unlock_irqrestore(&glink->idr_lock, flags);
1529 if (WARN(!channel, "close request on unknown channel\n"))
1532 /* cancel pending rx_done work */
1533 cancel_work_sync(&channel->intent_work);
1535 if (channel->rpdev) {
1536 strscpy_pad(chinfo.name, channel->name, sizeof(chinfo.name));
1537 chinfo.src = RPMSG_ADDR_ANY;
1538 chinfo.dst = RPMSG_ADDR_ANY;
1540 rpmsg_unregister_device(glink->dev, &chinfo);
1542 channel->rpdev = NULL;
1544 qcom_glink_send_close_ack(glink, channel->rcid);
1546 spin_lock_irqsave(&glink->idr_lock, flags);
1547 idr_remove(&glink->rcids, channel->rcid);
1549 spin_unlock_irqrestore(&glink->idr_lock, flags);
1551 kref_put(&channel->refcount, qcom_glink_channel_release);
1554 static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1556 struct rpmsg_channel_info chinfo;
1557 struct glink_channel *channel;
1558 unsigned long flags;
1560 /* To wakeup any blocking writers */
1561 wake_up_all(&glink->tx_avail_notify);
1563 spin_lock_irqsave(&glink->idr_lock, flags);
1564 channel = idr_find(&glink->lcids, lcid);
1565 if (WARN(!channel, "close ack on unknown channel\n")) {
1566 spin_unlock_irqrestore(&glink->idr_lock, flags);
1570 idr_remove(&glink->lcids, channel->lcid);
1572 spin_unlock_irqrestore(&glink->idr_lock, flags);
1574 /* Decouple the potential rpdev from the channel */
1575 if (channel->rpdev) {
1576 strscpy(chinfo.name, channel->name, sizeof(chinfo.name));
1577 chinfo.src = RPMSG_ADDR_ANY;
1578 chinfo.dst = RPMSG_ADDR_ANY;
1580 rpmsg_unregister_device(glink->dev, &chinfo);
1582 channel->rpdev = NULL;
1584 kref_put(&channel->refcount, qcom_glink_channel_release);
1587 static void qcom_glink_work(struct work_struct *work)
1589 struct qcom_glink *glink = container_of(work, struct qcom_glink,
1591 struct glink_defer_cmd *dcmd;
1592 struct glink_msg *msg;
1593 unsigned long flags;
1594 unsigned int param1;
1595 unsigned int param2;
1599 spin_lock_irqsave(&glink->rx_lock, flags);
1600 if (list_empty(&glink->rx_queue)) {
1601 spin_unlock_irqrestore(&glink->rx_lock, flags);
1604 dcmd = list_first_entry(&glink->rx_queue,
1605 struct glink_defer_cmd, node);
1606 list_del(&dcmd->node);
1607 spin_unlock_irqrestore(&glink->rx_lock, flags);
1610 cmd = le16_to_cpu(msg->cmd);
1611 param1 = le16_to_cpu(msg->param1);
1612 param2 = le32_to_cpu(msg->param2);
1615 case RPM_CMD_VERSION:
1616 qcom_glink_receive_version(glink, param1, param2);
1618 case RPM_CMD_VERSION_ACK:
1619 qcom_glink_receive_version_ack(glink, param1, param2);
1622 qcom_glink_rx_open(glink, param1, msg->data);
1625 qcom_glink_rx_close(glink, param1);
1627 case RPM_CMD_CLOSE_ACK:
1628 qcom_glink_rx_close_ack(glink, param1);
1630 case RPM_CMD_RX_INTENT_REQ:
1631 qcom_glink_handle_intent_req(glink, param1, param2);
1634 WARN(1, "Unknown defer object %d\n", cmd);
1642 static void qcom_glink_cancel_rx_work(struct qcom_glink *glink)
1644 struct glink_defer_cmd *dcmd;
1645 struct glink_defer_cmd *tmp;
1647 /* cancel any pending deferred rx_work */
1648 cancel_work_sync(&glink->rx_work);
1650 list_for_each_entry_safe(dcmd, tmp, &glink->rx_queue, node)
1654 static ssize_t rpmsg_name_show(struct device *dev,
1655 struct device_attribute *attr, char *buf)
1660 ret = of_property_read_string(dev->of_node, "label", &name);
1662 name = dev->of_node->name;
1664 return sysfs_emit(buf, "%s\n", name);
1666 static DEVICE_ATTR_RO(rpmsg_name);
1668 static struct attribute *qcom_glink_attrs[] = {
1669 &dev_attr_rpmsg_name.attr,
1672 ATTRIBUTE_GROUPS(qcom_glink);
1674 static void qcom_glink_device_release(struct device *dev)
1676 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1677 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1679 /* Release qcom_glink_alloc_channel() reference */
1680 kref_put(&channel->refcount, qcom_glink_channel_release);
1684 static int qcom_glink_create_chrdev(struct qcom_glink *glink)
1686 struct rpmsg_device *rpdev;
1687 struct glink_channel *channel;
1689 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1693 channel = qcom_glink_alloc_channel(glink, "rpmsg_chrdev");
1694 if (IS_ERR(channel)) {
1696 return PTR_ERR(channel);
1698 channel->rpdev = rpdev;
1700 rpdev->ept = &channel->ept;
1701 rpdev->ops = &glink_device_ops;
1702 rpdev->dev.parent = glink->dev;
1703 rpdev->dev.release = qcom_glink_device_release;
1705 return rpmsg_ctrldev_register_device(rpdev);
1708 struct qcom_glink *qcom_glink_native_probe(struct device *dev,
1709 unsigned long features,
1710 struct qcom_glink_pipe *rx,
1711 struct qcom_glink_pipe *tx,
1715 struct qcom_glink *glink;
1717 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1719 return ERR_PTR(-ENOMEM);
1722 glink->tx_pipe = tx;
1723 glink->rx_pipe = rx;
1725 glink->features = features;
1726 glink->intentless = intentless;
1728 spin_lock_init(&glink->tx_lock);
1729 spin_lock_init(&glink->rx_lock);
1730 INIT_LIST_HEAD(&glink->rx_queue);
1731 INIT_WORK(&glink->rx_work, qcom_glink_work);
1732 init_waitqueue_head(&glink->tx_avail_notify);
1734 spin_lock_init(&glink->idr_lock);
1735 idr_init(&glink->lcids);
1736 idr_init(&glink->rcids);
1738 glink->dev->groups = qcom_glink_groups;
1740 ret = device_add_groups(dev, qcom_glink_groups);
1742 dev_err(dev, "failed to add groups\n");
1744 ret = qcom_glink_send_version(glink);
1746 return ERR_PTR(ret);
1748 ret = qcom_glink_create_chrdev(glink);
1750 dev_err(glink->dev, "failed to register chrdev\n");
1754 EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
1756 static int qcom_glink_remove_device(struct device *dev, void *data)
1758 device_unregister(dev);
1763 void qcom_glink_native_remove(struct qcom_glink *glink)
1765 struct glink_channel *channel;
1769 qcom_glink_cancel_rx_work(glink);
1771 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1773 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1775 /* Release any defunct local channels, waiting for close-ack */
1776 idr_for_each_entry(&glink->lcids, channel, cid)
1777 kref_put(&channel->refcount, qcom_glink_channel_release);
1779 /* Release any defunct local channels, waiting for close-req */
1780 idr_for_each_entry(&glink->rcids, channel, cid)
1781 kref_put(&channel->refcount, qcom_glink_channel_release);
1783 idr_destroy(&glink->lcids);
1784 idr_destroy(&glink->rcids);
1786 EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
1788 MODULE_DESCRIPTION("Qualcomm GLINK driver");
1789 MODULE_LICENSE("GPL v2");