core.c 11.7 KB
Newer Older
P
Paul Mundt 已提交
1 2 3 4
/*
 * Shared interrupt handling code for IPR and INTC2 types of IRQs.
 *
 * Copyright (C) 2007, 2008 Magnus Damm
P
Paul Mundt 已提交
5
 * Copyright (C) 2009 - 2012 Paul Mundt
P
Paul Mundt 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Based on intc2.c and ipr.c
 *
 * Copyright (C) 1999  Niibe Yutaka & Takeshi Yaegashi
 * Copyright (C) 2000  Kazumoto Kojima
 * Copyright (C) 2001  David J. Mckay (david.mckay@st.com)
 * Copyright (C) 2003  Takashi Kusuda <kusuda-takashi@hitachi-ul.co.jp>
 * Copyright (C) 2005, 2006  Paul Mundt
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
#define pr_fmt(fmt) "intc: " fmt

#include <linux/init.h>
#include <linux/irq.h>
#include <linux/io.h>
#include <linux/slab.h>
25
#include <linux/stat.h>
P
Paul Mundt 已提交
26 27
#include <linux/interrupt.h>
#include <linux/sh_intc.h>
28
#include <linux/irqdomain.h>
29
#include <linux/device.h>
30
#include <linux/syscore_ops.h>
P
Paul Mundt 已提交
31 32 33
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/radix-tree.h>
34
#include <linux/export.h>
P
Paul Mundt 已提交
35
#include <linux/sort.h>
P
Paul Mundt 已提交
36 37 38 39
#include "internals.h"

LIST_HEAD(intc_list);
DEFINE_RAW_SPINLOCK(intc_big_lock);
40
static unsigned int nr_intc_controllers;
P
Paul Mundt 已提交
41 42 43 44 45 46

/*
 * Default priority level
 * - this needs to be at least 2 for 5-bit priorities on 7780
 */
static unsigned int default_prio_level = 2;	/* 2 - 16 */
47
static unsigned int intc_prio_level[INTC_NR_IRQS];	/* for now */
P
Paul Mundt 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

unsigned int intc_get_dfl_prio_level(void)
{
	return default_prio_level;
}

unsigned int intc_get_prio_level(unsigned int irq)
{
	return intc_prio_level[irq];
}

void intc_set_prio_level(unsigned int irq, unsigned int level)
{
	unsigned long flags;

	raw_spin_lock_irqsave(&intc_big_lock, flags);
	intc_prio_level[irq] = level;
	raw_spin_unlock_irqrestore(&intc_big_lock, flags);
}

static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
{
70
	generic_handle_irq((unsigned int)irq_get_handler_data(irq));
P
Paul Mundt 已提交
71 72 73 74 75 76 77 78
}

