Skip to content

Commit 76fd226

Browse files
jannaumarcan
authored andcommitted
aic: Add support for multi-die AIC2 as seen on the M1 Ultra
Multi-die IRQs are coded as in the ADT: die * max_irq + num Signed-off-by: Janne Grunau <[email protected]>
1 parent 524cb4a commit 76fd226

5 files changed

Lines changed: 154 additions & 73 deletions

File tree

src/aic.c

Lines changed: 99 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,97 @@
66
#include "assert.h"
77
#include "utils.h"
88

9-
u64 aic_base;
10-
119
#define MASK_REG(x) (4 * ((x) >> 5))
1210
#define MASK_BIT(x) BIT((x)&GENMASK(4, 0))
1311

14-
static const struct aic_regs aic1_regs = {
15-
.reg_size = AIC_REG_SIZE,
16-
.event = AIC_EVENT,
17-
.tgt_cpu = AIC_TARGET_CPU,
18-
.sw_set = AIC_SW_SET,
19-
.sw_clr = AIC_SW_CLR,
20-
.mask_set = AIC_MASK_SET,
21-
.mask_clr = AIC_MASK_CLR,
12+
static struct aic aic1 = {
13+
.version = 1,
14+
.nr_die = 1,
15+
.max_die = 1,
16+
.regs =
17+
{
18+
.reg_size = AIC_REG_SIZE,
19+
.event = AIC_EVENT,
20+
.tgt_cpu = AIC_TARGET_CPU,
21+
.sw_set = AIC_SW_SET,
22+
.sw_clr = AIC_SW_CLR,
23+
.mask_set = AIC_MASK_SET,
24+
.mask_clr = AIC_MASK_CLR,
25+
},
2226
};
2327

24-
static const struct aic_regs aic2_regs = {
25-
.reg_size = AIC2_REG_SIZE,
26-
.event = AIC2_EVENT,
27-
.config = AIC2_IRQ_CFG,
28-
.sw_set = AIC2_SW_SET,
29-
.sw_clr = AIC2_SW_CLR,
30-
.mask_set = AIC2_MASK_SET,
31-
.mask_clr = AIC2_MASK_CLR,
28+
static struct aic aic2 = {
29+
.version = 2,
30+
.regs =
31+
{
32+
.config = AIC2_IRQ_CFG,
33+
},
3234
};
3335

34-
const struct aic_regs *aic_regs;
36+
struct aic *aic;
3537

36-
static void aic2_init(int node)
38+
static int aic2_init(int node)
3739
{
40+
int ret = ADT_GETPROP(adt, node, "aic-iack-offset", &aic->regs.event);
41+
if (ret < 0) {
42+
printf("AIC: failed to get property aic-iack-offset\n");
43+
return ret;
44+
}
45+
46+
u32 info1 = read32(aic->base + AIC2_INFO1);
47+
aic->nr_die = FIELD_GET(AIC2_INFO1_LAST_DIE, info1) + 1;
48+
aic->nr_irq = FIELD_GET(AIC2_INFO1_NR_IRQ, info1);
49+
50+
u32 info3 = read32(aic->base + AIC2_INFO3);
51+
aic->max_die = FIELD_GET(AIC2_INFO3_MAX_DIE, info3);
52+
aic->max_irq = FIELD_GET(AIC2_INFO3_MAX_IRQ, info3);
53+
54+
if (aic->nr_die > AIC_MAX_DIES) {
55+
printf("AIC: more dies than supported: %u\n", aic->max_die);
56+
return -1;
57+
}
58+
59+
if (aic->max_irq > AIC_MAX_HW_NUM) {
60+
printf("AIC: more IRQs than supported: %u\n", aic->max_irq);
61+
return -1;
62+
}
63+
64+
const u64 start_off = aic->regs.config;
65+
u64 off = start_off + sizeof(u32) * aic->max_irq; /* IRQ_CFG */
66+
67+
aic->regs.sw_set = off;
68+
off += sizeof(u32) * (aic->max_irq >> 5); /* SW_SET */
69+
aic->regs.sw_clr = off;
70+
off += sizeof(u32) * (aic->max_irq >> 5); /* SW_CLR */
71+
aic->regs.mask_set = off;
72+
off += sizeof(u32) * (aic->max_irq >> 5); /* MASK_SET */
73+
aic->regs.mask_clr = off;
74+
off += sizeof(u32) * (aic->max_irq >> 5); /* MASK_CLR */
75+
off += sizeof(u32) * (aic->max_irq >> 5); /* HW_STATE */
76+
77+
aic->die_stride = off - start_off;
78+
aic->regs.reg_size = aic->regs.event + 4;
79+
80+
printf("AIC: AIC2 with %u/%u dies, %u/%u IRQs, reg_size:%05lx die_stride:%05x\n", aic->nr_die,
81+
aic->max_die, aic->nr_irq, aic->max_irq, aic->regs.reg_size, aic->die_stride);
82+
3883
u32 ext_intr_config_len;
3984
const u8 *ext_intr_config = adt_getprop(adt, node, "aic-ext-intr-cfg", &ext_intr_config_len);
4085

4186
if (ext_intr_config) {
4287
printf("AIC: Configuring %d external interrupts\n", ext_intr_config_len / 3);
4388
for (u32 i = 0; i < ext_intr_config_len; i += 3) {
44-
u16 irq = ext_intr_config[i] | (ext_intr_config[i + 1] << 8);
89+
u8 die = ext_intr_config[i + 1] >> 4;
90+
u16 irq = ext_intr_config[i] | ((ext_intr_config[i + 1] & 0xf) << 8);
4591
u8 target = ext_intr_config[i + 2];
46-
assert(irq < 0x1000); // Will probably need updating for multi-die
47-
mask32(aic_base + aic_regs->config + 4 * irq, AIC2_IRQ_CFG_TARGET,
48-
FIELD_PREP(AIC2_IRQ_CFG_TARGET, target));
92+
assert(die < aic->nr_die);
93+
assert(irq < aic->nr_irq);
94+
mask32(aic->base + aic->regs.config + die * aic->die_stride + 4 * irq,
95+
AIC2_IRQ_CFG_TARGET, FIELD_PREP(AIC2_IRQ_CFG_TARGET, target));
4996
}
5097
}
51-
return;
98+
99+
return 0;
52100
}
53101

54102
void aic_init(void)
@@ -61,32 +109,45 @@ void aic_init(void)
61109
return;
62110
}
63111

64-
if (adt_get_reg(adt, path, "reg", 0, &aic_base, NULL)) {
112+
if (adt_is_compatible(adt, node, "aic,1")) {
113+
aic = &aic1;
114+
} else if (adt_is_compatible(adt, node, "aic,2")) {
115+
aic = &aic2;
116+
} else {
117+
printf("AIC: Error: Unsupported version\n");
118+
return;
119+
}
120+
121+
if (adt_get_reg(adt, path, "reg", 0, &aic->base, NULL)) {
65122
printf("Failed to get AIC reg property!\n");
66123
return;
67124
}
68125

69-
if (adt_is_compatible(adt, node, "aic,1")) {
70-
printf("AIC: Version 1 @ 0x%lx\n", aic_base);
71-
aic_regs = &aic1_regs;
72-
} else if (adt_is_compatible(adt, node, "aic,2")) {
73-
printf("AIC: Version 2 @ 0x%lx\n", aic_base);
74-
aic_regs = &aic2_regs;
75-
aic2_init(node);
76-
} else {
77-
printf("AIC: Error: Unsupported version @ 0x%lx\n", aic_base);
126+
if (aic->version == 1) {
127+
printf("AIC: Version 1 @ 0x%lx\n", aic->base);
128+
aic->nr_irq = FIELD_GET(AIC_INFO_NR_HW, read32(aic->base + AIC_INFO));
129+
aic->max_irq = AIC1_MAX_IRQ;
130+
} else if (aic->version == 2) {
131+
printf("AIC: Version 2 @ 0x%lx\n", aic->base);
132+
int ret = aic2_init(node);
133+
if (ret < 0)
134+
aic = NULL;
78135
}
79136
}
80137

81138
void aic_set_sw(int irq, bool active)
82139
{
140+
u32 die = irq / aic->max_irq;
141+
irq = irq % aic->max_irq;
83142
if (active)
84-
write32(aic_base + aic_regs->sw_set + MASK_REG(irq), MASK_BIT(irq));
143+
write32(aic->base + aic->regs.sw_set + die * aic->die_stride + MASK_REG(irq),
144+
MASK_BIT(irq));
85145
else
86-
write32(aic_base + aic_regs->sw_clr + MASK_REG(irq), MASK_BIT(irq));
146+
write32(aic->base + aic->regs.sw_clr + die * aic->die_stride + MASK_REG(irq),
147+
MASK_BIT(irq));
87148
}
88149

89150
uint32_t aic_ack(void)
90151
{
91-
return read32(aic_base + aic_regs->event);
152+
return read32(aic->base + aic->regs.event);
92153
}

src/aic.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "types.h"
77

8-
extern u64 aic_base;
8+
#define AIC_MAX_DIES 4
99

1010
struct aic_regs {
1111
uint64_t reg_size;
@@ -18,7 +18,20 @@ struct aic_regs {
1818
uint64_t mask_clr;
1919
};
2020

21-
extern const struct aic_regs *aic_regs;
21+
struct aic {
22+
uint64_t base;
23+
uint32_t version;
24+
25+
uint32_t nr_irq;
26+
uint32_t nr_die;
27+
uint32_t max_irq;
28+
uint32_t max_die;
29+
uint32_t die_stride;
30+
31+
struct aic_regs regs;
32+
};
33+
34+
extern struct aic *aic;
2235

2336
void aic_init(void);
2437
void aic_set_sw(int irq, bool active);

src/aic_regs.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,24 @@
1919
#define AIC_CPU_IPI_MASK_SET(cpu) (0x5024 + ((cpu) << 7))
2020
#define AIC_CPU_IPI_MASK_CLR(cpu) (0x5028 + ((cpu) << 7))
2121

22-
#define AIC2_REG_SIZE 0x10000
23-
#define AIC2_INFO 0x0004
24-
#define AIC2_LATENCY 0x0204
25-
#define AIC2_EVENT 0xc000
26-
#define AIC2_IRQ_CFG 0x2000
27-
#define AIC2_SW_SET 0x6000
28-
#define AIC2_SW_CLR 0x6200
29-
#define AIC2_MASK_SET 0x6400
30-
#define AIC2_MASK_CLR 0x6800
22+
#define AIC2_INFO1 0x0004
23+
#define AIC2_INFO2 0x0008
24+
#define AIC2_INFO3 0x000c
25+
#define AIC2_LATENCY 0x0204
26+
#define AIC2_IRQ_CFG 0x2000
3127

3228
#define AIC2_IRQ_CFG_TARGET GENMASK(3, 0)
3329

3430
#define AIC_INFO_NR_HW GENMASK(15, 0)
3531

36-
#define AIC_EVENT_TYPE GENMASK(31, 16)
32+
#define AIC2_INFO1_NR_IRQ GENMASK(15, 0)
33+
#define AIC2_INFO1_LAST_DIE GENMASK(27, 24)
34+
35+
#define AIC2_INFO3_MAX_IRQ GENMASK(15, 0)
36+
#define AIC2_INFO3_MAX_DIE GENMASK(27, 24)
37+
38+
#define AIC_EVENT_DIE GENMASK(31, 24)
39+
#define AIC_EVENT_TYPE GENMASK(23, 16)
3740
#define AIC_EVENT_NUM GENMASK(15, 0)
3841

3942
#define AIC_EVENT_TYPE_HW 1
@@ -46,4 +49,5 @@
4649
#define AIC_IPI_OTHER BIT(0)
4750
#define AIC_IPI_SELF BIT(31)
4851

49-
#define AIC_MAX_HW_NUM (0x80 * 32)
52+
#define AIC1_MAX_IRQ 0x400
53+
#define AIC_MAX_HW_NUM (0x80 * 32) // max_irq of the M1 Max

src/exception.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "exception.h"
44
#include "aic.h"
5+
#include "aic_regs.h"
56
#include "cpu_regs.h"
67
#include "gxf.h"
78
#include "iodev.h"
@@ -285,8 +286,9 @@ void exc_irq(u64 *regs)
285286
{
286287
u32 reason = aic_ack();
287288

288-
printf("Exception: IRQ (from %s) type: %d num: %d mpidr: %lx\n", get_exception_source(0),
289-
reason >> 16, reason & 0xffff, mrs(MPIDR_EL1));
289+
printf("Exception: IRQ (from %s) die: %lu type: %lu num: %lu mpidr: %lx\n",
290+
get_exception_source(0), FIELD_GET(AIC_EVENT_DIE, reason),
291+
FIELD_GET(AIC_EVENT_TYPE, reason), FIELD_GET(AIC_EVENT_NUM, reason), mrs(MPIDR_EL1));
290292

291293
UNUSED(regs);
292294
// print_regs(regs);

src/hv_aic.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
#define IRQTRACE_IRQ BIT(0)
1111

12-
static u32 trace_hw_num[AIC_MAX_HW_NUM / 32];
12+
static u32 trace_hw_num[AIC_MAX_DIES][AIC_MAX_HW_NUM / 32];
1313

14-
static void emit_irqtrace(u16 type, u16 num)
14+
static void emit_irqtrace(u16 die, u16 type, u16 num)
1515
{
1616
struct hv_evt_irqtrace evt = {
1717
.flags = IRQTRACE_IRQ,
1818
.type = type,
19-
.num = num,
19+
.num = die * aic->max_irq + num,
2020
};
2121

2222
hv_wdt_suspend();
@@ -29,17 +29,21 @@ static bool trace_aic_event(struct exc_info *ctx, u64 addr, u64 *val, bool write
2929
if (!hv_pa_rw(ctx, addr, val, write, width))
3030
return false;
3131

32-
if (addr != (aic_base + aic_regs->event) || write || width != 2) {
32+
if (addr != (aic->base + aic->regs.event) || write || width != 2) {
3333
return true;
3434
}
3535

36-
u16 type = (*val & AIC_EVENT_TYPE) >> 16;
37-
u16 num = *val & AIC_EVENT_NUM;
36+
u16 die = FIELD_GET(AIC_EVENT_DIE, *val);
37+
u16 type = FIELD_GET(AIC_EVENT_TYPE, *val);
38+
u16 num = FIELD_GET(AIC_EVENT_NUM, *val);
39+
40+
if (die > AIC_MAX_DIES)
41+
return true;
3842

3943
switch (type) {
4044
case AIC_EVENT_TYPE_HW:
41-
if (trace_hw_num[num / 32] & BIT(num & 31)) {
42-
emit_irqtrace(type, num);
45+
if (trace_hw_num[die][num / 32] & BIT(num & 31)) {
46+
emit_irqtrace(die, type, num);
4347
}
4448
break;
4549
default:
@@ -54,17 +58,19 @@ bool hv_trace_irq(u32 type, u32 num, u32 count, u32 flags)
5458
{
5559
dprintf("HV: hv_trace_irq type: %u start: %u num: %u flags: 0x%x\n", type, num, count, flags);
5660
if (type == AIC_EVENT_TYPE_HW) {
57-
if (num >= AIC_MAX_HW_NUM || count > AIC_MAX_HW_NUM - num) {
58-
printf("HV: invalid IRQ range: (%u, %u)\n", num, num + count);
61+
u32 die = num / aic->max_irq;
62+
num %= AIC_MAX_HW_NUM;
63+
if (die >= aic->max_irq || num >= AIC_MAX_HW_NUM || count > AIC_MAX_HW_NUM - num) {
64+
printf("HV: invalid IRQ range: (%u, %u) for die %u\n", num, num + count, die);
5965
return false;
6066
}
6167
for (u32 n = num; n < num + count; n++) {
6268
switch (flags) {
6369
case IRQTRACE_IRQ:
64-
trace_hw_num[n / 32] |= BIT(n & 31);
70+
trace_hw_num[die][n / 32] |= BIT(n & 31);
6571
break;
6672
default:
67-
trace_hw_num[n / 32] &= ~(BIT(n & 31));
73+
trace_hw_num[die][n / 32] &= ~(BIT(n & 31));
6874
break;
6975
}
7076
}
@@ -73,20 +79,15 @@ bool hv_trace_irq(u32 type, u32 num, u32 count, u32 flags)
7379
return false;
7480
}
7581

76-
if (!aic_base) {
82+
if (!aic) {
7783
printf("HV: AIC not initialized\n");
7884
return false;
7985
}
8086

8187
static bool hooked = false;
8288

83-
if (aic_base && !hooked) {
84-
u32 nr_hw = FIELD_GET(AIC_INFO_NR_HW, read32(aic_base + AIC_INFO));
85-
if (nr_hw > AIC_MAX_HW_NUM) {
86-
printf("HV: AIC supports more IRQs than expected! nr_hw: %u\n", nr_hw);
87-
return false;
88-
}
89-
hv_map_hook(aic_base, trace_aic_event, aic_regs->reg_size);
89+
if (aic && !hooked) {
90+
hv_map_hook(aic->base, trace_aic_event, aic->regs.reg_size);
9091
hooked = true;
9192
}
9293

0 commit comments

Comments
 (0)