msi.c 22 KB

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