intc.c 35.5 KB
Newer Older
1 2 3
/*
 * Shared interrupt handling code for IPR and INTC2 types of IRQs.
 *
M
Magnus Damm 已提交
4
 * Copyright (C) 2007, 2008 Magnus Damm
5
 * Copyright (C) 2009, 2010 Paul Mundt
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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.
 */
19 20
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

21 22 23 24
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/io.h>
25
#include <linux/slab.h>
26
#include <linux/interrupt.h>
27
#include <linux/sh_intc.h>
M
Magnus Damm 已提交
28 29
#include <linux/sysdev.h>
#include <linux/list.h>
P
Paul Mundt 已提交
30
#include <linux/topology.h>
P
Paul Mundt 已提交
31
#include <linux/bitmap.h>
32
#include <linux/cpumask.h>
33 34 35 36 37
#include <linux/spinlock.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/radix-tree.h>
#include <linux/mutex.h>
P
Paul Mundt 已提交
38
#include <asm/sizes.h>
M
Magnus Damm 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

#define _INTC_MK(fn, mode, addr_e, addr_d, width, shift) \
	((shift) | ((width) << 5) | ((fn) << 9) | ((mode) << 13) | \
	 ((addr_e) << 16) | ((addr_d << 24)))

#define _INTC_SHIFT(h) (h & 0x1f)
#define _INTC_WIDTH(h) ((h >> 5) & 0xf)
#define _INTC_FN(h) ((h >> 9) & 0xf)
#define _INTC_MODE(h) ((h >> 13) & 0x7)
#define _INTC_ADDR_E(h) ((h >> 16) & 0xff)
#define _INTC_ADDR_D(h) ((h >> 24) & 0xff)

struct intc_handle_int {
	unsigned int irq;
	unsigned long handle;
};
55

M
Magnus Damm 已提交
56 57 58 59 60 61
struct intc_window {
	phys_addr_t phys;
	void __iomem *virt;
	unsigned long size;
};

62 63 64 65 66
struct intc_map_entry {
	intc_enum enum_id;
	struct intc_desc_int *desc;
};

M
Magnus Damm 已提交
67
struct intc_desc_int {
M
Magnus Damm 已提交
68 69
	struct list_head list;
	struct sys_device sysdev;
70
	struct radix_tree_root tree;
71
	pm_message_t state;
M
Magnus Damm 已提交
72
	unsigned long *reg;
M
Magnus Damm 已提交
73 74 75
#ifdef CONFIG_SMP
	unsigned long *smp;
#endif
M
Magnus Damm 已提交
76 77 78 79 80
	unsigned int nr_reg;
	struct intc_handle_int *prio;
	unsigned int nr_prio;
	struct intc_handle_int *sense;
	unsigned int nr_sense;
M
Magnus Damm 已提交
81 82
	struct intc_window *window;
	unsigned int nr_windows;
M
Magnus Damm 已提交
83 84
	struct irq_chip chip;
};
85

M
Magnus Damm 已提交
86 87
static LIST_HEAD(intc_list);

P
Paul Mundt 已提交
88 89 90 91 92 93 94 95 96 97 98 99
/*
 * The intc_irq_map provides a global map of bound IRQ vectors for a
 * given platform. Allocation of IRQs are either static through the CPU
 * vector map, or dynamic in the case of board mux vectors or MSI.
 *
 * As this is a central point for all IRQ controllers on the system,
 * each of the available sources are mapped out here. This combined with
 * sparseirq makes it quite trivial to keep the vector map tightly packed
 * when dynamically creating IRQs, as well as tying in to otherwise
 * unused irq_desc positions in the sparse array.
 */
static DECLARE_BITMAP(intc_irq_map, NR_IRQS);
100
static struct intc_map_entry intc_irq_xlate[NR_IRQS];
P
Paul Mundt 已提交
101
static DEFINE_SPINLOCK(vector_lock);
102
static DEFINE_MUTEX(irq_xlate_mutex);
P
Paul Mundt 已提交
103

M
Magnus Damm 已提交
104 105 106 107 108 109 110 111 112 113
#ifdef CONFIG_SMP
#define IS_SMP(x) x.smp
#define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
#define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
#else
#define IS_SMP(x) 0
#define INTC_REG(d, x, c) (d->reg[(x)])
#define SMP_NR(d, x) 1
#endif

P
Paul Mundt 已提交
114 115
static unsigned int intc_prio_level[NR_IRQS];	/* for now */
static unsigned int default_prio_level = 2;	/* 2 - 16 */
M
Magnus Damm 已提交
116
static unsigned long ack_handle[NR_IRQS];
117 118 119
#ifdef CONFIG_INTC_BALANCING
static unsigned long dist_handle[NR_IRQS];
#endif
120

M
Magnus Damm 已提交
121
static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
122 123
{
	struct irq_chip *chip = get_irq_chip(irq);
124
	return container_of(chip, struct intc_desc_int, chip);
125 126
}

127 128 129 130 131 132 133 134 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 162 163 164 165 166 167
static unsigned long intc_phys_to_virt(struct intc_desc_int *d,
				       unsigned long address)
{
	struct intc_window *window;
	int k;

	/* scan through physical windows and convert address */
	for (k = 0; k < d->nr_windows; k++) {
		window = d->window + k;

		if (address < window->phys)
			continue;

		if (address >= (window->phys + window->size))
			continue;

		address -= window->phys;
		address += (unsigned long)window->virt;

		return address;
	}

	/* no windows defined, register must be 1:1 mapped virt:phys */
	return address;
}

static unsigned int intc_get_reg(struct intc_desc_int *d, unsigned long address)
{
	unsigned int k;

	address = intc_phys_to_virt(d, address);

	for (k = 0; k < d->nr_reg; k++) {
		if (d->reg[k] == address)
			return k;
	}

	BUG();
	return 0;
}

168 169
static inline unsigned int set_field(unsigned int value,
				     unsigned int field_value,
M
Magnus Damm 已提交
170
				     unsigned int handle)
171
{
M
Magnus Damm 已提交
172 173 174
	unsigned int width = _INTC_WIDTH(handle);
	unsigned int shift = _INTC_SHIFT(handle);

175 176 177 178 179
	value &= ~(((1 << width) - 1) << shift);
	value |= field_value << shift;
	return value;
}

