KVM
Classes | Macros | Functions | Variables
posted_intr.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  pi_desc
 

Macros

#define POSTED_INTR_ON   0
 
#define POSTED_INTR_SN   1
 
#define PID_TABLE_ENTRY_VALID   1
 

Functions

struct pi_desc __aligned (64)
 
static bool pi_test_and_set_on (struct pi_desc *pi_desc)
 
static bool pi_test_and_clear_on (struct pi_desc *pi_desc)
 
static bool pi_test_and_clear_sn (struct pi_desc *pi_desc)
 
static bool pi_test_and_set_pir (int vector, struct pi_desc *pi_desc)
 
static bool pi_is_pir_empty (struct pi_desc *pi_desc)
 
static void pi_set_sn (struct pi_desc *pi_desc)
 
static void pi_set_on (struct pi_desc *pi_desc)
 
static void pi_clear_on (struct pi_desc *pi_desc)
 
static void pi_clear_sn (struct pi_desc *pi_desc)
 
static bool pi_test_on (struct pi_desc *pi_desc)
 
static bool pi_test_sn (struct pi_desc *pi_desc)
 
void vmx_vcpu_pi_load (struct kvm_vcpu *vcpu, int cpu)
 
void vmx_vcpu_pi_put (struct kvm_vcpu *vcpu)
 
void pi_wakeup_handler (void)
 
void __init pi_init_cpu (int cpu)
 
bool pi_has_pending_interrupt (struct kvm_vcpu *vcpu)
 
int vmx_pi_update_irte (struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, bool set)
 
void vmx_pi_start_assignment (struct kvm *kvm)
 

Variables

u32 pir [8]
 
union {
   struct {
      u16   on: 1
 
      u16   sn: 1
 
      u16   rsvd_1: 14
 
      u8   nv
 
      u8   rsvd_2
 
      u32   ndst
 
   } 
 
   u64   control
 
}; 
 
u32 rsvd [6]
 

Macro Definition Documentation

◆ PID_TABLE_ENTRY_VALID

#define PID_TABLE_ENTRY_VALID   1

Definition at line 8 of file posted_intr.h.

◆ POSTED_INTR_ON

#define POSTED_INTR_ON   0

Definition at line 5 of file posted_intr.h.

◆ POSTED_INTR_SN

#define POSTED_INTR_SN   1

Definition at line 6 of file posted_intr.h.

Function Documentation

◆ __aligned()

struct pi_desc __aligned ( 64  )

◆ pi_clear_on()

static void pi_clear_on ( struct pi_desc pi_desc)
inlinestatic

Definition at line 73 of file posted_intr.h.

74 {
75  clear_bit(POSTED_INTR_ON,
76  (unsigned long *)&pi_desc->control);
77 }
#define POSTED_INTR_ON
Definition: posted_intr.h:5
u64 control
Definition: posted_intr.h:28
Here is the caller graph for this function:

◆ pi_clear_sn()

static void pi_clear_sn ( struct pi_desc pi_desc)
inlinestatic

Definition at line 79 of file posted_intr.h.

80 {
81  clear_bit(POSTED_INTR_SN,
82  (unsigned long *)&pi_desc->control);
83 }
#define POSTED_INTR_SN
Definition: posted_intr.h:6

◆ pi_has_pending_interrupt()

bool pi_has_pending_interrupt ( struct kvm_vcpu *  vcpu)

Definition at line 240 of file posted_intr.c.

241 {
242  struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
243 
244  return pi_test_on(pi_desc) ||
246 }
static struct pi_desc * vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
Definition: posted_intr.c:34
static bool pi_test_sn(struct pi_desc *pi_desc)
Definition: posted_intr.h:91
static bool pi_is_pir_empty(struct pi_desc *pi_desc)
Definition: posted_intr.h:56
static bool pi_test_on(struct pi_desc *pi_desc)
Definition: posted_intr.h:85
Here is the call graph for this function:

◆ pi_init_cpu()

void __init pi_init_cpu ( int  cpu)

Definition at line 234 of file posted_intr.c.

235 {
236  INIT_LIST_HEAD(&per_cpu(wakeup_vcpus_on_cpu, cpu));
237  raw_spin_lock_init(&per_cpu(wakeup_vcpus_on_cpu_lock, cpu));
238 }
Here is the caller graph for this function:

