xref: /openbmc/linux/arch/powerpc/platforms/ps3/interrupt.c (revision b1eeb38e456281c37bbfc270a6ca08605b7e7045)
12832a81dSGeoff Levand /*
22832a81dSGeoff Levand  *  PS3 interrupt routines.
32832a81dSGeoff Levand  *
42832a81dSGeoff Levand  *  Copyright (C) 2006 Sony Computer Entertainment Inc.
52832a81dSGeoff Levand  *  Copyright 2006 Sony Corp.
62832a81dSGeoff Levand  *
72832a81dSGeoff Levand  *  This program is free software; you can redistribute it and/or modify
82832a81dSGeoff Levand  *  it under the terms of the GNU General Public License as published by
92832a81dSGeoff Levand  *  the Free Software Foundation; version 2 of the License.
102832a81dSGeoff Levand  *
112832a81dSGeoff Levand  *  This program is distributed in the hope that it will be useful,
122832a81dSGeoff Levand  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
132832a81dSGeoff Levand  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
142832a81dSGeoff Levand  *  GNU General Public License for more details.
152832a81dSGeoff Levand  *
162832a81dSGeoff Levand  *  You should have received a copy of the GNU General Public License
172832a81dSGeoff Levand  *  along with this program; if not, write to the Free Software
182832a81dSGeoff Levand  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
192832a81dSGeoff Levand  */
202832a81dSGeoff Levand 
212832a81dSGeoff Levand #include <linux/kernel.h>
222832a81dSGeoff Levand #include <linux/module.h>
232832a81dSGeoff Levand #include <linux/irq.h>
242832a81dSGeoff Levand 
252832a81dSGeoff Levand #include <asm/machdep.h>
262832a81dSGeoff Levand #include <asm/udbg.h>
272832a81dSGeoff Levand #include <asm/ps3.h>
282832a81dSGeoff Levand #include <asm/lv1call.h>
292832a81dSGeoff Levand 
302832a81dSGeoff Levand #include "platform.h"
312832a81dSGeoff Levand 
322832a81dSGeoff Levand #if defined(DEBUG)
332832a81dSGeoff Levand #define DBG(fmt...) udbg_printf(fmt)
342832a81dSGeoff Levand #else
352832a81dSGeoff Levand #define DBG(fmt...) do{if(0)printk(fmt);}while(0)
362832a81dSGeoff Levand #endif
372832a81dSGeoff Levand 
382832a81dSGeoff Levand /**
39861be32cSGeoff Levand  * struct ps3_bmp - a per cpu irq status and mask bitmap structure
40861be32cSGeoff Levand  * @status: 256 bit status bitmap indexed by plug
41861be32cSGeoff Levand  * @unused_1:
42861be32cSGeoff Levand  * @mask: 256 bit mask bitmap indexed by plug
43861be32cSGeoff Levand  * @unused_2:
44861be32cSGeoff Levand  * @lock:
45861be32cSGeoff Levand  * @ipi_debug_brk_mask:
46861be32cSGeoff Levand  *
47861be32cSGeoff Levand  * The HV mantains per SMT thread mappings of HV outlet to HV plug on
48861be32cSGeoff Levand  * behalf of the guest.  These mappings are implemented as 256 bit guest
49861be32cSGeoff Levand  * supplied bitmaps indexed by plug number.  The addresses of the bitmaps
50861be32cSGeoff Levand  * are registered with the HV through lv1_configure_irq_state_bitmap().
51861be32cSGeoff Levand  *
52861be32cSGeoff Levand  * The HV supports 256 plugs per thread, assigned as {0..255}, for a total
53861be32cSGeoff Levand  * of 512 plugs supported on a processor.  To simplify the logic this
54861be32cSGeoff Levand  * implementation equates HV plug value to Linux virq value, constrains each
55861be32cSGeoff Levand  * interrupt to have a system wide unique plug number, and limits the range
56861be32cSGeoff Levand  * of the plug values to map into the first dword of the bitmaps.  This
57861be32cSGeoff Levand  * gives a usable range of plug values of  {NUM_ISA_INTERRUPTS..63}.  Note
58861be32cSGeoff Levand  * that there is no constraint on how many in this set an individual thread
59861be32cSGeoff Levand  * can acquire.
60861be32cSGeoff Levand  */
61861be32cSGeoff Levand 
62861be32cSGeoff Levand struct ps3_bmp {
63861be32cSGeoff Levand 	struct {
64861be32cSGeoff Levand 		u64 status;
65861be32cSGeoff Levand 		u64 unused_1[3];
66861be32cSGeoff Levand 		u64 mask;
67861be32cSGeoff Levand 		u64 unused_2[3];
68861be32cSGeoff Levand 	};
69861be32cSGeoff Levand 	u64 ipi_debug_brk_mask;
70861be32cSGeoff Levand 	spinlock_t lock;
71861be32cSGeoff Levand };
72861be32cSGeoff Levand 
73861be32cSGeoff Levand /**
74861be32cSGeoff Levand  * struct ps3_private - a per cpu data structure
75861be32cSGeoff Levand  * @bmp: ps3_bmp structure
76861be32cSGeoff Levand  * @node: HV logical_ppe_id
77861be32cSGeoff Levand  * @cpu: HV thread_id
78861be32cSGeoff Levand  */
79861be32cSGeoff Levand 
80861be32cSGeoff Levand struct ps3_private {
81861be32cSGeoff Levand 	struct ps3_bmp bmp __attribute__ ((aligned (64)));
82861be32cSGeoff Levand 	u64 node;
83861be32cSGeoff Levand 	unsigned int cpu;
84861be32cSGeoff Levand };
85861be32cSGeoff Levand 
86861be32cSGeoff Levand static DEFINE_PER_CPU(struct ps3_private, ps3_private);
87861be32cSGeoff Levand 
88*b1eeb38eSGeert Uytterhoeven int ps3_alloc_irq(enum ps3_cpu_binding cpu, unsigned long outlet,
89861be32cSGeoff Levand 	unsigned int *virq)
90861be32cSGeoff Levand {
91861be32cSGeoff Levand 	int result;
92861be32cSGeoff Levand 	struct ps3_private *pd;
93861be32cSGeoff Levand 
94861be32cSGeoff Levand 	/* This defines the default interrupt distribution policy. */
95861be32cSGeoff Levand 
96861be32cSGeoff Levand 	if (cpu == PS3_BINDING_CPU_ANY)
97861be32cSGeoff Levand 		cpu = 0;
98861be32cSGeoff Levand 
99861be32cSGeoff Levand 	pd = &per_cpu(ps3_private, cpu);
100861be32cSGeoff Levand 
101861be32cSGeoff Levand 	*virq = irq_create_mapping(NULL, outlet);
102861be32cSGeoff Levand 
103861be32cSGeoff Levand 	if (*virq == NO_IRQ) {
104861be32cSGeoff Levand 		pr_debug("%s:%d: irq_create_mapping failed: outlet %lu\n",
105861be32cSGeoff Levand 			__func__, __LINE__, outlet);
106861be32cSGeoff Levand 		result = -ENOMEM;
107861be32cSGeoff Levand 		goto fail_create;
108861be32cSGeoff Levand 	}
109861be32cSGeoff Levand 
110861be32cSGeoff Levand 	/* Binds outlet to cpu + virq. */
111861be32cSGeoff Levand 
112861be32cSGeoff Levand 	result = lv1_connect_irq_plug_ext(pd->node, pd->cpu, *virq, outlet, 0);
113861be32cSGeoff Levand 
114861be32cSGeoff Levand 	if (result) {
115861be32cSGeoff Levand 		pr_info("%s:%d: lv1_connect_irq_plug_ext failed: %s\n",
116861be32cSGeoff Levand 		__func__, __LINE__, ps3_result(result));
117861be32cSGeoff Levand 		result = -EPERM;
118861be32cSGeoff Levand 		goto fail_connect;
119861be32cSGeoff Levand 	}
120861be32cSGeoff Levand 
121861be32cSGeoff Levand 	pr_debug("%s:%d: outlet %lu => cpu %u, virq %u\n", __func__, __LINE__,
122861be32cSGeoff Levand 		outlet, cpu, *virq);
123861be32cSGeoff Levand 
124861be32cSGeoff Levand 	result = set_irq_chip_data(*virq, pd);
125861be32cSGeoff Levand 
126861be32cSGeoff Levand 	if (result) {
127861be32cSGeoff Levand 		pr_debug("%s:%d: set_irq_chip_data failed\n",
128861be32cSGeoff Levand 			__func__, __LINE__);
129861be32cSGeoff Levand 		goto fail_set;
130861be32cSGeoff Levand 	}
131861be32cSGeoff Levand 
132861be32cSGeoff Levand 	return result;
133861be32cSGeoff Levand 
134861be32cSGeoff Levand fail_set:
135861be32cSGeoff Levand 	lv1_disconnect_irq_plug_ext(pd->node, pd->cpu, *virq);
136861be32cSGeoff Levand fail_connect:
137861be32cSGeoff Levand 	irq_dispose_mapping(*virq);
138861be32cSGeoff Levand fail_create:
139861be32cSGeoff Levand 	return result;
140861be32cSGeoff Levand }
141*b1eeb38eSGeert Uytterhoeven EXPORT_SYMBOL_GPL(ps3_alloc_irq);
142861be32cSGeoff Levand 
143*b1eeb38eSGeert Uytterhoeven int ps3_free_irq(unsigned int virq)
144861be32cSGeoff Levand {
145861be32cSGeoff Levand 	int result;
146861be32cSGeoff Levand 	const struct ps3_private *pd = get_irq_chip_data(virq);
147861be32cSGeoff Levand 
148861be32cSGeoff Levand 	pr_debug("%s:%d: node %lu, cpu %d, virq %u\n", __func__, __LINE__,
149861be32cSGeoff Levand 		pd->node, pd->cpu, virq);
150861be32cSGeoff Levand 
151861be32cSGeoff Levand 	result = lv1_disconnect_irq_plug_ext(pd->node, pd->cpu, virq);
152861be32cSGeoff Levand 
153861be32cSGeoff Levand 	if (result)
154861be32cSGeoff Levand 		pr_info("%s:%d: lv1_disconnect_irq_plug_ext failed: %s\n",
155861be32cSGeoff Levand 		__func__, __LINE__, ps3_result(result));
156861be32cSGeoff Levand 
157861be32cSGeoff Levand 	set_irq_chip_data(virq, NULL);
158861be32cSGeoff Levand 	irq_dispose_mapping(virq);
159*b1eeb38eSGeert Uytterhoeven 	return result;
160861be32cSGeoff Levand }
161*b1eeb38eSGeert Uytterhoeven EXPORT_SYMBOL_GPL(ps3_free_irq);
162861be32cSGeoff Levand 
163861be32cSGeoff Levand /**
1642832a81dSGeoff Levand  * ps3_alloc_io_irq - Assign a virq to a system bus device.
165861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
166861be32cSGeoff Levand  * serviced on.
167861be32cSGeoff Levand  * @interrupt_id: The device interrupt id read from the system repository.
1682832a81dSGeoff Levand  * @virq: The assigned Linux virq.
1692832a81dSGeoff Levand  *
1702832a81dSGeoff Levand  * An io irq represents a non-virtualized device interrupt.  interrupt_id
1712832a81dSGeoff Levand  * coresponds to the interrupt number of the interrupt controller.
1722832a81dSGeoff Levand  */
1732832a81dSGeoff Levand 
174861be32cSGeoff Levand int ps3_alloc_io_irq(enum ps3_cpu_binding cpu, unsigned int interrupt_id,
175861be32cSGeoff Levand 	unsigned int *virq)
1762832a81dSGeoff Levand {
1772832a81dSGeoff Levand 	int result;
1782832a81dSGeoff Levand 	unsigned long outlet;
1792832a81dSGeoff Levand 
1802832a81dSGeoff Levand 	result = lv1_construct_io_irq_outlet(interrupt_id, &outlet);
1812832a81dSGeoff Levand 
1822832a81dSGeoff Levand 	if (result) {
1832832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_construct_io_irq_outlet failed: %s\n",
1842832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
1852832a81dSGeoff Levand 		return result;
1862832a81dSGeoff Levand 	}
1872832a81dSGeoff Levand 
188*b1eeb38eSGeert Uytterhoeven 	result = ps3_alloc_irq(cpu, outlet, virq);
189861be32cSGeoff Levand 	BUG_ON(result);
1902832a81dSGeoff Levand 
191861be32cSGeoff Levand 	return result;
1922832a81dSGeoff Levand }
1932832a81dSGeoff Levand 
1942832a81dSGeoff Levand int ps3_free_io_irq(unsigned int virq)
1952832a81dSGeoff Levand {
1962832a81dSGeoff Levand 	int result;
1972832a81dSGeoff Levand 
1982832a81dSGeoff Levand 	result = lv1_destruct_io_irq_outlet(virq_to_hw(virq));
1992832a81dSGeoff Levand 
200ded84bcbSGeert Uytterhoeven 	if (result)
2012832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_destruct_io_irq_outlet failed: %s\n",
2022832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
2032832a81dSGeoff Levand 
204*b1eeb38eSGeert Uytterhoeven 	ps3_free_irq(virq);
2052832a81dSGeoff Levand 
2062832a81dSGeoff Levand 	return result;
2072832a81dSGeoff Levand }
2082832a81dSGeoff Levand 
2092832a81dSGeoff Levand /**
2102832a81dSGeoff Levand  * ps3_alloc_event_irq - Allocate a virq for use with a system event.
211861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
212861be32cSGeoff Levand  * serviced on.
2132832a81dSGeoff Levand  * @virq: The assigned Linux virq.
2142832a81dSGeoff Levand  *
2152832a81dSGeoff Levand  * The virq can be used with lv1_connect_interrupt_event_receive_port() to
2162832a81dSGeoff Levand  * arrange to receive events, or with ps3_send_event_locally() to signal
2172832a81dSGeoff Levand  * events.
2182832a81dSGeoff Levand  */
2192832a81dSGeoff Levand 
220861be32cSGeoff Levand int ps3_alloc_event_irq(enum ps3_cpu_binding cpu, unsigned int *virq)
2212832a81dSGeoff Levand {
2222832a81dSGeoff Levand 	int result;
2232832a81dSGeoff Levand 	unsigned long outlet;
2242832a81dSGeoff Levand 
2252832a81dSGeoff Levand 	result = lv1_construct_event_receive_port(&outlet);
2262832a81dSGeoff Levand 
2272832a81dSGeoff Levand 	if (result) {
2282832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_construct_event_receive_port failed: %s\n",
2292832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
2302832a81dSGeoff Levand 		*virq = NO_IRQ;
2312832a81dSGeoff Levand 		return result;
2322832a81dSGeoff Levand 	}
2332832a81dSGeoff Levand 
234*b1eeb38eSGeert Uytterhoeven 	result = ps3_alloc_irq(cpu, outlet, virq);
235861be32cSGeoff Levand 	BUG_ON(result);
2362832a81dSGeoff Levand 
237861be32cSGeoff Levand 	return result;
2382832a81dSGeoff Levand }
2392832a81dSGeoff Levand 
2402832a81dSGeoff Levand int ps3_free_event_irq(unsigned int virq)
2412832a81dSGeoff Levand {
2422832a81dSGeoff Levand 	int result;
2432832a81dSGeoff Levand 
2442832a81dSGeoff Levand 	pr_debug(" -> %s:%d\n", __func__, __LINE__);
2452832a81dSGeoff Levand 
2462832a81dSGeoff Levand 	result = lv1_destruct_event_receive_port(virq_to_hw(virq));
2472832a81dSGeoff Levand 
2482832a81dSGeoff Levand 	if (result)
2492832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_destruct_event_receive_port failed: %s\n",
2502832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
2512832a81dSGeoff Levand 
252*b1eeb38eSGeert Uytterhoeven 	ps3_free_irq(virq);
2532832a81dSGeoff Levand 
2542832a81dSGeoff Levand 	pr_debug(" <- %s:%d\n", __func__, __LINE__);
2552832a81dSGeoff Levand 	return result;
2562832a81dSGeoff Levand }
2572832a81dSGeoff Levand 
2582832a81dSGeoff Levand int ps3_send_event_locally(unsigned int virq)
2592832a81dSGeoff Levand {
2602832a81dSGeoff Levand 	return lv1_send_event_locally(virq_to_hw(virq));
2612832a81dSGeoff Levand }
2622832a81dSGeoff Levand 
2632832a81dSGeoff Levand /**
2642832a81dSGeoff Levand  * ps3_connect_event_irq - Assign a virq to a system bus device.
265861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
266861be32cSGeoff Levand  * serviced on.
2672832a81dSGeoff Levand  * @did: The HV device identifier read from the system repository.
2682832a81dSGeoff Levand  * @interrupt_id: The device interrupt id read from the system repository.
2692832a81dSGeoff Levand  * @virq: The assigned Linux virq.
2702832a81dSGeoff Levand  *
2712832a81dSGeoff Levand  * An event irq represents a virtual device interrupt.  The interrupt_id
2722832a81dSGeoff Levand  * coresponds to the software interrupt number.
2732832a81dSGeoff Levand  */
2742832a81dSGeoff Levand 
275861be32cSGeoff Levand int ps3_connect_event_irq(enum ps3_cpu_binding cpu,
276861be32cSGeoff Levand 	const struct ps3_device_id *did, unsigned int interrupt_id,
277861be32cSGeoff Levand 	unsigned int *virq)
2782832a81dSGeoff Levand {
2792832a81dSGeoff Levand 	int result;
2802832a81dSGeoff Levand 
281861be32cSGeoff Levand 	result = ps3_alloc_event_irq(cpu, virq);
2822832a81dSGeoff Levand 
2832832a81dSGeoff Levand 	if (result)
2842832a81dSGeoff Levand 		return result;
2852832a81dSGeoff Levand 
2862832a81dSGeoff Levand 	result = lv1_connect_interrupt_event_receive_port(did->bus_id,
2872832a81dSGeoff Levand 		did->dev_id, virq_to_hw(*virq), interrupt_id);
2882832a81dSGeoff Levand 
2892832a81dSGeoff Levand 	if (result) {
2902832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_connect_interrupt_event_receive_port"
2912832a81dSGeoff Levand 			" failed: %s\n", __func__, __LINE__,
2922832a81dSGeoff Levand 			ps3_result(result));
2932832a81dSGeoff Levand 		ps3_free_event_irq(*virq);
2942832a81dSGeoff Levand 		*virq = NO_IRQ;
2952832a81dSGeoff Levand 		return result;
2962832a81dSGeoff Levand 	}
2972832a81dSGeoff Levand 
2982832a81dSGeoff Levand 	pr_debug("%s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
2992832a81dSGeoff Levand 		interrupt_id, *virq);
3002832a81dSGeoff Levand 
3012832a81dSGeoff Levand 	return 0;
3022832a81dSGeoff Levand }
3032832a81dSGeoff Levand 
3042832a81dSGeoff Levand int ps3_disconnect_event_irq(const struct ps3_device_id *did,
3052832a81dSGeoff Levand 	unsigned int interrupt_id, unsigned int virq)
3062832a81dSGeoff Levand {
3072832a81dSGeoff Levand 	int result;
3082832a81dSGeoff Levand 
3092832a81dSGeoff Levand 	pr_debug(" -> %s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
3102832a81dSGeoff Levand 		interrupt_id, virq);
3112832a81dSGeoff Levand 
3122832a81dSGeoff Levand 	result = lv1_disconnect_interrupt_event_receive_port(did->bus_id,
3132832a81dSGeoff Levand 		did->dev_id, virq_to_hw(virq), interrupt_id);
3142832a81dSGeoff Levand 
3152832a81dSGeoff Levand 	if (result)
3162832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_disconnect_interrupt_event_receive_port"
3172832a81dSGeoff Levand 			" failed: %s\n", __func__, __LINE__,
3182832a81dSGeoff Levand 			ps3_result(result));
3192832a81dSGeoff Levand 
3202832a81dSGeoff Levand 	ps3_free_event_irq(virq);
3212832a81dSGeoff Levand 
3222832a81dSGeoff Levand 	pr_debug(" <- %s:%d\n", __func__, __LINE__);
3232832a81dSGeoff Levand 	return result;
3242832a81dSGeoff Levand }
3252832a81dSGeoff Levand 
3262832a81dSGeoff Levand /**
3272832a81dSGeoff Levand  * ps3_alloc_vuart_irq - Configure the system virtual uart virq.
328861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
329861be32cSGeoff Levand  * serviced on.
3302832a81dSGeoff Levand  * @virt_addr_bmp: The caller supplied virtual uart interrupt bitmap.
3312832a81dSGeoff Levand  * @virq: The assigned Linux virq.
3322832a81dSGeoff Levand  *
3332832a81dSGeoff Levand  * The system supports only a single virtual uart, so multiple calls without
3342832a81dSGeoff Levand  * freeing the interrupt will return a wrong state error.
3352832a81dSGeoff Levand  */
3362832a81dSGeoff Levand 
337861be32cSGeoff Levand int ps3_alloc_vuart_irq(enum ps3_cpu_binding cpu, void* virt_addr_bmp,
338861be32cSGeoff Levand 	unsigned int *virq)
3392832a81dSGeoff Levand {
3402832a81dSGeoff Levand 	int result;
3412832a81dSGeoff Levand 	unsigned long outlet;
342861be32cSGeoff Levand 	u64 lpar_addr;
3432832a81dSGeoff Levand 
344861be32cSGeoff Levand 	BUG_ON(!is_kernel_addr((u64)virt_addr_bmp));
3452832a81dSGeoff Levand 
3462832a81dSGeoff Levand 	lpar_addr = ps3_mm_phys_to_lpar(__pa(virt_addr_bmp));
3472832a81dSGeoff Levand 
3482832a81dSGeoff Levand 	result = lv1_configure_virtual_uart_irq(lpar_addr, &outlet);
3492832a81dSGeoff Levand 
3502832a81dSGeoff Levand 	if (result) {
3512832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
3522832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
3532832a81dSGeoff Levand 		return result;
3542832a81dSGeoff Levand 	}
3552832a81dSGeoff Levand 
356*b1eeb38eSGeert Uytterhoeven 	result = ps3_alloc_irq(cpu, outlet, virq);
357861be32cSGeoff Levand 	BUG_ON(result);
3582832a81dSGeoff Levand 
359861be32cSGeoff Levand 	return result;
3602832a81dSGeoff Levand }
3612832a81dSGeoff Levand 
3622832a81dSGeoff Levand int ps3_free_vuart_irq(unsigned int virq)
3632832a81dSGeoff Levand {
3642832a81dSGeoff Levand 	int result;
3652832a81dSGeoff Levand 
3662832a81dSGeoff Levand 	result = lv1_deconfigure_virtual_uart_irq();
3672832a81dSGeoff Levand 
3682832a81dSGeoff Levand 	if (result) {
3692832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
3702832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
3712832a81dSGeoff Levand 		return result;
3722832a81dSGeoff Levand 	}
3732832a81dSGeoff Levand 
374*b1eeb38eSGeert Uytterhoeven 	ps3_free_irq(virq);
3752832a81dSGeoff Levand 
3762832a81dSGeoff Levand 	return result;
3772832a81dSGeoff Levand }
3782832a81dSGeoff Levand 
3792832a81dSGeoff Levand /**
3802832a81dSGeoff Levand  * ps3_alloc_spe_irq - Configure an spe virq.
381861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
382861be32cSGeoff Levand  * serviced on.
3832832a81dSGeoff Levand  * @spe_id: The spe_id returned from lv1_construct_logical_spe().
3842832a81dSGeoff Levand  * @class: The spe interrupt class {0,1,2}.
3852832a81dSGeoff Levand  * @virq: The assigned Linux virq.
3862832a81dSGeoff Levand  *
3872832a81dSGeoff Levand  */
3882832a81dSGeoff Levand 
389861be32cSGeoff Levand int ps3_alloc_spe_irq(enum ps3_cpu_binding cpu, unsigned long spe_id,
390861be32cSGeoff Levand 	unsigned int class, unsigned int *virq)
3912832a81dSGeoff Levand {
3922832a81dSGeoff Levand 	int result;
3932832a81dSGeoff Levand 	unsigned long outlet;
3942832a81dSGeoff Levand 
3952832a81dSGeoff Levand 	BUG_ON(class > 2);
3962832a81dSGeoff Levand 
3972832a81dSGeoff Levand 	result = lv1_get_spe_irq_outlet(spe_id, class, &outlet);
3982832a81dSGeoff Levand 
3992832a81dSGeoff Levand 	if (result) {
4002832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_get_spe_irq_outlet failed: %s\n",
4012832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
4022832a81dSGeoff Levand 		return result;
4032832a81dSGeoff Levand 	}
4042832a81dSGeoff Levand 
405*b1eeb38eSGeert Uytterhoeven 	result = ps3_alloc_irq(cpu, outlet, virq);
406861be32cSGeoff Levand 	BUG_ON(result);
4072832a81dSGeoff Levand 
408861be32cSGeoff Levand 	return result;
4092832a81dSGeoff Levand }
4102832a81dSGeoff Levand 
4112832a81dSGeoff Levand int ps3_free_spe_irq(unsigned int virq)
4122832a81dSGeoff Levand {
413*b1eeb38eSGeert Uytterhoeven 	ps3_free_irq(virq);
4142832a81dSGeoff Levand 	return 0;
4152832a81dSGeoff Levand }
4162832a81dSGeoff Levand 
417*b1eeb38eSGeert Uytterhoeven 
4182832a81dSGeoff Levand #define PS3_INVALID_OUTLET ((irq_hw_number_t)-1)
4192832a81dSGeoff Levand #define PS3_PLUG_MAX 63
4202832a81dSGeoff Levand 
4212832a81dSGeoff Levand #if defined(DEBUG)
422861be32cSGeoff Levand static void _dump_64_bmp(const char *header, const u64 *p, unsigned cpu,
4232832a81dSGeoff Levand 	const char* func, int line)
4242832a81dSGeoff Levand {
4252832a81dSGeoff Levand 	pr_debug("%s:%d: %s %u {%04lx_%04lx_%04lx_%04lx}\n",
4262832a81dSGeoff Levand 		func, line, header, cpu,
4272832a81dSGeoff Levand 		*p >> 48, (*p >> 32) & 0xffff, (*p >> 16) & 0xffff,
4282832a81dSGeoff Levand 		*p & 0xffff);
4292832a81dSGeoff Levand }
4302832a81dSGeoff Levand 
4312832a81dSGeoff Levand static void __attribute__ ((unused)) _dump_256_bmp(const char *header,
432861be32cSGeoff Levand 	const u64 *p, unsigned cpu, const char* func, int line)
4332832a81dSGeoff Levand {
4342832a81dSGeoff Levand 	pr_debug("%s:%d: %s %u {%016lx:%016lx:%016lx:%016lx}\n",
4352832a81dSGeoff Levand 		func, line, header, cpu, p[0], p[1], p[2], p[3]);
4362832a81dSGeoff Levand }
4372832a81dSGeoff Levand 
4382832a81dSGeoff Levand #define dump_bmp(_x) _dump_bmp(_x, __func__, __LINE__)
4399633ac8dSGeoff Levand static void _dump_bmp(struct ps3_private* pd, const char* func, int line)
4402832a81dSGeoff Levand {
4412832a81dSGeoff Levand 	unsigned long flags;
4422832a81dSGeoff Levand 
4432832a81dSGeoff Levand 	spin_lock_irqsave(&pd->bmp.lock, flags);
4442832a81dSGeoff Levand 	_dump_64_bmp("stat", &pd->bmp.status, pd->cpu, func, line);
4452832a81dSGeoff Levand 	_dump_64_bmp("mask", &pd->bmp.mask, pd->cpu, func, line);
4462832a81dSGeoff Levand 	spin_unlock_irqrestore(&pd->bmp.lock, flags);
4472832a81dSGeoff Levand }
4482832a81dSGeoff Levand 
4492832a81dSGeoff Levand #define dump_mask(_x) _dump_mask(_x, __func__, __LINE__)
4509633ac8dSGeoff Levand static void __attribute__ ((unused)) _dump_mask(struct ps3_private* pd,
4512832a81dSGeoff Levand 	const char* func, int line)
4522832a81dSGeoff Levand {
4532832a81dSGeoff Levand 	unsigned long flags;
4542832a81dSGeoff Levand 
4552832a81dSGeoff Levand 	spin_lock_irqsave(&pd->bmp.lock, flags);
4562832a81dSGeoff Levand 	_dump_64_bmp("mask", &pd->bmp.mask, pd->cpu, func, line);
4572832a81dSGeoff Levand 	spin_unlock_irqrestore(&pd->bmp.lock, flags);
4582832a81dSGeoff Levand }
4592832a81dSGeoff Levand #else
4609633ac8dSGeoff Levand static void dump_bmp(struct ps3_private* pd) {};
4612832a81dSGeoff Levand #endif /* defined(DEBUG) */
4622832a81dSGeoff Levand 
4639633ac8dSGeoff Levand static void ps3_chip_mask(unsigned int virq)
4642832a81dSGeoff Levand {
4659633ac8dSGeoff Levand 	struct ps3_private *pd = get_irq_chip_data(virq);
466861be32cSGeoff Levand 	u64 bit = 0x8000000000000000UL >> virq;
467861be32cSGeoff Levand 	u64 *p = &pd->bmp.mask;
468861be32cSGeoff Levand 	u64 old;
4699cf9e196SBenjamin Herrenschmidt 	unsigned long flags;
4702832a81dSGeoff Levand 
4712832a81dSGeoff Levand 	pr_debug("%s:%d: cpu %u, virq %d\n", __func__, __LINE__, pd->cpu, virq);
4722832a81dSGeoff Levand 
4739cf9e196SBenjamin Herrenschmidt 	local_irq_save(flags);
4749cf9e196SBenjamin Herrenschmidt 	asm volatile(
4759cf9e196SBenjamin Herrenschmidt 		     "1:	ldarx %0,0,%3\n"
4769cf9e196SBenjamin Herrenschmidt 		     "andc	%0,%0,%2\n"
4779cf9e196SBenjamin Herrenschmidt 		     "stdcx.	%0,0,%3\n"
4789cf9e196SBenjamin Herrenschmidt 		     "bne-	1b"
4799cf9e196SBenjamin Herrenschmidt 		     : "=&r" (old), "+m" (*p)
4809cf9e196SBenjamin Herrenschmidt 		     : "r" (bit), "r" (p)
4819cf9e196SBenjamin Herrenschmidt 		     : "cc" );
4822832a81dSGeoff Levand 
4832832a81dSGeoff Levand 	lv1_did_update_interrupt_mask(pd->node, pd->cpu);
4849cf9e196SBenjamin Herrenschmidt 	local_irq_restore(flags);
4852832a81dSGeoff Levand }
4862832a81dSGeoff Levand 
4879633ac8dSGeoff Levand static void ps3_chip_unmask(unsigned int virq)
4882832a81dSGeoff Levand {
4899633ac8dSGeoff Levand 	struct ps3_private *pd = get_irq_chip_data(virq);
490861be32cSGeoff Levand 	u64 bit = 0x8000000000000000UL >> virq;
491861be32cSGeoff Levand 	u64 *p = &pd->bmp.mask;
492861be32cSGeoff Levand 	u64 old;
4939cf9e196SBenjamin Herrenschmidt 	unsigned long flags;
4942832a81dSGeoff Levand 
4952832a81dSGeoff Levand 	pr_debug("%s:%d: cpu %u, virq %d\n", __func__, __LINE__, pd->cpu, virq);
4962832a81dSGeoff Levand 
4979cf9e196SBenjamin Herrenschmidt 	local_irq_save(flags);
4989cf9e196SBenjamin Herrenschmidt 	asm volatile(
4999cf9e196SBenjamin Herrenschmidt 		     "1:	ldarx %0,0,%3\n"
5009cf9e196SBenjamin Herrenschmidt 		     "or	%0,%0,%2\n"
5019cf9e196SBenjamin Herrenschmidt 		     "stdcx.	%0,0,%3\n"
5029cf9e196SBenjamin Herrenschmidt 		     "bne-	1b"
5039cf9e196SBenjamin Herrenschmidt 		     : "=&r" (old), "+m" (*p)
5049cf9e196SBenjamin Herrenschmidt 		     : "r" (bit), "r" (p)
5059cf9e196SBenjamin Herrenschmidt 		     : "cc" );
5062832a81dSGeoff Levand 
5072832a81dSGeoff Levand 	lv1_did_update_interrupt_mask(pd->node, pd->cpu);
5089cf9e196SBenjamin Herrenschmidt 	local_irq_restore(flags);
5092832a81dSGeoff Levand }
5102832a81dSGeoff Levand 
5119633ac8dSGeoff Levand static void ps3_chip_eoi(unsigned int virq)
5122832a81dSGeoff Levand {
513407e24a0SGeoff Levand 	const struct ps3_private *pd = get_irq_chip_data(virq);
514407e24a0SGeoff Levand 	lv1_end_of_interrupt_ext(pd->node, pd->cpu, virq);
5152832a81dSGeoff Levand }
5162832a81dSGeoff Levand 
5172832a81dSGeoff Levand static struct irq_chip irq_chip = {
5182832a81dSGeoff Levand 	.typename = "ps3",
5199633ac8dSGeoff Levand 	.mask = ps3_chip_mask,
5209633ac8dSGeoff Levand 	.unmask = ps3_chip_unmask,
5219633ac8dSGeoff Levand 	.eoi = ps3_chip_eoi,
5222832a81dSGeoff Levand };
5232832a81dSGeoff Levand 
5249633ac8dSGeoff Levand static void ps3_host_unmap(struct irq_host *h, unsigned int virq)
5252832a81dSGeoff Levand {
526861be32cSGeoff Levand 	set_irq_chip_data(virq, NULL);
5272832a81dSGeoff Levand }
5282832a81dSGeoff Levand 
5299633ac8dSGeoff Levand static int ps3_host_map(struct irq_host *h, unsigned int virq,
5302832a81dSGeoff Levand 	irq_hw_number_t hwirq)
5312832a81dSGeoff Levand {
532861be32cSGeoff Levand 	pr_debug("%s:%d: hwirq %lu, virq %u\n", __func__, __LINE__, hwirq,
533861be32cSGeoff Levand 		virq);
5342832a81dSGeoff Levand 
5352832a81dSGeoff Levand 	set_irq_chip_and_handler(virq, &irq_chip, handle_fasteoi_irq);
5362832a81dSGeoff Levand 
537861be32cSGeoff Levand 	return 0;
5382832a81dSGeoff Levand }
5392832a81dSGeoff Levand 
5409633ac8dSGeoff Levand static struct irq_host_ops ps3_host_ops = {
5419633ac8dSGeoff Levand 	.map = ps3_host_map,
5429633ac8dSGeoff Levand 	.unmap = ps3_host_unmap,
5432832a81dSGeoff Levand };
5442832a81dSGeoff Levand 
5452832a81dSGeoff Levand void __init ps3_register_ipi_debug_brk(unsigned int cpu, unsigned int virq)
5462832a81dSGeoff Levand {
5479633ac8dSGeoff Levand 	struct ps3_private *pd = &per_cpu(ps3_private, cpu);
5482832a81dSGeoff Levand 
5492832a81dSGeoff Levand 	pd->bmp.ipi_debug_brk_mask = 0x8000000000000000UL >> virq;
5502832a81dSGeoff Levand 
5512832a81dSGeoff Levand 	pr_debug("%s:%d: cpu %u, virq %u, mask %lxh\n", __func__, __LINE__,
5522832a81dSGeoff Levand 		cpu, virq, pd->bmp.ipi_debug_brk_mask);
5532832a81dSGeoff Levand }
5542832a81dSGeoff Levand 
5559cf9e196SBenjamin Herrenschmidt unsigned int ps3_get_irq(void)
5562832a81dSGeoff Levand {
5579cf9e196SBenjamin Herrenschmidt 	struct ps3_private *pd = &__get_cpu_var(ps3_private);
558861be32cSGeoff Levand 	u64 x = (pd->bmp.status & pd->bmp.mask);
5599cf9e196SBenjamin Herrenschmidt 	unsigned int plug;
5602832a81dSGeoff Levand 
5612832a81dSGeoff Levand 	/* check for ipi break first to stop this cpu ASAP */
5622832a81dSGeoff Levand 
5639cf9e196SBenjamin Herrenschmidt 	if (x & pd->bmp.ipi_debug_brk_mask)
5649cf9e196SBenjamin Herrenschmidt 		x &= pd->bmp.ipi_debug_brk_mask;
5652832a81dSGeoff Levand 
5669cf9e196SBenjamin Herrenschmidt 	asm volatile("cntlzd %0,%1" : "=r" (plug) : "r" (x));
5679cf9e196SBenjamin Herrenschmidt 	plug &= 0x3f;
5682832a81dSGeoff Levand 
5699cf9e196SBenjamin Herrenschmidt 	if (unlikely(plug) == NO_IRQ) {
5702832a81dSGeoff Levand 		pr_debug("%s:%d: no plug found: cpu %u\n", __func__, __LINE__,
5712832a81dSGeoff Levand 			pd->cpu);
5729633ac8dSGeoff Levand 		dump_bmp(&per_cpu(ps3_private, 0));
5739633ac8dSGeoff Levand 		dump_bmp(&per_cpu(ps3_private, 1));
5742832a81dSGeoff Levand 		return NO_IRQ;
5752832a81dSGeoff Levand 	}
5762832a81dSGeoff Levand 
5772832a81dSGeoff Levand #if defined(DEBUG)
5789cf9e196SBenjamin Herrenschmidt 	if (unlikely(plug < NUM_ISA_INTERRUPTS || plug > PS3_PLUG_MAX)) {
5799633ac8dSGeoff Levand 		dump_bmp(&per_cpu(ps3_private, 0));
5809633ac8dSGeoff Levand 		dump_bmp(&per_cpu(ps3_private, 1));
5812832a81dSGeoff Levand 		BUG();
5822832a81dSGeoff Levand 	}
5832832a81dSGeoff Levand #endif
5842832a81dSGeoff Levand 	return plug;
5852832a81dSGeoff Levand }
5862832a81dSGeoff Levand 
5872832a81dSGeoff Levand void __init ps3_init_IRQ(void)
5882832a81dSGeoff Levand {
5892832a81dSGeoff Levand 	int result;
5902832a81dSGeoff Levand 	unsigned cpu;
5912832a81dSGeoff Levand 	struct irq_host *host;
5922832a81dSGeoff Levand 
5939633ac8dSGeoff Levand 	host = irq_alloc_host(IRQ_HOST_MAP_NOMAP, 0, &ps3_host_ops,
5942832a81dSGeoff Levand 		PS3_INVALID_OUTLET);
5952832a81dSGeoff Levand 	irq_set_default_host(host);
5962832a81dSGeoff Levand 	irq_set_virq_count(PS3_PLUG_MAX + 1);
5972832a81dSGeoff Levand 
5982832a81dSGeoff Levand 	for_each_possible_cpu(cpu) {
5999633ac8dSGeoff Levand 		struct ps3_private *pd = &per_cpu(ps3_private, cpu);
6002832a81dSGeoff Levand 
601407e24a0SGeoff Levand 		lv1_get_logical_ppe_id(&pd->node);
602407e24a0SGeoff Levand 		pd->cpu = get_hard_smp_processor_id(cpu);
6032832a81dSGeoff Levand 		spin_lock_init(&pd->bmp.lock);
6042832a81dSGeoff Levand 
605407e24a0SGeoff Levand 		pr_debug("%s:%d: node %lu, cpu %d, bmp %lxh\n", __func__,
606407e24a0SGeoff Levand 			__LINE__, pd->node, pd->cpu,
607407e24a0SGeoff Levand 			ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
608407e24a0SGeoff Levand 
609407e24a0SGeoff Levand 		result = lv1_configure_irq_state_bitmap(pd->node, pd->cpu,
610407e24a0SGeoff Levand 			ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
6112832a81dSGeoff Levand 
6122832a81dSGeoff Levand 		if (result)
6132832a81dSGeoff Levand 			pr_debug("%s:%d: lv1_configure_irq_state_bitmap failed:"
6142832a81dSGeoff Levand 				" %s\n", __func__, __LINE__,
6152832a81dSGeoff Levand 				ps3_result(result));
6162832a81dSGeoff Levand 	}
6172832a81dSGeoff Levand 
6182832a81dSGeoff Levand 	ppc_md.get_irq = ps3_get_irq;
6192832a81dSGeoff Levand }
620