tlb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * KVM/MIPS TLB handling, this file is part of the Linux host kernel so that
  7. * TLB handlers run from KSEG0
  8. *
  9. * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
  10. * Authors: Sanjay Lal <[email protected]>
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/smp.h>
  14. #include <linux/mm.h>
  15. #include <linux/delay.h>
  16. #include <linux/export.h>
  17. #include <linux/kvm_host.h>
  18. #include <linux/srcu.h>
  19. #include <asm/cpu.h>
  20. #include <asm/bootinfo.h>
  21. #include <asm/mipsregs.h>
  22. #include <asm/mmu_context.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/tlb.h>
  25. #include <asm/tlbdebug.h>
  26. #undef CONFIG_MIPS_MT
  27. #include <asm/r4kcache.h>
  28. #define CONFIG_MIPS_MT
  29. unsigned long GUESTID_MASK;
  30. EXPORT_SYMBOL_GPL(GUESTID_MASK);
  31. unsigned long GUESTID_FIRST_VERSION;
  32. EXPORT_SYMBOL_GPL(GUESTID_FIRST_VERSION);
  33. unsigned long GUESTID_VERSION_MASK;
  34. EXPORT_SYMBOL_GPL(GUESTID_VERSION_MASK);
  35. static u32 kvm_mips_get_root_asid(struct kvm_vcpu *vcpu)
  36. {
  37. struct mm_struct *gpa_mm = &vcpu->kvm->arch.gpa_mm;
  38. if (cpu_has_guestid)
  39. return 0;
  40. else
  41. return cpu_asid(smp_processor_id(), gpa_mm);
  42. }
  43. static int _kvm_mips_host_tlb_inv(unsigned long entryhi)
  44. {
  45. int idx;
  46. write_c0_entryhi(entryhi);
  47. mtc0_tlbw_hazard();
  48. tlb_probe();
  49. tlb_probe_hazard();
  50. idx = read_c0_index();
  51. BUG_ON(idx >= current_cpu_data.tlbsize);
  52. if (idx >= 0) {
  53. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  54. write_c0_entrylo0(0);
  55. write_c0_entrylo1(0);
  56. mtc0_tlbw_hazard();
  57. tlb_write_indexed();
  58. tlbw_use_hazard();
  59. }
  60. return idx;
  61. }
  62. /* GuestID management */
  63. /**
  64. * clear_root_gid() - Set GuestCtl1.RID for normal root operation.
  65. */
  66. static inline void clear_root_gid(void)
  67. {
  68. if (cpu_has_guestid) {
  69. clear_c0_guestctl1(MIPS_GCTL1_RID);
  70. mtc0_tlbw_hazard();
  71. }
  72. }
  73. /**
  74. * set_root_gid_to_guest_gid() - Set GuestCtl1.RID to match GuestCtl1.ID.
  75. *
  76. * Sets the root GuestID to match the current guest GuestID, for TLB operation
  77. * on the GPA->RPA mappings in the root TLB.
  78. *
  79. * The caller must be sure to disable HTW while the root GID is set, and
  80. * possibly longer if TLB registers are modified.
  81. */
  82. static inline void set_root_gid_to_guest_gid(void)
  83. {
  84. unsigned int guestctl1;
  85. if (cpu_has_guestid) {
  86. back_to_back_c0_hazard();
  87. guestctl1 = read_c0_guestctl1();
  88. guestctl1 = (guestctl1 & ~MIPS_GCTL1_RID) |
  89. ((guestctl1 & MIPS_GCTL1_ID) >> MIPS_GCTL1_ID_SHIFT)
  90. << MIPS_GCTL1_RID_SHIFT;
  91. write_c0_guestctl1(guestctl1);
  92. mtc0_tlbw_hazard();
  93. }
  94. }
  95. int kvm_vz_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long va)
  96. {
  97. int idx;
  98. unsigned long flags, old_entryhi;
  99. local_irq_save(flags);
  100. htw_stop();
  101. /* Set root GuestID for root probe and write of guest TLB entry */
  102. set_root_gid_to_guest_gid();
  103. old_entryhi = read_c0_entryhi();
  104. idx = _kvm_mips_host_tlb_inv((va & VPN2_MASK) |
  105. kvm_mips_get_root_asid(vcpu));
  106. write_c0_entryhi(old_entryhi);
  107. clear_root_gid();
  108. mtc0_tlbw_hazard();
  109. htw_start();
  110. local_irq_restore(flags);
  111. /*
  112. * We don't want to get reserved instruction exceptions for missing tlb
  113. * entries.
  114. */
  115. if (cpu_has_vtag_icache)
  116. flush_icache_all();
  117. if (idx > 0)
  118. kvm_debug("%s: Invalidated root entryhi %#lx @ idx %d\n",
  119. __func__, (va & VPN2_MASK) |
  120. kvm_mips_get_root_asid(vcpu), idx);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL_GPL(kvm_vz_host_tlb_inv);
  124. /**
  125. * kvm_vz_guest_tlb_lookup() - Lookup a guest VZ TLB mapping.
  126. * @vcpu: KVM VCPU pointer.
  127. * @gpa: Guest virtual address in a TLB mapped guest segment.
  128. * @gpa: Pointer to output guest physical address it maps to.
  129. *
  130. * Converts a guest virtual address in a guest TLB mapped segment to a guest
  131. * physical address, by probing the guest TLB.
  132. *
  133. * Returns: 0 if guest TLB mapping exists for @gva. *@gpa will have been
  134. * written.
  135. * -EFAULT if no guest TLB mapping exists for @gva. *@gpa may not
  136. * have been written.
  137. */
  138. int kvm_vz_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long gva,
  139. unsigned long *gpa)
  140. {
  141. unsigned long o_entryhi, o_entrylo[2], o_pagemask;
  142. unsigned int o_index;
  143. unsigned long entrylo[2], pagemask, pagemaskbit, pa;
  144. unsigned long flags;
  145. int index;
  146. /* Probe the guest TLB for a mapping */
  147. local_irq_save(flags);
  148. /* Set root GuestID for root probe of guest TLB entry */
  149. htw_stop();
  150. set_root_gid_to_guest_gid();
  151. o_entryhi = read_gc0_entryhi();
  152. o_index = read_gc0_index();
  153. write_gc0_entryhi((o_entryhi & 0x3ff) | (gva & ~0xfffl));
  154. mtc0_tlbw_hazard();
  155. guest_tlb_probe();
  156. tlb_probe_hazard();
  157. index = read_gc0_index();
  158. if (index < 0) {
  159. /* No match, fail */
  160. write_gc0_entryhi(o_entryhi);
  161. write_gc0_index(o_index);
  162. clear_root_gid();
  163. htw_start();
  164. local_irq_restore(flags);
  165. return -EFAULT;
  166. }
  167. /* Match! read the TLB entry */
  168. o_entrylo[0] = read_gc0_entrylo0();
  169. o_entrylo[1] = read_gc0_entrylo1();
  170. o_pagemask = read_gc0_pagemask();
  171. mtc0_tlbr_hazard();
  172. guest_tlb_read();
  173. tlb_read_hazard();
  174. entrylo[0] = read_gc0_entrylo0();
  175. entrylo[1] = read_gc0_entrylo1();
  176. pagemask = ~read_gc0_pagemask() & ~0x1fffl;
  177. write_gc0_entryhi(o_entryhi);
  178. write_gc0_index(o_index);
  179. write_gc0_entrylo0(o_entrylo[0]);
  180. write_gc0_entrylo1(o_entrylo[1]);
  181. write_gc0_pagemask(o_pagemask);
  182. clear_root_gid();
  183. htw_start();
  184. local_irq_restore(flags);
  185. /* Select one of the EntryLo values and interpret the GPA */
  186. pagemaskbit = (pagemask ^ (pagemask & (pagemask - 1))) >> 1;
  187. pa = entrylo[!!(gva & pagemaskbit)];
  188. /*
  189. * TLB entry may have become invalid since TLB probe if physical FTLB
  190. * entries are shared between threads (e.g. I6400).
  191. */
  192. if (!(pa & ENTRYLO_V))
  193. return -EFAULT;
  194. /*
  195. * Note, this doesn't take guest MIPS32 XPA into account, where PFN is
  196. * split with XI/RI in the middle.
  197. */
  198. pa = (pa << 6) & ~0xfffl;
  199. pa |= gva & ~(pagemask | pagemaskbit);
  200. *gpa = pa;
  201. return 0;
  202. }
  203. EXPORT_SYMBOL_GPL(kvm_vz_guest_tlb_lookup);
  204. /**
  205. * kvm_vz_local_flush_roottlb_all_guests() - Flush all root TLB entries for
  206. * guests.
  207. *
  208. * Invalidate all entries in root tlb which are GPA mappings.
  209. */
  210. void kvm_vz_local_flush_roottlb_all_guests(void)
  211. {
  212. unsigned long flags;
  213. unsigned long old_entryhi, old_pagemask, old_guestctl1;
  214. int entry;
  215. if (WARN_ON(!cpu_has_guestid))
  216. return;
  217. local_irq_save(flags);
  218. htw_stop();
  219. /* TLBR may clobber EntryHi.ASID, PageMask, and GuestCtl1.RID */
  220. old_entryhi = read_c0_entryhi();
  221. old_pagemask = read_c0_pagemask();
  222. old_guestctl1 = read_c0_guestctl1();
  223. /*
  224. * Invalidate guest entries in root TLB while leaving root entries
  225. * intact when possible.
  226. */
  227. for (entry = 0; entry < current_cpu_data.tlbsize; entry++) {
  228. write_c0_index(entry);
  229. mtc0_tlbw_hazard();
  230. tlb_read();
  231. tlb_read_hazard();
  232. /* Don't invalidate non-guest (RVA) mappings in the root TLB */
  233. if (!(read_c0_guestctl1() & MIPS_GCTL1_RID))
  234. continue;
  235. /* Make sure all entries differ. */
  236. write_c0_entryhi(UNIQUE_ENTRYHI(entry));
  237. write_c0_entrylo0(0);
  238. write_c0_entrylo1(0);
  239. write_c0_guestctl1(0);
  240. mtc0_tlbw_hazard();
  241. tlb_write_indexed();
  242. }
  243. write_c0_entryhi(old_entryhi);
  244. write_c0_pagemask(old_pagemask);
  245. write_c0_guestctl1(old_guestctl1);
  246. tlbw_use_hazard();
  247. htw_start();
  248. local_irq_restore(flags);
  249. }
  250. EXPORT_SYMBOL_GPL(kvm_vz_local_flush_roottlb_all_guests);
  251. /**
  252. * kvm_vz_local_flush_guesttlb_all() - Flush all guest TLB entries.
  253. *
  254. * Invalidate all entries in guest tlb irrespective of guestid.
  255. */
  256. void kvm_vz_local_flush_guesttlb_all(void)
  257. {
  258. unsigned long flags;
  259. unsigned long old_index;
  260. unsigned long old_entryhi;
  261. unsigned long old_entrylo[2];
  262. unsigned long old_pagemask;
  263. int entry;
  264. u64 cvmmemctl2 = 0;
  265. local_irq_save(flags);
  266. /* Preserve all clobbered guest registers */
  267. old_index = read_gc0_index();
  268. old_entryhi = read_gc0_entryhi();
  269. old_entrylo[0] = read_gc0_entrylo0();
  270. old_entrylo[1] = read_gc0_entrylo1();
  271. old_pagemask = read_gc0_pagemask();
  272. switch (current_cpu_type()) {
  273. case CPU_CAVIUM_OCTEON3:
  274. /* Inhibit machine check due to multiple matching TLB entries */
  275. cvmmemctl2 = read_c0_cvmmemctl2();
  276. cvmmemctl2 |= CVMMEMCTL2_INHIBITTS;
  277. write_c0_cvmmemctl2(cvmmemctl2);
  278. break;
  279. }
  280. /* Invalidate guest entries in guest TLB */
  281. write_gc0_entrylo0(0);
  282. write_gc0_entrylo1(0);
  283. write_gc0_pagemask(0);
  284. for (entry = 0; entry < current_cpu_data.guest.tlbsize; entry++) {
  285. /* Make sure all entries differ. */
  286. write_gc0_index(entry);
  287. write_gc0_entryhi(UNIQUE_GUEST_ENTRYHI(entry));
  288. mtc0_tlbw_hazard();
  289. guest_tlb_write_indexed();
  290. }
  291. if (cvmmemctl2) {
  292. cvmmemctl2 &= ~CVMMEMCTL2_INHIBITTS;
  293. write_c0_cvmmemctl2(cvmmemctl2);
  294. }
  295. write_gc0_index(old_index);
  296. write_gc0_entryhi(old_entryhi);
  297. write_gc0_entrylo0(old_entrylo[0]);
  298. write_gc0_entrylo1(old_entrylo[1]);
  299. write_gc0_pagemask(old_pagemask);
  300. tlbw_use_hazard();
  301. local_irq_restore(flags);
  302. }
  303. EXPORT_SYMBOL_GPL(kvm_vz_local_flush_guesttlb_all);
  304. /**
  305. * kvm_vz_save_guesttlb() - Save a range of guest TLB entries.
  306. * @buf: Buffer to write TLB entries into.
  307. * @index: Start index.
  308. * @count: Number of entries to save.
  309. *
  310. * Save a range of guest TLB entries. The caller must ensure interrupts are
  311. * disabled.
  312. */
  313. void kvm_vz_save_guesttlb(struct kvm_mips_tlb *buf, unsigned int index,
  314. unsigned int count)
  315. {
  316. unsigned int end = index + count;
  317. unsigned long old_entryhi, old_entrylo0, old_entrylo1, old_pagemask;
  318. unsigned int guestctl1 = 0;
  319. int old_index, i;
  320. /* Save registers we're about to clobber */
  321. old_index = read_gc0_index();
  322. old_entryhi = read_gc0_entryhi();
  323. old_entrylo0 = read_gc0_entrylo0();
  324. old_entrylo1 = read_gc0_entrylo1();
  325. old_pagemask = read_gc0_pagemask();
  326. /* Set root GuestID for root probe */
  327. htw_stop();
  328. set_root_gid_to_guest_gid();
  329. if (cpu_has_guestid)
  330. guestctl1 = read_c0_guestctl1();
  331. /* Read each entry from guest TLB */
  332. for (i = index; i < end; ++i, ++buf) {
  333. write_gc0_index(i);
  334. mtc0_tlbr_hazard();
  335. guest_tlb_read();
  336. tlb_read_hazard();
  337. if (cpu_has_guestid &&
  338. (read_c0_guestctl1() ^ guestctl1) & MIPS_GCTL1_RID) {
  339. /* Entry invalid or belongs to another guest */
  340. buf->tlb_hi = UNIQUE_GUEST_ENTRYHI(i);
  341. buf->tlb_lo[0] = 0;
  342. buf->tlb_lo[1] = 0;
  343. buf->tlb_mask = 0;
  344. } else {
  345. /* Entry belongs to the right guest */
  346. buf->tlb_hi = read_gc0_entryhi();
  347. buf->tlb_lo[0] = read_gc0_entrylo0();
  348. buf->tlb_lo[1] = read_gc0_entrylo1();
  349. buf->tlb_mask = read_gc0_pagemask();
  350. }
  351. }
  352. /* Clear root GuestID again */
  353. clear_root_gid();
  354. htw_start();
  355. /* Restore clobbered registers */
  356. write_gc0_index(old_index);
  357. write_gc0_entryhi(old_entryhi);
  358. write_gc0_entrylo0(old_entrylo0);
  359. write_gc0_entrylo1(old_entrylo1);
  360. write_gc0_pagemask(old_pagemask);
  361. tlbw_use_hazard();
  362. }
  363. EXPORT_SYMBOL_GPL(kvm_vz_save_guesttlb);
  364. /**
  365. * kvm_vz_load_guesttlb() - Save a range of guest TLB entries.
  366. * @buf: Buffer to read TLB entries from.
  367. * @index: Start index.
  368. * @count: Number of entries to load.
  369. *
  370. * Load a range of guest TLB entries. The caller must ensure interrupts are
  371. * disabled.
  372. */
  373. void kvm_vz_load_guesttlb(const struct kvm_mips_tlb *buf, unsigned int index,
  374. unsigned int count)
  375. {
  376. unsigned int end = index + count;
  377. unsigned long old_entryhi, old_entrylo0, old_entrylo1, old_pagemask;
  378. int old_index, i;
  379. /* Save registers we're about to clobber */
  380. old_index = read_gc0_index();
  381. old_entryhi = read_gc0_entryhi();
  382. old_entrylo0 = read_gc0_entrylo0();
  383. old_entrylo1 = read_gc0_entrylo1();
  384. old_pagemask = read_gc0_pagemask();
  385. /* Set root GuestID for root probe */
  386. htw_stop();
  387. set_root_gid_to_guest_gid();
  388. /* Write each entry to guest TLB */
  389. for (i = index; i < end; ++i, ++buf) {
  390. write_gc0_index(i);
  391. write_gc0_entryhi(buf->tlb_hi);
  392. write_gc0_entrylo0(buf->tlb_lo[0]);
  393. write_gc0_entrylo1(buf->tlb_lo[1]);
  394. write_gc0_pagemask(buf->tlb_mask);
  395. mtc0_tlbw_hazard();
  396. guest_tlb_write_indexed();
  397. }
  398. /* Clear root GuestID again */
  399. clear_root_gid();
  400. htw_start();
  401. /* Restore clobbered registers */
  402. write_gc0_index(old_index);
  403. write_gc0_entryhi(old_entryhi);
  404. write_gc0_entrylo0(old_entrylo0);
  405. write_gc0_entrylo1(old_entrylo1);
  406. write_gc0_pagemask(old_pagemask);
  407. tlbw_use_hazard();
  408. }
  409. EXPORT_SYMBOL_GPL(kvm_vz_load_guesttlb);
  410. #ifdef CONFIG_CPU_LOONGSON64
  411. void kvm_loongson_clear_guest_vtlb(void)
  412. {
  413. int idx = read_gc0_index();
  414. /* Set root GuestID for root probe and write of guest TLB entry */
  415. set_root_gid_to_guest_gid();
  416. write_gc0_index(0);
  417. guest_tlbinvf();
  418. write_gc0_index(idx);
  419. clear_root_gid();
  420. set_c0_diag(LOONGSON_DIAG_ITLB | LOONGSON_DIAG_DTLB);
  421. }
  422. EXPORT_SYMBOL_GPL(kvm_loongson_clear_guest_vtlb);
  423. void kvm_loongson_clear_guest_ftlb(void)
  424. {
  425. int i;
  426. int idx = read_gc0_index();
  427. /* Set root GuestID for root probe and write of guest TLB entry */
  428. set_root_gid_to_guest_gid();
  429. for (i = current_cpu_data.tlbsizevtlb;
  430. i < (current_cpu_data.tlbsizevtlb +
  431. current_cpu_data.tlbsizeftlbsets);
  432. i++) {
  433. write_gc0_index(i);
  434. guest_tlbinvf();
  435. }
  436. write_gc0_index(idx);
  437. clear_root_gid();
  438. set_c0_diag(LOONGSON_DIAG_ITLB | LOONGSON_DIAG_DTLB);
  439. }
  440. EXPORT_SYMBOL_GPL(kvm_loongson_clear_guest_ftlb);
  441. #endif