blob: 85dc6ba5f9f4427d4b6579e84fb87f4908a0de26 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* arch/arm/mach-msm/qdsp5/adsp.c
2 *
3 * Register/Interrupt access for userspace aDSP library.
4 *
5 * Copyright (C) 2008 Google, Inc.
Duy Truonge833aca2013-02-12 13:35:08 -08006 * Copyright (c) 2008-2012, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07007 * Author: Iliyan Malchev <ibm@android.com>
8 *
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19
20/* TODO:
21 * - move shareable rpc code outside of adsp.c
22 * - general solution for virt->phys patchup
23 * - queue IDs should be relative to modules
24 * - disallow access to non-associated queues
25 */
26
27#include <linux/clk.h>
28#include <linux/delay.h>
29#include <linux/interrupt.h>
30#include <linux/kernel.h>
31#include <linux/kthread.h>
32#include <linux/module.h>
33#include <linux/uaccess.h>
34#include <linux/wait.h>
35#include <linux/wakelock.h>
36#include <linux/slab.h>
Chaithanya Krishna Bacharajua3931822012-05-09 13:44:07 +053037#include <linux/workqueue.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038#include <mach/debug_mm.h>
39#include <linux/debugfs.h>
40
41#ifdef CONFIG_DEBUG_FS
42static struct dentry *dentry_adsp;
43static struct dentry *dentry_wdata;
44static struct dentry *dentry_rdata;
45static int wdump, rdump;
46#endif /* CONFIG_DEBUG_FS */
47static struct wake_lock adsp_wake_lock;
48static inline void prevent_suspend(void)
49{
50 wake_lock(&adsp_wake_lock);
51}
52static inline void allow_suspend(void)
53{
54 wake_unlock(&adsp_wake_lock);
55}
56
57#include <linux/io.h>
58#include <mach/msm_iomap.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059#include <mach/msm_adsp.h>
60#include "adsp.h"
61
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062static struct adsp_info adsp_info;
63static struct msm_rpc_endpoint *rpc_cb_server_client;
64static struct msm_adsp_module *adsp_modules;
65static int adsp_open_count;
66
67static uint32_t rpc_adsp_rtos_atom_prog;
68static uint32_t rpc_adsp_rtos_atom_vers;
69static uint32_t rpc_adsp_rtos_atom_vers_comp;
70static uint32_t rpc_adsp_rtos_mtoa_prog;
71static uint32_t rpc_adsp_rtos_mtoa_vers;
72static uint32_t rpc_adsp_rtos_mtoa_vers_comp;
73static DEFINE_MUTEX(adsp_open_lock);
74
Chaithanya Krishna Bacharajua3931822012-05-09 13:44:07 +053075static struct workqueue_struct *msm_adsp_probe_work_queue;
76static void adsp_probe_work(struct work_struct *work);
77static DECLARE_WORK(msm_adsp_probe_work, adsp_probe_work);
78
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079/* protect interactions with the ADSP command/message queue */
80static spinlock_t adsp_cmd_lock;
81static spinlock_t adsp_write_lock;
82
83static uint32_t current_image = -1;
84
85void adsp_set_image(struct adsp_info *info, uint32_t image)
86{
87 current_image = image;
88}
89
90/*
91 * Checks whether the module_id is available in the
92 * module_entries table.If module_id is available returns `0`.
93 * If module_id is not available returns `-ENXIO`.
94 */
95static int32_t adsp_validate_module(uint32_t module_id)
96{
97 uint32_t *ptr;
98 uint32_t module_index;
99 uint32_t num_mod_entries;
100
101 ptr = adsp_info.init_info_ptr->module_entries;
102 num_mod_entries = adsp_info.init_info_ptr->module_table_size;
103
104 for (module_index = 0; module_index < num_mod_entries; module_index++)
105 if (module_id == ptr[module_index])
106 return 0;
107
108 return -ENXIO;
109}
110
111static int32_t adsp_validate_queue(uint32_t mod_id, unsigned q_idx,
112 uint32_t size)
113{
114 int32_t i;
115 struct adsp_rtos_mp_mtoa_init_info_type *sptr;
116
117 sptr = adsp_info.init_info_ptr;
118 for (i = 0; i < sptr->mod_to_q_entries; i++)
119 if (mod_id == sptr->mod_to_q_tbl[i].module)
120 if (q_idx == sptr->mod_to_q_tbl[i].q_type) {
121 if (size <= sptr->mod_to_q_tbl[i].q_max_len)
122 return 0;
123 MM_ERR("q_idx: %d is not a valid queue \
124 for module %x\n", q_idx, mod_id);
125 return -EINVAL;
126 }
127 MM_ERR("cmd_buf size is more than allowed size\n");
128 return -EINVAL;
129}
130
131uint32_t adsp_get_module(struct adsp_info *info, uint32_t task)
132{
133 return info->task_to_module[current_image][task];
134}
135
136uint32_t adsp_get_queue_offset(struct adsp_info *info, uint32_t queue_id)
137{
138 return info->queue_offset[current_image][queue_id];
139}
140
141static int rpc_adsp_rtos_app_to_modem(uint32_t cmd, uint32_t module,
142 struct msm_adsp_module *adsp_module)
143{
144 int rc;
145 struct rpc_adsp_rtos_app_to_modem_args_t rpc_req;
146 struct rpc_reply_hdr rpc_rsp;
147
148 rpc_req.gotit = cpu_to_be32(1);
149 rpc_req.cmd = cpu_to_be32(cmd);
150 rpc_req.proc_id = cpu_to_be32(RPC_ADSP_RTOS_PROC_APPS);
151 rpc_req.module = cpu_to_be32(module);
152 rc = msm_rpc_call_reply(adsp_module->rpc_client,
153 RPC_ADSP_RTOS_APP_TO_MODEM_PROC,
154 &rpc_req, sizeof(rpc_req),
155 &rpc_rsp, sizeof(rpc_rsp),
156 5 * HZ);
157
158 if (rc < 0) {
159 MM_ERR("error receiving RPC reply: %d (%d)\n",
160 rc, -ERESTARTSYS);
161 return rc;
162 }
163
164 if (be32_to_cpu(rpc_rsp.reply_stat) != RPCMSG_REPLYSTAT_ACCEPTED) {
165 MM_ERR("RPC call was denied!\n");
166 return -EPERM;
167 }
168
169 if (be32_to_cpu(rpc_rsp.data.acc_hdr.accept_stat) !=
170 RPC_ACCEPTSTAT_SUCCESS) {
171 MM_ERR("RPC call was not successful (%d)\n",
172 be32_to_cpu(rpc_rsp.data.acc_hdr.accept_stat));
173 return -EINVAL;
174 }
175
176 return 0;
177}
178
179static int get_module_index(uint32_t id)
180{
181 int mod_idx;
182 for (mod_idx = 0; mod_idx < adsp_info.module_count; mod_idx++)
183 if (adsp_info.module[mod_idx].id == id)
184 return mod_idx;
185
186 return -ENXIO;
187}
188
189static struct msm_adsp_module *find_adsp_module_by_id(
190 struct adsp_info *info, uint32_t id)
191{
192 int mod_idx;
193
194 if (id > info->max_module_id) {
195 return NULL;
196 } else {
197 mod_idx = get_module_index(id);
198 if (mod_idx < 0)
199 return NULL;
200 return info->id_to_module[mod_idx];
201 }
202}
203
204static struct msm_adsp_module *find_adsp_module_by_name(
205 struct adsp_info *info, const char *name)
206{
207 unsigned n;
208 for (n = 0; n < info->module_count; n++)
209 if (!strcmp(name, adsp_modules[n].name))
210 return adsp_modules + n;
211 return NULL;
212}
213
214static int adsp_rpc_init(struct msm_adsp_module *adsp_module)
215{
216 /* remove the original connect once compatible support is complete */
217 adsp_module->rpc_client = msm_rpc_connect(
218 rpc_adsp_rtos_atom_prog,
219 rpc_adsp_rtos_atom_vers,
220 MSM_RPC_UNINTERRUPTIBLE);
221 if (IS_ERR(adsp_module->rpc_client))
222 adsp_module->rpc_client = msm_rpc_connect_compatible(
223 rpc_adsp_rtos_atom_prog,
224 rpc_adsp_rtos_atom_vers_comp,
225 MSM_RPC_UNINTERRUPTIBLE);
226
227 if (IS_ERR(adsp_module->rpc_client)) {
228 int rc = PTR_ERR(adsp_module->rpc_client);
229 adsp_module->rpc_client = 0;
230 MM_ERR("could not open rpc client: %d\n", rc);
231 return rc;
232 }
233
234 return 0;
235}
236
237/*
238 * Send RPC_ADSP_RTOS_CMD_GET_INIT_INFO cmd to ARM9 and get
239 * queue offsets and module entries (init info) as part of the event.
240 */
241static void msm_get_init_info(void)
242{
243 int rc;
244 struct rpc_adsp_rtos_app_to_modem_args_t rpc_req;
245 struct rpc_reply_hdr rpc_rsp;
246
247 adsp_info.init_info_rpc_client = msm_rpc_connect(
248 rpc_adsp_rtos_atom_prog,
249 rpc_adsp_rtos_atom_vers,
250 MSM_RPC_UNINTERRUPTIBLE);
251 if (IS_ERR(adsp_info.init_info_rpc_client)) {
252 adsp_info.init_info_rpc_client = msm_rpc_connect_compatible(
253 rpc_adsp_rtos_atom_prog,
254 rpc_adsp_rtos_atom_vers_comp,
255 MSM_RPC_UNINTERRUPTIBLE);
256 if (IS_ERR(adsp_info.init_info_rpc_client)) {
257 rc = PTR_ERR(adsp_info.init_info_rpc_client);
258 adsp_info.init_info_rpc_client = 0;
259 MM_ERR("could not open rpc client: %d\n", rc);
260 return;
261 }
262 }
263
264 rpc_req.gotit = cpu_to_be32(1);
265 rpc_req.cmd = cpu_to_be32(RPC_ADSP_RTOS_CMD_GET_INIT_INFO);
266 rpc_req.proc_id = cpu_to_be32(RPC_ADSP_RTOS_PROC_APPS);
267 rpc_req.module = 0;
268
269 rc = msm_rpc_call_reply(adsp_info.init_info_rpc_client,
270 RPC_ADSP_RTOS_APP_TO_MODEM_PROC,
271 &rpc_req, sizeof(rpc_req),
272 &rpc_rsp, sizeof(rpc_rsp),
273 5 * HZ);
274
275 if (rc < 0)
276 MM_ERR("could not send RPC request: %d\n", rc);
277}
278
279int msm_adsp_get(const char *name, struct msm_adsp_module **out,
280 struct msm_adsp_ops *ops, void *driver_data)
281{
282 struct msm_adsp_module *module;
283 int rc = 0;
284 static uint32_t init_info_cmd_sent;
285
Manish Dewangan691f1c42012-02-10 12:50:14 +0530286 mutex_lock(&adsp_info.lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 if (!init_info_cmd_sent) {
288 init_waitqueue_head(&adsp_info.init_info_wait);
289 msm_get_init_info();
290 rc = wait_event_timeout(adsp_info.init_info_wait,
291 adsp_info.init_info_state == ADSP_STATE_INIT_INFO,
292 5 * HZ);
293 if (!rc) {
294 MM_ERR("INIT_INFO failed\n");
Manish Dewangan691f1c42012-02-10 12:50:14 +0530295 mutex_unlock(&adsp_info.lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296 return -ETIMEDOUT;
Manish Dewangan691f1c42012-02-10 12:50:14 +0530297
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 }
299 init_info_cmd_sent++;
300 }
Manish Dewangan691f1c42012-02-10 12:50:14 +0530301 mutex_unlock(&adsp_info.lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302
303 module = find_adsp_module_by_name(&adsp_info, name);
304 if (!module)
305 return -ENODEV;
306
307 mutex_lock(&module->lock);
308 MM_INFO("opening module %s\n", module->name);
309
310 if (module->ops) {
311 rc = -EBUSY;
312 goto done;
313 }
314
315 rc = adsp_rpc_init(module);
316 if (rc)
317 goto done;
318
319 module->ops = ops;
320 module->driver_data = driver_data;
321 *out = module;
322 rc = rpc_adsp_rtos_app_to_modem(RPC_ADSP_RTOS_CMD_REGISTER_APP,
323 module->id, module);
324 if (rc) {
325 module->ops = NULL;
326 module->driver_data = NULL;
327 *out = NULL;
328 MM_ERR("REGISTER_APP failed\n");
329 goto done;
330 }
331
332 MM_DBG("module %s has been registered\n", module->name);
333
334done:
335 mutex_unlock(&module->lock);
336 return rc;
337}
338EXPORT_SYMBOL(msm_adsp_get);
339
340static int msm_adsp_disable_locked(struct msm_adsp_module *module);
341
342void msm_adsp_put(struct msm_adsp_module *module)
343{
344 unsigned long flags;
345
346 mutex_lock(&module->lock);
347 if (module->ops) {
348 MM_INFO("closing module %s\n", module->name);
349
350 /* lock to ensure a dsp event cannot be delivered
351 * during or after removal of the ops and driver_data
352 */
353 spin_lock_irqsave(&adsp_cmd_lock, flags);
354 module->ops = NULL;
355 module->driver_data = NULL;
356 spin_unlock_irqrestore(&adsp_cmd_lock, flags);
357
358 if (module->state != ADSP_STATE_DISABLED) {
359 MM_INFO("disabling module %s\n", module->name);
360 msm_adsp_disable_locked(module);
361 }
362
363 msm_rpc_close(module->rpc_client);
364 module->rpc_client = 0;
365 } else {
366 MM_INFO("module %s is already closed\n", module->name);
367 }
368 mutex_unlock(&module->lock);
369}
370EXPORT_SYMBOL(msm_adsp_put);
371
372/* this should be common code with rpc_servers.c */
373static int rpc_send_accepted_void_reply(struct msm_rpc_endpoint *client,
374 uint32_t xid, uint32_t accept_status)
375{
376 int rc = 0;
377 uint8_t reply_buf[sizeof(struct rpc_reply_hdr)];
378 struct rpc_reply_hdr *reply = (struct rpc_reply_hdr *)reply_buf;
379
380 reply->xid = cpu_to_be32(xid);
381 reply->type = cpu_to_be32(1); /* reply */
382 reply->reply_stat = cpu_to_be32(RPCMSG_REPLYSTAT_ACCEPTED);
383
384 reply->data.acc_hdr.accept_stat = cpu_to_be32(accept_status);
385 reply->data.acc_hdr.verf_flavor = 0;
386 reply->data.acc_hdr.verf_length = 0;
387
388 rc = msm_rpc_write(rpc_cb_server_client, reply_buf, sizeof(reply_buf));
389 if (rc < 0)
390 MM_ERR("could not write RPC response: %d\n", rc);
391 return rc;
392}
393
394int __msm_adsp_write(struct msm_adsp_module *module, unsigned dsp_queue_addr,
395 void *cmd_buf, size_t cmd_size)
396{
397 uint32_t ctrl_word;
398 uint32_t dsp_q_addr;
399 uint32_t dsp_addr;
400 uint32_t cmd_id = 0;
401 int cnt = 0;
402 int ret_status = 0;
403 unsigned long flags;
404 struct adsp_info *info;
405
406 if (!module || !cmd_buf) {
407 MM_ERR("Called with NULL parameters\n");
408 return -EINVAL;
409 }
410 info = module->info;
411 spin_lock_irqsave(&adsp_write_lock, flags);
412
413 if (module->state != ADSP_STATE_ENABLED) {
414 spin_unlock_irqrestore(&adsp_write_lock, flags);
415 MM_ERR("module %s not enabled before write\n", module->name);
416 return -ENODEV;
417 }
418 if (adsp_validate_module(module->id)) {
419 spin_unlock_irqrestore(&adsp_write_lock, flags);
420 MM_ERR("module id validation failed %s %d\n",
421 module->name, module->id);
422 return -ENXIO;
423 }
424 if (dsp_queue_addr >= QDSP_MAX_NUM_QUEUES) {
425 spin_unlock_irqrestore(&adsp_write_lock, flags);
426 MM_ERR("Invalid Queue Index: %d\n", dsp_queue_addr);
427 return -ENXIO;
428 }
429 if (adsp_validate_queue(module->id, dsp_queue_addr, cmd_size)) {
430 spin_unlock_irqrestore(&adsp_write_lock, flags);
431 return -EINVAL;
432 }
433 dsp_q_addr = adsp_get_queue_offset(info, dsp_queue_addr);
434 dsp_q_addr &= ADSP_RTOS_WRITE_CTRL_WORD_DSP_ADDR_M;
435
436 /* Poll until the ADSP is ready to accept a command.
437 * Wait for 100us, return error if it's not responding.
438 * If this returns an error, we need to disable ALL modules and
439 * then retry.
440 */
441 while (((ctrl_word = readl(info->write_ctrl)) &
442 ADSP_RTOS_WRITE_CTRL_WORD_READY_M) !=
443 ADSP_RTOS_WRITE_CTRL_WORD_READY_V) {
444 if (cnt > 50) {
445 MM_ERR("timeout waiting for DSP write ready\n");
446 ret_status = -EIO;
447 goto fail;
448 }
449 MM_DBG("waiting for DSP write ready\n");
450 udelay(2);
451 cnt++;
452 }
453
454 /* Set the mutex bits */
455 ctrl_word &= ~(ADSP_RTOS_WRITE_CTRL_WORD_MUTEX_M);
456 ctrl_word |= ADSP_RTOS_WRITE_CTRL_WORD_MUTEX_NAVAIL_V;
457
458 /* Clear the command bits */
459 ctrl_word &= ~(ADSP_RTOS_WRITE_CTRL_WORD_CMD_M);
460
461 /* Set the queue address bits */
462 ctrl_word &= ~(ADSP_RTOS_WRITE_CTRL_WORD_DSP_ADDR_M);
463 ctrl_word |= dsp_q_addr;
464
465 writel(ctrl_word, info->write_ctrl);
466
467 /* Generate an interrupt to the DSP. This notifies the DSP that
468 * we are about to send a command on this particular queue. The
469 * DSP will in response change its state.
470 */
471 writel(1, info->send_irq);
472
473 /* Poll until the adsp responds to the interrupt; this does not
474 * generate an interrupt from the adsp. This should happen within
475 * 5ms.
476 */
477 cnt = 0;
478 while ((readl(info->write_ctrl) &
479 ADSP_RTOS_WRITE_CTRL_WORD_MUTEX_M) ==
480 ADSP_RTOS_WRITE_CTRL_WORD_MUTEX_NAVAIL_V) {
481 if (cnt > 2500) {
482 MM_ERR("timeout waiting for adsp ack\n");
483 ret_status = -EIO;
484 goto fail;
485 }
486 udelay(2);
487 cnt++;
488 }
489
490 /* Read the ctrl word */
491 ctrl_word = readl(info->write_ctrl);
492
493 if ((ctrl_word & ADSP_RTOS_WRITE_CTRL_WORD_STATUS_M) !=
494 ADSP_RTOS_WRITE_CTRL_WORD_NO_ERR_V) {
495 ret_status = -EAGAIN;
496 goto fail;
497 } else {
498 /* No error */
499 /* Get the DSP buffer address */
500 dsp_addr = (ctrl_word & ADSP_RTOS_WRITE_CTRL_WORD_DSP_ADDR_M) +
501 (uint32_t)MSM_AD5_BASE;
502
503 if (dsp_addr < (uint32_t)(MSM_AD5_BASE + QDSP_RAMC_OFFSET)) {
504 uint16_t *buf_ptr = (uint16_t *) cmd_buf;
505 uint16_t *dsp_addr16 = (uint16_t *)dsp_addr;
506 cmd_size /= sizeof(uint16_t);
507
508 /* Save the command ID */
509 cmd_id = (uint32_t) buf_ptr[0];
510
511 /* Copy the command to DSP memory */
512 cmd_size++;
513 while (--cmd_size)
514 *dsp_addr16++ = *buf_ptr++;
515 } else {
516 uint32_t *buf_ptr = (uint32_t *) cmd_buf;
517 uint32_t *dsp_addr32 = (uint32_t *)dsp_addr;
518 cmd_size /= sizeof(uint32_t);
519
520 /* Save the command ID */
521 cmd_id = buf_ptr[0];
522
523 cmd_size++;
524 while (--cmd_size)
525 *dsp_addr32++ = *buf_ptr++;
526 }
527
528 /* Set the mutex bits */
529 ctrl_word &= ~(ADSP_RTOS_WRITE_CTRL_WORD_MUTEX_M);
530 ctrl_word |= ADSP_RTOS_WRITE_CTRL_WORD_MUTEX_NAVAIL_V;
531
532 /* Set the command bits to write done */
533 ctrl_word &= ~(ADSP_RTOS_WRITE_CTRL_WORD_CMD_M);
534 ctrl_word |= ADSP_RTOS_WRITE_CTRL_WORD_CMD_WRITE_DONE_V;
535
536 /* Set the queue address bits */
537 ctrl_word &= ~(ADSP_RTOS_WRITE_CTRL_WORD_DSP_ADDR_M);
538 ctrl_word |= dsp_q_addr;
539
540 writel(ctrl_word, info->write_ctrl);
541
542 /* Generate an interrupt to the DSP. It does not respond with
543 * an interrupt, and we do not need to wait for it to
544 * acknowledge, because it will hold the mutex lock until it's
545 * ready to receive more commands again.
546 */
547 writel(1, info->send_irq);
548
549 module->num_commands++;
550 } /* Ctrl word status bits were 00, no error in the ctrl word */
551
552fail:
553 spin_unlock_irqrestore(&adsp_write_lock, flags);
554 return ret_status;
555}
556EXPORT_SYMBOL(msm_adsp_write);
557
558int msm_adsp_write(struct msm_adsp_module *module, unsigned dsp_queue_addr,
559 void *cmd_buf, size_t cmd_size)
560{
561 int rc, retries = 0;
562#ifdef CONFIG_DEBUG_FS
563 uint16_t *ptr;
564 int ii;
565
566 if (wdump > 0) {
567 ptr = cmd_buf;
568 pr_info("A->D:%x\n", module->id);
569 pr_info("adsp: %x %d\n", dsp_queue_addr, cmd_size);
570 for (ii = 0; ii < cmd_size/2; ii++)
571 pr_info("%x ", ptr[ii]);
572 pr_info("\n");
573 }
574#endif /* CONFIG_DEBUG_FS */
575 do {
576 rc = __msm_adsp_write(module, dsp_queue_addr, cmd_buf,
577 cmd_size);
578 if (rc == -EAGAIN)
579 udelay(10);
580 } while (rc == -EAGAIN && retries++ < 300);
581 if (retries > 50)
582 MM_ERR("adsp: %s command took %d attempts: rc %d\n",
583 module->name, retries, rc);
584 return rc;
585}
586
587static void *event_addr;
588static void read_event(void *buf, size_t len)
589{
590 uint32_t dptr[3];
591 struct rpc_adsp_rtos_modem_to_app_args_t *sptr;
592 struct adsp_rtos_mp_mtoa_type *pkt_ptr;
593
594 sptr = event_addr;
595 pkt_ptr = &sptr->mtoa_pkt.adsp_rtos_mp_mtoa_data.mp_mtoa_packet;
596
597 dptr[0] = be32_to_cpu(sptr->mtoa_pkt.mp_mtoa_header.event);
598 dptr[1] = be32_to_cpu(pkt_ptr->module);
599 dptr[2] = be32_to_cpu(pkt_ptr->image);
600
601 if (len > EVENT_LEN)
602 len = EVENT_LEN;
603
604 memcpy(buf, dptr, len);
605}
606
607static void handle_adsp_rtos_mtoa_app(struct rpc_request_hdr *req)
608{
609 struct rpc_adsp_rtos_modem_to_app_args_t *args =
610 (struct rpc_adsp_rtos_modem_to_app_args_t *)req;
611 uint32_t event;
612 uint32_t proc_id;
613 uint32_t module_id;
614 uint32_t image;
615 struct msm_adsp_module *module;
616 struct adsp_rtos_mp_mtoa_type *pkt_ptr;
617 struct queue_to_offset_type *qptr;
618 struct queue_to_offset_type *qtbl;
619 struct mod_to_queue_offsets *mqptr;
620 struct mod_to_queue_offsets *mqtbl;
621 uint32_t *mptr;
622 uint32_t *mtbl;
623 uint32_t q_idx;
624 uint32_t num_entries;
625 uint32_t entries_per_image;
626 struct adsp_rtos_mp_mtoa_init_info_type *iptr;
627 struct adsp_rtos_mp_mtoa_init_info_type *sptr;
628 int32_t i_no, e_idx;
629
630 event = be32_to_cpu(args->mtoa_pkt.mp_mtoa_header.event);
631 proc_id = be32_to_cpu(args->mtoa_pkt.mp_mtoa_header.proc_id);
632
633 if (event == RPC_ADSP_RTOS_INIT_INFO) {
634 MM_INFO("INIT_INFO Event\n");
635 sptr = &args->mtoa_pkt.adsp_rtos_mp_mtoa_data.mp_mtoa_init_packet;
636
637 iptr = adsp_info.init_info_ptr;
638 iptr->image_count = be32_to_cpu(sptr->image_count);
639 if (iptr->image_count > IMG_MAX)
640 iptr->image_count = IMG_MAX;
641 iptr->num_queue_offsets = be32_to_cpu(sptr->num_queue_offsets);
642 num_entries = iptr->num_queue_offsets;
643 if (num_entries > ENTRIES_MAX) {
644 num_entries = ENTRIES_MAX;
645 iptr->num_queue_offsets = ENTRIES_MAX;
646 }
647 qptr = &sptr->queue_offsets_tbl[0][0];
648 for (i_no = 0; i_no < iptr->image_count; i_no++) {
649 qtbl = &iptr->queue_offsets_tbl[i_no][0];
650 for (e_idx = 0; e_idx < num_entries; e_idx++) {
651 qtbl[e_idx].offset = be32_to_cpu(qptr->offset);
652 qtbl[e_idx].queue = be32_to_cpu(qptr->queue);
653 q_idx = be32_to_cpu(qptr->queue);
654 iptr->queue_offsets[i_no][q_idx] = qtbl[e_idx].offset;
655 qptr++;
656 }
657 }
658
659 num_entries = be32_to_cpu(sptr->num_task_module_entries);
660 if (num_entries > ENTRIES_MAX)
661 num_entries = ENTRIES_MAX;
662 iptr->num_task_module_entries = num_entries;
663 entries_per_image = num_entries / iptr->image_count;
664 mptr = &sptr->task_to_module_tbl[0][0];
665 for (i_no = 0; i_no < iptr->image_count; i_no++) {
666 mtbl = &iptr->task_to_module_tbl[i_no][0];
667 for (e_idx = 0; e_idx < entries_per_image; e_idx++) {
668 mtbl[e_idx] = be32_to_cpu(*mptr);
669 mptr++;
670 }
671 }
672
673 iptr->module_table_size = be32_to_cpu(sptr->module_table_size);
674#if CONFIG_ADSP_RPC_VER > 0x30001
675 if (iptr->module_table_size > MODULES_MAX)
676 iptr->module_table_size = MODULES_MAX;
677#else
678 if (iptr->module_table_size > ENTRIES_MAX)
679 iptr->module_table_size = ENTRIES_MAX;
680#endif
681 mptr = &sptr->module_entries[0];
682 for (i_no = 0; i_no < iptr->module_table_size; i_no++)
683 iptr->module_entries[i_no] = be32_to_cpu(mptr[i_no]);
684
685 mqptr = &sptr->mod_to_q_tbl[0];
686 mqtbl = &iptr->mod_to_q_tbl[0];
687 iptr->mod_to_q_entries = be32_to_cpu(sptr->mod_to_q_entries);
688 if (iptr->mod_to_q_entries > ENTRIES_MAX)
689 iptr->mod_to_q_entries = ENTRIES_MAX;
690 for (e_idx = 0; e_idx < iptr->mod_to_q_entries; e_idx++) {
691 mqtbl[e_idx].module = be32_to_cpu(mqptr->module);
692 mqtbl[e_idx].q_type = be32_to_cpu(mqptr->q_type);
693 mqtbl[e_idx].q_max_len = be32_to_cpu(mqptr->q_max_len);
694 mqptr++;
695 }
696
697 adsp_info.init_info_state = ADSP_STATE_INIT_INFO;
698 rpc_send_accepted_void_reply(rpc_cb_server_client, req->xid,
699 RPC_ACCEPTSTAT_SUCCESS);
700 wake_up(&adsp_info.init_info_wait);
701
702 return;
703 }
704
705 pkt_ptr = &args->mtoa_pkt.adsp_rtos_mp_mtoa_data.mp_mtoa_packet;
706 module_id = be32_to_cpu(pkt_ptr->module);
707 image = be32_to_cpu(pkt_ptr->image);
708
709 MM_DBG("rpc event=%d, proc_id=%d, module=%d, image=%d\n",
710 event, proc_id, module_id, image);
711
712 module = find_adsp_module_by_id(&adsp_info, module_id);
713 if (!module) {
714 MM_ERR("module %d is not supported!\n", module_id);
715 rpc_send_accepted_void_reply(rpc_cb_server_client, req->xid,
716 RPC_ACCEPTSTAT_GARBAGE_ARGS);
717 return;
718 }
719
720 mutex_lock(&module->lock);
721 switch (event) {
722 case RPC_ADSP_RTOS_MOD_READY:
Laxminath Kasam46499192012-09-04 12:00:11 +0530723 if (module->state == ADSP_STATE_ENABLING) {
724 MM_INFO("module %s: READY\n", module->name);
725 module->state = ADSP_STATE_ENABLED;
726 wake_up(&module->state_wait);
727 adsp_set_image(module->info, image);
728 break;
729 } else {
730 MM_ERR("module %s got READY event in state[%d]\n",
731 module->name,
732 module->state);
733 rpc_send_accepted_void_reply(rpc_cb_server_client,
734 req->xid,
735 RPC_ACCEPTSTAT_GARBAGE_ARGS);
736 mutex_unlock(&module->lock);
737 return;
738 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 case RPC_ADSP_RTOS_MOD_DISABLE:
740 MM_INFO("module %s: DISABLED\n", module->name);
741 module->state = ADSP_STATE_DISABLED;
742 wake_up(&module->state_wait);
743 break;
744 case RPC_ADSP_RTOS_SERVICE_RESET:
745 MM_INFO("module %s: SERVICE_RESET\n", module->name);
746 module->state = ADSP_STATE_DISABLED;
747 wake_up(&module->state_wait);
748 break;
749 case RPC_ADSP_RTOS_CMD_SUCCESS:
750 MM_INFO("module %s: CMD_SUCCESS\n", module->name);
751 break;
752 case RPC_ADSP_RTOS_CMD_FAIL:
753 MM_INFO("module %s: CMD_FAIL\n", module->name);
754 break;
755 case RPC_ADSP_RTOS_DISABLE_FAIL:
756 MM_INFO("module %s: DISABLE_FAIL\n", module->name);
757 break;
758 default:
759 MM_ERR("unknown event %d\n", event);
760 rpc_send_accepted_void_reply(rpc_cb_server_client, req->xid,
761 RPC_ACCEPTSTAT_GARBAGE_ARGS);
762 mutex_unlock(&module->lock);
763 return;
764 }
765 rpc_send_accepted_void_reply(rpc_cb_server_client, req->xid,
766 RPC_ACCEPTSTAT_SUCCESS);
767#ifdef CONFIG_MSM_ADSP_REPORT_EVENTS
768 event_addr = (uint32_t *)req;
769 module->ops->event(module->driver_data,
770 EVENT_MSG_ID,
771 EVENT_LEN,
772 read_event);
773#endif
774 mutex_unlock(&module->lock);
775}
776
777static int handle_adsp_rtos_mtoa(struct rpc_request_hdr *req)
778{
779 switch (req->procedure) {
780 case RPC_ADSP_RTOS_MTOA_NULL_PROC:
781 rpc_send_accepted_void_reply(rpc_cb_server_client,
782 req->xid,
783 RPC_ACCEPTSTAT_SUCCESS);
784 break;
785#if CONFIG_ADSP_RPC_VER > 0x30001
786 case RPC_ADSP_RTOS_MTOA_INIT_INFO_PROC:
787 case RPC_ADSP_RTOS_MTOA_EVENT_INFO_PROC:
788#else
789 case RPC_ADSP_RTOS_MODEM_TO_APP_PROC:
790#endif
791 handle_adsp_rtos_mtoa_app(req);
792 break;
793 default:
794 MM_ERR("unknowned proc %d\n", req->procedure);
795 rpc_send_accepted_void_reply(
796 rpc_cb_server_client, req->xid,
797 RPC_ACCEPTSTAT_PROC_UNAVAIL);
798 break;
799 }
800 return 0;
801}
802
803/* this should be common code with rpc_servers.c */
804static int adsp_rpc_thread(void *data)
805{
806 void *buffer;
807 struct rpc_request_hdr *req;
808 int rc, exit = 0;
809
810 do {
811 rc = msm_rpc_read(rpc_cb_server_client, &buffer, -1, -1);
812 if (rc < 0) {
813 MM_ERR("could not read rpc: %d\n", rc);
814 break;
815 }
816 req = (struct rpc_request_hdr *)buffer;
817
818 req->type = be32_to_cpu(req->type);
819 req->xid = be32_to_cpu(req->xid);
820 req->rpc_vers = be32_to_cpu(req->rpc_vers);
821 req->prog = be32_to_cpu(req->prog);
822 req->vers = be32_to_cpu(req->vers);
823 req->procedure = be32_to_cpu(req->procedure);
824
825 if (req->type != 0)
826 goto bad_rpc;
827 if (req->rpc_vers != 2)
828 goto bad_rpc;
829 if (req->prog != rpc_adsp_rtos_mtoa_prog)
830 goto bad_rpc;
831 if (!msm_rpc_is_compatible_version(rpc_adsp_rtos_mtoa_vers,
832 req->vers))
833 goto bad_rpc;
834
835 handle_adsp_rtos_mtoa(req);
836 kfree(buffer);
837 continue;
838
839bad_rpc:
840 MM_ERR("bogus rpc from modem\n");
841 kfree(buffer);
842 } while (!exit);
843 do_exit(0);
844}
845
846static size_t read_event_size;
847static void *read_event_addr;
848
849static void read_event_16(void *buf, size_t len)
850{
851 uint16_t *dst = buf;
852 uint16_t *src = read_event_addr;
853 len /= 2;
854 if (len > read_event_size)
855 len = read_event_size;
856 while (len--)
857 *dst++ = *src++;
858}
859
860static void read_event_32(void *buf, size_t len)
861{
862 uint32_t *dst = buf;
863 uint32_t *src = read_event_addr;
864 len /= 2;
865 if (len > read_event_size)
866 len = read_event_size;
867 while (len--)
868 *dst++ = *src++;
869}
870
871static int adsp_rtos_read_ctrl_word_cmd_tast_to_h_v(
872 struct adsp_info *info, void *dsp_addr)
873{
874 struct msm_adsp_module *module;
875 unsigned rtos_task_id;
876 unsigned msg_id;
877 unsigned msg_length;
878#ifdef CONFIG_DEBUG_FS
Laxminath Kasam6ebf8902012-06-06 16:08:17 +0530879 uint16_t *ptr16;
880 uint32_t *ptr32;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881 int ii;
882#endif /* CONFIG_DEBUG_FS */
883 void (*func)(void *, size_t);
884
885 if (dsp_addr >= (void *)(MSM_AD5_BASE + QDSP_RAMC_OFFSET)) {
886 uint32_t *dsp_addr32 = dsp_addr;
887 uint32_t tmp = *dsp_addr32++;
888 rtos_task_id = (tmp & ADSP_RTOS_READ_CTRL_WORD_TASK_ID_M) >> 8;
889 msg_id = (tmp & ADSP_RTOS_READ_CTRL_WORD_MSG_ID_M);
890 read_event_size = tmp >> 16;
891 read_event_addr = dsp_addr32;
892 msg_length = read_event_size * sizeof(uint32_t);
893 func = read_event_32;
894 } else {
895 uint16_t *dsp_addr16 = dsp_addr;
896 uint16_t tmp = *dsp_addr16++;
897 rtos_task_id = (tmp & ADSP_RTOS_READ_CTRL_WORD_TASK_ID_M) >> 8;
898 msg_id = tmp & ADSP_RTOS_READ_CTRL_WORD_MSG_ID_M;
899 read_event_size = *dsp_addr16++;
900 read_event_addr = dsp_addr16;
901 msg_length = read_event_size * sizeof(uint16_t);
902 func = read_event_16;
903 }
904
905 if (rtos_task_id > info->max_task_id) {
906 MM_ERR("bogus task id %d\n", rtos_task_id);
907 return 0;
908 }
909 module = find_adsp_module_by_id(info,
910 adsp_get_module(info, rtos_task_id));
911
912 if (!module) {
913 MM_ERR("no module for task id %d\n", rtos_task_id);
914 return 0;
915 }
916
917 module->num_events++;
918
919 if (!module->ops) {
920 MM_ERR("module %s is not open\n", module->name);
921 return 0;
922 }
923#ifdef CONFIG_DEBUG_FS
Laxminath Kasam6ebf8902012-06-06 16:08:17 +0530924 if (rdump > 0 &&
925 (dsp_addr >= (void *)(MSM_AD5_BASE + QDSP_RAMC_OFFSET))) {
926 ptr32 = read_event_addr;
927 pr_info("D->A\n");
928 pr_info("m_id = %x id = %x\n", module->id, msg_id);
929 for (ii = 0; ii < msg_length/4; ii++)
930 pr_info("%x ", ptr32[ii]);
931 pr_info("\n");
932 } else if (rdump > 0) {
933 ptr16 = read_event_addr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700934 pr_info("D->A\n");
935 pr_info("m_id = %x id = %x\n", module->id, msg_id);
936 for (ii = 0; ii < msg_length/2; ii++)
Laxminath Kasam6ebf8902012-06-06 16:08:17 +0530937 pr_info("%x ", ptr16[ii]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938 pr_info("\n");
939 }
940#endif /* CONFIG_DEBUG_FS */
941
942 module->ops->event(module->driver_data, msg_id, msg_length, func);
943 return 0;
944}
945
946static int adsp_get_event(struct adsp_info *info)
947{
948 uint32_t ctrl_word;
949 uint32_t ready;
950 void *dsp_addr;
951 uint32_t cmd_type;
952 int cnt;
953 unsigned long flags;
954 int rc = 0;
955
956 spin_lock_irqsave(&adsp_cmd_lock, flags);
957
958 /* Whenever the DSP has a message, it updates this control word
959 * and generates an interrupt. When we receive the interrupt, we
960 * read this register to find out what ADSP task the command is
961 * comming from.
962 *
963 * The ADSP should *always* be ready on the first call, but the
964 * irq handler calls us in a loop (to handle back-to-back command
965 * processing), so we give the DSP some time to return to the
966 * ready state. The DSP will not issue another IRQ for events
967 * pending between the first IRQ and the event queue being drained,
968 * unfortunately.
969 */
970
971 for (cnt = 0; cnt < 50; cnt++) {
972 ctrl_word = readl(info->read_ctrl);
973
974 if ((ctrl_word & ADSP_RTOS_READ_CTRL_WORD_FLAG_M) ==
975 ADSP_RTOS_READ_CTRL_WORD_FLAG_UP_CONT_V)
976 goto ready;
977
978 udelay(2);
979 }
980 MM_ERR("not ready after 100uS\n");
981 rc = -EBUSY;
982 goto done;
983
984ready:
985 /* Here we check to see if there are pending messages. If there are
986 * none, we siply return -EAGAIN to indicate that there are no more
987 * messages pending.
988 */
989 ready = ctrl_word & ADSP_RTOS_READ_CTRL_WORD_READY_M;
990 if ((ready != ADSP_RTOS_READ_CTRL_WORD_READY_V) &&
991 (ready != ADSP_RTOS_READ_CTRL_WORD_CONT_V)) {
992 rc = -EAGAIN;
993 goto done;
994 }
995
996 /* DSP says that there are messages waiting for the host to read */
997
998 /* Get the Command Type */
999 cmd_type = ctrl_word & ADSP_RTOS_READ_CTRL_WORD_CMD_TYPE_M;
1000
1001 /* Get the DSP buffer address */
1002 dsp_addr = (void *)((ctrl_word &
1003 ADSP_RTOS_READ_CTRL_WORD_DSP_ADDR_M) +
1004 (uint32_t)MSM_AD5_BASE);
1005
1006 /* We can only handle Task-to-Host messages */
1007 if (cmd_type != ADSP_RTOS_READ_CTRL_WORD_CMD_TASK_TO_H_V) {
1008 MM_ERR("unknown dsp cmd_type %d\n", cmd_type);
1009 rc = -EIO;
1010 goto done;
1011 }
1012
1013 adsp_rtos_read_ctrl_word_cmd_tast_to_h_v(info, dsp_addr);
1014
1015 ctrl_word = readl(info->read_ctrl);
1016 ctrl_word &= ~ADSP_RTOS_READ_CTRL_WORD_READY_M;
1017
1018 /* Write ctrl word to the DSP */
1019 writel(ctrl_word, info->read_ctrl);
1020
1021 /* Generate an interrupt to the DSP */
1022 writel(1, info->send_irq);
1023
1024done:
1025 spin_unlock_irqrestore(&adsp_cmd_lock, flags);
1026 return rc;
1027}
1028
1029static irqreturn_t adsp_irq_handler(int irq, void *data)
1030{
1031 struct adsp_info *info = &adsp_info;
1032 int cnt = 0;
1033 for (cnt = 0; cnt < 15; cnt++)
1034 if (adsp_get_event(info) < 0)
1035 break;
1036 if (cnt > info->event_backlog_max)
1037 info->event_backlog_max = cnt;
1038 info->events_received += cnt;
1039 if (cnt == 15)
1040 MM_ERR("too many (%d) events for single irq!\n", cnt);
1041 return IRQ_HANDLED;
1042}
1043
1044int adsp_set_clkrate(struct msm_adsp_module *module, unsigned long clk_rate)
1045{
Manish Dewangan49037122012-02-10 18:25:25 +05301046 if (!module)
1047 return -EINVAL;
1048
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049 if (module->clk && clk_rate)
Matt Wagantallf13bee62011-11-08 15:36:32 -08001050 return clk_set_rate(module->clk, clk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001051
1052 return -EINVAL;
1053}
1054
Manish Dewangan8e87bc12012-02-09 20:25:15 +05301055int msm_adsp_generate_event(void *data,
1056 struct msm_adsp_module *mod,
1057 unsigned event_id,
1058 unsigned event_length,
1059 unsigned event_size,
1060 void *msg)
1061{
1062 unsigned long flags;
1063 void (*func)(void *, size_t);
1064
Manish Dewangan49037122012-02-10 18:25:25 +05301065 if (!mod)
1066 return -EINVAL;
1067
Manish Dewangan8e87bc12012-02-09 20:25:15 +05301068 if (event_size == sizeof(uint32_t))
1069 func = read_event_32;
1070 else if (event_size == sizeof(uint16_t))
1071 func = read_event_16;
1072 else
1073 return -EINVAL;
1074
1075 spin_lock_irqsave(&adsp_cmd_lock, flags);
1076 read_event_addr = msg;
Manish Dewangan68532ec2012-03-12 10:31:31 +05301077 read_event_size = event_length;
Manish Dewangan8e87bc12012-02-09 20:25:15 +05301078 mod->ops->event(data, event_id, event_length, func);
1079 spin_unlock_irqrestore(&adsp_cmd_lock, flags);
1080 return 0;
1081}
1082
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001083int msm_adsp_enable(struct msm_adsp_module *module)
1084{
1085 int rc = 0;
Laxminath Kasam047e8832012-07-25 16:48:15 +05301086 struct msm_adsp_module *module_en = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001087
Manish Dewangan49037122012-02-10 18:25:25 +05301088 if (!module)
1089 return -EINVAL;
1090
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091 MM_INFO("enable '%s'state[%d] id[%d]\n",
1092 module->name, module->state, module->id);
Laxminath Kasam047e8832012-07-25 16:48:15 +05301093 if (!strncmp(module->name, "JPEGTASK", sizeof(module->name)))
1094 module_en = find_adsp_module_by_name(&adsp_info, "VIDEOTASK");
1095 else if (!strncmp(module->name, "VIDEOTASK", sizeof(module->name)))
1096 module_en = find_adsp_module_by_name(&adsp_info, "JPEGTASK");
1097 if (module_en) {
1098 mutex_lock(&module_en->lock);
1099 if (module_en->state == ADSP_STATE_ENABLED ||
1100 module_en->state == ADSP_STATE_ENABLING) {
1101 MM_ERR("both jpeg and video module can't"\
1102 " exist at a time\n");
1103 mutex_unlock(&module_en->lock);
1104 return -EINVAL;
1105 }
1106 mutex_unlock(&module_en->lock);
1107 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108
1109 mutex_lock(&module->lock);
1110 switch (module->state) {
1111 case ADSP_STATE_DISABLED:
1112 rc = rpc_adsp_rtos_app_to_modem(RPC_ADSP_RTOS_CMD_ENABLE,
1113 module->id, module);
1114 if (rc)
1115 break;
1116 module->state = ADSP_STATE_ENABLING;
1117 mutex_unlock(&module->lock);
1118 rc = wait_event_timeout(module->state_wait,
1119 module->state != ADSP_STATE_ENABLING,
1120 1 * HZ);
1121 mutex_lock(&module->lock);
1122 if (module->state == ADSP_STATE_ENABLED) {
1123 rc = 0;
1124 } else {
1125 MM_ERR("module '%s' enable timed out\n", module->name);
1126 rc = -ETIMEDOUT;
1127 }
1128 if (module->open_count++ == 0 && module->clk)
Laxminath Kasame448b242012-03-21 13:50:19 +05301129 clk_prepare_enable(module->clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001130
1131 mutex_lock(&adsp_open_lock);
1132 if (adsp_open_count++ == 0) {
Laxminath Kasam34aea162012-02-15 12:21:49 +05301133 enable_irq(adsp_info.int_adsp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001134 prevent_suspend();
1135 }
1136 mutex_unlock(&adsp_open_lock);
1137 break;
1138 case ADSP_STATE_ENABLING:
1139 MM_DBG("module '%s' enable in progress\n", module->name);
1140 break;
1141 case ADSP_STATE_ENABLED:
1142 MM_DBG("module '%s' already enabled\n", module->name);
1143 break;
1144 case ADSP_STATE_DISABLING:
1145 MM_ERR("module '%s' disable in progress\n", module->name);
1146 rc = -EBUSY;
1147 break;
1148 }
1149 mutex_unlock(&module->lock);
1150 return rc;
1151}
1152EXPORT_SYMBOL(msm_adsp_enable);
1153
1154int msm_adsp_disable_event_rsp(struct msm_adsp_module *module)
1155{
1156 int rc = 0;
1157
Manish Dewangan49037122012-02-10 18:25:25 +05301158 if (!module)
1159 return -EINVAL;
1160
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161 mutex_lock(&module->lock);
1162
1163 rc = rpc_adsp_rtos_app_to_modem(RPC_ADSP_RTOS_CMD_DISABLE_EVENT_RSP,
1164 module->id, module);
1165 mutex_unlock(&module->lock);
1166
1167 return rc;
1168}
1169EXPORT_SYMBOL(msm_adsp_disable_event_rsp);
1170
1171static int msm_adsp_disable_locked(struct msm_adsp_module *module)
1172{
1173 int rc = 0;
1174
Manish Dewangan49037122012-02-10 18:25:25 +05301175 if (!module)
1176 return -EINVAL;
1177
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 switch (module->state) {
1179 case ADSP_STATE_DISABLED:
1180 MM_DBG("module '%s' already disabled\n", module->name);
1181 break;
1182 case ADSP_STATE_ENABLING:
1183 case ADSP_STATE_ENABLED:
1184 rc = rpc_adsp_rtos_app_to_modem(RPC_ADSP_RTOS_CMD_DISABLE,
1185 module->id, module);
1186 module->state = ADSP_STATE_DISABLED;
1187 if (--module->open_count == 0 && module->clk)
Laxminath Kasame448b242012-03-21 13:50:19 +05301188 clk_disable_unprepare(module->clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189 mutex_lock(&adsp_open_lock);
1190 if (--adsp_open_count == 0) {
Laxminath Kasam34aea162012-02-15 12:21:49 +05301191 disable_irq(adsp_info.int_adsp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001192 allow_suspend();
1193 MM_DBG("disable interrupt\n");
1194 }
1195 mutex_unlock(&adsp_open_lock);
1196 }
1197 return rc;
1198}
1199
1200int msm_adsp_disable(struct msm_adsp_module *module)
1201{
1202 int rc;
Manish Dewangan49037122012-02-10 18:25:25 +05301203
1204 if (!module)
1205 return -EINVAL;
1206
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 MM_INFO("disable '%s'\n", module->name);
1208 mutex_lock(&module->lock);
1209 rc = msm_adsp_disable_locked(module);
1210 mutex_unlock(&module->lock);
1211 return rc;
1212}
1213EXPORT_SYMBOL(msm_adsp_disable);
1214
1215static int msm_adsp_probe(struct platform_device *pdev)
1216{
1217 unsigned count;
1218 int rc, i;
1219
Laxminath Kasam34aea162012-02-15 12:21:49 +05301220 adsp_info.int_adsp = platform_get_irq(pdev, 0);
1221 if (adsp_info.int_adsp < 0) {
1222 MM_ERR("no irq resource?\n");
1223 return -ENODEV;
1224 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225
1226 wake_lock_init(&adsp_wake_lock, WAKE_LOCK_SUSPEND, "adsp");
1227 adsp_info.init_info_ptr = kzalloc(
1228 (sizeof(struct adsp_rtos_mp_mtoa_init_info_type)), GFP_KERNEL);
1229 if (!adsp_info.init_info_ptr)
1230 return -ENOMEM;
1231
1232 rc = adsp_init_info(&adsp_info);
1233 if (rc)
1234 return rc;
1235 adsp_info.send_irq += (uint32_t) MSM_AD5_BASE;
1236 adsp_info.read_ctrl += (uint32_t) MSM_AD5_BASE;
1237 adsp_info.write_ctrl += (uint32_t) MSM_AD5_BASE;
1238 count = adsp_info.module_count;
1239
1240 adsp_modules = kzalloc(
1241 (sizeof(struct msm_adsp_module) + sizeof(void *)) *
1242 count, GFP_KERNEL);
1243 if (!adsp_modules)
1244 return -ENOMEM;
1245
1246 adsp_info.id_to_module = (void *) (adsp_modules + count);
1247
1248 spin_lock_init(&adsp_cmd_lock);
1249 spin_lock_init(&adsp_write_lock);
Manish Dewangan691f1c42012-02-10 12:50:14 +05301250 mutex_init(&adsp_info.lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001251
Laxminath Kasam34aea162012-02-15 12:21:49 +05301252 rc = request_irq(adsp_info.int_adsp, adsp_irq_handler,
1253 IRQF_TRIGGER_RISING, "adsp", 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001254 if (rc < 0)
1255 goto fail_request_irq;
Laxminath Kasam34aea162012-02-15 12:21:49 +05301256 disable_irq(adsp_info.int_adsp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001257
1258 rpc_cb_server_client = msm_rpc_open();
1259 if (IS_ERR(rpc_cb_server_client)) {
1260 rpc_cb_server_client = NULL;
1261 rc = PTR_ERR(rpc_cb_server_client);
1262 MM_ERR("could not create rpc server (%d)\n", rc);
1263 goto fail_rpc_open;
1264 }
1265
1266 rc = msm_rpc_register_server(rpc_cb_server_client,
1267 rpc_adsp_rtos_mtoa_prog,
1268 rpc_adsp_rtos_mtoa_vers);
1269 if (rc) {
1270 MM_ERR("could not register callback server (%d)\n", rc);
1271 goto fail_rpc_register;
1272 }
1273
Chaithanya Krishna Bacharajua3931822012-05-09 13:44:07 +05301274 /* schedule start of kernel thread later using work queue */
1275 queue_work(msm_adsp_probe_work_queue, &msm_adsp_probe_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001276
1277 for (i = 0; i < count; i++) {
1278 struct msm_adsp_module *mod = adsp_modules + i;
1279 mutex_init(&mod->lock);
1280 init_waitqueue_head(&mod->state_wait);
1281 mod->info = &adsp_info;
1282 mod->name = adsp_info.module[i].name;
1283 mod->id = adsp_info.module[i].id;
1284 if (adsp_info.module[i].clk_name)
1285 mod->clk = clk_get(NULL, adsp_info.module[i].clk_name);
1286 else
1287 mod->clk = NULL;
1288 if (mod->clk && adsp_info.module[i].clk_rate)
Matt Wagantallf13bee62011-11-08 15:36:32 -08001289 clk_set_rate(mod->clk, adsp_info.module[i].clk_rate);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290 mod->verify_cmd = adsp_info.module[i].verify_cmd;
1291 mod->patch_event = adsp_info.module[i].patch_event;
Saikumar Kondaparthi316620f2012-06-26 15:43:22 +05301292 INIT_HLIST_HEAD(&mod->ion_regions);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293 mod->pdev.name = adsp_info.module[i].pdev_name;
1294 mod->pdev.id = -1;
1295 adsp_info.id_to_module[i] = mod;
1296 platform_device_register(&mod->pdev);
1297 }
1298
1299 msm_adsp_publish_cdevs(adsp_modules, count);
1300 rmtask_init();
1301
1302 return 0;
1303
1304fail_rpc_register:
1305 msm_rpc_close(rpc_cb_server_client);
1306 rpc_cb_server_client = NULL;
1307fail_rpc_open:
Laxminath Kasam34aea162012-02-15 12:21:49 +05301308 enable_irq(adsp_info.int_adsp);
1309 free_irq(adsp_info.int_adsp, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310fail_request_irq:
1311 kfree(adsp_modules);
1312 kfree(adsp_info.init_info_ptr);
1313 return rc;
1314}
Chaithanya Krishna Bacharajua3931822012-05-09 13:44:07 +05301315
1316static void adsp_probe_work(struct work_struct *work)
1317{
1318 /* start the kernel thread to process the callbacks */
1319 kthread_run(adsp_rpc_thread, NULL, "kadspd");
1320}
1321
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322#ifdef CONFIG_DEBUG_FS
1323static int get_parameters(char *buf, long int *param1, int num_of_par)
1324{
1325 char *token;
1326 int base, cnt;
1327
1328 token = strsep(&buf, " ");
1329
1330 for (cnt = 0; cnt < num_of_par; cnt++) {
1331 if (token != NULL) {
1332 if ((token[1] == 'x') || (token[1] == 'X'))
1333 base = 16;
1334 else
1335 base = 10;
1336
1337 if (strict_strtoul(token, base, &param1[cnt]) != 0)
1338 return -EINVAL;
1339
1340 token = strsep(&buf, " ");
1341 }
1342 else
1343 return -EINVAL;
1344 }
1345 return 0;
1346}
1347
1348
1349static ssize_t adsp_debug_open(struct inode *inode, struct file *file)
1350{
1351 file->private_data = inode->i_private;
1352 pr_debug("adsp debugfs opened\n");
1353 return 0;
1354}
1355static ssize_t adsp_debug_write(struct file *file, const char __user *buf,
1356 size_t cnt, loff_t *ppos)
1357{
1358 char *access_str = file->private_data;
1359 char lbuf[32];
1360 int rc;
1361 long int param[5];
1362
1363 if (cnt > sizeof(lbuf) - 1)
1364 return -EINVAL;
1365 rc = copy_from_user(lbuf, buf, cnt);
1366 if (rc) {
1367 pr_info("Unable to copy data from user space\n");
1368 return -EFAULT;
1369 }
1370 lbuf[cnt] = '\0';
1371
1372 if (!strcmp(access_str, "write_log")) {
1373 if (get_parameters(lbuf, param, 1) == 0) {
1374 switch (param[0]) {
1375 case 1:
1376 if (wdump <= 0)
1377 wdump = 1;
1378 pr_debug("write cmd to DSP(A->D) dump \
1379 started:%d\n", wdump);
1380 break;
1381 case 0:
1382 if (wdump > 0)
1383 wdump = 0;
1384 pr_debug("Stop write cmd to \
1385 DSP(A->D):%d\n", wdump);
1386 break;
1387 default:
1388 rc = -EINVAL;
1389 break;
1390 }
1391 } else
1392 rc = -EINVAL;
1393 } else if (!strcmp(access_str, "read_log")) {
1394 if (get_parameters(lbuf, param, 1) == 0) {
1395 switch (param[0]) {
1396 case 1:
1397 if (rdump <= 0)
1398 rdump = 1;
1399 pr_debug("write cmd from DSP(D->A) dump \
1400 started:%d\n", wdump);
1401 break;
1402 case 0:
1403 if (rdump > 0)
1404 rdump = 0;
1405 pr_debug("Stop write cmd from \
1406 DSP(D->A):%d\n", wdump);
1407 break;
1408 default:
1409 rc = -EINVAL;
1410 break;
1411 }
1412 } else
1413 rc = -EINVAL;
1414 } else {
1415 rc = -EINVAL;
1416 }
1417 if (rc == 0)
1418 rc = cnt;
1419 else {
1420 pr_err("%s: rc = %d\n", __func__, rc);
1421 pr_info("\nWrong command: Use =>\n");
1422 pr_info("-------------------------\n");
1423 pr_info("To Start A->D:: echo \"1\">/sys/kernel/debug/ \
1424 adsp_cmd/write_log\n");
1425 pr_info("To Start D->A:: echo \"1\">/sys/kernel/debug/ \
1426 adsp_cmd/read_log\n");
1427 pr_info("To Stop A->D:: echo \"0\">/sys/kernel/debug/ \
1428 adsp_cmd/write_log\n");
1429 pr_info("To Stop D->A:: echo \"0\">/sys/kernel/debug/ \
1430 adsp_cmd/read_log\n");
1431 pr_info("------------------------\n");
1432 }
1433
1434 return rc;
1435}
1436#endif
1437
1438static struct platform_driver msm_adsp_driver = {
1439 .probe = msm_adsp_probe,
1440 .driver = {
1441 .owner = THIS_MODULE,
1442 },
1443};
1444
Laxminath Kasam34aea162012-02-15 12:21:49 +05301445static const char msm_adsp_driver_name[] = "msm_adsp";
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001446
1447#ifdef CONFIG_DEBUG_FS
1448static const struct file_operations adsp_debug_fops = {
1449 .write = adsp_debug_write,
1450 .open = adsp_debug_open,
1451};
1452#endif
1453
1454static int __init adsp_init(void)
1455{
1456 int rc;
1457
1458#ifdef CONFIG_DEBUG_FS
1459 dentry_adsp = debugfs_create_dir("adsp_cmd", 0);
1460 if (!IS_ERR(dentry_adsp)) {
1461 dentry_wdata = debugfs_create_file("write_log", \
1462 S_IFREG | S_IRUGO, dentry_adsp,
1463 (void *) "write_log" , &adsp_debug_fops);
1464 dentry_rdata = debugfs_create_file("read_log", \
1465 S_IFREG | S_IRUGO, dentry_adsp,
1466 (void *) "read_log", &adsp_debug_fops);
1467 }
1468 rdump = 0;
1469 wdump = 0;
1470#endif /* CONFIG_DEBUG_FS */
1471
1472 rpc_adsp_rtos_atom_prog = 0x3000000a;
1473 rpc_adsp_rtos_atom_vers = 0x10001;
1474 rpc_adsp_rtos_atom_vers_comp = 0x00010001;
1475 rpc_adsp_rtos_mtoa_prog = 0x3000000b;
1476#if CONFIG_ADSP_RPC_VER > 0x30001
1477 rpc_adsp_rtos_mtoa_vers = 0x30002;
1478 rpc_adsp_rtos_mtoa_vers_comp = 0x00030002;
1479#else
1480 rpc_adsp_rtos_mtoa_vers = 0x30001;
1481 rpc_adsp_rtos_mtoa_vers_comp = 0x00030001;
1482#endif
1483
Chaithanya Krishna Bacharajua3931822012-05-09 13:44:07 +05301484 msm_adsp_probe_work_queue = create_workqueue("msm_adsp_probe");
1485 if (msm_adsp_probe_work_queue == NULL)
1486 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001487 msm_adsp_driver.driver.name = msm_adsp_driver_name;
1488 rc = platform_driver_register(&msm_adsp_driver);
1489 MM_INFO("%s -- %d\n", msm_adsp_driver_name, rc);
1490 return rc;
1491}
1492
1493device_initcall(adsp_init);