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 || (!imsic_noipi && 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 (!imsic_noipi && 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 if (!imsic_noipi)
587 irq_matrix_assign_system(imsic->matrix, IMSIC_IPI_ID, false);
588
589 return 0;
590 }
591
imsic_populate_global_dt(struct fwnode_handle * fwnode,struct imsic_global_config * global,u32 * nr_parent_irqs)592 static int __init imsic_populate_global_dt(struct fwnode_handle *fwnode,
593 struct imsic_global_config *global,
594 u32 *nr_parent_irqs)
595 {
596 int rc;
597
598 /* Find number of guest index bits in MSI address */
599 rc = of_property_read_u32(to_of_node(fwnode), "riscv,guest-index-bits",
600 &global->guest_index_bits);
601 if (rc)
602 global->guest_index_bits = 0;
603
604 /* Find number of HART index bits */
605 rc = of_property_read_u32(to_of_node(fwnode), "riscv,hart-index-bits",
606 &global->hart_index_bits);
607 if (rc) {
608 /* Assume default value */
609 global->hart_index_bits = __fls(*nr_parent_irqs);
610 if (BIT(global->hart_index_bits) < *nr_parent_irqs)
611 global->hart_index_bits++;
612 }
613
614 /* Find number of group index bits */
615 rc = of_property_read_u32(to_of_node(fwnode), "riscv,group-index-bits",
616 &global->group_index_bits);
617 if (rc)
618 global->group_index_bits = 0;
619
620 /*
621 * Find first bit position of group index.
622 * If not specified assumed the default APLIC-IMSIC configuration.
623 */
624 rc = of_property_read_u32(to_of_node(fwnode), "riscv,group-index-shift",
625 &global->group_index_shift);
626 if (rc)
627 global->group_index_shift = IMSIC_MMIO_PAGE_SHIFT * 2;
628
629 /* Find number of interrupt identities */
630 rc = of_property_read_u32(to_of_node(fwnode), "riscv,num-ids",
631 &global->nr_ids);
632 if (rc) {
633 pr_err("%pfwP: number of interrupt identities not found\n", fwnode);
634 return rc;
635 }
636
637 /* Find number of guest interrupt identities */
638 rc = of_property_read_u32(to_of_node(fwnode), "riscv,num-guest-ids",
639 &global->nr_guest_ids);
640 if (rc)
641 global->nr_guest_ids = global->nr_ids;
642
643 return 0;
644 }
645
imsic_populate_global_acpi(struct fwnode_handle * fwnode,struct imsic_global_config * global,u32 * nr_parent_irqs,void * opaque)646 static int __init imsic_populate_global_acpi(struct fwnode_handle *fwnode,
647 struct imsic_global_config *global,
648 u32 *nr_parent_irqs, void *opaque)
649 {
650 struct acpi_madt_imsic *imsic = (struct acpi_madt_imsic *)opaque;
651
652 global->guest_index_bits = imsic->guest_index_bits;
653 global->hart_index_bits = imsic->hart_index_bits;
654 global->group_index_bits = imsic->group_index_bits;
655 global->group_index_shift = imsic->group_index_shift;
656 global->nr_ids = imsic->num_ids;
657 global->nr_guest_ids = imsic->num_guest_ids;
658 return 0;
659 }
660
imsic_get_parent_hartid(struct fwnode_handle * fwnode,u32 index,unsigned long * hartid)661 static int __init imsic_get_parent_hartid(struct fwnode_handle *fwnode,
662 u32 index, unsigned long *hartid)
663 {
664 struct of_phandle_args parent;
665 int rc;
666
667 if (!is_of_node(fwnode)) {
668 if (hartid)
669 *hartid = acpi_rintc_index_to_hartid(index);
670
671 if (!hartid || (*hartid == INVALID_HARTID))
672 return -EINVAL;
673
674 return 0;
675 }
676
677 rc = of_irq_parse_one(to_of_node(fwnode), index, &parent);
678 if (rc)
679 return rc;
680
681 /*
682 * Skip interrupts other than external interrupts for
683 * current privilege level.
684 */
685 if (parent.args[0] != RV_IRQ_EXT)
686 return -EINVAL;
687
688 return riscv_of_parent_hartid(parent.np, hartid);
689 }
690
imsic_get_mmio_resource(struct fwnode_handle * fwnode,u32 index,struct resource * res)691 static int __init imsic_get_mmio_resource(struct fwnode_handle *fwnode,
692 u32 index, struct resource *res)
693 {
694 if (!is_of_node(fwnode))
695 return acpi_rintc_get_imsic_mmio_info(index, res);
696
697 return of_address_to_resource(to_of_node(fwnode), index, res);
698 }
699
imsic_parse_fwnode(struct fwnode_handle * fwnode,struct imsic_global_config * global,u32 * nr_parent_irqs,u32 * nr_mmios,void * opaque)700 static int __init imsic_parse_fwnode(struct fwnode_handle *fwnode,
701 struct imsic_global_config *global,
702 u32 *nr_parent_irqs,
703 u32 *nr_mmios,
704 void *opaque)
705 {
706 unsigned long hartid;
707 struct resource res;
708 int rc;
709 u32 i;
710
711 *nr_parent_irqs = 0;
712 *nr_mmios = 0;
713
714 /* Find number of parent interrupts */
715 while (!imsic_get_parent_hartid(fwnode, *nr_parent_irqs, &hartid))
716 (*nr_parent_irqs)++;
717 if (!*nr_parent_irqs) {
718 pr_err("%pfwP: no parent irqs available\n", fwnode);
719 return -EINVAL;
720 }
721
722 if (is_of_node(fwnode))
723 rc = imsic_populate_global_dt(fwnode, global, nr_parent_irqs);
724 else
725 rc = imsic_populate_global_acpi(fwnode, global, nr_parent_irqs, opaque);
726
727 if (rc)
728 return rc;
729
730 /* Sanity check guest index bits */
731 i = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT;
732 if (i < global->guest_index_bits) {
733 pr_err("%pfwP: guest index bits too big\n", fwnode);
734 return -EINVAL;
735 }
736
737 /* Sanity check HART index bits */
738 i = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT - global->guest_index_bits;
739 if (i < global->hart_index_bits) {
740 pr_err("%pfwP: HART index bits too big\n", fwnode);
741 return -EINVAL;
742 }
743
744 /* Sanity check group index bits */
745 i = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT -
746 global->guest_index_bits - global->hart_index_bits;
747 if (i < global->group_index_bits) {
748 pr_err("%pfwP: group index bits too big\n", fwnode);
749 return -EINVAL;
750 }
751
752 /* Sanity check group index shift */
753 i = global->group_index_bits + global->group_index_shift - 1;
754 if (i >= BITS_PER_LONG) {
755 pr_err("%pfwP: group index shift too big\n", fwnode);
756 return -EINVAL;
757 }
758
759 /* Sanity check number of interrupt identities */
760 if (global->nr_ids < IMSIC_MIN_ID ||
761 global->nr_ids >= IMSIC_MAX_ID ||
762 (global->nr_ids & IMSIC_MIN_ID) != IMSIC_MIN_ID) {
763 pr_err("%pfwP: invalid number of interrupt identities\n", fwnode);
764 return -EINVAL;
765 }
766
767 /* Sanity check number of guest interrupt identities */
768 if (global->nr_guest_ids < IMSIC_MIN_ID ||
769 global->nr_guest_ids >= IMSIC_MAX_ID ||
770 (global->nr_guest_ids & IMSIC_MIN_ID) != IMSIC_MIN_ID) {
771 pr_err("%pfwP: invalid number of guest interrupt identities\n", fwnode);
772 return -EINVAL;
773 }
774
775 /* Compute base address */
776 rc = imsic_get_mmio_resource(fwnode, 0, &res);
777 if (rc) {
778 pr_err("%pfwP: first MMIO resource not found\n", fwnode);
779 return -EINVAL;
780 }
781 global->base_addr = res.start;
782 global->base_addr &= ~(BIT(global->guest_index_bits +
783 global->hart_index_bits +
784 IMSIC_MMIO_PAGE_SHIFT) - 1);
785 global->base_addr &= ~((BIT(global->group_index_bits) - 1) <<
786 global->group_index_shift);
787
788 /* Find number of MMIO register sets */
789 while (!imsic_get_mmio_resource(fwnode, *nr_mmios, &res))
790 (*nr_mmios)++;
791
792 return 0;
793 }
794
imsic_setup_state(struct fwnode_handle * fwnode,void * opaque)795 int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
796 {
797 u32 i, j, index, nr_parent_irqs, nr_mmios, nr_handlers = 0;
798 struct imsic_global_config *global;
799 struct imsic_local_config *local;
800 void __iomem **mmios_va = NULL;
801 struct resource *mmios = NULL;
802 unsigned long reloff, hartid;
803 phys_addr_t base_addr;
804 int rc, cpu;
805
806 /*
807 * Only one IMSIC instance allowed in a platform for clean
808 * implementation of SMP IRQ affinity and per-CPU IPIs.
809 *
810 * This means on a multi-socket (or multi-die) platform we
811 * will have multiple MMIO regions for one IMSIC instance.
812 */
813 if (imsic) {
814 pr_err("%pfwP: already initialized hence ignoring\n", fwnode);
815 return -EALREADY;
816 }
817
818 if (!riscv_isa_extension_available(NULL, SxAIA)) {
819 pr_err("%pfwP: AIA support not available\n", fwnode);
820 return -ENODEV;
821 }
822
823 imsic = kzalloc(sizeof(*imsic), GFP_KERNEL);
824 if (!imsic)
825 return -ENOMEM;
826 imsic->fwnode = fwnode;
827 global = &imsic->global;
828
829 global->local = alloc_percpu(typeof(*global->local));
830 if (!global->local) {
831 rc = -ENOMEM;
832 goto out_free_priv;
833 }
834
835 /* Parse IMSIC fwnode */
836 rc = imsic_parse_fwnode(fwnode, global, &nr_parent_irqs, &nr_mmios, opaque);
837 if (rc)
838 goto out_free_local;
839
840 /* Allocate MMIO resource array */
841 mmios = kcalloc(nr_mmios, sizeof(*mmios), GFP_KERNEL);
842 if (!mmios) {
843 rc = -ENOMEM;
844 goto out_free_local;
845 }
846
847 /* Allocate MMIO virtual address array */
848 mmios_va = kcalloc(nr_mmios, sizeof(*mmios_va), GFP_KERNEL);
849 if (!mmios_va) {
850 rc = -ENOMEM;
851 goto out_iounmap;
852 }
853
854 /* Parse and map MMIO register sets */
855 for (i = 0; i < nr_mmios; i++) {
856 rc = imsic_get_mmio_resource(fwnode, i, &mmios[i]);
857 if (rc) {
858 pr_err("%pfwP: unable to parse MMIO regset %d\n", fwnode, i);
859 goto out_iounmap;
860 }
861
862 base_addr = mmios[i].start;
863 base_addr &= ~(BIT(global->guest_index_bits +
864 global->hart_index_bits +
865 IMSIC_MMIO_PAGE_SHIFT) - 1);
866 base_addr &= ~((BIT(global->group_index_bits) - 1) <<
867 global->group_index_shift);
868 if (base_addr != global->base_addr) {
869 rc = -EINVAL;
870 pr_err("%pfwP: address mismatch for regset %d\n", fwnode, i);
871 goto out_iounmap;
872 }
873
874 mmios_va[i] = ioremap(mmios[i].start, resource_size(&mmios[i]));
875 if (!mmios_va[i]) {
876 rc = -EIO;
877 pr_err("%pfwP: unable to map MMIO regset %d\n", fwnode, i);
878 goto out_iounmap;
879 }
880 }
881
882 /* Initialize local (or per-CPU )state */
883 rc = imsic_local_init();
884 if (rc) {
885 pr_err("%pfwP: failed to initialize local state\n",
886 fwnode);
887 goto out_iounmap;
888 }
889
890 /* Configure handlers for target CPUs */
891 for (i = 0; i < nr_parent_irqs; i++) {
892 rc = imsic_get_parent_hartid(fwnode, i, &hartid);
893 if (rc) {
894 pr_warn("%pfwP: hart ID for parent irq%d not found\n", fwnode, i);
895 continue;
896 }
897
898 cpu = riscv_hartid_to_cpuid(hartid);
899 if (cpu < 0) {
900 pr_warn("%pfwP: invalid cpuid for parent irq%d\n", fwnode, i);
901 continue;
902 }
903
904 /* Find MMIO location of MSI page */
905 index = nr_mmios;
906 reloff = i * BIT(global->guest_index_bits) *
907 IMSIC_MMIO_PAGE_SZ;
908 for (j = 0; nr_mmios; j++) {
909 if (reloff < resource_size(&mmios[j])) {
910 index = j;
911 break;
912 }
913
914 /*
915 * MMIO region size may not be aligned to
916 * BIT(global->guest_index_bits) * IMSIC_MMIO_PAGE_SZ
917 * if holes are present.
918 */
919 reloff -= ALIGN(resource_size(&mmios[j]),
920 BIT(global->guest_index_bits) * IMSIC_MMIO_PAGE_SZ);
921 }
922 if (index >= nr_mmios) {
923 pr_warn("%pfwP: MMIO not found for parent irq%d\n", fwnode, i);
924 continue;
925 }
926
927 local = per_cpu_ptr(global->local, cpu);
928 local->msi_pa = mmios[index].start + reloff;
929 local->msi_va = mmios_va[index] + reloff;
930
931 nr_handlers++;
932 }
933
934 /* If no CPU handlers found then can't take interrupts */
935 if (!nr_handlers) {
936 pr_err("%pfwP: No CPU handlers found\n", fwnode);
937 rc = -ENODEV;
938 goto out_local_cleanup;
939 }
940
941 /* Initialize matrix allocator */
942 rc = imsic_matrix_init();
943 if (rc) {
944 pr_err("%pfwP: failed to create matrix allocator\n", fwnode);
945 goto out_local_cleanup;
946 }
947
948 /* We don't need MMIO arrays anymore so let's free-up */
949 kfree(mmios_va);
950 kfree(mmios);
951
952 return 0;
953
954 out_local_cleanup:
955 imsic_local_cleanup();
956 out_iounmap:
957 for (i = 0; i < nr_mmios; i++) {
958 if (mmios_va[i])
959 iounmap(mmios_va[i]);
960 }
961 kfree(mmios_va);
962 kfree(mmios);
963 out_free_local:
964 free_percpu(imsic->global.local);
965 out_free_priv:
966 kfree(imsic);
967 imsic = NULL;
968 return rc;
969 }
970