ti_sci_inta_msi.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Texas Instruments' K3 Interrupt Aggregator MSI bus
  4. *
  5. * Copyright (C) 2018-2019 Texas Instruments Incorporated - http://www.ti.com/
  6. * Lokesh Vutla <lokeshvutla@ti.com>
  7. */
  8. #include <linux/irq.h>
  9. #include <linux/irqdomain.h>
  10. #include <linux/msi.h>
  11. #include <linux/of_address.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/soc/ti/ti_sci_inta_msi.h>
  15. #include <linux/soc/ti/ti_sci_protocol.h>
  16. static void ti_sci_inta_msi_write_msg(struct irq_data *data,
  17. struct msi_msg *msg)
  18. {
  19. /* Nothing to do */
  20. }
  21. static void ti_sci_inta_msi_compose_msi_msg(struct irq_data *data,
  22. struct msi_msg *msg)
  23. {
  24. /* Nothing to do */
  25. }
  26. static void ti_sci_inta_msi_update_chip_ops(struct msi_domain_info *info)
  27. {
  28. struct irq_chip *chip = info->chip;
  29. if (WARN_ON(!chip))
  30. return;
  31. chip->irq_request_resources = irq_chip_request_resources_parent;
  32. chip->irq_release_resources = irq_chip_release_resources_parent;
  33. chip->irq_compose_msi_msg = ti_sci_inta_msi_compose_msi_msg;
  34. chip->irq_write_msi_msg = ti_sci_inta_msi_write_msg;
  35. chip->irq_set_type = irq_chip_set_type_parent;
  36. chip->irq_unmask = irq_chip_unmask_parent;
  37. chip->irq_mask = irq_chip_mask_parent;
  38. chip->irq_ack = irq_chip_ack_parent;
  39. }
  40. struct irq_domain *ti_sci_inta_msi_create_irq_domain(struct fwnode_handle *fwnode,
  41. struct msi_domain_info *info,
  42. struct irq_domain *parent)
  43. {
  44. struct irq_domain *domain;
  45. ti_sci_inta_msi_update_chip_ops(info);
  46. domain = msi_create_irq_domain(fwnode, info, parent);
  47. if (domain)
  48. irq_domain_update_bus_token(domain, DOMAIN_BUS_TI_SCI_INTA_MSI);
  49. return domain;
  50. }
  51. EXPORT_SYMBOL_GPL(ti_sci_inta_msi_create_irq_domain);
  52. static void ti_sci_inta_msi_free_descs(struct device *dev)
  53. {
  54. struct msi_desc *desc, *tmp;
  55. list_for_each_entry_safe(desc, tmp, dev_to_msi_list(dev), list) {
  56. list_del(&desc->list);
  57. free_msi_entry(desc);
  58. }
  59. }
  60. static int ti_sci_inta_msi_alloc_descs(struct device *dev,
  61. struct ti_sci_resource *res)
  62. {
  63. struct msi_desc *msi_desc;
  64. int set, i, count = 0;
  65. for (set = 0; set < res->sets; set++) {
  66. for (i = 0; i < res->desc[set].num; i++) {
  67. msi_desc = alloc_msi_entry(dev, 1, NULL);
  68. if (!msi_desc) {
  69. ti_sci_inta_msi_free_descs(dev);
  70. return -ENOMEM;
  71. }
  72. msi_desc->inta.dev_index = res->desc[set].start + i;
  73. INIT_LIST_HEAD(&msi_desc->list);
  74. list_add_tail(&msi_desc->list, dev_to_msi_list(dev));
  75. count++;
  76. }
  77. }
  78. return count;
  79. }
  80. int ti_sci_inta_msi_domain_alloc_irqs(struct device *dev,
  81. struct ti_sci_resource *res)
  82. {
  83. struct platform_device *pdev = to_platform_device(dev);
  84. struct irq_domain *msi_domain;
  85. int ret, nvec;
  86. msi_domain = dev_get_msi_domain(dev);
  87. if (!msi_domain)
  88. return -EINVAL;
  89. if (pdev->id < 0)
  90. return -ENODEV;
  91. nvec = ti_sci_inta_msi_alloc_descs(dev, res);
  92. if (nvec <= 0)
  93. return nvec;
  94. ret = msi_domain_alloc_irqs(msi_domain, dev, nvec);
  95. if (ret) {
  96. dev_err(dev, "Failed to allocate IRQs %d\n", ret);
  97. goto cleanup;
  98. }
  99. return 0;
  100. cleanup:
  101. ti_sci_inta_msi_free_descs(&pdev->dev);
  102. return ret;
  103. }
  104. EXPORT_SYMBOL_GPL(ti_sci_inta_msi_domain_alloc_irqs);
  105. void ti_sci_inta_msi_domain_free_irqs(struct device *dev)
  106. {
  107. msi_domain_free_irqs(dev->msi_domain, dev);
  108. ti_sci_inta_msi_free_descs(dev);
  109. }
  110. EXPORT_SYMBOL_GPL(ti_sci_inta_msi_domain_free_irqs);
  111. unsigned int ti_sci_inta_msi_get_virq(struct device *dev, u32 dev_index)
  112. {
  113. struct msi_desc *desc;
  114. for_each_msi_entry(desc, dev)
  115. if (desc->inta.dev_index == dev_index)
  116. return desc->irq;
  117. return -ENODEV;
  118. }
  119. EXPORT_SYMBOL_GPL(ti_sci_inta_msi_get_virq);