irq.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ACPI GSI IRQ layer
  4. *
  5. * Copyright (C) 2015 ARM Ltd.
  6. * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/irq.h>
  10. #include <linux/irqdomain.h>
  11. #include <linux/of.h>
  12. enum acpi_irq_model_id acpi_irq_model;
  13. static struct fwnode_handle *acpi_gsi_domain_id;
  14. /**
  15. * acpi_gsi_to_irq() - Retrieve the linux irq number for a given GSI
  16. * @gsi: GSI IRQ number to map
  17. * @irq: pointer where linux IRQ number is stored
  18. *
  19. * irq location updated with irq value [>0 on success, 0 on failure]
  20. *
  21. * Returns: 0 on success
  22. * -EINVAL on failure
  23. */
  24. int acpi_gsi_to_irq(u32 gsi, unsigned int *irq)
  25. {
  26. struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
  27. DOMAIN_BUS_ANY);
  28. *irq = irq_find_mapping(d, gsi);
  29. /*
  30. * *irq == 0 means no mapping, that should
  31. * be reported as a failure
  32. */
  33. return (*irq > 0) ? 0 : -EINVAL;
  34. }
  35. EXPORT_SYMBOL_GPL(acpi_gsi_to_irq);
  36. /**
  37. * acpi_register_gsi() - Map a GSI to a linux IRQ number
  38. * @dev: device for which IRQ has to be mapped
  39. * @gsi: GSI IRQ number
  40. * @trigger: trigger type of the GSI number to be mapped
  41. * @polarity: polarity of the GSI to be mapped
  42. *
  43. * Returns: a valid linux IRQ number on success
  44. * -EINVAL on failure
  45. */
  46. int acpi_register_gsi(struct device *dev, u32 gsi, int trigger,
  47. int polarity)
  48. {
  49. struct irq_fwspec fwspec;
  50. if (WARN_ON(!acpi_gsi_domain_id)) {
  51. pr_warn("GSI: No registered irqchip, giving up\n");
  52. return -EINVAL;
  53. }
  54. fwspec.fwnode = acpi_gsi_domain_id;
  55. fwspec.param[0] = gsi;
  56. fwspec.param[1] = acpi_dev_get_irq_type(trigger, polarity);
  57. fwspec.param_count = 2;
  58. return irq_create_fwspec_mapping(&fwspec);
  59. }
  60. EXPORT_SYMBOL_GPL(acpi_register_gsi);
  61. /**
  62. * acpi_unregister_gsi() - Free a GSI<->linux IRQ number mapping
  63. * @gsi: GSI IRQ number
  64. */
  65. void acpi_unregister_gsi(u32 gsi)
  66. {
  67. struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
  68. DOMAIN_BUS_ANY);
  69. int irq = irq_find_mapping(d, gsi);
  70. irq_dispose_mapping(irq);
  71. }
  72. EXPORT_SYMBOL_GPL(acpi_unregister_gsi);
  73. /**
  74. * acpi_get_irq_source_fwhandle() - Retrieve fwhandle from IRQ resource source.
  75. * @source: acpi_resource_source to use for the lookup.
  76. *
  77. * Description:
  78. * Retrieve the fwhandle of the device referenced by the given IRQ resource
  79. * source.
  80. *
  81. * Return:
  82. * The referenced device fwhandle or NULL on failure
  83. */
  84. static struct fwnode_handle *
  85. acpi_get_irq_source_fwhandle(const struct acpi_resource_source *source)
  86. {
  87. struct fwnode_handle *result;
  88. struct acpi_device *device;
  89. acpi_handle handle;
  90. acpi_status status;
  91. if (!source->string_length)
  92. return acpi_gsi_domain_id;
  93. status = acpi_get_handle(NULL, source->string_ptr, &handle);
  94. if (WARN_ON(ACPI_FAILURE(status)))
  95. return NULL;
  96. device = acpi_bus_get_acpi_device(handle);
  97. if (WARN_ON(!device))
  98. return NULL;
  99. result = &device->fwnode;
  100. acpi_bus_put_acpi_device(device);
  101. return result;
  102. }
  103. /*
  104. * Context for the resource walk used to lookup IRQ resources.
  105. * Contains a return code, the lookup index, and references to the flags
  106. * and fwspec where the result is returned.
  107. */
  108. struct acpi_irq_parse_one_ctx {
  109. int rc;
  110. unsigned int index;
  111. unsigned long *res_flags;
  112. struct irq_fwspec *fwspec;
  113. };
  114. /**
  115. * acpi_irq_parse_one_match - Handle a matching IRQ resource.
  116. * @fwnode: matching fwnode
  117. * @hwirq: hardware IRQ number
  118. * @triggering: triggering attributes of hwirq
  119. * @polarity: polarity attributes of hwirq
  120. * @polarity: polarity attributes of hwirq
  121. * @shareable: shareable attributes of hwirq
  122. * @ctx: acpi_irq_parse_one_ctx updated by this function
  123. *
  124. * Description:
  125. * Handle a matching IRQ resource by populating the given ctx with
  126. * the information passed.
  127. */
  128. static inline void acpi_irq_parse_one_match(struct fwnode_handle *fwnode,
  129. u32 hwirq, u8 triggering,
  130. u8 polarity, u8 shareable,
  131. struct acpi_irq_parse_one_ctx *ctx)
  132. {
  133. if (!fwnode)
  134. return;
  135. ctx->rc = 0;
  136. *ctx->res_flags = acpi_dev_irq_flags(triggering, polarity, shareable);
  137. ctx->fwspec->fwnode = fwnode;
  138. ctx->fwspec->param[0] = hwirq;
  139. ctx->fwspec->param[1] = acpi_dev_get_irq_type(triggering, polarity);
  140. ctx->fwspec->param_count = 2;
  141. }
  142. /**
  143. * acpi_irq_parse_one_cb - Handle the given resource.
  144. * @ares: resource to handle
  145. * @context: context for the walk
  146. *
  147. * Description:
  148. * This is called by acpi_walk_resources passing each resource returned by
  149. * the _CRS method. We only inspect IRQ resources. Since IRQ resources
  150. * might contain multiple interrupts we check if the index is within this
  151. * one's interrupt array, otherwise we subtract the current resource IRQ
  152. * count from the lookup index to prepare for the next resource.
  153. * Once a match is found we call acpi_irq_parse_one_match to populate
  154. * the result and end the walk by returning AE_CTRL_TERMINATE.
  155. *
  156. * Return:
  157. * AE_OK if the walk should continue, AE_CTRL_TERMINATE if a matching
  158. * IRQ resource was found.
  159. */
  160. static acpi_status acpi_irq_parse_one_cb(struct acpi_resource *ares,
  161. void *context)
  162. {
  163. struct acpi_irq_parse_one_ctx *ctx = context;
  164. struct acpi_resource_irq *irq;
  165. struct acpi_resource_extended_irq *eirq;
  166. struct fwnode_handle *fwnode;
  167. switch (ares->type) {
  168. case ACPI_RESOURCE_TYPE_IRQ:
  169. irq = &ares->data.irq;
  170. if (ctx->index >= irq->interrupt_count) {
  171. ctx->index -= irq->interrupt_count;
  172. return AE_OK;
  173. }
  174. fwnode = acpi_gsi_domain_id;
  175. acpi_irq_parse_one_match(fwnode, irq->interrupts[ctx->index],
  176. irq->triggering, irq->polarity,
  177. irq->shareable, ctx);
  178. return AE_CTRL_TERMINATE;
  179. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  180. eirq = &ares->data.extended_irq;
  181. if (eirq->producer_consumer == ACPI_PRODUCER)
  182. return AE_OK;
  183. if (ctx->index >= eirq->interrupt_count) {
  184. ctx->index -= eirq->interrupt_count;
  185. return AE_OK;
  186. }
  187. fwnode = acpi_get_irq_source_fwhandle(&eirq->resource_source);
  188. acpi_irq_parse_one_match(fwnode, eirq->interrupts[ctx->index],
  189. eirq->triggering, eirq->polarity,
  190. eirq->shareable, ctx);
  191. return AE_CTRL_TERMINATE;
  192. }
  193. return AE_OK;
  194. }
  195. /**
  196. * acpi_irq_parse_one - Resolve an interrupt for a device
  197. * @handle: the device whose interrupt is to be resolved
  198. * @index: index of the interrupt to resolve
  199. * @fwspec: structure irq_fwspec filled by this function
  200. * @flags: resource flags filled by this function
  201. *
  202. * Description:
  203. * Resolves an interrupt for a device by walking its CRS resources to find
  204. * the appropriate ACPI IRQ resource and populating the given struct irq_fwspec
  205. * and flags.
  206. *
  207. * Return:
  208. * The result stored in ctx.rc by the callback, or the default -EINVAL value
  209. * if an error occurs.
  210. */
  211. static int acpi_irq_parse_one(acpi_handle handle, unsigned int index,
  212. struct irq_fwspec *fwspec, unsigned long *flags)
  213. {
  214. struct acpi_irq_parse_one_ctx ctx = { -EINVAL, index, flags, fwspec };
  215. acpi_walk_resources(handle, METHOD_NAME__CRS, acpi_irq_parse_one_cb, &ctx);
  216. return ctx.rc;
  217. }
  218. /**
  219. * acpi_irq_get - Lookup an ACPI IRQ resource and use it to initialize resource.
  220. * @handle: ACPI device handle
  221. * @index: ACPI IRQ resource index to lookup
  222. * @res: Linux IRQ resource to initialize
  223. *
  224. * Description:
  225. * Look for the ACPI IRQ resource with the given index and use it to initialize
  226. * the given Linux IRQ resource.
  227. *
  228. * Return:
  229. * 0 on success
  230. * -EINVAL if an error occurs
  231. * -EPROBE_DEFER if the IRQ lookup/conversion failed
  232. */
  233. int acpi_irq_get(acpi_handle handle, unsigned int index, struct resource *res)
  234. {
  235. struct irq_fwspec fwspec;
  236. struct irq_domain *domain;
  237. unsigned long flags;
  238. int rc;
  239. rc = acpi_irq_parse_one(handle, index, &fwspec, &flags);
  240. if (rc)
  241. return rc;
  242. domain = irq_find_matching_fwnode(fwspec.fwnode, DOMAIN_BUS_ANY);
  243. if (!domain)
  244. return -EPROBE_DEFER;
  245. rc = irq_create_fwspec_mapping(&fwspec);
  246. if (rc <= 0)
  247. return -EINVAL;
  248. res->start = rc;
  249. res->end = rc;
  250. res->flags = flags;
  251. return 0;
  252. }
  253. EXPORT_SYMBOL_GPL(acpi_irq_get);
  254. /**
  255. * acpi_set_irq_model - Setup the GSI irqdomain information
  256. * @model: the value assigned to acpi_irq_model
  257. * @fwnode: the irq_domain identifier for mapping and looking up
  258. * GSI interrupts
  259. */
  260. void __init acpi_set_irq_model(enum acpi_irq_model_id model,
  261. struct fwnode_handle *fwnode)
  262. {
  263. acpi_irq_model = model;
  264. acpi_gsi_domain_id = fwnode;
  265. }
  266. /**
  267. * acpi_irq_create_hierarchy - Create a hierarchical IRQ domain with the default
  268. * GSI domain as its parent.
  269. * @flags: Irq domain flags associated with the domain
  270. * @size: Size of the domain.
  271. * @fwnode: Optional fwnode of the interrupt controller
  272. * @ops: Pointer to the interrupt domain callbacks
  273. * @host_data: Controller private data pointer
  274. */
  275. struct irq_domain *acpi_irq_create_hierarchy(unsigned int flags,
  276. unsigned int size,
  277. struct fwnode_handle *fwnode,
  278. const struct irq_domain_ops *ops,
  279. void *host_data)
  280. {
  281. struct irq_domain *d = irq_find_matching_fwnode(acpi_gsi_domain_id,
  282. DOMAIN_BUS_ANY);
  283. if (!d)
  284. return NULL;
  285. return irq_domain_create_hierarchy(d, flags, size, fwnode, ops,
  286. host_data);
  287. }
  288. EXPORT_SYMBOL_GPL(acpi_irq_create_hierarchy);