summaryrefslogtreecommitdiff
path: root/drivers/video/tegra/dc/overlay.c
blob: b40f8470373e5694f41989c34520c1d31eddd8ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
 * drivers/video/tegra/overlay/overlay.c
 *
 * Copyright (c) 2010-2011, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/miscdevice.h>
#include <linux/mm.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/tegra_overlay.h>
#include <linux/uaccess.h>

#include <asm/atomic.h>

#include <mach/dc.h>
#include <mach/fb.h>
#include <mach/nvhost.h>

#include "dc_priv.h"
#include "../nvmap/nvmap.h"
#include "overlay.h"

DEFINE_MUTEX(tegra_flip_lock);

struct overlay_client;

struct overlay {
	struct overlay_client	*owner;
};

struct tegra_overlay_info {
	struct miscdevice	dev;

	struct list_head	clients;
	spinlock_t		clients_lock;

	struct overlay		overlays[DC_N_WINDOWS];
	struct mutex		overlays_lock;

	struct nvhost_device	*ndev;

	struct nvmap_client	*overlay_nvmap;

	struct tegra_dc		*dc;

	struct tegra_dc_blend	blend;

	struct workqueue_struct	*flip_wq;

	/* Big enough for tegra_dc%u when %u < 10 */
	char			name[10];
};

struct overlay_client {
	struct tegra_overlay_info	*dev;
	struct list_head		list;
	struct task_struct		*task;
	struct nvmap_client		*user_nvmap;
};

struct tegra_overlay_flip_win {
	struct tegra_overlay_windowattr	attr;
	struct nvmap_handle_ref		*handle;
	dma_addr_t			phys_addr;
};

struct tegra_overlay_flip_data {
	struct work_struct		work;
	struct tegra_overlay_info	*overlay;
	struct tegra_overlay_flip_win	win[TEGRA_FB_FLIP_N_WINDOWS];
	u32				syncpt_max;
	u32				flags;
};

/* Overlay window manipulation */
static int tegra_overlay_pin_window(struct tegra_overlay_info *overlay,
				    struct tegra_overlay_flip_win *flip_win,
				    struct nvmap_client *user_nvmap)
{
	struct nvmap_handle_ref *win_dupe;
	struct nvmap_handle *win_handle;
	unsigned long buff_id = flip_win->attr.buff_id;

	if (!buff_id)
		return 0;

	win_handle = nvmap_get_handle_id(user_nvmap, buff_id);
	if (win_handle == NULL) {
		dev_err(&overlay->ndev->dev, "%s: flip invalid "
			"handle %08lx\n", current->comm, buff_id);
		return -EPERM;
	}

	/* duplicate the new framebuffer's handle into the fb driver's
	 * nvmap context, to ensure that the handle won't be freed as
	 * long as it is in-use by the fb driver */
	win_dupe = nvmap_duplicate_handle_id(overlay->overlay_nvmap, buff_id);
	nvmap_handle_put(win_handle);

	if (IS_ERR(win_dupe)) {
		dev_err(&overlay->ndev->dev, "couldn't duplicate handle\n");
		return PTR_ERR(win_dupe);
	}

	flip_win->handle = win_dupe;

	flip_win->phys_addr = nvmap_pin(overlay->overlay_nvmap, win_dupe);
	if (IS_ERR((void *)flip_win->phys_addr)) {
		dev_err(&overlay->ndev->dev, "couldn't pin handle\n");
		nvmap_free(overlay->overlay_nvmap, win_dupe);
		return PTR_ERR((void *)flip_win->phys_addr);
	}

	return 0;
}

