msi.c 19 KB

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