M
Magnus Damm 已提交
180
static void write_8(unsigned long addr, unsigned long h, unsigned long data)
181
{
182
	__raw_writeb(set_field(0, data, h), addr);
183
	(void)__raw_readb(addr);	/* Defeat write posting */
184 185
}

M
Magnus Damm 已提交
186
static void write_16(unsigned long addr, unsigned long h, unsigned long data)
187
{
188
	__raw_writew(set_field(0, data, h), addr);
189
	(void)__raw_readw(addr);	/* Defeat write posting */
190 191
}

M
Magnus Damm 已提交
192
static void write_32(unsigned long addr, unsigned long h, unsigned long data)
193
{
194
	__raw_writel(set_field(0, data, h), addr);
195
	(void)__raw_readl(addr);	/* Defeat write posting */
196 197
}

M
Magnus Damm 已提交
198
static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
199
{
M
Magnus Damm 已提交
200 201
	unsigned long flags;
	local_irq_save(flags);
202
	__raw_writeb(set_field(__raw_readb(addr), data, h), addr);
203
	(void)__raw_readb(addr);	/* Defeat write posting */
M
Magnus Damm 已提交
204
	local_irq_restore(flags);
205 206
}

M
Magnus Damm 已提交
207
static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
208
{
M
Magnus Damm 已提交
209 210
	unsigned long flags;
	local_irq_save(flags);
211
	__raw_writew(set_field(__raw_readw(addr), data, h), addr);
212
	(void)__raw_readw(addr);	/* Defeat write posting */
M
Magnus Damm 已提交
213
	local_irq_restore(flags);
214 215
}

M
Magnus Damm 已提交
216
static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
217
{
M
Magnus Damm 已提交
218 219
	unsigned long flags;
	local_irq_save(flags);
220
	__raw_writel(set_field(__raw_readl(addr), data, h), addr);
221
	(void)__raw_readl(addr);	/* Defeat write posting */
M
Magnus Damm 已提交
222
	local_irq_restore(flags);
223 224
}

M
Magnus Damm 已提交
225 226 227 228 229 230 231 232 233 234 235 236
enum {	REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };

static void (*intc_reg_fns[])(unsigned long addr,
			      unsigned long h,
			      unsigned long data) = {
	[REG_FN_WRITE_BASE + 0] = write_8,
	[REG_FN_WRITE_BASE + 1] = write_16,
	[REG_FN_WRITE_BASE + 3] = write_32,
	[REG_FN_MODIFY_BASE + 0] = modify_8,
	[REG_FN_MODIFY_BASE + 1] = modify_16,
	[REG_FN_MODIFY_BASE + 3] = modify_32,
};
237

M
Magnus Damm 已提交
238 239 240 241 242 243
enum {	MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
	MODE_MASK_REG,       /* Bit(s) set -> interrupt disabled */
	MODE_DUAL_REG,       /* Two registers, set bit to enable / disable */
	MODE_PRIO_REG,       /* Priority value written to enable interrupt */
	MODE_PCLR_REG,       /* Above plus all bits set to disable interrupt */
};
244

M
Magnus Damm 已提交
245 246 247 248 249 250
static void intc_mode_field(unsigned long addr,
			    unsigned long handle,
			    void (*fn)(unsigned long,
				       unsigned long,
				       unsigned long),
			    unsigned int irq)
251
{
M
Magnus Damm 已提交
252
	fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
253 254
}

M
Magnus Damm 已提交
255 256 257 258 259 260
static void intc_mode_zero(unsigned long addr,
			   unsigned long handle,
			   void (*fn)(unsigned long,
				       unsigned long,
				       unsigned long),
			   unsigned int irq)
261
{
M
Magnus Damm 已提交
262
	fn(addr, handle, 0);
263 264
}

M
Magnus Damm 已提交
265 266 267 268 269 270
static void intc_mode_prio(unsigned long addr,
			   unsigned long handle,
			   void (*fn)(unsigned long,
				       unsigned long,
				       unsigned long),
			   unsigned int irq)
271
{
M
Magnus Damm 已提交
272
	fn(addr, handle, intc_prio_level[irq]);
273 274
}

M
Magnus Damm 已提交
275 276 277 278 279 280 281 282 283 284 285 286
static void (*intc_enable_fns[])(unsigned long addr,
				 unsigned long handle,
				 void (*fn)(unsigned long,
					    unsigned long,
					    unsigned long),
				 unsigned int irq) = {
	[MODE_ENABLE_REG] = intc_mode_field,
	[MODE_MASK_REG] = intc_mode_zero,
	[MODE_DUAL_REG] = intc_mode_field,
	[MODE_PRIO_REG] = intc_mode_prio,
	[MODE_PCLR_REG] = intc_mode_prio,
};
287

M
Magnus Damm 已提交
288 289 290 291 292 293 294 295 296 297 298 299
static void (*intc_disable_fns[])(unsigned long addr,
				  unsigned long handle,
				  void (*fn)(unsigned long,
					     unsigned long,
					     unsigned long),
				  unsigned int irq) = {
	[MODE_ENABLE_REG] = intc_mode_zero,
	[MODE_MASK_REG] = intc_mode_field,
	[MODE_DUAL_REG] = intc_mode_field,
	[MODE_PRIO_REG] = intc_mode_zero,
	[MODE_PCLR_REG] = intc_mode_field,
};
300

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 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
#ifdef CONFIG_INTC_BALANCING
static inline void intc_balancing_enable(unsigned int irq)
{
	struct intc_desc_int *d = get_intc_desc(irq);
	unsigned long handle = dist_handle[irq];
	unsigned long addr;

	if (irq_balancing_disabled(irq) || !handle)
		return;

	addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
	intc_reg_fns[_INTC_FN(handle)](addr, handle, 1);
}

static inline void intc_balancing_disable(unsigned int irq)
{
	struct intc_desc_int *d = get_intc_desc(irq);
	unsigned long handle = dist_handle[irq];
	unsigned long addr;

	if (irq_balancing_disabled(irq) || !handle)
		return;

	addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
	intc_reg_fns[_INTC_FN(handle)](addr, handle, 0);
}

