msi.c 18 KB

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