xref: /openbmc/linux/arch/powerpc/platforms/ps3/interrupt.c (revision aab835007097122c3a1e7a7dddda0cf89a94cd4e)
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/lv1call.h>
2842d284bcSStephen Rothwell #include <asm/smp.h>
292832a81dSGeoff Levand 
302832a81dSGeoff Levand #include "platform.h"
312832a81dSGeoff Levand 
322832a81dSGeoff Levand #if defined(DEBUG)
3383bb643dSGeert Uytterhoeven #define DBG udbg_printf
342832a81dSGeoff Levand #else
3583bb643dSGeert Uytterhoeven #define DBG pr_debug
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().
5157715765SGeoff Levand  * The HV requires that the 512 bits of status + mask not cross a page
5257715765SGeoff Levand  * boundary.  PS3_BMP_MINALIGN is used to define this minimal 64 byte
5357715765SGeoff 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 
6557715765SGeoff Levand #define PS3_BMP_MINALIGN 64
6657715765SGeoff 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
81*aab83500SGeoff Levand  * @ppe_id: HV logical_ppe_id
82*aab83500SGeoff Levand  * @thread_id: HV thread_id
83861be32cSGeoff Levand  */
84861be32cSGeoff Levand 
85861be32cSGeoff Levand struct ps3_private {
8657715765SGeoff Levand 	struct ps3_bmp bmp __attribute__ ((aligned (PS3_BMP_MINALIGN)));
87*aab83500SGeoff Levand 	u64 ppe_id;
88*aab83500SGeoff Levand 	u64 thread_id;
89861be32cSGeoff Levand };
90861be32cSGeoff Levand 
91861be32cSGeoff Levand static DEFINE_PER_CPU(struct ps3_private, ps3_private);
92861be32cSGeoff Levand 
93dc4f60c2SGeoff Levand /**
94743c1bb0SGeoff Levand  * ps3_chip_mask - Set an interrupt mask bit in ps3_bmp.
95743c1bb0SGeoff Levand  * @virq: The assigned Linux virq.
96743c1bb0SGeoff Levand  *
97743c1bb0SGeoff Levand  * Sets ps3_bmp.mask and calls lv1_did_update_interrupt_mask().
98743c1bb0SGeoff Levand  */
99743c1bb0SGeoff Levand 
100743c1bb0SGeoff Levand static void ps3_chip_mask(unsigned int virq)
101743c1bb0SGeoff Levand {
102743c1bb0SGeoff Levand 	struct ps3_private *pd = get_irq_chip_data(virq);
103743c1bb0SGeoff Levand 	u64 bit = 0x8000000000000000UL >> virq;
104743c1bb0SGeoff Levand 	u64 *p = &pd->bmp.mask;
105743c1bb0SGeoff Levand 	u64 old;
106743c1bb0SGeoff Levand 	unsigned long flags;
107743c1bb0SGeoff Levand 
108*aab83500SGeoff Levand 	pr_debug("%s:%d: thread_id %lu, virq %d\n", __func__, __LINE__,
109*aab83500SGeoff Levand 		pd->thread_id, virq);
110743c1bb0SGeoff Levand 
111743c1bb0SGeoff Levand 	local_irq_save(flags);
112743c1bb0SGeoff Levand 	asm volatile(
113743c1bb0SGeoff Levand 		     "1:	ldarx %0,0,%3\n"
114743c1bb0SGeoff Levand 		     "andc	%0,%0,%2\n"
115743c1bb0SGeoff Levand 		     "stdcx.	%0,0,%3\n"
116743c1bb0SGeoff Levand 		     "bne-	1b"
117743c1bb0SGeoff Levand 		     : "=&r" (old), "+m" (*p)
118743c1bb0SGeoff Levand 		     : "r" (bit), "r" (p)
119743c1bb0SGeoff Levand 		     : "cc" );
120743c1bb0SGeoff Levand 
121*aab83500SGeoff Levand 	lv1_did_update_interrupt_mask(pd->ppe_id, pd->thread_id);
122743c1bb0SGeoff Levand 	local_irq_restore(flags);
123743c1bb0SGeoff Levand }
124743c1bb0SGeoff Levand 
125743c1bb0SGeoff Levand /**
126743c1bb0SGeoff Levand  * ps3_chip_unmask - Clear an interrupt mask bit in ps3_bmp.
127743c1bb0SGeoff Levand  * @virq: The assigned Linux virq.
128743c1bb0SGeoff Levand  *
129743c1bb0SGeoff Levand  * Clears ps3_bmp.mask and calls lv1_did_update_interrupt_mask().
130743c1bb0SGeoff Levand  */
131743c1bb0SGeoff Levand 
132743c1bb0SGeoff Levand static void ps3_chip_unmask(unsigned int virq)
133743c1bb0SGeoff Levand {
134743c1bb0SGeoff Levand 	struct ps3_private *pd = get_irq_chip_data(virq);
135743c1bb0SGeoff Levand 	u64 bit = 0x8000000000000000UL >> virq;
136743c1bb0SGeoff Levand 	u64 *p = &pd->bmp.mask;
137743c1bb0SGeoff Levand 	u64 old;
138743c1bb0SGeoff Levand 	unsigned long flags;
139743c1bb0SGeoff Levand 
140*aab83500SGeoff Levand 	pr_debug("%s:%d: thread_id %lu, virq %d\n", __func__, __LINE__,
141*aab83500SGeoff Levand 		pd->thread_id, virq);
142743c1bb0SGeoff Levand 
143743c1bb0SGeoff Levand 	local_irq_save(flags);
144743c1bb0SGeoff Levand 	asm volatile(
145743c1bb0SGeoff Levand 		     "1:	ldarx %0,0,%3\n"
146743c1bb0SGeoff Levand 		     "or	%0,%0,%2\n"
147743c1bb0SGeoff Levand 		     "stdcx.	%0,0,%3\n"
148743c1bb0SGeoff Levand 		     "bne-	1b"
149743c1bb0SGeoff Levand 		     : "=&r" (old), "+m" (*p)
150743c1bb0SGeoff Levand 		     : "r" (bit), "r" (p)
151743c1bb0SGeoff Levand 		     : "cc" );
152743c1bb0SGeoff Levand 
153*aab83500SGeoff Levand 	lv1_did_update_interrupt_mask(pd->ppe_id, pd->thread_id);
154743c1bb0SGeoff Levand 	local_irq_restore(flags);
155743c1bb0SGeoff Levand }
156743c1bb0SGeoff Levand 
157743c1bb0SGeoff Levand /**
158743c1bb0SGeoff Levand  * ps3_chip_eoi - HV end-of-interrupt.
159743c1bb0SGeoff Levand  * @virq: The assigned Linux virq.
160743c1bb0SGeoff Levand  *
161743c1bb0SGeoff Levand  * Calls lv1_end_of_interrupt_ext().
162743c1bb0SGeoff Levand  */
163743c1bb0SGeoff Levand 
164743c1bb0SGeoff Levand static void ps3_chip_eoi(unsigned int virq)
165743c1bb0SGeoff Levand {
166743c1bb0SGeoff Levand 	const struct ps3_private *pd = get_irq_chip_data(virq);
167*aab83500SGeoff Levand 	lv1_end_of_interrupt_ext(pd->ppe_id, pd->thread_id, virq);
168743c1bb0SGeoff Levand }
169743c1bb0SGeoff Levand 
170743c1bb0SGeoff Levand /**
171743c1bb0SGeoff Levand  * ps3_irq_chip - Represents the ps3_bmp as a Linux struct irq_chip.
172743c1bb0SGeoff Levand  */
173743c1bb0SGeoff Levand 
174743c1bb0SGeoff Levand static struct irq_chip ps3_irq_chip = {
175743c1bb0SGeoff Levand 	.typename = "ps3",
176743c1bb0SGeoff Levand 	.mask = ps3_chip_mask,
177743c1bb0SGeoff Levand 	.unmask = ps3_chip_unmask,
178743c1bb0SGeoff Levand 	.eoi = ps3_chip_eoi,
179743c1bb0SGeoff Levand };
180743c1bb0SGeoff Levand 
181743c1bb0SGeoff Levand /**
182dc4f60c2SGeoff Levand  * ps3_virq_setup - virq related setup.
183dc4f60c2SGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
184dc4f60c2SGeoff Levand  * serviced on.
185dc4f60c2SGeoff Levand  * @outlet: The HV outlet from the various create outlet routines.
186dc4f60c2SGeoff Levand  * @virq: The assigned Linux virq.
187dc4f60c2SGeoff Levand  *
188dc4f60c2SGeoff Levand  * Calls irq_create_mapping() to get a virq and sets the chip data to
189dc4f60c2SGeoff Levand  * ps3_private data.
190dc4f60c2SGeoff Levand  */
191dc4f60c2SGeoff Levand 
192dc4f60c2SGeoff Levand int ps3_virq_setup(enum ps3_cpu_binding cpu, unsigned long outlet,
193861be32cSGeoff Levand 	unsigned int *virq)
194861be32cSGeoff Levand {
195861be32cSGeoff Levand 	int result;
196861be32cSGeoff Levand 	struct ps3_private *pd;
197861be32cSGeoff Levand 
198861be32cSGeoff Levand 	/* This defines the default interrupt distribution policy. */
199861be32cSGeoff Levand 
200861be32cSGeoff Levand 	if (cpu == PS3_BINDING_CPU_ANY)
201861be32cSGeoff Levand 		cpu = 0;
202861be32cSGeoff Levand 
203861be32cSGeoff Levand 	pd = &per_cpu(ps3_private, cpu);
204861be32cSGeoff Levand 
205861be32cSGeoff Levand 	*virq = irq_create_mapping(NULL, outlet);
206861be32cSGeoff Levand 
207861be32cSGeoff Levand 	if (*virq == NO_IRQ) {
208861be32cSGeoff Levand 		pr_debug("%s:%d: irq_create_mapping failed: outlet %lu\n",
209861be32cSGeoff Levand 			__func__, __LINE__, outlet);
210861be32cSGeoff Levand 		result = -ENOMEM;
211861be32cSGeoff Levand 		goto fail_create;
212861be32cSGeoff Levand 	}
213861be32cSGeoff Levand 
214861be32cSGeoff Levand 	pr_debug("%s:%d: outlet %lu => cpu %u, virq %u\n", __func__, __LINE__,
215861be32cSGeoff Levand 		outlet, cpu, *virq);
216861be32cSGeoff Levand 
217861be32cSGeoff Levand 	result = set_irq_chip_data(*virq, pd);
218861be32cSGeoff Levand 
219861be32cSGeoff Levand 	if (result) {
220861be32cSGeoff Levand 		pr_debug("%s:%d: set_irq_chip_data failed\n",
221861be32cSGeoff Levand 			__func__, __LINE__);
222861be32cSGeoff Levand 		goto fail_set;
223861be32cSGeoff Levand 	}
224861be32cSGeoff Levand 
2259263e85aSGeoff Levand 	ps3_chip_mask(*virq);
2269263e85aSGeoff Levand 
227861be32cSGeoff Levand 	return result;
228861be32cSGeoff Levand 
229861be32cSGeoff Levand fail_set:
230861be32cSGeoff Levand 	irq_dispose_mapping(*virq);
231861be32cSGeoff Levand fail_create:
232861be32cSGeoff Levand 	return result;
233861be32cSGeoff Levand }
234861be32cSGeoff Levand 
235dc4f60c2SGeoff Levand /**
236dc4f60c2SGeoff Levand  * ps3_virq_destroy - virq related teardown.
237dc4f60c2SGeoff Levand  * @virq: The assigned Linux virq.
238dc4f60c2SGeoff Levand  *
239dc4f60c2SGeoff Levand  * Clears chip data and calls irq_dispose_mapping() for the virq.
240dc4f60c2SGeoff Levand  */
241dc4f60c2SGeoff Levand 
242dc4f60c2SGeoff Levand int ps3_virq_destroy(unsigned int virq)
243dc4f60c2SGeoff Levand {
244dc4f60c2SGeoff Levand 	const struct ps3_private *pd = get_irq_chip_data(virq);
245dc4f60c2SGeoff Levand 
246*aab83500SGeoff Levand 	pr_debug("%s:%d: ppe_id %lu, thread_id %lu, virq %u\n", __func__,
247*aab83500SGeoff Levand 		__LINE__, pd->ppe_id, pd->thread_id, virq);
248dc4f60c2SGeoff Levand 
249dc4f60c2SGeoff Levand 	set_irq_chip_data(virq, NULL);
250dc4f60c2SGeoff Levand 	irq_dispose_mapping(virq);
251dc4f60c2SGeoff Levand 
252dc4f60c2SGeoff Levand 	pr_debug("%s:%d <-\n", __func__, __LINE__);
253dc4f60c2SGeoff Levand 	return 0;
254dc4f60c2SGeoff Levand }
255dc4f60c2SGeoff Levand 
256dc4f60c2SGeoff Levand /**
257dc4f60c2SGeoff Levand  * ps3_irq_plug_setup - Generic outlet and virq related setup.
258dc4f60c2SGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
259dc4f60c2SGeoff Levand  * serviced on.
260dc4f60c2SGeoff Levand  * @outlet: The HV outlet from the various create outlet routines.
261dc4f60c2SGeoff Levand  * @virq: The assigned Linux virq.
262dc4f60c2SGeoff Levand  *
263dc4f60c2SGeoff Levand  * Sets up virq and connects the irq plug.
264dc4f60c2SGeoff Levand  */
265dc4f60c2SGeoff Levand 
266dc4f60c2SGeoff Levand int ps3_irq_plug_setup(enum ps3_cpu_binding cpu, unsigned long outlet,
267dc4f60c2SGeoff Levand 	unsigned int *virq)
268dc4f60c2SGeoff Levand {
269dc4f60c2SGeoff Levand 	int result;
270dc4f60c2SGeoff Levand 	struct ps3_private *pd;
271dc4f60c2SGeoff Levand 
272dc4f60c2SGeoff Levand 	result = ps3_virq_setup(cpu, outlet, virq);
273dc4f60c2SGeoff Levand 
274dc4f60c2SGeoff Levand 	if (result) {
275dc4f60c2SGeoff Levand 		pr_debug("%s:%d: ps3_virq_setup failed\n", __func__, __LINE__);
276dc4f60c2SGeoff Levand 		goto fail_setup;
277dc4f60c2SGeoff Levand 	}
278dc4f60c2SGeoff Levand 
279dc4f60c2SGeoff Levand 	pd = get_irq_chip_data(*virq);
280dc4f60c2SGeoff Levand 
281dc4f60c2SGeoff Levand 	/* Binds outlet to cpu + virq. */
282dc4f60c2SGeoff Levand 
283*aab83500SGeoff Levand 	result = lv1_connect_irq_plug_ext(pd->ppe_id, pd->thread_id, *virq,
284*aab83500SGeoff Levand 		outlet, 0);
285dc4f60c2SGeoff Levand 
286dc4f60c2SGeoff Levand 	if (result) {
287dc4f60c2SGeoff Levand 		pr_info("%s:%d: lv1_connect_irq_plug_ext failed: %s\n",
288dc4f60c2SGeoff Levand 		__func__, __LINE__, ps3_result(result));
289dc4f60c2SGeoff Levand 		result = -EPERM;
290dc4f60c2SGeoff Levand 		goto fail_connect;
291dc4f60c2SGeoff Levand 	}
292dc4f60c2SGeoff Levand 
293dc4f60c2SGeoff Levand 	return result;
294dc4f60c2SGeoff Levand 
295dc4f60c2SGeoff Levand fail_connect:
296dc4f60c2SGeoff Levand 	ps3_virq_destroy(*virq);
297dc4f60c2SGeoff Levand fail_setup:
298dc4f60c2SGeoff Levand 	return result;
299dc4f60c2SGeoff Levand }
300dc4f60c2SGeoff Levand EXPORT_SYMBOL_GPL(ps3_irq_plug_setup);
301dc4f60c2SGeoff Levand 
302dc4f60c2SGeoff Levand /**
303dc4f60c2SGeoff Levand  * ps3_irq_plug_destroy - Generic outlet and virq related teardown.
304dc4f60c2SGeoff Levand  * @virq: The assigned Linux virq.
305dc4f60c2SGeoff Levand  *
306dc4f60c2SGeoff Levand  * Disconnects the irq plug and tears down virq.
307dc4f60c2SGeoff Levand  * Do not call for system bus event interrupts setup with
308dc4f60c2SGeoff Levand  * ps3_sb_event_receive_port_setup().
309dc4f60c2SGeoff Levand  */
310dc4f60c2SGeoff Levand 
311dc4f60c2SGeoff Levand int ps3_irq_plug_destroy(unsigned int virq)
312861be32cSGeoff Levand {
313861be32cSGeoff Levand 	int result;
314861be32cSGeoff Levand 	const struct ps3_private *pd = get_irq_chip_data(virq);
315861be32cSGeoff Levand 
316*aab83500SGeoff Levand 	pr_debug("%s:%d: ppe_id %lu, thread_id %lu, virq %u\n", __func__,
317*aab83500SGeoff Levand 		__LINE__, pd->ppe_id, pd->thread_id, virq);
318861be32cSGeoff Levand 
3199263e85aSGeoff Levand 	ps3_chip_mask(virq);
3209263e85aSGeoff Levand 
321*aab83500SGeoff Levand 	result = lv1_disconnect_irq_plug_ext(pd->ppe_id, pd->thread_id, virq);
322861be32cSGeoff Levand 
323861be32cSGeoff Levand 	if (result)
324861be32cSGeoff Levand 		pr_info("%s:%d: lv1_disconnect_irq_plug_ext failed: %s\n",
325861be32cSGeoff Levand 		__func__, __LINE__, ps3_result(result));
326861be32cSGeoff Levand 
327dc4f60c2SGeoff Levand 	ps3_virq_destroy(virq);
328dc4f60c2SGeoff Levand 
329b1eeb38eSGeert Uytterhoeven 	return result;
330861be32cSGeoff Levand }
331dc4f60c2SGeoff Levand EXPORT_SYMBOL_GPL(ps3_irq_plug_destroy);
332861be32cSGeoff Levand 
333861be32cSGeoff Levand /**
334dc4f60c2SGeoff Levand  * ps3_event_receive_port_setup - Setup an event receive port.
335861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
336861be32cSGeoff Levand  * serviced on.
3372832a81dSGeoff Levand  * @virq: The assigned Linux virq.
3382832a81dSGeoff Levand  *
3392832a81dSGeoff Levand  * The virq can be used with lv1_connect_interrupt_event_receive_port() to
340dc4f60c2SGeoff Levand  * arrange to receive interrupts from system-bus devices, or with
341dc4f60c2SGeoff Levand  * ps3_send_event_locally() to signal events.
3422832a81dSGeoff Levand  */
3432832a81dSGeoff Levand 
344dc4f60c2SGeoff Levand int ps3_event_receive_port_setup(enum ps3_cpu_binding cpu, unsigned int *virq)
3452832a81dSGeoff Levand {
3462832a81dSGeoff Levand 	int result;
3472832a81dSGeoff Levand 	unsigned long outlet;
3482832a81dSGeoff Levand 
3492832a81dSGeoff Levand 	result = lv1_construct_event_receive_port(&outlet);
3502832a81dSGeoff Levand 
3512832a81dSGeoff Levand 	if (result) {
3522832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_construct_event_receive_port failed: %s\n",
3532832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
3542832a81dSGeoff Levand 		*virq = NO_IRQ;
3552832a81dSGeoff Levand 		return result;
3562832a81dSGeoff Levand 	}
3572832a81dSGeoff Levand 
358dc4f60c2SGeoff Levand 	result = ps3_irq_plug_setup(cpu, outlet, virq);
359861be32cSGeoff Levand 	BUG_ON(result);
3602832a81dSGeoff Levand 
361861be32cSGeoff Levand 	return result;
3622832a81dSGeoff Levand }
363dc4f60c2SGeoff Levand EXPORT_SYMBOL_GPL(ps3_event_receive_port_setup);
3642832a81dSGeoff Levand 
365dc4f60c2SGeoff Levand /**
366dc4f60c2SGeoff Levand  * ps3_event_receive_port_destroy - Destroy an event receive port.
367dc4f60c2SGeoff Levand  * @virq: The assigned Linux virq.
368dc4f60c2SGeoff Levand  *
369dc4f60c2SGeoff Levand  * Since ps3_event_receive_port_destroy destroys the receive port outlet,
370dc4f60c2SGeoff Levand  * SB devices need to call disconnect_interrupt_event_receive_port() before
371dc4f60c2SGeoff Levand  * this.
372dc4f60c2SGeoff Levand  */
373dc4f60c2SGeoff Levand 
374dc4f60c2SGeoff Levand int ps3_event_receive_port_destroy(unsigned int virq)
3752832a81dSGeoff Levand {
3762832a81dSGeoff Levand 	int result;
3772832a81dSGeoff Levand 
3789263e85aSGeoff Levand 	pr_debug(" -> %s:%d virq %u\n", __func__, __LINE__, virq);
3799263e85aSGeoff Levand 
3809263e85aSGeoff Levand 	ps3_chip_mask(virq);
3812832a81dSGeoff Levand 
3822832a81dSGeoff Levand 	result = lv1_destruct_event_receive_port(virq_to_hw(virq));
3832832a81dSGeoff Levand 
3842832a81dSGeoff Levand 	if (result)
3852832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_destruct_event_receive_port failed: %s\n",
3862832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
3872832a81dSGeoff Levand 
3889263e85aSGeoff Levand 	/*
3899263e85aSGeoff Levand 	 * Don't call ps3_virq_destroy() here since ps3_smp_cleanup_cpu()
3909263e85aSGeoff Levand 	 * calls from interrupt context (smp_call_function) when kexecing.
391dc4f60c2SGeoff Levand 	 */
392dc4f60c2SGeoff Levand 
3932832a81dSGeoff Levand 	pr_debug(" <- %s:%d\n", __func__, __LINE__);
3942832a81dSGeoff Levand 	return result;
3952832a81dSGeoff Levand }
3962832a81dSGeoff Levand 
3972832a81dSGeoff Levand int ps3_send_event_locally(unsigned int virq)
3982832a81dSGeoff Levand {
3992832a81dSGeoff Levand 	return lv1_send_event_locally(virq_to_hw(virq));
4002832a81dSGeoff Levand }
4012832a81dSGeoff Levand 
4022832a81dSGeoff Levand /**
403dc4f60c2SGeoff Levand  * ps3_sb_event_receive_port_setup - Setup a system bus event receive port.
404861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
405861be32cSGeoff Levand  * serviced on.
4066bb5cf10SGeoff Levand  * @dev: The system bus device instance.
4072832a81dSGeoff Levand  * @virq: The assigned Linux virq.
4082832a81dSGeoff Levand  *
4092832a81dSGeoff Levand  * An event irq represents a virtual device interrupt.  The interrupt_id
4102832a81dSGeoff Levand  * coresponds to the software interrupt number.
4112832a81dSGeoff Levand  */
4122832a81dSGeoff Levand 
4136bb5cf10SGeoff Levand int ps3_sb_event_receive_port_setup(struct ps3_system_bus_device *dev,
4146bb5cf10SGeoff Levand 	enum ps3_cpu_binding cpu, unsigned int *virq)
4152832a81dSGeoff Levand {
416dc4f60c2SGeoff Levand 	/* this should go in system-bus.c */
417dc4f60c2SGeoff Levand 
4182832a81dSGeoff Levand 	int result;
4192832a81dSGeoff Levand 
420dc4f60c2SGeoff Levand 	result = ps3_event_receive_port_setup(cpu, virq);
4212832a81dSGeoff Levand 
4222832a81dSGeoff Levand 	if (result)
4232832a81dSGeoff Levand 		return result;
4242832a81dSGeoff Levand 
4256bb5cf10SGeoff Levand 	result = lv1_connect_interrupt_event_receive_port(dev->bus_id,
4266bb5cf10SGeoff Levand 		dev->dev_id, virq_to_hw(*virq), dev->interrupt_id);
4272832a81dSGeoff Levand 
4282832a81dSGeoff Levand 	if (result) {
4292832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_connect_interrupt_event_receive_port"
4302832a81dSGeoff Levand 			" failed: %s\n", __func__, __LINE__,
4312832a81dSGeoff Levand 			ps3_result(result));
432dc4f60c2SGeoff Levand 		ps3_event_receive_port_destroy(*virq);
4332832a81dSGeoff Levand 		*virq = NO_IRQ;
4342832a81dSGeoff Levand 		return result;
4352832a81dSGeoff Levand 	}
4362832a81dSGeoff Levand 
4372832a81dSGeoff Levand 	pr_debug("%s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
4386bb5cf10SGeoff Levand 		dev->interrupt_id, *virq);
4392832a81dSGeoff Levand 
4402832a81dSGeoff Levand 	return 0;
4412832a81dSGeoff Levand }
442dc4f60c2SGeoff Levand EXPORT_SYMBOL(ps3_sb_event_receive_port_setup);
4432832a81dSGeoff Levand 
4446bb5cf10SGeoff Levand int ps3_sb_event_receive_port_destroy(struct ps3_system_bus_device *dev,
4456bb5cf10SGeoff Levand 	unsigned int virq)
4462832a81dSGeoff Levand {
447dc4f60c2SGeoff Levand 	/* this should go in system-bus.c */
448dc4f60c2SGeoff Levand 
4492832a81dSGeoff Levand 	int result;
4502832a81dSGeoff Levand 
4512832a81dSGeoff Levand 	pr_debug(" -> %s:%d: interrupt_id %u, virq %u\n", __func__, __LINE__,
4526bb5cf10SGeoff Levand 		dev->interrupt_id, virq);
4532832a81dSGeoff Levand 
4546bb5cf10SGeoff Levand 	result = lv1_disconnect_interrupt_event_receive_port(dev->bus_id,
4556bb5cf10SGeoff Levand 		dev->dev_id, virq_to_hw(virq), dev->interrupt_id);
4562832a81dSGeoff Levand 
4572832a81dSGeoff Levand 	if (result)
4582832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_disconnect_interrupt_event_receive_port"
4592832a81dSGeoff Levand 			" failed: %s\n", __func__, __LINE__,
4602832a81dSGeoff Levand 			ps3_result(result));
4612832a81dSGeoff Levand 
462dc4f60c2SGeoff Levand 	result = ps3_event_receive_port_destroy(virq);
463dc4f60c2SGeoff Levand 	BUG_ON(result);
4642832a81dSGeoff Levand 
4659263e85aSGeoff Levand 	/*
4669263e85aSGeoff Levand 	 * ps3_event_receive_port_destroy() destroys the IRQ plug,
4679263e85aSGeoff Levand 	 * so don't call ps3_irq_plug_destroy() here.
4689263e85aSGeoff Levand 	 */
4699263e85aSGeoff Levand 
4709263e85aSGeoff Levand 	result = ps3_virq_destroy(virq);
4719263e85aSGeoff Levand 	BUG_ON(result);
4729263e85aSGeoff Levand 
4732832a81dSGeoff Levand 	pr_debug(" <- %s:%d\n", __func__, __LINE__);
4742832a81dSGeoff Levand 	return result;
4752832a81dSGeoff Levand }
476dc4f60c2SGeoff Levand EXPORT_SYMBOL(ps3_sb_event_receive_port_destroy);
4772832a81dSGeoff Levand 
4782832a81dSGeoff Levand /**
479dc4f60c2SGeoff Levand  * ps3_io_irq_setup - Setup a system bus io irq.
480dc4f60c2SGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
481dc4f60c2SGeoff Levand  * serviced on.
482dc4f60c2SGeoff Levand  * @interrupt_id: The device interrupt id read from the system repository.
483dc4f60c2SGeoff Levand  * @virq: The assigned Linux virq.
484dc4f60c2SGeoff Levand  *
485dc4f60c2SGeoff Levand  * An io irq represents a non-virtualized device interrupt.  interrupt_id
486dc4f60c2SGeoff Levand  * coresponds to the interrupt number of the interrupt controller.
487dc4f60c2SGeoff Levand  */
488dc4f60c2SGeoff Levand 
489dc4f60c2SGeoff Levand int ps3_io_irq_setup(enum ps3_cpu_binding cpu, unsigned int interrupt_id,
490dc4f60c2SGeoff Levand 	unsigned int *virq)
491dc4f60c2SGeoff Levand {
492dc4f60c2SGeoff Levand 	int result;
493dc4f60c2SGeoff Levand 	unsigned long outlet;
494dc4f60c2SGeoff Levand 
495dc4f60c2SGeoff Levand 	result = lv1_construct_io_irq_outlet(interrupt_id, &outlet);
496dc4f60c2SGeoff Levand 
497dc4f60c2SGeoff Levand 	if (result) {
498dc4f60c2SGeoff Levand 		pr_debug("%s:%d: lv1_construct_io_irq_outlet failed: %s\n",
499dc4f60c2SGeoff Levand 			__func__, __LINE__, ps3_result(result));
500dc4f60c2SGeoff Levand 		return result;
501dc4f60c2SGeoff Levand 	}
502dc4f60c2SGeoff Levand 
503dc4f60c2SGeoff Levand 	result = ps3_irq_plug_setup(cpu, outlet, virq);
504dc4f60c2SGeoff Levand 	BUG_ON(result);
505dc4f60c2SGeoff Levand 
506dc4f60c2SGeoff Levand 	return result;
507dc4f60c2SGeoff Levand }
508dc4f60c2SGeoff Levand EXPORT_SYMBOL_GPL(ps3_io_irq_setup);
509dc4f60c2SGeoff Levand 
510dc4f60c2SGeoff Levand int ps3_io_irq_destroy(unsigned int virq)
511dc4f60c2SGeoff Levand {
512dc4f60c2SGeoff Levand 	int result;
5139263e85aSGeoff Levand 	unsigned long outlet = virq_to_hw(virq);
514dc4f60c2SGeoff Levand 
5159263e85aSGeoff Levand 	ps3_chip_mask(virq);
5169263e85aSGeoff Levand 
5179263e85aSGeoff Levand 	/*
5189263e85aSGeoff Levand 	 * lv1_destruct_io_irq_outlet() will destroy the IRQ plug,
5199263e85aSGeoff Levand 	 * so call ps3_irq_plug_destroy() first.
5209263e85aSGeoff Levand 	 */
5219263e85aSGeoff Levand 
5229263e85aSGeoff Levand 	result = ps3_irq_plug_destroy(virq);
5239263e85aSGeoff Levand 	BUG_ON(result);
5249263e85aSGeoff Levand 
5259263e85aSGeoff Levand 	result = lv1_destruct_io_irq_outlet(outlet);
526dc4f60c2SGeoff Levand 
527dc4f60c2SGeoff Levand 	if (result)
528dc4f60c2SGeoff Levand 		pr_debug("%s:%d: lv1_destruct_io_irq_outlet failed: %s\n",
529dc4f60c2SGeoff Levand 			__func__, __LINE__, ps3_result(result));
530dc4f60c2SGeoff Levand 
531dc4f60c2SGeoff Levand 	return result;
532dc4f60c2SGeoff Levand }
533dc4f60c2SGeoff Levand EXPORT_SYMBOL_GPL(ps3_io_irq_destroy);
534dc4f60c2SGeoff Levand 
535dc4f60c2SGeoff Levand /**
536dc4f60c2SGeoff Levand  * ps3_vuart_irq_setup - Setup the system virtual uart virq.
537861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
538861be32cSGeoff Levand  * serviced on.
5392832a81dSGeoff Levand  * @virt_addr_bmp: The caller supplied virtual uart interrupt bitmap.
5402832a81dSGeoff Levand  * @virq: The assigned Linux virq.
5412832a81dSGeoff Levand  *
5422832a81dSGeoff Levand  * The system supports only a single virtual uart, so multiple calls without
5432832a81dSGeoff Levand  * freeing the interrupt will return a wrong state error.
5442832a81dSGeoff Levand  */
5452832a81dSGeoff Levand 
546dc4f60c2SGeoff Levand int ps3_vuart_irq_setup(enum ps3_cpu_binding cpu, void* virt_addr_bmp,
547861be32cSGeoff Levand 	unsigned int *virq)
5482832a81dSGeoff Levand {
5492832a81dSGeoff Levand 	int result;
5502832a81dSGeoff Levand 	unsigned long outlet;
551861be32cSGeoff Levand 	u64 lpar_addr;
5522832a81dSGeoff Levand 
553861be32cSGeoff Levand 	BUG_ON(!is_kernel_addr((u64)virt_addr_bmp));
5542832a81dSGeoff Levand 
5552832a81dSGeoff Levand 	lpar_addr = ps3_mm_phys_to_lpar(__pa(virt_addr_bmp));
5562832a81dSGeoff Levand 
5572832a81dSGeoff Levand 	result = lv1_configure_virtual_uart_irq(lpar_addr, &outlet);
5582832a81dSGeoff Levand 
5592832a81dSGeoff Levand 	if (result) {
5602832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
5612832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
5622832a81dSGeoff Levand 		return result;
5632832a81dSGeoff Levand 	}
5642832a81dSGeoff Levand 
565dc4f60c2SGeoff Levand 	result = ps3_irq_plug_setup(cpu, outlet, virq);
566861be32cSGeoff Levand 	BUG_ON(result);
5672832a81dSGeoff Levand 
568861be32cSGeoff Levand 	return result;
5692832a81dSGeoff Levand }
5707626e78dSGeoff Levand EXPORT_SYMBOL_GPL(ps3_vuart_irq_setup);
5712832a81dSGeoff Levand 
572dc4f60c2SGeoff Levand int ps3_vuart_irq_destroy(unsigned int virq)
5732832a81dSGeoff Levand {
5742832a81dSGeoff Levand 	int result;
5752832a81dSGeoff Levand 
5769263e85aSGeoff Levand 	ps3_chip_mask(virq);
5772832a81dSGeoff Levand 	result = lv1_deconfigure_virtual_uart_irq();
5782832a81dSGeoff Levand 
5792832a81dSGeoff Levand 	if (result) {
5802832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_configure_virtual_uart_irq failed: %s\n",
5812832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
5822832a81dSGeoff Levand 		return result;
5832832a81dSGeoff Levand 	}
5842832a81dSGeoff Levand 
585dc4f60c2SGeoff Levand 	result = ps3_irq_plug_destroy(virq);
586dc4f60c2SGeoff Levand 	BUG_ON(result);
5872832a81dSGeoff Levand 
5882832a81dSGeoff Levand 	return result;
5892832a81dSGeoff Levand }
5907626e78dSGeoff Levand EXPORT_SYMBOL_GPL(ps3_vuart_irq_destroy);
5912832a81dSGeoff Levand 
5922832a81dSGeoff Levand /**
593dc4f60c2SGeoff Levand  * ps3_spe_irq_setup - Setup an spe virq.
594861be32cSGeoff Levand  * @cpu: enum ps3_cpu_binding indicating the cpu the interrupt should be
595861be32cSGeoff Levand  * serviced on.
5962832a81dSGeoff Levand  * @spe_id: The spe_id returned from lv1_construct_logical_spe().
5972832a81dSGeoff Levand  * @class: The spe interrupt class {0,1,2}.
5982832a81dSGeoff Levand  * @virq: The assigned Linux virq.
5992832a81dSGeoff Levand  *
6002832a81dSGeoff Levand  */
6012832a81dSGeoff Levand 
602dc4f60c2SGeoff Levand int ps3_spe_irq_setup(enum ps3_cpu_binding cpu, unsigned long spe_id,
603861be32cSGeoff Levand 	unsigned int class, unsigned int *virq)
6042832a81dSGeoff Levand {
6052832a81dSGeoff Levand 	int result;
6062832a81dSGeoff Levand 	unsigned long outlet;
6072832a81dSGeoff Levand 
6082832a81dSGeoff Levand 	BUG_ON(class > 2);
6092832a81dSGeoff Levand 
6102832a81dSGeoff Levand 	result = lv1_get_spe_irq_outlet(spe_id, class, &outlet);
6112832a81dSGeoff Levand 
6122832a81dSGeoff Levand 	if (result) {
6132832a81dSGeoff Levand 		pr_debug("%s:%d: lv1_get_spe_irq_outlet failed: %s\n",
6142832a81dSGeoff Levand 			__func__, __LINE__, ps3_result(result));
6152832a81dSGeoff Levand 		return result;
6162832a81dSGeoff Levand 	}
6172832a81dSGeoff Levand 
618dc4f60c2SGeoff Levand 	result = ps3_irq_plug_setup(cpu, outlet, virq);
619861be32cSGeoff Levand 	BUG_ON(result);
6202832a81dSGeoff Levand 
621861be32cSGeoff Levand 	return result;
6222832a81dSGeoff Levand }
6232832a81dSGeoff Levand 
624dc4f60c2SGeoff Levand int ps3_spe_irq_destroy(unsigned int virq)
6252832a81dSGeoff Levand {
6269263e85aSGeoff Levand 	int result;
6279263e85aSGeoff Levand 
6289263e85aSGeoff Levand 	ps3_chip_mask(virq);
6299263e85aSGeoff Levand 
6309263e85aSGeoff Levand 	result = ps3_irq_plug_destroy(virq);
631dc4f60c2SGeoff Levand 	BUG_ON(result);
6329263e85aSGeoff Levand 
6339263e85aSGeoff Levand 	return result;
6342832a81dSGeoff Levand }
6352832a81dSGeoff Levand 
636b1eeb38eSGeert Uytterhoeven 
6372832a81dSGeoff Levand #define PS3_INVALID_OUTLET ((irq_hw_number_t)-1)
6382832a81dSGeoff Levand #define PS3_PLUG_MAX 63
6392832a81dSGeoff Levand 
6402832a81dSGeoff Levand #if defined(DEBUG)
641861be32cSGeoff Levand static void _dump_64_bmp(const char *header, const u64 *p, unsigned cpu,
6422832a81dSGeoff Levand 	const char* func, int line)
6432832a81dSGeoff Levand {
6442832a81dSGeoff Levand 	pr_debug("%s:%d: %s %u {%04lx_%04lx_%04lx_%04lx}\n",
6452832a81dSGeoff Levand 		func, line, header, cpu,
6462832a81dSGeoff Levand 		*p >> 48, (*p >> 32) & 0xffff, (*p >> 16) & 0xffff,
6472832a81dSGeoff Levand 		*p & 0xffff);
6482832a81dSGeoff Levand }
6492832a81dSGeoff Levand 
650848cfdc5SGeoff Levand static void __maybe_unused _dump_256_bmp(const char *header,
651861be32cSGeoff Levand 	const u64 *p, unsigned cpu, const char* func, int line)
6522832a81dSGeoff Levand {
6532832a81dSGeoff Levand 	pr_debug("%s:%d: %s %u {%016lx:%016lx:%016lx:%016lx}\n",
6542832a81dSGeoff Levand 		func, line, header, cpu, p[0], p[1], p[2], p[3]);
6552832a81dSGeoff Levand }
6562832a81dSGeoff Levand 
6572832a81dSGeoff Levand #define dump_bmp(_x) _dump_bmp(_x, __func__, __LINE__)
6589633ac8dSGeoff Levand static void _dump_bmp(struct ps3_private* pd, const char* func, int line)
6592832a81dSGeoff Levand {
6602832a81dSGeoff Levand 	unsigned long flags;
6612832a81dSGeoff Levand 
6622832a81dSGeoff Levand 	spin_lock_irqsave(&pd->bmp.lock, flags);
663*aab83500SGeoff Levand 	_dump_64_bmp("stat", &pd->bmp.status, pd->thread_id, func, line);
664*aab83500SGeoff Levand 	_dump_64_bmp("mask", &pd->bmp.mask, pd->thread_id, func, line);
6652832a81dSGeoff Levand 	spin_unlock_irqrestore(&pd->bmp.lock, flags);
6662832a81dSGeoff Levand }
6672832a81dSGeoff Levand 
6682832a81dSGeoff Levand #define dump_mask(_x) _dump_mask(_x, __func__, __LINE__)
669848cfdc5SGeoff Levand static void __maybe_unused _dump_mask(struct ps3_private *pd,
6702832a81dSGeoff Levand 	const char* func, int line)
6712832a81dSGeoff Levand {
6722832a81dSGeoff Levand 	unsigned long flags;
6732832a81dSGeoff Levand 
6742832a81dSGeoff Levand 	spin_lock_irqsave(&pd->bmp.lock, flags);
675*aab83500SGeoff Levand 	_dump_64_bmp("mask", &pd->bmp.mask, pd->thread_id, func, line);
6762832a81dSGeoff Levand 	spin_unlock_irqrestore(&pd->bmp.lock, flags);
6772832a81dSGeoff Levand }
6782832a81dSGeoff Levand #else
6799633ac8dSGeoff Levand static void dump_bmp(struct ps3_private* pd) {};
6802832a81dSGeoff Levand #endif /* defined(DEBUG) */
6812832a81dSGeoff Levand 
6829633ac8dSGeoff Levand static void ps3_host_unmap(struct irq_host *h, unsigned int virq)
6832832a81dSGeoff Levand {
684861be32cSGeoff Levand 	set_irq_chip_data(virq, NULL);
6852832a81dSGeoff Levand }
6862832a81dSGeoff Levand 
6879633ac8dSGeoff Levand static int ps3_host_map(struct irq_host *h, unsigned int virq,
6882832a81dSGeoff Levand 	irq_hw_number_t hwirq)
6892832a81dSGeoff Levand {
690861be32cSGeoff Levand 	pr_debug("%s:%d: hwirq %lu, virq %u\n", __func__, __LINE__, hwirq,
691861be32cSGeoff Levand 		virq);
6922832a81dSGeoff Levand 
6939263e85aSGeoff Levand 	set_irq_chip_and_handler(virq, &ps3_irq_chip, handle_fasteoi_irq);
6942832a81dSGeoff Levand 
695861be32cSGeoff Levand 	return 0;
6962832a81dSGeoff Levand }
6972832a81dSGeoff Levand 
6989633ac8dSGeoff Levand static struct irq_host_ops ps3_host_ops = {
6999633ac8dSGeoff Levand 	.map = ps3_host_map,
7009633ac8dSGeoff Levand 	.unmap = ps3_host_unmap,
7012832a81dSGeoff Levand };
7022832a81dSGeoff Levand 
7032832a81dSGeoff Levand void __init ps3_register_ipi_debug_brk(unsigned int cpu, unsigned int virq)
7042832a81dSGeoff Levand {
7059633ac8dSGeoff Levand 	struct ps3_private *pd = &per_cpu(ps3_private, cpu);
7062832a81dSGeoff Levand 
7072832a81dSGeoff Levand 	pd->bmp.ipi_debug_brk_mask = 0x8000000000000000UL >> virq;
7082832a81dSGeoff Levand 
7092832a81dSGeoff Levand 	pr_debug("%s:%d: cpu %u, virq %u, mask %lxh\n", __func__, __LINE__,
7102832a81dSGeoff Levand 		cpu, virq, pd->bmp.ipi_debug_brk_mask);
7112832a81dSGeoff Levand }
7122832a81dSGeoff Levand 
7139263e85aSGeoff Levand static unsigned int ps3_get_irq(void)
7142832a81dSGeoff Levand {
7159cf9e196SBenjamin Herrenschmidt 	struct ps3_private *pd = &__get_cpu_var(ps3_private);
716861be32cSGeoff Levand 	u64 x = (pd->bmp.status & pd->bmp.mask);
7179cf9e196SBenjamin Herrenschmidt 	unsigned int plug;
7182832a81dSGeoff Levand 
7192832a81dSGeoff Levand 	/* check for ipi break first to stop this cpu ASAP */
7202832a81dSGeoff Levand 
7219cf9e196SBenjamin Herrenschmidt 	if (x & pd->bmp.ipi_debug_brk_mask)
7229cf9e196SBenjamin Herrenschmidt 		x &= pd->bmp.ipi_debug_brk_mask;
7232832a81dSGeoff Levand 
7249cf9e196SBenjamin Herrenschmidt 	asm volatile("cntlzd %0,%1" : "=r" (plug) : "r" (x));
7259cf9e196SBenjamin Herrenschmidt 	plug &= 0x3f;
7262832a81dSGeoff Levand 
7279cf9e196SBenjamin Herrenschmidt 	if (unlikely(plug) == NO_IRQ) {
728*aab83500SGeoff Levand 		pr_debug("%s:%d: no plug found: thread_id %lu\n", __func__,
729*aab83500SGeoff Levand 			__LINE__, pd->thread_id);
7309633ac8dSGeoff Levand 		dump_bmp(&per_cpu(ps3_private, 0));
7319633ac8dSGeoff Levand 		dump_bmp(&per_cpu(ps3_private, 1));
7322832a81dSGeoff Levand 		return NO_IRQ;
7332832a81dSGeoff Levand 	}
7342832a81dSGeoff Levand 
7352832a81dSGeoff Levand #if defined(DEBUG)
7369cf9e196SBenjamin Herrenschmidt 	if (unlikely(plug < NUM_ISA_INTERRUPTS || plug > PS3_PLUG_MAX)) {
7379633ac8dSGeoff Levand 		dump_bmp(&per_cpu(ps3_private, 0));
7389633ac8dSGeoff Levand 		dump_bmp(&per_cpu(ps3_private, 1));
7392832a81dSGeoff Levand 		BUG();
7402832a81dSGeoff Levand 	}
7412832a81dSGeoff Levand #endif
7422832a81dSGeoff Levand 	return plug;
7432832a81dSGeoff Levand }
7442832a81dSGeoff Levand 
7452832a81dSGeoff Levand void __init ps3_init_IRQ(void)
7462832a81dSGeoff Levand {
7472832a81dSGeoff Levand 	int result;
7482832a81dSGeoff Levand 	unsigned cpu;
7492832a81dSGeoff Levand 	struct irq_host *host;
7502832a81dSGeoff Levand 
7519633ac8dSGeoff Levand 	host = irq_alloc_host(IRQ_HOST_MAP_NOMAP, 0, &ps3_host_ops,
7522832a81dSGeoff Levand 		PS3_INVALID_OUTLET);
7532832a81dSGeoff Levand 	irq_set_default_host(host);
7542832a81dSGeoff Levand 	irq_set_virq_count(PS3_PLUG_MAX + 1);
7552832a81dSGeoff Levand 
7562832a81dSGeoff Levand 	for_each_possible_cpu(cpu) {
7579633ac8dSGeoff Levand 		struct ps3_private *pd = &per_cpu(ps3_private, cpu);
7582832a81dSGeoff Levand 
759*aab83500SGeoff Levand 		lv1_get_logical_ppe_id(&pd->ppe_id);
760*aab83500SGeoff Levand 		pd->thread_id = get_hard_smp_processor_id(cpu);
7612832a81dSGeoff Levand 		spin_lock_init(&pd->bmp.lock);
7622832a81dSGeoff Levand 
763*aab83500SGeoff Levand 		pr_debug("%s:%d: ppe_id %lu, thread_id %lu, bmp %lxh\n",
764*aab83500SGeoff Levand 			__func__, __LINE__, pd->ppe_id, pd->thread_id,
765407e24a0SGeoff Levand 			ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
766407e24a0SGeoff Levand 
767*aab83500SGeoff Levand 		result = lv1_configure_irq_state_bitmap(pd->ppe_id,
768*aab83500SGeoff Levand 			pd->thread_id, ps3_mm_phys_to_lpar(__pa(&pd->bmp)));
7692832a81dSGeoff Levand 
7702832a81dSGeoff Levand 		if (result)
7712832a81dSGeoff Levand 			pr_debug("%s:%d: lv1_configure_irq_state_bitmap failed:"
7722832a81dSGeoff Levand 				" %s\n", __func__, __LINE__,
7732832a81dSGeoff Levand 				ps3_result(result));
7742832a81dSGeoff Levand 	}
7752832a81dSGeoff Levand 
7762832a81dSGeoff Levand 	ppc_md.get_irq = ps3_get_irq;
7772832a81dSGeoff Levand }
7789263e85aSGeoff Levand 
7799263e85aSGeoff Levand void ps3_shutdown_IRQ(int cpu)
7809263e85aSGeoff Levand {
7819263e85aSGeoff Levand 	int result;
7829263e85aSGeoff Levand 	u64 ppe_id;
7839263e85aSGeoff Levand 	u64 thread_id = get_hard_smp_processor_id(cpu);
7849263e85aSGeoff Levand 
7859263e85aSGeoff Levand 	lv1_get_logical_ppe_id(&ppe_id);
7869263e85aSGeoff Levand 	result = lv1_configure_irq_state_bitmap(ppe_id, thread_id, 0);
7879263e85aSGeoff Levand 
7889263e85aSGeoff Levand 	DBG("%s:%d: lv1_configure_irq_state_bitmap (%lu:%lu/%d) %s\n", __func__,
7899263e85aSGeoff Levand 		__LINE__, ppe_id, thread_id, cpu, ps3_result(result));
7909263e85aSGeoff Levand }
791