1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2017, Linaro Ltd.
7 #include <linux/completion.h>
8 #include <linux/module.h>
9 #include <linux/notifier.h>
10 #include <linux/rpmsg.h>
11 #include <linux/remoteproc/qcom_rproc.h>
14 * struct do_cleanup_msg - The data structure for an SSR do_cleanup message
15 * version: The G-Link SSR protocol version
16 * command: The G-Link SSR command - do_cleanup
17 * seq_num: Sequence number
18 * name_len: Length of the name of the subsystem being restarted
19 * name: G-Link edge name of the subsystem being restarted
21 struct do_cleanup_msg
{
30 * struct cleanup_done_msg - The data structure for an SSR cleanup_done message
31 * version: The G-Link SSR protocol version
32 * response: The G-Link SSR response to a do_cleanup command, cleanup_done
33 * seq_num: Sequence number
35 struct cleanup_done_msg
{
42 * G-Link SSR protocol commands
44 #define GLINK_SSR_DO_CLEANUP 0
45 #define GLINK_SSR_CLEANUP_DONE 1
49 struct rpmsg_endpoint
*ept
;
51 struct notifier_block nb
;
54 struct completion completion
;
57 /* Notifier list for all registered glink_ssr instances */
58 static BLOCKING_NOTIFIER_HEAD(ssr_notifiers
);
61 * qcom_glink_ssr_notify() - notify GLINK SSR about stopped remoteproc
62 * @ssr_name: name of the remoteproc that has been stopped
64 void qcom_glink_ssr_notify(const char *ssr_name
)
66 blocking_notifier_call_chain(&ssr_notifiers
, 0, (void *)ssr_name
);
68 EXPORT_SYMBOL_GPL(qcom_glink_ssr_notify
);
70 static int qcom_glink_ssr_callback(struct rpmsg_device
*rpdev
,
71 void *data
, int len
, void *priv
, u32 addr
)
73 struct cleanup_done_msg
*msg
= data
;
74 struct glink_ssr
*ssr
= dev_get_drvdata(&rpdev
->dev
);
76 if (len
< sizeof(*msg
)) {
77 dev_err(ssr
->dev
, "message too short\n");
81 if (le32_to_cpu(msg
->version
) != 0)
84 if (le32_to_cpu(msg
->response
) != GLINK_SSR_CLEANUP_DONE
)
87 if (le32_to_cpu(msg
->seq_num
) != ssr
->seq_num
) {
88 dev_err(ssr
->dev
, "invalid sequence number of response\n");
92 complete(&ssr
->completion
);
97 static int qcom_glink_ssr_notifier_call(struct notifier_block
*nb
,
101 struct glink_ssr
*ssr
= container_of(nb
, struct glink_ssr
, nb
);
102 struct do_cleanup_msg msg
;
103 char *ssr_name
= data
;
107 reinit_completion(&ssr
->completion
);
109 memset(&msg
, 0, sizeof(msg
));
110 msg
.command
= cpu_to_le32(GLINK_SSR_DO_CLEANUP
);
111 msg
.seq_num
= cpu_to_le32(ssr
->seq_num
);
112 msg
.name_len
= cpu_to_le32(strlen(ssr_name
));
113 strlcpy(msg
.name
, ssr_name
, sizeof(msg
.name
));
115 ret
= rpmsg_send(ssr
->ept
, &msg
, sizeof(msg
));
117 dev_err(ssr
->dev
, "failed to send cleanup message\n");
119 ret
= wait_for_completion_timeout(&ssr
->completion
, HZ
);
121 dev_err(ssr
->dev
, "timeout waiting for cleanup done message\n");
126 static int qcom_glink_ssr_probe(struct rpmsg_device
*rpdev
)
128 struct glink_ssr
*ssr
;
130 ssr
= devm_kzalloc(&rpdev
->dev
, sizeof(*ssr
), GFP_KERNEL
);
134 init_completion(&ssr
->completion
);
136 ssr
->dev
= &rpdev
->dev
;
137 ssr
->ept
= rpdev
->ept
;
138 ssr
->nb
.notifier_call
= qcom_glink_ssr_notifier_call
;
140 dev_set_drvdata(&rpdev
->dev
, ssr
);
142 return blocking_notifier_chain_register(&ssr_notifiers
, &ssr
->nb
);
145 static void qcom_glink_ssr_remove(struct rpmsg_device
*rpdev
)
147 struct glink_ssr
*ssr
= dev_get_drvdata(&rpdev
->dev
);
149 blocking_notifier_chain_unregister(&ssr_notifiers
, &ssr
->nb
);
152 static const struct rpmsg_device_id qcom_glink_ssr_match
[] = {
157 static struct rpmsg_driver qcom_glink_ssr_driver
= {
158 .probe
= qcom_glink_ssr_probe
,
159 .remove
= qcom_glink_ssr_remove
,
160 .callback
= qcom_glink_ssr_callback
,
161 .id_table
= qcom_glink_ssr_match
,
163 .name
= "qcom_glink_ssr",
166 module_rpmsg_driver(qcom_glink_ssr_driver
);