irq-gic-v3.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /*
  2. * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved.
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/acpi.h>
  18. #include <linux/cpu.h>
  19. #include <linux/cpu_pm.h>
  20. #include <linux/delay.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irqdomain.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/percpu.h>
  27. #include <linux/slab.h>
  28. #include <linux/irqchip.h>
  29. #include <linux/irqchip/arm-gic-v3.h>
  30. #include <asm/cputype.h>
  31. #include <asm/exception.h>
  32. #include <asm/smp_plat.h>
  33. #include <asm/virt.h>
  34. #include "irq-gic-common.h"
  35. struct redist_region {
  36. void __iomem *redist_base;
  37. phys_addr_t phys_base;
  38. bool single_redist;
  39. };
  40. struct gic_chip_data {
  41. void __iomem *dist_base;
  42. struct redist_region *redist_regions;
  43. struct rdists rdists;
  44. struct irq_domain *domain;
  45. u64 redist_stride;
  46. u32 nr_redist_regions;
  47. unsigned int irq_nr;
  48. };
  49. static struct gic_chip_data gic_data __read_mostly;
  50. static struct static_key supports_deactivate = STATIC_KEY_INIT_TRUE;
  51. #define gic_data_rdist() (this_cpu_ptr(gic_data.rdists.rdist))
  52. #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
  53. #define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K)
  54. /* Our default, arbitrary priority value. Linux only uses one anyway. */
  55. #define DEFAULT_PMR_VALUE 0xf0
  56. static inline unsigned int gic_irq(struct irq_data *d)
  57. {
  58. return d->hwirq;
  59. }
  60. static inline int gic_irq_in_rdist(struct irq_data *d)
  61. {
  62. return gic_irq(d) < 32;
  63. }
  64. static inline void __iomem *gic_dist_base(struct irq_data *d)
  65. {
  66. if (gic_irq_in_rdist(d)) /* SGI+PPI -> SGI_base for this CPU */
  67. return gic_data_rdist_sgi_base();
  68. if (d->hwirq <= 1023) /* SPI -> dist_base */
  69. return gic_data.dist_base;
  70. return NULL;
  71. }
  72. static void gic_do_wait_for_rwp(void __iomem *base)
  73. {
  74. u32 count = 1000000; /* 1s! */
  75. while (readl_relaxed(base + GICD_CTLR) & GICD_CTLR_RWP) {
  76. count--;
  77. if (!count) {
  78. pr_err_ratelimited("RWP timeout, gone fishing\n");
  79. return;
  80. }
  81. cpu_relax();
  82. udelay(1);
  83. };
  84. }
  85. /* Wait for completion of a distributor change */
  86. static void gic_dist_wait_for_rwp(void)
  87. {
  88. gic_do_wait_for_rwp(gic_data.dist_base);
  89. }
  90. /* Wait for completion of a redistributor change */
  91. static void gic_redist_wait_for_rwp(void)
  92. {
  93. gic_do_wait_for_rwp(gic_data_rdist_rd_base());
  94. }
  95. #ifdef CONFIG_ARM64
  96. static DEFINE_STATIC_KEY_FALSE(is_cavium_thunderx);
  97. static u64 __maybe_unused gic_read_iar(void)
  98. {
  99. if (static_branch_unlikely(&is_cavium_thunderx))
  100. return gic_read_iar_cavium_thunderx();
  101. else
  102. return gic_read_iar_common();
  103. }
  104. #endif
  105. static void gic_enable_redist(bool enable)
  106. {
  107. void __iomem *rbase;
  108. u32 count = 1000000; /* 1s! */
  109. u32 val;
  110. rbase = gic_data_rdist_rd_base();
  111. val = readl_relaxed(rbase + GICR_WAKER);
  112. if (enable)
  113. /* Wake up this CPU redistributor */
  114. val &= ~GICR_WAKER_ProcessorSleep;
  115. else
  116. val |= GICR_WAKER_ProcessorSleep;
  117. writel_relaxed(val, rbase + GICR_WAKER);
  118. if (!enable) { /* Check that GICR_WAKER is writeable */
  119. val = readl_relaxed(rbase + GICR_WAKER);
  120. if (!(val & GICR_WAKER_ProcessorSleep))
  121. return; /* No PM support in this redistributor */
  122. }
  123. while (count--) {
  124. val = readl_relaxed(rbase + GICR_WAKER);
  125. if (enable ^ (val & GICR_WAKER_ChildrenAsleep))
  126. break;
  127. cpu_relax();
  128. udelay(1);
  129. };
  130. if (!count)
  131. pr_err_ratelimited("redistributor failed to %s...\n",
  132. enable ? "wakeup" : "sleep");
  133. }
  134. /*
  135. * Routines to disable, enable, EOI and route interrupts
  136. */
  137. static int gic_peek_irq(struct irq_data *d, u32 offset)
  138. {
  139. u32 mask = 1 << (gic_irq(d) % 32);
  140. void __iomem *base;
  141. if (gic_irq_in_rdist(d))
  142. base = gic_data_rdist_sgi_base();
  143. else
  144. base = gic_data.dist_base;
  145. return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask);
  146. }
  147. static void gic_poke_irq(struct irq_data *d, u32 offset)
  148. {
  149. u32 mask = 1 << (gic_irq(d) % 32);
  150. void (*rwp_wait)(void);
  151. void __iomem *base;
  152. if (gic_irq_in_rdist(d)) {
  153. base = gic_data_rdist_sgi_base();
  154. rwp_wait = gic_redist_wait_for_rwp;
  155. } else {
  156. base = gic_data.dist_base;
  157. rwp_wait = gic_dist_wait_for_rwp;
  158. }
  159. writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4);
  160. rwp_wait();
  161. }
  162. static void gic_mask_irq(struct irq_data *d)
  163. {
  164. gic_poke_irq(d, GICD_ICENABLER);
  165. }
  166. static void gic_eoimode1_mask_irq(struct irq_data *d)
  167. {
  168. gic_mask_irq(d);
  169. /*
  170. * When masking a forwarded interrupt, make sure it is
  171. * deactivated as well.
  172. *
  173. * This ensures that an interrupt that is getting
  174. * disabled/masked will not get "stuck", because there is
  175. * noone to deactivate it (guest is being terminated).
  176. */
  177. if (irqd_is_forwarded_to_vcpu(d))
  178. gic_poke_irq(d, GICD_ICACTIVER);
  179. }
  180. static void gic_unmask_irq(struct irq_data *d)
  181. {
  182. gic_poke_irq(d, GICD_ISENABLER);
  183. }
  184. static int gic_irq_set_irqchip_state(struct irq_data *d,
  185. enum irqchip_irq_state which, bool val)
  186. {
  187. u32 reg;
  188. if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
  189. return -EINVAL;
  190. switch (which) {
  191. case IRQCHIP_STATE_PENDING:
  192. reg = val ? GICD_ISPENDR : GICD_ICPENDR;
  193. break;
  194. case IRQCHIP_STATE_ACTIVE:
  195. reg = val ? GICD_ISACTIVER : GICD_ICACTIVER;
  196. break;
  197. case IRQCHIP_STATE_MASKED:
  198. reg = val ? GICD_ICENABLER : GICD_ISENABLER;
  199. break;
  200. default:
  201. return -EINVAL;
  202. }
  203. gic_poke_irq(d, reg);
  204. return 0;
  205. }
  206. static int gic_irq_get_irqchip_state(struct irq_data *d,
  207. enum irqchip_irq_state which, bool *val)
  208. {
  209. if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
  210. return -EINVAL;
  211. switch (which) {
  212. case IRQCHIP_STATE_PENDING:
  213. *val = gic_peek_irq(d, GICD_ISPENDR);
  214. break;
  215. case IRQCHIP_STATE_ACTIVE:
  216. *val = gic_peek_irq(d, GICD_ISACTIVER);
  217. break;
  218. case IRQCHIP_STATE_MASKED:
  219. *val = !gic_peek_irq(d, GICD_ISENABLER);
  220. break;
  221. default:
  222. return -EINVAL;
  223. }
  224. return 0;
  225. }
  226. static void gic_eoi_irq(struct irq_data *d)
  227. {
  228. gic_write_eoir(gic_irq(d));
  229. }
  230. static void gic_eoimode1_eoi_irq(struct irq_data *d)
  231. {
  232. /*
  233. * No need to deactivate an LPI, or an interrupt that
  234. * is is getting forwarded to a vcpu.
  235. */
  236. if (gic_irq(d) >= 8192 || irqd_is_forwarded_to_vcpu(d))
  237. return;
  238. gic_write_dir(gic_irq(d));
  239. }
  240. static int gic_set_type(struct irq_data *d, unsigned int type)
  241. {
  242. unsigned int irq = gic_irq(d);
  243. void (*rwp_wait)(void);
  244. void __iomem *base;
  245. /* Interrupt configuration for SGIs can't be changed */
  246. if (irq < 16)
  247. return -EINVAL;
  248. /* SPIs have restrictions on the supported types */
  249. if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
  250. type != IRQ_TYPE_EDGE_RISING)
  251. return -EINVAL;
  252. if (gic_irq_in_rdist(d)) {
  253. base = gic_data_rdist_sgi_base();
  254. rwp_wait = gic_redist_wait_for_rwp;
  255. } else {
  256. base = gic_data.dist_base;
  257. rwp_wait = gic_dist_wait_for_rwp;
  258. }
  259. return gic_configure_irq(irq, type, base, rwp_wait);
  260. }
  261. static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
  262. {
  263. if (vcpu)
  264. irqd_set_forwarded_to_vcpu(d);
  265. else
  266. irqd_clr_forwarded_to_vcpu(d);
  267. return 0;
  268. }
  269. static u64 gic_mpidr_to_affinity(unsigned long mpidr)
  270. {
  271. u64 aff;
  272. aff = ((u64)MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |
  273. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  274. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  275. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  276. return aff;
  277. }
  278. static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
  279. {
  280. u32 irqnr;
  281. do {
  282. irqnr = gic_read_iar();
  283. if (likely(irqnr > 15 && irqnr < 1020) || irqnr >= 8192) {
  284. int err;
  285. if (static_key_true(&supports_deactivate))
  286. gic_write_eoir(irqnr);
  287. err = handle_domain_irq(gic_data.domain, irqnr, regs);
  288. if (err) {
  289. WARN_ONCE(true, "Unexpected interrupt received!\n");
  290. if (static_key_true(&supports_deactivate)) {
  291. if (irqnr < 8192)
  292. gic_write_dir(irqnr);
  293. } else {
  294. gic_write_eoir(irqnr);
  295. }
  296. }
  297. continue;
  298. }
  299. if (irqnr < 16) {
  300. gic_write_eoir(irqnr);
  301. if (static_key_true(&supports_deactivate))
  302. gic_write_dir(irqnr);
  303. #ifdef CONFIG_SMP
  304. handle_IPI(irqnr, regs);
  305. #else
  306. WARN_ONCE(true, "Unexpected SGI received!\n");
  307. #endif
  308. continue;
  309. }
  310. } while (irqnr != ICC_IAR1_EL1_SPURIOUS);
  311. }
  312. static void __init gic_dist_init(void)
  313. {
  314. unsigned int i;
  315. u64 affinity;
  316. void __iomem *base = gic_data.dist_base;
  317. /* Disable the distributor */
  318. writel_relaxed(0, base + GICD_CTLR);
  319. gic_dist_wait_for_rwp();
  320. gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp);
  321. /* Enable distributor with ARE, Group1 */
  322. writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1,
  323. base + GICD_CTLR);
  324. /*
  325. * Set all global interrupts to the boot CPU only. ARE must be
  326. * enabled.
  327. */
  328. affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id()));
  329. for (i = 32; i < gic_data.irq_nr; i++)
  330. gic_write_irouter(affinity, base + GICD_IROUTER + i * 8);
  331. }
  332. static int gic_populate_rdist(void)
  333. {
  334. unsigned long mpidr = cpu_logical_map(smp_processor_id());
  335. u64 typer;
  336. u32 aff;
  337. int i;
  338. /*
  339. * Convert affinity to a 32bit value that can be matched to
  340. * GICR_TYPER bits [63:32].
  341. */
  342. aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |
  343. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  344. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  345. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  346. for (i = 0; i < gic_data.nr_redist_regions; i++) {
  347. void __iomem *ptr = gic_data.redist_regions[i].redist_base;
  348. u32 reg;
  349. reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;
  350. if (reg != GIC_PIDR2_ARCH_GICv3 &&
  351. reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */
  352. pr_warn("No redistributor present @%p\n", ptr);
  353. break;
  354. }
  355. do {
  356. typer = gic_read_typer(ptr + GICR_TYPER);
  357. if ((typer >> 32) == aff) {
  358. u64 offset = ptr - gic_data.redist_regions[i].redist_base;
  359. gic_data_rdist_rd_base() = ptr;
  360. gic_data_rdist()->phys_base = gic_data.redist_regions[i].phys_base + offset;
  361. pr_info("CPU%d: found redistributor %lx region %d:%pa\n",
  362. smp_processor_id(), mpidr, i,
  363. &gic_data_rdist()->phys_base);
  364. return 0;
  365. }
  366. if (gic_data.redist_regions[i].single_redist)
  367. break;
  368. if (gic_data.redist_stride) {
  369. ptr += gic_data.redist_stride;
  370. } else {
  371. ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
  372. if (typer & GICR_TYPER_VLPIS)
  373. ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
  374. }
  375. } while (!(typer & GICR_TYPER_LAST));
  376. }
  377. /* We couldn't even deal with ourselves... */
  378. WARN(true, "CPU%d: mpidr %lx has no re-distributor!\n",
  379. smp_processor_id(), mpidr);
  380. return -ENODEV;
  381. }
  382. static void gic_cpu_sys_reg_init(void)
  383. {
  384. /*
  385. * Need to check that the SRE bit has actually been set. If
  386. * not, it means that SRE is disabled at EL2. We're going to
  387. * die painfully, and there is nothing we can do about it.
  388. *
  389. * Kindly inform the luser.
  390. */
  391. if (!gic_enable_sre())
  392. pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
  393. /* Set priority mask register */
  394. gic_write_pmr(DEFAULT_PMR_VALUE);
  395. if (static_key_true(&supports_deactivate)) {
  396. /* EOI drops priority only (mode 1) */
  397. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
  398. } else {
  399. /* EOI deactivates interrupt too (mode 0) */
  400. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
  401. }
  402. /* ... and let's hit the road... */
  403. gic_write_grpen1(1);
  404. }
  405. static int gic_dist_supports_lpis(void)
  406. {
  407. return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS);
  408. }
  409. static void gic_cpu_init(void)
  410. {
  411. void __iomem *rbase;
  412. /* Register ourselves with the rest of the world */
  413. if (gic_populate_rdist())
  414. return;
  415. gic_enable_redist(true);
  416. rbase = gic_data_rdist_sgi_base();
  417. gic_cpu_config(rbase, gic_redist_wait_for_rwp);
  418. /* Give LPIs a spin */
  419. if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
  420. its_cpu_init();
  421. /* initialise system registers */
  422. gic_cpu_sys_reg_init();
  423. }
  424. #ifdef CONFIG_SMP
  425. static int gic_secondary_init(struct notifier_block *nfb,
  426. unsigned long action, void *hcpu)
  427. {
  428. if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
  429. gic_cpu_init();
  430. return NOTIFY_OK;
  431. }
  432. /*
  433. * Notifier for enabling the GIC CPU interface. Set an arbitrarily high
  434. * priority because the GIC needs to be up before the ARM generic timers.
  435. */
  436. static struct notifier_block gic_cpu_notifier = {
  437. .notifier_call = gic_secondary_init,
  438. .priority = 100,
  439. };
  440. static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
  441. unsigned long cluster_id)
  442. {
  443. int cpu = *base_cpu;
  444. unsigned long mpidr = cpu_logical_map(cpu);
  445. u16 tlist = 0;
  446. while (cpu < nr_cpu_ids) {
  447. /*
  448. * If we ever get a cluster of more than 16 CPUs, just
  449. * scream and skip that CPU.
  450. */
  451. if (WARN_ON((mpidr & 0xff) >= 16))
  452. goto out;
  453. tlist |= 1 << (mpidr & 0xf);
  454. cpu = cpumask_next(cpu, mask);
  455. if (cpu >= nr_cpu_ids)
  456. goto out;
  457. mpidr = cpu_logical_map(cpu);
  458. if (cluster_id != (mpidr & ~0xffUL)) {
  459. cpu--;
  460. goto out;
  461. }
  462. }
  463. out:
  464. *base_cpu = cpu;
  465. return tlist;
  466. }
  467. #define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
  468. (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
  469. << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
  470. static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
  471. {
  472. u64 val;
  473. val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) |
  474. MPIDR_TO_SGI_AFFINITY(cluster_id, 2) |
  475. irq << ICC_SGI1R_SGI_ID_SHIFT |
  476. MPIDR_TO_SGI_AFFINITY(cluster_id, 1) |
  477. tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
  478. pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
  479. gic_write_sgi1r(val);
  480. }
  481. static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  482. {
  483. int cpu;
  484. if (WARN_ON(irq >= 16))
  485. return;
  486. /*
  487. * Ensure that stores to Normal memory are visible to the
  488. * other CPUs before issuing the IPI.
  489. */
  490. smp_wmb();
  491. for_each_cpu(cpu, mask) {
  492. unsigned long cluster_id = cpu_logical_map(cpu) & ~0xffUL;
  493. u16 tlist;
  494. tlist = gic_compute_target_list(&cpu, mask, cluster_id);
  495. gic_send_sgi(cluster_id, tlist, irq);
  496. }
  497. /* Force the above writes to ICC_SGI1R_EL1 to be executed */
  498. isb();
  499. }
  500. static void gic_smp_init(void)
  501. {
  502. set_smp_cross_call(gic_raise_softirq);
  503. register_cpu_notifier(&gic_cpu_notifier);
  504. }
  505. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  506. bool force)
  507. {
  508. unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
  509. void __iomem *reg;
  510. int enabled;
  511. u64 val;
  512. if (gic_irq_in_rdist(d))
  513. return -EINVAL;
  514. /* If interrupt was enabled, disable it first */
  515. enabled = gic_peek_irq(d, GICD_ISENABLER);
  516. if (enabled)
  517. gic_mask_irq(d);
  518. reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
  519. val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
  520. gic_write_irouter(val, reg);
  521. /*
  522. * If the interrupt was enabled, enabled it again. Otherwise,
  523. * just wait for the distributor to have digested our changes.
  524. */
  525. if (enabled)
  526. gic_unmask_irq(d);
  527. else
  528. gic_dist_wait_for_rwp();
  529. return IRQ_SET_MASK_OK_DONE;
  530. }
  531. #else
  532. #define gic_set_affinity NULL
  533. #define gic_smp_init() do { } while(0)
  534. #endif
  535. #ifdef CONFIG_CPU_PM
  536. static int gic_cpu_pm_notifier(struct notifier_block *self,
  537. unsigned long cmd, void *v)
  538. {
  539. if (cmd == CPU_PM_EXIT) {
  540. gic_enable_redist(true);
  541. gic_cpu_sys_reg_init();
  542. } else if (cmd == CPU_PM_ENTER) {
  543. gic_write_grpen1(0);
  544. gic_enable_redist(false);
  545. }
  546. return NOTIFY_OK;
  547. }
  548. static struct notifier_block gic_cpu_pm_notifier_block = {
  549. .notifier_call = gic_cpu_pm_notifier,
  550. };
  551. static void gic_cpu_pm_init(void)
  552. {
  553. cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
  554. }
  555. #else
  556. static inline void gic_cpu_pm_init(void) { }
  557. #endif /* CONFIG_CPU_PM */
  558. static struct irq_chip gic_chip = {
  559. .name = "GICv3",
  560. .irq_mask = gic_mask_irq,
  561. .irq_unmask = gic_unmask_irq,
  562. .irq_eoi = gic_eoi_irq,
  563. .irq_set_type = gic_set_type,
  564. .irq_set_affinity = gic_set_affinity,
  565. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  566. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  567. .flags = IRQCHIP_SET_TYPE_MASKED,
  568. };
  569. static struct irq_chip gic_eoimode1_chip = {
  570. .name = "GICv3",
  571. .irq_mask = gic_eoimode1_mask_irq,
  572. .irq_unmask = gic_unmask_irq,
  573. .irq_eoi = gic_eoimode1_eoi_irq,
  574. .irq_set_type = gic_set_type,
  575. .irq_set_affinity = gic_set_affinity,
  576. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  577. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  578. .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
  579. .flags = IRQCHIP_SET_TYPE_MASKED,
  580. };
  581. #define GIC_ID_NR (1U << gic_data.rdists.id_bits)
  582. static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
  583. irq_hw_number_t hw)
  584. {
  585. struct irq_chip *chip = &gic_chip;
  586. if (static_key_true(&supports_deactivate))
  587. chip = &gic_eoimode1_chip;
  588. /* SGIs are private to the core kernel */
  589. if (hw < 16)
  590. return -EPERM;
  591. /* Nothing here */
  592. if (hw >= gic_data.irq_nr && hw < 8192)
  593. return -EPERM;
  594. /* Off limits */
  595. if (hw >= GIC_ID_NR)
  596. return -EPERM;
  597. /* PPIs */
  598. if (hw < 32) {
  599. irq_set_percpu_devid(irq);
  600. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  601. handle_percpu_devid_irq, NULL, NULL);
  602. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  603. }
  604. /* SPIs */
  605. if (hw >= 32 && hw < gic_data.irq_nr) {
  606. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  607. handle_fasteoi_irq, NULL, NULL);
  608. irq_set_probe(irq);
  609. }
  610. /* LPIs */
  611. if (hw >= 8192 && hw < GIC_ID_NR) {
  612. if (!gic_dist_supports_lpis())
  613. return -EPERM;
  614. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  615. handle_fasteoi_irq, NULL, NULL);
  616. }
  617. return 0;
  618. }
  619. static int gic_irq_domain_translate(struct irq_domain *d,
  620. struct irq_fwspec *fwspec,
  621. unsigned long *hwirq,
  622. unsigned int *type)
  623. {
  624. if (is_of_node(fwspec->fwnode)) {
  625. if (fwspec->param_count < 3)
  626. return -EINVAL;
  627. switch (fwspec->param[0]) {
  628. case 0: /* SPI */
  629. *hwirq = fwspec->param[1] + 32;
  630. break;
  631. case 1: /* PPI */
  632. *hwirq = fwspec->param[1] + 16;
  633. break;
  634. case GIC_IRQ_TYPE_LPI: /* LPI */
  635. *hwirq = fwspec->param[1];
  636. break;
  637. default:
  638. return -EINVAL;
  639. }
  640. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  641. return 0;
  642. }
  643. if (is_fwnode_irqchip(fwspec->fwnode)) {
  644. if(fwspec->param_count != 2)
  645. return -EINVAL;
  646. *hwirq = fwspec->param[0];
  647. *type = fwspec->param[1];
  648. return 0;
  649. }
  650. return -EINVAL;
  651. }
  652. static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  653. unsigned int nr_irqs, void *arg)
  654. {
  655. int i, ret;
  656. irq_hw_number_t hwirq;
  657. unsigned int type = IRQ_TYPE_NONE;
  658. struct irq_fwspec *fwspec = arg;
  659. ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
  660. if (ret)
  661. return ret;
  662. for (i = 0; i < nr_irqs; i++)
  663. gic_irq_domain_map(domain, virq + i, hwirq + i);
  664. return 0;
  665. }
  666. static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  667. unsigned int nr_irqs)
  668. {
  669. int i;
  670. for (i = 0; i < nr_irqs; i++) {
  671. struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  672. irq_set_handler(virq + i, NULL);
  673. irq_domain_reset_irq_data(d);
  674. }
  675. }
  676. static const struct irq_domain_ops gic_irq_domain_ops = {
  677. .translate = gic_irq_domain_translate,
  678. .alloc = gic_irq_domain_alloc,
  679. .free = gic_irq_domain_free,
  680. };
  681. static void gicv3_enable_quirks(void)
  682. {
  683. #ifdef CONFIG_ARM64
  684. if (cpus_have_cap(ARM64_WORKAROUND_CAVIUM_23154))
  685. static_branch_enable(&is_cavium_thunderx);
  686. #endif
  687. }
  688. static int __init gic_init_bases(void __iomem *dist_base,
  689. struct redist_region *rdist_regs,
  690. u32 nr_redist_regions,
  691. u64 redist_stride,
  692. struct fwnode_handle *handle)
  693. {
  694. struct device_node *node;
  695. u32 typer;
  696. int gic_irqs;
  697. int err;
  698. if (!is_hyp_mode_available())
  699. static_key_slow_dec(&supports_deactivate);
  700. if (static_key_true(&supports_deactivate))
  701. pr_info("GIC: Using split EOI/Deactivate mode\n");
  702. gic_data.dist_base = dist_base;
  703. gic_data.redist_regions = rdist_regs;
  704. gic_data.nr_redist_regions = nr_redist_regions;
  705. gic_data.redist_stride = redist_stride;
  706. gicv3_enable_quirks();
  707. /*
  708. * Find out how many interrupts are supported.
  709. * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)
  710. */
  711. typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
  712. gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer);
  713. gic_irqs = GICD_TYPER_IRQS(typer);
  714. if (gic_irqs > 1020)
  715. gic_irqs = 1020;
  716. gic_data.irq_nr = gic_irqs;
  717. gic_data.domain = irq_domain_create_tree(handle, &gic_irq_domain_ops,
  718. &gic_data);
  719. gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
  720. if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
  721. err = -ENOMEM;
  722. goto out_free;
  723. }
  724. set_handle_irq(gic_handle_irq);
  725. node = to_of_node(handle);
  726. if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis() &&
  727. node) /* Temp hack to prevent ITS init for ACPI */
  728. its_init(node, &gic_data.rdists, gic_data.domain);
  729. gic_smp_init();
  730. gic_dist_init();
  731. gic_cpu_init();
  732. gic_cpu_pm_init();
  733. return 0;
  734. out_free:
  735. if (gic_data.domain)
  736. irq_domain_remove(gic_data.domain);
  737. free_percpu(gic_data.rdists.rdist);
  738. return err;
  739. }
  740. static int __init gic_validate_dist_version(void __iomem *dist_base)
  741. {
  742. u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  743. if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4)
  744. return -ENODEV;
  745. return 0;
  746. }
  747. static int __init gic_of_init(struct device_node *node, struct device_node *parent)
  748. {
  749. void __iomem *dist_base;
  750. struct redist_region *rdist_regs;
  751. u64 redist_stride;
  752. u32 nr_redist_regions;
  753. int err, i;
  754. dist_base = of_iomap(node, 0);
  755. if (!dist_base) {
  756. pr_err("%s: unable to map gic dist registers\n",
  757. node->full_name);
  758. return -ENXIO;
  759. }
  760. err = gic_validate_dist_version(dist_base);
  761. if (err) {
  762. pr_err("%s: no distributor detected, giving up\n",
  763. node->full_name);
  764. goto out_unmap_dist;
  765. }
  766. if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions))
  767. nr_redist_regions = 1;
  768. rdist_regs = kzalloc(sizeof(*rdist_regs) * nr_redist_regions, GFP_KERNEL);
  769. if (!rdist_regs) {
  770. err = -ENOMEM;
  771. goto out_unmap_dist;
  772. }
  773. for (i = 0; i < nr_redist_regions; i++) {
  774. struct resource res;
  775. int ret;
  776. ret = of_address_to_resource(node, 1 + i, &res);
  777. rdist_regs[i].redist_base = of_iomap(node, 1 + i);
  778. if (ret || !rdist_regs[i].redist_base) {
  779. pr_err("%s: couldn't map region %d\n",
  780. node->full_name, i);
  781. err = -ENODEV;
  782. goto out_unmap_rdist;
  783. }
  784. rdist_regs[i].phys_base = res.start;
  785. }
  786. if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
  787. redist_stride = 0;
  788. err = gic_init_bases(dist_base, rdist_regs, nr_redist_regions,
  789. redist_stride, &node->fwnode);
  790. if (!err)
  791. return 0;
  792. out_unmap_rdist:
  793. for (i = 0; i < nr_redist_regions; i++)
  794. if (rdist_regs[i].redist_base)
  795. iounmap(rdist_regs[i].redist_base);
  796. kfree(rdist_regs);
  797. out_unmap_dist:
  798. iounmap(dist_base);
  799. return err;
  800. }
  801. IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);
  802. #ifdef CONFIG_ACPI
  803. static void __iomem *dist_base;
  804. static struct redist_region *redist_regs __initdata;
  805. static u32 nr_redist_regions __initdata;
  806. static bool single_redist;
  807. static void __init
  808. gic_acpi_register_redist(phys_addr_t phys_base, void __iomem *redist_base)
  809. {
  810. static int count = 0;
  811. redist_regs[count].phys_base = phys_base;
  812. redist_regs[count].redist_base = redist_base;
  813. redist_regs[count].single_redist = single_redist;
  814. count++;
  815. }
  816. static int __init
  817. gic_acpi_parse_madt_redist(struct acpi_subtable_header *header,
  818. const unsigned long end)
  819. {
  820. struct acpi_madt_generic_redistributor *redist =
  821. (struct acpi_madt_generic_redistributor *)header;
  822. void __iomem *redist_base;
  823. redist_base = ioremap(redist->base_address, redist->length);
  824. if (!redist_base) {
  825. pr_err("Couldn't map GICR region @%llx\n", redist->base_address);
  826. return -ENOMEM;
  827. }
  828. gic_acpi_register_redist(redist->base_address, redist_base);
  829. return 0;
  830. }
  831. static int __init
  832. gic_acpi_parse_madt_gicc(struct acpi_subtable_header *header,
  833. const unsigned long end)
  834. {
  835. struct acpi_madt_generic_interrupt *gicc =
  836. (struct acpi_madt_generic_interrupt *)header;
  837. u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  838. u32 size = reg == GIC_PIDR2_ARCH_GICv4 ? SZ_64K * 4 : SZ_64K * 2;
  839. void __iomem *redist_base;
  840. redist_base = ioremap(gicc->gicr_base_address, size);
  841. if (!redist_base)
  842. return -ENOMEM;
  843. gic_acpi_register_redist(gicc->gicr_base_address, redist_base);
  844. return 0;
  845. }
  846. static int __init gic_acpi_collect_gicr_base(void)
  847. {
  848. acpi_tbl_entry_handler redist_parser;
  849. enum acpi_madt_type type;
  850. if (single_redist) {
  851. type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
  852. redist_parser = gic_acpi_parse_madt_gicc;
  853. } else {
  854. type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
  855. redist_parser = gic_acpi_parse_madt_redist;
  856. }
  857. /* Collect redistributor base addresses in GICR entries */
  858. if (acpi_table_parse_madt(type, redist_parser, 0) > 0)
  859. return 0;
  860. pr_info("No valid GICR entries exist\n");
  861. return -ENODEV;
  862. }
  863. static int __init gic_acpi_match_gicr(struct acpi_subtable_header *header,
  864. const unsigned long end)
  865. {
  866. /* Subtable presence means that redist exists, that's it */
  867. return 0;
  868. }
  869. static int __init gic_acpi_match_gicc(struct acpi_subtable_header *header,
  870. const unsigned long end)
  871. {
  872. struct acpi_madt_generic_interrupt *gicc =
  873. (struct acpi_madt_generic_interrupt *)header;
  874. /*
  875. * If GICC is enabled and has valid gicr base address, then it means
  876. * GICR base is presented via GICC
  877. */
  878. if ((gicc->flags & ACPI_MADT_ENABLED) && gicc->gicr_base_address)
  879. return 0;
  880. return -ENODEV;
  881. }
  882. static int __init gic_acpi_count_gicr_regions(void)
  883. {
  884. int count;
  885. /*
  886. * Count how many redistributor regions we have. It is not allowed
  887. * to mix redistributor description, GICR and GICC subtables have to be
  888. * mutually exclusive.
  889. */
  890. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
  891. gic_acpi_match_gicr, 0);
  892. if (count > 0) {
  893. single_redist = false;
  894. return count;
  895. }
  896. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  897. gic_acpi_match_gicc, 0);
  898. if (count > 0)
  899. single_redist = true;
  900. return count;
  901. }
  902. static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header,
  903. struct acpi_probe_entry *ape)
  904. {
  905. struct acpi_madt_generic_distributor *dist;
  906. int count;
  907. dist = (struct acpi_madt_generic_distributor *)header;
  908. if (dist->version != ape->driver_data)
  909. return false;
  910. /* We need to do that exercise anyway, the sooner the better */
  911. count = gic_acpi_count_gicr_regions();
  912. if (count <= 0)
  913. return false;
  914. nr_redist_regions = count;
  915. return true;
  916. }
  917. #define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K)
  918. static int __init
  919. gic_acpi_init(struct acpi_subtable_header *header, const unsigned long end)
  920. {
  921. struct acpi_madt_generic_distributor *dist;
  922. struct fwnode_handle *domain_handle;
  923. int i, err;
  924. /* Get distributor base address */
  925. dist = (struct acpi_madt_generic_distributor *)header;
  926. dist_base = ioremap(dist->base_address, ACPI_GICV3_DIST_MEM_SIZE);
  927. if (!dist_base) {
  928. pr_err("Unable to map GICD registers\n");
  929. return -ENOMEM;
  930. }
  931. err = gic_validate_dist_version(dist_base);
  932. if (err) {
  933. pr_err("No distributor detected at @%p, giving up", dist_base);
  934. goto out_dist_unmap;
  935. }
  936. redist_regs = kzalloc(sizeof(*redist_regs) * nr_redist_regions,
  937. GFP_KERNEL);
  938. if (!redist_regs) {
  939. err = -ENOMEM;
  940. goto out_dist_unmap;
  941. }
  942. err = gic_acpi_collect_gicr_base();
  943. if (err)
  944. goto out_redist_unmap;
  945. domain_handle = irq_domain_alloc_fwnode(dist_base);
  946. if (!domain_handle) {
  947. err = -ENOMEM;
  948. goto out_redist_unmap;
  949. }
  950. err = gic_init_bases(dist_base, redist_regs, nr_redist_regions, 0,
  951. domain_handle);
  952. if (err)
  953. goto out_fwhandle_free;
  954. acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, domain_handle);
  955. return 0;
  956. out_fwhandle_free:
  957. irq_domain_free_fwnode(domain_handle);
  958. out_redist_unmap:
  959. for (i = 0; i < nr_redist_regions; i++)
  960. if (redist_regs[i].redist_base)
  961. iounmap(redist_regs[i].redist_base);
  962. kfree(redist_regs);
  963. out_dist_unmap:
  964. iounmap(dist_base);
  965. return err;
  966. }
  967. IRQCHIP_ACPI_DECLARE(gic_v3, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  968. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V3,
  969. gic_acpi_init);
  970. IRQCHIP_ACPI_DECLARE(gic_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  971. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V4,
  972. gic_acpi_init);
  973. IRQCHIP_ACPI_DECLARE(gic_v3_or_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  974. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_NONE,
  975. gic_acpi_init);
  976. #endif