static void __init intc_register_irq(struct intc_desc *desc,
				     struct intc_desc_int *d,
				     intc_enum enum_id,
				     unsigned int irq)
{
	struct intc_handle_int *hp;
P
Paul Mundt 已提交
79
	struct irq_data *irq_data;
P
Paul Mundt 已提交
80 81 82 83 84 85 86
	unsigned int data[2], primary;
	unsigned long flags;

	/*
	 * Register the IRQ position with the global IRQ map, then insert
	 * it in to the radix tree.
	 */
87
	irq_reserve_irq(irq);
P
Paul Mundt 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

	raw_spin_lock_irqsave(&intc_big_lock, flags);
	radix_tree_insert(&d->tree, enum_id, intc_irq_xlate_get(irq));
	raw_spin_unlock_irqrestore(&intc_big_lock, flags);

	/*
	 * Prefer single interrupt source bitmap over other combinations:
	 *
	 * 1. bitmap, single interrupt source
	 * 2. priority, single interrupt source
	 * 3. bitmap, multiple interrupt sources (groups)
	 * 4. priority, multiple interrupt sources (groups)
	 */
	data[0] = intc_get_mask_handle(desc, d, enum_id, 0);
	data[1] = intc_get_prio_handle(desc, d, enum_id, 0);

	primary = 0;
	if (!data[0] && data[1])
		primary = 1;

	if (!data[0] && !data[1])
		pr_warning("missing unique irq mask for irq %d (vect 0x%04x)\n",
			   irq, irq2evt(irq));

	data[0] = data[0] ? data[0] : intc_get_mask_handle(desc, d, enum_id, 1);
	data[1] = data[1] ? data[1] : intc_get_prio_handle(desc, d, enum_id, 1);

	if (!data[primary])
		primary ^= 1;

	BUG_ON(!data[primary]); /* must have primary masking method */

P
Paul Mundt 已提交
120 121
	irq_data = irq_get_irq_data(irq);

P
Paul Mundt 已提交
122
	disable_irq_nosync(irq);
123 124 125
	irq_set_chip_and_handler_name(irq, &d->chip, handle_level_irq,
				      "level");
	irq_set_chip_data(irq, (void *)data[primary]);
P
Paul Mundt 已提交
126 127 128 129 130 131 132 133

	/*
	 * set priority level
	 */
	intc_set_prio_level(irq, intc_get_dfl_prio_level());

	/* enable secondary masking method if present */
	if (data[!primary])
P
Paul Mundt 已提交
134
		_intc_enable(irq_data, data[!primary]);
P
Paul Mundt 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

	/* add irq to d->prio list if priority is available */
	if (data[1]) {
		hp = d->prio + d->nr_prio;
		hp->irq = irq;
		hp->handle = data[1];

		if (primary) {
			/*
			 * only secondary priority should access registers, so
			 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
			 */
			hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
			hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
		}
		d->nr_prio++;
	}

	/* add irq to d->sense list if sense is available */
	data[0] = intc_get_sense_handle(desc, d, enum_id);
	if (data[0]) {
		(d->sense + d->nr_sense)->irq = irq;
		(d->sense + d->nr_sense)->handle = data[0];
		d->nr_sense++;
	}

	/* irq should be disabled by default */
P
Paul Mundt 已提交
162
	d->chip.irq_mask(irq_data);
P
Paul Mundt 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 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

	intc_set_ack_handle(irq, desc, d, enum_id);
	intc_set_dist_handle(irq, desc, d, enum_id);

	activate_irq(irq);
}

static unsigned int __init save_reg(struct intc_desc_int *d,
				    unsigned int cnt,
				    unsigned long value,
				    unsigned int smp)
{
	if (value) {
		value = intc_phys_to_virt(d, value);

		d->reg[cnt] = value;
#ifdef CONFIG_SMP
		d->smp[cnt] = smp;
#endif
		return 1;
	}

	return 0;
}

int __init register_intc_controller(struct intc_desc *desc)
{
	unsigned int i, k, smp;
	struct intc_hw_desc *hw = &desc->hw;
	struct intc_desc_int *d;
	struct resource *res;

	pr_info("Registered controller '%s' with %u IRQs\n",
		desc->name, hw->nr_vectors);

	d = kzalloc(sizeof(*d), GFP_NOWAIT);
	if (!d)
		goto err0;

	INIT_LIST_HEAD(&d->list);
	list_add_tail(&d->list, &intc_list);

	raw_spin_lock_init(&d->lock);
206
	INIT_RADIX_TREE(&d->tree, GFP_ATOMIC);
P
Paul Mundt 已提交
207 208 209 210 211 212 213 214 215 216 217 218 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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

	d->index = nr_intc_controllers;

	if (desc->num_resources) {
		d->nr_windows = desc->num_resources;
		d->window = kzalloc(d->nr_windows * sizeof(*d->window),
				    GFP_NOWAIT);
		if (!d->window)
			goto err1;

		for (k = 0; k < d->nr_windows; k++) {
			res = desc->resource + k;
			WARN_ON(resource_type(res) != IORESOURCE_MEM);
			d->window[k].phys = res->start;
			d->window[k].size = resource_size(res);
			d->window[k].virt = ioremap_nocache(res->start,
							 resource_size(res));
			if (!d->window[k].virt)
				goto err2;
		}
	}

	d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
#ifdef CONFIG_INTC_BALANCING
	if (d->nr_reg)
		d->nr_reg += hw->nr_mask_regs;
#endif
	d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
	d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
	d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
	d->nr_reg += hw->subgroups ? hw->nr_subgroups : 0;

	d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
	if (!d->reg)
		goto err2;

#ifdef CONFIG_SMP
	d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
	if (!d->smp)
		goto err3;
#endif
	k = 0;

	if (hw->mask_regs) {
		for (i = 0; i < hw->nr_mask_regs; i++) {
			smp = IS_SMP(hw->mask_regs[i]);
			k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
			k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
#ifdef CONFIG_INTC_BALANCING
			k += save_reg(d, k, hw->mask_regs[i].dist_reg, 0);
#endif
		}
	}

	if (hw->prio_regs) {
		d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
				  GFP_NOWAIT);
		if (!d->prio)
			goto err4;

		for (i = 0; i < hw->nr_prio_regs; i++) {
			smp = IS_SMP(hw->prio_regs[i]);
			k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
			k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
		}
P
Paul Mundt 已提交
272 273 274

		sort(d->prio, hw->nr_prio_regs, sizeof(*d->prio),
		     intc_handle_int_cmp, NULL);
P
Paul Mundt 已提交
275 276 277 278 279 280 281 282 283 284
	}

	if (hw->sense_regs) {
		d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
				   GFP_NOWAIT);
		if (!d->sense)
			goto err5;

		for (i = 0; i < hw->nr_sense_regs; i++)
			k += save_reg(d, k, hw->sense_regs[i].reg, 0);
