summaryrefslogtreecommitdiff
path: root/drivers/video/tegra/fb.c
blob: 9f5bd9a71e68d2dd5a375bde452181dd9444e78a (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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/*
 * drivers/video/tegra/fb.c
 *
 * Copyright (C) 2010 Google, Inc.
 * Author: Erik Gilling <konkers@android.com>
 *         Colin Cross <ccross@android.com>
 *         Travis Geiselbrecht <travis@palm.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 *
 */

#include <linux/fb.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/file.h>
#include <linux/workqueue.h>

#include <asm/atomic.h>

#include <video/tegrafb.h>

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

#include "host/dev.h"
#include "nvmap/nvmap.h"
#include "dc/dc_priv.h"

struct tegra_fb_info {
	struct tegra_dc_win	*win;
	struct nvhost_device	*ndev;
	struct fb_info		*info;
	bool			valid;

	struct resource		*fb_mem;

	int			xres;
	int			yres;

	atomic_t		in_use;
	struct nvmap_client	*user_nvmap;
	struct nvmap_client	*fb_nvmap;

	struct workqueue_struct	*flip_wq;
};

struct tegra_fb_flip_win {
	struct tegra_fb_windowattr	attr;
	struct nvmap_handle_ref		*handle;
	dma_addr_t			phys_addr;
};

struct tegra_fb_flip_data {
	struct work_struct		work;
	struct tegra_fb_info		*fb;
	struct tegra_fb_flip_win	win[TEGRA_FB_FLIP_N_WINDOWS];
	u32				syncpt_max;
};

/* palette array used by the fbcon */
static u32 pseudo_palette[16];

static int tegra_fb_open(struct fb_info *info, int user)
{
	struct tegra_fb_info *tegra_fb = info->par;

	atomic_inc(&tegra_fb->in_use);

	return 0;
}

static int tegra_fb_release(struct fb_info *info, int user)
{
	struct tegra_fb_info *tegra_fb = info->par;
	struct fb_var_screeninfo *var = &info->var;

	if (atomic_dec_return(&tegra_fb->in_use))
		return 0;

	flush_workqueue(tegra_fb->flip_wq);

	if (tegra_fb->win->cur_handle) {
		nvmap_unpin(tegra_fb->fb_nvmap, tegra_fb->win->cur_handle);
		nvmap_free(tegra_fb->fb_nvmap, tegra_fb->win->cur_handle);

		tegra_fb->win->cur_handle = NULL;

		tegra_fb->win->x = 0;
		tegra_fb->win->y = 0;
		tegra_fb->win->w = var->xres;
		tegra_fb->win->h = var->yres;
		tegra_fb->win->out_x = 0;
		tegra_fb->win->out_y = 0;
		tegra_fb->win->out_w = var->xres;
		tegra_fb->win->out_h = var->yres;
		tegra_fb->win->flags = TEGRA_WIN_FLAG_ENABLED;
	}

	if (tegra_fb->user_nvmap) {
		nvmap_client_put(tegra_fb->user_nvmap);
		tegra_fb->user_nvmap = NULL;
	}

	return 0;
}

static int tegra_fb_check_var(struct fb_var_screeninfo *var,
			      struct fb_info *info)
{
	if ((var->yres * var->xres * var->bits_per_pixel / 8 * 2) >
	    info->screen_size)
		return -EINVAL;

	/* double yres_virtual to allow double buffering through pan_display */
	var->yres_virtual = var->yres * 2;

	return 0;
}

static int tegra_fb_set_par(struct fb_info *info)
{
	struct tegra_fb_info *tegra_fb = info->par;
	struct fb_var_screeninfo *var = &info->var;

	if (var->bits_per_pixel) {
		/* we only support RGB ordering for now */
		switch (var->bits_per_pixel) {
		case 32:
			var->red.offset = 0;
			var->red.length = 8;
			var->green.offset = 8;
			var->green.length = 8;
			var->blue.offset = 16;
			var->blue.length = 8;
			var->transp.offset = 24;
			var->transp.length = 8;
			tegra_fb->win->fmt = TEGRA_WIN_FMT_R8G8B8A8;
			break;
		case 16:
			var->red.offset = 11;
			var->red.length = 5;
			var->green.offset = 5;
			var->green.length = 6;
			var->blue.offset = 0;
			var->blue.length = 5;
			tegra_fb->win->fmt = TEGRA_WIN_FMT_B5G6R5;
			break;

		default:
			return -EINVAL;
		}
		info->fix.line_length = var->xres * var->bits_per_pixel / 8;
		tegra_fb->win->stride = info->fix.line_length;
		tegra_fb->win->stride_uv = 0;
		tegra_fb->win->offset_u = 0;
		tegra_fb->win->offset_v = 0;
	}

	if (var->pixclock) {
		struct tegra_dc_mode mode;

		info->mode = (struct fb_videomode *)
			fb_find_best_mode(var, &info->modelist);
		if (!info->mode) {
			dev_warn(&tegra_fb->ndev->dev, "can't match video mode\n");
			return -EINVAL;
		}

		mode.pclk = PICOS2KHZ(info->mode->pixclock) * 1000;
		mode.h_ref_to_sync = 1;
		mode.v_ref_to_sync = 1;
		mode.h_sync_width = info->mode->hsync_len;
		mode.v_sync_width = info->mode->vsync_len;
		mode.h_back_porch = info->mode->left_margin;
		mode.v_back_porch = info->mode->upper_margin;
		mode.h_active = info->mode->xres;
		mode.v_active = info->mode->yres;
		mode.h_front_porch = info->mode->right_margin;
		mode.v_front_porch = info->mode->lower_margin;
		/*
		 * only enable stereo if the mode supports it and
		 * client requests it
		 */
		mode.stereo_mode = !!(var->vmode & info->mode->vmode &
					FB_VMODE_STEREO_FRAME_PACK);

		if (mode.stereo_mode) {
			mode.pclk *= 2;
			/* total v_active = yres*2 + activespace */
			mode.v_active = info->mode->yres*2 +
					info->mode->vsync_len +
					info->mode->upper_margin +
					info->mode->lower_margin;
		}

		mode.flags = 0;

		if (!(info->mode->sync & FB_SYNC_HOR_HIGH_ACT))
			mode.flags |= TEGRA_DC_MODE_FLAG_NEG_H_SYNC;

		if (!(info->mode->sync & FB_SYNC_VERT_HIGH_ACT))
			mode.flags |= TEGRA_DC_MODE_FLAG_NEG_V_SYNC;

		tegra_dc_set_mode(tegra_fb->win->dc, &mode);

		tegra_fb->win->w = info->mode->xres;
		tegra_fb->win->h = info->mode->yres;
		tegra_fb->win->out_w = info->mode->xres;
		tegra_fb->win->out_h = info->mode->yres;
	}
	return 0;
}

static int tegra_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
	unsigned blue, unsigned transp, struct fb_info *info)
{
	struct fb_var_screeninfo *var = &info->var;

	if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
	    info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
		u32 v;

		if (regno >= 16)
			return -EINVAL;

		red = (red >> (16 - info->var.red.length));
		green = (green >> (16 - info->var.green.length));
		blue = (blue >> (16 - info->var.blue.length));

		v = (red << var->red.offset) |
			(green << var->green.offset) |
			(blue << var->blue.offset);

		((u32 *)info->pseudo_palette)[regno] = v;
	}

	return 0;
}

