msi.c 34 KB

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