vgic.c 62 KB

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