msi.c 35 KB

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