vgic-its.c 72 KB

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