aboutsummaryrefslogtreecommitdiffstats
blob: 2bfe52d7e0cfd730827827039bb2d31353c21ac8 (plain) (blame)
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
/*
 * gcmmu.c
 *
 * Copyright (C) 2010-2011 Vivante Corporation.
 *
 * This package 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 PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/highmem.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/pagemap.h>
#include <linux/sched.h>
#include "gcmain.h"

#define GCZONE_NONE		0
#define GCZONE_ALL		(~0U)
#define GCZONE_INIT		(1 << 0)
#define GCZONE_MAPPING		(1 << 1)
#define GCZONE_CONTEXT		(1 << 2)
#define GCZONE_MASTER		(1 << 3)
#define GCZONE_FIXUP		(1 << 4)
#define GCZONE_FLUSH		(1 << 5)
#define GCZONE_ARENA		(1 << 6)
#define GCZONE_DUMPMAP		(1 << 7)
#define GCZONE_DUMPUNMAP	(1 << 8)

GCDBG_FILTERDEF(mmu, GCZONE_NONE,
		"init",
		"mapping",
		"context",
		"master",
		"fixup",
		"flush",
		"arena",
		"dumpmap",
		"dumpunmap")


#define USE_CACHED_MASTER	0
#define USE_CACHED_SLAVE	0
/*******************************************************************************
 * Internal definitions.
 */

/* Slave table preallocation block; can describe an array of slave tables. */
struct gcmmustlbblock {
	/* Slave table allocation. */
	struct gcpage pages;

	/* Next block of preallocated slave memory. */
	struct gcmmustlbblock *next;
};


/*******************************************************************************
 * Call back to enable MMU.
 */

static void event_enable_mmu(struct gcevent *gcevent, unsigned int *flags)
{
	GCENTER(GCZONE_INIT);

	/*
	* Enable MMU. For security reasons, once it is enabled,
	* the only way to disable is to reset the system.
	*/
	gc_write_reg(
		GCREG_MMU_CONTROL_Address,
		GCSETFIELDVAL(0, GCREG_MMU_CONTROL, ENABLE, ENABLE));

	/* After MMU command buffer is processed, FE will stop.
	 * Let the control thread know that FE needs to be restarted. */
	if (flags == NULL)
		GCERR("flags are not set.\n");
	else
		*flags |= GC_CMDBUF_START_FE;

	GCEXIT(GCZONE_INIT);
}


/*******************************************************************************
 * Arena record management.
 */

static enum gcerror get_arena(struct gcmmu *gcmmu, struct gcmmuarena **arena)
{
	enum gcerror gcerror = GCERR_NONE;
	struct gcmmuarena *temp;

	GCENTER(GCZONE_ARENA);

	GCLOCK(&gcmmu->lock);

	if (list_empty(&gcmmu->vacarena)) {
		temp = kmalloc(sizeof(struct gcmmuarena), GFP_KERNEL);
		if (temp == NULL) {
			GCERR("arena entry allocation failed.\n");
			gcerror = GCERR_SETGRP(GCERR_OODM,
						GCERR_MMU_ARENA_ALLOC);
			goto exit;
		}
	} else {
		struct list_head *head;
		head = gcmmu->vacarena.next;
		temp = list_entry(head, struct gcmmuarena, link);
		list_del(head);
	}

	*arena = temp;

exit:
	GCUNLOCK(&gcmmu->lock);

	GCEXITARG(GCZONE_ARENA, "gc%s = 0x%08X\n",
		(gcerror == GCERR_NONE) ? "result" : "error", gcerror);
	return gcerror;
}

static inline bool siblings(struct list_head *head,
			    struct list_head *arenahead1,
			    struct list_head *arenahead2)
{
	struct gcmmuarena *arena1;
	struct gcmmuarena *arena2;

	if ((arenahead1 == head) || (arenahead2 == head))
		return false;

	arena1 = list_entry(arenahead1, struct gcmmuarena, link);
	arena2 = list_entry(arenahead2, struct gcmmuarena, link);

	return (arena1->end.absolute == arena2->start.absolute) ? true : false;
}

/*******************************************************************************
 * Slave table allocation management.
 */

static enum gcerror allocate_slave(struct gcmmucontext *gcmmucontext,
				   union gcmmuloc index)
{
	enum gcerror gcerror;
	struct gcmmustlbblock *block = NULL;
	struct gcmmustlb *slave;
	unsigned int *mtlblogical;
	unsigned int prealloccount;
	unsigned int preallocsize;
	unsigned int preallocentries;
	unsigned int physical;
	unsigned int *logical;
	unsigned int i;

	GCENTER(GCZONE_MAPPING);

	/* Allocate a new prealloc block wrapper. */
	block = kmalloc(sizeof(struct gcmmustlbblock), GFP_KERNEL);
	if (block == NULL) {
		GCERR("failed to allocate slave page table wrapper\n");
		gcerror = GCERR_SETGRP(GCERR_OODM,
				       GCERR_MMU_STLB_ALLOC);
		goto exit;
	}

	/* Determine the number and the size of tables to allocate. */
	prealloccount = min(GCMMU_STLB_PREALLOC_COUNT,
			    GCMMU_MTLB_ENTRY_NUM - index.loc.mtlb);

	preallocsize = prealloccount * GCMMU_STLB_SIZE;
	preallocentries = prealloccount * GCMMU_STLB_ENTRY_NUM;

	GCDBG(GCZONE_MAPPING, "preallocating %d slave tables.\n",
	      prealloccount);

	/* Allocate slave table pool. */
#if USE_CACHED_SLAVE
	gcerror = gc_alloc_cached(&block->pages, preallocsize);
#else
	gcerror = gc_alloc_noncached(&block->pages, preallocsize);
#endif
	if (gcerror != GCERR_NONE) {
		GCERR("failed to allocate slave page table\n");
		gcerror = GCERR_SETGRP(gcerror, GCERR_MMU_STLB_ALLOC);
		goto exit;
	}

