1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
4  * Copyright (c) 2017, Linaro Ltd.
5  */
6 
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>
12 
13 /**
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
20  */
21 struct do_cleanup_msg {
22 	__le32 version;
23 	__le32 command;
24 	__le32 seq_num;
25 	__le32 name_len;
26 	char name[32];
27 };
28 
29 /**
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
34  */
35 struct cleanup_done_msg {
36 	__le32 version;
37 	__le32 response;
38 	__le32 seq_num;
39 };
40 
41 /**
42  * G-Link SSR protocol commands
43  */
44 #define GLINK_SSR_DO_CLEANUP	0
45 #define GLINK_SSR_CLEANUP_DONE	1
46 
47 struct glink_ssr {
48 	struct device *dev;
49 	struct rpmsg_endpoint *ept;
50 
51 	struct notifier_block nb;
52 
53 	u32 seq_num;
54 	struct completion completion;
55 };
56 
57 /* Notifier list for all registered glink_ssr instances */
58 static BLOCKING_NOTIFIER_HEAD(ssr_notifiers);
59 
60 /**
61  * qcom_glink_ssr_notify() - notify GLINK SSR about stopped remoteproc
62  * @ssr_name:	name of the remoteproc that has been stopped
63  */
64 void qcom_glink_ssr_notify(const char *ssr_name)
65 {
66 	blocking_notifier_call_chain(&ssr_notifiers, 0, (void *)ssr_name);
67 }
68 EXPORT_SYMBOL_GPL(qcom_glink_ssr_notify);
69 
70 static int qcom_glink_ssr_callback(struct rpmsg_device *rpdev,
71 				   void *data, int len, void *priv, u32 addr)
72 {
73 	struct cleanup_done_msg *msg = data;
74 	struct glink_ssr *ssr = dev_get_drvdata(&rpdev->dev);
75 
76 	if (len < sizeof(*msg)) {
77 		dev_err(ssr->dev, "message too short\n");
78 		return -EINVAL;
79 	}
80 
81 	if (le32_to_cpu(msg->version) != 0)
82 		return -EINVAL;
83 
84 	if (le32_to_cpu(msg->response) != GLINK_SSR_CLEANUP_DONE)
85 		return 0;
86 
87 	if (le32_to_cpu(msg->seq_num) != ssr->seq_num) {
88 		dev_err(ssr->dev, "invalid sequence number of response\n");
89 		return -EINVAL;
90 	}
91 
92 	complete(&ssr->completion);
93 
94 	return 0;
95 }
96 
97 static int qcom_glink_ssr_notifier_call(struct notifier_block *nb,
98 					unsigned long event,
99 					void *data)
100 {
101 	struct glink_ssr *ssr = container_of(nb, struct glink_ssr, nb);
102 	struct do_cleanup_msg msg;
103 	char *ssr_name = data;
104 	int ret;
105 
106 	ssr->seq_num++;
107 	reinit_completion(&ssr->completion);
108 
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));
114 
115 	ret = rpmsg_send(ssr->ept, &msg, sizeof(msg));
116 	if (ret < 0)
117 		dev_err(ssr->dev, "failed to send cleanup message\n");
118 
119 	ret = wait_for_completion_timeout(&ssr->completion, HZ);
120 	if (!ret)
121 		dev_err(ssr->dev, "timeout waiting for cleanup done message\n");
122 
123 	return NOTIFY_DONE;
124 }
125 
126 static int qcom_glink_ssr_probe(struct rpmsg_device *rpdev)
127 {
128 	struct glink_ssr *ssr;
129 
130 	ssr = devm_kzalloc(&rpdev->dev, sizeof(*ssr), GFP_KERNEL);
131 	if (!ssr)
132 		return -ENOMEM;
133 
134 	init_completion(&ssr->completion);
135 
136 	ssr->dev = &rpdev->dev;
137 	ssr->ept = rpdev->ept;
138 	ssr->nb.notifier_call = qcom_glink_ssr_notifier_call;
139 
140 	dev_set_drvdata(&rpdev->dev, ssr);
141 
142 	return blocking_notifier_chain_register(&ssr_notifiers, &ssr->nb);
143 }
144 
145 static void qcom_glink_ssr_remove(struct rpmsg_device *rpdev)
146 {
147 	struct glink_ssr *ssr = dev_get_drvdata(&rpdev->dev);
148 
149 	blocking_notifier_chain_unregister(&ssr_notifiers, &ssr->nb);
150 }
151 
152 static const struct rpmsg_device_id qcom_glink_ssr_match[] = {
153 	{ "glink_ssr" },
154 	{}
155 };
156 
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,
162 	.drv = {
163 		.name = "qcom_glink_ssr",
164 	},
165 };
166 module_rpmsg_driver(qcom_glink_ssr_driver);
167