msi.c 34 KB

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