vgic.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176
  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/uaccess.h>
  27. #include <linux/irqchip/arm-gic.h>
  28. #include <asm/kvm_emulate.h>
  29. #include <asm/kvm_arm.h>
  30. #include <asm/kvm_mmu.h>
  31. #include <trace/events/kvm.h>
  32. #include <asm/kvm.h>
  33. #include <kvm/iodev.h>
  34. /*
  35. * How the whole thing works (courtesy of Christoffer Dall):
  36. *
  37. * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if
  38. * something is pending on the CPU interface.
  39. * - Interrupts that are pending on the distributor are stored on the
  40. * vgic.irq_pending vgic bitmap (this bitmap is updated by both user land
  41. * ioctls and guest mmio ops, and other in-kernel peripherals such as the
  42. * arch. timers).
  43. * - Every time the bitmap changes, the irq_pending_on_cpu oracle is
  44. * recalculated
  45. * - To calculate the oracle, we need info for each cpu from
  46. * compute_pending_for_cpu, which considers:
  47. * - PPI: dist->irq_pending & dist->irq_enable
  48. * - SPI: dist->irq_pending & dist->irq_enable & dist->irq_spi_target
  49. * - irq_spi_target is a 'formatted' version of the GICD_ITARGETSRn
  50. * registers, stored on each vcpu. We only keep one bit of
  51. * information per interrupt, making sure that only one vcpu can
  52. * accept the interrupt.
  53. * - If any of the above state changes, we must recalculate the oracle.
  54. * - The same is true when injecting an interrupt, except that we only
  55. * consider a single interrupt at a time. The irq_spi_cpu array
  56. * contains the target CPU for each SPI.
  57. *
  58. * The handling of level interrupts adds some extra complexity. We
  59. * need to track when the interrupt has been EOIed, so we can sample
  60. * the 'line' again. This is achieved as such:
  61. *
  62. * - When a level interrupt is moved onto a vcpu, the corresponding
  63. * bit in irq_queued is set. As long as this bit is set, the line
  64. * will be ignored for further interrupts. The interrupt is injected
  65. * into the vcpu with the GICH_LR_EOI bit set (generate a
  66. * maintenance interrupt on EOI).
  67. * - When the interrupt is EOIed, the maintenance interrupt fires,
  68. * and clears the corresponding bit in irq_queued. This allows the
  69. * interrupt line to be sampled again.
  70. * - Note that level-triggered interrupts can also be set to pending from
  71. * writes to GICD_ISPENDRn and lowering the external input line does not
  72. * cause the interrupt to become inactive in such a situation.
  73. * Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become
  74. * inactive as long as the external input line is held high.
  75. */
  76. #include "vgic.h"
  77. static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
  78. static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu);
  79. static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr);
  80. static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc);
  81. static const struct vgic_ops *vgic_ops;
  82. static const struct vgic_params *vgic;
  83. static void add_sgi_source(struct kvm_vcpu *vcpu, int irq, int source)
  84. {
  85. vcpu->kvm->arch.vgic.vm_ops.add_sgi_source(vcpu, irq, source);
  86. }
  87. static bool queue_sgi(struct kvm_vcpu *vcpu, int irq)
  88. {
  89. return vcpu->kvm->arch.vgic.vm_ops.queue_sgi(vcpu, irq);
  90. }
  91. int kvm_vgic_map_resources(struct kvm *kvm)
  92. {
  93. return kvm->arch.vgic.vm_ops.map_resources(kvm, vgic);
  94. }
  95. /*
  96. * struct vgic_bitmap contains a bitmap made of unsigned longs, but
  97. * extracts u32s out of them.
  98. *
  99. * This does not work on 64-bit BE systems, because the bitmap access
  100. * will store two consecutive 32-bit words with the higher-addressed
  101. * register's bits at the lower index and the lower-addressed register's
  102. * bits at the higher index.
  103. *
  104. * Therefore, swizzle the register index when accessing the 32-bit word
  105. * registers to access the right register's value.
  106. */
  107. #if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64
  108. #define REG_OFFSET_SWIZZLE 1
  109. #else
  110. #define REG_OFFSET_SWIZZLE 0
  111. #endif
  112. static int vgic_init_bitmap(struct vgic_bitmap *b, int nr_cpus, int nr_irqs)
  113. {
  114. int nr_longs;
  115. nr_longs = nr_cpus + BITS_TO_LONGS(nr_irqs - VGIC_NR_PRIVATE_IRQS);
  116. b->private = kzalloc(sizeof(unsigned long) * nr_longs, GFP_KERNEL);
  117. if (!b->private)
  118. return -ENOMEM;
  119. b->shared = b->private + nr_cpus;
  120. return 0;
  121. }
  122. static void vgic_free_bitmap(struct vgic_bitmap *b)
  123. {
  124. kfree(b->private);
  125. b->private = NULL;
  126. b->shared = NULL;
  127. }
  128. /*
  129. * Call this function to convert a u64 value to an unsigned long * bitmask
  130. * in a way that works on both 32-bit and 64-bit LE and BE platforms.
  131. *
  132. * Warning: Calling this function may modify *val.
  133. */
  134. static unsigned long *u64_to_bitmask(u64 *val)
  135. {
  136. #if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32
  137. *val = (*val >> 32) | (*val << 32);
  138. #endif
  139. return (unsigned long *)val;
  140. }
  141. u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x, int cpuid, u32 offset)
  142. {
  143. offset >>= 2;
  144. if (!offset)
  145. return (u32 *)(x->private + cpuid) + REG_OFFSET_SWIZZLE;
  146. else
  147. return (u32 *)(x->shared) + ((offset - 1) ^ REG_OFFSET_SWIZZLE);
  148. }
  149. static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
  150. int cpuid, int irq)
  151. {
  152. if (irq < VGIC_NR_PRIVATE_IRQS)
  153. return test_bit(irq, x->private + cpuid);
  154. return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared);
  155. }
  156. void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
  157. int irq, int val)
  158. {
  159. unsigned long *reg;
  160. if (irq < VGIC_NR_PRIVATE_IRQS) {
  161. reg = x->private + cpuid;
  162. } else {
  163. reg = x->shared;
  164. irq -= VGIC_NR_PRIVATE_IRQS;
  165. }
  166. if (val)
  167. set_bit(irq, reg);
  168. else
  169. clear_bit(irq, reg);
  170. }
  171. static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
  172. {
  173. return x->private + cpuid;
  174. }
  175. unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
  176. {
  177. return x->shared;
  178. }
  179. static int vgic_init_bytemap(struct vgic_bytemap *x, int nr_cpus, int nr_irqs)
  180. {
  181. int size;
  182. size = nr_cpus * VGIC_NR_PRIVATE_IRQS;
  183. size += nr_irqs - VGIC_NR_PRIVATE_IRQS;
  184. x->private = kzalloc(size, GFP_KERNEL);
  185. if (!x->private)
  186. return -ENOMEM;
  187. x->shared = x->private + nr_cpus * VGIC_NR_PRIVATE_IRQS / sizeof(u32);
  188. return 0;
  189. }
  190. static void vgic_free_bytemap(struct vgic_bytemap *b)
  191. {
  192. kfree(b->private);
  193. b->private = NULL;
  194. b->shared = NULL;
  195. }
  196. u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
  197. {
  198. u32 *reg;
  199. if (offset < VGIC_NR_PRIVATE_IRQS) {
  200. reg = x->private;
  201. offset += cpuid * VGIC_NR_PRIVATE_IRQS;
  202. } else {
  203. reg = x->shared;
  204. offset -= VGIC_NR_PRIVATE_IRQS;
  205. }
  206. return reg + (offset / sizeof(u32));
  207. }
  208. #define VGIC_CFG_LEVEL 0
  209. #define VGIC_CFG_EDGE 1
  210. static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
  211. {
  212. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  213. int irq_val;
  214. irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
  215. return irq_val == VGIC_CFG_EDGE;
  216. }
  217. static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
  218. {
  219. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  220. return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
  221. }
  222. static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq)
  223. {
  224. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  225. return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq);
  226. }
  227. static int vgic_irq_is_active(struct kvm_vcpu *vcpu, int irq)
  228. {
  229. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  230. return vgic_bitmap_get_irq_val(&dist->irq_active, vcpu->vcpu_id, irq);
  231. }
  232. static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq)
  233. {
  234. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  235. vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1);
  236. }
  237. static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq)
  238. {
  239. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  240. vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0);
  241. }
  242. static void vgic_irq_set_active(struct kvm_vcpu *vcpu, int irq)
  243. {
  244. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  245. vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 1);
  246. }
  247. static void vgic_irq_clear_active(struct kvm_vcpu *vcpu, int irq)
  248. {
  249. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  250. vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 0);
  251. }
  252. static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq)
  253. {
  254. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  255. return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq);
  256. }
  257. static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq)
  258. {
  259. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  260. vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1);
  261. }
  262. static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq)
  263. {
  264. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  265. vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0);
  266. }
  267. static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq)
  268. {
  269. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  270. return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq);
  271. }
  272. static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq)
  273. {
  274. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  275. vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0);
  276. }
  277. static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
  278. {
  279. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  280. return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq);
  281. }
  282. void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq)
  283. {
  284. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  285. vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1);
  286. }
  287. void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq)
  288. {
  289. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  290. vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0);
  291. }
  292. static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
  293. {
  294. if (irq < VGIC_NR_PRIVATE_IRQS)
  295. set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
  296. else
  297. set_bit(irq - VGIC_NR_PRIVATE_IRQS,
  298. vcpu->arch.vgic_cpu.pending_shared);
  299. }
  300. void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
  301. {
  302. if (irq < VGIC_NR_PRIVATE_IRQS)
  303. clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
  304. else
  305. clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
  306. vcpu->arch.vgic_cpu.pending_shared);
  307. }
  308. static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq)
  309. {
  310. return vgic_irq_is_edge(vcpu, irq) || !vgic_irq_is_queued(vcpu, irq);
  311. }
  312. /**
  313. * vgic_reg_access - access vgic register
  314. * @mmio: pointer to the data describing the mmio access
  315. * @reg: pointer to the virtual backing of vgic distributor data
  316. * @offset: least significant 2 bits used for word offset
  317. * @mode: ACCESS_ mode (see defines above)
  318. *
  319. * Helper to make vgic register access easier using one of the access
  320. * modes defined for vgic register access
  321. * (read,raz,write-ignored,setbit,clearbit,write)
  322. */
  323. void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
  324. phys_addr_t offset, int mode)
  325. {
  326. int word_offset = (offset & 3) * 8;
  327. u32 mask = (1UL << (mmio->len * 8)) - 1;
  328. u32 regval;
  329. /*
  330. * Any alignment fault should have been delivered to the guest
  331. * directly (ARM ARM B3.12.7 "Prioritization of aborts").
  332. */
  333. if (reg) {
  334. regval = *reg;
  335. } else {
  336. BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
  337. regval = 0;
  338. }
  339. if (mmio->is_write) {
  340. u32 data = mmio_data_read(mmio, mask) << word_offset;
  341. switch (ACCESS_WRITE_MASK(mode)) {
  342. case ACCESS_WRITE_IGNORED:
  343. return;
  344. case ACCESS_WRITE_SETBIT:
  345. regval |= data;
  346. break;
  347. case ACCESS_WRITE_CLEARBIT:
  348. regval &= ~data;
  349. break;
  350. case ACCESS_WRITE_VALUE:
  351. regval = (regval & ~(mask << word_offset)) | data;
  352. break;
  353. }
  354. *reg = regval;
  355. } else {
  356. switch (ACCESS_READ_MASK(mode)) {
  357. case ACCESS_READ_RAZ:
  358. regval = 0;
  359. /* fall through */
  360. case ACCESS_READ_VALUE:
  361. mmio_data_write(mmio, mask, regval >> word_offset);
  362. }
  363. }
  364. }
  365. bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
  366. phys_addr_t offset)
  367. {
  368. vgic_reg_access(mmio, NULL, offset,
  369. ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
  370. return false;
  371. }
  372. bool vgic_handle_enable_reg(struct kvm *kvm, struct kvm_exit_mmio *mmio,
  373. phys_addr_t offset, int vcpu_id, int access)
  374. {
  375. u32 *reg;
  376. int mode = ACCESS_READ_VALUE | access;
  377. struct kvm_vcpu *target_vcpu = kvm_get_vcpu(kvm, vcpu_id);
  378. reg = vgic_bitmap_get_reg(&kvm->arch.vgic.irq_enabled, vcpu_id, offset);
  379. vgic_reg_access(mmio, reg, offset, mode);
  380. if (mmio->is_write) {
  381. if (access & ACCESS_WRITE_CLEARBIT) {
  382. if (offset < 4) /* Force SGI enabled */
  383. *reg |= 0xffff;
  384. vgic_retire_disabled_irqs(target_vcpu);
  385. }
  386. vgic_update_state(kvm);
  387. return true;
  388. }
  389. return false;
  390. }
  391. bool vgic_handle_set_pending_reg(struct kvm *kvm,
  392. struct kvm_exit_mmio *mmio,
  393. phys_addr_t offset, int vcpu_id)
  394. {
  395. u32 *reg, orig;
  396. u32 level_mask;
  397. int mode = ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT;
  398. struct vgic_dist *dist = &kvm->arch.vgic;
  399. reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu_id, offset);
  400. level_mask = (~(*reg));
  401. /* Mark both level and edge triggered irqs as pending */
  402. reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
  403. orig = *reg;
  404. vgic_reg_access(mmio, reg, offset, mode);
  405. if (mmio->is_write) {
  406. /* Set the soft-pending flag only for level-triggered irqs */
  407. reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
  408. vcpu_id, offset);
  409. vgic_reg_access(mmio, reg, offset, mode);
  410. *reg &= level_mask;
  411. /* Ignore writes to SGIs */
  412. if (offset < 2) {
  413. *reg &= ~0xffff;
  414. *reg |= orig & 0xffff;
  415. }
  416. vgic_update_state(kvm);
  417. return true;
  418. }
  419. return false;
  420. }
  421. bool vgic_handle_clear_pending_reg(struct kvm *kvm,
  422. struct kvm_exit_mmio *mmio,
  423. phys_addr_t offset, int vcpu_id)
  424. {
  425. u32 *level_active;
  426. u32 *reg, orig;
  427. int mode = ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT;
  428. struct vgic_dist *dist = &kvm->arch.vgic;
  429. reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
  430. orig = *reg;
  431. vgic_reg_access(mmio, reg, offset, mode);
  432. if (mmio->is_write) {
  433. /* Re-set level triggered level-active interrupts */
  434. level_active = vgic_bitmap_get_reg(&dist->irq_level,
  435. vcpu_id, offset);
  436. reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset);
  437. *reg |= *level_active;
  438. /* Ignore writes to SGIs */
  439. if (offset < 2) {
  440. *reg &= ~0xffff;
  441. *reg |= orig & 0xffff;
  442. }
  443. /* Clear soft-pending flags */
  444. reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
  445. vcpu_id, offset);
  446. vgic_reg_access(mmio, reg, offset, mode);
  447. vgic_update_state(kvm);
  448. return true;
  449. }
  450. return false;
  451. }
  452. bool vgic_handle_set_active_reg(struct kvm *kvm,
  453. struct kvm_exit_mmio *mmio,
  454. phys_addr_t offset, int vcpu_id)
  455. {
  456. u32 *reg;
  457. struct vgic_dist *dist = &kvm->arch.vgic;
  458. reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset);
  459. vgic_reg_access(mmio, reg, offset,
  460. ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
  461. if (mmio->is_write) {
  462. vgic_update_state(kvm);
  463. return true;
  464. }
  465. return false;
  466. }
  467. bool vgic_handle_clear_active_reg(struct kvm *kvm,
  468. struct kvm_exit_mmio *mmio,
  469. phys_addr_t offset, int vcpu_id)
  470. {
  471. u32 *reg;
  472. struct vgic_dist *dist = &kvm->arch.vgic;
  473. reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset);
  474. vgic_reg_access(mmio, reg, offset,
  475. ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
  476. if (mmio->is_write) {
  477. vgic_update_state(kvm);
  478. return true;
  479. }
  480. return false;
  481. }
  482. static u32 vgic_cfg_expand(u16 val)
  483. {
  484. u32 res = 0;
  485. int i;
  486. /*
  487. * Turn a 16bit value like abcd...mnop into a 32bit word
  488. * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
  489. */
  490. for (i = 0; i < 16; i++)
  491. res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
  492. return res;
  493. }
  494. static u16 vgic_cfg_compress(u32 val)
  495. {
  496. u16 res = 0;
  497. int i;
  498. /*
  499. * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
  500. * abcd...mnop which is what we really care about.
  501. */
  502. for (i = 0; i < 16; i++)
  503. res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
  504. return res;
  505. }
  506. /*
  507. * The distributor uses 2 bits per IRQ for the CFG register, but the
  508. * LSB is always 0. As such, we only keep the upper bit, and use the
  509. * two above functions to compress/expand the bits
  510. */
  511. bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio,
  512. phys_addr_t offset)
  513. {
  514. u32 val;
  515. if (offset & 4)
  516. val = *reg >> 16;
  517. else
  518. val = *reg & 0xffff;
  519. val = vgic_cfg_expand(val);
  520. vgic_reg_access(mmio, &val, offset,
  521. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  522. if (mmio->is_write) {
  523. if (offset < 8) {
  524. *reg = ~0U; /* Force PPIs/SGIs to 1 */
  525. return false;
  526. }
  527. val = vgic_cfg_compress(val);
  528. if (offset & 4) {
  529. *reg &= 0xffff;
  530. *reg |= val << 16;
  531. } else {
  532. *reg &= 0xffff << 16;
  533. *reg |= val;
  534. }
  535. }
  536. return false;
  537. }
  538. /**
  539. * vgic_unqueue_irqs - move pending/active IRQs from LRs to the distributor
  540. * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
  541. *
  542. * Move any IRQs that have already been assigned to LRs back to the
  543. * emulated distributor state so that the complete emulated state can be read
  544. * from the main emulation structures without investigating the LRs.
  545. */
  546. void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
  547. {
  548. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  549. int i;
  550. for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
  551. struct vgic_lr lr = vgic_get_lr(vcpu, i);
  552. /*
  553. * There are three options for the state bits:
  554. *
  555. * 01: pending
  556. * 10: active
  557. * 11: pending and active
  558. */
  559. BUG_ON(!(lr.state & LR_STATE_MASK));
  560. /* Reestablish SGI source for pending and active IRQs */
  561. if (lr.irq < VGIC_NR_SGIS)
  562. add_sgi_source(vcpu, lr.irq, lr.source);
  563. /*
  564. * If the LR holds an active (10) or a pending and active (11)
  565. * interrupt then move the active state to the
  566. * distributor tracking bit.
  567. */
  568. if (lr.state & LR_STATE_ACTIVE) {
  569. vgic_irq_set_active(vcpu, lr.irq);
  570. lr.state &= ~LR_STATE_ACTIVE;
  571. }
  572. /*
  573. * Reestablish the pending state on the distributor and the
  574. * CPU interface. It may have already been pending, but that
  575. * is fine, then we are only setting a few bits that were
  576. * already set.
  577. */
  578. if (lr.state & LR_STATE_PENDING) {
  579. vgic_dist_irq_set_pending(vcpu, lr.irq);
  580. lr.state &= ~LR_STATE_PENDING;
  581. }
  582. vgic_set_lr(vcpu, i, lr);
  583. /*
  584. * Mark the LR as free for other use.
  585. */
  586. BUG_ON(lr.state & LR_STATE_MASK);
  587. vgic_retire_lr(i, lr.irq, vcpu);
  588. vgic_irq_clear_queued(vcpu, lr.irq);
  589. /* Finally update the VGIC state. */
  590. vgic_update_state(vcpu->kvm);
  591. }
  592. }
  593. const
  594. struct vgic_io_range *vgic_find_range(const struct vgic_io_range *ranges,
  595. int len, gpa_t offset)
  596. {
  597. while (ranges->len) {
  598. if (offset >= ranges->base &&
  599. (offset + len) <= (ranges->base + ranges->len))
  600. return ranges;
  601. ranges++;
  602. }
  603. return NULL;
  604. }
  605. static bool vgic_validate_access(const struct vgic_dist *dist,
  606. const struct vgic_io_range *range,
  607. unsigned long offset)
  608. {
  609. int irq;
  610. if (!range->bits_per_irq)
  611. return true; /* Not an irq-based access */
  612. irq = offset * 8 / range->bits_per_irq;
  613. if (irq >= dist->nr_irqs)
  614. return false;
  615. return true;
  616. }
  617. /*
  618. * Call the respective handler function for the given range.
  619. * We split up any 64 bit accesses into two consecutive 32 bit
  620. * handler calls and merge the result afterwards.
  621. * We do this in a little endian fashion regardless of the host's
  622. * or guest's endianness, because the GIC is always LE and the rest of
  623. * the code (vgic_reg_access) also puts it in a LE fashion already.
  624. * At this point we have already identified the handle function, so
  625. * range points to that one entry and offset is relative to this.
  626. */
  627. static bool call_range_handler(struct kvm_vcpu *vcpu,
  628. struct kvm_exit_mmio *mmio,
  629. unsigned long offset,
  630. const struct vgic_io_range *range)
  631. {
  632. struct kvm_exit_mmio mmio32;
  633. bool ret;
  634. if (likely(mmio->len <= 4))
  635. return range->handle_mmio(vcpu, mmio, offset);
  636. /*
  637. * Any access bigger than 4 bytes (that we currently handle in KVM)
  638. * is actually 8 bytes long, caused by a 64-bit access
  639. */
  640. mmio32.len = 4;
  641. mmio32.is_write = mmio->is_write;
  642. mmio32.private = mmio->private;
  643. mmio32.phys_addr = mmio->phys_addr + 4;
  644. mmio32.data = &((u32 *)mmio->data)[1];
  645. ret = range->handle_mmio(vcpu, &mmio32, offset + 4);
  646. mmio32.phys_addr = mmio->phys_addr;
  647. mmio32.data = &((u32 *)mmio->data)[0];
  648. ret |= range->handle_mmio(vcpu, &mmio32, offset);
  649. return ret;
  650. }
  651. /**
  652. * vgic_handle_mmio_access - handle an in-kernel MMIO access
  653. * This is called by the read/write KVM IO device wrappers below.
  654. * @vcpu: pointer to the vcpu performing the access
  655. * @this: pointer to the KVM IO device in charge
  656. * @addr: guest physical address of the access
  657. * @len: size of the access
  658. * @val: pointer to the data region
  659. * @is_write: read or write access
  660. *
  661. * returns true if the MMIO access could be performed
  662. */
  663. static int vgic_handle_mmio_access(struct kvm_vcpu *vcpu,
  664. struct kvm_io_device *this, gpa_t addr,
  665. int len, void *val, bool is_write)
  666. {
  667. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  668. struct vgic_io_device *iodev = container_of(this,
  669. struct vgic_io_device, dev);
  670. struct kvm_run *run = vcpu->run;
  671. const struct vgic_io_range *range;
  672. struct kvm_exit_mmio mmio;
  673. bool updated_state;
  674. gpa_t offset;
  675. offset = addr - iodev->addr;
  676. range = vgic_find_range(iodev->reg_ranges, len, offset);
  677. if (unlikely(!range || !range->handle_mmio)) {
  678. pr_warn("Unhandled access %d %08llx %d\n", is_write, addr, len);
  679. return -ENXIO;
  680. }
  681. mmio.phys_addr = addr;
  682. mmio.len = len;
  683. mmio.is_write = is_write;
  684. mmio.data = val;
  685. mmio.private = iodev->redist_vcpu;
  686. spin_lock(&dist->lock);
  687. offset -= range->base;
  688. if (vgic_validate_access(dist, range, offset)) {
  689. updated_state = call_range_handler(vcpu, &mmio, offset, range);
  690. } else {
  691. if (!is_write)
  692. memset(val, 0, len);
  693. updated_state = false;
  694. }
  695. spin_unlock(&dist->lock);
  696. run->mmio.is_write = is_write;
  697. run->mmio.len = len;
  698. run->mmio.phys_addr = addr;
  699. memcpy(run->mmio.data, val, len);
  700. kvm_handle_mmio_return(vcpu, run);
  701. if (updated_state)
  702. vgic_kick_vcpus(vcpu->kvm);
  703. return 0;
  704. }
  705. static int vgic_handle_mmio_read(struct kvm_vcpu *vcpu,
  706. struct kvm_io_device *this,
  707. gpa_t addr, int len, void *val)
  708. {
  709. return vgic_handle_mmio_access(vcpu, this, addr, len, val, false);
  710. }
  711. static int vgic_handle_mmio_write(struct kvm_vcpu *vcpu,
  712. struct kvm_io_device *this,
  713. gpa_t addr, int len, const void *val)
  714. {
  715. return vgic_handle_mmio_access(vcpu, this, addr, len, (void *)val,
  716. true);
  717. }
  718. struct kvm_io_device_ops vgic_io_ops = {
  719. .read = vgic_handle_mmio_read,
  720. .write = vgic_handle_mmio_write,
  721. };
  722. /**
  723. * vgic_register_kvm_io_dev - register VGIC register frame on the KVM I/O bus
  724. * @kvm: The VM structure pointer
  725. * @base: The (guest) base address for the register frame
  726. * @len: Length of the register frame window
  727. * @ranges: Describing the handler functions for each register
  728. * @redist_vcpu_id: The VCPU ID to pass on to the handlers on call
  729. * @iodev: Points to memory to be passed on to the handler
  730. *
  731. * @iodev stores the parameters of this function to be usable by the handler
  732. * respectively the dispatcher function (since the KVM I/O bus framework lacks
  733. * an opaque parameter). Initialization is done in this function, but the
  734. * reference should be valid and unique for the whole VGIC lifetime.
  735. * If the register frame is not mapped for a specific VCPU, pass -1 to
  736. * @redist_vcpu_id.
  737. */
  738. int vgic_register_kvm_io_dev(struct kvm *kvm, gpa_t base, int len,
  739. const struct vgic_io_range *ranges,
  740. int redist_vcpu_id,
  741. struct vgic_io_device *iodev)
  742. {
  743. struct kvm_vcpu *vcpu = NULL;
  744. int ret;
  745. if (redist_vcpu_id >= 0)
  746. vcpu = kvm_get_vcpu(kvm, redist_vcpu_id);
  747. iodev->addr = base;
  748. iodev->len = len;
  749. iodev->reg_ranges = ranges;
  750. iodev->redist_vcpu = vcpu;
  751. kvm_iodevice_init(&iodev->dev, &vgic_io_ops);
  752. mutex_lock(&kvm->slots_lock);
  753. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, base, len,
  754. &iodev->dev);
  755. mutex_unlock(&kvm->slots_lock);
  756. /* Mark the iodev as invalid if registration fails. */
  757. if (ret)
  758. iodev->dev.ops = NULL;
  759. return ret;
  760. }
  761. static int vgic_nr_shared_irqs(struct vgic_dist *dist)
  762. {
  763. return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS;
  764. }
  765. static int compute_active_for_cpu(struct kvm_vcpu *vcpu)
  766. {
  767. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  768. unsigned long *active, *enabled, *act_percpu, *act_shared;
  769. unsigned long active_private, active_shared;
  770. int nr_shared = vgic_nr_shared_irqs(dist);
  771. int vcpu_id;
  772. vcpu_id = vcpu->vcpu_id;
  773. act_percpu = vcpu->arch.vgic_cpu.active_percpu;
  774. act_shared = vcpu->arch.vgic_cpu.active_shared;
  775. active = vgic_bitmap_get_cpu_map(&dist->irq_active, vcpu_id);
  776. enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
  777. bitmap_and(act_percpu, active, enabled, VGIC_NR_PRIVATE_IRQS);
  778. active = vgic_bitmap_get_shared_map(&dist->irq_active);
  779. enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
  780. bitmap_and(act_shared, active, enabled, nr_shared);
  781. bitmap_and(act_shared, act_shared,
  782. vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
  783. nr_shared);
  784. active_private = find_first_bit(act_percpu, VGIC_NR_PRIVATE_IRQS);
  785. active_shared = find_first_bit(act_shared, nr_shared);
  786. return (active_private < VGIC_NR_PRIVATE_IRQS ||
  787. active_shared < nr_shared);
  788. }
  789. static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
  790. {
  791. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  792. unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
  793. unsigned long pending_private, pending_shared;
  794. int nr_shared = vgic_nr_shared_irqs(dist);
  795. int vcpu_id;
  796. vcpu_id = vcpu->vcpu_id;
  797. pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
  798. pend_shared = vcpu->arch.vgic_cpu.pending_shared;
  799. pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
  800. enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
  801. bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
  802. pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
  803. enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
  804. bitmap_and(pend_shared, pending, enabled, nr_shared);
  805. bitmap_and(pend_shared, pend_shared,
  806. vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
  807. nr_shared);
  808. pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
  809. pending_shared = find_first_bit(pend_shared, nr_shared);
  810. return (pending_private < VGIC_NR_PRIVATE_IRQS ||
  811. pending_shared < vgic_nr_shared_irqs(dist));
  812. }
  813. /*
  814. * Update the interrupt state and determine which CPUs have pending
  815. * or active interrupts. Must be called with distributor lock held.
  816. */
  817. void vgic_update_state(struct kvm *kvm)
  818. {
  819. struct vgic_dist *dist = &kvm->arch.vgic;
  820. struct kvm_vcpu *vcpu;
  821. int c;
  822. if (!dist->enabled) {
  823. set_bit(0, dist->irq_pending_on_cpu);
  824. return;
  825. }
  826. kvm_for_each_vcpu(c, vcpu, kvm) {
  827. if (compute_pending_for_cpu(vcpu))
  828. set_bit(c, dist->irq_pending_on_cpu);
  829. if (compute_active_for_cpu(vcpu))
  830. set_bit(c, dist->irq_active_on_cpu);
  831. else
  832. clear_bit(c, dist->irq_active_on_cpu);
  833. }
  834. }
  835. static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
  836. {
  837. return vgic_ops->get_lr(vcpu, lr);
  838. }
  839. static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
  840. struct vgic_lr vlr)
  841. {
  842. vgic_ops->set_lr(vcpu, lr, vlr);
  843. }
  844. static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
  845. struct vgic_lr vlr)
  846. {
  847. vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
  848. }
  849. static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
  850. {
  851. return vgic_ops->get_elrsr(vcpu);
  852. }
  853. static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
  854. {
  855. return vgic_ops->get_eisr(vcpu);
  856. }
  857. static inline void vgic_clear_eisr(struct kvm_vcpu *vcpu)
  858. {
  859. vgic_ops->clear_eisr(vcpu);
  860. }
  861. static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
  862. {
  863. return vgic_ops->get_interrupt_status(vcpu);
  864. }
  865. static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
  866. {
  867. vgic_ops->enable_underflow(vcpu);
  868. }
  869. static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
  870. {
  871. vgic_ops->disable_underflow(vcpu);
  872. }
  873. void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
  874. {
  875. vgic_ops->get_vmcr(vcpu, vmcr);
  876. }
  877. void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
  878. {
  879. vgic_ops->set_vmcr(vcpu, vmcr);
  880. }
  881. static inline void vgic_enable(struct kvm_vcpu *vcpu)
  882. {
  883. vgic_ops->enable(vcpu);
  884. }
  885. static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
  886. {
  887. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  888. struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
  889. vlr.state = 0;
  890. vgic_set_lr(vcpu, lr_nr, vlr);
  891. clear_bit(lr_nr, vgic_cpu->lr_used);
  892. vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
  893. vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
  894. }
  895. /*
  896. * An interrupt may have been disabled after being made pending on the
  897. * CPU interface (the classic case is a timer running while we're
  898. * rebooting the guest - the interrupt would kick as soon as the CPU
  899. * interface gets enabled, with deadly consequences).
  900. *
  901. * The solution is to examine already active LRs, and check the
  902. * interrupt is still enabled. If not, just retire it.
  903. */
  904. static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
  905. {
  906. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  907. int lr;
  908. for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
  909. struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
  910. if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
  911. vgic_retire_lr(lr, vlr.irq, vcpu);
  912. if (vgic_irq_is_queued(vcpu, vlr.irq))
  913. vgic_irq_clear_queued(vcpu, vlr.irq);
  914. }
  915. }
  916. }
  917. static void vgic_queue_irq_to_lr(struct kvm_vcpu *vcpu, int irq,
  918. int lr_nr, struct vgic_lr vlr)
  919. {
  920. if (vgic_irq_is_active(vcpu, irq)) {
  921. vlr.state |= LR_STATE_ACTIVE;
  922. kvm_debug("Set active, clear distributor: 0x%x\n", vlr.state);
  923. vgic_irq_clear_active(vcpu, irq);
  924. vgic_update_state(vcpu->kvm);
  925. } else if (vgic_dist_irq_is_pending(vcpu, irq)) {
  926. vlr.state |= LR_STATE_PENDING;
  927. kvm_debug("Set pending: 0x%x\n", vlr.state);
  928. }
  929. if (!vgic_irq_is_edge(vcpu, irq))
  930. vlr.state |= LR_EOI_INT;
  931. vgic_set_lr(vcpu, lr_nr, vlr);
  932. vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
  933. }
  934. /*
  935. * Queue an interrupt to a CPU virtual interface. Return true on success,
  936. * or false if it wasn't possible to queue it.
  937. * sgi_source must be zero for any non-SGI interrupts.
  938. */
  939. bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
  940. {
  941. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  942. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  943. struct vgic_lr vlr;
  944. int lr;
  945. /* Sanitize the input... */
  946. BUG_ON(sgi_source_id & ~7);
  947. BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
  948. BUG_ON(irq >= dist->nr_irqs);
  949. kvm_debug("Queue IRQ%d\n", irq);
  950. lr = vgic_cpu->vgic_irq_lr_map[irq];
  951. /* Do we have an active interrupt for the same CPUID? */
  952. if (lr != LR_EMPTY) {
  953. vlr = vgic_get_lr(vcpu, lr);
  954. if (vlr.source == sgi_source_id) {
  955. kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
  956. BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
  957. vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
  958. return true;
  959. }
  960. }
  961. /* Try to use another LR for this interrupt */
  962. lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
  963. vgic->nr_lr);
  964. if (lr >= vgic->nr_lr)
  965. return false;
  966. kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
  967. vgic_cpu->vgic_irq_lr_map[irq] = lr;
  968. set_bit(lr, vgic_cpu->lr_used);
  969. vlr.irq = irq;
  970. vlr.source = sgi_source_id;
  971. vlr.state = 0;
  972. vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
  973. return true;
  974. }
  975. static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
  976. {
  977. if (!vgic_can_sample_irq(vcpu, irq))
  978. return true; /* level interrupt, already queued */
  979. if (vgic_queue_irq(vcpu, 0, irq)) {
  980. if (vgic_irq_is_edge(vcpu, irq)) {
  981. vgic_dist_irq_clear_pending(vcpu, irq);
  982. vgic_cpu_irq_clear(vcpu, irq);
  983. } else {
  984. vgic_irq_set_queued(vcpu, irq);
  985. }
  986. return true;
  987. }
  988. return false;
  989. }
  990. /*
  991. * Fill the list registers with pending interrupts before running the
  992. * guest.
  993. */
  994. static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  995. {
  996. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  997. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  998. unsigned long *pa_percpu, *pa_shared;
  999. int i, vcpu_id;
  1000. int overflow = 0;
  1001. int nr_shared = vgic_nr_shared_irqs(dist);
  1002. vcpu_id = vcpu->vcpu_id;
  1003. pa_percpu = vcpu->arch.vgic_cpu.pend_act_percpu;
  1004. pa_shared = vcpu->arch.vgic_cpu.pend_act_shared;
  1005. bitmap_or(pa_percpu, vgic_cpu->pending_percpu, vgic_cpu->active_percpu,
  1006. VGIC_NR_PRIVATE_IRQS);
  1007. bitmap_or(pa_shared, vgic_cpu->pending_shared, vgic_cpu->active_shared,
  1008. nr_shared);
  1009. /*
  1010. * We may not have any pending interrupt, or the interrupts
  1011. * may have been serviced from another vcpu. In all cases,
  1012. * move along.
  1013. */
  1014. if (!kvm_vgic_vcpu_pending_irq(vcpu) && !kvm_vgic_vcpu_active_irq(vcpu))
  1015. goto epilog;
  1016. /* SGIs */
  1017. for_each_set_bit(i, pa_percpu, VGIC_NR_SGIS) {
  1018. if (!queue_sgi(vcpu, i))
  1019. overflow = 1;
  1020. }
  1021. /* PPIs */
  1022. for_each_set_bit_from(i, pa_percpu, VGIC_NR_PRIVATE_IRQS) {
  1023. if (!vgic_queue_hwirq(vcpu, i))
  1024. overflow = 1;
  1025. }
  1026. /* SPIs */
  1027. for_each_set_bit(i, pa_shared, nr_shared) {
  1028. if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
  1029. overflow = 1;
  1030. }
  1031. epilog:
  1032. if (overflow) {
  1033. vgic_enable_underflow(vcpu);
  1034. } else {
  1035. vgic_disable_underflow(vcpu);
  1036. /*
  1037. * We're about to run this VCPU, and we've consumed
  1038. * everything the distributor had in store for
  1039. * us. Claim we don't have anything pending. We'll
  1040. * adjust that if needed while exiting.
  1041. */
  1042. clear_bit(vcpu_id, dist->irq_pending_on_cpu);
  1043. }
  1044. }
  1045. static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
  1046. {
  1047. u32 status = vgic_get_interrupt_status(vcpu);
  1048. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1049. bool level_pending = false;
  1050. struct kvm *kvm = vcpu->kvm;
  1051. kvm_debug("STATUS = %08x\n", status);
  1052. if (status & INT_STATUS_EOI) {
  1053. /*
  1054. * Some level interrupts have been EOIed. Clear their
  1055. * active bit.
  1056. */
  1057. u64 eisr = vgic_get_eisr(vcpu);
  1058. unsigned long *eisr_ptr = u64_to_bitmask(&eisr);
  1059. int lr;
  1060. for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
  1061. struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
  1062. WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
  1063. spin_lock(&dist->lock);
  1064. vgic_irq_clear_queued(vcpu, vlr.irq);
  1065. WARN_ON(vlr.state & LR_STATE_MASK);
  1066. vlr.state = 0;
  1067. vgic_set_lr(vcpu, lr, vlr);
  1068. /*
  1069. * If the IRQ was EOIed it was also ACKed and we we
  1070. * therefore assume we can clear the soft pending
  1071. * state (should it had been set) for this interrupt.
  1072. *
  1073. * Note: if the IRQ soft pending state was set after
  1074. * the IRQ was acked, it actually shouldn't be
  1075. * cleared, but we have no way of knowing that unless
  1076. * we start trapping ACKs when the soft-pending state
  1077. * is set.
  1078. */
  1079. vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
  1080. /*
  1081. * kvm_notify_acked_irq calls kvm_set_irq()
  1082. * to reset the IRQ level. Need to release the
  1083. * lock for kvm_set_irq to grab it.
  1084. */
  1085. spin_unlock(&dist->lock);
  1086. kvm_notify_acked_irq(kvm, 0,
  1087. vlr.irq - VGIC_NR_PRIVATE_IRQS);
  1088. spin_lock(&dist->lock);
  1089. /* Any additional pending interrupt? */
  1090. if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
  1091. vgic_cpu_irq_set(vcpu, vlr.irq);
  1092. level_pending = true;
  1093. } else {
  1094. vgic_dist_irq_clear_pending(vcpu, vlr.irq);
  1095. vgic_cpu_irq_clear(vcpu, vlr.irq);
  1096. }
  1097. spin_unlock(&dist->lock);
  1098. /*
  1099. * Despite being EOIed, the LR may not have
  1100. * been marked as empty.
  1101. */
  1102. vgic_sync_lr_elrsr(vcpu, lr, vlr);
  1103. }
  1104. }
  1105. if (status & INT_STATUS_UNDERFLOW)
  1106. vgic_disable_underflow(vcpu);
  1107. /*
  1108. * In the next iterations of the vcpu loop, if we sync the vgic state
  1109. * after flushing it, but before entering the guest (this happens for
  1110. * pending signals and vmid rollovers), then make sure we don't pick
  1111. * up any old maintenance interrupts here.
  1112. */
  1113. vgic_clear_eisr(vcpu);
  1114. return level_pending;
  1115. }
  1116. /* Sync back the VGIC state after a guest run */
  1117. static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  1118. {
  1119. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1120. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1121. u64 elrsr;
  1122. unsigned long *elrsr_ptr;
  1123. int lr, pending;
  1124. bool level_pending;
  1125. level_pending = vgic_process_maintenance(vcpu);
  1126. elrsr = vgic_get_elrsr(vcpu);
  1127. elrsr_ptr = u64_to_bitmask(&elrsr);
  1128. /* Clear mappings for empty LRs */
  1129. for_each_set_bit(lr, elrsr_ptr, vgic->nr_lr) {
  1130. struct vgic_lr vlr;
  1131. if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
  1132. continue;
  1133. vlr = vgic_get_lr(vcpu, lr);
  1134. BUG_ON(vlr.irq >= dist->nr_irqs);
  1135. vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
  1136. }
  1137. /* Check if we still have something up our sleeve... */
  1138. pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
  1139. if (level_pending || pending < vgic->nr_lr)
  1140. set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
  1141. }
  1142. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  1143. {
  1144. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1145. if (!irqchip_in_kernel(vcpu->kvm))
  1146. return;
  1147. spin_lock(&dist->lock);
  1148. __kvm_vgic_flush_hwstate(vcpu);
  1149. spin_unlock(&dist->lock);
  1150. }
  1151. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  1152. {
  1153. if (!irqchip_in_kernel(vcpu->kvm))
  1154. return;
  1155. __kvm_vgic_sync_hwstate(vcpu);
  1156. }
  1157. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  1158. {
  1159. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1160. if (!irqchip_in_kernel(vcpu->kvm))
  1161. return 0;
  1162. return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
  1163. }
  1164. int kvm_vgic_vcpu_active_irq(struct kvm_vcpu *vcpu)
  1165. {
  1166. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1167. if (!irqchip_in_kernel(vcpu->kvm))
  1168. return 0;
  1169. return test_bit(vcpu->vcpu_id, dist->irq_active_on_cpu);
  1170. }
  1171. void vgic_kick_vcpus(struct kvm *kvm)
  1172. {
  1173. struct kvm_vcpu *vcpu;
  1174. int c;
  1175. /*
  1176. * We've injected an interrupt, time to find out who deserves
  1177. * a good kick...
  1178. */
  1179. kvm_for_each_vcpu(c, vcpu, kvm) {
  1180. if (kvm_vgic_vcpu_pending_irq(vcpu))
  1181. kvm_vcpu_kick(vcpu);
  1182. }
  1183. }
  1184. static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
  1185. {
  1186. int edge_triggered = vgic_irq_is_edge(vcpu, irq);
  1187. /*
  1188. * Only inject an interrupt if:
  1189. * - edge triggered and we have a rising edge
  1190. * - level triggered and we change level
  1191. */
  1192. if (edge_triggered) {
  1193. int state = vgic_dist_irq_is_pending(vcpu, irq);
  1194. return level > state;
  1195. } else {
  1196. int state = vgic_dist_irq_get_level(vcpu, irq);
  1197. return level != state;
  1198. }
  1199. }
  1200. static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
  1201. unsigned int irq_num, bool level)
  1202. {
  1203. struct vgic_dist *dist = &kvm->arch.vgic;
  1204. struct kvm_vcpu *vcpu;
  1205. int edge_triggered, level_triggered;
  1206. int enabled;
  1207. bool ret = true, can_inject = true;
  1208. spin_lock(&dist->lock);
  1209. vcpu = kvm_get_vcpu(kvm, cpuid);
  1210. edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
  1211. level_triggered = !edge_triggered;
  1212. if (!vgic_validate_injection(vcpu, irq_num, level)) {
  1213. ret = false;
  1214. goto out;
  1215. }
  1216. if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
  1217. cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
  1218. if (cpuid == VCPU_NOT_ALLOCATED) {
  1219. /* Pretend we use CPU0, and prevent injection */
  1220. cpuid = 0;
  1221. can_inject = false;
  1222. }
  1223. vcpu = kvm_get_vcpu(kvm, cpuid);
  1224. }
  1225. kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
  1226. if (level) {
  1227. if (level_triggered)
  1228. vgic_dist_irq_set_level(vcpu, irq_num);
  1229. vgic_dist_irq_set_pending(vcpu, irq_num);
  1230. } else {
  1231. if (level_triggered) {
  1232. vgic_dist_irq_clear_level(vcpu, irq_num);
  1233. if (!vgic_dist_irq_soft_pend(vcpu, irq_num))
  1234. vgic_dist_irq_clear_pending(vcpu, irq_num);
  1235. }
  1236. ret = false;
  1237. goto out;
  1238. }
  1239. enabled = vgic_irq_is_enabled(vcpu, irq_num);
  1240. if (!enabled || !can_inject) {
  1241. ret = false;
  1242. goto out;
  1243. }
  1244. if (!vgic_can_sample_irq(vcpu, irq_num)) {
  1245. /*
  1246. * Level interrupt in progress, will be picked up
  1247. * when EOId.
  1248. */
  1249. ret = false;
  1250. goto out;
  1251. }
  1252. if (level) {
  1253. vgic_cpu_irq_set(vcpu, irq_num);
  1254. set_bit(cpuid, dist->irq_pending_on_cpu);
  1255. }
  1256. out:
  1257. spin_unlock(&dist->lock);
  1258. return ret ? cpuid : -EINVAL;
  1259. }
  1260. /**
  1261. * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
  1262. * @kvm: The VM structure pointer
  1263. * @cpuid: The CPU for PPIs
  1264. * @irq_num: The IRQ number that is assigned to the device
  1265. * @level: Edge-triggered: true: to trigger the interrupt
  1266. * false: to ignore the call
  1267. * Level-sensitive true: activates an interrupt
  1268. * false: deactivates an interrupt
  1269. *
  1270. * The GIC is not concerned with devices being active-LOW or active-HIGH for
  1271. * level-sensitive interrupts. You can think of the level parameter as 1
  1272. * being HIGH and 0 being LOW and all devices being active-HIGH.
  1273. */
  1274. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
  1275. bool level)
  1276. {
  1277. int ret = 0;
  1278. int vcpu_id;
  1279. if (unlikely(!vgic_initialized(kvm))) {
  1280. /*
  1281. * We only provide the automatic initialization of the VGIC
  1282. * for the legacy case of a GICv2. Any other type must
  1283. * be explicitly initialized once setup with the respective
  1284. * KVM device call.
  1285. */
  1286. if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2) {
  1287. ret = -EBUSY;
  1288. goto out;
  1289. }
  1290. mutex_lock(&kvm->lock);
  1291. ret = vgic_init(kvm);
  1292. mutex_unlock(&kvm->lock);
  1293. if (ret)
  1294. goto out;
  1295. }
  1296. if (irq_num >= kvm->arch.vgic.nr_irqs)
  1297. return -EINVAL;
  1298. vcpu_id = vgic_update_irq_pending(kvm, cpuid, irq_num, level);
  1299. if (vcpu_id >= 0) {
  1300. /* kick the specified vcpu */
  1301. kvm_vcpu_kick(kvm_get_vcpu(kvm, vcpu_id));
  1302. }
  1303. out:
  1304. return ret;
  1305. }
  1306. static irqreturn_t vgic_maintenance_handler(int irq, void *data)
  1307. {
  1308. /*
  1309. * We cannot rely on the vgic maintenance interrupt to be
  1310. * delivered synchronously. This means we can only use it to
  1311. * exit the VM, and we perform the handling of EOIed
  1312. * interrupts on the exit path (see vgic_process_maintenance).
  1313. */
  1314. return IRQ_HANDLED;
  1315. }
  1316. void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
  1317. {
  1318. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1319. kfree(vgic_cpu->pending_shared);
  1320. kfree(vgic_cpu->active_shared);
  1321. kfree(vgic_cpu->pend_act_shared);
  1322. kfree(vgic_cpu->vgic_irq_lr_map);
  1323. vgic_cpu->pending_shared = NULL;
  1324. vgic_cpu->active_shared = NULL;
  1325. vgic_cpu->pend_act_shared = NULL;
  1326. vgic_cpu->vgic_irq_lr_map = NULL;
  1327. }
  1328. static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
  1329. {
  1330. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1331. int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
  1332. vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
  1333. vgic_cpu->active_shared = kzalloc(sz, GFP_KERNEL);
  1334. vgic_cpu->pend_act_shared = kzalloc(sz, GFP_KERNEL);
  1335. vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
  1336. if (!vgic_cpu->pending_shared
  1337. || !vgic_cpu->active_shared
  1338. || !vgic_cpu->pend_act_shared
  1339. || !vgic_cpu->vgic_irq_lr_map) {
  1340. kvm_vgic_vcpu_destroy(vcpu);
  1341. return -ENOMEM;
  1342. }
  1343. memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
  1344. /*
  1345. * Store the number of LRs per vcpu, so we don't have to go
  1346. * all the way to the distributor structure to find out. Only
  1347. * assembly code should use this one.
  1348. */
  1349. vgic_cpu->nr_lr = vgic->nr_lr;
  1350. return 0;
  1351. }
  1352. /**
  1353. * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
  1354. *
  1355. * The host's GIC naturally limits the maximum amount of VCPUs a guest
  1356. * can use.
  1357. */
  1358. int kvm_vgic_get_max_vcpus(void)
  1359. {
  1360. return vgic->max_gic_vcpus;
  1361. }
  1362. void kvm_vgic_destroy(struct kvm *kvm)
  1363. {
  1364. struct vgic_dist *dist = &kvm->arch.vgic;
  1365. struct kvm_vcpu *vcpu;
  1366. int i;
  1367. kvm_for_each_vcpu(i, vcpu, kvm)
  1368. kvm_vgic_vcpu_destroy(vcpu);
  1369. vgic_free_bitmap(&dist->irq_enabled);
  1370. vgic_free_bitmap(&dist->irq_level);
  1371. vgic_free_bitmap(&dist->irq_pending);
  1372. vgic_free_bitmap(&dist->irq_soft_pend);
  1373. vgic_free_bitmap(&dist->irq_queued);
  1374. vgic_free_bitmap(&dist->irq_cfg);
  1375. vgic_free_bytemap(&dist->irq_priority);
  1376. if (dist->irq_spi_target) {
  1377. for (i = 0; i < dist->nr_cpus; i++)
  1378. vgic_free_bitmap(&dist->irq_spi_target[i]);
  1379. }
  1380. kfree(dist->irq_sgi_sources);
  1381. kfree(dist->irq_spi_cpu);
  1382. kfree(dist->irq_spi_mpidr);
  1383. kfree(dist->irq_spi_target);
  1384. kfree(dist->irq_pending_on_cpu);
  1385. kfree(dist->irq_active_on_cpu);
  1386. dist->irq_sgi_sources = NULL;
  1387. dist->irq_spi_cpu = NULL;
  1388. dist->irq_spi_target = NULL;
  1389. dist->irq_pending_on_cpu = NULL;
  1390. dist->irq_active_on_cpu = NULL;
  1391. dist->nr_cpus = 0;
  1392. }
  1393. /*
  1394. * Allocate and initialize the various data structures. Must be called
  1395. * with kvm->lock held!
  1396. */
  1397. int vgic_init(struct kvm *kvm)
  1398. {
  1399. struct vgic_dist *dist = &kvm->arch.vgic;
  1400. struct kvm_vcpu *vcpu;
  1401. int nr_cpus, nr_irqs;
  1402. int ret, i, vcpu_id;
  1403. if (vgic_initialized(kvm))
  1404. return 0;
  1405. nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
  1406. if (!nr_cpus) /* No vcpus? Can't be good... */
  1407. return -ENODEV;
  1408. /*
  1409. * If nobody configured the number of interrupts, use the
  1410. * legacy one.
  1411. */
  1412. if (!dist->nr_irqs)
  1413. dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
  1414. nr_irqs = dist->nr_irqs;
  1415. ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs);
  1416. ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs);
  1417. ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs);
  1418. ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs);
  1419. ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs);
  1420. ret |= vgic_init_bitmap(&dist->irq_active, nr_cpus, nr_irqs);
  1421. ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs);
  1422. ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs);
  1423. if (ret)
  1424. goto out;
  1425. dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL);
  1426. dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL);
  1427. dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus,
  1428. GFP_KERNEL);
  1429. dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
  1430. GFP_KERNEL);
  1431. dist->irq_active_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
  1432. GFP_KERNEL);
  1433. if (!dist->irq_sgi_sources ||
  1434. !dist->irq_spi_cpu ||
  1435. !dist->irq_spi_target ||
  1436. !dist->irq_pending_on_cpu ||
  1437. !dist->irq_active_on_cpu) {
  1438. ret = -ENOMEM;
  1439. goto out;
  1440. }
  1441. for (i = 0; i < nr_cpus; i++)
  1442. ret |= vgic_init_bitmap(&dist->irq_spi_target[i],
  1443. nr_cpus, nr_irqs);
  1444. if (ret)
  1445. goto out;
  1446. ret = kvm->arch.vgic.vm_ops.init_model(kvm);
  1447. if (ret)
  1448. goto out;
  1449. kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
  1450. ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
  1451. if (ret) {
  1452. kvm_err("VGIC: Failed to allocate vcpu memory\n");
  1453. break;
  1454. }
  1455. for (i = 0; i < dist->nr_irqs; i++) {
  1456. if (i < VGIC_NR_PPIS)
  1457. vgic_bitmap_set_irq_val(&dist->irq_enabled,
  1458. vcpu->vcpu_id, i, 1);
  1459. if (i < VGIC_NR_PRIVATE_IRQS)
  1460. vgic_bitmap_set_irq_val(&dist->irq_cfg,
  1461. vcpu->vcpu_id, i,
  1462. VGIC_CFG_EDGE);
  1463. }
  1464. vgic_enable(vcpu);
  1465. }
  1466. out:
  1467. if (ret)
  1468. kvm_vgic_destroy(kvm);
  1469. return ret;
  1470. }
  1471. static int init_vgic_model(struct kvm *kvm, int type)
  1472. {
  1473. switch (type) {
  1474. case KVM_DEV_TYPE_ARM_VGIC_V2:
  1475. vgic_v2_init_emulation(kvm);
  1476. break;
  1477. #ifdef CONFIG_ARM_GIC_V3
  1478. case KVM_DEV_TYPE_ARM_VGIC_V3:
  1479. vgic_v3_init_emulation(kvm);
  1480. break;
  1481. #endif
  1482. default:
  1483. return -ENODEV;
  1484. }
  1485. if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus)
  1486. return -E2BIG;
  1487. return 0;
  1488. }
  1489. int kvm_vgic_create(struct kvm *kvm, u32 type)
  1490. {
  1491. int i, vcpu_lock_idx = -1, ret;
  1492. struct kvm_vcpu *vcpu;
  1493. mutex_lock(&kvm->lock);
  1494. if (irqchip_in_kernel(kvm)) {
  1495. ret = -EEXIST;
  1496. goto out;
  1497. }
  1498. /*
  1499. * This function is also called by the KVM_CREATE_IRQCHIP handler,
  1500. * which had no chance yet to check the availability of the GICv2
  1501. * emulation. So check this here again. KVM_CREATE_DEVICE does
  1502. * the proper checks already.
  1503. */
  1504. if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && !vgic->can_emulate_gicv2) {
  1505. ret = -ENODEV;
  1506. goto out;
  1507. }
  1508. /*
  1509. * Any time a vcpu is run, vcpu_load is called which tries to grab the
  1510. * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
  1511. * that no other VCPUs are run while we create the vgic.
  1512. */
  1513. ret = -EBUSY;
  1514. kvm_for_each_vcpu(i, vcpu, kvm) {
  1515. if (!mutex_trylock(&vcpu->mutex))
  1516. goto out_unlock;
  1517. vcpu_lock_idx = i;
  1518. }
  1519. kvm_for_each_vcpu(i, vcpu, kvm) {
  1520. if (vcpu->arch.has_run_once)
  1521. goto out_unlock;
  1522. }
  1523. ret = 0;
  1524. ret = init_vgic_model(kvm, type);
  1525. if (ret)
  1526. goto out_unlock;
  1527. spin_lock_init(&kvm->arch.vgic.lock);
  1528. kvm->arch.vgic.in_kernel = true;
  1529. kvm->arch.vgic.vgic_model = type;
  1530. kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
  1531. kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
  1532. kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
  1533. kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF;
  1534. out_unlock:
  1535. for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
  1536. vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
  1537. mutex_unlock(&vcpu->mutex);
  1538. }
  1539. out:
  1540. mutex_unlock(&kvm->lock);
  1541. return ret;
  1542. }
  1543. static int vgic_ioaddr_overlap(struct kvm *kvm)
  1544. {
  1545. phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
  1546. phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
  1547. if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
  1548. return 0;
  1549. if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
  1550. (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
  1551. return -EBUSY;
  1552. return 0;
  1553. }
  1554. static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
  1555. phys_addr_t addr, phys_addr_t size)
  1556. {
  1557. int ret;
  1558. if (addr & ~KVM_PHYS_MASK)
  1559. return -E2BIG;
  1560. if (addr & (SZ_4K - 1))
  1561. return -EINVAL;
  1562. if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
  1563. return -EEXIST;
  1564. if (addr + size < addr)
  1565. return -EINVAL;
  1566. *ioaddr = addr;
  1567. ret = vgic_ioaddr_overlap(kvm);
  1568. if (ret)
  1569. *ioaddr = VGIC_ADDR_UNDEF;
  1570. return ret;
  1571. }
  1572. /**
  1573. * kvm_vgic_addr - set or get vgic VM base addresses
  1574. * @kvm: pointer to the vm struct
  1575. * @type: the VGIC addr type, one of KVM_VGIC_V[23]_ADDR_TYPE_XXX
  1576. * @addr: pointer to address value
  1577. * @write: if true set the address in the VM address space, if false read the
  1578. * address
  1579. *
  1580. * Set or get the vgic base addresses for the distributor and the virtual CPU
  1581. * interface in the VM physical address space. These addresses are properties
  1582. * of the emulated core/SoC and therefore user space initially knows this
  1583. * information.
  1584. */
  1585. int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
  1586. {
  1587. int r = 0;
  1588. struct vgic_dist *vgic = &kvm->arch.vgic;
  1589. int type_needed;
  1590. phys_addr_t *addr_ptr, block_size;
  1591. phys_addr_t alignment;
  1592. mutex_lock(&kvm->lock);
  1593. switch (type) {
  1594. case KVM_VGIC_V2_ADDR_TYPE_DIST:
  1595. type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
  1596. addr_ptr = &vgic->vgic_dist_base;
  1597. block_size = KVM_VGIC_V2_DIST_SIZE;
  1598. alignment = SZ_4K;
  1599. break;
  1600. case KVM_VGIC_V2_ADDR_TYPE_CPU:
  1601. type_needed = KVM_DEV_TYPE_ARM_VGIC_V2;
  1602. addr_ptr = &vgic->vgic_cpu_base;
  1603. block_size = KVM_VGIC_V2_CPU_SIZE;
  1604. alignment = SZ_4K;
  1605. break;
  1606. #ifdef CONFIG_ARM_GIC_V3
  1607. case KVM_VGIC_V3_ADDR_TYPE_DIST:
  1608. type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
  1609. addr_ptr = &vgic->vgic_dist_base;
  1610. block_size = KVM_VGIC_V3_DIST_SIZE;
  1611. alignment = SZ_64K;
  1612. break;
  1613. case KVM_VGIC_V3_ADDR_TYPE_REDIST:
  1614. type_needed = KVM_DEV_TYPE_ARM_VGIC_V3;
  1615. addr_ptr = &vgic->vgic_redist_base;
  1616. block_size = KVM_VGIC_V3_REDIST_SIZE;
  1617. alignment = SZ_64K;
  1618. break;
  1619. #endif
  1620. default:
  1621. r = -ENODEV;
  1622. goto out;
  1623. }
  1624. if (vgic->vgic_model != type_needed) {
  1625. r = -ENODEV;
  1626. goto out;
  1627. }
  1628. if (write) {
  1629. if (!IS_ALIGNED(*addr, alignment))
  1630. r = -EINVAL;
  1631. else
  1632. r = vgic_ioaddr_assign(kvm, addr_ptr, *addr,
  1633. block_size);
  1634. } else {
  1635. *addr = *addr_ptr;
  1636. }
  1637. out:
  1638. mutex_unlock(&kvm->lock);
  1639. return r;
  1640. }
  1641. int vgic_set_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  1642. {
  1643. int r;
  1644. switch (attr->group) {
  1645. case KVM_DEV_ARM_VGIC_GRP_ADDR: {
  1646. u64 __user *uaddr = (u64 __user *)(long)attr->addr;
  1647. u64 addr;
  1648. unsigned long type = (unsigned long)attr->attr;
  1649. if (copy_from_user(&addr, uaddr, sizeof(addr)))
  1650. return -EFAULT;
  1651. r = kvm_vgic_addr(dev->kvm, type, &addr, true);
  1652. return (r == -ENODEV) ? -ENXIO : r;
  1653. }
  1654. case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
  1655. u32 __user *uaddr = (u32 __user *)(long)attr->addr;
  1656. u32 val;
  1657. int ret = 0;
  1658. if (get_user(val, uaddr))
  1659. return -EFAULT;
  1660. /*
  1661. * We require:
  1662. * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
  1663. * - at most 1024 interrupts
  1664. * - a multiple of 32 interrupts
  1665. */
  1666. if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
  1667. val > VGIC_MAX_IRQS ||
  1668. (val & 31))
  1669. return -EINVAL;
  1670. mutex_lock(&dev->kvm->lock);
  1671. if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs)
  1672. ret = -EBUSY;
  1673. else
  1674. dev->kvm->arch.vgic.nr_irqs = val;
  1675. mutex_unlock(&dev->kvm->lock);
  1676. return ret;
  1677. }
  1678. case KVM_DEV_ARM_VGIC_GRP_CTRL: {
  1679. switch (attr->attr) {
  1680. case KVM_DEV_ARM_VGIC_CTRL_INIT:
  1681. r = vgic_init(dev->kvm);
  1682. return r;
  1683. }
  1684. break;
  1685. }
  1686. }
  1687. return -ENXIO;
  1688. }
  1689. int vgic_get_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  1690. {
  1691. int r = -ENXIO;
  1692. switch (attr->group) {
  1693. case KVM_DEV_ARM_VGIC_GRP_ADDR: {
  1694. u64 __user *uaddr = (u64 __user *)(long)attr->addr;
  1695. u64 addr;
  1696. unsigned long type = (unsigned long)attr->attr;
  1697. r = kvm_vgic_addr(dev->kvm, type, &addr, false);
  1698. if (r)
  1699. return (r == -ENODEV) ? -ENXIO : r;
  1700. if (copy_to_user(uaddr, &addr, sizeof(addr)))
  1701. return -EFAULT;
  1702. break;
  1703. }
  1704. case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
  1705. u32 __user *uaddr = (u32 __user *)(long)attr->addr;
  1706. r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr);
  1707. break;
  1708. }
  1709. }
  1710. return r;
  1711. }
  1712. int vgic_has_attr_regs(const struct vgic_io_range *ranges, phys_addr_t offset)
  1713. {
  1714. if (vgic_find_range(ranges, 4, offset))
  1715. return 0;
  1716. else
  1717. return -ENXIO;
  1718. }
  1719. static void vgic_init_maintenance_interrupt(void *info)
  1720. {
  1721. enable_percpu_irq(vgic->maint_irq, 0);
  1722. }
  1723. static int vgic_cpu_notify(struct notifier_block *self,
  1724. unsigned long action, void *cpu)
  1725. {
  1726. switch (action) {
  1727. case CPU_STARTING:
  1728. case CPU_STARTING_FROZEN:
  1729. vgic_init_maintenance_interrupt(NULL);
  1730. break;
  1731. case CPU_DYING:
  1732. case CPU_DYING_FROZEN:
  1733. disable_percpu_irq(vgic->maint_irq);
  1734. break;
  1735. }
  1736. return NOTIFY_OK;
  1737. }
  1738. static struct notifier_block vgic_cpu_nb = {
  1739. .notifier_call = vgic_cpu_notify,
  1740. };
  1741. static const struct of_device_id vgic_ids[] = {
  1742. { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
  1743. { .compatible = "arm,cortex-a7-gic", .data = vgic_v2_probe, },
  1744. { .compatible = "arm,gic-400", .data = vgic_v2_probe, },
  1745. { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
  1746. {},
  1747. };
  1748. int kvm_vgic_hyp_init(void)
  1749. {
  1750. const struct of_device_id *matched_id;
  1751. const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
  1752. const struct vgic_params **);
  1753. struct device_node *vgic_node;
  1754. int ret;
  1755. vgic_node = of_find_matching_node_and_match(NULL,
  1756. vgic_ids, &matched_id);
  1757. if (!vgic_node) {
  1758. kvm_err("error: no compatible GIC node found\n");
  1759. return -ENODEV;
  1760. }
  1761. vgic_probe = matched_id->data;
  1762. ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
  1763. if (ret)
  1764. return ret;
  1765. ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
  1766. "vgic", kvm_get_running_vcpus());
  1767. if (ret) {
  1768. kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
  1769. return ret;
  1770. }
  1771. ret = __register_cpu_notifier(&vgic_cpu_nb);
  1772. if (ret) {
  1773. kvm_err("Cannot register vgic CPU notifier\n");
  1774. goto out_free_irq;
  1775. }
  1776. /* Callback into for arch code for setup */
  1777. vgic_arch_setup(vgic);
  1778. on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
  1779. return 0;
  1780. out_free_irq:
  1781. free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
  1782. return ret;
  1783. }
  1784. int kvm_irq_map_gsi(struct kvm *kvm,
  1785. struct kvm_kernel_irq_routing_entry *entries,
  1786. int gsi)
  1787. {
  1788. return 0;
  1789. }
  1790. int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
  1791. {
  1792. return pin;
  1793. }
  1794. int kvm_set_irq(struct kvm *kvm, int irq_source_id,
  1795. u32 irq, int level, bool line_status)
  1796. {
  1797. unsigned int spi = irq + VGIC_NR_PRIVATE_IRQS;
  1798. trace_kvm_set_irq(irq, level, irq_source_id);
  1799. BUG_ON(!vgic_initialized(kvm));
  1800. if (spi > kvm->arch.vgic.nr_irqs)
  1801. return -EINVAL;
  1802. return kvm_vgic_inject_irq(kvm, 0, spi, level);
  1803. }
  1804. /* MSI not implemented yet */
  1805. int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
  1806. struct kvm *kvm, int irq_source_id,
  1807. int level, bool line_status)
  1808. {
  1809. return 0;
  1810. }