irq-gic-v3-its.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  1. /*
  2. * Copyright (C) 2013, 2014 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 <asm/cputype.h>
  38. #include <asm/exception.h>
  39. #include "irq-gic-common.h"
  40. #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
  41. #define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
  42. #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
  43. #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
  44. /*
  45. * Collection structure - just an ID, and a redistributor address to
  46. * ping. We use one per CPU as a bag of interrupts assigned to this
  47. * CPU.
  48. */
  49. struct its_collection {
  50. u64 target_address;
  51. u16 col_id;
  52. };
  53. /*
  54. * The ITS_BASER structure - contains memory information, cached
  55. * value of BASER register configuration and ITS page size.
  56. */
  57. struct its_baser {
  58. void *base;
  59. u64 val;
  60. u32 order;
  61. u32 psz;
  62. };
  63. /*
  64. * The ITS structure - contains most of the infrastructure, with the
  65. * top-level MSI domain, the command queue, the collections, and the
  66. * list of devices writing to it.
  67. */
  68. struct its_node {
  69. raw_spinlock_t lock;
  70. struct list_head entry;
  71. void __iomem *base;
  72. phys_addr_t phys_base;
  73. struct its_cmd_block *cmd_base;
  74. struct its_cmd_block *cmd_write;
  75. struct its_baser tables[GITS_BASER_NR_REGS];
  76. struct its_collection *collections;
  77. struct list_head its_device_list;
  78. u64 flags;
  79. u32 ite_size;
  80. u32 device_ids;
  81. int numa_node;
  82. };
  83. #define ITS_ITT_ALIGN SZ_256
  84. /* Convert page order to size in bytes */
  85. #define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
  86. struct event_lpi_map {
  87. unsigned long *lpi_map;
  88. u16 *col_map;
  89. irq_hw_number_t lpi_base;
  90. int nr_lpis;
  91. };
  92. /*
  93. * The ITS view of a device - belongs to an ITS, a collection, owns an
  94. * interrupt translation table, and a list of interrupts.
  95. */
  96. struct its_device {
  97. struct list_head entry;
  98. struct its_node *its;
  99. struct event_lpi_map event_map;
  100. void *itt;
  101. u32 nr_ites;
  102. u32 device_id;
  103. };
  104. static LIST_HEAD(its_nodes);
  105. static DEFINE_SPINLOCK(its_lock);
  106. static struct rdists *gic_rdists;
  107. static struct irq_domain *its_parent;
  108. #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
  109. #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
  110. static struct its_collection *dev_event_to_col(struct its_device *its_dev,
  111. u32 event)
  112. {
  113. struct its_node *its = its_dev->its;
  114. return its->collections + its_dev->event_map.col_map[event];
  115. }
  116. /*
  117. * ITS command descriptors - parameters to be encoded in a command
  118. * block.
  119. */
  120. struct its_cmd_desc {
  121. union {
  122. struct {
  123. struct its_device *dev;
  124. u32 event_id;
  125. } its_inv_cmd;
  126. struct {
  127. struct its_device *dev;
  128. u32 event_id;
  129. } its_int_cmd;
  130. struct {
  131. struct its_device *dev;
  132. int valid;
  133. } its_mapd_cmd;
  134. struct {
  135. struct its_collection *col;
  136. int valid;
  137. } its_mapc_cmd;
  138. struct {
  139. struct its_device *dev;
  140. u32 phys_id;
  141. u32 event_id;
  142. } its_mapti_cmd;
  143. struct {
  144. struct its_device *dev;
  145. struct its_collection *col;
  146. u32 event_id;
  147. } its_movi_cmd;
  148. struct {
  149. struct its_device *dev;
  150. u32 event_id;
  151. } its_discard_cmd;
  152. struct {
  153. struct its_collection *col;
  154. } its_invall_cmd;
  155. };
  156. };
  157. /*
  158. * The ITS command block, which is what the ITS actually parses.
  159. */
  160. struct its_cmd_block {
  161. u64 raw_cmd[4];
  162. };
  163. #define ITS_CMD_QUEUE_SZ SZ_64K
  164. #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
  165. typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
  166. struct its_cmd_desc *);
  167. static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
  168. {
  169. u64 mask = GENMASK_ULL(h, l);
  170. *raw_cmd &= ~mask;
  171. *raw_cmd |= (val << l) & mask;
  172. }
  173. static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
  174. {
  175. its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
  176. }
  177. static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
  178. {
  179. its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
  180. }
  181. static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
  182. {
  183. its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
  184. }
  185. static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
  186. {
  187. its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
  188. }
  189. static void its_encode_size(struct its_cmd_block *cmd, u8 size)
  190. {
  191. its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
  192. }
  193. static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
  194. {
  195. its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8);
  196. }
  197. static void its_encode_valid(struct its_cmd_block *cmd, int valid)
  198. {
  199. its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
  200. }
  201. static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
  202. {
  203. its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16);
  204. }
  205. static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
  206. {
  207. its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
  208. }
  209. static inline void its_fixup_cmd(struct its_cmd_block *cmd)
  210. {
  211. /* Let's fixup BE commands */
  212. cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
  213. cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
  214. cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
  215. cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
  216. }
  217. static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
  218. struct its_cmd_desc *desc)
  219. {
  220. unsigned long itt_addr;
  221. u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
  222. itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
  223. itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
  224. its_encode_cmd(cmd, GITS_CMD_MAPD);
  225. its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
  226. its_encode_size(cmd, size - 1);
  227. its_encode_itt(cmd, itt_addr);
  228. its_encode_valid(cmd, desc->its_mapd_cmd.valid);
  229. its_fixup_cmd(cmd);
  230. return NULL;
  231. }
  232. static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
  233. struct its_cmd_desc *desc)
  234. {
  235. its_encode_cmd(cmd, GITS_CMD_MAPC);
  236. its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
  237. its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
  238. its_encode_valid(cmd, desc->its_mapc_cmd.valid);
  239. its_fixup_cmd(cmd);
  240. return desc->its_mapc_cmd.col;
  241. }
  242. static struct its_collection *its_build_mapti_cmd(struct its_cmd_block *cmd,
  243. struct its_cmd_desc *desc)
  244. {
  245. struct its_collection *col;
  246. col = dev_event_to_col(desc->its_mapti_cmd.dev,
  247. desc->its_mapti_cmd.event_id);
  248. its_encode_cmd(cmd, GITS_CMD_MAPTI);
  249. its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
  250. its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
  251. its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
  252. its_encode_collection(cmd, col->col_id);
  253. its_fixup_cmd(cmd);
  254. return col;
  255. }
  256. static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
  257. struct its_cmd_desc *desc)
  258. {
  259. struct its_collection *col;
  260. col = dev_event_to_col(desc->its_movi_cmd.dev,
  261. desc->its_movi_cmd.event_id);
  262. its_encode_cmd(cmd, GITS_CMD_MOVI);
  263. its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
  264. its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
  265. its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
  266. its_fixup_cmd(cmd);
  267. return col;
  268. }
  269. static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
  270. struct its_cmd_desc *desc)
  271. {
  272. struct its_collection *col;
  273. col = dev_event_to_col(desc->its_discard_cmd.dev,
  274. desc->its_discard_cmd.event_id);
  275. its_encode_cmd(cmd, GITS_CMD_DISCARD);
  276. its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
  277. its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
  278. its_fixup_cmd(cmd);
  279. return col;
  280. }
  281. static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
  282. struct its_cmd_desc *desc)
  283. {
  284. struct its_collection *col;
  285. col = dev_event_to_col(desc->its_inv_cmd.dev,
  286. desc->its_inv_cmd.event_id);
  287. its_encode_cmd(cmd, GITS_CMD_INV);
  288. its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
  289. its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
  290. its_fixup_cmd(cmd);
  291. return col;
  292. }
  293. static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
  294. struct its_cmd_desc *desc)
  295. {
  296. its_encode_cmd(cmd, GITS_CMD_INVALL);
  297. its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
  298. its_fixup_cmd(cmd);
  299. return NULL;
  300. }
  301. static u64 its_cmd_ptr_to_offset(struct its_node *its,
  302. struct its_cmd_block *ptr)
  303. {
  304. return (ptr - its->cmd_base) * sizeof(*ptr);
  305. }
  306. static int its_queue_full(struct its_node *its)
  307. {
  308. int widx;
  309. int ridx;
  310. widx = its->cmd_write - its->cmd_base;
  311. ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
  312. /* This is incredibly unlikely to happen, unless the ITS locks up. */
  313. if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
  314. return 1;
  315. return 0;
  316. }
  317. static struct its_cmd_block *its_allocate_entry(struct its_node *its)
  318. {
  319. struct its_cmd_block *cmd;
  320. u32 count = 1000000; /* 1s! */
  321. while (its_queue_full(its)) {
  322. count--;
  323. if (!count) {
  324. pr_err_ratelimited("ITS queue not draining\n");
  325. return NULL;
  326. }
  327. cpu_relax();
  328. udelay(1);
  329. }
  330. cmd = its->cmd_write++;
  331. /* Handle queue wrapping */
  332. if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
  333. its->cmd_write = its->cmd_base;
  334. /* Clear command */
  335. cmd->raw_cmd[0] = 0;
  336. cmd->raw_cmd[1] = 0;
  337. cmd->raw_cmd[2] = 0;
  338. cmd->raw_cmd[3] = 0;
  339. return cmd;
  340. }
  341. static struct its_cmd_block *its_post_commands(struct its_node *its)
  342. {
  343. u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
  344. writel_relaxed(wr, its->base + GITS_CWRITER);
  345. return its->cmd_write;
  346. }
  347. static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
  348. {
  349. /*
  350. * Make sure the commands written to memory are observable by
  351. * the ITS.
  352. */
  353. if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
  354. gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
  355. else
  356. dsb(ishst);
  357. }
  358. static void its_wait_for_range_completion(struct its_node *its,
  359. struct its_cmd_block *from,
  360. struct its_cmd_block *to)
  361. {
  362. u64 rd_idx, from_idx, to_idx;
  363. u32 count = 1000000; /* 1s! */
  364. from_idx = its_cmd_ptr_to_offset(its, from);
  365. to_idx = its_cmd_ptr_to_offset(its, to);
  366. while (1) {
  367. rd_idx = readl_relaxed(its->base + GITS_CREADR);
  368. if (rd_idx >= to_idx || rd_idx < from_idx)
  369. break;
  370. count--;
  371. if (!count) {
  372. pr_err_ratelimited("ITS queue timeout\n");
  373. return;
  374. }
  375. cpu_relax();
  376. udelay(1);
  377. }
  378. }
  379. static void its_send_single_command(struct its_node *its,
  380. its_cmd_builder_t builder,
  381. struct its_cmd_desc *desc)
  382. {
  383. struct its_cmd_block *cmd, *sync_cmd, *next_cmd;
  384. struct its_collection *sync_col;
  385. unsigned long flags;
  386. raw_spin_lock_irqsave(&its->lock, flags);
  387. cmd = its_allocate_entry(its);
  388. if (!cmd) { /* We're soooooo screewed... */
  389. pr_err_ratelimited("ITS can't allocate, dropping command\n");
  390. raw_spin_unlock_irqrestore(&its->lock, flags);
  391. return;
  392. }
  393. sync_col = builder(cmd, desc);
  394. its_flush_cmd(its, cmd);
  395. if (sync_col) {
  396. sync_cmd = its_allocate_entry(its);
  397. if (!sync_cmd) {
  398. pr_err_ratelimited("ITS can't SYNC, skipping\n");
  399. goto post;
  400. }
  401. its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
  402. its_encode_target(sync_cmd, sync_col->target_address);
  403. its_fixup_cmd(sync_cmd);
  404. its_flush_cmd(its, sync_cmd);
  405. }
  406. post:
  407. next_cmd = its_post_commands(its);
  408. raw_spin_unlock_irqrestore(&its->lock, flags);
  409. its_wait_for_range_completion(its, cmd, next_cmd);
  410. }
  411. static void its_send_inv(struct its_device *dev, u32 event_id)
  412. {
  413. struct its_cmd_desc desc;
  414. desc.its_inv_cmd.dev = dev;
  415. desc.its_inv_cmd.event_id = event_id;
  416. its_send_single_command(dev->its, its_build_inv_cmd, &desc);
  417. }
  418. static void its_send_mapd(struct its_device *dev, int valid)
  419. {
  420. struct its_cmd_desc desc;
  421. desc.its_mapd_cmd.dev = dev;
  422. desc.its_mapd_cmd.valid = !!valid;
  423. its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
  424. }
  425. static void its_send_mapc(struct its_node *its, struct its_collection *col,
  426. int valid)
  427. {
  428. struct its_cmd_desc desc;
  429. desc.its_mapc_cmd.col = col;
  430. desc.its_mapc_cmd.valid = !!valid;
  431. its_send_single_command(its, its_build_mapc_cmd, &desc);
  432. }
  433. static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
  434. {
  435. struct its_cmd_desc desc;
  436. desc.its_mapti_cmd.dev = dev;
  437. desc.its_mapti_cmd.phys_id = irq_id;
  438. desc.its_mapti_cmd.event_id = id;
  439. its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
  440. }
  441. static void its_send_movi(struct its_device *dev,
  442. struct its_collection *col, u32 id)
  443. {
  444. struct its_cmd_desc desc;
  445. desc.its_movi_cmd.dev = dev;
  446. desc.its_movi_cmd.col = col;
  447. desc.its_movi_cmd.event_id = id;
  448. its_send_single_command(dev->its, its_build_movi_cmd, &desc);
  449. }
  450. static void its_send_discard(struct its_device *dev, u32 id)
  451. {
  452. struct its_cmd_desc desc;
  453. desc.its_discard_cmd.dev = dev;
  454. desc.its_discard_cmd.event_id = id;
  455. its_send_single_command(dev->its, its_build_discard_cmd, &desc);
  456. }
  457. static void its_send_invall(struct its_node *its, struct its_collection *col)
  458. {
  459. struct its_cmd_desc desc;
  460. desc.its_invall_cmd.col = col;
  461. its_send_single_command(its, its_build_invall_cmd, &desc);
  462. }
  463. /*
  464. * irqchip functions - assumes MSI, mostly.
  465. */
  466. static inline u32 its_get_event_id(struct irq_data *d)
  467. {
  468. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  469. return d->hwirq - its_dev->event_map.lpi_base;
  470. }
  471. static void lpi_set_config(struct irq_data *d, bool enable)
  472. {
  473. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  474. irq_hw_number_t hwirq = d->hwirq;
  475. u32 id = its_get_event_id(d);
  476. u8 *cfg = page_address(gic_rdists->prop_page) + hwirq - 8192;
  477. if (enable)
  478. *cfg |= LPI_PROP_ENABLED;
  479. else
  480. *cfg &= ~LPI_PROP_ENABLED;
  481. /*
  482. * Make the above write visible to the redistributors.
  483. * And yes, we're flushing exactly: One. Single. Byte.
  484. * Humpf...
  485. */
  486. if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
  487. gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
  488. else
  489. dsb(ishst);
  490. its_send_inv(its_dev, id);
  491. }
  492. static void its_mask_irq(struct irq_data *d)
  493. {
  494. lpi_set_config(d, false);
  495. }
  496. static void its_unmask_irq(struct irq_data *d)
  497. {
  498. lpi_set_config(d, true);
  499. }
  500. static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  501. bool force)
  502. {
  503. unsigned int cpu;
  504. const struct cpumask *cpu_mask = cpu_online_mask;
  505. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  506. struct its_collection *target_col;
  507. u32 id = its_get_event_id(d);
  508. /* lpi cannot be routed to a redistributor that is on a foreign node */
  509. if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
  510. if (its_dev->its->numa_node >= 0) {
  511. cpu_mask = cpumask_of_node(its_dev->its->numa_node);
  512. if (!cpumask_intersects(mask_val, cpu_mask))
  513. return -EINVAL;
  514. }
  515. }
  516. cpu = cpumask_any_and(mask_val, cpu_mask);
  517. if (cpu >= nr_cpu_ids)
  518. return -EINVAL;
  519. /* don't set the affinity when the target cpu is same as current one */
  520. if (cpu != its_dev->event_map.col_map[id]) {
  521. target_col = &its_dev->its->collections[cpu];
  522. its_send_movi(its_dev, target_col, id);
  523. its_dev->event_map.col_map[id] = cpu;
  524. }
  525. return IRQ_SET_MASK_OK_DONE;
  526. }
  527. static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
  528. {
  529. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  530. struct its_node *its;
  531. u64 addr;
  532. its = its_dev->its;
  533. addr = its->phys_base + GITS_TRANSLATER;
  534. msg->address_lo = lower_32_bits(addr);
  535. msg->address_hi = upper_32_bits(addr);
  536. msg->data = its_get_event_id(d);
  537. iommu_dma_map_msi_msg(d->irq, msg);
  538. }
  539. static struct irq_chip its_irq_chip = {
  540. .name = "ITS",
  541. .irq_mask = its_mask_irq,
  542. .irq_unmask = its_unmask_irq,
  543. .irq_eoi = irq_chip_eoi_parent,
  544. .irq_set_affinity = its_set_affinity,
  545. .irq_compose_msi_msg = its_irq_compose_msi_msg,
  546. };
  547. /*
  548. * How we allocate LPIs:
  549. *
  550. * The GIC has id_bits bits for interrupt identifiers. From there, we
  551. * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
  552. * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
  553. * bits to the right.
  554. *
  555. * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
  556. */
  557. #define IRQS_PER_CHUNK_SHIFT 5
  558. #define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
  559. #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
  560. static unsigned long *lpi_bitmap;
  561. static u32 lpi_chunks;
  562. static u32 lpi_id_bits;
  563. static DEFINE_SPINLOCK(lpi_lock);
  564. static int its_lpi_to_chunk(int lpi)
  565. {
  566. return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
  567. }
  568. static int its_chunk_to_lpi(int chunk)
  569. {
  570. return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
  571. }
  572. static int __init its_lpi_init(u32 id_bits)
  573. {
  574. lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
  575. lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
  576. GFP_KERNEL);
  577. if (!lpi_bitmap) {
  578. lpi_chunks = 0;
  579. return -ENOMEM;
  580. }
  581. pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
  582. return 0;
  583. }
  584. static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
  585. {
  586. unsigned long *bitmap = NULL;
  587. int chunk_id;
  588. int nr_chunks;
  589. int i;
  590. nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
  591. spin_lock(&lpi_lock);
  592. do {
  593. chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
  594. 0, nr_chunks, 0);
  595. if (chunk_id < lpi_chunks)
  596. break;
  597. nr_chunks--;
  598. } while (nr_chunks > 0);
  599. if (!nr_chunks)
  600. goto out;
  601. bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
  602. GFP_ATOMIC);
  603. if (!bitmap)
  604. goto out;
  605. for (i = 0; i < nr_chunks; i++)
  606. set_bit(chunk_id + i, lpi_bitmap);
  607. *base = its_chunk_to_lpi(chunk_id);
  608. *nr_ids = nr_chunks * IRQS_PER_CHUNK;
  609. out:
  610. spin_unlock(&lpi_lock);
  611. if (!bitmap)
  612. *base = *nr_ids = 0;
  613. return bitmap;
  614. }
  615. static void its_lpi_free(struct event_lpi_map *map)
  616. {
  617. int base = map->lpi_base;
  618. int nr_ids = map->nr_lpis;
  619. int lpi;
  620. spin_lock(&lpi_lock);
  621. for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
  622. int chunk = its_lpi_to_chunk(lpi);
  623. BUG_ON(chunk > lpi_chunks);
  624. if (test_bit(chunk, lpi_bitmap)) {
  625. clear_bit(chunk, lpi_bitmap);
  626. } else {
  627. pr_err("Bad LPI chunk %d\n", chunk);
  628. }
  629. }
  630. spin_unlock(&lpi_lock);
  631. kfree(map->lpi_map);
  632. kfree(map->col_map);
  633. }
  634. /*
  635. * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
  636. * deal with (one configuration byte per interrupt). PENDBASE has to
  637. * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
  638. */
  639. #define LPI_NRBITS lpi_id_bits
  640. #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
  641. #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
  642. #define LPI_PROP_DEFAULT_PRIO 0xa0
  643. static int __init its_alloc_lpi_tables(void)
  644. {
  645. phys_addr_t paddr;
  646. lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
  647. gic_rdists->prop_page = alloc_pages(GFP_NOWAIT,
  648. get_order(LPI_PROPBASE_SZ));
  649. if (!gic_rdists->prop_page) {
  650. pr_err("Failed to allocate PROPBASE\n");
  651. return -ENOMEM;
  652. }
  653. paddr = page_to_phys(gic_rdists->prop_page);
  654. pr_info("GIC: using LPI property table @%pa\n", &paddr);
  655. /* Priority 0xa0, Group-1, disabled */
  656. memset(page_address(gic_rdists->prop_page),
  657. LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
  658. LPI_PROPBASE_SZ);
  659. /* Make sure the GIC will observe the written configuration */
  660. gic_flush_dcache_to_poc(page_address(gic_rdists->prop_page), LPI_PROPBASE_SZ);
  661. return its_lpi_init(lpi_id_bits);
  662. }
  663. static const char *its_base_type_string[] = {
  664. [GITS_BASER_TYPE_DEVICE] = "Devices",
  665. [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
  666. [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
  667. [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
  668. [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
  669. [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
  670. [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
  671. };
  672. static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
  673. {
  674. u32 idx = baser - its->tables;
  675. return gits_read_baser(its->base + GITS_BASER + (idx << 3));
  676. }
  677. static void its_write_baser(struct its_node *its, struct its_baser *baser,
  678. u64 val)
  679. {
  680. u32 idx = baser - its->tables;
  681. gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
  682. baser->val = its_read_baser(its, baser);
  683. }
  684. static int its_setup_baser(struct its_node *its, struct its_baser *baser,
  685. u64 cache, u64 shr, u32 psz, u32 order,
  686. bool indirect)
  687. {
  688. u64 val = its_read_baser(its, baser);
  689. u64 esz = GITS_BASER_ENTRY_SIZE(val);
  690. u64 type = GITS_BASER_TYPE(val);
  691. u32 alloc_pages;
  692. void *base;
  693. u64 tmp;
  694. retry_alloc_baser:
  695. alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
  696. if (alloc_pages > GITS_BASER_PAGES_MAX) {
  697. pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
  698. &its->phys_base, its_base_type_string[type],
  699. alloc_pages, GITS_BASER_PAGES_MAX);
  700. alloc_pages = GITS_BASER_PAGES_MAX;
  701. order = get_order(GITS_BASER_PAGES_MAX * psz);
  702. }
  703. base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  704. if (!base)
  705. return -ENOMEM;
  706. retry_baser:
  707. val = (virt_to_phys(base) |
  708. (type << GITS_BASER_TYPE_SHIFT) |
  709. ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
  710. ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
  711. cache |
  712. shr |
  713. GITS_BASER_VALID);
  714. val |= indirect ? GITS_BASER_INDIRECT : 0x0;
  715. switch (psz) {
  716. case SZ_4K:
  717. val |= GITS_BASER_PAGE_SIZE_4K;
  718. break;
  719. case SZ_16K:
  720. val |= GITS_BASER_PAGE_SIZE_16K;
  721. break;
  722. case SZ_64K:
  723. val |= GITS_BASER_PAGE_SIZE_64K;
  724. break;
  725. }
  726. its_write_baser(its, baser, val);
  727. tmp = baser->val;
  728. if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
  729. /*
  730. * Shareability didn't stick. Just use
  731. * whatever the read reported, which is likely
  732. * to be the only thing this redistributor
  733. * supports. If that's zero, make it
  734. * non-cacheable as well.
  735. */
  736. shr = tmp & GITS_BASER_SHAREABILITY_MASK;
  737. if (!shr) {
  738. cache = GITS_BASER_nC;
  739. gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
  740. }
  741. goto retry_baser;
  742. }
  743. if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
  744. /*
  745. * Page size didn't stick. Let's try a smaller
  746. * size and retry. If we reach 4K, then
  747. * something is horribly wrong...
  748. */
  749. free_pages((unsigned long)base, order);
  750. baser->base = NULL;
  751. switch (psz) {
  752. case SZ_16K:
  753. psz = SZ_4K;
  754. goto retry_alloc_baser;
  755. case SZ_64K:
  756. psz = SZ_16K;
  757. goto retry_alloc_baser;
  758. }
  759. }
  760. if (val != tmp) {
  761. pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
  762. &its->phys_base, its_base_type_string[type],
  763. val, tmp);
  764. free_pages((unsigned long)base, order);
  765. return -ENXIO;
  766. }
  767. baser->order = order;
  768. baser->base = base;
  769. baser->psz = psz;
  770. tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
  771. pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
  772. &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
  773. its_base_type_string[type],
  774. (unsigned long)virt_to_phys(base),
  775. indirect ? "indirect" : "flat", (int)esz,
  776. psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
  777. return 0;
  778. }
  779. static bool its_parse_baser_device(struct its_node *its, struct its_baser *baser,
  780. u32 psz, u32 *order)
  781. {
  782. u64 esz = GITS_BASER_ENTRY_SIZE(its_read_baser(its, baser));
  783. u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
  784. u32 ids = its->device_ids;
  785. u32 new_order = *order;
  786. bool indirect = false;
  787. /* No need to enable Indirection if memory requirement < (psz*2)bytes */
  788. if ((esz << ids) > (psz * 2)) {
  789. /*
  790. * Find out whether hw supports a single or two-level table by
  791. * table by reading bit at offset '62' after writing '1' to it.
  792. */
  793. its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
  794. indirect = !!(baser->val & GITS_BASER_INDIRECT);
  795. if (indirect) {
  796. /*
  797. * The size of the lvl2 table is equal to ITS page size
  798. * which is 'psz'. For computing lvl1 table size,
  799. * subtract ID bits that sparse lvl2 table from 'ids'
  800. * which is reported by ITS hardware times lvl1 table
  801. * entry size.
  802. */
  803. ids -= ilog2(psz / (int)esz);
  804. esz = GITS_LVL1_ENTRY_SIZE;
  805. }
  806. }
  807. /*
  808. * Allocate as many entries as required to fit the
  809. * range of device IDs that the ITS can grok... The ID
  810. * space being incredibly sparse, this results in a
  811. * massive waste of memory if two-level device table
  812. * feature is not supported by hardware.
  813. */
  814. new_order = max_t(u32, get_order(esz << ids), new_order);
  815. if (new_order >= MAX_ORDER) {
  816. new_order = MAX_ORDER - 1;
  817. ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
  818. pr_warn("ITS@%pa: Device Table too large, reduce ids %u->%u\n",
  819. &its->phys_base, its->device_ids, ids);
  820. }
  821. *order = new_order;
  822. return indirect;
  823. }
  824. static void its_free_tables(struct its_node *its)
  825. {
  826. int i;
  827. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  828. if (its->tables[i].base) {
  829. free_pages((unsigned long)its->tables[i].base,
  830. its->tables[i].order);
  831. its->tables[i].base = NULL;
  832. }
  833. }
  834. }
  835. static int its_alloc_tables(struct its_node *its)
  836. {
  837. u64 typer = gic_read_typer(its->base + GITS_TYPER);
  838. u32 ids = GITS_TYPER_DEVBITS(typer);
  839. u64 shr = GITS_BASER_InnerShareable;
  840. u64 cache = GITS_BASER_RaWaWb;
  841. u32 psz = SZ_64K;
  842. int err, i;
  843. if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
  844. /*
  845. * erratum 22375: only alloc 8MB table size
  846. * erratum 24313: ignore memory access type
  847. */
  848. cache = GITS_BASER_nCnB;
  849. ids = 0x14; /* 20 bits, 8MB */
  850. }
  851. its->device_ids = ids;
  852. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  853. struct its_baser *baser = its->tables + i;
  854. u64 val = its_read_baser(its, baser);
  855. u64 type = GITS_BASER_TYPE(val);
  856. u32 order = get_order(psz);
  857. bool indirect = false;
  858. if (type == GITS_BASER_TYPE_NONE)
  859. continue;
  860. if (type == GITS_BASER_TYPE_DEVICE)
  861. indirect = its_parse_baser_device(its, baser, psz, &order);
  862. err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
  863. if (err < 0) {
  864. its_free_tables(its);
  865. return err;
  866. }
  867. /* Update settings which will be used for next BASERn */
  868. psz = baser->psz;
  869. cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
  870. shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
  871. }
  872. return 0;
  873. }
  874. static int its_alloc_collections(struct its_node *its)
  875. {
  876. its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
  877. GFP_KERNEL);
  878. if (!its->collections)
  879. return -ENOMEM;
  880. return 0;
  881. }
  882. static void its_cpu_init_lpis(void)
  883. {
  884. void __iomem *rbase = gic_data_rdist_rd_base();
  885. struct page *pend_page;
  886. u64 val, tmp;
  887. /* If we didn't allocate the pending table yet, do it now */
  888. pend_page = gic_data_rdist()->pend_page;
  889. if (!pend_page) {
  890. phys_addr_t paddr;
  891. /*
  892. * The pending pages have to be at least 64kB aligned,
  893. * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
  894. */
  895. pend_page = alloc_pages(GFP_NOWAIT | __GFP_ZERO,
  896. get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
  897. if (!pend_page) {
  898. pr_err("Failed to allocate PENDBASE for CPU%d\n",
  899. smp_processor_id());
  900. return;
  901. }
  902. /* Make sure the GIC will observe the zero-ed page */
  903. gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
  904. paddr = page_to_phys(pend_page);
  905. pr_info("CPU%d: using LPI pending table @%pa\n",
  906. smp_processor_id(), &paddr);
  907. gic_data_rdist()->pend_page = pend_page;
  908. }
  909. /* Disable LPIs */
  910. val = readl_relaxed(rbase + GICR_CTLR);
  911. val &= ~GICR_CTLR_ENABLE_LPIS;
  912. writel_relaxed(val, rbase + GICR_CTLR);
  913. /*
  914. * Make sure any change to the table is observable by the GIC.
  915. */
  916. dsb(sy);
  917. /* set PROPBASE */
  918. val = (page_to_phys(gic_rdists->prop_page) |
  919. GICR_PROPBASER_InnerShareable |
  920. GICR_PROPBASER_RaWaWb |
  921. ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
  922. gicr_write_propbaser(val, rbase + GICR_PROPBASER);
  923. tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
  924. if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
  925. if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
  926. /*
  927. * The HW reports non-shareable, we must
  928. * remove the cacheability attributes as
  929. * well.
  930. */
  931. val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
  932. GICR_PROPBASER_CACHEABILITY_MASK);
  933. val |= GICR_PROPBASER_nC;
  934. gicr_write_propbaser(val, rbase + GICR_PROPBASER);
  935. }
  936. pr_info_once("GIC: using cache flushing for LPI property table\n");
  937. gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
  938. }
  939. /* set PENDBASE */
  940. val = (page_to_phys(pend_page) |
  941. GICR_PENDBASER_InnerShareable |
  942. GICR_PENDBASER_RaWaWb);
  943. gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
  944. tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
  945. if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
  946. /*
  947. * The HW reports non-shareable, we must remove the
  948. * cacheability attributes as well.
  949. */
  950. val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
  951. GICR_PENDBASER_CACHEABILITY_MASK);
  952. val |= GICR_PENDBASER_nC;
  953. gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
  954. }
  955. /* Enable LPIs */
  956. val = readl_relaxed(rbase + GICR_CTLR);
  957. val |= GICR_CTLR_ENABLE_LPIS;
  958. writel_relaxed(val, rbase + GICR_CTLR);
  959. /* Make sure the GIC has seen the above */
  960. dsb(sy);
  961. }
  962. static void its_cpu_init_collection(void)
  963. {
  964. struct its_node *its;
  965. int cpu;
  966. spin_lock(&its_lock);
  967. cpu = smp_processor_id();
  968. list_for_each_entry(its, &its_nodes, entry) {
  969. u64 target;
  970. /* avoid cross node collections and its mapping */
  971. if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
  972. struct device_node *cpu_node;
  973. cpu_node = of_get_cpu_node(cpu, NULL);
  974. if (its->numa_node != NUMA_NO_NODE &&
  975. its->numa_node != of_node_to_nid(cpu_node))
  976. continue;
  977. }
  978. /*
  979. * We now have to bind each collection to its target
  980. * redistributor.
  981. */
  982. if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
  983. /*
  984. * This ITS wants the physical address of the
  985. * redistributor.
  986. */
  987. target = gic_data_rdist()->phys_base;
  988. } else {
  989. /*
  990. * This ITS wants a linear CPU number.
  991. */
  992. target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
  993. target = GICR_TYPER_CPU_NUMBER(target) << 16;
  994. }
  995. /* Perform collection mapping */
  996. its->collections[cpu].target_address = target;
  997. its->collections[cpu].col_id = cpu;
  998. its_send_mapc(its, &its->collections[cpu], 1);
  999. its_send_invall(its, &its->collections[cpu]);
  1000. }
  1001. spin_unlock(&its_lock);
  1002. }
  1003. static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
  1004. {
  1005. struct its_device *its_dev = NULL, *tmp;
  1006. unsigned long flags;
  1007. raw_spin_lock_irqsave(&its->lock, flags);
  1008. list_for_each_entry(tmp, &its->its_device_list, entry) {
  1009. if (tmp->device_id == dev_id) {
  1010. its_dev = tmp;
  1011. break;
  1012. }
  1013. }
  1014. raw_spin_unlock_irqrestore(&its->lock, flags);
  1015. return its_dev;
  1016. }
  1017. static struct its_baser *its_get_baser(struct its_node *its, u32 type)
  1018. {
  1019. int i;
  1020. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  1021. if (GITS_BASER_TYPE(its->tables[i].val) == type)
  1022. return &its->tables[i];
  1023. }
  1024. return NULL;
  1025. }
  1026. static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
  1027. {
  1028. struct its_baser *baser;
  1029. struct page *page;
  1030. u32 esz, idx;
  1031. __le64 *table;
  1032. baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
  1033. /* Don't allow device id that exceeds ITS hardware limit */
  1034. if (!baser)
  1035. return (ilog2(dev_id) < its->device_ids);
  1036. /* Don't allow device id that exceeds single, flat table limit */
  1037. esz = GITS_BASER_ENTRY_SIZE(baser->val);
  1038. if (!(baser->val & GITS_BASER_INDIRECT))
  1039. return (dev_id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
  1040. /* Compute 1st level table index & check if that exceeds table limit */
  1041. idx = dev_id >> ilog2(baser->psz / esz);
  1042. if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
  1043. return false;
  1044. table = baser->base;
  1045. /* Allocate memory for 2nd level table */
  1046. if (!table[idx]) {
  1047. page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
  1048. if (!page)
  1049. return false;
  1050. /* Flush Lvl2 table to PoC if hw doesn't support coherency */
  1051. if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
  1052. gic_flush_dcache_to_poc(page_address(page), baser->psz);
  1053. table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
  1054. /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
  1055. if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
  1056. gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
  1057. /* Ensure updated table contents are visible to ITS hardware */
  1058. dsb(sy);
  1059. }
  1060. return true;
  1061. }
  1062. static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
  1063. int nvecs)
  1064. {
  1065. struct its_device *dev;
  1066. unsigned long *lpi_map;
  1067. unsigned long flags;
  1068. u16 *col_map = NULL;
  1069. void *itt;
  1070. int lpi_base;
  1071. int nr_lpis;
  1072. int nr_ites;
  1073. int sz;
  1074. if (!its_alloc_device_table(its, dev_id))
  1075. return NULL;
  1076. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1077. /*
  1078. * At least one bit of EventID is being used, hence a minimum
  1079. * of two entries. No, the architecture doesn't let you
  1080. * express an ITT with a single entry.
  1081. */
  1082. nr_ites = max(2UL, roundup_pow_of_two(nvecs));
  1083. sz = nr_ites * its->ite_size;
  1084. sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
  1085. itt = kzalloc(sz, GFP_KERNEL);
  1086. lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
  1087. if (lpi_map)
  1088. col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
  1089. if (!dev || !itt || !lpi_map || !col_map) {
  1090. kfree(dev);
  1091. kfree(itt);
  1092. kfree(lpi_map);
  1093. kfree(col_map);
  1094. return NULL;
  1095. }
  1096. gic_flush_dcache_to_poc(itt, sz);
  1097. dev->its = its;
  1098. dev->itt = itt;
  1099. dev->nr_ites = nr_ites;
  1100. dev->event_map.lpi_map = lpi_map;
  1101. dev->event_map.col_map = col_map;
  1102. dev->event_map.lpi_base = lpi_base;
  1103. dev->event_map.nr_lpis = nr_lpis;
  1104. dev->device_id = dev_id;
  1105. INIT_LIST_HEAD(&dev->entry);
  1106. raw_spin_lock_irqsave(&its->lock, flags);
  1107. list_add(&dev->entry, &its->its_device_list);
  1108. raw_spin_unlock_irqrestore(&its->lock, flags);
  1109. /* Map device to its ITT */
  1110. its_send_mapd(dev, 1);
  1111. return dev;
  1112. }
  1113. static void its_free_device(struct its_device *its_dev)
  1114. {
  1115. unsigned long flags;
  1116. raw_spin_lock_irqsave(&its_dev->its->lock, flags);
  1117. list_del(&its_dev->entry);
  1118. raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
  1119. kfree(its_dev->itt);
  1120. kfree(its_dev);
  1121. }
  1122. static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
  1123. {
  1124. int idx;
  1125. idx = find_first_zero_bit(dev->event_map.lpi_map,
  1126. dev->event_map.nr_lpis);
  1127. if (idx == dev->event_map.nr_lpis)
  1128. return -ENOSPC;
  1129. *hwirq = dev->event_map.lpi_base + idx;
  1130. set_bit(idx, dev->event_map.lpi_map);
  1131. return 0;
  1132. }
  1133. static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
  1134. int nvec, msi_alloc_info_t *info)
  1135. {
  1136. struct its_node *its;
  1137. struct its_device *its_dev;
  1138. struct msi_domain_info *msi_info;
  1139. u32 dev_id;
  1140. /*
  1141. * We ignore "dev" entierely, and rely on the dev_id that has
  1142. * been passed via the scratchpad. This limits this domain's
  1143. * usefulness to upper layers that definitely know that they
  1144. * are built on top of the ITS.
  1145. */
  1146. dev_id = info->scratchpad[0].ul;
  1147. msi_info = msi_get_domain_info(domain);
  1148. its = msi_info->data;
  1149. its_dev = its_find_device(its, dev_id);
  1150. if (its_dev) {
  1151. /*
  1152. * We already have seen this ID, probably through
  1153. * another alias (PCI bridge of some sort). No need to
  1154. * create the device.
  1155. */
  1156. pr_debug("Reusing ITT for devID %x\n", dev_id);
  1157. goto out;
  1158. }
  1159. its_dev = its_create_device(its, dev_id, nvec);
  1160. if (!its_dev)
  1161. return -ENOMEM;
  1162. pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
  1163. out:
  1164. info->scratchpad[0].ptr = its_dev;
  1165. return 0;
  1166. }
  1167. static struct msi_domain_ops its_msi_domain_ops = {
  1168. .msi_prepare = its_msi_prepare,
  1169. };
  1170. static int its_irq_gic_domain_alloc(struct irq_domain *domain,
  1171. unsigned int virq,
  1172. irq_hw_number_t hwirq)
  1173. {
  1174. struct irq_fwspec fwspec;
  1175. if (irq_domain_get_of_node(domain->parent)) {
  1176. fwspec.fwnode = domain->parent->fwnode;
  1177. fwspec.param_count = 3;
  1178. fwspec.param[0] = GIC_IRQ_TYPE_LPI;
  1179. fwspec.param[1] = hwirq;
  1180. fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
  1181. } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
  1182. fwspec.fwnode = domain->parent->fwnode;
  1183. fwspec.param_count = 2;
  1184. fwspec.param[0] = hwirq;
  1185. fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
  1186. } else {
  1187. return -EINVAL;
  1188. }
  1189. return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  1190. }
  1191. static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  1192. unsigned int nr_irqs, void *args)
  1193. {
  1194. msi_alloc_info_t *info = args;
  1195. struct its_device *its_dev = info->scratchpad[0].ptr;
  1196. irq_hw_number_t hwirq;
  1197. int err;
  1198. int i;
  1199. for (i = 0; i < nr_irqs; i++) {
  1200. err = its_alloc_device_irq(its_dev, &hwirq);
  1201. if (err)
  1202. return err;
  1203. err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
  1204. if (err)
  1205. return err;
  1206. irq_domain_set_hwirq_and_chip(domain, virq + i,
  1207. hwirq, &its_irq_chip, its_dev);
  1208. pr_debug("ID:%d pID:%d vID:%d\n",
  1209. (int)(hwirq - its_dev->event_map.lpi_base),
  1210. (int) hwirq, virq + i);
  1211. }
  1212. return 0;
  1213. }
  1214. static void its_irq_domain_activate(struct irq_domain *domain,
  1215. struct irq_data *d)
  1216. {
  1217. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1218. u32 event = its_get_event_id(d);
  1219. const struct cpumask *cpu_mask = cpu_online_mask;
  1220. /* get the cpu_mask of local node */
  1221. if (its_dev->its->numa_node >= 0)
  1222. cpu_mask = cpumask_of_node(its_dev->its->numa_node);
  1223. /* Bind the LPI to the first possible CPU */
  1224. its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
  1225. /* Map the GIC IRQ and event to the device */
  1226. its_send_mapti(its_dev, d->hwirq, event);
  1227. }
  1228. static void its_irq_domain_deactivate(struct irq_domain *domain,
  1229. struct irq_data *d)
  1230. {
  1231. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1232. u32 event = its_get_event_id(d);
  1233. /* Stop the delivery of interrupts */
  1234. its_send_discard(its_dev, event);
  1235. }
  1236. static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  1237. unsigned int nr_irqs)
  1238. {
  1239. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  1240. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1241. int i;
  1242. for (i = 0; i < nr_irqs; i++) {
  1243. struct irq_data *data = irq_domain_get_irq_data(domain,
  1244. virq + i);
  1245. u32 event = its_get_event_id(data);
  1246. /* Mark interrupt index as unused */
  1247. clear_bit(event, its_dev->event_map.lpi_map);
  1248. /* Nuke the entry in the domain */
  1249. irq_domain_reset_irq_data(data);
  1250. }
  1251. /* If all interrupts have been freed, start mopping the floor */
  1252. if (bitmap_empty(its_dev->event_map.lpi_map,
  1253. its_dev->event_map.nr_lpis)) {
  1254. its_lpi_free(&its_dev->event_map);
  1255. /* Unmap device/itt */
  1256. its_send_mapd(its_dev, 0);
  1257. its_free_device(its_dev);
  1258. }
  1259. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  1260. }
  1261. static const struct irq_domain_ops its_domain_ops = {
  1262. .alloc = its_irq_domain_alloc,
  1263. .free = its_irq_domain_free,
  1264. .activate = its_irq_domain_activate,
  1265. .deactivate = its_irq_domain_deactivate,
  1266. };
  1267. static int its_force_quiescent(void __iomem *base)
  1268. {
  1269. u32 count = 1000000; /* 1s */
  1270. u32 val;
  1271. val = readl_relaxed(base + GITS_CTLR);
  1272. /*
  1273. * GIC architecture specification requires the ITS to be both
  1274. * disabled and quiescent for writes to GITS_BASER<n> or
  1275. * GITS_CBASER to not have UNPREDICTABLE results.
  1276. */
  1277. if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
  1278. return 0;
  1279. /* Disable the generation of all interrupts to this ITS */
  1280. val &= ~GITS_CTLR_ENABLE;
  1281. writel_relaxed(val, base + GITS_CTLR);
  1282. /* Poll GITS_CTLR and wait until ITS becomes quiescent */
  1283. while (1) {
  1284. val = readl_relaxed(base + GITS_CTLR);
  1285. if (val & GITS_CTLR_QUIESCENT)
  1286. return 0;
  1287. count--;
  1288. if (!count)
  1289. return -EBUSY;
  1290. cpu_relax();
  1291. udelay(1);
  1292. }
  1293. }
  1294. static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
  1295. {
  1296. struct its_node *its = data;
  1297. its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
  1298. }
  1299. static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
  1300. {
  1301. struct its_node *its = data;
  1302. its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
  1303. }
  1304. static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
  1305. {
  1306. struct its_node *its = data;
  1307. /* On QDF2400, the size of the ITE is 16Bytes */
  1308. its->ite_size = 16;
  1309. }
  1310. static const struct gic_quirk its_quirks[] = {
  1311. #ifdef CONFIG_CAVIUM_ERRATUM_22375
  1312. {
  1313. .desc = "ITS: Cavium errata 22375, 24313",
  1314. .iidr = 0xa100034c, /* ThunderX pass 1.x */
  1315. .mask = 0xffff0fff,
  1316. .init = its_enable_quirk_cavium_22375,
  1317. },
  1318. #endif
  1319. #ifdef CONFIG_CAVIUM_ERRATUM_23144
  1320. {
  1321. .desc = "ITS: Cavium erratum 23144",
  1322. .iidr = 0xa100034c, /* ThunderX pass 1.x */
  1323. .mask = 0xffff0fff,
  1324. .init = its_enable_quirk_cavium_23144,
  1325. },
  1326. #endif
  1327. #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
  1328. {
  1329. .desc = "ITS: QDF2400 erratum 0065",
  1330. .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
  1331. .mask = 0xffffffff,
  1332. .init = its_enable_quirk_qdf2400_e0065,
  1333. },
  1334. #endif
  1335. {
  1336. }
  1337. };
  1338. static void its_enable_quirks(struct its_node *its)
  1339. {
  1340. u32 iidr = readl_relaxed(its->base + GITS_IIDR);
  1341. gic_enable_quirks(iidr, its_quirks, its);
  1342. }
  1343. static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
  1344. {
  1345. struct irq_domain *inner_domain;
  1346. struct msi_domain_info *info;
  1347. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1348. if (!info)
  1349. return -ENOMEM;
  1350. inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
  1351. if (!inner_domain) {
  1352. kfree(info);
  1353. return -ENOMEM;
  1354. }
  1355. inner_domain->parent = its_parent;
  1356. inner_domain->bus_token = DOMAIN_BUS_NEXUS;
  1357. inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP;
  1358. info->ops = &its_msi_domain_ops;
  1359. info->data = its;
  1360. inner_domain->host_data = info;
  1361. return 0;
  1362. }
  1363. static int __init its_probe_one(struct resource *res,
  1364. struct fwnode_handle *handle, int numa_node)
  1365. {
  1366. struct its_node *its;
  1367. void __iomem *its_base;
  1368. u32 val;
  1369. u64 baser, tmp;
  1370. int err;
  1371. its_base = ioremap(res->start, resource_size(res));
  1372. if (!its_base) {
  1373. pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
  1374. return -ENOMEM;
  1375. }
  1376. val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
  1377. if (val != 0x30 && val != 0x40) {
  1378. pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
  1379. err = -ENODEV;
  1380. goto out_unmap;
  1381. }
  1382. err = its_force_quiescent(its_base);
  1383. if (err) {
  1384. pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
  1385. goto out_unmap;
  1386. }
  1387. pr_info("ITS %pR\n", res);
  1388. its = kzalloc(sizeof(*its), GFP_KERNEL);
  1389. if (!its) {
  1390. err = -ENOMEM;
  1391. goto out_unmap;
  1392. }
  1393. raw_spin_lock_init(&its->lock);
  1394. INIT_LIST_HEAD(&its->entry);
  1395. INIT_LIST_HEAD(&its->its_device_list);
  1396. its->base = its_base;
  1397. its->phys_base = res->start;
  1398. its->ite_size = ((gic_read_typer(its_base + GITS_TYPER) >> 4) & 0xf) + 1;
  1399. its->numa_node = numa_node;
  1400. its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1401. get_order(ITS_CMD_QUEUE_SZ));
  1402. if (!its->cmd_base) {
  1403. err = -ENOMEM;
  1404. goto out_free_its;
  1405. }
  1406. its->cmd_write = its->cmd_base;
  1407. its_enable_quirks(its);
  1408. err = its_alloc_tables(its);
  1409. if (err)
  1410. goto out_free_cmd;
  1411. err = its_alloc_collections(its);
  1412. if (err)
  1413. goto out_free_tables;
  1414. baser = (virt_to_phys(its->cmd_base) |
  1415. GITS_CBASER_RaWaWb |
  1416. GITS_CBASER_InnerShareable |
  1417. (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
  1418. GITS_CBASER_VALID);
  1419. gits_write_cbaser(baser, its->base + GITS_CBASER);
  1420. tmp = gits_read_cbaser(its->base + GITS_CBASER);
  1421. if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
  1422. if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
  1423. /*
  1424. * The HW reports non-shareable, we must
  1425. * remove the cacheability attributes as
  1426. * well.
  1427. */
  1428. baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
  1429. GITS_CBASER_CACHEABILITY_MASK);
  1430. baser |= GITS_CBASER_nC;
  1431. gits_write_cbaser(baser, its->base + GITS_CBASER);
  1432. }
  1433. pr_info("ITS: using cache flushing for cmd queue\n");
  1434. its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
  1435. }
  1436. gits_write_cwriter(0, its->base + GITS_CWRITER);
  1437. writel_relaxed(GITS_CTLR_ENABLE, its->base + GITS_CTLR);
  1438. err = its_init_domain(handle, its);
  1439. if (err)
  1440. goto out_free_tables;
  1441. spin_lock(&its_lock);
  1442. list_add(&its->entry, &its_nodes);
  1443. spin_unlock(&its_lock);
  1444. return 0;
  1445. out_free_tables:
  1446. its_free_tables(its);
  1447. out_free_cmd:
  1448. free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
  1449. out_free_its:
  1450. kfree(its);
  1451. out_unmap:
  1452. iounmap(its_base);
  1453. pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
  1454. return err;
  1455. }
  1456. static bool gic_rdists_supports_plpis(void)
  1457. {
  1458. return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
  1459. }
  1460. int its_cpu_init(void)
  1461. {
  1462. if (!list_empty(&its_nodes)) {
  1463. if (!gic_rdists_supports_plpis()) {
  1464. pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
  1465. return -ENXIO;
  1466. }
  1467. its_cpu_init_lpis();
  1468. its_cpu_init_collection();
  1469. }
  1470. return 0;
  1471. }
  1472. static const struct of_device_id its_device_id[] = {
  1473. { .compatible = "arm,gic-v3-its", },
  1474. {},
  1475. };
  1476. static int __init its_of_probe(struct device_node *node)
  1477. {
  1478. struct device_node *np;
  1479. struct resource res;
  1480. for (np = of_find_matching_node(node, its_device_id); np;
  1481. np = of_find_matching_node(np, its_device_id)) {
  1482. if (!of_property_read_bool(np, "msi-controller")) {
  1483. pr_warn("%s: no msi-controller property, ITS ignored\n",
  1484. np->full_name);
  1485. continue;
  1486. }
  1487. if (of_address_to_resource(np, 0, &res)) {
  1488. pr_warn("%s: no regs?\n", np->full_name);
  1489. continue;
  1490. }
  1491. its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
  1492. }
  1493. return 0;
  1494. }
  1495. #ifdef CONFIG_ACPI
  1496. #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
  1497. #if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531)
  1498. struct its_srat_map {
  1499. /* numa node id */
  1500. u32 numa_node;
  1501. /* GIC ITS ID */
  1502. u32 its_id;
  1503. };
  1504. static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata;
  1505. static int its_in_srat __initdata;
  1506. static int __init acpi_get_its_numa_node(u32 its_id)
  1507. {
  1508. int i;
  1509. for (i = 0; i < its_in_srat; i++) {
  1510. if (its_id == its_srat_maps[i].its_id)
  1511. return its_srat_maps[i].numa_node;
  1512. }
  1513. return NUMA_NO_NODE;
  1514. }
  1515. static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
  1516. const unsigned long end)
  1517. {
  1518. int node;
  1519. struct acpi_srat_gic_its_affinity *its_affinity;
  1520. its_affinity = (struct acpi_srat_gic_its_affinity *)header;
  1521. if (!its_affinity)
  1522. return -EINVAL;
  1523. if (its_affinity->header.length < sizeof(*its_affinity)) {
  1524. pr_err("SRAT: Invalid header length %d in ITS affinity\n",
  1525. its_affinity->header.length);
  1526. return -EINVAL;
  1527. }
  1528. if (its_in_srat >= MAX_NUMNODES) {
  1529. pr_err("SRAT: ITS affinity exceeding max count[%d]\n",
  1530. MAX_NUMNODES);
  1531. return -EINVAL;
  1532. }
  1533. node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
  1534. if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
  1535. pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
  1536. return 0;
  1537. }
  1538. its_srat_maps[its_in_srat].numa_node = node;
  1539. its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
  1540. its_in_srat++;
  1541. pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
  1542. its_affinity->proximity_domain, its_affinity->its_id, node);
  1543. return 0;
  1544. }
  1545. static void __init acpi_table_parse_srat_its(void)
  1546. {
  1547. acpi_table_parse_entries(ACPI_SIG_SRAT,
  1548. sizeof(struct acpi_table_srat),
  1549. ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
  1550. gic_acpi_parse_srat_its, 0);
  1551. }
  1552. #else
  1553. static void __init acpi_table_parse_srat_its(void) { }
  1554. static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
  1555. #endif
  1556. static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
  1557. const unsigned long end)
  1558. {
  1559. struct acpi_madt_generic_translator *its_entry;
  1560. struct fwnode_handle *dom_handle;
  1561. struct resource res;
  1562. int err;
  1563. its_entry = (struct acpi_madt_generic_translator *)header;
  1564. memset(&res, 0, sizeof(res));
  1565. res.start = its_entry->base_address;
  1566. res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
  1567. res.flags = IORESOURCE_MEM;
  1568. dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
  1569. if (!dom_handle) {
  1570. pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
  1571. &res.start);
  1572. return -ENOMEM;
  1573. }
  1574. err = iort_register_domain_token(its_entry->translation_id, dom_handle);
  1575. if (err) {
  1576. pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
  1577. &res.start, its_entry->translation_id);
  1578. goto dom_err;
  1579. }
  1580. err = its_probe_one(&res, dom_handle,
  1581. acpi_get_its_numa_node(its_entry->translation_id));
  1582. if (!err)
  1583. return 0;
  1584. iort_deregister_domain_token(its_entry->translation_id);
  1585. dom_err:
  1586. irq_domain_free_fwnode(dom_handle);
  1587. return err;
  1588. }
  1589. static void __init its_acpi_probe(void)
  1590. {
  1591. acpi_table_parse_srat_its();
  1592. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
  1593. gic_acpi_parse_madt_its, 0);
  1594. }
  1595. #else
  1596. static void __init its_acpi_probe(void) { }
  1597. #endif
  1598. int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
  1599. struct irq_domain *parent_domain)
  1600. {
  1601. struct device_node *of_node;
  1602. its_parent = parent_domain;
  1603. of_node = to_of_node(handle);
  1604. if (of_node)
  1605. its_of_probe(of_node);
  1606. else
  1607. its_acpi_probe();
  1608. if (list_empty(&its_nodes)) {
  1609. pr_warn("ITS: No ITS available, not enabling LPIs\n");
  1610. return -ENXIO;
  1611. }
  1612. gic_rdists = rdists;
  1613. return its_alloc_lpi_tables();
  1614. }