msi.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  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. * Copyright (C) 2016 Christoph Hellwig.
  8. */
  9. #include <linux/err.h>
  10. #include <linux/mm.h>
  11. #include <linux/irq.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/export.h>
  14. #include <linux/ioport.h>
  15. #include <linux/pci.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/msi.h>
  18. #include <linux/smp.h>
  19. #include <linux/errno.h>
  20. #include <linux/io.h>
  21. #include <linux/acpi_iort.h>
  22. #include <linux/slab.h>
  23. #include <linux/irqdomain.h>
  24. #include <linux/of_irq.h>
  25. #include "pci.h"
  26. static int pci_msi_enable = 1;
  27. int pci_msi_ignore_mask;
  28. #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1)
  29. #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
  30. static int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  31. {
  32. struct irq_domain *domain;
  33. domain = dev_get_msi_domain(&dev->dev);
  34. if (domain && irq_domain_is_hierarchy(domain))
  35. return msi_domain_alloc_irqs(domain, &dev->dev, nvec);
  36. return arch_setup_msi_irqs(dev, nvec, type);
  37. }
  38. static void pci_msi_teardown_msi_irqs(struct pci_dev *dev)
  39. {
  40. struct irq_domain *domain;
  41. domain = dev_get_msi_domain(&dev->dev);
  42. if (domain && irq_domain_is_hierarchy(domain))
  43. msi_domain_free_irqs(domain, &dev->dev);
  44. else
  45. arch_teardown_msi_irqs(dev);
  46. }
  47. #else
  48. #define pci_msi_setup_msi_irqs arch_setup_msi_irqs
  49. #define pci_msi_teardown_msi_irqs arch_teardown_msi_irqs
  50. #endif
  51. /* Arch hooks */
  52. int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  53. {
  54. struct msi_controller *chip = dev->bus->msi;
  55. int err;
  56. if (!chip || !chip->setup_irq)
  57. return -EINVAL;
  58. err = chip->setup_irq(chip, dev, desc);
  59. if (err < 0)
  60. return err;
  61. irq_set_chip_data(desc->irq, chip);
  62. return 0;
  63. }
  64. void __weak arch_teardown_msi_irq(unsigned int irq)
  65. {
  66. struct msi_controller *chip = irq_get_chip_data(irq);
  67. if (!chip || !chip->teardown_irq)
  68. return;
  69. chip->teardown_irq(chip, irq);
  70. }
  71. int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  72. {
  73. struct msi_controller *chip = dev->bus->msi;
  74. struct msi_desc *entry;
  75. int ret;
  76. if (chip && chip->setup_irqs)
  77. return chip->setup_irqs(chip, dev, nvec, type);
  78. /*
  79. * If an architecture wants to support multiple MSI, it needs to
  80. * override arch_setup_msi_irqs()
  81. */
  82. if (type == PCI_CAP_ID_MSI && nvec > 1)
  83. return 1;
  84. for_each_pci_msi_entry(entry, dev) {
  85. ret = arch_setup_msi_irq(dev, entry);
  86. if (ret < 0)
  87. return ret;
  88. if (ret > 0)
  89. return -ENOSPC;
  90. }
  91. return 0;
  92. }
  93. /*
  94. * We have a default implementation available as a separate non-weak
  95. * function, as it is used by the Xen x86 PCI code
  96. */
  97. void default_teardown_msi_irqs(struct pci_dev *dev)
  98. {
  99. int i;
  100. struct msi_desc *entry;
  101. for_each_pci_msi_entry(entry, dev)
  102. if (entry->irq)
  103. for (i = 0; i < entry->nvec_used; i++)
  104. arch_teardown_msi_irq(entry->irq + i);
  105. }
  106. void __weak arch_teardown_msi_irqs(struct pci_dev *dev)
  107. {
  108. return default_teardown_msi_irqs(dev);
  109. }
  110. static void default_restore_msi_irq(struct pci_dev *dev, int irq)
  111. {
  112. struct msi_desc *entry;
  113. entry = NULL;
  114. if (dev->msix_enabled) {
  115. for_each_pci_msi_entry(entry, dev) {
  116. if (irq == entry->irq)
  117. break;
  118. }
  119. } else if (dev->msi_enabled) {
  120. entry = irq_get_msi_desc(irq);
  121. }
  122. if (entry)
  123. __pci_write_msi_msg(entry, &entry->msg);
  124. }
  125. void __weak arch_restore_msi_irqs(struct pci_dev *dev)
  126. {
  127. return default_restore_msi_irqs(dev);
  128. }
  129. static inline __attribute_const__ u32 msi_mask(unsigned x)
  130. {
  131. /* Don't shift by >= width of type */
  132. if (x >= 5)
  133. return 0xffffffff;
  134. return (1 << (1 << x)) - 1;
  135. }
  136. /*
  137. * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to
  138. * mask all MSI interrupts by clearing the MSI enable bit does not work
  139. * reliably as devices without an INTx disable bit will then generate a
  140. * level IRQ which will never be cleared.
  141. */
  142. u32 __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
  143. {
  144. u32 mask_bits = desc->masked;
  145. if (pci_msi_ignore_mask || !desc->msi_attrib.maskbit)
  146. return 0;
  147. mask_bits &= ~mask;
  148. mask_bits |= flag;
  149. pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos,
  150. mask_bits);
  151. return mask_bits;
  152. }
  153. static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag)
  154. {
  155. desc->masked = __pci_msi_desc_mask_irq(desc, mask, flag);
  156. }
  157. static void __iomem *pci_msix_desc_addr(struct msi_desc *desc)
  158. {
  159. return desc->mask_base +
  160. desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  161. }
  162. /*
  163. * This internal function does not flush PCI writes to the device.
  164. * All users must ensure that they read from the device before either
  165. * assuming that the device state is up to date, or returning out of this
  166. * file. This saves a few milliseconds when initialising devices with lots
  167. * of MSI-X interrupts.
  168. */
  169. u32 __pci_msix_desc_mask_irq(struct msi_desc *desc, u32 flag)
  170. {
  171. u32 mask_bits = desc->masked;
  172. if (pci_msi_ignore_mask)
  173. return 0;
  174. mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
  175. if (flag)
  176. mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
  177. writel(mask_bits, pci_msix_desc_addr(desc) + PCI_MSIX_ENTRY_VECTOR_CTRL);
  178. return mask_bits;
  179. }
  180. static void msix_mask_irq(struct msi_desc *desc, u32 flag)
  181. {
  182. desc->masked = __pci_msix_desc_mask_irq(desc, flag);
  183. }
  184. static void msi_set_mask_bit(struct irq_data *data, u32 flag)
  185. {
  186. struct msi_desc *desc = irq_data_get_msi_desc(data);
  187. if (desc->msi_attrib.is_msix) {
  188. msix_mask_irq(desc, flag);
  189. readl(desc->mask_base); /* Flush write to device */
  190. } else {
  191. unsigned offset = data->irq - desc->irq;
  192. msi_mask_irq(desc, 1 << offset, flag << offset);
  193. }
  194. }
  195. /**
  196. * pci_msi_mask_irq - Generic irq chip callback to mask PCI/MSI interrupts
  197. * @data: pointer to irqdata associated to that interrupt
  198. */
  199. void pci_msi_mask_irq(struct irq_data *data)
  200. {
  201. msi_set_mask_bit(data, 1);
  202. }
  203. EXPORT_SYMBOL_GPL(pci_msi_mask_irq);
  204. /**
  205. * pci_msi_unmask_irq - Generic irq chip callback to unmask PCI/MSI interrupts
  206. * @data: pointer to irqdata associated to that interrupt
  207. */
  208. void pci_msi_unmask_irq(struct irq_data *data)
  209. {
  210. msi_set_mask_bit(data, 0);
  211. }
  212. EXPORT_SYMBOL_GPL(pci_msi_unmask_irq);
  213. void default_restore_msi_irqs(struct pci_dev *dev)
  214. {
  215. struct msi_desc *entry;
  216. for_each_pci_msi_entry(entry, dev)
  217. default_restore_msi_irq(dev, entry->irq);
  218. }
  219. void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  220. {
  221. struct pci_dev *dev = msi_desc_to_pci_dev(entry);
  222. BUG_ON(dev->current_state != PCI_D0);
  223. if (entry->msi_attrib.is_msix) {
  224. void __iomem *base = pci_msix_desc_addr(entry);
  225. msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
  226. msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
  227. msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
  228. } else {
  229. int pos = dev->msi_cap;
  230. u16 data;
  231. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
  232. &msg->address_lo);
  233. if (entry->msi_attrib.is_64) {
  234. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
  235. &msg->address_hi);
  236. pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
  237. } else {
  238. msg->address_hi = 0;
  239. pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
  240. }
  241. msg->data = data;
  242. }
  243. }
  244. void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  245. {
  246. struct pci_dev *dev = msi_desc_to_pci_dev(entry);
  247. if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
  248. /* Don't touch the hardware now */
  249. } else if (entry->msi_attrib.is_msix) {
  250. void __iomem *base = pci_msix_desc_addr(entry);
  251. writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
  252. writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
  253. writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
  254. } else {
  255. int pos = dev->msi_cap;
  256. u16 msgctl;
  257. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
  258. msgctl &= ~PCI_MSI_FLAGS_QSIZE;
  259. msgctl |= entry->msi_attrib.multiple << 4;
  260. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
  261. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
  262. msg->address_lo);
  263. if (entry->msi_attrib.is_64) {
  264. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
  265. msg->address_hi);
  266. pci_write_config_word(dev, pos + PCI_MSI_DATA_64,
  267. msg->data);
  268. } else {
  269. pci_write_config_word(dev, pos + PCI_MSI_DATA_32,
  270. msg->data);
  271. }
  272. }
  273. entry->msg = *msg;
  274. }
  275. void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
  276. {
  277. struct msi_desc *entry = irq_get_msi_desc(irq);
  278. __pci_write_msi_msg(entry, msg);
  279. }
  280. EXPORT_SYMBOL_GPL(pci_write_msi_msg);
  281. static void free_msi_irqs(struct pci_dev *dev)
  282. {
  283. struct list_head *msi_list = dev_to_msi_list(&dev->dev);
  284. struct msi_desc *entry, *tmp;
  285. struct attribute **msi_attrs;
  286. struct device_attribute *dev_attr;
  287. int i, count = 0;
  288. for_each_pci_msi_entry(entry, dev)
  289. if (entry->irq)
  290. for (i = 0; i < entry->nvec_used; i++)
  291. BUG_ON(irq_has_action(entry->irq + i));
  292. pci_msi_teardown_msi_irqs(dev);
  293. list_for_each_entry_safe(entry, tmp, msi_list, list) {
  294. if (entry->msi_attrib.is_msix) {
  295. if (list_is_last(&entry->list, msi_list))
  296. iounmap(entry->mask_base);
  297. }
  298. list_del(&entry->list);
  299. free_msi_entry(entry);
  300. }
  301. if (dev->msi_irq_groups) {
  302. sysfs_remove_groups(&dev->dev.kobj, dev->msi_irq_groups);
  303. msi_attrs = dev->msi_irq_groups[0]->attrs;
  304. while (msi_attrs[count]) {
  305. dev_attr = container_of(msi_attrs[count],
  306. struct device_attribute, attr);
  307. kfree(dev_attr->attr.name);
  308. kfree(dev_attr);
  309. ++count;
  310. }
  311. kfree(msi_attrs);
  312. kfree(dev->msi_irq_groups[0]);
  313. kfree(dev->msi_irq_groups);
  314. dev->msi_irq_groups = NULL;
  315. }
  316. }
  317. static void pci_intx_for_msi(struct pci_dev *dev, int enable)
  318. {
  319. if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
  320. pci_intx(dev, enable);
  321. }
  322. static void __pci_restore_msi_state(struct pci_dev *dev)
  323. {
  324. u16 control;
  325. struct msi_desc *entry;
  326. if (!dev->msi_enabled)
  327. return;
  328. entry = irq_get_msi_desc(dev->irq);
  329. pci_intx_for_msi(dev, 0);
  330. pci_msi_set_enable(dev, 0);
  331. arch_restore_msi_irqs(dev);
  332. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  333. msi_mask_irq(entry, msi_mask(entry->msi_attrib.multi_cap),
  334. entry->masked);
  335. control &= ~PCI_MSI_FLAGS_QSIZE;
  336. control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
  337. pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
  338. }
  339. static void __pci_restore_msix_state(struct pci_dev *dev)
  340. {
  341. struct msi_desc *entry;
  342. if (!dev->msix_enabled)
  343. return;
  344. BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
  345. /* route the table */
  346. pci_intx_for_msi(dev, 0);
  347. pci_msix_clear_and_set_ctrl(dev, 0,
  348. PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
  349. arch_restore_msi_irqs(dev);
  350. for_each_pci_msi_entry(entry, dev)
  351. msix_mask_irq(entry, entry->masked);
  352. pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
  353. }
  354. void pci_restore_msi_state(struct pci_dev *dev)
  355. {
  356. __pci_restore_msi_state(dev);
  357. __pci_restore_msix_state(dev);
  358. }
  359. EXPORT_SYMBOL_GPL(pci_restore_msi_state);
  360. static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
  361. char *buf)
  362. {
  363. struct msi_desc *entry;
  364. unsigned long irq;
  365. int retval;
  366. retval = kstrtoul(attr->attr.name, 10, &irq);
  367. if (retval)
  368. return retval;
  369. entry = irq_get_msi_desc(irq);
  370. if (entry)
  371. return sprintf(buf, "%s\n",
  372. entry->msi_attrib.is_msix ? "msix" : "msi");
  373. return -ENODEV;
  374. }
  375. static int populate_msi_sysfs(struct pci_dev *pdev)
  376. {
  377. struct attribute **msi_attrs;
  378. struct attribute *msi_attr;
  379. struct device_attribute *msi_dev_attr;
  380. struct attribute_group *msi_irq_group;
  381. const struct attribute_group **msi_irq_groups;
  382. struct msi_desc *entry;
  383. int ret = -ENOMEM;
  384. int num_msi = 0;
  385. int count = 0;
  386. int i;
  387. /* Determine how many msi entries we have */
  388. for_each_pci_msi_entry(entry, pdev)
  389. num_msi += entry->nvec_used;
  390. if (!num_msi)
  391. return 0;
  392. /* Dynamically create the MSI attributes for the PCI device */
  393. msi_attrs = kzalloc(sizeof(void *) * (num_msi + 1), GFP_KERNEL);
  394. if (!msi_attrs)
  395. return -ENOMEM;
  396. for_each_pci_msi_entry(entry, pdev) {
  397. for (i = 0; i < entry->nvec_used; i++) {
  398. msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL);
  399. if (!msi_dev_attr)
  400. goto error_attrs;
  401. msi_attrs[count] = &msi_dev_attr->attr;
  402. sysfs_attr_init(&msi_dev_attr->attr);
  403. msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d",
  404. entry->irq + i);
  405. if (!msi_dev_attr->attr.name)
  406. goto error_attrs;
  407. msi_dev_attr->attr.mode = S_IRUGO;
  408. msi_dev_attr->show = msi_mode_show;
  409. ++count;
  410. }
  411. }
  412. msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL);
  413. if (!msi_irq_group)
  414. goto error_attrs;
  415. msi_irq_group->name = "msi_irqs";
  416. msi_irq_group->attrs = msi_attrs;
  417. msi_irq_groups = kzalloc(sizeof(void *) * 2, GFP_KERNEL);
  418. if (!msi_irq_groups)
  419. goto error_irq_group;
  420. msi_irq_groups[0] = msi_irq_group;
  421. ret = sysfs_create_groups(&pdev->dev.kobj, msi_irq_groups);
  422. if (ret)
  423. goto error_irq_groups;
  424. pdev->msi_irq_groups = msi_irq_groups;
  425. return 0;
  426. error_irq_groups:
  427. kfree(msi_irq_groups);
  428. error_irq_group:
  429. kfree(msi_irq_group);
  430. error_attrs:
  431. count = 0;
  432. msi_attr = msi_attrs[count];
  433. while (msi_attr) {
  434. msi_dev_attr = container_of(msi_attr, struct device_attribute, attr);
  435. kfree(msi_attr->name);
  436. kfree(msi_dev_attr);
  437. ++count;
  438. msi_attr = msi_attrs[count];
  439. }
  440. kfree(msi_attrs);
  441. return ret;
  442. }
  443. static struct msi_desc *
  444. msi_setup_entry(struct pci_dev *dev, int nvec, const struct irq_affinity *affd)
  445. {
  446. struct cpumask *masks = NULL;
  447. struct msi_desc *entry;
  448. u16 control;
  449. if (affd)
  450. masks = irq_create_affinity_masks(nvec, affd);
  451. /* MSI Entry Initialization */
  452. entry = alloc_msi_entry(&dev->dev, nvec, masks);
  453. if (!entry)
  454. goto out;
  455. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  456. entry->msi_attrib.is_msix = 0;
  457. entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
  458. entry->msi_attrib.entry_nr = 0;
  459. entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
  460. entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
  461. entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
  462. entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
  463. if (control & PCI_MSI_FLAGS_64BIT)
  464. entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
  465. else
  466. entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
  467. /* Save the initial mask status */
  468. if (entry->msi_attrib.maskbit)
  469. pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
  470. out:
  471. kfree(masks);
  472. return entry;
  473. }
  474. static int msi_verify_entries(struct pci_dev *dev)
  475. {
  476. struct msi_desc *entry;
  477. for_each_pci_msi_entry(entry, dev) {
  478. if (!dev->no_64bit_msi || !entry->msg.address_hi)
  479. continue;
  480. dev_err(&dev->dev, "Device has broken 64-bit MSI but arch"
  481. " tried to assign one above 4G\n");
  482. return -EIO;
  483. }
  484. return 0;
  485. }
  486. /**
  487. * msi_capability_init - configure device's MSI capability structure
  488. * @dev: pointer to the pci_dev data structure of MSI device function
  489. * @nvec: number of interrupts to allocate
  490. * @affd: description of automatic irq affinity assignments (may be %NULL)
  491. *
  492. * Setup the MSI capability structure of the device with the requested
  493. * number of interrupts. A return value of zero indicates the successful
  494. * setup of an entry with the new MSI irq. A negative return value indicates
  495. * an error, and a positive return value indicates the number of interrupts
  496. * which could have been allocated.
  497. */
  498. static int msi_capability_init(struct pci_dev *dev, int nvec,
  499. const struct irq_affinity *affd)
  500. {
  501. struct msi_desc *entry;
  502. int ret;
  503. unsigned mask;
  504. pci_msi_set_enable(dev, 0); /* Disable MSI during set up */
  505. entry = msi_setup_entry(dev, nvec, affd);
  506. if (!entry)
  507. return -ENOMEM;
  508. /* All MSIs are unmasked by default, Mask them all */
  509. mask = msi_mask(entry->msi_attrib.multi_cap);
  510. msi_mask_irq(entry, mask, mask);
  511. list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
  512. /* Configure MSI capability structure */
  513. ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
  514. if (ret) {
  515. msi_mask_irq(entry, mask, ~mask);
  516. free_msi_irqs(dev);
  517. return ret;
  518. }
  519. ret = msi_verify_entries(dev);
  520. if (ret) {
  521. msi_mask_irq(entry, mask, ~mask);
  522. free_msi_irqs(dev);
  523. return ret;
  524. }
  525. ret = populate_msi_sysfs(dev);
  526. if (ret) {
  527. msi_mask_irq(entry, mask, ~mask);
  528. free_msi_irqs(dev);
  529. return ret;
  530. }
  531. /* Set MSI enabled bits */
  532. pci_intx_for_msi(dev, 0);
  533. pci_msi_set_enable(dev, 1);
  534. dev->msi_enabled = 1;
  535. pcibios_free_irq(dev);
  536. dev->irq = entry->irq;
  537. return 0;
  538. }
  539. static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
  540. {
  541. resource_size_t phys_addr;
  542. u32 table_offset;
  543. unsigned long flags;
  544. u8 bir;
  545. pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
  546. &table_offset);
  547. bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
  548. flags = pci_resource_flags(dev, bir);
  549. if (!flags || (flags & IORESOURCE_UNSET))
  550. return NULL;
  551. table_offset &= PCI_MSIX_TABLE_OFFSET;
  552. phys_addr = pci_resource_start(dev, bir) + table_offset;
  553. return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  554. }
  555. static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
  556. struct msix_entry *entries, int nvec,
  557. const struct irq_affinity *affd)
  558. {
  559. struct cpumask *curmsk, *masks = NULL;
  560. struct msi_desc *entry;
  561. int ret, i;
  562. if (affd)
  563. masks = irq_create_affinity_masks(nvec, affd);
  564. for (i = 0, curmsk = masks; i < nvec; i++) {
  565. entry = alloc_msi_entry(&dev->dev, 1, curmsk);
  566. if (!entry) {
  567. if (!i)
  568. iounmap(base);
  569. else
  570. free_msi_irqs(dev);
  571. /* No enough memory. Don't try again */
  572. ret = -ENOMEM;
  573. goto out;
  574. }
  575. entry->msi_attrib.is_msix = 1;
  576. entry->msi_attrib.is_64 = 1;
  577. if (entries)
  578. entry->msi_attrib.entry_nr = entries[i].entry;
  579. else
  580. entry->msi_attrib.entry_nr = i;
  581. entry->msi_attrib.default_irq = dev->irq;
  582. entry->mask_base = base;
  583. list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
  584. if (masks)
  585. curmsk++;
  586. }
  587. ret = 0;
  588. out:
  589. kfree(masks);
  590. return ret;
  591. }
  592. static void msix_program_entries(struct pci_dev *dev,
  593. struct msix_entry *entries)
  594. {
  595. struct msi_desc *entry;
  596. int i = 0;
  597. for_each_pci_msi_entry(entry, dev) {
  598. if (entries)
  599. entries[i++].vector = entry->irq;
  600. entry->masked = readl(pci_msix_desc_addr(entry) +
  601. PCI_MSIX_ENTRY_VECTOR_CTRL);
  602. msix_mask_irq(entry, 1);
  603. }
  604. }
  605. /**
  606. * msix_capability_init - configure device's MSI-X capability
  607. * @dev: pointer to the pci_dev data structure of MSI-X device function
  608. * @entries: pointer to an array of struct msix_entry entries
  609. * @nvec: number of @entries
  610. * @affd: Optional pointer to enable automatic affinity assignement
  611. *
  612. * Setup the MSI-X capability structure of device function with a
  613. * single MSI-X irq. A return of zero indicates the successful setup of
  614. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  615. **/
  616. static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
  617. int nvec, const struct irq_affinity *affd)
  618. {
  619. int ret;
  620. u16 control;
  621. void __iomem *base;
  622. /* Ensure MSI-X is disabled while it is set up */
  623. pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
  624. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  625. /* Request & Map MSI-X table region */
  626. base = msix_map_region(dev, msix_table_size(control));
  627. if (!base)
  628. return -ENOMEM;
  629. ret = msix_setup_entries(dev, base, entries, nvec, affd);
  630. if (ret)
  631. return ret;
  632. ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
  633. if (ret)
  634. goto out_avail;
  635. /* Check if all MSI entries honor device restrictions */
  636. ret = msi_verify_entries(dev);
  637. if (ret)
  638. goto out_free;
  639. /*
  640. * Some devices require MSI-X to be enabled before we can touch the
  641. * MSI-X registers. We need to mask all the vectors to prevent
  642. * interrupts coming in before they're fully set up.
  643. */
  644. pci_msix_clear_and_set_ctrl(dev, 0,
  645. PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE);
  646. msix_program_entries(dev, entries);
  647. ret = populate_msi_sysfs(dev);
  648. if (ret)
  649. goto out_free;
  650. /* Set MSI-X enabled bits and unmask the function */
  651. pci_intx_for_msi(dev, 0);
  652. dev->msix_enabled = 1;
  653. pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
  654. pcibios_free_irq(dev);
  655. return 0;
  656. out_avail:
  657. if (ret < 0) {
  658. /*
  659. * If we had some success, report the number of irqs
  660. * we succeeded in setting up.
  661. */
  662. struct msi_desc *entry;
  663. int avail = 0;
  664. for_each_pci_msi_entry(entry, dev) {
  665. if (entry->irq != 0)
  666. avail++;
  667. }
  668. if (avail != 0)
  669. ret = avail;
  670. }
  671. out_free:
  672. free_msi_irqs(dev);
  673. return ret;
  674. }
  675. /**
  676. * pci_msi_supported - check whether MSI may be enabled on a device
  677. * @dev: pointer to the pci_dev data structure of MSI device function
  678. * @nvec: how many MSIs have been requested ?
  679. *
  680. * Look at global flags, the device itself, and its parent buses
  681. * to determine if MSI/-X are supported for the device. If MSI/-X is
  682. * supported return 1, else return 0.
  683. **/
  684. static int pci_msi_supported(struct pci_dev *dev, int nvec)
  685. {
  686. struct pci_bus *bus;
  687. /* MSI must be globally enabled and supported by the device */
  688. if (!pci_msi_enable)
  689. return 0;
  690. if (!dev || dev->no_msi || dev->current_state != PCI_D0)
  691. return 0;
  692. /*
  693. * You can't ask to have 0 or less MSIs configured.
  694. * a) it's stupid ..
  695. * b) the list manipulation code assumes nvec >= 1.
  696. */
  697. if (nvec < 1)
  698. return 0;
  699. /*
  700. * Any bridge which does NOT route MSI transactions from its
  701. * secondary bus to its primary bus must set NO_MSI flag on
  702. * the secondary pci_bus.
  703. * We expect only arch-specific PCI host bus controller driver
  704. * or quirks for specific PCI bridges to be setting NO_MSI.
  705. */
  706. for (bus = dev->bus; bus; bus = bus->parent)
  707. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  708. return 0;
  709. return 1;
  710. }
  711. /**
  712. * pci_msi_vec_count - Return the number of MSI vectors a device can send
  713. * @dev: device to report about
  714. *
  715. * This function returns the number of MSI vectors a device requested via
  716. * Multiple Message Capable register. It returns a negative errno if the
  717. * device is not capable sending MSI interrupts. Otherwise, the call succeeds
  718. * and returns a power of two, up to a maximum of 2^5 (32), according to the
  719. * MSI specification.
  720. **/
  721. int pci_msi_vec_count(struct pci_dev *dev)
  722. {
  723. int ret;
  724. u16 msgctl;
  725. if (!dev->msi_cap)
  726. return -EINVAL;
  727. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
  728. ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
  729. return ret;
  730. }
  731. EXPORT_SYMBOL(pci_msi_vec_count);
  732. static void pci_msi_shutdown(struct pci_dev *dev)
  733. {
  734. struct msi_desc *desc;
  735. u32 mask;
  736. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  737. return;
  738. BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
  739. desc = first_pci_msi_entry(dev);
  740. pci_msi_set_enable(dev, 0);
  741. pci_intx_for_msi(dev, 1);
  742. dev->msi_enabled = 0;
  743. /* Return the device with MSI unmasked as initial states */
  744. mask = msi_mask(desc->msi_attrib.multi_cap);
  745. /* Keep cached state to be restored */
  746. __pci_msi_desc_mask_irq(desc, mask, ~mask);
  747. /* Restore dev->irq to its default pin-assertion irq */
  748. dev->irq = desc->msi_attrib.default_irq;
  749. pcibios_alloc_irq(dev);
  750. }
  751. void pci_disable_msi(struct pci_dev *dev)
  752. {
  753. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  754. return;
  755. pci_msi_shutdown(dev);
  756. free_msi_irqs(dev);
  757. }
  758. EXPORT_SYMBOL(pci_disable_msi);
  759. /**
  760. * pci_msix_vec_count - return the number of device's MSI-X table entries
  761. * @dev: pointer to the pci_dev data structure of MSI-X device function
  762. * This function returns the number of device's MSI-X table entries and
  763. * therefore the number of MSI-X vectors device is capable of sending.
  764. * It returns a negative errno if the device is not capable of sending MSI-X
  765. * interrupts.
  766. **/
  767. int pci_msix_vec_count(struct pci_dev *dev)
  768. {
  769. u16 control;
  770. if (!dev->msix_cap)
  771. return -EINVAL;
  772. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  773. return msix_table_size(control);
  774. }
  775. EXPORT_SYMBOL(pci_msix_vec_count);
  776. static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
  777. int nvec, const struct irq_affinity *affd)
  778. {
  779. int nr_entries;
  780. int i, j;
  781. if (!pci_msi_supported(dev, nvec))
  782. return -EINVAL;
  783. nr_entries = pci_msix_vec_count(dev);
  784. if (nr_entries < 0)
  785. return nr_entries;
  786. if (nvec > nr_entries)
  787. return nr_entries;
  788. if (entries) {
  789. /* Check for any invalid entries */
  790. for (i = 0; i < nvec; i++) {
  791. if (entries[i].entry >= nr_entries)
  792. return -EINVAL; /* invalid entry */
  793. for (j = i + 1; j < nvec; j++) {
  794. if (entries[i].entry == entries[j].entry)
  795. return -EINVAL; /* duplicate entry */
  796. }
  797. }
  798. }
  799. WARN_ON(!!dev->msix_enabled);
  800. /* Check whether driver already requested for MSI irq */
  801. if (dev->msi_enabled) {
  802. dev_info(&dev->dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
  803. return -EINVAL;
  804. }
  805. return msix_capability_init(dev, entries, nvec, affd);
  806. }
  807. static void pci_msix_shutdown(struct pci_dev *dev)
  808. {
  809. struct msi_desc *entry;
  810. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  811. return;
  812. if (pci_dev_is_disconnected(dev)) {
  813. dev->msix_enabled = 0;
  814. return;
  815. }
  816. /* Return the device with MSI-X masked as initial states */
  817. for_each_pci_msi_entry(entry, dev) {
  818. /* Keep cached states to be restored */
  819. __pci_msix_desc_mask_irq(entry, 1);
  820. }
  821. pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
  822. pci_intx_for_msi(dev, 1);
  823. dev->msix_enabled = 0;
  824. pcibios_alloc_irq(dev);
  825. }
  826. void pci_disable_msix(struct pci_dev *dev)
  827. {
  828. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  829. return;
  830. pci_msix_shutdown(dev);
  831. free_msi_irqs(dev);
  832. }
  833. EXPORT_SYMBOL(pci_disable_msix);
  834. void pci_no_msi(void)
  835. {
  836. pci_msi_enable = 0;
  837. }
  838. /**
  839. * pci_msi_enabled - is MSI enabled?
  840. *
  841. * Returns true if MSI has not been disabled by the command-line option
  842. * pci=nomsi.
  843. **/
  844. int pci_msi_enabled(void)
  845. {
  846. return pci_msi_enable;
  847. }
  848. EXPORT_SYMBOL(pci_msi_enabled);
  849. static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
  850. const struct irq_affinity *affd)
  851. {
  852. int nvec;
  853. int rc;
  854. if (!pci_msi_supported(dev, minvec))
  855. return -EINVAL;
  856. WARN_ON(!!dev->msi_enabled);
  857. /* Check whether driver already requested MSI-X irqs */
  858. if (dev->msix_enabled) {
  859. dev_info(&dev->dev,
  860. "can't enable MSI (MSI-X already enabled)\n");
  861. return -EINVAL;
  862. }
  863. if (maxvec < minvec)
  864. return -ERANGE;
  865. nvec = pci_msi_vec_count(dev);
  866. if (nvec < 0)
  867. return nvec;
  868. if (nvec < minvec)
  869. return -ENOSPC;
  870. if (nvec > maxvec)
  871. nvec = maxvec;
  872. for (;;) {
  873. if (affd) {
  874. nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
  875. if (nvec < minvec)
  876. return -ENOSPC;
  877. }
  878. rc = msi_capability_init(dev, nvec, affd);
  879. if (rc == 0)
  880. return nvec;
  881. if (rc < 0)
  882. return rc;
  883. if (rc < minvec)
  884. return -ENOSPC;
  885. nvec = rc;
  886. }
  887. }
  888. /* deprecated, don't use */
  889. int pci_enable_msi(struct pci_dev *dev)
  890. {
  891. int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
  892. if (rc < 0)
  893. return rc;
  894. return 0;
  895. }
  896. EXPORT_SYMBOL(pci_enable_msi);
  897. static int __pci_enable_msix_range(struct pci_dev *dev,
  898. struct msix_entry *entries, int minvec,
  899. int maxvec, const struct irq_affinity *affd)
  900. {
  901. int rc, nvec = maxvec;
  902. if (maxvec < minvec)
  903. return -ERANGE;
  904. for (;;) {
  905. if (affd) {
  906. nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
  907. if (nvec < minvec)
  908. return -ENOSPC;
  909. }
  910. rc = __pci_enable_msix(dev, entries, nvec, affd);
  911. if (rc == 0)
  912. return nvec;
  913. if (rc < 0)
  914. return rc;
  915. if (rc < minvec)
  916. return -ENOSPC;
  917. nvec = rc;
  918. }
  919. }
  920. /**
  921. * pci_enable_msix_range - configure device's MSI-X capability structure
  922. * @dev: pointer to the pci_dev data structure of MSI-X device function
  923. * @entries: pointer to an array of MSI-X entries
  924. * @minvec: minimum number of MSI-X irqs requested
  925. * @maxvec: maximum number of MSI-X irqs requested
  926. *
  927. * Setup the MSI-X capability structure of device function with a maximum
  928. * possible number of interrupts in the range between @minvec and @maxvec
  929. * upon its software driver call to request for MSI-X mode enabled on its
  930. * hardware device function. It returns a negative errno if an error occurs.
  931. * If it succeeds, it returns the actual number of interrupts allocated and
  932. * indicates the successful configuration of MSI-X capability structure
  933. * with new allocated MSI-X interrupts.
  934. **/
  935. int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
  936. int minvec, int maxvec)
  937. {
  938. return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL);
  939. }
  940. EXPORT_SYMBOL(pci_enable_msix_range);
  941. /**
  942. * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
  943. * @dev: PCI device to operate on
  944. * @min_vecs: minimum number of vectors required (must be >= 1)
  945. * @max_vecs: maximum (desired) number of vectors
  946. * @flags: flags or quirks for the allocation
  947. * @affd: optional description of the affinity requirements
  948. *
  949. * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
  950. * vectors if available, and fall back to a single legacy vector
  951. * if neither is available. Return the number of vectors allocated,
  952. * (which might be smaller than @max_vecs) if successful, or a negative
  953. * error code on error. If less than @min_vecs interrupt vectors are
  954. * available for @dev the function will fail with -ENOSPC.
  955. *
  956. * To get the Linux IRQ number used for a vector that can be passed to
  957. * request_irq() use the pci_irq_vector() helper.
  958. */
  959. int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
  960. unsigned int max_vecs, unsigned int flags,
  961. const struct irq_affinity *affd)
  962. {
  963. static const struct irq_affinity msi_default_affd;
  964. int vecs = -ENOSPC;
  965. if (flags & PCI_IRQ_AFFINITY) {
  966. if (!affd)
  967. affd = &msi_default_affd;
  968. } else {
  969. if (WARN_ON(affd))
  970. affd = NULL;
  971. }
  972. if (flags & PCI_IRQ_MSIX) {
  973. vecs = __pci_enable_msix_range(dev, NULL, min_vecs, max_vecs,
  974. affd);
  975. if (vecs > 0)
  976. return vecs;
  977. }
  978. if (flags & PCI_IRQ_MSI) {
  979. vecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, affd);
  980. if (vecs > 0)
  981. return vecs;
  982. }
  983. /* use legacy irq if allowed */
  984. if (flags & PCI_IRQ_LEGACY) {
  985. if (min_vecs == 1 && dev->irq) {
  986. pci_intx(dev, 1);
  987. return 1;
  988. }
  989. }
  990. return vecs;
  991. }
  992. EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
  993. /**
  994. * pci_free_irq_vectors - free previously allocated IRQs for a device
  995. * @dev: PCI device to operate on
  996. *
  997. * Undoes the allocations and enabling in pci_alloc_irq_vectors().
  998. */
  999. void pci_free_irq_vectors(struct pci_dev *dev)
  1000. {
  1001. pci_disable_msix(dev);
  1002. pci_disable_msi(dev);
  1003. }
  1004. EXPORT_SYMBOL(pci_free_irq_vectors);
  1005. /**
  1006. * pci_irq_vector - return Linux IRQ number of a device vector
  1007. * @dev: PCI device to operate on
  1008. * @nr: device-relative interrupt vector index (0-based).
  1009. */
  1010. int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
  1011. {
  1012. if (dev->msix_enabled) {
  1013. struct msi_desc *entry;
  1014. int i = 0;
  1015. for_each_pci_msi_entry(entry, dev) {
  1016. if (i == nr)
  1017. return entry->irq;
  1018. i++;
  1019. }
  1020. WARN_ON_ONCE(1);
  1021. return -EINVAL;
  1022. }
  1023. if (dev->msi_enabled) {
  1024. struct msi_desc *entry = first_pci_msi_entry(dev);
  1025. if (WARN_ON_ONCE(nr >= entry->nvec_used))
  1026. return -EINVAL;
  1027. } else {
  1028. if (WARN_ON_ONCE(nr > 0))
  1029. return -EINVAL;
  1030. }
  1031. return dev->irq + nr;
  1032. }
  1033. EXPORT_SYMBOL(pci_irq_vector);
  1034. /**
  1035. * pci_irq_get_affinity - return the affinity of a particular msi vector
  1036. * @dev: PCI device to operate on
  1037. * @nr: device-relative interrupt vector index (0-based).
  1038. */
  1039. const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
  1040. {
  1041. if (dev->msix_enabled) {
  1042. struct msi_desc *entry;
  1043. int i = 0;
  1044. for_each_pci_msi_entry(entry, dev) {
  1045. if (i == nr)
  1046. return entry->affinity;
  1047. i++;
  1048. }
  1049. WARN_ON_ONCE(1);
  1050. return NULL;
  1051. } else if (dev->msi_enabled) {
  1052. struct msi_desc *entry = first_pci_msi_entry(dev);
  1053. if (WARN_ON_ONCE(!entry || !entry->affinity ||
  1054. nr >= entry->nvec_used))
  1055. return NULL;
  1056. return &entry->affinity[nr];
  1057. } else {
  1058. return cpu_possible_mask;
  1059. }
  1060. }
  1061. EXPORT_SYMBOL(pci_irq_get_affinity);
  1062. /**
  1063. * pci_irq_get_node - return the numa node of a particular msi vector
  1064. * @pdev: PCI device to operate on
  1065. * @vec: device-relative interrupt vector index (0-based).
  1066. */
  1067. int pci_irq_get_node(struct pci_dev *pdev, int vec)
  1068. {
  1069. const struct cpumask *mask;
  1070. mask = pci_irq_get_affinity(pdev, vec);
  1071. if (mask)
  1072. return local_memory_node(cpu_to_node(cpumask_first(mask)));
  1073. return dev_to_node(&pdev->dev);
  1074. }
  1075. EXPORT_SYMBOL(pci_irq_get_node);
  1076. struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
  1077. {
  1078. return to_pci_dev(desc->dev);
  1079. }
  1080. EXPORT_SYMBOL(msi_desc_to_pci_dev);
  1081. void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
  1082. {
  1083. struct pci_dev *dev = msi_desc_to_pci_dev(desc);
  1084. return dev->bus->sysdata;
  1085. }
  1086. EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata);
  1087. #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
  1088. /**
  1089. * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
  1090. * @irq_data: Pointer to interrupt data of the MSI interrupt
  1091. * @msg: Pointer to the message
  1092. */
  1093. void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
  1094. {
  1095. struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
  1096. /*
  1097. * For MSI-X desc->irq is always equal to irq_data->irq. For
  1098. * MSI only the first interrupt of MULTI MSI passes the test.
  1099. */
  1100. if (desc->irq == irq_data->irq)
  1101. __pci_write_msi_msg(desc, msg);
  1102. }
  1103. /**
  1104. * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
  1105. * @dev: Pointer to the PCI device
  1106. * @desc: Pointer to the msi descriptor
  1107. *
  1108. * The ID number is only used within the irqdomain.
  1109. */
  1110. irq_hw_number_t pci_msi_domain_calc_hwirq(struct pci_dev *dev,
  1111. struct msi_desc *desc)
  1112. {
  1113. return (irq_hw_number_t)desc->msi_attrib.entry_nr |
  1114. PCI_DEVID(dev->bus->number, dev->devfn) << 11 |
  1115. (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
  1116. }
  1117. static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
  1118. {
  1119. return !desc->msi_attrib.is_msix && desc->nvec_used > 1;
  1120. }
  1121. /**
  1122. * pci_msi_domain_check_cap - Verify that @domain supports the capabilities for @dev
  1123. * @domain: The interrupt domain to check
  1124. * @info: The domain info for verification
  1125. * @dev: The device to check
  1126. *
  1127. * Returns:
  1128. * 0 if the functionality is supported
  1129. * 1 if Multi MSI is requested, but the domain does not support it
  1130. * -ENOTSUPP otherwise
  1131. */
  1132. int pci_msi_domain_check_cap(struct irq_domain *domain,
  1133. struct msi_domain_info *info, struct device *dev)
  1134. {
  1135. struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
  1136. /* Special handling to support __pci_enable_msi_range() */
  1137. if (pci_msi_desc_is_multi_msi(desc) &&
  1138. !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
  1139. return 1;
  1140. else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
  1141. return -ENOTSUPP;
  1142. return 0;
  1143. }
  1144. static int pci_msi_domain_handle_error(struct irq_domain *domain,
  1145. struct msi_desc *desc, int error)
  1146. {
  1147. /* Special handling to support __pci_enable_msi_range() */
  1148. if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
  1149. return 1;
  1150. return error;
  1151. }
  1152. #ifdef GENERIC_MSI_DOMAIN_OPS
  1153. static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
  1154. struct msi_desc *desc)
  1155. {
  1156. arg->desc = desc;
  1157. arg->hwirq = pci_msi_domain_calc_hwirq(msi_desc_to_pci_dev(desc),
  1158. desc);
  1159. }
  1160. #else
  1161. #define pci_msi_domain_set_desc NULL
  1162. #endif
  1163. static struct msi_domain_ops pci_msi_domain_ops_default = {
  1164. .set_desc = pci_msi_domain_set_desc,
  1165. .msi_check = pci_msi_domain_check_cap,
  1166. .handle_error = pci_msi_domain_handle_error,
  1167. };
  1168. static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
  1169. {
  1170. struct msi_domain_ops *ops = info->ops;
  1171. if (ops == NULL) {
  1172. info->ops = &pci_msi_domain_ops_default;
  1173. } else {
  1174. if (ops->set_desc == NULL)
  1175. ops->set_desc = pci_msi_domain_set_desc;
  1176. if (ops->msi_check == NULL)
  1177. ops->msi_check = pci_msi_domain_check_cap;
  1178. if (ops->handle_error == NULL)
  1179. ops->handle_error = pci_msi_domain_handle_error;
  1180. }
  1181. }
  1182. static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
  1183. {
  1184. struct irq_chip *chip = info->chip;
  1185. BUG_ON(!chip);
  1186. if (!chip->irq_write_msi_msg)
  1187. chip->irq_write_msi_msg = pci_msi_domain_write_msg;
  1188. if (!chip->irq_mask)
  1189. chip->irq_mask = pci_msi_mask_irq;
  1190. if (!chip->irq_unmask)
  1191. chip->irq_unmask = pci_msi_unmask_irq;
  1192. }
  1193. /**
  1194. * pci_msi_create_irq_domain - Create a MSI interrupt domain
  1195. * @fwnode: Optional fwnode of the interrupt controller
  1196. * @info: MSI domain info
  1197. * @parent: Parent irq domain
  1198. *
  1199. * Updates the domain and chip ops and creates a MSI interrupt domain.
  1200. *
  1201. * Returns:
  1202. * A domain pointer or NULL in case of failure.
  1203. */
  1204. struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
  1205. struct msi_domain_info *info,
  1206. struct irq_domain *parent)
  1207. {
  1208. struct irq_domain *domain;
  1209. if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
  1210. pci_msi_domain_update_dom_ops(info);
  1211. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  1212. pci_msi_domain_update_chip_ops(info);
  1213. info->flags |= MSI_FLAG_ACTIVATE_EARLY;
  1214. domain = msi_create_irq_domain(fwnode, info, parent);
  1215. if (!domain)
  1216. return NULL;
  1217. irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
  1218. return domain;
  1219. }
  1220. EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
  1221. /*
  1222. * Users of the generic MSI infrastructure expect a device to have a single ID,
  1223. * so with DMA aliases we have to pick the least-worst compromise. Devices with
  1224. * DMA phantom functions tend to still emit MSIs from the real function number,
  1225. * so we ignore those and only consider topological aliases where either the
  1226. * alias device or RID appears on a different bus number. We also make the
  1227. * reasonable assumption that bridges are walked in an upstream direction (so
  1228. * the last one seen wins), and the much braver assumption that the most likely
  1229. * case is that of PCI->PCIe so we should always use the alias RID. This echoes
  1230. * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
  1231. * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
  1232. * for taking ownership all we can really do is close our eyes and hope...
  1233. */
  1234. static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
  1235. {
  1236. u32 *pa = data;
  1237. u8 bus = PCI_BUS_NUM(*pa);
  1238. if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
  1239. *pa = alias;
  1240. return 0;
  1241. }
  1242. /**
  1243. * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
  1244. * @domain: The interrupt domain
  1245. * @pdev: The PCI device.
  1246. *
  1247. * The RID for a device is formed from the alias, with a firmware
  1248. * supplied mapping applied
  1249. *
  1250. * Returns: The RID.
  1251. */
  1252. u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
  1253. {
  1254. struct device_node *of_node;
  1255. u32 rid = PCI_DEVID(pdev->bus->number, pdev->devfn);
  1256. pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
  1257. of_node = irq_domain_get_of_node(domain);
  1258. rid = of_node ? of_msi_map_rid(&pdev->dev, of_node, rid) :
  1259. iort_msi_map_rid(&pdev->dev, rid);
  1260. return rid;
  1261. }
  1262. /**
  1263. * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
  1264. * @pdev: The PCI device
  1265. *
  1266. * Use the firmware data to find a device-specific MSI domain
  1267. * (i.e. not one that is set as a default).
  1268. *
  1269. * Returns: The corresponding MSI domain or NULL if none has been found.
  1270. */
  1271. struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
  1272. {
  1273. struct irq_domain *dom;
  1274. u32 rid = PCI_DEVID(pdev->bus->number, pdev->devfn);
  1275. pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
  1276. dom = of_msi_map_get_device_domain(&pdev->dev, rid);
  1277. if (!dom)
  1278. dom = iort_get_device_domain(&pdev->dev, rid);
  1279. return dom;
  1280. }
  1281. #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */