msi.c 19.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/*
 * File:	msi.c
 * Purpose:	PCI Message Signaled Interrupt (MSI)
 *
 * Copyright (C) 2003-2004 Intel
 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
 */

9
#include <linux/err.h>
L
Linus Torvalds 已提交
10 11 12 13 14 15 16
#include <linux/mm.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/pci.h>
#include <linux/proc_fs.h>
17
#include <linux/msi.h>
D
Dan Williams 已提交
18
#include <linux/smp.h>
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27

#include <asm/errno.h>
#include <asm/io.h>

#include "pci.h"
#include "msi.h"

static int pci_msi_enable = 1;

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/* Arch hooks */

int __attribute__ ((weak))
arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
{
	return 0;
}

int __attribute__ ((weak))
arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *entry)
{
	return 0;
}

int __attribute__ ((weak))
arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
{
	struct msi_desc *entry;
	int ret;

	list_for_each_entry(entry, &dev->msi_list, list) {
		ret = arch_setup_msi_irq(dev, entry);
		if (ret)
			return ret;
	}

	return 0;
}

void __attribute__ ((weak)) arch_teardown_msi_irq(unsigned int irq)
{
	return;
}

void __attribute__ ((weak))
arch_teardown_msi_irqs(struct pci_dev *dev)
{
	struct msi_desc *entry;

	list_for_each_entry(entry, &dev->msi_list, list) {
		if (entry->irq != 0)
			arch_teardown_msi_irq(entry->irq);
	}
}

73
static void __msi_set_enable(struct pci_dev *dev, int pos, int enable)
74 75 76 77 78 79 80 81 82 83 84 85
{
	u16 control;

	if (pos) {
		pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
		control &= ~PCI_MSI_FLAGS_ENABLE;
		if (enable)
			control |= PCI_MSI_FLAGS_ENABLE;
		pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
	}
}

