blob: f1f3e4913c1073dafb1c4a5c2a8db4df60e2302c [file] [log] [blame]
Karthikeyan Mani84287252019-01-15 16:36:08 -08001/* Copyright (c) 2010-2014, 2016-2019 The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302 *
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/kernel.h>
14#include <linux/module.h>
15#include <linux/types.h>
16#include <linux/uaccess.h>
17#include <linux/spinlock.h>
18#include <linux/list.h>
19#include <linux/sched.h>
20#include <linux/wait.h>
21#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/delay.h>
24#include <linux/debugfs.h>
25#include <linux/platform_device.h>
26#include <linux/sysfs.h>
27#include <linux/device.h>
Laxminath Kasam31b26c52018-02-12 16:32:01 +053028#include <linux/of.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053029#include <linux/slab.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053030#include <linux/ipc_logging.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053031#include <soc/qcom/subsystem_restart.h>
32#include <soc/qcom/scm.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053033#include <dsp/apr_audio-v2.h>
34#include <dsp/audio_notifier.h>
35#include <ipc/apr.h>
36#include <ipc/apr_tal.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053037
38#define APR_PKT_IPC_LOG_PAGE_CNT 2
39
Laxminath Kasam31b26c52018-02-12 16:32:01 +053040static struct device *apr_dev_ptr;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053041static struct apr_q6 q6;
42static struct apr_client client[APR_DEST_MAX][APR_CLIENT_MAX];
43static void *apr_pkt_ctx;
44static wait_queue_head_t dsp_wait;
45static wait_queue_head_t modem_wait;
46static bool is_modem_up;
47static bool is_initial_boot;
Laxminath Kasam31b26c52018-02-12 16:32:01 +053048static bool is_child_devices_loaded;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053049/* Subsystem restart: QDSP6 data, functions */
50static struct workqueue_struct *apr_reset_workqueue;
51static void apr_reset_deregister(struct work_struct *work);
52static void dispatch_event(unsigned long code, uint16_t proc);
53struct apr_reset_work {
54 void *handle;
55 struct work_struct work;
56};
57
58static bool apr_cf_debug;
59
60#ifdef CONFIG_DEBUG_FS
61static struct dentry *debugfs_apr_debug;
62static ssize_t apr_debug_write(struct file *filp, const char __user *ubuf,
63 size_t cnt, loff_t *ppos)
64{
65 char cmd;
66
67 if (copy_from_user(&cmd, ubuf, 1))
68 return -EFAULT;
69
70 apr_cf_debug = (cmd == '1') ? true : false;
71
72 return cnt;
73}
74
75static const struct file_operations apr_debug_ops = {
76 .write = apr_debug_write,
77};
78#endif
79
80#define APR_PKT_INFO(x...) \
81do { \
82 if (apr_pkt_ctx) \
83 ipc_log_string(apr_pkt_ctx, "<APR>: "x); \
84} while (0)
85
86
87struct apr_svc_table {
88 char name[64];
89 int idx;
90 int id;
91 int client_id;
92};
93
94static const struct apr_svc_table svc_tbl_qdsp6[] = {
95 {
96 .name = "AFE",
97 .idx = 0,
98 .id = APR_SVC_AFE,
99 .client_id = APR_CLIENT_AUDIO,
100 },
101 {
102 .name = "ASM",
103 .idx = 1,
104 .id = APR_SVC_ASM,
105 .client_id = APR_CLIENT_AUDIO,
106 },
107 {
108 .name = "ADM",
109 .idx = 2,
110 .id = APR_SVC_ADM,
111 .client_id = APR_CLIENT_AUDIO,
112 },
113 {
114 .name = "CORE",
115 .idx = 3,
116 .id = APR_SVC_ADSP_CORE,
117 .client_id = APR_CLIENT_AUDIO,
118 },
119 {
120 .name = "TEST",
121 .idx = 4,
122 .id = APR_SVC_TEST_CLIENT,
123 .client_id = APR_CLIENT_AUDIO,
124 },
125 {
126 .name = "MVM",
127 .idx = 5,
128 .id = APR_SVC_ADSP_MVM,
129 .client_id = APR_CLIENT_AUDIO,
130 },
131 {
132 .name = "CVS",
133 .idx = 6,
134 .id = APR_SVC_ADSP_CVS,
135 .client_id = APR_CLIENT_AUDIO,
136 },
137 {
138 .name = "CVP",
139 .idx = 7,
140 .id = APR_SVC_ADSP_CVP,
141 .client_id = APR_CLIENT_AUDIO,
142 },
143 {
144 .name = "USM",
145 .idx = 8,
146 .id = APR_SVC_USM,
147 .client_id = APR_CLIENT_AUDIO,
148 },
149 {
150 .name = "VIDC",
151 .idx = 9,
152 .id = APR_SVC_VIDC,
153 },
154 {
155 .name = "LSM",
156 .idx = 10,
157 .id = APR_SVC_LSM,
158 .client_id = APR_CLIENT_AUDIO,
159 },
160};
161
162static struct apr_svc_table svc_tbl_voice[] = {
163 {
164 .name = "VSM",
165 .idx = 0,
166 .id = APR_SVC_VSM,
167 .client_id = APR_CLIENT_VOICE,
168 },
169 {
170 .name = "VPM",
171 .idx = 1,
172 .id = APR_SVC_VPM,
173 .client_id = APR_CLIENT_VOICE,
174 },
175 {
176 .name = "MVS",
177 .idx = 2,
178 .id = APR_SVC_MVS,
179 .client_id = APR_CLIENT_VOICE,
180 },
181 {
182 .name = "MVM",
183 .idx = 3,
184 .id = APR_SVC_MVM,
185 .client_id = APR_CLIENT_VOICE,
186 },
187 {
188 .name = "CVS",
189 .idx = 4,
190 .id = APR_SVC_CVS,
191 .client_id = APR_CLIENT_VOICE,
192 },
193 {
194 .name = "CVP",
195 .idx = 5,
196 .id = APR_SVC_CVP,
197 .client_id = APR_CLIENT_VOICE,
198 },
199 {
200 .name = "SRD",
201 .idx = 6,
202 .id = APR_SVC_SRD,
203 .client_id = APR_CLIENT_VOICE,
204 },
205 {
206 .name = "TEST",
207 .idx = 7,
208 .id = APR_SVC_TEST_CLIENT,
209 .client_id = APR_CLIENT_VOICE,
210 },
211};
212
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530213/**
214 * apr_get_modem_state:
215 *
216 * Returns current modem load status
217 *
218 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530219enum apr_subsys_state apr_get_modem_state(void)
220{
221 return atomic_read(&q6.modem_state);
222}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530223EXPORT_SYMBOL(apr_get_modem_state);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530224
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530225/**
226 * apr_set_modem_state - Update modem load status.
227 *
228 * @state: State to update modem load status
229 *
230 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530231void apr_set_modem_state(enum apr_subsys_state state)
232{
233 atomic_set(&q6.modem_state, state);
234}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530235EXPORT_SYMBOL(apr_set_modem_state);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530236
237enum apr_subsys_state apr_cmpxchg_modem_state(enum apr_subsys_state prev,
238 enum apr_subsys_state new)
239{
240 return atomic_cmpxchg(&q6.modem_state, prev, new);
241}
242
243static void apr_modem_down(unsigned long opcode)
244{
245 apr_set_modem_state(APR_SUBSYS_DOWN);
246 dispatch_event(opcode, APR_DEST_MODEM);
247}
248
249static void apr_modem_up(void)
250{
251 if (apr_cmpxchg_modem_state(APR_SUBSYS_DOWN, APR_SUBSYS_UP) ==
252 APR_SUBSYS_DOWN)
253 wake_up(&modem_wait);
254 is_modem_up = 1;
255}
256
257enum apr_subsys_state apr_get_q6_state(void)
258{
259 return atomic_read(&q6.q6_state);
260}
261EXPORT_SYMBOL(apr_get_q6_state);
262
263int apr_set_q6_state(enum apr_subsys_state state)
264{
265 pr_debug("%s: setting adsp state %d\n", __func__, state);
266 if (state < APR_SUBSYS_DOWN || state > APR_SUBSYS_LOADED)
267 return -EINVAL;
268 atomic_set(&q6.q6_state, state);
269 return 0;
270}
271EXPORT_SYMBOL(apr_set_q6_state);
272
273enum apr_subsys_state apr_cmpxchg_q6_state(enum apr_subsys_state prev,
274 enum apr_subsys_state new)
275{
276 return atomic_cmpxchg(&q6.q6_state, prev, new);
277}
278
279static void apr_adsp_down(unsigned long opcode)
280{
281 apr_set_q6_state(APR_SUBSYS_DOWN);
282 dispatch_event(opcode, APR_DEST_QDSP6);
283}
284
285static void apr_adsp_up(void)
286{
287 if (apr_cmpxchg_q6_state(APR_SUBSYS_DOWN, APR_SUBSYS_LOADED) ==
288 APR_SUBSYS_DOWN)
289 wake_up(&dsp_wait);
Laxminath Kasam31b26c52018-02-12 16:32:01 +0530290
291 if (!is_child_devices_loaded) {
292 struct platform_device *pdev;
293 struct device_node *node;
294 int ret;
295
296 for_each_child_of_node(apr_dev_ptr->of_node, node) {
297 pdev = platform_device_alloc(node->name, -1);
298 if (!pdev) {
299 dev_err(apr_dev_ptr, "%s: pdev memory alloc failed\n",
300 __func__);
301 return;
302 }
303 pdev->dev.parent = apr_dev_ptr;
304 pdev->dev.of_node = node;
305
306 ret = platform_device_add(pdev);
307 if (ret) {
308 dev_err(apr_dev_ptr,
309 "%s: Cannot add platform device\n",
310 __func__);
311 platform_device_put(pdev);
312 return;
313 }
314 }
315 is_child_devices_loaded = true;
316 }
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530317}
318
319int apr_wait_for_device_up(int dest_id)
320{
321 int rc = -1;
322
323 if (dest_id == APR_DEST_MODEM)
324 rc = wait_event_interruptible_timeout(modem_wait,
325 (apr_get_modem_state() == APR_SUBSYS_UP),
326 (1 * HZ));
327 else if (dest_id == APR_DEST_QDSP6)
328 rc = wait_event_interruptible_timeout(dsp_wait,
329 (apr_get_q6_state() == APR_SUBSYS_UP),
330 (1 * HZ));
331 else
332 pr_err("%s: unknown dest_id %d\n", __func__, dest_id);
333 /* returns left time */
334 return rc;
335}
336
337int apr_load_adsp_image(void)
338{
339 int rc = 0;
340
341 mutex_lock(&q6.lock);
342 if (apr_get_q6_state() == APR_SUBSYS_UP) {
343 q6.pil = subsystem_get("adsp");
344 if (IS_ERR(q6.pil)) {
345 rc = PTR_ERR(q6.pil);
346 pr_err("APR: Unable to load q6 image, error:%d\n", rc);
347 } else {
348 apr_set_q6_state(APR_SUBSYS_LOADED);
349 pr_debug("APR: Image is loaded, stated\n");
350 }
351 } else if (apr_get_q6_state() == APR_SUBSYS_LOADED) {
352 pr_debug("APR: q6 image already loaded\n");
353 } else {
354 pr_debug("APR: cannot load state %d\n", apr_get_q6_state());
355 }
356 mutex_unlock(&q6.lock);
357 return rc;
358}
359
360struct apr_client *apr_get_client(int dest_id, int client_id)
361{
362 return &client[dest_id][client_id];
363}
364
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530365/**
366 * apr_send_pkt - Clients call to send packet
367 * to destination processor.
368 *
369 * @handle: APR service handle
370 * @buf: payload to send to destination processor.
371 *
372 * Returns Bytes(>0)pkt_size on success or error on failure.
373 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530374int apr_send_pkt(void *handle, uint32_t *buf)
375{
376 struct apr_svc *svc = handle;
377 struct apr_client *clnt;
378 struct apr_hdr *hdr;
379 uint16_t dest_id;
380 uint16_t client_id;
381 uint16_t w_len;
382 int rc;
383 unsigned long flags;
384
385 if (!handle || !buf) {
386 pr_err("APR: Wrong parameters\n");
387 return -EINVAL;
388 }
389 if (svc->need_reset) {
Laxminath Kasamc1880702018-04-04 10:59:57 +0530390 pr_err_ratelimited("apr: send_pkt service need reset\n");
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530391 return -ENETRESET;
392 }
393
394 if ((svc->dest_id == APR_DEST_QDSP6) &&
395 (apr_get_q6_state() != APR_SUBSYS_LOADED)) {
Laxminath Kasamc1880702018-04-04 10:59:57 +0530396 pr_err_ratelimited("%s: Still dsp is not Up\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530397 return -ENETRESET;
398 } else if ((svc->dest_id == APR_DEST_MODEM) &&
399 (apr_get_modem_state() == APR_SUBSYS_DOWN)) {
400 pr_err("apr: Still Modem is not Up\n");
401 return -ENETRESET;
402 }
403
404 spin_lock_irqsave(&svc->w_lock, flags);
405 dest_id = svc->dest_id;
406 client_id = svc->client_id;
407 clnt = &client[dest_id][client_id];
408
409 if (!client[dest_id][client_id].handle) {
Laxminath Kasamc1880702018-04-04 10:59:57 +0530410 pr_err_ratelimited("APR: Still service is not yet opened\n");
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530411 spin_unlock_irqrestore(&svc->w_lock, flags);
412 return -EINVAL;
413 }
414 hdr = (struct apr_hdr *)buf;
415
416 hdr->src_domain = APR_DOMAIN_APPS;
417 hdr->src_svc = svc->id;
418 hdr->dest_domain = svc->dest_domain;
419 hdr->dest_svc = svc->id;
420
421 if (unlikely(apr_cf_debug)) {
422 APR_PKT_INFO(
423 "Tx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X]",
424 (hdr->src_domain << 8) | hdr->src_svc,
425 (hdr->dest_domain << 8) | hdr->dest_svc, hdr->opcode,
426 hdr->token);
427 }
428
429 rc = apr_tal_write(clnt->handle, buf,
430 (struct apr_pkt_priv *)&svc->pkt_owner,
431 hdr->pkt_size);
432 if (rc >= 0) {
433 w_len = rc;
434 if (w_len != hdr->pkt_size) {
435 pr_err("%s: Unable to write whole APR pkt successfully: %d\n",
436 __func__, rc);
437 rc = -EINVAL;
438 }
439 } else {
440 pr_err("%s: Write APR pkt failed with error %d\n",
441 __func__, rc);
442 }
443 spin_unlock_irqrestore(&svc->w_lock, flags);
444
445 return rc;
446}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530447EXPORT_SYMBOL(apr_send_pkt);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530448
449int apr_pkt_config(void *handle, struct apr_pkt_cfg *cfg)
450{
451 struct apr_svc *svc = (struct apr_svc *)handle;
452 uint16_t dest_id;
453 uint16_t client_id;
454 struct apr_client *clnt;
455
456 if (!handle) {
457 pr_err("%s: Invalid handle\n", __func__);
458 return -EINVAL;
459 }
460
461 if (svc->need_reset) {
462 pr_err("%s: service need reset\n", __func__);
463 return -ENETRESET;
464 }
465
466 svc->pkt_owner = cfg->pkt_owner;
467 dest_id = svc->dest_id;
468 client_id = svc->client_id;
469 clnt = &client[dest_id][client_id];
470
471 return apr_tal_rx_intents_config(clnt->handle,
472 cfg->intents.num_of_intents, cfg->intents.size);
473}
474
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530475/**
476 * apr_register - Clients call to register
477 * to APR.
478 *
479 * @dest: destination processor
480 * @svc_name: name of service to register as
481 * @svc_fn: callback function to trigger when response
482 * ack or packets received from destination processor.
483 * @src_port: Port number within a service
484 * @priv: private data of client, passed back in cb fn.
485 *
486 * Returns apr_svc handle on success or NULL on failure.
487 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530488struct apr_svc *apr_register(char *dest, char *svc_name, apr_fn svc_fn,
489 uint32_t src_port, void *priv)
490{
491 struct apr_client *clnt;
492 int client_id = 0;
493 int svc_idx = 0;
494 int svc_id = 0;
495 int dest_id = 0;
496 int domain_id = 0;
497 int temp_port = 0;
498 struct apr_svc *svc = NULL;
499 int rc = 0;
500 bool can_open_channel = true;
501
502 if (!dest || !svc_name || !svc_fn)
503 return NULL;
504
505 if (!strcmp(dest, "ADSP"))
506 domain_id = APR_DOMAIN_ADSP;
507 else if (!strcmp(dest, "MODEM")) {
508 /* Don't request for SMD channels if destination is MODEM,
509 * as these channels are no longer used and these clients
510 * are to listen only for MODEM SSR events
511 */
512 can_open_channel = false;
513 domain_id = APR_DOMAIN_MODEM;
514 } else {
515 pr_err("APR: wrong destination\n");
516 goto done;
517 }
518
519 dest_id = apr_get_dest_id(dest);
520
521 if (dest_id == APR_DEST_QDSP6) {
522 if (apr_get_q6_state() != APR_SUBSYS_LOADED) {
Laxminath Kasamc1880702018-04-04 10:59:57 +0530523 pr_err_ratelimited("%s: adsp not up\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530524 return NULL;
525 }
526 pr_debug("%s: adsp Up\n", __func__);
527 } else if (dest_id == APR_DEST_MODEM) {
528 if (apr_get_modem_state() == APR_SUBSYS_DOWN) {
529 if (is_modem_up) {
530 pr_err("%s: modem shutdown due to SSR, ret",
531 __func__);
532 return NULL;
533 }
534 pr_debug("%s: Wait for modem to bootup\n", __func__);
535 rc = apr_wait_for_device_up(APR_DEST_MODEM);
536 if (rc == 0) {
537 pr_err("%s: Modem is not Up\n", __func__);
538 return NULL;
539 }
540 }
541 pr_debug("%s: modem Up\n", __func__);
542 }
543
544 if (apr_get_svc(svc_name, domain_id, &client_id, &svc_idx, &svc_id)) {
Laxminath Kasamc1880702018-04-04 10:59:57 +0530545 pr_err_ratelimited("%s: apr_get_svc failed\n", __func__);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530546 goto done;
547 }
548
549 clnt = &client[dest_id][client_id];
550 mutex_lock(&clnt->m_lock);
551 if (!clnt->handle && can_open_channel) {
552 clnt->handle = apr_tal_open(client_id, dest_id,
553 APR_DL_SMD, apr_cb_func, NULL);
554 if (!clnt->handle) {
555 svc = NULL;
Laxminath Kasamc1880702018-04-04 10:59:57 +0530556 pr_err_ratelimited("APR: Unable to open handle\n");
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530557 mutex_unlock(&clnt->m_lock);
558 goto done;
559 }
560 }
561 mutex_unlock(&clnt->m_lock);
562 svc = &clnt->svc[svc_idx];
563 mutex_lock(&svc->m_lock);
564 clnt->id = client_id;
565 if (svc->need_reset) {
566 mutex_unlock(&svc->m_lock);
Laxminath Kasamc1880702018-04-04 10:59:57 +0530567 pr_err_ratelimited("APR: Service needs reset\n");
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530568 goto done;
569 }
570 svc->id = svc_id;
571 svc->dest_id = dest_id;
572 svc->client_id = client_id;
573 svc->dest_domain = domain_id;
574 svc->pkt_owner = APR_PKT_OWNER_DRIVER;
575
576 if (src_port != 0xFFFFFFFF) {
577 temp_port = ((src_port >> 8) * 8) + (src_port & 0xFF);
578 pr_debug("port = %d t_port = %d\n", src_port, temp_port);
579 if (temp_port >= APR_MAX_PORTS || temp_port < 0) {
580 pr_err("APR: temp_port out of bounds\n");
581 mutex_unlock(&svc->m_lock);
582 return NULL;
583 }
584 if (!svc->svc_cnt)
585 clnt->svc_cnt++;
586 svc->port_cnt++;
587 svc->port_fn[temp_port] = svc_fn;
588 svc->port_priv[temp_port] = priv;
589 svc->svc_cnt++;
590 } else {
591 if (!svc->fn) {
592 if (!svc->svc_cnt)
593 clnt->svc_cnt++;
594 svc->fn = svc_fn;
595 svc->priv = priv;
596 svc->svc_cnt++;
597 }
598 }
599
600 mutex_unlock(&svc->m_lock);
601done:
602 return svc;
603}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530604EXPORT_SYMBOL(apr_register);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530605
606
607void apr_cb_func(void *buf, int len, void *priv)
608{
609 struct apr_client_data data;
610 struct apr_client *apr_client;
611 struct apr_svc *c_svc;
612 struct apr_hdr *hdr;
613 uint16_t hdr_size;
614 uint16_t msg_type;
615 uint16_t ver;
616 uint16_t src;
617 uint16_t svc;
618 uint16_t clnt;
619 int i;
620 int temp_port = 0;
621 uint32_t *ptr;
622
623 pr_debug("APR2: len = %d\n", len);
624 ptr = buf;
625 pr_debug("\n*****************\n");
626 for (i = 0; i < len/4; i++)
627 pr_debug("%x ", ptr[i]);
628 pr_debug("\n");
629 pr_debug("\n*****************\n");
630
631 if (!buf || len <= APR_HDR_SIZE) {
632 pr_err("APR: Improper apr pkt received:%pK %d\n", buf, len);
633 return;
634 }
635 hdr = buf;
636
637 ver = hdr->hdr_field;
638 ver = (ver & 0x000F);
639 if (ver > APR_PKT_VER + 1) {
640 pr_err("APR: Wrong version: %d\n", ver);
641 return;
642 }
643
644 hdr_size = hdr->hdr_field;
645 hdr_size = ((hdr_size & 0x00F0) >> 0x4) * 4;
646 if (hdr_size < APR_HDR_SIZE) {
647 pr_err("APR: Wrong hdr size:%d\n", hdr_size);
648 return;
649 }
650
651 if (hdr->pkt_size < APR_HDR_SIZE) {
652 pr_err("APR: Wrong paket size\n");
653 return;
654 }
Karthikeyan Mani84287252019-01-15 16:36:08 -0800655
656 if (hdr->pkt_size < hdr_size) {
657 pr_err("APR: Packet size less than header size\n");
658 return;
659 }
660
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530661 msg_type = hdr->hdr_field;
662 msg_type = (msg_type >> 0x08) & 0x0003;
663 if (msg_type >= APR_MSG_TYPE_MAX && msg_type != APR_BASIC_RSP_RESULT) {
664 pr_err("APR: Wrong message type: %d\n", msg_type);
665 return;
666 }
667
668 if (hdr->src_domain >= APR_DOMAIN_MAX ||
669 hdr->dest_domain >= APR_DOMAIN_MAX ||
670 hdr->src_svc >= APR_SVC_MAX ||
671 hdr->dest_svc >= APR_SVC_MAX) {
672 pr_err("APR: Wrong APR header\n");
673 return;
674 }
675
676 svc = hdr->dest_svc;
677 if (hdr->src_domain == APR_DOMAIN_MODEM) {
678 if (svc == APR_SVC_MVS || svc == APR_SVC_MVM ||
679 svc == APR_SVC_CVS || svc == APR_SVC_CVP ||
680 svc == APR_SVC_TEST_CLIENT)
681 clnt = APR_CLIENT_VOICE;
682 else {
683 pr_err("APR: Wrong svc :%d\n", svc);
684 return;
685 }
686 } else if (hdr->src_domain == APR_DOMAIN_ADSP) {
687 if (svc == APR_SVC_AFE || svc == APR_SVC_ASM ||
688 svc == APR_SVC_VSM || svc == APR_SVC_VPM ||
689 svc == APR_SVC_ADM || svc == APR_SVC_ADSP_CORE ||
690 svc == APR_SVC_USM ||
691 svc == APR_SVC_TEST_CLIENT || svc == APR_SVC_ADSP_MVM ||
692 svc == APR_SVC_ADSP_CVS || svc == APR_SVC_ADSP_CVP ||
693 svc == APR_SVC_LSM)
694 clnt = APR_CLIENT_AUDIO;
695 else if (svc == APR_SVC_VIDC)
696 clnt = APR_CLIENT_AUDIO;
697 else {
698 pr_err("APR: Wrong svc :%d\n", svc);
699 return;
700 }
701 } else {
702 pr_err("APR: Pkt from wrong source: %d\n", hdr->src_domain);
703 return;
704 }
705
706 src = apr_get_data_src(hdr);
707 if (src == APR_DEST_MAX)
708 return;
709
710 pr_debug("src =%d clnt = %d\n", src, clnt);
711 apr_client = &client[src][clnt];
712 for (i = 0; i < APR_SVC_MAX; i++)
713 if (apr_client->svc[i].id == svc) {
714 pr_debug("%d\n", apr_client->svc[i].id);
715 c_svc = &apr_client->svc[i];
716 break;
717 }
718
719 if (i == APR_SVC_MAX) {
720 pr_err("APR: service is not registered\n");
721 return;
722 }
723 pr_debug("svc_idx = %d\n", i);
724 pr_debug("%x %x %x %pK %pK\n", c_svc->id, c_svc->dest_id,
725 c_svc->client_id, c_svc->fn, c_svc->priv);
726 data.payload_size = hdr->pkt_size - hdr_size;
727 data.opcode = hdr->opcode;
728 data.src = src;
729 data.src_port = hdr->src_port;
730 data.dest_port = hdr->dest_port;
731 data.token = hdr->token;
732 data.msg_type = msg_type;
733 data.payload = NULL;
734 if (data.payload_size > 0)
735 data.payload = (char *)hdr + hdr_size;
736
737 if (unlikely(apr_cf_debug)) {
738 if (hdr->opcode == APR_BASIC_RSP_RESULT && data.payload) {
739 uint32_t *ptr = data.payload;
740
741 APR_PKT_INFO(
742 "Rx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X] rc[0x%X]",
743 (hdr->src_domain << 8) | hdr->src_svc,
744 (hdr->dest_domain << 8) | hdr->dest_svc,
745 hdr->opcode, hdr->token, ptr[1]);
746 } else {
747 APR_PKT_INFO(
748 "Rx: src_addr[0x%X] dest_addr[0x%X] opcode[0x%X] token[0x%X]",
749 (hdr->src_domain << 8) | hdr->src_svc,
750 (hdr->dest_domain << 8) | hdr->dest_svc, hdr->opcode,
751 hdr->token);
752 }
753 }
754
755 temp_port = ((data.dest_port >> 8) * 8) + (data.dest_port & 0xFF);
Aditya Bavanarid3047b22017-11-22 15:29:52 +0530756 if (((temp_port >= 0) && (temp_port < APR_MAX_PORTS))
757 && (c_svc->port_cnt && c_svc->port_fn[temp_port]))
758 c_svc->port_fn[temp_port](&data,
759 c_svc->port_priv[temp_port]);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530760 else if (c_svc->fn)
761 c_svc->fn(&data, c_svc->priv);
762 else
763 pr_err("APR: Rxed a packet for NULL callback\n");
764}
765
766int apr_get_svc(const char *svc_name, int domain_id, int *client_id,
767 int *svc_idx, int *svc_id)
768{
769 int i;
770 int size;
771 struct apr_svc_table *tbl;
772 int ret = 0;
773
774 if (domain_id == APR_DOMAIN_ADSP) {
775 tbl = (struct apr_svc_table *)&svc_tbl_qdsp6;
776 size = ARRAY_SIZE(svc_tbl_qdsp6);
777 } else {
778 tbl = (struct apr_svc_table *)&svc_tbl_voice;
779 size = ARRAY_SIZE(svc_tbl_voice);
780 }
781
782 for (i = 0; i < size; i++) {
783 if (!strcmp(svc_name, tbl[i].name)) {
784 *client_id = tbl[i].client_id;
785 *svc_idx = tbl[i].idx;
786 *svc_id = tbl[i].id;
787 break;
788 }
789 }
790
791 pr_debug("%s: svc_name = %s c_id = %d domain_id = %d\n",
792 __func__, svc_name, *client_id, domain_id);
793 if (i == size) {
794 pr_err("%s: APR: Wrong svc name %s\n", __func__, svc_name);
795 ret = -EINVAL;
796 }
797
798 return ret;
799}
800
801static void apr_reset_deregister(struct work_struct *work)
802{
803 struct apr_svc *handle = NULL;
804 struct apr_reset_work *apr_reset =
805 container_of(work, struct apr_reset_work, work);
806
807 handle = apr_reset->handle;
808 pr_debug("%s:handle[%pK]\n", __func__, handle);
809 apr_deregister(handle);
810 kfree(apr_reset);
811}
812
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530813/**
Banajit Goswami88327322017-12-08 10:51:58 -0800814 * apr_start_rx_rt - Clients call to vote for thread
815 * priority upgrade whenever needed.
816 *
817 * @handle: APR service handle
818 *
819 * Returns 0 on success or error otherwise.
820 */
821int apr_start_rx_rt(void *handle)
822{
823 int rc = 0;
824 struct apr_svc *svc = handle;
825 uint16_t dest_id = 0;
826 uint16_t client_id = 0;
827
828 if (!svc) {
829 pr_err("%s: Invalid APR handle\n", __func__);
830 return -EINVAL;
831 }
832
833 mutex_lock(&svc->m_lock);
834 dest_id = svc->dest_id;
835 client_id = svc->client_id;
836
837 if ((client_id >= APR_CLIENT_MAX) || (dest_id >= APR_DEST_MAX)) {
838 pr_err("%s: %s invalid. client_id = %u, dest_id = %u\n",
839 __func__,
840 client_id >= APR_CLIENT_MAX ? "Client ID" : "Dest ID",
841 client_id, dest_id);
842 rc = -EINVAL;
843 goto exit;
844 }
845
846 if (!client[dest_id][client_id].handle) {
847 pr_err("%s: Client handle is NULL\n", __func__);
848 rc = -EINVAL;
849 goto exit;
850 }
851
852 rc = apr_tal_start_rx_rt(client[dest_id][client_id].handle);
853 if (rc)
854 pr_err("%s: failed to set RT thread priority for APR RX. rc = %d\n",
855 __func__, rc);
856
857exit:
858 mutex_unlock(&svc->m_lock);
859 return rc;
860}
861EXPORT_SYMBOL(apr_start_rx_rt);
862
863/**
864 * apr_end_rx_rt - Clients call to unvote for thread
865 * priority upgrade (perviously voted with
866 * apr_start_rx_rt()).
867 *
868 * @handle: APR service handle
869 *
870 * Returns 0 on success or error otherwise.
871 */
872int apr_end_rx_rt(void *handle)
873{
874 int rc = 0;
875 struct apr_svc *svc = handle;
876 uint16_t dest_id = 0;
877 uint16_t client_id = 0;
878
879 if (!svc) {
880 pr_err("%s: Invalid APR handle\n", __func__);
881 return -EINVAL;
882 }
883
884 mutex_lock(&svc->m_lock);
885 dest_id = svc->dest_id;
886 client_id = svc->client_id;
887
888 if ((client_id >= APR_CLIENT_MAX) || (dest_id >= APR_DEST_MAX)) {
889 pr_err("%s: %s invalid. client_id = %u, dest_id = %u\n",
890 __func__,
891 client_id >= APR_CLIENT_MAX ? "Client ID" : "Dest ID",
892 client_id, dest_id);
893 rc = -EINVAL;
894 goto exit;
895 }
896
897 if (!client[dest_id][client_id].handle) {
898 pr_err("%s: Client handle is NULL\n", __func__);
899 rc = -EINVAL;
900 goto exit;
901 }
902
903 rc = apr_tal_end_rx_rt(client[dest_id][client_id].handle);
904 if (rc)
905 pr_err("%s: failed to reset RT thread priority for APR RX. rc = %d\n",
906 __func__, rc);
907
908exit:
909 mutex_unlock(&svc->m_lock);
910 return rc;
911}
912EXPORT_SYMBOL(apr_end_rx_rt);
913
914/**
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530915 * apr_deregister - Clients call to de-register
916 * from APR.
917 *
918 * @handle: APR service handle to de-register
919 *
920 * Returns 0 on success or -EINVAL on error.
921 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530922int apr_deregister(void *handle)
923{
924 struct apr_svc *svc = handle;
925 struct apr_client *clnt;
926 uint16_t dest_id;
927 uint16_t client_id;
928
929 if (!handle)
930 return -EINVAL;
931
932 mutex_lock(&svc->m_lock);
933 if (!svc->svc_cnt) {
934 pr_err("%s: svc already deregistered. svc = %pK\n",
935 __func__, svc);
936 mutex_unlock(&svc->m_lock);
937 return -EINVAL;
938 }
939
940 dest_id = svc->dest_id;
941 client_id = svc->client_id;
942 clnt = &client[dest_id][client_id];
943
944 if (svc->svc_cnt > 0) {
945 if (svc->port_cnt)
946 svc->port_cnt--;
947 svc->svc_cnt--;
948 if (!svc->svc_cnt) {
949 client[dest_id][client_id].svc_cnt--;
950 pr_debug("%s: service is reset %pK\n", __func__, svc);
951 }
952 }
953
954 if (!svc->svc_cnt) {
955 svc->priv = NULL;
956 svc->id = 0;
957 svc->fn = NULL;
958 svc->dest_id = 0;
959 svc->client_id = 0;
960 svc->need_reset = 0x0;
961 }
962 if (client[dest_id][client_id].handle &&
963 !client[dest_id][client_id].svc_cnt) {
964 apr_tal_close(client[dest_id][client_id].handle);
965 client[dest_id][client_id].handle = NULL;
966 }
967 mutex_unlock(&svc->m_lock);
968
969 return 0;
970}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530971EXPORT_SYMBOL(apr_deregister);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530972
Laxminath Kasam8b1366a2017-10-05 01:44:16 +0530973/**
974 * apr_reset - sets up workqueue to de-register
975 * the given APR service handle.
976 *
977 * @handle: APR service handle
978 *
979 */
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530980void apr_reset(void *handle)
981{
982 struct apr_reset_work *apr_reset_worker = NULL;
983
984 if (!handle)
985 return;
986 pr_debug("%s: handle[%pK]\n", __func__, handle);
987
988 if (apr_reset_workqueue == NULL) {
989 pr_err("%s: apr_reset_workqueue is NULL\n", __func__);
990 return;
991 }
992
993 apr_reset_worker = kzalloc(sizeof(struct apr_reset_work),
994 GFP_ATOMIC);
995
996 if (apr_reset_worker == NULL) {
997 pr_err("%s: mem failure\n", __func__);
998 return;
999 }
1000
1001 apr_reset_worker->handle = handle;
1002 INIT_WORK(&apr_reset_worker->work, apr_reset_deregister);
1003 queue_work(apr_reset_workqueue, &apr_reset_worker->work);
1004}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301005EXPORT_SYMBOL(apr_reset);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301006
1007/* Dispatch the Reset events to Modem and audio clients */
1008static void dispatch_event(unsigned long code, uint16_t proc)
1009{
1010 struct apr_client *apr_client;
1011 struct apr_client_data data;
1012 struct apr_svc *svc;
1013 uint16_t clnt;
1014 int i, j;
1015
Laxminath Kasam8f7ccc22017-08-28 17:35:04 +05301016 memset(&data, 0, sizeof(data));
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301017 data.opcode = RESET_EVENTS;
1018 data.reset_event = code;
1019
1020 /* Service domain can be different from the processor */
1021 data.reset_proc = apr_get_reset_domain(proc);
1022
1023 clnt = APR_CLIENT_AUDIO;
1024 apr_client = &client[proc][clnt];
1025 for (i = 0; i < APR_SVC_MAX; i++) {
1026 mutex_lock(&apr_client->svc[i].m_lock);
1027 if (apr_client->svc[i].fn) {
1028 apr_client->svc[i].need_reset = 0x1;
1029 apr_client->svc[i].fn(&data, apr_client->svc[i].priv);
1030 }
1031 if (apr_client->svc[i].port_cnt) {
1032 svc = &(apr_client->svc[i]);
1033 svc->need_reset = 0x1;
1034 for (j = 0; j < APR_MAX_PORTS; j++)
1035 if (svc->port_fn[j])
1036 svc->port_fn[j](&data,
1037 svc->port_priv[j]);
1038 }
1039 mutex_unlock(&apr_client->svc[i].m_lock);
1040 }
1041
1042 clnt = APR_CLIENT_VOICE;
1043 apr_client = &client[proc][clnt];
1044 for (i = 0; i < APR_SVC_MAX; i++) {
1045 mutex_lock(&apr_client->svc[i].m_lock);
1046 if (apr_client->svc[i].fn) {
1047 apr_client->svc[i].need_reset = 0x1;
1048 apr_client->svc[i].fn(&data, apr_client->svc[i].priv);
1049 }
1050 if (apr_client->svc[i].port_cnt) {
1051 svc = &(apr_client->svc[i]);
1052 svc->need_reset = 0x1;
1053 for (j = 0; j < APR_MAX_PORTS; j++)
1054 if (svc->port_fn[j])
1055 svc->port_fn[j](&data,
1056 svc->port_priv[j]);
1057 }
1058 mutex_unlock(&apr_client->svc[i].m_lock);
1059 }
1060}
1061
1062static int apr_notifier_service_cb(struct notifier_block *this,
1063 unsigned long opcode, void *data)
1064{
1065 struct audio_notifier_cb_data *cb_data = data;
1066
1067 if (cb_data == NULL) {
1068 pr_err("%s: Callback data is NULL!\n", __func__);
1069 goto done;
1070 }
1071
1072 pr_debug("%s: Service opcode 0x%lx, domain %d\n",
1073 __func__, opcode, cb_data->domain);
1074
1075 switch (opcode) {
1076 case AUDIO_NOTIFIER_SERVICE_DOWN:
1077 /*
1078 * Use flag to ignore down notifications during
1079 * initial boot. There is no benefit from error
1080 * recovery notifications during initial boot
1081 * up since everything is expected to be down.
1082 */
1083 if (is_initial_boot) {
1084 is_initial_boot = false;
1085 break;
1086 }
1087 if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
1088 apr_modem_down(opcode);
1089 else
1090 apr_adsp_down(opcode);
1091 break;
1092 case AUDIO_NOTIFIER_SERVICE_UP:
1093 is_initial_boot = false;
1094 if (cb_data->domain == AUDIO_NOTIFIER_MODEM_DOMAIN)
1095 apr_modem_up();
1096 else
1097 apr_adsp_up();
1098 break;
1099 default:
1100 break;
1101 }
1102done:
1103 return NOTIFY_OK;
1104}
1105
1106static struct notifier_block adsp_service_nb = {
1107 .notifier_call = apr_notifier_service_cb,
1108 .priority = 0,
1109};
1110
1111static struct notifier_block modem_service_nb = {
1112 .notifier_call = apr_notifier_service_cb,
1113 .priority = 0,
1114};
1115
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301116#ifdef CONFIG_DEBUG_FS
1117static int __init apr_debug_init(void)
1118{
1119 debugfs_apr_debug = debugfs_create_file("msm_apr_debug",
1120 S_IFREG | 0444, NULL, NULL,
1121 &apr_debug_ops);
1122 return 0;
1123}
1124#else
1125static int __init apr_debug_init(void)
1126(
1127 return 0;
1128)
1129#endif
1130
Laxminath Kasam31b26c52018-02-12 16:32:01 +05301131static void apr_cleanup(void)
1132{
1133 int i, j, k;
1134
1135 subsys_notif_deregister("apr_modem");
1136 subsys_notif_deregister("apr_adsp");
1137 if (apr_reset_workqueue)
1138 destroy_workqueue(apr_reset_workqueue);
1139 mutex_destroy(&q6.lock);
1140 for (i = 0; i < APR_DEST_MAX; i++) {
1141 for (j = 0; j < APR_CLIENT_MAX; j++) {
1142 mutex_destroy(&client[i][j].m_lock);
1143 for (k = 0; k < APR_SVC_MAX; k++)
1144 mutex_destroy(&client[i][j].svc[k].m_lock);
1145 }
1146 }
1147}
1148
1149static int apr_probe(struct platform_device *pdev)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301150{
1151 int i, j, k;
1152
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301153 init_waitqueue_head(&dsp_wait);
1154 init_waitqueue_head(&modem_wait);
1155
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301156 for (i = 0; i < APR_DEST_MAX; i++)
1157 for (j = 0; j < APR_CLIENT_MAX; j++) {
1158 mutex_init(&client[i][j].m_lock);
1159 for (k = 0; k < APR_SVC_MAX; k++) {
1160 mutex_init(&client[i][j].svc[k].m_lock);
1161 spin_lock_init(&client[i][j].svc[k].w_lock);
1162 }
1163 }
1164 apr_set_subsys_state();
1165 mutex_init(&q6.lock);
1166 apr_reset_workqueue = create_singlethread_workqueue("apr_driver");
1167 if (!apr_reset_workqueue)
1168 return -ENOMEM;
1169
1170 apr_pkt_ctx = ipc_log_context_create(APR_PKT_IPC_LOG_PAGE_CNT,
1171 "apr", 0);
1172 if (!apr_pkt_ctx)
1173 pr_err("%s: Unable to create ipc log context\n", __func__);
1174
1175 is_initial_boot = true;
1176 subsys_notif_register("apr_adsp", AUDIO_NOTIFIER_ADSP_DOMAIN,
1177 &adsp_service_nb);
1178 subsys_notif_register("apr_modem", AUDIO_NOTIFIER_MODEM_DOMAIN,
1179 &modem_service_nb);
1180
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301181 apr_tal_init();
Laxminath Kasam31b26c52018-02-12 16:32:01 +05301182 apr_dev_ptr = &pdev->dev;
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301183 return apr_debug_init();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301184}
Laxminath Kasam31b26c52018-02-12 16:32:01 +05301185
1186static int apr_remove(struct platform_device *pdev)
1187{
1188 apr_cleanup();
1189 return 0;
1190}
1191
1192static const struct of_device_id apr_machine_of_match[] = {
1193 { .compatible = "qcom,msm-audio-apr", },
1194 {},
1195};
1196
1197static struct platform_driver apr_driver = {
1198 .probe = apr_probe,
1199 .remove = apr_remove,
1200 .driver = {
1201 .name = "audio_apr",
1202 .owner = THIS_MODULE,
1203 .of_match_table = apr_machine_of_match,
1204 }
1205};
1206
1207static int __init apr_init(void)
1208{
1209 platform_driver_register(&apr_driver);
1210 apr_dummy_init();
1211 return 0;
1212}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301213module_init(apr_init);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301214
Laxminath Kasam31b26c52018-02-12 16:32:01 +05301215static void __exit apr_exit(void)
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301216{
Laxminath Kasam31b26c52018-02-12 16:32:01 +05301217 apr_dummy_exit();
1218 platform_driver_unregister(&apr_driver);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301219}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301220module_exit(apr_exit);
Laxminath Kasam31b26c52018-02-12 16:32:01 +05301221
1222MODULE_DESCRIPTION("APR DRIVER");
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05301223MODULE_LICENSE("GPL v2");
Laxminath Kasam31b26c52018-02-12 16:32:01 +05301224MODULE_DEVICE_TABLE(of, apr_machine_of_match);