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. if (!masks)
  452. dev_err(&dev->dev, "can't allocate MSI affinity masks for %d vectors\n",
  453. nvec);
  454. }
  455. /* MSI Entry Initialization */
  456. entry = alloc_msi_entry(&dev->dev, nvec, masks);
  457. if (!entry)
  458. goto out;
  459. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  460. entry->msi_attrib.is_msix = 0;
  461. entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT);
  462. entry->msi_attrib.entry_nr = 0;
  463. entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT);
  464. entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
  465. entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;
  466. entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));
  467. if (control & PCI_MSI_FLAGS_64BIT)
  468. entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
  469. else
  470. entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
  471. /* Save the initial mask status */
  472. if (entry->msi_attrib.maskbit)
  473. pci_read_config_dword(dev, entry->mask_pos, &entry->masked);
  474. out:
  475. kfree(masks);
  476. return entry;
  477. }
  478. static int msi_verify_entries(struct pci_dev *dev)
  479. {
  480. struct msi_desc *entry;
  481. for_each_pci_msi_entry(entry, dev) {
  482. if (!dev->no_64bit_msi || !entry->msg.address_hi)
  483. continue;
  484. dev_err(&dev->dev, "Device has broken 64-bit MSI but arch"
  485. " tried to assign one above 4G\n");
  486. return -EIO;
  487. }
  488. return 0;
  489. }
  490. /**
  491. * msi_capability_init - configure device's MSI capability structure
  492. * @dev: pointer to the pci_dev data structure of MSI device function
  493. * @nvec: number of interrupts to allocate
  494. * @affd: description of automatic irq affinity assignments (may be %NULL)
  495. *
  496. * Setup the MSI capability structure of the device with the requested
  497. * number of interrupts. A return value of zero indicates the successful
  498. * setup of an entry with the new MSI irq. A negative return value indicates
  499. * an error, and a positive return value indicates the number of interrupts
  500. * which could have been allocated.
  501. */
  502. static int msi_capability_init(struct pci_dev *dev, int nvec,
  503. const struct irq_affinity *affd)
  504. {
  505. struct msi_desc *entry;
  506. int ret;
  507. unsigned mask;
  508. pci_msi_set_enable(dev, 0); /* Disable MSI during set up */
  509. entry = msi_setup_entry(dev, nvec, affd);
  510. if (!entry)
  511. return -ENOMEM;
  512. /* All MSIs are unmasked by default, Mask them all */
  513. mask = msi_mask(entry->msi_attrib.multi_cap);
  514. msi_mask_irq(entry, mask, mask);
  515. list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
  516. /* Configure MSI capability structure */
  517. ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
  518. if (ret) {
  519. msi_mask_irq(entry, mask, ~mask);
  520. free_msi_irqs(dev);
  521. return ret;
  522. }
  523. ret = msi_verify_entries(dev);
  524. if (ret) {
  525. msi_mask_irq(entry, mask, ~mask);
  526. free_msi_irqs(dev);
  527. return ret;
  528. }
  529. ret = populate_msi_sysfs(dev);
  530. if (ret) {
  531. msi_mask_irq(entry, mask, ~mask);
  532. free_msi_irqs(dev);
  533. return ret;
  534. }
  535. /* Set MSI enabled bits */
  536. pci_intx_for_msi(dev, 0);
  537. pci_msi_set_enable(dev, 1);
  538. dev->msi_enabled = 1;
  539. pcibios_free_irq(dev);
  540. dev->irq = entry->irq;
  541. return 0;
  542. }
  543. static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries)
  544. {
  545. resource_size_t phys_addr;
  546. u32 table_offset;
  547. unsigned long flags;
  548. u8 bir;
  549. pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
  550. &table_offset);
  551. bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
  552. flags = pci_resource_flags(dev, bir);
  553. if (!flags || (flags & IORESOURCE_UNSET))
  554. return NULL;
  555. table_offset &= PCI_MSIX_TABLE_OFFSET;
  556. phys_addr = pci_resource_start(dev, bir) + table_offset;
  557. return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  558. }
  559. static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
  560. struct msix_entry *entries, int nvec,
  561. const struct irq_affinity *affd)
  562. {
  563. struct cpumask *curmsk, *masks = NULL;
  564. struct msi_desc *entry;
  565. int ret, i;
  566. if (affd) {
  567. masks = irq_create_affinity_masks(nvec, affd);
  568. if (!masks)
  569. dev_err(&dev->dev, "can't allocate MSI-X affinity masks for %d vectors\n",
  570. nvec);
  571. }
  572. for (i = 0, curmsk = masks; i < nvec; i++) {
  573. entry = alloc_msi_entry(&dev->dev, 1, curmsk);
  574. if (!entry) {
  575. if (!i)
  576. iounmap(base);
  577. else
  578. free_msi_irqs(dev);
  579. /* No enough memory. Don't try again */
  580. ret = -ENOMEM;
  581. goto out;
  582. }
  583. entry->msi_attrib.is_msix = 1;
  584. entry->msi_attrib.is_64 = 1;
  585. if (entries)
  586. entry->msi_attrib.entry_nr = entries[i].entry;
  587. else
  588. entry->msi_attrib.entry_nr = i;
  589. entry->msi_attrib.default_irq = dev->irq;
  590. entry->mask_base = base;
  591. list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
  592. if (masks)
  593. curmsk++;
  594. }
  595. ret = 0;
  596. out:
  597. kfree(masks);
  598. return ret;
  599. }
  600. static void msix_program_entries(struct pci_dev *dev,
  601. struct msix_entry *entries)
  602. {
  603. struct msi_desc *entry;
  604. int i = 0;
  605. for_each_pci_msi_entry(entry, dev) {
  606. if (entries)
  607. entries[i++].vector = entry->irq;
  608. entry->masked = readl(pci_msix_desc_addr(entry) +
  609. PCI_MSIX_ENTRY_VECTOR_CTRL);
  610. msix_mask_irq(entry, 1);
  611. }
  612. }
  613. /**
  614. * msix_capability_init - configure device's MSI-X capability
  615. * @dev: pointer to the pci_dev data structure of MSI-X device function
  616. * @entries: pointer to an array of struct msix_entry entries
  617. * @nvec: number of @entries
  618. * @affd: Optional pointer to enable automatic affinity assignement
  619. *
  620. * Setup the MSI-X capability structure of device function with a
  621. * single MSI-X irq. A return of zero indicates the successful setup of
  622. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  623. **/
  624. static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
  625. int nvec, const struct irq_affinity *affd)
  626. {
  627. int ret;
  628. u16 control;
  629. void __iomem *base;
  630. /* Ensure MSI-X is disabled while it is set up */
  631. pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
  632. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  633. /* Request & Map MSI-X table region */
  634. base = msix_map_region(dev, msix_table_size(control));
  635. if (!base)
  636. return -ENOMEM;
  637. ret = msix_setup_entries(dev, base, entries, nvec, affd);
  638. if (ret)
  639. return ret;
  640. ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
  641. if (ret)
  642. goto out_avail;
  643. /* Check if all MSI entries honor device restrictions */
  644. ret = msi_verify_entries(dev);
  645. if (ret)
  646. goto out_free;
  647. /*
  648. * Some devices require MSI-X to be enabled before we can touch the
  649. * MSI-X registers. We need to mask all the vectors to prevent
  650. * interrupts coming in before they're fully set up.
  651. */
  652. pci_msix_clear_and_set_ctrl(dev, 0,
  653. PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE);
  654. msix_program_entries(dev, entries);
  655. ret = populate_msi_sysfs(dev);
  656. if (ret)
  657. goto out_free;
  658. /* Set MSI-X enabled bits and unmask the function */
  659. pci_intx_for_msi(dev, 0);
  660. dev->msix_enabled = 1;
  661. pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
  662. pcibios_free_irq(dev);
  663. return 0;
  664. out_avail:
  665. if (ret < 0) {
  666. /*
  667. * If we had some success, report the number of irqs
  668. * we succeeded in setting up.
  669. */
  670. struct msi_desc *entry;
  671. int avail = 0;
  672. for_each_pci_msi_entry(entry, dev) {
  673. if (entry->irq != 0)
  674. avail++;
  675. }
  676. if (avail != 0)
  677. ret = avail;
  678. }
  679. out_free:
  680. free_msi_irqs(dev);
  681. return ret;
  682. }
  683. /**
  684. * pci_msi_supported - check whether MSI may be enabled on a device
  685. * @dev: pointer to the pci_dev data structure of MSI device function
  686. * @nvec: how many MSIs have been requested ?
  687. *
  688. * Look at global flags, the device itself, and its parent buses
  689. * to determine if MSI/-X are supported for the device. If MSI/-X is
  690. * supported return 1, else return 0.
  691. **/
  692. static int pci_msi_supported(struct pci_dev *dev, int nvec)
  693. {
  694. struct pci_bus *bus;
  695. /* MSI must be globally enabled and supported by the device */
  696. if (!pci_msi_enable)
  697. return 0;
  698. if (!dev || dev->no_msi || dev->current_state != PCI_D0)
  699. return 0;
  700. /*
  701. * You can't ask to have 0 or less MSIs configured.
  702. * a) it's stupid ..
  703. * b) the list manipulation code assumes nvec >= 1.
  704. */
  705. if (nvec < 1)
  706. return 0;
  707. /*
  708. * Any bridge which does NOT route MSI transactions from its
  709. * secondary bus to its primary bus must set NO_MSI flag on
  710. * the secondary pci_bus.
  711. * We expect only arch-specific PCI host bus controller driver
  712. * or quirks for specific PCI bridges to be setting NO_MSI.
  713. */
  714. for (bus = dev->bus; bus; bus = bus->parent)
  715. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  716. return 0;
  717. return 1;
  718. }
  719. /**
  720. * pci_msi_vec_count - Return the number of MSI vectors a device can send
  721. * @dev: device to report about
  722. *
  723. * This function returns the number of MSI vectors a device requested via
  724. * Multiple Message Capable register. It returns a negative errno if the
  725. * device is not capable sending MSI interrupts. Otherwise, the call succeeds
  726. * and returns a power of two, up to a maximum of 2^5 (32), according to the
  727. * MSI specification.
  728. **/
  729. int pci_msi_vec_count(struct pci_dev *dev)
  730. {
  731. int ret;
  732. u16 msgctl;
  733. if (!dev->msi_cap)
  734. return -EINVAL;
  735. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
  736. ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
  737. return ret;
  738. }
  739. EXPORT_SYMBOL(pci_msi_vec_count);
  740. static void pci_msi_shutdown(struct pci_dev *dev)
  741. {
  742. struct msi_desc *desc;
  743. u32 mask;
  744. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  745. return;
  746. BUG_ON(list_empty(dev_to_msi_list(&dev->dev)));
  747. desc = first_pci_msi_entry(dev);
  748. pci_msi_set_enable(dev, 0);
  749. pci_intx_for_msi(dev, 1);
  750. dev->msi_enabled = 0;
  751. /* Return the device with MSI unmasked as initial states */
  752. mask = msi_mask(desc->msi_attrib.multi_cap);
  753. /* Keep cached state to be restored */
  754. __pci_msi_desc_mask_irq(desc, mask, ~mask);
  755. /* Restore dev->irq to its default pin-assertion irq */
  756. dev->irq = desc->msi_attrib.default_irq;
  757. pcibios_alloc_irq(dev);
  758. }
  759. void pci_disable_msi(struct pci_dev *dev)
  760. {
  761. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  762. return;
  763. pci_msi_shutdown(dev);
  764. free_msi_irqs(dev);
  765. }
  766. EXPORT_SYMBOL(pci_disable_msi);
  767. /**
  768. * pci_msix_vec_count - return the number of device's MSI-X table entries
  769. * @dev: pointer to the pci_dev data structure of MSI-X device function
  770. * This function returns the number of device's MSI-X table entries and
  771. * therefore the number of MSI-X vectors device is capable of sending.
  772. * It returns a negative errno if the device is not capable of sending MSI-X
  773. * interrupts.
  774. **/
  775. int pci_msix_vec_count(struct pci_dev *dev)
  776. {
  777. u16 control;
  778. if (!dev->msix_cap)
  779. return -EINVAL;
  780. pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
  781. return msix_table_size(control);
  782. }
  783. EXPORT_SYMBOL(pci_msix_vec_count);
  784. static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,
  785. int nvec, const struct irq_affinity *affd)
  786. {
  787. int nr_entries;
  788. int i, j;
  789. if (!pci_msi_supported(dev, nvec))
  790. return -EINVAL;
  791. nr_entries = pci_msix_vec_count(dev);
  792. if (nr_entries < 0)
  793. return nr_entries;
  794. if (nvec > nr_entries)
  795. return nr_entries;
  796. if (entries) {
  797. /* Check for any invalid entries */
  798. for (i = 0; i < nvec; i++) {
  799. if (entries[i].entry >= nr_entries)
  800. return -EINVAL; /* invalid entry */
  801. for (j = i + 1; j < nvec; j++) {
  802. if (entries[i].entry == entries[j].entry)
  803. return -EINVAL; /* duplicate entry */
  804. }
  805. }
  806. }
  807. WARN_ON(!!dev->msix_enabled);
  808. /* Check whether driver already requested for MSI irq */
  809. if (dev->msi_enabled) {
  810. dev_info(&dev->dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
  811. return -EINVAL;
  812. }
  813. return msix_capability_init(dev, entries, nvec, affd);
  814. }
  815. static void pci_msix_shutdown(struct pci_dev *dev)
  816. {
  817. struct msi_desc *entry;
  818. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  819. return;
  820. if (pci_dev_is_disconnected(dev)) {
  821. dev->msix_enabled = 0;
  822. return;
  823. }
  824. /* Return the device with MSI-X masked as initial states */
  825. for_each_pci_msi_entry(entry, dev) {
  826. /* Keep cached states to be restored */
  827. __pci_msix_desc_mask_irq(entry, 1);
  828. }
  829. pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
  830. pci_intx_for_msi(dev, 1);
  831. dev->msix_enabled = 0;
  832. pcibios_alloc_irq(dev);
  833. }
  834. void pci_disable_msix(struct pci_dev *dev)
  835. {
  836. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  837. return;
  838. pci_msix_shutdown(dev);
  839. free_msi_irqs(dev);
  840. }
  841. EXPORT_SYMBOL(pci_disable_msix);
  842. void pci_no_msi(void)
  843. {
  844. pci_msi_enable = 0;
  845. }
  846. /**
  847. * pci_msi_enabled - is MSI enabled?
  848. *
  849. * Returns true if MSI has not been disabled by the command-line option
  850. * pci=nomsi.
  851. **/
  852. int pci_msi_enabled(void)
  853. {
  854. return pci_msi_enable;
  855. }
  856. EXPORT_SYMBOL(pci_msi_enabled);
  857. static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
  858. const struct irq_affinity *affd)
  859. {
  860. int nvec;
  861. int rc;
  862. if (!pci_msi_supported(dev, minvec))
  863. return -EINVAL;
  864. WARN_ON(!!dev->msi_enabled);
  865. /* Check whether driver already requested MSI-X irqs */
  866. if (dev->msix_enabled) {
  867. dev_info(&dev->dev,
  868. "can't enable MSI (MSI-X already enabled)\n");
  869. return -EINVAL;
  870. }
  871. if (maxvec < minvec)
  872. return -ERANGE;
  873. nvec = pci_msi_vec_count(dev);
  874. if (nvec < 0)
  875. return nvec;
  876. if (nvec < minvec)
  877. return -ENOSPC;
  878. if (nvec > maxvec)
  879. nvec = maxvec;
  880. for (;;) {
  881. if (affd) {
  882. nvec = irq_calc_affinity_vectors(nvec, affd);
  883. if (nvec < minvec)
  884. return -ENOSPC;
  885. }
  886. rc = msi_capability_init(dev, nvec, affd);
  887. if (rc == 0)
  888. return nvec;
  889. if (rc < 0)
  890. return rc;
  891. if (rc < minvec)
  892. return -ENOSPC;
  893. nvec = rc;
  894. }
  895. }
  896. /* deprecated, don't use */
  897. int pci_enable_msi(struct pci_dev *dev)
  898. {
  899. int rc = __pci_enable_msi_range(dev, 1, 1, NULL);
  900. if (rc < 0)
  901. return rc;
  902. return 0;
  903. }
  904. EXPORT_SYMBOL(pci_enable_msi);
  905. static int __pci_enable_msix_range(struct pci_dev *dev,
  906. struct msix_entry *entries, int minvec,
  907. int maxvec, const struct irq_affinity *affd)
  908. {
  909. int rc, nvec = maxvec;
  910. if (maxvec < minvec)
  911. return -ERANGE;
  912. for (;;) {
  913. if (affd) {
  914. nvec = irq_calc_affinity_vectors(nvec, affd);
  915. if (nvec < minvec)
  916. return -ENOSPC;
  917. }
  918. rc = __pci_enable_msix(dev, entries, nvec, affd);
  919. if (rc == 0)
  920. return nvec;
  921. if (rc < 0)
  922. return rc;
  923. if (rc < minvec)
  924. return -ENOSPC;
  925. nvec = rc;
  926. }
  927. }
  928. /**
  929. * pci_enable_msix_range - configure device's MSI-X capability structure
  930. * @dev: pointer to the pci_dev data structure of MSI-X device function
  931. * @entries: pointer to an array of MSI-X entries
  932. * @minvec: minimum number of MSI-X irqs requested
  933. * @maxvec: maximum number of MSI-X irqs requested
  934. *
  935. * Setup the MSI-X capability structure of device function with a maximum
  936. * possible number of interrupts in the range between @minvec and @maxvec
  937. * upon its software driver call to request for MSI-X mode enabled on its
  938. * hardware device function. It returns a negative errno if an error occurs.
  939. * If it succeeds, it returns the actual number of interrupts allocated and
  940. * indicates the successful configuration of MSI-X capability structure
  941. * with new allocated MSI-X interrupts.
  942. **/
  943. int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
  944. int minvec, int maxvec)
  945. {
  946. return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL);
  947. }
  948. EXPORT_SYMBOL(pci_enable_msix_range);
  949. /**
  950. * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
  951. * @dev: PCI device to operate on
  952. * @min_vecs: minimum number of vectors required (must be >= 1)
  953. * @max_vecs: maximum (desired) number of vectors
  954. * @flags: flags or quirks for the allocation
  955. * @affd: optional description of the affinity requirements
  956. *
  957. * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
  958. * vectors if available, and fall back to a single legacy vector
  959. * if neither is available. Return the number of vectors allocated,
  960. * (which might be smaller than @max_vecs) if successful, or a negative
  961. * error code on error. If less than @min_vecs interrupt vectors are
  962. * available for @dev the function will fail with -ENOSPC.
  963. *
  964. * To get the Linux IRQ number used for a vector that can be passed to
  965. * request_irq() use the pci_irq_vector() helper.
  966. */
  967. int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
  968. unsigned int max_vecs, unsigned int flags,
  969. const struct irq_affinity *affd)
  970. {
  971. static const struct irq_affinity msi_default_affd;
  972. int vecs = -ENOSPC;
  973. if (flags & PCI_IRQ_AFFINITY) {
  974. if (!affd)
  975. affd = &msi_default_affd;
  976. if (affd->pre_vectors + affd->post_vectors > min_vecs)
  977. return -EINVAL;
  978. /*
  979. * If there aren't any vectors left after applying the pre/post
  980. * vectors don't bother with assigning affinity.
  981. */
  982. if (affd->pre_vectors + affd->post_vectors == min_vecs)
  983. affd = NULL;
  984. } else {
  985. if (WARN_ON(affd))
  986. affd = NULL;
  987. }
  988. if (flags & PCI_IRQ_MSIX) {
  989. vecs = __pci_enable_msix_range(dev, NULL, min_vecs, max_vecs,
  990. affd);
  991. if (vecs > 0)
  992. return vecs;
  993. }
  994. if (flags & PCI_IRQ_MSI) {
  995. vecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, affd);
  996. if (vecs > 0)
  997. return vecs;
  998. }
  999. /* use legacy irq if allowed */
  1000. if (flags & PCI_IRQ_LEGACY) {
  1001. if (min_vecs == 1 && dev->irq) {
  1002. pci_intx(dev, 1);
  1003. return 1;
  1004. }
  1005. }
  1006. return vecs;
  1007. }
  1008. EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
  1009. /**
  1010. * pci_free_irq_vectors - free previously allocated IRQs for a device
  1011. * @dev: PCI device to operate on
  1012. *
  1013. * Undoes the allocations and enabling in pci_alloc_irq_vectors().
  1014. */
  1015. void pci_free_irq_vectors(struct pci_dev *dev)
  1016. {
  1017. pci_disable_msix(dev);
  1018. pci_disable_msi(dev);
  1019. }
  1020. EXPORT_SYMBOL(pci_free_irq_vectors);
  1021. /**
  1022. * pci_irq_vector - return Linux IRQ number of a device vector
  1023. * @dev: PCI device to operate on
  1024. * @nr: device-relative interrupt vector index (0-based).
  1025. */
  1026. int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
  1027. {
  1028. if (dev->msix_enabled) {
  1029. struct msi_desc *entry;
  1030. int i = 0;
  1031. for_each_pci_msi_entry(entry, dev) {
  1032. if (i == nr)
  1033. return entry->irq;
  1034. i++;
  1035. }
  1036. WARN_ON_ONCE(1);
  1037. return -EINVAL;
  1038. }
  1039. if (dev->msi_enabled) {
  1040. struct msi_desc *entry = first_pci_msi_entry(dev);
  1041. if (WARN_ON_ONCE(nr >= entry->nvec_used))
  1042. return -EINVAL;
  1043. } else {
  1044. if (WARN_ON_ONCE(nr > 0))
  1045. return -EINVAL;
  1046. }
  1047. return dev->irq + nr;
  1048. }
  1049. EXPORT_SYMBOL(pci_irq_vector);
  1050. /**
  1051. * pci_irq_get_affinity - return the affinity of a particular msi vector
  1052. * @dev: PCI device to operate on
  1053. * @nr: device-relative interrupt vector index (0-based).
  1054. */
  1055. const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
  1056. {
  1057. if (dev->msix_enabled) {
  1058. struct msi_desc *entry;
  1059. int i = 0;
  1060. for_each_pci_msi_entry(entry, dev) {
  1061. if (i == nr)
  1062. return entry->affinity;
  1063. i++;
  1064. }
  1065. WARN_ON_ONCE(1);
  1066. return NULL;
  1067. } else if (dev->msi_enabled) {
  1068. struct msi_desc *entry = first_pci_msi_entry(dev);
  1069. if (WARN_ON_ONCE(!entry || !entry->affinity ||
  1070. nr >= entry->nvec_used))
  1071. return NULL;
  1072. return &entry->affinity[nr];
  1073. } else {
  1074. return cpu_possible_mask;
  1075. }
  1076. }
  1077. EXPORT_SYMBOL(pci_irq_get_affinity);
  1078. /**
  1079. * pci_irq_get_node - return the numa node of a particular msi vector
  1080. * @pdev: PCI device to operate on
  1081. * @vec: device-relative interrupt vector index (0-based).
  1082. */
  1083. int pci_irq_get_node(struct pci_dev *pdev, int vec)
  1084. {
  1085. const struct cpumask *mask;
  1086. mask = pci_irq_get_affinity(pdev, vec);
  1087. if (mask)
  1088. return local_memory_node(cpu_to_node(cpumask_first(mask)));
  1089. return dev_to_node(&pdev->dev);
  1090. }
  1091. EXPORT_SYMBOL(pci_irq_get_node);
  1092. struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
  1093. {
  1094. return to_pci_dev(desc->dev);
  1095. }
  1096. EXPORT_SYMBOL(msi_desc_to_pci_dev);
  1097. void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
  1098. {
  1099. struct pci_dev *dev = msi_desc_to_pci_dev(desc);
  1100. return dev->bus->sysdata;
  1101. }
  1102. EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata);
  1103. #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
  1104. /**
  1105. * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
  1106. * @irq_data: Pointer to interrupt data of the MSI interrupt
  1107. * @msg: Pointer to the message
  1108. */
  1109. void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
  1110. {
  1111. struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
  1112. /*
  1113. * For MSI-X desc->irq is always equal to irq_data->irq. For
  1114. * MSI only the first interrupt of MULTI MSI passes the test.
  1115. */
  1116. if (desc->irq == irq_data->irq)
  1117. __pci_write_msi_msg(desc, msg);
  1118. }
  1119. /**
  1120. * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
  1121. * @dev: Pointer to the PCI device
  1122. * @desc: Pointer to the msi descriptor
  1123. *
  1124. * The ID number is only used within the irqdomain.
  1125. */
  1126. irq_hw_number_t pci_msi_domain_calc_hwirq(struct pci_dev *dev,
  1127. struct msi_desc *desc)
  1128. {
  1129. return (irq_hw_number_t)desc->msi_attrib.entry_nr |
  1130. PCI_DEVID(dev->bus->number, dev->devfn) << 11 |
  1131. (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
  1132. }
  1133. static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
  1134. {
  1135. return !desc->msi_attrib.is_msix && desc->nvec_used > 1;
  1136. }
  1137. /**
  1138. * pci_msi_domain_check_cap - Verify that @domain supports the capabilities for @dev
  1139. * @domain: The interrupt domain to check
  1140. * @info: The domain info for verification
  1141. * @dev: The device to check
  1142. *
  1143. * Returns:
  1144. * 0 if the functionality is supported
  1145. * 1 if Multi MSI is requested, but the domain does not support it
  1146. * -ENOTSUPP otherwise
  1147. */
  1148. int pci_msi_domain_check_cap(struct irq_domain *domain,
  1149. struct msi_domain_info *info, struct device *dev)
  1150. {
  1151. struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
  1152. /* Special handling to support __pci_enable_msi_range() */
  1153. if (pci_msi_desc_is_multi_msi(desc) &&
  1154. !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
  1155. return 1;
  1156. else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
  1157. return -ENOTSUPP;
  1158. return 0;
  1159. }
  1160. static int pci_msi_domain_handle_error(struct irq_domain *domain,
  1161. struct msi_desc *desc, int error)
  1162. {
  1163. /* Special handling to support __pci_enable_msi_range() */
  1164. if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
  1165. return 1;
  1166. return error;
  1167. }
  1168. #ifdef GENERIC_MSI_DOMAIN_OPS
  1169. static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
  1170. struct msi_desc *desc)
  1171. {
  1172. arg->desc = desc;
  1173. arg->hwirq = pci_msi_domain_calc_hwirq(msi_desc_to_pci_dev(desc),
  1174. desc);
  1175. }
  1176. #else
  1177. #define pci_msi_domain_set_desc NULL
  1178. #endif
  1179. static struct msi_domain_ops pci_msi_domain_ops_default = {
  1180. .set_desc = pci_msi_domain_set_desc,
  1181. .msi_check = pci_msi_domain_check_cap,
  1182. .handle_error = pci_msi_domain_handle_error,
  1183. };
  1184. static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
  1185. {
  1186. struct msi_domain_ops *ops = info->ops;
  1187. if (ops == NULL) {
  1188. info->ops = &pci_msi_domain_ops_default;
  1189. } else {
  1190. if (ops->set_desc == NULL)
  1191. ops->set_desc = pci_msi_domain_set_desc;
  1192. if (ops->msi_check == NULL)
  1193. ops->msi_check = pci_msi_domain_check_cap;
  1194. if (ops->handle_error == NULL)
  1195. ops->handle_error = pci_msi_domain_handle_error;
  1196. }
  1197. }
  1198. static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
  1199. {
  1200. struct irq_chip *chip = info->chip;
  1201. BUG_ON(!chip);
  1202. if (!chip->irq_write_msi_msg)
  1203. chip->irq_write_msi_msg = pci_msi_domain_write_msg;
  1204. if (!chip->irq_mask)
  1205. chip->irq_mask = pci_msi_mask_irq;
  1206. if (!chip->irq_unmask)
  1207. chip->irq_unmask = pci_msi_unmask_irq;
  1208. }
  1209. /**
  1210. * pci_msi_create_irq_domain - Create a MSI interrupt domain
  1211. * @fwnode: Optional fwnode of the interrupt controller
  1212. * @info: MSI domain info
  1213. * @parent: Parent irq domain
  1214. *
  1215. * Updates the domain and chip ops and creates a MSI interrupt domain.
  1216. *
  1217. * Returns:
  1218. * A domain pointer or NULL in case of failure.
  1219. */
  1220. struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
  1221. struct msi_domain_info *info,
  1222. struct irq_domain *parent)
  1223. {
  1224. struct irq_domain *domain;
  1225. if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
  1226. pci_msi_domain_update_dom_ops(info);
  1227. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  1228. pci_msi_domain_update_chip_ops(info);
  1229. info->flags |= MSI_FLAG_ACTIVATE_EARLY;
  1230. domain = msi_create_irq_domain(fwnode, info, parent);
  1231. if (!domain)
  1232. return NULL;
  1233. domain->bus_token = DOMAIN_BUS_PCI_MSI;
  1234. return domain;
  1235. }
  1236. EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
  1237. static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
  1238. {
  1239. u32 *pa = data;
  1240. *pa = alias;
  1241. return 0;
  1242. }
  1243. /**
  1244. * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
  1245. * @domain: The interrupt domain
  1246. * @pdev: The PCI device.
  1247. *
  1248. * The RID for a device is formed from the alias, with a firmware
  1249. * supplied mapping applied
  1250. *
  1251. * Returns: The RID.
  1252. */
  1253. u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
  1254. {
  1255. struct device_node *of_node;
  1256. u32 rid = 0;
  1257. pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
  1258. of_node = irq_domain_get_of_node(domain);
  1259. rid = of_node ? of_msi_map_rid(&pdev->dev, of_node, rid) :
  1260. iort_msi_map_rid(&pdev->dev, rid);
  1261. return rid;
  1262. }
  1263. /**
  1264. * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
  1265. * @pdev: The PCI device
  1266. *
  1267. * Use the firmware data to find a device-specific MSI domain
  1268. * (i.e. not one that is ste as a default).
  1269. *
  1270. * Returns: The coresponding MSI domain or NULL if none has been found.
  1271. */
  1272. struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
  1273. {
  1274. struct irq_domain *dom;
  1275. u32 rid = 0;
  1276. pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
  1277. dom = of_msi_map_get_device_domain(&pdev->dev, rid);
  1278. if (!dom)
  1279. dom = iort_get_device_domain(&pdev->dev, rid);
  1280. return dom;
  1281. }
  1282. #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */