msi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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_irqs(struct pci_dev* dev);
  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. msi_free_irqs(dev);
  300. return ret;
  301. }
  302. /* Set MSI enabled bits */
  303. pci_intx(dev, 0); /* disable intx */
  304. msi_set_enable(dev, 1);
  305. dev->msi_enabled = 1;
  306. dev->irq = entry->irq;
  307. return 0;
  308. }
  309. /**
  310. * msix_capability_init - configure device's MSI-X capability
  311. * @dev: pointer to the pci_dev data structure of MSI-X device function
  312. * @entries: pointer to an array of struct msix_entry entries
  313. * @nvec: number of @entries
  314. *
  315. * Setup the MSI-X capability structure of device function with a
  316. * single MSI-X irq. A return of zero indicates the successful setup of
  317. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  318. **/
  319. static int msix_capability_init(struct pci_dev *dev,
  320. struct msix_entry *entries, int nvec)
  321. {
  322. struct msi_desc *entry;
  323. int pos, i, j, nr_entries, ret;
  324. unsigned long phys_addr;
  325. u32 table_offset;
  326. u16 control;
  327. u8 bir;
  328. void __iomem *base;
  329. msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
  330. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  331. /* Request & Map MSI-X table region */
  332. pci_read_config_word(dev, msi_control_reg(pos), &control);
  333. nr_entries = multi_msix_capable(control);
  334. pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
  335. bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
  336. table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
  337. phys_addr = pci_resource_start (dev, bir) + table_offset;
  338. base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  339. if (base == NULL)
  340. return -ENOMEM;
  341. /* MSI-X Table Initialization */
  342. for (i = 0; i < nvec; i++) {
  343. entry = alloc_msi_entry();
  344. if (!entry)
  345. break;
  346. j = entries[i].entry;
  347. entry->msi_attrib.type = PCI_CAP_ID_MSIX;
  348. entry->msi_attrib.is_64 = 1;
  349. entry->msi_attrib.entry_nr = j;
  350. entry->msi_attrib.maskbit = 1;
  351. entry->msi_attrib.masked = 1;
  352. entry->msi_attrib.default_irq = dev->irq;
  353. entry->msi_attrib.pos = pos;
  354. entry->dev = dev;
  355. entry->mask_base = base;
  356. list_add(&entry->list, &dev->msi_list);
  357. }
  358. ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
  359. if (ret) {
  360. int avail = 0;
  361. list_for_each_entry(entry, &dev->msi_list, list) {
  362. if (entry->irq != 0) {
  363. avail++;
  364. }
  365. }
  366. msi_free_irqs(dev);
  367. /* If we had some success report the number of irqs
  368. * we succeeded in setting up.
  369. */
  370. if (avail == 0)
  371. avail = ret;
  372. return avail;
  373. }
  374. i = 0;
  375. list_for_each_entry(entry, &dev->msi_list, list) {
  376. entries[i].vector = entry->irq;
  377. set_irq_msi(entry->irq, entry);
  378. i++;
  379. }
  380. /* Set MSI-X enabled bits */
  381. pci_intx(dev, 0); /* disable intx */
  382. msix_set_enable(dev, 1);
  383. dev->msix_enabled = 1;
  384. return 0;
  385. }
  386. /**
  387. * pci_msi_check_device - check whether MSI may be enabled on a device
  388. * @dev: pointer to the pci_dev data structure of MSI device function
  389. * @nvec: how many MSIs have been requested ?
  390. * @type: are we checking for MSI or MSI-X ?
  391. *
  392. * Look at global flags, the device itself, and its parent busses
  393. * to determine if MSI/-X are supported for the device. If MSI/-X is
  394. * supported return 0, else return an error code.
  395. **/
  396. static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
  397. {
  398. struct pci_bus *bus;
  399. int ret;
  400. /* MSI must be globally enabled and supported by the device */
  401. if (!pci_msi_enable || !dev || dev->no_msi)
  402. return -EINVAL;
  403. /*
  404. * You can't ask to have 0 or less MSIs configured.
  405. * a) it's stupid ..
  406. * b) the list manipulation code assumes nvec >= 1.
  407. */
  408. if (nvec < 1)
  409. return -ERANGE;
  410. /* Any bridge which does NOT route MSI transactions from it's
  411. * secondary bus to it's primary bus must set NO_MSI flag on
  412. * the secondary pci_bus.
  413. * We expect only arch-specific PCI host bus controller driver
  414. * or quirks for specific PCI bridges to be setting NO_MSI.
  415. */
  416. for (bus = dev->bus; bus; bus = bus->parent)
  417. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  418. return -EINVAL;
  419. ret = arch_msi_check_device(dev, nvec, type);
  420. if (ret)
  421. return ret;
  422. if (!pci_find_capability(dev, type))
  423. return -EINVAL;
  424. return 0;
  425. }
  426. /**
  427. * pci_enable_msi - configure device's MSI capability structure
  428. * @dev: pointer to the pci_dev data structure of MSI device function
  429. *
  430. * Setup the MSI capability structure of device function with
  431. * a single MSI irq upon its software driver call to request for
  432. * MSI mode enabled on its hardware device function. A return of zero
  433. * indicates the successful setup of an entry zero with the new MSI
  434. * irq or non-zero for otherwise.
  435. **/
  436. int pci_enable_msi(struct pci_dev* dev)
  437. {
  438. int status;
  439. status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI);
  440. if (status)
  441. return status;
  442. WARN_ON(!!dev->msi_enabled);
  443. /* Check whether driver already requested for MSI-X irqs */
  444. if (dev->msix_enabled) {
  445. printk(KERN_INFO "PCI: %s: Can't enable MSI. "
  446. "Device already has MSI-X enabled\n",
  447. pci_name(dev));
  448. return -EINVAL;
  449. }
  450. status = msi_capability_init(dev);
  451. return status;
  452. }
  453. EXPORT_SYMBOL(pci_enable_msi);
  454. void pci_disable_msi(struct pci_dev* dev)
  455. {
  456. struct msi_desc *entry;
  457. int default_irq;
  458. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  459. return;
  460. msi_set_enable(dev, 0);
  461. pci_intx(dev, 1); /* enable intx */
  462. dev->msi_enabled = 0;
  463. BUG_ON(list_empty(&dev->msi_list));
  464. entry = list_entry(dev->msi_list.next, struct msi_desc, list);
  465. if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
  466. return;
  467. }
  468. default_irq = entry->msi_attrib.default_irq;
  469. msi_free_irqs(dev);
  470. /* Restore dev->irq to its default pin-assertion irq */
  471. dev->irq = default_irq;
  472. }
  473. EXPORT_SYMBOL(pci_disable_msi);
  474. static int msi_free_irqs(struct pci_dev* dev)
  475. {
  476. struct msi_desc *entry, *tmp;
  477. list_for_each_entry(entry, &dev->msi_list, list)
  478. BUG_ON(irq_has_action(entry->irq));
  479. arch_teardown_msi_irqs(dev);
  480. list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
  481. if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) {
  482. if (list_is_last(&entry->list, &dev->msi_list))
  483. iounmap(entry->mask_base);
  484. writel(1, entry->mask_base + entry->msi_attrib.entry_nr
  485. * PCI_MSIX_ENTRY_SIZE
  486. + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
  487. }
  488. list_del(&entry->list);
  489. kfree(entry);
  490. }
  491. return 0;
  492. }
  493. /**
  494. * pci_enable_msix - configure device's MSI-X capability structure
  495. * @dev: pointer to the pci_dev data structure of MSI-X device function
  496. * @entries: pointer to an array of MSI-X entries
  497. * @nvec: number of MSI-X irqs requested for allocation by device driver
  498. *
  499. * Setup the MSI-X capability structure of device function with the number
  500. * of requested irqs upon its software driver call to request for
  501. * MSI-X mode enabled on its hardware device function. A return of zero
  502. * indicates the successful configuration of MSI-X capability structure
  503. * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
  504. * Or a return of > 0 indicates that driver request is exceeding the number
  505. * of irqs available. Driver should use the returned value to re-send
  506. * its request.
  507. **/
  508. int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
  509. {
  510. int status, pos, nr_entries;
  511. int i, j;
  512. u16 control;
  513. if (!entries)
  514. return -EINVAL;
  515. status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
  516. if (status)
  517. return status;
  518. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  519. pci_read_config_word(dev, msi_control_reg(pos), &control);
  520. nr_entries = multi_msix_capable(control);
  521. if (nvec > nr_entries)
  522. return -EINVAL;
  523. /* Check for any invalid entries */
  524. for (i = 0; i < nvec; i++) {
  525. if (entries[i].entry >= nr_entries)
  526. return -EINVAL; /* invalid entry */
  527. for (j = i + 1; j < nvec; j++) {
  528. if (entries[i].entry == entries[j].entry)
  529. return -EINVAL; /* duplicate entry */
  530. }
  531. }
  532. WARN_ON(!!dev->msix_enabled);
  533. /* Check whether driver already requested for MSI irq */
  534. if (dev->msi_enabled) {
  535. printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
  536. "Device already has an MSI irq assigned\n",
  537. pci_name(dev));
  538. return -EINVAL;
  539. }
  540. status = msix_capability_init(dev, entries, nvec);
  541. return status;
  542. }
  543. EXPORT_SYMBOL(pci_enable_msix);
  544. static void msix_free_all_irqs(struct pci_dev *dev)
  545. {
  546. msi_free_irqs(dev);
  547. }
  548. void pci_disable_msix(struct pci_dev* dev)
  549. {
  550. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  551. return;
  552. msix_set_enable(dev, 0);
  553. pci_intx(dev, 1); /* enable intx */
  554. dev->msix_enabled = 0;
  555. msix_free_all_irqs(dev);
  556. }
  557. EXPORT_SYMBOL(pci_disable_msix);
  558. /**
  559. * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
  560. * @dev: pointer to the pci_dev data structure of MSI(X) device function
  561. *
  562. * Being called during hotplug remove, from which the device function
  563. * is hot-removed. All previous assigned MSI/MSI-X irqs, if
  564. * allocated for this device function, are reclaimed to unused state,
  565. * which may be used later on.
  566. **/
  567. void msi_remove_pci_irq_vectors(struct pci_dev* dev)
  568. {
  569. if (!pci_msi_enable || !dev)
  570. return;
  571. if (dev->msi_enabled)
  572. msi_free_irqs(dev);
  573. if (dev->msix_enabled)
  574. msix_free_all_irqs(dev);
  575. }
  576. void pci_no_msi(void)
  577. {
  578. pci_msi_enable = 0;
  579. }
  580. void pci_msi_init_pci_dev(struct pci_dev *dev)
  581. {
  582. INIT_LIST_HEAD(&dev->msi_list);
  583. }
  584. /* Arch hooks */
  585. int __attribute__ ((weak))
  586. arch_msi_check_device(struct pci_dev* dev, int nvec, int type)
  587. {
  588. return 0;
  589. }
  590. int __attribute__ ((weak))
  591. arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *entry)
  592. {
  593. return 0;
  594. }
  595. int __attribute__ ((weak))
  596. arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  597. {
  598. struct msi_desc *entry;
  599. int ret;
  600. list_for_each_entry(entry, &dev->msi_list, list) {
  601. ret = arch_setup_msi_irq(dev, entry);
  602. if (ret)
  603. return ret;
  604. }
  605. return 0;
  606. }
  607. void __attribute__ ((weak)) arch_teardown_msi_irq(unsigned int irq)
  608. {
  609. return;
  610. }
  611. void __attribute__ ((weak))
  612. arch_teardown_msi_irqs(struct pci_dev *dev)
  613. {
  614. struct msi_desc *entry;
  615. list_for_each_entry(entry, &dev->msi_list, list) {
  616. if (entry->irq != 0)
  617. arch_teardown_msi_irq(entry->irq);
  618. }
  619. }