summaryrefslogtreecommitdiff
path: root/drivers/video/tegra/host/dev.c
blob: 8b577cc9045fbd351a2c4c203941e8443697f1ba (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
/*
 * drivers/video/tegra/host/dev.c
 *
 * Tegra Graphics Host Driver Entrypoint
 *
 * Copyright (c) 2010, 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 "dev.h"

#include <linux/slab.h>
#include <linux/string.h>
#include <linux/spinlock.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/platform_device.h>
#include <linux/uaccess.h>
#include <linux/file.h>
#include <linux/clk.h>

#include <asm/io.h>

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

#define DRIVER_NAME "tegra_grhost"
#define IFACE_NAME "nvhost"

static int nvhost_major = NVHOST_MAJOR;
static int nvhost_minor = NVHOST_CHANNEL_BASE;

struct nvhost_channel_userctx {
	struct nvhost_channel *ch;
	struct nvhost_hwctx *hwctx;
	u32 syncpt_id;
	u32 syncpt_incrs;
	u32 cmdbufs_pending;
	u32 relocs_pending;
	struct nvmap_handle_ref *gather_mem;
	struct nvhost_op_pair *gathers;
	int num_gathers;
	int pinarray_size;
	struct nvmap_pinarray_elem pinarray[NVHOST_MAX_HANDLES];
	struct nvmap_handle *unpinarray[NVHOST_MAX_HANDLES];
	struct nvmap_client *nvmap;
};

struct nvhost_ctrl_userctx {
	struct nvhost_master *dev;
	u32 mod_locks[NV_HOST1X_NB_MLOCKS];
};

static int nvhost_channelrelease(struct inode *inode, struct file *filp)
{
	struct nvhost_channel_userctx *priv = filp->private_data;

	filp->private_data = NULL;

	nvhost_putchannel(priv->ch, priv->hwctx);

	if (priv->hwctx)
		priv->ch->ctxhandler.put(priv->hwctx);

	if (priv->gathers)
		nvmap_munmap(priv->gather_mem, priv->gathers);

	if (!IS_ERR_OR_NULL(priv->gather_mem))
		nvmap_free(priv->ch->dev->nvmap, priv->gather_mem);

	nvmap_client_put(priv->nvmap);
	kfree(priv);
	return 0;
}

static int nvhost_channelopen(struct inode *inode, struct file *filp)
{
	struct nvhost_channel_userctx *priv;
	struct nvhost_channel *ch;
	size_t gather_size;

	ch = container_of(inode->i_cdev, struct nvhost_channel, cdev);
	ch = nvhost_getchannel(ch);
	if (!ch)
		return -ENOMEM;

	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv) {
		nvhost_putchannel(ch, NULL);
		return -ENOMEM;
	}
	filp->private_data = priv;
	priv->ch = ch;
	gather_size = sizeof(struct nvhost_op_pair) * NVHOST_MAX_GATHERS;
	priv->gather_mem = nvmap_alloc(ch->dev->nvmap, gather_size, 32,
				       NVMAP_HANDLE_CACHEABLE);
	if (IS_ERR(priv->gather_mem))
		goto fail;

	if (ch->ctxhandler.alloc) {
		priv->hwctx = ch->ctxhandler.alloc(ch);
		if (!priv->hwctx)
			goto fail;
	}

	priv->gathers = (struct nvhost_op_pair *)nvmap_mmap(priv->gather_mem);

	return 0;
fail:
	nvhost_channelrelease(inode, filp);
	return -ENOMEM;
}

static void add_gather(struct nvhost_channel_userctx *ctx, int idx,
		       u32 mem_id, u32 words, u32 offset)
{
	struct nvmap_pinarray_elem *pin;
	pin = &ctx->pinarray[ctx->pinarray_size++];
	pin->patch_mem = (u32)nvmap_ref_to_handle(ctx->gather_mem);
	pin->patch_offset = (idx * sizeof(struct nvhost_op_pair)) +
		offsetof(struct nvhost_op_pair, op2);
	pin->pin_mem = mem_id;
	pin->pin_offset = offset;
	ctx->gathers[idx].op1 = nvhost_opcode_gather(0, words);
}

static void reset_submit(struct nvhost_channel_userctx *ctx)
{
	ctx->cmdbufs_pending = 0;
	ctx->relocs_pending = 0;
}

static ssize_t nvhost_channelwrite(struct file *filp, const char __user *buf,
				size_t count, loff_t *offp)
{
	struct nvhost_channel_userctx *priv = filp->private_data;
	size_t remaining = count;
	int err = 0;

	while (remaining) {
		size_t consumed;
		if (!priv->relocs_pending && !priv->cmdbufs_pending) {
			consumed = sizeof(struct nvhost_submit_hdr);
			if (remaining < consumed)
				break;
			if (copy_from_user(&priv->syncpt_id, buf, consumed)) {
				err = -EFAULT;
				break;
			}
			if (!priv->cmdbufs_pending) {
				err = -EFAULT;
				break;
			}
			/* leave room for ctx switch */
			priv->num_gathers = 2;
			priv->pinarray_size = 0;
		} else if (priv->cmdbufs_pending) {
			struct nvhost_cmdbuf cmdbuf;
			consumed = sizeof(cmdbuf);
			if (remaining < consumed)
				break;
			if (copy_from_user(&cmdbuf, buf, consumed)) {
				err = -EFAULT;
				break;
			}
			add_gather(priv, priv->num_gathers++,
				   cmdbuf.mem, cmdbuf.words, cmdbuf.offset);
			priv->cmdbufs_pending--;
		} else if (priv->relocs_pending) {
			int numrelocs = remaining / sizeof(struct nvhost_reloc);
			if (!numrelocs)
				break;
			numrelocs = min_t(int, numrelocs, priv->relocs_pending);
			consumed = numrelocs * sizeof(struct nvhost_reloc);
			if (copy_from_user(&priv->pinarray[priv->pinarray_size],
						buf, consumed)) {
				err = -EFAULT;
				break;
			}
			priv->pinarray_size += numrelocs;
			priv->relocs_pending -= numrelocs;
		} else {
			err = -EFAULT;
			break;
		}
		remaining -= consumed;
		buf += consumed;
	}

	if (err < 0) {
		dev_err(&priv->ch->dev->pdev->dev, "channel write error\n");
		reset_submit(priv);
		return err;
	}

	return (count - remaining);
}