static unsigned int intc_dist_data(struct intc_desc *desc,
				   struct intc_desc_int *d,
				   intc_enum enum_id)
{
	struct intc_mask_reg *mr = desc->hw.mask_regs;
	unsigned int i, j, fn, mode;
	unsigned long reg_e, reg_d;

	for (i = 0; mr && enum_id && i < desc->hw.nr_mask_regs; i++) {
		mr = desc->hw.mask_regs + i;

		/*
		 * Skip this entry if there's no auto-distribution
		 * register associated with it.
		 */
		if (!mr->dist_reg)
			continue;

		for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
			if (mr->enum_ids[j] != enum_id)
				continue;

			fn = REG_FN_MODIFY_BASE;
			mode = MODE_ENABLE_REG;
			reg_e = mr->dist_reg;
			reg_d = mr->dist_reg;

			fn += (mr->reg_width >> 3) - 1;
			return _INTC_MK(fn, mode,
					intc_get_reg(d, reg_e),
					intc_get_reg(d, reg_d),
					1,
					(mr->reg_width - 1) - j);
		}
	}

	/*
	 * It's possible we've gotten here with no distribution options
	 * available for the IRQ in question, so we just skip over those.
	 */
	return 0;
}
#else
static inline void intc_balancing_enable(unsigned int irq)
{
}

static inline void intc_balancing_disable(unsigned int irq)
{
}
#endif

M
Magnus Damm 已提交
380
static inline void _intc_enable(unsigned int irq, unsigned long handle)
381
{
M
Magnus Damm 已提交
382
	struct intc_desc_int *d = get_intc_desc(irq);
M
Magnus Damm 已提交
383 384
	unsigned long addr;
	unsigned int cpu;
385

M
Magnus Damm 已提交
386
	for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
387 388 389 390
#ifdef CONFIG_SMP
		if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
			continue;
#endif
M
Magnus Damm 已提交
391 392 393 394
		addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
		intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
						    [_INTC_FN(handle)], irq);
	}
395 396

	intc_balancing_enable(irq);
397 398
}

399 400
static void intc_enable(unsigned int irq)
{
M
Magnus Damm 已提交
401
	_intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
402 403 404 405
}

static void intc_disable(unsigned int irq)
{
M
Magnus Damm 已提交
406
	struct intc_desc_int *d = get_intc_desc(irq);
407
	unsigned long handle = (unsigned long)get_irq_chip_data(irq);
M
Magnus Damm 已提交
408 409
	unsigned long addr;
	unsigned int cpu;
410

411 412
	intc_balancing_disable(irq);

M
Magnus Damm 已提交
413
	for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
414 415 416 417
#ifdef CONFIG_SMP
		if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
			continue;
#endif
M
Magnus Damm 已提交
418 419 420 421
		addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
		intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
						     [_INTC_FN(handle)], irq);
	}
422 423
}

M
Magnus Damm 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
static void (*intc_enable_noprio_fns[])(unsigned long addr,
					unsigned long handle,
					void (*fn)(unsigned long,
						   unsigned long,
						   unsigned long),
					unsigned int irq) = {
	[MODE_ENABLE_REG] = intc_mode_field,
	[MODE_MASK_REG] = intc_mode_zero,
	[MODE_DUAL_REG] = intc_mode_field,
	[MODE_PRIO_REG] = intc_mode_field,
	[MODE_PCLR_REG] = intc_mode_field,
};

static void intc_enable_disable(struct intc_desc_int *d,
				unsigned long handle, int do_enable)
{
	unsigned long addr;
	unsigned int cpu;
	void (*fn)(unsigned long, unsigned long,
		   void (*)(unsigned long, unsigned long, unsigned long),
		   unsigned int);

	if (do_enable) {
		for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
			addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
			fn = intc_enable_noprio_fns[_INTC_MODE(handle)];
			fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
		}
	} else {
		for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
			addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
			fn = intc_disable_fns[_INTC_MODE(handle)];
			fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
		}
	}
}

M
Magnus Damm 已提交
461 462 463 464 465
static int intc_set_wake(unsigned int irq, unsigned int on)
{
	return 0; /* allow wakeup, but setup hardware in intc_suspend() */
}

466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
#ifdef CONFIG_SMP
/*
 * This is held with the irq desc lock held, so we don't require any
 * additional locking here at the intc desc level. The affinity mask is
 * later tested in the enable/disable paths.
 */
static int intc_set_affinity(unsigned int irq, const struct cpumask *cpumask)
{
	if (!cpumask_intersects(cpumask, cpu_online_mask))
		return -1;

	cpumask_copy(irq_to_desc(irq)->affinity, cpumask);

	return 0;
}
#endif

M
Magnus Damm 已提交
483 484 485 486 487 488 489 490
static void intc_mask_ack(unsigned int irq)
{
	struct intc_desc_int *d = get_intc_desc(irq);
	unsigned long handle = ack_handle[irq];
	unsigned long addr;

	intc_disable(irq);

491
	/* read register and write zero only to the associated bit */
M
Magnus Damm 已提交
492 493
	if (handle) {
		addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
494 495
		switch (_INTC_FN(handle)) {
		case REG_FN_MODIFY_BASE + 0:	/* 8bit */
496 497
			__raw_readb(addr);
			__raw_writeb(0xff ^ set_field(0, 1, handle), addr);
498 499
			break;
		case REG_FN_MODIFY_BASE + 1:	/* 16bit */
500 501
			__raw_readw(addr);
			__raw_writew(0xffff ^ set_field(0, 1, handle), addr);
502 503
			break;
		case REG_FN_MODIFY_BASE + 3:	/* 32bit */
504 505
			__raw_readl(addr);
			__raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
506 507 508 509 510
			break;
		default:
			BUG();
			break;
		}
M
Magnus Damm 已提交
511 512 513
	}
}

M
Magnus Damm 已提交
514 515 516
static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
					     unsigned int nr_hp,
					     unsigned int irq)
517
{
M
Magnus Damm 已提交
518 519
	int i;

520 521
	/*
	 * this doesn't scale well, but...
522 523 524 525 526 527 528 529 530 531
	 *
	 * this function should only be used for cerain uncommon
	 * operations such as intc_set_priority() and intc_set_sense()
	 * and in those rare cases performance doesn't matter that much.
	 * keeping the memory footprint low is more important.
	 *
	 * one rather simple way to speed this up and still keep the
	 * memory footprint down is to make sure the array is sorted
	 * and then perform a bisect to lookup the irq.
	 */
M
Magnus Damm 已提交
532 533 534 535 536 537
	for (i = 0; i < nr_hp; i++) {
		if ((hp + i)->irq != irq)
			continue;

		return hp + i;
	}
538

M
Magnus Damm 已提交
539
	return NULL;
540 541
}

