vgic-its.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. /*
  2. * GICv3 ITS emulation
  3. *
  4. * Copyright (C) 2015,2016 ARM Ltd.
  5. * Author: Andre Przywara <andre.przywara@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/cpu.h>
  20. #include <linux/kvm.h>
  21. #include <linux/kvm_host.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/list.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/irqchip/arm-gic-v3.h>
  26. #include <asm/kvm_emulate.h>
  27. #include <asm/kvm_arm.h>
  28. #include <asm/kvm_mmu.h>
  29. #include "vgic.h"
  30. #include "vgic-mmio.h"
  31. /*
  32. * Creates a new (reference to a) struct vgic_irq for a given LPI.
  33. * If this LPI is already mapped on another ITS, we increase its refcount
  34. * and return a pointer to the existing structure.
  35. * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq.
  36. * This function returns a pointer to the _unlocked_ structure.
  37. */
  38. static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid)
  39. {
  40. struct vgic_dist *dist = &kvm->arch.vgic;
  41. struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq;
  42. /* In this case there is no put, since we keep the reference. */
  43. if (irq)
  44. return irq;
  45. irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL);
  46. if (!irq)
  47. return ERR_PTR(-ENOMEM);
  48. INIT_LIST_HEAD(&irq->lpi_list);
  49. INIT_LIST_HEAD(&irq->ap_list);
  50. spin_lock_init(&irq->irq_lock);
  51. irq->config = VGIC_CONFIG_EDGE;
  52. kref_init(&irq->refcount);
  53. irq->intid = intid;
  54. spin_lock(&dist->lpi_list_lock);
  55. /*
  56. * There could be a race with another vgic_add_lpi(), so we need to
  57. * check that we don't add a second list entry with the same LPI.
  58. */
  59. list_for_each_entry(oldirq, &dist->lpi_list_head, lpi_list) {
  60. if (oldirq->intid != intid)
  61. continue;
  62. /* Someone was faster with adding this LPI, lets use that. */
  63. kfree(irq);
  64. irq = oldirq;
  65. /*
  66. * This increases the refcount, the caller is expected to
  67. * call vgic_put_irq() on the returned pointer once it's
  68. * finished with the IRQ.
  69. */
  70. vgic_get_irq_kref(irq);
  71. goto out_unlock;
  72. }
  73. list_add_tail(&irq->lpi_list, &dist->lpi_list_head);
  74. dist->lpi_list_count++;
  75. out_unlock:
  76. spin_unlock(&dist->lpi_list_lock);
  77. return irq;
  78. }
  79. struct its_device {
  80. struct list_head dev_list;
  81. /* the head for the list of ITTEs */
  82. struct list_head itt_head;
  83. u32 device_id;
  84. };
  85. #define COLLECTION_NOT_MAPPED ((u32)~0)
  86. struct its_collection {
  87. struct list_head coll_list;
  88. u32 collection_id;
  89. u32 target_addr;
  90. };
  91. #define its_is_collection_mapped(coll) ((coll) && \
  92. ((coll)->target_addr != COLLECTION_NOT_MAPPED))
  93. struct its_itte {
  94. struct list_head itte_list;
  95. struct vgic_irq *irq;
  96. struct its_collection *collection;
  97. u32 lpi;
  98. u32 event_id;
  99. };
  100. /*
  101. * Find and returns a device in the device table for an ITS.
  102. * Must be called with the its_lock mutex held.
  103. */
  104. static struct its_device *find_its_device(struct vgic_its *its, u32 device_id)
  105. {
  106. struct its_device *device;
  107. list_for_each_entry(device, &its->device_list, dev_list)
  108. if (device_id == device->device_id)
  109. return device;
  110. return NULL;
  111. }
  112. /*
  113. * Find and returns an interrupt translation table entry (ITTE) for a given
  114. * Device ID/Event ID pair on an ITS.
  115. * Must be called with the its_lock mutex held.
  116. */
  117. static struct its_itte *find_itte(struct vgic_its *its, u32 device_id,
  118. u32 event_id)
  119. {
  120. struct its_device *device;
  121. struct its_itte *itte;
  122. device = find_its_device(its, device_id);
  123. if (device == NULL)
  124. return NULL;
  125. list_for_each_entry(itte, &device->itt_head, itte_list)
  126. if (itte->event_id == event_id)
  127. return itte;
  128. return NULL;
  129. }
  130. /* To be used as an iterator this macro misses the enclosing parentheses */
  131. #define for_each_lpi_its(dev, itte, its) \
  132. list_for_each_entry(dev, &(its)->device_list, dev_list) \
  133. list_for_each_entry(itte, &(dev)->itt_head, itte_list)
  134. /*
  135. * We only implement 48 bits of PA at the moment, although the ITS
  136. * supports more. Let's be restrictive here.
  137. */
  138. #define BASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16))
  139. #define CBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12))
  140. #define PENDBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16))
  141. #define PROPBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12))
  142. #define GIC_LPI_OFFSET 8192
  143. /*
  144. * Finds and returns a collection in the ITS collection table.
  145. * Must be called with the its_lock mutex held.
  146. */
  147. static struct its_collection *find_collection(struct vgic_its *its, int coll_id)
  148. {
  149. struct its_collection *collection;
  150. list_for_each_entry(collection, &its->collection_list, coll_list) {
  151. if (coll_id == collection->collection_id)
  152. return collection;
  153. }
  154. return NULL;
  155. }
  156. #define LPI_PROP_ENABLE_BIT(p) ((p) & LPI_PROP_ENABLED)
  157. #define LPI_PROP_PRIORITY(p) ((p) & 0xfc)
  158. /*
  159. * Reads the configuration data for a given LPI from guest memory and
  160. * updates the fields in struct vgic_irq.
  161. * If filter_vcpu is not NULL, applies only if the IRQ is targeting this
  162. * VCPU. Unconditionally applies if filter_vcpu is NULL.
  163. */
  164. static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
  165. struct kvm_vcpu *filter_vcpu)
  166. {
  167. u64 propbase = PROPBASER_ADDRESS(kvm->arch.vgic.propbaser);
  168. u8 prop;
  169. int ret;
  170. ret = kvm_read_guest_lock(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
  171. &prop, 1);
  172. if (ret)
  173. return ret;
  174. spin_lock(&irq->irq_lock);
  175. if (!filter_vcpu || filter_vcpu == irq->target_vcpu) {
  176. irq->priority = LPI_PROP_PRIORITY(prop);
  177. irq->enabled = LPI_PROP_ENABLE_BIT(prop);
  178. vgic_queue_irq_unlock(kvm, irq);
  179. } else {
  180. spin_unlock(&irq->irq_lock);
  181. }
  182. return 0;
  183. }
  184. /*
  185. * Create a snapshot of the current LPI list, so that we can enumerate all
  186. * LPIs without holding any lock.
  187. * Returns the array length and puts the kmalloc'ed array into intid_ptr.
  188. */
  189. static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
  190. {
  191. struct vgic_dist *dist = &kvm->arch.vgic;
  192. struct vgic_irq *irq;
  193. u32 *intids;
  194. int irq_count = dist->lpi_list_count, i = 0;
  195. /*
  196. * We use the current value of the list length, which may change
  197. * after the kmalloc. We don't care, because the guest shouldn't
  198. * change anything while the command handling is still running,
  199. * and in the worst case we would miss a new IRQ, which one wouldn't
  200. * expect to be covered by this command anyway.
  201. */
  202. intids = kmalloc_array(irq_count, sizeof(intids[0]), GFP_KERNEL);
  203. if (!intids)
  204. return -ENOMEM;
  205. spin_lock(&dist->lpi_list_lock);
  206. list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
  207. /* We don't need to "get" the IRQ, as we hold the list lock. */
  208. intids[i] = irq->intid;
  209. if (++i == irq_count)
  210. break;
  211. }
  212. spin_unlock(&dist->lpi_list_lock);
  213. *intid_ptr = intids;
  214. return irq_count;
  215. }
  216. /*
  217. * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI
  218. * is targeting) to the VGIC's view, which deals with target VCPUs.
  219. * Needs to be called whenever either the collection for a LPIs has
  220. * changed or the collection itself got retargeted.
  221. */
  222. static void update_affinity_itte(struct kvm *kvm, struct its_itte *itte)
  223. {
  224. struct kvm_vcpu *vcpu;
  225. if (!its_is_collection_mapped(itte->collection))
  226. return;
  227. vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr);
  228. spin_lock(&itte->irq->irq_lock);
  229. itte->irq->target_vcpu = vcpu;
  230. spin_unlock(&itte->irq->irq_lock);
  231. }
  232. /*
  233. * Updates the target VCPU for every LPI targeting this collection.
  234. * Must be called with the its_lock mutex held.
  235. */
  236. static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its,
  237. struct its_collection *coll)
  238. {
  239. struct its_device *device;
  240. struct its_itte *itte;
  241. for_each_lpi_its(device, itte, its) {
  242. if (!itte->collection || coll != itte->collection)
  243. continue;
  244. update_affinity_itte(kvm, itte);
  245. }
  246. }
  247. static u32 max_lpis_propbaser(u64 propbaser)
  248. {
  249. int nr_idbits = (propbaser & 0x1f) + 1;
  250. return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS);
  251. }
  252. /*
  253. * Scan the whole LPI pending table and sync the pending bit in there
  254. * with our own data structures. This relies on the LPI being
  255. * mapped before.
  256. */
  257. static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
  258. {
  259. gpa_t pendbase = PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
  260. struct vgic_irq *irq;
  261. int last_byte_offset = -1;
  262. int ret = 0;
  263. u32 *intids;
  264. int nr_irqs, i;
  265. u8 pendmask;
  266. nr_irqs = vgic_copy_lpi_list(vcpu->kvm, &intids);
  267. if (nr_irqs < 0)
  268. return nr_irqs;
  269. for (i = 0; i < nr_irqs; i++) {
  270. int byte_offset, bit_nr;
  271. byte_offset = intids[i] / BITS_PER_BYTE;
  272. bit_nr = intids[i] % BITS_PER_BYTE;
  273. /*
  274. * For contiguously allocated LPIs chances are we just read
  275. * this very same byte in the last iteration. Reuse that.
  276. */
  277. if (byte_offset != last_byte_offset) {
  278. ret = kvm_read_guest_lock(vcpu->kvm,
  279. pendbase + byte_offset,
  280. &pendmask, 1);
  281. if (ret) {
  282. kfree(intids);
  283. return ret;
  284. }
  285. last_byte_offset = byte_offset;
  286. }
  287. irq = vgic_get_irq(vcpu->kvm, NULL, intids[i]);
  288. spin_lock(&irq->irq_lock);
  289. irq->pending = pendmask & (1U << bit_nr);
  290. vgic_queue_irq_unlock(vcpu->kvm, irq);
  291. vgic_put_irq(vcpu->kvm, irq);
  292. }
  293. kfree(intids);
  294. return ret;
  295. }
  296. static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,
  297. struct vgic_its *its,
  298. gpa_t addr, unsigned int len)
  299. {
  300. u64 reg = GITS_TYPER_PLPIS;
  301. /*
  302. * We use linear CPU numbers for redistributor addressing,
  303. * so GITS_TYPER.PTA is 0.
  304. * Also we force all PROPBASER registers to be the same, so
  305. * CommonLPIAff is 0 as well.
  306. * To avoid memory waste in the guest, we keep the number of IDBits and
  307. * DevBits low - as least for the time being.
  308. */
  309. reg |= 0x0f << GITS_TYPER_DEVBITS_SHIFT;
  310. reg |= 0x0f << GITS_TYPER_IDBITS_SHIFT;
  311. return extract_bytes(reg, addr & 7, len);
  312. }
  313. static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,
  314. struct vgic_its *its,
  315. gpa_t addr, unsigned int len)
  316. {
  317. return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
  318. }
  319. static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
  320. struct vgic_its *its,
  321. gpa_t addr, unsigned int len)
  322. {
  323. switch (addr & 0xffff) {
  324. case GITS_PIDR0:
  325. return 0x92; /* part number, bits[7:0] */
  326. case GITS_PIDR1:
  327. return 0xb4; /* part number, bits[11:8] */
  328. case GITS_PIDR2:
  329. return GIC_PIDR2_ARCH_GICv3 | 0x0b;
  330. case GITS_PIDR4:
  331. return 0x40; /* This is a 64K software visible page */
  332. /* The following are the ID registers for (any) GIC. */
  333. case GITS_CIDR0:
  334. return 0x0d;
  335. case GITS_CIDR1:
  336. return 0xf0;
  337. case GITS_CIDR2:
  338. return 0x05;
  339. case GITS_CIDR3:
  340. return 0xb1;
  341. }
  342. return 0;
  343. }
  344. /*
  345. * Find the target VCPU and the LPI number for a given devid/eventid pair
  346. * and make this IRQ pending, possibly injecting it.
  347. * Must be called with the its_lock mutex held.
  348. * Returns 0 on success, a positive error value for any ITS mapping
  349. * related errors and negative error values for generic errors.
  350. */
  351. static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
  352. u32 devid, u32 eventid)
  353. {
  354. struct kvm_vcpu *vcpu;
  355. struct its_itte *itte;
  356. if (!its->enabled)
  357. return -EBUSY;
  358. itte = find_itte(its, devid, eventid);
  359. if (!itte || !its_is_collection_mapped(itte->collection))
  360. return E_ITS_INT_UNMAPPED_INTERRUPT;
  361. vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr);
  362. if (!vcpu)
  363. return E_ITS_INT_UNMAPPED_INTERRUPT;
  364. if (!vcpu->arch.vgic_cpu.lpis_enabled)
  365. return -EBUSY;
  366. spin_lock(&itte->irq->irq_lock);
  367. itte->irq->pending = true;
  368. vgic_queue_irq_unlock(kvm, itte->irq);
  369. return 0;
  370. }
  371. static struct vgic_io_device *vgic_get_its_iodev(struct kvm_io_device *dev)
  372. {
  373. struct vgic_io_device *iodev;
  374. if (dev->ops != &kvm_io_gic_ops)
  375. return NULL;
  376. iodev = container_of(dev, struct vgic_io_device, dev);
  377. if (iodev->iodev_type != IODEV_ITS)
  378. return NULL;
  379. return iodev;
  380. }
  381. /*
  382. * Queries the KVM IO bus framework to get the ITS pointer from the given
  383. * doorbell address.
  384. * We then call vgic_its_trigger_msi() with the decoded data.
  385. * According to the KVM_SIGNAL_MSI API description returns 1 on success.
  386. */
  387. int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
  388. {
  389. u64 address;
  390. struct kvm_io_device *kvm_io_dev;
  391. struct vgic_io_device *iodev;
  392. int ret;
  393. if (!vgic_has_its(kvm))
  394. return -ENODEV;
  395. if (!(msi->flags & KVM_MSI_VALID_DEVID))
  396. return -EINVAL;
  397. address = (u64)msi->address_hi << 32 | msi->address_lo;
  398. kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address);
  399. if (!kvm_io_dev)
  400. return -EINVAL;
  401. iodev = vgic_get_its_iodev(kvm_io_dev);
  402. if (!iodev)
  403. return -EINVAL;
  404. mutex_lock(&iodev->its->its_lock);
  405. ret = vgic_its_trigger_msi(kvm, iodev->its, msi->devid, msi->data);
  406. mutex_unlock(&iodev->its->its_lock);
  407. if (ret < 0)
  408. return ret;
  409. /*
  410. * KVM_SIGNAL_MSI demands a return value > 0 for success and 0
  411. * if the guest has blocked the MSI. So we map any LPI mapping
  412. * related error to that.
  413. */
  414. if (ret)
  415. return 0;
  416. else
  417. return 1;
  418. }
  419. /* Requires the its_lock to be held. */
  420. static void its_free_itte(struct kvm *kvm, struct its_itte *itte)
  421. {
  422. list_del(&itte->itte_list);
  423. /* This put matches the get in vgic_add_lpi. */
  424. if (itte->irq)
  425. vgic_put_irq(kvm, itte->irq);
  426. kfree(itte);
  427. }
  428. static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)
  429. {
  430. return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1);
  431. }
  432. #define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8)
  433. #define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32)
  434. #define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32)
  435. #define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32)
  436. #define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16)
  437. #define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32)
  438. #define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1)
  439. /*
  440. * The DISCARD command frees an Interrupt Translation Table Entry (ITTE).
  441. * Must be called with the its_lock mutex held.
  442. */
  443. static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,
  444. u64 *its_cmd)
  445. {
  446. u32 device_id = its_cmd_get_deviceid(its_cmd);
  447. u32 event_id = its_cmd_get_id(its_cmd);
  448. struct its_itte *itte;
  449. itte = find_itte(its, device_id, event_id);
  450. if (itte && itte->collection) {
  451. /*
  452. * Though the spec talks about removing the pending state, we
  453. * don't bother here since we clear the ITTE anyway and the
  454. * pending state is a property of the ITTE struct.
  455. */
  456. its_free_itte(kvm, itte);
  457. return 0;
  458. }
  459. return E_ITS_DISCARD_UNMAPPED_INTERRUPT;
  460. }
  461. /*
  462. * The MOVI command moves an ITTE to a different collection.
  463. * Must be called with the its_lock mutex held.
  464. */
  465. static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,
  466. u64 *its_cmd)
  467. {
  468. u32 device_id = its_cmd_get_deviceid(its_cmd);
  469. u32 event_id = its_cmd_get_id(its_cmd);
  470. u32 coll_id = its_cmd_get_collection(its_cmd);
  471. struct kvm_vcpu *vcpu;
  472. struct its_itte *itte;
  473. struct its_collection *collection;
  474. itte = find_itte(its, device_id, event_id);
  475. if (!itte)
  476. return E_ITS_MOVI_UNMAPPED_INTERRUPT;
  477. if (!its_is_collection_mapped(itte->collection))
  478. return E_ITS_MOVI_UNMAPPED_COLLECTION;
  479. collection = find_collection(its, coll_id);
  480. if (!its_is_collection_mapped(collection))
  481. return E_ITS_MOVI_UNMAPPED_COLLECTION;
  482. itte->collection = collection;
  483. vcpu = kvm_get_vcpu(kvm, collection->target_addr);
  484. spin_lock(&itte->irq->irq_lock);
  485. itte->irq->target_vcpu = vcpu;
  486. spin_unlock(&itte->irq->irq_lock);
  487. return 0;
  488. }
  489. /*
  490. * Check whether an ID can be stored into the corresponding guest table.
  491. * For a direct table this is pretty easy, but gets a bit nasty for
  492. * indirect tables. We check whether the resulting guest physical address
  493. * is actually valid (covered by a memslot and guest accessbible).
  494. * For this we have to read the respective first level entry.
  495. */
  496. static bool vgic_its_check_id(struct vgic_its *its, u64 baser, int id)
  497. {
  498. int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
  499. int index;
  500. u64 indirect_ptr;
  501. gfn_t gfn;
  502. if (!(baser & GITS_BASER_INDIRECT)) {
  503. phys_addr_t addr;
  504. if (id >= (l1_tbl_size / GITS_BASER_ENTRY_SIZE(baser)))
  505. return false;
  506. addr = BASER_ADDRESS(baser) + id * GITS_BASER_ENTRY_SIZE(baser);
  507. gfn = addr >> PAGE_SHIFT;
  508. return kvm_is_visible_gfn(its->dev->kvm, gfn);
  509. }
  510. /* calculate and check the index into the 1st level */
  511. index = id / (SZ_64K / GITS_BASER_ENTRY_SIZE(baser));
  512. if (index >= (l1_tbl_size / sizeof(u64)))
  513. return false;
  514. /* Each 1st level entry is represented by a 64-bit value. */
  515. if (kvm_read_guest_lock(its->dev->kvm,
  516. BASER_ADDRESS(baser) + index * sizeof(indirect_ptr),
  517. &indirect_ptr, sizeof(indirect_ptr)))
  518. return false;
  519. indirect_ptr = le64_to_cpu(indirect_ptr);
  520. /* check the valid bit of the first level entry */
  521. if (!(indirect_ptr & BIT_ULL(63)))
  522. return false;
  523. /*
  524. * Mask the guest physical address and calculate the frame number.
  525. * Any address beyond our supported 48 bits of PA will be caught
  526. * by the actual check in the final step.
  527. */
  528. indirect_ptr &= GENMASK_ULL(51, 16);
  529. /* Find the address of the actual entry */
  530. index = id % (SZ_64K / GITS_BASER_ENTRY_SIZE(baser));
  531. indirect_ptr += index * GITS_BASER_ENTRY_SIZE(baser);
  532. gfn = indirect_ptr >> PAGE_SHIFT;
  533. return kvm_is_visible_gfn(its->dev->kvm, gfn);
  534. }
  535. static int vgic_its_alloc_collection(struct vgic_its *its,
  536. struct its_collection **colp,
  537. u32 coll_id)
  538. {
  539. struct its_collection *collection;
  540. if (!vgic_its_check_id(its, its->baser_coll_table, coll_id))
  541. return E_ITS_MAPC_COLLECTION_OOR;
  542. collection = kzalloc(sizeof(*collection), GFP_KERNEL);
  543. if (!collection)
  544. return -ENOMEM;
  545. collection->collection_id = coll_id;
  546. collection->target_addr = COLLECTION_NOT_MAPPED;
  547. list_add_tail(&collection->coll_list, &its->collection_list);
  548. *colp = collection;
  549. return 0;
  550. }
  551. static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
  552. {
  553. struct its_collection *collection;
  554. struct its_device *device;
  555. struct its_itte *itte;
  556. /*
  557. * Clearing the mapping for that collection ID removes the
  558. * entry from the list. If there wasn't any before, we can
  559. * go home early.
  560. */
  561. collection = find_collection(its, coll_id);
  562. if (!collection)
  563. return;
  564. for_each_lpi_its(device, itte, its)
  565. if (itte->collection &&
  566. itte->collection->collection_id == coll_id)
  567. itte->collection = NULL;
  568. list_del(&collection->coll_list);
  569. kfree(collection);
  570. }
  571. /*
  572. * The MAPTI and MAPI commands map LPIs to ITTEs.
  573. * Must be called with its_lock mutex held.
  574. */
  575. static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
  576. u64 *its_cmd)
  577. {
  578. u32 device_id = its_cmd_get_deviceid(its_cmd);
  579. u32 event_id = its_cmd_get_id(its_cmd);
  580. u32 coll_id = its_cmd_get_collection(its_cmd);
  581. struct its_itte *itte;
  582. struct its_device *device;
  583. struct its_collection *collection, *new_coll = NULL;
  584. int lpi_nr;
  585. struct vgic_irq *irq;
  586. device = find_its_device(its, device_id);
  587. if (!device)
  588. return E_ITS_MAPTI_UNMAPPED_DEVICE;
  589. if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI)
  590. lpi_nr = its_cmd_get_physical_id(its_cmd);
  591. else
  592. lpi_nr = event_id;
  593. if (lpi_nr < GIC_LPI_OFFSET ||
  594. lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser))
  595. return E_ITS_MAPTI_PHYSICALID_OOR;
  596. /* If there is an existing mapping, behavior is UNPREDICTABLE. */
  597. if (find_itte(its, device_id, event_id))
  598. return 0;
  599. collection = find_collection(its, coll_id);
  600. if (!collection) {
  601. int ret = vgic_its_alloc_collection(its, &collection, coll_id);
  602. if (ret)
  603. return ret;
  604. new_coll = collection;
  605. }
  606. itte = kzalloc(sizeof(struct its_itte), GFP_KERNEL);
  607. if (!itte) {
  608. if (new_coll)
  609. vgic_its_free_collection(its, coll_id);
  610. return -ENOMEM;
  611. }
  612. itte->event_id = event_id;
  613. list_add_tail(&itte->itte_list, &device->itt_head);
  614. itte->collection = collection;
  615. itte->lpi = lpi_nr;
  616. irq = vgic_add_lpi(kvm, lpi_nr);
  617. if (IS_ERR(irq)) {
  618. if (new_coll)
  619. vgic_its_free_collection(its, coll_id);
  620. its_free_itte(kvm, itte);
  621. return PTR_ERR(irq);
  622. }
  623. itte->irq = irq;
  624. update_affinity_itte(kvm, itte);
  625. /*
  626. * We "cache" the configuration table entries in out struct vgic_irq's.
  627. * However we only have those structs for mapped IRQs, so we read in
  628. * the respective config data from memory here upon mapping the LPI.
  629. */
  630. update_lpi_config(kvm, itte->irq, NULL);
  631. return 0;
  632. }
  633. /* Requires the its_lock to be held. */
  634. static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device)
  635. {
  636. struct its_itte *itte, *temp;
  637. /*
  638. * The spec says that unmapping a device with still valid
  639. * ITTEs associated is UNPREDICTABLE. We remove all ITTEs,
  640. * since we cannot leave the memory unreferenced.
  641. */
  642. list_for_each_entry_safe(itte, temp, &device->itt_head, itte_list)
  643. its_free_itte(kvm, itte);
  644. list_del(&device->dev_list);
  645. kfree(device);
  646. }
  647. /*
  648. * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs).
  649. * Must be called with the its_lock mutex held.
  650. */
  651. static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
  652. u64 *its_cmd)
  653. {
  654. u32 device_id = its_cmd_get_deviceid(its_cmd);
  655. bool valid = its_cmd_get_validbit(its_cmd);
  656. struct its_device *device;
  657. if (!vgic_its_check_id(its, its->baser_device_table, device_id))
  658. return E_ITS_MAPD_DEVICE_OOR;
  659. device = find_its_device(its, device_id);
  660. /*
  661. * The spec says that calling MAPD on an already mapped device
  662. * invalidates all cached data for this device. We implement this
  663. * by removing the mapping and re-establishing it.
  664. */
  665. if (device)
  666. vgic_its_unmap_device(kvm, device);
  667. /*
  668. * The spec does not say whether unmapping a not-mapped device
  669. * is an error, so we are done in any case.
  670. */
  671. if (!valid)
  672. return 0;
  673. device = kzalloc(sizeof(struct its_device), GFP_KERNEL);
  674. if (!device)
  675. return -ENOMEM;
  676. device->device_id = device_id;
  677. INIT_LIST_HEAD(&device->itt_head);
  678. list_add_tail(&device->dev_list, &its->device_list);
  679. return 0;
  680. }
  681. /*
  682. * The MAPC command maps collection IDs to redistributors.
  683. * Must be called with the its_lock mutex held.
  684. */
  685. static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its,
  686. u64 *its_cmd)
  687. {
  688. u16 coll_id;
  689. u32 target_addr;
  690. struct its_collection *collection;
  691. bool valid;
  692. valid = its_cmd_get_validbit(its_cmd);
  693. coll_id = its_cmd_get_collection(its_cmd);
  694. target_addr = its_cmd_get_target_addr(its_cmd);
  695. if (target_addr >= atomic_read(&kvm->online_vcpus))
  696. return E_ITS_MAPC_PROCNUM_OOR;
  697. if (!valid) {
  698. vgic_its_free_collection(its, coll_id);
  699. } else {
  700. collection = find_collection(its, coll_id);
  701. if (!collection) {
  702. int ret;
  703. ret = vgic_its_alloc_collection(its, &collection,
  704. coll_id);
  705. if (ret)
  706. return ret;
  707. collection->target_addr = target_addr;
  708. } else {
  709. collection->target_addr = target_addr;
  710. update_affinity_collection(kvm, its, collection);
  711. }
  712. }
  713. return 0;
  714. }
  715. /*
  716. * The CLEAR command removes the pending state for a particular LPI.
  717. * Must be called with the its_lock mutex held.
  718. */
  719. static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its,
  720. u64 *its_cmd)
  721. {
  722. u32 device_id = its_cmd_get_deviceid(its_cmd);
  723. u32 event_id = its_cmd_get_id(its_cmd);
  724. struct its_itte *itte;
  725. itte = find_itte(its, device_id, event_id);
  726. if (!itte)
  727. return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
  728. itte->irq->pending = false;
  729. return 0;
  730. }
  731. /*
  732. * The INV command syncs the configuration bits from the memory table.
  733. * Must be called with the its_lock mutex held.
  734. */
  735. static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its,
  736. u64 *its_cmd)
  737. {
  738. u32 device_id = its_cmd_get_deviceid(its_cmd);
  739. u32 event_id = its_cmd_get_id(its_cmd);
  740. struct its_itte *itte;
  741. itte = find_itte(its, device_id, event_id);
  742. if (!itte)
  743. return E_ITS_INV_UNMAPPED_INTERRUPT;
  744. return update_lpi_config(kvm, itte->irq, NULL);
  745. }
  746. /*
  747. * The INVALL command requests flushing of all IRQ data in this collection.
  748. * Find the VCPU mapped to that collection, then iterate over the VM's list
  749. * of mapped LPIs and update the configuration for each IRQ which targets
  750. * the specified vcpu. The configuration will be read from the in-memory
  751. * configuration table.
  752. * Must be called with the its_lock mutex held.
  753. */
  754. static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
  755. u64 *its_cmd)
  756. {
  757. u32 coll_id = its_cmd_get_collection(its_cmd);
  758. struct its_collection *collection;
  759. struct kvm_vcpu *vcpu;
  760. struct vgic_irq *irq;
  761. u32 *intids;
  762. int irq_count, i;
  763. collection = find_collection(its, coll_id);
  764. if (!its_is_collection_mapped(collection))
  765. return E_ITS_INVALL_UNMAPPED_COLLECTION;
  766. vcpu = kvm_get_vcpu(kvm, collection->target_addr);
  767. irq_count = vgic_copy_lpi_list(kvm, &intids);
  768. if (irq_count < 0)
  769. return irq_count;
  770. for (i = 0; i < irq_count; i++) {
  771. irq = vgic_get_irq(kvm, NULL, intids[i]);
  772. if (!irq)
  773. continue;
  774. update_lpi_config(kvm, irq, vcpu);
  775. vgic_put_irq(kvm, irq);
  776. }
  777. kfree(intids);
  778. return 0;
  779. }
  780. /*
  781. * The MOVALL command moves the pending state of all IRQs targeting one
  782. * redistributor to another. We don't hold the pending state in the VCPUs,
  783. * but in the IRQs instead, so there is really not much to do for us here.
  784. * However the spec says that no IRQ must target the old redistributor
  785. * afterwards, so we make sure that no LPI is using the associated target_vcpu.
  786. * This command affects all LPIs in the system that target that redistributor.
  787. */
  788. static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,
  789. u64 *its_cmd)
  790. {
  791. struct vgic_dist *dist = &kvm->arch.vgic;
  792. u32 target1_addr = its_cmd_get_target_addr(its_cmd);
  793. u32 target2_addr = its_cmd_mask_field(its_cmd, 3, 16, 32);
  794. struct kvm_vcpu *vcpu1, *vcpu2;
  795. struct vgic_irq *irq;
  796. if (target1_addr >= atomic_read(&kvm->online_vcpus) ||
  797. target2_addr >= atomic_read(&kvm->online_vcpus))
  798. return E_ITS_MOVALL_PROCNUM_OOR;
  799. if (target1_addr == target2_addr)
  800. return 0;
  801. vcpu1 = kvm_get_vcpu(kvm, target1_addr);
  802. vcpu2 = kvm_get_vcpu(kvm, target2_addr);
  803. spin_lock(&dist->lpi_list_lock);
  804. list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
  805. spin_lock(&irq->irq_lock);
  806. if (irq->target_vcpu == vcpu1)
  807. irq->target_vcpu = vcpu2;
  808. spin_unlock(&irq->irq_lock);
  809. }
  810. spin_unlock(&dist->lpi_list_lock);
  811. return 0;
  812. }
  813. /*
  814. * The INT command injects the LPI associated with that DevID/EvID pair.
  815. * Must be called with the its_lock mutex held.
  816. */
  817. static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
  818. u64 *its_cmd)
  819. {
  820. u32 msi_data = its_cmd_get_id(its_cmd);
  821. u64 msi_devid = its_cmd_get_deviceid(its_cmd);
  822. return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data);
  823. }
  824. /*
  825. * This function is called with the its_cmd lock held, but the ITS data
  826. * structure lock dropped.
  827. */
  828. static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
  829. u64 *its_cmd)
  830. {
  831. int ret = -ENODEV;
  832. mutex_lock(&its->its_lock);
  833. switch (its_cmd_get_command(its_cmd)) {
  834. case GITS_CMD_MAPD:
  835. ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd);
  836. break;
  837. case GITS_CMD_MAPC:
  838. ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd);
  839. break;
  840. case GITS_CMD_MAPI:
  841. ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
  842. break;
  843. case GITS_CMD_MAPTI:
  844. ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
  845. break;
  846. case GITS_CMD_MOVI:
  847. ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd);
  848. break;
  849. case GITS_CMD_DISCARD:
  850. ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd);
  851. break;
  852. case GITS_CMD_CLEAR:
  853. ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd);
  854. break;
  855. case GITS_CMD_MOVALL:
  856. ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd);
  857. break;
  858. case GITS_CMD_INT:
  859. ret = vgic_its_cmd_handle_int(kvm, its, its_cmd);
  860. break;
  861. case GITS_CMD_INV:
  862. ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd);
  863. break;
  864. case GITS_CMD_INVALL:
  865. ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd);
  866. break;
  867. case GITS_CMD_SYNC:
  868. /* we ignore this command: we are in sync all of the time */
  869. ret = 0;
  870. break;
  871. }
  872. mutex_unlock(&its->its_lock);
  873. return ret;
  874. }
  875. static u64 vgic_sanitise_its_baser(u64 reg)
  876. {
  877. reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK,
  878. GITS_BASER_SHAREABILITY_SHIFT,
  879. vgic_sanitise_shareability);
  880. reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK,
  881. GITS_BASER_INNER_CACHEABILITY_SHIFT,
  882. vgic_sanitise_inner_cacheability);
  883. reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK,
  884. GITS_BASER_OUTER_CACHEABILITY_SHIFT,
  885. vgic_sanitise_outer_cacheability);
  886. /* Bits 15:12 contain bits 51:48 of the PA, which we don't support. */
  887. reg &= ~GENMASK_ULL(15, 12);
  888. /* We support only one (ITS) page size: 64K */
  889. reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K;
  890. return reg;
  891. }
  892. static u64 vgic_sanitise_its_cbaser(u64 reg)
  893. {
  894. reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK,
  895. GITS_CBASER_SHAREABILITY_SHIFT,
  896. vgic_sanitise_shareability);
  897. reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK,
  898. GITS_CBASER_INNER_CACHEABILITY_SHIFT,
  899. vgic_sanitise_inner_cacheability);
  900. reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK,
  901. GITS_CBASER_OUTER_CACHEABILITY_SHIFT,
  902. vgic_sanitise_outer_cacheability);
  903. /*
  904. * Sanitise the physical address to be 64k aligned.
  905. * Also limit the physical addresses to 48 bits.
  906. */
  907. reg &= ~(GENMASK_ULL(51, 48) | GENMASK_ULL(15, 12));
  908. return reg;
  909. }
  910. static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm,
  911. struct vgic_its *its,
  912. gpa_t addr, unsigned int len)
  913. {
  914. return extract_bytes(its->cbaser, addr & 7, len);
  915. }
  916. static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its,
  917. gpa_t addr, unsigned int len,
  918. unsigned long val)
  919. {
  920. /* When GITS_CTLR.Enable is 1, this register is RO. */
  921. if (its->enabled)
  922. return;
  923. mutex_lock(&its->cmd_lock);
  924. its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val);
  925. its->cbaser = vgic_sanitise_its_cbaser(its->cbaser);
  926. its->creadr = 0;
  927. /*
  928. * CWRITER is architecturally UNKNOWN on reset, but we need to reset
  929. * it to CREADR to make sure we start with an empty command buffer.
  930. */
  931. its->cwriter = its->creadr;
  932. mutex_unlock(&its->cmd_lock);
  933. }
  934. #define ITS_CMD_BUFFER_SIZE(baser) ((((baser) & 0xff) + 1) << 12)
  935. #define ITS_CMD_SIZE 32
  936. #define ITS_CMD_OFFSET(reg) ((reg) & GENMASK(19, 5))
  937. /* Must be called with the cmd_lock held. */
  938. static void vgic_its_process_commands(struct kvm *kvm, struct vgic_its *its)
  939. {
  940. gpa_t cbaser;
  941. u64 cmd_buf[4];
  942. /* Commands are only processed when the ITS is enabled. */
  943. if (!its->enabled)
  944. return;
  945. cbaser = CBASER_ADDRESS(its->cbaser);
  946. while (its->cwriter != its->creadr) {
  947. int ret = kvm_read_guest_lock(kvm, cbaser + its->creadr,
  948. cmd_buf, ITS_CMD_SIZE);
  949. /*
  950. * If kvm_read_guest() fails, this could be due to the guest
  951. * programming a bogus value in CBASER or something else going
  952. * wrong from which we cannot easily recover.
  953. * According to section 6.3.2 in the GICv3 spec we can just
  954. * ignore that command then.
  955. */
  956. if (!ret)
  957. vgic_its_handle_command(kvm, its, cmd_buf);
  958. its->creadr += ITS_CMD_SIZE;
  959. if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser))
  960. its->creadr = 0;
  961. }
  962. }
  963. /*
  964. * By writing to CWRITER the guest announces new commands to be processed.
  965. * To avoid any races in the first place, we take the its_cmd lock, which
  966. * protects our ring buffer variables, so that there is only one user
  967. * per ITS handling commands at a given time.
  968. */
  969. static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its,
  970. gpa_t addr, unsigned int len,
  971. unsigned long val)
  972. {
  973. u64 reg;
  974. if (!its)
  975. return;
  976. mutex_lock(&its->cmd_lock);
  977. reg = update_64bit_reg(its->cwriter, addr & 7, len, val);
  978. reg = ITS_CMD_OFFSET(reg);
  979. if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
  980. mutex_unlock(&its->cmd_lock);
  981. return;
  982. }
  983. its->cwriter = reg;
  984. vgic_its_process_commands(kvm, its);
  985. mutex_unlock(&its->cmd_lock);
  986. }
  987. static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm,
  988. struct vgic_its *its,
  989. gpa_t addr, unsigned int len)
  990. {
  991. return extract_bytes(its->cwriter, addr & 0x7, len);
  992. }
  993. static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,
  994. struct vgic_its *its,
  995. gpa_t addr, unsigned int len)
  996. {
  997. return extract_bytes(its->creadr, addr & 0x7, len);
  998. }
  999. #define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7)
  1000. static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm,
  1001. struct vgic_its *its,
  1002. gpa_t addr, unsigned int len)
  1003. {
  1004. u64 reg;
  1005. switch (BASER_INDEX(addr)) {
  1006. case 0:
  1007. reg = its->baser_device_table;
  1008. break;
  1009. case 1:
  1010. reg = its->baser_coll_table;
  1011. break;
  1012. default:
  1013. reg = 0;
  1014. break;
  1015. }
  1016. return extract_bytes(reg, addr & 7, len);
  1017. }
  1018. #define GITS_BASER_RO_MASK (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56))
  1019. static void vgic_mmio_write_its_baser(struct kvm *kvm,
  1020. struct vgic_its *its,
  1021. gpa_t addr, unsigned int len,
  1022. unsigned long val)
  1023. {
  1024. u64 entry_size, device_type;
  1025. u64 reg, *regptr, clearbits = 0;
  1026. /* When GITS_CTLR.Enable is 1, we ignore write accesses. */
  1027. if (its->enabled)
  1028. return;
  1029. switch (BASER_INDEX(addr)) {
  1030. case 0:
  1031. regptr = &its->baser_device_table;
  1032. entry_size = 8;
  1033. device_type = GITS_BASER_TYPE_DEVICE;
  1034. break;
  1035. case 1:
  1036. regptr = &its->baser_coll_table;
  1037. entry_size = 8;
  1038. device_type = GITS_BASER_TYPE_COLLECTION;
  1039. clearbits = GITS_BASER_INDIRECT;
  1040. break;
  1041. default:
  1042. return;
  1043. }
  1044. reg = update_64bit_reg(*regptr, addr & 7, len, val);
  1045. reg &= ~GITS_BASER_RO_MASK;
  1046. reg &= ~clearbits;
  1047. reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
  1048. reg |= device_type << GITS_BASER_TYPE_SHIFT;
  1049. reg = vgic_sanitise_its_baser(reg);
  1050. *regptr = reg;
  1051. }
  1052. static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu,
  1053. struct vgic_its *its,
  1054. gpa_t addr, unsigned int len)
  1055. {
  1056. u32 reg = 0;
  1057. mutex_lock(&its->cmd_lock);
  1058. if (its->creadr == its->cwriter)
  1059. reg |= GITS_CTLR_QUIESCENT;
  1060. if (its->enabled)
  1061. reg |= GITS_CTLR_ENABLE;
  1062. mutex_unlock(&its->cmd_lock);
  1063. return reg;
  1064. }
  1065. static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its,
  1066. gpa_t addr, unsigned int len,
  1067. unsigned long val)
  1068. {
  1069. mutex_lock(&its->cmd_lock);
  1070. its->enabled = !!(val & GITS_CTLR_ENABLE);
  1071. /*
  1072. * Try to process any pending commands. This function bails out early
  1073. * if the ITS is disabled or no commands have been queued.
  1074. */
  1075. vgic_its_process_commands(kvm, its);
  1076. mutex_unlock(&its->cmd_lock);
  1077. }
  1078. #define REGISTER_ITS_DESC(off, rd, wr, length, acc) \
  1079. { \
  1080. .reg_offset = off, \
  1081. .len = length, \
  1082. .access_flags = acc, \
  1083. .its_read = rd, \
  1084. .its_write = wr, \
  1085. }
  1086. static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its,
  1087. gpa_t addr, unsigned int len, unsigned long val)
  1088. {
  1089. /* Ignore */
  1090. }
  1091. static struct vgic_register_region its_registers[] = {
  1092. REGISTER_ITS_DESC(GITS_CTLR,
  1093. vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4,
  1094. VGIC_ACCESS_32bit),
  1095. REGISTER_ITS_DESC(GITS_IIDR,
  1096. vgic_mmio_read_its_iidr, its_mmio_write_wi, 4,
  1097. VGIC_ACCESS_32bit),
  1098. REGISTER_ITS_DESC(GITS_TYPER,
  1099. vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
  1100. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  1101. REGISTER_ITS_DESC(GITS_CBASER,
  1102. vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8,
  1103. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  1104. REGISTER_ITS_DESC(GITS_CWRITER,
  1105. vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8,
  1106. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  1107. REGISTER_ITS_DESC(GITS_CREADR,
  1108. vgic_mmio_read_its_creadr, its_mmio_write_wi, 8,
  1109. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  1110. REGISTER_ITS_DESC(GITS_BASER,
  1111. vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
  1112. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  1113. REGISTER_ITS_DESC(GITS_IDREGS_BASE,
  1114. vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30,
  1115. VGIC_ACCESS_32bit),
  1116. };
  1117. /* This is called on setting the LPI enable bit in the redistributor. */
  1118. void vgic_enable_lpis(struct kvm_vcpu *vcpu)
  1119. {
  1120. if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ))
  1121. its_sync_lpi_pending_table(vcpu);
  1122. }
  1123. static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its)
  1124. {
  1125. struct vgic_io_device *iodev = &its->iodev;
  1126. int ret;
  1127. if (!its->initialized)
  1128. return -EBUSY;
  1129. if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base))
  1130. return -ENXIO;
  1131. iodev->regions = its_registers;
  1132. iodev->nr_regions = ARRAY_SIZE(its_registers);
  1133. kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops);
  1134. iodev->base_addr = its->vgic_its_base;
  1135. iodev->iodev_type = IODEV_ITS;
  1136. iodev->its = its;
  1137. mutex_lock(&kvm->slots_lock);
  1138. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr,
  1139. KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
  1140. mutex_unlock(&kvm->slots_lock);
  1141. return ret;
  1142. }
  1143. #define INITIAL_BASER_VALUE \
  1144. (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \
  1145. GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \
  1146. GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \
  1147. ((8ULL - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) | \
  1148. GITS_BASER_PAGE_SIZE_64K)
  1149. #define INITIAL_PROPBASER_VALUE \
  1150. (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb) | \
  1151. GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner) | \
  1152. GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable))
  1153. static int vgic_its_create(struct kvm_device *dev, u32 type)
  1154. {
  1155. struct vgic_its *its;
  1156. if (type != KVM_DEV_TYPE_ARM_VGIC_ITS)
  1157. return -ENODEV;
  1158. its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL);
  1159. if (!its)
  1160. return -ENOMEM;
  1161. mutex_init(&its->its_lock);
  1162. mutex_init(&its->cmd_lock);
  1163. its->vgic_its_base = VGIC_ADDR_UNDEF;
  1164. INIT_LIST_HEAD(&its->device_list);
  1165. INIT_LIST_HEAD(&its->collection_list);
  1166. dev->kvm->arch.vgic.has_its = true;
  1167. its->initialized = false;
  1168. its->enabled = false;
  1169. its->dev = dev;
  1170. its->baser_device_table = INITIAL_BASER_VALUE |
  1171. ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT);
  1172. its->baser_coll_table = INITIAL_BASER_VALUE |
  1173. ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT);
  1174. dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE;
  1175. dev->private = its;
  1176. return 0;
  1177. }
  1178. static void vgic_its_destroy(struct kvm_device *kvm_dev)
  1179. {
  1180. struct kvm *kvm = kvm_dev->kvm;
  1181. struct vgic_its *its = kvm_dev->private;
  1182. struct its_device *dev;
  1183. struct its_itte *itte;
  1184. struct list_head *dev_cur, *dev_temp;
  1185. struct list_head *cur, *temp;
  1186. /*
  1187. * We may end up here without the lists ever having been initialized.
  1188. * Check this and bail out early to avoid dereferencing a NULL pointer.
  1189. */
  1190. if (!its->device_list.next)
  1191. return;
  1192. mutex_lock(&its->its_lock);
  1193. list_for_each_safe(dev_cur, dev_temp, &its->device_list) {
  1194. dev = container_of(dev_cur, struct its_device, dev_list);
  1195. list_for_each_safe(cur, temp, &dev->itt_head) {
  1196. itte = (container_of(cur, struct its_itte, itte_list));
  1197. its_free_itte(kvm, itte);
  1198. }
  1199. list_del(dev_cur);
  1200. kfree(dev);
  1201. }
  1202. list_for_each_safe(cur, temp, &its->collection_list) {
  1203. list_del(cur);
  1204. kfree(container_of(cur, struct its_collection, coll_list));
  1205. }
  1206. mutex_unlock(&its->its_lock);
  1207. kfree(its);
  1208. }
  1209. static int vgic_its_has_attr(struct kvm_device *dev,
  1210. struct kvm_device_attr *attr)
  1211. {
  1212. switch (attr->group) {
  1213. case KVM_DEV_ARM_VGIC_GRP_ADDR:
  1214. switch (attr->attr) {
  1215. case KVM_VGIC_ITS_ADDR_TYPE:
  1216. return 0;
  1217. }
  1218. break;
  1219. case KVM_DEV_ARM_VGIC_GRP_CTRL:
  1220. switch (attr->attr) {
  1221. case KVM_DEV_ARM_VGIC_CTRL_INIT:
  1222. return 0;
  1223. }
  1224. break;
  1225. }
  1226. return -ENXIO;
  1227. }
  1228. static int vgic_its_set_attr(struct kvm_device *dev,
  1229. struct kvm_device_attr *attr)
  1230. {
  1231. struct vgic_its *its = dev->private;
  1232. int ret;
  1233. switch (attr->group) {
  1234. case KVM_DEV_ARM_VGIC_GRP_ADDR: {
  1235. u64 __user *uaddr = (u64 __user *)(long)attr->addr;
  1236. unsigned long type = (unsigned long)attr->attr;
  1237. u64 addr;
  1238. if (type != KVM_VGIC_ITS_ADDR_TYPE)
  1239. return -ENODEV;
  1240. if (copy_from_user(&addr, uaddr, sizeof(addr)))
  1241. return -EFAULT;
  1242. ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base,
  1243. addr, SZ_64K);
  1244. if (ret)
  1245. return ret;
  1246. its->vgic_its_base = addr;
  1247. return 0;
  1248. }
  1249. case KVM_DEV_ARM_VGIC_GRP_CTRL:
  1250. switch (attr->attr) {
  1251. case KVM_DEV_ARM_VGIC_CTRL_INIT:
  1252. its->initialized = true;
  1253. return 0;
  1254. }
  1255. break;
  1256. }
  1257. return -ENXIO;
  1258. }
  1259. static int vgic_its_get_attr(struct kvm_device *dev,
  1260. struct kvm_device_attr *attr)
  1261. {
  1262. switch (attr->group) {
  1263. case KVM_DEV_ARM_VGIC_GRP_ADDR: {
  1264. struct vgic_its *its = dev->private;
  1265. u64 addr = its->vgic_its_base;
  1266. u64 __user *uaddr = (u64 __user *)(long)attr->addr;
  1267. unsigned long type = (unsigned long)attr->attr;
  1268. if (type != KVM_VGIC_ITS_ADDR_TYPE)
  1269. return -ENODEV;
  1270. if (copy_to_user(uaddr, &addr, sizeof(addr)))
  1271. return -EFAULT;
  1272. break;
  1273. default:
  1274. return -ENXIO;
  1275. }
  1276. }
  1277. return 0;
  1278. }
  1279. static struct kvm_device_ops kvm_arm_vgic_its_ops = {
  1280. .name = "kvm-arm-vgic-its",
  1281. .create = vgic_its_create,
  1282. .destroy = vgic_its_destroy,
  1283. .set_attr = vgic_its_set_attr,
  1284. .get_attr = vgic_its_get_attr,
  1285. .has_attr = vgic_its_has_attr,
  1286. };
  1287. int kvm_vgic_register_its_device(void)
  1288. {
  1289. return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
  1290. KVM_DEV_TYPE_ARM_VGIC_ITS);
  1291. }
  1292. /*
  1293. * Registers all ITSes with the kvm_io_bus framework.
  1294. * To follow the existing VGIC initialization sequence, this has to be
  1295. * done as late as possible, just before the first VCPU runs.
  1296. */
  1297. int vgic_register_its_iodevs(struct kvm *kvm)
  1298. {
  1299. struct kvm_device *dev;
  1300. int ret = 0;
  1301. list_for_each_entry(dev, &kvm->devices, vm_node) {
  1302. if (dev->ops != &kvm_arm_vgic_its_ops)
  1303. continue;
  1304. ret = vgic_register_its_iodev(kvm, dev->private);
  1305. if (ret)
  1306. return ret;
  1307. /*
  1308. * We don't need to care about tearing down previously
  1309. * registered ITSes, as the kvm_io_bus framework removes
  1310. * them for us if the VM gets destroyed.
  1311. */
  1312. }
  1313. return ret;
  1314. }