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/of_irq.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <linux/rpmsg.h>
18 #include <linux/sizes.h>
19 #include <linux/slab.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 * @mbox_client: mailbox client
82 * @mbox_chan: mailbox channel
83 * @rx_pipe: pipe object for receive FIFO
84 * @tx_pipe: pipe object for transmit FIFO
85 * @irq: IRQ for signaling incoming events
86 * @rx_work: worker for handling received control messages
87 * @rx_lock: protects the @rx_queue
88 * @rx_queue: queue of received control messages to be processed in @rx_work
89 * @tx_lock: synchronizes operations on the tx fifo
90 * @idr_lock: synchronizes @lcids and @rcids modifications
91 * @lcids: idr of all channels with a known local channel id
92 * @rcids: idr of all channels with a known remote channel id
93 * @features: remote features
94 * @intentless: flag to indicate that there is no intent
99 struct mbox_client mbox_client;
100 struct mbox_chan *mbox_chan;
102 struct qcom_glink_pipe *rx_pipe;
103 struct qcom_glink_pipe *tx_pipe;
107 struct work_struct rx_work;
109 struct list_head rx_queue;
116 unsigned long features;
129 * struct glink_channel - internal representation of a channel
130 * @rpdev: rpdev reference, only used for primary endpoints
131 * @ept: rpmsg endpoint this channel is associated with
132 * @glink: qcom_glink context handle
133 * @refcount: refcount for the channel object
134 * @recv_lock: guard for @ept.cb
135 * @name: unique channel name/identifier
136 * @lcid: channel id, in local space
137 * @rcid: channel id, in remote space
138 * @intent_lock: lock for protection of @liids, @riids
139 * @liids: idr of all local intents
140 * @riids: idr of all remote intents
141 * @intent_work: worker responsible for transmitting rx_done packets
142 * @done_intents: list of intents that needs to be announced rx_done
143 * @buf: receive buffer, for gathering fragments
144 * @buf_offset: write offset in @buf
145 * @buf_size: size of current @buf
146 * @open_ack: completed once remote has acked the open-request
147 * @open_req: completed once open-request has been received
148 * @intent_req_lock: Synchronises multiple intent requests
149 * @intent_req_result: Result of intent request
150 * @intent_req_comp: Completion 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 bool intent_req_result;
181 struct completion intent_req_comp;
184 #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
186 static const struct rpmsg_endpoint_ops glink_endpoint_ops;
188 #define RPM_CMD_VERSION 0
189 #define RPM_CMD_VERSION_ACK 1
190 #define RPM_CMD_OPEN 2
191 #define RPM_CMD_CLOSE 3
192 #define RPM_CMD_OPEN_ACK 4
193 #define RPM_CMD_INTENT 5
194 #define RPM_CMD_RX_DONE 6
195 #define RPM_CMD_RX_INTENT_REQ 7
196 #define RPM_CMD_RX_INTENT_REQ_ACK 8
197 #define RPM_CMD_TX_DATA 9
198 #define RPM_CMD_CLOSE_ACK 11
199 #define RPM_CMD_TX_DATA_CONT 12
200 #define RPM_CMD_READ_NOTIF 13
201 #define RPM_CMD_RX_DONE_W_REUSE 14
203 #define GLINK_FEATURE_INTENTLESS BIT(1)
205 static void qcom_glink_rx_done_work(struct work_struct *work);
207 static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
210 struct glink_channel *channel;
212 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
214 return ERR_PTR(-ENOMEM);
216 /* Setup glink internal glink_channel data */
217 spin_lock_init(&channel->recv_lock);
218 spin_lock_init(&channel->intent_lock);
219 mutex_init(&channel->intent_req_lock);
221 channel->glink = glink;
222 channel->name = kstrdup(name, GFP_KERNEL);
224 init_completion(&channel->open_req);
225 init_completion(&channel->open_ack);
226 init_completion(&channel->intent_req_comp);
228 INIT_LIST_HEAD(&channel->done_intents);
229 INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
231 idr_init(&channel->liids);
232 idr_init(&channel->riids);
233 kref_init(&channel->refcount);
238 static void qcom_glink_channel_release(struct kref *ref)
240 struct glink_channel *channel = container_of(ref, struct glink_channel,
244 spin_lock_irqsave(&channel->intent_lock, flags);
245 idr_destroy(&channel->liids);
246 idr_destroy(&channel->riids);
247 spin_unlock_irqrestore(&channel->intent_lock, flags);
249 kfree(channel->name);
253 static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
255 return glink->rx_pipe->avail(glink->rx_pipe);
258 static void qcom_glink_rx_peak(struct qcom_glink *glink,
259 void *data, unsigned int offset, size_t count)
261 glink->rx_pipe->peak(glink->rx_pipe, data, offset, count);
264 static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
266 glink->rx_pipe->advance(glink->rx_pipe, count);
269 static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
271 return glink->tx_pipe->avail(glink->tx_pipe);
274 static void qcom_glink_tx_write(struct qcom_glink *glink,
275 const void *hdr, size_t hlen,
276 const void *data, size_t dlen)
278 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
281 static int qcom_glink_tx(struct qcom_glink *glink,
282 const void *hdr, size_t hlen,
283 const void *data, size_t dlen, bool wait)
285 unsigned int tlen = hlen + dlen;
289 /* Reject packets that are too big */
290 if (tlen >= glink->tx_pipe->length)
293 spin_lock_irqsave(&glink->tx_lock, flags);
295 while (qcom_glink_tx_avail(glink) < tlen) {
301 /* Wait without holding the tx_lock */
302 spin_unlock_irqrestore(&glink->tx_lock, flags);
304 usleep_range(10000, 15000);
306 spin_lock_irqsave(&glink->tx_lock, flags);
309 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
311 mbox_send_message(glink->mbox_chan, NULL);
312 mbox_client_txdone(glink->mbox_chan, 0);
315 spin_unlock_irqrestore(&glink->tx_lock, flags);
320 static int qcom_glink_send_version(struct qcom_glink *glink)
322 struct glink_msg msg;
324 msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
325 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
326 msg.param2 = cpu_to_le32(glink->features);
328 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
331 static void qcom_glink_send_version_ack(struct qcom_glink *glink)
333 struct glink_msg msg;
335 msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
336 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
337 msg.param2 = cpu_to_le32(glink->features);
339 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
342 static void qcom_glink_send_open_ack(struct qcom_glink *glink,
343 struct glink_channel *channel)
345 struct glink_msg msg;
347 msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
348 msg.param1 = cpu_to_le16(channel->rcid);
349 msg.param2 = cpu_to_le32(0);
351 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
354 static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
355 unsigned int cid, bool granted)
357 struct glink_channel *channel;
360 spin_lock_irqsave(&glink->idr_lock, flags);
361 channel = idr_find(&glink->rcids, cid);
362 spin_unlock_irqrestore(&glink->idr_lock, flags);
364 dev_err(glink->dev, "unable to find channel\n");
368 channel->intent_req_result = granted;
369 complete(&channel->intent_req_comp);
373 * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
374 * @glink: Ptr to the glink edge
375 * @channel: Ptr to the channel that the open req is sent
377 * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
378 * Will return with refcount held, regardless of outcome.
380 * Returns 0 on success, negative errno otherwise.
382 static int qcom_glink_send_open_req(struct qcom_glink *glink,
383 struct glink_channel *channel)
386 struct glink_msg msg;
387 u8 name[GLINK_NAME_SIZE];
389 int name_len = strlen(channel->name) + 1;
390 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
394 kref_get(&channel->refcount);
396 spin_lock_irqsave(&glink->idr_lock, flags);
397 ret = idr_alloc_cyclic(&glink->lcids, channel,
398 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
400 spin_unlock_irqrestore(&glink->idr_lock, flags);
406 req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
407 req.msg.param1 = cpu_to_le16(channel->lcid);
408 req.msg.param2 = cpu_to_le32(name_len);
409 strcpy(req.name, channel->name);
411 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
418 spin_lock_irqsave(&glink->idr_lock, flags);
419 idr_remove(&glink->lcids, channel->lcid);
421 spin_unlock_irqrestore(&glink->idr_lock, flags);
426 static void qcom_glink_send_close_req(struct qcom_glink *glink,
427 struct glink_channel *channel)
429 struct glink_msg req;
431 req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
432 req.param1 = cpu_to_le16(channel->lcid);
435 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
438 static void qcom_glink_send_close_ack(struct qcom_glink *glink,
441 struct glink_msg req;
443 req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
444 req.param1 = cpu_to_le16(rcid);
447 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
450 static void qcom_glink_rx_done_work(struct work_struct *work)
452 struct glink_channel *channel = container_of(work, struct glink_channel,
454 struct qcom_glink *glink = channel->glink;
455 struct glink_core_rx_intent *intent, *tmp;
462 unsigned int cid = channel->lcid;
467 spin_lock_irqsave(&channel->intent_lock, flags);
468 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
469 list_del(&intent->node);
470 spin_unlock_irqrestore(&channel->intent_lock, flags);
472 reuse = intent->reuse;
474 cmd.id = reuse ? RPM_CMD_RX_DONE_W_REUSE : RPM_CMD_RX_DONE;
478 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
483 spin_lock_irqsave(&channel->intent_lock, flags);
485 spin_unlock_irqrestore(&channel->intent_lock, flags);
488 static void qcom_glink_rx_done(struct qcom_glink *glink,
489 struct glink_channel *channel,
490 struct glink_core_rx_intent *intent)
492 /* We don't send RX_DONE to intentless systems */
493 if (glink->intentless) {
499 /* Take it off the tree of receive intents */
500 if (!intent->reuse) {
501 spin_lock(&channel->intent_lock);
502 idr_remove(&channel->liids, intent->id);
503 spin_unlock(&channel->intent_lock);
506 /* Schedule the sending of a rx_done indication */
507 spin_lock(&channel->intent_lock);
508 list_add_tail(&intent->node, &channel->done_intents);
509 spin_unlock(&channel->intent_lock);
511 schedule_work(&channel->intent_work);
515 * qcom_glink_receive_version() - receive version/features from remote system
517 * @glink: pointer to transport interface
518 * @version: remote version
519 * @features: remote features
521 * This function is called in response to a remote-initiated version/feature
522 * negotiation sequence.
524 static void qcom_glink_receive_version(struct qcom_glink *glink,
531 case GLINK_VERSION_1:
532 glink->features &= features;
535 qcom_glink_send_version_ack(glink);
541 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
543 * @glink: pointer to transport interface
544 * @version: remote version response
545 * @features: remote features response
547 * This function is called in response to a local-initiated version/feature
548 * negotiation sequence and is the counter-offer from the remote side based
549 * upon the initial version and feature set requested.
551 static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
557 /* Version negotiation failed */
559 case GLINK_VERSION_1:
560 if (features == glink->features)
563 glink->features &= features;
566 qcom_glink_send_version(glink);
572 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
573 * wire format and transmit
574 * @glink: The transport to transmit on.
575 * @channel: The glink channel
576 * @granted: The request response to encode.
578 * Return: 0 on success or standard Linux error code.
580 static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
581 struct glink_channel *channel,
584 struct glink_msg msg;
586 msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
587 msg.param1 = cpu_to_le16(channel->lcid);
588 msg.param2 = cpu_to_le32(granted);
590 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
596 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
598 * @glink: The transport to transmit on.
599 * @channel: The local channel
600 * @intent: The intent to pass on to remote.
602 * Return: 0 on success or standard Linux error code.
604 static int qcom_glink_advertise_intent(struct qcom_glink *glink,
605 struct glink_channel *channel,
606 struct glink_core_rx_intent *intent)
617 cmd.id = cpu_to_le16(RPM_CMD_INTENT);
618 cmd.lcid = cpu_to_le16(channel->lcid);
619 cmd.count = cpu_to_le32(1);
620 cmd.size = cpu_to_le32(intent->size);
621 cmd.liid = cpu_to_le32(intent->id);
623 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
628 static struct glink_core_rx_intent *
629 qcom_glink_alloc_intent(struct qcom_glink *glink,
630 struct glink_channel *channel,
634 struct glink_core_rx_intent *intent;
638 intent = kzalloc(sizeof(*intent), GFP_KERNEL);
642 intent->data = kzalloc(size, GFP_KERNEL);
646 spin_lock_irqsave(&channel->intent_lock, flags);
647 ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
649 spin_unlock_irqrestore(&channel->intent_lock, flags);
652 spin_unlock_irqrestore(&channel->intent_lock, flags);
656 intent->reuse = reuseable;
667 static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
668 u32 cid, uint32_t iid,
671 struct glink_core_rx_intent *intent;
672 struct glink_channel *channel;
675 spin_lock_irqsave(&glink->idr_lock, flags);
676 channel = idr_find(&glink->rcids, cid);
677 spin_unlock_irqrestore(&glink->idr_lock, flags);
679 dev_err(glink->dev, "invalid channel id received\n");
683 spin_lock_irqsave(&channel->intent_lock, flags);
684 intent = idr_find(&channel->riids, iid);
687 spin_unlock_irqrestore(&channel->intent_lock, flags);
688 dev_err(glink->dev, "invalid intent id received\n");
692 intent->in_use = false;
695 idr_remove(&channel->riids, intent->id);
698 spin_unlock_irqrestore(&channel->intent_lock, flags);
702 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
704 * @glink: Pointer to the transport interface
705 * @cid: Remote channel ID
706 * @size: size of the intent
708 * The function searches for the local channel to which the request for
709 * rx_intent has arrived and allocates and notifies the remote back
711 static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
712 u32 cid, size_t size)
714 struct glink_core_rx_intent *intent;
715 struct glink_channel *channel;
718 spin_lock_irqsave(&glink->idr_lock, flags);
719 channel = idr_find(&glink->rcids, cid);
720 spin_unlock_irqrestore(&glink->idr_lock, flags);
723 pr_err("%s channel not found for cid %d\n", __func__, cid);
727 intent = qcom_glink_alloc_intent(glink, channel, size, false);
729 qcom_glink_advertise_intent(glink, channel, intent);
731 qcom_glink_send_intent_req_ack(glink, channel, !!intent);
734 static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
736 struct glink_defer_cmd *dcmd;
738 extra = ALIGN(extra, 8);
740 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
741 dev_dbg(glink->dev, "Insufficient data in rx fifo");
745 dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC);
749 INIT_LIST_HEAD(&dcmd->node);
751 qcom_glink_rx_peak(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
753 spin_lock(&glink->rx_lock);
754 list_add_tail(&dcmd->node, &glink->rx_queue);
755 spin_unlock(&glink->rx_lock);
757 schedule_work(&glink->rx_work);
758 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
763 static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
765 struct glink_core_rx_intent *intent;
766 struct glink_channel *channel;
768 struct glink_msg msg;
772 unsigned int chunk_size;
773 unsigned int left_size;
779 if (avail < sizeof(hdr)) {
780 dev_dbg(glink->dev, "Not enough data in fifo\n");
784 qcom_glink_rx_peak(glink, &hdr, 0, sizeof(hdr));
785 chunk_size = le32_to_cpu(hdr.chunk_size);
786 left_size = le32_to_cpu(hdr.left_size);
788 if (avail < sizeof(hdr) + chunk_size) {
789 dev_dbg(glink->dev, "Payload not yet in fifo\n");
793 if (WARN(chunk_size % 4, "Incoming data must be word aligned\n"))
796 rcid = le16_to_cpu(hdr.msg.param1);
797 spin_lock_irqsave(&glink->idr_lock, flags);
798 channel = idr_find(&glink->rcids, rcid);
799 spin_unlock_irqrestore(&glink->idr_lock, flags);
801 dev_dbg(glink->dev, "Data on non-existing channel\n");
803 /* Drop the message */
807 if (glink->intentless) {
808 /* Might have an ongoing, fragmented, message to append */
810 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
814 intent->data = kmalloc(chunk_size + left_size,
821 intent->id = 0xbabababa;
822 intent->size = chunk_size + left_size;
825 channel->buf = intent;
827 intent = channel->buf;
830 liid = le32_to_cpu(hdr.msg.param2);
832 spin_lock_irqsave(&channel->intent_lock, flags);
833 intent = idr_find(&channel->liids, liid);
834 spin_unlock_irqrestore(&channel->intent_lock, flags);
838 "no intent found for channel %s intent %d",
839 channel->name, liid);
844 if (intent->size - intent->offset < chunk_size) {
845 dev_err(glink->dev, "Insufficient space in intent\n");
847 /* The packet header lied, drop payload */
851 qcom_glink_rx_peak(glink, intent->data + intent->offset,
852 sizeof(hdr), chunk_size);
853 intent->offset += chunk_size;
855 /* Handle message when no fragments remain to be received */
857 spin_lock(&channel->recv_lock);
858 if (channel->ept.cb) {
859 channel->ept.cb(channel->ept.rpdev,
865 spin_unlock(&channel->recv_lock);
870 qcom_glink_rx_done(glink, channel, intent);
874 qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
879 static void qcom_glink_handle_intent(struct qcom_glink *glink,
884 struct glink_core_rx_intent *intent;
885 struct glink_channel *channel;
892 struct glink_msg msg;
893 struct intent_pair intents[];
896 const size_t msglen = sizeof(*msg) + sizeof(struct intent_pair) * count;
901 if (avail < msglen) {
902 dev_dbg(glink->dev, "Not enough data in fifo\n");
906 spin_lock_irqsave(&glink->idr_lock, flags);
907 channel = idr_find(&glink->rcids, cid);
908 spin_unlock_irqrestore(&glink->idr_lock, flags);
910 dev_err(glink->dev, "intents for non-existing channel\n");
914 msg = kmalloc(msglen, GFP_ATOMIC);
918 qcom_glink_rx_peak(glink, msg, 0, msglen);
920 for (i = 0; i < count; ++i) {
921 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
925 intent->id = le32_to_cpu(msg->intents[i].iid);
926 intent->size = le32_to_cpu(msg->intents[i].size);
928 spin_lock_irqsave(&channel->intent_lock, flags);
929 ret = idr_alloc(&channel->riids, intent,
930 intent->id, intent->id + 1, GFP_ATOMIC);
931 spin_unlock_irqrestore(&channel->intent_lock, flags);
934 dev_err(glink->dev, "failed to store remote intent\n");
938 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
941 static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
943 struct glink_channel *channel;
945 spin_lock(&glink->idr_lock);
946 channel = idr_find(&glink->lcids, lcid);
947 spin_unlock(&glink->idr_lock);
949 dev_err(glink->dev, "Invalid open ack packet\n");
953 complete(&channel->open_ack);
958 static irqreturn_t qcom_glink_native_intr(int irq, void *data)
960 struct qcom_glink *glink = data;
961 struct glink_msg msg;
969 avail = qcom_glink_rx_avail(glink);
970 if (avail < sizeof(msg))
973 qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
975 cmd = le16_to_cpu(msg.cmd);
976 param1 = le16_to_cpu(msg.param1);
977 param2 = le32_to_cpu(msg.param2);
980 case RPM_CMD_VERSION:
981 case RPM_CMD_VERSION_ACK:
983 case RPM_CMD_CLOSE_ACK:
984 case RPM_CMD_RX_INTENT_REQ:
985 ret = qcom_glink_rx_defer(glink, 0);
987 case RPM_CMD_OPEN_ACK:
988 ret = qcom_glink_rx_open_ack(glink, param1);
989 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
992 ret = qcom_glink_rx_defer(glink, param2);
994 case RPM_CMD_TX_DATA:
995 case RPM_CMD_TX_DATA_CONT:
996 ret = qcom_glink_rx_data(glink, avail);
998 case RPM_CMD_READ_NOTIF:
999 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1001 mbox_send_message(glink->mbox_chan, NULL);
1002 mbox_client_txdone(glink->mbox_chan, 0);
1004 case RPM_CMD_INTENT:
1005 qcom_glink_handle_intent(glink, param1, param2, avail);
1007 case RPM_CMD_RX_DONE:
1008 qcom_glink_handle_rx_done(glink, param1, param2, false);
1009 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1011 case RPM_CMD_RX_DONE_W_REUSE:
1012 qcom_glink_handle_rx_done(glink, param1, param2, true);
1013 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1015 case RPM_CMD_RX_INTENT_REQ_ACK:
1016 qcom_glink_handle_intent_req_ack(glink, param1, param2);
1017 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1020 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1032 /* Locally initiated rpmsg_create_ept */
1033 static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1036 struct glink_channel *channel;
1038 unsigned long flags;
1040 channel = qcom_glink_alloc_channel(glink, name);
1041 if (IS_ERR(channel))
1042 return ERR_CAST(channel);
1044 ret = qcom_glink_send_open_req(glink, channel);
1046 goto release_channel;
1048 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1052 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1056 qcom_glink_send_open_ack(glink, channel);
1061 /* qcom_glink_send_open_req() did register the channel in lcids*/
1062 spin_lock_irqsave(&glink->idr_lock, flags);
1063 idr_remove(&glink->lcids, channel->lcid);
1064 spin_unlock_irqrestore(&glink->idr_lock, flags);
1067 /* Release qcom_glink_send_open_req() reference */
1068 kref_put(&channel->refcount, qcom_glink_channel_release);
1069 /* Release qcom_glink_alloc_channel() reference */
1070 kref_put(&channel->refcount, qcom_glink_channel_release);
1072 return ERR_PTR(-ETIMEDOUT);
1075 /* Remote initiated rpmsg_create_ept */
1076 static int qcom_glink_create_remote(struct qcom_glink *glink,
1077 struct glink_channel *channel)
1081 qcom_glink_send_open_ack(glink, channel);
1083 ret = qcom_glink_send_open_req(glink, channel);
1087 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1097 * Send a close request to "undo" our open-ack. The close-ack will
1098 * release the last reference.
1100 qcom_glink_send_close_req(glink, channel);
1102 /* Release qcom_glink_send_open_req() reference */
1103 kref_put(&channel->refcount, qcom_glink_channel_release);
1108 static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1111 struct rpmsg_channel_info
1114 struct glink_channel *parent = to_glink_channel(rpdev->ept);
1115 struct glink_channel *channel;
1116 struct qcom_glink *glink = parent->glink;
1117 struct rpmsg_endpoint *ept;
1118 const char *name = chinfo.name;
1121 unsigned long flags;
1123 spin_lock_irqsave(&glink->idr_lock, flags);
1124 idr_for_each_entry(&glink->rcids, channel, cid) {
1125 if (!strcmp(channel->name, name))
1128 spin_unlock_irqrestore(&glink->idr_lock, flags);
1131 channel = qcom_glink_create_local(glink, name);
1132 if (IS_ERR(channel))
1135 ret = qcom_glink_create_remote(glink, channel);
1140 ept = &channel->ept;
1144 ept->ops = &glink_endpoint_ops;
1149 static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1151 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1152 struct device_node *np = rpdev->dev.of_node;
1153 struct qcom_glink *glink = channel->glink;
1154 struct glink_core_rx_intent *intent;
1155 const struct property *prop = NULL;
1156 __be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
1159 __be32 *val = defaults;
1162 if (glink->intentless)
1165 prop = of_find_property(np, "qcom,intents", NULL);
1168 num_groups = prop->length / sizeof(u32) / 2;
1171 /* Channel is now open, advertise base set of intents */
1172 while (num_groups--) {
1173 size = be32_to_cpup(val++);
1174 num_intents = be32_to_cpup(val++);
1175 while (num_intents--) {
1176 intent = qcom_glink_alloc_intent(glink, channel, size,
1181 qcom_glink_advertise_intent(glink, channel, intent);
1187 static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1189 struct glink_channel *channel = to_glink_channel(ept);
1190 struct qcom_glink *glink = channel->glink;
1191 unsigned long flags;
1193 spin_lock_irqsave(&channel->recv_lock, flags);
1194 channel->ept.cb = NULL;
1195 spin_unlock_irqrestore(&channel->recv_lock, flags);
1197 /* Decouple the potential rpdev from the channel */
1198 channel->rpdev = NULL;
1200 qcom_glink_send_close_req(glink, channel);
1203 static int qcom_glink_request_intent(struct qcom_glink *glink,
1204 struct glink_channel *channel,
1215 mutex_lock(&channel->intent_req_lock);
1217 reinit_completion(&channel->intent_req_comp);
1219 cmd.id = RPM_CMD_RX_INTENT_REQ;
1220 cmd.cid = channel->lcid;
1223 ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1227 ret = wait_for_completion_timeout(&channel->intent_req_comp, 10 * HZ);
1229 dev_err(glink->dev, "intent request timed out\n");
1232 ret = channel->intent_req_result ? 0 : -ECANCELED;
1236 mutex_unlock(&channel->intent_req_lock);
1240 static int __qcom_glink_send(struct glink_channel *channel,
1241 void *data, int len, bool wait)
1243 struct qcom_glink *glink = channel->glink;
1244 struct glink_core_rx_intent *intent = NULL;
1245 struct glink_core_rx_intent *tmp;
1248 struct glink_msg msg;
1253 unsigned long flags;
1255 if (!glink->intentless) {
1257 spin_lock_irqsave(&channel->intent_lock, flags);
1258 idr_for_each_entry(&channel->riids, tmp, iid) {
1259 if (tmp->size >= len && !tmp->in_use) {
1262 else if (intent->size > tmp->size)
1264 if (intent->size == len)
1269 intent->in_use = true;
1270 spin_unlock_irqrestore(&channel->intent_lock, flags);
1272 /* We found an available intent */
1279 ret = qcom_glink_request_intent(glink, channel, len);
1287 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
1288 req.msg.param1 = cpu_to_le16(channel->lcid);
1289 req.msg.param2 = cpu_to_le32(iid);
1290 req.chunk_size = cpu_to_le32(len);
1291 req.left_size = cpu_to_le32(0);
1293 ret = qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
1295 /* Mark intent available if we failed */
1297 intent->in_use = false;
1302 static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1304 struct glink_channel *channel = to_glink_channel(ept);
1306 return __qcom_glink_send(channel, data, len, true);
1309 static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1311 struct glink_channel *channel = to_glink_channel(ept);
1313 return __qcom_glink_send(channel, data, len, false);
1317 * Finds the device_node for the glink child interested in this channel.
1319 static struct device_node *qcom_glink_match_channel(struct device_node *node,
1320 const char *channel)
1322 struct device_node *child;
1327 for_each_available_child_of_node(node, child) {
1328 key = "qcom,glink-channels";
1329 ret = of_property_read_string(child, key, &name);
1333 if (strcmp(name, channel) == 0)
1340 static const struct rpmsg_device_ops glink_device_ops = {
1341 .create_ept = qcom_glink_create_ept,
1342 .announce_create = qcom_glink_announce_create,
1345 static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1346 .destroy_ept = qcom_glink_destroy_ept,
1347 .send = qcom_glink_send,
1348 .trysend = qcom_glink_trysend,
1351 static void qcom_glink_rpdev_release(struct device *dev)
1353 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1354 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1356 channel->rpdev = NULL;
1360 static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1363 struct glink_channel *channel;
1364 struct rpmsg_device *rpdev;
1365 bool create_device = false;
1366 struct device_node *node;
1369 unsigned long flags;
1371 spin_lock_irqsave(&glink->idr_lock, flags);
1372 idr_for_each_entry(&glink->lcids, channel, lcid) {
1373 if (!strcmp(channel->name, name))
1376 spin_unlock_irqrestore(&glink->idr_lock, flags);
1379 channel = qcom_glink_alloc_channel(glink, name);
1380 if (IS_ERR(channel))
1381 return PTR_ERR(channel);
1383 /* The opening dance was initiated by the remote */
1384 create_device = true;
1387 spin_lock_irqsave(&glink->idr_lock, flags);
1388 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
1390 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
1391 spin_unlock_irqrestore(&glink->idr_lock, flags);
1394 channel->rcid = ret;
1395 spin_unlock_irqrestore(&glink->idr_lock, flags);
1397 complete(&channel->open_req);
1399 if (create_device) {
1400 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1406 rpdev->ept = &channel->ept;
1407 strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
1408 rpdev->src = RPMSG_ADDR_ANY;
1409 rpdev->dst = RPMSG_ADDR_ANY;
1410 rpdev->ops = &glink_device_ops;
1412 node = qcom_glink_match_channel(glink->dev->of_node, name);
1413 rpdev->dev.of_node = node;
1414 rpdev->dev.parent = glink->dev;
1415 rpdev->dev.release = qcom_glink_rpdev_release;
1417 ret = rpmsg_register_device(rpdev);
1421 channel->rpdev = rpdev;
1429 spin_lock_irqsave(&glink->idr_lock, flags);
1430 idr_remove(&glink->rcids, channel->rcid);
1432 spin_unlock_irqrestore(&glink->idr_lock, flags);
1434 /* Release the reference, iff we took it */
1436 kref_put(&channel->refcount, qcom_glink_channel_release);
1441 static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1443 struct rpmsg_channel_info chinfo;
1444 struct glink_channel *channel;
1445 unsigned long flags;
1447 spin_lock_irqsave(&glink->idr_lock, flags);
1448 channel = idr_find(&glink->rcids, rcid);
1449 spin_unlock_irqrestore(&glink->idr_lock, flags);
1450 if (WARN(!channel, "close request on unknown channel\n"))
1453 /* cancel pending rx_done work */
1454 cancel_work_sync(&channel->intent_work);
1456 if (channel->rpdev) {
1457 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1458 chinfo.src = RPMSG_ADDR_ANY;
1459 chinfo.dst = RPMSG_ADDR_ANY;
1461 rpmsg_unregister_device(glink->dev, &chinfo);
1464 qcom_glink_send_close_ack(glink, channel->rcid);
1466 spin_lock_irqsave(&glink->idr_lock, flags);
1467 idr_remove(&glink->rcids, channel->rcid);
1469 spin_unlock_irqrestore(&glink->idr_lock, flags);
1471 kref_put(&channel->refcount, qcom_glink_channel_release);
1474 static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1476 struct glink_channel *channel;
1477 unsigned long flags;
1479 spin_lock_irqsave(&glink->idr_lock, flags);
1480 channel = idr_find(&glink->lcids, lcid);
1481 if (WARN(!channel, "close ack on unknown channel\n")) {
1482 spin_unlock_irqrestore(&glink->idr_lock, flags);
1486 idr_remove(&glink->lcids, channel->lcid);
1488 spin_unlock_irqrestore(&glink->idr_lock, flags);
1490 kref_put(&channel->refcount, qcom_glink_channel_release);
1493 static void qcom_glink_work(struct work_struct *work)
1495 struct qcom_glink *glink = container_of(work, struct qcom_glink,
1497 struct glink_defer_cmd *dcmd;
1498 struct glink_msg *msg;
1499 unsigned long flags;
1500 unsigned int param1;
1501 unsigned int param2;
1505 spin_lock_irqsave(&glink->rx_lock, flags);
1506 if (list_empty(&glink->rx_queue)) {
1507 spin_unlock_irqrestore(&glink->rx_lock, flags);
1510 dcmd = list_first_entry(&glink->rx_queue,
1511 struct glink_defer_cmd, node);
1512 list_del(&dcmd->node);
1513 spin_unlock_irqrestore(&glink->rx_lock, flags);
1516 cmd = le16_to_cpu(msg->cmd);
1517 param1 = le16_to_cpu(msg->param1);
1518 param2 = le32_to_cpu(msg->param2);
1521 case RPM_CMD_VERSION:
1522 qcom_glink_receive_version(glink, param1, param2);
1524 case RPM_CMD_VERSION_ACK:
1525 qcom_glink_receive_version_ack(glink, param1, param2);
1528 qcom_glink_rx_open(glink, param1, msg->data);
1531 qcom_glink_rx_close(glink, param1);
1533 case RPM_CMD_CLOSE_ACK:
1534 qcom_glink_rx_close_ack(glink, param1);
1536 case RPM_CMD_RX_INTENT_REQ:
1537 qcom_glink_handle_intent_req(glink, param1, param2);
1540 WARN(1, "Unknown defer object %d\n", cmd);
1548 struct qcom_glink *qcom_glink_native_probe(struct device *dev,
1549 unsigned long features,
1550 struct qcom_glink_pipe *rx,
1551 struct qcom_glink_pipe *tx,
1556 struct qcom_glink *glink;
1558 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1560 return ERR_PTR(-ENOMEM);
1563 glink->tx_pipe = tx;
1564 glink->rx_pipe = rx;
1566 glink->features = features;
1567 glink->intentless = intentless;
1569 spin_lock_init(&glink->tx_lock);
1570 spin_lock_init(&glink->rx_lock);
1571 INIT_LIST_HEAD(&glink->rx_queue);
1572 INIT_WORK(&glink->rx_work, qcom_glink_work);
1574 spin_lock_init(&glink->idr_lock);
1575 idr_init(&glink->lcids);
1576 idr_init(&glink->rcids);
1578 glink->mbox_client.dev = dev;
1579 glink->mbox_client.knows_txdone = true;
1580 glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1581 if (IS_ERR(glink->mbox_chan)) {
1582 if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1583 dev_err(dev, "failed to acquire IPC channel\n");
1584 return ERR_CAST(glink->mbox_chan);
1587 irq = of_irq_get(dev->of_node, 0);
1588 ret = devm_request_irq(dev, irq,
1589 qcom_glink_native_intr,
1590 IRQF_NO_SUSPEND | IRQF_SHARED,
1591 "glink-native", glink);
1593 dev_err(dev, "failed to request IRQ\n");
1594 return ERR_PTR(ret);
1599 ret = qcom_glink_send_version(glink);
1601 return ERR_PTR(ret);
1605 EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
1607 static int qcom_glink_remove_device(struct device *dev, void *data)
1609 device_unregister(dev);
1614 void qcom_glink_native_remove(struct qcom_glink *glink)
1616 struct glink_channel *channel;
1619 unsigned long flags;
1621 disable_irq(glink->irq);
1622 cancel_work_sync(&glink->rx_work);
1624 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1626 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1628 spin_lock_irqsave(&glink->idr_lock, flags);
1629 /* Release any defunct local channels, waiting for close-ack */
1630 idr_for_each_entry(&glink->lcids, channel, cid)
1631 kref_put(&channel->refcount, qcom_glink_channel_release);
1633 idr_destroy(&glink->lcids);
1634 idr_destroy(&glink->rcids);
1635 spin_unlock_irqrestore(&glink->idr_lock, flags);
1636 mbox_free_channel(glink->mbox_chan);
1638 EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
1640 void qcom_glink_native_unregister(struct qcom_glink *glink)
1642 device_unregister(glink->dev);
1644 EXPORT_SYMBOL_GPL(qcom_glink_native_unregister);
1646 MODULE_DESCRIPTION("Qualcomm GLINK driver");
1647 MODULE_LICENSE("GPL v2");