Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/arch/arm64/kvm/vgic/vgic-debug.c
50904 views
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
* Copyright (C) 2016 Linaro
4
* Author: Christoffer Dall <[email protected]>
5
*/
6
7
#include <linux/cpu.h>
8
#include <linux/debugfs.h>
9
#include <linux/interrupt.h>
10
#include <linux/kvm_host.h>
11
#include <linux/seq_file.h>
12
#include <kvm/arm_vgic.h>
13
#include <asm/kvm_mmu.h>
14
#include "vgic.h"
15
16
/*
17
* Structure to control looping through the entire vgic state. We start at
18
* zero for each field and move upwards. So, if dist_id is 0 we print the
19
* distributor info. When dist_id is 1, we have already printed it and move
20
* on.
21
*
22
* When vcpu_id < nr_cpus we print the vcpu info until vcpu_id == nr_cpus and
23
* so on.
24
*/
25
struct vgic_state_iter {
26
int nr_cpus;
27
int nr_spis;
28
int nr_lpis;
29
int dist_id;
30
int vcpu_id;
31
unsigned long intid;
32
int lpi_idx;
33
};
34
35
static void iter_next(struct kvm *kvm, struct vgic_state_iter *iter)
36
{
37
struct vgic_dist *dist = &kvm->arch.vgic;
38
39
if (iter->dist_id == 0) {
40
iter->dist_id++;
41
return;
42
}
43
44
/*
45
* Let the xarray drive the iterator after the last SPI, as the iterator
46
* has exhausted the sequentially-allocated INTID space.
47
*/
48
if (iter->intid >= (iter->nr_spis + VGIC_NR_PRIVATE_IRQS - 1) &&
49
iter->nr_lpis) {
50
if (iter->lpi_idx < iter->nr_lpis)
51
xa_find_after(&dist->lpi_xa, &iter->intid,
52
VGIC_LPI_MAX_INTID,
53
LPI_XA_MARK_DEBUG_ITER);
54
iter->lpi_idx++;
55
return;
56
}
57
58
iter->intid++;
59
if (iter->intid == VGIC_NR_PRIVATE_IRQS &&
60
++iter->vcpu_id < iter->nr_cpus)
61
iter->intid = 0;
62
}
63
64
static int iter_mark_lpis(struct kvm *kvm)
65
{
66
struct vgic_dist *dist = &kvm->arch.vgic;
67
unsigned long intid, flags;
68
struct vgic_irq *irq;
69
int nr_lpis = 0;
70
71
xa_lock_irqsave(&dist->lpi_xa, flags);
72
73
xa_for_each(&dist->lpi_xa, intid, irq) {
74
if (!vgic_try_get_irq_ref(irq))
75
continue;
76
77
__xa_set_mark(&dist->lpi_xa, intid, LPI_XA_MARK_DEBUG_ITER);
78
nr_lpis++;
79
}
80
81
xa_unlock_irqrestore(&dist->lpi_xa, flags);
82
83
return nr_lpis;
84
}
85
86
static void iter_unmark_lpis(struct kvm *kvm)
87
{
88
struct vgic_dist *dist = &kvm->arch.vgic;
89
unsigned long intid, flags;
90
struct vgic_irq *irq;
91
92
xa_for_each_marked(&dist->lpi_xa, intid, irq, LPI_XA_MARK_DEBUG_ITER) {
93
xa_lock_irqsave(&dist->lpi_xa, flags);
94
__xa_clear_mark(&dist->lpi_xa, intid, LPI_XA_MARK_DEBUG_ITER);
95
xa_unlock_irqrestore(&dist->lpi_xa, flags);
96
97
/* vgic_put_irq() expects to be called outside of the xa_lock */
98
vgic_put_irq(kvm, irq);
99
}
100
}
101
102
static void iter_init(struct kvm *kvm, struct vgic_state_iter *iter,
103
loff_t pos)
104
{
105
int nr_cpus = atomic_read(&kvm->online_vcpus);
106
107
memset(iter, 0, sizeof(*iter));
108
109
iter->nr_cpus = nr_cpus;
110
iter->nr_spis = kvm->arch.vgic.nr_spis;
111
if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3)
112
iter->nr_lpis = iter_mark_lpis(kvm);
113
114
/* Fast forward to the right position if needed */
115
while (pos--)
116
iter_next(kvm, iter);
117
}
118
119
static bool end_of_vgic(struct vgic_state_iter *iter)
120
{
121
return iter->dist_id > 0 &&
122
iter->vcpu_id == iter->nr_cpus &&
123
iter->intid >= (iter->nr_spis + VGIC_NR_PRIVATE_IRQS) &&
124
(!iter->nr_lpis || iter->lpi_idx > iter->nr_lpis);
125
}
126
127
static void *vgic_debug_start(struct seq_file *s, loff_t *pos)
128
{
129
struct kvm *kvm = s->private;
130
struct vgic_state_iter *iter;
131
132
mutex_lock(&kvm->arch.config_lock);
133
iter = kvm->arch.vgic.iter;
134
if (iter) {
135
iter = ERR_PTR(-EBUSY);
136
goto out;
137
}
138
139
iter = kmalloc(sizeof(*iter), GFP_KERNEL);
140
if (!iter) {
141
iter = ERR_PTR(-ENOMEM);
142
goto out;
143
}
144
145
iter_init(kvm, iter, *pos);
146
kvm->arch.vgic.iter = iter;
147
148
if (end_of_vgic(iter))
149
iter = NULL;
150
out:
151
mutex_unlock(&kvm->arch.config_lock);
152
return iter;
153
}
154
155
static void *vgic_debug_next(struct seq_file *s, void *v, loff_t *pos)
156
{
157
struct kvm *kvm = s->private;
158
struct vgic_state_iter *iter = kvm->arch.vgic.iter;
159
160
++*pos;
161
iter_next(kvm, iter);
162
if (end_of_vgic(iter))
163
iter = NULL;
164
return iter;
165
}
166
167
static void vgic_debug_stop(struct seq_file *s, void *v)
168
{
169
struct kvm *kvm = s->private;
170
struct vgic_state_iter *iter;
171
172
/*
173
* If the seq file wasn't properly opened, there's nothing to clearn
174
* up.
175
*/
176
if (IS_ERR(v))
177
return;
178
179
mutex_lock(&kvm->arch.config_lock);
180
iter = kvm->arch.vgic.iter;
181
iter_unmark_lpis(kvm);
182
kfree(iter);
183
kvm->arch.vgic.iter = NULL;
184
mutex_unlock(&kvm->arch.config_lock);
185
}
186
187
static void print_dist_state(struct seq_file *s, struct vgic_dist *dist,
188
struct vgic_state_iter *iter)
189
{
190
bool v3 = dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3;
191
192
seq_printf(s, "Distributor\n");
193
seq_printf(s, "===========\n");
194
seq_printf(s, "vgic_model:\t%s\n", v3 ? "GICv3" : "GICv2");
195
seq_printf(s, "nr_spis:\t%d\n", dist->nr_spis);
196
if (v3)
197
seq_printf(s, "nr_lpis:\t%d\n", iter->nr_lpis);
198
seq_printf(s, "enabled:\t%d\n", dist->enabled);
199
seq_printf(s, "\n");
200
201
seq_printf(s, "P=pending_latch, L=line_level, A=active\n");
202
seq_printf(s, "E=enabled, H=hw, C=config (level=1, edge=0)\n");
203
seq_printf(s, "G=group\n");
204
}
205
206
static void print_header(struct seq_file *s, struct vgic_irq *irq,
207
struct kvm_vcpu *vcpu)
208
{
209
int id = 0;
210
char *hdr = "SPI ";
211
212
if (vcpu) {
213
hdr = "VCPU";
214
id = vcpu->vcpu_idx;
215
}
216
217
seq_printf(s, "\n");
218
seq_printf(s, "%s%2d TYP ID TGT_ID PLAEHCG HWID TARGET SRC PRI VCPU_ID\n", hdr, id);
219
seq_printf(s, "----------------------------------------------------------------\n");
220
}
221
222
static void print_irq_state(struct seq_file *s, struct vgic_irq *irq,
223
struct kvm_vcpu *vcpu)
224
{
225
char *type;
226
bool pending;
227
228
if (irq->intid < VGIC_NR_SGIS)
229
type = "SGI";
230
else if (irq->intid < VGIC_NR_PRIVATE_IRQS)
231
type = "PPI";
232
else if (irq->intid < VGIC_MAX_SPI)
233
type = "SPI";
234
else
235
type = "LPI";
236
237
if (irq->intid ==0 || irq->intid == VGIC_NR_PRIVATE_IRQS)
238
print_header(s, irq, vcpu);
239
240
pending = irq->pending_latch;
241
if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
242
int err;
243
244
err = irq_get_irqchip_state(irq->host_irq,
245
IRQCHIP_STATE_PENDING,
246
&pending);
247
WARN_ON_ONCE(err);
248
}
249
250
seq_printf(s, " %s %4d "
251
" %2d "
252
"%d%d%d%d%d%d%d "
253
"%8d "
254
"%8x "
255
" %2x "
256
"%3d "
257
" %2d "
258
"\n",
259
type, irq->intid,
260
(irq->target_vcpu) ? irq->target_vcpu->vcpu_idx : -1,
261
pending,
262
irq->line_level,
263
irq->active,
264
irq->enabled,
265
irq->hw,
266
irq->config == VGIC_CONFIG_LEVEL,
267
irq->group,
268
irq->hwintid,
269
irq->mpidr,
270
irq->source,
271
irq->priority,
272
(irq->vcpu) ? irq->vcpu->vcpu_idx : -1);
273
}
274
275
static int vgic_debug_show(struct seq_file *s, void *v)
276
{
277
struct kvm *kvm = s->private;
278
struct vgic_state_iter *iter = v;
279
struct vgic_irq *irq;
280
struct kvm_vcpu *vcpu = NULL;
281
unsigned long flags;
282
283
if (iter->dist_id == 0) {
284
print_dist_state(s, &kvm->arch.vgic, iter);
285
return 0;
286
}
287
288
if (!kvm->arch.vgic.initialized)
289
return 0;
290
291
if (iter->vcpu_id < iter->nr_cpus)
292
vcpu = kvm_get_vcpu(kvm, iter->vcpu_id);
293
294
/*
295
* Expect this to succeed, as iter_mark_lpis() takes a reference on
296
* every LPI to be visited.
297
*/
298
if (iter->intid < VGIC_NR_PRIVATE_IRQS)
299
irq = vgic_get_vcpu_irq(vcpu, iter->intid);
300
else
301
irq = vgic_get_irq(kvm, iter->intid);
302
if (WARN_ON_ONCE(!irq))
303
return -EINVAL;
304
305
raw_spin_lock_irqsave(&irq->irq_lock, flags);
306
print_irq_state(s, irq, vcpu);
307
raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
308
309
vgic_put_irq(kvm, irq);
310
return 0;
311
}
312
313
static const struct seq_operations vgic_debug_sops = {
314
.start = vgic_debug_start,
315
.next = vgic_debug_next,
316
.stop = vgic_debug_stop,
317
.show = vgic_debug_show
318
};
319
320
DEFINE_SEQ_ATTRIBUTE(vgic_debug);
321
322
void vgic_debug_init(struct kvm *kvm)
323
{
324
debugfs_create_file("vgic-state", 0444, kvm->debugfs_dentry, kvm,
325
&vgic_debug_fops);
326
}
327
328
void vgic_debug_destroy(struct kvm *kvm)
329
{
330
}
331
332
/**
333
* struct vgic_its_iter - Iterator for traversing VGIC ITS device tables.
334
* @dev: Pointer to the current its_device being processed.
335
* @ite: Pointer to the current its_ite within the device being processed.
336
*
337
* This structure is used to maintain the current position during iteration
338
* over the ITS device tables. It holds pointers to both the current device
339
* and the current ITE within that device.
340
*/
341
struct vgic_its_iter {
342
struct its_device *dev;
343
struct its_ite *ite;
344
};
345
346
/**
347
* end_of_iter - Checks if the iterator has reached the end.
348
* @iter: The iterator to check.
349
*
350
* When the iterator completed processing the final ITE in the last device
351
* table, it was marked to indicate the end of iteration by setting its
352
* device and ITE pointers to NULL.
353
* This function checks whether the iterator was marked as end.
354
*
355
* Return: True if the iterator is marked as end, false otherwise.
356
*/
357
static inline bool end_of_iter(struct vgic_its_iter *iter)
358
{
359
return !iter->dev && !iter->ite;
360
}
361
362
/**
363
* vgic_its_iter_next - Advances the iterator to the next entry in the ITS tables.
364
* @its: The VGIC ITS structure.
365
* @iter: The iterator to advance.
366
*
367
* This function moves the iterator to the next ITE within the current device,
368
* or to the first ITE of the next device if the current ITE is the last in
369
* the device. If the current device is the last device, the iterator is set
370
* to indicate the end of iteration.
371
*/
372
static void vgic_its_iter_next(struct vgic_its *its, struct vgic_its_iter *iter)
373
{
374
struct its_device *dev = iter->dev;
375
struct its_ite *ite = iter->ite;
376
377
if (!ite || list_is_last(&ite->ite_list, &dev->itt_head)) {
378
if (list_is_last(&dev->dev_list, &its->device_list)) {
379
dev = NULL;
380
ite = NULL;
381
} else {
382
dev = list_next_entry(dev, dev_list);
383
ite = list_first_entry_or_null(&dev->itt_head,
384
struct its_ite,
385
ite_list);
386
}
387
} else {
388
ite = list_next_entry(ite, ite_list);
389
}
390
391
iter->dev = dev;
392
iter->ite = ite;
393
}
394
395
/**
396
* vgic_its_debug_start - Start function for the seq_file interface.
397
* @s: The seq_file structure.
398
* @pos: The starting position (offset).
399
*
400
* This function initializes the iterator to the beginning of the ITS tables
401
* and advances it to the specified position. It acquires the its_lock mutex
402
* to protect shared data.
403
*
404
* Return: An iterator pointer on success, NULL if no devices are found or
405
* the end of the list is reached, or ERR_PTR(-ENOMEM) on memory
406
* allocation failure.
407
*/
408
static void *vgic_its_debug_start(struct seq_file *s, loff_t *pos)
409
{
410
struct vgic_its *its = s->private;
411
struct vgic_its_iter *iter;
412
struct its_device *dev;
413
loff_t offset = *pos;
414
415
mutex_lock(&its->its_lock);
416
417
dev = list_first_entry_or_null(&its->device_list,
418
struct its_device, dev_list);
419
if (!dev)
420
return NULL;
421
422
iter = kmalloc(sizeof(*iter), GFP_KERNEL);
423
if (!iter)
424
return ERR_PTR(-ENOMEM);
425
426
iter->dev = dev;
427
iter->ite = list_first_entry_or_null(&dev->itt_head,
428
struct its_ite, ite_list);
429
430
while (!end_of_iter(iter) && offset--)
431
vgic_its_iter_next(its, iter);
432
433
if (end_of_iter(iter)) {
434
kfree(iter);
435
return NULL;
436
}
437
438
return iter;
439
}
440
441
/**
442
* vgic_its_debug_next - Next function for the seq_file interface.
443
* @s: The seq_file structure.
444
* @v: The current iterator.
445
* @pos: The current position (offset).
446
*
447
* This function advances the iterator to the next entry and increments the
448
* position.
449
*
450
* Return: An iterator pointer on success, or NULL if the end of the list is
451
* reached.
452
*/
453
static void *vgic_its_debug_next(struct seq_file *s, void *v, loff_t *pos)
454
{
455
struct vgic_its *its = s->private;
456
struct vgic_its_iter *iter = v;
457
458
++*pos;
459
vgic_its_iter_next(its, iter);
460
461
if (end_of_iter(iter)) {
462
kfree(iter);
463
return NULL;
464
}
465
return iter;
466
}
467
468
/**
469
* vgic_its_debug_stop - Stop function for the seq_file interface.
470
* @s: The seq_file structure.
471
* @v: The current iterator.
472
*
473
* This function frees the iterator and releases the its_lock mutex.
474
*/
475
static void vgic_its_debug_stop(struct seq_file *s, void *v)
476
{
477
struct vgic_its *its = s->private;
478
struct vgic_its_iter *iter = v;
479
480
if (!IS_ERR_OR_NULL(iter))
481
kfree(iter);
482
mutex_unlock(&its->its_lock);
483
}
484
485
/**
486
* vgic_its_debug_show - Show function for the seq_file interface.
487
* @s: The seq_file structure.
488
* @v: The current iterator.
489
*
490
* This function formats and prints the ITS table entry information to the
491
* seq_file output.
492
*
493
* Return: 0 on success.
494
*/
495
static int vgic_its_debug_show(struct seq_file *s, void *v)
496
{
497
struct vgic_its_iter *iter = v;
498
struct its_device *dev = iter->dev;
499
struct its_ite *ite = iter->ite;
500
501
if (!ite)
502
return 0;
503
504
if (list_is_first(&ite->ite_list, &dev->itt_head)) {
505
seq_printf(s, "\n");
506
seq_printf(s, "Device ID: 0x%x, Event ID Range: [0 - %llu]\n",
507
dev->device_id, BIT_ULL(dev->num_eventid_bits) - 1);
508
seq_printf(s, "EVENT_ID INTID HWINTID TARGET COL_ID HW\n");
509
seq_printf(s, "-----------------------------------------------\n");
510
}
511
512
if (ite->irq && ite->collection) {
513
seq_printf(s, "%8u %8u %8u %8u %8u %2d\n",
514
ite->event_id, ite->irq->intid, ite->irq->hwintid,
515
ite->collection->target_addr,
516
ite->collection->collection_id, ite->irq->hw);
517
}
518
519
return 0;
520
}
521
522
static const struct seq_operations vgic_its_debug_sops = {
523
.start = vgic_its_debug_start,
524
.next = vgic_its_debug_next,
525
.stop = vgic_its_debug_stop,
526
.show = vgic_its_debug_show
527
};
528
529
DEFINE_SEQ_ATTRIBUTE(vgic_its_debug);
530
531
/**
532
* vgic_its_debug_init - Initializes the debugfs interface for VGIC ITS.
533
* @dev: The KVM device structure.
534
*
535
* This function creates a debugfs file named "vgic-its-state@%its_base"
536
* to expose the ITS table information.
537
*
538
* Return: 0 on success.
539
*/
540
int vgic_its_debug_init(struct kvm_device *dev)
541
{
542
struct vgic_its *its = dev->private;
543
char *name;
544
545
name = kasprintf(GFP_KERNEL, "vgic-its-state@%llx", (u64)its->vgic_its_base);
546
if (!name)
547
return -ENOMEM;
548
549
debugfs_create_file(name, 0444, dev->kvm->debugfs_dentry, its, &vgic_its_debug_fops);
550
551
kfree(name);
552
return 0;
553
}
554
555
void vgic_its_debug_destroy(struct kvm_device *dev)
556
{
557
}
558
559