1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2021 Western Digital Corporation or its affiliates.
4 * Copyright (C) 2022 Ventana Micro Systems Inc.
5 */
6
7 #define pr_fmt(fmt) "riscv-imsic: " fmt
8 #include <linux/acpi.h>
9 #include <linux/cpu.h>
10 #include <linux/bitmap.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
17 #include <linux/seq_file.h>
18 #include <linux/spinlock.h>
19 #include <linux/smp.h>
20 #include <asm/hwcap.h>
21
22 #include "irq-riscv-imsic-state.h"
23
24 #define IMSIC_DISABLE_EIDELIVERY 0
25 #define IMSIC_ENABLE_EIDELIVERY 1
26 #define IMSIC_DISABLE_EITHRESHOLD 1
27 #define IMSIC_ENABLE_EITHRESHOLD 0
28
imsic_csr_write(unsigned long reg,unsigned long val)29 static inline void imsic_csr_write(unsigned long reg, unsigned long val)
30 {
31 csr_write(CSR_ISELECT, reg);
32 csr_write(CSR_IREG, val);
33 }
34
imsic_csr_read(unsigned long reg)35 static inline unsigned long imsic_csr_read(unsigned long reg)
36 {
37 csr_write(CSR_ISELECT, reg);
38 return csr_read(CSR_IREG);
39 }
40
imsic_csr_read_clear(unsigned long reg,unsigned long val)41 static inline unsigned long imsic_csr_read_clear(unsigned long reg, unsigned long val)
42 {
43 csr_write(CSR_ISELECT, reg);
44 return csr_read_clear(CSR_IREG, val);
45 }
46
imsic_csr_set(unsigned long reg,unsigned long val)47 static inline void imsic_csr_set(unsigned long reg, unsigned long val)
48 {
49 csr_write(CSR_ISELECT, reg);
50 csr_set(CSR_IREG, val);
51 }
52
imsic_csr_clear(unsigned long reg,unsigned long val)53 static inline void imsic_csr_clear(unsigned long reg, unsigned long val)
54 {
55 csr_write(CSR_ISELECT, reg);
56 csr_clear(CSR_IREG, val);
57 }
58
59 struct imsic_priv *imsic;
60
imsic_get_global_config(void)61 const struct imsic_global_config *imsic_get_global_config(void)
62 {
63 return imsic ? &imsic->global : NULL;
64 }
65 EXPORT_SYMBOL_GPL(imsic_get_global_config);
66
__imsic_eix_read_clear(unsigned long id,bool pend)67 static bool __imsic_eix_read_clear(unsigned long id, bool pend)
68 {
69 unsigned long isel, imask;
70
71 isel = id / BITS_PER_LONG;
72 isel *= BITS_PER_LONG / IMSIC_EIPx_BITS;
73 isel += pend ? IMSIC_EIP0 : IMSIC_EIE0;
74 imask = BIT(id & (__riscv_xlen - 1));
75
76 return !!(imsic_csr_read_clear(isel, imask) & imask);
77 }
78
__imsic_id_read_clear_enabled(unsigned long id)79 static inline bool __imsic_id_read_clear_enabled(unsigned long id)
80 {
81 return __imsic_eix_read_clear(id, false);
82 }
83
__imsic_id_read_clear_pending(unsigned long id)84 static inline bool __imsic_id_read_clear_pending(unsigned long id)
85 {
86 return __imsic_eix_read_clear(id, true);
87 }
88
__imsic_eix_update(unsigned long base_id,unsigned long num_id,bool pend,bool val)89 void __imsic_eix_update(unsigned long base_id, unsigned long num_id, bool pend, bool val)
90 {
91 unsigned long id = base_id, last_id = base_id + num_id;
92 unsigned long i, isel, ireg;
93
94 while (id < last_id) {
95 isel = id / BITS_PER_LONG;
96 isel *= BITS_PER_LONG / IMSIC_EIPx_BITS;
97 isel += pend ? IMSIC_EIP0 : IMSIC_EIE0;
98
99 /*
100 * Prepare the ID mask to be programmed in the
101 * IMSIC EIEx and EIPx registers. These registers
102 * are XLEN-wide and we must not touch IDs which
103 * are < base_id and >= (base_id + num_id).
104 */
105 ireg = 0;
106 for (i = id & (__riscv_xlen - 1); id < last_id && i < __riscv_xlen; i++) {
107 ireg |= BIT(i);
108 id++;
109 }
110
111 /*
112 * The IMSIC EIEx and EIPx registers are indirectly
113 * accessed via using ISELECT and IREG CSRs so we
114 * need to access these CSRs without getting preempted.
115 *
116 * All existing users of this function call this
117 * function with local IRQs disabled so we don't
118 * need to do anything special here.
119 */
120 if (val)
121 imsic_csr_set(isel, ireg);
122 else
123 imsic_csr_clear(isel, ireg);
124 }
125 }
126
__imsic_local_sync(struct imsic_local_priv * lpriv)127 static bool __imsic_local_sync(struct imsic_local_priv *lpriv)
128 {
129 struct imsic_local_config *tlocal, *mlocal;
130 struct imsic_vector *vec, *tvec, *mvec;
131 bool ret = true;
132 int i;
133
134 lockdep_assert_held(&lpriv->lock);
135
136 for_each_set_bit(i, lpriv->dirty_bitmap, imsic->global.nr_ids + 1) {
137 if (!i || i == IMSIC_IPI_ID)
138 goto skip;
139 vec = &lpriv->vectors[i];
140
141 if (READ_ONCE(vec->enable))
142 __imsic_id_set_enable(i);
143 else
144 __imsic_id_clear_enable(i);
145
146 /*
147 * Clear the previous vector pointer of the new vector only
148 * after the movement is complete on the old CPU.
149 */
150 mvec = READ_ONCE(vec->move_prev);
151 if (mvec) {
152 /*
153 * If the old vector has not been updated then
154 * try again in the next sync-up call.
155 */
156 if (READ_ONCE(mvec->move_next)) {
157 ret = false;
158 continue;
159 }
160
161 WRITE_ONCE(vec->move_prev, NULL);
162 }
163
164 /*
165 * If a vector was being moved to a new vector on some other
166 * CPU then we can get a MSI during the movement so check the
167 * ID pending bit and re-trigger the new ID on other CPU using
168 * MMIO write.
169 */
170 mvec = READ_ONCE(vec->move_next);
171 if (mvec) {
172 /*
173 * Devices having non-atomic MSI update might see
174 * an intermediate state so check both old ID and
175 * new ID for pending interrupts.
176 *
177 * For details, see imsic_irq_set_affinity().
178 */
179 tvec = vec->local_id == mvec->local_id ?
180 NULL : &lpriv->vectors[mvec->local_id];
181
182 if (tvec && !irq_can_move_in_process_context(irq_get_irq_data(vec->irq)) &&
183 __imsic_id_read_clear_pending(tvec->local_id)) {
184 /* Retrigger temporary vector if it was already in-use */
185 if (READ_ONCE(tvec->enable)) {
186 tlocal = per_cpu_ptr(imsic->global.local, tvec->cpu);
187 writel_relaxed(tvec->local_id, tlocal->msi_va);
188 }
189
190 mlocal = per_cpu_ptr(imsic->global.local, mvec->cpu);
191 writel_relaxed(mvec->local_id, mlocal->msi_va);
192 }
193
194 if (__imsic_id_read_clear_pending(vec->local_id)) {
195 mlocal = per_cpu_ptr(imsic->global.local, mvec->cpu);
196 writel_relaxed(mvec->local_id, mlocal->msi_va);
197 }
198
199 WRITE_ONCE(vec->move_next, NULL);
200 imsic_vector_free(vec);
201 }
202
203 skip:
204 bitmap_clear(lpriv->dirty_bitmap, i, 1);
205 }
206
207 return ret;
208 }
209
210 #ifdef CONFIG_SMP
__imsic_local_timer_start(struct imsic_local_priv * lpriv,unsigned int cpu)211 static void __imsic_local_timer_start(struct imsic_local_priv *lpriv, unsigned int cpu)
212 {
213 lockdep_assert_held(&lpriv->lock);
214
215 if (!timer_pending(&lpriv->timer)) {
216 lpriv->timer.expires = jiffies + 1;
217 add_timer_on(&lpriv->timer, cpu);
218 }
219 }
220 #else
__imsic_local_timer_start(struct imsic_local_priv * lpriv,unsigned int cpu)221 static inline void __imsic_local_timer_start(struct imsic_local_priv *lpriv, unsigned int cpu)
222 {
223 }
224 #endif
225
imsic_local_sync_all(bool force_all)226 void imsic_local_sync_all(bool force_all)
227 {
228 struct imsic_local_priv *lpriv = this_cpu_ptr(imsic->lpriv);
229 unsigned long flags;
230
231 raw_spin_lock_irqsave(&lpriv->lock, flags);
232
233 if (force_all)
234 bitmap_fill(lpriv->dirty_bitmap, imsic->global.nr_ids + 1);
235 if (!__imsic_local_sync(lpriv))
236 __imsic_local_timer_start(lpriv, smp_processor_id());
237
238 raw_spin_unlock_irqrestore(&lpriv->lock, flags);
239 }
240
imsic_local_delivery(bool enable)241 void imsic_local_delivery(bool enable)
242 {
243 if (enable) {
244 imsic_csr_write(IMSIC_EITHRESHOLD, IMSIC_ENABLE_EITHRESHOLD);
245 imsic_csr_write(IMSIC_EIDELIVERY, IMSIC_ENABLE_EIDELIVERY);
246 return;
247 }
248
249 imsic_csr_write(IMSIC_EIDELIVERY, IMSIC_DISABLE_EIDELIVERY);
250 imsic_csr_write(IMSIC_EITHRESHOLD, IMSIC_DISABLE_EITHRESHOLD);
251 }
252
253 #ifdef CONFIG_SMP
imsic_local_timer_callback(struct timer_list * timer)254 static void imsic_local_timer_callback(struct timer_list *timer)
255 {
256 imsic_local_sync_all(false);
257 }
258
__imsic_remote_sync(struct imsic_local_priv * lpriv,unsigned int cpu)259 static void __imsic_remote_sync(struct imsic_local_priv *lpriv, unsigned int cpu)
260 {
261 lockdep_assert_held(&lpriv->lock);
262
263 /*
264 * The spinlock acquire/release semantics ensure that changes
265 * to vector enable, vector move and dirty bitmap are visible
266 * to the target CPU.
267 */
268
269 /*
270 * We schedule a timer on the target CPU if the target CPU is not
271 * same as the current CPU. An offline CPU will unconditionally
272 * synchronize IDs through imsic_starting_cpu() when the
273 * CPU is brought up.
274 */
275 if (cpu_online(cpu)) {
276 if (cpu == smp_processor_id()) {
277 if (__imsic_local_sync(lpriv))
278 return;
279 }
280
281 __imsic_local_timer_start(lpriv, cpu);
282 }
283 }
284 #else
__imsic_remote_sync(struct imsic_local_priv * lpriv,unsigned int cpu)285 static void __imsic_remote_sync(struct imsic_local_priv *lpriv, unsigned int cpu)
286 {
287 lockdep_assert_held(&lpriv->lock);
288 __imsic_local_sync(lpriv);
289 }
290 #endif
291
imsic_vector_mask(struct imsic_vector * vec)292 void imsic_vector_mask(struct imsic_vector *vec)
293 {
294 struct imsic_local_priv *lpriv;
295
296 lpriv = per_cpu_ptr(imsic->lpriv, vec->cpu);
297 if (WARN_ON_ONCE(&lpriv->vectors[vec->local_id] != vec))
298 return;
299
300 /*
301 * This function is called through Linux irq subsystem with
302 * irqs disabled so no need to save/restore irq flags.
303 */
304
305 raw_spin_lock(&lpriv->lock);
306
307 WRITE_ONCE(vec->enable, false);
308 bitmap_set(lpriv->dirty_bitmap, vec->local_id, 1);
309 __imsic_remote_sync(lpriv, vec->cpu);
310
311 raw_spin_unlock(&lpriv->lock);
312 }
313
imsic_vector_unmask(struct imsic_vector * vec)314 void imsic_vector_unmask(struct imsic_vector *vec)
315 {
316 struct imsic_local_priv *lpriv;
317
318 lpriv = per_cpu_ptr(imsic->lpriv, vec->cpu);
319 if (WARN_ON_ONCE(&lpriv->vectors[vec->local_id] != vec))
320 return;
321
322 /*
323 * This function is called through Linux irq subsystem with
324 * irqs disabled so no need to save/restore irq flags.
325 */
326
327 raw_spin_lock(&lpriv->lock);
328
329 WRITE_ONCE(vec->enable, true);
330 bitmap_set(lpriv->dirty_bitmap, vec->local_id, 1);
331 __imsic_remote_sync(lpriv, vec->cpu);
332
333 raw_spin_unlock(&lpriv->lock);
334 }
335
imsic_vector_force_move_cleanup(struct imsic_vector * vec)336 void imsic_vector_force_move_cleanup(struct imsic_vector *vec)
337 {
338 struct imsic_local_priv *lpriv;
339 struct imsic_vector *mvec;
340 unsigned long flags;
341
342 lpriv = per_cpu_ptr(imsic->lpriv, vec->cpu);
343 raw_spin_lock_irqsave(&lpriv->lock, flags);
344
345 mvec = READ_ONCE(vec->move_prev);
346 WRITE_ONCE(vec->move_prev, NULL);
347 if (mvec)
348 imsic_vector_free(mvec);
349
350 raw_spin_unlock_irqrestore(&lpriv->lock, flags);
351 }
352
imsic_vector_move_update(struct imsic_local_priv * lpriv,struct imsic_vector * vec,bool is_old_vec,bool new_enable,struct imsic_vector * move_vec)353 static bool imsic_vector_move_update(struct imsic_local_priv *lpriv,
354 struct imsic_vector *vec, bool is_old_vec,
355 bool new_enable, struct imsic_vector *move_vec)
356 {
357 unsigned long flags;
358 bool enabled;
359
360 raw_spin_lock_irqsave(&lpriv->lock, flags);
361
362 /* Update enable and move details */
363 enabled = READ_ONCE(vec->enable);
364 WRITE_ONCE(vec->enable, new_enable);
365 if (is_old_vec)
366 WRITE_ONCE(vec->move_next, move_vec);
367 else
368 WRITE_ONCE(vec->move_prev, move_vec);
369
370 /* Mark the vector as dirty and synchronize */
371 bitmap_set(lpriv->dirty_bitmap, vec->local_id, 1);
372 __imsic_remote_sync(lpriv, vec->cpu);
373
374 raw_spin_unlock_irqrestore(&lpriv->lock, flags);
375
376 return enabled;
377 }
378
imsic_vector_move(struct imsic_vector * old_vec,struct imsic_vector * new_vec)379 void imsic_vector_move(struct imsic_vector *old_vec, struct imsic_vector *new_vec)
380 {
381 struct imsic_local_priv *old_lpriv, *new_lpriv;
382 bool enabled;
383
384 if (WARN_ON_ONCE(old_vec->cpu == new_vec->cpu))
385 return;
386
387 old_lpriv = per_cpu_ptr(imsic->lpriv, old_vec->cpu);
388 if (WARN_ON_ONCE(&old_lpriv->vectors[old_vec->local_id] != old_vec))
389 return;
390
391 new_lpriv = per_cpu_ptr(imsic->lpriv, new_vec->cpu);
392 if (WARN_ON_ONCE(&new_lpriv->vectors[new_vec->local_id] != new_vec))
393 return;
394
395 /*
396 * Move and re-trigger the new vector based on the pending
397 * state of the old vector because we might get a device
398 * interrupt on the old vector while device was being moved
399 * to the new vector.
400 */
401 enabled = imsic_vector_move_update(old_lpriv, old_vec, true, false, new_vec);
402 imsic_vector_move_update(new_lpriv, new_vec, false, enabled, old_vec);
403 }
404
405 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
imsic_vector_debug_show(struct seq_file * m,struct imsic_vector * vec,int ind)406 void imsic_vector_debug_show(struct seq_file *m, struct imsic_vector *vec, int ind)
407 {
408 struct imsic_local_priv *lpriv;
409 struct imsic_vector *mvec;
410 bool is_enabled;
411
412 lpriv = per_cpu_ptr(imsic->lpriv, vec->cpu);
413 if (WARN_ON_ONCE(&lpriv->vectors[vec->local_id] != vec))
414 return;
415
416 is_enabled = imsic_vector_isenabled(vec);
417 mvec = imsic_vector_get_move(vec);
418
419 seq_printf(m, "%*starget_cpu : %5u\n", ind, "", vec->cpu);
420 seq_printf(m, "%*starget_local_id : %5u\n", ind, "", vec->local_id);
421 seq_printf(m, "%*sis_reserved : %5u\n", ind, "",
422 (vec->local_id <= IMSIC_IPI_ID) ? 1 : 0);
423 seq_printf(m, "%*sis_enabled : %5u\n", ind, "", is_enabled ? 1 : 0);
424 seq_printf(m, "%*sis_move_pending : %5u\n", ind, "", mvec ? 1 : 0);
425 if (mvec) {
426 seq_printf(m, "%*smove_cpu : %5u\n", ind, "", mvec->cpu);
427 seq_printf(m, "%*smove_local_id : %5u\n", ind, "", mvec->local_id);
428 }
429 }
430
imsic_vector_debug_show_summary(struct seq_file * m,int ind)431 void imsic_vector_debug_show_summary(struct seq_file *m, int ind)
432 {
433 irq_matrix_debug_show(m, imsic->matrix, ind);
434 }
435 #endif
436
imsic_vector_from_local_id(unsigned int cpu,unsigned int local_id)437 struct imsic_vector *imsic_vector_from_local_id(unsigned int cpu, unsigned int local_id)
438 {
439 struct imsic_local_priv *lpriv = per_cpu_ptr(imsic->lpriv, cpu);
440
441 if (!lpriv || imsic->global.nr_ids < local_id)
442 return NULL;
443
444 return &lpriv->vectors[local_id];
445 }
446
imsic_vector_alloc(unsigned int irq,const struct cpumask * mask)447 struct imsic_vector *imsic_vector_alloc(unsigned int irq, const struct cpumask *mask)
448 {
449 struct imsic_vector *vec = NULL;
450 struct imsic_local_priv *lpriv;
451 unsigned long flags;
452 unsigned int cpu;
453 int local_id;
454
455 raw_spin_lock_irqsave(&imsic->matrix_lock, flags);
456 local_id = irq_matrix_alloc(imsic->matrix, mask, false, &cpu);
457 raw_spin_unlock_irqrestore(&imsic->matrix_lock, flags);
458 if (local_id < 0)
459 return NULL;
460
461 lpriv = per_cpu_ptr(imsic->lpriv, cpu);
462 vec = &lpriv->vectors[local_id];
463 vec->irq = irq;
464 vec->enable = false;
465 vec->move_next = NULL;
466 vec->move_prev = NULL;
467
468 return vec;
469 }
470
imsic_vector_free(struct imsic_vector * vec)471 void imsic_vector_free(struct imsic_vector *vec)
472 {
473 unsigned long flags;
474
475 raw_spin_lock_irqsave(&imsic->matrix_lock, flags);
476 vec->irq = 0;
477 irq_matrix_free(imsic->matrix, vec->cpu, vec->local_id, false);
478 raw_spin_unlock_irqrestore(&imsic->matrix_lock, flags);
479 }
480
imsic_local_cleanup(void)481 static void __init imsic_local_cleanup(void)
482 {
483 struct imsic_local_priv *lpriv;
484 int cpu;
485
486 for_each_possible_cpu(cpu) {
487 lpriv = per_cpu_ptr(imsic->lpriv, cpu);
488
489 bitmap_free(lpriv->dirty_bitmap);
490 kfree(lpriv->vectors);
491 }
492
493 free_percpu(imsic->lpriv);
494 }
495
imsic_local_init(void)496 static int __init imsic_local_init(void)
497 {
498 struct imsic_global_config *global = &imsic->global;
499 struct imsic_local_priv *lpriv;
500 struct imsic_vector *vec;
501 int cpu, i;
502
503 /* Allocate per-CPU private state */
504 imsic->lpriv = alloc_percpu(typeof(*imsic->lpriv));
505 if (!imsic->lpriv)
506 return -ENOMEM;
507
508 /* Setup per-CPU private state */
509 for_each_possible_cpu(cpu) {
510 lpriv = per_cpu_ptr(imsic->lpriv, cpu);
511
512 raw_spin_lock_init(&lpriv->lock);
513
514 /* Allocate dirty bitmap */
515 lpriv->dirty_bitmap = bitmap_zalloc(global->nr_ids + 1, GFP_KERNEL);
516 if (!lpriv->dirty_bitmap)
517 goto fail_local_cleanup;
518
519 #ifdef CONFIG_SMP
520 /* Setup lazy timer for synchronization */
521 timer_setup(&lpriv->timer, imsic_local_timer_callback, TIMER_PINNED);
522 #endif
523
524 /* Allocate vector array */
525 lpriv->vectors = kcalloc(global->nr_ids + 1, sizeof(*lpriv->vectors),
526 GFP_KERNEL);
527 if (!lpriv->vectors)
528 goto fail_local_cleanup;
529
530 /* Setup vector array */
531 for (i = 0; i <= global->nr_ids; i++) {
532 vec = &lpriv->vectors[i];
533 vec->cpu = cpu;
534 vec->local_id = i;
535 vec->irq = 0;
536 }
537 }
538
539 return 0;
540
541 fail_local_cleanup:
542 imsic_local_cleanup();
543 return -ENOMEM;
544 }
545
imsic_state_online(void)546 void imsic_state_online(void)
547 {
548 unsigned long flags;
549
550 raw_spin_lock_irqsave(&imsic->matrix_lock, flags);
551 irq_matrix_online(imsic->matrix);
552 raw_spin_unlock_irqrestore(&imsic->matrix_lock, flags);
553 }
554
imsic_state_offline(void)555 void imsic_state_offline(void)
556 {
557 unsigned long flags;
558
559 raw_spin_lock_irqsave(&imsic->matrix_lock, flags);
560 irq_matrix_offline(imsic->matrix);
561 raw_spin_unlock_irqrestore(&imsic->matrix_lock, flags);
562
563 #ifdef CONFIG_SMP
564 struct imsic_local_priv *lpriv = this_cpu_ptr(imsic->lpriv);
565
566 raw_spin_lock_irqsave(&lpriv->lock, flags);
567 WARN_ON_ONCE(timer_delete_sync_try(&lpriv->timer) < 0);
568 raw_spin_unlock_irqrestore(&lpriv->lock, flags);
569 #endif
570 }
571
imsic_matrix_init(void)572 static int __init imsic_matrix_init(void)
573 {
574 struct imsic_global_config *global = &imsic->global;
575
576 raw_spin_lock_init(&imsic->matrix_lock);
577 imsic->matrix = irq_alloc_matrix(global->nr_ids + 1,
578 0, global->nr_ids + 1);
579 if (!imsic->matrix)
580 return -ENOMEM;
581
582 /* Reserve ID#0 because it is special and never implemented */
583 irq_matrix_assign_system(imsic->matrix, 0, false);
584
585 /* Reserve IPI ID because it is special and used internally */
586 irq_matrix_assign_system(imsic->matrix, IMSIC_IPI_ID, false);
587
588 return 0;
589 }
590
imsic_populate_global_dt(struct fwnode_handle * fwnode,struct imsic_global_config * global,u32 * nr_parent_irqs)591 static int __init imsic_populate_global_dt(struct fwnode_handle *fwnode,
592 struct imsic_global_config *global,
593 u32 *nr_parent_irqs)
594 {
595 int rc;
596
597 /* Find number of guest index bits in MSI address */
598 rc = of_property_read_u32(to_of_node(fwnode), "riscv,guest-index-bits",
599 &global->guest_index_bits);
600 if (rc)
601 global->guest_index_bits = 0;
602
603 /* Find number of HART index bits */
604 rc = of_property_read_u32(to_of_node(fwnode), "riscv,hart-index-bits",
605 &global->hart_index_bits);
606 if (rc) {
607 /* Assume default value */
608 global->hart_index_bits = __fls(*nr_parent_irqs);
609 if (BIT(global->hart_index_bits) < *nr_parent_irqs)
610 global->hart_index_bits++;
611 }
612
613 /* Find number of group index bits */
614 rc = of_property_read_u32(to_of_node(fwnode), "riscv,group-index-bits",
615 &global->group_index_bits);
616 if (rc)
617 global->group_index_bits = 0;
618
619 /*
620 * Find first bit position of group index.
621 * If not specified assumed the default APLIC-IMSIC configuration.
622 */
623 rc = of_property_read_u32(to_of_node(fwnode), "riscv,group-index-shift",
624 &global->group_index_shift);
625 if (rc)
626 global->group_index_shift = IMSIC_MMIO_PAGE_SHIFT * 2;
627
628 /* Find number of interrupt identities */
629 rc = of_property_read_u32(to_of_node(fwnode), "riscv,num-ids",
630 &global->nr_ids);
631 if (rc) {
632 pr_err("%pfwP: number of interrupt identities not found\n", fwnode);
633 return rc;
634 }
635
636 /* Find number of guest interrupt identities */
637 rc = of_property_read_u32(to_of_node(fwnode), "riscv,num-guest-ids",
638 &global->nr_guest_ids);
639 if (rc)
640 global->nr_guest_ids = global->nr_ids;
641
642 return 0;
643 }
644
imsic_populate_global_acpi(struct fwnode_handle * fwnode,struct imsic_global_config * global,u32 * nr_parent_irqs,void * opaque)645 static int __init imsic_populate_global_acpi(struct fwnode_handle *fwnode,
646 struct imsic_global_config *global,
647 u32 *nr_parent_irqs, void *opaque)
648 {
649 struct acpi_madt_imsic *imsic = (struct acpi_madt_imsic *)opaque;
650
651 global->guest_index_bits = imsic->guest_index_bits;
652 global->hart_index_bits = imsic->hart_index_bits;
653 global->group_index_bits = imsic->group_index_bits;
654 global->group_index_shift = imsic->group_index_shift;
655 global->nr_ids = imsic->num_ids;
656 global->nr_guest_ids = imsic->num_guest_ids;
657 return 0;
658 }
659
imsic_get_parent_hartid(struct fwnode_handle * fwnode,u32 index,unsigned long * hartid)660 static int __init imsic_get_parent_hartid(struct fwnode_handle *fwnode,
661 u32 index, unsigned long *hartid)
662 {
663 struct of_phandle_args parent;
664 int rc;
665
666 if (!is_of_node(fwnode)) {
667 if (hartid)
668 *hartid = acpi_rintc_index_to_hartid(index);
669
670 if (!hartid || (*hartid == INVALID_HARTID))
671 return -EINVAL;
672
673 return 0;
674 }
675
676 rc = of_irq_parse_one(to_of_node(fwnode), index, &parent);
677 if (rc)
678 return rc;
679
680 /*
681 * Skip interrupts other than external interrupts for
682 * current privilege level.
683 */
684 if (parent.args[0] != RV_IRQ_EXT)
685 return -EINVAL;
686
687 return riscv_of_parent_hartid(parent.np, hartid);
688 }
689
imsic_get_mmio_resource(struct fwnode_handle * fwnode,u32 index,struct resource * res)690 static int __init imsic_get_mmio_resource(struct fwnode_handle *fwnode,
691 u32 index, struct resource *res)
692 {
693 if (!is_of_node(fwnode))
694 return acpi_rintc_get_imsic_mmio_info(index, res);
695
696 return of_address_to_resource(to_of_node(fwnode), index, res);
697 }
698
imsic_parse_fwnode(struct fwnode_handle * fwnode,struct imsic_global_config * global,u32 * nr_parent_irqs,u32 * nr_mmios,void * opaque)699 static int __init imsic_parse_fwnode(struct fwnode_handle *fwnode,
700 struct imsic_global_config *global,
701 u32 *nr_parent_irqs,
702 u32 *nr_mmios,
703 void *opaque)
704 {
705 unsigned long hartid;
706 struct resource res;
707 int rc;
708 u32 i;
709
710 *nr_parent_irqs = 0;
711 *nr_mmios = 0;
712
713 /* Find number of parent interrupts */
714 while (!imsic_get_parent_hartid(fwnode, *nr_parent_irqs, &hartid))
715 (*nr_parent_irqs)++;
716 if (!*nr_parent_irqs) {
717 pr_err("%pfwP: no parent irqs available\n", fwnode);
718 return -EINVAL;
719 }
720
721 if (is_of_node(fwnode))
722 rc = imsic_populate_global_dt(fwnode, global, nr_parent_irqs);
723 else
724 rc = imsic_populate_global_acpi(fwnode, global, nr_parent_irqs, opaque);
725
726 if (rc)
727 return rc;
728
729 /* Sanity check guest index bits */
730 i = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT;
731 if (i < global->guest_index_bits) {
732 pr_err("%pfwP: guest index bits too big\n", fwnode);
733 return -EINVAL;
734 }
735
736 /* Sanity check HART index bits */
737 i = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT - global->guest_index_bits;
738 if (i < global->hart_index_bits) {
739 pr_err("%pfwP: HART index bits too big\n", fwnode);
740 return -EINVAL;
741 }
742
743 /* Sanity check group index bits */
744 i = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT -
745 global->guest_index_bits - global->hart_index_bits;
746 if (i < global->group_index_bits) {
747 pr_err("%pfwP: group index bits too big\n", fwnode);
748 return -EINVAL;
749 }
750
751 /* Sanity check group index shift */
752 i = global->group_index_bits + global->group_index_shift - 1;
753 if (i >= BITS_PER_LONG) {
754 pr_err("%pfwP: group index shift too big\n", fwnode);
755 return -EINVAL;
756 }
757
758 /* Sanity check number of interrupt identities */
759 if (global->nr_ids < IMSIC_MIN_ID ||
760 global->nr_ids >= IMSIC_MAX_ID ||
761 (global->nr_ids & IMSIC_MIN_ID) != IMSIC_MIN_ID) {
762 pr_err("%pfwP: invalid number of interrupt identities\n", fwnode);
763 return -EINVAL;
764 }
765
766 /* Sanity check number of guest interrupt identities */
767 if (global->nr_guest_ids < IMSIC_MIN_ID ||
768 global->nr_guest_ids >= IMSIC_MAX_ID ||
769 (global->nr_guest_ids & IMSIC_MIN_ID) != IMSIC_MIN_ID) {
770 pr_err("%pfwP: invalid number of guest interrupt identities\n", fwnode);
771 return -EINVAL;
772 }
773
774 /* Compute base address */
775 rc = imsic_get_mmio_resource(fwnode, 0, &res);
776 if (rc) {
777 pr_err("%pfwP: first MMIO resource not found\n", fwnode);
778 return -EINVAL;
779 }
780 global->base_addr = res.start;
781 global->base_addr &= ~(BIT(global->guest_index_bits +
782 global->hart_index_bits +
783 IMSIC_MMIO_PAGE_SHIFT) - 1);
784 global->base_addr &= ~((BIT(global->group_index_bits) - 1) <<
785 global->group_index_shift);
786
787 /* Find number of MMIO register sets */
788 while (!imsic_get_mmio_resource(fwnode, *nr_mmios, &res))
789 (*nr_mmios)++;
790
791 return 0;
792 }
793
imsic_setup_state(struct fwnode_handle * fwnode,void * opaque)794 int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
795 {
796 u32 i, j, index, nr_parent_irqs, nr_mmios, nr_handlers = 0;
797 struct imsic_global_config *global;
798 struct imsic_local_config *local;
799 void __iomem **mmios_va = NULL;
800 struct resource *mmios = NULL;
801 unsigned long reloff, hartid;
802 phys_addr_t base_addr;
803 int rc, cpu;
804
805 /*
806 * Only one IMSIC instance allowed in a platform for clean
807 * implementation of SMP IRQ affinity and per-CPU IPIs.
808 *
809 * This means on a multi-socket (or multi-die) platform we
810 * will have multiple MMIO regions for one IMSIC instance.
811 */
812 if (imsic) {
813 pr_err("%pfwP: already initialized hence ignoring\n", fwnode);
814 return -EALREADY;
815 }
816
817 if (!riscv_isa_extension_available(NULL, SxAIA)) {
818 pr_err("%pfwP: AIA support not available\n", fwnode);
819 return -ENODEV;
820 }
821
822 imsic = kzalloc(sizeof(*imsic), GFP_KERNEL);
823 if (!imsic)
824 return -ENOMEM;
825 imsic->fwnode = fwnode;
826 global = &imsic->global;
827
828 global->local = alloc_percpu(typeof(*global->local));
829 if (!global->local) {
830 rc = -ENOMEM;
831 goto out_free_priv;
832 }
833
834 /* Parse IMSIC fwnode */
835 rc = imsic_parse_fwnode(fwnode, global, &nr_parent_irqs, &nr_mmios, opaque);
836 if (rc)
837 goto out_free_local;
838
839 /* Allocate MMIO resource array */
840 mmios = kcalloc(nr_mmios, sizeof(*mmios), GFP_KERNEL);
841 if (!mmios) {
842 rc = -ENOMEM;
843 goto out_free_local;
844 }
845
846 /* Allocate MMIO virtual address array */
847 mmios_va = kcalloc(nr_mmios, sizeof(*mmios_va), GFP_KERNEL);
848 if (!mmios_va) {
849 rc = -ENOMEM;
850 goto out_iounmap;
851 }
852
853 /* Parse and map MMIO register sets */
854 for (i = 0; i < nr_mmios; i++) {
855 rc = imsic_get_mmio_resource(fwnode, i, &mmios[i]);
856 if (rc) {
857 pr_err("%pfwP: unable to parse MMIO regset %d\n", fwnode, i);
858 goto out_iounmap;
859 }
860
861 base_addr = mmios[i].start;
862 base_addr &= ~(BIT(global->guest_index_bits +
863 global->hart_index_bits +
864 IMSIC_MMIO_PAGE_SHIFT) - 1);
865 base_addr &= ~((BIT(global->group_index_bits) - 1) <<
866 global->group_index_shift);
867 if (base_addr != global->base_addr) {
868 rc = -EINVAL;
869 pr_err("%pfwP: address mismatch for regset %d\n", fwnode, i);
870 goto out_iounmap;
871 }
872
873 mmios_va[i] = ioremap(mmios[i].start, resource_size(&mmios[i]));
874 if (!mmios_va[i]) {
875 rc = -EIO;
876 pr_err("%pfwP: unable to map MMIO regset %d\n", fwnode, i);
877 goto out_iounmap;
878 }
879 }
880
881 /* Initialize local (or per-CPU )state */
882 rc = imsic_local_init();
883 if (rc) {
884 pr_err("%pfwP: failed to initialize local state\n",
885 fwnode);
886 goto out_iounmap;
887 }
888
889 /* Configure handlers for target CPUs */
890 for (i = 0; i < nr_parent_irqs; i++) {
891 rc = imsic_get_parent_hartid(fwnode, i, &hartid);
892 if (rc) {
893 pr_warn("%pfwP: hart ID for parent irq%d not found\n", fwnode, i);
894 continue;
895 }
896
897 cpu = riscv_hartid_to_cpuid(hartid);
898 if (cpu < 0) {
899 pr_warn("%pfwP: invalid cpuid for parent irq%d\n", fwnode, i);
900 continue;
901 }
902
903 /* Find MMIO location of MSI page */
904 index = nr_mmios;
905 reloff = i * BIT(global->guest_index_bits) *
906 IMSIC_MMIO_PAGE_SZ;
907 for (j = 0; nr_mmios; j++) {
908 if (reloff < resource_size(&mmios[j])) {
909 index = j;
910 break;
911 }
912
913 /*
914 * MMIO region size may not be aligned to
915 * BIT(global->guest_index_bits) * IMSIC_MMIO_PAGE_SZ
916 * if holes are present.
917 */
918 reloff -= ALIGN(resource_size(&mmios[j]),
919 BIT(global->guest_index_bits) * IMSIC_MMIO_PAGE_SZ);
920 }
921 if (index >= nr_mmios) {
922 pr_warn("%pfwP: MMIO not found for parent irq%d\n", fwnode, i);
923 continue;
924 }
925
926 local = per_cpu_ptr(global->local, cpu);
927 local->msi_pa = mmios[index].start + reloff;
928 local->msi_va = mmios_va[index] + reloff;
929
930 nr_handlers++;
931 }
932
933 /* If no CPU handlers found then can't take interrupts */
934 if (!nr_handlers) {
935 pr_err("%pfwP: No CPU handlers found\n", fwnode);
936 rc = -ENODEV;
937 goto out_local_cleanup;
938 }
939
940 /* Initialize matrix allocator */
941 rc = imsic_matrix_init();
942 if (rc) {
943 pr_err("%pfwP: failed to create matrix allocator\n", fwnode);
944 goto out_local_cleanup;
945 }
946
947 /* We don't need MMIO arrays anymore so let's free-up */
948 kfree(mmios_va);
949 kfree(mmios);
950
951 return 0;
952
953 out_local_cleanup:
954 imsic_local_cleanup();
955 out_iounmap:
956 for (i = 0; i < nr_mmios; i++) {
957 if (mmios_va[i])
958 iounmap(mmios_va[i]);
959 }
960 kfree(mmios_va);
961 kfree(mmios);
962 out_free_local:
963 free_percpu(imsic->global.local);
964 out_free_priv:
965 kfree(imsic);
966 imsic = NULL;
967 return rc;
968 }
969