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 *mlocal;
130 	struct imsic_vector *vec, *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 			if (__imsic_id_read_clear_pending(i)) {
173 				mlocal = per_cpu_ptr(imsic->global.local, mvec->cpu);
174 				writel_relaxed(mvec->local_id, mlocal->msi_va);
175 			}
176 
177 			WRITE_ONCE(vec->move_next, NULL);
178 			imsic_vector_free(&lpriv->vectors[i]);
179 		}
180 
181 skip:
182 		bitmap_clear(lpriv->dirty_bitmap, i, 1);
183 	}
184 
185 	return ret;
186 }
187 
188 #ifdef CONFIG_SMP
__imsic_local_timer_start(struct imsic_local_priv * lpriv,unsigned int cpu)189 static void __imsic_local_timer_start(struct imsic_local_priv *lpriv, unsigned int cpu)
190 {
191 	lockdep_assert_held(&lpriv->lock);
192 
193 	if (!timer_pending(&lpriv->timer)) {
194 		lpriv->timer.expires = jiffies + 1;
195 		add_timer_on(&lpriv->timer, cpu);
196 	}
197 }
198 #else
__imsic_local_timer_start(struct imsic_local_priv * lpriv,unsigned int cpu)199 static inline void __imsic_local_timer_start(struct imsic_local_priv *lpriv, unsigned int cpu)
200 {
201 }
202 #endif
203 
imsic_local_sync_all(bool force_all)204 void imsic_local_sync_all(bool force_all)
205 {
206 	struct imsic_local_priv *lpriv = this_cpu_ptr(imsic->lpriv);
207 	unsigned long flags;
208 
209 	raw_spin_lock_irqsave(&lpriv->lock, flags);
210 
211 	if (force_all)
212 		bitmap_fill(lpriv->dirty_bitmap, imsic->global.nr_ids + 1);
213 	if (!__imsic_local_sync(lpriv))
214 		__imsic_local_timer_start(lpriv, smp_processor_id());
215 
216 	raw_spin_unlock_irqrestore(&lpriv->lock, flags);
217 }
218 
imsic_local_delivery(bool enable)219 void imsic_local_delivery(bool enable)
220 {
221 	if (enable) {
222 		imsic_csr_write(IMSIC_EITHRESHOLD, IMSIC_ENABLE_EITHRESHOLD);
223 		imsic_csr_write(IMSIC_EIDELIVERY, IMSIC_ENABLE_EIDELIVERY);
224 		return;
225 	}
226 
227 	imsic_csr_write(IMSIC_EIDELIVERY, IMSIC_DISABLE_EIDELIVERY);
228 	imsic_csr_write(IMSIC_EITHRESHOLD, IMSIC_DISABLE_EITHRESHOLD);
229 }
230 
231 #ifdef CONFIG_SMP
imsic_local_timer_callback(struct timer_list * timer)232 static void imsic_local_timer_callback(struct timer_list *timer)
233 {
234 	imsic_local_sync_all(false);
235 }
236 
__imsic_remote_sync(struct imsic_local_priv * lpriv,unsigned int cpu)237 static void __imsic_remote_sync(struct imsic_local_priv *lpriv, unsigned int cpu)
238 {
239 	lockdep_assert_held(&lpriv->lock);
240 
241 	/*
242 	 * The spinlock acquire/release semantics ensure that changes
243 	 * to vector enable, vector move and dirty bitmap are visible
244 	 * to the target CPU.
245 	 */
246 
247 	/*
248 	 * We schedule a timer on the target CPU if the target CPU is not
249 	 * same as the current CPU. An offline CPU will unconditionally
250 	 * synchronize IDs through imsic_starting_cpu() when the
251 	 * CPU is brought up.
252 	 */
253 	if (cpu_online(cpu)) {
254 		if (cpu == smp_processor_id()) {
255 			if (__imsic_local_sync(lpriv))
256 				return;
257 		}
258 
259 		__imsic_local_timer_start(lpriv, cpu);
260 	}
261 }
262 #else
__imsic_remote_sync(struct imsic_local_priv * lpriv,unsigned int cpu)263 static void __imsic_remote_sync(struct imsic_local_priv *lpriv, unsigned int cpu)
264 {
265 	lockdep_assert_held(&lpriv->lock);
266 	__imsic_local_sync(lpriv);
267 }
268 #endif
269 
imsic_vector_mask(struct imsic_vector * vec)270 void imsic_vector_mask(struct imsic_vector *vec)
271 {
272 	struct imsic_local_priv *lpriv;
273 
274 	lpriv = per_cpu_ptr(imsic->lpriv, vec->cpu);
275 	if (WARN_ON_ONCE(&lpriv->vectors[vec->local_id] != vec))
276 		return;
277 
278 	/*
279 	 * This function is called through Linux irq subsystem with
280 	 * irqs disabled so no need to save/restore irq flags.
281 	 */
282 
283 	raw_spin_lock(&lpriv->lock);
284 
285 	WRITE_ONCE(vec->enable, false);
286 	bitmap_set(lpriv->dirty_bitmap, vec->local_id, 1);
287 	__imsic_remote_sync(lpriv, vec->cpu);
288 
289 	raw_spin_unlock(&lpriv->lock);
290 }
291 
imsic_vector_unmask(struct imsic_vector * vec)292 void imsic_vector_unmask(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, true);
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_move_update(struct imsic_local_priv * lpriv,struct imsic_vector * vec,bool is_old_vec,bool new_enable,struct imsic_vector * move_vec)314 static bool imsic_vector_move_update(struct imsic_local_priv *lpriv,
315 				     struct imsic_vector *vec, bool is_old_vec,
316 				     bool new_enable, struct imsic_vector *move_vec)
317 {
318 	unsigned long flags;
319 	bool enabled;
320 
321 	raw_spin_lock_irqsave(&lpriv->lock, flags);
322 
323 	/* Update enable and move details */
324 	enabled = READ_ONCE(vec->enable);
325 	WRITE_ONCE(vec->enable, new_enable);
326 	if (is_old_vec)
327 		WRITE_ONCE(vec->move_next, move_vec);
328 	else
329 		WRITE_ONCE(vec->move_prev, move_vec);
330 
331 	/* Mark the vector as dirty and synchronize */
332 	bitmap_set(lpriv->dirty_bitmap, vec->local_id, 1);
333 	__imsic_remote_sync(lpriv, vec->cpu);
334 
335 	raw_spin_unlock_irqrestore(&lpriv->lock, flags);
336 
337 	return enabled;
338 }
339 
imsic_vector_move(struct imsic_vector * old_vec,struct imsic_vector * new_vec)340 void imsic_vector_move(struct imsic_vector *old_vec, struct imsic_vector *new_vec)
341 {
342 	struct imsic_local_priv *old_lpriv, *new_lpriv;
343 	bool enabled;
344 
345 	if (WARN_ON_ONCE(old_vec->cpu == new_vec->cpu))
346 		return;
347 
348 	old_lpriv = per_cpu_ptr(imsic->lpriv, old_vec->cpu);
349 	if (WARN_ON_ONCE(&old_lpriv->vectors[old_vec->local_id] != old_vec))
350 		return;
351 
352 	new_lpriv = per_cpu_ptr(imsic->lpriv, new_vec->cpu);
353 	if (WARN_ON_ONCE(&new_lpriv->vectors[new_vec->local_id] != new_vec))
354 		return;
355 
356 	/*
357 	 * Move and re-trigger the new vector based on the pending
358 	 * state of the old vector because we might get a device
359 	 * interrupt on the old vector while device was being moved
360 	 * to the new vector.
361 	 */
362 	enabled = imsic_vector_move_update(old_lpriv, old_vec, true, false, new_vec);
363 	imsic_vector_move_update(new_lpriv, new_vec, false, enabled, old_vec);
364 }
365 
366 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
imsic_vector_debug_show(struct seq_file * m,struct imsic_vector * vec,int ind)367 void imsic_vector_debug_show(struct seq_file *m, struct imsic_vector *vec, int ind)
368 {
369 	struct imsic_local_priv *lpriv;
370 	struct imsic_vector *mvec;
371 	bool is_enabled;
372 
373 	lpriv = per_cpu_ptr(imsic->lpriv, vec->cpu);
374 	if (WARN_ON_ONCE(&lpriv->vectors[vec->local_id] != vec))
375 		return;
376 
377 	is_enabled = imsic_vector_isenabled(vec);
378 	mvec = imsic_vector_get_move(vec);
379 
380 	seq_printf(m, "%*starget_cpu      : %5u\n", ind, "", vec->cpu);
381 	seq_printf(m, "%*starget_local_id : %5u\n", ind, "", vec->local_id);
382 	seq_printf(m, "%*sis_reserved     : %5u\n", ind, "",
383 		   (vec->local_id <= IMSIC_IPI_ID) ? 1 : 0);
384 	seq_printf(m, "%*sis_enabled      : %5u\n", ind, "", is_enabled ? 1 : 0);
385 	seq_printf(m, "%*sis_move_pending : %5u\n", ind, "", mvec ? 1 : 0);
386 	if (mvec) {
387 		seq_printf(m, "%*smove_cpu        : %5u\n", ind, "", mvec->cpu);
388 		seq_printf(m, "%*smove_local_id   : %5u\n", ind, "", mvec->local_id);
389 	}
390 }
391 
imsic_vector_debug_show_summary(struct seq_file * m,int ind)392 void imsic_vector_debug_show_summary(struct seq_file *m, int ind)
393 {
394 	irq_matrix_debug_show(m, imsic->matrix, ind);
395 }
396 #endif
397 
imsic_vector_from_local_id(unsigned int cpu,unsigned int local_id)398 struct imsic_vector *imsic_vector_from_local_id(unsigned int cpu, unsigned int local_id)
399 {
400 	struct imsic_local_priv *lpriv = per_cpu_ptr(imsic->lpriv, cpu);
401 
402 	if (!lpriv || imsic->global.nr_ids < local_id)
403 		return NULL;
404 
405 	return &lpriv->vectors[local_id];
406 }
407 
imsic_vector_alloc(unsigned int hwirq,const struct cpumask * mask)408 struct imsic_vector *imsic_vector_alloc(unsigned int hwirq, const struct cpumask *mask)
409 {
410 	struct imsic_vector *vec = NULL;
411 	struct imsic_local_priv *lpriv;
412 	unsigned long flags;
413 	unsigned int cpu;
414 	int local_id;
415 
416 	raw_spin_lock_irqsave(&imsic->matrix_lock, flags);
417 	local_id = irq_matrix_alloc(imsic->matrix, mask, false, &cpu);
418 	raw_spin_unlock_irqrestore(&imsic->matrix_lock, flags);
419 	if (local_id < 0)
420 		return NULL;
421 
422 	lpriv = per_cpu_ptr(imsic->lpriv, cpu);
423 	vec = &lpriv->vectors[local_id];
424 	vec->hwirq = hwirq;
425 	vec->enable = false;
426 	vec->move_next = NULL;
427 	vec->move_prev = NULL;
428 
429 	return vec;
430 }
431 
imsic_vector_free(struct imsic_vector * vec)432 void imsic_vector_free(struct imsic_vector *vec)
433 {
434 	unsigned long flags;
435 
436 	raw_spin_lock_irqsave(&imsic->matrix_lock, flags);
437 	vec->hwirq = UINT_MAX;
438 	irq_matrix_free(imsic->matrix, vec->cpu, vec->local_id, false);
439 	raw_spin_unlock_irqrestore(&imsic->matrix_lock, flags);
440 }
441 
imsic_local_cleanup(void)442 static void __init imsic_local_cleanup(void)
443 {
444 	struct imsic_local_priv *lpriv;
445 	int cpu;
446 
447 	for_each_possible_cpu(cpu) {
448 		lpriv = per_cpu_ptr(imsic->lpriv, cpu);
449 
450 		bitmap_free(lpriv->dirty_bitmap);
451 		kfree(lpriv->vectors);
452 	}
453 
454 	free_percpu(imsic->lpriv);
455 }
456 
imsic_local_init(void)457 static int __init imsic_local_init(void)
458 {
459 	struct imsic_global_config *global = &imsic->global;
460 	struct imsic_local_priv *lpriv;
461 	struct imsic_vector *vec;
462 	int cpu, i;
463 
464 	/* Allocate per-CPU private state */
465 	imsic->lpriv = alloc_percpu(typeof(*imsic->lpriv));
466 	if (!imsic->lpriv)
467 		return -ENOMEM;
468 
469 	/* Setup per-CPU private state */
470 	for_each_possible_cpu(cpu) {
471 		lpriv = per_cpu_ptr(imsic->lpriv, cpu);
472 
473 		raw_spin_lock_init(&lpriv->lock);
474 
475 		/* Allocate dirty bitmap */
476 		lpriv->dirty_bitmap = bitmap_zalloc(global->nr_ids + 1, GFP_KERNEL);
477 		if (!lpriv->dirty_bitmap)
478 			goto fail_local_cleanup;
479 
480 #ifdef CONFIG_SMP
481 		/* Setup lazy timer for synchronization */
482 		timer_setup(&lpriv->timer, imsic_local_timer_callback, TIMER_PINNED);
483 #endif
484 
485 		/* Allocate vector array */
486 		lpriv->vectors = kcalloc(global->nr_ids + 1, sizeof(*lpriv->vectors),
487 					 GFP_KERNEL);
488 		if (!lpriv->vectors)
489 			goto fail_local_cleanup;
490 
491 		/* Setup vector array */
492 		for (i = 0; i <= global->nr_ids; i++) {
493 			vec = &lpriv->vectors[i];
494 			vec->cpu = cpu;
495 			vec->local_id = i;
496 			vec->hwirq = UINT_MAX;
497 		}
498 	}
499 
500 	return 0;
501 
502 fail_local_cleanup:
503 	imsic_local_cleanup();
504 	return -ENOMEM;
505 }
506 
imsic_state_online(void)507 void imsic_state_online(void)
508 {
509 	unsigned long flags;
510 
511 	raw_spin_lock_irqsave(&imsic->matrix_lock, flags);
512 	irq_matrix_online(imsic->matrix);
513 	raw_spin_unlock_irqrestore(&imsic->matrix_lock, flags);
514 }
515 
imsic_state_offline(void)516 void imsic_state_offline(void)
517 {
518 	unsigned long flags;
519 
520 	raw_spin_lock_irqsave(&imsic->matrix_lock, flags);
521 	irq_matrix_offline(imsic->matrix);
522 	raw_spin_unlock_irqrestore(&imsic->matrix_lock, flags);
523 
524 #ifdef CONFIG_SMP
525 	struct imsic_local_priv *lpriv = this_cpu_ptr(imsic->lpriv);
526 
527 	raw_spin_lock_irqsave(&lpriv->lock, flags);
528 	WARN_ON_ONCE(try_to_del_timer_sync(&lpriv->timer) < 0);
529 	raw_spin_unlock_irqrestore(&lpriv->lock, flags);
530 #endif
531 }
532 
imsic_matrix_init(void)533 static int __init imsic_matrix_init(void)
534 {
535 	struct imsic_global_config *global = &imsic->global;
536 
537 	raw_spin_lock_init(&imsic->matrix_lock);
538 	imsic->matrix = irq_alloc_matrix(global->nr_ids + 1,
539 					 0, global->nr_ids + 1);
540 	if (!imsic->matrix)
541 		return -ENOMEM;
542 
543 	/* Reserve ID#0 because it is special and never implemented */
544 	irq_matrix_assign_system(imsic->matrix, 0, false);
545 
546 	/* Reserve IPI ID because it is special and used internally */
547 	irq_matrix_assign_system(imsic->matrix, IMSIC_IPI_ID, false);
548 
549 	return 0;
550 }
551 
imsic_populate_global_dt(struct fwnode_handle * fwnode,struct imsic_global_config * global,u32 * nr_parent_irqs)552 static int __init imsic_populate_global_dt(struct fwnode_handle *fwnode,
553 					   struct imsic_global_config *global,
554 					   u32 *nr_parent_irqs)
555 {
556 	int rc;
557 
558 	/* Find number of guest index bits in MSI address */
559 	rc = of_property_read_u32(to_of_node(fwnode), "riscv,guest-index-bits",
560 				  &global->guest_index_bits);
561 	if (rc)
562 		global->guest_index_bits = 0;
563 
564 	/* Find number of HART index bits */
565 	rc = of_property_read_u32(to_of_node(fwnode), "riscv,hart-index-bits",
566 				  &global->hart_index_bits);
567 	if (rc) {
568 		/* Assume default value */
569 		global->hart_index_bits = __fls(*nr_parent_irqs);
570 		if (BIT(global->hart_index_bits) < *nr_parent_irqs)
571 			global->hart_index_bits++;
572 	}
573 
574 	/* Find number of group index bits */
575 	rc = of_property_read_u32(to_of_node(fwnode), "riscv,group-index-bits",
576 				  &global->group_index_bits);
577 	if (rc)
578 		global->group_index_bits = 0;
579 
580 	/*
581 	 * Find first bit position of group index.
582 	 * If not specified assumed the default APLIC-IMSIC configuration.
583 	 */
584 	rc = of_property_read_u32(to_of_node(fwnode), "riscv,group-index-shift",
585 				  &global->group_index_shift);
586 	if (rc)
587 		global->group_index_shift = IMSIC_MMIO_PAGE_SHIFT * 2;
588 
589 	/* Find number of interrupt identities */
590 	rc = of_property_read_u32(to_of_node(fwnode), "riscv,num-ids",
591 				  &global->nr_ids);
592 	if (rc) {
593 		pr_err("%pfwP: number of interrupt identities not found\n", fwnode);
594 		return rc;
595 	}
596 
597 	/* Find number of guest interrupt identities */
598 	rc = of_property_read_u32(to_of_node(fwnode), "riscv,num-guest-ids",
599 				  &global->nr_guest_ids);
600 	if (rc)
601 		global->nr_guest_ids = global->nr_ids;
602 
603 	return 0;
604 }
605 
imsic_populate_global_acpi(struct fwnode_handle * fwnode,struct imsic_global_config * global,u32 * nr_parent_irqs,void * opaque)606 static int __init imsic_populate_global_acpi(struct fwnode_handle *fwnode,
607 					     struct imsic_global_config *global,
608 					     u32 *nr_parent_irqs, void *opaque)
609 {
610 	struct acpi_madt_imsic *imsic = (struct acpi_madt_imsic *)opaque;
611 
612 	global->guest_index_bits = imsic->guest_index_bits;
613 	global->hart_index_bits = imsic->hart_index_bits;
614 	global->group_index_bits = imsic->group_index_bits;
615 	global->group_index_shift = imsic->group_index_shift;
616 	global->nr_ids = imsic->num_ids;
617 	global->nr_guest_ids = imsic->num_guest_ids;
618 	return 0;
619 }
620 
imsic_get_parent_hartid(struct fwnode_handle * fwnode,u32 index,unsigned long * hartid)621 static int __init imsic_get_parent_hartid(struct fwnode_handle *fwnode,
622 					  u32 index, unsigned long *hartid)
623 {
624 	struct of_phandle_args parent;
625 	int rc;
626 
627 	if (!is_of_node(fwnode)) {
628 		if (hartid)
629 			*hartid = acpi_rintc_index_to_hartid(index);
630 
631 		if (!hartid || (*hartid == INVALID_HARTID))
632 			return -EINVAL;
633 
634 		return 0;
635 	}
636 
637 	rc = of_irq_parse_one(to_of_node(fwnode), index, &parent);
638 	if (rc)
639 		return rc;
640 
641 	/*
642 	 * Skip interrupts other than external interrupts for
643 	 * current privilege level.
644 	 */
645 	if (parent.args[0] != RV_IRQ_EXT)
646 		return -EINVAL;
647 
648 	return riscv_of_parent_hartid(parent.np, hartid);
649 }
650 
imsic_get_mmio_resource(struct fwnode_handle * fwnode,u32 index,struct resource * res)651 static int __init imsic_get_mmio_resource(struct fwnode_handle *fwnode,
652 					  u32 index, struct resource *res)
653 {
654 	if (!is_of_node(fwnode))
655 		return acpi_rintc_get_imsic_mmio_info(index, res);
656 
657 	return of_address_to_resource(to_of_node(fwnode), index, res);
658 }
659 
imsic_parse_fwnode(struct fwnode_handle * fwnode,struct imsic_global_config * global,u32 * nr_parent_irqs,u32 * nr_mmios,void * opaque)660 static int __init imsic_parse_fwnode(struct fwnode_handle *fwnode,
661 				     struct imsic_global_config *global,
662 				     u32 *nr_parent_irqs,
663 				     u32 *nr_mmios,
664 				     void *opaque)
665 {
666 	unsigned long hartid;
667 	struct resource res;
668 	int rc;
669 	u32 i;
670 
671 	*nr_parent_irqs = 0;
672 	*nr_mmios = 0;
673 
674 	/* Find number of parent interrupts */
675 	while (!imsic_get_parent_hartid(fwnode, *nr_parent_irqs, &hartid))
676 		(*nr_parent_irqs)++;
677 	if (!*nr_parent_irqs) {
678 		pr_err("%pfwP: no parent irqs available\n", fwnode);
679 		return -EINVAL;
680 	}
681 
682 	if (is_of_node(fwnode))
683 		rc = imsic_populate_global_dt(fwnode, global, nr_parent_irqs);
684 	else
685 		rc = imsic_populate_global_acpi(fwnode, global, nr_parent_irqs, opaque);
686 
687 	if (rc)
688 		return rc;
689 
690 	/* Sanity check guest index bits */
691 	i = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT;
692 	if (i < global->guest_index_bits) {
693 		pr_err("%pfwP: guest index bits too big\n", fwnode);
694 		return -EINVAL;
695 	}
696 
697 	/* Sanity check HART index bits */
698 	i = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT - global->guest_index_bits;
699 	if (i < global->hart_index_bits) {
700 		pr_err("%pfwP: HART index bits too big\n", fwnode);
701 		return -EINVAL;
702 	}
703 
704 	/* Sanity check group index bits */
705 	i = BITS_PER_LONG - IMSIC_MMIO_PAGE_SHIFT -
706 	    global->guest_index_bits - global->hart_index_bits;
707 	if (i < global->group_index_bits) {
708 		pr_err("%pfwP: group index bits too big\n", fwnode);
709 		return -EINVAL;
710 	}
711 
712 	/* Sanity check group index shift */
713 	i = global->group_index_bits + global->group_index_shift - 1;
714 	if (i >= BITS_PER_LONG) {
715 		pr_err("%pfwP: group index shift too big\n", fwnode);
716 		return -EINVAL;
717 	}
718 
719 	/* Sanity check number of interrupt identities */
720 	if (global->nr_ids < IMSIC_MIN_ID ||
721 	    global->nr_ids >= IMSIC_MAX_ID ||
722 	    (global->nr_ids & IMSIC_MIN_ID) != IMSIC_MIN_ID) {
723 		pr_err("%pfwP: invalid number of interrupt identities\n", fwnode);
724 		return -EINVAL;
725 	}
726 
727 	/* Sanity check number of guest interrupt identities */
728 	if (global->nr_guest_ids < IMSIC_MIN_ID ||
729 	    global->nr_guest_ids >= IMSIC_MAX_ID ||
730 	    (global->nr_guest_ids & IMSIC_MIN_ID) != IMSIC_MIN_ID) {
731 		pr_err("%pfwP: invalid number of guest interrupt identities\n", fwnode);
732 		return -EINVAL;
733 	}
734 
735 	/* Compute base address */
736 	rc = imsic_get_mmio_resource(fwnode, 0, &res);
737 	if (rc) {
738 		pr_err("%pfwP: first MMIO resource not found\n", fwnode);
739 		return -EINVAL;
740 	}
741 	global->base_addr = res.start;
742 	global->base_addr &= ~(BIT(global->guest_index_bits +
743 				   global->hart_index_bits +
744 				   IMSIC_MMIO_PAGE_SHIFT) - 1);
745 	global->base_addr &= ~((BIT(global->group_index_bits) - 1) <<
746 			       global->group_index_shift);
747 
748 	/* Find number of MMIO register sets */
749 	while (!imsic_get_mmio_resource(fwnode, *nr_mmios, &res))
750 		(*nr_mmios)++;
751 
752 	return 0;
753 }
754 
imsic_setup_state(struct fwnode_handle * fwnode,void * opaque)755 int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
756 {
757 	u32 i, j, index, nr_parent_irqs, nr_mmios, nr_handlers = 0;
758 	struct imsic_global_config *global;
759 	struct imsic_local_config *local;
760 	void __iomem **mmios_va = NULL;
761 	struct resource *mmios = NULL;
762 	unsigned long reloff, hartid;
763 	phys_addr_t base_addr;
764 	int rc, cpu;
765 
766 	/*
767 	 * Only one IMSIC instance allowed in a platform for clean
768 	 * implementation of SMP IRQ affinity and per-CPU IPIs.
769 	 *
770 	 * This means on a multi-socket (or multi-die) platform we
771 	 * will have multiple MMIO regions for one IMSIC instance.
772 	 */
773 	if (imsic) {
774 		pr_err("%pfwP: already initialized hence ignoring\n", fwnode);
775 		return -EALREADY;
776 	}
777 
778 	if (!riscv_isa_extension_available(NULL, SxAIA)) {
779 		pr_err("%pfwP: AIA support not available\n", fwnode);
780 		return -ENODEV;
781 	}
782 
783 	imsic = kzalloc(sizeof(*imsic), GFP_KERNEL);
784 	if (!imsic)
785 		return -ENOMEM;
786 	imsic->fwnode = fwnode;
787 	global = &imsic->global;
788 
789 	global->local = alloc_percpu(typeof(*global->local));
790 	if (!global->local) {
791 		rc = -ENOMEM;
792 		goto out_free_priv;
793 	}
794 
795 	/* Parse IMSIC fwnode */
796 	rc = imsic_parse_fwnode(fwnode, global, &nr_parent_irqs, &nr_mmios, opaque);
797 	if (rc)
798 		goto out_free_local;
799 
800 	/* Allocate MMIO resource array */
801 	mmios = kcalloc(nr_mmios, sizeof(*mmios), GFP_KERNEL);
802 	if (!mmios) {
803 		rc = -ENOMEM;
804 		goto out_free_local;
805 	}
806 
807 	/* Allocate MMIO virtual address array */
808 	mmios_va = kcalloc(nr_mmios, sizeof(*mmios_va), GFP_KERNEL);
809 	if (!mmios_va) {
810 		rc = -ENOMEM;
811 		goto out_iounmap;
812 	}
813 
814 	/* Parse and map MMIO register sets */
815 	for (i = 0; i < nr_mmios; i++) {
816 		rc = imsic_get_mmio_resource(fwnode, i, &mmios[i]);
817 		if (rc) {
818 			pr_err("%pfwP: unable to parse MMIO regset %d\n", fwnode, i);
819 			goto out_iounmap;
820 		}
821 
822 		base_addr = mmios[i].start;
823 		base_addr &= ~(BIT(global->guest_index_bits +
824 				   global->hart_index_bits +
825 				   IMSIC_MMIO_PAGE_SHIFT) - 1);
826 		base_addr &= ~((BIT(global->group_index_bits) - 1) <<
827 			       global->group_index_shift);
828 		if (base_addr != global->base_addr) {
829 			rc = -EINVAL;
830 			pr_err("%pfwP: address mismatch for regset %d\n", fwnode, i);
831 			goto out_iounmap;
832 		}
833 
834 		mmios_va[i] = ioremap(mmios[i].start, resource_size(&mmios[i]));
835 		if (!mmios_va[i]) {
836 			rc = -EIO;
837 			pr_err("%pfwP: unable to map MMIO regset %d\n", fwnode, i);
838 			goto out_iounmap;
839 		}
840 	}
841 
842 	/* Initialize local (or per-CPU )state */
843 	rc = imsic_local_init();
844 	if (rc) {
845 		pr_err("%pfwP: failed to initialize local state\n",
846 		       fwnode);
847 		goto out_iounmap;
848 	}
849 
850 	/* Configure handlers for target CPUs */
851 	for (i = 0; i < nr_parent_irqs; i++) {
852 		rc = imsic_get_parent_hartid(fwnode, i, &hartid);
853 		if (rc) {
854 			pr_warn("%pfwP: hart ID for parent irq%d not found\n", fwnode, i);
855 			continue;
856 		}
857 
858 		cpu = riscv_hartid_to_cpuid(hartid);
859 		if (cpu < 0) {
860 			pr_warn("%pfwP: invalid cpuid for parent irq%d\n", fwnode, i);
861 			continue;
862 		}
863 
864 		/* Find MMIO location of MSI page */
865 		index = nr_mmios;
866 		reloff = i * BIT(global->guest_index_bits) *
867 			 IMSIC_MMIO_PAGE_SZ;
868 		for (j = 0; nr_mmios; j++) {
869 			if (reloff < resource_size(&mmios[j])) {
870 				index = j;
871 				break;
872 			}
873 
874 			/*
875 			 * MMIO region size may not be aligned to
876 			 * BIT(global->guest_index_bits) * IMSIC_MMIO_PAGE_SZ
877 			 * if holes are present.
878 			 */
879 			reloff -= ALIGN(resource_size(&mmios[j]),
880 			BIT(global->guest_index_bits) * IMSIC_MMIO_PAGE_SZ);
881 		}
882 		if (index >= nr_mmios) {
883 			pr_warn("%pfwP: MMIO not found for parent irq%d\n", fwnode, i);
884 			continue;
885 		}
886 
887 		local = per_cpu_ptr(global->local, cpu);
888 		local->msi_pa = mmios[index].start + reloff;
889 		local->msi_va = mmios_va[index] + reloff;
890 
891 		nr_handlers++;
892 	}
893 
894 	/* If no CPU handlers found then can't take interrupts */
895 	if (!nr_handlers) {
896 		pr_err("%pfwP: No CPU handlers found\n", fwnode);
897 		rc = -ENODEV;
898 		goto out_local_cleanup;
899 	}
900 
901 	/* Initialize matrix allocator */
902 	rc = imsic_matrix_init();
903 	if (rc) {
904 		pr_err("%pfwP: failed to create matrix allocator\n", fwnode);
905 		goto out_local_cleanup;
906 	}
907 
908 	/* We don't need MMIO arrays anymore so let's free-up */
909 	kfree(mmios_va);
910 	kfree(mmios);
911 
912 	return 0;
913 
914 out_local_cleanup:
915 	imsic_local_cleanup();
916 out_iounmap:
917 	for (i = 0; i < nr_mmios; i++) {
918 		if (mmios_va[i])
919 			iounmap(mmios_va[i]);
920 	}
921 	kfree(mmios_va);
922 	kfree(mmios);
923 out_free_local:
924 	free_percpu(imsic->global.local);
925 out_free_priv:
926 	kfree(imsic);
927 	imsic = NULL;
928 	return rc;
929 }
930