Linux Audio

Check our new training course

Buildroot integration, development and maintenance

Need a Buildroot system for your embedded project?
Loading...
v6.8
  1// 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>
  9#include <linux/interrupt.h>
 10#include <linux/io.h>
 11#include <linux/of_irq.h>
 
 12#include <linux/platform_device.h>
 13#include <linux/remoteproc/qcom_rproc.h>
 14#include <linux/rpmsg.h>
 15
 16#include "qcom_common.h"
 17
 18static BLOCKING_NOTIFIER_HEAD(sysmon_notifiers);
 19
 20struct qcom_sysmon {
 21	struct rproc_subdev subdev;
 22	struct rproc *rproc;
 23
 24	int state;
 25	struct mutex state_lock;
 26
 27	struct list_head node;
 28
 29	const char *name;
 30
 31	int shutdown_irq;
 32	int ssctl_version;
 33	int ssctl_instance;
 34
 35	struct notifier_block nb;
 36
 37	struct device *dev;
 38
 39	struct rpmsg_endpoint *ept;
 40	struct completion comp;
 41	struct completion ind_comp;
 42	struct completion shutdown_comp;
 43	struct completion ssctl_comp;
 44	struct mutex lock;
 45
 46	bool ssr_ack;
 47	bool shutdown_acked;
 48
 49	struct qmi_handle qmi;
 50	struct sockaddr_qrtr ssctl;
 51};
 52
 53enum {
 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
 60static 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
 67struct sysmon_event {
 68	const char *subsys_name;
 69	u32 ssr_event;
 70};
 71
 72static DEFINE_MUTEX(sysmon_lock);
 73static LIST_HEAD(sysmon_list);
 74
 75/**
 76 * sysmon_send_event() - send notification of other remote's SSR event
 77 * @sysmon:	sysmon context
 78 * @event:	sysmon event context
 79 */
 80static void sysmon_send_event(struct qcom_sysmon *sysmon,
 81			      const struct sysmon_event *event)
 82{
 83	char req[50];
 84	int len;
 85	int ret;
 86
 87	len = snprintf(req, sizeof(req), "ssr:%s:%s", event->subsys_name,
 88		       sysmon_state_string[event->ssr_event]);
 89	if (len >= sizeof(req))
 90		return;
 91
 92	mutex_lock(&sysmon->lock);
 93	reinit_completion(&sysmon->comp);
 94	sysmon->ssr_ack = false;
 95
 96	ret = rpmsg_send(sysmon->ept, req, len);
 97	if (ret < 0) {
 98		dev_err(sysmon->dev, "failed to send sysmon event\n");
 99		goto out_unlock;
100	}
101
102	ret = wait_for_completion_timeout(&sysmon->comp,
103					  msecs_to_jiffies(5000));
104	if (!ret) {
105		dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
106		goto out_unlock;
107	}
108
109	if (!sysmon->ssr_ack)
110		dev_err(sysmon->dev, "unexpected response to sysmon event\n");
111
112out_unlock:
113	mutex_unlock(&sysmon->lock);
114}
115
116/**
117 * sysmon_request_shutdown() - request graceful shutdown of remote
118 * @sysmon:	sysmon context
119 *
120 * Return: boolean indicator of the remote processor acking the request
121 */
122static bool sysmon_request_shutdown(struct qcom_sysmon *sysmon)
123{
124	char *req = "ssr:shutdown";
125	bool acked = false;
126	int ret;
127
128	mutex_lock(&sysmon->lock);
129	reinit_completion(&sysmon->comp);
130	sysmon->ssr_ack = false;
131
132	ret = rpmsg_send(sysmon->ept, req, strlen(req) + 1);
133	if (ret < 0) {
134		dev_err(sysmon->dev, "send sysmon shutdown request failed\n");
135		goto out_unlock;
136	}
137
138	ret = wait_for_completion_timeout(&sysmon->comp,
139					  msecs_to_jiffies(5000));
140	if (!ret) {
141		dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
142		goto out_unlock;
143	}
144
145	if (!sysmon->ssr_ack)
146		dev_err(sysmon->dev,
147			"unexpected response to sysmon shutdown request\n");
148	else
149		acked = true;
150
151out_unlock:
152	mutex_unlock(&sysmon->lock);
153
154	return acked;
155}
156
157static int sysmon_callback(struct rpmsg_device *rpdev, void *data, int count,
158			   void *priv, u32 addr)
159{
160	struct qcom_sysmon *sysmon = priv;
161	const char *ssr_ack = "ssr:ack";
162	const int ssr_ack_len = strlen(ssr_ack) + 1;
163
164	if (!sysmon)
165		return -EINVAL;
166
167	if (count >= ssr_ack_len && !memcmp(data, ssr_ack, ssr_ack_len))
168		sysmon->ssr_ack = true;
169
170	complete(&sysmon->comp);
171
172	return 0;
173}
174
175#define SSCTL_SHUTDOWN_REQ		0x21
176#define SSCTL_SHUTDOWN_READY_IND	0x21
177#define SSCTL_SUBSYS_EVENT_REQ		0x23
178
179#define SSCTL_MAX_MSG_LEN		7
180
181#define SSCTL_SUBSYS_NAME_LENGTH	15
182
183enum {
 
 
 
 
 
 
 
184	SSCTL_SSR_EVENT_FORCED,
185	SSCTL_SSR_EVENT_GRACEFUL,
186};
187
188struct ssctl_shutdown_resp {
189	struct qmi_response_type_v01 resp;
190};
191
192static const struct qmi_elem_info ssctl_shutdown_resp_ei[] = {
193	{
194		.data_type	= QMI_STRUCT,
195		.elem_len	= 1,
196		.elem_size	= sizeof(struct qmi_response_type_v01),
197		.array_type	= NO_ARRAY,
198		.tlv_type	= 0x02,
199		.offset		= offsetof(struct ssctl_shutdown_resp, resp),
200		.ei_array	= qmi_response_type_v01_ei,
201	},
202	{}
203};
204
205struct ssctl_subsys_event_req {
206	u8 subsys_name_len;
207	char subsys_name[SSCTL_SUBSYS_NAME_LENGTH];
208	u32 event;
209	u8 evt_driven_valid;
210	u32 evt_driven;
211};
212
213static const struct qmi_elem_info ssctl_subsys_event_req_ei[] = {
214	{
215		.data_type	= QMI_DATA_LEN,
216		.elem_len	= 1,
217		.elem_size	= sizeof(uint8_t),
218		.array_type	= NO_ARRAY,
219		.tlv_type	= 0x01,
220		.offset		= offsetof(struct ssctl_subsys_event_req,
221					   subsys_name_len),
222		.ei_array	= NULL,
223	},
224	{
225		.data_type	= QMI_UNSIGNED_1_BYTE,
226		.elem_len	= SSCTL_SUBSYS_NAME_LENGTH,
227		.elem_size	= sizeof(char),
228		.array_type	= VAR_LEN_ARRAY,
229		.tlv_type	= 0x01,
230		.offset		= offsetof(struct ssctl_subsys_event_req,
231					   subsys_name),
232		.ei_array	= NULL,
233	},
234	{
235		.data_type	= QMI_SIGNED_4_BYTE_ENUM,
236		.elem_len	= 1,
237		.elem_size	= sizeof(uint32_t),
238		.array_type	= NO_ARRAY,
239		.tlv_type	= 0x02,
240		.offset		= offsetof(struct ssctl_subsys_event_req,
241					   event),
242		.ei_array	= NULL,
243	},
244	{
245		.data_type	= QMI_OPT_FLAG,
246		.elem_len	= 1,
247		.elem_size	= sizeof(uint8_t),
248		.array_type	= NO_ARRAY,
249		.tlv_type	= 0x10,
250		.offset		= offsetof(struct ssctl_subsys_event_req,
251					   evt_driven_valid),
252		.ei_array	= NULL,
253	},
254	{
255		.data_type	= QMI_SIGNED_4_BYTE_ENUM,
256		.elem_len	= 1,
257		.elem_size	= sizeof(uint32_t),
258		.array_type	= NO_ARRAY,
259		.tlv_type	= 0x10,
260		.offset		= offsetof(struct ssctl_subsys_event_req,
261					   evt_driven),
262		.ei_array	= NULL,
263	},
264	{}
265};
266
267struct ssctl_subsys_event_resp {
268	struct qmi_response_type_v01 resp;
269};
270
271static const struct qmi_elem_info ssctl_subsys_event_resp_ei[] = {
272	{
273		.data_type	= QMI_STRUCT,
274		.elem_len	= 1,
275		.elem_size	= sizeof(struct qmi_response_type_v01),
276		.array_type	= NO_ARRAY,
277		.tlv_type	= 0x02,
278		.offset		= offsetof(struct ssctl_subsys_event_resp,
279					   resp),
280		.ei_array	= qmi_response_type_v01_ei,
281	},
282	{}
283};
284
285static const struct qmi_elem_info ssctl_shutdown_ind_ei[] = {
286	{}
287};
288
289static void sysmon_ind_cb(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
290			  struct qmi_txn *txn, const void *data)
291{
292	struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
293
294	complete(&sysmon->ind_comp);
295}
296
297static const struct qmi_msg_handler qmi_indication_handler[] = {
298	{
299		.type = QMI_INDICATION,
300		.msg_id = SSCTL_SHUTDOWN_READY_IND,
301		.ei = ssctl_shutdown_ind_ei,
302		.decoded_size = 0,
303		.fn = sysmon_ind_cb
304	},
305	{}
306};
307
308static bool ssctl_request_shutdown_wait(struct qcom_sysmon *sysmon)
309{
310	int ret;
311
312	ret = wait_for_completion_timeout(&sysmon->shutdown_comp, 10 * HZ);
313	if (ret)
314		return true;
315
316	ret = try_wait_for_completion(&sysmon->ind_comp);
317	if (ret)
318		return true;
319
320	dev_err(sysmon->dev, "timeout waiting for shutdown ack\n");
321	return false;
322}
323
324/**
325 * ssctl_request_shutdown() - request shutdown via SSCTL QMI service
326 * @sysmon:	sysmon context
327 *
328 * Return: boolean indicator of the remote processor acking the request
329 */
330static bool ssctl_request_shutdown(struct qcom_sysmon *sysmon)
331{
332	struct ssctl_shutdown_resp resp;
333	struct qmi_txn txn;
334	bool acked = false;
335	int ret;
336
337	reinit_completion(&sysmon->ind_comp);
338	reinit_completion(&sysmon->shutdown_comp);
339	ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_shutdown_resp_ei, &resp);
340	if (ret < 0) {
341		dev_err(sysmon->dev, "failed to allocate QMI txn\n");
342		return false;
343	}
344
345	ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
346			       SSCTL_SHUTDOWN_REQ, 0, NULL, NULL);
347	if (ret < 0) {
348		dev_err(sysmon->dev, "failed to send shutdown request\n");
349		qmi_txn_cancel(&txn);
350		return false;
351	}
352
353	ret = qmi_txn_wait(&txn, 5 * HZ);
354	if (ret < 0) {
355		dev_err(sysmon->dev, "timeout waiting for shutdown response\n");
356	} else if (resp.resp.result) {
357		dev_err(sysmon->dev, "shutdown request rejected\n");
358	} else {
359		dev_dbg(sysmon->dev, "shutdown request completed\n");
360		acked = true;
361	}
362
363	if (sysmon->shutdown_irq > 0)
364		return ssctl_request_shutdown_wait(sysmon);
365
366	return acked;
 
 
 
 
 
 
 
 
 