static int tegra_overlay_set_windowattr(struct tegra_overlay_info *overlay,
					struct tegra_dc_win *win,
					const struct tegra_overlay_flip_win *flip_win)
{
	int xres, yres;
	if (flip_win->handle == NULL) {
		win->flags = 0;
		win->cur_handle = NULL;
		return 0;
	}

	xres = overlay->dc->mode.h_active;
	yres = overlay->dc->mode.v_active;

	win->flags = TEGRA_WIN_FLAG_ENABLED;
	if (flip_win->attr.blend == TEGRA_FB_WIN_BLEND_PREMULT)
		win->flags |= TEGRA_WIN_FLAG_BLEND_PREMULT;
	else if (flip_win->attr.blend == TEGRA_FB_WIN_BLEND_COVERAGE)
		win->flags |= TEGRA_WIN_FLAG_BLEND_COVERAGE;
	if (flip_win->attr.flags & TEGRA_FB_WIN_FLAG_INVERT_H)
		win->flags |= TEGRA_WIN_FLAG_INVERT_H;
	if (flip_win->attr.flags & TEGRA_FB_WIN_FLAG_INVERT_V)
		win->flags |= TEGRA_WIN_FLAG_INVERT_V;

	win->fmt = flip_win->attr.pixformat;
	win->x = flip_win->attr.x;
	win->y = flip_win->attr.y;
	win->w = flip_win->attr.w;
	win->h = flip_win->attr.h;
	win->out_x = flip_win->attr.out_x;
	win->out_y = flip_win->attr.out_y;
	win->out_w = flip_win->attr.out_w;
	win->out_h = flip_win->attr.out_h;

	WARN_ONCE(win->out_x >= xres,
		"%s:application window x offset(%d) exceeds display width(%d)\n",
		dev_name(&win->dc->ndev->dev), win->out_x, xres);
	WARN_ONCE(win->out_y >= yres,
		"%s:application window y offset(%d) exceeds display height(%d)\n",
		dev_name(&win->dc->ndev->dev), win->out_y, yres);
	WARN_ONCE(win->out_x + win->out_w > xres && win->out_x < xres,
		"%s:application window width(%d) exceeds display width(%d)\n",
		dev_name(&win->dc->ndev->dev), win->out_x + win->out_w, xres);
	WARN_ONCE(win->out_y + win->out_h > yres && win->out_y < yres,
		"%s:application window height(%d) exceeds display height(%d)\n",
		dev_name(&win->dc->ndev->dev), win->out_y + win->out_h, yres);

	if (((win->out_x + win->out_w) > xres) && (win->out_x < xres)) {
		long new_w = xres - win->out_x;
		win->w = win->w * new_w / win->out_w;
	        win->out_w = new_w;
	}
	if (((win->out_y + win->out_h) > yres) && (win->out_y < yres)) {
		long new_h = yres - win->out_y;
		win->h = win->h * new_h / win->out_h;
	        win->out_h = new_h;
	}

	win->z = flip_win->attr.z;
	win->cur_handle = flip_win->handle;

	/* STOPSHIP verify that this won't read outside of the surface */
	win->phys_addr = flip_win->phys_addr + flip_win->attr.offset;
	win->offset_u = flip_win->attr.offset_u + flip_win->attr.offset;
	win->offset_v = flip_win->attr.offset_v + flip_win->attr.offset;
	win->stride = flip_win->attr.stride;
	win->stride_uv = flip_win->attr.stride_uv;
	if (flip_win->attr.tiled)
		win->flags |= TEGRA_WIN_FLAG_TILED;

	if ((s32)flip_win->attr.pre_syncpt_id >= 0) {
		nvhost_syncpt_wait_timeout(&overlay->ndev->host->syncpt,
					   flip_win->attr.pre_syncpt_id,
					   flip_win->attr.pre_syncpt_val,
					   msecs_to_jiffies(500));
	}

	/* Store the blend state incase we need to reorder later */
	overlay->blend.z[win->idx] = win->z;
	overlay->blend.flags[win->idx] = win->flags & TEGRA_WIN_BLEND_FLAGS_MASK;

	return 0;
}

/* overlay policy for premult is dst alpha, which needs reassignment */
/* of blend settings for the DC */
static void tegra_overlay_blend_reorder(struct tegra_dc_blend *blend,
					struct tegra_dc_win *windows[])
{
	int idx, below;

	/* Copy across the original blend state to each window */
	for (idx = 0; idx < DC_N_WINDOWS; idx++) {
		windows[idx]->z = blend->z[idx];
		windows[idx]->flags &= ~TEGRA_WIN_BLEND_FLAGS_MASK;
		windows[idx]->flags |= blend->flags[idx];
	}

