vgic.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479
  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. /*
  32. * How the whole thing works (courtesy of Christoffer Dall):
  33. *
  34. * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if
  35. * something is pending on the CPU interface.
  36. * - Interrupts that are pending on the distributor are stored on the
  37. * vgic.irq_pending vgic bitmap (this bitmap is updated by both user land
  38. * ioctls and guest mmio ops, and other in-kernel peripherals such as the
  39. * arch. timers).
  40. * - Every time the bitmap changes, the irq_pending_on_cpu oracle is
  41. * recalculated
  42. * - To calculate the oracle, we need info for each cpu from
  43. * compute_pending_for_cpu, which considers:
  44. * - PPI: dist->irq_pending & dist->irq_enable
  45. * - SPI: dist->irq_pending & dist->irq_enable & dist->irq_spi_target
  46. * - irq_spi_target is a 'formatted' version of the GICD_ITARGETSRn
  47. * registers, stored on each vcpu. We only keep one bit of
  48. * information per interrupt, making sure that only one vcpu can
  49. * accept the interrupt.
  50. * - If any of the above state changes, we must recalculate the oracle.
  51. * - The same is true when injecting an interrupt, except that we only
  52. * consider a single interrupt at a time. The irq_spi_cpu array
  53. * contains the target CPU for each SPI.
  54. *
  55. * The handling of level interrupts adds some extra complexity. We
  56. * need to track when the interrupt has been EOIed, so we can sample
  57. * the 'line' again. This is achieved as such:
  58. *
  59. * - When a level interrupt is moved onto a vcpu, the corresponding
  60. * bit in irq_queued is set. As long as this bit is set, the line
  61. * will be ignored for further interrupts. The interrupt is injected
  62. * into the vcpu with the GICH_LR_EOI bit set (generate a
  63. * maintenance interrupt on EOI).
  64. * - When the interrupt is EOIed, the maintenance interrupt fires,
  65. * and clears the corresponding bit in irq_queued. This allows the
  66. * interrupt line to be sampled again.
  67. * - Note that level-triggered interrupts can also be set to pending from
  68. * writes to GICD_ISPENDRn and lowering the external input line does not
  69. * cause the interrupt to become inactive in such a situation.
  70. * Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become
  71. * inactive as long as the external input line is held high.
  72. */
  73. #define VGIC_ADDR_UNDEF (-1)
  74. #define IS_VGIC_ADDR_UNDEF(_x) ((_x) == VGIC_ADDR_UNDEF)
  75. #define PRODUCT_ID_KVM 0x4b /* ASCII code K */
  76. #define IMPLEMENTER_ARM 0x43b
  77. #define GICC_ARCH_VERSION_V2 0x2
  78. #define ACCESS_READ_VALUE (1 << 0)
  79. #define ACCESS_READ_RAZ (0 << 0)
  80. #define ACCESS_READ_MASK(x) ((x) & (1 << 0))
  81. #define ACCESS_WRITE_IGNORED (0 << 1)
  82. #define ACCESS_WRITE_SETBIT (1 << 1)
  83. #define ACCESS_WRITE_CLEARBIT (2 << 1)
  84. #define ACCESS_WRITE_VALUE (3 << 1)
  85. #define ACCESS_WRITE_MASK(x) ((x) & (3 << 1))
  86. static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
  87. static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu);
  88. static void vgic_update_state(struct kvm *kvm);
  89. static void vgic_kick_vcpus(struct kvm *kvm);
  90. static u8 *vgic_get_sgi_sources(struct vgic_dist *dist, int vcpu_id, int sgi);
  91. static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg);
  92. static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr);
  93. static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc);
  94. static void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
  95. static void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
  96. static const struct vgic_ops *vgic_ops;
  97. static const struct vgic_params *vgic;
  98. /*
  99. * struct vgic_bitmap contains a bitmap made of unsigned longs, but
  100. * extracts u32s out of them.
  101. *
  102. * This does not work on 64-bit BE systems, because the bitmap access
  103. * will store two consecutive 32-bit words with the higher-addressed
  104. * register's bits at the lower index and the lower-addressed register's
  105. * bits at the higher index.
  106. *
  107. * Therefore, swizzle the register index when accessing the 32-bit word
  108. * registers to access the right register's value.
  109. */
  110. #if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64
  111. #define REG_OFFSET_SWIZZLE 1
  112. #else
  113. #define REG_OFFSET_SWIZZLE 0
  114. #endif
  115. static int vgic_init_bitmap(struct vgic_bitmap *b, int nr_cpus, int nr_irqs)
  116. {
  117. int nr_longs;
  118. nr_longs = nr_cpus + BITS_TO_LONGS(nr_irqs - VGIC_NR_PRIVATE_IRQS);
  119. b->private = kzalloc(sizeof(unsigned long) * nr_longs, GFP_KERNEL);
  120. if (!b->private)
  121. return -ENOMEM;
  122. b->shared = b->private + nr_cpus;
  123. return 0;
  124. }
  125. static void vgic_free_bitmap(struct vgic_bitmap *b)
  126. {
  127. kfree(b->private);
  128. b->private = NULL;
  129. b->shared = NULL;
  130. }
  131. /*
  132. * Call this function to convert a u64 value to an unsigned long * bitmask
  133. * in a way that works on both 32-bit and 64-bit LE and BE platforms.
  134. *
  135. * Warning: Calling this function may modify *val.
  136. */
  137. static unsigned long *u64_to_bitmask(u64 *val)
  138. {
  139. #if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32
  140. *val = (*val >> 32) | (*val << 32);
  141. #endif
  142. return (unsigned long *)val;
  143. }
  144. static u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x,
  145. int cpuid, u32 offset)
  146. {
  147. offset >>= 2;
  148. if (!offset)
  149. return (u32 *)(x->private + cpuid) + REG_OFFSET_SWIZZLE;
  150. else
  151. return (u32 *)(x->shared) + ((offset - 1) ^ REG_OFFSET_SWIZZLE);
  152. }
  153. static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x,
  154. int cpuid, int irq)
  155. {
  156. if (irq < VGIC_NR_PRIVATE_IRQS)
  157. return test_bit(irq, x->private + cpuid);
  158. return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared);
  159. }
  160. static void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid,
  161. int irq, int val)
  162. {
  163. unsigned long *reg;
  164. if (irq < VGIC_NR_PRIVATE_IRQS) {
  165. reg = x->private + cpuid;
  166. } else {
  167. reg = x->shared;
  168. irq -= VGIC_NR_PRIVATE_IRQS;
  169. }
  170. if (val)
  171. set_bit(irq, reg);
  172. else
  173. clear_bit(irq, reg);
  174. }
  175. static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid)
  176. {
  177. return x->private + cpuid;
  178. }
  179. static unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x)
  180. {
  181. return x->shared;
  182. }
  183. static int vgic_init_bytemap(struct vgic_bytemap *x, int nr_cpus, int nr_irqs)
  184. {
  185. int size;
  186. size = nr_cpus * VGIC_NR_PRIVATE_IRQS;
  187. size += nr_irqs - VGIC_NR_PRIVATE_IRQS;
  188. x->private = kzalloc(size, GFP_KERNEL);
  189. if (!x->private)
  190. return -ENOMEM;
  191. x->shared = x->private + nr_cpus * VGIC_NR_PRIVATE_IRQS / sizeof(u32);
  192. return 0;
  193. }
  194. static void vgic_free_bytemap(struct vgic_bytemap *b)
  195. {
  196. kfree(b->private);
  197. b->private = NULL;
  198. b->shared = NULL;
  199. }
  200. static u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset)
  201. {
  202. u32 *reg;
  203. if (offset < VGIC_NR_PRIVATE_IRQS) {
  204. reg = x->private;
  205. offset += cpuid * VGIC_NR_PRIVATE_IRQS;
  206. } else {
  207. reg = x->shared;
  208. offset -= VGIC_NR_PRIVATE_IRQS;
  209. }
  210. return reg + (offset / sizeof(u32));
  211. }
  212. #define VGIC_CFG_LEVEL 0
  213. #define VGIC_CFG_EDGE 1
  214. static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq)
  215. {
  216. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  217. int irq_val;
  218. irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq);
  219. return irq_val == VGIC_CFG_EDGE;
  220. }
  221. static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq)
  222. {
  223. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  224. return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq);
  225. }
  226. static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq)
  227. {
  228. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  229. return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq);
  230. }
  231. static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq)
  232. {
  233. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  234. vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1);
  235. }
  236. static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq)
  237. {
  238. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  239. vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0);
  240. }
  241. static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq)
  242. {
  243. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  244. return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq);
  245. }
  246. static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq)
  247. {
  248. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  249. vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1);
  250. }
  251. static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq)
  252. {
  253. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  254. vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0);
  255. }
  256. static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq)
  257. {
  258. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  259. return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq);
  260. }
  261. static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq)
  262. {
  263. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  264. vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0);
  265. }
  266. static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
  267. {
  268. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  269. return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq);
  270. }
  271. static void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq)
  272. {
  273. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  274. vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1);
  275. }
  276. static void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq)
  277. {
  278. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  279. vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0);
  280. }
  281. static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq)
  282. {
  283. if (irq < VGIC_NR_PRIVATE_IRQS)
  284. set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
  285. else
  286. set_bit(irq - VGIC_NR_PRIVATE_IRQS,
  287. vcpu->arch.vgic_cpu.pending_shared);
  288. }
  289. static void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq)
  290. {
  291. if (irq < VGIC_NR_PRIVATE_IRQS)
  292. clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu);
  293. else
  294. clear_bit(irq - VGIC_NR_PRIVATE_IRQS,
  295. vcpu->arch.vgic_cpu.pending_shared);
  296. }
  297. static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq)
  298. {
  299. return vgic_irq_is_edge(vcpu, irq) || !vgic_irq_is_queued(vcpu, irq);
  300. }
  301. static u32 mmio_data_read(struct kvm_exit_mmio *mmio, u32 mask)
  302. {
  303. return le32_to_cpu(*((u32 *)mmio->data)) & mask;
  304. }
  305. static void mmio_data_write(struct kvm_exit_mmio *mmio, u32 mask, u32 value)
  306. {
  307. *((u32 *)mmio->data) = cpu_to_le32(value) & mask;
  308. }
  309. /**
  310. * vgic_reg_access - access vgic register
  311. * @mmio: pointer to the data describing the mmio access
  312. * @reg: pointer to the virtual backing of vgic distributor data
  313. * @offset: least significant 2 bits used for word offset
  314. * @mode: ACCESS_ mode (see defines above)
  315. *
  316. * Helper to make vgic register access easier using one of the access
  317. * modes defined for vgic register access
  318. * (read,raz,write-ignored,setbit,clearbit,write)
  319. */
  320. static void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg,
  321. phys_addr_t offset, int mode)
  322. {
  323. int word_offset = (offset & 3) * 8;
  324. u32 mask = (1UL << (mmio->len * 8)) - 1;
  325. u32 regval;
  326. /*
  327. * Any alignment fault should have been delivered to the guest
  328. * directly (ARM ARM B3.12.7 "Prioritization of aborts").
  329. */
  330. if (reg) {
  331. regval = *reg;
  332. } else {
  333. BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED));
  334. regval = 0;
  335. }
  336. if (mmio->is_write) {
  337. u32 data = mmio_data_read(mmio, mask) << word_offset;
  338. switch (ACCESS_WRITE_MASK(mode)) {
  339. case ACCESS_WRITE_IGNORED:
  340. return;
  341. case ACCESS_WRITE_SETBIT:
  342. regval |= data;
  343. break;
  344. case ACCESS_WRITE_CLEARBIT:
  345. regval &= ~data;
  346. break;
  347. case ACCESS_WRITE_VALUE:
  348. regval = (regval & ~(mask << word_offset)) | data;
  349. break;
  350. }
  351. *reg = regval;
  352. } else {
  353. switch (ACCESS_READ_MASK(mode)) {
  354. case ACCESS_READ_RAZ:
  355. regval = 0;
  356. /* fall through */
  357. case ACCESS_READ_VALUE:
  358. mmio_data_write(mmio, mask, regval >> word_offset);
  359. }
  360. }
  361. }
  362. static bool handle_mmio_misc(struct kvm_vcpu *vcpu,
  363. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  364. {
  365. u32 reg;
  366. u32 word_offset = offset & 3;
  367. switch (offset & ~3) {
  368. case 0: /* GICD_CTLR */
  369. reg = vcpu->kvm->arch.vgic.enabled;
  370. vgic_reg_access(mmio, &reg, word_offset,
  371. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  372. if (mmio->is_write) {
  373. vcpu->kvm->arch.vgic.enabled = reg & 1;
  374. vgic_update_state(vcpu->kvm);
  375. return true;
  376. }
  377. break;
  378. case 4: /* GICD_TYPER */
  379. reg = (atomic_read(&vcpu->kvm->online_vcpus) - 1) << 5;
  380. reg |= (vcpu->kvm->arch.vgic.nr_irqs >> 5) - 1;
  381. vgic_reg_access(mmio, &reg, word_offset,
  382. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  383. break;
  384. case 8: /* GICD_IIDR */
  385. reg = (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
  386. vgic_reg_access(mmio, &reg, word_offset,
  387. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  388. break;
  389. }
  390. return false;
  391. }
  392. static bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu,
  393. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  394. {
  395. vgic_reg_access(mmio, NULL, offset,
  396. ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
  397. return false;
  398. }
  399. static bool handle_mmio_set_enable_reg(struct kvm_vcpu *vcpu,
  400. struct kvm_exit_mmio *mmio,
  401. phys_addr_t offset)
  402. {
  403. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
  404. vcpu->vcpu_id, offset);
  405. vgic_reg_access(mmio, reg, offset,
  406. ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
  407. if (mmio->is_write) {
  408. vgic_update_state(vcpu->kvm);
  409. return true;
  410. }
  411. return false;
  412. }
  413. static bool handle_mmio_clear_enable_reg(struct kvm_vcpu *vcpu,
  414. struct kvm_exit_mmio *mmio,
  415. phys_addr_t offset)
  416. {
  417. u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_enabled,
  418. vcpu->vcpu_id, offset);
  419. vgic_reg_access(mmio, reg, offset,
  420. ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
  421. if (mmio->is_write) {
  422. if (offset < 4) /* Force SGI enabled */
  423. *reg |= 0xffff;
  424. vgic_retire_disabled_irqs(vcpu);
  425. vgic_update_state(vcpu->kvm);
  426. return true;
  427. }
  428. return false;
  429. }
  430. static bool handle_mmio_set_pending_reg(struct kvm_vcpu *vcpu,
  431. struct kvm_exit_mmio *mmio,
  432. phys_addr_t offset)
  433. {
  434. u32 *reg, orig;
  435. u32 level_mask;
  436. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  437. reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu->vcpu_id, offset);
  438. level_mask = (~(*reg));
  439. /* Mark both level and edge triggered irqs as pending */
  440. reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu->vcpu_id, offset);
  441. orig = *reg;
  442. vgic_reg_access(mmio, reg, offset,
  443. ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
  444. if (mmio->is_write) {
  445. /* Set the soft-pending flag only for level-triggered irqs */
  446. reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
  447. vcpu->vcpu_id, offset);
  448. vgic_reg_access(mmio, reg, offset,
  449. ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
  450. *reg &= level_mask;
  451. /* Ignore writes to SGIs */
  452. if (offset < 2) {
  453. *reg &= ~0xffff;
  454. *reg |= orig & 0xffff;
  455. }
  456. vgic_update_state(vcpu->kvm);
  457. return true;
  458. }
  459. return false;
  460. }
  461. static bool handle_mmio_clear_pending_reg(struct kvm_vcpu *vcpu,
  462. struct kvm_exit_mmio *mmio,
  463. phys_addr_t offset)
  464. {
  465. u32 *level_active;
  466. u32 *reg, orig;
  467. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  468. reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu->vcpu_id, offset);
  469. orig = *reg;
  470. vgic_reg_access(mmio, reg, offset,
  471. ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
  472. if (mmio->is_write) {
  473. /* Re-set level triggered level-active interrupts */
  474. level_active = vgic_bitmap_get_reg(&dist->irq_level,
  475. vcpu->vcpu_id, offset);
  476. reg = vgic_bitmap_get_reg(&dist->irq_pending,
  477. vcpu->vcpu_id, offset);
  478. *reg |= *level_active;
  479. /* Ignore writes to SGIs */
  480. if (offset < 2) {
  481. *reg &= ~0xffff;
  482. *reg |= orig & 0xffff;
  483. }
  484. /* Clear soft-pending flags */
  485. reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
  486. vcpu->vcpu_id, offset);
  487. vgic_reg_access(mmio, reg, offset,
  488. ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
  489. vgic_update_state(vcpu->kvm);
  490. return true;
  491. }
  492. return false;
  493. }
  494. static bool handle_mmio_priority_reg(struct kvm_vcpu *vcpu,
  495. struct kvm_exit_mmio *mmio,
  496. phys_addr_t offset)
  497. {
  498. u32 *reg = vgic_bytemap_get_reg(&vcpu->kvm->arch.vgic.irq_priority,
  499. vcpu->vcpu_id, offset);
  500. vgic_reg_access(mmio, reg, offset,
  501. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  502. return false;
  503. }
  504. #define GICD_ITARGETSR_SIZE 32
  505. #define GICD_CPUTARGETS_BITS 8
  506. #define GICD_IRQS_PER_ITARGETSR (GICD_ITARGETSR_SIZE / GICD_CPUTARGETS_BITS)
  507. static u32 vgic_get_target_reg(struct kvm *kvm, int irq)
  508. {
  509. struct vgic_dist *dist = &kvm->arch.vgic;
  510. int i;
  511. u32 val = 0;
  512. irq -= VGIC_NR_PRIVATE_IRQS;
  513. for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++)
  514. val |= 1 << (dist->irq_spi_cpu[irq + i] + i * 8);
  515. return val;
  516. }
  517. static void vgic_set_target_reg(struct kvm *kvm, u32 val, int irq)
  518. {
  519. struct vgic_dist *dist = &kvm->arch.vgic;
  520. struct kvm_vcpu *vcpu;
  521. int i, c;
  522. unsigned long *bmap;
  523. u32 target;
  524. irq -= VGIC_NR_PRIVATE_IRQS;
  525. /*
  526. * Pick the LSB in each byte. This ensures we target exactly
  527. * one vcpu per IRQ. If the byte is null, assume we target
  528. * CPU0.
  529. */
  530. for (i = 0; i < GICD_IRQS_PER_ITARGETSR; i++) {
  531. int shift = i * GICD_CPUTARGETS_BITS;
  532. target = ffs((val >> shift) & 0xffU);
  533. target = target ? (target - 1) : 0;
  534. dist->irq_spi_cpu[irq + i] = target;
  535. kvm_for_each_vcpu(c, vcpu, kvm) {
  536. bmap = vgic_bitmap_get_shared_map(&dist->irq_spi_target[c]);
  537. if (c == target)
  538. set_bit(irq + i, bmap);
  539. else
  540. clear_bit(irq + i, bmap);
  541. }
  542. }
  543. }
  544. static bool handle_mmio_target_reg(struct kvm_vcpu *vcpu,
  545. struct kvm_exit_mmio *mmio,
  546. phys_addr_t offset)
  547. {
  548. u32 reg;
  549. /* We treat the banked interrupts targets as read-only */
  550. if (offset < 32) {
  551. u32 roreg = 1 << vcpu->vcpu_id;
  552. roreg |= roreg << 8;
  553. roreg |= roreg << 16;
  554. vgic_reg_access(mmio, &roreg, offset,
  555. ACCESS_READ_VALUE | ACCESS_WRITE_IGNORED);
  556. return false;
  557. }
  558. reg = vgic_get_target_reg(vcpu->kvm, offset & ~3U);
  559. vgic_reg_access(mmio, &reg, offset,
  560. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  561. if (mmio->is_write) {
  562. vgic_set_target_reg(vcpu->kvm, reg, offset & ~3U);
  563. vgic_update_state(vcpu->kvm);
  564. return true;
  565. }
  566. return false;
  567. }
  568. static u32 vgic_cfg_expand(u16 val)
  569. {
  570. u32 res = 0;
  571. int i;
  572. /*
  573. * Turn a 16bit value like abcd...mnop into a 32bit word
  574. * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is.
  575. */
  576. for (i = 0; i < 16; i++)
  577. res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1);
  578. return res;
  579. }
  580. static u16 vgic_cfg_compress(u32 val)
  581. {
  582. u16 res = 0;
  583. int i;
  584. /*
  585. * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like
  586. * abcd...mnop which is what we really care about.
  587. */
  588. for (i = 0; i < 16; i++)
  589. res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i;
  590. return res;
  591. }
  592. /*
  593. * The distributor uses 2 bits per IRQ for the CFG register, but the
  594. * LSB is always 0. As such, we only keep the upper bit, and use the
  595. * two above functions to compress/expand the bits
  596. */
  597. static bool handle_mmio_cfg_reg(struct kvm_vcpu *vcpu,
  598. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  599. {
  600. u32 val;
  601. u32 *reg;
  602. reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_cfg,
  603. vcpu->vcpu_id, offset >> 1);
  604. if (offset & 4)
  605. val = *reg >> 16;
  606. else
  607. val = *reg & 0xffff;
  608. val = vgic_cfg_expand(val);
  609. vgic_reg_access(mmio, &val, offset,
  610. ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
  611. if (mmio->is_write) {
  612. if (offset < 8) {
  613. *reg = ~0U; /* Force PPIs/SGIs to 1 */
  614. return false;
  615. }
  616. val = vgic_cfg_compress(val);
  617. if (offset & 4) {
  618. *reg &= 0xffff;
  619. *reg |= val << 16;
  620. } else {
  621. *reg &= 0xffff << 16;
  622. *reg |= val;
  623. }
  624. }
  625. return false;
  626. }
  627. static bool handle_mmio_sgi_reg(struct kvm_vcpu *vcpu,
  628. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  629. {
  630. u32 reg;
  631. vgic_reg_access(mmio, &reg, offset,
  632. ACCESS_READ_RAZ | ACCESS_WRITE_VALUE);
  633. if (mmio->is_write) {
  634. vgic_dispatch_sgi(vcpu, reg);
  635. vgic_update_state(vcpu->kvm);
  636. return true;
  637. }
  638. return false;
  639. }
  640. /**
  641. * vgic_unqueue_irqs - move pending IRQs from LRs to the distributor
  642. * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs
  643. *
  644. * Move any pending IRQs that have already been assigned to LRs back to the
  645. * emulated distributor state so that the complete emulated state can be read
  646. * from the main emulation structures without investigating the LRs.
  647. *
  648. * Note that IRQs in the active state in the LRs get their pending state moved
  649. * to the distributor but the active state stays in the LRs, because we don't
  650. * track the active state on the distributor side.
  651. */
  652. static void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
  653. {
  654. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  655. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  656. int vcpu_id = vcpu->vcpu_id;
  657. int i;
  658. for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) {
  659. struct vgic_lr lr = vgic_get_lr(vcpu, i);
  660. /*
  661. * There are three options for the state bits:
  662. *
  663. * 01: pending
  664. * 10: active
  665. * 11: pending and active
  666. *
  667. * If the LR holds only an active interrupt (not pending) then
  668. * just leave it alone.
  669. */
  670. if ((lr.state & LR_STATE_MASK) == LR_STATE_ACTIVE)
  671. continue;
  672. /*
  673. * Reestablish the pending state on the distributor and the
  674. * CPU interface. It may have already been pending, but that
  675. * is fine, then we are only setting a few bits that were
  676. * already set.
  677. */
  678. vgic_dist_irq_set_pending(vcpu, lr.irq);
  679. if (lr.irq < VGIC_NR_SGIS)
  680. *vgic_get_sgi_sources(dist, vcpu_id, lr.irq) |= 1 << lr.source;
  681. lr.state &= ~LR_STATE_PENDING;
  682. vgic_set_lr(vcpu, i, lr);
  683. /*
  684. * If there's no state left on the LR (it could still be
  685. * active), then the LR does not hold any useful info and can
  686. * be marked as free for other use.
  687. */
  688. if (!(lr.state & LR_STATE_MASK)) {
  689. vgic_retire_lr(i, lr.irq, vcpu);
  690. vgic_irq_clear_queued(vcpu, lr.irq);
  691. }
  692. /* Finally update the VGIC state. */
  693. vgic_update_state(vcpu->kvm);
  694. }
  695. }
  696. /* Handle reads of GICD_CPENDSGIRn and GICD_SPENDSGIRn */
  697. static bool read_set_clear_sgi_pend_reg(struct kvm_vcpu *vcpu,
  698. struct kvm_exit_mmio *mmio,
  699. phys_addr_t offset)
  700. {
  701. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  702. int sgi;
  703. int min_sgi = (offset & ~0x3);
  704. int max_sgi = min_sgi + 3;
  705. int vcpu_id = vcpu->vcpu_id;
  706. u32 reg = 0;
  707. /* Copy source SGIs from distributor side */
  708. for (sgi = min_sgi; sgi <= max_sgi; sgi++) {
  709. int shift = 8 * (sgi - min_sgi);
  710. reg |= ((u32)*vgic_get_sgi_sources(dist, vcpu_id, sgi)) << shift;
  711. }
  712. mmio_data_write(mmio, ~0, reg);
  713. return false;
  714. }
  715. static bool write_set_clear_sgi_pend_reg(struct kvm_vcpu *vcpu,
  716. struct kvm_exit_mmio *mmio,
  717. phys_addr_t offset, bool set)
  718. {
  719. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  720. int sgi;
  721. int min_sgi = (offset & ~0x3);
  722. int max_sgi = min_sgi + 3;
  723. int vcpu_id = vcpu->vcpu_id;
  724. u32 reg;
  725. bool updated = false;
  726. reg = mmio_data_read(mmio, ~0);
  727. /* Clear pending SGIs on the distributor */
  728. for (sgi = min_sgi; sgi <= max_sgi; sgi++) {
  729. u8 mask = reg >> (8 * (sgi - min_sgi));
  730. u8 *src = vgic_get_sgi_sources(dist, vcpu_id, sgi);
  731. if (set) {
  732. if ((*src & mask) != mask)
  733. updated = true;
  734. *src |= mask;
  735. } else {
  736. if (*src & mask)
  737. updated = true;
  738. *src &= ~mask;
  739. }
  740. }
  741. if (updated)
  742. vgic_update_state(vcpu->kvm);
  743. return updated;
  744. }
  745. static bool handle_mmio_sgi_set(struct kvm_vcpu *vcpu,
  746. struct kvm_exit_mmio *mmio,
  747. phys_addr_t offset)
  748. {
  749. if (!mmio->is_write)
  750. return read_set_clear_sgi_pend_reg(vcpu, mmio, offset);
  751. else
  752. return write_set_clear_sgi_pend_reg(vcpu, mmio, offset, true);
  753. }
  754. static bool handle_mmio_sgi_clear(struct kvm_vcpu *vcpu,
  755. struct kvm_exit_mmio *mmio,
  756. phys_addr_t offset)
  757. {
  758. if (!mmio->is_write)
  759. return read_set_clear_sgi_pend_reg(vcpu, mmio, offset);
  760. else
  761. return write_set_clear_sgi_pend_reg(vcpu, mmio, offset, false);
  762. }
  763. /*
  764. * I would have liked to use the kvm_bus_io_*() API instead, but it
  765. * cannot cope with banked registers (only the VM pointer is passed
  766. * around, and we need the vcpu). One of these days, someone please
  767. * fix it!
  768. */
  769. struct mmio_range {
  770. phys_addr_t base;
  771. unsigned long len;
  772. int bits_per_irq;
  773. bool (*handle_mmio)(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio,
  774. phys_addr_t offset);
  775. };
  776. static const struct mmio_range vgic_dist_ranges[] = {
  777. {
  778. .base = GIC_DIST_CTRL,
  779. .len = 12,
  780. .bits_per_irq = 0,
  781. .handle_mmio = handle_mmio_misc,
  782. },
  783. {
  784. .base = GIC_DIST_IGROUP,
  785. .len = VGIC_MAX_IRQS / 8,
  786. .bits_per_irq = 1,
  787. .handle_mmio = handle_mmio_raz_wi,
  788. },
  789. {
  790. .base = GIC_DIST_ENABLE_SET,
  791. .len = VGIC_MAX_IRQS / 8,
  792. .bits_per_irq = 1,
  793. .handle_mmio = handle_mmio_set_enable_reg,
  794. },
  795. {
  796. .base = GIC_DIST_ENABLE_CLEAR,
  797. .len = VGIC_MAX_IRQS / 8,
  798. .bits_per_irq = 1,
  799. .handle_mmio = handle_mmio_clear_enable_reg,
  800. },
  801. {
  802. .base = GIC_DIST_PENDING_SET,
  803. .len = VGIC_MAX_IRQS / 8,
  804. .bits_per_irq = 1,
  805. .handle_mmio = handle_mmio_set_pending_reg,
  806. },
  807. {
  808. .base = GIC_DIST_PENDING_CLEAR,
  809. .len = VGIC_MAX_IRQS / 8,
  810. .bits_per_irq = 1,
  811. .handle_mmio = handle_mmio_clear_pending_reg,
  812. },
  813. {
  814. .base = GIC_DIST_ACTIVE_SET,
  815. .len = VGIC_MAX_IRQS / 8,
  816. .bits_per_irq = 1,
  817. .handle_mmio = handle_mmio_raz_wi,
  818. },
  819. {
  820. .base = GIC_DIST_ACTIVE_CLEAR,
  821. .len = VGIC_MAX_IRQS / 8,
  822. .bits_per_irq = 1,
  823. .handle_mmio = handle_mmio_raz_wi,
  824. },
  825. {
  826. .base = GIC_DIST_PRI,
  827. .len = VGIC_MAX_IRQS,
  828. .bits_per_irq = 8,
  829. .handle_mmio = handle_mmio_priority_reg,
  830. },
  831. {
  832. .base = GIC_DIST_TARGET,
  833. .len = VGIC_MAX_IRQS,
  834. .bits_per_irq = 8,
  835. .handle_mmio = handle_mmio_target_reg,
  836. },
  837. {
  838. .base = GIC_DIST_CONFIG,
  839. .len = VGIC_MAX_IRQS / 4,
  840. .bits_per_irq = 2,
  841. .handle_mmio = handle_mmio_cfg_reg,
  842. },
  843. {
  844. .base = GIC_DIST_SOFTINT,
  845. .len = 4,
  846. .handle_mmio = handle_mmio_sgi_reg,
  847. },
  848. {
  849. .base = GIC_DIST_SGI_PENDING_CLEAR,
  850. .len = VGIC_NR_SGIS,
  851. .handle_mmio = handle_mmio_sgi_clear,
  852. },
  853. {
  854. .base = GIC_DIST_SGI_PENDING_SET,
  855. .len = VGIC_NR_SGIS,
  856. .handle_mmio = handle_mmio_sgi_set,
  857. },
  858. {}
  859. };
  860. static const
  861. struct mmio_range *find_matching_range(const struct mmio_range *ranges,
  862. struct kvm_exit_mmio *mmio,
  863. phys_addr_t offset)
  864. {
  865. const struct mmio_range *r = ranges;
  866. while (r->len) {
  867. if (offset >= r->base &&
  868. (offset + mmio->len) <= (r->base + r->len))
  869. return r;
  870. r++;
  871. }
  872. return NULL;
  873. }
  874. static bool vgic_validate_access(const struct vgic_dist *dist,
  875. const struct mmio_range *range,
  876. unsigned long offset)
  877. {
  878. int irq;
  879. if (!range->bits_per_irq)
  880. return true; /* Not an irq-based access */
  881. irq = offset * 8 / range->bits_per_irq;
  882. if (irq >= dist->nr_irqs)
  883. return false;
  884. return true;
  885. }
  886. /**
  887. * vgic_handle_mmio - handle an in-kernel MMIO access
  888. * @vcpu: pointer to the vcpu performing the access
  889. * @run: pointer to the kvm_run structure
  890. * @mmio: pointer to the data describing the access
  891. *
  892. * returns true if the MMIO access has been performed in kernel space,
  893. * and false if it needs to be emulated in user space.
  894. */
  895. bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  896. struct kvm_exit_mmio *mmio)
  897. {
  898. const struct mmio_range *range;
  899. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  900. unsigned long base = dist->vgic_dist_base;
  901. bool updated_state;
  902. unsigned long offset;
  903. if (!irqchip_in_kernel(vcpu->kvm) ||
  904. mmio->phys_addr < base ||
  905. (mmio->phys_addr + mmio->len) > (base + KVM_VGIC_V2_DIST_SIZE))
  906. return false;
  907. /* We don't support ldrd / strd or ldm / stm to the emulated vgic */
  908. if (mmio->len > 4) {
  909. kvm_inject_dabt(vcpu, mmio->phys_addr);
  910. return true;
  911. }
  912. offset = mmio->phys_addr - base;
  913. range = find_matching_range(vgic_dist_ranges, mmio, offset);
  914. if (unlikely(!range || !range->handle_mmio)) {
  915. pr_warn("Unhandled access %d %08llx %d\n",
  916. mmio->is_write, mmio->phys_addr, mmio->len);
  917. return false;
  918. }
  919. spin_lock(&vcpu->kvm->arch.vgic.lock);
  920. offset = mmio->phys_addr - range->base - base;
  921. if (vgic_validate_access(dist, range, offset)) {
  922. updated_state = range->handle_mmio(vcpu, mmio, offset);
  923. } else {
  924. vgic_reg_access(mmio, NULL, offset,
  925. ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED);
  926. updated_state = false;
  927. }
  928. spin_unlock(&vcpu->kvm->arch.vgic.lock);
  929. kvm_prepare_mmio(run, mmio);
  930. kvm_handle_mmio_return(vcpu, run);
  931. if (updated_state)
  932. vgic_kick_vcpus(vcpu->kvm);
  933. return true;
  934. }
  935. static u8 *vgic_get_sgi_sources(struct vgic_dist *dist, int vcpu_id, int sgi)
  936. {
  937. return dist->irq_sgi_sources + vcpu_id * VGIC_NR_SGIS + sgi;
  938. }
  939. static void vgic_dispatch_sgi(struct kvm_vcpu *vcpu, u32 reg)
  940. {
  941. struct kvm *kvm = vcpu->kvm;
  942. struct vgic_dist *dist = &kvm->arch.vgic;
  943. int nrcpus = atomic_read(&kvm->online_vcpus);
  944. u8 target_cpus;
  945. int sgi, mode, c, vcpu_id;
  946. vcpu_id = vcpu->vcpu_id;
  947. sgi = reg & 0xf;
  948. target_cpus = (reg >> 16) & 0xff;
  949. mode = (reg >> 24) & 3;
  950. switch (mode) {
  951. case 0:
  952. if (!target_cpus)
  953. return;
  954. break;
  955. case 1:
  956. target_cpus = ((1 << nrcpus) - 1) & ~(1 << vcpu_id) & 0xff;
  957. break;
  958. case 2:
  959. target_cpus = 1 << vcpu_id;
  960. break;
  961. }
  962. kvm_for_each_vcpu(c, vcpu, kvm) {
  963. if (target_cpus & 1) {
  964. /* Flag the SGI as pending */
  965. vgic_dist_irq_set_pending(vcpu, sgi);
  966. *vgic_get_sgi_sources(dist, c, sgi) |= 1 << vcpu_id;
  967. kvm_debug("SGI%d from CPU%d to CPU%d\n", sgi, vcpu_id, c);
  968. }
  969. target_cpus >>= 1;
  970. }
  971. }
  972. static int vgic_nr_shared_irqs(struct vgic_dist *dist)
  973. {
  974. return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS;
  975. }
  976. static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
  977. {
  978. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  979. unsigned long *pending, *enabled, *pend_percpu, *pend_shared;
  980. unsigned long pending_private, pending_shared;
  981. int nr_shared = vgic_nr_shared_irqs(dist);
  982. int vcpu_id;
  983. vcpu_id = vcpu->vcpu_id;
  984. pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
  985. pend_shared = vcpu->arch.vgic_cpu.pending_shared;
  986. pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
  987. enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
  988. bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
  989. pending = vgic_bitmap_get_shared_map(&dist->irq_pending);
  990. enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled);
  991. bitmap_and(pend_shared, pending, enabled, nr_shared);
  992. bitmap_and(pend_shared, pend_shared,
  993. vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]),
  994. nr_shared);
  995. pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS);
  996. pending_shared = find_first_bit(pend_shared, nr_shared);
  997. return (pending_private < VGIC_NR_PRIVATE_IRQS ||
  998. pending_shared < vgic_nr_shared_irqs(dist));
  999. }
  1000. /*
  1001. * Update the interrupt state and determine which CPUs have pending
  1002. * interrupts. Must be called with distributor lock held.
  1003. */
  1004. static void vgic_update_state(struct kvm *kvm)
  1005. {
  1006. struct vgic_dist *dist = &kvm->arch.vgic;
  1007. struct kvm_vcpu *vcpu;
  1008. int c;
  1009. if (!dist->enabled) {
  1010. set_bit(0, dist->irq_pending_on_cpu);
  1011. return;
  1012. }
  1013. kvm_for_each_vcpu(c, vcpu, kvm) {
  1014. if (compute_pending_for_cpu(vcpu)) {
  1015. pr_debug("CPU%d has pending interrupts\n", c);
  1016. set_bit(c, dist->irq_pending_on_cpu);
  1017. }
  1018. }
  1019. }
  1020. static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr)
  1021. {
  1022. return vgic_ops->get_lr(vcpu, lr);
  1023. }
  1024. static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
  1025. struct vgic_lr vlr)
  1026. {
  1027. vgic_ops->set_lr(vcpu, lr, vlr);
  1028. }
  1029. static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
  1030. struct vgic_lr vlr)
  1031. {
  1032. vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
  1033. }
  1034. static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
  1035. {
  1036. return vgic_ops->get_elrsr(vcpu);
  1037. }
  1038. static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu)
  1039. {
  1040. return vgic_ops->get_eisr(vcpu);
  1041. }
  1042. static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu)
  1043. {
  1044. return vgic_ops->get_interrupt_status(vcpu);
  1045. }
  1046. static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu)
  1047. {
  1048. vgic_ops->enable_underflow(vcpu);
  1049. }
  1050. static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu)
  1051. {
  1052. vgic_ops->disable_underflow(vcpu);
  1053. }
  1054. static inline void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
  1055. {
  1056. vgic_ops->get_vmcr(vcpu, vmcr);
  1057. }
  1058. static void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
  1059. {
  1060. vgic_ops->set_vmcr(vcpu, vmcr);
  1061. }
  1062. static inline void vgic_enable(struct kvm_vcpu *vcpu)
  1063. {
  1064. vgic_ops->enable(vcpu);
  1065. }
  1066. static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
  1067. {
  1068. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1069. struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
  1070. vlr.state = 0;
  1071. vgic_set_lr(vcpu, lr_nr, vlr);
  1072. clear_bit(lr_nr, vgic_cpu->lr_used);
  1073. vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
  1074. }
  1075. /*
  1076. * An interrupt may have been disabled after being made pending on the
  1077. * CPU interface (the classic case is a timer running while we're
  1078. * rebooting the guest - the interrupt would kick as soon as the CPU
  1079. * interface gets enabled, with deadly consequences).
  1080. *
  1081. * The solution is to examine already active LRs, and check the
  1082. * interrupt is still enabled. If not, just retire it.
  1083. */
  1084. static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
  1085. {
  1086. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1087. int lr;
  1088. for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) {
  1089. struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
  1090. if (!vgic_irq_is_enabled(vcpu, vlr.irq)) {
  1091. vgic_retire_lr(lr, vlr.irq, vcpu);
  1092. if (vgic_irq_is_queued(vcpu, vlr.irq))
  1093. vgic_irq_clear_queued(vcpu, vlr.irq);
  1094. }
  1095. }
  1096. }
  1097. /*
  1098. * Queue an interrupt to a CPU virtual interface. Return true on success,
  1099. * or false if it wasn't possible to queue it.
  1100. */
  1101. static bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
  1102. {
  1103. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1104. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1105. struct vgic_lr vlr;
  1106. int lr;
  1107. /* Sanitize the input... */
  1108. BUG_ON(sgi_source_id & ~7);
  1109. BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS);
  1110. BUG_ON(irq >= dist->nr_irqs);
  1111. kvm_debug("Queue IRQ%d\n", irq);
  1112. lr = vgic_cpu->vgic_irq_lr_map[irq];
  1113. /* Do we have an active interrupt for the same CPUID? */
  1114. if (lr != LR_EMPTY) {
  1115. vlr = vgic_get_lr(vcpu, lr);
  1116. if (vlr.source == sgi_source_id) {
  1117. kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
  1118. BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
  1119. vlr.state |= LR_STATE_PENDING;
  1120. vgic_set_lr(vcpu, lr, vlr);
  1121. return true;
  1122. }
  1123. }
  1124. /* Try to use another LR for this interrupt */
  1125. lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used,
  1126. vgic->nr_lr);
  1127. if (lr >= vgic->nr_lr)
  1128. return false;
  1129. kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
  1130. vgic_cpu->vgic_irq_lr_map[irq] = lr;
  1131. set_bit(lr, vgic_cpu->lr_used);
  1132. vlr.irq = irq;
  1133. vlr.source = sgi_source_id;
  1134. vlr.state = LR_STATE_PENDING;
  1135. if (!vgic_irq_is_edge(vcpu, irq))
  1136. vlr.state |= LR_EOI_INT;
  1137. vgic_set_lr(vcpu, lr, vlr);
  1138. return true;
  1139. }
  1140. static bool vgic_queue_sgi(struct kvm_vcpu *vcpu, int irq)
  1141. {
  1142. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1143. unsigned long sources;
  1144. int vcpu_id = vcpu->vcpu_id;
  1145. int c;
  1146. sources = *vgic_get_sgi_sources(dist, vcpu_id, irq);
  1147. for_each_set_bit(c, &sources, dist->nr_cpus) {
  1148. if (vgic_queue_irq(vcpu, c, irq))
  1149. clear_bit(c, &sources);
  1150. }
  1151. *vgic_get_sgi_sources(dist, vcpu_id, irq) = sources;
  1152. /*
  1153. * If the sources bitmap has been cleared it means that we
  1154. * could queue all the SGIs onto link registers (see the
  1155. * clear_bit above), and therefore we are done with them in
  1156. * our emulated gic and can get rid of them.
  1157. */
  1158. if (!sources) {
  1159. vgic_dist_irq_clear_pending(vcpu, irq);
  1160. vgic_cpu_irq_clear(vcpu, irq);
  1161. return true;
  1162. }
  1163. return false;
  1164. }
  1165. static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq)
  1166. {
  1167. if (!vgic_can_sample_irq(vcpu, irq))
  1168. return true; /* level interrupt, already queued */
  1169. if (vgic_queue_irq(vcpu, 0, irq)) {
  1170. if (vgic_irq_is_edge(vcpu, irq)) {
  1171. vgic_dist_irq_clear_pending(vcpu, irq);
  1172. vgic_cpu_irq_clear(vcpu, irq);
  1173. } else {
  1174. vgic_irq_set_queued(vcpu, irq);
  1175. }
  1176. return true;
  1177. }
  1178. return false;
  1179. }
  1180. /*
  1181. * Fill the list registers with pending interrupts before running the
  1182. * guest.
  1183. */
  1184. static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  1185. {
  1186. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1187. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1188. int i, vcpu_id;
  1189. int overflow = 0;
  1190. vcpu_id = vcpu->vcpu_id;
  1191. /*
  1192. * We may not have any pending interrupt, or the interrupts
  1193. * may have been serviced from another vcpu. In all cases,
  1194. * move along.
  1195. */
  1196. if (!kvm_vgic_vcpu_pending_irq(vcpu)) {
  1197. pr_debug("CPU%d has no pending interrupt\n", vcpu_id);
  1198. goto epilog;
  1199. }
  1200. /* SGIs */
  1201. for_each_set_bit(i, vgic_cpu->pending_percpu, VGIC_NR_SGIS) {
  1202. if (!vgic_queue_sgi(vcpu, i))
  1203. overflow = 1;
  1204. }
  1205. /* PPIs */
  1206. for_each_set_bit_from(i, vgic_cpu->pending_percpu, VGIC_NR_PRIVATE_IRQS) {
  1207. if (!vgic_queue_hwirq(vcpu, i))
  1208. overflow = 1;
  1209. }
  1210. /* SPIs */
  1211. for_each_set_bit(i, vgic_cpu->pending_shared, vgic_nr_shared_irqs(dist)) {
  1212. if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS))
  1213. overflow = 1;
  1214. }
  1215. epilog:
  1216. if (overflow) {
  1217. vgic_enable_underflow(vcpu);
  1218. } else {
  1219. vgic_disable_underflow(vcpu);
  1220. /*
  1221. * We're about to run this VCPU, and we've consumed
  1222. * everything the distributor had in store for
  1223. * us. Claim we don't have anything pending. We'll
  1224. * adjust that if needed while exiting.
  1225. */
  1226. clear_bit(vcpu_id, dist->irq_pending_on_cpu);
  1227. }
  1228. }
  1229. static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
  1230. {
  1231. u32 status = vgic_get_interrupt_status(vcpu);
  1232. bool level_pending = false;
  1233. kvm_debug("STATUS = %08x\n", status);
  1234. if (status & INT_STATUS_EOI) {
  1235. /*
  1236. * Some level interrupts have been EOIed. Clear their
  1237. * active bit.
  1238. */
  1239. u64 eisr = vgic_get_eisr(vcpu);
  1240. unsigned long *eisr_ptr = u64_to_bitmask(&eisr);
  1241. int lr;
  1242. for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
  1243. struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
  1244. WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
  1245. vgic_irq_clear_queued(vcpu, vlr.irq);
  1246. WARN_ON(vlr.state & LR_STATE_MASK);
  1247. vlr.state = 0;
  1248. vgic_set_lr(vcpu, lr, vlr);
  1249. /*
  1250. * If the IRQ was EOIed it was also ACKed and we we
  1251. * therefore assume we can clear the soft pending
  1252. * state (should it had been set) for this interrupt.
  1253. *
  1254. * Note: if the IRQ soft pending state was set after
  1255. * the IRQ was acked, it actually shouldn't be
  1256. * cleared, but we have no way of knowing that unless
  1257. * we start trapping ACKs when the soft-pending state
  1258. * is set.
  1259. */
  1260. vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
  1261. /* Any additional pending interrupt? */
  1262. if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
  1263. vgic_cpu_irq_set(vcpu, vlr.irq);
  1264. level_pending = true;
  1265. } else {
  1266. vgic_dist_irq_clear_pending(vcpu, vlr.irq);
  1267. vgic_cpu_irq_clear(vcpu, vlr.irq);
  1268. }
  1269. /*
  1270. * Despite being EOIed, the LR may not have
  1271. * been marked as empty.
  1272. */
  1273. vgic_sync_lr_elrsr(vcpu, lr, vlr);
  1274. }
  1275. }
  1276. if (status & INT_STATUS_UNDERFLOW)
  1277. vgic_disable_underflow(vcpu);
  1278. return level_pending;
  1279. }
  1280. /*
  1281. * Sync back the VGIC state after a guest run. The distributor lock is
  1282. * needed so we don't get preempted in the middle of the state processing.
  1283. */
  1284. static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  1285. {
  1286. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1287. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1288. u64 elrsr;
  1289. unsigned long *elrsr_ptr;
  1290. int lr, pending;
  1291. bool level_pending;
  1292. level_pending = vgic_process_maintenance(vcpu);
  1293. elrsr = vgic_get_elrsr(vcpu);
  1294. elrsr_ptr = u64_to_bitmask(&elrsr);
  1295. /* Clear mappings for empty LRs */
  1296. for_each_set_bit(lr, elrsr_ptr, vgic->nr_lr) {
  1297. struct vgic_lr vlr;
  1298. if (!test_and_clear_bit(lr, vgic_cpu->lr_used))
  1299. continue;
  1300. vlr = vgic_get_lr(vcpu, lr);
  1301. BUG_ON(vlr.irq >= dist->nr_irqs);
  1302. vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
  1303. }
  1304. /* Check if we still have something up our sleeve... */
  1305. pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
  1306. if (level_pending || pending < vgic->nr_lr)
  1307. set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
  1308. }
  1309. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  1310. {
  1311. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1312. if (!irqchip_in_kernel(vcpu->kvm))
  1313. return;
  1314. spin_lock(&dist->lock);
  1315. __kvm_vgic_flush_hwstate(vcpu);
  1316. spin_unlock(&dist->lock);
  1317. }
  1318. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  1319. {
  1320. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1321. if (!irqchip_in_kernel(vcpu->kvm))
  1322. return;
  1323. spin_lock(&dist->lock);
  1324. __kvm_vgic_sync_hwstate(vcpu);
  1325. spin_unlock(&dist->lock);
  1326. }
  1327. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  1328. {
  1329. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1330. if (!irqchip_in_kernel(vcpu->kvm))
  1331. return 0;
  1332. return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
  1333. }
  1334. static void vgic_kick_vcpus(struct kvm *kvm)
  1335. {
  1336. struct kvm_vcpu *vcpu;
  1337. int c;
  1338. /*
  1339. * We've injected an interrupt, time to find out who deserves
  1340. * a good kick...
  1341. */
  1342. kvm_for_each_vcpu(c, vcpu, kvm) {
  1343. if (kvm_vgic_vcpu_pending_irq(vcpu))
  1344. kvm_vcpu_kick(vcpu);
  1345. }
  1346. }
  1347. static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
  1348. {
  1349. int edge_triggered = vgic_irq_is_edge(vcpu, irq);
  1350. /*
  1351. * Only inject an interrupt if:
  1352. * - edge triggered and we have a rising edge
  1353. * - level triggered and we change level
  1354. */
  1355. if (edge_triggered) {
  1356. int state = vgic_dist_irq_is_pending(vcpu, irq);
  1357. return level > state;
  1358. } else {
  1359. int state = vgic_dist_irq_get_level(vcpu, irq);
  1360. return level != state;
  1361. }
  1362. }
  1363. static bool vgic_update_irq_pending(struct kvm *kvm, int cpuid,
  1364. unsigned int irq_num, bool level)
  1365. {
  1366. struct vgic_dist *dist = &kvm->arch.vgic;
  1367. struct kvm_vcpu *vcpu;
  1368. int edge_triggered, level_triggered;
  1369. int enabled;
  1370. bool ret = true;
  1371. spin_lock(&dist->lock);
  1372. vcpu = kvm_get_vcpu(kvm, cpuid);
  1373. edge_triggered = vgic_irq_is_edge(vcpu, irq_num);
  1374. level_triggered = !edge_triggered;
  1375. if (!vgic_validate_injection(vcpu, irq_num, level)) {
  1376. ret = false;
  1377. goto out;
  1378. }
  1379. if (irq_num >= VGIC_NR_PRIVATE_IRQS) {
  1380. cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS];
  1381. vcpu = kvm_get_vcpu(kvm, cpuid);
  1382. }
  1383. kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
  1384. if (level) {
  1385. if (level_triggered)
  1386. vgic_dist_irq_set_level(vcpu, irq_num);
  1387. vgic_dist_irq_set_pending(vcpu, irq_num);
  1388. } else {
  1389. if (level_triggered) {
  1390. vgic_dist_irq_clear_level(vcpu, irq_num);
  1391. if (!vgic_dist_irq_soft_pend(vcpu, irq_num))
  1392. vgic_dist_irq_clear_pending(vcpu, irq_num);
  1393. } else {
  1394. vgic_dist_irq_clear_pending(vcpu, irq_num);
  1395. }
  1396. }
  1397. enabled = vgic_irq_is_enabled(vcpu, irq_num);
  1398. if (!enabled) {
  1399. ret = false;
  1400. goto out;
  1401. }
  1402. if (!vgic_can_sample_irq(vcpu, irq_num)) {
  1403. /*
  1404. * Level interrupt in progress, will be picked up
  1405. * when EOId.
  1406. */
  1407. ret = false;
  1408. goto out;
  1409. }
  1410. if (level) {
  1411. vgic_cpu_irq_set(vcpu, irq_num);
  1412. set_bit(cpuid, dist->irq_pending_on_cpu);
  1413. }
  1414. out:
  1415. spin_unlock(&dist->lock);
  1416. return ret;
  1417. }
  1418. /**
  1419. * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
  1420. * @kvm: The VM structure pointer
  1421. * @cpuid: The CPU for PPIs
  1422. * @irq_num: The IRQ number that is assigned to the device
  1423. * @level: Edge-triggered: true: to trigger the interrupt
  1424. * false: to ignore the call
  1425. * Level-sensitive true: activates an interrupt
  1426. * false: deactivates an interrupt
  1427. *
  1428. * The GIC is not concerned with devices being active-LOW or active-HIGH for
  1429. * level-sensitive interrupts. You can think of the level parameter as 1
  1430. * being HIGH and 0 being LOW and all devices being active-HIGH.
  1431. */
  1432. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
  1433. bool level)
  1434. {
  1435. if (likely(vgic_initialized(kvm)) &&
  1436. vgic_update_irq_pending(kvm, cpuid, irq_num, level))
  1437. vgic_kick_vcpus(kvm);
  1438. return 0;
  1439. }
  1440. static irqreturn_t vgic_maintenance_handler(int irq, void *data)
  1441. {
  1442. /*
  1443. * We cannot rely on the vgic maintenance interrupt to be
  1444. * delivered synchronously. This means we can only use it to
  1445. * exit the VM, and we perform the handling of EOIed
  1446. * interrupts on the exit path (see vgic_process_maintenance).
  1447. */
  1448. return IRQ_HANDLED;
  1449. }
  1450. void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
  1451. {
  1452. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1453. kfree(vgic_cpu->pending_shared);
  1454. kfree(vgic_cpu->vgic_irq_lr_map);
  1455. vgic_cpu->pending_shared = NULL;
  1456. vgic_cpu->vgic_irq_lr_map = NULL;
  1457. }
  1458. static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
  1459. {
  1460. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1461. int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
  1462. vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
  1463. vgic_cpu->vgic_irq_lr_map = kzalloc(nr_irqs, GFP_KERNEL);
  1464. if (!vgic_cpu->pending_shared || !vgic_cpu->vgic_irq_lr_map) {
  1465. kvm_vgic_vcpu_destroy(vcpu);
  1466. return -ENOMEM;
  1467. }
  1468. return 0;
  1469. }
  1470. /**
  1471. * kvm_vgic_vcpu_init - Initialize per-vcpu VGIC state
  1472. * @vcpu: pointer to the vcpu struct
  1473. *
  1474. * Initialize the vgic_cpu struct and vgic_dist struct fields pertaining to
  1475. * this vcpu and enable the VGIC for this VCPU
  1476. */
  1477. static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
  1478. {
  1479. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  1480. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  1481. int i;
  1482. for (i = 0; i < dist->nr_irqs; i++) {
  1483. if (i < VGIC_NR_PPIS)
  1484. vgic_bitmap_set_irq_val(&dist->irq_enabled,
  1485. vcpu->vcpu_id, i, 1);
  1486. if (i < VGIC_NR_PRIVATE_IRQS)
  1487. vgic_bitmap_set_irq_val(&dist->irq_cfg,
  1488. vcpu->vcpu_id, i, VGIC_CFG_EDGE);
  1489. vgic_cpu->vgic_irq_lr_map[i] = LR_EMPTY;
  1490. }
  1491. /*
  1492. * Store the number of LRs per vcpu, so we don't have to go
  1493. * all the way to the distributor structure to find out. Only
  1494. * assembly code should use this one.
  1495. */
  1496. vgic_cpu->nr_lr = vgic->nr_lr;
  1497. vgic_enable(vcpu);
  1498. }
  1499. void kvm_vgic_destroy(struct kvm *kvm)
  1500. {
  1501. struct vgic_dist *dist = &kvm->arch.vgic;
  1502. struct kvm_vcpu *vcpu;
  1503. int i;
  1504. kvm_for_each_vcpu(i, vcpu, kvm)
  1505. kvm_vgic_vcpu_destroy(vcpu);
  1506. vgic_free_bitmap(&dist->irq_enabled);
  1507. vgic_free_bitmap(&dist->irq_level);
  1508. vgic_free_bitmap(&dist->irq_pending);
  1509. vgic_free_bitmap(&dist->irq_soft_pend);
  1510. vgic_free_bitmap(&dist->irq_queued);
  1511. vgic_free_bitmap(&dist->irq_cfg);
  1512. vgic_free_bytemap(&dist->irq_priority);
  1513. if (dist->irq_spi_target) {
  1514. for (i = 0; i < dist->nr_cpus; i++)
  1515. vgic_free_bitmap(&dist->irq_spi_target[i]);
  1516. }
  1517. kfree(dist->irq_sgi_sources);
  1518. kfree(dist->irq_spi_cpu);
  1519. kfree(dist->irq_spi_target);
  1520. kfree(dist->irq_pending_on_cpu);
  1521. dist->irq_sgi_sources = NULL;
  1522. dist->irq_spi_cpu = NULL;
  1523. dist->irq_spi_target = NULL;
  1524. dist->irq_pending_on_cpu = NULL;
  1525. }
  1526. /*
  1527. * Allocate and initialize the various data structures. Must be called
  1528. * with kvm->lock held!
  1529. */
  1530. static int vgic_init_maps(struct kvm *kvm)
  1531. {
  1532. struct vgic_dist *dist = &kvm->arch.vgic;
  1533. struct kvm_vcpu *vcpu;
  1534. int nr_cpus, nr_irqs;
  1535. int ret, i;
  1536. if (dist->nr_cpus) /* Already allocated */
  1537. return 0;
  1538. nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
  1539. if (!nr_cpus) /* No vcpus? Can't be good... */
  1540. return -EINVAL;
  1541. /*
  1542. * If nobody configured the number of interrupts, use the
  1543. * legacy one.
  1544. */
  1545. if (!dist->nr_irqs)
  1546. dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
  1547. nr_irqs = dist->nr_irqs;
  1548. ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs);
  1549. ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs);
  1550. ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs);
  1551. ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs);
  1552. ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs);
  1553. ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs);
  1554. ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs);
  1555. if (ret)
  1556. goto out;
  1557. dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL);
  1558. dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL);
  1559. dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus,
  1560. GFP_KERNEL);
  1561. dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long),
  1562. GFP_KERNEL);
  1563. if (!dist->irq_sgi_sources ||
  1564. !dist->irq_spi_cpu ||
  1565. !dist->irq_spi_target ||
  1566. !dist->irq_pending_on_cpu) {
  1567. ret = -ENOMEM;
  1568. goto out;
  1569. }
  1570. for (i = 0; i < nr_cpus; i++)
  1571. ret |= vgic_init_bitmap(&dist->irq_spi_target[i],
  1572. nr_cpus, nr_irqs);
  1573. if (ret)
  1574. goto out;
  1575. kvm_for_each_vcpu(i, vcpu, kvm) {
  1576. ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
  1577. if (ret) {
  1578. kvm_err("VGIC: Failed to allocate vcpu memory\n");
  1579. break;
  1580. }
  1581. }
  1582. for (i = VGIC_NR_PRIVATE_IRQS; i < dist->nr_irqs; i += 4)
  1583. vgic_set_target_reg(kvm, 0, i);
  1584. out:
  1585. if (ret)
  1586. kvm_vgic_destroy(kvm);
  1587. return ret;
  1588. }
  1589. /**
  1590. * kvm_vgic_init - Initialize global VGIC state before running any VCPUs
  1591. * @kvm: pointer to the kvm struct
  1592. *
  1593. * Map the virtual CPU interface into the VM before running any VCPUs. We
  1594. * can't do this at creation time, because user space must first set the
  1595. * virtual CPU interface address in the guest physical address space. Also
  1596. * initialize the ITARGETSRn regs to 0 on the emulated distributor.
  1597. */
  1598. int kvm_vgic_init(struct kvm *kvm)
  1599. {
  1600. struct kvm_vcpu *vcpu;
  1601. int ret = 0, i;
  1602. if (!irqchip_in_kernel(kvm))
  1603. return 0;
  1604. mutex_lock(&kvm->lock);
  1605. if (vgic_initialized(kvm))
  1606. goto out;
  1607. if (IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_dist_base) ||
  1608. IS_VGIC_ADDR_UNDEF(kvm->arch.vgic.vgic_cpu_base)) {
  1609. kvm_err("Need to set vgic cpu and dist addresses first\n");
  1610. ret = -ENXIO;
  1611. goto out;
  1612. }
  1613. ret = vgic_init_maps(kvm);
  1614. if (ret) {
  1615. kvm_err("Unable to allocate maps\n");
  1616. goto out;
  1617. }
  1618. ret = kvm_phys_addr_ioremap(kvm, kvm->arch.vgic.vgic_cpu_base,
  1619. vgic->vcpu_base, KVM_VGIC_V2_CPU_SIZE,
  1620. true);
  1621. if (ret) {
  1622. kvm_err("Unable to remap VGIC CPU to VCPU\n");
  1623. goto out;
  1624. }
  1625. kvm_for_each_vcpu(i, vcpu, kvm)
  1626. kvm_vgic_vcpu_init(vcpu);
  1627. kvm->arch.vgic.ready = true;
  1628. out:
  1629. if (ret)
  1630. kvm_vgic_destroy(kvm);
  1631. mutex_unlock(&kvm->lock);
  1632. return ret;
  1633. }
  1634. int kvm_vgic_create(struct kvm *kvm)
  1635. {
  1636. int i, vcpu_lock_idx = -1, ret = 0;
  1637. struct kvm_vcpu *vcpu;
  1638. mutex_lock(&kvm->lock);
  1639. if (kvm->arch.vgic.vctrl_base) {
  1640. ret = -EEXIST;
  1641. goto out;
  1642. }
  1643. /*
  1644. * Any time a vcpu is run, vcpu_load is called which tries to grab the
  1645. * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
  1646. * that no other VCPUs are run while we create the vgic.
  1647. */
  1648. kvm_for_each_vcpu(i, vcpu, kvm) {
  1649. if (!mutex_trylock(&vcpu->mutex))
  1650. goto out_unlock;
  1651. vcpu_lock_idx = i;
  1652. }
  1653. kvm_for_each_vcpu(i, vcpu, kvm) {
  1654. if (vcpu->arch.has_run_once) {
  1655. ret = -EBUSY;
  1656. goto out_unlock;
  1657. }
  1658. }
  1659. spin_lock_init(&kvm->arch.vgic.lock);
  1660. kvm->arch.vgic.in_kernel = true;
  1661. kvm->arch.vgic.vctrl_base = vgic->vctrl_base;
  1662. kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
  1663. kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
  1664. out_unlock:
  1665. for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
  1666. vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
  1667. mutex_unlock(&vcpu->mutex);
  1668. }
  1669. out:
  1670. mutex_unlock(&kvm->lock);
  1671. return ret;
  1672. }
  1673. static int vgic_ioaddr_overlap(struct kvm *kvm)
  1674. {
  1675. phys_addr_t dist = kvm->arch.vgic.vgic_dist_base;
  1676. phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base;
  1677. if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu))
  1678. return 0;
  1679. if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) ||
  1680. (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist))
  1681. return -EBUSY;
  1682. return 0;
  1683. }
  1684. static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr,
  1685. phys_addr_t addr, phys_addr_t size)
  1686. {
  1687. int ret;
  1688. if (addr & ~KVM_PHYS_MASK)
  1689. return -E2BIG;
  1690. if (addr & (SZ_4K - 1))
  1691. return -EINVAL;
  1692. if (!IS_VGIC_ADDR_UNDEF(*ioaddr))
  1693. return -EEXIST;
  1694. if (addr + size < addr)
  1695. return -EINVAL;
  1696. *ioaddr = addr;
  1697. ret = vgic_ioaddr_overlap(kvm);
  1698. if (ret)
  1699. *ioaddr = VGIC_ADDR_UNDEF;
  1700. return ret;
  1701. }
  1702. /**
  1703. * kvm_vgic_addr - set or get vgic VM base addresses
  1704. * @kvm: pointer to the vm struct
  1705. * @type: the VGIC addr type, one of KVM_VGIC_V2_ADDR_TYPE_XXX
  1706. * @addr: pointer to address value
  1707. * @write: if true set the address in the VM address space, if false read the
  1708. * address
  1709. *
  1710. * Set or get the vgic base addresses for the distributor and the virtual CPU
  1711. * interface in the VM physical address space. These addresses are properties
  1712. * of the emulated core/SoC and therefore user space initially knows this
  1713. * information.
  1714. */
  1715. int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
  1716. {
  1717. int r = 0;
  1718. struct vgic_dist *vgic = &kvm->arch.vgic;
  1719. mutex_lock(&kvm->lock);
  1720. switch (type) {
  1721. case KVM_VGIC_V2_ADDR_TYPE_DIST:
  1722. if (write) {
  1723. r = vgic_ioaddr_assign(kvm, &vgic->vgic_dist_base,
  1724. *addr, KVM_VGIC_V2_DIST_SIZE);
  1725. } else {
  1726. *addr = vgic->vgic_dist_base;
  1727. }
  1728. break;
  1729. case KVM_VGIC_V2_ADDR_TYPE_CPU:
  1730. if (write) {
  1731. r = vgic_ioaddr_assign(kvm, &vgic->vgic_cpu_base,
  1732. *addr, KVM_VGIC_V2_CPU_SIZE);
  1733. } else {
  1734. *addr = vgic->vgic_cpu_base;
  1735. }
  1736. break;
  1737. default:
  1738. r = -ENODEV;
  1739. }
  1740. mutex_unlock(&kvm->lock);
  1741. return r;
  1742. }
  1743. static bool handle_cpu_mmio_misc(struct kvm_vcpu *vcpu,
  1744. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  1745. {
  1746. bool updated = false;
  1747. struct vgic_vmcr vmcr;
  1748. u32 *vmcr_field;
  1749. u32 reg;
  1750. vgic_get_vmcr(vcpu, &vmcr);
  1751. switch (offset & ~0x3) {
  1752. case GIC_CPU_CTRL:
  1753. vmcr_field = &vmcr.ctlr;
  1754. break;
  1755. case GIC_CPU_PRIMASK:
  1756. vmcr_field = &vmcr.pmr;
  1757. break;
  1758. case GIC_CPU_BINPOINT:
  1759. vmcr_field = &vmcr.bpr;
  1760. break;
  1761. case GIC_CPU_ALIAS_BINPOINT:
  1762. vmcr_field = &vmcr.abpr;
  1763. break;
  1764. default:
  1765. BUG();
  1766. }
  1767. if (!mmio->is_write) {
  1768. reg = *vmcr_field;
  1769. mmio_data_write(mmio, ~0, reg);
  1770. } else {
  1771. reg = mmio_data_read(mmio, ~0);
  1772. if (reg != *vmcr_field) {
  1773. *vmcr_field = reg;
  1774. vgic_set_vmcr(vcpu, &vmcr);
  1775. updated = true;
  1776. }
  1777. }
  1778. return updated;
  1779. }
  1780. static bool handle_mmio_abpr(struct kvm_vcpu *vcpu,
  1781. struct kvm_exit_mmio *mmio, phys_addr_t offset)
  1782. {
  1783. return handle_cpu_mmio_misc(vcpu, mmio, GIC_CPU_ALIAS_BINPOINT);
  1784. }
  1785. static bool handle_cpu_mmio_ident(struct kvm_vcpu *vcpu,
  1786. struct kvm_exit_mmio *mmio,
  1787. phys_addr_t offset)
  1788. {
  1789. u32 reg;
  1790. if (mmio->is_write)
  1791. return false;
  1792. /* GICC_IIDR */
  1793. reg = (PRODUCT_ID_KVM << 20) |
  1794. (GICC_ARCH_VERSION_V2 << 16) |
  1795. (IMPLEMENTER_ARM << 0);
  1796. mmio_data_write(mmio, ~0, reg);
  1797. return false;
  1798. }
  1799. /*
  1800. * CPU Interface Register accesses - these are not accessed by the VM, but by
  1801. * user space for saving and restoring VGIC state.
  1802. */
  1803. static const struct mmio_range vgic_cpu_ranges[] = {
  1804. {
  1805. .base = GIC_CPU_CTRL,
  1806. .len = 12,
  1807. .handle_mmio = handle_cpu_mmio_misc,
  1808. },
  1809. {
  1810. .base = GIC_CPU_ALIAS_BINPOINT,
  1811. .len = 4,
  1812. .handle_mmio = handle_mmio_abpr,
  1813. },
  1814. {
  1815. .base = GIC_CPU_ACTIVEPRIO,
  1816. .len = 16,
  1817. .handle_mmio = handle_mmio_raz_wi,
  1818. },
  1819. {
  1820. .base = GIC_CPU_IDENT,
  1821. .len = 4,
  1822. .handle_mmio = handle_cpu_mmio_ident,
  1823. },
  1824. };
  1825. static int vgic_attr_regs_access(struct kvm_device *dev,
  1826. struct kvm_device_attr *attr,
  1827. u32 *reg, bool is_write)
  1828. {
  1829. const struct mmio_range *r = NULL, *ranges;
  1830. phys_addr_t offset;
  1831. int ret, cpuid, c;
  1832. struct kvm_vcpu *vcpu, *tmp_vcpu;
  1833. struct vgic_dist *vgic;
  1834. struct kvm_exit_mmio mmio;
  1835. offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
  1836. cpuid = (attr->attr & KVM_DEV_ARM_VGIC_CPUID_MASK) >>
  1837. KVM_DEV_ARM_VGIC_CPUID_SHIFT;
  1838. mutex_lock(&dev->kvm->lock);
  1839. ret = vgic_init_maps(dev->kvm);
  1840. if (ret)
  1841. goto out;
  1842. if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) {
  1843. ret = -EINVAL;
  1844. goto out;
  1845. }
  1846. vcpu = kvm_get_vcpu(dev->kvm, cpuid);
  1847. vgic = &dev->kvm->arch.vgic;
  1848. mmio.len = 4;
  1849. mmio.is_write = is_write;
  1850. if (is_write)
  1851. mmio_data_write(&mmio, ~0, *reg);
  1852. switch (attr->group) {
  1853. case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
  1854. mmio.phys_addr = vgic->vgic_dist_base + offset;
  1855. ranges = vgic_dist_ranges;
  1856. break;
  1857. case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
  1858. mmio.phys_addr = vgic->vgic_cpu_base + offset;
  1859. ranges = vgic_cpu_ranges;
  1860. break;
  1861. default:
  1862. BUG();
  1863. }
  1864. r = find_matching_range(ranges, &mmio, offset);
  1865. if (unlikely(!r || !r->handle_mmio)) {
  1866. ret = -ENXIO;
  1867. goto out;
  1868. }
  1869. spin_lock(&vgic->lock);
  1870. /*
  1871. * Ensure that no other VCPU is running by checking the vcpu->cpu
  1872. * field. If no other VPCUs are running we can safely access the VGIC
  1873. * state, because even if another VPU is run after this point, that
  1874. * VCPU will not touch the vgic state, because it will block on
  1875. * getting the vgic->lock in kvm_vgic_sync_hwstate().
  1876. */
  1877. kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm) {
  1878. if (unlikely(tmp_vcpu->cpu != -1)) {
  1879. ret = -EBUSY;
  1880. goto out_vgic_unlock;
  1881. }
  1882. }
  1883. /*
  1884. * Move all pending IRQs from the LRs on all VCPUs so the pending
  1885. * state can be properly represented in the register state accessible
  1886. * through this API.
  1887. */
  1888. kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm)
  1889. vgic_unqueue_irqs(tmp_vcpu);
  1890. offset -= r->base;
  1891. r->handle_mmio(vcpu, &mmio, offset);
  1892. if (!is_write)
  1893. *reg = mmio_data_read(&mmio, ~0);
  1894. ret = 0;
  1895. out_vgic_unlock:
  1896. spin_unlock(&vgic->lock);
  1897. out:
  1898. mutex_unlock(&dev->kvm->lock);
  1899. return ret;
  1900. }
  1901. static int vgic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  1902. {
  1903. int r;
  1904. switch (attr->group) {
  1905. case KVM_DEV_ARM_VGIC_GRP_ADDR: {
  1906. u64 __user *uaddr = (u64 __user *)(long)attr->addr;
  1907. u64 addr;
  1908. unsigned long type = (unsigned long)attr->attr;
  1909. if (copy_from_user(&addr, uaddr, sizeof(addr)))
  1910. return -EFAULT;
  1911. r = kvm_vgic_addr(dev->kvm, type, &addr, true);
  1912. return (r == -ENODEV) ? -ENXIO : r;
  1913. }
  1914. case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
  1915. case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
  1916. u32 __user *uaddr = (u32 __user *)(long)attr->addr;
  1917. u32 reg;
  1918. if (get_user(reg, uaddr))
  1919. return -EFAULT;
  1920. return vgic_attr_regs_access(dev, attr, &reg, true);
  1921. }
  1922. case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
  1923. u32 __user *uaddr = (u32 __user *)(long)attr->addr;
  1924. u32 val;
  1925. int ret = 0;
  1926. if (get_user(val, uaddr))
  1927. return -EFAULT;
  1928. /*
  1929. * We require:
  1930. * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs
  1931. * - at most 1024 interrupts
  1932. * - a multiple of 32 interrupts
  1933. */
  1934. if (val < (VGIC_NR_PRIVATE_IRQS + 32) ||
  1935. val > VGIC_MAX_IRQS ||
  1936. (val & 31))
  1937. return -EINVAL;
  1938. mutex_lock(&dev->kvm->lock);
  1939. if (vgic_initialized(dev->kvm) || dev->kvm->arch.vgic.nr_irqs)
  1940. ret = -EBUSY;
  1941. else
  1942. dev->kvm->arch.vgic.nr_irqs = val;
  1943. mutex_unlock(&dev->kvm->lock);
  1944. return ret;
  1945. }
  1946. }
  1947. return -ENXIO;
  1948. }
  1949. static int vgic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  1950. {
  1951. int r = -ENXIO;
  1952. switch (attr->group) {
  1953. case KVM_DEV_ARM_VGIC_GRP_ADDR: {
  1954. u64 __user *uaddr = (u64 __user *)(long)attr->addr;
  1955. u64 addr;
  1956. unsigned long type = (unsigned long)attr->attr;
  1957. r = kvm_vgic_addr(dev->kvm, type, &addr, false);
  1958. if (r)
  1959. return (r == -ENODEV) ? -ENXIO : r;
  1960. if (copy_to_user(uaddr, &addr, sizeof(addr)))
  1961. return -EFAULT;
  1962. break;
  1963. }
  1964. case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
  1965. case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: {
  1966. u32 __user *uaddr = (u32 __user *)(long)attr->addr;
  1967. u32 reg = 0;
  1968. r = vgic_attr_regs_access(dev, attr, &reg, false);
  1969. if (r)
  1970. return r;
  1971. r = put_user(reg, uaddr);
  1972. break;
  1973. }
  1974. case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: {
  1975. u32 __user *uaddr = (u32 __user *)(long)attr->addr;
  1976. r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr);
  1977. break;
  1978. }
  1979. }
  1980. return r;
  1981. }
  1982. static int vgic_has_attr_regs(const struct mmio_range *ranges,
  1983. phys_addr_t offset)
  1984. {
  1985. struct kvm_exit_mmio dev_attr_mmio;
  1986. dev_attr_mmio.len = 4;
  1987. if (find_matching_range(ranges, &dev_attr_mmio, offset))
  1988. return 0;
  1989. else
  1990. return -ENXIO;
  1991. }
  1992. static int vgic_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  1993. {
  1994. phys_addr_t offset;
  1995. switch (attr->group) {
  1996. case KVM_DEV_ARM_VGIC_GRP_ADDR:
  1997. switch (attr->attr) {
  1998. case KVM_VGIC_V2_ADDR_TYPE_DIST:
  1999. case KVM_VGIC_V2_ADDR_TYPE_CPU:
  2000. return 0;
  2001. }
  2002. break;
  2003. case KVM_DEV_ARM_VGIC_GRP_DIST_REGS:
  2004. offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
  2005. return vgic_has_attr_regs(vgic_dist_ranges, offset);
  2006. case KVM_DEV_ARM_VGIC_GRP_CPU_REGS:
  2007. offset = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK;
  2008. return vgic_has_attr_regs(vgic_cpu_ranges, offset);
  2009. case KVM_DEV_ARM_VGIC_GRP_NR_IRQS:
  2010. return 0;
  2011. }
  2012. return -ENXIO;
  2013. }
  2014. static void vgic_destroy(struct kvm_device *dev)
  2015. {
  2016. kfree(dev);
  2017. }
  2018. static int vgic_create(struct kvm_device *dev, u32 type)
  2019. {
  2020. return kvm_vgic_create(dev->kvm);
  2021. }
  2022. static struct kvm_device_ops kvm_arm_vgic_v2_ops = {
  2023. .name = "kvm-arm-vgic",
  2024. .create = vgic_create,
  2025. .destroy = vgic_destroy,
  2026. .set_attr = vgic_set_attr,
  2027. .get_attr = vgic_get_attr,
  2028. .has_attr = vgic_has_attr,
  2029. };
  2030. static void vgic_init_maintenance_interrupt(void *info)
  2031. {
  2032. enable_percpu_irq(vgic->maint_irq, 0);
  2033. }
  2034. static int vgic_cpu_notify(struct notifier_block *self,
  2035. unsigned long action, void *cpu)
  2036. {
  2037. switch (action) {
  2038. case CPU_STARTING:
  2039. case CPU_STARTING_FROZEN:
  2040. vgic_init_maintenance_interrupt(NULL);
  2041. break;
  2042. case CPU_DYING:
  2043. case CPU_DYING_FROZEN:
  2044. disable_percpu_irq(vgic->maint_irq);
  2045. break;
  2046. }
  2047. return NOTIFY_OK;
  2048. }
  2049. static struct notifier_block vgic_cpu_nb = {
  2050. .notifier_call = vgic_cpu_notify,
  2051. };
  2052. static const struct of_device_id vgic_ids[] = {
  2053. { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, },
  2054. { .compatible = "arm,gic-v3", .data = vgic_v3_probe, },
  2055. {},
  2056. };
  2057. int kvm_vgic_hyp_init(void)
  2058. {
  2059. const struct of_device_id *matched_id;
  2060. const int (*vgic_probe)(struct device_node *,const struct vgic_ops **,
  2061. const struct vgic_params **);
  2062. struct device_node *vgic_node;
  2063. int ret;
  2064. vgic_node = of_find_matching_node_and_match(NULL,
  2065. vgic_ids, &matched_id);
  2066. if (!vgic_node) {
  2067. kvm_err("error: no compatible GIC node found\n");
  2068. return -ENODEV;
  2069. }
  2070. vgic_probe = matched_id->data;
  2071. ret = vgic_probe(vgic_node, &vgic_ops, &vgic);
  2072. if (ret)
  2073. return ret;
  2074. ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler,
  2075. "vgic", kvm_get_running_vcpus());
  2076. if (ret) {
  2077. kvm_err("Cannot register interrupt %d\n", vgic->maint_irq);
  2078. return ret;
  2079. }
  2080. ret = __register_cpu_notifier(&vgic_cpu_nb);
  2081. if (ret) {
  2082. kvm_err("Cannot register vgic CPU notifier\n");
  2083. goto out_free_irq;
  2084. }
  2085. /* Callback into for arch code for setup */
  2086. vgic_arch_setup(vgic);
  2087. on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1);
  2088. return kvm_register_device_ops(&kvm_arm_vgic_v2_ops,
  2089. KVM_DEV_TYPE_ARM_VGIC_V2);
  2090. out_free_irq:
  2091. free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus());
  2092. return ret;
  2093. }