irq.c 9.9 KB

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