msi.c 20 KB

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