irq-gic-v3-its.c 40 KB

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