blob: debda779a240ada27059f30736f69e39c7a33f18 [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>
Michael Ellerman433c5c22015-04-28 10:34:35 +100015#include <linux/spinlock.h>
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000016
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080017#include <linux/uaccess.h>
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000018#include <asm/kvm_book3s.h>
19#include <asm/kvm_ppc.h>
20#include <asm/hvcall.h>
21#include <asm/xics.h>
22#include <asm/debug.h>
Paul Mackerras7bfa9ad2013-08-06 14:13:44 +100023#include <asm/time.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
Greg Kurz4e33d1f2015-09-03 11:12:59 +020044 * sources and avoiding simultaneous deliveries of the same interrupt.
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000045 *
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 Mackerrasb1a42862016-05-04 21:07:52 +100095 if ((level == 1 && state->lsi) || 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
Paul Mackerras5d375192016-08-19 15:35:56 +1000102 /* Record which CPU this arrived on for passed-through interrupts */
103 if (state->host_irq)
104 state->intr_cpu = raw_smp_processor_id();
105
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000106 /* Attempt delivery */
107 icp_deliver_irq(xics, NULL, irq);
108
Paul Mackerras25a2150b2014-06-30 20:51:14 +1000109 return 0;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000110}
111
112static void ics_check_resend(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
113 struct kvmppc_icp *icp)
114{
115 int i;
116
Suresh Warrier34cb7952015-03-20 20:39:46 +1100117 unsigned long flags;
118
119 local_irq_save(flags);
120 arch_spin_lock(&ics->lock);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000121
122 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
123 struct ics_irq_state *state = &ics->irq_state[i];
124
125 if (!state->resend)
126 continue;
127
128 XICS_DBG("resend %#x prio %#x\n", state->number,
129 state->priority);
130
Suresh Warrier34cb7952015-03-20 20:39:46 +1100131 arch_spin_unlock(&ics->lock);
132 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000133 icp_deliver_irq(xics, icp, state->number);
Suresh Warrier34cb7952015-03-20 20:39:46 +1100134 local_irq_save(flags);
135 arch_spin_lock(&ics->lock);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000136 }
137
Suresh Warrier34cb7952015-03-20 20:39:46 +1100138 arch_spin_unlock(&ics->lock);
139 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000140}
141
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000142static bool write_xive(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
143 struct ics_irq_state *state,
144 u32 server, u32 priority, u32 saved_priority)
145{
146 bool deliver;
Suresh Warrier34cb7952015-03-20 20:39:46 +1100147 unsigned long flags;
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000148
Suresh Warrier34cb7952015-03-20 20:39:46 +1100149 local_irq_save(flags);
150 arch_spin_lock(&ics->lock);
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000151
152 state->server = server;
153 state->priority = priority;
154 state->saved_priority = saved_priority;
155 deliver = false;
156 if ((state->masked_pending || state->resend) && priority != MASKED) {
157 state->masked_pending = 0;
158 deliver = true;
159 }
160
Suresh Warrier34cb7952015-03-20 20:39:46 +1100161 arch_spin_unlock(&ics->lock);
162 local_irq_restore(flags);
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000163
164 return deliver;
165}
166
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000167int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server, u32 priority)
168{
169 struct kvmppc_xics *xics = kvm->arch.xics;
170 struct kvmppc_icp *icp;
171 struct kvmppc_ics *ics;
172 struct ics_irq_state *state;
173 u16 src;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000174
175 if (!xics)
176 return -ENODEV;
177
178 ics = kvmppc_xics_find_ics(xics, irq, &src);
179 if (!ics)
180 return -EINVAL;
181 state = &ics->irq_state[src];
182
183 icp = kvmppc_xics_find_server(kvm, server);
184 if (!icp)
185 return -EINVAL;
186
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000187 XICS_DBG("set_xive %#x server %#x prio %#x MP:%d RS:%d\n",
188 irq, server, priority,
189 state->masked_pending, state->resend);
190
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000191 if (write_xive(xics, ics, state, server, priority, priority))
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000192 icp_deliver_irq(xics, icp, irq);
193
194 return 0;
195}
196
197int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, u32 *priority)
198{
199 struct kvmppc_xics *xics = kvm->arch.xics;
200 struct kvmppc_ics *ics;
201 struct ics_irq_state *state;
202 u16 src;
Suresh Warrier34cb7952015-03-20 20:39:46 +1100203 unsigned long flags;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000204
205 if (!xics)
206 return -ENODEV;
207
208 ics = kvmppc_xics_find_ics(xics, irq, &src);
209 if (!ics)
210 return -EINVAL;
211 state = &ics->irq_state[src];
212
Suresh Warrier34cb7952015-03-20 20:39:46 +1100213 local_irq_save(flags);
214 arch_spin_lock(&ics->lock);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000215 *server = state->server;
216 *priority = state->priority;
Suresh Warrier34cb7952015-03-20 20:39:46 +1100217 arch_spin_unlock(&ics->lock);
218 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000219
220 return 0;
221}
222
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000223int kvmppc_xics_int_on(struct kvm *kvm, u32 irq)
224{
225 struct kvmppc_xics *xics = kvm->arch.xics;
226 struct kvmppc_icp *icp;
227 struct kvmppc_ics *ics;
228 struct ics_irq_state *state;
229 u16 src;
230
231 if (!xics)
232 return -ENODEV;
233
234 ics = kvmppc_xics_find_ics(xics, irq, &src);
235 if (!ics)
236 return -EINVAL;
237 state = &ics->irq_state[src];
238
239 icp = kvmppc_xics_find_server(kvm, state->server);
240 if (!icp)
241 return -EINVAL;
242
243 if (write_xive(xics, ics, state, state->server, state->saved_priority,
244 state->saved_priority))
245 icp_deliver_irq(xics, icp, irq);
246
247 return 0;
248}
249
250int kvmppc_xics_int_off(struct kvm *kvm, u32 irq)
251{
252 struct kvmppc_xics *xics = kvm->arch.xics;
253 struct kvmppc_ics *ics;
254 struct ics_irq_state *state;
255 u16 src;
256
257 if (!xics)
258 return -ENODEV;
259
260 ics = kvmppc_xics_find_ics(xics, irq, &src);
261 if (!ics)
262 return -EINVAL;
263 state = &ics->irq_state[src];
264
265 write_xive(xics, ics, state, state->server, MASKED, state->priority);
266
267 return 0;
268}
269
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000270/* -- ICP routines, including hcalls -- */
271
272static inline bool icp_try_update(struct kvmppc_icp *icp,
273 union kvmppc_icp_state old,
274 union kvmppc_icp_state new,
275 bool change_self)
276{
277 bool success;
278
279 /* Calculate new output value */
280 new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
281
282 /* Attempt atomic update */
283 success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
284 if (!success)
285 goto bail;
286
Alexey Kardashevskiyade3ac62016-04-29 14:57:23 +1000287 XICS_DBG("UPD [%04lx] - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000288 icp->server_num,
289 old.cppr, old.mfrr, old.pending_pri, old.xisr,
290 old.need_resend, old.out_ee);
291 XICS_DBG("UPD - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
292 new.cppr, new.mfrr, new.pending_pri, new.xisr,
293 new.need_resend, new.out_ee);
294 /*
295 * Check for output state update
296 *
297 * Note that this is racy since another processor could be updating
298 * the state already. This is why we never clear the interrupt output
299 * here, we only ever set it. The clear only happens prior to doing
300 * an update and only by the processor itself. Currently we do it
301 * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
302 *
303 * We also do not try to figure out whether the EE state has changed,
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000304 * we unconditionally set it if the new state calls for it. The reason
305 * for that is that we opportunistically remove the pending interrupt
306 * flag when raising CPPR, so we need to set it back here if an
307 * interrupt is still pending.
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000308 */
309 if (new.out_ee) {
310 kvmppc_book3s_queue_irqprio(icp->vcpu,
311 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
312 if (!change_self)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000313 kvmppc_fast_vcpu_kick(icp->vcpu);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000314 }
315 bail:
316 return success;
317}
318
319static void icp_check_resend(struct kvmppc_xics *xics,
320 struct kvmppc_icp *icp)
321{
322 u32 icsid;
323
324 /* Order this load with the test for need_resend in the caller */
325 smp_rmb();
326 for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
327 struct kvmppc_ics *ics = xics->ics[icsid];
328
329 if (!test_and_clear_bit(icsid, icp->resend_map))
330 continue;
331 if (!ics)
332 continue;
333 ics_check_resend(xics, ics, icp);
334 }
335}
336
337static bool icp_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
338 u32 *reject)
339{
340 union kvmppc_icp_state old_state, new_state;
341 bool success;
342
Alexey Kardashevskiyade3ac62016-04-29 14:57:23 +1000343 XICS_DBG("try deliver %#x(P:%#x) to server %#lx\n", irq, priority,
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000344 icp->server_num);
345
346 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100347 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000348
349 *reject = 0;
350
351 /* See if we can deliver */
352 success = new_state.cppr > priority &&
353 new_state.mfrr > priority &&
354 new_state.pending_pri > priority;
355
356 /*
357 * If we can, check for a rejection and perform the
358 * delivery
359 */
360 if (success) {
361 *reject = new_state.xisr;
362 new_state.xisr = irq;
363 new_state.pending_pri = priority;
364 } else {
365 /*
366 * If we failed to deliver we set need_resend
367 * so a subsequent CPPR state change causes us
368 * to try a new delivery.
369 */
370 new_state.need_resend = true;
371 }
372
373 } while (!icp_try_update(icp, old_state, new_state, false));
374
375 return success;
376}
377
378static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
379 u32 new_irq)
380{
381 struct ics_irq_state *state;
382 struct kvmppc_ics *ics;
383 u32 reject;
384 u16 src;
Suresh Warrier34cb7952015-03-20 20:39:46 +1100385 unsigned long flags;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000386
387 /*
388 * This is used both for initial delivery of an interrupt and
389 * for subsequent rejection.
390 *
391 * Rejection can be racy vs. resends. We have evaluated the
392 * rejection in an atomic ICP transaction which is now complete,
393 * so potentially the ICP can already accept the interrupt again.
394 *
395 * So we need to retry the delivery. Essentially the reject path
396 * boils down to a failed delivery. Always.
397 *
398 * Now the interrupt could also have moved to a different target,
399 * thus we may need to re-do the ICP lookup as well
400 */
401
402 again:
403 /* Get the ICS state and lock it */
404 ics = kvmppc_xics_find_ics(xics, new_irq, &src);
405 if (!ics) {
406 XICS_DBG("icp_deliver_irq: IRQ 0x%06x not found !\n", new_irq);
407 return;
408 }
409 state = &ics->irq_state[src];
410
411 /* Get a lock on the ICS */
Suresh Warrier34cb7952015-03-20 20:39:46 +1100412 local_irq_save(flags);
413 arch_spin_lock(&ics->lock);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000414
415 /* Get our server */
416 if (!icp || state->server != icp->server_num) {
417 icp = kvmppc_xics_find_server(xics->kvm, state->server);
418 if (!icp) {
419 pr_warn("icp_deliver_irq: IRQ 0x%06x server 0x%x not found !\n",
420 new_irq, state->server);
421 goto out;
422 }
423 }
424
425 /* Clear the resend bit of that interrupt */
426 state->resend = 0;
427
428 /*
429 * If masked, bail out
430 *
431 * Note: PAPR doesn't mention anything about masked pending
432 * when doing a resend, only when doing a delivery.
433 *
434 * However that would have the effect of losing a masked
435 * interrupt that was rejected and isn't consistent with
436 * the whole masked_pending business which is about not
437 * losing interrupts that occur while masked.
438 *
Adam Buchbinder446957b2016-02-24 10:51:11 -0800439 * I don't differentiate normal deliveries and resends, this
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000440 * implementation will differ from PAPR and not lose such
441 * interrupts.
442 */
443 if (state->priority == MASKED) {
444 XICS_DBG("irq %#x masked pending\n", new_irq);
445 state->masked_pending = 1;
446 goto out;
447 }
448
449 /*
450 * Try the delivery, this will set the need_resend flag
451 * in the ICP as part of the atomic transaction if the
452 * delivery is not possible.
453 *
454 * Note that if successful, the new delivery might have itself
455 * rejected an interrupt that was "delivered" before we took the
Suresh Warrier34cb7952015-03-20 20:39:46 +1100456 * ics spin lock.
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000457 *
458 * In this case we do the whole sequence all over again for the
459 * new guy. We cannot assume that the rejected interrupt is less
460 * favored than the new one, and thus doesn't need to be delivered,
461 * because by the time we exit icp_try_to_deliver() the target
462 * processor may well have alrady consumed & completed it, and thus
463 * the rejected interrupt might actually be already acceptable.
464 */
465 if (icp_try_to_deliver(icp, new_irq, state->priority, &reject)) {
466 /*
467 * Delivery was successful, did we reject somebody else ?
468 */
469 if (reject && reject != XICS_IPI) {
Suresh Warrier34cb7952015-03-20 20:39:46 +1100470 arch_spin_unlock(&ics->lock);
471 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000472 new_irq = reject;
473 goto again;
474 }
475 } else {
476 /*
477 * We failed to deliver the interrupt we need to set the
478 * resend map bit and mark the ICS state as needing a resend
479 */
480 set_bit(ics->icsid, icp->resend_map);
481 state->resend = 1;
482
483 /*
484 * If the need_resend flag got cleared in the ICP some time
485 * between icp_try_to_deliver() atomic update and now, then
486 * we know it might have missed the resend_map bit. So we
487 * retry
488 */
489 smp_mb();
490 if (!icp->state.need_resend) {
Suresh Warrier34cb7952015-03-20 20:39:46 +1100491 arch_spin_unlock(&ics->lock);
492 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000493 goto again;
494 }
495 }
496 out:
Suresh Warrier34cb7952015-03-20 20:39:46 +1100497 arch_spin_unlock(&ics->lock);
498 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000499}
500
501static void icp_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
502 u8 new_cppr)
503{
504 union kvmppc_icp_state old_state, new_state;
505 bool resend;
506
507 /*
508 * This handles several related states in one operation:
509 *
510 * ICP State: Down_CPPR
511 *
512 * Load CPPR with new value and if the XISR is 0
513 * then check for resends:
514 *
515 * ICP State: Resend
516 *
517 * If MFRR is more favored than CPPR, check for IPIs
518 * and notify ICS of a potential resend. This is done
519 * asynchronously (when used in real mode, we will have
520 * to exit here).
521 *
522 * We do not handle the complete Check_IPI as documented
523 * here. In the PAPR, this state will be used for both
524 * Set_MFRR and Down_CPPR. However, we know that we aren't
525 * changing the MFRR state here so we don't need to handle
526 * the case of an MFRR causing a reject of a pending irq,
527 * this will have been handled when the MFRR was set in the
528 * first place.
529 *
530 * Thus we don't have to handle rejects, only resends.
531 *
532 * When implementing real mode for HV KVM, resend will lead to
533 * a H_TOO_HARD return and the whole transaction will be handled
534 * in virtual mode.
535 */
536 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100537 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000538
539 /* Down_CPPR */
540 new_state.cppr = new_cppr;
541
542 /*
543 * Cut down Resend / Check_IPI / IPI
544 *
545 * The logic is that we cannot have a pending interrupt
546 * trumped by an IPI at this point (see above), so we
547 * know that either the pending interrupt is already an
548 * IPI (in which case we don't care to override it) or
549 * it's either more favored than us or non existent
550 */
551 if (new_state.mfrr < new_cppr &&
552 new_state.mfrr <= new_state.pending_pri) {
553 WARN_ON(new_state.xisr != XICS_IPI &&
554 new_state.xisr != 0);
555 new_state.pending_pri = new_state.mfrr;
556 new_state.xisr = XICS_IPI;
557 }
558
559 /* Latch/clear resend bit */
560 resend = new_state.need_resend;
561 new_state.need_resend = 0;
562
563 } while (!icp_try_update(icp, old_state, new_state, true));
564
565 /*
566 * Now handle resend checks. Those are asynchronous to the ICP
567 * state update in HW (ie bus transactions) so we can handle them
568 * separately here too
569 */
570 if (resend)
571 icp_check_resend(xics, icp);
572}
573
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000574static noinline unsigned long kvmppc_h_xirr(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000575{
576 union kvmppc_icp_state old_state, new_state;
577 struct kvmppc_icp *icp = vcpu->arch.icp;
578 u32 xirr;
579
580 /* First, remove EE from the processor */
581 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
582 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
583
584 /*
585 * ICP State: Accept_Interrupt
586 *
587 * Return the pending interrupt (if any) along with the
588 * current CPPR, then clear the XISR & set CPPR to the
589 * pending priority
590 */
591 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100592 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000593
594 xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
595 if (!old_state.xisr)
596 break;
597 new_state.cppr = new_state.pending_pri;
598 new_state.pending_pri = 0xff;
599 new_state.xisr = 0;
600
601 } while (!icp_try_update(icp, old_state, new_state, true));
602
603 XICS_DBG("h_xirr vcpu %d xirr %#x\n", vcpu->vcpu_id, xirr);
604
605 return xirr;
606}
607
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000608static noinline int kvmppc_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
609 unsigned long mfrr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000610{
611 union kvmppc_icp_state old_state, new_state;
612 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
613 struct kvmppc_icp *icp;
614 u32 reject;
615 bool resend;
616 bool local;
617
618 XICS_DBG("h_ipi vcpu %d to server %lu mfrr %#lx\n",
619 vcpu->vcpu_id, server, mfrr);
620
621 icp = vcpu->arch.icp;
622 local = icp->server_num == server;
623 if (!local) {
624 icp = kvmppc_xics_find_server(vcpu->kvm, server);
625 if (!icp)
626 return H_PARAMETER;
627 }
628
629 /*
630 * ICP state: Set_MFRR
631 *
632 * If the CPPR is more favored than the new MFRR, then
633 * nothing needs to be rejected as there can be no XISR to
634 * reject. If the MFRR is being made less favored then
635 * there might be a previously-rejected interrupt needing
636 * to be resent.
637 *
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000638 * ICP state: Check_IPI
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100639 *
640 * If the CPPR is less favored, then we might be replacing
641 * an interrupt, and thus need to possibly reject it.
642 *
643 * ICP State: IPI
644 *
645 * Besides rejecting any pending interrupts, we also
646 * update XISR and pending_pri to mark IPI as pending.
647 *
648 * PAPR does not describe this state, but if the MFRR is being
649 * made less favored than its earlier value, there might be
650 * a previously-rejected interrupt needing to be resent.
651 * Ideally, we would want to resend only if
652 * prio(pending_interrupt) < mfrr &&
653 * prio(pending_interrupt) < cppr
654 * where pending interrupt is the one that was rejected. But
655 * we don't have that state, so we simply trigger a resend
656 * whenever the MFRR is made less favored.
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000657 */
658 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100659 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000660
661 /* Set_MFRR */
662 new_state.mfrr = mfrr;
663
664 /* Check_IPI */
665 reject = 0;
666 resend = false;
667 if (mfrr < new_state.cppr) {
668 /* Reject a pending interrupt if not an IPI */
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100669 if (mfrr <= new_state.pending_pri) {
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000670 reject = new_state.xisr;
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100671 new_state.pending_pri = mfrr;
672 new_state.xisr = XICS_IPI;
673 }
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000674 }
675
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100676 if (mfrr > old_state.mfrr) {
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000677 resend = new_state.need_resend;
678 new_state.need_resend = 0;
679 }
680 } while (!icp_try_update(icp, old_state, new_state, local));
681
682 /* Handle reject */
683 if (reject && reject != XICS_IPI)
684 icp_deliver_irq(xics, icp, reject);
685
686 /* Handle resend */
687 if (resend)
688 icp_check_resend(xics, icp);
689
690 return H_SUCCESS;
691}
692
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000693static int kvmppc_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server)
694{
695 union kvmppc_icp_state state;
696 struct kvmppc_icp *icp;
697
698 icp = vcpu->arch.icp;
699 if (icp->server_num != server) {
700 icp = kvmppc_xics_find_server(vcpu->kvm, server);
701 if (!icp)
702 return H_PARAMETER;
703 }
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100704 state = READ_ONCE(icp->state);
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000705 kvmppc_set_gpr(vcpu, 4, ((u32)state.cppr << 24) | state.xisr);
706 kvmppc_set_gpr(vcpu, 5, state.mfrr);
707 return H_SUCCESS;
708}
709
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000710static noinline void kvmppc_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000711{
712 union kvmppc_icp_state old_state, new_state;
713 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
714 struct kvmppc_icp *icp = vcpu->arch.icp;
715 u32 reject;
716
717 XICS_DBG("h_cppr vcpu %d cppr %#lx\n", vcpu->vcpu_id, cppr);
718
719 /*
720 * ICP State: Set_CPPR
721 *
722 * We can safely compare the new value with the current
723 * value outside of the transaction as the CPPR is only
724 * ever changed by the processor on itself
725 */
726 if (cppr > icp->state.cppr)
727 icp_down_cppr(xics, icp, cppr);
728 else if (cppr == icp->state.cppr)
729 return;
730
731 /*
732 * ICP State: Up_CPPR
733 *
734 * The processor is raising its priority, this can result
735 * in a rejection of a pending interrupt:
736 *
737 * ICP State: Reject_Current
738 *
739 * We can remove EE from the current processor, the update
740 * transaction will set it again if needed
741 */
742 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
743 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
744
745 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100746 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000747
748 reject = 0;
749 new_state.cppr = cppr;
750
751 if (cppr <= new_state.pending_pri) {
752 reject = new_state.xisr;
753 new_state.xisr = 0;
754 new_state.pending_pri = 0xff;
755 }
756
757 } while (!icp_try_update(icp, old_state, new_state, true));
758
759 /*
760 * Check for rejects. They are handled by doing a new delivery
761 * attempt (see comments in icp_deliver_irq).
762 */
763 if (reject && reject != XICS_IPI)
764 icp_deliver_irq(xics, icp, reject);
765}
766
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000767static noinline int kvmppc_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000768{
769 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
770 struct kvmppc_icp *icp = vcpu->arch.icp;
771 struct kvmppc_ics *ics;
772 struct ics_irq_state *state;
773 u32 irq = xirr & 0x00ffffff;
774 u16 src;
775
776 XICS_DBG("h_eoi vcpu %d eoi %#lx\n", vcpu->vcpu_id, xirr);
777
778 /*
779 * ICP State: EOI
780 *
781 * Note: If EOI is incorrectly used by SW to lower the CPPR
782 * value (ie more favored), we do not check for rejection of
783 * a pending interrupt, this is a SW error and PAPR sepcifies
784 * that we don't have to deal with it.
785 *
786 * The sending of an EOI to the ICS is handled after the
787 * CPPR update
788 *
789 * ICP State: Down_CPPR which we handle
790 * in a separate function as it's shared with H_CPPR.
791 */
792 icp_down_cppr(xics, icp, xirr >> 24);
793
794 /* IPIs have no EOI */
795 if (irq == XICS_IPI)
796 return H_SUCCESS;
797 /*
798 * EOI handling: If the interrupt is still asserted, we need to
799 * resend it. We can take a lockless "peek" at the ICS state here.
800 *
801 * "Message" interrupts will never have "asserted" set
802 */
803 ics = kvmppc_xics_find_ics(xics, irq, &src);
804 if (!ics) {
805 XICS_DBG("h_eoi: IRQ 0x%06x not found !\n", irq);
806 return H_PARAMETER;
807 }
808 state = &ics->irq_state[src];
809
810 /* Still asserted, resend it */
811 if (state->asserted)
812 icp_deliver_irq(xics, icp, irq);
813
Paul Mackerras25a2150b2014-06-30 20:51:14 +1000814 kvm_notify_acked_irq(vcpu->kvm, 0, irq);
815
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000816 return H_SUCCESS;
817}
818
Suresh Warrierf7af5202016-08-19 15:35:52 +1000819int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000820{
821 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
822 struct kvmppc_icp *icp = vcpu->arch.icp;
823
824 XICS_DBG("XICS_RM: H_%x completing, act: %x state: %lx tgt: %p\n",
825 hcall, icp->rm_action, icp->rm_dbgstate.raw, icp->rm_dbgtgt);
826
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100827 if (icp->rm_action & XICS_RM_KICK_VCPU) {
828 icp->n_rm_kick_vcpu++;
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000829 kvmppc_fast_vcpu_kick(icp->rm_kick_target);
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100830 }
831 if (icp->rm_action & XICS_RM_CHECK_RESEND) {
832 icp->n_rm_check_resend++;
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100833 icp_check_resend(xics, icp->rm_resend_icp);
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100834 }
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100835 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}
Suresh Warrierf7af5202016-08-19 15:35:52 +1000844EXPORT_SYMBOL_GPL(kvmppc_xics_rm_complete);
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000845
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000846int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 req)
847{
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000848 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000849 unsigned long res;
850 int rc = H_SUCCESS;
851
852 /* Check if we have an ICP */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000853 if (!xics || !vcpu->arch.icp)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000854 return H_HARDWARE;
855
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000856 /* These requests don't have real-mode implementations at present */
857 switch (req) {
858 case H_XIRR_X:
859 res = kvmppc_h_xirr(vcpu);
860 kvmppc_set_gpr(vcpu, 4, res);
861 kvmppc_set_gpr(vcpu, 5, get_tb());
862 return rc;
863 case H_IPOLL:
864 rc = kvmppc_h_ipoll(vcpu, kvmppc_get_gpr(vcpu, 4));
865 return rc;
866 }
867
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000868 /* Check for real mode returning too hard */
Aneesh Kumar K.Va78b55d2013-10-07 22:18:02 +0530869 if (xics->real_mode && is_kvmppc_hv_enabled(vcpu->kvm))
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000870 return kvmppc_xics_rm_complete(vcpu, req);
871
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000872 switch (req) {
873 case H_XIRR:
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000874 res = kvmppc_h_xirr(vcpu);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000875 kvmppc_set_gpr(vcpu, 4, res);
876 break;
877 case H_CPPR:
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000878 kvmppc_h_cppr(vcpu, kvmppc_get_gpr(vcpu, 4));
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000879 break;
880 case H_EOI:
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000881 rc = kvmppc_h_eoi(vcpu, kvmppc_get_gpr(vcpu, 4));
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000882 break;
883 case H_IPI:
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000884 rc = kvmppc_h_ipi(vcpu, kvmppc_get_gpr(vcpu, 4),
885 kvmppc_get_gpr(vcpu, 5));
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000886 break;
887 }
888
889 return rc;
890}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530891EXPORT_SYMBOL_GPL(kvmppc_xics_hcall);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000892
893
894/* -- Initialisation code etc. -- */
895
Suresh Warrieraf893c72016-08-19 15:35:53 +1000896static void xics_debugfs_irqmap(struct seq_file *m,
897 struct kvmppc_passthru_irqmap *pimap)
898{
899 int i;
900
901 if (!pimap)
902 return;
903 seq_printf(m, "========\nPIRQ mappings: %d maps\n===========\n",
904 pimap->n_mapped);
905 for (i = 0; i < pimap->n_mapped; i++) {
906 seq_printf(m, "r_hwirq=%x, v_hwirq=%x\n",
907 pimap->mapped[i].r_hwirq, pimap->mapped[i].v_hwirq);
908 }
909}
910
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000911static int xics_debug_show(struct seq_file *m, void *private)
912{
913 struct kvmppc_xics *xics = m->private;
914 struct kvm *kvm = xics->kvm;
915 struct kvm_vcpu *vcpu;
916 int icsid, i;
Suresh Warrier34cb7952015-03-20 20:39:46 +1100917 unsigned long flags;
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100918 unsigned long t_rm_kick_vcpu, t_rm_check_resend;
Li Zhong5efa6602016-11-11 12:57:32 +0800919 unsigned long t_rm_notify_eoi;
Suresh Warrier6e0365b2015-03-20 20:39:48 +1100920 unsigned long t_reject, t_check_resend;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000921
922 if (!kvm)
923 return 0;
924
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100925 t_rm_kick_vcpu = 0;
926 t_rm_notify_eoi = 0;
927 t_rm_check_resend = 0;
Suresh Warrier6e0365b2015-03-20 20:39:48 +1100928 t_check_resend = 0;
929 t_reject = 0;
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100930
Suresh Warrieraf893c72016-08-19 15:35:53 +1000931 xics_debugfs_irqmap(m, kvm->arch.pimap);
932
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000933 seq_printf(m, "=========\nICP state\n=========\n");
934
935 kvm_for_each_vcpu(i, vcpu, kvm) {
936 struct kvmppc_icp *icp = vcpu->arch.icp;
937 union kvmppc_icp_state state;
938
939 if (!icp)
940 continue;
941
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100942 state.raw = READ_ONCE(icp->state.raw);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000943 seq_printf(m, "cpu server %#lx XIRR:%#x PPRI:%#x CPPR:%#x MFRR:%#x OUT:%d NR:%d\n",
944 icp->server_num, state.xisr,
945 state.pending_pri, state.cppr, state.mfrr,
946 state.out_ee, state.need_resend);
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100947 t_rm_kick_vcpu += icp->n_rm_kick_vcpu;
948 t_rm_notify_eoi += icp->n_rm_notify_eoi;
949 t_rm_check_resend += icp->n_rm_check_resend;
Suresh Warrier6e0365b2015-03-20 20:39:48 +1100950 t_check_resend += icp->n_check_resend;
951 t_reject += icp->n_reject;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000952 }
953
Li Zhong5efa6602016-11-11 12:57:32 +0800954 seq_printf(m, "ICP Guest->Host totals: kick_vcpu=%lu check_resend=%lu notify_eoi=%lu\n",
Suresh E. Warrier878610f2015-03-20 20:39:45 +1100955 t_rm_kick_vcpu, t_rm_check_resend,
Li Zhong5efa6602016-11-11 12:57:32 +0800956 t_rm_notify_eoi);
Suresh Warrier6e0365b2015-03-20 20:39:48 +1100957 seq_printf(m, "ICP Real Mode totals: check_resend=%lu resend=%lu\n",
958 t_check_resend, t_reject);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000959 for (icsid = 0; icsid <= KVMPPC_XICS_MAX_ICS_ID; icsid++) {
960 struct kvmppc_ics *ics = xics->ics[icsid];
961
962 if (!ics)
963 continue;
964
965 seq_printf(m, "=========\nICS state for ICS 0x%x\n=========\n",
966 icsid);
967
Suresh Warrier34cb7952015-03-20 20:39:46 +1100968 local_irq_save(flags);
969 arch_spin_lock(&ics->lock);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000970
971 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
972 struct ics_irq_state *irq = &ics->irq_state[i];
973
974 seq_printf(m, "irq 0x%06x: server %#x prio %#x save prio %#x asserted %d resend %d masked pending %d\n",
975 irq->number, irq->server, irq->priority,
976 irq->saved_priority, irq->asserted,
977 irq->resend, irq->masked_pending);
978
979 }
Suresh Warrier34cb7952015-03-20 20:39:46 +1100980 arch_spin_unlock(&ics->lock);
981 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000982 }
983 return 0;
984}
985
986static int xics_debug_open(struct inode *inode, struct file *file)
987{
988 return single_open(file, xics_debug_show, inode->i_private);
989}
990
991static const struct file_operations xics_debug_fops = {
992 .open = xics_debug_open,
993 .read = seq_read,
994 .llseek = seq_lseek,
995 .release = single_release,
996};
997
998static void xics_debugfs_init(struct kvmppc_xics *xics)
999{
1000 char *name;
1001
1002 name = kasprintf(GFP_KERNEL, "kvm-xics-%p", xics);
1003 if (!name) {
1004 pr_err("%s: no memory for name\n", __func__);
1005 return;
1006 }
1007
1008 xics->dentry = debugfs_create_file(name, S_IRUGO, powerpc_debugfs_root,
1009 xics, &xics_debug_fops);
1010
1011 pr_debug("%s: created %s\n", __func__, name);
1012 kfree(name);
1013}
1014
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001015static struct kvmppc_ics *kvmppc_xics_create_ics(struct kvm *kvm,
1016 struct kvmppc_xics *xics, int irq)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001017{
1018 struct kvmppc_ics *ics;
1019 int i, icsid;
1020
1021 icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
1022
1023 mutex_lock(&kvm->lock);
1024
1025 /* ICS already exists - somebody else got here first */
1026 if (xics->ics[icsid])
1027 goto out;
1028
1029 /* Create the ICS */
1030 ics = kzalloc(sizeof(struct kvmppc_ics), GFP_KERNEL);
1031 if (!ics)
1032 goto out;
1033
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001034 ics->icsid = icsid;
1035
1036 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
1037 ics->irq_state[i].number = (icsid << KVMPPC_XICS_ICS_SHIFT) | i;
1038 ics->irq_state[i].priority = MASKED;
1039 ics->irq_state[i].saved_priority = MASKED;
1040 }
1041 smp_wmb();
1042 xics->ics[icsid] = ics;
1043
1044 if (icsid > xics->max_icsid)
1045 xics->max_icsid = icsid;
1046
1047 out:
1048 mutex_unlock(&kvm->lock);
1049 return xics->ics[icsid];
1050}
1051
1052int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server_num)
1053{
1054 struct kvmppc_icp *icp;
1055
1056 if (!vcpu->kvm->arch.xics)
1057 return -ENODEV;
1058
1059 if (kvmppc_xics_find_server(vcpu->kvm, server_num))
1060 return -EEXIST;
1061
1062 icp = kzalloc(sizeof(struct kvmppc_icp), GFP_KERNEL);
1063 if (!icp)
1064 return -ENOMEM;
1065
1066 icp->vcpu = vcpu;
1067 icp->server_num = server_num;
1068 icp->state.mfrr = MASKED;
1069 icp->state.pending_pri = MASKED;
1070 vcpu->arch.icp = icp;
1071
1072 XICS_DBG("created server for vcpu %d\n", vcpu->vcpu_id);
1073
1074 return 0;
1075}
1076
Paul Mackerras8b786452013-04-17 20:32:26 +00001077u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu)
1078{
1079 struct kvmppc_icp *icp = vcpu->arch.icp;
1080 union kvmppc_icp_state state;
1081
1082 if (!icp)
1083 return 0;
1084 state = icp->state;
1085 return ((u64)state.cppr << KVM_REG_PPC_ICP_CPPR_SHIFT) |
1086 ((u64)state.xisr << KVM_REG_PPC_ICP_XISR_SHIFT) |
1087 ((u64)state.mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT) |
1088 ((u64)state.pending_pri << KVM_REG_PPC_ICP_PPRI_SHIFT);
1089}
1090
1091int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
1092{
1093 struct kvmppc_icp *icp = vcpu->arch.icp;
1094 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
1095 union kvmppc_icp_state old_state, new_state;
1096 struct kvmppc_ics *ics;
1097 u8 cppr, mfrr, pending_pri;
1098 u32 xisr;
1099 u16 src;
1100 bool resend;
1101
1102 if (!icp || !xics)
1103 return -ENOENT;
1104
1105 cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT;
1106 xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) &
1107 KVM_REG_PPC_ICP_XISR_MASK;
1108 mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT;
1109 pending_pri = icpval >> KVM_REG_PPC_ICP_PPRI_SHIFT;
1110
1111 /* Require the new state to be internally consistent */
1112 if (xisr == 0) {
1113 if (pending_pri != 0xff)
1114 return -EINVAL;
1115 } else if (xisr == XICS_IPI) {
1116 if (pending_pri != mfrr || pending_pri >= cppr)
1117 return -EINVAL;
1118 } else {
1119 if (pending_pri >= mfrr || pending_pri >= cppr)
1120 return -EINVAL;
1121 ics = kvmppc_xics_find_ics(xics, xisr, &src);
1122 if (!ics)
1123 return -EINVAL;
1124 }
1125
1126 new_state.raw = 0;
1127 new_state.cppr = cppr;
1128 new_state.xisr = xisr;
1129 new_state.mfrr = mfrr;
1130 new_state.pending_pri = pending_pri;
1131
1132 /*
1133 * Deassert the CPU interrupt request.
1134 * icp_try_update will reassert it if necessary.
1135 */
1136 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
1137 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
1138
1139 /*
1140 * Note that if we displace an interrupt from old_state.xisr,
1141 * we don't mark it as rejected. We expect userspace to set
1142 * the state of the interrupt sources to be consistent with
1143 * the ICP states (either before or afterwards, which doesn't
1144 * matter). We do handle resends due to CPPR becoming less
1145 * favoured because that is necessary to end up with a
1146 * consistent state in the situation where userspace restores
1147 * the ICS states before the ICP states.
1148 */
1149 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +01001150 old_state = READ_ONCE(icp->state);
Paul Mackerras8b786452013-04-17 20:32:26 +00001151
1152 if (new_state.mfrr <= old_state.mfrr) {
1153 resend = false;
1154 new_state.need_resend = old_state.need_resend;
1155 } else {
1156 resend = old_state.need_resend;
1157 new_state.need_resend = 0;
1158 }
1159 } while (!icp_try_update(icp, old_state, new_state, false));
1160
1161 if (resend)
1162 icp_check_resend(xics, icp);
1163
1164 return 0;
1165}
1166
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001167static int xics_get_source(struct kvmppc_xics *xics, long irq, u64 addr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001168{
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001169 int ret;
1170 struct kvmppc_ics *ics;
1171 struct ics_irq_state *irqp;
1172 u64 __user *ubufp = (u64 __user *) addr;
1173 u16 idx;
1174 u64 val, prio;
Suresh Warrier34cb7952015-03-20 20:39:46 +11001175 unsigned long flags;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001176
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001177 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1178 if (!ics)
1179 return -ENOENT;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001180
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001181 irqp = &ics->irq_state[idx];
Suresh Warrier34cb7952015-03-20 20:39:46 +11001182 local_irq_save(flags);
1183 arch_spin_lock(&ics->lock);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001184 ret = -ENOENT;
1185 if (irqp->exists) {
1186 val = irqp->server;
1187 prio = irqp->priority;
1188 if (prio == MASKED) {
1189 val |= KVM_XICS_MASKED;
1190 prio = irqp->saved_priority;
1191 }
1192 val |= prio << KVM_XICS_PRIORITY_SHIFT;
Paul Mackerrasb1a42862016-05-04 21:07:52 +10001193 if (irqp->lsi) {
1194 val |= KVM_XICS_LEVEL_SENSITIVE;
1195 if (irqp->asserted)
1196 val |= KVM_XICS_PENDING;
1197 } else if (irqp->masked_pending || irqp->resend)
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001198 val |= KVM_XICS_PENDING;
1199 ret = 0;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001200 }
Suresh Warrier34cb7952015-03-20 20:39:46 +11001201 arch_spin_unlock(&ics->lock);
1202 local_irq_restore(flags);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001203
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001204 if (!ret && put_user(val, ubufp))
1205 ret = -EFAULT;
1206
1207 return ret;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001208}
1209
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001210static int xics_set_source(struct kvmppc_xics *xics, long irq, u64 addr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001211{
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001212 struct kvmppc_ics *ics;
1213 struct ics_irq_state *irqp;
1214 u64 __user *ubufp = (u64 __user *) addr;
1215 u16 idx;
1216 u64 val;
1217 u8 prio;
1218 u32 server;
Suresh Warrier34cb7952015-03-20 20:39:46 +11001219 unsigned long flags;
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001220
1221 if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
1222 return -ENOENT;
1223
1224 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1225 if (!ics) {
1226 ics = kvmppc_xics_create_ics(xics->kvm, xics, irq);
1227 if (!ics)
1228 return -ENOMEM;
1229 }
1230 irqp = &ics->irq_state[idx];
1231 if (get_user(val, ubufp))
1232 return -EFAULT;
1233
1234 server = val & KVM_XICS_DESTINATION_MASK;
1235 prio = val >> KVM_XICS_PRIORITY_SHIFT;
1236 if (prio != MASKED &&
1237 kvmppc_xics_find_server(xics->kvm, server) == NULL)
1238 return -EINVAL;
1239
Suresh Warrier34cb7952015-03-20 20:39:46 +11001240 local_irq_save(flags);
1241 arch_spin_lock(&ics->lock);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001242 irqp->server = server;
1243 irqp->saved_priority = prio;
1244 if (val & KVM_XICS_MASKED)
1245 prio = MASKED;
1246 irqp->priority = prio;
1247 irqp->resend = 0;
1248 irqp->masked_pending = 0;
Paul Mackerrasb1a42862016-05-04 21:07:52 +10001249 irqp->lsi = 0;
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001250 irqp->asserted = 0;
Paul Mackerrasb1a42862016-05-04 21:07:52 +10001251 if (val & KVM_XICS_LEVEL_SENSITIVE) {
1252 irqp->lsi = 1;
1253 if (val & KVM_XICS_PENDING)
1254 irqp->asserted = 1;
1255 }
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001256 irqp->exists = 1;
Suresh Warrier34cb7952015-03-20 20:39:46 +11001257 arch_spin_unlock(&ics->lock);
1258 local_irq_restore(flags);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001259
1260 if (val & KVM_XICS_PENDING)
1261 icp_deliver_irq(xics, NULL, irqp->number);
1262
1263 return 0;
1264}
1265
1266int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
1267 bool line_status)
1268{
1269 struct kvmppc_xics *xics = kvm->arch.xics;
1270
Paul Mackerrase48ba1c2016-08-10 11:13:09 +10001271 if (!xics)
1272 return -ENODEV;
Paul Mackerras25a2150b2014-06-30 20:51:14 +10001273 return ics_deliver_irq(xics, irq, level);
1274}
1275
Paul Mackerrasb1a42862016-05-04 21:07:52 +10001276int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *irq_entry,
1277 struct kvm *kvm, int irq_source_id,
1278 int level, bool line_status)
Paul Mackerras25a2150b2014-06-30 20:51:14 +10001279{
Paul Mackerras25a2150b2014-06-30 20:51:14 +10001280 return kvm_set_irq(kvm, irq_source_id, irq_entry->gsi,
1281 level, line_status);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001282}
1283
1284static int xics_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1285{
1286 struct kvmppc_xics *xics = dev->private;
1287
1288 switch (attr->group) {
1289 case KVM_DEV_XICS_GRP_SOURCES:
1290 return xics_set_source(xics, attr->attr, attr->addr);
1291 }
1292 return -ENXIO;
1293}
1294
1295static int xics_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1296{
1297 struct kvmppc_xics *xics = dev->private;
1298
1299 switch (attr->group) {
1300 case KVM_DEV_XICS_GRP_SOURCES:
1301 return xics_get_source(xics, attr->attr, attr->addr);
1302 }
1303 return -ENXIO;
1304}
1305
1306static int xics_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1307{
1308 switch (attr->group) {
1309 case KVM_DEV_XICS_GRP_SOURCES:
1310 if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
1311 attr->attr < KVMPPC_XICS_NR_IRQS)
1312 return 0;
1313 break;
1314 }
1315 return -ENXIO;
1316}
1317
1318static void kvmppc_xics_free(struct kvm_device *dev)
1319{
1320 struct kvmppc_xics *xics = dev->private;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001321 int i;
1322 struct kvm *kvm = xics->kvm;
1323
1324 debugfs_remove(xics->dentry);
1325
1326 if (kvm)
1327 kvm->arch.xics = NULL;
1328
1329 for (i = 0; i <= xics->max_icsid; i++)
1330 kfree(xics->ics[i]);
1331 kfree(xics);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001332 kfree(dev);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001333}
1334
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001335static int kvmppc_xics_create(struct kvm_device *dev, u32 type)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001336{
1337 struct kvmppc_xics *xics;
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001338 struct kvm *kvm = dev->kvm;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001339 int ret = 0;
1340
1341 xics = kzalloc(sizeof(*xics), GFP_KERNEL);
1342 if (!xics)
1343 return -ENOMEM;
1344
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001345 dev->private = xics;
1346 xics->dev = dev;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001347 xics->kvm = kvm;
1348
1349 /* Already there ? */
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001350 if (kvm->arch.xics)
1351 ret = -EEXIST;
1352 else
1353 kvm->arch.xics = xics;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001354
Gleb Natapov458ff3c2013-09-01 15:53:46 +03001355 if (ret) {
1356 kfree(xics);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001357 return ret;
Gleb Natapov458ff3c2013-09-01 15:53:46 +03001358 }
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001359
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301360#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001361 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
1362 /* Enable real mode support */
1363 xics->real_mode = ENABLE_REALMODE;
1364 xics->real_mode_dbg = DEBUG_REALMODE;
1365 }
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301366#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001367
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001368 return 0;
1369}
1370
Christoffer Dall023e9fd2016-08-09 19:13:00 +02001371static void kvmppc_xics_init(struct kvm_device *dev)
1372{
1373 struct kvmppc_xics *xics = (struct kvmppc_xics *)dev->private;
1374
1375 xics_debugfs_init(xics);
1376}
1377
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001378struct kvm_device_ops kvm_xics_ops = {
1379 .name = "kvm-xics",
1380 .create = kvmppc_xics_create,
Christoffer Dall023e9fd2016-08-09 19:13:00 +02001381 .init = kvmppc_xics_init,
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001382 .destroy = kvmppc_xics_free,
1383 .set_attr = xics_set_attr,
1384 .get_attr = xics_get_attr,
1385 .has_attr = xics_has_attr,
1386};
1387
1388int kvmppc_xics_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
1389 u32 xcpu)
1390{
1391 struct kvmppc_xics *xics = dev->private;
1392 int r = -EBUSY;
1393
1394 if (dev->ops != &kvm_xics_ops)
1395 return -EPERM;
1396 if (xics->kvm != vcpu->kvm)
1397 return -EPERM;
1398 if (vcpu->arch.irq_type)
1399 return -EBUSY;
1400
1401 r = kvmppc_xics_create_icp(vcpu, xcpu);
1402 if (!r)
1403 vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
1404
1405 return r;
1406}
1407
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001408void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu)
1409{
1410 if (!vcpu->arch.icp)
1411 return;
1412 kfree(vcpu->arch.icp);
1413 vcpu->arch.icp = NULL;
1414 vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
1415}
Paul Mackerras25a2150b2014-06-30 20:51:14 +10001416
1417static int xics_set_irq(struct kvm_kernel_irq_routing_entry *e,
1418 struct kvm *kvm, int irq_source_id, int level,
1419 bool line_status)
1420{
1421 return kvm_set_irq(kvm, irq_source_id, e->gsi, level, line_status);
1422}
1423
1424int kvm_irq_map_gsi(struct kvm *kvm,
1425 struct kvm_kernel_irq_routing_entry *entries, int gsi)
1426{
1427 entries->gsi = gsi;
1428 entries->type = KVM_IRQ_ROUTING_IRQCHIP;
1429 entries->set = xics_set_irq;
1430 entries->irqchip.irqchip = 0;
1431 entries->irqchip.pin = gsi;
1432 return 1;
1433}
1434
1435int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
1436{
1437 return pin;
1438}
Paul Mackerras5d375192016-08-19 15:35:56 +10001439
1440void kvmppc_xics_set_mapped(struct kvm *kvm, unsigned long irq,
1441 unsigned long host_irq)
1442{
1443 struct kvmppc_xics *xics = kvm->arch.xics;
1444 struct kvmppc_ics *ics;
1445 u16 idx;
1446
1447 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1448 if (!ics)
1449 return;
1450
1451 ics->irq_state[idx].host_irq = host_irq;
1452 ics->irq_state[idx].intr_cpu = -1;
1453}
1454EXPORT_SYMBOL_GPL(kvmppc_xics_set_mapped);
1455
1456void kvmppc_xics_clr_mapped(struct kvm *kvm, unsigned long irq,
1457 unsigned long host_irq)
1458{
1459 struct kvmppc_xics *xics = kvm->arch.xics;
1460 struct kvmppc_ics *ics;
1461 u16 idx;
1462
1463 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1464 if (!ics)
1465 return;
1466
1467 ics->irq_state[idx].host_irq = 0;
1468}
1469EXPORT_SYMBOL_GPL(kvmppc_xics_clr_mapped);