irq.c 8.2 KB

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