irq.c 10.4 KB
Newer Older
1 2 3
/*
 * This module supports the iSeries PCI bus interrupt handling
 * Copyright (C) 20yy  <Robert L Holtorf> <IBM Corp>
4
 * Copyright (C) 2004-2005 IBM Corporation
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the:
 * Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * Change Activity:
 *   Created, December 13, 2000 by Wayne Holm
 * End Change Activity
 */
L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33 34 35
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/threads.h>
#include <linux/smp.h>
#include <linux/param.h>
#include <linux/string.h>
#include <linux/bootmem.h>
#include <linux/irq.h>
#include <linux/spinlock.h>

36
#include <asm/paca.h>
37
#include <asm/iseries/hv_types.h>
38
#include <asm/iseries/hv_lp_event.h>
39
#include <asm/iseries/hv_call_xm.h>
40
#include <asm/iseries/it_lp_queue.h>
41 42

#include "irq.h"
43
#include "pci.h"
44
#include "call_pci.h"
45
#include "smp.h"
46

47 48
#ifdef CONFIG_PCI

49 50 51 52 53 54 55 56 57 58
enum pci_event_type {
	pe_bus_created		= 0,	/* PHB has been created */
	pe_bus_error		= 1,	/* PHB has failed */
	pe_bus_failed		= 2,	/* Msg to Secondary, Primary failed bus */
	pe_node_failed		= 4,	/* Multi-adapter bridge has failed */
	pe_node_recovered	= 5,	/* Multi-adapter bridge has recovered */
	pe_bus_recovered	= 12,	/* PHB has been recovered */
	pe_unquiese_bus		= 18,	/* Secondary bus unqiescing */
	pe_bridge_error		= 21,	/* Bridge Error */
	pe_slot_interrupt	= 22	/* Slot interrupt */
59 60
};

61 62
struct pci_event {
	struct HvLpEvent event;
63
	union {
64
		u64 __align;		/* Align on an 8-byte boundary */
65 66
		struct {
			u32		fisr;
67 68 69 70 71 72 73 74 75 76 77 78 79 80
			HvBusNumber	bus_number;
			HvSubBusNumber	sub_bus_number;
			HvAgentId	dev_id;
		} slot;
		struct {
			HvBusNumber	bus_number;
			HvSubBusNumber	sub_bus_number;
		} bus;
		struct {
			HvBusNumber	bus_number;
			HvSubBusNumber	sub_bus_number;
			HvAgentId	dev_id;
		} node;
	} data;
81 82
};

83 84 85 86
static DEFINE_SPINLOCK(pending_irqs_lock);
static int num_pending_irqs;
static int pending_irqs[NR_IRQS];

O
Olaf Hering 已提交
87
static void int_received(struct pci_event *event)
88 89 90
{
	int irq;

91 92 93
	switch (event->event.xSubtype) {
	case pe_slot_interrupt:
		irq = event->event.xCorrelationToken;
94 95 96 97 98 99 100 101 102 103 104 105
		if (irq < NR_IRQS) {
			spin_lock(&pending_irqs_lock);
			pending_irqs[irq]++;
			num_pending_irqs++;
			spin_unlock(&pending_irqs_lock);
		} else {
			printk(KERN_WARNING "int_received: bad irq number %d\n",
					irq);
			HvCallPci_eoi(event->data.slot.bus_number,
					event->data.slot.sub_bus_number,
					event->data.slot.dev_id);
		}
106 107
		break;
		/* Ignore error recovery events for now */
108 109 110
	case pe_bus_created:
		printk(KERN_INFO "int_received: system bus %d created\n",
			event->data.bus.bus_number);
111
		break;
112 113 114 115
	case pe_bus_error:
	case pe_bus_failed:
		printk(KERN_INFO "int_received: system bus %d failed\n",
			event->data.bus.bus_number);
116
		break;
117 118 119 120
	case pe_bus_recovered:
	case pe_unquiese_bus:
		printk(KERN_INFO "int_received: system bus %d recovered\n",
			event->data.bus.bus_number);
121
		break;
122 123
	case pe_node_failed:
	case pe_bridge_error:
124
		printk(KERN_INFO
125 126 127 128
			"int_received: multi-adapter bridge %d/%d/%d failed\n",
			event->data.node.bus_number,
			event->data.node.sub_bus_number,
			event->data.node.dev_id);
129
		break;
130
	case pe_node_recovered:
131
		printk(KERN_INFO
132 133 134 135
			"int_received: multi-adapter bridge %d/%d/%d recovered\n",
			event->data.node.bus_number,
			event->data.node.sub_bus_number,
			event->data.node.dev_id);
136 137 138
		break;
	default:
		printk(KERN_ERR
139 140
			"int_received: unrecognized event subtype 0x%x\n",
			event->event.xSubtype);
141 142 143 144
		break;
	}
}

