4 * DSP-BIOS Bridge driver support functions for TI OMAP processors.
6 * DSP/BIOS Bridge driver interface.
8 * Copyright (C) 2005-2006 Texas Instruments, Inc.
10 * This package is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 #include <linux/types.h>
22 #include <linux/platform_device.h>
24 #include <linux/module.h>
25 #include <linux/device.h>
26 #include <linux/init.h>
27 #include <linux/moduleparam.h>
28 #include <linux/cdev.h>
30 /* ----------------------------------- DSP/BIOS Bridge */
31 #include <dspbridge/dbdefs.h>
33 /* ----------------------------------- OS Adaptation Layer */
34 #include <dspbridge/clk.h>
36 /* ----------------------------------- Platform Manager */
37 #include <dspbridge/dspapi.h>
38 #include <dspbridge/dspdrv.h>
40 /* ----------------------------------- Resource Manager */
41 #include <dspbridge/pwr.h>
43 #include <dspbridge/resourcecleanup.h>
44 #include <dspbridge/proc.h>
45 #include <dspbridge/dev.h>
47 #ifdef CONFIG_TIDSPBRIDGE_DVFS
48 #include <mach-omap2/omap3-opp.h>
51 /* ----------------------------------- Globals */
52 #define DSPBRIDGE_VERSION "0.3"
55 struct platform_device
*omap_dspbridge_dev
;
56 struct device
*bridge
;
58 /* This is a test variable used by Bridge to test different sleep states */
59 s32 dsp_test_sleepstate
;
61 static struct cdev bridge_cdev
;
63 static struct class *bridge_class
;
65 static u32 driver_context
;
66 static s32 driver_major
;
67 static char *base_img
;
69 static s32 shm_size
= 0x500000; /* 5 MB */
70 static int tc_wordswapon
; /* Default value is always false */
71 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
72 #define REC_TIMEOUT 5000 /*recovery timeout in msecs */
73 static atomic_t bridge_cref
; /* number of bridge open handles */
74 static struct workqueue_struct
*bridge_rec_queue
;
75 static struct work_struct bridge_recovery_work
;
76 static DECLARE_COMPLETION(bridge_comp
);
77 static DECLARE_COMPLETION(bridge_open_comp
);
82 struct omap34_xx_bridge_suspend_data
{
84 wait_queue_head_t suspend_wq
;
87 static struct omap34_xx_bridge_suspend_data bridge_suspend_data
;
89 static int omap34_xxbridge_suspend_lockout(struct omap34_xx_bridge_suspend_data
93 if ((f
)->f_flags
& O_NONBLOCK
)
95 wait_event_interruptible((s
)->suspend_wq
, (s
)->suspended
== 0);
101 module_param(dsp_debug
, int, 0);
102 MODULE_PARM_DESC(dsp_debug
, "Wait after loading DSP image. default = false");
104 module_param(dsp_test_sleepstate
, int, 0);
105 MODULE_PARM_DESC(dsp_test_sleepstate
, "DSP Sleep state = 0");
107 module_param(base_img
, charp
, 0);
108 MODULE_PARM_DESC(base_img
, "DSP base image, default = NULL");
110 module_param(shm_size
, int, 0);
111 MODULE_PARM_DESC(shm_size
, "shm size, default = 4 MB, minimum = 64 KB");
113 module_param(tc_wordswapon
, int, 0);
114 MODULE_PARM_DESC(tc_wordswapon
, "TC Word Swap Option. default = 0");
116 MODULE_AUTHOR("Texas Instruments");
117 MODULE_LICENSE("GPL");
118 MODULE_VERSION(DSPBRIDGE_VERSION
);
121 * This function is called when an application opens handle to the
124 static int bridge_open(struct inode
*ip
, struct file
*filp
)
127 struct process_context
*pr_ctxt
= NULL
;
130 * Allocate a new process context and insert it into global
131 * process context list.
134 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
136 if (filp
->f_flags
& O_NONBLOCK
||
137 wait_for_completion_interruptible(&bridge_open_comp
))
141 pr_ctxt
= kzalloc(sizeof(struct process_context
), GFP_KERNEL
);
145 pr_ctxt
->res_state
= PROC_RES_ALLOCATED
;
146 spin_lock_init(&pr_ctxt
->dmm_map_lock
);
147 INIT_LIST_HEAD(&pr_ctxt
->dmm_map_list
);
148 spin_lock_init(&pr_ctxt
->dmm_rsv_lock
);
149 INIT_LIST_HEAD(&pr_ctxt
->dmm_rsv_list
);
151 pr_ctxt
->node_id
= kzalloc(sizeof(struct idr
), GFP_KERNEL
);
152 if (!pr_ctxt
->node_id
) {
157 idr_init(pr_ctxt
->node_id
);
159 pr_ctxt
->stream_id
= kzalloc(sizeof(struct idr
), GFP_KERNEL
);
160 if (!pr_ctxt
->stream_id
) {
165 idr_init(pr_ctxt
->stream_id
);
167 filp
->private_data
= pr_ctxt
;
169 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
170 atomic_inc(&bridge_cref
);
175 kfree(pr_ctxt
->node_id
);
182 * This function is called when an application closes handle to the bridge
185 static int bridge_release(struct inode
*ip
, struct file
*filp
)
188 struct process_context
*pr_ctxt
;
190 if (!filp
->private_data
) {
195 pr_ctxt
= filp
->private_data
;
196 flush_signals(current
);
197 drv_remove_all_resources(pr_ctxt
);
198 proc_detach(pr_ctxt
);
199 kfree(pr_ctxt
->node_id
);
200 kfree(pr_ctxt
->stream_id
);
203 filp
->private_data
= NULL
;
206 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
207 if (!atomic_dec_return(&bridge_cref
))
208 complete(&bridge_comp
);
213 /* This function provides IO interface to the bridge driver. */
214 static long bridge_ioctl(struct file
*filp
, unsigned int code
,
219 union trapped_args buf_in
;
221 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
228 status
= omap34_xxbridge_suspend_lockout(&bridge_suspend_data
, filp
);
233 if (!filp
->private_data
) {
238 status
= copy_from_user(&buf_in
, (union trapped_args
*)args
,
239 sizeof(union trapped_args
));
242 status
= api_call_dev_ioctl(code
, &buf_in
, &retval
,
248 dev_dbg(bridge
, "%s: IOCTL Failed, code: 0x%x "
249 "status 0x%x\n", __func__
, code
, status
);
259 /* This function maps kernel space memory to user space memory. */
260 static int bridge_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
264 vma
->vm_flags
|= VM_RESERVED
| VM_IO
;
265 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
267 dev_dbg(bridge
, "%s: vm filp %p start %lx end %lx page_prot %ulx "
268 "flags %lx\n", __func__
, filp
,
269 vma
->vm_start
, vma
->vm_end
, vma
->vm_page_prot
,
272 status
= remap_pfn_range(vma
, vma
->vm_start
, vma
->vm_pgoff
,
273 vma
->vm_end
- vma
->vm_start
,
281 static const struct file_operations bridge_fops
= {
283 .release
= bridge_release
,
284 .unlocked_ioctl
= bridge_ioctl
,
286 .llseek
= noop_llseek
,
290 static u32 time_out
= 1000;
291 #ifdef CONFIG_TIDSPBRIDGE_DVFS
292 s32 dsp_max_opps
= VDD1_OPP5
;
295 /* Maximum Opps that can be requested by IVA */
297 #ifdef CONFIG_TIDSPBRIDGE_DVFS
298 const struct omap_opp vdd1_rate_table_bridge
[] = {
301 {S125M
, VDD1_OPP1
, 0},
303 {S250M
, VDD1_OPP2
, 0},
305 {S500M
, VDD1_OPP3
, 0},
307 {S550M
, VDD1_OPP4
, 0},
309 {S600M
, VDD1_OPP5
, 0},
314 struct omap_dsp_platform_data
*omap_dspbridge_pdata
;
316 u32 vdd1_dsp_freq
[6][4] = {
319 {0, 90000, 0, 86000},
321 {0, 180000, 80000, 170000},
323 {0, 360000, 160000, 340000},
325 {0, 396000, 325000, 376000},
327 {0, 430000, 355000, 430000},
330 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
331 static void bridge_recover(struct work_struct
*work
)
333 struct dev_object
*dev
;
334 struct cfg_devnode
*dev_node
;
335 if (atomic_read(&bridge_cref
)) {
336 INIT_COMPLETION(bridge_comp
);
337 while (!wait_for_completion_timeout(&bridge_comp
,
338 msecs_to_jiffies(REC_TIMEOUT
)))
339 pr_info("%s:%d handle(s) still opened\n",
340 __func__
, atomic_read(&bridge_cref
));
342 dev
= dev_get_first();
343 dev_get_dev_node(dev
, &dev_node
);
344 if (!dev_node
|| proc_auto_start(dev_node
, dev
))
345 pr_err("DSP could not be restarted\n");
347 complete_all(&bridge_open_comp
);
350 void bridge_recover_schedule(void)
352 INIT_COMPLETION(bridge_open_comp
);
354 queue_work(bridge_rec_queue
, &bridge_recovery_work
);
357 #ifdef CONFIG_TIDSPBRIDGE_DVFS
358 static int dspbridge_scale_notification(struct notifier_block
*op
,
359 unsigned long val
, void *ptr
)
361 struct omap_dsp_platform_data
*pdata
=
362 omap_dspbridge_dev
->dev
.platform_data
;
364 if (CPUFREQ_POSTCHANGE
== val
&& pdata
->dsp_get_opp
)
365 pwr_pm_post_scale(PRCM_VDD1
, pdata
->dsp_get_opp());
370 static struct notifier_block iva_clk_notifier
= {
371 .notifier_call
= dspbridge_scale_notification
,
377 * omap3_bridge_startup() - perform low lever initializations
378 * @pdev: pointer to platform device
380 * Initializes recovery, PM and DVFS required data, before calling
381 * clk and memory init routines.
383 static int omap3_bridge_startup(struct platform_device
*pdev
)
385 struct omap_dsp_platform_data
*pdata
= pdev
->dev
.platform_data
;
386 struct drv_data
*drv_datap
= NULL
;
387 u32 phys_membase
, phys_memsize
;
390 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
391 bridge_rec_queue
= create_workqueue("bridge_rec_queue");
392 INIT_WORK(&bridge_recovery_work
, bridge_recover
);
393 INIT_COMPLETION(bridge_comp
);
397 /* Initialize the wait queue */
398 bridge_suspend_data
.suspended
= 0;
399 init_waitqueue_head(&bridge_suspend_data
.suspend_wq
);
401 #ifdef CONFIG_TIDSPBRIDGE_DVFS
402 for (i
= 0; i
< 6; i
++)
403 pdata
->mpu_speed
[i
] = vdd1_rate_table_bridge
[i
].rate
;
405 err
= cpufreq_register_notifier(&iva_clk_notifier
,
406 CPUFREQ_TRANSITION_NOTIFIER
);
408 pr_err("%s: clk_notifier_register failed for iva2_ck\n",
415 drv_datap
= kzalloc(sizeof(struct drv_data
), GFP_KERNEL
);
421 drv_datap
->shm_size
= shm_size
;
422 drv_datap
->tc_wordswapon
= tc_wordswapon
;
425 drv_datap
->base_img
= kmalloc(strlen(base_img
) + 1, GFP_KERNEL
);
426 if (!drv_datap
->base_img
) {
430 strncpy(drv_datap
->base_img
, base_img
, strlen(base_img
) + 1);
433 dev_set_drvdata(bridge
, drv_datap
);
435 if (shm_size
< 0x10000) { /* 64 KB */
437 pr_err("%s: shm size must be at least 64 KB\n", __func__
);
440 dev_dbg(bridge
, "%s: requested shm_size = 0x%x\n", __func__
, shm_size
);
442 phys_membase
= pdata
->phys_mempool_base
;
443 phys_memsize
= pdata
->phys_mempool_size
;
444 if (phys_membase
> 0 && phys_memsize
> 0)
445 mem_ext_phys_pool_init(phys_membase
, phys_memsize
);
448 dev_dbg(bridge
, "%s: TC Word Swap is enabled\n", __func__
);
450 driver_context
= dsp_init(&err
);
452 pr_err("DSP Bridge driver initialization failed\n");
459 mem_ext_phys_pool_release();
461 kfree(drv_datap
->base_img
);
465 #ifdef CONFIG_TIDSPBRIDGE_DVFS
466 cpufreq_unregister_notifier(&iva_clk_notifier
,
467 CPUFREQ_TRANSITION_NOTIFIER
);
474 static int __devinit
omap34_xx_bridge_probe(struct platform_device
*pdev
)
478 #ifdef CONFIG_TIDSPBRIDGE_DVFS
482 omap_dspbridge_dev
= pdev
;
484 /* Global bridge device */
485 bridge
= &omap_dspbridge_dev
->dev
;
487 /* Bridge low level initializations */
488 err
= omap3_bridge_startup(pdev
);
492 /* use 2.6 device model */
493 err
= alloc_chrdev_region(&dev
, 0, 1, "DspBridge");
495 pr_err("%s: Can't get major %d\n", __func__
, driver_major
);
499 cdev_init(&bridge_cdev
, &bridge_fops
);
500 bridge_cdev
.owner
= THIS_MODULE
;
502 err
= cdev_add(&bridge_cdev
, dev
, 1);
504 pr_err("%s: Failed to add bridge device\n", __func__
);
509 bridge_class
= class_create(THIS_MODULE
, "ti_bridge");
510 if (IS_ERR(bridge_class
)) {
511 pr_err("%s: Error creating bridge class\n", __func__
);
515 driver_major
= MAJOR(dev
);
516 device_create(bridge_class
, NULL
, MKDEV(driver_major
, 0),
518 pr_info("DSP Bridge driver loaded\n");
523 cdev_del(&bridge_cdev
);
525 unregister_chrdev_region(dev
, 1);
530 static int __devexit
omap34_xx_bridge_remove(struct platform_device
*pdev
)
534 struct drv_data
*drv_datap
= dev_get_drvdata(bridge
);
536 /* Retrieve the Object handle from the driver data */
537 if (!drv_datap
|| !drv_datap
->drv_object
) {
539 pr_err("%s: Failed to retrieve the object handle\n", __func__
);
543 #ifdef CONFIG_TIDSPBRIDGE_DVFS
544 if (cpufreq_unregister_notifier(&iva_clk_notifier
,
545 CPUFREQ_TRANSITION_NOTIFIER
))
546 pr_err("%s: cpufreq_unregister_notifier failed for iva2_ck\n",
548 #endif /* #ifdef CONFIG_TIDSPBRIDGE_DVFS */
550 if (driver_context
) {
551 /* Put the DSP in reset state */
552 dsp_deinit(driver_context
);
557 dev_set_drvdata(bridge
, NULL
);
560 mem_ext_phys_pool_release();
564 devno
= MKDEV(driver_major
, 0);
565 cdev_del(&bridge_cdev
);
566 unregister_chrdev_region(devno
, 1);
568 /* remove the device from sysfs */
569 device_destroy(bridge_class
, MKDEV(driver_major
, 0));
570 class_destroy(bridge_class
);
577 static int bridge_suspend(struct platform_device
*pdev
, pm_message_t state
)
580 u32 command
= PWR_EMERGENCYDEEPSLEEP
;
582 status
= pwr_sleep_dsp(command
, time_out
);
586 bridge_suspend_data
.suspended
= 1;
590 static int bridge_resume(struct platform_device
*pdev
)
594 status
= pwr_wake_dsp(time_out
);
598 bridge_suspend_data
.suspended
= 0;
599 wake_up(&bridge_suspend_data
.suspend_wq
);
604 static struct platform_driver bridge_driver
= {
608 .probe
= omap34_xx_bridge_probe
,
609 .remove
= __devexit_p(omap34_xx_bridge_remove
),
611 .suspend
= bridge_suspend
,
612 .resume
= bridge_resume
,
616 static int __init
bridge_init(void)
618 return platform_driver_register(&bridge_driver
);
621 static void __exit
bridge_exit(void)
623 platform_driver_unregister(&bridge_driver
);
626 /* To remove all process resources before removing the process from the
627 * process context list */
628 int drv_remove_all_resources(void *process_ctxt
)
631 struct process_context
*ctxt
= (struct process_context
*)process_ctxt
;
632 drv_remove_all_strm_res_elements(ctxt
);
633 drv_remove_all_node_res_elements(ctxt
);
634 drv_remove_all_dmm_res_elements(ctxt
);
635 ctxt
->res_state
= PROC_RES_FREED
;
639 /* Bridge driver initialization and de-initialization functions */
640 module_init(bridge_init
);
641 module_exit(bridge_exit
);