blob: 7e562ccb699734b3b6ce77579309cbdb87437817 [file] [log] [blame]
Paul Mundt2be6bb0c2010-10-05 22:10:30 +09001/*
2 * Shared interrupt handling code for IPR and INTC2 types of IRQs.
3 *
4 * Copyright (C) 2007, 2008 Magnus Damm
Paul Mundtb59f9f92012-01-24 17:41:55 +09005 * Copyright (C) 2009 - 2012 Paul Mundt
Paul Mundt2be6bb0c2010-10-05 22:10:30 +09006 *
7 * Based on intc2.c and ipr.c
8 *
9 * Copyright (C) 1999 Niibe Yutaka & Takeshi Yaegashi
10 * Copyright (C) 2000 Kazumoto Kojima
11 * Copyright (C) 2001 David J. Mckay (david.mckay@st.com)
12 * Copyright (C) 2003 Takashi Kusuda <kusuda-takashi@hitachi-ul.co.jp>
13 * Copyright (C) 2005, 2006 Paul Mundt
14 *
15 * This file is subject to the terms and conditions of the GNU General Public
16 * License. See the file "COPYING" in the main directory of this archive
17 * for more details.
18 */
19#define pr_fmt(fmt) "intc: " fmt
20
21#include <linux/init.h>
22#include <linux/irq.h>
23#include <linux/io.h>
24#include <linux/slab.h>
Paul Gortmaker0c438712011-07-31 17:40:26 -040025#include <linux/stat.h>
Paul Mundt2be6bb0c2010-10-05 22:10:30 +090026#include <linux/interrupt.h>
27#include <linux/sh_intc.h>
Kay Sieversf4e73bf2011-12-21 15:09:52 -080028#include <linux/device.h>
Rafael J. Wysockia696b892011-03-22 20:19:28 +000029#include <linux/syscore_ops.h>
Paul Mundt2be6bb0c2010-10-05 22:10:30 +090030#include <linux/list.h>
31#include <linux/spinlock.h>
32#include <linux/radix-tree.h>
Paul Gortmakerf7be3452011-07-31 19:20:02 -040033#include <linux/export.h>
Paul Mundtb59f9f92012-01-24 17:41:55 +090034#include <linux/sort.h>
Paul Mundt2be6bb0c2010-10-05 22:10:30 +090035#include "internals.h"
36
37LIST_HEAD(intc_list);
38DEFINE_RAW_SPINLOCK(intc_big_lock);
Paul Mundt5fbebcb2012-01-24 14:07:18 +090039static unsigned int nr_intc_controllers;
Paul Mundt2be6bb0c2010-10-05 22:10:30 +090040
41/*
42 * Default priority level
43 * - this needs to be at least 2 for 5-bit priorities on 7780
44 */
45static unsigned int default_prio_level = 2; /* 2 - 16 */
Rob Herring0f552392012-01-17 13:10:25 -060046static unsigned int intc_prio_level[INTC_NR_IRQS]; /* for now */
Paul Mundt2be6bb0c2010-10-05 22:10:30 +090047
48unsigned int intc_get_dfl_prio_level(void)
49{
50 return default_prio_level;
51}
52
53unsigned int intc_get_prio_level(unsigned int irq)
54{
55 return intc_prio_level[irq];
56}
57
58void intc_set_prio_level(unsigned int irq, unsigned int level)
59{
60 unsigned long flags;
61
62 raw_spin_lock_irqsave(&intc_big_lock, flags);
63 intc_prio_level[irq] = level;
64 raw_spin_unlock_irqrestore(&intc_big_lock, flags);
65}
66
67static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
68{
Thomas Gleixnerfcb89182011-03-24 16:31:17 +010069 generic_handle_irq((unsigned int)irq_get_handler_data(irq));
Paul Mundt2be6bb0c2010-10-05 22:10:30 +090070}
71
72static void __init intc_register_irq(struct intc_desc *desc,
73 struct intc_desc_int *d,
74 intc_enum enum_id,
75 unsigned int irq)
76{
77 struct intc_handle_int *hp;
Paul Mundt26599a92010-10-27 15:42:10 +090078 struct irq_data *irq_data;
Paul Mundt2be6bb0c2010-10-05 22:10:30 +090079 unsigned int data[2], primary;
80 unsigned long flags;
81
82 /*
83 * Register the IRQ position with the global IRQ map, then insert
84 * it in to the radix tree.
85 */
Paul Mundt20f95e02010-11-01 16:10:48 -040086 irq_reserve_irq(irq);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +090087
88 raw_spin_lock_irqsave(&intc_big_lock, flags);
89 radix_tree_insert(&d->tree, enum_id, intc_irq_xlate_get(irq));
90 raw_spin_unlock_irqrestore(&intc_big_lock, flags);
91
92 /*
93 * Prefer single interrupt source bitmap over other combinations:
94 *
95 * 1. bitmap, single interrupt source
96 * 2. priority, single interrupt source
97 * 3. bitmap, multiple interrupt sources (groups)
98 * 4. priority, multiple interrupt sources (groups)
99 */
100 data[0] = intc_get_mask_handle(desc, d, enum_id, 0);
101 data[1] = intc_get_prio_handle(desc, d, enum_id, 0);
102
103 primary = 0;
104 if (!data[0] && data[1])
105 primary = 1;
106
107 if (!data[0] && !data[1])
108 pr_warning("missing unique irq mask for irq %d (vect 0x%04x)\n",
109 irq, irq2evt(irq));
110
111 data[0] = data[0] ? data[0] : intc_get_mask_handle(desc, d, enum_id, 1);
112 data[1] = data[1] ? data[1] : intc_get_prio_handle(desc, d, enum_id, 1);
113
114 if (!data[primary])
115 primary ^= 1;
116
117 BUG_ON(!data[primary]); /* must have primary masking method */
118
Paul Mundt26599a92010-10-27 15:42:10 +0900119 irq_data = irq_get_irq_data(irq);
120
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900121 disable_irq_nosync(irq);
Thomas Gleixnerfcb89182011-03-24 16:31:17 +0100122 irq_set_chip_and_handler_name(irq, &d->chip, handle_level_irq,
123 "level");
124 irq_set_chip_data(irq, (void *)data[primary]);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900125
126 /*
127 * set priority level
128 */
129 intc_set_prio_level(irq, intc_get_dfl_prio_level());
130
131 /* enable secondary masking method if present */
132 if (data[!primary])
Paul Mundt26599a92010-10-27 15:42:10 +0900133 _intc_enable(irq_data, data[!primary]);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900134
135 /* add irq to d->prio list if priority is available */
136 if (data[1]) {
137 hp = d->prio + d->nr_prio;
138 hp->irq = irq;
139 hp->handle = data[1];
140
141 if (primary) {
142 /*
143 * only secondary priority should access registers, so
144 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
145 */
146 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
147 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
148 }
149 d->nr_prio++;
150 }
151
152 /* add irq to d->sense list if sense is available */
153 data[0] = intc_get_sense_handle(desc, d, enum_id);
154 if (data[0]) {
155 (d->sense + d->nr_sense)->irq = irq;
156 (d->sense + d->nr_sense)->handle = data[0];
157 d->nr_sense++;
158 }
159
160 /* irq should be disabled by default */
Paul Mundt26599a92010-10-27 15:42:10 +0900161 d->chip.irq_mask(irq_data);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900162
163 intc_set_ack_handle(irq, desc, d, enum_id);
164 intc_set_dist_handle(irq, desc, d, enum_id);
165
166 activate_irq(irq);
167}
168
169static unsigned int __init save_reg(struct intc_desc_int *d,
170 unsigned int cnt,
171 unsigned long value,
172 unsigned int smp)
173{
174 if (value) {
175 value = intc_phys_to_virt(d, value);
176
177 d->reg[cnt] = value;
178#ifdef CONFIG_SMP
179 d->smp[cnt] = smp;
180#endif
181 return 1;
182 }
183
184 return 0;
185}
186
187int __init register_intc_controller(struct intc_desc *desc)
188{
189 unsigned int i, k, smp;
190 struct intc_hw_desc *hw = &desc->hw;
191 struct intc_desc_int *d;
192 struct resource *res;
193
194 pr_info("Registered controller '%s' with %u IRQs\n",
195 desc->name, hw->nr_vectors);
196
197 d = kzalloc(sizeof(*d), GFP_NOWAIT);
198 if (!d)
199 goto err0;
200
201 INIT_LIST_HEAD(&d->list);
202 list_add_tail(&d->list, &intc_list);
203
204 raw_spin_lock_init(&d->lock);
Paul Mundt30f2ba32010-12-24 19:38:37 +0900205 INIT_RADIX_TREE(&d->tree, GFP_ATOMIC);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900206
207 d->index = nr_intc_controllers;
208
209 if (desc->num_resources) {
210 d->nr_windows = desc->num_resources;
211 d->window = kzalloc(d->nr_windows * sizeof(*d->window),
212 GFP_NOWAIT);
213 if (!d->window)
214 goto err1;
215
216 for (k = 0; k < d->nr_windows; k++) {
217 res = desc->resource + k;
218 WARN_ON(resource_type(res) != IORESOURCE_MEM);
219 d->window[k].phys = res->start;
220 d->window[k].size = resource_size(res);
221 d->window[k].virt = ioremap_nocache(res->start,
222 resource_size(res));
223 if (!d->window[k].virt)
224 goto err2;
225 }
226 }
227
228 d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
229#ifdef CONFIG_INTC_BALANCING
230 if (d->nr_reg)
231 d->nr_reg += hw->nr_mask_regs;
232#endif
233 d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
234 d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
235 d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
236 d->nr_reg += hw->subgroups ? hw->nr_subgroups : 0;
237
238 d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
239 if (!d->reg)
240 goto err2;
241
242#ifdef CONFIG_SMP
243 d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
244 if (!d->smp)
245 goto err3;
246#endif
247 k = 0;
248
249 if (hw->mask_regs) {
250 for (i = 0; i < hw->nr_mask_regs; i++) {
251 smp = IS_SMP(hw->mask_regs[i]);
252 k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
253 k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
254#ifdef CONFIG_INTC_BALANCING
255 k += save_reg(d, k, hw->mask_regs[i].dist_reg, 0);
256#endif
257 }
258 }
259
260 if (hw->prio_regs) {
261 d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
262 GFP_NOWAIT);
263 if (!d->prio)
264 goto err4;
265
266 for (i = 0; i < hw->nr_prio_regs; i++) {
267 smp = IS_SMP(hw->prio_regs[i]);
268 k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
269 k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
270 }
Paul Mundtb59f9f92012-01-24 17:41:55 +0900271
272 sort(d->prio, hw->nr_prio_regs, sizeof(*d->prio),
273 intc_handle_int_cmp, NULL);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900274 }
275
276 if (hw->sense_regs) {
277 d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
278 GFP_NOWAIT);
279 if (!d->sense)
280 goto err5;
281
282 for (i = 0; i < hw->nr_sense_regs; i++)
283 k += save_reg(d, k, hw->sense_regs[i].reg, 0);
Paul Mundtb59f9f92012-01-24 17:41:55 +0900284
285 sort(d->sense, hw->nr_sense_regs, sizeof(*d->sense),
286 intc_handle_int_cmp, NULL);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900287 }
288
289 if (hw->subgroups)
290 for (i = 0; i < hw->nr_subgroups; i++)
291 if (hw->subgroups[i].reg)
292 k+= save_reg(d, k, hw->subgroups[i].reg, 0);
293
294 memcpy(&d->chip, &intc_irq_chip, sizeof(struct irq_chip));
295 d->chip.name = desc->name;
296
297 if (hw->ack_regs)
298 for (i = 0; i < hw->nr_ack_regs; i++)
299 k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
300 else
Paul Mundt26599a92010-10-27 15:42:10 +0900301 d->chip.irq_mask_ack = d->chip.irq_disable;
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900302
303 /* disable bits matching force_disable before registering irqs */
304 if (desc->force_disable)
305 intc_enable_disable_enum(desc, d, desc->force_disable, 0);
306
307 /* disable bits matching force_enable before registering irqs */
308 if (desc->force_enable)
309 intc_enable_disable_enum(desc, d, desc->force_enable, 0);
310
311 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
312
313 /* register the vectors one by one */
314 for (i = 0; i < hw->nr_vectors; i++) {
315 struct intc_vect *vect = hw->vectors + i;
316 unsigned int irq = evt2irq(vect->vect);
Thomas Gleixnerc4318ba2010-10-12 02:03:09 +0900317 int res;
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900318
319 if (!vect->enum_id)
320 continue;
321
Thomas Gleixnerc4318ba2010-10-12 02:03:09 +0900322 res = irq_alloc_desc_at(irq, numa_node_id());
323 if (res != irq && res != -EEXIST) {
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900324 pr_err("can't get irq_desc for %d\n", irq);
325 continue;
326 }
327
328 intc_irq_xlate_set(irq, vect->enum_id, d);
329 intc_register_irq(desc, d, vect->enum_id, irq);
330
331 for (k = i + 1; k < hw->nr_vectors; k++) {
332 struct intc_vect *vect2 = hw->vectors + k;
333 unsigned int irq2 = evt2irq(vect2->vect);
334
335 if (vect->enum_id != vect2->enum_id)
336 continue;
337
338 /*
339 * In the case of multi-evt handling and sparse
340 * IRQ support, each vector still needs to have
341 * its own backing irq_desc.
342 */
Thomas Gleixnerc4318ba2010-10-12 02:03:09 +0900343 res = irq_alloc_desc_at(irq2, numa_node_id());
344 if (res != irq2 && res != -EEXIST) {
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900345 pr_err("can't get irq_desc for %d\n", irq2);
346 continue;
347 }
348
349 vect2->enum_id = 0;
350
351 /* redirect this interrupts to the first one */
Thomas Gleixnerfcb89182011-03-24 16:31:17 +0100352 irq_set_chip(irq2, &dummy_irq_chip);
353 irq_set_chained_handler(irq2, intc_redirect_irq);
354 irq_set_handler_data(irq2, (void *)irq);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900355 }
356 }
357
358 intc_subgroup_init(desc, d);
359
360 /* enable bits matching force_enable after registering irqs */
361 if (desc->force_enable)
362 intc_enable_disable_enum(desc, d, desc->force_enable, 1);
363
Rafael J. Wysocki0f966d72011-12-23 01:23:30 +0100364 d->skip_suspend = desc->skip_syscore_suspend;
365
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900366 nr_intc_controllers++;
367
368 return 0;
369err5:
370 kfree(d->prio);
371err4:
372#ifdef CONFIG_SMP
373 kfree(d->smp);
374err3:
375#endif
376 kfree(d->reg);
377err2:
378 for (k = 0; k < d->nr_windows; k++)
379 if (d->window[k].virt)
380 iounmap(d->window[k].virt);
381
382 kfree(d->window);
383err1:
384 kfree(d);
385err0:
386 pr_err("unable to allocate INTC memory\n");
387
388 return -ENOMEM;
389}
390
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000391static int intc_suspend(void)
392{
393 struct intc_desc_int *d;
394
395 list_for_each_entry(d, &intc_list, list) {
396 int irq;
397
Rafael J. Wysocki0f966d72011-12-23 01:23:30 +0100398 if (d->skip_suspend)
399 continue;
400
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000401 /* enable wakeup irqs belonging to this intc controller */
402 for_each_active_irq(irq) {
403 struct irq_data *data;
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000404 struct irq_chip *chip;
405
406 data = irq_get_irq_data(irq);
407 chip = irq_data_get_irq_chip(data);
408 if (chip != &d->chip)
409 continue;
Thomas Gleixnera821b272011-03-24 14:47:46 +0100410 if (irqd_is_wakeup_set(data))
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000411 chip->irq_enable(data);
412 }
413 }
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000414 return 0;
415}
416
417static void intc_resume(void)
418{
419 struct intc_desc_int *d;
420
421 list_for_each_entry(d, &intc_list, list) {
422 int irq;
423
Rafael J. Wysocki0f966d72011-12-23 01:23:30 +0100424 if (d->skip_suspend)
425 continue;
426
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000427 for_each_active_irq(irq) {
428 struct irq_data *data;
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000429 struct irq_chip *chip;
430
431 data = irq_get_irq_data(irq);
432 chip = irq_data_get_irq_chip(data);
433 /*
434 * This will catch the redirect and VIRQ cases
435 * due to the dummy_irq_chip being inserted.
436 */
437 if (chip != &d->chip)
438 continue;
Thomas Gleixnera821b272011-03-24 14:47:46 +0100439 if (irqd_irq_disabled(data))
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000440 chip->irq_disable(data);
441 else
442 chip->irq_enable(data);
443 }
444 }
445}
446
447struct syscore_ops intc_syscore_ops = {
448 .suspend = intc_suspend,
449 .resume = intc_resume,
450};
451
Kay Sieversf4e73bf2011-12-21 15:09:52 -0800452struct bus_type intc_subsys = {
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000453 .name = "intc",
Kay Sieversf4e73bf2011-12-21 15:09:52 -0800454 .dev_name = "intc",
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000455};
456
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900457static ssize_t
Kay Sieversf4e73bf2011-12-21 15:09:52 -0800458show_intc_name(struct device *dev, struct device_attribute *attr, char *buf)
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900459{
460 struct intc_desc_int *d;
461
Kay Sieversf4e73bf2011-12-21 15:09:52 -0800462 d = container_of(dev, struct intc_desc_int, dev);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900463
464 return sprintf(buf, "%s\n", d->chip.name);
465}
466
Kay Sieversf4e73bf2011-12-21 15:09:52 -0800467static DEVICE_ATTR(name, S_IRUGO, show_intc_name, NULL);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900468
Kay Sieversf4e73bf2011-12-21 15:09:52 -0800469static int __init register_intc_devs(void)
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900470{
471 struct intc_desc_int *d;
472 int error;
473
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000474 register_syscore_ops(&intc_syscore_ops);
475
Kay Sieversf4e73bf2011-12-21 15:09:52 -0800476 error = subsys_system_register(&intc_subsys, NULL);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900477 if (!error) {
478 list_for_each_entry(d, &intc_list, list) {
Kay Sieversf4e73bf2011-12-21 15:09:52 -0800479 d->dev.id = d->index;
480 d->dev.bus = &intc_subsys;
481 error = device_register(&d->dev);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900482 if (error == 0)
Kay Sieversf4e73bf2011-12-21 15:09:52 -0800483 error = device_create_file(&d->dev,
484 &dev_attr_name);
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900485 if (error)
486 break;
487 }
488 }
489
490 if (error)
Kay Sieversf4e73bf2011-12-21 15:09:52 -0800491 pr_err("device registration error\n");
Paul Mundt2be6bb0c2010-10-05 22:10:30 +0900492
493 return error;
494}
Kay Sieversf4e73bf2011-12-21 15:09:52 -0800495device_initcall(register_intc_devs);