1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_POWERPC_PLPAR_WRAPPERS_H
3 #define _ASM_POWERPC_PLPAR_WRAPPERS_H
5 #ifdef CONFIG_PPC_PSERIES
7 #include <linux/string.h>
8 #include <linux/irqflags.h>
9 #include <linux/delay.h>
11 #include <asm/hvcall.h>
13 #include <asm/lppaca.h>
16 static inline long poll_pending(void)
18 return plpar_hcall_norets(H_POLL_PENDING);
21 static inline u8 get_cede_latency_hint(void)
23 return get_lppaca()->cede_latency_hint;
26 static inline void set_cede_latency_hint(u8 latency_hint)
28 get_lppaca()->cede_latency_hint = latency_hint;
31 static inline long cede_processor(void)
34 * We cannot call tracepoints inside RCU idle regions which
35 * means we must not trace H_CEDE.
37 return plpar_hcall_norets_notrace(H_CEDE);
40 static inline long extended_cede_processor(unsigned long latency_hint)
43 u8 old_latency_hint = get_cede_latency_hint();
45 set_cede_latency_hint(latency_hint);
47 rc = cede_processor();
49 /* Ensure that H_CEDE returns with IRQs on */
50 if (WARN_ON(IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG) && !(mfmsr() & MSR_EE)))
53 set_cede_latency_hint(old_latency_hint);
58 static inline long vpa_call(unsigned long flags, unsigned long cpu,
61 flags = flags << H_VPA_FUNC_SHIFT;
63 return plpar_hcall_norets(H_REGISTER_VPA, flags, cpu, vpa);
66 static inline long unregister_vpa(unsigned long cpu)
68 return vpa_call(H_VPA_DEREG_VPA, cpu, 0);
71 static inline long register_vpa(unsigned long cpu, unsigned long vpa)
73 return vpa_call(H_VPA_REG_VPA, cpu, vpa);
76 static inline long unregister_slb_shadow(unsigned long cpu)
78 return vpa_call(H_VPA_DEREG_SLB, cpu, 0);
81 static inline long register_slb_shadow(unsigned long cpu, unsigned long vpa)
83 return vpa_call(H_VPA_REG_SLB, cpu, vpa);
86 static inline long unregister_dtl(unsigned long cpu)
88 return vpa_call(H_VPA_DEREG_DTL, cpu, 0);
91 static inline long register_dtl(unsigned long cpu, unsigned long vpa)
93 return vpa_call(H_VPA_REG_DTL, cpu, vpa);
96 extern void vpa_init(int cpu);
98 static inline long plpar_pte_enter(unsigned long flags,
99 unsigned long hpte_group, unsigned long hpte_v,
100 unsigned long hpte_r, unsigned long *slot)
103 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
105 rc = plpar_hcall(H_ENTER, retbuf, flags, hpte_group, hpte_v, hpte_r);
112 static inline long plpar_pte_remove(unsigned long flags, unsigned long ptex,
113 unsigned long avpn, unsigned long *old_pteh_ret,
114 unsigned long *old_ptel_ret)
117 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
119 rc = plpar_hcall(H_REMOVE, retbuf, flags, ptex, avpn);
121 *old_pteh_ret = retbuf[0];
122 *old_ptel_ret = retbuf[1];
127 /* plpar_pte_remove_raw can be called in real mode. It calls plpar_hcall_raw */
128 static inline long plpar_pte_remove_raw(unsigned long flags, unsigned long ptex,
129 unsigned long avpn, unsigned long *old_pteh_ret,
130 unsigned long *old_ptel_ret)
133 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
135 rc = plpar_hcall_raw(H_REMOVE, retbuf, flags, ptex, avpn);
137 *old_pteh_ret = retbuf[0];
138 *old_ptel_ret = retbuf[1];
143 static inline long plpar_pte_read(unsigned long flags, unsigned long ptex,
144 unsigned long *old_pteh_ret, unsigned long *old_ptel_ret)
147 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
149 rc = plpar_hcall(H_READ, retbuf, flags, ptex);
151 *old_pteh_ret = retbuf[0];
152 *old_ptel_ret = retbuf[1];
157 /* plpar_pte_read_raw can be called in real mode. It calls plpar_hcall_raw */
158 static inline long plpar_pte_read_raw(unsigned long flags, unsigned long ptex,
159 unsigned long *old_pteh_ret, unsigned long *old_ptel_ret)
162 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
164 rc = plpar_hcall_raw(H_READ, retbuf, flags, ptex);
166 *old_pteh_ret = retbuf[0];
167 *old_ptel_ret = retbuf[1];
173 * ptes must be 8*sizeof(unsigned long)
175 static inline long plpar_pte_read_4(unsigned long flags, unsigned long ptex,
180 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
182 rc = plpar_hcall9(H_READ, retbuf, flags | H_READ_4, ptex);
184 memcpy(ptes, retbuf, 8*sizeof(unsigned long));
190 * plpar_pte_read_4_raw can be called in real mode.
191 * ptes must be 8*sizeof(unsigned long)
193 static inline long plpar_pte_read_4_raw(unsigned long flags, unsigned long ptex,
198 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
200 rc = plpar_hcall9_raw(H_READ, retbuf, flags | H_READ_4, ptex);
202 memcpy(ptes, retbuf, 8*sizeof(unsigned long));
207 static inline long plpar_pte_protect(unsigned long flags, unsigned long ptex,
210 return plpar_hcall_norets(H_PROTECT, flags, ptex, avpn);
213 static inline long plpar_resize_hpt_prepare(unsigned long flags,
216 return plpar_hcall_norets(H_RESIZE_HPT_PREPARE, flags, shift);
219 static inline long plpar_resize_hpt_commit(unsigned long flags,
222 return plpar_hcall_norets(H_RESIZE_HPT_COMMIT, flags, shift);
225 static inline long plpar_tce_get(unsigned long liobn, unsigned long ioba,
226 unsigned long *tce_ret)
229 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
231 rc = plpar_hcall(H_GET_TCE, retbuf, liobn, ioba);
233 *tce_ret = retbuf[0];
238 static inline long plpar_tce_put(unsigned long liobn, unsigned long ioba,
239 unsigned long tceval)
241 return plpar_hcall_norets(H_PUT_TCE, liobn, ioba, tceval);
244 static inline long plpar_tce_put_indirect(unsigned long liobn,
245 unsigned long ioba, unsigned long page, unsigned long count)
247 return plpar_hcall_norets(H_PUT_TCE_INDIRECT, liobn, ioba, page, count);
250 static inline long plpar_tce_stuff(unsigned long liobn, unsigned long ioba,
251 unsigned long tceval, unsigned long count)
253 return plpar_hcall_norets(H_STUFF_TCE, liobn, ioba, tceval, count);
256 /* Set various resource mode parameters */
257 static inline long plpar_set_mode(unsigned long mflags, unsigned long resource,
258 unsigned long value1, unsigned long value2)
260 return plpar_hcall_norets(H_SET_MODE, mflags, resource, value1, value2);
264 * Enable relocation on exceptions on this partition
266 * Note: this call has a partition wide scope and can take a while to complete.
267 * If it returns H_LONG_BUSY_* it should be retried periodically until it
270 static inline long enable_reloc_on_exceptions(void)
272 /* mflags = 3: Exceptions at 0xC000000000004000 */
273 return plpar_set_mode(3, H_SET_MODE_RESOURCE_ADDR_TRANS_MODE, 0, 0);
277 * Disable relocation on exceptions on this partition
279 * Note: this call has a partition wide scope and can take a while to complete.
280 * If it returns H_LONG_BUSY_* it should be retried periodically until it
283 static inline long disable_reloc_on_exceptions(void) {
284 return plpar_set_mode(0, H_SET_MODE_RESOURCE_ADDR_TRANS_MODE, 0, 0);
288 * Take exceptions in big endian mode on this partition
290 * Note: this call has a partition wide scope and can take a while to complete.
291 * If it returns H_LONG_BUSY_* it should be retried periodically until it
294 static inline long enable_big_endian_exceptions(void)
296 /* mflags = 0: big endian exceptions */
297 return plpar_set_mode(0, H_SET_MODE_RESOURCE_LE, 0, 0);
301 * Take exceptions in little endian mode on this partition
303 * Note: this call has a partition wide scope and can take a while to complete.
304 * If it returns H_LONG_BUSY_* it should be retried periodically until it
307 static inline long enable_little_endian_exceptions(void)
309 /* mflags = 1: little endian exceptions */
310 return plpar_set_mode(1, H_SET_MODE_RESOURCE_LE, 0, 0);
313 static inline long plpar_set_ciabr(unsigned long ciabr)
315 return plpar_set_mode(0, H_SET_MODE_RESOURCE_SET_CIABR, ciabr, 0);
318 static inline long plpar_set_watchpoint0(unsigned long dawr0, unsigned long dawrx0)
320 return plpar_set_mode(0, H_SET_MODE_RESOURCE_SET_DAWR0, dawr0, dawrx0);
323 static inline long plpar_set_watchpoint1(unsigned long dawr1, unsigned long dawrx1)
325 return plpar_set_mode(0, H_SET_MODE_RESOURCE_SET_DAWR1, dawr1, dawrx1);
328 static inline long plpar_signal_sys_reset(long cpu)
330 return plpar_hcall_norets(H_SIGNAL_SYS_RESET, cpu);
333 static inline long plpar_get_cpu_characteristics(struct h_cpu_char_result *p)
335 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
338 rc = plpar_hcall(H_GET_CPU_CHARACTERISTICS, retbuf);
339 if (rc == H_SUCCESS) {
340 p->character = retbuf[0];
341 p->behaviour = retbuf[1];
347 static inline long plpar_guest_create(unsigned long flags, unsigned long *guest_id)
349 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
355 rc = plpar_hcall(H_GUEST_CREATE, retbuf, flags, token);
357 *guest_id = retbuf[0];
364 if (H_IS_LONG_BUSY(rc)) {
366 msleep(get_longbusy_msecs(rc));
370 } while (rc == H_BUSY);
375 static inline long plpar_guest_create_vcpu(unsigned long flags,
376 unsigned long guest_id,
377 unsigned long vcpu_id)
382 rc = plpar_hcall_norets(H_GUEST_CREATE_VCPU, 0, guest_id, vcpu_id);
387 if (H_IS_LONG_BUSY(rc)) {
388 msleep(get_longbusy_msecs(rc));
392 } while (rc == H_BUSY);
397 static inline long plpar_guest_set_state(unsigned long flags,
398 unsigned long guest_id,
399 unsigned long vcpu_id,
400 unsigned long data_buffer,
401 unsigned long data_size,
402 unsigned long *failed_index)
404 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
408 rc = plpar_hcall(H_GUEST_SET_STATE, retbuf, flags, guest_id,
409 vcpu_id, data_buffer, data_size);
416 if (H_IS_LONG_BUSY(rc)) {
417 mdelay(get_longbusy_msecs(rc));
421 if (rc == H_INVALID_ELEMENT_ID)
422 *failed_index = retbuf[0];
423 else if (rc == H_INVALID_ELEMENT_SIZE)
424 *failed_index = retbuf[0];
425 else if (rc == H_INVALID_ELEMENT_VALUE)
426 *failed_index = retbuf[0];
434 static inline long plpar_guest_get_state(unsigned long flags,
435 unsigned long guest_id,
436 unsigned long vcpu_id,
437 unsigned long data_buffer,
438 unsigned long data_size,
439 unsigned long *failed_index)
441 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
445 rc = plpar_hcall(H_GUEST_GET_STATE, retbuf, flags, guest_id,
446 vcpu_id, data_buffer, data_size);
453 if (H_IS_LONG_BUSY(rc)) {
454 mdelay(get_longbusy_msecs(rc));
458 if (rc == H_INVALID_ELEMENT_ID)
459 *failed_index = retbuf[0];
460 else if (rc == H_INVALID_ELEMENT_SIZE)
461 *failed_index = retbuf[0];
462 else if (rc == H_INVALID_ELEMENT_VALUE)
463 *failed_index = retbuf[0];
471 static inline long plpar_guest_run_vcpu(unsigned long flags, unsigned long guest_id,
472 unsigned long vcpu_id, int *trap,
473 unsigned long *failed_index)
475 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
478 rc = plpar_hcall(H_GUEST_RUN_VCPU, retbuf, flags, guest_id, vcpu_id);
481 else if (rc == H_INVALID_ELEMENT_ID)
482 *failed_index = retbuf[0];
483 else if (rc == H_INVALID_ELEMENT_SIZE)
484 *failed_index = retbuf[0];
485 else if (rc == H_INVALID_ELEMENT_VALUE)
486 *failed_index = retbuf[0];
491 static inline long plpar_guest_delete(unsigned long flags, u64 guest_id)
496 rc = plpar_hcall_norets(H_GUEST_DELETE, flags, guest_id);
500 if (H_IS_LONG_BUSY(rc)) {
501 msleep(get_longbusy_msecs(rc));
505 } while (rc == H_BUSY);
510 static inline long plpar_guest_set_capabilities(unsigned long flags,
511 unsigned long capabilities)
513 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
517 rc = plpar_hcall(H_GUEST_SET_CAPABILITIES, retbuf, flags, capabilities);
521 if (H_IS_LONG_BUSY(rc)) {
522 msleep(get_longbusy_msecs(rc));
525 } while (rc == H_BUSY);
530 static inline long plpar_guest_get_capabilities(unsigned long flags,
531 unsigned long *capabilities)
533 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
537 rc = plpar_hcall(H_GUEST_GET_CAPABILITIES, retbuf, flags);
541 if (H_IS_LONG_BUSY(rc)) {
542 msleep(get_longbusy_msecs(rc));
545 } while (rc == H_BUSY);
548 *capabilities = retbuf[0];
554 * Wrapper to H_RPT_INVALIDATE hcall that handles return values appropriately
556 * - Returns H_SUCCESS on success
557 * - For H_BUSY return value, we retry the hcall.
558 * - For any other hcall failures, attempt a full flush once before
559 * resorting to BUG().
561 * Note: This hcall is expected to fail only very rarely. The correct
562 * error recovery of killing the process/guest will be eventually
565 static inline long pseries_rpt_invalidate(u64 pid, u64 target, u64 type,
566 u64 page_sizes, u64 start, u64 end)
572 rc = plpar_hcall_norets(H_RPT_INVALIDATE, pid, target, type,
573 page_sizes, start, end);
577 } else if (rc == H_SUCCESS)
580 /* Flush request failed, try with a full flush once */
581 if (type & H_RPTI_TYPE_NESTED)
582 all = H_RPTI_TYPE_NESTED | H_RPTI_TYPE_NESTED_ALL;
584 all = H_RPTI_TYPE_ALL;
586 rc = plpar_hcall_norets(H_RPT_INVALIDATE, pid, target,
587 all, page_sizes, 0, -1UL);
591 } else if (rc == H_SUCCESS)
598 #else /* !CONFIG_PPC_PSERIES */
600 static inline long plpar_set_ciabr(unsigned long ciabr)
605 static inline long plpar_pte_read_4(unsigned long flags, unsigned long ptex,
611 static inline long pseries_rpt_invalidate(u64 pid, u64 target, u64 type,
612 u64 page_sizes, u64 start, u64 end)
617 static inline long plpar_guest_create_vcpu(unsigned long flags,
618 unsigned long guest_id,
619 unsigned long vcpu_id)
624 static inline long plpar_guest_get_state(unsigned long flags,
625 unsigned long guest_id,
626 unsigned long vcpu_id,
627 unsigned long data_buffer,
628 unsigned long data_size,
629 unsigned long *failed_index)
634 static inline long plpar_guest_set_state(unsigned long flags,
635 unsigned long guest_id,
636 unsigned long vcpu_id,
637 unsigned long data_buffer,
638 unsigned long data_size,
639 unsigned long *failed_index)
644 static inline long plpar_guest_run_vcpu(unsigned long flags, unsigned long guest_id,
645 unsigned long vcpu_id, int *trap,
646 unsigned long *failed_index)
651 static inline long plpar_guest_create(unsigned long flags, unsigned long *guest_id)
656 static inline long plpar_guest_delete(unsigned long flags, u64 guest_id)
661 static inline long plpar_guest_get_capabilities(unsigned long flags,
662 unsigned long *capabilities)
667 static inline long plpar_guest_set_capabilities(unsigned long flags,
668 unsigned long capabilities)
673 #endif /* CONFIG_PPC_PSERIES */
675 #endif /* _ASM_POWERPC_PLPAR_WRAPPERS_H */