irq_64.c 25 KB

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