irq-gic-v3.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. /*
  2. * Copyright (C) 2013-2017 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. #define pr_fmt(fmt) "GICv3: " fmt
  18. #include <linux/acpi.h>
  19. #include <linux/cpu.h>
  20. #include <linux/cpu_pm.h>
  21. #include <linux/delay.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irqdomain.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/percpu.h>
  28. #include <linux/slab.h>
  29. #include <linux/irqchip.h>
  30. #include <linux/irqchip/arm-gic-common.h>
  31. #include <linux/irqchip/arm-gic-v3.h>
  32. #include <linux/irqchip/irq-partition-percpu.h>
  33. #include <asm/cputype.h>
  34. #include <asm/exception.h>
  35. #include <asm/smp_plat.h>
  36. #include <asm/virt.h>
  37. #include "irq-gic-common.h"
  38. struct redist_region {
  39. void __iomem *redist_base;
  40. phys_addr_t phys_base;
  41. bool single_redist;
  42. };
  43. struct gic_chip_data {
  44. struct fwnode_handle *fwnode;
  45. void __iomem *dist_base;
  46. struct redist_region *redist_regions;
  47. struct rdists rdists;
  48. struct irq_domain *domain;
  49. u64 redist_stride;
  50. u32 nr_redist_regions;
  51. bool has_rss;
  52. unsigned int irq_nr;
  53. struct partition_desc *ppi_descs[16];
  54. };
  55. static struct gic_chip_data gic_data __read_mostly;
  56. static struct static_key supports_deactivate = STATIC_KEY_INIT_TRUE;
  57. static struct gic_kvm_info gic_v3_kvm_info;
  58. static DEFINE_PER_CPU(bool, has_rss);
  59. #define MPIDR_RS(mpidr) (((mpidr) & 0xF0UL) >> 4)
  60. #define gic_data_rdist() (this_cpu_ptr(gic_data.rdists.rdist))
  61. #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
  62. #define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K)
  63. /* Our default, arbitrary priority value. Linux only uses one anyway. */
  64. #define DEFAULT_PMR_VALUE 0xf0
  65. static inline unsigned int gic_irq(struct irq_data *d)
  66. {
  67. return d->hwirq;
  68. }
  69. static inline int gic_irq_in_rdist(struct irq_data *d)
  70. {
  71. return gic_irq(d) < 32;
  72. }
  73. static inline void __iomem *gic_dist_base(struct irq_data *d)
  74. {
  75. if (gic_irq_in_rdist(d)) /* SGI+PPI -> SGI_base for this CPU */
  76. return gic_data_rdist_sgi_base();
  77. if (d->hwirq <= 1023) /* SPI -> dist_base */
  78. return gic_data.dist_base;
  79. return NULL;
  80. }
  81. static void gic_do_wait_for_rwp(void __iomem *base)
  82. {
  83. u32 count = 1000000; /* 1s! */
  84. while (readl_relaxed(base + GICD_CTLR) & GICD_CTLR_RWP) {
  85. count--;
  86. if (!count) {
  87. pr_err_ratelimited("RWP timeout, gone fishing\n");
  88. return;
  89. }
  90. cpu_relax();
  91. udelay(1);
  92. };
  93. }
  94. /* Wait for completion of a distributor change */
  95. static void gic_dist_wait_for_rwp(void)
  96. {
  97. gic_do_wait_for_rwp(gic_data.dist_base);
  98. }
  99. /* Wait for completion of a redistributor change */
  100. static void gic_redist_wait_for_rwp(void)
  101. {
  102. gic_do_wait_for_rwp(gic_data_rdist_rd_base());
  103. }
  104. #ifdef CONFIG_ARM64
  105. static u64 __maybe_unused gic_read_iar(void)
  106. {
  107. if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_23154))
  108. return gic_read_iar_cavium_thunderx();
  109. else
  110. return gic_read_iar_common();
  111. }
  112. #endif
  113. static void gic_enable_redist(bool enable)
  114. {
  115. void __iomem *rbase;
  116. u32 count = 1000000; /* 1s! */
  117. u32 val;
  118. rbase = gic_data_rdist_rd_base();
  119. val = readl_relaxed(rbase + GICR_WAKER);
  120. if (enable)
  121. /* Wake up this CPU redistributor */
  122. val &= ~GICR_WAKER_ProcessorSleep;
  123. else
  124. val |= GICR_WAKER_ProcessorSleep;
  125. writel_relaxed(val, rbase + GICR_WAKER);
  126. if (!enable) { /* Check that GICR_WAKER is writeable */
  127. val = readl_relaxed(rbase + GICR_WAKER);
  128. if (!(val & GICR_WAKER_ProcessorSleep))
  129. return; /* No PM support in this redistributor */
  130. }
  131. while (--count) {
  132. val = readl_relaxed(rbase + GICR_WAKER);
  133. if (enable ^ (bool)(val & GICR_WAKER_ChildrenAsleep))
  134. break;
  135. cpu_relax();
  136. udelay(1);
  137. };
  138. if (!count)
  139. pr_err_ratelimited("redistributor failed to %s...\n",
  140. enable ? "wakeup" : "sleep");
  141. }
  142. /*
  143. * Routines to disable, enable, EOI and route interrupts
  144. */
  145. static int gic_peek_irq(struct irq_data *d, u32 offset)
  146. {
  147. u32 mask = 1 << (gic_irq(d) % 32);
  148. void __iomem *base;
  149. if (gic_irq_in_rdist(d))
  150. base = gic_data_rdist_sgi_base();
  151. else
  152. base = gic_data.dist_base;
  153. return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask);
  154. }
  155. static void gic_poke_irq(struct irq_data *d, u32 offset)
  156. {
  157. u32 mask = 1 << (gic_irq(d) % 32);
  158. void (*rwp_wait)(void);
  159. void __iomem *base;
  160. if (gic_irq_in_rdist(d)) {
  161. base = gic_data_rdist_sgi_base();
  162. rwp_wait = gic_redist_wait_for_rwp;
  163. } else {
  164. base = gic_data.dist_base;
  165. rwp_wait = gic_dist_wait_for_rwp;
  166. }
  167. writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4);
  168. rwp_wait();
  169. }
  170. static void gic_mask_irq(struct irq_data *d)
  171. {
  172. gic_poke_irq(d, GICD_ICENABLER);
  173. }
  174. static void gic_eoimode1_mask_irq(struct irq_data *d)
  175. {
  176. gic_mask_irq(d);
  177. /*
  178. * When masking a forwarded interrupt, make sure it is
  179. * deactivated as well.
  180. *
  181. * This ensures that an interrupt that is getting
  182. * disabled/masked will not get "stuck", because there is
  183. * noone to deactivate it (guest is being terminated).
  184. */
  185. if (irqd_is_forwarded_to_vcpu(d))
  186. gic_poke_irq(d, GICD_ICACTIVER);
  187. }
  188. static void gic_unmask_irq(struct irq_data *d)
  189. {
  190. gic_poke_irq(d, GICD_ISENABLER);
  191. }
  192. static int gic_irq_set_irqchip_state(struct irq_data *d,
  193. enum irqchip_irq_state which, bool val)
  194. {
  195. u32 reg;
  196. if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
  197. return -EINVAL;
  198. switch (which) {
  199. case IRQCHIP_STATE_PENDING:
  200. reg = val ? GICD_ISPENDR : GICD_ICPENDR;
  201. break;
  202. case IRQCHIP_STATE_ACTIVE:
  203. reg = val ? GICD_ISACTIVER : GICD_ICACTIVER;
  204. break;
  205. case IRQCHIP_STATE_MASKED:
  206. reg = val ? GICD_ICENABLER : GICD_ISENABLER;
  207. break;
  208. default:
  209. return -EINVAL;
  210. }
  211. gic_poke_irq(d, reg);
  212. return 0;
  213. }
  214. static int gic_irq_get_irqchip_state(struct irq_data *d,
  215. enum irqchip_irq_state which, bool *val)
  216. {
  217. if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
  218. return -EINVAL;
  219. switch (which) {
  220. case IRQCHIP_STATE_PENDING:
  221. *val = gic_peek_irq(d, GICD_ISPENDR);
  222. break;
  223. case IRQCHIP_STATE_ACTIVE:
  224. *val = gic_peek_irq(d, GICD_ISACTIVER);
  225. break;
  226. case IRQCHIP_STATE_MASKED:
  227. *val = !gic_peek_irq(d, GICD_ISENABLER);
  228. break;
  229. default:
  230. return -EINVAL;
  231. }
  232. return 0;
  233. }
  234. static void gic_eoi_irq(struct irq_data *d)
  235. {
  236. gic_write_eoir(gic_irq(d));
  237. }
  238. static void gic_eoimode1_eoi_irq(struct irq_data *d)
  239. {
  240. /*
  241. * No need to deactivate an LPI, or an interrupt that
  242. * is is getting forwarded to a vcpu.
  243. */
  244. if (gic_irq(d) >= 8192 || irqd_is_forwarded_to_vcpu(d))
  245. return;
  246. gic_write_dir(gic_irq(d));
  247. }
  248. static int gic_set_type(struct irq_data *d, unsigned int type)
  249. {
  250. unsigned int irq = gic_irq(d);
  251. void (*rwp_wait)(void);
  252. void __iomem *base;
  253. /* Interrupt configuration for SGIs can't be changed */
  254. if (irq < 16)
  255. return -EINVAL;
  256. /* SPIs have restrictions on the supported types */
  257. if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
  258. type != IRQ_TYPE_EDGE_RISING)
  259. return -EINVAL;
  260. if (gic_irq_in_rdist(d)) {
  261. base = gic_data_rdist_sgi_base();
  262. rwp_wait = gic_redist_wait_for_rwp;
  263. } else {
  264. base = gic_data.dist_base;
  265. rwp_wait = gic_dist_wait_for_rwp;
  266. }
  267. return gic_configure_irq(irq, type, base, rwp_wait);
  268. }
  269. static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
  270. {
  271. if (vcpu)
  272. irqd_set_forwarded_to_vcpu(d);
  273. else
  274. irqd_clr_forwarded_to_vcpu(d);
  275. return 0;
  276. }
  277. static u64 gic_mpidr_to_affinity(unsigned long mpidr)
  278. {
  279. u64 aff;
  280. aff = ((u64)MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |
  281. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  282. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  283. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  284. return aff;
  285. }
  286. static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
  287. {
  288. u32 irqnr;
  289. do {
  290. irqnr = gic_read_iar();
  291. if (likely(irqnr > 15 && irqnr < 1020) || irqnr >= 8192) {
  292. int err;
  293. if (static_key_true(&supports_deactivate))
  294. gic_write_eoir(irqnr);
  295. else
  296. isb();
  297. err = handle_domain_irq(gic_data.domain, irqnr, regs);
  298. if (err) {
  299. WARN_ONCE(true, "Unexpected interrupt received!\n");
  300. if (static_key_true(&supports_deactivate)) {
  301. if (irqnr < 8192)
  302. gic_write_dir(irqnr);
  303. } else {
  304. gic_write_eoir(irqnr);
  305. }
  306. }
  307. continue;
  308. }
  309. if (irqnr < 16) {
  310. gic_write_eoir(irqnr);
  311. if (static_key_true(&supports_deactivate))
  312. gic_write_dir(irqnr);
  313. #ifdef CONFIG_SMP
  314. /*
  315. * Unlike GICv2, we don't need an smp_rmb() here.
  316. * The control dependency from gic_read_iar to
  317. * the ISB in gic_write_eoir is enough to ensure
  318. * that any shared data read by handle_IPI will
  319. * be read after the ACK.
  320. */
  321. handle_IPI(irqnr, regs);
  322. #else
  323. WARN_ONCE(true, "Unexpected SGI received!\n");
  324. #endif
  325. continue;
  326. }
  327. } while (irqnr != ICC_IAR1_EL1_SPURIOUS);
  328. }
  329. static void __init gic_dist_init(void)
  330. {
  331. unsigned int i;
  332. u64 affinity;
  333. void __iomem *base = gic_data.dist_base;
  334. /* Disable the distributor */
  335. writel_relaxed(0, base + GICD_CTLR);
  336. gic_dist_wait_for_rwp();
  337. /*
  338. * Configure SPIs as non-secure Group-1. This will only matter
  339. * if the GIC only has a single security state. This will not
  340. * do the right thing if the kernel is running in secure mode,
  341. * but that's not the intended use case anyway.
  342. */
  343. for (i = 32; i < gic_data.irq_nr; i += 32)
  344. writel_relaxed(~0, base + GICD_IGROUPR + i / 8);
  345. gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp);
  346. /* Enable distributor with ARE, Group1 */
  347. writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1,
  348. base + GICD_CTLR);
  349. /*
  350. * Set all global interrupts to the boot CPU only. ARE must be
  351. * enabled.
  352. */
  353. affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id()));
  354. for (i = 32; i < gic_data.irq_nr; i++)
  355. gic_write_irouter(affinity, base + GICD_IROUTER + i * 8);
  356. }
  357. static int gic_iterate_rdists(int (*fn)(struct redist_region *, void __iomem *))
  358. {
  359. int ret = -ENODEV;
  360. int i;
  361. for (i = 0; i < gic_data.nr_redist_regions; i++) {
  362. void __iomem *ptr = gic_data.redist_regions[i].redist_base;
  363. u64 typer;
  364. u32 reg;
  365. reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;
  366. if (reg != GIC_PIDR2_ARCH_GICv3 &&
  367. reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */
  368. pr_warn("No redistributor present @%p\n", ptr);
  369. break;
  370. }
  371. do {
  372. typer = gic_read_typer(ptr + GICR_TYPER);
  373. ret = fn(gic_data.redist_regions + i, ptr);
  374. if (!ret)
  375. return 0;
  376. if (gic_data.redist_regions[i].single_redist)
  377. break;
  378. if (gic_data.redist_stride) {
  379. ptr += gic_data.redist_stride;
  380. } else {
  381. ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
  382. if (typer & GICR_TYPER_VLPIS)
  383. ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
  384. }
  385. } while (!(typer & GICR_TYPER_LAST));
  386. }
  387. return ret ? -ENODEV : 0;
  388. }
  389. static int __gic_populate_rdist(struct redist_region *region, void __iomem *ptr)
  390. {
  391. unsigned long mpidr = cpu_logical_map(smp_processor_id());
  392. u64 typer;
  393. u32 aff;
  394. /*
  395. * Convert affinity to a 32bit value that can be matched to
  396. * GICR_TYPER bits [63:32].
  397. */
  398. aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |
  399. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  400. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  401. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  402. typer = gic_read_typer(ptr + GICR_TYPER);
  403. if ((typer >> 32) == aff) {
  404. u64 offset = ptr - region->redist_base;
  405. gic_data_rdist_rd_base() = ptr;
  406. gic_data_rdist()->phys_base = region->phys_base + offset;
  407. pr_info("CPU%d: found redistributor %lx region %d:%pa\n",
  408. smp_processor_id(), mpidr,
  409. (int)(region - gic_data.redist_regions),
  410. &gic_data_rdist()->phys_base);
  411. return 0;
  412. }
  413. /* Try next one */
  414. return 1;
  415. }
  416. static int gic_populate_rdist(void)
  417. {
  418. if (gic_iterate_rdists(__gic_populate_rdist) == 0)
  419. return 0;
  420. /* We couldn't even deal with ourselves... */
  421. WARN(true, "CPU%d: mpidr %lx has no re-distributor!\n",
  422. smp_processor_id(),
  423. (unsigned long)cpu_logical_map(smp_processor_id()));
  424. return -ENODEV;
  425. }
  426. static int __gic_update_vlpi_properties(struct redist_region *region,
  427. void __iomem *ptr)
  428. {
  429. u64 typer = gic_read_typer(ptr + GICR_TYPER);
  430. gic_data.rdists.has_vlpis &= !!(typer & GICR_TYPER_VLPIS);
  431. gic_data.rdists.has_direct_lpi &= !!(typer & GICR_TYPER_DirectLPIS);
  432. return 1;
  433. }
  434. static void gic_update_vlpi_properties(void)
  435. {
  436. gic_iterate_rdists(__gic_update_vlpi_properties);
  437. pr_info("%sVLPI support, %sdirect LPI support\n",
  438. !gic_data.rdists.has_vlpis ? "no " : "",
  439. !gic_data.rdists.has_direct_lpi ? "no " : "");
  440. }
  441. static void gic_cpu_sys_reg_init(void)
  442. {
  443. int i, cpu = smp_processor_id();
  444. u64 mpidr = cpu_logical_map(cpu);
  445. u64 need_rss = MPIDR_RS(mpidr);
  446. /*
  447. * Need to check that the SRE bit has actually been set. If
  448. * not, it means that SRE is disabled at EL2. We're going to
  449. * die painfully, and there is nothing we can do about it.
  450. *
  451. * Kindly inform the luser.
  452. */
  453. if (!gic_enable_sre())
  454. pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
  455. /* Set priority mask register */
  456. gic_write_pmr(DEFAULT_PMR_VALUE);
  457. /*
  458. * Some firmwares hand over to the kernel with the BPR changed from
  459. * its reset value (and with a value large enough to prevent
  460. * any pre-emptive interrupts from working at all). Writing a zero
  461. * to BPR restores is reset value.
  462. */
  463. gic_write_bpr1(0);
  464. if (static_key_true(&supports_deactivate)) {
  465. /* EOI drops priority only (mode 1) */
  466. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
  467. } else {
  468. /* EOI deactivates interrupt too (mode 0) */
  469. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
  470. }
  471. /* ... and let's hit the road... */
  472. gic_write_grpen1(1);
  473. /* Keep the RSS capability status in per_cpu variable */
  474. per_cpu(has_rss, cpu) = !!(gic_read_ctlr() & ICC_CTLR_EL1_RSS);
  475. /* Check all the CPUs have capable of sending SGIs to other CPUs */
  476. for_each_online_cpu(i) {
  477. bool have_rss = per_cpu(has_rss, i) && per_cpu(has_rss, cpu);
  478. need_rss |= MPIDR_RS(cpu_logical_map(i));
  479. if (need_rss && (!have_rss))
  480. pr_crit("CPU%d (%lx) can't SGI CPU%d (%lx), no RSS\n",
  481. cpu, (unsigned long)mpidr,
  482. i, (unsigned long)cpu_logical_map(i));
  483. }
  484. /**
  485. * GIC spec says, when ICC_CTLR_EL1.RSS==1 and GICD_TYPER.RSS==0,
  486. * writing ICC_ASGI1R_EL1 register with RS != 0 is a CONSTRAINED
  487. * UNPREDICTABLE choice of :
  488. * - The write is ignored.
  489. * - The RS field is treated as 0.
  490. */
  491. if (need_rss && (!gic_data.has_rss))
  492. pr_crit_once("RSS is required but GICD doesn't support it\n");
  493. }
  494. static int gic_dist_supports_lpis(void)
  495. {
  496. return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS);
  497. }
  498. static void gic_cpu_init(void)
  499. {
  500. void __iomem *rbase;
  501. /* Register ourselves with the rest of the world */
  502. if (gic_populate_rdist())
  503. return;
  504. gic_enable_redist(true);
  505. rbase = gic_data_rdist_sgi_base();
  506. /* Configure SGIs/PPIs as non-secure Group-1 */
  507. writel_relaxed(~0, rbase + GICR_IGROUPR0);
  508. gic_cpu_config(rbase, gic_redist_wait_for_rwp);
  509. /* Give LPIs a spin */
  510. if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
  511. its_cpu_init();
  512. /* initialise system registers */
  513. gic_cpu_sys_reg_init();
  514. }
  515. #ifdef CONFIG_SMP
  516. #define MPIDR_TO_SGI_RS(mpidr) (MPIDR_RS(mpidr) << ICC_SGI1R_RS_SHIFT)
  517. #define MPIDR_TO_SGI_CLUSTER_ID(mpidr) ((mpidr) & ~0xFUL)
  518. static int gic_starting_cpu(unsigned int cpu)
  519. {
  520. gic_cpu_init();
  521. return 0;
  522. }
  523. static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
  524. unsigned long cluster_id)
  525. {
  526. int next_cpu, cpu = *base_cpu;
  527. unsigned long mpidr = cpu_logical_map(cpu);
  528. u16 tlist = 0;
  529. while (cpu < nr_cpu_ids) {
  530. tlist |= 1 << (mpidr & 0xf);
  531. next_cpu = cpumask_next(cpu, mask);
  532. if (next_cpu >= nr_cpu_ids)
  533. goto out;
  534. cpu = next_cpu;
  535. mpidr = cpu_logical_map(cpu);
  536. if (cluster_id != MPIDR_TO_SGI_CLUSTER_ID(mpidr)) {
  537. cpu--;
  538. goto out;
  539. }
  540. }
  541. out:
  542. *base_cpu = cpu;
  543. return tlist;
  544. }
  545. #define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
  546. (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
  547. << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
  548. static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
  549. {
  550. u64 val;
  551. val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) |
  552. MPIDR_TO_SGI_AFFINITY(cluster_id, 2) |
  553. irq << ICC_SGI1R_SGI_ID_SHIFT |
  554. MPIDR_TO_SGI_AFFINITY(cluster_id, 1) |
  555. MPIDR_TO_SGI_RS(cluster_id) |
  556. tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
  557. pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
  558. gic_write_sgi1r(val);
  559. }
  560. static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  561. {
  562. int cpu;
  563. if (WARN_ON(irq >= 16))
  564. return;
  565. /*
  566. * Ensure that stores to Normal memory are visible to the
  567. * other CPUs before issuing the IPI.
  568. */
  569. smp_wmb();
  570. for_each_cpu(cpu, mask) {
  571. u64 cluster_id = MPIDR_TO_SGI_CLUSTER_ID(cpu_logical_map(cpu));
  572. u16 tlist;
  573. tlist = gic_compute_target_list(&cpu, mask, cluster_id);
  574. gic_send_sgi(cluster_id, tlist, irq);
  575. }
  576. /* Force the above writes to ICC_SGI1R_EL1 to be executed */
  577. isb();
  578. }
  579. static void gic_smp_init(void)
  580. {
  581. set_smp_cross_call(gic_raise_softirq);
  582. cpuhp_setup_state_nocalls(CPUHP_AP_IRQ_GIC_STARTING,
  583. "irqchip/arm/gicv3:starting",
  584. gic_starting_cpu, NULL);
  585. }
  586. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  587. bool force)
  588. {
  589. unsigned int cpu;
  590. void __iomem *reg;
  591. int enabled;
  592. u64 val;
  593. if (force)
  594. cpu = cpumask_first(mask_val);
  595. else
  596. cpu = cpumask_any_and(mask_val, cpu_online_mask);
  597. if (cpu >= nr_cpu_ids)
  598. return -EINVAL;
  599. if (gic_irq_in_rdist(d))
  600. return -EINVAL;
  601. /* If interrupt was enabled, disable it first */
  602. enabled = gic_peek_irq(d, GICD_ISENABLER);
  603. if (enabled)
  604. gic_mask_irq(d);
  605. reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
  606. val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
  607. gic_write_irouter(val, reg);
  608. /*
  609. * If the interrupt was enabled, enabled it again. Otherwise,
  610. * just wait for the distributor to have digested our changes.
  611. */
  612. if (enabled)
  613. gic_unmask_irq(d);
  614. else
  615. gic_dist_wait_for_rwp();
  616. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  617. return IRQ_SET_MASK_OK_DONE;
  618. }
  619. #else
  620. #define gic_set_affinity NULL
  621. #define gic_smp_init() do { } while(0)
  622. #endif
  623. #ifdef CONFIG_CPU_PM
  624. /* Check whether it's single security state view */
  625. static bool gic_dist_security_disabled(void)
  626. {
  627. return readl_relaxed(gic_data.dist_base + GICD_CTLR) & GICD_CTLR_DS;
  628. }
  629. static int gic_cpu_pm_notifier(struct notifier_block *self,
  630. unsigned long cmd, void *v)
  631. {
  632. if (cmd == CPU_PM_EXIT) {
  633. if (gic_dist_security_disabled())
  634. gic_enable_redist(true);
  635. gic_cpu_sys_reg_init();
  636. } else if (cmd == CPU_PM_ENTER && gic_dist_security_disabled()) {
  637. gic_write_grpen1(0);
  638. gic_enable_redist(false);
  639. }
  640. return NOTIFY_OK;
  641. }
  642. static struct notifier_block gic_cpu_pm_notifier_block = {
  643. .notifier_call = gic_cpu_pm_notifier,
  644. };
  645. static void gic_cpu_pm_init(void)
  646. {
  647. cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
  648. }
  649. #else
  650. static inline void gic_cpu_pm_init(void) { }
  651. #endif /* CONFIG_CPU_PM */
  652. static struct irq_chip gic_chip = {
  653. .name = "GICv3",
  654. .irq_mask = gic_mask_irq,
  655. .irq_unmask = gic_unmask_irq,
  656. .irq_eoi = gic_eoi_irq,
  657. .irq_set_type = gic_set_type,
  658. .irq_set_affinity = gic_set_affinity,
  659. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  660. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  661. .flags = IRQCHIP_SET_TYPE_MASKED,
  662. };
  663. static struct irq_chip gic_eoimode1_chip = {
  664. .name = "GICv3",
  665. .irq_mask = gic_eoimode1_mask_irq,
  666. .irq_unmask = gic_unmask_irq,
  667. .irq_eoi = gic_eoimode1_eoi_irq,
  668. .irq_set_type = gic_set_type,
  669. .irq_set_affinity = gic_set_affinity,
  670. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  671. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  672. .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
  673. .flags = IRQCHIP_SET_TYPE_MASKED,
  674. };
  675. #define GIC_ID_NR (1U << gic_data.rdists.id_bits)
  676. static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
  677. irq_hw_number_t hw)
  678. {
  679. struct irq_chip *chip = &gic_chip;
  680. if (static_key_true(&supports_deactivate))
  681. chip = &gic_eoimode1_chip;
  682. /* SGIs are private to the core kernel */
  683. if (hw < 16)
  684. return -EPERM;
  685. /* Nothing here */
  686. if (hw >= gic_data.irq_nr && hw < 8192)
  687. return -EPERM;
  688. /* Off limits */
  689. if (hw >= GIC_ID_NR)
  690. return -EPERM;
  691. /* PPIs */
  692. if (hw < 32) {
  693. irq_set_percpu_devid(irq);
  694. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  695. handle_percpu_devid_irq, NULL, NULL);
  696. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  697. }
  698. /* SPIs */
  699. if (hw >= 32 && hw < gic_data.irq_nr) {
  700. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  701. handle_fasteoi_irq, NULL, NULL);
  702. irq_set_probe(irq);
  703. irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(irq)));
  704. }
  705. /* LPIs */
  706. if (hw >= 8192 && hw < GIC_ID_NR) {
  707. if (!gic_dist_supports_lpis())
  708. return -EPERM;
  709. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  710. handle_fasteoi_irq, NULL, NULL);
  711. }
  712. return 0;
  713. }
  714. static int gic_irq_domain_translate(struct irq_domain *d,
  715. struct irq_fwspec *fwspec,
  716. unsigned long *hwirq,
  717. unsigned int *type)
  718. {
  719. if (is_of_node(fwspec->fwnode)) {
  720. if (fwspec->param_count < 3)
  721. return -EINVAL;
  722. switch (fwspec->param[0]) {
  723. case 0: /* SPI */
  724. *hwirq = fwspec->param[1] + 32;
  725. break;
  726. case 1: /* PPI */
  727. *hwirq = fwspec->param[1] + 16;
  728. break;
  729. case GIC_IRQ_TYPE_LPI: /* LPI */
  730. *hwirq = fwspec->param[1];
  731. break;
  732. default:
  733. return -EINVAL;
  734. }
  735. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  736. return 0;
  737. }
  738. if (is_fwnode_irqchip(fwspec->fwnode)) {
  739. if(fwspec->param_count != 2)
  740. return -EINVAL;
  741. *hwirq = fwspec->param[0];
  742. *type = fwspec->param[1];
  743. return 0;
  744. }
  745. return -EINVAL;
  746. }
  747. static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  748. unsigned int nr_irqs, void *arg)
  749. {
  750. int i, ret;
  751. irq_hw_number_t hwirq;
  752. unsigned int type = IRQ_TYPE_NONE;
  753. struct irq_fwspec *fwspec = arg;
  754. ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
  755. if (ret)
  756. return ret;
  757. for (i = 0; i < nr_irqs; i++) {
  758. ret = gic_irq_domain_map(domain, virq + i, hwirq + i);
  759. if (ret)
  760. return ret;
  761. }
  762. return 0;
  763. }
  764. static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  765. unsigned int nr_irqs)
  766. {
  767. int i;
  768. for (i = 0; i < nr_irqs; i++) {
  769. struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  770. irq_set_handler(virq + i, NULL);
  771. irq_domain_reset_irq_data(d);
  772. }
  773. }
  774. static int gic_irq_domain_select(struct irq_domain *d,
  775. struct irq_fwspec *fwspec,
  776. enum irq_domain_bus_token bus_token)
  777. {
  778. /* Not for us */
  779. if (fwspec->fwnode != d->fwnode)
  780. return 0;
  781. /* If this is not DT, then we have a single domain */
  782. if (!is_of_node(fwspec->fwnode))
  783. return 1;
  784. /*
  785. * If this is a PPI and we have a 4th (non-null) parameter,
  786. * then we need to match the partition domain.
  787. */
  788. if (fwspec->param_count >= 4 &&
  789. fwspec->param[0] == 1 && fwspec->param[3] != 0)
  790. return d == partition_get_domain(gic_data.ppi_descs[fwspec->param[1]]);
  791. return d == gic_data.domain;
  792. }
  793. static const struct irq_domain_ops gic_irq_domain_ops = {
  794. .translate = gic_irq_domain_translate,
  795. .alloc = gic_irq_domain_alloc,
  796. .free = gic_irq_domain_free,
  797. .select = gic_irq_domain_select,
  798. };
  799. static int partition_domain_translate(struct irq_domain *d,
  800. struct irq_fwspec *fwspec,
  801. unsigned long *hwirq,
  802. unsigned int *type)
  803. {
  804. struct device_node *np;
  805. int ret;
  806. np = of_find_node_by_phandle(fwspec->param[3]);
  807. if (WARN_ON(!np))
  808. return -EINVAL;
  809. ret = partition_translate_id(gic_data.ppi_descs[fwspec->param[1]],
  810. of_node_to_fwnode(np));
  811. if (ret < 0)
  812. return ret;
  813. *hwirq = ret;
  814. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  815. return 0;
  816. }
  817. static const struct irq_domain_ops partition_domain_ops = {
  818. .translate = partition_domain_translate,
  819. .select = gic_irq_domain_select,
  820. };
  821. static int __init gic_init_bases(void __iomem *dist_base,
  822. struct redist_region *rdist_regs,
  823. u32 nr_redist_regions,
  824. u64 redist_stride,
  825. struct fwnode_handle *handle)
  826. {
  827. u32 typer;
  828. int gic_irqs;
  829. int err;
  830. if (!is_hyp_mode_available())
  831. static_key_slow_dec(&supports_deactivate);
  832. if (static_key_true(&supports_deactivate))
  833. pr_info("GIC: Using split EOI/Deactivate mode\n");
  834. gic_data.fwnode = handle;
  835. gic_data.dist_base = dist_base;
  836. gic_data.redist_regions = rdist_regs;
  837. gic_data.nr_redist_regions = nr_redist_regions;
  838. gic_data.redist_stride = redist_stride;
  839. /*
  840. * Find out how many interrupts are supported.
  841. * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)
  842. */
  843. typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
  844. gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer);
  845. gic_irqs = GICD_TYPER_IRQS(typer);
  846. if (gic_irqs > 1020)
  847. gic_irqs = 1020;
  848. gic_data.irq_nr = gic_irqs;
  849. gic_data.domain = irq_domain_create_tree(handle, &gic_irq_domain_ops,
  850. &gic_data);
  851. gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
  852. gic_data.rdists.has_vlpis = true;
  853. gic_data.rdists.has_direct_lpi = true;
  854. if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
  855. err = -ENOMEM;
  856. goto out_free;
  857. }
  858. gic_data.has_rss = !!(typer & GICD_TYPER_RSS);
  859. pr_info("Distributor has %sRange Selector support\n",
  860. gic_data.has_rss ? "" : "no ");
  861. set_handle_irq(gic_handle_irq);
  862. gic_update_vlpi_properties();
  863. if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
  864. its_init(handle, &gic_data.rdists, gic_data.domain);
  865. gic_smp_init();
  866. gic_dist_init();
  867. gic_cpu_init();
  868. gic_cpu_pm_init();
  869. return 0;
  870. out_free:
  871. if (gic_data.domain)
  872. irq_domain_remove(gic_data.domain);
  873. free_percpu(gic_data.rdists.rdist);
  874. return err;
  875. }
  876. static int __init gic_validate_dist_version(void __iomem *dist_base)
  877. {
  878. u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  879. if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4)
  880. return -ENODEV;
  881. return 0;
  882. }
  883. static int get_cpu_number(struct device_node *dn)
  884. {
  885. const __be32 *cell;
  886. u64 hwid;
  887. int cpu;
  888. cell = of_get_property(dn, "reg", NULL);
  889. if (!cell)
  890. return -1;
  891. hwid = of_read_number(cell, of_n_addr_cells(dn));
  892. /*
  893. * Non affinity bits must be set to 0 in the DT
  894. */
  895. if (hwid & ~MPIDR_HWID_BITMASK)
  896. return -1;
  897. for_each_possible_cpu(cpu)
  898. if (cpu_logical_map(cpu) == hwid)
  899. return cpu;
  900. return -1;
  901. }
  902. /* Create all possible partitions at boot time */
  903. static void __init gic_populate_ppi_partitions(struct device_node *gic_node)
  904. {
  905. struct device_node *parts_node, *child_part;
  906. int part_idx = 0, i;
  907. int nr_parts;
  908. struct partition_affinity *parts;
  909. parts_node = of_find_node_by_name(gic_node, "ppi-partitions");
  910. if (!parts_node)
  911. return;
  912. nr_parts = of_get_child_count(parts_node);
  913. if (!nr_parts)
  914. return;
  915. parts = kzalloc(sizeof(*parts) * nr_parts, GFP_KERNEL);
  916. if (WARN_ON(!parts))
  917. return;
  918. for_each_child_of_node(parts_node, child_part) {
  919. struct partition_affinity *part;
  920. int n;
  921. part = &parts[part_idx];
  922. part->partition_id = of_node_to_fwnode(child_part);
  923. pr_info("GIC: PPI partition %s[%d] { ",
  924. child_part->name, part_idx);
  925. n = of_property_count_elems_of_size(child_part, "affinity",
  926. sizeof(u32));
  927. WARN_ON(n <= 0);
  928. for (i = 0; i < n; i++) {
  929. int err, cpu;
  930. u32 cpu_phandle;
  931. struct device_node *cpu_node;
  932. err = of_property_read_u32_index(child_part, "affinity",
  933. i, &cpu_phandle);
  934. if (WARN_ON(err))
  935. continue;
  936. cpu_node = of_find_node_by_phandle(cpu_phandle);
  937. if (WARN_ON(!cpu_node))
  938. continue;
  939. cpu = get_cpu_number(cpu_node);
  940. if (WARN_ON(cpu == -1))
  941. continue;
  942. pr_cont("%pOF[%d] ", cpu_node, cpu);
  943. cpumask_set_cpu(cpu, &part->mask);
  944. }
  945. pr_cont("}\n");
  946. part_idx++;
  947. }
  948. for (i = 0; i < 16; i++) {
  949. unsigned int irq;
  950. struct partition_desc *desc;
  951. struct irq_fwspec ppi_fwspec = {
  952. .fwnode = gic_data.fwnode,
  953. .param_count = 3,
  954. .param = {
  955. [0] = 1,
  956. [1] = i,
  957. [2] = IRQ_TYPE_NONE,
  958. },
  959. };
  960. irq = irq_create_fwspec_mapping(&ppi_fwspec);
  961. if (WARN_ON(!irq))
  962. continue;
  963. desc = partition_create_desc(gic_data.fwnode, parts, nr_parts,
  964. irq, &partition_domain_ops);
  965. if (WARN_ON(!desc))
  966. continue;
  967. gic_data.ppi_descs[i] = desc;
  968. }
  969. }
  970. static void __init gic_of_setup_kvm_info(struct device_node *node)
  971. {
  972. int ret;
  973. struct resource r;
  974. u32 gicv_idx;
  975. gic_v3_kvm_info.type = GIC_V3;
  976. gic_v3_kvm_info.maint_irq = irq_of_parse_and_map(node, 0);
  977. if (!gic_v3_kvm_info.maint_irq)
  978. return;
  979. if (of_property_read_u32(node, "#redistributor-regions",
  980. &gicv_idx))
  981. gicv_idx = 1;
  982. gicv_idx += 3; /* Also skip GICD, GICC, GICH */
  983. ret = of_address_to_resource(node, gicv_idx, &r);
  984. if (!ret)
  985. gic_v3_kvm_info.vcpu = r;
  986. gic_v3_kvm_info.has_v4 = gic_data.rdists.has_vlpis;
  987. gic_set_kvm_info(&gic_v3_kvm_info);
  988. }
  989. static int __init gic_of_init(struct device_node *node, struct device_node *parent)
  990. {
  991. void __iomem *dist_base;
  992. struct redist_region *rdist_regs;
  993. u64 redist_stride;
  994. u32 nr_redist_regions;
  995. int err, i;
  996. dist_base = of_iomap(node, 0);
  997. if (!dist_base) {
  998. pr_err("%pOF: unable to map gic dist registers\n", node);
  999. return -ENXIO;
  1000. }
  1001. err = gic_validate_dist_version(dist_base);
  1002. if (err) {
  1003. pr_err("%pOF: no distributor detected, giving up\n", node);
  1004. goto out_unmap_dist;
  1005. }
  1006. if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions))
  1007. nr_redist_regions = 1;
  1008. rdist_regs = kzalloc(sizeof(*rdist_regs) * nr_redist_regions, GFP_KERNEL);
  1009. if (!rdist_regs) {
  1010. err = -ENOMEM;
  1011. goto out_unmap_dist;
  1012. }
  1013. for (i = 0; i < nr_redist_regions; i++) {
  1014. struct resource res;
  1015. int ret;
  1016. ret = of_address_to_resource(node, 1 + i, &res);
  1017. rdist_regs[i].redist_base = of_iomap(node, 1 + i);
  1018. if (ret || !rdist_regs[i].redist_base) {
  1019. pr_err("%pOF: couldn't map region %d\n", node, i);
  1020. err = -ENODEV;
  1021. goto out_unmap_rdist;
  1022. }
  1023. rdist_regs[i].phys_base = res.start;
  1024. }
  1025. if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
  1026. redist_stride = 0;
  1027. err = gic_init_bases(dist_base, rdist_regs, nr_redist_regions,
  1028. redist_stride, &node->fwnode);
  1029. if (err)
  1030. goto out_unmap_rdist;
  1031. gic_populate_ppi_partitions(node);
  1032. if (static_key_true(&supports_deactivate))
  1033. gic_of_setup_kvm_info(node);
  1034. return 0;
  1035. out_unmap_rdist:
  1036. for (i = 0; i < nr_redist_regions; i++)
  1037. if (rdist_regs[i].redist_base)
  1038. iounmap(rdist_regs[i].redist_base);
  1039. kfree(rdist_regs);
  1040. out_unmap_dist:
  1041. iounmap(dist_base);
  1042. return err;
  1043. }
  1044. IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);
  1045. #ifdef CONFIG_ACPI
  1046. static struct
  1047. {
  1048. void __iomem *dist_base;
  1049. struct redist_region *redist_regs;
  1050. u32 nr_redist_regions;
  1051. bool single_redist;
  1052. u32 maint_irq;
  1053. int maint_irq_mode;
  1054. phys_addr_t vcpu_base;
  1055. } acpi_data __initdata;
  1056. static void __init
  1057. gic_acpi_register_redist(phys_addr_t phys_base, void __iomem *redist_base)
  1058. {
  1059. static int count = 0;
  1060. acpi_data.redist_regs[count].phys_base = phys_base;
  1061. acpi_data.redist_regs[count].redist_base = redist_base;
  1062. acpi_data.redist_regs[count].single_redist = acpi_data.single_redist;
  1063. count++;
  1064. }
  1065. static int __init
  1066. gic_acpi_parse_madt_redist(struct acpi_subtable_header *header,
  1067. const unsigned long end)
  1068. {
  1069. struct acpi_madt_generic_redistributor *redist =
  1070. (struct acpi_madt_generic_redistributor *)header;
  1071. void __iomem *redist_base;
  1072. redist_base = ioremap(redist->base_address, redist->length);
  1073. if (!redist_base) {
  1074. pr_err("Couldn't map GICR region @%llx\n", redist->base_address);
  1075. return -ENOMEM;
  1076. }
  1077. gic_acpi_register_redist(redist->base_address, redist_base);
  1078. return 0;
  1079. }
  1080. static int __init
  1081. gic_acpi_parse_madt_gicc(struct acpi_subtable_header *header,
  1082. const unsigned long end)
  1083. {
  1084. struct acpi_madt_generic_interrupt *gicc =
  1085. (struct acpi_madt_generic_interrupt *)header;
  1086. u32 reg = readl_relaxed(acpi_data.dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  1087. u32 size = reg == GIC_PIDR2_ARCH_GICv4 ? SZ_64K * 4 : SZ_64K * 2;
  1088. void __iomem *redist_base;
  1089. redist_base = ioremap(gicc->gicr_base_address, size);
  1090. if (!redist_base)
  1091. return -ENOMEM;
  1092. gic_acpi_register_redist(gicc->gicr_base_address, redist_base);
  1093. return 0;
  1094. }
  1095. static int __init gic_acpi_collect_gicr_base(void)
  1096. {
  1097. acpi_tbl_entry_handler redist_parser;
  1098. enum acpi_madt_type type;
  1099. if (acpi_data.single_redist) {
  1100. type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
  1101. redist_parser = gic_acpi_parse_madt_gicc;
  1102. } else {
  1103. type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
  1104. redist_parser = gic_acpi_parse_madt_redist;
  1105. }
  1106. /* Collect redistributor base addresses in GICR entries */
  1107. if (acpi_table_parse_madt(type, redist_parser, 0) > 0)
  1108. return 0;
  1109. pr_info("No valid GICR entries exist\n");
  1110. return -ENODEV;
  1111. }
  1112. static int __init gic_acpi_match_gicr(struct acpi_subtable_header *header,
  1113. const unsigned long end)
  1114. {
  1115. /* Subtable presence means that redist exists, that's it */
  1116. return 0;
  1117. }
  1118. static int __init gic_acpi_match_gicc(struct acpi_subtable_header *header,
  1119. const unsigned long end)
  1120. {
  1121. struct acpi_madt_generic_interrupt *gicc =
  1122. (struct acpi_madt_generic_interrupt *)header;
  1123. /*
  1124. * If GICC is enabled and has valid gicr base address, then it means
  1125. * GICR base is presented via GICC
  1126. */
  1127. if ((gicc->flags & ACPI_MADT_ENABLED) && gicc->gicr_base_address)
  1128. return 0;
  1129. return -ENODEV;
  1130. }
  1131. static int __init gic_acpi_count_gicr_regions(void)
  1132. {
  1133. int count;
  1134. /*
  1135. * Count how many redistributor regions we have. It is not allowed
  1136. * to mix redistributor description, GICR and GICC subtables have to be
  1137. * mutually exclusive.
  1138. */
  1139. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
  1140. gic_acpi_match_gicr, 0);
  1141. if (count > 0) {
  1142. acpi_data.single_redist = false;
  1143. return count;
  1144. }
  1145. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  1146. gic_acpi_match_gicc, 0);
  1147. if (count > 0)
  1148. acpi_data.single_redist = true;
  1149. return count;
  1150. }
  1151. static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header,
  1152. struct acpi_probe_entry *ape)
  1153. {
  1154. struct acpi_madt_generic_distributor *dist;
  1155. int count;
  1156. dist = (struct acpi_madt_generic_distributor *)header;
  1157. if (dist->version != ape->driver_data)
  1158. return false;
  1159. /* We need to do that exercise anyway, the sooner the better */
  1160. count = gic_acpi_count_gicr_regions();
  1161. if (count <= 0)
  1162. return false;
  1163. acpi_data.nr_redist_regions = count;
  1164. return true;
  1165. }
  1166. static int __init gic_acpi_parse_virt_madt_gicc(struct acpi_subtable_header *header,
  1167. const unsigned long end)
  1168. {
  1169. struct acpi_madt_generic_interrupt *gicc =
  1170. (struct acpi_madt_generic_interrupt *)header;
  1171. int maint_irq_mode;
  1172. static int first_madt = true;
  1173. /* Skip unusable CPUs */
  1174. if (!(gicc->flags & ACPI_MADT_ENABLED))
  1175. return 0;
  1176. maint_irq_mode = (gicc->flags & ACPI_MADT_VGIC_IRQ_MODE) ?
  1177. ACPI_EDGE_SENSITIVE : ACPI_LEVEL_SENSITIVE;
  1178. if (first_madt) {
  1179. first_madt = false;
  1180. acpi_data.maint_irq = gicc->vgic_interrupt;
  1181. acpi_data.maint_irq_mode = maint_irq_mode;
  1182. acpi_data.vcpu_base = gicc->gicv_base_address;
  1183. return 0;
  1184. }
  1185. /*
  1186. * The maintenance interrupt and GICV should be the same for every CPU
  1187. */
  1188. if ((acpi_data.maint_irq != gicc->vgic_interrupt) ||
  1189. (acpi_data.maint_irq_mode != maint_irq_mode) ||
  1190. (acpi_data.vcpu_base != gicc->gicv_base_address))
  1191. return -EINVAL;
  1192. return 0;
  1193. }
  1194. static bool __init gic_acpi_collect_virt_info(void)
  1195. {
  1196. int count;
  1197. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  1198. gic_acpi_parse_virt_madt_gicc, 0);
  1199. return (count > 0);
  1200. }
  1201. #define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K)
  1202. #define ACPI_GICV2_VCTRL_MEM_SIZE (SZ_4K)
  1203. #define ACPI_GICV2_VCPU_MEM_SIZE (SZ_8K)
  1204. static void __init gic_acpi_setup_kvm_info(void)
  1205. {
  1206. int irq;
  1207. if (!gic_acpi_collect_virt_info()) {
  1208. pr_warn("Unable to get hardware information used for virtualization\n");
  1209. return;
  1210. }
  1211. gic_v3_kvm_info.type = GIC_V3;
  1212. irq = acpi_register_gsi(NULL, acpi_data.maint_irq,
  1213. acpi_data.maint_irq_mode,
  1214. ACPI_ACTIVE_HIGH);
  1215. if (irq <= 0)
  1216. return;
  1217. gic_v3_kvm_info.maint_irq = irq;
  1218. if (acpi_data.vcpu_base) {
  1219. struct resource *vcpu = &gic_v3_kvm_info.vcpu;
  1220. vcpu->flags = IORESOURCE_MEM;
  1221. vcpu->start = acpi_data.vcpu_base;
  1222. vcpu->end = vcpu->start + ACPI_GICV2_VCPU_MEM_SIZE - 1;
  1223. }
  1224. gic_v3_kvm_info.has_v4 = gic_data.rdists.has_vlpis;
  1225. gic_set_kvm_info(&gic_v3_kvm_info);
  1226. }
  1227. static int __init
  1228. gic_acpi_init(struct acpi_subtable_header *header, const unsigned long end)
  1229. {
  1230. struct acpi_madt_generic_distributor *dist;
  1231. struct fwnode_handle *domain_handle;
  1232. size_t size;
  1233. int i, err;
  1234. /* Get distributor base address */
  1235. dist = (struct acpi_madt_generic_distributor *)header;
  1236. acpi_data.dist_base = ioremap(dist->base_address,
  1237. ACPI_GICV3_DIST_MEM_SIZE);
  1238. if (!acpi_data.dist_base) {
  1239. pr_err("Unable to map GICD registers\n");
  1240. return -ENOMEM;
  1241. }
  1242. err = gic_validate_dist_version(acpi_data.dist_base);
  1243. if (err) {
  1244. pr_err("No distributor detected at @%p, giving up",
  1245. acpi_data.dist_base);
  1246. goto out_dist_unmap;
  1247. }
  1248. size = sizeof(*acpi_data.redist_regs) * acpi_data.nr_redist_regions;
  1249. acpi_data.redist_regs = kzalloc(size, GFP_KERNEL);
  1250. if (!acpi_data.redist_regs) {
  1251. err = -ENOMEM;
  1252. goto out_dist_unmap;
  1253. }
  1254. err = gic_acpi_collect_gicr_base();
  1255. if (err)
  1256. goto out_redist_unmap;
  1257. domain_handle = irq_domain_alloc_fwnode(acpi_data.dist_base);
  1258. if (!domain_handle) {
  1259. err = -ENOMEM;
  1260. goto out_redist_unmap;
  1261. }
  1262. err = gic_init_bases(acpi_data.dist_base, acpi_data.redist_regs,
  1263. acpi_data.nr_redist_regions, 0, domain_handle);
  1264. if (err)
  1265. goto out_fwhandle_free;
  1266. acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, domain_handle);
  1267. if (static_key_true(&supports_deactivate))
  1268. gic_acpi_setup_kvm_info();
  1269. return 0;
  1270. out_fwhandle_free:
  1271. irq_domain_free_fwnode(domain_handle);
  1272. out_redist_unmap:
  1273. for (i = 0; i < acpi_data.nr_redist_regions; i++)
  1274. if (acpi_data.redist_regs[i].redist_base)
  1275. iounmap(acpi_data.redist_regs[i].redist_base);
  1276. kfree(acpi_data.redist_regs);
  1277. out_dist_unmap:
  1278. iounmap(acpi_data.dist_base);
  1279. return err;
  1280. }
  1281. IRQCHIP_ACPI_DECLARE(gic_v3, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1282. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V3,
  1283. gic_acpi_init);
  1284. IRQCHIP_ACPI_DECLARE(gic_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1285. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V4,
  1286. gic_acpi_init);
  1287. IRQCHIP_ACPI_DECLARE(gic_v3_or_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1288. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_NONE,
  1289. gic_acpi_init);
  1290. #endif