blob: 3deb3333d53e84d900b1ec82f4b0f4967f525305 [file] [log] [blame]
Thomas Gleixner7d828062011-04-03 11:42:53 +02001/*
2 * Library implementing the most common irq chip callback functions
3 *
4 * Copyright (C) 2011, Thomas Gleixner
5 */
6#include <linux/io.h>
7#include <linux/irq.h>
8#include <linux/slab.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -04009#include <linux/export.h>
Thomas Gleixner7d828062011-04-03 11:42:53 +020010#include <linux/interrupt.h>
11#include <linux/kernel_stat.h>
Thomas Gleixnercfefd212011-04-15 22:36:08 +020012#include <linux/syscore_ops.h>
Thomas Gleixner7d828062011-04-03 11:42:53 +020013
14#include "internals.h"
15
Thomas Gleixnercfefd212011-04-15 22:36:08 +020016static LIST_HEAD(gc_list);
17static DEFINE_RAW_SPINLOCK(gc_lock);
18
Thomas Gleixner7d828062011-04-03 11:42:53 +020019/**
20 * irq_gc_noop - NOOP function
21 * @d: irq_data
22 */
23void irq_gc_noop(struct irq_data *d)
24{
25}
26
27/**
28 * irq_gc_mask_disable_reg - Mask chip via disable register
29 * @d: irq_data
30 *
31 * Chip has separate enable/disable registers instead of a single mask
32 * register.
33 */
34void irq_gc_mask_disable_reg(struct irq_data *d)
35{
36 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Gerlando Falautocfeaa932013-05-06 14:30:17 +000037 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Thomas Gleixner966dc732013-05-06 14:30:22 +000038 u32 mask = d->mask;
Thomas Gleixner7d828062011-04-03 11:42:53 +020039
40 irq_gc_lock(gc);
Gerlando Falautocfeaa932013-05-06 14:30:17 +000041 irq_reg_writel(mask, gc->reg_base + ct->regs.disable);
Gerlando Falauto899f0e62013-05-06 14:30:19 +000042 *ct->mask_cache &= ~mask;
Thomas Gleixner7d828062011-04-03 11:42:53 +020043 irq_gc_unlock(gc);
44}
45
46/**
47 * irq_gc_mask_set_mask_bit - Mask chip via setting bit in mask register
48 * @d: irq_data
49 *
50 * Chip has a single mask register. Values of this register are cached
51 * and protected by gc->lock
52 */
53void irq_gc_mask_set_bit(struct irq_data *d)
54{
55 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Gerlando Falautocfeaa932013-05-06 14:30:17 +000056 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Thomas Gleixner966dc732013-05-06 14:30:22 +000057 u32 mask = d->mask;
Thomas Gleixner7d828062011-04-03 11:42:53 +020058
59 irq_gc_lock(gc);
Gerlando Falauto899f0e62013-05-06 14:30:19 +000060 *ct->mask_cache |= mask;
61 irq_reg_writel(*ct->mask_cache, gc->reg_base + ct->regs.mask);
Thomas Gleixner7d828062011-04-03 11:42:53 +020062 irq_gc_unlock(gc);
63}
64
65/**
66 * irq_gc_mask_set_mask_bit - Mask chip via clearing bit in mask register
67 * @d: irq_data
68 *
69 * Chip has a single mask register. Values of this register are cached
70 * and protected by gc->lock
71 */
72void irq_gc_mask_clr_bit(struct irq_data *d)
73{
74 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Gerlando Falautocfeaa932013-05-06 14:30:17 +000075 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Thomas Gleixner966dc732013-05-06 14:30:22 +000076 u32 mask = d->mask;
Thomas Gleixner7d828062011-04-03 11:42:53 +020077
78 irq_gc_lock(gc);
Gerlando Falauto899f0e62013-05-06 14:30:19 +000079 *ct->mask_cache &= ~mask;
80 irq_reg_writel(*ct->mask_cache, gc->reg_base + ct->regs.mask);
Thomas Gleixner7d828062011-04-03 11:42:53 +020081 irq_gc_unlock(gc);
82}
83
84/**
85 * irq_gc_unmask_enable_reg - Unmask chip via enable register
86 * @d: irq_data
87 *
88 * Chip has separate enable/disable registers instead of a single mask
89 * register.
90 */
91void irq_gc_unmask_enable_reg(struct irq_data *d)
92{
93 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Gerlando Falautocfeaa932013-05-06 14:30:17 +000094 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Thomas Gleixner966dc732013-05-06 14:30:22 +000095 u32 mask = d->mask;
Thomas Gleixner7d828062011-04-03 11:42:53 +020096
97 irq_gc_lock(gc);
Gerlando Falautocfeaa932013-05-06 14:30:17 +000098 irq_reg_writel(mask, gc->reg_base + ct->regs.enable);
Gerlando Falauto899f0e62013-05-06 14:30:19 +000099 *ct->mask_cache |= mask;
Thomas Gleixner7d828062011-04-03 11:42:53 +0200100 irq_gc_unlock(gc);
101}
102
103/**
Simon Guinot659fb322011-07-06 12:41:31 -0400104 * irq_gc_ack_set_bit - Ack pending interrupt via setting bit
Thomas Gleixner7d828062011-04-03 11:42:53 +0200105 * @d: irq_data
106 */
Simon Guinot659fb322011-07-06 12:41:31 -0400107void irq_gc_ack_set_bit(struct irq_data *d)
Thomas Gleixner7d828062011-04-03 11:42:53 +0200108{
109 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Gerlando Falautocfeaa932013-05-06 14:30:17 +0000110 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Thomas Gleixner966dc732013-05-06 14:30:22 +0000111 u32 mask = d->mask;
Thomas Gleixner7d828062011-04-03 11:42:53 +0200112
113 irq_gc_lock(gc);
Gerlando Falautocfeaa932013-05-06 14:30:17 +0000114 irq_reg_writel(mask, gc->reg_base + ct->regs.ack);
Thomas Gleixner7d828062011-04-03 11:42:53 +0200115 irq_gc_unlock(gc);
116}
117
118/**
Simon Guinot659fb322011-07-06 12:41:31 -0400119 * irq_gc_ack_clr_bit - Ack pending interrupt via clearing bit
120 * @d: irq_data
121 */
122void irq_gc_ack_clr_bit(struct irq_data *d)
123{
124 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Gerlando Falautocfeaa932013-05-06 14:30:17 +0000125 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Thomas Gleixner966dc732013-05-06 14:30:22 +0000126 u32 mask = ~d->mask;
Simon Guinot659fb322011-07-06 12:41:31 -0400127
128 irq_gc_lock(gc);
Gerlando Falautocfeaa932013-05-06 14:30:17 +0000129 irq_reg_writel(mask, gc->reg_base + ct->regs.ack);
Simon Guinot659fb322011-07-06 12:41:31 -0400130 irq_gc_unlock(gc);
131}
132
133/**
Thomas Gleixner7d828062011-04-03 11:42:53 +0200134 * irq_gc_mask_disable_reg_and_ack- Mask and ack pending interrupt
135 * @d: irq_data
136 */
137void irq_gc_mask_disable_reg_and_ack(struct irq_data *d)
138{
139 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Gerlando Falautocfeaa932013-05-06 14:30:17 +0000140 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Thomas Gleixner966dc732013-05-06 14:30:22 +0000141 u32 mask = d->mask;
Thomas Gleixner7d828062011-04-03 11:42:53 +0200142
143 irq_gc_lock(gc);
Gerlando Falautocfeaa932013-05-06 14:30:17 +0000144 irq_reg_writel(mask, gc->reg_base + ct->regs.mask);
145 irq_reg_writel(mask, gc->reg_base + ct->regs.ack);
Thomas Gleixner7d828062011-04-03 11:42:53 +0200146 irq_gc_unlock(gc);
147}
148
149/**
150 * irq_gc_eoi - EOI interrupt
151 * @d: irq_data
152 */
153void irq_gc_eoi(struct irq_data *d)
154{
155 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Gerlando Falautocfeaa932013-05-06 14:30:17 +0000156 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Thomas Gleixner966dc732013-05-06 14:30:22 +0000157 u32 mask = d->mask;
Thomas Gleixner7d828062011-04-03 11:42:53 +0200158
159 irq_gc_lock(gc);
Gerlando Falautocfeaa932013-05-06 14:30:17 +0000160 irq_reg_writel(mask, gc->reg_base + ct->regs.eoi);
Thomas Gleixner7d828062011-04-03 11:42:53 +0200161 irq_gc_unlock(gc);
162}
163
164/**
165 * irq_gc_set_wake - Set/clr wake bit for an interrupt
166 * @d: irq_data
167 *
168 * For chips where the wake from suspend functionality is not
169 * configured in a separate register and the wakeup active state is
170 * just stored in a bitmask.
171 */
172int irq_gc_set_wake(struct irq_data *d, unsigned int on)
173{
174 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Thomas Gleixner966dc732013-05-06 14:30:22 +0000175 u32 mask = d->mask;
Thomas Gleixner7d828062011-04-03 11:42:53 +0200176
177 if (!(mask & gc->wake_enabled))
178 return -EINVAL;
179
180 irq_gc_lock(gc);
181 if (on)
182 gc->wake_active |= mask;
183 else
184 gc->wake_active &= ~mask;
185 irq_gc_unlock(gc);
186 return 0;
187}
188
Thomas Gleixner3528d822013-05-06 14:30:25 +0000189static void
190irq_init_generic_chip(struct irq_chip_generic *gc, const char *name,
191 int num_ct, unsigned int irq_base,
192 void __iomem *reg_base, irq_flow_handler_t handler)
193{
194 raw_spin_lock_init(&gc->lock);
195 gc->num_ct = num_ct;
196 gc->irq_base = irq_base;
197 gc->reg_base = reg_base;
198 gc->chip_types->chip.name = name;
199 gc->chip_types->handler = handler;
200}
201
Thomas Gleixner7d828062011-04-03 11:42:53 +0200202/**
203 * irq_alloc_generic_chip - Allocate a generic chip and initialize it
204 * @name: Name of the irq chip
205 * @num_ct: Number of irq_chip_type instances associated with this
206 * @irq_base: Interrupt base nr for this chip
207 * @reg_base: Register base address (virtual)
208 * @handler: Default flow handler associated with this chip
209 *
210 * Returns an initialized irq_chip_generic structure. The chip defaults
211 * to the primary (index 0) irq_chip_type and @handler
212 */
213struct irq_chip_generic *
214irq_alloc_generic_chip(const char *name, int num_ct, unsigned int irq_base,
215 void __iomem *reg_base, irq_flow_handler_t handler)
216{
217 struct irq_chip_generic *gc;
218 unsigned long sz = sizeof(*gc) + num_ct * sizeof(struct irq_chip_type);
219
220 gc = kzalloc(sz, GFP_KERNEL);
221 if (gc) {
Thomas Gleixner3528d822013-05-06 14:30:25 +0000222 irq_init_generic_chip(gc, name, num_ct, irq_base, reg_base,
223 handler);
Thomas Gleixner7d828062011-04-03 11:42:53 +0200224 }
225 return gc;
226}
Nobuhiro Iwamatsu825de2e2011-10-17 11:08:46 +0900227EXPORT_SYMBOL_GPL(irq_alloc_generic_chip);
Thomas Gleixner7d828062011-04-03 11:42:53 +0200228
Thomas Gleixner3528d822013-05-06 14:30:25 +0000229static void
230irq_gc_init_mask_cache(struct irq_chip_generic *gc, enum irq_gc_flags flags)
231{
232 struct irq_chip_type *ct = gc->chip_types;
233 u32 *mskptr = &gc->mask_cache, mskreg = ct->regs.mask;
234 int i;
235
236 for (i = 0; i < gc->num_ct; i++) {
237 if (flags & IRQ_GC_MASK_CACHE_PER_TYPE) {
238 mskptr = &ct[i].mask_cache_priv;
239 mskreg = ct[i].regs.mask;
240 }
241 ct[i].mask_cache = mskptr;
242 if (flags & IRQ_GC_INIT_MASK_CACHE)
243 *mskptr = irq_reg_readl(gc->reg_base + mskreg);
244 }
245}
246
Thomas Gleixner7d828062011-04-03 11:42:53 +0200247/*
248 * Separate lockdep class for interrupt chip which can nest irq_desc
249 * lock.
250 */
251static struct lock_class_key irq_nested_lock_class;
252
253/**
254 * irq_setup_generic_chip - Setup a range of interrupts with a generic chip
255 * @gc: Generic irq chip holding all data
256 * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
257 * @flags: Flags for initialization
258 * @clr: IRQ_* bits to clear
259 * @set: IRQ_* bits to set
260 *
261 * Set up max. 32 interrupts starting from gc->irq_base. Note, this
262 * initializes all interrupts to the primary irq_chip_type and its
263 * associated handler.
264 */
265void irq_setup_generic_chip(struct irq_chip_generic *gc, u32 msk,
266 enum irq_gc_flags flags, unsigned int clr,
267 unsigned int set)
268{
269 struct irq_chip_type *ct = gc->chip_types;
Thomas Gleixnerd0051812013-05-06 14:30:24 +0000270 struct irq_chip *chip = &ct->chip;
Thomas Gleixner7d828062011-04-03 11:42:53 +0200271 unsigned int i;
272
Thomas Gleixnercfefd212011-04-15 22:36:08 +0200273 raw_spin_lock(&gc_lock);
274 list_add_tail(&gc->list, &gc_list);
275 raw_spin_unlock(&gc_lock);
276
Thomas Gleixner3528d822013-05-06 14:30:25 +0000277 irq_gc_init_mask_cache(gc, flags);
Gerlando Falauto899f0e62013-05-06 14:30:19 +0000278
Thomas Gleixner7d828062011-04-03 11:42:53 +0200279 for (i = gc->irq_base; msk; msk >>= 1, i++) {
jhbird.choi@samsung.com1dd75f92011-07-21 15:29:14 +0900280 if (!(msk & 0x01))
Thomas Gleixner7d828062011-04-03 11:42:53 +0200281 continue;
282
283 if (flags & IRQ_GC_INIT_NESTED_LOCK)
284 irq_set_lockdep_class(i, &irq_nested_lock_class);
285
Thomas Gleixner966dc732013-05-06 14:30:22 +0000286 if (!(flags & IRQ_GC_NO_MASK)) {
287 struct irq_data *d = irq_get_irq_data(i);
288
Thomas Gleixnerd0051812013-05-06 14:30:24 +0000289 if (chip->irq_calc_mask)
290 chip->irq_calc_mask(d);
291 else
292 d->mask = 1 << (i - gc->irq_base);
Thomas Gleixner966dc732013-05-06 14:30:22 +0000293 }
Thomas Gleixnerd0051812013-05-06 14:30:24 +0000294 irq_set_chip_and_handler(i, chip, ct->handler);
Thomas Gleixner7d828062011-04-03 11:42:53 +0200295 irq_set_chip_data(i, gc);
296 irq_modify_status(i, clr, set);
297 }
298 gc->irq_cnt = i - gc->irq_base;
299}
Nobuhiro Iwamatsu825de2e2011-10-17 11:08:46 +0900300EXPORT_SYMBOL_GPL(irq_setup_generic_chip);
Thomas Gleixner7d828062011-04-03 11:42:53 +0200301
302/**
303 * irq_setup_alt_chip - Switch to alternative chip
304 * @d: irq_data for this interrupt
305 * @type Flow type to be initialized
306 *
307 * Only to be called from chip->irq_set_type() callbacks.
308 */
309int irq_setup_alt_chip(struct irq_data *d, unsigned int type)
310{
311 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
312 struct irq_chip_type *ct = gc->chip_types;
313 unsigned int i;
314
315 for (i = 0; i < gc->num_ct; i++, ct++) {
316 if (ct->type & type) {
317 d->chip = &ct->chip;
318 irq_data_to_desc(d)->handle_irq = ct->handler;
319 return 0;
320 }
321 }
322 return -EINVAL;
323}
Nobuhiro Iwamatsu825de2e2011-10-17 11:08:46 +0900324EXPORT_SYMBOL_GPL(irq_setup_alt_chip);
Thomas Gleixnercfefd212011-04-15 22:36:08 +0200325
326/**
327 * irq_remove_generic_chip - Remove a chip
328 * @gc: Generic irq chip holding all data
329 * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
330 * @clr: IRQ_* bits to clear
331 * @set: IRQ_* bits to set
332 *
333 * Remove up to 32 interrupts starting from gc->irq_base.
334 */
335void irq_remove_generic_chip(struct irq_chip_generic *gc, u32 msk,
336 unsigned int clr, unsigned int set)
337{
338 unsigned int i = gc->irq_base;
339
340 raw_spin_lock(&gc_lock);
341 list_del(&gc->list);
342 raw_spin_unlock(&gc_lock);
343
344 for (; msk; msk >>= 1, i++) {
jhbird.choi@samsung.com1dd75f92011-07-21 15:29:14 +0900345 if (!(msk & 0x01))
Thomas Gleixnercfefd212011-04-15 22:36:08 +0200346 continue;
347
348 /* Remove handler first. That will mask the irq line */
349 irq_set_handler(i, NULL);
350 irq_set_chip(i, &no_irq_chip);
351 irq_set_chip_data(i, NULL);
352 irq_modify_status(i, clr, set);
353 }
354}
Nobuhiro Iwamatsu825de2e2011-10-17 11:08:46 +0900355EXPORT_SYMBOL_GPL(irq_remove_generic_chip);
Thomas Gleixnercfefd212011-04-15 22:36:08 +0200356
357#ifdef CONFIG_PM
358static int irq_gc_suspend(void)
359{
360 struct irq_chip_generic *gc;
361
362 list_for_each_entry(gc, &gc_list, list) {
363 struct irq_chip_type *ct = gc->chip_types;
364
365 if (ct->chip.irq_suspend)
366 ct->chip.irq_suspend(irq_get_irq_data(gc->irq_base));
367 }
368 return 0;
369}
370
371static void irq_gc_resume(void)
372{
373 struct irq_chip_generic *gc;
374
375 list_for_each_entry(gc, &gc_list, list) {
376 struct irq_chip_type *ct = gc->chip_types;
377
378 if (ct->chip.irq_resume)
379 ct->chip.irq_resume(irq_get_irq_data(gc->irq_base));
380 }
381}
382#else
383#define irq_gc_suspend NULL
384#define irq_gc_resume NULL
385#endif
386
387static void irq_gc_shutdown(void)
388{
389 struct irq_chip_generic *gc;
390
391 list_for_each_entry(gc, &gc_list, list) {
392 struct irq_chip_type *ct = gc->chip_types;
393
394 if (ct->chip.irq_pm_shutdown)
395 ct->chip.irq_pm_shutdown(irq_get_irq_data(gc->irq_base));
396 }
397}
398
399static struct syscore_ops irq_gc_syscore_ops = {
400 .suspend = irq_gc_suspend,
401 .resume = irq_gc_resume,
402 .shutdown = irq_gc_shutdown,
403};
404
405static int __init irq_gc_init_ops(void)
406{
407 register_syscore_ops(&irq_gc_syscore_ops);
408 return 0;
409}
410device_initcall(irq_gc_init_ops);