msi.c 40 KB

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