msi.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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 Signaled Interrupts 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 <linux/sysfs.h>
  18. #include <linux/pci.h>
  19. #include "internals.h"
  20. static inline int msi_sysfs_create_group(struct device *dev);
  21. /**
  22. * msi_alloc_desc - Allocate an initialized msi_desc
  23. * @dev: Pointer to the device for which this is allocated
  24. * @nvec: The number of vectors used in this entry
  25. * @affinity: Optional pointer to an affinity mask array size of @nvec
  26. *
  27. * If @affinity is not %NULL then an affinity array[@nvec] is allocated
  28. * and the affinity masks and flags from @affinity are copied.
  29. *
  30. * Return: pointer to allocated &msi_desc on success or %NULL on failure
  31. */
  32. static struct msi_desc *msi_alloc_desc(struct device *dev, int nvec,
  33. const struct irq_affinity_desc *affinity)
  34. {
  35. struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  36. if (!desc)
  37. return NULL;
  38. desc->dev = dev;
  39. desc->nvec_used = nvec;
  40. if (affinity) {
  41. desc->affinity = kmemdup(affinity, nvec * sizeof(*desc->affinity), GFP_KERNEL);
  42. if (!desc->affinity) {
  43. kfree(desc);
  44. return NULL;
  45. }
  46. }
  47. return desc;
  48. }
  49. static void msi_free_desc(struct msi_desc *desc)
  50. {
  51. kfree(desc->affinity);
  52. kfree(desc);
  53. }
  54. static int msi_insert_desc(struct msi_device_data *md, struct msi_desc *desc, unsigned int index)
  55. {
  56. int ret;
  57. desc->msi_index = index;
  58. ret = xa_insert(&md->__store, index, desc, GFP_KERNEL);
  59. if (ret)
  60. msi_free_desc(desc);
  61. return ret;
  62. }
  63. /**
  64. * msi_add_msi_desc - Allocate and initialize a MSI descriptor
  65. * @dev: Pointer to the device for which the descriptor is allocated
  66. * @init_desc: Pointer to an MSI descriptor to initialize the new descriptor
  67. *
  68. * Return: 0 on success or an appropriate failure code.
  69. */
  70. int msi_add_msi_desc(struct device *dev, struct msi_desc *init_desc)
  71. {
  72. struct msi_desc *desc;
  73. lockdep_assert_held(&dev->msi.data->mutex);
  74. desc = msi_alloc_desc(dev, init_desc->nvec_used, init_desc->affinity);
  75. if (!desc)
  76. return -ENOMEM;
  77. /* Copy type specific data to the new descriptor. */
  78. desc->pci = init_desc->pci;
  79. return msi_insert_desc(dev->msi.data, desc, init_desc->msi_index);
  80. }
  81. /**
  82. * msi_add_simple_msi_descs - Allocate and initialize MSI descriptors
  83. * @dev: Pointer to the device for which the descriptors are allocated
  84. * @index: Index for the first MSI descriptor
  85. * @ndesc: Number of descriptors to allocate
  86. *
  87. * Return: 0 on success or an appropriate failure code.
  88. */
  89. static int msi_add_simple_msi_descs(struct device *dev, unsigned int index, unsigned int ndesc)
  90. {
  91. unsigned int idx, last = index + ndesc - 1;
  92. struct msi_desc *desc;
  93. int ret;
  94. lockdep_assert_held(&dev->msi.data->mutex);
  95. for (idx = index; idx <= last; idx++) {
  96. desc = msi_alloc_desc(dev, 1, NULL);
  97. if (!desc)
  98. goto fail_mem;
  99. ret = msi_insert_desc(dev->msi.data, desc, idx);
  100. if (ret)
  101. goto fail;
  102. }
  103. return 0;
  104. fail_mem:
  105. ret = -ENOMEM;
  106. fail:
  107. msi_free_msi_descs_range(dev, MSI_DESC_NOTASSOCIATED, index, last);
  108. return ret;
  109. }
  110. static bool msi_desc_match(struct msi_desc *desc, enum msi_desc_filter filter)
  111. {
  112. switch (filter) {
  113. case MSI_DESC_ALL:
  114. return true;
  115. case MSI_DESC_NOTASSOCIATED:
  116. return !desc->irq;
  117. case MSI_DESC_ASSOCIATED:
  118. return !!desc->irq;
  119. }
  120. WARN_ON_ONCE(1);
  121. return false;
  122. }
  123. /**
  124. * msi_free_msi_descs_range - Free MSI descriptors of a device
  125. * @dev: Device to free the descriptors
  126. * @filter: Descriptor state filter
  127. * @first_index: Index to start freeing from
  128. * @last_index: Last index to be freed
  129. */
  130. void msi_free_msi_descs_range(struct device *dev, enum msi_desc_filter filter,
  131. unsigned int first_index, unsigned int last_index)
  132. {
  133. struct xarray *xa = &dev->msi.data->__store;
  134. struct msi_desc *desc;
  135. unsigned long idx;
  136. lockdep_assert_held(&dev->msi.data->mutex);
  137. xa_for_each_range(xa, idx, desc, first_index, last_index) {
  138. if (msi_desc_match(desc, filter)) {
  139. xa_erase(xa, idx);
  140. msi_free_desc(desc);
  141. }
  142. }
  143. }
  144. void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
  145. {
  146. *msg = entry->msg;
  147. }
  148. void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
  149. {
  150. struct msi_desc *entry = irq_get_msi_desc(irq);
  151. __get_cached_msi_msg(entry, msg);
  152. }
  153. EXPORT_SYMBOL_GPL(get_cached_msi_msg);
  154. static void msi_device_data_release(struct device *dev, void *res)
  155. {
  156. struct msi_device_data *md = res;
  157. WARN_ON_ONCE(!xa_empty(&md->__store));
  158. xa_destroy(&md->__store);
  159. dev->msi.data = NULL;
  160. }
  161. /**
  162. * msi_setup_device_data - Setup MSI device data
  163. * @dev: Device for which MSI device data should be set up
  164. *
  165. * Return: 0 on success, appropriate error code otherwise
  166. *
  167. * This can be called more than once for @dev. If the MSI device data is
  168. * already allocated the call succeeds. The allocated memory is
  169. * automatically released when the device is destroyed.
  170. */
  171. int msi_setup_device_data(struct device *dev)
  172. {
  173. struct msi_device_data *md;
  174. int ret;
  175. if (dev->msi.data)
  176. return 0;
  177. md = devres_alloc(msi_device_data_release, sizeof(*md), GFP_KERNEL);
  178. if (!md)
  179. return -ENOMEM;
  180. ret = msi_sysfs_create_group(dev);
  181. if (ret) {
  182. devres_free(md);
  183. return ret;
  184. }
  185. xa_init(&md->__store);
  186. mutex_init(&md->mutex);
  187. dev->msi.data = md;
  188. devres_add(dev, md);
  189. return 0;
  190. }
  191. /**
  192. * msi_lock_descs - Lock the MSI descriptor storage of a device
  193. * @dev: Device to operate on
  194. */
  195. void msi_lock_descs(struct device *dev)
  196. {
  197. mutex_lock(&dev->msi.data->mutex);
  198. }
  199. EXPORT_SYMBOL_GPL(msi_lock_descs);
  200. /**
  201. * msi_unlock_descs - Unlock the MSI descriptor storage of a device
  202. * @dev: Device to operate on
  203. */
  204. void msi_unlock_descs(struct device *dev)
  205. {
  206. /* Invalidate the index wich was cached by the iterator */
  207. dev->msi.data->__iter_idx = MSI_MAX_INDEX;
  208. mutex_unlock(&dev->msi.data->mutex);
  209. }
  210. EXPORT_SYMBOL_GPL(msi_unlock_descs);
  211. static struct msi_desc *msi_find_desc(struct msi_device_data *md, enum msi_desc_filter filter)
  212. {
  213. struct msi_desc *desc;
  214. xa_for_each_start(&md->__store, md->__iter_idx, desc, md->__iter_idx) {
  215. if (msi_desc_match(desc, filter))
  216. return desc;
  217. }
  218. md->__iter_idx = MSI_MAX_INDEX;
  219. return NULL;
  220. }
  221. /**
  222. * msi_first_desc - Get the first MSI descriptor of a device
  223. * @dev: Device to operate on
  224. * @filter: Descriptor state filter
  225. *
  226. * Must be called with the MSI descriptor mutex held, i.e. msi_lock_descs()
  227. * must be invoked before the call.
  228. *
  229. * Return: Pointer to the first MSI descriptor matching the search
  230. * criteria, NULL if none found.
  231. */
  232. struct msi_desc *msi_first_desc(struct device *dev, enum msi_desc_filter filter)
  233. {
  234. struct msi_device_data *md = dev->msi.data;
  235. if (WARN_ON_ONCE(!md))
  236. return NULL;
  237. lockdep_assert_held(&md->mutex);
  238. md->__iter_idx = 0;
  239. return msi_find_desc(md, filter);
  240. }
  241. EXPORT_SYMBOL_GPL(msi_first_desc);
  242. /**
  243. * msi_next_desc - Get the next MSI descriptor of a device
  244. * @dev: Device to operate on
  245. *
  246. * The first invocation of msi_next_desc() has to be preceeded by a
  247. * successful invocation of __msi_first_desc(). Consecutive invocations are
  248. * only valid if the previous one was successful. All these operations have
  249. * to be done within the same MSI mutex held region.
  250. *
  251. * Return: Pointer to the next MSI descriptor matching the search
  252. * criteria, NULL if none found.
  253. */
  254. struct msi_desc *msi_next_desc(struct device *dev, enum msi_desc_filter filter)
  255. {
  256. struct msi_device_data *md = dev->msi.data;
  257. if (WARN_ON_ONCE(!md))
  258. return NULL;
  259. lockdep_assert_held(&md->mutex);
  260. if (md->__iter_idx >= (unsigned long)MSI_MAX_INDEX)
  261. return NULL;
  262. md->__iter_idx++;
  263. return msi_find_desc(md, filter);
  264. }
  265. EXPORT_SYMBOL_GPL(msi_next_desc);
  266. /**
  267. * msi_get_virq - Return Linux interrupt number of a MSI interrupt
  268. * @dev: Device to operate on
  269. * @index: MSI interrupt index to look for (0-based)
  270. *
  271. * Return: The Linux interrupt number on success (> 0), 0 if not found
  272. */
  273. unsigned int msi_get_virq(struct device *dev, unsigned int index)
  274. {
  275. struct msi_desc *desc;
  276. unsigned int ret = 0;
  277. bool pcimsi;
  278. if (!dev->msi.data)
  279. return 0;
  280. pcimsi = dev_is_pci(dev) ? to_pci_dev(dev)->msi_enabled : false;
  281. msi_lock_descs(dev);
  282. desc = xa_load(&dev->msi.data->__store, pcimsi ? 0 : index);
  283. if (desc && desc->irq) {
  284. /*
  285. * PCI-MSI has only one descriptor for multiple interrupts.
  286. * PCI-MSIX and platform MSI use a descriptor per
  287. * interrupt.
  288. */
  289. if (pcimsi) {
  290. if (index < desc->nvec_used)
  291. ret = desc->irq + index;
  292. } else {
  293. ret = desc->irq;
  294. }
  295. }
  296. msi_unlock_descs(dev);
  297. return ret;
  298. }
  299. EXPORT_SYMBOL_GPL(msi_get_virq);
  300. #ifdef CONFIG_SYSFS
  301. static struct attribute *msi_dev_attrs[] = {
  302. NULL
  303. };
  304. static const struct attribute_group msi_irqs_group = {
  305. .name = "msi_irqs",
  306. .attrs = msi_dev_attrs,
  307. };
  308. static inline int msi_sysfs_create_group(struct device *dev)
  309. {
  310. return devm_device_add_group(dev, &msi_irqs_group);
  311. }
  312. static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
  313. char *buf)
  314. {
  315. /* MSI vs. MSIX is per device not per interrupt */
  316. bool is_msix = dev_is_pci(dev) ? to_pci_dev(dev)->msix_enabled : false;
  317. return sysfs_emit(buf, "%s\n", is_msix ? "msix" : "msi");
  318. }
  319. static void msi_sysfs_remove_desc(struct device *dev, struct msi_desc *desc)
  320. {
  321. struct device_attribute *attrs = desc->sysfs_attrs;
  322. int i;
  323. if (!attrs)
  324. return;
  325. desc->sysfs_attrs = NULL;
  326. for (i = 0; i < desc->nvec_used; i++) {
  327. if (attrs[i].show)
  328. sysfs_remove_file_from_group(&dev->kobj, &attrs[i].attr, msi_irqs_group.name);
  329. kfree(attrs[i].attr.name);
  330. }
  331. kfree(attrs);
  332. }
  333. static int msi_sysfs_populate_desc(struct device *dev, struct msi_desc *desc)
  334. {
  335. struct device_attribute *attrs;
  336. int ret, i;
  337. attrs = kcalloc(desc->nvec_used, sizeof(*attrs), GFP_KERNEL);
  338. if (!attrs)
  339. return -ENOMEM;
  340. desc->sysfs_attrs = attrs;
  341. for (i = 0; i < desc->nvec_used; i++) {
  342. sysfs_attr_init(&attrs[i].attr);
  343. attrs[i].attr.name = kasprintf(GFP_KERNEL, "%d", desc->irq + i);
  344. if (!attrs[i].attr.name) {
  345. ret = -ENOMEM;
  346. goto fail;
  347. }
  348. attrs[i].attr.mode = 0444;
  349. attrs[i].show = msi_mode_show;
  350. ret = sysfs_add_file_to_group(&dev->kobj, &attrs[i].attr, msi_irqs_group.name);
  351. if (ret) {
  352. attrs[i].show = NULL;
  353. goto fail;
  354. }
  355. }
  356. return 0;
  357. fail:
  358. msi_sysfs_remove_desc(dev, desc);
  359. return ret;
  360. }
  361. #if defined(CONFIG_PCI_MSI_ARCH_FALLBACKS) || defined(CONFIG_PCI_XEN)
  362. /**
  363. * msi_device_populate_sysfs - Populate msi_irqs sysfs entries for a device
  364. * @dev: The device (PCI, platform etc) which will get sysfs entries
  365. */
  366. int msi_device_populate_sysfs(struct device *dev)
  367. {
  368. struct msi_desc *desc;
  369. int ret;
  370. msi_for_each_desc(desc, dev, MSI_DESC_ASSOCIATED) {
  371. if (desc->sysfs_attrs)
  372. continue;
  373. ret = msi_sysfs_populate_desc(dev, desc);
  374. if (ret)
  375. return ret;
  376. }
  377. return 0;
  378. }
  379. /**
  380. * msi_device_destroy_sysfs - Destroy msi_irqs sysfs entries for a device
  381. * @dev: The device (PCI, platform etc) for which to remove
  382. * sysfs entries
  383. */
  384. void msi_device_destroy_sysfs(struct device *dev)
  385. {
  386. struct msi_desc *desc;
  387. msi_for_each_desc(desc, dev, MSI_DESC_ALL)
  388. msi_sysfs_remove_desc(dev, desc);
  389. }
  390. #endif /* CONFIG_PCI_MSI_ARCH_FALLBACK || CONFIG_PCI_XEN */
  391. #else /* CONFIG_SYSFS */
  392. static inline int msi_sysfs_create_group(struct device *dev) { return 0; }
  393. static inline int msi_sysfs_populate_desc(struct device *dev, struct msi_desc *desc) { return 0; }
  394. static inline void msi_sysfs_remove_desc(struct device *dev, struct msi_desc *desc) { }
  395. #endif /* !CONFIG_SYSFS */
  396. #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
  397. static inline void irq_chip_write_msi_msg(struct irq_data *data,
  398. struct msi_msg *msg)
  399. {
  400. data->chip->irq_write_msi_msg(data, msg);
  401. }
  402. static void msi_check_level(struct irq_domain *domain, struct msi_msg *msg)
  403. {
  404. struct msi_domain_info *info = domain->host_data;
  405. /*
  406. * If the MSI provider has messed with the second message and
  407. * not advertized that it is level-capable, signal the breakage.
  408. */
  409. WARN_ON(!((info->flags & MSI_FLAG_LEVEL_CAPABLE) &&
  410. (info->chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)) &&
  411. (msg[1].address_lo || msg[1].address_hi || msg[1].data));
  412. }
  413. /**
  414. * msi_domain_set_affinity - Generic affinity setter function for MSI domains
  415. * @irq_data: The irq data associated to the interrupt
  416. * @mask: The affinity mask to set
  417. * @force: Flag to enforce setting (disable online checks)
  418. *
  419. * Intended to be used by MSI interrupt controllers which are
  420. * implemented with hierarchical domains.
  421. *
  422. * Return: IRQ_SET_MASK_* result code
  423. */
  424. int msi_domain_set_affinity(struct irq_data *irq_data,
  425. const struct cpumask *mask, bool force)
  426. {
  427. struct irq_data *parent = irq_data->parent_data;
  428. struct msi_msg msg[2] = { [1] = { }, };
  429. int ret;
  430. ret = parent->chip->irq_set_affinity(parent, mask, force);
  431. if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
  432. BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
  433. msi_check_level(irq_data->domain, msg);
  434. irq_chip_write_msi_msg(irq_data, msg);
  435. }
  436. return ret;
  437. }
  438. static int msi_domain_activate(struct irq_domain *domain,
  439. struct irq_data *irq_data, bool early)
  440. {
  441. struct msi_msg msg[2] = { [1] = { }, };
  442. BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
  443. msi_check_level(irq_data->domain, msg);
  444. irq_chip_write_msi_msg(irq_data, msg);
  445. return 0;
  446. }
  447. static void msi_domain_deactivate(struct irq_domain *domain,
  448. struct irq_data *irq_data)
  449. {
  450. struct msi_msg msg[2];
  451. memset(msg, 0, sizeof(msg));
  452. irq_chip_write_msi_msg(irq_data, msg);
  453. }
  454. static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
  455. unsigned int nr_irqs, void *arg)
  456. {
  457. struct msi_domain_info *info = domain->host_data;
  458. struct msi_domain_ops *ops = info->ops;
  459. irq_hw_number_t hwirq = ops->get_hwirq(info, arg);
  460. int i, ret;
  461. if (irq_find_mapping(domain, hwirq) > 0)
  462. return -EEXIST;
  463. if (domain->parent) {
  464. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
  465. if (ret < 0)
  466. return ret;
  467. }
  468. for (i = 0; i < nr_irqs; i++) {
  469. ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg);
  470. if (ret < 0) {
  471. if (ops->msi_free) {
  472. for (i--; i > 0; i--)
  473. ops->msi_free(domain, info, virq + i);
  474. }
  475. irq_domain_free_irqs_top(domain, virq, nr_irqs);
  476. return ret;
  477. }
  478. }
  479. return 0;
  480. }
  481. static void msi_domain_free(struct irq_domain *domain, unsigned int virq,
  482. unsigned int nr_irqs)
  483. {
  484. struct msi_domain_info *info = domain->host_data;
  485. int i;
  486. if (info->ops->msi_free) {
  487. for (i = 0; i < nr_irqs; i++)
  488. info->ops->msi_free(domain, info, virq + i);
  489. }
  490. irq_domain_free_irqs_top(domain, virq, nr_irqs);
  491. }
  492. static const struct irq_domain_ops msi_domain_ops = {
  493. .alloc = msi_domain_alloc,
  494. .free = msi_domain_free,
  495. .activate = msi_domain_activate,
  496. .deactivate = msi_domain_deactivate,
  497. };
  498. static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info,
  499. msi_alloc_info_t *arg)
  500. {
  501. return arg->hwirq;
  502. }
  503. static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev,
  504. int nvec, msi_alloc_info_t *arg)
  505. {
  506. memset(arg, 0, sizeof(*arg));
  507. return 0;
  508. }
  509. static void msi_domain_ops_set_desc(msi_alloc_info_t *arg,
  510. struct msi_desc *desc)
  511. {
  512. arg->desc = desc;
  513. }
  514. static int msi_domain_ops_init(struct irq_domain *domain,
  515. struct msi_domain_info *info,
  516. unsigned int virq, irq_hw_number_t hwirq,
  517. msi_alloc_info_t *arg)
  518. {
  519. irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip,
  520. info->chip_data);
  521. if (info->handler && info->handler_name) {
  522. __irq_set_handler(virq, info->handler, 0, info->handler_name);
  523. if (info->handler_data)
  524. irq_set_handler_data(virq, info->handler_data);
  525. }
  526. return 0;
  527. }
  528. static int msi_domain_ops_check(struct irq_domain *domain,
  529. struct msi_domain_info *info,
  530. struct device *dev)
  531. {
  532. return 0;
  533. }
  534. static struct msi_domain_ops msi_domain_ops_default = {
  535. .get_hwirq = msi_domain_ops_get_hwirq,
  536. .msi_init = msi_domain_ops_init,
  537. .msi_check = msi_domain_ops_check,
  538. .msi_prepare = msi_domain_ops_prepare,
  539. .set_desc = msi_domain_ops_set_desc,
  540. .domain_alloc_irqs = __msi_domain_alloc_irqs,
  541. .domain_free_irqs = __msi_domain_free_irqs,
  542. };
  543. static void msi_domain_update_dom_ops(struct msi_domain_info *info)
  544. {
  545. struct msi_domain_ops *ops = info->ops;
  546. if (ops == NULL) {
  547. info->ops = &msi_domain_ops_default;
  548. return;
  549. }
  550. if (ops->domain_alloc_irqs == NULL)
  551. ops->domain_alloc_irqs = msi_domain_ops_default.domain_alloc_irqs;
  552. if (ops->domain_free_irqs == NULL)
  553. ops->domain_free_irqs = msi_domain_ops_default.domain_free_irqs;
  554. if (!(info->flags & MSI_FLAG_USE_DEF_DOM_OPS))
  555. return;
  556. if (ops->get_hwirq == NULL)
  557. ops->get_hwirq = msi_domain_ops_default.get_hwirq;
  558. if (ops->msi_init == NULL)
  559. ops->msi_init = msi_domain_ops_default.msi_init;
  560. if (ops->msi_check == NULL)
  561. ops->msi_check = msi_domain_ops_default.msi_check;
  562. if (ops->msi_prepare == NULL)
  563. ops->msi_prepare = msi_domain_ops_default.msi_prepare;
  564. if (ops->set_desc == NULL)
  565. ops->set_desc = msi_domain_ops_default.set_desc;
  566. }
  567. static void msi_domain_update_chip_ops(struct msi_domain_info *info)
  568. {
  569. struct irq_chip *chip = info->chip;
  570. BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask);
  571. if (!chip->irq_set_affinity)
  572. chip->irq_set_affinity = msi_domain_set_affinity;
  573. }
  574. /**
  575. * msi_create_irq_domain - Create an MSI interrupt domain
  576. * @fwnode: Optional fwnode of the interrupt controller
  577. * @info: MSI domain info
  578. * @parent: Parent irq domain
  579. *
  580. * Return: pointer to the created &struct irq_domain or %NULL on failure
  581. */
  582. struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
  583. struct msi_domain_info *info,
  584. struct irq_domain *parent)
  585. {
  586. struct irq_domain *domain;
  587. msi_domain_update_dom_ops(info);
  588. if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
  589. msi_domain_update_chip_ops(info);
  590. domain = irq_domain_create_hierarchy(parent, IRQ_DOMAIN_FLAG_MSI, 0,
  591. fwnode, &msi_domain_ops, info);
  592. if (domain && !domain->name && info->chip)
  593. domain->name = info->chip->name;
  594. return domain;
  595. }
  596. int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
  597. int nvec, msi_alloc_info_t *arg)
  598. {
  599. struct msi_domain_info *info = domain->host_data;
  600. struct msi_domain_ops *ops = info->ops;
  601. int ret;
  602. ret = ops->msi_check(domain, info, dev);
  603. if (ret == 0)
  604. ret = ops->msi_prepare(domain, dev, nvec, arg);
  605. return ret;
  606. }
  607. int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
  608. int virq_base, int nvec, msi_alloc_info_t *arg)
  609. {
  610. struct msi_domain_info *info = domain->host_data;
  611. struct msi_domain_ops *ops = info->ops;
  612. struct msi_desc *desc;
  613. int ret, virq;
  614. msi_lock_descs(dev);
  615. ret = msi_add_simple_msi_descs(dev, virq_base, nvec);
  616. if (ret)
  617. goto unlock;
  618. for (virq = virq_base; virq < virq_base + nvec; virq++) {
  619. desc = xa_load(&dev->msi.data->__store, virq);
  620. desc->irq = virq;
  621. ops->set_desc(arg, desc);
  622. ret = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
  623. if (ret)
  624. goto fail;
  625. irq_set_msi_desc(virq, desc);
  626. }
  627. msi_unlock_descs(dev);
  628. return 0;
  629. fail:
  630. for (--virq; virq >= virq_base; virq--)
  631. irq_domain_free_irqs_common(domain, virq, 1);
  632. msi_free_msi_descs_range(dev, MSI_DESC_ALL, virq_base, virq_base + nvec - 1);
  633. unlock:
  634. msi_unlock_descs(dev);
  635. return ret;
  636. }
  637. /*
  638. * Carefully check whether the device can use reservation mode. If
  639. * reservation mode is enabled then the early activation will assign a
  640. * dummy vector to the device. If the PCI/MSI device does not support
  641. * masking of the entry then this can result in spurious interrupts when
  642. * the device driver is not absolutely careful. But even then a malfunction
  643. * of the hardware could result in a spurious interrupt on the dummy vector
  644. * and render the device unusable. If the entry can be masked then the core
  645. * logic will prevent the spurious interrupt and reservation mode can be
  646. * used. For now reservation mode is restricted to PCI/MSI.
  647. */
  648. static bool msi_check_reservation_mode(struct irq_domain *domain,
  649. struct msi_domain_info *info,
  650. struct device *dev)
  651. {
  652. struct msi_desc *desc;
  653. switch(domain->bus_token) {
  654. case DOMAIN_BUS_PCI_MSI:
  655. case DOMAIN_BUS_VMD_MSI:
  656. break;
  657. default:
  658. return false;
  659. }
  660. if (!(info->flags & MSI_FLAG_MUST_REACTIVATE))
  661. return false;
  662. if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_ignore_mask)
  663. return false;
  664. /*
  665. * Checking the first MSI descriptor is sufficient. MSIX supports
  666. * masking and MSI does so when the can_mask attribute is set.
  667. */
  668. desc = msi_first_desc(dev, MSI_DESC_ALL);
  669. return desc->pci.msi_attrib.is_msix || desc->pci.msi_attrib.can_mask;
  670. }
  671. static int msi_handle_pci_fail(struct irq_domain *domain, struct msi_desc *desc,
  672. int allocated)
  673. {
  674. switch(domain->bus_token) {
  675. case DOMAIN_BUS_PCI_MSI:
  676. case DOMAIN_BUS_VMD_MSI:
  677. if (IS_ENABLED(CONFIG_PCI_MSI))
  678. break;
  679. fallthrough;
  680. default:
  681. return -ENOSPC;
  682. }
  683. /* Let a failed PCI multi MSI allocation retry */
  684. if (desc->nvec_used > 1)
  685. return 1;
  686. /* If there was a successful allocation let the caller know */
  687. return allocated ? allocated : -ENOSPC;
  688. }
  689. #define VIRQ_CAN_RESERVE 0x01
  690. #define VIRQ_ACTIVATE 0x02
  691. static int msi_init_virq(struct irq_domain *domain, int virq, unsigned int vflags)
  692. {
  693. struct irq_data *irqd = irq_domain_get_irq_data(domain, virq);
  694. int ret;
  695. if (!(vflags & VIRQ_CAN_RESERVE)) {
  696. irqd_clr_can_reserve(irqd);
  697. /*
  698. * If the interrupt is managed but no CPU is available to
  699. * service it, shut it down until better times. Note that
  700. * we only do this on the !RESERVE path as x86 (the only
  701. * architecture using this flag) deals with this in a
  702. * different way by using a catch-all vector.
  703. */
  704. if ((vflags & VIRQ_ACTIVATE) &&
  705. irqd_affinity_is_managed(irqd) &&
  706. !cpumask_intersects(irq_data_get_affinity_mask(irqd),
  707. cpu_online_mask)) {
  708. irqd_set_managed_shutdown(irqd);
  709. return 0;
  710. }
  711. }
  712. if (!(vflags & VIRQ_ACTIVATE))
  713. return 0;
  714. ret = irq_domain_activate_irq(irqd, vflags & VIRQ_CAN_RESERVE);
  715. if (ret)
  716. return ret;
  717. /*
  718. * If the interrupt uses reservation mode, clear the activated bit
  719. * so request_irq() will assign the final vector.
  720. */
  721. if (vflags & VIRQ_CAN_RESERVE)
  722. irqd_clr_activated(irqd);
  723. return 0;
  724. }
  725. int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
  726. int nvec)
  727. {
  728. struct msi_domain_info *info = domain->host_data;
  729. struct msi_domain_ops *ops = info->ops;
  730. msi_alloc_info_t arg = { };
  731. unsigned int vflags = 0;
  732. struct msi_desc *desc;
  733. int allocated = 0;
  734. int i, ret, virq;
  735. ret = msi_domain_prepare_irqs(domain, dev, nvec, &arg);
  736. if (ret)
  737. return ret;
  738. /*
  739. * This flag is set by the PCI layer as we need to activate
  740. * the MSI entries before the PCI layer enables MSI in the
  741. * card. Otherwise the card latches a random msi message.
  742. */
  743. if (info->flags & MSI_FLAG_ACTIVATE_EARLY)
  744. vflags |= VIRQ_ACTIVATE;
  745. /*
  746. * Interrupt can use a reserved vector and will not occupy
  747. * a real device vector until the interrupt is requested.
  748. */
  749. if (msi_check_reservation_mode(domain, info, dev))
  750. vflags |= VIRQ_CAN_RESERVE;
  751. msi_for_each_desc(desc, dev, MSI_DESC_NOTASSOCIATED) {
  752. ops->set_desc(&arg, desc);
  753. virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
  754. dev_to_node(dev), &arg, false,
  755. desc->affinity);
  756. if (virq < 0)
  757. return msi_handle_pci_fail(domain, desc, allocated);
  758. for (i = 0; i < desc->nvec_used; i++) {
  759. irq_set_msi_desc_off(virq, i, desc);
  760. irq_debugfs_copy_devname(virq + i, dev);
  761. ret = msi_init_virq(domain, virq + i, vflags);
  762. if (ret)
  763. return ret;
  764. }
  765. if (info->flags & MSI_FLAG_DEV_SYSFS) {
  766. ret = msi_sysfs_populate_desc(dev, desc);
  767. if (ret)
  768. return ret;
  769. }
  770. allocated++;
  771. }
  772. return 0;
  773. }
  774. static int msi_domain_add_simple_msi_descs(struct msi_domain_info *info,
  775. struct device *dev,
  776. unsigned int num_descs)
  777. {
  778. if (!(info->flags & MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS))
  779. return 0;
  780. return msi_add_simple_msi_descs(dev, 0, num_descs);
  781. }
  782. /**
  783. * msi_domain_alloc_irqs_descs_locked - Allocate interrupts from a MSI interrupt domain
  784. * @domain: The domain to allocate from
  785. * @dev: Pointer to device struct of the device for which the interrupts
  786. * are allocated
  787. * @nvec: The number of interrupts to allocate
  788. *
  789. * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
  790. * pair. Use this for MSI irqdomains which implement their own vector
  791. * allocation/free.
  792. *
  793. * Return: %0 on success or an error code.
  794. */
  795. int msi_domain_alloc_irqs_descs_locked(struct irq_domain *domain, struct device *dev,
  796. int nvec)
  797. {
  798. struct msi_domain_info *info = domain->host_data;
  799. struct msi_domain_ops *ops = info->ops;
  800. int ret;
  801. lockdep_assert_held(&dev->msi.data->mutex);
  802. ret = msi_domain_add_simple_msi_descs(info, dev, nvec);
  803. if (ret)
  804. return ret;
  805. ret = ops->domain_alloc_irqs(domain, dev, nvec);
  806. if (ret)
  807. msi_domain_free_irqs_descs_locked(domain, dev);
  808. return ret;
  809. }
  810. /**
  811. * msi_domain_alloc_irqs - Allocate interrupts from a MSI interrupt domain
  812. * @domain: The domain to allocate from
  813. * @dev: Pointer to device struct of the device for which the interrupts
  814. * are allocated
  815. * @nvec: The number of interrupts to allocate
  816. *
  817. * Return: %0 on success or an error code.
  818. */
  819. int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, int nvec)
  820. {
  821. int ret;
  822. msi_lock_descs(dev);
  823. ret = msi_domain_alloc_irqs_descs_locked(domain, dev, nvec);
  824. msi_unlock_descs(dev);
  825. return ret;
  826. }
  827. void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
  828. {
  829. struct msi_domain_info *info = domain->host_data;
  830. struct irq_data *irqd;
  831. struct msi_desc *desc;
  832. int i;
  833. /* Only handle MSI entries which have an interrupt associated */
  834. msi_for_each_desc(desc, dev, MSI_DESC_ASSOCIATED) {
  835. /* Make sure all interrupts are deactivated */
  836. for (i = 0; i < desc->nvec_used; i++) {
  837. irqd = irq_domain_get_irq_data(domain, desc->irq + i);
  838. if (irqd && irqd_is_activated(irqd))
  839. irq_domain_deactivate_irq(irqd);
  840. }
  841. irq_domain_free_irqs(desc->irq, desc->nvec_used);
  842. if (info->flags & MSI_FLAG_DEV_SYSFS)
  843. msi_sysfs_remove_desc(dev, desc);
  844. desc->irq = 0;
  845. }
  846. }
  847. static void msi_domain_free_msi_descs(struct msi_domain_info *info,
  848. struct device *dev)
  849. {
  850. if (info->flags & MSI_FLAG_FREE_MSI_DESCS)
  851. msi_free_msi_descs(dev);
  852. }
  853. /**
  854. * msi_domain_free_irqs_descs_locked - Free interrupts from a MSI interrupt @domain associated to @dev
  855. * @domain: The domain to managing the interrupts
  856. * @dev: Pointer to device struct of the device for which the interrupts
  857. * are free
  858. *
  859. * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
  860. * pair. Use this for MSI irqdomains which implement their own vector
  861. * allocation.
  862. */
  863. void msi_domain_free_irqs_descs_locked(struct irq_domain *domain, struct device *dev)
  864. {
  865. struct msi_domain_info *info = domain->host_data;
  866. struct msi_domain_ops *ops = info->ops;
  867. lockdep_assert_held(&dev->msi.data->mutex);
  868. ops->domain_free_irqs(domain, dev);
  869. msi_domain_free_msi_descs(info, dev);
  870. }
  871. /**
  872. * msi_domain_free_irqs - Free interrupts from a MSI interrupt @domain associated to @dev
  873. * @domain: The domain to managing the interrupts
  874. * @dev: Pointer to device struct of the device for which the interrupts
  875. * are free
  876. */
  877. void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
  878. {
  879. msi_lock_descs(dev);
  880. msi_domain_free_irqs_descs_locked(domain, dev);
  881. msi_unlock_descs(dev);
  882. }
  883. /**
  884. * msi_get_domain_info - Get the MSI interrupt domain info for @domain
  885. * @domain: The interrupt domain to retrieve data from
  886. *
  887. * Return: the pointer to the msi_domain_info stored in @domain->host_data.
  888. */
  889. struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain)
  890. {
  891. return (struct msi_domain_info *)domain->host_data;
  892. }
  893. #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */