msi.c 19 KB

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