vgic.c 59 KB

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