static int nvhost_ioctl_channel_flush(struct nvhost_channel_userctx *ctx,
                                      struct nvhost_get_param_args *args,
                                      int null_kickoff)
{
	struct nvhost_cpuinterrupt ctxsw;
	int gather_idx = 2;
	int num_intrs = 0;
	u32 syncval;
	int num_unpin;
	int err;
	int nulled_incrs = null_kickoff ? ctx->syncpt_incrs : 0;

	if (ctx->relocs_pending || ctx->cmdbufs_pending) {
		reset_submit(ctx);
		dev_err(&ctx->ch->dev->pdev->dev, "channel submit out of sync\n");
		return -EFAULT;
	}
	if (!ctx->nvmap) {
		dev_err(&ctx->ch->dev->pdev->dev, "no nvmap context set\n");
		return -EFAULT;
	}
	if (ctx->num_gathers <= 2)
		return 0;

	/* keep module powered */
	nvhost_module_busy(&ctx->ch->mod);

	/* pin mem handles and patch physical addresses */
	num_unpin = nvmap_pin_array(ctx->nvmap,
				    nvmap_ref_to_handle(ctx->gather_mem),
				    ctx->pinarray, ctx->pinarray_size,
				    ctx->unpinarray);
	if (num_unpin < 0) {
		dev_warn(&ctx->ch->dev->pdev->dev, "nvmap_pin_array failed: "
			 "%d\n", num_unpin);
		nvhost_module_idle(&ctx->ch->mod);
		return num_unpin;
	}

	/* get submit lock */
	err = mutex_lock_interruptible(&ctx->ch->submitlock);
	if (err) {
		nvmap_unpin_handles(ctx->nvmap, ctx->unpinarray, num_unpin);
		nvhost_module_idle(&ctx->ch->mod);
		return err;
	}

	/* context switch */
	if (ctx->ch->cur_ctx != ctx->hwctx) {
		struct nvhost_hwctx *hw = ctx->hwctx;
		if (hw && hw->valid) {
			gather_idx--;
			ctx->gathers[gather_idx].op1 =
				nvhost_opcode_gather(0, hw->restore_size);
			ctx->gathers[gather_idx].op2 = hw->restore_phys;
			ctx->syncpt_incrs += hw->restore_incrs;
		}
		hw = ctx->ch->cur_ctx;
		if (hw) {
			gather_idx--;
			ctx->gathers[gather_idx].op1 =
				nvhost_opcode_gather(0, hw->save_size);
			ctx->gathers[gather_idx].op2 = hw->save_phys;
			ctx->syncpt_incrs += hw->save_incrs;
			num_intrs = 1;
			ctxsw.syncpt_val = hw->save_incrs - 1;
			ctxsw.intr_data = hw;
			hw->valid = true;
			ctx->ch->ctxhandler.get(hw);
		}
		ctx->ch->cur_ctx = ctx->hwctx;
	}

	/* add a setclass for modules that require it */
	if (gather_idx == 2 && ctx->ch->desc->class) {
		gather_idx--;
		ctx->gathers[gather_idx].op1 =
			nvhost_opcode_setclass(ctx->ch->desc->class, 0, 0);
		ctx->gathers[gather_idx].op2 = NVHOST_OPCODE_NOOP;
	}

	/* get absolute sync value */
	if (BIT(ctx->syncpt_id) & NVSYNCPTS_CLIENT_MANAGED)
		syncval = nvhost_syncpt_set_max(&ctx->ch->dev->syncpt,
						ctx->syncpt_id, ctx->syncpt_incrs);
	else
		syncval = nvhost_syncpt_incr_max(&ctx->ch->dev->syncpt,
						ctx->syncpt_id, ctx->syncpt_incrs);

	/* patch absolute syncpt value into interrupt triggers */
	ctxsw.syncpt_val += syncval - ctx->syncpt_incrs;

	nvhost_channel_submit(ctx->ch, ctx->nvmap, &ctx->gathers[gather_idx],
			      (null_kickoff ? 2 : ctx->num_gathers) - gather_idx, &ctxsw, num_intrs,
			      ctx->unpinarray, num_unpin,
			      ctx->syncpt_id, syncval,
			      nulled_incrs);

	/* schedule a submit complete interrupt */
	nvhost_intr_add_action(&ctx->ch->dev->intr, ctx->syncpt_id, syncval,
			NVHOST_INTR_ACTION_SUBMIT_COMPLETE, ctx->ch, NULL);

	mutex_unlock(&ctx->ch->submitlock);
	args->value = syncval;
	return 0;
}

