msi.c 39 KB

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