86 87 88 89 90
static void msi_set_enable(struct pci_dev *dev, int enable)
{
	__msi_set_enable(dev, pci_find_capability(dev, PCI_CAP_ID_MSI), enable);
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static void msix_set_enable(struct pci_dev *dev, int enable)
{
	int pos;
	u16 control;

	pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
	if (pos) {
		pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
		control &= ~PCI_MSIX_FLAGS_ENABLE;
		if (enable)
			control |= PCI_MSIX_FLAGS_ENABLE;
		pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
	}
}

106 107
static inline __attribute_const__ u32 msi_mask(unsigned x)
{
108 109 110 111
	/* Don't shift by >= width of type */
	if (x >= 5)
		return 0xffffffff;
	return (1 << (1 << x)) - 1;
112 113
}

Y
Yinghai Lu 已提交
114
static void msix_flush_writes(struct irq_desc *desc)
M
Mitch Williams 已提交
115 116 117
{
	struct msi_desc *entry;

Y
Yinghai Lu 已提交
118
	entry = get_irq_desc_msi(desc);
M
Mitch Williams 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	BUG_ON(!entry || !entry->dev);
	switch (entry->msi_attrib.type) {
	case PCI_CAP_ID_MSI:
		/* nothing to do */
		break;
	case PCI_CAP_ID_MSIX:
	{
		int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
			PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
		readl(entry->mask_base + offset);
		break;
	}
	default:
		BUG();
		break;
	}
}

137 138 139 140 141 142 143 144 145
/*
 * PCI 2.3 does not specify mask bits for each MSI interrupt.  Attempting to
 * mask all MSI interrupts by clearing the MSI enable bit does not work
 * reliably as devices without an INTx disable bit will then generate a
 * level IRQ which will never be cleared.
 *
 * Returns 1 if it succeeded in masking the interrupt and 0 if the device
 * doesn't support MSI masking.
 */
Y
Yinghai Lu 已提交
146
static int msi_set_mask_bits(struct irq_desc *desc, u32 mask, u32 flag)
L
Linus Torvalds 已提交
147 148 149
{
	struct msi_desc *entry;

Y
Yinghai Lu 已提交
150
	entry = get_irq_desc_msi(desc);
151
	BUG_ON(!entry || !entry->dev);
L
Linus Torvalds 已提交
152 153
	switch (entry->msi_attrib.type) {
	case PCI_CAP_ID_MSI:
154
		if (entry->msi_attrib.maskbit) {
S
Satoru Takeuchi 已提交
155 156
			int pos;
			u32 mask_bits;
157 158 159

			pos = (long)entry->mask_base;
			pci_read_config_dword(entry->dev, pos, &mask_bits);
160 161
			mask_bits &= ~(mask);
			mask_bits |= flag & mask;
162
			pci_write_config_dword(entry->dev, pos, mask_bits);
163
		} else {
164
			return 0;
165
		}
L
Linus Torvalds 已提交
166 167 168 169 170 171
		break;
	case PCI_CAP_ID_MSIX:
	{
		int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
			PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
		writel(flag, entry->mask_base + offset);
172
		readl(entry->mask_base + offset);
L
Linus Torvalds 已提交
173 174 175
		break;
	}
	default:
176
		BUG();
L
Linus Torvalds 已提交
177 178
		break;
	}
179
	entry->msi_attrib.masked = !!flag;
180
	return 1;
L
Linus Torvalds 已提交
181 182
}

Y
Yinghai Lu 已提交
183
void read_msi_msg_desc(struct irq_desc *desc, struct msi_msg *msg)
L
Linus Torvalds 已提交
184
{
Y
Yinghai Lu 已提交
185
	struct msi_desc *entry = get_irq_desc_msi(desc);
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	switch(entry->msi_attrib.type) {
	case PCI_CAP_ID_MSI:
	{
		struct pci_dev *dev = entry->dev;
		int pos = entry->msi_attrib.pos;
		u16 data;

		pci_read_config_dword(dev, msi_lower_address_reg(pos),
					&msg->address_lo);
		if (entry->msi_attrib.is_64) {
			pci_read_config_dword(dev, msi_upper_address_reg(pos),
						&msg->address_hi);
			pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
		} else {
			msg->address_hi = 0;
201
			pci_read_config_word(dev, msi_data_reg(pos, 0), &data);
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		}
		msg->data = data;
		break;
	}
	case PCI_CAP_ID_MSIX:
	{
		void __iomem *base;
		base = entry->mask_base +
			entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;

		msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
		msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
		msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
 		break;
 	}
 	default:
		BUG();
	}
}
L
Linus Torvalds 已提交
221

Y
Yinghai Lu 已提交
222
void read_msi_msg(unsigned int irq, struct msi_msg *msg)
223
{
Y
Yinghai Lu 已提交
224 225 226 227 228 229 230 231
	struct irq_desc *desc = irq_to_desc(irq);

	read_msi_msg_desc(desc, msg);
}

void write_msi_msg_desc(struct irq_desc *desc, struct msi_msg *msg)
{
	struct msi_desc *entry = get_irq_desc_msi(desc);
L
Linus Torvalds 已提交
232 233 234
	switch (entry->msi_attrib.type) {
	case PCI_CAP_ID_MSI:
	{
235 236 237 238 239 240 241 242 243 244 245 246 247 248
		struct pci_dev *dev = entry->dev;
		int pos = entry->msi_attrib.pos;

		pci_write_config_dword(dev, msi_lower_address_reg(pos),
					msg->address_lo);
		if (entry->msi_attrib.is_64) {
			pci_write_config_dword(dev, msi_upper_address_reg(pos),
						msg->address_hi);
			pci_write_config_word(dev, msi_data_reg(pos, 1),
						msg->data);
		} else {
			pci_write_config_word(dev, msi_data_reg(pos, 0),
						msg->data);
		}
L
Linus Torvalds 已提交
249 250 251 252
		break;
	}
	case PCI_CAP_ID_MSIX:
	{
253 254 255 256 257 258 259 260 261
		void __iomem *base;
		base = entry->mask_base +
			entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;

		writel(msg->address_lo,
			base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
		writel(msg->address_hi,
			base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
		writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
L
Linus Torvalds 已提交
262 263 264
		break;
	}
	default:
265
		BUG();
L
Linus Torvalds 已提交
266
	}
267
	entry->msg = *msg;
L
Linus Torvalds 已提交
268
}
269

Y
Yinghai Lu 已提交
270 271 272 273 274 275 276
void write_msi_msg(unsigned int irq, struct msi_msg *msg)
{
	struct irq_desc *desc = irq_to_desc(irq);

	write_msi_msg_desc(desc, msg);
}

277
void mask_msi_irq(unsigned int irq)
L
Linus Torvalds 已提交
278
{
Y
Yinghai Lu 已提交
279 280 281 282
	struct irq_desc *desc = irq_to_desc(irq);

	msi_set_mask_bits(desc, 1, 1);
	msix_flush_writes(desc);
L
Linus Torvalds 已提交
283 284
}

285
void unmask_msi_irq(unsigned int irq)
L
Linus Torvalds 已提交
286
{
Y
Yinghai Lu 已提交
287 288 289 290
	struct irq_desc *desc = irq_to_desc(irq);

	msi_set_mask_bits(desc, 1, 0);
	msix_flush_writes(desc);
L
Linus Torvalds 已提交
291 292
}

293
static int msi_free_irqs(struct pci_dev* dev);
S
Satoru Takeuchi 已提交
294

L
Linus Torvalds 已提交
295 296 297 298
static struct msi_desc* alloc_msi_entry(void)
{
	struct msi_desc *entry;

M
Michael Ellerman 已提交
299
	entry = kzalloc(sizeof(struct msi_desc), GFP_KERNEL);
L
Linus Torvalds 已提交
300 301 302
	if (!entry)
		return NULL;

303 304
	INIT_LIST_HEAD(&entry->list);
	entry->irq = 0;
L
Linus Torvalds 已提交
305 306 307 308 309
	entry->dev = NULL;

	return entry;
}

310 311 312 313 314 315
static void pci_intx_for_msi(struct pci_dev *dev, int enable)
{
	if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
		pci_intx(dev, enable);
}

316
static void __pci_restore_msi_state(struct pci_dev *dev)
317
{
318
	int pos;
319
	u16 control;
320
	struct msi_desc *entry;
321

322 323 324
	if (!dev->msi_enabled)
		return;

325 326
	entry = get_irq_msi(dev->irq);
	pos = entry->msi_attrib.pos;
327

328
	pci_intx_for_msi(dev, 0);
329
	msi_set_enable(dev, 0);
330
	write_msi_msg(dev->irq, &entry->msg);
Y
Yinghai Lu 已提交
331 332 333
	if (entry->msi_attrib.maskbit) {
		struct irq_desc *desc = irq_to_desc(dev->irq);
		msi_set_mask_bits(desc, entry->msi_attrib.maskbits_mask,
334
				  entry->msi_attrib.masked);
Y
Yinghai Lu 已提交
335
	}
336 337

	pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
338 339
	control &= ~PCI_MSI_FLAGS_QSIZE;
	control |= PCI_MSI_FLAGS_ENABLE;
340
	pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
341 342 343
}

static void __pci_restore_msix_state(struct pci_dev *dev)
344 345 346
{
	int pos;
	struct msi_desc *entry;
347
	u16 control;
348

E
Eric W. Biederman 已提交
349 350 351
	if (!dev->msix_enabled)
		return;

352
	/* route the table */
353
	pci_intx_for_msi(dev, 0);
354
	msix_set_enable(dev, 0);
355

356
	list_for_each_entry(entry, &dev->msi_list, list) {
Y
Yinghai Lu 已提交
357
		struct irq_desc *desc = irq_to_desc(entry->irq);
358
		write_msi_msg(entry->irq, &entry->msg);
Y
Yinghai Lu 已提交
359
		msi_set_mask_bits(desc, 1, entry->msi_attrib.masked);
360 361
	}

362 363
	BUG_ON(list_empty(&dev->msi_list));
	entry = list_entry(dev->msi_list.next, struct msi_desc, list);
364
	pos = entry->msi_attrib.pos;
365 366 367 368
	pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
	control &= ~PCI_MSIX_FLAGS_MASKALL;
	control |= PCI_MSIX_FLAGS_ENABLE;
	pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
369
}
370 371 372 373 374 375

void pci_restore_msi_state(struct pci_dev *dev)
{
	__pci_restore_msi_state(dev);
	__pci_restore_msix_state(dev);
}
376
EXPORT_SYMBOL_GPL(pci_restore_msi_state);
377

L
Linus Torvalds 已提交
378 379 380 381
/**
 * msi_capability_init - configure device's MSI capability structure
 * @dev: pointer to the pci_dev data structure of MSI device function
 *
382
 * Setup the MSI capability structure of device function with a single
383
 * MSI irq, regardless of device function is capable of handling
L
Linus Torvalds 已提交
384
 * multiple messages. A return of zero indicates the successful setup
385
 * of an entry zero with the new MSI irq or non-zero for otherwise.
L
Linus Torvalds 已提交
386 387 388 389
 **/
static int msi_capability_init(struct pci_dev *dev)
{
	struct msi_desc *entry;
390
	int pos, ret;
L
Linus Torvalds 已提交
391 392
	u16 control;

393 394
	msi_set_enable(dev, 0);	/* Ensure msi is disabled as I set it up */

L
Linus Torvalds 已提交
395 396 397
   	pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
	pci_read_config_word(dev, msi_control_reg(pos), &control);
	/* MSI Entry Initialization */
398 399 400
	entry = alloc_msi_entry();
	if (!entry)
		return -ENOMEM;
401

L
Linus Torvalds 已提交
402
	entry->msi_attrib.type = PCI_CAP_ID_MSI;
403
	entry->msi_attrib.is_64 = is_64bit_address(control);
L
Linus Torvalds 已提交
404 405
	entry->msi_attrib.entry_nr = 0;
	entry->msi_attrib.maskbit = is_mask_bit_support(control);
406
	entry->msi_attrib.masked = 1;
407
	entry->msi_attrib.default_irq = dev->irq;	/* Save IOAPIC IRQ */
408
	entry->msi_attrib.pos = pos;
409 410
	entry->dev = dev;
	if (entry->msi_attrib.maskbit) {
411 412 413 414 415
		unsigned int base, maskbits, temp;

		base = msi_mask_bits_reg(pos, entry->msi_attrib.is_64);
		entry->mask_base = (void __iomem *)(long)base;

416
		/* All MSIs are unmasked by default, Mask them all */
417
		pci_read_config_dword(dev, base, &maskbits);
418
		temp = msi_mask((control & PCI_MSI_FLAGS_QMASK) >> 1);
419
		maskbits |= temp;
420
		pci_write_config_dword(dev, base, maskbits);
421
		entry->msi_attrib.maskbits_mask = temp;
422
	}
423
	list_add_tail(&entry->list, &dev->msi_list);
424

L
Linus Torvalds 已提交
425
	/* Configure MSI capability structure */
426
	ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI);
427
	if (ret) {
428
		msi_free_irqs(dev);
429
		return ret;
430
	}
431

L
Linus Torvalds 已提交
432
	/* Set MSI enabled bits	 */
433
	pci_intx_for_msi(dev, 0);
434 435
	msi_set_enable(dev, 1);
	dev->msi_enabled = 1;
L
Linus Torvalds 已提交
436

437
	dev->irq = entry->irq;
L
Linus Torvalds 已提交
438 439 440 441 442 443
	return 0;
}

/**
 * msix_capability_init - configure device's MSI-X capability
 * @dev: pointer to the pci_dev data structure of MSI-X device function
R
Randy Dunlap 已提交
444 445
 * @entries: pointer to an array of struct msix_entry entries
 * @nvec: number of @entries
L
Linus Torvalds 已提交
446
 *
447
 * Setup the MSI-X capability structure of device function with a
448 449
 * single MSI-X irq. A return of zero indicates the successful setup of
 * requested MSI-X entries with allocated irqs or non-zero for otherwise.
L
Linus Torvalds 已提交
450 451 452 453
 **/
static int msix_capability_init(struct pci_dev *dev,
				struct msix_entry *entries, int nvec)
{
454
	struct msi_desc *entry;
455
	int pos, i, j, nr_entries, ret;
456 457
	unsigned long phys_addr;
	u32 table_offset;
L
Linus Torvalds 已提交
458 459 460 461
 	u16 control;
	u8 bir;
	void __iomem *base;

462 463
	msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */

L
Linus Torvalds 已提交
464 465 466 467
   	pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
	/* Request & Map MSI-X table region */
 	pci_read_config_word(dev, msi_control_reg(pos), &control);
	nr_entries = multi_msix_capable(control);
468 469

 	pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
L
Linus Torvalds 已提交
470
	bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
471 472
	table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
	phys_addr = pci_resource_start (dev, bir) + table_offset;
L
Linus Torvalds 已提交
473 474 475 476 477 478
	base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
	if (base == NULL)
		return -ENOMEM;

	/* MSI-X Table Initialization */
	for (i = 0; i < nvec; i++) {
479 480
		entry = alloc_msi_entry();
		if (!entry)
L
Linus Torvalds 已提交
481 482 483 484
			break;

 		j = entries[i].entry;
		entry->msi_attrib.type = PCI_CAP_ID_MSIX;
485
		entry->msi_attrib.is_64 = 1;
L
Linus Torvalds 已提交
486 487
		entry->msi_attrib.entry_nr = j;
		entry->msi_attrib.maskbit = 1;
488
		entry->msi_attrib.masked = 1;
489
		entry->msi_attrib.default_irq = dev->irq;
490
		entry->msi_attrib.pos = pos;
L
Linus Torvalds 已提交
491 492
		entry->dev = dev;
		entry->mask_base = base;
493

494
		list_add_tail(&entry->list, &dev->msi_list);
L
Linus Torvalds 已提交
495
	}
496 497 498 499 500 501 502 503

	ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
	if (ret) {
		int avail = 0;
		list_for_each_entry(entry, &dev->msi_list, list) {
			if (entry->irq != 0) {
				avail++;
			}
L
Linus Torvalds 已提交
504
		}
505

506 507
		msi_free_irqs(dev);

508 509 510
		/* If we had some success report the number of irqs
		 * we succeeded in setting up.
		 */
511 512
		if (avail == 0)
			avail = ret;
513
		return avail;
L
Linus Torvalds 已提交
514
	}
515 516 517 518 519 520 521

	i = 0;
	list_for_each_entry(entry, &dev->msi_list, list) {
		entries[i].vector = entry->irq;
		set_irq_msi(entry->irq, entry);
		i++;
	}
L
Linus Torvalds 已提交
522
	/* Set MSI-X enabled bits */
523
	pci_intx_for_msi(dev, 0);
524 525
	msix_set_enable(dev, 1);
	dev->msix_enabled = 1;
L
Linus Torvalds 已提交
526 527 528 529

	return 0;
}

530
/**
531
 * pci_msi_check_device - check whether MSI may be enabled on a device
532
 * @dev: pointer to the pci_dev data structure of MSI device function
533
 * @nvec: how many MSIs have been requested ?
534
 * @type: are we checking for MSI or MSI-X ?
535
 *
536
 * Look at global flags, the device itself, and its parent busses
537 538
 * to determine if MSI/-X are supported for the device. If MSI/-X is
 * supported return 0, else return an error code.
539
 **/
540
static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
541 542
{
	struct pci_bus *bus;
543
	int ret;
544

545
	/* MSI must be globally enabled and supported by the device */
546 547 548
	if (!pci_msi_enable || !dev || dev->no_msi)
		return -EINVAL;

549 550 551 552 553 554 555 556
	/*
	 * You can't ask to have 0 or less MSIs configured.
	 *  a) it's stupid ..
	 *  b) the list manipulation code assumes nvec >= 1.
	 */
	if (nvec < 1)
		return -ERANGE;

557 558 559 560 561 562
	/* Any bridge which does NOT route MSI transactions from it's
	 * secondary bus to it's primary bus must set NO_MSI flag on
	 * the secondary pci_bus.
	 * We expect only arch-specific PCI host bus controller driver
	 * or quirks for specific PCI bridges to be setting NO_MSI.
	 */
563 564 565 566
	for (bus = dev->bus; bus; bus = bus->parent)
		if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
			return -EINVAL;

567 568 569 570
	ret = arch_msi_check_device(dev, nvec, type);
	if (ret)
		return ret;

571 572 573
	if (!pci_find_capability(dev, type))
		return -EINVAL;

574 575 576
	return 0;
}

L
Linus Torvalds 已提交
577 578 579 580 581
/**
 * pci_enable_msi - configure device's MSI capability structure
 * @dev: pointer to the pci_dev data structure of MSI device function
 *
 * Setup the MSI capability structure of device function with
582
 * a single MSI irq upon its software driver call to request for
L
Linus Torvalds 已提交
583 584
 * MSI mode enabled on its hardware device function. A return of zero
 * indicates the successful setup of an entry zero with the new MSI
585
 * irq or non-zero for otherwise.
L
Linus Torvalds 已提交
586 587 588
 **/
int pci_enable_msi(struct pci_dev* dev)
{
589
	int status;
L
Linus Torvalds 已提交
590

591 592 593
	status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI);
	if (status)
		return status;
L
Linus Torvalds 已提交
594

E
Eric W. Biederman 已提交
595
	WARN_ON(!!dev->msi_enabled);
L
Linus Torvalds 已提交
596

597
	/* Check whether driver already requested for MSI-X irqs */
598
	if (dev->msix_enabled) {
599 600
		dev_info(&dev->dev, "can't enable MSI "
			 "(MSI-X already enabled)\n");
601
		return -EINVAL;
L
Linus Torvalds 已提交
602 603 604 605
	}
	status = msi_capability_init(dev);
	return status;
}
606
EXPORT_SYMBOL(pci_enable_msi);
L
Linus Torvalds 已提交
607

608
void pci_msi_shutdown(struct pci_dev* dev)
L
Linus Torvalds 已提交
609 610 611
{
	struct msi_desc *entry;

612
	if (!pci_msi_enable || !dev || !dev->msi_enabled)
E
Eric W. Biederman 已提交
613 614
		return;

615
	msi_set_enable(dev, 0);
616
	pci_intx_for_msi(dev, 1);
617
	dev->msi_enabled = 0;
618

619 620
	BUG_ON(list_empty(&dev->msi_list));
	entry = list_entry(dev->msi_list.next, struct msi_desc, list);
621 622 623
	/* Return the the pci reset with msi irqs unmasked */
	if (entry->msi_attrib.maskbit) {
		u32 mask = entry->msi_attrib.maskbits_mask;
Y
Yinghai Lu 已提交
624 625
		struct irq_desc *desc = irq_to_desc(dev->irq);
		msi_set_mask_bits(desc, mask, ~mask);
626
	}
627
	if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI)
L
Linus Torvalds 已提交
628
		return;
629 630

	/* Restore dev->irq to its default pin-assertion irq */
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
	dev->irq = entry->msi_attrib.default_irq;
}
void pci_disable_msi(struct pci_dev* dev)
{
	struct msi_desc *entry;

	if (!pci_msi_enable || !dev || !dev->msi_enabled)
		return;

	pci_msi_shutdown(dev);

	entry = list_entry(dev->msi_list.next, struct msi_desc, list);
	if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI)
		return;

	msi_free_irqs(dev);
