cfe033249809c7935b2fd46c3f0da0a61c7f40a9
[linux-2.6.git] / drivers / media / video / tegra / nvavp / nvavp_dev.c
1 /*
2  * drivers/media/video/tegra/nvavp/nvavp_dev.c
3  *
4  * Copyright (C) 2011-2012 NVIDIA Corp.
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/completion.h>
14 #include <linux/delay.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/err.h>
17 #include <linux/firmware.h>
18 #include <linux/fs.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/ioctl.h>
22 #include <linux/irq.h>
23 #include <linux/kref.h>
24 #include <linux/list.h>
25 #include <linux/miscdevice.h>
26 #include <linux/mutex.h>
27 #include <linux/nvhost.h>
28 #include <linux/platform_device.h>
29 #include <linux/rbtree.h>
30 #include <linux/seq_file.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/tegra_nvavp.h>
34 #include <linux/types.h>
35 #include <linux/vmalloc.h>
36 #include <linux/workqueue.h>
37
38 #include <mach/clk.h>
39 #include <mach/hardware.h>
40 #include <mach/io.h>
41 #include <mach/iomap.h>
42 #include <mach/legacy_irq.h>
43 #include <linux/nvmap.h>
44
45 #include "../../../../video/tegra/host/host1x/host1x_syncpt.h"
46 #include "../../../../video/tegra/host/dev.h"
47 #include "../../../../video/tegra/host/nvhost_acm.h"
48
49 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU)
50 #include "../avp/headavp.h"
51 #endif
52 #include "nvavp_os.h"
53
54 #define TEGRA_NVAVP_NAME                        "nvavp"
55
56 #define NVAVP_PUSHBUFFER_SIZE                   4096
57
58 #define NVAVP_PUSHBUFFER_MIN_UPDATE_SPACE       (sizeof(u32) * 3)
59
60 #define TEGRA_NVAVP_RESET_VECTOR_ADDR   \
61                 (IO_ADDRESS(TEGRA_EXCEPTION_VECTORS_BASE) + 0x200)
62
63 #define FLOW_CTRL_HALT_COP_EVENTS       IO_ADDRESS(TEGRA_FLOW_CTRL_BASE + 0x4)
64 #define FLOW_MODE_STOP                  (0x2 << 29)
65 #define FLOW_MODE_NONE                  0x0
66
67 #define NVAVP_OS_INBOX                  IO_ADDRESS(TEGRA_RES_SEMA_BASE + 0x10)
68 #define NVAVP_OS_OUTBOX                 IO_ADDRESS(TEGRA_RES_SEMA_BASE + 0x20)
69
70 #define NVAVP_INBOX_VALID               (1 << 29)
71
72 /* AVP behavior params */
73 #define NVAVP_OS_IDLE_TIMEOUT           100 /* milli-seconds */
74
75 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
76 /* Two control channels: Audio and Video channels */
77 #define NVAVP_NUM_CHANNELS              2
78
79 #define NVAVP_AUDIO_CHANNEL             1
80
81 #define IS_AUDIO_CHANNEL_ID(channel_id) (channel_id == NVAVP_AUDIO_CHANNEL ? 1: 0)
82 #else
83 #define NVAVP_NUM_CHANNELS              1
84 #endif
85
86 /* Channel ID 0 represents the Video channel control area */
87 #define NVAVP_VIDEO_CHANNEL             0
88 /* Channel ID 1 represents the Audio channel control area */
89
90 #define IS_VIDEO_CHANNEL_ID(channel_id) (channel_id == NVAVP_VIDEO_CHANNEL ? 1: 0)
91
92
93 struct nvavp_channel {
94         struct mutex                    pushbuffer_lock;
95         struct nvmap_handle_ref         *pushbuf_handle;
96         unsigned long                   pushbuf_phys;
97         u8                              *pushbuf_data;
98         u32                             pushbuf_index;
99         u32                             pushbuf_fence;
100         struct nv_e276_control          *os_control;
101 };
102
103 struct nvavp_info {
104         u32                             clk_enabled;
105         struct clk                      *bsev_clk;
106         struct clk                      *vde_clk;
107         struct clk                      *cop_clk;
108
109         /* used for dvfs */
110         struct clk                      *sclk;
111         struct clk                      *emc_clk;
112         unsigned long                   sclk_rate;
113         unsigned long                   emc_clk_rate;
114
115         int                             mbox_from_avp_pend_irq;
116
117         struct mutex                    open_lock;
118         int                             refcount;
119         int                             video_initialized;
120 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
121         int                             audio_initialized;
122 #endif
123         struct work_struct              clock_disable_work;
124
125         /* os information */
126         struct nvavp_os_info            os_info;
127
128         /* ucode information */
129         struct nvavp_ucode_info         ucode_info;
130
131         /* client for driver allocations, persistent */
132         struct nvmap_client             *nvmap;
133
134         bool                            pending;
135
136         struct nvavp_channel            channel_info[NVAVP_NUM_CHANNELS];
137
138         struct nvhost_syncpt            *nvhost_syncpt;
139         u32                             syncpt_id;
140         u32                             syncpt_value;
141
142         struct nvhost_device            *nvhost_dev;
143         struct miscdevice               video_misc_dev;
144 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
145         struct miscdevice               audio_misc_dev;
146 #endif
147 };
148
149 struct nvavp_clientctx {
150         struct nvmap_client *nvmap;
151         struct nvavp_pushbuffer_submit_hdr submit_hdr;
152         struct nvavp_reloc relocs[NVAVP_MAX_RELOCATION_COUNT];
153         struct nvmap_handle_ref *gather_mem;
154         int num_relocs;
155         struct nvavp_info *nvavp;
156         u32 clk_reqs;
157         int channel_id;
158 };
159
160 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
161 static int nvavp_get_audio_init_status(struct nvavp_info *nvavp)
162 {
163         return nvavp->audio_initialized;
164 }
165
166 static void nvavp_set_audio_init_status(struct nvavp_info *nvavp, int status)
167 {
168         nvavp->audio_initialized = status;
169 }
170 #endif
171
172 static void nvavp_set_video_init_status(struct nvavp_info *nvavp, int status)
173 {
174         nvavp->video_initialized = status;
175 }
176
177 static int nvavp_get_video_init_status(struct nvavp_info *nvavp)
178 {
179         return nvavp->video_initialized;
180 }
181
182 static struct nvavp_channel *nvavp_get_channel_info(struct nvavp_info *nvavp, int channel_id)
183 {
184         return &nvavp->channel_info[channel_id];
185 }
186
187 static void nvavp_set_channel_control_area(struct nvavp_info *nvavp, int channel_id)
188 {
189         struct nv_e276_control *control;
190         struct nvavp_os_info *os = &nvavp->os_info;
191         u32 temp;
192         void *ptr;
193         struct nvavp_channel *channel_info;
194
195         ptr = os->data + os->control_offset + (sizeof(struct nv_e276_control) * channel_id);
196
197         channel_info = nvavp_get_channel_info(nvavp, channel_id);
198         channel_info->os_control = (struct nv_e276_control *)ptr;
199
200         control = channel_info->os_control;
201
202         /* init get and put pointers */
203         writel(0x0, &control->put);
204         writel(0x0, &control->get);
205
206         pr_debug("nvavp_set_channel_control_area for channel_id (%d):\
207                 control->put (0x%08x) control->get (0x%08x)\n",
208                 channel_id, (u32) &control->put, (u32) &control->get);
209
210         /* enable avp VDE clock control and disable iram clock gating */
211         writel(0x0, &control->idle_clk_enable);
212         writel(0x0, &control->iram_clk_gating);
213
214         /* enable avp idle timeout interrupt */
215         writel(0x1, &control->idle_notify_enable);
216         writel(NVAVP_OS_IDLE_TIMEOUT, &control->idle_notify_delay);
217
218         /* init dma start and end pointers */
219         writel(channel_info->pushbuf_phys, &control->dma_start);
220         writel((channel_info->pushbuf_phys + NVAVP_PUSHBUFFER_SIZE),
221                                                 &control->dma_end);
222
223         writel(0x00, &channel_info->pushbuf_index);
224         temp = NVAVP_PUSHBUFFER_SIZE - NVAVP_PUSHBUFFER_MIN_UPDATE_SPACE;
225         writel(temp, &channel_info->pushbuf_fence);
226 }
227
228 static struct clk *nvavp_clk_get(struct nvavp_info *nvavp, int id)
229 {
230         if (!nvavp)
231                 return NULL;
232
233         if (id == NVAVP_MODULE_ID_AVP)
234                 return nvavp->sclk;
235         if (id == NVAVP_MODULE_ID_VDE)
236                 return nvavp->vde_clk;
237         if (id == NVAVP_MODULE_ID_EMC)
238                 return nvavp->emc_clk;
239
240         return NULL;
241 }
242
243 static void nvavp_clks_enable(struct nvavp_info *nvavp)
244 {
245         if (nvavp->clk_enabled++ == 0) {
246                 nvhost_module_busy(nvhost_get_host(nvavp->nvhost_dev)->dev);
247                 clk_enable(nvavp->bsev_clk);
248                 clk_enable(nvavp->vde_clk);
249                 clk_set_rate(nvavp->emc_clk, nvavp->emc_clk_rate);
250                 clk_set_rate(nvavp->sclk, nvavp->sclk_rate);
251                 dev_dbg(&nvavp->nvhost_dev->dev, "%s: setting sclk to %lu\n",
252                                 __func__, nvavp->sclk_rate);
253                 dev_dbg(&nvavp->nvhost_dev->dev, "%s: setting emc_clk to %lu\n",
254                                 __func__, nvavp->emc_clk_rate);
255         }
256 }
257
258 static void nvavp_clks_disable(struct nvavp_info *nvavp)
259 {
260         if (--nvavp->clk_enabled == 0) {
261                 clk_disable(nvavp->bsev_clk);
262                 clk_disable(nvavp->vde_clk);
263                 clk_set_rate(nvavp->emc_clk, 0);
264                 clk_set_rate(nvavp->sclk, 0);
265                 nvhost_module_idle(nvhost_get_host(nvavp->nvhost_dev)->dev);
266                 dev_dbg(&nvavp->nvhost_dev->dev, "%s: resetting emc_clk "
267                                 "and sclk\n", __func__);
268         }
269 }
270
271 static u32 nvavp_check_idle(struct nvavp_info *nvavp)
272 {
273         struct nvavp_channel *channel_info = nvavp_get_channel_info(nvavp, NVAVP_VIDEO_CHANNEL);
274         struct nv_e276_control *control = channel_info->os_control;
275
276         return (control->put == control->get) ? 1 : 0;
277 }
278
279 static void clock_disable_handler(struct work_struct *work)
280 {
281         struct nvavp_info *nvavp;
282         struct nvavp_channel *channel_info;
283
284         nvavp = container_of(work, struct nvavp_info,
285                             clock_disable_work);
286         channel_info = nvavp_get_channel_info(nvavp, NVAVP_VIDEO_CHANNEL);
287
288         mutex_lock(&channel_info->pushbuffer_lock);
289         mutex_lock(&nvavp->open_lock);
290         if (nvavp_check_idle(nvavp) && nvavp->pending) {
291                 nvavp->pending = false;
292                 nvavp_clks_disable(nvavp);
293         }
294         mutex_unlock(&nvavp->open_lock);
295         mutex_unlock(&channel_info->pushbuffer_lock);
296 }
297
298 static int nvavp_service(struct nvavp_info *nvavp)
299 {
300         struct nvavp_os_info *os = &nvavp->os_info;
301         u8 *debug_print;
302         u32 inbox;
303
304         inbox = readl(NVAVP_OS_INBOX);
305         if (!(inbox & NVAVP_INBOX_VALID))
306                 inbox = 0x00000000;
307
308         writel(0x00000000, NVAVP_OS_INBOX);
309
310         if (inbox & NVE276_OS_INTERRUPT_VIDEO_IDLE)
311                 schedule_work(&nvavp->clock_disable_work);
312
313 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
314         if (inbox & NVE276_OS_INTERRUPT_AUDIO_IDLE)
315                 pr_debug("nvavp_service NVE276_OS_INTERRUPT_AUDIO_IDLE\n");
316 #endif
317         if (inbox & NVE276_OS_INTERRUPT_DEBUG_STRING) {
318                 /* Should only occur with debug AVP OS builds */
319                 debug_print = os->data;
320                 debug_print += os->debug_offset;
321                 dev_info(&nvavp->nvhost_dev->dev, "%s\n", debug_print);
322         }
323         if (inbox & (NVE276_OS_INTERRUPT_SEMAPHORE_AWAKEN |
324                      NVE276_OS_INTERRUPT_EXECUTE_AWAKEN)) {
325                 dev_info(&nvavp->nvhost_dev->dev,
326                         "AVP awaken event (0x%x)\n", inbox);
327         }
328         if (inbox & NVE276_OS_INTERRUPT_AVP_FATAL_ERROR) {
329                 dev_err(&nvavp->nvhost_dev->dev,
330                         "fatal AVP error (0x%08X)\n", inbox);
331         }
332         if (inbox & NVE276_OS_INTERRUPT_AVP_BREAKPOINT)
333                 dev_err(&nvavp->nvhost_dev->dev, "AVP breakpoint hit\n");
334         if (inbox & NVE276_OS_INTERRUPT_TIMEOUT)
335                 dev_err(&nvavp->nvhost_dev->dev, "AVP timeout\n");
336
337         return 0;
338 }
339
340 static irqreturn_t nvavp_mbox_pending_isr(int irq, void *data)
341 {
342         struct nvavp_info *nvavp = data;
343
344         nvavp_service(nvavp);
345
346         return IRQ_HANDLED;
347 }
348
349 static void nvavp_halt_avp(struct nvavp_info *nvavp)
350 {
351         /* ensure the AVP is halted */
352         writel(FLOW_MODE_STOP, FLOW_CTRL_HALT_COP_EVENTS);
353         tegra_periph_reset_assert(nvavp->cop_clk);
354
355         writel(0, NVAVP_OS_OUTBOX);
356         writel(0, NVAVP_OS_INBOX);
357 }
358
359 static int nvavp_reset_avp(struct nvavp_info *nvavp, unsigned long reset_addr)
360 {
361 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU)
362         unsigned long stub_code_phys = virt_to_phys(_tegra_avp_boot_stub);
363         dma_addr_t stub_data_phys;
364
365         _tegra_avp_boot_stub_data.map_phys_addr = avp->kernel_phys;
366         _tegra_avp_boot_stub_data.jump_addr = reset_addr;
367         wmb();
368         stub_data_phys = dma_map_single(NULL, &_tegra_avp_boot_stub_data,
369                                         sizeof(_tegra_avp_boot_stub_data),
370                                         DMA_TO_DEVICE);
371         rmb();
372         reset_addr = (unsigned long)stub_data_phys;
373 #endif
374         writel(FLOW_MODE_STOP, FLOW_CTRL_HALT_COP_EVENTS);
375
376         writel(reset_addr, TEGRA_NVAVP_RESET_VECTOR_ADDR);
377
378         clk_enable(nvavp->sclk);
379         clk_enable(nvavp->emc_clk);
380
381         /* If sclk_rate and emc_clk is not set by user space,
382          * max clock in dvfs table will be used to get best performance.
383          */
384         nvavp->sclk_rate = ULONG_MAX;
385         nvavp->emc_clk_rate = ULONG_MAX;
386
387         tegra_periph_reset_assert(nvavp->cop_clk);
388         udelay(2);
389         tegra_periph_reset_deassert(nvavp->cop_clk);
390
391         writel(FLOW_MODE_NONE, FLOW_CTRL_HALT_COP_EVENTS);
392
393 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU)
394         dma_unmap_single(NULL, stub_data_phys,
395                          sizeof(_tegra_avp_boot_stub_data),
396                          DMA_TO_DEVICE);
397 #endif
398         return 0;
399 }
400
401 static void nvavp_halt_vde(struct nvavp_info *nvavp)
402 {
403         if (nvavp->clk_enabled && !nvavp->pending)
404                 BUG();
405
406         if (nvavp->pending) {
407                 nvavp_clks_disable(nvavp);
408                 nvavp->pending = false;
409         }
410
411         tegra_periph_reset_assert(nvavp->bsev_clk);
412         tegra_periph_reset_assert(nvavp->vde_clk);
413 }
414
415 static int nvavp_reset_vde(struct nvavp_info *nvavp)
416 {
417         if (nvavp->clk_enabled)
418                 BUG();
419
420         nvavp_clks_enable(nvavp);
421
422         tegra_periph_reset_assert(nvavp->bsev_clk);
423         udelay(2);
424         tegra_periph_reset_deassert(nvavp->bsev_clk);
425
426         tegra_periph_reset_assert(nvavp->vde_clk);
427         udelay(2);
428         tegra_periph_reset_deassert(nvavp->vde_clk);
429
430         /*
431          * VDE clock is set to max freq by default.
432          * VDE clock can be set to different freq if needed
433          * through ioctl.
434          */
435         clk_set_rate(nvavp->vde_clk, ULONG_MAX);
436
437         nvavp_clks_disable(nvavp);
438
439         return 0;
440 }
441
442 static int nvavp_pushbuffer_alloc(struct nvavp_info *nvavp, int channel_id)
443 {
444         int ret = 0;
445
446         struct nvavp_channel *channel_info = nvavp_get_channel_info(
447                                                         nvavp, channel_id);
448
449         channel_info->pushbuf_handle = nvmap_alloc(nvavp->nvmap,
450                                                 NVAVP_PUSHBUFFER_SIZE,
451                                                 SZ_1M, NVMAP_HANDLE_UNCACHEABLE,
452                                                 0);
453         if (IS_ERR(channel_info->pushbuf_handle)) {
454                 dev_err(&nvavp->nvhost_dev->dev,
455                         "cannot create pushbuffer handle\n");
456                 ret = PTR_ERR(channel_info->pushbuf_handle);
457                 goto err_pushbuf_alloc;
458         }
459         channel_info->pushbuf_data = (u8 *)nvmap_mmap(
460                                                 channel_info->pushbuf_handle);
461
462         if (!channel_info->pushbuf_data) {
463                 dev_err(&nvavp->nvhost_dev->dev,
464                         "cannot map pushbuffer handle\n");
465                 ret = -ENOMEM;
466                 goto err_pushbuf_mmap;
467         }
468         channel_info->pushbuf_phys = nvmap_pin(nvavp->nvmap,
469                                         channel_info->pushbuf_handle);
470         if (IS_ERR((void *)channel_info->pushbuf_phys)) {
471                 dev_err(&nvavp->nvhost_dev->dev,
472                         "cannot pin pushbuffer handle\n");
473                 ret = PTR_ERR((void *)channel_info->pushbuf_phys);
474                 goto err_pushbuf_pin;
475         }
476
477         memset(channel_info->pushbuf_data, 0, NVAVP_PUSHBUFFER_SIZE);
478
479         return 0;
480
481 err_pushbuf_pin:
482         nvmap_munmap(channel_info->pushbuf_handle, channel_info->pushbuf_data);
483 err_pushbuf_mmap:
484         nvmap_free(nvavp->nvmap, channel_info->pushbuf_handle);
485 err_pushbuf_alloc:
486         return ret;
487 }
488
489 static void nvavp_pushbuffer_free(struct nvavp_info *nvavp)
490 {
491         int channel_id;
492
493         for (channel_id = 0; channel_id < NVAVP_NUM_CHANNELS; channel_id++) {
494                 if (nvavp->channel_info[channel_id].pushbuf_data) {
495                         nvmap_unpin(nvavp->nvmap,
496                                 nvavp->channel_info[channel_id].pushbuf_handle);
497                         nvmap_munmap(
498                                 nvavp->channel_info[channel_id].pushbuf_handle,
499                                 nvavp->channel_info[channel_id].pushbuf_data);
500                         nvmap_free(nvavp->nvmap,
501                                 nvavp->channel_info[channel_id].pushbuf_handle);
502                 }
503         }
504 }
505
506
507 static int nvavp_pushbuffer_init(struct nvavp_info *nvavp)
508 {
509         int ret, channel_id;
510
511         for (channel_id = 0; channel_id < NVAVP_NUM_CHANNELS; channel_id++) {
512                 ret = nvavp_pushbuffer_alloc(nvavp, channel_id);
513                 if (ret) {
514                         dev_err(&nvavp->nvhost_dev->dev,
515                                 "unable to alloc pushbuffer\n");
516                         return ret;
517                 }
518                 nvavp_set_channel_control_area(nvavp, channel_id);
519                 if (IS_VIDEO_CHANNEL_ID(channel_id)) {
520                         nvavp->syncpt_id = NVSYNCPT_AVP_0;
521                         nvavp->syncpt_value = nvhost_syncpt_read(
522                                                         nvavp->nvhost_syncpt,
523                                                         nvavp->syncpt_id);
524                 }
525
526         }
527         return 0;
528 }
529
530 static void nvavp_pushbuffer_deinit(struct nvavp_info *nvavp)
531 {
532         nvavp_pushbuffer_free(nvavp);
533 }
534
535 static int nvavp_pushbuffer_update(struct nvavp_info *nvavp, u32 phys_addr,
536                         u32 gather_count, struct nvavp_syncpt *syncpt,
537                         u32 ext_ucode_flag, int channel_id)
538 {
539         struct nvavp_channel  *channel_info;
540         struct nv_e276_control *control;
541         u32 gather_cmd, setucode_cmd, sync = 0;
542         u32 wordcount = 0;
543         u32 index, value = -1;
544
545         channel_info = nvavp_get_channel_info(nvavp, channel_id);
546
547         control = channel_info->os_control;
548         pr_debug("nvavp_pushbuffer_update for channel_id (%d):\
549                 control->put (0x%x) control->get (0x%x)\n",
550                 channel_id, (u32) &control->put, (u32) &control->get);
551
552         mutex_lock(&channel_info->pushbuffer_lock);
553
554         /* check for pushbuffer wrapping */
555         if (channel_info->pushbuf_index >= channel_info->pushbuf_fence)
556                 channel_info->pushbuf_index = 0;
557
558         if (!ext_ucode_flag) {
559                 setucode_cmd =
560                         NVE26E_CH_OPCODE_INCR(NVE276_SET_MICROCODE_A, 3);
561
562                 index = wordcount + channel_info->pushbuf_index;
563                 writel(setucode_cmd, (channel_info->pushbuf_data + index));
564                 wordcount += sizeof(u32);
565
566                 index = wordcount + channel_info->pushbuf_index;
567                 writel(0, (channel_info->pushbuf_data + index));
568                 wordcount += sizeof(u32);
569
570                 index = wordcount + channel_info->pushbuf_index;
571                 writel(nvavp->ucode_info.phys,
572                         (channel_info->pushbuf_data + index));
573                 wordcount += sizeof(u32);
574
575                 index = wordcount + channel_info->pushbuf_index;
576                 writel(nvavp->ucode_info.size,
577                         (channel_info->pushbuf_data + index));
578                 wordcount += sizeof(u32);
579         }
580
581         gather_cmd = NVE26E_CH_OPCODE_GATHER(0, 0, 0, gather_count);
582
583         if (syncpt) {
584                 value = ++nvavp->syncpt_value;
585                 /* XXX: NvSchedValueWrappingComparison */
586                 sync = NVE26E_CH_OPCODE_IMM(NVE26E_HOST1X_INCR_SYNCPT,
587                         (NVE26E_HOST1X_INCR_SYNCPT_COND_OP_DONE << 8) |
588                         (nvavp->syncpt_id & 0xFF));
589         }
590
591         /* write commands out */
592         index = wordcount + channel_info->pushbuf_index;
593         writel(gather_cmd, (channel_info->pushbuf_data + index));
594         wordcount += sizeof(u32);
595
596         index = wordcount + channel_info->pushbuf_index;
597         writel(phys_addr, (channel_info->pushbuf_data + index));
598         wordcount += sizeof(u32);
599
600         if (syncpt) {
601                 index = wordcount + channel_info->pushbuf_index;
602                 writel(sync, (channel_info->pushbuf_data + index));
603                 wordcount += sizeof(u32);
604         }
605
606         /* enable clocks to VDE/BSEV */
607         if (IS_VIDEO_CHANNEL_ID(channel_id)) {
608                 mutex_lock(&nvavp->open_lock);
609                 if (!nvavp->pending) {
610                         nvavp_clks_enable(nvavp);
611                         nvavp->pending = true;
612                 }
613                 mutex_unlock(&nvavp->open_lock);
614         }
615
616         /* update put pointer */
617         channel_info->pushbuf_index = (channel_info->pushbuf_index + wordcount)&
618                                         (NVAVP_PUSHBUFFER_SIZE - 1);
619
620         writel(channel_info->pushbuf_index, &control->put);
621         wmb();
622
623         /* wake up avp */
624
625         if (IS_VIDEO_CHANNEL_ID(channel_id)) {
626                 pr_debug("Wake up Video Channel\n");
627                 writel(0xA0000001, NVAVP_OS_OUTBOX);
628         }
629         else {
630 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
631                 if (IS_AUDIO_CHANNEL_ID(channel_id)) {
632                         pr_debug("Wake up Audio Channel\n");
633                         writel(0xA0000002, NVAVP_OS_OUTBOX);
634                 }
635 #endif
636         }
637         /* Fill out fence struct */
638         if (syncpt) {
639                 syncpt->id = nvavp->syncpt_id;
640                 syncpt->value = value;
641         }
642
643         mutex_unlock(&channel_info->pushbuffer_lock);
644
645         return 0;
646 }
647
648 static void nvavp_unload_ucode(struct nvavp_info *nvavp)
649 {
650         nvmap_unpin(nvavp->nvmap, nvavp->ucode_info.handle);
651         nvmap_munmap(nvavp->ucode_info.handle, nvavp->ucode_info.data);
652         nvmap_free(nvavp->nvmap, nvavp->ucode_info.handle);
653         kfree(nvavp->ucode_info.ucode_bin);
654 }
655
656 static int nvavp_load_ucode(struct nvavp_info *nvavp)
657 {
658         struct nvavp_ucode_info *ucode_info = &nvavp->ucode_info;
659         const struct firmware *nvavp_ucode_fw;
660         char fw_ucode_file[32];
661         void *ptr;
662         int ret = 0;
663
664         if (!ucode_info->ucode_bin) {
665                 sprintf(fw_ucode_file, "nvavp_vid_ucode.bin");
666
667                 ret = request_firmware(&nvavp_ucode_fw, fw_ucode_file,
668                                         nvavp->video_misc_dev.this_device);
669                 if (ret) {
670                         /* Try alternative version */
671                         sprintf(fw_ucode_file, "nvavp_vid_ucode_alt.bin");
672
673                         ret = request_firmware(&nvavp_ucode_fw,
674                                                 fw_ucode_file,
675                                                 nvavp->video_misc_dev.this_device);
676
677                         if (ret) {
678                                 dev_err(&nvavp->nvhost_dev->dev,
679                                         "cannot read ucode firmware '%s'\n",
680                                         fw_ucode_file);
681                                 goto err_req_ucode;
682                         }
683                 }
684
685                 dev_info(&nvavp->nvhost_dev->dev,
686                         "read ucode firmware from '%s' (%d bytes)\n",
687                         fw_ucode_file, nvavp_ucode_fw->size);
688
689                 ptr = (void *)nvavp_ucode_fw->data;
690
691                 if (strncmp((const char *)ptr, "NVAVPAPP", 8)) {
692                         dev_info(&nvavp->nvhost_dev->dev,
693                                 "ucode hdr string mismatch\n");
694                         ret = -EINVAL;
695                         goto err_req_ucode;
696                 }
697                 ptr += 8;
698                 ucode_info->size = nvavp_ucode_fw->size - 8;
699
700                 ucode_info->ucode_bin = kzalloc(ucode_info->size,
701                                                 GFP_KERNEL);
702                 if (!ucode_info->ucode_bin) {
703                         dev_err(&nvavp->nvhost_dev->dev,
704                                 "cannot allocate ucode bin\n");
705                         ret = -ENOMEM;
706                         goto err_ubin_alloc;
707                 }
708
709                 ucode_info->handle = nvmap_alloc(nvavp->nvmap,
710                                                 nvavp->ucode_info.size,
711                                         SZ_1M, NVMAP_HANDLE_UNCACHEABLE, 0);
712                 if (IS_ERR(ucode_info->handle)) {
713                         dev_err(&nvavp->nvhost_dev->dev,
714                                 "cannot create ucode handle\n");
715                         ret = PTR_ERR(ucode_info->handle);
716                         goto err_ucode_alloc;
717                 }
718                 ucode_info->data = (u8 *)nvmap_mmap(ucode_info->handle);
719                 if (!ucode_info->data) {
720                         dev_err(&nvavp->nvhost_dev->dev,
721                                 "cannot map ucode handle\n");
722                         ret = -ENOMEM;
723                         goto err_ucode_mmap;
724                 }
725                 ucode_info->phys = nvmap_pin(nvavp->nvmap, ucode_info->handle);
726                 if (IS_ERR((void *)ucode_info->phys)) {
727                         dev_err(&nvavp->nvhost_dev->dev,
728                                 "cannot pin ucode handle\n");
729                         ret = PTR_ERR((void *)ucode_info->phys);
730                         goto err_ucode_pin;
731                 }
732                 memcpy(ucode_info->ucode_bin, ptr, ucode_info->size);
733                 release_firmware(nvavp_ucode_fw);
734         }
735
736         memcpy(ucode_info->data, ucode_info->ucode_bin, ucode_info->size);
737         return 0;
738
739 err_ucode_pin:
740         nvmap_munmap(ucode_info->handle, ucode_info->data);
741 err_ucode_mmap:
742         nvmap_free(nvavp->nvmap, ucode_info->handle);
743 err_ucode_alloc:
744         kfree(nvavp->ucode_info.ucode_bin);
745 err_ubin_alloc:
746         release_firmware(nvavp_ucode_fw);
747 err_req_ucode:
748         return ret;
749 }
750
751 static void nvavp_unload_os(struct nvavp_info *nvavp)
752 {
753         nvmap_unpin(nvavp->nvmap, nvavp->os_info.handle);
754         nvmap_munmap(nvavp->os_info.handle, nvavp->os_info.data);
755 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU)
756         nvmap_free(nvavp->nvmap, nvavp->os_info.handle);
757 #elif defined(CONFIG_TEGRA_AVP_KERNEL_ON_SMMU)
758         nvmap_free_iovm(nvavp->nvmap, nvavp->os_info.handle);
759 #endif
760         kfree(nvavp->os_info.os_bin);
761 }
762
763 static int nvavp_load_os(struct nvavp_info *nvavp, char *fw_os_file)
764 {
765         struct nvavp_os_info *os_info = &nvavp->os_info;
766         const struct firmware *nvavp_os_fw;
767         void *ptr;
768         u32 size;
769         int ret = 0;
770
771         if (!os_info->os_bin) {
772                 ret = request_firmware(&nvavp_os_fw, fw_os_file,
773                                         nvavp->video_misc_dev.this_device);
774                 if (ret) {
775                         dev_err(&nvavp->nvhost_dev->dev,
776                                 "cannot read os firmware '%s'\n", fw_os_file);
777                         goto err_req_fw;
778                 }
779
780                 dev_info(&nvavp->nvhost_dev->dev,
781                         "read firmware from '%s' (%d bytes)\n",
782                         fw_os_file, nvavp_os_fw->size);
783
784                 ptr = (void *)nvavp_os_fw->data;
785
786                 if (strncmp((const char *)ptr, "NVAVP-OS", 8)) {
787                         dev_info(&nvavp->nvhost_dev->dev,
788                                 "os hdr string mismatch\n");
789                         ret = -EINVAL;
790                         goto err_os_bin;
791                 }
792
793                 ptr += 8;
794                 os_info->entry_offset = *((u32 *)ptr);
795                 ptr += sizeof(u32);
796                 os_info->control_offset = *((u32 *)ptr);
797                 ptr += sizeof(u32);
798                 os_info->debug_offset = *((u32 *)ptr);
799                 ptr += sizeof(u32);
800
801                 size = *((u32 *)ptr);    ptr += sizeof(u32);
802
803                 os_info->size = size;
804                 os_info->os_bin = kzalloc(os_info->size,
805                                                 GFP_KERNEL);
806                 if (!os_info->os_bin) {
807                         dev_err(&nvavp->nvhost_dev->dev,
808                                 "cannot allocate os bin\n");
809                         ret = -ENOMEM;
810                         goto err_os_bin;
811                 }
812
813                 memcpy(os_info->os_bin, ptr, os_info->size);
814                 memset(os_info->data + os_info->size, 0, SZ_1M - os_info->size);
815
816                 dev_info(&nvavp->nvhost_dev->dev,
817                         "entry=%08x control=%08x debug=%08x size=%d\n",
818                         os_info->entry_offset, os_info->control_offset,
819                         os_info->debug_offset, os_info->size);
820                 release_firmware(nvavp_os_fw);
821         }
822
823         memcpy(os_info->data, os_info->os_bin, os_info->size);
824         os_info->reset_addr = os_info->phys + os_info->entry_offset;
825
826         dev_info(&nvavp->nvhost_dev->dev,
827                 "AVP os at vaddr=%p paddr=%lx reset_addr=%p\n",
828                 os_info->data, (unsigned long)(os_info->phys),
829                                 (void *)os_info->reset_addr);
830         return 0;
831
832 err_os_bin:
833         release_firmware(nvavp_os_fw);
834 err_req_fw:
835         return ret;
836 }
837
838
839 static int nvavp_os_init(struct nvavp_info *nvavp)
840 {
841         char fw_os_file[32];
842         int ret = 0;
843         int video_initialized, audio_initialized = 0;
844
845         video_initialized = nvavp_get_video_init_status(nvavp);
846
847 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
848         audio_initialized = nvavp_get_audio_init_status(nvavp);
849 #endif
850         pr_debug("video_initialized(%d) audio_initialized(%d)\n",
851                 video_initialized, audio_initialized);
852
853         /* Video and Audio both are initialized */
854         if (video_initialized || audio_initialized)
855                 return ret;
856
857         /* Video or Audio both are uninitialized */
858         pr_debug("video_initialized == audio_initialized (%d)\n",
859                 nvavp->video_initialized);
860 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU) /* Tegra2 with AVP MMU */
861         /* paddr is any address returned from nvmap_pin */
862         /* vaddr is AVP_KERNEL_VIRT_BASE */
863         dev_info(&nvavp->nvhost_dev->dev,
864                 "using AVP MMU to relocate AVP os\n");
865         sprintf(fw_os_file, "nvavp_os.bin");
866         nvavp->os_info.reset_addr = AVP_KERNEL_VIRT_BASE;
867 #elif defined(CONFIG_TEGRA_AVP_KERNEL_ON_SMMU) /* Tegra3 with SMMU */
868         /* paddr is any address behind SMMU */
869         /* vaddr is TEGRA_SMMU_BASE */
870         dev_info(&nvavp->nvhost_dev->dev,
871                 "using SMMU at %lx to load AVP kernel\n",
872                 (unsigned long)nvavp->os_info.phys);
873         BUG_ON(nvavp->os_info.phys != 0xeff00000
874                 && nvavp->os_info.phys != 0x0ff00000);
875         sprintf(fw_os_file, "nvavp_os_%08lx.bin",
876                 (unsigned long)nvavp->os_info.phys);
877         nvavp->os_info.reset_addr = nvavp->os_info.phys;
878 #else /* nvmem= carveout */
879         /* paddr is found in nvmem= carveout */
880         /* vaddr is same as paddr */
881         /* Find nvmem carveout */
882         if (!pfn_valid(__phys_to_pfn(0x8e000000))) {
883                 nvavp->os_info.phys = 0x8e000000;
884         } else if (!pfn_valid(__phys_to_pfn(0x9e000000))) {
885                 nvavp->os_info.phys = 0x9e000000;
886         } else if (!pfn_valid(__phys_to_pfn(0xbe000000))) {
887                 nvavp->os_info.phys = 0xbe000000;
888         } else {
889                 dev_err(&nvavp->nvhost_dev->dev,
890                         "cannot find nvmem= carveout to load AVP os\n");
891                 dev_err(&nvavp->nvhost_dev->dev,
892                         "check kernel command line "
893                         "to see if nvmem= is defined\n");
894                 BUG();
895         }
896         dev_info(&nvavp->nvhost_dev->dev,
897                 "using nvmem= carveout at %lx to load AVP os\n",
898                 nvavp->os_info.phys);
899         sprintf(fw_os_file, "nvavp_os_%08lx.bin", nvavp->os_info.phys);
900         nvavp->os_info.reset_addr = nvavp->os_info.phys;
901         nvavp->os_info.data = ioremap(nvavp->os_info.phys, SZ_1M);
902 #endif
903         ret = nvavp_load_os(nvavp, fw_os_file);
904         if (ret) {
905                 dev_err(&nvavp->nvhost_dev->dev,
906                         "unable to load os firmware '%s'\n", fw_os_file);
907                 goto err_exit;
908         }
909
910         ret = nvavp_pushbuffer_init(nvavp);
911         if (ret) {
912                 dev_err(&nvavp->nvhost_dev->dev,
913                         "unable to init pushbuffer\n");
914                 goto err_exit;
915         }
916         tegra_init_legacy_irq_cop();
917         enable_irq(nvavp->mbox_from_avp_pend_irq);
918 err_exit:
919         return ret;
920 }
921
922 static int nvavp_init(struct nvavp_info *nvavp, int channel_id)
923 {
924         int ret = 0;
925
926         ret = nvavp_os_init(nvavp);
927         if (ret) {
928                 dev_err(&nvavp->nvhost_dev->dev,
929                         "unable to load os firmware and allocate buffers\n");
930         }
931
932         if (IS_VIDEO_CHANNEL_ID(channel_id) &&
933                 (!nvavp_get_video_init_status(nvavp)) ) {
934                 pr_debug("nvavp_init : channel_ID (%d)\n", channel_id);
935                 ret = nvavp_load_ucode(nvavp);
936                 if (ret) {
937                         dev_err(&nvavp->nvhost_dev->dev,
938                                 "unable to load ucode\n");
939                         goto err_exit;
940                 }
941
942                 nvavp_reset_vde(nvavp);
943                 nvavp_reset_avp(nvavp, nvavp->os_info.reset_addr);
944
945                 nvavp_set_video_init_status(nvavp, 1);
946         }
947 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
948         if (IS_AUDIO_CHANNEL_ID(channel_id) &&
949                 (!nvavp_get_audio_init_status(nvavp))) {
950                 pr_debug("nvavp_init : channel_ID (%d)\n", channel_id);
951                 nvavp_reset_avp(nvavp, nvavp->os_info.reset_addr);
952                 nvavp_set_audio_init_status(nvavp, 1);
953         }
954 #endif
955
956 err_exit:
957         return ret;
958 }
959
960 static void nvavp_uninit(struct nvavp_info *nvavp)
961 {
962         int video_initialized, audio_initialized = 0;
963
964         video_initialized = nvavp_get_video_init_status(nvavp);
965
966 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
967         audio_initialized = nvavp_get_audio_init_status(nvavp);
968 #endif
969
970         pr_debug("nvavp_uninit video_initialized(%d) audio_initialized(%d)\n",
971                 video_initialized, audio_initialized);
972
973         /* Video and Audio both are uninitialized */
974         if (!video_initialized && !audio_initialized)
975                 return;
976
977         if (video_initialized) {
978                 pr_debug("nvavp_uninit nvavp->video_initialized\n");
979                 cancel_work_sync(&nvavp->clock_disable_work);
980
981                 nvavp_halt_vde(nvavp);
982
983                 clk_disable(nvavp->sclk);
984                 clk_disable(nvavp->emc_clk);
985
986                 nvavp_set_video_init_status(nvavp, 0);
987                 video_initialized = 0;
988         }
989
990 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
991         if (audio_initialized) {
992                 nvavp_set_audio_init_status(nvavp, 0);
993                 audio_initialized = 0;
994         }
995 #endif
996
997         /* Video and Audio both becomes uninitialized */
998         if (video_initialized == audio_initialized) {
999                 pr_debug("nvavp_uninit both channels unitialized\n");
1000                 disable_irq(nvavp->mbox_from_avp_pend_irq);
1001                 nvavp_pushbuffer_deinit(nvavp);
1002                 nvavp_halt_avp(nvavp);
1003         }
1004 }
1005
1006 static int nvavp_set_clock_ioctl(struct file *filp, unsigned int cmd,
1007                                                         unsigned long arg)
1008 {
1009         struct nvavp_clientctx *clientctx = filp->private_data;
1010         struct nvavp_info *nvavp = clientctx->nvavp;
1011         struct clk *c;
1012         struct nvavp_clock_args config;
1013
1014         if (copy_from_user(&config, (void __user *)arg, sizeof(struct nvavp_clock_args)))
1015                 return -EFAULT;
1016
1017         dev_dbg(&nvavp->nvhost_dev->dev, "%s: clk_id=%d, clk_rate=%u\n",
1018                         __func__, config.id, config.rate);
1019
1020         if (config.id == NVAVP_MODULE_ID_AVP)
1021                 nvavp->sclk_rate = config.rate;
1022         else if (config.id == NVAVP_MODULE_ID_EMC)
1023                 nvavp->emc_clk_rate = config.rate;
1024
1025         c = nvavp_clk_get(nvavp, config.id);
1026         if (IS_ERR_OR_NULL(c))
1027                 return -EINVAL;
1028
1029         clk_enable(c);
1030         clk_set_rate(c, config.rate);
1031
1032         config.rate = clk_get_rate(c);
1033         clk_disable(c);
1034         if (copy_to_user((void __user *)arg, &config, sizeof(struct nvavp_clock_args)))
1035                 return -EFAULT;
1036
1037         return 0;
1038 }
1039
1040 static int nvavp_get_clock_ioctl(struct file *filp, unsigned int cmd,
1041                                                         unsigned long arg)
1042 {
1043         struct nvavp_clientctx *clientctx = filp->private_data;
1044         struct nvavp_info *nvavp = clientctx->nvavp;
1045         struct clk *c;
1046         struct nvavp_clock_args config;
1047
1048         if (copy_from_user(&config, (void __user *)arg, sizeof(struct nvavp_clock_args)))
1049                 return -EFAULT;
1050
1051         c = nvavp_clk_get(nvavp, config.id);
1052         if (IS_ERR_OR_NULL(c))
1053                 return -EINVAL;
1054
1055         clk_enable(c);
1056         config.rate = clk_get_rate(c);
1057         clk_disable(c);
1058
1059         if (copy_to_user((void __user *)arg, &config, sizeof(struct nvavp_clock_args)))
1060                 return -EFAULT;
1061
1062         return 0;
1063 }
1064
1065 static int nvavp_get_syncpointid_ioctl(struct file *filp, unsigned int cmd,
1066                                                         unsigned long arg)
1067 {
1068         struct nvavp_clientctx *clientctx = filp->private_data;
1069         struct nvavp_info *nvavp = clientctx->nvavp;
1070         u32 id = nvavp->syncpt_id;
1071
1072         if (_IOC_DIR(cmd) & _IOC_READ) {
1073                 if (copy_to_user((void __user *)arg, &id, sizeof(u32)))
1074                         return -EFAULT;
1075                 else
1076                         return 0;
1077         }
1078         return -EFAULT;
1079 }
1080
1081 static int nvavp_set_nvmapfd_ioctl(struct file *filp, unsigned int cmd,
1082                                                         unsigned long arg)
1083 {
1084         struct nvavp_clientctx *clientctx = filp->private_data;
1085         struct nvavp_set_nvmap_fd_args buf;
1086         struct nvmap_client *new_client;
1087         int fd;
1088
1089         if (_IOC_DIR(cmd) & _IOC_WRITE) {
1090                 if (copy_from_user(&buf, (void __user *)arg, _IOC_SIZE(cmd)))
1091                         return -EFAULT;
1092         }
1093
1094         fd = buf.fd;
1095         new_client = nvmap_client_get_file(fd);
1096         if (IS_ERR(new_client))
1097                 return PTR_ERR(new_client);
1098
1099         clientctx->nvmap = new_client;
1100         return 0;
1101 }
1102
1103 static int nvavp_pushbuffer_submit_ioctl(struct file *filp, unsigned int cmd,
1104                                                         unsigned long arg)
1105 {
1106         struct nvavp_clientctx *clientctx = filp->private_data;
1107         struct nvavp_info *nvavp = clientctx->nvavp;
1108         struct nvavp_pushbuffer_submit_hdr hdr;
1109         u32 *cmdbuf_data;
1110         struct nvmap_handle *cmdbuf_handle = NULL;
1111         struct nvmap_handle_ref *cmdbuf_dupe;
1112         int ret = 0, i;
1113         unsigned long phys_addr;
1114         unsigned long virt_addr;
1115         struct nvavp_pushbuffer_submit_hdr *user_hdr =
1116                         (struct nvavp_pushbuffer_submit_hdr *) arg;
1117         struct nvavp_syncpt syncpt;
1118
1119         syncpt.id = NVSYNCPT_INVALID;
1120         syncpt.value = 0;
1121
1122         if (_IOC_DIR(cmd) & _IOC_WRITE) {
1123                 if (copy_from_user(&hdr, (void __user *)arg,
1124                         sizeof(struct nvavp_pushbuffer_submit_hdr)))
1125                         return -EFAULT;
1126         }
1127
1128         if (!hdr.cmdbuf.mem)
1129                 return 0;
1130
1131         if (copy_from_user(clientctx->relocs, (void __user *)hdr.relocs,
1132                         sizeof(struct nvavp_reloc) * hdr.num_relocs)) {
1133                 return -EFAULT;
1134         }
1135
1136         cmdbuf_handle = nvmap_get_handle_id(clientctx->nvmap, hdr.cmdbuf.mem);
1137         if (cmdbuf_handle == NULL) {
1138                 dev_err(&nvavp->nvhost_dev->dev,
1139                         "invalid cmd buffer handle %08x\n", hdr.cmdbuf.mem);
1140                 return -EPERM;
1141         }
1142
1143         /* duplicate the new pushbuffer's handle into the nvavp driver's
1144          * nvmap context, to ensure that the handle won't be freed as
1145          * long as it is in-use by the fb driver */
1146         cmdbuf_dupe = nvmap_duplicate_handle_id(nvavp->nvmap, hdr.cmdbuf.mem);
1147         nvmap_handle_put(cmdbuf_handle);
1148
1149         if (IS_ERR(cmdbuf_dupe)) {
1150                 dev_err(&nvavp->nvhost_dev->dev,
1151                         "could not duplicate handle\n");
1152                 return PTR_ERR(cmdbuf_dupe);
1153         }
1154
1155         phys_addr = nvmap_pin(nvavp->nvmap, cmdbuf_dupe);
1156         if (IS_ERR((void *)phys_addr)) {
1157                 dev_err(&nvavp->nvhost_dev->dev, "could not pin handle\n");
1158                 nvmap_free(nvavp->nvmap, cmdbuf_dupe);
1159                 return PTR_ERR((void *)phys_addr);
1160         }
1161
1162         virt_addr = (unsigned long)nvmap_mmap(cmdbuf_dupe);
1163         if (!virt_addr) {
1164                 dev_err(&nvavp->nvhost_dev->dev, "cannot map cmdbuf handle\n");
1165                 ret = -ENOMEM;
1166                 goto err_cmdbuf_mmap;
1167         }
1168
1169         cmdbuf_data = (u32 *)(virt_addr + hdr.cmdbuf.offset);
1170
1171         for (i = 0; i < hdr.num_relocs; i++) {
1172                 u32 *reloc_addr, target_phys_addr;
1173
1174                 if (clientctx->relocs[i].cmdbuf_mem != hdr.cmdbuf.mem) {
1175                         dev_err(&nvavp->nvhost_dev->dev,
1176                                 "reloc info does not match target bufferID\n");
1177                         ret = -EPERM;
1178                         goto err_reloc_info;
1179                 }
1180
1181                 reloc_addr = cmdbuf_data +
1182                              (clientctx->relocs[i].cmdbuf_offset >> 2);
1183
1184                 target_phys_addr = nvmap_handle_address(clientctx->nvmap,
1185                                             clientctx->relocs[i].target);
1186                 target_phys_addr += clientctx->relocs[i].target_offset;
1187                 writel(target_phys_addr, reloc_addr);
1188         }
1189
1190         if (hdr.syncpt) {
1191                 ret = nvavp_pushbuffer_update(nvavp,
1192                                              (phys_addr + hdr.cmdbuf.offset),
1193                                               hdr.cmdbuf.words, &syncpt,
1194                                               (hdr.flags & NVAVP_UCODE_EXT),
1195                                                 clientctx->channel_id);
1196
1197                 if (copy_to_user((void __user *)user_hdr->syncpt, &syncpt,
1198                                 sizeof(struct nvavp_syncpt))) {
1199                         ret = -EFAULT;
1200                         goto err_reloc_info;
1201                 }
1202         } else {
1203                 ret = nvavp_pushbuffer_update(nvavp,
1204                                              (phys_addr + hdr.cmdbuf.offset),
1205                                               hdr.cmdbuf.words, NULL,
1206                                               (hdr.flags & NVAVP_UCODE_EXT),
1207                                                 clientctx->channel_id);
1208         }
1209
1210 err_reloc_info:
1211         nvmap_munmap(cmdbuf_dupe, (void *)virt_addr);
1212 err_cmdbuf_mmap:
1213         nvmap_unpin(nvavp->nvmap, cmdbuf_dupe);
1214         nvmap_free(nvavp->nvmap, cmdbuf_dupe);
1215         return ret;
1216 }
1217
1218 static int nvavp_wake_avp_ioctl(struct file *filp, unsigned int cmd,
1219                                                         unsigned long arg)
1220 {
1221         wmb();
1222         /* wake up avp */
1223         writel(0xA0000001, NVAVP_OS_OUTBOX);
1224         return 0;
1225 }
1226
1227 static int nvavp_force_clock_stay_on_ioctl(struct file *filp, unsigned int cmd,
1228                                                         unsigned long arg)
1229 {
1230         struct nvavp_clientctx *clientctx = filp->private_data;
1231         struct nvavp_info *nvavp = clientctx->nvavp;
1232         struct nvavp_clock_stay_on_state_args clock;
1233
1234         if (copy_from_user(&clock, (void __user *)arg,
1235                            sizeof(struct nvavp_clock_stay_on_state_args)))
1236                 return -EFAULT;
1237
1238         dev_dbg(&nvavp->nvhost_dev->dev, "%s: state=%d\n",
1239                 __func__, clock.state);
1240
1241         if (clock.state != NVAVP_CLOCK_STAY_ON_DISABLED &&
1242             clock.state !=  NVAVP_CLOCK_STAY_ON_ENABLED) {
1243                 dev_err(&nvavp->nvhost_dev->dev, "%s: invalid argument=%d\n",
1244                         __func__, clock.state);
1245                 return -EINVAL;
1246         }
1247
1248         mutex_lock(&nvavp->open_lock);
1249         if (clock.state) {
1250                 if (clientctx->clk_reqs++ == 0)
1251                         nvavp_clks_enable(nvavp);
1252         } else {
1253                 if (--clientctx->clk_reqs == 0)
1254                         nvavp_clks_disable(nvavp);
1255         }
1256         mutex_unlock(&nvavp->open_lock);
1257         return 0;
1258 }
1259
1260 static int tegra_nvavp_open(struct inode *inode, struct file *filp, int channel_id)
1261 {
1262         struct miscdevice *miscdev = filp->private_data;
1263         struct nvavp_info *nvavp = dev_get_drvdata(miscdev->parent);
1264         int ret = 0;
1265         struct nvavp_clientctx *clientctx;
1266
1267         dev_dbg(&nvavp->nvhost_dev->dev, "%s: ++\n", __func__);
1268
1269         nonseekable_open(inode, filp);
1270
1271         clientctx = kzalloc(sizeof(*clientctx), GFP_KERNEL);
1272         if (!clientctx)
1273                 return -ENOMEM;
1274
1275         mutex_lock(&nvavp->open_lock);
1276
1277         pr_debug("tegra_nvavp_open channel_id (%d)\n", channel_id);
1278
1279         clientctx->channel_id = channel_id;
1280
1281         ret = nvavp_init(nvavp, channel_id);
1282
1283         if (!ret)
1284                 nvavp->refcount++;
1285
1286         clientctx->nvmap = nvavp->nvmap;
1287         clientctx->nvavp = nvavp;
1288
1289         filp->private_data = clientctx;
1290
1291         mutex_unlock(&nvavp->open_lock);
1292
1293         return ret;
1294 }
1295
1296 static int tegra_nvavp_video_open(struct inode *inode, struct file *filp)
1297 {
1298         pr_debug("tegra_nvavp_video_open NVAVP_VIDEO_CHANNEL\n");
1299         return tegra_nvavp_open(inode, filp, NVAVP_VIDEO_CHANNEL);
1300 }
1301
1302 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1303 static int tegra_nvavp_audio_open(struct inode *inode, struct file *filp)
1304 {
1305         pr_debug("tegra_nvavp_audio_open NVAVP_AUDIO_CHANNEL\n");
1306         return tegra_nvavp_open(inode, filp, NVAVP_AUDIO_CHANNEL);
1307 }
1308 #endif
1309
1310 static int tegra_nvavp_release(struct inode *inode, struct file *filp)
1311 {
1312         struct nvavp_clientctx *clientctx = filp->private_data;
1313         struct nvavp_info *nvavp = clientctx->nvavp;
1314         int ret = 0;
1315
1316         dev_dbg(&nvavp->nvhost_dev->dev, "%s: ++\n", __func__);
1317
1318         filp->private_data = NULL;
1319
1320         mutex_lock(&nvavp->open_lock);
1321
1322         if (!nvavp->refcount) {
1323                 dev_err(&nvavp->nvhost_dev->dev,
1324                         "releasing while in invalid state\n");
1325                 ret = -EINVAL;
1326                 goto out;
1327         }
1328
1329         /* if this client had any requests, drop our clk ref */
1330         if (clientctx->clk_reqs)
1331                 nvavp_clks_disable(nvavp);
1332
1333         if (nvavp->refcount > 0)
1334                 nvavp->refcount--;
1335         if (!nvavp->refcount)
1336                 nvavp_uninit(nvavp);
1337
1338 out:
1339         nvmap_client_put(clientctx->nvmap);
1340         mutex_unlock(&nvavp->open_lock);
1341         kfree(clientctx);
1342         return ret;
1343 }
1344
1345 static long tegra_nvavp_ioctl(struct file *filp, unsigned int cmd,
1346                             unsigned long arg)
1347 {
1348         int ret = 0;
1349
1350         if (_IOC_TYPE(cmd) != NVAVP_IOCTL_MAGIC ||
1351             _IOC_NR(cmd) < NVAVP_IOCTL_MIN_NR ||
1352             _IOC_NR(cmd) > NVAVP_IOCTL_MAX_NR)
1353                 return -EFAULT;
1354
1355         switch (cmd) {
1356         case NVAVP_IOCTL_SET_NVMAP_FD:
1357                 ret = nvavp_set_nvmapfd_ioctl(filp, cmd, arg);
1358                 break;
1359         case NVAVP_IOCTL_GET_SYNCPOINT_ID:
1360                 ret = nvavp_get_syncpointid_ioctl(filp, cmd, arg);
1361                 break;
1362         case NVAVP_IOCTL_PUSH_BUFFER_SUBMIT:
1363                 ret = nvavp_pushbuffer_submit_ioctl(filp, cmd, arg);
1364                 break;
1365         case NVAVP_IOCTL_SET_CLOCK:
1366                 ret = nvavp_set_clock_ioctl(filp, cmd, arg);
1367                 break;
1368         case NVAVP_IOCTL_GET_CLOCK:
1369                 ret = nvavp_get_clock_ioctl(filp, cmd, arg);
1370                 break;
1371         case NVAVP_IOCTL_WAKE_AVP:
1372                 ret = nvavp_wake_avp_ioctl(filp, cmd, arg);
1373                 break;
1374         case NVAVP_IOCTL_FORCE_CLOCK_STAY_ON:
1375                 ret = nvavp_force_clock_stay_on_ioctl(filp, cmd, arg);
1376                 break;
1377         default:
1378                 ret = -EINVAL;
1379                 break;
1380         }
1381         return ret;
1382 }
1383
1384 static const struct file_operations tegra_video_nvavp_fops = {
1385         .owner          = THIS_MODULE,
1386         .open           = tegra_nvavp_video_open,
1387         .release        = tegra_nvavp_release,
1388         .unlocked_ioctl = tegra_nvavp_ioctl,
1389 };
1390
1391 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1392 static const struct file_operations tegra_audio_nvavp_fops = {
1393         .owner          = THIS_MODULE,
1394         .open           = tegra_nvavp_audio_open,
1395         .release        = tegra_nvavp_release,
1396         .unlocked_ioctl = tegra_nvavp_ioctl,
1397 };
1398 #endif
1399
1400 static int tegra_nvavp_probe(struct nvhost_device *ndev,
1401         struct nvhost_device_id *id_table)
1402 {
1403         struct nvavp_info *nvavp;
1404         int irq;
1405         unsigned int heap_mask;
1406         u32 iovmm_addr;
1407         int ret = 0, channel_id;
1408
1409         irq = nvhost_get_irq_byname(ndev, "mbox_from_nvavp_pending");
1410         if (irq < 0) {
1411                 dev_err(&ndev->dev, "invalid nvhost data\n");
1412                 return -EINVAL;
1413         }
1414
1415
1416         nvavp = kzalloc(sizeof(struct nvavp_info), GFP_KERNEL);
1417         if (!nvavp) {
1418                 dev_err(&ndev->dev, "cannot allocate avp_info\n");
1419                 return -ENOMEM;
1420         }
1421
1422         memset(nvavp, 0, sizeof(*nvavp));
1423
1424         nvavp->nvhost_syncpt = &nvhost_get_host(ndev)->syncpt;
1425         if (!nvavp->nvhost_syncpt) {
1426                 dev_err(&ndev->dev, "cannot get syncpt handle\n");
1427                 ret = -ENOENT;
1428                 goto err_get_syncpt;
1429         }
1430
1431         nvavp->nvmap = nvmap_create_client(nvmap_dev, "nvavp_drv");
1432         if (IS_ERR_OR_NULL(nvavp->nvmap)) {
1433                 dev_err(&ndev->dev, "cannot create nvmap client\n");
1434                 ret = PTR_ERR(nvavp->nvmap);
1435                 goto err_nvmap_create_drv_client;
1436         }
1437
1438 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU) /* Tegra2 with AVP MMU */
1439         heap_mask = NVMAP_HEAP_CARVEOUT_GENERIC;
1440 #elif defined(CONFIG_TEGRA_AVP_KERNEL_ON_SMMU) /* Tegra3 with SMMU */
1441         heap_mask = NVMAP_HEAP_IOVMM;
1442 #else /* nvmem= carveout */
1443         heap_mask = 0;
1444 #endif
1445         switch (heap_mask) {
1446         case NVMAP_HEAP_IOVMM:
1447
1448                 iovmm_addr = 0x0ff00000;
1449
1450                 nvavp->os_info.handle = nvmap_alloc_iovm(nvavp->nvmap, SZ_1M,
1451                                                 L1_CACHE_BYTES,
1452                                                 NVMAP_HANDLE_UNCACHEABLE,
1453                                                 iovmm_addr);
1454                 if (IS_ERR_OR_NULL(nvavp->os_info.handle)) {
1455                         dev_err(&ndev->dev,
1456                                 "cannot create os handle\n");
1457                         ret = PTR_ERR(nvavp->os_info.handle);
1458                         goto err_nvmap_alloc;
1459                 }
1460
1461                 nvavp->os_info.data = nvmap_mmap(nvavp->os_info.handle);
1462                 if (!nvavp->os_info.data) {
1463                         dev_err(&ndev->dev,
1464                                 "cannot map os handle\n");
1465                         ret = -ENOMEM;
1466                         goto err_nvmap_mmap;
1467                 }
1468
1469                 nvavp->os_info.phys =
1470                         nvmap_pin(nvavp->nvmap, nvavp->os_info.handle);
1471                 if (IS_ERR_OR_NULL((void *)nvavp->os_info.phys)) {
1472                         dev_err(&ndev->dev,
1473                                 "cannot pin os handle\n");
1474                         ret = PTR_ERR((void *)nvavp->os_info.phys);
1475                         goto err_nvmap_pin;
1476                 }
1477
1478                 dev_info(&ndev->dev,
1479                         "allocated IOVM at %lx for AVP os\n",
1480                         (unsigned long)nvavp->os_info.phys);
1481                 break;
1482         case NVMAP_HEAP_CARVEOUT_GENERIC:
1483                 nvavp->os_info.handle = nvmap_alloc(nvavp->nvmap, SZ_1M, SZ_1M,
1484                                                 NVMAP_HANDLE_UNCACHEABLE, 0);
1485                 if (IS_ERR_OR_NULL(nvavp->os_info.handle)) {
1486                         dev_err(&ndev->dev, "cannot create AVP os handle\n");
1487                         ret = PTR_ERR(nvavp->os_info.handle);
1488                         goto err_nvmap_alloc;
1489                 }
1490
1491                 nvavp->os_info.data = nvmap_mmap(nvavp->os_info.handle);
1492                 if (!nvavp->os_info.data) {
1493                         dev_err(&ndev->dev, "cannot map AVP os handle\n");
1494                         ret = -ENOMEM;
1495                         goto err_nvmap_mmap;
1496                 }
1497
1498                 nvavp->os_info.phys = nvmap_pin(nvavp->nvmap,
1499                                         nvavp->os_info.handle);
1500                 if (IS_ERR_OR_NULL((void *)nvavp->os_info.phys)) {
1501                         dev_err(&ndev->dev, "cannot pin AVP os handle\n");
1502                         ret = PTR_ERR((void *)nvavp->os_info.phys);
1503                         goto err_nvmap_pin;
1504                 }
1505
1506                 dev_info(&ndev->dev,
1507                         "allocated carveout memory at %lx for AVP os\n",
1508                         (unsigned long)nvavp->os_info.phys);
1509                 break;
1510         default:
1511                 dev_err(&ndev->dev, "invalid/non-supported heap for AVP os\n");
1512                 ret = -EINVAL;
1513                 goto err_get_syncpt;
1514         }
1515
1516         nvavp->mbox_from_avp_pend_irq = irq;
1517         mutex_init(&nvavp->open_lock);
1518
1519         for (channel_id = 0; channel_id < NVAVP_NUM_CHANNELS; channel_id++)
1520                 mutex_init(&nvavp->channel_info[channel_id].pushbuffer_lock);
1521
1522         /* TODO DO NOT USE NVAVP DEVICE */
1523         nvavp->cop_clk = clk_get(&ndev->dev, "cop");
1524         if (IS_ERR(nvavp->cop_clk)) {
1525                 dev_err(&ndev->dev, "cannot get cop clock\n");
1526                 ret = -ENOENT;
1527                 goto err_get_cop_clk;
1528         }
1529
1530         nvavp->vde_clk = clk_get(&ndev->dev, "vde");
1531         if (IS_ERR(nvavp->vde_clk)) {
1532                 dev_err(&ndev->dev, "cannot get vde clock\n");
1533                 ret = -ENOENT;
1534                 goto err_get_vde_clk;
1535         }
1536
1537         nvavp->bsev_clk = clk_get(&ndev->dev, "bsev");
1538         if (IS_ERR(nvavp->bsev_clk)) {
1539                 dev_err(&ndev->dev, "cannot get bsev clock\n");
1540                 ret = -ENOENT;
1541                 goto err_get_bsev_clk;
1542         }
1543
1544         nvavp->sclk = clk_get(&ndev->dev, "sclk");
1545         if (IS_ERR(nvavp->sclk)) {
1546                 dev_err(&ndev->dev, "cannot get avp.sclk clock\n");
1547                 ret = -ENOENT;
1548                 goto err_get_sclk;
1549         }
1550
1551         nvavp->emc_clk = clk_get(&ndev->dev, "emc");
1552         if (IS_ERR(nvavp->emc_clk)) {
1553                 dev_err(&ndev->dev, "cannot get emc clock\n");
1554                 ret = -ENOENT;
1555                 goto err_get_emc_clk;
1556         }
1557
1558         nvavp->clk_enabled = 0;
1559         nvavp_halt_avp(nvavp);
1560
1561         INIT_WORK(&nvavp->clock_disable_work, clock_disable_handler);
1562
1563         nvavp->video_misc_dev.minor = MISC_DYNAMIC_MINOR;
1564         nvavp->video_misc_dev.name = "tegra_avpchannel";
1565         nvavp->video_misc_dev.fops = &tegra_video_nvavp_fops;
1566         nvavp->video_misc_dev.mode = S_IRWXUGO;
1567         nvavp->video_misc_dev.parent = &ndev->dev;
1568
1569         ret = misc_register(&nvavp->video_misc_dev);
1570         if (ret) {
1571                 dev_err(&ndev->dev, "unable to register misc device!\n");
1572                 goto err_misc_reg;
1573         }
1574
1575 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1576         nvavp->audio_misc_dev.minor = MISC_DYNAMIC_MINOR;
1577         nvavp->audio_misc_dev.name = "tegra_audio_avpchannel";
1578         nvavp->audio_misc_dev.fops = &tegra_audio_nvavp_fops;
1579         nvavp->audio_misc_dev.mode = S_IRWXUGO;
1580         nvavp->audio_misc_dev.parent = &ndev->dev;
1581
1582         ret = misc_register(&nvavp->audio_misc_dev);
1583         if (ret) {
1584         dev_err(&ndev->dev, "unable to register misc device!\n");
1585                 goto err_audio_misc_reg;
1586         }
1587 #endif
1588
1589         ret = request_irq(irq, nvavp_mbox_pending_isr, 0,
1590                           TEGRA_NVAVP_NAME, nvavp);
1591         if (ret) {
1592                 dev_err(&ndev->dev, "cannot register irq handler\n");
1593                 goto err_req_irq_pend;
1594         }
1595         disable_irq(nvavp->mbox_from_avp_pend_irq);
1596
1597         nvhost_set_drvdata(ndev, nvavp);
1598         nvavp->nvhost_dev = ndev;
1599
1600         return 0;
1601
1602 err_req_irq_pend:
1603 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1604         misc_deregister(&nvavp->audio_misc_dev);
1605 err_audio_misc_reg:
1606 #endif
1607         misc_deregister(&nvavp->video_misc_dev);
1608 err_misc_reg:
1609         clk_put(nvavp->emc_clk);
1610 err_get_emc_clk:
1611         clk_put(nvavp->sclk);
1612 err_get_sclk:
1613         clk_put(nvavp->bsev_clk);
1614 err_get_bsev_clk:
1615         clk_put(nvavp->vde_clk);
1616 err_get_vde_clk:
1617         clk_put(nvavp->cop_clk);
1618 err_get_cop_clk:
1619         nvmap_unpin(nvavp->nvmap, nvavp->os_info.handle);
1620 err_nvmap_pin:
1621         nvmap_munmap(nvavp->os_info.handle, nvavp->os_info.data);
1622 err_nvmap_mmap:
1623 #if defined(CONFIG_TEGRA_AVP_KERNEL_ON_MMU)
1624         nvmap_free(nvavp->nvmap, nvavp->os_info.handle);
1625 #elif defined(CONFIG_TEGRA_AVP_KERNEL_ON_SMMU)
1626         nvmap_free_iovm(nvavp->nvmap, nvavp->os_info.handle);
1627 #endif
1628 err_nvmap_alloc:
1629         nvmap_client_put(nvavp->nvmap);
1630 err_nvmap_create_drv_client:
1631 err_get_syncpt:
1632         kfree(nvavp);
1633         return ret;
1634 }
1635
1636 static int tegra_nvavp_remove(struct nvhost_device *ndev)
1637 {
1638         struct nvavp_info *nvavp = nvhost_get_drvdata(ndev);
1639
1640         if (!nvavp)
1641                 return 0;
1642
1643         mutex_lock(&nvavp->open_lock);
1644         if (nvavp->refcount) {
1645                 mutex_unlock(&nvavp->open_lock);
1646                 return -EBUSY;
1647         }
1648         mutex_unlock(&nvavp->open_lock);
1649
1650         nvavp_unload_ucode(nvavp);
1651         nvavp_unload_os(nvavp);
1652
1653         misc_deregister(&nvavp->video_misc_dev);
1654
1655 #if defined(CONFIG_TEGRA_NVAVP_AUDIO)
1656         misc_deregister(&nvavp->audio_misc_dev);
1657 #endif
1658         clk_put(nvavp->bsev_clk);
1659         clk_put(nvavp->vde_clk);
1660         clk_put(nvavp->cop_clk);
1661
1662         clk_put(nvavp->emc_clk);
1663         clk_put(nvavp->sclk);
1664
1665         nvmap_client_put(nvavp->nvmap);
1666
1667         kfree(nvavp);
1668         return 0;
1669 }
1670
1671 #ifdef CONFIG_PM
1672 static int tegra_nvavp_suspend(struct nvhost_device *ndev, pm_message_t state)
1673 {
1674         struct nvavp_info *nvavp = nvhost_get_drvdata(ndev);
1675         int ret = 0;
1676
1677         mutex_lock(&nvavp->open_lock);
1678
1679         if (nvavp->refcount) {
1680                 if (!nvavp->clk_enabled)
1681                         nvavp_uninit(nvavp);
1682                 else
1683                         ret = -EBUSY;
1684         }
1685
1686         mutex_unlock(&nvavp->open_lock);
1687
1688         return ret;
1689 }
1690
1691 static int tegra_nvavp_resume(struct nvhost_device *ndev)
1692 {
1693         struct nvavp_info *nvavp = nvhost_get_drvdata(ndev);
1694
1695         mutex_lock(&nvavp->open_lock);
1696
1697         if (nvavp->refcount)
1698                 nvavp_init(nvavp, NVAVP_VIDEO_CHANNEL);
1699
1700         mutex_unlock(&nvavp->open_lock);
1701
1702         return 0;
1703 }
1704 #endif
1705
1706 static struct nvhost_driver tegra_nvavp_driver = {
1707         .driver = {
1708                 .name   = TEGRA_NVAVP_NAME,
1709                 .owner  = THIS_MODULE,
1710         },
1711         .probe          = tegra_nvavp_probe,
1712         .remove         = tegra_nvavp_remove,
1713 #ifdef CONFIG_PM
1714         .suspend        = tegra_nvavp_suspend,
1715         .resume         = tegra_nvavp_resume,
1716 #endif
1717 };
1718
1719 static int __init tegra_nvavp_init(void)
1720 {
1721         return nvhost_driver_register(&tegra_nvavp_driver);
1722 }
1723
1724 static void __exit tegra_nvavp_exit(void)
1725 {
1726         nvhost_driver_unregister(&tegra_nvavp_driver);
1727 }
1728
1729 module_init(tegra_nvavp_init);
1730 module_exit(tegra_nvavp_exit);
1731
1732 MODULE_AUTHOR("NVIDIA");
1733 MODULE_DESCRIPTION("Channel based AVP driver for Tegra");
1734 MODULE_VERSION("1.0");
1735 MODULE_LICENSE("Dual BSD/GPL");