blob: acd9c4c1bcc1ad1b70b3c6ea2e0591157c095c8f [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * Register/Interrupt access for userspace aDSP library.
3 *
4 * Copyright (C) 2008 Google, Inc.
Laxminath Kasam1d8255d2012-02-15 13:10:19 +05305 * Copyright (c) 2008-2009,2011-2012 Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07006 * Author: Iliyan Malchev <ibm@android.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19/* TODO:
20 * - move shareable rpc code outside of adsp.c
21 * - general solution for virt->phys patchup
22 * - queue IDs should be relative to modules
23 * - disallow access to non-associated queues
24 */
25
26#include <linux/clk.h>
27#include <linux/delay.h>
28#include <linux/interrupt.h>
29#include <linux/kernel.h>
30#include <linux/kthread.h>
31#include <linux/module.h>
32#include <linux/uaccess.h>
33#include <linux/wait.h>
34#include <linux/slab.h>
35#include <linux/io.h>
36#include <mach/msm_iomap.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037#include <mach/msm_adsp.h>
38#include "adsp.h"
39#include <mach/debug_mm.h>
40#include <linux/debugfs.h>
41
42#ifdef CONFIG_DEBUG_FS
43static struct dentry *dentry_adsp;
44static struct dentry *dentry_wdata;
45static struct dentry *dentry_rdata;
46static int wdump, rdump;
47#endif /* CONFIG_DEBUG_FS */
48
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049static struct adsp_info adsp_info;
50static struct msm_adsp_module *adsp_modules;
51static int adsp_open_count;
52
53static DEFINE_MUTEX(adsp_open_lock);
54
55/* protect interactions with the ADSP command/message queue */
56static spinlock_t adsp_cmd_lock;
57static spinlock_t adsp_write_lock;
58
59static uint32_t current_image = -1;
60
61void adsp_set_image(struct adsp_info *info, uint32_t image)
62{
63 current_image = image;
64}
65
66/*
67 * Checks whether the module_id is available in the
68 * module_entries table.If module_id is available returns `0`.
69 * If module_id is not available returns `-ENXIO`.
70 */
71static int32_t adsp_validate_module(uint32_t module_id)
72{
73 uint32_t *ptr;
74 uint32_t module_index;
75 uint32_t num_mod_entries;
76
77 ptr = adsp_info.init_info_ptr->module_entries;
78 num_mod_entries = adsp_info.init_info_ptr->module_table_size;
79
80 for (module_index = 0; module_index < num_mod_entries; module_index++)
81 if (module_id == ptr[module_index])
82 return 0;
83
84 return -ENXIO;
85}
86
87static int32_t adsp_validate_queue(uint32_t mod_id, unsigned q_idx,
88 uint32_t size)
89{
90 int32_t i;
91 struct adsp_rtos_mp_mtoa_init_info_type *sptr;
92
93 sptr = adsp_info.init_info_ptr;
94 for (i = 0; i < sptr->mod_to_q_entries; i++)
95 if (mod_id == sptr->mod_to_q_tbl[i].module)
96 if (q_idx == sptr->mod_to_q_tbl[i].q_type) {
97 if (size <= sptr->mod_to_q_tbl[i].q_max_len)
98 return 0;
99 MM_ERR("q_idx: %d is not a valid queue \
100 for module %x\n", q_idx, mod_id);
101 return -EINVAL;
102 }
103 MM_ERR("cmd_buf size is more than allowed size\n");
104 return -EINVAL;
105}
106
107uint32_t adsp_get_module(struct adsp_info *info, uint32_t task)
108{
109 return info->task_to_module[current_image][task];
110}
111
112uint32_t adsp_get_queue_offset(struct adsp_info *info, uint32_t queue_id)
113{
114 return info->queue_offset[current_image][queue_id];
115}
116
117static int rpc_adsp_rtos_app_to_modem(uint32_t cmd, uint32_t module,
118 struct msm_adsp_module *adsp_module)
119{
120 struct adsp_rtos_atom_cmd adspsvc_cmd;
121 int err;
122
123 adspsvc_cmd.cmd = cmd;
124 adspsvc_cmd.proc_id = RPC_ADSP_RTOS_PROC_APPS;
125 adspsvc_cmd.module = module;
126 adspsvc_cmd.cb_handle = adsp_info.cb_handle;
127
128 err = dalrpc_fcn_5(DALDEVICE_ADSP_CMD_IDX | 0x80000000,
129 adsp_info.handle,
130 &adspsvc_cmd, sizeof(adspsvc_cmd));
131 if (err < 0)
132 MM_ERR("ADSP command send Failed\n");
133
134 return 0;
135}
136
137static int get_module_index(uint32_t id)
138{
139 int mod_idx;
140 for (mod_idx = 0; mod_idx < adsp_info.module_count; mod_idx++)
141 if (adsp_info.module[mod_idx].id == id)
142 return mod_idx;
143
144 return -ENXIO;
145}
146
147static struct msm_adsp_module *find_adsp_module_by_id(
148 struct adsp_info *info, uint32_t id)
149{
150 int mod_idx;
151
152 if (id > info->max_module_id) {
153 return NULL;
154 } else {
155 mod_idx = get_module_index(id);
156 if (mod_idx < 0)
157 return NULL;
158 return info->id_to_module[mod_idx];
159 }
160}
161
162static struct msm_adsp_module *find_adsp_module_by_name(
163 struct adsp_info *info, const char *name)
164{
165 unsigned n;
166 for (n = 0; n < info->module_count; n++)
167 if (!strcmp(name, adsp_modules[n].name))
168 return adsp_modules + n;
169 return NULL;
170}
171
172/*
173 * Send RPC_ADSP_RTOS_CMD_GET_INIT_INFO cmd to ARM9 and get
174 * queue offsets and module entries (init info) as part of the event.
175 */
176static void msm_get_init_info(void)
177{
178 struct adsp_rtos_atom_cmd cmd;
179 int err;
180
181 cmd.cmd = RPC_ADSP_RTOS_CMD_GET_INIT_INFO;
182 cmd.proc_id = RPC_ADSP_RTOS_PROC_APPS;
183 cmd.module = 0;
184 cmd.cb_handle = adsp_info.cb_handle;
185
186 err = dalrpc_fcn_5(DALDEVICE_ADSP_CMD_IDX | 0x80000000,
187 adsp_info.handle,
188 &cmd, sizeof(cmd));
189 if (err < 0)
190 MM_ERR("INIT_INFO command send Failed\n");
191}
192
193int msm_adsp_get(const char *name, struct msm_adsp_module **out,
194 struct msm_adsp_ops *ops, void *driver_data)
195{
196 struct msm_adsp_module *module;
197 int rc = 0;
198
199 module = find_adsp_module_by_name(&adsp_info, name);
200 if (!module)
201 return -ENODEV;
202
203 mutex_lock(&module->lock);
204 MM_DBG("opening module %s\n", module->name);
205
206 if (module->ops) {
207 rc = -EBUSY;
208 mutex_unlock(&module->lock);
209 goto done;
210 }
211
212 module->ops = ops;
213 module->driver_data = driver_data;
214 *out = module;
215 mutex_unlock(&module->lock);
216 rc = rpc_adsp_rtos_app_to_modem(RPC_ADSP_RTOS_CMD_REGISTER_APP,
217 module->id, module);
218 if (rc) {
219 mutex_lock(&module->lock);
220 module->ops = NULL;
221 module->driver_data = NULL;
222 *out = NULL;
223 MM_ERR("REGISTER_APP failed\n");
224 mutex_unlock(&module->lock);
225 goto done;
226 }
227
228 MM_INFO("module %s has been registered\n", module->name);
229
230done:
231 return rc;
232}
233EXPORT_SYMBOL(msm_adsp_get);
234
235void msm_adsp_put(struct msm_adsp_module *module)
236{
237 unsigned long flags;
238
239 mutex_lock(&module->lock);
240 if (module->ops) {
241 MM_INFO("closing module %s\n", module->name);
242
243 /* lock to ensure a dsp event cannot be delivered
244 * during or after removal of the ops and driver_data
245 */
246 spin_lock_irqsave(&adsp_cmd_lock, flags);
247 module->ops = NULL;
248 module->driver_data = NULL;
249 spin_unlock_irqrestore(&adsp_cmd_lock, flags);
250
251 if (module->state != ADSP_STATE_DISABLED) {
252 MM_INFO("disabling module %s\n", module->name);
253 mutex_unlock(&module->lock);
254 msm_adsp_disable(module);
255 return;
256 }
257 } else {
258 MM_INFO("module %s is already closed\n", module->name);
259 }
260 mutex_unlock(&module->lock);
261}
262EXPORT_SYMBOL(msm_adsp_put);
263
264int __msm_adsp_write(struct msm_adsp_module *module, unsigned dsp_queue_addr,
265 void *cmd_buf, size_t cmd_size)
266{
267 uint32_t ctrl_word;
268 uint32_t dsp_q_addr;
269 uint32_t dsp_addr;
270 uint32_t cmd_id = 0;
271 int cnt = 0;
272 int ret_status = 0;
273 unsigned long flags;
274 struct adsp_info *info;
275
276 if (!module || !cmd_buf) {
277 MM_ERR("Called with NULL parameters\n");
278 return -EINVAL;
279 }
280 info = module->info;
281 spin_lock_irqsave(&adsp_write_lock, flags);
282
283 if (module->state != ADSP_STATE_ENABLED) {
284 spin_unlock_irqrestore(&adsp_write_lock, flags);
285 MM_ERR("module %s not enabled before write\n", module->name);
286 return -ENODEV;
287 }
288 if (adsp_validate_module(module->id)) {
289 spin_unlock_irqrestore(&adsp_write_lock, flags);
290 MM_ERR("module id validation failed %s %d\n",
291 module->name, module->id);
292 return -ENXIO;
293 }
294 if (dsp_queue_addr >= QDSP_MAX_NUM_QUEUES) {
295 spin_unlock_irqrestore(&adsp_write_lock, flags);
296 MM_ERR("Invalid Queue Index: %d\n", dsp_queue_addr);
297 return -ENXIO;
298 }
299 if (adsp_validate_queue(module->id, dsp_queue_addr, cmd_size)) {
300 spin_unlock_irqrestore(&adsp_write_lock, flags);
301 return -EINVAL;
302 }
303 dsp_q_addr = adsp_get_queue_offset(info, dsp_queue_addr);
304 dsp_q_addr &= ADSP_RTOS_WRITE_CTRL_WORD_DSP_ADDR_M;
305
306 /* Poll until the ADSP is ready to accept a command.
307 * Wait for 100us, return error if it's not responding.
308 * If this returns an error, we need to disable ALL modules and
309 * then retry.
310 */
311 while (((ctrl_word = readl(info->write_ctrl)) &
312 ADSP_RTOS_WRITE_CTRL_WORD_READY_M) !=
313 ADSP_RTOS_WRITE_CTRL_WORD_READY_V) {
314 if (cnt > 50) {
315 MM_ERR("timeout waiting for DSP write ready\n");
316 ret_status = -EIO;
317 goto fail;
318 }
319 MM_DBG("waiting for DSP write ready\n");
320 udelay(2);
321 cnt++;
322 }
323
324 /* Set the mutex bits */
325 ctrl_word &= ~(ADSP_RTOS_WRITE_CTRL_WORD_MUTEX_M);
326 ctrl_word |= ADSP_RTOS_WRITE_CTRL_WORD_MUTEX_NAVAIL_V;
327
328 /* Clear the command bits */
329 ctrl_word &= ~(ADSP_RTOS_WRITE_CTRL_WORD_CMD_M);
330
331 /* Set the queue address bits */
332 ctrl_word &= ~(ADSP_RTOS_WRITE_CTRL_WORD_DSP_ADDR_M);
333 ctrl_word |= dsp_q_addr;
334
335 writel(ctrl_word, info->write_ctrl);
336
337 /* Generate an interrupt to the DSP. This notifies the DSP that
338 * we are about to send a command on this particular queue. The
339 * DSP will in response change its state.
340 */
341 writel(1, info->send_irq);
342
343 /* Poll until the adsp responds to the interrupt; this does not
344 * generate an interrupt from the adsp. This should happen within
345 * 5ms.
346 */
347 cnt = 0;
348 while ((readl(info->write_ctrl) &
349 ADSP_RTOS_WRITE_CTRL_WORD_MUTEX_M) ==
350 ADSP_RTOS_WRITE_CTRL_WORD_MUTEX_NAVAIL_V) {
351 if (cnt > 2500) {
352 MM_ERR("timeout waiting for adsp ack\n");
353 ret_status = -EIO;
354 goto fail;
355 }
356 udelay(2);
357 cnt++;
358 }
359
360 /* Read the ctrl word */
361 ctrl_word = readl(info->write_ctrl);
362
363 if ((ctrl_word & ADSP_RTOS_WRITE_CTRL_WORD_STATUS_M) !=
364 ADSP_RTOS_WRITE_CTRL_WORD_NO_ERR_V) {
365 ret_status = -EAGAIN;
366 goto fail;
367 } else {
368 /* No error */
369 /* Get the DSP buffer address */
370 dsp_addr = (ctrl_word & ADSP_RTOS_WRITE_CTRL_WORD_DSP_ADDR_M) +
371 (uint32_t)MSM_AD5_BASE;
372
373 if (dsp_addr < (uint32_t)(MSM_AD5_BASE + QDSP_RAMC_OFFSET)) {
374 uint16_t *buf_ptr = (uint16_t *) cmd_buf;
375 uint16_t *dsp_addr16 = (uint16_t *)dsp_addr;
376 cmd_size /= sizeof(uint16_t);
377
378 /* Save the command ID */
379 cmd_id = (uint32_t) buf_ptr[0];
380
381 /* Copy the command to DSP memory */
382 cmd_size++;
383 while (--cmd_size)
384 *dsp_addr16++ = *buf_ptr++;
385 } else {
386 uint32_t *buf_ptr = (uint32_t *) cmd_buf;
387 uint32_t *dsp_addr32 = (uint32_t *)dsp_addr;
388 cmd_size /= sizeof(uint32_t);
389
390 /* Save the command ID */
391 cmd_id = buf_ptr[0];
392
393 cmd_size++;
394 while (--cmd_size)
395 *dsp_addr32++ = *buf_ptr++;
396 }
397
398 /* Set the mutex bits */
399 ctrl_word &= ~(ADSP_RTOS_WRITE_CTRL_WORD_MUTEX_M);
400 ctrl_word |= ADSP_RTOS_WRITE_CTRL_WORD_MUTEX_NAVAIL_V;
401
402 /* Set the command bits to write done */
403 ctrl_word &= ~(ADSP_RTOS_WRITE_CTRL_WORD_CMD_M);
404 ctrl_word |= ADSP_RTOS_WRITE_CTRL_WORD_CMD_WRITE_DONE_V;
405
406 /* Set the queue address bits */
407 ctrl_word &= ~(ADSP_RTOS_WRITE_CTRL_WORD_DSP_ADDR_M);
408 ctrl_word |= dsp_q_addr;
409
410 writel(ctrl_word, info->write_ctrl);
411
412 /* Generate an interrupt to the DSP. It does not respond with
413 * an interrupt, and we do not need to wait for it to
414 * acknowledge, because it will hold the mutex lock until it's
415 * ready to receive more commands again.
416 */
417 writel(1, info->send_irq);
418
419 module->num_commands++;
420 } /* Ctrl word status bits were 00, no error in the ctrl word */
421
422fail:
423 spin_unlock_irqrestore(&adsp_write_lock, flags);
424 return ret_status;
425}
426EXPORT_SYMBOL(msm_adsp_write);
427
428int msm_adsp_write(struct msm_adsp_module *module, unsigned dsp_queue_addr,
429 void *cmd_buf, size_t cmd_size)
430{
431 int rc, retries = 0;
432#ifdef CONFIG_DEBUG_FS
433 uint16_t *ptr;
434 int ii;
435
436 if (wdump > 0) {
437 ptr = cmd_buf;
438 pr_info("A->D:%x\n", module->id);
439 pr_info("adsp: %x %d\n", dsp_queue_addr, cmd_size);
440 for (ii = 0; ii < cmd_size/2; ii++)
441 pr_info("%x ", ptr[ii]);
442 pr_info("\n");
443 }
444#endif /* CONFIG_DEBUG_FS */
445 do {
446 rc = __msm_adsp_write(module, dsp_queue_addr, cmd_buf,
447 cmd_size);
448 if (rc == -EAGAIN)
449 udelay(50);
450 } while (rc == -EAGAIN && retries++ < 300);
451 if (retries > 20)
452 MM_INFO("%s command took %d attempts: rc %d\n",
453 module->name, retries, rc);
454 return rc;
455}
456
457#ifdef CONFIG_MSM_ADSP_REPORT_EVENTS
458static void *event_addr;
459static void read_event(void *buf, size_t len)
460{
461 uint32_t dptr[3];
462 struct adsp_rtos_mp_mtoa_s_type *sptr;
463 struct adsp_rtos_mp_mtoa_type *pkt_ptr;
464
465 sptr = event_addr;
466 pkt_ptr = &sptr->adsp_rtos_mp_mtoa_data.mp_mtoa_packet;
467
468 dptr[0] = sptr->mp_mtoa_header.event;
469 dptr[1] = pkt_ptr->module;
470 dptr[2] = pkt_ptr->image;
471
472 if (len > EVENT_LEN)
473 len = EVENT_LEN;
474
475 memcpy(buf, dptr, len);
476}
477#endif
478
479static void adsp_rtos_mtoa_cb(void *context, uint32_t param,
480 void *evt_buf, uint32_t len)
481{
482 struct adsp_rtos_mp_mtoa_s_type *args = NULL;
483 uint32_t event = 0;
484 uint32_t proc_id = 0;
485 uint32_t module_id;
486 uint32_t image;
487 struct msm_adsp_module *module;
488 struct adsp_rtos_mp_mtoa_type *pkt_ptr;
489 struct queue_to_offset_type *qptr;
490 struct queue_to_offset_type *qtbl;
491 struct mod_to_queue_offsets *mqptr;
492 struct mod_to_queue_offsets *mqtbl;
493 uint32_t *mptr;
494 uint32_t *mtbl;
495 uint32_t q_idx;
496 uint32_t num_entries;
497 uint32_t entries_per_image;
498 struct adsp_rtos_mp_mtoa_init_info_type *iptr;
499 struct adsp_rtos_mp_mtoa_init_info_type *sptr;
500 int32_t i_no, e_idx;
501 static uint32_t init_info_completed;
502 static uint32_t init_info_len =
503 sizeof(struct adsp_rtos_mp_mtoa_header_type);
504 static uint32_t next_init_info_byte;
505 static uint32_t expected_byte = 1;
506 uint32_t hdr_len = sizeof(struct adsp_rtos_mp_mtoa_header_type);
507
508 if (len) {
509 args = (struct adsp_rtos_mp_mtoa_s_type *) evt_buf;
510 event = args->mp_mtoa_header.event;
511 proc_id = args->mp_mtoa_header.proc_id;
512 }
513
514 if (!init_info_completed && event == RPC_ADSP_RTOS_INIT_INFO) {
515 memcpy(((char *)adsp_info.raw_event) + init_info_len,
516 (char *)evt_buf + hdr_len + 4,
517 len - ((hdr_len + 4)));
518 init_info_len += (len - (hdr_len + 4));
519 evt_buf += hdr_len;
520 next_init_info_byte = *(uint32_t *) evt_buf;
521 expected_byte += len;
522 if (next_init_info_byte &&
523 (expected_byte != next_init_info_byte)) {
524 MM_ERR("INIT_INFO - expecting next byte to be %d\n"
525 "\tbut ADSPSVC indicated next byte to be %d\n",
526 expected_byte, next_init_info_byte);
527 return;
528 }
529 if (!next_init_info_byte) {
530 args = adsp_info.raw_event;
531 args->mp_mtoa_header.event = event;
532 args->mp_mtoa_header.proc_id = proc_id;
533 init_info_completed = 1;
534 } else
535 return;
536 }
537
538 if (event == RPC_ADSP_RTOS_INIT_INFO) {
539 MM_INFO("INIT_INFO Event\n");
540 sptr = &args->adsp_rtos_mp_mtoa_data.mp_mtoa_init_packet;
541
542 iptr = adsp_info.init_info_ptr;
543 iptr->image_count = sptr->image_count;
544 if (iptr->image_count > IMG_MAX)
545 iptr->image_count = IMG_MAX;
546 iptr->num_queue_offsets = sptr->num_queue_offsets;
547 num_entries = iptr->num_queue_offsets;
548 if (num_entries > ENTRIES_MAX) {
549 num_entries = ENTRIES_MAX;
550 iptr->num_queue_offsets = ENTRIES_MAX;
551 }
552 qptr = &sptr->queue_offsets_tbl[0][0];
553 for (i_no = 0; i_no < iptr->image_count; i_no++) {
554 qtbl = &iptr->queue_offsets_tbl[i_no][0];
555 for (e_idx = 0; e_idx < num_entries; e_idx++) {
556 qtbl[e_idx].offset = qptr->offset;
557 qtbl[e_idx].queue = qptr->queue;
558 q_idx = qptr->queue;
559 iptr->queue_offsets[i_no][q_idx] =
560 qtbl[e_idx].offset;
561 qptr++;
562 }
563 }
564
565 num_entries = sptr->num_task_module_entries;
566 if (num_entries > ENTRIES_MAX)
567 num_entries = ENTRIES_MAX;
568 iptr->num_task_module_entries = num_entries;
569 entries_per_image = num_entries / iptr->image_count;
570 mptr = &sptr->task_to_module_tbl[0][0];
571 for (i_no = 0; i_no < iptr->image_count; i_no++) {
572 mtbl = &iptr->task_to_module_tbl[i_no][0];
573 for (e_idx = 0; e_idx < entries_per_image; e_idx++) {
574 mtbl[e_idx] = *mptr;
575 mptr++;
576 }
577 }
578
579 iptr->module_table_size = sptr->module_table_size;
580 if (iptr->module_table_size > MODULES_MAX)
581 iptr->module_table_size = MODULES_MAX;
582 mptr = &sptr->module_entries[0];
583 for (i_no = 0; i_no < iptr->module_table_size; i_no++)
584 iptr->module_entries[i_no] = mptr[i_no];
585
586 mqptr = &sptr->mod_to_q_tbl[0];
587 mqtbl = &iptr->mod_to_q_tbl[0];
588 iptr->mod_to_q_entries = sptr->mod_to_q_entries;
589 if (iptr->mod_to_q_entries > ENTRIES_MAX)
590 iptr->mod_to_q_entries = ENTRIES_MAX;
591 for (e_idx = 0; e_idx < iptr->mod_to_q_entries; e_idx++) {
592 mqtbl[e_idx].module = mqptr->module;
593 mqtbl[e_idx].q_type = mqptr->q_type;
594 mqtbl[e_idx].q_max_len = mqptr->q_max_len;
595 mqptr++;
596 }
597
598 adsp_info.init_info_state = ADSP_STATE_INIT_INFO;
599 kfree(adsp_info.raw_event);
600 wake_up(&adsp_info.init_info_wait);
601 return;
602 }
603 pkt_ptr = &args->adsp_rtos_mp_mtoa_data.mp_mtoa_packet;
604 module_id = pkt_ptr->module;
605 image = pkt_ptr->image;
606
607 MM_INFO("rpc event=%d, proc_id=%d, module=%d, image=%d\n",
608 event, proc_id, module_id, image);
609
610 module = find_adsp_module_by_id(&adsp_info, module_id);
611 if (!module) {
612 MM_ERR("module %d is not supported!\n", module_id);
613 return;
614 }
615
616 mutex_lock(&module->lock);
617 switch (event) {
618 case RPC_ADSP_RTOS_MOD_READY:
619 MM_INFO("module %s: READY\n", module->name);
620 module->state = ADSP_STATE_ENABLED;
621 wake_up(&module->state_wait);
622 adsp_set_image(module->info, image);
623 break;
624 case RPC_ADSP_RTOS_MOD_DISABLE:
625 MM_INFO("module %s: DISABLED\n", module->name);
626 module->state = ADSP_STATE_DISABLED;
627 wake_up(&module->state_wait);
628 break;
629 case RPC_ADSP_RTOS_SERVICE_RESET:
630 MM_INFO("module %s: SERVICE_RESET\n", module->name);
631 module->state = ADSP_STATE_DISABLED;
632 wake_up(&module->state_wait);
633 break;
634 case RPC_ADSP_RTOS_CMD_SUCCESS:
635 MM_INFO("module %s: CMD_SUCCESS\n", module->name);
636 break;
637 case RPC_ADSP_RTOS_CMD_FAIL:
638 MM_INFO("module %s: CMD_FAIL\n", module->name);
639 break;
640 case RPC_ADSP_RTOS_DISABLE_FAIL:
641 MM_INFO("module %s: DISABLE_FAIL\n", module->name);
642 break;
643 default:
644 MM_ERR("unknown event %d\n", event);
645 mutex_unlock(&module->lock);
646 return;
647 }
648#ifdef CONFIG_MSM_ADSP_REPORT_EVENTS
649 event_addr = (uint32_t *)evt_buf;
650 if (module->ops)
651 module->ops->event(module->driver_data,
652 EVENT_MSG_ID,
653 EVENT_LEN,
654 read_event);
655#endif
656 mutex_unlock(&module->lock);
657}
658
659static size_t read_event_size;
660static void *read_event_addr;
661
662static void read_event_16(void *buf, size_t len)
663{
664 uint16_t *dst = buf;
665 uint16_t *src = read_event_addr;
666 len /= 2;
667 if (len > read_event_size)
668 len = read_event_size;
669 while (len--)
670 *dst++ = *src++;
671}
672
673static void read_event_32(void *buf, size_t len)
674{
675 uint32_t *dst = buf;
676 uint32_t *src = read_event_addr;
677 len /= 2;
678 if (len > read_event_size)
679 len = read_event_size;
680 while (len--)
681 *dst++ = *src++;
682}
683
684static int adsp_rtos_read_ctrl_word_cmd_tast_to_h_v(
685 struct adsp_info *info, void *dsp_addr)
686{
687 struct msm_adsp_module *module;
688 unsigned rtos_task_id;
689 unsigned msg_id;
690 unsigned msg_length;
691#ifdef CONFIG_DEBUG_FS
692 uint16_t *ptr;
693 int ii;
694#endif /* CONFIG_DEBUG_FS */
695 void (*func)(void *, size_t);
696
697 if (dsp_addr >= (void *)(MSM_AD5_BASE + QDSP_RAMC_OFFSET)) {
698 uint32_t *dsp_addr32 = dsp_addr;
699 uint32_t tmp = *dsp_addr32++;
700 rtos_task_id = (tmp & ADSP_RTOS_READ_CTRL_WORD_TASK_ID_M) >> 8;
701 msg_id = (tmp & ADSP_RTOS_READ_CTRL_WORD_MSG_ID_M);
702 read_event_size = tmp >> 16;
703 read_event_addr = dsp_addr32;
704 msg_length = read_event_size * sizeof(uint32_t);
705 func = read_event_32;
706 } else {
707 uint16_t *dsp_addr16 = dsp_addr;
708 uint16_t tmp = *dsp_addr16++;
709 rtos_task_id = (tmp & ADSP_RTOS_READ_CTRL_WORD_TASK_ID_M) >> 8;
710 msg_id = tmp & ADSP_RTOS_READ_CTRL_WORD_MSG_ID_M;
711 read_event_size = *dsp_addr16++;
712 read_event_addr = dsp_addr16;
713 msg_length = read_event_size * sizeof(uint16_t);
714 func = read_event_16;
715 }
716
717 if (rtos_task_id > info->max_task_id) {
718 MM_ERR("bogus task id %d\n", rtos_task_id);
719 return 0;
720 }
721 module = find_adsp_module_by_id(info,
722 adsp_get_module(info, rtos_task_id));
723
724 if (!module) {
725 MM_ERR("no module for task id %d\n", rtos_task_id);
726 return 0;
727 }
728
729 module->num_events++;
730
731 if (!module->ops) {
732 MM_ERR("module %s is not open\n", module->name);
733 return 0;
734 }
735#ifdef CONFIG_DEBUG_FS
736 if (rdump > 0) {
737 ptr = read_event_addr;
738 pr_info("D->A\n");
739 pr_info("m_id = %x id = %x\n", module->id, msg_id);
740 for (ii = 0; ii < msg_length/2; ii++)
741 pr_info("%x ", ptr[ii]);
742 pr_info("\n");
743 }
744#endif /* CONFIG_DEBUG_FS */
745
746 module->ops->event(module->driver_data, msg_id, msg_length, func);
747 return 0;
748}
749
750static int adsp_get_event(struct adsp_info *info)
751{
752 uint32_t ctrl_word;
753 uint32_t ready;
754 void *dsp_addr;
755 uint32_t cmd_type;
756 int cnt;
757 unsigned long flags;
758 int rc = 0;
759
760 spin_lock_irqsave(&adsp_cmd_lock, flags);
761
762 /* Whenever the DSP has a message, it updates this control word
763 * and generates an interrupt. When we receive the interrupt, we
764 * read this register to find out what ADSP task the command is
765 * comming from.
766 *
767 * The ADSP should *always* be ready on the first call, but the
768 * irq handler calls us in a loop (to handle back-to-back command
769 * processing), so we give the DSP some time to return to the
770 * ready state. The DSP will not issue another IRQ for events
771 * pending between the first IRQ and the event queue being drained,
772 * unfortunately.
773 */
774
775 for (cnt = 0; cnt < 50; cnt++) {
776 ctrl_word = readl(info->read_ctrl);
777
778 if ((ctrl_word & ADSP_RTOS_READ_CTRL_WORD_FLAG_M) ==
779 ADSP_RTOS_READ_CTRL_WORD_FLAG_UP_CONT_V)
780 goto ready;
781
782 udelay(2);
783 }
784 MM_ERR("not ready after 100uS\n");
785 rc = -EBUSY;
786 goto done;
787
788ready:
789 /* Here we check to see if there are pending messages. If there are
790 * none, we siply return -EAGAIN to indicate that there are no more
791 * messages pending.
792 */
793 ready = ctrl_word & ADSP_RTOS_READ_CTRL_WORD_READY_M;
794 if ((ready != ADSP_RTOS_READ_CTRL_WORD_READY_V) &&
795 (ready != ADSP_RTOS_READ_CTRL_WORD_CONT_V)) {
796 rc = -EAGAIN;
797 goto done;
798 }
799
800 /* DSP says that there are messages waiting for the host to read */
801
802 /* Get the Command Type */
803 cmd_type = ctrl_word & ADSP_RTOS_READ_CTRL_WORD_CMD_TYPE_M;
804
805 /* Get the DSP buffer address */
806 dsp_addr = (void *)((ctrl_word &
807 ADSP_RTOS_READ_CTRL_WORD_DSP_ADDR_M) +
808 (uint32_t)MSM_AD5_BASE);
809
810 /* We can only handle Task-to-Host messages */
811 if (cmd_type != ADSP_RTOS_READ_CTRL_WORD_CMD_TASK_TO_H_V) {
812 MM_ERR("unknown dsp cmd_type %d\n", cmd_type);
813 rc = -EIO;
814 goto done;
815 }
816
817 adsp_rtos_read_ctrl_word_cmd_tast_to_h_v(info, dsp_addr);
818
819 ctrl_word = readl(info->read_ctrl);
820 ctrl_word &= ~ADSP_RTOS_READ_CTRL_WORD_READY_M;
821
822 /* Write ctrl word to the DSP */
823 writel(ctrl_word, info->read_ctrl);
824
825 /* Generate an interrupt to the DSP */
826 writel(1, info->send_irq);
827
828done:
829 spin_unlock_irqrestore(&adsp_cmd_lock, flags);
830 return rc;
831}
832
833static irqreturn_t adsp_irq_handler(int irq, void *data)
834{
835 struct adsp_info *info = &adsp_info;
836 int cnt = 0;
837 for (cnt = 0; cnt < 15; cnt++)
838 if (adsp_get_event(info) < 0)
839 break;
840 if (cnt > info->event_backlog_max)
841 info->event_backlog_max = cnt;
842 info->events_received += cnt;
843 if (cnt == 15)
844 MM_ERR("too many (%d) events for single irq!\n", cnt);
845 return IRQ_HANDLED;
846}
847
848int adsp_set_clkrate(struct msm_adsp_module *module, unsigned long clk_rate)
849{
850 if (module->clk && clk_rate)
Matt Wagantallf13bee62011-11-08 15:36:32 -0800851 return clk_set_rate(module->clk, clk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852
853 return -EINVAL;
854}
855
856int msm_adsp_enable(struct msm_adsp_module *module)
857{
858 int rc = 0;
859
860 MM_INFO("enable '%s'state[%d] id[%d]\n",
861 module->name, module->state, module->id);
862
863 mutex_lock(&module->lock);
864 switch (module->state) {
865 case ADSP_STATE_DISABLED:
866 module->state = ADSP_STATE_ENABLING;
867 mutex_unlock(&module->lock);
868 rc = rpc_adsp_rtos_app_to_modem(RPC_ADSP_RTOS_CMD_ENABLE,
869 module->id, module);
870 if (rc) {
871 mutex_lock(&module->lock);
872 module->state = ADSP_STATE_DISABLED;
873 break;
874 }
875 rc = wait_event_timeout(module->state_wait,
876 module->state != ADSP_STATE_ENABLING,
877 1 * HZ);
878 mutex_lock(&module->lock);
879 if (module->state == ADSP_STATE_ENABLED) {
880 rc = 0;
881 } else {
882 MM_ERR("module '%s' enable timed out\n", module->name);
883 rc = -ETIMEDOUT;
884 }
885 if (module->open_count++ == 0 && module->clk)
886 clk_enable(module->clk);
887
888 mutex_lock(&adsp_open_lock);
889 if (adsp_open_count++ == 0)
Laxminath Kasam1d8255d2012-02-15 13:10:19 +0530890 enable_irq(adsp_info.int_adsp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700891 mutex_unlock(&adsp_open_lock);
892 break;
893 case ADSP_STATE_ENABLING:
894 MM_DBG("module '%s' enable in progress\n", module->name);
895 break;
896 case ADSP_STATE_ENABLED:
897 MM_DBG("module '%s' already enabled\n", module->name);
898 break;
899 case ADSP_STATE_DISABLING:
900 MM_ERR("module '%s' disable in progress\n", module->name);
901 rc = -EBUSY;
902 break;
903 }
904 mutex_unlock(&module->lock);
905 return rc;
906}
907EXPORT_SYMBOL(msm_adsp_enable);
908
909int msm_adsp_disable_event_rsp(struct msm_adsp_module *module)
910{
911 int rc = 0;
912
913 mutex_lock(&module->lock);
914
915 rc = rpc_adsp_rtos_app_to_modem(RPC_ADSP_RTOS_CMD_DISABLE_EVENT_RSP,
916 module->id, module);
917 mutex_unlock(&module->lock);
918
919 return rc;
920}
921EXPORT_SYMBOL(msm_adsp_disable_event_rsp);
922
923int msm_adsp_disable(struct msm_adsp_module *module)
924{
925 int rc = 0;
926
927 mutex_lock(&module->lock);
928 switch (module->state) {
929 case ADSP_STATE_DISABLED:
930 MM_DBG("module '%s' already disabled\n", module->name);
931 mutex_unlock(&module->lock);
932 break;
933 case ADSP_STATE_ENABLING:
934 case ADSP_STATE_ENABLED:
935 mutex_unlock(&module->lock);
936 rc = rpc_adsp_rtos_app_to_modem(RPC_ADSP_RTOS_CMD_DISABLE,
937 module->id, module);
938 mutex_lock(&module->lock);
939 module->state = ADSP_STATE_DISABLED;
940 if (--module->open_count == 0 && module->clk)
941 clk_disable(module->clk);
942 mutex_unlock(&module->lock);
943 mutex_lock(&adsp_open_lock);
944 if (--adsp_open_count == 0) {
Laxminath Kasam1d8255d2012-02-15 13:10:19 +0530945 disable_irq(adsp_info.int_adsp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 MM_INFO("disable interrupt\n");
947 }
948 mutex_unlock(&adsp_open_lock);
949 break;
950 }
951 return rc;
952}
953EXPORT_SYMBOL(msm_adsp_disable);
954
955static int msm_adsp_probe(struct platform_device *pdev)
956{
957 unsigned count;
958 int rc, i;
959
Laxminath Kasam1d8255d2012-02-15 13:10:19 +0530960 adsp_info.int_adsp = platform_get_irq(pdev, 0);
961 if (adsp_info.int_adsp < 0) {
962 MM_ERR("no irq resource?\n");
963 return -ENODEV;
964 }
965
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966 adsp_info.init_info_ptr = kzalloc(
967 (sizeof(struct adsp_rtos_mp_mtoa_init_info_type)), GFP_KERNEL);
968 if (!adsp_info.init_info_ptr)
969 return -ENOMEM;
970
971 adsp_info.raw_event = kzalloc(
972 (sizeof(struct adsp_rtos_mp_mtoa_s_type)), GFP_KERNEL);
973 if (!adsp_info.raw_event) {
974 kfree(adsp_info.init_info_ptr);
975 return -ENOMEM;
976 }
977
978 rc = adsp_init_info(&adsp_info);
979 if (rc) {
980 kfree(adsp_info.init_info_ptr);
981 kfree(adsp_info.raw_event);
982 return rc;
983 }
984 adsp_info.send_irq += (uint32_t) MSM_AD5_BASE;
985 adsp_info.read_ctrl += (uint32_t) MSM_AD5_BASE;
986 adsp_info.write_ctrl += (uint32_t) MSM_AD5_BASE;
987 count = adsp_info.module_count;
988
989 adsp_modules = kzalloc(
990 (sizeof(struct msm_adsp_module) + sizeof(void *)) *
991 count, GFP_KERNEL);
992 if (!adsp_modules) {
993 kfree(adsp_info.init_info_ptr);
994 kfree(adsp_info.raw_event);
995 return -ENOMEM;
996 }
997
998 adsp_info.id_to_module = (void *) (adsp_modules + count);
999
1000 spin_lock_init(&adsp_cmd_lock);
1001 spin_lock_init(&adsp_write_lock);
1002
Laxminath Kasam1d8255d2012-02-15 13:10:19 +05301003 rc = request_irq(adsp_info.int_adsp, adsp_irq_handler,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004 IRQF_TRIGGER_RISING, "adsp", 0);
1005 if (rc < 0)
1006 goto fail_request_irq;
Laxminath Kasam1d8255d2012-02-15 13:10:19 +05301007 disable_irq(adsp_info.int_adsp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008
1009 for (i = 0; i < count; i++) {
1010 struct msm_adsp_module *mod = adsp_modules + i;
1011 mutex_init(&mod->lock);
1012 init_waitqueue_head(&mod->state_wait);
1013 mod->info = &adsp_info;
1014 mod->name = adsp_info.module[i].name;
1015 mod->id = adsp_info.module[i].id;
1016 if (adsp_info.module[i].clk_name)
1017 mod->clk = clk_get(NULL, adsp_info.module[i].clk_name);
1018 else
1019 mod->clk = NULL;
1020 if (mod->clk && adsp_info.module[i].clk_rate)
Matt Wagantallf13bee62011-11-08 15:36:32 -08001021 clk_set_rate(mod->clk, adsp_info.module[i].clk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001022 mod->verify_cmd = adsp_info.module[i].verify_cmd;
1023 mod->patch_event = adsp_info.module[i].patch_event;
1024 INIT_HLIST_HEAD(&mod->pmem_regions);
1025 mod->pdev.name = adsp_info.module[i].pdev_name;
1026 mod->pdev.id = -1;
1027 adsp_info.id_to_module[i] = mod;
1028 platform_device_register(&mod->pdev);
1029 }
1030
1031 msm_adsp_publish_cdevs(adsp_modules, count);
1032
1033 rc = daldevice_attach(DALRPC_ADSPSVC_DEVICEID, DALRPC_ADSPSVC_PORT,
1034 DALRPC_ADSPSVC_DEST, &adsp_info.handle);
1035 if (rc) {
1036 MM_ERR("adsp attach failed : %d\n", rc);
1037 goto fail_dal_attach;
1038 }
1039
1040 adsp_info.cb_handle = dalrpc_alloc_cb(adsp_info.handle,
1041 adsp_rtos_mtoa_cb, NULL);
1042 if (adsp_info.cb_handle == NULL) {
1043 MM_ERR("Callback registration failed\n");
1044 goto fail_allocate_cb;
1045 }
1046
1047 /* Get INIT_INFO */
1048 init_waitqueue_head(&adsp_info.init_info_wait);
1049 msm_get_init_info();
1050 rc = wait_event_timeout(adsp_info.init_info_wait,
1051 adsp_info.init_info_state == ADSP_STATE_INIT_INFO,
1052 10 * HZ);
1053 if (!rc) {
1054 MM_ERR("INIT_INFO failed\n");
1055 rc = -ETIMEDOUT;
1056 } else
1057 return 0;
1058
1059fail_allocate_cb:
1060 daldevice_detach(adsp_info.handle);
1061 adsp_info.handle = NULL;
1062fail_dal_attach:
Laxminath Kasam1d8255d2012-02-15 13:10:19 +05301063 enable_irq(adsp_info.int_adsp);
1064 free_irq(adsp_info.int_adsp, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001065fail_request_irq:
1066 kfree(adsp_modules);
1067 kfree(adsp_info.init_info_ptr);
1068 kfree(adsp_info.raw_event);
1069 return rc;
1070}
1071
1072#ifdef CONFIG_DEBUG_FS
1073static int get_parameters(char *buf, long int *param1, int num_of_par)
1074{
1075 char *token;
1076 int base, cnt;
1077
1078 token = strsep(&buf, " ");
1079
1080 for (cnt = 0; cnt < num_of_par; cnt++) {
1081 if (token != NULL) {
1082 if ((token[1] == 'x') || (token[1] == 'X'))
1083 base = 16;
1084 else
1085 base = 10;
1086
1087 if (strict_strtoul(token, base, &param1[cnt]) != 0)
1088 return -EINVAL;
1089
1090 token = strsep(&buf, " ");
1091 }
1092 else
1093 return -EINVAL;
1094 }
1095 return 0;
1096}
1097
1098static ssize_t adsp_debug_open(struct inode *inode, struct file *file)
1099{
1100 file->private_data = inode->i_private;
1101 pr_debug("adsp debugfs opened\n");
1102 return 0;
1103}
1104static ssize_t adsp_debug_write(struct file *file, const char __user *buf,
1105 size_t cnt, loff_t *ppos)
1106{
1107 char *access_str = file->private_data;
1108 char lbuf[32];
1109 int rc;
1110 long int param[5];
1111
1112 if (cnt > sizeof(lbuf) - 1)
1113 return -EINVAL;
1114 rc = copy_from_user(lbuf, buf, cnt);
1115 if (rc) {
1116 pr_info("Unable to copy data from user space\n");
1117 return -EFAULT;
1118 }
1119 lbuf[cnt] = '\0';
1120
1121 if (!strncmp(access_str, "write_log", 9)) {
1122 if (get_parameters(lbuf, param, 1) == 0) {
1123 switch (param[0]) {
1124 case 1:
1125 if (wdump <= 0)
1126 wdump = 1;
1127 pr_debug("write cmd to DSP(A->D) dump \
1128 started:%d\n", wdump);
1129 break;
1130 case 0:
1131 if (wdump > 0)
1132 wdump = 0;
1133 pr_debug("Stop write cmd to \
1134 DSP(A->D):%d\n", wdump);
1135 break;
1136 default:
1137 rc = -EINVAL;
1138 break;
1139 }
1140 } else
1141 rc = -EINVAL;
1142 } else if (!strncmp(access_str, "read_log", 8)) {
1143 if (get_parameters(lbuf, param, 1) == 0) {
1144 switch (param[0]) {
1145 case 1:
1146 if (rdump <= 0)
1147 rdump = 1;
1148 pr_debug("write cmd from DSP(D->A) dump \
1149 started:%d\n", wdump);
1150 break;
1151 case 0:
1152 if (rdump > 0)
1153 rdump = 0;
1154 pr_debug("Stop write cmd from \
1155 DSP(D->A):%d\n", wdump);
1156 break;
1157 default:
1158 rc = -EINVAL;
1159 break;
1160 }
1161 } else
1162 rc = -EINVAL;
1163 } else {
1164 rc = -EINVAL;
1165 }
1166 if (rc == 0)
1167 rc = cnt;
1168 else {
1169 pr_err("%s: rc = %d\n", __func__, rc);
1170 pr_info("\nWrong command: Use =>\n");
1171 pr_info("-------------------------\n");
1172 pr_info("To Start A->D:: echo \"1\">/sys/kernel/debug/ \
1173 adsp_cmd/write_log\n");
1174 pr_info("To Start D->A:: echo \"1\">/sys/kernel/debug/ \
1175 adsp_cmd/read_log\n");
1176 pr_info("To Stop A->D:: echo \"0\">/sys/kernel/debug/ \
1177 adsp_cmd/write_log\n");
1178 pr_info("To Stop D->A:: echo \"0\">/sys/kernel/debug/ \
1179 adsp_cmd/read_log\n");
1180 pr_info("------------------------\n");
1181 }
1182
1183 return rc;
1184}
1185#endif
1186
1187static struct platform_driver msm_adsp_driver = {
1188 .probe = msm_adsp_probe,
1189 .driver = {
1190 .owner = THIS_MODULE,
1191 },
1192};
1193
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194static char msm_adsp_driver_name[] = "msm_adsp";
1195
1196#ifdef CONFIG_DEBUG_FS
1197static const struct file_operations adsp_debug_fops = {
1198 .write = adsp_debug_write,
1199 .open = adsp_debug_open,
1200};
1201#endif
1202
1203static int __init adsp_init(void)
1204{
1205 int rc;
1206
1207#ifdef CONFIG_DEBUG_FS
1208 dentry_adsp = debugfs_create_dir("adsp_cmd", 0);
1209 if (!IS_ERR(dentry_adsp)) {
1210 dentry_wdata = debugfs_create_file("write_log", \
1211 S_IFREG | S_IRUGO, dentry_adsp,
1212 (void *) "write_log" , &adsp_debug_fops);
1213 dentry_rdata = debugfs_create_file("read_log", \
1214 S_IFREG | S_IRUGO, dentry_adsp,
1215 (void *) "read_log", &adsp_debug_fops);
1216 }
1217#endif /* CONFIG_DEBUG_FS */
1218
1219 msm_adsp_driver.driver.name = msm_adsp_driver_name;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220 rc = platform_driver_register(&msm_adsp_driver);
1221 MM_INFO("%s -- %d\n", msm_adsp_driver_name, rc);
1222 return rc;
1223}
1224
1225device_initcall(adsp_init);