msi.c 34 KB

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