static int tegra_fb_blank(int blank, struct fb_info *info)
{
	struct tegra_fb_info *tegra_fb = info->par;

	switch (blank) {
	case FB_BLANK_UNBLANK:
		dev_dbg(&tegra_fb->ndev->dev, "unblank\n");
		tegra_dc_enable(tegra_fb->win->dc);
		return 0;

	case FB_BLANK_POWERDOWN:
		dev_dbg(&tegra_fb->ndev->dev, "blank\n");
		flush_workqueue(tegra_fb->flip_wq);
		tegra_dc_disable(tegra_fb->win->dc);
		return 0;

	default:
		return -ENOTTY;
	}
}

void tegra_fb_suspend(struct tegra_fb_info *tegra_fb)
{
	flush_workqueue(tegra_fb->flip_wq);
}


static int tegra_fb_pan_display(struct fb_var_screeninfo *var,
				struct fb_info *info)
{
	struct tegra_fb_info *tegra_fb = info->par;
	char __iomem *flush_start;
	char __iomem *flush_end;
	u32 addr;

	if (!tegra_fb->win->cur_handle) {
		flush_start = info->screen_base + (var->yoffset * info->fix.line_length);
		flush_end = flush_start + (var->yres * info->fix.line_length);

		info->var.xoffset = var->xoffset;
		info->var.yoffset = var->yoffset;

		addr = info->fix.smem_start + (var->yoffset * info->fix.line_length) +
			(var->xoffset * (var->bits_per_pixel/8));

		tegra_fb->win->phys_addr = addr;
		/* TODO: update virt_addr */

		tegra_dc_update_windows(&tegra_fb->win, 1);
		tegra_dc_sync_windows(&tegra_fb->win, 1);
	}

