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