hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
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
/*
 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:     GPL-2.0+
 */
 
#include <common.h>
#include <dm.h>
#include <fdtdec.h>
#include <malloc.h>
#include <asm/io.h>
#include <asm/u-boot-arm.h>
#include <irq-generic.h>
#include "irq-internal.h"
 
DECLARE_GLOBAL_DATA_PTR;
 
static LIST_HEAD(virq_desc_head);
static u32 virq_id = PLATFORM_MAX_IRQ;
 
static u32 virq_id_alloc(void)
{
   return ++virq_id;
}
 
struct virq_data {
   int irq;
   u32 flag;
   u32 count;
 
   void *data;
   interrupt_handler_t *handle_irq;
};
 
/* The structure to maintail the irqchip and child virqs */
struct virq_desc {
   struct virq_chip *chip;        /* irq chip */
   struct virq_data *virqs;    /* child irq data list */
   struct udevice *parent;        /* parent device */
   int pirq;            /* parent irq */
   int use_count;            /* enable count */
   int irq_base;            /* child irq base */
   int irq_end;            /* child irq end */
   uint reg_stride;
   uint unalign_reg_idx;
   uint unalign_reg_stride;
   uint *status_buf;
 
   struct list_head node;
};
 
static struct virq_desc *find_virq_desc(int irq)
{
   struct virq_desc *desc;
   struct list_head *node;
 
   list_for_each(node, &virq_desc_head) {
       desc = list_entry(node, struct virq_desc, node);
       if (irq >= desc->irq_base && irq <= desc->irq_end)
           return desc;
   }
 
   return NULL;
}
 
static struct virq_desc *find_virq_desc_by_pirq(int parent_irq)
{
   struct virq_desc *desc;
   struct list_head *node;
 
   list_for_each(node, &virq_desc_head) {
       desc = list_entry(node, struct virq_desc, node);
       if (parent_irq == desc->pirq)
           return desc;
   }
 
   return NULL;
}
 
int virq_to_irq(struct virq_chip *chip, int virq)
{
   struct virq_desc *desc;
   struct list_head *node;
   int irq;
 
   if (!chip)
       return -EINVAL;
 
   list_for_each(node, &virq_desc_head) {
       desc = list_entry(node, struct virq_desc, node);
       if (desc->chip == chip) {
           irq = desc->irq_base + virq;
           if (irq >= desc->irq_base && irq <= desc->irq_end)
               return irq;
       }
   }
 
   return -ENONET;
}
 
int bad_virq(int irq)
{
   return !find_virq_desc(irq);
}
 
void virqs_show(int pirq)
{
   struct virq_data *vdata;
   struct virq_desc *desc;
   struct udevice *dev;
   int num;
   int i;
 
   desc = find_virq_desc_by_pirq(pirq);
   if (!desc)
          return;
 
   vdata = desc->virqs;
   num = desc->irq_end - desc->irq_base;
 
   for (i = 0; i < num; i++) {
       if (!vdata[i].handle_irq)
           continue;
 
       dev = (struct udevice *)vdata[i].data;
       printf(" %3d    %d     0x%08lx    %-12s    |-- %-12s   %d\n",
              vdata[i].irq,
              vdata[i].flag & IRQ_FLG_ENABLE ? 1 : 0,
              (ulong)vdata[i].handle_irq, dev->driver->name, dev->name,
              vdata[i].count);
   }
}
 
int virq_install_handler(int irq, interrupt_handler_t *handler, void *data)
{
   struct virq_desc *desc;
   int virq;
 
   if (!handler)
       return -EINVAL;
 
   desc = find_virq_desc(irq);
   if (!desc)
       return -ENOENT;
 
   virq = irq - desc->irq_base;
   if (desc->virqs[virq].handle_irq)
       return -EBUSY;
 
   desc->virqs[virq].handle_irq = handler;
   desc->virqs[virq].data = data;
 
   return 0;
}
 
void virq_free_handler(int irq)
{
   struct virq_desc *desc;
   int virq;
 
   desc = find_virq_desc(irq);
   if (!desc)
       return;
 
   virq = irq - desc->irq_base;
   desc->virqs[virq].handle_irq = NULL;
   desc->virqs[virq].data = NULL;
}
 
static uint reg_base_get(struct virq_desc *desc, uint reg_base, int idx)
{
   int reg_addr;
 
   if (idx <= desc->unalign_reg_idx) {
       reg_addr = reg_base + (idx * desc->unalign_reg_stride);
   } else {
       reg_addr = reg_base +
              (desc->unalign_reg_idx * desc->unalign_reg_stride);
       reg_addr += (idx - desc->unalign_reg_idx) * desc->reg_stride;
   }
 
   return reg_addr;
}
 
void virq_chip_generic_handler(int pirq, void *pdata)
{
   struct virq_chip *chip;
   struct virq_desc *desc;
   struct virq_data *vdata;
   struct udevice *parent;
   uint status_reg;
   void *data;
   int irq;
   int ret;
   int i;
 
   desc = find_virq_desc_by_pirq(pirq);
   if (!desc)
       return;
 
   chip = desc->chip;
   vdata = desc->virqs;
   parent = (struct udevice *)pdata;
 
   if (!chip || !vdata || !parent)
       return;
 
   /* Read all status register */
   for (i = 0; i < chip->num_regs; i++) {
       status_reg = reg_base_get(desc, chip->status_base, i);
       desc->status_buf[i] = chip->read(parent, status_reg);
       if (desc->status_buf[i] < 0) {
           printf("%s: Read status register 0x%x failed, ret=%d\n",
                  __func__, status_reg, desc->status_buf[i]);
       }
   }
 
   /* Handle all virq handler */
   for (i = 0; i < chip->num_irqs; i++) {
       if (desc->status_buf[chip->irqs[i].reg_offset] &
           chip->irqs[i].mask) {
           irq = vdata[i].irq;
           data = vdata[i].data;
 
           if (vdata[i].handle_irq) {
               vdata[i].count++;
               vdata[i].handle_irq(irq, data);
           }
       }
   }
 
   /* Clear all status register */
   for (i = 0; i < chip->num_regs; i++) {
       status_reg = reg_base_get(desc, chip->status_base, i);
       ret = chip->write(parent, status_reg, ~0U);
       if (ret)
           printf("%s: Clear status register 0x%x failed, ret=%d\n",
                  __func__, status_reg, ret);
   }
}
 