M
Magnus Damm 已提交
542
int intc_set_priority(unsigned int irq, unsigned int prio)
543
{
M
Magnus Damm 已提交
544 545 546 547 548 549 550 551
	struct intc_desc_int *d = get_intc_desc(irq);
	struct intc_handle_int *ihp;

	if (!intc_prio_level[irq] || prio <= 1)
		return -EINVAL;

	ihp = intc_find_irq(d->prio, d->nr_prio, irq);
	if (ihp) {
552
		if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
M
Magnus Damm 已提交
553
			return -EINVAL;
554

M
Magnus Damm 已提交
555 556 557 558 559 560 561
		intc_prio_level[irq] = prio;

		/*
		 * only set secondary masking method directly
		 * primary masking method is using intc_prio_level[irq]
		 * priority level will be set during next enable()
		 */
562
		if (_INTC_FN(ihp->handle) != REG_FN_ERR)
M
Magnus Damm 已提交
563 564 565
			_intc_enable(irq, ihp->handle);
	}
	return 0;
566 567 568 569 570 571 572 573
}

#define VALID(x) (x | 0x80)

static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
	[IRQ_TYPE_EDGE_FALLING] = VALID(0),
	[IRQ_TYPE_EDGE_RISING] = VALID(1),
	[IRQ_TYPE_LEVEL_LOW] = VALID(2),
574 575 576 577
	/* SH7706, SH7707 and SH7709 do not support high level triggered */
#if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
    !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
    !defined(CONFIG_CPU_SUBTYPE_SH7709)
578
	[IRQ_TYPE_LEVEL_HIGH] = VALID(3),
579
#endif
580 581 582 583
};

static int intc_set_sense(unsigned int irq, unsigned int type)
{
M
Magnus Damm 已提交
584
	struct intc_desc_int *d = get_intc_desc(irq);
585
	unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
M
Magnus Damm 已提交
586 587
	struct intc_handle_int *ihp;
	unsigned long addr;
588

M
Magnus Damm 已提交
589
	if (!value)
590 591
		return -EINVAL;

M
Magnus Damm 已提交
592 593
	ihp = intc_find_irq(d->sense, d->nr_sense, irq);
	if (ihp) {
M
Magnus Damm 已提交
594
		addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
M
Magnus Damm 已提交
595
		intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
596
	}
M
Magnus Damm 已提交
597
	return 0;
598 599
}

M
Magnus Damm 已提交
600 601
static intc_enum __init intc_grp_id(struct intc_desc *desc,
				    intc_enum enum_id)
M
Magnus Damm 已提交
602
{
603
	struct intc_group *g = desc->hw.groups;
M
Magnus Damm 已提交
604 605
	unsigned int i, j;

606 607
	for (i = 0; g && enum_id && i < desc->hw.nr_groups; i++) {
		g = desc->hw.groups + i;
M
Magnus Damm 已提交
608 609 610 611 612 613 614 615 616 617 618 619

		for (j = 0; g->enum_ids[j]; j++) {
			if (g->enum_ids[j] != enum_id)
				continue;

			return g->enum_id;
		}
	}

	return 0;
}

M
Magnus Damm 已提交
620 621 622 623 624
static unsigned int __init _intc_mask_data(struct intc_desc *desc,
					   struct intc_desc_int *d,
					   intc_enum enum_id,
					   unsigned int *reg_idx,
					   unsigned int *fld_idx)
625
{
626
	struct intc_mask_reg *mr = desc->hw.mask_regs;
M
Magnus Damm 已提交
627
	unsigned int fn, mode;
M
Magnus Damm 已提交
628
	unsigned long reg_e, reg_d;
629

M
Magnus Damm 已提交
630 631
	while (mr && enum_id && *reg_idx < desc->hw.nr_mask_regs) {
		mr = desc->hw.mask_regs + *reg_idx;
632

M
Magnus Damm 已提交
633 634
		for (; *fld_idx < ARRAY_SIZE(mr->enum_ids); (*fld_idx)++) {
			if (mr->enum_ids[*fld_idx] != enum_id)
635 636
				continue;

M
Magnus Damm 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
			if (mr->set_reg && mr->clr_reg) {
				fn = REG_FN_WRITE_BASE;
				mode = MODE_DUAL_REG;
				reg_e = mr->clr_reg;
				reg_d = mr->set_reg;
			} else {
				fn = REG_FN_MODIFY_BASE;
				if (mr->set_reg) {
					mode = MODE_ENABLE_REG;
					reg_e = mr->set_reg;
					reg_d = mr->set_reg;
				} else {
					mode = MODE_MASK_REG;
					reg_e = mr->clr_reg;
					reg_d = mr->clr_reg;
				}
653 654
			}

M
Magnus Damm 已提交
655 656 657 658 659
			fn += (mr->reg_width >> 3) - 1;
			return _INTC_MK(fn, mode,
					intc_get_reg(d, reg_e),
					intc_get_reg(d, reg_d),
					1,
M
Magnus Damm 已提交
660
					(mr->reg_width - 1) - *fld_idx);
661
		}
M
Magnus Damm 已提交
662 663 664

		*fld_idx = 0;
		(*reg_idx)++;
665 666
	}

M
Magnus Damm 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
	return 0;
}

static unsigned int __init intc_mask_data(struct intc_desc *desc,
					  struct intc_desc_int *d,
					  intc_enum enum_id, int do_grps)
{
	unsigned int i = 0;
	unsigned int j = 0;
	unsigned int ret;

	ret = _intc_mask_data(desc, d, enum_id, &i, &j);
	if (ret)
		return ret;

M
Magnus Damm 已提交
682
	if (do_grps)
M
Magnus Damm 已提交
683
		return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
M
Magnus Damm 已提交
684

685 686 687
	return 0;
}

M
Magnus Damm 已提交
688 689 690 691 692
static unsigned int __init _intc_prio_data(struct intc_desc *desc,
					   struct intc_desc_int *d,
					   intc_enum enum_id,
					   unsigned int *reg_idx,
					   unsigned int *fld_idx)
