vgic.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /*
  2. * Copyright (C) 2012 ARM Ltd.
  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, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/kvm.h>
  19. #include <linux/kvm_host.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <asm/kvm_emulate.h>
  23. /*
  24. * How the whole thing works (courtesy of Christoffer Dall):
  25. *
  26. * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if
  27. * something is pending
  28. * - VGIC pending interrupts are stored on the vgic.irq_state vgic
  29. * bitmap (this bitmap is updated by both user land ioctls and guest
  30. * mmio ops, and other in-kernel peripherals such as the
  31. * arch. timers) and indicate the 'wire' state.
  32. * - Every time the bitmap changes, the irq_pending_on_cpu oracle is
  33. * recalculated
  34. * - To calculate the oracle, we need info for each cpu from
  35. * compute_pending_for_cpu, which considers:
  36. * - PPI: dist->irq_state & dist->irq_enable
  37. * - SPI: dist->irq_state & dist->irq_enable & dist->irq_spi_target
  38. * - irq_spi_target is a 'formatted' version of the GICD_ICFGR
  39. * registers, stored on each vcpu. We only keep one bit of
  40. * information per interrupt, making sure that only one vcpu can
  41. * accept the interrupt.
  42. * - The same is true when injecting an interrupt, except that we only
  43. * consider a single interrupt at a time. The irq_spi_cpu array
  44. * contains the target CPU for each SPI.
  45. *
  46. * The handling of level interrupts adds some extra complexity. We
  47. * need to track when the interrupt has been EOIed, so we can sample
  48. * the 'line' again. This is achieved as such:
  49. *
  50. * - When a level interrupt is moved onto a vcpu, the corresponding
  51. * bit in irq_active is set. As long as this bit is set, the line
  52. * will be ignored for further interrupts. The interrupt is injected
  53. * into the vcpu with the GICH_LR_EOI bit set (generate a
  54. * maintenance interrupt on EOI).
  55. * - When the interrupt is EOIed, the maintenance interrupt fires,
  56. * and clears the corresponding bit in irq_active. This allow the
  57. * interrupt line to be sampled again.
  58. */
  59. #define VGIC_ADDR_UNDEF (-1)
  60. #define IS_VGIC_ADDR_UNDEF(_x) ((_x) == VGIC_ADDR_UNDEF)
  61. #define ACCESS_READ_VALUE (1 << 0)
  62. #define ACCESS_READ_RAZ (0 << 0)
  63. #define ACCESS_READ_MASK(x) ((x) & (1 << 0))
  64. #define ACCESS_WRITE_IGNORED (0 << 1)
  65. #define ACCESS_WRITE_SETBIT (1 << 1)
  66. #define ACCESS_WRITE_CLEARBIT (2 << 1)
  67. #define ACCESS_WRITE_VALUE (3 << 1)
  68. #define ACCESS_WRITE_MASK(x) ((x) & (3 << 1))
  69. static void vgic_update_state(struct kvm *kvm);
  70. static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg);
  71. static u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x,
  72. int cpuid, u32 offset)
  73. {
  74. offset >>= 2;
  75. if (!offset)
  76. return x->percpu[cpuid].reg;
  77. else
  78. return x->shared.reg + offset - 1;
  79. }
  80. static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
  81. int cpuid, int irq)
  82. {
  83. if (irq < VGIC_NR_PRIVATE_IRQS)
  84. return test_bit(irq, x->percpu[cpuid].reg_ul);
  85. return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared.reg_ul);
  86. }
  87. static void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
  88. int irq, int val)
  89. {
  90. unsigned long *reg;
  91. if (irq < VGIC_NR_PRIVATE_IRQS) {
  92. reg = x->percpu[cpuid].reg_ul;
  93. } else {
  94. reg = x->shared.reg_ul;
  95. irq -= VGIC_NR_PRIVATE_IRQS;
  96. }
  97. if (val)
  98. set_bit(irq, reg);
  99. else
  100. clear_bit(irq, reg);
  101. }
  102. static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
  103. {
  104. if (unlikely(cpuid >= VGIC_MAX_CPUS))
  105. return NULL;
  106. return x->percpu[cpuid].reg_ul;
  107. }
  108. static unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
  109. {
  110. return x->shared.reg_ul;
  111. }
  112. static u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
  113. {
  114. offset >>= 2;
  115. BUG_ON(offset > (VGIC_NR_IRQS / 4));
  116. if (offset < 4)
  117. return x->percpu[cpuid] + offset;
  118. else
  119. return x->shared + offset - 8;
  120. }
  121. #define VGIC_CFG_LEVEL 0
  122. #define VGIC_CFG_EDGE 1
  123. static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
  124. {
  125. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  126. int irq_val;
  127. irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
  128. return irq_val == VGIC_CFG_EDGE;
  129. }
  130. static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
  131. {
  132. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  133. return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
  134. }
  135. static int vgic_irq_is_active(struct kvm_vcpu *vcpu, int irq)
  136. {
  137. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  138. return vgic_bitmap_get_irq_val(&dist->irq_active, vcpu->vcpu_id, irq);
  139. }
  140. static void vgic_irq_set_active(struct kvm_vcpu *vcpu, int irq)
  141. {
  142. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  143. vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 1);
  144. }
  145. static void vgic_irq_clear_active(struct kvm_vcpu *vcpu, int irq)
  146. {
  147. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  148. vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 0);
  149. }
  150. static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
  151. {
  152. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  153. return vgic_bitmap_get_irq_val(&dist->irq_state, vcpu->vcpu_id, irq);
  154. }
  155. static void vgic_dist_irq_set(struct kvm_vcpu *vcpu, int irq)
  156. {
  157. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  158. vgic_bitmap_set_irq_val(&dist->irq_state, vcpu->vcpu_id, irq, 1);
  159. }
  160. static void vgic_dist_irq_clear(struct kvm_vcpu *vcpu, int irq)
  161. {
  162. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  163. vgic_bitmap_set_irq_val(&dist->irq_state, vcpu->vcpu_id, irq, 0);
  164. }
  165. static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
  166. {
  167. if (irq < VGIC_NR_PRIVATE_IRQS)
  168. set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
  169. else
  170. set_bit(irq - VGIC_NR_PRIVATE_IRQS,
  171. vcpu->arch.vgic_cpu.pending_shared);
  172. }
  173. static void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
  174. {
  175. if (irq < VGIC_NR_PRIVATE_IRQS)
  176. clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
  177. else
  178. clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
  179. vcpu->arch.vgic_cpu.pending_shared);
  180. }
  181. static u32 mmio_data_read(struct kvm_exit_mmio *mmio, u32 mask)
  182. {
  183. return *((u32 *)mmio->data) & mask;
  184. }
  185. static void mmio_data_write(struct kvm_exit_mmio *mmio, u32 mask, u32 value)
  186. {
  187. *((u32 *)mmio->data) = value & mask;
  188. }
  189. /**
  190. * vgic_reg_access - access vgic register
  191. * @mmio: pointer to the data describing the mmio access
  192. * @reg: pointer to the virtual backing of vgic distributor data
  193. * @offset: least significant 2 bits used for word offset
  194. * @mode: ACCESS_ mode (see defines above)
  195. *
  196. * Helper to make vgic register access easier using one of the access
  197. * modes defined for vgic register access
  198. * (read,raz,write-ignored,setbit,clearbit,write)
  199. */
  200. static void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
  201. phys_addr_t offset, int mode)
  202. {
  203. int word_offset = (offset & 3) * 8;
  204. u32 mask = (1UL << (mmio->len * 8)) - 1;
  205. u32 regval;
  206. /*
  207. * Any alignment fault should have been delivered to the guest
  208. * directly (ARM ARM B3.12.7 "Prioritization of aborts").
  209. */
  210. if (reg) {
  211. regval = *reg;
  212. } else {
  213. BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
  214. regval = 0;
  215. }
  216. if (mmio->is_write) {
  217. u32 data = mmio_data_read(mmio, mask) << word_offset;
  218. switch (ACCESS_WRITE_MASK(mode)) {
  219. case ACCESS_WRITE_IGNORED:
  220. return;
  221. case ACCESS_WRITE_SETBIT:
  222. regval |= data;
  223. break;
  224. case ACCESS_WRITE_CLEARBIT:
  225. regval &= ~data;
  226. break;
  227. case ACCESS_WRITE_VALUE:
  228. regval = (regval & ~(mask << word_offset)) | data;
  229. break;
  230. }
  231. *reg = regval;
  232. } else {
  233. switch (ACCESS_READ_MASK(mode)) {
  234. case ACCESS_READ_RAZ:
  235. regval = 0;
  236. /* fall through */
  237. case ACCESS_READ_VALUE:
  238. mmio_data_write(mmio, mask, regval >> word_offset);
  239. }
  240. }
  241. }
  242. static bool handle_mmio_misc(struct kvm_vcpu *vcpu,
  243. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  244. {
  245. u32 reg;
  246. u32 word_offset = offset & 3;
  247. switch (offset & ~3) {
  248. case 0: /* CTLR */
  249. reg = vcpu->kvm->arch.vgic.enabled;
  250. vgic_reg_access(mmio, &reg, word_offset,
  251. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  252. if (mmio->is_write) {
  253. vcpu->kvm->arch.vgic.enabled = reg & 1;
  254. vgic_update_state(vcpu->kvm);
  255. return true;
  256. }
  257. break;
  258. case 4: /* TYPER */
  259. reg = (atomic_read(&vcpu->kvm->online_vcpus) - 1) << 5;
  260. reg |= (VGIC_NR_IRQS >> 5) - 1;
  261. vgic_reg_access(mmio, &reg, word_offset,
  262. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  263. break;
  264. case 8: /* IIDR */
  265. reg = 0x4B00043B;
  266. vgic_reg_access(mmio, &reg, word_offset,
  267. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  268. break;
  269. }
  270. return false;
  271. }
  272. static bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu,
  273. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  274. {
  275. vgic_reg_access(mmio, NULL, offset,
  276. ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
  277. return false;
  278. }
  279. static bool handle_mmio_set_enable_reg(struct kvm_vcpu *vcpu,
  280. struct kvm_exit_mmio *mmio,
  281. phys_addr_t offset)
  282. {
  283. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
  284. vcpu->vcpu_id, offset);
  285. vgic_reg_access(mmio, reg, offset,
  286. ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
  287. if (mmio->is_write) {
  288. vgic_update_state(vcpu->kvm);
  289. return true;
  290. }
  291. return false;
  292. }
  293. static bool handle_mmio_clear_enable_reg(struct kvm_vcpu *vcpu,
  294. struct kvm_exit_mmio *mmio,
  295. phys_addr_t offset)
  296. {
  297. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
  298. vcpu->vcpu_id, offset);
  299. vgic_reg_access(mmio, reg, offset,
  300. ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
  301. if (mmio->is_write) {
  302. if (offset < 4) /* Force SGI enabled */
  303. *reg |= 0xffff;
  304. vgic_update_state(vcpu->kvm);
  305. return true;
  306. }
  307. return false;
  308. }
  309. static bool handle_mmio_set_pending_reg(struct kvm_vcpu *vcpu,
  310. struct kvm_exit_mmio *mmio,
  311. phys_addr_t offset)
  312. {
  313. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_state,
  314. vcpu->vcpu_id, offset);
  315. vgic_reg_access(mmio, reg, offset,
  316. ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
  317. if (mmio->is_write) {
  318. vgic_update_state(vcpu->kvm);
  319. return true;
  320. }
  321. return false;
  322. }
  323. static bool handle_mmio_clear_pending_reg(struct kvm_vcpu *vcpu,
  324. struct kvm_exit_mmio *mmio,
  325. phys_addr_t offset)
  326. {
  327. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_state,
  328. vcpu->vcpu_id, offset);
  329. vgic_reg_access(mmio, reg, offset,
  330. ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
  331. if (mmio->is_write) {
  332. vgic_update_state(vcpu->kvm);
  333. return true;
  334. }
  335. return false;
  336. }
  337. static bool handle_mmio_priority_reg(struct kvm_vcpu *vcpu,
  338. struct kvm_exit_mmio *mmio,
  339. phys_addr_t offset)
  340. {
  341. u32 *reg = vgic_bytemap_get_reg(&vcpu->kvm->arch.vgic.irq_priority,
  342. vcpu->vcpu_id, offset);
  343. vgic_reg_access(mmio, reg, offset,
  344. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  345. return false;
  346. }
  347. #define GICD_ITARGETSR_SIZE 32
  348. #define GICD_CPUTARGETS_BITS 8
  349. #define GICD_IRQS_PER_ITARGETSR (GICD_ITARGETSR_SIZE / GICD_CPUTARGETS_BITS)
  350. static u32 vgic_get_target_reg(struct kvm *kvm, int irq)
  351. {
  352. struct vgic_dist *dist = &kvm->arch.vgic;
  353. struct kvm_vcpu *vcpu;
  354. int i, c;
  355. unsigned long *bmap;
  356. u32 val = 0;
  357. irq -= VGIC_NR_PRIVATE_IRQS;
  358. kvm_for_each_vcpu(c, vcpu, kvm) {
  359. bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
  360. for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++)
  361. if (test_bit(irq + i, bmap))
  362. val |= 1 << (c + i * 8);
  363. }
  364. return val;
  365. }
  366. static void vgic_set_target_reg(struct kvm *kvm, u32 val, int irq)
  367. {
  368. struct vgic_dist *dist = &kvm->arch.vgic;
  369. struct kvm_vcpu *vcpu;
  370. int i, c;
  371. unsigned long *bmap;
  372. u32 target;
  373. irq -= VGIC_NR_PRIVATE_IRQS;
  374. /*
  375. * Pick the LSB in each byte. This ensures we target exactly
  376. * one vcpu per IRQ. If the byte is null, assume we target
  377. * CPU0.
  378. */
  379. for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++) {
  380. int shift = i * GICD_CPUTARGETS_BITS;
  381. target = ffs((val >> shift) & 0xffU);
  382. target = target ? (target - 1) : 0;
  383. dist->irq_spi_cpu[irq + i] = target;
  384. kvm_for_each_vcpu(c, vcpu, kvm) {
  385. bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
  386. if (c == target)
  387. set_bit(irq + i, bmap);
  388. else
  389. clear_bit(irq + i, bmap);
  390. }
  391. }
  392. }
  393. static bool handle_mmio_target_reg(struct kvm_vcpu *vcpu,
  394. struct kvm_exit_mmio *mmio,
  395. phys_addr_t offset)
  396. {
  397. u32 reg;
  398. /* We treat the banked interrupts targets as read-only */
  399. if (offset < 32) {
  400. u32 roreg = 1 << vcpu->vcpu_id;
  401. roreg |= roreg << 8;
  402. roreg |= roreg << 16;
  403. vgic_reg_access(mmio, &roreg, offset,
  404. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  405. return false;
  406. }
  407. reg = vgic_get_target_reg(vcpu->kvm, offset & ~3U);
  408. vgic_reg_access(mmio, &reg, offset,
  409. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  410. if (mmio->is_write) {
  411. vgic_set_target_reg(vcpu->kvm, reg, offset & ~3U);
  412. vgic_update_state(vcpu->kvm);
  413. return true;
  414. }
  415. return false;
  416. }
  417. static u32 vgic_cfg_expand(u16 val)
  418. {
  419. u32 res = 0;
  420. int i;
  421. /*
  422. * Turn a 16bit value like abcd...mnop into a 32bit word
  423. * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
  424. */
  425. for (i = 0; i < 16; i++)
  426. res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
  427. return res;
  428. }
  429. static u16 vgic_cfg_compress(u32 val)
  430. {
  431. u16 res = 0;
  432. int i;
  433. /*
  434. * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
  435. * abcd...mnop which is what we really care about.
  436. */
  437. for (i = 0; i < 16; i++)
  438. res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
  439. return res;
  440. }
  441. /*
  442. * The distributor uses 2 bits per IRQ for the CFG register, but the
  443. * LSB is always 0. As such, we only keep the upper bit, and use the
  444. * two above functions to compress/expand the bits
  445. */
  446. static bool handle_mmio_cfg_reg(struct kvm_vcpu *vcpu,
  447. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  448. {
  449. u32 val;
  450. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_cfg,
  451. vcpu->vcpu_id, offset >> 1);
  452. if (offset & 2)
  453. val = *reg >> 16;
  454. else
  455. val = *reg & 0xffff;
  456. val = vgic_cfg_expand(val);
  457. vgic_reg_access(mmio, &val, offset,
  458. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  459. if (mmio->is_write) {
  460. if (offset < 4) {
  461. *reg = ~0U; /* Force PPIs/SGIs to 1 */
  462. return false;
  463. }
  464. val = vgic_cfg_compress(val);
  465. if (offset & 2) {
  466. *reg &= 0xffff;
  467. *reg |= val << 16;
  468. } else {
  469. *reg &= 0xffff << 16;
  470. *reg |= val;
  471. }
  472. }
  473. return false;
  474. }
  475. static bool handle_mmio_sgi_reg(struct kvm_vcpu *vcpu,
  476. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  477. {
  478. u32 reg;
  479. vgic_reg_access(mmio, &reg, offset,
  480. ACCESS_READ_RAZ | ACCESS_WRITE_VALUE);
  481. if (mmio->is_write) {
  482. vgic_dispatch_sgi(vcpu, reg);
  483. vgic_update_state(vcpu->kvm);
  484. return true;
  485. }
  486. return false;
  487. }
  488. /*
  489. * I would have liked to use the kvm_bus_io_*() API instead, but it
  490. * cannot cope with banked registers (only the VM pointer is passed
  491. * around, and we need the vcpu). One of these days, someone please
  492. * fix it!
  493. */
  494. struct mmio_range {
  495. phys_addr_t base;
  496. unsigned long len;
  497. bool (*handle_mmio)(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
  498. phys_addr_t offset);
  499. };
  500. static const struct mmio_range vgic_ranges[] = {
  501. {
  502. .base = GIC_DIST_CTRL,
  503. .len = 12,
  504. .handle_mmio = handle_mmio_misc,
  505. },
  506. {
  507. .base = GIC_DIST_IGROUP,
  508. .len = VGIC_NR_IRQS / 8,
  509. .handle_mmio = handle_mmio_raz_wi,
  510. },
  511. {
  512. .base = GIC_DIST_ENABLE_SET,
  513. .len = VGIC_NR_IRQS / 8,
  514. .handle_mmio = handle_mmio_set_enable_reg,
  515. },
  516. {
  517. .base = GIC_DIST_ENABLE_CLEAR,
  518. .len = VGIC_NR_IRQS / 8,
  519. .handle_mmio = handle_mmio_clear_enable_reg,
  520. },
  521. {
  522. .base = GIC_DIST_PENDING_SET,
  523. .len = VGIC_NR_IRQS / 8,
  524. .handle_mmio = handle_mmio_set_pending_reg,
  525. },
  526. {
  527. .base = GIC_DIST_PENDING_CLEAR,
  528. .len = VGIC_NR_IRQS / 8,
  529. .handle_mmio = handle_mmio_clear_pending_reg,
  530. },
  531. {
  532. .base = GIC_DIST_ACTIVE_SET,
  533. .len = VGIC_NR_IRQS / 8,
  534. .handle_mmio = handle_mmio_raz_wi,
  535. },
  536. {
  537. .base = GIC_DIST_ACTIVE_CLEAR,
  538. .len = VGIC_NR_IRQS / 8,
  539. .handle_mmio = handle_mmio_raz_wi,
  540. },
  541. {
  542. .base = GIC_DIST_PRI,
  543. .len = VGIC_NR_IRQS,
  544. .handle_mmio = handle_mmio_priority_reg,
  545. },
  546. {
  547. .base = GIC_DIST_TARGET,
  548. .len = VGIC_NR_IRQS,
  549. .handle_mmio = handle_mmio_target_reg,
  550. },
  551. {
  552. .base = GIC_DIST_CONFIG,
  553. .len = VGIC_NR_IRQS / 4,
  554. .handle_mmio = handle_mmio_cfg_reg,
  555. },
  556. {
  557. .base = GIC_DIST_SOFTINT,
  558. .len = 4,
  559. .handle_mmio = handle_mmio_sgi_reg,
  560. },
  561. {}
  562. };
  563. static const
  564. struct mmio_range *find_matching_range(const struct mmio_range *ranges,
  565. struct kvm_exit_mmio *mmio,
  566. phys_addr_t base)
  567. {
  568. const struct mmio_range *r = ranges;
  569. phys_addr_t addr = mmio->phys_addr - base;
  570. while (r->len) {
  571. if (addr >= r->base &&
  572. (addr + mmio->len) <= (r->base + r->len))
  573. return r;
  574. r++;
  575. }
  576. return NULL;
  577. }
  578. /**
  579. * vgic_handle_mmio - handle an in-kernel MMIO access
  580. * @vcpu: pointer to the vcpu performing the access
  581. * @run: pointer to the kvm_run structure
  582. * @mmio: pointer to the data describing the access
  583. *
  584. * returns true if the MMIO access has been performed in kernel space,
  585. * and false if it needs to be emulated in user space.
  586. */
  587. bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  588. struct kvm_exit_mmio *mmio)
  589. {
  590. const struct mmio_range *range;
  591. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  592. unsigned long base = dist->vgic_dist_base;
  593. bool updated_state;
  594. unsigned long offset;
  595. if (!irqchip_in_kernel(vcpu->kvm) ||
  596. mmio->phys_addr < base ||
  597. (mmio->phys_addr + mmio->len) > (base + KVM_VGIC_V2_DIST_SIZE))
  598. return false;
  599. /* We don't support ldrd / strd or ldm / stm to the emulated vgic */
  600. if (mmio->len > 4) {
  601. kvm_inject_dabt(vcpu, mmio->phys_addr);
  602. return true;
  603. }
  604. range = find_matching_range(vgic_ranges, mmio, base);
  605. if (unlikely(!range || !range->handle_mmio)) {
  606. pr_warn("Unhandled access %d %08llx %d\n",
  607. mmio->is_write, mmio->phys_addr, mmio->len);
  608. return false;
  609. }
  610. spin_lock(&vcpu->kvm->arch.vgic.lock);
  611. offset = mmio->phys_addr - range->base - base;
  612. updated_state = range->handle_mmio(vcpu, mmio, offset);
  613. spin_unlock(&vcpu->kvm->arch.vgic.lock);
  614. kvm_prepare_mmio(run, mmio);
  615. kvm_handle_mmio_return(vcpu, run);
  616. return true;
  617. }
  618. static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg)
  619. {
  620. struct kvm *kvm = vcpu->kvm;
  621. struct vgic_dist *dist = &kvm->arch.vgic;
  622. int nrcpus = atomic_read(&kvm->online_vcpus);
  623. u8 target_cpus;
  624. int sgi, mode, c, vcpu_id;
  625. vcpu_id = vcpu->vcpu_id;
  626. sgi = reg & 0xf;
  627. target_cpus = (reg >> 16) & 0xff;
  628. mode = (reg >> 24) & 3;
  629. switch (mode) {
  630. case 0:
  631. if (!target_cpus)
  632. return;
  633. case 1:
  634. target_cpus = ((1 << nrcpus) - 1) & ~(1 << vcpu_id) & 0xff;
  635. break;
  636. case 2:
  637. target_cpus = 1 << vcpu_id;
  638. break;
  639. }
  640. kvm_for_each_vcpu(c, vcpu, kvm) {
  641. if (target_cpus & 1) {
  642. /* Flag the SGI as pending */
  643. vgic_dist_irq_set(vcpu, sgi);
  644. dist->irq_sgi_sources[c][sgi] |= 1 << vcpu_id;
  645. kvm_debug("SGI%d from CPU%d to CPU%d\n", sgi, vcpu_id, c);
  646. }
  647. target_cpus >>= 1;
  648. }
  649. }
  650. static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
  651. {
  652. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  653. unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
  654. unsigned long pending_private, pending_shared;
  655. int vcpu_id;
  656. vcpu_id = vcpu->vcpu_id;
  657. pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
  658. pend_shared = vcpu->arch.vgic_cpu.pending_shared;
  659. pending = vgic_bitmap_get_cpu_map(&dist->irq_state, vcpu_id);
  660. enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
  661. bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
  662. pending = vgic_bitmap_get_shared_map(&dist->irq_state);
  663. enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
  664. bitmap_and(pend_shared, pending, enabled, VGIC_NR_SHARED_IRQS);
  665. bitmap_and(pend_shared, pend_shared,
  666. vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
  667. VGIC_NR_SHARED_IRQS);
  668. pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
  669. pending_shared = find_first_bit(pend_shared, VGIC_NR_SHARED_IRQS);
  670. return (pending_private < VGIC_NR_PRIVATE_IRQS ||
  671. pending_shared < VGIC_NR_SHARED_IRQS);
  672. }
  673. /*
  674. * Update the interrupt state and determine which CPUs have pending
  675. * interrupts. Must be called with distributor lock held.
  676. */
  677. static void vgic_update_state(struct kvm *kvm)
  678. {
  679. struct vgic_dist *dist = &kvm->arch.vgic;
  680. struct kvm_vcpu *vcpu;
  681. int c;
  682. if (!dist->enabled) {
  683. set_bit(0, &dist->irq_pending_on_cpu);
  684. return;
  685. }
  686. kvm_for_each_vcpu(c, vcpu, kvm) {
  687. if (compute_pending_for_cpu(vcpu)) {
  688. pr_debug("CPU%d has pending interrupts\n", c);
  689. set_bit(c, &dist->irq_pending_on_cpu);
  690. }
  691. }
  692. }
  693. #define LR_CPUID(lr) \
  694. (((lr) & GICH_LR_PHYSID_CPUID) >> GICH_LR_PHYSID_CPUID_SHIFT)
  695. #define MK_LR_PEND(src, irq) \
  696. (GICH_LR_PENDING_BIT | ((src) << GICH_LR_PHYSID_CPUID_SHIFT) | (irq))
  697. /*
  698. * Queue an interrupt to a CPU virtual interface. Return true on success,
  699. * or false if it wasn't possible to queue it.
  700. */
  701. static bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
  702. {
  703. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  704. int lr;
  705. /* Sanitize the input... */
  706. BUG_ON(sgi_source_id & ~7);
  707. BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
  708. BUG_ON(irq >= VGIC_NR_IRQS);
  709. kvm_debug("Queue IRQ%d\n", irq);
  710. lr = vgic_cpu->vgic_irq_lr_map[irq];
  711. /* Do we have an active interrupt for the same CPUID? */
  712. if (lr != LR_EMPTY &&
  713. (LR_CPUID(vgic_cpu->vgic_lr[lr]) == sgi_source_id)) {
  714. kvm_debug("LR%d piggyback for IRQ%d %x\n",
  715. lr, irq, vgic_cpu->vgic_lr[lr]);
  716. BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
  717. vgic_cpu->vgic_lr[lr] |= GICH_LR_PENDING_BIT;
  718. goto out;
  719. }
  720. /* Try to use another LR for this interrupt */
  721. lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
  722. vgic_cpu->nr_lr);
  723. if (lr >= vgic_cpu->nr_lr)
  724. return false;
  725. kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
  726. vgic_cpu->vgic_lr[lr] = MK_LR_PEND(sgi_source_id, irq);
  727. vgic_cpu->vgic_irq_lr_map[irq] = lr;
  728. set_bit(lr, vgic_cpu->lr_used);
  729. out:
  730. if (!vgic_irq_is_edge(vcpu, irq))
  731. vgic_cpu->vgic_lr[lr] |= GICH_LR_EOI;
  732. return true;
  733. }
  734. static bool vgic_queue_sgi(struct kvm_vcpu *vcpu, int irq)
  735. {
  736. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  737. unsigned long sources;
  738. int vcpu_id = vcpu->vcpu_id;
  739. int c;
  740. sources = dist->irq_sgi_sources[vcpu_id][irq];
  741. for_each_set_bit(c, &sources, VGIC_MAX_CPUS) {
  742. if (vgic_queue_irq(vcpu, c, irq))
  743. clear_bit(c, &sources);
  744. }
  745. dist->irq_sgi_sources[vcpu_id][irq] = sources;
  746. /*
  747. * If the sources bitmap has been cleared it means that we
  748. * could queue all the SGIs onto link registers (see the
  749. * clear_bit above), and therefore we are done with them in
  750. * our emulated gic and can get rid of them.
  751. */
  752. if (!sources) {
  753. vgic_dist_irq_clear(vcpu, irq);
  754. vgic_cpu_irq_clear(vcpu, irq);
  755. return true;
  756. }
  757. return false;
  758. }
  759. static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
  760. {
  761. if (vgic_irq_is_active(vcpu, irq))
  762. return true; /* level interrupt, already queued */
  763. if (vgic_queue_irq(vcpu, 0, irq)) {
  764. if (vgic_irq_is_edge(vcpu, irq)) {
  765. vgic_dist_irq_clear(vcpu, irq);
  766. vgic_cpu_irq_clear(vcpu, irq);
  767. } else {
  768. vgic_irq_set_active(vcpu, irq);
  769. }
  770. return true;
  771. }
  772. return false;
  773. }
  774. /*
  775. * Fill the list registers with pending interrupts before running the
  776. * guest.
  777. */
  778. static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  779. {
  780. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  781. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  782. int i, vcpu_id;
  783. int overflow = 0;
  784. vcpu_id = vcpu->vcpu_id;
  785. /*
  786. * We may not have any pending interrupt, or the interrupts
  787. * may have been serviced from another vcpu. In all cases,
  788. * move along.
  789. */
  790. if (!kvm_vgic_vcpu_pending_irq(vcpu)) {
  791. pr_debug("CPU%d has no pending interrupt\n", vcpu_id);
  792. goto epilog;
  793. }
  794. /* SGIs */
  795. for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) {
  796. if (!vgic_queue_sgi(vcpu, i))
  797. overflow = 1;
  798. }
  799. /* PPIs */
  800. for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) {
  801. if (!vgic_queue_hwirq(vcpu, i))
  802. overflow = 1;
  803. }
  804. /* SPIs */
  805. for_each_set_bit(i, vgic_cpu->pending_shared, VGIC_NR_SHARED_IRQS) {
  806. if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
  807. overflow = 1;
  808. }
  809. epilog:
  810. if (overflow) {
  811. vgic_cpu->vgic_hcr |= GICH_HCR_UIE;
  812. } else {
  813. vgic_cpu->vgic_hcr &= ~GICH_HCR_UIE;
  814. /*
  815. * We're about to run this VCPU, and we've consumed
  816. * everything the distributor had in store for
  817. * us. Claim we don't have anything pending. We'll
  818. * adjust that if needed while exiting.
  819. */
  820. clear_bit(vcpu_id, &dist->irq_pending_on_cpu);
  821. }
  822. }
  823. static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
  824. {
  825. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  826. bool level_pending = false;
  827. kvm_debug("MISR = %08x\n", vgic_cpu->vgic_misr);
  828. /*
  829. * We do not need to take the distributor lock here, since the only
  830. * action we perform is clearing the irq_active_bit for an EOIed
  831. * level interrupt. There is a potential race with
  832. * the queuing of an interrupt in __kvm_vgic_flush_hwstate(), where we
  833. * check if the interrupt is already active. Two possibilities:
  834. *
  835. * - The queuing is occurring on the same vcpu: cannot happen,
  836. * as we're already in the context of this vcpu, and
  837. * executing the handler
  838. * - The interrupt has been migrated to another vcpu, and we
  839. * ignore this interrupt for this run. Big deal. It is still
  840. * pending though, and will get considered when this vcpu
  841. * exits.
  842. */
  843. if (vgic_cpu->vgic_misr & GICH_MISR_EOI) {
  844. /*
  845. * Some level interrupts have been EOIed. Clear their
  846. * active bit.
  847. */
  848. int lr, irq;
  849. for_each_set_bit(lr, (unsigned long *)vgic_cpu->vgic_eisr,
  850. vgic_cpu->nr_lr) {
  851. irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
  852. vgic_irq_clear_active(vcpu, irq);
  853. vgic_cpu->vgic_lr[lr] &= ~GICH_LR_EOI;
  854. /* Any additional pending interrupt? */
  855. if (vgic_dist_irq_is_pending(vcpu, irq)) {
  856. vgic_cpu_irq_set(vcpu, irq);
  857. level_pending = true;
  858. } else {
  859. vgic_cpu_irq_clear(vcpu, irq);
  860. }
  861. }
  862. }
  863. if (vgic_cpu->vgic_misr & GICH_MISR_U)
  864. vgic_cpu->vgic_hcr &= ~GICH_HCR_UIE;
  865. return level_pending;
  866. }
  867. /*
  868. * Sync back the VGIC state after a guest run. We do not really touch
  869. * the distributor here (the irq_pending_on_cpu bit is safe to set),
  870. * so there is no need for taking its lock.
  871. */
  872. static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  873. {
  874. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  875. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  876. int lr, pending;
  877. bool level_pending;
  878. level_pending = vgic_process_maintenance(vcpu);
  879. /* Clear mappings for empty LRs */
  880. for_each_set_bit(lr, (unsigned long *)vgic_cpu->vgic_elrsr,
  881. vgic_cpu->nr_lr) {
  882. int irq;
  883. if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
  884. continue;
  885. irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
  886. BUG_ON(irq >= VGIC_NR_IRQS);
  887. vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
  888. }
  889. /* Check if we still have something up our sleeve... */
  890. pending = find_first_zero_bit((unsigned long *)vgic_cpu->vgic_elrsr,
  891. vgic_cpu->nr_lr);
  892. if (level_pending || pending < vgic_cpu->nr_lr)
  893. set_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
  894. }
  895. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  896. {
  897. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  898. if (!irqchip_in_kernel(vcpu->kvm))
  899. return;
  900. spin_lock(&dist->lock);
  901. __kvm_vgic_flush_hwstate(vcpu);
  902. spin_unlock(&dist->lock);
  903. }
  904. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  905. {
  906. if (!irqchip_in_kernel(vcpu->kvm))
  907. return;
  908. __kvm_vgic_sync_hwstate(vcpu);
  909. }
  910. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  911. {
  912. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  913. if (!irqchip_in_kernel(vcpu->kvm))
  914. return 0;
  915. return test_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
  916. }
  917. static bool vgic_ioaddr_overlap(struct kvm *kvm)
  918. {
  919. phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
  920. phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
  921. if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
  922. return 0;
  923. if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
  924. (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
  925. return -EBUSY;
  926. return 0;
  927. }
  928. static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
  929. phys_addr_t addr, phys_addr_t size)
  930. {
  931. int ret;
  932. if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
  933. return -EEXIST;
  934. if (addr + size < addr)
  935. return -EINVAL;
  936. ret = vgic_ioaddr_overlap(kvm);
  937. if (ret)
  938. return ret;
  939. *ioaddr = addr;
  940. return ret;
  941. }
  942. int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr)
  943. {
  944. int r = 0;
  945. struct vgic_dist *vgic = &kvm->arch.vgic;
  946. if (addr & ~KVM_PHYS_MASK)
  947. return -E2BIG;
  948. if (addr & ~PAGE_MASK)
  949. return -EINVAL;
  950. mutex_lock(&kvm->lock);
  951. switch (type) {
  952. case KVM_VGIC_V2_ADDR_TYPE_DIST:
  953. r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
  954. addr, KVM_VGIC_V2_DIST_SIZE);
  955. break;
  956. case KVM_VGIC_V2_ADDR_TYPE_CPU:
  957. r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
  958. addr, KVM_VGIC_V2_CPU_SIZE);
  959. break;
  960. default:
  961. r = -ENODEV;
  962. }
  963. mutex_unlock(&kvm->lock);
  964. return r;
  965. }