vgic.c 50 KB

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