vgic-mmio-v3.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * VGICv3 MMIO handling functions
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/irqchip/arm-gic-v3.h>
  14. #include <linux/kvm.h>
  15. #include <linux/kvm_host.h>
  16. #include <kvm/iodev.h>
  17. #include <kvm/arm_vgic.h>
  18. #include <asm/kvm_emulate.h>
  19. #include <asm/kvm_arm.h>
  20. #include <asm/kvm_mmu.h>
  21. #include "vgic.h"
  22. #include "vgic-mmio.h"
  23. /* extract @num bytes at @offset bytes offset in data */
  24. unsigned long extract_bytes(u64 data, unsigned int offset,
  25. unsigned int num)
  26. {
  27. return (data >> (offset * 8)) & GENMASK_ULL(num * 8 - 1, 0);
  28. }
  29. /* allows updates of any half of a 64-bit register (or the whole thing) */
  30. u64 update_64bit_reg(u64 reg, unsigned int offset, unsigned int len,
  31. unsigned long val)
  32. {
  33. int lower = (offset & 4) * 8;
  34. int upper = lower + 8 * len - 1;
  35. reg &= ~GENMASK_ULL(upper, lower);
  36. val &= GENMASK_ULL(len * 8 - 1, 0);
  37. return reg | ((u64)val << lower);
  38. }
  39. bool vgic_has_its(struct kvm *kvm)
  40. {
  41. struct vgic_dist *dist = &kvm->arch.vgic;
  42. if (dist->vgic_model != KVM_DEV_TYPE_ARM_VGIC_V3)
  43. return false;
  44. return dist->has_its;
  45. }
  46. bool vgic_supports_direct_msis(struct kvm *kvm)
  47. {
  48. return kvm_vgic_global_state.has_gicv4 && vgic_has_its(kvm);
  49. }
  50. /*
  51. * The Revision field in the IIDR have the following meanings:
  52. *
  53. * Revision 2: Interrupt groups are guest-configurable and signaled using
  54. * their configured groups.
  55. */
  56. static unsigned long vgic_mmio_read_v3_misc(struct kvm_vcpu *vcpu,
  57. gpa_t addr, unsigned int len)
  58. {
  59. struct vgic_dist *vgic = &vcpu->kvm->arch.vgic;
  60. u32 value = 0;
  61. switch (addr & 0x0c) {
  62. case GICD_CTLR:
  63. if (vgic->enabled)
  64. value |= GICD_CTLR_ENABLE_SS_G1;
  65. value |= GICD_CTLR_ARE_NS | GICD_CTLR_DS;
  66. break;
  67. case GICD_TYPER:
  68. value = vgic->nr_spis + VGIC_NR_PRIVATE_IRQS;
  69. value = (value >> 5) - 1;
  70. if (vgic_has_its(vcpu->kvm)) {
  71. value |= (INTERRUPT_ID_BITS_ITS - 1) << 19;
  72. value |= GICD_TYPER_LPIS;
  73. } else {
  74. value |= (INTERRUPT_ID_BITS_SPIS - 1) << 19;
  75. }
  76. break;
  77. case GICD_IIDR:
  78. value = (PRODUCT_ID_KVM << GICD_IIDR_PRODUCT_ID_SHIFT) |
  79. (vgic->implementation_rev << GICD_IIDR_REVISION_SHIFT) |
  80. (IMPLEMENTER_ARM << GICD_IIDR_IMPLEMENTER_SHIFT);
  81. break;
  82. default:
  83. return 0;
  84. }
  85. return value;
  86. }
  87. static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu,
  88. gpa_t addr, unsigned int len,
  89. unsigned long val)
  90. {
  91. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  92. bool was_enabled = dist->enabled;
  93. switch (addr & 0x0c) {
  94. case GICD_CTLR:
  95. dist->enabled = val & GICD_CTLR_ENABLE_SS_G1;
  96. if (!was_enabled && dist->enabled)
  97. vgic_kick_vcpus(vcpu->kvm);
  98. break;
  99. case GICD_TYPER:
  100. case GICD_IIDR:
  101. return;
  102. }
  103. }
  104. static int vgic_mmio_uaccess_write_v3_misc(struct kvm_vcpu *vcpu,
  105. gpa_t addr, unsigned int len,
  106. unsigned long val)
  107. {
  108. switch (addr & 0x0c) {
  109. case GICD_IIDR:
  110. if (val != vgic_mmio_read_v3_misc(vcpu, addr, len))
  111. return -EINVAL;
  112. }
  113. vgic_mmio_write_v3_misc(vcpu, addr, len, val);
  114. return 0;
  115. }
  116. static unsigned long vgic_mmio_read_irouter(struct kvm_vcpu *vcpu,
  117. gpa_t addr, unsigned int len)
  118. {
  119. int intid = VGIC_ADDR_TO_INTID(addr, 64);
  120. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, NULL, intid);
  121. unsigned long ret = 0;
  122. if (!irq)
  123. return 0;
  124. /* The upper word is RAZ for us. */
  125. if (!(addr & 4))
  126. ret = extract_bytes(READ_ONCE(irq->mpidr), addr & 7, len);
  127. vgic_put_irq(vcpu->kvm, irq);
  128. return ret;
  129. }
  130. static void vgic_mmio_write_irouter(struct kvm_vcpu *vcpu,
  131. gpa_t addr, unsigned int len,
  132. unsigned long val)
  133. {
  134. int intid = VGIC_ADDR_TO_INTID(addr, 64);
  135. struct vgic_irq *irq;
  136. unsigned long flags;
  137. /* The upper word is WI for us since we don't implement Aff3. */
  138. if (addr & 4)
  139. return;
  140. irq = vgic_get_irq(vcpu->kvm, NULL, intid);
  141. if (!irq)
  142. return;
  143. spin_lock_irqsave(&irq->irq_lock, flags);
  144. /* We only care about and preserve Aff0, Aff1 and Aff2. */
  145. irq->mpidr = val & GENMASK(23, 0);
  146. irq->target_vcpu = kvm_mpidr_to_vcpu(vcpu->kvm, irq->mpidr);
  147. spin_unlock_irqrestore(&irq->irq_lock, flags);
  148. vgic_put_irq(vcpu->kvm, irq);
  149. }
  150. static unsigned long vgic_mmio_read_v3r_ctlr(struct kvm_vcpu *vcpu,
  151. gpa_t addr, unsigned int len)
  152. {
  153. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  154. return vgic_cpu->lpis_enabled ? GICR_CTLR_ENABLE_LPIS : 0;
  155. }
  156. static void vgic_mmio_write_v3r_ctlr(struct kvm_vcpu *vcpu,
  157. gpa_t addr, unsigned int len,
  158. unsigned long val)
  159. {
  160. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  161. bool was_enabled = vgic_cpu->lpis_enabled;
  162. if (!vgic_has_its(vcpu->kvm))
  163. return;
  164. vgic_cpu->lpis_enabled = val & GICR_CTLR_ENABLE_LPIS;
  165. if (!was_enabled && vgic_cpu->lpis_enabled)
  166. vgic_enable_lpis(vcpu);
  167. }
  168. static unsigned long vgic_mmio_read_v3r_typer(struct kvm_vcpu *vcpu,
  169. gpa_t addr, unsigned int len)
  170. {
  171. unsigned long mpidr = kvm_vcpu_get_mpidr_aff(vcpu);
  172. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  173. struct vgic_redist_region *rdreg = vgic_cpu->rdreg;
  174. int target_vcpu_id = vcpu->vcpu_id;
  175. gpa_t last_rdist_typer = rdreg->base + GICR_TYPER +
  176. (rdreg->free_index - 1) * KVM_VGIC_V3_REDIST_SIZE;
  177. u64 value;
  178. value = (u64)(mpidr & GENMASK(23, 0)) << 32;
  179. value |= ((target_vcpu_id & 0xffff) << 8);
  180. if (addr == last_rdist_typer)
  181. value |= GICR_TYPER_LAST;
  182. if (vgic_has_its(vcpu->kvm))
  183. value |= GICR_TYPER_PLPIS;
  184. return extract_bytes(value, addr & 7, len);
  185. }
  186. static unsigned long vgic_mmio_read_v3r_iidr(struct kvm_vcpu *vcpu,
  187. gpa_t addr, unsigned int len)
  188. {
  189. return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
  190. }
  191. static unsigned long vgic_mmio_read_v3_idregs(struct kvm_vcpu *vcpu,
  192. gpa_t addr, unsigned int len)
  193. {
  194. switch (addr & 0xffff) {
  195. case GICD_PIDR2:
  196. /* report a GICv3 compliant implementation */
  197. return 0x3b;
  198. }
  199. return 0;
  200. }
  201. static unsigned long vgic_v3_uaccess_read_pending(struct kvm_vcpu *vcpu,
  202. gpa_t addr, unsigned int len)
  203. {
  204. u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
  205. u32 value = 0;
  206. int i;
  207. /*
  208. * pending state of interrupt is latched in pending_latch variable.
  209. * Userspace will save and restore pending state and line_level
  210. * separately.
  211. * Refer to Documentation/virtual/kvm/devices/arm-vgic-v3.txt
  212. * for handling of ISPENDR and ICPENDR.
  213. */
  214. for (i = 0; i < len * 8; i++) {
  215. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  216. if (irq->pending_latch)
  217. value |= (1U << i);
  218. vgic_put_irq(vcpu->kvm, irq);
  219. }
  220. return value;
  221. }
  222. static int vgic_v3_uaccess_write_pending(struct kvm_vcpu *vcpu,
  223. gpa_t addr, unsigned int len,
  224. unsigned long val)
  225. {
  226. u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
  227. int i;
  228. unsigned long flags;
  229. for (i = 0; i < len * 8; i++) {
  230. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  231. spin_lock_irqsave(&irq->irq_lock, flags);
  232. if (test_bit(i, &val)) {
  233. /*
  234. * pending_latch is set irrespective of irq type
  235. * (level or edge) to avoid dependency that VM should
  236. * restore irq config before pending info.
  237. */
  238. irq->pending_latch = true;
  239. vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
  240. } else {
  241. irq->pending_latch = false;
  242. spin_unlock_irqrestore(&irq->irq_lock, flags);
  243. }
  244. vgic_put_irq(vcpu->kvm, irq);
  245. }
  246. return 0;
  247. }
  248. /* We want to avoid outer shareable. */
  249. u64 vgic_sanitise_shareability(u64 field)
  250. {
  251. switch (field) {
  252. case GIC_BASER_OuterShareable:
  253. return GIC_BASER_InnerShareable;
  254. default:
  255. return field;
  256. }
  257. }
  258. /* Avoid any inner non-cacheable mapping. */
  259. u64 vgic_sanitise_inner_cacheability(u64 field)
  260. {
  261. switch (field) {
  262. case GIC_BASER_CACHE_nCnB:
  263. case GIC_BASER_CACHE_nC:
  264. return GIC_BASER_CACHE_RaWb;
  265. default:
  266. return field;
  267. }
  268. }
  269. /* Non-cacheable or same-as-inner are OK. */
  270. u64 vgic_sanitise_outer_cacheability(u64 field)
  271. {
  272. switch (field) {
  273. case GIC_BASER_CACHE_SameAsInner:
  274. case GIC_BASER_CACHE_nC:
  275. return field;
  276. default:
  277. return GIC_BASER_CACHE_nC;
  278. }
  279. }
  280. u64 vgic_sanitise_field(u64 reg, u64 field_mask, int field_shift,
  281. u64 (*sanitise_fn)(u64))
  282. {
  283. u64 field = (reg & field_mask) >> field_shift;
  284. field = sanitise_fn(field) << field_shift;
  285. return (reg & ~field_mask) | field;
  286. }
  287. #define PROPBASER_RES0_MASK \
  288. (GENMASK_ULL(63, 59) | GENMASK_ULL(55, 52) | GENMASK_ULL(6, 5))
  289. #define PENDBASER_RES0_MASK \
  290. (BIT_ULL(63) | GENMASK_ULL(61, 59) | GENMASK_ULL(55, 52) | \
  291. GENMASK_ULL(15, 12) | GENMASK_ULL(6, 0))
  292. static u64 vgic_sanitise_pendbaser(u64 reg)
  293. {
  294. reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
  295. GICR_PENDBASER_SHAREABILITY_SHIFT,
  296. vgic_sanitise_shareability);
  297. reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
  298. GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
  299. vgic_sanitise_inner_cacheability);
  300. reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
  301. GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
  302. vgic_sanitise_outer_cacheability);
  303. reg &= ~PENDBASER_RES0_MASK;
  304. reg &= ~GENMASK_ULL(51, 48);
  305. return reg;
  306. }
  307. static u64 vgic_sanitise_propbaser(u64 reg)
  308. {
  309. reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
  310. GICR_PROPBASER_SHAREABILITY_SHIFT,
  311. vgic_sanitise_shareability);
  312. reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
  313. GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
  314. vgic_sanitise_inner_cacheability);
  315. reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
  316. GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
  317. vgic_sanitise_outer_cacheability);
  318. reg &= ~PROPBASER_RES0_MASK;
  319. reg &= ~GENMASK_ULL(51, 48);
  320. return reg;
  321. }
  322. static unsigned long vgic_mmio_read_propbase(struct kvm_vcpu *vcpu,
  323. gpa_t addr, unsigned int len)
  324. {
  325. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  326. return extract_bytes(dist->propbaser, addr & 7, len);
  327. }
  328. static void vgic_mmio_write_propbase(struct kvm_vcpu *vcpu,
  329. gpa_t addr, unsigned int len,
  330. unsigned long val)
  331. {
  332. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  333. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  334. u64 old_propbaser, propbaser;
  335. /* Storing a value with LPIs already enabled is undefined */
  336. if (vgic_cpu->lpis_enabled)
  337. return;
  338. do {
  339. old_propbaser = READ_ONCE(dist->propbaser);
  340. propbaser = old_propbaser;
  341. propbaser = update_64bit_reg(propbaser, addr & 4, len, val);
  342. propbaser = vgic_sanitise_propbaser(propbaser);
  343. } while (cmpxchg64(&dist->propbaser, old_propbaser,
  344. propbaser) != old_propbaser);
  345. }
  346. static unsigned long vgic_mmio_read_pendbase(struct kvm_vcpu *vcpu,
  347. gpa_t addr, unsigned int len)
  348. {
  349. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  350. return extract_bytes(vgic_cpu->pendbaser, addr & 7, len);
  351. }
  352. static void vgic_mmio_write_pendbase(struct kvm_vcpu *vcpu,
  353. gpa_t addr, unsigned int len,
  354. unsigned long val)
  355. {
  356. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  357. u64 old_pendbaser, pendbaser;
  358. /* Storing a value with LPIs already enabled is undefined */
  359. if (vgic_cpu->lpis_enabled)
  360. return;
  361. do {
  362. old_pendbaser = READ_ONCE(vgic_cpu->pendbaser);
  363. pendbaser = old_pendbaser;
  364. pendbaser = update_64bit_reg(pendbaser, addr & 4, len, val);
  365. pendbaser = vgic_sanitise_pendbaser(pendbaser);
  366. } while (cmpxchg64(&vgic_cpu->pendbaser, old_pendbaser,
  367. pendbaser) != old_pendbaser);
  368. }
  369. /*
  370. * The GICv3 per-IRQ registers are split to control PPIs and SGIs in the
  371. * redistributors, while SPIs are covered by registers in the distributor
  372. * block. Trying to set private IRQs in this block gets ignored.
  373. * We take some special care here to fix the calculation of the register
  374. * offset.
  375. */
  376. #define REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(off, rd, wr, ur, uw, bpi, acc) \
  377. { \
  378. .reg_offset = off, \
  379. .bits_per_irq = bpi, \
  380. .len = (bpi * VGIC_NR_PRIVATE_IRQS) / 8, \
  381. .access_flags = acc, \
  382. .read = vgic_mmio_read_raz, \
  383. .write = vgic_mmio_write_wi, \
  384. }, { \
  385. .reg_offset = off + (bpi * VGIC_NR_PRIVATE_IRQS) / 8, \
  386. .bits_per_irq = bpi, \
  387. .len = (bpi * (1024 - VGIC_NR_PRIVATE_IRQS)) / 8, \
  388. .access_flags = acc, \
  389. .read = rd, \
  390. .write = wr, \
  391. .uaccess_read = ur, \
  392. .uaccess_write = uw, \
  393. }
  394. static const struct vgic_register_region vgic_v3_dist_registers[] = {
  395. REGISTER_DESC_WITH_LENGTH_UACCESS(GICD_CTLR,
  396. vgic_mmio_read_v3_misc, vgic_mmio_write_v3_misc,
  397. NULL, vgic_mmio_uaccess_write_v3_misc,
  398. 16, VGIC_ACCESS_32bit),
  399. REGISTER_DESC_WITH_LENGTH(GICD_STATUSR,
  400. vgic_mmio_read_rao, vgic_mmio_write_wi, 4,
  401. VGIC_ACCESS_32bit),
  402. REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IGROUPR,
  403. vgic_mmio_read_group, vgic_mmio_write_group, NULL, NULL, 1,
  404. VGIC_ACCESS_32bit),
  405. REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISENABLER,
  406. vgic_mmio_read_enable, vgic_mmio_write_senable, NULL, NULL, 1,
  407. VGIC_ACCESS_32bit),
  408. REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICENABLER,
  409. vgic_mmio_read_enable, vgic_mmio_write_cenable, NULL, NULL, 1,
  410. VGIC_ACCESS_32bit),
  411. REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISPENDR,
  412. vgic_mmio_read_pending, vgic_mmio_write_spending,
  413. vgic_v3_uaccess_read_pending, vgic_v3_uaccess_write_pending, 1,
  414. VGIC_ACCESS_32bit),
  415. REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICPENDR,
  416. vgic_mmio_read_pending, vgic_mmio_write_cpending,
  417. vgic_mmio_read_raz, vgic_mmio_uaccess_write_wi, 1,
  418. VGIC_ACCESS_32bit),
  419. REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISACTIVER,
  420. vgic_mmio_read_active, vgic_mmio_write_sactive,
  421. NULL, vgic_mmio_uaccess_write_sactive, 1,
  422. VGIC_ACCESS_32bit),
  423. REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICACTIVER,
  424. vgic_mmio_read_active, vgic_mmio_write_cactive,
  425. NULL, vgic_mmio_uaccess_write_cactive,
  426. 1, VGIC_ACCESS_32bit),
  427. REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IPRIORITYR,
  428. vgic_mmio_read_priority, vgic_mmio_write_priority, NULL, NULL,
  429. 8, VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
  430. REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ITARGETSR,
  431. vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 8,
  432. VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
  433. REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ICFGR,
  434. vgic_mmio_read_config, vgic_mmio_write_config, NULL, NULL, 2,
  435. VGIC_ACCESS_32bit),
  436. REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IGRPMODR,
  437. vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
  438. VGIC_ACCESS_32bit),
  439. REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IROUTER,
  440. vgic_mmio_read_irouter, vgic_mmio_write_irouter, NULL, NULL, 64,
  441. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  442. REGISTER_DESC_WITH_LENGTH(GICD_IDREGS,
  443. vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48,
  444. VGIC_ACCESS_32bit),
  445. };
  446. static const struct vgic_register_region vgic_v3_rdbase_registers[] = {
  447. REGISTER_DESC_WITH_LENGTH(GICR_CTLR,
  448. vgic_mmio_read_v3r_ctlr, vgic_mmio_write_v3r_ctlr, 4,
  449. VGIC_ACCESS_32bit),
  450. REGISTER_DESC_WITH_LENGTH(GICR_STATUSR,
  451. vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
  452. VGIC_ACCESS_32bit),
  453. REGISTER_DESC_WITH_LENGTH(GICR_IIDR,
  454. vgic_mmio_read_v3r_iidr, vgic_mmio_write_wi, 4,
  455. VGIC_ACCESS_32bit),
  456. REGISTER_DESC_WITH_LENGTH(GICR_TYPER,
  457. vgic_mmio_read_v3r_typer, vgic_mmio_write_wi, 8,
  458. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  459. REGISTER_DESC_WITH_LENGTH(GICR_WAKER,
  460. vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
  461. VGIC_ACCESS_32bit),
  462. REGISTER_DESC_WITH_LENGTH(GICR_PROPBASER,
  463. vgic_mmio_read_propbase, vgic_mmio_write_propbase, 8,
  464. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  465. REGISTER_DESC_WITH_LENGTH(GICR_PENDBASER,
  466. vgic_mmio_read_pendbase, vgic_mmio_write_pendbase, 8,
  467. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  468. REGISTER_DESC_WITH_LENGTH(GICR_IDREGS,
  469. vgic_mmio_read_v3_idregs, vgic_mmio_write_wi, 48,
  470. VGIC_ACCESS_32bit),
  471. };
  472. static const struct vgic_register_region vgic_v3_sgibase_registers[] = {
  473. REGISTER_DESC_WITH_LENGTH(GICR_IGROUPR0,
  474. vgic_mmio_read_group, vgic_mmio_write_group, 4,
  475. VGIC_ACCESS_32bit),
  476. REGISTER_DESC_WITH_LENGTH(GICR_ISENABLER0,
  477. vgic_mmio_read_enable, vgic_mmio_write_senable, 4,
  478. VGIC_ACCESS_32bit),
  479. REGISTER_DESC_WITH_LENGTH(GICR_ICENABLER0,
  480. vgic_mmio_read_enable, vgic_mmio_write_cenable, 4,
  481. VGIC_ACCESS_32bit),
  482. REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_ISPENDR0,
  483. vgic_mmio_read_pending, vgic_mmio_write_spending,
  484. vgic_v3_uaccess_read_pending, vgic_v3_uaccess_write_pending, 4,
  485. VGIC_ACCESS_32bit),
  486. REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_ICPENDR0,
  487. vgic_mmio_read_pending, vgic_mmio_write_cpending,
  488. vgic_mmio_read_raz, vgic_mmio_uaccess_write_wi, 4,
  489. VGIC_ACCESS_32bit),
  490. REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_ISACTIVER0,
  491. vgic_mmio_read_active, vgic_mmio_write_sactive,
  492. NULL, vgic_mmio_uaccess_write_sactive,
  493. 4, VGIC_ACCESS_32bit),
  494. REGISTER_DESC_WITH_LENGTH_UACCESS(GICR_ICACTIVER0,
  495. vgic_mmio_read_active, vgic_mmio_write_cactive,
  496. NULL, vgic_mmio_uaccess_write_cactive,
  497. 4, VGIC_ACCESS_32bit),
  498. REGISTER_DESC_WITH_LENGTH(GICR_IPRIORITYR0,
  499. vgic_mmio_read_priority, vgic_mmio_write_priority, 32,
  500. VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
  501. REGISTER_DESC_WITH_LENGTH(GICR_ICFGR0,
  502. vgic_mmio_read_config, vgic_mmio_write_config, 8,
  503. VGIC_ACCESS_32bit),
  504. REGISTER_DESC_WITH_LENGTH(GICR_IGRPMODR0,
  505. vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
  506. VGIC_ACCESS_32bit),
  507. REGISTER_DESC_WITH_LENGTH(GICR_NSACR,
  508. vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
  509. VGIC_ACCESS_32bit),
  510. };
  511. unsigned int vgic_v3_init_dist_iodev(struct vgic_io_device *dev)
  512. {
  513. dev->regions = vgic_v3_dist_registers;
  514. dev->nr_regions = ARRAY_SIZE(vgic_v3_dist_registers);
  515. kvm_iodevice_init(&dev->dev, &kvm_io_gic_ops);
  516. return SZ_64K;
  517. }
  518. /**
  519. * vgic_register_redist_iodev - register a single redist iodev
  520. * @vcpu: The VCPU to which the redistributor belongs
  521. *
  522. * Register a KVM iodev for this VCPU's redistributor using the address
  523. * provided.
  524. *
  525. * Return 0 on success, -ERRNO otherwise.
  526. */
  527. int vgic_register_redist_iodev(struct kvm_vcpu *vcpu)
  528. {
  529. struct kvm *kvm = vcpu->kvm;
  530. struct vgic_dist *vgic = &kvm->arch.vgic;
  531. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  532. struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev;
  533. struct vgic_io_device *sgi_dev = &vcpu->arch.vgic_cpu.sgi_iodev;
  534. struct vgic_redist_region *rdreg;
  535. gpa_t rd_base, sgi_base;
  536. int ret;
  537. if (!IS_VGIC_ADDR_UNDEF(vgic_cpu->rd_iodev.base_addr))
  538. return 0;
  539. /*
  540. * We may be creating VCPUs before having set the base address for the
  541. * redistributor region, in which case we will come back to this
  542. * function for all VCPUs when the base address is set. Just return
  543. * without doing any work for now.
  544. */
  545. rdreg = vgic_v3_rdist_free_slot(&vgic->rd_regions);
  546. if (!rdreg)
  547. return 0;
  548. if (!vgic_v3_check_base(kvm))
  549. return -EINVAL;
  550. vgic_cpu->rdreg = rdreg;
  551. rd_base = rdreg->base + rdreg->free_index * KVM_VGIC_V3_REDIST_SIZE;
  552. sgi_base = rd_base + SZ_64K;
  553. kvm_iodevice_init(&rd_dev->dev, &kvm_io_gic_ops);
  554. rd_dev->base_addr = rd_base;
  555. rd_dev->iodev_type = IODEV_REDIST;
  556. rd_dev->regions = vgic_v3_rdbase_registers;
  557. rd_dev->nr_regions = ARRAY_SIZE(vgic_v3_rdbase_registers);
  558. rd_dev->redist_vcpu = vcpu;
  559. mutex_lock(&kvm->slots_lock);
  560. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, rd_base,
  561. SZ_64K, &rd_dev->dev);
  562. mutex_unlock(&kvm->slots_lock);
  563. if (ret)
  564. return ret;
  565. kvm_iodevice_init(&sgi_dev->dev, &kvm_io_gic_ops);
  566. sgi_dev->base_addr = sgi_base;
  567. sgi_dev->iodev_type = IODEV_REDIST;
  568. sgi_dev->regions = vgic_v3_sgibase_registers;
  569. sgi_dev->nr_regions = ARRAY_SIZE(vgic_v3_sgibase_registers);
  570. sgi_dev->redist_vcpu = vcpu;
  571. mutex_lock(&kvm->slots_lock);
  572. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, sgi_base,
  573. SZ_64K, &sgi_dev->dev);
  574. if (ret) {
  575. kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS,
  576. &rd_dev->dev);
  577. goto out;
  578. }
  579. rdreg->free_index++;
  580. out:
  581. mutex_unlock(&kvm->slots_lock);
  582. return ret;
  583. }
  584. static void vgic_unregister_redist_iodev(struct kvm_vcpu *vcpu)
  585. {
  586. struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev;
  587. struct vgic_io_device *sgi_dev = &vcpu->arch.vgic_cpu.sgi_iodev;
  588. kvm_io_bus_unregister_dev(vcpu->kvm, KVM_MMIO_BUS, &rd_dev->dev);
  589. kvm_io_bus_unregister_dev(vcpu->kvm, KVM_MMIO_BUS, &sgi_dev->dev);
  590. }
  591. static int vgic_register_all_redist_iodevs(struct kvm *kvm)
  592. {
  593. struct kvm_vcpu *vcpu;
  594. int c, ret = 0;
  595. kvm_for_each_vcpu(c, vcpu, kvm) {
  596. ret = vgic_register_redist_iodev(vcpu);
  597. if (ret)
  598. break;
  599. }
  600. if (ret) {
  601. /* The current c failed, so we start with the previous one. */
  602. mutex_lock(&kvm->slots_lock);
  603. for (c--; c >= 0; c--) {
  604. vcpu = kvm_get_vcpu(kvm, c);
  605. vgic_unregister_redist_iodev(vcpu);
  606. }
  607. mutex_unlock(&kvm->slots_lock);
  608. }
  609. return ret;
  610. }
  611. /**
  612. * vgic_v3_insert_redist_region - Insert a new redistributor region
  613. *
  614. * Performs various checks before inserting the rdist region in the list.
  615. * Those tests depend on whether the size of the rdist region is known
  616. * (ie. count != 0). The list is sorted by rdist region index.
  617. *
  618. * @kvm: kvm handle
  619. * @index: redist region index
  620. * @base: base of the new rdist region
  621. * @count: number of redistributors the region is made of (0 in the old style
  622. * single region, whose size is induced from the number of vcpus)
  623. *
  624. * Return 0 on success, < 0 otherwise
  625. */
  626. static int vgic_v3_insert_redist_region(struct kvm *kvm, uint32_t index,
  627. gpa_t base, uint32_t count)
  628. {
  629. struct vgic_dist *d = &kvm->arch.vgic;
  630. struct vgic_redist_region *rdreg;
  631. struct list_head *rd_regions = &d->rd_regions;
  632. size_t size = count * KVM_VGIC_V3_REDIST_SIZE;
  633. int ret;
  634. /* single rdist region already set ?*/
  635. if (!count && !list_empty(rd_regions))
  636. return -EINVAL;
  637. /* cross the end of memory ? */
  638. if (base + size < base)
  639. return -EINVAL;
  640. if (list_empty(rd_regions)) {
  641. if (index != 0)
  642. return -EINVAL;
  643. } else {
  644. rdreg = list_last_entry(rd_regions,
  645. struct vgic_redist_region, list);
  646. if (index != rdreg->index + 1)
  647. return -EINVAL;
  648. /* Cannot add an explicitly sized regions after legacy region */
  649. if (!rdreg->count)
  650. return -EINVAL;
  651. }
  652. /*
  653. * For legacy single-region redistributor regions (!count),
  654. * check that the redistributor region does not overlap with the
  655. * distributor's address space.
  656. */
  657. if (!count && !IS_VGIC_ADDR_UNDEF(d->vgic_dist_base) &&
  658. vgic_dist_overlap(kvm, base, size))
  659. return -EINVAL;
  660. /* collision with any other rdist region? */
  661. if (vgic_v3_rdist_overlap(kvm, base, size))
  662. return -EINVAL;
  663. rdreg = kzalloc(sizeof(*rdreg), GFP_KERNEL);
  664. if (!rdreg)
  665. return -ENOMEM;
  666. rdreg->base = VGIC_ADDR_UNDEF;
  667. ret = vgic_check_ioaddr(kvm, &rdreg->base, base, SZ_64K);
  668. if (ret)
  669. goto free;
  670. rdreg->base = base;
  671. rdreg->count = count;
  672. rdreg->free_index = 0;
  673. rdreg->index = index;
  674. list_add_tail(&rdreg->list, rd_regions);
  675. return 0;
  676. free:
  677. kfree(rdreg);
  678. return ret;
  679. }
  680. int vgic_v3_set_redist_base(struct kvm *kvm, u32 index, u64 addr, u32 count)
  681. {
  682. int ret;
  683. ret = vgic_v3_insert_redist_region(kvm, index, addr, count);
  684. if (ret)
  685. return ret;
  686. /*
  687. * Register iodevs for each existing VCPU. Adding more VCPUs
  688. * afterwards will register the iodevs when needed.
  689. */
  690. ret = vgic_register_all_redist_iodevs(kvm);
  691. if (ret)
  692. return ret;
  693. return 0;
  694. }
  695. int vgic_v3_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr)
  696. {
  697. const struct vgic_register_region *region;
  698. struct vgic_io_device iodev;
  699. struct vgic_reg_attr reg_attr;
  700. struct kvm_vcpu *vcpu;
  701. gpa_t addr;
  702. int ret;
  703. ret = vgic_v3_parse_attr(dev, attr, &reg_attr);
  704. if (ret)
  705. return ret;
  706. vcpu = reg_attr.vcpu;
  707. addr = reg_attr.addr;
  708. switch (attr->group) {
  709. case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
  710. iodev.regions = vgic_v3_dist_registers;
  711. iodev.nr_regions = ARRAY_SIZE(vgic_v3_dist_registers);
  712. iodev.base_addr = 0;
  713. break;
  714. case KVM_DEV_ARM_VGIC_GRP_REDIST_REGS:{
  715. iodev.regions = vgic_v3_rdbase_registers;
  716. iodev.nr_regions = ARRAY_SIZE(vgic_v3_rdbase_registers);
  717. iodev.base_addr = 0;
  718. break;
  719. }
  720. case KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS: {
  721. u64 reg, id;
  722. id = (attr->attr & KVM_DEV_ARM_VGIC_SYSREG_INSTR_MASK);
  723. return vgic_v3_has_cpu_sysregs_attr(vcpu, 0, id, &reg);
  724. }
  725. default:
  726. return -ENXIO;
  727. }
  728. /* We only support aligned 32-bit accesses. */
  729. if (addr & 3)
  730. return -ENXIO;
  731. region = vgic_get_mmio_region(vcpu, &iodev, addr, sizeof(u32));
  732. if (!region)
  733. return -ENXIO;
  734. return 0;
  735. }
  736. /*
  737. * Compare a given affinity (level 1-3 and a level 0 mask, from the SGI
  738. * generation register ICC_SGI1R_EL1) with a given VCPU.
  739. * If the VCPU's MPIDR matches, return the level0 affinity, otherwise
  740. * return -1.
  741. */
  742. static int match_mpidr(u64 sgi_aff, u16 sgi_cpu_mask, struct kvm_vcpu *vcpu)
  743. {
  744. unsigned long affinity;
  745. int level0;
  746. /*
  747. * Split the current VCPU's MPIDR into affinity level 0 and the
  748. * rest as this is what we have to compare against.
  749. */
  750. affinity = kvm_vcpu_get_mpidr_aff(vcpu);
  751. level0 = MPIDR_AFFINITY_LEVEL(affinity, 0);
  752. affinity &= ~MPIDR_LEVEL_MASK;
  753. /* bail out if the upper three levels don't match */
  754. if (sgi_aff != affinity)
  755. return -1;
  756. /* Is this VCPU's bit set in the mask ? */
  757. if (!(sgi_cpu_mask & BIT(level0)))
  758. return -1;
  759. return level0;
  760. }
  761. /*
  762. * The ICC_SGI* registers encode the affinity differently from the MPIDR,
  763. * so provide a wrapper to use the existing defines to isolate a certain
  764. * affinity level.
  765. */
  766. #define SGI_AFFINITY_LEVEL(reg, level) \
  767. ((((reg) & ICC_SGI1R_AFFINITY_## level ##_MASK) \
  768. >> ICC_SGI1R_AFFINITY_## level ##_SHIFT) << MPIDR_LEVEL_SHIFT(level))
  769. /**
  770. * vgic_v3_dispatch_sgi - handle SGI requests from VCPUs
  771. * @vcpu: The VCPU requesting a SGI
  772. * @reg: The value written into ICC_{ASGI1,SGI0,SGI1}R by that VCPU
  773. * @allow_group1: Does the sysreg access allow generation of G1 SGIs
  774. *
  775. * With GICv3 (and ARE=1) CPUs trigger SGIs by writing to a system register.
  776. * This will trap in sys_regs.c and call this function.
  777. * This ICC_SGI1R_EL1 register contains the upper three affinity levels of the
  778. * target processors as well as a bitmask of 16 Aff0 CPUs.
  779. * If the interrupt routing mode bit is not set, we iterate over all VCPUs to
  780. * check for matching ones. If this bit is set, we signal all, but not the
  781. * calling VCPU.
  782. */
  783. void vgic_v3_dispatch_sgi(struct kvm_vcpu *vcpu, u64 reg, bool allow_group1)
  784. {
  785. struct kvm *kvm = vcpu->kvm;
  786. struct kvm_vcpu *c_vcpu;
  787. u16 target_cpus;
  788. u64 mpidr;
  789. int sgi, c;
  790. int vcpu_id = vcpu->vcpu_id;
  791. bool broadcast;
  792. unsigned long flags;
  793. sgi = (reg & ICC_SGI1R_SGI_ID_MASK) >> ICC_SGI1R_SGI_ID_SHIFT;
  794. broadcast = reg & BIT_ULL(ICC_SGI1R_IRQ_ROUTING_MODE_BIT);
  795. target_cpus = (reg & ICC_SGI1R_TARGET_LIST_MASK) >> ICC_SGI1R_TARGET_LIST_SHIFT;
  796. mpidr = SGI_AFFINITY_LEVEL(reg, 3);
  797. mpidr |= SGI_AFFINITY_LEVEL(reg, 2);
  798. mpidr |= SGI_AFFINITY_LEVEL(reg, 1);
  799. /*
  800. * We iterate over all VCPUs to find the MPIDRs matching the request.
  801. * If we have handled one CPU, we clear its bit to detect early
  802. * if we are already finished. This avoids iterating through all
  803. * VCPUs when most of the times we just signal a single VCPU.
  804. */
  805. kvm_for_each_vcpu(c, c_vcpu, kvm) {
  806. struct vgic_irq *irq;
  807. /* Exit early if we have dealt with all requested CPUs */
  808. if (!broadcast && target_cpus == 0)
  809. break;
  810. /* Don't signal the calling VCPU */
  811. if (broadcast && c == vcpu_id)
  812. continue;
  813. if (!broadcast) {
  814. int level0;
  815. level0 = match_mpidr(mpidr, target_cpus, c_vcpu);
  816. if (level0 == -1)
  817. continue;
  818. /* remove this matching VCPU from the mask */
  819. target_cpus &= ~BIT(level0);
  820. }
  821. irq = vgic_get_irq(vcpu->kvm, c_vcpu, sgi);
  822. spin_lock_irqsave(&irq->irq_lock, flags);
  823. /*
  824. * An access targetting Group0 SGIs can only generate
  825. * those, while an access targetting Group1 SGIs can
  826. * generate interrupts of either group.
  827. */
  828. if (!irq->group || allow_group1) {
  829. irq->pending_latch = true;
  830. vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
  831. } else {
  832. spin_unlock_irqrestore(&irq->irq_lock, flags);
  833. }
  834. vgic_put_irq(vcpu->kvm, irq);
  835. }
  836. }
  837. int vgic_v3_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
  838. int offset, u32 *val)
  839. {
  840. struct vgic_io_device dev = {
  841. .regions = vgic_v3_dist_registers,
  842. .nr_regions = ARRAY_SIZE(vgic_v3_dist_registers),
  843. };
  844. return vgic_uaccess(vcpu, &dev, is_write, offset, val);
  845. }
  846. int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
  847. int offset, u32 *val)
  848. {
  849. struct vgic_io_device rd_dev = {
  850. .regions = vgic_v3_rdbase_registers,
  851. .nr_regions = ARRAY_SIZE(vgic_v3_rdbase_registers),
  852. };
  853. struct vgic_io_device sgi_dev = {
  854. .regions = vgic_v3_sgibase_registers,
  855. .nr_regions = ARRAY_SIZE(vgic_v3_sgibase_registers),
  856. };
  857. /* SGI_base is the next 64K frame after RD_base */
  858. if (offset >= SZ_64K)
  859. return vgic_uaccess(vcpu, &sgi_dev, is_write, offset - SZ_64K,
  860. val);
  861. else
  862. return vgic_uaccess(vcpu, &rd_dev, is_write, offset, val);
  863. }
  864. int vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write,
  865. u32 intid, u64 *val)
  866. {
  867. if (intid % 32)
  868. return -EINVAL;
  869. if (is_write)
  870. vgic_write_irq_line_level_info(vcpu, intid, *val);
  871. else
  872. *val = vgic_read_irq_line_level_info(vcpu, intid);
  873. return 0;
  874. }