L
Linus Torvalds 已提交
647
}
648
EXPORT_SYMBOL(pci_disable_msi);
L
Linus Torvalds 已提交
649

650
static int msi_free_irqs(struct pci_dev* dev)
L
Linus Torvalds 已提交
651
{
652
	struct msi_desc *entry, *tmp;
M
Michael Ellerman 已提交
653

654 655 656 657
	list_for_each_entry(entry, &dev->msi_list, list) {
		if (entry->irq)
			BUG_ON(irq_has_action(entry->irq));
	}
L
Linus Torvalds 已提交
658

659
	arch_teardown_msi_irqs(dev);
L
Linus Torvalds 已提交
660

661 662 663 664 665
	list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
		if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) {
			writel(1, entry->mask_base + entry->msi_attrib.entry_nr
				  * PCI_MSIX_ENTRY_SIZE
				  + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
666 667 668

			if (list_is_last(&entry->list, &dev->msi_list))
				iounmap(entry->mask_base);
669 670 671
		}
		list_del(&entry->list);
		kfree(entry);
L
Linus Torvalds 已提交
672 673 674 675 676 677 678 679
	}

	return 0;
}

/**
 * pci_enable_msix - configure device's MSI-X capability structure
 * @dev: pointer to the pci_dev data structure of MSI-X device function
680
 * @entries: pointer to an array of MSI-X entries
681
 * @nvec: number of MSI-X irqs requested for allocation by device driver
L
Linus Torvalds 已提交
682 683
 *
 * Setup the MSI-X capability structure of device function with the number
684
 * of requested irqs upon its software driver call to request for
L
Linus Torvalds 已提交
685 686
 * MSI-X mode enabled on its hardware device function. A return of zero
 * indicates the successful configuration of MSI-X capability structure
687
 * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
L
Linus Torvalds 已提交
688
 * Or a return of > 0 indicates that driver request is exceeding the number
689
 * of irqs available. Driver should use the returned value to re-send
L
Linus Torvalds 已提交
690 691 692 693
 * its request.
 **/