	/* Add the block to the list. */
	block->next = gcmmucontext->slavealloc;
	gcmmucontext->slavealloc = block;

	/* Get shortcuts to the pointers. */
	physical = block->pages.physical;
	logical = block->pages.logical;

	/* Invalidate all slave entries. */
	for (i = 0; i < preallocentries; i += 1)
		logical[i] = GCMMU_STLB_ENTRY_VACANT;

	/* Init the slaves. */
	slave = &gcmmucontext->slave[index.loc.mtlb];
	mtlblogical = &gcmmucontext->master.logical[index.loc.mtlb];

	for (i = 0; i < prealloccount; i += 1) {
		mtlblogical[i]
			= (physical & GCMMU_MTLB_SLAVE_MASK)
			| GCMMU_MTLB_4K_PAGE
			| GCMMU_MTLB_EXCEPTION
			| GCMMU_MTLB_PRESENT;

		slave[i].physical = physical;
		slave[i].logical = logical;

		physical += GCMMU_STLB_SIZE;
		logical = (unsigned int *)
			((unsigned char *) logical + GCMMU_STLB_SIZE);
	}

#if USE_CACHED_MASTER
	/* Flush CPU cache. */
	gc_flush_region(gcmmucontext->master.physical,
			gcmmucontext->master.logical,
			index.loc.mtlb * sizeof(unsigned int),
			prealloccount * sizeof(unsigned int));
#else
		mb();
#endif


	GCEXIT(GCZONE_MAPPING);
	return GCERR_NONE;

exit:
	if (block != NULL)
		kfree(block);

	GCEXITARG(GCZONE_MAPPING, "gc%s = 0x%08X\n",
		(gcerror == GCERR_NONE) ? "result" : "error", gcerror);
	return gcerror;
}

/*******************************************************************************
 * Physical page array generation.
 */

static enum gcerror virt2phys(unsigned int logical, pte_t *physical)
{
	pgd_t *pgd;	/* Page Global Directory (PGD). */
	pmd_t *pmd;	/* Page Middle Directory (PMD). */
	pte_t *pte;	/* Page Table Entry (PTE). */

	/* Get the pointer to the entry in PGD for the address. */
	pgd = pgd_offset(current->mm, logical);
	if (pgd_none(*pgd) || pgd_bad(*pgd))
		return GCERR_MMU_PAGE_BAD;

	/* Get the pointer to the entry in PMD for the address. */
	pmd = pmd_offset((pud_t *)pgd, logical);
	if (pmd_none(*pmd) || pmd_bad(*pmd))
		return GCERR_MMU_PAGE_BAD;

	/* Get the pointer to the entry in PTE for the address. */
	pte = pte_offset_map(pmd, logical);
	if ((pte == NULL) || !pte_present(*pte))
		return GCERR_MMU_PAGE_BAD;

	*physical = (*pte & PAGE_MASK) | (logical & ~PAGE_MASK);
	return GCERR_NONE;
}

#if !defined(PFN_DOWN)
#	define PFN_DOWN(x) \
		((x) >> PAGE_SHIFT)
#endif

#if !defined(phys_to_pfn)
#	define phys_to_pfn(phys) \
		(PFN_DOWN(phys))
#endif

#if !defined(phys_to_page)
#	define phys_to_page(paddr) \
		(pfn_to_page(phys_to_pfn(paddr)))
#endif

static enum gcerror get_physical_pages(struct gcmmuphysmem *mem,
				       pte_t *parray,
				       struct gcmmuarena *arena)
{
	enum gcerror gcerror = GCERR_NONE;
	struct vm_area_struct *vma;
	struct page **pages = NULL;
	unsigned long base, start, end;
	unsigned int write;
	int i, count = 0;

	GCENTER(GCZONE_MAPPING);

	/* Get the base address. */
	base = mem->base;

	/* Reset page descriptor array. */
	arena->pages = NULL;

	/* Store the logical pointer. */
	arena->logical = (void *) base;

	/* Compute virtual memory area limits. */
	start = base + mem->offset;
	end = base + mem->count * mem->pagesize;
	GCDBG(GCZONE_MAPPING, "base = 0x%08X\n", base);
	GCDBG(GCZONE_MAPPING, "offset = 0x%08X\n", mem->offset);
	GCDBG(GCZONE_MAPPING, "count = %d\n", mem->count);
	GCDBG(GCZONE_MAPPING, "start = 0x%08X\n", start);
	GCDBG(GCZONE_MAPPING, "end = 0x%08X\n", end);

	vma = find_vma(current->mm, start);
	if (vma == NULL) {
		gcerror = GCERR_MMU_BUFFER_BAD;
		GCERR("failed to find VMA.\n");
		goto exit;
	}

	GCDBG(GCZONE_MAPPING, "vm_start = 0x%08X\n", vma->vm_start);
	GCDBG(GCZONE_MAPPING, "vm_end = 0x%08X\n", vma->vm_end);
	if ((start < vma->vm_start) || (end > vma->vm_end)) {
		gcerror = GCERR_MMU_BUFFER_BAD;
		GCERR("failed to find VMA...\n");
		goto exit;
	}

	/* Allocate page descriptor array. */
	pages = kmalloc(mem->count * sizeof(struct page *), GFP_KERNEL);
	if (pages == NULL) {
		GCERR("failed to allocate page descriptor array.\n");
		gcerror = GCERR_SETGRP(GCERR_OODM, GCERR_MMU_DESC_ALLOC);
		goto exit;
	}

	/* Query page descriptors. */
	write = ((vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) != 0) ? 1 : 0;