O
Olaf Hering 已提交
145
static void pci_event_handler(struct HvLpEvent *event)
146
{
147
	if (event && (event->xType == HvLpEvent_Type_PciIo)) {
148
		if (hvlpevent_is_int(event))
O
Olaf Hering 已提交
149
			int_received((struct pci_event *)event);
150
		else
151
			printk(KERN_ERR
152 153
				"pci_event_handler: unexpected ack received\n");
	} else if (event)
154
		printk(KERN_ERR
155 156
			"pci_event_handler: Unrecognized PCI event type 0x%x\n",
			(int)event->xType);
157
	else
158
		printk(KERN_ERR "pci_event_handler: NULL event received\n");
159 160
}

161
#define REAL_IRQ_TO_SUBBUS(irq)	(((irq) >> 14) & 0xff)
162 163 164 165
#define REAL_IRQ_TO_BUS(irq)	((((irq) >> 6) & 0xff) + 1)
#define REAL_IRQ_TO_IDSEL(irq)	((((irq) >> 3) & 7) + 1)
#define REAL_IRQ_TO_FUNC(irq)	((irq) & 7)

L
Linus Torvalds 已提交
166
/*
167 168
 * This will be called by device drivers (via enable_IRQ)
 * to enable INTA in the bridge interrupt status register.
L
Linus Torvalds 已提交
169
 */
170
static void iseries_enable_IRQ(struct irq_data *d)
L
Linus Torvalds 已提交
171
{
172 173
	u32 bus, dev_id, function, mask;
	const u32 sub_bus = 0;
174
	unsigned int rirq = (unsigned int)irq_map[d->irq].hwirq;
L
Linus Torvalds 已提交
175

176 177 178
	/* The IRQ has already been locked by the caller */
	bus = REAL_IRQ_TO_BUS(rirq);
	function = REAL_IRQ_TO_FUNC(rirq);
179
	dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
L
Linus Torvalds 已提交
180

181 182
	/* Unmask secondary INTA */
	mask = 0x80000000;
183
	HvCallPci_unmaskInterrupts(bus, sub_bus, dev_id, mask);
L
Linus Torvalds 已提交
184 185
}

186
/* This is called by iseries_activate_IRQs */
187
static unsigned int iseries_startup_IRQ(struct irq_data *d)
L
Linus Torvalds 已提交
188
{
189 190
	u32 bus, dev_id, function, mask;
	const u32 sub_bus = 0;
191
	unsigned int rirq = (unsigned int)irq_map[d->irq].hwirq;
L
Linus Torvalds 已提交
192 193 194

	bus = REAL_IRQ_TO_BUS(rirq);
	function = REAL_IRQ_TO_FUNC(rirq);
195
	dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
L
Linus Torvalds 已提交
196 197

	/* Link the IRQ number to the bridge */
198
	HvCallXm_connectBusUnit(bus, sub_bus, dev_id, d->irq);
L
Linus Torvalds 已提交
199 200 201

	/* Unmask bridge interrupts in the FISR */
	mask = 0x01010000 << function;
202
	HvCallPci_unmaskFisr(bus, sub_bus, dev_id, mask);
203
	iseries_enable_IRQ(d);
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216
	return 0;
}