	/* Find a window with PreMult */
	for (idx = 0; idx < DC_N_WINDOWS; idx++) {
		if (blend->flags[idx] == TEGRA_WIN_FLAG_BLEND_PREMULT)
			break;
	}
	if (idx == DC_N_WINDOWS)
		return;

	/* Find the window directly below it */
	for (below = 0; below < DC_N_WINDOWS; below++) {
		if (below == idx)
			continue;
		if (blend->z[below] > blend->z[idx])
			break;
	}
	if (below == DC_N_WINDOWS)
		return;

	/* Switch the flags and the ordering */
	windows[idx]->z = blend->z[below];
	windows[idx]->flags &= ~TEGRA_WIN_BLEND_FLAGS_MASK;
	windows[idx]->flags |= blend->flags[below];
	windows[below]->z = blend->z[idx];
	windows[below]->flags &= ~TEGRA_WIN_BLEND_FLAGS_MASK;
	windows[below]->flags |= blend->flags[idx];
}

static void tegra_overlay_flip_worker(struct work_struct *work)
{
	struct tegra_overlay_flip_data *data =
		container_of(work, struct tegra_overlay_flip_data, work);
	struct tegra_overlay_info *overlay = data->overlay;
	struct tegra_dc_win *win;
	struct tegra_dc_win *wins[TEGRA_FB_FLIP_N_WINDOWS];
	struct nvmap_handle_ref *unpin_handles[TEGRA_FB_FLIP_N_WINDOWS];
	int i, nr_win = 0, nr_unpin = 0;

	data = container_of(work, struct tegra_overlay_flip_data, work);

	for (i = 0; i < TEGRA_FB_FLIP_N_WINDOWS; i++) {
		struct tegra_overlay_flip_win *flip_win = &data->win[i];
		int idx = flip_win->attr.index;

		if (idx == -1)
			continue;

		win = tegra_dc_get_window(overlay->dc, idx);

		if (!win)
			continue;

		if (win->flags && win->cur_handle)
			unpin_handles[nr_unpin++] = win->cur_handle;

		tegra_overlay_set_windowattr(overlay, win, &data->win[i]);

		wins[nr_win++] = win;

#if 0
		if (flip_win->attr.pre_syncpt_id < 0)
			continue;
		printk("%08x %08x\n",
		       flip_win->attr.pre_syncpt_id,
		       flip_win->attr.pre_syncpt_val);

		nvhost_syncpt_wait_timeout(&overlay->ndev->host->syncpt,
					   flip_win->attr.pre_syncpt_id,
					   flip_win->attr.pre_syncpt_val,
					   msecs_to_jiffies(500));
#endif
	}

	if (data->flags & TEGRA_OVERLAY_FLIP_FLAG_BLEND_REORDER) {
		struct tegra_dc_win *dcwins[DC_N_WINDOWS];

		for (i = 0; i < DC_N_WINDOWS; i++)
			dcwins[i] = tegra_dc_get_window(overlay->dc, i);

		tegra_overlay_blend_reorder(&overlay->blend, dcwins);
		tegra_dc_update_windows(dcwins, DC_N_WINDOWS);
		tegra_dc_sync_windows(dcwins, DC_N_WINDOWS);
	} else {
		tegra_dc_update_windows(wins, nr_win);
		/* TODO: implement swapinterval here */
		tegra_dc_sync_windows(wins, nr_win);
	}

		tegra_dc_incr_syncpt_min(overlay->dc, data->syncpt_max);

	/* unpin and deref previous front buffers */
	for (i = 0; i < nr_unpin; i++) {
		nvmap_unpin(overlay->overlay_nvmap, unpin_handles[i]);
		nvmap_free(overlay->overlay_nvmap, unpin_handles[i]);
	}

	kfree(data);
}

static int tegra_overlay_flip(struct tegra_overlay_info *overlay,
			      struct tegra_overlay_flip_args *args,
			      struct nvmap_client *user_nvmap)
{
	struct tegra_overlay_flip_data *data;
	struct tegra_overlay_flip_win *flip_win;
	u32 syncpt_max;
	int i, err;

	if (WARN_ON(!overlay->ndev))
		return -EFAULT;

	mutex_lock(&tegra_flip_lock);
	if (!overlay->dc->enabled) {
		mutex_unlock(&tegra_flip_lock);
		return -EFAULT;
	}

