pic.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. int (*irq_map)(struct rt_pic *pic, int hwirq, rt_uint32_t mode);
  62. rt_err_t (*irq_parse)(struct rt_pic *pic, struct rt_ofw_cell_args *args, struct rt_pic_irq *out_pirq);
  63. #define RT_PIC_F_IRQ_ROUTING RT_BIT(0) /* Routing ISR when cascade */
  64. rt_ubase_t flags;
  65. };
  66. struct rt_pic_isr
  67. {
  68. rt_list_t list;
  69. #define RT_IRQ_F_NONE 0
  70. int flags;
  71. struct rt_irq_desc action;
  72. };
  73. #define RT_IRQ_AFFINITY_DECLARE(name) RT_BITMAP_DECLARE(name, RT_CPUS_NR)
  74. #define RT_IRQ_AFFINITY_SET(affinity, cpuid) rt_bitmap_set_bit(affinity, cpuid)
  75. #define RT_IRQ_AFFINITY_CLEAR(affinity, cpuid) rt_bitmap_clear_bit(affinity, cpuid)
  76. #ifdef RT_USING_PIC_STATISTICS
  77. struct rt_pic_irq_statistics
  78. {
  79. rt_ubase_t current_irq_begin[RT_CPUS_NR];
  80. rt_ubase_t max_irq_time_ns;
  81. rt_ubase_t min_irq_time_ns;
  82. rt_ubase_t sum_irq_time_ns;
  83. };
  84. #endif
  85. struct rt_pic_irq
  86. {
  87. int irq;
  88. int hwirq;
  89. #define RT_IRQ_MODE_NONE 0
  90. #define RT_IRQ_MODE_EDGE_RISING 1
  91. #define RT_IRQ_MODE_EDGE_FALLING 2
  92. #define RT_IRQ_MODE_EDGE_BOTH (RT_IRQ_MODE_EDGE_FALLING | RT_IRQ_MODE_EDGE_RISING)
  93. #define RT_IRQ_MODE_LEVEL_HIGH 4
  94. #define RT_IRQ_MODE_LEVEL_LOW 8
  95. #define RT_IRQ_MODE_LEVEL_MASK (RT_IRQ_MODE_LEVEL_LOW | RT_IRQ_MODE_LEVEL_HIGH)
  96. #define RT_IRQ_MODE_MASK 0xf
  97. rt_uint32_t mode;
  98. rt_uint32_t priority;
  99. RT_IRQ_AFFINITY_DECLARE(affinity);
  100. rt_list_t list;
  101. rt_list_t children_nodes;
  102. struct rt_pci_msi_desc *msi_desc;
  103. struct rt_pic_isr isr;
  104. struct rt_spinlock rw_lock;
  105. struct rt_pic *pic;
  106. struct rt_pic_irq *parent;
  107. #ifdef RT_USING_PIC_STATISTICS
  108. struct rt_pic_irq_statistics stat;
  109. #endif
  110. };
  111. void rt_pic_default_name(struct rt_pic *pic);
  112. struct rt_pic *rt_pic_dynamic_cast(void *ptr);
  113. rt_err_t rt_pic_linear_irq(struct rt_pic *pic, rt_size_t irq_nr);
  114. int rt_pic_config_ipi(struct rt_pic *pic, int ipi_index, int hwirq);
  115. int rt_pic_config_irq(struct rt_pic *pic, int irq_index, int hwirq);
  116. rt_inline struct rt_pic_irq *rt_pic_find_irq(struct rt_pic *pic, int irq_index)
  117. {
  118. /* This is a quickly interface */
  119. RT_ASSERT(pic != RT_NULL);
  120. RT_ASSERT(pic->pirqs != RT_NULL);
  121. RT_ASSERT(irq_index < pic->irq_nr);
  122. return &pic->pirqs[irq_index];
  123. }
  124. struct rt_pic_irq *rt_pic_find_ipi(struct rt_pic *pic, int ipi_index);
  125. struct rt_pic_irq *rt_pic_find_pirq(struct rt_pic *pic, int irq);
  126. rt_err_t rt_pic_cascade(struct rt_pic_irq *pirq, int parent_irq);
  127. rt_err_t rt_pic_uncascade(struct rt_pic_irq *pirq);
  128. rt_err_t rt_pic_attach_irq(int irq, rt_isr_handler_t handler, void *uid, const char *name, int flags);
  129. rt_err_t rt_pic_detach_irq(int irq, void *uid);
  130. rt_err_t rt_pic_add_traps(rt_bool_t (*handler)(void *), void *data);
  131. rt_err_t rt_pic_do_traps(void);
  132. rt_err_t rt_pic_handle_isr(struct rt_pic_irq *pirq);
  133. /* User-implemented extensions */
  134. rt_err_t rt_pic_user_extends(struct rt_pic *pic);
  135. rt_err_t rt_pic_irq_init(void);
  136. rt_err_t rt_pic_irq_finit(void);
  137. void rt_pic_irq_enable(int irq);
  138. void rt_pic_irq_disable(int irq);
  139. void rt_pic_irq_ack(int irq);
  140. void rt_pic_irq_mask(int irq);
  141. void rt_pic_irq_unmask(int irq);
  142. void rt_pic_irq_eoi(int irq);
  143. rt_err_t rt_pic_irq_set_priority(int irq, rt_uint32_t priority);
  144. rt_uint32_t rt_pic_irq_get_priority(int irq);
  145. rt_err_t rt_pic_irq_set_affinity(int irq, rt_bitmap_t *affinity);
  146. rt_err_t rt_pic_irq_get_affinity(int irq, rt_bitmap_t *out_affinity);
  147. rt_err_t rt_pic_irq_set_triger_mode(int irq, rt_uint32_t mode);
  148. rt_uint32_t rt_pic_irq_get_triger_mode(int irq);
  149. void rt_pic_irq_send_ipi(int irq, rt_bitmap_t *cpumask);
  150. void rt_pic_irq_parent_enable(struct rt_pic_irq *pirq);
  151. void rt_pic_irq_parent_disable(struct rt_pic_irq *pirq);
  152. void rt_pic_irq_parent_ack(struct rt_pic_irq *pirq);
  153. void rt_pic_irq_parent_mask(struct rt_pic_irq *pirq);
  154. void rt_pic_irq_parent_unmask(struct rt_pic_irq *pirq);
  155. void rt_pic_irq_parent_eoi(struct rt_pic_irq *pirq);
  156. rt_err_t rt_pic_irq_parent_set_priority(struct rt_pic_irq *pirq, rt_uint32_t priority);
  157. rt_err_t rt_pic_irq_parent_set_affinity(struct rt_pic_irq *pirq, rt_bitmap_t *affinity);
  158. rt_err_t rt_pic_irq_parent_set_triger_mode(struct rt_pic_irq *pirq, rt_uint32_t mode);
  159. #define RT_PIC_OFW_DECLARE(name, ids, handler) RT_OFW_STUB_EXPORT(name, ids, pic, handler)
  160. rt_err_t rt_pic_init(void);
  161. #endif /* __PIC_H__ */