pic.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. 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. struct rt_pic_irq
  74. {
  75. int irq;
  76. int hwirq;
  77. #define RT_IRQ_MODE_NONE 0
  78. #define RT_IRQ_MODE_EDGE_RISING 1
  79. #define RT_IRQ_MODE_EDGE_FALLING 2
  80. #define RT_IRQ_MODE_EDGE_BOTH (RT_IRQ_MODE_EDGE_FALLING | RT_IRQ_MODE_EDGE_RISING)
  81. #define RT_IRQ_MODE_LEVEL_HIGH 4
  82. #define RT_IRQ_MODE_LEVEL_LOW 8
  83. #define RT_IRQ_MODE_LEVEL_MASK (RT_IRQ_MODE_LEVEL_LOW | RT_IRQ_MODE_LEVEL_HIGH)
  84. #define RT_IRQ_MODE_MASK 0xf
  85. rt_uint32_t mode;
  86. rt_uint32_t priority;
  87. RT_DECLARE_BITMAP(affinity, RT_CPUS_NR);
  88. rt_list_t list;
  89. rt_list_t children_nodes;
  90. struct rt_pci_msi_desc *msi_desc;
  91. struct rt_pic_isr isr;
  92. struct rt_spinlock rw_lock;
  93. struct rt_pic *pic;
  94. struct rt_pic_irq *parent;
  95. };
  96. void rt_pic_default_name(struct rt_pic *pic);
  97. struct rt_pic *rt_pic_dynamic_cast(void *ptr);
  98. rt_err_t rt_pic_linear_irq(struct rt_pic *pic, rt_size_t irq_nr);
  99. int rt_pic_config_ipi(struct rt_pic *pic, int ipi_index, int hwirq);
  100. int rt_pic_config_irq(struct rt_pic *pic, int irq_index, int hwirq);
  101. rt_inline struct rt_pic_irq *rt_pic_find_irq(struct rt_pic *pic, int irq_index)
  102. {
  103. /* This is a quickly interface */
  104. RT_ASSERT(pic != RT_NULL);
  105. RT_ASSERT(pic->pirqs != RT_NULL);
  106. RT_ASSERT(irq_index < pic->irq_nr);
  107. return &pic->pirqs[irq_index];
  108. }
  109. struct rt_pic_irq *rt_pic_find_ipi(struct rt_pic *pic, int ipi_index);
  110. struct rt_pic_irq *rt_pic_find_pirq(struct rt_pic *pic, int irq);
  111. rt_err_t rt_pic_cascade(struct rt_pic_irq *pirq, int parent_irq);
  112. rt_err_t rt_pic_uncascade(struct rt_pic_irq *pirq);
  113. rt_err_t rt_pic_attach_irq(int irq, rt_isr_handler_t handler, void *uid, const char *name, int flags);
  114. rt_err_t rt_pic_detach_irq(int irq, void *uid);
  115. rt_err_t rt_pic_add_traps(rt_bool_t (*handler)(void *), void *data);
  116. rt_err_t rt_pic_do_traps(void);
  117. rt_err_t rt_pic_handle_isr(struct rt_pic_irq *pirq);
  118. /* User-implemented extensions */
  119. rt_err_t rt_pic_user_extends(struct rt_pic *pic);
  120. rt_err_t rt_pic_irq_init(void);
  121. rt_err_t rt_pic_irq_finit(void);
  122. void rt_pic_irq_enable(int irq);
  123. void rt_pic_irq_disable(int irq);
  124. void rt_pic_irq_ack(int irq);
  125. void rt_pic_irq_mask(int irq);
  126. void rt_pic_irq_unmask(int irq);
  127. void rt_pic_irq_eoi(int irq);
  128. rt_err_t rt_pic_irq_set_priority(int irq, rt_uint32_t priority);
  129. rt_uint32_t rt_pic_irq_get_priority(int irq);
  130. rt_err_t rt_pic_irq_set_affinity(int irq, rt_bitmap_t *affinity);
  131. rt_err_t rt_pic_irq_get_affinity(int irq, rt_bitmap_t *out_affinity);
  132. rt_err_t rt_pic_irq_set_triger_mode(int irq, rt_uint32_t mode);
  133. rt_uint32_t rt_pic_irq_get_triger_mode(int irq);
  134. void rt_pic_irq_send_ipi(int irq, rt_bitmap_t *cpumask);
  135. void rt_pic_irq_parent_enable(struct rt_pic_irq *pirq);
  136. void rt_pic_irq_parent_disable(struct rt_pic_irq *pirq);
  137. void rt_pic_irq_parent_ack(struct rt_pic_irq *pirq);
  138. void rt_pic_irq_parent_mask(struct rt_pic_irq *pirq);
  139. void rt_pic_irq_parent_unmask(struct rt_pic_irq *pirq);
  140. void rt_pic_irq_parent_eoi(struct rt_pic_irq *pirq);
  141. rt_err_t rt_pic_irq_parent_set_priority(struct rt_pic_irq *pirq, rt_uint32_t priority);
  142. rt_err_t rt_pic_irq_parent_set_affinity(struct rt_pic_irq *pirq, rt_bitmap_t *affinity);
  143. rt_err_t rt_pic_irq_parent_set_triger_mode(struct rt_pic_irq *pirq, rt_uint32_t mode);
  144. #define RT_PIC_OFW_DECLARE(name, ids, handler) RT_OFW_STUB_EXPORT(name, ids, pic, handler)
  145. rt_err_t rt_pic_init(void);
  146. #endif /* __PIC_H__ */