irqchip.c (6724af486903df57338c14424e02599e371cf563) irqchip.c (e73f61e41f3bb9bc453fd9c21b82d0b89c2b83e7)
1/*
2 * irqchip.c: Common API for in kernel interrupt controllers
3 * Copyright (c) 2007, Intel Corporation.
4 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
5 * Copyright (c) 2013, Alexander Graf <agraf@suse.de>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,

--- 19 unchanged lines hidden (view full) ---

28#include <linux/slab.h>
29#include <linux/srcu.h>
30#include <linux/export.h>
31#include <trace/events/kvm.h>
32#include "irq.h"
33
34struct kvm_irq_routing_table {
35 int chip[KVM_NR_IRQCHIPS][KVM_IRQCHIP_NUM_PINS];
1/*
2 * irqchip.c: Common API for in kernel interrupt controllers
3 * Copyright (c) 2007, Intel Corporation.
4 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
5 * Copyright (c) 2013, Alexander Graf <agraf@suse.de>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,

--- 19 unchanged lines hidden (view full) ---

28#include <linux/slab.h>
29#include <linux/srcu.h>
30#include <linux/export.h>
31#include <trace/events/kvm.h>
32#include "irq.h"
33
34struct kvm_irq_routing_table {
35 int chip[KVM_NR_IRQCHIPS][KVM_IRQCHIP_NUM_PINS];
36 struct kvm_kernel_irq_routing_entry *rt_entries;
37 u32 nr_rt_entries;
38 /*
39 * Array indexed by gsi. Each entry contains list of irq chips
40 * the gsi is connected to.
41 */
42 struct hlist_head map[0];
43};
44

--- 68 unchanged lines hidden (view full) ---

113 continue;
114
115 ret = r + ((ret < 0) ? 0 : ret);
116 }
117
118 return ret;
119}
120
36 u32 nr_rt_entries;
37 /*
38 * Array indexed by gsi. Each entry contains list of irq chips
39 * the gsi is connected to.
40 */
41 struct hlist_head map[0];
42};
43

--- 68 unchanged lines hidden (view full) ---

112 continue;
113
114 ret = r + ((ret < 0) ? 0 : ret);
115 }
116
117 return ret;
118}
119
120static void free_irq_routing_table(struct kvm_irq_routing_table *rt)
121{
122 int i;
123
124 if (!rt)
125 return;
126
127 for (i = 0; i < rt->nr_rt_entries; ++i) {
128 struct kvm_kernel_irq_routing_entry *e;
129 struct hlist_node *n;
130
131 hlist_for_each_entry_safe(e, n, &rt->map[i], link) {
132 hlist_del(&e->link);
133 kfree(e);
134 }
135 }
136
137 kfree(rt);
138}
139
121void kvm_free_irq_routing(struct kvm *kvm)
122{
123 /* Called only during vm destruction. Nobody can use the pointer
124 at this stage */
140void kvm_free_irq_routing(struct kvm *kvm)
141{
142 /* Called only during vm destruction. Nobody can use the pointer
143 at this stage */
125 kfree(kvm->irq_routing);
144 struct kvm_irq_routing_table *rt = rcu_access_pointer(kvm->irq_routing);
145 free_irq_routing_table(rt);
126}
127
128static int setup_routing_entry(struct kvm_irq_routing_table *rt,
129 struct kvm_kernel_irq_routing_entry *e,
130 const struct kvm_irq_routing_entry *ue)
131{
132 int r = -EINVAL;
133 struct kvm_kernel_irq_routing_entry *ei;

--- 34 unchanged lines hidden (view full) ---

168 for (i = 0; i < nr; ++i) {
169 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
170 return -EINVAL;
171 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
172 }
173
174 nr_rt_entries += 1;
175
146}
147
148static int setup_routing_entry(struct kvm_irq_routing_table *rt,
149 struct kvm_kernel_irq_routing_entry *e,
150 const struct kvm_irq_routing_entry *ue)
151{
152 int r = -EINVAL;
153 struct kvm_kernel_irq_routing_entry *ei;

--- 34 unchanged lines hidden (view full) ---

188 for (i = 0; i < nr; ++i) {
189 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
190 return -EINVAL;
191 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
192 }
193
194 nr_rt_entries += 1;
195
176 new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
177 + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
196 new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head)),
178 GFP_KERNEL);
179
180 if (!new)
181 return -ENOMEM;
182
197 GFP_KERNEL);
198
199 if (!new)
200 return -ENOMEM;
201
183 new->rt_entries = (void *)&new->map[nr_rt_entries];
184
185 new->nr_rt_entries = nr_rt_entries;
186 for (i = 0; i < KVM_NR_IRQCHIPS; i++)
187 for (j = 0; j < KVM_IRQCHIP_NUM_PINS; j++)
188 new->chip[i][j] = -1;
189
190 for (i = 0; i < nr; ++i) {
202 new->nr_rt_entries = nr_rt_entries;
203 for (i = 0; i < KVM_NR_IRQCHIPS; i++)
204 for (j = 0; j < KVM_IRQCHIP_NUM_PINS; j++)
205 new->chip[i][j] = -1;
206
207 for (i = 0; i < nr; ++i) {
208 struct kvm_kernel_irq_routing_entry *e;
209
210 r = -ENOMEM;
211 e = kzalloc(sizeof(*e), GFP_KERNEL);
212 if (!e)
213 goto out;
214
191 r = -EINVAL;
192 if (ue->flags)
193 goto out;
215 r = -EINVAL;
216 if (ue->flags)
217 goto out;
194 r = setup_routing_entry(new, &new->rt_entries[i], ue);
218 r = setup_routing_entry(new, e, ue);
195 if (r)
196 goto out;
197 ++ue;
198 }
199
200 mutex_lock(&kvm->irq_lock);
201 old = kvm->irq_routing;
202 rcu_assign_pointer(kvm->irq_routing, new);
203 kvm_irq_routing_update(kvm);
204 mutex_unlock(&kvm->irq_lock);
205
206 synchronize_srcu_expedited(&kvm->irq_srcu);
207
208 new = old;
209 r = 0;
210
211out:
219 if (r)
220 goto out;
221 ++ue;
222 }
223
224 mutex_lock(&kvm->irq_lock);
225 old = kvm->irq_routing;
226 rcu_assign_pointer(kvm->irq_routing, new);
227 kvm_irq_routing_update(kvm);
228 mutex_unlock(&kvm->irq_lock);
229
230 synchronize_srcu_expedited(&kvm->irq_srcu);
231
232 new = old;
233 r = 0;
234
235out:
212 kfree(new);
236 free_irq_routing_table(new);
237
213 return r;
214}
238 return r;
239}