msi.c 22 KB

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