blob: 13660506b9b01ba88ee8d3cae6b54cb20a9e4eab [file] [log] [blame]
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2017, Linaro Ltd.
4 */
5#include <linux/firmware.h>
6#include <linux/module.h>
7#include <linux/notifier.h>
8#include <linux/slab.h>
Sibi Sankar027045a2019-01-08 15:53:43 +05309#include <linux/interrupt.h>
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -070010#include <linux/io.h>
Sibi Sankar027045a2019-01-08 15:53:43 +053011#include <linux/of_irq.h>
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -070012#include <linux/of_platform.h>
13#include <linux/platform_device.h>
14#include <linux/remoteproc/qcom_rproc.h>
15#include <linux/rpmsg.h>
16
17#include "qcom_common.h"
18
19static BLOCKING_NOTIFIER_HEAD(sysmon_notifiers);
20
21struct qcom_sysmon {
22 struct rproc_subdev subdev;
23 struct rproc *rproc;
24
25 struct list_head node;
26
27 const char *name;
28
Sibi Sankar027045a2019-01-08 15:53:43 +053029 int shutdown_irq;
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -070030 int ssctl_version;
31 int ssctl_instance;
32
33 struct notifier_block nb;
34
35 struct device *dev;
36
37 struct rpmsg_endpoint *ept;
38 struct completion comp;
Sibi Sankar74f27202019-01-08 15:53:44 +053039 struct completion ind_comp;
Sibi Sankar027045a2019-01-08 15:53:43 +053040 struct completion shutdown_comp;
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -070041 struct mutex lock;
42
43 bool ssr_ack;
44
45 struct qmi_handle qmi;
46 struct sockaddr_qrtr ssctl;
47};
48
Siddharth Gupta66a43472020-04-08 16:36:38 -070049enum {
50 SSCTL_SSR_EVENT_BEFORE_POWERUP,
51 SSCTL_SSR_EVENT_AFTER_POWERUP,
52 SSCTL_SSR_EVENT_BEFORE_SHUTDOWN,
53 SSCTL_SSR_EVENT_AFTER_SHUTDOWN,
54};
55
56static 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",
61};
62
63struct sysmon_event {
64 const char *subsys_name;
65 u32 ssr_event;
66};
67
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -070068static DEFINE_MUTEX(sysmon_lock);
69static LIST_HEAD(sysmon_list);
70
71/**
72 * sysmon_send_event() - send notification of other remote's SSR event
73 * @sysmon: sysmon context
74 * @name: other remote's name
75 */
Siddharth Gupta66a43472020-04-08 16:36:38 -070076static void sysmon_send_event(struct qcom_sysmon *sysmon,
77 const struct sysmon_event *event)
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -070078{
79 char req[50];
80 int len;
81 int ret;
82
Siddharth Gupta66a43472020-04-08 16:36:38 -070083 len = snprintf(req, sizeof(req), "ssr:%s:%s", event->subsys_name,
84 sysmon_state_string[event->ssr_event]);
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -070085 if (len >= sizeof(req))
86 return;
87
88 mutex_lock(&sysmon->lock);
89 reinit_completion(&sysmon->comp);
90 sysmon->ssr_ack = false;
91
92 ret = rpmsg_send(sysmon->ept, req, len);
93 if (ret < 0) {
94 dev_err(sysmon->dev, "failed to send sysmon event\n");
95 goto out_unlock;
96 }
97
98 ret = wait_for_completion_timeout(&sysmon->comp,
99 msecs_to_jiffies(5000));
100 if (!ret) {
101 dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
102 goto out_unlock;
103 }
104
105 if (!sysmon->ssr_ack)
106 dev_err(sysmon->dev, "unexpected response to sysmon event\n");
107
108out_unlock:
109 mutex_unlock(&sysmon->lock);
110}
111
112/**
113 * sysmon_request_shutdown() - request graceful shutdown of remote
114 * @sysmon: sysmon context
115 */
116static void sysmon_request_shutdown(struct qcom_sysmon *sysmon)
117{
118 char *req = "ssr:shutdown";
119 int ret;
120
121 mutex_lock(&sysmon->lock);
122 reinit_completion(&sysmon->comp);
123 sysmon->ssr_ack = false;
124
125 ret = rpmsg_send(sysmon->ept, req, strlen(req) + 1);
126 if (ret < 0) {
127 dev_err(sysmon->dev, "send sysmon shutdown request failed\n");
128 goto out_unlock;
129 }
130
131 ret = wait_for_completion_timeout(&sysmon->comp,
132 msecs_to_jiffies(5000));
133 if (!ret) {
134 dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
135 goto out_unlock;
136 }
137
138 if (!sysmon->ssr_ack)
139 dev_err(sysmon->dev,
140 "unexpected response to sysmon shutdown request\n");
141
142out_unlock:
143 mutex_unlock(&sysmon->lock);
144}
145
146static int sysmon_callback(struct rpmsg_device *rpdev, void *data, int count,
147 void *priv, u32 addr)
148{
149 struct qcom_sysmon *sysmon = priv;
150 const char *ssr_ack = "ssr:ack";
151 const int ssr_ack_len = strlen(ssr_ack) + 1;
152
153 if (!sysmon)
154 return -EINVAL;
155
156 if (count >= ssr_ack_len && !memcmp(data, ssr_ack, ssr_ack_len))
157 sysmon->ssr_ack = true;
158
159 complete(&sysmon->comp);
160
161 return 0;
162}
163
164#define SSCTL_SHUTDOWN_REQ 0x21
Sibi Sankar74f27202019-01-08 15:53:44 +0530165#define SSCTL_SHUTDOWN_READY_IND 0x21
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700166#define SSCTL_SUBSYS_EVENT_REQ 0x23
167
168#define SSCTL_MAX_MSG_LEN 7
169
170#define SSCTL_SUBSYS_NAME_LENGTH 15
171
172enum {
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700173 SSCTL_SSR_EVENT_FORCED,
174 SSCTL_SSR_EVENT_GRACEFUL,
175};
176
177struct ssctl_shutdown_resp {
178 struct qmi_response_type_v01 resp;
179};
180
181static struct qmi_elem_info ssctl_shutdown_resp_ei[] = {
182 {
183 .data_type = QMI_STRUCT,
184 .elem_len = 1,
185 .elem_size = sizeof(struct qmi_response_type_v01),
186 .array_type = NO_ARRAY,
187 .tlv_type = 0x02,
188 .offset = offsetof(struct ssctl_shutdown_resp, resp),
189 .ei_array = qmi_response_type_v01_ei,
190 },
191 {}
192};
193
194struct ssctl_subsys_event_req {
195 u8 subsys_name_len;
196 char subsys_name[SSCTL_SUBSYS_NAME_LENGTH];
197 u32 event;
198 u8 evt_driven_valid;
199 u32 evt_driven;
200};
201
202static struct qmi_elem_info ssctl_subsys_event_req_ei[] = {
203 {
204 .data_type = QMI_DATA_LEN,
205 .elem_len = 1,
206 .elem_size = sizeof(uint8_t),
207 .array_type = NO_ARRAY,
208 .tlv_type = 0x01,
209 .offset = offsetof(struct ssctl_subsys_event_req,
210 subsys_name_len),
211 .ei_array = NULL,
212 },
213 {
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,
218 .tlv_type = 0x01,
219 .offset = offsetof(struct ssctl_subsys_event_req,
220 subsys_name),
221 .ei_array = NULL,
222 },
223 {
224 .data_type = QMI_SIGNED_4_BYTE_ENUM,
225 .elem_len = 1,
226 .elem_size = sizeof(uint32_t),
227 .array_type = NO_ARRAY,
228 .tlv_type = 0x02,
229 .offset = offsetof(struct ssctl_subsys_event_req,
230 event),
231 .ei_array = NULL,
232 },
233 {
234 .data_type = QMI_OPT_FLAG,
235 .elem_len = 1,
236 .elem_size = sizeof(uint8_t),
237 .array_type = NO_ARRAY,
238 .tlv_type = 0x10,
239 .offset = offsetof(struct ssctl_subsys_event_req,
240 evt_driven_valid),
241 .ei_array = NULL,
242 },
243 {
244 .data_type = QMI_SIGNED_4_BYTE_ENUM,
245 .elem_len = 1,
246 .elem_size = sizeof(uint32_t),
247 .array_type = NO_ARRAY,
248 .tlv_type = 0x10,
249 .offset = offsetof(struct ssctl_subsys_event_req,
250 evt_driven),
251 .ei_array = NULL,
252 },
253 {}
254};
255
256struct ssctl_subsys_event_resp {
257 struct qmi_response_type_v01 resp;
258};
259
260static struct qmi_elem_info ssctl_subsys_event_resp_ei[] = {
261 {
262 .data_type = QMI_STRUCT,
263 .elem_len = 1,
264 .elem_size = sizeof(struct qmi_response_type_v01),
265 .array_type = NO_ARRAY,
266 .tlv_type = 0x02,
267 .offset = offsetof(struct ssctl_subsys_event_resp,
268 resp),
269 .ei_array = qmi_response_type_v01_ei,
270 },
271 {}
272};
273
Sibi Sankar74f27202019-01-08 15:53:44 +0530274static struct qmi_elem_info ssctl_shutdown_ind_ei[] = {
275 {}
276};
277
278static void sysmon_ind_cb(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
279 struct qmi_txn *txn, const void *data)
280{
281 struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
282
283 complete(&sysmon->ind_comp);
284}
285
286static struct qmi_msg_handler qmi_indication_handler[] = {
287 {
288 .type = QMI_INDICATION,
289 .msg_id = SSCTL_SHUTDOWN_READY_IND,
290 .ei = ssctl_shutdown_ind_ei,
291 .decoded_size = 0,
292 .fn = sysmon_ind_cb
293 },
294 {}
295};
296
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700297/**
298 * ssctl_request_shutdown() - request shutdown via SSCTL QMI service
299 * @sysmon: sysmon context
300 */
301static void ssctl_request_shutdown(struct qcom_sysmon *sysmon)
302{
303 struct ssctl_shutdown_resp resp;
304 struct qmi_txn txn;
305 int ret;
306
Sibi Sankar74f27202019-01-08 15:53:44 +0530307 reinit_completion(&sysmon->ind_comp);
308 reinit_completion(&sysmon->shutdown_comp);
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700309 ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_shutdown_resp_ei, &resp);
310 if (ret < 0) {
311 dev_err(sysmon->dev, "failed to allocate QMI txn\n");
312 return;
313 }
314
315 ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
316 SSCTL_SHUTDOWN_REQ, 0, NULL, NULL);
317 if (ret < 0) {
318 dev_err(sysmon->dev, "failed to send shutdown request\n");
319 qmi_txn_cancel(&txn);
320 return;
321 }
322
323 ret = qmi_txn_wait(&txn, 5 * HZ);
324 if (ret < 0)
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");
328 else
329 dev_dbg(sysmon->dev, "shutdown request completed\n");
Sibi Sankar74f27202019-01-08 15:53:44 +0530330
331 if (sysmon->shutdown_irq > 0) {
332 ret = wait_for_completion_timeout(&sysmon->shutdown_comp,
333 10 * HZ);
334 if (!ret) {
335 ret = try_wait_for_completion(&sysmon->ind_comp);
336 if (!ret)
337 dev_err(sysmon->dev,
338 "timeout waiting for shutdown ack\n");
339 }
340 }
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700341}
342
343/**
344 * ssctl_send_event() - send notification of other remote's SSR event
345 * @sysmon: sysmon context
346 * @name: other remote's name
347 */
Siddharth Gupta66a43472020-04-08 16:36:38 -0700348static void ssctl_send_event(struct qcom_sysmon *sysmon,
349 const struct sysmon_event *event)
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700350{
351 struct ssctl_subsys_event_resp resp;
352 struct ssctl_subsys_event_req req;
353 struct qmi_txn txn;
354 int ret;
355
356 memset(&resp, 0, sizeof(resp));
357 ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_subsys_event_resp_ei, &resp);
358 if (ret < 0) {
359 dev_err(sysmon->dev, "failed to allocate QMI txn\n");
360 return;
361 }
362
363 memset(&req, 0, sizeof(req));
Siddharth Gupta66a43472020-04-08 16:36:38 -0700364 strlcpy(req.subsys_name, event->subsys_name, sizeof(req.subsys_name));
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700365 req.subsys_name_len = strlen(req.subsys_name);
Siddharth Gupta66a43472020-04-08 16:36:38 -0700366 req.event = event->ssr_event;
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700367 req.evt_driven_valid = true;
368 req.evt_driven = SSCTL_SSR_EVENT_FORCED;
369
370 ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
371 SSCTL_SUBSYS_EVENT_REQ, 40,
372 ssctl_subsys_event_req_ei, &req);
373 if (ret < 0) {
374 dev_err(sysmon->dev, "failed to send shutdown request\n");
375 qmi_txn_cancel(&txn);
376 return;
377 }
378
379 ret = qmi_txn_wait(&txn, 5 * HZ);
380 if (ret < 0)
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");
384 else
385 dev_dbg(sysmon->dev, "ssr event send completed\n");
386}
387
388/**
389 * ssctl_new_server() - QMI callback indicating a new service
390 * @qmi: QMI handle
391 * @svc: service information
392 *
393 * Return: 0 if we're interested in this service, -EINVAL otherwise.
394 */
395static int ssctl_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
396{
397 struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
398
399 switch (svc->version) {
400 case 1:
401 if (svc->instance != 0)
402 return -EINVAL;
403 if (strcmp(sysmon->name, "modem"))
404 return -EINVAL;
405 break;
406 case 2:
407 if (svc->instance != sysmon->ssctl_instance)
408 return -EINVAL;
409 break;
410 default:
411 return -EINVAL;
Ma Feng40f6a662019-12-19 14:19:36 +0800412 }
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700413
414 sysmon->ssctl_version = svc->version;
415
416 sysmon->ssctl.sq_family = AF_QIPCRTR;
417 sysmon->ssctl.sq_node = svc->node;
418 sysmon->ssctl.sq_port = svc->port;
419
420 svc->priv = sysmon;
421
422 return 0;
423}
424
425/**
426 * ssctl_del_server() - QMI callback indicating that @svc is removed
427 * @qmi: QMI handle
428 * @svc: service information
429 */
430static void ssctl_del_server(struct qmi_handle *qmi, struct qmi_service *svc)
431{
432 struct qcom_sysmon *sysmon = svc->priv;
433
434 sysmon->ssctl_version = 0;
435}
436
437static const struct qmi_ops ssctl_ops = {
438 .new_server = ssctl_new_server,
439 .del_server = ssctl_del_server,
440};
441
442static int sysmon_start(struct rproc_subdev *subdev)
443{
444 return 0;
445}
446
447static void sysmon_stop(struct rproc_subdev *subdev, bool crashed)
448{
449 struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon, subdev);
Siddharth Gupta66a43472020-04-08 16:36:38 -0700450 struct sysmon_event event = {
451 .subsys_name = sysmon->name,
452 .ssr_event = SSCTL_SSR_EVENT_BEFORE_SHUTDOWN
453 };
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700454
Siddharth Gupta66a43472020-04-08 16:36:38 -0700455 blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700456
457 /* Don't request graceful shutdown if we've crashed */
458 if (crashed)
459 return;
460
461 if (sysmon->ssctl_version)
462 ssctl_request_shutdown(sysmon);
463 else if (sysmon->ept)
464 sysmon_request_shutdown(sysmon);
465}
466
467/**
468 * sysmon_notify() - notify sysmon target of another's SSR
469 * @nb: notifier_block associated with sysmon instance
470 * @event: unused
471 * @data: SSR identifier of the remote that is going down
472 */
473static int sysmon_notify(struct notifier_block *nb, unsigned long event,
474 void *data)
475{
476 struct qcom_sysmon *sysmon = container_of(nb, struct qcom_sysmon, nb);
477 struct rproc *rproc = sysmon->rproc;
Siddharth Gupta66a43472020-04-08 16:36:38 -0700478 struct sysmon_event *sysmon_event = data;
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700479
480 /* Skip non-running rprocs and the originating instance */
Siddharth Gupta66a43472020-04-08 16:36:38 -0700481 if (rproc->state != RPROC_RUNNING ||
482 !strcmp(sysmon_event->subsys_name, sysmon->name)) {
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700483 dev_dbg(sysmon->dev, "not notifying %s\n", sysmon->name);
484 return NOTIFY_DONE;
485 }
486
487 /* Only SSCTL version 2 supports SSR events */
488 if (sysmon->ssctl_version == 2)
Siddharth Gupta66a43472020-04-08 16:36:38 -0700489 ssctl_send_event(sysmon, sysmon_event);
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700490 else if (sysmon->ept)
Siddharth Gupta66a43472020-04-08 16:36:38 -0700491 sysmon_send_event(sysmon, sysmon_event);
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700492
493 return NOTIFY_DONE;
494}
495
Sibi Sankar027045a2019-01-08 15:53:43 +0530496static irqreturn_t sysmon_shutdown_interrupt(int irq, void *data)
497{
498 struct qcom_sysmon *sysmon = data;
499
500 complete(&sysmon->shutdown_comp);
501
502 return IRQ_HANDLED;
503}
504
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700505/**
506 * qcom_add_sysmon_subdev() - create a sysmon subdev for the given remoteproc
507 * @rproc: rproc context to associate the subdev with
508 * @name: name of this subdev, to use in SSR
509 * @ssctl_instance: instance id of the ssctl QMI service
510 *
511 * Return: A new qcom_sysmon object, or NULL on failure
512 */
513struct qcom_sysmon *qcom_add_sysmon_subdev(struct rproc *rproc,
514 const char *name,
515 int ssctl_instance)
516{
517 struct qcom_sysmon *sysmon;
518 int ret;
519
520 sysmon = kzalloc(sizeof(*sysmon), GFP_KERNEL);
521 if (!sysmon)
Sibi Sankar027045a2019-01-08 15:53:43 +0530522 return ERR_PTR(-ENOMEM);
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700523
524 sysmon->dev = rproc->dev.parent;
525 sysmon->rproc = rproc;
526
527 sysmon->name = name;
528 sysmon->ssctl_instance = ssctl_instance;
529
530 init_completion(&sysmon->comp);
Sibi Sankar74f27202019-01-08 15:53:44 +0530531 init_completion(&sysmon->ind_comp);
Sibi Sankar027045a2019-01-08 15:53:43 +0530532 init_completion(&sysmon->shutdown_comp);
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700533 mutex_init(&sysmon->lock);
534
Sibi Sankar027045a2019-01-08 15:53:43 +0530535 sysmon->shutdown_irq = of_irq_get_byname(sysmon->dev->of_node,
536 "shutdown-ack");
537 if (sysmon->shutdown_irq < 0) {
538 if (sysmon->shutdown_irq != -ENODATA) {
539 dev_err(sysmon->dev,
540 "failed to retrieve shutdown-ack IRQ\n");
541 return ERR_PTR(sysmon->shutdown_irq);
542 }
543 } else {
544 ret = devm_request_threaded_irq(sysmon->dev,
545 sysmon->shutdown_irq,
546 NULL, sysmon_shutdown_interrupt,
547 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
548 "q6v5 shutdown-ack", sysmon);
549 if (ret) {
550 dev_err(sysmon->dev,
551 "failed to acquire shutdown-ack IRQ\n");
552 return ERR_PTR(ret);
553 }
554 }
555
Sibi Sankar74f27202019-01-08 15:53:44 +0530556 ret = qmi_handle_init(&sysmon->qmi, SSCTL_MAX_MSG_LEN, &ssctl_ops,
557 qmi_indication_handler);
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700558 if (ret < 0) {
559 dev_err(sysmon->dev, "failed to initialize qmi handle\n");
560 kfree(sysmon);
Sibi Sankar027045a2019-01-08 15:53:43 +0530561 return ERR_PTR(ret);
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700562 }
563
564 qmi_add_lookup(&sysmon->qmi, 43, 0, 0);
565
Bjorn Andersson49026762018-06-26 07:11:57 -0500566 sysmon->subdev.start = sysmon_start;
567 sysmon->subdev.stop = sysmon_stop;
568
569 rproc_add_subdev(rproc, &sysmon->subdev);
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700570
571 sysmon->nb.notifier_call = sysmon_notify;
572 blocking_notifier_chain_register(&sysmon_notifiers, &sysmon->nb);
573
574 mutex_lock(&sysmon_lock);
575 list_add(&sysmon->node, &sysmon_list);
576 mutex_unlock(&sysmon_lock);
577
578 return sysmon;
579}
580EXPORT_SYMBOL_GPL(qcom_add_sysmon_subdev);
581
582/**
583 * qcom_remove_sysmon_subdev() - release a qcom_sysmon
584 * @sysmon: sysmon context, as retrieved by qcom_add_sysmon_subdev()
585 */
586void qcom_remove_sysmon_subdev(struct qcom_sysmon *sysmon)
587{
588 if (!sysmon)
589 return;
590
591 mutex_lock(&sysmon_lock);
592 list_del(&sysmon->node);
593 mutex_unlock(&sysmon_lock);
594
595 blocking_notifier_chain_unregister(&sysmon_notifiers, &sysmon->nb);
596
597 rproc_remove_subdev(sysmon->rproc, &sysmon->subdev);
598
599 qmi_handle_release(&sysmon->qmi);
600
601 kfree(sysmon);
602}
603EXPORT_SYMBOL_GPL(qcom_remove_sysmon_subdev);
604
605/**
606 * sysmon_probe() - probe sys_mon channel
607 * @rpdev: rpmsg device handle
608 *
609 * Find the sysmon context associated with the ancestor remoteproc and assign
610 * this rpmsg device with said sysmon context.
611 *
612 * Return: 0 on success, negative errno on failure.
613 */
614static int sysmon_probe(struct rpmsg_device *rpdev)
615{
616 struct qcom_sysmon *sysmon;
617 struct rproc *rproc;
618
619 rproc = rproc_get_by_child(&rpdev->dev);
620 if (!rproc) {
621 dev_err(&rpdev->dev, "sysmon device not child of rproc\n");
622 return -EINVAL;
623 }
624
625 mutex_lock(&sysmon_lock);
626 list_for_each_entry(sysmon, &sysmon_list, node) {
627 if (sysmon->rproc == rproc)
628 goto found;
629 }
630 mutex_unlock(&sysmon_lock);
631
632 dev_err(&rpdev->dev, "no sysmon associated with parent rproc\n");
633
634 return -EINVAL;
635
636found:
637 mutex_unlock(&sysmon_lock);
638
639 rpdev->ept->priv = sysmon;
640 sysmon->ept = rpdev->ept;
641
642 return 0;
643}
644
645/**
646 * sysmon_remove() - sys_mon channel remove handler
647 * @rpdev: rpmsg device handle
648 *
649 * Disassociate the rpmsg device with the sysmon instance.
650 */
651static void sysmon_remove(struct rpmsg_device *rpdev)
652{
653 struct qcom_sysmon *sysmon = rpdev->ept->priv;
654
655 sysmon->ept = NULL;
656}
657
658static const struct rpmsg_device_id sysmon_match[] = {
659 { "sys_mon" },
660 {}
661};
662
663static struct rpmsg_driver sysmon_driver = {
664 .probe = sysmon_probe,
665 .remove = sysmon_remove,
666 .callback = sysmon_callback,
667 .id_table = sysmon_match,
668 .drv = {
669 .name = "qcom_sysmon",
670 },
671};
672
673module_rpmsg_driver(sysmon_driver);
674
675MODULE_DESCRIPTION("Qualcomm sysmon driver");
676MODULE_LICENSE("GPL v2");