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 #include <linux/platform_data/dsp-omap.h>
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;
68 static s32 shm_size = 0x500000; /* 5 MB */
69 static int tc_wordswapon; /* Default value is always false */
70 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
71 #define REC_TIMEOUT 5000 /*recovery timeout in msecs */
72 static atomic_t bridge_cref; /* number of bridge open handles */
73 static struct workqueue_struct *bridge_rec_queue;
74 static struct work_struct bridge_recovery_work;
75 static DECLARE_COMPLETION(bridge_comp);
76 static DECLARE_COMPLETION(bridge_open_comp);
81 struct omap34_xx_bridge_suspend_data {
83 wait_queue_head_t suspend_wq;
86 static struct omap34_xx_bridge_suspend_data bridge_suspend_data;
88 static int omap34_xxbridge_suspend_lockout(struct omap34_xx_bridge_suspend_data
92 if ((f)->f_flags & O_NONBLOCK)
94 wait_event_interruptible((s)->suspend_wq, (s)->suspended == 0);
100 module_param(dsp_debug, int, 0);
101 MODULE_PARM_DESC(dsp_debug, "Wait after loading DSP image. default = false");
103 module_param(dsp_test_sleepstate, int, 0);
104 MODULE_PARM_DESC(dsp_test_sleepstate, "DSP Sleep state = 0");
106 module_param(base_img, charp, 0);
107 MODULE_PARM_DESC(base_img, "DSP base image, default = NULL");
109 module_param(shm_size, int, 0);
110 MODULE_PARM_DESC(shm_size, "shm size, default = 4 MB, minimum = 64 KB");
112 module_param(tc_wordswapon, int, 0);
113 MODULE_PARM_DESC(tc_wordswapon, "TC Word Swap Option. default = 0");
115 MODULE_AUTHOR("Texas Instruments");
116 MODULE_LICENSE("GPL");
117 MODULE_VERSION(DSPBRIDGE_VERSION);
120 * This function is called when an application opens handle to the
123 static int bridge_open(struct inode *ip, struct file *filp)
126 struct process_context *pr_ctxt = NULL;
129 * Allocate a new process context and insert it into global
130 * process context list.
133 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
135 if (filp->f_flags & O_NONBLOCK ||
136 wait_for_completion_interruptible(&bridge_open_comp))
140 pr_ctxt = kzalloc(sizeof(struct process_context), GFP_KERNEL);
144 pr_ctxt->res_state = PROC_RES_ALLOCATED;
145 spin_lock_init(&pr_ctxt->dmm_map_lock);
146 INIT_LIST_HEAD(&pr_ctxt->dmm_map_list);
147 spin_lock_init(&pr_ctxt->dmm_rsv_lock);
148 INIT_LIST_HEAD(&pr_ctxt->dmm_rsv_list);
150 pr_ctxt->node_id = kzalloc(sizeof(struct idr), GFP_KERNEL);
151 if (!pr_ctxt->node_id) {
156 idr_init(pr_ctxt->node_id);
158 pr_ctxt->stream_id = kzalloc(sizeof(struct idr), GFP_KERNEL);
159 if (!pr_ctxt->stream_id) {
164 idr_init(pr_ctxt->stream_id);
166 filp->private_data = pr_ctxt;
168 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
169 atomic_inc(&bridge_cref);
174 kfree(pr_ctxt->node_id);
181 * This function is called when an application closes handle to the bridge
184 static int bridge_release(struct inode *ip, struct file *filp)
187 struct process_context *pr_ctxt;
189 if (!filp->private_data) {
194 pr_ctxt = filp->private_data;
195 flush_signals(current);
196 drv_remove_all_resources(pr_ctxt);
197 proc_detach(pr_ctxt);
198 kfree(pr_ctxt->node_id);
199 kfree(pr_ctxt->stream_id);
202 filp->private_data = NULL;
205 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
206 if (!atomic_dec_return(&bridge_cref))
207 complete(&bridge_comp);
212 /* This function provides IO interface to the bridge driver. */
213 static long bridge_ioctl(struct file *filp, unsigned int code,
218 union trapped_args buf_in;
220 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
227 status = omap34_xxbridge_suspend_lockout(&bridge_suspend_data, filp);
232 if (!filp->private_data) {
237 status = copy_from_user(&buf_in, (union trapped_args *)args,
238 sizeof(union trapped_args));
241 status = api_call_dev_ioctl(code, &buf_in, &retval,
247 dev_dbg(bridge, "%s: IOCTL Failed, code: 0x%x "
248 "status 0x%x\n", __func__, code, status);
258 /* This function maps kernel space memory to user space memory. */
259 static int bridge_mmap(struct file *filp, struct vm_area_struct *vma)
261 struct omap_dsp_platform_data *pdata =
262 omap_dspbridge_dev->dev.platform_data;
264 /* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */
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 return vm_iomap_memory(vma,
273 pdata->phys_mempool_base,
274 pdata->phys_mempool_size);
277 static const struct file_operations bridge_fops = {
279 .release = bridge_release,
280 .unlocked_ioctl = bridge_ioctl,
282 .llseek = noop_llseek,
286 static u32 time_out = 1000;
287 #ifdef CONFIG_TIDSPBRIDGE_DVFS
288 s32 dsp_max_opps = VDD1_OPP5;
291 /* Maximum Opps that can be requested by IVA */
293 #ifdef CONFIG_TIDSPBRIDGE_DVFS
294 const struct omap_opp vdd1_rate_table_bridge[] = {
297 {S125M, VDD1_OPP1, 0},
299 {S250M, VDD1_OPP2, 0},
301 {S500M, VDD1_OPP3, 0},
303 {S550M, VDD1_OPP4, 0},
305 {S600M, VDD1_OPP5, 0},
310 struct omap_dsp_platform_data *omap_dspbridge_pdata;
312 u32 vdd1_dsp_freq[6][4] = {
315 {0, 90000, 0, 86000},
317 {0, 180000, 80000, 170000},
319 {0, 360000, 160000, 340000},
321 {0, 396000, 325000, 376000},
323 {0, 430000, 355000, 430000},
326 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
327 static void bridge_recover(struct work_struct *work)
329 struct dev_object *dev;
330 struct cfg_devnode *dev_node;
331 if (atomic_read(&bridge_cref)) {
332 reinit_completion(&bridge_comp);
333 while (!wait_for_completion_timeout(&bridge_comp,
334 msecs_to_jiffies(REC_TIMEOUT)))
335 pr_info("%s:%d handle(s) still opened\n",
336 __func__, atomic_read(&bridge_cref));
338 dev = dev_get_first();
339 dev_get_dev_node(dev, &dev_node);
340 if (!dev_node || proc_auto_start(dev_node, dev))
341 pr_err("DSP could not be restarted\n");
343 complete_all(&bridge_open_comp);
346 void bridge_recover_schedule(void)
348 reinit_completion(&bridge_open_comp);
350 queue_work(bridge_rec_queue, &bridge_recovery_work);
353 #ifdef CONFIG_TIDSPBRIDGE_DVFS
354 static int dspbridge_scale_notification(struct notifier_block *op,
355 unsigned long val, void *ptr)
357 struct omap_dsp_platform_data *pdata =
358 omap_dspbridge_dev->dev.platform_data;
360 if (CPUFREQ_POSTCHANGE == val && pdata->dsp_get_opp)
361 pwr_pm_post_scale(PRCM_VDD1, pdata->dsp_get_opp());
366 static struct notifier_block iva_clk_notifier = {
367 .notifier_call = dspbridge_scale_notification,
373 * omap3_bridge_startup() - perform low lever initializations
374 * @pdev: pointer to platform device
376 * Initializes recovery, PM and DVFS required data, before calling
377 * clk and memory init routines.
379 static int omap3_bridge_startup(struct platform_device *pdev)
381 struct omap_dsp_platform_data *pdata = pdev->dev.platform_data;
382 struct drv_data *drv_datap = NULL;
383 u32 phys_membase, phys_memsize;
386 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
387 bridge_rec_queue = create_workqueue("bridge_rec_queue");
388 INIT_WORK(&bridge_recovery_work, bridge_recover);
389 reinit_completion(&bridge_comp);
393 /* Initialize the wait queue */
394 bridge_suspend_data.suspended = 0;
395 init_waitqueue_head(&bridge_suspend_data.suspend_wq);
397 #ifdef CONFIG_TIDSPBRIDGE_DVFS
398 for (i = 0; i < 6; i++)
399 pdata->mpu_speed[i] = vdd1_rate_table_bridge[i].rate;
401 err = cpufreq_register_notifier(&iva_clk_notifier,
402 CPUFREQ_TRANSITION_NOTIFIER);
404 pr_err("%s: clk_notifier_register failed for iva2_ck\n",
411 drv_datap = kzalloc(sizeof(struct drv_data), GFP_KERNEL);
417 drv_datap->shm_size = shm_size;
418 drv_datap->tc_wordswapon = tc_wordswapon;
421 drv_datap->base_img = kstrdup(base_img, GFP_KERNEL);
422 if (!drv_datap->base_img) {
428 dev_set_drvdata(bridge, drv_datap);
430 if (shm_size < 0x10000) { /* 64 KB */
432 pr_err("%s: shm size must be at least 64 KB\n", __func__);
435 dev_dbg(bridge, "%s: requested shm_size = 0x%x\n", __func__, shm_size);
437 phys_membase = pdata->phys_mempool_base;
438 phys_memsize = pdata->phys_mempool_size;
439 if (phys_membase > 0 && phys_memsize > 0)
440 mem_ext_phys_pool_init(phys_membase, phys_memsize);
443 dev_dbg(bridge, "%s: TC Word Swap is enabled\n", __func__);
445 driver_context = dsp_init(&err);
447 pr_err("DSP Bridge driver initialization failed\n");
454 mem_ext_phys_pool_release();
456 kfree(drv_datap->base_img);
460 #ifdef CONFIG_TIDSPBRIDGE_DVFS
461 cpufreq_unregister_notifier(&iva_clk_notifier,
462 CPUFREQ_TRANSITION_NOTIFIER);
469 static int omap34_xx_bridge_probe(struct platform_device *pdev)
473 #ifdef CONFIG_TIDSPBRIDGE_DVFS
477 omap_dspbridge_dev = pdev;
479 /* Global bridge device */
480 bridge = &omap_dspbridge_dev->dev;
482 /* Bridge low level initializations */
483 err = omap3_bridge_startup(pdev);
487 /* use 2.6 device model */
488 err = alloc_chrdev_region(&dev, 0, 1, "DspBridge");
490 pr_err("%s: Can't get major %d\n", __func__, driver_major);
494 cdev_init(&bridge_cdev, &bridge_fops);
495 bridge_cdev.owner = THIS_MODULE;
497 err = cdev_add(&bridge_cdev, dev, 1);
499 pr_err("%s: Failed to add bridge device\n", __func__);
504 bridge_class = class_create(THIS_MODULE, "ti_bridge");
505 if (IS_ERR(bridge_class)) {
506 pr_err("%s: Error creating bridge class\n", __func__);
507 err = PTR_ERR(bridge_class);
511 driver_major = MAJOR(dev);
512 device_create(bridge_class, NULL, MKDEV(driver_major, 0),
514 pr_info("DSP Bridge driver loaded\n");
519 cdev_del(&bridge_cdev);
521 unregister_chrdev_region(dev, 1);
526 static int omap34_xx_bridge_remove(struct platform_device *pdev)
530 struct drv_data *drv_datap = dev_get_drvdata(bridge);
532 /* Retrieve the Object handle from the driver data */
533 if (!drv_datap || !drv_datap->drv_object) {
535 pr_err("%s: Failed to retrieve the object handle\n", __func__);
539 #ifdef CONFIG_TIDSPBRIDGE_DVFS
540 if (cpufreq_unregister_notifier(&iva_clk_notifier,
541 CPUFREQ_TRANSITION_NOTIFIER))
542 pr_err("%s: cpufreq_unregister_notifier failed for iva2_ck\n",
544 #endif /* #ifdef CONFIG_TIDSPBRIDGE_DVFS */
546 if (driver_context) {
547 /* Put the DSP in reset state */
548 dsp_deinit(driver_context);
553 dev_set_drvdata(bridge, NULL);
556 mem_ext_phys_pool_release();
560 devno = MKDEV(driver_major, 0);
561 cdev_del(&bridge_cdev);
562 unregister_chrdev_region(devno, 1);
564 /* remove the device from sysfs */
565 device_destroy(bridge_class, MKDEV(driver_major, 0));
566 class_destroy(bridge_class);
573 static int bridge_suspend(struct platform_device *pdev, pm_message_t state)
576 u32 command = PWR_EMERGENCYDEEPSLEEP;
578 status = pwr_sleep_dsp(command, time_out);
582 bridge_suspend_data.suspended = 1;
586 static int bridge_resume(struct platform_device *pdev)
590 status = pwr_wake_dsp(time_out);
594 bridge_suspend_data.suspended = 0;
595 wake_up(&bridge_suspend_data.suspend_wq);
600 static struct platform_driver bridge_driver = {
604 .probe = omap34_xx_bridge_probe,
605 .remove = omap34_xx_bridge_remove,
607 .suspend = bridge_suspend,
608 .resume = bridge_resume,
612 /* To remove all process resources before removing the process from the
613 * process context list */
614 int drv_remove_all_resources(void *process_ctxt)
617 struct process_context *ctxt = (struct process_context *)process_ctxt;
618 drv_remove_all_strm_res_elements(ctxt);
619 drv_remove_all_node_res_elements(ctxt);
620 drv_remove_all_dmm_res_elements(ctxt);
621 ctxt->res_state = PROC_RES_FREED;
625 module_platform_driver(bridge_driver);