aboutsummaryrefslogtreecommitdiff
path: root/arch/ia64/xen/xen_pv_ops.c
blob: 3e8d350fdf39bf6bcdfaf314dd0cd5c9b0a0c4a9 (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
/******************************************************************************
 * arch/ia64/xen/xen_pv_ops.c
 *
 * Copyright (c) 2008 Isaku Yamahata <yamahata at valinux co jp>
 *                    VA Linux Systems Japan K.K.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/console.h>
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/pm.h>
#include <linux/unistd.h>

#include <asm/xen/hypervisor.h>
#include <asm/xen/xencomm.h>
#include <asm/xen/privop.h>

#include "irq_xen.h"
#include "time.h"

/***************************************************************************
 * general info
 */
static struct pv_info xen_info __initdata = {
	.kernel_rpl = 2,	/* or 1: determin at runtime */
	.paravirt_enabled = 1,
	.name = "Xen/ia64",
};

#define IA64_RSC_PL_SHIFT	2
#define IA64_RSC_PL_BIT_SIZE	2
#define IA64_RSC_PL_MASK	\
	(((1UL << IA64_RSC_PL_BIT_SIZE) - 1) << IA64_RSC_PL_SHIFT)

static void __init
xen_info_init(void)
{
	/* Xenified Linux/ia64 may run on pl = 1 or 2.
	 * determin at run time. */
	unsigned long rsc = ia64_getreg(_IA64_REG_AR_RSC);
	unsigned int rpl = (rsc & IA64_RSC_PL_MASK) >> IA64_RSC_PL_SHIFT;
	xen_info.kernel_rpl = rpl;
}

/***************************************************************************
 * pv_init_ops
 * initialization hooks.
 */

static void
xen_panic_hypercall(struct unw_frame_info *info, void *arg)
{
	current->thread.ksp = (__u64)info->sw - 16;
	HYPERVISOR_shutdown(SHUTDOWN_crash);
	/* we're never actually going to get here... */
}

static int
xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
{
	unw_init_running(xen_panic_hypercall, NULL);
	/* we're never actually going to get here... */
	return NOTIFY_DONE;
}

static struct notifier_block xen_panic_block = {
	xen_panic_event, NULL, 0 /* try to go last */
};

static void xen_pm_power_off(void)
{
	local_irq_disable();
	HYPERVISOR_shutdown(SHUTDOWN_poweroff);
}

static void __init
xen_banner(void)
{
	printk(KERN_INFO
	       "Running on Xen! pl = %d start_info_pfn=0x%lx nr_pages=%ld "
	       "flags=0x%x\n",
	       xen_info.kernel_rpl,
	       HYPERVISOR_shared_info->arch.start_info_pfn,
	       xen_start_info->nr_pages, xen_start_info->flags);
}

static int __init
xen_reserve_memory(struct rsvd_region *region)
{
	region->start = (unsigned long)__va(
		(HYPERVISOR_shared_info->arch.start_info_pfn << PAGE_SHIFT));
	region->end   = region->start + PAGE_SIZE;
	return 1;
}

static void __init
xen_arch_setup_early(void)
{
	struct shared_info *s;
	BUG_ON(!xen_pv_domain());

	s = HYPERVISOR_shared_info;
	xen_start_info = __va(s->arch.start_info_pfn << PAGE_SHIFT);

	/* Must be done before any hypercall.  */
	xencomm_initialize();

	xen_setup_features();
	/* Register a call for panic conditions. */
	atomic_notifier_chain_register(&panic_notifier_list,
				       &xen_panic_block);
	pm_power_off = xen_pm_power_off;

	xen_ia64_enable_opt_feature();
}

static void __init
xen_arch_setup_console(char **cmdline_p)
{
	add_preferred_console("xenboot", 0, NULL);
	add_preferred_console("tty", 0, NULL);
	/* use hvc_xen */
	add_preferred_console("hvc", 0, NULL);

#if !defined(CONFIG_VT) || !defined(CONFIG_DUMMY_CONSOLE)
	conswitchp = NULL;
#endif
}

static int __init
xen_arch_setup_nomca(void)
{
	return 1;
}

static void __init
xen_post_smp_prepare_boot_cpu(void)
{
	xen_setup_vcpu_info_placement();
}

#ifdef ASM_SUPPORTED
static unsigned long __init_or_module
xen_patch_bundle(void *sbundle, void *ebundle, unsigned long type);
#endif
static void __init
xen_patch_branch(unsigned long tag, unsigned long type);

static const struct pv_init_ops xen_init_ops __initconst = {
	.banner = xen_banner,

	.reserve_memory = xen_reserve_memory,

	.arch_setup_early = xen_arch_setup_early,
	.arch_setup_console = xen_arch_setup_console,
	.arch_setup_nomca = xen_arch_setup_nomca,

	.post_smp_prepare_boot_cpu = xen_post_smp_prepare_boot_cpu,
#ifdef ASM_SUPPORTED
	.patch_bundle = xen_patch_bundle,
#endif
	.patch_branch = xen_patch_branch,
};

/***************************************************************************
 * pv_fsys_data
 * addresses for fsys
 */

extern unsigned long xen_fsyscall_table[NR_syscalls];
extern char xen_fsys_bubble_down[];
struct pv_fsys_data xen_fsys_data __initdata = {
	.fsyscall_table = (unsigned long *)xen_fsyscall_table,
	.fsys_bubble_down = (void *)xen_fsys_bubble_down,
};

/***************************************************************************
 * pv_patchdata
 * patchdata addresses
 */

#define DECLARE(name)							\
	extern unsigned long __xen_start_gate_##name##_patchlist[];	\
	extern unsigned long __xen_end_gate_##name##_patchlist[]

DECLARE(fsyscall);
DECLARE(brl_fsys_bubble_down);
DECLARE(vtop);
DECLARE(mckinley_e9);

extern unsigned long __xen_start_gate_section[];

#define ASSIGN(name)							\
	.start_##name##_patchlist =					\
		(unsigned long)__xen_start_gate_##name##_patchlist,	\
	.end_##name##_patchlist =					\
		(unsigned long)__xen_end_gate_##name##_patchlist

static struct pv_patchdata xen_patchdata __initdata = {
	ASSIGN(fsyscall),
	ASSIGN(brl_fsys_bubble_down),
	ASSIGN(vtop),
	ASSIGN(mckinley_e9),

	.gate_section = (void*)__xen_start_gate_section,
};

/***************************************************************************
 * pv_cpu_ops
 * intrinsics hooks.
 */

#ifndef ASM_SUPPORTED
static void
xen_set_itm_with_offset(unsigned long val)
{
	/* ia64_cpu_local_tick() calls this with interrupt enabled. */
	/* WARN_ON(!irqs_disabled()); */
	xen_set_itm(val - XEN_MAPPEDREGS->itc_offset);
}

static unsigned long
xen_get_itm_with_offset(void)
{
	/* unused at this moment */
	printk(KERN_DEBUG "%s is called.\n", __func__);

	WARN_ON(!irqs_disabled());
	return ia64_native_getreg(_IA64_REG_CR_ITM) +
		XEN_MAPPEDREGS->itc_offset;
}

/* ia64_set_itc() is only called by
 * cpu_init() with ia64_set_itc(0) and ia64_sync_itc().
 * So XEN_MAPPEDRESG->itc_offset cal be considered as almost constant.
 */
static void
xen_set_itc(unsigned long val)
{
	unsigned long mitc;

	WARN_ON(!irqs_disabled());
	mitc = ia64_native_getreg(_IA64_REG_AR_ITC);
	XEN_MAPPEDREGS->itc_offset = val - mitc;
	XEN_MAPPEDREGS->itc_last = val;
}

static unsigned long
xen_get_itc(void)
{
	unsigned long res;
	unsigned long itc_offset;
	unsigned long itc_last;
	unsigned long ret_itc_last;

	itc_offset = XEN_MAPPEDREGS->itc_offset;
	do {
		itc_last = XEN_MAPPEDREGS->itc_last;
		res = ia64_native_getreg(_IA64_REG_AR_ITC);
		res += itc_offset;
		if (itc_last >= res)
			res = itc_last + 1;
		ret_itc_last = cmpxchg(&XEN_MAPPEDREGS->itc_last,
				       itc_last, res);
	} while (unlikely(ret_itc_last != itc_last));
	return res;

#if 0
	/* ia64_itc_udelay() calls ia64_get_itc() with interrupt enabled.
	   Should it be paravirtualized instead? */
	WARN_ON(!irqs_disabled());
	itc_offset = XEN_MAPPEDREGS->itc_offset;
	itc_last = XEN_MAPPEDREGS->itc_last;
	res = ia64_native_getreg(_IA64_REG_AR_ITC);
	res += itc_offset;
	if (itc_last >= res)
		res = itc_last + 1;
	XEN_MAPPEDREGS->itc_last = res;
	return res;
#endif
}

static void xen_setreg(int regnum, unsigned long val)
{
	switch (regnum) {
	case _IA64_REG_AR_KR0 ... _IA64_REG_AR_KR7:
		xen_set_kr(regnum - _IA64_REG_AR_KR0, val);
		break;
	case _IA64_REG_AR_ITC:
		xen_set_itc(val);
		break;
	case _IA64_REG_CR_TPR:
		xen_set_tpr(val);
		break;
	case _IA64_REG_CR_ITM:
		xen_set_itm_with_offset(val);
		break;
	case _IA64_REG_CR_EOI:
		xen_eoi(val);
		break;
	default:
		ia64_native_setreg_func(regnum, val);
		break;
	}
}

static unsigned long xen_getreg(int regnum)
{
	unsigned long res;

	switch (regnum) {
	case _IA64_REG_PSR:
		res = xen_get_psr();
		break;
	case _IA64_REG_AR_ITC:
		res = xen_get_itc();
		break;
	case _IA64_REG_CR_ITM:
		res = xen_get_itm_with_offset();
		break;
	case _IA64_REG_CR_IVR:
		res = xen_get_ivr();
		break;
	case _IA64_REG_CR_TPR:
		res = xen_get_tpr();
		break;
	default:
		res = ia64_native_getreg_func(regnum);
		break;
	}
	return res;
}

/* turning on interrupts is a bit more complicated.. write to the
 * memory-mapped virtual psr.i bit first (to avoid race condition),
 * then if any interrupts were pending, we have to execute a hyperprivop
 * to ensure the pending interrupt gets delivered; else we're done! */
static void
xen_ssm_i(void)
{
	int old = xen_get_virtual_psr_i();
	xen_set_virtual_psr_i(1);
	barrier();
	if (!old && xen_get_virtual_pend())
		xen_hyper_ssm_i();
}

/* turning off interrupts can be paravirtualized simply by writing
 * to a memory-mapped virtual psr.i bit (implemented as a 16-bit bool) */
static void
xen_rsm_i(void)
{
	xen_set_virtual_psr_i(0);
	barrier();
}

static unsigned long
xen_get_psr_i(void)
{
	return xen_get_virtual_psr_i() ? IA64_PSR_I : 0;
}

static void
xen_intrin_local_irq_restore(unsigned long mask)
{
	if (mask & IA64_PSR_I)
		xen_ssm_i();
	else
		xen_rsm_i();
}
#else
#define __DEFINE_FUNC(name, code)					\
	extern const char xen_ ## name ## _direct_start[];		\
	extern const char xen_ ## name ## _direct_end[];		\
	asm (".align 32\n"						\
	     ".proc xen_" #name "\n"					\
	     "xen_" #name ":\n"						\
	     "xen_" #name "_direct_start:\n"				\
	     code							\
	     "xen_" #name "_direct_end:\n"				\
	     "br.cond.sptk.many b6\n"					\
	     ".endp xen_" #name "\n")

#define DEFINE_VOID_FUNC0(name, code)		\
	extern void				\
	xen_ ## name (void);			\
	__DEFINE_FUNC(name, code)

#define DEFINE_VOID_FUNC1(name, code)		\
	extern void				\
	xen_ ## name (unsigned long arg);	\
	__DEFINE_FUNC(name, code)

#define DEFINE_VOID_FUNC1_VOID(name, code)	\
	extern void				\
	xen_ ## name (void *arg);		\
	__DEFINE_FUNC(name, code)

#define DEFINE_VOID_FUNC2(name, code)		\
	extern void				\
	xen_ ## name (unsigned long arg0,	\
		      unsigned long arg1);	\
	__DEFINE_FUNC(name, code)

#define DEFINE_FUNC0(name, code)		\
	extern unsigned long			\
	xen_ ## name (void);			\
	__DEFINE_FUNC(name, code)

#define DEFINE_FUNC1(name, type, code)		\
	extern unsigned long			\
	xen_ ## name (type arg);		\
	__DEFINE_FUNC(name, code)

#define XEN_PSR_I_ADDR_ADDR     (XSI_BASE + XSI_PSR_I_ADDR_OFS)

/*
 * static void xen_set_itm_with_offset(unsigned long val)
 *        xen_set_itm(val - XEN_MAPPEDREGS->itc_offset);
 */
/* 2 bundles */
DEFINE_VOID_FUNC1(set_itm_with_offset,
		  "mov r2 = " __stringify(XSI_BASE) " + "
		  __stringify(XSI_ITC_OFFSET_OFS) "\n"
		  ";;\n"
		  "ld8 r3 = [r2]\n"
		  ";;\n"
		  "sub r8 = r8, r3\n"
		  "break " __stringify(HYPERPRIVOP_SET_ITM) "\n");

/*
 * static unsigned long xen_get_itm_with_offset(void)
 *    return ia64_native_getreg(_IA64_REG_CR_ITM) + XEN_MAPPEDREGS->itc_offset;
 */
/* 2 bundles */
DEFINE_FUNC0(get_itm_with_offset,
	     "mov r2 = " __stringify(XSI_BASE) " + "
	     __stringify(XSI_ITC_OFFSET_OFS) "\n"
	     ";;\n"
	     "ld8 r3 = [r2]\n"
	     "mov r8 = cr.itm\n"
	     ";;\n"
	     "add r8 = r8, r2\n");

/*
 * static void xen_set_itc(unsigned long val)
 *	unsigned long mitc;
 *
 *	WARN_ON(!irqs_disabled());
 *	mitc = ia64_native_getreg(_IA64_REG_AR_ITC);
 *	XEN_MAPPEDREGS->itc_offset = val - mitc;
 *	XEN_MAPPEDREGS->itc_last = val;
 */
/* 2 bundles */
DEFINE_VOID_FUNC1(set_itc,
		  "mov r2 = " __stringify(XSI_BASE) " + "
		  __stringify(XSI_ITC_LAST_OFS) "\n"
		  "mov r3 = ar.itc\n"
		  ";;\n"
		  "sub r3 = r8, r3\n"
		  "st8 [r2] = r8, "
		  __stringify(XSI_ITC_LAST_OFS) " - "
		  __stringify(XSI_ITC_OFFSET_OFS) "\n"
		  ";;\n"
		  "st8 [r2] = r3\n");

/*
 * static unsigned long xen_get_itc(void)
 *	unsigned long res;
 *	unsigned long itc_offset;
 *	unsigned long itc_last;
 *	unsigned long ret_itc_last;
 *
 *	itc_offset = XEN_MAPPEDREGS->itc_offset;
 *	do {
 *		itc_last = XEN_MAPPEDREGS->itc_last;
 *		res = ia64_native_getreg(_IA64_REG_AR_ITC);
 *		res += itc_offset;
 *		if (itc_last >= res)
 *			res = itc_last + 1;
 *		ret_itc_last = cmpxchg(&XEN_MAPPEDREGS->itc_last,
 *				       itc_last, res);
 *	} while (unlikely(ret_itc_last != itc_last));
 *	return res;
 */
/* 5 bundles */
DEFINE_FUNC0(get_itc,
	     "mov r2 = " __stringify(XSI_BASE) " + "
	     __stringify(XSI_ITC_OFFSET_OFS) "\n"
	     ";;\n"
	     "ld8 r9 = [r2], " __stringify(XSI_ITC_LAST_OFS) " - "
	     __stringify(XSI_ITC_OFFSET_OFS) "\n"
					/* r9 = itc_offset */
					/* r2 = XSI_ITC_OFFSET */
	     "888:\n"
	     "mov r8 = ar.itc\n"	/* res = ar.itc */
	     ";;\n"
	     "ld8 r3 = [r2]\n"		/* r3 = itc_last */
	     "add r8 = r8, r9\n"	/* res = ar.itc + itc_offset */
	     ";;\n"
	     "cmp.gtu p6, p0 = r3, r8\n"
	     ";;\n"
	     "(p6) add r8 = 1, r3\n"	/* if (itc_last > res) itc_last + 1 */
	     ";;\n"
	     "mov ar.ccv = r8\n"
	     ";;\n"
	     "cmpxchg8.acq r10 = [r2], r8, ar.ccv\n"
	     ";;\n"
	     "cmp.ne p6, p0 = r10, r3\n"
	     "(p6) hint @pause\n"
	     "(p6) br.cond.spnt 888b\n");

DEFINE_VOID_FUNC1_VOID(fc,
		       "break " __stringify(HYPERPRIVOP_FC) "\n");

/*
 * psr_i_addr_addr = XEN_PSR_I_ADDR_ADDR
 * masked_addr = *psr_i_addr_addr
 * pending_intr_addr = masked_addr - 1
 * if (val & IA64_PSR_I) {
 *   masked = *masked_addr
 *   *masked_addr = 0:xen_set_virtual_psr_i(1)
 *   compiler barrier
 *   if (masked) {
 *      uint8_t pending = *pending_intr_addr;
 *      if (pending)
 *              XEN_HYPER_SSM_I
 *   }
 * } else {
 *   *masked_addr = 1:xen_set_virtual_psr_i(0)
 * }
 */
/* 6 bundles */
DEFINE_VOID_FUNC1(intrin_local_irq_restore,
		  /* r8 = input value: 0 or IA64_PSR_I
		   * p6 =  (flags & IA64_PSR_I)
		   *    = if clause
		   * p7 = !(flags & IA64_PSR_I)
		   *    = else clause
		   */
		  "cmp.ne p6, p7 = r8, r0\n"
		  "mov r9 = " __stringify(XEN_PSR_I_ADDR_ADDR) "\n"
		  ";;\n"
		  /* r9 = XEN_PSR_I_ADDR */
		  "ld8 r9 = [r9]\n"
		  ";;\n"

		  /* r10 = masked previous value */
		  "(p6)	ld1.acq r10 = [r9]\n"
		  ";;\n"

		  /* p8 = !masked interrupt masked previously? */
		  "(p6)	cmp.ne.unc p8, p0 = r10, r0\n"

		  /* p7 = else clause */
		  "(p7)	mov r11 = 1\n"
		  ";;\n"
		  /* masked = 1 */
		  "(p7)	st1.rel [r9] = r11\n"

		  /* p6 = if clause */
		  /* masked = 0
		   * r9 = masked_addr - 1
		   *    = pending_intr_addr
		   */
		  "(p8)	st1.rel [r9] = r0, -1\n"
		  ";;\n"
		  /* r8 = pending_intr */
		  "(p8)	ld1.acq r11 = [r9]\n"
		  ";;\n"
		  /* p9 = interrupt pending? */
		  "(p8)	cmp.ne.unc p9, p10 = r11, r0\n"
		  ";;\n"
		  "(p10) mf\n"
		  /* issue hypercall to trigger interrupt */
		  "(p9)	break " __stringify(HYPERPRIVOP_SSM_I) "\n");

DEFINE_VOID_FUNC2(ptcga,
		  "break " __stringify(HYPERPRIVOP_PTC_GA) "\n");
DEFINE_VOID_FUNC2(set_rr,
		  "break " __stringify(HYPERPRIVOP_SET_RR) "\n");

/*
 * tmp = XEN_MAPPEDREGS->interrupt_mask_addr = XEN_PSR_I_ADDR_ADDR;
 * tmp = *tmp
 * tmp = *tmp;
 * psr_i = tmp? 0: IA64_PSR_I;
 */
/* 4 bundles */
DEFINE_FUNC0(get_psr_i,
	     "mov r9 = " __stringify(XEN_PSR_I_ADDR_ADDR) "\n"
	     ";;\n"
	     "ld8 r9 = [r9]\n"			/* r9 = XEN_PSR_I_ADDR */
	     "mov r8 = 0\n"			/* psr_i = 0 */
	     ";;\n"
	     "ld1.acq r9 = [r9]\n"		/* r9 = XEN_PSR_I */
	     ";;\n"
	     "cmp.eq.unc p6, p0 = r9, r0\n"	/* p6 = (XEN_PSR_I != 0) */
	     ";;\n"
	     "(p6) mov r8 = " __stringify(1 << IA64_PSR_I_BIT) "\n");

DEFINE_FUNC1(thash, unsigned long,
	     "break " __stringify(HYPERPRIVOP_THASH) "\n");
DEFINE_FUNC1(get_cpuid, int,
	     "break " __stringify(HYPERPRIVOP_GET_CPUID) "\n");
DEFINE_FUNC1(get_pmd, int,
	     "break " __stringify(HYPERPRIVOP_GET_PMD) "\n");
DEFINE_FUNC1(get_rr, unsigned long,
	     "break " __stringify(HYPERPRIVOP_GET_RR) "\n");

/*
 * void xen_privop_ssm_i(void)
 *
 * int masked = !xen_get_virtual_psr_i();
 *	// masked = *(*XEN_MAPPEDREGS->interrupt_mask_addr)
 * xen_set_virtual_psr_i(1)
 *	// *(*XEN_MAPPEDREGS->interrupt_mask_addr) = 0
 * // compiler barrier
 * if (masked) {
 *	uint8_t* pend_int_addr =
 *		(uint8_t*)(*XEN_MAPPEDREGS->interrupt_mask_addr) - 1;
 *	uint8_t pending = *pend_int_addr;
 *	if (pending)
 *		XEN_HYPER_SSM_I
 * }
 */
/* 4 bundles */
DEFINE_VOID_FUNC0(ssm_i,
		  "mov r8 = " __stringify(XEN_PSR_I_ADDR_ADDR) "\n"
		  ";;\n"
		  "ld8 r8 = [r8]\n"		/* r8 = XEN_PSR_I_ADDR */
		  ";;\n"
		  "ld1.acq r9 = [r8]\n"		/* r9 = XEN_PSR_I */
		  ";;\n"
		  "st1.rel [r8] = r0, -1\n"	/* psr_i = 0. enable interrupt
						 * r8 = XEN_PSR_I_ADDR - 1
						 *    = pend_int_addr
						 */
		  "cmp.eq.unc p0, p6 = r9, r0\n"/* p6 = !XEN_PSR_I
						 * previously interrupt
						 * masked?
						 */
		  ";;\n"
		  "(p6) ld1.acq r8 = [r8]\n"	/* r8 = xen_pend_int */
		  ";;\n"
		  "(p6) cmp.eq.unc p6, p7 = r8, r0\n"	/*interrupt pending?*/
		  ";;\n"
		  /* issue hypercall to get interrupt */
		  "(p7) break " __stringify(HYPERPRIVOP_SSM_I) "\n"
		  ";;\n");

/*
 * psr_i_addr_addr = XEN_MAPPEDREGS->interrupt_mask_addr
 *		   = XEN_PSR_I_ADDR_ADDR;
 * psr_i_addr = *psr_i_addr_addr;
 * *psr_i_addr = 1;
 */
/* 2 bundles */
DEFINE_VOID_FUNC0(rsm_i,
		  "mov r8 = " __stringify(XEN_PSR_I_ADDR_ADDR) "\n"
						/* r8 = XEN_PSR_I_ADDR */
		  "mov r9 = 1\n"
		  ";;\n"
		  "ld8 r8 = [r8]\n"		/* r8 = XEN_PSR_I */
		  ";;\n"
		  "st1.rel [r8] = r9\n");	/* XEN_PSR_I = 1 */

extern void
xen_set_rr0_to_rr4(unsigned long val0, unsigned long val1,
		   unsigned long val2, unsigned long val3,
		   unsigned long val4);
__DEFINE_FUNC(set_rr0_to_rr4,
	      "break " __stringify(HYPERPRIVOP_SET_RR0_TO_RR4) "\n");


extern unsigned long xen_getreg(int regnum);
#define __DEFINE_GET_REG(id, privop)					\
	"mov r2 = " __stringify(_IA64_REG_ ## id) "\n"			\
	";;\n"								\
	"cmp.eq p6, p0 = r2, r8\n"					\
	";;\n"								\
	"(p6) break " __stringify(HYPERPRIVOP_GET_ ## privop) "\n"	\
	"(p6) br.cond.sptk.many b6\n"					\
	";;\n"

__DEFINE_FUNC(getreg,
	      __DEFINE_GET_REG(PSR, PSR)

	      /* get_itc */
	      "mov r2 = " __stringify(_IA64_REG_AR_ITC) "\n"
	      ";;\n"
	      "cmp.eq p6, p0 = r2, r8\n"
	      ";;\n"
	      "(p6) br.cond.spnt xen_get_itc\n"
	      ";;\n"

	      /* get itm */
	      "mov r2 = " __stringify(_IA64_REG_CR_ITM) "\n"
	      ";;\n"
	      "cmp.eq p6, p0 = r2, r8\n"
	      ";;\n"
	      "(p6) br.cond.spnt xen_get_itm_with_offset\n"
	      ";;\n"

	      __DEFINE_GET_REG(CR_IVR, IVR)
	      __DEFINE_GET_REG(CR_TPR, TPR)

	      /* fall back */
	      "movl r2 = ia64_native_getreg_func\n"
	      ";;\n"
	      "mov b7 = r2\n"
	      ";;\n"
	      "br.cond.sptk.many b7\n");

extern void xen_setreg(int regnum, unsigned long val);
#define __DEFINE_SET_REG(id, privop)					\
	"mov r2 = " __stringify(_IA64_REG_ ## id) "\n"			\
	";;\n"								\
	"cmp.eq p6, p0 = r2, r9\n"					\
	";;\n"								\
	"(p6) break " __stringify(HYPERPRIVOP_ ## privop) "\n"		\
	"(p6) br.cond.sptk.many b6\n"					\
	";;\n"

__DEFINE_FUNC(setreg,
	      /* kr0 .. kr 7*/
	      /*
	       * if (_IA64_REG_AR_KR0 <= regnum &&
	       *     regnum <= _IA64_REG_AR_KR7) {
	       *     register __index asm ("r8") = regnum - _IA64_REG_AR_KR0
	       *     register __val asm ("r9") = val
	       *    "break HYPERPRIVOP_SET_KR"
	       * }
	       */
	      "mov r17 = r9\n"
	      "mov r2 = " __stringify(_IA64_REG_AR_KR0) "\n"
	      ";;\n"
	      "cmp.ge p6, p0 = r9, r2\n"
	      "sub r17 = r17, r2\n"
	      ";;\n"
	      "(p6) cmp.ge.unc p7, p0 = "
	      __stringify(_IA64_REG_AR_KR7) " - " __stringify(_IA64_REG_AR_KR0)
	      ", r17\n"
	      ";;\n"
	      "(p7) mov r9 = r8\n"
	      ";;\n"
	      "(p7) mov r8 = r17\n"
	      "(p7) break " __stringify(HYPERPRIVOP_SET_KR) "\n"

	      /* set itm */
	      "mov r2 = " __stringify(_IA64_REG_CR_ITM) "\n"
	      ";;\n"
	      "cmp.eq p6, p0 = r2, r8\n"
	      ";;\n"
	      "(p6) br.cond.spnt xen_set_itm_with_offset\n"

	      /* set itc */
	      "mov r2 = " __stringify(_IA64_REG_AR_ITC) "\n"
	      ";;\n"
	      "cmp.eq p6, p0 = r2, r8\n"
	      ";;\n"
	      "(p6) br.cond.spnt xen_set_itc\n"

	      __DEFINE_SET_REG(CR_TPR, SET_TPR)
	      __DEFINE_SET_REG(CR_EOI, EOI)

	      /* fall back */
	      "movl r2 = ia64_native_setreg_func\n"
	      ";;\n"
	      "mov b7 = r2\n"
	      ";;\n"
	      "br.cond.sptk.many b7\n");
#endif

static const struct pv_cpu_ops xen_cpu_ops __initconst = {
	.fc		= xen_fc,
	.thash		= xen_thash,
	.get_cpuid	= xen_get_cpuid,
	.get_pmd	= xen_get_pmd,
	.getreg		= xen_getreg,
	.setreg		= xen_setreg,
	.ptcga		= xen_ptcga,
	.get_rr		= xen_get_rr,
	.set_rr		= xen_set_rr,
	.set_rr0_to_rr4	= xen_set_rr0_to_rr4,
	.ssm_i		= xen_ssm_i,
	.rsm_i		= xen_rsm_i,
	.get_psr_i	= xen_get_psr_i,
	.intrin_local_irq_restore
			= xen_intrin_local_irq_restore,
};

/******************************************************************************
 * replacement of hand written assembly codes.
 */

extern char xen_switch_to;
extern char xen_leave_syscall;
extern char xen_work_processed_syscall;
extern char xen_leave_kernel;

const struct pv_cpu_asm_switch xen_cpu_asm_switch = {
	.switch_to		= (unsigned long)&xen_switch_to,
	.leave_syscall		= (unsigned long)&xen_leave_syscall,
	.work_processed_syscall	= (unsigned long)&xen_work_processed_syscall,
	.leave_kernel		= (unsigned long)&xen_leave_kernel,
};

/***************************************************************************
 * pv_iosapic_ops
 * iosapic read/write hooks.
 */
static void
xen_pcat_compat_init(void)
{
	/* nothing */
}

static struct irq_chip*
xen_iosapic_get_irq_chip(unsigned long trigger)
{
	return NULL;
}

static unsigned int
xen_iosapic_read(char __iomem *iosapic, unsigned int reg)
{
	struct physdev_apic apic_op;
	int ret;

	apic_op.apic_physbase = (unsigned long)iosapic -
					__IA64_UNCACHED_OFFSET;
	apic_op.reg = reg;
	ret = HYPERVISOR_physdev_op(PHYSDEVOP_apic_read, &apic_op);
	if (ret)
		return ret;
	return apic_op.value;
}

static void
xen_iosapic_write(char __iomem *iosapic, unsigned int reg, u32 val)
{
	struct physdev_apic apic_op;

	apic_op.apic_physbase = (unsigned long)iosapic -
					__IA64_UNCACHED_OFFSET;
	apic_op.reg = reg;
	apic_op.value = val;
	HYPERVISOR_physdev_op(PHYSDEVOP_apic_write, &apic_op);
}

static struct pv_iosapic_ops xen_iosapic_ops __initdata = {
	.pcat_compat_init = xen_pcat_compat_init,
	.__get_irq_chip = xen_iosapic_get_irq_chip,

	.__read = xen_iosapic_read,
	.__write = xen_iosapic_write,
};

/***************************************************************************
 * pv_ops initialization
 */

void __init
xen_setup_pv_ops(void)
{
	xen_info_init();
	pv_info = xen_info;
	pv_init_ops = xen_init_ops;
	pv_fsys_data = xen_fsys_data;
	pv_patchdata = xen_patchdata;
	pv_cpu_ops = xen_cpu_ops;
	pv_iosapic_ops = xen_iosapic_ops;
	pv_irq_ops = xen_irq_ops;
	pv_time_ops = xen_time_ops;

	paravirt_cpu_asm_init(&xen_cpu_asm_switch);
}

#ifdef ASM_SUPPORTED
/***************************************************************************
 * binary pacthing
 * pv_init_ops.patch_bundle
 */

#define DEFINE_FUNC_GETREG(name, privop)				\
	DEFINE_FUNC0(get_ ## name,					\
		     "break "__stringify(HYPERPRIVOP_GET_ ## privop) "\n")

DEFINE_FUNC_GETREG(psr, PSR);
DEFINE_FUNC_GETREG(eflag, EFLAG);
DEFINE_FUNC_GETREG(ivr, IVR);
DEFINE_FUNC_GETREG(tpr, TPR);

#define DEFINE_FUNC_SET_KR(n)						\
	DEFINE_VOID_FUNC0(set_kr ## n,					\
			  ";;\n"					\
			  "mov r9 = r8\n"				\
			  "mov r8 = " #n "\n"				\
			  "break " __stringify(HYPERPRIVOP_SET_KR) "\n")

DEFINE_FUNC_SET_KR(0);
DEFINE_FUNC_SET_KR(1);
DEFINE_FUNC_SET_KR(2);
DEFINE_FUNC_SET_KR(3);
DEFINE_FUNC_SET_KR(4);
DEFINE_FUNC_SET_KR(5);
DEFINE_FUNC_SET_KR(6);
DEFINE_FUNC_SET_KR(7);

#define __DEFINE_FUNC_SETREG(name, privop)				\
	DEFINE_VOID_FUNC0(name,						\
			  "break "__stringify(HYPERPRIVOP_ ## privop) "\n")

#define DEFINE_FUNC_SETREG(name, privop)			\
	__DEFINE_FUNC_SETREG(set_ ## name, SET_ ## privop)

DEFINE_FUNC_SETREG(eflag, EFLAG);
DEFINE_FUNC_SETREG(tpr, TPR);
__DEFINE_FUNC_SETREG(eoi, EOI);

extern const char xen_check_events[];
extern const char __xen_intrin_local_irq_restore_direct_start[];
extern const char __xen_intrin_local_irq_restore_direct_end[];
extern const unsigned long __xen_intrin_local_irq_restore_direct_reloc;

asm (
	".align 32\n"
	".proc xen_check_events\n"
	"xen_check_events:\n"
	/* masked = 0
	 * r9 = masked_addr - 1
	 *    = pending_intr_addr
	 */
	"st1.rel [r9] = r0, -1\n"
	";;\n"
	/* r8 = pending_intr */
	"ld1.acq r11 = [r9]\n"
	";;\n"
	/* p9 = interrupt pending? */
	"cmp.ne p9, p10 = r11, r0\n"
	";;\n"
	"(p10) mf\n"
	/* issue hypercall to trigger interrupt */
	"(p9) break " __stringify(HYPERPRIVOP_SSM_I) "\n"
	"br.cond.sptk.many b6\n"
	".endp xen_check_events\n"
	"\n"
	".align 32\n"
	".proc __xen_intrin_local_irq_restore_direct\n"
	"__xen_intrin_local_irq_restore_direct:\n"
	"__xen_intrin_local_irq_restore_direct_start:\n"
	"1:\n"
	"{\n"
	"cmp.ne p6, p7 = r8, r0\n"
	"mov r17 = ip\n" /* get ip to calc return address */
	"mov r9 = "__stringify(XEN_PSR_I_ADDR_ADDR) "\n"
	";;\n"
	"}\n"
	"{\n"
	/* r9 = XEN_PSR_I_ADDR */
	"ld8 r9 = [r9]\n"
	";;\n"
	/* r10 = masked previous value */
	"(p6) ld1.acq r10 = [r9]\n"
	"adds r17 =  1f - 1b, r17\n" /* calculate return address */
	";;\n"
	"}\n"
	"{\n"
	/* p8 = !masked interrupt masked previously? */
	"(p6) cmp.ne.unc p8, p0 = r10, r0\n"
	"\n"
	/* p7 = else clause */
	"(p7) mov r11 = 1\n"
	";;\n"
	"(p8) mov b6 = r17\n" /* set return address */
	"}\n"
	"{\n"
	/* masked = 1 */
	"(p7) st1.rel [r9] = r11\n"
	"\n"
	"[99:]\n"
	"(p8) brl.cond.dptk.few xen_check_events\n"
	"}\n"
	/* pv calling stub is 5 bundles. fill nop to adjust return address */
	"{\n"
	"nop 0\n"
	"nop 0\n"
	"nop 0\n"
	"}\n"
	"1:\n"
	"__xen_intrin_local_irq_restore_direct_end:\n"
	".endp __xen_intrin_local_irq_restore_direct\n"
	"\n"
	".align 8\n"
	"__xen_intrin_local_irq_restore_direct_reloc:\n"
	"data8 99b\n"
);

static struct paravirt_patch_bundle_elem xen_patch_bundle_elems[]
__initdata_or_module =
{
#define XEN_PATCH_BUNDLE_ELEM(name, type)		\
	{						\
		(void*)xen_ ## name ## _direct_start,	\
		(void*)xen_ ## name ## _direct_end,	\
		PARAVIRT_PATCH_TYPE_ ## type,		\
	}

	XEN_PATCH_BUNDLE_ELEM(fc, FC),
	XEN_PATCH_BUNDLE_ELEM(thash, THASH),
	XEN_PATCH_BUNDLE_ELEM(get_cpuid, GET_CPUID),
	XEN_PATCH_BUNDLE_ELEM(get_pmd, GET_PMD),
	XEN_PATCH_BUNDLE_ELEM(ptcga, PTCGA),
	XEN_PATCH_BUNDLE_ELEM(get_rr, GET_RR),
	XEN_PATCH_BUNDLE_ELEM(set_rr, SET_RR),
	XEN_PATCH_BUNDLE_ELEM(set_rr0_to_rr4, SET_RR0_TO_RR4),
	XEN_PATCH_BUNDLE_ELEM(ssm_i, SSM_I),
	XEN_PATCH_BUNDLE_ELEM(rsm_i, RSM_I),
	XEN_PATCH_BUNDLE_ELEM(get_psr_i, GET_PSR_I),
	{
		(void*)__xen_intrin_local_irq_restore_direct_start,
		(void*)__xen_intrin_local_irq_restore_direct_end,
		PARAVIRT_PATCH_TYPE_INTRIN_LOCAL_IRQ_RESTORE,
	},

#define XEN_PATCH_BUNDLE_ELEM_GETREG(name, reg)			\
	{							\
		xen_get_ ## name ## _direct_start,		\
		xen_get_ ## name ## _direct_end,		\
		PARAVIRT_PATCH_TYPE_GETREG + _IA64_REG_ ## reg, \
	}

	XEN_PATCH_BUNDLE_ELEM_GETREG(psr, PSR),
	XEN_PATCH_BUNDLE_ELEM_GETREG(eflag, AR_EFLAG),

	XEN_PATCH_BUNDLE_ELEM_GETREG(ivr, CR_IVR),
	XEN_PATCH_BUNDLE_ELEM_GETREG(tpr, CR_TPR),

	XEN_PATCH_BUNDLE_ELEM_GETREG(itc, AR_ITC),
	XEN_PATCH_BUNDLE_ELEM_GETREG(itm_with_offset, CR_ITM),


#define __XEN_PATCH_BUNDLE_ELEM_SETREG(name, reg)		\
	{							\
		xen_ ## name ## _direct_start,			\
		xen_ ## name ## _direct_end,			\
		PARAVIRT_PATCH_TYPE_SETREG + _IA64_REG_ ## reg, \
	}

#define XEN_PATCH_BUNDLE_ELEM_SETREG(name, reg)			\
	__XEN_PATCH_BUNDLE_ELEM_SETREG(set_ ## name, reg)

	XEN_PATCH_BUNDLE_ELEM_SETREG(kr0, AR_KR0),
	XEN_PATCH_BUNDLE_ELEM_SETREG(kr1, AR_KR1),
	XEN_PATCH_BUNDLE_ELEM_SETREG(kr2, AR_KR2),
	XEN_PATCH_BUNDLE_ELEM_SETREG(kr3, AR_KR3),
	XEN_PATCH_BUNDLE_ELEM_SETREG(kr4, AR_KR4),
	XEN_PATCH_BUNDLE_ELEM_SETREG(kr5, AR_KR5),
	XEN_PATCH_BUNDLE_ELEM_SETREG(kr6, AR_KR6),
	XEN_PATCH_BUNDLE_ELEM_SETREG(kr7, AR_KR7),

	XEN_PATCH_BUNDLE_ELEM_SETREG(eflag, AR_EFLAG),
	XEN_PATCH_BUNDLE_ELEM_SETREG(tpr, CR_TPR),
	__XEN_PATCH_BUNDLE_ELEM_SETREG(eoi, CR_EOI),

	XEN_PATCH_BUNDLE_ELEM_SETREG(itc, AR_ITC),
	XEN_PATCH_BUNDLE_ELEM_SETREG(itm_with_offset, CR_ITM),
};

static unsigned long __init_or_module
xen_patch_bundle(void *sbundle, void *ebundle, unsigned long type)
{
	const unsigned long nelems = sizeof(xen_patch_bundle_elems) /
		sizeof(xen_patch_bundle_elems[0]);
	unsigned long used;
	const struct paravirt_patch_bundle_elem *found;

	used = __paravirt_patch_apply_bundle(sbundle, ebundle, type,
					     xen_patch_bundle_elems, nelems,
					     &found);

	if (found == NULL)
		/* fallback */
		return ia64_native_patch_bundle(sbundle, ebundle, type);
	if (used == 0)
		return used;

	/* relocation */
	switch (type) {
	case PARAVIRT_PATCH_TYPE_INTRIN_LOCAL_IRQ_RESTORE: {
		unsigned long reloc =
			__xen_intrin_local_irq_restore_direct_reloc;
		unsigned long reloc_offset = reloc - (unsigned long)
			__xen_intrin_local_irq_restore_direct_start;
		unsigned long tag = (unsigned long)sbundle + reloc_offset;
		paravirt_patch_reloc_brl(tag, xen_check_events);
		break;
	}
	default:
		/* nothing */
		break;
	}
	return used;
}
#endif /* ASM_SUPPOTED */

const struct paravirt_patch_branch_target xen_branch_target[]
__initconst = {
#define PARAVIRT_BR_TARGET(name, type)			\
	{						\
		&xen_ ## name,				\
		PARAVIRT_PATCH_TYPE_BR_ ## type,	\
	}
	PARAVIRT_BR_TARGET(switch_to, SWITCH_TO),
	PARAVIRT_BR_TARGET(leave_syscall, LEAVE_SYSCALL),
	PARAVIRT_BR_TARGET(work_processed_syscall, WORK_PROCESSED_SYSCALL),
	PARAVIRT_BR_TARGET(leave_kernel, LEAVE_KERNEL),
};

static void __init
xen_patch_branch(unsigned long tag, unsigned long type)
{
	__paravirt_patch_apply_branch(tag, type, xen_branch_target,
					ARRAY_SIZE(xen_branch_target));
}