msi.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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. if (!chip->irq_mask)
  1041. chip->irq_mask = pci_msi_mask_irq;
  1042. if (!chip->irq_unmask)
  1043. chip->irq_unmask = pci_msi_unmask_irq;
  1044. }
  1045. /**
  1046. * pci_msi_create_irq_domain - Creat a MSI interrupt domain
  1047. * @node: Optional device-tree node of the interrupt controller
  1048. * @info: MSI domain info
  1049. * @parent: Parent irq domain
  1050. *
  1051. * Updates the domain and chip ops and creates a MSI interrupt domain.
  1052. *
  1053. * Returns:
  1054. * A domain pointer or NULL in case of failure.
  1055. */
  1056. struct irq_domain *pci_msi_create_irq_domain(struct device_node *node,
  1057. struct msi_domain_info *info,
  1058. struct irq_domain *parent)
  1059. {
  1060. struct irq_domain *domain;
  1061. if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
  1062. pci_msi_domain_update_dom_ops(info);
  1063. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  1064. pci_msi_domain_update_chip_ops(info);
  1065. domain = msi_create_irq_domain(node, info, parent);
  1066. if (!domain)
  1067. return NULL;
  1068. domain->bus_token = DOMAIN_BUS_PCI_MSI;
  1069. return domain;
  1070. }
  1071. /**
  1072. * pci_msi_domain_alloc_irqs - Allocate interrupts for @dev in @domain
  1073. * @domain: The interrupt domain to allocate from
  1074. * @dev: The device for which to allocate
  1075. * @nvec: The number of interrupts to allocate
  1076. * @type: Unused to allow simpler migration from the arch_XXX interfaces
  1077. *
  1078. * Returns:
  1079. * A virtual interrupt number or an error code in case of failure
  1080. */
  1081. int pci_msi_domain_alloc_irqs(struct irq_domain *domain, struct pci_dev *dev,
  1082. int nvec, int type)
  1083. {
  1084. return msi_domain_alloc_irqs(domain, &dev->dev, nvec);
  1085. }
  1086. /**
  1087. * pci_msi_domain_free_irqs - Free interrupts for @dev in @domain
  1088. * @domain: The interrupt domain
  1089. * @dev: The device for which to free interrupts
  1090. */
  1091. void pci_msi_domain_free_irqs(struct irq_domain *domain, struct pci_dev *dev)
  1092. {
  1093. msi_domain_free_irqs(domain, &dev->dev);
  1094. }
  1095. /**
  1096. * pci_msi_create_default_irq_domain - Create a default MSI interrupt domain
  1097. * @node: Optional device-tree node of the interrupt controller
  1098. * @info: MSI domain info
  1099. * @parent: Parent irq domain
  1100. *
  1101. * Returns: A domain pointer or NULL in case of failure. If successful
  1102. * the default PCI/MSI irqdomain pointer is updated.
  1103. */
  1104. struct irq_domain *pci_msi_create_default_irq_domain(struct device_node *node,
  1105. struct msi_domain_info *info, struct irq_domain *parent)
  1106. {
  1107. struct irq_domain *domain;
  1108. mutex_lock(&pci_msi_domain_lock);
  1109. if (pci_msi_default_domain) {
  1110. pr_err("PCI: default irq domain for PCI MSI has already been created.\n");
  1111. domain = NULL;
  1112. } else {
  1113. domain = pci_msi_create_irq_domain(node, info, parent);
  1114. pci_msi_default_domain = domain;
  1115. }
  1116. mutex_unlock(&pci_msi_domain_lock);
  1117. return domain;
  1118. }
  1119. #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */