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.
19 /* ----------------------------------- Host OS */
21 #include <dspbridge/host_os.h>
22 #include <linux/types.h>
23 #include <linux/platform_device.h>
27 #include <linux/module.h>
30 #include <linux/device.h>
31 #include <linux/init.h>
32 #include <linux/moduleparam.h>
33 #include <linux/cdev.h>
35 /* ----------------------------------- DSP/BIOS Bridge */
36 #include <dspbridge/dbdefs.h>
38 /* ----------------------------------- Trace & Debug */
39 #include <dspbridge/dbc.h>
41 /* ----------------------------------- OS Adaptation Layer */
42 #include <dspbridge/clk.h>
43 #include <dspbridge/sync.h>
45 /* ----------------------------------- Platform Manager */
46 #include <dspbridge/dspapi-ioctl.h>
47 #include <dspbridge/dspapi.h>
48 #include <dspbridge/dspdrv.h>
50 /* ----------------------------------- Resource Manager */
51 #include <dspbridge/pwr.h>
53 /* ----------------------------------- This */
54 #include <drv_interface.h>
56 #include <dspbridge/resourcecleanup.h>
57 #include <dspbridge/chnl.h>
58 #include <dspbridge/proc.h>
59 #include <dspbridge/dev.h>
60 #include <dspbridge/drvdefs.h>
61 #include <dspbridge/drv.h>
63 #ifdef CONFIG_TIDSPBRIDGE_DVFS
64 #include <mach-omap2/omap3-opp.h>
67 #define BRIDGE_NAME "C6410"
68 /* ----------------------------------- Globals */
69 #define DRIVER_NAME "DspBridge"
70 #define DSPBRIDGE_VERSION "0.3"
73 struct platform_device *omap_dspbridge_dev;
74 struct device *bridge;
76 /* This is a test variable used by Bridge to test different sleep states */
77 s32 dsp_test_sleepstate;
79 static struct cdev bridge_cdev;
81 static struct class *bridge_class;
83 static u32 driver_context;
84 static s32 driver_major;
85 static char *base_img;
87 static s32 shm_size = 0x500000; /* 5 MB */
88 static int tc_wordswapon; /* Default value is always false */
89 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
90 #define REC_TIMEOUT 5000 /*recovery timeout in msecs */
91 static atomic_t bridge_cref; /* number of bridge open handles */
92 static struct workqueue_struct *bridge_rec_queue;
93 static struct work_struct bridge_recovery_work;
94 static DECLARE_COMPLETION(bridge_comp);
95 static DECLARE_COMPLETION(bridge_open_comp);
100 struct omap34_xx_bridge_suspend_data {
102 wait_queue_head_t suspend_wq;
105 static struct omap34_xx_bridge_suspend_data bridge_suspend_data;
107 static int omap34_xxbridge_suspend_lockout(struct omap34_xx_bridge_suspend_data
110 if ((s)->suspended) {
111 if ((f)->f_flags & O_NONBLOCK)
113 wait_event_interruptible((s)->suspend_wq, (s)->suspended == 0);
119 module_param(dsp_debug, int, 0);
120 MODULE_PARM_DESC(dsp_debug, "Wait after loading DSP image. default = false");
122 module_param(dsp_test_sleepstate, int, 0);
123 MODULE_PARM_DESC(dsp_test_sleepstate, "DSP Sleep state = 0");
125 module_param(base_img, charp, 0);
126 MODULE_PARM_DESC(base_img, "DSP base image, default = NULL");
128 module_param(shm_size, int, 0);
129 MODULE_PARM_DESC(shm_size, "shm size, default = 4 MB, minimum = 64 KB");
131 module_param(tc_wordswapon, int, 0);
132 MODULE_PARM_DESC(tc_wordswapon, "TC Word Swap Option. default = 0");
134 MODULE_AUTHOR("Texas Instruments");
135 MODULE_LICENSE("GPL");
136 MODULE_VERSION(DSPBRIDGE_VERSION);
138 static char *driver_name = DRIVER_NAME;
140 static const struct file_operations bridge_fops = {
142 .release = bridge_release,
143 .unlocked_ioctl = bridge_ioctl,
148 static u32 time_out = 1000;
149 #ifdef CONFIG_TIDSPBRIDGE_DVFS
150 s32 dsp_max_opps = VDD1_OPP5;
153 /* Maximum Opps that can be requested by IVA */
155 #ifdef CONFIG_TIDSPBRIDGE_DVFS
156 const struct omap_opp vdd1_rate_table_bridge[] = {
159 {S125M, VDD1_OPP1, 0},
161 {S250M, VDD1_OPP2, 0},
163 {S500M, VDD1_OPP3, 0},
165 {S550M, VDD1_OPP4, 0},
167 {S600M, VDD1_OPP5, 0},
172 struct dspbridge_platform_data *omap_dspbridge_pdata;
174 u32 vdd1_dsp_freq[6][4] = {
177 {0, 90000, 0, 86000},
179 {0, 180000, 80000, 170000},
181 {0, 360000, 160000, 340000},
183 {0, 396000, 325000, 376000},
185 {0, 430000, 355000, 430000},
188 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
189 static void bridge_recover(struct work_struct *work)
191 struct dev_object *dev;
192 struct cfg_devnode *dev_node;
193 if (atomic_read(&bridge_cref)) {
194 INIT_COMPLETION(bridge_comp);
195 while (!wait_for_completion_timeout(&bridge_comp,
196 msecs_to_jiffies(REC_TIMEOUT)))
197 pr_info("%s:%d handle(s) still opened\n",
198 __func__, atomic_read(&bridge_cref));
200 dev = dev_get_first();
201 dev_get_dev_node(dev, &dev_node);
202 if (!dev_node || proc_auto_start(dev_node, dev))
203 pr_err("DSP could not be restarted\n");
205 complete_all(&bridge_open_comp);
208 void bridge_recover_schedule(void)
210 INIT_COMPLETION(bridge_open_comp);
212 queue_work(bridge_rec_queue, &bridge_recovery_work);
215 #ifdef CONFIG_TIDSPBRIDGE_DVFS
216 static int dspbridge_scale_notification(struct notifier_block *op,
217 unsigned long val, void *ptr)
219 struct dspbridge_platform_data *pdata =
220 omap_dspbridge_dev->dev.platform_data;
222 if (CPUFREQ_POSTCHANGE == val && pdata->dsp_get_opp)
223 pwr_pm_post_scale(PRCM_VDD1, pdata->dsp_get_opp());
228 static struct notifier_block iva_clk_notifier = {
229 .notifier_call = dspbridge_scale_notification,
235 * omap3_bridge_startup() - perform low lever initializations
236 * @pdev: pointer to platform device
238 * Initializes recovery, PM and DVFS required data, before calling
239 * clk and memory init routines.
241 static int omap3_bridge_startup(struct platform_device *pdev)
243 struct dspbridge_platform_data *pdata = pdev->dev.platform_data;
244 struct drv_data *drv_datap = NULL;
245 u32 phys_membase, phys_memsize;
248 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
249 bridge_rec_queue = create_workqueue("bridge_rec_queue");
250 INIT_WORK(&bridge_recovery_work, bridge_recover);
251 INIT_COMPLETION(bridge_comp);
255 /* Initialize the wait queue */
256 bridge_suspend_data.suspended = 0;
257 init_waitqueue_head(&bridge_suspend_data.suspend_wq);
259 #ifdef CONFIG_TIDSPBRIDGE_DVFS
260 for (i = 0; i < 6; i++)
261 pdata->mpu_speed[i] = vdd1_rate_table_bridge[i].rate;
263 err = cpufreq_register_notifier(&iva_clk_notifier,
264 CPUFREQ_TRANSITION_NOTIFIER);
266 pr_err("%s: clk_notifier_register failed for iva2_ck\n",
273 drv_datap = kzalloc(sizeof(struct drv_data), GFP_KERNEL);
279 drv_datap->shm_size = shm_size;
280 drv_datap->tc_wordswapon = tc_wordswapon;
283 drv_datap->base_img = kmalloc(strlen(base_img) + 1, GFP_KERNEL);
284 if (!drv_datap->base_img) {
288 strncpy(drv_datap->base_img, base_img, strlen(base_img) + 1);
291 dev_set_drvdata(bridge, drv_datap);
293 if (shm_size < 0x10000) { /* 64 KB */
295 pr_err("%s: shm size must be at least 64 KB\n", __func__);
298 dev_dbg(bridge, "%s: requested shm_size = 0x%x\n", __func__, shm_size);
300 phys_membase = pdata->phys_mempool_base;
301 phys_memsize = pdata->phys_mempool_size;
302 if (phys_membase > 0 && phys_memsize > 0)
303 mem_ext_phys_pool_init(phys_membase, phys_memsize);
306 dev_dbg(bridge, "%s: TC Word Swap is enabled\n", __func__);
308 driver_context = dsp_init(&err);
310 pr_err("DSP Bridge driver initialization failed\n");
317 mem_ext_phys_pool_release();
319 kfree(drv_datap->base_img);
323 #ifdef CONFIG_TIDSPBRIDGE_DVFS
324 cpufreq_unregister_notifier(&iva_clk_notifier,
325 CPUFREQ_TRANSITION_NOTIFIER);
332 static int __devinit omap34_xx_bridge_probe(struct platform_device *pdev)
336 #ifdef CONFIG_TIDSPBRIDGE_DVFS
340 omap_dspbridge_dev = pdev;
342 /* Global bridge device */
343 bridge = &omap_dspbridge_dev->dev;
345 /* Bridge low level initializations */
346 err = omap3_bridge_startup(pdev);
350 /* use 2.6 device model */
351 err = alloc_chrdev_region(&dev, 0, 1, driver_name);
353 pr_err("%s: Can't get major %d\n", __func__, driver_major);
357 cdev_init(&bridge_cdev, &bridge_fops);
358 bridge_cdev.owner = THIS_MODULE;
360 err = cdev_add(&bridge_cdev, dev, 1);
362 pr_err("%s: Failed to add bridge device\n", __func__);
367 bridge_class = class_create(THIS_MODULE, "ti_bridge");
368 if (IS_ERR(bridge_class)) {
369 pr_err("%s: Error creating bridge class\n", __func__);
373 driver_major = MAJOR(dev);
374 device_create(bridge_class, NULL, MKDEV(driver_major, 0),
376 pr_info("DSP Bridge driver loaded\n");
381 cdev_del(&bridge_cdev);
383 unregister_chrdev_region(dev, 1);
388 static int __devexit omap34_xx_bridge_remove(struct platform_device *pdev)
393 struct drv_data *drv_datap = dev_get_drvdata(bridge);
395 /* Retrieve the Object handle from the driver data */
396 if (!drv_datap || !drv_datap->drv_object) {
398 pr_err("%s: Failed to retrieve the object handle\n", __func__);
402 #ifdef CONFIG_TIDSPBRIDGE_DVFS
403 if (cpufreq_unregister_notifier(&iva_clk_notifier,
404 CPUFREQ_TRANSITION_NOTIFIER))
405 pr_err("%s: cpufreq_unregister_notifier failed for iva2_ck\n",
407 #endif /* #ifdef CONFIG_TIDSPBRIDGE_DVFS */
409 if (driver_context) {
410 /* Put the DSP in reset state */
411 ret = dsp_deinit(driver_context);
413 DBC_ASSERT(ret == true);
417 mem_ext_phys_pool_release();
421 devno = MKDEV(driver_major, 0);
422 cdev_del(&bridge_cdev);
423 unregister_chrdev_region(devno, 1);
425 /* remove the device from sysfs */
426 device_destroy(bridge_class, MKDEV(driver_major, 0));
427 class_destroy(bridge_class);
434 static int BRIDGE_SUSPEND(struct platform_device *pdev, pm_message_t state)
437 u32 command = PWR_EMERGENCYDEEPSLEEP;
439 status = pwr_sleep_dsp(command, time_out);
443 bridge_suspend_data.suspended = 1;
447 static int BRIDGE_RESUME(struct platform_device *pdev)
451 status = pwr_wake_dsp(time_out);
455 bridge_suspend_data.suspended = 0;
456 wake_up(&bridge_suspend_data.suspend_wq);
460 #define BRIDGE_SUSPEND NULL
461 #define BRIDGE_RESUME NULL
464 static struct platform_driver bridge_driver = {
468 .probe = omap34_xx_bridge_probe,
469 .remove = __devexit_p(omap34_xx_bridge_remove),
470 .suspend = BRIDGE_SUSPEND,
471 .resume = BRIDGE_RESUME,
474 static int __init bridge_init(void)
476 return platform_driver_register(&bridge_driver);
479 static void __exit bridge_exit(void)
481 platform_driver_unregister(&bridge_driver);
485 * This function is called when an application opens handle to the
488 static int bridge_open(struct inode *ip, struct file *filp)
491 struct process_context *pr_ctxt = NULL;
494 * Allocate a new process context and insert it into global
495 * process context list.
498 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
500 if (filp->f_flags & O_NONBLOCK ||
501 wait_for_completion_interruptible(&bridge_open_comp))
505 pr_ctxt = kzalloc(sizeof(struct process_context), GFP_KERNEL);
507 pr_ctxt->res_state = PROC_RES_ALLOCATED;
508 spin_lock_init(&pr_ctxt->dmm_map_lock);
509 INIT_LIST_HEAD(&pr_ctxt->dmm_map_list);
510 spin_lock_init(&pr_ctxt->dmm_rsv_lock);
511 INIT_LIST_HEAD(&pr_ctxt->dmm_rsv_list);
513 pr_ctxt->node_id = kzalloc(sizeof(struct idr), GFP_KERNEL);
514 if (pr_ctxt->node_id) {
515 idr_init(pr_ctxt->node_id);
521 pr_ctxt->stream_id = kzalloc(sizeof(struct idr), GFP_KERNEL);
522 if (pr_ctxt->stream_id)
523 idr_init(pr_ctxt->stream_id);
530 filp->private_data = pr_ctxt;
531 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
533 atomic_inc(&bridge_cref);
539 * This function is called when an application closes handle to the bridge
542 static int bridge_release(struct inode *ip, struct file *filp)
545 struct process_context *pr_ctxt;
547 if (!filp->private_data) {
552 pr_ctxt = filp->private_data;
553 flush_signals(current);
554 drv_remove_all_resources(pr_ctxt);
555 proc_detach(pr_ctxt);
558 filp->private_data = NULL;
561 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
562 if (!atomic_dec_return(&bridge_cref))
563 complete(&bridge_comp);
568 /* This function provides IO interface to the bridge driver. */
569 static long bridge_ioctl(struct file *filp, unsigned int code,
574 union trapped_args buf_in;
576 DBC_REQUIRE(filp != NULL);
577 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
584 status = omap34_xxbridge_suspend_lockout(&bridge_suspend_data, filp);
589 if (!filp->private_data) {
594 status = copy_from_user(&buf_in, (union trapped_args *)args,
595 sizeof(union trapped_args));
598 status = api_call_dev_ioctl(code, &buf_in, &retval,
604 dev_dbg(bridge, "%s: IOCTL Failed, code: 0x%x "
605 "status 0x%x\n", __func__, code, status);
615 /* This function maps kernel space memory to user space memory. */
616 static int bridge_mmap(struct file *filp, struct vm_area_struct *vma)
618 u32 offset = vma->vm_pgoff << PAGE_SHIFT;
621 DBC_ASSERT(vma->vm_start < vma->vm_end);
623 vma->vm_flags |= VM_RESERVED | VM_IO;
624 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
626 dev_dbg(bridge, "%s: vm filp %p offset %x start %lx end %lx page_prot "
627 "%lx flags %lx\n", __func__, filp, offset,
628 vma->vm_start, vma->vm_end, vma->vm_page_prot, vma->vm_flags);
630 status = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
631 vma->vm_end - vma->vm_start,
639 /* To remove all process resources before removing the process from the
640 * process context list */
641 int drv_remove_all_resources(void *process_ctxt)
644 struct process_context *ctxt = (struct process_context *)process_ctxt;
645 drv_remove_all_strm_res_elements(ctxt);
646 drv_remove_all_node_res_elements(ctxt);
647 drv_remove_all_dmm_res_elements(ctxt);
648 ctxt->res_state = PROC_RES_FREED;
652 /* Bridge driver initialization and de-initialization functions */
653 module_init(bridge_init);
654 module_exit(bridge_exit);