irq-gic-v3-its.c 93 KB

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