irq_64.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /* irq.c: UltraSparc IRQ handling/init/registry.
  2. *
  3. * Copyright (C) 1997, 2007, 2008 David S. Miller (davem@davemloft.net)
  4. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  5. * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
  6. */
  7. #include <linux/module.h>
  8. #include <linux/sched.h>
  9. #include <linux/linkage.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/errno.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/signal.h>
  14. #include <linux/mm.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/slab.h>
  17. #include <linux/random.h>
  18. #include <linux/init.h>
  19. #include <linux/delay.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/ftrace.h>
  23. #include <linux/irq.h>
  24. #include <linux/kmemleak.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/processor.h>
  27. #include <asm/atomic.h>
  28. #include <asm/system.h>
  29. #include <asm/irq.h>
  30. #include <asm/io.h>
  31. #include <asm/iommu.h>
  32. #include <asm/upa.h>
  33. #include <asm/oplib.h>
  34. #include <asm/prom.h>
  35. #include <asm/timer.h>
  36. #include <asm/smp.h>
  37. #include <asm/starfire.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/cache.h>
  40. #include <asm/cpudata.h>
  41. #include <asm/auxio.h>
  42. #include <asm/head.h>
  43. #include <asm/hypervisor.h>
  44. #include <asm/cacheflush.h>
  45. #include "entry.h"
  46. #include "cpumap.h"
  47. #include "kstack.h"
  48. #define NUM_IVECS (IMAP_INR + 1)
  49. struct ino_bucket *ivector_table;
  50. unsigned long ivector_table_pa;
  51. /* On several sun4u processors, it is illegal to mix bypass and
  52. * non-bypass accesses. Therefore we access all INO buckets
  53. * using bypass accesses only.
  54. */
  55. static unsigned long bucket_get_chain_pa(unsigned long bucket_pa)
  56. {
  57. unsigned long ret;
  58. __asm__ __volatile__("ldxa [%1] %2, %0"
  59. : "=&r" (ret)
  60. : "r" (bucket_pa +
  61. offsetof(struct ino_bucket,
  62. __irq_chain_pa)),
  63. "i" (ASI_PHYS_USE_EC));
  64. return ret;
  65. }
  66. static void bucket_clear_chain_pa(unsigned long bucket_pa)
  67. {
  68. __asm__ __volatile__("stxa %%g0, [%0] %1"
  69. : /* no outputs */
  70. : "r" (bucket_pa +
  71. offsetof(struct ino_bucket,
  72. __irq_chain_pa)),
  73. "i" (ASI_PHYS_USE_EC));
  74. }
  75. static unsigned int bucket_get_irq(unsigned long bucket_pa)
  76. {
  77. unsigned int ret;
  78. __asm__ __volatile__("lduwa [%1] %2, %0"
  79. : "=&r" (ret)
  80. : "r" (bucket_pa +
  81. offsetof(struct ino_bucket,
  82. __irq)),
  83. "i" (ASI_PHYS_USE_EC));
  84. return ret;
  85. }
  86. static void bucket_set_irq(unsigned long bucket_pa, unsigned int irq)
  87. {
  88. __asm__ __volatile__("stwa %0, [%1] %2"
  89. : /* no outputs */
  90. : "r" (irq),
  91. "r" (bucket_pa +
  92. offsetof(struct ino_bucket,
  93. __irq)),
  94. "i" (ASI_PHYS_USE_EC));
  95. }
  96. #define irq_work_pa(__cpu) &(trap_block[(__cpu)].irq_worklist_pa)
  97. static struct {
  98. unsigned int dev_handle;
  99. unsigned int dev_ino;
  100. unsigned int in_use;
  101. } irq_table[NR_IRQS];
  102. static DEFINE_SPINLOCK(irq_alloc_lock);
  103. unsigned char irq_alloc(unsigned int dev_handle, unsigned int dev_ino)
  104. {
  105. unsigned long flags;
  106. unsigned char ent;
  107. BUILD_BUG_ON(NR_IRQS >= 256);
  108. spin_lock_irqsave(&irq_alloc_lock, flags);
  109. for (ent = 1; ent < NR_IRQS; ent++) {
  110. if (!irq_table[ent].in_use)
  111. break;
  112. }
  113. if (ent >= NR_IRQS) {
  114. printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
  115. ent = 0;
  116. } else {
  117. irq_table[ent].dev_handle = dev_handle;
  118. irq_table[ent].dev_ino = dev_ino;
  119. irq_table[ent].in_use = 1;
  120. }
  121. spin_unlock_irqrestore(&irq_alloc_lock, flags);
  122. return ent;
  123. }
  124. #ifdef CONFIG_PCI_MSI
  125. void irq_free(unsigned int irq)
  126. {
  127. unsigned long flags;
  128. if (irq >= NR_IRQS)
  129. return;
  130. spin_lock_irqsave(&irq_alloc_lock, flags);
  131. irq_table[irq].in_use = 0;
  132. spin_unlock_irqrestore(&irq_alloc_lock, flags);
  133. }
  134. #endif
  135. /*
  136. * /proc/interrupts printing:
  137. */
  138. int arch_show_interrupts(struct seq_file *p, int prec)
  139. {
  140. int j;
  141. seq_printf(p, "NMI: ");
  142. for_each_online_cpu(j)
  143. seq_printf(p, "%10u ", cpu_data(j).__nmi_count);
  144. seq_printf(p, " Non-maskable interrupts\n");
  145. return 0;
  146. }
  147. static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
  148. {
  149. unsigned int tid;
  150. if (this_is_starfire) {
  151. tid = starfire_translate(imap, cpuid);
  152. tid <<= IMAP_TID_SHIFT;
  153. tid &= IMAP_TID_UPA;
  154. } else {
  155. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  156. unsigned long ver;
  157. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  158. if ((ver >> 32UL) == __JALAPENO_ID ||
  159. (ver >> 32UL) == __SERRANO_ID) {
  160. tid = cpuid << IMAP_TID_SHIFT;
  161. tid &= IMAP_TID_JBUS;
  162. } else {
  163. unsigned int a = cpuid & 0x1f;
  164. unsigned int n = (cpuid >> 5) & 0x1f;
  165. tid = ((a << IMAP_AID_SHIFT) |
  166. (n << IMAP_NID_SHIFT));
  167. tid &= (IMAP_AID_SAFARI |
  168. IMAP_NID_SAFARI);
  169. }
  170. } else {
  171. tid = cpuid << IMAP_TID_SHIFT;
  172. tid &= IMAP_TID_UPA;
  173. }
  174. }
  175. return tid;
  176. }
  177. struct irq_handler_data {
  178. unsigned long iclr;
  179. unsigned long imap;
  180. void (*pre_handler)(unsigned int, void *, void *);
  181. void *arg1;
  182. void *arg2;
  183. };
  184. #ifdef CONFIG_SMP
  185. static int irq_choose_cpu(unsigned int irq, const struct cpumask *affinity)
  186. {
  187. cpumask_t mask;
  188. int cpuid;
  189. cpumask_copy(&mask, affinity);
  190. if (cpumask_equal(&mask, cpu_online_mask)) {
  191. cpuid = map_to_cpu(irq);
  192. } else {
  193. cpumask_t tmp;
  194. cpumask_and(&tmp, cpu_online_mask, &mask);
  195. cpuid = cpumask_empty(&tmp) ? map_to_cpu(irq) : cpumask_first(&tmp);
  196. }
  197. return cpuid;
  198. }
  199. #else
  200. #define irq_choose_cpu(irq, affinity) \
  201. real_hard_smp_processor_id()
  202. #endif
  203. static void sun4u_irq_enable(struct irq_data *data)
  204. {
  205. struct irq_handler_data *handler_data = data->handler_data;
  206. if (likely(handler_data)) {
  207. unsigned long cpuid, imap, val;
  208. unsigned int tid;
  209. cpuid = irq_choose_cpu(data->irq, data->affinity);
  210. imap = handler_data->imap;
  211. tid = sun4u_compute_tid(imap, cpuid);
  212. val = upa_readq(imap);
  213. val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
  214. IMAP_AID_SAFARI | IMAP_NID_SAFARI);
  215. val |= tid | IMAP_VALID;
  216. upa_writeq(val, imap);
  217. upa_writeq(ICLR_IDLE, handler_data->iclr);
  218. }
  219. }
  220. static int sun4u_set_affinity(struct irq_data *data,
  221. const struct cpumask *mask, bool force)
  222. {
  223. struct irq_handler_data *handler_data = data->handler_data;
  224. if (likely(handler_data)) {
  225. unsigned long cpuid, imap, val;
  226. unsigned int tid;
  227. cpuid = irq_choose_cpu(data->irq, mask);
  228. imap = handler_data->imap;
  229. tid = sun4u_compute_tid(imap, cpuid);
  230. val = upa_readq(imap);
  231. val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
  232. IMAP_AID_SAFARI | IMAP_NID_SAFARI);
  233. val |= tid | IMAP_VALID;
  234. upa_writeq(val, imap);
  235. upa_writeq(ICLR_IDLE, handler_data->iclr);
  236. }
  237. return 0;
  238. }
  239. /* Don't do anything. The desc->status check for IRQ_DISABLED in
  240. * handler_irq() will skip the handler call and that will leave the
  241. * interrupt in the sent state. The next ->enable() call will hit the
  242. * ICLR register to reset the state machine.
  243. *
  244. * This scheme is necessary, instead of clearing the Valid bit in the
  245. * IMAP register, to handle the case of IMAP registers being shared by
  246. * multiple INOs (and thus ICLR registers). Since we use a different
  247. * virtual IRQ for each shared IMAP instance, the generic code thinks
  248. * there is only one user so it prematurely calls ->disable() on
  249. * free_irq().
  250. *
  251. * We have to provide an explicit ->disable() method instead of using
  252. * NULL to get the default. The reason is that if the generic code
  253. * sees that, it also hooks up a default ->shutdown method which
  254. * invokes ->mask() which we do not want. See irq_chip_set_defaults().
  255. */
  256. static void sun4u_irq_disable(struct irq_data *data)
  257. {
  258. }
  259. static void sun4u_irq_eoi(struct irq_data *data)
  260. {
  261. struct irq_handler_data *handler_data = data->handler_data;
  262. if (likely(handler_data))
  263. upa_writeq(ICLR_IDLE, handler_data->iclr);
  264. }
  265. static void sun4v_irq_enable(struct irq_data *data)
  266. {
  267. unsigned int ino = irq_table[data->irq].dev_ino;
  268. unsigned long cpuid = irq_choose_cpu(data->irq, data->affinity);
  269. int err;
  270. err = sun4v_intr_settarget(ino, cpuid);
  271. if (err != HV_EOK)
  272. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  273. "err(%d)\n", ino, cpuid, err);
  274. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  275. if (err != HV_EOK)
  276. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  277. "err(%d)\n", ino, err);
  278. err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
  279. if (err != HV_EOK)
  280. printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
  281. ino, err);
  282. }
  283. static int sun4v_set_affinity(struct irq_data *data,
  284. const struct cpumask *mask, bool force)
  285. {
  286. unsigned int ino = irq_table[data->irq].dev_ino;
  287. unsigned long cpuid = irq_choose_cpu(data->irq, mask);
  288. int err;
  289. err = sun4v_intr_settarget(ino, cpuid);
  290. if (err != HV_EOK)
  291. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  292. "err(%d)\n", ino, cpuid, err);
  293. return 0;
  294. }
  295. static void sun4v_irq_disable(struct irq_data *data)
  296. {
  297. unsigned int ino = irq_table[data->irq].dev_ino;
  298. int err;
  299. err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
  300. if (err != HV_EOK)
  301. printk(KERN_ERR "sun4v_intr_setenabled(%x): "
  302. "err(%d)\n", ino, err);
  303. }
  304. static void sun4v_irq_eoi(struct irq_data *data)
  305. {
  306. unsigned int ino = irq_table[data->irq].dev_ino;
  307. int err;
  308. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  309. if (err != HV_EOK)
  310. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  311. "err(%d)\n", ino, err);
  312. }
  313. static void sun4v_virq_enable(struct irq_data *data)
  314. {
  315. unsigned long cpuid, dev_handle, dev_ino;
  316. int err;
  317. cpuid = irq_choose_cpu(data->irq, data->affinity);
  318. dev_handle = irq_table[data->irq].dev_handle;
  319. dev_ino = irq_table[data->irq].dev_ino;
  320. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  321. if (err != HV_EOK)
  322. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  323. "err(%d)\n",
  324. dev_handle, dev_ino, cpuid, err);
  325. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  326. HV_INTR_STATE_IDLE);
  327. if (err != HV_EOK)
  328. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  329. "HV_INTR_STATE_IDLE): err(%d)\n",
  330. dev_handle, dev_ino, err);
  331. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  332. HV_INTR_ENABLED);
  333. if (err != HV_EOK)
  334. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  335. "HV_INTR_ENABLED): err(%d)\n",
  336. dev_handle, dev_ino, err);
  337. }
  338. static int sun4v_virt_set_affinity(struct irq_data *data,
  339. const struct cpumask *mask, bool force)
  340. {
  341. unsigned long cpuid, dev_handle, dev_ino;
  342. int err;
  343. cpuid = irq_choose_cpu(data->irq, mask);
  344. dev_handle = irq_table[data->irq].dev_handle;
  345. dev_ino = irq_table[data->irq].dev_ino;
  346. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  347. if (err != HV_EOK)
  348. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  349. "err(%d)\n",
  350. dev_handle, dev_ino, cpuid, err);
  351. return 0;
  352. }
  353. static void sun4v_virq_disable(struct irq_data *data)
  354. {
  355. unsigned long dev_handle, dev_ino;
  356. int err;
  357. dev_handle = irq_table[data->irq].dev_handle;
  358. dev_ino = irq_table[data->irq].dev_ino;
  359. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  360. HV_INTR_DISABLED);
  361. if (err != HV_EOK)
  362. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  363. "HV_INTR_DISABLED): err(%d)\n",
  364. dev_handle, dev_ino, err);
  365. }
  366. static void sun4v_virq_eoi(struct irq_data *data)
  367. {
  368. unsigned long dev_handle, dev_ino;
  369. int err;
  370. dev_handle = irq_table[data->irq].dev_handle;
  371. dev_ino = irq_table[data->irq].dev_ino;
  372. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  373. HV_INTR_STATE_IDLE);
  374. if (err != HV_EOK)
  375. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  376. "HV_INTR_STATE_IDLE): err(%d)\n",
  377. dev_handle, dev_ino, err);
  378. }
  379. static struct irq_chip sun4u_irq = {
  380. .name = "sun4u",
  381. .irq_enable = sun4u_irq_enable,
  382. .irq_disable = sun4u_irq_disable,
  383. .irq_eoi = sun4u_irq_eoi,
  384. .irq_set_affinity = sun4u_set_affinity,
  385. .flags = IRQCHIP_EOI_IF_HANDLED,
  386. };
  387. static struct irq_chip sun4v_irq = {
  388. .name = "sun4v",
  389. .irq_enable = sun4v_irq_enable,
  390. .irq_disable = sun4v_irq_disable,
  391. .irq_eoi = sun4v_irq_eoi,
  392. .irq_set_affinity = sun4v_set_affinity,
  393. .flags = IRQCHIP_EOI_IF_HANDLED,
  394. };
  395. static struct irq_chip sun4v_virq = {
  396. .name = "vsun4v",
  397. .irq_enable = sun4v_virq_enable,
  398. .irq_disable = sun4v_virq_disable,
  399. .irq_eoi = sun4v_virq_eoi,
  400. .irq_set_affinity = sun4v_virt_set_affinity,
  401. .flags = IRQCHIP_EOI_IF_HANDLED,
  402. };
  403. static void pre_flow_handler(struct irq_data *d)
  404. {
  405. struct irq_handler_data *handler_data = irq_data_get_irq_handler_data(d);
  406. unsigned int ino = irq_table[d->irq].dev_ino;
  407. handler_data->pre_handler(ino, handler_data->arg1, handler_data->arg2);
  408. }
  409. void irq_install_pre_handler(int irq,
  410. void (*func)(unsigned int, void *, void *),
  411. void *arg1, void *arg2)
  412. {
  413. struct irq_handler_data *handler_data = irq_get_handler_data(irq);
  414. handler_data->pre_handler = func;
  415. handler_data->arg1 = arg1;
  416. handler_data->arg2 = arg2;
  417. __irq_set_preflow_handler(irq, pre_flow_handler);
  418. }
  419. unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
  420. {
  421. struct ino_bucket *bucket;
  422. struct irq_handler_data *handler_data;
  423. unsigned int irq;
  424. int ino;
  425. BUG_ON(tlb_type == hypervisor);
  426. ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
  427. bucket = &ivector_table[ino];
  428. irq = bucket_get_irq(__pa(bucket));
  429. if (!irq) {
  430. irq = irq_alloc(0, ino);
  431. bucket_set_irq(__pa(bucket), irq);
  432. irq_set_chip_and_handler_name(irq, &sun4u_irq,
  433. handle_fasteoi_irq, "IVEC");
  434. }
  435. handler_data = irq_get_handler_data(irq);
  436. if (unlikely(handler_data))
  437. goto out;
  438. handler_data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  439. if (unlikely(!handler_data)) {
  440. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  441. prom_halt();
  442. }
  443. irq_set_handler_data(irq, handler_data);
  444. handler_data->imap = imap;
  445. handler_data->iclr = iclr;
  446. out:
  447. return irq;
  448. }
  449. static unsigned int sun4v_build_common(unsigned long sysino,
  450. struct irq_chip *chip)
  451. {
  452. struct ino_bucket *bucket;
  453. struct irq_handler_data *handler_data;
  454. unsigned int irq;
  455. BUG_ON(tlb_type != hypervisor);
  456. bucket = &ivector_table[sysino];
  457. irq = bucket_get_irq(__pa(bucket));
  458. if (!irq) {
  459. irq = irq_alloc(0, sysino);
  460. bucket_set_irq(__pa(bucket), irq);
  461. irq_set_chip_and_handler_name(irq, chip, handle_fasteoi_irq,
  462. "IVEC");
  463. }
  464. handler_data = irq_get_handler_data(irq);
  465. if (unlikely(handler_data))
  466. goto out;
  467. handler_data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  468. if (unlikely(!handler_data)) {
  469. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  470. prom_halt();
  471. }
  472. irq_set_handler_data(irq, handler_data);
  473. /* Catch accidental accesses to these things. IMAP/ICLR handling
  474. * is done by hypervisor calls on sun4v platforms, not by direct
  475. * register accesses.
  476. */
  477. handler_data->imap = ~0UL;
  478. handler_data->iclr = ~0UL;
  479. out:
  480. return irq;
  481. }
  482. unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
  483. {
  484. unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
  485. return sun4v_build_common(sysino, &sun4v_irq);
  486. }
  487. unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
  488. {
  489. struct irq_handler_data *handler_data;
  490. unsigned long hv_err, cookie;
  491. struct ino_bucket *bucket;
  492. unsigned int irq;
  493. bucket = kzalloc(sizeof(struct ino_bucket), GFP_ATOMIC);
  494. if (unlikely(!bucket))
  495. return 0;
  496. /* The only reference we store to the IRQ bucket is
  497. * by physical address which kmemleak can't see, tell
  498. * it that this object explicitly is not a leak and
  499. * should be scanned.
  500. */
  501. kmemleak_not_leak(bucket);
  502. __flush_dcache_range((unsigned long) bucket,
  503. ((unsigned long) bucket +
  504. sizeof(struct ino_bucket)));
  505. irq = irq_alloc(devhandle, devino);
  506. bucket_set_irq(__pa(bucket), irq);
  507. irq_set_chip_and_handler_name(irq, &sun4v_virq, handle_fasteoi_irq,
  508. "IVEC");
  509. handler_data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  510. if (unlikely(!handler_data))
  511. return 0;
  512. /* In order to make the LDC channel startup sequence easier,
  513. * especially wrt. locking, we do not let request_irq() enable
  514. * the interrupt.
  515. */
  516. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  517. irq_set_handler_data(irq, handler_data);
  518. /* Catch accidental accesses to these things. IMAP/ICLR handling
  519. * is done by hypervisor calls on sun4v platforms, not by direct
  520. * register accesses.
  521. */
  522. handler_data->imap = ~0UL;
  523. handler_data->iclr = ~0UL;
  524. cookie = ~__pa(bucket);
  525. hv_err = sun4v_vintr_set_cookie(devhandle, devino, cookie);
  526. if (hv_err) {
  527. prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
  528. "err=%lu\n", devhandle, devino, hv_err);
  529. prom_halt();
  530. }
  531. return irq;
  532. }
  533. void ack_bad_irq(unsigned int irq)
  534. {
  535. unsigned int ino = irq_table[irq].dev_ino;
  536. if (!ino)
  537. ino = 0xdeadbeef;
  538. printk(KERN_CRIT "Unexpected IRQ from ino[%x] irq[%u]\n",
  539. ino, irq);
  540. }
  541. void *hardirq_stack[NR_CPUS];
  542. void *softirq_stack[NR_CPUS];
  543. void __irq_entry handler_irq(int pil, struct pt_regs *regs)
  544. {
  545. unsigned long pstate, bucket_pa;
  546. struct pt_regs *old_regs;
  547. void *orig_sp;
  548. clear_softint(1 << pil);
  549. old_regs = set_irq_regs(regs);
  550. irq_enter();
  551. /* Grab an atomic snapshot of the pending IVECs. */
  552. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  553. "wrpr %0, %3, %%pstate\n\t"
  554. "ldx [%2], %1\n\t"
  555. "stx %%g0, [%2]\n\t"
  556. "wrpr %0, 0x0, %%pstate\n\t"
  557. : "=&r" (pstate), "=&r" (bucket_pa)
  558. : "r" (irq_work_pa(smp_processor_id())),
  559. "i" (PSTATE_IE)
  560. : "memory");
  561. orig_sp = set_hardirq_stack();
  562. while (bucket_pa) {
  563. unsigned long next_pa;
  564. unsigned int irq;
  565. next_pa = bucket_get_chain_pa(bucket_pa);
  566. irq = bucket_get_irq(bucket_pa);
  567. bucket_clear_chain_pa(bucket_pa);
  568. generic_handle_irq(irq);
  569. bucket_pa = next_pa;
  570. }
  571. restore_hardirq_stack(orig_sp);
  572. irq_exit();
  573. set_irq_regs(old_regs);
  574. }
  575. void do_softirq(void)
  576. {
  577. unsigned long flags;
  578. if (in_interrupt())
  579. return;
  580. local_irq_save(flags);
  581. if (local_softirq_pending()) {
  582. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  583. sp += THREAD_SIZE - 192 - STACK_BIAS;
  584. __asm__ __volatile__("mov %%sp, %0\n\t"
  585. "mov %1, %%sp"
  586. : "=&r" (orig_sp)
  587. : "r" (sp));
  588. __do_softirq();
  589. __asm__ __volatile__("mov %0, %%sp"
  590. : : "r" (orig_sp));
  591. }
  592. local_irq_restore(flags);
  593. }
  594. #ifdef CONFIG_HOTPLUG_CPU
  595. void fixup_irqs(void)
  596. {
  597. unsigned int irq;
  598. for (irq = 0; irq < NR_IRQS; irq++) {
  599. struct irq_desc *desc = irq_to_desc(irq);
  600. struct irq_data *data = irq_desc_get_irq_data(desc);
  601. unsigned long flags;
  602. raw_spin_lock_irqsave(&desc->lock, flags);
  603. if (desc->action && !irqd_is_per_cpu(data)) {
  604. if (data->chip->irq_set_affinity)
  605. data->chip->irq_set_affinity(data,
  606. data->affinity,
  607. false);
  608. }
  609. raw_spin_unlock_irqrestore(&desc->lock, flags);
  610. }
  611. tick_ops->disable_irq();
  612. }
  613. #endif
  614. struct sun5_timer {
  615. u64 count0;
  616. u64 limit0;
  617. u64 count1;
  618. u64 limit1;
  619. };
  620. static struct sun5_timer *prom_timers;
  621. static u64 prom_limit0, prom_limit1;
  622. static void map_prom_timers(void)
  623. {
  624. struct device_node *dp;
  625. const unsigned int *addr;
  626. /* PROM timer node hangs out in the top level of device siblings... */
  627. dp = of_find_node_by_path("/");
  628. dp = dp->child;
  629. while (dp) {
  630. if (!strcmp(dp->name, "counter-timer"))
  631. break;
  632. dp = dp->sibling;
  633. }
  634. /* Assume if node is not present, PROM uses different tick mechanism
  635. * which we should not care about.
  636. */
  637. if (!dp) {
  638. prom_timers = (struct sun5_timer *) 0;
  639. return;
  640. }
  641. /* If PROM is really using this, it must be mapped by him. */
  642. addr = of_get_property(dp, "address", NULL);
  643. if (!addr) {
  644. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  645. prom_timers = (struct sun5_timer *) 0;
  646. return;
  647. }
  648. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  649. }
  650. static void kill_prom_timer(void)
  651. {
  652. if (!prom_timers)
  653. return;
  654. /* Save them away for later. */
  655. prom_limit0 = prom_timers->limit0;
  656. prom_limit1 = prom_timers->limit1;
  657. /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
  658. * We turn both off here just to be paranoid.
  659. */
  660. prom_timers->limit0 = 0;
  661. prom_timers->limit1 = 0;
  662. /* Wheee, eat the interrupt packet too... */
  663. __asm__ __volatile__(
  664. " mov 0x40, %%g2\n"
  665. " ldxa [%%g0] %0, %%g1\n"
  666. " ldxa [%%g2] %1, %%g1\n"
  667. " stxa %%g0, [%%g0] %0\n"
  668. " membar #Sync\n"
  669. : /* no outputs */
  670. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  671. : "g1", "g2");
  672. }
  673. void notrace init_irqwork_curcpu(void)
  674. {
  675. int cpu = hard_smp_processor_id();
  676. trap_block[cpu].irq_worklist_pa = 0UL;
  677. }
  678. /* Please be very careful with register_one_mondo() and
  679. * sun4v_register_mondo_queues().
  680. *
  681. * On SMP this gets invoked from the CPU trampoline before
  682. * the cpu has fully taken over the trap table from OBP,
  683. * and it's kernel stack + %g6 thread register state is
  684. * not fully cooked yet.
  685. *
  686. * Therefore you cannot make any OBP calls, not even prom_printf,
  687. * from these two routines.
  688. */
  689. static void __cpuinit notrace register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
  690. {
  691. unsigned long num_entries = (qmask + 1) / 64;
  692. unsigned long status;
  693. status = sun4v_cpu_qconf(type, paddr, num_entries);
  694. if (status != HV_EOK) {
  695. prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
  696. "err %lu\n", type, paddr, num_entries, status);
  697. prom_halt();
  698. }
  699. }
  700. void __cpuinit notrace sun4v_register_mondo_queues(int this_cpu)
  701. {
  702. struct trap_per_cpu *tb = &trap_block[this_cpu];
  703. register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
  704. tb->cpu_mondo_qmask);
  705. register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
  706. tb->dev_mondo_qmask);
  707. register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
  708. tb->resum_qmask);
  709. register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
  710. tb->nonresum_qmask);
  711. }
  712. /* Each queue region must be a power of 2 multiple of 64 bytes in
  713. * size. The base real address must be aligned to the size of the
  714. * region. Thus, an 8KB queue must be 8KB aligned, for example.
  715. */
  716. static void __init alloc_one_queue(unsigned long *pa_ptr, unsigned long qmask)
  717. {
  718. unsigned long size = PAGE_ALIGN(qmask + 1);
  719. unsigned long order = get_order(size);
  720. unsigned long p;
  721. p = __get_free_pages(GFP_KERNEL, order);
  722. if (!p) {
  723. prom_printf("SUN4V: Error, cannot allocate queue.\n");
  724. prom_halt();
  725. }
  726. *pa_ptr = __pa(p);
  727. }
  728. static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
  729. {
  730. #ifdef CONFIG_SMP
  731. unsigned long page;
  732. BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
  733. page = get_zeroed_page(GFP_KERNEL);
  734. if (!page) {
  735. prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
  736. prom_halt();
  737. }
  738. tb->cpu_mondo_block_pa = __pa(page);
  739. tb->cpu_list_pa = __pa(page + 64);
  740. #endif
  741. }
  742. /* Allocate mondo and error queues for all possible cpus. */
  743. static void __init sun4v_init_mondo_queues(void)
  744. {
  745. int cpu;
  746. for_each_possible_cpu(cpu) {
  747. struct trap_per_cpu *tb = &trap_block[cpu];
  748. alloc_one_queue(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
  749. alloc_one_queue(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
  750. alloc_one_queue(&tb->resum_mondo_pa, tb->resum_qmask);
  751. alloc_one_queue(&tb->resum_kernel_buf_pa, tb->resum_qmask);
  752. alloc_one_queue(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
  753. alloc_one_queue(&tb->nonresum_kernel_buf_pa,
  754. tb->nonresum_qmask);
  755. }
  756. }
  757. static void __init init_send_mondo_info(void)
  758. {
  759. int cpu;
  760. for_each_possible_cpu(cpu) {
  761. struct trap_per_cpu *tb = &trap_block[cpu];
  762. init_cpu_send_mondo_info(tb);
  763. }
  764. }
  765. static struct irqaction timer_irq_action = {
  766. .name = "timer",
  767. };
  768. /* Only invoked on boot processor. */
  769. void __init init_IRQ(void)
  770. {
  771. unsigned long size;
  772. map_prom_timers();
  773. kill_prom_timer();
  774. size = sizeof(struct ino_bucket) * NUM_IVECS;
  775. ivector_table = kzalloc(size, GFP_KERNEL);
  776. if (!ivector_table) {
  777. prom_printf("Fatal error, cannot allocate ivector_table\n");
  778. prom_halt();
  779. }
  780. __flush_dcache_range((unsigned long) ivector_table,
  781. ((unsigned long) ivector_table) + size);
  782. ivector_table_pa = __pa(ivector_table);
  783. if (tlb_type == hypervisor)
  784. sun4v_init_mondo_queues();
  785. init_send_mondo_info();
  786. if (tlb_type == hypervisor) {
  787. /* Load up the boot cpu's entries. */
  788. sun4v_register_mondo_queues(hard_smp_processor_id());
  789. }
  790. /* We need to clear any IRQ's pending in the soft interrupt
  791. * registers, a spurious one could be left around from the
  792. * PROM timer which we just disabled.
  793. */
  794. clear_softint(get_softint());
  795. /* Now that ivector table is initialized, it is safe
  796. * to receive IRQ vector traps. We will normally take
  797. * one or two right now, in case some device PROM used
  798. * to boot us wants to speak to us. We just ignore them.
  799. */
  800. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  801. "or %%g1, %0, %%g1\n\t"
  802. "wrpr %%g1, 0x0, %%pstate"
  803. : /* No outputs */
  804. : "i" (PSTATE_IE)
  805. : "g1");
  806. irq_to_desc(0)->action = &timer_irq_action;
  807. }