◆ pi_is_pir_empty()

static bool pi_is_pir_empty ( struct pi_desc pi_desc)
inlinestatic

Definition at line 56 of file posted_intr.h.

57 {
58  return bitmap_empty((unsigned long *)pi_desc->pir, NR_VECTORS);
59 }
u32 pir[8]
Definition: posted_intr.h:12
Here is the caller graph for this function:

◆ pi_set_on()

static void pi_set_on ( struct pi_desc pi_desc)
inlinestatic

Definition at line 67 of file posted_intr.h.

68 {
69  set_bit(POSTED_INTR_ON,
70  (unsigned long *)&pi_desc->control);
71 }
Here is the caller graph for this function:

◆ pi_set_sn()

static void pi_set_sn ( struct pi_desc pi_desc)
inlinestatic

Definition at line 61 of file posted_intr.h.

62 {
63  set_bit(POSTED_INTR_SN,
64  (unsigned long *)&pi_desc->control);
65 }
Here is the caller graph for this function:

◆ pi_test_and_clear_on()

static bool pi_test_and_clear_on ( struct pi_desc pi_desc)
inlinestatic

Definition at line 39 of file posted_intr.h.

40 {
41  return test_and_clear_bit(POSTED_INTR_ON,
42  (unsigned long *)&pi_desc->control);
43 }
Here is the caller graph for this function:

◆ pi_test_and_clear_sn()

static bool pi_test_and_clear_sn ( struct pi_desc pi_desc)
inlinestatic

Definition at line 45 of file posted_intr.h.

46 {
47  return test_and_clear_bit(POSTED_INTR_SN,
48  (unsigned long *)&pi_desc->control);
49 }
Here is the caller graph for this function:

◆ pi_test_and_set_on()

static bool pi_test_and_set_on ( struct pi_desc pi_desc)
inlinestatic

Definition at line 33 of file posted_intr.h.

34 {
35  return test_and_set_bit(POSTED_INTR_ON,
36  (unsigned long *)&pi_desc->control);
37 }
Here is the caller graph for this function:

◆ pi_test_and_set_pir()

static bool pi_test_and_set_pir ( int  vector,
struct pi_desc pi_desc 
)
inlinestatic

Definition at line 51 of file posted_intr.h.

52 {
53  return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
54 }
Here is the caller graph for this function:

◆ pi_test_on()

static bool pi_test_on ( struct pi_desc pi_desc)
inlinestatic

Definition at line 85 of file posted_intr.h.

86 {
87  return test_bit(POSTED_INTR_ON,
88  (unsigned long *)&pi_desc->control);
89 }
Here is the caller graph for this function:

◆ pi_test_sn()

static bool pi_test_sn ( struct pi_desc pi_desc)
inlinestatic

Definition at line 91 of file posted_intr.h.

92 {
93  return test_bit(POSTED_INTR_SN,
94  (unsigned long *)&pi_desc->control);
95 }
Here is the caller graph for this function:

◆ pi_wakeup_handler()

void pi_wakeup_handler ( void  )

Definition at line 218 of file posted_intr.c.

219 {
220  int cpu = smp_processor_id();
221  struct list_head *wakeup_list = &per_cpu(wakeup_vcpus_on_cpu, cpu);
222  raw_spinlock_t *spinlock = &per_cpu(wakeup_vcpus_on_cpu_lock, cpu);
223  struct vcpu_vmx *vmx;
224 
225  raw_spin_lock(spinlock);
226  list_for_each_entry(vmx, wakeup_list, pi_wakeup_list) {
227 
228  if (pi_test_on(&vmx->pi_desc))
229  kvm_vcpu_wake_up(&vmx->vcpu);
230  }
231  raw_spin_unlock(spinlock);
232 }
bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu)
Definition: kvm_main.c:3915
Definition: vmx.h:251
struct list_head pi_wakeup_list
Definition: vmx.h:326
struct kvm_vcpu vcpu
Definition: vmx.h:252
struct pi_desc pi_desc
Definition: vmx.h:323
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmx_pi_start_assignment()

void vmx_pi_start_assignment ( struct kvm *  kvm)

Definition at line 255 of file posted_intr.c.

