msi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Intel Corp.
  4. * Author: Jiang Liu <jiang.liu@linux.intel.com>
  5. *
  6. * This file is licensed under GPLv2.
  7. *
  8. * This file contains common code to support Message Signalled Interrupt for
  9. * PCI compatible and non PCI compatible devices.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/device.h>
  13. #include <linux/irq.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/msi.h>
  16. #include <linux/slab.h>
  17. #include "internals.h"
  18. /**
  19. * alloc_msi_entry - Allocate an initialize msi_entry
  20. * @dev: Pointer to the device for which this is allocated
  21. * @nvec: The number of vectors used in this entry
  22. * @affinity: Optional pointer to an affinity mask array size of @nvec
  23. *
  24. * If @affinity is not NULL then a an affinity array[@nvec] is allocated
  25. * and the affinity masks from @affinity are copied.
  26. */
  27. struct msi_desc *
  28. alloc_msi_entry(struct device *dev, int nvec, const struct cpumask *affinity)
  29. {
  30. struct msi_desc *desc;
  31. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  32. if (!desc)
  33. return NULL;
  34. INIT_LIST_HEAD(&desc->list);
  35. desc->dev = dev;
  36. desc->nvec_used = nvec;
  37. if (affinity) {
  38. desc->affinity = kmemdup(affinity,
  39. nvec * sizeof(*desc->affinity), GFP_KERNEL);
  40. if (!desc->affinity) {
  41. kfree(desc);
  42. return NULL;
  43. }
  44. }
  45. return desc;
  46. }
  47. void free_msi_entry(struct msi_desc *entry)
  48. {
  49. kfree(entry->affinity);
  50. kfree(entry);
  51. }
  52. void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  53. {
  54. *msg = entry->msg;
  55. }
  56. void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
  57. {
  58. struct msi_desc *entry = irq_get_msi_desc(irq);
  59. __get_cached_msi_msg(entry, msg);
  60. }
  61. EXPORT_SYMBOL_GPL(get_cached_msi_msg);
  62. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  63. static inline void irq_chip_write_msi_msg(struct irq_data *data,
  64. struct msi_msg *msg)
  65. {
  66. data->chip->irq_write_msi_msg(data, msg);
  67. }
  68. /**
  69. * msi_domain_set_affinity - Generic affinity setter function for MSI domains
  70. * @irq_data: The irq data associated to the interrupt
  71. * @mask: The affinity mask to set
  72. * @force: Flag to enforce setting (disable online checks)
  73. *
  74. * Intended to be used by MSI interrupt controllers which are
  75. * implemented with hierarchical domains.
  76. */
  77. int msi_domain_set_affinity(struct irq_data *irq_data,
  78. const struct cpumask *mask, bool force)
  79. {
  80. struct irq_data *parent = irq_data->parent_data;
  81. struct msi_msg msg;
  82. int ret;
  83. ret = parent->chip->irq_set_affinity(parent, mask, force);
  84. if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
  85. BUG_ON(irq_chip_compose_msi_msg(irq_data, &msg));
  86. irq_chip_write_msi_msg(irq_data, &msg);
  87. }
  88. return ret;
  89. }
  90. static int msi_domain_activate(struct irq_domain *domain,
  91. struct irq_data *irq_data, bool early)
  92. {
  93. struct msi_msg msg;
  94. BUG_ON(irq_chip_compose_msi_msg(irq_data, &msg));
  95. irq_chip_write_msi_msg(irq_data, &msg);
  96. return 0;
  97. }
  98. static void msi_domain_deactivate(struct irq_domain *domain,
  99. struct irq_data *irq_data)
  100. {
  101. struct msi_msg msg;
  102. memset(&msg, 0, sizeof(msg));
  103. irq_chip_write_msi_msg(irq_data, &msg);
  104. }
  105. static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
  106. unsigned int nr_irqs, void *arg)
  107. {
  108. struct msi_domain_info *info = domain->host_data;
  109. struct msi_domain_ops *ops = info->ops;
  110. irq_hw_number_t hwirq = ops->get_hwirq(info, arg);
  111. int i, ret;
  112. if (irq_find_mapping(domain, hwirq) > 0)
  113. return -EEXIST;
  114. if (domain->parent) {
  115. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  116. if (ret < 0)
  117. return ret;
  118. }
  119. for (i = 0; i < nr_irqs; i++) {
  120. ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg);
  121. if (ret < 0) {
  122. if (ops->msi_free) {
  123. for (i--; i > 0; i--)
  124. ops->msi_free(domain, info, virq + i);
  125. }
  126. irq_domain_free_irqs_top(domain, virq, nr_irqs);
  127. return ret;
  128. }
  129. }
  130. return 0;
  131. }
  132. static void msi_domain_free(struct irq_domain *domain, unsigned int virq,
  133. unsigned int nr_irqs)
  134. {
  135. struct msi_domain_info *info = domain->host_data;
  136. int i;
  137. if (info->ops->msi_free) {
  138. for (i = 0; i < nr_irqs; i++)
  139. info->ops->msi_free(domain, info, virq + i);
  140. }
  141. irq_domain_free_irqs_top(domain, virq, nr_irqs);
  142. }
  143. static const struct irq_domain_ops msi_domain_ops = {
  144. .alloc = msi_domain_alloc,
  145. .free = msi_domain_free,
  146. .activate = msi_domain_activate,
  147. .deactivate = msi_domain_deactivate,
  148. };
  149. #ifdef GENERIC_MSI_DOMAIN_OPS
  150. static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info,
  151. msi_alloc_info_t *arg)
  152. {
  153. return arg->hwirq;
  154. }
  155. static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev,
  156. int nvec, msi_alloc_info_t *arg)
  157. {
  158. memset(arg, 0, sizeof(*arg));
  159. return 0;
  160. }
  161. static void msi_domain_ops_set_desc(msi_alloc_info_t *arg,
  162. struct msi_desc *desc)
  163. {
  164. arg->desc = desc;
  165. }
  166. #else
  167. #define msi_domain_ops_get_hwirq NULL
  168. #define msi_domain_ops_prepare NULL
  169. #define msi_domain_ops_set_desc NULL
  170. #endif /* !GENERIC_MSI_DOMAIN_OPS */
  171. static int msi_domain_ops_init(struct irq_domain *domain,
  172. struct msi_domain_info *info,
  173. unsigned int virq, irq_hw_number_t hwirq,
  174. msi_alloc_info_t *arg)
  175. {
  176. irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip,
  177. info->chip_data);
  178. if (info->handler && info->handler_name) {
  179. __irq_set_handler(virq, info->handler, 0, info->handler_name);
  180. if (info->handler_data)
  181. irq_set_handler_data(virq, info->handler_data);
  182. }
  183. return 0;
  184. }
  185. static int msi_domain_ops_check(struct irq_domain *domain,
  186. struct msi_domain_info *info,
  187. struct device *dev)
  188. {
  189. return 0;
  190. }
  191. static struct msi_domain_ops msi_domain_ops_default = {
  192. .get_hwirq = msi_domain_ops_get_hwirq,
  193. .msi_init = msi_domain_ops_init,
  194. .msi_check = msi_domain_ops_check,
  195. .msi_prepare = msi_domain_ops_prepare,
  196. .set_desc = msi_domain_ops_set_desc,
  197. };
  198. static void msi_domain_update_dom_ops(struct msi_domain_info *info)
  199. {
  200. struct msi_domain_ops *ops = info->ops;
  201. if (ops == NULL) {
  202. info->ops = &msi_domain_ops_default;
  203. return;
  204. }
  205. if (ops->get_hwirq == NULL)
  206. ops->get_hwirq = msi_domain_ops_default.get_hwirq;
  207. if (ops->msi_init == NULL)
  208. ops->msi_init = msi_domain_ops_default.msi_init;
  209. if (ops->msi_check == NULL)
  210. ops->msi_check = msi_domain_ops_default.msi_check;
  211. if (ops->msi_prepare == NULL)
  212. ops->msi_prepare = msi_domain_ops_default.msi_prepare;
  213. if (ops->set_desc == NULL)
  214. ops->set_desc = msi_domain_ops_default.set_desc;
  215. }
  216. static void msi_domain_update_chip_ops(struct msi_domain_info *info)
  217. {
  218. struct irq_chip *chip = info->chip;
  219. BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask);
  220. if (!chip->irq_set_affinity)
  221. chip->irq_set_affinity = msi_domain_set_affinity;
  222. }
  223. /**
  224. * msi_create_irq_domain - Create a MSI interrupt domain
  225. * @fwnode: Optional fwnode of the interrupt controller
  226. * @info: MSI domain info
  227. * @parent: Parent irq domain
  228. */
  229. struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
  230. struct msi_domain_info *info,
  231. struct irq_domain *parent)
  232. {
  233. struct irq_domain *domain;
  234. if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
  235. msi_domain_update_dom_ops(info);
  236. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  237. msi_domain_update_chip_ops(info);
  238. domain = irq_domain_create_hierarchy(parent, IRQ_DOMAIN_FLAG_MSI, 0,
  239. fwnode, &msi_domain_ops, info);
  240. if (domain && !domain->name && info->chip)
  241. domain->name = info->chip->name;
  242. return domain;
  243. }
  244. int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
  245. int nvec, msi_alloc_info_t *arg)
  246. {
  247. struct msi_domain_info *info = domain->host_data;
  248. struct msi_domain_ops *ops = info->ops;
  249. int ret;
  250. ret = ops->msi_check(domain, info, dev);
  251. if (ret == 0)
  252. ret = ops->msi_prepare(domain, dev, nvec, arg);
  253. return ret;
  254. }
  255. int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
  256. int virq, int nvec, msi_alloc_info_t *arg)
  257. {
  258. struct msi_domain_info *info = domain->host_data;
  259. struct msi_domain_ops *ops = info->ops;
  260. struct msi_desc *desc;
  261. int ret = 0;
  262. for_each_msi_entry(desc, dev) {
  263. /* Don't even try the multi-MSI brain damage. */
  264. if (WARN_ON(!desc->irq || desc->nvec_used != 1)) {
  265. ret = -EINVAL;
  266. break;
  267. }
  268. if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
  269. continue;
  270. ops->set_desc(arg, desc);
  271. /* Assumes the domain mutex is held! */
  272. ret = irq_domain_alloc_irqs_hierarchy(domain, desc->irq, 1,
  273. arg);
  274. if (ret)
  275. break;
  276. irq_set_msi_desc_off(desc->irq, 0, desc);
  277. }
  278. if (ret) {
  279. /* Mop up the damage */
  280. for_each_msi_entry(desc, dev) {
  281. if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
  282. continue;
  283. irq_domain_free_irqs_common(domain, desc->irq, 1);
  284. }
  285. }
  286. return ret;
  287. }
  288. /*
  289. * Carefully check whether the device can use reservation mode. If
  290. * reservation mode is enabled then the early activation will assign a
  291. * dummy vector to the device. If the PCI/MSI device does not support
  292. * masking of the entry then this can result in spurious interrupts when
  293. * the device driver is not absolutely careful. But even then a malfunction
  294. * of the hardware could result in a spurious interrupt on the dummy vector
  295. * and render the device unusable. If the entry can be masked then the core
  296. * logic will prevent the spurious interrupt and reservation mode can be
  297. * used. For now reservation mode is restricted to PCI/MSI.
  298. */
  299. static bool msi_check_reservation_mode(struct irq_domain *domain,
  300. struct msi_domain_info *info,
  301. struct device *dev)
  302. {
  303. struct msi_desc *desc;
  304. if (domain->bus_token != DOMAIN_BUS_PCI_MSI)
  305. return false;
  306. if (!(info->flags & MSI_FLAG_MUST_REACTIVATE))
  307. return false;
  308. if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_ignore_mask)
  309. return false;
  310. /*
  311. * Checking the first MSI descriptor is sufficient. MSIX supports
  312. * masking and MSI does so when the maskbit is set.
  313. */
  314. desc = first_msi_entry(dev);
  315. return desc->msi_attrib.is_msix || desc->msi_attrib.maskbit;
  316. }
  317. /**
  318. * msi_domain_alloc_irqs - Allocate interrupts from a MSI interrupt domain
  319. * @domain: The domain to allocate from
  320. * @dev: Pointer to device struct of the device for which the interrupts
  321. * are allocated
  322. * @nvec: The number of interrupts to allocate
  323. *
  324. * Returns 0 on success or an error code.
  325. */
  326. int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
  327. int nvec)
  328. {
  329. struct msi_domain_info *info = domain->host_data;
  330. struct msi_domain_ops *ops = info->ops;
  331. struct irq_data *irq_data;
  332. struct msi_desc *desc;
  333. msi_alloc_info_t arg;
  334. int i, ret, virq;
  335. bool can_reserve;
  336. ret = msi_domain_prepare_irqs(domain, dev, nvec, &arg);
  337. if (ret)
  338. return ret;
  339. for_each_msi_entry(desc, dev) {
  340. ops->set_desc(&arg, desc);
  341. virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
  342. dev_to_node(dev), &arg, false,
  343. desc->affinity);
  344. if (virq < 0) {
  345. ret = -ENOSPC;
  346. if (ops->handle_error)
  347. ret = ops->handle_error(domain, desc, ret);
  348. if (ops->msi_finish)
  349. ops->msi_finish(&arg, ret);
  350. return ret;
  351. }
  352. for (i = 0; i < desc->nvec_used; i++) {
  353. irq_set_msi_desc_off(virq, i, desc);
  354. irq_debugfs_copy_devname(virq + i, dev);
  355. }
  356. }
  357. if (ops->msi_finish)
  358. ops->msi_finish(&arg, 0);
  359. can_reserve = msi_check_reservation_mode(domain, info, dev);
  360. for_each_msi_entry(desc, dev) {
  361. virq = desc->irq;
  362. if (desc->nvec_used == 1)
  363. dev_dbg(dev, "irq %d for MSI\n", virq);
  364. else
  365. dev_dbg(dev, "irq [%d-%d] for MSI\n",
  366. virq, virq + desc->nvec_used - 1);
  367. /*
  368. * This flag is set by the PCI layer as we need to activate
  369. * the MSI entries before the PCI layer enables MSI in the
  370. * card. Otherwise the card latches a random msi message.
  371. */
  372. if (!(info->flags & MSI_FLAG_ACTIVATE_EARLY))
  373. continue;
  374. irq_data = irq_domain_get_irq_data(domain, desc->irq);
  375. if (!can_reserve)
  376. irqd_clr_can_reserve(irq_data);
  377. ret = irq_domain_activate_irq(irq_data, can_reserve);
  378. if (ret)
  379. goto cleanup;
  380. }
  381. /*
  382. * If these interrupts use reservation mode, clear the activated bit
  383. * so request_irq() will assign the final vector.
  384. */
  385. if (can_reserve) {
  386. for_each_msi_entry(desc, dev) {
  387. irq_data = irq_domain_get_irq_data(domain, desc->irq);
  388. irqd_clr_activated(irq_data);
  389. }
  390. }
  391. return 0;
  392. cleanup:
  393. for_each_msi_entry(desc, dev) {
  394. struct irq_data *irqd;
  395. if (desc->irq == virq)
  396. break;
  397. irqd = irq_domain_get_irq_data(domain, desc->irq);
  398. if (irqd_is_activated(irqd))
  399. irq_domain_deactivate_irq(irqd);
  400. }
  401. msi_domain_free_irqs(domain, dev);
  402. return ret;
  403. }
  404. /**
  405. * msi_domain_free_irqs - Free interrupts from a MSI interrupt @domain associated tp @dev
  406. * @domain: The domain to managing the interrupts
  407. * @dev: Pointer to device struct of the device for which the interrupts
  408. * are free
  409. */
  410. void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
  411. {
  412. struct msi_desc *desc;
  413. for_each_msi_entry(desc, dev) {
  414. /*
  415. * We might have failed to allocate an MSI early
  416. * enough that there is no IRQ associated to this
  417. * entry. If that's the case, don't do anything.
  418. */
  419. if (desc->irq) {
  420. irq_domain_free_irqs(desc->irq, desc->nvec_used);
  421. desc->irq = 0;
  422. }
  423. }
  424. }
  425. /**
  426. * msi_get_domain_info - Get the MSI interrupt domain info for @domain
  427. * @domain: The interrupt domain to retrieve data from
  428. *
  429. * Returns the pointer to the msi_domain_info stored in
  430. * @domain->host_data.
  431. */
  432. struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain)
  433. {
  434. return (struct msi_domain_info *)domain->host_data;
  435. }
  436. #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */