vgic.c 55 KB

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