plpar_wrappers.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_POWERPC_PLPAR_WRAPPERS_H
  3. #define _ASM_POWERPC_PLPAR_WRAPPERS_H
  4. #ifdef CONFIG_PPC_PSERIES
  5. #include <linux/string.h>
  6. #include <linux/irqflags.h>
  7. #include <asm/hvcall.h>
  8. #include <asm/paca.h>
  9. #include <asm/lppaca.h>
  10. #include <asm/page.h>
  11. static inline long poll_pending(void)
  12. {
  13. return plpar_hcall_norets(H_POLL_PENDING);
  14. }
  15. static inline u8 get_cede_latency_hint(void)
  16. {
  17. return get_lppaca()->cede_latency_hint;
  18. }
  19. static inline void set_cede_latency_hint(u8 latency_hint)
  20. {
  21. get_lppaca()->cede_latency_hint = latency_hint;
  22. }
  23. static inline long cede_processor(void)
  24. {
  25. /*
  26. * We cannot call tracepoints inside RCU idle regions which
  27. * means we must not trace H_CEDE.
  28. */
  29. return plpar_hcall_norets_notrace(H_CEDE);
  30. }
  31. static inline long extended_cede_processor(unsigned long latency_hint)
  32. {
  33. long rc;
  34. u8 old_latency_hint = get_cede_latency_hint();
  35. set_cede_latency_hint(latency_hint);
  36. rc = cede_processor();
  37. /* Ensure that H_CEDE returns with IRQs on */
  38. if (WARN_ON(IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG) && !(mfmsr() & MSR_EE)))
  39. __hard_irq_enable();
  40. set_cede_latency_hint(old_latency_hint);
  41. return rc;
  42. }
  43. static inline long vpa_call(unsigned long flags, unsigned long cpu,
  44. unsigned long vpa)
  45. {
  46. flags = flags << H_VPA_FUNC_SHIFT;
  47. return plpar_hcall_norets(H_REGISTER_VPA, flags, cpu, vpa);
  48. }
  49. static inline long unregister_vpa(unsigned long cpu)
  50. {
  51. return vpa_call(H_VPA_DEREG_VPA, cpu, 0);
  52. }
  53. static inline long register_vpa(unsigned long cpu, unsigned long vpa)
  54. {
  55. return vpa_call(H_VPA_REG_VPA, cpu, vpa);
  56. }
  57. static inline long unregister_slb_shadow(unsigned long cpu)
  58. {
  59. return vpa_call(H_VPA_DEREG_SLB, cpu, 0);
  60. }
  61. static inline long register_slb_shadow(unsigned long cpu, unsigned long vpa)
  62. {
  63. return vpa_call(H_VPA_REG_SLB, cpu, vpa);
  64. }
  65. static inline long unregister_dtl(unsigned long cpu)
  66. {
  67. return vpa_call(H_VPA_DEREG_DTL, cpu, 0);
  68. }
  69. static inline long register_dtl(unsigned long cpu, unsigned long vpa)
  70. {
  71. return vpa_call(H_VPA_REG_DTL, cpu, vpa);
  72. }
  73. extern void vpa_init(int cpu);
  74. static inline long plpar_pte_enter(unsigned long flags,
  75. unsigned long hpte_group, unsigned long hpte_v,
  76. unsigned long hpte_r, unsigned long *slot)
  77. {
  78. long rc;
  79. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  80. rc = plpar_hcall(H_ENTER, retbuf, flags, hpte_group, hpte_v, hpte_r);
  81. *slot = retbuf[0];
  82. return rc;
  83. }
  84. static inline long plpar_pte_remove(unsigned long flags, unsigned long ptex,
  85. unsigned long avpn, unsigned long *old_pteh_ret,
  86. unsigned long *old_ptel_ret)
  87. {
  88. long rc;
  89. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  90. rc = plpar_hcall(H_REMOVE, retbuf, flags, ptex, avpn);
  91. *old_pteh_ret = retbuf[0];
  92. *old_ptel_ret = retbuf[1];
  93. return rc;
  94. }
  95. /* plpar_pte_remove_raw can be called in real mode. It calls plpar_hcall_raw */
  96. static inline long plpar_pte_remove_raw(unsigned long flags, unsigned long ptex,
  97. unsigned long avpn, unsigned long *old_pteh_ret,
  98. unsigned long *old_ptel_ret)
  99. {
  100. long rc;
  101. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  102. rc = plpar_hcall_raw(H_REMOVE, retbuf, flags, ptex, avpn);
  103. *old_pteh_ret = retbuf[0];
  104. *old_ptel_ret = retbuf[1];
  105. return rc;
  106. }
  107. static inline long plpar_pte_read(unsigned long flags, unsigned long ptex,
  108. unsigned long *old_pteh_ret, unsigned long *old_ptel_ret)
  109. {
  110. long rc;
  111. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  112. rc = plpar_hcall(H_READ, retbuf, flags, ptex);
  113. *old_pteh_ret = retbuf[0];
  114. *old_ptel_ret = retbuf[1];
  115. return rc;
  116. }
  117. /* plpar_pte_read_raw can be called in real mode. It calls plpar_hcall_raw */
  118. static inline long plpar_pte_read_raw(unsigned long flags, unsigned long ptex,
  119. unsigned long *old_pteh_ret, unsigned long *old_ptel_ret)
  120. {
  121. long rc;
  122. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  123. rc = plpar_hcall_raw(H_READ, retbuf, flags, ptex);
  124. *old_pteh_ret = retbuf[0];
  125. *old_ptel_ret = retbuf[1];
  126. return rc;
  127. }
  128. /*
  129. * ptes must be 8*sizeof(unsigned long)
  130. */
  131. static inline long plpar_pte_read_4(unsigned long flags, unsigned long ptex,
  132. unsigned long *ptes)
  133. {
  134. long rc;
  135. unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
  136. rc = plpar_hcall9(H_READ, retbuf, flags | H_READ_4, ptex);
  137. memcpy(ptes, retbuf, 8*sizeof(unsigned long));
  138. return rc;
  139. }
  140. /*
  141. * plpar_pte_read_4_raw can be called in real mode.
  142. * ptes must be 8*sizeof(unsigned long)
  143. */
  144. static inline long plpar_pte_read_4_raw(unsigned long flags, unsigned long ptex,
  145. unsigned long *ptes)
  146. {
  147. long rc;
  148. unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
  149. rc = plpar_hcall9_raw(H_READ, retbuf, flags | H_READ_4, ptex);
  150. memcpy(ptes, retbuf, 8*sizeof(unsigned long));
  151. return rc;
  152. }
  153. static inline long plpar_pte_protect(unsigned long flags, unsigned long ptex,
  154. unsigned long avpn)
  155. {
  156. return plpar_hcall_norets(H_PROTECT, flags, ptex, avpn);
  157. }
  158. static inline long plpar_resize_hpt_prepare(unsigned long flags,
  159. unsigned long shift)
  160. {
  161. return plpar_hcall_norets(H_RESIZE_HPT_PREPARE, flags, shift);
  162. }
  163. static inline long plpar_resize_hpt_commit(unsigned long flags,
  164. unsigned long shift)
  165. {
  166. return plpar_hcall_norets(H_RESIZE_HPT_COMMIT, flags, shift);
  167. }
  168. static inline long plpar_tce_get(unsigned long liobn, unsigned long ioba,
  169. unsigned long *tce_ret)
  170. {
  171. long rc;
  172. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  173. rc = plpar_hcall(H_GET_TCE, retbuf, liobn, ioba);
  174. *tce_ret = retbuf[0];
  175. return rc;
  176. }
  177. static inline long plpar_tce_put(unsigned long liobn, unsigned long ioba,
  178. unsigned long tceval)
  179. {
  180. return plpar_hcall_norets(H_PUT_TCE, liobn, ioba, tceval);
  181. }
  182. static inline long plpar_tce_put_indirect(unsigned long liobn,
  183. unsigned long ioba, unsigned long page, unsigned long count)
  184. {
  185. return plpar_hcall_norets(H_PUT_TCE_INDIRECT, liobn, ioba, page, count);
  186. }
  187. static inline long plpar_tce_stuff(unsigned long liobn, unsigned long ioba,
  188. unsigned long tceval, unsigned long count)
  189. {
  190. return plpar_hcall_norets(H_STUFF_TCE, liobn, ioba, tceval, count);
  191. }
  192. /* Set various resource mode parameters */
  193. static inline long plpar_set_mode(unsigned long mflags, unsigned long resource,
  194. unsigned long value1, unsigned long value2)
  195. {
  196. return plpar_hcall_norets(H_SET_MODE, mflags, resource, value1, value2);
  197. }
  198. /*
  199. * Enable relocation on exceptions on this partition
  200. *
  201. * Note: this call has a partition wide scope and can take a while to complete.
  202. * If it returns H_LONG_BUSY_* it should be retried periodically until it
  203. * returns H_SUCCESS.
  204. */
  205. static inline long enable_reloc_on_exceptions(void)
  206. {
  207. /* mflags = 3: Exceptions at 0xC000000000004000 */
  208. return plpar_set_mode(3, H_SET_MODE_RESOURCE_ADDR_TRANS_MODE, 0, 0);
  209. }
  210. /*
  211. * Disable relocation on exceptions on this partition
  212. *
  213. * Note: this call has a partition wide scope and can take a while to complete.
  214. * If it returns H_LONG_BUSY_* it should be retried periodically until it
  215. * returns H_SUCCESS.
  216. */
  217. static inline long disable_reloc_on_exceptions(void) {
  218. return plpar_set_mode(0, H_SET_MODE_RESOURCE_ADDR_TRANS_MODE, 0, 0);
  219. }
  220. /*
  221. * Take exceptions in big endian mode on this partition
  222. *
  223. * Note: this call has a partition wide scope and can take a while to complete.
  224. * If it returns H_LONG_BUSY_* it should be retried periodically until it
  225. * returns H_SUCCESS.
  226. */
  227. static inline long enable_big_endian_exceptions(void)
  228. {
  229. /* mflags = 0: big endian exceptions */
  230. return plpar_set_mode(0, H_SET_MODE_RESOURCE_LE, 0, 0);
  231. }
  232. /*
  233. * Take exceptions in little endian mode on this partition
  234. *
  235. * Note: this call has a partition wide scope and can take a while to complete.
  236. * If it returns H_LONG_BUSY_* it should be retried periodically until it
  237. * returns H_SUCCESS.
  238. */
  239. static inline long enable_little_endian_exceptions(void)
  240. {
  241. /* mflags = 1: little endian exceptions */
  242. return plpar_set_mode(1, H_SET_MODE_RESOURCE_LE, 0, 0);
  243. }
  244. static inline long plpar_set_ciabr(unsigned long ciabr)
  245. {
  246. return plpar_set_mode(0, H_SET_MODE_RESOURCE_SET_CIABR, ciabr, 0);
  247. }
  248. static inline long plpar_set_watchpoint0(unsigned long dawr0, unsigned long dawrx0)
  249. {
  250. return plpar_set_mode(0, H_SET_MODE_RESOURCE_SET_DAWR0, dawr0, dawrx0);
  251. }
  252. static inline long plpar_set_watchpoint1(unsigned long dawr1, unsigned long dawrx1)
  253. {
  254. return plpar_set_mode(0, H_SET_MODE_RESOURCE_SET_DAWR1, dawr1, dawrx1);
  255. }
  256. static inline long plpar_signal_sys_reset(long cpu)
  257. {
  258. return plpar_hcall_norets(H_SIGNAL_SYS_RESET, cpu);
  259. }
  260. static inline long plpar_get_cpu_characteristics(struct h_cpu_char_result *p)
  261. {
  262. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  263. long rc;
  264. rc = plpar_hcall(H_GET_CPU_CHARACTERISTICS, retbuf);
  265. if (rc == H_SUCCESS) {
  266. p->character = retbuf[0];
  267. p->behaviour = retbuf[1];
  268. }
  269. return rc;
  270. }
  271. /*
  272. * Wrapper to H_RPT_INVALIDATE hcall that handles return values appropriately
  273. *
  274. * - Returns H_SUCCESS on success
  275. * - For H_BUSY return value, we retry the hcall.
  276. * - For any other hcall failures, attempt a full flush once before
  277. * resorting to BUG().
  278. *
  279. * Note: This hcall is expected to fail only very rarely. The correct
  280. * error recovery of killing the process/guest will be eventually
  281. * needed.
  282. */
  283. static inline long pseries_rpt_invalidate(u32 pid, u64 target, u64 type,
  284. u64 page_sizes, u64 start, u64 end)
  285. {
  286. long rc;
  287. unsigned long all;
  288. while (true) {
  289. rc = plpar_hcall_norets(H_RPT_INVALIDATE, pid, target, type,
  290. page_sizes, start, end);
  291. if (rc == H_BUSY) {
  292. cpu_relax();
  293. continue;
  294. } else if (rc == H_SUCCESS)
  295. return rc;
  296. /* Flush request failed, try with a full flush once */
  297. if (type & H_RPTI_TYPE_NESTED)
  298. all = H_RPTI_TYPE_NESTED | H_RPTI_TYPE_NESTED_ALL;
  299. else
  300. all = H_RPTI_TYPE_ALL;
  301. retry:
  302. rc = plpar_hcall_norets(H_RPT_INVALIDATE, pid, target,
  303. all, page_sizes, 0, -1UL);
  304. if (rc == H_BUSY) {
  305. cpu_relax();
  306. goto retry;
  307. } else if (rc == H_SUCCESS)
  308. return rc;
  309. BUG();
  310. }
  311. }
  312. #else /* !CONFIG_PPC_PSERIES */
  313. static inline long plpar_set_ciabr(unsigned long ciabr)
  314. {
  315. return 0;
  316. }
  317. static inline long plpar_pte_read_4(unsigned long flags, unsigned long ptex,
  318. unsigned long *ptes)
  319. {
  320. return 0;
  321. }
  322. static inline long pseries_rpt_invalidate(u32 pid, u64 target, u64 type,
  323. u64 page_sizes, u64 start, u64 end)
  324. {
  325. return 0;
  326. }
  327. #endif /* CONFIG_PPC_PSERIES */
  328. #endif /* _ASM_POWERPC_PLPAR_WRAPPERS_H */