irqdomain.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * irq_domain - IRQ translation domains
  3. *
  4. * Translation infrastructure between hw and linux irq numbers. This is
  5. * helpful for interrupt controllers to implement mapping between hardware
  6. * irq numbers and the Linux irq number space.
  7. *
  8. * irq_domains also have hooks for translating device tree or other
  9. * firmware interrupt representations into a hardware irq number that
  10. * can be mapped back to a Linux irq number without any extra platform
  11. * support code.
  12. *
  13. * Interrupt controller "domain" data structure. This could be defined as a
  14. * irq domain controller. That is, it handles the mapping between hardware
  15. * and virtual interrupt numbers for a given interrupt domain. The domain
  16. * structure is generally created by the PIC code for a given PIC instance
  17. * (though a domain can cover more than one PIC if they have a flat number
  18. * model). It's the domain callbacks that are responsible for setting the
  19. * irq_chip on a given irq_desc after it's been mapped.
  20. *
  21. * The host code and data structures use a fwnode_handle pointer to
  22. * identify the domain. In some cases, and in order to preserve source
  23. * code compatibility, this fwnode pointer is "upgraded" to a DT
  24. * device_node. For those firmware infrastructures that do not provide
  25. * a unique identifier for an interrupt controller, the irq_domain
  26. * code offers a fwnode allocator.
  27. */
  28. #ifndef _LINUX_IRQDOMAIN_H
  29. #define _LINUX_IRQDOMAIN_H
  30. #include <linux/types.h>
  31. #include <linux/irqhandler.h>
  32. #include <linux/of.h>
  33. #include <linux/radix-tree.h>
  34. struct device_node;
  35. struct irq_domain;
  36. struct of_device_id;
  37. struct irq_chip;
  38. struct irq_data;
  39. /* Number of irqs reserved for a legacy isa controller */
  40. #define NUM_ISA_INTERRUPTS 16
  41. #define IRQ_DOMAIN_IRQ_SPEC_PARAMS 16
  42. /**
  43. * struct irq_fwspec - generic IRQ specifier structure
  44. *
  45. * @fwnode: Pointer to a firmware-specific descriptor
  46. * @param_count: Number of device-specific parameters
  47. * @param: Device-specific parameters
  48. *
  49. * This structure, directly modeled after of_phandle_args, is used to
  50. * pass a device-specific description of an interrupt.
  51. */
  52. struct irq_fwspec {
  53. struct fwnode_handle *fwnode;
  54. int param_count;
  55. u32 param[IRQ_DOMAIN_IRQ_SPEC_PARAMS];
  56. };
  57. /*
  58. * Should several domains have the same device node, but serve
  59. * different purposes (for example one domain is for PCI/MSI, and the
  60. * other for wired IRQs), they can be distinguished using a
  61. * bus-specific token. Most domains are expected to only carry
  62. * DOMAIN_BUS_ANY.
  63. */
  64. enum irq_domain_bus_token {
  65. DOMAIN_BUS_ANY = 0,
  66. DOMAIN_BUS_PCI_MSI,
  67. DOMAIN_BUS_PLATFORM_MSI,
  68. DOMAIN_BUS_NEXUS,
  69. };
  70. /**
  71. * struct irq_domain_ops - Methods for irq_domain objects
  72. * @match: Match an interrupt controller device node to a host, returns
  73. * 1 on a match
  74. * @map: Create or update a mapping between a virtual irq number and a hw
  75. * irq number. This is called only once for a given mapping.
  76. * @unmap: Dispose of such a mapping
  77. * @xlate: Given a device tree node and interrupt specifier, decode
  78. * the hardware irq number and linux irq type value.
  79. *
  80. * Functions below are provided by the driver and called whenever a new mapping
  81. * is created or an old mapping is disposed. The driver can then proceed to
  82. * whatever internal data structures management is required. It also needs
  83. * to setup the irq_desc when returning from map().
  84. */
  85. struct irq_domain_ops {
  86. int (*match)(struct irq_domain *d, struct device_node *node,
  87. enum irq_domain_bus_token bus_token);
  88. int (*map)(struct irq_domain *d, unsigned int virq, irq_hw_number_t hw);
  89. void (*unmap)(struct irq_domain *d, unsigned int virq);
  90. int (*xlate)(struct irq_domain *d, struct device_node *node,
  91. const u32 *intspec, unsigned int intsize,
  92. unsigned long *out_hwirq, unsigned int *out_type);
  93. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  94. /* extended V2 interfaces to support hierarchy irq_domains */
  95. int (*alloc)(struct irq_domain *d, unsigned int virq,
  96. unsigned int nr_irqs, void *arg);
  97. void (*free)(struct irq_domain *d, unsigned int virq,
  98. unsigned int nr_irqs);
  99. void (*activate)(struct irq_domain *d, struct irq_data *irq_data);
  100. void (*deactivate)(struct irq_domain *d, struct irq_data *irq_data);
  101. int (*translate)(struct irq_domain *d, struct irq_fwspec *fwspec,
  102. unsigned long *out_hwirq, unsigned int *out_type);
  103. #endif
  104. };
  105. extern struct irq_domain_ops irq_generic_chip_ops;
  106. struct irq_domain_chip_generic;
  107. /**
  108. * struct irq_domain - Hardware interrupt number translation object
  109. * @link: Element in global irq_domain list.
  110. * @name: Name of interrupt domain
  111. * @ops: pointer to irq_domain methods
  112. * @host_data: private data pointer for use by owner. Not touched by irq_domain
  113. * core code.
  114. * @flags: host per irq_domain flags
  115. *
  116. * Optional elements
  117. * @of_node: Pointer to device tree nodes associated with the irq_domain. Used
  118. * when decoding device tree interrupt specifiers.
  119. * @gc: Pointer to a list of generic chips. There is a helper function for
  120. * setting up one or more generic chips for interrupt controllers
  121. * drivers using the generic chip library which uses this pointer.
  122. * @parent: Pointer to parent irq_domain to support hierarchy irq_domains
  123. *
  124. * Revmap data, used internally by irq_domain
  125. * @revmap_direct_max_irq: The largest hwirq that can be set for controllers that
  126. * support direct mapping
  127. * @revmap_size: Size of the linear map table @linear_revmap[]
  128. * @revmap_tree: Radix map tree for hwirqs that don't fit in the linear map
  129. * @linear_revmap: Linear table of hwirq->virq reverse mappings
  130. */
  131. struct irq_domain {
  132. struct list_head link;
  133. const char *name;
  134. const struct irq_domain_ops *ops;
  135. void *host_data;
  136. unsigned int flags;
  137. /* Optional data */
  138. struct fwnode_handle *fwnode;
  139. enum irq_domain_bus_token bus_token;
  140. struct irq_domain_chip_generic *gc;
  141. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  142. struct irq_domain *parent;
  143. #endif
  144. /* reverse map data. The linear map gets appended to the irq_domain */
  145. irq_hw_number_t hwirq_max;
  146. unsigned int revmap_direct_max_irq;
  147. unsigned int revmap_size;
  148. struct radix_tree_root revmap_tree;
  149. unsigned int linear_revmap[];
  150. };
  151. /* Irq domain flags */
  152. enum {
  153. /* Irq domain is hierarchical */
  154. IRQ_DOMAIN_FLAG_HIERARCHY = (1 << 0),
  155. /* Core calls alloc/free recursive through the domain hierarchy. */
  156. IRQ_DOMAIN_FLAG_AUTO_RECURSIVE = (1 << 1),
  157. /*
  158. * Flags starting from IRQ_DOMAIN_FLAG_NONCORE are reserved
  159. * for implementation specific purposes and ignored by the
  160. * core code.
  161. */
  162. IRQ_DOMAIN_FLAG_NONCORE = (1 << 16),
  163. };
  164. static inline struct device_node *irq_domain_get_of_node(struct irq_domain *d)
  165. {
  166. return to_of_node(d->fwnode);
  167. }
  168. #ifdef CONFIG_IRQ_DOMAIN
  169. struct fwnode_handle *irq_domain_alloc_fwnode(void *data);
  170. void irq_domain_free_fwnode(struct fwnode_handle *fwnode);
  171. struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
  172. irq_hw_number_t hwirq_max, int direct_max,
  173. const struct irq_domain_ops *ops,
  174. void *host_data);
  175. struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
  176. unsigned int size,
  177. unsigned int first_irq,
  178. const struct irq_domain_ops *ops,
  179. void *host_data);
  180. struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
  181. unsigned int size,
  182. unsigned int first_irq,
  183. irq_hw_number_t first_hwirq,
  184. const struct irq_domain_ops *ops,
  185. void *host_data);
  186. extern struct irq_domain *irq_find_matching_fwnode(struct fwnode_handle *fwnode,
  187. enum irq_domain_bus_token bus_token);
  188. extern void irq_set_default_host(struct irq_domain *host);
  189. static inline struct fwnode_handle *of_node_to_fwnode(struct device_node *node)
  190. {
  191. return node ? &node->fwnode : NULL;
  192. }
  193. static inline struct irq_domain *irq_find_matching_host(struct device_node *node,
  194. enum irq_domain_bus_token bus_token)
  195. {
  196. return irq_find_matching_fwnode(of_node_to_fwnode(node), bus_token);
  197. }
  198. static inline struct irq_domain *irq_find_host(struct device_node *node)
  199. {
  200. return irq_find_matching_host(node, DOMAIN_BUS_ANY);
  201. }
  202. /**
  203. * irq_domain_add_linear() - Allocate and register a linear revmap irq_domain.
  204. * @of_node: pointer to interrupt controller's device tree node.
  205. * @size: Number of interrupts in the domain.
  206. * @ops: map/unmap domain callbacks
  207. * @host_data: Controller private data pointer
  208. */
  209. static inline struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
  210. unsigned int size,
  211. const struct irq_domain_ops *ops,
  212. void *host_data)
  213. {
  214. return __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
  215. }
  216. static inline struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
  217. unsigned int max_irq,
  218. const struct irq_domain_ops *ops,
  219. void *host_data)
  220. {
  221. return __irq_domain_add(of_node_to_fwnode(of_node), 0, max_irq, max_irq, ops, host_data);
  222. }
  223. static inline struct irq_domain *irq_domain_add_legacy_isa(
  224. struct device_node *of_node,
  225. const struct irq_domain_ops *ops,
  226. void *host_data)
  227. {
  228. return irq_domain_add_legacy(of_node, NUM_ISA_INTERRUPTS, 0, 0, ops,
  229. host_data);
  230. }
  231. static inline struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
  232. const struct irq_domain_ops *ops,
  233. void *host_data)
  234. {
  235. return __irq_domain_add(of_node_to_fwnode(of_node), 0, ~0, 0, ops, host_data);
  236. }
  237. static inline struct irq_domain *irq_domain_create_linear(struct fwnode_handle *fwnode,
  238. unsigned int size,
  239. const struct irq_domain_ops *ops,
  240. void *host_data)
  241. {
  242. return __irq_domain_add(fwnode, size, size, 0, ops, host_data);
  243. }
  244. static inline struct irq_domain *irq_domain_create_tree(struct fwnode_handle *fwnode,
  245. const struct irq_domain_ops *ops,
  246. void *host_data)
  247. {
  248. return __irq_domain_add(fwnode, 0, ~0, 0, ops, host_data);
  249. }
  250. extern void irq_domain_remove(struct irq_domain *host);
  251. extern int irq_domain_associate(struct irq_domain *domain, unsigned int irq,
  252. irq_hw_number_t hwirq);
  253. extern void irq_domain_associate_many(struct irq_domain *domain,
  254. unsigned int irq_base,
  255. irq_hw_number_t hwirq_base, int count);
  256. extern void irq_domain_disassociate(struct irq_domain *domain,
  257. unsigned int irq);
  258. extern unsigned int irq_create_mapping(struct irq_domain *host,
  259. irq_hw_number_t hwirq);
  260. extern unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec);
  261. extern void irq_dispose_mapping(unsigned int virq);
  262. /**
  263. * irq_linear_revmap() - Find a linux irq from a hw irq number.
  264. * @domain: domain owning this hardware interrupt
  265. * @hwirq: hardware irq number in that domain space
  266. *
  267. * This is a fast path alternative to irq_find_mapping() that can be
  268. * called directly by irq controller code to save a handful of
  269. * instructions. It is always safe to call, but won't find irqs mapped
  270. * using the radix tree.
  271. */
  272. static inline unsigned int irq_linear_revmap(struct irq_domain *domain,
  273. irq_hw_number_t hwirq)
  274. {
  275. return hwirq < domain->revmap_size ? domain->linear_revmap[hwirq] : 0;
  276. }
  277. extern unsigned int irq_find_mapping(struct irq_domain *host,
  278. irq_hw_number_t hwirq);
  279. extern unsigned int irq_create_direct_mapping(struct irq_domain *host);
  280. extern int irq_create_strict_mappings(struct irq_domain *domain,
  281. unsigned int irq_base,
  282. irq_hw_number_t hwirq_base, int count);
  283. static inline int irq_create_identity_mapping(struct irq_domain *host,
  284. irq_hw_number_t hwirq)
  285. {
  286. return irq_create_strict_mappings(host, hwirq, hwirq, 1);
  287. }
  288. extern const struct irq_domain_ops irq_domain_simple_ops;
  289. /* stock xlate functions */
  290. int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
  291. const u32 *intspec, unsigned int intsize,
  292. irq_hw_number_t *out_hwirq, unsigned int *out_type);
  293. int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
  294. const u32 *intspec, unsigned int intsize,
  295. irq_hw_number_t *out_hwirq, unsigned int *out_type);
  296. int irq_domain_xlate_onetwocell(struct irq_domain *d, struct device_node *ctrlr,
  297. const u32 *intspec, unsigned int intsize,
  298. irq_hw_number_t *out_hwirq, unsigned int *out_type);
  299. /* V2 interfaces to support hierarchy IRQ domains. */
  300. extern struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
  301. unsigned int virq);
  302. extern void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
  303. irq_hw_number_t hwirq, struct irq_chip *chip,
  304. void *chip_data, irq_flow_handler_t handler,
  305. void *handler_data, const char *handler_name);
  306. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  307. extern struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
  308. unsigned int flags, unsigned int size,
  309. struct fwnode_handle *fwnode,
  310. const struct irq_domain_ops *ops, void *host_data);
  311. static inline struct irq_domain *irq_domain_add_hierarchy(struct irq_domain *parent,
  312. unsigned int flags,
  313. unsigned int size,
  314. struct device_node *node,
  315. const struct irq_domain_ops *ops,
  316. void *host_data)
  317. {
  318. return irq_domain_create_hierarchy(parent, flags, size,
  319. of_node_to_fwnode(node),
  320. ops, host_data);
  321. }
  322. extern int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
  323. unsigned int nr_irqs, int node, void *arg,
  324. bool realloc);
  325. extern void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs);
  326. extern void irq_domain_activate_irq(struct irq_data *irq_data);
  327. extern void irq_domain_deactivate_irq(struct irq_data *irq_data);
  328. static inline int irq_domain_alloc_irqs(struct irq_domain *domain,
  329. unsigned int nr_irqs, int node, void *arg)
  330. {
  331. return __irq_domain_alloc_irqs(domain, -1, nr_irqs, node, arg, false);
  332. }
  333. extern int irq_domain_set_hwirq_and_chip(struct irq_domain *domain,
  334. unsigned int virq,
  335. irq_hw_number_t hwirq,
  336. struct irq_chip *chip,
  337. void *chip_data);
  338. extern void irq_domain_reset_irq_data(struct irq_data *irq_data);
  339. extern void irq_domain_free_irqs_common(struct irq_domain *domain,
  340. unsigned int virq,
  341. unsigned int nr_irqs);
  342. extern void irq_domain_free_irqs_top(struct irq_domain *domain,
  343. unsigned int virq, unsigned int nr_irqs);
  344. extern int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
  345. unsigned int irq_base,
  346. unsigned int nr_irqs, void *arg);
  347. extern void irq_domain_free_irqs_parent(struct irq_domain *domain,
  348. unsigned int irq_base,
  349. unsigned int nr_irqs);
  350. static inline bool irq_domain_is_hierarchy(struct irq_domain *domain)
  351. {
  352. return domain->flags & IRQ_DOMAIN_FLAG_HIERARCHY;
  353. }
  354. #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
  355. static inline void irq_domain_activate_irq(struct irq_data *data) { }
  356. static inline void irq_domain_deactivate_irq(struct irq_data *data) { }
  357. static inline int irq_domain_alloc_irqs(struct irq_domain *domain,
  358. unsigned int nr_irqs, int node, void *arg)
  359. {
  360. return -1;
  361. }
  362. static inline bool irq_domain_is_hierarchy(struct irq_domain *domain)
  363. {
  364. return false;
  365. }
  366. #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
  367. #else /* CONFIG_IRQ_DOMAIN */
  368. static inline void irq_dispose_mapping(unsigned int virq) { }
  369. static inline void irq_domain_activate_irq(struct irq_data *data) { }
  370. static inline void irq_domain_deactivate_irq(struct irq_data *data) { }
  371. #endif /* !CONFIG_IRQ_DOMAIN */
  372. #endif /* _LINUX_IRQDOMAIN_H */