static long nvhost_channelctl(struct file *filp,
	unsigned int cmd, unsigned long arg)
{
	struct nvhost_channel_userctx *priv = filp->private_data;
	u8 buf[NVHOST_IOCTL_CHANNEL_MAX_ARG_SIZE];
	int err = 0;

	if ((_IOC_TYPE(cmd) != NVHOST_IOCTL_MAGIC) ||
		(_IOC_NR(cmd) == 0) ||
		(_IOC_NR(cmd) > NVHOST_IOCTL_CHANNEL_LAST))
		return -EFAULT;

	BUG_ON(_IOC_SIZE(cmd) > NVHOST_IOCTL_CHANNEL_MAX_ARG_SIZE);

	if (_IOC_DIR(cmd) & _IOC_WRITE) {
		if (copy_from_user(buf, (void __user *)arg, _IOC_SIZE(cmd)))
			return -EFAULT;
	}

	switch (cmd) {
	case NVHOST_IOCTL_CHANNEL_FLUSH:
		err = nvhost_ioctl_channel_flush(priv, (void *)buf, 0);
		break;
	case NVHOST_IOCTL_CHANNEL_NULL_KICKOFF:
		err = nvhost_ioctl_channel_flush(priv, (void *)buf, 1);
		break;
	case NVHOST_IOCTL_CHANNEL_GET_SYNCPOINTS:
		((struct nvhost_get_param_args *)buf)->value =
			priv->ch->desc->syncpts;
		break;
	case NVHOST_IOCTL_CHANNEL_GET_WAITBASES:
		((struct nvhost_get_param_args *)buf)->value =
			priv->ch->desc->waitbases;
		break;
	case NVHOST_IOCTL_CHANNEL_GET_MODMUTEXES:
		((struct nvhost_get_param_args *)buf)->value =
			priv->ch->desc->modulemutexes;
		break;
	case NVHOST_IOCTL_CHANNEL_SET_NVMAP_FD:
	{
		int fd = (int)((struct nvhost_set_nvmap_fd_args *)buf)->fd;
		struct nvmap_client *new_client = nvmap_client_get_file(fd);

		if (IS_ERR(new_client)) {
			err = PTR_ERR(new_client);
			break;
		}

		if (priv->nvmap)
			nvmap_client_put(priv->nvmap);

		priv->nvmap = new_client;
		break;
	}
	default:
		err = -ENOTTY;
		break;
	}

	if ((err == 0) && (_IOC_DIR(cmd) & _IOC_READ))
		err = copy_to_user((void __user *)arg, buf, _IOC_SIZE(cmd));

	return err;
}

