ehv_pic.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Driver for ePAPR Embedded Hypervisor PIC
  3. *
  4. * Copyright 2008-2011 Freescale Semiconductor, Inc.
  5. *
  6. * Author: Ashish Kalra <ashish.kalra@freescale.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/irq.h>
  16. #include <linux/smp.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <asm/io.h>
  23. #include <asm/irq.h>
  24. #include <asm/smp.h>
  25. #include <asm/machdep.h>
  26. #include <asm/ehv_pic.h>
  27. #include <asm/fsl_hcalls.h>
  28. #include "../../../kernel/irq/settings.h"
  29. static struct ehv_pic *global_ehv_pic;
  30. static DEFINE_SPINLOCK(ehv_pic_lock);
  31. static u32 hwirq_intspec[NR_EHV_PIC_INTS];
  32. static u32 __iomem *mpic_percpu_base_vaddr;
  33. #define IRQ_TYPE_MPIC_DIRECT 4
  34. #define MPIC_EOI 0x00B0
  35. /*
  36. * Linux descriptor level callbacks
  37. */
  38. void ehv_pic_unmask_irq(struct irq_data *d)
  39. {
  40. unsigned int src = virq_to_hw(d->irq);
  41. ev_int_set_mask(src, 0);
  42. }
  43. void ehv_pic_mask_irq(struct irq_data *d)
  44. {
  45. unsigned int src = virq_to_hw(d->irq);
  46. ev_int_set_mask(src, 1);
  47. }
  48. void ehv_pic_end_irq(struct irq_data *d)
  49. {
  50. unsigned int src = virq_to_hw(d->irq);
  51. ev_int_eoi(src);
  52. }
  53. void ehv_pic_direct_end_irq(struct irq_data *d)
  54. {
  55. out_be32(mpic_percpu_base_vaddr + MPIC_EOI / 4, 0);
  56. }
  57. int ehv_pic_set_affinity(struct irq_data *d, const struct cpumask *dest,
  58. bool force)
  59. {
  60. unsigned int src = virq_to_hw(d->irq);
  61. unsigned int config, prio, cpu_dest;
  62. int cpuid = irq_choose_cpu(dest);
  63. unsigned long flags;
  64. spin_lock_irqsave(&ehv_pic_lock, flags);
  65. ev_int_get_config(src, &config, &prio, &cpu_dest);
  66. ev_int_set_config(src, config, prio, cpuid);
  67. spin_unlock_irqrestore(&ehv_pic_lock, flags);
  68. return IRQ_SET_MASK_OK;
  69. }
  70. static unsigned int ehv_pic_type_to_vecpri(unsigned int type)
  71. {
  72. /* Now convert sense value */
  73. switch (type & IRQ_TYPE_SENSE_MASK) {
  74. case IRQ_TYPE_EDGE_RISING:
  75. return EHV_PIC_INFO(VECPRI_SENSE_EDGE) |
  76. EHV_PIC_INFO(VECPRI_POLARITY_POSITIVE);
  77. case IRQ_TYPE_EDGE_FALLING:
  78. case IRQ_TYPE_EDGE_BOTH:
  79. return EHV_PIC_INFO(VECPRI_SENSE_EDGE) |
  80. EHV_PIC_INFO(VECPRI_POLARITY_NEGATIVE);
  81. case IRQ_TYPE_LEVEL_HIGH:
  82. return EHV_PIC_INFO(VECPRI_SENSE_LEVEL) |
  83. EHV_PIC_INFO(VECPRI_POLARITY_POSITIVE);
  84. case IRQ_TYPE_LEVEL_LOW:
  85. default:
  86. return EHV_PIC_INFO(VECPRI_SENSE_LEVEL) |
  87. EHV_PIC_INFO(VECPRI_POLARITY_NEGATIVE);
  88. }
  89. }
  90. int ehv_pic_set_irq_type(struct irq_data *d, unsigned int flow_type)
  91. {
  92. unsigned int src = virq_to_hw(d->irq);
  93. struct irq_desc *desc = irq_to_desc(d->irq);
  94. unsigned int vecpri, vold, vnew, prio, cpu_dest;
  95. unsigned long flags;
  96. if (flow_type == IRQ_TYPE_NONE)
  97. flow_type = IRQ_TYPE_LEVEL_LOW;
  98. irq_settings_clr_level(desc);
  99. irq_settings_set_trigger_mask(desc, flow_type);
  100. if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
  101. irq_settings_set_level(desc);
  102. vecpri = ehv_pic_type_to_vecpri(flow_type);
  103. spin_lock_irqsave(&ehv_pic_lock, flags);
  104. ev_int_get_config(src, &vold, &prio, &cpu_dest);
  105. vnew = vold & ~(EHV_PIC_INFO(VECPRI_POLARITY_MASK) |
  106. EHV_PIC_INFO(VECPRI_SENSE_MASK));
  107. vnew |= vecpri;
  108. /*
  109. * TODO : Add specific interface call for platform to set
  110. * individual interrupt priorities.
  111. * platform currently using static/default priority for all ints
  112. */
  113. prio = 8;
  114. ev_int_set_config(src, vecpri, prio, cpu_dest);
  115. spin_unlock_irqrestore(&ehv_pic_lock, flags);
  116. return 0;
  117. }
  118. static struct irq_chip ehv_pic_irq_chip = {
  119. .irq_mask = ehv_pic_mask_irq,
  120. .irq_unmask = ehv_pic_unmask_irq,
  121. .irq_eoi = ehv_pic_end_irq,
  122. .irq_set_type = ehv_pic_set_irq_type,
  123. };
  124. static struct irq_chip ehv_pic_direct_eoi_irq_chip = {
  125. .irq_mask = ehv_pic_mask_irq,
  126. .irq_unmask = ehv_pic_unmask_irq,
  127. .irq_eoi = ehv_pic_direct_end_irq,
  128. .irq_set_type = ehv_pic_set_irq_type,
  129. };
  130. /* Return an interrupt vector or NO_IRQ if no interrupt is pending. */
  131. unsigned int ehv_pic_get_irq(void)
  132. {
  133. int irq;
  134. BUG_ON(global_ehv_pic == NULL);
  135. if (global_ehv_pic->coreint_flag)
  136. irq = mfspr(SPRN_EPR); /* if core int mode */
  137. else
  138. ev_int_iack(0, &irq); /* legacy mode */
  139. if (irq == 0xFFFF) /* 0xFFFF --> no irq is pending */
  140. return NO_IRQ;
  141. /*
  142. * this will also setup revmap[] in the slow path for the first
  143. * time, next calls will always use fast path by indexing revmap
  144. */
  145. return irq_linear_revmap(global_ehv_pic->irqhost, irq);
  146. }
  147. static int ehv_pic_host_match(struct irq_domain *h, struct device_node *node)
  148. {
  149. /* Exact match, unless ehv_pic node is NULL */
  150. return h->of_node == NULL || h->of_node == node;
  151. }
  152. static int ehv_pic_host_map(struct irq_domain *h, unsigned int virq,
  153. irq_hw_number_t hw)
  154. {
  155. struct ehv_pic *ehv_pic = h->host_data;
  156. struct irq_chip *chip;
  157. /* Default chip */
  158. chip = &ehv_pic->hc_irq;
  159. if (mpic_percpu_base_vaddr)
  160. if (hwirq_intspec[hw] & IRQ_TYPE_MPIC_DIRECT)
  161. chip = &ehv_pic_direct_eoi_irq_chip;
  162. irq_set_chip_data(virq, chip);
  163. /*
  164. * using handle_fasteoi_irq as our irq handler, this will
  165. * only call the eoi callback and suitable for the MPIC
  166. * controller which set ISR/IPR automatically and clear the
  167. * highest priority active interrupt in ISR/IPR when we do
  168. * a specific eoi
  169. */
  170. irq_set_chip_and_handler(virq, chip, handle_fasteoi_irq);
  171. /* Set default irq type */
  172. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  173. return 0;
  174. }
  175. static int ehv_pic_host_xlate(struct irq_domain *h, struct device_node *ct,
  176. const u32 *intspec, unsigned int intsize,
  177. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  178. {
  179. /*
  180. * interrupt sense values coming from the guest device tree
  181. * interrupt specifiers can have four possible sense and
  182. * level encoding information and they need to
  183. * be translated between firmware type & linux type.
  184. */
  185. static unsigned char map_of_senses_to_linux_irqtype[4] = {
  186. IRQ_TYPE_EDGE_FALLING,
  187. IRQ_TYPE_EDGE_RISING,
  188. IRQ_TYPE_LEVEL_LOW,
  189. IRQ_TYPE_LEVEL_HIGH,
  190. };
  191. *out_hwirq = intspec[0];
  192. if (intsize > 1) {
  193. hwirq_intspec[intspec[0]] = intspec[1];
  194. *out_flags = map_of_senses_to_linux_irqtype[intspec[1] &
  195. ~IRQ_TYPE_MPIC_DIRECT];
  196. } else {
  197. *out_flags = IRQ_TYPE_NONE;
  198. }
  199. return 0;
  200. }
  201. static const struct irq_domain_ops ehv_pic_host_ops = {
  202. .match = ehv_pic_host_match,
  203. .map = ehv_pic_host_map,
  204. .xlate = ehv_pic_host_xlate,
  205. };
  206. void __init ehv_pic_init(void)
  207. {
  208. struct device_node *np, *np2;
  209. struct ehv_pic *ehv_pic;
  210. int coreint_flag = 1;
  211. np = of_find_compatible_node(NULL, NULL, "epapr,hv-pic");
  212. if (!np) {
  213. pr_err("ehv_pic_init: could not find epapr,hv-pic node\n");
  214. return;
  215. }
  216. if (!of_find_property(np, "has-external-proxy", NULL))
  217. coreint_flag = 0;
  218. ehv_pic = kzalloc(sizeof(struct ehv_pic), GFP_KERNEL);
  219. if (!ehv_pic) {
  220. of_node_put(np);
  221. return;
  222. }
  223. ehv_pic->irqhost = irq_domain_add_linear(np, NR_EHV_PIC_INTS,
  224. &ehv_pic_host_ops, ehv_pic);
  225. if (!ehv_pic->irqhost) {
  226. of_node_put(np);
  227. kfree(ehv_pic);
  228. return;
  229. }
  230. np2 = of_find_compatible_node(NULL, NULL, "fsl,hv-mpic-per-cpu");
  231. if (np2) {
  232. mpic_percpu_base_vaddr = of_iomap(np2, 0);
  233. if (!mpic_percpu_base_vaddr)
  234. pr_err("ehv_pic_init: of_iomap failed\n");
  235. of_node_put(np2);
  236. }
  237. ehv_pic->hc_irq = ehv_pic_irq_chip;
  238. ehv_pic->hc_irq.irq_set_affinity = ehv_pic_set_affinity;
  239. ehv_pic->coreint_flag = coreint_flag;
  240. global_ehv_pic = ehv_pic;
  241. irq_set_default_host(global_ehv_pic->irqhost);
  242. }