msi.c 39 KB

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