pic.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. rt_list_t list;
  23. struct rt_pic_ops *ops;
  24. void *priv_data;
  25. void *user_data;
  26. struct rt_pic *parent;
  27. int irq_start;
  28. rt_size_t irq_nr;
  29. struct rt_pic_irq *pirqs;
  30. };
  31. struct rt_pic_ops
  32. {
  33. const char *name;
  34. rt_err_t (*irq_init)(struct rt_pic *pic);
  35. rt_err_t (*irq_finit)(struct rt_pic *pic);
  36. void (*irq_enable)(struct rt_pic_irq *pirq);
  37. void (*irq_disable)(struct rt_pic_irq *pirq);
  38. void (*irq_ack)(struct rt_pic_irq *pirq);
  39. void (*irq_mask)(struct rt_pic_irq *pirq);
  40. void (*irq_unmask)(struct rt_pic_irq *pirq);
  41. void (*irq_eoi)(struct rt_pic_irq *pirq);
  42. rt_err_t (*irq_set_priority)(struct rt_pic_irq *pirq, rt_uint32_t priority);
  43. rt_err_t (*irq_set_affinity)(struct rt_pic_irq *pirq, rt_bitmap_t *affinity);
  44. rt_err_t (*irq_set_triger_mode)(struct rt_pic_irq *pirq, rt_uint32_t mode);
  45. void (*irq_send_ipi)(struct rt_pic_irq *pirq, rt_bitmap_t *cpumask);
  46. void (*irq_compose_msi_msg)(struct rt_pic_irq *pirq, struct rt_pci_msi_msg *msg);
  47. void (*irq_write_msi_msg)(struct rt_pic_irq *pirq, struct rt_pci_msi_msg *msg);
  48. int (*irq_alloc_msi)(struct rt_pic *pic, struct rt_pci_msi_desc *msi_desc);
  49. void (*irq_free_msi)(struct rt_pic *pic, int irq);
  50. int (*irq_map)(struct rt_pic *pic, int hwirq, rt_uint32_t mode);
  51. rt_err_t (*irq_parse)(struct rt_pic *pic, struct rt_ofw_cell_args *args, struct rt_pic_irq *out_pirq);
  52. };
  53. struct rt_pic_isr
  54. {
  55. rt_list_t list;
  56. #define RT_IRQ_F_NONE 0
  57. int flags;
  58. struct rt_irq_desc action;
  59. };
  60. struct rt_pic_irq
  61. {
  62. int irq;
  63. int hwirq;
  64. #define RT_IRQ_MODE_NONE 0
  65. #define RT_IRQ_MODE_EDGE_RISING 1
  66. #define RT_IRQ_MODE_EDGE_FALLING 2
  67. #define RT_IRQ_MODE_EDGE_BOTH (RT_IRQ_MODE_EDGE_FALLING | RT_IRQ_MODE_EDGE_RISING)
  68. #define RT_IRQ_MODE_LEVEL_HIGH 4
  69. #define RT_IRQ_MODE_LEVEL_LOW 8
  70. #define RT_IRQ_MODE_LEVEL_MASK (RT_IRQ_MODE_LEVEL_LOW | RT_IRQ_MODE_LEVEL_HIGH)
  71. #define RT_IRQ_MODE_MASK 0xf
  72. rt_uint32_t mode;
  73. rt_uint32_t priority;
  74. RT_DECLARE_BITMAP(affinity, RT_CPUS_NR);
  75. struct rt_pci_msi_desc *msi_desc;
  76. struct rt_pic_isr isr;
  77. struct rt_spinlock rw_lock;
  78. struct rt_pic *pic;
  79. };
  80. rt_err_t rt_pic_linear_irq(struct rt_pic *pic, rt_size_t irq_nr);
  81. int rt_pic_config_ipi(struct rt_pic *pic, int ipi_index, int hwirq);
  82. int rt_pic_config_irq(struct rt_pic *pic, int irq_index, int hwirq);
  83. rt_inline struct rt_pic_irq *rt_pic_find_irq(struct rt_pic *pic, int irq_index)
  84. {
  85. /* This is a quickly interface */
  86. RT_ASSERT(pic != RT_NULL);
  87. RT_ASSERT(pic->pirqs != RT_NULL);
  88. RT_ASSERT(irq_index < pic->irq_nr);
  89. return &pic->pirqs[irq_index];
  90. }
  91. struct rt_pic_irq *rt_pic_find_ipi(struct rt_pic *pic, int ipi_index);
  92. int rt_pic_cascade(struct rt_pic *pic, struct rt_pic *parent_pic, int hwirq, rt_uint32_t mode);
  93. void rt_pic_uncascade(struct rt_pic *pic, int irq);
  94. rt_err_t rt_pic_attach_irq(int irq, rt_isr_handler_t handler, void *uid, const char *name, int flags);
  95. rt_err_t rt_pic_detach_irq(int irq, void *uid);
  96. rt_err_t rt_pic_add_traps(rt_bool_t (*handler)(void *), void *data);
  97. rt_err_t rt_pic_do_traps(void);
  98. rt_err_t rt_pic_handle_isr(struct rt_pic_irq *pirq);
  99. /* User-implemented extensions */
  100. rt_err_t rt_pic_user_extends(struct rt_pic *pic);
  101. rt_err_t rt_pic_irq_init(void);
  102. rt_err_t rt_pic_irq_finit(void);
  103. void rt_pic_irq_enable(int irq);
  104. void rt_pic_irq_disable(int irq);
  105. void rt_pic_irq_ack(int irq);
  106. void rt_pic_irq_mask(int irq);
  107. void rt_pic_irq_unmask(int irq);
  108. void rt_pic_irq_eoi(int irq);
  109. rt_err_t rt_pic_irq_set_priority(int irq, rt_uint32_t priority);
  110. rt_uint32_t rt_pic_irq_get_priority(int irq);
  111. rt_err_t rt_pic_irq_set_affinity(int irq, rt_bitmap_t *affinity);
  112. rt_err_t rt_pic_irq_get_affinity(int irq, rt_bitmap_t *out_affinity);
  113. rt_err_t rt_pic_irq_set_triger_mode(int irq, rt_uint32_t mode);
  114. rt_uint32_t rt_pic_irq_get_triger_mode(int irq);
  115. void rt_pic_irq_send_ipi(int irq, rt_bitmap_t *cpumask);
  116. void rt_pic_irq_parent_enable(struct rt_pic *ppic, struct rt_pic_irq *pirq);
  117. void rt_pic_irq_parent_disable(struct rt_pic *ppic, struct rt_pic_irq *pirq);
  118. void rt_pic_irq_parent_ack(struct rt_pic *ppic, struct rt_pic_irq *pirq);
  119. void rt_pic_irq_parent_mask(struct rt_pic *ppic, struct rt_pic_irq *pirq);
  120. void rt_pic_irq_parent_unmask(struct rt_pic *ppic, struct rt_pic_irq *pirq);
  121. void rt_pic_irq_parent_eoi(struct rt_pic *ppic, struct rt_pic_irq *pirq);
  122. rt_err_t rt_pic_irq_parent_set_priority(struct rt_pic *ppic, struct rt_pic_irq *pirq, rt_uint32_t priority);
  123. rt_err_t rt_pic_irq_parent_set_affinity(struct rt_pic *ppic, struct rt_pic_irq *pirq, rt_bitmap_t *affinity);
  124. rt_err_t rt_pic_irq_parent_set_triger_mode(struct rt_pic *ppic, struct rt_pic_irq *pirq, rt_uint32_t mode);
  125. #define RT_PIC_OFW_DECLARE(name, ids, handler) RT_OFW_STUB_EXPORT(name, ids, pic, handler)
  126. rt_err_t rt_pic_init(void);
  127. #endif /* __PIC_H__ */