core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Shared interrupt handling code for IPR and INTC2 types of IRQs.
  3. *
  4. * Copyright (C) 2007, 2008 Magnus Damm
  5. * Copyright (C) 2009 - 2012 Paul Mundt
  6. *
  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. #include <linux/init.h>
  21. #include <linux/irq.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <linux/stat.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/sh_intc.h>
  27. #include <linux/irqdomain.h>
  28. #include <linux/device.h>
  29. #include <linux/syscore_ops.h>
  30. #include <linux/list.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/radix-tree.h>
  33. #include <linux/export.h>
  34. #include <linux/sort.h>
  35. #include "internals.h"
  36. LIST_HEAD(intc_list);
  37. DEFINE_RAW_SPINLOCK(intc_big_lock);
  38. static unsigned int nr_intc_controllers;
  39. /*
  40. * Default priority level
  41. * - this needs to be at least 2 for 5-bit priorities on 7780
  42. */
  43. static unsigned int default_prio_level = 2; /* 2 - 16 */
  44. static unsigned int intc_prio_level[INTC_NR_IRQS]; /* for now */
  45. unsigned int intc_get_dfl_prio_level(void)
  46. {
  47. return default_prio_level;
  48. }
  49. unsigned int intc_get_prio_level(unsigned int irq)
  50. {
  51. return intc_prio_level[irq];
  52. }
  53. void intc_set_prio_level(unsigned int irq, unsigned int level)
  54. {
  55. unsigned long flags;
  56. raw_spin_lock_irqsave(&intc_big_lock, flags);
  57. intc_prio_level[irq] = level;
  58. raw_spin_unlock_irqrestore(&intc_big_lock, flags);
  59. }
  60. static void intc_redirect_irq(struct irq_desc *desc)
  61. {
  62. generic_handle_irq((unsigned int)irq_desc_get_handler_data(desc));
  63. }
  64. static void __init intc_register_irq(struct intc_desc *desc,
  65. struct intc_desc_int *d,
  66. intc_enum enum_id,
  67. unsigned int irq)
  68. {
  69. struct intc_handle_int *hp;
  70. struct irq_data *irq_data;
  71. unsigned int data[2], primary;
  72. unsigned long flags;
  73. raw_spin_lock_irqsave(&intc_big_lock, flags);
  74. radix_tree_insert(&d->tree, enum_id, intc_irq_xlate_get(irq));
  75. raw_spin_unlock_irqrestore(&intc_big_lock, flags);
  76. /*
  77. * Prefer single interrupt source bitmap over other combinations:
  78. *
  79. * 1. bitmap, single interrupt source
  80. * 2. priority, single interrupt source
  81. * 3. bitmap, multiple interrupt sources (groups)
  82. * 4. priority, multiple interrupt sources (groups)
  83. */
  84. data[0] = intc_get_mask_handle(desc, d, enum_id, 0);
  85. data[1] = intc_get_prio_handle(desc, d, enum_id, 0);
  86. primary = 0;
  87. if (!data[0] && data[1])
  88. primary = 1;
  89. if (!data[0] && !data[1])
  90. pr_warning("missing unique irq mask for irq %d (vect 0x%04x)\n",
  91. irq, irq2evt(irq));
  92. data[0] = data[0] ? data[0] : intc_get_mask_handle(desc, d, enum_id, 1);
  93. data[1] = data[1] ? data[1] : intc_get_prio_handle(desc, d, enum_id, 1);
  94. if (!data[primary])
  95. primary ^= 1;
  96. BUG_ON(!data[primary]); /* must have primary masking method */
  97. irq_data = irq_get_irq_data(irq);
  98. disable_irq_nosync(irq);
  99. irq_set_chip_and_handler_name(irq, &d->chip, handle_level_irq,
  100. "level");
  101. irq_set_chip_data(irq, (void *)data[primary]);
  102. /*
  103. * set priority level
  104. */
  105. intc_set_prio_level(irq, intc_get_dfl_prio_level());
  106. /* enable secondary masking method if present */
  107. if (data[!primary])
  108. _intc_enable(irq_data, data[!primary]);
  109. /* add irq to d->prio list if priority is available */
  110. if (data[1]) {
  111. hp = d->prio + d->nr_prio;
  112. hp->irq = irq;
  113. hp->handle = data[1];
  114. if (primary) {
  115. /*
  116. * only secondary priority should access registers, so
  117. * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
  118. */
  119. hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
  120. hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
  121. }
  122. d->nr_prio++;
  123. }
  124. /* add irq to d->sense list if sense is available */
  125. data[0] = intc_get_sense_handle(desc, d, enum_id);
  126. if (data[0]) {
  127. (d->sense + d->nr_sense)->irq = irq;
  128. (d->sense + d->nr_sense)->handle = data[0];
  129. d->nr_sense++;
  130. }
  131. /* irq should be disabled by default */
  132. d->chip.irq_mask(irq_data);
  133. intc_set_ack_handle(irq, desc, d, enum_id);
  134. intc_set_dist_handle(irq, desc, d, enum_id);
  135. activate_irq(irq);
  136. }
  137. static unsigned int __init save_reg(struct intc_desc_int *d,
  138. unsigned int cnt,
  139. unsigned long value,
  140. unsigned int smp)
  141. {
  142. if (value) {
  143. value = intc_phys_to_virt(d, value);
  144. d->reg[cnt] = value;
  145. #ifdef CONFIG_SMP
  146. d->smp[cnt] = smp;
  147. #endif
  148. return 1;
  149. }
  150. return 0;
  151. }
  152. int __init register_intc_controller(struct intc_desc *desc)
  153. {
  154. unsigned int i, k, smp;
  155. struct intc_hw_desc *hw = &desc->hw;
  156. struct intc_desc_int *d;
  157. struct resource *res;
  158. pr_info("Registered controller '%s' with %u IRQs\n",
  159. desc->name, hw->nr_vectors);
  160. d = kzalloc(sizeof(*d), GFP_NOWAIT);
  161. if (!d)
  162. goto err0;
  163. INIT_LIST_HEAD(&d->list);
  164. list_add_tail(&d->list, &intc_list);
  165. raw_spin_lock_init(&d->lock);
  166. INIT_RADIX_TREE(&d->tree, GFP_ATOMIC);
  167. d->index = nr_intc_controllers;
  168. if (desc->num_resources) {
  169. d->nr_windows = desc->num_resources;
  170. d->window = kcalloc(d->nr_windows, sizeof(*d->window),
  171. GFP_NOWAIT);
  172. if (!d->window)
  173. goto err1;
  174. for (k = 0; k < d->nr_windows; k++) {
  175. res = desc->resource + k;
  176. WARN_ON(resource_type(res) != IORESOURCE_MEM);
  177. d->window[k].phys = res->start;
  178. d->window[k].size = resource_size(res);
  179. d->window[k].virt = ioremap_nocache(res->start,
  180. resource_size(res));
  181. if (!d->window[k].virt)
  182. goto err2;
  183. }
  184. }
  185. d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
  186. #ifdef CONFIG_INTC_BALANCING
  187. if (d->nr_reg)
  188. d->nr_reg += hw->nr_mask_regs;
  189. #endif
  190. d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
  191. d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
  192. d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
  193. d->nr_reg += hw->subgroups ? hw->nr_subgroups : 0;
  194. d->reg = kcalloc(d->nr_reg, sizeof(*d->reg), GFP_NOWAIT);
  195. if (!d->reg)
  196. goto err2;
  197. #ifdef CONFIG_SMP
  198. d->smp = kcalloc(d->nr_reg, sizeof(*d->smp), GFP_NOWAIT);
  199. if (!d->smp)
  200. goto err3;
  201. #endif
  202. k = 0;
  203. if (hw->mask_regs) {
  204. for (i = 0; i < hw->nr_mask_regs; i++) {
  205. smp = IS_SMP(hw->mask_regs[i]);
  206. k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
  207. k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
  208. #ifdef CONFIG_INTC_BALANCING
  209. k += save_reg(d, k, hw->mask_regs[i].dist_reg, 0);
  210. #endif
  211. }
  212. }
  213. if (hw->prio_regs) {
  214. d->prio = kcalloc(hw->nr_vectors, sizeof(*d->prio),
  215. GFP_NOWAIT);
  216. if (!d->prio)
  217. goto err4;
  218. for (i = 0; i < hw->nr_prio_regs; i++) {
  219. smp = IS_SMP(hw->prio_regs[i]);
  220. k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
  221. k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
  222. }
  223. sort(d->prio, hw->nr_prio_regs, sizeof(*d->prio),
  224. intc_handle_int_cmp, NULL);
  225. }
  226. if (hw->sense_regs) {
  227. d->sense = kcalloc(hw->nr_vectors, sizeof(*d->sense),
  228. GFP_NOWAIT);
  229. if (!d->sense)
  230. goto err5;
  231. for (i = 0; i < hw->nr_sense_regs; i++)
  232. k += save_reg(d, k, hw->sense_regs[i].reg, 0);
  233. sort(d->sense, hw->nr_sense_regs, sizeof(*d->sense),
  234. intc_handle_int_cmp, NULL);
  235. }
  236. if (hw->subgroups)
  237. for (i = 0; i < hw->nr_subgroups; i++)
  238. if (hw->subgroups[i].reg)
  239. k+= save_reg(d, k, hw->subgroups[i].reg, 0);
  240. memcpy(&d->chip, &intc_irq_chip, sizeof(struct irq_chip));
  241. d->chip.name = desc->name;
  242. if (hw->ack_regs)
  243. for (i = 0; i < hw->nr_ack_regs; i++)
  244. k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
  245. else
  246. d->chip.irq_mask_ack = d->chip.irq_disable;
  247. /* disable bits matching force_disable before registering irqs */
  248. if (desc->force_disable)
  249. intc_enable_disable_enum(desc, d, desc->force_disable, 0);
  250. /* disable bits matching force_enable before registering irqs */
  251. if (desc->force_enable)
  252. intc_enable_disable_enum(desc, d, desc->force_enable, 0);
  253. BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
  254. intc_irq_domain_init(d, hw);
  255. /* register the vectors one by one */
  256. for (i = 0; i < hw->nr_vectors; i++) {
  257. struct intc_vect *vect = hw->vectors + i;
  258. unsigned int irq = evt2irq(vect->vect);
  259. int res;
  260. if (!vect->enum_id)
  261. continue;
  262. res = irq_create_identity_mapping(d->domain, irq);
  263. if (unlikely(res)) {
  264. if (res == -EEXIST) {
  265. res = irq_domain_associate(d->domain, irq, irq);
  266. if (unlikely(res)) {
  267. pr_err("domain association failure\n");
  268. continue;
  269. }
  270. } else {
  271. pr_err("can't identity map IRQ %d\n", irq);
  272. continue;
  273. }
  274. }
  275. intc_irq_xlate_set(irq, vect->enum_id, d);
  276. intc_register_irq(desc, d, vect->enum_id, irq);
  277. for (k = i + 1; k < hw->nr_vectors; k++) {
  278. struct intc_vect *vect2 = hw->vectors + k;
  279. unsigned int irq2 = evt2irq(vect2->vect);
  280. if (vect->enum_id != vect2->enum_id)
  281. continue;
  282. /*
  283. * In the case of multi-evt handling and sparse
  284. * IRQ support, each vector still needs to have
  285. * its own backing irq_desc.
  286. */
  287. res = irq_create_identity_mapping(d->domain, irq2);
  288. if (unlikely(res)) {
  289. if (res == -EEXIST) {
  290. res = irq_domain_associate(d->domain,
  291. irq2, irq2);
  292. if (unlikely(res)) {
  293. pr_err("domain association "
  294. "failure\n");
  295. continue;
  296. }
  297. } else {
  298. pr_err("can't identity map IRQ %d\n",
  299. irq);
  300. continue;
  301. }
  302. }
  303. vect2->enum_id = 0;
  304. /* redirect this interrupts to the first one */
  305. irq_set_chip(irq2, &dummy_irq_chip);
  306. irq_set_chained_handler_and_data(irq2,
  307. intc_redirect_irq,
  308. (void *)irq);
  309. }
  310. }
  311. intc_subgroup_init(desc, d);
  312. /* enable bits matching force_enable after registering irqs */
  313. if (desc->force_enable)
  314. intc_enable_disable_enum(desc, d, desc->force_enable, 1);
  315. d->skip_suspend = desc->skip_syscore_suspend;
  316. nr_intc_controllers++;
  317. return 0;
  318. err5:
  319. kfree(d->prio);
  320. err4:
  321. #ifdef CONFIG_SMP
  322. kfree(d->smp);
  323. err3:
  324. #endif
  325. kfree(d->reg);
  326. err2:
  327. for (k = 0; k < d->nr_windows; k++)
  328. if (d->window[k].virt)
  329. iounmap(d->window[k].virt);
  330. kfree(d->window);
  331. err1:
  332. kfree(d);
  333. err0:
  334. pr_err("unable to allocate INTC memory\n");
  335. return -ENOMEM;
  336. }
  337. static int intc_suspend(void)
  338. {
  339. struct intc_desc_int *d;
  340. list_for_each_entry(d, &intc_list, list) {
  341. int irq;
  342. if (d->skip_suspend)
  343. continue;
  344. /* enable wakeup irqs belonging to this intc controller */
  345. for_each_active_irq(irq) {
  346. struct irq_data *data;
  347. struct irq_chip *chip;
  348. data = irq_get_irq_data(irq);
  349. chip = irq_data_get_irq_chip(data);
  350. if (chip != &d->chip)
  351. continue;
  352. if (irqd_is_wakeup_set(data))
  353. chip->irq_enable(data);
  354. }
  355. }
  356. return 0;
  357. }
  358. static void intc_resume(void)
  359. {
  360. struct intc_desc_int *d;
  361. list_for_each_entry(d, &intc_list, list) {
  362. int irq;
  363. if (d->skip_suspend)
  364. continue;
  365. for_each_active_irq(irq) {
  366. struct irq_data *data;
  367. struct irq_chip *chip;
  368. data = irq_get_irq_data(irq);
  369. chip = irq_data_get_irq_chip(data);
  370. /*
  371. * This will catch the redirect and VIRQ cases
  372. * due to the dummy_irq_chip being inserted.
  373. */
  374. if (chip != &d->chip)
  375. continue;
  376. if (irqd_irq_disabled(data))
  377. chip->irq_disable(data);
  378. else
  379. chip->irq_enable(data);
  380. }
  381. }
  382. }
  383. struct syscore_ops intc_syscore_ops = {
  384. .suspend = intc_suspend,
  385. .resume = intc_resume,
  386. };
  387. struct bus_type intc_subsys = {
  388. .name = "intc",
  389. .dev_name = "intc",
  390. };
  391. static ssize_t
  392. show_intc_name(struct device *dev, struct device_attribute *attr, char *buf)
  393. {
  394. struct intc_desc_int *d;
  395. d = container_of(dev, struct intc_desc_int, dev);
  396. return sprintf(buf, "%s\n", d->chip.name);
  397. }
  398. static DEVICE_ATTR(name, S_IRUGO, show_intc_name, NULL);
  399. static int __init register_intc_devs(void)
  400. {
  401. struct intc_desc_int *d;
  402. int error;
  403. register_syscore_ops(&intc_syscore_ops);
  404. error = subsys_system_register(&intc_subsys, NULL);
  405. if (!error) {
  406. list_for_each_entry(d, &intc_list, list) {
  407. d->dev.id = d->index;
  408. d->dev.bus = &intc_subsys;
  409. error = device_register(&d->dev);
  410. if (error == 0)
  411. error = device_create_file(&d->dev,
  412. &dev_attr_name);
  413. if (error)
  414. break;
  415. }
  416. }
  417. if (error)
  418. pr_err("device registration error\n");
  419. return error;
  420. }
  421. device_initcall(register_intc_devs);