pic-gicv2.c 15 KB

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