367}
368
369/**
370 * ssctl_send_event() - send notification of other remote's SSR event
371 * @sysmon:	sysmon context
372 * @event:	sysmon event context
373 */
374static void ssctl_send_event(struct qcom_sysmon *sysmon,
375			     const struct sysmon_event *event)
376{
377	struct ssctl_subsys_event_resp resp;
378	struct ssctl_subsys_event_req req;
379	struct qmi_txn txn;
380	int ret;
381
382	memset(&resp, 0, sizeof(resp));
383	ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_subsys_event_resp_ei, &resp);
384	if (ret < 0) {
385		dev_err(sysmon->dev, "failed to allocate QMI txn\n");
386		return;
387	}
388
389	memset(&req, 0, sizeof(req));
390	strscpy(req.subsys_name, event->subsys_name, sizeof(req.subsys_name));
391	req.subsys_name_len = strlen(req.subsys_name);
392	req.event = event->ssr_event;
393	req.evt_driven_valid = true;
394	req.evt_driven = SSCTL_SSR_EVENT_FORCED;
395
396	ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
397			       SSCTL_SUBSYS_EVENT_REQ, 40,
398			       ssctl_subsys_event_req_ei, &req);
399	if (ret < 0) {
400		dev_err(sysmon->dev, "failed to send subsystem event\n");
401		qmi_txn_cancel(&txn);
402		return;
403	}
404
405	ret = qmi_txn_wait(&txn, 5 * HZ);
406	if (ret < 0)
407		dev_err(sysmon->dev, "timeout waiting for subsystem event response\n");
408	else if (resp.resp.result)
409		dev_err(sysmon->dev, "subsystem event rejected\n");
410	else
411		dev_dbg(sysmon->dev, "subsystem event accepted\n");
412}
413
414/**
415 * ssctl_new_server() - QMI callback indicating a new service
416 * @qmi:	QMI handle
417 * @svc:	service information
418 *
419 * Return: 0 if we're interested in this service, -EINVAL otherwise.
420 */
421static int ssctl_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
422{
423	struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
424
425	switch (svc->version) {
426	case 1:
427		if (svc->instance != 0)
428			return -EINVAL;
429		if (strcmp(sysmon->name, "modem"))
430			return -EINVAL;
431		break;
432	case 2:
433		if (svc->instance != sysmon->ssctl_instance)
434			return -EINVAL;
435		break;
436	default:
437		return -EINVAL;
438	}
439
440	sysmon->ssctl_version = svc->version;
441
442	sysmon->ssctl.sq_family = AF_QIPCRTR;
443	sysmon->ssctl.sq_node = svc->node;
444	sysmon->ssctl.sq_port = svc->port;
445
446	svc->priv = sysmon;
447
448	complete(&sysmon->ssctl_comp);
449
450	return 0;
451}
452
453/**
454 * ssctl_del_server() - QMI callback indicating that @svc is removed
455 * @qmi:	QMI handle
456 * @svc:	service information
457 */
458static void ssctl_del_server(struct qmi_handle *qmi, struct qmi_service *svc)
459{
460	struct qcom_sysmon *sysmon = svc->priv;
461
462	sysmon->ssctl_version = 0;
463}
464
465static const struct qmi_ops ssctl_ops = {
466	.new_server = ssctl_new_server,
467	.del_server = ssctl_del_server,
468};
469
470static int sysmon_prepare(struct rproc_subdev *subdev)
471{
472	struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
473						  subdev);
474	struct sysmon_event event = {
475		.subsys_name = sysmon->name,
476		.ssr_event = SSCTL_SSR_EVENT_BEFORE_POWERUP
477	};
478
479	mutex_lock(&sysmon->state_lock);
480	sysmon->state = SSCTL_SSR_EVENT_BEFORE_POWERUP;
481	blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
482	mutex_unlock(&sysmon->state_lock);
483
484	return 0;
485}
486
487/**
488 * sysmon_start() - start callback for the sysmon remoteproc subdevice
489 * @subdev:	instance of the sysmon subdevice
490 *
491 * Inform all the listners of sysmon notifications that the rproc associated
492 * to @subdev has booted up. The rproc that booted up also needs to know
493 * which rprocs are already up and running, so send start notifications
494 * on behalf of all the online rprocs.
495 */
496static int sysmon_start(struct rproc_subdev *subdev)
497{
498	struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
499						  subdev);
500	struct qcom_sysmon *target;
501	struct sysmon_event event = {
502		.subsys_name = sysmon->name,
503		.ssr_event = SSCTL_SSR_EVENT_AFTER_POWERUP
504	};
505
506	reinit_completion(&sysmon->ssctl_comp);
507	mutex_lock(&sysmon->state_lock);
508	sysmon->state = SSCTL_SSR_EVENT_AFTER_POWERUP;
509	blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
510	mutex_unlock(&sysmon->state_lock);
511
512	mutex_lock(&sysmon_lock);
513	list_for_each_entry(target, &sysmon_list, node) {
514		mutex_lock(&target->state_lock);
515		if (target == sysmon || target->state != SSCTL_SSR_EVENT_AFTER_POWERUP) {
516			mutex_unlock(&target->state_lock);
517			continue;
518		}
519
520		event.subsys_name = target->name;
521		event.ssr_event = target->state;
522
523		if (sysmon->ssctl_version == 2)
524			ssctl_send_event(sysmon, &event);
525		else if (sysmon->ept)
526			sysmon_send_event(sysmon, &event);
527		mutex_unlock(&target->state_lock);
528	}
529	mutex_unlock(&sysmon_lock);
530
531	return 0;
532}
533
534static void sysmon_stop(struct rproc_subdev *subdev, bool crashed)
535{
536	struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon, subdev);
537	struct sysmon_event event = {
538		.subsys_name = sysmon->name,
539		.ssr_event = SSCTL_SSR_EVENT_BEFORE_SHUTDOWN
540	};
541
542	sysmon->shutdown_acked = false;
543
544	mutex_lock(&sysmon->state_lock);
545	sysmon->state = SSCTL_SSR_EVENT_BEFORE_SHUTDOWN;
546	blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
547	mutex_unlock(&sysmon->state_lock);
548
549	/* Don't request graceful shutdown if we've crashed */
550	if (crashed)
551		return;
552
553	if (sysmon->ssctl_instance) {
554		if (!wait_for_completion_timeout(&sysmon->ssctl_comp, HZ / 2))
555			dev_err(sysmon->dev, "timeout waiting for ssctl service\n");
556	}
557
558	if (sysmon->ssctl_version)
559		sysmon->shutdown_acked = ssctl_request_shutdown(sysmon);
560	else if (sysmon->ept)
561		sysmon->shutdown_acked = sysmon_request_shutdown(sysmon);
562}
563
564static void sysmon_unprepare(struct rproc_subdev *subdev)
565{
566	struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
567						  subdev);
568	struct sysmon_event event = {
569		.subsys_name = sysmon->name,
570		.ssr_event = SSCTL_SSR_EVENT_AFTER_SHUTDOWN
571	};
572
573	mutex_lock(&sysmon->state_lock);
574	sysmon->state = SSCTL_SSR_EVENT_AFTER_SHUTDOWN;
575	blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
576	mutex_unlock(&sysmon->state_lock);
577}
578
579/**
580 * sysmon_notify() - notify sysmon target of another's SSR
581 * @nb:		notifier_block associated with sysmon instance
582 * @event:	unused
583 * @data:	SSR identifier of the remote that is going down
584 */
585static int sysmon_notify(struct notifier_block *nb, unsigned long event,
586			 void *data)
587{
588	struct qcom_sysmon *sysmon = container_of(nb, struct qcom_sysmon, nb);
589	struct sysmon_event *sysmon_event = data;
 
590
591	/* Skip non-running rprocs and the originating instance */
592	if (sysmon->state != SSCTL_SSR_EVENT_AFTER_POWERUP ||
593	    !strcmp(sysmon_event->subsys_name, sysmon->name)) {
594		dev_dbg(sysmon->dev, "not notifying %s\n", sysmon->name);
595		return NOTIFY_DONE;
596	}
597
598	/* Only SSCTL version 2 supports SSR events */
599	if (sysmon->ssctl_version == 2)
600		ssctl_send_event(sysmon, sysmon_event);
601	else if (sysmon->ept)
602		sysmon_send_event(sysmon, sysmon_event);
603
604	return NOTIFY_DONE;
605}
606
607static irqreturn_t sysmon_shutdown_interrupt(int irq, void *data)
608{
609	struct qcom_sysmon *sysmon = data;
610
611	complete(&sysmon->shutdown_comp);
612
613	return IRQ_HANDLED;
614}
615
616/**
617 * qcom_add_sysmon_subdev() - create a sysmon subdev for the given remoteproc
618 * @rproc:	rproc context to associate the subdev with
619 * @name:	name of this subdev, to use in SSR
620 * @ssctl_instance: instance id of the ssctl QMI service
621 *
622 * Return: A new qcom_sysmon object, or NULL on failure
623 */
624struct qcom_sysmon *qcom_add_sysmon_subdev(struct rproc *rproc,
625					   const char *name,
626					   int ssctl_instance)
627{
628	struct qcom_sysmon *sysmon;
629	int ret;
630
631	sysmon = kzalloc(sizeof(*sysmon), GFP_KERNEL);
632	if (!sysmon)
633		return ERR_PTR(-ENOMEM);
634
635	sysmon->dev = rproc->dev.parent;
636	sysmon->rproc = rproc;
637
638	sysmon->name = name;
639	sysmon->ssctl_instance = ssctl_instance;
640
641	init_completion(&sysmon->comp);
642	init_completion(&sysmon->ind_comp);
643	init_completion(&sysmon->shutdown_comp);
644	init_completion(&sysmon->ssctl_comp);
645	mutex_init(&sysmon->lock);
646	mutex_init(&sysmon->state_lock);
647
648	sysmon->shutdown_irq = of_irq_get_byname(sysmon->dev->of_node,
649						 "shutdown-ack");
650	if (sysmon->shutdown_irq < 0) {
651		if (sysmon->shutdown_irq != -ENODATA) {
652			dev_err(sysmon->dev,
653				"failed to retrieve shutdown-ack IRQ\n");
654			ret = sysmon->shutdown_irq;
655			kfree(sysmon);
656			return ERR_PTR(ret);
657		}
658	} else {
659		ret = devm_request_threaded_irq(sysmon->dev,
660						sysmon->shutdown_irq,
661						NULL, sysmon_shutdown_interrupt,
662						IRQF_TRIGGER_RISING | IRQF_ONESHOT,
663						"q6v5 shutdown-ack", sysmon);
664		if (ret) {
665			dev_err(sysmon->dev,
666				"failed to acquire shutdown-ack IRQ\n");
667			kfree(sysmon);
668			return ERR_PTR(ret);
669		}
670	}
671
672	ret = qmi_handle_init(&sysmon->qmi, SSCTL_MAX_MSG_LEN, &ssctl_ops,
673			      qmi_indication_handler);
674	if (ret < 0) {
675		dev_err(sysmon->dev, "failed to initialize qmi handle\n");
676		kfree(sysmon);
677		return ERR_PTR(ret);
678	}
679
680	qmi_add_lookup(&sysmon->qmi, 43, 0, 0);
681
682	sysmon->subdev.prepare = sysmon_prepare;
683	sysmon->subdev.start = sysmon_start;
684	sysmon->subdev.stop = sysmon_stop;
685	sysmon->subdev.unprepare = sysmon_unprepare;
686
687	rproc_add_subdev(rproc, &sysmon->subdev);
688
689	sysmon->nb.notifier_call = sysmon_notify;
690	blocking_notifier_chain_register(&sysmon_notifiers, &sysmon->nb);
691
692	mutex_lock(&sysmon_lock);
693	list_add(&sysmon->node, &sysmon_list);
694	mutex_unlock(&sysmon_lock);
695
696	return sysmon;
697}
698EXPORT_SYMBOL_GPL(qcom_add_sysmon_subdev);
699
700/**
701 * qcom_remove_sysmon_subdev() - release a qcom_sysmon
702 * @sysmon:	sysmon context, as retrieved by qcom_add_sysmon_subdev()
703 */
704void qcom_remove_sysmon_subdev(struct qcom_sysmon *sysmon)
705{
706	if (!sysmon)
707		return;
708
709	mutex_lock(&sysmon_lock);
710	list_del(&sysmon->node);
711	mutex_unlock(&sysmon_lock);
712
713	blocking_notifier_chain_unregister(&sysmon_notifiers, &sysmon->nb);
714
715	rproc_remove_subdev(sysmon->rproc, &sysmon->subdev);
716
717	qmi_handle_release(&sysmon->qmi);
718
719	kfree(sysmon);
720}
721EXPORT_SYMBOL_GPL(qcom_remove_sysmon_subdev);
722
723/**
724 * qcom_sysmon_shutdown_acked() - query the success of the last shutdown
725 * @sysmon:	sysmon context
726 *
727 * When sysmon is used to request a graceful shutdown of the remote processor
728 * this can be used by the remoteproc driver to query the success, in order to
729 * know if it should fall back to other means of requesting a shutdown.
730 *
731 * Return: boolean indicator of the success of the last shutdown request
732 */
733bool qcom_sysmon_shutdown_acked(struct qcom_sysmon *sysmon)
734{
735	return sysmon && sysmon->shutdown_acked;
736}
737EXPORT_SYMBOL_GPL(qcom_sysmon_shutdown_acked);
738
739/**
740 * sysmon_probe() - probe sys_mon channel
741 * @rpdev:	rpmsg device handle
742 *
743 * Find the sysmon context associated with the ancestor remoteproc and assign
744 * this rpmsg device with said sysmon context.
745 *
746 * Return: 0 on success, negative errno on failure.
747 */
748static int sysmon_probe(struct rpmsg_device *rpdev)
749{
750	struct qcom_sysmon *sysmon;
751	struct rproc *rproc;
752
753	rproc = rproc_get_by_child(&rpdev->dev);
754	if (!rproc) {
755		dev_err(&rpdev->dev, "sysmon device not child of rproc\n");
756		return -EINVAL;
757	}
758
759	mutex_lock(&sysmon_lock);
760	list_for_each_entry(sysmon, &sysmon_list, node) {
761		if (sysmon->rproc == rproc)
762			goto found;
763	}
764	mutex_unlock(&sysmon_lock);
765
766	dev_err(&rpdev->dev, "no sysmon associated with parent rproc\n");
767
768	return -EINVAL;
769
770found:
771	mutex_unlock(&sysmon_lock);
772
773	rpdev->ept->priv = sysmon;
774	sysmon->ept = rpdev->ept;
775
776	return 0;
777}
778
779/**
780 * sysmon_remove() - sys_mon channel remove handler
781 * @rpdev:	rpmsg device handle
782 *
783 * Disassociate the rpmsg device with the sysmon instance.
784 */
785static void sysmon_remove(struct rpmsg_device *rpdev)
786{
787	struct qcom_sysmon *sysmon = rpdev->ept->priv;
788
789	sysmon->ept = NULL;
790}
791
792static const struct rpmsg_device_id sysmon_match[] = {
793	{ "sys_mon" },
794	{}
795};
796
797static struct rpmsg_driver sysmon_driver = {
798	.probe = sysmon_probe,
799	.remove = sysmon_remove,
800	.callback = sysmon_callback,
801	.id_table = sysmon_match,
802	.drv = {
803		.name = "qcom_sysmon",
804	},
805};
806
807module_rpmsg_driver(sysmon_driver);
808
809MODULE_DESCRIPTION("Qualcomm sysmon driver");
810MODULE_LICENSE("GPL v2");
v5.4
  1// 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>
  9#include <linux/interrupt.h>
 10#include <linux/io.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>
 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
 29	int shutdown_irq;
 30	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;
 39	struct completion ind_comp;
 40	struct completion shutdown_comp;
 
 41	struct mutex lock;
 42
 43	bool ssr_ack;
 
 44
 45	struct qmi_handle qmi;
 46	struct sockaddr_qrtr ssctl;
 47};
 48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 49static DEFINE_MUTEX(sysmon_lock);
 50static LIST_HEAD(sysmon_list);
 51
 52/**
 53 * sysmon_send_event() - send notification of other remote's SSR event
 54 * @sysmon:	sysmon context
 55 * @name:	other remote's name
 56 */
 57static void sysmon_send_event(struct qcom_sysmon *sysmon, const char *name)
 
 58{
 59	char req[50];
 60	int len;
 61	int ret;
 62
 63	len = snprintf(req, sizeof(req), "ssr:%s:before_shutdown", name);
 
 64	if (len >= sizeof(req))
 65		return;
 66
 67	mutex_lock(&sysmon->lock);
 68	reinit_completion(&sysmon->comp);
 69	sysmon->ssr_ack = false;
 70
 71	ret = rpmsg_send(sysmon->ept, req, len);
 72	if (ret < 0) {
 73		dev_err(sysmon->dev, "failed to send sysmon event\n");
 74		goto out_unlock;
 75	}
 76
 77	ret = wait_for_completion_timeout(&sysmon->comp,
 78					  msecs_to_jiffies(5000));
 79	if (!ret) {
 80		dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
 81		goto out_unlock;
 82	}
 83
 84	if (!sysmon->ssr_ack)
 85		dev_err(sysmon->dev, "unexpected response to sysmon event\n");
 86
 87out_unlock:
 88	mutex_unlock(&sysmon->lock);
 89}
 90
 91/**
 92 * sysmon_request_shutdown() - request graceful shutdown of remote
 93 * @sysmon:	sysmon context
 
 
 94 */
 95static void sysmon_request_shutdown(struct qcom_sysmon *sysmon)
 96{
 97	char *req = "ssr:shutdown";
 
 98	int ret;
 99
100	mutex_lock(&sysmon->lock);
101	reinit_completion(&sysmon->comp);
102	sysmon->ssr_ack = false;
103
104	ret = rpmsg_send(sysmon->ept, req, strlen(req) + 1);
105	if (ret < 0) {
106		dev_err(sysmon->dev, "send sysmon shutdown request failed\n");
107		goto out_unlock;
108	}
109
110	ret = wait_for_completion_timeout(&sysmon->comp,
111					  msecs_to_jiffies(5000));
112	if (!ret) {
113		dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
114		goto out_unlock;
115	}
116
117	if (!sysmon->ssr_ack)
118		dev_err(sysmon->dev,
119			"unexpected response to sysmon shutdown request\n");
 
 
120
121out_unlock:
122	mutex_unlock(&sysmon->lock);
 
 
123}
124
125static int sysmon_callback(struct rpmsg_device *rpdev, void *data, int count,
126			   void *priv, u32 addr)
127{
128	struct qcom_sysmon *sysmon = priv;
129	const char *ssr_ack = "ssr:ack";
130	const int ssr_ack_len = strlen(ssr_ack) + 1;
131
132	if (!sysmon)
133		return -EINVAL;
134
135	if (count >= ssr_ack_len && !memcmp(data, ssr_ack, ssr_ack_len))
136		sysmon->ssr_ack = true;
137
138	complete(&sysmon->comp);
139
140	return 0;
141}
142
143#define SSCTL_SHUTDOWN_REQ		0x21
144#define SSCTL_SHUTDOWN_READY_IND	0x21
145#define SSCTL_SUBSYS_EVENT_REQ		0x23
146
147#define SSCTL_MAX_MSG_LEN		7
148
149#define SSCTL_SUBSYS_NAME_LENGTH	15
150
151enum {
152	SSCTL_SSR_EVENT_BEFORE_POWERUP,
153	SSCTL_SSR_EVENT_AFTER_POWERUP,
154	SSCTL_SSR_EVENT_BEFORE_SHUTDOWN,
155	SSCTL_SSR_EVENT_AFTER_SHUTDOWN,
156};
157
158enum {
159	SSCTL_SSR_EVENT_FORCED,
160	SSCTL_SSR_EVENT_GRACEFUL,
161};
162
163struct ssctl_shutdown_resp {
164	struct qmi_response_type_v01 resp;
165};
166
167static struct qmi_elem_info ssctl_shutdown_resp_ei[] = {
168	{
169		.data_type	= QMI_STRUCT,
170		.elem_len	= 1,
171		.elem_size	= sizeof(struct qmi_response_type_v01),
172		.array_type	= NO_ARRAY,
173		.tlv_type	= 0x02,
174		.offset		= offsetof(struct ssctl_shutdown_resp, resp),
175		.ei_array	= qmi_response_type_v01_ei,
176	},
177	{}
178};
179
180struct ssctl_subsys_event_req {
181	u8 subsys_name_len;
182	char subsys_name[SSCTL_SUBSYS_NAME_LENGTH];
183	u32 event;
184	u8 evt_driven_valid;
185	u32 evt_driven;
186};
187
188static struct qmi_elem_info ssctl_subsys_event_req_ei[] = {
189	{
190		.data_type	= QMI_DATA_LEN,
191		.elem_len	= 1,
192		.elem_size	= sizeof(uint8_t),
193		.array_type	= NO_ARRAY,
194		.tlv_type	= 0x01,
195		.offset		= offsetof(struct ssctl_subsys_event_req,
196					   subsys_name_len),
197		.ei_array	= NULL,
198	},
199	{
200		.data_type	= QMI_UNSIGNED_1_BYTE,
201		.elem_len	= SSCTL_SUBSYS_NAME_LENGTH,
202		.elem_size	= sizeof(char),
203		.array_type	= VAR_LEN_ARRAY,
204		.tlv_type	= 0x01,
205		.offset		= offsetof(struct ssctl_subsys_event_req,
206					   subsys_name),
207		.ei_array	= NULL,
208	},
209	{
210		.data_type	= QMI_SIGNED_4_BYTE_ENUM,
211		.elem_len	= 1,
212		.elem_size	= sizeof(uint32_t),
213		.array_type	= NO_ARRAY,
214		.tlv_type	= 0x02,
215		.offset		= offsetof(struct ssctl_subsys_event_req,
216					   event),
217		.ei_array	= NULL,
218	},
219	{
220		.data_type	= QMI_OPT_FLAG,
221		.elem_len	= 1,
222		.elem_size	= sizeof(uint8_t),
223		.array_type	= NO_ARRAY,
224		.tlv_type	= 0x10,
225		.offset		= offsetof(struct ssctl_subsys_event_req,
226					   evt_driven_valid),
227		.ei_array	= NULL,
228	},
229	{
230		.data_type	= QMI_SIGNED_4_BYTE_ENUM,
231		.elem_len	= 1,
232		.elem_size	= sizeof(uint32_t),
233		.array_type	= NO_ARRAY,
234		.tlv_type	= 0x10,
235		.offset		= offsetof(struct ssctl_subsys_event_req,
236					   evt_driven),
237		.ei_array	= NULL,
238	},
239	{}
240};
241
242struct ssctl_subsys_event_resp {
243	struct qmi_response_type_v01 resp;
244};
245
246static struct qmi_elem_info ssctl_subsys_event_resp_ei[] = {
247	{
248		.data_type	= QMI_STRUCT,
249		.elem_len	= 1,
250		.elem_size	= sizeof(struct qmi_response_type_v01),
251		.array_type	= NO_ARRAY,
252		.tlv_type	= 0x02,
253		.offset		= offsetof(struct ssctl_subsys_event_resp,
254					   resp),
255		.ei_array	= qmi_response_type_v01_ei,
256	},
257	{}
258};
259
260static struct qmi_elem_info ssctl_shutdown_ind_ei[] = {
261	{}
262};
263
264static void sysmon_ind_cb(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
265			  struct qmi_txn *txn, const void *data)
266{
267	struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
268
269	complete(&sysmon->ind_comp);
270}
271
272static struct qmi_msg_handler qmi_indication_handler[] = {
273	{
274		.type = QMI_INDICATION,
275		.msg_id = SSCTL_SHUTDOWN_READY_IND,
276		.ei = ssctl_shutdown_ind_ei,
277		.decoded_size = 0,
278		.fn = sysmon_ind_cb
279	},
280	{}
281};
282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283/**
284 * ssctl_request_shutdown() - request shutdown via SSCTL QMI service
285 * @sysmon:	sysmon context
 
 
286 */
287static void ssctl_request_shutdown(struct qcom_sysmon *sysmon)
288{
289	struct ssctl_shutdown_resp resp;
290	struct qmi_txn txn;
 
291	int ret;
292
293	reinit_completion(&sysmon->ind_comp);
294	reinit_completion(&sysmon->shutdown_comp);
295	ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_shutdown_resp_ei, &resp);
296	if (ret < 0) {
297		dev_err(sysmon->dev, "failed to allocate QMI txn\n");
298		return;
299	}
300
301	ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
302			       SSCTL_SHUTDOWN_REQ, 0, NULL, NULL);
303	if (ret < 0) {
304		dev_err(sysmon->dev, "failed to send shutdown request\n");
305		qmi_txn_cancel(&txn);
306		return;
307	}
308
309	ret = qmi_txn_wait(&txn, 5 * HZ);
310	if (ret < 0)
311		dev_err(sysmon->dev, "failed receiving QMI response\n");
312	else if (resp.resp.result)
313		dev_err(sysmon->dev, "shutdown request failed\n");
314	else
315		dev_dbg(sysmon->dev, "shutdown request completed\n");
 
 
 
 
 