693
{
694
	struct intc_prio_reg *pr = desc->hw.prio_regs;
M
Magnus Damm 已提交
695
	unsigned int fn, n, mode, bit;
M
Magnus Damm 已提交
696
	unsigned long reg_e, reg_d;
697

M
Magnus Damm 已提交
698 699
	while (pr && enum_id && *reg_idx < desc->hw.nr_prio_regs) {
		pr = desc->hw.prio_regs + *reg_idx;
700

M
Magnus Damm 已提交
701 702
		for (; *fld_idx < ARRAY_SIZE(pr->enum_ids); (*fld_idx)++) {
			if (pr->enum_ids[*fld_idx] != enum_id)
703 704
				continue;

M
Magnus Damm 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717
			if (pr->set_reg && pr->clr_reg) {
				fn = REG_FN_WRITE_BASE;
				mode = MODE_PCLR_REG;
				reg_e = pr->set_reg;
				reg_d = pr->clr_reg;
			} else {
				fn = REG_FN_MODIFY_BASE;
				mode = MODE_PRIO_REG;
				if (!pr->set_reg)
					BUG();
				reg_e = pr->set_reg;
				reg_d = pr->set_reg;
			}
718

M
Magnus Damm 已提交
719
			fn += (pr->reg_width >> 3) - 1;
M
Magnus Damm 已提交
720
			n = *fld_idx + 1;
721

M
Magnus Damm 已提交
722
			BUG_ON(n * pr->field_width > pr->reg_width);
723

M
Magnus Damm 已提交
724
			bit = pr->reg_width - (n * pr->field_width);
725

M
Magnus Damm 已提交
726 727 728 729
			return _INTC_MK(fn, mode,
					intc_get_reg(d, reg_e),
					intc_get_reg(d, reg_d),
					pr->field_width, bit);
730
		}
M
Magnus Damm 已提交
731 732 733

		*fld_idx = 0;
		(*reg_idx)++;
734 735
	}

M
Magnus Damm 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
	return 0;
}

static unsigned int __init intc_prio_data(struct intc_desc *desc,
					  struct intc_desc_int *d,
					  intc_enum enum_id, int do_grps)
{
	unsigned int i = 0;
	unsigned int j = 0;
	unsigned int ret;

	ret = _intc_prio_data(desc, d, enum_id, &i, &j);
	if (ret)
		return ret;

M
Magnus Damm 已提交
751
	if (do_grps)
M
Magnus Damm 已提交
752 753 754 755 756
		return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);

	return 0;
}

M
Magnus Damm 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
static void __init intc_enable_disable_enum(struct intc_desc *desc,
					    struct intc_desc_int *d,
					    intc_enum enum_id, int enable)
{
	unsigned int i, j, data;

	/* go through and enable/disable all mask bits */
	i = j = 0;
	do {
		data = _intc_mask_data(desc, d, enum_id, &i, &j);
		if (data)
			intc_enable_disable(d, data, enable);
		j++;
	} while (data);

	/* go through and enable/disable all priority fields */
	i = j = 0;
	do {
		data = _intc_prio_data(desc, d, enum_id, &i, &j);
		if (data)
			intc_enable_disable(d, data, enable);

		j++;
	} while (data);
}

M
Magnus Damm 已提交
783 784 785 786
static unsigned int __init intc_ack_data(struct intc_desc *desc,
					  struct intc_desc_int *d,
					  intc_enum enum_id)
{
787
	struct intc_mask_reg *mr = desc->hw.ack_regs;
M
Magnus Damm 已提交
788 789 790
	unsigned int i, j, fn, mode;
	unsigned long reg_e, reg_d;

791 792
	for (i = 0; mr && enum_id && i < desc->hw.nr_ack_regs; i++) {
		mr = desc->hw.ack_regs + i;
M
Magnus Damm 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814

		for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
			if (mr->enum_ids[j] != enum_id)
				continue;

			fn = REG_FN_MODIFY_BASE;
			mode = MODE_ENABLE_REG;
			reg_e = mr->set_reg;
			reg_d = mr->set_reg;

			fn += (mr->reg_width >> 3) - 1;
			return _INTC_MK(fn, mode,
					intc_get_reg(d, reg_e),
					intc_get_reg(d, reg_d),
					1,
					(mr->reg_width - 1) - j);
		}
	}

	return 0;
}

M
Magnus Damm 已提交
815 816 817 818
static unsigned int __init intc_sense_data(struct intc_desc *desc,
					   struct intc_desc_int *d,
					   intc_enum enum_id)
{
819
	struct intc_sense_reg *sr = desc->hw.sense_regs;
M
Magnus Damm 已提交
820 821
	unsigned int i, j, fn, bit;

822 823
	for (i = 0; sr && enum_id && i < desc->hw.nr_sense_regs; i++) {
		sr = desc->hw.sense_regs + i;
M
Magnus Damm 已提交
824 825 826 827 828 829 830 831

		for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
			if (sr->enum_ids[j] != enum_id)
				continue;

			fn = REG_FN_MODIFY_BASE;
			fn += (sr->reg_width >> 3) - 1;

832 833 834
			BUG_ON((j + 1) * sr->field_width > sr->reg_width);

			bit = sr->reg_width - ((j + 1) * sr->field_width);
M
Magnus Damm 已提交
835 836 837 838 839

			return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
					0, sr->field_width, bit);
		}
	}
M
Magnus Damm 已提交
840

841 842 843
	return 0;
}

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
unsigned int intc_irq_lookup(const char *chipname, intc_enum enum_id)
{
	struct intc_map_entry *ptr;
	struct intc_desc_int *d;
	unsigned int irq = 0;

	list_for_each_entry(d, &intc_list, list) {
		if (strcmp(d->chip.name, chipname) == 0) {
			ptr = radix_tree_lookup(&d->tree, enum_id);
			if (ptr) {
				irq = ptr - intc_irq_xlate;
				break;
			}
		}
	}

	return irq;
}
EXPORT_SYMBOL_GPL(intc_irq_lookup);

