2 * Copyright 2011 IBM Corporation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/types.h>
11 #include <linux/threads.h>
12 #include <linux/kernel.h>
13 #include <linux/irq.h>
14 #include <linux/debugfs.h>
15 #include <linux/smp.h>
16 #include <linux/interrupt.h>
17 #include <linux/seq_file.h>
18 #include <linux/init.h>
19 #include <linux/cpu.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
27 #include <asm/machdep.h>
29 #include <asm/errno.h>
32 #include <asm/firmware.h>
34 /* Globals common to all ICP/ICS implementations */
35 const struct icp_ops
*icp_ops
;
37 unsigned int xics_default_server
= 0xff;
38 unsigned int xics_default_distrib_server
= 0;
39 unsigned int xics_interrupt_server_size
= 8;
41 DEFINE_PER_CPU(struct xics_cppr
, xics_cppr
);
43 struct irq_domain
*xics_host
;
45 static LIST_HEAD(ics_list
);
47 void xics_update_irq_servers(void)
50 struct device_node
*np
;
55 /* Find the server numbers for the boot cpu. */
56 np
= of_get_cpu_node(boot_cpuid
, NULL
);
59 hcpuid
= get_hard_smp_processor_id(boot_cpuid
);
60 xics_default_server
= xics_default_distrib_server
= hcpuid
;
62 pr_devel("xics: xics_default_server = 0x%x\n", xics_default_server
);
64 ireg
= of_get_property(np
, "ibm,ppc-interrupt-gserver#s", &ilen
);
70 i
= ilen
/ sizeof(int);
72 /* Global interrupt distribution server is specified in the last
73 * entry of "ibm,ppc-interrupt-gserver#s" property. Get the last
74 * entry fom this property for current boot cpu id and use it as
75 * default distribution server
77 for (j
= 0; j
< i
; j
+= 2) {
78 if (be32_to_cpu(ireg
[j
]) == hcpuid
) {
79 xics_default_distrib_server
= be32_to_cpu(ireg
[j
+1]);
83 pr_devel("xics: xics_default_distrib_server = 0x%x\n",
84 xics_default_distrib_server
);
88 /* GIQ stuff, currently only supported on RTAS setups, will have
89 * to be sorted properly for bare metal
91 void xics_set_cpu_giq(unsigned int gserver
, unsigned int join
)
93 #ifdef CONFIG_PPC_RTAS
97 if (!rtas_indicator_present(GLOBAL_INTERRUPT_QUEUE
, NULL
))
100 index
= (1UL << xics_interrupt_server_size
) - 1 - gserver
;
102 status
= rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE
, index
, join
);
104 WARN(status
< 0, "set-indicator(%d, %d, %u) returned %d\n",
105 GLOBAL_INTERRUPT_QUEUE
, index
, join
, status
);
109 void xics_setup_cpu(void)
111 icp_ops
->set_priority(LOWEST_PRIORITY
);
113 xics_set_cpu_giq(xics_default_distrib_server
, 1);
116 void xics_mask_unknown_vec(unsigned int vec
)
120 pr_err("Interrupt 0x%x (real) is invalid, disabling it.\n", vec
);
122 list_for_each_entry(ics
, &ics_list
, link
)
123 ics
->mask_unknown(ics
, vec
);
129 static void xics_request_ipi(void)
133 ipi
= irq_create_mapping(xics_host
, XICS_IPI
);
134 BUG_ON(ipi
== NO_IRQ
);
137 * IPIs are marked IRQF_PERCPU. The handler was set in map.
139 BUG_ON(request_irq(ipi
, icp_ops
->ipi_action
,
140 IRQF_PERCPU
| IRQF_NO_THREAD
, "IPI", NULL
));
143 void __init
xics_smp_probe(void)
145 /* Setup cause_ipi callback based on which ICP is used */
146 smp_ops
->cause_ipi
= icp_ops
->cause_ipi
;
148 /* Register all the IPIs */
152 #endif /* CONFIG_SMP */
154 void xics_teardown_cpu(void)
156 struct xics_cppr
*os_cppr
= this_cpu_ptr(&xics_cppr
);
159 * we have to reset the cppr index to 0 because we're
160 * not going to return from the IPI
163 icp_ops
->set_priority(0);
164 icp_ops
->teardown_cpu();
167 void xics_kexec_teardown_cpu(int secondary
)
171 icp_ops
->flush_ipi();
174 * Some machines need to have at least one cpu in the GIQ,
175 * so leave the master cpu in the group.
178 xics_set_cpu_giq(xics_default_distrib_server
, 0);
182 #ifdef CONFIG_HOTPLUG_CPU
184 /* Interrupts are disabled. */
185 void xics_migrate_irqs_away(void)
187 int cpu
= smp_processor_id(), hw_cpu
= hard_smp_processor_id();
188 unsigned int irq
, virq
;
189 struct irq_desc
*desc
;
191 /* If we used to be the default server, move to the new "boot_cpuid" */
192 if (hw_cpu
== xics_default_server
)
193 xics_update_irq_servers();
195 /* Reject any interrupt that was queued to us... */
196 icp_ops
->set_priority(0);
198 /* Remove ourselves from the global interrupt queue */
199 xics_set_cpu_giq(xics_default_distrib_server
, 0);
201 /* Allow IPIs again... */
202 icp_ops
->set_priority(DEFAULT_PRIORITY
);
204 for_each_irq_desc(virq
, desc
) {
205 struct irq_chip
*chip
;
210 /* We can't set affinity on ISA interrupts */
211 if (virq
< NUM_ISA_INTERRUPTS
)
213 /* We only need to migrate enabled IRQS */
216 if (desc
->irq_data
.domain
!= xics_host
)
218 irq
= desc
->irq_data
.hwirq
;
219 /* We need to get IPIs still. */
220 if (irq
== XICS_IPI
|| irq
== XICS_IRQ_SPURIOUS
)
222 chip
= irq_desc_get_chip(desc
);
223 if (!chip
|| !chip
->irq_set_affinity
)
226 raw_spin_lock_irqsave(&desc
->lock
, flags
);
228 /* Locate interrupt server */
230 ics
= irq_desc_get_chip_data(desc
);
232 server
= ics
->get_server(ics
, irq
);
234 printk(KERN_ERR
"%s: Can't find server for irq %d\n",
239 /* We only support delivery to all cpus or to one cpu.
240 * The irq has to be migrated only in the single cpu
243 if (server
!= hw_cpu
)
246 /* This is expected during cpu offline. */
248 pr_warning("IRQ %u affinity broken off cpu %u\n",
251 /* Reset affinity to all cpus */
252 raw_spin_unlock_irqrestore(&desc
->lock
, flags
);
253 irq_set_affinity(virq
, cpu_all_mask
);
256 raw_spin_unlock_irqrestore(&desc
->lock
, flags
);
259 #endif /* CONFIG_HOTPLUG_CPU */
263 * For the moment we only implement delivery to all cpus or one cpu.
265 * If the requested affinity is cpu_all_mask, we set global affinity.
266 * If not we set it to the first cpu in the mask, even if multiple cpus
267 * are set. This is so things like irqbalance (which set core and package
268 * wide affinities) do the right thing.
270 * We need to fix this to implement support for the links
272 int xics_get_irq_server(unsigned int virq
, const struct cpumask
*cpumask
,
273 unsigned int strict_check
)
276 if (!distribute_irqs
)
277 return xics_default_server
;
279 if (!cpumask_subset(cpu_possible_mask
, cpumask
)) {
280 int server
= cpumask_first_and(cpu_online_mask
, cpumask
);
282 if (server
< nr_cpu_ids
)
283 return get_hard_smp_processor_id(server
);
290 * Workaround issue with some versions of JS20 firmware that
291 * deliver interrupts to cpus which haven't been started. This
292 * happens when using the maxcpus= boot option.
294 if (cpumask_equal(cpu_online_mask
, cpu_present_mask
))
295 return xics_default_distrib_server
;
297 return xics_default_server
;
299 #endif /* CONFIG_SMP */
301 static int xics_host_match(struct irq_domain
*h
, struct device_node
*node
,
302 enum irq_domain_bus_token bus_token
)
306 list_for_each_entry(ics
, &ics_list
, link
)
307 if (ics
->host_match(ics
, node
))
314 static void xics_ipi_unmask(struct irq_data
*d
) { }
315 static void xics_ipi_mask(struct irq_data
*d
) { }
317 static struct irq_chip xics_ipi_chip
= {
319 .irq_eoi
= NULL
, /* Patched at init time */
320 .irq_mask
= xics_ipi_mask
,
321 .irq_unmask
= xics_ipi_unmask
,
324 static int xics_host_map(struct irq_domain
*h
, unsigned int virq
,
329 pr_devel("xics: map virq %d, hwirq 0x%lx\n", virq
, hw
);
331 /* They aren't all level sensitive but we just don't really know */
332 irq_set_status_flags(virq
, IRQ_LEVEL
);
334 /* Don't call into ICS for IPIs */
335 if (hw
== XICS_IPI
) {
336 irq_set_chip_and_handler(virq
, &xics_ipi_chip
,
341 /* Let the ICS setup the chip data */
342 list_for_each_entry(ics
, &ics_list
, link
)
343 if (ics
->map(ics
, virq
) == 0)
349 static int xics_host_xlate(struct irq_domain
*h
, struct device_node
*ct
,
350 const u32
*intspec
, unsigned int intsize
,
351 irq_hw_number_t
*out_hwirq
, unsigned int *out_flags
)
354 /* Current xics implementation translates everything
355 * to level. It is not technically right for MSIs but this
356 * is irrelevant at this point. We might get smarter in the future
358 *out_hwirq
= intspec
[0];
359 *out_flags
= IRQ_TYPE_LEVEL_LOW
;
364 static const struct irq_domain_ops xics_host_ops
= {
365 .match
= xics_host_match
,
366 .map
= xics_host_map
,
367 .xlate
= xics_host_xlate
,
370 static void __init
xics_init_host(void)
372 xics_host
= irq_domain_add_tree(NULL
, &xics_host_ops
, NULL
);
373 BUG_ON(xics_host
== NULL
);
374 irq_set_default_host(xics_host
);
377 void __init
xics_register_ics(struct ics
*ics
)
379 list_add(&ics
->link
, &ics_list
);
382 static void __init
xics_get_server_size(void)
384 struct device_node
*np
;
387 /* We fetch the interrupt server size from the first ICS node
390 np
= of_find_compatible_node(NULL
, NULL
, "ibm,ppc-xics");
393 isize
= of_get_property(np
, "ibm,interrupt-server#-size", NULL
);
396 xics_interrupt_server_size
= be32_to_cpu(*isize
);
400 void __init
xics_init(void)
404 /* Fist locate ICP */
405 if (firmware_has_feature(FW_FEATURE_LPAR
))
408 rc
= icp_native_init();
410 pr_warning("XICS: Cannot find a Presentation Controller !\n");
414 /* Copy get_irq callback over to ppc_md */
415 ppc_md
.get_irq
= icp_ops
->get_irq
;
417 /* Patch up IPI chip EOI */
418 xics_ipi_chip
.irq_eoi
= icp_ops
->eoi
;
421 rc
= ics_rtas_init();
423 rc
= ics_opal_init();
425 pr_warning("XICS: Cannot find a Source Controller !\n");
427 /* Initialize common bits */
428 xics_get_server_size();
429 xics_update_irq_servers();