	data = kzalloc(sizeof(*data), GFP_KERNEL);
	if (data == NULL) {
		dev_err(&overlay->ndev->dev,
			"can't allocate memory for flip\n");
		mutex_unlock(&tegra_flip_lock);
		return -ENOMEM;
	}

	INIT_WORK(&data->work, tegra_overlay_flip_worker);
	data->overlay = overlay;
	data->flags = args->flags;

	for (i = 0; i < TEGRA_FB_FLIP_N_WINDOWS; i++) {
		flip_win = &data->win[i];

		memcpy(&flip_win->attr, &args->win[i], sizeof(flip_win->attr));

		if (flip_win->attr.index == -1)
			continue;

		err = tegra_overlay_pin_window(overlay, flip_win, user_nvmap);
		if (err < 0) {
			dev_err(&overlay->ndev->dev,
				"error setting window attributes\n");
			goto surf_err;
		}
	}

	syncpt_max = tegra_dc_incr_syncpt_max(overlay->dc);
	data->syncpt_max = syncpt_max;

	queue_work(overlay->flip_wq, &data->work);

	args->post_syncpt_val = syncpt_max;
	args->post_syncpt_id = tegra_dc_get_syncpt_id(overlay->dc);
	mutex_unlock(&tegra_flip_lock);

	return 0;

surf_err:
	while (i--) {
		if (data->win[i].handle) {
			nvmap_unpin(overlay->overlay_nvmap,
				    data->win[i].handle);
			nvmap_free(overlay->overlay_nvmap,
				   data->win[i].handle);
		}
	}
	kfree(data);
	mutex_unlock(&tegra_flip_lock);
	return err;
}
static void tegra_overlay_set_emc_freq(struct tegra_overlay_info *dev)
{
	unsigned long emc_freq = 0;
	int i;

	for (i = 0; i < dev->dc->n_windows; i++) {
		if (dev->overlays[i].owner != NULL)
			emc_freq += dev->dc->mode.pclk*(i==1?2:1)*2;
	}
	clk_set_rate(dev->dc->emc_clk, emc_freq);
}

/* Overlay functions */
static bool tegra_overlay_get(struct overlay_client *client, int idx)
{
	struct tegra_overlay_info *dev = client->dev;
	bool ret = false;

	if (idx < 0 || idx > dev->dc->n_windows)
		return ret;

	mutex_lock(&dev->overlays_lock);
	if (dev->overlays[idx].owner == NULL) {
		dev->overlays[idx].owner = client;
		ret = true;
		if (dev->dc->mode.pclk != 0)
			tegra_overlay_set_emc_freq(dev);
	}
	mutex_unlock(&dev->overlays_lock);

	return ret;
}

static void tegra_overlay_put_locked(struct overlay_client *client, int idx)
{
	struct tegra_overlay_flip_args flip_args;
	struct tegra_overlay_info *dev = client->dev;

	if (idx < 0 || idx > dev->dc->n_windows)
		return;

	if (dev->overlays[idx].owner != client)
		return;

	dev->overlays[idx].owner = NULL;

	flip_args.win[0].index = idx;
	flip_args.win[0].buff_id = 0;
	flip_args.win[1].index = -1;
	flip_args.win[2].index = -1;
	flip_args.flags = 0;

	tegra_overlay_flip(dev, &flip_args, NULL);
	if (dev->dc->mode.pclk != 0)
		tegra_overlay_set_emc_freq(dev);
}

static void tegra_overlay_put(struct overlay_client *client, int idx)
{
	mutex_lock(&client->dev->overlays_lock);
	tegra_overlay_put_locked(client, idx);
	mutex_unlock(&client->dev->overlays_lock);
}

/* Ioctl implementations */
static int tegra_overlay_ioctl_open(struct overlay_client *client,
				    void __user *arg)
{
	int idx = -1;

	if (copy_from_user(&idx, arg, sizeof(idx)))
		return -EFAULT;

	if (!tegra_overlay_get(client, idx))
		return -EBUSY;

	if (copy_to_user(arg, &idx, sizeof(idx))) {
		tegra_overlay_put(client, idx);
		return -EFAULT;
	}

	return 0;
}