256 {
257  if (!irq_remapping_cap(IRQ_POSTING_CAP))
258  return;
259 
260  kvm_make_all_cpus_request(kvm, KVM_REQ_UNBLOCK);
261 }
bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req)
Definition: kvm_main.c:340
Here is the call graph for this function:

◆ vmx_pi_update_irte()

int vmx_pi_update_irte ( struct kvm *  kvm,
unsigned int  host_irq,
uint32_t  guest_irq,
bool  set 
)

Definition at line 272 of file posted_intr.c.

274 {
275  struct kvm_kernel_irq_routing_entry *e;
276  struct kvm_irq_routing_table *irq_rt;
277  struct kvm_lapic_irq irq;
278  struct kvm_vcpu *vcpu;
279  struct vcpu_data vcpu_info;
280  int idx, ret = 0;
281 
282  if (!vmx_can_use_vtd_pi(kvm))
283  return 0;
284 
285  idx = srcu_read_lock(&kvm->irq_srcu);
286  irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
287  if (guest_irq >= irq_rt->nr_rt_entries ||
288  hlist_empty(&irq_rt->map[guest_irq])) {
289  pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
290  guest_irq, irq_rt->nr_rt_entries);
291  goto out;
292  }
293 
294  hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
295  if (e->type != KVM_IRQ_ROUTING_MSI)
296  continue;
297  /*
298  * VT-d PI cannot support posting multicast/broadcast
299  * interrupts to a vCPU, we still use interrupt remapping
300  * for these kind of interrupts.
301  *
302  * For lowest-priority interrupts, we only support
303  * those with single CPU as the destination, e.g. user
304  * configures the interrupts via /proc/irq or uses
305  * irqbalance to make the interrupts single-CPU.
306  *
307  * We will support full lowest-priority interrupt later.
308  *
309  * In addition, we can only inject generic interrupts using
310  * the PI mechanism, refuse to route others through it.
311  */
312 
313  kvm_set_msi_irq(kvm, e, &irq);
314  if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
315  !kvm_irq_is_postable(&irq)) {
316  /*
317  * Make sure the IRTE is in remapped mode if
318  * we don't handle it in posted mode.
319  */
320  ret = irq_set_vcpu_affinity(host_irq, NULL);
321  if (ret < 0) {
322  printk(KERN_INFO
323  "failed to back to remapped mode, irq: %u\n",
324  host_irq);
325  goto out;
326  }
327 
328  continue;
329  }
330 
331  vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
332  vcpu_info.vector = irq.vector;
333 
334  trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
335  vcpu_info.vector, vcpu_info.pi_desc_addr, set);
336 
337  if (set)
338  ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
339  else
340  ret = irq_set_vcpu_affinity(host_irq, NULL);
341 
342  if (ret < 0) {
343  printk(KERN_INFO "%s: failed to update PI IRTE\n",
344  __func__);
345  goto out;
346  }
347  }
348 
349  ret = 0;
350 out:
351  srcu_read_unlock(&kvm->irq_srcu, idx);
352  return ret;
353 }
void kvm_set_msi_irq(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e, struct kvm_lapic_irq *irq)
Definition: irq_comm.c:104
bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq, struct kvm_vcpu **dest_vcpu)
Definition: irq_comm.c:338
static bool vmx_can_use_vtd_pi(struct kvm *kvm)
Definition: posted_intr.c:135
Here is the call graph for this function:

◆ vmx_vcpu_pi_load()

void vmx_vcpu_pi_load ( struct kvm_vcpu *  vcpu,
int  cpu 
)

Definition at line 53 of file posted_intr.c.

