irq-loongson-pch-msi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020, Jiaxun Yang <[email protected]>
  4. * Loongson PCH MSI support
  5. */
  6. #define pr_fmt(fmt) "pch-msi: " fmt
  7. #include <linux/irqchip.h>
  8. #include <linux/msi.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/of_pci.h>
  13. #include <linux/pci.h>
  14. #include <linux/slab.h>
  15. static int nr_pics;
  16. struct pch_msi_data {
  17. struct mutex msi_map_lock;
  18. phys_addr_t doorbell;
  19. u32 irq_first; /* The vector number that MSIs starts */
  20. u32 num_irqs; /* The number of vectors for MSIs */
  21. unsigned long *msi_map;
  22. };
  23. static struct fwnode_handle *pch_msi_handle[MAX_IO_PICS];
  24. static void pch_msi_mask_msi_irq(struct irq_data *d)
  25. {
  26. pci_msi_mask_irq(d);
  27. irq_chip_mask_parent(d);
  28. }
  29. static void pch_msi_unmask_msi_irq(struct irq_data *d)
  30. {
  31. irq_chip_unmask_parent(d);
  32. pci_msi_unmask_irq(d);
  33. }
  34. static struct irq_chip pch_msi_irq_chip = {
  35. .name = "PCH PCI MSI",
  36. .irq_mask = pch_msi_mask_msi_irq,
  37. .irq_unmask = pch_msi_unmask_msi_irq,
  38. .irq_ack = irq_chip_ack_parent,
  39. .irq_set_affinity = irq_chip_set_affinity_parent,
  40. };
  41. static int pch_msi_allocate_hwirq(struct pch_msi_data *priv, int num_req)
  42. {
  43. int first;
  44. mutex_lock(&priv->msi_map_lock);
  45. first = bitmap_find_free_region(priv->msi_map, priv->num_irqs,
  46. get_count_order(num_req));
  47. if (first < 0) {
  48. mutex_unlock(&priv->msi_map_lock);
  49. return -ENOSPC;
  50. }
  51. mutex_unlock(&priv->msi_map_lock);
  52. return priv->irq_first + first;
  53. }
  54. static void pch_msi_free_hwirq(struct pch_msi_data *priv,
  55. int hwirq, int num_req)
  56. {
  57. int first = hwirq - priv->irq_first;
  58. mutex_lock(&priv->msi_map_lock);
  59. bitmap_release_region(priv->msi_map, first, get_count_order(num_req));
  60. mutex_unlock(&priv->msi_map_lock);
  61. }
  62. static void pch_msi_compose_msi_msg(struct irq_data *data,
  63. struct msi_msg *msg)
  64. {
  65. struct pch_msi_data *priv = irq_data_get_irq_chip_data(data);
  66. msg->address_hi = upper_32_bits(priv->doorbell);
  67. msg->address_lo = lower_32_bits(priv->doorbell);
  68. msg->data = data->hwirq;
  69. }
  70. static struct msi_domain_info pch_msi_domain_info = {
  71. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  72. MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX,
  73. .chip = &pch_msi_irq_chip,
  74. };
  75. static struct irq_chip middle_irq_chip = {
  76. .name = "PCH MSI",
  77. .irq_mask = irq_chip_mask_parent,
  78. .irq_unmask = irq_chip_unmask_parent,
  79. .irq_ack = irq_chip_ack_parent,
  80. .irq_set_affinity = irq_chip_set_affinity_parent,
  81. .irq_compose_msi_msg = pch_msi_compose_msi_msg,
  82. };
  83. static int pch_msi_parent_domain_alloc(struct irq_domain *domain,
  84. unsigned int virq, int hwirq)
  85. {
  86. struct irq_fwspec fwspec;
  87. fwspec.fwnode = domain->parent->fwnode;
  88. fwspec.param_count = 1;
  89. fwspec.param[0] = hwirq;
  90. return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  91. }
  92. static int pch_msi_middle_domain_alloc(struct irq_domain *domain,
  93. unsigned int virq,
  94. unsigned int nr_irqs, void *args)
  95. {
  96. struct pch_msi_data *priv = domain->host_data;
  97. int hwirq, err, i;
  98. hwirq = pch_msi_allocate_hwirq(priv, nr_irqs);
  99. if (hwirq < 0)
  100. return hwirq;
  101. for (i = 0; i < nr_irqs; i++) {
  102. err = pch_msi_parent_domain_alloc(domain, virq + i, hwirq + i);
  103. if (err)
  104. goto err_hwirq;
  105. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  106. &middle_irq_chip, priv);
  107. }
  108. return 0;
  109. err_hwirq:
  110. pch_msi_free_hwirq(priv, hwirq, nr_irqs);
  111. irq_domain_free_irqs_parent(domain, virq, i - 1);
  112. return err;
  113. }
  114. static void pch_msi_middle_domain_free(struct irq_domain *domain,
  115. unsigned int virq,
  116. unsigned int nr_irqs)
  117. {
  118. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  119. struct pch_msi_data *priv = irq_data_get_irq_chip_data(d);
  120. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  121. pch_msi_free_hwirq(priv, d->hwirq, nr_irqs);
  122. }
  123. static const struct irq_domain_ops pch_msi_middle_domain_ops = {
  124. .alloc = pch_msi_middle_domain_alloc,
  125. .free = pch_msi_middle_domain_free,
  126. };
  127. static int pch_msi_init_domains(struct pch_msi_data *priv,
  128. struct irq_domain *parent,
  129. struct fwnode_handle *domain_handle)
  130. {
  131. struct irq_domain *middle_domain, *msi_domain;
  132. middle_domain = irq_domain_create_linear(domain_handle,
  133. priv->num_irqs,
  134. &pch_msi_middle_domain_ops,
  135. priv);
  136. if (!middle_domain) {
  137. pr_err("Failed to create the MSI middle domain\n");
  138. return -ENOMEM;
  139. }
  140. middle_domain->parent = parent;
  141. irq_domain_update_bus_token(middle_domain, DOMAIN_BUS_NEXUS);
  142. msi_domain = pci_msi_create_irq_domain(domain_handle,
  143. &pch_msi_domain_info,
  144. middle_domain);
  145. if (!msi_domain) {
  146. pr_err("Failed to create PCI MSI domain\n");
  147. irq_domain_remove(middle_domain);
  148. return -ENOMEM;
  149. }
  150. return 0;
  151. }
  152. static int pch_msi_init(phys_addr_t msg_address, int irq_base, int irq_count,
  153. struct irq_domain *parent_domain, struct fwnode_handle *domain_handle)
  154. {
  155. int ret;
  156. struct pch_msi_data *priv;
  157. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  158. if (!priv)
  159. return -ENOMEM;
  160. mutex_init(&priv->msi_map_lock);
  161. priv->doorbell = msg_address;
  162. priv->irq_first = irq_base;
  163. priv->num_irqs = irq_count;
  164. priv->msi_map = bitmap_zalloc(priv->num_irqs, GFP_KERNEL);
  165. if (!priv->msi_map)
  166. goto err_priv;
  167. pr_debug("Registering %d MSIs, starting at %d\n",
  168. priv->num_irqs, priv->irq_first);
  169. ret = pch_msi_init_domains(priv, parent_domain, domain_handle);
  170. if (ret)
  171. goto err_map;
  172. pch_msi_handle[nr_pics++] = domain_handle;
  173. return 0;
  174. err_map:
  175. bitmap_free(priv->msi_map);
  176. err_priv:
  177. kfree(priv);
  178. return -EINVAL;
  179. }
  180. #ifdef CONFIG_OF
  181. static int pch_msi_of_init(struct device_node *node, struct device_node *parent)
  182. {
  183. int err;
  184. int irq_base, irq_count;
  185. struct resource res;
  186. struct irq_domain *parent_domain;
  187. parent_domain = irq_find_host(parent);
  188. if (!parent_domain) {
  189. pr_err("Failed to find the parent domain\n");
  190. return -ENXIO;
  191. }
  192. if (of_address_to_resource(node, 0, &res)) {
  193. pr_err("Failed to allocate resource\n");
  194. return -EINVAL;
  195. }
  196. if (of_property_read_u32(node, "loongson,msi-base-vec", &irq_base)) {
  197. pr_err("Unable to parse MSI vec base\n");
  198. return -EINVAL;
  199. }
  200. if (of_property_read_u32(node, "loongson,msi-num-vecs", &irq_count)) {
  201. pr_err("Unable to parse MSI vec number\n");
  202. return -EINVAL;
  203. }
  204. err = pch_msi_init(res.start, irq_base, irq_count, parent_domain, of_node_to_fwnode(node));
  205. if (err < 0)
  206. return err;
  207. return 0;
  208. }
  209. IRQCHIP_DECLARE(pch_msi, "loongson,pch-msi-1.0", pch_msi_of_init);
  210. #endif
  211. #ifdef CONFIG_ACPI
  212. struct fwnode_handle *get_pch_msi_handle(int pci_segment)
  213. {
  214. int i;
  215. for (i = 0; i < MAX_IO_PICS; i++) {
  216. if (msi_group[i].pci_segment == pci_segment)
  217. return pch_msi_handle[i];
  218. }
  219. return NULL;
  220. }
  221. int __init pch_msi_acpi_init(struct irq_domain *parent,
  222. struct acpi_madt_msi_pic *acpi_pchmsi)
  223. {
  224. int ret;
  225. struct fwnode_handle *domain_handle;
  226. domain_handle = irq_domain_alloc_fwnode(&acpi_pchmsi->msg_address);
  227. ret = pch_msi_init(acpi_pchmsi->msg_address, acpi_pchmsi->start,
  228. acpi_pchmsi->count, parent, domain_handle);
  229. if (ret < 0)
  230. irq_domain_free_fwnode(domain_handle);
  231. return ret;
  232. }
  233. #endif