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;
26 struct mutex state_lock;
28 struct list_head node;
36 struct notifier_block nb;
40 struct rpmsg_endpoint *ept;
41 struct completion comp;
42 struct completion ind_comp;
43 struct completion shutdown_comp;
44 struct completion ssctl_comp;
49 struct qmi_handle qmi;
50 struct sockaddr_qrtr ssctl;
54 SSCTL_SSR_EVENT_BEFORE_POWERUP,
55 SSCTL_SSR_EVENT_AFTER_POWERUP,
56 SSCTL_SSR_EVENT_BEFORE_SHUTDOWN,
57 SSCTL_SSR_EVENT_AFTER_SHUTDOWN,
60 static const char * const sysmon_state_string[] = {
61 [SSCTL_SSR_EVENT_BEFORE_POWERUP] = "before_powerup",
62 [SSCTL_SSR_EVENT_AFTER_POWERUP] = "after_powerup",
63 [SSCTL_SSR_EVENT_BEFORE_SHUTDOWN] = "before_shutdown",
64 [SSCTL_SSR_EVENT_AFTER_SHUTDOWN] = "after_shutdown",
68 const char *subsys_name;
72 static DEFINE_MUTEX(sysmon_lock);
73 static LIST_HEAD(sysmon_list);
76 * sysmon_send_event() - send notification of other remote's SSR event
77 * @sysmon: sysmon context
78 * @event: sysmon event context
80 static void sysmon_send_event(struct qcom_sysmon *sysmon,
81 const struct sysmon_event *event)
87 len = snprintf(req, sizeof(req), "ssr:%s:%s", event->subsys_name,
88 sysmon_state_string[event->ssr_event]);
89 if (len >= sizeof(req))
92 mutex_lock(&sysmon->lock);
93 reinit_completion(&sysmon->comp);
94 sysmon->ssr_ack = false;
96 ret = rpmsg_send(sysmon->ept, req, len);
98 dev_err(sysmon->dev, "failed to send sysmon event\n");
102 ret = wait_for_completion_timeout(&sysmon->comp,
103 msecs_to_jiffies(5000));
105 dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
109 if (!sysmon->ssr_ack)
110 dev_err(sysmon->dev, "unexpected response to sysmon event\n");
113 mutex_unlock(&sysmon->lock);
117 * sysmon_request_shutdown() - request graceful shutdown of remote
118 * @sysmon: sysmon context
120 static void sysmon_request_shutdown(struct qcom_sysmon *sysmon)
122 char *req = "ssr:shutdown";
125 mutex_lock(&sysmon->lock);
126 reinit_completion(&sysmon->comp);
127 sysmon->ssr_ack = false;
129 ret = rpmsg_send(sysmon->ept, req, strlen(req) + 1);
131 dev_err(sysmon->dev, "send sysmon shutdown request failed\n");
135 ret = wait_for_completion_timeout(&sysmon->comp,
136 msecs_to_jiffies(5000));
138 dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
142 if (!sysmon->ssr_ack)
144 "unexpected response to sysmon shutdown request\n");
147 mutex_unlock(&sysmon->lock);
150 static int sysmon_callback(struct rpmsg_device *rpdev, void *data, int count,
151 void *priv, u32 addr)
153 struct qcom_sysmon *sysmon = priv;
154 const char *ssr_ack = "ssr:ack";
155 const int ssr_ack_len = strlen(ssr_ack) + 1;
160 if (count >= ssr_ack_len && !memcmp(data, ssr_ack, ssr_ack_len))
161 sysmon->ssr_ack = true;
163 complete(&sysmon->comp);
168 #define SSCTL_SHUTDOWN_REQ 0x21
169 #define SSCTL_SHUTDOWN_READY_IND 0x21
170 #define SSCTL_SUBSYS_EVENT_REQ 0x23
172 #define SSCTL_MAX_MSG_LEN 7
174 #define SSCTL_SUBSYS_NAME_LENGTH 15
177 SSCTL_SSR_EVENT_FORCED,
178 SSCTL_SSR_EVENT_GRACEFUL,
181 struct ssctl_shutdown_resp {
182 struct qmi_response_type_v01 resp;
185 static struct qmi_elem_info ssctl_shutdown_resp_ei[] = {
187 .data_type = QMI_STRUCT,
189 .elem_size = sizeof(struct qmi_response_type_v01),
190 .array_type = NO_ARRAY,
192 .offset = offsetof(struct ssctl_shutdown_resp, resp),
193 .ei_array = qmi_response_type_v01_ei,
198 struct ssctl_subsys_event_req {
200 char subsys_name[SSCTL_SUBSYS_NAME_LENGTH];
206 static struct qmi_elem_info ssctl_subsys_event_req_ei[] = {
208 .data_type = QMI_DATA_LEN,
210 .elem_size = sizeof(uint8_t),
211 .array_type = NO_ARRAY,
213 .offset = offsetof(struct ssctl_subsys_event_req,
218 .data_type = QMI_UNSIGNED_1_BYTE,
219 .elem_len = SSCTL_SUBSYS_NAME_LENGTH,
220 .elem_size = sizeof(char),
221 .array_type = VAR_LEN_ARRAY,
223 .offset = offsetof(struct ssctl_subsys_event_req,
228 .data_type = QMI_SIGNED_4_BYTE_ENUM,
230 .elem_size = sizeof(uint32_t),
231 .array_type = NO_ARRAY,
233 .offset = offsetof(struct ssctl_subsys_event_req,
238 .data_type = QMI_OPT_FLAG,
240 .elem_size = sizeof(uint8_t),
241 .array_type = NO_ARRAY,
243 .offset = offsetof(struct ssctl_subsys_event_req,
248 .data_type = QMI_SIGNED_4_BYTE_ENUM,
250 .elem_size = sizeof(uint32_t),
251 .array_type = NO_ARRAY,
253 .offset = offsetof(struct ssctl_subsys_event_req,
260 struct ssctl_subsys_event_resp {
261 struct qmi_response_type_v01 resp;
264 static struct qmi_elem_info ssctl_subsys_event_resp_ei[] = {
266 .data_type = QMI_STRUCT,
268 .elem_size = sizeof(struct qmi_response_type_v01),
269 .array_type = NO_ARRAY,
271 .offset = offsetof(struct ssctl_subsys_event_resp,
273 .ei_array = qmi_response_type_v01_ei,
278 static struct qmi_elem_info ssctl_shutdown_ind_ei[] = {
282 static void sysmon_ind_cb(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
283 struct qmi_txn *txn, const void *data)
285 struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
287 complete(&sysmon->ind_comp);
290 static struct qmi_msg_handler qmi_indication_handler[] = {
292 .type = QMI_INDICATION,
293 .msg_id = SSCTL_SHUTDOWN_READY_IND,
294 .ei = ssctl_shutdown_ind_ei,
302 * ssctl_request_shutdown() - request shutdown via SSCTL QMI service
303 * @sysmon: sysmon context
305 static void ssctl_request_shutdown(struct qcom_sysmon *sysmon)
307 struct ssctl_shutdown_resp resp;
311 reinit_completion(&sysmon->ind_comp);
312 reinit_completion(&sysmon->shutdown_comp);
313 ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_shutdown_resp_ei, &resp);
315 dev_err(sysmon->dev, "failed to allocate QMI txn\n");
319 ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
320 SSCTL_SHUTDOWN_REQ, 0, NULL, NULL);
322 dev_err(sysmon->dev, "failed to send shutdown request\n");
323 qmi_txn_cancel(&txn);
327 ret = qmi_txn_wait(&txn, 5 * HZ);
329 dev_err(sysmon->dev, "failed receiving QMI response\n");
330 else if (resp.resp.result)
331 dev_err(sysmon->dev, "shutdown request failed\n");
333 dev_dbg(sysmon->dev, "shutdown request completed\n");
335 if (sysmon->shutdown_irq > 0) {
336 ret = wait_for_completion_timeout(&sysmon->shutdown_comp,
339 ret = try_wait_for_completion(&sysmon->ind_comp);
342 "timeout waiting for shutdown ack\n");
348 * ssctl_send_event() - send notification of other remote's SSR event
349 * @sysmon: sysmon context
350 * @event: sysmon event context
352 static void ssctl_send_event(struct qcom_sysmon *sysmon,
353 const struct sysmon_event *event)
355 struct ssctl_subsys_event_resp resp;
356 struct ssctl_subsys_event_req req;
360 memset(&resp, 0, sizeof(resp));
361 ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_subsys_event_resp_ei, &resp);
363 dev_err(sysmon->dev, "failed to allocate QMI txn\n");
367 memset(&req, 0, sizeof(req));
368 strlcpy(req.subsys_name, event->subsys_name, sizeof(req.subsys_name));
369 req.subsys_name_len = strlen(req.subsys_name);
370 req.event = event->ssr_event;
371 req.evt_driven_valid = true;
372 req.evt_driven = SSCTL_SSR_EVENT_FORCED;
374 ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
375 SSCTL_SUBSYS_EVENT_REQ, 40,
376 ssctl_subsys_event_req_ei, &req);
378 dev_err(sysmon->dev, "failed to send shutdown request\n");
379 qmi_txn_cancel(&txn);
383 ret = qmi_txn_wait(&txn, 5 * HZ);
385 dev_err(sysmon->dev, "failed receiving QMI response\n");
386 else if (resp.resp.result)
387 dev_err(sysmon->dev, "ssr event send failed\n");
389 dev_dbg(sysmon->dev, "ssr event send completed\n");
393 * ssctl_new_server() - QMI callback indicating a new service
395 * @svc: service information
397 * Return: 0 if we're interested in this service, -EINVAL otherwise.
399 static int ssctl_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
401 struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
403 switch (svc->version) {
405 if (svc->instance != 0)
407 if (strcmp(sysmon->name, "modem"))
411 if (svc->instance != sysmon->ssctl_instance)
418 sysmon->ssctl_version = svc->version;
420 sysmon->ssctl.sq_family = AF_QIPCRTR;
421 sysmon->ssctl.sq_node = svc->node;
422 sysmon->ssctl.sq_port = svc->port;
426 complete(&sysmon->ssctl_comp);
432 * ssctl_del_server() - QMI callback indicating that @svc is removed
434 * @svc: service information
436 static void ssctl_del_server(struct qmi_handle *qmi, struct qmi_service *svc)
438 struct qcom_sysmon *sysmon = svc->priv;
440 sysmon->ssctl_version = 0;
443 static const struct qmi_ops ssctl_ops = {
444 .new_server = ssctl_new_server,
445 .del_server = ssctl_del_server,
448 static int sysmon_prepare(struct rproc_subdev *subdev)
450 struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
452 struct sysmon_event event = {
453 .subsys_name = sysmon->name,
454 .ssr_event = SSCTL_SSR_EVENT_BEFORE_POWERUP
457 mutex_lock(&sysmon->state_lock);
458 sysmon->state = SSCTL_SSR_EVENT_BEFORE_POWERUP;
459 blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
460 mutex_unlock(&sysmon->state_lock);
466 * sysmon_start() - start callback for the sysmon remoteproc subdevice
467 * @subdev: instance of the sysmon subdevice
469 * Inform all the listners of sysmon notifications that the rproc associated
470 * to @subdev has booted up. The rproc that booted up also needs to know
471 * which rprocs are already up and running, so send start notifications
472 * on behalf of all the online rprocs.
474 static int sysmon_start(struct rproc_subdev *subdev)
476 struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
478 struct qcom_sysmon *target;
479 struct sysmon_event event = {
480 .subsys_name = sysmon->name,
481 .ssr_event = SSCTL_SSR_EVENT_AFTER_POWERUP
484 reinit_completion(&sysmon->ssctl_comp);
485 mutex_lock(&sysmon->state_lock);
486 sysmon->state = SSCTL_SSR_EVENT_AFTER_POWERUP;
487 blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
488 mutex_unlock(&sysmon->state_lock);
490 mutex_lock(&sysmon_lock);
491 list_for_each_entry(target, &sysmon_list, node) {
492 if (target == sysmon)
495 mutex_lock(&target->state_lock);
496 event.subsys_name = target->name;
497 event.ssr_event = target->state;
499 if (sysmon->ssctl_version == 2)
500 ssctl_send_event(sysmon, &event);
501 else if (sysmon->ept)
502 sysmon_send_event(sysmon, &event);
503 mutex_unlock(&target->state_lock);
505 mutex_unlock(&sysmon_lock);
510 static void sysmon_stop(struct rproc_subdev *subdev, bool crashed)
512 struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon, subdev);
513 struct sysmon_event event = {
514 .subsys_name = sysmon->name,
515 .ssr_event = SSCTL_SSR_EVENT_BEFORE_SHUTDOWN
518 mutex_lock(&sysmon->state_lock);
519 sysmon->state = SSCTL_SSR_EVENT_BEFORE_SHUTDOWN;
520 blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
521 mutex_unlock(&sysmon->state_lock);
523 /* Don't request graceful shutdown if we've crashed */
527 if (sysmon->ssctl_instance) {
528 if (!wait_for_completion_timeout(&sysmon->ssctl_comp, HZ / 2))
529 dev_err(sysmon->dev, "timeout waiting for ssctl service\n");
532 if (sysmon->ssctl_version)
533 ssctl_request_shutdown(sysmon);
534 else if (sysmon->ept)
535 sysmon_request_shutdown(sysmon);
538 static void sysmon_unprepare(struct rproc_subdev *subdev)
540 struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
542 struct sysmon_event event = {
543 .subsys_name = sysmon->name,
544 .ssr_event = SSCTL_SSR_EVENT_AFTER_SHUTDOWN
547 mutex_lock(&sysmon->state_lock);
548 sysmon->state = SSCTL_SSR_EVENT_AFTER_SHUTDOWN;
549 blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
550 mutex_unlock(&sysmon->state_lock);
554 * sysmon_notify() - notify sysmon target of another's SSR
555 * @nb: notifier_block associated with sysmon instance
557 * @data: SSR identifier of the remote that is going down
559 static int sysmon_notify(struct notifier_block *nb, unsigned long event,
562 struct qcom_sysmon *sysmon = container_of(nb, struct qcom_sysmon, nb);
563 struct sysmon_event *sysmon_event = data;
565 /* Skip non-running rprocs and the originating instance */
566 if (sysmon->state != SSCTL_SSR_EVENT_AFTER_POWERUP ||
567 !strcmp(sysmon_event->subsys_name, sysmon->name)) {
568 dev_dbg(sysmon->dev, "not notifying %s\n", sysmon->name);
572 /* Only SSCTL version 2 supports SSR events */
573 if (sysmon->ssctl_version == 2)
574 ssctl_send_event(sysmon, sysmon_event);
575 else if (sysmon->ept)
576 sysmon_send_event(sysmon, sysmon_event);
581 static irqreturn_t sysmon_shutdown_interrupt(int irq, void *data)
583 struct qcom_sysmon *sysmon = data;
585 complete(&sysmon->shutdown_comp);
591 * qcom_add_sysmon_subdev() - create a sysmon subdev for the given remoteproc
592 * @rproc: rproc context to associate the subdev with
593 * @name: name of this subdev, to use in SSR
594 * @ssctl_instance: instance id of the ssctl QMI service
596 * Return: A new qcom_sysmon object, or NULL on failure
598 struct qcom_sysmon *qcom_add_sysmon_subdev(struct rproc *rproc,
602 struct qcom_sysmon *sysmon;
605 sysmon = kzalloc(sizeof(*sysmon), GFP_KERNEL);
607 return ERR_PTR(-ENOMEM);
609 sysmon->dev = rproc->dev.parent;
610 sysmon->rproc = rproc;
613 sysmon->ssctl_instance = ssctl_instance;
615 init_completion(&sysmon->comp);
616 init_completion(&sysmon->ind_comp);
617 init_completion(&sysmon->shutdown_comp);
618 init_completion(&sysmon->ssctl_comp);
619 mutex_init(&sysmon->lock);
620 mutex_init(&sysmon->state_lock);
622 sysmon->shutdown_irq = of_irq_get_byname(sysmon->dev->of_node,
624 if (sysmon->shutdown_irq < 0) {
625 if (sysmon->shutdown_irq != -ENODATA) {
627 "failed to retrieve shutdown-ack IRQ\n");
628 return ERR_PTR(sysmon->shutdown_irq);
631 ret = devm_request_threaded_irq(sysmon->dev,
632 sysmon->shutdown_irq,
633 NULL, sysmon_shutdown_interrupt,
634 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
635 "q6v5 shutdown-ack", sysmon);
638 "failed to acquire shutdown-ack IRQ\n");
643 ret = qmi_handle_init(&sysmon->qmi, SSCTL_MAX_MSG_LEN, &ssctl_ops,
644 qmi_indication_handler);
646 dev_err(sysmon->dev, "failed to initialize qmi handle\n");
651 qmi_add_lookup(&sysmon->qmi, 43, 0, 0);
653 sysmon->subdev.prepare = sysmon_prepare;
654 sysmon->subdev.start = sysmon_start;
655 sysmon->subdev.stop = sysmon_stop;
656 sysmon->subdev.unprepare = sysmon_unprepare;
658 rproc_add_subdev(rproc, &sysmon->subdev);
660 sysmon->nb.notifier_call = sysmon_notify;
661 blocking_notifier_chain_register(&sysmon_notifiers, &sysmon->nb);
663 mutex_lock(&sysmon_lock);
664 list_add(&sysmon->node, &sysmon_list);
665 mutex_unlock(&sysmon_lock);
669 EXPORT_SYMBOL_GPL(qcom_add_sysmon_subdev);
672 * qcom_remove_sysmon_subdev() - release a qcom_sysmon
673 * @sysmon: sysmon context, as retrieved by qcom_add_sysmon_subdev()
675 void qcom_remove_sysmon_subdev(struct qcom_sysmon *sysmon)
680 mutex_lock(&sysmon_lock);
681 list_del(&sysmon->node);
682 mutex_unlock(&sysmon_lock);
684 blocking_notifier_chain_unregister(&sysmon_notifiers, &sysmon->nb);
686 rproc_remove_subdev(sysmon->rproc, &sysmon->subdev);
688 qmi_handle_release(&sysmon->qmi);
692 EXPORT_SYMBOL_GPL(qcom_remove_sysmon_subdev);
695 * sysmon_probe() - probe sys_mon channel
696 * @rpdev: rpmsg device handle
698 * Find the sysmon context associated with the ancestor remoteproc and assign
699 * this rpmsg device with said sysmon context.
701 * Return: 0 on success, negative errno on failure.
703 static int sysmon_probe(struct rpmsg_device *rpdev)
705 struct qcom_sysmon *sysmon;
708 rproc = rproc_get_by_child(&rpdev->dev);
710 dev_err(&rpdev->dev, "sysmon device not child of rproc\n");
714 mutex_lock(&sysmon_lock);
715 list_for_each_entry(sysmon, &sysmon_list, node) {
716 if (sysmon->rproc == rproc)
719 mutex_unlock(&sysmon_lock);
721 dev_err(&rpdev->dev, "no sysmon associated with parent rproc\n");
726 mutex_unlock(&sysmon_lock);
728 rpdev->ept->priv = sysmon;
729 sysmon->ept = rpdev->ept;
735 * sysmon_remove() - sys_mon channel remove handler
736 * @rpdev: rpmsg device handle
738 * Disassociate the rpmsg device with the sysmon instance.
740 static void sysmon_remove(struct rpmsg_device *rpdev)
742 struct qcom_sysmon *sysmon = rpdev->ept->priv;
747 static const struct rpmsg_device_id sysmon_match[] = {
752 static struct rpmsg_driver sysmon_driver = {
753 .probe = sysmon_probe,
754 .remove = sysmon_remove,
755 .callback = sysmon_callback,
756 .id_table = sysmon_match,
758 .name = "qcom_sysmon",
762 module_rpmsg_driver(sysmon_driver);
764 MODULE_DESCRIPTION("Qualcomm sysmon driver");
765 MODULE_LICENSE("GPL v2");