M
Magnus Damm 已提交
864 865 866
static void __init intc_register_irq(struct intc_desc *desc,
				     struct intc_desc_int *d,
				     intc_enum enum_id,
867 868
				     unsigned int irq)
{
869
	struct intc_handle_int *hp;
M
Magnus Damm 已提交
870 871
	unsigned int data[2], primary;

P
Paul Mundt 已提交
872
	/*
873 874
	 * Register the IRQ position with the global IRQ map, then insert
	 * it in to the radix tree.
P
Paul Mundt 已提交
875 876 877
	 */
	set_bit(irq, intc_irq_map);

878 879 880 881
	mutex_lock(&irq_xlate_mutex);
	radix_tree_insert(&d->tree, enum_id, &intc_irq_xlate[irq]);
	mutex_unlock(&irq_xlate_mutex);

882 883 884
	/*
	 * Prefer single interrupt source bitmap over other combinations:
	 *
M
Magnus Damm 已提交
885 886 887 888 889
	 * 1. bitmap, single interrupt source
	 * 2. priority, single interrupt source
	 * 3. bitmap, multiple interrupt sources (groups)
	 * 4. priority, multiple interrupt sources (groups)
	 */
M
Magnus Damm 已提交
890 891
	data[0] = intc_mask_data(desc, d, enum_id, 0);
	data[1] = intc_prio_data(desc, d, enum_id, 0);
M
Magnus Damm 已提交
892 893 894 895 896

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

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

M
Magnus Damm 已提交
901 902
	data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
	data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
M
Magnus Damm 已提交
903 904 905 906 907

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

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

	disable_irq_nosync(irq);
M
Magnus Damm 已提交
910
	set_irq_chip_and_handler_name(irq, &d->chip,
911
				      handle_level_irq, "level");
M
Magnus Damm 已提交
912
	set_irq_chip_data(irq, (void *)data[primary]);
913

914 915
	/*
	 * set priority level
916 917
	 * - this needs to be at least 2 for 5-bit priorities on 7780
	 */
P
Paul Mundt 已提交
918
	intc_prio_level[irq] = default_prio_level;
M
Magnus Damm 已提交
919

M
Magnus Damm 已提交
920 921
	/* enable secondary masking method if present */
	if (data[!primary])
M
Magnus Damm 已提交
922 923 924 925
		_intc_enable(irq, data[!primary]);

	/* add irq to d->prio list if priority is available */
	if (data[1]) {
926 927 928 929 930 931 932 933 934 935 936 937
		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);
		}
M
Magnus Damm 已提交
938 939 940 941 942 943 944 945 946 947
		d->nr_prio++;
	}

	/* add irq to d->sense list if sense is available */
	data[0] = intc_sense_data(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++;
	}
948 949

	/* irq should be disabled by default */
M
Magnus Damm 已提交
950
	d->chip.mask(irq);
M
Magnus Damm 已提交
951

952
	if (desc->hw.ack_regs)
M
Magnus Damm 已提交
953
		ack_handle[irq] = intc_ack_data(desc, d, enum_id);
954

955 956 957 958 959
#ifdef CONFIG_INTC_BALANCING
	if (desc->hw.mask_regs)
		dist_handle[irq] = intc_dist_data(desc, d, enum_id);
#endif

960 961 962
#ifdef CONFIG_ARM
	set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
#endif
963 964
}

M
Magnus Damm 已提交
965 966 967 968 969 970
static unsigned int __init save_reg(struct intc_desc_int *d,
				    unsigned int cnt,
				    unsigned long value,
				    unsigned int smp)
{
	if (value) {
M
Magnus Damm 已提交
971 972
		value = intc_phys_to_virt(d, value);

M
Magnus Damm 已提交
973 974 975 976 977 978 979 980 981 982
		d->reg[cnt] = value;
#ifdef CONFIG_SMP
		d->smp[cnt] = smp;
#endif
		return 1;
	}

	return 0;
}

983
static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
984
{
985
	generic_handle_irq((unsigned int)get_irq_data(irq));
986
}
M
Magnus Damm 已提交
987

988
int __init register_intc_controller(struct intc_desc *desc)
989
{
P
Paul Mundt 已提交
990
	unsigned int i, k, smp;
991
	struct intc_hw_desc *hw = &desc->hw;
M
Magnus Damm 已提交
992
	struct intc_desc_int *d;
M
Magnus Damm 已提交
993
	struct resource *res;
M
Magnus Damm 已提交
994

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

998
	d = kzalloc(sizeof(*d), GFP_NOWAIT);
999 1000
	if (!d)
		goto err0;
M
Magnus Damm 已提交
1001

M
Magnus Damm 已提交
1002 1003 1004
	INIT_LIST_HEAD(&d->list);
	list_add(&d->list, &intc_list);

M
Magnus Damm 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
	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;
		}
	}

1024
	d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
1025 1026 1027 1028
#ifdef CONFIG_INTC_BALANCING
	if (d->nr_reg)
		d->nr_reg += hw->nr_mask_regs;
#endif
1029 1030 1031
	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;
1032

1033
	d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
1034
	if (!d->reg)
M
Magnus Damm 已提交
1035
		goto err2;
1036

M
Magnus Damm 已提交
1037
#ifdef CONFIG_SMP
1038
	d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
1039
	if (!d->smp)
M
Magnus Damm 已提交
1040
		goto err3;
M
Magnus Damm 已提交
1041
#endif
M
Magnus Damm 已提交
1042 1043
	k = 0;

1044 1045 1046 1047 1048
	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);
1049 1050 1051
#ifdef CONFIG_INTC_BALANCING
			k += save_reg(d, k, hw->mask_regs[i].dist_reg, 0);
#endif
M
Magnus Damm 已提交
1052 1053 1054
		}
	}

1055 1056 1057
	if (hw->prio_regs) {
		d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
				  GFP_NOWAIT);
1058
		if (!d->prio)
M
Magnus Damm 已提交
1059
			goto err4;
M
Magnus Damm 已提交
1060

1061 1062 1063 1064
		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);
M
Magnus Damm 已提交
1065 1066 1067
		}
	}

1068 1069 1070
	if (hw->sense_regs) {
		d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
				   GFP_NOWAIT);
1071
		if (!d->sense)
M
Magnus Damm 已提交
1072
			goto err5;
M
Magnus Damm 已提交
1073

1074 1075
		for (i = 0; i < hw->nr_sense_regs; i++)
			k += save_reg(d, k, hw->sense_regs[i].reg, 0);
M
Magnus Damm 已提交
1076 1077 1078 1079 1080 1081
	}

	d->chip.name = desc->name;
	d->chip.mask = intc_disable;
	d->chip.unmask = intc_enable;
	d->chip.mask_ack = intc_disable;
1082 1083 1084
	d->chip.enable = intc_enable;
	d->chip.disable = intc_disable;
	d->chip.shutdown = intc_disable;
M
Magnus Damm 已提交
1085
	d->chip.set_type = intc_set_sense;
M
Magnus Damm 已提交
1086
	d->chip.set_wake = intc_set_wake;
1087 1088 1089
#ifdef CONFIG_SMP
	d->chip.set_affinity = intc_set_affinity;
#endif
1090

1091 1092 1093
	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);