int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
{
694
	int status, pos, nr_entries;
E
Eric W. Biederman 已提交
695
	int i, j;
L
Linus Torvalds 已提交
696 697
	u16 control;

698
	if (!entries)
L
Linus Torvalds 已提交
699 700
 		return -EINVAL;

701 702 703 704
	status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
	if (status)
		return status;

705
	pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
L
Linus Torvalds 已提交
706 707 708 709 710 711 712 713 714 715 716 717 718 719
	pci_read_config_word(dev, msi_control_reg(pos), &control);
	nr_entries = multi_msix_capable(control);
	if (nvec > nr_entries)
		return -EINVAL;

	/* Check for any invalid entries */
	for (i = 0; i < nvec; i++) {
		if (entries[i].entry >= nr_entries)
			return -EINVAL;		/* invalid entry */
		for (j = i + 1; j < nvec; j++) {
			if (entries[i].entry == entries[j].entry)
				return -EINVAL;	/* duplicate entry */
		}
	}
E
Eric W. Biederman 已提交
720
	WARN_ON(!!dev->msix_enabled);
721

722
	/* Check whether driver already requested for MSI irq */
723
   	if (dev->msi_enabled) {
724 725
		dev_info(&dev->dev, "can't enable MSI-X "
		       "(MSI IRQ already assigned)\n");
L
Linus Torvalds 已提交
726 727 728 729 730
		return -EINVAL;
	}
	status = msix_capability_init(dev, entries, nvec);
	return status;
}
731
EXPORT_SYMBOL(pci_enable_msix);
L
Linus Torvalds 已提交
732

