vgic-mmio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * VGIC MMIO handling functions
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/bitops.h>
  14. #include <linux/bsearch.h>
  15. #include <linux/kvm.h>
  16. #include <linux/kvm_host.h>
  17. #include <kvm/iodev.h>
  18. #include <kvm/arm_vgic.h>
  19. #include "vgic.h"
  20. #include "vgic-mmio.h"
  21. unsigned long vgic_mmio_read_raz(struct kvm_vcpu *vcpu,
  22. gpa_t addr, unsigned int len)
  23. {
  24. return 0;
  25. }
  26. unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu,
  27. gpa_t addr, unsigned int len)
  28. {
  29. return -1UL;
  30. }
  31. void vgic_mmio_write_wi(struct kvm_vcpu *vcpu, gpa_t addr,
  32. unsigned int len, unsigned long val)
  33. {
  34. /* Ignore */
  35. }
  36. /*
  37. * Read accesses to both GICD_ICENABLER and GICD_ISENABLER return the value
  38. * of the enabled bit, so there is only one function for both here.
  39. */
  40. unsigned long vgic_mmio_read_enable(struct kvm_vcpu *vcpu,
  41. gpa_t addr, unsigned int len)
  42. {
  43. u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
  44. u32 value = 0;
  45. int i;
  46. /* Loop over all IRQs affected by this read */
  47. for (i = 0; i < len * 8; i++) {
  48. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  49. if (irq->enabled)
  50. value |= (1U << i);
  51. vgic_put_irq(vcpu->kvm, irq);
  52. }
  53. return value;
  54. }
  55. void vgic_mmio_write_senable(struct kvm_vcpu *vcpu,
  56. gpa_t addr, unsigned int len,
  57. unsigned long val)
  58. {
  59. u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
  60. int i;
  61. for_each_set_bit(i, &val, len * 8) {
  62. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  63. spin_lock(&irq->irq_lock);
  64. irq->enabled = true;
  65. vgic_queue_irq_unlock(vcpu->kvm, irq);
  66. vgic_put_irq(vcpu->kvm, irq);
  67. }
  68. }
  69. void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu,
  70. gpa_t addr, unsigned int len,
  71. unsigned long val)
  72. {
  73. u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
  74. int i;
  75. for_each_set_bit(i, &val, len * 8) {
  76. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  77. spin_lock(&irq->irq_lock);
  78. irq->enabled = false;
  79. spin_unlock(&irq->irq_lock);
  80. vgic_put_irq(vcpu->kvm, irq);
  81. }
  82. }
  83. unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu,
  84. gpa_t addr, unsigned int len)
  85. {
  86. u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
  87. u32 value = 0;
  88. int i;
  89. /* Loop over all IRQs affected by this read */
  90. for (i = 0; i < len * 8; i++) {
  91. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  92. if (irq->pending)
  93. value |= (1U << i);
  94. vgic_put_irq(vcpu->kvm, irq);
  95. }
  96. return value;
  97. }
  98. void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,
  99. gpa_t addr, unsigned int len,
  100. unsigned long val)
  101. {
  102. u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
  103. int i;
  104. for_each_set_bit(i, &val, len * 8) {
  105. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  106. spin_lock(&irq->irq_lock);
  107. irq->pending = true;
  108. if (irq->config == VGIC_CONFIG_LEVEL)
  109. irq->soft_pending = true;
  110. vgic_queue_irq_unlock(vcpu->kvm, irq);
  111. vgic_put_irq(vcpu->kvm, irq);
  112. }
  113. }
  114. void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,
  115. gpa_t addr, unsigned int len,
  116. unsigned long val)
  117. {
  118. u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
  119. int i;
  120. for_each_set_bit(i, &val, len * 8) {
  121. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  122. spin_lock(&irq->irq_lock);
  123. if (irq->config == VGIC_CONFIG_LEVEL) {
  124. irq->soft_pending = false;
  125. irq->pending = irq->line_level;
  126. } else {
  127. irq->pending = false;
  128. }
  129. spin_unlock(&irq->irq_lock);
  130. vgic_put_irq(vcpu->kvm, irq);
  131. }
  132. }
  133. unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu,
  134. gpa_t addr, unsigned int len)
  135. {
  136. u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
  137. u32 value = 0;
  138. int i;
  139. /* Loop over all IRQs affected by this read */
  140. for (i = 0; i < len * 8; i++) {
  141. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  142. if (irq->active)
  143. value |= (1U << i);
  144. vgic_put_irq(vcpu->kvm, irq);
  145. }
  146. return value;
  147. }
  148. static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
  149. bool new_active_state)
  150. {
  151. struct kvm_vcpu *requester_vcpu;
  152. spin_lock(&irq->irq_lock);
  153. /*
  154. * The vcpu parameter here can mean multiple things depending on how
  155. * this function is called; when handling a trap from the kernel it
  156. * depends on the GIC version, and these functions are also called as
  157. * part of save/restore from userspace.
  158. *
  159. * Therefore, we have to figure out the requester in a reliable way.
  160. *
  161. * When accessing VGIC state from user space, the requester_vcpu is
  162. * NULL, which is fine, because we guarantee that no VCPUs are running
  163. * when accessing VGIC state from user space so irq->vcpu->cpu is
  164. * always -1.
  165. */
  166. requester_vcpu = kvm_arm_get_running_vcpu();
  167. /*
  168. * If this virtual IRQ was written into a list register, we
  169. * have to make sure the CPU that runs the VCPU thread has
  170. * synced back the LR state to the struct vgic_irq.
  171. *
  172. * As long as the conditions below are true, we know the VCPU thread
  173. * may be on its way back from the guest (we kicked the VCPU thread in
  174. * vgic_change_active_prepare) and still has to sync back this IRQ,
  175. * so we release and re-acquire the spin_lock to let the other thread
  176. * sync back the IRQ.
  177. */
  178. while (irq->vcpu && /* IRQ may have state in an LR somewhere */
  179. irq->vcpu != requester_vcpu && /* Current thread is not the VCPU thread */
  180. irq->vcpu->cpu != -1) /* VCPU thread is running */
  181. cond_resched_lock(&irq->irq_lock);
  182. irq->active = new_active_state;
  183. if (new_active_state)
  184. vgic_queue_irq_unlock(vcpu->kvm, irq);
  185. else
  186. spin_unlock(&irq->irq_lock);
  187. }
  188. /*
  189. * If we are fiddling with an IRQ's active state, we have to make sure the IRQ
  190. * is not queued on some running VCPU's LRs, because then the change to the
  191. * active state can be overwritten when the VCPU's state is synced coming back
  192. * from the guest.
  193. *
  194. * For shared interrupts, we have to stop all the VCPUs because interrupts can
  195. * be migrated while we don't hold the IRQ locks and we don't want to be
  196. * chasing moving targets.
  197. *
  198. * For private interrupts, we only have to make sure the single and only VCPU
  199. * that can potentially queue the IRQ is stopped.
  200. */
  201. static void vgic_change_active_prepare(struct kvm_vcpu *vcpu, u32 intid)
  202. {
  203. if (intid < VGIC_NR_PRIVATE_IRQS)
  204. kvm_arm_halt_vcpu(vcpu);
  205. else
  206. kvm_arm_halt_guest(vcpu->kvm);
  207. }
  208. /* See vgic_change_active_prepare */
  209. static void vgic_change_active_finish(struct kvm_vcpu *vcpu, u32 intid)
  210. {
  211. if (intid < VGIC_NR_PRIVATE_IRQS)
  212. kvm_arm_resume_vcpu(vcpu);
  213. else
  214. kvm_arm_resume_guest(vcpu->kvm);
  215. }
  216. void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,
  217. gpa_t addr, unsigned int len,
  218. unsigned long val)
  219. {
  220. u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
  221. int i;
  222. vgic_change_active_prepare(vcpu, intid);
  223. for_each_set_bit(i, &val, len * 8) {
  224. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  225. vgic_mmio_change_active(vcpu, irq, false);
  226. vgic_put_irq(vcpu->kvm, irq);
  227. }
  228. vgic_change_active_finish(vcpu, intid);
  229. }
  230. void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,
  231. gpa_t addr, unsigned int len,
  232. unsigned long val)
  233. {
  234. u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
  235. int i;
  236. vgic_change_active_prepare(vcpu, intid);
  237. for_each_set_bit(i, &val, len * 8) {
  238. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  239. vgic_mmio_change_active(vcpu, irq, true);
  240. vgic_put_irq(vcpu->kvm, irq);
  241. }
  242. vgic_change_active_finish(vcpu, intid);
  243. }
  244. unsigned long vgic_mmio_read_priority(struct kvm_vcpu *vcpu,
  245. gpa_t addr, unsigned int len)
  246. {
  247. u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
  248. int i;
  249. u64 val = 0;
  250. for (i = 0; i < len; i++) {
  251. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  252. val |= (u64)irq->priority << (i * 8);
  253. vgic_put_irq(vcpu->kvm, irq);
  254. }
  255. return val;
  256. }
  257. /*
  258. * We currently don't handle changing the priority of an interrupt that
  259. * is already pending on a VCPU. If there is a need for this, we would
  260. * need to make this VCPU exit and re-evaluate the priorities, potentially
  261. * leading to this interrupt getting presented now to the guest (if it has
  262. * been masked by the priority mask before).
  263. */
  264. void vgic_mmio_write_priority(struct kvm_vcpu *vcpu,
  265. gpa_t addr, unsigned int len,
  266. unsigned long val)
  267. {
  268. u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
  269. int i;
  270. for (i = 0; i < len; i++) {
  271. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  272. spin_lock(&irq->irq_lock);
  273. /* Narrow the priority range to what we actually support */
  274. irq->priority = (val >> (i * 8)) & GENMASK(7, 8 - VGIC_PRI_BITS);
  275. spin_unlock(&irq->irq_lock);
  276. vgic_put_irq(vcpu->kvm, irq);
  277. }
  278. }
  279. unsigned long vgic_mmio_read_config(struct kvm_vcpu *vcpu,
  280. gpa_t addr, unsigned int len)
  281. {
  282. u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
  283. u32 value = 0;
  284. int i;
  285. for (i = 0; i < len * 4; i++) {
  286. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  287. if (irq->config == VGIC_CONFIG_EDGE)
  288. value |= (2U << (i * 2));
  289. vgic_put_irq(vcpu->kvm, irq);
  290. }
  291. return value;
  292. }
  293. void vgic_mmio_write_config(struct kvm_vcpu *vcpu,
  294. gpa_t addr, unsigned int len,
  295. unsigned long val)
  296. {
  297. u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
  298. int i;
  299. for (i = 0; i < len * 4; i++) {
  300. struct vgic_irq *irq;
  301. /*
  302. * The configuration cannot be changed for SGIs in general,
  303. * for PPIs this is IMPLEMENTATION DEFINED. The arch timer
  304. * code relies on PPIs being level triggered, so we also
  305. * make them read-only here.
  306. */
  307. if (intid + i < VGIC_NR_PRIVATE_IRQS)
  308. continue;
  309. irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
  310. spin_lock(&irq->irq_lock);
  311. if (test_bit(i * 2 + 1, &val)) {
  312. irq->config = VGIC_CONFIG_EDGE;
  313. } else {
  314. irq->config = VGIC_CONFIG_LEVEL;
  315. irq->pending = irq->line_level | irq->soft_pending;
  316. }
  317. spin_unlock(&irq->irq_lock);
  318. vgic_put_irq(vcpu->kvm, irq);
  319. }
  320. }
  321. static int match_region(const void *key, const void *elt)
  322. {
  323. const unsigned int offset = (unsigned long)key;
  324. const struct vgic_register_region *region = elt;
  325. if (offset < region->reg_offset)
  326. return -1;
  327. if (offset >= region->reg_offset + region->len)
  328. return 1;
  329. return 0;
  330. }
  331. /* Find the proper register handler entry given a certain address offset. */
  332. static const struct vgic_register_region *
  333. vgic_find_mmio_region(const struct vgic_register_region *region, int nr_regions,
  334. unsigned int offset)
  335. {
  336. return bsearch((void *)(uintptr_t)offset, region, nr_regions,
  337. sizeof(region[0]), match_region);
  338. }
  339. /*
  340. * kvm_mmio_read_buf() returns a value in a format where it can be converted
  341. * to a byte array and be directly observed as the guest wanted it to appear
  342. * in memory if it had done the store itself, which is LE for the GIC, as the
  343. * guest knows the GIC is always LE.
  344. *
  345. * We convert this value to the CPUs native format to deal with it as a data
  346. * value.
  347. */
  348. unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len)
  349. {
  350. unsigned long data = kvm_mmio_read_buf(val, len);
  351. switch (len) {
  352. case 1:
  353. return data;
  354. case 2:
  355. return le16_to_cpu(data);
  356. case 4:
  357. return le32_to_cpu(data);
  358. default:
  359. return le64_to_cpu(data);
  360. }
  361. }
  362. /*
  363. * kvm_mmio_write_buf() expects a value in a format such that if converted to
  364. * a byte array it is observed as the guest would see it if it could perform
  365. * the load directly. Since the GIC is LE, and the guest knows this, the
  366. * guest expects a value in little endian format.
  367. *
  368. * We convert the data value from the CPUs native format to LE so that the
  369. * value is returned in the proper format.
  370. */
  371. void vgic_data_host_to_mmio_bus(void *buf, unsigned int len,
  372. unsigned long data)
  373. {
  374. switch (len) {
  375. case 1:
  376. break;
  377. case 2:
  378. data = cpu_to_le16(data);
  379. break;
  380. case 4:
  381. data = cpu_to_le32(data);
  382. break;
  383. default:
  384. data = cpu_to_le64(data);
  385. }
  386. kvm_mmio_write_buf(buf, len, data);
  387. }
  388. static
  389. struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev)
  390. {
  391. return container_of(dev, struct vgic_io_device, dev);
  392. }
  393. static bool check_region(const struct kvm *kvm,
  394. const struct vgic_register_region *region,
  395. gpa_t addr, int len)
  396. {
  397. int flags, nr_irqs = kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
  398. switch (len) {
  399. case sizeof(u8):
  400. flags = VGIC_ACCESS_8bit;
  401. break;
  402. case sizeof(u32):
  403. flags = VGIC_ACCESS_32bit;
  404. break;
  405. case sizeof(u64):
  406. flags = VGIC_ACCESS_64bit;
  407. break;
  408. default:
  409. return false;
  410. }
  411. if ((region->access_flags & flags) && IS_ALIGNED(addr, len)) {
  412. if (!region->bits_per_irq)
  413. return true;
  414. /* Do we access a non-allocated IRQ? */
  415. return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs;
  416. }
  417. return false;
  418. }
  419. static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  420. gpa_t addr, int len, void *val)
  421. {
  422. struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
  423. const struct vgic_register_region *region;
  424. unsigned long data = 0;
  425. region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
  426. addr - iodev->base_addr);
  427. if (!region || !check_region(vcpu->kvm, region, addr, len)) {
  428. memset(val, 0, len);
  429. return 0;
  430. }
  431. switch (iodev->iodev_type) {
  432. case IODEV_CPUIF:
  433. data = region->read(vcpu, addr, len);
  434. break;
  435. case IODEV_DIST:
  436. data = region->read(vcpu, addr, len);
  437. break;
  438. case IODEV_REDIST:
  439. data = region->read(iodev->redist_vcpu, addr, len);
  440. break;
  441. case IODEV_ITS:
  442. data = region->its_read(vcpu->kvm, iodev->its, addr, len);
  443. break;
  444. }
  445. vgic_data_host_to_mmio_bus(val, len, data);
  446. return 0;
  447. }
  448. static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
  449. gpa_t addr, int len, const void *val)
  450. {
  451. struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
  452. const struct vgic_register_region *region;
  453. unsigned long data = vgic_data_mmio_bus_to_host(val, len);
  454. region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
  455. addr - iodev->base_addr);
  456. if (!region || !check_region(vcpu->kvm, region, addr, len))
  457. return 0;
  458. switch (iodev->iodev_type) {
  459. case IODEV_CPUIF:
  460. region->write(vcpu, addr, len, data);
  461. break;
  462. case IODEV_DIST:
  463. region->write(vcpu, addr, len, data);
  464. break;
  465. case IODEV_REDIST:
  466. region->write(iodev->redist_vcpu, addr, len, data);
  467. break;
  468. case IODEV_ITS:
  469. region->its_write(vcpu->kvm, iodev->its, addr, len, data);
  470. break;
  471. }
  472. return 0;
  473. }
  474. struct kvm_io_device_ops kvm_io_gic_ops = {
  475. .read = dispatch_mmio_read,
  476. .write = dispatch_mmio_write,
  477. };
  478. int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
  479. enum vgic_type type)
  480. {
  481. struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev;
  482. int ret = 0;
  483. unsigned int len;
  484. switch (type) {
  485. case VGIC_V2:
  486. len = vgic_v2_init_dist_iodev(io_device);
  487. break;
  488. case VGIC_V3:
  489. len = vgic_v3_init_dist_iodev(io_device);
  490. break;
  491. default:
  492. BUG_ON(1);
  493. }
  494. io_device->base_addr = dist_base_address;
  495. io_device->iodev_type = IODEV_DIST;
  496. io_device->redist_vcpu = NULL;
  497. mutex_lock(&kvm->slots_lock);
  498. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, dist_base_address,
  499. len, &io_device->dev);
  500. mutex_unlock(&kvm->slots_lock);
  501. return ret;
  502. }