generic-chip.c 9.6 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Library implementing the most common irq chip callback functions
 *
 * Copyright (C) 2011, Thomas Gleixner
 */
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/slab.h>
9
#include <linux/export.h>
10 11
#include <linux/interrupt.h>
#include <linux/kernel_stat.h>
12
#include <linux/syscore_ops.h>
13 14 15

#include "internals.h"

16 17 18
static LIST_HEAD(gc_list);
static DEFINE_RAW_SPINLOCK(gc_lock);

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/**
 * irq_gc_noop - NOOP function
 * @d: irq_data
 */
void irq_gc_noop(struct irq_data *d)
{
}

/**
 * irq_gc_mask_disable_reg - Mask chip via disable register
 * @d: irq_data
 *
 * Chip has separate enable/disable registers instead of a single mask
 * register.
 */
void irq_gc_mask_disable_reg(struct irq_data *d)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
37
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
38
	u32 mask = d->mask;
39 40

	irq_gc_lock(gc);
41
	irq_reg_writel(mask, gc->reg_base + ct->regs.disable);
42
	*ct->mask_cache &= ~mask;
43 44 45 46 47 48 49 50 51 52 53 54 55
	irq_gc_unlock(gc);
}

/**
 * irq_gc_mask_set_mask_bit - Mask chip via setting bit in mask register
 * @d: irq_data
 *
 * Chip has a single mask register. Values of this register are cached
 * and protected by gc->lock
 */
void irq_gc_mask_set_bit(struct irq_data *d)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
56
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
57
	u32 mask = d->mask;
58 59

	irq_gc_lock(gc);
60 61
	*ct->mask_cache |= mask;
	irq_reg_writel(*ct->mask_cache, gc->reg_base + ct->regs.mask);
62 63 64 65 66 67 68 69 70 71 72 73 74
	irq_gc_unlock(gc);
}

/**
 * irq_gc_mask_set_mask_bit - Mask chip via clearing bit in mask register
 * @d: irq_data
 *
 * Chip has a single mask register. Values of this register are cached
 * and protected by gc->lock
 */
void irq_gc_mask_clr_bit(struct irq_data *d)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
75
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
76
	u32 mask = d->mask;
77 78

	irq_gc_lock(gc);
79 80
	*ct->mask_cache &= ~mask;
	irq_reg_writel(*ct->mask_cache, gc->reg_base + ct->regs.mask);
81 82 83 84 85 86 87 88 89 90 91 92 93
	irq_gc_unlock(gc);
}

/**
 * irq_gc_unmask_enable_reg - Unmask chip via enable register
 * @d: irq_data
 *
 * Chip has separate enable/disable registers instead of a single mask
 * register.
 */
void irq_gc_unmask_enable_reg(struct irq_data *d)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
94
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
95
	u32 mask = d->mask;
96 97

	irq_gc_lock(gc);
98
	irq_reg_writel(mask, gc->reg_base + ct->regs.enable);
99
	*ct->mask_cache |= mask;
100 101 102 103
	irq_gc_unlock(gc);
}

/**
104
 * irq_gc_ack_set_bit - Ack pending interrupt via setting bit
105 106
 * @d: irq_data
 */
107
void irq_gc_ack_set_bit(struct irq_data *d)
108 109
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
110
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
111
	u32 mask = d->mask;
112 113

	irq_gc_lock(gc);
114
	irq_reg_writel(mask, gc->reg_base + ct->regs.ack);
115 116 117
	irq_gc_unlock(gc);
}

118 119 120 121 122 123 124
/**
 * irq_gc_ack_clr_bit - Ack pending interrupt via clearing bit
 * @d: irq_data
 */
void irq_gc_ack_clr_bit(struct irq_data *d)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
125
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
126
	u32 mask = ~d->mask;
127 128

	irq_gc_lock(gc);
129
	irq_reg_writel(mask, gc->reg_base + ct->regs.ack);
130 131 132
	irq_gc_unlock(gc);
}

133 134 135 136 137 138 139
/**
 * irq_gc_mask_disable_reg_and_ack- Mask and ack pending interrupt
 * @d: irq_data
 */
void irq_gc_mask_disable_reg_and_ack(struct irq_data *d)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
140
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
141
	u32 mask = d->mask;
142 143

	irq_gc_lock(gc);