	down_read(&current->mm->mmap_sem);
	count = get_user_pages(current, current->mm, base, mem->count,
			       write, 1, pages, NULL);
	up_read(&current->mm->mmap_sem);

	if (count < 0) {
		/* Kernel allocated buffer. */
		for (i = 0; i < mem->count; i += 1) {
			gcerror = virt2phys(base, &parray[i]);
			if (gcerror != GCERR_NONE) {
				GCERR("failed to convert virtual address.\n");
				goto exit;
			}

			base += mem->pagesize;
		}
	} else if (count == mem->count) {
		/* User allocated buffer. */
		for (i = 0; i < mem->count; i += 1) {
			parray[i] = page_to_phys(pages[i]);
			if (phys_to_page(parray[i]) != pages[i]) {
				GCERR("failed to convert page address.\n");
				gcerror = GCERR_MMU_PAGE_BAD;
				goto exit;
			}
		}

		/* Set page descriptor array. */
		arena->pages = pages;
	} else {
		GCERR("invalid number of pages.\n");
		gcerror = GCERR_MMU_BUFFER_BAD;
		goto exit;
	}

exit:
	if (arena->pages == NULL) {
		for (i = 0; i < count; i += 1)
			page_cache_release(pages[i]);

		if (pages != NULL)
			kfree(pages);
	}

	GCEXIT(GCZONE_MAPPING);
	return gcerror;
}

static void release_physical_pages(struct gcmmuarena *arena)
{
	unsigned int i;

	if (arena->pages != NULL) {
		for (i = 0; i < arena->count; i += 1)
			page_cache_release(arena->pages[i]);

		kfree(arena->pages);
		arena->pages = NULL;
	}
}

/*******************************************************************************
 * MMU management API.
 */

enum gcerror gcmmu_init(struct gccorecontext *gccorecontext)
{
	enum gcerror gcerror;
	struct gcmmu *gcmmu = &gccorecontext->gcmmu;

	GCENTER(GCZONE_INIT);

	/* Initialize access lock. */
	GCLOCK_INIT(&gcmmu->lock);

	/* Allocate one page. */
	gcerror = gc_alloc_noncached(&gcmmu->gcpage, PAGE_SIZE);
	if (gcerror != GCERR_NONE) {
		GCERR("failed to allocate MMU management buffer.\n");
		gcerror = GCERR_SETGRP(gcerror, GCERR_MMU_SAFE_ALLOC);
		goto exit;
	}

	/* Determine the location of the physical command buffer. */
	gcmmu->cmdbufphys = gcmmu->gcpage.physical;
	gcmmu->cmdbuflog = gcmmu->gcpage.logical;
	gcmmu->cmdbufsize = PAGE_SIZE - GCMMU_SAFE_ZONE_SIZE;

	/* Determine the location of the safe zone. */
	gcmmu->safezonephys = gcmmu->gcpage.physical + gcmmu->cmdbufsize;
	gcmmu->safezonelog = (unsigned int *) ((unsigned char *)
		gcmmu->gcpage.logical + gcmmu->cmdbufsize);
	gcmmu->safezonesize = GCMMU_SAFE_ZONE_SIZE;

	/* Reset the master table. */
	gcmmu->master = ~0U;

	/* Initialize the list of vacant arenas. */
	INIT_LIST_HEAD(&gcmmu->vacarena);

exit:
	GCEXITARG(GCZONE_INIT, "gc%s = 0x%08X\n",
		(gcerror == GCERR_NONE) ? "result" : "error", gcerror);
	return gcerror;
}

void gcmmu_exit(struct gccorecontext *gccorecontext)
{
	struct gcmmu *gcmmu = &gccorecontext->gcmmu;
	struct list_head *head;
	struct gcmmuarena *arena;

	GCENTER(GCZONE_INIT);

	/* Free the safe zone. */
	gc_free_noncached(&gcmmu->gcpage);

	/* Free vacant arena list. */
	while (!list_empty(&gcmmu->vacarena)) {
		head = gcmmu->vacarena.next;
		arena = list_entry(head, struct gcmmuarena, link);
		list_del(head);
		kfree(arena);
	}

	GCEXIT(GCZONE_INIT);
}

