msi.c 35 KB

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