blob: 01ee800efde3e91e149af9d2a76f7a4854b52ef7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
David S. Miller4a907de2007-06-13 00:01:04 -07002/* irq.c: UltraSparc IRQ handling/init/registry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David S. Miller227c3312008-04-26 02:19:18 -07004 * Copyright (C) 1997, 2007, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
6 * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/sched.h>
David S. Miller98430992008-09-16 11:44:00 -070010#include <linux/linkage.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/ptrace.h>
12#include <linux/errno.h>
13#include <linux/kernel_stat.h>
14#include <linux/signal.h>
15#include <linux/mm.h>
16#include <linux/interrupt.h>
17#include <linux/slab.h>
18#include <linux/random.h>
19#include <linux/init.h>
20#include <linux/delay.h>
21#include <linux/proc_fs.h>
22#include <linux/seq_file.h>
David S. Miller9960e9e2010-04-07 04:41:33 -070023#include <linux/ftrace.h>
David S. Millere18e2a02006-06-20 01:23:32 -070024#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/ptrace.h>
27#include <asm/processor.h>
Arun Sharma600634972011-07-26 16:09:06 -070028#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/irq.h>
Sven Hartge2e457ef2005-10-08 21:12:04 -070030#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/iommu.h>
32#include <asm/upa.h>
33#include <asm/oplib.h>
David S. Miller25c75812006-06-22 20:21:22 -070034#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/timer.h>
36#include <asm/smp.h>
37#include <asm/starfire.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080038#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/cache.h>
40#include <asm/cpudata.h>
David S. Miller63b61452005-06-27 17:04:45 -070041#include <asm/auxio.h>
David S. Miller92704a12006-02-26 23:27:19 -080042#include <asm/head.h>
David S. Miller4a907de2007-06-13 00:01:04 -070043#include <asm/hypervisor.h>
David S. Miller42d5f992007-10-13 23:03:21 -070044#include <asm/cacheflush.h>
Thomas Gleixnerdb1cc7a2021-02-10 00:40:53 +010045#include <asm/softirq_stack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
David S. Millerd91aa122008-03-26 00:37:51 -070047#include "entry.h"
Hong H. Pham280ff972009-06-04 02:10:11 -070048#include "cpumap.h"
David S. Millerec687882010-04-14 02:04:29 -070049#include "kstack.h"
David S. Millere18e2a02006-06-20 01:23:32 -070050
David S. Miller10397e42007-10-13 21:43:31 -070051struct ino_bucket *ivector_table;
David S. Millereb2d8d62007-10-13 21:42:46 -070052unsigned long ivector_table_pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
David S. Miller42d5f992007-10-13 23:03:21 -070054/* On several sun4u processors, it is illegal to mix bypass and
55 * non-bypass accesses. Therefore we access all INO buckets
56 * using bypass accesses only.
57 */
58static unsigned long bucket_get_chain_pa(unsigned long bucket_pa)
59{
60 unsigned long ret;
61
62 __asm__ __volatile__("ldxa [%1] %2, %0"
63 : "=&r" (ret)
64 : "r" (bucket_pa +
65 offsetof(struct ino_bucket,
66 __irq_chain_pa)),
67 "i" (ASI_PHYS_USE_EC));
68
69 return ret;
70}
71
72static void bucket_clear_chain_pa(unsigned long bucket_pa)
73{
74 __asm__ __volatile__("stxa %%g0, [%0] %1"
75 : /* no outputs */
76 : "r" (bucket_pa +
77 offsetof(struct ino_bucket,
78 __irq_chain_pa)),
79 "i" (ASI_PHYS_USE_EC));
80}
81
Sam Ravnborgfe414932011-01-22 11:32:19 +000082static unsigned int bucket_get_irq(unsigned long bucket_pa)
David S. Miller42d5f992007-10-13 23:03:21 -070083{
84 unsigned int ret;
85
86 __asm__ __volatile__("lduwa [%1] %2, %0"
87 : "=&r" (ret)
88 : "r" (bucket_pa +
89 offsetof(struct ino_bucket,
Sam Ravnborgfe414932011-01-22 11:32:19 +000090 __irq)),
David S. Miller42d5f992007-10-13 23:03:21 -070091 "i" (ASI_PHYS_USE_EC));
92
93 return ret;
94}
95
Sam Ravnborgfe414932011-01-22 11:32:19 +000096static void bucket_set_irq(unsigned long bucket_pa, unsigned int irq)
David S. Miller42d5f992007-10-13 23:03:21 -070097{
98 __asm__ __volatile__("stwa %0, [%1] %2"
99 : /* no outputs */
Sam Ravnborgfe414932011-01-22 11:32:19 +0000100 : "r" (irq),
David S. Miller42d5f992007-10-13 23:03:21 -0700101 "r" (bucket_pa +
102 offsetof(struct ino_bucket,
Sam Ravnborgfe414932011-01-22 11:32:19 +0000103 __irq)),
David S. Miller42d5f992007-10-13 23:03:21 -0700104 "i" (ASI_PHYS_USE_EC));
105}
106
David S. Millereb2d8d62007-10-13 21:42:46 -0700107#define irq_work_pa(__cpu) &(trap_block[(__cpu)].irq_worklist_pa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
bob piccoee6a9332014-09-25 12:25:03 -0700109static unsigned long hvirq_major __initdata;
110static int __init early_hvirq_major(char *p)
David S. Miller8047e242006-06-20 01:22:35 -0700111{
bob piccoee6a9332014-09-25 12:25:03 -0700112 int rc = kstrtoul(p, 10, &hvirq_major);
David S. Miller8047e242006-06-20 01:22:35 -0700113
bob piccoee6a9332014-09-25 12:25:03 -0700114 return rc;
115}
116early_param("hvirq", early_hvirq_major);
David S. Miller8047e242006-06-20 01:22:35 -0700117
bob piccoee6a9332014-09-25 12:25:03 -0700118static int hv_irq_version;
David S. Miller759f89e2007-10-11 03:16:13 -0700119
bob piccoee6a9332014-09-25 12:25:03 -0700120/* Major version 2.0 of HV_GRP_INTR added support for the VIRQ cookie
121 * based interfaces, but:
122 *
123 * 1) Several OSs, Solaris and Linux included, use them even when only
124 * negotiating version 1.0 (or failing to negotiate at all). So the
125 * hypervisor has a workaround that provides the VIRQ interfaces even
126 * when only verion 1.0 of the API is in use.
127 *
128 * 2) Second, and more importantly, with major version 2.0 these VIRQ
129 * interfaces only were actually hooked up for LDC interrupts, even
130 * though the Hypervisor specification clearly stated:
131 *
132 * The new interrupt API functions will be available to a guest
133 * when it negotiates version 2.0 in the interrupt API group 0x2. When
134 * a guest negotiates version 2.0, all interrupt sources will only
135 * support using the cookie interface, and any attempt to use the
136 * version 1.0 interrupt APIs numbered 0xa0 to 0xa6 will result in the
137 * ENOTSUPPORTED error being returned.
138 *
139 * with an emphasis on "all interrupt sources".
140 *
141 * To correct this, major version 3.0 was created which does actually
142 * support VIRQs for all interrupt sources (not just LDC devices). So
143 * if we want to move completely over the cookie based VIRQs we must
144 * negotiate major version 3.0 or later of HV_GRP_INTR.
145 */
146static bool sun4v_cookie_only_virqs(void)
147{
148 if (hv_irq_version >= 3)
149 return true;
150 return false;
David S. Miller8047e242006-06-20 01:22:35 -0700151}
152
bob piccoee6a9332014-09-25 12:25:03 -0700153static void __init irq_init_hv(void)
David S. Miller8047e242006-06-20 01:22:35 -0700154{
bob piccoee6a9332014-09-25 12:25:03 -0700155 unsigned long hv_error, major, minor = 0;
David S. Miller8047e242006-06-20 01:22:35 -0700156
bob piccoee6a9332014-09-25 12:25:03 -0700157 if (tlb_type != hypervisor)
David S. Miller35a17eb2007-02-10 17:41:02 -0800158 return;
159
bob piccoee6a9332014-09-25 12:25:03 -0700160 if (hvirq_major)
161 major = hvirq_major;
162 else
163 major = 3;
David S. Miller759f89e2007-10-11 03:16:13 -0700164
bob piccoee6a9332014-09-25 12:25:03 -0700165 hv_error = sun4v_hvapi_register(HV_GRP_INTR, major, &minor);
166 if (!hv_error)
167 hv_irq_version = major;
168 else
169 hv_irq_version = 1;
David S. Miller35a17eb2007-02-10 17:41:02 -0800170
bob piccoee6a9332014-09-25 12:25:03 -0700171 pr_info("SUN4V: Using IRQ API major %d, cookie only virqs %s\n",
172 hv_irq_version,
173 sun4v_cookie_only_virqs() ? "enabled" : "disabled");
David S. Miller8047e242006-06-20 01:22:35 -0700174}
bob piccoee6a9332014-09-25 12:25:03 -0700175
176/* This function is for the timer interrupt.*/
177int __init arch_probe_nr_irqs(void)
178{
179 return 1;
180}
181
182#define DEFAULT_NUM_IVECS (0xfffU)
183static unsigned int nr_ivec = DEFAULT_NUM_IVECS;
184#define NUM_IVECS (nr_ivec)
185
186static unsigned int __init size_nr_ivec(void)
187{
188 if (tlb_type == hypervisor) {
189 switch (sun4v_chip_type) {
190 /* Athena's devhandle|devino is large.*/
191 case SUN4V_CHIP_SPARC64X:
192 nr_ivec = 0xffff;
193 break;
194 }
195 }
196 return nr_ivec;
197}
198
199struct irq_handler_data {
200 union {
201 struct {
202 unsigned int dev_handle;
203 unsigned int dev_ino;
204 };
205 unsigned long sysino;
206 };
207 struct ino_bucket bucket;
208 unsigned long iclr;
209 unsigned long imap;
210};
211
212static inline unsigned int irq_data_to_handle(struct irq_data *data)
213{
Jiang Liu6a4a5b32015-06-01 16:05:17 +0800214 struct irq_handler_data *ihd = irq_data_get_irq_handler_data(data);
bob piccoee6a9332014-09-25 12:25:03 -0700215
216 return ihd->dev_handle;
217}
218
219static inline unsigned int irq_data_to_ino(struct irq_data *data)
220{
Jiang Liu6a4a5b32015-06-01 16:05:17 +0800221 struct irq_handler_data *ihd = irq_data_get_irq_handler_data(data);
bob piccoee6a9332014-09-25 12:25:03 -0700222
223 return ihd->dev_ino;
224}
225
226static inline unsigned long irq_data_to_sysino(struct irq_data *data)
227{
Jiang Liu6a4a5b32015-06-01 16:05:17 +0800228 struct irq_handler_data *ihd = irq_data_get_irq_handler_data(data);
bob piccoee6a9332014-09-25 12:25:03 -0700229
230 return ihd->sysino;
231}
232
233void irq_free(unsigned int irq)
234{
235 void *data = irq_get_handler_data(irq);
236
237 kfree(data);
238 irq_set_handler_data(irq, NULL);
239 irq_free_descs(irq, 1);
240}
241
242unsigned int irq_alloc(unsigned int dev_handle, unsigned int dev_ino)
243{
244 int irq;
245
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900246 irq = __irq_alloc_descs(-1, 1, 1, numa_node_id(), NULL, NULL);
bob piccoee6a9332014-09-25 12:25:03 -0700247 if (irq <= 0)
248 goto out;
249
250 return irq;
251out:
252 return 0;
253}
254
255static unsigned int cookie_exists(u32 devhandle, unsigned int devino)
256{
257 unsigned long hv_err, cookie;
258 struct ino_bucket *bucket;
259 unsigned int irq = 0U;
260
261 hv_err = sun4v_vintr_get_cookie(devhandle, devino, &cookie);
262 if (hv_err) {
263 pr_err("HV get cookie failed hv_err = %ld\n", hv_err);
264 goto out;
265 }
266
267 if (cookie & ((1UL << 63UL))) {
268 cookie = ~cookie;
269 bucket = (struct ino_bucket *) __va(cookie);
270 irq = bucket->__irq;
271 }
272out:
273 return irq;
274}
275
276static unsigned int sysino_exists(u32 devhandle, unsigned int devino)
277{
278 unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
279 struct ino_bucket *bucket;
280 unsigned int irq;
281
282 bucket = &ivector_table[sysino];
283 irq = bucket_get_irq(__pa(bucket));
284
285 return irq;
286}
287
288void ack_bad_irq(unsigned int irq)
289{
290 pr_crit("BAD IRQ ack %d\n", irq);
291}
292
293void irq_install_pre_handler(int irq,
294 void (*func)(unsigned int, void *, void *),
295 void *arg1, void *arg2)
296{
297 pr_warn("IRQ pre handler NOT supported.\n");
298}
David S. Miller8047e242006-06-20 01:22:35 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/*
David S. Millere18e2a02006-06-20 01:23:32 -0700301 * /proc/interrupts printing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
Thomas Gleixnerfa680c72011-03-24 18:03:13 +0100303int arch_show_interrupts(struct seq_file *p, int prec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Thomas Gleixnerfa680c72011-03-24 18:03:13 +0100305 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Thomas Gleixnerfa680c72011-03-24 18:03:13 +0100307 seq_printf(p, "NMI: ");
308 for_each_online_cpu(j)
309 seq_printf(p, "%10u ", cpu_data(j).__nmi_count);
310 seq_printf(p, " Non-maskable interrupts\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return 0;
312}
313
David S. Millerebd8c562006-02-17 08:38:06 -0800314static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
315{
316 unsigned int tid;
317
318 if (this_is_starfire) {
319 tid = starfire_translate(imap, cpuid);
320 tid <<= IMAP_TID_SHIFT;
321 tid &= IMAP_TID_UPA;
322 } else {
323 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
324 unsigned long ver;
325
326 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
327 if ((ver >> 32UL) == __JALAPENO_ID ||
328 (ver >> 32UL) == __SERRANO_ID) {
329 tid = cpuid << IMAP_TID_SHIFT;
330 tid &= IMAP_TID_JBUS;
331 } else {
332 unsigned int a = cpuid & 0x1f;
333 unsigned int n = (cpuid >> 5) & 0x1f;
334
335 tid = ((a << IMAP_AID_SHIFT) |
336 (n << IMAP_NID_SHIFT));
337 tid &= (IMAP_AID_SAFARI |
Joe Perchesa419aef2009-08-18 11:18:35 -0700338 IMAP_NID_SAFARI);
David S. Millerebd8c562006-02-17 08:38:06 -0800339 }
340 } else {
341 tid = cpuid << IMAP_TID_SHIFT;
342 tid &= IMAP_TID_UPA;
343 }
344 }
345
346 return tid;
347}
348
David S. Millere18e2a02006-06-20 01:23:32 -0700349#ifdef CONFIG_SMP
Sam Ravnborgfe414932011-01-22 11:32:19 +0000350static int irq_choose_cpu(unsigned int irq, const struct cpumask *affinity)
David S. Millere18e2a02006-06-20 01:23:32 -0700351{
David S. Millere18e2a02006-06-20 01:23:32 -0700352 int cpuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Dawei Lic4650ba2024-04-24 10:55:45 +0800354 if (cpumask_equal(affinity, cpu_online_mask)) {
Sam Ravnborgfe414932011-01-22 11:32:19 +0000355 cpuid = map_to_cpu(irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700356 } else {
Dawei Lic4650ba2024-04-24 10:55:45 +0800357 cpuid = cpumask_first_and(affinity, cpu_online_mask);
358 cpuid = cpuid < nr_cpu_ids ? cpuid : map_to_cpu(irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700359 }
360
361 return cpuid;
362}
363#else
Sam Ravnborgfe414932011-01-22 11:32:19 +0000364#define irq_choose_cpu(irq, affinity) \
David S. Miller6abce772010-01-26 04:16:49 -0800365 real_hard_smp_processor_id()
David S. Millere18e2a02006-06-20 01:23:32 -0700366#endif
367
Sam Ravnborg4832b992011-01-22 11:32:18 +0000368static void sun4u_irq_enable(struct irq_data *data)
David S. Millere18e2a02006-06-20 01:23:32 -0700369{
Jiang Liu6a4a5b32015-06-01 16:05:17 +0800370 struct irq_handler_data *handler_data;
David S. Millere18e2a02006-06-20 01:23:32 -0700371
Jiang Liu6a4a5b32015-06-01 16:05:17 +0800372 handler_data = irq_data_get_irq_handler_data(data);
Sam Ravnborgcae787282011-01-22 11:32:16 +0000373 if (likely(handler_data)) {
David S. Miller861fe902007-05-02 17:31:36 -0700374 unsigned long cpuid, imap, val;
David S. Millere18e2a02006-06-20 01:23:32 -0700375 unsigned int tid;
376
Jiang Liud7185a92015-06-01 16:05:35 +0800377 cpuid = irq_choose_cpu(data->irq,
378 irq_data_get_affinity_mask(data));
Sam Ravnborgcae787282011-01-22 11:32:16 +0000379 imap = handler_data->imap;
David S. Millere18e2a02006-06-20 01:23:32 -0700380
381 tid = sun4u_compute_tid(imap, cpuid);
382
David S. Miller861fe902007-05-02 17:31:36 -0700383 val = upa_readq(imap);
384 val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
385 IMAP_AID_SAFARI | IMAP_NID_SAFARI);
386 val |= tid | IMAP_VALID;
387 upa_writeq(val, imap);
Sam Ravnborgcae787282011-01-22 11:32:16 +0000388 upa_writeq(ICLR_IDLE, handler_data->iclr);
David S. Millere18e2a02006-06-20 01:23:32 -0700389 }
390}
391
Sam Ravnborg4832b992011-01-22 11:32:18 +0000392static int sun4u_set_affinity(struct irq_data *data,
393 const struct cpumask *mask, bool force)
David S. Millerb53bcb62007-07-14 03:16:13 -0700394{
Jiang Liu6a4a5b32015-06-01 16:05:17 +0800395 struct irq_handler_data *handler_data;
David S. Miller1091ce62010-01-20 19:30:49 -0800396
Jiang Liu6a4a5b32015-06-01 16:05:17 +0800397 handler_data = irq_data_get_irq_handler_data(data);
Sam Ravnborgcae787282011-01-22 11:32:16 +0000398 if (likely(handler_data)) {
David S. Miller1091ce62010-01-20 19:30:49 -0800399 unsigned long cpuid, imap, val;
400 unsigned int tid;
401
Sam Ravnborg4832b992011-01-22 11:32:18 +0000402 cpuid = irq_choose_cpu(data->irq, mask);
Sam Ravnborgcae787282011-01-22 11:32:16 +0000403 imap = handler_data->imap;
David S. Miller1091ce62010-01-20 19:30:49 -0800404
405 tid = sun4u_compute_tid(imap, cpuid);
406
407 val = upa_readq(imap);
408 val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
409 IMAP_AID_SAFARI | IMAP_NID_SAFARI);
410 val |= tid | IMAP_VALID;
411 upa_writeq(val, imap);
Sam Ravnborgcae787282011-01-22 11:32:16 +0000412 upa_writeq(ICLR_IDLE, handler_data->iclr);
David S. Miller1091ce62010-01-20 19:30:49 -0800413 }
Yinghai Lud5dedd42009-04-27 17:59:21 -0700414
415 return 0;
David S. Millerb53bcb62007-07-14 03:16:13 -0700416}
417
David S. Millerd0cac392009-03-04 14:43:47 -0800418/* Don't do anything. The desc->status check for IRQ_DISABLED in
419 * handler_irq() will skip the handler call and that will leave the
420 * interrupt in the sent state. The next ->enable() call will hit the
421 * ICLR register to reset the state machine.
422 *
423 * This scheme is necessary, instead of clearing the Valid bit in the
424 * IMAP register, to handle the case of IMAP registers being shared by
425 * multiple INOs (and thus ICLR registers). Since we use a different
426 * virtual IRQ for each shared IMAP instance, the generic code thinks
427 * there is only one user so it prematurely calls ->disable() on
428 * free_irq().
429 *
430 * We have to provide an explicit ->disable() method instead of using
431 * NULL to get the default. The reason is that if the generic code
432 * sees that, it also hooks up a default ->shutdown method which
433 * invokes ->mask() which we do not want. See irq_chip_set_defaults().
434 */
Sam Ravnborg4832b992011-01-22 11:32:18 +0000435static void sun4u_irq_disable(struct irq_data *data)
David S. Millere18e2a02006-06-20 01:23:32 -0700436{
David S. Millere18e2a02006-06-20 01:23:32 -0700437}
438
Sam Ravnborg4832b992011-01-22 11:32:18 +0000439static void sun4u_irq_eoi(struct irq_data *data)
David S. Millere18e2a02006-06-20 01:23:32 -0700440{
Jiang Liu6a4a5b32015-06-01 16:05:17 +0800441 struct irq_handler_data *handler_data;
David S. Millere18e2a02006-06-20 01:23:32 -0700442
Jiang Liu6a4a5b32015-06-01 16:05:17 +0800443 handler_data = irq_data_get_irq_handler_data(data);
Sam Ravnborgcae787282011-01-22 11:32:16 +0000444 if (likely(handler_data))
445 upa_writeq(ICLR_IDLE, handler_data->iclr);
David S. Millere18e2a02006-06-20 01:23:32 -0700446}
447
Sam Ravnborg4832b992011-01-22 11:32:18 +0000448static void sun4v_irq_enable(struct irq_data *data)
David S. Millere18e2a02006-06-20 01:23:32 -0700449{
Jiang Liud7185a92015-06-01 16:05:35 +0800450 unsigned long cpuid = irq_choose_cpu(data->irq,
451 irq_data_get_affinity_mask(data));
bob piccoee6a9332014-09-25 12:25:03 -0700452 unsigned int ino = irq_data_to_sysino(data);
David S. Miller771823002007-10-13 23:41:28 -0700453 int err;
David S. Millere18e2a02006-06-20 01:23:32 -0700454
David S. Miller771823002007-10-13 23:41:28 -0700455 err = sun4v_intr_settarget(ino, cpuid);
456 if (err != HV_EOK)
457 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
458 "err(%d)\n", ino, cpuid, err);
459 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
460 if (err != HV_EOK)
461 printk(KERN_ERR "sun4v_intr_setstate(%x): "
462 "err(%d)\n", ino, err);
463 err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
464 if (err != HV_EOK)
465 printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
466 ino, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Sam Ravnborg4832b992011-01-22 11:32:18 +0000469static int sun4v_set_affinity(struct irq_data *data,
470 const struct cpumask *mask, bool force)
David S. Millerb53bcb62007-07-14 03:16:13 -0700471{
Sam Ravnborg4832b992011-01-22 11:32:18 +0000472 unsigned long cpuid = irq_choose_cpu(data->irq, mask);
bob piccoee6a9332014-09-25 12:25:03 -0700473 unsigned int ino = irq_data_to_sysino(data);
David S. Miller771823002007-10-13 23:41:28 -0700474 int err;
David S. Millerb53bcb62007-07-14 03:16:13 -0700475
David S. Miller771823002007-10-13 23:41:28 -0700476 err = sun4v_intr_settarget(ino, cpuid);
477 if (err != HV_EOK)
478 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
479 "err(%d)\n", ino, cpuid, err);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700480
481 return 0;
David S. Millerb53bcb62007-07-14 03:16:13 -0700482}
483
Sam Ravnborg4832b992011-01-22 11:32:18 +0000484static void sun4v_irq_disable(struct irq_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
bob piccoee6a9332014-09-25 12:25:03 -0700486 unsigned int ino = irq_data_to_sysino(data);
David S. Miller771823002007-10-13 23:41:28 -0700487 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
David S. Miller771823002007-10-13 23:41:28 -0700489 err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
490 if (err != HV_EOK)
491 printk(KERN_ERR "sun4v_intr_setenabled(%x): "
492 "err(%d)\n", ino, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Sam Ravnborg4832b992011-01-22 11:32:18 +0000495static void sun4v_irq_eoi(struct irq_data *data)
David S. Miller088dd1f2005-07-04 13:24:38 -0700496{
bob piccoee6a9332014-09-25 12:25:03 -0700497 unsigned int ino = irq_data_to_sysino(data);
David S. Miller771823002007-10-13 23:41:28 -0700498 int err;
David S. Miller5a606b72007-07-09 22:40:36 -0700499
David S. Miller771823002007-10-13 23:41:28 -0700500 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
501 if (err != HV_EOK)
502 printk(KERN_ERR "sun4v_intr_setstate(%x): "
503 "err(%d)\n", ino, err);
David S. Miller088dd1f2005-07-04 13:24:38 -0700504}
505
Sam Ravnborg4832b992011-01-22 11:32:18 +0000506static void sun4v_virq_enable(struct irq_data *data)
David S. Miller4a907de2007-06-13 00:01:04 -0700507{
bob piccoee6a9332014-09-25 12:25:03 -0700508 unsigned long dev_handle = irq_data_to_handle(data);
509 unsigned long dev_ino = irq_data_to_ino(data);
510 unsigned long cpuid;
David S. Miller771823002007-10-13 23:41:28 -0700511 int err;
David S. Miller4a907de2007-06-13 00:01:04 -0700512
Jiang Liud7185a92015-06-01 16:05:35 +0800513 cpuid = irq_choose_cpu(data->irq, irq_data_get_affinity_mask(data));
David S. Miller4a907de2007-06-13 00:01:04 -0700514
David S. Miller771823002007-10-13 23:41:28 -0700515 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
516 if (err != HV_EOK)
517 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
518 "err(%d)\n",
519 dev_handle, dev_ino, cpuid, err);
520 err = sun4v_vintr_set_state(dev_handle, dev_ino,
521 HV_INTR_STATE_IDLE);
522 if (err != HV_EOK)
523 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
524 "HV_INTR_STATE_IDLE): err(%d)\n",
525 dev_handle, dev_ino, err);
526 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
527 HV_INTR_ENABLED);
528 if (err != HV_EOK)
529 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
530 "HV_INTR_ENABLED): err(%d)\n",
531 dev_handle, dev_ino, err);
David S. Miller4a907de2007-06-13 00:01:04 -0700532}
533
Sam Ravnborg4832b992011-01-22 11:32:18 +0000534static int sun4v_virt_set_affinity(struct irq_data *data,
535 const struct cpumask *mask, bool force)
David S. Millerb53bcb62007-07-14 03:16:13 -0700536{
bob piccoee6a9332014-09-25 12:25:03 -0700537 unsigned long dev_handle = irq_data_to_handle(data);
538 unsigned long dev_ino = irq_data_to_ino(data);
539 unsigned long cpuid;
David S. Miller771823002007-10-13 23:41:28 -0700540 int err;
David S. Millerb53bcb62007-07-14 03:16:13 -0700541
Sam Ravnborg4832b992011-01-22 11:32:18 +0000542 cpuid = irq_choose_cpu(data->irq, mask);
David S. Millerb53bcb62007-07-14 03:16:13 -0700543
David S. Miller771823002007-10-13 23:41:28 -0700544 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
545 if (err != HV_EOK)
546 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
547 "err(%d)\n",
548 dev_handle, dev_ino, cpuid, err);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700549
550 return 0;
David S. Millerb53bcb62007-07-14 03:16:13 -0700551}
552
Sam Ravnborg4832b992011-01-22 11:32:18 +0000553static void sun4v_virq_disable(struct irq_data *data)
David S. Miller4a907de2007-06-13 00:01:04 -0700554{
bob piccoee6a9332014-09-25 12:25:03 -0700555 unsigned long dev_handle = irq_data_to_handle(data);
556 unsigned long dev_ino = irq_data_to_ino(data);
David S. Miller771823002007-10-13 23:41:28 -0700557 int err;
David S. Miller4a907de2007-06-13 00:01:04 -0700558
David S. Miller4a907de2007-06-13 00:01:04 -0700559
David S. Miller771823002007-10-13 23:41:28 -0700560 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
561 HV_INTR_DISABLED);
562 if (err != HV_EOK)
563 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
564 "HV_INTR_DISABLED): err(%d)\n",
565 dev_handle, dev_ino, err);
David S. Miller4a907de2007-06-13 00:01:04 -0700566}
567
Sam Ravnborg4832b992011-01-22 11:32:18 +0000568static void sun4v_virq_eoi(struct irq_data *data)
David S. Miller4a907de2007-06-13 00:01:04 -0700569{
bob piccoee6a9332014-09-25 12:25:03 -0700570 unsigned long dev_handle = irq_data_to_handle(data);
571 unsigned long dev_ino = irq_data_to_ino(data);
David S. Miller771823002007-10-13 23:41:28 -0700572 int err;
David S. Miller5a606b72007-07-09 22:40:36 -0700573
David S. Miller771823002007-10-13 23:41:28 -0700574 err = sun4v_vintr_set_state(dev_handle, dev_ino,
575 HV_INTR_STATE_IDLE);
576 if (err != HV_EOK)
577 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
578 "HV_INTR_STATE_IDLE): err(%d)\n",
579 dev_handle, dev_ino, err);
David S. Miller4a907de2007-06-13 00:01:04 -0700580}
581
David S. Miller729e7d72006-12-12 00:59:12 -0800582static struct irq_chip sun4u_irq = {
Sam Ravnborg4832b992011-01-22 11:32:18 +0000583 .name = "sun4u",
584 .irq_enable = sun4u_irq_enable,
585 .irq_disable = sun4u_irq_disable,
586 .irq_eoi = sun4u_irq_eoi,
587 .irq_set_affinity = sun4u_set_affinity,
Thomas Gleixnerfcd8d4f2011-03-24 09:03:45 +0100588 .flags = IRQCHIP_EOI_IF_HANDLED,
David S. Millere18e2a02006-06-20 01:23:32 -0700589};
590
David S. Miller729e7d72006-12-12 00:59:12 -0800591static struct irq_chip sun4v_irq = {
Sam Ravnborg4832b992011-01-22 11:32:18 +0000592 .name = "sun4v",
593 .irq_enable = sun4v_irq_enable,
594 .irq_disable = sun4v_irq_disable,
595 .irq_eoi = sun4v_irq_eoi,
596 .irq_set_affinity = sun4v_set_affinity,
Thomas Gleixnerfcd8d4f2011-03-24 09:03:45 +0100597 .flags = IRQCHIP_EOI_IF_HANDLED,
David S. Millere18e2a02006-06-20 01:23:32 -0700598};
599
David S. Miller4a907de2007-06-13 00:01:04 -0700600static struct irq_chip sun4v_virq = {
Sam Ravnborg4832b992011-01-22 11:32:18 +0000601 .name = "vsun4v",
602 .irq_enable = sun4v_virq_enable,
603 .irq_disable = sun4v_virq_disable,
604 .irq_eoi = sun4v_virq_eoi,
605 .irq_set_affinity = sun4v_virt_set_affinity,
Thomas Gleixnerfcd8d4f2011-03-24 09:03:45 +0100606 .flags = IRQCHIP_EOI_IF_HANDLED,
David S. Miller4a907de2007-06-13 00:01:04 -0700607};
608
David S. Millere18e2a02006-06-20 01:23:32 -0700609unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Sam Ravnborgcae787282011-01-22 11:32:16 +0000611 struct irq_handler_data *handler_data;
bob piccoee6a9332014-09-25 12:25:03 -0700612 struct ino_bucket *bucket;
Sam Ravnborgfe414932011-01-22 11:32:19 +0000613 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 int ino;
615
David S. Miller10951ee2006-02-13 18:22:57 -0800616 BUG_ON(tlb_type == hypervisor);
617
David S. Miller861fe902007-05-02 17:31:36 -0700618 ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
David S. Miller088dd1f2005-07-04 13:24:38 -0700619 bucket = &ivector_table[ino];
Sam Ravnborgfe414932011-01-22 11:32:19 +0000620 irq = bucket_get_irq(__pa(bucket));
621 if (!irq) {
622 irq = irq_alloc(0, ino);
623 bucket_set_irq(__pa(bucket), irq);
Thomas Gleixner394d4412011-03-24 17:52:54 +0100624 irq_set_chip_and_handler_name(irq, &sun4u_irq,
625 handle_fasteoi_irq, "IVEC");
David S. Miller088dd1f2005-07-04 13:24:38 -0700626 }
627
Thomas Gleixner394d4412011-03-24 17:52:54 +0100628 handler_data = irq_get_handler_data(irq);
Sam Ravnborgcae787282011-01-22 11:32:16 +0000629 if (unlikely(handler_data))
David S. Millere18e2a02006-06-20 01:23:32 -0700630 goto out;
631
Sam Ravnborgcae787282011-01-22 11:32:16 +0000632 handler_data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
633 if (unlikely(!handler_data)) {
David S. Millere18e2a02006-06-20 01:23:32 -0700634 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
David S. Miller088dd1f2005-07-04 13:24:38 -0700635 prom_halt();
636 }
Thomas Gleixner394d4412011-03-24 17:52:54 +0100637 irq_set_handler_data(irq, handler_data);
David S. Miller088dd1f2005-07-04 13:24:38 -0700638
Sam Ravnborgcae787282011-01-22 11:32:16 +0000639 handler_data->imap = imap;
640 handler_data->iclr = iclr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
David S. Miller088dd1f2005-07-04 13:24:38 -0700642out:
Sam Ravnborgfe414932011-01-22 11:32:19 +0000643 return irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
bob piccoee6a9332014-09-25 12:25:03 -0700646static unsigned int sun4v_build_common(u32 devhandle, unsigned int devino,
647 void (*handler_data_init)(struct irq_handler_data *data,
648 u32 devhandle, unsigned int devino),
649 struct irq_chip *chip)
David S. Millere3999572006-02-13 18:16:10 -0800650{
bob piccoee6a9332014-09-25 12:25:03 -0700651 struct irq_handler_data *data;
Sam Ravnborgfe414932011-01-22 11:32:19 +0000652 unsigned int irq;
David S. Millere18e2a02006-06-20 01:23:32 -0700653
bob piccoee6a9332014-09-25 12:25:03 -0700654 irq = irq_alloc(devhandle, devino);
655 if (!irq)
David S. Millere18e2a02006-06-20 01:23:32 -0700656 goto out;
657
bob piccoee6a9332014-09-25 12:25:03 -0700658 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
659 if (unlikely(!data)) {
660 pr_err("IRQ handler data allocation failed.\n");
661 irq_free(irq);
662 irq = 0;
663 goto out;
David S. Millere18e2a02006-06-20 01:23:32 -0700664 }
David S. Millere3999572006-02-13 18:16:10 -0800665
bob piccoee6a9332014-09-25 12:25:03 -0700666 irq_set_handler_data(irq, data);
667 handler_data_init(data, devhandle, devino);
668 irq_set_chip_and_handler_name(irq, chip, handle_fasteoi_irq, "IVEC");
669 data->imap = ~0UL;
670 data->iclr = ~0UL;
671out:
672 return irq;
673}
674
675static unsigned long cookie_assign(unsigned int irq, u32 devhandle,
676 unsigned int devino)
677{
678 struct irq_handler_data *ihd = irq_get_handler_data(irq);
679 unsigned long hv_error, cookie;
680
681 /* handler_irq needs to find the irq. cookie is seen signed in
682 * sun4v_dev_mondo and treated as a non ivector_table delivery.
David S. Millere3999572006-02-13 18:16:10 -0800683 */
bob piccoee6a9332014-09-25 12:25:03 -0700684 ihd->bucket.__irq = irq;
685 cookie = ~__pa(&ihd->bucket);
David S. Millere3999572006-02-13 18:16:10 -0800686
bob piccoee6a9332014-09-25 12:25:03 -0700687 hv_error = sun4v_vintr_set_cookie(devhandle, devino, cookie);
688 if (hv_error)
689 pr_err("HV vintr set cookie failed = %ld\n", hv_error);
690
691 return hv_error;
692}
693
694static void cookie_handler_data(struct irq_handler_data *data,
695 u32 devhandle, unsigned int devino)
696{
697 data->dev_handle = devhandle;
698 data->dev_ino = devino;
699}
700
701static unsigned int cookie_build_irq(u32 devhandle, unsigned int devino,
702 struct irq_chip *chip)
703{
704 unsigned long hv_error;
705 unsigned int irq;
706
707 irq = sun4v_build_common(devhandle, devino, cookie_handler_data, chip);
708
709 hv_error = cookie_assign(irq, devhandle, devino);
710 if (hv_error) {
711 irq_free(irq);
712 irq = 0;
713 }
714
715 return irq;
716}
717
718static unsigned int sun4v_build_cookie(u32 devhandle, unsigned int devino)
719{
720 unsigned int irq;
721
722 irq = cookie_exists(devhandle, devino);
723 if (irq)
724 goto out;
725
726 irq = cookie_build_irq(devhandle, devino, &sun4v_virq);
727
728out:
729 return irq;
730}
731
732static void sysino_set_bucket(unsigned int irq)
733{
734 struct irq_handler_data *ihd = irq_get_handler_data(irq);
735 struct ino_bucket *bucket;
736 unsigned long sysino;
737
738 sysino = sun4v_devino_to_sysino(ihd->dev_handle, ihd->dev_ino);
739 BUG_ON(sysino >= nr_ivec);
740 bucket = &ivector_table[sysino];
741 bucket_set_irq(__pa(bucket), irq);
742}
743
744static void sysino_handler_data(struct irq_handler_data *data,
745 u32 devhandle, unsigned int devino)
746{
747 unsigned long sysino;
748
749 sysino = sun4v_devino_to_sysino(devhandle, devino);
750 data->sysino = sysino;
751}
752
753static unsigned int sysino_build_irq(u32 devhandle, unsigned int devino,
754 struct irq_chip *chip)
755{
756 unsigned int irq;
757
758 irq = sun4v_build_common(devhandle, devino, sysino_handler_data, chip);
759 if (!irq)
760 goto out;
761
762 sysino_set_bucket(irq);
763out:
764 return irq;
765}
766
767static int sun4v_build_sysino(u32 devhandle, unsigned int devino)
768{
769 int irq;
770
771 irq = sysino_exists(devhandle, devino);
772 if (irq)
773 goto out;
774
775 irq = sysino_build_irq(devhandle, devino, &sun4v_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700776out:
Sam Ravnborgfe414932011-01-22 11:32:19 +0000777 return irq;
David S. Millere3999572006-02-13 18:16:10 -0800778}
779
David S. Miller4a907de2007-06-13 00:01:04 -0700780unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
781{
Sam Ravnborgfe414932011-01-22 11:32:19 +0000782 unsigned int irq;
David S. Miller4a907de2007-06-13 00:01:04 -0700783
bob piccoee6a9332014-09-25 12:25:03 -0700784 if (sun4v_cookie_only_virqs())
785 irq = sun4v_build_cookie(devhandle, devino);
786 else
787 irq = sun4v_build_sysino(devhandle, devino);
David S. Miller4a907de2007-06-13 00:01:04 -0700788
Sam Ravnborgfe414932011-01-22 11:32:19 +0000789 return irq;
David S. Miller4a907de2007-06-13 00:01:04 -0700790}
791
bob piccoee6a9332014-09-25 12:25:03 -0700792unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
David S. Miller088dd1f2005-07-04 13:24:38 -0700793{
bob piccoee6a9332014-09-25 12:25:03 -0700794 int irq;
David S. Miller088dd1f2005-07-04 13:24:38 -0700795
bob piccoee6a9332014-09-25 12:25:03 -0700796 irq = cookie_build_irq(devhandle, devino, &sun4v_virq);
797 if (!irq)
798 goto out;
David S. Miller088dd1f2005-07-04 13:24:38 -0700799
bob piccoee6a9332014-09-25 12:25:03 -0700800 /* This is borrowed from the original function.
801 */
802 irq_set_status_flags(irq, IRQ_NOAUTOEN);
803
804out:
805 return irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
David S. Miller4f70f7a2008-08-12 18:33:56 -0700808void *hardirq_stack[NR_CPUS];
809void *softirq_stack[NR_CPUS];
810
Sam Ravnborgd4d1ec42011-01-22 11:32:15 +0000811void __irq_entry handler_irq(int pil, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
David S. Millereb2d8d62007-10-13 21:42:46 -0700813 unsigned long pstate, bucket_pa;
Al Viro6d24c8d2006-10-08 08:23:28 -0400814 struct pt_regs *old_regs;
David S. Miller4f70f7a2008-08-12 18:33:56 -0700815 void *orig_sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Sam Ravnborgd4d1ec42011-01-22 11:32:15 +0000817 clear_softint(1 << pil);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Al Viro6d24c8d2006-10-08 08:23:28 -0400819 old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
David S. Millera650d382007-10-12 02:59:40 -0700822 /* Grab an atomic snapshot of the pending IVECs. */
823 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
824 "wrpr %0, %3, %%pstate\n\t"
825 "ldx [%2], %1\n\t"
826 "stx %%g0, [%2]\n\t"
827 "wrpr %0, 0x0, %%pstate\n\t"
David S. Millereb2d8d62007-10-13 21:42:46 -0700828 : "=&r" (pstate), "=&r" (bucket_pa)
829 : "r" (irq_work_pa(smp_processor_id())),
David S. Millera650d382007-10-12 02:59:40 -0700830 "i" (PSTATE_IE)
831 : "memory");
832
David S. Miller4f70f7a2008-08-12 18:33:56 -0700833 orig_sp = set_hardirq_stack();
834
David S. Millereb2d8d62007-10-13 21:42:46 -0700835 while (bucket_pa) {
836 unsigned long next_pa;
Sam Ravnborgfe414932011-01-22 11:32:19 +0000837 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
David S. Miller42d5f992007-10-13 23:03:21 -0700839 next_pa = bucket_get_chain_pa(bucket_pa);
Sam Ravnborgfe414932011-01-22 11:32:19 +0000840 irq = bucket_get_irq(bucket_pa);
David S. Miller42d5f992007-10-13 23:03:21 -0700841 bucket_clear_chain_pa(bucket_pa);
David S. Millerfd0504c32006-06-20 01:20:00 -0700842
Thomas Gleixnerfcd8d4f2011-03-24 09:03:45 +0100843 generic_handle_irq(irq);
David S. Millereb2d8d62007-10-13 21:42:46 -0700844
845 bucket_pa = next_pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
David S. Millere18e2a02006-06-20 01:23:32 -0700847
David S. Miller4f70f7a2008-08-12 18:33:56 -0700848 restore_hardirq_stack(orig_sp);
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 irq_exit();
Al Viro6d24c8d2006-10-08 08:23:28 -0400851 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Sebastian Andrzej Siewior8cbb2b52022-08-25 10:25:05 +0200854#ifdef CONFIG_SOFTIRQ_ON_OWN_STACK
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200855void do_softirq_own_stack(void)
David S. Miller4f70f7a2008-08-12 18:33:56 -0700856{
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200857 void *orig_sp, *sp = softirq_stack[smp_processor_id()];
David S. Miller4f70f7a2008-08-12 18:33:56 -0700858
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200859 sp += THREAD_SIZE - 192 - STACK_BIAS;
David S. Miller4f70f7a2008-08-12 18:33:56 -0700860
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200861 __asm__ __volatile__("mov %%sp, %0\n\t"
862 "mov %1, %%sp"
863 : "=&r" (orig_sp)
864 : "r" (sp));
865 __do_softirq();
866 __asm__ __volatile__("mov %0, %%sp"
867 : : "r" (orig_sp));
David S. Miller4f70f7a2008-08-12 18:33:56 -0700868}
Sebastian Andrzej Siewiorf2c50922022-06-14 20:18:14 +0200869#endif
David S. Miller4f70f7a2008-08-12 18:33:56 -0700870
David S. Millere02044092007-07-16 03:49:40 -0700871#ifdef CONFIG_HOTPLUG_CPU
872void fixup_irqs(void)
873{
874 unsigned int irq;
875
876 for (irq = 0; irq < NR_IRQS; irq++) {
Thomas Gleixner16741ea2011-03-24 17:57:12 +0100877 struct irq_desc *desc = irq_to_desc(irq);
bob piccoee6a9332014-09-25 12:25:03 -0700878 struct irq_data *data;
David S. Millere02044092007-07-16 03:49:40 -0700879 unsigned long flags;
880
bob piccoee6a9332014-09-25 12:25:03 -0700881 if (!desc)
882 continue;
883 data = irq_desc_get_irq_data(desc);
Thomas Gleixner16741ea2011-03-24 17:57:12 +0100884 raw_spin_lock_irqsave(&desc->lock, flags);
885 if (desc->action && !irqd_is_per_cpu(data)) {
Sam Ravnborg4832b992011-01-22 11:32:18 +0000886 if (data->chip->irq_set_affinity)
887 data->chip->irq_set_affinity(data,
Jiang Liud7185a92015-06-01 16:05:35 +0800888 irq_data_get_affinity_mask(data),
889 false);
David S. Millere02044092007-07-16 03:49:40 -0700890 }
Thomas Gleixner16741ea2011-03-24 17:57:12 +0100891 raw_spin_unlock_irqrestore(&desc->lock, flags);
David S. Millere02044092007-07-16 03:49:40 -0700892 }
David S. Miller2eb2f772008-09-08 17:21:07 -0700893
894 tick_ops->disable_irq();
David S. Millere02044092007-07-16 03:49:40 -0700895}
896#endif
897
David S. Millercdd51862005-07-24 19:36:13 -0700898struct sun5_timer {
899 u64 count0;
900 u64 limit0;
901 u64 count1;
902 u64 limit1;
903};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
David S. Millercdd51862005-07-24 19:36:13 -0700905static struct sun5_timer *prom_timers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906static u64 prom_limit0, prom_limit1;
907
908static void map_prom_timers(void)
909{
David S. Miller25c75812006-06-22 20:21:22 -0700910 struct device_node *dp;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700911 const unsigned int *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /* PROM timer node hangs out in the top level of device siblings... */
David S. Miller25c75812006-06-22 20:21:22 -0700914 dp = of_find_node_by_path("/");
915 dp = dp->child;
916 while (dp) {
Rob Herring29c990d2018-11-16 15:06:58 -0600917 if (of_node_name_eq(dp, "counter-timer"))
David S. Miller25c75812006-06-22 20:21:22 -0700918 break;
919 dp = dp->sibling;
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 /* Assume if node is not present, PROM uses different tick mechanism
923 * which we should not care about.
924 */
David S. Miller25c75812006-06-22 20:21:22 -0700925 if (!dp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 prom_timers = (struct sun5_timer *) 0;
927 return;
928 }
929
930 /* If PROM is really using this, it must be mapped by him. */
David S. Miller25c75812006-06-22 20:21:22 -0700931 addr = of_get_property(dp, "address", NULL);
932 if (!addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 prom_printf("PROM does not have timer mapped, trying to continue.\n");
934 prom_timers = (struct sun5_timer *) 0;
935 return;
936 }
937 prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
938}
939
940static void kill_prom_timer(void)
941{
942 if (!prom_timers)
943 return;
944
945 /* Save them away for later. */
946 prom_limit0 = prom_timers->limit0;
947 prom_limit1 = prom_timers->limit1;
948
David S. Milleree906c92012-05-12 00:35:45 -0700949 /* Just as in sun4c PROM uses timer which ticks at IRQ 14.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * We turn both off here just to be paranoid.
951 */
952 prom_timers->limit0 = 0;
953 prom_timers->limit1 = 0;
954
955 /* Wheee, eat the interrupt packet too... */
956 __asm__ __volatile__(
957" mov 0x40, %%g2\n"
958" ldxa [%%g0] %0, %%g1\n"
959" ldxa [%%g2] %1, %%g1\n"
960" stxa %%g0, [%%g0] %0\n"
961" membar #Sync\n"
962 : /* no outputs */
963 : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
964 : "g1", "g2");
965}
966
David S. Miller98430992008-09-16 11:44:00 -0700967void notrace init_irqwork_curcpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 int cpu = hard_smp_processor_id();
970
David S. Millereb2d8d62007-10-13 21:42:46 -0700971 trap_block[cpu].irq_worklist_pa = 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
David S. Miller5cbc3072007-05-25 15:49:59 -0700974/* Please be very careful with register_one_mondo() and
975 * sun4v_register_mondo_queues().
976 *
977 * On SMP this gets invoked from the CPU trampoline before
978 * the cpu has fully taken over the trap table from OBP,
Bjorn Helgaas3cc208f2024-01-03 17:16:05 -0600979 * and its kernel stack + %g6 thread register state is
David S. Miller5cbc3072007-05-25 15:49:59 -0700980 * not fully cooked yet.
981 *
982 * Therefore you cannot make any OBP calls, not even prom_printf,
983 * from these two routines.
984 */
Paul Gortmaker2066aad2013-06-17 15:43:14 -0400985static void notrace register_one_mondo(unsigned long paddr, unsigned long type,
986 unsigned long qmask)
David S. Millerac29c112006-02-08 00:08:23 -0800987{
David S. Miller5cbc3072007-05-25 15:49:59 -0700988 unsigned long num_entries = (qmask + 1) / 64;
David S. Miller94f87622006-02-16 14:26:53 -0800989 unsigned long status;
David S. Millerac29c112006-02-08 00:08:23 -0800990
David S. Miller94f87622006-02-16 14:26:53 -0800991 status = sun4v_cpu_qconf(type, paddr, num_entries);
992 if (status != HV_EOK) {
993 prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
994 "err %lu\n", type, paddr, num_entries, status);
David S. Millerac29c112006-02-08 00:08:23 -0800995 prom_halt();
996 }
997}
998
Paul Gortmaker2066aad2013-06-17 15:43:14 -0400999void notrace sun4v_register_mondo_queues(int this_cpu)
David S. Miller5b0c05722006-02-08 02:53:50 -08001000{
David S. Millerb5a37e92006-02-11 23:07:13 -08001001 struct trap_per_cpu *tb = &trap_block[this_cpu];
1002
David S. Miller5cbc3072007-05-25 15:49:59 -07001003 register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
1004 tb->cpu_mondo_qmask);
1005 register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
1006 tb->dev_mondo_qmask);
1007 register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
1008 tb->resum_qmask);
1009 register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
1010 tb->nonresum_qmask);
David S. Millerb5a37e92006-02-11 23:07:13 -08001011}
1012
David S. Miller14a2ff62009-06-25 19:00:47 -07001013/* Each queue region must be a power of 2 multiple of 64 bytes in
1014 * size. The base real address must be aligned to the size of the
1015 * region. Thus, an 8KB queue must be 8KB aligned, for example.
1016 */
1017static void __init alloc_one_queue(unsigned long *pa_ptr, unsigned long qmask)
David S. Millerb5a37e92006-02-11 23:07:13 -08001018{
David S. Miller5cbc3072007-05-25 15:49:59 -07001019 unsigned long size = PAGE_ALIGN(qmask + 1);
David S. Miller14a2ff62009-06-25 19:00:47 -07001020 unsigned long order = get_order(size);
1021 unsigned long p;
1022
Liam R. Howlett7a7dc962017-01-17 10:59:02 -05001023 p = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
David S. Miller5cbc3072007-05-25 15:49:59 -07001024 if (!p) {
David S. Miller14a2ff62009-06-25 19:00:47 -07001025 prom_printf("SUN4V: Error, cannot allocate queue.\n");
David S. Miller5b0c05722006-02-08 02:53:50 -08001026 prom_halt();
1027 }
1028
David S. Miller5cbc3072007-05-25 15:49:59 -07001029 *pa_ptr = __pa(p);
David S. Miller5b0c05722006-02-08 02:53:50 -08001030}
1031
David S. Millerb434e712007-08-08 17:32:33 -07001032static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
David S. Miller1d2f1f92006-02-08 16:41:20 -08001033{
1034#ifdef CONFIG_SMP
David S. Miller14a2ff62009-06-25 19:00:47 -07001035 unsigned long page;
Jane Chuc79a1372017-06-06 14:32:29 -06001036 void *mondo, *p;
David S. Miller1d2f1f92006-02-08 16:41:20 -08001037
Jane Chuc79a1372017-06-06 14:32:29 -06001038 BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > PAGE_SIZE);
1039
1040 /* Make sure mondo block is 64byte aligned */
1041 p = kzalloc(127, GFP_KERNEL);
1042 if (!p) {
1043 prom_printf("SUN4V: Error, cannot allocate mondo block.\n");
1044 prom_halt();
1045 }
1046 mondo = (void *)(((unsigned long)p + 63) & ~0x3f);
1047 tb->cpu_mondo_block_pa = __pa(mondo);
David S. Miller1d2f1f92006-02-08 16:41:20 -08001048
David S. Miller14a2ff62009-06-25 19:00:47 -07001049 page = get_zeroed_page(GFP_KERNEL);
David S. Miller1d2f1f92006-02-08 16:41:20 -08001050 if (!page) {
Jane Chuc79a1372017-06-06 14:32:29 -06001051 prom_printf("SUN4V: Error, cannot allocate cpu list page.\n");
David S. Miller1d2f1f92006-02-08 16:41:20 -08001052 prom_halt();
1053 }
1054
Jane Chuc79a1372017-06-06 14:32:29 -06001055 tb->cpu_list_pa = __pa(page);
David S. Miller1d2f1f92006-02-08 16:41:20 -08001056#endif
1057}
1058
David S. Millerb434e712007-08-08 17:32:33 -07001059/* Allocate mondo and error queues for all possible cpus. */
1060static void __init sun4v_init_mondo_queues(void)
David S. Millerac29c112006-02-08 00:08:23 -08001061{
David S. Millerb434e712007-08-08 17:32:33 -07001062 int cpu;
David S. Millerac29c112006-02-08 00:08:23 -08001063
David S. Millerb434e712007-08-08 17:32:33 -07001064 for_each_possible_cpu(cpu) {
1065 struct trap_per_cpu *tb = &trap_block[cpu];
David S. Miller1d2f1f92006-02-08 16:41:20 -08001066
David S. Miller14a2ff62009-06-25 19:00:47 -07001067 alloc_one_queue(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
1068 alloc_one_queue(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
1069 alloc_one_queue(&tb->resum_mondo_pa, tb->resum_qmask);
1070 alloc_one_queue(&tb->resum_kernel_buf_pa, tb->resum_qmask);
1071 alloc_one_queue(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
1072 alloc_one_queue(&tb->nonresum_kernel_buf_pa,
1073 tb->nonresum_qmask);
David S. Miller43f58922008-08-04 16:13:51 -07001074 }
1075}
1076
1077static void __init init_send_mondo_info(void)
1078{
1079 int cpu;
1080
1081 for_each_possible_cpu(cpu) {
1082 struct trap_per_cpu *tb = &trap_block[cpu];
David S. Millerb434e712007-08-08 17:32:33 -07001083
1084 init_cpu_send_mondo_info(tb);
David S. Miller72aff532006-02-17 01:29:17 -08001085 }
David S. Millerac29c112006-02-08 00:08:23 -08001086}
1087
David S. Millere18e2a02006-06-20 01:23:32 -07001088static struct irqaction timer_irq_action = {
1089 .name = "timer",
1090};
1091
bob piccoee6a9332014-09-25 12:25:03 -07001092static void __init irq_ivector_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
bob piccoee6a9332014-09-25 12:25:03 -07001094 unsigned long size, order;
1095 unsigned int ivecs;
David S. Miller10397e42007-10-13 21:43:31 -07001096
bob piccoee6a9332014-09-25 12:25:03 -07001097 /* If we are doing cookie only VIRQs then we do not need the ivector
1098 * table to process interrupts.
1099 */
1100 if (sun4v_cookie_only_virqs())
1101 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
bob piccoee6a9332014-09-25 12:25:03 -07001103 ivecs = size_nr_ivec();
1104 size = sizeof(struct ino_bucket) * ivecs;
1105 order = get_order(size);
1106 ivector_table = (struct ino_bucket *)
1107 __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
David S. Miller10397e42007-10-13 21:43:31 -07001108 if (!ivector_table) {
1109 prom_printf("Fatal error, cannot allocate ivector_table\n");
1110 prom_halt();
1111 }
David S. Miller42d5f992007-10-13 23:03:21 -07001112 __flush_dcache_range((unsigned long) ivector_table,
1113 ((unsigned long) ivector_table) + size);
David S. Miller10397e42007-10-13 21:43:31 -07001114
1115 ivector_table_pa = __pa(ivector_table);
bob piccoee6a9332014-09-25 12:25:03 -07001116}
1117
1118/* Only invoked on boot processor.*/
1119void __init init_IRQ(void)
1120{
1121 irq_init_hv();
1122 irq_ivector_init();
1123 map_prom_timers();
1124 kill_prom_timer();
David S. Millereb2d8d62007-10-13 21:42:46 -07001125
David S. Millerac29c112006-02-08 00:08:23 -08001126 if (tlb_type == hypervisor)
David S. Millerb434e712007-08-08 17:32:33 -07001127 sun4v_init_mondo_queues();
David S. Millerac29c112006-02-08 00:08:23 -08001128
David S. Miller43f58922008-08-04 16:13:51 -07001129 init_send_mondo_info();
1130
1131 if (tlb_type == hypervisor) {
1132 /* Load up the boot cpu's entries. */
1133 sun4v_register_mondo_queues(hard_smp_processor_id());
1134 }
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /* We need to clear any IRQ's pending in the soft interrupt
1137 * registers, a spurious one could be left around from the
1138 * PROM timer which we just disabled.
1139 */
1140 clear_softint(get_softint());
1141
1142 /* Now that ivector table is initialized, it is safe
1143 * to receive IRQ vector traps. We will normally take
1144 * one or two right now, in case some device PROM used
1145 * to boot us wants to speak to us. We just ignore them.
1146 */
1147 __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
1148 "or %%g1, %0, %%g1\n\t"
1149 "wrpr %%g1, 0x0, %%pstate"
1150 : /* No outputs */
1151 : "i" (PSTATE_IE)
1152 : "g1");
David S. Millere18e2a02006-06-20 01:23:32 -07001153
Thomas Gleixner16741ea2011-03-24 17:57:12 +01001154 irq_to_desc(0)->action = &timer_irq_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}