enum gcerror gcmmu_create_context(struct gccorecontext *gccorecontext,
				  struct gcmmucontext *gcmmucontext,
				  pid_t pid)
{
	enum gcerror gcerror;
	struct gcmmu *gcmmu = &gccorecontext->gcmmu;
	struct gcmmuarena *arena = NULL;
	unsigned int *logical;
	unsigned int i;

	GCENTER(GCZONE_CONTEXT);

	if (gcmmucontext == NULL) {
		gcerror = GCERR_MMU_CTXT_BAD;
		goto exit;
	}

	/* Reset the context. */
	memset(gcmmucontext, 0, sizeof(struct gcmmucontext));

	/* Initialize access lock. */
	GCLOCK_INIT(&gcmmucontext->lock);

	/* Initialize arena lists. */
	INIT_LIST_HEAD(&gcmmucontext->vacant);
	INIT_LIST_HEAD(&gcmmucontext->allocated);

	/* Mark context as dirty. */
	gcmmucontext->dirty = true;

	/* Set PID. */
	gcmmucontext->pid = pid;

	/* Allocate MTLB table. */
#if USE_CACHED_MASTER
	gcerror = gc_alloc_cached(&gcmmucontext->master, GCMMU_MTLB_SIZE);
#else
	gcerror = gc_alloc_noncached(&gcmmucontext->master, GCMMU_MTLB_SIZE);
#endif
	if (gcerror != GCERR_NONE) {
		gcerror = GCERR_SETGRP(gcerror, GCERR_MMU_MTLB_ALLOC);
		goto exit;
	}

	/* Invalidate MTLB entries. */
	logical = gcmmucontext->master.logical;
	for (i = 0; i < GCMMU_MTLB_ENTRY_NUM; i += 1)
		logical[i] = GCMMU_MTLB_ENTRY_VACANT;

	/* Set MMU table mode. */
	gcmmucontext->mmuconfig.reg.master_mask
		= GCREG_MMU_CONFIGURATION_MASK_MODE_ENABLED;
	gcmmucontext->mmuconfig.reg.master = GCMMU_MTLB_MODE;

	/* Set the table address. */
	gcmmucontext->mmuconfig.reg.address_mask
		= GCREG_MMU_CONFIGURATION_MASK_ADDRESS_ENABLED;
	gcmmucontext->mmuconfig.reg.address
		= GCGETFIELD(gcmmucontext->master.physical,
			     GCREG_MMU_CONFIGURATION, ADDRESS);

	/* Allocate the first vacant arena. */
	gcerror = get_arena(gcmmu, &arena);
	if (gcerror != GCERR_NONE)
		goto exit;

	/* Entire range is currently vacant. */
	arena->start.absolute = 0;
	arena->end.absolute =
	arena->count = GCMMU_MTLB_ENTRY_NUM * GCMMU_STLB_ENTRY_NUM;
	list_add(&arena->link, &gcmmucontext->vacant);
	GCDUMPARENA(GCZONE_ARENA, "initial vacant arena", arena);

	/* Map the command queue. */
	gcerror = gcqueue_map(gccorecontext, gcmmucontext);
	if (gcerror != GCERR_NONE)
		goto exit;

	/* Reference MMU. */
	gcmmu->refcount += 1;

	GCEXIT(GCZONE_CONTEXT);
	return GCERR_NONE;

exit:
	gcmmu_destroy_context(gccorecontext, gcmmucontext);

	GCEXITARG(GCZONE_CONTEXT, "gcerror = 0x%08X\n", gcerror);
	return gcerror;
}

enum gcerror gcmmu_destroy_context(struct gccorecontext *gccorecontext,
				   struct gcmmucontext *gcmmucontext)
{
	enum gcerror gcerror;
	struct gcmmu *gcmmu = &gccorecontext->gcmmu;
	struct list_head *head;
	struct gcmmuarena *arena;
	struct gcmmustlbblock *nextblock;

	GCENTER(GCZONE_CONTEXT);

	if (gcmmucontext == NULL) {
		gcerror = GCERR_MMU_CTXT_BAD;
		goto exit;
	}

	/* Unmap the command queue. */
	gcerror = gcqueue_unmap(gccorecontext, gcmmucontext);
	if (gcerror != GCERR_NONE)
		goto exit;

	/* Free allocated arenas. */
	while (!list_empty(&gcmmucontext->allocated)) {
		head = gcmmucontext->allocated.next;
		arena = list_entry(head, struct gcmmuarena, link);
		release_physical_pages(arena);
		list_move(head, &gcmmucontext->vacant);
	}

	/* Free slave tables. */
	while (gcmmucontext->slavealloc != NULL) {
#if USE_CACHED_SLAVE
		gc_free_cached(&gcmmucontext->slavealloc->pages);
#else
		gc_free_noncached(&gcmmucontext->slavealloc->pages);
#endif
		nextblock = gcmmucontext->slavealloc->next;
		kfree(gcmmucontext->slavealloc);
		gcmmucontext->slavealloc = nextblock;
	}

	/* Reset the master table. */
	if (gcmmu->master == gcmmucontext->mmuconfig.raw)
		gcmmu->master = ~0U;

	/* Free the master table. */
#if USE_CACHED_MASTER
	gc_free_cached(&gcmmucontext->master);
#else
	gc_free_noncached(&gcmmucontext->master);
#endif

	/* Free arenas. */
	GCLOCK(&gcmmu->lock);
	list_splice_init(&gcmmucontext->vacant, &gcmmu->vacarena);
	GCUNLOCK(&gcmmu->lock);

	/* Dereference. */
	gcmmu->refcount -= 1;

	GCEXIT(GCZONE_CONTEXT);
	return GCERR_NONE;

exit:
	GCEXITARG(GCZONE_CONTEXT, "gcerror = 0x%08X\n", gcerror);
	return gcerror;
}

enum gcerror gcmmu_enable(struct gccorecontext *gccorecontext,
			  struct gcqueue *gcqueue)
{
	enum gcerror gcerror;
	struct gcmmu *gcmmu = &gccorecontext->gcmmu;
	struct list_head *head;
	struct gccmdbuf *headcmdbuf;
	struct gccmdbuf *gccmdbuf = NULL;
	struct gcevent *gcevent = NULL;
	struct gcmommuinit *gcmommuinit;
	struct gcmosignal *gcmosignal;
	struct gccmdend *gccmdend;
	unsigned int status, enabled;

	GCENTER(GCZONE_INIT);

	/* Read the MMU status. */
	status = gc_read_reg(GCREG_MMU_CONTROL_Address);
	enabled = GCGETFIELD(status, GCREG_MMU_CONTROL, ENABLE);

