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>
13 #include <asm/machdep.h>
15 #include <asm/lv1call.h>
21 #define DBG udbg_printf
22 #define FAIL udbg_printf
29 * struct ps3_bmp - a per cpu irq status and mask bitmap structure
30 * @status: 256 bit status bitmap indexed by plug
31 * @unused_1: Alignment
32 * @mask: 256 bit mask bitmap indexed by plug
33 * @unused_2: Alignment
35 * The HV maintains per SMT thread mappings of HV outlet to HV plug on
36 * behalf of the guest. These mappings are implemented as 256 bit guest
37 * supplied bitmaps indexed by plug number. The addresses of the bitmaps
38 * are registered with the HV through lv1_configure_irq_state_bitmap().
39 * The HV requires that the 512 bits of status + mask not cross a page
40 * boundary. PS3_BMP_MINALIGN is used to define this minimal 64 byte
43 * The HV supports 256 plugs per thread, assigned as {0..255}, for a total
44 * of 512 plugs supported on a processor. To simplify the logic this
45 * implementation equates HV plug value to Linux virq value, constrains each
46 * interrupt to have a system wide unique plug number, and limits the range
47 * of the plug values to map into the first dword of the bitmaps. This
48 * gives a usable range of plug values of {NUM_ISA_INTERRUPTS..63}. Note
49 * that there is no constraint on how many in this set an individual thread
52 * The mask is declared as unsigned long so we can use set/clear_bit on it.
55 #define PS3_BMP_MINALIGN 64
67 * struct ps3_private - a per cpu data structure
68 * @bmp: ps3_bmp structure
69 * @bmp_lock: Synchronize access to bmp.
70 * @ipi_debug_brk_mask: Mask for debug break IPIs
71 * @ppe_id: HV logical_ppe_id
72 * @thread_id: HV thread_id
73 * @ipi_mask: Mask of IPI virqs
77 struct ps3_bmp bmp __attribute__ ((aligned (PS3_BMP_MINALIGN)));
81 unsigned long ipi_debug_brk_mask;
82 unsigned long ipi_mask;
85 static DEFINE_PER_CPU(struct ps3_private, ps3_private);
88 * ps3_chip_mask - Set an interrupt mask bit in ps3_bmp.
89 * @virq: The assigned Linux virq.
91 * Sets ps3_bmp.mask and calls lv1_did_update_interrupt_mask().
94 static void ps3_chip_mask(struct irq_data *d)
96 struct ps3_private *pd = irq_data_get_irq_chip_data(d);
99 DBG("%s:%d: thread_id %llu, virq %d\n", __func__, __LINE__,
100 pd->thread_id, d->irq);
102 local_irq_save(flags);
103 clear_bit(63 - d->irq, &pd->bmp.mask);
104 lv1_did_update_interrupt_mask(pd->ppe_id, pd->thread_id);
105 local_irq_restore(flags);
109 * ps3_chip_unmask - Clear an interrupt mask bit in ps3_bmp.
110 * @virq: The assigned Linux virq.
112 * Clears ps3_bmp.mask and calls lv1_did_update_interrupt_mask().
115 static void ps3_chip_unmask(struct irq_data *d)
117 struct ps3_private *pd = irq_data_get_irq_chip_data(d);
120 DBG("%s:%d: thread_id %llu, virq %d\n", __func__, __LINE__,
121 pd->thread_id, d->irq);
123 local_irq_save(flags);
124 set_bit(63 - d->irq, &pd->bmp.mask);
125 lv1_did_update_interrupt_mask(pd->ppe_id, pd->thread_id);
126 local_irq_restore(flags);
130 * ps3_chip_eoi - HV end-of-interrupt.
131 * @virq: The assigned Linux virq.
133 * Calls lv1_end_of_interrupt_ext().
136 static void ps3_chip_eoi(struct irq_data *d)
138 const struct ps3_private *pd = irq_data_get_irq_chip_data(d);
140 /* non-IPIs are EOIed here. */
142 if (!test_bit(63 - d->irq, &pd->ipi_mask))
143 lv1_end_of_interrupt_ext(pd->ppe_id, pd->thread_id, d->irq);
147 * ps3_irq_chip - Represents the ps3_bmp as a Linux struct irq_chip.
150 static struct irq_chip ps3_irq_chip = {
152 .irq_mask = ps3_chip_mask,
153 .irq_unmask = ps3_chip_unmask,
154 .irq_eoi = ps3_chip_eoi,
158 * ps3_virq_setup - virq related setup.
159 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
161 * @outlet: The HV outlet from the various create outlet routines.
162 * @virq: The assigned Linux virq.
164 * Calls irq_create_mapping() to get a virq and sets the chip data to
168 static int ps3_virq_setup(enum ps3_cpu_binding cpu, unsigned long outlet,
172 struct ps3_private *pd;
174 /* This defines the default interrupt distribution policy. */
176 if (cpu == PS3_BINDING_CPU_ANY)
179 pd = &per_cpu(ps3_private, cpu);
181 *virq = irq_create_mapping(NULL, outlet);
184 FAIL("%s:%d: irq_create_mapping failed: outlet %lu\n",
185 __func__, __LINE__, outlet);
190 DBG("%s:%d: outlet %lu => cpu %u, virq %u\n", __func__, __LINE__,
193 result = irq_set_chip_data(*virq, pd);
196 FAIL("%s:%d: irq_set_chip_data failed\n",
201 ps3_chip_mask(irq_get_irq_data(*virq));
206 irq_dispose_mapping(*virq);
212 * ps3_virq_destroy - virq related teardown.
213 * @virq: The assigned Linux virq.
215 * Clears chip data and calls irq_dispose_mapping() for the virq.
218 static int ps3_virq_destroy(unsigned int virq)
220 const struct ps3_private *pd = irq_get_chip_data(virq);
222 DBG("%s:%d: ppe_id %llu, thread_id %llu, virq %u\n", __func__,
223 __LINE__, pd->ppe_id, pd->thread_id, virq);
225 irq_set_chip_data(virq, NULL);
226 irq_dispose_mapping(virq);
228 DBG("%s:%d <-\n", __func__, __LINE__);
233 * ps3_irq_plug_setup - Generic outlet and virq related setup.
234 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
236 * @outlet: The HV outlet from the various create outlet routines.
237 * @virq: The assigned Linux virq.
239 * Sets up virq and connects the irq plug.
242 int ps3_irq_plug_setup(enum ps3_cpu_binding cpu, unsigned long outlet,
246 struct ps3_private *pd;
248 result = ps3_virq_setup(cpu, outlet, virq);
251 FAIL("%s:%d: ps3_virq_setup failed\n", __func__, __LINE__);
255 pd = irq_get_chip_data(*virq);
257 /* Binds outlet to cpu + virq. */
259 result = lv1_connect_irq_plug_ext(pd->ppe_id, pd->thread_id, *virq,
263 FAIL("%s:%d: lv1_connect_irq_plug_ext failed: %s\n",
264 __func__, __LINE__, ps3_result(result));
272 ps3_virq_destroy(*virq);
276 EXPORT_SYMBOL_GPL(ps3_irq_plug_setup);
279 * ps3_irq_plug_destroy - Generic outlet and virq related teardown.
280 * @virq: The assigned Linux virq.
282 * Disconnects the irq plug and tears down virq.
283 * Do not call for system bus event interrupts setup with
284 * ps3_sb_event_receive_port_setup().
287 int ps3_irq_plug_destroy(unsigned int virq)
290 const struct ps3_private *pd = irq_get_chip_data(virq);
292 DBG("%s:%d: ppe_id %llu, thread_id %llu, virq %u\n", __func__,
293 __LINE__, pd->ppe_id, pd->thread_id, virq);
295 ps3_chip_mask(irq_get_irq_data(virq));
297 result = lv1_disconnect_irq_plug_ext(pd->ppe_id, pd->thread_id, virq);
300 FAIL("%s:%d: lv1_disconnect_irq_plug_ext failed: %s\n",
301 __func__, __LINE__, ps3_result(result));
303 ps3_virq_destroy(virq);
307 EXPORT_SYMBOL_GPL(ps3_irq_plug_destroy);
310 * ps3_event_receive_port_setup - Setup an event receive port.
311 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
313 * @virq: The assigned Linux virq.
315 * The virq can be used with lv1_connect_interrupt_event_receive_port() to
316 * arrange to receive interrupts from system-bus devices, or with
317 * ps3_send_event_locally() to signal events.
320 int ps3_event_receive_port_setup(enum ps3_cpu_binding cpu, unsigned int *virq)
325 result = lv1_construct_event_receive_port(&outlet);
328 FAIL("%s:%d: lv1_construct_event_receive_port failed: %s\n",
329 __func__, __LINE__, ps3_result(result));
334 result = ps3_irq_plug_setup(cpu, outlet, virq);
339 EXPORT_SYMBOL_GPL(ps3_event_receive_port_setup);
342 * ps3_event_receive_port_destroy - Destroy an event receive port.
343 * @virq: The assigned Linux virq.
345 * Since ps3_event_receive_port_destroy destroys the receive port outlet,
346 * SB devices need to call disconnect_interrupt_event_receive_port() before
350 int ps3_event_receive_port_destroy(unsigned int virq)
354 DBG(" -> %s:%d virq %u\n", __func__, __LINE__, virq);
356 ps3_chip_mask(irq_get_irq_data(virq));
358 result = lv1_destruct_event_receive_port(virq_to_hw(virq));
361 FAIL("%s:%d: lv1_destruct_event_receive_port failed: %s\n",
362 __func__, __LINE__, ps3_result(result));
365 * Don't call ps3_virq_destroy() here since ps3_smp_cleanup_cpu()
366 * calls from interrupt context (smp_call_function) when kexecing.
369 DBG(" <- %s:%d\n", __func__, __LINE__);
373 int ps3_send_event_locally(unsigned int virq)
375 return lv1_send_event_locally(virq_to_hw(virq));
379 * ps3_sb_event_receive_port_setup - Setup a system bus event receive port.
380 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
382 * @dev: The system bus device instance.
383 * @virq: The assigned Linux virq.
385 * An event irq represents a virtual device interrupt. The interrupt_id
386 * coresponds to the software interrupt number.
389 int ps3_sb_event_receive_port_setup(struct ps3_system_bus_device *dev,
390 enum ps3_cpu_binding cpu, unsigned int *virq)
392 /* this should go in system-bus.c */
396 result = ps3_event_receive_port_setup(cpu, virq);
401 result = lv1_connect_interrupt_event_receive_port(dev->bus_id,
402 dev->dev_id, virq_to_hw(*virq), dev->interrupt_id);
405 FAIL("%s:%d: lv1_connect_interrupt_event_receive_port"
406 " failed: %s\n", __func__, __LINE__,
408 ps3_event_receive_port_destroy(*virq);
413 DBG("%s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
414 dev->interrupt_id, *virq);
418 EXPORT_SYMBOL(ps3_sb_event_receive_port_setup);
420 int ps3_sb_event_receive_port_destroy(struct ps3_system_bus_device *dev,
423 /* this should go in system-bus.c */
427 DBG(" -> %s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
428 dev->interrupt_id, virq);
430 result = lv1_disconnect_interrupt_event_receive_port(dev->bus_id,
431 dev->dev_id, virq_to_hw(virq), dev->interrupt_id);
434 FAIL("%s:%d: lv1_disconnect_interrupt_event_receive_port"
435 " failed: %s\n", __func__, __LINE__,
438 result = ps3_event_receive_port_destroy(virq);
442 * ps3_event_receive_port_destroy() destroys the IRQ plug,
443 * so don't call ps3_irq_plug_destroy() here.
446 result = ps3_virq_destroy(virq);
449 DBG(" <- %s:%d\n", __func__, __LINE__);
452 EXPORT_SYMBOL(ps3_sb_event_receive_port_destroy);
455 * ps3_io_irq_setup - Setup a system bus io irq.
456 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
458 * @interrupt_id: The device interrupt id read from the system repository.
459 * @virq: The assigned Linux virq.
461 * An io irq represents a non-virtualized device interrupt. interrupt_id
462 * coresponds to the interrupt number of the interrupt controller.
465 int ps3_io_irq_setup(enum ps3_cpu_binding cpu, unsigned int interrupt_id,
471 result = lv1_construct_io_irq_outlet(interrupt_id, &outlet);
474 FAIL("%s:%d: lv1_construct_io_irq_outlet failed: %s\n",
475 __func__, __LINE__, ps3_result(result));
479 result = ps3_irq_plug_setup(cpu, outlet, virq);
484 EXPORT_SYMBOL_GPL(ps3_io_irq_setup);
486 int ps3_io_irq_destroy(unsigned int virq)
489 unsigned long outlet = virq_to_hw(virq);
491 ps3_chip_mask(irq_get_irq_data(virq));
494 * lv1_destruct_io_irq_outlet() will destroy the IRQ plug,
495 * so call ps3_irq_plug_destroy() first.
498 result = ps3_irq_plug_destroy(virq);
501 result = lv1_destruct_io_irq_outlet(outlet);
504 FAIL("%s:%d: lv1_destruct_io_irq_outlet failed: %s\n",
505 __func__, __LINE__, ps3_result(result));
509 EXPORT_SYMBOL_GPL(ps3_io_irq_destroy);
512 * ps3_vuart_irq_setup - Setup the system virtual uart virq.
513 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
515 * @virt_addr_bmp: The caller supplied virtual uart interrupt bitmap.
516 * @virq: The assigned Linux virq.
518 * The system supports only a single virtual uart, so multiple calls without
519 * freeing the interrupt will return a wrong state error.
522 int ps3_vuart_irq_setup(enum ps3_cpu_binding cpu, void* virt_addr_bmp,
529 BUG_ON(!is_kernel_addr((u64)virt_addr_bmp));
531 lpar_addr = ps3_mm_phys_to_lpar(__pa(virt_addr_bmp));
533 result = lv1_configure_virtual_uart_irq(lpar_addr, &outlet);
536 FAIL("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
537 __func__, __LINE__, ps3_result(result));
541 result = ps3_irq_plug_setup(cpu, outlet, virq);
546 EXPORT_SYMBOL_GPL(ps3_vuart_irq_setup);
548 int ps3_vuart_irq_destroy(unsigned int virq)
552 ps3_chip_mask(irq_get_irq_data(virq));
553 result = lv1_deconfigure_virtual_uart_irq();
556 FAIL("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
557 __func__, __LINE__, ps3_result(result));
561 result = ps3_irq_plug_destroy(virq);
566 EXPORT_SYMBOL_GPL(ps3_vuart_irq_destroy);
569 * ps3_spe_irq_setup - Setup an spe virq.
570 * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
572 * @spe_id: The spe_id returned from lv1_construct_logical_spe().
573 * @class: The spe interrupt class {0,1,2}.
574 * @virq: The assigned Linux virq.
578 int ps3_spe_irq_setup(enum ps3_cpu_binding cpu, unsigned long spe_id,
579 unsigned int class, unsigned int *virq)
586 result = lv1_get_spe_irq_outlet(spe_id, class, &outlet);
589 FAIL("%s:%d: lv1_get_spe_irq_outlet failed: %s\n",
590 __func__, __LINE__, ps3_result(result));
594 result = ps3_irq_plug_setup(cpu, outlet, virq);
600 int ps3_spe_irq_destroy(unsigned int virq)
604 ps3_chip_mask(irq_get_irq_data(virq));
606 result = ps3_irq_plug_destroy(virq);
613 #define PS3_INVALID_OUTLET ((irq_hw_number_t)-1)
614 #define PS3_PLUG_MAX 63
617 static void _dump_64_bmp(const char *header, const u64 *p, unsigned cpu,
618 const char* func, int line)
620 pr_debug("%s:%d: %s %u {%04llx_%04llx_%04llx_%04llx}\n",
621 func, line, header, cpu,
622 *p >> 48, (*p >> 32) & 0xffff, (*p >> 16) & 0xffff,
626 static void __maybe_unused _dump_256_bmp(const char *header,
627 const u64 *p, unsigned cpu, const char* func, int line)
629 pr_debug("%s:%d: %s %u {%016llx:%016llx:%016llx:%016llx}\n",
630 func, line, header, cpu, p[0], p[1], p[2], p[3]);
633 #define dump_bmp(_x) _dump_bmp(_x, __func__, __LINE__)
634 static void _dump_bmp(struct ps3_private* pd, const char* func, int line)
638 spin_lock_irqsave(&pd->bmp_lock, flags);
639 _dump_64_bmp("stat", &pd->bmp.status, pd->thread_id, func, line);
640 _dump_64_bmp("mask", (u64*)&pd->bmp.mask, pd->thread_id, func, line);
641 spin_unlock_irqrestore(&pd->bmp_lock, flags);
644 #define dump_mask(_x) _dump_mask(_x, __func__, __LINE__)
645 static void __maybe_unused _dump_mask(struct ps3_private *pd,
646 const char* func, int line)
650 spin_lock_irqsave(&pd->bmp_lock, flags);
651 _dump_64_bmp("mask", (u64*)&pd->bmp.mask, pd->thread_id, func, line);
652 spin_unlock_irqrestore(&pd->bmp_lock, flags);
655 static void dump_bmp(struct ps3_private* pd) {};
656 #endif /* defined(DEBUG) */
658 static int ps3_host_map(struct irq_domain *h, unsigned int virq,
659 irq_hw_number_t hwirq)
661 DBG("%s:%d: hwirq %lu, virq %u\n", __func__, __LINE__, hwirq,
664 irq_set_chip_and_handler(virq, &ps3_irq_chip, handle_fasteoi_irq);
669 static int ps3_host_match(struct irq_domain *h, struct device_node *np,
670 enum irq_domain_bus_token bus_token)
676 static const struct irq_domain_ops ps3_host_ops = {
678 .match = ps3_host_match,
681 void __init ps3_register_ipi_debug_brk(unsigned int cpu, unsigned int virq)
683 struct ps3_private *pd = &per_cpu(ps3_private, cpu);
685 set_bit(63 - virq, &pd->ipi_debug_brk_mask);
687 DBG("%s:%d: cpu %u, virq %u, mask %lxh\n", __func__, __LINE__,
688 cpu, virq, pd->ipi_debug_brk_mask);
691 void __init ps3_register_ipi_irq(unsigned int cpu, unsigned int virq)
693 struct ps3_private *pd = &per_cpu(ps3_private, cpu);
695 set_bit(63 - virq, &pd->ipi_mask);
697 DBG("%s:%d: cpu %u, virq %u, ipi_mask %lxh\n", __func__, __LINE__,
698 cpu, virq, pd->ipi_mask);
701 static unsigned int ps3_get_irq(void)
703 struct ps3_private *pd = this_cpu_ptr(&ps3_private);
704 u64 x = (pd->bmp.status & pd->bmp.mask);
707 /* check for ipi break first to stop this cpu ASAP */
709 if (x & pd->ipi_debug_brk_mask)
710 x &= pd->ipi_debug_brk_mask;
712 asm volatile("cntlzd %0,%1" : "=r" (plug) : "r" (x));
715 if (unlikely(!plug)) {
716 DBG("%s:%d: no plug found: thread_id %llu\n", __func__,
717 __LINE__, pd->thread_id);
718 dump_bmp(&per_cpu(ps3_private, 0));
719 dump_bmp(&per_cpu(ps3_private, 1));
724 if (unlikely(plug < NUM_ISA_INTERRUPTS || plug > PS3_PLUG_MAX)) {
725 dump_bmp(&per_cpu(ps3_private, 0));
726 dump_bmp(&per_cpu(ps3_private, 1));
731 /* IPIs are EOIed here. */
733 if (test_bit(63 - plug, &pd->ipi_mask))
734 lv1_end_of_interrupt_ext(pd->ppe_id, pd->thread_id, plug);
739 void __init ps3_init_IRQ(void)
743 struct irq_domain *host;
745 host = irq_domain_add_nomap(NULL, PS3_PLUG_MAX + 1, &ps3_host_ops, NULL);
746 irq_set_default_host(host);
748 for_each_possible_cpu(cpu) {
749 struct ps3_private *pd = &per_cpu(ps3_private, cpu);
751 lv1_get_logical_ppe_id(&pd->ppe_id);
752 pd->thread_id = get_hard_smp_processor_id(cpu);
753 spin_lock_init(&pd->bmp_lock);
755 DBG("%s:%d: ppe_id %llu, thread_id %llu, bmp %lxh\n",
756 __func__, __LINE__, pd->ppe_id, pd->thread_id,
757 ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
759 result = lv1_configure_irq_state_bitmap(pd->ppe_id,
760 pd->thread_id, ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
763 FAIL("%s:%d: lv1_configure_irq_state_bitmap failed:"
764 " %s\n", __func__, __LINE__,
768 ppc_md.get_irq = ps3_get_irq;
771 void ps3_shutdown_IRQ(int cpu)
775 u64 thread_id = get_hard_smp_processor_id(cpu);
777 lv1_get_logical_ppe_id(&ppe_id);
778 result = lv1_configure_irq_state_bitmap(ppe_id, thread_id, 0);
780 DBG("%s:%d: lv1_configure_irq_state_bitmap (%llu:%llu/%d) %s\n", __func__,
781 __LINE__, ppe_id, thread_id, cpu, ps3_result(result));