aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/video/omap2/dsscomp/device.c
blob: 80cc21b7ce24a44826d7629cfb74c7032463c61a (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
/*
 * linux/drivers/video/omap2/dsscomp/device.c
 *
 * DSS Composition file device and ioctl support
 *
 * Copyright (C) 2011 Texas Instruments, Inc
 * Author: Lajos Molnar <molnar@ti.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#define DEBUG

#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/file.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/anon_inodes.h>
#include <linux/list.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/sched.h>
#include <linux/syscalls.h>

#define MODULE_NAME	"dsscomp"

#include <video/omapdss.h>
#include <video/dsscomp.h>
#include <plat/dsscomp.h>
#include "dsscomp.h"

#include <linux/debugfs.h>

static DECLARE_WAIT_QUEUE_HEAD(waitq);
static DEFINE_MUTEX(wait_mtx);

static u32 hwc_virt_to_phys(u32 arg)
{
	pmd_t *pmd;
	pte_t *ptep;

	pgd_t *pgd = pgd_offset(current->mm, arg);
	if (pgd_none(*pgd) || pgd_bad(*pgd))
		return 0;

	pmd = pmd_offset(pgd, arg);
	if (pmd_none(*pmd) || pmd_bad(*pmd))
		return 0;

	ptep = pte_offset_map(pmd, arg);
	if (ptep && pte_present(*ptep))
		return (PAGE_MASK & *ptep) | (~PAGE_MASK & arg);

	return 0;
}

/*
 * ===========================================================================
 *		WAIT OPERATIONS
 * ===========================================================================
 */

static void sync_drop(struct dsscomp_sync_obj *sync)
{
	if (sync && atomic_dec_and_test(&sync->refs)) {
		if (debug & DEBUG_WAITS)
			pr_info("free sync [%p]\n", sync);

		kfree(sync);
	}
}

static int sync_setup(const char *name, const struct file_operations *fops,
				struct dsscomp_sync_obj *sync, int flags)
{
	if (!sync)
		return -ENOMEM;

	sync->refs.counter = 1;
	sync->fd = anon_inode_getfd(name, fops, sync, flags);
	return sync->fd < 0 ? sync->fd : 0;
}

static int sync_finalize(struct dsscomp_sync_obj *sync, int r)
{
	if (sync) {
		if (r < 0)
			/* delete sync object on failure */
			sys_close(sync->fd);
		else
			/* return file descriptor on success */
			r = sync->fd;
	}
	return r;
}

/* wait for programming or release of a composition */
int dsscomp_wait(struct dsscomp_sync_obj *sync, enum dsscomp_wait_phase phase,
								int timeout)
{
	mutex_lock(&wait_mtx);
	if (debug & DEBUG_WAITS)
		pr_info("wait %s on [%p]\n",
			phase == DSSCOMP_WAIT_DISPLAYED ? "display" :
			phase == DSSCOMP_WAIT_PROGRAMMED ? "program" :
			"release", sync);

	if (sync->state < phase) {
		mutex_unlock(&wait_mtx);

		timeout = wait_event_interruptible_timeout(waitq,
			sync->state >= phase, timeout);
		if (debug & DEBUG_WAITS)
			pr_info("wait over [%p]: %s %d\n", sync,
				 timeout < 0 ? "signal" :
				 timeout > 0 ? "ok" : "timeout",
				 timeout);
		if (timeout <= 0)
			return timeout ? : -ETIME;

		mutex_lock(&wait_mtx);
	}
	mutex_unlock(&wait_mtx);

	return 0;
}
EXPORT_SYMBOL(dsscomp_wait);

static void dsscomp_queue_cb(void *data, int status)
{
	struct dsscomp_sync_obj *sync = data;
	enum dsscomp_wait_phase phase =
		status == DSS_COMPLETION_PROGRAMMED ? DSSCOMP_WAIT_PROGRAMMED :
		status == DSS_COMPLETION_DISPLAYED ? DSSCOMP_WAIT_DISPLAYED :
		DSSCOMP_WAIT_RELEASED, old_phase;

	mutex_lock(&wait_mtx);
	old_phase = sync->state;
	if (old_phase < phase)
		sync->state = phase;
	mutex_unlock(&wait_mtx);

	if (status & DSS_COMPLETION_RELEASED)
		sync_drop(sync);
	if (old_phase < phase)
		wake_up_interruptible_sync(&waitq);
}

static int sync_release(struct inode *inode, struct file *filp)
{
	struct dsscomp_sync_obj *sync = filp->private_data;
	sync_drop(sync);
	return 0;
}

static long sync_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	int r = 0;
	struct dsscomp_sync_obj *sync = filp->private_data;
	void __user *ptr = (void __user *)arg;

	switch (cmd) {
	case DSSCIOC_WAIT:
	{
		struct dsscomp_wait_data wd;
		r = copy_from_user(&wd, ptr, sizeof(wd)) ? :
		    dsscomp_wait(sync, wd.phase,
					usecs_to_jiffies(wd.timeout_us));
		break;
	}
	default:
		r = -EINVAL;
	}
	return r;
}

