]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/plat-omap/dsp/dsp_ctl.c
82140af392287d163481d2ea84aa683d13d70794
[linux-2.6-omap-h63xx.git] / arch / arm / plat-omap / dsp / dsp_ctl.c
1 /*
2  * This file is part of OMAP DSP driver (DSP Gateway version 3.3.1)
3  *
4  * Copyright (C) 2002-2006 Nokia Corporation. All rights reserved.
5  *
6  * Contact: Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <linux/module.h>
25 #include <linux/fs.h>
26 #include <linux/device.h>
27 #include <linux/init.h>
28 #include <linux/sched.h>
29 #include <linux/delay.h>
30 #include <linux/platform_device.h>
31 #include <linux/clk.h>
32 #include <linux/mutex.h>
33 #include <asm/uaccess.h>
34 #include <asm/io.h>
35 #include <asm/ioctls.h>
36 #include <asm/arch/mailbox.h>
37 #include "hardware_dsp.h"
38 #include "dsp_mbcmd.h"
39 #include "dsp.h"
40 #include "ipbuf.h"
41 #include "ioctl.h"
42
43 enum dsp_space_e {
44         SPACE_MEM,
45         SPACE_IO,
46 };
47
48 #ifdef CONFIG_OMAP_DSP_FBEXPORT
49 static enum fbstat_e {
50         FBSTAT_DISABLED = 0,
51         FBSTAT_ENABLED,
52         FBSTAT_MAX,
53 } fbstat = FBSTAT_ENABLED;
54 #endif
55
56 static enum cfgstat_e cfgstat;
57 int mbx_revision;
58 static u8 n_stask;
59
60 static ssize_t ifver_show(struct device *dev, struct device_attribute *attr,
61                           char *buf);
62 static ssize_t cpustat_show(struct device *dev, struct device_attribute *attr,
63                             char *buf);
64 static ssize_t icrmask_show(struct device *dev, struct device_attribute *attr,
65                             char *buf);
66 static ssize_t icrmask_store(struct device *dev, struct device_attribute *attr,
67                              const char *buf, size_t count);
68 static ssize_t loadinfo_show(struct device *dev, struct device_attribute *attr,
69                              char *buf);
70
71 #define __ATTR_RW(_name, _mode) { \
72         .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE },     \
73         .show   = _name##_show,                                 \
74         .store  = _name##_store,                                        \
75 }
76
77 static struct device_attribute dev_attr_ifver     = __ATTR_RO(ifver);
78 static struct device_attribute dev_attr_cpustat   = __ATTR_RO(cpustat);
79 static struct device_attribute dev_attr_icrmask   = __ATTR_RW(icrmask, 0644);
80 static struct device_attribute dev_attr_loadinfo  = __ATTR_RO(loadinfo);
81
82 /*
83  * misc interactive mailbox command operations
84  */
85 static struct misc_mb_wait_struct {
86         struct mutex lock;
87         wait_queue_head_t wait_q;
88         u8 cmd_h;
89         u8 cmd_l;
90         u16 *retvp;
91 } misc_mb_wait = {
92         .lock = __MUTEX_INITIALIZER(misc_mb_wait.lock),
93         .wait_q = __WAIT_QUEUE_HEAD_INITIALIZER(misc_mb_wait.wait_q),
94 };
95
96 static int __misc_mbcompose_send_and_wait(u8 cmd_h, u8 cmd_l, u16 data,
97                                           u16 *retvp)
98 {
99         struct mbcmd mb = MBCMD_INIT(cmd_h, cmd_l, data);
100         int ret = 0;
101
102         if (mutex_lock_interruptible(&misc_mb_wait.lock))
103                 return -EINTR;
104
105         misc_mb_wait.cmd_h = mb.cmd_h;
106         misc_mb_wait.cmd_l = mb.cmd_l;
107         misc_mb_wait.retvp = retvp;
108         dsp_mbcmd_send_and_wait(&mb, &misc_mb_wait.wait_q);
109
110         if (misc_mb_wait.cmd_h != 0)
111                 ret = -EINVAL;
112
113         mutex_unlock(&misc_mb_wait.lock);
114         return ret;
115 }
116
117 #define misc_mbcompose_send_and_wait(cmd_h, cmd_l, data, retvp) \
118                 __misc_mbcompose_send_and_wait(MBX_CMD_DSP_##cmd_h, (cmd_l), \
119                                                (data), (retvp));
120
121 static int misc_mbcmd_response(struct mbcmd *mb, int argc, int match_cmd_l_flag)
122 {
123         volatile u16 *buf;
124         int i;
125
126         /* if match_cmd_l_v flag is set, cmd_l needs to be matched as well. */
127         if (!waitqueue_active(&misc_mb_wait.wait_q) ||
128             (misc_mb_wait.cmd_h != mb->cmd_h) ||
129             (match_cmd_l_flag && (misc_mb_wait.cmd_l != mb->cmd_l))) {
130                 const struct cmdinfo *ci = cmdinfo[mb->cmd_h];
131                 char cmdstr[32];
132
133                 if (ci->cmd_l_type == CMD_L_TYPE_SUBCMD)
134                         sprintf(cmdstr, "%s:%s", ci->name, subcmd_name(mb));
135                 else
136                         strcpy(cmdstr, ci->name);
137                 printk(KERN_WARNING
138                        "mbx: unexpected command %s received!\n", cmdstr);
139                 return -1;
140         }
141
142         /*
143          * if argc == 1, receive data through mbx:data register.
144          * if argc > 1, receive through ipbuf_sys.
145          */
146         if (argc == 1)
147                 misc_mb_wait.retvp[0] = mb->data;
148         else if (argc > 1) {
149                 if (dsp_mem_enable(ipbuf_sys_da) < 0) {
150                         printk(KERN_ERR "mbx: %s - ipbuf_sys_da read failed!\n",
151                                cmdinfo[mb->cmd_h]->name);
152                         return -1;
153                 }
154                 if (sync_with_dsp(&ipbuf_sys_da->s, TID_ANON, 10) < 0) {
155                         printk(KERN_ERR "mbx: %s - IPBUF sync failed!\n",
156                                cmdinfo[mb->cmd_h]->name);
157                         dsp_mem_disable(ipbuf_sys_da);
158                         return -1;
159                 }
160                 /* need word access. do not use memcpy. */
161                 buf = ipbuf_sys_da->d;
162                 for (i = 0; i < argc; i++)
163                         misc_mb_wait.retvp[i] = buf[i];
164                 release_ipbuf_pvt(ipbuf_sys_da);
165                 dsp_mem_disable(ipbuf_sys_da);
166         }
167
168         misc_mb_wait.cmd_h = 0;
169         wake_up_interruptible(&misc_mb_wait.wait_q);
170         return 0;
171 }
172
173 static int dsp_regread(enum dsp_space_e space, u16 adr, u16 *val)
174 {
175         u8 cmd_l = (space == SPACE_MEM) ? REGRW_MEMR : REGRW_IOR;
176         int ret;
177
178         ret = misc_mbcompose_send_and_wait(REGRW, cmd_l, adr, val);
179         if ((ret < 0) && (ret != -EINTR))
180                 printk(KERN_ERR "omapdsp: register read error!\n");
181
182         return ret;
183 }
184
185 static int dsp_regwrite(enum dsp_space_e space, u16 adr, u16 val)
186 {
187         u8 cmd_l = (space == SPACE_MEM) ? REGRW_MEMW : REGRW_IOW;
188         struct mb_exarg arg = {
189                 .tid  = TID_ANON,
190                 .argc = 1,
191                 .argv = &val,
192         };
193
194         mbcompose_send_exarg(REGRW, cmd_l, adr, &arg);
195         return 0;
196 }
197
198 static int dsp_getvar(u8 varid, u16 *val)
199 {
200         int ret;
201
202         ret = misc_mbcompose_send_and_wait(GETVAR, varid, 0, val);
203         if ((ret < 0) && (ret != -EINTR))
204                 printk(KERN_ERR "omapdsp: variable read error!\n");
205
206         return ret;
207 }
208
209 static int dsp_setvar(u8 varid, u16 val)
210 {
211         mbcompose_send(SETVAR, varid, val);
212         return 0;
213 }
214
215 /*
216  * dsp_cfg() return value
217  *  = 0: OK
218  *  = 1: failed, but state is clear. (DSPCFG command failed)
219  *  < 0: failed. need cleanup.
220  */
221 static int dsp_cfg(void)
222 {
223         int ret = 0;
224
225 #ifdef CONFIG_ARCH_OMAP1
226         /* for safety */
227         dsp_mem_usecount_clear();
228 #endif
229
230         /*
231          * DSPCFG command and dsp_mem_start() must be called
232          * while internal mem is on.
233          */
234         dsp_mem_enable((void *)dspmem_base);
235
236         dsp_mbx_start();
237         dsp_twch_start();
238         dsp_mem_start();
239         dsp_err_start();
240
241         mbx_revision = -1;
242
243         ret = misc_mbcompose_send_and_wait(DSPCFG, DSPCFG_REQ, 0, NULL);
244         if (ret < 0) {
245                 if (ret != -EINTR)
246                         printk(KERN_ERR "omapdsp: configuration error!\n");
247                 ret = 1;
248                 goto out;
249         }
250
251 #if defined(CONFIG_ARCH_OMAP1) && defined(OLD_BINARY_SUPPORT)
252         /*
253          * MBREV 3.2 or earlier doesn't assume DMA domain is on
254          * when DSPCFG command is sent
255          */
256         if ((mbx_revision == MBREV_3_0) ||
257             (mbx_revision == MBREV_3_2)) {
258                 if ((ret = mbcompose_send(PM, PM_ENABLE, DSPREG_ICR_DMA)) < 0)
259                         goto out;
260         }
261 #endif
262
263         if ((ret = dsp_task_config_all(n_stask)) < 0)
264                 goto out;
265
266         /* initialization */
267 #ifdef CONFIG_OMAP_DSP_FBEXPORT
268         fbstat = FBSTAT_ENABLED;
269 #endif
270
271         /* send parameter */
272         if ((ret = dsp_setvar(VARID_ICRMASK, dsp_cpustat_get_icrmask())) < 0)
273                 goto out;
274
275         /* create runtime sysfs entries */
276         device_create_file(&dsp_device.dev, &dev_attr_loadinfo);
277
278 out:
279         dsp_mem_disable((void *)dspmem_base);
280         return ret;
281 }
282
283 static int dsp_uncfg(void)
284 {
285         if (dsp_taskmod_busy()) {
286                 printk(KERN_WARNING "omapdsp: tasks are busy.\n");
287                 return -EBUSY;
288         }
289
290         /* FIXME: lock task module */
291
292         /* remove runtime sysfs entries */
293         device_remove_file(&dsp_device.dev, &dev_attr_loadinfo);
294
295         dsp_mbx_stop();
296         dsp_twch_stop();
297         dsp_mem_stop();
298         dsp_err_stop();
299         dsp_dbg_stop();
300         dsp_task_unconfig_all();
301         ipbuf_stop();
302
303         return 0;
304 }
305
306 static int dsp_suspend(void)
307 {
308         int ret;
309
310         ret = misc_mbcompose_send_and_wait(SUSPEND, 0, 0, NULL);
311         if (ret < 0) {
312                 if (ret != -EINVAL)
313                         printk(KERN_ERR "omapdsp: DSP suspend error!\n");
314                 return ret;
315         }
316
317         udelay(100);    /* wait for DSP-side execution */
318         return 0;
319 }
320
321 int dsp_cfgstat_request(enum cfgstat_e st_req)
322 {
323         static DEFINE_MUTEX(cfgstat_lock);
324         int ret = 0, ret_override = 0;
325
326         if (mutex_lock_interruptible(&cfgstat_lock))
327                 return -EINTR;
328
329 again:
330         switch (st_req) {
331
332         /* cfgstat takes CLEAN, READY or SUSPEND,
333            while st_req can take SUSPEND in addition. */
334
335         case CFGSTAT_CLEAN:
336                 if (cfgstat == CFGSTAT_CLEAN)
337                         goto up_out;
338                 if ((ret = dsp_uncfg()) < 0)
339                         goto up_out;
340                 break;
341
342         case CFGSTAT_READY:
343                 if (cfgstat != CFGSTAT_CLEAN) {
344                         printk(KERN_ERR "omapdsp: DSP is ready already!\n");
345                         ret = -EINVAL;
346                         goto up_out;
347                 }
348
349                 ret = dsp_cfg();
350                 if (ret > 0) {  /* failed, but state is clear. */
351                         ret = -EINVAL;
352                         goto up_out;
353                 } else if (ret < 0) {   /* failed, need cleanup. */
354                         st_req = CFGSTAT_CLEAN;
355                         ret_override = ret;
356                         goto again;
357                 }
358                 break;
359
360         /*
361          * suspend / resume
362          * DSP is not reset within this code, but done in omap_pm_suspend.
363          * so if these functions are called from sysfs,
364          * DSP should be reset / unreset out of these functions.
365          */
366         case CFGSTAT_SUSPEND:
367                 switch (cfgstat) {
368
369                 case CFGSTAT_CLEAN:
370                         if (dsp_cpustat_get_stat() == CPUSTAT_RUN) {
371                                 printk(KERN_WARNING
372                                        "omapdsp: illegal operation -- trying "
373                                        "suspend DSP while it is running but "
374                                        "not configured.\n"
375                                        "  Resetting DSP.\n");
376                                 dsp_cpustat_request(CPUSTAT_RESET);
377                                 ret = -EINVAL;
378                         }
379                         goto up_out;
380
381                 case CFGSTAT_READY:
382                         if ((ret = dsp_suspend()) < 0)
383                                 goto up_out;
384                         break;
385
386                 case CFGSTAT_SUSPEND:
387                         goto up_out;
388
389                 default:
390                         BUG();
391
392                 }
393
394                 break;
395
396         case CFGSTAT_RESUME:
397                 if (cfgstat != CFGSTAT_SUSPEND) {
398                         printk(KERN_WARNING
399                                "omapdsp: DSP resume request, but DSP is not in "
400                                "suspend state.\n");
401                         ret = -EINVAL;
402                         goto up_out;
403                 }
404                 st_req = CFGSTAT_READY;
405                 break;
406
407         default:
408                 BUG();
409
410         }
411
412         cfgstat = st_req;
413 up_out:
414         mutex_unlock(&cfgstat_lock);
415         return ret_override ? ret_override : ret;
416 }
417
418 enum cfgstat_e dsp_cfgstat_get_stat(void)
419 {
420         return cfgstat;
421 }
422
423 /*
424  * polls all tasks
425  */
426 static int dsp_poll(void)
427 {
428         int ret;
429
430         ret = misc_mbcompose_send_and_wait(POLL, 0, 0, NULL);
431         if ((ret < 0) && (ret != -EINTR))
432                 printk(KERN_ERR "omapdsp: poll error!\n");
433
434         return ret;
435 }
436
437 int dsp_set_runlevel(u8 level)
438 {
439         if (level == RUNLEVEL_RECOVERY) {
440                 if (mbcompose_send_recovery(RUNLEVEL, level, 0) < 0)
441                         return -EINVAL;
442         } else {
443                 if ((level < RUNLEVEL_USER) ||
444                     (level > RUNLEVEL_SUPER))
445                         return -EINVAL;
446                 if (mbcompose_send(RUNLEVEL, level, 0) < 0)
447                         return -EINVAL;
448         }
449
450         return 0;
451 }
452
453 #ifdef CONFIG_OMAP_DSP_FBEXPORT
454 static void dsp_fbctl_enable(void)
455 {
456         mbcompose_send(KFUNC, KFUNC_FBCTL, FBCTL_ENABLE);
457 }
458
459 static int dsp_fbctl_disable(void)
460 {
461         int ret;
462
463         ret = misc_mbcompose_send_and_wait(KFUNC, KFUNC_FBCTL, FBCTL_DISABLE,
464                                            NULL);
465         if ((ret < 0) && (ret != -EINTR))
466                 printk(KERN_ERR "omapdsp: fb disable error!\n");
467
468         return 0;
469 }
470
471 static int dsp_fbstat_request(enum fbstat_e st)
472 {
473         static DEFINE_MUTEX(fbstat_lock);
474         int ret = 0;
475
476         if (mutex_lock_interruptible(&fbstat_lock))
477                 return -EINTR;
478
479         if (st == fbstat)
480                 goto up_out;
481
482         switch (st) {
483         case FBSTAT_ENABLED:
484                 dsp_fbctl_enable();
485                 break;
486         case FBSTAT_DISABLED:
487                 if ((ret = dsp_fbctl_disable()) < 0)
488                         goto up_out;
489                 break;
490         default:
491                 BUG();
492         }
493
494         fbstat = st;
495 up_out:
496         mutex_unlock(&fbstat_lock);
497         return 0;
498 }
499 #endif /* CONFIG_OMAP_DSP_FBEXPORT */
500
501 /*
502  * DSP control device file operations
503  */
504 static int dsp_ctl_ioctl(struct inode *inode, struct file *file,
505                          unsigned int cmd, unsigned long arg)
506 {
507         int ret = 0;
508
509         switch (cmd) {
510         /*
511          * command level 1: commands which don't need lock
512          */
513         case DSPCTL_IOCTL_RUN:
514                 dsp_cpustat_request(CPUSTAT_RUN);
515                 break;
516
517         case DSPCTL_IOCTL_RESET:
518                 dsp_cpustat_request(CPUSTAT_RESET);
519                 break;
520
521         case DSPCTL_IOCTL_SETRSTVECT:
522                 ret = dsp_set_rstvect((dsp_long_t)arg);
523                 break;
524
525 #ifdef CONFIG_ARCH_OMAP1
526         case DSPCTL_IOCTL_CPU_IDLE:
527                 dsp_cpustat_request(CPUSTAT_CPU_IDLE);
528                 break;
529
530         case DSPCTL_IOCTL_GBL_IDLE:
531                 dsp_cpustat_request(CPUSTAT_GBL_IDLE);
532                 break;
533
534         case DSPCTL_IOCTL_MPUI_WORDSWAP_ON:
535                 mpui_wordswap_on();
536                 break;
537
538         case DSPCTL_IOCTL_MPUI_WORDSWAP_OFF:
539                 mpui_wordswap_off();
540                 break;
541
542         case DSPCTL_IOCTL_MPUI_BYTESWAP_ON:
543                 mpui_byteswap_on();
544                 break;
545
546         case DSPCTL_IOCTL_MPUI_BYTESWAP_OFF:
547                 mpui_byteswap_off();
548                 break;
549 #endif /* CONFIG_ARCH_OMAP1 */
550
551         case DSPCTL_IOCTL_TASKCNT:
552                 ret = dsp_task_count();
553                 break;
554
555         case DSPCTL_IOCTL_MBSEND:
556                 {
557                         struct omap_dsp_mailbox_cmd u_cmd;
558                         mbx_msg_t msg;
559                         if (copy_from_user(&u_cmd, (void *)arg, sizeof(u_cmd)))
560                                 return -EFAULT;
561                         msg = (u_cmd.cmd << 16) | u_cmd.data;
562                         ret = dsp_mbcmd_send((struct mbcmd *)&msg);
563                         break;
564                 }
565
566         case DSPCTL_IOCTL_SETVAR:
567                 {
568                         struct omap_dsp_varinfo var;
569                         if (copy_from_user(&var, (void *)arg, sizeof(var)))
570                                 return -EFAULT;
571                         ret = dsp_setvar(var.varid, var.val[0]);
572                         break;
573                 }
574
575         case DSPCTL_IOCTL_RUNLEVEL:
576                 ret = dsp_set_runlevel(arg);
577                 break;
578
579 #ifdef CONFIG_OMAP_DSP_FBEXPORT
580         case DSPCTL_IOCTL_FBEN:
581                 ret = dsp_fbstat_request(FBSTAT_ENABLED);
582                 break;
583 #endif
584
585         /*
586          * command level 2: commands which need lock
587          */
588         case DSPCTL_IOCTL_DSPCFG:
589                 ret = dsp_cfgstat_request(CFGSTAT_READY);
590                 break;
591
592         case DSPCTL_IOCTL_DSPUNCFG:
593                 ret = dsp_cfgstat_request(CFGSTAT_CLEAN);
594                 break;
595
596         case DSPCTL_IOCTL_POLL:
597                 ret = dsp_poll();
598                 break;
599
600 #ifdef CONFIG_OMAP_DSP_FBEXPORT
601         case DSPCTL_IOCTL_FBDIS:
602                 ret = dsp_fbstat_request(FBSTAT_DISABLED);
603                 break;
604 #endif
605
606         case DSPCTL_IOCTL_SUSPEND:
607                 if ((ret = dsp_cfgstat_request(CFGSTAT_SUSPEND)) < 0)
608                         break;
609                 dsp_cpustat_request(CPUSTAT_RESET);
610                 break;
611
612         case DSPCTL_IOCTL_RESUME:
613                 if ((ret = dsp_cfgstat_request(CFGSTAT_RESUME)) < 0)
614                         break;
615                 dsp_cpustat_request(CPUSTAT_RUN);
616                 break;
617
618         case DSPCTL_IOCTL_REGMEMR:
619                 {
620                         struct omap_dsp_reginfo *u_reg = (void *)arg;
621                         u16 adr, val;
622
623                         if (copy_from_user(&adr, &u_reg->adr, sizeof(u16)))
624                                 return -EFAULT;
625                         if ((ret = dsp_regread(SPACE_MEM, adr, &val)) < 0)
626                                 return ret;
627                         if (copy_to_user(&u_reg->val, &val, sizeof(u16)))
628                                 return -EFAULT;
629                         break;
630                 }
631
632         case DSPCTL_IOCTL_REGMEMW:
633                 {
634                         struct omap_dsp_reginfo reg;
635
636                         if (copy_from_user(&reg, (void *)arg, sizeof(reg)))
637                                 return -EFAULT;
638                         ret = dsp_regwrite(SPACE_MEM, reg.adr, reg.val);
639                         break;
640                 }
641
642         case DSPCTL_IOCTL_REGIOR:
643                 {
644                         struct omap_dsp_reginfo *u_reg = (void *)arg;
645                         u16 adr, val;
646
647                         if (copy_from_user(&adr, &u_reg->adr, sizeof(u16)))
648                                 return -EFAULT;
649                         if ((ret = dsp_regread(SPACE_IO, adr, &val)) < 0)
650                                 return ret;
651                         if (copy_to_user(&u_reg->val, &val, sizeof(u16)))
652                                 return -EFAULT;
653                         break;
654                 }
655
656         case DSPCTL_IOCTL_REGIOW:
657                 {
658                         struct omap_dsp_reginfo reg;
659
660                         if (copy_from_user(&reg, (void *)arg, sizeof(reg)))
661                                 return -EFAULT;
662                         ret = dsp_regwrite(SPACE_IO, reg.adr, reg.val);
663                         break;
664                 }
665
666         case DSPCTL_IOCTL_GETVAR:
667                 {
668                         struct omap_dsp_varinfo *u_var = (void *)arg;
669                         u8 varid;
670                         u16 val[5]; /* maximum */
671                         int argc;
672
673                         if (copy_from_user(&varid, &u_var->varid, sizeof(u8)))
674                                 return -EFAULT;
675                         switch (varid) {
676                         case VARID_ICRMASK:
677                                 argc = 1;
678                                 break;
679                         case VARID_LOADINFO:
680                                 argc = 5;
681                                 break;
682                         default:
683                                 return -EINVAL;
684                         }
685                         if ((ret = dsp_getvar(varid, val)) < 0)
686                                 return ret;
687                         if (copy_to_user(&u_var->val, val, sizeof(u16) * argc))
688                                 return -EFAULT;
689                         break;
690                 }
691
692         default:
693                 return -ENOIOCTLCMD;
694         }
695
696         return ret;
697 }
698
699 /*
700  * functions called from mailbox interrupt routine
701  */
702 void mbx_suspend(struct mbcmd *mb)
703 {
704         misc_mbcmd_response(mb, 0, 0);
705 }
706
707 void mbx_dspcfg(struct mbcmd *mb)
708 {
709         u8 last   = mb->cmd_l & 0x80;
710         u8 cfgcmd = mb->cmd_l & 0x7f;
711         static dsp_long_t tmp_ipb_adr;
712
713         if (!waitqueue_active(&misc_mb_wait.wait_q) ||
714             (misc_mb_wait.cmd_h != MBX_CMD_DSP_DSPCFG)) {
715                 printk(KERN_WARNING
716                        "mbx: DSPCFG command received, "
717                        "but nobody is waiting for it...\n");
718                 return;
719         }
720
721         /* mailbox protocol check */
722         if (cfgcmd == DSPCFG_PROTREV) {
723                 mbx_revision = mb->data;
724                 if (mbx_revision == MBPROT_REVISION)
725                         return;
726 #ifdef OLD_BINARY_SUPPORT
727                 else if ((mbx_revision == MBREV_3_0) ||
728                          (mbx_revision == MBREV_3_2)) {
729                         printk(KERN_WARNING
730                                "mbx: ***** old DSP binary *****\n"
731                                "  Please update your DSP application.\n");
732                         return;
733                 }
734 #endif
735                 else {
736                         printk(KERN_ERR
737                                "mbx: protocol revision check error!\n"
738                                "  expected=0x%04x, received=0x%04x\n",
739                                MBPROT_REVISION, mb->data);
740                         mbx_revision = -1;
741                         goto abort1;
742                 }
743         }
744
745         /*
746          * following commands are accepted only after
747          * revision check has been passed.
748          */
749         if (!mbx_revision < 0) {
750                 printk(KERN_INFO
751                        "mbx: DSPCFG command received, "
752                        "but revision check has not been passed.\n");
753                 return;
754         }
755
756         switch (cfgcmd) {
757         case DSPCFG_SYSADRH:
758                 tmp_ipb_adr = (u32)mb->data << 16;
759                 break;
760
761         case DSPCFG_SYSADRL:
762                 tmp_ipb_adr |= mb->data;
763                 break;
764
765         case DSPCFG_ABORT:
766                 goto abort1;
767
768         default:
769                 printk(KERN_ERR
770                        "mbx: Unknown CFG command: cmd_l=0x%02x, data=0x%04x\n",
771                        mb->cmd_l, mb->data);
772                 return;
773         }
774
775         if (last) {
776                 void *badr;
777                 u16 bln;
778                 u16 bsz;
779                 volatile u16 *buf;
780                 void *ipb_sys_da, *ipb_sys_ad;
781                 void *mbseq;     /* FIXME: 3.4 obsolete */
782                 short *dbg_buf;
783                 u16 dbg_buf_sz, dbg_line_sz;
784                 struct mem_sync_struct mem_sync, *mem_syncp;
785
786                 ipb_sys_da = dspword_to_virt(tmp_ipb_adr);
787                 if (ipbuf_sys_config(ipb_sys_da, DIR_D2A) < 0)
788                         goto abort1;
789
790                 if (dsp_mem_enable(ipbuf_sys_da) < 0) {
791                         printk(KERN_ERR "mbx: DSPCFG - ipbuf_sys_da read failed!\n");
792                         goto abort1;
793                 }
794                 if (sync_with_dsp(&ipbuf_sys_da->s, TID_ANON, 10) < 0) {
795                         printk(KERN_ERR "mbx: DSPCFG - IPBUF sync failed!\n");
796                         dsp_mem_disable(ipbuf_sys_da);
797                         goto abort1;
798                 }
799                 /*
800                  * read configuration data on system IPBUF
801                  * we must read with 16bit-access
802                  */
803 #ifdef OLD_BINARY_SUPPORT
804                 if (mbx_revision == MBPROT_REVISION) {
805 #endif
806                         buf = ipbuf_sys_da->d;
807                         n_stask        = buf[0];
808                         bln            = buf[1];
809                         bsz            = buf[2];
810                         badr           = MKVIRT(buf[3], buf[4]);
811                         /* ipb_sys_da     = MKVIRT(buf[5], buf[6]); */
812                         ipb_sys_ad     = MKVIRT(buf[7], buf[8]);
813                         mbseq          = MKVIRT(buf[9], buf[10]);
814                         dbg_buf        = MKVIRT(buf[11], buf[12]);
815                         dbg_buf_sz     = buf[13];
816                         dbg_line_sz    = buf[14];
817                         mem_sync.DARAM = MKVIRT(buf[15], buf[16]);
818                         mem_sync.SARAM = MKVIRT(buf[17], buf[18]);
819                         mem_sync.SDRAM = MKVIRT(buf[19], buf[20]);
820                         mem_syncp = &mem_sync;
821 #ifdef OLD_BINARY_SUPPORT
822                 } else if (mbx_revision == MBREV_3_2) {
823                         buf = ipbuf_sys_da->d;
824                         n_stask     = buf[0];
825                         bln         = buf[1];
826                         bsz         = buf[2];
827                         badr        = MKVIRT(buf[3], buf[4]);
828                         /* ipb_sys_da  = MKVIRT(buf[5], buf[6]); */
829                         ipb_sys_ad  = MKVIRT(buf[7], buf[8]);
830                         mbseq       = MKVIRT(buf[9], buf[10]);
831                         dbg_buf     = NULL;
832                         dbg_buf_sz  = 0;
833                         dbg_line_sz = 0;
834                         mem_syncp   = NULL;
835                 } else if (mbx_revision == MBREV_3_0) {
836                         buf = ipbuf_sys_da->d;
837                         n_stask     = buf[0];
838                         bln         = buf[1];
839                         bsz         = buf[2];
840                         badr        = MKVIRT(buf[3], buf[4]);
841                         /* bkeep       = buf[5]; */
842                         /* ipb_sys_da  = MKVIRT(buf[6], buf[7]); */
843                         ipb_sys_ad  = MKVIRT(buf[8], buf[9]);
844                         mbseq       = MKVIRT(buf[10], buf[11]);
845                         dbg_buf     = NULL;
846                         dbg_buf_sz  = 0;
847                         dbg_line_sz = 0;
848                         mem_syncp   = NULL;
849                 } else { /* should not occur */
850                         dsp_mem_disable(ipbuf_sys_da);
851                         goto abort1;
852                 }
853 #endif /* OLD_BINARY_SUPPORT */
854
855                 release_ipbuf_pvt(ipbuf_sys_da);
856                 dsp_mem_disable(ipbuf_sys_da);
857
858                 /*
859                  * following configurations need to be done before
860                  * waking up the dspcfg initiator process.
861                  */
862                 if (ipbuf_sys_config(ipb_sys_ad, DIR_A2D) < 0)
863                         goto abort1;
864                 if (ipbuf_config(bln, bsz, badr) < 0)
865                         goto abort1;
866                 if (dsp_mbx_config(mbseq) < 0)
867                         goto abort2;
868                 if (dsp_dbg_config(dbg_buf, dbg_buf_sz, dbg_line_sz) < 0)
869                         goto abort2;
870                 if (dsp_mem_sync_config(mem_syncp) < 0)
871                         goto abort2;
872
873                 misc_mb_wait.cmd_h = 0;
874                 wake_up_interruptible(&misc_mb_wait.wait_q);
875         }
876         return;
877
878 abort2:
879         ipbuf_stop();
880 abort1:
881         wake_up_interruptible(&misc_mb_wait.wait_q);
882         return;
883 }
884
885 void mbx_poll(struct mbcmd *mb)
886 {
887         misc_mbcmd_response(mb, 0, 0);
888 }
889
890 void mbx_regrw(struct mbcmd *mb)
891 {
892         switch (mb->cmd_l) {
893         case REGRW_DATA:
894                 misc_mbcmd_response(mb, 1, 0);
895                 break;
896         default:
897                 printk(KERN_ERR
898                        "mbx: Illegal REGRW command: "
899                        "cmd_l=0x%02x, data=0x%04x\n", mb->cmd_l, mb->data);
900                 return;
901         }
902 }
903
904 void mbx_getvar(struct mbcmd *mb)
905 {
906         switch (mb->cmd_l) {
907         case VARID_ICRMASK:
908                 misc_mbcmd_response(mb, 1, 1);
909                 break;
910         case VARID_LOADINFO:
911                 misc_mbcmd_response(mb, 5, 1);
912                 break;
913         default:
914                 printk(KERN_ERR
915                        "mbx: Illegal GETVAR command: "
916                        "cmd_l=0x%02x, data=0x%04x\n", mb->cmd_l, mb->data);
917                 return;
918         }
919 }
920
921 void mbx_fbctl_disable(struct mbcmd *mb)
922 {
923         misc_mbcmd_response(mb, 0, 0);
924 }
925
926 struct file_operations dsp_ctl_fops = {
927         .owner   = THIS_MODULE,
928         .ioctl   = dsp_ctl_ioctl,
929 };
930
931 /*
932  * sysfs files
933  */
934
935 /* ifver */
936 static ssize_t ifver_show(struct device *dev, struct device_attribute *attr,
937                           char *buf)
938 {
939         int len = 0;
940
941         /*
942          * I/F VERSION descriptions:
943          *
944          * 3.2: sysfs / udev support
945          *      KMEM_RESERVE / KMEM_RELEASE ioctls for mem device
946          * 3.3: added following ioctls
947          *      DSPCTL_IOCTL_GBL_IDLE
948          *      DSPCTL_IOCTL_CPU_IDLE (instead of DSPCTL_IOCTL_IDLE)
949          *      DSPCTL_IOCTL_POLL
950          */
951
952         /*
953          * print all supporting I/F VERSIONs, like followings.
954          *
955          * len += sprintf(buf, "3.2\n");
956          * len += sprintf(buf, "3.3\n");
957          */
958         len += sprintf(buf + len, "3.2\n");
959         len += sprintf(buf + len, "3.3\n");
960
961         return len;
962 }
963
964 /* cpustat */
965 static char *cpustat_name[CPUSTAT_MAX] = {
966         [CPUSTAT_RESET]    = "reset",
967 #ifdef CONFIG_ARCH_OMAP1
968         [CPUSTAT_GBL_IDLE] = "gbl_idle",
969         [CPUSTAT_CPU_IDLE] = "cpu_idle",
970 #endif
971         [CPUSTAT_RUN]      = "run",
972 };
973
974 static ssize_t cpustat_show(struct device *dev, struct device_attribute *attr,
975                             char *buf)
976 {
977         return sprintf(buf, "%s\n", cpustat_name[dsp_cpustat_get_stat()]);
978 }
979
980 /* icrmask */
981 static ssize_t icrmask_show(struct device *dev, struct device_attribute *attr,
982                             char *buf)
983 {
984         return sprintf(buf, "0x%04x\n", dsp_cpustat_get_icrmask());
985 }
986
987 static ssize_t icrmask_store(struct device *dev, struct device_attribute *attr,
988                              const char *buf, size_t count)
989 {
990         u16 mask;
991         int ret;
992
993         mask = simple_strtol(buf, NULL, 16);
994         dsp_cpustat_set_icrmask(mask);
995
996         if (dsp_cfgstat_get_stat() == CFGSTAT_READY) {
997                 ret = dsp_setvar(VARID_ICRMASK, mask);
998                 if (ret < 0)
999                         return ret;
1000         }
1001
1002         return count;
1003 }
1004
1005 /* loadinfo */
1006 static ssize_t loadinfo_show(struct device *dev, struct device_attribute *attr,
1007                              char *buf)
1008 {
1009         int len;
1010         int ret;
1011         u16 val[5];
1012
1013         if ((ret = dsp_getvar(VARID_LOADINFO, val)) < 0)
1014                 return ret;
1015
1016         /*
1017          * load info value range is 0(free) - 10000(busy):
1018          * if CPU load is not measured on DSP, it sets 0xffff at val[0].
1019          */
1020
1021         if (val[0] == 0xffff) {
1022                 len = sprintf(buf,
1023                               "currently DSP load info is not available.\n");
1024                 goto out;
1025         }
1026
1027         len = sprintf(buf,
1028                       "DSP load info:\n"
1029                       "  10ms average = %3d.%02d%%\n"
1030                       "  1sec average = %3d.%02d%%  busiest 10ms = %3d.%02d%%\n"
1031                       "  1min average = %3d.%02d%%  busiest 1s   = %3d.%02d%%\n",
1032                       val[0]/100, val[0]%100,
1033                       val[1]/100, val[1]%100, val[2]/100, val[2]%100,
1034                       val[3]/100, val[3]%100, val[4]/100, val[4]%100);
1035 out:
1036         return len;
1037 }
1038
1039 void __init dsp_ctl_init(void)
1040 {
1041         device_create_file(&dsp_device.dev, &dev_attr_ifver);
1042         device_create_file(&dsp_device.dev, &dev_attr_cpustat);
1043         device_create_file(&dsp_device.dev, &dev_attr_icrmask);
1044 }
1045
1046 void dsp_ctl_exit(void)
1047 {
1048         device_remove_file(&dsp_device.dev, &dev_attr_ifver);
1049         device_remove_file(&dsp_device.dev, &dev_attr_cpustat);
1050         device_remove_file(&dsp_device.dev, &dev_attr_icrmask);
1051 }