xen_pv_ops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /******************************************************************************
  2. * arch/ia64/xen/xen_pv_ops.c
  3. *
  4. * Copyright (c) 2008 Isaku Yamahata <yamahata at valinux co jp>
  5. * VA Linux Systems Japan K.K.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/console.h>
  23. #include <linux/irq.h>
  24. #include <linux/kernel.h>
  25. #include <linux/pm.h>
  26. #include <linux/unistd.h>
  27. #include <asm/xen/hypervisor.h>
  28. #include <asm/xen/xencomm.h>
  29. #include <asm/xen/privop.h>
  30. #include "irq_xen.h"
  31. #include "time.h"
  32. /***************************************************************************
  33. * general info
  34. */
  35. static struct pv_info xen_info __initdata = {
  36. .kernel_rpl = 2, /* or 1: determin at runtime */
  37. .paravirt_enabled = 1,
  38. .name = "Xen/ia64",
  39. };
  40. #define IA64_RSC_PL_SHIFT 2
  41. #define IA64_RSC_PL_BIT_SIZE 2
  42. #define IA64_RSC_PL_MASK \
  43. (((1UL << IA64_RSC_PL_BIT_SIZE) - 1) << IA64_RSC_PL_SHIFT)
  44. static void __init
  45. xen_info_init(void)
  46. {
  47. /* Xenified Linux/ia64 may run on pl = 1 or 2.
  48. * determin at run time. */
  49. unsigned long rsc = ia64_getreg(_IA64_REG_AR_RSC);
  50. unsigned int rpl = (rsc & IA64_RSC_PL_MASK) >> IA64_RSC_PL_SHIFT;
  51. xen_info.kernel_rpl = rpl;
  52. }
  53. /***************************************************************************
  54. * pv_init_ops
  55. * initialization hooks.
  56. */
  57. static void
  58. xen_panic_hypercall(struct unw_frame_info *info, void *arg)
  59. {
  60. current->thread.ksp = (__u64)info->sw - 16;
  61. HYPERVISOR_shutdown(SHUTDOWN_crash);
  62. /* we're never actually going to get here... */
  63. }
  64. static int
  65. xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
  66. {
  67. unw_init_running(xen_panic_hypercall, NULL);
  68. /* we're never actually going to get here... */
  69. return NOTIFY_DONE;
  70. }
  71. static struct notifier_block xen_panic_block = {
  72. xen_panic_event, NULL, 0 /* try to go last */
  73. };
  74. static void xen_pm_power_off(void)
  75. {
  76. local_irq_disable();
  77. HYPERVISOR_shutdown(SHUTDOWN_poweroff);
  78. }
  79. static void __init
  80. xen_banner(void)
  81. {
  82. printk(KERN_INFO
  83. "Running on Xen! pl = %d start_info_pfn=0x%lx nr_pages=%ld "
  84. "flags=0x%x\n",
  85. xen_info.kernel_rpl,
  86. HYPERVISOR_shared_info->arch.start_info_pfn,
  87. xen_start_info->nr_pages, xen_start_info->flags);
  88. }
  89. static int __init
  90. xen_reserve_memory(struct rsvd_region *region)
  91. {
  92. region->start = (unsigned long)__va(
  93. (HYPERVISOR_shared_info->arch.start_info_pfn << PAGE_SHIFT));
  94. region->end = region->start + PAGE_SIZE;
  95. return 1;
  96. }
  97. static void __init
  98. xen_arch_setup_early(void)
  99. {
  100. struct shared_info *s;
  101. BUG_ON(!xen_pv_domain());
  102. s = HYPERVISOR_shared_info;
  103. xen_start_info = __va(s->arch.start_info_pfn << PAGE_SHIFT);
  104. /* Must be done before any hypercall. */
  105. xencomm_initialize();
  106. xen_setup_features();
  107. /* Register a call for panic conditions. */
  108. atomic_notifier_chain_register(&panic_notifier_list,
  109. &xen_panic_block);
  110. pm_power_off = xen_pm_power_off;
  111. xen_ia64_enable_opt_feature();
  112. }
  113. static void __init
  114. xen_arch_setup_console(char **cmdline_p)
  115. {
  116. add_preferred_console("xenboot", 0, NULL);
  117. add_preferred_console("tty", 0, NULL);
  118. /* use hvc_xen */
  119. add_preferred_console("hvc", 0, NULL);
  120. #if !defined(CONFIG_VT) || !defined(CONFIG_DUMMY_CONSOLE)
  121. conswitchp = NULL;
  122. #endif
  123. }
  124. static int __init
  125. xen_arch_setup_nomca(void)
  126. {
  127. return 1;
  128. }
  129. static void __init
  130. xen_post_smp_prepare_boot_cpu(void)
  131. {
  132. xen_setup_vcpu_info_placement();
  133. }
  134. static const struct pv_init_ops xen_init_ops __initconst = {
  135. .banner = xen_banner,
  136. .reserve_memory = xen_reserve_memory,
  137. .arch_setup_early = xen_arch_setup_early,
  138. .arch_setup_console = xen_arch_setup_console,
  139. .arch_setup_nomca = xen_arch_setup_nomca,
  140. .post_smp_prepare_boot_cpu = xen_post_smp_prepare_boot_cpu,
  141. };
  142. /***************************************************************************
  143. * pv_fsys_data
  144. * addresses for fsys
  145. */
  146. extern unsigned long xen_fsyscall_table[NR_syscalls];
  147. extern char xen_fsys_bubble_down[];
  148. struct pv_fsys_data xen_fsys_data __initdata = {
  149. .fsyscall_table = (unsigned long *)xen_fsyscall_table,
  150. .fsys_bubble_down = (void *)xen_fsys_bubble_down,
  151. };
  152. /***************************************************************************
  153. * pv_patchdata
  154. * patchdata addresses
  155. */
  156. #define DECLARE(name) \
  157. extern unsigned long __xen_start_gate_##name##_patchlist[]; \
  158. extern unsigned long __xen_end_gate_##name##_patchlist[]
  159. DECLARE(fsyscall);
  160. DECLARE(brl_fsys_bubble_down);
  161. DECLARE(vtop);
  162. DECLARE(mckinley_e9);
  163. extern unsigned long __xen_start_gate_section[];
  164. #define ASSIGN(name) \
  165. .start_##name##_patchlist = \
  166. (unsigned long)__xen_start_gate_##name##_patchlist, \
  167. .end_##name##_patchlist = \
  168. (unsigned long)__xen_end_gate_##name##_patchlist
  169. static struct pv_patchdata xen_patchdata __initdata = {
  170. ASSIGN(fsyscall),
  171. ASSIGN(brl_fsys_bubble_down),
  172. ASSIGN(vtop),
  173. ASSIGN(mckinley_e9),
  174. .gate_section = (void*)__xen_start_gate_section,
  175. };
  176. /***************************************************************************
  177. * pv_cpu_ops
  178. * intrinsics hooks.
  179. */
  180. static void
  181. xen_set_itm_with_offset(unsigned long val)
  182. {
  183. /* ia64_cpu_local_tick() calls this with interrupt enabled. */
  184. /* WARN_ON(!irqs_disabled()); */
  185. xen_set_itm(val - XEN_MAPPEDREGS->itc_offset);
  186. }
  187. static unsigned long
  188. xen_get_itm_with_offset(void)
  189. {
  190. /* unused at this moment */
  191. printk(KERN_DEBUG "%s is called.\n", __func__);
  192. WARN_ON(!irqs_disabled());
  193. return ia64_native_getreg(_IA64_REG_CR_ITM) +
  194. XEN_MAPPEDREGS->itc_offset;
  195. }
  196. /* ia64_set_itc() is only called by
  197. * cpu_init() with ia64_set_itc(0) and ia64_sync_itc().
  198. * So XEN_MAPPEDRESG->itc_offset cal be considered as almost constant.
  199. */
  200. static void
  201. xen_set_itc(unsigned long val)
  202. {
  203. unsigned long mitc;
  204. WARN_ON(!irqs_disabled());
  205. mitc = ia64_native_getreg(_IA64_REG_AR_ITC);
  206. XEN_MAPPEDREGS->itc_offset = val - mitc;
  207. XEN_MAPPEDREGS->itc_last = val;
  208. }
  209. static unsigned long
  210. xen_get_itc(void)
  211. {
  212. unsigned long res;
  213. unsigned long itc_offset;
  214. unsigned long itc_last;
  215. unsigned long ret_itc_last;
  216. itc_offset = XEN_MAPPEDREGS->itc_offset;
  217. do {
  218. itc_last = XEN_MAPPEDREGS->itc_last;
  219. res = ia64_native_getreg(_IA64_REG_AR_ITC);
  220. res += itc_offset;
  221. if (itc_last >= res)
  222. res = itc_last + 1;
  223. ret_itc_last = cmpxchg(&XEN_MAPPEDREGS->itc_last,
  224. itc_last, res);
  225. } while (unlikely(ret_itc_last != itc_last));
  226. return res;
  227. #if 0
  228. /* ia64_itc_udelay() calls ia64_get_itc() with interrupt enabled.
  229. Should it be paravirtualized instead? */
  230. WARN_ON(!irqs_disabled());
  231. itc_offset = XEN_MAPPEDREGS->itc_offset;
  232. itc_last = XEN_MAPPEDREGS->itc_last;
  233. res = ia64_native_getreg(_IA64_REG_AR_ITC);
  234. res += itc_offset;
  235. if (itc_last >= res)
  236. res = itc_last + 1;
  237. XEN_MAPPEDREGS->itc_last = res;
  238. return res;
  239. #endif
  240. }
  241. static void xen_setreg(int regnum, unsigned long val)
  242. {
  243. switch (regnum) {
  244. case _IA64_REG_AR_KR0 ... _IA64_REG_AR_KR7:
  245. xen_set_kr(regnum - _IA64_REG_AR_KR0, val);
  246. break;
  247. #ifdef CONFIG_IA32_SUPPORT
  248. case _IA64_REG_AR_EFLAG:
  249. xen_set_eflag(val);
  250. break;
  251. #endif
  252. case _IA64_REG_AR_ITC:
  253. xen_set_itc(val);
  254. break;
  255. case _IA64_REG_CR_TPR:
  256. xen_set_tpr(val);
  257. break;
  258. case _IA64_REG_CR_ITM:
  259. xen_set_itm_with_offset(val);
  260. break;
  261. case _IA64_REG_CR_EOI:
  262. xen_eoi(val);
  263. break;
  264. default:
  265. ia64_native_setreg_func(regnum, val);
  266. break;
  267. }
  268. }
  269. static unsigned long xen_getreg(int regnum)
  270. {
  271. unsigned long res;
  272. switch (regnum) {
  273. case _IA64_REG_PSR:
  274. res = xen_get_psr();
  275. break;
  276. #ifdef CONFIG_IA32_SUPPORT
  277. case _IA64_REG_AR_EFLAG:
  278. res = xen_get_eflag();
  279. break;
  280. #endif
  281. case _IA64_REG_AR_ITC:
  282. res = xen_get_itc();
  283. break;
  284. case _IA64_REG_CR_ITM:
  285. res = xen_get_itm_with_offset();
  286. break;
  287. case _IA64_REG_CR_IVR:
  288. res = xen_get_ivr();
  289. break;
  290. case _IA64_REG_CR_TPR:
  291. res = xen_get_tpr();
  292. break;
  293. default:
  294. res = ia64_native_getreg_func(regnum);
  295. break;
  296. }
  297. return res;
  298. }
  299. /* turning on interrupts is a bit more complicated.. write to the
  300. * memory-mapped virtual psr.i bit first (to avoid race condition),
  301. * then if any interrupts were pending, we have to execute a hyperprivop
  302. * to ensure the pending interrupt gets delivered; else we're done! */
  303. static void
  304. xen_ssm_i(void)
  305. {
  306. int old = xen_get_virtual_psr_i();
  307. xen_set_virtual_psr_i(1);
  308. barrier();
  309. if (!old && xen_get_virtual_pend())
  310. xen_hyper_ssm_i();
  311. }
  312. /* turning off interrupts can be paravirtualized simply by writing
  313. * to a memory-mapped virtual psr.i bit (implemented as a 16-bit bool) */
  314. static void
  315. xen_rsm_i(void)
  316. {
  317. xen_set_virtual_psr_i(0);
  318. barrier();
  319. }
  320. static unsigned long
  321. xen_get_psr_i(void)
  322. {
  323. return xen_get_virtual_psr_i() ? IA64_PSR_I : 0;
  324. }
  325. static void
  326. xen_intrin_local_irq_restore(unsigned long mask)
  327. {
  328. if (mask & IA64_PSR_I)
  329. xen_ssm_i();
  330. else
  331. xen_rsm_i();
  332. }
  333. static const struct pv_cpu_ops xen_cpu_ops __initconst = {
  334. .fc = xen_fc,
  335. .thash = xen_thash,
  336. .get_cpuid = xen_get_cpuid,
  337. .get_pmd = xen_get_pmd,
  338. .getreg = xen_getreg,
  339. .setreg = xen_setreg,
  340. .ptcga = xen_ptcga,
  341. .get_rr = xen_get_rr,
  342. .set_rr = xen_set_rr,
  343. .set_rr0_to_rr4 = xen_set_rr0_to_rr4,
  344. .ssm_i = xen_ssm_i,
  345. .rsm_i = xen_rsm_i,
  346. .get_psr_i = xen_get_psr_i,
  347. .intrin_local_irq_restore
  348. = xen_intrin_local_irq_restore,
  349. };
  350. /******************************************************************************
  351. * replacement of hand written assembly codes.
  352. */
  353. extern char xen_switch_to;
  354. extern char xen_leave_syscall;
  355. extern char xen_work_processed_syscall;
  356. extern char xen_leave_kernel;
  357. const struct pv_cpu_asm_switch xen_cpu_asm_switch = {
  358. .switch_to = (unsigned long)&xen_switch_to,
  359. .leave_syscall = (unsigned long)&xen_leave_syscall,
  360. .work_processed_syscall = (unsigned long)&xen_work_processed_syscall,
  361. .leave_kernel = (unsigned long)&xen_leave_kernel,
  362. };
  363. /***************************************************************************
  364. * pv_iosapic_ops
  365. * iosapic read/write hooks.
  366. */
  367. static void
  368. xen_pcat_compat_init(void)
  369. {
  370. /* nothing */
  371. }
  372. static struct irq_chip*
  373. xen_iosapic_get_irq_chip(unsigned long trigger)
  374. {
  375. return NULL;
  376. }
  377. static unsigned int
  378. xen_iosapic_read(char __iomem *iosapic, unsigned int reg)
  379. {
  380. struct physdev_apic apic_op;
  381. int ret;
  382. apic_op.apic_physbase = (unsigned long)iosapic -
  383. __IA64_UNCACHED_OFFSET;
  384. apic_op.reg = reg;
  385. ret = HYPERVISOR_physdev_op(PHYSDEVOP_apic_read, &apic_op);
  386. if (ret)
  387. return ret;
  388. return apic_op.value;
  389. }
  390. static void
  391. xen_iosapic_write(char __iomem *iosapic, unsigned int reg, u32 val)
  392. {
  393. struct physdev_apic apic_op;
  394. apic_op.apic_physbase = (unsigned long)iosapic -
  395. __IA64_UNCACHED_OFFSET;
  396. apic_op.reg = reg;
  397. apic_op.value = val;
  398. HYPERVISOR_physdev_op(PHYSDEVOP_apic_write, &apic_op);
  399. }
  400. static const struct pv_iosapic_ops xen_iosapic_ops __initconst = {
  401. .pcat_compat_init = xen_pcat_compat_init,
  402. .__get_irq_chip = xen_iosapic_get_irq_chip,
  403. .__read = xen_iosapic_read,
  404. .__write = xen_iosapic_write,
  405. };
  406. /***************************************************************************
  407. * pv_ops initialization
  408. */
  409. void __init
  410. xen_setup_pv_ops(void)
  411. {
  412. xen_info_init();
  413. pv_info = xen_info;
  414. pv_init_ops = xen_init_ops;
  415. pv_fsys_data = xen_fsys_data;
  416. pv_patchdata = xen_patchdata;
  417. pv_cpu_ops = xen_cpu_ops;
  418. pv_iosapic_ops = xen_iosapic_ops;
  419. pv_irq_ops = xen_irq_ops;
  420. pv_time_ops = xen_time_ops;
  421. paravirt_cpu_asm_init(&xen_cpu_asm_switch);
  422. }