irq-gic-v3-its.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880
  1. /*
  2. * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/acpi.h>
  18. #include <linux/acpi_iort.h>
  19. #include <linux/bitmap.h>
  20. #include <linux/cpu.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-iommu.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irqdomain.h>
  25. #include <linux/list.h>
  26. #include <linux/list_sort.h>
  27. #include <linux/log2.h>
  28. #include <linux/mm.h>
  29. #include <linux/msi.h>
  30. #include <linux/of.h>
  31. #include <linux/of_address.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/of_pci.h>
  34. #include <linux/of_platform.h>
  35. #include <linux/percpu.h>
  36. #include <linux/slab.h>
  37. #include <linux/syscore_ops.h>
  38. #include <linux/irqchip.h>
  39. #include <linux/irqchip/arm-gic-v3.h>
  40. #include <linux/irqchip/arm-gic-v4.h>
  41. #include <asm/cputype.h>
  42. #include <asm/exception.h>
  43. #include "irq-gic-common.h"
  44. #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
  45. #define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
  46. #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
  47. #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
  48. static u32 lpi_id_bits;
  49. /*
  50. * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
  51. * deal with (one configuration byte per interrupt). PENDBASE has to
  52. * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
  53. */
  54. #define LPI_NRBITS lpi_id_bits
  55. #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
  56. #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
  57. #define LPI_PROP_DEFAULT_PRIO 0xa0
  58. /*
  59. * Collection structure - just an ID, and a redistributor address to
  60. * ping. We use one per CPU as a bag of interrupts assigned to this
  61. * CPU.
  62. */
  63. struct its_collection {
  64. u64 target_address;
  65. u16 col_id;
  66. };
  67. /*
  68. * The ITS_BASER structure - contains memory information, cached
  69. * value of BASER register configuration and ITS page size.
  70. */
  71. struct its_baser {
  72. void *base;
  73. u64 val;
  74. u32 order;
  75. u32 psz;
  76. };
  77. struct its_device;
  78. /*
  79. * The ITS structure - contains most of the infrastructure, with the
  80. * top-level MSI domain, the command queue, the collections, and the
  81. * list of devices writing to it.
  82. *
  83. * dev_alloc_lock has to be taken for device allocations, while the
  84. * spinlock must be taken to parse data structures such as the device
  85. * list.
  86. */
  87. struct its_node {
  88. raw_spinlock_t lock;
  89. struct mutex dev_alloc_lock;
  90. struct list_head entry;
  91. void __iomem *base;
  92. phys_addr_t phys_base;
  93. struct its_cmd_block *cmd_base;
  94. struct its_cmd_block *cmd_write;
  95. struct its_baser tables[GITS_BASER_NR_REGS];
  96. struct its_collection *collections;
  97. struct fwnode_handle *fwnode_handle;
  98. u64 (*get_msi_base)(struct its_device *its_dev);
  99. u64 cbaser_save;
  100. u32 ctlr_save;
  101. struct list_head its_device_list;
  102. u64 flags;
  103. unsigned long list_nr;
  104. u32 ite_size;
  105. u32 device_ids;
  106. int numa_node;
  107. unsigned int msi_domain_flags;
  108. u32 pre_its_base; /* for Socionext Synquacer */
  109. bool is_v4;
  110. int vlpi_redist_offset;
  111. };
  112. #define ITS_ITT_ALIGN SZ_256
  113. /* The maximum number of VPEID bits supported by VLPI commands */
  114. #define ITS_MAX_VPEID_BITS (16)
  115. #define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
  116. /* Convert page order to size in bytes */
  117. #define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
  118. struct event_lpi_map {
  119. unsigned long *lpi_map;
  120. u16 *col_map;
  121. irq_hw_number_t lpi_base;
  122. int nr_lpis;
  123. struct mutex vlpi_lock;
  124. struct its_vm *vm;
  125. struct its_vlpi_map *vlpi_maps;
  126. int nr_vlpis;
  127. };
  128. /*
  129. * The ITS view of a device - belongs to an ITS, owns an interrupt
  130. * translation table, and a list of interrupts. If it some of its
  131. * LPIs are injected into a guest (GICv4), the event_map.vm field
  132. * indicates which one.
  133. */
  134. struct its_device {
  135. struct list_head entry;
  136. struct its_node *its;
  137. struct event_lpi_map event_map;
  138. void *itt;
  139. u32 nr_ites;
  140. u32 device_id;
  141. bool shared;
  142. };
  143. static struct {
  144. raw_spinlock_t lock;
  145. struct its_device *dev;
  146. struct its_vpe **vpes;
  147. int next_victim;
  148. } vpe_proxy;
  149. static LIST_HEAD(its_nodes);
  150. static DEFINE_RAW_SPINLOCK(its_lock);
  151. static struct rdists *gic_rdists;
  152. static struct irq_domain *its_parent;
  153. static unsigned long its_list_map;
  154. static u16 vmovp_seq_num;
  155. static DEFINE_RAW_SPINLOCK(vmovp_lock);
  156. static DEFINE_IDA(its_vpeid_ida);
  157. #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
  158. #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
  159. #define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
  160. static u16 get_its_list(struct its_vm *vm)
  161. {
  162. struct its_node *its;
  163. unsigned long its_list = 0;
  164. list_for_each_entry(its, &its_nodes, entry) {
  165. if (!its->is_v4)
  166. continue;
  167. if (vm->vlpi_count[its->list_nr])
  168. __set_bit(its->list_nr, &its_list);
  169. }
  170. return (u16)its_list;
  171. }
  172. static struct its_collection *dev_event_to_col(struct its_device *its_dev,
  173. u32 event)
  174. {
  175. struct its_node *its = its_dev->its;
  176. return its->collections + its_dev->event_map.col_map[event];
  177. }
  178. static struct its_collection *valid_col(struct its_collection *col)
  179. {
  180. if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(15, 0)))
  181. return NULL;
  182. return col;
  183. }
  184. static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
  185. {
  186. if (valid_col(its->collections + vpe->col_idx))
  187. return vpe;
  188. return NULL;
  189. }
  190. /*
  191. * ITS command descriptors - parameters to be encoded in a command
  192. * block.
  193. */
  194. struct its_cmd_desc {
  195. union {
  196. struct {
  197. struct its_device *dev;
  198. u32 event_id;
  199. } its_inv_cmd;
  200. struct {
  201. struct its_device *dev;
  202. u32 event_id;
  203. } its_clear_cmd;
  204. struct {
  205. struct its_device *dev;
  206. u32 event_id;
  207. } its_int_cmd;
  208. struct {
  209. struct its_device *dev;
  210. int valid;
  211. } its_mapd_cmd;
  212. struct {
  213. struct its_collection *col;
  214. int valid;
  215. } its_mapc_cmd;
  216. struct {
  217. struct its_device *dev;
  218. u32 phys_id;
  219. u32 event_id;
  220. } its_mapti_cmd;
  221. struct {
  222. struct its_device *dev;
  223. struct its_collection *col;
  224. u32 event_id;
  225. } its_movi_cmd;
  226. struct {
  227. struct its_device *dev;
  228. u32 event_id;
  229. } its_discard_cmd;
  230. struct {
  231. struct its_collection *col;
  232. } its_invall_cmd;
  233. struct {
  234. struct its_vpe *vpe;
  235. } its_vinvall_cmd;
  236. struct {
  237. struct its_vpe *vpe;
  238. struct its_collection *col;
  239. bool valid;
  240. } its_vmapp_cmd;
  241. struct {
  242. struct its_vpe *vpe;
  243. struct its_device *dev;
  244. u32 virt_id;
  245. u32 event_id;
  246. bool db_enabled;
  247. } its_vmapti_cmd;
  248. struct {
  249. struct its_vpe *vpe;
  250. struct its_device *dev;
  251. u32 event_id;
  252. bool db_enabled;
  253. } its_vmovi_cmd;
  254. struct {
  255. struct its_vpe *vpe;
  256. struct its_collection *col;
  257. u16 seq_num;
  258. u16 its_list;
  259. } its_vmovp_cmd;
  260. };
  261. };
  262. /*
  263. * The ITS command block, which is what the ITS actually parses.
  264. */
  265. struct its_cmd_block {
  266. u64 raw_cmd[4];
  267. };
  268. #define ITS_CMD_QUEUE_SZ SZ_64K
  269. #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
  270. typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
  271. struct its_cmd_block *,
  272. struct its_cmd_desc *);
  273. typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
  274. struct its_cmd_block *,
  275. struct its_cmd_desc *);
  276. static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
  277. {
  278. u64 mask = GENMASK_ULL(h, l);
  279. *raw_cmd &= ~mask;
  280. *raw_cmd |= (val << l) & mask;
  281. }
  282. static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
  283. {
  284. its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
  285. }
  286. static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
  287. {
  288. its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
  289. }
  290. static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
  291. {
  292. its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
  293. }
  294. static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
  295. {
  296. its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
  297. }
  298. static void its_encode_size(struct its_cmd_block *cmd, u8 size)
  299. {
  300. its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
  301. }
  302. static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
  303. {
  304. its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
  305. }
  306. static void its_encode_valid(struct its_cmd_block *cmd, int valid)
  307. {
  308. its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
  309. }
  310. static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
  311. {
  312. its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
  313. }
  314. static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
  315. {
  316. its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
  317. }
  318. static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
  319. {
  320. its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
  321. }
  322. static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
  323. {
  324. its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
  325. }
  326. static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
  327. {
  328. its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
  329. }
  330. static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
  331. {
  332. its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
  333. }
  334. static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
  335. {
  336. its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
  337. }
  338. static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
  339. {
  340. its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
  341. }
  342. static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
  343. {
  344. its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
  345. }
  346. static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
  347. {
  348. its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
  349. }
  350. static inline void its_fixup_cmd(struct its_cmd_block *cmd)
  351. {
  352. /* Let's fixup BE commands */
  353. cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
  354. cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
  355. cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
  356. cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
  357. }
  358. static struct its_collection *its_build_mapd_cmd(struct its_node *its,
  359. struct its_cmd_block *cmd,
  360. struct its_cmd_desc *desc)
  361. {
  362. unsigned long itt_addr;
  363. u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
  364. itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
  365. itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
  366. its_encode_cmd(cmd, GITS_CMD_MAPD);
  367. its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
  368. its_encode_size(cmd, size - 1);
  369. its_encode_itt(cmd, itt_addr);
  370. its_encode_valid(cmd, desc->its_mapd_cmd.valid);
  371. its_fixup_cmd(cmd);
  372. return NULL;
  373. }
  374. static struct its_collection *its_build_mapc_cmd(struct its_node *its,
  375. struct its_cmd_block *cmd,
  376. struct its_cmd_desc *desc)
  377. {
  378. its_encode_cmd(cmd, GITS_CMD_MAPC);
  379. its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
  380. its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
  381. its_encode_valid(cmd, desc->its_mapc_cmd.valid);
  382. its_fixup_cmd(cmd);
  383. return desc->its_mapc_cmd.col;
  384. }
  385. static struct its_collection *its_build_mapti_cmd(struct its_node *its,
  386. struct its_cmd_block *cmd,
  387. struct its_cmd_desc *desc)
  388. {
  389. struct its_collection *col;
  390. col = dev_event_to_col(desc->its_mapti_cmd.dev,
  391. desc->its_mapti_cmd.event_id);
  392. its_encode_cmd(cmd, GITS_CMD_MAPTI);
  393. its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
  394. its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
  395. its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
  396. its_encode_collection(cmd, col->col_id);
  397. its_fixup_cmd(cmd);
  398. return valid_col(col);
  399. }
  400. static struct its_collection *its_build_movi_cmd(struct its_node *its,
  401. struct its_cmd_block *cmd,
  402. struct its_cmd_desc *desc)
  403. {
  404. struct its_collection *col;
  405. col = dev_event_to_col(desc->its_movi_cmd.dev,
  406. desc->its_movi_cmd.event_id);
  407. its_encode_cmd(cmd, GITS_CMD_MOVI);
  408. its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
  409. its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
  410. its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
  411. its_fixup_cmd(cmd);
  412. return valid_col(col);
  413. }
  414. static struct its_collection *its_build_discard_cmd(struct its_node *its,
  415. struct its_cmd_block *cmd,
  416. struct its_cmd_desc *desc)
  417. {
  418. struct its_collection *col;
  419. col = dev_event_to_col(desc->its_discard_cmd.dev,
  420. desc->its_discard_cmd.event_id);
  421. its_encode_cmd(cmd, GITS_CMD_DISCARD);
  422. its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
  423. its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
  424. its_fixup_cmd(cmd);
  425. return valid_col(col);
  426. }
  427. static struct its_collection *its_build_inv_cmd(struct its_node *its,
  428. struct its_cmd_block *cmd,
  429. struct its_cmd_desc *desc)
  430. {
  431. struct its_collection *col;
  432. col = dev_event_to_col(desc->its_inv_cmd.dev,
  433. desc->its_inv_cmd.event_id);
  434. its_encode_cmd(cmd, GITS_CMD_INV);
  435. its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
  436. its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
  437. its_fixup_cmd(cmd);
  438. return valid_col(col);
  439. }
  440. static struct its_collection *its_build_int_cmd(struct its_node *its,
  441. struct its_cmd_block *cmd,
  442. struct its_cmd_desc *desc)
  443. {
  444. struct its_collection *col;
  445. col = dev_event_to_col(desc->its_int_cmd.dev,
  446. desc->its_int_cmd.event_id);
  447. its_encode_cmd(cmd, GITS_CMD_INT);
  448. its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
  449. its_encode_event_id(cmd, desc->its_int_cmd.event_id);
  450. its_fixup_cmd(cmd);
  451. return valid_col(col);
  452. }
  453. static struct its_collection *its_build_clear_cmd(struct its_node *its,
  454. struct its_cmd_block *cmd,
  455. struct its_cmd_desc *desc)
  456. {
  457. struct its_collection *col;
  458. col = dev_event_to_col(desc->its_clear_cmd.dev,
  459. desc->its_clear_cmd.event_id);
  460. its_encode_cmd(cmd, GITS_CMD_CLEAR);
  461. its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
  462. its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
  463. its_fixup_cmd(cmd);
  464. return valid_col(col);
  465. }
  466. static struct its_collection *its_build_invall_cmd(struct its_node *its,
  467. struct its_cmd_block *cmd,
  468. struct its_cmd_desc *desc)
  469. {
  470. its_encode_cmd(cmd, GITS_CMD_INVALL);
  471. its_encode_collection(cmd, desc->its_invall_cmd.col->col_id);
  472. its_fixup_cmd(cmd);
  473. return NULL;
  474. }
  475. static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
  476. struct its_cmd_block *cmd,
  477. struct its_cmd_desc *desc)
  478. {
  479. its_encode_cmd(cmd, GITS_CMD_VINVALL);
  480. its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
  481. its_fixup_cmd(cmd);
  482. return valid_vpe(its, desc->its_vinvall_cmd.vpe);
  483. }
  484. static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
  485. struct its_cmd_block *cmd,
  486. struct its_cmd_desc *desc)
  487. {
  488. unsigned long vpt_addr;
  489. u64 target;
  490. vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
  491. target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
  492. its_encode_cmd(cmd, GITS_CMD_VMAPP);
  493. its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
  494. its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
  495. its_encode_target(cmd, target);
  496. its_encode_vpt_addr(cmd, vpt_addr);
  497. its_encode_vpt_size(cmd, LPI_NRBITS - 1);
  498. its_fixup_cmd(cmd);
  499. return valid_vpe(its, desc->its_vmapp_cmd.vpe);
  500. }
  501. static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
  502. struct its_cmd_block *cmd,
  503. struct its_cmd_desc *desc)
  504. {
  505. u32 db;
  506. if (desc->its_vmapti_cmd.db_enabled)
  507. db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
  508. else
  509. db = 1023;
  510. its_encode_cmd(cmd, GITS_CMD_VMAPTI);
  511. its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
  512. its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
  513. its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
  514. its_encode_db_phys_id(cmd, db);
  515. its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
  516. its_fixup_cmd(cmd);
  517. return valid_vpe(its, desc->its_vmapti_cmd.vpe);
  518. }
  519. static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
  520. struct its_cmd_block *cmd,
  521. struct its_cmd_desc *desc)
  522. {
  523. u32 db;
  524. if (desc->its_vmovi_cmd.db_enabled)
  525. db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
  526. else
  527. db = 1023;
  528. its_encode_cmd(cmd, GITS_CMD_VMOVI);
  529. its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
  530. its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
  531. its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
  532. its_encode_db_phys_id(cmd, db);
  533. its_encode_db_valid(cmd, true);
  534. its_fixup_cmd(cmd);
  535. return valid_vpe(its, desc->its_vmovi_cmd.vpe);
  536. }
  537. static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
  538. struct its_cmd_block *cmd,
  539. struct its_cmd_desc *desc)
  540. {
  541. u64 target;
  542. target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
  543. its_encode_cmd(cmd, GITS_CMD_VMOVP);
  544. its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
  545. its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
  546. its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
  547. its_encode_target(cmd, target);
  548. its_fixup_cmd(cmd);
  549. return valid_vpe(its, desc->its_vmovp_cmd.vpe);
  550. }
  551. static u64 its_cmd_ptr_to_offset(struct its_node *its,
  552. struct its_cmd_block *ptr)
  553. {
  554. return (ptr - its->cmd_base) * sizeof(*ptr);
  555. }
  556. static int its_queue_full(struct its_node *its)
  557. {
  558. int widx;
  559. int ridx;
  560. widx = its->cmd_write - its->cmd_base;
  561. ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
  562. /* This is incredibly unlikely to happen, unless the ITS locks up. */
  563. if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
  564. return 1;
  565. return 0;
  566. }
  567. static struct its_cmd_block *its_allocate_entry(struct its_node *its)
  568. {
  569. struct its_cmd_block *cmd;
  570. u32 count = 1000000; /* 1s! */
  571. while (its_queue_full(its)) {
  572. count--;
  573. if (!count) {
  574. pr_err_ratelimited("ITS queue not draining\n");
  575. return NULL;
  576. }
  577. cpu_relax();
  578. udelay(1);
  579. }
  580. cmd = its->cmd_write++;
  581. /* Handle queue wrapping */
  582. if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
  583. its->cmd_write = its->cmd_base;
  584. /* Clear command */
  585. cmd->raw_cmd[0] = 0;
  586. cmd->raw_cmd[1] = 0;
  587. cmd->raw_cmd[2] = 0;
  588. cmd->raw_cmd[3] = 0;
  589. return cmd;
  590. }
  591. static struct its_cmd_block *its_post_commands(struct its_node *its)
  592. {
  593. u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
  594. writel_relaxed(wr, its->base + GITS_CWRITER);
  595. return its->cmd_write;
  596. }
  597. static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
  598. {
  599. /*
  600. * Make sure the commands written to memory are observable by
  601. * the ITS.
  602. */
  603. if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
  604. gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
  605. else
  606. dsb(ishst);
  607. }
  608. static int its_wait_for_range_completion(struct its_node *its,
  609. u64 prev_idx,
  610. struct its_cmd_block *to)
  611. {
  612. u64 rd_idx, to_idx, linear_idx;
  613. u32 count = 1000000; /* 1s! */
  614. /* Linearize to_idx if the command set has wrapped around */
  615. to_idx = its_cmd_ptr_to_offset(its, to);
  616. if (to_idx < prev_idx)
  617. to_idx += ITS_CMD_QUEUE_SZ;
  618. linear_idx = prev_idx;
  619. while (1) {
  620. s64 delta;
  621. rd_idx = readl_relaxed(its->base + GITS_CREADR);
  622. /*
  623. * Compute the read pointer progress, taking the
  624. * potential wrap-around into account.
  625. */
  626. delta = rd_idx - prev_idx;
  627. if (rd_idx < prev_idx)
  628. delta += ITS_CMD_QUEUE_SZ;
  629. linear_idx += delta;
  630. if (linear_idx >= to_idx)
  631. break;
  632. count--;
  633. if (!count) {
  634. pr_err_ratelimited("ITS queue timeout (%llu %llu)\n",
  635. to_idx, linear_idx);
  636. return -1;
  637. }
  638. prev_idx = rd_idx;
  639. cpu_relax();
  640. udelay(1);
  641. }
  642. return 0;
  643. }
  644. /* Warning, macro hell follows */
  645. #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
  646. void name(struct its_node *its, \
  647. buildtype builder, \
  648. struct its_cmd_desc *desc) \
  649. { \
  650. struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
  651. synctype *sync_obj; \
  652. unsigned long flags; \
  653. u64 rd_idx; \
  654. \
  655. raw_spin_lock_irqsave(&its->lock, flags); \
  656. \
  657. cmd = its_allocate_entry(its); \
  658. if (!cmd) { /* We're soooooo screewed... */ \
  659. raw_spin_unlock_irqrestore(&its->lock, flags); \
  660. return; \
  661. } \
  662. sync_obj = builder(its, cmd, desc); \
  663. its_flush_cmd(its, cmd); \
  664. \
  665. if (sync_obj) { \
  666. sync_cmd = its_allocate_entry(its); \
  667. if (!sync_cmd) \
  668. goto post; \
  669. \
  670. buildfn(its, sync_cmd, sync_obj); \
  671. its_flush_cmd(its, sync_cmd); \
  672. } \
  673. \
  674. post: \
  675. rd_idx = readl_relaxed(its->base + GITS_CREADR); \
  676. next_cmd = its_post_commands(its); \
  677. raw_spin_unlock_irqrestore(&its->lock, flags); \
  678. \
  679. if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \
  680. pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
  681. }
  682. static void its_build_sync_cmd(struct its_node *its,
  683. struct its_cmd_block *sync_cmd,
  684. struct its_collection *sync_col)
  685. {
  686. its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
  687. its_encode_target(sync_cmd, sync_col->target_address);
  688. its_fixup_cmd(sync_cmd);
  689. }
  690. static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
  691. struct its_collection, its_build_sync_cmd)
  692. static void its_build_vsync_cmd(struct its_node *its,
  693. struct its_cmd_block *sync_cmd,
  694. struct its_vpe *sync_vpe)
  695. {
  696. its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
  697. its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
  698. its_fixup_cmd(sync_cmd);
  699. }
  700. static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
  701. struct its_vpe, its_build_vsync_cmd)
  702. static void its_send_int(struct its_device *dev, u32 event_id)
  703. {
  704. struct its_cmd_desc desc;
  705. desc.its_int_cmd.dev = dev;
  706. desc.its_int_cmd.event_id = event_id;
  707. its_send_single_command(dev->its, its_build_int_cmd, &desc);
  708. }
  709. static void its_send_clear(struct its_device *dev, u32 event_id)
  710. {
  711. struct its_cmd_desc desc;
  712. desc.its_clear_cmd.dev = dev;
  713. desc.its_clear_cmd.event_id = event_id;
  714. its_send_single_command(dev->its, its_build_clear_cmd, &desc);
  715. }
  716. static void its_send_inv(struct its_device *dev, u32 event_id)
  717. {
  718. struct its_cmd_desc desc;
  719. desc.its_inv_cmd.dev = dev;
  720. desc.its_inv_cmd.event_id = event_id;
  721. its_send_single_command(dev->its, its_build_inv_cmd, &desc);
  722. }
  723. static void its_send_mapd(struct its_device *dev, int valid)
  724. {
  725. struct its_cmd_desc desc;
  726. desc.its_mapd_cmd.dev = dev;
  727. desc.its_mapd_cmd.valid = !!valid;
  728. its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
  729. }
  730. static void its_send_mapc(struct its_node *its, struct its_collection *col,
  731. int valid)
  732. {
  733. struct its_cmd_desc desc;
  734. desc.its_mapc_cmd.col = col;
  735. desc.its_mapc_cmd.valid = !!valid;
  736. its_send_single_command(its, its_build_mapc_cmd, &desc);
  737. }
  738. static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
  739. {
  740. struct its_cmd_desc desc;
  741. desc.its_mapti_cmd.dev = dev;
  742. desc.its_mapti_cmd.phys_id = irq_id;
  743. desc.its_mapti_cmd.event_id = id;
  744. its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
  745. }
  746. static void its_send_movi(struct its_device *dev,
  747. struct its_collection *col, u32 id)
  748. {
  749. struct its_cmd_desc desc;
  750. desc.its_movi_cmd.dev = dev;
  751. desc.its_movi_cmd.col = col;
  752. desc.its_movi_cmd.event_id = id;
  753. its_send_single_command(dev->its, its_build_movi_cmd, &desc);
  754. }
  755. static void its_send_discard(struct its_device *dev, u32 id)
  756. {
  757. struct its_cmd_desc desc;
  758. desc.its_discard_cmd.dev = dev;
  759. desc.its_discard_cmd.event_id = id;
  760. its_send_single_command(dev->its, its_build_discard_cmd, &desc);
  761. }
  762. static void its_send_invall(struct its_node *its, struct its_collection *col)
  763. {
  764. struct its_cmd_desc desc;
  765. desc.its_invall_cmd.col = col;
  766. its_send_single_command(its, its_build_invall_cmd, &desc);
  767. }
  768. static void its_send_vmapti(struct its_device *dev, u32 id)
  769. {
  770. struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
  771. struct its_cmd_desc desc;
  772. desc.its_vmapti_cmd.vpe = map->vpe;
  773. desc.its_vmapti_cmd.dev = dev;
  774. desc.its_vmapti_cmd.virt_id = map->vintid;
  775. desc.its_vmapti_cmd.event_id = id;
  776. desc.its_vmapti_cmd.db_enabled = map->db_enabled;
  777. its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
  778. }
  779. static void its_send_vmovi(struct its_device *dev, u32 id)
  780. {
  781. struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
  782. struct its_cmd_desc desc;
  783. desc.its_vmovi_cmd.vpe = map->vpe;
  784. desc.its_vmovi_cmd.dev = dev;
  785. desc.its_vmovi_cmd.event_id = id;
  786. desc.its_vmovi_cmd.db_enabled = map->db_enabled;
  787. its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
  788. }
  789. static void its_send_vmapp(struct its_node *its,
  790. struct its_vpe *vpe, bool valid)
  791. {
  792. struct its_cmd_desc desc;
  793. desc.its_vmapp_cmd.vpe = vpe;
  794. desc.its_vmapp_cmd.valid = valid;
  795. desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
  796. its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
  797. }
  798. static void its_send_vmovp(struct its_vpe *vpe)
  799. {
  800. struct its_cmd_desc desc = {};
  801. struct its_node *its;
  802. unsigned long flags;
  803. int col_id = vpe->col_idx;
  804. desc.its_vmovp_cmd.vpe = vpe;
  805. if (!its_list_map) {
  806. its = list_first_entry(&its_nodes, struct its_node, entry);
  807. desc.its_vmovp_cmd.col = &its->collections[col_id];
  808. its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
  809. return;
  810. }
  811. /*
  812. * Yet another marvel of the architecture. If using the
  813. * its_list "feature", we need to make sure that all ITSs
  814. * receive all VMOVP commands in the same order. The only way
  815. * to guarantee this is to make vmovp a serialization point.
  816. *
  817. * Wall <-- Head.
  818. */
  819. raw_spin_lock_irqsave(&vmovp_lock, flags);
  820. desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
  821. desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm);
  822. /* Emit VMOVPs */
  823. list_for_each_entry(its, &its_nodes, entry) {
  824. if (!its->is_v4)
  825. continue;
  826. if (!vpe->its_vm->vlpi_count[its->list_nr])
  827. continue;
  828. desc.its_vmovp_cmd.col = &its->collections[col_id];
  829. its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
  830. }
  831. raw_spin_unlock_irqrestore(&vmovp_lock, flags);
  832. }
  833. static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
  834. {
  835. struct its_cmd_desc desc;
  836. desc.its_vinvall_cmd.vpe = vpe;
  837. its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
  838. }
  839. /*
  840. * irqchip functions - assumes MSI, mostly.
  841. */
  842. static inline u32 its_get_event_id(struct irq_data *d)
  843. {
  844. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  845. return d->hwirq - its_dev->event_map.lpi_base;
  846. }
  847. static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
  848. {
  849. irq_hw_number_t hwirq;
  850. struct page *prop_page;
  851. u8 *cfg;
  852. if (irqd_is_forwarded_to_vcpu(d)) {
  853. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  854. u32 event = its_get_event_id(d);
  855. struct its_vlpi_map *map;
  856. prop_page = its_dev->event_map.vm->vprop_page;
  857. map = &its_dev->event_map.vlpi_maps[event];
  858. hwirq = map->vintid;
  859. /* Remember the updated property */
  860. map->properties &= ~clr;
  861. map->properties |= set | LPI_PROP_GROUP1;
  862. } else {
  863. prop_page = gic_rdists->prop_page;
  864. hwirq = d->hwirq;
  865. }
  866. cfg = page_address(prop_page) + hwirq - 8192;
  867. *cfg &= ~clr;
  868. *cfg |= set | LPI_PROP_GROUP1;
  869. /*
  870. * Make the above write visible to the redistributors.
  871. * And yes, we're flushing exactly: One. Single. Byte.
  872. * Humpf...
  873. */
  874. if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
  875. gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
  876. else
  877. dsb(ishst);
  878. }
  879. static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
  880. {
  881. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  882. lpi_write_config(d, clr, set);
  883. its_send_inv(its_dev, its_get_event_id(d));
  884. }
  885. static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
  886. {
  887. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  888. u32 event = its_get_event_id(d);
  889. if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
  890. return;
  891. its_dev->event_map.vlpi_maps[event].db_enabled = enable;
  892. /*
  893. * More fun with the architecture:
  894. *
  895. * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
  896. * value or to 1023, depending on the enable bit. But that
  897. * would be issueing a mapping for an /existing/ DevID+EventID
  898. * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
  899. * to the /same/ vPE, using this opportunity to adjust the
  900. * doorbell. Mouahahahaha. We loves it, Precious.
  901. */
  902. its_send_vmovi(its_dev, event);
  903. }
  904. static void its_mask_irq(struct irq_data *d)
  905. {
  906. if (irqd_is_forwarded_to_vcpu(d))
  907. its_vlpi_set_doorbell(d, false);
  908. lpi_update_config(d, LPI_PROP_ENABLED, 0);
  909. }
  910. static void its_unmask_irq(struct irq_data *d)
  911. {
  912. if (irqd_is_forwarded_to_vcpu(d))
  913. its_vlpi_set_doorbell(d, true);
  914. lpi_update_config(d, 0, LPI_PROP_ENABLED);
  915. }
  916. static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  917. bool force)
  918. {
  919. unsigned int cpu;
  920. const struct cpumask *cpu_mask = cpu_online_mask;
  921. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  922. struct its_collection *target_col;
  923. u32 id = its_get_event_id(d);
  924. /* A forwarded interrupt should use irq_set_vcpu_affinity */
  925. if (irqd_is_forwarded_to_vcpu(d))
  926. return -EINVAL;
  927. /* lpi cannot be routed to a redistributor that is on a foreign node */
  928. if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
  929. if (its_dev->its->numa_node >= 0) {
  930. cpu_mask = cpumask_of_node(its_dev->its->numa_node);
  931. if (!cpumask_intersects(mask_val, cpu_mask))
  932. return -EINVAL;
  933. }
  934. }
  935. cpu = cpumask_any_and(mask_val, cpu_mask);
  936. if (cpu >= nr_cpu_ids)
  937. return -EINVAL;
  938. /* don't set the affinity when the target cpu is same as current one */
  939. if (cpu != its_dev->event_map.col_map[id]) {
  940. target_col = &its_dev->its->collections[cpu];
  941. its_send_movi(its_dev, target_col, id);
  942. its_dev->event_map.col_map[id] = cpu;
  943. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  944. }
  945. return IRQ_SET_MASK_OK_DONE;
  946. }
  947. static u64 its_irq_get_msi_base(struct its_device *its_dev)
  948. {
  949. struct its_node *its = its_dev->its;
  950. return its->phys_base + GITS_TRANSLATER;
  951. }
  952. static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
  953. {
  954. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  955. struct its_node *its;
  956. u64 addr;
  957. its = its_dev->its;
  958. addr = its->get_msi_base(its_dev);
  959. msg->address_lo = lower_32_bits(addr);
  960. msg->address_hi = upper_32_bits(addr);
  961. msg->data = its_get_event_id(d);
  962. iommu_dma_map_msi_msg(d->irq, msg);
  963. }
  964. static int its_irq_set_irqchip_state(struct irq_data *d,
  965. enum irqchip_irq_state which,
  966. bool state)
  967. {
  968. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  969. u32 event = its_get_event_id(d);
  970. if (which != IRQCHIP_STATE_PENDING)
  971. return -EINVAL;
  972. if (state)
  973. its_send_int(its_dev, event);
  974. else
  975. its_send_clear(its_dev, event);
  976. return 0;
  977. }
  978. static void its_map_vm(struct its_node *its, struct its_vm *vm)
  979. {
  980. unsigned long flags;
  981. /* Not using the ITS list? Everything is always mapped. */
  982. if (!its_list_map)
  983. return;
  984. raw_spin_lock_irqsave(&vmovp_lock, flags);
  985. /*
  986. * If the VM wasn't mapped yet, iterate over the vpes and get
  987. * them mapped now.
  988. */
  989. vm->vlpi_count[its->list_nr]++;
  990. if (vm->vlpi_count[its->list_nr] == 1) {
  991. int i;
  992. for (i = 0; i < vm->nr_vpes; i++) {
  993. struct its_vpe *vpe = vm->vpes[i];
  994. struct irq_data *d = irq_get_irq_data(vpe->irq);
  995. /* Map the VPE to the first possible CPU */
  996. vpe->col_idx = cpumask_first(cpu_online_mask);
  997. its_send_vmapp(its, vpe, true);
  998. its_send_vinvall(its, vpe);
  999. irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
  1000. }
  1001. }
  1002. raw_spin_unlock_irqrestore(&vmovp_lock, flags);
  1003. }
  1004. static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
  1005. {
  1006. unsigned long flags;
  1007. /* Not using the ITS list? Everything is always mapped. */
  1008. if (!its_list_map)
  1009. return;
  1010. raw_spin_lock_irqsave(&vmovp_lock, flags);
  1011. if (!--vm->vlpi_count[its->list_nr]) {
  1012. int i;
  1013. for (i = 0; i < vm->nr_vpes; i++)
  1014. its_send_vmapp(its, vm->vpes[i], false);
  1015. }
  1016. raw_spin_unlock_irqrestore(&vmovp_lock, flags);
  1017. }
  1018. static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
  1019. {
  1020. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1021. u32 event = its_get_event_id(d);
  1022. int ret = 0;
  1023. if (!info->map)
  1024. return -EINVAL;
  1025. mutex_lock(&its_dev->event_map.vlpi_lock);
  1026. if (!its_dev->event_map.vm) {
  1027. struct its_vlpi_map *maps;
  1028. maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
  1029. GFP_KERNEL);
  1030. if (!maps) {
  1031. ret = -ENOMEM;
  1032. goto out;
  1033. }
  1034. its_dev->event_map.vm = info->map->vm;
  1035. its_dev->event_map.vlpi_maps = maps;
  1036. } else if (its_dev->event_map.vm != info->map->vm) {
  1037. ret = -EINVAL;
  1038. goto out;
  1039. }
  1040. /* Get our private copy of the mapping information */
  1041. its_dev->event_map.vlpi_maps[event] = *info->map;
  1042. if (irqd_is_forwarded_to_vcpu(d)) {
  1043. /* Already mapped, move it around */
  1044. its_send_vmovi(its_dev, event);
  1045. } else {
  1046. /* Ensure all the VPEs are mapped on this ITS */
  1047. its_map_vm(its_dev->its, info->map->vm);
  1048. /*
  1049. * Flag the interrupt as forwarded so that we can
  1050. * start poking the virtual property table.
  1051. */
  1052. irqd_set_forwarded_to_vcpu(d);
  1053. /* Write out the property to the prop table */
  1054. lpi_write_config(d, 0xff, info->map->properties);
  1055. /* Drop the physical mapping */
  1056. its_send_discard(its_dev, event);
  1057. /* and install the virtual one */
  1058. its_send_vmapti(its_dev, event);
  1059. /* Increment the number of VLPIs */
  1060. its_dev->event_map.nr_vlpis++;
  1061. }
  1062. out:
  1063. mutex_unlock(&its_dev->event_map.vlpi_lock);
  1064. return ret;
  1065. }
  1066. static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
  1067. {
  1068. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1069. u32 event = its_get_event_id(d);
  1070. int ret = 0;
  1071. mutex_lock(&its_dev->event_map.vlpi_lock);
  1072. if (!its_dev->event_map.vm ||
  1073. !its_dev->event_map.vlpi_maps[event].vm) {
  1074. ret = -EINVAL;
  1075. goto out;
  1076. }
  1077. /* Copy our mapping information to the incoming request */
  1078. *info->map = its_dev->event_map.vlpi_maps[event];
  1079. out:
  1080. mutex_unlock(&its_dev->event_map.vlpi_lock);
  1081. return ret;
  1082. }
  1083. static int its_vlpi_unmap(struct irq_data *d)
  1084. {
  1085. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1086. u32 event = its_get_event_id(d);
  1087. int ret = 0;
  1088. mutex_lock(&its_dev->event_map.vlpi_lock);
  1089. if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
  1090. ret = -EINVAL;
  1091. goto out;
  1092. }
  1093. /* Drop the virtual mapping */
  1094. its_send_discard(its_dev, event);
  1095. /* and restore the physical one */
  1096. irqd_clr_forwarded_to_vcpu(d);
  1097. its_send_mapti(its_dev, d->hwirq, event);
  1098. lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
  1099. LPI_PROP_ENABLED |
  1100. LPI_PROP_GROUP1));
  1101. /* Potentially unmap the VM from this ITS */
  1102. its_unmap_vm(its_dev->its, its_dev->event_map.vm);
  1103. /*
  1104. * Drop the refcount and make the device available again if
  1105. * this was the last VLPI.
  1106. */
  1107. if (!--its_dev->event_map.nr_vlpis) {
  1108. its_dev->event_map.vm = NULL;
  1109. kfree(its_dev->event_map.vlpi_maps);
  1110. }
  1111. out:
  1112. mutex_unlock(&its_dev->event_map.vlpi_lock);
  1113. return ret;
  1114. }
  1115. static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
  1116. {
  1117. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1118. if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
  1119. return -EINVAL;
  1120. if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
  1121. lpi_update_config(d, 0xff, info->config);
  1122. else
  1123. lpi_write_config(d, 0xff, info->config);
  1124. its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
  1125. return 0;
  1126. }
  1127. static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
  1128. {
  1129. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1130. struct its_cmd_info *info = vcpu_info;
  1131. /* Need a v4 ITS */
  1132. if (!its_dev->its->is_v4)
  1133. return -EINVAL;
  1134. /* Unmap request? */
  1135. if (!info)
  1136. return its_vlpi_unmap(d);
  1137. switch (info->cmd_type) {
  1138. case MAP_VLPI:
  1139. return its_vlpi_map(d, info);
  1140. case GET_VLPI:
  1141. return its_vlpi_get(d, info);
  1142. case PROP_UPDATE_VLPI:
  1143. case PROP_UPDATE_AND_INV_VLPI:
  1144. return its_vlpi_prop_update(d, info);
  1145. default:
  1146. return -EINVAL;
  1147. }
  1148. }
  1149. static struct irq_chip its_irq_chip = {
  1150. .name = "ITS",
  1151. .irq_mask = its_mask_irq,
  1152. .irq_unmask = its_unmask_irq,
  1153. .irq_eoi = irq_chip_eoi_parent,
  1154. .irq_set_affinity = its_set_affinity,
  1155. .irq_compose_msi_msg = its_irq_compose_msi_msg,
  1156. .irq_set_irqchip_state = its_irq_set_irqchip_state,
  1157. .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
  1158. };
  1159. /*
  1160. * How we allocate LPIs:
  1161. *
  1162. * lpi_range_list contains ranges of LPIs that are to available to
  1163. * allocate from. To allocate LPIs, just pick the first range that
  1164. * fits the required allocation, and reduce it by the required
  1165. * amount. Once empty, remove the range from the list.
  1166. *
  1167. * To free a range of LPIs, add a free range to the list, sort it and
  1168. * merge the result if the new range happens to be adjacent to an
  1169. * already free block.
  1170. *
  1171. * The consequence of the above is that allocation is cost is low, but
  1172. * freeing is expensive. We assumes that freeing rarely occurs.
  1173. */
  1174. #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
  1175. static DEFINE_MUTEX(lpi_range_lock);
  1176. static LIST_HEAD(lpi_range_list);
  1177. struct lpi_range {
  1178. struct list_head entry;
  1179. u32 base_id;
  1180. u32 span;
  1181. };
  1182. static struct lpi_range *mk_lpi_range(u32 base, u32 span)
  1183. {
  1184. struct lpi_range *range;
  1185. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1186. if (range) {
  1187. INIT_LIST_HEAD(&range->entry);
  1188. range->base_id = base;
  1189. range->span = span;
  1190. }
  1191. return range;
  1192. }
  1193. static int lpi_range_cmp(void *priv, struct list_head *a, struct list_head *b)
  1194. {
  1195. struct lpi_range *ra, *rb;
  1196. ra = container_of(a, struct lpi_range, entry);
  1197. rb = container_of(b, struct lpi_range, entry);
  1198. return ra->base_id - rb->base_id;
  1199. }
  1200. static void merge_lpi_ranges(void)
  1201. {
  1202. struct lpi_range *range, *tmp;
  1203. list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
  1204. if (!list_is_last(&range->entry, &lpi_range_list) &&
  1205. (tmp->base_id == (range->base_id + range->span))) {
  1206. tmp->base_id = range->base_id;
  1207. tmp->span += range->span;
  1208. list_del(&range->entry);
  1209. kfree(range);
  1210. }
  1211. }
  1212. }
  1213. static int alloc_lpi_range(u32 nr_lpis, u32 *base)
  1214. {
  1215. struct lpi_range *range, *tmp;
  1216. int err = -ENOSPC;
  1217. mutex_lock(&lpi_range_lock);
  1218. list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
  1219. if (range->span >= nr_lpis) {
  1220. *base = range->base_id;
  1221. range->base_id += nr_lpis;
  1222. range->span -= nr_lpis;
  1223. if (range->span == 0) {
  1224. list_del(&range->entry);
  1225. kfree(range);
  1226. }
  1227. err = 0;
  1228. break;
  1229. }
  1230. }
  1231. mutex_unlock(&lpi_range_lock);
  1232. pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
  1233. return err;
  1234. }
  1235. static int free_lpi_range(u32 base, u32 nr_lpis)
  1236. {
  1237. struct lpi_range *new;
  1238. int err = 0;
  1239. mutex_lock(&lpi_range_lock);
  1240. new = mk_lpi_range(base, nr_lpis);
  1241. if (!new) {
  1242. err = -ENOMEM;
  1243. goto out;
  1244. }
  1245. list_add(&new->entry, &lpi_range_list);
  1246. list_sort(NULL, &lpi_range_list, lpi_range_cmp);
  1247. merge_lpi_ranges();
  1248. out:
  1249. mutex_unlock(&lpi_range_lock);
  1250. return err;
  1251. }
  1252. static int __init its_lpi_init(u32 id_bits)
  1253. {
  1254. u32 lpis = (1UL << id_bits) - 8192;
  1255. u32 numlpis;
  1256. int err;
  1257. numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
  1258. if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
  1259. lpis = numlpis;
  1260. pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
  1261. lpis);
  1262. }
  1263. /*
  1264. * Initializing the allocator is just the same as freeing the
  1265. * full range of LPIs.
  1266. */
  1267. err = free_lpi_range(8192, lpis);
  1268. pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
  1269. return err;
  1270. }
  1271. static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
  1272. {
  1273. unsigned long *bitmap = NULL;
  1274. int err = 0;
  1275. do {
  1276. err = alloc_lpi_range(nr_irqs, base);
  1277. if (!err)
  1278. break;
  1279. nr_irqs /= 2;
  1280. } while (nr_irqs > 0);
  1281. if (!nr_irqs)
  1282. err = -ENOSPC;
  1283. if (err)
  1284. goto out;
  1285. bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC);
  1286. if (!bitmap)
  1287. goto out;
  1288. *nr_ids = nr_irqs;
  1289. out:
  1290. if (!bitmap)
  1291. *base = *nr_ids = 0;
  1292. return bitmap;
  1293. }
  1294. static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
  1295. {
  1296. WARN_ON(free_lpi_range(base, nr_ids));
  1297. kfree(bitmap);
  1298. }
  1299. static struct page *its_allocate_prop_table(gfp_t gfp_flags)
  1300. {
  1301. struct page *prop_page;
  1302. prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
  1303. if (!prop_page)
  1304. return NULL;
  1305. /* Priority 0xa0, Group-1, disabled */
  1306. memset(page_address(prop_page),
  1307. LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
  1308. LPI_PROPBASE_SZ);
  1309. /* Make sure the GIC will observe the written configuration */
  1310. gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
  1311. return prop_page;
  1312. }
  1313. static void its_free_prop_table(struct page *prop_page)
  1314. {
  1315. free_pages((unsigned long)page_address(prop_page),
  1316. get_order(LPI_PROPBASE_SZ));
  1317. }
  1318. static int __init its_alloc_lpi_tables(void)
  1319. {
  1320. phys_addr_t paddr;
  1321. lpi_id_bits = min_t(u32, GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
  1322. ITS_MAX_LPI_NRBITS);
  1323. gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
  1324. if (!gic_rdists->prop_page) {
  1325. pr_err("Failed to allocate PROPBASE\n");
  1326. return -ENOMEM;
  1327. }
  1328. paddr = page_to_phys(gic_rdists->prop_page);
  1329. pr_info("GIC: using LPI property table @%pa\n", &paddr);
  1330. return its_lpi_init(lpi_id_bits);
  1331. }
  1332. static const char *its_base_type_string[] = {
  1333. [GITS_BASER_TYPE_DEVICE] = "Devices",
  1334. [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
  1335. [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
  1336. [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
  1337. [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
  1338. [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
  1339. [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
  1340. };
  1341. static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
  1342. {
  1343. u32 idx = baser - its->tables;
  1344. return gits_read_baser(its->base + GITS_BASER + (idx << 3));
  1345. }
  1346. static void its_write_baser(struct its_node *its, struct its_baser *baser,
  1347. u64 val)
  1348. {
  1349. u32 idx = baser - its->tables;
  1350. gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
  1351. baser->val = its_read_baser(its, baser);
  1352. }
  1353. static int its_setup_baser(struct its_node *its, struct its_baser *baser,
  1354. u64 cache, u64 shr, u32 psz, u32 order,
  1355. bool indirect)
  1356. {
  1357. u64 val = its_read_baser(its, baser);
  1358. u64 esz = GITS_BASER_ENTRY_SIZE(val);
  1359. u64 type = GITS_BASER_TYPE(val);
  1360. u64 baser_phys, tmp;
  1361. u32 alloc_pages;
  1362. void *base;
  1363. retry_alloc_baser:
  1364. alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
  1365. if (alloc_pages > GITS_BASER_PAGES_MAX) {
  1366. pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
  1367. &its->phys_base, its_base_type_string[type],
  1368. alloc_pages, GITS_BASER_PAGES_MAX);
  1369. alloc_pages = GITS_BASER_PAGES_MAX;
  1370. order = get_order(GITS_BASER_PAGES_MAX * psz);
  1371. }
  1372. base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  1373. if (!base)
  1374. return -ENOMEM;
  1375. baser_phys = virt_to_phys(base);
  1376. /* Check if the physical address of the memory is above 48bits */
  1377. if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
  1378. /* 52bit PA is supported only when PageSize=64K */
  1379. if (psz != SZ_64K) {
  1380. pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
  1381. free_pages((unsigned long)base, order);
  1382. return -ENXIO;
  1383. }
  1384. /* Convert 52bit PA to 48bit field */
  1385. baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
  1386. }
  1387. retry_baser:
  1388. val = (baser_phys |
  1389. (type << GITS_BASER_TYPE_SHIFT) |
  1390. ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
  1391. ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
  1392. cache |
  1393. shr |
  1394. GITS_BASER_VALID);
  1395. val |= indirect ? GITS_BASER_INDIRECT : 0x0;
  1396. switch (psz) {
  1397. case SZ_4K:
  1398. val |= GITS_BASER_PAGE_SIZE_4K;
  1399. break;
  1400. case SZ_16K:
  1401. val |= GITS_BASER_PAGE_SIZE_16K;
  1402. break;
  1403. case SZ_64K:
  1404. val |= GITS_BASER_PAGE_SIZE_64K;
  1405. break;
  1406. }
  1407. its_write_baser(its, baser, val);
  1408. tmp = baser->val;
  1409. if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
  1410. /*
  1411. * Shareability didn't stick. Just use
  1412. * whatever the read reported, which is likely
  1413. * to be the only thing this redistributor
  1414. * supports. If that's zero, make it
  1415. * non-cacheable as well.
  1416. */
  1417. shr = tmp & GITS_BASER_SHAREABILITY_MASK;
  1418. if (!shr) {
  1419. cache = GITS_BASER_nC;
  1420. gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
  1421. }
  1422. goto retry_baser;
  1423. }
  1424. if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
  1425. /*
  1426. * Page size didn't stick. Let's try a smaller
  1427. * size and retry. If we reach 4K, then
  1428. * something is horribly wrong...
  1429. */
  1430. free_pages((unsigned long)base, order);
  1431. baser->base = NULL;
  1432. switch (psz) {
  1433. case SZ_16K:
  1434. psz = SZ_4K;
  1435. goto retry_alloc_baser;
  1436. case SZ_64K:
  1437. psz = SZ_16K;
  1438. goto retry_alloc_baser;
  1439. }
  1440. }
  1441. if (val != tmp) {
  1442. pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
  1443. &its->phys_base, its_base_type_string[type],
  1444. val, tmp);
  1445. free_pages((unsigned long)base, order);
  1446. return -ENXIO;
  1447. }
  1448. baser->order = order;
  1449. baser->base = base;
  1450. baser->psz = psz;
  1451. tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
  1452. pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
  1453. &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
  1454. its_base_type_string[type],
  1455. (unsigned long)virt_to_phys(base),
  1456. indirect ? "indirect" : "flat", (int)esz,
  1457. psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
  1458. return 0;
  1459. }
  1460. static bool its_parse_indirect_baser(struct its_node *its,
  1461. struct its_baser *baser,
  1462. u32 psz, u32 *order, u32 ids)
  1463. {
  1464. u64 tmp = its_read_baser(its, baser);
  1465. u64 type = GITS_BASER_TYPE(tmp);
  1466. u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
  1467. u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
  1468. u32 new_order = *order;
  1469. bool indirect = false;
  1470. /* No need to enable Indirection if memory requirement < (psz*2)bytes */
  1471. if ((esz << ids) > (psz * 2)) {
  1472. /*
  1473. * Find out whether hw supports a single or two-level table by
  1474. * table by reading bit at offset '62' after writing '1' to it.
  1475. */
  1476. its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
  1477. indirect = !!(baser->val & GITS_BASER_INDIRECT);
  1478. if (indirect) {
  1479. /*
  1480. * The size of the lvl2 table is equal to ITS page size
  1481. * which is 'psz'. For computing lvl1 table size,
  1482. * subtract ID bits that sparse lvl2 table from 'ids'
  1483. * which is reported by ITS hardware times lvl1 table
  1484. * entry size.
  1485. */
  1486. ids -= ilog2(psz / (int)esz);
  1487. esz = GITS_LVL1_ENTRY_SIZE;
  1488. }
  1489. }
  1490. /*
  1491. * Allocate as many entries as required to fit the
  1492. * range of device IDs that the ITS can grok... The ID
  1493. * space being incredibly sparse, this results in a
  1494. * massive waste of memory if two-level device table
  1495. * feature is not supported by hardware.
  1496. */
  1497. new_order = max_t(u32, get_order(esz << ids), new_order);
  1498. if (new_order >= MAX_ORDER) {
  1499. new_order = MAX_ORDER - 1;
  1500. ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
  1501. pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
  1502. &its->phys_base, its_base_type_string[type],
  1503. its->device_ids, ids);
  1504. }
  1505. *order = new_order;
  1506. return indirect;
  1507. }
  1508. static void its_free_tables(struct its_node *its)
  1509. {
  1510. int i;
  1511. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  1512. if (its->tables[i].base) {
  1513. free_pages((unsigned long)its->tables[i].base,
  1514. its->tables[i].order);
  1515. its->tables[i].base = NULL;
  1516. }
  1517. }
  1518. }
  1519. static int its_alloc_tables(struct its_node *its)
  1520. {
  1521. u64 shr = GITS_BASER_InnerShareable;
  1522. u64 cache = GITS_BASER_RaWaWb;
  1523. u32 psz = SZ_64K;
  1524. int err, i;
  1525. if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
  1526. /* erratum 24313: ignore memory access type */
  1527. cache = GITS_BASER_nCnB;
  1528. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  1529. struct its_baser *baser = its->tables + i;
  1530. u64 val = its_read_baser(its, baser);
  1531. u64 type = GITS_BASER_TYPE(val);
  1532. u32 order = get_order(psz);
  1533. bool indirect = false;
  1534. switch (type) {
  1535. case GITS_BASER_TYPE_NONE:
  1536. continue;
  1537. case GITS_BASER_TYPE_DEVICE:
  1538. indirect = its_parse_indirect_baser(its, baser,
  1539. psz, &order,
  1540. its->device_ids);
  1541. break;
  1542. case GITS_BASER_TYPE_VCPU:
  1543. indirect = its_parse_indirect_baser(its, baser,
  1544. psz, &order,
  1545. ITS_MAX_VPEID_BITS);
  1546. break;
  1547. }
  1548. err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
  1549. if (err < 0) {
  1550. its_free_tables(its);
  1551. return err;
  1552. }
  1553. /* Update settings which will be used for next BASERn */
  1554. psz = baser->psz;
  1555. cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
  1556. shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
  1557. }
  1558. return 0;
  1559. }
  1560. static int its_alloc_collections(struct its_node *its)
  1561. {
  1562. int i;
  1563. its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
  1564. GFP_KERNEL);
  1565. if (!its->collections)
  1566. return -ENOMEM;
  1567. for (i = 0; i < nr_cpu_ids; i++)
  1568. its->collections[i].target_address = ~0ULL;
  1569. return 0;
  1570. }
  1571. static struct page *its_allocate_pending_table(gfp_t gfp_flags)
  1572. {
  1573. struct page *pend_page;
  1574. /*
  1575. * The pending pages have to be at least 64kB aligned,
  1576. * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
  1577. */
  1578. pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
  1579. get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
  1580. if (!pend_page)
  1581. return NULL;
  1582. /* Make sure the GIC will observe the zero-ed page */
  1583. gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
  1584. return pend_page;
  1585. }
  1586. static void its_free_pending_table(struct page *pt)
  1587. {
  1588. free_pages((unsigned long)page_address(pt),
  1589. get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
  1590. }
  1591. static u64 its_clear_vpend_valid(void __iomem *vlpi_base)
  1592. {
  1593. u32 count = 1000000; /* 1s! */
  1594. bool clean;
  1595. u64 val;
  1596. val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
  1597. val &= ~GICR_VPENDBASER_Valid;
  1598. gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
  1599. do {
  1600. val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
  1601. clean = !(val & GICR_VPENDBASER_Dirty);
  1602. if (!clean) {
  1603. count--;
  1604. cpu_relax();
  1605. udelay(1);
  1606. }
  1607. } while (!clean && count);
  1608. return val;
  1609. }
  1610. static void its_cpu_init_lpis(void)
  1611. {
  1612. void __iomem *rbase = gic_data_rdist_rd_base();
  1613. struct page *pend_page;
  1614. u64 val, tmp;
  1615. /* If we didn't allocate the pending table yet, do it now */
  1616. pend_page = gic_data_rdist()->pend_page;
  1617. if (!pend_page) {
  1618. phys_addr_t paddr;
  1619. pend_page = its_allocate_pending_table(GFP_NOWAIT);
  1620. if (!pend_page) {
  1621. pr_err("Failed to allocate PENDBASE for CPU%d\n",
  1622. smp_processor_id());
  1623. return;
  1624. }
  1625. paddr = page_to_phys(pend_page);
  1626. pr_info("CPU%d: using LPI pending table @%pa\n",
  1627. smp_processor_id(), &paddr);
  1628. gic_data_rdist()->pend_page = pend_page;
  1629. }
  1630. /* set PROPBASE */
  1631. val = (page_to_phys(gic_rdists->prop_page) |
  1632. GICR_PROPBASER_InnerShareable |
  1633. GICR_PROPBASER_RaWaWb |
  1634. ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
  1635. gicr_write_propbaser(val, rbase + GICR_PROPBASER);
  1636. tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
  1637. if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
  1638. if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
  1639. /*
  1640. * The HW reports non-shareable, we must
  1641. * remove the cacheability attributes as
  1642. * well.
  1643. */
  1644. val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
  1645. GICR_PROPBASER_CACHEABILITY_MASK);
  1646. val |= GICR_PROPBASER_nC;
  1647. gicr_write_propbaser(val, rbase + GICR_PROPBASER);
  1648. }
  1649. pr_info_once("GIC: using cache flushing for LPI property table\n");
  1650. gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
  1651. }
  1652. /* set PENDBASE */
  1653. val = (page_to_phys(pend_page) |
  1654. GICR_PENDBASER_InnerShareable |
  1655. GICR_PENDBASER_RaWaWb);
  1656. gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
  1657. tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
  1658. if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
  1659. /*
  1660. * The HW reports non-shareable, we must remove the
  1661. * cacheability attributes as well.
  1662. */
  1663. val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
  1664. GICR_PENDBASER_CACHEABILITY_MASK);
  1665. val |= GICR_PENDBASER_nC;
  1666. gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
  1667. }
  1668. /* Enable LPIs */
  1669. val = readl_relaxed(rbase + GICR_CTLR);
  1670. val |= GICR_CTLR_ENABLE_LPIS;
  1671. writel_relaxed(val, rbase + GICR_CTLR);
  1672. if (gic_rdists->has_vlpis) {
  1673. void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
  1674. /*
  1675. * It's possible for CPU to receive VLPIs before it is
  1676. * sheduled as a vPE, especially for the first CPU, and the
  1677. * VLPI with INTID larger than 2^(IDbits+1) will be considered
  1678. * as out of range and dropped by GIC.
  1679. * So we initialize IDbits to known value to avoid VLPI drop.
  1680. */
  1681. val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
  1682. pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
  1683. smp_processor_id(), val);
  1684. gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
  1685. /*
  1686. * Also clear Valid bit of GICR_VPENDBASER, in case some
  1687. * ancient programming gets left in and has possibility of
  1688. * corrupting memory.
  1689. */
  1690. val = its_clear_vpend_valid(vlpi_base);
  1691. WARN_ON(val & GICR_VPENDBASER_Dirty);
  1692. }
  1693. /* Make sure the GIC has seen the above */
  1694. dsb(sy);
  1695. }
  1696. static void its_cpu_init_collection(struct its_node *its)
  1697. {
  1698. int cpu = smp_processor_id();
  1699. u64 target;
  1700. /* avoid cross node collections and its mapping */
  1701. if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
  1702. struct device_node *cpu_node;
  1703. cpu_node = of_get_cpu_node(cpu, NULL);
  1704. if (its->numa_node != NUMA_NO_NODE &&
  1705. its->numa_node != of_node_to_nid(cpu_node))
  1706. return;
  1707. }
  1708. /*
  1709. * We now have to bind each collection to its target
  1710. * redistributor.
  1711. */
  1712. if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
  1713. /*
  1714. * This ITS wants the physical address of the
  1715. * redistributor.
  1716. */
  1717. target = gic_data_rdist()->phys_base;
  1718. } else {
  1719. /* This ITS wants a linear CPU number. */
  1720. target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
  1721. target = GICR_TYPER_CPU_NUMBER(target) << 16;
  1722. }
  1723. /* Perform collection mapping */
  1724. its->collections[cpu].target_address = target;
  1725. its->collections[cpu].col_id = cpu;
  1726. its_send_mapc(its, &its->collections[cpu], 1);
  1727. its_send_invall(its, &its->collections[cpu]);
  1728. }
  1729. static void its_cpu_init_collections(void)
  1730. {
  1731. struct its_node *its;
  1732. raw_spin_lock(&its_lock);
  1733. list_for_each_entry(its, &its_nodes, entry)
  1734. its_cpu_init_collection(its);
  1735. raw_spin_unlock(&its_lock);
  1736. }
  1737. static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
  1738. {
  1739. struct its_device *its_dev = NULL, *tmp;
  1740. unsigned long flags;
  1741. raw_spin_lock_irqsave(&its->lock, flags);
  1742. list_for_each_entry(tmp, &its->its_device_list, entry) {
  1743. if (tmp->device_id == dev_id) {
  1744. its_dev = tmp;
  1745. break;
  1746. }
  1747. }
  1748. raw_spin_unlock_irqrestore(&its->lock, flags);
  1749. return its_dev;
  1750. }
  1751. static struct its_baser *its_get_baser(struct its_node *its, u32 type)
  1752. {
  1753. int i;
  1754. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  1755. if (GITS_BASER_TYPE(its->tables[i].val) == type)
  1756. return &its->tables[i];
  1757. }
  1758. return NULL;
  1759. }
  1760. static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
  1761. {
  1762. struct page *page;
  1763. u32 esz, idx;
  1764. __le64 *table;
  1765. /* Don't allow device id that exceeds single, flat table limit */
  1766. esz = GITS_BASER_ENTRY_SIZE(baser->val);
  1767. if (!(baser->val & GITS_BASER_INDIRECT))
  1768. return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
  1769. /* Compute 1st level table index & check if that exceeds table limit */
  1770. idx = id >> ilog2(baser->psz / esz);
  1771. if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
  1772. return false;
  1773. table = baser->base;
  1774. /* Allocate memory for 2nd level table */
  1775. if (!table[idx]) {
  1776. page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
  1777. if (!page)
  1778. return false;
  1779. /* Flush Lvl2 table to PoC if hw doesn't support coherency */
  1780. if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
  1781. gic_flush_dcache_to_poc(page_address(page), baser->psz);
  1782. table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
  1783. /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
  1784. if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
  1785. gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
  1786. /* Ensure updated table contents are visible to ITS hardware */
  1787. dsb(sy);
  1788. }
  1789. return true;
  1790. }
  1791. static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
  1792. {
  1793. struct its_baser *baser;
  1794. baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
  1795. /* Don't allow device id that exceeds ITS hardware limit */
  1796. if (!baser)
  1797. return (ilog2(dev_id) < its->device_ids);
  1798. return its_alloc_table_entry(baser, dev_id);
  1799. }
  1800. static bool its_alloc_vpe_table(u32 vpe_id)
  1801. {
  1802. struct its_node *its;
  1803. /*
  1804. * Make sure the L2 tables are allocated on *all* v4 ITSs. We
  1805. * could try and only do it on ITSs corresponding to devices
  1806. * that have interrupts targeted at this VPE, but the
  1807. * complexity becomes crazy (and you have tons of memory
  1808. * anyway, right?).
  1809. */
  1810. list_for_each_entry(its, &its_nodes, entry) {
  1811. struct its_baser *baser;
  1812. if (!its->is_v4)
  1813. continue;
  1814. baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
  1815. if (!baser)
  1816. return false;
  1817. if (!its_alloc_table_entry(baser, vpe_id))
  1818. return false;
  1819. }
  1820. return true;
  1821. }
  1822. static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
  1823. int nvecs, bool alloc_lpis)
  1824. {
  1825. struct its_device *dev;
  1826. unsigned long *lpi_map = NULL;
  1827. unsigned long flags;
  1828. u16 *col_map = NULL;
  1829. void *itt;
  1830. int lpi_base;
  1831. int nr_lpis;
  1832. int nr_ites;
  1833. int sz;
  1834. if (!its_alloc_device_table(its, dev_id))
  1835. return NULL;
  1836. if (WARN_ON(!is_power_of_2(nvecs)))
  1837. nvecs = roundup_pow_of_two(nvecs);
  1838. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1839. /*
  1840. * Even if the device wants a single LPI, the ITT must be
  1841. * sized as a power of two (and you need at least one bit...).
  1842. */
  1843. nr_ites = max(2, nvecs);
  1844. sz = nr_ites * its->ite_size;
  1845. sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
  1846. itt = kzalloc(sz, GFP_KERNEL);
  1847. if (alloc_lpis) {
  1848. lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
  1849. if (lpi_map)
  1850. col_map = kcalloc(nr_lpis, sizeof(*col_map),
  1851. GFP_KERNEL);
  1852. } else {
  1853. col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
  1854. nr_lpis = 0;
  1855. lpi_base = 0;
  1856. }
  1857. if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
  1858. kfree(dev);
  1859. kfree(itt);
  1860. kfree(lpi_map);
  1861. kfree(col_map);
  1862. return NULL;
  1863. }
  1864. gic_flush_dcache_to_poc(itt, sz);
  1865. dev->its = its;
  1866. dev->itt = itt;
  1867. dev->nr_ites = nr_ites;
  1868. dev->event_map.lpi_map = lpi_map;
  1869. dev->event_map.col_map = col_map;
  1870. dev->event_map.lpi_base = lpi_base;
  1871. dev->event_map.nr_lpis = nr_lpis;
  1872. mutex_init(&dev->event_map.vlpi_lock);
  1873. dev->device_id = dev_id;
  1874. INIT_LIST_HEAD(&dev->entry);
  1875. raw_spin_lock_irqsave(&its->lock, flags);
  1876. list_add(&dev->entry, &its->its_device_list);
  1877. raw_spin_unlock_irqrestore(&its->lock, flags);
  1878. /* Map device to its ITT */
  1879. its_send_mapd(dev, 1);
  1880. return dev;
  1881. }
  1882. static void its_free_device(struct its_device *its_dev)
  1883. {
  1884. unsigned long flags;
  1885. raw_spin_lock_irqsave(&its_dev->its->lock, flags);
  1886. list_del(&its_dev->entry);
  1887. raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
  1888. kfree(its_dev->itt);
  1889. kfree(its_dev);
  1890. }
  1891. static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
  1892. {
  1893. int idx;
  1894. idx = bitmap_find_free_region(dev->event_map.lpi_map,
  1895. dev->event_map.nr_lpis,
  1896. get_count_order(nvecs));
  1897. if (idx < 0)
  1898. return -ENOSPC;
  1899. *hwirq = dev->event_map.lpi_base + idx;
  1900. set_bit(idx, dev->event_map.lpi_map);
  1901. return 0;
  1902. }
  1903. static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
  1904. int nvec, msi_alloc_info_t *info)
  1905. {
  1906. struct its_node *its;
  1907. struct its_device *its_dev;
  1908. struct msi_domain_info *msi_info;
  1909. u32 dev_id;
  1910. int err = 0;
  1911. /*
  1912. * We ignore "dev" entierely, and rely on the dev_id that has
  1913. * been passed via the scratchpad. This limits this domain's
  1914. * usefulness to upper layers that definitely know that they
  1915. * are built on top of the ITS.
  1916. */
  1917. dev_id = info->scratchpad[0].ul;
  1918. msi_info = msi_get_domain_info(domain);
  1919. its = msi_info->data;
  1920. if (!gic_rdists->has_direct_lpi &&
  1921. vpe_proxy.dev &&
  1922. vpe_proxy.dev->its == its &&
  1923. dev_id == vpe_proxy.dev->device_id) {
  1924. /* Bad luck. Get yourself a better implementation */
  1925. WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
  1926. dev_id);
  1927. return -EINVAL;
  1928. }
  1929. mutex_lock(&its->dev_alloc_lock);
  1930. its_dev = its_find_device(its, dev_id);
  1931. if (its_dev) {
  1932. /*
  1933. * We already have seen this ID, probably through
  1934. * another alias (PCI bridge of some sort). No need to
  1935. * create the device.
  1936. */
  1937. its_dev->shared = true;
  1938. pr_debug("Reusing ITT for devID %x\n", dev_id);
  1939. goto out;
  1940. }
  1941. its_dev = its_create_device(its, dev_id, nvec, true);
  1942. if (!its_dev) {
  1943. err = -ENOMEM;
  1944. goto out;
  1945. }
  1946. pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
  1947. out:
  1948. mutex_unlock(&its->dev_alloc_lock);
  1949. info->scratchpad[0].ptr = its_dev;
  1950. return err;
  1951. }
  1952. static struct msi_domain_ops its_msi_domain_ops = {
  1953. .msi_prepare = its_msi_prepare,
  1954. };
  1955. static int its_irq_gic_domain_alloc(struct irq_domain *domain,
  1956. unsigned int virq,
  1957. irq_hw_number_t hwirq)
  1958. {
  1959. struct irq_fwspec fwspec;
  1960. if (irq_domain_get_of_node(domain->parent)) {
  1961. fwspec.fwnode = domain->parent->fwnode;
  1962. fwspec.param_count = 3;
  1963. fwspec.param[0] = GIC_IRQ_TYPE_LPI;
  1964. fwspec.param[1] = hwirq;
  1965. fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
  1966. } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
  1967. fwspec.fwnode = domain->parent->fwnode;
  1968. fwspec.param_count = 2;
  1969. fwspec.param[0] = hwirq;
  1970. fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
  1971. } else {
  1972. return -EINVAL;
  1973. }
  1974. return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  1975. }
  1976. static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  1977. unsigned int nr_irqs, void *args)
  1978. {
  1979. msi_alloc_info_t *info = args;
  1980. struct its_device *its_dev = info->scratchpad[0].ptr;
  1981. struct irq_data *irqd;
  1982. irq_hw_number_t hwirq;
  1983. int err;
  1984. int i;
  1985. err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
  1986. if (err)
  1987. return err;
  1988. for (i = 0; i < nr_irqs; i++) {
  1989. err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
  1990. if (err)
  1991. return err;
  1992. irq_domain_set_hwirq_and_chip(domain, virq + i,
  1993. hwirq + i, &its_irq_chip, its_dev);
  1994. irqd = irq_get_irq_data(virq + i);
  1995. irqd_set_single_target(irqd);
  1996. irqd_set_affinity_on_activate(irqd);
  1997. pr_debug("ID:%d pID:%d vID:%d\n",
  1998. (int)(hwirq + i - its_dev->event_map.lpi_base),
  1999. (int)(hwirq + i), virq + i);
  2000. }
  2001. return 0;
  2002. }
  2003. static int its_irq_domain_activate(struct irq_domain *domain,
  2004. struct irq_data *d, bool reserve)
  2005. {
  2006. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  2007. u32 event = its_get_event_id(d);
  2008. const struct cpumask *cpu_mask = cpu_online_mask;
  2009. int cpu;
  2010. /* get the cpu_mask of local node */
  2011. if (its_dev->its->numa_node >= 0)
  2012. cpu_mask = cpumask_of_node(its_dev->its->numa_node);
  2013. /* Bind the LPI to the first possible CPU */
  2014. cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
  2015. if (cpu >= nr_cpu_ids) {
  2016. if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
  2017. return -EINVAL;
  2018. cpu = cpumask_first(cpu_online_mask);
  2019. }
  2020. its_dev->event_map.col_map[event] = cpu;
  2021. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  2022. /* Map the GIC IRQ and event to the device */
  2023. its_send_mapti(its_dev, d->hwirq, event);
  2024. return 0;
  2025. }
  2026. static void its_irq_domain_deactivate(struct irq_domain *domain,
  2027. struct irq_data *d)
  2028. {
  2029. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  2030. u32 event = its_get_event_id(d);
  2031. /* Stop the delivery of interrupts */
  2032. its_send_discard(its_dev, event);
  2033. }
  2034. static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  2035. unsigned int nr_irqs)
  2036. {
  2037. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  2038. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  2039. struct its_node *its = its_dev->its;
  2040. int i;
  2041. bitmap_release_region(its_dev->event_map.lpi_map,
  2042. its_get_event_id(irq_domain_get_irq_data(domain, virq)),
  2043. get_count_order(nr_irqs));
  2044. for (i = 0; i < nr_irqs; i++) {
  2045. struct irq_data *data = irq_domain_get_irq_data(domain,
  2046. virq + i);
  2047. /* Nuke the entry in the domain */
  2048. irq_domain_reset_irq_data(data);
  2049. }
  2050. mutex_lock(&its->dev_alloc_lock);
  2051. /*
  2052. * If all interrupts have been freed, start mopping the
  2053. * floor. This is conditionned on the device not being shared.
  2054. */
  2055. if (!its_dev->shared &&
  2056. bitmap_empty(its_dev->event_map.lpi_map,
  2057. its_dev->event_map.nr_lpis)) {
  2058. its_lpi_free(its_dev->event_map.lpi_map,
  2059. its_dev->event_map.lpi_base,
  2060. its_dev->event_map.nr_lpis);
  2061. kfree(its_dev->event_map.col_map);
  2062. /* Unmap device/itt */
  2063. its_send_mapd(its_dev, 0);
  2064. its_free_device(its_dev);
  2065. }
  2066. mutex_unlock(&its->dev_alloc_lock);
  2067. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  2068. }
  2069. static const struct irq_domain_ops its_domain_ops = {
  2070. .alloc = its_irq_domain_alloc,
  2071. .free = its_irq_domain_free,
  2072. .activate = its_irq_domain_activate,
  2073. .deactivate = its_irq_domain_deactivate,
  2074. };
  2075. /*
  2076. * This is insane.
  2077. *
  2078. * If a GICv4 doesn't implement Direct LPIs (which is extremely
  2079. * likely), the only way to perform an invalidate is to use a fake
  2080. * device to issue an INV command, implying that the LPI has first
  2081. * been mapped to some event on that device. Since this is not exactly
  2082. * cheap, we try to keep that mapping around as long as possible, and
  2083. * only issue an UNMAP if we're short on available slots.
  2084. *
  2085. * Broken by design(tm).
  2086. */
  2087. static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
  2088. {
  2089. /* Already unmapped? */
  2090. if (vpe->vpe_proxy_event == -1)
  2091. return;
  2092. its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
  2093. vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
  2094. /*
  2095. * We don't track empty slots at all, so let's move the
  2096. * next_victim pointer if we can quickly reuse that slot
  2097. * instead of nuking an existing entry. Not clear that this is
  2098. * always a win though, and this might just generate a ripple
  2099. * effect... Let's just hope VPEs don't migrate too often.
  2100. */
  2101. if (vpe_proxy.vpes[vpe_proxy.next_victim])
  2102. vpe_proxy.next_victim = vpe->vpe_proxy_event;
  2103. vpe->vpe_proxy_event = -1;
  2104. }
  2105. static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
  2106. {
  2107. if (!gic_rdists->has_direct_lpi) {
  2108. unsigned long flags;
  2109. raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
  2110. its_vpe_db_proxy_unmap_locked(vpe);
  2111. raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
  2112. }
  2113. }
  2114. static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
  2115. {
  2116. /* Already mapped? */
  2117. if (vpe->vpe_proxy_event != -1)
  2118. return;
  2119. /* This slot was already allocated. Kick the other VPE out. */
  2120. if (vpe_proxy.vpes[vpe_proxy.next_victim])
  2121. its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
  2122. /* Map the new VPE instead */
  2123. vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
  2124. vpe->vpe_proxy_event = vpe_proxy.next_victim;
  2125. vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
  2126. vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
  2127. its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
  2128. }
  2129. static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
  2130. {
  2131. unsigned long flags;
  2132. struct its_collection *target_col;
  2133. if (gic_rdists->has_direct_lpi) {
  2134. void __iomem *rdbase;
  2135. rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
  2136. gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
  2137. while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
  2138. cpu_relax();
  2139. return;
  2140. }
  2141. raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
  2142. its_vpe_db_proxy_map_locked(vpe);
  2143. target_col = &vpe_proxy.dev->its->collections[to];
  2144. its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
  2145. vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
  2146. raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
  2147. }
  2148. static int its_vpe_set_affinity(struct irq_data *d,
  2149. const struct cpumask *mask_val,
  2150. bool force)
  2151. {
  2152. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  2153. int cpu = cpumask_first(mask_val);
  2154. /*
  2155. * Changing affinity is mega expensive, so let's be as lazy as
  2156. * we can and only do it if we really have to. Also, if mapped
  2157. * into the proxy device, we need to move the doorbell
  2158. * interrupt to its new location.
  2159. */
  2160. if (vpe->col_idx != cpu) {
  2161. int from = vpe->col_idx;
  2162. vpe->col_idx = cpu;
  2163. its_send_vmovp(vpe);
  2164. its_vpe_db_proxy_move(vpe, from, cpu);
  2165. }
  2166. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  2167. return IRQ_SET_MASK_OK_DONE;
  2168. }
  2169. static void its_vpe_schedule(struct its_vpe *vpe)
  2170. {
  2171. void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
  2172. u64 val;
  2173. /* Schedule the VPE */
  2174. val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
  2175. GENMASK_ULL(51, 12);
  2176. val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
  2177. val |= GICR_VPROPBASER_RaWb;
  2178. val |= GICR_VPROPBASER_InnerShareable;
  2179. gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
  2180. val = virt_to_phys(page_address(vpe->vpt_page)) &
  2181. GENMASK_ULL(51, 16);
  2182. val |= GICR_VPENDBASER_RaWaWb;
  2183. val |= GICR_VPENDBASER_NonShareable;
  2184. /*
  2185. * There is no good way of finding out if the pending table is
  2186. * empty as we can race against the doorbell interrupt very
  2187. * easily. So in the end, vpe->pending_last is only an
  2188. * indication that the vcpu has something pending, not one
  2189. * that the pending table is empty. A good implementation
  2190. * would be able to read its coarse map pretty quickly anyway,
  2191. * making this a tolerable issue.
  2192. */
  2193. val |= GICR_VPENDBASER_PendingLast;
  2194. val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
  2195. val |= GICR_VPENDBASER_Valid;
  2196. gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
  2197. }
  2198. static void its_vpe_deschedule(struct its_vpe *vpe)
  2199. {
  2200. void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
  2201. u64 val;
  2202. val = its_clear_vpend_valid(vlpi_base);
  2203. if (unlikely(val & GICR_VPENDBASER_Dirty)) {
  2204. pr_err_ratelimited("ITS virtual pending table not cleaning\n");
  2205. vpe->idai = false;
  2206. vpe->pending_last = true;
  2207. } else {
  2208. vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
  2209. vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
  2210. }
  2211. }
  2212. static void its_vpe_invall(struct its_vpe *vpe)
  2213. {
  2214. struct its_node *its;
  2215. list_for_each_entry(its, &its_nodes, entry) {
  2216. if (!its->is_v4)
  2217. continue;
  2218. if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
  2219. continue;
  2220. /*
  2221. * Sending a VINVALL to a single ITS is enough, as all
  2222. * we need is to reach the redistributors.
  2223. */
  2224. its_send_vinvall(its, vpe);
  2225. return;
  2226. }
  2227. }
  2228. static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
  2229. {
  2230. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  2231. struct its_cmd_info *info = vcpu_info;
  2232. switch (info->cmd_type) {
  2233. case SCHEDULE_VPE:
  2234. its_vpe_schedule(vpe);
  2235. return 0;
  2236. case DESCHEDULE_VPE:
  2237. its_vpe_deschedule(vpe);
  2238. return 0;
  2239. case INVALL_VPE:
  2240. its_vpe_invall(vpe);
  2241. return 0;
  2242. default:
  2243. return -EINVAL;
  2244. }
  2245. }
  2246. static void its_vpe_send_cmd(struct its_vpe *vpe,
  2247. void (*cmd)(struct its_device *, u32))
  2248. {
  2249. unsigned long flags;
  2250. raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
  2251. its_vpe_db_proxy_map_locked(vpe);
  2252. cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
  2253. raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
  2254. }
  2255. static void its_vpe_send_inv(struct irq_data *d)
  2256. {
  2257. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  2258. if (gic_rdists->has_direct_lpi) {
  2259. void __iomem *rdbase;
  2260. rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
  2261. gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
  2262. while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
  2263. cpu_relax();
  2264. } else {
  2265. its_vpe_send_cmd(vpe, its_send_inv);
  2266. }
  2267. }
  2268. static void its_vpe_mask_irq(struct irq_data *d)
  2269. {
  2270. /*
  2271. * We need to unmask the LPI, which is described by the parent
  2272. * irq_data. Instead of calling into the parent (which won't
  2273. * exactly do the right thing, let's simply use the
  2274. * parent_data pointer. Yes, I'm naughty.
  2275. */
  2276. lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
  2277. its_vpe_send_inv(d);
  2278. }
  2279. static void its_vpe_unmask_irq(struct irq_data *d)
  2280. {
  2281. /* Same hack as above... */
  2282. lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
  2283. its_vpe_send_inv(d);
  2284. }
  2285. static int its_vpe_set_irqchip_state(struct irq_data *d,
  2286. enum irqchip_irq_state which,
  2287. bool state)
  2288. {
  2289. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  2290. if (which != IRQCHIP_STATE_PENDING)
  2291. return -EINVAL;
  2292. if (gic_rdists->has_direct_lpi) {
  2293. void __iomem *rdbase;
  2294. rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
  2295. if (state) {
  2296. gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
  2297. } else {
  2298. gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
  2299. while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
  2300. cpu_relax();
  2301. }
  2302. } else {
  2303. if (state)
  2304. its_vpe_send_cmd(vpe, its_send_int);
  2305. else
  2306. its_vpe_send_cmd(vpe, its_send_clear);
  2307. }
  2308. return 0;
  2309. }
  2310. static int its_vpe_retrigger(struct irq_data *d)
  2311. {
  2312. return !its_vpe_set_irqchip_state(d, IRQCHIP_STATE_PENDING, true);
  2313. }
  2314. static struct irq_chip its_vpe_irq_chip = {
  2315. .name = "GICv4-vpe",
  2316. .irq_mask = its_vpe_mask_irq,
  2317. .irq_unmask = its_vpe_unmask_irq,
  2318. .irq_eoi = irq_chip_eoi_parent,
  2319. .irq_set_affinity = its_vpe_set_affinity,
  2320. .irq_retrigger = its_vpe_retrigger,
  2321. .irq_set_irqchip_state = its_vpe_set_irqchip_state,
  2322. .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
  2323. };
  2324. static int its_vpe_id_alloc(void)
  2325. {
  2326. return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
  2327. }
  2328. static void its_vpe_id_free(u16 id)
  2329. {
  2330. ida_simple_remove(&its_vpeid_ida, id);
  2331. }
  2332. static int its_vpe_init(struct its_vpe *vpe)
  2333. {
  2334. struct page *vpt_page;
  2335. int vpe_id;
  2336. /* Allocate vpe_id */
  2337. vpe_id = its_vpe_id_alloc();
  2338. if (vpe_id < 0)
  2339. return vpe_id;
  2340. /* Allocate VPT */
  2341. vpt_page = its_allocate_pending_table(GFP_KERNEL);
  2342. if (!vpt_page) {
  2343. its_vpe_id_free(vpe_id);
  2344. return -ENOMEM;
  2345. }
  2346. if (!its_alloc_vpe_table(vpe_id)) {
  2347. its_vpe_id_free(vpe_id);
  2348. its_free_pending_table(vpt_page);
  2349. return -ENOMEM;
  2350. }
  2351. vpe->vpe_id = vpe_id;
  2352. vpe->vpt_page = vpt_page;
  2353. vpe->vpe_proxy_event = -1;
  2354. return 0;
  2355. }
  2356. static void its_vpe_teardown(struct its_vpe *vpe)
  2357. {
  2358. its_vpe_db_proxy_unmap(vpe);
  2359. its_vpe_id_free(vpe->vpe_id);
  2360. its_free_pending_table(vpe->vpt_page);
  2361. }
  2362. static void its_vpe_irq_domain_free(struct irq_domain *domain,
  2363. unsigned int virq,
  2364. unsigned int nr_irqs)
  2365. {
  2366. struct its_vm *vm = domain->host_data;
  2367. int i;
  2368. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  2369. for (i = 0; i < nr_irqs; i++) {
  2370. struct irq_data *data = irq_domain_get_irq_data(domain,
  2371. virq + i);
  2372. struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
  2373. BUG_ON(vm != vpe->its_vm);
  2374. clear_bit(data->hwirq, vm->db_bitmap);
  2375. its_vpe_teardown(vpe);
  2376. irq_domain_reset_irq_data(data);
  2377. }
  2378. if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
  2379. its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
  2380. its_free_prop_table(vm->vprop_page);
  2381. }
  2382. }
  2383. static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  2384. unsigned int nr_irqs, void *args)
  2385. {
  2386. struct its_vm *vm = args;
  2387. unsigned long *bitmap;
  2388. struct page *vprop_page;
  2389. int base, nr_ids, i, err = 0;
  2390. BUG_ON(!vm);
  2391. bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
  2392. if (!bitmap)
  2393. return -ENOMEM;
  2394. if (nr_ids < nr_irqs) {
  2395. its_lpi_free(bitmap, base, nr_ids);
  2396. return -ENOMEM;
  2397. }
  2398. vprop_page = its_allocate_prop_table(GFP_KERNEL);
  2399. if (!vprop_page) {
  2400. its_lpi_free(bitmap, base, nr_ids);
  2401. return -ENOMEM;
  2402. }
  2403. vm->db_bitmap = bitmap;
  2404. vm->db_lpi_base = base;
  2405. vm->nr_db_lpis = nr_ids;
  2406. vm->vprop_page = vprop_page;
  2407. for (i = 0; i < nr_irqs; i++) {
  2408. vm->vpes[i]->vpe_db_lpi = base + i;
  2409. err = its_vpe_init(vm->vpes[i]);
  2410. if (err)
  2411. break;
  2412. err = its_irq_gic_domain_alloc(domain, virq + i,
  2413. vm->vpes[i]->vpe_db_lpi);
  2414. if (err)
  2415. break;
  2416. irq_domain_set_hwirq_and_chip(domain, virq + i, i,
  2417. &its_vpe_irq_chip, vm->vpes[i]);
  2418. set_bit(i, bitmap);
  2419. }
  2420. if (err) {
  2421. if (i > 0)
  2422. its_vpe_irq_domain_free(domain, virq, i - 1);
  2423. its_lpi_free(bitmap, base, nr_ids);
  2424. its_free_prop_table(vprop_page);
  2425. }
  2426. return err;
  2427. }
  2428. static int its_vpe_irq_domain_activate(struct irq_domain *domain,
  2429. struct irq_data *d, bool reserve)
  2430. {
  2431. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  2432. struct its_node *its;
  2433. /* If we use the list map, we issue VMAPP on demand... */
  2434. if (its_list_map)
  2435. return 0;
  2436. /* Map the VPE to the first possible CPU */
  2437. vpe->col_idx = cpumask_first(cpu_online_mask);
  2438. list_for_each_entry(its, &its_nodes, entry) {
  2439. if (!its->is_v4)
  2440. continue;
  2441. its_send_vmapp(its, vpe, true);
  2442. its_send_vinvall(its, vpe);
  2443. }
  2444. irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
  2445. return 0;
  2446. }
  2447. static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
  2448. struct irq_data *d)
  2449. {
  2450. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  2451. struct its_node *its;
  2452. /*
  2453. * If we use the list map, we unmap the VPE once no VLPIs are
  2454. * associated with the VM.
  2455. */
  2456. if (its_list_map)
  2457. return;
  2458. list_for_each_entry(its, &its_nodes, entry) {
  2459. if (!its->is_v4)
  2460. continue;
  2461. its_send_vmapp(its, vpe, false);
  2462. }
  2463. }
  2464. static const struct irq_domain_ops its_vpe_domain_ops = {
  2465. .alloc = its_vpe_irq_domain_alloc,
  2466. .free = its_vpe_irq_domain_free,
  2467. .activate = its_vpe_irq_domain_activate,
  2468. .deactivate = its_vpe_irq_domain_deactivate,
  2469. };
  2470. static int its_force_quiescent(void __iomem *base)
  2471. {
  2472. u32 count = 1000000; /* 1s */
  2473. u32 val;
  2474. val = readl_relaxed(base + GITS_CTLR);
  2475. /*
  2476. * GIC architecture specification requires the ITS to be both
  2477. * disabled and quiescent for writes to GITS_BASER<n> or
  2478. * GITS_CBASER to not have UNPREDICTABLE results.
  2479. */
  2480. if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
  2481. return 0;
  2482. /* Disable the generation of all interrupts to this ITS */
  2483. val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
  2484. writel_relaxed(val, base + GITS_CTLR);
  2485. /* Poll GITS_CTLR and wait until ITS becomes quiescent */
  2486. while (1) {
  2487. val = readl_relaxed(base + GITS_CTLR);
  2488. if (val & GITS_CTLR_QUIESCENT)
  2489. return 0;
  2490. count--;
  2491. if (!count)
  2492. return -EBUSY;
  2493. cpu_relax();
  2494. udelay(1);
  2495. }
  2496. }
  2497. static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
  2498. {
  2499. struct its_node *its = data;
  2500. /* erratum 22375: only alloc 8MB table size */
  2501. its->device_ids = 0x14; /* 20 bits, 8MB */
  2502. its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
  2503. return true;
  2504. }
  2505. static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
  2506. {
  2507. struct its_node *its = data;
  2508. its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
  2509. return true;
  2510. }
  2511. static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
  2512. {
  2513. struct its_node *its = data;
  2514. /* On QDF2400, the size of the ITE is 16Bytes */
  2515. its->ite_size = 16;
  2516. return true;
  2517. }
  2518. static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
  2519. {
  2520. struct its_node *its = its_dev->its;
  2521. /*
  2522. * The Socionext Synquacer SoC has a so-called 'pre-ITS',
  2523. * which maps 32-bit writes targeted at a separate window of
  2524. * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
  2525. * with device ID taken from bits [device_id_bits + 1:2] of
  2526. * the window offset.
  2527. */
  2528. return its->pre_its_base + (its_dev->device_id << 2);
  2529. }
  2530. static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
  2531. {
  2532. struct its_node *its = data;
  2533. u32 pre_its_window[2];
  2534. u32 ids;
  2535. if (!fwnode_property_read_u32_array(its->fwnode_handle,
  2536. "socionext,synquacer-pre-its",
  2537. pre_its_window,
  2538. ARRAY_SIZE(pre_its_window))) {
  2539. its->pre_its_base = pre_its_window[0];
  2540. its->get_msi_base = its_irq_get_msi_base_pre_its;
  2541. ids = ilog2(pre_its_window[1]) - 2;
  2542. if (its->device_ids > ids)
  2543. its->device_ids = ids;
  2544. /* the pre-ITS breaks isolation, so disable MSI remapping */
  2545. its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
  2546. return true;
  2547. }
  2548. return false;
  2549. }
  2550. static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
  2551. {
  2552. struct its_node *its = data;
  2553. /*
  2554. * Hip07 insists on using the wrong address for the VLPI
  2555. * page. Trick it into doing the right thing...
  2556. */
  2557. its->vlpi_redist_offset = SZ_128K;
  2558. return true;
  2559. }
  2560. static const struct gic_quirk its_quirks[] = {
  2561. #ifdef CONFIG_CAVIUM_ERRATUM_22375
  2562. {
  2563. .desc = "ITS: Cavium errata 22375, 24313",
  2564. .iidr = 0xa100034c, /* ThunderX pass 1.x */
  2565. .mask = 0xffff0fff,
  2566. .init = its_enable_quirk_cavium_22375,
  2567. },
  2568. #endif
  2569. #ifdef CONFIG_CAVIUM_ERRATUM_23144
  2570. {
  2571. .desc = "ITS: Cavium erratum 23144",
  2572. .iidr = 0xa100034c, /* ThunderX pass 1.x */
  2573. .mask = 0xffff0fff,
  2574. .init = its_enable_quirk_cavium_23144,
  2575. },
  2576. #endif
  2577. #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
  2578. {
  2579. .desc = "ITS: QDF2400 erratum 0065",
  2580. .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
  2581. .mask = 0xffffffff,
  2582. .init = its_enable_quirk_qdf2400_e0065,
  2583. },
  2584. #endif
  2585. #ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
  2586. {
  2587. /*
  2588. * The Socionext Synquacer SoC incorporates ARM's own GIC-500
  2589. * implementation, but with a 'pre-ITS' added that requires
  2590. * special handling in software.
  2591. */
  2592. .desc = "ITS: Socionext Synquacer pre-ITS",
  2593. .iidr = 0x0001143b,
  2594. .mask = 0xffffffff,
  2595. .init = its_enable_quirk_socionext_synquacer,
  2596. },
  2597. #endif
  2598. #ifdef CONFIG_HISILICON_ERRATUM_161600802
  2599. {
  2600. .desc = "ITS: Hip07 erratum 161600802",
  2601. .iidr = 0x00000004,
  2602. .mask = 0xffffffff,
  2603. .init = its_enable_quirk_hip07_161600802,
  2604. },
  2605. #endif
  2606. {
  2607. }
  2608. };
  2609. static void its_enable_quirks(struct its_node *its)
  2610. {
  2611. u32 iidr = readl_relaxed(its->base + GITS_IIDR);
  2612. gic_enable_quirks(iidr, its_quirks, its);
  2613. }
  2614. static int its_save_disable(void)
  2615. {
  2616. struct its_node *its;
  2617. int err = 0;
  2618. raw_spin_lock(&its_lock);
  2619. list_for_each_entry(its, &its_nodes, entry) {
  2620. void __iomem *base;
  2621. base = its->base;
  2622. its->ctlr_save = readl_relaxed(base + GITS_CTLR);
  2623. err = its_force_quiescent(base);
  2624. if (err) {
  2625. pr_err("ITS@%pa: failed to quiesce: %d\n",
  2626. &its->phys_base, err);
  2627. writel_relaxed(its->ctlr_save, base + GITS_CTLR);
  2628. goto err;
  2629. }
  2630. its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
  2631. }
  2632. err:
  2633. if (err) {
  2634. list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
  2635. void __iomem *base;
  2636. base = its->base;
  2637. writel_relaxed(its->ctlr_save, base + GITS_CTLR);
  2638. }
  2639. }
  2640. raw_spin_unlock(&its_lock);
  2641. return err;
  2642. }
  2643. static void its_restore_enable(void)
  2644. {
  2645. struct its_node *its;
  2646. int ret;
  2647. raw_spin_lock(&its_lock);
  2648. list_for_each_entry(its, &its_nodes, entry) {
  2649. void __iomem *base;
  2650. int i;
  2651. base = its->base;
  2652. /*
  2653. * Make sure that the ITS is disabled. If it fails to quiesce,
  2654. * don't restore it since writing to CBASER or BASER<n>
  2655. * registers is undefined according to the GIC v3 ITS
  2656. * Specification.
  2657. *
  2658. * Firmware resuming with the ITS enabled is terminally broken.
  2659. */
  2660. WARN_ON(readl_relaxed(base + GITS_CTLR) & GITS_CTLR_ENABLE);
  2661. ret = its_force_quiescent(base);
  2662. if (ret) {
  2663. pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
  2664. &its->phys_base, ret);
  2665. continue;
  2666. }
  2667. gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
  2668. /*
  2669. * Writing CBASER resets CREADR to 0, so make CWRITER and
  2670. * cmd_write line up with it.
  2671. */
  2672. its->cmd_write = its->cmd_base;
  2673. gits_write_cwriter(0, base + GITS_CWRITER);
  2674. /* Restore GITS_BASER from the value cache. */
  2675. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  2676. struct its_baser *baser = &its->tables[i];
  2677. if (!(baser->val & GITS_BASER_VALID))
  2678. continue;
  2679. its_write_baser(its, baser, baser->val);
  2680. }
  2681. writel_relaxed(its->ctlr_save, base + GITS_CTLR);
  2682. /*
  2683. * Reinit the collection if it's stored in the ITS. This is
  2684. * indicated by the col_id being less than the HCC field.
  2685. * CID < HCC as specified in the GIC v3 Documentation.
  2686. */
  2687. if (its->collections[smp_processor_id()].col_id <
  2688. GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
  2689. its_cpu_init_collection(its);
  2690. }
  2691. raw_spin_unlock(&its_lock);
  2692. }
  2693. static struct syscore_ops its_syscore_ops = {
  2694. .suspend = its_save_disable,
  2695. .resume = its_restore_enable,
  2696. };
  2697. static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
  2698. {
  2699. struct irq_domain *inner_domain;
  2700. struct msi_domain_info *info;
  2701. info = kzalloc(sizeof(*info), GFP_KERNEL);
  2702. if (!info)
  2703. return -ENOMEM;
  2704. inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
  2705. if (!inner_domain) {
  2706. kfree(info);
  2707. return -ENOMEM;
  2708. }
  2709. inner_domain->parent = its_parent;
  2710. irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
  2711. inner_domain->flags |= its->msi_domain_flags;
  2712. info->ops = &its_msi_domain_ops;
  2713. info->data = its;
  2714. inner_domain->host_data = info;
  2715. return 0;
  2716. }
  2717. static int its_init_vpe_domain(void)
  2718. {
  2719. struct its_node *its;
  2720. u32 devid;
  2721. int entries;
  2722. if (gic_rdists->has_direct_lpi) {
  2723. pr_info("ITS: Using DirectLPI for VPE invalidation\n");
  2724. return 0;
  2725. }
  2726. /* Any ITS will do, even if not v4 */
  2727. its = list_first_entry(&its_nodes, struct its_node, entry);
  2728. entries = roundup_pow_of_two(nr_cpu_ids);
  2729. vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
  2730. GFP_KERNEL);
  2731. if (!vpe_proxy.vpes) {
  2732. pr_err("ITS: Can't allocate GICv4 proxy device array\n");
  2733. return -ENOMEM;
  2734. }
  2735. /* Use the last possible DevID */
  2736. devid = GENMASK(its->device_ids - 1, 0);
  2737. vpe_proxy.dev = its_create_device(its, devid, entries, false);
  2738. if (!vpe_proxy.dev) {
  2739. kfree(vpe_proxy.vpes);
  2740. pr_err("ITS: Can't allocate GICv4 proxy device\n");
  2741. return -ENOMEM;
  2742. }
  2743. BUG_ON(entries > vpe_proxy.dev->nr_ites);
  2744. raw_spin_lock_init(&vpe_proxy.lock);
  2745. vpe_proxy.next_victim = 0;
  2746. pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
  2747. devid, vpe_proxy.dev->nr_ites);
  2748. return 0;
  2749. }
  2750. static int __init its_compute_its_list_map(struct resource *res,
  2751. void __iomem *its_base)
  2752. {
  2753. int its_number;
  2754. u32 ctlr;
  2755. /*
  2756. * This is assumed to be done early enough that we're
  2757. * guaranteed to be single-threaded, hence no
  2758. * locking. Should this change, we should address
  2759. * this.
  2760. */
  2761. its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
  2762. if (its_number >= GICv4_ITS_LIST_MAX) {
  2763. pr_err("ITS@%pa: No ITSList entry available!\n",
  2764. &res->start);
  2765. return -EINVAL;
  2766. }
  2767. ctlr = readl_relaxed(its_base + GITS_CTLR);
  2768. ctlr &= ~GITS_CTLR_ITS_NUMBER;
  2769. ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
  2770. writel_relaxed(ctlr, its_base + GITS_CTLR);
  2771. ctlr = readl_relaxed(its_base + GITS_CTLR);
  2772. if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
  2773. its_number = ctlr & GITS_CTLR_ITS_NUMBER;
  2774. its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
  2775. }
  2776. if (test_and_set_bit(its_number, &its_list_map)) {
  2777. pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
  2778. &res->start, its_number);
  2779. return -EINVAL;
  2780. }
  2781. return its_number;
  2782. }
  2783. static int __init its_probe_one(struct resource *res,
  2784. struct fwnode_handle *handle, int numa_node)
  2785. {
  2786. struct its_node *its;
  2787. void __iomem *its_base;
  2788. u32 val, ctlr;
  2789. u64 baser, tmp, typer;
  2790. int err;
  2791. its_base = ioremap(res->start, resource_size(res));
  2792. if (!its_base) {
  2793. pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
  2794. return -ENOMEM;
  2795. }
  2796. val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
  2797. if (val != 0x30 && val != 0x40) {
  2798. pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
  2799. err = -ENODEV;
  2800. goto out_unmap;
  2801. }
  2802. err = its_force_quiescent(its_base);
  2803. if (err) {
  2804. pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
  2805. goto out_unmap;
  2806. }
  2807. pr_info("ITS %pR\n", res);
  2808. its = kzalloc(sizeof(*its), GFP_KERNEL);
  2809. if (!its) {
  2810. err = -ENOMEM;
  2811. goto out_unmap;
  2812. }
  2813. raw_spin_lock_init(&its->lock);
  2814. mutex_init(&its->dev_alloc_lock);
  2815. INIT_LIST_HEAD(&its->entry);
  2816. INIT_LIST_HEAD(&its->its_device_list);
  2817. typer = gic_read_typer(its_base + GITS_TYPER);
  2818. its->base = its_base;
  2819. its->phys_base = res->start;
  2820. its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
  2821. its->device_ids = GITS_TYPER_DEVBITS(typer);
  2822. its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
  2823. if (its->is_v4) {
  2824. if (!(typer & GITS_TYPER_VMOVP)) {
  2825. err = its_compute_its_list_map(res, its_base);
  2826. if (err < 0)
  2827. goto out_free_its;
  2828. its->list_nr = err;
  2829. pr_info("ITS@%pa: Using ITS number %d\n",
  2830. &res->start, err);
  2831. } else {
  2832. pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
  2833. }
  2834. }
  2835. its->numa_node = numa_node;
  2836. its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  2837. get_order(ITS_CMD_QUEUE_SZ));
  2838. if (!its->cmd_base) {
  2839. err = -ENOMEM;
  2840. goto out_free_its;
  2841. }
  2842. its->cmd_write = its->cmd_base;
  2843. its->fwnode_handle = handle;
  2844. its->get_msi_base = its_irq_get_msi_base;
  2845. its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
  2846. its_enable_quirks(its);
  2847. err = its_alloc_tables(its);
  2848. if (err)
  2849. goto out_free_cmd;
  2850. err = its_alloc_collections(its);
  2851. if (err)
  2852. goto out_free_tables;
  2853. baser = (virt_to_phys(its->cmd_base) |
  2854. GITS_CBASER_RaWaWb |
  2855. GITS_CBASER_InnerShareable |
  2856. (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
  2857. GITS_CBASER_VALID);
  2858. gits_write_cbaser(baser, its->base + GITS_CBASER);
  2859. tmp = gits_read_cbaser(its->base + GITS_CBASER);
  2860. if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
  2861. if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
  2862. /*
  2863. * The HW reports non-shareable, we must
  2864. * remove the cacheability attributes as
  2865. * well.
  2866. */
  2867. baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
  2868. GITS_CBASER_CACHEABILITY_MASK);
  2869. baser |= GITS_CBASER_nC;
  2870. gits_write_cbaser(baser, its->base + GITS_CBASER);
  2871. }
  2872. pr_info("ITS: using cache flushing for cmd queue\n");
  2873. its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
  2874. }
  2875. gits_write_cwriter(0, its->base + GITS_CWRITER);
  2876. ctlr = readl_relaxed(its->base + GITS_CTLR);
  2877. ctlr |= GITS_CTLR_ENABLE;
  2878. if (its->is_v4)
  2879. ctlr |= GITS_CTLR_ImDe;
  2880. writel_relaxed(ctlr, its->base + GITS_CTLR);
  2881. err = its_init_domain(handle, its);
  2882. if (err)
  2883. goto out_free_tables;
  2884. raw_spin_lock(&its_lock);
  2885. list_add(&its->entry, &its_nodes);
  2886. raw_spin_unlock(&its_lock);
  2887. return 0;
  2888. out_free_tables:
  2889. its_free_tables(its);
  2890. out_free_cmd:
  2891. free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
  2892. out_free_its:
  2893. kfree(its);
  2894. out_unmap:
  2895. iounmap(its_base);
  2896. pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
  2897. return err;
  2898. }
  2899. static bool gic_rdists_supports_plpis(void)
  2900. {
  2901. return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
  2902. }
  2903. static int redist_disable_lpis(void)
  2904. {
  2905. void __iomem *rbase = gic_data_rdist_rd_base();
  2906. u64 timeout = USEC_PER_SEC;
  2907. u64 val;
  2908. /*
  2909. * If coming via a CPU hotplug event, we don't need to disable
  2910. * LPIs before trying to re-enable them. They are already
  2911. * configured and all is well in the world. Detect this case
  2912. * by checking the allocation of the pending table for the
  2913. * current CPU.
  2914. */
  2915. if (gic_data_rdist()->pend_page)
  2916. return 0;
  2917. if (!gic_rdists_supports_plpis()) {
  2918. pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
  2919. return -ENXIO;
  2920. }
  2921. val = readl_relaxed(rbase + GICR_CTLR);
  2922. if (!(val & GICR_CTLR_ENABLE_LPIS))
  2923. return 0;
  2924. pr_warn("CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
  2925. smp_processor_id());
  2926. add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
  2927. /* Disable LPIs */
  2928. val &= ~GICR_CTLR_ENABLE_LPIS;
  2929. writel_relaxed(val, rbase + GICR_CTLR);
  2930. /* Make sure any change to GICR_CTLR is observable by the GIC */
  2931. dsb(sy);
  2932. /*
  2933. * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
  2934. * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
  2935. * Error out if we time out waiting for RWP to clear.
  2936. */
  2937. while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
  2938. if (!timeout) {
  2939. pr_err("CPU%d: Timeout while disabling LPIs\n",
  2940. smp_processor_id());
  2941. return -ETIMEDOUT;
  2942. }
  2943. udelay(1);
  2944. timeout--;
  2945. }
  2946. /*
  2947. * After it has been written to 1, it is IMPLEMENTATION
  2948. * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
  2949. * cleared to 0. Error out if clearing the bit failed.
  2950. */
  2951. if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
  2952. pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
  2953. return -EBUSY;
  2954. }
  2955. return 0;
  2956. }
  2957. int its_cpu_init(void)
  2958. {
  2959. if (!list_empty(&its_nodes)) {
  2960. int ret;
  2961. ret = redist_disable_lpis();
  2962. if (ret)
  2963. return ret;
  2964. its_cpu_init_lpis();
  2965. its_cpu_init_collections();
  2966. }
  2967. return 0;
  2968. }
  2969. static const struct of_device_id its_device_id[] = {
  2970. { .compatible = "arm,gic-v3-its", },
  2971. {},
  2972. };
  2973. static int __init its_of_probe(struct device_node *node)
  2974. {
  2975. struct device_node *np;
  2976. struct resource res;
  2977. for (np = of_find_matching_node(node, its_device_id); np;
  2978. np = of_find_matching_node(np, its_device_id)) {
  2979. if (!of_device_is_available(np))
  2980. continue;
  2981. if (!of_property_read_bool(np, "msi-controller")) {
  2982. pr_warn("%pOF: no msi-controller property, ITS ignored\n",
  2983. np);
  2984. continue;
  2985. }
  2986. if (of_address_to_resource(np, 0, &res)) {
  2987. pr_warn("%pOF: no regs?\n", np);
  2988. continue;
  2989. }
  2990. its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
  2991. }
  2992. return 0;
  2993. }
  2994. #ifdef CONFIG_ACPI
  2995. #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
  2996. #ifdef CONFIG_ACPI_NUMA
  2997. struct its_srat_map {
  2998. /* numa node id */
  2999. u32 numa_node;
  3000. /* GIC ITS ID */
  3001. u32 its_id;
  3002. };
  3003. static struct its_srat_map *its_srat_maps __initdata;
  3004. static int its_in_srat __initdata;
  3005. static int __init acpi_get_its_numa_node(u32 its_id)
  3006. {
  3007. int i;
  3008. for (i = 0; i < its_in_srat; i++) {
  3009. if (its_id == its_srat_maps[i].its_id)
  3010. return its_srat_maps[i].numa_node;
  3011. }
  3012. return NUMA_NO_NODE;
  3013. }
  3014. static int __init gic_acpi_match_srat_its(struct acpi_subtable_header *header,
  3015. const unsigned long end)
  3016. {
  3017. return 0;
  3018. }
  3019. static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
  3020. const unsigned long end)
  3021. {
  3022. int node;
  3023. struct acpi_srat_gic_its_affinity *its_affinity;
  3024. its_affinity = (struct acpi_srat_gic_its_affinity *)header;
  3025. if (!its_affinity)
  3026. return -EINVAL;
  3027. if (its_affinity->header.length < sizeof(*its_affinity)) {
  3028. pr_err("SRAT: Invalid header length %d in ITS affinity\n",
  3029. its_affinity->header.length);
  3030. return -EINVAL;
  3031. }
  3032. node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
  3033. if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
  3034. pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
  3035. return 0;
  3036. }
  3037. its_srat_maps[its_in_srat].numa_node = node;
  3038. its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
  3039. its_in_srat++;
  3040. pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
  3041. its_affinity->proximity_domain, its_affinity->its_id, node);
  3042. return 0;
  3043. }
  3044. static void __init acpi_table_parse_srat_its(void)
  3045. {
  3046. int count;
  3047. count = acpi_table_parse_entries(ACPI_SIG_SRAT,
  3048. sizeof(struct acpi_table_srat),
  3049. ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
  3050. gic_acpi_match_srat_its, 0);
  3051. if (count <= 0)
  3052. return;
  3053. its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
  3054. GFP_KERNEL);
  3055. if (!its_srat_maps) {
  3056. pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
  3057. return;
  3058. }
  3059. acpi_table_parse_entries(ACPI_SIG_SRAT,
  3060. sizeof(struct acpi_table_srat),
  3061. ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
  3062. gic_acpi_parse_srat_its, 0);
  3063. }
  3064. /* free the its_srat_maps after ITS probing */
  3065. static void __init acpi_its_srat_maps_free(void)
  3066. {
  3067. kfree(its_srat_maps);
  3068. }
  3069. #else
  3070. static void __init acpi_table_parse_srat_its(void) { }
  3071. static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
  3072. static void __init acpi_its_srat_maps_free(void) { }
  3073. #endif
  3074. static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
  3075. const unsigned long end)
  3076. {
  3077. struct acpi_madt_generic_translator *its_entry;
  3078. struct fwnode_handle *dom_handle;
  3079. struct resource res;
  3080. int err;
  3081. its_entry = (struct acpi_madt_generic_translator *)header;
  3082. memset(&res, 0, sizeof(res));
  3083. res.start = its_entry->base_address;
  3084. res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
  3085. res.flags = IORESOURCE_MEM;
  3086. dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
  3087. if (!dom_handle) {
  3088. pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
  3089. &res.start);
  3090. return -ENOMEM;
  3091. }
  3092. err = iort_register_domain_token(its_entry->translation_id, res.start,
  3093. dom_handle);
  3094. if (err) {
  3095. pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
  3096. &res.start, its_entry->translation_id);
  3097. goto dom_err;
  3098. }
  3099. err = its_probe_one(&res, dom_handle,
  3100. acpi_get_its_numa_node(its_entry->translation_id));
  3101. if (!err)
  3102. return 0;
  3103. iort_deregister_domain_token(its_entry->translation_id);
  3104. dom_err:
  3105. irq_domain_free_fwnode(dom_handle);
  3106. return err;
  3107. }
  3108. static void __init its_acpi_probe(void)
  3109. {
  3110. acpi_table_parse_srat_its();
  3111. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
  3112. gic_acpi_parse_madt_its, 0);
  3113. acpi_its_srat_maps_free();
  3114. }
  3115. #else
  3116. static void __init its_acpi_probe(void) { }
  3117. #endif
  3118. int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
  3119. struct irq_domain *parent_domain)
  3120. {
  3121. struct device_node *of_node;
  3122. struct its_node *its;
  3123. bool has_v4 = false;
  3124. int err;
  3125. its_parent = parent_domain;
  3126. of_node = to_of_node(handle);
  3127. if (of_node)
  3128. its_of_probe(of_node);
  3129. else
  3130. its_acpi_probe();
  3131. if (list_empty(&its_nodes)) {
  3132. pr_warn("ITS: No ITS available, not enabling LPIs\n");
  3133. return -ENXIO;
  3134. }
  3135. gic_rdists = rdists;
  3136. err = its_alloc_lpi_tables();
  3137. if (err)
  3138. return err;
  3139. list_for_each_entry(its, &its_nodes, entry)
  3140. has_v4 |= its->is_v4;
  3141. if (has_v4 & rdists->has_vlpis) {
  3142. if (its_init_vpe_domain() ||
  3143. its_init_v4(parent_domain, &its_vpe_domain_ops)) {
  3144. rdists->has_vlpis = false;
  3145. pr_err("ITS: Disabling GICv4 support\n");
  3146. }
  3147. }
  3148. register_syscore_ops(&its_syscore_ops);
  3149. return 0;
  3150. }