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 long cede_processor(void)
24 * We cannot call tracepoints inside RCU idle regions which
25 * means we must not trace H_CEDE.
27 return plpar_hcall_norets_notrace(H_CEDE);
30 static inline long vpa_call(unsigned long flags, unsigned long cpu,
33 flags = flags << H_VPA_FUNC_SHIFT;
35 return plpar_hcall_norets(H_REGISTER_VPA, flags, cpu, vpa);
38 static inline long unregister_vpa(unsigned long cpu)
40 return vpa_call(H_VPA_DEREG_VPA, cpu, 0);
43 static inline long register_vpa(unsigned long cpu, unsigned long vpa)
45 return vpa_call(H_VPA_REG_VPA, cpu, vpa);
48 static inline long unregister_slb_shadow(unsigned long cpu)
50 return vpa_call(H_VPA_DEREG_SLB, cpu, 0);
53 static inline long register_slb_shadow(unsigned long cpu, unsigned long vpa)
55 return vpa_call(H_VPA_REG_SLB, cpu, vpa);
58 static inline long unregister_dtl(unsigned long cpu)
60 return vpa_call(H_VPA_DEREG_DTL, cpu, 0);
63 static inline long register_dtl(unsigned long cpu, unsigned long vpa)
65 return vpa_call(H_VPA_REG_DTL, cpu, vpa);
68 extern void vpa_init(int cpu);
70 static inline long plpar_pte_enter(unsigned long flags,
71 unsigned long hpte_group, unsigned long hpte_v,
72 unsigned long hpte_r, unsigned long *slot)
75 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
77 rc = plpar_hcall(H_ENTER, retbuf, flags, hpte_group, hpte_v, hpte_r);
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)
89 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
91 rc = plpar_hcall(H_REMOVE, retbuf, flags, ptex, avpn);
93 *old_pteh_ret = retbuf[0];
94 *old_ptel_ret = retbuf[1];
99 /* plpar_pte_remove_raw can be called in real mode. It calls plpar_hcall_raw */
100 static inline long plpar_pte_remove_raw(unsigned long flags, unsigned long ptex,
101 unsigned long avpn, unsigned long *old_pteh_ret,
102 unsigned long *old_ptel_ret)
105 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
107 rc = plpar_hcall_raw(H_REMOVE, retbuf, flags, ptex, avpn);
109 *old_pteh_ret = retbuf[0];
110 *old_ptel_ret = retbuf[1];
115 static inline long plpar_pte_read(unsigned long flags, unsigned long ptex,
116 unsigned long *old_pteh_ret, unsigned long *old_ptel_ret)
119 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
121 rc = plpar_hcall(H_READ, retbuf, flags, ptex);
123 *old_pteh_ret = retbuf[0];
124 *old_ptel_ret = retbuf[1];
129 /* plpar_pte_read_raw can be called in real mode. It calls plpar_hcall_raw */
130 static inline long plpar_pte_read_raw(unsigned long flags, unsigned long ptex,
131 unsigned long *old_pteh_ret, unsigned long *old_ptel_ret)
134 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
136 rc = plpar_hcall_raw(H_READ, retbuf, flags, ptex);
138 *old_pteh_ret = retbuf[0];
139 *old_ptel_ret = retbuf[1];
145 * ptes must be 8*sizeof(unsigned long)
147 static inline long plpar_pte_read_4(unsigned long flags, unsigned long ptex,
152 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
154 rc = plpar_hcall9(H_READ, retbuf, flags | H_READ_4, ptex);
156 memcpy(ptes, retbuf, 8*sizeof(unsigned long));
162 * plpar_pte_read_4_raw can be called in real mode.
163 * ptes must be 8*sizeof(unsigned long)
165 static inline long plpar_pte_read_4_raw(unsigned long flags, unsigned long ptex,
170 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
172 rc = plpar_hcall9_raw(H_READ, retbuf, flags | H_READ_4, ptex);
174 memcpy(ptes, retbuf, 8*sizeof(unsigned long));
179 static inline long plpar_pte_protect(unsigned long flags, unsigned long ptex,
182 return plpar_hcall_norets(H_PROTECT, flags, ptex, avpn);
185 static inline long plpar_resize_hpt_prepare(unsigned long flags,
188 return plpar_hcall_norets(H_RESIZE_HPT_PREPARE, flags, shift);
191 static inline long plpar_resize_hpt_commit(unsigned long flags,
194 return plpar_hcall_norets(H_RESIZE_HPT_COMMIT, flags, shift);
197 static inline long plpar_tce_get(unsigned long liobn, unsigned long ioba,
198 unsigned long *tce_ret)
201 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
203 rc = plpar_hcall(H_GET_TCE, retbuf, liobn, ioba);
205 *tce_ret = retbuf[0];
210 static inline long plpar_tce_put(unsigned long liobn, unsigned long ioba,
211 unsigned long tceval)
213 return plpar_hcall_norets(H_PUT_TCE, liobn, ioba, tceval);
216 static inline long plpar_tce_put_indirect(unsigned long liobn,
217 unsigned long ioba, unsigned long page, unsigned long count)
219 return plpar_hcall_norets(H_PUT_TCE_INDIRECT, liobn, ioba, page, count);
222 static inline long plpar_tce_stuff(unsigned long liobn, unsigned long ioba,
223 unsigned long tceval, unsigned long count)
225 return plpar_hcall_norets(H_STUFF_TCE, liobn, ioba, tceval, count);
228 /* Set various resource mode parameters */
229 static inline long plpar_set_mode(unsigned long mflags, unsigned long resource,
230 unsigned long value1, unsigned long value2)
232 return plpar_hcall_norets(H_SET_MODE, mflags, resource, value1, value2);
236 * Enable relocation on exceptions on this partition
238 * Note: this call has a partition wide scope and can take a while to complete.
239 * If it returns H_LONG_BUSY_* it should be retried periodically until it
242 static inline long enable_reloc_on_exceptions(void)
244 /* mflags = 3: Exceptions at 0xC000000000004000 */
245 return plpar_set_mode(3, H_SET_MODE_RESOURCE_ADDR_TRANS_MODE, 0, 0);
249 * Disable relocation on exceptions on this partition
251 * Note: this call has a partition wide scope and can take a while to complete.
252 * If it returns H_LONG_BUSY_* it should be retried periodically until it
255 static inline long disable_reloc_on_exceptions(void) {
256 return plpar_set_mode(0, H_SET_MODE_RESOURCE_ADDR_TRANS_MODE, 0, 0);
260 * Take exceptions in big endian mode on this partition
262 * Note: this call has a partition wide scope and can take a while to complete.
263 * If it returns H_LONG_BUSY_* it should be retried periodically until it
266 static inline long enable_big_endian_exceptions(void)
268 /* mflags = 0: big endian exceptions */
269 return plpar_set_mode(0, H_SET_MODE_RESOURCE_LE, 0, 0);
273 * Take exceptions in little endian mode on this partition
275 * Note: this call has a partition wide scope and can take a while to complete.
276 * If it returns H_LONG_BUSY_* it should be retried periodically until it
279 static inline long enable_little_endian_exceptions(void)
281 /* mflags = 1: little endian exceptions */
282 return plpar_set_mode(1, H_SET_MODE_RESOURCE_LE, 0, 0);
285 static inline long plpar_set_ciabr(unsigned long ciabr)
287 return plpar_set_mode(0, H_SET_MODE_RESOURCE_SET_CIABR, ciabr, 0);
290 static inline long plpar_set_watchpoint0(unsigned long dawr0, unsigned long dawrx0)
292 return plpar_set_mode(0, H_SET_MODE_RESOURCE_SET_DAWR0, dawr0, dawrx0);
295 static inline long plpar_set_watchpoint1(unsigned long dawr1, unsigned long dawrx1)
297 return plpar_set_mode(0, H_SET_MODE_RESOURCE_SET_DAWR1, dawr1, dawrx1);
300 static inline long plpar_signal_sys_reset(long cpu)
302 return plpar_hcall_norets(H_SIGNAL_SYS_RESET, cpu);
305 static inline long plpar_get_cpu_characteristics(struct h_cpu_char_result *p)
307 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
310 rc = plpar_hcall(H_GET_CPU_CHARACTERISTICS, retbuf);
311 if (rc == H_SUCCESS) {
312 p->character = retbuf[0];
313 p->behaviour = retbuf[1];
319 static inline long plpar_guest_create(unsigned long flags, unsigned long *guest_id)
321 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
327 rc = plpar_hcall(H_GUEST_CREATE, retbuf, flags, token);
329 *guest_id = retbuf[0];
336 if (H_IS_LONG_BUSY(rc)) {
338 msleep(get_longbusy_msecs(rc));
342 } while (rc == H_BUSY);
347 static inline long plpar_guest_create_vcpu(unsigned long flags,
348 unsigned long guest_id,
349 unsigned long vcpu_id)
354 rc = plpar_hcall_norets(H_GUEST_CREATE_VCPU, 0, guest_id, vcpu_id);
359 if (H_IS_LONG_BUSY(rc)) {
360 msleep(get_longbusy_msecs(rc));
364 } while (rc == H_BUSY);
369 static inline long plpar_guest_set_state(unsigned long flags,
370 unsigned long guest_id,
371 unsigned long vcpu_id,
372 unsigned long data_buffer,
373 unsigned long data_size,
374 unsigned long *failed_index)
376 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
380 rc = plpar_hcall(H_GUEST_SET_STATE, retbuf, flags, guest_id,
381 vcpu_id, data_buffer, data_size);
388 if (H_IS_LONG_BUSY(rc)) {
389 mdelay(get_longbusy_msecs(rc));
393 if (rc == H_INVALID_ELEMENT_ID)
394 *failed_index = retbuf[0];
395 else if (rc == H_INVALID_ELEMENT_SIZE)
396 *failed_index = retbuf[0];
397 else if (rc == H_INVALID_ELEMENT_VALUE)
398 *failed_index = retbuf[0];
406 static inline long plpar_guest_get_state(unsigned long flags,
407 unsigned long guest_id,
408 unsigned long vcpu_id,
409 unsigned long data_buffer,
410 unsigned long data_size,
411 unsigned long *failed_index)
413 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
417 rc = plpar_hcall(H_GUEST_GET_STATE, retbuf, flags, guest_id,
418 vcpu_id, data_buffer, data_size);
425 if (H_IS_LONG_BUSY(rc)) {
426 mdelay(get_longbusy_msecs(rc));
430 if (rc == H_INVALID_ELEMENT_ID)
431 *failed_index = retbuf[0];
432 else if (rc == H_INVALID_ELEMENT_SIZE)
433 *failed_index = retbuf[0];
434 else if (rc == H_INVALID_ELEMENT_VALUE)
435 *failed_index = retbuf[0];
443 static inline long plpar_guest_run_vcpu(unsigned long flags, unsigned long guest_id,
444 unsigned long vcpu_id, int *trap,
445 unsigned long *failed_index)
447 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
450 rc = plpar_hcall(H_GUEST_RUN_VCPU, retbuf, flags, guest_id, vcpu_id);
453 else if (rc == H_INVALID_ELEMENT_ID)
454 *failed_index = retbuf[0];
455 else if (rc == H_INVALID_ELEMENT_SIZE)
456 *failed_index = retbuf[0];
457 else if (rc == H_INVALID_ELEMENT_VALUE)
458 *failed_index = retbuf[0];
463 static inline long plpar_guest_delete(unsigned long flags, u64 guest_id)
468 rc = plpar_hcall_norets(H_GUEST_DELETE, flags, guest_id);
472 if (H_IS_LONG_BUSY(rc)) {
473 msleep(get_longbusy_msecs(rc));
477 } while (rc == H_BUSY);
482 static inline long plpar_guest_set_capabilities(unsigned long flags,
483 unsigned long capabilities)
485 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
489 rc = plpar_hcall(H_GUEST_SET_CAPABILITIES, retbuf, flags, capabilities);
493 if (H_IS_LONG_BUSY(rc)) {
494 msleep(get_longbusy_msecs(rc));
497 } while (rc == H_BUSY);
502 static inline long plpar_guest_get_capabilities(unsigned long flags,
503 unsigned long *capabilities)
505 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
509 rc = plpar_hcall(H_GUEST_GET_CAPABILITIES, retbuf, flags);
513 if (H_IS_LONG_BUSY(rc)) {
514 msleep(get_longbusy_msecs(rc));
517 } while (rc == H_BUSY);
520 *capabilities = retbuf[0];
526 * Wrapper to H_RPT_INVALIDATE hcall that handles return values appropriately
528 * - Returns H_SUCCESS on success
529 * - For H_BUSY return value, we retry the hcall.
530 * - For any other hcall failures, attempt a full flush once before
531 * resorting to BUG().
533 * Note: This hcall is expected to fail only very rarely. The correct
534 * error recovery of killing the process/guest will be eventually
537 static inline long pseries_rpt_invalidate(u64 pid, u64 target, u64 type,
538 u64 page_sizes, u64 start, u64 end)
544 rc = plpar_hcall_norets(H_RPT_INVALIDATE, pid, target, type,
545 page_sizes, start, end);
549 } else if (rc == H_SUCCESS)
552 /* Flush request failed, try with a full flush once */
553 if (type & H_RPTI_TYPE_NESTED)
554 all = H_RPTI_TYPE_NESTED | H_RPTI_TYPE_NESTED_ALL;
556 all = H_RPTI_TYPE_ALL;
558 rc = plpar_hcall_norets(H_RPT_INVALIDATE, pid, target,
559 all, page_sizes, 0, -1UL);
563 } else if (rc == H_SUCCESS)
570 #else /* !CONFIG_PPC_PSERIES */
572 static inline long plpar_set_ciabr(unsigned long ciabr)
577 static inline long plpar_pte_read_4(unsigned long flags, unsigned long ptex,
583 static inline long pseries_rpt_invalidate(u64 pid, u64 target, u64 type,
584 u64 page_sizes, u64 start, u64 end)
589 static inline long plpar_guest_create_vcpu(unsigned long flags,
590 unsigned long guest_id,
591 unsigned long vcpu_id)
596 static inline long plpar_guest_get_state(unsigned long flags,
597 unsigned long guest_id,
598 unsigned long vcpu_id,
599 unsigned long data_buffer,
600 unsigned long data_size,
601 unsigned long *failed_index)
606 static inline long plpar_guest_set_state(unsigned long flags,
607 unsigned long guest_id,
608 unsigned long vcpu_id,
609 unsigned long data_buffer,
610 unsigned long data_size,
611 unsigned long *failed_index)
616 static inline long plpar_guest_run_vcpu(unsigned long flags, unsigned long guest_id,
617 unsigned long vcpu_id, int *trap,
618 unsigned long *failed_index)
623 static inline long plpar_guest_create(unsigned long flags, unsigned long *guest_id)
628 static inline long plpar_guest_delete(unsigned long flags, u64 guest_id)
633 static inline long plpar_guest_get_capabilities(unsigned long flags,
634 unsigned long *capabilities)
639 static inline long plpar_guest_set_capabilities(unsigned long flags,
640 unsigned long capabilities)
645 #endif /* CONFIG_PPC_PSERIES */
647 #endif /* _ASM_POWERPC_PLPAR_WRAPPERS_H */