msi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * File: msi.c
  3. * Purpose: PCI Message Signaled Interrupt (MSI)
  4. *
  5. * Copyright (C) 2003-2004 Intel
  6. * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
  7. */
  8. #include <linux/err.h>
  9. #include <linux/mm.h>
  10. #include <linux/irq.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/init.h>
  13. #include <linux/ioport.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/pci.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/msi.h>
  18. #include <asm/errno.h>
  19. #include <asm/io.h>
  20. #include <asm/smp.h>
  21. #include "pci.h"
  22. #include "msi.h"
  23. static struct kmem_cache* msi_cachep;
  24. static int pci_msi_enable = 1;
  25. static int msi_cache_init(void)
  26. {
  27. msi_cachep = kmem_cache_create("msi_cache", sizeof(struct msi_desc),
  28. 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  29. if (!msi_cachep)
  30. return -ENOMEM;
  31. return 0;
  32. }
  33. static void msi_set_enable(struct pci_dev *dev, int enable)
  34. {
  35. int pos;
  36. u16 control;
  37. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  38. if (pos) {
  39. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  40. control &= ~PCI_MSI_FLAGS_ENABLE;
  41. if (enable)
  42. control |= PCI_MSI_FLAGS_ENABLE;
  43. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  44. }
  45. }
  46. static void msix_set_enable(struct pci_dev *dev, int enable)
  47. {
  48. int pos;
  49. u16 control;
  50. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  51. if (pos) {
  52. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  53. control &= ~PCI_MSIX_FLAGS_ENABLE;
  54. if (enable)
  55. control |= PCI_MSIX_FLAGS_ENABLE;
  56. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  57. }
  58. }
  59. static void msi_set_mask_bit(unsigned int irq, int flag)
  60. {
  61. struct msi_desc *entry;
  62. entry = get_irq_msi(irq);
  63. BUG_ON(!entry || !entry->dev);
  64. switch (entry->msi_attrib.type) {
  65. case PCI_CAP_ID_MSI:
  66. if (entry->msi_attrib.maskbit) {
  67. int pos;
  68. u32 mask_bits;
  69. pos = (long)entry->mask_base;
  70. pci_read_config_dword(entry->dev, pos, &mask_bits);
  71. mask_bits &= ~(1);
  72. mask_bits |= flag;
  73. pci_write_config_dword(entry->dev, pos, mask_bits);
  74. } else {
  75. msi_set_enable(entry->dev, !flag);
  76. }
  77. break;
  78. case PCI_CAP_ID_MSIX:
  79. {
  80. int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  81. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
  82. writel(flag, entry->mask_base + offset);
  83. readl(entry->mask_base + offset);
  84. break;
  85. }
  86. default:
  87. BUG();
  88. break;
  89. }
  90. entry->msi_attrib.masked = !!flag;
  91. }
  92. void read_msi_msg(unsigned int irq, struct msi_msg *msg)
  93. {
  94. struct msi_desc *entry = get_irq_msi(irq);
  95. switch(entry->msi_attrib.type) {
  96. case PCI_CAP_ID_MSI:
  97. {
  98. struct pci_dev *dev = entry->dev;
  99. int pos = entry->msi_attrib.pos;
  100. u16 data;
  101. pci_read_config_dword(dev, msi_lower_address_reg(pos),
  102. &msg->address_lo);
  103. if (entry->msi_attrib.is_64) {
  104. pci_read_config_dword(dev, msi_upper_address_reg(pos),
  105. &msg->address_hi);
  106. pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
  107. } else {
  108. msg->address_hi = 0;
  109. pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
  110. }
  111. msg->data = data;
  112. break;
  113. }
  114. case PCI_CAP_ID_MSIX:
  115. {
  116. void __iomem *base;
  117. base = entry->mask_base +
  118. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  119. msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  120. msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  121. msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
  122. break;
  123. }
  124. default:
  125. BUG();
  126. }
  127. }
  128. void write_msi_msg(unsigned int irq, struct msi_msg *msg)
  129. {
  130. struct msi_desc *entry = get_irq_msi(irq);
  131. switch (entry->msi_attrib.type) {
  132. case PCI_CAP_ID_MSI:
  133. {
  134. struct pci_dev *dev = entry->dev;
  135. int pos = entry->msi_attrib.pos;
  136. pci_write_config_dword(dev, msi_lower_address_reg(pos),
  137. msg->address_lo);
  138. if (entry->msi_attrib.is_64) {
  139. pci_write_config_dword(dev, msi_upper_address_reg(pos),
  140. msg->address_hi);
  141. pci_write_config_word(dev, msi_data_reg(pos, 1),
  142. msg->data);
  143. } else {
  144. pci_write_config_word(dev, msi_data_reg(pos, 0),
  145. msg->data);
  146. }
  147. break;
  148. }
  149. case PCI_CAP_ID_MSIX:
  150. {
  151. void __iomem *base;
  152. base = entry->mask_base +
  153. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  154. writel(msg->address_lo,
  155. base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  156. writel(msg->address_hi,
  157. base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  158. writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
  159. break;
  160. }
  161. default:
  162. BUG();
  163. }
  164. entry->msg = *msg;
  165. }
  166. void mask_msi_irq(unsigned int irq)
  167. {
  168. msi_set_mask_bit(irq, 1);
  169. }
  170. void unmask_msi_irq(unsigned int irq)
  171. {
  172. msi_set_mask_bit(irq, 0);
  173. }
  174. static int msi_free_irq(struct pci_dev* dev, int irq);
  175. static int msi_init(void)
  176. {
  177. static int status = -ENOMEM;
  178. if (!status)
  179. return status;
  180. status = msi_cache_init();
  181. if (status < 0) {
  182. pci_msi_enable = 0;
  183. printk(KERN_WARNING "PCI: MSI cache init failed\n");
  184. return status;
  185. }
  186. return status;
  187. }
  188. static struct msi_desc* alloc_msi_entry(void)
  189. {
  190. struct msi_desc *entry;
  191. entry = kmem_cache_zalloc(msi_cachep, GFP_KERNEL);
  192. if (!entry)
  193. return NULL;
  194. entry->link.tail = entry->link.head = 0; /* single message */
  195. entry->dev = NULL;
  196. return entry;
  197. }
  198. #ifdef CONFIG_PM
  199. static void __pci_restore_msi_state(struct pci_dev *dev)
  200. {
  201. int pos;
  202. u16 control;
  203. struct msi_desc *entry;
  204. if (!dev->msi_enabled)
  205. return;
  206. entry = get_irq_msi(dev->irq);
  207. pos = entry->msi_attrib.pos;
  208. pci_intx(dev, 0); /* disable intx */
  209. msi_set_enable(dev, 0);
  210. write_msi_msg(dev->irq, &entry->msg);
  211. if (entry->msi_attrib.maskbit)
  212. msi_set_mask_bit(dev->irq, entry->msi_attrib.masked);
  213. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  214. control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
  215. if (entry->msi_attrib.maskbit || !entry->msi_attrib.masked)
  216. control |= PCI_MSI_FLAGS_ENABLE;
  217. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  218. }
  219. static void __pci_restore_msix_state(struct pci_dev *dev)
  220. {
  221. int pos;
  222. int irq, head, tail = 0;
  223. struct msi_desc *entry;
  224. u16 control;
  225. if (!dev->msix_enabled)
  226. return;
  227. /* route the table */
  228. pci_intx(dev, 0); /* disable intx */
  229. msix_set_enable(dev, 0);
  230. irq = head = dev->first_msi_irq;
  231. entry = get_irq_msi(irq);
  232. pos = entry->msi_attrib.pos;
  233. while (head != tail) {
  234. entry = get_irq_msi(irq);
  235. write_msi_msg(irq, &entry->msg);
  236. msi_set_mask_bit(irq, entry->msi_attrib.masked);
  237. tail = entry->link.tail;
  238. irq = tail;
  239. }
  240. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  241. control &= ~PCI_MSIX_FLAGS_MASKALL;
  242. control |= PCI_MSIX_FLAGS_ENABLE;
  243. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  244. }
  245. void pci_restore_msi_state(struct pci_dev *dev)
  246. {
  247. __pci_restore_msi_state(dev);
  248. __pci_restore_msix_state(dev);
  249. }
  250. #endif /* CONFIG_PM */
  251. /**
  252. * msi_capability_init - configure device's MSI capability structure
  253. * @dev: pointer to the pci_dev data structure of MSI device function
  254. *
  255. * Setup the MSI capability structure of device function with a single
  256. * MSI irq, regardless of device function is capable of handling
  257. * multiple messages. A return of zero indicates the successful setup
  258. * of an entry zero with the new MSI irq or non-zero for otherwise.
  259. **/
  260. static int msi_capability_init(struct pci_dev *dev)
  261. {
  262. struct msi_desc *entry;
  263. int pos, irq;
  264. u16 control;
  265. msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
  266. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  267. pci_read_config_word(dev, msi_control_reg(pos), &control);
  268. /* MSI Entry Initialization */
  269. entry = alloc_msi_entry();
  270. if (!entry)
  271. return -ENOMEM;
  272. entry->msi_attrib.type = PCI_CAP_ID_MSI;
  273. entry->msi_attrib.is_64 = is_64bit_address(control);
  274. entry->msi_attrib.entry_nr = 0;
  275. entry->msi_attrib.maskbit = is_mask_bit_support(control);
  276. entry->msi_attrib.masked = 1;
  277. entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
  278. entry->msi_attrib.pos = pos;
  279. if (is_mask_bit_support(control)) {
  280. entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
  281. is_64bit_address(control));
  282. }
  283. entry->dev = dev;
  284. if (entry->msi_attrib.maskbit) {
  285. unsigned int maskbits, temp;
  286. /* All MSIs are unmasked by default, Mask them all */
  287. pci_read_config_dword(dev,
  288. msi_mask_bits_reg(pos, is_64bit_address(control)),
  289. &maskbits);
  290. temp = (1 << multi_msi_capable(control));
  291. temp = ((temp - 1) & ~temp);
  292. maskbits |= temp;
  293. pci_write_config_dword(dev,
  294. msi_mask_bits_reg(pos, is_64bit_address(control)),
  295. maskbits);
  296. }
  297. /* Configure MSI capability structure */
  298. irq = arch_setup_msi_irq(dev, entry);
  299. if (irq < 0) {
  300. kmem_cache_free(msi_cachep, entry);
  301. return irq;
  302. }
  303. entry->link.head = irq;
  304. entry->link.tail = irq;
  305. dev->first_msi_irq = irq;
  306. set_irq_msi(irq, entry);
  307. /* Set MSI enabled bits */
  308. pci_intx(dev, 0); /* disable intx */
  309. msi_set_enable(dev, 1);
  310. dev->msi_enabled = 1;
  311. dev->irq = irq;
  312. return 0;
  313. }
  314. /**
  315. * msix_capability_init - configure device's MSI-X capability
  316. * @dev: pointer to the pci_dev data structure of MSI-X device function
  317. * @entries: pointer to an array of struct msix_entry entries
  318. * @nvec: number of @entries
  319. *
  320. * Setup the MSI-X capability structure of device function with a
  321. * single MSI-X irq. A return of zero indicates the successful setup of
  322. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  323. **/
  324. static int msix_capability_init(struct pci_dev *dev,
  325. struct msix_entry *entries, int nvec)
  326. {
  327. struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
  328. int irq, pos, i, j, nr_entries, temp = 0;
  329. unsigned long phys_addr;
  330. u32 table_offset;
  331. u16 control;
  332. u8 bir;
  333. void __iomem *base;
  334. msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
  335. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  336. /* Request & Map MSI-X table region */
  337. pci_read_config_word(dev, msi_control_reg(pos), &control);
  338. nr_entries = multi_msix_capable(control);
  339. pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
  340. bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
  341. table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
  342. phys_addr = pci_resource_start (dev, bir) + table_offset;
  343. base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  344. if (base == NULL)
  345. return -ENOMEM;
  346. /* MSI-X Table Initialization */
  347. for (i = 0; i < nvec; i++) {
  348. entry = alloc_msi_entry();
  349. if (!entry)
  350. break;
  351. j = entries[i].entry;
  352. entry->msi_attrib.type = PCI_CAP_ID_MSIX;
  353. entry->msi_attrib.is_64 = 1;
  354. entry->msi_attrib.entry_nr = j;
  355. entry->msi_attrib.maskbit = 1;
  356. entry->msi_attrib.masked = 1;
  357. entry->msi_attrib.default_irq = dev->irq;
  358. entry->msi_attrib.pos = pos;
  359. entry->dev = dev;
  360. entry->mask_base = base;
  361. /* Configure MSI-X capability structure */
  362. irq = arch_setup_msi_irq(dev, entry);
  363. if (irq < 0) {
  364. kmem_cache_free(msi_cachep, entry);
  365. break;
  366. }
  367. entries[i].vector = irq;
  368. if (!head) {
  369. entry->link.head = irq;
  370. entry->link.tail = irq;
  371. head = entry;
  372. } else {
  373. entry->link.head = temp;
  374. entry->link.tail = tail->link.tail;
  375. tail->link.tail = irq;
  376. head->link.head = irq;
  377. }
  378. temp = irq;
  379. tail = entry;
  380. set_irq_msi(irq, entry);
  381. }
  382. if (i != nvec) {
  383. int avail = i - 1;
  384. i--;
  385. for (; i >= 0; i--) {
  386. irq = (entries + i)->vector;
  387. msi_free_irq(dev, irq);
  388. (entries + i)->vector = 0;
  389. }
  390. /* If we had some success report the number of irqs
  391. * we succeeded in setting up.
  392. */
  393. if (avail <= 0)
  394. avail = -EBUSY;
  395. return avail;
  396. }
  397. dev->first_msi_irq = entries[0].vector;
  398. /* Set MSI-X enabled bits */
  399. pci_intx(dev, 0); /* disable intx */
  400. msix_set_enable(dev, 1);
  401. dev->msix_enabled = 1;
  402. return 0;
  403. }
  404. /**
  405. * pci_msi_supported - check whether MSI may be enabled on device
  406. * @dev: pointer to the pci_dev data structure of MSI device function
  407. *
  408. * Look at global flags, the device itself, and its parent busses
  409. * to return 0 if MSI are supported for the device.
  410. **/
  411. static
  412. int pci_msi_supported(struct pci_dev * dev)
  413. {
  414. struct pci_bus *bus;
  415. /* MSI must be globally enabled and supported by the device */
  416. if (!pci_msi_enable || !dev || dev->no_msi)
  417. return -EINVAL;
  418. /* Any bridge which does NOT route MSI transactions from it's
  419. * secondary bus to it's primary bus must set NO_MSI flag on
  420. * the secondary pci_bus.
  421. * We expect only arch-specific PCI host bus controller driver
  422. * or quirks for specific PCI bridges to be setting NO_MSI.
  423. */
  424. for (bus = dev->bus; bus; bus = bus->parent)
  425. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  426. return -EINVAL;
  427. return 0;
  428. }
  429. /**
  430. * pci_enable_msi - configure device's MSI capability structure
  431. * @dev: pointer to the pci_dev data structure of MSI device function
  432. *
  433. * Setup the MSI capability structure of device function with
  434. * a single MSI irq upon its software driver call to request for
  435. * MSI mode enabled on its hardware device function. A return of zero
  436. * indicates the successful setup of an entry zero with the new MSI
  437. * irq or non-zero for otherwise.
  438. **/
  439. int pci_enable_msi(struct pci_dev* dev)
  440. {
  441. int pos, status;
  442. if (pci_msi_supported(dev) < 0)
  443. return -EINVAL;
  444. status = msi_init();
  445. if (status < 0)
  446. return status;
  447. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  448. if (!pos)
  449. return -EINVAL;
  450. WARN_ON(!!dev->msi_enabled);
  451. /* Check whether driver already requested for MSI-X irqs */
  452. if (dev->msix_enabled) {
  453. printk(KERN_INFO "PCI: %s: Can't enable MSI. "
  454. "Device already has MSI-X enabled\n",
  455. pci_name(dev));
  456. return -EINVAL;
  457. }
  458. status = msi_capability_init(dev);
  459. return status;
  460. }
  461. void pci_disable_msi(struct pci_dev* dev)
  462. {
  463. struct msi_desc *entry;
  464. int default_irq;
  465. if (!pci_msi_enable)
  466. return;
  467. if (!dev)
  468. return;
  469. if (!dev->msi_enabled)
  470. return;
  471. msi_set_enable(dev, 0);
  472. pci_intx(dev, 1); /* enable intx */
  473. dev->msi_enabled = 0;
  474. entry = get_irq_msi(dev->first_msi_irq);
  475. if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
  476. return;
  477. }
  478. if (irq_has_action(dev->first_msi_irq)) {
  479. printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without "
  480. "free_irq() on MSI irq %d\n",
  481. pci_name(dev), dev->first_msi_irq);
  482. BUG_ON(irq_has_action(dev->first_msi_irq));
  483. } else {
  484. default_irq = entry->msi_attrib.default_irq;
  485. msi_free_irq(dev, dev->first_msi_irq);
  486. /* Restore dev->irq to its default pin-assertion irq */
  487. dev->irq = default_irq;
  488. }
  489. dev->first_msi_irq = 0;
  490. }
  491. static int msi_free_irq(struct pci_dev* dev, int irq)
  492. {
  493. struct msi_desc *entry;
  494. int head, entry_nr, type;
  495. void __iomem *base;
  496. entry = get_irq_msi(irq);
  497. if (!entry || entry->dev != dev) {
  498. return -EINVAL;
  499. }
  500. type = entry->msi_attrib.type;
  501. entry_nr = entry->msi_attrib.entry_nr;
  502. head = entry->link.head;
  503. base = entry->mask_base;
  504. get_irq_msi(entry->link.head)->link.tail = entry->link.tail;
  505. get_irq_msi(entry->link.tail)->link.head = entry->link.head;
  506. arch_teardown_msi_irq(irq);
  507. kmem_cache_free(msi_cachep, entry);
  508. if (type == PCI_CAP_ID_MSIX) {
  509. writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
  510. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
  511. if (head == irq)
  512. iounmap(base);
  513. }
  514. return 0;
  515. }
  516. /**
  517. * pci_enable_msix - configure device's MSI-X capability structure
  518. * @dev: pointer to the pci_dev data structure of MSI-X device function
  519. * @entries: pointer to an array of MSI-X entries
  520. * @nvec: number of MSI-X irqs requested for allocation by device driver
  521. *
  522. * Setup the MSI-X capability structure of device function with the number
  523. * of requested irqs upon its software driver call to request for
  524. * MSI-X mode enabled on its hardware device function. A return of zero
  525. * indicates the successful configuration of MSI-X capability structure
  526. * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
  527. * Or a return of > 0 indicates that driver request is exceeding the number
  528. * of irqs available. Driver should use the returned value to re-send
  529. * its request.
  530. **/
  531. int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
  532. {
  533. int status, pos, nr_entries;
  534. int i, j;
  535. u16 control;
  536. if (!entries || pci_msi_supported(dev) < 0)
  537. return -EINVAL;
  538. status = msi_init();
  539. if (status < 0)
  540. return status;
  541. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  542. if (!pos)
  543. return -EINVAL;
  544. pci_read_config_word(dev, msi_control_reg(pos), &control);
  545. nr_entries = multi_msix_capable(control);
  546. if (nvec > nr_entries)
  547. return -EINVAL;
  548. /* Check for any invalid entries */
  549. for (i = 0; i < nvec; i++) {
  550. if (entries[i].entry >= nr_entries)
  551. return -EINVAL; /* invalid entry */
  552. for (j = i + 1; j < nvec; j++) {
  553. if (entries[i].entry == entries[j].entry)
  554. return -EINVAL; /* duplicate entry */
  555. }
  556. }
  557. WARN_ON(!!dev->msix_enabled);
  558. /* Check whether driver already requested for MSI irq */
  559. if (dev->msi_enabled) {
  560. printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
  561. "Device already has an MSI irq assigned\n",
  562. pci_name(dev));
  563. return -EINVAL;
  564. }
  565. status = msix_capability_init(dev, entries, nvec);
  566. return status;
  567. }
  568. void pci_disable_msix(struct pci_dev* dev)
  569. {
  570. int irq, head, tail = 0, warning = 0;
  571. if (!pci_msi_enable)
  572. return;
  573. if (!dev)
  574. return;
  575. if (!dev->msix_enabled)
  576. return;
  577. msix_set_enable(dev, 0);
  578. pci_intx(dev, 1); /* enable intx */
  579. dev->msix_enabled = 0;
  580. irq = head = dev->first_msi_irq;
  581. while (head != tail) {
  582. tail = get_irq_msi(irq)->link.tail;
  583. if (irq_has_action(irq))
  584. warning = 1;
  585. else if (irq != head) /* Release MSI-X irq */
  586. msi_free_irq(dev, irq);
  587. irq = tail;
  588. }
  589. msi_free_irq(dev, irq);
  590. if (warning) {
  591. printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without "
  592. "free_irq() on all MSI-X irqs\n",
  593. pci_name(dev));
  594. BUG_ON(warning > 0);
  595. }
  596. dev->first_msi_irq = 0;
  597. }
  598. /**
  599. * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
  600. * @dev: pointer to the pci_dev data structure of MSI(X) device function
  601. *
  602. * Being called during hotplug remove, from which the device function
  603. * is hot-removed. All previous assigned MSI/MSI-X irqs, if
  604. * allocated for this device function, are reclaimed to unused state,
  605. * which may be used later on.
  606. **/
  607. void msi_remove_pci_irq_vectors(struct pci_dev* dev)
  608. {
  609. if (!pci_msi_enable || !dev)
  610. return;
  611. if (dev->msi_enabled) {
  612. if (irq_has_action(dev->first_msi_irq)) {
  613. printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
  614. "called without free_irq() on MSI irq %d\n",
  615. pci_name(dev), dev->first_msi_irq);
  616. BUG_ON(irq_has_action(dev->first_msi_irq));
  617. } else /* Release MSI irq assigned to this device */
  618. msi_free_irq(dev, dev->first_msi_irq);
  619. }
  620. if (dev->msix_enabled) {
  621. int irq, head, tail = 0, warning = 0;
  622. void __iomem *base = NULL;
  623. irq = head = dev->first_msi_irq;
  624. while (head != tail) {
  625. tail = get_irq_msi(irq)->link.tail;
  626. base = get_irq_msi(irq)->mask_base;
  627. if (irq_has_action(irq))
  628. warning = 1;
  629. else if (irq != head) /* Release MSI-X irq */
  630. msi_free_irq(dev, irq);
  631. irq = tail;
  632. }
  633. msi_free_irq(dev, irq);
  634. if (warning) {
  635. iounmap(base);
  636. printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() "
  637. "called without free_irq() on all MSI-X irqs\n",
  638. pci_name(dev));
  639. BUG_ON(warning > 0);
  640. }
  641. }
  642. }
  643. void pci_no_msi(void)
  644. {
  645. pci_msi_enable = 0;
  646. }
  647. EXPORT_SYMBOL(pci_enable_msi);
  648. EXPORT_SYMBOL(pci_disable_msi);
  649. EXPORT_SYMBOL(pci_enable_msix);
  650. EXPORT_SYMBOL(pci_disable_msix);