2411626326ecfc766c33d4b4c0871f0930206c2c
[linux-3.10.git] / drivers / media / platform / tegra / nvavp / nvavp_dev.c
1 /*
2  * drivers/media/video/tegra/nvavp/nvavp_dev.c
3  *
4  * Copyright (c) 2011-2014, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This file is licensed under the terms of the GNU General Public License
7  * version 2. This program is licensed "as is" without any warranty of any
8  * kind, whether express or implied.
9  */
10
11 #include <linux/uaccess.h>
12 #include <linux/clk.h>
13 #include <linux/compat.h>
14 #include <linux/completion.h>
15 #include <linux/delay.h>
16 #include <linux/dma-buf.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/err.h>
19 #include <linux/firmware.h>
20 #include <linux/fs.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/ioctl.h>
24 #include <linux/irq.h>
25 #include <linux/kref.h>
26 #include <linux/list.h>
27 #include <linux/miscdevice.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/nvhost.h>
31 #include <linux/platform_device.h>
32 #include <linux/rbtree.h>
33 #include <linux/seq_file.h>
34 #include <linux/slab.h>
35 #include <linux/string.h>
36 #include <linux/tegra_nvavp.h>
37 #include <linux/types.h>
38 #include <linux/vmalloc.h>
39 #include <linux/workqueue.h>
40 #include <linux/pm_runtime.h>
41 #include <linux/clk/tegra.h>
42 #include <linux/tegra-powergate.h>
43 #include <linux/irqchip/tegra.h>
44 #include <linux/sched.h>
45 #include <linux/memblock.h>
46 #include <linux/anon_inodes.h>
47 #include <linux/tegra_pm_domains.h>
48
49
50 #include <linux/pm_qos.h>
51
52 #include <linux/of.h>
53 #include <linux/of_device.h>
54 #include <linux/of_platform.h>
55 #include <linux/of_address.h>
56 #include <linux/tegra-timer.h>
57
58 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU)
59 #include "../avp/headavp.h"
60 #endif
61 #include "nvavp_os.h"
62
63 #define TEGRA_NVAVP_NAME                        "nvavp"
64
65 #define NVAVP_PUSHBUFFER_SIZE                   4096
66
67 #define NVAVP_PUSHBUFFER_MIN_UPDATE_SPACE       (sizeof(u32) * 3)
68
69 static void __iomem *nvavp_reg_base;
70
71 #define TEGRA_NVAVP_RESET_VECTOR_ADDR   (nvavp_reg_base + 0xe200)
72
73 #define FLOW_CTRL_HALT_COP_EVENTS       (nvavp_reg_base + 0x6000 + 0x4)
74 #define FLOW_MODE_STOP                  (0x2 << 29)
75 #define FLOW_MODE_NONE                  0x0
76
77 #define NVAVP_OS_INBOX                  (nvavp_reg_base + 0x10)
78 #define NVAVP_OS_OUTBOX                 (nvavp_reg_base + 0x20)
79
80 #define NVAVP_INBOX_VALID               (1 << 29)
81
82 /* AVP behavior params */
83 #define NVAVP_OS_IDLE_TIMEOUT           100 /* milli-seconds */
84 #define NVAVP_OUTBOX_WRITE_TIMEOUT      1000 /* milli-seconds */
85
86 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
87 /* Two control channels: Audio and Video channels */
88 #define NVAVP_NUM_CHANNELS              2
89
90 #define NVAVP_AUDIO_CHANNEL             1
91
92 #define IS_AUDIO_CHANNEL_ID(channel_id) (channel_id == NVAVP_AUDIO_CHANNEL ? 1: 0)
93 #else
94 #define NVAVP_NUM_CHANNELS              1
95 #endif
96
97 /* Channel ID 0 represents the Video channel control area */
98 #define NVAVP_VIDEO_CHANNEL             0
99 /* Channel ID 1 represents the Audio channel control area */
100
101 #define IS_VIDEO_CHANNEL_ID(channel_id) (channel_id == NVAVP_VIDEO_CHANNEL ? 1: 0)
102
103 #define SCLK_BOOST_RATE         40000000
104
105 static bool boost_sclk;
106 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
107 static bool audio_enabled;
108 #endif
109
110 struct nvavp_channel {
111         struct mutex                    pushbuffer_lock;
112         dma_addr_t                      pushbuf_phys;
113         u8                              *pushbuf_data;
114         u32                             pushbuf_index;
115         u32                             pushbuf_fence;
116         struct nv_e276_control          *os_control;
117 };
118
119 struct nvavp_info {
120         u32                             clk_enabled;
121         struct clk                      *bsev_clk;
122         struct clk                      *vde_clk;
123         struct clk                      *cop_clk;
124 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
125         struct clk                      *bsea_clk;
126         struct clk                      *vcp_clk;
127 #endif
128
129         /* used for dvfs */
130         struct clk                      *sclk;
131         struct clk                      *emc_clk;
132         unsigned long                   sclk_rate;
133         unsigned long                   emc_clk_rate;
134
135         int                             mbox_from_avp_pend_irq;
136
137         struct mutex                    open_lock;
138         int                             refcount;
139         int                             video_initialized;
140         int                             video_refcnt;
141 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
142         int                             audio_initialized;
143         int                             audio_refcnt;
144         struct work_struct              app_notify_work;
145         void                            (*audio_notify)(void);
146 #endif
147         struct work_struct              clock_disable_work;
148
149         /* os information */
150         struct nvavp_os_info            os_info;
151
152         /* ucode information */
153         struct nvavp_ucode_info         ucode_info;
154
155         /* client to change min cpu freq rate*/
156         struct  pm_qos_request          min_cpu_freq_req;
157
158         /* client to change number of min online cpus*/
159         struct  pm_qos_request          min_online_cpus_req;
160
161         struct nvavp_channel            channel_info[NVAVP_NUM_CHANNELS];
162         bool                            pending;
163         bool                            stay_on;
164
165         u32                             syncpt_id;
166         u32                             syncpt_value;
167
168         struct platform_device          *nvhost_dev;
169         struct miscdevice               video_misc_dev;
170 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
171         struct miscdevice               audio_misc_dev;
172 #endif
173         struct task_struct              *init_task;
174 };
175
176 struct nvavp_clientctx {
177         struct nvavp_pushbuffer_submit_hdr submit_hdr;
178         struct nvavp_reloc relocs[NVAVP_MAX_RELOCATION_COUNT];
179         int num_relocs;
180         struct nvavp_info *nvavp;
181         int channel_id;
182         u32 clk_reqs;
183         spinlock_t iova_lock;
184         struct rb_root iova_handles;
185 };
186 static struct nvavp_info *nvavp_info_ctx;
187
188 static int nvavp_runtime_get(struct nvavp_info *nvavp)
189 {
190         if (nvavp->init_task != current) {
191                 mutex_unlock(&nvavp->open_lock);
192                 pm_runtime_get_sync(&nvavp->nvhost_dev->dev);
193                 mutex_lock(&nvavp->open_lock);
194         }
195         else
196                 pm_runtime_get_noresume(&nvavp->nvhost_dev->dev);
197
198         return 0;
199 }
200
201 static void nvavp_runtime_put(struct nvavp_info *nvavp)
202 {
203         pm_runtime_mark_last_busy(&nvavp->nvhost_dev->dev);
204         pm_runtime_put_autosuspend(&nvavp->nvhost_dev->dev);
205 }
206
207 static struct device_dma_parameters nvavp_dma_parameters = {
208         .max_segment_size = UINT_MAX,
209 };
210
211 struct nvavp_iova_info {
212         struct rb_node node;
213         atomic_t ref;
214         dma_addr_t addr;
215         struct dma_buf *dmabuf;
216         struct dma_buf_attachment *attachment;
217         struct sg_table *sgt;
218 };
219
220 /*
221  * Unmap's dmabuf and removes the iova info from rb tree
222  * Call with client iova_lock held.
223  */
224 static void nvavp_remove_iova_info_locked(
225         struct nvavp_clientctx *clientctx,
226         struct nvavp_iova_info *b)
227 {
228         struct nvavp_info *nvavp = clientctx->nvavp;
229
230         dev_dbg(&nvavp->nvhost_dev->dev,
231                 "remove iova addr (0x%lx))\n", (unsigned long)b->addr);
232         dma_buf_unmap_attachment(b->attachment,
233                 b->sgt, DMA_BIDIRECTIONAL);
234         dma_buf_detach(b->dmabuf, b->attachment);
235         dma_buf_put(b->dmabuf);
236         rb_erase(&b->node, &clientctx->iova_handles);
237         kfree(b);
238 }
239
240 /*
241  * Searches the given addr in rb tree and return valid pointer if present
242  * Call with client iova_lock held.
243  */
244 static struct nvavp_iova_info *nvavp_search_iova_info_locked(
245         struct nvavp_clientctx *clientctx, struct dma_buf *dmabuf,
246         struct rb_node **curr_parent)
247 {
248         struct rb_node *parent = NULL;
249         struct rb_node **p = &clientctx->iova_handles.rb_node;
250
251         while (*p) {
252                 struct nvavp_iova_info *b;
253                 parent = *p;
254                 b = rb_entry(parent, struct nvavp_iova_info, node);
255                 if (b->dmabuf == dmabuf)
256                         return b;
257                 else if (dmabuf > b->dmabuf)
258                         p = &parent->rb_right;
259                 else
260                         p = &parent->rb_left;
261         }
262         *curr_parent = parent;
263         return NULL;
264 }
265
266 /*
267  * Adds a newly-created iova info handle to the rb tree
268  * Call with client iova_lock held.
269  */
270 static void nvavp_add_iova_info_locked(struct nvavp_clientctx *clientctx,
271         struct nvavp_iova_info *h, struct rb_node *parent)
272 {
273         struct nvavp_iova_info *b;
274         struct nvavp_info *nvavp = clientctx->nvavp;
275         struct rb_node **p = &clientctx->iova_handles.rb_node;
276
277         dev_dbg(&nvavp->nvhost_dev->dev,
278                 "add iova addr (0x%lx))\n", (unsigned long)h->addr);
279
280         if (parent) {
281                 b = rb_entry(parent, struct nvavp_iova_info, node);
282                 if (h->dmabuf > b->dmabuf)
283                         p = &parent->rb_right;
284                 else
285                         p = &parent->rb_left;
286         }
287         rb_link_node(&h->node, parent, p);
288         rb_insert_color(&h->node, &clientctx->iova_handles);
289 }
290
291 /*
292  * Maps and adds the iova address if already not present in rb tree
293  * if present, update ref count and return iova return iova address
294  */
295 static int nvavp_get_iova_addr(struct nvavp_clientctx *clientctx,
296         struct dma_buf *dmabuf, dma_addr_t *addr)
297 {
298         struct nvavp_info *nvavp = clientctx->nvavp;
299         struct nvavp_iova_info *h;
300         struct nvavp_iova_info *b = NULL;
301         struct rb_node *curr_parent = NULL;
302         int ret = 0;
303
304         spin_lock(&clientctx->iova_lock);
305         b = nvavp_search_iova_info_locked(clientctx, dmabuf, &curr_parent);
306         if (b) {
307                 /* dmabuf already present in rb tree */
308                 atomic_inc(&b->ref);
309                 *addr = b->addr;
310                 dev_dbg(&nvavp->nvhost_dev->dev,
311                         "found iova addr (0x%pa) ref count(%d))\n",
312                         &(b->addr), atomic_read(&b->ref));
313                 goto out;
314         }
315         spin_unlock(&clientctx->iova_lock);
316
317         /* create new iova_info node */
318         h = kzalloc(sizeof(*h), GFP_KERNEL);
319         if (!h)
320                 return -ENOMEM;
321
322         h->dmabuf = dmabuf;
323         h->attachment = dma_buf_attach(dmabuf, &nvavp->nvhost_dev->dev);
324         if (IS_ERR(h->attachment)) {
325                 dev_err(&nvavp->nvhost_dev->dev, "cannot attach dmabuf\n");
326                 ret = PTR_ERR(h->attachment);
327                 goto err_put;
328         }
329
330         h->sgt = dma_buf_map_attachment(h->attachment, DMA_BIDIRECTIONAL);
331         if (IS_ERR(h->sgt)) {
332                 dev_err(&nvavp->nvhost_dev->dev, "cannot map dmabuf\n");
333                 ret = PTR_ERR(h->sgt);
334                 goto err_map;
335         }
336
337         h->addr = sg_dma_address(h->sgt->sgl);
338         atomic_set(&h->ref, 1);
339
340         spin_lock(&clientctx->iova_lock);
341         b = nvavp_search_iova_info_locked(clientctx, dmabuf, &curr_parent);
342         if (b) {
343                 dev_dbg(&nvavp->nvhost_dev->dev,
344                         "found iova addr (0x%pa) ref count(%d))\n",
345                         &(b->addr), atomic_read(&b->ref));
346                 atomic_inc(&b->ref);
347                 *addr = b->addr;
348                 spin_unlock(&clientctx->iova_lock);
349                 goto err_exist;
350         }
351         nvavp_add_iova_info_locked(clientctx, h, curr_parent);
352         *addr = h->addr;
353
354 out:
355         spin_unlock(&clientctx->iova_lock);
356         return 0;
357 err_exist:
358         dma_buf_unmap_attachment(h->attachment, h->sgt, DMA_BIDIRECTIONAL);
359 err_map:
360         dma_buf_detach(dmabuf, h->attachment);
361 err_put:
362         dma_buf_put(dmabuf);
363         kfree(h);
364         return ret;
365 }
366
367 /*
368  * Release the given iova address if it is last client otherwise dec ref count.
369  */
370 static void nvavp_release_iova_addr(struct nvavp_clientctx *clientctx,
371         struct dma_buf *dmabuf, dma_addr_t addr)
372 {
373         struct nvavp_info *nvavp = clientctx->nvavp;
374         struct nvavp_iova_info *b = NULL;
375         struct rb_node *curr_parent;
376
377         spin_lock(&clientctx->iova_lock);
378         b = nvavp_search_iova_info_locked(clientctx, dmabuf, &curr_parent);
379         if (!b) {
380                 dev_err(&nvavp->nvhost_dev->dev,
381                         "error iova addr (0x%pa) is not found\n", &addr);
382                 goto out;
383         }
384         /* if it is last reference, release iova info */
385         if (atomic_sub_return(1, &b->ref) == 0)
386                 nvavp_remove_iova_info_locked(clientctx, b);
387 out:
388         spin_unlock(&clientctx->iova_lock);
389 }
390
391 /*
392  * Release all the iova addresses in rb tree
393  */
394 static void nvavp_remove_iova_mapping(struct nvavp_clientctx *clientctx)
395 {
396         struct rb_node *p = NULL;
397         struct nvavp_iova_info *b;
398
399         spin_lock(&clientctx->iova_lock);
400         while ((p = rb_first(&clientctx->iova_handles))) {
401                 b = rb_entry(p, struct nvavp_iova_info, node);
402                 nvavp_remove_iova_info_locked(clientctx, b);
403         }
404         spin_unlock(&clientctx->iova_lock);
405 }
406
407 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
408 static int nvavp_get_audio_init_status(struct nvavp_info *nvavp)
409 {
410         return nvavp->audio_initialized;
411 }
412
413 static void nvavp_set_audio_init_status(struct nvavp_info *nvavp, int status)
414 {
415         nvavp->audio_initialized = status;
416 }
417 #endif
418
419 static void nvavp_set_video_init_status(struct nvavp_info *nvavp, int status)
420 {
421         nvavp->video_initialized = status;
422 }
423
424 static int nvavp_get_video_init_status(struct nvavp_info *nvavp)
425 {
426         return nvavp->video_initialized;
427 }
428
429 static struct nvavp_channel *nvavp_get_channel_info(struct nvavp_info *nvavp, int channel_id)
430 {
431         return &nvavp->channel_info[channel_id];
432 }
433
434 static int nvavp_outbox_write(unsigned int val)
435 {
436         unsigned int wait_ms = 0;
437
438         while (readl(NVAVP_OS_OUTBOX)) {
439                 usleep_range(1000, 2000);
440                 if (++wait_ms > NVAVP_OUTBOX_WRITE_TIMEOUT) {
441                         pr_err("No update from AVP in %d ms\n", wait_ms);
442                         return -ETIMEDOUT;
443                 }
444         }
445         writel(val, NVAVP_OS_OUTBOX);
446         return 0;
447 }
448
449 static void nvavp_set_channel_control_area(struct nvavp_info *nvavp, int channel_id)
450 {
451         struct nv_e276_control *control;
452         struct nvavp_os_info *os = &nvavp->os_info;
453         u32 temp;
454         void *ptr;
455         struct nvavp_channel *channel_info;
456
457         ptr = os->data + os->control_offset + (sizeof(struct nv_e276_control) * channel_id);
458
459         channel_info = nvavp_get_channel_info(nvavp, channel_id);
460         channel_info->os_control = (struct nv_e276_control *)ptr;
461
462         control = channel_info->os_control;
463
464         /* init get and put pointers */
465         writel(0x0, &control->put);
466         writel(0x0, &control->get);
467
468         pr_debug("nvavp_set_channel_control_area for channel_id (%d):\
469                 control->put (0x%08x) control->get (0x%08x)\n",
470                 channel_id, (u32) &control->put, (u32) &control->get);
471
472         /* Clock gating disabled for video and enabled for audio  */
473         if (IS_VIDEO_CHANNEL_ID(channel_id))
474                 writel(0x1, &control->idle_clk_enable);
475         else
476                 writel(0x0, &control->idle_clk_enable);
477
478         /* Disable iram clock gating */
479         writel(0x0, &control->iram_clk_gating);
480
481         /* enable avp idle timeout interrupt */
482         writel(0x1, &control->idle_notify_enable);
483         writel(NVAVP_OS_IDLE_TIMEOUT, &control->idle_notify_delay);
484
485 #if defined(CONFIG_ARCH_TEGRA_11x_SOC) || defined(CONFIG_ARCH_TEGRA_14x_SOC)
486         /* enable sync pt trap enable for avp */
487         if (IS_VIDEO_CHANNEL_ID(channel_id))
488                 writel(0x1, &control->sync_pt_incr_trap_enable);
489 #endif
490
491         /* init dma start and end pointers */
492         writel(channel_info->pushbuf_phys, &control->dma_start);
493         writel((channel_info->pushbuf_phys + NVAVP_PUSHBUFFER_SIZE),
494                                                 &control->dma_end);
495
496         writel(0x00, &channel_info->pushbuf_index);
497         temp = NVAVP_PUSHBUFFER_SIZE - NVAVP_PUSHBUFFER_MIN_UPDATE_SPACE;
498         writel(temp, &channel_info->pushbuf_fence);
499 }
500
501 static struct clk *nvavp_clk_get(struct nvavp_info *nvavp, int id)
502 {
503         if (!nvavp)
504                 return NULL;
505
506         if (id == NVAVP_MODULE_ID_AVP)
507                 return nvavp->sclk;
508         if (id == NVAVP_MODULE_ID_VDE)
509                 return nvavp->vde_clk;
510         if (id == NVAVP_MODULE_ID_EMC)
511                 return nvavp->emc_clk;
512
513         return NULL;
514 }
515
516 static int nvavp_powergate_vde(struct nvavp_info *nvavp)
517 {
518         int ret = 0;
519
520         dev_dbg(&nvavp->nvhost_dev->dev, "%s++\n", __func__);
521
522         /* Powergate VDE */
523         ret = tegra_powergate_partition(TEGRA_POWERGATE_VDEC);
524         if (ret)
525                 dev_err(&nvavp->nvhost_dev->dev,
526                                 "%s: powergate failed\n",
527                                 __func__);
528
529         return ret;
530 }
531
532 static int nvavp_unpowergate_vde(struct nvavp_info *nvavp)
533 {
534         int ret = 0;
535
536         dev_dbg(&nvavp->nvhost_dev->dev, "%s++\n", __func__);
537
538         /* UnPowergate VDE */
539         ret = tegra_unpowergate_partition(TEGRA_POWERGATE_VDEC);
540         if (ret)
541                 dev_err(&nvavp->nvhost_dev->dev,
542                                 "%s: unpowergate failed\n",
543                                 __func__);
544
545         return ret;
546 }
547
548 static void nvavp_clks_enable(struct nvavp_info *nvavp)
549 {
550         if (nvavp->clk_enabled == 0) {
551                 nvavp_runtime_get(nvavp);
552                 nvavp->clk_enabled++;
553                 nvhost_module_busy_ext(nvavp->nvhost_dev);
554                 clk_prepare_enable(nvavp->bsev_clk);
555                 clk_prepare_enable(nvavp->vde_clk);
556                 nvavp_unpowergate_vde(nvavp);
557                 clk_set_rate(nvavp->emc_clk, nvavp->emc_clk_rate);
558                 clk_set_rate(nvavp->sclk, nvavp->sclk_rate);
559                 dev_dbg(&nvavp->nvhost_dev->dev, "%s: setting sclk to %lu\n",
560                                 __func__, nvavp->sclk_rate);
561                 dev_dbg(&nvavp->nvhost_dev->dev, "%s: setting emc_clk to %lu\n",
562                                 __func__, nvavp->emc_clk_rate);
563         } else {
564                 nvavp->clk_enabled++;
565         }
566 }
567
568 static void nvavp_clks_disable(struct nvavp_info *nvavp)
569 {
570         if ((--nvavp->clk_enabled == 0) && !nvavp->stay_on) {
571                 clk_disable_unprepare(nvavp->bsev_clk);
572                 clk_disable_unprepare(nvavp->vde_clk);
573                 clk_set_rate(nvavp->emc_clk, 0);
574                 if (boost_sclk)
575                         clk_set_rate(nvavp->sclk, SCLK_BOOST_RATE);
576                 else
577                         clk_set_rate(nvavp->sclk, 0);
578                 nvavp_powergate_vde(nvavp);
579                 nvhost_module_idle_ext(nvavp->nvhost_dev);
580                 nvavp_runtime_put(nvavp);
581                 dev_dbg(&nvavp->nvhost_dev->dev, "%s: resetting emc_clk "
582                                 "and sclk\n", __func__);
583         }
584 }
585
586 static u32 nvavp_check_idle(struct nvavp_info *nvavp, int channel_id)
587 {
588         struct nvavp_channel *channel_info = nvavp_get_channel_info(nvavp, channel_id);
589         struct nv_e276_control *control = channel_info->os_control;
590
591         return (control->put == control->get) ? 1 : 0;
592 }
593
594 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
595 static void app_notify_handler(struct work_struct *work)
596 {
597         struct nvavp_info *nvavp;
598
599         nvavp = container_of(work, struct nvavp_info,
600                             app_notify_work);
601         if (nvavp->audio_notify)
602                 nvavp->audio_notify();
603         else
604                 kobject_uevent(&nvavp->nvhost_dev->dev.kobj, KOBJ_CHANGE);
605 }
606 #endif
607
608 static void clock_disable_handler(struct work_struct *work)
609 {
610         struct nvavp_info *nvavp;
611         struct nvavp_channel *channel_info;
612
613         nvavp = container_of(work, struct nvavp_info,
614                             clock_disable_work);
615
616         channel_info = nvavp_get_channel_info(nvavp, NVAVP_VIDEO_CHANNEL);
617         mutex_lock(&channel_info->pushbuffer_lock);
618         mutex_lock(&nvavp->open_lock);
619         if (nvavp_check_idle(nvavp, NVAVP_VIDEO_CHANNEL) && nvavp->pending) {
620                 nvavp->pending = false;
621                 nvavp_clks_disable(nvavp);
622         }
623         mutex_unlock(&nvavp->open_lock);
624         mutex_unlock(&channel_info->pushbuffer_lock);
625 }
626
627 static int nvavp_service(struct nvavp_info *nvavp)
628 {
629         struct nvavp_os_info *os = &nvavp->os_info;
630         u8 *debug_print;
631         u32 inbox;
632
633         inbox = readl(NVAVP_OS_INBOX);
634         if (!(inbox & NVAVP_INBOX_VALID))
635                 inbox = 0x00000000;
636
637         if ((inbox & NVE276_OS_INTERRUPT_VIDEO_IDLE) && (!nvavp->stay_on))
638                 schedule_work(&nvavp->clock_disable_work);
639
640         if (inbox & NVE276_OS_INTERRUPT_SYNCPT_INCR_TRAP) {
641                 /* sync pnt incr */
642                 if (nvavp->syncpt_id == NVE276_OS_SYNCPT_INCR_TRAP_GET_SYNCPT(inbox))
643                         nvhost_syncpt_cpu_incr_ext(
644                                 nvavp->nvhost_dev, nvavp->syncpt_id);
645         }
646
647 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
648         if (inbox & NVE276_OS_INTERRUPT_AUDIO_IDLE) {
649                 if (audio_enabled) {
650                         audio_enabled = false;
651                         nvavp_runtime_put(nvavp);
652                 }
653                 pr_debug("nvavp_service NVE276_OS_INTERRUPT_AUDIO_IDLE\n");
654         }
655 #endif
656         if (inbox & NVE276_OS_INTERRUPT_DEBUG_STRING) {
657                 /* Should only occur with debug AVP OS builds */
658                 debug_print = os->data;
659                 debug_print += os->debug_offset;
660                 dev_info(&nvavp->nvhost_dev->dev, "%s\n", debug_print);
661         }
662         if (inbox & (NVE276_OS_INTERRUPT_SEMAPHORE_AWAKEN |
663                      NVE276_OS_INTERRUPT_EXECUTE_AWAKEN)) {
664                 dev_info(&nvavp->nvhost_dev->dev,
665                         "AVP awaken event (0x%x)\n", inbox);
666         }
667         if (inbox & NVE276_OS_INTERRUPT_AVP_FATAL_ERROR) {
668                 dev_err(&nvavp->nvhost_dev->dev,
669                         "fatal AVP error (0x%08X)\n", inbox);
670         }
671         if (inbox & NVE276_OS_INTERRUPT_AVP_BREAKPOINT)
672                 dev_err(&nvavp->nvhost_dev->dev, "AVP breakpoint hit\n");
673         if (inbox & NVE276_OS_INTERRUPT_TIMEOUT)
674                 dev_err(&nvavp->nvhost_dev->dev, "AVP timeout\n");
675         writel(inbox & NVAVP_INBOX_VALID, NVAVP_OS_INBOX);
676
677 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
678         if (inbox & NVE276_OS_INTERRUPT_APP_NOTIFY) {
679                 pr_debug("nvavp_service NVE276_OS_INTERRUPT_APP_NOTIFY\n");
680                 schedule_work(&nvavp->app_notify_work);
681         }
682 #endif
683
684         return 0;
685 }
686
687 static irqreturn_t nvavp_mbox_pending_isr(int irq, void *data)
688 {
689         struct nvavp_info *nvavp = data;
690
691         nvavp_service(nvavp);
692
693         return IRQ_HANDLED;
694 }
695
696 static void nvavp_halt_avp(struct nvavp_info *nvavp)
697 {
698         /* ensure the AVP is halted */
699         writel(FLOW_MODE_STOP, FLOW_CTRL_HALT_COP_EVENTS);
700         tegra_periph_reset_assert(nvavp->cop_clk);
701
702         writel(0, NVAVP_OS_OUTBOX);
703         writel(0, NVAVP_OS_INBOX);
704 }
705
706 static int nvavp_reset_avp(struct nvavp_info *nvavp, unsigned long reset_addr)
707 {
708 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU)
709         unsigned long stub_code_phys = virt_to_phys(_tegra_avp_boot_stub);
710         dma_addr_t stub_data_phys;
711 #endif
712
713 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
714         if (!(nvavp_check_idle(nvavp, NVAVP_AUDIO_CHANNEL)))
715                 return 0;
716 #endif
717
718 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU)
719         _tegra_avp_boot_stub_data.map_phys_addr = avp->kernel_phys;
720         _tegra_avp_boot_stub_data.jump_addr = reset_addr;
721         wmb();
722         stub_data_phys = dma_map_single(NULL, &_tegra_avp_boot_stub_data,
723                                         sizeof(_tegra_avp_boot_stub_data),
724                                         DMA_TO_DEVICE);
725         rmb();
726         reset_addr = (unsigned long)stub_data_phys;
727 #endif
728         writel(FLOW_MODE_STOP, FLOW_CTRL_HALT_COP_EVENTS);
729
730         writel(reset_addr, TEGRA_NVAVP_RESET_VECTOR_ADDR);
731
732         clk_prepare_enable(nvavp->sclk);
733         clk_prepare_enable(nvavp->emc_clk);
734
735         /* If sclk_rate and emc_clk is not set by user space,
736          * max clock in dvfs table will be used to get best performance.
737          */
738         nvavp->sclk_rate = ULONG_MAX;
739         nvavp->emc_clk_rate = ULONG_MAX;
740
741         tegra_periph_reset_assert(nvavp->cop_clk);
742         udelay(2);
743         tegra_periph_reset_deassert(nvavp->cop_clk);
744
745         writel(FLOW_MODE_NONE, FLOW_CTRL_HALT_COP_EVENTS);
746
747 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU)
748         dma_unmap_single(NULL, stub_data_phys,
749                          sizeof(_tegra_avp_boot_stub_data),
750                          DMA_TO_DEVICE);
751 #endif
752         return 0;
753 }
754
755 static void nvavp_halt_vde(struct nvavp_info *nvavp)
756 {
757         if (nvavp->clk_enabled && !nvavp->pending)
758                 BUG();
759
760         if (nvavp->pending) {
761                 nvavp_clks_disable(nvavp);
762                 nvavp->pending = false;
763         }
764
765         tegra_periph_reset_assert(nvavp->bsev_clk);
766         tegra_periph_reset_assert(nvavp->vde_clk);
767 }
768
769 static int nvavp_reset_vde(struct nvavp_info *nvavp)
770 {
771         if (nvavp->clk_enabled)
772                 BUG();
773
774         nvavp_clks_enable(nvavp);
775
776         tegra_periph_reset_assert(nvavp->bsev_clk);
777         udelay(2);
778         tegra_periph_reset_deassert(nvavp->bsev_clk);
779
780         tegra_periph_reset_assert(nvavp->vde_clk);
781         udelay(2);
782         tegra_periph_reset_deassert(nvavp->vde_clk);
783
784         /*
785          * VDE clock is set to max freq by default.
786          * VDE clock can be set to different freq if needed
787          * through ioctl.
788          */
789         clk_set_rate(nvavp->vde_clk, ULONG_MAX);
790
791         nvavp_clks_disable(nvavp);
792
793         return 0;
794 }
795
796 static int nvavp_pushbuffer_alloc(struct nvavp_info *nvavp, int channel_id)
797 {
798         int ret = 0;
799
800         struct nvavp_channel *channel_info = nvavp_get_channel_info(
801                                                         nvavp, channel_id);
802
803         channel_info->pushbuf_data = dma_zalloc_coherent(&nvavp->nvhost_dev->dev,
804                                                            NVAVP_PUSHBUFFER_SIZE,
805                                                            &channel_info->pushbuf_phys,
806                                                            GFP_KERNEL);
807
808         if (!channel_info->pushbuf_data) {
809                 dev_err(&nvavp->nvhost_dev->dev,
810                         "cannot alloc pushbuffer memory\n");
811                 ret = -ENOMEM;
812         }
813
814         return ret;
815 }
816
817 static void nvavp_pushbuffer_free(struct nvavp_info *nvavp)
818 {
819         int channel_id;
820
821         for (channel_id = 0; channel_id < NVAVP_NUM_CHANNELS; channel_id++) {
822                 if (nvavp->channel_info[channel_id].pushbuf_data) {
823                         dma_free_coherent(&nvavp->nvhost_dev->dev,
824                                             NVAVP_PUSHBUFFER_SIZE,
825                                             nvavp->channel_info[channel_id].pushbuf_data,
826                                             nvavp->channel_info[channel_id].pushbuf_phys);
827                 }
828         }
829 }
830
831
832 static int nvavp_pushbuffer_init(struct nvavp_info *nvavp)
833 {
834         int ret, channel_id;
835         u32 val;
836
837         for (channel_id = 0; channel_id < NVAVP_NUM_CHANNELS; channel_id++) {
838                 ret = nvavp_pushbuffer_alloc(nvavp, channel_id);
839                 if (ret) {
840                         dev_err(&nvavp->nvhost_dev->dev,
841                                 "unable to alloc pushbuffer\n");
842                         return ret;
843                 }
844                 nvavp_set_channel_control_area(nvavp, channel_id);
845                 if (IS_VIDEO_CHANNEL_ID(channel_id)) {
846                         nvavp->syncpt_id = NVSYNCPT_AVP_0;
847                         if (!nvhost_syncpt_read_ext_check(nvavp->nvhost_dev,
848                                         nvavp->syncpt_id, &val))
849                                 nvavp->syncpt_value = val;
850                 }
851
852         }
853         return 0;
854 }
855
856 static void nvavp_pushbuffer_deinit(struct nvavp_info *nvavp)
857 {
858         nvavp_pushbuffer_free(nvavp);
859 }
860
861 static int nvavp_pushbuffer_update(struct nvavp_info *nvavp, u32 phys_addr,
862                         u32 gather_count, struct nvavp_syncpt *syncpt,
863                         u32 ext_ucode_flag, int channel_id)
864 {
865         struct nvavp_channel  *channel_info;
866         struct nv_e276_control *control;
867         u32 gather_cmd, setucode_cmd, sync = 0;
868         u32 wordcount = 0;
869         u32 index, value = -1;
870         int ret = 0;
871
872         mutex_lock(&nvavp->open_lock);
873         nvavp_runtime_get(nvavp);
874         mutex_unlock(&nvavp->open_lock);
875         channel_info = nvavp_get_channel_info(nvavp, channel_id);
876
877         control = channel_info->os_control;
878         pr_debug("nvavp_pushbuffer_update for channel_id (%d):\
879                 control->put (0x%x) control->get (0x%x)\n",
880                 channel_id, (u32) &control->put, (u32) &control->get);
881
882         mutex_lock(&channel_info->pushbuffer_lock);
883
884         /* check for pushbuffer wrapping */
885         if (channel_info->pushbuf_index >= channel_info->pushbuf_fence)
886                 channel_info->pushbuf_index = 0;
887
888         if (!ext_ucode_flag) {
889                 setucode_cmd =
890                         NVE26E_CH_OPCODE_INCR(NVE276_SET_MICROCODE_A, 3);
891
892                 index = wordcount + channel_info->pushbuf_index;
893                 writel(setucode_cmd, (channel_info->pushbuf_data + index));
894                 wordcount += sizeof(u32);
895
896                 index = wordcount + channel_info->pushbuf_index;
897                 writel(0, (channel_info->pushbuf_data + index));
898                 wordcount += sizeof(u32);
899
900                 index = wordcount + channel_info->pushbuf_index;
901                 writel(nvavp->ucode_info.phys,
902                         (channel_info->pushbuf_data + index));
903                 wordcount += sizeof(u32);
904
905                 index = wordcount + channel_info->pushbuf_index;
906                 writel(nvavp->ucode_info.size,
907                         (channel_info->pushbuf_data + index));
908                 wordcount += sizeof(u32);
909         }
910
911         gather_cmd = NVE26E_CH_OPCODE_GATHER(0, 0, 0, gather_count);
912
913         if (syncpt) {
914                 value = ++nvavp->syncpt_value;
915                 /* XXX: NvSchedValueWrappingComparison */
916                 sync = NVE26E_CH_OPCODE_IMM(NVE26E_HOST1X_INCR_SYNCPT,
917                         (NVE26E_HOST1X_INCR_SYNCPT_COND_OP_DONE << 8) |
918                         (nvavp->syncpt_id & 0xFF));
919         }
920
921         /* write commands out */
922         index = wordcount + channel_info->pushbuf_index;
923         writel(gather_cmd, (channel_info->pushbuf_data + index));
924         wordcount += sizeof(u32);
925
926         index = wordcount + channel_info->pushbuf_index;
927         writel(phys_addr, (channel_info->pushbuf_data + index));
928         wordcount += sizeof(u32);
929
930         if (syncpt) {
931                 index = wordcount + channel_info->pushbuf_index;
932                 writel(sync, (channel_info->pushbuf_data + index));
933                 wordcount += sizeof(u32);
934         }
935
936         /* enable clocks to VDE/BSEV */
937         mutex_lock(&nvavp->open_lock);
938         if (!nvavp->pending && IS_VIDEO_CHANNEL_ID(channel_id)) {
939                 nvavp_clks_enable(nvavp);
940                 nvavp->pending = true;
941         }
942         mutex_unlock(&nvavp->open_lock);
943
944         /* update put pointer */
945         channel_info->pushbuf_index = (channel_info->pushbuf_index + wordcount)&
946                                         (NVAVP_PUSHBUFFER_SIZE - 1);
947
948         writel(channel_info->pushbuf_index, &control->put);
949         wmb();
950
951         /* wake up avp */
952
953         if (IS_VIDEO_CHANNEL_ID(channel_id)) {
954                 pr_debug("Wake up Video Channel\n");
955                 ret = nvavp_outbox_write(0xA0000001);
956                 if (ret < 0)
957                         goto err_exit;
958         }
959         else {
960 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
961                 if (IS_AUDIO_CHANNEL_ID(channel_id)) {
962                         pr_debug("Wake up Audio Channel\n");
963                         if (!audio_enabled) {
964                                 mutex_lock(&nvavp->open_lock);
965                                 nvavp_runtime_get(nvavp);
966                                 mutex_unlock(&nvavp->open_lock);
967                                 audio_enabled = true;
968                         }
969                         ret = nvavp_outbox_write(0xA0000002);
970                         if (ret < 0)
971                                 goto err_exit;
972                 }
973 #endif
974         }
975         /* Fill out fence struct */
976         if (syncpt) {
977                 syncpt->id = nvavp->syncpt_id;
978                 syncpt->value = value;
979         }
980
981 err_exit:
982         mutex_unlock(&channel_info->pushbuffer_lock);
983         nvavp_runtime_put(nvavp);
984
985         return 0;
986 }
987
988 static void nvavp_unload_ucode(struct nvavp_info *nvavp)
989 {
990         dma_free_coherent(&nvavp->nvhost_dev->dev,  nvavp->ucode_info.size,
991                            nvavp->ucode_info.data, nvavp->ucode_info.phys);
992         kfree(nvavp->ucode_info.ucode_bin);
993 }
994
995 static int nvavp_load_ucode(struct nvavp_info *nvavp)
996 {
997         struct nvavp_ucode_info *ucode_info = &nvavp->ucode_info;
998         const struct firmware *nvavp_ucode_fw;
999         char fw_ucode_file[32];
1000         void *ptr;
1001         int ret = 0;
1002
1003         if (!ucode_info->ucode_bin) {
1004                 sprintf(fw_ucode_file, "nvavp_vid_ucode.bin");
1005
1006                 ret = request_firmware(&nvavp_ucode_fw, fw_ucode_file,
1007                                         nvavp->video_misc_dev.this_device);
1008                 if (ret) {
1009                         /* Try alternative version */
1010                         sprintf(fw_ucode_file, "nvavp_vid_ucode_alt.bin");
1011
1012                         ret = request_firmware(&nvavp_ucode_fw,
1013                                                 fw_ucode_file,
1014                                                 nvavp->video_misc_dev.this_device);
1015
1016                         if (ret) {
1017                                 dev_err(&nvavp->nvhost_dev->dev,
1018                                         "cannot read ucode firmware '%s'\n",
1019                                         fw_ucode_file);
1020                                 goto err_req_ucode;
1021                         }
1022                 }
1023
1024                 dev_dbg(&nvavp->nvhost_dev->dev,
1025                         "read ucode firmware from '%s' (%d bytes)\n",
1026                         fw_ucode_file, nvavp_ucode_fw->size);
1027
1028                 ptr = (void *)nvavp_ucode_fw->data;
1029
1030                 if (strncmp((const char *)ptr, "NVAVPAPP", 8)) {
1031                         dev_dbg(&nvavp->nvhost_dev->dev,
1032                                 "ucode hdr string mismatch\n");
1033                         ret = -EINVAL;
1034                         goto err_req_ucode;
1035                 }
1036                 ptr += 8;
1037                 ucode_info->size = nvavp_ucode_fw->size - 8;
1038
1039                 ucode_info->ucode_bin = kzalloc(ucode_info->size,
1040                                                 GFP_KERNEL);
1041                 if (!ucode_info->ucode_bin) {
1042                         dev_err(&nvavp->nvhost_dev->dev,
1043                                 "cannot allocate ucode bin\n");
1044                         ret = -ENOMEM;
1045                         goto err_ubin_alloc;
1046                 }
1047
1048                 ucode_info->data = dma_alloc_coherent(&nvavp->nvhost_dev->dev,
1049                                                 ucode_info->size,
1050                                                 &ucode_info->phys,
1051                                                 GFP_KERNEL);
1052                 if (!ucode_info->data) {
1053                         dev_err(&nvavp->nvhost_dev->dev,
1054                                 "cannot alloc memory for ucode\n");
1055                         ret = -ENOMEM;
1056                         goto err_ucode_alloc;
1057                 }
1058                 memcpy(ucode_info->ucode_bin, ptr, ucode_info->size);
1059                 release_firmware(nvavp_ucode_fw);
1060         }
1061
1062         memcpy(ucode_info->data, ucode_info->ucode_bin, ucode_info->size);
1063         return 0;
1064
1065 err_ucode_alloc:
1066         kfree(nvavp->ucode_info.ucode_bin);
1067 err_ubin_alloc:
1068         release_firmware(nvavp_ucode_fw);
1069 err_req_ucode:
1070         return ret;
1071 }
1072
1073 static void nvavp_unload_os(struct nvavp_info *nvavp)
1074 {
1075         dma_free_coherent(&nvavp->nvhost_dev->dev, SZ_1M,
1076                 nvavp->os_info.data, nvavp->os_info.phys);
1077         kfree(nvavp->os_info.os_bin);
1078 }
1079
1080 static int nvavp_load_os(struct nvavp_info *nvavp, char *fw_os_file)
1081 {
1082         struct nvavp_os_info *os_info = &nvavp->os_info;
1083         const struct firmware *nvavp_os_fw;
1084         void *ptr;
1085         u32 size;
1086         int ret = 0;
1087
1088         if (!os_info->os_bin) {
1089                 ret = request_firmware(&nvavp_os_fw, fw_os_file,
1090                                         nvavp->video_misc_dev.this_device);
1091                 if (ret) {
1092                         dev_err(&nvavp->nvhost_dev->dev,
1093                                 "cannot read os firmware '%s'\n", fw_os_file);
1094                         goto err_req_fw;
1095                 }
1096
1097                 dev_dbg(&nvavp->nvhost_dev->dev,
1098                         "read firmware from '%s' (%d bytes)\n",
1099                         fw_os_file, nvavp_os_fw->size);
1100
1101                 ptr = (void *)nvavp_os_fw->data;
1102
1103                 if (strncmp((const char *)ptr, "NVAVP-OS", 8)) {
1104                         dev_dbg(&nvavp->nvhost_dev->dev,
1105                                 "os hdr string mismatch\n");
1106                         ret = -EINVAL;
1107                         goto err_os_bin;
1108                 }
1109
1110                 ptr += 8;
1111                 os_info->entry_offset = *((u32 *)ptr);
1112                 ptr += sizeof(u32);
1113                 os_info->control_offset = *((u32 *)ptr);
1114                 ptr += sizeof(u32);
1115                 os_info->debug_offset = *((u32 *)ptr);
1116                 ptr += sizeof(u32);
1117
1118                 size = *((u32 *)ptr);    ptr += sizeof(u32);
1119
1120                 os_info->size = size;
1121                 os_info->os_bin = kzalloc(os_info->size,
1122                                                 GFP_KERNEL);
1123                 if (!os_info->os_bin) {
1124                         dev_err(&nvavp->nvhost_dev->dev,
1125                                 "cannot allocate os bin\n");
1126                         ret = -ENOMEM;
1127                         goto err_os_bin;
1128                 }
1129
1130                 memcpy(os_info->os_bin, ptr, os_info->size);
1131                 memset(os_info->data + os_info->size, 0, SZ_1M - os_info->size);
1132
1133                 dev_dbg(&nvavp->nvhost_dev->dev,
1134                         "entry=%08x control=%08x debug=%08x size=%d\n",
1135                         os_info->entry_offset, os_info->control_offset,
1136                         os_info->debug_offset, os_info->size);
1137                 release_firmware(nvavp_os_fw);
1138         }
1139
1140         memcpy(os_info->data, os_info->os_bin, os_info->size);
1141         os_info->reset_addr = os_info->phys + os_info->entry_offset;
1142
1143         dev_dbg(&nvavp->nvhost_dev->dev,
1144                 "AVP os at vaddr=%p paddr=%llx reset_addr=%llx\n",
1145                 os_info->data, (u64)(os_info->phys), (u64)os_info->reset_addr);
1146         return 0;
1147
1148 err_os_bin:
1149         release_firmware(nvavp_os_fw);
1150 err_req_fw:
1151         return ret;
1152 }
1153
1154
1155 static int nvavp_os_init(struct nvavp_info *nvavp)
1156 {
1157         char fw_os_file[32];
1158         int ret = 0;
1159         int video_initialized, audio_initialized = 0;
1160
1161         video_initialized = nvavp_get_video_init_status(nvavp);
1162
1163 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1164         audio_initialized = nvavp_get_audio_init_status(nvavp);
1165 #endif
1166         pr_debug("video_initialized(%d) audio_initialized(%d)\n",
1167                 video_initialized, audio_initialized);
1168
1169         /* Video and Audio both are initialized */
1170         if (video_initialized || audio_initialized)
1171                 return ret;
1172
1173         /* Video or Audio both are uninitialized */
1174         pr_debug("video_initialized == audio_initialized (%d)\n",
1175                 nvavp->video_initialized);
1176 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU) /* Tegra2 with AVP MMU */
1177         /* paddr is phys address */
1178         /* vaddr is AVP_KERNEL_VIRT_BASE */
1179         dev_dbg(&nvavp->nvhost_dev->dev,
1180                 "using AVP MMU to relocate AVP os\n");
1181         sprintf(fw_os_file, "nvavp_os.bin");
1182         nvavp->os_info.reset_addr = AVP_KERNEL_VIRT_BASE;
1183 #elif defined(CONFIG_TEGRA_AVP_KERNEL_ON_SMMU) /* Tegra3 with SMMU */
1184         /* paddr is any address behind SMMU */
1185         /* vaddr is TEGRA_SMMU_BASE */
1186         dev_dbg(&nvavp->nvhost_dev->dev,
1187                 "using SMMU at %lx to load AVP kernel\n",
1188                 (unsigned long)nvavp->os_info.phys);
1189         BUG_ON(nvavp->os_info.phys != 0xeff00000
1190                 && nvavp->os_info.phys != 0x0ff00000
1191                 && nvavp->os_info.phys != 0x8ff00000);
1192         sprintf(fw_os_file, "nvavp_os_%08lx.bin",
1193                 (unsigned long)nvavp->os_info.phys);
1194         nvavp->os_info.reset_addr = nvavp->os_info.phys;
1195 #else /* nvmem= carveout */
1196         dev_dbg(&nvavp->nvhost_dev->dev,
1197                 "using nvmem= carveout at %llx to load AVP os\n",
1198                 (u64)nvavp->os_info.phys);
1199         sprintf(fw_os_file, "nvavp_os_%08llx.bin", (u64)nvavp->os_info.phys);
1200         nvavp->os_info.reset_addr = nvavp->os_info.phys;
1201         nvavp->os_info.data = ioremap(nvavp->os_info.phys, SZ_1M);
1202 #endif
1203         ret = nvavp_load_os(nvavp, fw_os_file);
1204         if (ret) {
1205                 dev_err(&nvavp->nvhost_dev->dev,
1206                         "unable to load os firmware '%s'\n", fw_os_file);
1207                 goto err_exit;
1208         }
1209
1210         ret = nvavp_pushbuffer_init(nvavp);
1211         if (ret) {
1212                 dev_err(&nvavp->nvhost_dev->dev,
1213                         "unable to init pushbuffer\n");
1214                 goto err_exit;
1215         }
1216         tegra_init_legacy_irq_cop();
1217         enable_irq(nvavp->mbox_from_avp_pend_irq);
1218 err_exit:
1219         return ret;
1220 }
1221
1222 static int nvavp_init(struct nvavp_info *nvavp, int channel_id)
1223 {
1224         int ret = 0;
1225         int video_initialized = 0, audio_initialized = 0;
1226
1227         nvavp->init_task = current;
1228
1229         ret = nvavp_os_init(nvavp);
1230         if (ret) {
1231                 dev_err(&nvavp->nvhost_dev->dev,
1232                         "unable to load os firmware and allocate buffers\n");
1233         }
1234
1235         video_initialized = nvavp_get_video_init_status(nvavp);
1236 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1237         audio_initialized = nvavp_get_audio_init_status(nvavp);
1238 #endif
1239
1240         if (IS_VIDEO_CHANNEL_ID(channel_id) && (!video_initialized)) {
1241                 pr_debug("nvavp_init : channel_ID (%d)\n", channel_id);
1242                 ret = nvavp_load_ucode(nvavp);
1243                 if (ret) {
1244                         dev_err(&nvavp->nvhost_dev->dev,
1245                                 "unable to load ucode\n");
1246                         goto err_exit;
1247                 }
1248
1249                 nvavp_reset_vde(nvavp);
1250                 nvavp_reset_avp(nvavp, nvavp->os_info.reset_addr);
1251
1252                 nvavp_set_video_init_status(nvavp, 1);
1253         }
1254 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1255         if (IS_AUDIO_CHANNEL_ID(channel_id) && (!audio_initialized)) {
1256                 pr_debug("nvavp_init : channel_ID (%d)\n", channel_id);
1257                 nvavp_reset_avp(nvavp, nvavp->os_info.reset_addr);
1258                 nvavp_set_audio_init_status(nvavp, 1);
1259         }
1260 #endif
1261
1262 err_exit:
1263         nvavp->init_task = NULL;
1264         return ret;
1265 }
1266
1267 #define TIMER_EN        (1 << 31)
1268 #define TIMER_PERIODIC  (1 << 30)
1269 #define TIMER_PCR       0x4
1270 #define TIMER_PCR_INTR  (1 << 30)
1271
1272 static void nvavp_uninit(struct nvavp_info *nvavp)
1273 {
1274         int video_initialized, audio_initialized = 0;
1275         unsigned int reg;
1276
1277         video_initialized = nvavp_get_video_init_status(nvavp);
1278
1279 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1280         audio_initialized = nvavp_get_audio_init_status(nvavp);
1281 #endif
1282
1283         pr_debug("nvavp_uninit video_initialized(%d) audio_initialized(%d)\n",
1284                 video_initialized, audio_initialized);
1285
1286         /* Video and Audio both are uninitialized */
1287         if (!video_initialized && !audio_initialized)
1288                 return;
1289
1290         nvavp->init_task = current;
1291
1292         if (video_initialized) {
1293                 pr_debug("nvavp_uninit nvavp->video_initialized\n");
1294                 cancel_work_sync(&nvavp->clock_disable_work);
1295                 nvavp_halt_vde(nvavp);
1296                 nvavp_set_video_init_status(nvavp, 0);
1297                 video_initialized = 0;
1298         }
1299
1300 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1301         if (audio_initialized) {
1302                 cancel_work_sync(&nvavp->app_notify_work);
1303                 nvavp_set_audio_init_status(nvavp, 0);
1304                 audio_initialized = 0;
1305         }
1306 #endif
1307
1308         /* Video and Audio both becomes uninitialized */
1309         if (!video_initialized && !audio_initialized) {
1310                 pr_debug("nvavp_uninit both channels uninitialized\n");
1311
1312                 clk_disable_unprepare(nvavp->sclk);
1313                 clk_disable_unprepare(nvavp->emc_clk);
1314                 disable_irq(nvavp->mbox_from_avp_pend_irq);
1315                 nvavp_pushbuffer_deinit(nvavp);
1316                 nvavp_halt_avp(nvavp);
1317         }
1318
1319         /*
1320          * WAR: turn off TMR2 for fix LP1 wake up by TMR2.
1321          * turn off the periodic interrupt and the timer temporarily
1322          */
1323         reg = timer_readl(TIMER2_OFFSET + TIMER_PTV);
1324         reg &= ~(TIMER_EN | TIMER_PERIODIC);
1325         timer_writel(reg, TIMER2_OFFSET + TIMER_PTV);
1326
1327         /* write a 1 to the intr_clr field to clear the interrupt */
1328         reg = TIMER_PCR_INTR;
1329         timer_writel(reg, TIMER2_OFFSET + TIMER_PCR);
1330         nvavp->init_task = NULL;
1331 }
1332
1333 static int nvcpu_set_clock(struct nvavp_info *nvavp,
1334                                 struct nvavp_clock_args config,
1335                                 unsigned long arg)
1336 {
1337         dev_dbg(&nvavp->nvhost_dev->dev, "%s: update cpu freq to clk_rate=%u\n",
1338                         __func__, config.rate);
1339
1340         if (config.rate > 0)
1341                 pm_qos_update_request(&nvavp->min_cpu_freq_req, config.rate);
1342         else
1343                 pm_qos_update_request(&nvavp->min_cpu_freq_req,
1344                                         PM_QOS_CPU_FREQ_MIN_DEFAULT_VALUE);
1345
1346         return 0;
1347 }
1348
1349 static int nvavp_map_iova(struct file *filp, unsigned int cmd,
1350                                                         unsigned long arg)
1351 {
1352         struct nvavp_clientctx *clientctx = filp->private_data;
1353         struct nvavp_info *nvavp = clientctx->nvavp;
1354         struct nvavp_map_args map_arg;
1355         struct dma_buf *dmabuf;
1356         dma_addr_t addr = 0;
1357         int ret = 0;
1358
1359         if (copy_from_user(&map_arg, (void __user *)arg,
1360                 sizeof(struct nvavp_map_args))) {
1361                 dev_err(&nvavp->nvhost_dev->dev,
1362                         "failed to copy memory handle\n");
1363                 return -EFAULT;
1364         }
1365         if (!map_arg.fd) {
1366                 dev_err(&nvavp->nvhost_dev->dev,
1367                         "invalid memory handle %08x\n", map_arg.fd);
1368                 return -EINVAL;
1369         }
1370
1371         dmabuf = dma_buf_get(map_arg.fd);
1372         if (IS_ERR(dmabuf)) {
1373                 dev_err(&nvavp->nvhost_dev->dev,
1374                         "invalid buffer handle %08x\n", map_arg.fd);
1375                 return PTR_ERR(dmabuf);
1376         }
1377
1378         ret = nvavp_get_iova_addr(clientctx, dmabuf, &addr);
1379         if (ret)
1380                 goto out;
1381
1382         map_arg.addr = (__u32)addr;
1383
1384         if (copy_to_user((void __user *)arg, &map_arg,
1385                 sizeof(struct nvavp_map_args))) {
1386                 dev_err(&nvavp->nvhost_dev->dev,
1387                         "failed to copy phys addr\n");
1388                 ret = -EFAULT;
1389         }
1390
1391 out:
1392         return ret;
1393 }
1394
1395 static int nvavp_unmap_iova(struct file *filp, unsigned long arg)
1396 {
1397         struct nvavp_clientctx *clientctx = filp->private_data;
1398         struct nvavp_info *nvavp = clientctx->nvavp;
1399         struct nvavp_map_args map_arg;
1400         struct dma_buf *dmabuf;
1401
1402         if (copy_from_user(&map_arg, (void __user *)arg,
1403                 sizeof(struct nvavp_map_args))) {
1404                 dev_err(&nvavp->nvhost_dev->dev,
1405                         "failed to copy memory handle\n");
1406                 return -EFAULT;
1407         }
1408
1409         dmabuf = dma_buf_get(map_arg.fd);
1410         if (IS_ERR(dmabuf)) {
1411                 dev_err(&nvavp->nvhost_dev->dev,
1412                         "invalid buffer handle %08x\n", map_arg.fd);
1413                 return PTR_ERR(dmabuf);
1414         }
1415
1416         nvavp_release_iova_addr(clientctx, dmabuf, (dma_addr_t)map_arg.addr);
1417         dma_buf_put(dmabuf);
1418
1419         return 0;
1420 }
1421
1422 static int nvavp_set_clock_ioctl(struct file *filp, unsigned int cmd,
1423                                                         unsigned long arg)
1424 {
1425         struct nvavp_clientctx *clientctx = filp->private_data;
1426         struct nvavp_info *nvavp = clientctx->nvavp;
1427         struct clk *c;
1428         struct nvavp_clock_args config;
1429
1430         if (copy_from_user(&config, (void __user *)arg, sizeof(struct nvavp_clock_args)))
1431                 return -EFAULT;
1432
1433         dev_dbg(&nvavp->nvhost_dev->dev, "%s: clk_id=%d, clk_rate=%u\n",
1434                         __func__, config.id, config.rate);
1435
1436         if (config.id == NVAVP_MODULE_ID_AVP)
1437                 nvavp->sclk_rate = config.rate;
1438         else if (config.id == NVAVP_MODULE_ID_EMC)
1439                 nvavp->emc_clk_rate = config.rate;
1440         else if (config.id == NVAVP_MODULE_ID_CPU)
1441                 return nvcpu_set_clock(nvavp, config, arg);
1442
1443         c = nvavp_clk_get(nvavp, config.id);
1444         if (IS_ERR_OR_NULL(c))
1445                 return -EINVAL;
1446
1447         clk_prepare_enable(c);
1448         clk_set_rate(c, config.rate);
1449
1450         config.rate = clk_get_rate(c);
1451         clk_disable_unprepare(c);
1452         if (copy_to_user((void __user *)arg, &config, sizeof(struct nvavp_clock_args)))
1453                 return -EFAULT;
1454
1455         return 0;
1456 }
1457
1458 static int nvavp_get_clock_ioctl(struct file *filp, unsigned int cmd,
1459                                                         unsigned long arg)
1460 {
1461         struct nvavp_clientctx *clientctx = filp->private_data;
1462         struct nvavp_info *nvavp = clientctx->nvavp;
1463         struct clk *c;
1464         struct nvavp_clock_args config;
1465
1466         if (copy_from_user(&config, (void __user *)arg, sizeof(struct nvavp_clock_args)))
1467                 return -EFAULT;
1468
1469         c = nvavp_clk_get(nvavp, config.id);
1470         if (IS_ERR_OR_NULL(c))
1471                 return -EINVAL;
1472
1473         clk_prepare_enable(c);
1474         config.rate = clk_get_rate(c);
1475         clk_disable_unprepare(c);
1476
1477         if (copy_to_user((void __user *)arg, &config, sizeof(struct nvavp_clock_args)))
1478                 return -EFAULT;
1479
1480         return 0;
1481 }
1482
1483 static int nvavp_get_syncpointid_ioctl(struct file *filp, unsigned int cmd,
1484                                                         unsigned long arg)
1485 {
1486         struct nvavp_clientctx *clientctx = filp->private_data;
1487         struct nvavp_info *nvavp = clientctx->nvavp;
1488         u32 id = nvavp->syncpt_id;
1489
1490         if (_IOC_DIR(cmd) & _IOC_READ) {
1491                 if (copy_to_user((void __user *)arg, &id, sizeof(u32)))
1492                         return -EFAULT;
1493                 else
1494                         return 0;
1495         }
1496         return -EFAULT;
1497 }
1498
1499 static int nvavp_pushbuffer_submit_ioctl(struct file *filp, unsigned int cmd,
1500                                                         unsigned long arg)
1501 {
1502         struct nvavp_clientctx *clientctx = filp->private_data;
1503         struct nvavp_info *nvavp = clientctx->nvavp;
1504         struct nvavp_pushbuffer_submit_hdr hdr;
1505         u32 *cmdbuf_data;
1506         struct dma_buf *cmdbuf_dmabuf;
1507         struct dma_buf_attachment *cmdbuf_attach;
1508         struct sg_table *cmdbuf_sgt;
1509         int ret = 0, i;
1510         phys_addr_t phys_addr;
1511         unsigned long virt_addr;
1512         struct nvavp_pushbuffer_submit_hdr *user_hdr =
1513                         (struct nvavp_pushbuffer_submit_hdr *) arg;
1514         struct nvavp_syncpt syncpt;
1515
1516         syncpt.id = NVSYNCPT_INVALID;
1517         syncpt.value = 0;
1518
1519         if (_IOC_DIR(cmd) & _IOC_WRITE) {
1520                 if (copy_from_user(&hdr, (void __user *)arg,
1521                         sizeof(struct nvavp_pushbuffer_submit_hdr)))
1522                         return -EFAULT;
1523         }
1524
1525         if (!hdr.cmdbuf.mem)
1526                 return 0;
1527
1528         if (copy_from_user(clientctx->relocs, (void __user *)hdr.relocs,
1529                         sizeof(struct nvavp_reloc) * hdr.num_relocs)) {
1530                 return -EFAULT;
1531         }
1532
1533         cmdbuf_dmabuf = dma_buf_get(hdr.cmdbuf.mem);
1534         if (IS_ERR(cmdbuf_dmabuf)) {
1535                 dev_err(&nvavp->nvhost_dev->dev,
1536                         "invalid cmd buffer handle %08x\n", hdr.cmdbuf.mem);
1537                 return PTR_ERR(cmdbuf_dmabuf);
1538         }
1539
1540         cmdbuf_attach = dma_buf_attach(cmdbuf_dmabuf, &nvavp->nvhost_dev->dev);
1541         if (IS_ERR(cmdbuf_attach)) {
1542                 dev_err(&nvavp->nvhost_dev->dev, "cannot attach cmdbuf_dmabuf\n");
1543                 ret = PTR_ERR(cmdbuf_attach);
1544                 goto err_dmabuf_attach;
1545         }
1546
1547         cmdbuf_sgt = dma_buf_map_attachment(cmdbuf_attach, DMA_BIDIRECTIONAL);
1548         if (IS_ERR(cmdbuf_sgt)) {
1549                 dev_err(&nvavp->nvhost_dev->dev, "cannot map cmdbuf_dmabuf\n");
1550                 ret = PTR_ERR(cmdbuf_sgt);
1551                 goto err_dmabuf_map;
1552         }
1553
1554         phys_addr = sg_dma_address(cmdbuf_sgt->sgl);
1555
1556         virt_addr = (unsigned long)dma_buf_vmap(cmdbuf_dmabuf);
1557         if (!virt_addr) {
1558                 dev_err(&nvavp->nvhost_dev->dev, "cannot vmap cmdbuf_dmabuf\n");
1559                 ret = -ENOMEM;
1560                 goto err_dmabuf_vmap;
1561         }
1562
1563         cmdbuf_data = (u32 *)(virt_addr + hdr.cmdbuf.offset);
1564         for (i = 0; i < hdr.num_relocs; i++) {
1565                 struct dma_buf *target_dmabuf;
1566                 struct dma_buf_attachment *target_attach;
1567                 struct sg_table *target_sgt;
1568                 u32 *reloc_addr, target_phys_addr;
1569
1570                 if (clientctx->relocs[i].cmdbuf_mem != hdr.cmdbuf.mem) {
1571                         dev_err(&nvavp->nvhost_dev->dev,
1572                                 "reloc info does not match target bufferID\n");
1573                         ret = -EPERM;
1574                         goto err_reloc_info;
1575                 }
1576
1577                 reloc_addr = cmdbuf_data +
1578                              (clientctx->relocs[i].cmdbuf_offset >> 2);
1579
1580                 target_dmabuf = dma_buf_get(clientctx->relocs[i].target);
1581                 if (IS_ERR(target_dmabuf)) {
1582                         ret = PTR_ERR(target_dmabuf);
1583                         goto target_dmabuf_fail;
1584                 }
1585                 target_attach = dma_buf_attach(target_dmabuf,
1586                                                &nvavp->nvhost_dev->dev);
1587                 if (IS_ERR(target_attach)) {
1588                         ret = PTR_ERR(target_attach);
1589                         goto target_attach_fail;
1590                 }
1591                 target_sgt = dma_buf_map_attachment(target_attach,
1592                                                     DMA_BIDIRECTIONAL);
1593                 if (IS_ERR(target_sgt)) {
1594                         ret = PTR_ERR(target_sgt);
1595                         goto target_map_fail;
1596                 }
1597
1598                 target_phys_addr = sg_dma_address(target_sgt->sgl);
1599                 if (!target_phys_addr)
1600                         target_phys_addr = sg_phys(target_sgt->sgl);
1601                 target_phys_addr += clientctx->relocs[i].target_offset;
1602                 writel(target_phys_addr, reloc_addr);
1603                 dma_buf_unmap_attachment(target_attach, target_sgt,
1604                                          DMA_BIDIRECTIONAL);
1605 target_map_fail:
1606                 dma_buf_detach(target_dmabuf, target_attach);
1607 target_attach_fail:
1608                 dma_buf_put(target_dmabuf);
1609 target_dmabuf_fail:
1610                 if (ret != 0)
1611                         goto err_reloc_info;
1612         }
1613
1614         if (hdr.syncpt) {
1615                 ret = nvavp_pushbuffer_update(nvavp,
1616                                              (phys_addr + hdr.cmdbuf.offset),
1617                                               hdr.cmdbuf.words, &syncpt,
1618                                               (hdr.flags & NVAVP_UCODE_EXT),
1619                                                 clientctx->channel_id);
1620
1621                 if (copy_to_user((void __user *)user_hdr->syncpt, &syncpt,
1622                                 sizeof(struct nvavp_syncpt))) {
1623                         ret = -EFAULT;
1624                         goto err_reloc_info;
1625                 }
1626         } else {
1627                 ret = nvavp_pushbuffer_update(nvavp,
1628                                              (phys_addr + hdr.cmdbuf.offset),
1629                                               hdr.cmdbuf.words, NULL,
1630                                               (hdr.flags & NVAVP_UCODE_EXT),
1631                                                 clientctx->channel_id);
1632         }
1633
1634 err_reloc_info:
1635         dma_buf_vunmap(cmdbuf_dmabuf, (void *)virt_addr);
1636 err_dmabuf_vmap:
1637         dma_buf_unmap_attachment(cmdbuf_attach, cmdbuf_sgt, DMA_BIDIRECTIONAL);
1638 err_dmabuf_map:
1639         dma_buf_detach(cmdbuf_dmabuf, cmdbuf_attach);
1640 err_dmabuf_attach:
1641         dma_buf_put(cmdbuf_dmabuf);
1642         return ret;
1643 }
1644
1645 #ifdef CONFIG_COMPAT
1646 static int nvavp_pushbuffer_submit_compat_ioctl(struct file *filp,
1647                                                         unsigned int cmd,
1648                                                         unsigned long arg)
1649 {
1650         struct nvavp_clientctx *clientctx = filp->private_data;
1651         struct nvavp_info *nvavp = clientctx->nvavp;
1652         struct nvavp_pushbuffer_submit_hdr_v32 hdr_v32;
1653         struct nvavp_pushbuffer_submit_hdr __user *user_hdr;
1654         int ret = 0;
1655
1656         if (_IOC_DIR(cmd) & _IOC_WRITE) {
1657                 if (copy_from_user(&hdr_v32, (void __user *)arg,
1658                         sizeof(struct nvavp_pushbuffer_submit_hdr_v32)))
1659                         return -EFAULT;
1660         }
1661
1662         if (!hdr_v32.cmdbuf.mem)
1663                 return 0;
1664
1665         user_hdr = compat_alloc_user_space(sizeof(*user_hdr));
1666         if (!access_ok(VERIFY_WRITE, user_hdr, sizeof(*user_hdr)))
1667                 return -EFAULT;
1668
1669         if (__put_user(hdr_v32.cmdbuf.mem, &user_hdr->cmdbuf.mem)
1670             || __put_user(hdr_v32.cmdbuf.offset, &user_hdr->cmdbuf.offset)
1671             || __put_user(hdr_v32.cmdbuf.words, &user_hdr->cmdbuf.words)
1672             || __put_user((void __user *)(unsigned long)hdr_v32.relocs,
1673                           &user_hdr->relocs)
1674             || __put_user(hdr_v32.num_relocs, &user_hdr->num_relocs)
1675             || __put_user((void __user *)(unsigned long)hdr_v32.syncpt,
1676                           &user_hdr->syncpt)
1677             || __put_user(hdr_v32.flags, &user_hdr->flags))
1678                 return -EFAULT;
1679
1680         ret = nvavp_pushbuffer_submit_ioctl(filp, cmd, (unsigned long)user_hdr);
1681         if (ret)
1682                 return ret;
1683
1684         if (__get_user(hdr_v32.syncpt, &user_hdr->syncpt))
1685                 return -EFAULT;
1686
1687         if (copy_to_user((void __user *)arg, &hdr_v32,
1688                           sizeof(struct nvavp_pushbuffer_submit_hdr_v32))) {
1689                 ret = -EFAULT;
1690         }
1691
1692         return ret;
1693 }
1694 #endif
1695
1696 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1697 int nvavp_pushbuffer_submit_audio(nvavp_clientctx_t client, int cmd_buf_phys,
1698                                   int cmd_buf_words)
1699 {
1700         struct nvavp_clientctx *clientctx = client;
1701         struct nvavp_info *nvavp = clientctx->nvavp;
1702
1703         return nvavp_pushbuffer_update(nvavp,
1704                                       cmd_buf_phys,
1705                                       cmd_buf_words, NULL,
1706                                       NVAVP_UCODE_EXT,
1707                                       NVAVP_AUDIO_CHANNEL);
1708 }
1709 EXPORT_SYMBOL_GPL(nvavp_pushbuffer_submit_audio);
1710
1711 void nvavp_register_audio_cb(nvavp_clientctx_t client, void (*cb)(void))
1712 {
1713         struct nvavp_clientctx *clientctx = client;
1714         struct nvavp_info *nvavp = clientctx->nvavp;
1715
1716         nvavp->audio_notify = cb;
1717 }
1718 EXPORT_SYMBOL_GPL(nvavp_register_audio_cb);
1719 #endif
1720
1721 static int nvavp_wake_avp_ioctl(struct file *filp, unsigned int cmd,
1722                                                         unsigned long arg)
1723 {
1724         wmb();
1725         /* wake up avp */
1726         return nvavp_outbox_write(0xA0000001);
1727 }
1728
1729 static int nvavp_force_clock_stay_on_ioctl(struct file *filp, unsigned int cmd,
1730                                                         unsigned long arg)
1731 {
1732         struct nvavp_clientctx *clientctx = filp->private_data;
1733         struct nvavp_info *nvavp = clientctx->nvavp;
1734         struct nvavp_clock_stay_on_state_args clock;
1735
1736         if (copy_from_user(&clock, (void __user *)arg,
1737                            sizeof(struct nvavp_clock_stay_on_state_args)))
1738                 return -EFAULT;
1739
1740         dev_dbg(&nvavp->nvhost_dev->dev, "%s: state=%d\n",
1741                 __func__, clock.state);
1742
1743         if (clock.state != NVAVP_CLOCK_STAY_ON_DISABLED &&
1744                    clock.state !=  NVAVP_CLOCK_STAY_ON_ENABLED) {
1745                 dev_err(&nvavp->nvhost_dev->dev, "%s: invalid argument=%d\n",
1746                         __func__, clock.state);
1747                 return -EINVAL;
1748         }
1749
1750         if (clock.state) {
1751                 mutex_lock(&nvavp->open_lock);
1752                 if (clientctx->clk_reqs++ == 0) {
1753                         nvavp_clks_enable(nvavp);
1754                         nvavp->stay_on = true;
1755                 }
1756                 mutex_unlock(&nvavp->open_lock);
1757                 cancel_work_sync(&nvavp->clock_disable_work);
1758         } else {
1759                 mutex_lock(&nvavp->open_lock);
1760                 if (--clientctx->clk_reqs == 0) {
1761                         nvavp->stay_on = false;
1762                         nvavp_clks_disable(nvavp);
1763                 }
1764                 mutex_unlock(&nvavp->open_lock);
1765                 if (!nvavp->stay_on)
1766                         schedule_work(&nvavp->clock_disable_work);
1767         }
1768         return 0;
1769 }
1770
1771 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1772 int nvavp_enable_audio_clocks(nvavp_clientctx_t client, u32 clk_id)
1773 {
1774         struct nvavp_clientctx *clientctx = client;
1775         struct nvavp_info *nvavp = clientctx->nvavp;
1776
1777         dev_dbg(&nvavp->nvhost_dev->dev, "%s: clk_id = %d\n",
1778                         __func__, clk_id);
1779
1780         mutex_lock(&nvavp->open_lock);
1781         if (clk_id == NVAVP_MODULE_ID_VCP)
1782                 clk_prepare_enable(nvavp->vcp_clk);
1783         else if (clk_id == NVAVP_MODULE_ID_BSEA)
1784                 clk_prepare_enable(nvavp->bsea_clk);
1785         mutex_unlock(&nvavp->open_lock);
1786         return 0;
1787 }
1788 EXPORT_SYMBOL_GPL(nvavp_enable_audio_clocks);
1789
1790 int nvavp_disable_audio_clocks(nvavp_clientctx_t client, u32 clk_id)
1791 {
1792         struct nvavp_clientctx *clientctx = client;
1793         struct nvavp_info *nvavp = clientctx->nvavp;
1794
1795         dev_dbg(&nvavp->nvhost_dev->dev, "%s: clk_id = %d\n",
1796                         __func__, clk_id);
1797
1798         mutex_lock(&nvavp->open_lock);
1799         if (clk_id == NVAVP_MODULE_ID_VCP)
1800                 clk_disable_unprepare(nvavp->vcp_clk);
1801         else if (clk_id == NVAVP_MODULE_ID_BSEA)
1802                 clk_disable_unprepare(nvavp->bsea_clk);
1803         mutex_unlock(&nvavp->open_lock);
1804         return 0;
1805 }
1806 EXPORT_SYMBOL_GPL(nvavp_disable_audio_clocks);
1807 #else
1808 int nvavp_enable_audio_clocks(nvavp_clientctx_t client, u32 clk_id)
1809 {
1810         return 0;
1811 }
1812 EXPORT_SYMBOL_GPL(nvavp_enable_audio_clocks);
1813
1814 int nvavp_disable_audio_clocks(nvavp_clientctx_t client, u32_clk_id)
1815 {
1816         return 0;
1817 }
1818 EXPORT_SYMBOL_GPL(nvavp_disable_audio_clocks);
1819 #endif
1820
1821 static int nvavp_set_min_online_cpus_ioctl(struct file *filp, unsigned int cmd,
1822                                         unsigned long arg)
1823 {
1824         struct nvavp_clientctx *clientctx = filp->private_data;
1825         struct nvavp_info *nvavp = clientctx->nvavp;
1826         struct nvavp_num_cpus_args config;
1827
1828         if (copy_from_user(&config, (void __user *)arg,
1829                                         sizeof(struct nvavp_num_cpus_args)))
1830                 return -EFAULT;
1831
1832         dev_dbg(&nvavp->nvhost_dev->dev, "%s: min_online_cpus=%d\n",
1833                         __func__, config.min_online_cpus);
1834
1835         if (config.min_online_cpus > 0)
1836                 pm_qos_update_request(&nvavp->min_online_cpus_req,
1837                                         config.min_online_cpus);
1838         else
1839                 pm_qos_update_request(&nvavp->min_online_cpus_req,
1840                                         PM_QOS_CPU_FREQ_MIN_DEFAULT_VALUE);
1841
1842         return 0;
1843 }
1844
1845 static int tegra_nvavp_open(struct nvavp_info *nvavp,
1846                         struct nvavp_clientctx **client, int channel_id)
1847 {
1848         struct nvavp_clientctx *clientctx;
1849         int ret = 0;
1850
1851         dev_dbg(&nvavp->nvhost_dev->dev, "%s: ++\n", __func__);
1852
1853         clientctx = kzalloc(sizeof(*clientctx), GFP_KERNEL);
1854         if (!clientctx)
1855                 return -ENOMEM;
1856
1857         pr_debug("tegra_nvavp_open channel_id (%d)\n", channel_id);
1858
1859         clientctx->channel_id = channel_id;
1860
1861         ret = nvavp_init(nvavp, channel_id);
1862
1863         if (!ret) {
1864                 nvavp->refcount++;
1865                 if (IS_VIDEO_CHANNEL_ID(channel_id))
1866                         nvavp->video_refcnt++;
1867                 if (IS_AUDIO_CHANNEL_ID(channel_id))
1868                         nvavp->audio_refcnt++;
1869         }
1870
1871         clientctx->nvavp = nvavp;
1872         clientctx->iova_handles = RB_ROOT;
1873         *client = clientctx;
1874
1875         return ret;
1876 }
1877
1878 static int tegra_nvavp_video_open(struct inode *inode, struct file *filp)
1879 {
1880         struct miscdevice *miscdev = filp->private_data;
1881         struct nvavp_info *nvavp = dev_get_drvdata(miscdev->parent);
1882         struct nvavp_clientctx *clientctx;
1883         int ret = 0;
1884
1885         pr_debug("tegra_nvavp_video_open NVAVP_VIDEO_CHANNEL\n");
1886
1887         nonseekable_open(inode, filp);
1888
1889         mutex_lock(&nvavp->open_lock);
1890         ret = tegra_nvavp_open(nvavp, &clientctx, NVAVP_VIDEO_CHANNEL);
1891         filp->private_data = clientctx;
1892         mutex_unlock(&nvavp->open_lock);
1893
1894         return ret;
1895 }
1896
1897 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1898 static int tegra_nvavp_audio_open(struct inode *inode, struct file *filp)
1899 {
1900         struct miscdevice *miscdev = filp->private_data;
1901         struct nvavp_info *nvavp = dev_get_drvdata(miscdev->parent);
1902         struct nvavp_clientctx *clientctx;
1903         int ret = 0;
1904
1905         pr_debug("tegra_nvavp_audio_open NVAVP_AUDIO_CHANNEL\n");
1906
1907         nonseekable_open(inode, filp);
1908
1909         mutex_lock(&nvavp->open_lock);
1910         ret = tegra_nvavp_open(nvavp, &clientctx, NVAVP_AUDIO_CHANNEL);
1911         filp->private_data = clientctx;
1912         mutex_unlock(&nvavp->open_lock);
1913
1914         return ret;
1915 }
1916
1917 int tegra_nvavp_audio_client_open(nvavp_clientctx_t *clientctx)
1918 {
1919         struct nvavp_info *nvavp = nvavp_info_ctx;
1920         int ret = 0;
1921
1922         mutex_lock(&nvavp->open_lock);
1923         ret = tegra_nvavp_open(nvavp, (struct nvavp_clientctx **)clientctx,
1924                                 NVAVP_AUDIO_CHANNEL);
1925         mutex_unlock(&nvavp->open_lock);
1926
1927         return ret;
1928 }
1929 EXPORT_SYMBOL_GPL(tegra_nvavp_audio_client_open);
1930 #endif
1931
1932 static int tegra_nvavp_release(struct nvavp_clientctx *clientctx,
1933                                int channel_id)
1934 {
1935         struct nvavp_info *nvavp = clientctx->nvavp;
1936         int ret = 0;
1937
1938         dev_dbg(&nvavp->nvhost_dev->dev, "%s: ++\n", __func__);
1939
1940         if (!nvavp->refcount) {
1941                 dev_err(&nvavp->nvhost_dev->dev,
1942                         "releasing while in invalid state\n");
1943                 ret = -EINVAL;
1944                 goto out;
1945         }
1946
1947         /* if this client had any requests, drop our clk ref */
1948         if (clientctx->clk_reqs)
1949                 nvavp_clks_disable(nvavp);
1950
1951         if (nvavp->refcount > 0)
1952                 nvavp->refcount--;
1953         if (!nvavp->refcount) {
1954                 mutex_unlock(&nvavp->open_lock);
1955                 nvavp_uninit(nvavp);
1956                 mutex_lock(&nvavp->open_lock);
1957         }
1958
1959         if (IS_VIDEO_CHANNEL_ID(channel_id))
1960                 nvavp->video_refcnt--;
1961         if (IS_AUDIO_CHANNEL_ID(channel_id))
1962                 nvavp->audio_refcnt--;
1963
1964 out:
1965         nvavp_remove_iova_mapping(clientctx);
1966         kfree(clientctx);
1967         return ret;
1968 }
1969
1970 static int tegra_nvavp_video_release(struct inode *inode, struct file *filp)
1971 {
1972         struct nvavp_clientctx *clientctx = filp->private_data;
1973         struct nvavp_info *nvavp = clientctx->nvavp;
1974         int ret = 0;
1975
1976         mutex_lock(&nvavp->open_lock);
1977         filp->private_data = NULL;
1978         ret = tegra_nvavp_release(clientctx, NVAVP_VIDEO_CHANNEL);
1979         mutex_unlock(&nvavp->open_lock);
1980
1981         return ret;
1982 }
1983
1984 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1985 static int tegra_nvavp_audio_release(struct inode *inode,
1986                                           struct file *filp)
1987 {
1988         struct nvavp_clientctx *clientctx = filp->private_data;
1989         struct nvavp_info *nvavp = clientctx->nvavp;
1990         int ret = 0;
1991
1992         mutex_lock(&nvavp->open_lock);
1993         filp->private_data = NULL;
1994         ret = tegra_nvavp_release(clientctx, NVAVP_AUDIO_CHANNEL);
1995         mutex_unlock(&nvavp->open_lock);
1996
1997         return ret;
1998 }
1999
2000 int tegra_nvavp_audio_client_release(nvavp_clientctx_t client)
2001 {
2002         struct nvavp_clientctx *clientctx = client;
2003         struct nvavp_info *nvavp = clientctx->nvavp;
2004         int ret = 0;
2005
2006         mutex_lock(&nvavp->open_lock);
2007         ret = tegra_nvavp_release(clientctx, NVAVP_AUDIO_CHANNEL);
2008         mutex_unlock(&nvavp->open_lock);
2009
2010         return ret;
2011 }
2012 EXPORT_SYMBOL_GPL(tegra_nvavp_audio_client_release);
2013 #endif
2014
2015
2016 static int
2017 nvavp_channel_open(struct file *filp, struct nvavp_channel_open_args *arg)
2018 {
2019         int fd, err = 0;
2020         struct file *file;
2021         char *name;
2022         struct nvavp_clientctx *clientctx = filp->private_data;
2023         struct nvavp_info *nvavp = clientctx->nvavp;
2024
2025         err = get_unused_fd_flags(O_RDWR);
2026         if (err < 0)
2027                 return err;
2028
2029         fd = err;
2030
2031         name = kasprintf(GFP_KERNEL, "nvavp-channel-fd%d", fd);
2032         if (!name) {
2033                 err = -ENOMEM;
2034                 put_unused_fd(fd);
2035                 return err;
2036         }
2037
2038         file = anon_inode_getfile(name, filp->f_op, &(nvavp->video_misc_dev),
2039                         O_RDWR);
2040         kfree(name);
2041         if (IS_ERR(file)) {
2042                 err = PTR_ERR(file);
2043                 put_unused_fd(fd);
2044                 return err;
2045         }
2046
2047         fd_install(fd, file);
2048
2049         nonseekable_open(file->f_inode, filp);
2050         mutex_lock(&nvavp->open_lock);
2051         err = tegra_nvavp_open(nvavp,
2052                 (struct nvavp_clientctx **)&file->private_data,
2053                 clientctx->channel_id);
2054         if (err) {
2055                 put_unused_fd(fd);
2056                 fput(file);
2057                 mutex_unlock(&nvavp->open_lock);
2058                 return err;
2059         }
2060         mutex_unlock(&nvavp->open_lock);
2061
2062         arg->channel_fd = fd;
2063         return err;
2064 }
2065
2066 static long tegra_nvavp_ioctl(struct file *filp, unsigned int cmd,
2067                             unsigned long arg)
2068 {
2069         struct nvavp_clientctx *clientctx = filp->private_data;
2070         struct nvavp_clock_args config;
2071         int ret = 0;
2072         u8 buf[NVAVP_IOCTL_CHANNEL_MAX_ARG_SIZE];
2073
2074         if (_IOC_TYPE(cmd) != NVAVP_IOCTL_MAGIC ||
2075             _IOC_NR(cmd) < NVAVP_IOCTL_MIN_NR ||
2076             _IOC_NR(cmd) > NVAVP_IOCTL_MAX_NR)
2077                 return -EFAULT;
2078
2079         switch (cmd) {
2080         case NVAVP_IOCTL_SET_NVMAP_FD:
2081                 break;
2082         case NVAVP_IOCTL_GET_SYNCPOINT_ID:
2083                 ret = nvavp_get_syncpointid_ioctl(filp, cmd, arg);
2084                 break;
2085         case NVAVP_IOCTL_PUSH_BUFFER_SUBMIT:
2086                 ret = nvavp_pushbuffer_submit_ioctl(filp, cmd, arg);
2087                 break;
2088         case NVAVP_IOCTL_SET_CLOCK:
2089                 ret = nvavp_set_clock_ioctl(filp, cmd, arg);
2090                 break;
2091         case NVAVP_IOCTL_GET_CLOCK:
2092                 ret = nvavp_get_clock_ioctl(filp, cmd, arg);
2093                 break;
2094         case NVAVP_IOCTL_WAKE_AVP:
2095                 ret = nvavp_wake_avp_ioctl(filp, cmd, arg);
2096                 break;
2097         case NVAVP_IOCTL_FORCE_CLOCK_STAY_ON:
2098                 ret = nvavp_force_clock_stay_on_ioctl(filp, cmd, arg);
2099                 break;
2100         case NVAVP_IOCTL_ENABLE_AUDIO_CLOCKS:
2101                 if (copy_from_user(&config, (void __user *)arg,
2102                         sizeof(struct nvavp_clock_args))) {
2103                         ret = -EFAULT;
2104                         break;
2105                 }
2106                 ret = nvavp_enable_audio_clocks(clientctx, config.id);
2107                 break;
2108         case NVAVP_IOCTL_DISABLE_AUDIO_CLOCKS:
2109                 if (copy_from_user(&config, (void __user *)arg,
2110                         sizeof(struct nvavp_clock_args))) {
2111                         ret = -EFAULT;
2112                         break;
2113                 }
2114                 ret = nvavp_disable_audio_clocks(clientctx, config.id);
2115                 break;
2116         case NVAVP_IOCTL_SET_MIN_ONLINE_CPUS:
2117                 ret = nvavp_set_min_online_cpus_ioctl(filp, cmd, arg);
2118                 break;
2119         case NVAVP_IOCTL_MAP_IOVA:
2120                 ret = nvavp_map_iova(filp, cmd, arg);
2121                 break;
2122         case NVAVP_IOCTL_UNMAP_IOVA:
2123                 ret = nvavp_unmap_iova(filp, arg);
2124                 break;
2125         case NVAVP_IOCTL_CHANNEL_OPEN:
2126                 ret = nvavp_channel_open(filp, (void *)buf);
2127                 if (ret == 0)
2128                         ret = copy_to_user((void __user *)arg, buf,
2129                         _IOC_SIZE(cmd));
2130                 break;
2131         default:
2132                 ret = -EINVAL;
2133                 break;
2134         }
2135         return ret;
2136 }
2137
2138 #ifdef CONFIG_COMPAT
2139 static long tegra_nvavp_compat_ioctl(struct file *filp, unsigned int cmd,
2140                             unsigned long arg)
2141 {
2142         int ret = 0;
2143
2144         if (_IOC_TYPE(cmd) != NVAVP_IOCTL_MAGIC ||
2145             _IOC_NR(cmd) < NVAVP_IOCTL_MIN_NR ||
2146             _IOC_NR(cmd) > NVAVP_IOCTL_MAX_NR)
2147                 return -EFAULT;
2148
2149         switch (cmd) {
2150         case NVAVP_IOCTL_PUSH_BUFFER_SUBMIT32:
2151                 ret = nvavp_pushbuffer_submit_compat_ioctl(filp, cmd, arg);
2152                 break;
2153         default:
2154                 ret = tegra_nvavp_ioctl(filp, cmd, arg);
2155                 break;
2156         }
2157         return ret;
2158 }
2159 #endif
2160
2161 static const struct file_operations tegra_video_nvavp_fops = {
2162         .owner          = THIS_MODULE,
2163         .open           = tegra_nvavp_video_open,
2164         .release        = tegra_nvavp_video_release,
2165         .unlocked_ioctl = tegra_nvavp_ioctl,
2166 #ifdef CONFIG_COMPAT
2167         .compat_ioctl   = tegra_nvavp_compat_ioctl,
2168 #endif
2169 };
2170
2171 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2172 static const struct file_operations tegra_audio_nvavp_fops = {
2173         .owner          = THIS_MODULE,
2174         .open           = tegra_nvavp_audio_open,
2175         .release        = tegra_nvavp_audio_release,
2176         .unlocked_ioctl = tegra_nvavp_ioctl,
2177 #ifdef CONFIG_COMPAT
2178         .compat_ioctl   = tegra_nvavp_compat_ioctl,
2179 #endif
2180 };
2181 #endif
2182
2183 static ssize_t boost_sclk_show(struct device *dev,
2184         struct device_attribute *attr, char *buf)
2185 {
2186         return snprintf(buf, PAGE_SIZE, "%d\n", boost_sclk);
2187 }
2188
2189 static ssize_t boost_sclk_store(struct device *dev,
2190         struct device_attribute *attr, const char *buf, size_t count)
2191 {
2192         struct platform_device *ndev = to_platform_device(dev);
2193         struct nvavp_info *nvavp = platform_get_drvdata(ndev);
2194         unsigned long val = 0;
2195
2196         if (kstrtoul(buf, 10, &val) < 0)
2197                 return -EINVAL;
2198
2199         if (val)
2200                 clk_set_rate(nvavp->sclk, SCLK_BOOST_RATE);
2201         else if (!val)
2202                 clk_set_rate(nvavp->sclk, 0);
2203
2204         boost_sclk = val;
2205
2206         return count;
2207 }
2208
2209 DEVICE_ATTR(boost_sclk, S_IRUGO | S_IWUSR, boost_sclk_show, boost_sclk_store);
2210
2211 enum nvavp_heap {
2212         NVAVP_USE_SMMU = (1 << 0),
2213         NVAVP_USE_CARVEOUT = (1 << 1)
2214 };
2215
2216 static int nvavp_reserve_os_mem(struct nvavp_info *nvavp, dma_addr_t phys)
2217 {
2218         int ret = -ENOMEM;
2219         if (!pfn_valid(__phys_to_pfn(phys))) {
2220                 if (memblock_reserve(phys, SZ_1M)) {
2221                         dev_err(&nvavp->nvhost_dev->dev,
2222                                 "failed to reserve mem block %lx\n",
2223                                                 (unsigned long)phys);
2224                 } else
2225                         ret = 0;
2226         }
2227         return ret;
2228 }
2229
2230 #ifdef CONFIG_OF
2231 static struct of_device_id tegra_nvavp_of_match[] = {
2232         { .compatible = "nvidia,tegra30-nvavp", NULL },
2233         { .compatible = "nvidia,tegra114-nvavp", NULL },
2234         { .compatible = "nvidia,tegra124-nvavp", NULL },
2235         { },
2236 };
2237 #endif
2238
2239 static int tegra_nvavp_probe(struct platform_device *ndev)
2240 {
2241         struct nvavp_info *nvavp;
2242         int irq;
2243         enum nvavp_heap heap_mask;
2244         int ret = 0, channel_id;
2245         struct device_node *np;
2246
2247         np = ndev->dev.of_node;
2248         if (np) {
2249                 irq = platform_get_irq(ndev, 0);
2250                 nvavp_reg_base = of_iomap(np, 0);
2251         } else {
2252                 irq = platform_get_irq_byname(ndev, "mbox_from_nvavp_pending");
2253         }
2254
2255         if (irq < 0) {
2256                 dev_err(&ndev->dev, "invalid nvhost data\n");
2257                 return -EINVAL;
2258         }
2259
2260         if (!nvavp_reg_base) {
2261                 dev_err(&ndev->dev, "unable to map, memory mapped IO\n");
2262                 return -EINVAL;
2263         }
2264
2265         /* Set the max segment size supported. */
2266         ndev->dev.dma_parms = &nvavp_dma_parameters;
2267
2268         nvavp = kzalloc(sizeof(struct nvavp_info), GFP_KERNEL);
2269         if (!nvavp) {
2270                 dev_err(&ndev->dev, "cannot allocate avp_info\n");
2271                 return -ENOMEM;
2272         }
2273
2274         memset(nvavp, 0, sizeof(*nvavp));
2275
2276 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU) /* Tegra2 with AVP MMU */
2277         heap_mask = NVAVP_USE_CARVEOUT;
2278 #elif defined(CONFIG_TEGRA_AVP_KERNEL_ON_SMMU) /* Tegra3 with SMMU */
2279         heap_mask = NVAVP_USE_SMMU;
2280 #else /* nvmem= carveout */
2281         heap_mask = NVAVP_USE_CARVEOUT;
2282 #endif
2283         switch (heap_mask) {
2284         case NVAVP_USE_SMMU:
2285
2286                 nvavp->os_info.phys = 0x8ff00000;
2287                 nvavp->os_info.data = dma_alloc_at_coherent(
2288                                                         &ndev->dev,
2289                                                         SZ_1M,
2290                                                         &nvavp->os_info.phys,
2291                                                         GFP_KERNEL);
2292
2293                 if (!nvavp->os_info.data || nvavp->os_info.phys != 0x8ff00000) {
2294                         nvavp->os_info.phys = 0x0ff00000;
2295                         nvavp->os_info.data = dma_alloc_at_coherent(
2296                                                         &ndev->dev,
2297                                                         SZ_1M,
2298                                                         &nvavp->os_info.phys,
2299                                                         GFP_KERNEL);
2300
2301                         if (!nvavp->os_info.data ||
2302                             nvavp->os_info.phys != 0x0ff00000) {
2303                                 dev_err(&ndev->dev, "cannot allocate IOVA memory\n");
2304                                 ret = -ENOMEM;
2305                         }
2306                 }
2307
2308                 dev_info(&ndev->dev,
2309                         "allocated IOVA at %lx for AVP os\n",
2310                         (unsigned long)nvavp->os_info.phys);
2311                 break;
2312         case NVAVP_USE_CARVEOUT:
2313                 if (!nvavp_reserve_os_mem(nvavp, 0x8e000000))
2314                         nvavp->os_info.phys = 0x8e000000;
2315                 else if (!nvavp_reserve_os_mem(nvavp, 0xf7e00000))
2316                         nvavp->os_info.phys = 0xf7e00000;
2317                 else if (!nvavp_reserve_os_mem(nvavp, 0x9e000000))
2318                         nvavp->os_info.phys = 0x9e000000;
2319                 else if (!nvavp_reserve_os_mem(nvavp, 0xbe000000))
2320                         nvavp->os_info.phys = 0xbe000000;
2321                 else {
2322                         dev_err(&nvavp->nvhost_dev->dev,
2323                                 "cannot find nvmem= carveout to load AVP os\n");
2324                         dev_err(&nvavp->nvhost_dev->dev,
2325                                 "check kernel command line "
2326                                 "to see if nvmem= is defined\n");
2327                         BUG();
2328
2329                 }
2330
2331                 dev_info(&ndev->dev,
2332                         "allocated carveout memory at %lx for AVP os\n",
2333                         (unsigned long)nvavp->os_info.phys);
2334                 break;
2335         default:
2336                 dev_err(&ndev->dev, "invalid/non-supported heap for AVP os\n");
2337                 ret = -EINVAL;
2338                 goto err_get_syncpt;
2339         }
2340
2341         nvavp->mbox_from_avp_pend_irq = irq;
2342         mutex_init(&nvavp->open_lock);
2343
2344         for (channel_id = 0; channel_id < NVAVP_NUM_CHANNELS; channel_id++)
2345                 mutex_init(&nvavp->channel_info[channel_id].pushbuffer_lock);
2346
2347         /* TODO DO NOT USE NVAVP DEVICE */
2348         nvavp->cop_clk = clk_get(&ndev->dev, "cop");
2349         if (IS_ERR(nvavp->cop_clk)) {
2350                 dev_err(&ndev->dev, "cannot get cop clock\n");
2351                 ret = -ENOENT;
2352                 goto err_get_cop_clk;
2353         }
2354
2355         nvavp->vde_clk = clk_get(&ndev->dev, "vde");
2356         if (IS_ERR(nvavp->vde_clk)) {
2357                 dev_err(&ndev->dev, "cannot get vde clock\n");
2358                 ret = -ENOENT;
2359                 goto err_get_vde_clk;
2360         }
2361
2362         nvavp->bsev_clk = clk_get(&ndev->dev, "bsev");
2363         if (IS_ERR(nvavp->bsev_clk)) {
2364                 dev_err(&ndev->dev, "cannot get bsev clock\n");
2365                 ret = -ENOENT;
2366                 goto err_get_bsev_clk;
2367         }
2368
2369         nvavp->sclk = clk_get(&ndev->dev, "sclk");
2370         if (IS_ERR(nvavp->sclk)) {
2371                 dev_err(&ndev->dev, "cannot get avp.sclk clock\n");
2372                 ret = -ENOENT;
2373                 goto err_get_sclk;
2374         }
2375
2376         nvavp->emc_clk = clk_get(&ndev->dev, "emc");
2377         if (IS_ERR(nvavp->emc_clk)) {
2378                 dev_err(&ndev->dev, "cannot get emc clock\n");
2379                 ret = -ENOENT;
2380                 goto err_get_emc_clk;
2381         }
2382
2383 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2384         nvavp->bsea_clk = clk_get(&ndev->dev, "bsea");
2385         if (IS_ERR(nvavp->bsea_clk)) {
2386                 dev_err(&ndev->dev, "cannot get bsea clock\n");
2387                 ret = -ENOENT;
2388                 goto err_get_bsea_clk;
2389         }
2390
2391         nvavp->vcp_clk = clk_get(&ndev->dev, "vcp");
2392         if (IS_ERR(nvavp->vcp_clk)) {
2393                 dev_err(&ndev->dev, "cannot get vcp clock\n");
2394                 ret = -ENOENT;
2395                 goto err_get_vcp_clk;
2396         }
2397 #endif
2398
2399         nvavp->clk_enabled = 0;
2400         nvavp_halt_avp(nvavp);
2401
2402         INIT_WORK(&nvavp->clock_disable_work, clock_disable_handler);
2403
2404         nvavp->video_misc_dev.minor = MISC_DYNAMIC_MINOR;
2405         nvavp->video_misc_dev.name = "tegra_avpchannel";
2406         nvavp->video_misc_dev.fops = &tegra_video_nvavp_fops;
2407         nvavp->video_misc_dev.mode = S_IRWXUGO;
2408         nvavp->video_misc_dev.parent = &ndev->dev;
2409
2410         ret = misc_register(&nvavp->video_misc_dev);
2411         if (ret) {
2412                 dev_err(&ndev->dev, "unable to register misc device!\n");
2413                 goto err_misc_reg;
2414         }
2415
2416 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2417         INIT_WORK(&nvavp->app_notify_work, app_notify_handler);
2418         nvavp->audio_misc_dev.minor = MISC_DYNAMIC_MINOR;
2419         nvavp->audio_misc_dev.name = "tegra_audio_avpchannel";
2420         nvavp->audio_misc_dev.fops = &tegra_audio_nvavp_fops;
2421         nvavp->audio_misc_dev.mode = S_IRWXUGO;
2422         nvavp->audio_misc_dev.parent = &ndev->dev;
2423
2424         ret = misc_register(&nvavp->audio_misc_dev);
2425         if (ret) {
2426         dev_err(&ndev->dev, "unable to register misc device!\n");
2427                 goto err_audio_misc_reg;
2428         }
2429 #endif
2430
2431         ret = request_irq(irq, nvavp_mbox_pending_isr, 0,
2432                           TEGRA_NVAVP_NAME, nvavp);
2433         if (ret) {
2434                 dev_err(&ndev->dev, "cannot register irq handler\n");
2435                 goto err_req_irq_pend;
2436         }
2437         disable_irq(nvavp->mbox_from_avp_pend_irq);
2438
2439         nvavp->nvhost_dev = ndev;
2440         platform_set_drvdata(ndev, nvavp);
2441
2442         tegra_pd_add_device(&ndev->dev);
2443         pm_runtime_use_autosuspend(&ndev->dev);
2444         pm_runtime_set_autosuspend_delay(&ndev->dev, 2000);
2445         pm_runtime_enable(&ndev->dev);
2446
2447         ret = device_create_file(&ndev->dev, &dev_attr_boost_sclk);
2448         if (ret) {
2449                 dev_err(&ndev->dev,
2450                         "%s: device_create_file failed\n", __func__);
2451                 goto err_req_irq_pend;
2452         }
2453         nvavp_info_ctx = nvavp;
2454
2455         /* Add PM QoS request but leave it as default value */
2456         pm_qos_add_request(&nvavp->min_cpu_freq_req,
2457                                 PM_QOS_CPU_FREQ_MIN,
2458                                 PM_QOS_DEFAULT_VALUE);
2459         pm_qos_add_request(&nvavp->min_online_cpus_req,
2460                                 PM_QOS_MIN_ONLINE_CPUS,
2461                                 PM_QOS_DEFAULT_VALUE);
2462
2463         return 0;
2464
2465 err_req_irq_pend:
2466 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2467         misc_deregister(&nvavp->audio_misc_dev);
2468 err_audio_misc_reg:
2469 #endif
2470         misc_deregister(&nvavp->video_misc_dev);
2471 err_misc_reg:
2472 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2473         clk_put(nvavp->vcp_clk);
2474 err_get_vcp_clk:
2475         clk_put(nvavp->bsea_clk);
2476 err_get_bsea_clk:
2477 #endif
2478         clk_put(nvavp->emc_clk);
2479 err_get_emc_clk:
2480         clk_put(nvavp->sclk);
2481 err_get_sclk:
2482         clk_put(nvavp->bsev_clk);
2483 err_get_bsev_clk:
2484         clk_put(nvavp->vde_clk);
2485 err_get_vde_clk:
2486         clk_put(nvavp->cop_clk);
2487 err_get_cop_clk:
2488 err_get_syncpt:
2489         kfree(nvavp);
2490         return ret;
2491 }
2492
2493 static int tegra_nvavp_remove(struct platform_device *ndev)
2494 {
2495         struct nvavp_info *nvavp = platform_get_drvdata(ndev);
2496
2497         if (!nvavp)
2498                 return 0;
2499
2500         mutex_lock(&nvavp->open_lock);
2501         if (nvavp->refcount) {
2502                 mutex_unlock(&nvavp->open_lock);
2503                 return -EBUSY;
2504         }
2505         mutex_unlock(&nvavp->open_lock);
2506
2507         nvavp_unload_ucode(nvavp);
2508         nvavp_unload_os(nvavp);
2509
2510         device_remove_file(&ndev->dev, &dev_attr_boost_sclk);
2511
2512         misc_deregister(&nvavp->video_misc_dev);
2513
2514 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2515         misc_deregister(&nvavp->audio_misc_dev);
2516         clk_put(nvavp->vcp_clk);
2517         clk_put(nvavp->bsea_clk);
2518 #endif
2519         clk_put(nvavp->bsev_clk);
2520         clk_put(nvavp->vde_clk);
2521         clk_put(nvavp->cop_clk);
2522
2523         clk_put(nvavp->emc_clk);
2524         clk_put(nvavp->sclk);
2525
2526         if (!IS_ERR_OR_NULL(&nvavp->min_cpu_freq_req)) {
2527                 pm_qos_update_request(&nvavp->min_cpu_freq_req,
2528                                 PM_QOS_CPU_FREQ_MIN_DEFAULT_VALUE);
2529                 pm_qos_remove_request(&nvavp->min_cpu_freq_req);
2530         }
2531         if (!IS_ERR_OR_NULL(&nvavp->min_online_cpus_req)) {
2532                 pm_qos_update_request(&nvavp->min_online_cpus_req,
2533                                 PM_QOS_CPU_FREQ_MIN_DEFAULT_VALUE);
2534                 pm_qos_remove_request(&nvavp->min_online_cpus_req);
2535         }
2536
2537         kfree(nvavp);
2538         return 0;
2539 }
2540
2541 #ifdef CONFIG_PM
2542 static int tegra_nvavp_runtime_suspend(struct device *dev)
2543 {
2544         struct platform_device *pdev = to_platform_device(dev);
2545         struct nvavp_info *nvavp = platform_get_drvdata(pdev);
2546         int ret = 0;
2547
2548         if (nvavp->refcount) {
2549                 if (!nvavp->clk_enabled) {
2550 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2551                         if (nvavp_check_idle(nvavp, NVAVP_AUDIO_CHANNEL))
2552                                 nvavp_uninit(nvavp);
2553                         else
2554                                 ret = -EBUSY;
2555 #else
2556                         nvavp_uninit(nvavp);
2557 #endif
2558                 }
2559                 else {
2560                         ret = -EBUSY;
2561                 }
2562         }
2563
2564         return ret;
2565 }
2566
2567 static int tegra_nvavp_runtime_resume(struct device *dev)
2568 {
2569         struct platform_device *pdev = to_platform_device(dev);
2570         struct nvavp_info *nvavp = platform_get_drvdata(pdev);
2571
2572         mutex_lock(&nvavp->open_lock);
2573
2574         if (nvavp->video_refcnt)
2575                 nvavp_init(nvavp, NVAVP_VIDEO_CHANNEL);
2576 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2577         if (nvavp->audio_refcnt)
2578                 nvavp_init(nvavp, NVAVP_AUDIO_CHANNEL);
2579 #endif
2580
2581         mutex_unlock(&nvavp->open_lock);
2582
2583         return 0;
2584 }
2585
2586 static int tegra_nvavp_resume(struct device *dev)
2587 {
2588         struct platform_device *pdev = to_platform_device(dev);
2589         struct nvavp_info *nvavp = platform_get_drvdata(pdev);
2590
2591         /* To balance the unpowergate in suspend routine */
2592         nvavp_powergate_vde(nvavp);
2593
2594         tegra_nvavp_runtime_resume(dev);
2595
2596         return 0;
2597 }
2598
2599 static int tegra_nvavp_suspend(struct device *dev)
2600 {
2601         struct platform_device *pdev = to_platform_device(dev);
2602         struct nvavp_info *nvavp = platform_get_drvdata(pdev);
2603         int ret = 0;
2604
2605         mutex_lock(&nvavp->open_lock);
2606
2607         ret = tegra_nvavp_runtime_suspend(dev);
2608         if (ret) {
2609                 mutex_unlock(&nvavp->open_lock);
2610                 return ret;
2611         }
2612
2613         /* WAR: Leave partition vde on before suspend so that access
2614          * to BSEV registers immediatly after LP0 exit won't fail.
2615          */
2616         nvavp_unpowergate_vde(nvavp);
2617
2618         mutex_unlock(&nvavp->open_lock);
2619
2620         return 0;
2621 }
2622
2623 static const struct dev_pm_ops nvavp_pm_ops = {
2624         .runtime_suspend = tegra_nvavp_runtime_suspend,
2625         .runtime_resume = tegra_nvavp_runtime_resume,
2626         .suspend = tegra_nvavp_suspend,
2627         .resume = tegra_nvavp_resume,
2628 };
2629
2630 #define NVAVP_PM_OPS    (&nvavp_pm_ops)
2631
2632 #else /* CONFIG_PM */
2633
2634 #define NVAVP_PM_OPS    NULL
2635
2636 #endif /* CONFIG_PM */
2637
2638 static struct platform_driver tegra_nvavp_driver = {
2639         .driver = {
2640                 .name   = TEGRA_NVAVP_NAME,
2641                 .owner  = THIS_MODULE,
2642                 .pm     = NVAVP_PM_OPS,
2643                 .of_match_table = of_match_ptr(tegra_nvavp_of_match),
2644         },
2645         .probe          = tegra_nvavp_probe,
2646         .remove         = tegra_nvavp_remove,
2647 };
2648
2649 static int __init tegra_nvavp_init(void)
2650 {
2651         return platform_driver_register(&tegra_nvavp_driver);
2652 }
2653
2654 static void __exit tegra_nvavp_exit(void)
2655 {
2656         platform_driver_unregister(&tegra_nvavp_driver);
2657 }
2658
2659 module_init(tegra_nvavp_init);
2660 module_exit(tegra_nvavp_exit);
2661
2662 MODULE_AUTHOR("NVIDIA");
2663 MODULE_DESCRIPTION("Channel based AVP driver for Tegra");
2664 MODULE_VERSION("1.0");
2665 MODULE_LICENSE("Dual BSD/GPL");