msi.c 40 KB

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