	return 0;
}

static void tegra_fb_fillrect(struct fb_info *info,
			      const struct fb_fillrect *rect)
{
	cfb_fillrect(info, rect);
}

static void tegra_fb_copyarea(struct fb_info *info,
			      const struct fb_copyarea *region)
{
	cfb_copyarea(info, region);
}

static void tegra_fb_imageblit(struct fb_info *info,
			       const struct fb_image *image)
{
	cfb_imageblit(info, image);
}

/* TODO: implement ALLOC, FREE, BLANK ioctls */

static int tegra_fb_set_nvmap_fd(struct tegra_fb_info *tegra_fb, int fd)
{
	struct nvmap_client *nvmap = NULL;

	if (fd < 0)
		return -EINVAL;

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

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

	tegra_fb->user_nvmap = nvmap;

	return 0;
}

static int tegra_fb_pin_window(struct tegra_fb_info *tegra_fb,
			       struct tegra_fb_flip_win *flip_win)
{
	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(tegra_fb->user_nvmap, buff_id);
	if (win_handle == NULL) {
		dev_err(&tegra_fb->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(tegra_fb->fb_nvmap, buff_id);
	nvmap_handle_put(win_handle);

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

	flip_win->handle = win_dupe;

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

	return 0;
}

static int tegra_fb_set_windowattr(struct tegra_fb_info *tegra_fb,
				   struct tegra_dc_win *win,
				   const struct tegra_fb_flip_win *flip_win)
{
	int xres, yres;
	if (flip_win->handle == NULL) {
		win->flags = 0;
		win->cur_handle = NULL;
		return 0;
	}

	xres = tegra_fb->win->dc->mode.h_active;
	yres = tegra_fb->win->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;
	if (flip_win->attr.flags & TEGRA_FB_WIN_FLAG_TILED)
		win->flags |= TEGRA_WIN_FLAG_TILED;

	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;
	win->offset = flip_win->attr.offset;
	win->offset_u = flip_win->attr.offset_u;
	win->offset_v = flip_win->attr.offset_v;
	win->stride = flip_win->attr.stride;
	win->stride_uv = flip_win->attr.stride_uv;

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


	return 0;
}

static void tegra_fb_flip_worker(struct work_struct *work)
{
	struct tegra_fb_flip_data *data =
		container_of(work, struct tegra_fb_flip_data, work);
	struct tegra_fb_info *tegra_fb = data->fb;
	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_fb_flip_data, work);

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

		if (!win)
			continue;

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

		tegra_fb_set_windowattr(tegra_fb, 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(&tegra_fb->ndev->host->syncpt,
					   flip_win->attr.pre_syncpt_id,
					   flip_win->attr.pre_syncpt_val,
					   msecs_to_jiffies(500));
#endif
	}

	tegra_dc_update_windows(wins, nr_win);
	/* TODO: implement swapinterval here */
	tegra_dc_sync_windows(wins, nr_win);

	tegra_dc_incr_syncpt_min(tegra_fb->win->dc, data->syncpt_max);

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

	kfree(data);
}

static int tegra_fb_flip(struct tegra_fb_info *tegra_fb,
			 struct tegra_fb_flip_args *args)
{
	struct tegra_fb_flip_data *data;
	struct tegra_fb_flip_win *flip_win;
	u32 syncpt_max;
	int i, err;

