msi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 <linux/smp.h>
  18. #include <asm/errno.h>
  19. #include <asm/io.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, 0), &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. static void pci_intx_for_msi(struct pci_dev *dev, int enable)
  200. {
  201. if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
  202. pci_intx(dev, enable);
  203. }
  204. #ifdef CONFIG_PM
  205. static void __pci_restore_msi_state(struct pci_dev *dev)
  206. {
  207. int pos;
  208. u16 control;
  209. struct msi_desc *entry;
  210. if (!dev->msi_enabled)
  211. return;
  212. entry = get_irq_msi(dev->irq);
  213. pos = entry->msi_attrib.pos;
  214. pci_intx_for_msi(dev, 0);
  215. msi_set_enable(dev, 0);
  216. write_msi_msg(dev->irq, &entry->msg);
  217. if (entry->msi_attrib.maskbit)
  218. msi_set_mask_bit(dev->irq, entry->msi_attrib.masked);
  219. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  220. control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
  221. if (entry->msi_attrib.maskbit || !entry->msi_attrib.masked)
  222. control |= PCI_MSI_FLAGS_ENABLE;
  223. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  224. }
  225. static void __pci_restore_msix_state(struct pci_dev *dev)
  226. {
  227. int pos;
  228. struct msi_desc *entry;
  229. u16 control;
  230. if (!dev->msix_enabled)
  231. return;
  232. /* route the table */
  233. pci_intx_for_msi(dev, 0);
  234. msix_set_enable(dev, 0);
  235. list_for_each_entry(entry, &dev->msi_list, list) {
  236. write_msi_msg(entry->irq, &entry->msg);
  237. msi_set_mask_bit(entry->irq, entry->msi_attrib.masked);
  238. }
  239. BUG_ON(list_empty(&dev->msi_list));
  240. entry = list_entry(dev->msi_list.next, struct msi_desc, list);
  241. pos = entry->msi_attrib.pos;
  242. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  243. control &= ~PCI_MSIX_FLAGS_MASKALL;
  244. control |= PCI_MSIX_FLAGS_ENABLE;
  245. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  246. }
  247. void pci_restore_msi_state(struct pci_dev *dev)
  248. {
  249. __pci_restore_msi_state(dev);
  250. __pci_restore_msix_state(dev);
  251. }
  252. #endif /* CONFIG_PM */
  253. /**
  254. * msi_capability_init - configure device's MSI capability structure
  255. * @dev: pointer to the pci_dev data structure of MSI device function
  256. *
  257. * Setup the MSI capability structure of device function with a single
  258. * MSI irq, regardless of device function is capable of handling
  259. * multiple messages. A return of zero indicates the successful setup
  260. * of an entry zero with the new MSI irq or non-zero for otherwise.
  261. **/
  262. static int msi_capability_init(struct pci_dev *dev)
  263. {
  264. struct msi_desc *entry;
  265. int pos, ret;
  266. u16 control;
  267. msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
  268. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  269. pci_read_config_word(dev, msi_control_reg(pos), &control);
  270. /* MSI Entry Initialization */
  271. entry = alloc_msi_entry();
  272. if (!entry)
  273. return -ENOMEM;
  274. entry->msi_attrib.type = PCI_CAP_ID_MSI;
  275. entry->msi_attrib.is_64 = is_64bit_address(control);
  276. entry->msi_attrib.entry_nr = 0;
  277. entry->msi_attrib.maskbit = is_mask_bit_support(control);
  278. entry->msi_attrib.masked = 1;
  279. entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
  280. entry->msi_attrib.pos = pos;
  281. if (is_mask_bit_support(control)) {
  282. entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
  283. is_64bit_address(control));
  284. }
  285. entry->dev = dev;
  286. if (entry->msi_attrib.maskbit) {
  287. unsigned int maskbits, temp;
  288. /* All MSIs are unmasked by default, Mask them all */
  289. pci_read_config_dword(dev,
  290. msi_mask_bits_reg(pos, is_64bit_address(control)),
  291. &maskbits);
  292. temp = (1 << multi_msi_capable(control));
  293. temp = ((temp - 1) & ~temp);
  294. maskbits |= temp;
  295. pci_write_config_dword(dev,
  296. msi_mask_bits_reg(pos, is_64bit_address(control)),
  297. maskbits);
  298. }
  299. list_add_tail(&entry->list, &dev->msi_list);
  300. /* Configure MSI capability structure */
  301. ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI);
  302. if (ret) {
  303. msi_free_irqs(dev);
  304. return ret;
  305. }
  306. /* Set MSI enabled bits */
  307. pci_intx_for_msi(dev, 0);
  308. msi_set_enable(dev, 1);
  309. dev->msi_enabled = 1;
  310. dev->irq = entry->irq;
  311. return 0;
  312. }
  313. /**
  314. * msix_capability_init - configure device's MSI-X capability
  315. * @dev: pointer to the pci_dev data structure of MSI-X device function
  316. * @entries: pointer to an array of struct msix_entry entries
  317. * @nvec: number of @entries
  318. *
  319. * Setup the MSI-X capability structure of device function with a
  320. * single MSI-X irq. A return of zero indicates the successful setup of
  321. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  322. **/
  323. static int msix_capability_init(struct pci_dev *dev,
  324. struct msix_entry *entries, int nvec)
  325. {
  326. struct msi_desc *entry;
  327. int pos, i, j, nr_entries, ret;
  328. unsigned long phys_addr;
  329. u32 table_offset;
  330. u16 control;
  331. u8 bir;
  332. void __iomem *base;
  333. msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
  334. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  335. /* Request & Map MSI-X table region */
  336. pci_read_config_word(dev, msi_control_reg(pos), &control);
  337. nr_entries = multi_msix_capable(control);
  338. pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
  339. bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
  340. table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
  341. phys_addr = pci_resource_start (dev, bir) + table_offset;
  342. base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  343. if (base == NULL)
  344. return -ENOMEM;
  345. /* MSI-X Table Initialization */
  346. for (i = 0; i < nvec; i++) {
  347. entry = alloc_msi_entry();
  348. if (!entry)
  349. break;
  350. j = entries[i].entry;
  351. entry->msi_attrib.type = PCI_CAP_ID_MSIX;
  352. entry->msi_attrib.is_64 = 1;
  353. entry->msi_attrib.entry_nr = j;
  354. entry->msi_attrib.maskbit = 1;
  355. entry->msi_attrib.masked = 1;
  356. entry->msi_attrib.default_irq = dev->irq;
  357. entry->msi_attrib.pos = pos;
  358. entry->dev = dev;
  359. entry->mask_base = base;
  360. list_add_tail(&entry->list, &dev->msi_list);
  361. }
  362. ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
  363. if (ret) {
  364. int avail = 0;
  365. list_for_each_entry(entry, &dev->msi_list, list) {
  366. if (entry->irq != 0) {
  367. avail++;
  368. }
  369. }
  370. msi_free_irqs(dev);
  371. /* If we had some success report the number of irqs
  372. * we succeeded in setting up.
  373. */
  374. if (avail == 0)
  375. avail = ret;
  376. return avail;
  377. }
  378. i = 0;
  379. list_for_each_entry(entry, &dev->msi_list, list) {
  380. entries[i].vector = entry->irq;
  381. set_irq_msi(entry->irq, entry);
  382. i++;
  383. }
  384. /* Set MSI-X enabled bits */
  385. pci_intx_for_msi(dev, 0);
  386. msix_set_enable(dev, 1);
  387. dev->msix_enabled = 1;
  388. return 0;
  389. }
  390. /**
  391. * pci_msi_check_device - check whether MSI may be enabled on a device
  392. * @dev: pointer to the pci_dev data structure of MSI device function
  393. * @nvec: how many MSIs have been requested ?
  394. * @type: are we checking for MSI or MSI-X ?
  395. *
  396. * Look at global flags, the device itself, and its parent busses
  397. * to determine if MSI/-X are supported for the device. If MSI/-X is
  398. * supported return 0, else return an error code.
  399. **/
  400. static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
  401. {
  402. struct pci_bus *bus;
  403. int ret;
  404. /* MSI must be globally enabled and supported by the device */
  405. if (!pci_msi_enable || !dev || dev->no_msi)
  406. return -EINVAL;
  407. /*
  408. * You can't ask to have 0 or less MSIs configured.
  409. * a) it's stupid ..
  410. * b) the list manipulation code assumes nvec >= 1.
  411. */
  412. if (nvec < 1)
  413. return -ERANGE;
  414. /* Any bridge which does NOT route MSI transactions from it's
  415. * secondary bus to it's primary bus must set NO_MSI flag on
  416. * the secondary pci_bus.
  417. * We expect only arch-specific PCI host bus controller driver
  418. * or quirks for specific PCI bridges to be setting NO_MSI.
  419. */
  420. for (bus = dev->bus; bus; bus = bus->parent)
  421. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  422. return -EINVAL;
  423. ret = arch_msi_check_device(dev, nvec, type);
  424. if (ret)
  425. return ret;
  426. if (!pci_find_capability(dev, type))
  427. return -EINVAL;
  428. return 0;
  429. }
  430. /**
  431. * pci_enable_msi - configure device's MSI capability structure
  432. * @dev: pointer to the pci_dev data structure of MSI device function
  433. *
  434. * Setup the MSI capability structure of device function with
  435. * a single MSI irq upon its software driver call to request for
  436. * MSI mode enabled on its hardware device function. A return of zero
  437. * indicates the successful setup of an entry zero with the new MSI
  438. * irq or non-zero for otherwise.
  439. **/
  440. int pci_enable_msi(struct pci_dev* dev)
  441. {
  442. int status;
  443. status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI);
  444. if (status)
  445. return status;
  446. WARN_ON(!!dev->msi_enabled);
  447. /* Check whether driver already requested for MSI-X irqs */
  448. if (dev->msix_enabled) {
  449. printk(KERN_INFO "PCI: %s: Can't enable MSI. "
  450. "Device already has MSI-X enabled\n",
  451. pci_name(dev));
  452. return -EINVAL;
  453. }
  454. status = msi_capability_init(dev);
  455. return status;
  456. }
  457. EXPORT_SYMBOL(pci_enable_msi);
  458. void pci_disable_msi(struct pci_dev* dev)
  459. {
  460. struct msi_desc *entry;
  461. int default_irq;
  462. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  463. return;
  464. msi_set_enable(dev, 0);
  465. pci_intx_for_msi(dev, 1);
  466. dev->msi_enabled = 0;
  467. BUG_ON(list_empty(&dev->msi_list));
  468. entry = list_entry(dev->msi_list.next, struct msi_desc, list);
  469. if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
  470. return;
  471. }
  472. default_irq = entry->msi_attrib.default_irq;
  473. msi_free_irqs(dev);
  474. /* Restore dev->irq to its default pin-assertion irq */
  475. dev->irq = default_irq;
  476. }
  477. EXPORT_SYMBOL(pci_disable_msi);
  478. static int msi_free_irqs(struct pci_dev* dev)
  479. {
  480. struct msi_desc *entry, *tmp;
  481. list_for_each_entry(entry, &dev->msi_list, list) {
  482. if (entry->irq)
  483. BUG_ON(irq_has_action(entry->irq));
  484. }
  485. arch_teardown_msi_irqs(dev);
  486. list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
  487. if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) {
  488. writel(1, entry->mask_base + entry->msi_attrib.entry_nr
  489. * PCI_MSIX_ENTRY_SIZE
  490. + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
  491. if (list_is_last(&entry->list, &dev->msi_list))
  492. iounmap(entry->mask_base);
  493. }
  494. list_del(&entry->list);
  495. kfree(entry);
  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. msi_free_irqs(dev);
  553. }
  554. void pci_disable_msix(struct pci_dev* dev)
  555. {
  556. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  557. return;
  558. msix_set_enable(dev, 0);
  559. pci_intx_for_msi(dev, 1);
  560. dev->msix_enabled = 0;
  561. msix_free_all_irqs(dev);
  562. }
  563. EXPORT_SYMBOL(pci_disable_msix);
  564. /**
  565. * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
  566. * @dev: pointer to the pci_dev data structure of MSI(X) device function
  567. *
  568. * Being called during hotplug remove, from which the device function
  569. * is hot-removed. All previous assigned MSI/MSI-X irqs, if
  570. * allocated for this device function, are reclaimed to unused state,
  571. * which may be used later on.
  572. **/
  573. void msi_remove_pci_irq_vectors(struct pci_dev* dev)
  574. {
  575. if (!pci_msi_enable || !dev)
  576. return;
  577. if (dev->msi_enabled)
  578. msi_free_irqs(dev);
  579. if (dev->msix_enabled)
  580. msix_free_all_irqs(dev);
  581. }
  582. void pci_no_msi(void)
  583. {
  584. pci_msi_enable = 0;
  585. }
  586. void pci_msi_init_pci_dev(struct pci_dev *dev)
  587. {
  588. INIT_LIST_HEAD(&dev->msi_list);
  589. }
  590. /* Arch hooks */
  591. int __attribute__ ((weak))
  592. arch_msi_check_device(struct pci_dev* dev, int nvec, int type)
  593. {
  594. return 0;
  595. }
  596. int __attribute__ ((weak))
  597. arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *entry)
  598. {
  599. return 0;
  600. }
  601. int __attribute__ ((weak))
  602. arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  603. {
  604. struct msi_desc *entry;
  605. int ret;
  606. list_for_each_entry(entry, &dev->msi_list, list) {
  607. ret = arch_setup_msi_irq(dev, entry);
  608. if (ret)
  609. return ret;
  610. }
  611. return 0;
  612. }
  613. void __attribute__ ((weak)) arch_teardown_msi_irq(unsigned int irq)
  614. {
  615. return;
  616. }
  617. void __attribute__ ((weak))
  618. arch_teardown_msi_irqs(struct pci_dev *dev)
  619. {
  620. struct msi_desc *entry;
  621. list_for_each_entry(entry, &dev->msi_list, list) {
  622. if (entry->irq != 0)
  623. arch_teardown_msi_irq(entry->irq);
  624. }
  625. }