msi.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  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 & PCI_MSIX_ENTRY_CTRL_MASKBIT)
  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. /* Check whether driver already requested for MSI irq */
  800. if (dev->msi_enabled) {
  801. pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n");
  802. return -EINVAL;
  803. }
  804. return msix_capability_init(dev, entries, nvec, affd);
  805. }
  806. static void pci_msix_shutdown(struct pci_dev *dev)
  807. {
  808. struct msi_desc *entry;
  809. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  810. return;
  811. if (pci_dev_is_disconnected(dev)) {
  812. dev->msix_enabled = 0;
  813. return;
  814. }
  815. /* Return the device with MSI-X masked as initial states */
  816. for_each_pci_msi_entry(entry, dev) {
  817. /* Keep cached states to be restored */
  818. __pci_msix_desc_mask_irq(entry, 1);
  819. }
  820. pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
  821. pci_intx_for_msi(dev, 1);
  822. dev->msix_enabled = 0;
  823. pcibios_alloc_irq(dev);
  824. }
  825. void pci_disable_msix(struct pci_dev *dev)
  826. {
  827. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  828. return;
  829. pci_msix_shutdown(dev);
  830. free_msi_irqs(dev);
  831. }
  832. EXPORT_SYMBOL(pci_disable_msix);
  833. void pci_no_msi(void)
  834. {
  835. pci_msi_enable = 0;
  836. }
  837. /**
  838. * pci_msi_enabled - is MSI enabled?
  839. *
  840. * Returns true if MSI has not been disabled by the command-line option
  841. * pci=nomsi.
  842. **/
  843. int pci_msi_enabled(void)
  844. {
  845. return pci_msi_enable;
  846. }
  847. EXPORT_SYMBOL(pci_msi_enabled);
  848. static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
  849. const struct irq_affinity *affd)
  850. {
  851. int nvec;
  852. int rc;
  853. if (!pci_msi_supported(dev, minvec))
  854. return -EINVAL;
  855. /* Check whether driver already requested MSI-X irqs */
  856. if (dev->msix_enabled) {
  857. pci_info(dev, "can't enable MSI (MSI-X already enabled)\n");
  858. return -EINVAL;
  859. }
  860. if (maxvec < minvec)
  861. return -ERANGE;
  862. if (WARN_ON_ONCE(dev->msi_enabled))
  863. return -EINVAL;
  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. if (WARN_ON_ONCE(dev->msix_enabled))
  904. return -EINVAL;
  905. for (;;) {
  906. if (affd) {
  907. nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
  908. if (nvec < minvec)
  909. return -ENOSPC;
  910. }
  911. rc = __pci_enable_msix(dev, entries, nvec, affd);
  912. if (rc == 0)
  913. return nvec;
  914. if (rc < 0)
  915. return rc;
  916. if (rc < minvec)
  917. return -ENOSPC;
  918. nvec = rc;
  919. }
  920. }
  921. /**
  922. * pci_enable_msix_range - configure device's MSI-X capability structure
  923. * @dev: pointer to the pci_dev data structure of MSI-X device function
  924. * @entries: pointer to an array of MSI-X entries
  925. * @minvec: minimum number of MSI-X irqs requested
  926. * @maxvec: maximum number of MSI-X irqs requested
  927. *
  928. * Setup the MSI-X capability structure of device function with a maximum
  929. * possible number of interrupts in the range between @minvec and @maxvec
  930. * upon its software driver call to request for MSI-X mode enabled on its
  931. * hardware device function. It returns a negative errno if an error occurs.
  932. * If it succeeds, it returns the actual number of interrupts allocated and
  933. * indicates the successful configuration of MSI-X capability structure
  934. * with new allocated MSI-X interrupts.
  935. **/
  936. int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
  937. int minvec, int maxvec)
  938. {
  939. return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL);
  940. }
  941. EXPORT_SYMBOL(pci_enable_msix_range);
  942. /**
  943. * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device
  944. * @dev: PCI device to operate on
  945. * @min_vecs: minimum number of vectors required (must be >= 1)
  946. * @max_vecs: maximum (desired) number of vectors
  947. * @flags: flags or quirks for the allocation
  948. * @affd: optional description of the affinity requirements
  949. *
  950. * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI
  951. * vectors if available, and fall back to a single legacy vector
  952. * if neither is available. Return the number of vectors allocated,
  953. * (which might be smaller than @max_vecs) if successful, or a negative
  954. * error code on error. If less than @min_vecs interrupt vectors are
  955. * available for @dev the function will fail with -ENOSPC.
  956. *
  957. * To get the Linux IRQ number used for a vector that can be passed to
  958. * request_irq() use the pci_irq_vector() helper.
  959. */
  960. int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
  961. unsigned int max_vecs, unsigned int flags,
  962. const struct irq_affinity *affd)
  963. {
  964. static const struct irq_affinity msi_default_affd;
  965. int msix_vecs = -ENOSPC;
  966. int msi_vecs = -ENOSPC;
  967. if (flags & PCI_IRQ_AFFINITY) {
  968. if (!affd)
  969. affd = &msi_default_affd;
  970. } else {
  971. if (WARN_ON(affd))
  972. affd = NULL;
  973. }
  974. if (flags & PCI_IRQ_MSIX) {
  975. msix_vecs = __pci_enable_msix_range(dev, NULL, min_vecs,
  976. max_vecs, affd);
  977. if (msix_vecs > 0)
  978. return msix_vecs;
  979. }
  980. if (flags & PCI_IRQ_MSI) {
  981. msi_vecs = __pci_enable_msi_range(dev, min_vecs, max_vecs,
  982. affd);
  983. if (msi_vecs > 0)
  984. return msi_vecs;
  985. }
  986. /* use legacy irq if allowed */
  987. if (flags & PCI_IRQ_LEGACY) {
  988. if (min_vecs == 1 && dev->irq) {
  989. pci_intx(dev, 1);
  990. return 1;
  991. }
  992. }
  993. if (msix_vecs == -ENOSPC)
  994. return -ENOSPC;
  995. return msi_vecs;
  996. }
  997. EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);
  998. /**
  999. * pci_free_irq_vectors - free previously allocated IRQs for a device
  1000. * @dev: PCI device to operate on
  1001. *
  1002. * Undoes the allocations and enabling in pci_alloc_irq_vectors().
  1003. */
  1004. void pci_free_irq_vectors(struct pci_dev *dev)
  1005. {
  1006. pci_disable_msix(dev);
  1007. pci_disable_msi(dev);
  1008. }
  1009. EXPORT_SYMBOL(pci_free_irq_vectors);
  1010. /**
  1011. * pci_irq_vector - return Linux IRQ number of a device vector
  1012. * @dev: PCI device to operate on
  1013. * @nr: device-relative interrupt vector index (0-based).
  1014. */
  1015. int pci_irq_vector(struct pci_dev *dev, unsigned int nr)
  1016. {
  1017. if (dev->msix_enabled) {
  1018. struct msi_desc *entry;
  1019. int i = 0;
  1020. for_each_pci_msi_entry(entry, dev) {
  1021. if (i == nr)
  1022. return entry->irq;
  1023. i++;
  1024. }
  1025. WARN_ON_ONCE(1);
  1026. return -EINVAL;
  1027. }
  1028. if (dev->msi_enabled) {
  1029. struct msi_desc *entry = first_pci_msi_entry(dev);
  1030. if (WARN_ON_ONCE(nr >= entry->nvec_used))
  1031. return -EINVAL;
  1032. } else {
  1033. if (WARN_ON_ONCE(nr > 0))
  1034. return -EINVAL;
  1035. }
  1036. return dev->irq + nr;
  1037. }
  1038. EXPORT_SYMBOL(pci_irq_vector);
  1039. /**
  1040. * pci_irq_get_affinity - return the affinity of a particular msi vector
  1041. * @dev: PCI device to operate on
  1042. * @nr: device-relative interrupt vector index (0-based).
  1043. */
  1044. const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)
  1045. {
  1046. if (dev->msix_enabled) {
  1047. struct msi_desc *entry;
  1048. int i = 0;
  1049. for_each_pci_msi_entry(entry, dev) {
  1050. if (i == nr)
  1051. return entry->affinity;
  1052. i++;
  1053. }
  1054. WARN_ON_ONCE(1);
  1055. return NULL;
  1056. } else if (dev->msi_enabled) {
  1057. struct msi_desc *entry = first_pci_msi_entry(dev);
  1058. if (WARN_ON_ONCE(!entry || !entry->affinity ||
  1059. nr >= entry->nvec_used))
  1060. return NULL;
  1061. return &entry->affinity[nr];
  1062. } else {
  1063. return cpu_possible_mask;
  1064. }
  1065. }
  1066. EXPORT_SYMBOL(pci_irq_get_affinity);
  1067. /**
  1068. * pci_irq_get_node - return the numa node of a particular msi vector
  1069. * @pdev: PCI device to operate on
  1070. * @vec: device-relative interrupt vector index (0-based).
  1071. */
  1072. int pci_irq_get_node(struct pci_dev *pdev, int vec)
  1073. {
  1074. const struct cpumask *mask;
  1075. mask = pci_irq_get_affinity(pdev, vec);
  1076. if (mask)
  1077. return local_memory_node(cpu_to_node(cpumask_first(mask)));
  1078. return dev_to_node(&pdev->dev);
  1079. }
  1080. EXPORT_SYMBOL(pci_irq_get_node);
  1081. struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
  1082. {
  1083. return to_pci_dev(desc->dev);
  1084. }
  1085. EXPORT_SYMBOL(msi_desc_to_pci_dev);
  1086. void *msi_desc_to_pci_sysdata(struct msi_desc *desc)
  1087. {
  1088. struct pci_dev *dev = msi_desc_to_pci_dev(desc);
  1089. return dev->bus->sysdata;
  1090. }
  1091. EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata);
  1092. #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN
  1093. /**
  1094. * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space
  1095. * @irq_data: Pointer to interrupt data of the MSI interrupt
  1096. * @msg: Pointer to the message
  1097. */
  1098. void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg)
  1099. {
  1100. struct msi_desc *desc = irq_data_get_msi_desc(irq_data);
  1101. /*
  1102. * For MSI-X desc->irq is always equal to irq_data->irq. For
  1103. * MSI only the first interrupt of MULTI MSI passes the test.
  1104. */
  1105. if (desc->irq == irq_data->irq)
  1106. __pci_write_msi_msg(desc, msg);
  1107. }
  1108. /**
  1109. * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source
  1110. * @dev: Pointer to the PCI device
  1111. * @desc: Pointer to the msi descriptor
  1112. *
  1113. * The ID number is only used within the irqdomain.
  1114. */
  1115. irq_hw_number_t pci_msi_domain_calc_hwirq(struct pci_dev *dev,
  1116. struct msi_desc *desc)
  1117. {
  1118. return (irq_hw_number_t)desc->msi_attrib.entry_nr |
  1119. PCI_DEVID(dev->bus->number, dev->devfn) << 11 |
  1120. (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27;
  1121. }
  1122. static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc)
  1123. {
  1124. return !desc->msi_attrib.is_msix && desc->nvec_used > 1;
  1125. }
  1126. /**
  1127. * pci_msi_domain_check_cap - Verify that @domain supports the capabilities for @dev
  1128. * @domain: The interrupt domain to check
  1129. * @info: The domain info for verification
  1130. * @dev: The device to check
  1131. *
  1132. * Returns:
  1133. * 0 if the functionality is supported
  1134. * 1 if Multi MSI is requested, but the domain does not support it
  1135. * -ENOTSUPP otherwise
  1136. */
  1137. int pci_msi_domain_check_cap(struct irq_domain *domain,
  1138. struct msi_domain_info *info, struct device *dev)
  1139. {
  1140. struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev));
  1141. /* Special handling to support __pci_enable_msi_range() */
  1142. if (pci_msi_desc_is_multi_msi(desc) &&
  1143. !(info->flags & MSI_FLAG_MULTI_PCI_MSI))
  1144. return 1;
  1145. else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX))
  1146. return -ENOTSUPP;
  1147. return 0;
  1148. }
  1149. static int pci_msi_domain_handle_error(struct irq_domain *domain,
  1150. struct msi_desc *desc, int error)
  1151. {
  1152. /* Special handling to support __pci_enable_msi_range() */
  1153. if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC)
  1154. return 1;
  1155. return error;
  1156. }
  1157. #ifdef GENERIC_MSI_DOMAIN_OPS
  1158. static void pci_msi_domain_set_desc(msi_alloc_info_t *arg,
  1159. struct msi_desc *desc)
  1160. {
  1161. arg->desc = desc;
  1162. arg->hwirq = pci_msi_domain_calc_hwirq(msi_desc_to_pci_dev(desc),
  1163. desc);
  1164. }
  1165. #else
  1166. #define pci_msi_domain_set_desc NULL
  1167. #endif
  1168. static struct msi_domain_ops pci_msi_domain_ops_default = {
  1169. .set_desc = pci_msi_domain_set_desc,
  1170. .msi_check = pci_msi_domain_check_cap,
  1171. .handle_error = pci_msi_domain_handle_error,
  1172. };
  1173. static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info)
  1174. {
  1175. struct msi_domain_ops *ops = info->ops;
  1176. if (ops == NULL) {
  1177. info->ops = &pci_msi_domain_ops_default;
  1178. } else {
  1179. if (ops->set_desc == NULL)
  1180. ops->set_desc = pci_msi_domain_set_desc;
  1181. if (ops->msi_check == NULL)
  1182. ops->msi_check = pci_msi_domain_check_cap;
  1183. if (ops->handle_error == NULL)
  1184. ops->handle_error = pci_msi_domain_handle_error;
  1185. }
  1186. }
  1187. static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info)
  1188. {
  1189. struct irq_chip *chip = info->chip;
  1190. BUG_ON(!chip);
  1191. if (!chip->irq_write_msi_msg)
  1192. chip->irq_write_msi_msg = pci_msi_domain_write_msg;
  1193. if (!chip->irq_mask)
  1194. chip->irq_mask = pci_msi_mask_irq;
  1195. if (!chip->irq_unmask)
  1196. chip->irq_unmask = pci_msi_unmask_irq;
  1197. }
  1198. /**
  1199. * pci_msi_create_irq_domain - Create a MSI interrupt domain
  1200. * @fwnode: Optional fwnode of the interrupt controller
  1201. * @info: MSI domain info
  1202. * @parent: Parent irq domain
  1203. *
  1204. * Updates the domain and chip ops and creates a MSI interrupt domain.
  1205. *
  1206. * Returns:
  1207. * A domain pointer or NULL in case of failure.
  1208. */
  1209. struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode,
  1210. struct msi_domain_info *info,
  1211. struct irq_domain *parent)
  1212. {
  1213. struct irq_domain *domain;
  1214. if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE))
  1215. info->flags &= ~MSI_FLAG_LEVEL_CAPABLE;
  1216. if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
  1217. pci_msi_domain_update_dom_ops(info);
  1218. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  1219. pci_msi_domain_update_chip_ops(info);
  1220. info->flags |= MSI_FLAG_ACTIVATE_EARLY;
  1221. if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
  1222. info->flags |= MSI_FLAG_MUST_REACTIVATE;
  1223. /* PCI-MSI is oneshot-safe */
  1224. info->chip->flags |= IRQCHIP_ONESHOT_SAFE;
  1225. domain = msi_create_irq_domain(fwnode, info, parent);
  1226. if (!domain)
  1227. return NULL;
  1228. irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI);
  1229. return domain;
  1230. }
  1231. EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain);
  1232. /*
  1233. * Users of the generic MSI infrastructure expect a device to have a single ID,
  1234. * so with DMA aliases we have to pick the least-worst compromise. Devices with
  1235. * DMA phantom functions tend to still emit MSIs from the real function number,
  1236. * so we ignore those and only consider topological aliases where either the
  1237. * alias device or RID appears on a different bus number. We also make the
  1238. * reasonable assumption that bridges are walked in an upstream direction (so
  1239. * the last one seen wins), and the much braver assumption that the most likely
  1240. * case is that of PCI->PCIe so we should always use the alias RID. This echoes
  1241. * the logic from intel_irq_remapping's set_msi_sid(), which presumably works
  1242. * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions
  1243. * for taking ownership all we can really do is close our eyes and hope...
  1244. */
  1245. static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data)
  1246. {
  1247. u32 *pa = data;
  1248. u8 bus = PCI_BUS_NUM(*pa);
  1249. if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus)
  1250. *pa = alias;
  1251. return 0;
  1252. }
  1253. /**
  1254. * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID)
  1255. * @domain: The interrupt domain
  1256. * @pdev: The PCI device.
  1257. *
  1258. * The RID for a device is formed from the alias, with a firmware
  1259. * supplied mapping applied
  1260. *
  1261. * Returns: The RID.
  1262. */
  1263. u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev)
  1264. {
  1265. struct device_node *of_node;
  1266. u32 rid = PCI_DEVID(pdev->bus->number, pdev->devfn);
  1267. pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
  1268. of_node = irq_domain_get_of_node(domain);
  1269. rid = of_node ? of_msi_map_rid(&pdev->dev, of_node, rid) :
  1270. iort_msi_map_rid(&pdev->dev, rid);
  1271. return rid;
  1272. }
  1273. /**
  1274. * pci_msi_get_device_domain - Get the MSI domain for a given PCI device
  1275. * @pdev: The PCI device
  1276. *
  1277. * Use the firmware data to find a device-specific MSI domain
  1278. * (i.e. not one that is set as a default).
  1279. *
  1280. * Returns: The corresponding MSI domain or NULL if none has been found.
  1281. */
  1282. struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev)
  1283. {
  1284. struct irq_domain *dom;
  1285. u32 rid = PCI_DEVID(pdev->bus->number, pdev->devfn);
  1286. pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid);
  1287. dom = of_msi_map_get_device_domain(&pdev->dev, rid);
  1288. if (!dom)
  1289. dom = iort_get_device_domain(&pdev->dev, rid);
  1290. return dom;
  1291. }
  1292. #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */