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/wait.h>
20 #include <linux/workqueue.h>
21 #include <linux/mailbox_client.h>
23 #include "rpmsg_internal.h"
24 #include "qcom_glink_native.h"
26 #define GLINK_NAME_SIZE 32
27 #define GLINK_VERSION_1 1
29 #define RPM_GLINK_CID_MIN 1
30 #define RPM_GLINK_CID_MAX 65536
40 * struct glink_defer_cmd - deferred incoming control message
42 * @msg: message header
43 * @data: payload of the message
45 * Copy of a received control message, to be added to @rx_queue and processed
46 * by @rx_work of @qcom_glink.
48 struct glink_defer_cmd {
49 struct list_head node;
56 * struct glink_core_rx_intent - RX intent
59 * @data: pointer to the data (may be NULL for zero-copy)
60 * @id: remote or local intent ID
61 * @size: size of the original intent (do not modify)
62 * @reuse: To mark if the intent can be reused after first use
63 * @in_use: To mark if intent is already in use for the channel
64 * @offset: next write offset (initially 0)
67 struct glink_core_rx_intent {
75 struct list_head node;
79 * struct qcom_glink - driver context, relates to one remote subsystem
80 * @dev: reference to the associated struct device
81 * @rx_pipe: pipe object for receive FIFO
82 * @tx_pipe: pipe object for transmit FIFO
83 * @rx_work: worker for handling received control messages
84 * @rx_lock: protects the @rx_queue
85 * @rx_queue: queue of received control messages to be processed in @rx_work
86 * @tx_lock: synchronizes operations on the tx fifo
87 * @idr_lock: synchronizes @lcids and @rcids modifications
88 * @lcids: idr of all channels with a known local channel id
89 * @rcids: idr of all channels with a known remote channel id
90 * @features: remote features
91 * @intentless: flag to indicate that there is no intent
92 * @tx_avail_notify: Waitqueue for pending tx tasks
93 * @sent_read_notify: flag to check cmd sent or not
94 * @abort_tx: flag indicating that all tx attempts should fail
99 struct qcom_glink_pipe *rx_pipe;
100 struct qcom_glink_pipe *tx_pipe;
102 struct work_struct rx_work;
104 struct list_head rx_queue;
111 unsigned long features;
114 wait_queue_head_t tx_avail_notify;
115 bool sent_read_notify;
128 * struct glink_channel - internal representation of a channel
129 * @rpdev: rpdev reference, only used for primary endpoints
130 * @ept: rpmsg endpoint this channel is associated with
131 * @glink: qcom_glink context handle
132 * @refcount: refcount for the channel object
133 * @recv_lock: guard for @ept.cb
134 * @name: unique channel name/identifier
135 * @lcid: channel id, in local space
136 * @rcid: channel id, in remote space
137 * @intent_lock: lock for protection of @liids, @riids
138 * @liids: idr of all local intents
139 * @riids: idr of all remote intents
140 * @intent_work: worker responsible for transmitting rx_done packets
141 * @done_intents: list of intents that needs to be announced rx_done
142 * @buf: receive buffer, for gathering fragments
143 * @buf_offset: write offset in @buf
144 * @buf_size: size of current @buf
145 * @open_ack: completed once remote has acked the open-request
146 * @open_req: completed once open-request has been received
147 * @intent_req_lock: Synchronises multiple intent requests
148 * @intent_req_result: Result of intent request
149 * @intent_received: flag indicating that an intent has been received
150 * @intent_req_wq: wait queue for intent_req signalling
152 struct glink_channel {
153 struct rpmsg_endpoint ept;
155 struct rpmsg_device *rpdev;
156 struct qcom_glink *glink;
158 struct kref refcount;
160 spinlock_t recv_lock;
166 spinlock_t intent_lock;
169 struct work_struct intent_work;
170 struct list_head done_intents;
172 struct glink_core_rx_intent *buf;
176 struct completion open_ack;
177 struct completion open_req;
179 struct mutex intent_req_lock;
180 int intent_req_result;
181 bool intent_received;
182 wait_queue_head_t intent_req_wq;
185 #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
187 static const struct rpmsg_endpoint_ops glink_endpoint_ops;
189 #define GLINK_CMD_VERSION 0
190 #define GLINK_CMD_VERSION_ACK 1
191 #define GLINK_CMD_OPEN 2
192 #define GLINK_CMD_CLOSE 3
193 #define GLINK_CMD_OPEN_ACK 4
194 #define GLINK_CMD_INTENT 5
195 #define GLINK_CMD_RX_DONE 6
196 #define GLINK_CMD_RX_INTENT_REQ 7
197 #define GLINK_CMD_RX_INTENT_REQ_ACK 8
198 #define GLINK_CMD_TX_DATA 9
199 #define GLINK_CMD_CLOSE_ACK 11
200 #define GLINK_CMD_TX_DATA_CONT 12
201 #define GLINK_CMD_READ_NOTIF 13
202 #define GLINK_CMD_RX_DONE_W_REUSE 14
204 #define GLINK_FEATURE_INTENTLESS BIT(1)
206 static void qcom_glink_rx_done_work(struct work_struct *work);
208 static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
211 struct glink_channel *channel;
213 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
215 return ERR_PTR(-ENOMEM);
217 /* Setup glink internal glink_channel data */
218 spin_lock_init(&channel->recv_lock);
219 spin_lock_init(&channel->intent_lock);
220 mutex_init(&channel->intent_req_lock);
222 channel->glink = glink;
223 channel->name = kstrdup(name, GFP_KERNEL);
225 init_completion(&channel->open_req);
226 init_completion(&channel->open_ack);
227 init_waitqueue_head(&channel->intent_req_wq);
229 INIT_LIST_HEAD(&channel->done_intents);
230 INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
232 idr_init(&channel->liids);
233 idr_init(&channel->riids);
234 kref_init(&channel->refcount);
239 static void qcom_glink_channel_release(struct kref *ref)
241 struct glink_channel *channel = container_of(ref, struct glink_channel,
243 struct glink_core_rx_intent *intent;
244 struct glink_core_rx_intent *tmp;
248 /* cancel pending rx_done work */
249 cancel_work_sync(&channel->intent_work);
251 spin_lock_irqsave(&channel->intent_lock, flags);
252 /* Free all non-reuse intents pending rx_done work */
253 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
254 if (!intent->reuse) {
260 idr_for_each_entry(&channel->liids, tmp, iid) {
264 idr_destroy(&channel->liids);
266 idr_for_each_entry(&channel->riids, tmp, iid)
268 idr_destroy(&channel->riids);
269 spin_unlock_irqrestore(&channel->intent_lock, flags);
271 kfree(channel->name);
275 static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
277 return glink->rx_pipe->avail(glink->rx_pipe);
280 static void qcom_glink_rx_peek(struct qcom_glink *glink,
281 void *data, unsigned int offset, size_t count)
283 glink->rx_pipe->peek(glink->rx_pipe, data, offset, count);
286 static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
288 glink->rx_pipe->advance(glink->rx_pipe, count);
291 static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
293 return glink->tx_pipe->avail(glink->tx_pipe);
296 static void qcom_glink_tx_write(struct qcom_glink *glink,
297 const void *hdr, size_t hlen,
298 const void *data, size_t dlen)
300 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
303 static void qcom_glink_tx_kick(struct qcom_glink *glink)
305 glink->tx_pipe->kick(glink->tx_pipe);
308 static void qcom_glink_send_read_notify(struct qcom_glink *glink)
310 struct glink_msg msg;
312 msg.cmd = cpu_to_le16(GLINK_CMD_READ_NOTIF);
316 qcom_glink_tx_write(glink, &msg, sizeof(msg), NULL, 0);
318 qcom_glink_tx_kick(glink);
321 static int qcom_glink_tx(struct qcom_glink *glink,
322 const void *hdr, size_t hlen,
323 const void *data, size_t dlen, bool wait)
325 unsigned int tlen = hlen + dlen;
329 /* Reject packets that are too big */
330 if (tlen >= glink->tx_pipe->length)
333 spin_lock_irqsave(&glink->tx_lock, flags);
335 if (glink->abort_tx) {
340 while (qcom_glink_tx_avail(glink) < tlen) {
346 if (glink->abort_tx) {
351 if (!glink->sent_read_notify) {
352 glink->sent_read_notify = true;
353 qcom_glink_send_read_notify(glink);
356 /* Wait without holding the tx_lock */
357 spin_unlock_irqrestore(&glink->tx_lock, flags);
359 wait_event_timeout(glink->tx_avail_notify,
360 qcom_glink_tx_avail(glink) >= tlen, 10 * HZ);
362 spin_lock_irqsave(&glink->tx_lock, flags);
364 if (qcom_glink_tx_avail(glink) >= tlen)
365 glink->sent_read_notify = false;
368 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
369 qcom_glink_tx_kick(glink);
372 spin_unlock_irqrestore(&glink->tx_lock, flags);
377 static int qcom_glink_send_version(struct qcom_glink *glink)
379 struct glink_msg msg;
381 msg.cmd = cpu_to_le16(GLINK_CMD_VERSION);
382 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
383 msg.param2 = cpu_to_le32(glink->features);
385 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
388 static void qcom_glink_send_version_ack(struct qcom_glink *glink)
390 struct glink_msg msg;
392 msg.cmd = cpu_to_le16(GLINK_CMD_VERSION_ACK);
393 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
394 msg.param2 = cpu_to_le32(glink->features);
396 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
399 static void qcom_glink_send_open_ack(struct qcom_glink *glink,
400 struct glink_channel *channel)
402 struct glink_msg msg;
404 msg.cmd = cpu_to_le16(GLINK_CMD_OPEN_ACK);
405 msg.param1 = cpu_to_le16(channel->rcid);
406 msg.param2 = cpu_to_le32(0);
408 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
411 static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
412 unsigned int cid, bool granted)
414 struct glink_channel *channel;
417 spin_lock_irqsave(&glink->idr_lock, flags);
418 channel = idr_find(&glink->rcids, cid);
419 spin_unlock_irqrestore(&glink->idr_lock, flags);
421 dev_err(glink->dev, "unable to find channel\n");
425 WRITE_ONCE(channel->intent_req_result, granted);
426 wake_up_all(&channel->intent_req_wq);
429 static void qcom_glink_intent_req_abort(struct glink_channel *channel)
431 WRITE_ONCE(channel->intent_req_result, 0);
432 wake_up_all(&channel->intent_req_wq);
436 * qcom_glink_send_open_req() - send a GLINK_CMD_OPEN request to the remote
437 * @glink: Ptr to the glink edge
438 * @channel: Ptr to the channel that the open req is sent
440 * Allocates a local channel id and sends a GLINK_CMD_OPEN message to the remote.
441 * Will return with refcount held, regardless of outcome.
443 * Return: 0 on success, negative errno otherwise.
445 static int qcom_glink_send_open_req(struct qcom_glink *glink,
446 struct glink_channel *channel)
449 struct glink_msg msg;
450 u8 name[GLINK_NAME_SIZE];
452 int name_len = strlen(channel->name) + 1;
453 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
457 kref_get(&channel->refcount);
459 spin_lock_irqsave(&glink->idr_lock, flags);
460 ret = idr_alloc_cyclic(&glink->lcids, channel,
461 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
463 spin_unlock_irqrestore(&glink->idr_lock, flags);
469 req.msg.cmd = cpu_to_le16(GLINK_CMD_OPEN);
470 req.msg.param1 = cpu_to_le16(channel->lcid);
471 req.msg.param2 = cpu_to_le32(name_len);
472 strcpy(req.name, channel->name);
474 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
481 spin_lock_irqsave(&glink->idr_lock, flags);
482 idr_remove(&glink->lcids, channel->lcid);
484 spin_unlock_irqrestore(&glink->idr_lock, flags);
489 static void qcom_glink_send_close_req(struct qcom_glink *glink,
490 struct glink_channel *channel)
492 struct glink_msg req;
494 req.cmd = cpu_to_le16(GLINK_CMD_CLOSE);
495 req.param1 = cpu_to_le16(channel->lcid);
498 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
501 static void qcom_glink_send_close_ack(struct qcom_glink *glink,
504 struct glink_msg req;
506 req.cmd = cpu_to_le16(GLINK_CMD_CLOSE_ACK);
507 req.param1 = cpu_to_le16(rcid);
510 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
513 static void qcom_glink_rx_done_work(struct work_struct *work)
515 struct glink_channel *channel = container_of(work, struct glink_channel,
517 struct qcom_glink *glink = channel->glink;
518 struct glink_core_rx_intent *intent, *tmp;
525 unsigned int cid = channel->lcid;
530 spin_lock_irqsave(&channel->intent_lock, flags);
531 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
532 list_del(&intent->node);
533 spin_unlock_irqrestore(&channel->intent_lock, flags);
535 reuse = intent->reuse;
537 cmd.id = reuse ? GLINK_CMD_RX_DONE_W_REUSE : GLINK_CMD_RX_DONE;
541 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
546 spin_lock_irqsave(&channel->intent_lock, flags);
548 spin_unlock_irqrestore(&channel->intent_lock, flags);
551 static void qcom_glink_rx_done(struct qcom_glink *glink,
552 struct glink_channel *channel,
553 struct glink_core_rx_intent *intent)
555 /* We don't send RX_DONE to intentless systems */
556 if (glink->intentless) {
562 /* Take it off the tree of receive intents */
563 if (!intent->reuse) {
564 spin_lock(&channel->intent_lock);
565 idr_remove(&channel->liids, intent->id);
566 spin_unlock(&channel->intent_lock);
569 /* Schedule the sending of a rx_done indication */
570 spin_lock(&channel->intent_lock);
571 list_add_tail(&intent->node, &channel->done_intents);
572 spin_unlock(&channel->intent_lock);
574 schedule_work(&channel->intent_work);
578 * qcom_glink_receive_version() - receive version/features from remote system
580 * @glink: pointer to transport interface
581 * @version: remote version
582 * @features: remote features
584 * This function is called in response to a remote-initiated version/feature
585 * negotiation sequence.
587 static void qcom_glink_receive_version(struct qcom_glink *glink,
594 case GLINK_VERSION_1:
595 glink->features &= features;
598 qcom_glink_send_version_ack(glink);
604 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
606 * @glink: pointer to transport interface
607 * @version: remote version response
608 * @features: remote features response
610 * This function is called in response to a local-initiated version/feature
611 * negotiation sequence and is the counter-offer from the remote side based
612 * upon the initial version and feature set requested.
614 static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
620 /* Version negotiation failed */
622 case GLINK_VERSION_1:
623 if (features == glink->features)
626 glink->features &= features;
629 qcom_glink_send_version(glink);
635 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
636 * wire format and transmit
637 * @glink: The transport to transmit on.
638 * @channel: The glink channel
639 * @granted: The request response to encode.
641 * Return: 0 on success or standard Linux error code.
643 static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
644 struct glink_channel *channel,
647 struct glink_msg msg;
649 msg.cmd = cpu_to_le16(GLINK_CMD_RX_INTENT_REQ_ACK);
650 msg.param1 = cpu_to_le16(channel->lcid);
651 msg.param2 = cpu_to_le32(granted);
653 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
659 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
661 * @glink: The transport to transmit on.
662 * @channel: The local channel
663 * @intent: The intent to pass on to remote.
665 * Return: 0 on success or standard Linux error code.
667 static int qcom_glink_advertise_intent(struct qcom_glink *glink,
668 struct glink_channel *channel,
669 struct glink_core_rx_intent *intent)
680 cmd.id = cpu_to_le16(GLINK_CMD_INTENT);
681 cmd.lcid = cpu_to_le16(channel->lcid);
682 cmd.count = cpu_to_le32(1);
683 cmd.size = cpu_to_le32(intent->size);
684 cmd.liid = cpu_to_le32(intent->id);
686 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
691 static struct glink_core_rx_intent *
692 qcom_glink_alloc_intent(struct qcom_glink *glink,
693 struct glink_channel *channel,
697 struct glink_core_rx_intent *intent;
701 intent = kzalloc(sizeof(*intent), GFP_KERNEL);
705 intent->data = kzalloc(size, GFP_KERNEL);
709 spin_lock_irqsave(&channel->intent_lock, flags);
710 ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
712 spin_unlock_irqrestore(&channel->intent_lock, flags);
715 spin_unlock_irqrestore(&channel->intent_lock, flags);
719 intent->reuse = reuseable;
730 static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
731 u32 cid, uint32_t iid,
734 struct glink_core_rx_intent *intent;
735 struct glink_channel *channel;
738 spin_lock_irqsave(&glink->idr_lock, flags);
739 channel = idr_find(&glink->rcids, cid);
740 spin_unlock_irqrestore(&glink->idr_lock, flags);
742 dev_err(glink->dev, "invalid channel id received\n");
746 spin_lock_irqsave(&channel->intent_lock, flags);
747 intent = idr_find(&channel->riids, iid);
750 spin_unlock_irqrestore(&channel->intent_lock, flags);
751 dev_err(glink->dev, "invalid intent id received\n");
755 intent->in_use = false;
758 idr_remove(&channel->riids, intent->id);
761 spin_unlock_irqrestore(&channel->intent_lock, flags);
764 WRITE_ONCE(channel->intent_received, true);
765 wake_up_all(&channel->intent_req_wq);
770 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
772 * @glink: Pointer to the transport interface
773 * @cid: Remote channel ID
774 * @size: size of the intent
776 * The function searches for the local channel to which the request for
777 * rx_intent has arrived and allocates and notifies the remote back
779 static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
780 u32 cid, size_t size)
782 struct glink_core_rx_intent *intent;
783 struct glink_channel *channel;
786 spin_lock_irqsave(&glink->idr_lock, flags);
787 channel = idr_find(&glink->rcids, cid);
788 spin_unlock_irqrestore(&glink->idr_lock, flags);
791 pr_err("%s channel not found for cid %d\n", __func__, cid);
795 intent = qcom_glink_alloc_intent(glink, channel, size, false);
797 qcom_glink_advertise_intent(glink, channel, intent);
799 qcom_glink_send_intent_req_ack(glink, channel, !!intent);
802 static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
804 struct glink_defer_cmd *dcmd;
806 extra = ALIGN(extra, 8);
808 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
809 dev_dbg(glink->dev, "Insufficient data in rx fifo");
813 dcmd = kzalloc(struct_size(dcmd, data, extra), GFP_ATOMIC);
817 INIT_LIST_HEAD(&dcmd->node);
819 qcom_glink_rx_peek(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
821 spin_lock(&glink->rx_lock);
822 list_add_tail(&dcmd->node, &glink->rx_queue);
823 spin_unlock(&glink->rx_lock);
825 schedule_work(&glink->rx_work);
826 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
831 static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
833 struct glink_core_rx_intent *intent;
834 struct glink_channel *channel;
836 struct glink_msg msg;
840 unsigned int chunk_size;
841 unsigned int left_size;
847 if (avail < sizeof(hdr)) {
848 dev_dbg(glink->dev, "Not enough data in fifo\n");
852 qcom_glink_rx_peek(glink, &hdr, 0, sizeof(hdr));
853 chunk_size = le32_to_cpu(hdr.chunk_size);
854 left_size = le32_to_cpu(hdr.left_size);
856 if (avail < sizeof(hdr) + chunk_size) {
857 dev_dbg(glink->dev, "Payload not yet in fifo\n");
861 rcid = le16_to_cpu(hdr.msg.param1);
862 spin_lock_irqsave(&glink->idr_lock, flags);
863 channel = idr_find(&glink->rcids, rcid);
864 spin_unlock_irqrestore(&glink->idr_lock, flags);
866 dev_dbg(glink->dev, "Data on non-existing channel\n");
868 /* Drop the message */
872 if (glink->intentless) {
873 /* Might have an ongoing, fragmented, message to append */
875 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
879 intent->data = kmalloc(chunk_size + left_size,
886 intent->id = 0xbabababa;
887 intent->size = chunk_size + left_size;
890 channel->buf = intent;
892 intent = channel->buf;
895 liid = le32_to_cpu(hdr.msg.param2);
897 spin_lock_irqsave(&channel->intent_lock, flags);
898 intent = idr_find(&channel->liids, liid);
899 spin_unlock_irqrestore(&channel->intent_lock, flags);
903 "no intent found for channel %s intent %d",
904 channel->name, liid);
910 if (intent->size - intent->offset < chunk_size) {
911 dev_err(glink->dev, "Insufficient space in intent\n");
913 /* The packet header lied, drop payload */
917 qcom_glink_rx_peek(glink, intent->data + intent->offset,
918 sizeof(hdr), chunk_size);
919 intent->offset += chunk_size;
921 /* Handle message when no fragments remain to be received */
923 spin_lock(&channel->recv_lock);
924 if (channel->ept.cb) {
925 channel->ept.cb(channel->ept.rpdev,
931 spin_unlock(&channel->recv_lock);
936 qcom_glink_rx_done(glink, channel, intent);
940 qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
945 static void qcom_glink_handle_intent(struct qcom_glink *glink,
950 struct glink_core_rx_intent *intent;
951 struct glink_channel *channel;
958 struct glink_msg msg;
959 struct intent_pair intents[];
962 const size_t msglen = struct_size(msg, intents, count);
967 if (avail < msglen) {
968 dev_dbg(glink->dev, "Not enough data in fifo\n");
972 spin_lock_irqsave(&glink->idr_lock, flags);
973 channel = idr_find(&glink->rcids, cid);
974 spin_unlock_irqrestore(&glink->idr_lock, flags);
976 dev_err(glink->dev, "intents for non-existing channel\n");
977 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
981 msg = kmalloc(msglen, GFP_ATOMIC);
985 qcom_glink_rx_peek(glink, msg, 0, msglen);
987 for (i = 0; i < count; ++i) {
988 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
992 intent->id = le32_to_cpu(msg->intents[i].iid);
993 intent->size = le32_to_cpu(msg->intents[i].size);
995 spin_lock_irqsave(&channel->intent_lock, flags);
996 ret = idr_alloc(&channel->riids, intent,
997 intent->id, intent->id + 1, GFP_ATOMIC);
998 spin_unlock_irqrestore(&channel->intent_lock, flags);
1001 dev_err(glink->dev, "failed to store remote intent\n");
1004 WRITE_ONCE(channel->intent_received, true);
1005 wake_up_all(&channel->intent_req_wq);
1008 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
1011 static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
1013 struct glink_channel *channel;
1015 spin_lock(&glink->idr_lock);
1016 channel = idr_find(&glink->lcids, lcid);
1017 spin_unlock(&glink->idr_lock);
1019 dev_err(glink->dev, "Invalid open ack packet\n");
1023 complete_all(&channel->open_ack);
1028 void qcom_glink_native_rx(struct qcom_glink *glink)
1030 struct glink_msg msg;
1031 unsigned int param1;
1032 unsigned int param2;
1037 /* To wakeup any blocking writers */
1038 wake_up_all(&glink->tx_avail_notify);
1041 avail = qcom_glink_rx_avail(glink);
1042 if (avail < sizeof(msg))
1045 qcom_glink_rx_peek(glink, &msg, 0, sizeof(msg));
1047 cmd = le16_to_cpu(msg.cmd);
1048 param1 = le16_to_cpu(msg.param1);
1049 param2 = le32_to_cpu(msg.param2);
1052 case GLINK_CMD_VERSION:
1053 case GLINK_CMD_VERSION_ACK:
1054 case GLINK_CMD_CLOSE:
1055 case GLINK_CMD_CLOSE_ACK:
1056 case GLINK_CMD_RX_INTENT_REQ:
1057 ret = qcom_glink_rx_defer(glink, 0);
1059 case GLINK_CMD_OPEN_ACK:
1060 ret = qcom_glink_rx_open_ack(glink, param1);
1061 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1063 case GLINK_CMD_OPEN:
1064 ret = qcom_glink_rx_defer(glink, param2);
1066 case GLINK_CMD_TX_DATA:
1067 case GLINK_CMD_TX_DATA_CONT:
1068 ret = qcom_glink_rx_data(glink, avail);
1070 case GLINK_CMD_READ_NOTIF:
1071 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1072 qcom_glink_tx_kick(glink);
1074 case GLINK_CMD_INTENT:
1075 qcom_glink_handle_intent(glink, param1, param2, avail);
1077 case GLINK_CMD_RX_DONE:
1078 qcom_glink_handle_rx_done(glink, param1, param2, false);
1079 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1081 case GLINK_CMD_RX_DONE_W_REUSE:
1082 qcom_glink_handle_rx_done(glink, param1, param2, true);
1083 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1085 case GLINK_CMD_RX_INTENT_REQ_ACK:
1086 qcom_glink_handle_intent_req_ack(glink, param1, param2);
1087 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1090 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1099 EXPORT_SYMBOL(qcom_glink_native_rx);
1101 /* Locally initiated rpmsg_create_ept */
1102 static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1105 struct glink_channel *channel;
1107 unsigned long flags;
1109 channel = qcom_glink_alloc_channel(glink, name);
1110 if (IS_ERR(channel))
1111 return ERR_CAST(channel);
1113 ret = qcom_glink_send_open_req(glink, channel);
1115 goto release_channel;
1117 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1121 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1125 qcom_glink_send_open_ack(glink, channel);
1130 /* qcom_glink_send_open_req() did register the channel in lcids*/
1131 spin_lock_irqsave(&glink->idr_lock, flags);
1132 idr_remove(&glink->lcids, channel->lcid);
1133 spin_unlock_irqrestore(&glink->idr_lock, flags);
1136 /* Release qcom_glink_send_open_req() reference */
1137 kref_put(&channel->refcount, qcom_glink_channel_release);
1138 /* Release qcom_glink_alloc_channel() reference */
1139 kref_put(&channel->refcount, qcom_glink_channel_release);
1141 return ERR_PTR(-ETIMEDOUT);
1144 /* Remote initiated rpmsg_create_ept */
1145 static int qcom_glink_create_remote(struct qcom_glink *glink,
1146 struct glink_channel *channel)
1150 qcom_glink_send_open_ack(glink, channel);
1152 ret = qcom_glink_send_open_req(glink, channel);
1156 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1166 * Send a close request to "undo" our open-ack. The close-ack will
1167 * release qcom_glink_send_open_req() reference and the last reference
1168 * will be relesed after receiving remote_close or transport unregister
1169 * by calling qcom_glink_native_remove().
1171 qcom_glink_send_close_req(glink, channel);
1176 static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1179 struct rpmsg_channel_info
1182 struct glink_channel *parent = to_glink_channel(rpdev->ept);
1183 struct glink_channel *channel;
1184 struct qcom_glink *glink = parent->glink;
1185 struct rpmsg_endpoint *ept;
1186 const char *name = chinfo.name;
1189 unsigned long flags;
1191 spin_lock_irqsave(&glink->idr_lock, flags);
1192 idr_for_each_entry(&glink->rcids, channel, cid) {
1193 if (!strcmp(channel->name, name))
1196 spin_unlock_irqrestore(&glink->idr_lock, flags);
1199 channel = qcom_glink_create_local(glink, name);
1200 if (IS_ERR(channel))
1203 ret = qcom_glink_create_remote(glink, channel);
1208 ept = &channel->ept;
1212 ept->ops = &glink_endpoint_ops;
1217 static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1219 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1220 struct device_node *np = rpdev->dev.of_node;
1221 struct qcom_glink *glink = channel->glink;
1222 struct glink_core_rx_intent *intent;
1223 const struct property *prop = NULL;
1224 __be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
1227 __be32 *val = defaults;
1230 if (glink->intentless || !completion_done(&channel->open_ack))
1233 prop = of_find_property(np, "qcom,intents", NULL);
1236 num_groups = prop->length / sizeof(u32) / 2;
1239 /* Channel is now open, advertise base set of intents */
1240 while (num_groups--) {
1241 size = be32_to_cpup(val++);
1242 num_intents = be32_to_cpup(val++);
1243 while (num_intents--) {
1244 intent = qcom_glink_alloc_intent(glink, channel, size,
1249 qcom_glink_advertise_intent(glink, channel, intent);
1255 static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1257 struct glink_channel *channel = to_glink_channel(ept);
1258 struct qcom_glink *glink = channel->glink;
1259 unsigned long flags;
1261 spin_lock_irqsave(&channel->recv_lock, flags);
1262 channel->ept.cb = NULL;
1263 spin_unlock_irqrestore(&channel->recv_lock, flags);
1265 /* Decouple the potential rpdev from the channel */
1266 channel->rpdev = NULL;
1268 qcom_glink_send_close_req(glink, channel);
1271 static int qcom_glink_request_intent(struct qcom_glink *glink,
1272 struct glink_channel *channel,
1283 mutex_lock(&channel->intent_req_lock);
1285 WRITE_ONCE(channel->intent_req_result, -1);
1286 WRITE_ONCE(channel->intent_received, false);
1288 cmd.id = GLINK_CMD_RX_INTENT_REQ;
1289 cmd.cid = channel->lcid;
1292 ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1296 ret = wait_event_timeout(channel->intent_req_wq,
1297 READ_ONCE(channel->intent_req_result) >= 0 &&
1298 READ_ONCE(channel->intent_received),
1301 dev_err(glink->dev, "intent request timed out\n");
1304 ret = READ_ONCE(channel->intent_req_result) ? 0 : -ECANCELED;
1308 mutex_unlock(&channel->intent_req_lock);
1312 static int __qcom_glink_send(struct glink_channel *channel,
1313 void *data, int len, bool wait)
1315 struct qcom_glink *glink = channel->glink;
1316 struct glink_core_rx_intent *intent = NULL;
1317 struct glink_core_rx_intent *tmp;
1320 struct glink_msg msg;
1325 unsigned long flags;
1326 int chunk_size = len;
1329 if (!glink->intentless) {
1331 spin_lock_irqsave(&channel->intent_lock, flags);
1332 idr_for_each_entry(&channel->riids, tmp, iid) {
1333 if (tmp->size >= len && !tmp->in_use) {
1336 else if (intent->size > tmp->size)
1338 if (intent->size == len)
1343 intent->in_use = true;
1344 spin_unlock_irqrestore(&channel->intent_lock, flags);
1346 /* We found an available intent */
1353 ret = qcom_glink_request_intent(glink, channel, len);
1361 while (offset < len) {
1362 chunk_size = len - offset;
1363 if (chunk_size > SZ_8K && wait)
1366 req.msg.cmd = cpu_to_le16(offset == 0 ? GLINK_CMD_TX_DATA : GLINK_CMD_TX_DATA_CONT);
1367 req.msg.param1 = cpu_to_le16(channel->lcid);
1368 req.msg.param2 = cpu_to_le32(iid);
1369 req.chunk_size = cpu_to_le32(chunk_size);
1370 req.left_size = cpu_to_le32(len - offset - chunk_size);
1372 ret = qcom_glink_tx(glink, &req, sizeof(req), data + offset, chunk_size, wait);
1374 /* Mark intent available if we failed */
1376 intent->in_use = false;
1380 offset += chunk_size;
1386 static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1388 struct glink_channel *channel = to_glink_channel(ept);
1390 return __qcom_glink_send(channel, data, len, true);
1393 static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1395 struct glink_channel *channel = to_glink_channel(ept);
1397 return __qcom_glink_send(channel, data, len, false);
1400 static int qcom_glink_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1402 struct glink_channel *channel = to_glink_channel(ept);
1404 return __qcom_glink_send(channel, data, len, true);
1407 static int qcom_glink_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1409 struct glink_channel *channel = to_glink_channel(ept);
1411 return __qcom_glink_send(channel, data, len, false);
1415 * Finds the device_node for the glink child interested in this channel.
1417 static struct device_node *qcom_glink_match_channel(struct device_node *node,
1418 const char *channel)
1420 struct device_node *child;
1425 for_each_available_child_of_node(node, child) {
1426 key = "qcom,glink-channels";
1427 ret = of_property_read_string(child, key, &name);
1431 if (strcmp(name, channel) == 0)
1438 static const struct rpmsg_device_ops glink_device_ops = {
1439 .create_ept = qcom_glink_create_ept,
1440 .announce_create = qcom_glink_announce_create,
1443 static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1444 .destroy_ept = qcom_glink_destroy_ept,
1445 .send = qcom_glink_send,
1446 .sendto = qcom_glink_sendto,
1447 .trysend = qcom_glink_trysend,
1448 .trysendto = qcom_glink_trysendto,
1451 static void qcom_glink_rpdev_release(struct device *dev)
1453 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1455 kfree(rpdev->driver_override);
1459 static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1462 struct glink_channel *channel;
1463 struct rpmsg_device *rpdev;
1464 bool create_device = false;
1465 struct device_node *node;
1468 unsigned long flags;
1470 spin_lock_irqsave(&glink->idr_lock, flags);
1471 idr_for_each_entry(&glink->lcids, channel, lcid) {
1472 if (!strcmp(channel->name, name))
1475 spin_unlock_irqrestore(&glink->idr_lock, flags);
1478 channel = qcom_glink_alloc_channel(glink, name);
1479 if (IS_ERR(channel))
1480 return PTR_ERR(channel);
1482 /* The opening dance was initiated by the remote */
1483 create_device = true;
1486 spin_lock_irqsave(&glink->idr_lock, flags);
1487 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
1489 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
1490 spin_unlock_irqrestore(&glink->idr_lock, flags);
1493 channel->rcid = ret;
1494 spin_unlock_irqrestore(&glink->idr_lock, flags);
1496 complete_all(&channel->open_req);
1498 if (create_device) {
1499 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1505 rpdev->ept = &channel->ept;
1506 strscpy_pad(rpdev->id.name, name, RPMSG_NAME_SIZE);
1507 rpdev->src = RPMSG_ADDR_ANY;
1508 rpdev->dst = RPMSG_ADDR_ANY;
1509 rpdev->ops = &glink_device_ops;
1511 node = qcom_glink_match_channel(glink->dev->of_node, name);
1512 rpdev->dev.of_node = node;
1513 rpdev->dev.parent = glink->dev;
1514 rpdev->dev.release = qcom_glink_rpdev_release;
1516 ret = rpmsg_register_device(rpdev);
1520 channel->rpdev = rpdev;
1526 spin_lock_irqsave(&glink->idr_lock, flags);
1527 idr_remove(&glink->rcids, channel->rcid);
1529 spin_unlock_irqrestore(&glink->idr_lock, flags);
1531 /* Release the reference, iff we took it */
1533 kref_put(&channel->refcount, qcom_glink_channel_release);
1538 static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1540 struct rpmsg_channel_info chinfo;
1541 struct glink_channel *channel;
1542 unsigned long flags;
1544 spin_lock_irqsave(&glink->idr_lock, flags);
1545 channel = idr_find(&glink->rcids, rcid);
1546 spin_unlock_irqrestore(&glink->idr_lock, flags);
1547 if (WARN(!channel, "close request on unknown channel\n"))
1550 /* cancel pending rx_done work */
1551 cancel_work_sync(&channel->intent_work);
1553 if (channel->rpdev) {
1554 strscpy_pad(chinfo.name, channel->name, sizeof(chinfo.name));
1555 chinfo.src = RPMSG_ADDR_ANY;
1556 chinfo.dst = RPMSG_ADDR_ANY;
1558 rpmsg_unregister_device(glink->dev, &chinfo);
1560 channel->rpdev = NULL;
1562 qcom_glink_send_close_ack(glink, channel->rcid);
1564 spin_lock_irqsave(&glink->idr_lock, flags);
1565 idr_remove(&glink->rcids, channel->rcid);
1567 spin_unlock_irqrestore(&glink->idr_lock, flags);
1569 kref_put(&channel->refcount, qcom_glink_channel_release);
1572 static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1574 struct rpmsg_channel_info chinfo;
1575 struct glink_channel *channel;
1576 unsigned long flags;
1578 /* To wakeup any blocking writers */
1579 wake_up_all(&glink->tx_avail_notify);
1581 spin_lock_irqsave(&glink->idr_lock, flags);
1582 channel = idr_find(&glink->lcids, lcid);
1583 if (WARN(!channel, "close ack on unknown channel\n")) {
1584 spin_unlock_irqrestore(&glink->idr_lock, flags);
1588 idr_remove(&glink->lcids, channel->lcid);
1590 spin_unlock_irqrestore(&glink->idr_lock, flags);
1592 /* Decouple the potential rpdev from the channel */
1593 if (channel->rpdev) {
1594 strscpy(chinfo.name, channel->name, sizeof(chinfo.name));
1595 chinfo.src = RPMSG_ADDR_ANY;
1596 chinfo.dst = RPMSG_ADDR_ANY;
1598 rpmsg_unregister_device(glink->dev, &chinfo);
1600 channel->rpdev = NULL;
1602 kref_put(&channel->refcount, qcom_glink_channel_release);
1605 static void qcom_glink_work(struct work_struct *work)
1607 struct qcom_glink *glink = container_of(work, struct qcom_glink,
1609 struct glink_defer_cmd *dcmd;
1610 struct glink_msg *msg;
1611 unsigned long flags;
1612 unsigned int param1;
1613 unsigned int param2;
1617 spin_lock_irqsave(&glink->rx_lock, flags);
1618 if (list_empty(&glink->rx_queue)) {
1619 spin_unlock_irqrestore(&glink->rx_lock, flags);
1622 dcmd = list_first_entry(&glink->rx_queue,
1623 struct glink_defer_cmd, node);
1624 list_del(&dcmd->node);
1625 spin_unlock_irqrestore(&glink->rx_lock, flags);
1628 cmd = le16_to_cpu(msg->cmd);
1629 param1 = le16_to_cpu(msg->param1);
1630 param2 = le32_to_cpu(msg->param2);
1633 case GLINK_CMD_VERSION:
1634 qcom_glink_receive_version(glink, param1, param2);
1636 case GLINK_CMD_VERSION_ACK:
1637 qcom_glink_receive_version_ack(glink, param1, param2);
1639 case GLINK_CMD_OPEN:
1640 qcom_glink_rx_open(glink, param1, msg->data);
1642 case GLINK_CMD_CLOSE:
1643 qcom_glink_rx_close(glink, param1);
1645 case GLINK_CMD_CLOSE_ACK:
1646 qcom_glink_rx_close_ack(glink, param1);
1648 case GLINK_CMD_RX_INTENT_REQ:
1649 qcom_glink_handle_intent_req(glink, param1, param2);
1652 WARN(1, "Unknown defer object %d\n", cmd);
1660 static void qcom_glink_cancel_rx_work(struct qcom_glink *glink)
1662 struct glink_defer_cmd *dcmd;
1663 struct glink_defer_cmd *tmp;
1665 /* cancel any pending deferred rx_work */
1666 cancel_work_sync(&glink->rx_work);
1668 list_for_each_entry_safe(dcmd, tmp, &glink->rx_queue, node)
1672 static ssize_t rpmsg_name_show(struct device *dev,
1673 struct device_attribute *attr, char *buf)
1678 ret = of_property_read_string(dev->of_node, "label", &name);
1680 name = dev->of_node->name;
1682 return sysfs_emit(buf, "%s\n", name);
1684 static DEVICE_ATTR_RO(rpmsg_name);
1686 static struct attribute *qcom_glink_attrs[] = {
1687 &dev_attr_rpmsg_name.attr,
1690 ATTRIBUTE_GROUPS(qcom_glink);
1692 static void qcom_glink_device_release(struct device *dev)
1694 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1695 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1697 /* Release qcom_glink_alloc_channel() reference */
1698 kref_put(&channel->refcount, qcom_glink_channel_release);
1699 kfree(rpdev->driver_override);
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");