Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Celleb/Beat Interrupt controller |
| 3 | * |
| 4 | * (C) Copyright 2006-2007 TOSHIBA CORPORATION |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/irq.h> |
| 24 | #include <linux/percpu.h> |
| 25 | #include <linux/types.h> |
| 26 | |
| 27 | #include <asm/machdep.h> |
| 28 | |
Ishizaki Kou | ad2c698 | 2008-04-24 19:31:40 +1000 | [diff] [blame] | 29 | #include "beat_interrupt.h" |
| 30 | #include "beat_wrapper.h" |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 31 | |
| 32 | #define MAX_IRQS NR_IRQS |
Thomas Gleixner | 5181e79 | 2010-02-18 02:22:52 +0000 | [diff] [blame] | 33 | static DEFINE_RAW_SPINLOCK(beatic_irq_mask_lock); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 34 | static uint64_t beatic_irq_mask_enable[(MAX_IRQS+255)/64]; |
| 35 | static uint64_t beatic_irq_mask_ack[(MAX_IRQS+255)/64]; |
| 36 | |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 37 | static struct irq_domain *beatic_host; |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * In this implementation, "virq" == "IRQ plug number", |
| 41 | * "(irq_hw_number_t)hwirq" == "IRQ outlet number". |
| 42 | */ |
| 43 | |
| 44 | /* assumption: locked */ |
| 45 | static inline void beatic_update_irq_mask(unsigned int irq_plug) |
| 46 | { |
| 47 | int off; |
| 48 | unsigned long masks[4]; |
| 49 | |
| 50 | off = (irq_plug / 256) * 4; |
| 51 | masks[0] = beatic_irq_mask_enable[off + 0] |
Ishizaki Kou | 2fe37a6 | 2008-03-14 23:19:34 +1100 | [diff] [blame] | 52 | & beatic_irq_mask_ack[off + 0]; |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 53 | masks[1] = beatic_irq_mask_enable[off + 1] |
Ishizaki Kou | 2fe37a6 | 2008-03-14 23:19:34 +1100 | [diff] [blame] | 54 | & beatic_irq_mask_ack[off + 1]; |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 55 | masks[2] = beatic_irq_mask_enable[off + 2] |
Ishizaki Kou | 2fe37a6 | 2008-03-14 23:19:34 +1100 | [diff] [blame] | 56 | & beatic_irq_mask_ack[off + 2]; |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 57 | masks[3] = beatic_irq_mask_enable[off + 3] |
Ishizaki Kou | 2fe37a6 | 2008-03-14 23:19:34 +1100 | [diff] [blame] | 58 | & beatic_irq_mask_ack[off + 3]; |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 59 | if (beat_set_interrupt_mask(irq_plug&~255UL, |
| 60 | masks[0], masks[1], masks[2], masks[3]) != 0) |
| 61 | panic("Failed to set mask IRQ!"); |
| 62 | } |
| 63 | |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 64 | static void beatic_mask_irq(struct irq_data *d) |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 65 | { |
| 66 | unsigned long flags; |
| 67 | |
Thomas Gleixner | 5181e79 | 2010-02-18 02:22:52 +0000 | [diff] [blame] | 68 | raw_spin_lock_irqsave(&beatic_irq_mask_lock, flags); |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 69 | beatic_irq_mask_enable[d->irq/64] &= ~(1UL << (63 - (d->irq%64))); |
| 70 | beatic_update_irq_mask(d->irq); |
Thomas Gleixner | 5181e79 | 2010-02-18 02:22:52 +0000 | [diff] [blame] | 71 | raw_spin_unlock_irqrestore(&beatic_irq_mask_lock, flags); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 72 | } |
| 73 | |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 74 | static void beatic_unmask_irq(struct irq_data *d) |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 75 | { |
| 76 | unsigned long flags; |
| 77 | |
Thomas Gleixner | 5181e79 | 2010-02-18 02:22:52 +0000 | [diff] [blame] | 78 | raw_spin_lock_irqsave(&beatic_irq_mask_lock, flags); |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 79 | beatic_irq_mask_enable[d->irq/64] |= 1UL << (63 - (d->irq%64)); |
| 80 | beatic_update_irq_mask(d->irq); |
Thomas Gleixner | 5181e79 | 2010-02-18 02:22:52 +0000 | [diff] [blame] | 81 | raw_spin_unlock_irqrestore(&beatic_irq_mask_lock, flags); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 82 | } |
| 83 | |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 84 | static void beatic_ack_irq(struct irq_data *d) |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 85 | { |
| 86 | unsigned long flags; |
| 87 | |
Thomas Gleixner | 5181e79 | 2010-02-18 02:22:52 +0000 | [diff] [blame] | 88 | raw_spin_lock_irqsave(&beatic_irq_mask_lock, flags); |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 89 | beatic_irq_mask_ack[d->irq/64] &= ~(1UL << (63 - (d->irq%64))); |
| 90 | beatic_update_irq_mask(d->irq); |
Thomas Gleixner | 5181e79 | 2010-02-18 02:22:52 +0000 | [diff] [blame] | 91 | raw_spin_unlock_irqrestore(&beatic_irq_mask_lock, flags); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 92 | } |
| 93 | |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 94 | static void beatic_end_irq(struct irq_data *d) |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 95 | { |
| 96 | s64 err; |
| 97 | unsigned long flags; |
| 98 | |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 99 | err = beat_downcount_of_interrupt(d->irq); |
Ishizaki Kou | 2fe37a6 | 2008-03-14 23:19:34 +1100 | [diff] [blame] | 100 | if (err != 0) { |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 101 | if ((err & 0xFFFFFFFF) != 0xFFFFFFF5) /* -11: wrong state */ |
Ingo Molnar | fe33332 | 2009-01-06 14:26:03 +0000 | [diff] [blame] | 102 | panic("Failed to downcount IRQ! Error = %16llx", err); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 103 | |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 104 | printk(KERN_ERR "IRQ over-downcounted, plug %d\n", d->irq); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 105 | } |
Thomas Gleixner | 5181e79 | 2010-02-18 02:22:52 +0000 | [diff] [blame] | 106 | raw_spin_lock_irqsave(&beatic_irq_mask_lock, flags); |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 107 | beatic_irq_mask_ack[d->irq/64] |= 1UL << (63 - (d->irq%64)); |
| 108 | beatic_update_irq_mask(d->irq); |
Thomas Gleixner | 5181e79 | 2010-02-18 02:22:52 +0000 | [diff] [blame] | 109 | raw_spin_unlock_irqrestore(&beatic_irq_mask_lock, flags); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static struct irq_chip beatic_pic = { |
Anton Blanchard | fc380c0 | 2010-01-31 20:33:41 +0000 | [diff] [blame] | 113 | .name = "CELL-BEAT", |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 114 | .irq_unmask = beatic_unmask_irq, |
| 115 | .irq_mask = beatic_mask_irq, |
| 116 | .irq_eoi = beatic_end_irq, |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | /* |
| 120 | * Dispose binding hardware IRQ number (hw) and Virtuql IRQ number (virq), |
| 121 | * update flags. |
| 122 | * |
| 123 | * Note that the number (virq) is already assigned at upper layer. |
| 124 | */ |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 125 | static void beatic_pic_host_unmap(struct irq_domain *h, unsigned int virq) |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 126 | { |
| 127 | beat_destruct_irq_plug(virq); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Create or update binding hardware IRQ number (hw) and Virtuql |
| 132 | * IRQ number (virq). This is called only once for a given mapping. |
| 133 | * |
| 134 | * Note that the number (virq) is already assigned at upper layer. |
| 135 | */ |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 136 | static int beatic_pic_host_map(struct irq_domain *h, unsigned int virq, |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 137 | irq_hw_number_t hw) |
| 138 | { |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 139 | int64_t err; |
| 140 | |
Ishizaki Kou | 2fe37a6 | 2008-03-14 23:19:34 +1100 | [diff] [blame] | 141 | err = beat_construct_and_connect_irq_plug(virq, hw); |
| 142 | if (err < 0) |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 143 | return -EIO; |
| 144 | |
Thomas Gleixner | 98488db | 2011-03-25 15:43:57 +0100 | [diff] [blame] | 145 | irq_set_status_flags(virq, IRQ_LEVEL); |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 146 | irq_set_chip_and_handler(virq, &beatic_pic, handle_fasteoi_irq); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 151 | * Translate device-tree interrupt spec to irq_hw_number_t style (ulong), |
| 152 | * to pass away to irq_create_mapping(). |
| 153 | * |
| 154 | * Called from irq_create_of_mapping() only. |
| 155 | * Note: We have only 1 entry to translate. |
| 156 | */ |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 157 | static int beatic_pic_host_xlate(struct irq_domain *h, struct device_node *ct, |
Roman Fietze | 40d50cf | 2009-12-08 02:39:50 +0000 | [diff] [blame] | 158 | const u32 *intspec, unsigned int intsize, |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 159 | irq_hw_number_t *out_hwirq, |
| 160 | unsigned int *out_flags) |
| 161 | { |
Roman Fietze | 40d50cf | 2009-12-08 02:39:50 +0000 | [diff] [blame] | 162 | const u64 *intspec2 = (const u64 *)intspec; |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 163 | |
| 164 | *out_hwirq = *intspec2; |
| 165 | *out_flags |= IRQ_TYPE_LEVEL_LOW; |
| 166 | return 0; |
| 167 | } |
| 168 | |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 169 | static int beatic_pic_host_match(struct irq_domain *h, struct device_node *np) |
Michael Ellerman | 8528ab8 | 2007-08-28 18:47:55 +1000 | [diff] [blame] | 170 | { |
| 171 | /* Match all */ |
| 172 | return 1; |
| 173 | } |
| 174 | |
Grant Likely | 9f70b8e | 2012-01-26 12:24:34 -0700 | [diff] [blame] | 175 | static const struct irq_domain_ops beatic_pic_host_ops = { |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 176 | .map = beatic_pic_host_map, |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 177 | .unmap = beatic_pic_host_unmap, |
| 178 | .xlate = beatic_pic_host_xlate, |
Michael Ellerman | 8528ab8 | 2007-08-28 18:47:55 +1000 | [diff] [blame] | 179 | .match = beatic_pic_host_match, |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | /* |
| 183 | * Get an IRQ number |
| 184 | * Note: returns VIRQ |
| 185 | */ |
| 186 | static inline unsigned int beatic_get_irq_plug(void) |
| 187 | { |
| 188 | int i; |
| 189 | uint64_t pending[4], ub; |
| 190 | |
| 191 | for (i = 0; i < MAX_IRQS; i += 256) { |
| 192 | beat_detect_pending_interrupts(i, pending); |
| 193 | __asm__ ("cntlzd %0,%1":"=r"(ub): |
| 194 | "r"(pending[0] & beatic_irq_mask_enable[i/64+0] |
Ishizaki Kou | 2fe37a6 | 2008-03-14 23:19:34 +1100 | [diff] [blame] | 195 | & beatic_irq_mask_ack[i/64+0])); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 196 | if (ub != 64) |
| 197 | return i + ub + 0; |
| 198 | __asm__ ("cntlzd %0,%1":"=r"(ub): |
| 199 | "r"(pending[1] & beatic_irq_mask_enable[i/64+1] |
Ishizaki Kou | 2fe37a6 | 2008-03-14 23:19:34 +1100 | [diff] [blame] | 200 | & beatic_irq_mask_ack[i/64+1])); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 201 | if (ub != 64) |
| 202 | return i + ub + 64; |
| 203 | __asm__ ("cntlzd %0,%1":"=r"(ub): |
| 204 | "r"(pending[2] & beatic_irq_mask_enable[i/64+2] |
Ishizaki Kou | 2fe37a6 | 2008-03-14 23:19:34 +1100 | [diff] [blame] | 205 | & beatic_irq_mask_ack[i/64+2])); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 206 | if (ub != 64) |
| 207 | return i + ub + 128; |
| 208 | __asm__ ("cntlzd %0,%1":"=r"(ub): |
| 209 | "r"(pending[3] & beatic_irq_mask_enable[i/64+3] |
Ishizaki Kou | 2fe37a6 | 2008-03-14 23:19:34 +1100 | [diff] [blame] | 210 | & beatic_irq_mask_ack[i/64+3])); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 211 | if (ub != 64) |
| 212 | return i + ub + 192; |
| 213 | } |
| 214 | |
| 215 | return NO_IRQ; |
| 216 | } |
| 217 | unsigned int beatic_get_irq(void) |
| 218 | { |
| 219 | unsigned int ret; |
| 220 | |
| 221 | ret = beatic_get_irq_plug(); |
| 222 | if (ret != NO_IRQ) |
Lennert Buytenhek | d1ae63d | 2011-03-07 13:59:28 +0000 | [diff] [blame] | 223 | beatic_ack_irq(irq_get_irq_data(ret)); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | */ |
| 229 | void __init beatic_init_IRQ(void) |
| 230 | { |
| 231 | int i; |
| 232 | |
| 233 | memset(beatic_irq_mask_enable, 0, sizeof(beatic_irq_mask_enable)); |
| 234 | memset(beatic_irq_mask_ack, 255, sizeof(beatic_irq_mask_ack)); |
| 235 | for (i = 0; i < MAX_IRQS; i += 256) |
| 236 | beat_set_interrupt_mask(i, 0L, 0L, 0L, 0L); |
| 237 | |
| 238 | /* Set out get_irq function */ |
| 239 | ppc_md.get_irq = beatic_get_irq; |
| 240 | |
| 241 | /* Allocate an irq host */ |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 242 | beatic_host = irq_domain_add_nomap(NULL, ~0, &beatic_pic_host_ops, NULL); |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 243 | BUG_ON(beatic_host == NULL); |
| 244 | irq_set_default_host(beatic_host); |
| 245 | } |
| 246 | |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 247 | void beatic_deinit_IRQ(void) |
| 248 | { |
| 249 | int i; |
| 250 | |
Grant Likely | 4013369 | 2012-04-23 12:30:02 +0000 | [diff] [blame] | 251 | for (i = 1; i < nr_irqs; i++) |
Ishizaki Kou | 32f39b0 | 2007-02-02 16:37:30 +0900 | [diff] [blame] | 252 | beat_destruct_irq_plug(i); |
| 253 | } |