int virq_add_chip(struct udevice *dev, struct virq_chip *chip, int irq)
{
   struct virq_data *vdata;
   struct virq_desc *desc;
   uint *status_buf;
   uint status_reg;
   uint mask_reg;
   int ret;
   int i;
 
   if (irq < 0)
       return -EINVAL;
 
   desc = (struct virq_desc *)malloc(sizeof(*desc));
   if (!desc)
       return -ENOMEM;
 
   vdata = (struct virq_data *)calloc(sizeof(*vdata), chip->num_irqs);
   if (!vdata) {
       ret = -ENOMEM;
       goto free1;
   }
 
   status_buf = (uint *)calloc(sizeof(*status_buf), chip->num_irqs);
   if (!status_buf) {
       ret = -ENOMEM;
       goto free2;
   }
 
   for (i = 0; i < chip->num_irqs; i++)
       vdata[i].irq = virq_id_alloc();
 
   desc->parent = dev;
   desc->pirq = irq;
   desc->chip = chip;
   desc->virqs = vdata;
   desc->use_count = 0;
   desc->irq_base = vdata[0].irq;
   desc->irq_end = vdata[chip->num_irqs - 1].irq;
   desc->status_buf = status_buf;
   desc->reg_stride = chip->irq_reg_stride ? : 1;
   desc->unalign_reg_stride = chip->irq_unalign_reg_stride ? : 1;
   desc->unalign_reg_idx = chip->irq_unalign_reg_stride ?
                   chip->irq_unalign_reg_idx : 0;
   list_add_tail(&desc->node, &virq_desc_head);
 
   /* Mask all register */
   for (i = 0; i < chip->num_regs; i++) {
       mask_reg = reg_base_get(desc, chip->mask_base, i);
       ret = chip->write(dev, mask_reg, ~0U);
       if (ret)
           printf("%s: Set mask register 0x%x failed, ret=%d\n",
                  __func__, mask_reg, ret);
   }
 
   /* Clear all status */
   for (i = 0; i < chip->num_regs; i++) {
       status_reg = reg_base_get(desc, chip->status_base, i);
       ret = chip->write(dev, status_reg, ~0U);
       if (ret)
           printf("%s: Clear status register 0x%x failed, ret=%d\n",
                  __func__, status_reg, ret);
   }
 
   /* Add parent irq into interrupt framework with generic virq handler */
   irq_install_handler(irq, virq_chip_generic_handler, dev);
 
   return irq_handler_disable(irq);
 
free1:
   free(desc);
free2:
   free(status_buf);
 
   return ret;
}
 
static int virq_init(void)
{
   INIT_LIST_HEAD(&virq_desc_head);
   return 0;
}
 
static int __virq_enable(int irq, int enable)
{
   struct virq_chip *chip;
   struct virq_desc *desc;
   uint mask_reg, mask_val;
   uint reg_val;
   int virq;
   int ret;
 
   desc = find_virq_desc(irq);
   if (!desc) {
       printf("%s: %s Invalid irq %d\n",
              __func__, enable ? "Enable" : "Disable", irq);
       return -ENOENT;
   }
 
   chip = desc->chip;
   if (!chip)
       return -ENOENT;
 
   virq = irq - desc->irq_base;
   mask_val = chip->irqs[virq].mask;
   mask_reg = reg_base_get(desc, chip->mask_base,
               chip->irqs[virq].reg_offset);
   reg_val = chip->read(desc->parent, mask_reg);
   if (enable)
       reg_val &= ~mask_val;
   else
       reg_val |= mask_val;
 
   ret = chip->write(desc->parent, mask_reg, reg_val);
   if (ret) {
       printf("%s: Clear status register 0x%x failed, ret=%d\n",
              __func__, mask_reg, ret);
       return ret;
   }
 
   if (enable)
       desc->virqs[virq].flag |= IRQ_FLG_ENABLE;
   else
       desc->virqs[virq].flag &= ~IRQ_FLG_ENABLE;
 
   return 0;
}
 
static int virq_enable(int irq)
{
   struct virq_desc *desc = find_virq_desc(irq);
   int ret;
 
   if (bad_virq(irq))
       return -EINVAL;
 
   ret = __virq_enable(irq, 1);
   if (!ret) {
       if (desc->use_count == 0)
           irq_handler_enable(desc->pirq);
       desc->use_count++;
   }
 
   return ret;
}
 
static int virq_disable(int irq)
{
   struct virq_desc *desc = find_virq_desc(irq);
   int ret;
 
   if (bad_virq(irq))
       return -EINVAL;
 
   ret = __virq_enable(irq, 0);
   if (!ret) {
       if (desc->use_count <= 0)
           return ret;
 
       if (desc->use_count == 1)
           irq_handler_disable(desc->pirq);
       desc->use_count--;
   }
 
   return ret;
}
 
struct irq_chip virq_generic_chip = {
   .name        = "virq-irq-chip",
   .irq_init    = virq_init,
   .irq_enable    = virq_enable,
   .irq_disable    = virq_disable,
};
 
struct irq_chip *arch_virq_get_irqchip(void)
{
   return &virq_generic_chip;
}