pic.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-08-24 GuEe-GUI first version
  9. */
  10. #ifndef __PIC_H__
  11. #define __PIC_H__
  12. #include <rthw.h>
  13. #include <bitmap.h>
  14. #include <drivers/ofw.h>
  15. #include <drivers/core/dm.h>
  16. struct rt_pci_msi_desc;
  17. struct rt_pci_msi_msg;
  18. struct rt_pic_ops;
  19. struct rt_pic_irq;
  20. struct rt_pic
  21. {
  22. /*
  23. * Other IC is not implemented with PIC but rt_device/object, we need to
  24. * identify with this object:
  25. *
  26. * struct rt_ic_XYZ_device
  27. * {
  28. * struct rt_device parent;
  29. * struct rt_pic pic;
  30. * ...
  31. * };
  32. */
  33. struct rt_object parent;
  34. rt_list_t list;
  35. const struct rt_pic_ops *ops;
  36. void *priv_data;
  37. void *user_data;
  38. int irq_start;
  39. rt_size_t irq_nr;
  40. struct rt_pic_irq *pirqs;
  41. };
  42. struct rt_pic_ops
  43. {
  44. const char *name;
  45. rt_err_t (*irq_init)(struct rt_pic *pic);
  46. rt_err_t (*irq_finit)(struct rt_pic *pic);
  47. void (*irq_enable)(struct rt_pic_irq *pirq);
  48. void (*irq_disable)(struct rt_pic_irq *pirq);
  49. void (*irq_ack)(struct rt_pic_irq *pirq);
  50. void (*irq_mask)(struct rt_pic_irq *pirq);
  51. void (*irq_unmask)(struct rt_pic_irq *pirq);
  52. void (*irq_eoi)(struct rt_pic_irq *pirq);
  53. rt_err_t (*irq_set_priority)(struct rt_pic_irq *pirq, rt_uint32_t priority);
  54. rt_err_t (*irq_set_affinity)(struct rt_pic_irq *pirq, rt_bitmap_t *affinity);
  55. rt_err_t (*irq_set_triger_mode)(struct rt_pic_irq *pirq, rt_uint32_t mode);
  56. void (*irq_send_ipi)(struct rt_pic_irq *pirq, rt_bitmap_t *cpumask);
  57. void (*irq_compose_msi_msg)(struct rt_pic_irq *pirq, struct rt_pci_msi_msg *msg);
  58. void (*irq_write_msi_msg)(struct rt_pic_irq *pirq, struct rt_pci_msi_msg *msg);
  59. int (*irq_alloc_msi)(struct rt_pic *pic, struct rt_pci_msi_desc *msi_desc);
  60. void (*irq_free_msi)(struct rt_pic *pic, int irq);
  61. #define RT_IRQ_STATE_PENDING 0
  62. #define RT_IRQ_STATE_ACTIVE 1
  63. #define RT_IRQ_STATE_MASKED 2
  64. rt_err_t (*irq_set_state)(struct rt_pic *pic, int hwirq, int type, rt_bool_t state);
  65. rt_err_t (*irq_get_state)(struct rt_pic *pic, int hwirq, int type, rt_bool_t *out_state);
  66. int (*irq_map)(struct rt_pic *pic, int hwirq, rt_uint32_t mode);
  67. rt_err_t (*irq_parse)(struct rt_pic *pic, struct rt_ofw_cell_args *args, struct rt_pic_irq *out_pirq);
  68. #define RT_PIC_F_IRQ_ROUTING RT_BIT(0) /* Routing ISR when cascade */
  69. rt_ubase_t flags;
  70. };
  71. struct rt_pic_isr
  72. {
  73. rt_list_t list;
  74. #define RT_IRQ_F_NONE 0
  75. int flags;
  76. struct rt_irq_desc action;
  77. };
  78. #define RT_IRQ_AFFINITY_DECLARE(name) RT_BITMAP_DECLARE(name, RT_CPUS_NR)
  79. #define RT_IRQ_AFFINITY_SET(affinity, cpuid) rt_bitmap_set_bit(affinity, cpuid)
  80. #define RT_IRQ_AFFINITY_CLEAR(affinity, cpuid) rt_bitmap_clear_bit(affinity, cpuid)
  81. #ifdef RT_USING_PIC_STATISTICS
  82. struct rt_pic_irq_statistics
  83. {
  84. rt_ubase_t max_irq_time_ns;
  85. rt_ubase_t min_irq_time_ns;
  86. rt_ubase_t sum_irq_time_ns;
  87. };
  88. #endif
  89. struct rt_pic_irq
  90. {
  91. int irq;
  92. int hwirq;
  93. #define RT_IRQ_MODE_NONE 0
  94. #define RT_IRQ_MODE_EDGE_RISING 1
  95. #define RT_IRQ_MODE_EDGE_FALLING 2
  96. #define RT_IRQ_MODE_EDGE_BOTH (RT_IRQ_MODE_EDGE_FALLING | RT_IRQ_MODE_EDGE_RISING)
  97. #define RT_IRQ_MODE_LEVEL_HIGH 4
  98. #define RT_IRQ_MODE_LEVEL_LOW 8
  99. #define RT_IRQ_MODE_LEVEL_MASK (RT_IRQ_MODE_LEVEL_LOW | RT_IRQ_MODE_LEVEL_HIGH)
  100. #define RT_IRQ_MODE_MASK 0xf
  101. rt_uint32_t mode;
  102. rt_uint32_t priority;
  103. RT_IRQ_AFFINITY_DECLARE(affinity);
  104. rt_list_t list;
  105. rt_list_t children_nodes;
  106. struct rt_pci_msi_desc *msi_desc;
  107. struct rt_pic_isr isr;
  108. struct rt_spinlock rw_lock;
  109. struct rt_pic *pic;
  110. struct rt_pic_irq *parent;
  111. #ifdef RT_USING_PIC_STATISTICS
  112. struct rt_pic_irq_statistics stat;
  113. #endif
  114. };
  115. void rt_pic_default_name(struct rt_pic *pic);
  116. struct rt_pic *rt_pic_dynamic_cast(void *ptr);
  117. rt_err_t rt_pic_linear_irq(struct rt_pic *pic, rt_size_t irq_nr);
  118. rt_err_t rt_pic_cancel_irq(struct rt_pic *pic);
  119. int rt_pic_config_ipi(struct rt_pic *pic, int ipi_index, int hwirq);
  120. int rt_pic_config_irq(struct rt_pic *pic, int irq_index, int hwirq);
  121. rt_inline struct rt_pic_irq *rt_pic_find_irq(struct rt_pic *pic, int irq_index)
  122. {
  123. /* This is a quickly interface */
  124. RT_ASSERT(pic != RT_NULL);
  125. RT_ASSERT(pic->pirqs != RT_NULL);
  126. RT_ASSERT(irq_index < pic->irq_nr);
  127. return &pic->pirqs[irq_index];
  128. }
  129. struct rt_pic_irq *rt_pic_find_ipi(struct rt_pic *pic, int ipi_index);
  130. struct rt_pic_irq *rt_pic_find_pirq(struct rt_pic *pic, int irq);
  131. rt_err_t rt_pic_cascade(struct rt_pic_irq *pirq, int parent_irq);
  132. rt_err_t rt_pic_uncascade(struct rt_pic_irq *pirq);
  133. rt_err_t rt_pic_attach_irq(int irq, rt_isr_handler_t handler, void *uid, const char *name, int flags);
  134. rt_err_t rt_pic_detach_irq(int irq, void *uid);
  135. rt_err_t rt_pic_add_traps(rt_bool_t (*handler)(void *), void *data);
  136. rt_err_t rt_pic_do_traps(void);
  137. rt_err_t rt_pic_handle_isr(struct rt_pic_irq *pirq);
  138. /* User-implemented extensions */
  139. rt_err_t rt_pic_user_extends(struct rt_pic *pic);
  140. rt_err_t rt_pic_irq_init(void);
  141. rt_err_t rt_pic_irq_finit(void);
  142. void rt_pic_irq_enable(int irq);
  143. void rt_pic_irq_disable(int irq);
  144. void rt_pic_irq_ack(int irq);
  145. void rt_pic_irq_mask(int irq);
  146. void rt_pic_irq_unmask(int irq);
  147. void rt_pic_irq_eoi(int irq);
  148. rt_err_t rt_pic_irq_set_priority(int irq, rt_uint32_t priority);
  149. rt_uint32_t rt_pic_irq_get_priority(int irq);
  150. rt_err_t rt_pic_irq_set_affinity(int irq, rt_bitmap_t *affinity);
  151. rt_err_t rt_pic_irq_get_affinity(int irq, rt_bitmap_t *out_affinity);
  152. rt_err_t rt_pic_irq_set_triger_mode(int irq, rt_uint32_t mode);
  153. rt_uint32_t rt_pic_irq_get_triger_mode(int irq);
  154. void rt_pic_irq_send_ipi(int irq, rt_bitmap_t *cpumask);
  155. rt_err_t rt_pic_irq_set_state_raw(struct rt_pic *pic, int hwirq, int type, rt_bool_t state);
  156. rt_err_t rt_pic_irq_get_state_raw(struct rt_pic *pic, int hwirq, int type, rt_bool_t *out_state);
  157. rt_err_t rt_pic_irq_set_state(int irq, int type, rt_bool_t state);
  158. rt_err_t rt_pic_irq_get_state(int irq, int type, rt_bool_t *out_state);
  159. void rt_pic_irq_parent_enable(struct rt_pic_irq *pirq);
  160. void rt_pic_irq_parent_disable(struct rt_pic_irq *pirq);
  161. void rt_pic_irq_parent_ack(struct rt_pic_irq *pirq);
  162. void rt_pic_irq_parent_mask(struct rt_pic_irq *pirq);
  163. void rt_pic_irq_parent_unmask(struct rt_pic_irq *pirq);
  164. void rt_pic_irq_parent_eoi(struct rt_pic_irq *pirq);
  165. rt_err_t rt_pic_irq_parent_set_priority(struct rt_pic_irq *pirq, rt_uint32_t priority);
  166. rt_err_t rt_pic_irq_parent_set_affinity(struct rt_pic_irq *pirq, rt_bitmap_t *affinity);
  167. rt_err_t rt_pic_irq_parent_set_triger_mode(struct rt_pic_irq *pirq, rt_uint32_t mode);
  168. #define RT_PIC_OFW_DECLARE(name, ids, handler) RT_OFW_STUB_EXPORT(name, ids, pic, handler)
  169. rt_err_t rt_pic_init(void);
  170. #endif /* __PIC_H__ */