msi.c 35 KB

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