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
|
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-11-25 GuEe-GUI first version
* 2025-06-19 GuEe-GUI support upstream brcm,bcm2712-mip DT format
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#define DBG_TAG "pic.mip-msi"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#define MIP_INT_RAISE 0x00
#define MIP_INT_CLEARE 0x10
#define MIP_INT_CFGL_HOST 0x20
#define MIP_INT_CFGH_HOST 0x30
#define MIP_INT_MASKL_HOST 0x40
#define MIP_INT_MASKH_HOST 0x50
#define MIP_INT_MASKL_VPU 0x60
#define MIP_INT_MASKH_VPU 0x70
#define MIP_INT_STATUSL_HOST 0x80
#define MIP_INT_STATUSH_HOST 0x90
#define MIP_INT_STATUSL_VPU 0xa0
#define MIP_INT_STATUSH_VPU 0xb0
struct mip_msi
{
struct rt_pic parent;
struct rt_pic *ppic;
struct rt_ofw_node *pic_np;
void *base;
rt_ubase_t msg_addr;
rt_uint32_t msi_base; /* The SGI/SPI number that MSIs start */
rt_uint32_t spi_base; /* msi_base + 32, GIC INTID for MIP hwirq 0 */
rt_uint32_t num_msis; /* The number of SGIs for MSIs */
rt_uint32_t msi_offset; /* Shift the allocated msi up by N */
rt_ubase_t *msi_map;
struct rt_spinlock msi_map_lock;
};
#define raw_to_mip_msi(raw) rt_container_of(raw, struct mip_msi, parent)
static rt_err_t mip_msi_parse_pci_addr(struct rt_ofw_node *np, rt_uint64_t *addr)
{
int nr;
rt_uint32_t cells[2];
nr = rt_ofw_prop_read_u32_array_index(np, "brcm,msi-pci-addr", 0, 2, cells);
if (nr < 2)
{
LOG_E("brcm,msi-pci-addr needs two u32 cells, got %d", nr);
return -RT_EINVAL;
}
*addr = ((rt_uint64_t)cells[0] << 32) | cells[1];
if (rt_upper_32_bits(*addr) != 0xffU)
{
LOG_E("brcm,msi-pci-addr hi=%#x lo=%#x (expected hi=0xff)",
rt_upper_32_bits(*addr), rt_lower_32_bits(*addr));
return -RT_EINVAL;
}
return RT_EOK;
}
static rt_err_t mip_msi_parse_intc_dt(struct mip_msi *mip, struct rt_ofw_node *np)
{
mip->pic_np = rt_ofw_find_irq_parent(np, RT_NULL);
if (!mip->pic_np)
{
LOG_E("Unable to find PIC parent");
return -RT_EINVAL;
}
if (rt_ofw_prop_read_u32(np, "brcm,msi-base-spi", &mip->msi_base))
{
LOG_E("Unable to parse MSI base");
return -RT_EINVAL;
}
if (rt_ofw_prop_read_u32(np, "brcm,msi-num-spis", &mip->num_msis))
{
LOG_E("Unable to parse MSI numbers");
return -RT_EINVAL;
}
if (rt_ofw_prop_read_u32(np, "brcm,msi-offset", &mip->msi_offset))
{
mip->msi_offset = 0;
}
if (mip_msi_parse_pci_addr(np, &mip->msg_addr))
{
return -RT_EINVAL;
}
return RT_EOK;
}
static rt_err_t mip_msi_parse_upstream_dt(struct mip_msi *mip, struct rt_ofw_node *np)
{
rt_err_t err;
rt_uint64_t size;
struct rt_ofw_cell_args args;
if (rt_ofw_prop_read_u32(np, "brcm,msi-offset", &mip->msi_offset))
{
mip->msi_offset = 0;
}
err = rt_ofw_parse_phandle_cells(np, "msi-ranges", "#interrupt-cells", 0, &args);
if (err)
{
LOG_E("Unable to parse msi-ranges");
return err;
}
mip->pic_np = args.data;
if (!mip->pic_np)
{
LOG_E("Unable to find MSI parent from msi-ranges");
return -RT_EINVAL;
}
if (args.args_count < 2)
{
LOG_E("Invalid msi-ranges interrupt specifier");
return -RT_EINVAL;
}
mip->msi_base = args.args[1];
if (rt_ofw_prop_read_u32_index(np, "msi-ranges", args.args_count + 1, &mip->num_msis))
{
LOG_E("Unable to parse MSI count from msi-ranges");
return -RT_EINVAL;
}
if (rt_ofw_get_address(np, 1, &mip->msg_addr, &size))
{
LOG_E("Unable to parse MSI doorbell address");
return -RT_EINVAL;
}
return RT_EOK;
}
static rt_err_t mip_msi_irq_init(struct rt_pic *pic)
{
struct mip_msi *mip = raw_to_mip_msi(pic);
mip->ppic = rt_ofw_data(mip->pic_np);
return mip->ppic ? RT_EOK : -RT_ENOSYS;
}
static int mip_msi_parent_hwirq(struct mip_msi *mip, int hwirq)
{
return hwirq + mip->spi_base;
}
static rt_err_t mip_msi_link_parent(struct mip_msi *mip, struct rt_pic_irq *pirq)
{
rt_err_t err;
int parent_irq;
if (!mip || !pirq || !mip->ppic)
{
return -RT_EINVAL;
}
if (pirq->parent)
{
return RT_EOK;
}
parent_irq = mip->ppic->ops->irq_map(mip->ppic,
mip_msi_parent_hwirq(mip, pirq->hwirq), RT_IRQ_MODE_EDGE_RISING);
if (parent_irq < 0)
{
return parent_irq;
}
err = rt_pic_cascade(pirq, parent_irq);
if (err)
{
LOG_E("MIP cascade virq=%d parent_irq=%d err=%d", pirq->irq, parent_irq, err);
}
return err;
}
static void mip_msi_irq_mask(struct rt_pic_irq *pirq)
{
rt_pci_msi_mask_irq(pirq);
rt_pic_irq_parent_mask(pirq);
}
static void mip_msi_irq_unmask(struct rt_pic_irq *pirq)
{
struct mip_msi *mip = raw_to_mip_msi(pirq->pic);
mip_msi_link_parent(mip, pirq);
rt_pci_msi_unmask_irq(pirq);
rt_pic_irq_parent_unmask(pirq);
}
static void mip_msi_compose_msi_msg(struct rt_pic_irq *pirq, struct rt_pci_msi_msg *msg)
{
struct mip_msi *mip = raw_to_mip_msi(pirq->pic);
msg->address_hi = rt_upper_32_bits(mip->msg_addr);
msg->address_lo = rt_lower_32_bits(mip->msg_addr);
msg->data = pirq->hwirq;
}
static int mip_msi_irq_alloc_msi(struct rt_pic *pic, struct rt_pci_msi_desc *msi_desc)
{
rt_ubase_t level;
rt_err_t err;
int irq, hwirq, hwirq_index;
struct rt_pic_irq *pirq;
struct mip_msi *mip = raw_to_mip_msi(pic);
RT_UNUSED(msi_desc);
if (!mip->ppic)
{
LOG_E("MIP parent PIC not ready");
return -RT_ENOSYS;
}
level = rt_spin_lock_irqsave(&mip->msi_map_lock);
hwirq_index = rt_bitmap_next_clear_bit(mip->msi_map, 0, mip->num_msis);
if (hwirq_index >= mip->num_msis)
{
irq = -RT_EEMPTY;
goto _out_lock;
}
hwirq = hwirq_index + mip->msi_offset;
irq = rt_pic_config_irq(pic, hwirq_index, hwirq);
if (irq < 0)
{
goto _out_lock;
}
pirq = rt_pic_find_irq(pic, hwirq_index);
pirq->mode = RT_IRQ_MODE_EDGE_RISING;
err = mip_msi_link_parent(mip, pirq);
if (err)
{
irq = err;
goto _out_lock;
}
rt_bitmap_set_bit(mip->msi_map, hwirq_index);
_out_lock:
rt_spin_unlock_irqrestore(&mip->msi_map_lock, level);
return irq;
}
static void mip_msi_irq_free_msi(struct rt_pic *pic, int irq)
{
rt_ubase_t level;
struct rt_pic_irq *pirq;
struct mip_msi *mip = raw_to_mip_msi(pic);
pirq = rt_pic_find_pirq(pic, irq);
if (!pirq)
{
return;
}
rt_hw_interrupt_mask(irq);
rt_pic_uncascade(pirq);
level = rt_spin_lock_irqsave(&mip->msi_map_lock);
rt_bitmap_clear_bit(mip->msi_map, pirq->hwirq - mip->msi_offset);
rt_spin_unlock_irqrestore(&mip->msi_map_lock, level);
}
static rt_err_t mip_msi_irq_set_state(struct rt_pic *pic, int hwirq, int type, rt_bool_t state)
{
struct mip_msi *mip = raw_to_mip_msi(pic);
struct rt_pic *ppic = mip->ppic;
if (ppic->ops->irq_set_state)
{
return ppic->ops->irq_set_state(ppic, mip_msi_parent_hwirq(mip, hwirq),
type, state);
}
return -RT_ENOSYS;
}
static rt_err_t mip_msi_irq_get_state(struct rt_pic *pic, int hwirq, int type, rt_bool_t *out_state)
{
struct mip_msi *mip = raw_to_mip_msi(pic);
struct rt_pic *ppic = mip->ppic;
if (ppic->ops->irq_get_state)
{
return ppic->ops->irq_get_state(ppic, mip_msi_parent_hwirq(mip, hwirq),
type, out_state);
}
return -RT_ENOSYS;
}
const static struct rt_pic_ops mip_msi_ops =
{
.name = "MIP-MSI",
.irq_init = mip_msi_irq_init,
.irq_mask = mip_msi_irq_mask,
.irq_unmask = mip_msi_irq_unmask,
/* irq_eoi omitted: GIC root handler performs a single EOI. */
.irq_set_affinity = rt_pic_irq_parent_set_affinity,
.irq_set_triger_mode = rt_pic_irq_parent_set_triger_mode,
.irq_compose_msi_msg = mip_msi_compose_msi_msg,
.irq_alloc_msi = mip_msi_irq_alloc_msi,
.irq_free_msi = mip_msi_irq_free_msi,
.irq_set_state = mip_msi_irq_set_state,
.irq_get_state = mip_msi_irq_get_state,
.flags = RT_PIC_F_IRQ_ROUTING,
};
static rt_err_t mip_msi_ofw_init(struct rt_ofw_node *np, const struct rt_ofw_node_id *id)
{
rt_err_t err;
struct mip_msi *mip = rt_calloc(1, sizeof(*mip));
if (!mip)
{
return -RT_ENOMEM;
}
if (!rt_strcmp(id->compatible, "brcm,bcm2712-mip-intc"))
{
err = mip_msi_parse_intc_dt(mip, np);
}
else
{
err = mip_msi_parse_upstream_dt(mip, np);
}
if (err)
{
goto _fail;
}
/* SPI offset from MSI base */
mip->spi_base = mip->msi_base + 32;
mip->base = rt_ofw_iomap(np, 0);
if (!mip->base)
{
err = -RT_EIO;
goto _fail;
}
mip->msi_map = rt_calloc(RT_BITS_TO_LONGS(mip->num_msis), sizeof(*mip->msi_map));
if (!mip->msi_map)
{
err = -RT_ENOMEM;
goto _fail;
}
/*
* Begin with all MSI-Xs masked in for the host, masked out for the VPU,
* and edge-triggered.
*/
HWREG32(mip->base + MIP_INT_MASKL_HOST) = 0;
HWREG32(mip->base + MIP_INT_MASKH_HOST) = 0;
HWREG32(mip->base + MIP_INT_MASKL_VPU) = ~0;
HWREG32(mip->base + MIP_INT_MASKH_VPU) = ~0;
HWREG32(mip->base + MIP_INT_CFGL_HOST) = ~0;
HWREG32(mip->base + MIP_INT_CFGH_HOST) = ~0;
LOG_D("Found %d MSIx, starting at %d (offset %d)", mip->num_msis, mip->msi_base, mip->msi_offset);
rt_spin_lock_init(&mip->msi_map_lock);
mip->parent.priv_data = mip;
mip->parent.ops = &mip_msi_ops;
rt_pic_linear_irq(&mip->parent, mip->num_msis);
rt_pic_user_extends(&mip->parent);
rt_ofw_data(np) = &mip->parent;
rt_ofw_node_set_flag(np, RT_OFW_F_READLY);
return RT_EOK;
_fail:
if (mip->base)
{
rt_iounmap(mip->base);
}
if (mip->msi_map)
{
rt_free(mip->msi_map);
}
rt_free(mip);
return err;
}
static const struct rt_ofw_node_id mip_msi_ofw_ids[] =
{
{ .compatible = "brcm,bcm2712-mip-intc" },
{ .compatible = "brcm,bcm2712-mip" },
{ /* sentinel */ }
};
RT_PIC_OFW_DECLARE(mip_msi, mip_msi_ofw_ids, mip_msi_ofw_init);
|