vgic-its.c 63 KB

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