P
Paul Mundt 已提交
285 286 287

		sort(d->sense, hw->nr_sense_regs, sizeof(*d->sense),
		     intc_handle_int_cmp, NULL);
P
Paul Mundt 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301
	}

	if (hw->subgroups)
		for (i = 0; i < hw->nr_subgroups; i++)
			if (hw->subgroups[i].reg)
				k+= save_reg(d, k, hw->subgroups[i].reg, 0);

	memcpy(&d->chip, &intc_irq_chip, sizeof(struct irq_chip));
	d->chip.name = desc->name;

	if (hw->ack_regs)
		for (i = 0; i < hw->nr_ack_regs; i++)
			k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
	else
P
Paul Mundt 已提交
302
		d->chip.irq_mask_ack = d->chip.irq_disable;
P
Paul Mundt 已提交
303 304 305 306 307 308 309 310 311 312 313

	/* disable bits matching force_disable before registering irqs */
	if (desc->force_disable)
		intc_enable_disable_enum(desc, d, desc->force_disable, 0);

	/* disable bits matching force_enable before registering irqs */
	if (desc->force_enable)
		intc_enable_disable_enum(desc, d, desc->force_enable, 0);

	BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */

314 315
	intc_irq_domain_init(d, hw);

P
Paul Mundt 已提交
316 317 318 319
	/* register the vectors one by one */
	for (i = 0; i < hw->nr_vectors; i++) {
		struct intc_vect *vect = hw->vectors + i;
		unsigned int irq = evt2irq(vect->vect);
T
Thomas Gleixner 已提交
320
		int res;
P
Paul Mundt 已提交
321 322 323 324

		if (!vect->enum_id)
			continue;

325 326
		res = irq_create_identity_mapping(d->domain, irq);
		if (unlikely(res)) {
P
Paul Mundt 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
			pr_err("can't get irq_desc for %d\n", irq);
			continue;
		}

		intc_irq_xlate_set(irq, vect->enum_id, d);
		intc_register_irq(desc, d, vect->enum_id, irq);

		for (k = i + 1; k < hw->nr_vectors; k++) {
			struct intc_vect *vect2 = hw->vectors + k;
			unsigned int irq2 = evt2irq(vect2->vect);

			if (vect->enum_id != vect2->enum_id)
				continue;

			/*
			 * In the case of multi-evt handling and sparse
			 * IRQ support, each vector still needs to have
			 * its own backing irq_desc.
			 */
346 347
			res = irq_create_identity_mapping(d->domain, irq2);
			if (unlikely(res)) {
P
Paul Mundt 已提交
348 349 350 351 352 353 354
				pr_err("can't get irq_desc for %d\n", irq2);
				continue;
			}

			vect2->enum_id = 0;

			/* redirect this interrupts to the first one */
355 356 357
			irq_set_chip(irq2, &dummy_irq_chip);
			irq_set_chained_handler(irq2, intc_redirect_irq);
			irq_set_handler_data(irq2, (void *)irq);
P
Paul Mundt 已提交
358 359 360 361 362 363 364 365 366
		}
	}

	intc_subgroup_init(desc, d);

	/* enable bits matching force_enable after registering irqs */
	if (desc->force_enable)
		intc_enable_disable_enum(desc, d, desc->force_enable, 1);

367 368
	d->skip_suspend = desc->skip_syscore_suspend;

