msi.c 22 KB

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