static struct file_operations nvhost_channelops = {
	.owner = THIS_MODULE,
	.release = nvhost_channelrelease,
	.open = nvhost_channelopen,
	.write = nvhost_channelwrite,
	.unlocked_ioctl = nvhost_channelctl
};

static int nvhost_ctrlrelease(struct inode *inode, struct file *filp)
{
	struct nvhost_ctrl_userctx *priv = filp->private_data;
	int i;

	filp->private_data = NULL;
	if (priv->mod_locks[0])
		nvhost_module_idle(&priv->dev->mod);
	for (i = 1; i < NV_HOST1X_NB_MLOCKS; i++)
		if (priv->mod_locks[i])
			nvhost_mutex_unlock(&priv->dev->cpuaccess, i);
	kfree(priv);
	return 0;
}

static int nvhost_ctrlopen(struct inode *inode, struct file *filp)
{
	struct nvhost_master *host = container_of(inode->i_cdev, struct nvhost_master, cdev);
	struct nvhost_ctrl_userctx *priv;

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

	priv->dev = host;
	filp->private_data = priv;
	return 0;
}

static int nvhost_ioctl_ctrl_syncpt_read(
	struct nvhost_ctrl_userctx *ctx,
	struct nvhost_ctrl_syncpt_read_args *args)
{
	if (args->id >= NV_HOST1X_SYNCPT_NB_PTS)
		return -EINVAL;
	args->value = nvhost_syncpt_read(&ctx->dev->syncpt, args->id);
	return 0;
}

static int nvhost_ioctl_ctrl_syncpt_incr(
	struct nvhost_ctrl_userctx *ctx,
	struct nvhost_ctrl_syncpt_incr_args *args)
{
	if (args->id >= NV_HOST1X_SYNCPT_NB_PTS)
		return -EINVAL;
	nvhost_syncpt_incr(&ctx->dev->syncpt, args->id);
	return 0;
}

static int nvhost_ioctl_ctrl_syncpt_wait(
	struct nvhost_ctrl_userctx *ctx,
	struct nvhost_ctrl_syncpt_wait_args *args)
{
	u32 timeout;
	if (args->id >= NV_HOST1X_SYNCPT_NB_PTS)
		return -EINVAL;
	if (args->timeout == NVHOST_NO_TIMEOUT)
		timeout = MAX_SCHEDULE_TIMEOUT;
	else
		timeout = (u32)msecs_to_jiffies(args->timeout);

