msi.c 35 KB

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