vgic.c 62 KB

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