1 // SPDX-License-Identifier: GPL-2.0-only
3 * PS3 interrupt routines.
5 * Copyright (C) 2006 Sony Computer Entertainment Inc.
6 * Copyright 2006 Sony Corp.
9 #include <linux/kernel.h>
10 #include <linux/export.h>
11 #include <linux/irq.h>
12 #include <linux/irqdomain.h>
14 #include <asm/machdep.h>
16 #include <asm/lv1call.h>
22 #define DBG udbg_printf
23 #define FAIL udbg_printf
30 * struct ps3_bmp - a per cpu irq status and mask bitmap structure
31 * @status: 256 bit status bitmap indexed by plug
32 * @unused_1: Alignment
33 * @mask: 256 bit mask bitmap indexed by plug
34 * @unused_2: Alignment
36 * The HV maintains per SMT thread mappings of HV outlet to HV plug on
37 * behalf of the guest. These mappings are implemented as 256 bit guest
38 * supplied bitmaps indexed by plug number. The addresses of the bitmaps
39 * are registered with the HV through lv1_configure_irq_state_bitmap().
40 * The HV requires that the 512 bits of status + mask not cross a page
41 * boundary. PS3_BMP_MINALIGN is used to define this minimal 64 byte
44 * The HV supports 256 plugs per thread, assigned as {0..255}, for a total
45 * of 512 plugs supported on a processor. To simplify the logic this
46 * implementation equates HV plug value to Linux virq value, constrains each
47 * interrupt to have a system wide unique plug number, and limits the range
48 * of the plug values to map into the first dword of the bitmaps. This
49 * gives a usable range of plug values of {NR_IRQS_LEGACY..63}. Note
50 * that there is no constraint on how many in this set an individual thread
53 * The mask is declared as unsigned long so we can use set/clear_bit on it.
56 #define PS3_BMP_MINALIGN 64
68 * struct ps3_private - a per cpu data structure
69 * @bmp: ps3_bmp structure
70 * @bmp_lock: Synchronize access to bmp.
71 * @ipi_debug_brk_mask: Mask for debug break IPIs
72 * @ppe_id: HV logical_ppe_id
73 * @thread_id: HV thread_id
74 * @ipi_mask: Mask of IPI virqs
78 struct ps3_bmp bmp __attribute__ ((aligned (PS3_BMP_MINALIGN)));
82 unsigned long ipi_debug_brk_mask;
83 unsigned long ipi_mask;
86 static DEFINE_PER_CPU(struct ps3_private, ps3_private);
89 * ps3_chip_mask - Set an interrupt mask bit in ps3_bmp.
90 * @virq: The assigned Linux virq.
92 * Sets ps3_bmp.mask and calls lv1_did_update_interrupt_mask().
95 static void ps3_chip_mask(struct irq_data *d)
97 struct ps3_private *pd = irq_data_get_irq_chip_data(d);
100 DBG("%s:%d: thread_id %llu, virq %d\n", __func__, __LINE__,
101 pd->thread_id, d->irq);
103 local_irq_save(flags);
104 clear_bit(63 - d->irq, &pd->bmp.mask);
105 lv1_did_update_interrupt_mask(pd->ppe_id, pd->thread_id);
106 local_irq_restore(flags);
110 * ps3_chip_unmask - Clear an interrupt mask bit in ps3_bmp.
111 * @virq: The assigned Linux virq.
113 * Clears ps3_bmp.mask and calls lv1_did_update_interrupt_mask().
116 static void ps3_chip_unmask(struct irq_data *d)
118 struct ps3_private *pd = irq_data_get_irq_chip_data(d);
121 DBG("%s:%d: thread_id %llu, virq %d\n", __func__, __LINE__,
122 pd->thread_id, d->irq);
124 local_irq_save(flags);
125 set_bit(63 - d->irq, &pd->bmp.mask);
126 lv1_did_update_interrupt_mask(pd->ppe_id, pd->thread_id);
127 local_irq_restore(flags);
131 * ps3_chip_eoi - HV end-of-interrupt.
132 * @virq: The assigned Linux virq.
134 * Calls lv1_end_of_interrupt_ext().
137 static void ps3_chip_eoi(struct irq_data *d)
139 const struct ps3_private *pd = irq_data_get_irq_chip_data(d);
141 /* non-IPIs are EOIed here. */
143 if (!test_bit(63 - d->irq, &pd->ipi_mask))
144 lv1_end_of_interrupt_ext(pd->ppe_id, pd->thread_id, d->irq);
148 * ps3_irq_chip - Represents the ps3_bmp as a Linux struct irq_chip.
151 static struct irq_chip ps3_irq_chip = {
153 .irq_mask = ps3_chip_mask,
154 .irq_unmask = ps3_chip_unmask,
155 .irq_eoi = ps3_chip_eoi,
159 * ps3_virq_setup - virq related setup.
160 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
162 * @outlet: The HV outlet from the various create outlet routines.
163 * @virq: The assigned Linux virq.
165 * Calls irq_create_mapping() to get a virq and sets the chip data to
169 static int ps3_virq_setup(enum ps3_cpu_binding cpu, unsigned long outlet,
173 struct ps3_private *pd;
175 /* This defines the default interrupt distribution policy. */
177 if (cpu == PS3_BINDING_CPU_ANY)
180 pd = &per_cpu(ps3_private, cpu);
182 *virq = irq_create_mapping(NULL, outlet);
185 FAIL("%s:%d: irq_create_mapping failed: outlet %lu\n",
186 __func__, __LINE__, outlet);
191 DBG("%s:%d: outlet %lu => cpu %u, virq %u\n", __func__, __LINE__,
194 result = irq_set_chip_data(*virq, pd);
197 FAIL("%s:%d: irq_set_chip_data failed\n",
202 ps3_chip_mask(irq_get_irq_data(*virq));
207 irq_dispose_mapping(*virq);
213 * ps3_virq_destroy - virq related teardown.
214 * @virq: The assigned Linux virq.
216 * Clears chip data and calls irq_dispose_mapping() for the virq.
219 static int ps3_virq_destroy(unsigned int virq)
221 const struct ps3_private *pd = irq_get_chip_data(virq);
223 DBG("%s:%d: ppe_id %llu, thread_id %llu, virq %u\n", __func__,
224 __LINE__, pd->ppe_id, pd->thread_id, virq);
226 irq_set_chip_data(virq, NULL);
227 irq_dispose_mapping(virq);
229 DBG("%s:%d <-\n", __func__, __LINE__);
234 * ps3_irq_plug_setup - Generic outlet and virq related setup.
235 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
237 * @outlet: The HV outlet from the various create outlet routines.
238 * @virq: The assigned Linux virq.
240 * Sets up virq and connects the irq plug.
243 int ps3_irq_plug_setup(enum ps3_cpu_binding cpu, unsigned long outlet,
247 struct ps3_private *pd;
249 result = ps3_virq_setup(cpu, outlet, virq);
252 FAIL("%s:%d: ps3_virq_setup failed\n", __func__, __LINE__);
256 pd = irq_get_chip_data(*virq);
258 /* Binds outlet to cpu + virq. */
260 result = lv1_connect_irq_plug_ext(pd->ppe_id, pd->thread_id, *virq,
264 FAIL("%s:%d: lv1_connect_irq_plug_ext failed: %s\n",
265 __func__, __LINE__, ps3_result(result));
273 ps3_virq_destroy(*virq);
277 EXPORT_SYMBOL_GPL(ps3_irq_plug_setup);
280 * ps3_irq_plug_destroy - Generic outlet and virq related teardown.
281 * @virq: The assigned Linux virq.
283 * Disconnects the irq plug and tears down virq.
284 * Do not call for system bus event interrupts setup with
285 * ps3_sb_event_receive_port_setup().
288 int ps3_irq_plug_destroy(unsigned int virq)
291 const struct ps3_private *pd = irq_get_chip_data(virq);
293 DBG("%s:%d: ppe_id %llu, thread_id %llu, virq %u\n", __func__,
294 __LINE__, pd->ppe_id, pd->thread_id, virq);
296 ps3_chip_mask(irq_get_irq_data(virq));
298 result = lv1_disconnect_irq_plug_ext(pd->ppe_id, pd->thread_id, virq);
301 FAIL("%s:%d: lv1_disconnect_irq_plug_ext failed: %s\n",
302 __func__, __LINE__, ps3_result(result));
304 ps3_virq_destroy(virq);
308 EXPORT_SYMBOL_GPL(ps3_irq_plug_destroy);
311 * ps3_event_receive_port_setup - Setup an event receive port.
312 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
314 * @virq: The assigned Linux virq.
316 * The virq can be used with lv1_connect_interrupt_event_receive_port() to
317 * arrange to receive interrupts from system-bus devices, or with
318 * ps3_send_event_locally() to signal events.
321 int ps3_event_receive_port_setup(enum ps3_cpu_binding cpu, unsigned int *virq)
326 result = lv1_construct_event_receive_port(&outlet);
329 FAIL("%s:%d: lv1_construct_event_receive_port failed: %s\n",
330 __func__, __LINE__, ps3_result(result));
335 result = ps3_irq_plug_setup(cpu, outlet, virq);
340 EXPORT_SYMBOL_GPL(ps3_event_receive_port_setup);
343 * ps3_event_receive_port_destroy - Destroy an event receive port.
344 * @virq: The assigned Linux virq.
346 * Since ps3_event_receive_port_destroy destroys the receive port outlet,
347 * SB devices need to call disconnect_interrupt_event_receive_port() before
351 int ps3_event_receive_port_destroy(unsigned int virq)
355 DBG(" -> %s:%d virq %u\n", __func__, __LINE__, virq);
357 ps3_chip_mask(irq_get_irq_data(virq));
359 result = lv1_destruct_event_receive_port(virq_to_hw(virq));
362 FAIL("%s:%d: lv1_destruct_event_receive_port failed: %s\n",
363 __func__, __LINE__, ps3_result(result));
366 * Don't call ps3_virq_destroy() here since ps3_smp_cleanup_cpu()
367 * calls from interrupt context (smp_call_function) when kexecing.
370 DBG(" <- %s:%d\n", __func__, __LINE__);
374 int ps3_send_event_locally(unsigned int virq)
376 return lv1_send_event_locally(virq_to_hw(virq));
380 * ps3_sb_event_receive_port_setup - Setup a system bus event receive port.
381 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
383 * @dev: The system bus device instance.
384 * @virq: The assigned Linux virq.
386 * An event irq represents a virtual device interrupt. The interrupt_id
387 * coresponds to the software interrupt number.
390 int ps3_sb_event_receive_port_setup(struct ps3_system_bus_device *dev,
391 enum ps3_cpu_binding cpu, unsigned int *virq)
393 /* this should go in system-bus.c */
397 result = ps3_event_receive_port_setup(cpu, virq);
402 result = lv1_connect_interrupt_event_receive_port(dev->bus_id,
403 dev->dev_id, virq_to_hw(*virq), dev->interrupt_id);
406 FAIL("%s:%d: lv1_connect_interrupt_event_receive_port"
407 " failed: %s\n", __func__, __LINE__,
409 ps3_event_receive_port_destroy(*virq);
414 DBG("%s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
415 dev->interrupt_id, *virq);
419 EXPORT_SYMBOL(ps3_sb_event_receive_port_setup);
421 int ps3_sb_event_receive_port_destroy(struct ps3_system_bus_device *dev,
424 /* this should go in system-bus.c */
428 DBG(" -> %s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
429 dev->interrupt_id, virq);
431 result = lv1_disconnect_interrupt_event_receive_port(dev->bus_id,
432 dev->dev_id, virq_to_hw(virq), dev->interrupt_id);
435 FAIL("%s:%d: lv1_disconnect_interrupt_event_receive_port"
436 " failed: %s\n", __func__, __LINE__,
439 result = ps3_event_receive_port_destroy(virq);
443 * ps3_event_receive_port_destroy() destroys the IRQ plug,
444 * so don't call ps3_irq_plug_destroy() here.
447 result = ps3_virq_destroy(virq);
450 DBG(" <- %s:%d\n", __func__, __LINE__);
453 EXPORT_SYMBOL(ps3_sb_event_receive_port_destroy);
456 * ps3_io_irq_setup - Setup a system bus io irq.
457 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
459 * @interrupt_id: The device interrupt id read from the system repository.
460 * @virq: The assigned Linux virq.
462 * An io irq represents a non-virtualized device interrupt. interrupt_id
463 * coresponds to the interrupt number of the interrupt controller.
466 int ps3_io_irq_setup(enum ps3_cpu_binding cpu, unsigned int interrupt_id,
472 result = lv1_construct_io_irq_outlet(interrupt_id, &outlet);
475 FAIL("%s:%d: lv1_construct_io_irq_outlet failed: %s\n",
476 __func__, __LINE__, ps3_result(result));
480 result = ps3_irq_plug_setup(cpu, outlet, virq);
485 EXPORT_SYMBOL_GPL(ps3_io_irq_setup);
487 int ps3_io_irq_destroy(unsigned int virq)
490 unsigned long outlet = virq_to_hw(virq);
492 ps3_chip_mask(irq_get_irq_data(virq));
495 * lv1_destruct_io_irq_outlet() will destroy the IRQ plug,
496 * so call ps3_irq_plug_destroy() first.
499 result = ps3_irq_plug_destroy(virq);
502 result = lv1_destruct_io_irq_outlet(outlet);
505 FAIL("%s:%d: lv1_destruct_io_irq_outlet failed: %s\n",
506 __func__, __LINE__, ps3_result(result));
510 EXPORT_SYMBOL_GPL(ps3_io_irq_destroy);
513 * ps3_vuart_irq_setup - Setup the system virtual uart virq.
514 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
516 * @virt_addr_bmp: The caller supplied virtual uart interrupt bitmap.
517 * @virq: The assigned Linux virq.
519 * The system supports only a single virtual uart, so multiple calls without
520 * freeing the interrupt will return a wrong state error.
523 int ps3_vuart_irq_setup(enum ps3_cpu_binding cpu, void* virt_addr_bmp,
530 BUG_ON(!is_kernel_addr((u64)virt_addr_bmp));
532 lpar_addr = ps3_mm_phys_to_lpar(__pa(virt_addr_bmp));
534 result = lv1_configure_virtual_uart_irq(lpar_addr, &outlet);
537 FAIL("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
538 __func__, __LINE__, ps3_result(result));
542 result = ps3_irq_plug_setup(cpu, outlet, virq);
547 EXPORT_SYMBOL_GPL(ps3_vuart_irq_setup);
549 int ps3_vuart_irq_destroy(unsigned int virq)
553 ps3_chip_mask(irq_get_irq_data(virq));
554 result = lv1_deconfigure_virtual_uart_irq();
557 FAIL("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
558 __func__, __LINE__, ps3_result(result));
562 result = ps3_irq_plug_destroy(virq);
567 EXPORT_SYMBOL_GPL(ps3_vuart_irq_destroy);
570 * ps3_spe_irq_setup - Setup an spe virq.
571 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
573 * @spe_id: The spe_id returned from lv1_construct_logical_spe().
574 * @class: The spe interrupt class {0,1,2}.
575 * @virq: The assigned Linux virq.
579 int ps3_spe_irq_setup(enum ps3_cpu_binding cpu, unsigned long spe_id,
580 unsigned int class, unsigned int *virq)
587 result = lv1_get_spe_irq_outlet(spe_id, class, &outlet);
590 FAIL("%s:%d: lv1_get_spe_irq_outlet failed: %s\n",
591 __func__, __LINE__, ps3_result(result));
595 result = ps3_irq_plug_setup(cpu, outlet, virq);
601 int ps3_spe_irq_destroy(unsigned int virq)
605 ps3_chip_mask(irq_get_irq_data(virq));
607 result = ps3_irq_plug_destroy(virq);
614 #define PS3_INVALID_OUTLET ((irq_hw_number_t)-1)
615 #define PS3_PLUG_MAX 63
618 static void _dump_64_bmp(const char *header, const u64 *p, unsigned cpu,
619 const char* func, int line)
621 pr_debug("%s:%d: %s %u {%04llx_%04llx_%04llx_%04llx}\n",
622 func, line, header, cpu,
623 *p >> 48, (*p >> 32) & 0xffff, (*p >> 16) & 0xffff,
627 static void __maybe_unused _dump_256_bmp(const char *header,
628 const u64 *p, unsigned cpu, const char* func, int line)
630 pr_debug("%s:%d: %s %u {%016llx:%016llx:%016llx:%016llx}\n",
631 func, line, header, cpu, p[0], p[1], p[2], p[3]);
634 #define dump_bmp(_x) _dump_bmp(_x, __func__, __LINE__)
635 static void _dump_bmp(struct ps3_private* pd, const char* func, int line)
639 spin_lock_irqsave(&pd->bmp_lock, flags);
640 _dump_64_bmp("stat", &pd->bmp.status, pd->thread_id, func, line);
641 _dump_64_bmp("mask", (u64*)&pd->bmp.mask, pd->thread_id, func, line);
642 spin_unlock_irqrestore(&pd->bmp_lock, flags);
645 #define dump_mask(_x) _dump_mask(_x, __func__, __LINE__)
646 static void __maybe_unused _dump_mask(struct ps3_private *pd,
647 const char* func, int line)
651 spin_lock_irqsave(&pd->bmp_lock, flags);
652 _dump_64_bmp("mask", (u64*)&pd->bmp.mask, pd->thread_id, func, line);
653 spin_unlock_irqrestore(&pd->bmp_lock, flags);
656 static void dump_bmp(struct ps3_private* pd) {};
657 #endif /* defined(DEBUG) */
659 static int ps3_host_map(struct irq_domain *h, unsigned int virq,
660 irq_hw_number_t hwirq)
662 DBG("%s:%d: hwirq %lu, virq %u\n", __func__, __LINE__, hwirq,
665 irq_set_chip_and_handler(virq, &ps3_irq_chip, handle_fasteoi_irq);
670 static int ps3_host_match(struct irq_domain *h, struct device_node *np,
671 enum irq_domain_bus_token bus_token)
677 static const struct irq_domain_ops ps3_host_ops = {
679 .match = ps3_host_match,
682 void __init ps3_register_ipi_debug_brk(unsigned int cpu, unsigned int virq)
684 struct ps3_private *pd = &per_cpu(ps3_private, cpu);
686 set_bit(63 - virq, &pd->ipi_debug_brk_mask);
688 DBG("%s:%d: cpu %u, virq %u, mask %lxh\n", __func__, __LINE__,
689 cpu, virq, pd->ipi_debug_brk_mask);
692 void __init ps3_register_ipi_irq(unsigned int cpu, unsigned int virq)
694 struct ps3_private *pd = &per_cpu(ps3_private, cpu);
696 set_bit(63 - virq, &pd->ipi_mask);
698 DBG("%s:%d: cpu %u, virq %u, ipi_mask %lxh\n", __func__, __LINE__,
699 cpu, virq, pd->ipi_mask);
702 static unsigned int ps3_get_irq(void)
704 struct ps3_private *pd = this_cpu_ptr(&ps3_private);
705 u64 x = (pd->bmp.status & pd->bmp.mask);
708 /* check for ipi break first to stop this cpu ASAP */
710 if (x & pd->ipi_debug_brk_mask)
711 x &= pd->ipi_debug_brk_mask;
713 asm volatile("cntlzd %0,%1" : "=r" (plug) : "r" (x));
716 if (unlikely(!plug)) {
717 DBG("%s:%d: no plug found: thread_id %llu\n", __func__,
718 __LINE__, pd->thread_id);
719 dump_bmp(&per_cpu(ps3_private, 0));
720 dump_bmp(&per_cpu(ps3_private, 1));
725 if (unlikely(plug < NR_IRQS_LEGACY || plug > PS3_PLUG_MAX)) {
726 dump_bmp(&per_cpu(ps3_private, 0));
727 dump_bmp(&per_cpu(ps3_private, 1));
732 /* IPIs are EOIed here. */
734 if (test_bit(63 - plug, &pd->ipi_mask))
735 lv1_end_of_interrupt_ext(pd->ppe_id, pd->thread_id, plug);
740 void __init ps3_init_IRQ(void)
744 struct irq_domain *host;
746 host = irq_domain_add_nomap(NULL, PS3_PLUG_MAX + 1, &ps3_host_ops, NULL);
747 irq_set_default_host(host);
749 for_each_possible_cpu(cpu) {
750 struct ps3_private *pd = &per_cpu(ps3_private, cpu);
752 lv1_get_logical_ppe_id(&pd->ppe_id);
753 pd->thread_id = get_hard_smp_processor_id(cpu);
754 spin_lock_init(&pd->bmp_lock);
756 DBG("%s:%d: ppe_id %llu, thread_id %llu, bmp %lxh\n",
757 __func__, __LINE__, pd->ppe_id, pd->thread_id,
758 ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
760 result = lv1_configure_irq_state_bitmap(pd->ppe_id,
761 pd->thread_id, ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
764 FAIL("%s:%d: lv1_configure_irq_state_bitmap failed:"
765 " %s\n", __func__, __LINE__,
769 ppc_md.get_irq = ps3_get_irq;
772 void ps3_shutdown_IRQ(int cpu)
776 u64 thread_id = get_hard_smp_processor_id(cpu);
778 lv1_get_logical_ppe_id(&ppe_id);
779 result = lv1_configure_irq_state_bitmap(ppe_id, thread_id, 0);
781 DBG("%s:%d: lv1_configure_irq_state_bitmap (%llu:%llu/%d) %s\n", __func__,
782 __LINE__, ppe_id, thread_id, cpu, ps3_result(result));