.. | .. |
---|
6 | 6 | #include <linux/module.h> |
---|
7 | 7 | #include <linux/notifier.h> |
---|
8 | 8 | #include <linux/slab.h> |
---|
| 9 | +#include <linux/interrupt.h> |
---|
9 | 10 | #include <linux/io.h> |
---|
10 | | -#include <linux/notifier.h> |
---|
| 11 | +#include <linux/of_irq.h> |
---|
11 | 12 | #include <linux/of_platform.h> |
---|
12 | 13 | #include <linux/platform_device.h> |
---|
13 | 14 | #include <linux/remoteproc/qcom_rproc.h> |
---|
.. | .. |
---|
21 | 22 | struct rproc_subdev subdev; |
---|
22 | 23 | struct rproc *rproc; |
---|
23 | 24 | |
---|
| 25 | + int state; |
---|
| 26 | + struct mutex state_lock; |
---|
| 27 | + |
---|
24 | 28 | struct list_head node; |
---|
25 | 29 | |
---|
26 | 30 | const char *name; |
---|
27 | 31 | |
---|
| 32 | + int shutdown_irq; |
---|
28 | 33 | int ssctl_version; |
---|
29 | 34 | int ssctl_instance; |
---|
30 | 35 | |
---|
.. | .. |
---|
34 | 39 | |
---|
35 | 40 | struct rpmsg_endpoint *ept; |
---|
36 | 41 | struct completion comp; |
---|
| 42 | + struct completion ind_comp; |
---|
| 43 | + struct completion shutdown_comp; |
---|
| 44 | + struct completion ssctl_comp; |
---|
37 | 45 | struct mutex lock; |
---|
38 | 46 | |
---|
39 | 47 | bool ssr_ack; |
---|
.. | .. |
---|
42 | 50 | struct sockaddr_qrtr ssctl; |
---|
43 | 51 | }; |
---|
44 | 52 | |
---|
| 53 | +enum { |
---|
| 54 | + SSCTL_SSR_EVENT_BEFORE_POWERUP, |
---|
| 55 | + SSCTL_SSR_EVENT_AFTER_POWERUP, |
---|
| 56 | + SSCTL_SSR_EVENT_BEFORE_SHUTDOWN, |
---|
| 57 | + SSCTL_SSR_EVENT_AFTER_SHUTDOWN, |
---|
| 58 | +}; |
---|
| 59 | + |
---|
| 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", |
---|
| 65 | +}; |
---|
| 66 | + |
---|
| 67 | +struct sysmon_event { |
---|
| 68 | + const char *subsys_name; |
---|
| 69 | + u32 ssr_event; |
---|
| 70 | +}; |
---|
| 71 | + |
---|
45 | 72 | static DEFINE_MUTEX(sysmon_lock); |
---|
46 | 73 | static LIST_HEAD(sysmon_list); |
---|
47 | 74 | |
---|
48 | 75 | /** |
---|
49 | 76 | * sysmon_send_event() - send notification of other remote's SSR event |
---|
50 | 77 | * @sysmon: sysmon context |
---|
51 | | - * @name: other remote's name |
---|
| 78 | + * @event: sysmon event context |
---|
52 | 79 | */ |
---|
53 | | -static void sysmon_send_event(struct qcom_sysmon *sysmon, const char *name) |
---|
| 80 | +static void sysmon_send_event(struct qcom_sysmon *sysmon, |
---|
| 81 | + const struct sysmon_event *event) |
---|
54 | 82 | { |
---|
55 | 83 | char req[50]; |
---|
56 | 84 | int len; |
---|
57 | 85 | int ret; |
---|
58 | 86 | |
---|
59 | | - len = snprintf(req, sizeof(req), "ssr:%s:before_shutdown", name); |
---|
| 87 | + len = snprintf(req, sizeof(req), "ssr:%s:%s", event->subsys_name, |
---|
| 88 | + sysmon_state_string[event->ssr_event]); |
---|
60 | 89 | if (len >= sizeof(req)) |
---|
61 | 90 | return; |
---|
62 | 91 | |
---|
.. | .. |
---|
137 | 166 | } |
---|
138 | 167 | |
---|
139 | 168 | #define SSCTL_SHUTDOWN_REQ 0x21 |
---|
| 169 | +#define SSCTL_SHUTDOWN_READY_IND 0x21 |
---|
140 | 170 | #define SSCTL_SUBSYS_EVENT_REQ 0x23 |
---|
141 | 171 | |
---|
142 | 172 | #define SSCTL_MAX_MSG_LEN 7 |
---|
143 | 173 | |
---|
144 | 174 | #define SSCTL_SUBSYS_NAME_LENGTH 15 |
---|
145 | | - |
---|
146 | | -enum { |
---|
147 | | - SSCTL_SSR_EVENT_BEFORE_POWERUP, |
---|
148 | | - SSCTL_SSR_EVENT_AFTER_POWERUP, |
---|
149 | | - SSCTL_SSR_EVENT_BEFORE_SHUTDOWN, |
---|
150 | | - SSCTL_SSR_EVENT_AFTER_SHUTDOWN, |
---|
151 | | -}; |
---|
152 | 175 | |
---|
153 | 176 | enum { |
---|
154 | 177 | SSCTL_SSR_EVENT_FORCED, |
---|
.. | .. |
---|
252 | 275 | {} |
---|
253 | 276 | }; |
---|
254 | 277 | |
---|
| 278 | +static struct qmi_elem_info ssctl_shutdown_ind_ei[] = { |
---|
| 279 | + {} |
---|
| 280 | +}; |
---|
| 281 | + |
---|
| 282 | +static void sysmon_ind_cb(struct qmi_handle *qmi, struct sockaddr_qrtr *sq, |
---|
| 283 | + struct qmi_txn *txn, const void *data) |
---|
| 284 | +{ |
---|
| 285 | + struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi); |
---|
| 286 | + |
---|
| 287 | + complete(&sysmon->ind_comp); |
---|
| 288 | +} |
---|
| 289 | + |
---|
| 290 | +static struct qmi_msg_handler qmi_indication_handler[] = { |
---|
| 291 | + { |
---|
| 292 | + .type = QMI_INDICATION, |
---|
| 293 | + .msg_id = SSCTL_SHUTDOWN_READY_IND, |
---|
| 294 | + .ei = ssctl_shutdown_ind_ei, |
---|
| 295 | + .decoded_size = 0, |
---|
| 296 | + .fn = sysmon_ind_cb |
---|
| 297 | + }, |
---|
| 298 | + {} |
---|
| 299 | +}; |
---|
| 300 | + |
---|
255 | 301 | /** |
---|
256 | 302 | * ssctl_request_shutdown() - request shutdown via SSCTL QMI service |
---|
257 | 303 | * @sysmon: sysmon context |
---|
.. | .. |
---|
262 | 308 | struct qmi_txn txn; |
---|
263 | 309 | int ret; |
---|
264 | 310 | |
---|
| 311 | + reinit_completion(&sysmon->ind_comp); |
---|
| 312 | + reinit_completion(&sysmon->shutdown_comp); |
---|
265 | 313 | ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_shutdown_resp_ei, &resp); |
---|
266 | 314 | if (ret < 0) { |
---|
267 | 315 | dev_err(sysmon->dev, "failed to allocate QMI txn\n"); |
---|
.. | .. |
---|
283 | 331 | dev_err(sysmon->dev, "shutdown request failed\n"); |
---|
284 | 332 | else |
---|
285 | 333 | dev_dbg(sysmon->dev, "shutdown request completed\n"); |
---|
| 334 | + |
---|
| 335 | + if (sysmon->shutdown_irq > 0) { |
---|
| 336 | + ret = wait_for_completion_timeout(&sysmon->shutdown_comp, |
---|
| 337 | + 10 * HZ); |
---|
| 338 | + if (!ret) { |
---|
| 339 | + ret = try_wait_for_completion(&sysmon->ind_comp); |
---|
| 340 | + if (!ret) |
---|
| 341 | + dev_err(sysmon->dev, |
---|
| 342 | + "timeout waiting for shutdown ack\n"); |
---|
| 343 | + } |
---|
| 344 | + } |
---|
286 | 345 | } |
---|
287 | 346 | |
---|
288 | 347 | /** |
---|
289 | 348 | * ssctl_send_event() - send notification of other remote's SSR event |
---|
290 | 349 | * @sysmon: sysmon context |
---|
291 | | - * @name: other remote's name |
---|
| 350 | + * @event: sysmon event context |
---|
292 | 351 | */ |
---|
293 | | -static void ssctl_send_event(struct qcom_sysmon *sysmon, const char *name) |
---|
| 352 | +static void ssctl_send_event(struct qcom_sysmon *sysmon, |
---|
| 353 | + const struct sysmon_event *event) |
---|
294 | 354 | { |
---|
295 | 355 | struct ssctl_subsys_event_resp resp; |
---|
296 | 356 | struct ssctl_subsys_event_req req; |
---|
.. | .. |
---|
305 | 365 | } |
---|
306 | 366 | |
---|
307 | 367 | memset(&req, 0, sizeof(req)); |
---|
308 | | - strlcpy(req.subsys_name, name, sizeof(req.subsys_name)); |
---|
| 368 | + strlcpy(req.subsys_name, event->subsys_name, sizeof(req.subsys_name)); |
---|
309 | 369 | req.subsys_name_len = strlen(req.subsys_name); |
---|
310 | | - req.event = SSCTL_SSR_EVENT_BEFORE_SHUTDOWN; |
---|
| 370 | + req.event = event->ssr_event; |
---|
311 | 371 | req.evt_driven_valid = true; |
---|
312 | 372 | req.evt_driven = SSCTL_SSR_EVENT_FORCED; |
---|
313 | 373 | |
---|
.. | .. |
---|
353 | 413 | break; |
---|
354 | 414 | default: |
---|
355 | 415 | return -EINVAL; |
---|
356 | | - }; |
---|
| 416 | + } |
---|
357 | 417 | |
---|
358 | 418 | sysmon->ssctl_version = svc->version; |
---|
359 | 419 | |
---|
.. | .. |
---|
362 | 422 | sysmon->ssctl.sq_port = svc->port; |
---|
363 | 423 | |
---|
364 | 424 | svc->priv = sysmon; |
---|
| 425 | + |
---|
| 426 | + complete(&sysmon->ssctl_comp); |
---|
365 | 427 | |
---|
366 | 428 | return 0; |
---|
367 | 429 | } |
---|
.. | .. |
---|
383 | 445 | .del_server = ssctl_del_server, |
---|
384 | 446 | }; |
---|
385 | 447 | |
---|
| 448 | +static int sysmon_prepare(struct rproc_subdev *subdev) |
---|
| 449 | +{ |
---|
| 450 | + struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon, |
---|
| 451 | + subdev); |
---|
| 452 | + struct sysmon_event event = { |
---|
| 453 | + .subsys_name = sysmon->name, |
---|
| 454 | + .ssr_event = SSCTL_SSR_EVENT_BEFORE_POWERUP |
---|
| 455 | + }; |
---|
| 456 | + |
---|
| 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); |
---|
| 461 | + |
---|
| 462 | + return 0; |
---|
| 463 | +} |
---|
| 464 | + |
---|
| 465 | +/** |
---|
| 466 | + * sysmon_start() - start callback for the sysmon remoteproc subdevice |
---|
| 467 | + * @subdev: instance of the sysmon subdevice |
---|
| 468 | + * |
---|
| 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. |
---|
| 473 | + */ |
---|
386 | 474 | static int sysmon_start(struct rproc_subdev *subdev) |
---|
387 | 475 | { |
---|
| 476 | + struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon, |
---|
| 477 | + subdev); |
---|
| 478 | + struct qcom_sysmon *target; |
---|
| 479 | + struct sysmon_event event = { |
---|
| 480 | + .subsys_name = sysmon->name, |
---|
| 481 | + .ssr_event = SSCTL_SSR_EVENT_AFTER_POWERUP |
---|
| 482 | + }; |
---|
| 483 | + |
---|
| 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); |
---|
| 489 | + |
---|
| 490 | + mutex_lock(&sysmon_lock); |
---|
| 491 | + list_for_each_entry(target, &sysmon_list, node) { |
---|
| 492 | + if (target == sysmon) |
---|
| 493 | + continue; |
---|
| 494 | + |
---|
| 495 | + mutex_lock(&target->state_lock); |
---|
| 496 | + event.subsys_name = target->name; |
---|
| 497 | + event.ssr_event = target->state; |
---|
| 498 | + |
---|
| 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); |
---|
| 504 | + } |
---|
| 505 | + mutex_unlock(&sysmon_lock); |
---|
| 506 | + |
---|
388 | 507 | return 0; |
---|
389 | 508 | } |
---|
390 | 509 | |
---|
391 | 510 | static void sysmon_stop(struct rproc_subdev *subdev, bool crashed) |
---|
392 | 511 | { |
---|
393 | 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 |
---|
| 516 | + }; |
---|
394 | 517 | |
---|
395 | | - blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)sysmon->name); |
---|
| 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); |
---|
396 | 522 | |
---|
397 | 523 | /* Don't request graceful shutdown if we've crashed */ |
---|
398 | 524 | if (crashed) |
---|
399 | 525 | return; |
---|
400 | 526 | |
---|
| 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"); |
---|
| 530 | + } |
---|
| 531 | + |
---|
401 | 532 | if (sysmon->ssctl_version) |
---|
402 | 533 | ssctl_request_shutdown(sysmon); |
---|
403 | 534 | else if (sysmon->ept) |
---|
404 | 535 | sysmon_request_shutdown(sysmon); |
---|
| 536 | +} |
---|
| 537 | + |
---|
| 538 | +static void sysmon_unprepare(struct rproc_subdev *subdev) |
---|
| 539 | +{ |
---|
| 540 | + struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon, |
---|
| 541 | + subdev); |
---|
| 542 | + struct sysmon_event event = { |
---|
| 543 | + .subsys_name = sysmon->name, |
---|
| 544 | + .ssr_event = SSCTL_SSR_EVENT_AFTER_SHUTDOWN |
---|
| 545 | + }; |
---|
| 546 | + |
---|
| 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); |
---|
405 | 551 | } |
---|
406 | 552 | |
---|
407 | 553 | /** |
---|
.. | .. |
---|
414 | 560 | void *data) |
---|
415 | 561 | { |
---|
416 | 562 | struct qcom_sysmon *sysmon = container_of(nb, struct qcom_sysmon, nb); |
---|
417 | | - struct rproc *rproc = sysmon->rproc; |
---|
418 | | - const char *ssr_name = data; |
---|
| 563 | + struct sysmon_event *sysmon_event = data; |
---|
419 | 564 | |
---|
420 | 565 | /* Skip non-running rprocs and the originating instance */ |
---|
421 | | - if (rproc->state != RPROC_RUNNING || !strcmp(data, sysmon->name)) { |
---|
| 566 | + if (sysmon->state != SSCTL_SSR_EVENT_AFTER_POWERUP || |
---|
| 567 | + !strcmp(sysmon_event->subsys_name, sysmon->name)) { |
---|
422 | 568 | dev_dbg(sysmon->dev, "not notifying %s\n", sysmon->name); |
---|
423 | 569 | return NOTIFY_DONE; |
---|
424 | 570 | } |
---|
425 | 571 | |
---|
426 | 572 | /* Only SSCTL version 2 supports SSR events */ |
---|
427 | 573 | if (sysmon->ssctl_version == 2) |
---|
428 | | - ssctl_send_event(sysmon, ssr_name); |
---|
| 574 | + ssctl_send_event(sysmon, sysmon_event); |
---|
429 | 575 | else if (sysmon->ept) |
---|
430 | | - sysmon_send_event(sysmon, ssr_name); |
---|
| 576 | + sysmon_send_event(sysmon, sysmon_event); |
---|
431 | 577 | |
---|
432 | 578 | return NOTIFY_DONE; |
---|
| 579 | +} |
---|
| 580 | + |
---|
| 581 | +static irqreturn_t sysmon_shutdown_interrupt(int irq, void *data) |
---|
| 582 | +{ |
---|
| 583 | + struct qcom_sysmon *sysmon = data; |
---|
| 584 | + |
---|
| 585 | + complete(&sysmon->shutdown_comp); |
---|
| 586 | + |
---|
| 587 | + return IRQ_HANDLED; |
---|
433 | 588 | } |
---|
434 | 589 | |
---|
435 | 590 | /** |
---|
.. | .. |
---|
449 | 604 | |
---|
450 | 605 | sysmon = kzalloc(sizeof(*sysmon), GFP_KERNEL); |
---|
451 | 606 | if (!sysmon) |
---|
452 | | - return NULL; |
---|
| 607 | + return ERR_PTR(-ENOMEM); |
---|
453 | 608 | |
---|
454 | 609 | sysmon->dev = rproc->dev.parent; |
---|
455 | 610 | sysmon->rproc = rproc; |
---|
.. | .. |
---|
458 | 613 | sysmon->ssctl_instance = ssctl_instance; |
---|
459 | 614 | |
---|
460 | 615 | init_completion(&sysmon->comp); |
---|
| 616 | + init_completion(&sysmon->ind_comp); |
---|
| 617 | + init_completion(&sysmon->shutdown_comp); |
---|
| 618 | + init_completion(&sysmon->ssctl_comp); |
---|
461 | 619 | mutex_init(&sysmon->lock); |
---|
| 620 | + mutex_init(&sysmon->state_lock); |
---|
462 | 621 | |
---|
463 | | - ret = qmi_handle_init(&sysmon->qmi, SSCTL_MAX_MSG_LEN, &ssctl_ops, NULL); |
---|
| 622 | + sysmon->shutdown_irq = of_irq_get_byname(sysmon->dev->of_node, |
---|
| 623 | + "shutdown-ack"); |
---|
| 624 | + if (sysmon->shutdown_irq < 0) { |
---|
| 625 | + if (sysmon->shutdown_irq != -ENODATA) { |
---|
| 626 | + dev_err(sysmon->dev, |
---|
| 627 | + "failed to retrieve shutdown-ack IRQ\n"); |
---|
| 628 | + ret = sysmon->shutdown_irq; |
---|
| 629 | + kfree(sysmon); |
---|
| 630 | + return ERR_PTR(ret); |
---|
| 631 | + } |
---|
| 632 | + } else { |
---|
| 633 | + ret = devm_request_threaded_irq(sysmon->dev, |
---|
| 634 | + sysmon->shutdown_irq, |
---|
| 635 | + NULL, sysmon_shutdown_interrupt, |
---|
| 636 | + IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
---|
| 637 | + "q6v5 shutdown-ack", sysmon); |
---|
| 638 | + if (ret) { |
---|
| 639 | + dev_err(sysmon->dev, |
---|
| 640 | + "failed to acquire shutdown-ack IRQ\n"); |
---|
| 641 | + kfree(sysmon); |
---|
| 642 | + return ERR_PTR(ret); |
---|
| 643 | + } |
---|
| 644 | + } |
---|
| 645 | + |
---|
| 646 | + ret = qmi_handle_init(&sysmon->qmi, SSCTL_MAX_MSG_LEN, &ssctl_ops, |
---|
| 647 | + qmi_indication_handler); |
---|
464 | 648 | if (ret < 0) { |
---|
465 | 649 | dev_err(sysmon->dev, "failed to initialize qmi handle\n"); |
---|
466 | 650 | kfree(sysmon); |
---|
467 | | - return NULL; |
---|
| 651 | + return ERR_PTR(ret); |
---|
468 | 652 | } |
---|
469 | 653 | |
---|
470 | 654 | qmi_add_lookup(&sysmon->qmi, 43, 0, 0); |
---|
471 | 655 | |
---|
| 656 | + sysmon->subdev.prepare = sysmon_prepare; |
---|
472 | 657 | sysmon->subdev.start = sysmon_start; |
---|
473 | 658 | sysmon->subdev.stop = sysmon_stop; |
---|
| 659 | + sysmon->subdev.unprepare = sysmon_unprepare; |
---|
474 | 660 | |
---|
475 | 661 | rproc_add_subdev(rproc, &sysmon->subdev); |
---|
476 | 662 | |
---|