irq_64.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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/sched.h>
  8. #include <linux/linkage.h>
  9. #include <linux/ptrace.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/signal.h>
  13. #include <linux/mm.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/slab.h>
  16. #include <linux/random.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/ftrace.h>
  22. #include <linux/irq.h>
  23. #include <linux/kmemleak.h>
  24. #include <asm/ptrace.h>
  25. #include <asm/processor.h>
  26. #include <linux/atomic.h>
  27. #include <asm/irq.h>
  28. #include <asm/io.h>
  29. #include <asm/iommu.h>
  30. #include <asm/upa.h>
  31. #include <asm/oplib.h>
  32. #include <asm/prom.h>
  33. #include <asm/timer.h>
  34. #include <asm/smp.h>
  35. #include <asm/starfire.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/cache.h>
  38. #include <asm/cpudata.h>
  39. #include <asm/auxio.h>
  40. #include <asm/head.h>
  41. #include <asm/hypervisor.h>
  42. #include <asm/cacheflush.h>
  43. #include "entry.h"
  44. #include "cpumap.h"
  45. #include "kstack.h"
  46. struct ino_bucket *ivector_table;
  47. unsigned long ivector_table_pa;
  48. /* On several sun4u processors, it is illegal to mix bypass and
  49. * non-bypass accesses. Therefore we access all INO buckets
  50. * using bypass accesses only.
  51. */
  52. static unsigned long bucket_get_chain_pa(unsigned long bucket_pa)
  53. {
  54. unsigned long ret;
  55. __asm__ __volatile__("ldxa [%1] %2, %0"
  56. : "=&r" (ret)
  57. : "r" (bucket_pa +
  58. offsetof(struct ino_bucket,
  59. __irq_chain_pa)),
  60. "i" (ASI_PHYS_USE_EC));
  61. return ret;
  62. }
  63. static void bucket_clear_chain_pa(unsigned long bucket_pa)
  64. {
  65. __asm__ __volatile__("stxa %%g0, [%0] %1"
  66. : /* no outputs */
  67. : "r" (bucket_pa +
  68. offsetof(struct ino_bucket,
  69. __irq_chain_pa)),
  70. "i" (ASI_PHYS_USE_EC));
  71. }
  72. static unsigned int bucket_get_irq(unsigned long bucket_pa)
  73. {
  74. unsigned int ret;
  75. __asm__ __volatile__("lduwa [%1] %2, %0"
  76. : "=&r" (ret)
  77. : "r" (bucket_pa +
  78. offsetof(struct ino_bucket,
  79. __irq)),
  80. "i" (ASI_PHYS_USE_EC));
  81. return ret;
  82. }
  83. static void bucket_set_irq(unsigned long bucket_pa, unsigned int irq)
  84. {
  85. __asm__ __volatile__("stwa %0, [%1] %2"
  86. : /* no outputs */
  87. : "r" (irq),
  88. "r" (bucket_pa +
  89. offsetof(struct ino_bucket,
  90. __irq)),
  91. "i" (ASI_PHYS_USE_EC));
  92. }
  93. #define irq_work_pa(__cpu) &(trap_block[(__cpu)].irq_worklist_pa)
  94. static unsigned long hvirq_major __initdata;
  95. static int __init early_hvirq_major(char *p)
  96. {
  97. int rc = kstrtoul(p, 10, &hvirq_major);
  98. return rc;
  99. }
  100. early_param("hvirq", early_hvirq_major);
  101. static int hv_irq_version;
  102. /* Major version 2.0 of HV_GRP_INTR added support for the VIRQ cookie
  103. * based interfaces, but:
  104. *
  105. * 1) Several OSs, Solaris and Linux included, use them even when only
  106. * negotiating version 1.0 (or failing to negotiate at all). So the
  107. * hypervisor has a workaround that provides the VIRQ interfaces even
  108. * when only verion 1.0 of the API is in use.
  109. *
  110. * 2) Second, and more importantly, with major version 2.0 these VIRQ
  111. * interfaces only were actually hooked up for LDC interrupts, even
  112. * though the Hypervisor specification clearly stated:
  113. *
  114. * The new interrupt API functions will be available to a guest
  115. * when it negotiates version 2.0 in the interrupt API group 0x2. When
  116. * a guest negotiates version 2.0, all interrupt sources will only
  117. * support using the cookie interface, and any attempt to use the
  118. * version 1.0 interrupt APIs numbered 0xa0 to 0xa6 will result in the
  119. * ENOTSUPPORTED error being returned.
  120. *
  121. * with an emphasis on "all interrupt sources".
  122. *
  123. * To correct this, major version 3.0 was created which does actually
  124. * support VIRQs for all interrupt sources (not just LDC devices). So
  125. * if we want to move completely over the cookie based VIRQs we must
  126. * negotiate major version 3.0 or later of HV_GRP_INTR.
  127. */
  128. static bool sun4v_cookie_only_virqs(void)
  129. {
  130. if (hv_irq_version >= 3)
  131. return true;
  132. return false;
  133. }
  134. static void __init irq_init_hv(void)
  135. {
  136. unsigned long hv_error, major, minor = 0;
  137. if (tlb_type != hypervisor)
  138. return;
  139. if (hvirq_major)
  140. major = hvirq_major;
  141. else
  142. major = 3;
  143. hv_error = sun4v_hvapi_register(HV_GRP_INTR, major, &minor);
  144. if (!hv_error)
  145. hv_irq_version = major;
  146. else
  147. hv_irq_version = 1;
  148. pr_info("SUN4V: Using IRQ API major %d, cookie only virqs %s\n",
  149. hv_irq_version,
  150. sun4v_cookie_only_virqs() ? "enabled" : "disabled");
  151. }
  152. /* This function is for the timer interrupt.*/
  153. int __init arch_probe_nr_irqs(void)
  154. {
  155. return 1;
  156. }
  157. #define DEFAULT_NUM_IVECS (0xfffU)
  158. static unsigned int nr_ivec = DEFAULT_NUM_IVECS;
  159. #define NUM_IVECS (nr_ivec)
  160. static unsigned int __init size_nr_ivec(void)
  161. {
  162. if (tlb_type == hypervisor) {
  163. switch (sun4v_chip_type) {
  164. /* Athena's devhandle|devino is large.*/
  165. case SUN4V_CHIP_SPARC64X:
  166. nr_ivec = 0xffff;
  167. break;
  168. }
  169. }
  170. return nr_ivec;
  171. }
  172. struct irq_handler_data {
  173. union {
  174. struct {
  175. unsigned int dev_handle;
  176. unsigned int dev_ino;
  177. };
  178. unsigned long sysino;
  179. };
  180. struct ino_bucket bucket;
  181. unsigned long iclr;
  182. unsigned long imap;
  183. };
  184. static inline unsigned int irq_data_to_handle(struct irq_data *data)
  185. {
  186. struct irq_handler_data *ihd = data->handler_data;
  187. return ihd->dev_handle;
  188. }
  189. static inline unsigned int irq_data_to_ino(struct irq_data *data)
  190. {
  191. struct irq_handler_data *ihd = data->handler_data;
  192. return ihd->dev_ino;
  193. }
  194. static inline unsigned long irq_data_to_sysino(struct irq_data *data)
  195. {
  196. struct irq_handler_data *ihd = data->handler_data;
  197. return ihd->sysino;
  198. }
  199. void irq_free(unsigned int irq)
  200. {
  201. void *data = irq_get_handler_data(irq);
  202. kfree(data);
  203. irq_set_handler_data(irq, NULL);
  204. irq_free_descs(irq, 1);
  205. }
  206. unsigned int irq_alloc(unsigned int dev_handle, unsigned int dev_ino)
  207. {
  208. int irq;
  209. irq = __irq_alloc_descs(-1, 1, 1, numa_node_id(), NULL);
  210. if (irq <= 0)
  211. goto out;
  212. return irq;
  213. out:
  214. return 0;
  215. }
  216. static unsigned int cookie_exists(u32 devhandle, unsigned int devino)
  217. {
  218. unsigned long hv_err, cookie;
  219. struct ino_bucket *bucket;
  220. unsigned int irq = 0U;
  221. hv_err = sun4v_vintr_get_cookie(devhandle, devino, &cookie);
  222. if (hv_err) {
  223. pr_err("HV get cookie failed hv_err = %ld\n", hv_err);
  224. goto out;
  225. }
  226. if (cookie & ((1UL << 63UL))) {
  227. cookie = ~cookie;
  228. bucket = (struct ino_bucket *) __va(cookie);
  229. irq = bucket->__irq;
  230. }
  231. out:
  232. return irq;
  233. }
  234. static unsigned int sysino_exists(u32 devhandle, unsigned int devino)
  235. {
  236. unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
  237. struct ino_bucket *bucket;
  238. unsigned int irq;
  239. bucket = &ivector_table[sysino];
  240. irq = bucket_get_irq(__pa(bucket));
  241. return irq;
  242. }
  243. void ack_bad_irq(unsigned int irq)
  244. {
  245. pr_crit("BAD IRQ ack %d\n", irq);
  246. }
  247. void irq_install_pre_handler(int irq,
  248. void (*func)(unsigned int, void *, void *),
  249. void *arg1, void *arg2)
  250. {
  251. pr_warn("IRQ pre handler NOT supported.\n");
  252. }
  253. /*
  254. * /proc/interrupts printing:
  255. */
  256. int arch_show_interrupts(struct seq_file *p, int prec)
  257. {
  258. int j;
  259. seq_printf(p, "NMI: ");
  260. for_each_online_cpu(j)
  261. seq_printf(p, "%10u ", cpu_data(j).__nmi_count);
  262. seq_printf(p, " Non-maskable interrupts\n");
  263. return 0;
  264. }
  265. static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
  266. {
  267. unsigned int tid;
  268. if (this_is_starfire) {
  269. tid = starfire_translate(imap, cpuid);
  270. tid <<= IMAP_TID_SHIFT;
  271. tid &= IMAP_TID_UPA;
  272. } else {
  273. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  274. unsigned long ver;
  275. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  276. if ((ver >> 32UL) == __JALAPENO_ID ||
  277. (ver >> 32UL) == __SERRANO_ID) {
  278. tid = cpuid << IMAP_TID_SHIFT;
  279. tid &= IMAP_TID_JBUS;
  280. } else {
  281. unsigned int a = cpuid & 0x1f;
  282. unsigned int n = (cpuid >> 5) & 0x1f;
  283. tid = ((a << IMAP_AID_SHIFT) |
  284. (n << IMAP_NID_SHIFT));
  285. tid &= (IMAP_AID_SAFARI |
  286. IMAP_NID_SAFARI);
  287. }
  288. } else {
  289. tid = cpuid << IMAP_TID_SHIFT;
  290. tid &= IMAP_TID_UPA;
  291. }
  292. }
  293. return tid;
  294. }
  295. #ifdef CONFIG_SMP
  296. static int irq_choose_cpu(unsigned int irq, const struct cpumask *affinity)
  297. {
  298. cpumask_t mask;
  299. int cpuid;
  300. cpumask_copy(&mask, affinity);
  301. if (cpumask_equal(&mask, cpu_online_mask)) {
  302. cpuid = map_to_cpu(irq);
  303. } else {
  304. cpumask_t tmp;
  305. cpumask_and(&tmp, cpu_online_mask, &mask);
  306. cpuid = cpumask_empty(&tmp) ? map_to_cpu(irq) : cpumask_first(&tmp);
  307. }
  308. return cpuid;
  309. }
  310. #else
  311. #define irq_choose_cpu(irq, affinity) \
  312. real_hard_smp_processor_id()
  313. #endif
  314. static void sun4u_irq_enable(struct irq_data *data)
  315. {
  316. struct irq_handler_data *handler_data = data->handler_data;
  317. if (likely(handler_data)) {
  318. unsigned long cpuid, imap, val;
  319. unsigned int tid;
  320. cpuid = irq_choose_cpu(data->irq, data->affinity);
  321. imap = handler_data->imap;
  322. tid = sun4u_compute_tid(imap, cpuid);
  323. val = upa_readq(imap);
  324. val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
  325. IMAP_AID_SAFARI | IMAP_NID_SAFARI);
  326. val |= tid | IMAP_VALID;
  327. upa_writeq(val, imap);
  328. upa_writeq(ICLR_IDLE, handler_data->iclr);
  329. }
  330. }
  331. static int sun4u_set_affinity(struct irq_data *data,
  332. const struct cpumask *mask, bool force)
  333. {
  334. struct irq_handler_data *handler_data = data->handler_data;
  335. if (likely(handler_data)) {
  336. unsigned long cpuid, imap, val;
  337. unsigned int tid;
  338. cpuid = irq_choose_cpu(data->irq, mask);
  339. imap = handler_data->imap;
  340. tid = sun4u_compute_tid(imap, cpuid);
  341. val = upa_readq(imap);
  342. val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
  343. IMAP_AID_SAFARI | IMAP_NID_SAFARI);
  344. val |= tid | IMAP_VALID;
  345. upa_writeq(val, imap);
  346. upa_writeq(ICLR_IDLE, handler_data->iclr);
  347. }
  348. return 0;
  349. }
  350. /* Don't do anything. The desc->status check for IRQ_DISABLED in
  351. * handler_irq() will skip the handler call and that will leave the
  352. * interrupt in the sent state. The next ->enable() call will hit the
  353. * ICLR register to reset the state machine.
  354. *
  355. * This scheme is necessary, instead of clearing the Valid bit in the
  356. * IMAP register, to handle the case of IMAP registers being shared by
  357. * multiple INOs (and thus ICLR registers). Since we use a different
  358. * virtual IRQ for each shared IMAP instance, the generic code thinks
  359. * there is only one user so it prematurely calls ->disable() on
  360. * free_irq().
  361. *
  362. * We have to provide an explicit ->disable() method instead of using
  363. * NULL to get the default. The reason is that if the generic code
  364. * sees that, it also hooks up a default ->shutdown method which
  365. * invokes ->mask() which we do not want. See irq_chip_set_defaults().
  366. */
  367. static void sun4u_irq_disable(struct irq_data *data)
  368. {
  369. }
  370. static void sun4u_irq_eoi(struct irq_data *data)
  371. {
  372. struct irq_handler_data *handler_data = data->handler_data;
  373. if (likely(handler_data))
  374. upa_writeq(ICLR_IDLE, handler_data->iclr);
  375. }
  376. static void sun4v_irq_enable(struct irq_data *data)
  377. {
  378. unsigned long cpuid = irq_choose_cpu(data->irq, data->affinity);
  379. unsigned int ino = irq_data_to_sysino(data);
  380. int err;
  381. err = sun4v_intr_settarget(ino, cpuid);
  382. if (err != HV_EOK)
  383. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  384. "err(%d)\n", ino, cpuid, err);
  385. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  386. if (err != HV_EOK)
  387. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  388. "err(%d)\n", ino, err);
  389. err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
  390. if (err != HV_EOK)
  391. printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
  392. ino, err);
  393. }
  394. static int sun4v_set_affinity(struct irq_data *data,
  395. const struct cpumask *mask, bool force)
  396. {
  397. unsigned long cpuid = irq_choose_cpu(data->irq, mask);
  398. unsigned int ino = irq_data_to_sysino(data);
  399. int err;
  400. err = sun4v_intr_settarget(ino, cpuid);
  401. if (err != HV_EOK)
  402. printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
  403. "err(%d)\n", ino, cpuid, err);
  404. return 0;
  405. }
  406. static void sun4v_irq_disable(struct irq_data *data)
  407. {
  408. unsigned int ino = irq_data_to_sysino(data);
  409. int err;
  410. err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
  411. if (err != HV_EOK)
  412. printk(KERN_ERR "sun4v_intr_setenabled(%x): "
  413. "err(%d)\n", ino, err);
  414. }
  415. static void sun4v_irq_eoi(struct irq_data *data)
  416. {
  417. unsigned int ino = irq_data_to_sysino(data);
  418. int err;
  419. err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
  420. if (err != HV_EOK)
  421. printk(KERN_ERR "sun4v_intr_setstate(%x): "
  422. "err(%d)\n", ino, err);
  423. }
  424. static void sun4v_virq_enable(struct irq_data *data)
  425. {
  426. unsigned long dev_handle = irq_data_to_handle(data);
  427. unsigned long dev_ino = irq_data_to_ino(data);
  428. unsigned long cpuid;
  429. int err;
  430. cpuid = irq_choose_cpu(data->irq, data->affinity);
  431. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  432. if (err != HV_EOK)
  433. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  434. "err(%d)\n",
  435. dev_handle, dev_ino, cpuid, err);
  436. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  437. HV_INTR_STATE_IDLE);
  438. if (err != HV_EOK)
  439. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  440. "HV_INTR_STATE_IDLE): err(%d)\n",
  441. dev_handle, dev_ino, err);
  442. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  443. HV_INTR_ENABLED);
  444. if (err != HV_EOK)
  445. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  446. "HV_INTR_ENABLED): err(%d)\n",
  447. dev_handle, dev_ino, err);
  448. }
  449. static int sun4v_virt_set_affinity(struct irq_data *data,
  450. const struct cpumask *mask, bool force)
  451. {
  452. unsigned long dev_handle = irq_data_to_handle(data);
  453. unsigned long dev_ino = irq_data_to_ino(data);
  454. unsigned long cpuid;
  455. int err;
  456. cpuid = irq_choose_cpu(data->irq, mask);
  457. err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
  458. if (err != HV_EOK)
  459. printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
  460. "err(%d)\n",
  461. dev_handle, dev_ino, cpuid, err);
  462. return 0;
  463. }
  464. static void sun4v_virq_disable(struct irq_data *data)
  465. {
  466. unsigned long dev_handle = irq_data_to_handle(data);
  467. unsigned long dev_ino = irq_data_to_ino(data);
  468. int err;
  469. err = sun4v_vintr_set_valid(dev_handle, dev_ino,
  470. HV_INTR_DISABLED);
  471. if (err != HV_EOK)
  472. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  473. "HV_INTR_DISABLED): err(%d)\n",
  474. dev_handle, dev_ino, err);
  475. }
  476. static void sun4v_virq_eoi(struct irq_data *data)
  477. {
  478. unsigned long dev_handle = irq_data_to_handle(data);
  479. unsigned long dev_ino = irq_data_to_ino(data);
  480. int err;
  481. err = sun4v_vintr_set_state(dev_handle, dev_ino,
  482. HV_INTR_STATE_IDLE);
  483. if (err != HV_EOK)
  484. printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
  485. "HV_INTR_STATE_IDLE): err(%d)\n",
  486. dev_handle, dev_ino, err);
  487. }
  488. static struct irq_chip sun4u_irq = {
  489. .name = "sun4u",
  490. .irq_enable = sun4u_irq_enable,
  491. .irq_disable = sun4u_irq_disable,
  492. .irq_eoi = sun4u_irq_eoi,
  493. .irq_set_affinity = sun4u_set_affinity,
  494. .flags = IRQCHIP_EOI_IF_HANDLED,
  495. };
  496. static struct irq_chip sun4v_irq = {
  497. .name = "sun4v",
  498. .irq_enable = sun4v_irq_enable,
  499. .irq_disable = sun4v_irq_disable,
  500. .irq_eoi = sun4v_irq_eoi,
  501. .irq_set_affinity = sun4v_set_affinity,
  502. .flags = IRQCHIP_EOI_IF_HANDLED,
  503. };
  504. static struct irq_chip sun4v_virq = {
  505. .name = "vsun4v",
  506. .irq_enable = sun4v_virq_enable,
  507. .irq_disable = sun4v_virq_disable,
  508. .irq_eoi = sun4v_virq_eoi,
  509. .irq_set_affinity = sun4v_virt_set_affinity,
  510. .flags = IRQCHIP_EOI_IF_HANDLED,
  511. };
  512. unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
  513. {
  514. struct irq_handler_data *handler_data;
  515. struct ino_bucket *bucket;
  516. unsigned int irq;
  517. int ino;
  518. BUG_ON(tlb_type == hypervisor);
  519. ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
  520. bucket = &ivector_table[ino];
  521. irq = bucket_get_irq(__pa(bucket));
  522. if (!irq) {
  523. irq = irq_alloc(0, ino);
  524. bucket_set_irq(__pa(bucket), irq);
  525. irq_set_chip_and_handler_name(irq, &sun4u_irq,
  526. handle_fasteoi_irq, "IVEC");
  527. }
  528. handler_data = irq_get_handler_data(irq);
  529. if (unlikely(handler_data))
  530. goto out;
  531. handler_data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  532. if (unlikely(!handler_data)) {
  533. prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
  534. prom_halt();
  535. }
  536. irq_set_handler_data(irq, handler_data);
  537. handler_data->imap = imap;
  538. handler_data->iclr = iclr;
  539. out:
  540. return irq;
  541. }
  542. static unsigned int sun4v_build_common(u32 devhandle, unsigned int devino,
  543. void (*handler_data_init)(struct irq_handler_data *data,
  544. u32 devhandle, unsigned int devino),
  545. struct irq_chip *chip)
  546. {
  547. struct irq_handler_data *data;
  548. unsigned int irq;
  549. irq = irq_alloc(devhandle, devino);
  550. if (!irq)
  551. goto out;
  552. data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
  553. if (unlikely(!data)) {
  554. pr_err("IRQ handler data allocation failed.\n");
  555. irq_free(irq);
  556. irq = 0;
  557. goto out;
  558. }
  559. irq_set_handler_data(irq, data);
  560. handler_data_init(data, devhandle, devino);
  561. irq_set_chip_and_handler_name(irq, chip, handle_fasteoi_irq, "IVEC");
  562. data->imap = ~0UL;
  563. data->iclr = ~0UL;
  564. out:
  565. return irq;
  566. }
  567. static unsigned long cookie_assign(unsigned int irq, u32 devhandle,
  568. unsigned int devino)
  569. {
  570. struct irq_handler_data *ihd = irq_get_handler_data(irq);
  571. unsigned long hv_error, cookie;
  572. /* handler_irq needs to find the irq. cookie is seen signed in
  573. * sun4v_dev_mondo and treated as a non ivector_table delivery.
  574. */
  575. ihd->bucket.__irq = irq;
  576. cookie = ~__pa(&ihd->bucket);
  577. hv_error = sun4v_vintr_set_cookie(devhandle, devino, cookie);
  578. if (hv_error)
  579. pr_err("HV vintr set cookie failed = %ld\n", hv_error);
  580. return hv_error;
  581. }
  582. static void cookie_handler_data(struct irq_handler_data *data,
  583. u32 devhandle, unsigned int devino)
  584. {
  585. data->dev_handle = devhandle;
  586. data->dev_ino = devino;
  587. }
  588. static unsigned int cookie_build_irq(u32 devhandle, unsigned int devino,
  589. struct irq_chip *chip)
  590. {
  591. unsigned long hv_error;
  592. unsigned int irq;
  593. irq = sun4v_build_common(devhandle, devino, cookie_handler_data, chip);
  594. hv_error = cookie_assign(irq, devhandle, devino);
  595. if (hv_error) {
  596. irq_free(irq);
  597. irq = 0;
  598. }
  599. return irq;
  600. }
  601. static unsigned int sun4v_build_cookie(u32 devhandle, unsigned int devino)
  602. {
  603. unsigned int irq;
  604. irq = cookie_exists(devhandle, devino);
  605. if (irq)
  606. goto out;
  607. irq = cookie_build_irq(devhandle, devino, &sun4v_virq);
  608. out:
  609. return irq;
  610. }
  611. static void sysino_set_bucket(unsigned int irq)
  612. {
  613. struct irq_handler_data *ihd = irq_get_handler_data(irq);
  614. struct ino_bucket *bucket;
  615. unsigned long sysino;
  616. sysino = sun4v_devino_to_sysino(ihd->dev_handle, ihd->dev_ino);
  617. BUG_ON(sysino >= nr_ivec);
  618. bucket = &ivector_table[sysino];
  619. bucket_set_irq(__pa(bucket), irq);
  620. }
  621. static void sysino_handler_data(struct irq_handler_data *data,
  622. u32 devhandle, unsigned int devino)
  623. {
  624. unsigned long sysino;
  625. sysino = sun4v_devino_to_sysino(devhandle, devino);
  626. data->sysino = sysino;
  627. }
  628. static unsigned int sysino_build_irq(u32 devhandle, unsigned int devino,
  629. struct irq_chip *chip)
  630. {
  631. unsigned int irq;
  632. irq = sun4v_build_common(devhandle, devino, sysino_handler_data, chip);
  633. if (!irq)
  634. goto out;
  635. sysino_set_bucket(irq);
  636. out:
  637. return irq;
  638. }
  639. static int sun4v_build_sysino(u32 devhandle, unsigned int devino)
  640. {
  641. int irq;
  642. irq = sysino_exists(devhandle, devino);
  643. if (irq)
  644. goto out;
  645. irq = sysino_build_irq(devhandle, devino, &sun4v_irq);
  646. out:
  647. return irq;
  648. }
  649. unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
  650. {
  651. unsigned int irq;
  652. if (sun4v_cookie_only_virqs())
  653. irq = sun4v_build_cookie(devhandle, devino);
  654. else
  655. irq = sun4v_build_sysino(devhandle, devino);
  656. return irq;
  657. }
  658. unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
  659. {
  660. int irq;
  661. irq = cookie_build_irq(devhandle, devino, &sun4v_virq);
  662. if (!irq)
  663. goto out;
  664. /* This is borrowed from the original function.
  665. */
  666. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  667. out:
  668. return irq;
  669. }
  670. void *hardirq_stack[NR_CPUS];
  671. void *softirq_stack[NR_CPUS];
  672. void __irq_entry handler_irq(int pil, struct pt_regs *regs)
  673. {
  674. unsigned long pstate, bucket_pa;
  675. struct pt_regs *old_regs;
  676. void *orig_sp;
  677. clear_softint(1 << pil);
  678. old_regs = set_irq_regs(regs);
  679. irq_enter();
  680. /* Grab an atomic snapshot of the pending IVECs. */
  681. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  682. "wrpr %0, %3, %%pstate\n\t"
  683. "ldx [%2], %1\n\t"
  684. "stx %%g0, [%2]\n\t"
  685. "wrpr %0, 0x0, %%pstate\n\t"
  686. : "=&r" (pstate), "=&r" (bucket_pa)
  687. : "r" (irq_work_pa(smp_processor_id())),
  688. "i" (PSTATE_IE)
  689. : "memory");
  690. orig_sp = set_hardirq_stack();
  691. while (bucket_pa) {
  692. unsigned long next_pa;
  693. unsigned int irq;
  694. next_pa = bucket_get_chain_pa(bucket_pa);
  695. irq = bucket_get_irq(bucket_pa);
  696. bucket_clear_chain_pa(bucket_pa);
  697. generic_handle_irq(irq);
  698. bucket_pa = next_pa;
  699. }
  700. restore_hardirq_stack(orig_sp);
  701. irq_exit();
  702. set_irq_regs(old_regs);
  703. }
  704. void do_softirq_own_stack(void)
  705. {
  706. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  707. sp += THREAD_SIZE - 192 - STACK_BIAS;
  708. __asm__ __volatile__("mov %%sp, %0\n\t"
  709. "mov %1, %%sp"
  710. : "=&r" (orig_sp)
  711. : "r" (sp));
  712. __do_softirq();
  713. __asm__ __volatile__("mov %0, %%sp"
  714. : : "r" (orig_sp));
  715. }
  716. #ifdef CONFIG_HOTPLUG_CPU
  717. void fixup_irqs(void)
  718. {
  719. unsigned int irq;
  720. for (irq = 0; irq < NR_IRQS; irq++) {
  721. struct irq_desc *desc = irq_to_desc(irq);
  722. struct irq_data *data;
  723. unsigned long flags;
  724. if (!desc)
  725. continue;
  726. data = irq_desc_get_irq_data(desc);
  727. raw_spin_lock_irqsave(&desc->lock, flags);
  728. if (desc->action && !irqd_is_per_cpu(data)) {
  729. if (data->chip->irq_set_affinity)
  730. data->chip->irq_set_affinity(data,
  731. data->affinity,
  732. false);
  733. }
  734. raw_spin_unlock_irqrestore(&desc->lock, flags);
  735. }
  736. tick_ops->disable_irq();
  737. }
  738. #endif
  739. struct sun5_timer {
  740. u64 count0;
  741. u64 limit0;
  742. u64 count1;
  743. u64 limit1;
  744. };
  745. static struct sun5_timer *prom_timers;
  746. static u64 prom_limit0, prom_limit1;
  747. static void map_prom_timers(void)
  748. {
  749. struct device_node *dp;
  750. const unsigned int *addr;
  751. /* PROM timer node hangs out in the top level of device siblings... */
  752. dp = of_find_node_by_path("/");
  753. dp = dp->child;
  754. while (dp) {
  755. if (!strcmp(dp->name, "counter-timer"))
  756. break;
  757. dp = dp->sibling;
  758. }
  759. /* Assume if node is not present, PROM uses different tick mechanism
  760. * which we should not care about.
  761. */
  762. if (!dp) {
  763. prom_timers = (struct sun5_timer *) 0;
  764. return;
  765. }
  766. /* If PROM is really using this, it must be mapped by him. */
  767. addr = of_get_property(dp, "address", NULL);
  768. if (!addr) {
  769. prom_printf("PROM does not have timer mapped, trying to continue.\n");
  770. prom_timers = (struct sun5_timer *) 0;
  771. return;
  772. }
  773. prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
  774. }
  775. static void kill_prom_timer(void)
  776. {
  777. if (!prom_timers)
  778. return;
  779. /* Save them away for later. */
  780. prom_limit0 = prom_timers->limit0;
  781. prom_limit1 = prom_timers->limit1;
  782. /* Just as in sun4c PROM uses timer which ticks at IRQ 14.
  783. * We turn both off here just to be paranoid.
  784. */
  785. prom_timers->limit0 = 0;
  786. prom_timers->limit1 = 0;
  787. /* Wheee, eat the interrupt packet too... */
  788. __asm__ __volatile__(
  789. " mov 0x40, %%g2\n"
  790. " ldxa [%%g0] %0, %%g1\n"
  791. " ldxa [%%g2] %1, %%g1\n"
  792. " stxa %%g0, [%%g0] %0\n"
  793. " membar #Sync\n"
  794. : /* no outputs */
  795. : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
  796. : "g1", "g2");
  797. }
  798. void notrace init_irqwork_curcpu(void)
  799. {
  800. int cpu = hard_smp_processor_id();
  801. trap_block[cpu].irq_worklist_pa = 0UL;
  802. }
  803. /* Please be very careful with register_one_mondo() and
  804. * sun4v_register_mondo_queues().
  805. *
  806. * On SMP this gets invoked from the CPU trampoline before
  807. * the cpu has fully taken over the trap table from OBP,
  808. * and it's kernel stack + %g6 thread register state is
  809. * not fully cooked yet.
  810. *
  811. * Therefore you cannot make any OBP calls, not even prom_printf,
  812. * from these two routines.
  813. */
  814. static void notrace register_one_mondo(unsigned long paddr, unsigned long type,
  815. unsigned long qmask)
  816. {
  817. unsigned long num_entries = (qmask + 1) / 64;
  818. unsigned long status;
  819. status = sun4v_cpu_qconf(type, paddr, num_entries);
  820. if (status != HV_EOK) {
  821. prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
  822. "err %lu\n", type, paddr, num_entries, status);
  823. prom_halt();
  824. }
  825. }
  826. void notrace sun4v_register_mondo_queues(int this_cpu)
  827. {
  828. struct trap_per_cpu *tb = &trap_block[this_cpu];
  829. register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
  830. tb->cpu_mondo_qmask);
  831. register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
  832. tb->dev_mondo_qmask);
  833. register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
  834. tb->resum_qmask);
  835. register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
  836. tb->nonresum_qmask);
  837. }
  838. /* Each queue region must be a power of 2 multiple of 64 bytes in
  839. * size. The base real address must be aligned to the size of the
  840. * region. Thus, an 8KB queue must be 8KB aligned, for example.
  841. */
  842. static void __init alloc_one_queue(unsigned long *pa_ptr, unsigned long qmask)
  843. {
  844. unsigned long size = PAGE_ALIGN(qmask + 1);
  845. unsigned long order = get_order(size);
  846. unsigned long p;
  847. p = __get_free_pages(GFP_KERNEL, order);
  848. if (!p) {
  849. prom_printf("SUN4V: Error, cannot allocate queue.\n");
  850. prom_halt();
  851. }
  852. *pa_ptr = __pa(p);
  853. }
  854. static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
  855. {
  856. #ifdef CONFIG_SMP
  857. unsigned long page;
  858. BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
  859. page = get_zeroed_page(GFP_KERNEL);
  860. if (!page) {
  861. prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
  862. prom_halt();
  863. }
  864. tb->cpu_mondo_block_pa = __pa(page);
  865. tb->cpu_list_pa = __pa(page + 64);
  866. #endif
  867. }
  868. /* Allocate mondo and error queues for all possible cpus. */
  869. static void __init sun4v_init_mondo_queues(void)
  870. {
  871. int cpu;
  872. for_each_possible_cpu(cpu) {
  873. struct trap_per_cpu *tb = &trap_block[cpu];
  874. alloc_one_queue(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
  875. alloc_one_queue(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
  876. alloc_one_queue(&tb->resum_mondo_pa, tb->resum_qmask);
  877. alloc_one_queue(&tb->resum_kernel_buf_pa, tb->resum_qmask);
  878. alloc_one_queue(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
  879. alloc_one_queue(&tb->nonresum_kernel_buf_pa,
  880. tb->nonresum_qmask);
  881. }
  882. }
  883. static void __init init_send_mondo_info(void)
  884. {
  885. int cpu;
  886. for_each_possible_cpu(cpu) {
  887. struct trap_per_cpu *tb = &trap_block[cpu];
  888. init_cpu_send_mondo_info(tb);
  889. }
  890. }
  891. static struct irqaction timer_irq_action = {
  892. .name = "timer",
  893. };
  894. static void __init irq_ivector_init(void)
  895. {
  896. unsigned long size, order;
  897. unsigned int ivecs;
  898. /* If we are doing cookie only VIRQs then we do not need the ivector
  899. * table to process interrupts.
  900. */
  901. if (sun4v_cookie_only_virqs())
  902. return;
  903. ivecs = size_nr_ivec();
  904. size = sizeof(struct ino_bucket) * ivecs;
  905. order = get_order(size);
  906. ivector_table = (struct ino_bucket *)
  907. __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  908. if (!ivector_table) {
  909. prom_printf("Fatal error, cannot allocate ivector_table\n");
  910. prom_halt();
  911. }
  912. __flush_dcache_range((unsigned long) ivector_table,
  913. ((unsigned long) ivector_table) + size);
  914. ivector_table_pa = __pa(ivector_table);
  915. }
  916. /* Only invoked on boot processor.*/
  917. void __init init_IRQ(void)
  918. {
  919. irq_init_hv();
  920. irq_ivector_init();
  921. map_prom_timers();
  922. kill_prom_timer();
  923. if (tlb_type == hypervisor)
  924. sun4v_init_mondo_queues();
  925. init_send_mondo_info();
  926. if (tlb_type == hypervisor) {
  927. /* Load up the boot cpu's entries. */
  928. sun4v_register_mondo_queues(hard_smp_processor_id());
  929. }
  930. /* We need to clear any IRQ's pending in the soft interrupt
  931. * registers, a spurious one could be left around from the
  932. * PROM timer which we just disabled.
  933. */
  934. clear_softint(get_softint());
  935. /* Now that ivector table is initialized, it is safe
  936. * to receive IRQ vector traps. We will normally take
  937. * one or two right now, in case some device PROM used
  938. * to boot us wants to speak to us. We just ignore them.
  939. */
  940. __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
  941. "or %%g1, %0, %%g1\n\t"
  942. "wrpr %%g1, 0x0, %%pstate"
  943. : /* No outputs */
  944. : "i" (PSTATE_IE)
  945. : "g1");
  946. irq_to_desc(0)->action = &timer_irq_action;
  947. }