	/* Is MMU enabled? */
	if (!enabled) {
		GCDBG(GCZONE_MASTER, "enabling MMU.\n");

		/* Queue cannot be empty. */
		if (list_empty(&gcqueue->queue)) {
			GCERR("queue is empty.");
			gcerror = GCERR_MMU_INIT;
			goto fail;
		}

		/* Get the first entry from the active queue. */
		head = gcqueue->queue.next;
		headcmdbuf = list_entry(head, struct gccmdbuf, link);

		/* Allocate command init buffer. */
		gcerror = gcqueue_alloc_cmdbuf(gcqueue, &gccmdbuf);
		if (gcerror != GCERR_NONE)
			goto fail;

		/* Add event for the current command buffer. */
		gcerror = gcqueue_alloc_event(gcqueue, &gcevent);
		if (gcerror != GCERR_NONE)
			goto fail;

		/* Get free interrupt. */
		gcerror = gcqueue_alloc_int(gcqueue, &gccmdbuf->interrupt);
		if (gcerror != GCERR_NONE)
			goto fail;

		/* Initialize the event and add to the list. */
		gcevent->handler = event_enable_mmu;

		/* Attach records. */
		list_add_tail(&gcevent->link, &gccmdbuf->events);
		list_add(&gccmdbuf->link, &gcqueue->queue);

		/* Program the safe zone and the master table address. */
		gcmommuinit = (struct gcmommuinit *) gcmmu->cmdbuflog;
		gcmommuinit->safe_ldst = gcmommuinit_safe_ldst;
		gcmommuinit->safe = gcmmu->safezonephys;
		gcmommuinit->mtlb = headcmdbuf->gcmmucontext->mmuconfig.raw;

		/* Update the cached master */
		gcmmu->master = gcmommuinit->mtlb;

		/* Configure EVENT command. */
		gcmosignal = (struct gcmosignal *) (gcmommuinit + 1);
		gcmosignal->signal_ldst = gcmosignal_signal_ldst;
		gcmosignal->signal.raw = 0;
		gcmosignal->signal.reg.id = gccmdbuf->interrupt;
		gcmosignal->signal.reg.pe = GCREG_EVENT_PE_SRC_ENABLE;

		/* Configure the END command. */
		gccmdend = (struct gccmdend *) (gcmosignal + 1);
		gccmdend->cmd.raw = gccmdend_const.cmd.raw;

		/* Initialize the command buffer. */
		gccmdbuf->gcmmucontext = headcmdbuf->gcmmucontext;
		gccmdbuf->logical = (unsigned char *) gcmmu->cmdbuflog;
		gccmdbuf->physical = gcmmu->cmdbufphys;
		gccmdbuf->size = sizeof(struct gcmommuinit)
			       + sizeof(struct gcmosignal)
			       + sizeof(struct gccmdend);
		gccmdbuf->count = (gccmdbuf->size + 7) >> 3;
		gccmdbuf->gcmoterminator = NULL;

		GCDUMPBUFFER(GCZONE_INIT, gccmdbuf->logical,
				gccmdbuf->physical, gccmdbuf->size);
	}

	GCEXIT(GCZONE_INIT);
	return GCERR_NONE;

fail:
	if (gcevent != NULL)
		gcqueue_free_event(gcqueue, gcevent);

	if (gccmdbuf != NULL)
		gcqueue_free_cmdbuf(gcqueue, gccmdbuf, NULL);

	GCEXITARG(GCZONE_CONTEXT, "gcerror = 0x%08X\n", gcerror);
	return gcerror;
}

enum gcerror gcmmu_set_master(struct gccorecontext *gccorecontext,
			      struct gcmmucontext *gcmmucontext)
{
	enum gcerror gcerror;
	struct gcmmu *gcmmu = &gccorecontext->gcmmu;
	struct gcmommumaster *gcmommumaster;

	GCENTER(GCZONE_MASTER);

	if (gcmmucontext == NULL) {
		gcerror = GCERR_MMU_CTXT_BAD;
		goto exit;
	}

	/* Did the master change? */
	if (gcmmu->master == gcmmucontext->mmuconfig.raw) {
		gcerror = GCERR_NONE;
		goto exit;
	}

	/* Flush required when switching mmu contexts */
	gcmmucontext->dirty = true;

	/* Allocate command buffer space. */
	gcerror = gcqueue_alloc(gccorecontext, gcmmucontext,
				sizeof(struct gcmommumaster),
				(void **) &gcmommumaster, NULL);
	if (gcerror != GCERR_NONE) {
		gcerror = GCERR_SETGRP(gcerror, GCERR_MMU_MTLB_SET);
		goto exit;
	}

	/* Program master table address. */
	gcmommumaster->master_ldst = gcmommumaster_master_ldst;
	gcmommumaster->master = gcmmucontext->mmuconfig.raw;

	/* Update master table address. */
	gcmmu->master = gcmmucontext->mmuconfig.raw;

exit:
	GCEXITARG(GCZONE_MASTER, "gc%s = 0x%08X\n",
		(gcerror == GCERR_NONE) ? "result" : "error", gcerror);
	return gcerror;
}

enum gcerror gcmmu_map(struct gccorecontext *gccorecontext,
		       struct gcmmucontext *gcmmucontext,
		       struct gcmmuphysmem *mem,
		       struct gcmmuarena **mapped)
{
	enum gcerror gcerror = GCERR_NONE;
	bool locked = false;
	struct gcmmu *gcmmu = &gccorecontext->gcmmu;
	struct list_head *arenahead;
	struct gcmmuarena *vacant = NULL, *split;
	struct gcmmustlb *slave;
	unsigned int *stlblogical;
	union gcmmuloc index;
	unsigned int i, allocated, count;
	pte_t *parray_alloc = NULL;
	pte_t *parray;

	GCENTER(GCZONE_MAPPING);

	if (gcmmucontext == NULL) {
		gcerror = GCERR_MMU_CTXT_BAD;
		goto exit;
	}

	if ((mem == NULL) || (mem->count <= 0) || (mapped == NULL) ||
		((mem->pagesize != 0) && (mem->pagesize != GCMMU_PAGE_SIZE))) {
		gcerror = GCERR_MMU_ARG;
		goto exit;
	}

	GCLOCK(&gcmmucontext->lock);
	locked = true;

	/*
	 * Find available sufficient arena.
	 */

	GCDBG(GCZONE_MAPPING, "mapping (%d) pages\n", mem->count);

	list_for_each(arenahead, &gcmmucontext->vacant) {
		vacant = list_entry(arenahead, struct gcmmuarena, link);
		if (vacant->count >= mem->count)
			break;
	}