	return nvhost_syncpt_wait_timeout(&ctx->dev->syncpt, args->id,
					args->thresh, timeout);
}

static int nvhost_ioctl_ctrl_module_mutex(
	struct nvhost_ctrl_userctx *ctx,
	struct nvhost_ctrl_module_mutex_args *args)
{
	int err = 0;
	if (args->id >= NV_HOST1X_NB_MLOCKS ||
	    args->lock > 1)
		return -EINVAL;

	if (args->lock && !ctx->mod_locks[args->id]) {
		if (args->id == 0)
			nvhost_module_busy(&ctx->dev->mod);
		else
			err = nvhost_mutex_try_lock(&ctx->dev->cpuaccess, args->id);
		if (!err)
			ctx->mod_locks[args->id] = 1;
	}
	else if (!args->lock && ctx->mod_locks[args->id]) {
		if (args->id == 0)
			nvhost_module_idle(&ctx->dev->mod);
		else
			nvhost_mutex_unlock(&ctx->dev->cpuaccess, args->id);
		ctx->mod_locks[args->id] = 0;
	}
	return err;
}

static int nvhost_ioctl_ctrl_module_regrdwr(
	struct nvhost_ctrl_userctx *ctx,
	struct nvhost_ctrl_module_regrdwr_args *args)
{
	u32 num_offsets = args->num_offsets;
	u32 *offsets = args->offsets;
	void *values = args->values;
	u32 vals[64];

	if (!nvhost_access_module_regs(&ctx->dev->cpuaccess, args->id) ||
	    (num_offsets == 0))
		return -EINVAL;

	while (num_offsets--) {
		u32 remaining = args->block_size;
		u32 offs;
		if (get_user(offs, offsets))
			return -EFAULT;
		offsets++;
		while (remaining) {
			u32 batch = min(remaining, 64*sizeof(u32));
			if (args->write) {
				if (copy_from_user(vals, values, batch))
					return -EFAULT;
				nvhost_write_module_regs(&ctx->dev->cpuaccess,
							args->id, offs, batch, vals);
			} else {
				nvhost_read_module_regs(&ctx->dev->cpuaccess,
							args->id, offs, batch, vals);
				if (copy_to_user(values, vals, batch))
					return -EFAULT;
			}
			remaining -= batch;
			offs += batch;
			values += batch;
		}
	}

	return 0;
}

static long nvhost_ctrlctl(struct file *filp,
	unsigned int cmd, unsigned long arg)
{
	struct nvhost_ctrl_userctx *priv = filp->private_data;
	u8 buf[NVHOST_IOCTL_CTRL_MAX_ARG_SIZE];
	int err = 0;

	if ((_IOC_TYPE(cmd) != NVHOST_IOCTL_MAGIC) ||
		(_IOC_NR(cmd) == 0) ||
		(_IOC_NR(cmd) > NVHOST_IOCTL_CTRL_LAST))
		return -EFAULT;

	BUG_ON(_IOC_SIZE(cmd) > NVHOST_IOCTL_CTRL_MAX_ARG_SIZE);

	if (_IOC_DIR(cmd) & _IOC_WRITE) {
		if (copy_from_user(buf, (void __user *)arg, _IOC_SIZE(cmd)))
			return -EFAULT;
	}

	switch (cmd) {
	case NVHOST_IOCTL_CTRL_SYNCPT_READ:
		err = nvhost_ioctl_ctrl_syncpt_read(priv, (void *)buf);
		break;
	case NVHOST_IOCTL_CTRL_SYNCPT_INCR:
		err = nvhost_ioctl_ctrl_syncpt_incr(priv, (void *)buf);
		break;
	case NVHOST_IOCTL_CTRL_SYNCPT_WAIT:
		err = nvhost_ioctl_ctrl_syncpt_wait(priv, (void *)buf);
		break;
	case NVHOST_IOCTL_CTRL_MODULE_MUTEX:
		err = nvhost_ioctl_ctrl_module_mutex(priv, (void *)buf);
		break;
	case NVHOST_IOCTL_CTRL_MODULE_REGRDWR:
		err = nvhost_ioctl_ctrl_module_regrdwr(priv, (void *)buf);
		break;
	default:
		err = -ENOTTY;
		break;
	}

	if ((err == 0) && (_IOC_DIR(cmd) & _IOC_READ))
		err = copy_to_user((void __user *)arg, buf, _IOC_SIZE(cmd));

	return err;
}