	if (WARN_ON(!tegra_fb->user_nvmap))
		return -EFAULT;

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

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

	INIT_WORK(&data->work, tegra_fb_flip_worker);
	data->fb = tegra_fb;

	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));

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

	syncpt_max = tegra_dc_incr_syncpt_max(tegra_fb->win->dc);
	data->syncpt_max = syncpt_max;

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

	args->post_syncpt_val = syncpt_max;
	args->post_syncpt_id = tegra_dc_get_syncpt_id(tegra_fb->win->dc);

	return 0;

surf_err:
	while (i--) {
		if (data->win[i].handle) {
			nvmap_unpin(tegra_fb->fb_nvmap,
				    data->win[i].handle);
			nvmap_free(tegra_fb->fb_nvmap,
				   data->win[i].handle);
		}
	}
	kfree(data);
	return err;
}

static int tegra_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
{
	struct tegra_fb_info *tegra_fb = info->par;
	struct tegra_dc_win *win = tegra_fb->win;
	unsigned long size = vma->vm_end - vma->vm_start;
	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;

	if (offset + size > win->size)
		return -EINVAL;

	offset += win->phys_addr + win->offset;

	if (remap_pfn_range(vma, vma->vm_start, offset >> PAGE_SHIFT,
			    size, PAGE_READONLY))
		return -EAGAIN;

	vma->vm_flags |= VM_RESERVED;
	return 0;
}

/* TODO: implement private window ioctls to set overlay x,y */

static int tegra_fb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
{
	struct tegra_fb_info *tegra_fb = info->par;
	struct tegra_fb_flip_args flip_args;
	struct tegra_fb_modedb modedb;
	struct fb_modelist *modelist;
	int i;
	int fd;
	int ret;

	switch (cmd) {
	case FBIO_TEGRA_SET_NVMAP_FD:
		if (copy_from_user(&fd, (void __user *)arg, sizeof(fd)))
			return -EFAULT;

		return tegra_fb_set_nvmap_fd(tegra_fb, fd);

	case FBIO_TEGRA_FLIP:
		if (copy_from_user(&flip_args, (void __user *)arg, sizeof(flip_args)))
			return -EFAULT;

		ret = tegra_fb_flip(tegra_fb, &flip_args);

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

		return ret;

	case FBIO_TEGRA_GET_MODEDB:
		if (copy_from_user(&modedb, (void __user *)arg, sizeof(modedb)))
			return -EFAULT;

		i = 0;
		list_for_each_entry(modelist, &info->modelist, list) {
			struct fb_var_screeninfo var;

			if (i >= modedb.modedb_len)
				break;

			/* fb_videomode_to_var doesn't fill out all the members
			   of fb_var_screeninfo */
			memset(&var, 0x0, sizeof(var));

			fb_videomode_to_var(&var, &modelist->mode);

			if (copy_to_user((void __user *)&modedb.modedb[i],
					 &var, sizeof(var)))
				return -EFAULT;
			i++;

			if (var.vmode & FB_VMODE_STEREO_MASK) {
				if (i >= modedb.modedb_len)
					break;
				var.vmode &= ~FB_VMODE_STEREO_MASK;
				if (copy_to_user(
					(void __user *)&modedb.modedb[i],
					 &var, sizeof(var)))
					return -EFAULT;
				i++;
			}
		}
		modedb.modedb_len = i;

		if (copy_to_user((void __user *)arg, &modedb, sizeof(modedb)))
			return -EFAULT;
		break;

	default:
		return -ENOTTY;
	}

	return 0;
}

static struct fb_ops tegra_fb_ops = {
	.owner = THIS_MODULE,
	.fb_open = tegra_fb_open,
	.fb_release = tegra_fb_release,
	.fb_check_var = tegra_fb_check_var,
	.fb_set_par = tegra_fb_set_par,
	.fb_setcolreg = tegra_fb_setcolreg,
	.fb_blank = tegra_fb_blank,
	.fb_pan_display = tegra_fb_pan_display,
	.fb_fillrect = tegra_fb_fillrect,
	.fb_copyarea = tegra_fb_copyarea,
	.fb_imageblit = tegra_fb_imageblit,
	.fb_mmap = tegra_fb_mmap,
	.fb_ioctl = tegra_fb_ioctl,
};