static int tegra_overlay_ioctl_close(struct overlay_client *client,
				     void __user *arg)
{
	int err = 0;
	int idx;

	if (copy_from_user(&idx, arg, sizeof(idx)))
		return -EFAULT;

	if (idx < 0 || idx > client->dev->dc->n_windows)
		return -EINVAL;

	mutex_lock(&client->dev->overlays_lock);
	if (client->dev->overlays[idx].owner == client)
		tegra_overlay_put_locked(client, idx);
	else
		err = -EINVAL;
	mutex_unlock(&client->dev->overlays_lock);

	return err;
}

static int tegra_overlay_ioctl_flip(struct overlay_client *client,
				    void __user *arg)
{
	int i = 0;
	int idx = 0;
	bool found_one = false;
	struct tegra_overlay_flip_args flip_args;

	if (!client->dev->dc->enabled)
		return -EPIPE;

	if (copy_from_user(&flip_args, arg, sizeof(flip_args)))
		return -EFAULT;

	for (i = 0; i < TEGRA_FB_FLIP_N_WINDOWS; i++) {
		idx = flip_args.win[i].index;
		if (idx == -1) {
			flip_args.win[i].buff_id = 0;
			continue;
		}

		if (idx < 0 || idx > client->dev->dc->n_windows) {
			dev_err(&client->dev->ndev->dev,
				"Flipping an invalid overlay! %d\n", idx);
			flip_args.win[i].index = -1;
			flip_args.win[i].buff_id = 0;
			continue;
		}

		if (client->dev->overlays[idx].owner != client) {
			dev_err(&client->dev->ndev->dev,
				"Flipping a non-owned overlay! %d\n", idx);
			flip_args.win[i].index = -1;
			flip_args.win[i].buff_id = 0;
			continue;
		}

		found_one = true;
	}

	if (!found_one)
		return -EFAULT;

	tegra_overlay_flip(client->dev, &flip_args, client->user_nvmap);

	if (copy_to_user(arg, &flip_args, sizeof(flip_args)))
		return -EFAULT;

	return 0;
}

static int tegra_overlay_ioctl_set_nvmap_fd(struct overlay_client *client,
					    void __user *arg)
{
	int fd;
	struct nvmap_client *nvmap = NULL;

	if (copy_from_user(&fd, arg, sizeof(fd)))
		return -EFAULT;

	if (fd < 0)
		return -EINVAL;

	nvmap = nvmap_client_get_file(fd);
	if (IS_ERR(nvmap))
		return PTR_ERR(nvmap);

	if (client->user_nvmap)
		nvmap_client_put(client->user_nvmap);

	client->user_nvmap = nvmap;

	return 0;
}

/* File operations */
static int tegra_overlay_open(struct inode *inode, struct file *filp)
{
	struct miscdevice *miscdev = filp->private_data;
	struct tegra_overlay_info *dev = container_of(miscdev,
						      struct tegra_overlay_info,
						      dev);
	struct overlay_client *priv;
	unsigned long flags;
	int ret;

	ret = nonseekable_open(inode, filp);
	if (unlikely(ret))
		return ret;

	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->dev = dev;

	get_task_struct(current);
	priv->task = current;

	spin_lock_irqsave(&dev->clients_lock, flags);
	list_add(&priv->list, &dev->clients);
	spin_unlock_irqrestore(&dev->clients_lock, flags);

	filp->private_data = priv;
	return 0;
}

static int tegra_overlay_release(struct inode *inode, struct file *filp)
{
	struct overlay_client *client = filp->private_data;
	unsigned long flags;
	int i;

	mutex_lock(&client->dev->overlays_lock);
	for (i = 0; i < client->dev->dc->n_windows; i++)
		if (client->dev->overlays[i].owner == client)
			tegra_overlay_put_locked(client, i);
	mutex_unlock(&client->dev->overlays_lock);

	spin_lock_irqsave(&client->dev->clients_lock, flags);
	list_del(&client->list);
	spin_unlock_irqrestore(&client->dev->clients_lock, flags);

	nvmap_client_put(client->user_nvmap);
	put_task_struct(client->task);

	kfree(client);
	return 0;
}

