msi.c 21 KB

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