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/rpmsg/qcom_glink.h>
12 #include <linux/remoteproc/qcom_rproc.h>
15 * struct do_cleanup_msg - The data structure for an SSR do_cleanup message
16 * @version: The G-Link SSR protocol version
17 * @command: The G-Link SSR command - do_cleanup
18 * @seq_num: Sequence number
19 * @name_len: Length of the name of the subsystem being restarted
20 * @name: G-Link edge name of the subsystem being restarted
22 struct do_cleanup_msg
{
31 * struct cleanup_done_msg - The data structure for an SSR cleanup_done message
32 * @version: The G-Link SSR protocol version
33 * @response: The G-Link SSR response to a do_cleanup command, cleanup_done
34 * @seq_num: Sequence number
36 struct cleanup_done_msg
{
43 * G-Link SSR protocol commands
45 #define GLINK_SSR_DO_CLEANUP 0
46 #define GLINK_SSR_CLEANUP_DONE 1
50 struct rpmsg_endpoint
*ept
;
52 struct notifier_block nb
;
55 struct completion completion
;
58 /* Notifier list for all registered glink_ssr instances */
59 static BLOCKING_NOTIFIER_HEAD(ssr_notifiers
);
62 * qcom_glink_ssr_notify() - notify GLINK SSR about stopped remoteproc
63 * @ssr_name: name of the remoteproc that has been stopped
65 void qcom_glink_ssr_notify(const char *ssr_name
)
67 blocking_notifier_call_chain(&ssr_notifiers
, 0, (void *)ssr_name
);
69 EXPORT_SYMBOL_GPL(qcom_glink_ssr_notify
);
71 static int qcom_glink_ssr_callback(struct rpmsg_device
*rpdev
,
72 void *data
, int len
, void *priv
, u32 addr
)
74 struct cleanup_done_msg
*msg
= data
;
75 struct glink_ssr
*ssr
= dev_get_drvdata(&rpdev
->dev
);
77 if (len
< sizeof(*msg
)) {
78 dev_err(ssr
->dev
, "message too short\n");
82 if (le32_to_cpu(msg
->version
) != 0)
85 if (le32_to_cpu(msg
->response
) != GLINK_SSR_CLEANUP_DONE
)
88 if (le32_to_cpu(msg
->seq_num
) != ssr
->seq_num
) {
89 dev_err(ssr
->dev
, "invalid sequence number of response\n");
93 complete(&ssr
->completion
);
98 static int qcom_glink_ssr_notifier_call(struct notifier_block
*nb
,
102 struct glink_ssr
*ssr
= container_of(nb
, struct glink_ssr
, nb
);
103 struct do_cleanup_msg msg
;
104 char *ssr_name
= data
;
108 reinit_completion(&ssr
->completion
);
110 memset(&msg
, 0, sizeof(msg
));
111 msg
.command
= cpu_to_le32(GLINK_SSR_DO_CLEANUP
);
112 msg
.seq_num
= cpu_to_le32(ssr
->seq_num
);
113 msg
.name_len
= cpu_to_le32(strlen(ssr_name
));
114 strscpy(msg
.name
, ssr_name
, sizeof(msg
.name
));
116 ret
= rpmsg_send(ssr
->ept
, &msg
, sizeof(msg
));
118 dev_err(ssr
->dev
, "failed to send cleanup message\n");
120 ret
= wait_for_completion_timeout(&ssr
->completion
, HZ
);
122 dev_err(ssr
->dev
, "timeout waiting for cleanup done message\n");
127 static int qcom_glink_ssr_probe(struct rpmsg_device
*rpdev
)
129 struct glink_ssr
*ssr
;
131 ssr
= devm_kzalloc(&rpdev
->dev
, sizeof(*ssr
), GFP_KERNEL
);
135 init_completion(&ssr
->completion
);
137 ssr
->dev
= &rpdev
->dev
;
138 ssr
->ept
= rpdev
->ept
;
139 ssr
->nb
.notifier_call
= qcom_glink_ssr_notifier_call
;
141 dev_set_drvdata(&rpdev
->dev
, ssr
);
143 return blocking_notifier_chain_register(&ssr_notifiers
, &ssr
->nb
);
146 static void qcom_glink_ssr_remove(struct rpmsg_device
*rpdev
)
148 struct glink_ssr
*ssr
= dev_get_drvdata(&rpdev
->dev
);
150 blocking_notifier_chain_unregister(&ssr_notifiers
, &ssr
->nb
);
153 static const struct rpmsg_device_id qcom_glink_ssr_match
[] = {
157 MODULE_DEVICE_TABLE(rpmsg
, qcom_glink_ssr_match
);
159 static struct rpmsg_driver qcom_glink_ssr_driver
= {
160 .probe
= qcom_glink_ssr_probe
,
161 .remove
= qcom_glink_ssr_remove
,
162 .callback
= qcom_glink_ssr_callback
,
163 .id_table
= qcom_glink_ssr_match
,
165 .name
= "qcom_glink_ssr",
168 module_rpmsg_driver(qcom_glink_ssr_driver
);