1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2020, The Linux Foundation. All rights reserved.
*
*/
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/spinlock.h>
#include <dt-bindings/interrupt-controller/arm-gic.h>
#include <linux/haven/hh_irq_lend.h>
#include <linux/haven/hh_rm_drv.h>
#include "hh_rm_drv_private.h"
struct hh_irq_entry {
hh_vmid_t vmid;
enum hh_vm_names vm_name;
hh_irq_handle_fn_v2 v2_handle;
hh_irq_handle_fn handle;
void *data;
enum {
HH_IRQ_STATE_NONE,
HH_IRQ_STATE_WAIT_RELEASE_OR_ACCEPT,
HH_IRQ_STATE_WAIT_LEND,
/* notification states */
HH_IRQ_STATE_RELEASED, /* svm -> pvm */
HH_IRQ_STATE_ACCEPTED, /* svm -> pvm */
HH_IRQ_STATE_LENT, /* pvm -> svm */
} state;
hh_virq_handle_t virq_handle;
};
static struct hh_irq_entry hh_irq_entries[HH_IRQ_LABEL_MAX];
static DEFINE_SPINLOCK(hh_irq_lend_lock);
static int hh_irq_released_accepted_nb_handler(struct notifier_block *this,
unsigned long cmd, void *data)
{
unsigned long flags;
enum hh_irq_label label;
struct hh_irq_entry *entry;
struct hh_rm_notif_vm_irq_released_payload *released;
struct hh_rm_notif_vm_irq_accepted_payload *accepted;
if (cmd != HH_RM_NOTIF_VM_IRQ_RELEASED &&
cmd != HH_RM_NOTIF_VM_IRQ_ACCEPTED)
return NOTIFY_DONE;
spin_lock_irqsave(&hh_irq_lend_lock, flags);
for (label = 0; label < HH_IRQ_LABEL_MAX; label++) {
entry = &hh_irq_entries[label];
if (entry->state != HH_IRQ_STATE_WAIT_RELEASE_OR_ACCEPT &&
entry->state != HH_IRQ_STATE_ACCEPTED)
continue;
switch (cmd) {
case HH_RM_NOTIF_VM_IRQ_RELEASED:
released = data;
if (released->virq_handle == entry->virq_handle) {
entry->state = HH_IRQ_STATE_RELEASED;
spin_unlock_irqrestore(&hh_irq_lend_lock,
flags);
entry->v2_handle(entry->data, cmd, label);
return NOTIFY_OK;
}
break;
case HH_RM_NOTIF_VM_IRQ_ACCEPTED:
accepted = data;
if (accepted->virq_handle == entry->virq_handle) {
entry->state = HH_IRQ_STATE_ACCEPTED;
spin_unlock_irqrestore(&hh_irq_lend_lock,
flags);
entry->v2_handle(entry->data, cmd, label);
return NOTIFY_OK;
}
break;
}
}
spin_unlock_irqrestore(&hh_irq_lend_lock, flags);
return NOTIFY_DONE;
}
static struct notifier_block hh_irq_released_accepted_nb = {
.notifier_call = hh_irq_released_accepted_nb_handler,
};
static int hh_irq_lent_nb_handler(struct notifier_block *this,
unsigned long cmd, void *data)
{
unsigned long flags;
enum hh_irq_label label;
enum hh_vm_names owner_name;
struct hh_irq_entry *entry;
struct hh_rm_notif_vm_irq_lent_payload *lent = data;
int ret;
if (cmd != HH_RM_NOTIF_VM_IRQ_LENT)
return NOTIFY_DONE;
ret = hh_rm_get_vm_name(lent->owner_vmid, &owner_name);
if (ret) {
pr_warn_ratelimited("%s: unknown name for vmid: %d\n", __func__,
lent->owner_vmid);
return ret;
}
spin_lock_irqsave(&hh_irq_lend_lock, flags);
for (label = 0; label < HH_IRQ_LABEL_MAX; label++) {
entry = &hh_irq_entries[label];
if (entry->state != HH_IRQ_STATE_WAIT_LEND)
continue;
if (label == lent->virq_label &&
(entry->vm_name == HH_VM_MAX ||
entry->vm_name == owner_name)) {
entry->vmid = lent->owner_vmid;
entry->virq_handle = lent->virq_handle;
entry->state = HH_IRQ_STATE_LENT;
spin_unlock_irqrestore(&hh_irq_lend_lock,
flags);
entry->v2_handle(entry->data, cmd, label);
return NOTIFY_OK;
}
}
spin_unlock_irqrestore(&hh_irq_lend_lock, flags);
return NOTIFY_DONE;
}
static struct notifier_block hh_irq_lent_nb = {
.notifier_call = hh_irq_lent_nb_handler,
};
/**
* hh_irq_lend_v2: Lend a hardware interrupt to another VM
* @label: vIRQ high-level label
* @name: VM name to send interrupt to
* @irq: Linux IRQ number to lend
* @cb_handle: callback to invoke when other VM release or accept the interrupt
* @data: Argument to pass to cb_handle
*
* Returns 0 on success also the handle corresponding to Linux IRQ#.
* Returns < 0 on error
*/
int hh_irq_lend_v2(enum hh_irq_label label, enum hh_vm_names name,
int irq, hh_irq_handle_fn_v2 cb_handle, void *data)
{
int ret, virq;
unsigned long flags;
struct hh_irq_entry *entry;
if (label >= HH_IRQ_LABEL_MAX || !cb_handle)
return -EINVAL;
entry = &hh_irq_entries[label];
if (hh_rm_irq_to_virq(irq, &virq))
return -EINVAL;
spin_lock_irqsave(&hh_irq_lend_lock, flags);
if (entry->state != HH_IRQ_STATE_NONE) {
spin_unlock_irqrestore(&hh_irq_lend_lock, flags);
return -EINVAL;
}
ret = hh_rm_get_vmid(name, &entry->vmid);
if (ret) {
entry->state = HH_IRQ_STATE_NONE;
spin_unlock_irqrestore(&hh_irq_lend_lock, flags);
return ret;
}
entry->v2_handle = cb_handle;
entry->data = data;
entry->state = HH_IRQ_STATE_WAIT_RELEASE_OR_ACCEPT;
spin_unlock_irqrestore(&hh_irq_lend_lock, flags);
return hh_rm_vm_irq_lend(entry->vmid, virq, label, &entry->virq_handle);
}
EXPORT_SYMBOL(hh_irq_lend_v2);
/**
* hh_irq_lend: Lend a hardware interrupt to another VM
* @label: vIRQ high-level label
* @name: VM name to send interrupt to
* @irq: Linux IRQ number to lend
* @cb_handle: callback to invoke when other VM release or accept the interrupt
* @data: Argument to pass to cb_handle
*
* Returns 0 on success also the handle corresponding to Linux IRQ#.
* Returns < 0 on error
*/
int hh_irq_lend(enum hh_irq_label label, enum hh_vm_names name,
int irq, hh_irq_handle_fn cb_handle, void *data)
{
struct hh_irq_entry *entry;
if (label >= HH_IRQ_LABEL_MAX || !cb_handle)
return -EINVAL;
entry = &hh_irq_entries[label];
entry->handle = cb_handle;
return 0;
}
EXPORT_SYMBOL(hh_irq_lend);
/**
* hh_irq_lend_notify: Pass the irq handle to other VM for accept
* @label: vIRQ high-level label
*
* Returns 0 on success, < 0 on error
*/
int hh_irq_lend_notify(enum hh_irq_label label)
{
struct hh_irq_entry *entry;
if (label >= HH_IRQ_LABEL_MAX)
return -EINVAL;
entry = &hh_irq_entries[label];
if (entry->state == HH_IRQ_STATE_NONE)
return -EINVAL;
return hh_rm_vm_irq_lend_notify(entry->vmid, entry->virq_handle);
}
EXPORT_SYMBOL(hh_irq_lend_notify);
/**
* hh_irq_reclaim: Reclaim a hardware interrupt after other VM
* has released.
* @label: vIRQ high-level label
*
* This function should be called inside or after on_release()
* callback from hh_irq_lend.
* This function is not thread-safe. Do not race with another hh_irq_reclaim
* with same label
*/
int hh_irq_reclaim(enum hh_irq_label label)
{
int ret;
struct hh_irq_entry *entry;
if (label >= HH_IRQ_LABEL_MAX)
return -EINVAL;
entry = &hh_irq_entries[label];
if (entry->state != HH_IRQ_STATE_WAIT_RELEASE_OR_ACCEPT &&
(entry->state != HH_IRQ_STATE_RELEASED))
return -EINVAL;
ret = hh_rm_vm_irq_reclaim(entry->virq_handle);
if (!ret)
entry->state = HH_IRQ_STATE_NONE;
return ret;
}
EXPORT_SYMBOL(hh_irq_reclaim);
/**
* hh_irq_wait_for_lend_v2: Register to claim a lent interrupt from another VM
* @label: vIRQ high-level label
* @name: Lender's VM name. If don't care, then use HH_VM_MAX
* @on_lend: callback to invoke when other VM lends the interrupt
* @data: Argument to pass to on_lend
*/
int hh_irq_wait_for_lend_v2(enum hh_irq_label label, enum hh_vm_names name,
hh_irq_handle_fn_v2 on_lend, void *data)
{
unsigned long flags;
struct hh_irq_entry *entry;
if (label >= HH_IRQ_LABEL_MAX || !on_lend)
return -EINVAL;
entry = &hh_irq_entries[label];
spin_lock_irqsave(&hh_irq_lend_lock, flags);
if (entry->state != HH_IRQ_STATE_NONE) {
spin_unlock_irqrestore(&hh_irq_lend_lock, flags);
return -EINVAL;
}
entry->vm_name = name;
entry->v2_handle = on_lend;
entry->data = data;
entry->state = HH_IRQ_STATE_WAIT_LEND;
spin_unlock_irqrestore(&hh_irq_lend_lock, flags);
return 0;
}
EXPORT_SYMBOL(hh_irq_wait_for_lend_v2);
/**
* hh_irq_wait_lend: Register to claim a lent interrupt from another VM
* @label: vIRQ high-level label
* @name: Lender's VM name. If don't care, then use HH_VM_MAX
* @on_lend: callback to invoke when other VM lends the interrupt
* @data: Argument to pass to on_lend
*/
int hh_irq_wait_for_lend(enum hh_irq_label label, enum hh_vm_names name,
hh_irq_handle_fn on_lend, void *data)
{
return 0;
}
EXPORT_SYMBOL(hh_irq_wait_for_lend);
/**
* hh_irq_accept: Register to receive interrupts with a lent vIRQ
* @label: vIRQ high-level label
* @irq: Linux IRQ# to associate vIRQ with. If don't care, use -1
* @type: IRQ flags to use when allowing RM to choose the IRQ. If irq parameter
* is specified, then type is unused.
*
* Returns the Linux IRQ# that vIRQ was registered to on success.
* Returns <0 on error
* This function is not thread-safe w.r.t. IRQ lend state. Do not race with
* with hh_irq_release or another hh_irq_accept with same label.
*/
int hh_irq_accept(enum hh_irq_label label, int irq, int type)
{
struct hh_irq_entry *entry;
u32 virq;
if (label >= HH_IRQ_LABEL_MAX)
return -EINVAL;
entry = &hh_irq_entries[label];
if (entry->state != HH_IRQ_STATE_LENT)
return -EINVAL;
if (irq != -1) {
if (hh_rm_irq_to_virq(irq, &virq))
return -EINVAL;
} else
virq = -1;
virq = hh_rm_vm_irq_accept(entry->virq_handle, virq);
if (virq < 0)
return virq;
if (irq == -1)
irq = hh_rm_virq_to_irq(virq, type);
entry->state = HH_IRQ_STATE_ACCEPTED;
return irq;
}
EXPORT_SYMBOL(hh_irq_accept);
/**
* hh_irq_accept_notify: Notify the lend vm (pvm) that IRQ is accepted
* @label: vIRQ high-level label
* @irq: Linux IRQ# to associate vIRQ with. If don't care, use -1
*
* Returns the Linux IRQ# that vIRQ was registered to on success.
* Returns <0 on error
* This function is not thread-safe w.r.t. IRQ lend state. Do not race with
* hh_irq_release or another hh_irq_accept with same label.
*/
int hh_irq_accept_notify(enum hh_irq_label label)
{
struct hh_irq_entry *entry;
if (label >= HH_IRQ_LABEL_MAX)
return -EINVAL;
entry = &hh_irq_entries[label];
if (entry->state != HH_IRQ_STATE_ACCEPTED)
return -EINVAL;
return hh_rm_vm_irq_accept_notify(entry->vmid,
entry->virq_handle);
}
EXPORT_SYMBOL(hh_irq_accept_notify);
/**
* hh_irq_release: Release a lent interrupt
* @label: vIRQ high-level label
* This function is not thread-safe w.r.t. IRQ lend state. Do not race with
* with hh_irq_accept or another hh_irq_release with same label.
*/
int hh_irq_release(enum hh_irq_label label)
{
int ret;
struct hh_irq_entry *entry;
if (label >= HH_IRQ_LABEL_MAX)
return -EINVAL;
entry = &hh_irq_entries[label];
if (entry->state != HH_IRQ_STATE_ACCEPTED)
return -EINVAL;
ret = hh_rm_vm_irq_release(entry->virq_handle);
if (!ret)
entry->state = HH_IRQ_STATE_WAIT_LEND;
return ret;
}
EXPORT_SYMBOL(hh_irq_release);
int hh_irq_release_notify(enum hh_irq_label label)
{
struct hh_irq_entry *entry;
if (label >= HH_IRQ_LABEL_MAX)
return -EINVAL;
entry = &hh_irq_entries[label];
if (entry->state != HH_IRQ_STATE_ACCEPTED &&
entry->state != HH_IRQ_STATE_WAIT_LEND)
return -EINVAL;
return hh_rm_vm_irq_release_notify(entry->vmid,
entry->virq_handle);
}
EXPORT_SYMBOL(hh_irq_release_notify);
static int __init hh_irq_lend_init(void)
{
int ret;
ret = hh_rm_register_notifier(&hh_irq_lent_nb);
if (ret)
return ret;
return hh_rm_register_notifier(&hh_irq_released_accepted_nb);
}
module_init(hh_irq_lend_init);
static void hh_irq_lend_exit(void)
{
hh_rm_unregister_notifier(&hh_irq_lent_nb);
hh_rm_unregister_notifier(&hh_irq_released_accepted_nb);
}
module_exit(hh_irq_lend_exit);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Haven IRQ Lending Library");
|