static const struct file_operations sync_fops = {
	.owner		= THIS_MODULE,
	.release	= sync_release,
	.unlocked_ioctl = sync_ioctl,
};

static long setup_mgr(struct dsscomp_dev *cdev,
					struct dsscomp_setup_mgr_data *d)
{
	int i, r;
	struct omap_dss_device *dev;
	struct omap_overlay_manager *mgr;
	dsscomp_t comp;
	struct dsscomp_sync_obj *sync = NULL;

	dump_comp_info(cdev, d, "queue");
	for (i = 0; i < d->num_ovls; i++)
		dump_ovl_info(cdev, d->ovls + i);

	/* verify display is valid and connected */
	if (d->mgr.ix >= cdev->num_displays)
		return -EINVAL;
	dev = cdev->displays[d->mgr.ix];
	if (!dev)
		return -EINVAL;
	mgr = dev->manager;
	if (!mgr)
		return -ENODEV;

	comp = dsscomp_new(mgr);
	if (IS_ERR(comp))
		return PTR_ERR(comp);

	/* swap red & blue if requested */
	if (d->mgr.swap_rb) {
		swap_rb_in_mgr_info(&d->mgr);
		for (i = 0; i < d->num_ovls; i++)
			swap_rb_in_ovl_info(d->ovls + i);
	}

	r = dsscomp_set_mgr(comp, &d->mgr);

	for (i = 0; i < d->num_ovls; i++) {
		struct dss2_ovl_info *oi = d->ovls + i;
		u32 addr = (u32) oi->address;

		/* convert addresses to user space */
		if (oi->cfg.color_mode == OMAP_DSS_COLOR_NV12)
			oi->uv = hwc_virt_to_phys(addr +
					oi->cfg.height * oi->cfg.stride);
		oi->ba = hwc_virt_to_phys(addr);

		r = r ? : dsscomp_set_ovl(comp, oi);
	}

	r = r ? : dsscomp_setup(comp, d->mode, d->win);

	/* create sync object */
	if (d->get_sync_obj) {
		sync = kzalloc(sizeof(*sync), GFP_KERNEL);
		r = sync_setup("dsscomp_sync", &sync_fops, sync, O_RDONLY);
		if (sync && (debug & DEBUG_WAITS))
			dev_info(DEV(cdev), "new sync [%p] on #%d\n", sync,
								sync->fd);
		if (r)
			sync_drop(sync);
	}

	/* drop composition if failed to create */
	if (r) {
		dsscomp_drop(comp);
		return r;
	}

	if (sync) {
		sync->refs.counter++;
		comp->extra_cb = dsscomp_queue_cb;
		comp->extra_cb_data = sync;
	}
	if (d->mode & DSSCOMP_SETUP_APPLY)
		r = dsscomp_delayed_apply(comp);

	/* delete sync object if failed to apply or create file */
	if (sync) {
		r = sync_finalize(sync, r);
		if (r < 0)
			sync_drop(sync);
	}
	return r;
}