void tegra_fb_update_monspecs(struct tegra_fb_info *fb_info,
			      struct fb_monspecs *specs,
			      bool (*mode_filter)(const struct tegra_dc *dc, struct fb_videomode *mode))
{
	struct fb_event event;
	struct fb_modelist *m;
	int i;

	mutex_lock(&fb_info->info->lock);
	fb_destroy_modedb(fb_info->info->monspecs.modedb);

	fb_destroy_modelist(&fb_info->info->modelist);

	if (specs == NULL) {
		struct tegra_dc_mode mode;
		memset(&fb_info->info->monspecs, 0x0,
		       sizeof(fb_info->info->monspecs));
		memset(&mode, 0x0, sizeof(mode));
		tegra_dc_set_mode(fb_info->win->dc, &mode);
		mutex_unlock(&fb_info->info->lock);
		return;
	}

	memcpy(&fb_info->info->monspecs, specs,
	       sizeof(fb_info->info->monspecs));

	for (i = 0; i < specs->modedb_len; i++) {
		if (mode_filter) {
			if (mode_filter(fb_info->win->dc, &specs->modedb[i]))
				fb_add_videomode(&specs->modedb[i],
						 &fb_info->info->modelist);
		} else {
			fb_add_videomode(&specs->modedb[i],
					 &fb_info->info->modelist);
		}
	}

	if (list_empty(&fb_info->info->modelist)) {
		struct tegra_dc_mode mode;
		memset(&fb_info->info->var, 0x0, sizeof(fb_info->info->var));
		memset(&mode, 0x0, sizeof(mode));
		tegra_dc_set_mode(fb_info->win->dc, &mode);
	} else {
		/* in case the first mode was not matched */
		m = list_first_entry(&fb_info->info->modelist, struct fb_modelist, list);
		m->mode.flag |= FB_MODE_IS_FIRST;
		fb_info->info->mode = (struct fb_videomode *)
			fb_find_best_display(specs, &fb_info->info->modelist);

		/* fb_videomode_to_var doesn't fill out all the members
		   of fb_var_screeninfo */
		memset(&fb_info->info->var, 0x0, sizeof(fb_info->info->var));

		fb_videomode_to_var(&fb_info->info->var, fb_info->info->mode);
		tegra_fb_set_par(fb_info->info);
	}

	event.info = fb_info->info;
	fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
	mutex_unlock(&fb_info->info->lock);
}

struct tegra_fb_info *tegra_fb_register(struct nvhost_device *ndev,
					struct tegra_dc *dc,
					struct tegra_fb_data *fb_data,
					struct resource *fb_mem)
{
	struct tegra_dc_win *win;
	struct fb_info *info;
	struct tegra_fb_info *tegra_fb;
	void __iomem *fb_base = NULL;
	unsigned long fb_size = 0;
	unsigned long fb_phys = 0;
	int ret = 0;

	win = tegra_dc_get_window(dc, fb_data->win);
	if (!win) {
		dev_err(&ndev->dev, "dc does not have a window at index %d\n",
			fb_data->win);
		return ERR_PTR(-ENOENT);
	}

	info = framebuffer_alloc(sizeof(struct tegra_fb_info), &ndev->dev);
	if (!info) {
		ret = -ENOMEM;
		goto err;
	}

	tegra_fb = info->par;
	tegra_fb->win = win;
	tegra_fb->ndev = ndev;
	tegra_fb->fb_mem = fb_mem;
	tegra_fb->xres = fb_data->xres;
	tegra_fb->yres = fb_data->yres;
	tegra_fb->fb_nvmap = nvmap_create_client(nvmap_dev, "tegra-fb");
	if (!tegra_fb->fb_nvmap) {
		dev_err(&ndev->dev, "couldn't create nvmap client\n");
		ret = -ENOMEM;
		goto err_free;
	}
	atomic_set(&tegra_fb->in_use, 0);

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

