blob: bcb5f9d98378c059bfd6f46e9f0205d92023cbd0 [file] [log] [blame]
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/module.h>
14#include <linux/slab.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053015#include <soc/qcom/scm.h>
16#include <soc/qcom/subsystem_notif.h>
17#include <soc/qcom/service-notifier.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053018#include <dsp/audio_notifier.h>
19#include "audio_ssr.h"
20#include "audio_pdr.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053021
22/* Audio states internal to notifier. Client */
23/* used states defined in audio_notifier.h */
24/* for AUDIO_NOTIFIER_SERVICE_DOWN & UP */
25#define NO_SERVICE -2
26#define UNINIT_SERVICE -1
27
28/*
29 * Used for each client registered with audio notifier
30 */
31struct client_data {
32 struct list_head list;
33 /* Notifier block given by client */
34 struct notifier_block *nb;
35 char client_name[20];
36 int service;
37 int domain;
38};
39
40/*
41 * Used for each service and domain combination
42 * Tracks information specific to the underlying
43 * service.
44 */
45struct service_info {
46 const char name[20];
47 int domain_id;
48 int state;
49 void *handle;
50 /* Notifier block registered to service */
51 struct notifier_block *nb;
52 /* Used to determine when to register and deregister service */
53 int num_of_clients;
54 /* List of all clients registered to the service and domain */
55 struct srcu_notifier_head client_nb_list;
56};
57
58static int audio_notifer_ssr_adsp_cb(struct notifier_block *this,
59 unsigned long opcode, void *data);
60static int audio_notifer_ssr_modem_cb(struct notifier_block *this,
61 unsigned long opcode, void *data);
62static int audio_notifer_pdr_adsp_cb(struct notifier_block *this,
63 unsigned long opcode, void *data);
64
65static struct notifier_block notifier_ssr_adsp_nb = {
66 .notifier_call = audio_notifer_ssr_adsp_cb,
67 .priority = 0,
68};
69
70static struct notifier_block notifier_ssr_modem_nb = {
71 .notifier_call = audio_notifer_ssr_modem_cb,
72 .priority = 0,
73};
74
75static struct notifier_block notifier_pdr_adsp_nb = {
76 .notifier_call = audio_notifer_pdr_adsp_cb,
77 .priority = 0,
78};
79
80static struct service_info service_data[AUDIO_NOTIFIER_MAX_SERVICES]
81 [AUDIO_NOTIFIER_MAX_DOMAINS] = {
82
83 {{
84 .name = "SSR_ADSP",
85 .domain_id = AUDIO_SSR_DOMAIN_ADSP,
86 .state = AUDIO_NOTIFIER_SERVICE_DOWN,
87 .nb = &notifier_ssr_adsp_nb
88 },
89 {
90 .name = "SSR_MODEM",
91 .domain_id = AUDIO_SSR_DOMAIN_MODEM,
92 .state = AUDIO_NOTIFIER_SERVICE_DOWN,
93 .nb = &notifier_ssr_modem_nb
94 } },
95
96 {{
97 .name = "PDR_ADSP",
98 .domain_id = AUDIO_PDR_DOMAIN_ADSP,
99 .state = UNINIT_SERVICE,
100 .nb = &notifier_pdr_adsp_nb
101 },
102 { /* PDR MODEM service not enabled */
103 .name = "INVALID",
104 .state = NO_SERVICE,
105 .nb = NULL
106 } }
107};
108
109/* Master list of all audio notifier clients */
110struct list_head client_list;
111struct mutex notifier_mutex;
112
113static int audio_notifer_get_default_service(int domain)
114{
115 int service = NO_SERVICE;
116
117 /* initial service to connect per domain */
118 switch (domain) {
119 case AUDIO_NOTIFIER_ADSP_DOMAIN:
120 service = AUDIO_NOTIFIER_PDR_SERVICE;
121 break;
122 case AUDIO_NOTIFIER_MODEM_DOMAIN:
123 service = AUDIO_NOTIFIER_SSR_SERVICE;
124 break;
125 }
126
127 return service;
128}
129
130static void audio_notifer_disable_service(int service)
131{
132 int i;
133
134 for (i = 0; i < AUDIO_NOTIFIER_MAX_DOMAINS; i++)
135 service_data[service][i].state = NO_SERVICE;
136}
137
138static bool audio_notifer_is_service_enabled(int service)
139{
140 int i;
141
142 for (i = 0; i < AUDIO_NOTIFIER_MAX_DOMAINS; i++)
143 if (service_data[service][i].state != NO_SERVICE)
144 return true;
145 return false;
146}
147
148static void audio_notifer_init_service(int service)
149{
150 int i;
151
152 for (i = 0; i < AUDIO_NOTIFIER_MAX_DOMAINS; i++) {
153 if (service_data[service][i].state == UNINIT_SERVICE)
154 service_data[service][i].state =
155 AUDIO_NOTIFIER_SERVICE_DOWN;
156 }
157}
158
159static int audio_notifer_reg_service(int service, int domain)
160{
161 void *handle;
162 int ret = 0;
163 int curr_state = AUDIO_NOTIFIER_SERVICE_DOWN;
164
165 switch (service) {
166 case AUDIO_NOTIFIER_SSR_SERVICE:
167 handle = audio_ssr_register(
168 service_data[service][domain].domain_id,
169 service_data[service][domain].nb);
170 break;
171 case AUDIO_NOTIFIER_PDR_SERVICE:
172 handle = audio_pdr_service_register(
173 service_data[service][domain].domain_id,
174 service_data[service][domain].nb, &curr_state);
175
176 if (curr_state == SERVREG_NOTIF_SERVICE_STATE_UP_V01)
177 curr_state = AUDIO_NOTIFIER_SERVICE_UP;
178 else
179 curr_state = AUDIO_NOTIFIER_SERVICE_DOWN;
180 break;
181 default:
182 pr_err("%s: Invalid service %d\n",
183 __func__, service);
184 ret = -EINVAL;
185 goto done;
186 }
187 if (IS_ERR_OR_NULL(handle)) {
188 pr_err("%s: handle is incorrect for service %s\n",
189 __func__, service_data[service][domain].name);
190 ret = -EINVAL;
191 goto done;
192 }
193 service_data[service][domain].state = curr_state;
194 service_data[service][domain].handle = handle;
195
196 pr_info("%s: service %s is in use\n",
197 __func__, service_data[service][domain].name);
198 pr_debug("%s: service %s has current state %d, handle 0x%pK\n",
199 __func__, service_data[service][domain].name,
200 service_data[service][domain].state,
201 service_data[service][domain].handle);
202done:
203 return ret;
204}
205
206static int audio_notifer_dereg_service(int service, int domain)
207{
208 int ret;
209
210 switch (service) {
211 case AUDIO_NOTIFIER_SSR_SERVICE:
212 ret = audio_ssr_deregister(
213 service_data[service][domain].handle,
214 service_data[service][domain].nb);
215 break;
216 case AUDIO_NOTIFIER_PDR_SERVICE:
217 ret = audio_pdr_service_deregister(
218 service_data[service][domain].handle,
219 service_data[service][domain].nb);
220 break;
221 default:
222 pr_err("%s: Invalid service %d\n",
223 __func__, service);
224 ret = -EINVAL;
225 goto done;
226 }
227 if (ret < 0) {
228 pr_err("%s: deregister failed for service %s, ret %d\n",
229 __func__, service_data[service][domain].name, ret);
230 goto done;
231 }
232
233 pr_debug("%s: service %s with handle 0x%pK deregistered\n",
234 __func__, service_data[service][domain].name,
235 service_data[service][domain].handle);
236
237 service_data[service][domain].state = AUDIO_NOTIFIER_SERVICE_DOWN;
238 service_data[service][domain].handle = NULL;
239done:
240 return ret;
241}
242
243static int audio_notifer_reg_client_service(struct client_data *client_data,
244 int service)
245{
246 int ret = 0;
247 int domain = client_data->domain;
248 struct audio_notifier_cb_data data;
249
250 switch (service) {
251 case AUDIO_NOTIFIER_SSR_SERVICE:
252 case AUDIO_NOTIFIER_PDR_SERVICE:
253 if (service_data[service][domain].num_of_clients == 0)
254 ret = audio_notifer_reg_service(service, domain);
255 break;
256 default:
257 pr_err("%s: Invalid service for client %s, service %d, domain %d\n",
258 __func__, client_data->client_name, service, domain);
259 ret = -EINVAL;
260 goto done;
261 }
262
263 if (ret < 0) {
264 pr_err("%s: service registration failed on service %s for client %s\n",
265 __func__, service_data[service][domain].name,
266 client_data->client_name);
267 goto done;
268 }
269
270 client_data->service = service;
271 srcu_notifier_chain_register(
272 &service_data[service][domain].client_nb_list,
273 client_data->nb);
274 service_data[service][domain].num_of_clients++;
275
276 pr_debug("%s: registered client %s on service %s, current state 0x%x\n",
277 __func__, client_data->client_name,
278 service_data[service][domain].name,
279 service_data[service][domain].state);
280
281 /*
282 * PDR registration returns current state
283 * Force callback of client with current state for PDR
284 */
285 if (client_data->service == AUDIO_NOTIFIER_PDR_SERVICE) {
286 data.service = service;
287 data.domain = domain;
288 (void)client_data->nb->notifier_call(client_data->nb,
289 service_data[service][domain].state, &data);
290 }
291done:
292 return ret;
293}
294
295static int audio_notifer_reg_client(struct client_data *client_data)
296{
297 int ret = 0;
298 int service;
299 int domain = client_data->domain;
300
301 service = audio_notifer_get_default_service(domain);
302 if (service < 0) {
303 pr_err("%s: service %d is incorrect\n", __func__, service);
304 ret = -EINVAL;
305 goto done;
306 }
307
308 /* Search through services to find a valid one to register client on. */
309 for (; service >= 0; service--) {
310 /* If a service is not initialized, wait for it to come up. */
311 if (service_data[service][domain].state == UNINIT_SERVICE)
312 goto done;
313 /* Skip unsupported service and domain combinations. */
314 if (service_data[service][domain].state < 0)
315 continue;
316 /* Only register clients who have not acquired a service. */
317 if (client_data->service != NO_SERVICE)
318 continue;
319
320 /*
321 * Only register clients, who have not acquired a service, on
322 * the best available service for their domain. Uninitialized
323 * services will try to register all of their clients after
324 * they initialize correctly or will disable their service and
325 * register clients on the next best avaialable service.
326 */
327 pr_debug("%s: register client %s on service %s",
328 __func__, client_data->client_name,
329 service_data[service][domain].name);
330
331 ret = audio_notifer_reg_client_service(client_data, service);
332 if (ret < 0)
333 pr_err("%s: client %s failed to register on service %s",
334 __func__, client_data->client_name,
335 service_data[service][domain].name);
336 }
337
338done:
339 return ret;
340}
341
342static int audio_notifer_dereg_client(struct client_data *client_data)
343{
344 int ret = 0;
345 int service = client_data->service;
346 int domain = client_data->domain;
347
348 switch (client_data->service) {
349 case AUDIO_NOTIFIER_SSR_SERVICE:
350 case AUDIO_NOTIFIER_PDR_SERVICE:
351 if (service_data[service][domain].num_of_clients == 1)
352 ret = audio_notifer_dereg_service(service, domain);
353 break;
354 case NO_SERVICE:
355 goto done;
356 default:
357 pr_err("%s: Invalid service for client %s, service %d\n",
358 __func__, client_data->client_name,
359 client_data->service);
360 ret = -EINVAL;
361 goto done;
362 }
363
364 if (ret < 0) {
365 pr_err("%s: deregister failed for client %s on service %s, ret %d\n",
366 __func__, client_data->client_name,
367 service_data[service][domain].name, ret);
368 goto done;
369 }
370
371 ret = srcu_notifier_chain_unregister(&service_data[service][domain].
372 client_nb_list, client_data->nb);
373 if (ret < 0) {
374 pr_err("%s: srcu_notifier_chain_unregister failed, ret %d\n",
375 __func__, ret);
376 goto done;
377 }
378
379 pr_debug("%s: deregistered client %s on service %s\n",
380 __func__, client_data->client_name,
381 service_data[service][domain].name);
382
383 client_data->service = NO_SERVICE;
384 if (service_data[service][domain].num_of_clients > 0)
385 service_data[service][domain].num_of_clients--;
386done:
387 return ret;
388}
389
390static void audio_notifer_reg_all_clients(void)
391{
392 struct list_head *ptr, *next;
393 struct client_data *client_data;
394 int ret;
395
396 list_for_each_safe(ptr, next, &client_list) {
397 client_data = list_entry(ptr, struct client_data, list);
398
399 ret = audio_notifer_reg_client(client_data);
400 if (ret < 0)
401 pr_err("%s: audio_notifer_reg_client failed for client %s, ret %d\n",
402 __func__, client_data->client_name,
403 ret);
404 }
405}
406
407static int audio_notifer_pdr_callback(struct notifier_block *this,
408 unsigned long opcode, void *data)
409{
410 pr_debug("%s: Audio PDR framework state 0x%lx\n",
411 __func__, opcode);
412 mutex_lock(&notifier_mutex);
413 if (opcode == AUDIO_PDR_FRAMEWORK_DOWN)
414 audio_notifer_disable_service(AUDIO_NOTIFIER_PDR_SERVICE);
415 else
416 audio_notifer_init_service(AUDIO_NOTIFIER_PDR_SERVICE);
417
418 audio_notifer_reg_all_clients();
419 mutex_unlock(&notifier_mutex);
420 return 0;
421}
422
423static struct notifier_block pdr_nb = {
424 .notifier_call = audio_notifer_pdr_callback,
425 .priority = 0,
426};
427
428static int audio_notifer_convert_opcode(unsigned long opcode,
429 unsigned long *notifier_opcode)
430{
431 int ret = 0;
432
433 switch (opcode) {
434 case SUBSYS_BEFORE_SHUTDOWN:
435 case SERVREG_NOTIF_SERVICE_STATE_DOWN_V01:
436 *notifier_opcode = AUDIO_NOTIFIER_SERVICE_DOWN;
437 break;
438 case SUBSYS_AFTER_POWERUP:
439 case SERVREG_NOTIF_SERVICE_STATE_UP_V01:
440 *notifier_opcode = AUDIO_NOTIFIER_SERVICE_UP;
441 break;
442 default:
443 pr_debug("%s: Unused opcode 0x%lx\n", __func__, opcode);
444 ret = -EINVAL;
445 }
446
447 return ret;
448}
449
450static int audio_notifer_service_cb(unsigned long opcode,
451 int service, int domain)
452{
453 int ret = 0;
454 unsigned long notifier_opcode;
455 struct audio_notifier_cb_data data;
456
457 if (audio_notifer_convert_opcode(opcode, &notifier_opcode) < 0)
458 goto done;
459
460 data.service = service;
461 data.domain = domain;
462
463 pr_debug("%s: service %s, opcode 0x%lx\n",
464 __func__, service_data[service][domain].name, notifier_opcode);
465
466 mutex_lock(&notifier_mutex);
467
468 service_data[service][domain].state = notifier_opcode;
469 ret = srcu_notifier_call_chain(&service_data[service][domain].
470 client_nb_list, notifier_opcode, &data);
471 if (ret < 0)
472 pr_err("%s: srcu_notifier_call_chain returned %d, service %s, opcode 0x%lx\n",
473 __func__, ret, service_data[service][domain].name,
474 notifier_opcode);
475
476 mutex_unlock(&notifier_mutex);
477done:
478 return NOTIFY_OK;
479}
480
481static int audio_notifer_pdr_adsp_cb(struct notifier_block *this,
482 unsigned long opcode, void *data)
483{
484 return audio_notifer_service_cb(opcode,
485 AUDIO_NOTIFIER_PDR_SERVICE,
486 AUDIO_NOTIFIER_ADSP_DOMAIN);
487}
488
489static int audio_notifer_ssr_adsp_cb(struct notifier_block *this,
490 unsigned long opcode, void *data)
491{
492 if (opcode == SUBSYS_BEFORE_SHUTDOWN)
493 audio_ssr_send_nmi(data);
494
495 return audio_notifer_service_cb(opcode,
496 AUDIO_NOTIFIER_SSR_SERVICE,
497 AUDIO_NOTIFIER_ADSP_DOMAIN);
498}
499
500static int audio_notifer_ssr_modem_cb(struct notifier_block *this,
501 unsigned long opcode, void *data)
502{
503 return audio_notifer_service_cb(opcode,
504 AUDIO_NOTIFIER_SSR_SERVICE,
505 AUDIO_NOTIFIER_MODEM_DOMAIN);
506}
507
508int audio_notifier_deregister(char *client_name)
509{
510 int ret = 0;
511 int ret2;
512 struct list_head *ptr, *next;
513 struct client_data *client_data;
514
515 if (client_name == NULL) {
516 pr_err("%s: client_name is NULL\n", __func__);
517 ret = -EINVAL;
518 goto done;
519 }
520 mutex_lock(&notifier_mutex);
521 list_for_each_safe(ptr, next, &client_list) {
522 client_data = list_entry(ptr, struct client_data, list);
523 if (!strcmp(client_name, client_data->client_name)) {
524 ret2 = audio_notifer_dereg_client(client_data);
525 if (ret2 < 0) {
526 pr_err("%s: audio_notifer_dereg_client failed, ret %d\n, service %d, domain %d",
527 __func__, ret2, client_data->service,
528 client_data->domain);
529 ret = ret2;
530 continue;
531 }
532 list_del(&client_data->list);
533 kfree(client_data);
534 }
535 }
536 mutex_unlock(&notifier_mutex);
537done:
538 return ret;
539}
540EXPORT_SYMBOL(audio_notifier_deregister);
541
542int audio_notifier_register(char *client_name, int domain,
543 struct notifier_block *nb)
544{
545 int ret;
546 struct client_data *client_data;
547
548 if (client_name == NULL) {
549 pr_err("%s: client_name is NULL\n", __func__);
550 ret = -EINVAL;
551 goto done;
552 } else if (nb == NULL) {
553 pr_err("%s: Notifier block is NULL\n", __func__);
554 ret = -EINVAL;
555 goto done;
556 }
557
558 client_data = kmalloc(sizeof(*client_data), GFP_KERNEL);
559 if (client_data == NULL) {
560 ret = -ENOMEM;
561 goto done;
562 }
563 INIT_LIST_HEAD(&client_data->list);
564 client_data->nb = nb;
565 strlcpy(client_data->client_name, client_name,
566 sizeof(client_data->client_name));
567 client_data->service = NO_SERVICE;
568 client_data->domain = domain;
569
570 mutex_lock(&notifier_mutex);
571 ret = audio_notifer_reg_client(client_data);
572 if (ret < 0) {
573 mutex_unlock(&notifier_mutex);
574 pr_err("%s: audio_notifer_reg_client for client %s failed ret = %d\n",
575 __func__, client_data->client_name,
576 ret);
577 kfree(client_data);
578 goto done;
579 }
580 list_add_tail(&client_data->list, &client_list);
581 mutex_unlock(&notifier_mutex);
582done:
583 return ret;
584}
585EXPORT_SYMBOL(audio_notifier_register);
586
587static int __init audio_notifier_subsys_init(void)
588{
589 int i, j;
590
591 mutex_init(&notifier_mutex);
592 INIT_LIST_HEAD(&client_list);
593 for (i = 0; i < AUDIO_NOTIFIER_MAX_SERVICES; i++) {
594 for (j = 0; j < AUDIO_NOTIFIER_MAX_DOMAINS; j++) {
595 if (service_data[i][j].state <= NO_SERVICE)
596 continue;
597
598 srcu_init_notifier_head(
599 &service_data[i][j].client_nb_list);
600 }
601 }
602
603 return 0;
604}
605subsys_initcall(audio_notifier_subsys_init);
606
607static int __init audio_notifier_init(void)
608{
609 int ret;
610
611 ret = audio_pdr_register(&pdr_nb);
612 if (ret < 0) {
613 pr_debug("%s: PDR register failed, ret = %d, disable service\n",
614 __func__, ret);
615 audio_notifer_disable_service(AUDIO_NOTIFIER_PDR_SERVICE);
616 }
617
618 /* Do not return error since PDR enablement is not critical */
619 return 0;
620}
621module_init(audio_notifier_init);
622
623static int __init audio_notifier_late_init(void)
624{
625 /*
626 * If pdr registration failed, register clients on next service
627 * Do in late init to ensure that SSR subsystem is initialized
628 */
629 mutex_lock(&notifier_mutex);
630 if (!audio_notifer_is_service_enabled(AUDIO_NOTIFIER_PDR_SERVICE))
631 audio_notifer_reg_all_clients();
632
633 mutex_unlock(&notifier_mutex);
634 return 0;
635}
636late_initcall(audio_notifier_late_init);