144 145
	irq_reg_writel(mask, gc->reg_base + ct->regs.mask);
	irq_reg_writel(mask, gc->reg_base + ct->regs.ack);
146 147 148 149 150 151 152 153 154 155
	irq_gc_unlock(gc);
}

/**
 * irq_gc_eoi - EOI interrupt
 * @d: irq_data
 */
void irq_gc_eoi(struct irq_data *d)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
156
	struct irq_chip_type *ct = irq_data_get_chip_type(d);
157
	u32 mask = d->mask;
158 159

	irq_gc_lock(gc);
160
	irq_reg_writel(mask, gc->reg_base + ct->regs.eoi);
161 162 163 164 165 166 167 168 169 170 171 172 173 174
	irq_gc_unlock(gc);
}

/**
 * irq_gc_set_wake - Set/clr wake bit for an interrupt
 * @d: irq_data
 *
 * For chips where the wake from suspend functionality is not
 * configured in a separate register and the wakeup active state is
 * just stored in a bitmask.
 */
int irq_gc_set_wake(struct irq_data *d, unsigned int on)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
175
	u32 mask = d->mask;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

	if (!(mask & gc->wake_enabled))
		return -EINVAL;

	irq_gc_lock(gc);
	if (on)
		gc->wake_active |= mask;
	else
		gc->wake_active &= ~mask;
	irq_gc_unlock(gc);
	return 0;
}

/**
 * irq_alloc_generic_chip - Allocate a generic chip and initialize it
 * @name:	Name of the irq chip
 * @num_ct:	Number of irq_chip_type instances associated with this
 * @irq_base:	Interrupt base nr for this chip
 * @reg_base:	Register base address (virtual)
 * @handler:	Default flow handler associated with this chip
 *
 * Returns an initialized irq_chip_generic structure. The chip defaults
 * to the primary (index 0) irq_chip_type and @handler
 */
struct irq_chip_generic *
irq_alloc_generic_chip(const char *name, int num_ct, unsigned int irq_base,
		       void __iomem *reg_base, irq_flow_handler_t handler)
{
	struct irq_chip_generic *gc;
	unsigned long sz = sizeof(*gc) + num_ct * sizeof(struct irq_chip_type);

	gc = kzalloc(sz, GFP_KERNEL);
	if (gc) {
		raw_spin_lock_init(&gc->lock);
		gc->num_ct = num_ct;
		gc->irq_base = irq_base;
		gc->reg_base = reg_base;
		gc->chip_types->chip.name = name;
		gc->chip_types->handler = handler;
	}
	return gc;
}
218
EXPORT_SYMBOL_GPL(irq_alloc_generic_chip);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

/*
 * Separate lockdep class for interrupt chip which can nest irq_desc
 * lock.
 */
static struct lock_class_key irq_nested_lock_class;

/**
 * irq_setup_generic_chip - Setup a range of interrupts with a generic chip
 * @gc:		Generic irq chip holding all data
 * @msk:	Bitmask holding the irqs to initialize relative to gc->irq_base
 * @flags:	Flags for initialization
 * @clr:	IRQ_* bits to clear
 * @set:	IRQ_* bits to set
 *
 * Set up max. 32 interrupts starting from gc->irq_base. Note, this
 * initializes all interrupts to the primary irq_chip_type and its
 * associated handler.
 */
void irq_setup_generic_chip(struct irq_chip_generic *gc, u32 msk,
			    enum irq_gc_flags flags, unsigned int clr,
			    unsigned int set)
{
	struct irq_chip_type *ct = gc->chip_types;
	unsigned int i;
244
	u32 *mskptr = &gc->mask_cache, mskreg = ct->regs.mask;
245

246 247 248 249
	raw_spin_lock(&gc_lock);
	list_add_tail(&gc->list, &gc_list);
	raw_spin_unlock(&gc_lock);

250 251 252 253 254 255 256 257 258
	for (i = 0; i < gc->num_ct; i++) {
		if (flags & IRQ_GC_MASK_CACHE_PER_TYPE) {
			mskptr = &ct[i].mask_cache_priv;
			mskreg = ct[i].regs.mask;
		}
		ct[i].mask_cache = mskptr;
		if (flags & IRQ_GC_INIT_MASK_CACHE)
			*mskptr = irq_reg_readl(gc->reg_base + mskreg);
	}
259

260
	for (i = gc->irq_base; msk; msk >>= 1, i++) {
261
		if (!(msk & 0x01))
262 263 264 265 266
			continue;

		if (flags & IRQ_GC_INIT_NESTED_LOCK)
			irq_set_lockdep_class(i, &irq_nested_lock_class);

267 268 269 270 271
		if (!(flags & IRQ_GC_NO_MASK)) {
			struct irq_data *d = irq_get_irq_data(i);

			d->mask = 1 << (i - gc->irq_base);
		}
272 273 274 275 276 277
		irq_set_chip_and_handler(i, &ct->chip, ct->handler);
		irq_set_chip_data(i, gc);
		irq_modify_status(i, clr, set);
	}
	gc->irq_cnt = i - gc->irq_base;
}
278
EXPORT_SYMBOL_GPL(irq_setup_generic_chip);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

