platform-msi.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MSI framework for platform devices
  4. *
  5. * Copyright (C) 2015 ARM Limited, All Rights Reserved.
  6. * Author: Marc Zyngier <marc.zyngier@arm.com>
  7. * Copyright (C) 2022 Linutronix GmbH
  8. */
  9. #include <linux/device.h>
  10. #include <linux/irqdomain.h>
  11. #include <linux/msi.h>
  12. /*
  13. * This indirection can go when platform_device_msi_init_and_alloc_irqs()
  14. * is switched to a proper irq_chip::irq_write_msi_msg() callback. Keep it
  15. * simple for now.
  16. */
  17. static void platform_msi_write_msi_msg(struct irq_data *d, struct msi_msg *msg)
  18. {
  19. irq_write_msi_msg_t cb = d->chip_data;
  20. cb(irq_data_get_msi_desc(d), msg);
  21. }
  22. static void platform_msi_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  23. {
  24. arg->desc = desc;
  25. arg->hwirq = desc->msi_index;
  26. }
  27. static const struct msi_domain_template platform_msi_template = {
  28. .chip = {
  29. .name = "pMSI",
  30. .irq_mask = irq_chip_mask_parent,
  31. .irq_unmask = irq_chip_unmask_parent,
  32. .irq_write_msi_msg = platform_msi_write_msi_msg,
  33. /* The rest is filled in by the platform MSI parent */
  34. },
  35. .ops = {
  36. .set_desc = platform_msi_set_desc,
  37. },
  38. .info = {
  39. .bus_token = DOMAIN_BUS_DEVICE_MSI,
  40. },
  41. };
  42. /**
  43. * platform_device_msi_init_and_alloc_irqs - Initialize platform device MSI
  44. * and allocate interrupts for @dev
  45. * @dev: The device for which to allocate interrupts
  46. * @nvec: The number of interrupts to allocate
  47. * @write_msi_msg: Callback to write an interrupt message for @dev
  48. *
  49. * Returns:
  50. * Zero for success, or an error code in case of failure
  51. *
  52. * This creates a MSI domain on @dev which has @dev->msi.domain as
  53. * parent. The parent domain sets up the new domain. The domain has
  54. * a fixed size of @nvec. The domain is managed by devres and will
  55. * be removed when the device is removed.
  56. *
  57. * Note: For migration purposes this falls back to the original platform_msi code
  58. * up to the point where all platforms have been converted to the MSI
  59. * parent model.
  60. */
  61. int platform_device_msi_init_and_alloc_irqs(struct device *dev, unsigned int nvec,
  62. irq_write_msi_msg_t write_msi_msg)
  63. {
  64. struct irq_domain *domain = dev->msi.domain;
  65. if (!domain || !write_msi_msg)
  66. return -EINVAL;
  67. /*
  68. * @write_msi_msg is stored in the resulting msi_domain_info::data.
  69. * The underlying domain creation mechanism will assign that
  70. * callback to the resulting irq chip.
  71. */
  72. if (!msi_create_device_irq_domain(dev, MSI_DEFAULT_DOMAIN,
  73. &platform_msi_template,
  74. nvec, NULL, write_msi_msg))
  75. return -ENODEV;
  76. return msi_domain_alloc_irqs_range(dev, MSI_DEFAULT_DOMAIN, 0, nvec - 1);
  77. }
  78. EXPORT_SYMBOL_GPL(platform_device_msi_init_and_alloc_irqs);
  79. /**
  80. * platform_device_msi_free_irqs_all - Free all interrupts for @dev
  81. * @dev: The device for which to free interrupts
  82. */
  83. void platform_device_msi_free_irqs_all(struct device *dev)
  84. {
  85. msi_domain_free_irqs_all(dev, MSI_DEFAULT_DOMAIN);
  86. msi_remove_device_irq_domain(dev, MSI_DEFAULT_DOMAIN);
  87. }
  88. EXPORT_SYMBOL_GPL(platform_device_msi_free_irqs_all);