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