static long query_display(struct dsscomp_dev *cdev,
					struct dsscomp_display_info *dis)
{
	struct omap_dss_device *dev;
	struct omap_overlay_manager *mgr;
	int i;

	/* get display */
	if (dis->ix >= cdev->num_displays)
		return -EINVAL;
	dev = cdev->displays[dis->ix];
	if (!dev)
		return -EINVAL;
	mgr = dev->manager;

	/* fill out display information */
	dis->channel = dev->channel;
	dis->enabled = (dev->state == OMAP_DSS_DISPLAY_SUSPENDED) ?
		dev->activate_after_resume :
		(dev->state == OMAP_DSS_DISPLAY_ACTIVE);
	dis->overlays_available = 0;
	dis->overlays_owned = 0;
#if 0
	dis->s3d_info = dev->panel.s3d_info;
#endif
	dis->state = dev->state;
	dis->timings = dev->panel.timings;

	dis->width_in_mm = DIV_ROUND_CLOSEST(dev->panel.width_in_um, 1000);
	dis->height_in_mm = DIV_ROUND_CLOSEST(dev->panel.height_in_um, 1000);

	/* find all overlays available for/owned by this display */
	for (i = 0; i < cdev->num_ovls && dis->enabled; i++) {
		if (cdev->ovls[i]->manager == mgr)
			dis->overlays_owned |= 1 << i;
		else if (!cdev->ovls[i]->info.enabled)
			dis->overlays_available |= 1 << i;
	}
	dis->overlays_available |= dis->overlays_owned;

	/* fill out manager information */
	if (mgr) {
		dis->mgr.alpha_blending = mgr->info.alpha_enabled;
		dis->mgr.default_color = mgr->info.default_color;
#if 0
		dis->mgr.interlaced = !strcmp(dev->name, "hdmi") &&
							is_hdmi_interlaced()
#else
		dis->mgr.interlaced =  0;
#endif
		dis->mgr.trans_enabled = mgr->info.trans_enabled;
		dis->mgr.trans_key = mgr->info.trans_key;
		dis->mgr.trans_key_type = mgr->info.trans_key_type;
	} else {
		/* display is disabled if it has no manager */
		memset(&dis->mgr, 0, sizeof(dis->mgr));
	}
	dis->mgr.ix = dis->ix;

	if (dis->modedb_len && dev->driver->get_modedb)
		dis->modedb_len = dev->driver->get_modedb(dev,
			(struct fb_videomode *) dis->modedb, dis->modedb_len);
	return 0;
}

static long check_ovl(struct dsscomp_dev *cdev,
					struct dsscomp_check_ovl_data *chk)
{
	/* for now return all overlays as possible */
	return (1 << cdev->num_ovls) - 1;
}

static long setup_display(struct dsscomp_dev *cdev,
				struct dsscomp_setup_display_data *dis)
{
	struct omap_dss_device *dev;

	/* get display */
	if (dis->ix >= cdev->num_displays)
		return -EINVAL;
	dev = cdev->displays[dis->ix];
	if (!dev)
		return -EINVAL;

	if (dev->driver->set_mode)
		return dev->driver->set_mode(dev,
					(struct fb_videomode *) &dis->mode);
	else
		return 0;
}

static void fill_cache(struct dsscomp_dev *cdev)
{
	unsigned long i;
	struct omap_dss_device *dssdev = NULL;

	cdev->num_ovls = min(omap_dss_get_num_overlays(), MAX_OVERLAYS);
	for (i = 0; i < cdev->num_ovls; i++)
		cdev->ovls[i] = omap_dss_get_overlay(i);

	cdev->num_mgrs = min(omap_dss_get_num_overlay_managers(), MAX_MANAGERS);
	for (i = 0; i < cdev->num_mgrs; i++)
		cdev->mgrs[i] = omap_dss_get_overlay_manager(i);

	for_each_dss_dev(dssdev) {
		const char *name = dev_name(&dssdev->dev);
		if (strncmp(name, "display", 7) ||
		    strict_strtoul(name + 7, 10, &i) ||
		    i >= MAX_DISPLAYS)
			continue;

		if (cdev->num_displays <= i)
			cdev->num_displays = i + 1;

		cdev->displays[i] = dssdev;
		dev_dbg(DEV(cdev), "display%lu=%s\n", i, dssdev->driver_name);

		cdev->state_notifiers[i].notifier_call = dsscomp_state_notifier;
		blocking_notifier_chain_register(&dssdev->state_notifiers,
						cdev->state_notifiers + i);
	}
	dev_info(DEV(cdev), "found %d displays and %d overlays\n",
				cdev->num_displays, cdev->num_ovls);
}