/**
 * irq_setup_alt_chip - Switch to alternative chip
 * @d:		irq_data for this interrupt
 * @type	Flow type to be initialized
 *
 * Only to be called from chip->irq_set_type() callbacks.
 */
int irq_setup_alt_chip(struct irq_data *d, unsigned int type)
{
	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
	struct irq_chip_type *ct = gc->chip_types;
	unsigned int i;

	for (i = 0; i < gc->num_ct; i++, ct++) {
		if (ct->type & type) {
			d->chip = &ct->chip;
			irq_data_to_desc(d)->handle_irq = ct->handler;
			return 0;
		}
	}
	return -EINVAL;
}
302
EXPORT_SYMBOL_GPL(irq_setup_alt_chip);
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

/**
 * irq_remove_generic_chip - Remove a chip
 * @gc:		Generic irq chip holding all data
 * @msk:	Bitmask holding the irqs to initialize relative to gc->irq_base
 * @clr:	IRQ_* bits to clear
 * @set:	IRQ_* bits to set
 *
 * Remove up to 32 interrupts starting from gc->irq_base.
 */
void irq_remove_generic_chip(struct irq_chip_generic *gc, u32 msk,
			     unsigned int clr, unsigned int set)
{
	unsigned int i = gc->irq_base;

	raw_spin_lock(&gc_lock);
	list_del(&gc->list);
	raw_spin_unlock(&gc_lock);

	for (; msk; msk >>= 1, i++) {
323
		if (!(msk & 0x01))
324 325 326 327 328 329 330 331 332
			continue;

		/* Remove handler first. That will mask the irq line */
		irq_set_handler(i, NULL);
		irq_set_chip(i, &no_irq_chip);
		irq_set_chip_data(i, NULL);
		irq_modify_status(i, clr, set);
	}
}
333
EXPORT_SYMBOL_GPL(irq_remove_generic_chip);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

#ifdef CONFIG_PM
static int irq_gc_suspend(void)
{
	struct irq_chip_generic *gc;

	list_for_each_entry(gc, &gc_list, list) {
		struct irq_chip_type *ct = gc->chip_types;

		if (ct->chip.irq_suspend)
			ct->chip.irq_suspend(irq_get_irq_data(gc->irq_base));
	}
	return 0;
}

static void irq_gc_resume(void)
{
	struct irq_chip_generic *gc;

	list_for_each_entry(gc, &gc_list, list) {
		struct irq_chip_type *ct = gc->chip_types;

		if (ct->chip.irq_resume)
			ct->chip.irq_resume(irq_get_irq_data(gc->irq_base));
	}
}
#else
#define irq_gc_suspend NULL
#define irq_gc_resume NULL
#endif

static void irq_gc_shutdown(void)
{
	struct irq_chip_generic *gc;

	list_for_each_entry(gc, &gc_list, list) {
		struct irq_chip_type *ct = gc->chip_types;

		if (ct->chip.irq_pm_shutdown)
			ct->chip.irq_pm_shutdown(irq_get_irq_data(gc->irq_base));
	}
}

static struct syscore_ops irq_gc_syscore_ops = {
	.suspend = irq_gc_suspend,
	.resume = irq_gc_resume,
	.shutdown = irq_gc_shutdown,
};

static int __init irq_gc_init_ops(void)
{
	register_syscore_ops(&irq_gc_syscore_ops);
	return 0;
}
device_initcall(irq_gc_init_ops);