vgic-its.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * GICv3 ITS emulation
  4. *
  5. * Copyright (C) 2015,2016 ARM Ltd.
  6. * Author: Andre Przywara <andre.przywara@arm.com>
  7. */
  8. #include <linux/cpu.h>
  9. #include <linux/kvm.h>
  10. #include <linux/kvm_host.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/list.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/list_sort.h>
  15. #include <linux/irqchip/arm-gic-v3.h>
  16. #include <asm/kvm_emulate.h>
  17. #include <asm/kvm_arm.h>
  18. #include <asm/kvm_mmu.h>
  19. #include "vgic.h"
  20. #include "vgic-mmio.h"
  21. static struct kvm_device_ops kvm_arm_vgic_its_ops;
  22. static int vgic_its_save_tables_v0(struct vgic_its *its);
  23. static int vgic_its_restore_tables_v0(struct vgic_its *its);
  24. static int vgic_its_commit_v0(struct vgic_its *its);
  25. static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
  26. struct kvm_vcpu *filter_vcpu, bool needs_inv);
  27. /*
  28. * Creates a new (reference to a) struct vgic_irq for a given LPI.
  29. * If this LPI is already mapped on another ITS, we increase its refcount
  30. * and return a pointer to the existing structure.
  31. * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq.
  32. * This function returns a pointer to the _unlocked_ structure.
  33. */
  34. static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid,
  35. struct kvm_vcpu *vcpu)
  36. {
  37. struct vgic_dist *dist = &kvm->arch.vgic;
  38. struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq;
  39. unsigned long flags;
  40. int ret;
  41. /* In this case there is no put, since we keep the reference. */
  42. if (irq)
  43. return irq;
  44. irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL_ACCOUNT);
  45. if (!irq)
  46. return ERR_PTR(-ENOMEM);
  47. ret = xa_reserve_irq(&dist->lpi_xa, intid, GFP_KERNEL_ACCOUNT);
  48. if (ret) {
  49. kfree(irq);
  50. return ERR_PTR(ret);
  51. }
  52. INIT_LIST_HEAD(&irq->ap_list);
  53. raw_spin_lock_init(&irq->irq_lock);
  54. irq->config = VGIC_CONFIG_EDGE;
  55. kref_init(&irq->refcount);
  56. irq->intid = intid;
  57. irq->target_vcpu = vcpu;
  58. irq->group = 1;
  59. xa_lock_irqsave(&dist->lpi_xa, flags);
  60. /*
  61. * There could be a race with another vgic_add_lpi(), so we need to
  62. * check that we don't add a second list entry with the same LPI.
  63. */
  64. oldirq = xa_load(&dist->lpi_xa, intid);
  65. if (vgic_try_get_irq_kref(oldirq)) {
  66. /* Someone was faster with adding this LPI, lets use that. */
  67. kfree(irq);
  68. irq = oldirq;
  69. goto out_unlock;
  70. }
  71. ret = xa_err(__xa_store(&dist->lpi_xa, intid, irq, 0));
  72. if (ret) {
  73. xa_release(&dist->lpi_xa, intid);
  74. kfree(irq);
  75. }
  76. out_unlock:
  77. xa_unlock_irqrestore(&dist->lpi_xa, flags);
  78. if (ret)
  79. return ERR_PTR(ret);
  80. /*
  81. * We "cache" the configuration table entries in our struct vgic_irq's.
  82. * However we only have those structs for mapped IRQs, so we read in
  83. * the respective config data from memory here upon mapping the LPI.
  84. *
  85. * Should any of these fail, behave as if we couldn't create the LPI
  86. * by dropping the refcount and returning the error.
  87. */
  88. ret = update_lpi_config(kvm, irq, NULL, false);
  89. if (ret) {
  90. vgic_put_irq(kvm, irq);
  91. return ERR_PTR(ret);
  92. }
  93. ret = vgic_v3_lpi_sync_pending_status(kvm, irq);
  94. if (ret) {
  95. vgic_put_irq(kvm, irq);
  96. return ERR_PTR(ret);
  97. }
  98. return irq;
  99. }
  100. struct its_device {
  101. struct list_head dev_list;
  102. /* the head for the list of ITTEs */
  103. struct list_head itt_head;
  104. u32 num_eventid_bits;
  105. gpa_t itt_addr;
  106. u32 device_id;
  107. };
  108. #define COLLECTION_NOT_MAPPED ((u32)~0)
  109. struct its_collection {
  110. struct list_head coll_list;
  111. u32 collection_id;
  112. u32 target_addr;
  113. };
  114. #define its_is_collection_mapped(coll) ((coll) && \
  115. ((coll)->target_addr != COLLECTION_NOT_MAPPED))
  116. struct its_ite {
  117. struct list_head ite_list;
  118. struct vgic_irq *irq;
  119. struct its_collection *collection;
  120. u32 event_id;
  121. };
  122. /**
  123. * struct vgic_its_abi - ITS abi ops and settings
  124. * @cte_esz: collection table entry size
  125. * @dte_esz: device table entry size
  126. * @ite_esz: interrupt translation table entry size
  127. * @save_tables: save the ITS tables into guest RAM
  128. * @restore_tables: restore the ITS internal structs from tables
  129. * stored in guest RAM
  130. * @commit: initialize the registers which expose the ABI settings,
  131. * especially the entry sizes
  132. */
  133. struct vgic_its_abi {
  134. int cte_esz;
  135. int dte_esz;
  136. int ite_esz;
  137. int (*save_tables)(struct vgic_its *its);
  138. int (*restore_tables)(struct vgic_its *its);
  139. int (*commit)(struct vgic_its *its);
  140. };
  141. #define ABI_0_ESZ 8
  142. #define ESZ_MAX ABI_0_ESZ
  143. static const struct vgic_its_abi its_table_abi_versions[] = {
  144. [0] = {
  145. .cte_esz = ABI_0_ESZ,
  146. .dte_esz = ABI_0_ESZ,
  147. .ite_esz = ABI_0_ESZ,
  148. .save_tables = vgic_its_save_tables_v0,
  149. .restore_tables = vgic_its_restore_tables_v0,
  150. .commit = vgic_its_commit_v0,
  151. },
  152. };
  153. #define NR_ITS_ABIS ARRAY_SIZE(its_table_abi_versions)
  154. inline const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its)
  155. {
  156. return &its_table_abi_versions[its->abi_rev];
  157. }
  158. static int vgic_its_set_abi(struct vgic_its *its, u32 rev)
  159. {
  160. const struct vgic_its_abi *abi;
  161. its->abi_rev = rev;
  162. abi = vgic_its_get_abi(its);
  163. return abi->commit(its);
  164. }
  165. /*
  166. * Find and returns a device in the device table for an ITS.
  167. * Must be called with the its_lock mutex held.
  168. */
  169. static struct its_device *find_its_device(struct vgic_its *its, u32 device_id)
  170. {
  171. struct its_device *device;
  172. list_for_each_entry(device, &its->device_list, dev_list)
  173. if (device_id == device->device_id)
  174. return device;
  175. return NULL;
  176. }
  177. /*
  178. * Find and returns an interrupt translation table entry (ITTE) for a given
  179. * Device ID/Event ID pair on an ITS.
  180. * Must be called with the its_lock mutex held.
  181. */
  182. static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,
  183. u32 event_id)
  184. {
  185. struct its_device *device;
  186. struct its_ite *ite;
  187. device = find_its_device(its, device_id);
  188. if (device == NULL)
  189. return NULL;
  190. list_for_each_entry(ite, &device->itt_head, ite_list)
  191. if (ite->event_id == event_id)
  192. return ite;
  193. return NULL;
  194. }
  195. /* To be used as an iterator this macro misses the enclosing parentheses */
  196. #define for_each_lpi_its(dev, ite, its) \
  197. list_for_each_entry(dev, &(its)->device_list, dev_list) \
  198. list_for_each_entry(ite, &(dev)->itt_head, ite_list)
  199. #define GIC_LPI_OFFSET 8192
  200. #define VITS_TYPER_IDBITS 16
  201. #define VITS_MAX_EVENTID (BIT(VITS_TYPER_IDBITS) - 1)
  202. #define VITS_TYPER_DEVBITS 16
  203. #define VITS_MAX_DEVID (BIT(VITS_TYPER_DEVBITS) - 1)
  204. #define VITS_DTE_MAX_DEVID_OFFSET (BIT(14) - 1)
  205. #define VITS_ITE_MAX_EVENTID_OFFSET (BIT(16) - 1)
  206. /*
  207. * Finds and returns a collection in the ITS collection table.
  208. * Must be called with the its_lock mutex held.
  209. */
  210. static struct its_collection *find_collection(struct vgic_its *its, int coll_id)
  211. {
  212. struct its_collection *collection;
  213. list_for_each_entry(collection, &its->collection_list, coll_list) {
  214. if (coll_id == collection->collection_id)
  215. return collection;
  216. }
  217. return NULL;
  218. }
  219. #define LPI_PROP_ENABLE_BIT(p) ((p) & LPI_PROP_ENABLED)
  220. #define LPI_PROP_PRIORITY(p) ((p) & 0xfc)
  221. /*
  222. * Reads the configuration data for a given LPI from guest memory and
  223. * updates the fields in struct vgic_irq.
  224. * If filter_vcpu is not NULL, applies only if the IRQ is targeting this
  225. * VCPU. Unconditionally applies if filter_vcpu is NULL.
  226. */
  227. static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
  228. struct kvm_vcpu *filter_vcpu, bool needs_inv)
  229. {
  230. u64 propbase = GICR_PROPBASER_ADDRESS(kvm->arch.vgic.propbaser);
  231. u8 prop;
  232. int ret;
  233. unsigned long flags;
  234. ret = kvm_read_guest_lock(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
  235. &prop, 1);
  236. if (ret)
  237. return ret;
  238. raw_spin_lock_irqsave(&irq->irq_lock, flags);
  239. if (!filter_vcpu || filter_vcpu == irq->target_vcpu) {
  240. irq->priority = LPI_PROP_PRIORITY(prop);
  241. irq->enabled = LPI_PROP_ENABLE_BIT(prop);
  242. if (!irq->hw) {
  243. vgic_queue_irq_unlock(kvm, irq, flags);
  244. return 0;
  245. }
  246. }
  247. raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
  248. if (irq->hw)
  249. return its_prop_update_vlpi(irq->host_irq, prop, needs_inv);
  250. return 0;
  251. }
  252. static int update_affinity(struct vgic_irq *irq, struct kvm_vcpu *vcpu)
  253. {
  254. int ret = 0;
  255. unsigned long flags;
  256. raw_spin_lock_irqsave(&irq->irq_lock, flags);
  257. irq->target_vcpu = vcpu;
  258. raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
  259. if (irq->hw) {
  260. struct its_vlpi_map map;
  261. ret = its_get_vlpi(irq->host_irq, &map);
  262. if (ret)
  263. return ret;
  264. if (map.vpe)
  265. atomic_dec(&map.vpe->vlpi_count);
  266. map.vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
  267. atomic_inc(&map.vpe->vlpi_count);
  268. ret = its_map_vlpi(irq->host_irq, &map);
  269. }
  270. return ret;
  271. }
  272. static struct kvm_vcpu *collection_to_vcpu(struct kvm *kvm,
  273. struct its_collection *col)
  274. {
  275. return kvm_get_vcpu_by_id(kvm, col->target_addr);
  276. }
  277. /*
  278. * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI
  279. * is targeting) to the VGIC's view, which deals with target VCPUs.
  280. * Needs to be called whenever either the collection for a LPIs has
  281. * changed or the collection itself got retargeted.
  282. */
  283. static void update_affinity_ite(struct kvm *kvm, struct its_ite *ite)
  284. {
  285. struct kvm_vcpu *vcpu;
  286. if (!its_is_collection_mapped(ite->collection))
  287. return;
  288. vcpu = collection_to_vcpu(kvm, ite->collection);
  289. update_affinity(ite->irq, vcpu);
  290. }
  291. /*
  292. * Updates the target VCPU for every LPI targeting this collection.
  293. * Must be called with the its_lock mutex held.
  294. */
  295. static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its,
  296. struct its_collection *coll)
  297. {
  298. struct its_device *device;
  299. struct its_ite *ite;
  300. for_each_lpi_its(device, ite, its) {
  301. if (ite->collection != coll)
  302. continue;
  303. update_affinity_ite(kvm, ite);
  304. }
  305. }
  306. static u32 max_lpis_propbaser(u64 propbaser)
  307. {
  308. int nr_idbits = (propbaser & 0x1f) + 1;
  309. return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS);
  310. }
  311. /*
  312. * Sync the pending table pending bit of LPIs targeting @vcpu
  313. * with our own data structures. This relies on the LPI being
  314. * mapped before.
  315. */
  316. static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
  317. {
  318. gpa_t pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
  319. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  320. unsigned long intid, flags;
  321. struct vgic_irq *irq;
  322. int last_byte_offset = -1;
  323. int ret = 0;
  324. u8 pendmask;
  325. xa_for_each(&dist->lpi_xa, intid, irq) {
  326. int byte_offset, bit_nr;
  327. byte_offset = intid / BITS_PER_BYTE;
  328. bit_nr = intid % BITS_PER_BYTE;
  329. /*
  330. * For contiguously allocated LPIs chances are we just read
  331. * this very same byte in the last iteration. Reuse that.
  332. */
  333. if (byte_offset != last_byte_offset) {
  334. ret = kvm_read_guest_lock(vcpu->kvm,
  335. pendbase + byte_offset,
  336. &pendmask, 1);
  337. if (ret)
  338. return ret;
  339. last_byte_offset = byte_offset;
  340. }
  341. irq = vgic_get_irq(vcpu->kvm, NULL, intid);
  342. if (!irq)
  343. continue;
  344. raw_spin_lock_irqsave(&irq->irq_lock, flags);
  345. if (irq->target_vcpu == vcpu)
  346. irq->pending_latch = pendmask & (1U << bit_nr);
  347. vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
  348. vgic_put_irq(vcpu->kvm, irq);
  349. }
  350. return ret;
  351. }
  352. static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,
  353. struct vgic_its *its,
  354. gpa_t addr, unsigned int len)
  355. {
  356. const struct vgic_its_abi *abi = vgic_its_get_abi(its);
  357. u64 reg = GITS_TYPER_PLPIS;
  358. /*
  359. * We use linear CPU numbers for redistributor addressing,
  360. * so GITS_TYPER.PTA is 0.
  361. * Also we force all PROPBASER registers to be the same, so
  362. * CommonLPIAff is 0 as well.
  363. * To avoid memory waste in the guest, we keep the number of IDBits and
  364. * DevBits low - as least for the time being.
  365. */
  366. reg |= GIC_ENCODE_SZ(VITS_TYPER_DEVBITS, 5) << GITS_TYPER_DEVBITS_SHIFT;
  367. reg |= GIC_ENCODE_SZ(VITS_TYPER_IDBITS, 5) << GITS_TYPER_IDBITS_SHIFT;
  368. reg |= GIC_ENCODE_SZ(abi->ite_esz, 4) << GITS_TYPER_ITT_ENTRY_SIZE_SHIFT;
  369. return extract_bytes(reg, addr & 7, len);
  370. }
  371. static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,
  372. struct vgic_its *its,
  373. gpa_t addr, unsigned int len)
  374. {
  375. u32 val;
  376. val = (its->abi_rev << GITS_IIDR_REV_SHIFT) & GITS_IIDR_REV_MASK;
  377. val |= (PRODUCT_ID_KVM << GITS_IIDR_PRODUCTID_SHIFT) | IMPLEMENTER_ARM;
  378. return val;
  379. }
  380. static int vgic_mmio_uaccess_write_its_iidr(struct kvm *kvm,
  381. struct vgic_its *its,
  382. gpa_t addr, unsigned int len,
  383. unsigned long val)
  384. {
  385. u32 rev = GITS_IIDR_REV(val);
  386. if (rev >= NR_ITS_ABIS)
  387. return -EINVAL;
  388. return vgic_its_set_abi(its, rev);
  389. }
  390. static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
  391. struct vgic_its *its,
  392. gpa_t addr, unsigned int len)
  393. {
  394. switch (addr & 0xffff) {
  395. case GITS_PIDR0:
  396. return 0x92; /* part number, bits[7:0] */
  397. case GITS_PIDR1:
  398. return 0xb4; /* part number, bits[11:8] */
  399. case GITS_PIDR2:
  400. return GIC_PIDR2_ARCH_GICv3 | 0x0b;
  401. case GITS_PIDR4:
  402. return 0x40; /* This is a 64K software visible page */
  403. /* The following are the ID registers for (any) GIC. */
  404. case GITS_CIDR0:
  405. return 0x0d;
  406. case GITS_CIDR1:
  407. return 0xf0;
  408. case GITS_CIDR2:
  409. return 0x05;
  410. case GITS_CIDR3:
  411. return 0xb1;
  412. }
  413. return 0;
  414. }
  415. static struct vgic_its *__vgic_doorbell_to_its(struct kvm *kvm, gpa_t db)
  416. {
  417. struct kvm_io_device *kvm_io_dev;
  418. struct vgic_io_device *iodev;
  419. kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, db);
  420. if (!kvm_io_dev)
  421. return ERR_PTR(-EINVAL);
  422. if (kvm_io_dev->ops != &kvm_io_gic_ops)
  423. return ERR_PTR(-EINVAL);
  424. iodev = container_of(kvm_io_dev, struct vgic_io_device, dev);
  425. if (iodev->iodev_type != IODEV_ITS)
  426. return ERR_PTR(-EINVAL);
  427. return iodev->its;
  428. }
  429. static unsigned long vgic_its_cache_key(u32 devid, u32 eventid)
  430. {
  431. return (((unsigned long)devid) << VITS_TYPER_IDBITS) | eventid;
  432. }
  433. static struct vgic_irq *vgic_its_check_cache(struct kvm *kvm, phys_addr_t db,
  434. u32 devid, u32 eventid)
  435. {
  436. unsigned long cache_key = vgic_its_cache_key(devid, eventid);
  437. struct vgic_its *its;
  438. struct vgic_irq *irq;
  439. if (devid > VITS_MAX_DEVID || eventid > VITS_MAX_EVENTID)
  440. return NULL;
  441. its = __vgic_doorbell_to_its(kvm, db);
  442. if (IS_ERR(its))
  443. return NULL;
  444. rcu_read_lock();
  445. irq = xa_load(&its->translation_cache, cache_key);
  446. if (!vgic_try_get_irq_kref(irq))
  447. irq = NULL;
  448. rcu_read_unlock();
  449. return irq;
  450. }
  451. static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its,
  452. u32 devid, u32 eventid,
  453. struct vgic_irq *irq)
  454. {
  455. unsigned long cache_key = vgic_its_cache_key(devid, eventid);
  456. struct vgic_irq *old;
  457. /* Do not cache a directly injected interrupt */
  458. if (irq->hw)
  459. return;
  460. /*
  461. * The irq refcount is guaranteed to be nonzero while holding the
  462. * its_lock, as the ITE (and the reference it holds) cannot be freed.
  463. */
  464. lockdep_assert_held(&its->its_lock);
  465. vgic_get_irq_kref(irq);
  466. /*
  467. * We could have raced with another CPU caching the same
  468. * translation behind our back, ensure we don't leak a
  469. * reference if that is the case.
  470. */
  471. old = xa_store(&its->translation_cache, cache_key, irq, GFP_KERNEL_ACCOUNT);
  472. if (old)
  473. vgic_put_irq(kvm, old);
  474. }
  475. static void vgic_its_invalidate_cache(struct vgic_its *its)
  476. {
  477. struct kvm *kvm = its->dev->kvm;
  478. struct vgic_irq *irq;
  479. unsigned long idx;
  480. xa_for_each(&its->translation_cache, idx, irq) {
  481. xa_erase(&its->translation_cache, idx);
  482. vgic_put_irq(kvm, irq);
  483. }
  484. }
  485. void vgic_its_invalidate_all_caches(struct kvm *kvm)
  486. {
  487. struct kvm_device *dev;
  488. struct vgic_its *its;
  489. rcu_read_lock();
  490. list_for_each_entry_rcu(dev, &kvm->devices, vm_node) {
  491. if (dev->ops != &kvm_arm_vgic_its_ops)
  492. continue;
  493. its = dev->private;
  494. vgic_its_invalidate_cache(its);
  495. }
  496. rcu_read_unlock();
  497. }
  498. int vgic_its_resolve_lpi(struct kvm *kvm, struct vgic_its *its,
  499. u32 devid, u32 eventid, struct vgic_irq **irq)
  500. {
  501. struct kvm_vcpu *vcpu;
  502. struct its_ite *ite;
  503. if (!its->enabled)
  504. return -EBUSY;
  505. ite = find_ite(its, devid, eventid);
  506. if (!ite || !its_is_collection_mapped(ite->collection))
  507. return E_ITS_INT_UNMAPPED_INTERRUPT;
  508. vcpu = collection_to_vcpu(kvm, ite->collection);
  509. if (!vcpu)
  510. return E_ITS_INT_UNMAPPED_INTERRUPT;
  511. if (!vgic_lpis_enabled(vcpu))
  512. return -EBUSY;
  513. vgic_its_cache_translation(kvm, its, devid, eventid, ite->irq);
  514. *irq = ite->irq;
  515. return 0;
  516. }
  517. struct vgic_its *vgic_msi_to_its(struct kvm *kvm, struct kvm_msi *msi)
  518. {
  519. u64 address;
  520. if (!vgic_has_its(kvm))
  521. return ERR_PTR(-ENODEV);
  522. if (!(msi->flags & KVM_MSI_VALID_DEVID))
  523. return ERR_PTR(-EINVAL);
  524. address = (u64)msi->address_hi << 32 | msi->address_lo;
  525. return __vgic_doorbell_to_its(kvm, address);
  526. }
  527. /*
  528. * Find the target VCPU and the LPI number for a given devid/eventid pair
  529. * and make this IRQ pending, possibly injecting it.
  530. * Must be called with the its_lock mutex held.
  531. * Returns 0 on success, a positive error value for any ITS mapping
  532. * related errors and negative error values for generic errors.
  533. */
  534. static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
  535. u32 devid, u32 eventid)
  536. {
  537. struct vgic_irq *irq = NULL;
  538. unsigned long flags;
  539. int err;
  540. err = vgic_its_resolve_lpi(kvm, its, devid, eventid, &irq);
  541. if (err)
  542. return err;
  543. if (irq->hw)
  544. return irq_set_irqchip_state(irq->host_irq,
  545. IRQCHIP_STATE_PENDING, true);
  546. raw_spin_lock_irqsave(&irq->irq_lock, flags);
  547. irq->pending_latch = true;
  548. vgic_queue_irq_unlock(kvm, irq, flags);
  549. return 0;
  550. }
  551. int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi)
  552. {
  553. struct vgic_irq *irq;
  554. unsigned long flags;
  555. phys_addr_t db;
  556. db = (u64)msi->address_hi << 32 | msi->address_lo;
  557. irq = vgic_its_check_cache(kvm, db, msi->devid, msi->data);
  558. if (!irq)
  559. return -EWOULDBLOCK;
  560. raw_spin_lock_irqsave(&irq->irq_lock, flags);
  561. irq->pending_latch = true;
  562. vgic_queue_irq_unlock(kvm, irq, flags);
  563. vgic_put_irq(kvm, irq);
  564. return 0;
  565. }
  566. /*
  567. * Queries the KVM IO bus framework to get the ITS pointer from the given
  568. * doorbell address.
  569. * We then call vgic_its_trigger_msi() with the decoded data.
  570. * According to the KVM_SIGNAL_MSI API description returns 1 on success.
  571. */
  572. int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
  573. {
  574. struct vgic_its *its;
  575. int ret;
  576. if (!vgic_its_inject_cached_translation(kvm, msi))
  577. return 1;
  578. its = vgic_msi_to_its(kvm, msi);
  579. if (IS_ERR(its))
  580. return PTR_ERR(its);
  581. mutex_lock(&its->its_lock);
  582. ret = vgic_its_trigger_msi(kvm, its, msi->devid, msi->data);
  583. mutex_unlock(&its->its_lock);
  584. if (ret < 0)
  585. return ret;
  586. /*
  587. * KVM_SIGNAL_MSI demands a return value > 0 for success and 0
  588. * if the guest has blocked the MSI. So we map any LPI mapping
  589. * related error to that.
  590. */
  591. if (ret)
  592. return 0;
  593. else
  594. return 1;
  595. }
  596. /* Requires the its_lock to be held. */
  597. static void its_free_ite(struct kvm *kvm, struct its_ite *ite)
  598. {
  599. list_del(&ite->ite_list);
  600. /* This put matches the get in vgic_add_lpi. */
  601. if (ite->irq) {
  602. if (ite->irq->hw)
  603. WARN_ON(its_unmap_vlpi(ite->irq->host_irq));
  604. vgic_put_irq(kvm, ite->irq);
  605. }
  606. kfree(ite);
  607. }
  608. static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)
  609. {
  610. return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1);
  611. }
  612. #define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8)
  613. #define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32)
  614. #define its_cmd_get_size(cmd) (its_cmd_mask_field(cmd, 1, 0, 5) + 1)
  615. #define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32)
  616. #define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32)
  617. #define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16)
  618. #define its_cmd_get_ittaddr(cmd) (its_cmd_mask_field(cmd, 2, 8, 44) << 8)
  619. #define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32)
  620. #define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1)
  621. /*
  622. * The DISCARD command frees an Interrupt Translation Table Entry (ITTE).
  623. * Must be called with the its_lock mutex held.
  624. */
  625. static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,
  626. u64 *its_cmd)
  627. {
  628. u32 device_id = its_cmd_get_deviceid(its_cmd);
  629. u32 event_id = its_cmd_get_id(its_cmd);
  630. struct its_ite *ite;
  631. ite = find_ite(its, device_id, event_id);
  632. if (ite && its_is_collection_mapped(ite->collection)) {
  633. struct its_device *device = find_its_device(its, device_id);
  634. int ite_esz = vgic_its_get_abi(its)->ite_esz;
  635. gpa_t gpa = device->itt_addr + ite->event_id * ite_esz;
  636. /*
  637. * Though the spec talks about removing the pending state, we
  638. * don't bother here since we clear the ITTE anyway and the
  639. * pending state is a property of the ITTE struct.
  640. */
  641. vgic_its_invalidate_cache(its);
  642. its_free_ite(kvm, ite);
  643. return vgic_its_write_entry_lock(its, gpa, 0, ite_esz);
  644. }
  645. return E_ITS_DISCARD_UNMAPPED_INTERRUPT;
  646. }
  647. /*
  648. * The MOVI command moves an ITTE to a different collection.
  649. * Must be called with the its_lock mutex held.
  650. */
  651. static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,
  652. u64 *its_cmd)
  653. {
  654. u32 device_id = its_cmd_get_deviceid(its_cmd);
  655. u32 event_id = its_cmd_get_id(its_cmd);
  656. u32 coll_id = its_cmd_get_collection(its_cmd);
  657. struct kvm_vcpu *vcpu;
  658. struct its_ite *ite;
  659. struct its_collection *collection;
  660. ite = find_ite(its, device_id, event_id);
  661. if (!ite)
  662. return E_ITS_MOVI_UNMAPPED_INTERRUPT;
  663. if (!its_is_collection_mapped(ite->collection))
  664. return E_ITS_MOVI_UNMAPPED_COLLECTION;
  665. collection = find_collection(its, coll_id);
  666. if (!its_is_collection_mapped(collection))
  667. return E_ITS_MOVI_UNMAPPED_COLLECTION;
  668. ite->collection = collection;
  669. vcpu = collection_to_vcpu(kvm, collection);
  670. vgic_its_invalidate_cache(its);
  671. return update_affinity(ite->irq, vcpu);
  672. }
  673. static bool __is_visible_gfn_locked(struct vgic_its *its, gpa_t gpa)
  674. {
  675. gfn_t gfn = gpa >> PAGE_SHIFT;
  676. int idx;
  677. bool ret;
  678. idx = srcu_read_lock(&its->dev->kvm->srcu);
  679. ret = kvm_is_visible_gfn(its->dev->kvm, gfn);
  680. srcu_read_unlock(&its->dev->kvm->srcu, idx);
  681. return ret;
  682. }
  683. /*
  684. * Check whether an ID can be stored into the corresponding guest table.
  685. * For a direct table this is pretty easy, but gets a bit nasty for
  686. * indirect tables. We check whether the resulting guest physical address
  687. * is actually valid (covered by a memslot and guest accessible).
  688. * For this we have to read the respective first level entry.
  689. */
  690. static bool vgic_its_check_id(struct vgic_its *its, u64 baser, u32 id,
  691. gpa_t *eaddr)
  692. {
  693. int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
  694. u64 indirect_ptr, type = GITS_BASER_TYPE(baser);
  695. phys_addr_t base = GITS_BASER_ADDR_48_to_52(baser);
  696. int esz = GITS_BASER_ENTRY_SIZE(baser);
  697. int index;
  698. switch (type) {
  699. case GITS_BASER_TYPE_DEVICE:
  700. if (id > VITS_MAX_DEVID)
  701. return false;
  702. break;
  703. case GITS_BASER_TYPE_COLLECTION:
  704. /* as GITS_TYPER.CIL == 0, ITS supports 16-bit collection ID */
  705. if (id >= BIT_ULL(16))
  706. return false;
  707. break;
  708. default:
  709. return false;
  710. }
  711. if (!(baser & GITS_BASER_INDIRECT)) {
  712. phys_addr_t addr;
  713. if (id >= (l1_tbl_size / esz))
  714. return false;
  715. addr = base + id * esz;
  716. if (eaddr)
  717. *eaddr = addr;
  718. return __is_visible_gfn_locked(its, addr);
  719. }
  720. /* calculate and check the index into the 1st level */
  721. index = id / (SZ_64K / esz);
  722. if (index >= (l1_tbl_size / sizeof(u64)))
  723. return false;
  724. /* Each 1st level entry is represented by a 64-bit value. */
  725. if (kvm_read_guest_lock(its->dev->kvm,
  726. base + index * sizeof(indirect_ptr),
  727. &indirect_ptr, sizeof(indirect_ptr)))
  728. return false;
  729. indirect_ptr = le64_to_cpu(indirect_ptr);
  730. /* check the valid bit of the first level entry */
  731. if (!(indirect_ptr & BIT_ULL(63)))
  732. return false;
  733. /* Mask the guest physical address and calculate the frame number. */
  734. indirect_ptr &= GENMASK_ULL(51, 16);
  735. /* Find the address of the actual entry */
  736. index = id % (SZ_64K / esz);
  737. indirect_ptr += index * esz;
  738. if (eaddr)
  739. *eaddr = indirect_ptr;
  740. return __is_visible_gfn_locked(its, indirect_ptr);
  741. }
  742. /*
  743. * Check whether an event ID can be stored in the corresponding Interrupt
  744. * Translation Table, which starts at device->itt_addr.
  745. */
  746. static bool vgic_its_check_event_id(struct vgic_its *its, struct its_device *device,
  747. u32 event_id)
  748. {
  749. const struct vgic_its_abi *abi = vgic_its_get_abi(its);
  750. int ite_esz = abi->ite_esz;
  751. gpa_t gpa;
  752. /* max table size is: BIT_ULL(device->num_eventid_bits) * ite_esz */
  753. if (event_id >= BIT_ULL(device->num_eventid_bits))
  754. return false;
  755. gpa = device->itt_addr + event_id * ite_esz;
  756. return __is_visible_gfn_locked(its, gpa);
  757. }
  758. /*
  759. * Add a new collection into the ITS collection table.
  760. * Returns 0 on success, and a negative error value for generic errors.
  761. */
  762. static int vgic_its_alloc_collection(struct vgic_its *its,
  763. struct its_collection **colp,
  764. u32 coll_id)
  765. {
  766. struct its_collection *collection;
  767. collection = kzalloc(sizeof(*collection), GFP_KERNEL_ACCOUNT);
  768. if (!collection)
  769. return -ENOMEM;
  770. collection->collection_id = coll_id;
  771. collection->target_addr = COLLECTION_NOT_MAPPED;
  772. list_add_tail(&collection->coll_list, &its->collection_list);
  773. *colp = collection;
  774. return 0;
  775. }
  776. static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
  777. {
  778. struct its_collection *collection;
  779. struct its_device *device;
  780. struct its_ite *ite;
  781. /*
  782. * Clearing the mapping for that collection ID removes the
  783. * entry from the list. If there wasn't any before, we can
  784. * go home early.
  785. */
  786. collection = find_collection(its, coll_id);
  787. if (!collection)
  788. return;
  789. for_each_lpi_its(device, ite, its)
  790. if (ite->collection &&
  791. ite->collection->collection_id == coll_id)
  792. ite->collection = NULL;
  793. list_del(&collection->coll_list);
  794. kfree(collection);
  795. }
  796. /* Must be called with its_lock mutex held */
  797. static struct its_ite *vgic_its_alloc_ite(struct its_device *device,
  798. struct its_collection *collection,
  799. u32 event_id)
  800. {
  801. struct its_ite *ite;
  802. ite = kzalloc(sizeof(*ite), GFP_KERNEL_ACCOUNT);
  803. if (!ite)
  804. return ERR_PTR(-ENOMEM);
  805. ite->event_id = event_id;
  806. ite->collection = collection;
  807. list_add_tail(&ite->ite_list, &device->itt_head);
  808. return ite;
  809. }
  810. /*
  811. * The MAPTI and MAPI commands map LPIs to ITTEs.
  812. * Must be called with its_lock mutex held.
  813. */
  814. static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
  815. u64 *its_cmd)
  816. {
  817. u32 device_id = its_cmd_get_deviceid(its_cmd);
  818. u32 event_id = its_cmd_get_id(its_cmd);
  819. u32 coll_id = its_cmd_get_collection(its_cmd);
  820. struct its_ite *ite;
  821. struct kvm_vcpu *vcpu = NULL;
  822. struct its_device *device;
  823. struct its_collection *collection, *new_coll = NULL;
  824. struct vgic_irq *irq;
  825. int lpi_nr;
  826. device = find_its_device(its, device_id);
  827. if (!device)
  828. return E_ITS_MAPTI_UNMAPPED_DEVICE;
  829. if (!vgic_its_check_event_id(its, device, event_id))
  830. return E_ITS_MAPTI_ID_OOR;
  831. if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI)
  832. lpi_nr = its_cmd_get_physical_id(its_cmd);
  833. else
  834. lpi_nr = event_id;
  835. if (lpi_nr < GIC_LPI_OFFSET ||
  836. lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser))
  837. return E_ITS_MAPTI_PHYSICALID_OOR;
  838. /* If there is an existing mapping, behavior is UNPREDICTABLE. */
  839. if (find_ite(its, device_id, event_id))
  840. return 0;
  841. collection = find_collection(its, coll_id);
  842. if (!collection) {
  843. int ret;
  844. if (!vgic_its_check_id(its, its->baser_coll_table, coll_id, NULL))
  845. return E_ITS_MAPC_COLLECTION_OOR;
  846. ret = vgic_its_alloc_collection(its, &collection, coll_id);
  847. if (ret)
  848. return ret;
  849. new_coll = collection;
  850. }
  851. ite = vgic_its_alloc_ite(device, collection, event_id);
  852. if (IS_ERR(ite)) {
  853. if (new_coll)
  854. vgic_its_free_collection(its, coll_id);
  855. return PTR_ERR(ite);
  856. }
  857. if (its_is_collection_mapped(collection))
  858. vcpu = collection_to_vcpu(kvm, collection);
  859. irq = vgic_add_lpi(kvm, lpi_nr, vcpu);
  860. if (IS_ERR(irq)) {
  861. if (new_coll)
  862. vgic_its_free_collection(its, coll_id);
  863. its_free_ite(kvm, ite);
  864. return PTR_ERR(irq);
  865. }
  866. ite->irq = irq;
  867. return 0;
  868. }
  869. /* Requires the its_lock to be held. */
  870. static void vgic_its_free_device(struct kvm *kvm, struct vgic_its *its,
  871. struct its_device *device)
  872. {
  873. struct its_ite *ite, *temp;
  874. /*
  875. * The spec says that unmapping a device with still valid
  876. * ITTEs associated is UNPREDICTABLE. We remove all ITTEs,
  877. * since we cannot leave the memory unreferenced.
  878. */
  879. list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list)
  880. its_free_ite(kvm, ite);
  881. vgic_its_invalidate_cache(its);
  882. list_del(&device->dev_list);
  883. kfree(device);
  884. }
  885. /* its lock must be held */
  886. static void vgic_its_free_device_list(struct kvm *kvm, struct vgic_its *its)
  887. {
  888. struct its_device *cur, *temp;
  889. list_for_each_entry_safe(cur, temp, &its->device_list, dev_list)
  890. vgic_its_free_device(kvm, its, cur);
  891. }
  892. /* its lock must be held */
  893. static void vgic_its_free_collection_list(struct kvm *kvm, struct vgic_its *its)
  894. {
  895. struct its_collection *cur, *temp;
  896. list_for_each_entry_safe(cur, temp, &its->collection_list, coll_list)
  897. vgic_its_free_collection(its, cur->collection_id);
  898. }
  899. /* Must be called with its_lock mutex held */
  900. static struct its_device *vgic_its_alloc_device(struct vgic_its *its,
  901. u32 device_id, gpa_t itt_addr,
  902. u8 num_eventid_bits)
  903. {
  904. struct its_device *device;
  905. device = kzalloc(sizeof(*device), GFP_KERNEL_ACCOUNT);
  906. if (!device)
  907. return ERR_PTR(-ENOMEM);
  908. device->device_id = device_id;
  909. device->itt_addr = itt_addr;
  910. device->num_eventid_bits = num_eventid_bits;
  911. INIT_LIST_HEAD(&device->itt_head);
  912. list_add_tail(&device->dev_list, &its->device_list);
  913. return device;
  914. }
  915. /*
  916. * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs).
  917. * Must be called with the its_lock mutex held.
  918. */
  919. static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
  920. u64 *its_cmd)
  921. {
  922. u32 device_id = its_cmd_get_deviceid(its_cmd);
  923. bool valid = its_cmd_get_validbit(its_cmd);
  924. u8 num_eventid_bits = its_cmd_get_size(its_cmd);
  925. gpa_t itt_addr = its_cmd_get_ittaddr(its_cmd);
  926. int dte_esz = vgic_its_get_abi(its)->dte_esz;
  927. struct its_device *device;
  928. gpa_t gpa;
  929. if (!vgic_its_check_id(its, its->baser_device_table, device_id, &gpa))
  930. return E_ITS_MAPD_DEVICE_OOR;
  931. if (valid && num_eventid_bits > VITS_TYPER_IDBITS)
  932. return E_ITS_MAPD_ITTSIZE_OOR;
  933. device = find_its_device(its, device_id);
  934. /*
  935. * The spec says that calling MAPD on an already mapped device
  936. * invalidates all cached data for this device. We implement this
  937. * by removing the mapping and re-establishing it.
  938. */
  939. if (device)
  940. vgic_its_free_device(kvm, its, device);
  941. /*
  942. * The spec does not say whether unmapping a not-mapped device
  943. * is an error, so we are done in any case.
  944. */
  945. if (!valid)
  946. return vgic_its_write_entry_lock(its, gpa, 0, dte_esz);
  947. device = vgic_its_alloc_device(its, device_id, itt_addr,
  948. num_eventid_bits);
  949. return PTR_ERR_OR_ZERO(device);
  950. }
  951. /*
  952. * The MAPC command maps collection IDs to redistributors.
  953. * Must be called with the its_lock mutex held.
  954. */
  955. static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its,
  956. u64 *its_cmd)
  957. {
  958. u16 coll_id;
  959. struct its_collection *collection;
  960. bool valid;
  961. valid = its_cmd_get_validbit(its_cmd);
  962. coll_id = its_cmd_get_collection(its_cmd);
  963. if (!valid) {
  964. vgic_its_free_collection(its, coll_id);
  965. vgic_its_invalidate_cache(its);
  966. } else {
  967. struct kvm_vcpu *vcpu;
  968. vcpu = kvm_get_vcpu_by_id(kvm, its_cmd_get_target_addr(its_cmd));
  969. if (!vcpu)
  970. return E_ITS_MAPC_PROCNUM_OOR;
  971. collection = find_collection(its, coll_id);
  972. if (!collection) {
  973. int ret;
  974. if (!vgic_its_check_id(its, its->baser_coll_table,
  975. coll_id, NULL))
  976. return E_ITS_MAPC_COLLECTION_OOR;
  977. ret = vgic_its_alloc_collection(its, &collection,
  978. coll_id);
  979. if (ret)
  980. return ret;
  981. collection->target_addr = vcpu->vcpu_id;
  982. } else {
  983. collection->target_addr = vcpu->vcpu_id;
  984. update_affinity_collection(kvm, its, collection);
  985. }
  986. }
  987. return 0;
  988. }
  989. /*
  990. * The CLEAR command removes the pending state for a particular LPI.
  991. * Must be called with the its_lock mutex held.
  992. */
  993. static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its,
  994. u64 *its_cmd)
  995. {
  996. u32 device_id = its_cmd_get_deviceid(its_cmd);
  997. u32 event_id = its_cmd_get_id(its_cmd);
  998. struct its_ite *ite;
  999. ite = find_ite(its, device_id, event_id);
  1000. if (!ite)
  1001. return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
  1002. ite->irq->pending_latch = false;
  1003. if (ite->irq->hw)
  1004. return irq_set_irqchip_state(ite->irq->host_irq,
  1005. IRQCHIP_STATE_PENDING, false);
  1006. return 0;
  1007. }
  1008. int vgic_its_inv_lpi(struct kvm *kvm, struct vgic_irq *irq)
  1009. {
  1010. return update_lpi_config(kvm, irq, NULL, true);
  1011. }
  1012. /*
  1013. * The INV command syncs the configuration bits from the memory table.
  1014. * Must be called with the its_lock mutex held.
  1015. */
  1016. static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its,
  1017. u64 *its_cmd)
  1018. {
  1019. u32 device_id = its_cmd_get_deviceid(its_cmd);
  1020. u32 event_id = its_cmd_get_id(its_cmd);
  1021. struct its_ite *ite;
  1022. ite = find_ite(its, device_id, event_id);
  1023. if (!ite)
  1024. return E_ITS_INV_UNMAPPED_INTERRUPT;
  1025. return vgic_its_inv_lpi(kvm, ite->irq);
  1026. }
  1027. /**
  1028. * vgic_its_invall - invalidate all LPIs targeting a given vcpu
  1029. * @vcpu: the vcpu for which the RD is targeted by an invalidation
  1030. *
  1031. * Contrary to the INVALL command, this targets a RD instead of a
  1032. * collection, and we don't need to hold the its_lock, since no ITS is
  1033. * involved here.
  1034. */
  1035. int vgic_its_invall(struct kvm_vcpu *vcpu)
  1036. {
  1037. struct kvm *kvm = vcpu->kvm;
  1038. struct vgic_dist *dist = &kvm->arch.vgic;
  1039. struct vgic_irq *irq;
  1040. unsigned long intid;
  1041. xa_for_each(&dist->lpi_xa, intid, irq) {
  1042. irq = vgic_get_irq(kvm, NULL, intid);
  1043. if (!irq)
  1044. continue;
  1045. update_lpi_config(kvm, irq, vcpu, false);
  1046. vgic_put_irq(kvm, irq);
  1047. }
  1048. if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.its_vm)
  1049. its_invall_vpe(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe);
  1050. return 0;
  1051. }
  1052. /*
  1053. * The INVALL command requests flushing of all IRQ data in this collection.
  1054. * Find the VCPU mapped to that collection, then iterate over the VM's list
  1055. * of mapped LPIs and update the configuration for each IRQ which targets
  1056. * the specified vcpu. The configuration will be read from the in-memory
  1057. * configuration table.
  1058. * Must be called with the its_lock mutex held.
  1059. */
  1060. static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
  1061. u64 *its_cmd)
  1062. {
  1063. u32 coll_id = its_cmd_get_collection(its_cmd);
  1064. struct its_collection *collection;
  1065. struct kvm_vcpu *vcpu;
  1066. collection = find_collection(its, coll_id);
  1067. if (!its_is_collection_mapped(collection))
  1068. return E_ITS_INVALL_UNMAPPED_COLLECTION;
  1069. vcpu = collection_to_vcpu(kvm, collection);
  1070. vgic_its_invall(vcpu);
  1071. return 0;
  1072. }
  1073. /*
  1074. * The MOVALL command moves the pending state of all IRQs targeting one
  1075. * redistributor to another. We don't hold the pending state in the VCPUs,
  1076. * but in the IRQs instead, so there is really not much to do for us here.
  1077. * However the spec says that no IRQ must target the old redistributor
  1078. * afterwards, so we make sure that no LPI is using the associated target_vcpu.
  1079. * This command affects all LPIs in the system that target that redistributor.
  1080. */
  1081. static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,
  1082. u64 *its_cmd)
  1083. {
  1084. struct vgic_dist *dist = &kvm->arch.vgic;
  1085. struct kvm_vcpu *vcpu1, *vcpu2;
  1086. struct vgic_irq *irq;
  1087. unsigned long intid;
  1088. /* We advertise GITS_TYPER.PTA==0, making the address the vcpu ID */
  1089. vcpu1 = kvm_get_vcpu_by_id(kvm, its_cmd_get_target_addr(its_cmd));
  1090. vcpu2 = kvm_get_vcpu_by_id(kvm, its_cmd_mask_field(its_cmd, 3, 16, 32));
  1091. if (!vcpu1 || !vcpu2)
  1092. return E_ITS_MOVALL_PROCNUM_OOR;
  1093. if (vcpu1 == vcpu2)
  1094. return 0;
  1095. xa_for_each(&dist->lpi_xa, intid, irq) {
  1096. irq = vgic_get_irq(kvm, NULL, intid);
  1097. if (!irq)
  1098. continue;
  1099. update_affinity(irq, vcpu2);
  1100. vgic_put_irq(kvm, irq);
  1101. }
  1102. vgic_its_invalidate_cache(its);
  1103. return 0;
  1104. }
  1105. /*
  1106. * The INT command injects the LPI associated with that DevID/EvID pair.
  1107. * Must be called with the its_lock mutex held.
  1108. */
  1109. static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
  1110. u64 *its_cmd)
  1111. {
  1112. u32 msi_data = its_cmd_get_id(its_cmd);
  1113. u64 msi_devid = its_cmd_get_deviceid(its_cmd);
  1114. return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data);
  1115. }
  1116. /*
  1117. * This function is called with the its_cmd lock held, but the ITS data
  1118. * structure lock dropped.
  1119. */
  1120. static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
  1121. u64 *its_cmd)
  1122. {
  1123. int ret = -ENODEV;
  1124. mutex_lock(&its->its_lock);
  1125. switch (its_cmd_get_command(its_cmd)) {
  1126. case GITS_CMD_MAPD:
  1127. ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd);
  1128. break;
  1129. case GITS_CMD_MAPC:
  1130. ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd);
  1131. break;
  1132. case GITS_CMD_MAPI:
  1133. ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
  1134. break;
  1135. case GITS_CMD_MAPTI:
  1136. ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
  1137. break;
  1138. case GITS_CMD_MOVI:
  1139. ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd);
  1140. break;
  1141. case GITS_CMD_DISCARD:
  1142. ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd);
  1143. break;
  1144. case GITS_CMD_CLEAR:
  1145. ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd);
  1146. break;
  1147. case GITS_CMD_MOVALL:
  1148. ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd);
  1149. break;
  1150. case GITS_CMD_INT:
  1151. ret = vgic_its_cmd_handle_int(kvm, its, its_cmd);
  1152. break;
  1153. case GITS_CMD_INV:
  1154. ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd);
  1155. break;
  1156. case GITS_CMD_INVALL:
  1157. ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd);
  1158. break;
  1159. case GITS_CMD_SYNC:
  1160. /* we ignore this command: we are in sync all of the time */
  1161. ret = 0;
  1162. break;
  1163. }
  1164. mutex_unlock(&its->its_lock);
  1165. return ret;
  1166. }
  1167. static u64 vgic_sanitise_its_baser(u64 reg)
  1168. {
  1169. reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK,
  1170. GITS_BASER_SHAREABILITY_SHIFT,
  1171. vgic_sanitise_shareability);
  1172. reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK,
  1173. GITS_BASER_INNER_CACHEABILITY_SHIFT,
  1174. vgic_sanitise_inner_cacheability);
  1175. reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK,
  1176. GITS_BASER_OUTER_CACHEABILITY_SHIFT,
  1177. vgic_sanitise_outer_cacheability);
  1178. /* We support only one (ITS) page size: 64K */
  1179. reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K;
  1180. return reg;
  1181. }
  1182. static u64 vgic_sanitise_its_cbaser(u64 reg)
  1183. {
  1184. reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK,
  1185. GITS_CBASER_SHAREABILITY_SHIFT,
  1186. vgic_sanitise_shareability);
  1187. reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK,
  1188. GITS_CBASER_INNER_CACHEABILITY_SHIFT,
  1189. vgic_sanitise_inner_cacheability);
  1190. reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK,
  1191. GITS_CBASER_OUTER_CACHEABILITY_SHIFT,
  1192. vgic_sanitise_outer_cacheability);
  1193. /* Sanitise the physical address to be 64k aligned. */
  1194. reg &= ~GENMASK_ULL(15, 12);
  1195. return reg;
  1196. }
  1197. static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm,
  1198. struct vgic_its *its,
  1199. gpa_t addr, unsigned int len)
  1200. {
  1201. return extract_bytes(its->cbaser, addr & 7, len);
  1202. }
  1203. static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its,
  1204. gpa_t addr, unsigned int len,
  1205. unsigned long val)
  1206. {
  1207. /* When GITS_CTLR.Enable is 1, this register is RO. */
  1208. if (its->enabled)
  1209. return;
  1210. mutex_lock(&its->cmd_lock);
  1211. its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val);
  1212. its->cbaser = vgic_sanitise_its_cbaser(its->cbaser);
  1213. its->creadr = 0;
  1214. /*
  1215. * CWRITER is architecturally UNKNOWN on reset, but we need to reset
  1216. * it to CREADR to make sure we start with an empty command buffer.
  1217. */
  1218. its->cwriter = its->creadr;
  1219. mutex_unlock(&its->cmd_lock);
  1220. }
  1221. #define ITS_CMD_BUFFER_SIZE(baser) ((((baser) & 0xff) + 1) << 12)
  1222. #define ITS_CMD_SIZE 32
  1223. #define ITS_CMD_OFFSET(reg) ((reg) & GENMASK(19, 5))
  1224. /* Must be called with the cmd_lock held. */
  1225. static void vgic_its_process_commands(struct kvm *kvm, struct vgic_its *its)
  1226. {
  1227. gpa_t cbaser;
  1228. u64 cmd_buf[4];
  1229. /* Commands are only processed when the ITS is enabled. */
  1230. if (!its->enabled)
  1231. return;
  1232. cbaser = GITS_CBASER_ADDRESS(its->cbaser);
  1233. while (its->cwriter != its->creadr) {
  1234. int ret = kvm_read_guest_lock(kvm, cbaser + its->creadr,
  1235. cmd_buf, ITS_CMD_SIZE);
  1236. /*
  1237. * If kvm_read_guest() fails, this could be due to the guest
  1238. * programming a bogus value in CBASER or something else going
  1239. * wrong from which we cannot easily recover.
  1240. * According to section 6.3.2 in the GICv3 spec we can just
  1241. * ignore that command then.
  1242. */
  1243. if (!ret)
  1244. vgic_its_handle_command(kvm, its, cmd_buf);
  1245. its->creadr += ITS_CMD_SIZE;
  1246. if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser))
  1247. its->creadr = 0;
  1248. }
  1249. }
  1250. /*
  1251. * By writing to CWRITER the guest announces new commands to be processed.
  1252. * To avoid any races in the first place, we take the its_cmd lock, which
  1253. * protects our ring buffer variables, so that there is only one user
  1254. * per ITS handling commands at a given time.
  1255. */
  1256. static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its,
  1257. gpa_t addr, unsigned int len,
  1258. unsigned long val)
  1259. {
  1260. u64 reg;
  1261. if (!its)
  1262. return;
  1263. mutex_lock(&its->cmd_lock);
  1264. reg = update_64bit_reg(its->cwriter, addr & 7, len, val);
  1265. reg = ITS_CMD_OFFSET(reg);
  1266. if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
  1267. mutex_unlock(&its->cmd_lock);
  1268. return;
  1269. }
  1270. its->cwriter = reg;
  1271. vgic_its_process_commands(kvm, its);
  1272. mutex_unlock(&its->cmd_lock);
  1273. }
  1274. static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm,
  1275. struct vgic_its *its,
  1276. gpa_t addr, unsigned int len)
  1277. {
  1278. return extract_bytes(its->cwriter, addr & 0x7, len);
  1279. }
  1280. static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,
  1281. struct vgic_its *its,
  1282. gpa_t addr, unsigned int len)
  1283. {
  1284. return extract_bytes(its->creadr, addr & 0x7, len);
  1285. }
  1286. static int vgic_mmio_uaccess_write_its_creadr(struct kvm *kvm,
  1287. struct vgic_its *its,
  1288. gpa_t addr, unsigned int len,
  1289. unsigned long val)
  1290. {
  1291. u32 cmd_offset;
  1292. int ret = 0;
  1293. mutex_lock(&its->cmd_lock);
  1294. if (its->enabled) {
  1295. ret = -EBUSY;
  1296. goto out;
  1297. }
  1298. cmd_offset = ITS_CMD_OFFSET(val);
  1299. if (cmd_offset >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
  1300. ret = -EINVAL;
  1301. goto out;
  1302. }
  1303. its->creadr = cmd_offset;
  1304. out:
  1305. mutex_unlock(&its->cmd_lock);
  1306. return ret;
  1307. }
  1308. #define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7)
  1309. static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm,
  1310. struct vgic_its *its,
  1311. gpa_t addr, unsigned int len)
  1312. {
  1313. u64 reg;
  1314. switch (BASER_INDEX(addr)) {
  1315. case 0:
  1316. reg = its->baser_device_table;
  1317. break;
  1318. case 1:
  1319. reg = its->baser_coll_table;
  1320. break;
  1321. default:
  1322. reg = 0;
  1323. break;
  1324. }
  1325. return extract_bytes(reg, addr & 7, len);
  1326. }
  1327. #define GITS_BASER_RO_MASK (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56))
  1328. static void vgic_mmio_write_its_baser(struct kvm *kvm,
  1329. struct vgic_its *its,
  1330. gpa_t addr, unsigned int len,
  1331. unsigned long val)
  1332. {
  1333. const struct vgic_its_abi *abi = vgic_its_get_abi(its);
  1334. u64 entry_size, table_type;
  1335. u64 reg, *regptr, clearbits = 0;
  1336. /* When GITS_CTLR.Enable is 1, we ignore write accesses. */
  1337. if (its->enabled)
  1338. return;
  1339. switch (BASER_INDEX(addr)) {
  1340. case 0:
  1341. regptr = &its->baser_device_table;
  1342. entry_size = abi->dte_esz;
  1343. table_type = GITS_BASER_TYPE_DEVICE;
  1344. break;
  1345. case 1:
  1346. regptr = &its->baser_coll_table;
  1347. entry_size = abi->cte_esz;
  1348. table_type = GITS_BASER_TYPE_COLLECTION;
  1349. clearbits = GITS_BASER_INDIRECT;
  1350. break;
  1351. default:
  1352. return;
  1353. }
  1354. reg = update_64bit_reg(*regptr, addr & 7, len, val);
  1355. reg &= ~GITS_BASER_RO_MASK;
  1356. reg &= ~clearbits;
  1357. reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
  1358. reg |= table_type << GITS_BASER_TYPE_SHIFT;
  1359. reg = vgic_sanitise_its_baser(reg);
  1360. *regptr = reg;
  1361. if (!(reg & GITS_BASER_VALID)) {
  1362. /* Take the its_lock to prevent a race with a save/restore */
  1363. mutex_lock(&its->its_lock);
  1364. switch (table_type) {
  1365. case GITS_BASER_TYPE_DEVICE:
  1366. vgic_its_free_device_list(kvm, its);
  1367. break;
  1368. case GITS_BASER_TYPE_COLLECTION:
  1369. vgic_its_free_collection_list(kvm, its);
  1370. break;
  1371. }
  1372. mutex_unlock(&its->its_lock);
  1373. }
  1374. }
  1375. static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu,
  1376. struct vgic_its *its,
  1377. gpa_t addr, unsigned int len)
  1378. {
  1379. u32 reg = 0;
  1380. mutex_lock(&its->cmd_lock);
  1381. if (its->creadr == its->cwriter)
  1382. reg |= GITS_CTLR_QUIESCENT;
  1383. if (its->enabled)
  1384. reg |= GITS_CTLR_ENABLE;
  1385. mutex_unlock(&its->cmd_lock);
  1386. return reg;
  1387. }
  1388. static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its,
  1389. gpa_t addr, unsigned int len,
  1390. unsigned long val)
  1391. {
  1392. mutex_lock(&its->cmd_lock);
  1393. /*
  1394. * It is UNPREDICTABLE to enable the ITS if any of the CBASER or
  1395. * device/collection BASER are invalid
  1396. */
  1397. if (!its->enabled && (val & GITS_CTLR_ENABLE) &&
  1398. (!(its->baser_device_table & GITS_BASER_VALID) ||
  1399. !(its->baser_coll_table & GITS_BASER_VALID) ||
  1400. !(its->cbaser & GITS_CBASER_VALID)))
  1401. goto out;
  1402. its->enabled = !!(val & GITS_CTLR_ENABLE);
  1403. if (!its->enabled)
  1404. vgic_its_invalidate_cache(its);
  1405. /*
  1406. * Try to process any pending commands. This function bails out early
  1407. * if the ITS is disabled or no commands have been queued.
  1408. */
  1409. vgic_its_process_commands(kvm, its);
  1410. out:
  1411. mutex_unlock(&its->cmd_lock);
  1412. }
  1413. #define REGISTER_ITS_DESC(off, rd, wr, length, acc) \
  1414. { \
  1415. .reg_offset = off, \
  1416. .len = length, \
  1417. .access_flags = acc, \
  1418. .its_read = rd, \
  1419. .its_write = wr, \
  1420. }
  1421. #define REGISTER_ITS_DESC_UACCESS(off, rd, wr, uwr, length, acc)\
  1422. { \
  1423. .reg_offset = off, \
  1424. .len = length, \
  1425. .access_flags = acc, \
  1426. .its_read = rd, \
  1427. .its_write = wr, \
  1428. .uaccess_its_write = uwr, \
  1429. }
  1430. static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its,
  1431. gpa_t addr, unsigned int len, unsigned long val)
  1432. {
  1433. /* Ignore */
  1434. }
  1435. static struct vgic_register_region its_registers[] = {
  1436. REGISTER_ITS_DESC(GITS_CTLR,
  1437. vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4,
  1438. VGIC_ACCESS_32bit),
  1439. REGISTER_ITS_DESC_UACCESS(GITS_IIDR,
  1440. vgic_mmio_read_its_iidr, its_mmio_write_wi,
  1441. vgic_mmio_uaccess_write_its_iidr, 4,
  1442. VGIC_ACCESS_32bit),
  1443. REGISTER_ITS_DESC(GITS_TYPER,
  1444. vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
  1445. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  1446. REGISTER_ITS_DESC(GITS_CBASER,
  1447. vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8,
  1448. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  1449. REGISTER_ITS_DESC(GITS_CWRITER,
  1450. vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8,
  1451. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  1452. REGISTER_ITS_DESC_UACCESS(GITS_CREADR,
  1453. vgic_mmio_read_its_creadr, its_mmio_write_wi,
  1454. vgic_mmio_uaccess_write_its_creadr, 8,
  1455. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  1456. REGISTER_ITS_DESC(GITS_BASER,
  1457. vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
  1458. VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
  1459. REGISTER_ITS_DESC(GITS_IDREGS_BASE,
  1460. vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30,
  1461. VGIC_ACCESS_32bit),
  1462. };
  1463. /* This is called on setting the LPI enable bit in the redistributor. */
  1464. void vgic_enable_lpis(struct kvm_vcpu *vcpu)
  1465. {
  1466. if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ))
  1467. its_sync_lpi_pending_table(vcpu);
  1468. }
  1469. static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its,
  1470. u64 addr)
  1471. {
  1472. struct vgic_io_device *iodev = &its->iodev;
  1473. int ret;
  1474. mutex_lock(&kvm->slots_lock);
  1475. if (!IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
  1476. ret = -EBUSY;
  1477. goto out;
  1478. }
  1479. its->vgic_its_base = addr;
  1480. iodev->regions = its_registers;
  1481. iodev->nr_regions = ARRAY_SIZE(its_registers);
  1482. kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops);
  1483. iodev->base_addr = its->vgic_its_base;
  1484. iodev->iodev_type = IODEV_ITS;
  1485. iodev->its = its;
  1486. ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr,
  1487. KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
  1488. out:
  1489. mutex_unlock(&kvm->slots_lock);
  1490. return ret;
  1491. }
  1492. #define INITIAL_BASER_VALUE \
  1493. (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \
  1494. GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \
  1495. GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \
  1496. GITS_BASER_PAGE_SIZE_64K)
  1497. #define INITIAL_PROPBASER_VALUE \
  1498. (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb) | \
  1499. GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner) | \
  1500. GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable))
  1501. static int vgic_its_create(struct kvm_device *dev, u32 type)
  1502. {
  1503. int ret;
  1504. struct vgic_its *its;
  1505. if (type != KVM_DEV_TYPE_ARM_VGIC_ITS)
  1506. return -ENODEV;
  1507. its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL_ACCOUNT);
  1508. if (!its)
  1509. return -ENOMEM;
  1510. mutex_lock(&dev->kvm->arch.config_lock);
  1511. if (vgic_initialized(dev->kvm)) {
  1512. ret = vgic_v4_init(dev->kvm);
  1513. if (ret < 0) {
  1514. mutex_unlock(&dev->kvm->arch.config_lock);
  1515. kfree(its);
  1516. return ret;
  1517. }
  1518. }
  1519. mutex_init(&its->its_lock);
  1520. mutex_init(&its->cmd_lock);
  1521. /* Yep, even more trickery for lock ordering... */
  1522. #ifdef CONFIG_LOCKDEP
  1523. mutex_lock(&its->cmd_lock);
  1524. mutex_lock(&its->its_lock);
  1525. mutex_unlock(&its->its_lock);
  1526. mutex_unlock(&its->cmd_lock);
  1527. #endif
  1528. its->vgic_its_base = VGIC_ADDR_UNDEF;
  1529. INIT_LIST_HEAD(&its->device_list);
  1530. INIT_LIST_HEAD(&its->collection_list);
  1531. xa_init(&its->translation_cache);
  1532. dev->kvm->arch.vgic.msis_require_devid = true;
  1533. dev->kvm->arch.vgic.has_its = true;
  1534. its->enabled = false;
  1535. its->dev = dev;
  1536. its->baser_device_table = INITIAL_BASER_VALUE |
  1537. ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT);
  1538. its->baser_coll_table = INITIAL_BASER_VALUE |
  1539. ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT);
  1540. dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE;
  1541. dev->private = its;
  1542. ret = vgic_its_set_abi(its, NR_ITS_ABIS - 1);
  1543. mutex_unlock(&dev->kvm->arch.config_lock);
  1544. return ret;
  1545. }
  1546. static void vgic_its_destroy(struct kvm_device *kvm_dev)
  1547. {
  1548. struct kvm *kvm = kvm_dev->kvm;
  1549. struct vgic_its *its = kvm_dev->private;
  1550. mutex_lock(&its->its_lock);
  1551. vgic_its_free_device_list(kvm, its);
  1552. vgic_its_free_collection_list(kvm, its);
  1553. vgic_its_invalidate_cache(its);
  1554. xa_destroy(&its->translation_cache);
  1555. mutex_unlock(&its->its_lock);
  1556. kfree(its);
  1557. kfree(kvm_dev);/* alloc by kvm_ioctl_create_device, free by .destroy */
  1558. }
  1559. static int vgic_its_has_attr_regs(struct kvm_device *dev,
  1560. struct kvm_device_attr *attr)
  1561. {
  1562. const struct vgic_register_region *region;
  1563. gpa_t offset = attr->attr;
  1564. int align;
  1565. align = (offset < GITS_TYPER) || (offset >= GITS_PIDR4) ? 0x3 : 0x7;
  1566. if (offset & align)
  1567. return -EINVAL;
  1568. region = vgic_find_mmio_region(its_registers,
  1569. ARRAY_SIZE(its_registers),
  1570. offset);
  1571. if (!region)
  1572. return -ENXIO;
  1573. return 0;
  1574. }
  1575. static int vgic_its_attr_regs_access(struct kvm_device *dev,
  1576. struct kvm_device_attr *attr,
  1577. u64 *reg, bool is_write)
  1578. {
  1579. const struct vgic_register_region *region;
  1580. struct vgic_its *its;
  1581. gpa_t addr, offset;
  1582. unsigned int len;
  1583. int align, ret = 0;
  1584. its = dev->private;
  1585. offset = attr->attr;
  1586. /*
  1587. * Although the spec supports upper/lower 32-bit accesses to
  1588. * 64-bit ITS registers, the userspace ABI requires 64-bit
  1589. * accesses to all 64-bit wide registers. We therefore only
  1590. * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID
  1591. * registers
  1592. */
  1593. if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4))
  1594. align = 0x3;
  1595. else
  1596. align = 0x7;
  1597. if (offset & align)
  1598. return -EINVAL;
  1599. mutex_lock(&dev->kvm->lock);
  1600. if (!lock_all_vcpus(dev->kvm)) {
  1601. mutex_unlock(&dev->kvm->lock);
  1602. return -EBUSY;
  1603. }
  1604. mutex_lock(&dev->kvm->arch.config_lock);
  1605. if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
  1606. ret = -ENXIO;
  1607. goto out;
  1608. }
  1609. region = vgic_find_mmio_region(its_registers,
  1610. ARRAY_SIZE(its_registers),
  1611. offset);
  1612. if (!region) {
  1613. ret = -ENXIO;
  1614. goto out;
  1615. }
  1616. addr = its->vgic_its_base + offset;
  1617. len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4;
  1618. if (is_write) {
  1619. if (region->uaccess_its_write)
  1620. ret = region->uaccess_its_write(dev->kvm, its, addr,
  1621. len, *reg);
  1622. else
  1623. region->its_write(dev->kvm, its, addr, len, *reg);
  1624. } else {
  1625. *reg = region->its_read(dev->kvm, its, addr, len);
  1626. }
  1627. out:
  1628. mutex_unlock(&dev->kvm->arch.config_lock);
  1629. unlock_all_vcpus(dev->kvm);
  1630. mutex_unlock(&dev->kvm->lock);
  1631. return ret;
  1632. }
  1633. static u32 compute_next_devid_offset(struct list_head *h,
  1634. struct its_device *dev)
  1635. {
  1636. struct its_device *next;
  1637. u32 next_offset;
  1638. if (list_is_last(&dev->dev_list, h))
  1639. return 0;
  1640. next = list_next_entry(dev, dev_list);
  1641. next_offset = next->device_id - dev->device_id;
  1642. return min_t(u32, next_offset, VITS_DTE_MAX_DEVID_OFFSET);
  1643. }
  1644. static u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite)
  1645. {
  1646. struct its_ite *next;
  1647. u32 next_offset;
  1648. if (list_is_last(&ite->ite_list, h))
  1649. return 0;
  1650. next = list_next_entry(ite, ite_list);
  1651. next_offset = next->event_id - ite->event_id;
  1652. return min_t(u32, next_offset, VITS_ITE_MAX_EVENTID_OFFSET);
  1653. }
  1654. /**
  1655. * typedef entry_fn_t - Callback called on a table entry restore path
  1656. * @its: its handle
  1657. * @id: id of the entry
  1658. * @entry: pointer to the entry
  1659. * @opaque: pointer to an opaque data
  1660. *
  1661. * Return: < 0 on error, 0 if last element was identified, id offset to next
  1662. * element otherwise
  1663. */
  1664. typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,
  1665. void *opaque);
  1666. /**
  1667. * scan_its_table - Scan a contiguous table in guest RAM and applies a function
  1668. * to each entry
  1669. *
  1670. * @its: its handle
  1671. * @base: base gpa of the table
  1672. * @size: size of the table in bytes
  1673. * @esz: entry size in bytes
  1674. * @start_id: the ID of the first entry in the table
  1675. * (non zero for 2d level tables)
  1676. * @fn: function to apply on each entry
  1677. * @opaque: pointer to opaque data
  1678. *
  1679. * Return: < 0 on error, 0 if last element was identified, 1 otherwise
  1680. * (the last element may not be found on second level tables)
  1681. */
  1682. static int scan_its_table(struct vgic_its *its, gpa_t base, int size, u32 esz,
  1683. int start_id, entry_fn_t fn, void *opaque)
  1684. {
  1685. struct kvm *kvm = its->dev->kvm;
  1686. unsigned long len = size;
  1687. int id = start_id;
  1688. gpa_t gpa = base;
  1689. char entry[ESZ_MAX];
  1690. int ret;
  1691. memset(entry, 0, esz);
  1692. while (true) {
  1693. int next_offset;
  1694. size_t byte_offset;
  1695. ret = kvm_read_guest_lock(kvm, gpa, entry, esz);
  1696. if (ret)
  1697. return ret;
  1698. next_offset = fn(its, id, entry, opaque);
  1699. if (next_offset <= 0)
  1700. return next_offset;
  1701. byte_offset = next_offset * esz;
  1702. if (byte_offset >= len)
  1703. break;
  1704. id += next_offset;
  1705. gpa += byte_offset;
  1706. len -= byte_offset;
  1707. }
  1708. return 1;
  1709. }
  1710. /*
  1711. * vgic_its_save_ite - Save an interrupt translation entry at @gpa
  1712. */
  1713. static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev,
  1714. struct its_ite *ite, gpa_t gpa, int ite_esz)
  1715. {
  1716. u32 next_offset;
  1717. u64 val;
  1718. next_offset = compute_next_eventid_offset(&dev->itt_head, ite);
  1719. val = ((u64)next_offset << KVM_ITS_ITE_NEXT_SHIFT) |
  1720. ((u64)ite->irq->intid << KVM_ITS_ITE_PINTID_SHIFT) |
  1721. ite->collection->collection_id;
  1722. val = cpu_to_le64(val);
  1723. return vgic_its_write_entry_lock(its, gpa, val, ite_esz);
  1724. }
  1725. /**
  1726. * vgic_its_restore_ite - restore an interrupt translation entry
  1727. *
  1728. * @its: its handle
  1729. * @event_id: id used for indexing
  1730. * @ptr: pointer to the ITE entry
  1731. * @opaque: pointer to the its_device
  1732. */
  1733. static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id,
  1734. void *ptr, void *opaque)
  1735. {
  1736. struct its_device *dev = opaque;
  1737. struct its_collection *collection;
  1738. struct kvm *kvm = its->dev->kvm;
  1739. struct kvm_vcpu *vcpu = NULL;
  1740. u64 val;
  1741. u64 *p = (u64 *)ptr;
  1742. struct vgic_irq *irq;
  1743. u32 coll_id, lpi_id;
  1744. struct its_ite *ite;
  1745. u32 offset;
  1746. val = *p;
  1747. val = le64_to_cpu(val);
  1748. coll_id = val & KVM_ITS_ITE_ICID_MASK;
  1749. lpi_id = (val & KVM_ITS_ITE_PINTID_MASK) >> KVM_ITS_ITE_PINTID_SHIFT;
  1750. if (!lpi_id)
  1751. return 1; /* invalid entry, no choice but to scan next entry */
  1752. if (lpi_id < VGIC_MIN_LPI)
  1753. return -EINVAL;
  1754. offset = val >> KVM_ITS_ITE_NEXT_SHIFT;
  1755. if (event_id + offset >= BIT_ULL(dev->num_eventid_bits))
  1756. return -EINVAL;
  1757. collection = find_collection(its, coll_id);
  1758. if (!collection)
  1759. return -EINVAL;
  1760. if (!vgic_its_check_event_id(its, dev, event_id))
  1761. return -EINVAL;
  1762. ite = vgic_its_alloc_ite(dev, collection, event_id);
  1763. if (IS_ERR(ite))
  1764. return PTR_ERR(ite);
  1765. if (its_is_collection_mapped(collection))
  1766. vcpu = kvm_get_vcpu_by_id(kvm, collection->target_addr);
  1767. irq = vgic_add_lpi(kvm, lpi_id, vcpu);
  1768. if (IS_ERR(irq)) {
  1769. its_free_ite(kvm, ite);
  1770. return PTR_ERR(irq);
  1771. }
  1772. ite->irq = irq;
  1773. return offset;
  1774. }
  1775. static int vgic_its_ite_cmp(void *priv, const struct list_head *a,
  1776. const struct list_head *b)
  1777. {
  1778. struct its_ite *itea = container_of(a, struct its_ite, ite_list);
  1779. struct its_ite *iteb = container_of(b, struct its_ite, ite_list);
  1780. if (itea->event_id < iteb->event_id)
  1781. return -1;
  1782. else
  1783. return 1;
  1784. }
  1785. static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
  1786. {
  1787. const struct vgic_its_abi *abi = vgic_its_get_abi(its);
  1788. gpa_t base = device->itt_addr;
  1789. struct its_ite *ite;
  1790. int ret;
  1791. int ite_esz = abi->ite_esz;
  1792. list_sort(NULL, &device->itt_head, vgic_its_ite_cmp);
  1793. list_for_each_entry(ite, &device->itt_head, ite_list) {
  1794. gpa_t gpa = base + ite->event_id * ite_esz;
  1795. /*
  1796. * If an LPI carries the HW bit, this means that this
  1797. * interrupt is controlled by GICv4, and we do not
  1798. * have direct access to that state without GICv4.1.
  1799. * Let's simply fail the save operation...
  1800. */
  1801. if (ite->irq->hw && !kvm_vgic_global_state.has_gicv4_1)
  1802. return -EACCES;
  1803. ret = vgic_its_save_ite(its, device, ite, gpa, ite_esz);
  1804. if (ret)
  1805. return ret;
  1806. }
  1807. return 0;
  1808. }
  1809. /**
  1810. * vgic_its_restore_itt - restore the ITT of a device
  1811. *
  1812. * @its: its handle
  1813. * @dev: device handle
  1814. *
  1815. * Return 0 on success, < 0 on error
  1816. */
  1817. static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
  1818. {
  1819. const struct vgic_its_abi *abi = vgic_its_get_abi(its);
  1820. gpa_t base = dev->itt_addr;
  1821. int ret;
  1822. int ite_esz = abi->ite_esz;
  1823. size_t max_size = BIT_ULL(dev->num_eventid_bits) * ite_esz;
  1824. ret = scan_its_table(its, base, max_size, ite_esz, 0,
  1825. vgic_its_restore_ite, dev);
  1826. /* scan_its_table returns +1 if all ITEs are invalid */
  1827. if (ret > 0)
  1828. ret = 0;
  1829. return ret;
  1830. }
  1831. /**
  1832. * vgic_its_save_dte - Save a device table entry at a given GPA
  1833. *
  1834. * @its: ITS handle
  1835. * @dev: ITS device
  1836. * @ptr: GPA
  1837. * @dte_esz: device table entry size
  1838. */
  1839. static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
  1840. gpa_t ptr, int dte_esz)
  1841. {
  1842. u64 val, itt_addr_field;
  1843. u32 next_offset;
  1844. itt_addr_field = dev->itt_addr >> 8;
  1845. next_offset = compute_next_devid_offset(&its->device_list, dev);
  1846. val = (1ULL << KVM_ITS_DTE_VALID_SHIFT |
  1847. ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) |
  1848. (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
  1849. (dev->num_eventid_bits - 1));
  1850. val = cpu_to_le64(val);
  1851. return vgic_its_write_entry_lock(its, ptr, val, dte_esz);
  1852. }
  1853. /**
  1854. * vgic_its_restore_dte - restore a device table entry
  1855. *
  1856. * @its: its handle
  1857. * @id: device id the DTE corresponds to
  1858. * @ptr: kernel VA where the 8 byte DTE is located
  1859. * @opaque: unused
  1860. *
  1861. * Return: < 0 on error, 0 if the dte is the last one, id offset to the
  1862. * next dte otherwise
  1863. */
  1864. static int vgic_its_restore_dte(struct vgic_its *its, u32 id,
  1865. void *ptr, void *opaque)
  1866. {
  1867. struct its_device *dev;
  1868. u64 baser = its->baser_device_table;
  1869. gpa_t itt_addr;
  1870. u8 num_eventid_bits;
  1871. u64 entry = *(u64 *)ptr;
  1872. bool valid;
  1873. u32 offset;
  1874. int ret;
  1875. entry = le64_to_cpu(entry);
  1876. valid = entry >> KVM_ITS_DTE_VALID_SHIFT;
  1877. num_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1;
  1878. itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK)
  1879. >> KVM_ITS_DTE_ITTADDR_SHIFT) << 8;
  1880. if (!valid)
  1881. return 1;
  1882. /* dte entry is valid */
  1883. offset = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT;
  1884. if (!vgic_its_check_id(its, baser, id, NULL))
  1885. return -EINVAL;
  1886. dev = vgic_its_alloc_device(its, id, itt_addr, num_eventid_bits);
  1887. if (IS_ERR(dev))
  1888. return PTR_ERR(dev);
  1889. ret = vgic_its_restore_itt(its, dev);
  1890. if (ret) {
  1891. vgic_its_free_device(its->dev->kvm, its, dev);
  1892. return ret;
  1893. }
  1894. return offset;
  1895. }
  1896. static int vgic_its_device_cmp(void *priv, const struct list_head *a,
  1897. const struct list_head *b)
  1898. {
  1899. struct its_device *deva = container_of(a, struct its_device, dev_list);
  1900. struct its_device *devb = container_of(b, struct its_device, dev_list);
  1901. if (deva->device_id < devb->device_id)
  1902. return -1;
  1903. else
  1904. return 1;
  1905. }
  1906. /*
  1907. * vgic_its_save_device_tables - Save the device table and all ITT
  1908. * into guest RAM
  1909. *
  1910. * L1/L2 handling is hidden by vgic_its_check_id() helper which directly
  1911. * returns the GPA of the device entry
  1912. */
  1913. static int vgic_its_save_device_tables(struct vgic_its *its)
  1914. {
  1915. const struct vgic_its_abi *abi = vgic_its_get_abi(its);
  1916. u64 baser = its->baser_device_table;
  1917. struct its_device *dev;
  1918. int dte_esz = abi->dte_esz;
  1919. if (!(baser & GITS_BASER_VALID))
  1920. return 0;
  1921. list_sort(NULL, &its->device_list, vgic_its_device_cmp);
  1922. list_for_each_entry(dev, &its->device_list, dev_list) {
  1923. int ret;
  1924. gpa_t eaddr;
  1925. if (!vgic_its_check_id(its, baser,
  1926. dev->device_id, &eaddr))
  1927. return -EINVAL;
  1928. ret = vgic_its_save_itt(its, dev);
  1929. if (ret)
  1930. return ret;
  1931. ret = vgic_its_save_dte(its, dev, eaddr, dte_esz);
  1932. if (ret)
  1933. return ret;
  1934. }
  1935. return 0;
  1936. }
  1937. /**
  1938. * handle_l1_dte - callback used for L1 device table entries (2 stage case)
  1939. *
  1940. * @its: its handle
  1941. * @id: index of the entry in the L1 table
  1942. * @addr: kernel VA
  1943. * @opaque: unused
  1944. *
  1945. * L1 table entries are scanned by steps of 1 entry
  1946. * Return < 0 if error, 0 if last dte was found when scanning the L2
  1947. * table, +1 otherwise (meaning next L1 entry must be scanned)
  1948. */
  1949. static int handle_l1_dte(struct vgic_its *its, u32 id, void *addr,
  1950. void *opaque)
  1951. {
  1952. const struct vgic_its_abi *abi = vgic_its_get_abi(its);
  1953. int l2_start_id = id * (SZ_64K / abi->dte_esz);
  1954. u64 entry = *(u64 *)addr;
  1955. int dte_esz = abi->dte_esz;
  1956. gpa_t gpa;
  1957. int ret;
  1958. entry = le64_to_cpu(entry);
  1959. if (!(entry & KVM_ITS_L1E_VALID_MASK))
  1960. return 1;
  1961. gpa = entry & KVM_ITS_L1E_ADDR_MASK;
  1962. ret = scan_its_table(its, gpa, SZ_64K, dte_esz,
  1963. l2_start_id, vgic_its_restore_dte, NULL);
  1964. return ret;
  1965. }
  1966. /*
  1967. * vgic_its_restore_device_tables - Restore the device table and all ITT
  1968. * from guest RAM to internal data structs
  1969. */
  1970. static int vgic_its_restore_device_tables(struct vgic_its *its)
  1971. {
  1972. const struct vgic_its_abi *abi = vgic_its_get_abi(its);
  1973. u64 baser = its->baser_device_table;
  1974. int l1_esz, ret;
  1975. int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
  1976. gpa_t l1_gpa;
  1977. if (!(baser & GITS_BASER_VALID))
  1978. return 0;
  1979. l1_gpa = GITS_BASER_ADDR_48_to_52(baser);
  1980. if (baser & GITS_BASER_INDIRECT) {
  1981. l1_esz = GITS_LVL1_ENTRY_SIZE;
  1982. ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
  1983. handle_l1_dte, NULL);
  1984. } else {
  1985. l1_esz = abi->dte_esz;
  1986. ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
  1987. vgic_its_restore_dte, NULL);
  1988. }
  1989. /* scan_its_table returns +1 if all entries are invalid */
  1990. if (ret > 0)
  1991. ret = 0;
  1992. if (ret < 0)
  1993. vgic_its_free_device_list(its->dev->kvm, its);
  1994. return ret;
  1995. }
  1996. static int vgic_its_save_cte(struct vgic_its *its,
  1997. struct its_collection *collection,
  1998. gpa_t gpa, int esz)
  1999. {
  2000. u64 val;
  2001. val = (1ULL << KVM_ITS_CTE_VALID_SHIFT |
  2002. ((u64)collection->target_addr << KVM_ITS_CTE_RDBASE_SHIFT) |
  2003. collection->collection_id);
  2004. val = cpu_to_le64(val);
  2005. return vgic_its_write_entry_lock(its, gpa, val, esz);
  2006. }
  2007. /*
  2008. * Restore a collection entry into the ITS collection table.
  2009. * Return +1 on success, 0 if the entry was invalid (which should be
  2010. * interpreted as end-of-table), and a negative error value for generic errors.
  2011. */
  2012. static int vgic_its_restore_cte(struct vgic_its *its, gpa_t gpa, int esz)
  2013. {
  2014. struct its_collection *collection;
  2015. struct kvm *kvm = its->dev->kvm;
  2016. u32 target_addr, coll_id;
  2017. u64 val;
  2018. int ret;
  2019. ret = vgic_its_read_entry_lock(its, gpa, &val, esz);
  2020. if (ret)
  2021. return ret;
  2022. val = le64_to_cpu(val);
  2023. if (!(val & KVM_ITS_CTE_VALID_MASK))
  2024. return 0;
  2025. target_addr = (u32)(val >> KVM_ITS_CTE_RDBASE_SHIFT);
  2026. coll_id = val & KVM_ITS_CTE_ICID_MASK;
  2027. if (target_addr != COLLECTION_NOT_MAPPED &&
  2028. !kvm_get_vcpu_by_id(kvm, target_addr))
  2029. return -EINVAL;
  2030. collection = find_collection(its, coll_id);
  2031. if (collection)
  2032. return -EEXIST;
  2033. if (!vgic_its_check_id(its, its->baser_coll_table, coll_id, NULL))
  2034. return -EINVAL;
  2035. ret = vgic_its_alloc_collection(its, &collection, coll_id);
  2036. if (ret)
  2037. return ret;
  2038. collection->target_addr = target_addr;
  2039. return 1;
  2040. }
  2041. /*
  2042. * vgic_its_save_collection_table - Save the collection table into
  2043. * guest RAM
  2044. */
  2045. static int vgic_its_save_collection_table(struct vgic_its *its)
  2046. {
  2047. const struct vgic_its_abi *abi = vgic_its_get_abi(its);
  2048. u64 baser = its->baser_coll_table;
  2049. gpa_t gpa = GITS_BASER_ADDR_48_to_52(baser);
  2050. struct its_collection *collection;
  2051. size_t max_size, filled = 0;
  2052. int ret, cte_esz = abi->cte_esz;
  2053. if (!(baser & GITS_BASER_VALID))
  2054. return 0;
  2055. max_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
  2056. list_for_each_entry(collection, &its->collection_list, coll_list) {
  2057. ret = vgic_its_save_cte(its, collection, gpa, cte_esz);
  2058. if (ret)
  2059. return ret;
  2060. gpa += cte_esz;
  2061. filled += cte_esz;
  2062. }
  2063. if (filled == max_size)
  2064. return 0;
  2065. /*
  2066. * table is not fully filled, add a last dummy element
  2067. * with valid bit unset
  2068. */
  2069. return vgic_its_write_entry_lock(its, gpa, 0, cte_esz);
  2070. }
  2071. /*
  2072. * vgic_its_restore_collection_table - reads the collection table
  2073. * in guest memory and restores the ITS internal state. Requires the
  2074. * BASER registers to be restored before.
  2075. */
  2076. static int vgic_its_restore_collection_table(struct vgic_its *its)
  2077. {
  2078. const struct vgic_its_abi *abi = vgic_its_get_abi(its);
  2079. u64 baser = its->baser_coll_table;
  2080. int cte_esz = abi->cte_esz;
  2081. size_t max_size, read = 0;
  2082. gpa_t gpa;
  2083. int ret;
  2084. if (!(baser & GITS_BASER_VALID))
  2085. return 0;
  2086. gpa = GITS_BASER_ADDR_48_to_52(baser);
  2087. max_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
  2088. while (read < max_size) {
  2089. ret = vgic_its_restore_cte(its, gpa, cte_esz);
  2090. if (ret <= 0)
  2091. break;
  2092. gpa += cte_esz;
  2093. read += cte_esz;
  2094. }
  2095. if (ret > 0)
  2096. return 0;
  2097. if (ret < 0)
  2098. vgic_its_free_collection_list(its->dev->kvm, its);
  2099. return ret;
  2100. }
  2101. /*
  2102. * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM
  2103. * according to v0 ABI
  2104. */
  2105. static int vgic_its_save_tables_v0(struct vgic_its *its)
  2106. {
  2107. int ret;
  2108. ret = vgic_its_save_device_tables(its);
  2109. if (ret)
  2110. return ret;
  2111. return vgic_its_save_collection_table(its);
  2112. }
  2113. /*
  2114. * vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM
  2115. * to internal data structs according to V0 ABI
  2116. *
  2117. */
  2118. static int vgic_its_restore_tables_v0(struct vgic_its *its)
  2119. {
  2120. int ret;
  2121. ret = vgic_its_restore_collection_table(its);
  2122. if (ret)
  2123. return ret;
  2124. ret = vgic_its_restore_device_tables(its);
  2125. if (ret)
  2126. vgic_its_free_collection_list(its->dev->kvm, its);
  2127. return ret;
  2128. }
  2129. static int vgic_its_commit_v0(struct vgic_its *its)
  2130. {
  2131. const struct vgic_its_abi *abi;
  2132. abi = vgic_its_get_abi(its);
  2133. its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
  2134. its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
  2135. its->baser_coll_table |= (GIC_ENCODE_SZ(abi->cte_esz, 5)
  2136. << GITS_BASER_ENTRY_SIZE_SHIFT);
  2137. its->baser_device_table |= (GIC_ENCODE_SZ(abi->dte_esz, 5)
  2138. << GITS_BASER_ENTRY_SIZE_SHIFT);
  2139. return 0;
  2140. }
  2141. static void vgic_its_reset(struct kvm *kvm, struct vgic_its *its)
  2142. {
  2143. /* We need to keep the ABI specific field values */
  2144. its->baser_coll_table &= ~GITS_BASER_VALID;
  2145. its->baser_device_table &= ~GITS_BASER_VALID;
  2146. its->cbaser = 0;
  2147. its->creadr = 0;
  2148. its->cwriter = 0;
  2149. its->enabled = 0;
  2150. vgic_its_free_device_list(kvm, its);
  2151. vgic_its_free_collection_list(kvm, its);
  2152. }
  2153. static int vgic_its_has_attr(struct kvm_device *dev,
  2154. struct kvm_device_attr *attr)
  2155. {
  2156. switch (attr->group) {
  2157. case KVM_DEV_ARM_VGIC_GRP_ADDR:
  2158. switch (attr->attr) {
  2159. case KVM_VGIC_ITS_ADDR_TYPE:
  2160. return 0;
  2161. }
  2162. break;
  2163. case KVM_DEV_ARM_VGIC_GRP_CTRL:
  2164. switch (attr->attr) {
  2165. case KVM_DEV_ARM_VGIC_CTRL_INIT:
  2166. return 0;
  2167. case KVM_DEV_ARM_ITS_CTRL_RESET:
  2168. return 0;
  2169. case KVM_DEV_ARM_ITS_SAVE_TABLES:
  2170. return 0;
  2171. case KVM_DEV_ARM_ITS_RESTORE_TABLES:
  2172. return 0;
  2173. }
  2174. break;
  2175. case KVM_DEV_ARM_VGIC_GRP_ITS_REGS:
  2176. return vgic_its_has_attr_regs(dev, attr);
  2177. }
  2178. return -ENXIO;
  2179. }
  2180. static int vgic_its_ctrl(struct kvm *kvm, struct vgic_its *its, u64 attr)
  2181. {
  2182. const struct vgic_its_abi *abi = vgic_its_get_abi(its);
  2183. int ret = 0;
  2184. if (attr == KVM_DEV_ARM_VGIC_CTRL_INIT) /* Nothing to do */
  2185. return 0;
  2186. mutex_lock(&kvm->lock);
  2187. if (!lock_all_vcpus(kvm)) {
  2188. mutex_unlock(&kvm->lock);
  2189. return -EBUSY;
  2190. }
  2191. mutex_lock(&kvm->arch.config_lock);
  2192. mutex_lock(&its->its_lock);
  2193. switch (attr) {
  2194. case KVM_DEV_ARM_ITS_CTRL_RESET:
  2195. vgic_its_reset(kvm, its);
  2196. break;
  2197. case KVM_DEV_ARM_ITS_SAVE_TABLES:
  2198. ret = abi->save_tables(its);
  2199. break;
  2200. case KVM_DEV_ARM_ITS_RESTORE_TABLES:
  2201. ret = abi->restore_tables(its);
  2202. break;
  2203. }
  2204. mutex_unlock(&its->its_lock);
  2205. mutex_unlock(&kvm->arch.config_lock);
  2206. unlock_all_vcpus(kvm);
  2207. mutex_unlock(&kvm->lock);
  2208. return ret;
  2209. }
  2210. /*
  2211. * kvm_arch_allow_write_without_running_vcpu - allow writing guest memory
  2212. * without the running VCPU when dirty ring is enabled.
  2213. *
  2214. * The running VCPU is required to track dirty guest pages when dirty ring
  2215. * is enabled. Otherwise, the backup bitmap should be used to track the
  2216. * dirty guest pages. When vgic/its tables are being saved, the backup
  2217. * bitmap is used to track the dirty guest pages due to the missed running
  2218. * VCPU in the period.
  2219. */
  2220. bool kvm_arch_allow_write_without_running_vcpu(struct kvm *kvm)
  2221. {
  2222. struct vgic_dist *dist = &kvm->arch.vgic;
  2223. return dist->table_write_in_progress;
  2224. }
  2225. static int vgic_its_set_attr(struct kvm_device *dev,
  2226. struct kvm_device_attr *attr)
  2227. {
  2228. struct vgic_its *its = dev->private;
  2229. int ret;
  2230. switch (attr->group) {
  2231. case KVM_DEV_ARM_VGIC_GRP_ADDR: {
  2232. u64 __user *uaddr = (u64 __user *)(long)attr->addr;
  2233. unsigned long type = (unsigned long)attr->attr;
  2234. u64 addr;
  2235. if (type != KVM_VGIC_ITS_ADDR_TYPE)
  2236. return -ENODEV;
  2237. if (copy_from_user(&addr, uaddr, sizeof(addr)))
  2238. return -EFAULT;
  2239. ret = vgic_check_iorange(dev->kvm, its->vgic_its_base,
  2240. addr, SZ_64K, KVM_VGIC_V3_ITS_SIZE);
  2241. if (ret)
  2242. return ret;
  2243. return vgic_register_its_iodev(dev->kvm, its, addr);
  2244. }
  2245. case KVM_DEV_ARM_VGIC_GRP_CTRL:
  2246. return vgic_its_ctrl(dev->kvm, its, attr->attr);
  2247. case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
  2248. u64 __user *uaddr = (u64 __user *)(long)attr->addr;
  2249. u64 reg;
  2250. if (get_user(reg, uaddr))
  2251. return -EFAULT;
  2252. return vgic_its_attr_regs_access(dev, attr, &reg, true);
  2253. }
  2254. }
  2255. return -ENXIO;
  2256. }
  2257. static int vgic_its_get_attr(struct kvm_device *dev,
  2258. struct kvm_device_attr *attr)
  2259. {
  2260. switch (attr->group) {
  2261. case KVM_DEV_ARM_VGIC_GRP_ADDR: {
  2262. struct vgic_its *its = dev->private;
  2263. u64 addr = its->vgic_its_base;
  2264. u64 __user *uaddr = (u64 __user *)(long)attr->addr;
  2265. unsigned long type = (unsigned long)attr->attr;
  2266. if (type != KVM_VGIC_ITS_ADDR_TYPE)
  2267. return -ENODEV;
  2268. if (copy_to_user(uaddr, &addr, sizeof(addr)))
  2269. return -EFAULT;
  2270. break;
  2271. }
  2272. case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
  2273. u64 __user *uaddr = (u64 __user *)(long)attr->addr;
  2274. u64 reg;
  2275. int ret;
  2276. ret = vgic_its_attr_regs_access(dev, attr, &reg, false);
  2277. if (ret)
  2278. return ret;
  2279. return put_user(reg, uaddr);
  2280. }
  2281. default:
  2282. return -ENXIO;
  2283. }
  2284. return 0;
  2285. }
  2286. static struct kvm_device_ops kvm_arm_vgic_its_ops = {
  2287. .name = "kvm-arm-vgic-its",
  2288. .create = vgic_its_create,
  2289. .destroy = vgic_its_destroy,
  2290. .set_attr = vgic_its_set_attr,
  2291. .get_attr = vgic_its_get_attr,
  2292. .has_attr = vgic_its_has_attr,
  2293. };
  2294. int kvm_vgic_register_its_device(void)
  2295. {
  2296. return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
  2297. KVM_DEV_TYPE_ARM_VGIC_ITS);
  2298. }