vgic.c 63 KB

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