vgic-its.c 65 KB

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