316
317	if (sysmon->shutdown_irq > 0) {
318		ret = wait_for_completion_timeout(&sysmon->shutdown_comp,
319						  10 * HZ);
320		if (!ret) {
321			ret = try_wait_for_completion(&sysmon->ind_comp);
322			if (!ret)
323				dev_err(sysmon->dev,
324					"timeout waiting for shutdown ack\n");
325		}
326	}
327}
328
329/**
330 * ssctl_send_event() - send notification of other remote's SSR event
331 * @sysmon:	sysmon context
332 * @name:	other remote's name
333 */
334static void ssctl_send_event(struct qcom_sysmon *sysmon, const char *name)
 
335{
336	struct ssctl_subsys_event_resp resp;
337	struct ssctl_subsys_event_req req;
338	struct qmi_txn txn;
339	int ret;
340
341	memset(&resp, 0, sizeof(resp));
342	ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_subsys_event_resp_ei, &resp);
343	if (ret < 0) {
344		dev_err(sysmon->dev, "failed to allocate QMI txn\n");
345		return;
346	}
347
348	memset(&req, 0, sizeof(req));
349	strlcpy(req.subsys_name, name, sizeof(req.subsys_name));
350	req.subsys_name_len = strlen(req.subsys_name);
351	req.event = SSCTL_SSR_EVENT_BEFORE_SHUTDOWN;
352	req.evt_driven_valid = true;
353	req.evt_driven = SSCTL_SSR_EVENT_FORCED;
354
355	ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
356			       SSCTL_SUBSYS_EVENT_REQ, 40,
357			       ssctl_subsys_event_req_ei, &req);
358	if (ret < 0) {
359		dev_err(sysmon->dev, "failed to send shutdown request\n");
360		qmi_txn_cancel(&txn);
361		return;
362	}
363
364	ret = qmi_txn_wait(&txn, 5 * HZ);
365	if (ret < 0)
366		dev_err(sysmon->dev, "failed receiving QMI response\n");
367	else if (resp.resp.result)
368		dev_err(sysmon->dev, "ssr event send failed\n");
369	else
370		dev_dbg(sysmon->dev, "ssr event send completed\n");
371}
372
373/**
374 * ssctl_new_server() - QMI callback indicating a new service
375 * @qmi:	QMI handle
376 * @svc:	service information
377 *
378 * Return: 0 if we're interested in this service, -EINVAL otherwise.
379 */
380static int ssctl_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
381{
382	struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
383
384	switch (svc->version) {
385	case 1:
386		if (svc->instance != 0)
387			return -EINVAL;
388		if (strcmp(sysmon->name, "modem"))
389			return -EINVAL;
390		break;
391	case 2:
392		if (svc->instance != sysmon->ssctl_instance)
393			return -EINVAL;
394		break;
395	default:
396		return -EINVAL;
397	};
398
399	sysmon->ssctl_version = svc->version;
400
401	sysmon->ssctl.sq_family = AF_QIPCRTR;
402	sysmon->ssctl.sq_node = svc->node;
403	sysmon->ssctl.sq_port = svc->port;
404
405	svc->priv = sysmon;
406
 
 
407	return 0;
408}
409
410/**
411 * ssctl_del_server() - QMI callback indicating that @svc is removed
412 * @qmi:	QMI handle
413 * @svc:	service information
414 */
415static void ssctl_del_server(struct qmi_handle *qmi, struct qmi_service *svc)
416{
417	struct qcom_sysmon *sysmon = svc->priv;
418
419	sysmon->ssctl_version = 0;
420}
421
422static const struct qmi_ops ssctl_ops = {
423	.new_server = ssctl_new_server,
424	.del_server = ssctl_del_server,
425};
426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
427static int sysmon_start(struct rproc_subdev *subdev)
428{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
429	return 0;
430}
431
432static void sysmon_stop(struct rproc_subdev *subdev, bool crashed)
433{
434	struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon, subdev);
 
 
 
 
435
436	blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)sysmon->name);
 
 
 
 
 
