msi.c 35 KB

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