msi.c 18 KB

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