pic-gicv2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. _init_cpu_id = rt_hw_cpu_id();
  56. gic->max_irq = HWREG32(base + GIC_DIST_TYPE) & 0x1f;
  57. gic->max_irq = (gic->max_irq + 1) * 32;
  58. /*
  59. * The GIC only supports up to 1020 interrupt sources.
  60. * Limit this to either the architected maximum, or the
  61. * platform maximum.
  62. */
  63. if (gic->max_irq > 1020)
  64. {
  65. gic->max_irq = 1020;
  66. }
  67. LOG_D("Max irq = %d", gic->max_irq);
  68. HWREG32(base + GIC_DIST_CTRL) = GICD_DISABLE;
  69. /* Set all global (unused) interrupts to this CPU only. */
  70. cpumask |= cpumask << 8;
  71. cpumask |= cpumask << 16;
  72. for (i = 32; i < gic->max_irq; i += 4)
  73. {
  74. HWREG32(base + GIC_DIST_TARGET + i * 4 / 4) = cpumask;
  75. }
  76. gic_common_dist_config(base, gic->max_irq, RT_NULL, RT_NULL);
  77. HWREG32(base + GIC_DIST_CTRL) = GICD_ENABLE;
  78. }
  79. static void gicv2_cpu_init(struct gicv2 *gic)
  80. {
  81. rt_uint32_t cpumask;
  82. void *base = gic->cpu_base;
  83. rt_uint32_t config = GICC_ENABLE;
  84. int cpu_id = rt_hw_cpu_id();
  85. cpumask = gicv2_cpumask_map(gic);
  86. _gicv2_cpumask_map[cpu_id] = cpumask;
  87. /*
  88. * Clear our mask from the other map entries in case they're
  89. * still undefined.
  90. */
  91. for (int i = 0; i < RT_ARRAY_SIZE(_gicv2_cpumask_map); ++i)
  92. {
  93. if (i != cpu_id)
  94. {
  95. _gicv2_cpumask_map[i] &= ~cpumask;
  96. }
  97. }
  98. gic_common_cpu_config(gic->dist_base, 32, RT_NULL, RT_NULL);
  99. HWREG32(base + GIC_CPU_PRIMASK) = GICC_INT_PRI_THRESHOLD;
  100. HWREG32(base + GIC_CPU_BINPOINT) = 0x7;
  101. #ifdef ARCH_SUPPORT_HYP
  102. _gicv2_eoi_mode_ns = RT_TRUE;
  103. #endif
  104. if (_gicv2_eoi_mode_ns)
  105. {
  106. config |= GIC_CPU_CTRL_EOI_MODE_NS;
  107. }
  108. HWREG32(base + GIC_CPU_CTRL) = config;
  109. }
  110. static rt_err_t gicv2_irq_init(struct rt_pic *pic)
  111. {
  112. gicv2_cpu_init(rt_container_of(pic, struct gicv2, parent));
  113. return RT_EOK;
  114. }
  115. static void gicv2_irq_ack(struct rt_pic_irq *pirq)
  116. {
  117. int hwirq = pirq->hwirq;
  118. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  119. if (!_gicv2_eoi_mode_ns)
  120. {
  121. HWREG32(gic->dist_base + GIC_DIST_PENDING_CLEAR + hwirq / 32 * 4) = 1U << (hwirq % 32);
  122. }
  123. HWREG32(gic->cpu_base + GIC_CPU_EOI) = hwirq;
  124. }
  125. static void gicv2_irq_mask(struct rt_pic_irq *pirq)
  126. {
  127. int hwirq = pirq->hwirq;
  128. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  129. HWREG32(gic->dist_base + GIC_DIST_ENABLE_CLEAR + hwirq / 32 * 4) = 1U << (hwirq % 32);
  130. }
  131. static void gicv2_irq_unmask(struct rt_pic_irq *pirq)
  132. {
  133. int hwirq = pirq->hwirq;
  134. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  135. HWREG32(gic->dist_base + GIC_DIST_ENABLE_SET + hwirq / 32 * 4) = 1U << (hwirq % 32);
  136. }
  137. static void gicv2_irq_eoi(struct rt_pic_irq *pirq)
  138. {
  139. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  140. if (_gicv2_eoi_mode_ns)
  141. {
  142. HWREG32(gic->cpu_base + GIC_CPU_DIR) = pirq->hwirq;
  143. }
  144. }
  145. static rt_err_t gicv2_irq_set_priority(struct rt_pic_irq *pirq, rt_uint32_t priority)
  146. {
  147. rt_uint32_t mask;
  148. int hwirq = pirq->hwirq;
  149. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  150. mask = HWREG32(gic->dist_base + GIC_DIST_PRI + hwirq / 4 * 4);
  151. mask &= ~(0xffU << ((hwirq % 4) * 8));
  152. mask |= ((priority & 0xffU) << ((hwirq % 4) * 8));
  153. HWREG32(gic->dist_base + GIC_DIST_PRI + hwirq / 4 * 4) = mask;
  154. return RT_EOK;
  155. }
  156. static rt_err_t gicv2_irq_set_affinity(struct rt_pic_irq *pirq, rt_bitmap_t *affinity)
  157. {
  158. int hwirq = pirq->hwirq;
  159. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  160. rt_uint32_t target_list = ((rt_uint8_t *)affinity)[gic - &_gicv2_list[0]];
  161. rt_uint8_t valb = _gicv2_cpumask_map[__rt_ffs(target_list) - 1];
  162. void *io_addr = gic->dist_base + GIC_DIST_TARGET + hwirq;
  163. if (valb == 0xfe)
  164. {
  165. return -RT_EIO;
  166. }
  167. if (needs_rmw_access)
  168. {
  169. /* RMW write byte */
  170. rt_uint32_t val;
  171. rt_ubase_t level;
  172. rt_ubase_t offset = (rt_ubase_t)io_addr & 3UL, shift = offset * 8;
  173. static struct rt_spinlock rmw_lock = {};
  174. level = rt_spin_lock_irqsave(&rmw_lock);
  175. io_addr -= offset;
  176. val = HWREG32(io_addr);
  177. val &= ~RT_GENMASK(shift + 7, shift);
  178. val |= valb << shift;
  179. HWREG32(io_addr) = val;
  180. rt_spin_unlock_irqrestore(&rmw_lock, level);
  181. }
  182. else
  183. {
  184. HWREG8(io_addr) = valb;
  185. }
  186. return RT_EOK;
  187. }
  188. static rt_err_t gicv2_irq_set_triger_mode(struct rt_pic_irq *pirq, rt_uint32_t mode)
  189. {
  190. rt_err_t err = RT_EOK;
  191. int hwirq = pirq->hwirq;
  192. struct gicv2 *gic = raw_to_gicv2(pirq->pic);
  193. if (hwirq >= GIC_SGI_NR)
  194. {
  195. err = gic_common_configure_irq(gic->dist_base + GIC_DIST_CONFIG, pirq->hwirq, mode, RT_NULL, RT_NULL);
  196. }
  197. else
  198. {
  199. err = -RT_ENOSYS;
  200. }
  201. return err;
  202. }
  203. static void gicv2_irq_send_ipi(struct rt_pic_irq *pirq, rt_bitmap_t *cpumask)
  204. {
  205. struct gicv2 *gic;
  206. int sgi = pirq->hwirq;
  207. rt_uint8_t *target_list = (rt_uint8_t *)cpumask;
  208. for (int i = 0; i < _gicv2_nr; ++i)
  209. {
  210. if (*target_list)
  211. {
  212. gic = &_gicv2_list[i];
  213. HWREG32(gic->dist_base + GIC_DIST_SOFTINT) = ((*target_list & 0xffU) << 16) | (sgi & 0xf);
  214. rt_hw_dsb();
  215. }
  216. ++target_list;
  217. }
  218. }
  219. static rt_err_t gicv2_irq_set_state(struct rt_pic *pic, int hwirq, int type, rt_bool_t state)
  220. {
  221. rt_err_t err = RT_EOK;
  222. rt_uint32_t offset = 0;
  223. struct gicv2 *gic = raw_to_gicv2(pic);
  224. switch (type)
  225. {
  226. case RT_IRQ_STATE_PENDING:
  227. offset = state ? GIC_DIST_PENDING_SET : GIC_DIST_PENDING_CLEAR;
  228. break;
  229. case RT_IRQ_STATE_ACTIVE:
  230. offset = state ? GIC_DIST_ACTIVE_SET : GIC_DIST_ACTIVE_CLEAR;
  231. break;
  232. case RT_IRQ_STATE_MASKED:
  233. offset = state ? GIC_DIST_ENABLE_CLEAR : GIC_DIST_ENABLE_SET;
  234. break;
  235. default:
  236. err = -RT_EINVAL;
  237. break;
  238. }
  239. if (!err)
  240. {
  241. rt_uint32_t mask = 1 << (hwirq % 32);
  242. HWREG32(gic->dist_base + offset + (hwirq / 32) * 4) = mask;
  243. }
  244. return err;
  245. }
  246. static rt_err_t gicv2_irq_get_state(struct rt_pic *pic, int hwirq, int type, rt_bool_t *out_state)
  247. {
  248. rt_err_t err = RT_EOK;
  249. rt_uint32_t offset = 0;
  250. struct gicv2 *gic = raw_to_gicv2(pic);
  251. switch (type)
  252. {
  253. case RT_IRQ_STATE_PENDING:
  254. offset = GIC_DIST_PENDING_SET;
  255. break;
  256. case RT_IRQ_STATE_ACTIVE:
  257. offset = GIC_DIST_ACTIVE_SET;
  258. break;
  259. case RT_IRQ_STATE_MASKED:
  260. offset = GIC_DIST_ENABLE_SET;
  261. break;
  262. default:
  263. err = -RT_EINVAL;
  264. break;
  265. }
  266. if (!err)
  267. {
  268. rt_uint32_t mask = 1 << (hwirq % 32);
  269. *out_state = !!(HWREG32(gic->dist_base + offset + (hwirq / 32) * 4) & mask);
  270. }
  271. return err;
  272. }
  273. static int gicv2_irq_map(struct rt_pic *pic, int hwirq, rt_uint32_t mode)
  274. {
  275. int irq, irq_index = hwirq - GIC_SGI_NR;
  276. struct rt_pic_irq *pirq = rt_pic_find_irq(pic, irq_index);
  277. if (pirq && hwirq >= GIC_SGI_NR)
  278. {
  279. pirq->mode = mode;
  280. pirq->priority = GICD_INT_DEF_PRI;
  281. if (hwirq < 32)
  282. {
  283. gic_fill_ppi_affinity(pirq->affinity);
  284. }
  285. else
  286. {
  287. RT_IRQ_AFFINITY_SET(pirq->affinity, _init_cpu_id);
  288. }
  289. irq = rt_pic_config_irq(pic, irq_index, hwirq);
  290. if (irq >= 0 && mode != RT_IRQ_MODE_LEVEL_HIGH)
  291. {
  292. gicv2_irq_set_triger_mode(pirq, mode);
  293. }
  294. }
  295. else
  296. {
  297. irq = -1;
  298. }
  299. return irq;
  300. }
  301. static rt_err_t gicv2_irq_parse(struct rt_pic *pic, struct rt_ofw_cell_args *args, struct rt_pic_irq *out_pirq)
  302. {
  303. rt_err_t err = RT_EOK;
  304. if (args->args_count == 3)
  305. {
  306. out_pirq->mode = args->args[2] & RT_IRQ_MODE_MASK;
  307. switch (args->args[0])
  308. {
  309. case 0:
  310. /* SPI */
  311. out_pirq->hwirq = args->args[1] + 32;
  312. break;
  313. case 1:
  314. /* PPI */
  315. out_pirq->hwirq = args->args[1] + 16;
  316. break;
  317. default:
  318. err = -RT_ENOSYS;
  319. break;
  320. }
  321. }
  322. else
  323. {
  324. err = -RT_EINVAL;
  325. }
  326. return err;
  327. }
  328. const static struct rt_pic_ops gicv2_ops =
  329. {
  330. .name = "GICv2",
  331. .irq_init = gicv2_irq_init,
  332. .irq_ack = gicv2_irq_ack,
  333. .irq_mask = gicv2_irq_mask,
  334. .irq_unmask = gicv2_irq_unmask,
  335. .irq_eoi = gicv2_irq_eoi,
  336. .irq_set_priority = gicv2_irq_set_priority,
  337. .irq_set_affinity = gicv2_irq_set_affinity,
  338. .irq_set_triger_mode = gicv2_irq_set_triger_mode,
  339. .irq_send_ipi = gicv2_irq_send_ipi,
  340. .irq_set_state = gicv2_irq_set_state,
  341. .irq_get_state = gicv2_irq_get_state,
  342. .irq_map = gicv2_irq_map,
  343. .irq_parse = gicv2_irq_parse,
  344. };
  345. static rt_bool_t gicv2_handler(void *data)
  346. {
  347. rt_bool_t res = RT_FALSE;
  348. int hwirq;
  349. struct gicv2 *gic = data;
  350. hwirq = HWREG32(gic->cpu_base + GIC_CPU_INTACK) & 0x3ffUL;
  351. if (!(hwirq >= 1020 && hwirq <= 1023))
  352. {
  353. struct rt_pic_irq *pirq;
  354. if (hwirq < GIC_SGI_NR)
  355. {
  356. rt_hw_rmb();
  357. pirq = rt_pic_find_ipi(&gic->parent, hwirq);
  358. }
  359. else
  360. {
  361. pirq = rt_pic_find_irq(&gic->parent, hwirq - GIC_SGI_NR);
  362. }
  363. gicv2_irq_ack(pirq);
  364. rt_pic_handle_isr(pirq);
  365. gicv2_irq_eoi(pirq);
  366. res = RT_TRUE;
  367. }
  368. return res;
  369. }
  370. static rt_err_t gicv2_enable_rmw_access(void *data)
  371. {
  372. if (rt_ofw_machine_is_compatible("renesas,emev2"))
  373. {
  374. needs_rmw_access = RT_TRUE;
  375. return RT_EOK;
  376. }
  377. return -RT_EINVAL;
  378. }
  379. static const struct gic_quirk _gicv2_quirks[] =
  380. {
  381. {
  382. .desc = "GICv2: Broken byte access",
  383. .compatible = "arm,pl390",
  384. .init = gicv2_enable_rmw_access,
  385. },
  386. { /* sentinel */ }
  387. };
  388. static rt_err_t gicv2_iomap_init(struct gicv2 *gic, rt_uint64_t *regs)
  389. {
  390. rt_err_t err = RT_EOK;
  391. int idx;
  392. const char *name[] =
  393. {
  394. "Distributor",
  395. "CPU interfaces",
  396. "Virtual interface control",
  397. "Virtual CPU interface",
  398. };
  399. do {
  400. /* GICD->GICC->GICH->GICV */
  401. gic->dist_size = regs[1];
  402. gic->dist_base = rt_ioremap((void *)regs[0], gic->dist_size);
  403. if (!gic->dist_base)
  404. {
  405. idx = 0;
  406. err = -RT_ERROR;
  407. break;
  408. }
  409. gic->cpu_size = regs[3];
  410. gic->cpu_base = rt_ioremap((void *)regs[2], gic->cpu_size);
  411. if (!gic->cpu_base)
  412. {
  413. idx = 1;
  414. err = -RT_ERROR;
  415. break;
  416. }
  417. /* ArchRev[4:7] */
  418. gic->version = HWREG32(gic->dist_base + GIC_DIST_ICPIDR2) >> 4;
  419. #ifdef ARCH_SUPPORT_HYP
  420. if (gic->version == 1)
  421. {
  422. break;
  423. }
  424. gic->hyp_size = regs[5];
  425. gic->hyp_base = rt_ioremap((void *)regs[4], gic->hyp_size);
  426. if (!gic->hyp_base)
  427. {
  428. idx = 2;
  429. err = -RT_ERROR;
  430. break;
  431. }
  432. gic->vcpu_size = regs[7];
  433. gic->vcpu_base = rt_ioremap((void *)regs[6], gic->vcpu_size);
  434. if (!gic->vcpu_base)
  435. {
  436. idx = 3;
  437. err = -RT_ERROR;
  438. break;
  439. }
  440. #endif /* ARCH_SUPPORT_HYP */
  441. } while (0);
  442. if (err)
  443. {
  444. RT_UNUSED(idx);
  445. RT_UNUSED(name);
  446. LOG_E("gic[%d] %s IO[%p, %p] map fail", _gicv2_nr, name[idx], regs[idx * 2], regs[idx * 2 + 1]);
  447. }
  448. return err;
  449. }
  450. static void gicv2_init(struct gicv2 *gic)
  451. {
  452. gicv2_dist_init(gic);
  453. gic->parent.priv_data = gic;
  454. gic->parent.ops = &gicv2_ops;
  455. rt_pic_linear_irq(&gic->parent, gic->max_irq + 1 - GIC_SGI_NR);
  456. gic_common_sgi_config(gic->dist_base, &gic->parent, _gicv2_nr * GIC_SGI_NR);
  457. rt_pic_add_traps(gicv2_handler, gic);
  458. rt_pic_user_extends(&gic->parent);
  459. }
  460. static void gicv2_init_fail(struct gicv2 *gic)
  461. {
  462. if (gic->dist_base)
  463. {
  464. rt_iounmap(gic->dist_base);
  465. }
  466. if (gic->cpu_base)
  467. {
  468. rt_iounmap(gic->cpu_base);
  469. }
  470. if (gic->hyp_base)
  471. {
  472. rt_iounmap(gic->hyp_base);
  473. }
  474. if (gic->vcpu_base)
  475. {
  476. rt_iounmap(gic->vcpu_base);
  477. }
  478. rt_memset(gic, 0, sizeof(*gic));
  479. }
  480. static rt_err_t gicv2_ofw_init(struct rt_ofw_node *np, const struct rt_ofw_node_id *id)
  481. {
  482. rt_err_t err = RT_EOK;
  483. struct gicv2 *gic = RT_NULL;
  484. do {
  485. rt_uint64_t regs[8];
  486. if (_gicv2_nr >= RT_PIC_ARM_GIC_MAX_NR)
  487. {
  488. LOG_W("GICv2/v1 table is full");
  489. err = -RT_EFULL;
  490. break;
  491. }
  492. gic = &_gicv2_list[_gicv2_nr];
  493. rt_ofw_get_address_array(np, RT_ARRAY_SIZE(regs), regs);
  494. if ((err = gicv2_iomap_init(gic, regs)))
  495. {
  496. break;
  497. }
  498. if (gic->version != 1 && gic->version != 2)
  499. {
  500. LOG_E("Version = %d is not support", gic->version);
  501. err = -RT_EINVAL;
  502. break;
  503. }
  504. gic_common_init_quirk_ofw(np, _gicv2_quirks, gic);
  505. gicv2_init(gic);
  506. rt_ofw_data(np) = &gic->parent;
  507. if (gic->version == 2)
  508. {
  509. #ifdef RT_PIC_ARM_GIC_V2M
  510. gicv2m_ofw_probe(np, id);
  511. #endif
  512. }
  513. ++_gicv2_nr;
  514. } while (0);
  515. if (err && gic)
  516. {
  517. gicv2_init_fail(gic);
  518. }
  519. return err;
  520. }
  521. static const struct rt_ofw_node_id gicv2_ofw_ids[] =
  522. {
  523. { .compatible = "arm,gic-400" },
  524. { .compatible = "arm,arm11mp-gic" },
  525. { .compatible = "arm,arm1176jzf-devchip-gic" },
  526. { .compatible = "arm,cortex-a15-gic" },
  527. { .compatible = "arm,cortex-a9-gic" },
  528. { .compatible = "arm,cortex-a7-gic" },
  529. { .compatible = "qcom,msm-8660-qgic" },
  530. { .compatible = "qcom,msm-qgic2" },
  531. { .compatible = "arm,pl390" },
  532. { /* sentinel */ }
  533. };
  534. RT_PIC_OFW_DECLARE(gicv2, gicv2_ofw_ids, gicv2_ofw_init);