ehv_pic.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Driver for ePAPR Embedded Hypervisor PIC
  3. *
  4. * Copyright 2008-2011 Freescale Semiconductor, Inc.
  5. *
  6. * Author: Ashish Kalra <[email protected]>
  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/irqdomain.h>
  17. #include <linux/smp.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <asm/io.h>
  24. #include <asm/irq.h>
  25. #include <asm/smp.h>
  26. #include <asm/machdep.h>
  27. #include <asm/ehv_pic.h>
  28. #include <asm/fsl_hcalls.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. unsigned int vecpri, vold, vnew, prio, cpu_dest;
  94. unsigned long flags;
  95. if (flow_type == IRQ_TYPE_NONE)
  96. flow_type = IRQ_TYPE_LEVEL_LOW;
  97. irqd_set_trigger_type(d, flow_type);
  98. vecpri = ehv_pic_type_to_vecpri(flow_type);
  99. spin_lock_irqsave(&ehv_pic_lock, flags);
  100. ev_int_get_config(src, &vold, &prio, &cpu_dest);
  101. vnew = vold & ~(EHV_PIC_INFO(VECPRI_POLARITY_MASK) |
  102. EHV_PIC_INFO(VECPRI_SENSE_MASK));
  103. vnew |= vecpri;
  104. /*
  105. * TODO : Add specific interface call for platform to set
  106. * individual interrupt priorities.
  107. * platform currently using static/default priority for all ints
  108. */
  109. prio = 8;
  110. ev_int_set_config(src, vecpri, prio, cpu_dest);
  111. spin_unlock_irqrestore(&ehv_pic_lock, flags);
  112. return IRQ_SET_MASK_OK_NOCOPY;
  113. }
  114. static struct irq_chip ehv_pic_irq_chip = {
  115. .irq_mask = ehv_pic_mask_irq,
  116. .irq_unmask = ehv_pic_unmask_irq,
  117. .irq_eoi = ehv_pic_end_irq,
  118. .irq_set_type = ehv_pic_set_irq_type,
  119. };
  120. static struct irq_chip ehv_pic_direct_eoi_irq_chip = {
  121. .irq_mask = ehv_pic_mask_irq,
  122. .irq_unmask = ehv_pic_unmask_irq,
  123. .irq_eoi = ehv_pic_direct_end_irq,
  124. .irq_set_type = ehv_pic_set_irq_type,
  125. };
  126. /* Return an interrupt vector or 0 if no interrupt is pending. */
  127. unsigned int ehv_pic_get_irq(void)
  128. {
  129. int irq;
  130. BUG_ON(global_ehv_pic == NULL);
  131. if (global_ehv_pic->coreint_flag)
  132. irq = mfspr(SPRN_EPR); /* if core int mode */
  133. else
  134. ev_int_iack(0, &irq); /* legacy mode */
  135. if (irq == 0xFFFF) /* 0xFFFF --> no irq is pending */
  136. return 0;
  137. /*
  138. * this will also setup revmap[] in the slow path for the first
  139. * time, next calls will always use fast path by indexing revmap
  140. */
  141. return irq_linear_revmap(global_ehv_pic->irqhost, irq);
  142. }
  143. static int ehv_pic_host_match(struct irq_domain *h, struct device_node *node,
  144. enum irq_domain_bus_token bus_token)
  145. {
  146. /* Exact match, unless ehv_pic node is NULL */
  147. struct device_node *of_node = irq_domain_get_of_node(h);
  148. return of_node == NULL || of_node == node;
  149. }
  150. static int ehv_pic_host_map(struct irq_domain *h, unsigned int virq,
  151. irq_hw_number_t hw)
  152. {
  153. struct ehv_pic *ehv_pic = h->host_data;
  154. struct irq_chip *chip;
  155. /* Default chip */
  156. chip = &ehv_pic->hc_irq;
  157. if (mpic_percpu_base_vaddr)
  158. if (hwirq_intspec[hw] & IRQ_TYPE_MPIC_DIRECT)
  159. chip = &ehv_pic_direct_eoi_irq_chip;
  160. irq_set_chip_data(virq, chip);
  161. /*
  162. * using handle_fasteoi_irq as our irq handler, this will
  163. * only call the eoi callback and suitable for the MPIC
  164. * controller which set ISR/IPR automatically and clear the
  165. * highest priority active interrupt in ISR/IPR when we do
  166. * a specific eoi
  167. */
  168. irq_set_chip_and_handler(virq, chip, handle_fasteoi_irq);
  169. /* Set default irq type */
  170. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  171. return 0;
  172. }
  173. static int ehv_pic_host_xlate(struct irq_domain *h, struct device_node *ct,
  174. const u32 *intspec, unsigned int intsize,
  175. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  176. {
  177. /*
  178. * interrupt sense values coming from the guest device tree
  179. * interrupt specifiers can have four possible sense and
  180. * level encoding information and they need to
  181. * be translated between firmware type & linux type.
  182. */
  183. static unsigned char map_of_senses_to_linux_irqtype[4] = {
  184. IRQ_TYPE_EDGE_FALLING,
  185. IRQ_TYPE_EDGE_RISING,
  186. IRQ_TYPE_LEVEL_LOW,
  187. IRQ_TYPE_LEVEL_HIGH,
  188. };
  189. *out_hwirq = intspec[0];
  190. if (intsize > 1) {
  191. hwirq_intspec[intspec[0]] = intspec[1];
  192. *out_flags = map_of_senses_to_linux_irqtype[intspec[1] &
  193. ~IRQ_TYPE_MPIC_DIRECT];
  194. } else {
  195. *out_flags = IRQ_TYPE_NONE;
  196. }
  197. return 0;
  198. }
  199. static const struct irq_domain_ops ehv_pic_host_ops = {
  200. .match = ehv_pic_host_match,
  201. .map = ehv_pic_host_map,
  202. .xlate = ehv_pic_host_xlate,
  203. };
  204. void __init ehv_pic_init(void)
  205. {
  206. struct device_node *np, *np2;
  207. struct ehv_pic *ehv_pic;
  208. int coreint_flag = 1;
  209. np = of_find_compatible_node(NULL, NULL, "epapr,hv-pic");
  210. if (!np) {
  211. pr_err("ehv_pic_init: could not find epapr,hv-pic node\n");
  212. return;
  213. }
  214. if (!of_find_property(np, "has-external-proxy", NULL))
  215. coreint_flag = 0;
  216. ehv_pic = kzalloc(sizeof(struct ehv_pic), GFP_KERNEL);
  217. if (!ehv_pic) {
  218. of_node_put(np);
  219. return;
  220. }
  221. ehv_pic->irqhost = irq_domain_add_linear(np, NR_EHV_PIC_INTS,
  222. &ehv_pic_host_ops, ehv_pic);
  223. if (!ehv_pic->irqhost) {
  224. of_node_put(np);
  225. kfree(ehv_pic);
  226. return;
  227. }
  228. np2 = of_find_compatible_node(NULL, NULL, "fsl,hv-mpic-per-cpu");
  229. if (np2) {
  230. mpic_percpu_base_vaddr = of_iomap(np2, 0);
  231. if (!mpic_percpu_base_vaddr)
  232. pr_err("ehv_pic_init: of_iomap failed\n");
  233. of_node_put(np2);
  234. }
  235. ehv_pic->hc_irq = ehv_pic_irq_chip;
  236. ehv_pic->hc_irq.irq_set_affinity = ehv_pic_set_affinity;
  237. ehv_pic->coreint_flag = coreint_flag;
  238. global_ehv_pic = ehv_pic;
  239. irq_set_default_host(global_ehv_pic->irqhost);
  240. }