mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / virt / kvm / arm / arch_timer.c
blob52b4225da32d8fe8e0994494c6911d27248e9b3d
1 /*
2 * Copyright (C) 2012 ARM Ltd.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
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.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/cpu.h>
20 #include <linux/of_irq.h>
21 #include <linux/kvm.h>
22 #include <linux/kvm_host.h>
23 #include <linux/interrupt.h>
25 #include <clocksource/arm_arch_timer.h>
26 #include <asm/arch_timer.h>
28 #include <kvm/arm_vgic.h>
29 #include <kvm/arm_arch_timer.h>
31 static struct timecounter *timecounter;
32 static struct workqueue_struct *wqueue;
33 static unsigned int host_vtimer_irq;
35 static cycle_t kvm_phys_timer_read(void)
37 return timecounter->cc->read(timecounter->cc);
40 static bool timer_is_armed(struct arch_timer_cpu *timer)
42 return timer->armed;
45 /* timer_arm: as in "arm the timer", not as in ARM the company */
46 static void timer_arm(struct arch_timer_cpu *timer, u64 ns)
48 timer->armed = true;
49 hrtimer_start(&timer->timer, ktime_add_ns(ktime_get(), ns),
50 HRTIMER_MODE_ABS);
53 static void timer_disarm(struct arch_timer_cpu *timer)
55 if (timer_is_armed(timer)) {
56 hrtimer_cancel(&timer->timer);
57 cancel_work_sync(&timer->expired);
58 timer->armed = false;
62 static void kvm_timer_inject_irq(struct kvm_vcpu *vcpu)
64 int ret;
65 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
67 timer->cntv_ctl |= ARCH_TIMER_CTRL_IT_MASK;
68 ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
69 timer->irq->irq,
70 timer->irq->level);
71 WARN_ON(ret);
74 static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
76 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
79 * We disable the timer in the world switch and let it be
80 * handled by kvm_timer_sync_hwstate(). Getting a timer
81 * interrupt at this point is a sure sign of some major
82 * breakage.
84 pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu);
85 return IRQ_HANDLED;
88 static void kvm_timer_inject_irq_work(struct work_struct *work)
90 struct kvm_vcpu *vcpu;
92 vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
93 vcpu->arch.timer_cpu.armed = false;
94 kvm_timer_inject_irq(vcpu);
97 static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
99 struct arch_timer_cpu *timer;
100 timer = container_of(hrt, struct arch_timer_cpu, timer);
101 queue_work(wqueue, &timer->expired);
102 return HRTIMER_NORESTART;
106 * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu
107 * @vcpu: The vcpu pointer
109 * Disarm any pending soft timers, since the world-switch code will write the
110 * virtual timer state back to the physical CPU.
112 void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
114 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
117 * We're about to run this vcpu again, so there is no need to
118 * keep the background timer running, as we're about to
119 * populate the CPU timer again.
121 timer_disarm(timer);
125 * kvm_timer_sync_hwstate - sync timer state from cpu
126 * @vcpu: The vcpu pointer
128 * Check if the virtual timer was armed and either schedule a corresponding
129 * soft timer or inject directly if already expired.
131 void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
133 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
134 cycle_t cval, now;
135 u64 ns;
137 if ((timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) ||
138 !(timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE))
139 return;
141 cval = timer->cntv_cval;
142 now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
144 BUG_ON(timer_is_armed(timer));
146 if (cval <= now) {
148 * Timer has already expired while we were not
149 * looking. Inject the interrupt and carry on.
151 kvm_timer_inject_irq(vcpu);
152 return;
155 ns = cyclecounter_cyc2ns(timecounter->cc, cval - now);
156 timer_arm(timer, ns);
159 void kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
160 const struct kvm_irq_level *irq)
162 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
165 * The vcpu timer irq number cannot be determined in
166 * kvm_timer_vcpu_init() because it is called much before
167 * kvm_vcpu_set_target(). To handle this, we determine
168 * vcpu timer irq number when the vcpu is reset.
170 timer->irq = irq;
173 void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
175 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
177 INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
178 hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
179 timer->timer.function = kvm_timer_expire;
182 static void kvm_timer_init_interrupt(void *info)
184 enable_percpu_irq(host_vtimer_irq, 0);
188 static int kvm_timer_cpu_notify(struct notifier_block *self,
189 unsigned long action, void *cpu)
191 switch (action) {
192 case CPU_STARTING:
193 case CPU_STARTING_FROZEN:
194 kvm_timer_init_interrupt(NULL);
195 break;
196 case CPU_DYING:
197 case CPU_DYING_FROZEN:
198 disable_percpu_irq(host_vtimer_irq);
199 break;
202 return NOTIFY_OK;
205 static struct notifier_block kvm_timer_cpu_nb = {
206 .notifier_call = kvm_timer_cpu_notify,
209 static const struct of_device_id arch_timer_of_match[] = {
210 { .compatible = "arm,armv7-timer", },
211 { .compatible = "arm,armv8-timer", },
215 int kvm_timer_hyp_init(void)
217 struct device_node *np;
218 unsigned int ppi;
219 int err;
221 timecounter = arch_timer_get_timecounter();
222 if (!timecounter)
223 return -ENODEV;
225 np = of_find_matching_node(NULL, arch_timer_of_match);
226 if (!np) {
227 kvm_err("kvm_arch_timer: can't find DT node\n");
228 return -ENODEV;
231 ppi = irq_of_parse_and_map(np, 2);
232 if (!ppi) {
233 kvm_err("kvm_arch_timer: no virtual timer interrupt\n");
234 err = -EINVAL;
235 goto out;
238 err = request_percpu_irq(ppi, kvm_arch_timer_handler,
239 "kvm guest timer", kvm_get_running_vcpus());
240 if (err) {
241 kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
242 ppi, err);
243 goto out;
246 host_vtimer_irq = ppi;
248 err = register_cpu_notifier(&kvm_timer_cpu_nb);
249 if (err) {
250 kvm_err("Cannot register timer CPU notifier\n");
251 goto out_free;
254 wqueue = create_singlethread_workqueue("kvm_arch_timer");
255 if (!wqueue) {
256 err = -ENOMEM;
257 goto out_free;
260 kvm_info("%s IRQ%d\n", np->name, ppi);
261 on_each_cpu(kvm_timer_init_interrupt, NULL, 1);
263 goto out;
264 out_free:
265 free_percpu_irq(ppi, kvm_get_running_vcpus());
266 out:
267 of_node_put(np);
268 return err;
271 void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
273 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
275 timer_disarm(timer);
278 void kvm_timer_enable(struct kvm *kvm)
280 if (kvm->arch.timer.enabled)
281 return;
284 * There is a potential race here between VCPUs starting for the first
285 * time, which may be enabling the timer multiple times. That doesn't
286 * hurt though, because we're just setting a variable to the same
287 * variable that it already was. The important thing is that all
288 * VCPUs have the enabled variable set, before entering the guest, if
289 * the arch timers are enabled.
291 if (timecounter && wqueue)
292 kvm->arch.timer.enabled = 1;
295 void kvm_timer_init(struct kvm *kvm)
297 kvm->arch.timer.cntvoff = kvm_phys_timer_read();