vgic-its.c 64 KB

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