static struct file_operations nvhost_ctrlops = {
	.owner = THIS_MODULE,
	.release = nvhost_ctrlrelease,
	.open = nvhost_ctrlopen,
	.unlocked_ioctl = nvhost_ctrlctl
};

static void power_host(struct nvhost_module *mod, enum nvhost_power_action action)
{
	struct nvhost_master *dev = container_of(mod, struct nvhost_master, mod);

	if (action == NVHOST_POWER_ACTION_ON) {
		nvhost_intr_configure(&dev->intr, clk_get_rate(mod->clk[0]));
	}
	else if (action == NVHOST_POWER_ACTION_OFF) {
		int i;
		for (i = 0; i < NVHOST_NUMCHANNELS; i++)
			nvhost_channel_suspend(&dev->channels[i]);
		nvhost_syncpt_save(&dev->syncpt);
	}
}

static int __devinit nvhost_user_init(struct nvhost_master *host)
{
	int i, err, devno;

	host->nvhost_class = class_create(THIS_MODULE, IFACE_NAME);
	if (IS_ERR(host->nvhost_class)) {
		err = PTR_ERR(host->nvhost_class);
		dev_err(&host->pdev->dev, "failed to create class\n");
		goto fail;
	}

	if (nvhost_major) {
		devno = MKDEV(nvhost_major, nvhost_minor);
		err = register_chrdev_region(devno, NVHOST_NUMCHANNELS + 1, IFACE_NAME);
	} else {
		err = alloc_chrdev_region(&devno, nvhost_minor,
					NVHOST_NUMCHANNELS + 1, IFACE_NAME);
		nvhost_major = MAJOR(devno);
	}
	if (err < 0) {
		dev_err(&host->pdev->dev, "failed to reserve chrdev region\n");
		goto fail;
	}

	for (i = 0; i < NVHOST_NUMCHANNELS; i++) {
		struct nvhost_channel *ch = &host->channels[i];

		cdev_init(&ch->cdev, &nvhost_channelops);
		ch->cdev.owner = THIS_MODULE;

		devno = MKDEV(nvhost_major, nvhost_minor + i);
		err = cdev_add(&ch->cdev, devno, 1);
		if (err < 0) {
			dev_err(&host->pdev->dev, "failed to add chan %i cdev\n", i);
			goto fail;
		}
		ch->node = device_create(host->nvhost_class, NULL, devno, NULL,
				IFACE_NAME "-%s", ch->desc->name);
		if (IS_ERR(ch->node)) {
			err = PTR_ERR(ch->node);
			dev_err(&host->pdev->dev, "failed to create chan %i device\n", i);
			goto fail;
		}
	}

	cdev_init(&host->cdev, &nvhost_ctrlops);
	host->cdev.owner = THIS_MODULE;
	devno = MKDEV(nvhost_major, nvhost_minor + NVHOST_NUMCHANNELS);
	err = cdev_add(&host->cdev, devno, 1);
	if (err < 0)
		goto fail;
	host->ctrl = device_create(host->nvhost_class, NULL, devno, NULL,
			IFACE_NAME "-ctrl");
	if (IS_ERR(host->ctrl)) {
		err = PTR_ERR(host->ctrl);
		dev_err(&host->pdev->dev, "failed to create ctrl device\n");
		goto fail;
	}

	return 0;
fail:
	return err;
}

