irq-gic-v3-its.c 86 KB

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