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>
9 #include <linux/interrupt.h>
11 #include <linux/of_irq.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/remoteproc/qcom_rproc.h>
15 #include <linux/rpmsg.h>
17 #include "qcom_common.h"
19 static BLOCKING_NOTIFIER_HEAD(sysmon_notifiers);
22 struct rproc_subdev subdev;
25 struct list_head node;
33 struct notifier_block nb;
37 struct rpmsg_endpoint *ept;
38 struct completion comp;
39 struct completion ind_comp;
40 struct completion shutdown_comp;
45 struct qmi_handle qmi;
46 struct sockaddr_qrtr ssctl;
50 SSCTL_SSR_EVENT_BEFORE_POWERUP,
51 SSCTL_SSR_EVENT_AFTER_POWERUP,
52 SSCTL_SSR_EVENT_BEFORE_SHUTDOWN,
53 SSCTL_SSR_EVENT_AFTER_SHUTDOWN,
56 static const char * const sysmon_state_string[] = {
57 [SSCTL_SSR_EVENT_BEFORE_POWERUP] = "before_powerup",
58 [SSCTL_SSR_EVENT_AFTER_POWERUP] = "after_powerup",
59 [SSCTL_SSR_EVENT_BEFORE_SHUTDOWN] = "before_shutdown",
60 [SSCTL_SSR_EVENT_AFTER_SHUTDOWN] = "after_shutdown",
64 const char *subsys_name;
68 static DEFINE_MUTEX(sysmon_lock);
69 static LIST_HEAD(sysmon_list);
72 * sysmon_send_event() - send notification of other remote's SSR event
73 * @sysmon: sysmon context
74 * @name: other remote's name
76 static void sysmon_send_event(struct qcom_sysmon *sysmon,
77 const struct sysmon_event *event)
83 len = snprintf(req, sizeof(req), "ssr:%s:%s", event->subsys_name,
84 sysmon_state_string[event->ssr_event]);
85 if (len >= sizeof(req))
88 mutex_lock(&sysmon->lock);
89 reinit_completion(&sysmon->comp);
90 sysmon->ssr_ack = false;
92 ret = rpmsg_send(sysmon->ept, req, len);
94 dev_err(sysmon->dev, "failed to send sysmon event\n");
98 ret = wait_for_completion_timeout(&sysmon->comp,
99 msecs_to_jiffies(5000));
101 dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
105 if (!sysmon->ssr_ack)
106 dev_err(sysmon->dev, "unexpected response to sysmon event\n");
109 mutex_unlock(&sysmon->lock);
113 * sysmon_request_shutdown() - request graceful shutdown of remote
114 * @sysmon: sysmon context
116 static void sysmon_request_shutdown(struct qcom_sysmon *sysmon)
118 char *req = "ssr:shutdown";
121 mutex_lock(&sysmon->lock);
122 reinit_completion(&sysmon->comp);
123 sysmon->ssr_ack = false;
125 ret = rpmsg_send(sysmon->ept, req, strlen(req) + 1);
127 dev_err(sysmon->dev, "send sysmon shutdown request failed\n");
131 ret = wait_for_completion_timeout(&sysmon->comp,
132 msecs_to_jiffies(5000));
134 dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
138 if (!sysmon->ssr_ack)
140 "unexpected response to sysmon shutdown request\n");
143 mutex_unlock(&sysmon->lock);
146 static int sysmon_callback(struct rpmsg_device *rpdev, void *data, int count,
147 void *priv, u32 addr)
149 struct qcom_sysmon *sysmon = priv;
150 const char *ssr_ack = "ssr:ack";
151 const int ssr_ack_len = strlen(ssr_ack) + 1;
156 if (count >= ssr_ack_len && !memcmp(data, ssr_ack, ssr_ack_len))
157 sysmon->ssr_ack = true;
159 complete(&sysmon->comp);
164 #define SSCTL_SHUTDOWN_REQ 0x21
165 #define SSCTL_SHUTDOWN_READY_IND 0x21
166 #define SSCTL_SUBSYS_EVENT_REQ 0x23
168 #define SSCTL_MAX_MSG_LEN 7
170 #define SSCTL_SUBSYS_NAME_LENGTH 15
173 SSCTL_SSR_EVENT_FORCED,
174 SSCTL_SSR_EVENT_GRACEFUL,
177 struct ssctl_shutdown_resp {
178 struct qmi_response_type_v01 resp;
181 static struct qmi_elem_info ssctl_shutdown_resp_ei[] = {
183 .data_type = QMI_STRUCT,
185 .elem_size = sizeof(struct qmi_response_type_v01),
186 .array_type = NO_ARRAY,
188 .offset = offsetof(struct ssctl_shutdown_resp, resp),
189 .ei_array = qmi_response_type_v01_ei,
194 struct ssctl_subsys_event_req {
196 char subsys_name[SSCTL_SUBSYS_NAME_LENGTH];
202 static struct qmi_elem_info ssctl_subsys_event_req_ei[] = {
204 .data_type = QMI_DATA_LEN,
206 .elem_size = sizeof(uint8_t),
207 .array_type = NO_ARRAY,
209 .offset = offsetof(struct ssctl_subsys_event_req,
214 .data_type = QMI_UNSIGNED_1_BYTE,
215 .elem_len = SSCTL_SUBSYS_NAME_LENGTH,
216 .elem_size = sizeof(char),
217 .array_type = VAR_LEN_ARRAY,
219 .offset = offsetof(struct ssctl_subsys_event_req,
224 .data_type = QMI_SIGNED_4_BYTE_ENUM,
226 .elem_size = sizeof(uint32_t),
227 .array_type = NO_ARRAY,
229 .offset = offsetof(struct ssctl_subsys_event_req,
234 .data_type = QMI_OPT_FLAG,
236 .elem_size = sizeof(uint8_t),
237 .array_type = NO_ARRAY,
239 .offset = offsetof(struct ssctl_subsys_event_req,
244 .data_type = QMI_SIGNED_4_BYTE_ENUM,
246 .elem_size = sizeof(uint32_t),
247 .array_type = NO_ARRAY,
249 .offset = offsetof(struct ssctl_subsys_event_req,
256 struct ssctl_subsys_event_resp {
257 struct qmi_response_type_v01 resp;
260 static struct qmi_elem_info ssctl_subsys_event_resp_ei[] = {
262 .data_type = QMI_STRUCT,
264 .elem_size = sizeof(struct qmi_response_type_v01),
265 .array_type = NO_ARRAY,
267 .offset = offsetof(struct ssctl_subsys_event_resp,
269 .ei_array = qmi_response_type_v01_ei,
274 static struct qmi_elem_info ssctl_shutdown_ind_ei[] = {
278 static void sysmon_ind_cb(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
279 struct qmi_txn *txn, const void *data)
281 struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
283 complete(&sysmon->ind_comp);
286 static struct qmi_msg_handler qmi_indication_handler[] = {
288 .type = QMI_INDICATION,
289 .msg_id = SSCTL_SHUTDOWN_READY_IND,
290 .ei = ssctl_shutdown_ind_ei,
298 * ssctl_request_shutdown() - request shutdown via SSCTL QMI service
299 * @sysmon: sysmon context
301 static void ssctl_request_shutdown(struct qcom_sysmon *sysmon)
303 struct ssctl_shutdown_resp resp;
307 reinit_completion(&sysmon->ind_comp);
308 reinit_completion(&sysmon->shutdown_comp);
309 ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_shutdown_resp_ei, &resp);
311 dev_err(sysmon->dev, "failed to allocate QMI txn\n");
315 ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
316 SSCTL_SHUTDOWN_REQ, 0, NULL, NULL);
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, "shutdown request failed\n");
329 dev_dbg(sysmon->dev, "shutdown request completed\n");
331 if (sysmon->shutdown_irq > 0) {
332 ret = wait_for_completion_timeout(&sysmon->shutdown_comp,
335 ret = try_wait_for_completion(&sysmon->ind_comp);
338 "timeout waiting for shutdown ack\n");
344 * ssctl_send_event() - send notification of other remote's SSR event
345 * @sysmon: sysmon context
346 * @name: other remote's name
348 static void ssctl_send_event(struct qcom_sysmon *sysmon,
349 const struct sysmon_event *event)
351 struct ssctl_subsys_event_resp resp;
352 struct ssctl_subsys_event_req req;
356 memset(&resp, 0, sizeof(resp));
357 ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_subsys_event_resp_ei, &resp);
359 dev_err(sysmon->dev, "failed to allocate QMI txn\n");
363 memset(&req, 0, sizeof(req));
364 strlcpy(req.subsys_name, event->subsys_name, sizeof(req.subsys_name));
365 req.subsys_name_len = strlen(req.subsys_name);
366 req.event = event->ssr_event;
367 req.evt_driven_valid = true;
368 req.evt_driven = SSCTL_SSR_EVENT_FORCED;
370 ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
371 SSCTL_SUBSYS_EVENT_REQ, 40,
372 ssctl_subsys_event_req_ei, &req);
374 dev_err(sysmon->dev, "failed to send shutdown request\n");
375 qmi_txn_cancel(&txn);
379 ret = qmi_txn_wait(&txn, 5 * HZ);
381 dev_err(sysmon->dev, "failed receiving QMI response\n");
382 else if (resp.resp.result)
383 dev_err(sysmon->dev, "ssr event send failed\n");
385 dev_dbg(sysmon->dev, "ssr event send completed\n");
389 * ssctl_new_server() - QMI callback indicating a new service
391 * @svc: service information
393 * Return: 0 if we're interested in this service, -EINVAL otherwise.
395 static int ssctl_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
397 struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
399 switch (svc->version) {
401 if (svc->instance != 0)
403 if (strcmp(sysmon->name, "modem"))
407 if (svc->instance != sysmon->ssctl_instance)
414 sysmon->ssctl_version = svc->version;
416 sysmon->ssctl.sq_family = AF_QIPCRTR;
417 sysmon->ssctl.sq_node = svc->node;
418 sysmon->ssctl.sq_port = svc->port;
426 * ssctl_del_server() - QMI callback indicating that @svc is removed
428 * @svc: service information
430 static void ssctl_del_server(struct qmi_handle *qmi, struct qmi_service *svc)
432 struct qcom_sysmon *sysmon = svc->priv;
434 sysmon->ssctl_version = 0;
437 static const struct qmi_ops ssctl_ops = {
438 .new_server = ssctl_new_server,
439 .del_server = ssctl_del_server,
442 static int sysmon_prepare(struct rproc_subdev *subdev)
444 struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
446 struct sysmon_event event = {
447 .subsys_name = sysmon->name,
448 .ssr_event = SSCTL_SSR_EVENT_BEFORE_POWERUP
451 blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
457 * sysmon_start() - start callback for the sysmon remoteproc subdevice
458 * @subdev: instance of the sysmon subdevice
460 * Inform all the listners of sysmon notifications that the rproc associated
461 * to @subdev has booted up. The rproc that booted up also needs to know
462 * which rprocs are already up and running, so send start notifications
463 * on behalf of all the online rprocs.
465 static int sysmon_start(struct rproc_subdev *subdev)
467 struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
469 struct qcom_sysmon *target;
470 struct sysmon_event event = {
471 .subsys_name = sysmon->name,
472 .ssr_event = SSCTL_SSR_EVENT_AFTER_POWERUP
475 blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
477 mutex_lock(&sysmon_lock);
478 list_for_each_entry(target, &sysmon_list, node) {
479 if (target == sysmon ||
480 target->rproc->state != RPROC_RUNNING)
483 event.subsys_name = target->name;
485 if (sysmon->ssctl_version == 2)
486 ssctl_send_event(sysmon, &event);
487 else if (sysmon->ept)
488 sysmon_send_event(sysmon, &event);
490 mutex_unlock(&sysmon_lock);
495 static void sysmon_stop(struct rproc_subdev *subdev, bool crashed)
497 struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon, subdev);
498 struct sysmon_event event = {
499 .subsys_name = sysmon->name,
500 .ssr_event = SSCTL_SSR_EVENT_BEFORE_SHUTDOWN
503 blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
505 /* Don't request graceful shutdown if we've crashed */
509 if (sysmon->ssctl_version)
510 ssctl_request_shutdown(sysmon);
511 else if (sysmon->ept)
512 sysmon_request_shutdown(sysmon);
515 static void sysmon_unprepare(struct rproc_subdev *subdev)
517 struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
519 struct sysmon_event event = {
520 .subsys_name = sysmon->name,
521 .ssr_event = SSCTL_SSR_EVENT_AFTER_SHUTDOWN
524 blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
528 * sysmon_notify() - notify sysmon target of another's SSR
529 * @nb: notifier_block associated with sysmon instance
531 * @data: SSR identifier of the remote that is going down
533 static int sysmon_notify(struct notifier_block *nb, unsigned long event,
536 struct qcom_sysmon *sysmon = container_of(nb, struct qcom_sysmon, nb);
537 struct rproc *rproc = sysmon->rproc;
538 struct sysmon_event *sysmon_event = data;
540 /* Skip non-running rprocs and the originating instance */
541 if (rproc->state != RPROC_RUNNING ||
542 !strcmp(sysmon_event->subsys_name, sysmon->name)) {
543 dev_dbg(sysmon->dev, "not notifying %s\n", sysmon->name);
547 /* Only SSCTL version 2 supports SSR events */
548 if (sysmon->ssctl_version == 2)
549 ssctl_send_event(sysmon, sysmon_event);
550 else if (sysmon->ept)
551 sysmon_send_event(sysmon, sysmon_event);
556 static irqreturn_t sysmon_shutdown_interrupt(int irq, void *data)
558 struct qcom_sysmon *sysmon = data;
560 complete(&sysmon->shutdown_comp);
566 * qcom_add_sysmon_subdev() - create a sysmon subdev for the given remoteproc
567 * @rproc: rproc context to associate the subdev with
568 * @name: name of this subdev, to use in SSR
569 * @ssctl_instance: instance id of the ssctl QMI service
571 * Return: A new qcom_sysmon object, or NULL on failure
573 struct qcom_sysmon *qcom_add_sysmon_subdev(struct rproc *rproc,
577 struct qcom_sysmon *sysmon;
580 sysmon = kzalloc(sizeof(*sysmon), GFP_KERNEL);
582 return ERR_PTR(-ENOMEM);
584 sysmon->dev = rproc->dev.parent;
585 sysmon->rproc = rproc;
588 sysmon->ssctl_instance = ssctl_instance;
590 init_completion(&sysmon->comp);
591 init_completion(&sysmon->ind_comp);
592 init_completion(&sysmon->shutdown_comp);
593 mutex_init(&sysmon->lock);
595 sysmon->shutdown_irq = of_irq_get_byname(sysmon->dev->of_node,
597 if (sysmon->shutdown_irq < 0) {
598 if (sysmon->shutdown_irq != -ENODATA) {
600 "failed to retrieve shutdown-ack IRQ\n");
601 return ERR_PTR(sysmon->shutdown_irq);
604 ret = devm_request_threaded_irq(sysmon->dev,
605 sysmon->shutdown_irq,
606 NULL, sysmon_shutdown_interrupt,
607 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
608 "q6v5 shutdown-ack", sysmon);
611 "failed to acquire shutdown-ack IRQ\n");
616 ret = qmi_handle_init(&sysmon->qmi, SSCTL_MAX_MSG_LEN, &ssctl_ops,
617 qmi_indication_handler);
619 dev_err(sysmon->dev, "failed to initialize qmi handle\n");
624 qmi_add_lookup(&sysmon->qmi, 43, 0, 0);
626 sysmon->subdev.prepare = sysmon_prepare;
627 sysmon->subdev.start = sysmon_start;
628 sysmon->subdev.stop = sysmon_stop;
629 sysmon->subdev.unprepare = sysmon_unprepare;
631 rproc_add_subdev(rproc, &sysmon->subdev);
633 sysmon->nb.notifier_call = sysmon_notify;
634 blocking_notifier_chain_register(&sysmon_notifiers, &sysmon->nb);
636 mutex_lock(&sysmon_lock);
637 list_add(&sysmon->node, &sysmon_list);
638 mutex_unlock(&sysmon_lock);
642 EXPORT_SYMBOL_GPL(qcom_add_sysmon_subdev);
645 * qcom_remove_sysmon_subdev() - release a qcom_sysmon
646 * @sysmon: sysmon context, as retrieved by qcom_add_sysmon_subdev()
648 void qcom_remove_sysmon_subdev(struct qcom_sysmon *sysmon)
653 mutex_lock(&sysmon_lock);
654 list_del(&sysmon->node);
655 mutex_unlock(&sysmon_lock);
657 blocking_notifier_chain_unregister(&sysmon_notifiers, &sysmon->nb);
659 rproc_remove_subdev(sysmon->rproc, &sysmon->subdev);
661 qmi_handle_release(&sysmon->qmi);
665 EXPORT_SYMBOL_GPL(qcom_remove_sysmon_subdev);
668 * sysmon_probe() - probe sys_mon channel
669 * @rpdev: rpmsg device handle
671 * Find the sysmon context associated with the ancestor remoteproc and assign
672 * this rpmsg device with said sysmon context.
674 * Return: 0 on success, negative errno on failure.
676 static int sysmon_probe(struct rpmsg_device *rpdev)
678 struct qcom_sysmon *sysmon;
681 rproc = rproc_get_by_child(&rpdev->dev);
683 dev_err(&rpdev->dev, "sysmon device not child of rproc\n");
687 mutex_lock(&sysmon_lock);
688 list_for_each_entry(sysmon, &sysmon_list, node) {
689 if (sysmon->rproc == rproc)
692 mutex_unlock(&sysmon_lock);
694 dev_err(&rpdev->dev, "no sysmon associated with parent rproc\n");
699 mutex_unlock(&sysmon_lock);
701 rpdev->ept->priv = sysmon;
702 sysmon->ept = rpdev->ept;
708 * sysmon_remove() - sys_mon channel remove handler
709 * @rpdev: rpmsg device handle
711 * Disassociate the rpmsg device with the sysmon instance.
713 static void sysmon_remove(struct rpmsg_device *rpdev)
715 struct qcom_sysmon *sysmon = rpdev->ept->priv;
720 static const struct rpmsg_device_id sysmon_match[] = {
725 static struct rpmsg_driver sysmon_driver = {
726 .probe = sysmon_probe,
727 .remove = sysmon_remove,
728 .callback = sysmon_callback,
729 .id_table = sysmon_match,
731 .name = "qcom_sysmon",
735 module_rpmsg_driver(sysmon_driver);
737 MODULE_DESCRIPTION("Qualcomm sysmon driver");
738 MODULE_LICENSE("GPL v2");