xref: /openbmc/linux/arch/powerpc/platforms/ps3/interrupt.c (revision 577157659fb0ace3b88dd75e2c6cb1af84b3040d)
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().
51*57715765SGeoff Levand  * The HV requires that the 512 bits of status + mask not cross a page
52*57715765SGeoff Levand  * boundary.  PS3_BMP_MINALIGN is used to define this minimal 64 byte
53*57715765SGeoff Levand  * alignment.
54861be32cSGeoff Levand  *
55861be32cSGeoff Levand  * The HV supports 256 plugs per thread, assigned as {0..255}, for a total
56861be32cSGeoff Levand  * of 512 plugs supported on a processor.  To simplify the logic this
57861be32cSGeoff Levand  * implementation equates HV plug value to Linux virq value, constrains each
58861be32cSGeoff Levand  * interrupt to have a system wide unique plug number, and limits the range
59861be32cSGeoff Levand  * of the plug values to map into the first dword of the bitmaps.  This
60861be32cSGeoff Levand  * gives a usable range of plug values of  {NUM_ISA_INTERRUPTS..63}.  Note
61861be32cSGeoff Levand  * that there is no constraint on how many in this set an individual thread
62861be32cSGeoff Levand  * can acquire.
63861be32cSGeoff Levand  */
64861be32cSGeoff Levand 
65*57715765SGeoff Levand #define PS3_BMP_MINALIGN 64
66*57715765SGeoff Levand 
67861be32cSGeoff Levand struct ps3_bmp {
68861be32cSGeoff Levand 	struct {
69861be32cSGeoff Levand 		u64 status;
70861be32cSGeoff Levand 		u64 unused_1[3];
71861be32cSGeoff Levand 		u64 mask;
72861be32cSGeoff Levand 		u64 unused_2[3];
73861be32cSGeoff Levand 	};
74861be32cSGeoff Levand 	u64 ipi_debug_brk_mask;
75861be32cSGeoff Levand 	spinlock_t lock;
76861be32cSGeoff Levand };
77861be32cSGeoff Levand 
78861be32cSGeoff Levand /**
79861be32cSGeoff Levand  * struct ps3_private - a per cpu data structure
80861be32cSGeoff Levand  * @bmp: ps3_bmp structure
81861be32cSGeoff Levand  * @node: HV logical_ppe_id
82861be32cSGeoff Levand  * @cpu: HV thread_id
83861be32cSGeoff Levand  */
84861be32cSGeoff Levand 
85861be32cSGeoff Levand struct ps3_private {
86*57715765SGeoff Levand 	struct ps3_bmp bmp __attribute__ ((aligned (PS3_BMP_MINALIGN)));
87861be32cSGeoff Levand 	u64 node;
88861be32cSGeoff Levand 	unsigned int cpu;
89861be32cSGeoff Levand };
90861be32cSGeoff Levand 
91861be32cSGeoff Levand static DEFINE_PER_CPU(struct ps3_private, ps3_private);
92861be32cSGeoff Levand 
93b1eeb38eSGeert Uytterhoeven int ps3_alloc_irq(enum ps3_cpu_binding cpu, unsigned long outlet,
94861be32cSGeoff Levand 	unsigned int *virq)
95861be32cSGeoff Levand {
96861be32cSGeoff Levand 	int result;
97861be32cSGeoff Levand 	struct ps3_private *pd;
98861be32cSGeoff Levand 
99861be32cSGeoff Levand 	/* This defines the default interrupt distribution policy. */
100861be32cSGeoff Levand 
101861be32cSGeoff Levand 	if (cpu == PS3_BINDING_CPU_ANY)
102861be32cSGeoff Levand 		cpu = 0;
103861be32cSGeoff Levand 
104861be32cSGeoff Levand 	pd = &per_cpu(ps3_private, cpu);
105861be32cSGeoff Levand 
106861be32cSGeoff Levand 	*virq = irq_create_mapping(NULL, outlet);
107861be32cSGeoff Levand 
108861be32cSGeoff Levand 	if (*virq == NO_IRQ) {
109861be32cSGeoff Levand 		pr_debug("%s:%d: irq_create_mapping failed: outlet %lu\n",
110861be32cSGeoff Levand 			__func__, __LINE__, outlet);
111861be32cSGeoff Levand 		result = -ENOMEM;
112861be32cSGeoff Levand 		goto fail_create;
113861be32cSGeoff Levand 	}
114861be32cSGeoff Levand 
115861be32cSGeoff Levand 	/* Binds outlet to cpu + virq. */
116861be32cSGeoff Levand 
117861be32cSGeoff Levand 	result = lv1_connect_irq_plug_ext(pd->node, pd->cpu, *virq, outlet, 0);
118861be32cSGeoff Levand 
119861be32cSGeoff Levand 	if (result) {
120861be32cSGeoff Levand 		pr_info("%s:%d: lv1_connect_irq_plug_ext failed: %s\n",
121861be32cSGeoff Levand 		__func__, __LINE__, ps3_result(result));
122861be32cSGeoff Levand 		result = -EPERM;
123861be32cSGeoff Levand 		goto fail_connect;
124861be32cSGeoff Levand 	}
125861be32cSGeoff Levand 
126861be32cSGeoff Levand 	pr_debug("%s:%d: outlet %lu => cpu %u, virq %u\n", __func__, __LINE__,
127861be32cSGeoff Levand 		outlet, cpu, *virq);
128861be32cSGeoff Levand 
129861be32cSGeoff Levand 	result = set_irq_chip_data(*virq, pd);
130861be32cSGeoff Levand 
131861be32cSGeoff Levand 	if (result) {
132861be32cSGeoff Levand 		pr_debug("%s:%d: set_irq_chip_data failed\n",
133861be32cSGeoff Levand 			__func__, __LINE__);
134861be32cSGeoff Levand 		goto fail_set;
135861be32cSGeoff Levand 	}
136861be32cSGeoff Levand 
137861be32cSGeoff Levand 	return result;
138861be32cSGeoff Levand 
139861be32cSGeoff Levand fail_set:
140861be32cSGeoff Levand 	lv1_disconnect_irq_plug_ext(pd->node, pd->cpu, *virq);
141861be32cSGeoff Levand fail_connect:
142861be32cSGeoff Levand 	irq_dispose_mapping(*virq);
143861be32cSGeoff Levand fail_create:
144861be32cSGeoff Levand 	return result;
145861be32cSGeoff Levand }
146b1eeb38eSGeert Uytterhoeven EXPORT_SYMBOL_GPL(ps3_alloc_irq);
147861be32cSGeoff Levand 
148b1eeb38eSGeert Uytterhoeven int ps3_free_irq(unsigned int virq)
149861be32cSGeoff Levand {
150861be32cSGeoff Levand 	int result;
151861be32cSGeoff Levand 	const struct ps3_private *pd = get_irq_chip_data(virq);
152861be32cSGeoff Levand 
153861be32cSGeoff Levand 	pr_debug("%s:%d: node %lu, cpu %d, virq %u\n", __func__, __LINE__,
154861be32cSGeoff Levand 		pd->node, pd->cpu, virq);
155861be32cSGeoff Levand 
156861be32cSGeoff Levand 	result = lv1_disconnect_irq_plug_ext(pd->node, pd->cpu, virq);
157861be32cSGeoff Levand 
158861be32cSGeoff Levand 	if (result)
159861be32cSGeoff Levand 		pr_info("%s:%d: lv1_disconnect_irq_plug_ext failed: %s\n",
160861be32cSGeoff Levand 		__func__, __LINE__, ps3_result(result));
161861be32cSGeoff Levand 
162861be32cSGeoff Levand 	set_irq_chip_data(virq, NULL);
163861be32cSGeoff Levand 	irq_dispose_mapping(virq);
164b1eeb38eSGeert Uytterhoeven 	return result;
165861be32cSGeoff Levand }
166b1eeb38eSGeert Uytterhoeven EXPORT_SYMBOL_GPL(ps3_free_irq);
167861be32cSGeoff Levand 
168861be32cSGeoff Levand /**
1692832a81dSGeoff Levand  * ps3_alloc_io_irq - Assign a virq to a system bus device.
170861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
171861be32cSGeoff Levand  * serviced on.
172861be32cSGeoff Levand  * @interrupt_id: The device interrupt id read from the system repository.
1732832a81dSGeoff Levand  * @virq: The assigned Linux virq.
1742832a81dSGeoff Levand  *
1752832a81dSGeoff Levand  * An io irq represents a non-virtualized device interrupt.  interrupt_id
1762832a81dSGeoff Levand  * coresponds to the interrupt number of the interrupt controller.
1772832a81dSGeoff Levand  */
1782832a81dSGeoff Levand 
179861be32cSGeoff Levand int ps3_alloc_io_irq(enum ps3_cpu_binding cpu, unsigned int interrupt_id,
180861be32cSGeoff Levand 	unsigned int *virq)
1812832a81dSGeoff Levand {
1822832a81dSGeoff Levand 	int result;
1832832a81dSGeoff Levand 	unsigned long outlet;
1842832a81dSGeoff Levand 
1852832a81dSGeoff Levand 	result = lv1_construct_io_irq_outlet(interrupt_id, &outlet);
1862832a81dSGeoff Levand 
1872832a81dSGeoff Levand 	if (result) {
1882832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_construct_io_irq_outlet failed: %s\n",
1892832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
1902832a81dSGeoff Levand 		return result;
1912832a81dSGeoff Levand 	}
1922832a81dSGeoff Levand 
193b1eeb38eSGeert Uytterhoeven 	result = ps3_alloc_irq(cpu, outlet, virq);
194861be32cSGeoff Levand 	BUG_ON(result);
1952832a81dSGeoff Levand 
196861be32cSGeoff Levand 	return result;
1972832a81dSGeoff Levand }
1982832a81dSGeoff Levand 
1992832a81dSGeoff Levand int ps3_free_io_irq(unsigned int virq)
2002832a81dSGeoff Levand {
2012832a81dSGeoff Levand 	int result;
2022832a81dSGeoff Levand 
2032832a81dSGeoff Levand 	result = lv1_destruct_io_irq_outlet(virq_to_hw(virq));
2042832a81dSGeoff Levand 
205ded84bcbSGeert Uytterhoeven 	if (result)
2062832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_destruct_io_irq_outlet failed: %s\n",
2072832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
2082832a81dSGeoff Levand 
209b1eeb38eSGeert Uytterhoeven 	ps3_free_irq(virq);
2102832a81dSGeoff Levand 
2112832a81dSGeoff Levand 	return result;
2122832a81dSGeoff Levand }
2132832a81dSGeoff Levand 
2142832a81dSGeoff Levand /**
2152832a81dSGeoff Levand  * ps3_alloc_event_irq - Allocate a virq for use with a system event.
216861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
217861be32cSGeoff Levand  * serviced on.
2182832a81dSGeoff Levand  * @virq: The assigned Linux virq.
2192832a81dSGeoff Levand  *
2202832a81dSGeoff Levand  * The virq can be used with lv1_connect_interrupt_event_receive_port() to
2212832a81dSGeoff Levand  * arrange to receive events, or with ps3_send_event_locally() to signal
2222832a81dSGeoff Levand  * events.
2232832a81dSGeoff Levand  */
2242832a81dSGeoff Levand 
225861be32cSGeoff Levand int ps3_alloc_event_irq(enum ps3_cpu_binding cpu, unsigned int *virq)
2262832a81dSGeoff Levand {
2272832a81dSGeoff Levand 	int result;
2282832a81dSGeoff Levand 	unsigned long outlet;
2292832a81dSGeoff Levand 
2302832a81dSGeoff Levand 	result = lv1_construct_event_receive_port(&outlet);
2312832a81dSGeoff Levand 
2322832a81dSGeoff Levand 	if (result) {
2332832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_construct_event_receive_port failed: %s\n",
2342832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
2352832a81dSGeoff Levand 		*virq = NO_IRQ;
2362832a81dSGeoff Levand 		return result;
2372832a81dSGeoff Levand 	}
2382832a81dSGeoff Levand 
239b1eeb38eSGeert Uytterhoeven 	result = ps3_alloc_irq(cpu, outlet, virq);
240861be32cSGeoff Levand 	BUG_ON(result);
2412832a81dSGeoff Levand 
242861be32cSGeoff Levand 	return result;
2432832a81dSGeoff Levand }
2442832a81dSGeoff Levand 
2452832a81dSGeoff Levand int ps3_free_event_irq(unsigned int virq)
2462832a81dSGeoff Levand {
2472832a81dSGeoff Levand 	int result;
2482832a81dSGeoff Levand 
2492832a81dSGeoff Levand 	pr_debug(" -> %s:%d\n", __func__, __LINE__);
2502832a81dSGeoff Levand 
2512832a81dSGeoff Levand 	result = lv1_destruct_event_receive_port(virq_to_hw(virq));
2522832a81dSGeoff Levand 
2532832a81dSGeoff Levand 	if (result)
2542832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_destruct_event_receive_port failed: %s\n",
2552832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
2562832a81dSGeoff Levand 
257b1eeb38eSGeert Uytterhoeven 	ps3_free_irq(virq);
2582832a81dSGeoff Levand 
2592832a81dSGeoff Levand 	pr_debug(" <- %s:%d\n", __func__, __LINE__);
2602832a81dSGeoff Levand 	return result;
2612832a81dSGeoff Levand }
2622832a81dSGeoff Levand 
2632832a81dSGeoff Levand int ps3_send_event_locally(unsigned int virq)
2642832a81dSGeoff Levand {
2652832a81dSGeoff Levand 	return lv1_send_event_locally(virq_to_hw(virq));
2662832a81dSGeoff Levand }
2672832a81dSGeoff Levand 
2682832a81dSGeoff Levand /**
2692832a81dSGeoff Levand  * ps3_connect_event_irq - Assign a virq to a system bus device.
270861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
271861be32cSGeoff Levand  * serviced on.
2722832a81dSGeoff Levand  * @did: The HV device identifier read from the system repository.
2732832a81dSGeoff Levand  * @interrupt_id: The device interrupt id read from the system repository.
2742832a81dSGeoff Levand  * @virq: The assigned Linux virq.
2752832a81dSGeoff Levand  *
2762832a81dSGeoff Levand  * An event irq represents a virtual device interrupt.  The interrupt_id
2772832a81dSGeoff Levand  * coresponds to the software interrupt number.
2782832a81dSGeoff Levand  */
2792832a81dSGeoff Levand 
280861be32cSGeoff Levand int ps3_connect_event_irq(enum ps3_cpu_binding cpu,
281861be32cSGeoff Levand 	const struct ps3_device_id *did, unsigned int interrupt_id,
282861be32cSGeoff Levand 	unsigned int *virq)
2832832a81dSGeoff Levand {
2842832a81dSGeoff Levand 	int result;
2852832a81dSGeoff Levand 
286861be32cSGeoff Levand 	result = ps3_alloc_event_irq(cpu, virq);
2872832a81dSGeoff Levand 
2882832a81dSGeoff Levand 	if (result)
2892832a81dSGeoff Levand 		return result;
2902832a81dSGeoff Levand 
2912832a81dSGeoff Levand 	result = lv1_connect_interrupt_event_receive_port(did->bus_id,
2922832a81dSGeoff Levand 		did->dev_id, virq_to_hw(*virq), interrupt_id);
2932832a81dSGeoff Levand 
2942832a81dSGeoff Levand 	if (result) {
2952832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_connect_interrupt_event_receive_port"
2962832a81dSGeoff Levand 			" failed: %s\n", __func__, __LINE__,
2972832a81dSGeoff Levand 			ps3_result(result));
2982832a81dSGeoff Levand 		ps3_free_event_irq(*virq);
2992832a81dSGeoff Levand 		*virq = NO_IRQ;
3002832a81dSGeoff Levand 		return result;
3012832a81dSGeoff Levand 	}
3022832a81dSGeoff Levand 
3032832a81dSGeoff Levand 	pr_debug("%s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
3042832a81dSGeoff Levand 		interrupt_id, *virq);
3052832a81dSGeoff Levand 
3062832a81dSGeoff Levand 	return 0;
3072832a81dSGeoff Levand }
3082832a81dSGeoff Levand 
3092832a81dSGeoff Levand int ps3_disconnect_event_irq(const struct ps3_device_id *did,
3102832a81dSGeoff Levand 	unsigned int interrupt_id, unsigned int virq)
3112832a81dSGeoff Levand {
3122832a81dSGeoff Levand 	int result;
3132832a81dSGeoff Levand 
3142832a81dSGeoff Levand 	pr_debug(" -> %s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
3152832a81dSGeoff Levand 		interrupt_id, virq);
3162832a81dSGeoff Levand 
3172832a81dSGeoff Levand 	result = lv1_disconnect_interrupt_event_receive_port(did->bus_id,
3182832a81dSGeoff Levand 		did->dev_id, virq_to_hw(virq), interrupt_id);
3192832a81dSGeoff Levand 
3202832a81dSGeoff Levand 	if (result)
3212832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_disconnect_interrupt_event_receive_port"
3222832a81dSGeoff Levand 			" failed: %s\n", __func__, __LINE__,
3232832a81dSGeoff Levand 			ps3_result(result));
3242832a81dSGeoff Levand 
3252832a81dSGeoff Levand 	ps3_free_event_irq(virq);
3262832a81dSGeoff Levand 
3272832a81dSGeoff Levand 	pr_debug(" <- %s:%d\n", __func__, __LINE__);
3282832a81dSGeoff Levand 	return result;
3292832a81dSGeoff Levand }
3302832a81dSGeoff Levand 
3312832a81dSGeoff Levand /**
3322832a81dSGeoff Levand  * ps3_alloc_vuart_irq - Configure the system virtual uart virq.
333861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
334861be32cSGeoff Levand  * serviced on.
3352832a81dSGeoff Levand  * @virt_addr_bmp: The caller supplied virtual uart interrupt bitmap.
3362832a81dSGeoff Levand  * @virq: The assigned Linux virq.
3372832a81dSGeoff Levand  *
3382832a81dSGeoff Levand  * The system supports only a single virtual uart, so multiple calls without
3392832a81dSGeoff Levand  * freeing the interrupt will return a wrong state error.
3402832a81dSGeoff Levand  */
3412832a81dSGeoff Levand 
342861be32cSGeoff Levand int ps3_alloc_vuart_irq(enum ps3_cpu_binding cpu, void* virt_addr_bmp,
343861be32cSGeoff Levand 	unsigned int *virq)
3442832a81dSGeoff Levand {
3452832a81dSGeoff Levand 	int result;
3462832a81dSGeoff Levand 	unsigned long outlet;
347861be32cSGeoff Levand 	u64 lpar_addr;
3482832a81dSGeoff Levand 
349861be32cSGeoff Levand 	BUG_ON(!is_kernel_addr((u64)virt_addr_bmp));
3502832a81dSGeoff Levand 
3512832a81dSGeoff Levand 	lpar_addr = ps3_mm_phys_to_lpar(__pa(virt_addr_bmp));
3522832a81dSGeoff Levand 
3532832a81dSGeoff Levand 	result = lv1_configure_virtual_uart_irq(lpar_addr, &outlet);
3542832a81dSGeoff Levand 
3552832a81dSGeoff Levand 	if (result) {
3562832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
3572832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
3582832a81dSGeoff Levand 		return result;
3592832a81dSGeoff Levand 	}
3602832a81dSGeoff Levand 
361b1eeb38eSGeert Uytterhoeven 	result = ps3_alloc_irq(cpu, outlet, virq);
362861be32cSGeoff Levand 	BUG_ON(result);
3632832a81dSGeoff Levand 
364861be32cSGeoff Levand 	return result;
3652832a81dSGeoff Levand }
3662832a81dSGeoff Levand 
3672832a81dSGeoff Levand int ps3_free_vuart_irq(unsigned int virq)
3682832a81dSGeoff Levand {
3692832a81dSGeoff Levand 	int result;
3702832a81dSGeoff Levand 
3712832a81dSGeoff Levand 	result = lv1_deconfigure_virtual_uart_irq();
3722832a81dSGeoff Levand 
3732832a81dSGeoff Levand 	if (result) {
3742832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
3752832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
3762832a81dSGeoff Levand 		return result;
3772832a81dSGeoff Levand 	}
3782832a81dSGeoff Levand 
379b1eeb38eSGeert Uytterhoeven 	ps3_free_irq(virq);
3802832a81dSGeoff Levand 
3812832a81dSGeoff Levand 	return result;
3822832a81dSGeoff Levand }
3832832a81dSGeoff Levand 
3842832a81dSGeoff Levand /**
3852832a81dSGeoff Levand  * ps3_alloc_spe_irq - Configure an spe virq.
386861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
387861be32cSGeoff Levand  * serviced on.
3882832a81dSGeoff Levand  * @spe_id: The spe_id returned from lv1_construct_logical_spe().
3892832a81dSGeoff Levand  * @class: The spe interrupt class {0,1,2}.
3902832a81dSGeoff Levand  * @virq: The assigned Linux virq.
3912832a81dSGeoff Levand  *
3922832a81dSGeoff Levand  */
3932832a81dSGeoff Levand 
394861be32cSGeoff Levand int ps3_alloc_spe_irq(enum ps3_cpu_binding cpu, unsigned long spe_id,
395861be32cSGeoff Levand 	unsigned int class, unsigned int *virq)
3962832a81dSGeoff Levand {
3972832a81dSGeoff Levand 	int result;
3982832a81dSGeoff Levand 	unsigned long outlet;
3992832a81dSGeoff Levand 
4002832a81dSGeoff Levand 	BUG_ON(class > 2);
4012832a81dSGeoff Levand 
4022832a81dSGeoff Levand 	result = lv1_get_spe_irq_outlet(spe_id, class, &outlet);
4032832a81dSGeoff Levand 
4042832a81dSGeoff Levand 	if (result) {
4052832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_get_spe_irq_outlet failed: %s\n",
4062832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
4072832a81dSGeoff Levand 		return result;
4082832a81dSGeoff Levand 	}
4092832a81dSGeoff Levand 
410b1eeb38eSGeert Uytterhoeven 	result = ps3_alloc_irq(cpu, outlet, virq);
411861be32cSGeoff Levand 	BUG_ON(result);
4122832a81dSGeoff Levand 
413861be32cSGeoff Levand 	return result;
4142832a81dSGeoff Levand }
4152832a81dSGeoff Levand 
4162832a81dSGeoff Levand int ps3_free_spe_irq(unsigned int virq)
4172832a81dSGeoff Levand {
418b1eeb38eSGeert Uytterhoeven 	ps3_free_irq(virq);
4192832a81dSGeoff Levand 	return 0;
4202832a81dSGeoff Levand }
4212832a81dSGeoff Levand 
422b1eeb38eSGeert Uytterhoeven 
4232832a81dSGeoff Levand #define PS3_INVALID_OUTLET ((irq_hw_number_t)-1)
4242832a81dSGeoff Levand #define PS3_PLUG_MAX 63
4252832a81dSGeoff Levand 
4262832a81dSGeoff Levand #if defined(DEBUG)
427861be32cSGeoff Levand static void _dump_64_bmp(const char *header, const u64 *p, unsigned cpu,
4282832a81dSGeoff Levand 	const char* func, int line)
4292832a81dSGeoff Levand {
4302832a81dSGeoff Levand 	pr_debug("%s:%d: %s %u {%04lx_%04lx_%04lx_%04lx}\n",
4312832a81dSGeoff Levand 		func, line, header, cpu,
4322832a81dSGeoff Levand 		*p >> 48, (*p >> 32) & 0xffff, (*p >> 16) & 0xffff,
4332832a81dSGeoff Levand 		*p & 0xffff);
4342832a81dSGeoff Levand }
4352832a81dSGeoff Levand 
4362832a81dSGeoff Levand static void __attribute__ ((unused)) _dump_256_bmp(const char *header,
437861be32cSGeoff Levand 	const u64 *p, unsigned cpu, const char* func, int line)
4382832a81dSGeoff Levand {
4392832a81dSGeoff Levand 	pr_debug("%s:%d: %s %u {%016lx:%016lx:%016lx:%016lx}\n",
4402832a81dSGeoff Levand 		func, line, header, cpu, p[0], p[1], p[2], p[3]);
4412832a81dSGeoff Levand }
4422832a81dSGeoff Levand 
4432832a81dSGeoff Levand #define dump_bmp(_x) _dump_bmp(_x, __func__, __LINE__)
4449633ac8dSGeoff Levand static void _dump_bmp(struct ps3_private* pd, const char* func, int line)
4452832a81dSGeoff Levand {
4462832a81dSGeoff Levand 	unsigned long flags;
4472832a81dSGeoff Levand 
4482832a81dSGeoff Levand 	spin_lock_irqsave(&pd->bmp.lock, flags);
4492832a81dSGeoff Levand 	_dump_64_bmp("stat", &pd->bmp.status, pd->cpu, func, line);
4502832a81dSGeoff Levand 	_dump_64_bmp("mask", &pd->bmp.mask, pd->cpu, func, line);
4512832a81dSGeoff Levand 	spin_unlock_irqrestore(&pd->bmp.lock, flags);
4522832a81dSGeoff Levand }
4532832a81dSGeoff Levand 
4542832a81dSGeoff Levand #define dump_mask(_x) _dump_mask(_x, __func__, __LINE__)
4559633ac8dSGeoff Levand static void __attribute__ ((unused)) _dump_mask(struct ps3_private* pd,
4562832a81dSGeoff Levand 	const char* func, int line)
4572832a81dSGeoff Levand {
4582832a81dSGeoff Levand 	unsigned long flags;
4592832a81dSGeoff Levand 
4602832a81dSGeoff Levand 	spin_lock_irqsave(&pd->bmp.lock, flags);
4612832a81dSGeoff Levand 	_dump_64_bmp("mask", &pd->bmp.mask, pd->cpu, func, line);
4622832a81dSGeoff Levand 	spin_unlock_irqrestore(&pd->bmp.lock, flags);
4632832a81dSGeoff Levand }
4642832a81dSGeoff Levand #else
4659633ac8dSGeoff Levand static void dump_bmp(struct ps3_private* pd) {};
4662832a81dSGeoff Levand #endif /* defined(DEBUG) */
4672832a81dSGeoff Levand 
4689633ac8dSGeoff Levand static void ps3_chip_mask(unsigned int virq)
4692832a81dSGeoff Levand {
4709633ac8dSGeoff Levand 	struct ps3_private *pd = get_irq_chip_data(virq);
471861be32cSGeoff Levand 	u64 bit = 0x8000000000000000UL >> virq;
472861be32cSGeoff Levand 	u64 *p = &pd->bmp.mask;
473861be32cSGeoff Levand 	u64 old;
4749cf9e196SBenjamin Herrenschmidt 	unsigned long flags;
4752832a81dSGeoff Levand 
4762832a81dSGeoff Levand 	pr_debug("%s:%d: cpu %u, virq %d\n", __func__, __LINE__, pd->cpu, virq);
4772832a81dSGeoff Levand 
4789cf9e196SBenjamin Herrenschmidt 	local_irq_save(flags);
4799cf9e196SBenjamin Herrenschmidt 	asm volatile(
4809cf9e196SBenjamin Herrenschmidt 		     "1:	ldarx %0,0,%3\n"
4819cf9e196SBenjamin Herrenschmidt 		     "andc	%0,%0,%2\n"
4829cf9e196SBenjamin Herrenschmidt 		     "stdcx.	%0,0,%3\n"
4839cf9e196SBenjamin Herrenschmidt 		     "bne-	1b"
4849cf9e196SBenjamin Herrenschmidt 		     : "=&r" (old), "+m" (*p)
4859cf9e196SBenjamin Herrenschmidt 		     : "r" (bit), "r" (p)
4869cf9e196SBenjamin Herrenschmidt 		     : "cc" );
4872832a81dSGeoff Levand 
4882832a81dSGeoff Levand 	lv1_did_update_interrupt_mask(pd->node, pd->cpu);
4899cf9e196SBenjamin Herrenschmidt 	local_irq_restore(flags);
4902832a81dSGeoff Levand }
4912832a81dSGeoff Levand 
4929633ac8dSGeoff Levand static void ps3_chip_unmask(unsigned int virq)
4932832a81dSGeoff Levand {
4949633ac8dSGeoff Levand 	struct ps3_private *pd = get_irq_chip_data(virq);
495861be32cSGeoff Levand 	u64 bit = 0x8000000000000000UL >> virq;
496861be32cSGeoff Levand 	u64 *p = &pd->bmp.mask;
497861be32cSGeoff Levand 	u64 old;
4989cf9e196SBenjamin Herrenschmidt 	unsigned long flags;
4992832a81dSGeoff Levand 
5002832a81dSGeoff Levand 	pr_debug("%s:%d: cpu %u, virq %d\n", __func__, __LINE__, pd->cpu, virq);
5012832a81dSGeoff Levand 
5029cf9e196SBenjamin Herrenschmidt 	local_irq_save(flags);
5039cf9e196SBenjamin Herrenschmidt 	asm volatile(
5049cf9e196SBenjamin Herrenschmidt 		     "1:	ldarx %0,0,%3\n"
5059cf9e196SBenjamin Herrenschmidt 		     "or	%0,%0,%2\n"
5069cf9e196SBenjamin Herrenschmidt 		     "stdcx.	%0,0,%3\n"
5079cf9e196SBenjamin Herrenschmidt 		     "bne-	1b"
5089cf9e196SBenjamin Herrenschmidt 		     : "=&r" (old), "+m" (*p)
5099cf9e196SBenjamin Herrenschmidt 		     : "r" (bit), "r" (p)
5109cf9e196SBenjamin Herrenschmidt 		     : "cc" );
5112832a81dSGeoff Levand 
5122832a81dSGeoff Levand 	lv1_did_update_interrupt_mask(pd->node, pd->cpu);
5139cf9e196SBenjamin Herrenschmidt 	local_irq_restore(flags);
5142832a81dSGeoff Levand }
5152832a81dSGeoff Levand 
5169633ac8dSGeoff Levand static void ps3_chip_eoi(unsigned int virq)
5172832a81dSGeoff Levand {
518407e24a0SGeoff Levand 	const struct ps3_private *pd = get_irq_chip_data(virq);
519407e24a0SGeoff Levand 	lv1_end_of_interrupt_ext(pd->node, pd->cpu, virq);
5202832a81dSGeoff Levand }
5212832a81dSGeoff Levand 
5222832a81dSGeoff Levand static struct irq_chip irq_chip = {
5232832a81dSGeoff Levand 	.typename = "ps3",
5249633ac8dSGeoff Levand 	.mask = ps3_chip_mask,
5259633ac8dSGeoff Levand 	.unmask = ps3_chip_unmask,
5269633ac8dSGeoff Levand 	.eoi = ps3_chip_eoi,
5272832a81dSGeoff Levand };
5282832a81dSGeoff Levand 
5299633ac8dSGeoff Levand static void ps3_host_unmap(struct irq_host *h, unsigned int virq)
5302832a81dSGeoff Levand {
531861be32cSGeoff Levand 	set_irq_chip_data(virq, NULL);
5322832a81dSGeoff Levand }
5332832a81dSGeoff Levand 
5349633ac8dSGeoff Levand static int ps3_host_map(struct irq_host *h, unsigned int virq,
5352832a81dSGeoff Levand 	irq_hw_number_t hwirq)
5362832a81dSGeoff Levand {
537861be32cSGeoff Levand 	pr_debug("%s:%d: hwirq %lu, virq %u\n", __func__, __LINE__, hwirq,
538861be32cSGeoff Levand 		virq);
5392832a81dSGeoff Levand 
5402832a81dSGeoff Levand 	set_irq_chip_and_handler(virq, &irq_chip, handle_fasteoi_irq);
5412832a81dSGeoff Levand 
542861be32cSGeoff Levand 	return 0;
5432832a81dSGeoff Levand }
5442832a81dSGeoff Levand 
5459633ac8dSGeoff Levand static struct irq_host_ops ps3_host_ops = {
5469633ac8dSGeoff Levand 	.map = ps3_host_map,
5479633ac8dSGeoff Levand 	.unmap = ps3_host_unmap,
5482832a81dSGeoff Levand };
5492832a81dSGeoff Levand 
5502832a81dSGeoff Levand void __init ps3_register_ipi_debug_brk(unsigned int cpu, unsigned int virq)
5512832a81dSGeoff Levand {
5529633ac8dSGeoff Levand 	struct ps3_private *pd = &per_cpu(ps3_private, cpu);
5532832a81dSGeoff Levand 
5542832a81dSGeoff Levand 	pd->bmp.ipi_debug_brk_mask = 0x8000000000000000UL >> virq;
5552832a81dSGeoff Levand 
5562832a81dSGeoff Levand 	pr_debug("%s:%d: cpu %u, virq %u, mask %lxh\n", __func__, __LINE__,
5572832a81dSGeoff Levand 		cpu, virq, pd->bmp.ipi_debug_brk_mask);
5582832a81dSGeoff Levand }
5592832a81dSGeoff Levand 
5609cf9e196SBenjamin Herrenschmidt unsigned int ps3_get_irq(void)
5612832a81dSGeoff Levand {
5629cf9e196SBenjamin Herrenschmidt 	struct ps3_private *pd = &__get_cpu_var(ps3_private);
563861be32cSGeoff Levand 	u64 x = (pd->bmp.status & pd->bmp.mask);
5649cf9e196SBenjamin Herrenschmidt 	unsigned int plug;
5652832a81dSGeoff Levand 
5662832a81dSGeoff Levand 	/* check for ipi break first to stop this cpu ASAP */
5672832a81dSGeoff Levand 
5689cf9e196SBenjamin Herrenschmidt 	if (x & pd->bmp.ipi_debug_brk_mask)
5699cf9e196SBenjamin Herrenschmidt 		x &= pd->bmp.ipi_debug_brk_mask;
5702832a81dSGeoff Levand 
5719cf9e196SBenjamin Herrenschmidt 	asm volatile("cntlzd %0,%1" : "=r" (plug) : "r" (x));
5729cf9e196SBenjamin Herrenschmidt 	plug &= 0x3f;
5732832a81dSGeoff Levand 
5749cf9e196SBenjamin Herrenschmidt 	if (unlikely(plug) == NO_IRQ) {
5752832a81dSGeoff Levand 		pr_debug("%s:%d: no plug found: cpu %u\n", __func__, __LINE__,
5762832a81dSGeoff Levand 			pd->cpu);
5779633ac8dSGeoff Levand 		dump_bmp(&per_cpu(ps3_private, 0));
5789633ac8dSGeoff Levand 		dump_bmp(&per_cpu(ps3_private, 1));
5792832a81dSGeoff Levand 		return NO_IRQ;
5802832a81dSGeoff Levand 	}
5812832a81dSGeoff Levand 
5822832a81dSGeoff Levand #if defined(DEBUG)
5839cf9e196SBenjamin Herrenschmidt 	if (unlikely(plug < NUM_ISA_INTERRUPTS || plug > PS3_PLUG_MAX)) {
5849633ac8dSGeoff Levand 		dump_bmp(&per_cpu(ps3_private, 0));
5859633ac8dSGeoff Levand 		dump_bmp(&per_cpu(ps3_private, 1));
5862832a81dSGeoff Levand 		BUG();
5872832a81dSGeoff Levand 	}
5882832a81dSGeoff Levand #endif
5892832a81dSGeoff Levand 	return plug;
5902832a81dSGeoff Levand }
5912832a81dSGeoff Levand 
5922832a81dSGeoff Levand void __init ps3_init_IRQ(void)
5932832a81dSGeoff Levand {
5942832a81dSGeoff Levand 	int result;
5952832a81dSGeoff Levand 	unsigned cpu;
5962832a81dSGeoff Levand 	struct irq_host *host;
5972832a81dSGeoff Levand 
5989633ac8dSGeoff Levand 	host = irq_alloc_host(IRQ_HOST_MAP_NOMAP, 0, &ps3_host_ops,
5992832a81dSGeoff Levand 		PS3_INVALID_OUTLET);
6002832a81dSGeoff Levand 	irq_set_default_host(host);
6012832a81dSGeoff Levand 	irq_set_virq_count(PS3_PLUG_MAX + 1);
6022832a81dSGeoff Levand 
6032832a81dSGeoff Levand 	for_each_possible_cpu(cpu) {
6049633ac8dSGeoff Levand 		struct ps3_private *pd = &per_cpu(ps3_private, cpu);
6052832a81dSGeoff Levand 
606407e24a0SGeoff Levand 		lv1_get_logical_ppe_id(&pd->node);
607407e24a0SGeoff Levand 		pd->cpu = get_hard_smp_processor_id(cpu);
6082832a81dSGeoff Levand 		spin_lock_init(&pd->bmp.lock);
6092832a81dSGeoff Levand 
610407e24a0SGeoff Levand 		pr_debug("%s:%d: node %lu, cpu %d, bmp %lxh\n", __func__,
611407e24a0SGeoff Levand 			__LINE__, pd->node, pd->cpu,
612407e24a0SGeoff Levand 			ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
613407e24a0SGeoff Levand 
614407e24a0SGeoff Levand 		result = lv1_configure_irq_state_bitmap(pd->node, pd->cpu,
615407e24a0SGeoff Levand 			ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
6162832a81dSGeoff Levand 
6172832a81dSGeoff Levand 		if (result)
6182832a81dSGeoff Levand 			pr_debug("%s:%d: lv1_configure_irq_state_bitmap failed:"
6192832a81dSGeoff Levand 				" %s\n", __func__, __LINE__,
6202832a81dSGeoff Levand 				ps3_result(result));
6212832a81dSGeoff Levand 	}
6222832a81dSGeoff Levand 
6232832a81dSGeoff Levand 	ppc_md.get_irq = ps3_get_irq;
6242832a81dSGeoff Levand }
625