msi.c 40 KB

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