pic-gicv2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2013-07-20 Bernard first version
  9. * 2014-04-03 Grissiom many enhancements
  10. * 2018-11-22 Jesven add rt_hw_ipi_send()
  11. * add rt_hw_ipi_handler_install()
  12. * 2022-08-24 GuEe-GUI add pic support
  13. * 2022-11-07 GuEe-GUI add v2m support
  14. */
  15. #include <rthw.h>
  16. #include <rtthread.h>
  17. #include <rtdevice.h>
  18. #define DBG_TAG "pic.gicv2"
  19. #define DBG_LVL DBG_INFO
  20. #include <rtdbg.h>
  21. #include <cpuport.h>
  22. #include <ioremap.h>
  23. #include "pic-gicv2.h"
  24. #include "pic-gic-common.h"
  25. #define GIC_CPU_IMAX 8
  26. #define raw_to_gicv2(raw) rt_container_of(raw, struct gicv2, parent)
  27. static rt_bool_t needs_rmw_access = RT_FALSE;
  28. static int _gicv2_nr = 0, _init_cpu_id = 0;
  29. static struct gicv2 _gicv2_list[RT_PIC_ARM_GIC_MAX_NR] = {};
  30. static rt_bool_t _gicv2_eoi_mode_ns = RT_FALSE;
  31. static rt_uint8_t _gicv2_cpumask_map[GIC_CPU_IMAX] =
  32. {
  33. [0 ... GIC_CPU_IMAX - 1] = 0xff,
  34. };
  35. static rt_uint8_t gicv2_cpumask_map(struct gicv2 *gic)
  36. {
  37. rt_uint32_t mask, i;
  38. for (i = mask = 0; i < 32; i += 4)
  39. {
  40. mask = HWREG32(gic->dist_base + GIC_DIST_TARGET + i);
  41. mask |= mask >> 16;
  42. mask |= mask >> 8;
  43. if (mask)
  44. {
  45. break;
  46. }
  47. }
  48. return mask;
  49. }
  50. static void gicv2_dist_init(struct gicv2 *gic)
  51. {
  52. void *base = gic->dist_base;
  53. rt_uint32_t i;
  54. rt_uint32_t cpumask = gicv2_cpumask_map(gic);
  55. gic->max_irq = HWREG32(base + GIC_DIST_TYPE) & 0x1f;
  56. gic->max_irq = (gic->max_irq + 1) * 32;
  57. /*
  58. * The GIC only supports up to 1020 interrupt sources.
  59. * Limit this to either the architected maximum, or the
  60. * platform maximum.
  61. */
  62. if (gic->max_irq > 1020)
  63. {
  64. gic->max_irq = 1020;
  65. }
  66. LOG_D("Max irq = %d", gic->max_irq);
  67. HWREG32(base + GIC_DIST_CTRL) = GICD_DISABLE;
  68. /* Set all global (unused) interrupts to this CPU only. */
  69. cpumask |= cpumask << 8;
  70. cpumask |= cpumask << 16;
  71. for (i = 32; i < gic->max_irq; i += 4)
  72. {
  73. HWREG32(base + GIC_DIST_TARGET + i * 4 / 4) = cpumask;
  74. }
  75. gic_common_dist_config(base, gic->max_irq, RT_NULL, RT_NULL);
  76. HWREG32(base + GIC_DIST_CTRL) = GICD_ENABLE;
  77. }
  78. static void gicv2_cpu_init(struct gicv2 *gic)
  79. {
  80. rt_uint32_t cpumask;
  81. void *base = gic->cpu_base;
  82. rt_uint32_t config = GICC_ENABLE;
  83. int cpu_id = _init_cpu_id = rt_hw_cpu_id();
  84. cpumask = gicv2_cpumask_map(gic);
  85. _gicv2_cpumask_map[cpu_id] = cpumask;
  86. /*
  87. * Clear our mask from the other map entries in case they're
  88. * still undefined.
  89. */
  90. for (int i = 0; i < RT_ARRAY_SIZE(_gicv2_cpumask_map); ++i)
  91. {
  92. if (i != cpu_id)
  93. {
  94. _gicv2_cpumask_map[i] &= ~cpumask;
  95. }
  96. }
  97. gic_common_cpu_config(gic->dist_base, 32, RT_NULL, RT_NULL);
  98. HWREG32(base + GIC_CPU_PRIMASK) = GICC_INT_PRI_THRESHOLD;
  99. HWREG32(base + GIC_CPU_BINPOINT) = 0x7;
  100. #ifdef ARCH_SUPPORT_HYP
  101. _gicv2_eoi_mode_ns = RT_TRUE;
  102. #endif
  103. if (_gicv2_eoi_mode_ns)
  104. {
  105. config |= GIC_CPU_CTRL_EOI_MODE_NS;
  106. }
  107. HWREG32(base + GIC_CPU_CTRL) = config;
  108. }
  109. static rt_err_t gicv2_irq_init(struct rt_pic *pic)
  110. {
  111. gicv2_cpu_init(rt_container_of(pic, struct gicv2, parent));
  112. return RT_EOK;
  113. }
  114. static void gicv2_irq_ack(struct rt_pic_irq *pirq)
  115. {
  116. int hwirq = pirq->hwirq;
  117. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  118. if (!_gicv2_eoi_mode_ns)
  119. {
  120. HWREG32(gic->dist_base + GIC_DIST_PENDING_CLEAR + hwirq / 32 * 4) = 1U << (hwirq % 32);
  121. }
  122. HWREG32(gic->cpu_base + GIC_CPU_EOI) = hwirq;
  123. }
  124. static void gicv2_irq_mask(struct rt_pic_irq *pirq)
  125. {
  126. int hwirq = pirq->hwirq;
  127. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  128. HWREG32(gic->dist_base + GIC_DIST_ENABLE_CLEAR + hwirq / 32 * 4) = 1U << (hwirq % 32);
  129. }
  130. static void gicv2_irq_unmask(struct rt_pic_irq *pirq)
  131. {
  132. int hwirq = pirq->hwirq;
  133. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  134. HWREG32(gic->dist_base + GIC_DIST_ENABLE_SET + hwirq / 32 * 4) = 1U << (hwirq % 32);
  135. }
  136. static void gicv2_irq_eoi(struct rt_pic_irq *pirq)
  137. {
  138. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  139. if (_gicv2_eoi_mode_ns)
  140. {
  141. HWREG32(gic->cpu_base + GIC_CPU_DIR) = pirq->hwirq;
  142. }
  143. }
  144. static rt_err_t gicv2_irq_set_priority(struct rt_pic_irq *pirq, rt_uint32_t priority)
  145. {
  146. rt_uint32_t mask;
  147. int hwirq = pirq->hwirq;
  148. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  149. mask = HWREG32(gic->dist_base + GIC_DIST_PRI + hwirq / 4 * 4);
  150. mask &= ~(0xffU << ((hwirq % 4) * 8));
  151. mask |= ((priority & 0xffU) << ((hwirq % 4) * 8));
  152. HWREG32(gic->dist_base + GIC_DIST_PRI + hwirq / 4 * 4) = mask;
  153. return RT_EOK;
  154. }
  155. static rt_err_t gicv2_irq_set_affinity(struct rt_pic_irq *pirq, rt_bitmap_t *affinity)
  156. {
  157. int hwirq = pirq->hwirq;
  158. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  159. rt_uint32_t target_list = ((rt_uint8_t *)affinity)[gic - &_gicv2_list[0]];
  160. rt_uint8_t valb = _gicv2_cpumask_map[__rt_ffs(target_list) - 1];
  161. void *io_addr = gic->dist_base + GIC_DIST_TARGET + hwirq;
  162. if (needs_rmw_access)
  163. {
  164. /* RMW write byte */
  165. rt_uint32_t val;
  166. rt_ubase_t level;
  167. rt_ubase_t offset = (rt_ubase_t)io_addr & 3UL, shift = offset * 8;
  168. static struct rt_spinlock rmw_lock = {};
  169. level = rt_spin_lock_irqsave(&rmw_lock);
  170. io_addr -= offset;
  171. val = HWREG32(io_addr);
  172. val &= ~RT_GENMASK(shift + 7, shift);
  173. val |= valb << shift;
  174. HWREG32(io_addr) = val;
  175. rt_spin_unlock_irqrestore(&rmw_lock, level);
  176. }
  177. else
  178. {
  179. HWREG8(io_addr) = valb;
  180. }
  181. return RT_EOK;
  182. }
  183. static rt_err_t gicv2_irq_set_triger_mode(struct rt_pic_irq *pirq, rt_uint32_t mode)
  184. {
  185. rt_err_t err = RT_EOK;
  186. int hwirq = pirq->hwirq;
  187. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  188. if (hwirq >= GIC_SGI_NR)
  189. {
  190. err = gic_common_configure_irq(gic->dist_base + GIC_DIST_CONFIG, pirq->hwirq, mode, RT_NULL, RT_NULL);
  191. }
  192. else
  193. {
  194. err = -RT_ENOSYS;
  195. }
  196. return err;
  197. }
  198. static void gicv2_irq_send_ipi(struct rt_pic_irq *pirq, rt_bitmap_t *cpumask)
  199. {
  200. struct gicv2 *gic;
  201. int sgi = pirq->hwirq;
  202. rt_uint8_t *target_list = (rt_uint8_t *)cpumask;
  203. for (int i = 0; i < _gicv2_nr; ++i)
  204. {
  205. if (*target_list)
  206. {
  207. gic = &_gicv2_list[i];
  208. HWREG32(gic->dist_base + GIC_DIST_SOFTINT) = ((*target_list & 0xffU) << 16) | (sgi & 0xf);
  209. rt_hw_dsb();
  210. }
  211. ++target_list;
  212. }
  213. }
  214. static int gicv2_irq_map(struct rt_pic *pic, int hwirq, rt_uint32_t mode)
  215. {
  216. int irq, irq_index = hwirq - GIC_SGI_NR;
  217. struct rt_pic_irq *pirq = rt_pic_find_irq(pic, irq_index);
  218. if (pirq && hwirq >= GIC_SGI_NR)
  219. {
  220. pirq->mode = mode;
  221. pirq->priority = GICD_INT_DEF_PRI;
  222. rt_bitmap_set_bit(pirq->affinity, _init_cpu_id);
  223. irq = rt_pic_config_irq(pic, irq_index, hwirq);
  224. if (irq >= 0 && mode != RT_IRQ_MODE_LEVEL_HIGH)
  225. {
  226. gicv2_irq_set_triger_mode(pirq, mode);
  227. }
  228. }
  229. else
  230. {
  231. irq = -1;
  232. }
  233. return irq;
  234. }
  235. static rt_err_t gicv2_irq_parse(struct rt_pic *pic, struct rt_ofw_cell_args *args, struct rt_pic_irq *out_pirq)
  236. {
  237. rt_err_t err = RT_EOK;
  238. if (args->args_count == 3)
  239. {
  240. out_pirq->mode = args->args[2] & RT_IRQ_MODE_MASK;
  241. switch (args->args[0])
  242. {
  243. case 0:
  244. /* SPI */
  245. out_pirq->hwirq = args->args[1] + 32;
  246. break;
  247. case 1:
  248. /* PPI */
  249. out_pirq->hwirq = args->args[1] + 16;
  250. break;
  251. default:
  252. err = -RT_ENOSYS;
  253. break;
  254. }
  255. }
  256. else
  257. {
  258. err = -RT_EINVAL;
  259. }
  260. return err;
  261. }
  262. static struct rt_pic_ops gicv2_ops =
  263. {
  264. .name = "GICv2",
  265. .irq_init = gicv2_irq_init,
  266. .irq_ack = gicv2_irq_ack,
  267. .irq_mask = gicv2_irq_mask,
  268. .irq_unmask = gicv2_irq_unmask,
  269. .irq_eoi = gicv2_irq_eoi,
  270. .irq_set_priority = gicv2_irq_set_priority,
  271. .irq_set_affinity = gicv2_irq_set_affinity,
  272. .irq_set_triger_mode = gicv2_irq_set_triger_mode,
  273. .irq_send_ipi = gicv2_irq_send_ipi,
  274. .irq_map = gicv2_irq_map,
  275. .irq_parse = gicv2_irq_parse,
  276. };
  277. static rt_bool_t gicv2_handler(void *data)
  278. {
  279. rt_bool_t res = RT_FALSE;
  280. int hwirq;
  281. struct gicv2 *gic = data;
  282. hwirq = HWREG32(gic->cpu_base + GIC_CPU_INTACK) & 0x3ffUL;
  283. if (!(hwirq >= 1020 && hwirq <= 1023))
  284. {
  285. struct rt_pic_irq *pirq;
  286. if (hwirq < GIC_SGI_NR)
  287. {
  288. rt_hw_rmb();
  289. pirq = rt_pic_find_ipi(&gic->parent, hwirq);
  290. }
  291. else
  292. {
  293. pirq = rt_pic_find_irq(&gic->parent, hwirq - GIC_SGI_NR);
  294. }
  295. gicv2_irq_ack(pirq);
  296. rt_pic_handle_isr(pirq);
  297. gicv2_irq_eoi(pirq);
  298. res = RT_TRUE;
  299. }
  300. return res;
  301. }
  302. static rt_err_t gicv2_enable_rmw_access(void *data)
  303. {
  304. if (rt_ofw_machine_is_compatible("renesas,emev2"))
  305. {
  306. needs_rmw_access = RT_TRUE;
  307. return RT_EOK;
  308. }
  309. return -RT_EINVAL;
  310. }
  311. static const struct gic_quirk _gicv2_quirks[] =
  312. {
  313. {
  314. .desc = "GICv2: Broken byte access",
  315. .compatible = "arm,pl390",
  316. .init = gicv2_enable_rmw_access,
  317. },
  318. { /* sentinel */ }
  319. };
  320. static rt_err_t gicv2_iomap_init(struct gicv2 *gic, rt_uint64_t *regs)
  321. {
  322. rt_err_t err = RT_EOK;
  323. int idx;
  324. const char *name[] =
  325. {
  326. "Distributor",
  327. "CPU interfaces",
  328. "Virtual interface control",
  329. "Virtual CPU interface",
  330. };
  331. do {
  332. /* GICD->GICC->GICH->GICV */
  333. gic->dist_size = regs[1];
  334. gic->dist_base = rt_ioremap((void *)regs[0], gic->dist_size);
  335. if (!gic->dist_base)
  336. {
  337. idx = 0;
  338. err = -RT_ERROR;
  339. break;
  340. }
  341. gic->cpu_size = regs[3];
  342. gic->cpu_base = rt_ioremap((void *)regs[2], gic->cpu_size);
  343. if (!gic->cpu_base)
  344. {
  345. idx = 1;
  346. err = -RT_ERROR;
  347. break;
  348. }
  349. /* ArchRev[4:7] */
  350. gic->version = HWREG32(gic->dist_base + GIC_DIST_ICPIDR2) >> 4;
  351. #ifdef ARCH_SUPPORT_HYP
  352. if (gic->version == 1)
  353. {
  354. break;
  355. }
  356. gic->hyp_size = regs[5];
  357. gic->hyp_base = rt_ioremap((void *)regs[4], gic->hyp_size);
  358. if (!gic->hyp_base)
  359. {
  360. idx = 2;
  361. err = -RT_ERROR;
  362. break;
  363. }
  364. gic->vcpu_size = regs[7];
  365. gic->vcpu_base = rt_ioremap((void *)regs[6], gic->vcpu_size);
  366. if (!gic->vcpu_base)
  367. {
  368. idx = 3;
  369. err = -RT_ERROR;
  370. break;
  371. }
  372. #endif /* ARCH_SUPPORT_HYP */
  373. } while (0);
  374. if (err)
  375. {
  376. RT_UNUSED(idx);
  377. RT_UNUSED(name);
  378. LOG_E("gic[%d] %s IO[%p, %p] map fail", _gicv2_nr, name[idx], regs[idx * 2], regs[idx * 2 + 1]);
  379. }
  380. return err;
  381. }
  382. static void gicv2_init(struct gicv2 *gic)
  383. {
  384. gicv2_dist_init(gic);
  385. gic->parent.priv_data = gic;
  386. gic->parent.ops = &gicv2_ops;
  387. rt_pic_linear_irq(&gic->parent, gic->max_irq + 1 - GIC_SGI_NR);
  388. gic_common_sgi_config(gic->dist_base, &gic->parent, _gicv2_nr * GIC_SGI_NR);
  389. rt_pic_add_traps(gicv2_handler, gic);
  390. rt_pic_user_extends(&gic->parent);
  391. }
  392. static void gicv2_init_fail(struct gicv2 *gic)
  393. {
  394. if (gic->dist_base)
  395. {
  396. rt_iounmap(gic->dist_base);
  397. }
  398. if (gic->cpu_base)
  399. {
  400. rt_iounmap(gic->cpu_base);
  401. }
  402. if (gic->hyp_base)
  403. {
  404. rt_iounmap(gic->hyp_base);
  405. }
  406. if (gic->vcpu_base)
  407. {
  408. rt_iounmap(gic->vcpu_base);
  409. }
  410. rt_memset(gic, 0, sizeof(*gic));
  411. }
  412. static rt_err_t gicv2_ofw_init(struct rt_ofw_node *np, const struct rt_ofw_node_id *id)
  413. {
  414. rt_err_t err = RT_EOK;
  415. struct gicv2 *gic = RT_NULL;
  416. do {
  417. rt_uint64_t regs[8];
  418. if (_gicv2_nr >= RT_PIC_ARM_GIC_MAX_NR)
  419. {
  420. LOG_W("GICv2/v1 table is full");
  421. err = -RT_EFULL;
  422. break;
  423. }
  424. gic = &_gicv2_list[_gicv2_nr];
  425. rt_ofw_get_address_array(np, RT_ARRAY_SIZE(regs), regs);
  426. if ((err = gicv2_iomap_init(gic, regs)))
  427. {
  428. break;
  429. }
  430. if (gic->version != 1 && gic->version != 2)
  431. {
  432. LOG_E("Version = %d is not support", gic->version);
  433. err = -RT_EINVAL;
  434. break;
  435. }
  436. gic_common_init_quirk_ofw(np, _gicv2_quirks, gic);
  437. gicv2_init(gic);
  438. rt_ofw_data(np) = &gic->parent;
  439. if (gic->version == 2)
  440. {
  441. #ifdef RT_PIC_ARM_GIC_V2M
  442. gicv2m_ofw_probe(np, id);
  443. #endif
  444. }
  445. ++_gicv2_nr;
  446. } while (0);
  447. if (err && gic)
  448. {
  449. gicv2_init_fail(gic);
  450. }
  451. return err;
  452. }
  453. static const struct rt_ofw_node_id gicv2_ofw_ids[] =
  454. {
  455. { .compatible = "arm,gic-400" },
  456. { .compatible = "arm,arm11mp-gic" },
  457. { .compatible = "arm,arm1176jzf-devchip-gic" },
  458. { .compatible = "arm,cortex-a15-gic" },
  459. { .compatible = "arm,cortex-a9-gic" },
  460. { .compatible = "arm,cortex-a7-gic" },
  461. { .compatible = "qcom,msm-8660-qgic" },
  462. { .compatible = "qcom,msm-qgic2" },
  463. { .compatible = "arm,pl390" },
  464. { /* sentinel */ }
  465. };
  466. RT_PIC_OFW_DECLARE(gicv2, gicv2_ofw_ids, gicv2_ofw_init);