static long comp_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	int r = 0;
	struct miscdevice *dev = filp->private_data;
	struct dsscomp_dev *cdev = container_of(dev, struct dsscomp_dev, dev);
	void __user *ptr = (void __user *)arg;

	union {
		struct {
			struct dsscomp_setup_mgr_data set;
			struct dss2_ovl_info ovl[MAX_OVERLAYS];
		} m;
		struct dsscomp_setup_dispc_data dispc;
		struct dsscomp_display_info dis;
		struct dsscomp_check_ovl_data chk;
		struct dsscomp_setup_display_data sdis;
	} u;

	dsscomp_gralloc_init(cdev);

	switch (cmd) {
	case DSSCIOC_SETUP_MGR:
	{
		r = copy_from_user(&u.m.set, ptr, sizeof(u.m.set)) ? :
		    u.m.set.num_ovls >= ARRAY_SIZE(u.m.ovl) ? -EINVAL :
		    copy_from_user(&u.m.ovl,
				(void __user *)arg + sizeof(u.m.set),
				sizeof(*u.m.ovl) * u.m.set.num_ovls) ? :
		    setup_mgr(cdev, &u.m.set);
		break;
	}
	case DSSCIOC_SETUP_DISPC:
	{
		r = copy_from_user(&u.dispc, ptr, sizeof(u.dispc)) ? :
		    dsscomp_gralloc_queue_ioctl(&u.dispc);
		break;
	}
	case DSSCIOC_QUERY_DISPLAY:
	{
		struct dsscomp_display_info *dis = NULL;
		r = copy_from_user(&u.dis, ptr, sizeof(u.dis));
		if (!r) {
			/* impose a safe limit on modedb_len to prevent
			 * wrap around/overflow calculation of the alloced
			 * size that would make it smaller than
			 * struct dsscomp_display_info and cause heap
			 * corruption.
			 */
			u.dis.modedb_len = clamp_val(u.dis.modedb_len, 0, 256);

			dis = kzalloc(sizeof(*dis->modedb) * u.dis.modedb_len +
						sizeof(*dis), GFP_KERNEL);
		}
		if (dis) {
			*dis = u.dis;
			r = query_display(cdev, dis) ? :
			    copy_to_user(ptr, dis, sizeof(*dis) +
				sizeof(*dis->modedb) * dis->modedb_len);
			kfree(dis);
		} else {
			r = r ? : -ENOMEM;
		}
		break;
	}
	case DSSCIOC_CHECK_OVL:
	{
		r = copy_from_user(&u.chk, ptr, sizeof(u.chk)) ? :
		    check_ovl(cdev, &u.chk);
		break;
	}
	case DSSCIOC_SETUP_DISPLAY:
	{
		r = copy_from_user(&u.sdis, ptr, sizeof(u.sdis)) ? :
		    setup_display(cdev, &u.sdis);
	}
	default:
		r = -EINVAL;
	}
	return r;
}

/* must implement open for filp->private_data to be filled */
static int comp_open(struct inode *inode, struct file *filp)
{
	return 0;
}

static const struct file_operations comp_fops = {
	.owner		= THIS_MODULE,
	.open		= comp_open,
	.unlocked_ioctl = comp_ioctl,
};

static int dsscomp_debug_show(struct seq_file *s, void *unused)
{
	void (*fn)(struct seq_file *s) = s->private;
	fn(s);
	return 0;
}

static int dsscomp_debug_open(struct inode *inode, struct file *file)
{
	return single_open(file, dsscomp_debug_show, inode->i_private);
}