/*
 * This is called out of iSeries_fixup to activate interrupt
 * generation for usable slots
 */
void __init iSeries_activate_IRQs()
{
	int irq;
	unsigned long flags;

	for_each_irq (irq) {
M
Michael Ellerman 已提交
217
		struct irq_desc *desc = irq_to_desc(irq);
218
		struct irq_chip *chip;
L
Linus Torvalds 已提交
219

220 221 222
		if (!desc)
			continue;

223
		chip = irq_desc_get_chip(desc);
224
		if (chip && chip->irq_startup) {
225
			raw_spin_lock_irqsave(&desc->lock, flags);
226
			chip->irq_startup(&desc->irq_data);
227
			raw_spin_unlock_irqrestore(&desc->lock, flags);
L
Linus Torvalds 已提交
228
		}
229
	}
L
Linus Torvalds 已提交
230 231 232
}

/*  this is not called anywhere currently */
233
static void iseries_shutdown_IRQ(struct irq_data *d)
L
Linus Torvalds 已提交
234
{
235 236
	u32 bus, dev_id, function, mask;
	const u32 sub_bus = 0;
237
	unsigned int rirq = (unsigned int)irq_map[d->irq].hwirq;
L
Linus Torvalds 已提交
238 239 240 241

	/* irq should be locked by the caller */
	bus = REAL_IRQ_TO_BUS(rirq);
	function = REAL_IRQ_TO_FUNC(rirq);
242
	dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
L
Linus Torvalds 已提交
243 244

	/* Invalidate the IRQ number in the bridge */
245
	HvCallXm_connectBusUnit(bus, sub_bus, dev_id, 0);
L
Linus Torvalds 已提交
246 247 248

	/* Mask bridge interrupts in the FISR */
	mask = 0x01010000 << function;
249
	HvCallPci_maskFisr(bus, sub_bus, dev_id, mask);
L
Linus Torvalds 已提交
250 251 252 253 254 255
}

/*
 * This will be called by device drivers (via disable_IRQ)
 * to disable INTA in the bridge interrupt status register.
 */
256
static void iseries_disable_IRQ(struct irq_data *d)
L
Linus Torvalds 已提交
257
{
258 259
	u32 bus, dev_id, function, mask;
	const u32 sub_bus = 0;
260
	unsigned int rirq = (unsigned int)irq_map[d->irq].hwirq;
L
Linus Torvalds 已提交
261 262 263 264

	/* The IRQ has already been locked by the caller */
	bus = REAL_IRQ_TO_BUS(rirq);
	function = REAL_IRQ_TO_FUNC(rirq);
265
	dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
L
Linus Torvalds 已提交
266 267 268

	/* Mask secondary INTA   */
	mask = 0x80000000;
269
	HvCallPci_maskInterrupts(bus, sub_bus, dev_id, mask);
L
Linus Torvalds 已提交
270 271
}

272
static void iseries_end_IRQ(struct irq_data *d)
L
Linus Torvalds 已提交
273
{
274
	unsigned int rirq = (unsigned int)irq_map[d->irq].hwirq;
275 276 277

	HvCallPci_eoi(REAL_IRQ_TO_BUS(rirq), REAL_IRQ_TO_SUBBUS(rirq),
		(REAL_IRQ_TO_IDSEL(rirq) << 4) + REAL_IRQ_TO_FUNC(rirq));
L
Linus Torvalds 已提交
278
}
279

280
static struct irq_chip iseries_pic = {
281
	.name		= "iSeries",
282 283 284 285 286
	.irq_startup	= iseries_startup_IRQ,
	.irq_shutdown	= iseries_shutdown_IRQ,
	.irq_unmask	= iseries_enable_IRQ,
	.irq_mask	= iseries_disable_IRQ,
	.irq_eoi	= iseries_end_IRQ
287 288 289 290 291
};

