1 /*
2 * drv_interface.c
3 *
4 * DSP-BIOS Bridge driver support functions for TI OMAP processors.
5 *
6 * DSP/BIOS Bridge driver interface.
7 *
8 * Copyright (C) 2005-2006 Texas Instruments, Inc.
9 *
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.
13 *
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.
17 */
18
19 /* ----------------------------------- Host OS */
20
21 #include <plat/dsp.h>
22
23 #include <dspbridge/host_os.h>
24 #include <linux/types.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm.h>
27
28 #ifdef MODULE
29 #include <linux/module.h>
30 #endif
31
32 #include <linux/device.h>
33 #include <linux/init.h>
34 #include <linux/moduleparam.h>
35 #include <linux/cdev.h>
36
37 /* ----------------------------------- DSP/BIOS Bridge */
38 #include <dspbridge/dbdefs.h>
39
40 /* ----------------------------------- Trace & Debug */
41 #include <dspbridge/dbc.h>
42
43 /* ----------------------------------- OS Adaptation Layer */
44 #include <dspbridge/clk.h>
45 #include <dspbridge/sync.h>
46
47 /* ----------------------------------- Platform Manager */
48 #include <dspbridge/dspapi-ioctl.h>
49 #include <dspbridge/dspapi.h>
50 #include <dspbridge/dspdrv.h>
51
52 /* ----------------------------------- Resource Manager */
53 #include <dspbridge/pwr.h>
54
55 /* ----------------------------------- This */
56 #include <drv_interface.h>
57
58 #include <dspbridge/resourcecleanup.h>
59 #include <dspbridge/chnl.h>
60 #include <dspbridge/proc.h>
61 #include <dspbridge/dev.h>
62 #include <dspbridge/drv.h>
63
64 #ifdef CONFIG_TIDSPBRIDGE_DVFS
65 #include <mach-omap2/omap3-opp.h>
66 #endif
67
68 /* ----------------------------------- Globals */
69 #define DRIVER_NAME "DspBridge"
70 #define DSPBRIDGE_VERSION "0.3"
71 s32 dsp_debug;
72
73 struct platform_device *omap_dspbridge_dev;
74 struct device *bridge;
75
76 /* This is a test variable used by Bridge to test different sleep states */
77 s32 dsp_test_sleepstate;
78
79 static struct cdev bridge_cdev;
80
81 static struct class *bridge_class;
82
83 static u32 driver_context;
84 static s32 driver_major;
85 static char *base_img;
86 char *iva_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);
96 static bool recover;
97 #endif
98
99 #ifdef CONFIG_PM
100 struct omap34_xx_bridge_suspend_data {
101 int suspended;
102 wait_queue_head_t suspend_wq;
103 };
104
105 static struct omap34_xx_bridge_suspend_data bridge_suspend_data;
106
omap34_xxbridge_suspend_lockout(struct omap34_xx_bridge_suspend_data * s,struct file * f)107 static int omap34_xxbridge_suspend_lockout(struct omap34_xx_bridge_suspend_data
108 *s, struct file *f)
109 {
110 if ((s)->suspended) {
111 if ((f)->f_flags & O_NONBLOCK)
112 return -EPERM;
113 wait_event_interruptible((s)->suspend_wq, (s)->suspended == 0);
114 }
115 return 0;
116 }
117 #endif
118
119 module_param(dsp_debug, int, 0);
120 MODULE_PARM_DESC(dsp_debug, "Wait after loading DSP image. default = false");
121
122 module_param(dsp_test_sleepstate, int, 0);
123 MODULE_PARM_DESC(dsp_test_sleepstate, "DSP Sleep state = 0");
124
125 module_param(base_img, charp, 0);
126 MODULE_PARM_DESC(base_img, "DSP base image, default = NULL");
127
128 module_param(shm_size, int, 0);
129 MODULE_PARM_DESC(shm_size, "shm size, default = 4 MB, minimum = 64 KB");
130
131 module_param(tc_wordswapon, int, 0);
132 MODULE_PARM_DESC(tc_wordswapon, "TC Word Swap Option. default = 0");
133
134 MODULE_AUTHOR("Texas Instruments");
135 MODULE_LICENSE("GPL");
136 MODULE_VERSION(DSPBRIDGE_VERSION);
137
138 static char *driver_name = DRIVER_NAME;
139
140 static const struct file_operations bridge_fops = {
141 .open = bridge_open,
142 .release = bridge_release,
143 .unlocked_ioctl = bridge_ioctl,
144 .mmap = bridge_mmap,
145 .llseek = noop_llseek,
146 };
147
148 #ifdef CONFIG_PM
149 static u32 time_out = 1000;
150 #ifdef CONFIG_TIDSPBRIDGE_DVFS
151 s32 dsp_max_opps = VDD1_OPP5;
152 #endif
153
154 /* Maximum Opps that can be requested by IVA */
155 /*vdd1 rate table */
156 #ifdef CONFIG_TIDSPBRIDGE_DVFS
157 const struct omap_opp vdd1_rate_table_bridge[] = {
158 {0, 0, 0},
159 /*OPP1 */
160 {S125M, VDD1_OPP1, 0},
161 /*OPP2 */
162 {S250M, VDD1_OPP2, 0},
163 /*OPP3 */
164 {S500M, VDD1_OPP3, 0},
165 /*OPP4 */
166 {S550M, VDD1_OPP4, 0},
167 /*OPP5 */
168 {S600M, VDD1_OPP5, 0},
169 };
170 #endif
171 #endif
172
173 struct omap_dsp_platform_data *omap_dspbridge_pdata;
174
175 u32 vdd1_dsp_freq[6][4] = {
176 {0, 0, 0, 0},
177 /*OPP1 */
178 {0, 90000, 0, 86000},
179 /*OPP2 */
180 {0, 180000, 80000, 170000},
181 /*OPP3 */
182 {0, 360000, 160000, 340000},
183 /*OPP4 */
184 {0, 396000, 325000, 376000},
185 /*OPP5 */
186 {0, 430000, 355000, 430000},
187 };
188
189 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
bridge_recover(struct work_struct * work)190 static void bridge_recover(struct work_struct *work)
191 {
192 struct dev_object *dev;
193 struct cfg_devnode *dev_node;
194 if (atomic_read(&bridge_cref)) {
195 INIT_COMPLETION(bridge_comp);
196 while (!wait_for_completion_timeout(&bridge_comp,
197 msecs_to_jiffies(REC_TIMEOUT)))
198 pr_info("%s:%d handle(s) still opened\n",
199 __func__, atomic_read(&bridge_cref));
200 }
201 dev = dev_get_first();
202 dev_get_dev_node(dev, &dev_node);
203 if (!dev_node || proc_auto_start(dev_node, dev))
204 pr_err("DSP could not be restarted\n");
205 recover = false;
206 complete_all(&bridge_open_comp);
207 }
208
bridge_recover_schedule(void)209 void bridge_recover_schedule(void)
210 {
211 INIT_COMPLETION(bridge_open_comp);
212 recover = true;
213 queue_work(bridge_rec_queue, &bridge_recovery_work);
214 }
215 #endif
216 #ifdef CONFIG_TIDSPBRIDGE_DVFS
dspbridge_scale_notification(struct notifier_block * op,unsigned long val,void * ptr)217 static int dspbridge_scale_notification(struct notifier_block *op,
218 unsigned long val, void *ptr)
219 {
220 struct omap_dsp_platform_data *pdata =
221 omap_dspbridge_dev->dev.platform_data;
222
223 if (CPUFREQ_POSTCHANGE == val && pdata->dsp_get_opp)
224 pwr_pm_post_scale(PRCM_VDD1, pdata->dsp_get_opp());
225
226 return 0;
227 }
228
229 static struct notifier_block iva_clk_notifier = {
230 .notifier_call = dspbridge_scale_notification,
231 NULL,
232 };
233 #endif
234
235 /**
236 * omap3_bridge_startup() - perform low lever initializations
237 * @pdev: pointer to platform device
238 *
239 * Initializes recovery, PM and DVFS required data, before calling
240 * clk and memory init routines.
241 */
omap3_bridge_startup(struct platform_device * pdev)242 static int omap3_bridge_startup(struct platform_device *pdev)
243 {
244 struct omap_dsp_platform_data *pdata = pdev->dev.platform_data;
245 struct drv_data *drv_datap = NULL;
246 u32 phys_membase, phys_memsize;
247 int err;
248
249 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
250 bridge_rec_queue = create_workqueue("bridge_rec_queue");
251 INIT_WORK(&bridge_recovery_work, bridge_recover);
252 INIT_COMPLETION(bridge_comp);
253 #endif
254
255 #ifdef CONFIG_PM
256 /* Initialize the wait queue */
257 bridge_suspend_data.suspended = 0;
258 init_waitqueue_head(&bridge_suspend_data.suspend_wq);
259
260 #ifdef CONFIG_TIDSPBRIDGE_DVFS
261 for (i = 0; i < 6; i++)
262 pdata->mpu_speed[i] = vdd1_rate_table_bridge[i].rate;
263
264 err = cpufreq_register_notifier(&iva_clk_notifier,
265 CPUFREQ_TRANSITION_NOTIFIER);
266 if (err)
267 pr_err("%s: clk_notifier_register failed for iva2_ck\n",
268 __func__);
269 #endif
270 #endif
271
272 dsp_clk_init();
273
274 drv_datap = kzalloc(sizeof(struct drv_data), GFP_KERNEL);
275 if (!drv_datap) {
276 err = -ENOMEM;
277 goto err1;
278 }
279
280 drv_datap->shm_size = shm_size;
281 drv_datap->tc_wordswapon = tc_wordswapon;
282
283 if (base_img) {
284 drv_datap->base_img = kmalloc(strlen(base_img) + 1, GFP_KERNEL);
285 if (!drv_datap->base_img) {
286 err = -ENOMEM;
287 goto err2;
288 }
289 strncpy(drv_datap->base_img, base_img, strlen(base_img) + 1);
290 }
291
292 dev_set_drvdata(bridge, drv_datap);
293
294 if (shm_size < 0x10000) { /* 64 KB */
295 err = -EINVAL;
296 pr_err("%s: shm size must be at least 64 KB\n", __func__);
297 goto err3;
298 }
299 dev_dbg(bridge, "%s: requested shm_size = 0x%x\n", __func__, shm_size);
300
301 phys_membase = pdata->phys_mempool_base;
302 phys_memsize = pdata->phys_mempool_size;
303 if (phys_membase > 0 && phys_memsize > 0)
304 mem_ext_phys_pool_init(phys_membase, phys_memsize);
305
306 if (tc_wordswapon)
307 dev_dbg(bridge, "%s: TC Word Swap is enabled\n", __func__);
308
309 driver_context = dsp_init(&err);
310 if (err) {
311 pr_err("DSP Bridge driver initialization failed\n");
312 goto err4;
313 }
314
315 return 0;
316
317 err4:
318 mem_ext_phys_pool_release();
319 err3:
320 kfree(drv_datap->base_img);
321 err2:
322 kfree(drv_datap);
323 err1:
324 #ifdef CONFIG_TIDSPBRIDGE_DVFS
325 cpufreq_unregister_notifier(&iva_clk_notifier,
326 CPUFREQ_TRANSITION_NOTIFIER);
327 #endif
328 dsp_clk_exit();
329
330 return err;
331 }
332
omap34_xx_bridge_probe(struct platform_device * pdev)333 static int __devinit omap34_xx_bridge_probe(struct platform_device *pdev)
334 {
335 int err;
336 dev_t dev = 0;
337 #ifdef CONFIG_TIDSPBRIDGE_DVFS
338 int i = 0;
339 #endif
340
341 omap_dspbridge_dev = pdev;
342
343 /* Global bridge device */
344 bridge = &omap_dspbridge_dev->dev;
345
346 /* Bridge low level initializations */
347 err = omap3_bridge_startup(pdev);
348 if (err)
349 goto err1;
350
351 /* use 2.6 device model */
352 err = alloc_chrdev_region(&dev, 0, 1, driver_name);
353 if (err) {
354 pr_err("%s: Can't get major %d\n", __func__, driver_major);
355 goto err1;
356 }
357
358 cdev_init(&bridge_cdev, &bridge_fops);
359 bridge_cdev.owner = THIS_MODULE;
360
361 err = cdev_add(&bridge_cdev, dev, 1);
362 if (err) {
363 pr_err("%s: Failed to add bridge device\n", __func__);
364 goto err2;
365 }
366
367 /* udev support */
368 bridge_class = class_create(THIS_MODULE, "ti_bridge");
369 if (IS_ERR(bridge_class)) {
370 pr_err("%s: Error creating bridge class\n", __func__);
371 goto err3;
372 }
373
374 driver_major = MAJOR(dev);
375 device_create(bridge_class, NULL, MKDEV(driver_major, 0),
376 NULL, "DspBridge");
377 pr_info("DSP Bridge driver loaded\n");
378
379 return 0;
380
381 err3:
382 cdev_del(&bridge_cdev);
383 err2:
384 unregister_chrdev_region(dev, 1);
385 err1:
386 return err;
387 }
388
omap34_xx_bridge_remove(struct platform_device * pdev)389 static int __devexit omap34_xx_bridge_remove(struct platform_device *pdev)
390 {
391 dev_t devno;
392 bool ret;
393 int status = 0;
394 struct drv_data *drv_datap = dev_get_drvdata(bridge);
395
396 /* Retrieve the Object handle from the driver data */
397 if (!drv_datap || !drv_datap->drv_object) {
398 status = -ENODATA;
399 pr_err("%s: Failed to retrieve the object handle\n", __func__);
400 goto func_cont;
401 }
402
403 #ifdef CONFIG_TIDSPBRIDGE_DVFS
404 if (cpufreq_unregister_notifier(&iva_clk_notifier,
405 CPUFREQ_TRANSITION_NOTIFIER))
406 pr_err("%s: cpufreq_unregister_notifier failed for iva2_ck\n",
407 __func__);
408 #endif /* #ifdef CONFIG_TIDSPBRIDGE_DVFS */
409
410 if (driver_context) {
411 /* Put the DSP in reset state */
412 ret = dsp_deinit(driver_context);
413 driver_context = 0;
414 DBC_ASSERT(ret == true);
415 }
416
417 func_cont:
418 mem_ext_phys_pool_release();
419
420 dsp_clk_exit();
421
422 devno = MKDEV(driver_major, 0);
423 cdev_del(&bridge_cdev);
424 unregister_chrdev_region(devno, 1);
425 if (bridge_class) {
426 /* remove the device from sysfs */
427 device_destroy(bridge_class, MKDEV(driver_major, 0));
428 class_destroy(bridge_class);
429
430 }
431 return 0;
432 }
433
434 #ifdef CONFIG_PM
BRIDGE_SUSPEND(struct platform_device * pdev,pm_message_t state)435 static int BRIDGE_SUSPEND(struct platform_device *pdev, pm_message_t state)
436 {
437 u32 status;
438 u32 command = PWR_EMERGENCYDEEPSLEEP;
439
440 status = pwr_sleep_dsp(command, time_out);
441 if (status)
442 return -1;
443
444 bridge_suspend_data.suspended = 1;
445 return 0;
446 }
447
BRIDGE_RESUME(struct platform_device * pdev)448 static int BRIDGE_RESUME(struct platform_device *pdev)
449 {
450 u32 status;
451
452 status = pwr_wake_dsp(time_out);
453 if (status)
454 return -1;
455
456 bridge_suspend_data.suspended = 0;
457 wake_up(&bridge_suspend_data.suspend_wq);
458 return 0;
459 }
460 #else
461 #define BRIDGE_SUSPEND NULL
462 #define BRIDGE_RESUME NULL
463 #endif
464
465 static struct platform_driver bridge_driver = {
466 .driver = {
467 .name = "omap-dsp",
468 },
469 .probe = omap34_xx_bridge_probe,
470 .remove = __devexit_p(omap34_xx_bridge_remove),
471 .suspend = BRIDGE_SUSPEND,
472 .resume = BRIDGE_RESUME,
473 };
474
bridge_init(void)475 static int __init bridge_init(void)
476 {
477 return platform_driver_register(&bridge_driver);
478 }
479
bridge_exit(void)480 static void __exit bridge_exit(void)
481 {
482 platform_driver_unregister(&bridge_driver);
483 }
484
485 /*
486 * This function is called when an application opens handle to the
487 * bridge driver.
488 */
bridge_open(struct inode * ip,struct file * filp)489 static int bridge_open(struct inode *ip, struct file *filp)
490 {
491 int status = 0;
492 struct process_context *pr_ctxt = NULL;
493
494 /*
495 * Allocate a new process context and insert it into global
496 * process context list.
497 */
498
499 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
500 if (recover) {
501 if (filp->f_flags & O_NONBLOCK ||
502 wait_for_completion_interruptible(&bridge_open_comp))
503 return -EBUSY;
504 }
505 #endif
506 pr_ctxt = kzalloc(sizeof(struct process_context), GFP_KERNEL);
507 if (pr_ctxt) {
508 pr_ctxt->res_state = PROC_RES_ALLOCATED;
509 spin_lock_init(&pr_ctxt->dmm_map_lock);
510 INIT_LIST_HEAD(&pr_ctxt->dmm_map_list);
511 spin_lock_init(&pr_ctxt->dmm_rsv_lock);
512 INIT_LIST_HEAD(&pr_ctxt->dmm_rsv_list);
513
514 pr_ctxt->node_id = kzalloc(sizeof(struct idr), GFP_KERNEL);
515 if (pr_ctxt->node_id) {
516 idr_init(pr_ctxt->node_id);
517 } else {
518 status = -ENOMEM;
519 goto err;
520 }
521
522 pr_ctxt->stream_id = kzalloc(sizeof(struct idr), GFP_KERNEL);
523 if (pr_ctxt->stream_id)
524 idr_init(pr_ctxt->stream_id);
525 else
526 status = -ENOMEM;
527 } else {
528 status = -ENOMEM;
529 }
530 err:
531 filp->private_data = pr_ctxt;
532 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
533 if (!status)
534 atomic_inc(&bridge_cref);
535 #endif
536 return status;
537 }
538
539 /*
540 * This function is called when an application closes handle to the bridge
541 * driver.
542 */
bridge_release(struct inode * ip,struct file * filp)543 static int bridge_release(struct inode *ip, struct file *filp)
544 {
545 int status = 0;
546 struct process_context *pr_ctxt;
547
548 if (!filp->private_data) {
549 status = -EIO;
550 goto err;
551 }
552
553 pr_ctxt = filp->private_data;
554 flush_signals(current);
555 drv_remove_all_resources(pr_ctxt);
556 proc_detach(pr_ctxt);
557 kfree(pr_ctxt);
558
559 filp->private_data = NULL;
560
561 err:
562 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
563 if (!atomic_dec_return(&bridge_cref))
564 complete(&bridge_comp);
565 #endif
566 return status;
567 }
568
569 /* This function provides IO interface to the bridge driver. */
bridge_ioctl(struct file * filp,unsigned int code,unsigned long args)570 static long bridge_ioctl(struct file *filp, unsigned int code,
571 unsigned long args)
572 {
573 int status;
574 u32 retval = 0;
575 union trapped_args buf_in;
576
577 DBC_REQUIRE(filp != NULL);
578 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
579 if (recover) {
580 status = -EIO;
581 goto err;
582 }
583 #endif
584 #ifdef CONFIG_PM
585 status = omap34_xxbridge_suspend_lockout(&bridge_suspend_data, filp);
586 if (status != 0)
587 return status;
588 #endif
589
590 if (!filp->private_data) {
591 status = -EIO;
592 goto err;
593 }
594
595 status = copy_from_user(&buf_in, (union trapped_args *)args,
596 sizeof(union trapped_args));
597
598 if (!status) {
599 status = api_call_dev_ioctl(code, &buf_in, &retval,
600 filp->private_data);
601
602 if (!status) {
603 status = retval;
604 } else {
605 dev_dbg(bridge, "%s: IOCTL Failed, code: 0x%x "
606 "status 0x%x\n", __func__, code, status);
607 status = -1;
608 }
609
610 }
611
612 err:
613 return status;
614 }
615
616 /* This function maps kernel space memory to user space memory. */
bridge_mmap(struct file * filp,struct vm_area_struct * vma)617 static int bridge_mmap(struct file *filp, struct vm_area_struct *vma)
618 {
619 u32 offset = vma->vm_pgoff << PAGE_SHIFT;
620 u32 status;
621
622 DBC_ASSERT(vma->vm_start < vma->vm_end);
623
624 vma->vm_flags |= VM_RESERVED | VM_IO;
625 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
626
627 dev_dbg(bridge, "%s: vm filp %p offset %x start %lx end %lx page_prot "
628 "%lx flags %lx\n", __func__, filp, offset,
629 vma->vm_start, vma->vm_end, vma->vm_page_prot, vma->vm_flags);
630
631 status = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
632 vma->vm_end - vma->vm_start,
633 vma->vm_page_prot);
634 if (status != 0)
635 status = -EAGAIN;
636
637 return status;
638 }
639
640 /* To remove all process resources before removing the process from the
641 * process context list */
drv_remove_all_resources(void * process_ctxt)642 int drv_remove_all_resources(void *process_ctxt)
643 {
644 int status = 0;
645 struct process_context *ctxt = (struct process_context *)process_ctxt;
646 drv_remove_all_strm_res_elements(ctxt);
647 drv_remove_all_node_res_elements(ctxt);
648 drv_remove_all_dmm_res_elements(ctxt);
649 ctxt->res_state = PROC_RES_FREED;
650 return status;
651 }
652
653 /* Bridge driver initialization and de-initialization functions */
654 module_init(bridge_init);
655 module_exit(bridge_exit);
656