msi.c 18 KB

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