platform: tegra: nvavp: fix for pre-decrement of clk_enabled cntr
[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-2016, 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) {
571                 nvavp->clk_enabled--;
572         }
573         if ((nvavp->clk_enabled == 0) && !nvavp->stay_on) {
574                 clk_disable_unprepare(nvavp->bsev_clk);
575                 clk_disable_unprepare(nvavp->vde_clk);
576                 clk_set_rate(nvavp->emc_clk, 0);
577                 if (boost_sclk)
578                         clk_set_rate(nvavp->sclk, SCLK_BOOST_RATE);
579                 else
580                         clk_set_rate(nvavp->sclk, 0);
581                 nvavp_powergate_vde(nvavp);
582                 nvhost_module_idle_ext(nvavp->nvhost_dev);
583                 nvavp_runtime_put(nvavp);
584                 dev_dbg(&nvavp->nvhost_dev->dev, "%s: resetting emc_clk "
585                                 "and sclk\n", __func__);
586         }
587 }
588
589 static u32 nvavp_check_idle(struct nvavp_info *nvavp, int channel_id)
590 {
591         struct nvavp_channel *channel_info = nvavp_get_channel_info(nvavp, channel_id);
592         struct nv_e276_control *control = channel_info->os_control;
593
594         return (control->put == control->get) ? 1 : 0;
595 }
596
597 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
598 static void app_notify_handler(struct work_struct *work)
599 {
600         struct nvavp_info *nvavp;
601
602         nvavp = container_of(work, struct nvavp_info,
603                             app_notify_work);
604         if (nvavp->audio_notify)
605                 nvavp->audio_notify();
606         else
607                 kobject_uevent(&nvavp->nvhost_dev->dev.kobj, KOBJ_CHANGE);
608 }
609 #endif
610
611 static void clock_disable_handler(struct work_struct *work)
612 {
613         struct nvavp_info *nvavp;
614         struct nvavp_channel *channel_info;
615
616         nvavp = container_of(work, struct nvavp_info,
617                             clock_disable_work);
618
619         channel_info = nvavp_get_channel_info(nvavp, NVAVP_VIDEO_CHANNEL);
620         mutex_lock(&channel_info->pushbuffer_lock);
621         mutex_lock(&nvavp->open_lock);
622         if (nvavp_check_idle(nvavp, NVAVP_VIDEO_CHANNEL) && nvavp->pending) {
623                 nvavp->pending = false;
624                 nvavp_clks_disable(nvavp);
625         }
626         mutex_unlock(&nvavp->open_lock);
627         mutex_unlock(&channel_info->pushbuffer_lock);
628 }
629
630 static int nvavp_service(struct nvavp_info *nvavp)
631 {
632         struct nvavp_os_info *os = &nvavp->os_info;
633         u8 *debug_print;
634         u32 inbox;
635
636         inbox = readl(NVAVP_OS_INBOX);
637         if (!(inbox & NVAVP_INBOX_VALID))
638                 inbox = 0x00000000;
639
640         if ((inbox & NVE276_OS_INTERRUPT_VIDEO_IDLE) && (!nvavp->stay_on))
641                 schedule_work(&nvavp->clock_disable_work);
642
643         if (inbox & NVE276_OS_INTERRUPT_SYNCPT_INCR_TRAP) {
644                 /* sync pnt incr */
645                 if (nvavp->syncpt_id == NVE276_OS_SYNCPT_INCR_TRAP_GET_SYNCPT(inbox))
646                         nvhost_syncpt_cpu_incr_ext(
647                                 nvavp->nvhost_dev, nvavp->syncpt_id);
648         }
649
650 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
651         if (inbox & NVE276_OS_INTERRUPT_AUDIO_IDLE) {
652                 if (audio_enabled) {
653                         audio_enabled = false;
654                         nvavp_runtime_put(nvavp);
655                 }
656                 pr_debug("nvavp_service NVE276_OS_INTERRUPT_AUDIO_IDLE\n");
657         }
658 #endif
659         if (inbox & NVE276_OS_INTERRUPT_DEBUG_STRING) {
660                 /* Should only occur with debug AVP OS builds */
661                 debug_print = os->data;
662                 debug_print += os->debug_offset;
663                 dev_info(&nvavp->nvhost_dev->dev, "%s\n", debug_print);
664         }
665         if (inbox & (NVE276_OS_INTERRUPT_SEMAPHORE_AWAKEN |
666                      NVE276_OS_INTERRUPT_EXECUTE_AWAKEN)) {
667                 dev_info(&nvavp->nvhost_dev->dev,
668                         "AVP awaken event (0x%x)\n", inbox);
669         }
670         if (inbox & NVE276_OS_INTERRUPT_AVP_FATAL_ERROR) {
671                 dev_err(&nvavp->nvhost_dev->dev,
672                         "fatal AVP error (0x%08X)\n", inbox);
673         }
674         if (inbox & NVE276_OS_INTERRUPT_AVP_BREAKPOINT)
675                 dev_err(&nvavp->nvhost_dev->dev, "AVP breakpoint hit\n");
676         if (inbox & NVE276_OS_INTERRUPT_TIMEOUT)
677                 dev_err(&nvavp->nvhost_dev->dev, "AVP timeout\n");
678         writel(inbox & NVAVP_INBOX_VALID, NVAVP_OS_INBOX);
679
680 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
681         if (inbox & NVE276_OS_INTERRUPT_APP_NOTIFY) {
682                 pr_debug("nvavp_service NVE276_OS_INTERRUPT_APP_NOTIFY\n");
683                 schedule_work(&nvavp->app_notify_work);
684         }
685 #endif
686
687         return 0;
688 }
689
690 static irqreturn_t nvavp_mbox_pending_isr(int irq, void *data)
691 {
692         struct nvavp_info *nvavp = data;
693
694         nvavp_service(nvavp);
695
696         return IRQ_HANDLED;
697 }
698
699 static void nvavp_halt_avp(struct nvavp_info *nvavp)
700 {
701         /* ensure the AVP is halted */
702         writel(FLOW_MODE_STOP, FLOW_CTRL_HALT_COP_EVENTS);
703         tegra_periph_reset_assert(nvavp->cop_clk);
704
705         writel(0, NVAVP_OS_OUTBOX);
706         writel(0, NVAVP_OS_INBOX);
707 }
708
709 static int nvavp_reset_avp(struct nvavp_info *nvavp, unsigned long reset_addr)
710 {
711 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU)
712         unsigned long stub_code_phys = virt_to_phys(_tegra_avp_boot_stub);
713         dma_addr_t stub_data_phys;
714 #endif
715
716 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
717         if (!(nvavp_check_idle(nvavp, NVAVP_AUDIO_CHANNEL)))
718                 return 0;
719 #endif
720
721 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU)
722         _tegra_avp_boot_stub_data.map_phys_addr = avp->kernel_phys;
723         _tegra_avp_boot_stub_data.jump_addr = reset_addr;
724         wmb();
725         stub_data_phys = dma_map_single(NULL, &_tegra_avp_boot_stub_data,
726                                         sizeof(_tegra_avp_boot_stub_data),
727                                         DMA_TO_DEVICE);
728         rmb();
729         reset_addr = (unsigned long)stub_data_phys;
730 #endif
731         writel(FLOW_MODE_STOP, FLOW_CTRL_HALT_COP_EVENTS);
732
733         writel(reset_addr, TEGRA_NVAVP_RESET_VECTOR_ADDR);
734
735         clk_prepare_enable(nvavp->sclk);
736         clk_prepare_enable(nvavp->emc_clk);
737
738         /* If sclk_rate and emc_clk is not set by user space,
739          * max clock in dvfs table will be used to get best performance.
740          */
741         nvavp->sclk_rate = ULONG_MAX;
742         nvavp->emc_clk_rate = ULONG_MAX;
743
744         tegra_periph_reset_assert(nvavp->cop_clk);
745         udelay(2);
746         tegra_periph_reset_deassert(nvavp->cop_clk);
747
748         writel(FLOW_MODE_NONE, FLOW_CTRL_HALT_COP_EVENTS);
749
750 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU)
751         dma_unmap_single(NULL, stub_data_phys,
752                          sizeof(_tegra_avp_boot_stub_data),
753                          DMA_TO_DEVICE);
754 #endif
755         return 0;
756 }
757
758 static void nvavp_halt_vde(struct nvavp_info *nvavp)
759 {
760         if (nvavp->clk_enabled && !nvavp->pending)
761                 BUG();
762
763         if (nvavp->pending) {
764                 nvavp_clks_disable(nvavp);
765                 nvavp->pending = false;
766         }
767
768         tegra_periph_reset_assert(nvavp->bsev_clk);
769         tegra_periph_reset_assert(nvavp->vde_clk);
770 }
771
772 static int nvavp_reset_vde(struct nvavp_info *nvavp)
773 {
774         if (nvavp->clk_enabled)
775                 BUG();
776
777         nvavp_clks_enable(nvavp);
778
779         tegra_periph_reset_assert(nvavp->bsev_clk);
780         udelay(2);
781         tegra_periph_reset_deassert(nvavp->bsev_clk);
782
783         tegra_periph_reset_assert(nvavp->vde_clk);
784         udelay(2);
785         tegra_periph_reset_deassert(nvavp->vde_clk);
786
787         /*
788          * VDE clock is set to max freq by default.
789          * VDE clock can be set to different freq if needed
790          * through ioctl.
791          */
792         clk_set_rate(nvavp->vde_clk, ULONG_MAX);
793
794         nvavp_clks_disable(nvavp);
795
796         return 0;
797 }
798
799 static int nvavp_pushbuffer_alloc(struct nvavp_info *nvavp, int channel_id)
800 {
801         int ret = 0;
802
803         struct nvavp_channel *channel_info = nvavp_get_channel_info(
804                                                         nvavp, channel_id);
805
806         channel_info->pushbuf_data = dma_zalloc_coherent(&nvavp->nvhost_dev->dev,
807                                                            NVAVP_PUSHBUFFER_SIZE,
808                                                            &channel_info->pushbuf_phys,
809                                                            GFP_KERNEL);
810
811         if (!channel_info->pushbuf_data) {
812                 dev_err(&nvavp->nvhost_dev->dev,
813                         "cannot alloc pushbuffer memory\n");
814                 ret = -ENOMEM;
815         }
816
817         return ret;
818 }
819
820 static void nvavp_pushbuffer_free(struct nvavp_info *nvavp)
821 {
822         int channel_id;
823
824         for (channel_id = 0; channel_id < NVAVP_NUM_CHANNELS; channel_id++) {
825                 if (nvavp->channel_info[channel_id].pushbuf_data) {
826                         dma_free_coherent(&nvavp->nvhost_dev->dev,
827                                             NVAVP_PUSHBUFFER_SIZE,
828                                             nvavp->channel_info[channel_id].pushbuf_data,
829                                             nvavp->channel_info[channel_id].pushbuf_phys);
830                 }
831         }
832 }
833
834
835 static int nvavp_pushbuffer_init(struct nvavp_info *nvavp)
836 {
837         int ret, channel_id;
838         u32 val;
839
840         for (channel_id = 0; channel_id < NVAVP_NUM_CHANNELS; channel_id++) {
841                 ret = nvavp_pushbuffer_alloc(nvavp, channel_id);
842                 if (ret) {
843                         dev_err(&nvavp->nvhost_dev->dev,
844                                 "unable to alloc pushbuffer\n");
845                         return ret;
846                 }
847                 nvavp_set_channel_control_area(nvavp, channel_id);
848                 if (IS_VIDEO_CHANNEL_ID(channel_id)) {
849                         nvavp->syncpt_id = NVSYNCPT_AVP_0;
850                         if (!nvhost_syncpt_read_ext_check(nvavp->nvhost_dev,
851                                         nvavp->syncpt_id, &val))
852                                 nvavp->syncpt_value = val;
853                 }
854
855         }
856         return 0;
857 }
858
859 static void nvavp_pushbuffer_deinit(struct nvavp_info *nvavp)
860 {
861         nvavp_pushbuffer_free(nvavp);
862 }
863
864 static int nvavp_pushbuffer_update(struct nvavp_info *nvavp, u32 phys_addr,
865                         u32 gather_count, struct nvavp_syncpt *syncpt,
866                         u32 ext_ucode_flag, int channel_id)
867 {
868         struct nvavp_channel  *channel_info;
869         struct nv_e276_control *control;
870         u32 gather_cmd, setucode_cmd, sync = 0;
871         u32 wordcount = 0;
872         u32 index, value = -1;
873         int ret = 0;
874
875         mutex_lock(&nvavp->open_lock);
876         nvavp_runtime_get(nvavp);
877         mutex_unlock(&nvavp->open_lock);
878         channel_info = nvavp_get_channel_info(nvavp, channel_id);
879
880         control = channel_info->os_control;
881         pr_debug("nvavp_pushbuffer_update for channel_id (%d):\
882                 control->put (0x%x) control->get (0x%x)\n",
883                 channel_id, (u32) &control->put, (u32) &control->get);
884
885         mutex_lock(&channel_info->pushbuffer_lock);
886
887         /* check for pushbuffer wrapping */
888         if (channel_info->pushbuf_index >= channel_info->pushbuf_fence)
889                 channel_info->pushbuf_index = 0;
890
891         if (!ext_ucode_flag) {
892                 setucode_cmd =
893                         NVE26E_CH_OPCODE_INCR(NVE276_SET_MICROCODE_A, 3);
894
895                 index = wordcount + channel_info->pushbuf_index;
896                 writel(setucode_cmd, (channel_info->pushbuf_data + index));
897                 wordcount += sizeof(u32);
898
899                 index = wordcount + channel_info->pushbuf_index;
900                 writel(0, (channel_info->pushbuf_data + index));
901                 wordcount += sizeof(u32);
902
903                 index = wordcount + channel_info->pushbuf_index;
904                 writel(nvavp->ucode_info.phys,
905                         (channel_info->pushbuf_data + index));
906                 wordcount += sizeof(u32);
907
908                 index = wordcount + channel_info->pushbuf_index;
909                 writel(nvavp->ucode_info.size,
910                         (channel_info->pushbuf_data + index));
911                 wordcount += sizeof(u32);
912         }
913
914         gather_cmd = NVE26E_CH_OPCODE_GATHER(0, 0, 0, gather_count);
915
916         if (syncpt) {
917                 value = ++nvavp->syncpt_value;
918                 /* XXX: NvSchedValueWrappingComparison */
919                 sync = NVE26E_CH_OPCODE_IMM(NVE26E_HOST1X_INCR_SYNCPT,
920                         (NVE26E_HOST1X_INCR_SYNCPT_COND_OP_DONE << 8) |
921                         (nvavp->syncpt_id & 0xFF));
922         }
923
924         /* write commands out */
925         index = wordcount + channel_info->pushbuf_index;
926         writel(gather_cmd, (channel_info->pushbuf_data + index));
927         wordcount += sizeof(u32);
928
929         index = wordcount + channel_info->pushbuf_index;
930         writel(phys_addr, (channel_info->pushbuf_data + index));
931         wordcount += sizeof(u32);
932
933         if (syncpt) {
934                 index = wordcount + channel_info->pushbuf_index;
935                 writel(sync, (channel_info->pushbuf_data + index));
936                 wordcount += sizeof(u32);
937         }
938
939         /* enable clocks to VDE/BSEV */
940         mutex_lock(&nvavp->open_lock);
941         if (!nvavp->pending && IS_VIDEO_CHANNEL_ID(channel_id)) {
942                 nvavp_clks_enable(nvavp);
943                 nvavp->pending = true;
944         }
945         mutex_unlock(&nvavp->open_lock);
946
947         /* update put pointer */
948         channel_info->pushbuf_index = (channel_info->pushbuf_index + wordcount)&
949                                         (NVAVP_PUSHBUFFER_SIZE - 1);
950
951         writel(channel_info->pushbuf_index, &control->put);
952         wmb();
953
954         /* wake up avp */
955
956         if (IS_VIDEO_CHANNEL_ID(channel_id)) {
957                 pr_debug("Wake up Video Channel\n");
958                 ret = nvavp_outbox_write(0xA0000001);
959                 if (ret < 0)
960                         goto err_exit;
961         }
962         else {
963 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
964                 if (IS_AUDIO_CHANNEL_ID(channel_id)) {
965                         pr_debug("Wake up Audio Channel\n");
966                         if (!audio_enabled) {
967                                 mutex_lock(&nvavp->open_lock);
968                                 nvavp_runtime_get(nvavp);
969                                 mutex_unlock(&nvavp->open_lock);
970                                 audio_enabled = true;
971                         }
972                         ret = nvavp_outbox_write(0xA0000002);
973                         if (ret < 0)
974                                 goto err_exit;
975                 }
976 #endif
977         }
978         /* Fill out fence struct */
979         if (syncpt) {
980                 syncpt->id = nvavp->syncpt_id;
981                 syncpt->value = value;
982         }
983
984 err_exit:
985         mutex_unlock(&channel_info->pushbuffer_lock);
986         nvavp_runtime_put(nvavp);
987
988         return 0;
989 }
990
991 static void nvavp_unload_ucode(struct nvavp_info *nvavp)
992 {
993         dma_free_coherent(&nvavp->nvhost_dev->dev,  nvavp->ucode_info.size,
994                            nvavp->ucode_info.data, nvavp->ucode_info.phys);
995         kfree(nvavp->ucode_info.ucode_bin);
996 }
997
998 static int nvavp_load_ucode(struct nvavp_info *nvavp)
999 {
1000         struct nvavp_ucode_info *ucode_info = &nvavp->ucode_info;
1001         const struct firmware *nvavp_ucode_fw;
1002         char fw_ucode_file[32];
1003         void *ptr;
1004         int ret = 0;
1005
1006         if (!ucode_info->ucode_bin) {
1007                 sprintf(fw_ucode_file, "nvavp_vid_ucode.bin");
1008
1009                 ret = request_firmware(&nvavp_ucode_fw, fw_ucode_file,
1010                                         nvavp->video_misc_dev.this_device);
1011                 if (ret) {
1012                         /* Try alternative version */
1013                         sprintf(fw_ucode_file, "nvavp_vid_ucode_alt.bin");
1014
1015                         ret = request_firmware(&nvavp_ucode_fw,
1016                                                 fw_ucode_file,
1017                                                 nvavp->video_misc_dev.this_device);
1018
1019                         if (ret) {
1020                                 dev_err(&nvavp->nvhost_dev->dev,
1021                                         "cannot read ucode firmware '%s'\n",
1022                                         fw_ucode_file);
1023                                 goto err_req_ucode;
1024                         }
1025                 }
1026
1027                 dev_dbg(&nvavp->nvhost_dev->dev,
1028                         "read ucode firmware from '%s' (%d bytes)\n",
1029                         fw_ucode_file, nvavp_ucode_fw->size);
1030
1031                 ptr = (void *)nvavp_ucode_fw->data;
1032
1033                 if (strncmp((const char *)ptr, "NVAVPAPP", 8)) {
1034                         dev_dbg(&nvavp->nvhost_dev->dev,
1035                                 "ucode hdr string mismatch\n");
1036                         ret = -EINVAL;
1037                         goto err_req_ucode;
1038                 }
1039                 ptr += 8;
1040                 ucode_info->size = nvavp_ucode_fw->size - 8;
1041
1042                 ucode_info->ucode_bin = kzalloc(ucode_info->size,
1043                                                 GFP_KERNEL);
1044                 if (!ucode_info->ucode_bin) {
1045                         dev_err(&nvavp->nvhost_dev->dev,
1046                                 "cannot allocate ucode bin\n");
1047                         ret = -ENOMEM;
1048                         goto err_ubin_alloc;
1049                 }
1050
1051                 ucode_info->data = dma_alloc_coherent(&nvavp->nvhost_dev->dev,
1052                                                 ucode_info->size,
1053                                                 &ucode_info->phys,
1054                                                 GFP_KERNEL);
1055                 if (!ucode_info->data) {
1056                         dev_err(&nvavp->nvhost_dev->dev,
1057                                 "cannot alloc memory for ucode\n");
1058                         ret = -ENOMEM;
1059                         goto err_ucode_alloc;
1060                 }
1061                 memcpy(ucode_info->ucode_bin, ptr, ucode_info->size);
1062                 release_firmware(nvavp_ucode_fw);
1063         }
1064
1065         memcpy(ucode_info->data, ucode_info->ucode_bin, ucode_info->size);
1066         return 0;
1067
1068 err_ucode_alloc:
1069         kfree(nvavp->ucode_info.ucode_bin);
1070 err_ubin_alloc:
1071         release_firmware(nvavp_ucode_fw);
1072 err_req_ucode:
1073         return ret;
1074 }
1075
1076 static void nvavp_unload_os(struct nvavp_info *nvavp)
1077 {
1078         dma_free_coherent(&nvavp->nvhost_dev->dev, SZ_1M,
1079                 nvavp->os_info.data, nvavp->os_info.phys);
1080         kfree(nvavp->os_info.os_bin);
1081 }
1082
1083 static int nvavp_load_os(struct nvavp_info *nvavp, char *fw_os_file)
1084 {
1085         struct nvavp_os_info *os_info = &nvavp->os_info;
1086         const struct firmware *nvavp_os_fw;
1087         void *ptr;
1088         u32 size;
1089         int ret = 0;
1090
1091         if (!os_info->os_bin) {
1092                 ret = request_firmware(&nvavp_os_fw, fw_os_file,
1093                                         nvavp->video_misc_dev.this_device);
1094                 if (ret) {
1095                         dev_err(&nvavp->nvhost_dev->dev,
1096                                 "cannot read os firmware '%s'\n", fw_os_file);
1097                         goto err_req_fw;
1098                 }
1099
1100                 dev_dbg(&nvavp->nvhost_dev->dev,
1101                         "read firmware from '%s' (%d bytes)\n",
1102                         fw_os_file, nvavp_os_fw->size);
1103
1104                 ptr = (void *)nvavp_os_fw->data;
1105
1106                 if (strncmp((const char *)ptr, "NVAVP-OS", 8)) {
1107                         dev_dbg(&nvavp->nvhost_dev->dev,
1108                                 "os hdr string mismatch\n");
1109                         ret = -EINVAL;
1110                         goto err_os_bin;
1111                 }
1112
1113                 ptr += 8;
1114                 os_info->entry_offset = *((u32 *)ptr);
1115                 ptr += sizeof(u32);
1116                 os_info->control_offset = *((u32 *)ptr);
1117                 ptr += sizeof(u32);
1118                 os_info->debug_offset = *((u32 *)ptr);
1119                 ptr += sizeof(u32);
1120
1121                 size = *((u32 *)ptr);    ptr += sizeof(u32);
1122
1123                 os_info->size = size;
1124                 os_info->os_bin = kzalloc(os_info->size,
1125                                                 GFP_KERNEL);
1126                 if (!os_info->os_bin) {
1127                         dev_err(&nvavp->nvhost_dev->dev,
1128                                 "cannot allocate os bin\n");
1129                         ret = -ENOMEM;
1130                         goto err_os_bin;
1131                 }
1132
1133                 memcpy(os_info->os_bin, ptr, os_info->size);
1134                 memset(os_info->data + os_info->size, 0, SZ_1M - os_info->size);
1135
1136                 dev_dbg(&nvavp->nvhost_dev->dev,
1137                         "entry=%08x control=%08x debug=%08x size=%d\n",
1138                         os_info->entry_offset, os_info->control_offset,
1139                         os_info->debug_offset, os_info->size);
1140                 release_firmware(nvavp_os_fw);
1141         }
1142
1143         memcpy(os_info->data, os_info->os_bin, os_info->size);
1144         os_info->reset_addr = os_info->phys + os_info->entry_offset;
1145
1146         dev_dbg(&nvavp->nvhost_dev->dev,
1147                 "AVP os at vaddr=%p paddr=%llx reset_addr=%llx\n",
1148                 os_info->data, (u64)(os_info->phys), (u64)os_info->reset_addr);
1149         return 0;
1150
1151 err_os_bin:
1152         release_firmware(nvavp_os_fw);
1153 err_req_fw:
1154         return ret;
1155 }
1156
1157
1158 static int nvavp_os_init(struct nvavp_info *nvavp)
1159 {
1160         char fw_os_file[32];
1161         int ret = 0;
1162         int video_initialized, audio_initialized = 0;
1163
1164         video_initialized = nvavp_get_video_init_status(nvavp);
1165
1166 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1167         audio_initialized = nvavp_get_audio_init_status(nvavp);
1168 #endif
1169         pr_debug("video_initialized(%d) audio_initialized(%d)\n",
1170                 video_initialized, audio_initialized);
1171
1172         /* Video and Audio both are initialized */
1173         if (video_initialized || audio_initialized)
1174                 return ret;
1175
1176         /* Video or Audio both are uninitialized */
1177         pr_debug("video_initialized == audio_initialized (%d)\n",
1178                 nvavp->video_initialized);
1179 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU) /* Tegra2 with AVP MMU */
1180         /* paddr is phys address */
1181         /* vaddr is AVP_KERNEL_VIRT_BASE */
1182         dev_dbg(&nvavp->nvhost_dev->dev,
1183                 "using AVP MMU to relocate AVP os\n");
1184         sprintf(fw_os_file, "nvavp_os.bin");
1185         nvavp->os_info.reset_addr = AVP_KERNEL_VIRT_BASE;
1186 #elif defined(CONFIG_TEGRA_AVP_KERNEL_ON_SMMU) /* Tegra3 with SMMU */
1187         /* paddr is any address behind SMMU */
1188         /* vaddr is TEGRA_SMMU_BASE */
1189         dev_dbg(&nvavp->nvhost_dev->dev,
1190                 "using SMMU at %lx to load AVP kernel\n",
1191                 (unsigned long)nvavp->os_info.phys);
1192         BUG_ON(nvavp->os_info.phys != 0xeff00000
1193                 && nvavp->os_info.phys != 0x0ff00000
1194                 && nvavp->os_info.phys != 0x8ff00000);
1195         sprintf(fw_os_file, "nvavp_os_%08lx.bin",
1196                 (unsigned long)nvavp->os_info.phys);
1197         nvavp->os_info.reset_addr = nvavp->os_info.phys;
1198 #else /* nvmem= carveout */
1199         dev_dbg(&nvavp->nvhost_dev->dev,
1200                 "using nvmem= carveout at %llx to load AVP os\n",
1201                 (u64)nvavp->os_info.phys);
1202         sprintf(fw_os_file, "nvavp_os_%08llx.bin", (u64)nvavp->os_info.phys);
1203         nvavp->os_info.reset_addr = nvavp->os_info.phys;
1204         nvavp->os_info.data = ioremap(nvavp->os_info.phys, SZ_1M);
1205 #endif
1206         ret = nvavp_load_os(nvavp, fw_os_file);
1207         if (ret) {
1208                 dev_err(&nvavp->nvhost_dev->dev,
1209                         "unable to load os firmware '%s'\n", fw_os_file);
1210                 goto err_exit;
1211         }
1212
1213         ret = nvavp_pushbuffer_init(nvavp);
1214         if (ret) {
1215                 dev_err(&nvavp->nvhost_dev->dev,
1216                         "unable to init pushbuffer\n");
1217                 goto err_exit;
1218         }
1219         tegra_init_legacy_irq_cop();
1220         enable_irq(nvavp->mbox_from_avp_pend_irq);
1221 err_exit:
1222         return ret;
1223 }
1224
1225 static int nvavp_init(struct nvavp_info *nvavp, int channel_id)
1226 {
1227         int ret = 0;
1228         int video_initialized = 0, audio_initialized = 0;
1229
1230         nvavp->init_task = current;
1231
1232         ret = nvavp_os_init(nvavp);
1233         if (ret) {
1234                 dev_err(&nvavp->nvhost_dev->dev,
1235                         "unable to load os firmware and allocate buffers\n");
1236         }
1237
1238         video_initialized = nvavp_get_video_init_status(nvavp);
1239 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1240         audio_initialized = nvavp_get_audio_init_status(nvavp);
1241 #endif
1242
1243         if (IS_VIDEO_CHANNEL_ID(channel_id) && (!video_initialized)) {
1244                 pr_debug("nvavp_init : channel_ID (%d)\n", channel_id);
1245                 ret = nvavp_load_ucode(nvavp);
1246                 if (ret) {
1247                         dev_err(&nvavp->nvhost_dev->dev,
1248                                 "unable to load ucode\n");
1249                         goto err_exit;
1250                 }
1251
1252                 nvavp_reset_vde(nvavp);
1253                 nvavp_reset_avp(nvavp, nvavp->os_info.reset_addr);
1254
1255                 nvavp_set_video_init_status(nvavp, 1);
1256         }
1257 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1258         if (IS_AUDIO_CHANNEL_ID(channel_id) && (!audio_initialized)) {
1259                 pr_debug("nvavp_init : channel_ID (%d)\n", channel_id);
1260                 nvavp_reset_avp(nvavp, nvavp->os_info.reset_addr);
1261                 nvavp_set_audio_init_status(nvavp, 1);
1262         }
1263 #endif
1264
1265 err_exit:
1266         nvavp->init_task = NULL;
1267         return ret;
1268 }
1269
1270 #define TIMER_EN        (1 << 31)
1271 #define TIMER_PERIODIC  (1 << 30)
1272 #define TIMER_PCR       0x4
1273 #define TIMER_PCR_INTR  (1 << 30)
1274
1275 static void nvavp_uninit(struct nvavp_info *nvavp)
1276 {
1277         int video_initialized, audio_initialized = 0;
1278         unsigned int reg;
1279
1280         video_initialized = nvavp_get_video_init_status(nvavp);
1281
1282 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1283         audio_initialized = nvavp_get_audio_init_status(nvavp);
1284 #endif
1285
1286         pr_debug("nvavp_uninit video_initialized(%d) audio_initialized(%d)\n",
1287                 video_initialized, audio_initialized);
1288
1289         /* Video and Audio both are uninitialized */
1290         if (!video_initialized && !audio_initialized)
1291                 return;
1292
1293         nvavp->init_task = current;
1294
1295         if (video_initialized) {
1296                 pr_debug("nvavp_uninit nvavp->video_initialized\n");
1297                 cancel_work_sync(&nvavp->clock_disable_work);
1298                 nvavp_halt_vde(nvavp);
1299                 nvavp_set_video_init_status(nvavp, 0);
1300                 video_initialized = 0;
1301         }
1302
1303 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1304         if (audio_initialized) {
1305                 cancel_work_sync(&nvavp->app_notify_work);
1306                 nvavp_set_audio_init_status(nvavp, 0);
1307                 audio_initialized = 0;
1308         }
1309 #endif
1310
1311         /* Video and Audio both becomes uninitialized */
1312         if (!video_initialized && !audio_initialized) {
1313                 pr_debug("nvavp_uninit both channels uninitialized\n");
1314
1315                 clk_disable_unprepare(nvavp->sclk);
1316                 clk_disable_unprepare(nvavp->emc_clk);
1317                 disable_irq(nvavp->mbox_from_avp_pend_irq);
1318                 nvavp_pushbuffer_deinit(nvavp);
1319                 nvavp_halt_avp(nvavp);
1320         }
1321
1322         /*
1323          * WAR: turn off TMR2 for fix LP1 wake up by TMR2.
1324          * turn off the periodic interrupt and the timer temporarily
1325          */
1326         reg = timer_readl(TIMER2_OFFSET + TIMER_PTV);
1327         reg &= ~(TIMER_EN | TIMER_PERIODIC);
1328         timer_writel(reg, TIMER2_OFFSET + TIMER_PTV);
1329
1330         /* write a 1 to the intr_clr field to clear the interrupt */
1331         reg = TIMER_PCR_INTR;
1332         timer_writel(reg, TIMER2_OFFSET + TIMER_PCR);
1333         nvavp->init_task = NULL;
1334 }
1335
1336 static int nvcpu_set_clock(struct nvavp_info *nvavp,
1337                                 struct nvavp_clock_args config,
1338                                 unsigned long arg)
1339 {
1340         dev_dbg(&nvavp->nvhost_dev->dev, "%s: update cpu freq to clk_rate=%u\n",
1341                         __func__, config.rate);
1342
1343         if (config.rate > 0)
1344                 pm_qos_update_request(&nvavp->min_cpu_freq_req, config.rate);
1345         else
1346                 pm_qos_update_request(&nvavp->min_cpu_freq_req,
1347                                         PM_QOS_CPU_FREQ_MIN_DEFAULT_VALUE);
1348
1349         return 0;
1350 }
1351
1352 static int nvavp_map_iova(struct file *filp, unsigned int cmd,
1353                                                         unsigned long arg)
1354 {
1355         struct nvavp_clientctx *clientctx = filp->private_data;
1356         struct nvavp_info *nvavp = clientctx->nvavp;
1357         struct nvavp_map_args map_arg;
1358         struct dma_buf *dmabuf;
1359         dma_addr_t addr = 0;
1360         int ret = 0;
1361
1362         if (copy_from_user(&map_arg, (void __user *)arg,
1363                 sizeof(struct nvavp_map_args))) {
1364                 dev_err(&nvavp->nvhost_dev->dev,
1365                         "failed to copy memory handle\n");
1366                 return -EFAULT;
1367         }
1368         if (!map_arg.fd) {
1369                 dev_err(&nvavp->nvhost_dev->dev,
1370                         "invalid memory handle %08x\n", map_arg.fd);
1371                 return -EINVAL;
1372         }
1373
1374         dmabuf = dma_buf_get(map_arg.fd);
1375         if (IS_ERR(dmabuf)) {
1376                 dev_err(&nvavp->nvhost_dev->dev,
1377                         "invalid buffer handle %08x\n", map_arg.fd);
1378                 return PTR_ERR(dmabuf);
1379         }
1380
1381         ret = nvavp_get_iova_addr(clientctx, dmabuf, &addr);
1382         if (ret)
1383                 goto out;
1384
1385         map_arg.addr = (__u32)addr;
1386
1387         if (copy_to_user((void __user *)arg, &map_arg,
1388                 sizeof(struct nvavp_map_args))) {
1389                 dev_err(&nvavp->nvhost_dev->dev,
1390                         "failed to copy phys addr\n");
1391                 ret = -EFAULT;
1392         }
1393
1394 out:
1395         return ret;
1396 }
1397
1398 static int nvavp_unmap_iova(struct file *filp, unsigned long arg)
1399 {
1400         struct nvavp_clientctx *clientctx = filp->private_data;
1401         struct nvavp_info *nvavp = clientctx->nvavp;
1402         struct nvavp_map_args map_arg;
1403         struct dma_buf *dmabuf;
1404
1405         if (copy_from_user(&map_arg, (void __user *)arg,
1406                 sizeof(struct nvavp_map_args))) {
1407                 dev_err(&nvavp->nvhost_dev->dev,
1408                         "failed to copy memory handle\n");
1409                 return -EFAULT;
1410         }
1411
1412         dmabuf = dma_buf_get(map_arg.fd);
1413         if (IS_ERR(dmabuf)) {
1414                 dev_err(&nvavp->nvhost_dev->dev,
1415                         "invalid buffer handle %08x\n", map_arg.fd);
1416                 return PTR_ERR(dmabuf);
1417         }
1418
1419         nvavp_release_iova_addr(clientctx, dmabuf, (dma_addr_t)map_arg.addr);
1420         dma_buf_put(dmabuf);
1421
1422         return 0;
1423 }
1424
1425 static int nvavp_set_clock_ioctl(struct file *filp, unsigned int cmd,
1426                                                         unsigned long arg)
1427 {
1428         struct nvavp_clientctx *clientctx = filp->private_data;
1429         struct nvavp_info *nvavp = clientctx->nvavp;
1430         struct clk *c;
1431         struct nvavp_clock_args config;
1432
1433         if (copy_from_user(&config, (void __user *)arg, sizeof(struct nvavp_clock_args)))
1434                 return -EFAULT;
1435
1436         dev_dbg(&nvavp->nvhost_dev->dev, "%s: clk_id=%d, clk_rate=%u\n",
1437                         __func__, config.id, config.rate);
1438
1439         if (config.id == NVAVP_MODULE_ID_AVP)
1440                 nvavp->sclk_rate = config.rate;
1441         else if (config.id == NVAVP_MODULE_ID_EMC)
1442                 nvavp->emc_clk_rate = config.rate;
1443         else if (config.id == NVAVP_MODULE_ID_CPU)
1444                 return nvcpu_set_clock(nvavp, config, arg);
1445
1446         c = nvavp_clk_get(nvavp, config.id);
1447         if (IS_ERR_OR_NULL(c))
1448                 return -EINVAL;
1449
1450         clk_prepare_enable(c);
1451         clk_set_rate(c, config.rate);
1452
1453         config.rate = clk_get_rate(c);
1454         clk_disable_unprepare(c);
1455         if (copy_to_user((void __user *)arg, &config, sizeof(struct nvavp_clock_args)))
1456                 return -EFAULT;
1457
1458         return 0;
1459 }
1460
1461 static int nvavp_get_clock_ioctl(struct file *filp, unsigned int cmd,
1462                                                         unsigned long arg)
1463 {
1464         struct nvavp_clientctx *clientctx = filp->private_data;
1465         struct nvavp_info *nvavp = clientctx->nvavp;
1466         struct clk *c;
1467         struct nvavp_clock_args config;
1468
1469         if (copy_from_user(&config, (void __user *)arg, sizeof(struct nvavp_clock_args)))
1470                 return -EFAULT;
1471
1472         c = nvavp_clk_get(nvavp, config.id);
1473         if (IS_ERR_OR_NULL(c))
1474                 return -EINVAL;
1475
1476         clk_prepare_enable(c);
1477         config.rate = clk_get_rate(c);
1478         clk_disable_unprepare(c);
1479
1480         if (copy_to_user((void __user *)arg, &config, sizeof(struct nvavp_clock_args)))
1481                 return -EFAULT;
1482
1483         return 0;
1484 }
1485
1486 static int nvavp_get_syncpointid_ioctl(struct file *filp, unsigned int cmd,
1487                                                         unsigned long arg)
1488 {
1489         struct nvavp_clientctx *clientctx = filp->private_data;
1490         struct nvavp_info *nvavp = clientctx->nvavp;
1491         u32 id = nvavp->syncpt_id;
1492
1493         if (_IOC_DIR(cmd) & _IOC_READ) {
1494                 if (copy_to_user((void __user *)arg, &id, sizeof(u32)))
1495                         return -EFAULT;
1496                 else
1497                         return 0;
1498         }
1499         return -EFAULT;
1500 }
1501
1502 static int nvavp_pushbuffer_submit_ioctl(struct file *filp, unsigned int cmd,
1503                                                         unsigned long arg)
1504 {
1505         struct nvavp_clientctx *clientctx = filp->private_data;
1506         struct nvavp_info *nvavp = clientctx->nvavp;
1507         struct nvavp_pushbuffer_submit_hdr hdr;
1508         u32 *cmdbuf_data;
1509         struct dma_buf *cmdbuf_dmabuf;
1510         struct dma_buf_attachment *cmdbuf_attach;
1511         struct sg_table *cmdbuf_sgt;
1512         int ret = 0, i;
1513         phys_addr_t phys_addr;
1514         unsigned long virt_addr;
1515         struct nvavp_pushbuffer_submit_hdr *user_hdr =
1516                         (struct nvavp_pushbuffer_submit_hdr *) arg;
1517         struct nvavp_syncpt syncpt;
1518
1519         syncpt.id = NVSYNCPT_INVALID;
1520         syncpt.value = 0;
1521
1522         if (_IOC_DIR(cmd) & _IOC_WRITE) {
1523                 if (copy_from_user(&hdr, (void __user *)arg,
1524                         sizeof(struct nvavp_pushbuffer_submit_hdr)))
1525                         return -EFAULT;
1526         }
1527
1528         if (!hdr.cmdbuf.mem)
1529                 return 0;
1530
1531         if (hdr.num_relocs > NVAVP_MAX_RELOCATION_COUNT) {
1532                 dev_err(&nvavp->nvhost_dev->dev,
1533                         "invalid num_relocs %d\n", hdr.num_relocs);
1534                 return -EINVAL;
1535         }
1536
1537         if (copy_from_user(clientctx->relocs, (void __user *)hdr.relocs,
1538                         sizeof(struct nvavp_reloc) * hdr.num_relocs)) {
1539                 return -EFAULT;
1540         }
1541
1542         cmdbuf_dmabuf = dma_buf_get(hdr.cmdbuf.mem);
1543         if (IS_ERR(cmdbuf_dmabuf)) {
1544                 dev_err(&nvavp->nvhost_dev->dev,
1545                         "invalid cmd buffer handle %08x\n", hdr.cmdbuf.mem);
1546                 return PTR_ERR(cmdbuf_dmabuf);
1547         }
1548
1549         if ((hdr.cmdbuf.offset & 3)
1550                 || (hdr.cmdbuf.offset >= cmdbuf_dmabuf->size)) {
1551                 dev_err(&nvavp->nvhost_dev->dev,
1552                         "invalid cmdbuf offset %d\n", hdr.cmdbuf.offset);
1553                 ret = -EINVAL;
1554                 goto err_dmabuf_attach;
1555         }
1556
1557         cmdbuf_attach = dma_buf_attach(cmdbuf_dmabuf, &nvavp->nvhost_dev->dev);
1558         if (IS_ERR(cmdbuf_attach)) {
1559                 dev_err(&nvavp->nvhost_dev->dev, "cannot attach cmdbuf_dmabuf\n");
1560                 ret = PTR_ERR(cmdbuf_attach);
1561                 goto err_dmabuf_attach;
1562         }
1563
1564         cmdbuf_sgt = dma_buf_map_attachment(cmdbuf_attach, DMA_BIDIRECTIONAL);
1565         if (IS_ERR(cmdbuf_sgt)) {
1566                 dev_err(&nvavp->nvhost_dev->dev, "cannot map cmdbuf_dmabuf\n");
1567                 ret = PTR_ERR(cmdbuf_sgt);
1568                 goto err_dmabuf_map;
1569         }
1570
1571         phys_addr = sg_dma_address(cmdbuf_sgt->sgl);
1572
1573         virt_addr = (unsigned long)dma_buf_vmap(cmdbuf_dmabuf);
1574         if (!virt_addr) {
1575                 dev_err(&nvavp->nvhost_dev->dev, "cannot vmap cmdbuf_dmabuf\n");
1576                 ret = -ENOMEM;
1577                 goto err_dmabuf_vmap;
1578         }
1579
1580         cmdbuf_data = (u32 *)(virt_addr + hdr.cmdbuf.offset);
1581         for (i = 0; i < hdr.num_relocs; i++) {
1582                 struct dma_buf *target_dmabuf;
1583                 struct dma_buf_attachment *target_attach;
1584                 struct sg_table *target_sgt;
1585                 u32 *reloc_addr, target_phys_addr;
1586
1587                 if (clientctx->relocs[i].cmdbuf_mem != hdr.cmdbuf.mem) {
1588                         dev_err(&nvavp->nvhost_dev->dev,
1589                                 "reloc info does not match target bufferID\n");
1590                         ret = -EPERM;
1591                         goto err_reloc_info;
1592                 }
1593
1594                 if ((clientctx->relocs[i].cmdbuf_offset & 3)
1595                         || (clientctx->relocs[i].cmdbuf_offset >=
1596                                 cmdbuf_dmabuf->size)
1597                         || (clientctx->relocs[i].cmdbuf_offset >=
1598                                 (cmdbuf_dmabuf->size - hdr.cmdbuf.offset))) {
1599                         dev_err(&nvavp->nvhost_dev->dev,
1600                                 "invalid reloc offset in cmdbuf %d\n",
1601                                 clientctx->relocs[i].cmdbuf_offset);
1602                         ret = -EINVAL;
1603                         goto err_reloc_info;
1604                 }
1605
1606                 reloc_addr = cmdbuf_data +
1607                              (clientctx->relocs[i].cmdbuf_offset >> 2);
1608
1609                 target_dmabuf = dma_buf_get(clientctx->relocs[i].target);
1610                 if (IS_ERR(target_dmabuf)) {
1611                         ret = PTR_ERR(target_dmabuf);
1612                         goto target_dmabuf_fail;
1613                 }
1614
1615                 if ((clientctx->relocs[i].target_offset & 3)
1616                         || (clientctx->relocs[i].target_offset >=
1617                                 target_dmabuf->size)) {
1618                         dev_err(&nvavp->nvhost_dev->dev,
1619                                 "invalid target offset in reloc %d\n",
1620                                 clientctx->relocs[i].target_offset);
1621                         ret = -EINVAL;
1622                         goto target_attach_fail;
1623                 }
1624
1625                 target_attach = dma_buf_attach(target_dmabuf,
1626                                                &nvavp->nvhost_dev->dev);
1627                 if (IS_ERR(target_attach)) {
1628                         ret = PTR_ERR(target_attach);
1629                         goto target_attach_fail;
1630                 }
1631                 target_sgt = dma_buf_map_attachment(target_attach,
1632                                                     DMA_BIDIRECTIONAL);
1633                 if (IS_ERR(target_sgt)) {
1634                         ret = PTR_ERR(target_sgt);
1635                         goto target_map_fail;
1636                 }
1637
1638                 target_phys_addr = sg_dma_address(target_sgt->sgl);
1639                 if (!target_phys_addr)
1640                         target_phys_addr = sg_phys(target_sgt->sgl);
1641                 target_phys_addr += clientctx->relocs[i].target_offset;
1642                 writel(target_phys_addr, reloc_addr);
1643                 dma_buf_unmap_attachment(target_attach, target_sgt,
1644                                          DMA_BIDIRECTIONAL);
1645 target_map_fail:
1646                 dma_buf_detach(target_dmabuf, target_attach);
1647 target_attach_fail:
1648                 dma_buf_put(target_dmabuf);
1649 target_dmabuf_fail:
1650                 if (ret != 0)
1651                         goto err_reloc_info;
1652         }
1653
1654         if (hdr.syncpt) {
1655                 ret = nvavp_pushbuffer_update(nvavp,
1656                                              (phys_addr + hdr.cmdbuf.offset),
1657                                               hdr.cmdbuf.words, &syncpt,
1658                                               (hdr.flags & NVAVP_UCODE_EXT),
1659                                                 clientctx->channel_id);
1660
1661                 if (copy_to_user((void __user *)user_hdr->syncpt, &syncpt,
1662                                 sizeof(struct nvavp_syncpt))) {
1663                         ret = -EFAULT;
1664                         goto err_reloc_info;
1665                 }
1666         } else {
1667                 ret = nvavp_pushbuffer_update(nvavp,
1668                                              (phys_addr + hdr.cmdbuf.offset),
1669                                               hdr.cmdbuf.words, NULL,
1670                                               (hdr.flags & NVAVP_UCODE_EXT),
1671                                                 clientctx->channel_id);
1672         }
1673
1674 err_reloc_info:
1675         dma_buf_vunmap(cmdbuf_dmabuf, (void *)virt_addr);
1676 err_dmabuf_vmap:
1677         dma_buf_unmap_attachment(cmdbuf_attach, cmdbuf_sgt, DMA_BIDIRECTIONAL);
1678 err_dmabuf_map:
1679         dma_buf_detach(cmdbuf_dmabuf, cmdbuf_attach);
1680 err_dmabuf_attach:
1681         dma_buf_put(cmdbuf_dmabuf);
1682         return ret;
1683 }
1684
1685 #ifdef CONFIG_COMPAT
1686 static int nvavp_pushbuffer_submit_compat_ioctl(struct file *filp,
1687                                                         unsigned int cmd,
1688                                                         unsigned long arg)
1689 {
1690         struct nvavp_clientctx *clientctx = filp->private_data;
1691         struct nvavp_info *nvavp = clientctx->nvavp;
1692         struct nvavp_pushbuffer_submit_hdr_v32 hdr_v32;
1693         struct nvavp_pushbuffer_submit_hdr __user *user_hdr;
1694         int ret = 0;
1695
1696         if (_IOC_DIR(cmd) & _IOC_WRITE) {
1697                 if (copy_from_user(&hdr_v32, (void __user *)arg,
1698                         sizeof(struct nvavp_pushbuffer_submit_hdr_v32)))
1699                         return -EFAULT;
1700         }
1701
1702         if (!hdr_v32.cmdbuf.mem)
1703                 return 0;
1704
1705         user_hdr = compat_alloc_user_space(sizeof(*user_hdr));
1706         if (!access_ok(VERIFY_WRITE, user_hdr, sizeof(*user_hdr)))
1707                 return -EFAULT;
1708
1709         if (__put_user(hdr_v32.cmdbuf.mem, &user_hdr->cmdbuf.mem)
1710             || __put_user(hdr_v32.cmdbuf.offset, &user_hdr->cmdbuf.offset)
1711             || __put_user(hdr_v32.cmdbuf.words, &user_hdr->cmdbuf.words)
1712             || __put_user((void __user *)(unsigned long)hdr_v32.relocs,
1713                           &user_hdr->relocs)
1714             || __put_user(hdr_v32.num_relocs, &user_hdr->num_relocs)
1715             || __put_user((void __user *)(unsigned long)hdr_v32.syncpt,
1716                           &user_hdr->syncpt)
1717             || __put_user(hdr_v32.flags, &user_hdr->flags))
1718                 return -EFAULT;
1719
1720         ret = nvavp_pushbuffer_submit_ioctl(filp, cmd, (unsigned long)user_hdr);
1721         if (ret)
1722                 return ret;
1723
1724         if (__get_user(hdr_v32.syncpt, &user_hdr->syncpt))
1725                 return -EFAULT;
1726
1727         if (copy_to_user((void __user *)arg, &hdr_v32,
1728                           sizeof(struct nvavp_pushbuffer_submit_hdr_v32))) {
1729                 ret = -EFAULT;
1730         }
1731
1732         return ret;
1733 }
1734 #endif
1735
1736 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1737 int nvavp_pushbuffer_submit_audio(nvavp_clientctx_t client, int cmd_buf_phys,
1738                                   int cmd_buf_words)
1739 {
1740         struct nvavp_clientctx *clientctx = client;
1741         struct nvavp_info *nvavp = clientctx->nvavp;
1742
1743         return nvavp_pushbuffer_update(nvavp,
1744                                       cmd_buf_phys,
1745                                       cmd_buf_words, NULL,
1746                                       NVAVP_UCODE_EXT,
1747                                       NVAVP_AUDIO_CHANNEL);
1748 }
1749 EXPORT_SYMBOL_GPL(nvavp_pushbuffer_submit_audio);
1750
1751 void nvavp_register_audio_cb(nvavp_clientctx_t client, void (*cb)(void))
1752 {
1753         struct nvavp_clientctx *clientctx = client;
1754         struct nvavp_info *nvavp = clientctx->nvavp;
1755
1756         nvavp->audio_notify = cb;
1757 }
1758 EXPORT_SYMBOL_GPL(nvavp_register_audio_cb);
1759 #endif
1760
1761 static int nvavp_wake_avp_ioctl(struct file *filp, unsigned int cmd,
1762                                                         unsigned long arg)
1763 {
1764         wmb();
1765         /* wake up avp */
1766         return nvavp_outbox_write(0xA0000001);
1767 }
1768
1769 static int nvavp_force_clock_stay_on_ioctl(struct file *filp, unsigned int cmd,
1770                                                         unsigned long arg)
1771 {
1772         struct nvavp_clientctx *clientctx = filp->private_data;
1773         struct nvavp_info *nvavp = clientctx->nvavp;
1774         struct nvavp_clock_stay_on_state_args clock;
1775
1776         if (copy_from_user(&clock, (void __user *)arg,
1777                            sizeof(struct nvavp_clock_stay_on_state_args)))
1778                 return -EFAULT;
1779
1780         dev_dbg(&nvavp->nvhost_dev->dev, "%s: state=%d\n",
1781                 __func__, clock.state);
1782
1783         if (clock.state != NVAVP_CLOCK_STAY_ON_DISABLED &&
1784                    clock.state !=  NVAVP_CLOCK_STAY_ON_ENABLED) {
1785                 dev_err(&nvavp->nvhost_dev->dev, "%s: invalid argument=%d\n",
1786                         __func__, clock.state);
1787                 return -EINVAL;
1788         }
1789
1790         if (clock.state) {
1791                 mutex_lock(&nvavp->open_lock);
1792                 if (clientctx->clk_reqs++ == 0) {
1793                         nvavp_clks_enable(nvavp);
1794                         nvavp->stay_on = true;
1795                 }
1796                 mutex_unlock(&nvavp->open_lock);
1797                 cancel_work_sync(&nvavp->clock_disable_work);
1798         } else {
1799                 mutex_lock(&nvavp->open_lock);
1800                 if (--clientctx->clk_reqs == 0) {
1801                         nvavp->stay_on = false;
1802                         nvavp_clks_disable(nvavp);
1803                 }
1804                 mutex_unlock(&nvavp->open_lock);
1805                 if (!nvavp->stay_on)
1806                         schedule_work(&nvavp->clock_disable_work);
1807         }
1808         return 0;
1809 }
1810
1811 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1812 int nvavp_enable_audio_clocks(nvavp_clientctx_t client, u32 clk_id)
1813 {
1814         struct nvavp_clientctx *clientctx = client;
1815         struct nvavp_info *nvavp = clientctx->nvavp;
1816
1817         dev_dbg(&nvavp->nvhost_dev->dev, "%s: clk_id = %d\n",
1818                         __func__, clk_id);
1819
1820         mutex_lock(&nvavp->open_lock);
1821         if (clk_id == NVAVP_MODULE_ID_VCP)
1822                 clk_prepare_enable(nvavp->vcp_clk);
1823         else if (clk_id == NVAVP_MODULE_ID_BSEA)
1824                 clk_prepare_enable(nvavp->bsea_clk);
1825         mutex_unlock(&nvavp->open_lock);
1826         return 0;
1827 }
1828 EXPORT_SYMBOL_GPL(nvavp_enable_audio_clocks);
1829
1830 int nvavp_disable_audio_clocks(nvavp_clientctx_t client, u32 clk_id)
1831 {
1832         struct nvavp_clientctx *clientctx = client;
1833         struct nvavp_info *nvavp = clientctx->nvavp;
1834
1835         dev_dbg(&nvavp->nvhost_dev->dev, "%s: clk_id = %d\n",
1836                         __func__, clk_id);
1837
1838         mutex_lock(&nvavp->open_lock);
1839         if (clk_id == NVAVP_MODULE_ID_VCP)
1840                 clk_disable_unprepare(nvavp->vcp_clk);
1841         else if (clk_id == NVAVP_MODULE_ID_BSEA)
1842                 clk_disable_unprepare(nvavp->bsea_clk);
1843         mutex_unlock(&nvavp->open_lock);
1844         return 0;
1845 }
1846 EXPORT_SYMBOL_GPL(nvavp_disable_audio_clocks);
1847 #else
1848 int nvavp_enable_audio_clocks(nvavp_clientctx_t client, u32 clk_id)
1849 {
1850         return 0;
1851 }
1852 EXPORT_SYMBOL_GPL(nvavp_enable_audio_clocks);
1853
1854 int nvavp_disable_audio_clocks(nvavp_clientctx_t client, u32_clk_id)
1855 {
1856         return 0;
1857 }
1858 EXPORT_SYMBOL_GPL(nvavp_disable_audio_clocks);
1859 #endif
1860
1861 static int nvavp_set_min_online_cpus_ioctl(struct file *filp, unsigned int cmd,
1862                                         unsigned long arg)
1863 {
1864         struct nvavp_clientctx *clientctx = filp->private_data;
1865         struct nvavp_info *nvavp = clientctx->nvavp;
1866         struct nvavp_num_cpus_args config;
1867
1868         if (copy_from_user(&config, (void __user *)arg,
1869                                         sizeof(struct nvavp_num_cpus_args)))
1870                 return -EFAULT;
1871
1872         dev_dbg(&nvavp->nvhost_dev->dev, "%s: min_online_cpus=%d\n",
1873                         __func__, config.min_online_cpus);
1874
1875         if (config.min_online_cpus > 0)
1876                 pm_qos_update_request(&nvavp->min_online_cpus_req,
1877                                         config.min_online_cpus);
1878         else
1879                 pm_qos_update_request(&nvavp->min_online_cpus_req,
1880                                         PM_QOS_CPU_FREQ_MIN_DEFAULT_VALUE);
1881
1882         return 0;
1883 }
1884
1885 static int tegra_nvavp_open(struct nvavp_info *nvavp,
1886                         struct nvavp_clientctx **client, int channel_id)
1887 {
1888         struct nvavp_clientctx *clientctx;
1889         int ret = 0;
1890
1891         dev_dbg(&nvavp->nvhost_dev->dev, "%s: ++\n", __func__);
1892
1893         clientctx = kzalloc(sizeof(*clientctx), GFP_KERNEL);
1894         if (!clientctx)
1895                 return -ENOMEM;
1896
1897         pr_debug("tegra_nvavp_open channel_id (%d)\n", channel_id);
1898
1899         clientctx->channel_id = channel_id;
1900
1901         ret = nvavp_init(nvavp, channel_id);
1902
1903         if (!ret) {
1904                 nvavp->refcount++;
1905                 if (IS_VIDEO_CHANNEL_ID(channel_id))
1906                         nvavp->video_refcnt++;
1907                 if (IS_AUDIO_CHANNEL_ID(channel_id))
1908                         nvavp->audio_refcnt++;
1909         }
1910
1911         clientctx->nvavp = nvavp;
1912         clientctx->iova_handles = RB_ROOT;
1913         *client = clientctx;
1914
1915         return ret;
1916 }
1917
1918 static int tegra_nvavp_video_open(struct inode *inode, struct file *filp)
1919 {
1920         struct miscdevice *miscdev = filp->private_data;
1921         struct nvavp_info *nvavp = dev_get_drvdata(miscdev->parent);
1922         struct nvavp_clientctx *clientctx;
1923         int ret = 0;
1924
1925         pr_debug("tegra_nvavp_video_open NVAVP_VIDEO_CHANNEL\n");
1926
1927         nonseekable_open(inode, filp);
1928
1929         mutex_lock(&nvavp->open_lock);
1930         ret = tegra_nvavp_open(nvavp, &clientctx, NVAVP_VIDEO_CHANNEL);
1931         filp->private_data = clientctx;
1932         mutex_unlock(&nvavp->open_lock);
1933
1934         return ret;
1935 }
1936
1937 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1938 static int tegra_nvavp_audio_open(struct inode *inode, struct file *filp)
1939 {
1940         struct miscdevice *miscdev = filp->private_data;
1941         struct nvavp_info *nvavp = dev_get_drvdata(miscdev->parent);
1942         struct nvavp_clientctx *clientctx;
1943         int ret = 0;
1944
1945         pr_debug("tegra_nvavp_audio_open NVAVP_AUDIO_CHANNEL\n");
1946
1947         nonseekable_open(inode, filp);
1948
1949         mutex_lock(&nvavp->open_lock);
1950         ret = tegra_nvavp_open(nvavp, &clientctx, NVAVP_AUDIO_CHANNEL);
1951         filp->private_data = clientctx;
1952         mutex_unlock(&nvavp->open_lock);
1953
1954         return ret;
1955 }
1956
1957 int tegra_nvavp_audio_client_open(nvavp_clientctx_t *clientctx)
1958 {
1959         struct nvavp_info *nvavp = nvavp_info_ctx;
1960         int ret = 0;
1961
1962         mutex_lock(&nvavp->open_lock);
1963         ret = tegra_nvavp_open(nvavp, (struct nvavp_clientctx **)clientctx,
1964                                 NVAVP_AUDIO_CHANNEL);
1965         mutex_unlock(&nvavp->open_lock);
1966
1967         return ret;
1968 }
1969 EXPORT_SYMBOL_GPL(tegra_nvavp_audio_client_open);
1970 #endif
1971
1972 static int tegra_nvavp_release(struct nvavp_clientctx *clientctx,
1973                                int channel_id)
1974 {
1975         struct nvavp_info *nvavp = clientctx->nvavp;
1976         int ret = 0;
1977
1978         dev_dbg(&nvavp->nvhost_dev->dev, "%s: ++\n", __func__);
1979
1980         if (!nvavp->refcount) {
1981                 dev_err(&nvavp->nvhost_dev->dev,
1982                         "releasing while in invalid state\n");
1983                 ret = -EINVAL;
1984                 goto out;
1985         }
1986
1987         /* if this client had any requests, drop our clk ref */
1988         if (clientctx->clk_reqs)
1989                 nvavp_clks_disable(nvavp);
1990
1991         if (nvavp->refcount > 0)
1992                 nvavp->refcount--;
1993         if (!nvavp->refcount) {
1994                 mutex_unlock(&nvavp->open_lock);
1995                 nvavp_uninit(nvavp);
1996                 mutex_lock(&nvavp->open_lock);
1997         }
1998
1999         if (IS_VIDEO_CHANNEL_ID(channel_id))
2000                 nvavp->video_refcnt--;
2001         if (IS_AUDIO_CHANNEL_ID(channel_id))
2002                 nvavp->audio_refcnt--;
2003
2004 out:
2005         nvavp_remove_iova_mapping(clientctx);
2006         kfree(clientctx);
2007         return ret;
2008 }
2009
2010 static int tegra_nvavp_video_release(struct inode *inode, struct file *filp)
2011 {
2012         struct nvavp_clientctx *clientctx = filp->private_data;
2013         struct nvavp_info *nvavp = clientctx->nvavp;
2014         int ret = 0;
2015
2016         mutex_lock(&nvavp->open_lock);
2017         filp->private_data = NULL;
2018         ret = tegra_nvavp_release(clientctx, NVAVP_VIDEO_CHANNEL);
2019         mutex_unlock(&nvavp->open_lock);
2020
2021         return ret;
2022 }
2023
2024 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2025 static int tegra_nvavp_audio_release(struct inode *inode,
2026                                           struct file *filp)
2027 {
2028         struct nvavp_clientctx *clientctx = filp->private_data;
2029         struct nvavp_info *nvavp = clientctx->nvavp;
2030         int ret = 0;
2031
2032         mutex_lock(&nvavp->open_lock);
2033         filp->private_data = NULL;
2034         ret = tegra_nvavp_release(clientctx, NVAVP_AUDIO_CHANNEL);
2035         mutex_unlock(&nvavp->open_lock);
2036
2037         return ret;
2038 }
2039
2040 int tegra_nvavp_audio_client_release(nvavp_clientctx_t client)
2041 {
2042         struct nvavp_clientctx *clientctx = client;
2043         struct nvavp_info *nvavp = clientctx->nvavp;
2044         int ret = 0;
2045
2046         mutex_lock(&nvavp->open_lock);
2047         ret = tegra_nvavp_release(clientctx, NVAVP_AUDIO_CHANNEL);
2048         mutex_unlock(&nvavp->open_lock);
2049
2050         return ret;
2051 }
2052 EXPORT_SYMBOL_GPL(tegra_nvavp_audio_client_release);
2053 #endif
2054
2055
2056 static int
2057 nvavp_channel_open(struct file *filp, struct nvavp_channel_open_args *arg)
2058 {
2059         int fd, err = 0;
2060         struct file *file;
2061         char *name;
2062         struct nvavp_clientctx *clientctx = filp->private_data;
2063         struct nvavp_info *nvavp = clientctx->nvavp;
2064
2065         err = get_unused_fd_flags(O_RDWR);
2066         if (err < 0)
2067                 return err;
2068
2069         fd = err;
2070
2071         name = kasprintf(GFP_KERNEL, "nvavp-channel-fd%d", fd);
2072         if (!name) {
2073                 err = -ENOMEM;
2074                 put_unused_fd(fd);
2075                 return err;
2076         }
2077
2078         file = anon_inode_getfile(name, filp->f_op, &(nvavp->video_misc_dev),
2079                         O_RDWR);
2080         kfree(name);
2081         if (IS_ERR(file)) {
2082                 err = PTR_ERR(file);
2083                 put_unused_fd(fd);
2084                 return err;
2085         }
2086
2087         fd_install(fd, file);
2088
2089         nonseekable_open(file->f_inode, filp);
2090         mutex_lock(&nvavp->open_lock);
2091         err = tegra_nvavp_open(nvavp,
2092                 (struct nvavp_clientctx **)&file->private_data,
2093                 clientctx->channel_id);
2094         if (err) {
2095                 put_unused_fd(fd);
2096                 fput(file);
2097                 mutex_unlock(&nvavp->open_lock);
2098                 return err;
2099         }
2100         mutex_unlock(&nvavp->open_lock);
2101
2102         arg->channel_fd = fd;
2103         return err;
2104 }
2105
2106 static long tegra_nvavp_ioctl(struct file *filp, unsigned int cmd,
2107                             unsigned long arg)
2108 {
2109         struct nvavp_clientctx *clientctx = filp->private_data;
2110         struct nvavp_clock_args config;
2111         int ret = 0;
2112         u8 buf[NVAVP_IOCTL_CHANNEL_MAX_ARG_SIZE];
2113
2114         if (_IOC_TYPE(cmd) != NVAVP_IOCTL_MAGIC ||
2115             _IOC_NR(cmd) < NVAVP_IOCTL_MIN_NR ||
2116             _IOC_NR(cmd) > NVAVP_IOCTL_MAX_NR)
2117                 return -EFAULT;
2118
2119         switch (cmd) {
2120         case NVAVP_IOCTL_SET_NVMAP_FD:
2121                 break;
2122         case NVAVP_IOCTL_GET_SYNCPOINT_ID:
2123                 ret = nvavp_get_syncpointid_ioctl(filp, cmd, arg);
2124                 break;
2125         case NVAVP_IOCTL_PUSH_BUFFER_SUBMIT:
2126                 ret = nvavp_pushbuffer_submit_ioctl(filp, cmd, arg);
2127                 break;
2128         case NVAVP_IOCTL_SET_CLOCK:
2129                 ret = nvavp_set_clock_ioctl(filp, cmd, arg);
2130                 break;
2131         case NVAVP_IOCTL_GET_CLOCK:
2132                 ret = nvavp_get_clock_ioctl(filp, cmd, arg);
2133                 break;
2134         case NVAVP_IOCTL_WAKE_AVP:
2135                 ret = nvavp_wake_avp_ioctl(filp, cmd, arg);
2136                 break;
2137         case NVAVP_IOCTL_FORCE_CLOCK_STAY_ON:
2138                 ret = nvavp_force_clock_stay_on_ioctl(filp, cmd, arg);
2139                 break;
2140         case NVAVP_IOCTL_ENABLE_AUDIO_CLOCKS:
2141                 if (copy_from_user(&config, (void __user *)arg,
2142                         sizeof(struct nvavp_clock_args))) {
2143                         ret = -EFAULT;
2144                         break;
2145                 }
2146                 ret = nvavp_enable_audio_clocks(clientctx, config.id);
2147                 break;
2148         case NVAVP_IOCTL_DISABLE_AUDIO_CLOCKS:
2149                 if (copy_from_user(&config, (void __user *)arg,
2150                         sizeof(struct nvavp_clock_args))) {
2151                         ret = -EFAULT;
2152                         break;
2153                 }
2154                 ret = nvavp_disable_audio_clocks(clientctx, config.id);
2155                 break;
2156         case NVAVP_IOCTL_SET_MIN_ONLINE_CPUS:
2157                 ret = nvavp_set_min_online_cpus_ioctl(filp, cmd, arg);
2158                 break;
2159         case NVAVP_IOCTL_MAP_IOVA:
2160                 ret = nvavp_map_iova(filp, cmd, arg);
2161                 break;
2162         case NVAVP_IOCTL_UNMAP_IOVA:
2163                 ret = nvavp_unmap_iova(filp, arg);
2164                 break;
2165         case NVAVP_IOCTL_CHANNEL_OPEN:
2166                 ret = nvavp_channel_open(filp, (void *)buf);
2167                 if (ret == 0)
2168                         ret = copy_to_user((void __user *)arg, buf,
2169                         _IOC_SIZE(cmd));
2170                 break;
2171         default:
2172                 ret = -EINVAL;
2173                 break;
2174         }
2175         return ret;
2176 }
2177
2178 #ifdef CONFIG_COMPAT
2179 static long tegra_nvavp_compat_ioctl(struct file *filp, unsigned int cmd,
2180                             unsigned long arg)
2181 {
2182         int ret = 0;
2183
2184         if (_IOC_TYPE(cmd) != NVAVP_IOCTL_MAGIC ||
2185             _IOC_NR(cmd) < NVAVP_IOCTL_MIN_NR ||
2186             _IOC_NR(cmd) > NVAVP_IOCTL_MAX_NR)
2187                 return -EFAULT;
2188
2189         switch (cmd) {
2190         case NVAVP_IOCTL_PUSH_BUFFER_SUBMIT32:
2191                 ret = nvavp_pushbuffer_submit_compat_ioctl(filp, cmd, arg);
2192                 break;
2193         default:
2194                 ret = tegra_nvavp_ioctl(filp, cmd, arg);
2195                 break;
2196         }
2197         return ret;
2198 }
2199 #endif
2200
2201 static const struct file_operations tegra_video_nvavp_fops = {
2202         .owner          = THIS_MODULE,
2203         .open           = tegra_nvavp_video_open,
2204         .release        = tegra_nvavp_video_release,
2205         .unlocked_ioctl = tegra_nvavp_ioctl,
2206 #ifdef CONFIG_COMPAT
2207         .compat_ioctl   = tegra_nvavp_compat_ioctl,
2208 #endif
2209 };
2210
2211 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2212 static const struct file_operations tegra_audio_nvavp_fops = {
2213         .owner          = THIS_MODULE,
2214         .open           = tegra_nvavp_audio_open,
2215         .release        = tegra_nvavp_audio_release,
2216         .unlocked_ioctl = tegra_nvavp_ioctl,
2217 #ifdef CONFIG_COMPAT
2218         .compat_ioctl   = tegra_nvavp_compat_ioctl,
2219 #endif
2220 };
2221 #endif
2222
2223 static ssize_t boost_sclk_show(struct device *dev,
2224         struct device_attribute *attr, char *buf)
2225 {
2226         return snprintf(buf, PAGE_SIZE, "%d\n", boost_sclk);
2227 }
2228
2229 static ssize_t boost_sclk_store(struct device *dev,
2230         struct device_attribute *attr, const char *buf, size_t count)
2231 {
2232         struct platform_device *ndev = to_platform_device(dev);
2233         struct nvavp_info *nvavp = platform_get_drvdata(ndev);
2234         unsigned long val = 0;
2235
2236         if (kstrtoul(buf, 10, &val) < 0)
2237                 return -EINVAL;
2238
2239         if (val)
2240                 clk_set_rate(nvavp->sclk, SCLK_BOOST_RATE);
2241         else if (!val)
2242                 clk_set_rate(nvavp->sclk, 0);
2243
2244         boost_sclk = val;
2245
2246         return count;
2247 }
2248
2249 DEVICE_ATTR(boost_sclk, S_IRUGO | S_IWUSR, boost_sclk_show, boost_sclk_store);
2250
2251 enum nvavp_heap {
2252         NVAVP_USE_SMMU = (1 << 0),
2253         NVAVP_USE_CARVEOUT = (1 << 1)
2254 };
2255
2256 static int nvavp_reserve_os_mem(struct nvavp_info *nvavp, dma_addr_t phys)
2257 {
2258         int ret = -ENOMEM;
2259         if (!pfn_valid(__phys_to_pfn(phys))) {
2260                 if (memblock_reserve(phys, SZ_1M)) {
2261                         dev_err(&nvavp->nvhost_dev->dev,
2262                                 "failed to reserve mem block %lx\n",
2263                                                 (unsigned long)phys);
2264                 } else
2265                         ret = 0;
2266         }
2267         return ret;
2268 }
2269
2270 #ifdef CONFIG_OF
2271 static struct of_device_id tegra_nvavp_of_match[] = {
2272         { .compatible = "nvidia,tegra30-nvavp", NULL },
2273         { .compatible = "nvidia,tegra114-nvavp", NULL },
2274         { .compatible = "nvidia,tegra124-nvavp", NULL },
2275         { },
2276 };
2277 #endif
2278
2279 static int tegra_nvavp_probe(struct platform_device *ndev)
2280 {
2281         struct nvavp_info *nvavp;
2282         int irq;
2283         enum nvavp_heap heap_mask;
2284         int ret = 0, channel_id;
2285         struct device_node *np;
2286
2287         np = ndev->dev.of_node;
2288         if (np) {
2289                 irq = platform_get_irq(ndev, 0);
2290                 nvavp_reg_base = of_iomap(np, 0);
2291         } else {
2292                 irq = platform_get_irq_byname(ndev, "mbox_from_nvavp_pending");
2293         }
2294
2295         if (irq < 0) {
2296                 dev_err(&ndev->dev, "invalid nvhost data\n");
2297                 return -EINVAL;
2298         }
2299
2300         if (!nvavp_reg_base) {
2301                 dev_err(&ndev->dev, "unable to map, memory mapped IO\n");
2302                 return -EINVAL;
2303         }
2304
2305         /* Set the max segment size supported. */
2306         ndev->dev.dma_parms = &nvavp_dma_parameters;
2307
2308         nvavp = kzalloc(sizeof(struct nvavp_info), GFP_KERNEL);
2309         if (!nvavp) {
2310                 dev_err(&ndev->dev, "cannot allocate avp_info\n");
2311                 return -ENOMEM;
2312         }
2313
2314         memset(nvavp, 0, sizeof(*nvavp));
2315
2316 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU) /* Tegra2 with AVP MMU */
2317         heap_mask = NVAVP_USE_CARVEOUT;
2318 #elif defined(CONFIG_TEGRA_AVP_KERNEL_ON_SMMU) /* Tegra3 with SMMU */
2319         heap_mask = NVAVP_USE_SMMU;
2320 #else /* nvmem= carveout */
2321         heap_mask = NVAVP_USE_CARVEOUT;
2322 #endif
2323         switch (heap_mask) {
2324         case NVAVP_USE_SMMU:
2325
2326                 nvavp->os_info.phys = 0x8ff00000;
2327                 nvavp->os_info.data = dma_alloc_at_coherent(
2328                                                         &ndev->dev,
2329                                                         SZ_1M,
2330                                                         &nvavp->os_info.phys,
2331                                                         GFP_KERNEL);
2332
2333                 if (!nvavp->os_info.data || nvavp->os_info.phys != 0x8ff00000) {
2334                         nvavp->os_info.phys = 0x0ff00000;
2335                         nvavp->os_info.data = dma_alloc_at_coherent(
2336                                                         &ndev->dev,
2337                                                         SZ_1M,
2338                                                         &nvavp->os_info.phys,
2339                                                         GFP_KERNEL);
2340
2341                         if (!nvavp->os_info.data ||
2342                             nvavp->os_info.phys != 0x0ff00000) {
2343                                 dev_err(&ndev->dev, "cannot allocate IOVA memory\n");
2344                                 ret = -ENOMEM;
2345                         }
2346                 }
2347
2348                 dev_info(&ndev->dev,
2349                         "allocated IOVA at %lx for AVP os\n",
2350                         (unsigned long)nvavp->os_info.phys);
2351                 break;
2352         case NVAVP_USE_CARVEOUT:
2353                 if (!nvavp_reserve_os_mem(nvavp, 0x8e000000))
2354                         nvavp->os_info.phys = 0x8e000000;
2355                 else if (!nvavp_reserve_os_mem(nvavp, 0xf7e00000))
2356                         nvavp->os_info.phys = 0xf7e00000;
2357                 else if (!nvavp_reserve_os_mem(nvavp, 0x9e000000))
2358                         nvavp->os_info.phys = 0x9e000000;
2359                 else if (!nvavp_reserve_os_mem(nvavp, 0xbe000000))
2360                         nvavp->os_info.phys = 0xbe000000;
2361                 else {
2362                         dev_err(&nvavp->nvhost_dev->dev,
2363                                 "cannot find nvmem= carveout to load AVP os\n");
2364                         dev_err(&nvavp->nvhost_dev->dev,
2365                                 "check kernel command line "
2366                                 "to see if nvmem= is defined\n");
2367                         BUG();
2368
2369                 }
2370
2371                 dev_info(&ndev->dev,
2372                         "allocated carveout memory at %lx for AVP os\n",
2373                         (unsigned long)nvavp->os_info.phys);
2374                 break;
2375         default:
2376                 dev_err(&ndev->dev, "invalid/non-supported heap for AVP os\n");
2377                 ret = -EINVAL;
2378                 goto err_get_syncpt;
2379         }
2380
2381         nvavp->mbox_from_avp_pend_irq = irq;
2382         mutex_init(&nvavp->open_lock);
2383
2384         for (channel_id = 0; channel_id < NVAVP_NUM_CHANNELS; channel_id++)
2385                 mutex_init(&nvavp->channel_info[channel_id].pushbuffer_lock);
2386
2387         /* TODO DO NOT USE NVAVP DEVICE */
2388         nvavp->cop_clk = clk_get(&ndev->dev, "cop");
2389         if (IS_ERR(nvavp->cop_clk)) {
2390                 dev_err(&ndev->dev, "cannot get cop clock\n");
2391                 ret = -ENOENT;
2392                 goto err_get_cop_clk;
2393         }
2394
2395         nvavp->vde_clk = clk_get(&ndev->dev, "vde");
2396         if (IS_ERR(nvavp->vde_clk)) {
2397                 dev_err(&ndev->dev, "cannot get vde clock\n");
2398                 ret = -ENOENT;
2399                 goto err_get_vde_clk;
2400         }
2401
2402         nvavp->bsev_clk = clk_get(&ndev->dev, "bsev");
2403         if (IS_ERR(nvavp->bsev_clk)) {
2404                 dev_err(&ndev->dev, "cannot get bsev clock\n");
2405                 ret = -ENOENT;
2406                 goto err_get_bsev_clk;
2407         }
2408
2409         nvavp->sclk = clk_get(&ndev->dev, "sclk");
2410         if (IS_ERR(nvavp->sclk)) {
2411                 dev_err(&ndev->dev, "cannot get avp.sclk clock\n");
2412                 ret = -ENOENT;
2413                 goto err_get_sclk;
2414         }
2415
2416         nvavp->emc_clk = clk_get(&ndev->dev, "emc");
2417         if (IS_ERR(nvavp->emc_clk)) {
2418                 dev_err(&ndev->dev, "cannot get emc clock\n");
2419                 ret = -ENOENT;
2420                 goto err_get_emc_clk;
2421         }
2422
2423 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2424         nvavp->bsea_clk = clk_get(&ndev->dev, "bsea");
2425         if (IS_ERR(nvavp->bsea_clk)) {
2426                 dev_err(&ndev->dev, "cannot get bsea clock\n");
2427                 ret = -ENOENT;
2428                 goto err_get_bsea_clk;
2429         }
2430
2431         nvavp->vcp_clk = clk_get(&ndev->dev, "vcp");
2432         if (IS_ERR(nvavp->vcp_clk)) {
2433                 dev_err(&ndev->dev, "cannot get vcp clock\n");
2434                 ret = -ENOENT;
2435                 goto err_get_vcp_clk;
2436         }
2437 #endif
2438
2439         nvavp->clk_enabled = 0;
2440         nvavp_halt_avp(nvavp);
2441
2442         INIT_WORK(&nvavp->clock_disable_work, clock_disable_handler);
2443
2444         nvavp->video_misc_dev.minor = MISC_DYNAMIC_MINOR;
2445         nvavp->video_misc_dev.name = "tegra_avpchannel";
2446         nvavp->video_misc_dev.fops = &tegra_video_nvavp_fops;
2447         nvavp->video_misc_dev.mode = S_IRWXUGO;
2448         nvavp->video_misc_dev.parent = &ndev->dev;
2449
2450         ret = misc_register(&nvavp->video_misc_dev);
2451         if (ret) {
2452                 dev_err(&ndev->dev, "unable to register misc device!\n");
2453                 goto err_misc_reg;
2454         }
2455
2456 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2457         INIT_WORK(&nvavp->app_notify_work, app_notify_handler);
2458         nvavp->audio_misc_dev.minor = MISC_DYNAMIC_MINOR;
2459         nvavp->audio_misc_dev.name = "tegra_audio_avpchannel";
2460         nvavp->audio_misc_dev.fops = &tegra_audio_nvavp_fops;
2461         nvavp->audio_misc_dev.mode = S_IRWXUGO;
2462         nvavp->audio_misc_dev.parent = &ndev->dev;
2463
2464         ret = misc_register(&nvavp->audio_misc_dev);
2465         if (ret) {
2466         dev_err(&ndev->dev, "unable to register misc device!\n");
2467                 goto err_audio_misc_reg;
2468         }
2469 #endif
2470
2471         ret = request_irq(irq, nvavp_mbox_pending_isr, 0,
2472                           TEGRA_NVAVP_NAME, nvavp);
2473         if (ret) {
2474                 dev_err(&ndev->dev, "cannot register irq handler\n");
2475                 goto err_req_irq_pend;
2476         }
2477         disable_irq(nvavp->mbox_from_avp_pend_irq);
2478
2479         nvavp->nvhost_dev = ndev;
2480         platform_set_drvdata(ndev, nvavp);
2481
2482         tegra_pd_add_device(&ndev->dev);
2483         pm_runtime_use_autosuspend(&ndev->dev);
2484         pm_runtime_set_autosuspend_delay(&ndev->dev, 2000);
2485         pm_runtime_enable(&ndev->dev);
2486
2487         ret = device_create_file(&ndev->dev, &dev_attr_boost_sclk);
2488         if (ret) {
2489                 dev_err(&ndev->dev,
2490                         "%s: device_create_file failed\n", __func__);
2491                 goto err_req_irq_pend;
2492         }
2493         nvavp_info_ctx = nvavp;
2494
2495         /* Add PM QoS request but leave it as default value */
2496         pm_qos_add_request(&nvavp->min_cpu_freq_req,
2497                                 PM_QOS_CPU_FREQ_MIN,
2498                                 PM_QOS_DEFAULT_VALUE);
2499         pm_qos_add_request(&nvavp->min_online_cpus_req,
2500                                 PM_QOS_MIN_ONLINE_CPUS,
2501                                 PM_QOS_DEFAULT_VALUE);
2502
2503         return 0;
2504
2505 err_req_irq_pend:
2506 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2507         misc_deregister(&nvavp->audio_misc_dev);
2508 err_audio_misc_reg:
2509 #endif
2510         misc_deregister(&nvavp->video_misc_dev);
2511 err_misc_reg:
2512 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2513         clk_put(nvavp->vcp_clk);
2514 err_get_vcp_clk:
2515         clk_put(nvavp->bsea_clk);
2516 err_get_bsea_clk:
2517 #endif
2518         clk_put(nvavp->emc_clk);
2519 err_get_emc_clk:
2520         clk_put(nvavp->sclk);
2521 err_get_sclk:
2522         clk_put(nvavp->bsev_clk);
2523 err_get_bsev_clk:
2524         clk_put(nvavp->vde_clk);
2525 err_get_vde_clk:
2526         clk_put(nvavp->cop_clk);
2527 err_get_cop_clk:
2528 err_get_syncpt:
2529         kfree(nvavp);
2530         return ret;
2531 }
2532
2533 static int tegra_nvavp_remove(struct platform_device *ndev)
2534 {
2535         struct nvavp_info *nvavp = platform_get_drvdata(ndev);
2536
2537         if (!nvavp)
2538                 return 0;
2539
2540         mutex_lock(&nvavp->open_lock);
2541         if (nvavp->refcount) {
2542                 mutex_unlock(&nvavp->open_lock);
2543                 return -EBUSY;
2544         }
2545         mutex_unlock(&nvavp->open_lock);
2546
2547         nvavp_unload_ucode(nvavp);
2548         nvavp_unload_os(nvavp);
2549
2550         device_remove_file(&ndev->dev, &dev_attr_boost_sclk);
2551
2552         misc_deregister(&nvavp->video_misc_dev);
2553
2554 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2555         misc_deregister(&nvavp->audio_misc_dev);
2556         clk_put(nvavp->vcp_clk);
2557         clk_put(nvavp->bsea_clk);
2558 #endif
2559         clk_put(nvavp->bsev_clk);
2560         clk_put(nvavp->vde_clk);
2561         clk_put(nvavp->cop_clk);
2562
2563         clk_put(nvavp->emc_clk);
2564         clk_put(nvavp->sclk);
2565
2566         if (!IS_ERR_OR_NULL(&nvavp->min_cpu_freq_req)) {
2567                 pm_qos_update_request(&nvavp->min_cpu_freq_req,
2568                                 PM_QOS_CPU_FREQ_MIN_DEFAULT_VALUE);
2569                 pm_qos_remove_request(&nvavp->min_cpu_freq_req);
2570         }
2571         if (!IS_ERR_OR_NULL(&nvavp->min_online_cpus_req)) {
2572                 pm_qos_update_request(&nvavp->min_online_cpus_req,
2573                                 PM_QOS_CPU_FREQ_MIN_DEFAULT_VALUE);
2574                 pm_qos_remove_request(&nvavp->min_online_cpus_req);
2575         }
2576
2577         kfree(nvavp);
2578         return 0;
2579 }
2580
2581 #ifdef CONFIG_PM
2582 static int tegra_nvavp_runtime_suspend(struct device *dev)
2583 {
2584         struct platform_device *pdev = to_platform_device(dev);
2585         struct nvavp_info *nvavp = platform_get_drvdata(pdev);
2586         int ret = 0;
2587
2588         if (nvavp->refcount) {
2589                 if (!nvavp->clk_enabled) {
2590 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2591                         if (nvavp_check_idle(nvavp, NVAVP_AUDIO_CHANNEL))
2592                                 nvavp_uninit(nvavp);
2593                         else
2594                                 ret = -EBUSY;
2595 #else
2596                         nvavp_uninit(nvavp);
2597 #endif
2598                 }
2599                 else {
2600                         ret = -EBUSY;
2601                 }
2602         }
2603
2604         return ret;
2605 }
2606
2607 static int tegra_nvavp_runtime_resume(struct device *dev)
2608 {
2609         struct platform_device *pdev = to_platform_device(dev);
2610         struct nvavp_info *nvavp = platform_get_drvdata(pdev);
2611
2612         mutex_lock(&nvavp->open_lock);
2613
2614         if (nvavp->video_refcnt)
2615                 nvavp_init(nvavp, NVAVP_VIDEO_CHANNEL);
2616 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
2617         if (nvavp->audio_refcnt)
2618                 nvavp_init(nvavp, NVAVP_AUDIO_CHANNEL);
2619 #endif
2620
2621         mutex_unlock(&nvavp->open_lock);
2622
2623         return 0;
2624 }
2625
2626 static int tegra_nvavp_resume(struct device *dev)
2627 {
2628         struct platform_device *pdev = to_platform_device(dev);
2629         struct nvavp_info *nvavp = platform_get_drvdata(pdev);
2630
2631         /* To balance the unpowergate in suspend routine */
2632         nvavp_powergate_vde(nvavp);
2633
2634         tegra_nvavp_runtime_resume(dev);
2635
2636         return 0;
2637 }
2638
2639 static int tegra_nvavp_suspend(struct device *dev)
2640 {
2641         struct platform_device *pdev = to_platform_device(dev);
2642         struct nvavp_info *nvavp = platform_get_drvdata(pdev);
2643         int ret = 0;
2644
2645         mutex_lock(&nvavp->open_lock);
2646
2647         ret = tegra_nvavp_runtime_suspend(dev);
2648         if (ret) {
2649                 mutex_unlock(&nvavp->open_lock);
2650                 return ret;
2651         }
2652
2653         /* WAR: Leave partition vde on before suspend so that access
2654          * to BSEV registers immediatly after LP0 exit won't fail.
2655          */
2656         nvavp_unpowergate_vde(nvavp);
2657
2658         mutex_unlock(&nvavp->open_lock);
2659
2660         return 0;
2661 }
2662
2663 static const struct dev_pm_ops nvavp_pm_ops = {
2664         .runtime_suspend = tegra_nvavp_runtime_suspend,
2665         .runtime_resume = tegra_nvavp_runtime_resume,
2666         .suspend = tegra_nvavp_suspend,
2667         .resume = tegra_nvavp_resume,
2668 };
2669
2670 #define NVAVP_PM_OPS    (&nvavp_pm_ops)
2671
2672 #else /* CONFIG_PM */
2673
2674 #define NVAVP_PM_OPS    NULL
2675
2676 #endif /* CONFIG_PM */
2677
2678 static struct platform_driver tegra_nvavp_driver = {
2679         .driver = {
2680                 .name   = TEGRA_NVAVP_NAME,
2681                 .owner  = THIS_MODULE,
2682                 .pm     = NVAVP_PM_OPS,
2683                 .of_match_table = of_match_ptr(tegra_nvavp_of_match),
2684         },
2685         .probe          = tegra_nvavp_probe,
2686         .remove         = tegra_nvavp_remove,
2687 };
2688
2689 static int __init tegra_nvavp_init(void)
2690 {
2691         return platform_driver_register(&tegra_nvavp_driver);
2692 }
2693
2694 static void __exit tegra_nvavp_exit(void)
2695 {
2696         platform_driver_unregister(&tegra_nvavp_driver);
2697 }
2698
2699 module_init(tegra_nvavp_init);
2700 module_exit(tegra_nvavp_exit);
2701
2702 MODULE_AUTHOR("NVIDIA");
2703 MODULE_DESCRIPTION("Channel based AVP driver for Tegra");
2704 MODULE_VERSION("1.0");
2705 MODULE_LICENSE("Dual BSD/GPL");