msi.c 39 KB

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