vgic.c 51 KB

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