msi.c 14 KB

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