static long tegra_overlay_ioctl(struct file *filp, unsigned int cmd,
				unsigned long arg)
{
	struct overlay_client *client = filp->private_data;
	int err = 0;
	void __user *uarg = (void __user *)arg;

	if (_IOC_TYPE(cmd) != TEGRA_OVERLAY_IOCTL_MAGIC)
		return -ENOTTY;

	if (_IOC_NR(cmd) < TEGRA_OVERLAY_IOCTL_MIN_NR)
		return -ENOTTY;

	if (_IOC_NR(cmd) > TEGRA_OVERLAY_IOCTL_MAX_NR)
		return -ENOTTY;

	if (_IOC_DIR(cmd) & _IOC_READ)
		err = !access_ok(VERIFY_WRITE, uarg, _IOC_SIZE(cmd));
	if (_IOC_DIR(cmd) & _IOC_WRITE)
		err = !access_ok(VERIFY_READ, uarg, _IOC_SIZE(cmd));

	if (err)
		return -EFAULT;

	switch (cmd) {
	case TEGRA_OVERLAY_IOCTL_OPEN_WINDOW:
		err = tegra_overlay_ioctl_open(client, uarg);
		break;
	case TEGRA_OVERLAY_IOCTL_CLOSE_WINDOW:
		err = tegra_overlay_ioctl_close(client, uarg);
		break;
	case TEGRA_OVERLAY_IOCTL_FLIP:
		err = tegra_overlay_ioctl_flip(client, uarg);
		break;
	case TEGRA_OVERLAY_IOCTL_SET_NVMAP_FD:
		err = tegra_overlay_ioctl_set_nvmap_fd(client, uarg);
		break;
	default:
		return -ENOTTY;
	}
	return err;
}

static const struct file_operations overlay_fops = {
	.owner		= THIS_MODULE,
	.open		= tegra_overlay_open,
	.release	= tegra_overlay_release,
	.unlocked_ioctl = tegra_overlay_ioctl,
};

/* Registration */
struct tegra_overlay_info *tegra_overlay_register(struct nvhost_device *ndev,
						  struct tegra_dc *dc)
{
	struct tegra_overlay_info *dev;
	int e;

	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev) {
		dev_err(&ndev->dev, "out of memory for device\n");
		return ERR_PTR(-ENOMEM);
	}

	snprintf(dev->name, sizeof(dev->name), "tegra_dc%u", ndev->id);

	dev->ndev = ndev;
	dev->dev.minor = MISC_DYNAMIC_MINOR;
	dev->dev.name = dev->name;
	dev->dev.fops = &overlay_fops;
	dev->dev.parent = &ndev->dev;

	spin_lock_init(&dev->clients_lock);
	INIT_LIST_HEAD(&dev->clients);

	mutex_init(&dev->overlays_lock);

	e = misc_register(&dev->dev);
	if (e) {
		dev_err(&ndev->dev, "unable to register miscdevice %s\n",
			dev->dev.name);
		goto fail;
	}

	dev->overlay_nvmap = nvmap_create_client(nvmap_dev, "overlay");
	if (!dev->overlay_nvmap) {
		dev_err(&ndev->dev, "couldn't create nvmap client\n");
		e = -ENOMEM;
		goto err_free;
	}

	dev->flip_wq = create_singlethread_workqueue(dev_name(&ndev->dev));
	if (!dev->flip_wq) {
		dev_err(&ndev->dev, "couldn't create flip work-queue\n");
		e = -ENOMEM;
		goto err_delete_wq;
	}

	dev->dc = dc;

	dev_info(&ndev->dev, "registered overlay\n");

	return dev;

err_delete_wq:
err_free:
fail:
	if (dev->dev.minor != MISC_DYNAMIC_MINOR)
		misc_deregister(&dev->dev);
	kfree(dev);
	return ERR_PTR(e);
}

void tegra_overlay_unregister(struct tegra_overlay_info *info)
{
	misc_deregister(&info->dev);

	kfree(info);
}

void tegra_overlay_disable(struct tegra_overlay_info *overlay_info)
{
	mutex_lock(&tegra_flip_lock);
	flush_workqueue(overlay_info->flip_wq);
	mutex_unlock(&tegra_flip_lock);
}