kvm_fw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * PAL/SAL call delegation
  3. *
  4. * Copyright (c) 2004 Li Susie <susie.li@intel.com>
  5. * Copyright (c) 2005 Yu Ke <ke.yu@intel.com>
  6. * Copyright (c) 2007 Xiantao Zhang <xiantao.zhang@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  19. * Place - Suite 330, Boston, MA 02111-1307 USA.
  20. */
  21. #include <linux/kvm_host.h>
  22. #include <linux/smp.h>
  23. #include "vti.h"
  24. #include "misc.h"
  25. #include <asm/pal.h>
  26. #include <asm/sal.h>
  27. #include <asm/tlb.h>
  28. /*
  29. * Handy macros to make sure that the PAL return values start out
  30. * as something meaningful.
  31. */
  32. #define INIT_PAL_STATUS_UNIMPLEMENTED(x) \
  33. { \
  34. x.status = PAL_STATUS_UNIMPLEMENTED; \
  35. x.v0 = 0; \
  36. x.v1 = 0; \
  37. x.v2 = 0; \
  38. }
  39. #define INIT_PAL_STATUS_SUCCESS(x) \
  40. { \
  41. x.status = PAL_STATUS_SUCCESS; \
  42. x.v0 = 0; \
  43. x.v1 = 0; \
  44. x.v2 = 0; \
  45. }
  46. static void kvm_get_pal_call_data(struct kvm_vcpu *vcpu,
  47. u64 *gr28, u64 *gr29, u64 *gr30, u64 *gr31) {
  48. struct exit_ctl_data *p;
  49. if (vcpu) {
  50. p = &vcpu->arch.exit_data;
  51. if (p->exit_reason == EXIT_REASON_PAL_CALL) {
  52. *gr28 = p->u.pal_data.gr28;
  53. *gr29 = p->u.pal_data.gr29;
  54. *gr30 = p->u.pal_data.gr30;
  55. *gr31 = p->u.pal_data.gr31;
  56. return ;
  57. }
  58. }
  59. printk(KERN_DEBUG"Failed to get vcpu pal data!!!\n");
  60. }
  61. static void set_pal_result(struct kvm_vcpu *vcpu,
  62. struct ia64_pal_retval result) {
  63. struct exit_ctl_data *p;
  64. p = kvm_get_exit_data(vcpu);
  65. if (p && p->exit_reason == EXIT_REASON_PAL_CALL) {
  66. p->u.pal_data.ret = result;
  67. return ;
  68. }
  69. INIT_PAL_STATUS_UNIMPLEMENTED(p->u.pal_data.ret);
  70. }
  71. static void set_sal_result(struct kvm_vcpu *vcpu,
  72. struct sal_ret_values result) {
  73. struct exit_ctl_data *p;
  74. p = kvm_get_exit_data(vcpu);
  75. if (p && p->exit_reason == EXIT_REASON_SAL_CALL) {
  76. p->u.sal_data.ret = result;
  77. return ;
  78. }
  79. printk(KERN_WARNING"Failed to set sal result!!\n");
  80. }
  81. struct cache_flush_args {
  82. u64 cache_type;
  83. u64 operation;
  84. u64 progress;
  85. long status;
  86. };
  87. cpumask_t cpu_cache_coherent_map;
  88. static void remote_pal_cache_flush(void *data)
  89. {
  90. struct cache_flush_args *args = data;
  91. long status;
  92. u64 progress = args->progress;
  93. status = ia64_pal_cache_flush(args->cache_type, args->operation,
  94. &progress, NULL);
  95. if (status != 0)
  96. args->status = status;
  97. }
  98. static struct ia64_pal_retval pal_cache_flush(struct kvm_vcpu *vcpu)
  99. {
  100. u64 gr28, gr29, gr30, gr31;
  101. struct ia64_pal_retval result = {0, 0, 0, 0};
  102. struct cache_flush_args args = {0, 0, 0, 0};
  103. long psr;
  104. gr28 = gr29 = gr30 = gr31 = 0;
  105. kvm_get_pal_call_data(vcpu, &gr28, &gr29, &gr30, &gr31);
  106. if (gr31 != 0)
  107. printk(KERN_ERR"vcpu:%p called cache_flush error!\n", vcpu);
  108. /* Always call Host Pal in int=1 */
  109. gr30 &= ~PAL_CACHE_FLUSH_CHK_INTRS;
  110. args.cache_type = gr29;
  111. args.operation = gr30;
  112. smp_call_function(remote_pal_cache_flush,
  113. (void *)&args, 1);
  114. if (args.status != 0)
  115. printk(KERN_ERR"pal_cache_flush error!,"
  116. "status:0x%lx\n", args.status);
  117. /*
  118. * Call Host PAL cache flush
  119. * Clear psr.ic when call PAL_CACHE_FLUSH
  120. */
  121. local_irq_save(psr);
  122. result.status = ia64_pal_cache_flush(gr29, gr30, &result.v1,
  123. &result.v0);
  124. local_irq_restore(psr);
  125. if (result.status != 0)
  126. printk(KERN_ERR"vcpu:%p crashed due to cache_flush err:%ld"
  127. "in1:%lx,in2:%lx\n",
  128. vcpu, result.status, gr29, gr30);
  129. #if 0
  130. if (gr29 == PAL_CACHE_TYPE_COHERENT) {
  131. cpus_setall(vcpu->arch.cache_coherent_map);
  132. cpu_clear(vcpu->cpu, vcpu->arch.cache_coherent_map);
  133. cpus_setall(cpu_cache_coherent_map);
  134. cpu_clear(vcpu->cpu, cpu_cache_coherent_map);
  135. }
  136. #endif
  137. return result;
  138. }
  139. struct ia64_pal_retval pal_cache_summary(struct kvm_vcpu *vcpu)
  140. {
  141. struct ia64_pal_retval result;
  142. PAL_CALL(result, PAL_CACHE_SUMMARY, 0, 0, 0);
  143. return result;
  144. }
  145. static struct ia64_pal_retval pal_freq_base(struct kvm_vcpu *vcpu)
  146. {
  147. struct ia64_pal_retval result;
  148. PAL_CALL(result, PAL_FREQ_BASE, 0, 0, 0);
  149. /*
  150. * PAL_FREQ_BASE may not be implemented in some platforms,
  151. * call SAL instead.
  152. */
  153. if (result.v0 == 0) {
  154. result.status = ia64_sal_freq_base(SAL_FREQ_BASE_PLATFORM,
  155. &result.v0,
  156. &result.v1);
  157. result.v2 = 0;
  158. }
  159. return result;
  160. }
  161. static struct ia64_pal_retval pal_freq_ratios(struct kvm_vcpu *vcpu)
  162. {
  163. struct ia64_pal_retval result;
  164. PAL_CALL(result, PAL_FREQ_RATIOS, 0, 0, 0);
  165. return result;
  166. }
  167. static struct ia64_pal_retval pal_logical_to_physica(struct kvm_vcpu *vcpu)
  168. {
  169. struct ia64_pal_retval result;
  170. INIT_PAL_STATUS_UNIMPLEMENTED(result);
  171. return result;
  172. }
  173. static struct ia64_pal_retval pal_platform_addr(struct kvm_vcpu *vcpu)
  174. {
  175. struct ia64_pal_retval result;
  176. INIT_PAL_STATUS_SUCCESS(result);
  177. return result;
  178. }
  179. static struct ia64_pal_retval pal_proc_get_features(struct kvm_vcpu *vcpu)
  180. {
  181. struct ia64_pal_retval result = {0, 0, 0, 0};
  182. long in0, in1, in2, in3;
  183. kvm_get_pal_call_data(vcpu, &in0, &in1, &in2, &in3);
  184. result.status = ia64_pal_proc_get_features(&result.v0, &result.v1,
  185. &result.v2, in2);
  186. return result;
  187. }
  188. static struct ia64_pal_retval pal_cache_info(struct kvm_vcpu *vcpu)
  189. {
  190. pal_cache_config_info_t ci;
  191. long status;
  192. unsigned long in0, in1, in2, in3, r9, r10;
  193. kvm_get_pal_call_data(vcpu, &in0, &in1, &in2, &in3);
  194. status = ia64_pal_cache_config_info(in1, in2, &ci);
  195. r9 = ci.pcci_info_1.pcci1_data;
  196. r10 = ci.pcci_info_2.pcci2_data;
  197. return ((struct ia64_pal_retval){status, r9, r10, 0});
  198. }
  199. #define GUEST_IMPL_VA_MSB 59
  200. #define GUEST_RID_BITS 18
  201. static struct ia64_pal_retval pal_vm_summary(struct kvm_vcpu *vcpu)
  202. {
  203. pal_vm_info_1_u_t vminfo1;
  204. pal_vm_info_2_u_t vminfo2;
  205. struct ia64_pal_retval result;
  206. PAL_CALL(result, PAL_VM_SUMMARY, 0, 0, 0);
  207. if (!result.status) {
  208. vminfo1.pvi1_val = result.v0;
  209. vminfo1.pal_vm_info_1_s.max_itr_entry = 8;
  210. vminfo1.pal_vm_info_1_s.max_dtr_entry = 8;
  211. result.v0 = vminfo1.pvi1_val;
  212. vminfo2.pal_vm_info_2_s.impl_va_msb = GUEST_IMPL_VA_MSB;
  213. vminfo2.pal_vm_info_2_s.rid_size = GUEST_RID_BITS;
  214. result.v1 = vminfo2.pvi2_val;
  215. }
  216. return result;
  217. }
  218. static struct ia64_pal_retval pal_vm_info(struct kvm_vcpu *vcpu)
  219. {
  220. struct ia64_pal_retval result;
  221. INIT_PAL_STATUS_UNIMPLEMENTED(result);
  222. return result;
  223. }
  224. static u64 kvm_get_pal_call_index(struct kvm_vcpu *vcpu)
  225. {
  226. u64 index = 0;
  227. struct exit_ctl_data *p;
  228. p = kvm_get_exit_data(vcpu);
  229. if (p && (p->exit_reason == EXIT_REASON_PAL_CALL))
  230. index = p->u.pal_data.gr28;
  231. return index;
  232. }
  233. int kvm_pal_emul(struct kvm_vcpu *vcpu, struct kvm_run *run)
  234. {
  235. u64 gr28;
  236. struct ia64_pal_retval result;
  237. int ret = 1;
  238. gr28 = kvm_get_pal_call_index(vcpu);
  239. /*printk("pal_call index:%lx\n",gr28);*/
  240. switch (gr28) {
  241. case PAL_CACHE_FLUSH:
  242. result = pal_cache_flush(vcpu);
  243. break;
  244. case PAL_CACHE_SUMMARY:
  245. result = pal_cache_summary(vcpu);
  246. break;
  247. case PAL_HALT_LIGHT:
  248. {
  249. vcpu->arch.timer_pending = 1;
  250. INIT_PAL_STATUS_SUCCESS(result);
  251. if (kvm_highest_pending_irq(vcpu) == -1)
  252. ret = kvm_emulate_halt(vcpu);
  253. }
  254. break;
  255. case PAL_FREQ_RATIOS:
  256. result = pal_freq_ratios(vcpu);
  257. break;
  258. case PAL_FREQ_BASE:
  259. result = pal_freq_base(vcpu);
  260. break;
  261. case PAL_LOGICAL_TO_PHYSICAL :
  262. result = pal_logical_to_physica(vcpu);
  263. break;
  264. case PAL_VM_SUMMARY :
  265. result = pal_vm_summary(vcpu);
  266. break;
  267. case PAL_VM_INFO :
  268. result = pal_vm_info(vcpu);
  269. break;
  270. case PAL_PLATFORM_ADDR :
  271. result = pal_platform_addr(vcpu);
  272. break;
  273. case PAL_CACHE_INFO:
  274. result = pal_cache_info(vcpu);
  275. break;
  276. case PAL_PTCE_INFO:
  277. INIT_PAL_STATUS_SUCCESS(result);
  278. result.v1 = (1L << 32) | 1L;
  279. break;
  280. case PAL_VM_PAGE_SIZE:
  281. result.status = ia64_pal_vm_page_size(&result.v0,
  282. &result.v1);
  283. break;
  284. case PAL_RSE_INFO:
  285. result.status = ia64_pal_rse_info(&result.v0,
  286. (pal_hints_u_t *)&result.v1);
  287. break;
  288. case PAL_PROC_GET_FEATURES:
  289. result = pal_proc_get_features(vcpu);
  290. break;
  291. case PAL_DEBUG_INFO:
  292. result.status = ia64_pal_debug_info(&result.v0,
  293. &result.v1);
  294. break;
  295. case PAL_VERSION:
  296. result.status = ia64_pal_version(
  297. (pal_version_u_t *)&result.v0,
  298. (pal_version_u_t *)&result.v1);
  299. break;
  300. case PAL_FIXED_ADDR:
  301. result.status = PAL_STATUS_SUCCESS;
  302. result.v0 = vcpu->vcpu_id;
  303. break;
  304. default:
  305. INIT_PAL_STATUS_UNIMPLEMENTED(result);
  306. printk(KERN_WARNING"kvm: Unsupported pal call,"
  307. " index:0x%lx\n", gr28);
  308. }
  309. set_pal_result(vcpu, result);
  310. return ret;
  311. }
  312. static struct sal_ret_values sal_emulator(struct kvm *kvm,
  313. long index, unsigned long in1,
  314. unsigned long in2, unsigned long in3,
  315. unsigned long in4, unsigned long in5,
  316. unsigned long in6, unsigned long in7)
  317. {
  318. unsigned long r9 = 0;
  319. unsigned long r10 = 0;
  320. long r11 = 0;
  321. long status;
  322. status = 0;
  323. switch (index) {
  324. case SAL_FREQ_BASE:
  325. status = ia64_sal_freq_base(in1, &r9, &r10);
  326. break;
  327. case SAL_PCI_CONFIG_READ:
  328. printk(KERN_WARNING"kvm: Not allowed to call here!"
  329. " SAL_PCI_CONFIG_READ\n");
  330. break;
  331. case SAL_PCI_CONFIG_WRITE:
  332. printk(KERN_WARNING"kvm: Not allowed to call here!"
  333. " SAL_PCI_CONFIG_WRITE\n");
  334. break;
  335. case SAL_SET_VECTORS:
  336. if (in1 == SAL_VECTOR_OS_BOOT_RENDEZ) {
  337. if (in4 != 0 || in5 != 0 || in6 != 0 || in7 != 0) {
  338. status = -2;
  339. } else {
  340. kvm->arch.rdv_sal_data.boot_ip = in2;
  341. kvm->arch.rdv_sal_data.boot_gp = in3;
  342. }
  343. printk("Rendvous called! iip:%lx\n\n", in2);
  344. } else
  345. printk(KERN_WARNING"kvm: CALLED SAL_SET_VECTORS %lu."
  346. "ignored...\n", in1);
  347. break;
  348. case SAL_GET_STATE_INFO:
  349. /* No more info. */
  350. status = -5;
  351. r9 = 0;
  352. break;
  353. case SAL_GET_STATE_INFO_SIZE:
  354. /* Return a dummy size. */
  355. status = 0;
  356. r9 = 128;
  357. break;
  358. case SAL_CLEAR_STATE_INFO:
  359. /* Noop. */
  360. break;
  361. case SAL_MC_RENDEZ:
  362. printk(KERN_WARNING
  363. "kvm: called SAL_MC_RENDEZ. ignored...\n");
  364. break;
  365. case SAL_MC_SET_PARAMS:
  366. printk(KERN_WARNING
  367. "kvm: called SAL_MC_SET_PARAMS.ignored!\n");
  368. break;
  369. case SAL_CACHE_FLUSH:
  370. if (1) {
  371. /*Flush using SAL.
  372. This method is faster but has a side
  373. effect on other vcpu running on
  374. this cpu. */
  375. status = ia64_sal_cache_flush(in1);
  376. } else {
  377. /*Maybe need to implement the method
  378. without side effect!*/
  379. status = 0;
  380. }
  381. break;
  382. case SAL_CACHE_INIT:
  383. printk(KERN_WARNING
  384. "kvm: called SAL_CACHE_INIT. ignored...\n");
  385. break;
  386. case SAL_UPDATE_PAL:
  387. printk(KERN_WARNING
  388. "kvm: CALLED SAL_UPDATE_PAL. ignored...\n");
  389. break;
  390. default:
  391. printk(KERN_WARNING"kvm: called SAL_CALL with unknown index."
  392. " index:%ld\n", index);
  393. status = -1;
  394. break;
  395. }
  396. return ((struct sal_ret_values) {status, r9, r10, r11});
  397. }
  398. static void kvm_get_sal_call_data(struct kvm_vcpu *vcpu, u64 *in0, u64 *in1,
  399. u64 *in2, u64 *in3, u64 *in4, u64 *in5, u64 *in6, u64 *in7){
  400. struct exit_ctl_data *p;
  401. p = kvm_get_exit_data(vcpu);
  402. if (p) {
  403. if (p->exit_reason == EXIT_REASON_SAL_CALL) {
  404. *in0 = p->u.sal_data.in0;
  405. *in1 = p->u.sal_data.in1;
  406. *in2 = p->u.sal_data.in2;
  407. *in3 = p->u.sal_data.in3;
  408. *in4 = p->u.sal_data.in4;
  409. *in5 = p->u.sal_data.in5;
  410. *in6 = p->u.sal_data.in6;
  411. *in7 = p->u.sal_data.in7;
  412. return ;
  413. }
  414. }
  415. *in0 = 0;
  416. }
  417. void kvm_sal_emul(struct kvm_vcpu *vcpu)
  418. {
  419. struct sal_ret_values result;
  420. u64 index, in1, in2, in3, in4, in5, in6, in7;
  421. kvm_get_sal_call_data(vcpu, &index, &in1, &in2,
  422. &in3, &in4, &in5, &in6, &in7);
  423. result = sal_emulator(vcpu->kvm, index, in1, in2, in3,
  424. in4, in5, in6, in7);
  425. set_sal_result(vcpu, result);
  426. }