msi.c 19 KB

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