xref: /linux/drivers/soc/qcom/qcom_pd_mapper.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Qualcomm Protection Domain mapper
4  *
5  * Copyright (c) 2023 Linaro Ltd.
6  */
7 
8 #include <linux/auxiliary_bus.h>
9 #include <linux/kernel.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/refcount.h>
14 #include <linux/slab.h>
15 #include <linux/soc/qcom/qmi.h>
16 
17 #include "pdr_internal.h"
18 
19 #define SERVREG_QMI_VERSION 0x101
20 #define SERVREG_QMI_INSTANCE 0
21 
22 #define TMS_SERVREG_SERVICE "tms/servreg"
23 
24 struct qcom_pdm_domain_data {
25 	const char *domain;
26 	u32 instance_id;
27 	/* NULL-terminated array */
28 	const char * services[];
29 };
30 
31 struct qcom_pdm_domain {
32 	struct list_head list;
33 	const char *name;
34 	u32 instance_id;
35 };
36 
37 struct qcom_pdm_service {
38 	struct list_head list;
39 	struct list_head domains;
40 	const char *name;
41 };
42 
43 struct qcom_pdm_data {
44 	refcount_t refcnt;
45 	struct qmi_handle handle;
46 	struct list_head services;
47 };
48 
49 static DEFINE_MUTEX(qcom_pdm_mutex); /* protects __qcom_pdm_data */
50 static struct qcom_pdm_data *__qcom_pdm_data;
51 
52 static struct qcom_pdm_service *qcom_pdm_find(struct qcom_pdm_data *data,
53 					      const char *name)
54 {
55 	struct qcom_pdm_service *service;
56 
57 	list_for_each_entry(service, &data->services, list) {
58 		if (!strcmp(service->name, name))
59 			return service;
60 	}
61 
62 	return NULL;
63 }
64 
65 static int qcom_pdm_add_service_domain(struct qcom_pdm_data *data,
66 				       const char *service_name,
67 				       const char *domain_name,
68 				       u32 instance_id)
69 {
70 	struct qcom_pdm_service *service;
71 	struct qcom_pdm_domain *domain;
72 
73 	service = qcom_pdm_find(data, service_name);
74 	if (service) {
75 		list_for_each_entry(domain, &service->domains, list) {
76 			if (!strcmp(domain->name, domain_name))
77 				return -EBUSY;
78 		}
79 	} else {
80 		service = kzalloc_obj(*service);
81 		if (!service)
82 			return -ENOMEM;
83 
84 		INIT_LIST_HEAD(&service->domains);
85 		service->name = service_name;
86 
87 		list_add_tail(&service->list, &data->services);
88 	}
89 
90 	domain = kzalloc_obj(*domain);
91 	if (!domain) {
92 		if (list_empty(&service->domains)) {
93 			list_del(&service->list);
94 			kfree(service);
95 		}
96 
97 		return -ENOMEM;
98 	}
99 
100 	domain->name = domain_name;
101 	domain->instance_id = instance_id;
102 	list_add_tail(&domain->list, &service->domains);
103 
104 	return 0;
105 }
106 
107 static int qcom_pdm_add_domain(struct qcom_pdm_data *data,
108 			       const struct qcom_pdm_domain_data *domain)
109 {
110 	int ret;
111 	int i;
112 
113 	ret = qcom_pdm_add_service_domain(data,
114 					  TMS_SERVREG_SERVICE,
115 					  domain->domain,
116 					  domain->instance_id);
117 	if (ret)
118 		return ret;
119 
120 	for (i = 0; domain->services[i]; i++) {
121 		ret = qcom_pdm_add_service_domain(data,
122 						  domain->services[i],
123 						  domain->domain,
124 						  domain->instance_id);
125 		if (ret)
126 			return ret;
127 	}
128 
129 	return 0;
130 
131 }
132 
133 static void qcom_pdm_free_domains(struct qcom_pdm_data *data)
134 {
135 	struct qcom_pdm_service *service, *tservice;
136 	struct qcom_pdm_domain *domain, *tdomain;
137 
138 	list_for_each_entry_safe(service, tservice, &data->services, list) {
139 		list_for_each_entry_safe(domain, tdomain, &service->domains, list) {
140 			list_del(&domain->list);
141 			kfree(domain);
142 		}
143 
144 		list_del(&service->list);
145 		kfree(service);
146 	}
147 }
148 
149 static void qcom_pdm_get_domain_list(struct qmi_handle *qmi,
150 				     struct sockaddr_qrtr *sq,
151 				     struct qmi_txn *txn,
152 				     const void *decoded)
153 {
154 	struct qcom_pdm_data *data = container_of(qmi, struct qcom_pdm_data, handle);
155 	const struct servreg_get_domain_list_req *req = decoded;
156 	struct servreg_get_domain_list_resp *rsp;
157 	struct qcom_pdm_service *service;
158 	u32 offset;
159 	int ret;
160 
161 	rsp = kzalloc_obj(*rsp);
162 	if (!rsp)
163 		return;
164 
165 	offset = req->domain_offset_valid ? req->domain_offset : 0;
166 
167 	rsp->resp.result = QMI_RESULT_SUCCESS_V01;
168 	rsp->resp.error = QMI_ERR_NONE_V01;
169 
170 	rsp->db_rev_count_valid = true;
171 	rsp->db_rev_count = 1;
172 
173 	rsp->total_domains_valid = true;
174 	rsp->total_domains = 0;
175 
176 	mutex_lock(&qcom_pdm_mutex);
177 
178 	service = qcom_pdm_find(data, req->service_name);
179 	if (service) {
180 		struct qcom_pdm_domain *domain;
181 
182 		rsp->domain_list_valid = true;
183 		rsp->domain_list_len = 0;
184 
185 		list_for_each_entry(domain, &service->domains, list) {
186 			u32 i = rsp->total_domains++;
187 
188 			if (i >= offset && i < SERVREG_DOMAIN_LIST_LENGTH) {
189 				u32 j = rsp->domain_list_len++;
190 
191 				strscpy(rsp->domain_list[j].name, domain->name,
192 					sizeof(rsp->domain_list[i].name));
193 				rsp->domain_list[j].instance = domain->instance_id;
194 
195 				pr_debug("PDM: found %s / %d\n", domain->name,
196 					 domain->instance_id);
197 			}
198 		}
199 	}
200 
201 	pr_debug("PDM: service '%s' offset %d returning %d domains (of %d)\n", req->service_name,
202 		 req->domain_offset_valid ? req->domain_offset : -1, rsp->domain_list_len, rsp->total_domains);
203 
204 	ret = qmi_send_response(qmi, sq, txn, SERVREG_GET_DOMAIN_LIST_REQ,
205 				SERVREG_GET_DOMAIN_LIST_RESP_MAX_LEN,
206 				servreg_get_domain_list_resp_ei, rsp);
207 	if (ret)
208 		pr_err("Error sending servreg response: %d\n", ret);
209 
210 	mutex_unlock(&qcom_pdm_mutex);
211 
212 	kfree(rsp);
213 }
214 
215 static void qcom_pdm_pfr(struct qmi_handle *qmi,
216 			 struct sockaddr_qrtr *sq,
217 			 struct qmi_txn *txn,
218 			 const void *decoded)
219 {
220 	const struct servreg_loc_pfr_req *req = decoded;
221 	struct servreg_loc_pfr_resp rsp = {};
222 	int ret;
223 
224 	pr_warn_ratelimited("PDM: service '%s' crash: '%s'\n", req->service, req->reason);
225 
226 	rsp.rsp.result = QMI_RESULT_SUCCESS_V01;
227 	rsp.rsp.error = QMI_ERR_NONE_V01;
228 
229 	ret = qmi_send_response(qmi, sq, txn, SERVREG_LOC_PFR_REQ,
230 				SERVREG_LOC_PFR_RESP_MAX_LEN,
231 				servreg_loc_pfr_resp_ei, &rsp);
232 	if (ret)
233 		pr_err("Error sending servreg response: %d\n", ret);
234 }
235 
236 static const struct qmi_msg_handler qcom_pdm_msg_handlers[] = {
237 	{
238 		.type = QMI_REQUEST,
239 		.msg_id = SERVREG_GET_DOMAIN_LIST_REQ,
240 		.ei = servreg_get_domain_list_req_ei,
241 		.decoded_size = sizeof(struct servreg_get_domain_list_req),
242 		.fn = qcom_pdm_get_domain_list,
243 	},
244 	{
245 		.type = QMI_REQUEST,
246 		.msg_id = SERVREG_LOC_PFR_REQ,
247 		.ei = servreg_loc_pfr_req_ei,
248 		.decoded_size = sizeof(struct servreg_loc_pfr_req),
249 		.fn = qcom_pdm_pfr,
250 	},
251 	{ },
252 };
253 
254 static const struct qcom_pdm_domain_data adsp_audio_pd = {
255 	.domain = "msm/adsp/audio_pd",
256 	.instance_id = 74,
257 	.services = {
258 		"avs/audio",
259 		NULL,
260 	},
261 };
262 
263 static const struct qcom_pdm_domain_data adsp_charger_pd = {
264 	.domain = "msm/adsp/charger_pd",
265 	.instance_id = 74,
266 	.services = { NULL },
267 };
268 
269 static const struct qcom_pdm_domain_data adsp_ois_pd = {
270 	.domain = "msm/adsp/ois_pd",
271 	.instance_id = 74,
272 	.services = { NULL, },
273 };
274 
275 static const struct qcom_pdm_domain_data adsp_root_pd = {
276 	.domain = "msm/adsp/root_pd",
277 	.instance_id = 74,
278 	.services = { NULL },
279 };
280 
281 static const struct qcom_pdm_domain_data adsp_root_pd_pdr = {
282 	.domain = "msm/adsp/root_pd",
283 	.instance_id = 74,
284 	.services = {
285 		"tms/pdr_enabled",
286 		NULL,
287 	},
288 };
289 
290 static const struct qcom_pdm_domain_data adsp_sensor_pd = {
291 	.domain = "msm/adsp/sensor_pd",
292 	.instance_id = 74,
293 	.services = { NULL },
294 };
295 
296 static const struct qcom_pdm_domain_data msm8996_adsp_audio_pd = {
297 	.domain = "msm/adsp/audio_pd",
298 	.instance_id = 4,
299 	.services = { NULL },
300 };
301 
302 static const struct qcom_pdm_domain_data msm8996_adsp_root_pd = {
303 	.domain = "msm/adsp/root_pd",
304 	.instance_id = 4,
305 	.services = { NULL },
306 };
307 
308 static const struct qcom_pdm_domain_data cdsp_root_pd = {
309 	.domain = "msm/cdsp/root_pd",
310 	.instance_id = 76,
311 	.services = { NULL },
312 };
313 
314 static const struct qcom_pdm_domain_data slpi_root_pd = {
315 	.domain = "msm/slpi/root_pd",
316 	.instance_id = 90,
317 	.services = { NULL },
318 };
319 
320 static const struct qcom_pdm_domain_data slpi_sensor_pd = {
321 	.domain = "msm/slpi/sensor_pd",
322 	.instance_id = 90,
323 	.services = { NULL },
324 };
325 
326 static const struct qcom_pdm_domain_data mpss_root_pd = {
327 	.domain = "msm/modem/root_pd",
328 	.instance_id = 180,
329 	.services = {
330 		NULL,
331 	},
332 };
333 
334 static const struct qcom_pdm_domain_data mpss_root_pd_gps = {
335 	.domain = "msm/modem/root_pd",
336 	.instance_id = 180,
337 	.services = {
338 		"gps/gps_service",
339 		NULL,
340 	},
341 };
342 
343 static const struct qcom_pdm_domain_data mpss_root_pd_gps_pdr = {
344 	.domain = "msm/modem/root_pd",
345 	.instance_id = 180,
346 	.services = {
347 		"gps/gps_service",
348 		"tms/pdr_enabled",
349 		NULL,
350 	},
351 };
352 
353 static const struct qcom_pdm_domain_data msm8996_mpss_root_pd = {
354 	.domain = "msm/modem/root_pd",
355 	.instance_id = 100,
356 	.services = { NULL },
357 };
358 
359 static const struct qcom_pdm_domain_data mpss_wlan_pd = {
360 	.domain = "msm/modem/wlan_pd",
361 	.instance_id = 180,
362 	.services = {
363 		"kernel/elf_loader",
364 		"wlan/fw",
365 		NULL,
366 	},
367 };
368 
369 static const struct qcom_pdm_domain_data *glymur_domains[] = {
370 	&adsp_audio_pd,
371 	&adsp_root_pd,
372 	&adsp_sensor_pd,
373 	&cdsp_root_pd,
374 	NULL,
375 };
376 
377 static const struct qcom_pdm_domain_data *kaanapali_domains[] = {
378 	&adsp_audio_pd,
379 	&adsp_ois_pd,
380 	&adsp_root_pd,
381 	&adsp_sensor_pd,
382 	&cdsp_root_pd,
383 	&mpss_root_pd_gps,
384 	NULL,
385 };
386 
387 static const struct qcom_pdm_domain_data *msm8996_domains[] = {
388 	&msm8996_adsp_audio_pd,
389 	&msm8996_adsp_root_pd,
390 	&msm8996_mpss_root_pd,
391 	NULL,
392 };
393 
394 static const struct qcom_pdm_domain_data *msm8998_domains[] = {
395 	&mpss_root_pd,
396 	&mpss_wlan_pd,
397 	NULL,
398 };
399 
400 static const struct qcom_pdm_domain_data *qcm2290_domains[] = {
401 	&adsp_audio_pd,
402 	&adsp_root_pd,
403 	&adsp_sensor_pd,
404 	&mpss_root_pd_gps,
405 	&mpss_wlan_pd,
406 	NULL,
407 };
408 
409 static const struct qcom_pdm_domain_data *qcs404_domains[] = {
410 	&adsp_audio_pd,
411 	&adsp_root_pd,
412 	&adsp_sensor_pd,
413 	&cdsp_root_pd,
414 	&mpss_root_pd,
415 	&mpss_wlan_pd,
416 	NULL,
417 };
418 
419 static const struct qcom_pdm_domain_data *qcs615_domains[] = {
420 	&adsp_audio_pd,
421 	&adsp_root_pd,
422 	&adsp_sensor_pd,
423 	&cdsp_root_pd,
424 	&mpss_root_pd,
425 	&mpss_wlan_pd,
426 	NULL,
427 };
428 
429 static const struct qcom_pdm_domain_data *sc7180_domains[] = {
430 	&adsp_audio_pd,
431 	&adsp_root_pd_pdr,
432 	&adsp_sensor_pd,
433 	&mpss_root_pd_gps_pdr,
434 	&mpss_wlan_pd,
435 	NULL,
436 };
437 
438 static const struct qcom_pdm_domain_data *sc7280_domains[] = {
439 	&adsp_audio_pd,
440 	&adsp_root_pd_pdr,
441 	&adsp_charger_pd,
442 	&adsp_sensor_pd,
443 	&cdsp_root_pd,
444 	&mpss_root_pd_gps_pdr,
445 	NULL,
446 };
447 
448 static const struct qcom_pdm_domain_data *sc8180x_domains[] = {
449 	&adsp_audio_pd,
450 	&adsp_root_pd,
451 	&adsp_charger_pd,
452 	&cdsp_root_pd,
453 	&mpss_root_pd_gps,
454 	&mpss_wlan_pd,
455 	NULL,
456 };
457 
458 static const struct qcom_pdm_domain_data *sc8280xp_domains[] = {
459 	&adsp_audio_pd,
460 	&adsp_root_pd_pdr,
461 	&adsp_charger_pd,
462 	&cdsp_root_pd,
463 	NULL,
464 };
465 
466 /* Unlike SDM660, SDM630/636 lack CDSP */
467 static const struct qcom_pdm_domain_data *sdm630_domains[] = {
468 	&adsp_audio_pd,
469 	&adsp_root_pd,
470 	&adsp_sensor_pd,
471 	&mpss_root_pd,
472 	&mpss_wlan_pd,
473 	NULL,
474 };
475 
476 static const struct qcom_pdm_domain_data *sdm660_domains[] = {
477 	&adsp_audio_pd,
478 	&adsp_root_pd,
479 	&adsp_sensor_pd,
480 	&cdsp_root_pd,
481 	&mpss_root_pd,
482 	&mpss_wlan_pd,
483 	NULL,
484 };
485 
486 static const struct qcom_pdm_domain_data *sdm670_domains[] = {
487 	&adsp_audio_pd,
488 	&adsp_root_pd,
489 	&cdsp_root_pd,
490 	&mpss_root_pd,
491 	&mpss_wlan_pd,
492 	NULL,
493 };
494 
495 static const struct qcom_pdm_domain_data *sdm845_domains[] = {
496 	&adsp_audio_pd,
497 	&adsp_root_pd,
498 	&cdsp_root_pd,
499 	&mpss_root_pd,
500 	&mpss_wlan_pd,
501 	&slpi_root_pd,
502 	&slpi_sensor_pd,
503 	NULL,
504 };
505 
506 static const struct qcom_pdm_domain_data *sm6115_domains[] = {
507 	&adsp_audio_pd,
508 	&adsp_root_pd,
509 	&adsp_sensor_pd,
510 	&cdsp_root_pd,
511 	&mpss_root_pd_gps,
512 	&mpss_wlan_pd,
513 	NULL,
514 };
515 
516 static const struct qcom_pdm_domain_data *sm6350_domains[] = {
517 	&adsp_audio_pd,
518 	&adsp_root_pd,
519 	&adsp_sensor_pd,
520 	&cdsp_root_pd,
521 	&mpss_wlan_pd,
522 	NULL,
523 };
524 
525 static const struct qcom_pdm_domain_data *sm7150_domains[] = {
526 	&adsp_audio_pd,
527 	&adsp_root_pd,
528 	&adsp_sensor_pd,
529 	&cdsp_root_pd,
530 	&mpss_root_pd_gps,
531 	&mpss_wlan_pd,
532 	NULL,
533 };
534 
535 static const struct qcom_pdm_domain_data *sm8150_domains[] = {
536 	&adsp_audio_pd,
537 	&adsp_root_pd,
538 	&cdsp_root_pd,
539 	&mpss_root_pd_gps,
540 	&mpss_wlan_pd,
541 	NULL,
542 };
543 
544 static const struct qcom_pdm_domain_data *sm8250_domains[] = {
545 	&adsp_audio_pd,
546 	&adsp_root_pd,
547 	&cdsp_root_pd,
548 	&slpi_root_pd,
549 	&slpi_sensor_pd,
550 	NULL,
551 };
552 
553 static const struct qcom_pdm_domain_data *sm8350_domains[] = {
554 	&adsp_audio_pd,
555 	&adsp_root_pd_pdr,
556 	&adsp_charger_pd,
557 	&cdsp_root_pd,
558 	&mpss_root_pd_gps,
559 	&slpi_root_pd,
560 	&slpi_sensor_pd,
561 	NULL,
562 };
563 
564 static const struct qcom_pdm_domain_data *sm8550_domains[] = {
565 	&adsp_audio_pd,
566 	&adsp_root_pd,
567 	&adsp_charger_pd,
568 	&adsp_sensor_pd,
569 	&cdsp_root_pd,
570 	&mpss_root_pd_gps,
571 	NULL,
572 };
573 
574 static const struct qcom_pdm_domain_data *x1e80100_domains[] = {
575 	&adsp_audio_pd,
576 	&adsp_root_pd,
577 	&adsp_charger_pd,
578 	&adsp_sensor_pd,
579 	&cdsp_root_pd,
580 	NULL,
581 };
582 
583 static const struct of_device_id qcom_pdm_domains[] __maybe_unused = {
584 	{ .compatible = "qcom,apq8016", .data = NULL, },
585 	{ .compatible = "qcom,apq8064", .data = NULL, },
586 	{ .compatible = "qcom,apq8074", .data = NULL, },
587 	{ .compatible = "qcom,apq8084", .data = NULL, },
588 	{ .compatible = "qcom,eliza", .data = sm8550_domains, },
589 	{ .compatible = "qcom,apq8096", .data = msm8996_domains, },
590 	{ .compatible = "qcom,glymur", .data = glymur_domains, },
591 	{ .compatible = "qcom,hawi", .data = kaanapali_domains, },
592 	{ .compatible = "qcom,kaanapali", .data = kaanapali_domains, },
593 	{ .compatible = "qcom,mahua", .data = glymur_domains, },
594 	{ .compatible = "qcom,milos", .data = sm8550_domains, },
595 	{ .compatible = "qcom,msm8226", .data = NULL, },
596 	{ .compatible = "qcom,msm8909", .data = NULL, },
597 	{ .compatible = "qcom,msm8916", .data = NULL, },
598 	{ .compatible = "qcom,msm8939", .data = NULL, },
599 	{ .compatible = "qcom,msm8974", .data = NULL, },
600 	{ .compatible = "qcom,msm8996", .data = msm8996_domains, },
601 	{ .compatible = "qcom,msm8998", .data = msm8998_domains, },
602 	{ .compatible = "qcom,qcm2290", .data = qcm2290_domains, },
603 	{ .compatible = "qcom,qcm6490", .data = sc7280_domains, },
604 	{ .compatible = "qcom,qcs404", .data = qcs404_domains, },
605 	{ .compatible = "qcom,qcs615", .data = qcs615_domains, },
606 	{ .compatible = "qcom,sc7180", .data = sc7180_domains, },
607 	{ .compatible = "qcom,sc7280", .data = sc7280_domains, },
608 	{ .compatible = "qcom,sc8180x", .data = sc8180x_domains, },
609 	{ .compatible = "qcom,sc8280xp", .data = sc8280xp_domains, },
610 	{ .compatible = "qcom,sdm630", .data = sdm630_domains, },
611 	{ .compatible = "qcom,sdm636", .data = sdm630_domains, },
612 	{ .compatible = "qcom,sda660", .data = sdm660_domains, },
613 	{ .compatible = "qcom,sdm660", .data = sdm660_domains, },
614 	{ .compatible = "qcom,sdm670", .data = sdm670_domains, },
615 	{ .compatible = "qcom,sdm845", .data = sdm845_domains, },
616 	{ .compatible = "qcom,sm4250", .data = sm6115_domains, },
617 	{ .compatible = "qcom,sm6115", .data = sm6115_domains, },
618 	{ .compatible = "qcom,sm6350", .data = sm6350_domains, },
619 	{ .compatible = "qcom,sm7150", .data = sm7150_domains, },
620 	{ .compatible = "qcom,sm7225", .data = sm6350_domains, },
621 	{ .compatible = "qcom,sm7325", .data = sc7280_domains, },
622 	{ .compatible = "qcom,sm8150", .data = sm8150_domains, },
623 	{ .compatible = "qcom,sm8250", .data = sm8250_domains, },
624 	{ .compatible = "qcom,sm8350", .data = sm8350_domains, },
625 	{ .compatible = "qcom,sm8450", .data = sm8350_domains, },
626 	{ .compatible = "qcom,sm8550", .data = sm8550_domains, },
627 	{ .compatible = "qcom,sm8650", .data = sm8550_domains, },
628 	{ .compatible = "qcom,sm8750", .data = sm8550_domains, },
629 	{ .compatible = "qcom,x1e80100", .data = x1e80100_domains, },
630 	{ .compatible = "qcom,x1p42100", .data = x1e80100_domains, },
631 	{},
632 };
633 
634 static void qcom_pdm_stop(struct qcom_pdm_data *data)
635 {
636 	qcom_pdm_free_domains(data);
637 
638 	/* The server is removed automatically */
639 	qmi_handle_release(&data->handle);
640 
641 	kfree(data);
642 }
643 
644 static struct qcom_pdm_data *qcom_pdm_start(void)
645 {
646 	const struct qcom_pdm_domain_data * const *domains;
647 	const struct of_device_id *match;
648 	struct qcom_pdm_data *data;
649 	int ret, i;
650 
651 	match = of_machine_get_match(qcom_pdm_domains);
652 	if (!match) {
653 		pr_notice("PDM: no support for the platform, userspace daemon might be required.\n");
654 		return ERR_PTR(-ENODEV);
655 	}
656 
657 	domains = match->data;
658 	if (!domains) {
659 		pr_debug("PDM: no domains\n");
660 		return ERR_PTR(-ENODEV);
661 	}
662 
663 	data = kzalloc_obj(*data);
664 	if (!data)
665 		return ERR_PTR(-ENOMEM);
666 
667 	INIT_LIST_HEAD(&data->services);
668 
669 	ret = qmi_handle_init(&data->handle, SERVREG_GET_DOMAIN_LIST_REQ_MAX_LEN,
670 			      NULL, qcom_pdm_msg_handlers);
671 	if (ret) {
672 		kfree(data);
673 		return ERR_PTR(ret);
674 	}
675 
676 	refcount_set(&data->refcnt, 1);
677 
678 	for (i = 0; domains[i]; i++) {
679 		ret = qcom_pdm_add_domain(data, domains[i]);
680 		if (ret)
681 			goto err_stop;
682 	}
683 
684 	ret = qmi_add_server(&data->handle, QMI_SERVICE_ID_SERVREG_LOC,
685 			     SERVREG_QMI_VERSION, SERVREG_QMI_INSTANCE);
686 	if (ret) {
687 		pr_err("PDM: error adding server %d\n", ret);
688 		goto err_stop;
689 	}
690 
691 	return data;
692 
693 err_stop:
694 	qcom_pdm_stop(data);
695 
696 	return ERR_PTR(ret);
697 }
698 
699 static int qcom_pdm_probe(struct auxiliary_device *auxdev,
700 			  const struct auxiliary_device_id *id)
701 
702 {
703 	struct qcom_pdm_data *data;
704 	int ret = 0;
705 
706 	mutex_lock(&qcom_pdm_mutex);
707 
708 	if (!__qcom_pdm_data) {
709 		data = qcom_pdm_start();
710 
711 		if (IS_ERR(data))
712 			ret = PTR_ERR(data);
713 		else
714 			__qcom_pdm_data = data;
715 	} else {
716 		refcount_inc(&__qcom_pdm_data->refcnt);
717 	}
718 
719 	auxiliary_set_drvdata(auxdev, __qcom_pdm_data);
720 
721 	mutex_unlock(&qcom_pdm_mutex);
722 
723 	return ret;
724 }
725 
726 static void qcom_pdm_remove(struct auxiliary_device *auxdev)
727 {
728 	struct qcom_pdm_data *data;
729 
730 	data = auxiliary_get_drvdata(auxdev);
731 	if (!data)
732 		return;
733 
734 	if (refcount_dec_and_mutex_lock(&data->refcnt, &qcom_pdm_mutex)) {
735 		__qcom_pdm_data = NULL;
736 		qcom_pdm_stop(data);
737 		mutex_unlock(&qcom_pdm_mutex);
738 	}
739 }
740 
741 static const struct auxiliary_device_id qcom_pdm_table[] = {
742 	{ .name = "qcom_common.pd-mapper" },
743 	{},
744 };
745 MODULE_DEVICE_TABLE(auxiliary, qcom_pdm_table);
746 
747 static struct auxiliary_driver qcom_pdm_drv = {
748 	.name = "qcom-pdm-mapper",
749 	.id_table = qcom_pdm_table,
750 	.probe = qcom_pdm_probe,
751 	.remove = qcom_pdm_remove,
752 };
753 module_auxiliary_driver(qcom_pdm_drv);
754 
755 MODULE_DESCRIPTION("Qualcomm Protection Domain Mapper");
756 MODULE_LICENSE("GPL");
757