M
Magnus Damm 已提交
1094 1095 1096 1097

		d->chip.mask_ack = intc_mask_ack;
	}

M
Magnus Damm 已提交
1098 1099 1100
	/* disable bits matching force_disable before registering irqs */
	if (desc->force_disable)
		intc_enable_disable_enum(desc, d, desc->force_disable, 0);
M
Magnus Damm 已提交
1101 1102 1103 1104 1105

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

M
Magnus Damm 已提交
1106 1107
	BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */

1108
	/* register the vectors one by one */
1109 1110
	for (i = 0; i < hw->nr_vectors; i++) {
		struct intc_vect *vect = hw->vectors + i;
1111 1112
		unsigned int irq = evt2irq(vect->vect);
		struct irq_desc *irq_desc;
P
Paul Mundt 已提交
1113

1114 1115 1116
		if (!vect->enum_id)
			continue;

P
Paul Mundt 已提交
1117
		irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
1118
		if (unlikely(!irq_desc)) {
1119
			pr_err("can't get irq_desc for %d\n", irq);
1120 1121 1122
			continue;
		}

1123 1124 1125
		intc_irq_xlate[irq].enum_id = vect->enum_id;
		intc_irq_xlate[irq].desc = d;

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

1128 1129
		for (k = i + 1; k < hw->nr_vectors; k++) {
			struct intc_vect *vect2 = hw->vectors + k;
1130 1131 1132 1133 1134
			unsigned int irq2 = evt2irq(vect2->vect);

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

1135 1136 1137 1138 1139 1140 1141
			/*
			 * In the case of multi-evt handling and sparse
			 * IRQ support, each vector still needs to have
			 * its own backing irq_desc.
			 */
			irq_desc = irq_to_desc_alloc_node(irq2, numa_node_id());
			if (unlikely(!irq_desc)) {
1142
				pr_err("can't get irq_desc for %d\n", irq2);
1143 1144 1145
				continue;
			}

1146 1147 1148
			vect2->enum_id = 0;

			/* redirect this interrupts to the first one */
1149
			set_irq_chip(irq2, &dummy_irq_chip);
1150
			set_irq_chained_handler(irq2, intc_redirect_irq);
1151 1152
			set_irq_data(irq2, (void *)irq);
		}
1153
	}
M
Magnus Damm 已提交
1154 1155 1156 1157

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

	return 0;
M
Magnus Damm 已提交
1160
err5:
1161
	kfree(d->prio);
M
Magnus Damm 已提交
1162
err4:
1163 1164
#ifdef CONFIG_SMP
	kfree(d->smp);
M
Magnus Damm 已提交
1165
err3:
1166 1167
#endif
	kfree(d->reg);
M
Magnus Damm 已提交
1168 1169 1170 1171 1172 1173 1174
err2:
	for (k = 0; k < d->nr_windows; k++)
		if (d->window[k].virt)
			iounmap(d->window[k].virt);

	kfree(d->window);
err1:
1175
	kfree(d);
M
Magnus Damm 已提交
1176
err0:
1177 1178 1179
	pr_err("unable to allocate INTC memory\n");

	return -ENOMEM;
1180
}
M
Magnus Damm 已提交
1181

P
Paul Mundt 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
#ifdef CONFIG_INTC_USERIMASK
static void __iomem *uimask;

int register_intc_userimask(unsigned long addr)
{
	if (unlikely(uimask))
		return -EBUSY;

	uimask = ioremap_nocache(addr, SZ_4K);
	if (unlikely(!uimask))
		return -ENOMEM;

1194
	pr_info("userimask support registered for levels 0 -> %d\n",
P
Paul Mundt 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
		default_prio_level - 1);

	return 0;
}

static ssize_t
show_intc_userimask(struct sysdev_class *cls,
		    struct sysdev_class_attribute *attr, char *buf)
{
	return sprintf(buf, "%d\n", (__raw_readl(uimask) >> 4) & 0xf);
}

static ssize_t
store_intc_userimask(struct sysdev_class *cls,
		     struct sysdev_class_attribute *attr,
		     const char *buf, size_t count)
{
	unsigned long level;

	level = simple_strtoul(buf, NULL, 10);

	/*
	 * Minimal acceptable IRQ levels are in the 2 - 16 range, but
	 * these are chomped so as to not interfere with normal IRQs.
	 *
	 * Level 1 is a special case on some CPUs in that it's not
	 * directly settable, but given that USERIMASK cuts off below a
	 * certain level, we don't care about this limitation here.
	 * Level 0 on the other hand equates to user masking disabled.
	 *
	 * We use default_prio_level as a cut off so that only special
	 * case opt-in IRQs can be mangled.
	 */
	if (level >= default_prio_level)
		return -EINVAL;

	__raw_writel(0xa5 << 24 | level << 4, uimask);

	return count;
}

static SYSDEV_CLASS_ATTR(userimask, S_IRUSR | S_IWUSR,
			 show_intc_userimask, store_intc_userimask);
#endif

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
#ifdef CONFIG_INTC_MAPPING_DEBUG
static int intc_irq_xlate_debug(struct seq_file *m, void *priv)
{
	int i;

	seq_printf(m, "%-5s  %-7s  %-15s\n", "irq", "enum", "chip name");

	for (i = 1; i < nr_irqs; i++) {
		struct intc_desc_int *desc = intc_irq_xlate[i].desc;

		if (!desc)
			continue;

		seq_printf(m, "%5d  ", i);
		seq_printf(m, "0x%05x  ", intc_irq_xlate[i].enum_id);
		seq_printf(m, "%-15s\n", desc->chip.name);
	}

	return 0;
}

static int intc_irq_xlate_open(struct inode *inode, struct file *file)
{
	return single_open(file, intc_irq_xlate_debug, inode->i_private);
}

