blob: 5f7beebd36a6937fac8fd68cb4379b16990a4e18 [file] [log] [blame]
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001/*
2 * Copyright 2012 Michael Ellerman, IBM Corporation.
3 * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/kernel.h>
11#include <linux/kvm_host.h>
12#include <linux/err.h>
13#include <linux/gfp.h>
Paul Mackerras5975a2e2013-04-27 00:28:37 +000014#include <linux/anon_inodes.h>
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000015
16#include <asm/uaccess.h>
17#include <asm/kvm_book3s.h>
18#include <asm/kvm_ppc.h>
19#include <asm/hvcall.h>
20#include <asm/xics.h>
21#include <asm/debug.h>
Paul Mackerras7bfa9ad2013-08-06 14:13:44 +100022#include <asm/time.h>
Suresh Warrier34cb7952015-03-20 20:39:46 +110023#include <asm/spinlock.h>
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000024
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
27
28#include "book3s_xics.h"
29
30#if 1
31#define XICS_DBG(fmt...) do { } while (0)
32#else
33#define XICS_DBG(fmt...) trace_printk(fmt)
34#endif
35
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +000036#define ENABLE_REALMODE true
37#define DEBUG_REALMODE false
38
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000039/*
40 * LOCKING
41 * =======
42 *
Suresh Warrier34cb7952015-03-20 20:39:46 +110043 * Each ICS has a spin lock protecting the information about the IRQ
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000044 * sources and avoiding simultaneous deliveries if the same interrupt.
45 *
46 * ICP operations are done via a single compare & swap transaction
47 * (most ICP state fits in the union kvmppc_icp_state)
48 */
49
50/*
51 * TODO
52 * ====
53 *
54 * - To speed up resends, keep a bitmap of "resend" set bits in the
55 * ICS
56 *
57 * - Speed up server# -> ICP lookup (array ? hash table ?)
58 *
59 * - Make ICS lockless as well, or at least a per-interrupt lock or hashed
60 * locks array to improve scalability
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000061 */
62
63/* -- ICS routines -- */
64
65static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
66 u32 new_irq);
67
Paul Mackerras25a2150b2014-06-30 20:51:14 +100068/*
69 * Return value ideally indicates how the interrupt was handled, but no
70 * callers look at it (given that we don't implement KVM_IRQ_LINE_STATUS),
71 * so just return 0.
72 */
73static int ics_deliver_irq(struct kvmppc_xics *xics, u32 irq, u32 level)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000074{
75 struct ics_irq_state *state;
76 struct kvmppc_ics *ics;
77 u16 src;
78
79 XICS_DBG("ics deliver %#x (level: %d)\n", irq, level);
80
81 ics = kvmppc_xics_find_ics(xics, irq, &src);
82 if (!ics) {
83 XICS_DBG("ics_deliver_irq: IRQ 0x%06x not found !\n", irq);
84 return -EINVAL;
85 }
86 state = &ics->irq_state[src];
87 if (!state->exists)
88 return -EINVAL;
89
90 /*
91 * We set state->asserted locklessly. This should be fine as
92 * we are the only setter, thus concurrent access is undefined
93 * to begin with.
94 */
Paul Mackerras25a2150b2014-06-30 20:51:14 +100095 if (level == 1 || level == KVM_INTERRUPT_SET_LEVEL)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000096 state->asserted = 1;
Paul Mackerras25a2150b2014-06-30 20:51:14 +100097 else if (level == 0 || level == KVM_INTERRUPT_UNSET) {
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000098 state->asserted = 0;
99 return 0;
100 }
101
102 /* Attempt delivery */
103 icp_deliver_irq(xics, NULL, irq);
104
Paul Mackerras25a2150b2014-06-30 20:51:14 +1000105 return 0;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000106}
107
108static void ics_check_resend(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
109 struct kvmppc_icp *icp)
110{
111 int i;
112
Suresh Warrier34cb7952015-03-20 20:39:46 +1100113 unsigned long flags;
114
115 local_irq_save(flags);
116 arch_spin_lock(&ics->lock);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000117
118 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
119 struct ics_irq_state *state = &ics->irq_state[i];
120
121 if (!state->resend)
122 continue;
123
124 XICS_DBG("resend %#x prio %#x\n", state->number,
125 state->priority);
126
Suresh Warrier34cb7952015-03-20 20:39:46 +1100127 arch_spin_unlock(&ics->lock);
128 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000129 icp_deliver_irq(xics, icp, state->number);
Suresh Warrier34cb7952015-03-20 20:39:46 +1100130 local_irq_save(flags);
131 arch_spin_lock(&ics->lock);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000132 }
133
Suresh Warrier34cb7952015-03-20 20:39:46 +1100134 arch_spin_unlock(&ics->lock);
135 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000136}
137
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000138static bool write_xive(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
139 struct ics_irq_state *state,
140 u32 server, u32 priority, u32 saved_priority)
141{
142 bool deliver;
Suresh Warrier34cb7952015-03-20 20:39:46 +1100143 unsigned long flags;
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000144
Suresh Warrier34cb7952015-03-20 20:39:46 +1100145 local_irq_save(flags);
146 arch_spin_lock(&ics->lock);
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000147
148 state->server = server;
149 state->priority = priority;
150 state->saved_priority = saved_priority;
151 deliver = false;
152 if ((state->masked_pending || state->resend) && priority != MASKED) {
153 state->masked_pending = 0;
154 deliver = true;
155 }
156
Suresh Warrier34cb7952015-03-20 20:39:46 +1100157 arch_spin_unlock(&ics->lock);
158 local_irq_restore(flags);
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000159
160 return deliver;
161}
162
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000163int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server, u32 priority)
164{
165 struct kvmppc_xics *xics = kvm->arch.xics;
166 struct kvmppc_icp *icp;
167 struct kvmppc_ics *ics;
168 struct ics_irq_state *state;
169 u16 src;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000170
171 if (!xics)
172 return -ENODEV;
173
174 ics = kvmppc_xics_find_ics(xics, irq, &src);
175 if (!ics)
176 return -EINVAL;
177 state = &ics->irq_state[src];
178
179 icp = kvmppc_xics_find_server(kvm, server);
180 if (!icp)
181 return -EINVAL;
182
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000183 XICS_DBG("set_xive %#x server %#x prio %#x MP:%d RS:%d\n",
184 irq, server, priority,
185 state->masked_pending, state->resend);
186
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000187 if (write_xive(xics, ics, state, server, priority, priority))
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000188 icp_deliver_irq(xics, icp, irq);
189
190 return 0;
191}
192
193int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, u32 *priority)
194{
195 struct kvmppc_xics *xics = kvm->arch.xics;
196 struct kvmppc_ics *ics;
197 struct ics_irq_state *state;
198 u16 src;
Suresh Warrier34cb7952015-03-20 20:39:46 +1100199 unsigned long flags;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000200
201 if (!xics)
202 return -ENODEV;
203
204 ics = kvmppc_xics_find_ics(xics, irq, &src);
205 if (!ics)
206 return -EINVAL;
207 state = &ics->irq_state[src];
208
Suresh Warrier34cb7952015-03-20 20:39:46 +1100209 local_irq_save(flags);
210 arch_spin_lock(&ics->lock);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000211 *server = state->server;
212 *priority = state->priority;
Suresh Warrier34cb7952015-03-20 20:39:46 +1100213 arch_spin_unlock(&ics->lock);
214 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000215
216 return 0;
217}
218
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000219int kvmppc_xics_int_on(struct kvm *kvm, u32 irq)
220{
221 struct kvmppc_xics *xics = kvm->arch.xics;
222 struct kvmppc_icp *icp;
223 struct kvmppc_ics *ics;
224 struct ics_irq_state *state;
225 u16 src;
226
227 if (!xics)
228 return -ENODEV;
229
230 ics = kvmppc_xics_find_ics(xics, irq, &src);
231 if (!ics)
232 return -EINVAL;
233 state = &ics->irq_state[src];
234
235 icp = kvmppc_xics_find_server(kvm, state->server);
236 if (!icp)
237 return -EINVAL;
238
239 if (write_xive(xics, ics, state, state->server, state->saved_priority,
240 state->saved_priority))
241 icp_deliver_irq(xics, icp, irq);
242
243 return 0;
244}
245
246int kvmppc_xics_int_off(struct kvm *kvm, u32 irq)
247{
248 struct kvmppc_xics *xics = kvm->arch.xics;
249 struct kvmppc_ics *ics;
250 struct ics_irq_state *state;
251 u16 src;
252
253 if (!xics)
254 return -ENODEV;
255
256 ics = kvmppc_xics_find_ics(xics, irq, &src);
257 if (!ics)
258 return -EINVAL;
259 state = &ics->irq_state[src];
260
261 write_xive(xics, ics, state, state->server, MASKED, state->priority);
262
263 return 0;
264}
265
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000266/* -- ICP routines, including hcalls -- */
267
268static inline bool icp_try_update(struct kvmppc_icp *icp,
269 union kvmppc_icp_state old,
270 union kvmppc_icp_state new,
271 bool change_self)
272{
273 bool success;
274
275 /* Calculate new output value */
276 new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
277
278 /* Attempt atomic update */
279 success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
280 if (!success)
281 goto bail;
282
283 XICS_DBG("UPD [%04x] - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
284 icp->server_num,
285 old.cppr, old.mfrr, old.pending_pri, old.xisr,
286 old.need_resend, old.out_ee);
287 XICS_DBG("UPD - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
288 new.cppr, new.mfrr, new.pending_pri, new.xisr,
289 new.need_resend, new.out_ee);
290 /*
291 * Check for output state update
292 *
293 * Note that this is racy since another processor could be updating
294 * the state already. This is why we never clear the interrupt output
295 * here, we only ever set it. The clear only happens prior to doing
296 * an update and only by the processor itself. Currently we do it
297 * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
298 *
299 * We also do not try to figure out whether the EE state has changed,
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000300 * we unconditionally set it if the new state calls for it. The reason
301 * for that is that we opportunistically remove the pending interrupt
302 * flag when raising CPPR, so we need to set it back here if an
303 * interrupt is still pending.
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000304 */
305 if (new.out_ee) {
306 kvmppc_book3s_queue_irqprio(icp->vcpu,
307 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
308 if (!change_self)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000309 kvmppc_fast_vcpu_kick(icp->vcpu);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000310 }
311 bail:
312 return success;
313}
314
315static void icp_check_resend(struct kvmppc_xics *xics,
316 struct kvmppc_icp *icp)
317{
318 u32 icsid;
319
320 /* Order this load with the test for need_resend in the caller */
321 smp_rmb();
322 for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
323 struct kvmppc_ics *ics = xics->ics[icsid];
324
325 if (!test_and_clear_bit(icsid, icp->resend_map))
326 continue;
327 if (!ics)
328 continue;
329 ics_check_resend(xics, ics, icp);
330 }
331}
332
333static bool icp_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
334 u32 *reject)
335{
336 union kvmppc_icp_state old_state, new_state;
337 bool success;
338
339 XICS_DBG("try deliver %#x(P:%#x) to server %#x\n", irq, priority,
340 icp->server_num);
341
342 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100343 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000344
345 *reject = 0;
346
347 /* See if we can deliver */
348 success = new_state.cppr > priority &&
349 new_state.mfrr > priority &&
350 new_state.pending_pri > priority;
351
352 /*
353 * If we can, check for a rejection and perform the
354 * delivery
355 */
356 if (success) {
357 *reject = new_state.xisr;
358 new_state.xisr = irq;
359 new_state.pending_pri = priority;
360 } else {
361 /*
362 * If we failed to deliver we set need_resend
363 * so a subsequent CPPR state change causes us
364 * to try a new delivery.
365 */
366 new_state.need_resend = true;
367 }
368
369 } while (!icp_try_update(icp, old_state, new_state, false));
370
371 return success;
372}
373
374static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
375 u32 new_irq)
376{
377 struct ics_irq_state *state;
378 struct kvmppc_ics *ics;
379 u32 reject;
380 u16 src;
Suresh Warrier34cb7952015-03-20 20:39:46 +1100381 unsigned long flags;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000382
383 /*
384 * This is used both for initial delivery of an interrupt and
385 * for subsequent rejection.
386 *
387 * Rejection can be racy vs. resends. We have evaluated the
388 * rejection in an atomic ICP transaction which is now complete,
389 * so potentially the ICP can already accept the interrupt again.
390 *
391 * So we need to retry the delivery. Essentially the reject path
392 * boils down to a failed delivery. Always.
393 *
394 * Now the interrupt could also have moved to a different target,
395 * thus we may need to re-do the ICP lookup as well
396 */
397
398 again:
399 /* Get the ICS state and lock it */
400 ics = kvmppc_xics_find_ics(xics, new_irq, &src);
401 if (!ics) {
402 XICS_DBG("icp_deliver_irq: IRQ 0x%06x not found !\n", new_irq);
403 return;
404 }
405 state = &ics->irq_state[src];
406
407 /* Get a lock on the ICS */
Suresh Warrier34cb7952015-03-20 20:39:46 +1100408 local_irq_save(flags);
409 arch_spin_lock(&ics->lock);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000410
411 /* Get our server */
412 if (!icp || state->server != icp->server_num) {
413 icp = kvmppc_xics_find_server(xics->kvm, state->server);
414 if (!icp) {
415 pr_warn("icp_deliver_irq: IRQ 0x%06x server 0x%x not found !\n",
416 new_irq, state->server);
417 goto out;
418 }
419 }
420
421 /* Clear the resend bit of that interrupt */
422 state->resend = 0;
423
424 /*
425 * If masked, bail out
426 *
427 * Note: PAPR doesn't mention anything about masked pending
428 * when doing a resend, only when doing a delivery.
429 *
430 * However that would have the effect of losing a masked
431 * interrupt that was rejected and isn't consistent with
432 * the whole masked_pending business which is about not
433 * losing interrupts that occur while masked.
434 *
435 * I don't differenciate normal deliveries and resends, this
436 * implementation will differ from PAPR and not lose such
437 * interrupts.
438 */
439 if (state->priority == MASKED) {
440 XICS_DBG("irq %#x masked pending\n", new_irq);
441 state->masked_pending = 1;
442 goto out;
443 }
444
445 /*
446 * Try the delivery, this will set the need_resend flag
447 * in the ICP as part of the atomic transaction if the
448 * delivery is not possible.
449 *
450 * Note that if successful, the new delivery might have itself
451 * rejected an interrupt that was "delivered" before we took the
Suresh Warrier34cb7952015-03-20 20:39:46 +1100452 * ics spin lock.
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000453 *
454 * In this case we do the whole sequence all over again for the
455 * new guy. We cannot assume that the rejected interrupt is less
456 * favored than the new one, and thus doesn't need to be delivered,
457 * because by the time we exit icp_try_to_deliver() the target
458 * processor may well have alrady consumed & completed it, and thus
459 * the rejected interrupt might actually be already acceptable.
460 */
461 if (icp_try_to_deliver(icp, new_irq, state->priority, &reject)) {
462 /*
463 * Delivery was successful, did we reject somebody else ?
464 */
465 if (reject && reject != XICS_IPI) {
Suresh Warrier34cb7952015-03-20 20:39:46 +1100466 arch_spin_unlock(&ics->lock);
467 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000468 new_irq = reject;
469 goto again;
470 }
471 } else {
472 /*
473 * We failed to deliver the interrupt we need to set the
474 * resend map bit and mark the ICS state as needing a resend
475 */
476 set_bit(ics->icsid, icp->resend_map);
477 state->resend = 1;
478
479 /*
480 * If the need_resend flag got cleared in the ICP some time
481 * between icp_try_to_deliver() atomic update and now, then
482 * we know it might have missed the resend_map bit. So we
483 * retry
484 */
485 smp_mb();
486 if (!icp->state.need_resend) {
Suresh Warrier34cb7952015-03-20 20:39:46 +1100487 arch_spin_unlock(&ics->lock);
488 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000489 goto again;
490 }
491 }
492 out:
Suresh Warrier34cb7952015-03-20 20:39:46 +1100493 arch_spin_unlock(&ics->lock);
494 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000495}
496
497static void icp_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
498 u8 new_cppr)
499{
500 union kvmppc_icp_state old_state, new_state;
501 bool resend;
502
503 /*
504 * This handles several related states in one operation:
505 *
506 * ICP State: Down_CPPR
507 *
508 * Load CPPR with new value and if the XISR is 0
509 * then check for resends:
510 *
511 * ICP State: Resend
512 *
513 * If MFRR is more favored than CPPR, check for IPIs
514 * and notify ICS of a potential resend. This is done
515 * asynchronously (when used in real mode, we will have
516 * to exit here).
517 *
518 * We do not handle the complete Check_IPI as documented
519 * here. In the PAPR, this state will be used for both
520 * Set_MFRR and Down_CPPR. However, we know that we aren't
521 * changing the MFRR state here so we don't need to handle
522 * the case of an MFRR causing a reject of a pending irq,
523 * this will have been handled when the MFRR was set in the
524 * first place.
525 *
526 * Thus we don't have to handle rejects, only resends.
527 *
528 * When implementing real mode for HV KVM, resend will lead to
529 * a H_TOO_HARD return and the whole transaction will be handled
530 * in virtual mode.
531 */
532 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100533 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000534
535 /* Down_CPPR */
536 new_state.cppr = new_cppr;
537
538 /*
539 * Cut down Resend / Check_IPI / IPI
540 *
541 * The logic is that we cannot have a pending interrupt
542 * trumped by an IPI at this point (see above), so we
543 * know that either the pending interrupt is already an
544 * IPI (in which case we don't care to override it) or
545 * it's either more favored than us or non existent
546 */
547 if (new_state.mfrr < new_cppr &&
548 new_state.mfrr <= new_state.pending_pri) {
549 WARN_ON(new_state.xisr != XICS_IPI &&
550 new_state.xisr != 0);
551 new_state.pending_pri = new_state.mfrr;
552 new_state.xisr = XICS_IPI;
553 }
554
555 /* Latch/clear resend bit */
556 resend = new_state.need_resend;
557 new_state.need_resend = 0;
558
559 } while (!icp_try_update(icp, old_state, new_state, true));
560
561 /*
562 * Now handle resend checks. Those are asynchronous to the ICP
563 * state update in HW (ie bus transactions) so we can handle them
564 * separately here too
565 */
566 if (resend)
567 icp_check_resend(xics, icp);
568}
569
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000570static noinline unsigned long kvmppc_h_xirr(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000571{
572 union kvmppc_icp_state old_state, new_state;
573 struct kvmppc_icp *icp = vcpu->arch.icp;
574 u32 xirr;
575
576 /* First, remove EE from the processor */
577 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
578 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
579
580 /*
581 * ICP State: Accept_Interrupt
582 *
583 * Return the pending interrupt (if any) along with the
584 * current CPPR, then clear the XISR & set CPPR to the
585 * pending priority
586 */
587 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100588 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000589
590 xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
591 if (!old_state.xisr)
592 break;
593 new_state.cppr = new_state.pending_pri;
594 new_state.pending_pri = 0xff;
595 new_state.xisr = 0;
596
597 } while (!icp_try_update(icp, old_state, new_state, true));
598
599 XICS_DBG("h_xirr vcpu %d xirr %#x\n", vcpu->vcpu_id, xirr);
600
601 return xirr;
602}
603
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000604static noinline int kvmppc_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
605 unsigned long mfrr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000606{
607 union kvmppc_icp_state old_state, new_state;
608 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
609 struct kvmppc_icp *icp;
610 u32 reject;
611 bool resend;
612 bool local;
613
614 XICS_DBG("h_ipi vcpu %d to server %lu mfrr %#lx\n",
615 vcpu->vcpu_id, server, mfrr);
616
617 icp = vcpu->arch.icp;
618 local = icp->server_num == server;
619 if (!local) {
620 icp = kvmppc_xics_find_server(vcpu->kvm, server);
621 if (!icp)
622 return H_PARAMETER;
623 }
624
625 /*
626 * ICP state: Set_MFRR
627 *
628 * If the CPPR is more favored than the new MFRR, then
629 * nothing needs to be rejected as there can be no XISR to
630 * reject. If the MFRR is being made less favored then
631 * there might be a previously-rejected interrupt needing
632 * to be resent.
633 *
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000634 * ICP state: Check_IPI
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100635 *
636 * If the CPPR is less favored, then we might be replacing
637 * an interrupt, and thus need to possibly reject it.
638 *
639 * ICP State: IPI
640 *
641 * Besides rejecting any pending interrupts, we also
642 * update XISR and pending_pri to mark IPI as pending.
643 *
644 * PAPR does not describe this state, but if the MFRR is being
645 * made less favored than its earlier value, there might be
646 * a previously-rejected interrupt needing to be resent.
647 * Ideally, we would want to resend only if
648 * prio(pending_interrupt) < mfrr &&
649 * prio(pending_interrupt) < cppr
650 * where pending interrupt is the one that was rejected. But
651 * we don't have that state, so we simply trigger a resend
652 * whenever the MFRR is made less favored.
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000653 */
654 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100655 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000656
657 /* Set_MFRR */
658 new_state.mfrr = mfrr;
659
660 /* Check_IPI */
661 reject = 0;
662 resend = false;
663 if (mfrr < new_state.cppr) {
664 /* Reject a pending interrupt if not an IPI */
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100665 if (mfrr <= new_state.pending_pri) {
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000666 reject = new_state.xisr;
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100667 new_state.pending_pri = mfrr;
668 new_state.xisr = XICS_IPI;
669 }
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000670 }
671
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100672 if (mfrr > old_state.mfrr) {
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000673 resend = new_state.need_resend;
674 new_state.need_resend = 0;
675 }
676 } while (!icp_try_update(icp, old_state, new_state, local));
677
678 /* Handle reject */
679 if (reject && reject != XICS_IPI)
680 icp_deliver_irq(xics, icp, reject);
681
682 /* Handle resend */
683 if (resend)
684 icp_check_resend(xics, icp);
685
686 return H_SUCCESS;
687}
688
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000689static int kvmppc_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server)
690{
691 union kvmppc_icp_state state;
692 struct kvmppc_icp *icp;
693
694 icp = vcpu->arch.icp;
695 if (icp->server_num != server) {
696 icp = kvmppc_xics_find_server(vcpu->kvm, server);
697 if (!icp)
698 return H_PARAMETER;
699 }
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100700 state = READ_ONCE(icp->state);
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000701 kvmppc_set_gpr(vcpu, 4, ((u32)state.cppr << 24) | state.xisr);
702 kvmppc_set_gpr(vcpu, 5, state.mfrr);
703 return H_SUCCESS;
704}
705
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000706static noinline void kvmppc_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000707{
708 union kvmppc_icp_state old_state, new_state;
709 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
710 struct kvmppc_icp *icp = vcpu->arch.icp;
711 u32 reject;
712
713 XICS_DBG("h_cppr vcpu %d cppr %#lx\n", vcpu->vcpu_id, cppr);
714
715 /*
716 * ICP State: Set_CPPR
717 *
718 * We can safely compare the new value with the current
719 * value outside of the transaction as the CPPR is only
720 * ever changed by the processor on itself
721 */
722 if (cppr > icp->state.cppr)
723 icp_down_cppr(xics, icp, cppr);
724 else if (cppr == icp->state.cppr)
725 return;
726
727 /*
728 * ICP State: Up_CPPR
729 *
730 * The processor is raising its priority, this can result
731 * in a rejection of a pending interrupt:
732 *
733 * ICP State: Reject_Current
734 *
735 * We can remove EE from the current processor, the update
736 * transaction will set it again if needed
737 */
738 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
739 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
740
741 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100742 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000743
744 reject = 0;
745 new_state.cppr = cppr;
746
747 if (cppr <= new_state.pending_pri) {
748 reject = new_state.xisr;
749 new_state.xisr = 0;
750 new_state.pending_pri = 0xff;
751 }
752
753 } while (!icp_try_update(icp, old_state, new_state, true));
754
755 /*
756 * Check for rejects. They are handled by doing a new delivery
757 * attempt (see comments in icp_deliver_irq).
758 */
759 if (reject && reject != XICS_IPI)
760 icp_deliver_irq(xics, icp, reject);
761}
762
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000763static noinline int kvmppc_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000764{
765 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
766 struct kvmppc_icp *icp = vcpu->arch.icp;
767 struct kvmppc_ics *ics;
768 struct ics_irq_state *state;
769 u32 irq = xirr & 0x00ffffff;
770 u16 src;
771
772 XICS_DBG("h_eoi vcpu %d eoi %#lx\n", vcpu->vcpu_id, xirr);
773
774 /*
775 * ICP State: EOI
776 *
777 * Note: If EOI is incorrectly used by SW to lower the CPPR
778 * value (ie more favored), we do not check for rejection of
779 * a pending interrupt, this is a SW error and PAPR sepcifies
780 * that we don't have to deal with it.
781 *
782 * The sending of an EOI to the ICS is handled after the
783 * CPPR update
784 *
785 * ICP State: Down_CPPR which we handle
786 * in a separate function as it's shared with H_CPPR.
787 */
788 icp_down_cppr(xics, icp, xirr >> 24);
789
790 /* IPIs have no EOI */
791 if (irq == XICS_IPI)
792 return H_SUCCESS;
793 /*
794 * EOI handling: If the interrupt is still asserted, we need to
795 * resend it. We can take a lockless "peek" at the ICS state here.
796 *
797 * "Message" interrupts will never have "asserted" set
798 */
799 ics = kvmppc_xics_find_ics(xics, irq, &src);
800 if (!ics) {
801 XICS_DBG("h_eoi: IRQ 0x%06x not found !\n", irq);
802 return H_PARAMETER;
803 }
804 state = &ics->irq_state[src];
805
806 /* Still asserted, resend it */
807 if (state->asserted)
808 icp_deliver_irq(xics, icp, irq);
809
Paul Mackerras25a2150b2014-06-30 20:51:14 +1000810 kvm_notify_acked_irq(vcpu->kvm, 0, irq);
811
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000812 return H_SUCCESS;
813}
814
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000815static noinline int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
816{
817 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
818 struct kvmppc_icp *icp = vcpu->arch.icp;
819
820 XICS_DBG("XICS_RM: H_%x completing, act: %x state: %lx tgt: %p\n",
821 hcall, icp->rm_action, icp->rm_dbgstate.raw, icp->rm_dbgtgt);
822
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100823 if (icp->rm_action & XICS_RM_KICK_VCPU) {
824 icp->n_rm_kick_vcpu++;
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000825 kvmppc_fast_vcpu_kick(icp->rm_kick_target);
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100826 }
827 if (icp->rm_action & XICS_RM_CHECK_RESEND) {
828 icp->n_rm_check_resend++;
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100829 icp_check_resend(xics, icp->rm_resend_icp);
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100830 }
831 if (icp->rm_action & XICS_RM_REJECT) {
832 icp->n_rm_reject++;
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000833 icp_deliver_irq(xics, icp, icp->rm_reject);
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100834 }
835 if (icp->rm_action & XICS_RM_NOTIFY_EOI) {
836 icp->n_rm_notify_eoi++;
Paul Mackerras25a2150b2014-06-30 20:51:14 +1000837 kvm_notify_acked_irq(vcpu->kvm, 0, icp->rm_eoied_irq);
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100838 }
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000839
840 icp->rm_action = 0;
841
842 return H_SUCCESS;
843}
844
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000845int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 req)
846{
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000847 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000848 unsigned long res;
849 int rc = H_SUCCESS;
850
851 /* Check if we have an ICP */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000852 if (!xics || !vcpu->arch.icp)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000853 return H_HARDWARE;
854
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000855 /* These requests don't have real-mode implementations at present */
856 switch (req) {
857 case H_XIRR_X:
858 res = kvmppc_h_xirr(vcpu);
859 kvmppc_set_gpr(vcpu, 4, res);
860 kvmppc_set_gpr(vcpu, 5, get_tb());
861 return rc;
862 case H_IPOLL:
863 rc = kvmppc_h_ipoll(vcpu, kvmppc_get_gpr(vcpu, 4));
864 return rc;
865 }
866
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000867 /* Check for real mode returning too hard */
Aneesh Kumar K.Va78b55d2013-10-07 22:18:02 +0530868 if (xics->real_mode && is_kvmppc_hv_enabled(vcpu->kvm))
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000869 return kvmppc_xics_rm_complete(vcpu, req);
870
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000871 switch (req) {
872 case H_XIRR:
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000873 res = kvmppc_h_xirr(vcpu);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000874 kvmppc_set_gpr(vcpu, 4, res);
875 break;
876 case H_CPPR:
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000877 kvmppc_h_cppr(vcpu, kvmppc_get_gpr(vcpu, 4));
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000878 break;
879 case H_EOI:
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000880 rc = kvmppc_h_eoi(vcpu, kvmppc_get_gpr(vcpu, 4));
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000881 break;
882 case H_IPI:
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000883 rc = kvmppc_h_ipi(vcpu, kvmppc_get_gpr(vcpu, 4),
884 kvmppc_get_gpr(vcpu, 5));
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000885 break;
886 }
887
888 return rc;
889}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530890EXPORT_SYMBOL_GPL(kvmppc_xics_hcall);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000891
892
893/* -- Initialisation code etc. -- */
894
895static int xics_debug_show(struct seq_file *m, void *private)
896{
897 struct kvmppc_xics *xics = m->private;
898 struct kvm *kvm = xics->kvm;
899 struct kvm_vcpu *vcpu;
900 int icsid, i;
Suresh Warrier34cb7952015-03-20 20:39:46 +1100901 unsigned long flags;
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100902 unsigned long t_rm_kick_vcpu, t_rm_check_resend;
903 unsigned long t_rm_reject, t_rm_notify_eoi;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000904
905 if (!kvm)
906 return 0;
907
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100908 t_rm_kick_vcpu = 0;
909 t_rm_notify_eoi = 0;
910 t_rm_check_resend = 0;
911 t_rm_reject = 0;
912
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000913 seq_printf(m, "=========\nICP state\n=========\n");
914
915 kvm_for_each_vcpu(i, vcpu, kvm) {
916 struct kvmppc_icp *icp = vcpu->arch.icp;
917 union kvmppc_icp_state state;
918
919 if (!icp)
920 continue;
921
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100922 state.raw = READ_ONCE(icp->state.raw);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000923 seq_printf(m, "cpu server %#lx XIRR:%#x PPRI:%#x CPPR:%#x MFRR:%#x OUT:%d NR:%d\n",
924 icp->server_num, state.xisr,
925 state.pending_pri, state.cppr, state.mfrr,
926 state.out_ee, state.need_resend);
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100927 t_rm_kick_vcpu += icp->n_rm_kick_vcpu;
928 t_rm_notify_eoi += icp->n_rm_notify_eoi;
929 t_rm_check_resend += icp->n_rm_check_resend;
930 t_rm_reject += icp->n_rm_reject;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000931 }
932
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100933 seq_puts(m, "ICP Guest Real Mode exit totals: ");
934 seq_printf(m, "\tkick_vcpu=%lu check_resend=%lu reject=%lu notify_eoi=%lu\n",
935 t_rm_kick_vcpu, t_rm_check_resend,
936 t_rm_reject, t_rm_notify_eoi);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000937 for (icsid = 0; icsid <= KVMPPC_XICS_MAX_ICS_ID; icsid++) {
938 struct kvmppc_ics *ics = xics->ics[icsid];
939
940 if (!ics)
941 continue;
942
943 seq_printf(m, "=========\nICS state for ICS 0x%x\n=========\n",
944 icsid);
945
Suresh Warrier34cb7952015-03-20 20:39:46 +1100946 local_irq_save(flags);
947 arch_spin_lock(&ics->lock);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000948
949 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
950 struct ics_irq_state *irq = &ics->irq_state[i];
951
952 seq_printf(m, "irq 0x%06x: server %#x prio %#x save prio %#x asserted %d resend %d masked pending %d\n",
953 irq->number, irq->server, irq->priority,
954 irq->saved_priority, irq->asserted,
955 irq->resend, irq->masked_pending);
956
957 }
Suresh Warrier34cb7952015-03-20 20:39:46 +1100958 arch_spin_unlock(&ics->lock);
959 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000960 }
961 return 0;
962}
963
964static int xics_debug_open(struct inode *inode, struct file *file)
965{
966 return single_open(file, xics_debug_show, inode->i_private);
967}
968
969static const struct file_operations xics_debug_fops = {
970 .open = xics_debug_open,
971 .read = seq_read,
972 .llseek = seq_lseek,
973 .release = single_release,
974};
975
976static void xics_debugfs_init(struct kvmppc_xics *xics)
977{
978 char *name;
979
980 name = kasprintf(GFP_KERNEL, "kvm-xics-%p", xics);
981 if (!name) {
982 pr_err("%s: no memory for name\n", __func__);
983 return;
984 }
985
986 xics->dentry = debugfs_create_file(name, S_IRUGO, powerpc_debugfs_root,
987 xics, &xics_debug_fops);
988
989 pr_debug("%s: created %s\n", __func__, name);
990 kfree(name);
991}
992
Paul Mackerras5975a2e2013-04-27 00:28:37 +0000993static struct kvmppc_ics *kvmppc_xics_create_ics(struct kvm *kvm,
994 struct kvmppc_xics *xics, int irq)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000995{
996 struct kvmppc_ics *ics;
997 int i, icsid;
998
999 icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
1000
1001 mutex_lock(&kvm->lock);
1002
1003 /* ICS already exists - somebody else got here first */
1004 if (xics->ics[icsid])
1005 goto out;
1006
1007 /* Create the ICS */
1008 ics = kzalloc(sizeof(struct kvmppc_ics), GFP_KERNEL);
1009 if (!ics)
1010 goto out;
1011
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001012 ics->icsid = icsid;
1013
1014 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
1015 ics->irq_state[i].number = (icsid << KVMPPC_XICS_ICS_SHIFT) | i;
1016 ics->irq_state[i].priority = MASKED;
1017 ics->irq_state[i].saved_priority = MASKED;
1018 }
1019 smp_wmb();
1020 xics->ics[icsid] = ics;
1021
1022 if (icsid > xics->max_icsid)
1023 xics->max_icsid = icsid;
1024
1025 out:
1026 mutex_unlock(&kvm->lock);
1027 return xics->ics[icsid];
1028}
1029
1030int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server_num)
1031{
1032 struct kvmppc_icp *icp;
1033
1034 if (!vcpu->kvm->arch.xics)
1035 return -ENODEV;
1036
1037 if (kvmppc_xics_find_server(vcpu->kvm, server_num))
1038 return -EEXIST;
1039
1040 icp = kzalloc(sizeof(struct kvmppc_icp), GFP_KERNEL);
1041 if (!icp)
1042 return -ENOMEM;
1043
1044 icp->vcpu = vcpu;
1045 icp->server_num = server_num;
1046 icp->state.mfrr = MASKED;
1047 icp->state.pending_pri = MASKED;
1048 vcpu->arch.icp = icp;
1049
1050 XICS_DBG("created server for vcpu %d\n", vcpu->vcpu_id);
1051
1052 return 0;
1053}
1054
Paul Mackerras8b786452013-04-17 20:32:26 +00001055u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu)
1056{
1057 struct kvmppc_icp *icp = vcpu->arch.icp;
1058 union kvmppc_icp_state state;
1059
1060 if (!icp)
1061 return 0;
1062 state = icp->state;
1063 return ((u64)state.cppr << KVM_REG_PPC_ICP_CPPR_SHIFT) |
1064 ((u64)state.xisr << KVM_REG_PPC_ICP_XISR_SHIFT) |
1065 ((u64)state.mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT) |
1066 ((u64)state.pending_pri << KVM_REG_PPC_ICP_PPRI_SHIFT);
1067}
1068
1069int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
1070{
1071 struct kvmppc_icp *icp = vcpu->arch.icp;
1072 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
1073 union kvmppc_icp_state old_state, new_state;
1074 struct kvmppc_ics *ics;
1075 u8 cppr, mfrr, pending_pri;
1076 u32 xisr;
1077 u16 src;
1078 bool resend;
1079
1080 if (!icp || !xics)
1081 return -ENOENT;
1082
1083 cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT;
1084 xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) &
1085 KVM_REG_PPC_ICP_XISR_MASK;
1086 mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT;
1087 pending_pri = icpval >> KVM_REG_PPC_ICP_PPRI_SHIFT;
1088
1089 /* Require the new state to be internally consistent */
1090 if (xisr == 0) {
1091 if (pending_pri != 0xff)
1092 return -EINVAL;
1093 } else if (xisr == XICS_IPI) {
1094 if (pending_pri != mfrr || pending_pri >= cppr)
1095 return -EINVAL;
1096 } else {
1097 if (pending_pri >= mfrr || pending_pri >= cppr)
1098 return -EINVAL;
1099 ics = kvmppc_xics_find_ics(xics, xisr, &src);
1100 if (!ics)
1101 return -EINVAL;
1102 }
1103
1104 new_state.raw = 0;
1105 new_state.cppr = cppr;
1106 new_state.xisr = xisr;
1107 new_state.mfrr = mfrr;
1108 new_state.pending_pri = pending_pri;
1109
1110 /*
1111 * Deassert the CPU interrupt request.
1112 * icp_try_update will reassert it if necessary.
1113 */
1114 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
1115 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
1116
1117 /*
1118 * Note that if we displace an interrupt from old_state.xisr,
1119 * we don't mark it as rejected. We expect userspace to set
1120 * the state of the interrupt sources to be consistent with
1121 * the ICP states (either before or afterwards, which doesn't
1122 * matter). We do handle resends due to CPPR becoming less
1123 * favoured because that is necessary to end up with a
1124 * consistent state in the situation where userspace restores
1125 * the ICS states before the ICP states.
1126 */
1127 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +01001128 old_state = READ_ONCE(icp->state);
Paul Mackerras8b786452013-04-17 20:32:26 +00001129
1130 if (new_state.mfrr <= old_state.mfrr) {
1131 resend = false;
1132 new_state.need_resend = old_state.need_resend;
1133 } else {
1134 resend = old_state.need_resend;
1135 new_state.need_resend = 0;
1136 }
1137 } while (!icp_try_update(icp, old_state, new_state, false));
1138
1139 if (resend)
1140 icp_check_resend(xics, icp);
1141
1142 return 0;
1143}
1144
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001145static int xics_get_source(struct kvmppc_xics *xics, long irq, u64 addr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001146{
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001147 int ret;
1148 struct kvmppc_ics *ics;
1149 struct ics_irq_state *irqp;
1150 u64 __user *ubufp = (u64 __user *) addr;
1151 u16 idx;
1152 u64 val, prio;
Suresh Warrier34cb7952015-03-20 20:39:46 +11001153 unsigned long flags;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001154
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001155 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1156 if (!ics)
1157 return -ENOENT;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001158
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001159 irqp = &ics->irq_state[idx];
Suresh Warrier34cb7952015-03-20 20:39:46 +11001160 local_irq_save(flags);
1161 arch_spin_lock(&ics->lock);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001162 ret = -ENOENT;
1163 if (irqp->exists) {
1164 val = irqp->server;
1165 prio = irqp->priority;
1166 if (prio == MASKED) {
1167 val |= KVM_XICS_MASKED;
1168 prio = irqp->saved_priority;
1169 }
1170 val |= prio << KVM_XICS_PRIORITY_SHIFT;
1171 if (irqp->asserted)
1172 val |= KVM_XICS_LEVEL_SENSITIVE | KVM_XICS_PENDING;
1173 else if (irqp->masked_pending || irqp->resend)
1174 val |= KVM_XICS_PENDING;
1175 ret = 0;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001176 }
Suresh Warrier34cb7952015-03-20 20:39:46 +11001177 arch_spin_unlock(&ics->lock);
1178 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001179
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001180 if (!ret && put_user(val, ubufp))
1181 ret = -EFAULT;
1182
1183 return ret;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001184}
1185
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001186static int xics_set_source(struct kvmppc_xics *xics, long irq, u64 addr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001187{
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001188 struct kvmppc_ics *ics;
1189 struct ics_irq_state *irqp;
1190 u64 __user *ubufp = (u64 __user *) addr;
1191 u16 idx;
1192 u64 val;
1193 u8 prio;
1194 u32 server;
Suresh Warrier34cb7952015-03-20 20:39:46 +11001195 unsigned long flags;
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001196
1197 if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
1198 return -ENOENT;
1199
1200 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1201 if (!ics) {
1202 ics = kvmppc_xics_create_ics(xics->kvm, xics, irq);
1203 if (!ics)
1204 return -ENOMEM;
1205 }
1206 irqp = &ics->irq_state[idx];
1207 if (get_user(val, ubufp))
1208 return -EFAULT;
1209
1210 server = val & KVM_XICS_DESTINATION_MASK;
1211 prio = val >> KVM_XICS_PRIORITY_SHIFT;
1212 if (prio != MASKED &&
1213 kvmppc_xics_find_server(xics->kvm, server) == NULL)
1214 return -EINVAL;
1215
Suresh Warrier34cb7952015-03-20 20:39:46 +11001216 local_irq_save(flags);
1217 arch_spin_lock(&ics->lock);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001218 irqp->server = server;
1219 irqp->saved_priority = prio;
1220 if (val & KVM_XICS_MASKED)
1221 prio = MASKED;
1222 irqp->priority = prio;
1223 irqp->resend = 0;
1224 irqp->masked_pending = 0;
1225 irqp->asserted = 0;
1226 if ((val & KVM_XICS_PENDING) && (val & KVM_XICS_LEVEL_SENSITIVE))
1227 irqp->asserted = 1;
1228 irqp->exists = 1;
Suresh Warrier34cb7952015-03-20 20:39:46 +11001229 arch_spin_unlock(&ics->lock);
1230 local_irq_restore(flags);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001231
1232 if (val & KVM_XICS_PENDING)
1233 icp_deliver_irq(xics, NULL, irqp->number);
1234
1235 return 0;
1236}
1237
1238int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
1239 bool line_status)
1240{
1241 struct kvmppc_xics *xics = kvm->arch.xics;
1242
Paul Mackerras25a2150b2014-06-30 20:51:14 +10001243 return ics_deliver_irq(xics, irq, level);
1244}
1245
1246int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
1247 int irq_source_id, int level, bool line_status)
1248{
1249 if (!level)
1250 return -1;
1251 return kvm_set_irq(kvm, irq_source_id, irq_entry->gsi,
1252 level, line_status);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001253}
1254
1255static int xics_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1256{
1257 struct kvmppc_xics *xics = dev->private;
1258
1259 switch (attr->group) {
1260 case KVM_DEV_XICS_GRP_SOURCES:
1261 return xics_set_source(xics, attr->attr, attr->addr);
1262 }
1263 return -ENXIO;
1264}
1265
1266static int xics_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1267{
1268 struct kvmppc_xics *xics = dev->private;
1269
1270 switch (attr->group) {
1271 case KVM_DEV_XICS_GRP_SOURCES:
1272 return xics_get_source(xics, attr->attr, attr->addr);
1273 }
1274 return -ENXIO;
1275}
1276
1277static int xics_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1278{
1279 switch (attr->group) {
1280 case KVM_DEV_XICS_GRP_SOURCES:
1281 if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
1282 attr->attr < KVMPPC_XICS_NR_IRQS)
1283 return 0;
1284 break;
1285 }
1286 return -ENXIO;
1287}
1288
1289static void kvmppc_xics_free(struct kvm_device *dev)
1290{
1291 struct kvmppc_xics *xics = dev->private;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001292 int i;
1293 struct kvm *kvm = xics->kvm;
1294
1295 debugfs_remove(xics->dentry);
1296
1297 if (kvm)
1298 kvm->arch.xics = NULL;
1299
1300 for (i = 0; i <= xics->max_icsid; i++)
1301 kfree(xics->ics[i]);
1302 kfree(xics);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001303 kfree(dev);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001304}
1305
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001306static int kvmppc_xics_create(struct kvm_device *dev, u32 type)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001307{
1308 struct kvmppc_xics *xics;
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001309 struct kvm *kvm = dev->kvm;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001310 int ret = 0;
1311
1312 xics = kzalloc(sizeof(*xics), GFP_KERNEL);
1313 if (!xics)
1314 return -ENOMEM;
1315
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001316 dev->private = xics;
1317 xics->dev = dev;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001318 xics->kvm = kvm;
1319
1320 /* Already there ? */
1321 mutex_lock(&kvm->lock);
1322 if (kvm->arch.xics)
1323 ret = -EEXIST;
1324 else
1325 kvm->arch.xics = xics;
1326 mutex_unlock(&kvm->lock);
1327
Gleb Natapov458ff3c2013-09-01 15:53:46 +03001328 if (ret) {
1329 kfree(xics);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001330 return ret;
Gleb Natapov458ff3c2013-09-01 15:53:46 +03001331 }
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001332
1333 xics_debugfs_init(xics);
1334
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301335#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001336 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
1337 /* Enable real mode support */
1338 xics->real_mode = ENABLE_REALMODE;
1339 xics->real_mode_dbg = DEBUG_REALMODE;
1340 }
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301341#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001342
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001343 return 0;
1344}
1345
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001346struct kvm_device_ops kvm_xics_ops = {
1347 .name = "kvm-xics",
1348 .create = kvmppc_xics_create,
1349 .destroy = kvmppc_xics_free,
1350 .set_attr = xics_set_attr,
1351 .get_attr = xics_get_attr,
1352 .has_attr = xics_has_attr,
1353};
1354
1355int kvmppc_xics_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
1356 u32 xcpu)
1357{
1358 struct kvmppc_xics *xics = dev->private;
1359 int r = -EBUSY;
1360
1361 if (dev->ops != &kvm_xics_ops)
1362 return -EPERM;
1363 if (xics->kvm != vcpu->kvm)
1364 return -EPERM;
1365 if (vcpu->arch.irq_type)
1366 return -EBUSY;
1367
1368 r = kvmppc_xics_create_icp(vcpu, xcpu);
1369 if (!r)
1370 vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
1371
1372 return r;
1373}
1374
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001375void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu)
1376{
1377 if (!vcpu->arch.icp)
1378 return;
1379 kfree(vcpu->arch.icp);
1380 vcpu->arch.icp = NULL;
1381 vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
1382}
Paul Mackerras25a2150b2014-06-30 20:51:14 +10001383
1384static int xics_set_irq(struct kvm_kernel_irq_routing_entry *e,
1385 struct kvm *kvm, int irq_source_id, int level,
1386 bool line_status)
1387{
1388 return kvm_set_irq(kvm, irq_source_id, e->gsi, level, line_status);
1389}
1390
1391int kvm_irq_map_gsi(struct kvm *kvm,
1392 struct kvm_kernel_irq_routing_entry *entries, int gsi)
1393{
1394 entries->gsi = gsi;
1395 entries->type = KVM_IRQ_ROUTING_IRQCHIP;
1396 entries->set = xics_set_irq;
1397 entries->irqchip.irqchip = 0;
1398 entries->irqchip.pin = gsi;
1399 return 1;
1400}
1401
1402int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
1403{
1404 return pin;
1405}