static int __devinit nvhost_probe(struct platform_device *pdev)
{
	struct nvhost_master *host;
	struct resource *regs, *intr0, *intr1;
	int i, err;

	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	intr0 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	intr1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);

	if (!regs || !intr0 || !intr1) {
		dev_err(&pdev->dev, "missing required platform resources\n");
		return -ENXIO;
	}

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

	host->pdev = pdev;

	host->nvmap = nvmap_create_client(nvmap_dev, "nvhost");
	if (!host->nvmap) {
		dev_err(&pdev->dev, "unable to create nvmap client\n");
		err = -EIO;
		goto fail;
	}

	host->reg_mem = request_mem_region(regs->start,
					resource_size(regs), pdev->name);
	if (!host->reg_mem) {
		dev_err(&pdev->dev, "failed to get host register memory\n");
		err = -ENXIO;
		goto fail;
	}
	host->aperture = ioremap(regs->start, resource_size(regs));
	if (!host->aperture) {
		dev_err(&pdev->dev, "failed to remap host registers\n");
		err = -ENXIO;
		goto fail;
	}
	host->sync_aperture = host->aperture +
		(NV_HOST1X_CHANNEL0_BASE +
			HOST1X_CHANNEL_SYNC_REG_BASE);

	for (i = 0; i < NVHOST_NUMCHANNELS; i++) {
		struct nvhost_channel *ch = &host->channels[i];
		err = nvhost_channel_init(ch, host, i);
		if (err < 0) {
			dev_err(&pdev->dev, "failed to init channel %d\n", i);
			goto fail;
		}
	}

	err = nvhost_cpuaccess_init(&host->cpuaccess, pdev);
	if (err) goto fail;
	err = nvhost_intr_init(&host->intr, intr1->start, intr0->start);
	if (err) goto fail;
	err = nvhost_user_init(host);
	if (err) goto fail;
	err = nvhost_module_init(&host->mod, "host1x", power_host, NULL, &pdev->dev);
	if (err) goto fail;

	platform_set_drvdata(pdev, host);

	clk_enable(host->mod.clk[0]);
	nvhost_syncpt_reset(&host->syncpt);
	clk_disable(host->mod.clk[0]);

	nvhost_bus_register(host);

	nvhost_debug_init(host);

	dev_info(&pdev->dev, "initialized\n");
	return 0;

fail:
	if (host->nvmap)
		nvmap_client_put(host->nvmap);
	/* TODO: [ahatala 2010-05-04] */
	kfree(host);
	return err;
}

static int __exit nvhost_remove(struct platform_device *pdev)
{
	return 0;
}

static int nvhost_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct nvhost_master *host = platform_get_drvdata(pdev);
	dev_info(&pdev->dev, "suspending\n");
	nvhost_module_suspend(&host->mod, true);
	clk_enable(host->mod.clk[0]);
	nvhost_syncpt_save(&host->syncpt);
	clk_disable(host->mod.clk[0]);
	dev_info(&pdev->dev, "suspended\n");
	return 0;
}

static int nvhost_resume(struct platform_device *pdev)
{
	struct nvhost_master *host = platform_get_drvdata(pdev);
	dev_info(&pdev->dev, "resuming\n");
	clk_enable(host->mod.clk[0]);
	nvhost_syncpt_reset(&host->syncpt);
	clk_disable(host->mod.clk[0]);
	dev_info(&pdev->dev, "resumed\n");
	return 0;
}

static struct platform_driver nvhost_driver = {
	.remove = __exit_p(nvhost_remove),
	.suspend = nvhost_suspend,
	.resume = nvhost_resume,
	.driver = {
		.owner = THIS_MODULE,
		.name = DRIVER_NAME
	}
};

static int __init nvhost_mod_init(void)
{
	return platform_driver_probe(&nvhost_driver, nvhost_probe);
}

static void __exit nvhost_mod_exit(void)
{
	platform_driver_unregister(&nvhost_driver);
}

module_init(nvhost_mod_init);
module_exit(nvhost_mod_exit);

MODULE_AUTHOR("NVIDIA");
MODULE_DESCRIPTION("Graphics host driver for Tegra products");
MODULE_VERSION("1.0");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_ALIAS("platform-nvhost");