blob: a4a8d9f0dcb735d6d16057aeb81fc9e4ac1bb5e1 [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>
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000023
24#include <linux/debugfs.h>
25#include <linux/seq_file.h>
26
27#include "book3s_xics.h"
28
29#if 1
30#define XICS_DBG(fmt...) do { } while (0)
31#else
32#define XICS_DBG(fmt...) trace_printk(fmt)
33#endif
34
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +000035#define ENABLE_REALMODE true
36#define DEBUG_REALMODE false
37
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000038/*
39 * LOCKING
40 * =======
41 *
42 * Each ICS has a mutex protecting the information about the IRQ
43 * sources and avoiding simultaneous deliveries if the same interrupt.
44 *
45 * ICP operations are done via a single compare & swap transaction
46 * (most ICP state fits in the union kvmppc_icp_state)
47 */
48
49/*
50 * TODO
51 * ====
52 *
53 * - To speed up resends, keep a bitmap of "resend" set bits in the
54 * ICS
55 *
56 * - Speed up server# -> ICP lookup (array ? hash table ?)
57 *
58 * - Make ICS lockless as well, or at least a per-interrupt lock or hashed
59 * locks array to improve scalability
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000060 */
61
62/* -- ICS routines -- */
63
64static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
65 u32 new_irq);
66
Paul Mackerras25a2150b2014-06-30 20:51:14 +100067/*
68 * Return value ideally indicates how the interrupt was handled, but no
69 * callers look at it (given that we don't implement KVM_IRQ_LINE_STATUS),
70 * so just return 0.
71 */
72static int ics_deliver_irq(struct kvmppc_xics *xics, u32 irq, u32 level)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000073{
74 struct ics_irq_state *state;
75 struct kvmppc_ics *ics;
76 u16 src;
77
78 XICS_DBG("ics deliver %#x (level: %d)\n", irq, level);
79
80 ics = kvmppc_xics_find_ics(xics, irq, &src);
81 if (!ics) {
82 XICS_DBG("ics_deliver_irq: IRQ 0x%06x not found !\n", irq);
83 return -EINVAL;
84 }
85 state = &ics->irq_state[src];
86 if (!state->exists)
87 return -EINVAL;
88
89 /*
90 * We set state->asserted locklessly. This should be fine as
91 * we are the only setter, thus concurrent access is undefined
92 * to begin with.
93 */
Paul Mackerras25a2150b2014-06-30 20:51:14 +100094 if (level == 1 || level == KVM_INTERRUPT_SET_LEVEL)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000095 state->asserted = 1;
Paul Mackerras25a2150b2014-06-30 20:51:14 +100096 else if (level == 0 || level == KVM_INTERRUPT_UNSET) {
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +000097 state->asserted = 0;
98 return 0;
99 }
100
101 /* Attempt delivery */
102 icp_deliver_irq(xics, NULL, irq);
103
Paul Mackerras25a2150b2014-06-30 20:51:14 +1000104 return 0;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000105}
106
107static void ics_check_resend(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
108 struct kvmppc_icp *icp)
109{
110 int i;
111
112 mutex_lock(&ics->lock);
113
114 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
115 struct ics_irq_state *state = &ics->irq_state[i];
116
117 if (!state->resend)
118 continue;
119
120 XICS_DBG("resend %#x prio %#x\n", state->number,
121 state->priority);
122
123 mutex_unlock(&ics->lock);
124 icp_deliver_irq(xics, icp, state->number);
125 mutex_lock(&ics->lock);
126 }
127
128 mutex_unlock(&ics->lock);
129}
130
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000131static bool write_xive(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
132 struct ics_irq_state *state,
133 u32 server, u32 priority, u32 saved_priority)
134{
135 bool deliver;
136
137 mutex_lock(&ics->lock);
138
139 state->server = server;
140 state->priority = priority;
141 state->saved_priority = saved_priority;
142 deliver = false;
143 if ((state->masked_pending || state->resend) && priority != MASKED) {
144 state->masked_pending = 0;
145 deliver = true;
146 }
147
148 mutex_unlock(&ics->lock);
149
150 return deliver;
151}
152
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000153int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server, u32 priority)
154{
155 struct kvmppc_xics *xics = kvm->arch.xics;
156 struct kvmppc_icp *icp;
157 struct kvmppc_ics *ics;
158 struct ics_irq_state *state;
159 u16 src;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000160
161 if (!xics)
162 return -ENODEV;
163
164 ics = kvmppc_xics_find_ics(xics, irq, &src);
165 if (!ics)
166 return -EINVAL;
167 state = &ics->irq_state[src];
168
169 icp = kvmppc_xics_find_server(kvm, server);
170 if (!icp)
171 return -EINVAL;
172
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000173 XICS_DBG("set_xive %#x server %#x prio %#x MP:%d RS:%d\n",
174 irq, server, priority,
175 state->masked_pending, state->resend);
176
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000177 if (write_xive(xics, ics, state, server, priority, priority))
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000178 icp_deliver_irq(xics, icp, irq);
179
180 return 0;
181}
182
183int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, u32 *priority)
184{
185 struct kvmppc_xics *xics = kvm->arch.xics;
186 struct kvmppc_ics *ics;
187 struct ics_irq_state *state;
188 u16 src;
189
190 if (!xics)
191 return -ENODEV;
192
193 ics = kvmppc_xics_find_ics(xics, irq, &src);
194 if (!ics)
195 return -EINVAL;
196 state = &ics->irq_state[src];
197
198 mutex_lock(&ics->lock);
199 *server = state->server;
200 *priority = state->priority;
201 mutex_unlock(&ics->lock);
202
203 return 0;
204}
205
Paul Mackerrasd19bd8622013-04-17 20:32:04 +0000206int kvmppc_xics_int_on(struct kvm *kvm, u32 irq)
207{
208 struct kvmppc_xics *xics = kvm->arch.xics;
209 struct kvmppc_icp *icp;
210 struct kvmppc_ics *ics;
211 struct ics_irq_state *state;
212 u16 src;
213
214 if (!xics)
215 return -ENODEV;
216
217 ics = kvmppc_xics_find_ics(xics, irq, &src);
218 if (!ics)
219 return -EINVAL;
220 state = &ics->irq_state[src];
221
222 icp = kvmppc_xics_find_server(kvm, state->server);
223 if (!icp)
224 return -EINVAL;
225
226 if (write_xive(xics, ics, state, state->server, state->saved_priority,
227 state->saved_priority))
228 icp_deliver_irq(xics, icp, irq);
229
230 return 0;
231}
232
233int kvmppc_xics_int_off(struct kvm *kvm, u32 irq)
234{
235 struct kvmppc_xics *xics = kvm->arch.xics;
236 struct kvmppc_ics *ics;
237 struct ics_irq_state *state;
238 u16 src;
239
240 if (!xics)
241 return -ENODEV;
242
243 ics = kvmppc_xics_find_ics(xics, irq, &src);
244 if (!ics)
245 return -EINVAL;
246 state = &ics->irq_state[src];
247
248 write_xive(xics, ics, state, state->server, MASKED, state->priority);
249
250 return 0;
251}
252
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000253/* -- ICP routines, including hcalls -- */
254
255static inline bool icp_try_update(struct kvmppc_icp *icp,
256 union kvmppc_icp_state old,
257 union kvmppc_icp_state new,
258 bool change_self)
259{
260 bool success;
261
262 /* Calculate new output value */
263 new.out_ee = (new.xisr && (new.pending_pri < new.cppr));
264
265 /* Attempt atomic update */
266 success = cmpxchg64(&icp->state.raw, old.raw, new.raw) == old.raw;
267 if (!success)
268 goto bail;
269
270 XICS_DBG("UPD [%04x] - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
271 icp->server_num,
272 old.cppr, old.mfrr, old.pending_pri, old.xisr,
273 old.need_resend, old.out_ee);
274 XICS_DBG("UPD - C:%02x M:%02x PP: %02x PI:%06x R:%d O:%d\n",
275 new.cppr, new.mfrr, new.pending_pri, new.xisr,
276 new.need_resend, new.out_ee);
277 /*
278 * Check for output state update
279 *
280 * Note that this is racy since another processor could be updating
281 * the state already. This is why we never clear the interrupt output
282 * here, we only ever set it. The clear only happens prior to doing
283 * an update and only by the processor itself. Currently we do it
284 * in Accept (H_XIRR) and Up_Cppr (H_XPPR).
285 *
286 * We also do not try to figure out whether the EE state has changed,
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000287 * we unconditionally set it if the new state calls for it. The reason
288 * for that is that we opportunistically remove the pending interrupt
289 * flag when raising CPPR, so we need to set it back here if an
290 * interrupt is still pending.
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000291 */
292 if (new.out_ee) {
293 kvmppc_book3s_queue_irqprio(icp->vcpu,
294 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
295 if (!change_self)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000296 kvmppc_fast_vcpu_kick(icp->vcpu);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000297 }
298 bail:
299 return success;
300}
301
302static void icp_check_resend(struct kvmppc_xics *xics,
303 struct kvmppc_icp *icp)
304{
305 u32 icsid;
306
307 /* Order this load with the test for need_resend in the caller */
308 smp_rmb();
309 for_each_set_bit(icsid, icp->resend_map, xics->max_icsid + 1) {
310 struct kvmppc_ics *ics = xics->ics[icsid];
311
312 if (!test_and_clear_bit(icsid, icp->resend_map))
313 continue;
314 if (!ics)
315 continue;
316 ics_check_resend(xics, ics, icp);
317 }
318}
319
320static bool icp_try_to_deliver(struct kvmppc_icp *icp, u32 irq, u8 priority,
321 u32 *reject)
322{
323 union kvmppc_icp_state old_state, new_state;
324 bool success;
325
326 XICS_DBG("try deliver %#x(P:%#x) to server %#x\n", irq, priority,
327 icp->server_num);
328
329 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100330 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000331
332 *reject = 0;
333
334 /* See if we can deliver */
335 success = new_state.cppr > priority &&
336 new_state.mfrr > priority &&
337 new_state.pending_pri > priority;
338
339 /*
340 * If we can, check for a rejection and perform the
341 * delivery
342 */
343 if (success) {
344 *reject = new_state.xisr;
345 new_state.xisr = irq;
346 new_state.pending_pri = priority;
347 } else {
348 /*
349 * If we failed to deliver we set need_resend
350 * so a subsequent CPPR state change causes us
351 * to try a new delivery.
352 */
353 new_state.need_resend = true;
354 }
355
356 } while (!icp_try_update(icp, old_state, new_state, false));
357
358 return success;
359}
360
361static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
362 u32 new_irq)
363{
364 struct ics_irq_state *state;
365 struct kvmppc_ics *ics;
366 u32 reject;
367 u16 src;
368
369 /*
370 * This is used both for initial delivery of an interrupt and
371 * for subsequent rejection.
372 *
373 * Rejection can be racy vs. resends. We have evaluated the
374 * rejection in an atomic ICP transaction which is now complete,
375 * so potentially the ICP can already accept the interrupt again.
376 *
377 * So we need to retry the delivery. Essentially the reject path
378 * boils down to a failed delivery. Always.
379 *
380 * Now the interrupt could also have moved to a different target,
381 * thus we may need to re-do the ICP lookup as well
382 */
383
384 again:
385 /* Get the ICS state and lock it */
386 ics = kvmppc_xics_find_ics(xics, new_irq, &src);
387 if (!ics) {
388 XICS_DBG("icp_deliver_irq: IRQ 0x%06x not found !\n", new_irq);
389 return;
390 }
391 state = &ics->irq_state[src];
392
393 /* Get a lock on the ICS */
394 mutex_lock(&ics->lock);
395
396 /* Get our server */
397 if (!icp || state->server != icp->server_num) {
398 icp = kvmppc_xics_find_server(xics->kvm, state->server);
399 if (!icp) {
400 pr_warn("icp_deliver_irq: IRQ 0x%06x server 0x%x not found !\n",
401 new_irq, state->server);
402 goto out;
403 }
404 }
405
406 /* Clear the resend bit of that interrupt */
407 state->resend = 0;
408
409 /*
410 * If masked, bail out
411 *
412 * Note: PAPR doesn't mention anything about masked pending
413 * when doing a resend, only when doing a delivery.
414 *
415 * However that would have the effect of losing a masked
416 * interrupt that was rejected and isn't consistent with
417 * the whole masked_pending business which is about not
418 * losing interrupts that occur while masked.
419 *
420 * I don't differenciate normal deliveries and resends, this
421 * implementation will differ from PAPR and not lose such
422 * interrupts.
423 */
424 if (state->priority == MASKED) {
425 XICS_DBG("irq %#x masked pending\n", new_irq);
426 state->masked_pending = 1;
427 goto out;
428 }
429
430 /*
431 * Try the delivery, this will set the need_resend flag
432 * in the ICP as part of the atomic transaction if the
433 * delivery is not possible.
434 *
435 * Note that if successful, the new delivery might have itself
436 * rejected an interrupt that was "delivered" before we took the
437 * icp mutex.
438 *
439 * In this case we do the whole sequence all over again for the
440 * new guy. We cannot assume that the rejected interrupt is less
441 * favored than the new one, and thus doesn't need to be delivered,
442 * because by the time we exit icp_try_to_deliver() the target
443 * processor may well have alrady consumed & completed it, and thus
444 * the rejected interrupt might actually be already acceptable.
445 */
446 if (icp_try_to_deliver(icp, new_irq, state->priority, &reject)) {
447 /*
448 * Delivery was successful, did we reject somebody else ?
449 */
450 if (reject && reject != XICS_IPI) {
451 mutex_unlock(&ics->lock);
452 new_irq = reject;
453 goto again;
454 }
455 } else {
456 /*
457 * We failed to deliver the interrupt we need to set the
458 * resend map bit and mark the ICS state as needing a resend
459 */
460 set_bit(ics->icsid, icp->resend_map);
461 state->resend = 1;
462
463 /*
464 * If the need_resend flag got cleared in the ICP some time
465 * between icp_try_to_deliver() atomic update and now, then
466 * we know it might have missed the resend_map bit. So we
467 * retry
468 */
469 smp_mb();
470 if (!icp->state.need_resend) {
471 mutex_unlock(&ics->lock);
472 goto again;
473 }
474 }
475 out:
476 mutex_unlock(&ics->lock);
477}
478
479static void icp_down_cppr(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
480 u8 new_cppr)
481{
482 union kvmppc_icp_state old_state, new_state;
483 bool resend;
484
485 /*
486 * This handles several related states in one operation:
487 *
488 * ICP State: Down_CPPR
489 *
490 * Load CPPR with new value and if the XISR is 0
491 * then check for resends:
492 *
493 * ICP State: Resend
494 *
495 * If MFRR is more favored than CPPR, check for IPIs
496 * and notify ICS of a potential resend. This is done
497 * asynchronously (when used in real mode, we will have
498 * to exit here).
499 *
500 * We do not handle the complete Check_IPI as documented
501 * here. In the PAPR, this state will be used for both
502 * Set_MFRR and Down_CPPR. However, we know that we aren't
503 * changing the MFRR state here so we don't need to handle
504 * the case of an MFRR causing a reject of a pending irq,
505 * this will have been handled when the MFRR was set in the
506 * first place.
507 *
508 * Thus we don't have to handle rejects, only resends.
509 *
510 * When implementing real mode for HV KVM, resend will lead to
511 * a H_TOO_HARD return and the whole transaction will be handled
512 * in virtual mode.
513 */
514 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100515 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000516
517 /* Down_CPPR */
518 new_state.cppr = new_cppr;
519
520 /*
521 * Cut down Resend / Check_IPI / IPI
522 *
523 * The logic is that we cannot have a pending interrupt
524 * trumped by an IPI at this point (see above), so we
525 * know that either the pending interrupt is already an
526 * IPI (in which case we don't care to override it) or
527 * it's either more favored than us or non existent
528 */
529 if (new_state.mfrr < new_cppr &&
530 new_state.mfrr <= new_state.pending_pri) {
531 WARN_ON(new_state.xisr != XICS_IPI &&
532 new_state.xisr != 0);
533 new_state.pending_pri = new_state.mfrr;
534 new_state.xisr = XICS_IPI;
535 }
536
537 /* Latch/clear resend bit */
538 resend = new_state.need_resend;
539 new_state.need_resend = 0;
540
541 } while (!icp_try_update(icp, old_state, new_state, true));
542
543 /*
544 * Now handle resend checks. Those are asynchronous to the ICP
545 * state update in HW (ie bus transactions) so we can handle them
546 * separately here too
547 */
548 if (resend)
549 icp_check_resend(xics, icp);
550}
551
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000552static noinline unsigned long kvmppc_h_xirr(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000553{
554 union kvmppc_icp_state old_state, new_state;
555 struct kvmppc_icp *icp = vcpu->arch.icp;
556 u32 xirr;
557
558 /* First, remove EE from the processor */
559 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
560 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
561
562 /*
563 * ICP State: Accept_Interrupt
564 *
565 * Return the pending interrupt (if any) along with the
566 * current CPPR, then clear the XISR & set CPPR to the
567 * pending priority
568 */
569 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100570 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000571
572 xirr = old_state.xisr | (((u32)old_state.cppr) << 24);
573 if (!old_state.xisr)
574 break;
575 new_state.cppr = new_state.pending_pri;
576 new_state.pending_pri = 0xff;
577 new_state.xisr = 0;
578
579 } while (!icp_try_update(icp, old_state, new_state, true));
580
581 XICS_DBG("h_xirr vcpu %d xirr %#x\n", vcpu->vcpu_id, xirr);
582
583 return xirr;
584}
585
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000586static noinline int kvmppc_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
587 unsigned long mfrr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000588{
589 union kvmppc_icp_state old_state, new_state;
590 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
591 struct kvmppc_icp *icp;
592 u32 reject;
593 bool resend;
594 bool local;
595
596 XICS_DBG("h_ipi vcpu %d to server %lu mfrr %#lx\n",
597 vcpu->vcpu_id, server, mfrr);
598
599 icp = vcpu->arch.icp;
600 local = icp->server_num == server;
601 if (!local) {
602 icp = kvmppc_xics_find_server(vcpu->kvm, server);
603 if (!icp)
604 return H_PARAMETER;
605 }
606
607 /*
608 * ICP state: Set_MFRR
609 *
610 * If the CPPR is more favored than the new MFRR, then
611 * nothing needs to be rejected as there can be no XISR to
612 * reject. If the MFRR is being made less favored then
613 * there might be a previously-rejected interrupt needing
614 * to be resent.
615 *
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000616 * ICP state: Check_IPI
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100617 *
618 * If the CPPR is less favored, then we might be replacing
619 * an interrupt, and thus need to possibly reject it.
620 *
621 * ICP State: IPI
622 *
623 * Besides rejecting any pending interrupts, we also
624 * update XISR and pending_pri to mark IPI as pending.
625 *
626 * PAPR does not describe this state, but if the MFRR is being
627 * made less favored than its earlier value, there might be
628 * a previously-rejected interrupt needing to be resent.
629 * Ideally, we would want to resend only if
630 * prio(pending_interrupt) < mfrr &&
631 * prio(pending_interrupt) < cppr
632 * where pending interrupt is the one that was rejected. But
633 * we don't have that state, so we simply trigger a resend
634 * whenever the MFRR is made less favored.
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000635 */
636 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100637 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000638
639 /* Set_MFRR */
640 new_state.mfrr = mfrr;
641
642 /* Check_IPI */
643 reject = 0;
644 resend = false;
645 if (mfrr < new_state.cppr) {
646 /* Reject a pending interrupt if not an IPI */
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100647 if (mfrr <= new_state.pending_pri) {
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000648 reject = new_state.xisr;
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100649 new_state.pending_pri = mfrr;
650 new_state.xisr = XICS_IPI;
651 }
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000652 }
653
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100654 if (mfrr > old_state.mfrr) {
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000655 resend = new_state.need_resend;
656 new_state.need_resend = 0;
657 }
658 } while (!icp_try_update(icp, old_state, new_state, local));
659
660 /* Handle reject */
661 if (reject && reject != XICS_IPI)
662 icp_deliver_irq(xics, icp, reject);
663
664 /* Handle resend */
665 if (resend)
666 icp_check_resend(xics, icp);
667
668 return H_SUCCESS;
669}
670
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000671static int kvmppc_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server)
672{
673 union kvmppc_icp_state state;
674 struct kvmppc_icp *icp;
675
676 icp = vcpu->arch.icp;
677 if (icp->server_num != server) {
678 icp = kvmppc_xics_find_server(vcpu->kvm, server);
679 if (!icp)
680 return H_PARAMETER;
681 }
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100682 state = READ_ONCE(icp->state);
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000683 kvmppc_set_gpr(vcpu, 4, ((u32)state.cppr << 24) | state.xisr);
684 kvmppc_set_gpr(vcpu, 5, state.mfrr);
685 return H_SUCCESS;
686}
687
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000688static noinline void kvmppc_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000689{
690 union kvmppc_icp_state old_state, new_state;
691 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
692 struct kvmppc_icp *icp = vcpu->arch.icp;
693 u32 reject;
694
695 XICS_DBG("h_cppr vcpu %d cppr %#lx\n", vcpu->vcpu_id, cppr);
696
697 /*
698 * ICP State: Set_CPPR
699 *
700 * We can safely compare the new value with the current
701 * value outside of the transaction as the CPPR is only
702 * ever changed by the processor on itself
703 */
704 if (cppr > icp->state.cppr)
705 icp_down_cppr(xics, icp, cppr);
706 else if (cppr == icp->state.cppr)
707 return;
708
709 /*
710 * ICP State: Up_CPPR
711 *
712 * The processor is raising its priority, this can result
713 * in a rejection of a pending interrupt:
714 *
715 * ICP State: Reject_Current
716 *
717 * We can remove EE from the current processor, the update
718 * transaction will set it again if needed
719 */
720 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
721 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
722
723 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100724 old_state = new_state = READ_ONCE(icp->state);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000725
726 reject = 0;
727 new_state.cppr = cppr;
728
729 if (cppr <= new_state.pending_pri) {
730 reject = new_state.xisr;
731 new_state.xisr = 0;
732 new_state.pending_pri = 0xff;
733 }
734
735 } while (!icp_try_update(icp, old_state, new_state, true));
736
737 /*
738 * Check for rejects. They are handled by doing a new delivery
739 * attempt (see comments in icp_deliver_irq).
740 */
741 if (reject && reject != XICS_IPI)
742 icp_deliver_irq(xics, icp, reject);
743}
744
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000745static noinline int kvmppc_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000746{
747 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
748 struct kvmppc_icp *icp = vcpu->arch.icp;
749 struct kvmppc_ics *ics;
750 struct ics_irq_state *state;
751 u32 irq = xirr & 0x00ffffff;
752 u16 src;
753
754 XICS_DBG("h_eoi vcpu %d eoi %#lx\n", vcpu->vcpu_id, xirr);
755
756 /*
757 * ICP State: EOI
758 *
759 * Note: If EOI is incorrectly used by SW to lower the CPPR
760 * value (ie more favored), we do not check for rejection of
761 * a pending interrupt, this is a SW error and PAPR sepcifies
762 * that we don't have to deal with it.
763 *
764 * The sending of an EOI to the ICS is handled after the
765 * CPPR update
766 *
767 * ICP State: Down_CPPR which we handle
768 * in a separate function as it's shared with H_CPPR.
769 */
770 icp_down_cppr(xics, icp, xirr >> 24);
771
772 /* IPIs have no EOI */
773 if (irq == XICS_IPI)
774 return H_SUCCESS;
775 /*
776 * EOI handling: If the interrupt is still asserted, we need to
777 * resend it. We can take a lockless "peek" at the ICS state here.
778 *
779 * "Message" interrupts will never have "asserted" set
780 */
781 ics = kvmppc_xics_find_ics(xics, irq, &src);
782 if (!ics) {
783 XICS_DBG("h_eoi: IRQ 0x%06x not found !\n", irq);
784 return H_PARAMETER;
785 }
786 state = &ics->irq_state[src];
787
788 /* Still asserted, resend it */
789 if (state->asserted)
790 icp_deliver_irq(xics, icp, irq);
791
Paul Mackerras25a2150b2014-06-30 20:51:14 +1000792 kvm_notify_acked_irq(vcpu->kvm, 0, irq);
793
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000794 return H_SUCCESS;
795}
796
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000797static noinline int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
798{
799 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
800 struct kvmppc_icp *icp = vcpu->arch.icp;
801
802 XICS_DBG("XICS_RM: H_%x completing, act: %x state: %lx tgt: %p\n",
803 hcall, icp->rm_action, icp->rm_dbgstate.raw, icp->rm_dbgtgt);
804
805 if (icp->rm_action & XICS_RM_KICK_VCPU)
806 kvmppc_fast_vcpu_kick(icp->rm_kick_target);
807 if (icp->rm_action & XICS_RM_CHECK_RESEND)
Suresh E. Warrier5b88cda2014-11-03 15:51:59 +1100808 icp_check_resend(xics, icp->rm_resend_icp);
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000809 if (icp->rm_action & XICS_RM_REJECT)
810 icp_deliver_irq(xics, icp, icp->rm_reject);
Paul Mackerras25a2150b2014-06-30 20:51:14 +1000811 if (icp->rm_action & XICS_RM_NOTIFY_EOI)
812 kvm_notify_acked_irq(vcpu->kvm, 0, icp->rm_eoied_irq);
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000813
814 icp->rm_action = 0;
815
816 return H_SUCCESS;
817}
818
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000819int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 req)
820{
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000821 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000822 unsigned long res;
823 int rc = H_SUCCESS;
824
825 /* Check if we have an ICP */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000826 if (!xics || !vcpu->arch.icp)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000827 return H_HARDWARE;
828
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000829 /* These requests don't have real-mode implementations at present */
830 switch (req) {
831 case H_XIRR_X:
832 res = kvmppc_h_xirr(vcpu);
833 kvmppc_set_gpr(vcpu, 4, res);
834 kvmppc_set_gpr(vcpu, 5, get_tb());
835 return rc;
836 case H_IPOLL:
837 rc = kvmppc_h_ipoll(vcpu, kvmppc_get_gpr(vcpu, 4));
838 return rc;
839 }
840
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000841 /* Check for real mode returning too hard */
Aneesh Kumar K.Va78b55d2013-10-07 22:18:02 +0530842 if (xics->real_mode && is_kvmppc_hv_enabled(vcpu->kvm))
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000843 return kvmppc_xics_rm_complete(vcpu, req);
844
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000845 switch (req) {
846 case H_XIRR:
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000847 res = kvmppc_h_xirr(vcpu);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000848 kvmppc_set_gpr(vcpu, 4, res);
849 break;
850 case H_CPPR:
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000851 kvmppc_h_cppr(vcpu, kvmppc_get_gpr(vcpu, 4));
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000852 break;
853 case H_EOI:
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000854 rc = kvmppc_h_eoi(vcpu, kvmppc_get_gpr(vcpu, 4));
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000855 break;
856 case H_IPI:
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +0000857 rc = kvmppc_h_ipi(vcpu, kvmppc_get_gpr(vcpu, 4),
858 kvmppc_get_gpr(vcpu, 5));
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000859 break;
860 }
861
862 return rc;
863}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530864EXPORT_SYMBOL_GPL(kvmppc_xics_hcall);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000865
866
867/* -- Initialisation code etc. -- */
868
869static int xics_debug_show(struct seq_file *m, void *private)
870{
871 struct kvmppc_xics *xics = m->private;
872 struct kvm *kvm = xics->kvm;
873 struct kvm_vcpu *vcpu;
874 int icsid, i;
875
876 if (!kvm)
877 return 0;
878
879 seq_printf(m, "=========\nICP state\n=========\n");
880
881 kvm_for_each_vcpu(i, vcpu, kvm) {
882 struct kvmppc_icp *icp = vcpu->arch.icp;
883 union kvmppc_icp_state state;
884
885 if (!icp)
886 continue;
887
Christian Borntraeger5ee07612015-01-06 22:41:46 +0100888 state.raw = READ_ONCE(icp->state.raw);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000889 seq_printf(m, "cpu server %#lx XIRR:%#x PPRI:%#x CPPR:%#x MFRR:%#x OUT:%d NR:%d\n",
890 icp->server_num, state.xisr,
891 state.pending_pri, state.cppr, state.mfrr,
892 state.out_ee, state.need_resend);
893 }
894
895 for (icsid = 0; icsid <= KVMPPC_XICS_MAX_ICS_ID; icsid++) {
896 struct kvmppc_ics *ics = xics->ics[icsid];
897
898 if (!ics)
899 continue;
900
901 seq_printf(m, "=========\nICS state for ICS 0x%x\n=========\n",
902 icsid);
903
904 mutex_lock(&ics->lock);
905
906 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
907 struct ics_irq_state *irq = &ics->irq_state[i];
908
909 seq_printf(m, "irq 0x%06x: server %#x prio %#x save prio %#x asserted %d resend %d masked pending %d\n",
910 irq->number, irq->server, irq->priority,
911 irq->saved_priority, irq->asserted,
912 irq->resend, irq->masked_pending);
913
914 }
915 mutex_unlock(&ics->lock);
916 }
917 return 0;
918}
919
920static int xics_debug_open(struct inode *inode, struct file *file)
921{
922 return single_open(file, xics_debug_show, inode->i_private);
923}
924
925static const struct file_operations xics_debug_fops = {
926 .open = xics_debug_open,
927 .read = seq_read,
928 .llseek = seq_lseek,
929 .release = single_release,
930};
931
932static void xics_debugfs_init(struct kvmppc_xics *xics)
933{
934 char *name;
935
936 name = kasprintf(GFP_KERNEL, "kvm-xics-%p", xics);
937 if (!name) {
938 pr_err("%s: no memory for name\n", __func__);
939 return;
940 }
941
942 xics->dentry = debugfs_create_file(name, S_IRUGO, powerpc_debugfs_root,
943 xics, &xics_debug_fops);
944
945 pr_debug("%s: created %s\n", __func__, name);
946 kfree(name);
947}
948
Paul Mackerras5975a2e2013-04-27 00:28:37 +0000949static struct kvmppc_ics *kvmppc_xics_create_ics(struct kvm *kvm,
950 struct kvmppc_xics *xics, int irq)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000951{
952 struct kvmppc_ics *ics;
953 int i, icsid;
954
955 icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
956
957 mutex_lock(&kvm->lock);
958
959 /* ICS already exists - somebody else got here first */
960 if (xics->ics[icsid])
961 goto out;
962
963 /* Create the ICS */
964 ics = kzalloc(sizeof(struct kvmppc_ics), GFP_KERNEL);
965 if (!ics)
966 goto out;
967
968 mutex_init(&ics->lock);
969 ics->icsid = icsid;
970
971 for (i = 0; i < KVMPPC_XICS_IRQ_PER_ICS; i++) {
972 ics->irq_state[i].number = (icsid << KVMPPC_XICS_ICS_SHIFT) | i;
973 ics->irq_state[i].priority = MASKED;
974 ics->irq_state[i].saved_priority = MASKED;
975 }
976 smp_wmb();
977 xics->ics[icsid] = ics;
978
979 if (icsid > xics->max_icsid)
980 xics->max_icsid = icsid;
981
982 out:
983 mutex_unlock(&kvm->lock);
984 return xics->ics[icsid];
985}
986
987int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server_num)
988{
989 struct kvmppc_icp *icp;
990
991 if (!vcpu->kvm->arch.xics)
992 return -ENODEV;
993
994 if (kvmppc_xics_find_server(vcpu->kvm, server_num))
995 return -EEXIST;
996
997 icp = kzalloc(sizeof(struct kvmppc_icp), GFP_KERNEL);
998 if (!icp)
999 return -ENOMEM;
1000
1001 icp->vcpu = vcpu;
1002 icp->server_num = server_num;
1003 icp->state.mfrr = MASKED;
1004 icp->state.pending_pri = MASKED;
1005 vcpu->arch.icp = icp;
1006
1007 XICS_DBG("created server for vcpu %d\n", vcpu->vcpu_id);
1008
1009 return 0;
1010}
1011
Paul Mackerras8b786452013-04-17 20:32:26 +00001012u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu)
1013{
1014 struct kvmppc_icp *icp = vcpu->arch.icp;
1015 union kvmppc_icp_state state;
1016
1017 if (!icp)
1018 return 0;
1019 state = icp->state;
1020 return ((u64)state.cppr << KVM_REG_PPC_ICP_CPPR_SHIFT) |
1021 ((u64)state.xisr << KVM_REG_PPC_ICP_XISR_SHIFT) |
1022 ((u64)state.mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT) |
1023 ((u64)state.pending_pri << KVM_REG_PPC_ICP_PPRI_SHIFT);
1024}
1025
1026int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
1027{
1028 struct kvmppc_icp *icp = vcpu->arch.icp;
1029 struct kvmppc_xics *xics = vcpu->kvm->arch.xics;
1030 union kvmppc_icp_state old_state, new_state;
1031 struct kvmppc_ics *ics;
1032 u8 cppr, mfrr, pending_pri;
1033 u32 xisr;
1034 u16 src;
1035 bool resend;
1036
1037 if (!icp || !xics)
1038 return -ENOENT;
1039
1040 cppr = icpval >> KVM_REG_PPC_ICP_CPPR_SHIFT;
1041 xisr = (icpval >> KVM_REG_PPC_ICP_XISR_SHIFT) &
1042 KVM_REG_PPC_ICP_XISR_MASK;
1043 mfrr = icpval >> KVM_REG_PPC_ICP_MFRR_SHIFT;
1044 pending_pri = icpval >> KVM_REG_PPC_ICP_PPRI_SHIFT;
1045
1046 /* Require the new state to be internally consistent */
1047 if (xisr == 0) {
1048 if (pending_pri != 0xff)
1049 return -EINVAL;
1050 } else if (xisr == XICS_IPI) {
1051 if (pending_pri != mfrr || pending_pri >= cppr)
1052 return -EINVAL;
1053 } else {
1054 if (pending_pri >= mfrr || pending_pri >= cppr)
1055 return -EINVAL;
1056 ics = kvmppc_xics_find_ics(xics, xisr, &src);
1057 if (!ics)
1058 return -EINVAL;
1059 }
1060
1061 new_state.raw = 0;
1062 new_state.cppr = cppr;
1063 new_state.xisr = xisr;
1064 new_state.mfrr = mfrr;
1065 new_state.pending_pri = pending_pri;
1066
1067 /*
1068 * Deassert the CPU interrupt request.
1069 * icp_try_update will reassert it if necessary.
1070 */
1071 kvmppc_book3s_dequeue_irqprio(icp->vcpu,
1072 BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
1073
1074 /*
1075 * Note that if we displace an interrupt from old_state.xisr,
1076 * we don't mark it as rejected. We expect userspace to set
1077 * the state of the interrupt sources to be consistent with
1078 * the ICP states (either before or afterwards, which doesn't
1079 * matter). We do handle resends due to CPPR becoming less
1080 * favoured because that is necessary to end up with a
1081 * consistent state in the situation where userspace restores
1082 * the ICS states before the ICP states.
1083 */
1084 do {
Christian Borntraeger5ee07612015-01-06 22:41:46 +01001085 old_state = READ_ONCE(icp->state);
Paul Mackerras8b786452013-04-17 20:32:26 +00001086
1087 if (new_state.mfrr <= old_state.mfrr) {
1088 resend = false;
1089 new_state.need_resend = old_state.need_resend;
1090 } else {
1091 resend = old_state.need_resend;
1092 new_state.need_resend = 0;
1093 }
1094 } while (!icp_try_update(icp, old_state, new_state, false));
1095
1096 if (resend)
1097 icp_check_resend(xics, icp);
1098
1099 return 0;
1100}
1101
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001102static int xics_get_source(struct kvmppc_xics *xics, long irq, u64 addr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001103{
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001104 int ret;
1105 struct kvmppc_ics *ics;
1106 struct ics_irq_state *irqp;
1107 u64 __user *ubufp = (u64 __user *) addr;
1108 u16 idx;
1109 u64 val, prio;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001110
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001111 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1112 if (!ics)
1113 return -ENOENT;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001114
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001115 irqp = &ics->irq_state[idx];
1116 mutex_lock(&ics->lock);
1117 ret = -ENOENT;
1118 if (irqp->exists) {
1119 val = irqp->server;
1120 prio = irqp->priority;
1121 if (prio == MASKED) {
1122 val |= KVM_XICS_MASKED;
1123 prio = irqp->saved_priority;
1124 }
1125 val |= prio << KVM_XICS_PRIORITY_SHIFT;
1126 if (irqp->asserted)
1127 val |= KVM_XICS_LEVEL_SENSITIVE | KVM_XICS_PENDING;
1128 else if (irqp->masked_pending || irqp->resend)
1129 val |= KVM_XICS_PENDING;
1130 ret = 0;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001131 }
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001132 mutex_unlock(&ics->lock);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001133
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001134 if (!ret && put_user(val, ubufp))
1135 ret = -EFAULT;
1136
1137 return ret;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001138}
1139
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001140static int xics_set_source(struct kvmppc_xics *xics, long irq, u64 addr)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001141{
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001142 struct kvmppc_ics *ics;
1143 struct ics_irq_state *irqp;
1144 u64 __user *ubufp = (u64 __user *) addr;
1145 u16 idx;
1146 u64 val;
1147 u8 prio;
1148 u32 server;
1149
1150 if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
1151 return -ENOENT;
1152
1153 ics = kvmppc_xics_find_ics(xics, irq, &idx);
1154 if (!ics) {
1155 ics = kvmppc_xics_create_ics(xics->kvm, xics, irq);
1156 if (!ics)
1157 return -ENOMEM;
1158 }
1159 irqp = &ics->irq_state[idx];
1160 if (get_user(val, ubufp))
1161 return -EFAULT;
1162
1163 server = val & KVM_XICS_DESTINATION_MASK;
1164 prio = val >> KVM_XICS_PRIORITY_SHIFT;
1165 if (prio != MASKED &&
1166 kvmppc_xics_find_server(xics->kvm, server) == NULL)
1167 return -EINVAL;
1168
1169 mutex_lock(&ics->lock);
1170 irqp->server = server;
1171 irqp->saved_priority = prio;
1172 if (val & KVM_XICS_MASKED)
1173 prio = MASKED;
1174 irqp->priority = prio;
1175 irqp->resend = 0;
1176 irqp->masked_pending = 0;
1177 irqp->asserted = 0;
1178 if ((val & KVM_XICS_PENDING) && (val & KVM_XICS_LEVEL_SENSITIVE))
1179 irqp->asserted = 1;
1180 irqp->exists = 1;
1181 mutex_unlock(&ics->lock);
1182
1183 if (val & KVM_XICS_PENDING)
1184 icp_deliver_irq(xics, NULL, irqp->number);
1185
1186 return 0;
1187}
1188
1189int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
1190 bool line_status)
1191{
1192 struct kvmppc_xics *xics = kvm->arch.xics;
1193
Paul Mackerras25a2150b2014-06-30 20:51:14 +10001194 return ics_deliver_irq(xics, irq, level);
1195}
1196
1197int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
1198 int irq_source_id, int level, bool line_status)
1199{
1200 if (!level)
1201 return -1;
1202 return kvm_set_irq(kvm, irq_source_id, irq_entry->gsi,
1203 level, line_status);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001204}
1205
1206static int xics_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1207{
1208 struct kvmppc_xics *xics = dev->private;
1209
1210 switch (attr->group) {
1211 case KVM_DEV_XICS_GRP_SOURCES:
1212 return xics_set_source(xics, attr->attr, attr->addr);
1213 }
1214 return -ENXIO;
1215}
1216
1217static int xics_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1218{
1219 struct kvmppc_xics *xics = dev->private;
1220
1221 switch (attr->group) {
1222 case KVM_DEV_XICS_GRP_SOURCES:
1223 return xics_get_source(xics, attr->attr, attr->addr);
1224 }
1225 return -ENXIO;
1226}
1227
1228static int xics_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1229{
1230 switch (attr->group) {
1231 case KVM_DEV_XICS_GRP_SOURCES:
1232 if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
1233 attr->attr < KVMPPC_XICS_NR_IRQS)
1234 return 0;
1235 break;
1236 }
1237 return -ENXIO;
1238}
1239
1240static void kvmppc_xics_free(struct kvm_device *dev)
1241{
1242 struct kvmppc_xics *xics = dev->private;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001243 int i;
1244 struct kvm *kvm = xics->kvm;
1245
1246 debugfs_remove(xics->dentry);
1247
1248 if (kvm)
1249 kvm->arch.xics = NULL;
1250
1251 for (i = 0; i <= xics->max_icsid; i++)
1252 kfree(xics->ics[i]);
1253 kfree(xics);
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001254 kfree(dev);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001255}
1256
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001257static int kvmppc_xics_create(struct kvm_device *dev, u32 type)
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001258{
1259 struct kvmppc_xics *xics;
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001260 struct kvm *kvm = dev->kvm;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001261 int ret = 0;
1262
1263 xics = kzalloc(sizeof(*xics), GFP_KERNEL);
1264 if (!xics)
1265 return -ENOMEM;
1266
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001267 dev->private = xics;
1268 xics->dev = dev;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001269 xics->kvm = kvm;
1270
1271 /* Already there ? */
1272 mutex_lock(&kvm->lock);
1273 if (kvm->arch.xics)
1274 ret = -EEXIST;
1275 else
1276 kvm->arch.xics = xics;
1277 mutex_unlock(&kvm->lock);
1278
Gleb Natapov458ff3c2013-09-01 15:53:46 +03001279 if (ret) {
1280 kfree(xics);
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001281 return ret;
Gleb Natapov458ff3c2013-09-01 15:53:46 +03001282 }
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001283
1284 xics_debugfs_init(xics);
1285
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301286#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001287 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
1288 /* Enable real mode support */
1289 xics->real_mode = ENABLE_REALMODE;
1290 xics->real_mode_dbg = DEBUG_REALMODE;
1291 }
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301292#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001293
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001294 return 0;
1295}
1296
Paul Mackerras5975a2e2013-04-27 00:28:37 +00001297struct kvm_device_ops kvm_xics_ops = {
1298 .name = "kvm-xics",
1299 .create = kvmppc_xics_create,
1300 .destroy = kvmppc_xics_free,
1301 .set_attr = xics_set_attr,
1302 .get_attr = xics_get_attr,
1303 .has_attr = xics_has_attr,
1304};
1305
1306int kvmppc_xics_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
1307 u32 xcpu)
1308{
1309 struct kvmppc_xics *xics = dev->private;
1310 int r = -EBUSY;
1311
1312 if (dev->ops != &kvm_xics_ops)
1313 return -EPERM;
1314 if (xics->kvm != vcpu->kvm)
1315 return -EPERM;
1316 if (vcpu->arch.irq_type)
1317 return -EBUSY;
1318
1319 r = kvmppc_xics_create_icp(vcpu, xcpu);
1320 if (!r)
1321 vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
1322
1323 return r;
1324}
1325
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001326void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu)
1327{
1328 if (!vcpu->arch.icp)
1329 return;
1330 kfree(vcpu->arch.icp);
1331 vcpu->arch.icp = NULL;
1332 vcpu->arch.irq_type = KVMPPC_IRQ_DEFAULT;
1333}
Paul Mackerras25a2150b2014-06-30 20:51:14 +10001334
1335static int xics_set_irq(struct kvm_kernel_irq_routing_entry *e,
1336 struct kvm *kvm, int irq_source_id, int level,
1337 bool line_status)
1338{
1339 return kvm_set_irq(kvm, irq_source_id, e->gsi, level, line_status);
1340}
1341
1342int kvm_irq_map_gsi(struct kvm *kvm,
1343 struct kvm_kernel_irq_routing_entry *entries, int gsi)
1344{
1345 entries->gsi = gsi;
1346 entries->type = KVM_IRQ_ROUTING_IRQCHIP;
1347 entries->set = xics_set_irq;
1348 entries->irqchip.irqchip = 0;
1349 entries->irqchip.pin = gsi;
1350 return 1;
1351}
1352
1353int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
1354{
1355 return pin;
1356}