msi.c 18 KB

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