msi.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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/export.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. static int pci_msi_enable = 1;
  23. #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
  24. /* Arch hooks */
  25. int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  26. {
  27. struct msi_chip *chip = dev->bus->msi;
  28. int err;
  29. if (!chip || !chip->setup_irq)
  30. return -EINVAL;
  31. err = chip->setup_irq(chip, dev, desc);
  32. if (err < 0)
  33. return err;
  34. irq_set_chip_data(desc->irq, chip);
  35. return 0;
  36. }
  37. void __weak arch_teardown_msi_irq(unsigned int irq)
  38. {
  39. struct msi_chip *chip = irq_get_chip_data(irq);
  40. if (!chip || !chip->teardown_irq)
  41. return;
  42. chip->teardown_irq(chip, irq);
  43. }
  44. int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  45. {
  46. struct msi_desc *entry;
  47. int ret;
  48. /*
  49. * If an architecture wants to support multiple MSI, it needs to
  50. * override arch_setup_msi_irqs()
  51. */
  52. if (type == PCI_CAP_ID_MSI && nvec > 1)
  53. return 1;
  54. list_for_each_entry(entry, &dev->msi_list, list) {
  55. ret = arch_setup_msi_irq(dev, entry);
  56. if (ret < 0)
  57. return ret;
  58. if (ret > 0)
  59. return -ENOSPC;
  60. }
  61. return 0;
  62. }
  63. /*
  64. * We have a default implementation available as a separate non-weak
  65. * function, as it is used by the Xen x86 PCI code
  66. */
  67. void default_teardown_msi_irqs(struct pci_dev *dev)
  68. {
  69. struct msi_desc *entry;
  70. list_for_each_entry(entry, &dev->msi_list, list) {
  71. int i, nvec;
  72. if (entry->irq == 0)
  73. continue;
  74. if (entry->nvec_used)
  75. nvec = entry->nvec_used;
  76. else
  77. nvec = 1 << entry->msi_attrib.multiple;
  78. for (i = 0; i < nvec; i++)
  79. arch_teardown_msi_irq(entry->irq + i);
  80. }
  81. }
  82. void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
  83. {
  84. return default_teardown_msi_irqs(dev);
  85. }
  86. static void default_restore_msi_irq(struct pci_dev *dev, int irq)
  87. {
  88. struct msi_desc *entry;
  89. entry = NULL;
  90. if (dev->msix_enabled) {
  91. list_for_each_entry(entry, &dev->msi_list, list) {
  92. if (irq == entry->irq)
  93. break;
  94. }
  95. } else if (dev->msi_enabled) {
  96. entry = irq_get_msi_desc(irq);
  97. }
  98. if (entry)
  99. __write_msi_msg(entry, &entry->msg);
  100. }
  101. void __weak arch_restore_msi_irqs(struct pci_dev *dev)
  102. {
  103. return default_restore_msi_irqs(dev);
  104. }
  105. static void msi_set_enable(struct pci_dev *dev, int enable)
  106. {
  107. u16 control;
  108. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  109. control &= ~PCI_MSI_FLAGS_ENABLE;
  110. if (enable)
  111. control |= PCI_MSI_FLAGS_ENABLE;
  112. pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
  113. }
  114. static void msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set)
  115. {
  116. u16 ctrl;
  117. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
  118. ctrl &= ~clear;
  119. ctrl |= set;
  120. pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl);
  121. }
  122. static inline __attribute_const__ u32 msi_mask(unsigned x)
  123. {
  124. /* Don't shift by >= width of type */
  125. if (x >= 5)
  126. return 0xffffffff;
  127. return (1 << (1 << x)) - 1;
  128. }
  129. /*
  130. * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
  131. * mask all MSI interrupts by clearing the MSI enable bit does not work
  132. * reliably as devices without an INTx disable bit will then generate a
  133. * level IRQ which will never be cleared.
  134. */
  135. u32 default_msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
  136. {
  137. u32 mask_bits = desc->masked;
  138. if (!desc->msi_attrib.maskbit)
  139. return 0;
  140. mask_bits &= ~mask;
  141. mask_bits |= flag;
  142. pci_write_config_dword(desc->dev, desc->mask_pos, mask_bits);
  143. return mask_bits;
  144. }
  145. __weak u32 arch_msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
  146. {
  147. return default_msi_mask_irq(desc, mask, flag);
  148. }
  149. static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
  150. {
  151. desc->masked = arch_msi_mask_irq(desc, mask, flag);
  152. }
  153. /*
  154. * This internal function does not flush PCI writes to the device.
  155. * All users must ensure that they read from the device before either
  156. * assuming that the device state is up to date, or returning out of this
  157. * file. This saves a few milliseconds when initialising devices with lots
  158. * of MSI-X interrupts.
  159. */
  160. u32 default_msix_mask_irq(struct msi_desc *desc, u32 flag)
  161. {
  162. u32 mask_bits = desc->masked;
  163. unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  164. PCI_MSIX_ENTRY_VECTOR_CTRL;
  165. mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
  166. if (flag)
  167. mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
  168. writel(mask_bits, desc->mask_base + offset);
  169. return mask_bits;
  170. }
  171. __weak u32 arch_msix_mask_irq(struct msi_desc *desc, u32 flag)
  172. {
  173. return default_msix_mask_irq(desc, flag);
  174. }
  175. static void msix_mask_irq(struct msi_desc *desc, u32 flag)
  176. {
  177. desc->masked = arch_msix_mask_irq(desc, flag);
  178. }
  179. static void msi_set_mask_bit(struct irq_data *data, u32 flag)
  180. {
  181. struct msi_desc *desc = irq_data_get_msi(data);
  182. if (desc->msi_attrib.is_msix) {
  183. msix_mask_irq(desc, flag);
  184. readl(desc->mask_base); /* Flush write to device */
  185. } else {
  186. unsigned offset = data->irq - desc->irq;
  187. msi_mask_irq(desc, 1 << offset, flag << offset);
  188. }
  189. }
  190. void mask_msi_irq(struct irq_data *data)
  191. {
  192. msi_set_mask_bit(data, 1);
  193. }
  194. void unmask_msi_irq(struct irq_data *data)
  195. {
  196. msi_set_mask_bit(data, 0);
  197. }
  198. void default_restore_msi_irqs(struct pci_dev *dev)
  199. {
  200. struct msi_desc *entry;
  201. list_for_each_entry(entry, &dev->msi_list, list) {
  202. default_restore_msi_irq(dev, entry->irq);
  203. }
  204. }
  205. void __read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  206. {
  207. BUG_ON(entry->dev->current_state != PCI_D0);
  208. if (entry->msi_attrib.is_msix) {
  209. void __iomem *base = entry->mask_base +
  210. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  211. msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
  212. msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
  213. msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
  214. } else {
  215. struct pci_dev *dev = entry->dev;
  216. int pos = dev->msi_cap;
  217. u16 data;
  218. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
  219. &msg->address_lo);
  220. if (entry->msi_attrib.is_64) {
  221. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
  222. &msg->address_hi);
  223. pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
  224. } else {
  225. msg->address_hi = 0;
  226. pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
  227. }
  228. msg->data = data;
  229. }
  230. }
  231. void read_msi_msg(unsigned int irq, struct msi_msg *msg)
  232. {
  233. struct msi_desc *entry = irq_get_msi_desc(irq);
  234. __read_msi_msg(entry, msg);
  235. }
  236. void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  237. {
  238. /* Assert that the cache is valid, assuming that
  239. * valid messages are not all-zeroes. */
  240. BUG_ON(!(entry->msg.address_hi | entry->msg.address_lo |
  241. entry->msg.data));
  242. *msg = entry->msg;
  243. }
  244. void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
  245. {
  246. struct msi_desc *entry = irq_get_msi_desc(irq);
  247. __get_cached_msi_msg(entry, msg);
  248. }
  249. EXPORT_SYMBOL_GPL(get_cached_msi_msg);
  250. void __write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  251. {
  252. if (entry->dev->current_state != PCI_D0) {
  253. /* Don't touch the hardware now */
  254. } else if (entry->msi_attrib.is_msix) {
  255. void __iomem *base;
  256. base = entry->mask_base +
  257. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  258. writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
  259. writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
  260. writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
  261. } else {
  262. struct pci_dev *dev = entry->dev;
  263. int pos = dev->msi_cap;
  264. u16 msgctl;
  265. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
  266. msgctl &= ~PCI_MSI_FLAGS_QSIZE;
  267. msgctl |= entry->msi_attrib.multiple << 4;
  268. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
  269. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
  270. msg->address_lo);
  271. if (entry->msi_attrib.is_64) {
  272. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
  273. msg->address_hi);
  274. pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
  275. msg->data);
  276. } else {
  277. pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
  278. msg->data);
  279. }
  280. }
  281. entry->msg = *msg;
  282. }
  283. void write_msi_msg(unsigned int irq, struct msi_msg *msg)
  284. {
  285. struct msi_desc *entry = irq_get_msi_desc(irq);
  286. __write_msi_msg(entry, msg);
  287. }
  288. EXPORT_SYMBOL_GPL(write_msi_msg);
  289. static void free_msi_irqs(struct pci_dev *dev)
  290. {
  291. struct msi_desc *entry, *tmp;
  292. struct attribute **msi_attrs;
  293. struct device_attribute *dev_attr;
  294. int count = 0;
  295. list_for_each_entry(entry, &dev->msi_list, list) {
  296. int i, nvec;
  297. if (!entry->irq)
  298. continue;
  299. if (entry->nvec_used)
  300. nvec = entry->nvec_used;
  301. else
  302. nvec = 1 << entry->msi_attrib.multiple;
  303. for (i = 0; i < nvec; i++)
  304. BUG_ON(irq_has_action(entry->irq + i));
  305. }
  306. arch_teardown_msi_irqs(dev);
  307. list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) {
  308. if (entry->msi_attrib.is_msix) {
  309. if (list_is_last(&entry->list, &dev->msi_list))
  310. iounmap(entry->mask_base);
  311. }
  312. list_del(&entry->list);
  313. kfree(entry);
  314. }
  315. if (dev->msi_irq_groups) {
  316. sysfs_remove_groups(&dev->dev.kobj, dev->msi_irq_groups);
  317. msi_attrs = dev->msi_irq_groups[0]->attrs;
  318. while (msi_attrs[count]) {
  319. dev_attr = container_of(msi_attrs[count],
  320. struct device_attribute, attr);
  321. kfree(dev_attr->attr.name);
  322. kfree(dev_attr);
  323. ++count;
  324. }
  325. kfree(msi_attrs);
  326. kfree(dev->msi_irq_groups[0]);
  327. kfree(dev->msi_irq_groups);
  328. dev->msi_irq_groups = NULL;
  329. }
  330. }
  331. static struct msi_desc *alloc_msi_entry(struct pci_dev *dev)
  332. {
  333. struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  334. if (!desc)
  335. return NULL;
  336. INIT_LIST_HEAD(&desc->list);
  337. desc->dev = dev;
  338. return desc;
  339. }
  340. static void pci_intx_for_msi(struct pci_dev *dev, int enable)
  341. {
  342. if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
  343. pci_intx(dev, enable);
  344. }
  345. static void __pci_restore_msi_state(struct pci_dev *dev)
  346. {
  347. u16 control;
  348. struct msi_desc *entry;
  349. if (!dev->msi_enabled)
  350. return;
  351. entry = irq_get_msi_desc(dev->irq);
  352. pci_intx_for_msi(dev, 0);
  353. msi_set_enable(dev, 0);
  354. arch_restore_msi_irqs(dev);
  355. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  356. msi_mask_irq(entry, msi_mask(entry->msi_attrib.multi_cap),
  357. entry->masked);
  358. control &= ~PCI_MSI_FLAGS_QSIZE;
  359. control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
  360. pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
  361. }
  362. static void __pci_restore_msix_state(struct pci_dev *dev)
  363. {
  364. struct msi_desc *entry;
  365. if (!dev->msix_enabled)
  366. return;
  367. BUG_ON(list_empty(&dev->msi_list));
  368. /* route the table */
  369. pci_intx_for_msi(dev, 0);
  370. msix_clear_and_set_ctrl(dev, 0,
  371. PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
  372. arch_restore_msi_irqs(dev);
  373. list_for_each_entry(entry, &dev->msi_list, list) {
  374. msix_mask_irq(entry, entry->masked);
  375. }
  376. msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
  377. }
  378. void pci_restore_msi_state(struct pci_dev *dev)
  379. {
  380. __pci_restore_msi_state(dev);
  381. __pci_restore_msix_state(dev);
  382. }
  383. EXPORT_SYMBOL_GPL(pci_restore_msi_state);
  384. static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
  385. char *buf)
  386. {
  387. struct msi_desc *entry;
  388. unsigned long irq;
  389. int retval;
  390. retval = kstrtoul(attr->attr.name, 10, &irq);
  391. if (retval)
  392. return retval;
  393. entry = irq_get_msi_desc(irq);
  394. if (entry)
  395. return sprintf(buf, "%s\n",
  396. entry->msi_attrib.is_msix ? "msix" : "msi");
  397. return -ENODEV;
  398. }
  399. static int populate_msi_sysfs(struct pci_dev *pdev)
  400. {
  401. struct attribute **msi_attrs;
  402. struct attribute *msi_attr;
  403. struct device_attribute *msi_dev_attr;
  404. struct attribute_group *msi_irq_group;
  405. const struct attribute_group **msi_irq_groups;
  406. struct msi_desc *entry;
  407. int ret = -ENOMEM;
  408. int num_msi = 0;
  409. int count = 0;
  410. /* Determine how many msi entries we have */
  411. list_for_each_entry(entry, &pdev->msi_list, list) {
  412. ++num_msi;
  413. }
  414. if (!num_msi)
  415. return 0;
  416. /* Dynamically create the MSI attributes for the PCI device */
  417. msi_attrs = kzalloc(sizeof(void *) * (num_msi + 1), GFP_KERNEL);
  418. if (!msi_attrs)
  419. return -ENOMEM;
  420. list_for_each_entry(entry, &pdev->msi_list, list) {
  421. msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
  422. if (!msi_dev_attr)
  423. goto error_attrs;
  424. msi_attrs[count] = &msi_dev_attr->attr;
  425. sysfs_attr_init(&msi_dev_attr->attr);
  426. msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
  427. entry->irq);
  428. if (!msi_dev_attr->attr.name)
  429. goto error_attrs;
  430. msi_dev_attr->attr.mode = S_IRUGO;
  431. msi_dev_attr->show = msi_mode_show;
  432. ++count;
  433. }
  434. msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL);
  435. if (!msi_irq_group)
  436. goto error_attrs;
  437. msi_irq_group->name = "msi_irqs";
  438. msi_irq_group->attrs = msi_attrs;
  439. msi_irq_groups = kzalloc(sizeof(void *) * 2, GFP_KERNEL);
  440. if (!msi_irq_groups)
  441. goto error_irq_group;
  442. msi_irq_groups[0] = msi_irq_group;
  443. ret = sysfs_create_groups(&pdev->dev.kobj, msi_irq_groups);
  444. if (ret)
  445. goto error_irq_groups;
  446. pdev->msi_irq_groups = msi_irq_groups;
  447. return 0;
  448. error_irq_groups:
  449. kfree(msi_irq_groups);
  450. error_irq_group:
  451. kfree(msi_irq_group);
  452. error_attrs:
  453. count = 0;
  454. msi_attr = msi_attrs[count];
  455. while (msi_attr) {
  456. msi_dev_attr = container_of(msi_attr, struct device_attribute, attr);
  457. kfree(msi_attr->name);
  458. kfree(msi_dev_attr);
  459. ++count;
  460. msi_attr = msi_attrs[count];
  461. }
  462. kfree(msi_attrs);
  463. return ret;
  464. }
  465. static struct msi_desc *msi_setup_entry(struct pci_dev *dev)
  466. {
  467. u16 control;
  468. struct msi_desc *entry;
  469. /* MSI Entry Initialization */
  470. entry = alloc_msi_entry(dev);
  471. if (!entry)
  472. return NULL;
  473. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  474. entry->msi_attrib.is_msix = 0;
  475. entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
  476. entry->msi_attrib.entry_nr = 0;
  477. entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
  478. entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
  479. entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
  480. if (control & PCI_MSI_FLAGS_64BIT)
  481. entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
  482. else
  483. entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
  484. /* Save the initial mask status */
  485. if (entry->msi_attrib.maskbit)
  486. pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
  487. return entry;
  488. }
  489. /**
  490. * msi_capability_init - configure device's MSI capability structure
  491. * @dev: pointer to the pci_dev data structure of MSI device function
  492. * @nvec: number of interrupts to allocate
  493. *
  494. * Setup the MSI capability structure of the device with the requested
  495. * number of interrupts. A return value of zero indicates the successful
  496. * setup of an entry with the new MSI irq. A negative return value indicates
  497. * an error, and a positive return value indicates the number of interrupts
  498. * which could have been allocated.
  499. */
  500. static int msi_capability_init(struct pci_dev *dev, int nvec)
  501. {
  502. struct msi_desc *entry;
  503. int ret;
  504. unsigned mask;
  505. msi_set_enable(dev, 0); /* Disable MSI during set up */
  506. entry = msi_setup_entry(dev);
  507. if (!entry)
  508. return -ENOMEM;
  509. /* All MSIs are unmasked by default, Mask them all */
  510. mask = msi_mask(entry->msi_attrib.multi_cap);
  511. msi_mask_irq(entry, mask, mask);
  512. list_add_tail(&entry->list, &dev->msi_list);
  513. /* Configure MSI capability structure */
  514. ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
  515. if (ret) {
  516. msi_mask_irq(entry, mask, ~mask);
  517. free_msi_irqs(dev);
  518. return ret;
  519. }
  520. ret = populate_msi_sysfs(dev);
  521. if (ret) {
  522. msi_mask_irq(entry, mask, ~mask);
  523. free_msi_irqs(dev);
  524. return ret;
  525. }
  526. /* Set MSI enabled bits */
  527. pci_intx_for_msi(dev, 0);
  528. msi_set_enable(dev, 1);
  529. dev->msi_enabled = 1;
  530. dev->irq = entry->irq;
  531. return 0;
  532. }
  533. static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
  534. {
  535. resource_size_t phys_addr;
  536. u32 table_offset;
  537. u8 bir;
  538. pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
  539. &table_offset);
  540. bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
  541. table_offset &= PCI_MSIX_TABLE_OFFSET;
  542. phys_addr = pci_resource_start(dev, bir) + table_offset;
  543. return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  544. }
  545. static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
  546. struct msix_entry *entries, int nvec)
  547. {
  548. struct msi_desc *entry;
  549. int i;
  550. for (i = 0; i < nvec; i++) {
  551. entry = alloc_msi_entry(dev);
  552. if (!entry) {
  553. if (!i)
  554. iounmap(base);
  555. else
  556. free_msi_irqs(dev);
  557. /* No enough memory. Don't try again */
  558. return -ENOMEM;
  559. }
  560. entry->msi_attrib.is_msix = 1;
  561. entry->msi_attrib.is_64 = 1;
  562. entry->msi_attrib.entry_nr = entries[i].entry;
  563. entry->msi_attrib.default_irq = dev->irq;
  564. entry->mask_base = base;
  565. list_add_tail(&entry->list, &dev->msi_list);
  566. }
  567. return 0;
  568. }
  569. static void msix_program_entries(struct pci_dev *dev,
  570. struct msix_entry *entries)
  571. {
  572. struct msi_desc *entry;
  573. int i = 0;
  574. list_for_each_entry(entry, &dev->msi_list, list) {
  575. int offset = entries[i].entry * PCI_MSIX_ENTRY_SIZE +
  576. PCI_MSIX_ENTRY_VECTOR_CTRL;
  577. entries[i].vector = entry->irq;
  578. irq_set_msi_desc(entry->irq, entry);
  579. entry->masked = readl(entry->mask_base + offset);
  580. msix_mask_irq(entry, 1);
  581. i++;
  582. }
  583. }
  584. /**
  585. * msix_capability_init - configure device's MSI-X capability
  586. * @dev: pointer to the pci_dev data structure of MSI-X device function
  587. * @entries: pointer to an array of struct msix_entry entries
  588. * @nvec: number of @entries
  589. *
  590. * Setup the MSI-X capability structure of device function with a
  591. * single MSI-X irq. A return of zero indicates the successful setup of
  592. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  593. **/
  594. static int msix_capability_init(struct pci_dev *dev,
  595. struct msix_entry *entries, int nvec)
  596. {
  597. int ret;
  598. u16 control;
  599. void __iomem *base;
  600. /* Ensure MSI-X is disabled while it is set up */
  601. msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
  602. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  603. /* Request & Map MSI-X table region */
  604. base = msix_map_region(dev, msix_table_size(control));
  605. if (!base)
  606. return -ENOMEM;
  607. ret = msix_setup_entries(dev, base, entries, nvec);
  608. if (ret)
  609. return ret;
  610. ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
  611. if (ret)
  612. goto out_avail;
  613. /*
  614. * Some devices require MSI-X to be enabled before we can touch the
  615. * MSI-X registers. We need to mask all the vectors to prevent
  616. * interrupts coming in before they're fully set up.
  617. */
  618. msix_clear_and_set_ctrl(dev, 0,
  619. PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE);
  620. msix_program_entries(dev, entries);
  621. ret = populate_msi_sysfs(dev);
  622. if (ret)
  623. goto out_free;
  624. /* Set MSI-X enabled bits and unmask the function */
  625. pci_intx_for_msi(dev, 0);
  626. dev->msix_enabled = 1;
  627. msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
  628. return 0;
  629. out_avail:
  630. if (ret < 0) {
  631. /*
  632. * If we had some success, report the number of irqs
  633. * we succeeded in setting up.
  634. */
  635. struct msi_desc *entry;
  636. int avail = 0;
  637. list_for_each_entry(entry, &dev->msi_list, list) {
  638. if (entry->irq != 0)
  639. avail++;
  640. }
  641. if (avail != 0)
  642. ret = avail;
  643. }
  644. out_free:
  645. free_msi_irqs(dev);
  646. return ret;
  647. }
  648. /**
  649. * pci_msi_supported - check whether MSI may be enabled on a device
  650. * @dev: pointer to the pci_dev data structure of MSI device function
  651. * @nvec: how many MSIs have been requested ?
  652. *
  653. * Look at global flags, the device itself, and its parent buses
  654. * to determine if MSI/-X are supported for the device. If MSI/-X is
  655. * supported return 1, else return 0.
  656. **/
  657. static int pci_msi_supported(struct pci_dev *dev, int nvec)
  658. {
  659. struct pci_bus *bus;
  660. /* MSI must be globally enabled and supported by the device */
  661. if (!pci_msi_enable)
  662. return 0;
  663. if (!dev || dev->no_msi || dev->current_state != PCI_D0)
  664. return 0;
  665. /*
  666. * You can't ask to have 0 or less MSIs configured.
  667. * a) it's stupid ..
  668. * b) the list manipulation code assumes nvec >= 1.
  669. */
  670. if (nvec < 1)
  671. return 0;
  672. /*
  673. * Any bridge which does NOT route MSI transactions from its
  674. * secondary bus to its primary bus must set NO_MSI flag on
  675. * the secondary pci_bus.
  676. * We expect only arch-specific PCI host bus controller driver
  677. * or quirks for specific PCI bridges to be setting NO_MSI.
  678. */
  679. for (bus = dev->bus; bus; bus = bus->parent)
  680. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  681. return 0;
  682. return 1;
  683. }
  684. /**
  685. * pci_msi_vec_count - Return the number of MSI vectors a device can send
  686. * @dev: device to report about
  687. *
  688. * This function returns the number of MSI vectors a device requested via
  689. * Multiple Message Capable register. It returns a negative errno if the
  690. * device is not capable sending MSI interrupts. Otherwise, the call succeeds
  691. * and returns a power of two, up to a maximum of 2^5 (32), according to the
  692. * MSI specification.
  693. **/
  694. int pci_msi_vec_count(struct pci_dev *dev)
  695. {
  696. int ret;
  697. u16 msgctl;
  698. if (!dev->msi_cap)
  699. return -EINVAL;
  700. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
  701. ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
  702. return ret;
  703. }
  704. EXPORT_SYMBOL(pci_msi_vec_count);
  705. void pci_msi_shutdown(struct pci_dev *dev)
  706. {
  707. struct msi_desc *desc;
  708. u32 mask;
  709. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  710. return;
  711. BUG_ON(list_empty(&dev->msi_list));
  712. desc = list_first_entry(&dev->msi_list, struct msi_desc, list);
  713. msi_set_enable(dev, 0);
  714. pci_intx_for_msi(dev, 1);
  715. dev->msi_enabled = 0;
  716. /* Return the device with MSI unmasked as initial states */
  717. mask = msi_mask(desc->msi_attrib.multi_cap);
  718. /* Keep cached state to be restored */
  719. arch_msi_mask_irq(desc, mask, ~mask);
  720. /* Restore dev->irq to its default pin-assertion irq */
  721. dev->irq = desc->msi_attrib.default_irq;
  722. }
  723. void pci_disable_msi(struct pci_dev *dev)
  724. {
  725. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  726. return;
  727. pci_msi_shutdown(dev);
  728. free_msi_irqs(dev);
  729. }
  730. EXPORT_SYMBOL(pci_disable_msi);
  731. /**
  732. * pci_msix_vec_count - return the number of device's MSI-X table entries
  733. * @dev: pointer to the pci_dev data structure of MSI-X device function
  734. * This function returns the number of device's MSI-X table entries and
  735. * therefore the number of MSI-X vectors device is capable of sending.
  736. * It returns a negative errno if the device is not capable of sending MSI-X
  737. * interrupts.
  738. **/
  739. int pci_msix_vec_count(struct pci_dev *dev)
  740. {
  741. u16 control;
  742. if (!dev->msix_cap)
  743. return -EINVAL;
  744. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  745. return msix_table_size(control);
  746. }
  747. EXPORT_SYMBOL(pci_msix_vec_count);
  748. /**
  749. * pci_enable_msix - configure device's MSI-X capability structure
  750. * @dev: pointer to the pci_dev data structure of MSI-X device function
  751. * @entries: pointer to an array of MSI-X entries
  752. * @nvec: number of MSI-X irqs requested for allocation by device driver
  753. *
  754. * Setup the MSI-X capability structure of device function with the number
  755. * of requested irqs upon its software driver call to request for
  756. * MSI-X mode enabled on its hardware device function. A return of zero
  757. * indicates the successful configuration of MSI-X capability structure
  758. * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
  759. * Or a return of > 0 indicates that driver request is exceeding the number
  760. * of irqs or MSI-X vectors available. Driver should use the returned value to
  761. * re-send its request.
  762. **/
  763. int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
  764. {
  765. int nr_entries;
  766. int i, j;
  767. if (!pci_msi_supported(dev, nvec))
  768. return -EINVAL;
  769. if (!entries)
  770. return -EINVAL;
  771. nr_entries = pci_msix_vec_count(dev);
  772. if (nr_entries < 0)
  773. return nr_entries;
  774. if (nvec > nr_entries)
  775. return nr_entries;
  776. /* Check for any invalid entries */
  777. for (i = 0; i < nvec; i++) {
  778. if (entries[i].entry >= nr_entries)
  779. return -EINVAL; /* invalid entry */
  780. for (j = i + 1; j < nvec; j++) {
  781. if (entries[i].entry == entries[j].entry)
  782. return -EINVAL; /* duplicate entry */
  783. }
  784. }
  785. WARN_ON(!!dev->msix_enabled);
  786. /* Check whether driver already requested for MSI irq */
  787. if (dev->msi_enabled) {
  788. dev_info(&dev->dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
  789. return -EINVAL;
  790. }
  791. return msix_capability_init(dev, entries, nvec);
  792. }
  793. EXPORT_SYMBOL(pci_enable_msix);
  794. void pci_msix_shutdown(struct pci_dev *dev)
  795. {
  796. struct msi_desc *entry;
  797. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  798. return;
  799. /* Return the device with MSI-X masked as initial states */
  800. list_for_each_entry(entry, &dev->msi_list, list) {
  801. /* Keep cached states to be restored */
  802. arch_msix_mask_irq(entry, 1);
  803. }
  804. msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
  805. pci_intx_for_msi(dev, 1);
  806. dev->msix_enabled = 0;
  807. }
  808. void pci_disable_msix(struct pci_dev *dev)
  809. {
  810. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  811. return;
  812. pci_msix_shutdown(dev);
  813. free_msi_irqs(dev);
  814. }
  815. EXPORT_SYMBOL(pci_disable_msix);
  816. void pci_no_msi(void)
  817. {
  818. pci_msi_enable = 0;
  819. }
  820. /**
  821. * pci_msi_enabled - is MSI enabled?
  822. *
  823. * Returns true if MSI has not been disabled by the command-line option
  824. * pci=nomsi.
  825. **/
  826. int pci_msi_enabled(void)
  827. {
  828. return pci_msi_enable;
  829. }
  830. EXPORT_SYMBOL(pci_msi_enabled);
  831. void pci_msi_init_pci_dev(struct pci_dev *dev)
  832. {
  833. INIT_LIST_HEAD(&dev->msi_list);
  834. /* Disable the msi hardware to avoid screaming interrupts
  835. * during boot. This is the power on reset default so
  836. * usually this should be a noop.
  837. */
  838. dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI);
  839. if (dev->msi_cap)
  840. msi_set_enable(dev, 0);
  841. dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  842. if (dev->msix_cap)
  843. msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
  844. }
  845. /**
  846. * pci_enable_msi_range - configure device's MSI capability structure
  847. * @dev: device to configure
  848. * @minvec: minimal number of interrupts to configure
  849. * @maxvec: maximum number of interrupts to configure
  850. *
  851. * This function tries to allocate a maximum possible number of interrupts in a
  852. * range between @minvec and @maxvec. It returns a negative errno if an error
  853. * occurs. If it succeeds, it returns the actual number of interrupts allocated
  854. * and updates the @dev's irq member to the lowest new interrupt number;
  855. * the other interrupt numbers allocated to this device are consecutive.
  856. **/
  857. int pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec)
  858. {
  859. int nvec;
  860. int rc;
  861. if (!pci_msi_supported(dev, minvec))
  862. return -EINVAL;
  863. WARN_ON(!!dev->msi_enabled);
  864. /* Check whether driver already requested MSI-X irqs */
  865. if (dev->msix_enabled) {
  866. dev_info(&dev->dev,
  867. "can't enable MSI (MSI-X already enabled)\n");
  868. return -EINVAL;
  869. }
  870. if (maxvec < minvec)
  871. return -ERANGE;
  872. nvec = pci_msi_vec_count(dev);
  873. if (nvec < 0)
  874. return nvec;
  875. else if (nvec < minvec)
  876. return -EINVAL;
  877. else if (nvec > maxvec)
  878. nvec = maxvec;
  879. do {
  880. rc = msi_capability_init(dev, nvec);
  881. if (rc < 0) {
  882. return rc;
  883. } else if (rc > 0) {
  884. if (rc < minvec)
  885. return -ENOSPC;
  886. nvec = rc;
  887. }
  888. } while (rc);
  889. return nvec;
  890. }
  891. EXPORT_SYMBOL(pci_enable_msi_range);
  892. /**
  893. * pci_enable_msix_range - configure device's MSI-X capability structure
  894. * @dev: pointer to the pci_dev data structure of MSI-X device function
  895. * @entries: pointer to an array of MSI-X entries
  896. * @minvec: minimum number of MSI-X irqs requested
  897. * @maxvec: maximum number of MSI-X irqs requested
  898. *
  899. * Setup the MSI-X capability structure of device function with a maximum
  900. * possible number of interrupts in the range between @minvec and @maxvec
  901. * upon its software driver call to request for MSI-X mode enabled on its
  902. * hardware device function. It returns a negative errno if an error occurs.
  903. * If it succeeds, it returns the actual number of interrupts allocated and
  904. * indicates the successful configuration of MSI-X capability structure
  905. * with new allocated MSI-X interrupts.
  906. **/
  907. int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
  908. int minvec, int maxvec)
  909. {
  910. int nvec = maxvec;
  911. int rc;
  912. if (maxvec < minvec)
  913. return -ERANGE;
  914. do {
  915. rc = pci_enable_msix(dev, entries, nvec);
  916. if (rc < 0) {
  917. return rc;
  918. } else if (rc > 0) {
  919. if (rc < minvec)
  920. return -ENOSPC;
  921. nvec = rc;
  922. }
  923. } while (rc);
  924. return nvec;
  925. }
  926. EXPORT_SYMBOL(pci_enable_msix_range);