	if (arenahead == &gcmmucontext->vacant) {
		gcerror = GCERR_MMU_OOM;
		goto exit;
	}

	GCDUMPARENA(GCZONE_ARENA, "allocating from arena", vacant);

	/*
	 * If page array isn't provided, create it here.
	 */

	/* Reset page array. */
	vacant->pages = NULL;

	/* No page array given? */
	if (mem->pages == NULL) {
		/* Allocate physical address array. */
		parray_alloc = kmalloc(mem->count * sizeof(pte_t *),
					GFP_KERNEL);
		if (parray_alloc == NULL) {
			GCERR("failed to allocate page address array\n");
			gcerror = GCERR_SETGRP(GCERR_OODM,
						GCERR_MMU_PHYS_ALLOC);
			goto exit;
		}

		/* Fetch page addresses. */
		gcerror = get_physical_pages(mem, parray_alloc, vacant);
		if (gcerror != GCERR_NONE)
			goto exit;

		parray = parray_alloc;

		GCDBG(GCZONE_MAPPING,
			"physical page array allocated (0x%08X)\n",
			(unsigned int) parray);
	} else {
		parray = mem->pages;

		GCDBG(GCZONE_MAPPING,
			"physical page array provided (0x%08X)\n",
			(unsigned int) parray);
	}

	/*
	 * Create the mapping.
	 */

	index.absolute = vacant->start.absolute;
	slave = &gcmmucontext->slave[index.loc.mtlb];
	count = mem->count;

	while (count > 0) {
		/* Allocate slaves if not yet allocated. */
		if (slave->logical == NULL) {
			gcerror = allocate_slave(gcmmucontext, index);
			if (gcerror != GCERR_NONE)
				goto exit;
		}

		/* Determine the number of entries allocated. */
		allocated = GCMMU_STLB_ENTRY_NUM - index.loc.stlb;
		if (allocated > count)
			allocated = count;

		/* Initialize slave entries. */
		stlblogical = &slave->logical[index.loc.stlb];
		for (i = 0; i < allocated; i += 1)
			*stlblogical++
				= (*parray++ & GCMMU_STLB_ADDRESS_MASK)
				| GCMMU_STLB_PRESENT
				| GCMMU_STLB_EXCEPTION
				| GCMMU_STLB_WRITEABLE;

#if USE_CACHED_SLAVE
		/* Flush CPU cache. */
		gc_flush_region(slave->physical, slave->logical,
				index.loc.stlb * sizeof(unsigned int),
				allocated * sizeof(unsigned int));
#else
		mb();
#endif

		GCDBG(GCZONE_MAPPING, "allocated %d pages at %d.%d\n",
		      allocated, index.loc.mtlb, index.loc.stlb);

		/* Advance. */
		slave += 1;
		index.absolute += allocated;
		count -= allocated;
	}

	/*
	 * Claim arena.
	 */

	/* Split the arena. */
	if (vacant->count != mem->count) {
		GCDBG(GCZONE_MAPPING, "splitting vacant arena\n");

		gcerror = get_arena(gcmmu, &split);
		if (gcerror != GCERR_NONE)
			goto exit;

		split->start.absolute = index.absolute;
		split->end.absolute = vacant->end.absolute;
		split->count = vacant->count - mem->count;
		list_add(&split->link, &vacant->link);

		vacant->end.absolute = index.absolute;
		vacant->count = mem->count;
	}

	GCDUMPARENA(GCZONE_ARENA, "allocated arena", vacant);

	/* Move the vacant arena to the list of allocated arenas. */
	list_move(&vacant->link, &gcmmucontext->allocated);

	/* Set page size. */
	mem->pagesize = GCMMU_PAGE_SIZE;

	/* Determine the virtual address. */
	vacant->address
		= ((vacant->start.loc.mtlb << GCMMU_MTLB_SHIFT)
					    & GCMMU_MTLB_MASK)
		| ((vacant->start.loc.stlb << GCMMU_STLB_SHIFT)
					    & GCMMU_STLB_MASK)
		| (mem->offset & GCMMU_OFFSET_MASK);

	/* Determine the size of the area. */
	vacant->size = mem->count * GCMMU_PAGE_SIZE - mem->offset;

	/* Invalidate the MMU. */
	gcmmucontext->dirty = true;

	/* Set the result. */
	*mapped = vacant;

	GCDBG(GCZONE_MAPPING, "mapped %d bytes at 0x%08X\n",
		vacant->size, vacant->address);

	/* Dump tables. */
	GCDUMPMMU(GCZONE_DUMPMAP, gcmmucontext);

exit:
	if (parray_alloc != NULL) {
		kfree(parray_alloc);

		if (gcerror != GCERR_NONE)
			release_physical_pages(vacant);
	}

	if (locked)
		GCUNLOCK(&gcmmucontext->lock);

	GCEXITARG(GCZONE_MAPPING, "gc%s = 0x%08X\n",
		(gcerror == GCERR_NONE) ? "result" : "error", gcerror);
	return gcerror;
}

enum gcerror gcmmu_unmap(struct gccorecontext *gccorecontext,
			 struct gcmmucontext *gcmmucontext,
			 struct gcmmuarena *mapped)
{
	enum gcerror gcerror = GCERR_NONE;
	bool locked = false;
	struct gcmmu *gcmmu = &gccorecontext->gcmmu;
	struct list_head *allochead, *prevhead, *nexthead;
	struct gcmmuarena *allocated, *prevvacant, *nextvacant = NULL;
	struct gcmmustlb *slave;
	unsigned int *stlblogical;
	union gcmmuloc index;
	unsigned int i, freed, count;

	GCENTER(GCZONE_MAPPING);

	if (gcmmucontext == NULL) {
		gcerror = GCERR_MMU_CTXT_BAD;
		goto exit;
	}

