2 * Driver for ePAPR Embedded Hypervisor PIC
4 * Copyright 2008-2011 Freescale Semiconductor, Inc.
6 * Author: Ashish Kalra <ashish.kalra@freescale.com>
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.
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/irq.h>
17 #include <linux/smp.h>
18 #include <linux/interrupt.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
26 #include <asm/machdep.h>
27 #include <asm/ehv_pic.h>
28 #include <asm/fsl_hcalls.h>
30 #include "../../../kernel/irq/settings.h"
32 static struct ehv_pic
*global_ehv_pic
;
33 static DEFINE_SPINLOCK(ehv_pic_lock
);
35 static u32 hwirq_intspec
[NR_EHV_PIC_INTS
];
36 static u32 __iomem
*mpic_percpu_base_vaddr
;
38 #define IRQ_TYPE_MPIC_DIRECT 4
39 #define MPIC_EOI 0x00B0
42 * Linux descriptor level callbacks
45 void ehv_pic_unmask_irq(struct irq_data
*d
)
47 unsigned int src
= virq_to_hw(d
->irq
);
49 ev_int_set_mask(src
, 0);
52 void ehv_pic_mask_irq(struct irq_data
*d
)
54 unsigned int src
= virq_to_hw(d
->irq
);
56 ev_int_set_mask(src
, 1);
59 void ehv_pic_end_irq(struct irq_data
*d
)
61 unsigned int src
= virq_to_hw(d
->irq
);
66 void ehv_pic_direct_end_irq(struct irq_data
*d
)
68 out_be32(mpic_percpu_base_vaddr
+ MPIC_EOI
/ 4, 0);
71 int ehv_pic_set_affinity(struct irq_data
*d
, const struct cpumask
*dest
,
74 unsigned int src
= virq_to_hw(d
->irq
);
75 unsigned int config
, prio
, cpu_dest
;
76 int cpuid
= irq_choose_cpu(dest
);
79 spin_lock_irqsave(&ehv_pic_lock
, flags
);
80 ev_int_get_config(src
, &config
, &prio
, &cpu_dest
);
81 ev_int_set_config(src
, config
, prio
, cpuid
);
82 spin_unlock_irqrestore(&ehv_pic_lock
, flags
);
84 return IRQ_SET_MASK_OK
;
87 static unsigned int ehv_pic_type_to_vecpri(unsigned int type
)
89 /* Now convert sense value */
91 switch (type
& IRQ_TYPE_SENSE_MASK
) {
92 case IRQ_TYPE_EDGE_RISING
:
93 return EHV_PIC_INFO(VECPRI_SENSE_EDGE
) |
94 EHV_PIC_INFO(VECPRI_POLARITY_POSITIVE
);
96 case IRQ_TYPE_EDGE_FALLING
:
97 case IRQ_TYPE_EDGE_BOTH
:
98 return EHV_PIC_INFO(VECPRI_SENSE_EDGE
) |
99 EHV_PIC_INFO(VECPRI_POLARITY_NEGATIVE
);
101 case IRQ_TYPE_LEVEL_HIGH
:
102 return EHV_PIC_INFO(VECPRI_SENSE_LEVEL
) |
103 EHV_PIC_INFO(VECPRI_POLARITY_POSITIVE
);
105 case IRQ_TYPE_LEVEL_LOW
:
107 return EHV_PIC_INFO(VECPRI_SENSE_LEVEL
) |
108 EHV_PIC_INFO(VECPRI_POLARITY_NEGATIVE
);
112 int ehv_pic_set_irq_type(struct irq_data
*d
, unsigned int flow_type
)
114 unsigned int src
= virq_to_hw(d
->irq
);
115 struct irq_desc
*desc
= irq_to_desc(d
->irq
);
116 unsigned int vecpri
, vold
, vnew
, prio
, cpu_dest
;
119 if (flow_type
== IRQ_TYPE_NONE
)
120 flow_type
= IRQ_TYPE_LEVEL_LOW
;
122 irq_settings_clr_level(desc
);
123 irq_settings_set_trigger_mask(desc
, flow_type
);
124 if (flow_type
& (IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
))
125 irq_settings_set_level(desc
);
127 vecpri
= ehv_pic_type_to_vecpri(flow_type
);
129 spin_lock_irqsave(&ehv_pic_lock
, flags
);
130 ev_int_get_config(src
, &vold
, &prio
, &cpu_dest
);
131 vnew
= vold
& ~(EHV_PIC_INFO(VECPRI_POLARITY_MASK
) |
132 EHV_PIC_INFO(VECPRI_SENSE_MASK
));
136 * TODO : Add specific interface call for platform to set
137 * individual interrupt priorities.
138 * platform currently using static/default priority for all ints
143 ev_int_set_config(src
, vecpri
, prio
, cpu_dest
);
145 spin_unlock_irqrestore(&ehv_pic_lock
, flags
);
149 static struct irq_chip ehv_pic_irq_chip
= {
150 .irq_mask
= ehv_pic_mask_irq
,
151 .irq_unmask
= ehv_pic_unmask_irq
,
152 .irq_eoi
= ehv_pic_end_irq
,
153 .irq_set_type
= ehv_pic_set_irq_type
,
156 static struct irq_chip ehv_pic_direct_eoi_irq_chip
= {
157 .irq_mask
= ehv_pic_mask_irq
,
158 .irq_unmask
= ehv_pic_unmask_irq
,
159 .irq_eoi
= ehv_pic_direct_end_irq
,
160 .irq_set_type
= ehv_pic_set_irq_type
,
163 /* Return an interrupt vector or NO_IRQ if no interrupt is pending. */
164 unsigned int ehv_pic_get_irq(void)
168 BUG_ON(global_ehv_pic
== NULL
);
170 if (global_ehv_pic
->coreint_flag
)
171 irq
= mfspr(SPRN_EPR
); /* if core int mode */
173 ev_int_iack(0, &irq
); /* legacy mode */
175 if (irq
== 0xFFFF) /* 0xFFFF --> no irq is pending */
179 * this will also setup revmap[] in the slow path for the first
180 * time, next calls will always use fast path by indexing revmap
182 return irq_linear_revmap(global_ehv_pic
->irqhost
, irq
);
185 static int ehv_pic_host_match(struct irq_domain
*h
, struct device_node
*node
)
187 /* Exact match, unless ehv_pic node is NULL */
188 return h
->of_node
== NULL
|| h
->of_node
== node
;
191 static int ehv_pic_host_map(struct irq_domain
*h
, unsigned int virq
,
194 struct ehv_pic
*ehv_pic
= h
->host_data
;
195 struct irq_chip
*chip
;
198 chip
= &ehv_pic
->hc_irq
;
200 if (mpic_percpu_base_vaddr
)
201 if (hwirq_intspec
[hw
] & IRQ_TYPE_MPIC_DIRECT
)
202 chip
= &ehv_pic_direct_eoi_irq_chip
;
204 irq_set_chip_data(virq
, chip
);
206 * using handle_fasteoi_irq as our irq handler, this will
207 * only call the eoi callback and suitable for the MPIC
208 * controller which set ISR/IPR automatically and clear the
209 * highest priority active interrupt in ISR/IPR when we do
212 irq_set_chip_and_handler(virq
, chip
, handle_fasteoi_irq
);
214 /* Set default irq type */
215 irq_set_irq_type(virq
, IRQ_TYPE_NONE
);
220 static int ehv_pic_host_xlate(struct irq_domain
*h
, struct device_node
*ct
,
221 const u32
*intspec
, unsigned int intsize
,
222 irq_hw_number_t
*out_hwirq
, unsigned int *out_flags
)
226 * interrupt sense values coming from the guest device tree
227 * interrupt specifiers can have four possible sense and
228 * level encoding information and they need to
229 * be translated between firmware type & linux type.
232 static unsigned char map_of_senses_to_linux_irqtype
[4] = {
233 IRQ_TYPE_EDGE_FALLING
,
234 IRQ_TYPE_EDGE_RISING
,
239 *out_hwirq
= intspec
[0];
241 hwirq_intspec
[intspec
[0]] = intspec
[1];
242 *out_flags
= map_of_senses_to_linux_irqtype
[intspec
[1] &
243 ~IRQ_TYPE_MPIC_DIRECT
];
245 *out_flags
= IRQ_TYPE_NONE
;
251 static const struct irq_domain_ops ehv_pic_host_ops
= {
252 .match
= ehv_pic_host_match
,
253 .map
= ehv_pic_host_map
,
254 .xlate
= ehv_pic_host_xlate
,
257 void __init
ehv_pic_init(void)
259 struct device_node
*np
, *np2
;
260 struct ehv_pic
*ehv_pic
;
261 int coreint_flag
= 1;
263 np
= of_find_compatible_node(NULL
, NULL
, "epapr,hv-pic");
265 pr_err("ehv_pic_init: could not find epapr,hv-pic node\n");
269 if (!of_find_property(np
, "has-external-proxy", NULL
))
272 ehv_pic
= kzalloc(sizeof(struct ehv_pic
), GFP_KERNEL
);
278 ehv_pic
->irqhost
= irq_domain_add_linear(np
, NR_EHV_PIC_INTS
,
279 &ehv_pic_host_ops
, ehv_pic
);
280 if (!ehv_pic
->irqhost
) {
286 np2
= of_find_compatible_node(NULL
, NULL
, "fsl,hv-mpic-per-cpu");
288 mpic_percpu_base_vaddr
= of_iomap(np2
, 0);
289 if (!mpic_percpu_base_vaddr
)
290 pr_err("ehv_pic_init: of_iomap failed\n");
295 ehv_pic
->hc_irq
= ehv_pic_irq_chip
;
296 ehv_pic
->hc_irq
.irq_set_affinity
= ehv_pic_set_affinity
;
297 ehv_pic
->coreint_flag
= coreint_flag
;
299 global_ehv_pic
= ehv_pic
;
300 irq_set_default_host(global_ehv_pic
->irqhost
);