irq-gic-v3-its.c 80 KB

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