	GCLOCK(&gcmmucontext->lock);
	locked = true;

	/*
	 * Find the arena.
	 */

	GCDBG(GCZONE_MAPPING, "unmapping arena 0x%08X\n",
		(unsigned int) mapped);

	list_for_each(allochead, &gcmmucontext->allocated) {
		allocated = list_entry(allochead, struct gcmmuarena, link);
		if (allocated == mapped)
			break;
	}

	if (allochead == &gcmmucontext->allocated) {
		GCERR("mapped arena 0x%08X not found.\n",
			(unsigned int) mapped);
		gcerror = GCERR_MMU_ARG;
		goto exit;
	}

	GCDBG(GCZONE_MAPPING, "mapped arena found:\n");
	GCDBG(GCZONE_MAPPING, "  arena phys = 0x%08X\n", allocated->address);
	GCDBG(GCZONE_MAPPING, "  arena size = %d\n", allocated->size);

	/*
	 * Free slave tables.
	 */

	index.absolute = allocated->start.absolute;
	slave = &gcmmucontext->slave[index.loc.mtlb];
	count = allocated->count;

	while (count > 0) {
		/* Determine the number of entries freed. */
		freed = GCMMU_STLB_ENTRY_NUM - index.loc.stlb;
		if (freed > count)
			freed = count;

		GCDBG(GCZONE_MAPPING, "freeing %d pages at %d.%d\n",
			freed, index.loc.mtlb, index.loc.stlb);

		/* Free slave entries. */
		stlblogical = &slave->logical[index.loc.stlb];
		for (i = 0; i < freed; i += 1)
			*stlblogical++ = GCMMU_STLB_ENTRY_VACANT;

#if USE_CACHED_SLAVE
		/* Flush CPU cache. */
		gc_flush_region(slave->physical, slave->logical,
				index.loc.stlb * sizeof(unsigned int),
				freed * sizeof(unsigned int));
#else
		mb();
#endif

		/* Advance. */
		slave += 1;
		index.absolute += freed;
		count -= freed;
	}

	/*
	 * Delete page cache for the arena.
	 */

	release_physical_pages(allocated);

	/*
	 * Find point of insertion and free the arena.
	 */

	GCDBG(GCZONE_MAPPING,
		"looking for the point of insertion.\n");

	list_for_each(nexthead, &gcmmucontext->vacant) {
		nextvacant = list_entry(nexthead, struct gcmmuarena, link);
		if (nextvacant->start.absolute >= allocated->end.absolute) {
			GCDBG(GCZONE_MAPPING, "  point of insertion found.\n");
			break;
		}
	}

	/* Get the previous vacant entry. */
	prevhead = nexthead->prev;

	/* Merge the area back into vacant list. */
	if (siblings(&gcmmucontext->vacant, prevhead, allochead)) {
		if (siblings(&gcmmucontext->vacant, allochead, nexthead)) {
			prevvacant = list_entry(prevhead, struct gcmmuarena,
						link);

			GCDBG(GCZONE_MAPPING, "merging three arenas:\n");

			GCDUMPARENA(GCZONE_ARENA, "previous arena", prevvacant);
			GCDUMPARENA(GCZONE_ARENA, "allocated arena", allocated);
			GCDUMPARENA(GCZONE_ARENA, "next arena", nextvacant);

			/* Merge all three arenas. */
			prevvacant->count += allocated->count;
			prevvacant->count += nextvacant->count;
			prevvacant->end.absolute = nextvacant->end.absolute;

			/* Free the merged arenas. */
			GCLOCK(&gcmmu->lock);
			list_move(allochead, &gcmmu->vacarena);
			list_move(nexthead, &gcmmu->vacarena);
			GCUNLOCK(&gcmmu->lock);
		} else {
			prevvacant = list_entry(prevhead, struct gcmmuarena,
						link);

			GCDBG(GCZONE_MAPPING, "merging with the previous:\n");

			GCDUMPARENA(GCZONE_ARENA, "previous arena", prevvacant);
			GCDUMPARENA(GCZONE_ARENA, "allocated arena", allocated);

			/* Merge with the previous. */
			prevvacant->count += allocated->count;
			prevvacant->end.absolute = allocated->end.absolute;

			/* Free the merged arena. */
			GCLOCK(&gcmmu->lock);
			list_move(allochead, &gcmmu->vacarena);
			GCUNLOCK(&gcmmu->lock);
		}
	} else if (siblings(&gcmmucontext->vacant, allochead, nexthead)) {
		GCDBG(GCZONE_MAPPING, "merged with the next:\n");

		GCDUMPARENA(GCZONE_ARENA, "allocated arena", allocated);
		GCDUMPARENA(GCZONE_ARENA, "next arena", nextvacant);

		/* Merge with the next arena. */
		nextvacant->start.absolute = allocated->start.absolute;
		nextvacant->count += allocated->count;

		/* Free the merged arena. */
		GCLOCK(&gcmmu->lock);
		list_move(allochead, &gcmmu->vacarena);
		GCUNLOCK(&gcmmu->lock);
	} else {
		GCDBG(GCZONE_MAPPING,
		      "nothing to merge with, inserting in between:\n");
		GCDUMPARENA(GCZONE_ARENA, "allocated arena", allocated);

		/* Neighbor vacant arenas are not siblings, can't merge. */
		list_move(allochead, prevhead);
	}

	/* Invalidate the MMU. */
	gcmmucontext->dirty = true;

	/* Dump tables. */
	GCDUMPMMU(GCZONE_DUMPUNMAP, gcmmucontext);

exit:
	if (locked)
		GCUNLOCK(&gcmmucontext->lock);

	GCEXITARG(GCZONE_MAPPING, "gc%s = 0x%08X\n",
		(gcerror == GCERR_NONE) ? "result" : "error", gcerror);
	return gcerror;
}