54 {
55  struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
56  struct vcpu_vmx *vmx = to_vmx(vcpu);
57  struct pi_desc old, new;
58  unsigned long flags;
59  unsigned int dest;
60 
61  /*
62  * To simplify hot-plug and dynamic toggling of APICv, keep PI.NDST and
63  * PI.SN up-to-date even if there is no assigned device or if APICv is
64  * deactivated due to a dynamic inhibit bit, e.g. for Hyper-V's SyncIC.
65  */
66  if (!enable_apicv || !lapic_in_kernel(vcpu))
67  return;
68 
69  /*
70  * If the vCPU wasn't on the wakeup list and wasn't migrated, then the
71  * full update can be skipped as neither the vector nor the destination
72  * needs to be changed.
73  */
74  if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR && vcpu->cpu == cpu) {
75  /*
76  * Clear SN if it was set due to being preempted. Again, do
77  * this even if there is no assigned device for simplicity.
78  */
80  goto after_clear_sn;
81  return;
82  }
83 
84  local_irq_save(flags);
85 
86  /*
87  * If the vCPU was waiting for wakeup, remove the vCPU from the wakeup
88  * list of the _previous_ pCPU, which will not be the same as the
89  * current pCPU if the task was migrated.
90  */
91  if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR) {
92  raw_spin_lock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu));
93  list_del(&vmx->pi_wakeup_list);
94  raw_spin_unlock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu));
95  }
96 
97  dest = cpu_physical_id(cpu);
98  if (!x2apic_mode)
99  dest = (dest << 8) & 0xFF00;
100 
101  old.control = READ_ONCE(pi_desc->control);
102  do {
103  new.control = old.control;
104 
105  /*
106  * Clear SN (as above) and refresh the destination APIC ID to
107  * handle task migration (@cpu != vcpu->cpu).
108  */
109  new.ndst = dest;
110  new.sn = 0;
111 
112  /*
113  * Restore the notification vector; in the blocking case, the
114  * descriptor was modified on "put" to use the wakeup vector.
115  */
116  new.nv = POSTED_INTR_VECTOR;
117  } while (pi_try_set_control(pi_desc, &old.control, new.control));
118 
119  local_irq_restore(flags);
120 
121 after_clear_sn:
122 
123  /*
124  * Clear SN before reading the bitmap. The VT-d firmware
125  * writes the bitmap and reads SN atomically (5.2.3 in the
126  * spec), so it doesn't really have a memory barrier that
127  * pairs with this, but we cannot do that and we need one.
128  */
129  smp_mb__after_atomic();
130 
131  if (!pi_is_pir_empty(pi_desc))
133 }
static bool lapic_in_kernel(struct kvm_vcpu *vcpu)
Definition: lapic.h:186
static int pi_try_set_control(struct pi_desc *pi_desc, u64 *pold, u64 new)
Definition: posted_intr.c:39
static bool pi_test_and_clear_sn(struct pi_desc *pi_desc)
Definition: posted_intr.h:45
static void pi_set_on(struct pi_desc *pi_desc)
Definition: posted_intr.h:67
static __always_inline struct vcpu_vmx * to_vmx(struct kvm_vcpu *vcpu)
Definition: vmx.h:657
bool __read_mostly enable_apicv
Definition: x86.c:235
uint32_t flags
Definition: xen.c:1
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vmx_vcpu_pi_put()

void vmx_vcpu_pi_put ( struct kvm_vcpu *  vcpu)

Definition at line 196 of file posted_intr.c.

197 {
198  struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
199 
200  if (!vmx_needs_pi_wakeup(vcpu))
201  return;
202 
203  if (kvm_vcpu_is_blocking(vcpu) && !vmx_interrupt_blocked(vcpu))
205 
206  /*
207  * Set SN when the vCPU is preempted. Note, the vCPU can both be seen
208  * as blocking and preempted, e.g. if it's preempted between setting
209  * its wait state and manually scheduling out.
210  */
211  if (vcpu->preempted)
213 }
static bool vmx_needs_pi_wakeup(struct kvm_vcpu *vcpu)
Definition: posted_intr.c:183
static void pi_enable_wakeup_handler(struct kvm_vcpu *vcpu)
Definition: posted_intr.c:146
static void pi_set_sn(struct pi_desc *pi_desc)
Definition: posted_intr.h:61
bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu)
Definition: vmx.c:5050
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ 

union { ... }

◆ control

u64 control

Definition at line 16 of file posted_intr.h.

◆ ndst

u32 ndst

Definition at line 14 of file posted_intr.h.

◆ nv

u8 nv

Definition at line 10 of file posted_intr.h.

◆ on

u16 on

Definition at line 4 of file posted_intr.h.

◆ pir

u32 pir[8]

Definition at line 0 of file posted_intr.h.

◆ rsvd

u32 rsvd[6]

Definition at line 18 of file posted_intr.h.

◆ rsvd_1

u16 rsvd_1

Definition at line 8 of file posted_intr.h.

◆ rsvd_2

u8 rsvd_2

Definition at line 12 of file posted_intr.h.

◆ sn

u16 sn

Definition at line 6 of file posted_intr.h.