static const struct file_operations dsscomp_debug_fops = {
	.open           = dsscomp_debug_open,
	.read           = seq_read,
	.llseek         = seq_lseek,
	.release        = single_release,
};

static int dsscomp_probe(struct platform_device *pdev)
{
	int ret;
	struct dsscomp_dev *cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
	if (!cdev) {
		pr_err("dsscomp: failed to allocate device.\n");
		return -ENOMEM;
	}
	cdev->dev.minor = MISC_DYNAMIC_MINOR;
	cdev->dev.name = "dsscomp";
	cdev->dev.mode = 0666;
	cdev->dev.fops = &comp_fops;

	ret = misc_register(&cdev->dev);
	if (ret) {
		pr_err("dsscomp: failed to register misc device.\n");
		return ret;
	}
	cdev->dbgfs = debugfs_create_dir("dsscomp", NULL);
	if (IS_ERR_OR_NULL(cdev->dbgfs))
		dev_warn(DEV(cdev), "failed to create debug files.\n");
	else {
		debugfs_create_file("comps", S_IRUGO,
			cdev->dbgfs, dsscomp_dbg_comps, &dsscomp_debug_fops);
		debugfs_create_file("gralloc", S_IRUGO,
			cdev->dbgfs, dsscomp_dbg_gralloc, &dsscomp_debug_fops);
#ifdef CONFIG_DSSCOMP_DEBUG_LOG
		debugfs_create_file("log", S_IRUGO,
			cdev->dbgfs, dsscomp_dbg_events, &dsscomp_debug_fops);
#endif
	}

	platform_set_drvdata(pdev, cdev);

	pr_info("dsscomp: initializing.\n");

	fill_cache(cdev);

	/* initialize queues */
	dsscomp_queue_init(cdev);
	dsscomp_gralloc_init(cdev);

	return 0;
}

static int dsscomp_remove(struct platform_device *pdev)
{
	struct dsscomp_dev *cdev = platform_get_drvdata(pdev);
	misc_deregister(&cdev->dev);
	debugfs_remove_recursive(cdev->dbgfs);
	dsscomp_queue_exit();
	dsscomp_gralloc_exit();
	kfree(cdev);

	return 0;
}

static struct platform_driver dsscomp_pdriver = {
	.probe = dsscomp_probe,
	.remove = dsscomp_remove,
	.driver = { .name = MODULE_NAME, .owner = THIS_MODULE }
};

static struct platform_device dsscomp_pdev = {
	.name = MODULE_NAME,
	.id = -1
};

static int __init dsscomp_init(void)
{
	int err = platform_driver_register(&dsscomp_pdriver);
	if (err)
		return err;

	err = platform_device_register(&dsscomp_pdev);
	if (err)
		platform_driver_unregister(&dsscomp_pdriver);
	return err;
}

static void __exit dsscomp_exit(void)
{
	platform_device_unregister(&dsscomp_pdev);
	platform_driver_unregister(&dsscomp_pdriver);
}

#define DUMP_CHUNK 256
static char dump_buf[64 * 1024];
void dsscomp_kdump(void)
{
	struct seq_file s = {
		.buf = dump_buf,
		.size = sizeof(dump_buf) - 1,
	};
	int i;

	dsscomp_dbg_events(&s);
	dsscomp_dbg_comps(&s);
	dsscomp_dbg_gralloc(&s);

	for (i = 0; i < s.count; i += DUMP_CHUNK) {
		if ((s.count - i) > DUMP_CHUNK) {
			char c = s.buf[i + DUMP_CHUNK];
			s.buf[i + DUMP_CHUNK] = 0;
			pr_cont("%s", s.buf + i);
			s.buf[i + DUMP_CHUNK] = c;
		} else {
			s.buf[s.count] = 0;
			pr_cont("%s", s.buf + i);
		}
	}
}
EXPORT_SYMBOL(dsscomp_kdump);

MODULE_LICENSE("GPL v2");
module_init(dsscomp_init);
module_exit(dsscomp_exit);