enum gcerror gcmmu_flush(struct gccorecontext *gccorecontext,
			 struct gcmmucontext *gcmmucontext)
{
	enum gcerror gcerror = GCERR_NONE;
	struct gcmommuflush *flushlogical;
	unsigned int flushaddress;
	struct gcqueue *gcqueue;

	GCENTER(GCZONE_FLUSH);

	GCLOCK(&gcmmucontext->lock);

	if (gcmmucontext->dirty) {
		/* Allocate space for the flush. */
		gcerror = gcqueue_alloc(gccorecontext, gcmmucontext,
					sizeof(struct gcmommuflush),
					(void **) &flushlogical, &flushaddress);
		if (gcerror != GCERR_NONE)
			goto exit;

		/* Get a shortcut to the queue object. */
		gcqueue = &gccorecontext->gcqueue;

		/* Store flush information. */
		gcqueue->flushlogical = flushlogical;
		gcqueue->flushaddress = flushaddress;

		/* Validate the context. */
		gcmmucontext->dirty = false;
	}

exit:
	GCUNLOCK(&gcmmucontext->lock);

	GCEXITARG(GCZONE_FLUSH, "gc%s = 0x%08X\n",
		(gcerror == GCERR_NONE) ? "result" : "error", gcerror);
	return gcerror;
}

void gcmmu_flush_finalize(struct gccmdbuf *gccmdbuf,
			  struct gcmommuflush *flushlogical,
			  unsigned int flushaddress)
{
	int size;
	unsigned int datacount;

	GCENTER(GCZONE_FLUSH);

	/* Compute the size of the command buffer after the flush block */
	size = gccmdbuf->physical + gccmdbuf->size
		- flushaddress - sizeof(struct gcmommuflush);

	/* Compute the data count. */
	datacount = (size + 7) >> 3;

	/* Flush 2D PE cache. */
	flushlogical->peflush.flush_ldst = gcmoflush_flush_ldst;
	flushlogical->peflush.flush.reg = gcregflush_pe2D;

	/* Arm the FE-PE semaphore. */
	flushlogical->peflushsema.sema_ldst = gcmosema_sema_ldst;
	flushlogical->peflushsema.sema.reg  = gcregsema_fe_pe;

	/* Stall FE until PE is done flushing. */
	flushlogical->peflushstall.cmd.fld = gcfldstall;
	flushlogical->peflushstall.arg.fld = gcfldstall_fe_pe;

	/* LINK to the next slot to flush FE FIFO. */
	flushlogical->feflush.cmd.fld = gcfldlink4;
	flushlogical->feflush.address
		= flushaddress
		+ offsetof(struct gcmommuflush, mmuflush_ldst);

	/* Flush MMU cache. */
	flushlogical->mmuflush_ldst = gcmommuflush_mmuflush_ldst;
	flushlogical->mmuflush.reg = gcregmmu_flush;

	/* Arm the FE-PE semaphore. */
	flushlogical->mmuflushsema.sema_ldst = gcmosema_sema_ldst;
	flushlogical->mmuflushsema.sema.reg  = gcregsema_fe_pe;

	/* Stall FE until PE is done flushing. */
	flushlogical->mmuflushstall.cmd.fld = gcfldstall;
	flushlogical->mmuflushstall.arg.fld = gcfldstall_fe_pe;

	/* LINK to the next slot to flush FE FIFO. */
	flushlogical->link.cmd.fld.opcode = GCREG_COMMAND_OPCODE_LINK;
	flushlogical->link.cmd.fld.count = datacount;
	flushlogical->link.address = flushaddress + sizeof(struct gcmommuflush);

	GCEXIT(GCZONE_FLUSH);
}

enum gcerror gcmmu_fixup(struct list_head *fixuplist,
			 unsigned int *data)
{
	enum gcerror gcerror = GCERR_NONE;
	struct list_head *head;
	struct gcfixup *gcfixup;
	struct gcfixupentry *table;
	struct gcmmuarena *arena;
	unsigned int dataoffset;
	unsigned int i;

	GCENTER(GCZONE_FIXUP);

	/* Process fixups. */
	list_for_each(head, fixuplist) {
		gcfixup = list_entry(head, struct gcfixup, link);

		GCDBG(GCZONE_FIXUP, "%d fixup(s) @ 0x%08X\n",
			gcfixup->count, (unsigned int) gcfixup);

		/* Apply fixups. */
		table = gcfixup->fixup;
		for (i = 0; i < gcfixup->count; i += 1) {
			GCDBG(GCZONE_FIXUP, "#%d\n", i);
			GCDBG(GCZONE_FIXUP, "  buffer offset = 0x%08X\n",
				table->dataoffset * 4);
			GCDBG(GCZONE_FIXUP, "  surface offset = 0x%08X\n",
				table->surfoffset);

			dataoffset = table->dataoffset;

			arena = (struct gcmmuarena *) data[dataoffset];
			if (!virt_addr_valid(arena)) {
				GCERR("bad arena @ 0x%08X\n",
				      (unsigned int) arena);
				gcerror = GCERR_OODM;
				goto exit;
			}

			GCDBG(GCZONE_FIXUP, "  arena = 0x%08X\n",
				(unsigned int) arena);
			GCDBG(GCZONE_FIXUP, "  arena phys = 0x%08X\n",
				arena->address);
			GCDBG(GCZONE_FIXUP, "  arena size = %d\n",
				arena->size);

			data[dataoffset] = arena->address + table->surfoffset;

			GCDBG(GCZONE_FIXUP, "  surface @ 0x%08X\n",
				data[dataoffset]);

			table += 1;
		}
	}

exit:
	GCEXITARG(GCZONE_FIXUP, "gc%s = 0x%08X\n",
		(gcerror == GCERR_NONE) ? "result" : "error", gcerror);
	return gcerror;
}