437
438	/* Don't request graceful shutdown if we've crashed */
439	if (crashed)
440		return;
441
 
 
 
 
 
442	if (sysmon->ssctl_version)
443		ssctl_request_shutdown(sysmon);
444	else if (sysmon->ept)
445		sysmon_request_shutdown(sysmon);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446}
447
448/**
449 * sysmon_notify() - notify sysmon target of another's SSR
450 * @nb:		notifier_block associated with sysmon instance
451 * @event:	unused
452 * @data:	SSR identifier of the remote that is going down
453 */
454static int sysmon_notify(struct notifier_block *nb, unsigned long event,
455			 void *data)
456{
457	struct qcom_sysmon *sysmon = container_of(nb, struct qcom_sysmon, nb);
458	struct rproc *rproc = sysmon->rproc;
459	const char *ssr_name = data;
460
461	/* Skip non-running rprocs and the originating instance */
462	if (rproc->state != RPROC_RUNNING || !strcmp(data, sysmon->name)) {
 
463		dev_dbg(sysmon->dev, "not notifying %s\n", sysmon->name);
464		return NOTIFY_DONE;
465	}
466
467	/* Only SSCTL version 2 supports SSR events */
468	if (sysmon->ssctl_version == 2)
469		ssctl_send_event(sysmon, ssr_name);
470	else if (sysmon->ept)
471		sysmon_send_event(sysmon, ssr_name);
472
473	return NOTIFY_DONE;
474}
475
476static irqreturn_t sysmon_shutdown_interrupt(int irq, void *data)
477{
478	struct qcom_sysmon *sysmon = data;
479
480	complete(&sysmon->shutdown_comp);
481
482	return IRQ_HANDLED;
483}
484
485/**
486 * qcom_add_sysmon_subdev() - create a sysmon subdev for the given remoteproc
487 * @rproc:	rproc context to associate the subdev with
488 * @name:	name of this subdev, to use in SSR
489 * @ssctl_instance: instance id of the ssctl QMI service
490 *
491 * Return: A new qcom_sysmon object, or NULL on failure
492 */
493struct qcom_sysmon *qcom_add_sysmon_subdev(struct rproc *rproc,
494					   const char *name,
495					   int ssctl_instance)
496{
497	struct qcom_sysmon *sysmon;
498	int ret;
499
500	sysmon = kzalloc(sizeof(*sysmon), GFP_KERNEL);
501	if (!sysmon)
502		return ERR_PTR(-ENOMEM);
503
504	sysmon->dev = rproc->dev.parent;
505	sysmon->rproc = rproc;
506
507	sysmon->name = name;
508	sysmon->ssctl_instance = ssctl_instance;
509
510	init_completion(&sysmon->comp);
511	init_completion(&sysmon->ind_comp);
512	init_completion(&sysmon->shutdown_comp);
 
513	mutex_init(&sysmon->lock);
 
514
515	sysmon->shutdown_irq = of_irq_get_byname(sysmon->dev->of_node,
516						 "shutdown-ack");
517	if (sysmon->shutdown_irq < 0) {
518		if (sysmon->shutdown_irq != -ENODATA) {
519			dev_err(sysmon->dev,
520				"failed to retrieve shutdown-ack IRQ\n");
521			return ERR_PTR(sysmon->shutdown_irq);
 
 
522		}
523	} else {
524		ret = devm_request_threaded_irq(sysmon->dev,
525						sysmon->shutdown_irq,
526						NULL, sysmon_shutdown_interrupt,
527						IRQF_TRIGGER_RISING | IRQF_ONESHOT,
528						"q6v5 shutdown-ack", sysmon);
529		if (ret) {
530			dev_err(sysmon->dev,
531				"failed to acquire shutdown-ack IRQ\n");
 
532			return ERR_PTR(ret);
533		}
534	}
535
536	ret = qmi_handle_init(&sysmon->qmi, SSCTL_MAX_MSG_LEN, &ssctl_ops,
537			      qmi_indication_handler);
538	if (ret < 0) {
539		dev_err(sysmon->dev, "failed to initialize qmi handle\n");
540		kfree(sysmon);
541		return ERR_PTR(ret);
542	}
543
544	qmi_add_lookup(&sysmon->qmi, 43, 0, 0);
545
 
546	sysmon->subdev.start = sysmon_start;
547	sysmon->subdev.stop = sysmon_stop;
 
548
549	rproc_add_subdev(rproc, &sysmon->subdev);
550
551	sysmon->nb.notifier_call = sysmon_notify;
552	blocking_notifier_chain_register(&sysmon_notifiers, &sysmon->nb);
553
554	mutex_lock(&sysmon_lock);
555	list_add(&sysmon->node, &sysmon_list);
556	mutex_unlock(&sysmon_lock);
557
558	return sysmon;
559}
560EXPORT_SYMBOL_GPL(qcom_add_sysmon_subdev);
561
562/**
563 * qcom_remove_sysmon_subdev() - release a qcom_sysmon
564 * @sysmon:	sysmon context, as retrieved by qcom_add_sysmon_subdev()
565 */
566void qcom_remove_sysmon_subdev(struct qcom_sysmon *sysmon)
567{
568	if (!sysmon)
569		return;
570
571	mutex_lock(&sysmon_lock);
572	list_del(&sysmon->node);
573	mutex_unlock(&sysmon_lock);
574
575	blocking_notifier_chain_unregister(&sysmon_notifiers, &sysmon->nb);
576
577	rproc_remove_subdev(sysmon->rproc, &sysmon->subdev);
578
579	qmi_handle_release(&sysmon->qmi);
580
581	kfree(sysmon);
582}
583EXPORT_SYMBOL_GPL(qcom_remove_sysmon_subdev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584
585/**
586 * sysmon_probe() - probe sys_mon channel
587 * @rpdev:	rpmsg device handle
588 *
589 * Find the sysmon context associated with the ancestor remoteproc and assign
590 * this rpmsg device with said sysmon context.
591 *
592 * Return: 0 on success, negative errno on failure.
593 */
594static int sysmon_probe(struct rpmsg_device *rpdev)
595{
596	struct qcom_sysmon *sysmon;
597	struct rproc *rproc;
598
599	rproc = rproc_get_by_child(&rpdev->dev);
600	if (!rproc) {
601		dev_err(&rpdev->dev, "sysmon device not child of rproc\n");
602		return -EINVAL;
603	}
604
605	mutex_lock(&sysmon_lock);
606	list_for_each_entry(sysmon, &sysmon_list, node) {
607		if (sysmon->rproc == rproc)
608			goto found;
609	}
610	mutex_unlock(&sysmon_lock);
611
612	dev_err(&rpdev->dev, "no sysmon associated with parent rproc\n");
613
614	return -EINVAL;
615
616found:
617	mutex_unlock(&sysmon_lock);
618
619	rpdev->ept->priv = sysmon;
620	sysmon->ept = rpdev->ept;
621
622	return 0;
623}
624
625/**
626 * sysmon_remove() - sys_mon channel remove handler
627 * @rpdev:	rpmsg device handle
628 *
629 * Disassociate the rpmsg device with the sysmon instance.
630 */
631static void sysmon_remove(struct rpmsg_device *rpdev)
632{
633	struct qcom_sysmon *sysmon = rpdev->ept->priv;
634
635	sysmon->ept = NULL;
636}
637
638static const struct rpmsg_device_id sysmon_match[] = {
639	{ "sys_mon" },
640	{}
641};
642
643static struct rpmsg_driver sysmon_driver = {
644	.probe = sysmon_probe,
645	.remove = sysmon_remove,
646	.callback = sysmon_callback,
647	.id_table = sysmon_match,
648	.drv = {
649		.name = "qcom_sysmon",
650	},
651};
652
653module_rpmsg_driver(sysmon_driver);
654
655MODULE_DESCRIPTION("Qualcomm sysmon driver");
656MODULE_LICENSE("GPL v2");