vgic.c 49 KB

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