irq-gic-v3.c 37 KB

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