	if (fb_mem) {
		fb_size = resource_size(fb_mem);
		fb_phys = fb_mem->start;
		fb_base = ioremap_nocache(fb_phys, fb_size);
		if (!fb_base) {
			dev_err(&ndev->dev, "fb can't be mapped\n");
			ret = -EBUSY;
			goto err_put_client;
		}
		tegra_fb->valid = true;
	}

	info->fbops = &tegra_fb_ops;
	info->pseudo_palette = pseudo_palette;
	info->screen_base = fb_base;
	info->screen_size = fb_size;

	strlcpy(info->fix.id, "tegra_fb", sizeof(info->fix.id));
	info->fix.type		= FB_TYPE_PACKED_PIXELS;
	info->fix.visual	= FB_VISUAL_TRUECOLOR;
	info->fix.xpanstep	= 1;
	info->fix.ypanstep	= 1;
	info->fix.accel		= FB_ACCEL_NONE;
	info->fix.smem_start	= fb_phys;
	info->fix.smem_len	= fb_size;

	info->var.xres			= fb_data->xres;
	info->var.yres			= fb_data->yres;
	info->var.xres_virtual		= fb_data->xres;
	info->var.yres_virtual		= fb_data->yres * 2;
	info->var.bits_per_pixel	= fb_data->bits_per_pixel;
	info->var.activate		= FB_ACTIVATE_VBL;
	info->var.height		= tegra_dc_get_out_height(dc);
	info->var.width			= tegra_dc_get_out_width(dc);
	info->var.pixclock		= 0;
	info->var.left_margin		= 0;
	info->var.right_margin		= 0;
	info->var.upper_margin		= 0;
	info->var.lower_margin		= 0;
	info->var.hsync_len		= 0;
	info->var.vsync_len		= 0;
	info->var.vmode			= FB_VMODE_NONINTERLACED;

	win->x = 0;
	win->y = 0;
	win->w = fb_data->xres;
	win->h = fb_data->yres;
	/* TODO: set to output res dc */
	win->out_x = 0;
	win->out_y = 0;
	win->out_w = fb_data->xres;
	win->out_h = fb_data->yres;
	win->z = 0;
	win->phys_addr = fb_phys;
	win->virt_addr = fb_base;
	win->offset_u = 0;
	win->offset_v = 0;
	win->stride = fb_data->xres * fb_data->bits_per_pixel / 8;
	win->stride_uv = 0;
	win->flags = TEGRA_WIN_FLAG_ENABLED;

	if (fb_mem)
		tegra_fb_set_par(info);

	if (register_framebuffer(info)) {
		dev_err(&ndev->dev, "failed to register framebuffer\n");
		ret = -ENODEV;
		goto err_iounmap_fb;
	}

	tegra_fb->info = info;

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

	if (fb_data->flags & TEGRA_FB_FLIP_ON_PROBE) {
		tegra_dc_update_windows(&tegra_fb->win, 1);
		tegra_dc_sync_windows(&tegra_fb->win, 1);
	}

	return tegra_fb;

err_iounmap_fb:
	iounmap(fb_base);
err_put_client:
	nvmap_client_put(tegra_fb->fb_nvmap);
err_delete_wq:
	destroy_workqueue(tegra_fb->flip_wq);
err_free:
	framebuffer_release(info);
err:
	return ERR_PTR(ret);
}

void tegra_fb_unregister(struct tegra_fb_info *fb_info)
{
	struct fb_info *info = fb_info->info;

	if (fb_info->win->cur_handle) {
		nvmap_unpin(fb_info->fb_nvmap, fb_info->win->cur_handle);
		nvmap_free(fb_info->fb_nvmap, fb_info->win->cur_handle);
	}

	if (fb_info->fb_nvmap)
		nvmap_client_put(fb_info->fb_nvmap);

	unregister_framebuffer(info);

	flush_workqueue(fb_info->flip_wq);
	destroy_workqueue(fb_info->flip_wq);

	iounmap(info->screen_base);
	framebuffer_release(info);
}