733
static void msix_free_all_irqs(struct pci_dev *dev)
L
Linus Torvalds 已提交
734
{
735
	msi_free_irqs(dev);
736 737
}

738
void pci_msix_shutdown(struct pci_dev* dev)
739
{
740
	if (!pci_msi_enable || !dev || !dev->msix_enabled)
E
Eric W. Biederman 已提交
741 742
		return;

743
	msix_set_enable(dev, 0);
744
	pci_intx_for_msi(dev, 1);
745
	dev->msix_enabled = 0;
746 747 748 749 750 751 752
}
void pci_disable_msix(struct pci_dev* dev)
{
	if (!pci_msi_enable || !dev || !dev->msix_enabled)
		return;

	pci_msix_shutdown(dev);
753

754
	msix_free_all_irqs(dev);
L
Linus Torvalds 已提交
755
}
756
EXPORT_SYMBOL(pci_disable_msix);
L
Linus Torvalds 已提交
757 758

/**
759
 * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
L
Linus Torvalds 已提交
760 761
 * @dev: pointer to the pci_dev data structure of MSI(X) device function
 *
762
 * Being called during hotplug remove, from which the device function
763
 * is hot-removed. All previous assigned MSI/MSI-X irqs, if
L
Linus Torvalds 已提交
764 765 766 767 768 769 770 771
 * allocated for this device function, are reclaimed to unused state,
 * which may be used later on.
 **/
void msi_remove_pci_irq_vectors(struct pci_dev* dev)
{
	if (!pci_msi_enable || !dev)
 		return;

772 773
	if (dev->msi_enabled)
		msi_free_irqs(dev);
L
Linus Torvalds 已提交
774

775 776
	if (dev->msix_enabled)
		msix_free_all_irqs(dev);
L
Linus Torvalds 已提交
777 778
}

779 780 781 782
void pci_no_msi(void)
{
	pci_msi_enable = 0;
}
783

784 785 786 787 788 789 790
/**
 * pci_msi_enabled - is MSI enabled?
 *
 * Returns true if MSI has not been disabled by the command-line option
 * pci=nomsi.
 **/
int pci_msi_enabled(void)
791
{
792
	return pci_msi_enable;
793
}
794
EXPORT_SYMBOL(pci_msi_enabled);
795

796
void pci_msi_init_pci_dev(struct pci_dev *dev)
797
{
798
	INIT_LIST_HEAD(&dev->msi_list);
799
}