vgic.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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 < 4)
  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. struct kvm_vcpu *vcpu;
  371. int i, c;
  372. unsigned long *bmap;
  373. u32 val = 0;
  374. irq -= VGIC_NR_PRIVATE_IRQS;
  375. kvm_for_each_vcpu(c, vcpu, kvm) {
  376. bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
  377. for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++)
  378. if (test_bit(irq + i, bmap))
  379. val |= 1 << (c + i * 8);
  380. }
  381. return val;
  382. }
  383. static void vgic_set_target_reg(struct kvm *kvm, u32 val, int irq)
  384. {
  385. struct vgic_dist *dist = &kvm->arch.vgic;
  386. struct kvm_vcpu *vcpu;
  387. int i, c;
  388. unsigned long *bmap;
  389. u32 target;
  390. irq -= VGIC_NR_PRIVATE_IRQS;
  391. /*
  392. * Pick the LSB in each byte. This ensures we target exactly
  393. * one vcpu per IRQ. If the byte is null, assume we target
  394. * CPU0.
  395. */
  396. for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++) {
  397. int shift = i * GICD_CPUTARGETS_BITS;
  398. target = ffs((val >> shift) & 0xffU);
  399. target = target ? (target - 1) : 0;
  400. dist->irq_spi_cpu[irq + i] = target;
  401. kvm_for_each_vcpu(c, vcpu, kvm) {
  402. bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
  403. if (c == target)
  404. set_bit(irq + i, bmap);
  405. else
  406. clear_bit(irq + i, bmap);
  407. }
  408. }
  409. }
  410. static bool handle_mmio_target_reg(struct kvm_vcpu *vcpu,
  411. struct kvm_exit_mmio *mmio,
  412. phys_addr_t offset)
  413. {
  414. u32 reg;
  415. /* We treat the banked interrupts targets as read-only */
  416. if (offset < 32) {
  417. u32 roreg = 1 << vcpu->vcpu_id;
  418. roreg |= roreg << 8;
  419. roreg |= roreg << 16;
  420. vgic_reg_access(mmio, &roreg, offset,
  421. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  422. return false;
  423. }
  424. reg = vgic_get_target_reg(vcpu->kvm, offset & ~3U);
  425. vgic_reg_access(mmio, &reg, offset,
  426. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  427. if (mmio->is_write) {
  428. vgic_set_target_reg(vcpu->kvm, reg, offset & ~3U);
  429. vgic_update_state(vcpu->kvm);
  430. return true;
  431. }
  432. return false;
  433. }
  434. static u32 vgic_cfg_expand(u16 val)
  435. {
  436. u32 res = 0;
  437. int i;
  438. /*
  439. * Turn a 16bit value like abcd...mnop into a 32bit word
  440. * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
  441. */
  442. for (i = 0; i < 16; i++)
  443. res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
  444. return res;
  445. }
  446. static u16 vgic_cfg_compress(u32 val)
  447. {
  448. u16 res = 0;
  449. int i;
  450. /*
  451. * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
  452. * abcd...mnop which is what we really care about.
  453. */
  454. for (i = 0; i < 16; i++)
  455. res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
  456. return res;
  457. }
  458. /*
  459. * The distributor uses 2 bits per IRQ for the CFG register, but the
  460. * LSB is always 0. As such, we only keep the upper bit, and use the
  461. * two above functions to compress/expand the bits
  462. */
  463. static bool handle_mmio_cfg_reg(struct kvm_vcpu *vcpu,
  464. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  465. {
  466. u32 val;
  467. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_cfg,
  468. vcpu->vcpu_id, offset >> 1);
  469. if (offset & 2)
  470. val = *reg >> 16;
  471. else
  472. val = *reg & 0xffff;
  473. val = vgic_cfg_expand(val);
  474. vgic_reg_access(mmio, &val, offset,
  475. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  476. if (mmio->is_write) {
  477. if (offset < 4) {
  478. *reg = ~0U; /* Force PPIs/SGIs to 1 */
  479. return false;
  480. }
  481. val = vgic_cfg_compress(val);
  482. if (offset & 2) {
  483. *reg &= 0xffff;
  484. *reg |= val << 16;
  485. } else {
  486. *reg &= 0xffff << 16;
  487. *reg |= val;
  488. }
  489. }
  490. return false;
  491. }
  492. static bool handle_mmio_sgi_reg(struct kvm_vcpu *vcpu,
  493. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  494. {
  495. u32 reg;
  496. vgic_reg_access(mmio, &reg, offset,
  497. ACCESS_READ_RAZ | ACCESS_WRITE_VALUE);
  498. if (mmio->is_write) {
  499. vgic_dispatch_sgi(vcpu, reg);
  500. vgic_update_state(vcpu->kvm);
  501. return true;
  502. }
  503. return false;
  504. }
  505. /*
  506. * I would have liked to use the kvm_bus_io_*() API instead, but it
  507. * cannot cope with banked registers (only the VM pointer is passed
  508. * around, and we need the vcpu). One of these days, someone please
  509. * fix it!
  510. */
  511. struct mmio_range {
  512. phys_addr_t base;
  513. unsigned long len;
  514. bool (*handle_mmio)(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
  515. phys_addr_t offset);
  516. };
  517. static const struct mmio_range vgic_ranges[] = {
  518. {
  519. .base = GIC_DIST_CTRL,
  520. .len = 12,
  521. .handle_mmio = handle_mmio_misc,
  522. },
  523. {
  524. .base = GIC_DIST_IGROUP,
  525. .len = VGIC_NR_IRQS / 8,
  526. .handle_mmio = handle_mmio_raz_wi,
  527. },
  528. {
  529. .base = GIC_DIST_ENABLE_SET,
  530. .len = VGIC_NR_IRQS / 8,
  531. .handle_mmio = handle_mmio_set_enable_reg,
  532. },
  533. {
  534. .base = GIC_DIST_ENABLE_CLEAR,
  535. .len = VGIC_NR_IRQS / 8,
  536. .handle_mmio = handle_mmio_clear_enable_reg,
  537. },
  538. {
  539. .base = GIC_DIST_PENDING_SET,
  540. .len = VGIC_NR_IRQS / 8,
  541. .handle_mmio = handle_mmio_set_pending_reg,
  542. },
  543. {
  544. .base = GIC_DIST_PENDING_CLEAR,
  545. .len = VGIC_NR_IRQS / 8,
  546. .handle_mmio = handle_mmio_clear_pending_reg,
  547. },
  548. {
  549. .base = GIC_DIST_ACTIVE_SET,
  550. .len = VGIC_NR_IRQS / 8,
  551. .handle_mmio = handle_mmio_raz_wi,
  552. },
  553. {
  554. .base = GIC_DIST_ACTIVE_CLEAR,
  555. .len = VGIC_NR_IRQS / 8,
  556. .handle_mmio = handle_mmio_raz_wi,
  557. },
  558. {
  559. .base = GIC_DIST_PRI,
  560. .len = VGIC_NR_IRQS,
  561. .handle_mmio = handle_mmio_priority_reg,
  562. },
  563. {
  564. .base = GIC_DIST_TARGET,
  565. .len = VGIC_NR_IRQS,
  566. .handle_mmio = handle_mmio_target_reg,
  567. },
  568. {
  569. .base = GIC_DIST_CONFIG,
  570. .len = VGIC_NR_IRQS / 4,
  571. .handle_mmio = handle_mmio_cfg_reg,
  572. },
  573. {
  574. .base = GIC_DIST_SOFTINT,
  575. .len = 4,
  576. .handle_mmio = handle_mmio_sgi_reg,
  577. },
  578. {}
  579. };
  580. static const
  581. struct mmio_range *find_matching_range(const struct mmio_range *ranges,
  582. struct kvm_exit_mmio *mmio,
  583. phys_addr_t base)
  584. {
  585. const struct mmio_range *r = ranges;
  586. phys_addr_t addr = mmio->phys_addr - base;
  587. while (r->len) {
  588. if (addr >= r->base &&
  589. (addr + mmio->len) <= (r->base + r->len))
  590. return r;
  591. r++;
  592. }
  593. return NULL;
  594. }
  595. /**
  596. * vgic_handle_mmio - handle an in-kernel MMIO access
  597. * @vcpu: pointer to the vcpu performing the access
  598. * @run: pointer to the kvm_run structure
  599. * @mmio: pointer to the data describing the access
  600. *
  601. * returns true if the MMIO access has been performed in kernel space,
  602. * and false if it needs to be emulated in user space.
  603. */
  604. bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  605. struct kvm_exit_mmio *mmio)
  606. {
  607. const struct mmio_range *range;
  608. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  609. unsigned long base = dist->vgic_dist_base;
  610. bool updated_state;
  611. unsigned long offset;
  612. if (!irqchip_in_kernel(vcpu->kvm) ||
  613. mmio->phys_addr < base ||
  614. (mmio->phys_addr + mmio->len) > (base + KVM_VGIC_V2_DIST_SIZE))
  615. return false;
  616. /* We don't support ldrd / strd or ldm / stm to the emulated vgic */
  617. if (mmio->len > 4) {
  618. kvm_inject_dabt(vcpu, mmio->phys_addr);
  619. return true;
  620. }
  621. range = find_matching_range(vgic_ranges, mmio, base);
  622. if (unlikely(!range || !range->handle_mmio)) {
  623. pr_warn("Unhandled access %d %08llx %d\n",
  624. mmio->is_write, mmio->phys_addr, mmio->len);
  625. return false;
  626. }
  627. spin_lock(&vcpu->kvm->arch.vgic.lock);
  628. offset = mmio->phys_addr - range->base - base;
  629. updated_state = range->handle_mmio(vcpu, mmio, offset);
  630. spin_unlock(&vcpu->kvm->arch.vgic.lock);
  631. kvm_prepare_mmio(run, mmio);
  632. kvm_handle_mmio_return(vcpu, run);
  633. if (updated_state)
  634. vgic_kick_vcpus(vcpu->kvm);
  635. return true;
  636. }
  637. static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg)
  638. {
  639. struct kvm *kvm = vcpu->kvm;
  640. struct vgic_dist *dist = &kvm->arch.vgic;
  641. int nrcpus = atomic_read(&kvm->online_vcpus);
  642. u8 target_cpus;
  643. int sgi, mode, c, vcpu_id;
  644. vcpu_id = vcpu->vcpu_id;
  645. sgi = reg & 0xf;
  646. target_cpus = (reg >> 16) & 0xff;
  647. mode = (reg >> 24) & 3;
  648. switch (mode) {
  649. case 0:
  650. if (!target_cpus)
  651. return;
  652. case 1:
  653. target_cpus = ((1 << nrcpus) - 1) & ~(1 << vcpu_id) & 0xff;
  654. break;
  655. case 2:
  656. target_cpus = 1 << vcpu_id;
  657. break;
  658. }
  659. kvm_for_each_vcpu(c, vcpu, kvm) {
  660. if (target_cpus & 1) {
  661. /* Flag the SGI as pending */
  662. vgic_dist_irq_set(vcpu, sgi);
  663. dist->irq_sgi_sources[c][sgi] |= 1 << vcpu_id;
  664. kvm_debug("SGI%d from CPU%d to CPU%d\n", sgi, vcpu_id, c);
  665. }
  666. target_cpus >>= 1;
  667. }
  668. }
  669. static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
  670. {
  671. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  672. unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
  673. unsigned long pending_private, pending_shared;
  674. int vcpu_id;
  675. vcpu_id = vcpu->vcpu_id;
  676. pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
  677. pend_shared = vcpu->arch.vgic_cpu.pending_shared;
  678. pending = vgic_bitmap_get_cpu_map(&dist->irq_state, vcpu_id);
  679. enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
  680. bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
  681. pending = vgic_bitmap_get_shared_map(&dist->irq_state);
  682. enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
  683. bitmap_and(pend_shared, pending, enabled, VGIC_NR_SHARED_IRQS);
  684. bitmap_and(pend_shared, pend_shared,
  685. vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
  686. VGIC_NR_SHARED_IRQS);
  687. pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
  688. pending_shared = find_first_bit(pend_shared, VGIC_NR_SHARED_IRQS);
  689. return (pending_private < VGIC_NR_PRIVATE_IRQS ||
  690. pending_shared < VGIC_NR_SHARED_IRQS);
  691. }
  692. /*
  693. * Update the interrupt state and determine which CPUs have pending
  694. * interrupts. Must be called with distributor lock held.
  695. */
  696. static void vgic_update_state(struct kvm *kvm)
  697. {
  698. struct vgic_dist *dist = &kvm->arch.vgic;
  699. struct kvm_vcpu *vcpu;
  700. int c;
  701. if (!dist->enabled) {
  702. set_bit(0, &dist->irq_pending_on_cpu);
  703. return;
  704. }
  705. kvm_for_each_vcpu(c, vcpu, kvm) {
  706. if (compute_pending_for_cpu(vcpu)) {
  707. pr_debug("CPU%d has pending interrupts\n", c);
  708. set_bit(c, &dist->irq_pending_on_cpu);
  709. }
  710. }
  711. }
  712. #define LR_CPUID(lr) \
  713. (((lr) & GICH_LR_PHYSID_CPUID) >> GICH_LR_PHYSID_CPUID_SHIFT)
  714. #define MK_LR_PEND(src, irq) \
  715. (GICH_LR_PENDING_BIT | ((src) << GICH_LR_PHYSID_CPUID_SHIFT) | (irq))
  716. /*
  717. * An interrupt may have been disabled after being made pending on the
  718. * CPU interface (the classic case is a timer running while we're
  719. * rebooting the guest - the interrupt would kick as soon as the CPU
  720. * interface gets enabled, with deadly consequences).
  721. *
  722. * The solution is to examine already active LRs, and check the
  723. * interrupt is still enabled. If not, just retire it.
  724. */
  725. static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
  726. {
  727. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  728. int lr;
  729. for_each_set_bit(lr, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
  730. int irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
  731. if (!vgic_irq_is_enabled(vcpu, irq)) {
  732. vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
  733. clear_bit(lr, vgic_cpu->lr_used);
  734. vgic_cpu->vgic_lr[lr] &= ~GICH_LR_STATE;
  735. if (vgic_irq_is_active(vcpu, irq))
  736. vgic_irq_clear_active(vcpu, irq);
  737. }
  738. }
  739. }
  740. /*
  741. * Queue an interrupt to a CPU virtual interface. Return true on success,
  742. * or false if it wasn't possible to queue it.
  743. */
  744. static bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
  745. {
  746. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  747. int lr;
  748. /* Sanitize the input... */
  749. BUG_ON(sgi_source_id & ~7);
  750. BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
  751. BUG_ON(irq >= VGIC_NR_IRQS);
  752. kvm_debug("Queue IRQ%d\n", irq);
  753. lr = vgic_cpu->vgic_irq_lr_map[irq];
  754. /* Do we have an active interrupt for the same CPUID? */
  755. if (lr != LR_EMPTY &&
  756. (LR_CPUID(vgic_cpu->vgic_lr[lr]) == sgi_source_id)) {
  757. kvm_debug("LR%d piggyback for IRQ%d %x\n",
  758. lr, irq, vgic_cpu->vgic_lr[lr]);
  759. BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
  760. vgic_cpu->vgic_lr[lr] |= GICH_LR_PENDING_BIT;
  761. goto out;
  762. }
  763. /* Try to use another LR for this interrupt */
  764. lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
  765. vgic_cpu->nr_lr);
  766. if (lr >= vgic_cpu->nr_lr)
  767. return false;
  768. kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
  769. vgic_cpu->vgic_lr[lr] = MK_LR_PEND(sgi_source_id, irq);
  770. vgic_cpu->vgic_irq_lr_map[irq] = lr;
  771. set_bit(lr, vgic_cpu->lr_used);
  772. out:
  773. if (!vgic_irq_is_edge(vcpu, irq))
  774. vgic_cpu->vgic_lr[lr] |= GICH_LR_EOI;
  775. return true;
  776. }
  777. static bool vgic_queue_sgi(struct kvm_vcpu *vcpu, int irq)
  778. {
  779. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  780. unsigned long sources;
  781. int vcpu_id = vcpu->vcpu_id;
  782. int c;
  783. sources = dist->irq_sgi_sources[vcpu_id][irq];
  784. for_each_set_bit(c, &sources, VGIC_MAX_CPUS) {
  785. if (vgic_queue_irq(vcpu, c, irq))
  786. clear_bit(c, &sources);
  787. }
  788. dist->irq_sgi_sources[vcpu_id][irq] = sources;
  789. /*
  790. * If the sources bitmap has been cleared it means that we
  791. * could queue all the SGIs onto link registers (see the
  792. * clear_bit above), and therefore we are done with them in
  793. * our emulated gic and can get rid of them.
  794. */
  795. if (!sources) {
  796. vgic_dist_irq_clear(vcpu, irq);
  797. vgic_cpu_irq_clear(vcpu, irq);
  798. return true;
  799. }
  800. return false;
  801. }
  802. static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
  803. {
  804. if (vgic_irq_is_active(vcpu, irq))
  805. return true; /* level interrupt, already queued */
  806. if (vgic_queue_irq(vcpu, 0, irq)) {
  807. if (vgic_irq_is_edge(vcpu, irq)) {
  808. vgic_dist_irq_clear(vcpu, irq);
  809. vgic_cpu_irq_clear(vcpu, irq);
  810. } else {
  811. vgic_irq_set_active(vcpu, irq);
  812. }
  813. return true;
  814. }
  815. return false;
  816. }
  817. /*
  818. * Fill the list registers with pending interrupts before running the
  819. * guest.
  820. */
  821. static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  822. {
  823. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  824. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  825. int i, vcpu_id;
  826. int overflow = 0;
  827. vcpu_id = vcpu->vcpu_id;
  828. /*
  829. * We may not have any pending interrupt, or the interrupts
  830. * may have been serviced from another vcpu. In all cases,
  831. * move along.
  832. */
  833. if (!kvm_vgic_vcpu_pending_irq(vcpu)) {
  834. pr_debug("CPU%d has no pending interrupt\n", vcpu_id);
  835. goto epilog;
  836. }
  837. /* SGIs */
  838. for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) {
  839. if (!vgic_queue_sgi(vcpu, i))
  840. overflow = 1;
  841. }
  842. /* PPIs */
  843. for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) {
  844. if (!vgic_queue_hwirq(vcpu, i))
  845. overflow = 1;
  846. }
  847. /* SPIs */
  848. for_each_set_bit(i, vgic_cpu->pending_shared, VGIC_NR_SHARED_IRQS) {
  849. if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
  850. overflow = 1;
  851. }
  852. epilog:
  853. if (overflow) {
  854. vgic_cpu->vgic_hcr |= GICH_HCR_UIE;
  855. } else {
  856. vgic_cpu->vgic_hcr &= ~GICH_HCR_UIE;
  857. /*
  858. * We're about to run this VCPU, and we've consumed
  859. * everything the distributor had in store for
  860. * us. Claim we don't have anything pending. We'll
  861. * adjust that if needed while exiting.
  862. */
  863. clear_bit(vcpu_id, &dist->irq_pending_on_cpu);
  864. }
  865. }
  866. static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
  867. {
  868. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  869. bool level_pending = false;
  870. kvm_debug("MISR = %08x\n", vgic_cpu->vgic_misr);
  871. /*
  872. * We do not need to take the distributor lock here, since the only
  873. * action we perform is clearing the irq_active_bit for an EOIed
  874. * level interrupt. There is a potential race with
  875. * the queuing of an interrupt in __kvm_vgic_flush_hwstate(), where we
  876. * check if the interrupt is already active. Two possibilities:
  877. *
  878. * - The queuing is occurring on the same vcpu: cannot happen,
  879. * as we're already in the context of this vcpu, and
  880. * executing the handler
  881. * - The interrupt has been migrated to another vcpu, and we
  882. * ignore this interrupt for this run. Big deal. It is still
  883. * pending though, and will get considered when this vcpu
  884. * exits.
  885. */
  886. if (vgic_cpu->vgic_misr & GICH_MISR_EOI) {
  887. /*
  888. * Some level interrupts have been EOIed. Clear their
  889. * active bit.
  890. */
  891. int lr, irq;
  892. for_each_set_bit(lr, (unsigned long *)vgic_cpu->vgic_eisr,
  893. vgic_cpu->nr_lr) {
  894. irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
  895. vgic_irq_clear_active(vcpu, irq);
  896. vgic_cpu->vgic_lr[lr] &= ~GICH_LR_EOI;
  897. /* Any additional pending interrupt? */
  898. if (vgic_dist_irq_is_pending(vcpu, irq)) {
  899. vgic_cpu_irq_set(vcpu, irq);
  900. level_pending = true;
  901. } else {
  902. vgic_cpu_irq_clear(vcpu, irq);
  903. }
  904. }
  905. }
  906. if (vgic_cpu->vgic_misr & GICH_MISR_U)
  907. vgic_cpu->vgic_hcr &= ~GICH_HCR_UIE;
  908. return level_pending;
  909. }
  910. /*
  911. * Sync back the VGIC state after a guest run. We do not really touch
  912. * the distributor here (the irq_pending_on_cpu bit is safe to set),
  913. * so there is no need for taking its lock.
  914. */
  915. static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  916. {
  917. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  918. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  919. int lr, pending;
  920. bool level_pending;
  921. level_pending = vgic_process_maintenance(vcpu);
  922. /* Clear mappings for empty LRs */
  923. for_each_set_bit(lr, (unsigned long *)vgic_cpu->vgic_elrsr,
  924. vgic_cpu->nr_lr) {
  925. int irq;
  926. if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
  927. continue;
  928. irq = vgic_cpu->vgic_lr[lr] & GICH_LR_VIRTUALID;
  929. BUG_ON(irq >= VGIC_NR_IRQS);
  930. vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
  931. }
  932. /* Check if we still have something up our sleeve... */
  933. pending = find_first_zero_bit((unsigned long *)vgic_cpu->vgic_elrsr,
  934. vgic_cpu->nr_lr);
  935. if (level_pending || pending < vgic_cpu->nr_lr)
  936. set_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
  937. }
  938. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  939. {
  940. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  941. if (!irqchip_in_kernel(vcpu->kvm))
  942. return;
  943. spin_lock(&dist->lock);
  944. __kvm_vgic_flush_hwstate(vcpu);
  945. spin_unlock(&dist->lock);
  946. }
  947. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  948. {
  949. if (!irqchip_in_kernel(vcpu->kvm))
  950. return;
  951. __kvm_vgic_sync_hwstate(vcpu);
  952. }
  953. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  954. {
  955. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  956. if (!irqchip_in_kernel(vcpu->kvm))
  957. return 0;
  958. return test_bit(vcpu->vcpu_id, &dist->irq_pending_on_cpu);
  959. }
  960. static void vgic_kick_vcpus(struct kvm *kvm)
  961. {
  962. struct kvm_vcpu *vcpu;
  963. int c;
  964. /*
  965. * We've injected an interrupt, time to find out who deserves
  966. * a good kick...
  967. */
  968. kvm_for_each_vcpu(c, vcpu, kvm) {
  969. if (kvm_vgic_vcpu_pending_irq(vcpu))
  970. kvm_vcpu_kick(vcpu);
  971. }
  972. }
  973. static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
  974. {
  975. int is_edge = vgic_irq_is_edge(vcpu, irq);
  976. int state = vgic_dist_irq_is_pending(vcpu, irq);
  977. /*
  978. * Only inject an interrupt if:
  979. * - edge triggered and we have a rising edge
  980. * - level triggered and we change level
  981. */
  982. if (is_edge)
  983. return level > state;
  984. else
  985. return level != state;
  986. }
  987. static bool vgic_update_irq_state(struct kvm *kvm, int cpuid,
  988. unsigned int irq_num, bool level)
  989. {
  990. struct vgic_dist *dist = &kvm->arch.vgic;
  991. struct kvm_vcpu *vcpu;
  992. int is_edge, is_level;
  993. int enabled;
  994. bool ret = true;
  995. spin_lock(&dist->lock);
  996. vcpu = kvm_get_vcpu(kvm, cpuid);
  997. is_edge = vgic_irq_is_edge(vcpu, irq_num);
  998. is_level = !is_edge;
  999. if (!vgic_validate_injection(vcpu, irq_num, level)) {
  1000. ret = false;
  1001. goto out;
  1002. }
  1003. if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
  1004. cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
  1005. vcpu = kvm_get_vcpu(kvm, cpuid);
  1006. }
  1007. kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
  1008. if (level)
  1009. vgic_dist_irq_set(vcpu, irq_num);
  1010. else
  1011. vgic_dist_irq_clear(vcpu, irq_num);
  1012. enabled = vgic_irq_is_enabled(vcpu, irq_num);
  1013. if (!enabled) {
  1014. ret = false;
  1015. goto out;
  1016. }
  1017. if (is_level && vgic_irq_is_active(vcpu, irq_num)) {
  1018. /*
  1019. * Level interrupt in progress, will be picked up
  1020. * when EOId.
  1021. */
  1022. ret = false;
  1023. goto out;
  1024. }
  1025. if (level) {
  1026. vgic_cpu_irq_set(vcpu, irq_num);
  1027. set_bit(cpuid, &dist->irq_pending_on_cpu);
  1028. }
  1029. out:
  1030. spin_unlock(&dist->lock);
  1031. return ret;
  1032. }
  1033. /**
  1034. * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
  1035. * @kvm: The VM structure pointer
  1036. * @cpuid: The CPU for PPIs
  1037. * @irq_num: The IRQ number that is assigned to the device
  1038. * @level: Edge-triggered: true: to trigger the interrupt
  1039. * false: to ignore the call
  1040. * Level-sensitive true: activates an interrupt
  1041. * false: deactivates an interrupt
  1042. *
  1043. * The GIC is not concerned with devices being active-LOW or active-HIGH for
  1044. * level-sensitive interrupts. You can think of the level parameter as 1
  1045. * being HIGH and 0 being LOW and all devices being active-HIGH.
  1046. */
  1047. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
  1048. bool level)
  1049. {
  1050. if (vgic_update_irq_state(kvm, cpuid, irq_num, level))
  1051. vgic_kick_vcpus(kvm);
  1052. return 0;
  1053. }
  1054. static irqreturn_t vgic_maintenance_handler(int irq, void *data)
  1055. {
  1056. /*
  1057. * We cannot rely on the vgic maintenance interrupt to be
  1058. * delivered synchronously. This means we can only use it to
  1059. * exit the VM, and we perform the handling of EOIed
  1060. * interrupts on the exit path (see vgic_process_maintenance).
  1061. */
  1062. return IRQ_HANDLED;
  1063. }
  1064. int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
  1065. {
  1066. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1067. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1068. int i;
  1069. if (!irqchip_in_kernel(vcpu->kvm))
  1070. return 0;
  1071. if (vcpu->vcpu_id >= VGIC_MAX_CPUS)
  1072. return -EBUSY;
  1073. for (i = 0; i < VGIC_NR_IRQS; i++) {
  1074. if (i < VGIC_NR_PPIS)
  1075. vgic_bitmap_set_irq_val(&dist->irq_enabled,
  1076. vcpu->vcpu_id, i, 1);
  1077. if (i < VGIC_NR_PRIVATE_IRQS)
  1078. vgic_bitmap_set_irq_val(&dist->irq_cfg,
  1079. vcpu->vcpu_id, i, VGIC_CFG_EDGE);
  1080. vgic_cpu->vgic_irq_lr_map[i] = LR_EMPTY;
  1081. }
  1082. /*
  1083. * By forcing VMCR to zero, the GIC will restore the binary
  1084. * points to their reset values. Anything else resets to zero
  1085. * anyway.
  1086. */
  1087. vgic_cpu->vgic_vmcr = 0;
  1088. vgic_cpu->nr_lr = vgic_nr_lr;
  1089. vgic_cpu->vgic_hcr = GICH_HCR_EN; /* Get the show on the road... */
  1090. return 0;
  1091. }
  1092. static void vgic_init_maintenance_interrupt(void *info)
  1093. {
  1094. enable_percpu_irq(vgic_maint_irq, 0);
  1095. }
  1096. static int vgic_cpu_notify(struct notifier_block *self,
  1097. unsigned long action, void *cpu)
  1098. {
  1099. switch (action) {
  1100. case CPU_STARTING:
  1101. case CPU_STARTING_FROZEN:
  1102. vgic_init_maintenance_interrupt(NULL);
  1103. break;
  1104. case CPU_DYING:
  1105. case CPU_DYING_FROZEN:
  1106. disable_percpu_irq(vgic_maint_irq);
  1107. break;
  1108. }
  1109. return NOTIFY_OK;
  1110. }
  1111. static struct notifier_block vgic_cpu_nb = {
  1112. .notifier_call = vgic_cpu_notify,
  1113. };
  1114. int kvm_vgic_hyp_init(void)
  1115. {
  1116. int ret;
  1117. struct resource vctrl_res;
  1118. struct resource vcpu_res;
  1119. vgic_node = of_find_compatible_node(NULL, NULL, "arm,cortex-a15-gic");
  1120. if (!vgic_node) {
  1121. kvm_err("error: no compatible vgic node in DT\n");
  1122. return -ENODEV;
  1123. }
  1124. vgic_maint_irq = irq_of_parse_and_map(vgic_node, 0);
  1125. if (!vgic_maint_irq) {
  1126. kvm_err("error getting vgic maintenance irq from DT\n");
  1127. ret = -ENXIO;
  1128. goto out;
  1129. }
  1130. ret = request_percpu_irq(vgic_maint_irq, vgic_maintenance_handler,
  1131. "vgic", kvm_get_running_vcpus());
  1132. if (ret) {
  1133. kvm_err("Cannot register interrupt %d\n", vgic_maint_irq);
  1134. goto out;
  1135. }
  1136. ret = register_cpu_notifier(&vgic_cpu_nb);
  1137. if (ret) {
  1138. kvm_err("Cannot register vgic CPU notifier\n");
  1139. goto out_free_irq;
  1140. }
  1141. ret = of_address_to_resource(vgic_node, 2, &vctrl_res);
  1142. if (ret) {
  1143. kvm_err("Cannot obtain VCTRL resource\n");
  1144. goto out_free_irq;
  1145. }
  1146. vgic_vctrl_base = of_iomap(vgic_node, 2);
  1147. if (!vgic_vctrl_base) {
  1148. kvm_err("Cannot ioremap VCTRL\n");
  1149. ret = -ENOMEM;
  1150. goto out_free_irq;
  1151. }
  1152. vgic_nr_lr = readl_relaxed(vgic_vctrl_base + GICH_VTR);
  1153. vgic_nr_lr = (vgic_nr_lr & 0x3f) + 1;
  1154. ret = create_hyp_io_mappings(vgic_vctrl_base,
  1155. vgic_vctrl_base + resource_size(&vctrl_res),
  1156. vctrl_res.start);
  1157. if (ret) {
  1158. kvm_err("Cannot map VCTRL into hyp\n");
  1159. goto out_unmap;
  1160. }
  1161. kvm_info("%s@%llx IRQ%d\n", vgic_node->name,
  1162. vctrl_res.start, vgic_maint_irq);
  1163. on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
  1164. if (of_address_to_resource(vgic_node, 3, &vcpu_res)) {
  1165. kvm_err("Cannot obtain VCPU resource\n");
  1166. ret = -ENXIO;
  1167. goto out_unmap;
  1168. }
  1169. vgic_vcpu_base = vcpu_res.start;
  1170. goto out;
  1171. out_unmap:
  1172. iounmap(vgic_vctrl_base);
  1173. out_free_irq:
  1174. free_percpu_irq(vgic_maint_irq, kvm_get_running_vcpus());
  1175. out:
  1176. of_node_put(vgic_node);
  1177. return ret;
  1178. }
  1179. int kvm_vgic_init(struct kvm *kvm)
  1180. {
  1181. int ret = 0, i;
  1182. mutex_lock(&kvm->lock);
  1183. if (vgic_initialized(kvm))
  1184. goto out;
  1185. if (IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_dist_base) ||
  1186. IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_cpu_base)) {
  1187. kvm_err("Need to set vgic cpu and dist addresses first\n");
  1188. ret = -ENXIO;
  1189. goto out;
  1190. }
  1191. ret = kvm_phys_addr_ioremap(kvm, kvm->arch.vgic.vgic_cpu_base,
  1192. vgic_vcpu_base, KVM_VGIC_V2_CPU_SIZE);
  1193. if (ret) {
  1194. kvm_err("Unable to remap VGIC CPU to VCPU\n");
  1195. goto out;
  1196. }
  1197. for (i = VGIC_NR_PRIVATE_IRQS; i < VGIC_NR_IRQS; i += 4)
  1198. vgic_set_target_reg(kvm, 0, i);
  1199. kvm_timer_init(kvm);
  1200. kvm->arch.vgic.ready = true;
  1201. out:
  1202. mutex_unlock(&kvm->lock);
  1203. return ret;
  1204. }
  1205. int kvm_vgic_create(struct kvm *kvm)
  1206. {
  1207. int ret = 0;
  1208. mutex_lock(&kvm->lock);
  1209. if (atomic_read(&kvm->online_vcpus) || kvm->arch.vgic.vctrl_base) {
  1210. ret = -EEXIST;
  1211. goto out;
  1212. }
  1213. spin_lock_init(&kvm->arch.vgic.lock);
  1214. kvm->arch.vgic.vctrl_base = vgic_vctrl_base;
  1215. kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
  1216. kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
  1217. out:
  1218. mutex_unlock(&kvm->lock);
  1219. return ret;
  1220. }
  1221. static bool vgic_ioaddr_overlap(struct kvm *kvm)
  1222. {
  1223. phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
  1224. phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
  1225. if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
  1226. return 0;
  1227. if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
  1228. (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
  1229. return -EBUSY;
  1230. return 0;
  1231. }
  1232. static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
  1233. phys_addr_t addr, phys_addr_t size)
  1234. {
  1235. int ret;
  1236. if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
  1237. return -EEXIST;
  1238. if (addr + size < addr)
  1239. return -EINVAL;
  1240. ret = vgic_ioaddr_overlap(kvm);
  1241. if (ret)
  1242. return ret;
  1243. *ioaddr = addr;
  1244. return ret;
  1245. }
  1246. int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr)
  1247. {
  1248. int r = 0;
  1249. struct vgic_dist *vgic = &kvm->arch.vgic;
  1250. if (addr & ~KVM_PHYS_MASK)
  1251. return -E2BIG;
  1252. if (addr & ~PAGE_MASK)
  1253. return -EINVAL;
  1254. mutex_lock(&kvm->lock);
  1255. switch (type) {
  1256. case KVM_VGIC_V2_ADDR_TYPE_DIST:
  1257. r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
  1258. addr, KVM_VGIC_V2_DIST_SIZE);
  1259. break;
  1260. case KVM_VGIC_V2_ADDR_TYPE_CPU:
  1261. r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
  1262. addr, KVM_VGIC_V2_CPU_SIZE);
  1263. break;
  1264. default:
  1265. r = -ENODEV;
  1266. }
  1267. mutex_unlock(&kvm->lock);
  1268. return r;
  1269. }