vgic.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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/cpu.h>
  19. #include <linux/kvm.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/io.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/irqchip/arm-gic.h>
  27. #include <asm/kvm_emulate.h>
  28. #include <asm/kvm_arm.h>
  29. #include <asm/kvm_mmu.h>
  30. /*
  31. * How the whole thing works (courtesy of Christoffer Dall):
  32. *
  33. * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if
  34. * something is pending
  35. * - VGIC pending interrupts are stored on the vgic.irq_state vgic
  36. * bitmap (this bitmap is updated by both user land ioctls and guest
  37. * mmio ops, and other in-kernel peripherals such as the
  38. * arch. timers) and indicate the 'wire' state.
  39. * - Every time the bitmap changes, the irq_pending_on_cpu oracle is
  40. * recalculated
  41. * - To calculate the oracle, we need info for each cpu from
  42. * compute_pending_for_cpu, which considers:
  43. * - PPI: dist->irq_state & dist->irq_enable
  44. * - SPI: dist->irq_state & dist->irq_enable & dist->irq_spi_target
  45. * - irq_spi_target is a 'formatted' version of the GICD_ICFGR
  46. * registers, stored on each vcpu. We only keep one bit of
  47. * information per interrupt, making sure that only one vcpu can
  48. * accept the interrupt.
  49. * - The same is true when injecting an interrupt, except that we only
  50. * consider a single interrupt at a time. The irq_spi_cpu array
  51. * contains the target CPU for each SPI.
  52. *
  53. * The handling of level interrupts adds some extra complexity. We
  54. * need to track when the interrupt has been EOIed, so we can sample
  55. * the 'line' again. This is achieved as such:
  56. *
  57. * - When a level interrupt is moved onto a vcpu, the corresponding
  58. * bit in irq_active is set. As long as this bit is set, the line
  59. * will be ignored for further interrupts. The interrupt is injected
  60. * into the vcpu with the GICH_LR_EOI bit set (generate a
  61. * maintenance interrupt on EOI).
  62. * - When the interrupt is EOIed, the maintenance interrupt fires,
  63. * and clears the corresponding bit in irq_active. This allow the
  64. * interrupt line to be sampled again.
  65. */
  66. #define VGIC_ADDR_UNDEF (-1)
  67. #define IS_VGIC_ADDR_UNDEF(_x) ((_x) == VGIC_ADDR_UNDEF)
  68. /* Physical address of vgic virtual cpu interface */
  69. static phys_addr_t vgic_vcpu_base;
  70. /* Virtual control interface base address */
  71. static void __iomem *vgic_vctrl_base;
  72. static struct device_node *vgic_node;
  73. #define ACCESS_READ_VALUE (1 << 0)
  74. #define ACCESS_READ_RAZ (0 << 0)
  75. #define ACCESS_READ_MASK(x) ((x) & (1 << 0))
  76. #define ACCESS_WRITE_IGNORED (0 << 1)
  77. #define ACCESS_WRITE_SETBIT (1 << 1)
  78. #define ACCESS_WRITE_CLEARBIT (2 << 1)
  79. #define ACCESS_WRITE_VALUE (3 << 1)
  80. #define ACCESS_WRITE_MASK(x) ((x) & (3 << 1))
  81. static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
  82. static void vgic_update_state(struct kvm *kvm);
  83. static void vgic_kick_vcpus(struct kvm *kvm);
  84. static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg);
  85. static u32 vgic_nr_lr;
  86. static unsigned int vgic_maint_irq;
  87. static u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x,
  88. int cpuid, u32 offset)
  89. {
  90. offset >>= 2;
  91. if (!offset)
  92. return x->percpu[cpuid].reg;
  93. else
  94. return x->shared.reg + offset - 1;
  95. }
  96. static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
  97. int cpuid, int irq)
  98. {
  99. if (irq < VGIC_NR_PRIVATE_IRQS)
  100. return test_bit(irq, x->percpu[cpuid].reg_ul);
  101. return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared.reg_ul);
  102. }
  103. static void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
  104. int irq, int val)
  105. {
  106. unsigned long *reg;
  107. if (irq < VGIC_NR_PRIVATE_IRQS) {
  108. reg = x->percpu[cpuid].reg_ul;
  109. } else {
  110. reg = x->shared.reg_ul;
  111. irq -= VGIC_NR_PRIVATE_IRQS;
  112. }
  113. if (val)
  114. set_bit(irq, reg);
  115. else
  116. clear_bit(irq, reg);
  117. }
  118. static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
  119. {
  120. if (unlikely(cpuid >= VGIC_MAX_CPUS))
  121. return NULL;
  122. return x->percpu[cpuid].reg_ul;
  123. }
  124. static unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
  125. {
  126. return x->shared.reg_ul;
  127. }
  128. static u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
  129. {
  130. offset >>= 2;
  131. BUG_ON(offset > (VGIC_NR_IRQS / 4));
  132. if (offset < 8)
  133. return x->percpu[cpuid] + offset;
  134. else
  135. return x->shared + offset - 8;
  136. }
  137. #define VGIC_CFG_LEVEL 0
  138. #define VGIC_CFG_EDGE 1
  139. static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
  140. {
  141. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  142. int irq_val;
  143. irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
  144. return irq_val == VGIC_CFG_EDGE;
  145. }
  146. static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
  147. {
  148. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  149. return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
  150. }
  151. static int vgic_irq_is_active(struct kvm_vcpu *vcpu, int irq)
  152. {
  153. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  154. return vgic_bitmap_get_irq_val(&dist->irq_active, vcpu->vcpu_id, irq);
  155. }
  156. static void vgic_irq_set_active(struct kvm_vcpu *vcpu, int irq)
  157. {
  158. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  159. vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 1);
  160. }
  161. static void vgic_irq_clear_active(struct kvm_vcpu *vcpu, int irq)
  162. {
  163. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  164. vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 0);
  165. }
  166. static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
  167. {
  168. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  169. return vgic_bitmap_get_irq_val(&dist->irq_state, vcpu->vcpu_id, irq);
  170. }
  171. static void vgic_dist_irq_set(struct kvm_vcpu *vcpu, int irq)
  172. {
  173. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  174. vgic_bitmap_set_irq_val(&dist->irq_state, vcpu->vcpu_id, irq, 1);
  175. }
  176. static void vgic_dist_irq_clear(struct kvm_vcpu *vcpu, int irq)
  177. {
  178. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  179. vgic_bitmap_set_irq_val(&dist->irq_state, vcpu->vcpu_id, irq, 0);
  180. }
  181. static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
  182. {
  183. if (irq < VGIC_NR_PRIVATE_IRQS)
  184. set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
  185. else
  186. set_bit(irq - VGIC_NR_PRIVATE_IRQS,
  187. vcpu->arch.vgic_cpu.pending_shared);
  188. }
  189. static void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
  190. {
  191. if (irq < VGIC_NR_PRIVATE_IRQS)
  192. clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
  193. else
  194. clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
  195. vcpu->arch.vgic_cpu.pending_shared);
  196. }
  197. static u32 mmio_data_read(struct kvm_exit_mmio *mmio, u32 mask)
  198. {
  199. return *((u32 *)mmio->data) & mask;
  200. }
  201. static void mmio_data_write(struct kvm_exit_mmio *mmio, u32 mask, u32 value)
  202. {
  203. *((u32 *)mmio->data) = value & mask;
  204. }
  205. /**
  206. * vgic_reg_access - access vgic register
  207. * @mmio: pointer to the data describing the mmio access
  208. * @reg: pointer to the virtual backing of vgic distributor data
  209. * @offset: least significant 2 bits used for word offset
  210. * @mode: ACCESS_ mode (see defines above)
  211. *
  212. * Helper to make vgic register access easier using one of the access
  213. * modes defined for vgic register access
  214. * (read,raz,write-ignored,setbit,clearbit,write)
  215. */
  216. static void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
  217. phys_addr_t offset, int mode)
  218. {
  219. int word_offset = (offset & 3) * 8;
  220. u32 mask = (1UL << (mmio->len * 8)) - 1;
  221. u32 regval;
  222. /*
  223. * Any alignment fault should have been delivered to the guest
  224. * directly (ARM ARM B3.12.7 "Prioritization of aborts").
  225. */
  226. if (reg) {
  227. regval = *reg;
  228. } else {
  229. BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
  230. regval = 0;
  231. }
  232. if (mmio->is_write) {
  233. u32 data = mmio_data_read(mmio, mask) << word_offset;
  234. switch (ACCESS_WRITE_MASK(mode)) {
  235. case ACCESS_WRITE_IGNORED:
  236. return;
  237. case ACCESS_WRITE_SETBIT:
  238. regval |= data;
  239. break;
  240. case ACCESS_WRITE_CLEARBIT:
  241. regval &= ~data;
  242. break;
  243. case ACCESS_WRITE_VALUE:
  244. regval = (regval & ~(mask << word_offset)) | data;
  245. break;
  246. }
  247. *reg = regval;
  248. } else {
  249. switch (ACCESS_READ_MASK(mode)) {
  250. case ACCESS_READ_RAZ:
  251. regval = 0;
  252. /* fall through */
  253. case ACCESS_READ_VALUE:
  254. mmio_data_write(mmio, mask, regval >> word_offset);
  255. }
  256. }
  257. }
  258. static bool handle_mmio_misc(struct kvm_vcpu *vcpu,
  259. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  260. {
  261. u32 reg;
  262. u32 word_offset = offset & 3;
  263. switch (offset & ~3) {
  264. case 0: /* CTLR */
  265. reg = vcpu->kvm->arch.vgic.enabled;
  266. vgic_reg_access(mmio, &reg, word_offset,
  267. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  268. if (mmio->is_write) {
  269. vcpu->kvm->arch.vgic.enabled = reg & 1;
  270. vgic_update_state(vcpu->kvm);
  271. return true;
  272. }
  273. break;
  274. case 4: /* TYPER */
  275. reg = (atomic_read(&vcpu->kvm->online_vcpus) - 1) << 5;
  276. reg |= (VGIC_NR_IRQS >> 5) - 1;
  277. vgic_reg_access(mmio, &reg, word_offset,
  278. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  279. break;
  280. case 8: /* IIDR */
  281. reg = 0x4B00043B;
  282. vgic_reg_access(mmio, &reg, word_offset,
  283. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  284. break;
  285. }
  286. return false;
  287. }
  288. static bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu,
  289. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  290. {
  291. vgic_reg_access(mmio, NULL, offset,
  292. ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
  293. return false;
  294. }
  295. static bool handle_mmio_set_enable_reg(struct kvm_vcpu *vcpu,
  296. struct kvm_exit_mmio *mmio,
  297. phys_addr_t offset)
  298. {
  299. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
  300. vcpu->vcpu_id, offset);
  301. vgic_reg_access(mmio, reg, offset,
  302. ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
  303. if (mmio->is_write) {
  304. vgic_update_state(vcpu->kvm);
  305. return true;
  306. }
  307. return false;
  308. }
  309. static bool handle_mmio_clear_enable_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_enabled,
  314. vcpu->vcpu_id, offset);
  315. vgic_reg_access(mmio, reg, offset,
  316. ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
  317. if (mmio->is_write) {
  318. if (offset < 4) /* Force SGI enabled */
  319. *reg |= 0xffff;
  320. vgic_retire_disabled_irqs(vcpu);
  321. vgic_update_state(vcpu->kvm);
  322. return true;
  323. }
  324. return false;
  325. }
  326. static bool handle_mmio_set_pending_reg(struct kvm_vcpu *vcpu,
  327. struct kvm_exit_mmio *mmio,
  328. phys_addr_t offset)
  329. {
  330. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_state,
  331. vcpu->vcpu_id, offset);
  332. vgic_reg_access(mmio, reg, offset,
  333. ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
  334. if (mmio->is_write) {
  335. vgic_update_state(vcpu->kvm);
  336. return true;
  337. }
  338. return false;
  339. }
  340. static bool handle_mmio_clear_pending_reg(struct kvm_vcpu *vcpu,
  341. struct kvm_exit_mmio *mmio,
  342. phys_addr_t offset)
  343. {
  344. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_state,
  345. vcpu->vcpu_id, offset);
  346. vgic_reg_access(mmio, reg, offset,
  347. ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
  348. if (mmio->is_write) {
  349. vgic_update_state(vcpu->kvm);
  350. return true;
  351. }
  352. return false;
  353. }
  354. static bool handle_mmio_priority_reg(struct kvm_vcpu *vcpu,
  355. struct kvm_exit_mmio *mmio,
  356. phys_addr_t offset)
  357. {
  358. u32 *reg = vgic_bytemap_get_reg(&vcpu->kvm->arch.vgic.irq_priority,
  359. vcpu->vcpu_id, offset);
  360. vgic_reg_access(mmio, reg, offset,
  361. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  362. return false;
  363. }
  364. #define GICD_ITARGETSR_SIZE 32
  365. #define GICD_CPUTARGETS_BITS 8
  366. #define GICD_IRQS_PER_ITARGETSR (GICD_ITARGETSR_SIZE / GICD_CPUTARGETS_BITS)
  367. static u32 vgic_get_target_reg(struct kvm *kvm, int irq)
  368. {
  369. struct vgic_dist *dist = &kvm->arch.vgic;
  370. int i;
  371. u32 val = 0;
  372. irq -= VGIC_NR_PRIVATE_IRQS;
  373. for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++)
  374. val |= 1 << (dist->irq_spi_cpu[irq + i] + i * 8);
  375. return val;
  376. }
  377. static void vgic_set_target_reg(struct kvm *kvm, u32 val, int irq)
  378. {
  379. struct vgic_dist *dist = &kvm->arch.vgic;
  380. struct kvm_vcpu *vcpu;
  381. int i, c;
  382. unsigned long *bmap;
  383. u32 target;
  384. irq -= VGIC_NR_PRIVATE_IRQS;
  385. /*
  386. * Pick the LSB in each byte. This ensures we target exactly
  387. * one vcpu per IRQ. If the byte is null, assume we target
  388. * CPU0.
  389. */
  390. for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++) {
  391. int shift = i * GICD_CPUTARGETS_BITS;
  392. target = ffs((val >> shift) & 0xffU);
  393. target = target ? (target - 1) : 0;
  394. dist->irq_spi_cpu[irq + i] = target;
  395. kvm_for_each_vcpu(c, vcpu, kvm) {
  396. bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
  397. if (c == target)
  398. set_bit(irq + i, bmap);
  399. else
  400. clear_bit(irq + i, bmap);
  401. }
  402. }
  403. }
  404. static bool handle_mmio_target_reg(struct kvm_vcpu *vcpu,
  405. struct kvm_exit_mmio *mmio,
  406. phys_addr_t offset)
  407. {
  408. u32 reg;
  409. /* We treat the banked interrupts targets as read-only */
  410. if (offset < 32) {
  411. u32 roreg = 1 << vcpu->vcpu_id;
  412. roreg |= roreg << 8;
  413. roreg |= roreg << 16;
  414. vgic_reg_access(mmio, &roreg, offset,
  415. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  416. return false;
  417. }
  418. reg = vgic_get_target_reg(vcpu->kvm, offset & ~3U);
  419. vgic_reg_access(mmio, &reg, offset,
  420. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  421. if (mmio->is_write) {
  422. vgic_set_target_reg(vcpu->kvm, reg, offset & ~3U);
  423. vgic_update_state(vcpu->kvm);
  424. return true;
  425. }
  426. return false;
  427. }
  428. static u32 vgic_cfg_expand(u16 val)
  429. {
  430. u32 res = 0;
  431. int i;
  432. /*
  433. * Turn a 16bit value like abcd...mnop into a 32bit word
  434. * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
  435. */
  436. for (i = 0; i < 16; i++)
  437. res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
  438. return res;
  439. }
  440. static u16 vgic_cfg_compress(u32 val)
  441. {
  442. u16 res = 0;
  443. int i;
  444. /*
  445. * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
  446. * abcd...mnop which is what we really care about.
  447. */
  448. for (i = 0; i < 16; i++)
  449. res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
  450. return res;
  451. }
  452. /*
  453. * The distributor uses 2 bits per IRQ for the CFG register, but the
  454. * LSB is always 0. As such, we only keep the upper bit, and use the
  455. * two above functions to compress/expand the bits
  456. */
  457. static bool handle_mmio_cfg_reg(struct kvm_vcpu *vcpu,
  458. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  459. {
  460. u32 val;
  461. u32 *reg;
  462. offset >>= 1;
  463. reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_cfg,
  464. vcpu->vcpu_id, offset);
  465. if (offset & 2)
  466. val = *reg >> 16;
  467. else
  468. val = *reg & 0xffff;
  469. val = vgic_cfg_expand(val);
  470. vgic_reg_access(mmio, &val, offset,
  471. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  472. if (mmio->is_write) {
  473. if (offset < 4) {
  474. *reg = ~0U; /* Force PPIs/SGIs to 1 */
  475. return false;
  476. }
  477. val = vgic_cfg_compress(val);
  478. if (offset & 2) {
  479. *reg &= 0xffff;
  480. *reg |= val << 16;
  481. } else {
  482. *reg &= 0xffff << 16;
  483. *reg |= val;
  484. }
  485. }
  486. return false;
  487. }
  488. static bool handle_mmio_sgi_reg(struct kvm_vcpu *vcpu,
  489. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  490. {
  491. u32 reg;
  492. vgic_reg_access(mmio, &reg, offset,
  493. ACCESS_READ_RAZ | ACCESS_WRITE_VALUE);
  494. if (mmio->is_write) {
  495. vgic_dispatch_sgi(vcpu, reg);
  496. vgic_update_state(vcpu->kvm);
  497. return true;
  498. }
  499. return false;
  500. }
  501. /*
  502. * I would have liked to use the kvm_bus_io_*() API instead, but it
  503. * cannot cope with banked registers (only the VM pointer is passed
  504. * around, and we need the vcpu). One of these days, someone please
  505. * fix it!
  506. */
  507. struct mmio_range {
  508. phys_addr_t base;
  509. unsigned long len;
  510. bool (*handle_mmio)(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
  511. phys_addr_t offset);
  512. };
  513. static const struct mmio_range vgic_ranges[] = {
  514. {
  515. .base = GIC_DIST_CTRL,
  516. .len = 12,
  517. .handle_mmio = handle_mmio_misc,
  518. },
  519. {
  520. .base = GIC_DIST_IGROUP,
  521. .len = VGIC_NR_IRQS / 8,
  522. .handle_mmio = handle_mmio_raz_wi,
  523. },
  524. {
  525. .base = GIC_DIST_ENABLE_SET,
  526. .len = VGIC_NR_IRQS / 8,
  527. .handle_mmio = handle_mmio_set_enable_reg,
  528. },
  529. {
  530. .base = GIC_DIST_ENABLE_CLEAR,
  531. .len = VGIC_NR_IRQS / 8,
  532. .handle_mmio = handle_mmio_clear_enable_reg,
  533. },
  534. {
  535. .base = GIC_DIST_PENDING_SET,
  536. .len = VGIC_NR_IRQS / 8,
  537. .handle_mmio = handle_mmio_set_pending_reg,
  538. },
  539. {
  540. .base = GIC_DIST_PENDING_CLEAR,
  541. .len = VGIC_NR_IRQS / 8,
  542. .handle_mmio = handle_mmio_clear_pending_reg,
  543. },
  544. {
  545. .base = GIC_DIST_ACTIVE_SET,
  546. .len = VGIC_NR_IRQS / 8,
  547. .handle_mmio = handle_mmio_raz_wi,
  548. },
  549. {
  550. .base = GIC_DIST_ACTIVE_CLEAR,
  551. .len = VGIC_NR_IRQS / 8,
  552. .handle_mmio = handle_mmio_raz_wi,
  553. },
  554. {
  555. .base = GIC_DIST_PRI,
  556. .len = VGIC_NR_IRQS,
  557. .handle_mmio = handle_mmio_priority_reg,
  558. },
  559. {
  560. .base = GIC_DIST_TARGET,
  561. .len = VGIC_NR_IRQS,
  562. .handle_mmio = handle_mmio_target_reg,
  563. },
  564. {
  565. .base = GIC_DIST_CONFIG,
  566. .len = VGIC_NR_IRQS / 4,
  567. .handle_mmio = handle_mmio_cfg_reg,
  568. },
  569. {
  570. .base = GIC_DIST_SOFTINT,
  571. .len = 4,
  572. .handle_mmio = handle_mmio_sgi_reg,
  573. },
  574. {}
  575. };
  576. static const
  577. struct mmio_range *find_matching_range(const struct mmio_range *ranges,
  578. struct kvm_exit_mmio *mmio,
  579. phys_addr_t base)
  580. {
  581. const struct mmio_range *r = ranges;
  582. phys_addr_t addr = mmio->phys_addr - base;
  583. while (r->len) {
  584. if (addr >= r->base &&
  585. (addr + mmio->len) <= (r->base + r->len))
  586. return r;
  587. r++;
  588. }
  589. return NULL;
  590. }
  591. /**
  592. * vgic_handle_mmio - handle an in-kernel MMIO access
  593. * @vcpu: pointer to the vcpu performing the access
  594. * @run: pointer to the kvm_run structure
  595. * @mmio: pointer to the data describing the access
  596. *
  597. * returns true if the MMIO access has been performed in kernel space,
  598. * and false if it needs to be emulated in user space.
  599. */
  600. bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  601. struct kvm_exit_mmio *mmio)
  602. {
  603. const struct mmio_range *range;
  604. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  605. unsigned long base = dist->vgic_dist_base;
  606. bool updated_state;
  607. unsigned long offset;
  608. if (!irqchip_in_kernel(vcpu->kvm) ||
  609. mmio->phys_addr < base ||
  610. (mmio->phys_addr + mmio->len) > (base + KVM_VGIC_V2_DIST_SIZE))
  611. return false;
  612. /* We don't support ldrd / strd or ldm / stm to the emulated vgic */
  613. if (mmio->len > 4) {
  614. kvm_inject_dabt(vcpu, mmio->phys_addr);
  615. return true;
  616. }
  617. range = find_matching_range(vgic_ranges, mmio, base);
  618. if (unlikely(!range || !range->handle_mmio)) {
  619. pr_warn("Unhandled access %d %08llx %d\n",
  620. mmio->is_write, mmio->phys_addr, mmio->len);
  621. return false;
  622. }
  623. spin_lock(&vcpu->kvm->arch.vgic.lock);
  624. offset = mmio->phys_addr - range->base - base;
  625. updated_state = range->handle_mmio(vcpu, mmio, offset);
  626. spin_unlock(&vcpu->kvm->arch.vgic.lock);
  627. kvm_prepare_mmio(run, mmio);
  628. kvm_handle_mmio_return(vcpu, run);
  629. if (updated_state)
  630. vgic_kick_vcpus(vcpu->kvm);
  631. return true;
  632. }
  633. static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg)
  634. {
  635. struct kvm *kvm = vcpu->kvm;
  636. struct vgic_dist *dist = &kvm->arch.vgic;
  637. int nrcpus = atomic_read(&kvm->online_vcpus);
  638. u8 target_cpus;
  639. int sgi, mode, c, vcpu_id;
  640. vcpu_id = vcpu->vcpu_id;
  641. sgi = reg & 0xf;
  642. target_cpus = (reg >> 16) & 0xff;
  643. mode = (reg >> 24) & 3;
  644. switch (mode) {
  645. case 0:
  646. if (!target_cpus)
  647. return;
  648. case 1:
  649. target_cpus = ((1 << nrcpus) - 1) & ~(1 << vcpu_id) & 0xff;
  650. break;
  651. case 2:
  652. target_cpus = 1 << vcpu_id;
  653. break;
  654. }
  655. kvm_for_each_vcpu(c, vcpu, kvm) {
  656. if (target_cpus & 1) {
  657. /* Flag the SGI as pending */
  658. vgic_dist_irq_set(vcpu, sgi);
  659. dist->irq_sgi_sources[c][sgi] |= 1 << vcpu_id;
  660. kvm_debug("SGI%d from CPU%d to CPU%d\n", sgi, vcpu_id, c);
  661. }
  662. target_cpus >>= 1;
  663. }
  664. }
  665. static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
  666. {
  667. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  668. unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
  669. unsigned long pending_private, pending_shared;
  670. int vcpu_id;
  671. vcpu_id = vcpu->vcpu_id;
  672. pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
  673. pend_shared = vcpu->arch.vgic_cpu.pending_shared;
  674. pending = vgic_bitmap_get_cpu_map(&dist->irq_state, vcpu_id);
  675. enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
  676. bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
  677. pending = vgic_bitmap_get_shared_map(&dist->irq_state);
  678. enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
  679. bitmap_and(pend_shared, pending, enabled, VGIC_NR_SHARED_IRQS);
  680. bitmap_and(pend_shared, pend_shared,
  681. vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
  682. VGIC_NR_SHARED_IRQS);
  683. pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
  684. pending_shared = find_first_bit(pend_shared, VGIC_NR_SHARED_IRQS);
  685. return (pending_private < VGIC_NR_PRIVATE_IRQS ||
  686. pending_shared < VGIC_NR_SHARED_IRQS);
  687. }
  688. /*
  689. * Update the interrupt state and determine which CPUs have pending
  690. * interrupts. Must be called with distributor lock held.
  691. */
  692. static void vgic_update_state(struct kvm *kvm)
  693. {
  694. struct vgic_dist *dist = &kvm->arch.vgic;
  695. struct kvm_vcpu *vcpu;
  696. int c;
  697. if (!dist->enabled) {
  698. set_bit(0, &dist->irq_pending_on_cpu);
  699. return;
  700. }
  701. kvm_for_each_vcpu(c, vcpu, kvm) {
  702. if (compute_pending_for_cpu(vcpu)) {
  703. pr_debug("CPU%d has pending interrupts\n", c);
  704. set_bit(c, &dist->irq_pending_on_cpu);
  705. }
  706. }
  707. }
  708. #define LR_CPUID(lr) \
  709. (((lr) & GICH_LR_PHYSID_CPUID) >> GICH_LR_PHYSID_CPUID_SHIFT)
  710. #define MK_LR_PEND(src, irq) \
  711. (GICH_LR_PENDING_BIT | ((src) << GICH_LR_PHYSID_CPUID_SHIFT) | (irq))
  712. /*
  713. * An interrupt may have been disabled after being made pending on the
  714. * CPU interface (the classic case is a timer running while we're
  715. * rebooting the guest - the interrupt would kick as soon as the CPU
  716. * interface gets enabled, with deadly consequences).
  717. *
  718. * The solution is to examine already active LRs, and check the
  719. * interrupt is still enabled. If not, just retire it.
  720. */
  721. static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
  722. {
  723. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  724. int lr;
  725. for_each_set_bit(lr, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
  726. int irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
  727. if (!vgic_irq_is_enabled(vcpu, irq)) {
  728. vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
  729. clear_bit(lr, vgic_cpu->lr_used);
  730. vgic_cpu->vgic_lr[lr] &= ~GICH_LR_STATE;
  731. if (vgic_irq_is_active(vcpu, irq))
  732. vgic_irq_clear_active(vcpu, irq);
  733. }
  734. }
  735. }
  736. /*
  737. * Queue an interrupt to a CPU virtual interface. Return true on success,
  738. * or false if it wasn't possible to queue it.
  739. */
  740. static bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
  741. {
  742. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  743. int lr;
  744. /* Sanitize the input... */
  745. BUG_ON(sgi_source_id & ~7);
  746. BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
  747. BUG_ON(irq >= VGIC_NR_IRQS);
  748. kvm_debug("Queue IRQ%d\n", irq);
  749. lr = vgic_cpu->vgic_irq_lr_map[irq];
  750. /* Do we have an active interrupt for the same CPUID? */
  751. if (lr != LR_EMPTY &&
  752. (LR_CPUID(vgic_cpu->vgic_lr[lr]) == sgi_source_id)) {
  753. kvm_debug("LR%d piggyback for IRQ%d %x\n",
  754. lr, irq, vgic_cpu->vgic_lr[lr]);
  755. BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
  756. vgic_cpu->vgic_lr[lr] |= GICH_LR_PENDING_BIT;
  757. return true;
  758. }
  759. /* Try to use another LR for this interrupt */
  760. lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
  761. vgic_cpu->nr_lr);
  762. if (lr >= vgic_cpu->nr_lr)
  763. return false;
  764. kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
  765. vgic_cpu->vgic_lr[lr] = MK_LR_PEND(sgi_source_id, irq);
  766. vgic_cpu->vgic_irq_lr_map[irq] = lr;
  767. set_bit(lr, vgic_cpu->lr_used);
  768. if (!vgic_irq_is_edge(vcpu, irq))
  769. vgic_cpu->vgic_lr[lr] |= GICH_LR_EOI;
  770. return true;
  771. }
  772. static bool vgic_queue_sgi(struct kvm_vcpu *vcpu, int irq)
  773. {
  774. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  775. unsigned long sources;
  776. int vcpu_id = vcpu->vcpu_id;
  777. int c;
  778. sources = dist->irq_sgi_sources[vcpu_id][irq];
  779. for_each_set_bit(c, &sources, VGIC_MAX_CPUS) {
  780. if (vgic_queue_irq(vcpu, c, irq))
  781. clear_bit(c, &sources);
  782. }
  783. dist->irq_sgi_sources[vcpu_id][irq] = sources;
  784. /*
  785. * If the sources bitmap has been cleared it means that we
  786. * could queue all the SGIs onto link registers (see the
  787. * clear_bit above), and therefore we are done with them in
  788. * our emulated gic and can get rid of them.
  789. */
  790. if (!sources) {
  791. vgic_dist_irq_clear(vcpu, irq);
  792. vgic_cpu_irq_clear(vcpu, irq);
  793. return true;
  794. }
  795. return false;
  796. }
  797. static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
  798. {
  799. if (vgic_irq_is_active(vcpu, irq))
  800. return true; /* level interrupt, already queued */
  801. if (vgic_queue_irq(vcpu, 0, irq)) {
  802. if (vgic_irq_is_edge(vcpu, irq)) {
  803. vgic_dist_irq_clear(vcpu, irq);
  804. vgic_cpu_irq_clear(vcpu, irq);
  805. } else {
  806. vgic_irq_set_active(vcpu, irq);
  807. }
  808. return true;
  809. }
  810. return false;
  811. }
  812. /*
  813. * Fill the list registers with pending interrupts before running the
  814. * guest.
  815. */
  816. static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  817. {
  818. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  819. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  820. int i, vcpu_id;
  821. int overflow = 0;
  822. vcpu_id = vcpu->vcpu_id;
  823. /*
  824. * We may not have any pending interrupt, or the interrupts
  825. * may have been serviced from another vcpu. In all cases,
  826. * move along.
  827. */
  828. if (!kvm_vgic_vcpu_pending_irq(vcpu)) {
  829. pr_debug("CPU%d has no pending interrupt\n", vcpu_id);
  830. goto epilog;
  831. }
  832. /* SGIs */
  833. for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) {
  834. if (!vgic_queue_sgi(vcpu, i))
  835. overflow = 1;
  836. }
  837. /* PPIs */
  838. for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) {
  839. if (!vgic_queue_hwirq(vcpu, i))
  840. overflow = 1;
  841. }
  842. /* SPIs */
  843. for_each_set_bit(i, vgic_cpu->pending_shared, VGIC_NR_SHARED_IRQS) {
  844. if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
  845. overflow = 1;
  846. }
  847. epilog:
  848. if (overflow) {
  849. vgic_cpu->vgic_hcr |= GICH_HCR_UIE;
  850. } else {
  851. vgic_cpu->vgic_hcr &= ~GICH_HCR_UIE;
  852. /*
  853. * We're about to run this VCPU, and we've consumed
  854. * everything the distributor had in store for
  855. * us. Claim we don't have anything pending. We'll
  856. * adjust that if needed while exiting.
  857. */
  858. clear_bit(vcpu_id, &dist->irq_pending_on_cpu);
  859. }
  860. }
  861. static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
  862. {
  863. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  864. bool level_pending = false;
  865. kvm_debug("MISR = %08x\n", vgic_cpu->vgic_misr);
  866. if (vgic_cpu->vgic_misr & GICH_MISR_EOI) {
  867. /*
  868. * Some level interrupts have been EOIed. Clear their
  869. * active bit.
  870. */
  871. int lr, irq;
  872. for_each_set_bit(lr, (unsigned long *)vgic_cpu->vgic_eisr,
  873. vgic_cpu->nr_lr) {
  874. irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
  875. vgic_irq_clear_active(vcpu, irq);
  876. vgic_cpu->vgic_lr[lr] &= ~GICH_LR_EOI;
  877. /* Any additional pending interrupt? */
  878. if (vgic_dist_irq_is_pending(vcpu, irq)) {
  879. vgic_cpu_irq_set(vcpu, irq);
  880. level_pending = true;
  881. } else {
  882. vgic_cpu_irq_clear(vcpu, irq);
  883. }
  884. /*
  885. * Despite being EOIed, the LR may not have
  886. * been marked as empty.
  887. */
  888. set_bit(lr, (unsigned long *)vgic_cpu->vgic_elrsr);
  889. vgic_cpu->vgic_lr[lr] &= ~GICH_LR_ACTIVE_BIT;
  890. }
  891. }
  892. if (vgic_cpu->vgic_misr & GICH_MISR_U)
  893. vgic_cpu->vgic_hcr &= ~GICH_HCR_UIE;
  894. return level_pending;
  895. }
  896. /*
  897. * Sync back the VGIC state after a guest run. The distributor lock is
  898. * needed so we don't get preempted in the middle of the state processing.
  899. */
  900. static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  901. {
  902. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  903. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  904. int lr, pending;
  905. bool level_pending;
  906. level_pending = vgic_process_maintenance(vcpu);
  907. /* Clear mappings for empty LRs */
  908. for_each_set_bit(lr, (unsigned long *)vgic_cpu->vgic_elrsr,
  909. vgic_cpu->nr_lr) {
  910. int irq;
  911. if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
  912. continue;
  913. irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
  914. BUG_ON(irq >= VGIC_NR_IRQS);
  915. vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
  916. }
  917. /* Check if we still have something up our sleeve... */
  918. pending = find_first_zero_bit((unsigned long *)vgic_cpu->vgic_elrsr,
  919. vgic_cpu->nr_lr);
  920. if (level_pending || pending < vgic_cpu->nr_lr)
  921. set_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
  922. }
  923. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  924. {
  925. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  926. if (!irqchip_in_kernel(vcpu->kvm))
  927. return;
  928. spin_lock(&dist->lock);
  929. __kvm_vgic_flush_hwstate(vcpu);
  930. spin_unlock(&dist->lock);
  931. }
  932. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  933. {
  934. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  935. if (!irqchip_in_kernel(vcpu->kvm))
  936. return;
  937. spin_lock(&dist->lock);
  938. __kvm_vgic_sync_hwstate(vcpu);
  939. spin_unlock(&dist->lock);
  940. }
  941. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  942. {
  943. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  944. if (!irqchip_in_kernel(vcpu->kvm))
  945. return 0;
  946. return test_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
  947. }
  948. static void vgic_kick_vcpus(struct kvm *kvm)
  949. {
  950. struct kvm_vcpu *vcpu;
  951. int c;
  952. /*
  953. * We've injected an interrupt, time to find out who deserves
  954. * a good kick...
  955. */
  956. kvm_for_each_vcpu(c, vcpu, kvm) {
  957. if (kvm_vgic_vcpu_pending_irq(vcpu))
  958. kvm_vcpu_kick(vcpu);
  959. }
  960. }
  961. static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
  962. {
  963. int is_edge = vgic_irq_is_edge(vcpu, irq);
  964. int state = vgic_dist_irq_is_pending(vcpu, irq);
  965. /*
  966. * Only inject an interrupt if:
  967. * - edge triggered and we have a rising edge
  968. * - level triggered and we change level
  969. */
  970. if (is_edge)
  971. return level > state;
  972. else
  973. return level != state;
  974. }
  975. static bool vgic_update_irq_state(struct kvm *kvm, int cpuid,
  976. unsigned int irq_num, bool level)
  977. {
  978. struct vgic_dist *dist = &kvm->arch.vgic;
  979. struct kvm_vcpu *vcpu;
  980. int is_edge, is_level;
  981. int enabled;
  982. bool ret = true;
  983. spin_lock(&dist->lock);
  984. vcpu = kvm_get_vcpu(kvm, cpuid);
  985. is_edge = vgic_irq_is_edge(vcpu, irq_num);
  986. is_level = !is_edge;
  987. if (!vgic_validate_injection(vcpu, irq_num, level)) {
  988. ret = false;
  989. goto out;
  990. }
  991. if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
  992. cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
  993. vcpu = kvm_get_vcpu(kvm, cpuid);
  994. }
  995. kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
  996. if (level)
  997. vgic_dist_irq_set(vcpu, irq_num);
  998. else
  999. vgic_dist_irq_clear(vcpu, irq_num);
  1000. enabled = vgic_irq_is_enabled(vcpu, irq_num);
  1001. if (!enabled) {
  1002. ret = false;
  1003. goto out;
  1004. }
  1005. if (is_level && vgic_irq_is_active(vcpu, irq_num)) {
  1006. /*
  1007. * Level interrupt in progress, will be picked up
  1008. * when EOId.
  1009. */
  1010. ret = false;
  1011. goto out;
  1012. }
  1013. if (level) {
  1014. vgic_cpu_irq_set(vcpu, irq_num);
  1015. set_bit(cpuid, &dist->irq_pending_on_cpu);
  1016. }
  1017. out:
  1018. spin_unlock(&dist->lock);
  1019. return ret;
  1020. }
  1021. /**
  1022. * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
  1023. * @kvm: The VM structure pointer
  1024. * @cpuid: The CPU for PPIs
  1025. * @irq_num: The IRQ number that is assigned to the device
  1026. * @level: Edge-triggered: true: to trigger the interrupt
  1027. * false: to ignore the call
  1028. * Level-sensitive true: activates an interrupt
  1029. * false: deactivates an interrupt
  1030. *
  1031. * The GIC is not concerned with devices being active-LOW or active-HIGH for
  1032. * level-sensitive interrupts. You can think of the level parameter as 1
  1033. * being HIGH and 0 being LOW and all devices being active-HIGH.
  1034. */
  1035. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
  1036. bool level)
  1037. {
  1038. if (vgic_update_irq_state(kvm, cpuid, irq_num, level))
  1039. vgic_kick_vcpus(kvm);
  1040. return 0;
  1041. }
  1042. static irqreturn_t vgic_maintenance_handler(int irq, void *data)
  1043. {
  1044. /*
  1045. * We cannot rely on the vgic maintenance interrupt to be
  1046. * delivered synchronously. This means we can only use it to
  1047. * exit the VM, and we perform the handling of EOIed
  1048. * interrupts on the exit path (see vgic_process_maintenance).
  1049. */
  1050. return IRQ_HANDLED;
  1051. }
  1052. int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
  1053. {
  1054. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1055. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1056. int i;
  1057. if (!irqchip_in_kernel(vcpu->kvm))
  1058. return 0;
  1059. if (vcpu->vcpu_id >= VGIC_MAX_CPUS)
  1060. return -EBUSY;
  1061. for (i = 0; i < VGIC_NR_IRQS; i++) {
  1062. if (i < VGIC_NR_PPIS)
  1063. vgic_bitmap_set_irq_val(&dist->irq_enabled,
  1064. vcpu->vcpu_id, i, 1);
  1065. if (i < VGIC_NR_PRIVATE_IRQS)
  1066. vgic_bitmap_set_irq_val(&dist->irq_cfg,
  1067. vcpu->vcpu_id, i, VGIC_CFG_EDGE);
  1068. vgic_cpu->vgic_irq_lr_map[i] = LR_EMPTY;
  1069. }
  1070. /*
  1071. * By forcing VMCR to zero, the GIC will restore the binary
  1072. * points to their reset values. Anything else resets to zero
  1073. * anyway.
  1074. */
  1075. vgic_cpu->vgic_vmcr = 0;
  1076. vgic_cpu->nr_lr = vgic_nr_lr;
  1077. vgic_cpu->vgic_hcr = GICH_HCR_EN; /* Get the show on the road... */
  1078. return 0;
  1079. }
  1080. static void vgic_init_maintenance_interrupt(void *info)
  1081. {
  1082. enable_percpu_irq(vgic_maint_irq, 0);
  1083. }
  1084. static int vgic_cpu_notify(struct notifier_block *self,
  1085. unsigned long action, void *cpu)
  1086. {
  1087. switch (action) {
  1088. case CPU_STARTING:
  1089. case CPU_STARTING_FROZEN:
  1090. vgic_init_maintenance_interrupt(NULL);
  1091. break;
  1092. case CPU_DYING:
  1093. case CPU_DYING_FROZEN:
  1094. disable_percpu_irq(vgic_maint_irq);
  1095. break;
  1096. }
  1097. return NOTIFY_OK;
  1098. }
  1099. static struct notifier_block vgic_cpu_nb = {
  1100. .notifier_call = vgic_cpu_notify,
  1101. };
  1102. int kvm_vgic_hyp_init(void)
  1103. {
  1104. int ret;
  1105. struct resource vctrl_res;
  1106. struct resource vcpu_res;
  1107. vgic_node = of_find_compatible_node(NULL, NULL, "arm,cortex-a15-gic");
  1108. if (!vgic_node) {
  1109. kvm_err("error: no compatible vgic node in DT\n");
  1110. return -ENODEV;
  1111. }
  1112. vgic_maint_irq = irq_of_parse_and_map(vgic_node, 0);
  1113. if (!vgic_maint_irq) {
  1114. kvm_err("error getting vgic maintenance irq from DT\n");
  1115. ret = -ENXIO;
  1116. goto out;
  1117. }
  1118. ret = request_percpu_irq(vgic_maint_irq, vgic_maintenance_handler,
  1119. "vgic", kvm_get_running_vcpus());
  1120. if (ret) {
  1121. kvm_err("Cannot register interrupt %d\n", vgic_maint_irq);
  1122. goto out;
  1123. }
  1124. ret = register_cpu_notifier(&vgic_cpu_nb);
  1125. if (ret) {
  1126. kvm_err("Cannot register vgic CPU notifier\n");
  1127. goto out_free_irq;
  1128. }
  1129. ret = of_address_to_resource(vgic_node, 2, &vctrl_res);
  1130. if (ret) {
  1131. kvm_err("Cannot obtain VCTRL resource\n");
  1132. goto out_free_irq;
  1133. }
  1134. vgic_vctrl_base = of_iomap(vgic_node, 2);
  1135. if (!vgic_vctrl_base) {
  1136. kvm_err("Cannot ioremap VCTRL\n");
  1137. ret = -ENOMEM;
  1138. goto out_free_irq;
  1139. }
  1140. vgic_nr_lr = readl_relaxed(vgic_vctrl_base + GICH_VTR);
  1141. vgic_nr_lr = (vgic_nr_lr & 0x3f) + 1;
  1142. ret = create_hyp_io_mappings(vgic_vctrl_base,
  1143. vgic_vctrl_base + resource_size(&vctrl_res),
  1144. vctrl_res.start);
  1145. if (ret) {
  1146. kvm_err("Cannot map VCTRL into hyp\n");
  1147. goto out_unmap;
  1148. }
  1149. kvm_info("%s@%llx IRQ%d\n", vgic_node->name,
  1150. vctrl_res.start, vgic_maint_irq);
  1151. on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
  1152. if (of_address_to_resource(vgic_node, 3, &vcpu_res)) {
  1153. kvm_err("Cannot obtain VCPU resource\n");
  1154. ret = -ENXIO;
  1155. goto out_unmap;
  1156. }
  1157. vgic_vcpu_base = vcpu_res.start;
  1158. goto out;
  1159. out_unmap:
  1160. iounmap(vgic_vctrl_base);
  1161. out_free_irq:
  1162. free_percpu_irq(vgic_maint_irq, kvm_get_running_vcpus());
  1163. out:
  1164. of_node_put(vgic_node);
  1165. return ret;
  1166. }
  1167. int kvm_vgic_init(struct kvm *kvm)
  1168. {
  1169. int ret = 0, i;
  1170. mutex_lock(&kvm->lock);
  1171. if (vgic_initialized(kvm))
  1172. goto out;
  1173. if (IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_dist_base) ||
  1174. IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_cpu_base)) {
  1175. kvm_err("Need to set vgic cpu and dist addresses first\n");
  1176. ret = -ENXIO;
  1177. goto out;
  1178. }
  1179. ret = kvm_phys_addr_ioremap(kvm, kvm->arch.vgic.vgic_cpu_base,
  1180. vgic_vcpu_base, KVM_VGIC_V2_CPU_SIZE);
  1181. if (ret) {
  1182. kvm_err("Unable to remap VGIC CPU to VCPU\n");
  1183. goto out;
  1184. }
  1185. for (i = VGIC_NR_PRIVATE_IRQS; i < VGIC_NR_IRQS; i += 4)
  1186. vgic_set_target_reg(kvm, 0, i);
  1187. kvm_timer_init(kvm);
  1188. kvm->arch.vgic.ready = true;
  1189. out:
  1190. mutex_unlock(&kvm->lock);
  1191. return ret;
  1192. }
  1193. int kvm_vgic_create(struct kvm *kvm)
  1194. {
  1195. int ret = 0;
  1196. mutex_lock(&kvm->lock);
  1197. if (atomic_read(&kvm->online_vcpus) || kvm->arch.vgic.vctrl_base) {
  1198. ret = -EEXIST;
  1199. goto out;
  1200. }
  1201. spin_lock_init(&kvm->arch.vgic.lock);
  1202. kvm->arch.vgic.vctrl_base = vgic_vctrl_base;
  1203. kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
  1204. kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
  1205. out:
  1206. mutex_unlock(&kvm->lock);
  1207. return ret;
  1208. }
  1209. static bool vgic_ioaddr_overlap(struct kvm *kvm)
  1210. {
  1211. phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
  1212. phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
  1213. if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
  1214. return 0;
  1215. if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
  1216. (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
  1217. return -EBUSY;
  1218. return 0;
  1219. }
  1220. static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
  1221. phys_addr_t addr, phys_addr_t size)
  1222. {
  1223. int ret;
  1224. if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
  1225. return -EEXIST;
  1226. if (addr + size < addr)
  1227. return -EINVAL;
  1228. ret = vgic_ioaddr_overlap(kvm);
  1229. if (ret)
  1230. return ret;
  1231. *ioaddr = addr;
  1232. return ret;
  1233. }
  1234. int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr)
  1235. {
  1236. int r = 0;
  1237. struct vgic_dist *vgic = &kvm->arch.vgic;
  1238. if (addr & ~KVM_PHYS_MASK)
  1239. return -E2BIG;
  1240. if (addr & (SZ_4K - 1))
  1241. return -EINVAL;
  1242. mutex_lock(&kvm->lock);
  1243. switch (type) {
  1244. case KVM_VGIC_V2_ADDR_TYPE_DIST:
  1245. r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
  1246. addr, KVM_VGIC_V2_DIST_SIZE);
  1247. break;
  1248. case KVM_VGIC_V2_ADDR_TYPE_CPU:
  1249. r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
  1250. addr, KVM_VGIC_V2_CPU_SIZE);
  1251. break;
  1252. default:
  1253. r = -ENODEV;
  1254. }
  1255. mutex_unlock(&kvm->lock);
  1256. return r;
  1257. }