msi.c 21 KB

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