vgic.c 65 KB

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