static const struct file_operations intc_irq_xlate_fops = {
	.open = intc_irq_xlate_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

static int __init intc_irq_xlate_init(void)
{
	/*
	 * XXX.. use arch_debugfs_dir here when all of the intc users are
	 * converted.
	 */
	if (debugfs_create_file("intc_irq_xlate", S_IRUGO, NULL, NULL,
				&intc_irq_xlate_fops) == NULL)
		return -ENOMEM;

	return 0;
}
fs_initcall(intc_irq_xlate_init);
#endif

1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
static ssize_t
show_intc_name(struct sys_device *dev, struct sysdev_attribute *attr, char *buf)
{
	struct intc_desc_int *d;

	d = container_of(dev, struct intc_desc_int, sysdev);

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

static SYSDEV_ATTR(name, S_IRUGO, show_intc_name, NULL);

M
Magnus Damm 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308
static int intc_suspend(struct sys_device *dev, pm_message_t state)
{
	struct intc_desc_int *d;
	struct irq_desc *desc;
	int irq;

	/* get intc controller associated with this sysdev */
	d = container_of(dev, struct intc_desc_int, sysdev);

1309 1310 1311 1312 1313
	switch (state.event) {
	case PM_EVENT_ON:
		if (d->state.event != PM_EVENT_FREEZE)
			break;
		for_each_irq_desc(irq, desc) {
1314
			if (desc->handle_irq == intc_redirect_irq)
P
Paul Mundt 已提交
1315
				continue;
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
			if (desc->chip != &d->chip)
				continue;
			if (desc->status & IRQ_DISABLED)
				intc_disable(irq);
			else
				intc_enable(irq);
		}
		break;
	case PM_EVENT_FREEZE:
		/* nothing has to be done */
		break;
	case PM_EVENT_SUSPEND:
		/* enable wakeup irqs belonging to this intc controller */
		for_each_irq_desc(irq, desc) {
			if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
				intc_enable(irq);
		}
		break;
M
Magnus Damm 已提交
1334
	}
1335
	d->state = state;
M
Magnus Damm 已提交
1336 1337 1338 1339

	return 0;
}

1340 1341 1342 1343 1344
static int intc_resume(struct sys_device *dev)
{
	return intc_suspend(dev, PMSG_ON);
}

M
Magnus Damm 已提交
1345 1346 1347
static struct sysdev_class intc_sysdev_class = {
	.name = "intc",
	.suspend = intc_suspend,
1348
	.resume = intc_resume,
M
Magnus Damm 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
};

/* register this intc as sysdev to allow suspend/resume */
static int __init register_intc_sysdevs(void)
{
	struct intc_desc_int *d;
	int error;
	int id = 0;

	error = sysdev_class_register(&intc_sysdev_class);
P
Paul Mundt 已提交
1359 1360 1361 1362 1363
#ifdef CONFIG_INTC_USERIMASK
	if (!error && uimask)
		error = sysdev_class_create_file(&intc_sysdev_class,
						 &attr_userimask);
#endif
M
Magnus Damm 已提交
1364 1365 1366 1367 1368
	if (!error) {
		list_for_each_entry(d, &intc_list, list) {
			d->sysdev.id = id;
			d->sysdev.cls = &intc_sysdev_class;
			error = sysdev_register(&d->sysdev);
1369 1370 1371
			if (error == 0)
				error = sysdev_create_file(&d->sysdev,
							   &attr_name);
M
Magnus Damm 已提交
1372 1373
			if (error)
				break;
1374

M
Magnus Damm 已提交
1375 1376 1377 1378 1379
			id++;
		}
	}

	if (error)
1380
		pr_err("sysdev registration error\n");
M
Magnus Damm 已提交
1381 1382 1383 1384

	return error;
}
device_initcall(register_intc_sysdevs);
P
Paul Mundt 已提交
1385 1386 1387 1388

/*
 * Dynamic IRQ allocation and deallocation
 */
1389
unsigned int create_irq_nr(unsigned int irq_want, int node)
P
Paul Mundt 已提交
1390 1391 1392 1393 1394 1395 1396 1397
{
	unsigned int irq = 0, new;
	unsigned long flags;
	struct irq_desc *desc;

	spin_lock_irqsave(&vector_lock, flags);

	/*
1398
	 * First try the wanted IRQ
P
Paul Mundt 已提交
1399
	 */
1400 1401 1402 1403
	if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
		new = irq_want;
	} else {
		/* .. then fall back to scanning. */
P
Paul Mundt 已提交
1404 1405 1406 1407 1408 1409 1410
		new = find_first_zero_bit(intc_irq_map, nr_irqs);
		if (unlikely(new == nr_irqs))
			goto out_unlock;

		__set_bit(new, intc_irq_map);
	}

1411 1412
	desc = irq_to_desc_alloc_node(new, node);
	if (unlikely(!desc)) {
1413
		pr_err("can't get irq_desc for %d\n", new);
1414 1415 1416 1417 1418 1419
		goto out_unlock;
	}

	desc = move_irq_desc(desc, node);
	irq = new;

P
Paul Mundt 已提交
1420 1421 1422
out_unlock:
	spin_unlock_irqrestore(&vector_lock, flags);

1423
	if (irq > 0) {
P
Paul Mundt 已提交
1424
		dynamic_irq_init(irq);
1425 1426 1427 1428
#ifdef CONFIG_ARM
		set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
#endif
	}
P
Paul Mundt 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437

	return irq;
}

int create_irq(void)
{
	int nid = cpu_to_node(smp_processor_id());
	int irq;

1438
	irq = create_irq_nr(NR_IRQS_LEGACY, nid);
P
Paul Mundt 已提交
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
	if (irq == 0)
		irq = -1;

	return irq;
}

void destroy_irq(unsigned int irq)
{
	unsigned long flags;

	dynamic_irq_cleanup(irq);

	spin_lock_irqsave(&vector_lock, flags);
	__clear_bit(irq, intc_irq_map);
	spin_unlock_irqrestore(&vector_lock, flags);
}
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468

int reserve_irq_vector(unsigned int irq)
{
	unsigned long flags;
	int ret = 0;

	spin_lock_irqsave(&vector_lock, flags);
	if (test_and_set_bit(irq, intc_irq_map))
		ret = -EBUSY;
	spin_unlock_irqrestore(&vector_lock, flags);

	return ret;
}

1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
void reserve_intc_vectors(struct intc_vect *vectors, unsigned int nr_vecs)
{
	unsigned long flags;
	int i;

	spin_lock_irqsave(&vector_lock, flags);
	for (i = 0; i < nr_vecs; i++)
		__set_bit(evt2irq(vectors[i].vect), intc_irq_map);
	spin_unlock_irqrestore(&vector_lock, flags);
}

1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
void reserve_irq_legacy(void)
{
	unsigned long flags;
	int i, j;

	spin_lock_irqsave(&vector_lock, flags);
	j = find_first_bit(intc_irq_map, nr_irqs);
	for (i = 0; i < j; i++)
		__set_bit(i, intc_irq_map);
	spin_unlock_irqrestore(&vector_lock, flags);
}