summaryrefslogtreecommitdiff
path: root/drivers/staging/omapdce/dce.c
blob: 26f13a9b924f2ec67f9473bb659b67c4fbb58021 (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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
/*
 * drivers/staging/omapdce/dce.c
 *
 * Copyright (C) 2011 Texas Instruments
 * Author: Rob Clark <rob.clark@linaro.org>
 *
 * 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/>.
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/rpmsg.h>
#include <linux/pm_runtime.h>
#include <plat/omap-pm.h>
#include <plat/clock.h>

#include "../omapdrm/omap_drm.h"
#include "../omapdrm/omap_drv.h"
#include "omap_dce.h"

#include "dce_rpc.h"

/* TODO: split util stuff into other file..  maybe split out some of the
 * _process() munging stuff..
 */

#define DBG(fmt,...)  DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
#define VERB(fmt,...) if (0) DRM_DEBUG(fmt, ##__VA_ARGS__) /* verbose debug */

/* Removeme.. */
static long mark(long *last)
{
    struct timeval t;
    do_gettimeofday(&t);
    if (last) {
        return t.tv_usec - *last;
    }
    return t.tv_usec;
}

#define MAX_ENGINES	32
#define MAX_CODECS	32
#define MAX_LOCKED_BUFFERS	32  /* actually, 16 should be enough if reorder queue is disabled */
#define MAX_BUFFER_OBJECTS	((2 * MAX_LOCKED_BUFFERS) + 4)
#define MAX_TRANSACTIONS	MAX_CODECS

enum dce_codec_quirks {
	DCE_CODEC_QUIRKS_NONE = 0,
	DCE_CODEC_QUIRKS_OUTBUFS_MEMTYPE_RAW = 1
};

struct dce_buffer {
	int32_t id;		/* zero for unused */
	struct drm_gem_object *y, *uv;
};

struct dce_engine {
	uint32_t engine;
};

struct dce_codec {
	enum omap_dce_codec codec_id;
	uint32_t codec;
	enum dce_codec_quirks quirks;
	struct dce_buffer locked_buffers[MAX_LOCKED_BUFFERS];
};

struct dce_file_priv {
	struct drm_device *dev;
	struct drm_file *file;

	/* NOTE: engine/codec end up being pointers (or similar) on
	 * coprocessor side.. so these are not exposed directly to
	 * userspace.  Instead userspace sees per-file unique handles
	 * which index into the table of engines/codecs.
	 */
	struct dce_engine engines[MAX_ENGINES];

	struct dce_codec codecs[MAX_CODECS];

	/* the token returned to userspace is an index into this table
	 * of msg request-ids.  This avoids any chance that userspace might
	 * try to guess another processes txn req_id and try to intercept
	 * the other processes reply..
	 */
	uint16_t req_ids[MAX_CODECS];
	atomic_t next_token;
};

/* per-transaction data.. indexed by req_id%MAX_REQUESTS
 */

struct omap_dce_txn {
	struct dce_file_priv *priv;  /* file currently using thix txn slot */
	struct dce_rpc_hdr *rsp;
	int len;
	int bo_count;
	struct drm_gem_object *objs[MAX_BUFFER_OBJECTS];
};
static struct omap_dce_txn txns[MAX_TRANSACTIONS];

/* note: eventually we could perhaps have per drm_device private data
 * and pull all this into a struct.. but in rpmsg_cb we don't have a
 * way to get at that, so for now all global..
 */
struct rpmsg_channel *rpdev;
static int dce_mapper_id = -1;
static atomic_t next_req_id = ATOMIC_INIT(0);
static DECLARE_WAIT_QUEUE_HEAD(wq);
static DEFINE_MUTEX(lock);  // TODO probably more locking needed..

/* PM runtime implementation */
/* PM dependencies of each "device" is defined by its position in the array */
/* get_sync : iva -> sl2if -> seq0 -> seq1 */
/* put_sync: seq1 -> seq0 -> sl2if -> iva */

#define NB_PM_DEVICES 4

struct dce_pm_device dce_pm_device_list[NB_PM_DEVICES] = {
	{.name = "iva.0", .dev = NULL},
	{.name = "sl2if", .dev = NULL},
	{.name = "iva_seq0.0", .dev = NULL},
	{.name = "iva_seq1.0", .dev = NULL}
};

static void dce_pm_runtime_get_sync(void)
{
	int i;
	for (i = 0; i < NB_PM_DEVICES; i++)
		pm_runtime_get_sync(dce_pm_device_list[i].dev);
}

static void dce_pm_runtime_put_sync(void)
{
	int i;
	for (i = (NB_PM_DEVICES - 1); i >= 0; i--)
		pm_runtime_put_sync(dce_pm_device_list[i].dev);
}

/*
 * Utils:
 */

#define hdr(r) ((void *)(r))

/* initialize header and assign request id: */
#define MKHDR(x) (struct dce_rpc_hdr){ .msg_id = DCE_RPC_##x, .req_id = atomic_inc_return(&next_req_id) }

/* GEM buffer -> paddr, plus add the buffer to the txn bookkeeping of
 * associated buffers that eventually need to be cleaned up when the
 * transaction completes
 */
static struct drm_gem_object * get_paddr(struct dce_file_priv *priv,
		struct dce_rpc_hdr *req, uint32_t *paddrp, int bo)
{
	struct omap_dce_txn *txn = &txns[req->req_id % ARRAY_SIZE(txns)];
	struct drm_gem_object *obj;
	dma_addr_t paddr;
	int ret;
long t;

	if (txn->bo_count >= ARRAY_SIZE(txn->objs)) {
		DBG("too many buffers!");
		return ERR_PTR(-ENOMEM);
	}

	obj = drm_gem_object_lookup(priv->dev, priv->file, bo);
	if (!obj) {
		DBG("bad handle: %d", bo);
		return ERR_PTR(-ENOENT);
	}

t = mark(NULL);
	ret = omap_gem_get_paddr(obj, &paddr, true);
DBG("get_paddr in %ld us", mark(&t));
	if (ret) {
		DBG("cannot map: %d", ret);
		return ERR_PTR(ret);
	}

	/* the coproc can only see 32bit addresses.. this might need
	 * to be revisited in the future with some conversion between
	 * device address and host address.  But currently they are
	 * the same.
	 */
	*paddrp = (uint32_t)paddr;

	txn->objs[txn->bo_count++] = obj;

	DBG("obj=%p", obj);

	return obj;
}

static int rpsend(struct dce_file_priv *priv, uint32_t *token,
		struct dce_rpc_hdr *req, int len)
{
	struct omap_dce_txn *txn =
			&txns[req->req_id % ARRAY_SIZE(txns)];

	WARN_ON(txn->priv);

	/* assign token: */
	if (token) {
		*token = atomic_inc_return(&priv->next_token) + 1;
		priv->req_ids[(*token-1) % ARRAY_SIZE(priv->req_ids)] = req->req_id;

		txn->priv = priv;

		// XXX  wait for paddrs to become valid!

	} else {
		/* message with no response: */
		req->req_id = 0xffff;          /* just for debug */
		WARN_ON(txn->bo_count > 0);    /* this is not valid */

		memset(txn, 0, sizeof(*txn));
	}

	return rpmsg_send(rpdev, req, len);
}

static int rpwait(struct dce_file_priv *priv, uint32_t token,
		struct dce_rpc_hdr **rsp, int len)
{
	uint16_t req_id = priv->req_ids[(token-1) % ARRAY_SIZE(priv->req_ids)];
	struct omap_dce_txn *txn = &txns[req_id % ARRAY_SIZE(txns)];
	int ret;

	if (txn->priv != priv) {
		dev_err(priv->dev->dev, "not my txn\n");
		return -EINVAL;
	}

	ret = wait_event_interruptible(wq, (txn->rsp));
	if (ret) {
		DBG("ret=%d", ret);
		return ret;
	}

	if (txn->len < len) {
		dev_err(priv->dev->dev, "rsp too short: %d < %d\n", txn->len, len);
		ret = -EINVAL;
		goto fail;
	}

	*rsp = txn->rsp;

fail:
	/* clear out state: */
	memset(txn, 0, sizeof(*txn));

	return ret;
}

static void txn_cleanup(struct omap_dce_txn *txn)
{
	int i;

	mutex_lock(&lock);

	kfree(txn->rsp);
	txn->rsp = NULL;

	/* unpin/unref buffers associated with this transaction */
	for (i = 0; i < txn->bo_count; i++) {
		struct drm_gem_object *obj = txn->objs[i];
		DBG("obj=%p", obj);
		omap_gem_put_paddr(obj);
		drm_gem_object_unreference_unlocked(obj);
	}
	txn->bo_count = 0;

	mutex_unlock(&lock);
}

static void rpcomplete(struct dce_rpc_hdr *rsp, int len)
{
	struct omap_dce_txn *txn = &txns[rsp->req_id % ARRAY_SIZE(txns)];

	if (!txn->priv) {
		/* we must of cleaned up already (killed process) */
		printk(KERN_ERR "dce: unexpected response.. killed process?\n");
		kfree(rsp);
		return;
	}

	txn_cleanup(txn);

	txn->len = len;
	txn->rsp = rsp;

	wake_up_all(&wq);
}

static int rpabort(struct dce_rpc_hdr *req, int ret)
{
	struct omap_dce_txn *txn =
			&txns[req->req_id % ARRAY_SIZE(txns)];

	DBG("txn failed: msg_id=%u, req_id=%u, ret=%d",
			(uint32_t)req->msg_id, (uint32_t)req->req_id, ret);

	txn_cleanup(txn);

	/* clear out state: */
	memset(txn, 0, sizeof(*txn));

	return ret;
}

/* helpers for tracking engine instances and mapping engine handle to engine
 * instance:
 */

static uint32_t engine_register(struct dce_file_priv *priv, uint32_t engine)
{
	int i;
	for (i = 0; i < ARRAY_SIZE(priv->engines); i++) {
		if (!priv->engines[i].engine) {
			priv->engines[i].engine = engine;
			return i+1;
		}
	}
	dev_err(priv->dev->dev, "too many engines\n");
	return 0;
}

static void engine_unregister(struct dce_file_priv *priv, uint32_t eng_handle)
{
	priv->engines[eng_handle-1].engine = 0;
}

static bool engine_valid(struct dce_file_priv *priv, uint32_t eng_handle)
{
	return (eng_handle > 0) &&
			(eng_handle <= ARRAY_SIZE(priv->engines)) &&
			(priv->engines[eng_handle-1].engine);
}

static int engine_get(struct dce_file_priv *priv, uint32_t eng_handle,
		uint32_t *engine)
{
	if (!engine_valid(priv, eng_handle))
		return -EINVAL;
	*engine = priv->engines[eng_handle-1].engine;
	return 0;
}

/* helpers for tracking codec instances and mapping codec handle to codec
 * instance:
 */

static void codec_unlockbuf(struct dce_file_priv *priv,
		uint32_t codec_handle, int32_t id);

static uint32_t codec_register(struct dce_file_priv *priv, uint32_t codec,
		enum omap_dce_codec codec_id, enum dce_codec_quirks quirks)
{
	int i;
	for (i = 0; i < ARRAY_SIZE(priv->codecs); i++) {
		if (!priv->codecs[i].codec) {
			priv->codecs[i].codec_id = codec_id;
			priv->codecs[i].codec = codec;
			priv->codecs[i].quirks = quirks;
			return i+1;
		}
	}
	dev_err(priv->dev->dev, "too many codecs\n");
	return 0;
}

static void codec_unregister(struct dce_file_priv *priv,
		uint32_t codec_handle)
{
	codec_unlockbuf(priv, codec_handle, 0);
	priv->codecs[codec_handle-1].codec = 0;
	priv->codecs[codec_handle-1].codec_id = 0;
}

static bool codec_valid(struct dce_file_priv *priv, uint32_t codec_handle)
{
	return (codec_handle > 0) &&
			(codec_handle <= ARRAY_SIZE(priv->codecs)) &&
			(priv->codecs[codec_handle-1].codec);
}

static int codec_get(struct dce_file_priv *priv, uint32_t codec_handle,
		uint32_t *codec, uint32_t *codec_id)
{
	if (!codec_valid(priv, codec_handle))
		return -EINVAL;
	*codec    = priv->codecs[codec_handle-1].codec;
	*codec_id = priv->codecs[codec_handle-1].codec_id;
	return 0;
}

static int codec_get_quirks(struct dce_file_priv *priv, uint32_t codec_handle,
		enum dce_codec_quirks *quirks)
{
	if (!codec_valid(priv, codec_handle))
		return -EINVAL;
	*quirks = priv->codecs[codec_handle-1].quirks;
	return 0;
}


static int codec_lockbuf(struct dce_file_priv *priv,
		uint32_t codec_handle, int32_t id,
		struct drm_gem_object *y, struct drm_gem_object *uv)
{
	struct dce_codec *codec = &priv->codecs[codec_handle-1];
	int i;

	for (i = 0; i < ARRAY_SIZE(codec->locked_buffers); i++) {
		struct dce_buffer *buf = &codec->locked_buffers[i];
		if (buf->id == 0) {
			dma_addr_t paddr;

			DBG("lock[%d]: y=%p, uv=%p", id, y, uv);

			/* for now, until the codecs support relocated buffers, keep
			 * an extra ref and paddr to keep it pinned
			 */
			drm_gem_object_reference(y);
			omap_gem_get_paddr(y, &paddr, true);

			if (uv) {
				drm_gem_object_reference(uv);
				omap_gem_get_paddr(uv, &paddr, true);
			}

			buf->id = id;
			buf->y = y;
			buf->uv = uv;

			return 0;
		}
	}
	dev_err(priv->dev->dev, "too many locked buffers!\n");
	return -ENOMEM;
}

static void codec_unlockbuf(struct dce_file_priv *priv,
		uint32_t codec_handle, int32_t id)
{
	struct dce_codec *codec = &priv->codecs[codec_handle-1];
	int i;

	for (i = 0; i < ARRAY_SIZE(codec->locked_buffers); i++) {
		struct dce_buffer *buf = &codec->locked_buffers[i];
		/* if id==0, unlock all buffers.. */
		if (((id == 0) && (buf->id != 0)) ||
			((id != 0) && (buf->id == id))) {
			struct drm_gem_object *y, *uv;

			y  = buf->y;
			uv = buf->uv;

			DBG("unlock[%d]: y=%p, uv=%p", buf->id, y, uv);

			/* release extra ref */
			omap_gem_put_paddr(y);
			drm_gem_object_unreference_unlocked(y);

			if (uv) {
				omap_gem_put_paddr(uv);
				drm_gem_object_unreference_unlocked(uv);
			}

			buf->id = 0;
			buf->y = NULL;
			buf->uv = NULL;

			/* if id==0, unlock all buffers.. */
			if (id != 0)
				return;
		}
	}
}


/*
 * Ioctl Handlers:
 */

static int engine_close(struct dce_file_priv *priv, uint32_t engine);
static int codec_delete(struct dce_file_priv *priv, uint32_t codec,
		enum omap_dce_codec codec_id);

static int ioctl_engine_open(struct drm_device *dev, void *data,
		struct drm_file *file)
{
	struct dce_file_priv *priv = omap_drm_file_priv(file, dce_mapper_id);
	struct drm_omap_dce_engine_open *arg = data;
	struct dce_rpc_engine_open_rsp *rsp;
	int ret;

	/* if we are not re-starting a syscall, send req */
	if (!arg->token) {
		struct dce_rpc_engine_open_req req = {
				.hdr = MKHDR(ENGINE_OPEN),
		};
		strncpy(req.name, arg->name, sizeof(req.name));
		ret = rpsend(priv, &arg->token, hdr(&req), sizeof(req));
		if (ret)
			return ret;
	}

	/* then wait for reply, which is interruptible */
	ret = rpwait(priv, arg->token, hdr(&rsp), sizeof(*rsp));
	if (ret)
		return ret;

	arg->eng_handle = engine_register(priv, rsp->engine);
	arg->error_code = rsp->error_code;

	if (!engine_valid(priv, arg->eng_handle)) {
		engine_close(priv, rsp->engine);
		ret = -ENOMEM;
	}

	kfree(rsp);

	return ret;
}

static int engine_close(struct dce_file_priv *priv, uint32_t engine)
{
	struct dce_rpc_engine_close_req req = {
			.hdr = MKHDR(ENGINE_CLOSE),
			.engine = engine,
	};
	return rpsend(priv, NULL, hdr(&req), sizeof(req));
}

static int ioctl_engine_close(struct drm_device *dev, void *data,
		struct drm_file *file)
{
	struct dce_file_priv *priv = omap_drm_file_priv(file, dce_mapper_id);
	struct drm_omap_dce_engine_close *arg = data;
	uint32_t engine;
	int ret;

	ret = engine_get(priv, arg->eng_handle, &engine);
	if (ret)
		return ret;

	engine_unregister(priv, arg->eng_handle);

	return engine_close(priv, engine);
}

static int ioctl_codec_create(struct drm_device *dev, void *data,
		struct drm_file *file)
{
	struct dce_file_priv *priv = omap_drm_file_priv(file, dce_mapper_id);
	struct drm_omap_dce_codec_create *arg = data;
	struct dce_rpc_codec_create_rsp *rsp;
	enum dce_codec_quirks quirks = DCE_CODEC_QUIRKS_NONE;
	int ret;

	/* if we are not re-starting a syscall, send req */
	if (!arg->token) {
		struct dce_rpc_codec_create_req req = {
				.hdr = MKHDR(CODEC_CREATE),
				.codec_id = arg->codec_id,
		};

		strncpy(req.name, arg->name, sizeof(req.name));
		if (!strcmp(req.name, "ivahd_vc1vdec") ||
			!strcmp(req.name, "ivahd_mpeg2vdec") ||
			!strcmp(req.name, "ivahd_jpegvdec"))
			quirks |= DCE_CODEC_QUIRKS_OUTBUFS_MEMTYPE_RAW;

		ret = engine_get(priv, arg->eng_handle, &req.engine);
		if (ret)
			return ret;

		ret = PTR_RET(get_paddr(priv, hdr(&req), &req.sparams, arg->sparams_bo));
		if (ret)
			goto rpsend_out;

		ret = rpsend(priv, &arg->token, hdr(&req), sizeof(req));
rpsend_out:
		if (ret)
			return rpabort(hdr(&req), ret);
	}

	/* then wait for reply, which is interruptible */
	ret = rpwait(priv, arg->token, hdr(&rsp), sizeof(*rsp));
	if (ret)
		return ret;

	arg->codec_handle = codec_register(priv, rsp->codec, arg->codec_id, quirks);

	if (!codec_valid(priv, arg->codec_handle)) {
		codec_delete(priv, rsp->codec, arg->codec_id);
		ret = -ENOMEM;
	}

	kfree(rsp);

	return ret;
}

static int ioctl_codec_control(struct drm_device *dev, void *data,
		struct drm_file *file)
{
	struct dce_file_priv *priv = omap_drm_file_priv(file, dce_mapper_id);
	struct drm_omap_dce_codec_control *arg = data;
	struct dce_rpc_codec_control_rsp *rsp;
	int ret;

	/* if we are not re-starting a syscall, send req */
	if (!arg->token) {
		struct dce_rpc_codec_control_req req = {
				.hdr = MKHDR(CODEC_CONTROL),
				.cmd_id = arg->cmd_id,
		};

		ret = codec_get(priv, arg->codec_handle, &req.codec, &req.codec_id);
		if (ret)
			return ret;

		ret = PTR_RET(get_paddr(priv, hdr(&req), &req.dparams, arg->dparams_bo));
		if (ret)
			goto rpsend_out;

		ret = PTR_RET(get_paddr(priv, hdr(&req), &req.status, arg->status_bo));
		if (ret)
			goto rpsend_out;

		ret = rpsend(priv, &arg->token, hdr(&req), sizeof(req));
rpsend_out:
		if (ret)
			return rpabort(hdr(&req), ret);
	}

	/* then wait for reply, which is interruptible */
	ret = rpwait(priv, arg->token, hdr(&rsp), sizeof(*rsp));
	if (ret)
		return ret;

	arg->result = rsp->result;

	kfree(rsp);

	return 0;
}

static int ioctl_codec_get_version(struct drm_device *dev, void *data,
		struct drm_file *file)
{
	struct dce_file_priv *priv = omap_drm_file_priv(file, dce_mapper_id);
	struct drm_omap_dce_codec_get_version *arg = data;
	struct dce_rpc_codec_get_version_rsp *rsp;
	int ret;

	/* if we are not re-starting a syscall, send req */
	if (!arg->token) {
		struct dce_rpc_codec_get_version_req req = {
				.hdr = MKHDR(CODEC_GET_VERSION)
		};

		ret = codec_get(priv, arg->codec_handle, &req.codec, &req.codec_id);
		if (ret)
			return ret;

		ret = PTR_RET(get_paddr(priv, hdr(&req), &req.dparams, arg->dparams_bo));
		if (ret)
			goto rpsend_out;

		ret = PTR_RET(get_paddr(priv, hdr(&req), &req.status, arg->status_bo));
		if (ret)
			goto rpsend_out;

		ret = PTR_RET(get_paddr(priv, hdr(&req), &req.version, arg->version_bo));
		if (ret)
			goto rpsend_out;

		ret = rpsend(priv, &arg->token, hdr(&req), sizeof(req));
rpsend_out:
		if (ret)
			return rpabort(hdr(&req), ret);
	}

	/* then wait for reply, which is interruptible */
	ret = rpwait(priv, arg->token, hdr(&rsp), sizeof(*rsp));
	if (ret)
		return ret;

	arg->result = rsp->result;

	kfree(rsp);

	return 0;
}

struct viddec3_in_args {
	int32_t size;		/* struct size */
	int32_t num_bytes;
	int32_t input_id;
};
struct videnc2_in_args {
	int32_t size;
	int32_t input_id;
	int32_t control;
};

union xdm2_buf_size {
    struct {
        int32_t width;
        int32_t height;
    } tiled;
    int32_t bytes;
};
struct xdm2_single_buf_desc {
    uint32_t buf;
    int16_t  mem_type;	/* XXX should be XDM_MEMTYPE_BO */
    int16_t  usage_mode;
    union xdm2_buf_size buf_size;
    int32_t  accessMask;
};
struct xdm2_buf_desc {
    int32_t num_bufs;
    struct xdm2_single_buf_desc descs[16];
};

struct video2_buf_desc {
    int32_t num_planes;
    int32_t num_meta_planes;
    int32_t data_layout;
    struct xdm2_single_buf_desc plane_desc[3];
    struct xdm2_single_buf_desc metadata_plane_desc[3];
    /* rest of the struct isn't interesting to kernel.. if you are
     * curious look at IVIDEO2_BufDesc in ivideo.h in codec-engine
     */
    uint32_t data[30];
};
#define XDM_MEMTYPE_RAW       0
#define XDM_MEMTYPE_TILED8    1
#define XDM_MEMTYPE_TILED16   2
#define XDM_MEMTYPE_TILED32   3
#define XDM_MEMTYPE_TILEDPAGE 4

/* copy_from_user helper that also checks to avoid overrunning
 * the 'to' buffer and advances dst ptr
 */
static inline int cfu(void **top, uint64_t from, int n, void *end)
{
	void *to = *top;
	int ret;
	if ((to + n) >= end) {
		DBG("dst buffer overflow!");
		return -EFAULT;
	}
	ret = copy_from_user(to, (char __user *)(uintptr_t)from, n);
	*top = to + n;
	return ret;
}

static inline struct drm_gem_object * handle_single_buf_desc(
		struct dce_file_priv *priv, struct dce_rpc_hdr *req,
		uint32_t base_bo, int mem_type,
		struct xdm2_single_buf_desc *desc)
{
	struct drm_gem_object *obj;
	uint32_t flags;
	int32_t offset = 0;
	int ret;

	/* maybe support remapping user ptrs later on.. */
	if (desc->mem_type != XDM_MEMTYPE_BO &&
			desc->mem_type != XDM_MEMTYPE_BO_OFFSET)
		return ERR_PTR(-EINVAL);

	if (desc->mem_type == XDM_MEMTYPE_BO_OFFSET) {
		/* desc->buf is an offset to base_bo, which is descs[0].buf as
		 * passed to _process */
		offset = desc->buf;
		desc->buf = base_bo;
	}

	obj = get_paddr(priv, req, &desc->buf, desc->buf);
	if (IS_ERR(obj))
		return obj;

	desc->buf += offset;

	flags = omap_gem_flags(obj);
	switch(flags & OMAP_BO_TILED) {
	case OMAP_BO_TILED_8:
		desc->mem_type = XDM_MEMTYPE_TILED8;
		break;
	case OMAP_BO_TILED_16:
		desc->mem_type = XDM_MEMTYPE_TILED16;
		break;
	case OMAP_BO_TILED_32:
		desc->mem_type = XDM_MEMTYPE_TILED32;
		break;
	default:
		desc->mem_type = mem_type;
		break;
	}

	if (flags & OMAP_BO_TILED) {
		uint16_t w, h;
		omap_gem_tiled_size(obj, &w, &h);
		desc->buf_size.tiled.width = w;
		desc->buf_size.tiled.height = h;
	}

	// XXX not sure if the codecs care about usage_mode.. but we
	// know if the buffer is cached or not so we could set DATASYNC
	// bit if needed..

	/* if we are using the GPU to render, it might still be reading
	 * from the buffer.. we don't want to overwrite it yet!
	 *
	 * Note: this is an interruptible wait, but in case we are
	 * interrupted the normal error cleanup paths (rpabort(), etc)
	 * will handle the cleanup
	 *
	 * Note: this is only for decoder output frames.. for encoder
	 * input frames, we don't care if anyone is reading, use
	 * instead OMAP_GEM_READ ad the access mode..
	 */
	ret = omap_gem_op_sync(obj, OMAP_GEM_WRITE);
	if (ret)
		return ERR_PTR(ret);

	return obj;
}

static inline int handle_buf_desc(struct dce_file_priv *priv,
		void **ptr, void *end, struct dce_rpc_hdr *req, uint64_t usr,
		int mem_type, struct drm_gem_object **o1,
		struct drm_gem_object **o2, uint8_t *len)
{
	struct xdm2_buf_desc *bufs = *ptr;
	uint32_t base_bo;
	int i, ret;

	/* read num_bufs field: */
	ret = cfu(ptr, usr, 4, end);
	if (ret)
		return ret;

	/* read rest of structure: */
	ret = cfu(ptr, usr+4, bufs->num_bufs * sizeof(bufs->descs[0]), end);
	if (ret)
		return ret;

	*len = (4 + bufs->num_bufs * sizeof(bufs->descs[0])) / 4;

	/* the bo used as a base for XDM_MEMTYPE_BO_OFFSET descriptors */
	base_bo = bufs->descs[0].buf;

	/* handle buffer mapping.. */
	for (i = 0; i < bufs->num_bufs; i++) {
		struct drm_gem_object *obj =
				handle_single_buf_desc(priv, req, base_bo,
						mem_type, &bufs->descs[i]);
		if (IS_ERR(obj)) {
			return PTR_ERR(obj);
		}

		if (i == 0)
			*o1 = obj;
		if (o2 && (i == 1))
			*o2 = obj;

	}

	return 0;
}

static inline int handle_video2_buf_desc(struct dce_file_priv *priv,
		void **ptr, void *end, struct dce_rpc_hdr *req, uint64_t usr,
		int mem_type, struct drm_gem_object **o1,
		struct drm_gem_object **o2, uint8_t *len)
{
	struct video2_buf_desc *bufs = *ptr;
	uint32_t base_bo;
	int i, ret;

	ret = cfu(ptr, usr, sizeof(*bufs), end);
	if (ret)
		return ret;

	*len = sizeof(*bufs) / 4;

	/* the bo used as a base for XDM_MEMTYPE_BO_OFFSET descriptors */
	base_bo = bufs->plane_desc[0].buf;

	/* handle buffer mapping.. */
	for (i = 0; i < bufs->num_planes; i++) {
		struct drm_gem_object *obj =
				handle_single_buf_desc(priv, req, base_bo,
						mem_type, &bufs->plane_desc[i]);
		if (IS_ERR(obj)) {
			return PTR_ERR(obj);
		}

		if (i == 0)
			*o1 = obj;
		if (o2 && (i == 1))
			*o2 = obj;
	}

	return 0;
}

/*
 *   VIDDEC3_process			VIDENC2_process
 *   VIDDEC3_InArgs *inArgs		VIDENC2_InArgs *inArgs
 *   XDM2_BufDesc *outBufs		XDM2_BufDesc *outBufs
 *   XDM2_BufDesc *inBufs		VIDEO2_BufDesc *inBufs
 */

static inline int handle_videnc2(struct dce_file_priv *priv,
		void **ptr, void *end, int32_t *input_id,
		struct dce_rpc_codec_process_req *req,
		struct drm_omap_dce_codec_process *arg)
{
	struct drm_gem_object *out = NULL, *y = NULL, *uv = NULL;
	struct videnc2_in_args *in_args = *ptr;
	int ret;

	/* handle in_args: */
	ret = cfu(ptr, arg->in_args, sizeof(*in_args), end);
	if (ret)
		return ret;

	if (in_args->size > sizeof(*in_args)) {
		int sz = in_args->size - sizeof(*in_args);
		/* this param can be variable length */
		ret = cfu(ptr, arg->in_args + sizeof(*in_args), sz, end);
		if (ret)
			return ret;
		/* in case the extra part size is not multiple of 4 */
		*ptr += round_up(sz, 4) - sz;
	}

	req->in_args_len = round_up(in_args->size, 4) / 4;

	/* handle out_bufs: */
	ret = handle_buf_desc(priv, ptr, end, hdr(req), arg->out_bufs,
			XDM_MEMTYPE_RAW, &out, NULL, &req->out_bufs_len);
	if (ret)
		return ret;

	/* handle in_bufs: */
	ret = handle_video2_buf_desc(priv, ptr, end, hdr(req), arg->in_bufs,
			XDM_MEMTYPE_TILEDPAGE, &y, &uv, &req->in_bufs_len);
	if (ret)
		return ret;

	*input_id = in_args->input_id;
	/* input_id=0 means no bufs, so we don't need to lock anything */
	if (in_args->input_id != 0)
		codec_lockbuf(priv, arg->codec_handle, in_args->input_id, y, uv);

	return 0;
}

static inline int handle_viddec3(struct dce_file_priv *priv,
		void **ptr, void *end, int32_t *input_id,
		struct dce_rpc_codec_process_req *req,
		struct drm_omap_dce_codec_process *arg,
		enum dce_codec_quirks quirks
		)
{
	struct drm_gem_object *in, *y = NULL, *uv = NULL;
	struct viddec3_in_args *in_args = *ptr;
	int mem_type;
	int ret;

	/* handle in_args: */
	ret = cfu(ptr, arg->in_args, sizeof(*in_args), end);
	if (ret)
		return ret;

	if (in_args->size > sizeof(*in_args)) {
		int sz = in_args->size - sizeof(*in_args);
		/* this param can be variable length */
		ret = cfu(ptr, arg->in_args + sizeof(*in_args), sz, end);
		if (ret)
			return ret;
		/* in case the extra part size is not multiple of 4 */
		*ptr += round_up(sz, 4) - sz;
	}

	req->in_args_len = round_up(in_args->size, 4) / 4;

	if (quirks & DCE_CODEC_QUIRKS_OUTBUFS_MEMTYPE_RAW)
		mem_type = XDM_MEMTYPE_RAW;
	else
		mem_type = XDM_MEMTYPE_TILEDPAGE;

	/* handle out_bufs: */
	ret = handle_buf_desc(priv, ptr, end, hdr(req), arg->out_bufs,
			mem_type, &y, &uv, &req->out_bufs_len);
	if (ret)
		return ret;

	/* handle in_bufs: */
	ret = handle_buf_desc(priv, ptr, end, hdr(req), arg->in_bufs,
			XDM_MEMTYPE_RAW, &in, NULL, &req->in_bufs_len);
	if (ret)
		return ret;

	*input_id = in_args->input_id;
	/* input_id=0 means no bufs, so we don't need to lock anything */
	if (in_args->input_id != 0)
		codec_lockbuf(priv, arg->codec_handle, in_args->input_id, y, uv);

	return 0;
}

#define RPMSG_BUF_SIZE		(512)  // ugg, would be nice not to hard-code..

static int ioctl_codec_process(struct drm_device *dev, void *data,
		struct drm_file *file)
{
	struct dce_file_priv *priv = omap_drm_file_priv(file, dce_mapper_id);
	struct drm_omap_dce_codec_process *arg = data;
	struct dce_rpc_codec_process_rsp *rsp;
	int ret, i;

	/* if we are not re-starting a syscall, send req */
	if (!arg->token) {
		/* worst-case size allocation.. */
		struct dce_rpc_codec_process_req *req = kzalloc(RPMSG_BUF_SIZE, GFP_KERNEL);
		void *ptr = &req->data[0];
		void *end = ((void *)req) + RPMSG_BUF_SIZE;
		int32_t input_id = 0;
		enum dce_codec_quirks quirks = DCE_CODEC_QUIRKS_NONE;

		req->hdr = MKHDR(CODEC_PROCESS);

		ret = codec_get(priv, arg->codec_handle, &req->codec, &req->codec_id);
		if (ret)
			goto rpsend_out;

		codec_get_quirks(priv, arg->codec_handle, &quirks);

		ret = PTR_RET(get_paddr(priv, hdr(&req), &req->out_args, arg->out_args_bo));
		if (ret)
			return rpabort(hdr(req), ret);

		/* the remainder of the req varies depending on codec family */
		switch (req->codec_id) {
		case OMAP_DCE_VIDENC2:
			ret = handle_videnc2(priv, &ptr, end, &input_id, req, arg);
			break;
		case OMAP_DCE_VIDDEC3:
			ret = handle_viddec3(priv, &ptr, end, &input_id, req, arg, quirks);
			break;
		default:
			ret = -EINVAL;
			break;
		}

		if (ret)
			goto rpsend_out;

		ret = rpsend(priv, &arg->token, hdr(req), ptr - (void *)req);
rpsend_out:
		kfree(req);
		if (ret) {
			/* if input buffer is already locked, unlock it now so we
			 * don't have a leak:
			 */
			if (input_id)
				codec_unlockbuf(priv, arg->codec_handle, input_id);
			return rpabort(hdr(req), ret);
		}
	}

	/* then wait for reply, which is interruptible */
	ret = rpwait(priv, arg->token, hdr(&rsp), sizeof(*rsp));
	if (ret)
		return ret;

	for (i = 0; i < rsp->count; i++) {
		codec_unlockbuf(priv, arg->codec_handle, rsp->freebuf_ids[i]);
	}

	arg->result = rsp->result;

	kfree(rsp);

	return 0;
}

static int codec_delete(struct dce_file_priv *priv, uint32_t codec,
		enum omap_dce_codec codec_id)
{
	struct dce_rpc_codec_delete_req req = {
			.hdr = MKHDR(CODEC_DELETE),
			.codec_id = codec_id,
			.codec = codec,
	};
	return rpsend(priv, NULL, hdr(&req), sizeof(req));
}

static int ioctl_codec_delete(struct drm_device *dev, void *data,
		struct drm_file *file)
{
	struct dce_file_priv *priv = omap_drm_file_priv(file, dce_mapper_id);
	struct drm_omap_dce_codec_delete *arg = data;
	uint32_t codec, codec_id;
	int ret;

	ret = codec_get(priv, arg->codec_handle, &codec, &codec_id);
	if (ret)
		return ret;

	codec_unregister(priv, arg->codec_handle);

	return codec_delete(priv, codec, codec_id);
}

/* NOTE: these are not public because the actual ioctl NR is dynamic..
 * use drmCommandXYZ(fd, dce_base + idx, ..)
 */
#define DRM_IOCTL_OMAP_DCE_ENGINE_OPEN		DRM_IOWR(DRM_OMAP_DCE_ENGINE_OPEN, struct drm_omap_dce_engine_open)
#define DRM_IOCTL_OMAP_DCE_ENGINE_CLOSE		DRM_IOW (DRM_OMAP_DCE_ENGINE_CLOSE, struct drm_omap_dce_engine_close)
#define DRM_IOCTL_OMAP_DCE_CODEC_CREATE		DRM_IOWR(DRM_OMAP_DCE_CODEC_CREATE, struct drm_omap_dce_codec_create)
#define DRM_IOCTL_OMAP_DCE_CODEC_CONTROL	DRM_IOWR(DRM_OMAP_DCE_CODEC_CONTROL, struct drm_omap_dce_codec_control)
#define DRM_IOCTL_OMAP_DCE_CODEC_PROCESS	DRM_IOWR(DRM_OMAP_DCE_CODEC_PROCESS, struct drm_omap_dce_codec_process)
#define DRM_IOCTL_OMAP_DCE_CODEC_DELETE		DRM_IOW (DRM_OMAP_DCE_CODEC_DELETE, struct drm_omap_dce_codec_delete)
#define DRM_IOCTL_OMAP_DCE_CODEC_GET_VERSION	DRM_IOW (DRM_OMAP_DCE_CODEC_GET_VERSION, struct drm_omap_dce_codec_get_version)

static struct drm_ioctl_desc dce_ioctls[] = {
		DRM_IOCTL_DEF_DRV(OMAP_DCE_ENGINE_OPEN, ioctl_engine_open, DRM_UNLOCKED|DRM_AUTH),
		DRM_IOCTL_DEF_DRV(OMAP_DCE_ENGINE_CLOSE, ioctl_engine_close, DRM_UNLOCKED|DRM_AUTH),
		DRM_IOCTL_DEF_DRV(OMAP_DCE_CODEC_CREATE, ioctl_codec_create, DRM_UNLOCKED|DRM_AUTH),
		DRM_IOCTL_DEF_DRV(OMAP_DCE_CODEC_CONTROL, ioctl_codec_control, DRM_UNLOCKED|DRM_AUTH),
		DRM_IOCTL_DEF_DRV(OMAP_DCE_CODEC_PROCESS, ioctl_codec_process, DRM_UNLOCKED|DRM_AUTH),
		DRM_IOCTL_DEF_DRV(OMAP_DCE_CODEC_DELETE, ioctl_codec_delete, DRM_UNLOCKED|DRM_AUTH),
		DRM_IOCTL_DEF_DRV(OMAP_DCE_CODEC_GET_VERSION, ioctl_codec_get_version, DRM_UNLOCKED|DRM_AUTH),
};

/*
 * Plugin API:
 */

static int dce_load(struct drm_device *dev, unsigned long flags)
{
	dce_mapper_id = omap_drm_register_mapper();
	return 0;
}

static int dce_unload(struct drm_device *dev)
{
	omap_drm_unregister_mapper(dce_mapper_id);
	dce_mapper_id = -1;
	// XXX should block until pending txns are done..
	return 0;
}

static int dce_open(struct drm_device *dev, struct drm_file *file)
{
	struct dce_file_priv *priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	priv->dev = dev;
	priv->file = file;
	omap_drm_file_set_priv(file, dce_mapper_id, priv);
	return 0;
}

static int dce_release(struct drm_device *dev, struct drm_file *file)
{
	struct dce_file_priv *priv = omap_drm_file_priv(file, dce_mapper_id);
	int i;

	// XXX not sure if this is legit..  maybe we end up with this scenario
	// when drm device file is opened prior to dce module being loaded??
	WARN_ON(!priv);
	if (!priv)
		return 0;

	/* cleanup any remaining codecs and engines on behalf of the process,
	 * in case the process crashed or didn't clean up properly for itself:
	 */

	for (i = 0; i < ARRAY_SIZE(priv->codecs); i++) {
		uint32_t codec = priv->codecs[i].codec;
		if (codec) {
			enum omap_dce_codec codec_id = priv->codecs[i].codec_id;
			codec_unregister(priv, i+1);
			codec_delete(priv, codec, codec_id);
		}
	}

	for (i = 0; i < ARRAY_SIZE(priv->engines); i++) {
		uint32_t engine = priv->engines[i].engine;
		if (engine) {
			engine_unregister(priv, i+1);
			engine_close(priv, engine);
		}
	}

	for (i = 0; i < ARRAY_SIZE(txns); i++) {
		if (txns[i].priv == priv) {
			txn_cleanup(&txns[i]);
			memset(&txns[i], 0, sizeof(txns[i]));
		}
	}

	kfree(priv);

	return 0;
}

static struct omap_drm_plugin plugin = {
		.name = "dce",

		.load = dce_load,
		.unload = dce_unload,
		.open = dce_open,
		.release = dce_release,

		.ioctls = dce_ioctls,
		.num_ioctls = ARRAY_SIZE(dce_ioctls),
		.ioctl_base = 0,  /* initialized when plugin is registered */
};

/*
 * RPMSG API:
 */

static int dce_pm_find_device(struct dce_pm_device *pm_device)
{
	int ret = 0;

	pm_device->dev = bus_find_device_by_name(&platform_bus_type,
						     NULL, pm_device->name);
	if (!pm_device->dev)
		ret = -1;

	return ret;
}

static int rpmsg_probe(struct rpmsg_channel *_rpdev)
{
	struct dce_rpc_connect_req req = {
			.hdr = MKHDR(CONNECT),
			.chipset_id = GET_OMAP_TYPE,
			.debug = drm_debug ? 1 : 3,
	};
	int ret, i;

	DBG("");
	rpdev = _rpdev;

	/* pm_runtime initialisation */
	for (i = 0 ;i < NB_PM_DEVICES; i++) {
		ret = dce_pm_find_device(&dce_pm_device_list[i]);
		if (ret) {
			DBG("couldn't find pm device %s \n",
			    dce_pm_device_list[i].name);
			return ret;
		}

		if (!pm_runtime_enabled(dce_pm_device_list[i].dev))
			pm_runtime_enable(dce_pm_device_list[i].dev);
	}

	dce_pm_runtime_get_sync();

	/* send connect msg: */
	ret = rpsend(NULL, NULL, hdr(&req), sizeof(req));
	if (ret) {
		DBG("rpsend failed: %d", ret);
		return ret;
	}

	return omap_drm_register_plugin(&plugin);
}

static void __devexit rpmsg_remove(struct rpmsg_channel *_rpdev)
{
	DBG("");
	omap_drm_unregister_plugin(&plugin);
	dce_pm_runtime_put_sync();
	rpdev = NULL;
}

static void rpmsg_cb(struct rpmsg_channel *rpdev, void *data,
		int len, void *priv, u32 src)
{
	void *data2;
	DBG("len=%d, src=%d", len, src);
	/* note: we have to copy the data, because the ptr is no more valid
	 * once this fxn returns, and it could take a while for the requesting
	 * thread to pick up the data.. maybe there is a more clever way to
	 * handle this..
	 */
	data2 = kzalloc(len, GFP_KERNEL);
	memcpy(data2, data, len);
	rpcomplete(data2, len);
}

static int dce_resume(struct device *dev)
{
	struct dce_rpc_connect_req req = {
			.hdr = MKHDR(CONNECT),
			.chipset_id = GET_OMAP_TYPE,
			.debug = drm_debug ? 1 : 3,
	};
	int ret;

	/* send resume msg: */
	ret = rpsend(NULL, NULL, hdr(&req), sizeof(req));
	if (ret) {
		DBG("rpsend failed: %d", ret);
		return ret;
	}

	return 0;
}

static const struct dev_pm_ops dce_pm_ops = {
	.resume = dce_resume,
};


static struct rpmsg_device_id rpmsg_id_table[] = {
		{ .name = "rpmsg-dce" },
		{ },
};

static struct rpmsg_driver rpmsg_driver = {
		.drv.name       = KBUILD_MODNAME,
		.drv.owner      = THIS_MODULE,
		.drv.pm         = &dce_pm_ops,
		.id_table       = rpmsg_id_table,
		.probe          = rpmsg_probe,
		.callback       = rpmsg_cb,
		.remove         = __devexit_p(rpmsg_remove),
};

static int __init omap_dce_init(void)
{
	DBG("");
	return register_rpmsg_driver(&rpmsg_driver);
}

static void __exit omap_dce_fini(void)
{
	DBG("");
	unregister_rpmsg_driver(&rpmsg_driver);
}

module_init(omap_dce_init);
module_exit(omap_dce_fini);

MODULE_AUTHOR("Rob Clark <rob.clark@linaro.org>");
MODULE_DESCRIPTION("OMAP DRM Video Decode/Encode");
MODULE_LICENSE("GPL v2");