irq_64.c 28 KB

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