irqdomain.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. struct cpumask;
  40. /* Number of irqs reserved for a legacy isa controller */
  41. #define NUM_ISA_INTERRUPTS 16
  42. #define IRQ_DOMAIN_IRQ_SPEC_PARAMS 16
  43. /**
  44. * struct irq_fwspec - generic IRQ specifier structure
  45. *
  46. * @fwnode: Pointer to a firmware-specific descriptor
  47. * @param_count: Number of device-specific parameters
  48. * @param: Device-specific parameters
  49. *
  50. * This structure, directly modeled after of_phandle_args, is used to
  51. * pass a device-specific description of an interrupt.
  52. */
  53. struct irq_fwspec {
  54. struct fwnode_handle *fwnode;
  55. int param_count;
  56. u32 param[IRQ_DOMAIN_IRQ_SPEC_PARAMS];
  57. };
  58. /*
  59. * Should several domains have the same device node, but serve
  60. * different purposes (for example one domain is for PCI/MSI, and the
  61. * other for wired IRQs), they can be distinguished using a
  62. * bus-specific token. Most domains are expected to only carry
  63. * DOMAIN_BUS_ANY.
  64. */
  65. enum irq_domain_bus_token {
  66. DOMAIN_BUS_ANY = 0,
  67. DOMAIN_BUS_WIRED,
  68. DOMAIN_BUS_PCI_MSI,
  69. DOMAIN_BUS_PLATFORM_MSI,
  70. DOMAIN_BUS_NEXUS,
  71. DOMAIN_BUS_IPI,
  72. DOMAIN_BUS_FSL_MC_MSI,
  73. };
  74. /**
  75. * struct irq_domain_ops - Methods for irq_domain objects
  76. * @match: Match an interrupt controller device node to a host, returns
  77. * 1 on a match
  78. * @map: Create or update a mapping between a virtual irq number and a hw
  79. * irq number. This is called only once for a given mapping.
  80. * @unmap: Dispose of such a mapping
  81. * @xlate: Given a device tree node and interrupt specifier, decode
  82. * the hardware irq number and linux irq type value.
  83. *
  84. * Functions below are provided by the driver and called whenever a new mapping
  85. * is created or an old mapping is disposed. The driver can then proceed to
  86. * whatever internal data structures management is required. It also needs
  87. * to setup the irq_desc when returning from map().
  88. */
  89. struct irq_domain_ops {
  90. int (*match)(struct irq_domain *d, struct device_node *node,
  91. enum irq_domain_bus_token bus_token);
  92. int (*select)(struct irq_domain *d, struct irq_fwspec *fwspec,
  93. enum irq_domain_bus_token bus_token);
  94. int (*map)(struct irq_domain *d, unsigned int virq, irq_hw_number_t hw);
  95. void (*unmap)(struct irq_domain *d, unsigned int virq);
  96. int (*xlate)(struct irq_domain *d, struct device_node *node,
  97. const u32 *intspec, unsigned int intsize,
  98. unsigned long *out_hwirq, unsigned int *out_type);
  99. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  100. /* extended V2 interfaces to support hierarchy irq_domains */
  101. int (*alloc)(struct irq_domain *d, unsigned int virq,
  102. unsigned int nr_irqs, void *arg);
  103. void (*free)(struct irq_domain *d, unsigned int virq,
  104. unsigned int nr_irqs);
  105. void (*activate)(struct irq_domain *d, struct irq_data *irq_data);
  106. void (*deactivate)(struct irq_domain *d, struct irq_data *irq_data);
  107. int (*translate)(struct irq_domain *d, struct irq_fwspec *fwspec,
  108. unsigned long *out_hwirq, unsigned int *out_type);
  109. #endif
  110. };
  111. extern struct irq_domain_ops irq_generic_chip_ops;
  112. struct irq_domain_chip_generic;
  113. /**
  114. * struct irq_domain - Hardware interrupt number translation object
  115. * @link: Element in global irq_domain list.
  116. * @name: Name of interrupt domain
  117. * @ops: pointer to irq_domain methods
  118. * @host_data: private data pointer for use by owner. Not touched by irq_domain
  119. * core code.
  120. * @flags: host per irq_domain flags
  121. *
  122. * Optional elements
  123. * @of_node: Pointer to device tree nodes associated with the irq_domain. Used
  124. * when decoding device tree interrupt specifiers.
  125. * @gc: Pointer to a list of generic chips. There is a helper function for
  126. * setting up one or more generic chips for interrupt controllers
  127. * drivers using the generic chip library which uses this pointer.
  128. * @parent: Pointer to parent irq_domain to support hierarchy irq_domains
  129. *
  130. * Revmap data, used internally by irq_domain
  131. * @revmap_direct_max_irq: The largest hwirq that can be set for controllers that
  132. * support direct mapping
  133. * @revmap_size: Size of the linear map table @linear_revmap[]
  134. * @revmap_tree: Radix map tree for hwirqs that don't fit in the linear map
  135. * @linear_revmap: Linear table of hwirq->virq reverse mappings
  136. */
  137. struct irq_domain {
  138. struct list_head link;
  139. const char *name;
  140. const struct irq_domain_ops *ops;
  141. void *host_data;
  142. unsigned int flags;
  143. /* Optional data */
  144. struct fwnode_handle *fwnode;
  145. enum irq_domain_bus_token bus_token;
  146. struct irq_domain_chip_generic *gc;
  147. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  148. struct irq_domain *parent;
  149. #endif
  150. /* reverse map data. The linear map gets appended to the irq_domain */
  151. irq_hw_number_t hwirq_max;
  152. unsigned int revmap_direct_max_irq;
  153. unsigned int revmap_size;
  154. struct radix_tree_root revmap_tree;
  155. unsigned int linear_revmap[];
  156. };
  157. /* Irq domain flags */
  158. enum {
  159. /* Irq domain is hierarchical */
  160. IRQ_DOMAIN_FLAG_HIERARCHY = (1 << 0),
  161. /* Core calls alloc/free recursive through the domain hierarchy. */
  162. IRQ_DOMAIN_FLAG_AUTO_RECURSIVE = (1 << 1),
  163. /* Irq domain is an IPI domain with virq per cpu */
  164. IRQ_DOMAIN_FLAG_IPI_PER_CPU = (1 << 2),
  165. /* Irq domain is an IPI domain with single virq */
  166. IRQ_DOMAIN_FLAG_IPI_SINGLE = (1 << 3),
  167. /*
  168. * Flags starting from IRQ_DOMAIN_FLAG_NONCORE are reserved
  169. * for implementation specific purposes and ignored by the
  170. * core code.
  171. */
  172. IRQ_DOMAIN_FLAG_NONCORE = (1 << 16),
  173. };
  174. static inline struct device_node *irq_domain_get_of_node(struct irq_domain *d)
  175. {
  176. return to_of_node(d->fwnode);
  177. }
  178. #ifdef CONFIG_IRQ_DOMAIN
  179. struct fwnode_handle *irq_domain_alloc_fwnode(void *data);
  180. void irq_domain_free_fwnode(struct fwnode_handle *fwnode);
  181. struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
  182. irq_hw_number_t hwirq_max, int direct_max,
  183. const struct irq_domain_ops *ops,
  184. void *host_data);
  185. struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
  186. unsigned int size,
  187. unsigned int first_irq,
  188. const struct irq_domain_ops *ops,
  189. void *host_data);
  190. struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
  191. unsigned int size,
  192. unsigned int first_irq,
  193. irq_hw_number_t first_hwirq,
  194. const struct irq_domain_ops *ops,
  195. void *host_data);
  196. extern struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
  197. enum irq_domain_bus_token bus_token);
  198. extern void irq_set_default_host(struct irq_domain *host);
  199. extern int irq_domain_alloc_descs(int virq, unsigned int nr_irqs,
  200. irq_hw_number_t hwirq, int node,
  201. const struct cpumask *affinity);
  202. static inline struct fwnode_handle *of_node_to_fwnode(struct device_node *node)
  203. {
  204. return node ? &node->fwnode : NULL;
  205. }
  206. static inline bool is_fwnode_irqchip(struct fwnode_handle *fwnode)
  207. {
  208. return fwnode && fwnode->type == FWNODE_IRQCHIP;
  209. }
  210. static inline
  211. struct irq_domain *irq_find_matching_fwnode(struct fwnode_handle *fwnode,
  212. enum irq_domain_bus_token bus_token)
  213. {
  214. struct irq_fwspec fwspec = {
  215. .fwnode = fwnode,
  216. };
  217. return irq_find_matching_fwspec(&fwspec, bus_token);
  218. }
  219. static inline struct irq_domain *irq_find_matching_host(struct device_node *node,
  220. enum irq_domain_bus_token bus_token)
  221. {
  222. return irq_find_matching_fwnode(of_node_to_fwnode(node), bus_token);
  223. }
  224. static inline struct irq_domain *irq_find_host(struct device_node *node)
  225. {
  226. return irq_find_matching_host(node, DOMAIN_BUS_ANY);
  227. }
  228. /**
  229. * irq_domain_add_linear() - Allocate and register a linear revmap irq_domain.
  230. * @of_node: pointer to interrupt controller's device tree node.
  231. * @size: Number of interrupts in the domain.
  232. * @ops: map/unmap domain callbacks
  233. * @host_data: Controller private data pointer
  234. */
  235. static inline struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
  236. unsigned int size,
  237. const struct irq_domain_ops *ops,
  238. void *host_data)
  239. {
  240. return __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
  241. }
  242. static inline struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
  243. unsigned int max_irq,
  244. const struct irq_domain_ops *ops,
  245. void *host_data)
  246. {
  247. return __irq_domain_add(of_node_to_fwnode(of_node), 0, max_irq, max_irq, ops, host_data);
  248. }
  249. static inline struct irq_domain *irq_domain_add_legacy_isa(
  250. struct device_node *of_node,
  251. const struct irq_domain_ops *ops,
  252. void *host_data)
  253. {
  254. return irq_domain_add_legacy(of_node, NUM_ISA_INTERRUPTS, 0, 0, ops,
  255. host_data);
  256. }
  257. static inline struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
  258. const struct irq_domain_ops *ops,
  259. void *host_data)
  260. {
  261. return __irq_domain_add(of_node_to_fwnode(of_node), 0, ~0, 0, ops, host_data);
  262. }
  263. static inline struct irq_domain *irq_domain_create_linear(struct fwnode_handle *fwnode,
  264. unsigned int size,
  265. const struct irq_domain_ops *ops,
  266. void *host_data)
  267. {
  268. return __irq_domain_add(fwnode, size, size, 0, ops, host_data);
  269. }
  270. static inline struct irq_domain *irq_domain_create_tree(struct fwnode_handle *fwnode,
  271. const struct irq_domain_ops *ops,
  272. void *host_data)
  273. {
  274. return __irq_domain_add(fwnode, 0, ~0, 0, ops, host_data);
  275. }
  276. extern void irq_domain_remove(struct irq_domain *host);
  277. extern int irq_domain_associate(struct irq_domain *domain, unsigned int irq,
  278. irq_hw_number_t hwirq);
  279. extern void irq_domain_associate_many(struct irq_domain *domain,
  280. unsigned int irq_base,
  281. irq_hw_number_t hwirq_base, int count);
  282. extern void irq_domain_disassociate(struct irq_domain *domain,
  283. unsigned int irq);
  284. extern unsigned int irq_create_mapping(struct irq_domain *host,
  285. irq_hw_number_t hwirq);
  286. extern unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec);
  287. extern void irq_dispose_mapping(unsigned int virq);
  288. /**
  289. * irq_linear_revmap() - Find a linux irq from a hw irq number.
  290. * @domain: domain owning this hardware interrupt
  291. * @hwirq: hardware irq number in that domain space
  292. *
  293. * This is a fast path alternative to irq_find_mapping() that can be
  294. * called directly by irq controller code to save a handful of
  295. * instructions. It is always safe to call, but won't find irqs mapped
  296. * using the radix tree.
  297. */
  298. static inline unsigned int irq_linear_revmap(struct irq_domain *domain,
  299. irq_hw_number_t hwirq)
  300. {
  301. return hwirq < domain->revmap_size ? domain->linear_revmap[hwirq] : 0;
  302. }
  303. extern unsigned int irq_find_mapping(struct irq_domain *host,
  304. irq_hw_number_t hwirq);
  305. extern unsigned int irq_create_direct_mapping(struct irq_domain *host);
  306. extern int irq_create_strict_mappings(struct irq_domain *domain,
  307. unsigned int irq_base,
  308. irq_hw_number_t hwirq_base, int count);
  309. static inline int irq_create_identity_mapping(struct irq_domain *host,
  310. irq_hw_number_t hwirq)
  311. {
  312. return irq_create_strict_mappings(host, hwirq, hwirq, 1);
  313. }
  314. extern const struct irq_domain_ops irq_domain_simple_ops;
  315. /* stock xlate functions */
  316. int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
  317. const u32 *intspec, unsigned int intsize,
  318. irq_hw_number_t *out_hwirq, unsigned int *out_type);
  319. int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
  320. const u32 *intspec, unsigned int intsize,
  321. irq_hw_number_t *out_hwirq, unsigned int *out_type);
  322. int irq_domain_xlate_onetwocell(struct irq_domain *d, struct device_node *ctrlr,
  323. const u32 *intspec, unsigned int intsize,
  324. irq_hw_number_t *out_hwirq, unsigned int *out_type);
  325. /* IPI functions */
  326. int irq_reserve_ipi(struct irq_domain *domain, const struct cpumask *dest);
  327. int irq_destroy_ipi(unsigned int irq, const struct cpumask *dest);
  328. /* V2 interfaces to support hierarchy IRQ domains. */
  329. extern struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
  330. unsigned int virq);
  331. extern void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
  332. irq_hw_number_t hwirq, struct irq_chip *chip,
  333. void *chip_data, irq_flow_handler_t handler,
  334. void *handler_data, const char *handler_name);
  335. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  336. extern struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
  337. unsigned int flags, unsigned int size,
  338. struct fwnode_handle *fwnode,
  339. const struct irq_domain_ops *ops, void *host_data);
  340. static inline struct irq_domain *irq_domain_add_hierarchy(struct irq_domain *parent,
  341. unsigned int flags,
  342. unsigned int size,
  343. struct device_node *node,
  344. const struct irq_domain_ops *ops,
  345. void *host_data)
  346. {
  347. return irq_domain_create_hierarchy(parent, flags, size,
  348. of_node_to_fwnode(node),
  349. ops, host_data);
  350. }
  351. extern int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
  352. unsigned int nr_irqs, int node, void *arg,
  353. bool realloc, const struct cpumask *affinity);
  354. extern void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs);
  355. extern void irq_domain_activate_irq(struct irq_data *irq_data);
  356. extern void irq_domain_deactivate_irq(struct irq_data *irq_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 __irq_domain_alloc_irqs(domain, -1, nr_irqs, node, arg, false,
  361. NULL);
  362. }
  363. extern int irq_domain_alloc_irqs_recursive(struct irq_domain *domain,
  364. unsigned int irq_base,
  365. unsigned int nr_irqs, void *arg);
  366. extern int irq_domain_set_hwirq_and_chip(struct irq_domain *domain,
  367. unsigned int virq,
  368. irq_hw_number_t hwirq,
  369. struct irq_chip *chip,
  370. void *chip_data);
  371. extern void irq_domain_reset_irq_data(struct irq_data *irq_data);
  372. extern void irq_domain_free_irqs_common(struct irq_domain *domain,
  373. unsigned int virq,
  374. unsigned int nr_irqs);
  375. extern void irq_domain_free_irqs_top(struct irq_domain *domain,
  376. unsigned int virq, unsigned int nr_irqs);
  377. extern int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
  378. unsigned int irq_base,
  379. unsigned int nr_irqs, void *arg);
  380. extern void irq_domain_free_irqs_parent(struct irq_domain *domain,
  381. unsigned int irq_base,
  382. unsigned int nr_irqs);
  383. static inline bool irq_domain_is_hierarchy(struct irq_domain *domain)
  384. {
  385. return domain->flags & IRQ_DOMAIN_FLAG_HIERARCHY;
  386. }
  387. static inline bool irq_domain_is_ipi(struct irq_domain *domain)
  388. {
  389. return domain->flags &
  390. (IRQ_DOMAIN_FLAG_IPI_PER_CPU | IRQ_DOMAIN_FLAG_IPI_SINGLE);
  391. }
  392. static inline bool irq_domain_is_ipi_per_cpu(struct irq_domain *domain)
  393. {
  394. return domain->flags & IRQ_DOMAIN_FLAG_IPI_PER_CPU;
  395. }
  396. static inline bool irq_domain_is_ipi_single(struct irq_domain *domain)
  397. {
  398. return domain->flags & IRQ_DOMAIN_FLAG_IPI_SINGLE;
  399. }
  400. #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
  401. static inline void irq_domain_activate_irq(struct irq_data *data) { }
  402. static inline void irq_domain_deactivate_irq(struct irq_data *data) { }
  403. static inline int irq_domain_alloc_irqs(struct irq_domain *domain,
  404. unsigned int nr_irqs, int node, void *arg)
  405. {
  406. return -1;
  407. }
  408. static inline void irq_domain_free_irqs(unsigned int virq,
  409. unsigned int nr_irqs) { }
  410. static inline bool irq_domain_is_hierarchy(struct irq_domain *domain)
  411. {
  412. return false;
  413. }
  414. static inline bool irq_domain_is_ipi(struct irq_domain *domain)
  415. {
  416. return false;
  417. }
  418. static inline bool irq_domain_is_ipi_per_cpu(struct irq_domain *domain)
  419. {
  420. return false;
  421. }
  422. static inline bool irq_domain_is_ipi_single(struct irq_domain *domain)
  423. {
  424. return false;
  425. }
  426. #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
  427. #else /* CONFIG_IRQ_DOMAIN */
  428. static inline void irq_dispose_mapping(unsigned int virq) { }
  429. static inline void irq_domain_activate_irq(struct irq_data *data) { }
  430. static inline void irq_domain_deactivate_irq(struct irq_data *data) { }
  431. static inline struct irq_domain *irq_find_matching_fwnode(
  432. struct fwnode_handle *fwnode, enum irq_domain_bus_token bus_token)
  433. {
  434. return NULL;
  435. }
  436. #endif /* !CONFIG_IRQ_DOMAIN */
  437. #endif /* _LINUX_IRQDOMAIN_H */