1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017, Linaro Ltd.
5 #include <linux/firmware.h>
6 #include <linux/module.h>
7 #include <linux/notifier.h>
8 #include <linux/slab.h>
10 #include <linux/notifier.h>
11 #include <linux/of_platform.h>
12 #include <linux/platform_device.h>
13 #include <linux/remoteproc/qcom_rproc.h>
14 #include <linux/rpmsg.h>
16 #include "qcom_common.h"
18 static BLOCKING_NOTIFIER_HEAD(sysmon_notifiers
);
21 struct rproc_subdev subdev
;
24 struct list_head node
;
31 struct notifier_block nb
;
35 struct rpmsg_endpoint
*ept
;
36 struct completion comp
;
41 struct qmi_handle qmi
;
42 struct sockaddr_qrtr ssctl
;
45 static DEFINE_MUTEX(sysmon_lock
);
46 static LIST_HEAD(sysmon_list
);
49 * sysmon_send_event() - send notification of other remote's SSR event
50 * @sysmon: sysmon context
51 * @name: other remote's name
53 static void sysmon_send_event(struct qcom_sysmon
*sysmon
, const char *name
)
59 len
= snprintf(req
, sizeof(req
), "ssr:%s:before_shutdown", name
);
60 if (len
>= sizeof(req
))
63 mutex_lock(&sysmon
->lock
);
64 reinit_completion(&sysmon
->comp
);
65 sysmon
->ssr_ack
= false;
67 ret
= rpmsg_send(sysmon
->ept
, req
, len
);
69 dev_err(sysmon
->dev
, "failed to send sysmon event\n");
73 ret
= wait_for_completion_timeout(&sysmon
->comp
,
74 msecs_to_jiffies(5000));
76 dev_err(sysmon
->dev
, "timeout waiting for sysmon ack\n");
81 dev_err(sysmon
->dev
, "unexpected response to sysmon event\n");
84 mutex_unlock(&sysmon
->lock
);
88 * sysmon_request_shutdown() - request graceful shutdown of remote
89 * @sysmon: sysmon context
91 static void sysmon_request_shutdown(struct qcom_sysmon
*sysmon
)
93 char *req
= "ssr:shutdown";
96 mutex_lock(&sysmon
->lock
);
97 reinit_completion(&sysmon
->comp
);
98 sysmon
->ssr_ack
= false;
100 ret
= rpmsg_send(sysmon
->ept
, req
, strlen(req
) + 1);
102 dev_err(sysmon
->dev
, "send sysmon shutdown request failed\n");
106 ret
= wait_for_completion_timeout(&sysmon
->comp
,
107 msecs_to_jiffies(5000));
109 dev_err(sysmon
->dev
, "timeout waiting for sysmon ack\n");
113 if (!sysmon
->ssr_ack
)
115 "unexpected response to sysmon shutdown request\n");
118 mutex_unlock(&sysmon
->lock
);
121 static int sysmon_callback(struct rpmsg_device
*rpdev
, void *data
, int count
,
122 void *priv
, u32 addr
)
124 struct qcom_sysmon
*sysmon
= priv
;
125 const char *ssr_ack
= "ssr:ack";
126 const int ssr_ack_len
= strlen(ssr_ack
) + 1;
131 if (count
>= ssr_ack_len
&& !memcmp(data
, ssr_ack
, ssr_ack_len
))
132 sysmon
->ssr_ack
= true;
134 complete(&sysmon
->comp
);
139 #define SSCTL_SHUTDOWN_REQ 0x21
140 #define SSCTL_SUBSYS_EVENT_REQ 0x23
142 #define SSCTL_MAX_MSG_LEN 7
144 #define SSCTL_SUBSYS_NAME_LENGTH 15
147 SSCTL_SSR_EVENT_BEFORE_POWERUP
,
148 SSCTL_SSR_EVENT_AFTER_POWERUP
,
149 SSCTL_SSR_EVENT_BEFORE_SHUTDOWN
,
150 SSCTL_SSR_EVENT_AFTER_SHUTDOWN
,
154 SSCTL_SSR_EVENT_FORCED
,
155 SSCTL_SSR_EVENT_GRACEFUL
,
158 struct ssctl_shutdown_resp
{
159 struct qmi_response_type_v01 resp
;
162 static struct qmi_elem_info ssctl_shutdown_resp_ei
[] = {
164 .data_type
= QMI_STRUCT
,
166 .elem_size
= sizeof(struct qmi_response_type_v01
),
167 .array_type
= NO_ARRAY
,
169 .offset
= offsetof(struct ssctl_shutdown_resp
, resp
),
170 .ei_array
= qmi_response_type_v01_ei
,
175 struct ssctl_subsys_event_req
{
177 char subsys_name
[SSCTL_SUBSYS_NAME_LENGTH
];
183 static struct qmi_elem_info ssctl_subsys_event_req_ei
[] = {
185 .data_type
= QMI_DATA_LEN
,
187 .elem_size
= sizeof(uint8_t),
188 .array_type
= NO_ARRAY
,
190 .offset
= offsetof(struct ssctl_subsys_event_req
,
195 .data_type
= QMI_UNSIGNED_1_BYTE
,
196 .elem_len
= SSCTL_SUBSYS_NAME_LENGTH
,
197 .elem_size
= sizeof(char),
198 .array_type
= VAR_LEN_ARRAY
,
200 .offset
= offsetof(struct ssctl_subsys_event_req
,
205 .data_type
= QMI_SIGNED_4_BYTE_ENUM
,
207 .elem_size
= sizeof(uint32_t),
208 .array_type
= NO_ARRAY
,
210 .offset
= offsetof(struct ssctl_subsys_event_req
,
215 .data_type
= QMI_OPT_FLAG
,
217 .elem_size
= sizeof(uint8_t),
218 .array_type
= NO_ARRAY
,
220 .offset
= offsetof(struct ssctl_subsys_event_req
,
225 .data_type
= QMI_SIGNED_4_BYTE_ENUM
,
227 .elem_size
= sizeof(uint32_t),
228 .array_type
= NO_ARRAY
,
230 .offset
= offsetof(struct ssctl_subsys_event_req
,
237 struct ssctl_subsys_event_resp
{
238 struct qmi_response_type_v01 resp
;
241 static struct qmi_elem_info ssctl_subsys_event_resp_ei
[] = {
243 .data_type
= QMI_STRUCT
,
245 .elem_size
= sizeof(struct qmi_response_type_v01
),
246 .array_type
= NO_ARRAY
,
248 .offset
= offsetof(struct ssctl_subsys_event_resp
,
250 .ei_array
= qmi_response_type_v01_ei
,
256 * ssctl_request_shutdown() - request shutdown via SSCTL QMI service
257 * @sysmon: sysmon context
259 static void ssctl_request_shutdown(struct qcom_sysmon
*sysmon
)
261 struct ssctl_shutdown_resp resp
;
265 ret
= qmi_txn_init(&sysmon
->qmi
, &txn
, ssctl_shutdown_resp_ei
, &resp
);
267 dev_err(sysmon
->dev
, "failed to allocate QMI txn\n");
271 ret
= qmi_send_request(&sysmon
->qmi
, &sysmon
->ssctl
, &txn
,
272 SSCTL_SHUTDOWN_REQ
, 0, NULL
, NULL
);
274 dev_err(sysmon
->dev
, "failed to send shutdown request\n");
275 qmi_txn_cancel(&txn
);
279 ret
= qmi_txn_wait(&txn
, 5 * HZ
);
281 dev_err(sysmon
->dev
, "failed receiving QMI response\n");
282 else if (resp
.resp
.result
)
283 dev_err(sysmon
->dev
, "shutdown request failed\n");
285 dev_dbg(sysmon
->dev
, "shutdown request completed\n");
289 * ssctl_send_event() - send notification of other remote's SSR event
290 * @sysmon: sysmon context
291 * @name: other remote's name
293 static void ssctl_send_event(struct qcom_sysmon
*sysmon
, const char *name
)
295 struct ssctl_subsys_event_resp resp
;
296 struct ssctl_subsys_event_req req
;
300 memset(&resp
, 0, sizeof(resp
));
301 ret
= qmi_txn_init(&sysmon
->qmi
, &txn
, ssctl_subsys_event_resp_ei
, &resp
);
303 dev_err(sysmon
->dev
, "failed to allocate QMI txn\n");
307 memset(&req
, 0, sizeof(req
));
308 strlcpy(req
.subsys_name
, name
, sizeof(req
.subsys_name
));
309 req
.subsys_name_len
= strlen(req
.subsys_name
);
310 req
.event
= SSCTL_SSR_EVENT_BEFORE_SHUTDOWN
;
311 req
.evt_driven_valid
= true;
312 req
.evt_driven
= SSCTL_SSR_EVENT_FORCED
;
314 ret
= qmi_send_request(&sysmon
->qmi
, &sysmon
->ssctl
, &txn
,
315 SSCTL_SUBSYS_EVENT_REQ
, 40,
316 ssctl_subsys_event_req_ei
, &req
);
318 dev_err(sysmon
->dev
, "failed to send shutdown request\n");
319 qmi_txn_cancel(&txn
);
323 ret
= qmi_txn_wait(&txn
, 5 * HZ
);
325 dev_err(sysmon
->dev
, "failed receiving QMI response\n");
326 else if (resp
.resp
.result
)
327 dev_err(sysmon
->dev
, "ssr event send failed\n");
329 dev_dbg(sysmon
->dev
, "ssr event send completed\n");
333 * ssctl_new_server() - QMI callback indicating a new service
335 * @svc: service information
337 * Return: 0 if we're interested in this service, -EINVAL otherwise.
339 static int ssctl_new_server(struct qmi_handle
*qmi
, struct qmi_service
*svc
)
341 struct qcom_sysmon
*sysmon
= container_of(qmi
, struct qcom_sysmon
, qmi
);
343 switch (svc
->version
) {
345 if (svc
->instance
!= 0)
347 if (strcmp(sysmon
->name
, "modem"))
351 if (svc
->instance
!= sysmon
->ssctl_instance
)
358 sysmon
->ssctl_version
= svc
->version
;
360 sysmon
->ssctl
.sq_family
= AF_QIPCRTR
;
361 sysmon
->ssctl
.sq_node
= svc
->node
;
362 sysmon
->ssctl
.sq_port
= svc
->port
;
370 * ssctl_del_server() - QMI callback indicating that @svc is removed
372 * @svc: service information
374 static void ssctl_del_server(struct qmi_handle
*qmi
, struct qmi_service
*svc
)
376 struct qcom_sysmon
*sysmon
= svc
->priv
;
378 sysmon
->ssctl_version
= 0;
381 static const struct qmi_ops ssctl_ops
= {
382 .new_server
= ssctl_new_server
,
383 .del_server
= ssctl_del_server
,
386 static int sysmon_start(struct rproc_subdev
*subdev
)
391 static void sysmon_stop(struct rproc_subdev
*subdev
, bool crashed
)
393 struct qcom_sysmon
*sysmon
= container_of(subdev
, struct qcom_sysmon
, subdev
);
395 blocking_notifier_call_chain(&sysmon_notifiers
, 0, (void *)sysmon
->name
);
397 /* Don't request graceful shutdown if we've crashed */
401 if (sysmon
->ssctl_version
)
402 ssctl_request_shutdown(sysmon
);
403 else if (sysmon
->ept
)
404 sysmon_request_shutdown(sysmon
);
408 * sysmon_notify() - notify sysmon target of another's SSR
409 * @nb: notifier_block associated with sysmon instance
411 * @data: SSR identifier of the remote that is going down
413 static int sysmon_notify(struct notifier_block
*nb
, unsigned long event
,
416 struct qcom_sysmon
*sysmon
= container_of(nb
, struct qcom_sysmon
, nb
);
417 struct rproc
*rproc
= sysmon
->rproc
;
418 const char *ssr_name
= data
;
420 /* Skip non-running rprocs and the originating instance */
421 if (rproc
->state
!= RPROC_RUNNING
|| !strcmp(data
, sysmon
->name
)) {
422 dev_dbg(sysmon
->dev
, "not notifying %s\n", sysmon
->name
);
426 /* Only SSCTL version 2 supports SSR events */
427 if (sysmon
->ssctl_version
== 2)
428 ssctl_send_event(sysmon
, ssr_name
);
429 else if (sysmon
->ept
)
430 sysmon_send_event(sysmon
, ssr_name
);
436 * qcom_add_sysmon_subdev() - create a sysmon subdev for the given remoteproc
437 * @rproc: rproc context to associate the subdev with
438 * @name: name of this subdev, to use in SSR
439 * @ssctl_instance: instance id of the ssctl QMI service
441 * Return: A new qcom_sysmon object, or NULL on failure
443 struct qcom_sysmon
*qcom_add_sysmon_subdev(struct rproc
*rproc
,
447 struct qcom_sysmon
*sysmon
;
450 sysmon
= kzalloc(sizeof(*sysmon
), GFP_KERNEL
);
454 sysmon
->dev
= rproc
->dev
.parent
;
455 sysmon
->rproc
= rproc
;
458 sysmon
->ssctl_instance
= ssctl_instance
;
460 init_completion(&sysmon
->comp
);
461 mutex_init(&sysmon
->lock
);
463 ret
= qmi_handle_init(&sysmon
->qmi
, SSCTL_MAX_MSG_LEN
, &ssctl_ops
, NULL
);
465 dev_err(sysmon
->dev
, "failed to initialize qmi handle\n");
470 qmi_add_lookup(&sysmon
->qmi
, 43, 0, 0);
472 rproc_add_subdev(rproc
, &sysmon
->subdev
, sysmon_start
, sysmon_stop
);
474 sysmon
->nb
.notifier_call
= sysmon_notify
;
475 blocking_notifier_chain_register(&sysmon_notifiers
, &sysmon
->nb
);
477 mutex_lock(&sysmon_lock
);
478 list_add(&sysmon
->node
, &sysmon_list
);
479 mutex_unlock(&sysmon_lock
);
483 EXPORT_SYMBOL_GPL(qcom_add_sysmon_subdev
);
486 * qcom_remove_sysmon_subdev() - release a qcom_sysmon
487 * @sysmon: sysmon context, as retrieved by qcom_add_sysmon_subdev()
489 void qcom_remove_sysmon_subdev(struct qcom_sysmon
*sysmon
)
494 mutex_lock(&sysmon_lock
);
495 list_del(&sysmon
->node
);
496 mutex_unlock(&sysmon_lock
);
498 blocking_notifier_chain_unregister(&sysmon_notifiers
, &sysmon
->nb
);
500 rproc_remove_subdev(sysmon
->rproc
, &sysmon
->subdev
);
502 qmi_handle_release(&sysmon
->qmi
);
506 EXPORT_SYMBOL_GPL(qcom_remove_sysmon_subdev
);
509 * sysmon_probe() - probe sys_mon channel
510 * @rpdev: rpmsg device handle
512 * Find the sysmon context associated with the ancestor remoteproc and assign
513 * this rpmsg device with said sysmon context.
515 * Return: 0 on success, negative errno on failure.
517 static int sysmon_probe(struct rpmsg_device
*rpdev
)
519 struct qcom_sysmon
*sysmon
;
522 rproc
= rproc_get_by_child(&rpdev
->dev
);
524 dev_err(&rpdev
->dev
, "sysmon device not child of rproc\n");
528 mutex_lock(&sysmon_lock
);
529 list_for_each_entry(sysmon
, &sysmon_list
, node
) {
530 if (sysmon
->rproc
== rproc
)
533 mutex_unlock(&sysmon_lock
);
535 dev_err(&rpdev
->dev
, "no sysmon associated with parent rproc\n");
540 mutex_unlock(&sysmon_lock
);
542 rpdev
->ept
->priv
= sysmon
;
543 sysmon
->ept
= rpdev
->ept
;
549 * sysmon_remove() - sys_mon channel remove handler
550 * @rpdev: rpmsg device handle
552 * Disassociate the rpmsg device with the sysmon instance.
554 static void sysmon_remove(struct rpmsg_device
*rpdev
)
556 struct qcom_sysmon
*sysmon
= rpdev
->ept
->priv
;
561 static const struct rpmsg_device_id sysmon_match
[] = {
566 static struct rpmsg_driver sysmon_driver
= {
567 .probe
= sysmon_probe
,
568 .remove
= sysmon_remove
,
569 .callback
= sysmon_callback
,
570 .id_table
= sysmon_match
,
572 .name
= "qcom_sysmon",
576 module_rpmsg_driver(sysmon_driver
);
578 MODULE_DESCRIPTION("Qualcomm sysmon driver");
579 MODULE_LICENSE("GPL v2");