P
Paul Mundt 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	nr_intc_controllers++;

	return 0;
err5:
	kfree(d->prio);
err4:
#ifdef CONFIG_SMP
	kfree(d->smp);
err3:
#endif
	kfree(d->reg);
err2:
	for (k = 0; k < d->nr_windows; k++)
		if (d->window[k].virt)
			iounmap(d->window[k].virt);

	kfree(d->window);
err1:
	kfree(d);
err0:
	pr_err("unable to allocate INTC memory\n");

	return -ENOMEM;
}

394
static int intc_suspend(void)
P
Paul Mundt 已提交
395 396 397
{
	struct intc_desc_int *d;

398 399
	list_for_each_entry(d, &intc_list, list) {
		int irq;
P
Paul Mundt 已提交
400

401 402 403
		if (d->skip_suspend)
			continue;

404 405 406 407
		/* enable wakeup irqs belonging to this intc controller */
		for_each_active_irq(irq) {
			struct irq_data *data;
			struct irq_chip *chip;
P
Paul Mundt 已提交
408

409 410 411 412
			data = irq_get_irq_data(irq);
			chip = irq_data_get_irq_chip(data);
			if (chip != &d->chip)
				continue;
413
			if (irqd_is_wakeup_set(data))
414 415 416 417 418
				chip->irq_enable(data);
		}
	}
	return 0;
}
P
Paul Mundt 已提交
419

420
static void intc_resume(void)
P
Paul Mundt 已提交
421 422 423
{
	struct intc_desc_int *d;

424 425
	list_for_each_entry(d, &intc_list, list) {
		int irq;
P
Paul Mundt 已提交
426

427 428 429
		if (d->skip_suspend)
			continue;

430
		for_each_active_irq(irq) {
431 432 433
			struct irq_data *data;
			struct irq_chip *chip;

P
Paul Mundt 已提交
434 435
			data = irq_get_irq_data(irq);
			chip = irq_data_get_irq_chip(data);
P
Paul Mundt 已提交
436 437 438 439
			/*
			 * This will catch the redirect and VIRQ cases
			 * due to the dummy_irq_chip being inserted.
			 */
P
Paul Mundt 已提交
440
			if (chip != &d->chip)
P
Paul Mundt 已提交
441
				continue;
442
			if (irqd_irq_disabled(data))
P
Paul Mundt 已提交
443
				chip->irq_disable(data);
P
Paul Mundt 已提交
444
			else
P
Paul Mundt 已提交
445
				chip->irq_enable(data);
P
Paul Mundt 已提交
446 447 448 449
		}
	}
}

450 451 452 453
struct syscore_ops intc_syscore_ops = {
	.suspend	= intc_suspend,
	.resume		= intc_resume,
};
P
Paul Mundt 已提交
454

455
struct bus_type intc_subsys = {
P
Paul Mundt 已提交
456
	.name		= "intc",
457
	.dev_name	= "intc",
P
Paul Mundt 已提交
458 459
};

460
static ssize_t
461
show_intc_name(struct device *dev, struct device_attribute *attr, char *buf)
462 463 464
{
	struct intc_desc_int *d;

465
	d = container_of(dev, struct intc_desc_int, dev);
466 467 468 469

	return sprintf(buf, "%s\n", d->chip.name);
}

470
static DEVICE_ATTR(name, S_IRUGO, show_intc_name, NULL);
471

472
static int __init register_intc_devs(void)
P
Paul Mundt 已提交
473 474 475 476
{
	struct intc_desc_int *d;
	int error;

477 478
	register_syscore_ops(&intc_syscore_ops);

479
	error = subsys_system_register(&intc_subsys, NULL);
P
Paul Mundt 已提交
480 481
	if (!error) {
		list_for_each_entry(d, &intc_list, list) {
482 483 484
			d->dev.id = d->index;
			d->dev.bus = &intc_subsys;
			error = device_register(&d->dev);
P
Paul Mundt 已提交
485
			if (error == 0)
486 487
				error = device_create_file(&d->dev,
							   &dev_attr_name);
P
Paul Mundt 已提交
488 489 490 491 492 493
			if (error)
				break;
		}
	}

	if (error)
494
		pr_err("device registration error\n");
P
Paul Mundt 已提交
495 496 497

	return error;
}
498
device_initcall(register_intc_devs);