/*
 * This is called out of iSeries_scan_slot to allocate an IRQ for an EADS slot
 * It calculates the irq value for the slot.
292
 * Note that sub_bus is always 0 (at the moment at least).
293
 */
294
int __init iSeries_allocate_IRQ(HvBusNumber bus,
295
		HvSubBusNumber sub_bus, u32 bsubbus)
296
{
297
	unsigned int realirq;
298 299
	u8 idsel = ISERIES_GET_DEVICE_FROM_SUBBUS(bsubbus);
	u8 function = ISERIES_GET_FUNCTION_FROM_SUBBUS(bsubbus);
300

301 302
	realirq = (((((sub_bus << 8) + (bus - 1)) << 3) + (idsel - 1)) << 3)
		+ function;
303

304
	return irq_create_mapping(NULL, realirq);
305
}
306

307 308
#endif /* CONFIG_PCI */

309 310 311
/*
 * Get the next pending IRQ.
 */
O
Olaf Hering 已提交
312
unsigned int iSeries_get_irq(void)
313
{
314
	int irq = NO_IRQ_IGNORE;
315 316

#ifdef CONFIG_SMP
317 318
	if (get_lppaca()->int_dword.fields.ipi_cnt) {
		get_lppaca()->int_dword.fields.ipi_cnt = 0;
319
		iSeries_smp_message_recv();
320 321 322
	}
#endif /* CONFIG_SMP */
	if (hvlpevent_is_pending())
O
Olaf Hering 已提交
323
		process_hvlpevents();
324

325
#ifdef CONFIG_PCI
326 327 328 329 330 331 332 333 334 335 336
	if (num_pending_irqs) {
		spin_lock(&pending_irqs_lock);
		for (irq = 0; irq < NR_IRQS; irq++) {
			if (pending_irqs[irq]) {
				pending_irqs[irq]--;
				num_pending_irqs--;
				break;
			}
		}
		spin_unlock(&pending_irqs_lock);
		if (irq >= NR_IRQS)
337
			irq = NO_IRQ_IGNORE;
338
	}
339
#endif
340 341

	return irq;
342
}
343

344 345
#ifdef CONFIG_PCI

346
static int iseries_irq_host_map(struct irq_host *h, unsigned int virq,
347
				irq_hw_number_t hw)
348
{
349
	irq_set_chip_and_handler(virq, &iseries_pic, handle_fasteoi_irq);
350 351 352 353

	return 0;
}

354 355 356 357 358 359
static int iseries_irq_host_match(struct irq_host *h, struct device_node *np)
{
	/* Match all */
	return 1;
}

360 361
static struct irq_host_ops iseries_irq_host_ops = {
	.map = iseries_irq_host_map,
362
	.match = iseries_irq_host_match,
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
};

/*
 * This is called by init_IRQ.  set in ppc_md.init_IRQ by iSeries_setup.c
 * It must be called before the bus walk.
 */
void __init iSeries_init_IRQ(void)
{
	/* Register PCI event handler and open an event path */
	struct irq_host *host;
	int ret;

	/*
	 * The Hypervisor only allows us up to 256 interrupt
	 * sources (the irq number is passed in a u8).
	 */
	irq_set_virq_count(256);

	/* Create irq host. No need for a revmap since HV will give us
	 * back our virtual irq number
	 */
384 385
	host = irq_alloc_host(NULL, IRQ_HOST_MAP_NOMAP, 0,
			      &iseries_irq_host_ops, 0);
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
	BUG_ON(host == NULL);
	irq_set_default_host(host);

	ret = HvLpEvent_registerHandler(HvLpEvent_Type_PciIo,
			&pci_event_handler);
	if (ret == 0) {
		ret = HvLpEvent_openPath(HvLpEvent_Type_PciIo, 0);
		if (ret != 0)
			printk(KERN_ERR "iseries_init_IRQ: open event path "
					"failed with rc 0x%x\n", ret);
	} else
		printk(KERN_ERR "iseries_init_IRQ: register handler "
				"failed with rc 0x%x\n", ret);
}

401
#endif	/* CONFIG_PCI */