irq.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. * 2022-08-25 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #include <drivers/pic.h>
  12. #include <drivers/ofw.h>
  13. #include <drivers/ofw_io.h>
  14. #include <drivers/ofw_irq.h>
  15. #define DBG_TAG "rtdm.ofw"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. #include "ofw_internal.h"
  19. static int ofw_interrupt_cells(struct rt_ofw_node *np)
  20. {
  21. int interrupt_cells = -RT_EEMPTY;
  22. rt_ofw_prop_read_u32(np, "#interrupt-cells", (rt_uint32_t *)&interrupt_cells);
  23. return interrupt_cells;
  24. }
  25. int rt_ofw_irq_cells(struct rt_ofw_node *np)
  26. {
  27. return np ? ofw_interrupt_cells(np) : -RT_EINVAL;
  28. }
  29. static rt_err_t ofw_parse_irq_map(struct rt_ofw_node *np, struct rt_ofw_cell_args *irq_args)
  30. {
  31. rt_err_t err = RT_EOK;
  32. rt_phandle ic_phandle = 0;
  33. rt_ssize_t map_len, map_mask_len;
  34. struct rt_ofw_node *ic_np = RT_NULL;
  35. const fdt32_t *addr, *map, *map_mask;
  36. int child_address_cells, child_interrupt_cells;
  37. int parent_address_cells, parent_interrupt_cells;
  38. int addr_cells, pin_cells, icaddr_cells, idx1, idx2, limit;
  39. /*
  40. * interrupt-map:
  41. * An interrupt-map is a property on a nexus node that bridges one
  42. * interrupt domain with a set of parent interrupt domains and specifies
  43. * how interrupt specifiers in the child domain are mapped to
  44. * their respective parent domains.
  45. *
  46. * The interrupt map is a table where each row is a mapping entry
  47. * consisting of five components: child unit address, child interrupt
  48. * specifier, interrupt-parent, parent unit address, parent interrupt
  49. * specifier.
  50. *
  51. * child unit address
  52. * The unit address of the child node being mapped. The number of
  53. * 32-bit cells required to specify this is described by the
  54. * #address-cells property of the bus node on which the child is
  55. * located.
  56. *
  57. * child interrupt specifier
  58. * The interrupt specifier of the child node being mapped. The number
  59. * of 32-bit cells required to specify this component is described by
  60. * the #interrupt-cells property of this node—the nexus node containing
  61. * the interrupt-map property.
  62. *
  63. * interrupt-parent
  64. * A single <phandle> value that points to the interrupt parent to
  65. * which the child domain is being mapped.
  66. *
  67. * parent unit address
  68. * The unit address in the domain of the interrupt parent. The number
  69. * of 32-bit cells required to specify this address is described by the
  70. * #address-cells property of the node pointed to by the
  71. * interrupt-parent field.
  72. *
  73. * parent interrupt specifier
  74. * The interrupt specifier in the parent domain. The number of 32-bit
  75. * cells required to specify this component is described by the
  76. * #interrupt-cells property of the node pointed to by the
  77. * interrupt-parent field.
  78. *
  79. * Lookups are performed on the interrupt mapping table by matching a
  80. * unit-address/interrupt specifier pair against the child components in
  81. * the interrupt-map. Because some fields in the unit interrupt specifier
  82. * may not be relevant, a mask is applied before the lookup is done.
  83. * Example:
  84. *
  85. * pic: interrupt-controller@0 {
  86. * interrupt-controller;
  87. * #address-cells = <0>; // icaddr (parent unit address)
  88. * #interrupt-cells = <1>; // icintr (parent interrupt specifier)
  89. * };
  90. *
  91. * gic: interrupt-controller@1 {
  92. * interrupt-controller;
  93. * #address-cells = <2>; // icaddr (parent unit address)
  94. * #interrupt-cells = <3>; // icintr (parent interrupt specifier)
  95. * };
  96. *
  97. * pcie {
  98. * #address-cells = <3>; // addr (child unit address)
  99. * #interrupt-cells = <1>; // pin (child interrupt specifier)
  100. * interrupt-parent = <&gic>;
  101. * interrupt-map-mask = <0x1800 0 0 7>;
  102. * interrupt-map =
  103. * // addr pin ic icintr
  104. * <0x0000 0 0 1 &pic 1>, // INTA SOLT 0
  105. * <0x0000 0 0 2 &pic 2>, // INTB
  106. * <0x0000 0 0 3 &pic 3>, // INTC
  107. * <0x0000 0 0 4 &pic 4>, // INTD
  108. * <0x0800 0 0 1 &pic 2>, // INTA SOLT 1
  109. * <0x0800 0 0 2 &pic 3>, // INTB
  110. * <0x0800 0 0 3 &pic 4>, // INTC
  111. * <0x0800 0 0 4 &pic 1>, // INTD
  112. * // addr pin ic icaddr icintr
  113. * <0x1000 0 0 1 &gic 0 0 GIC_SPI 3 IRQ_TYPE_LEVEL_HIGH>, // INTA SOLT 2
  114. * <0x1000 0 0 2 &gic 0 0 GIC_SPI 4 IRQ_TYPE_LEVEL_HIGH>, // INTB
  115. * <0x1000 0 0 3 &gic 0 0 GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>, // INTC
  116. * <0x1000 0 0 4 &gic 0 0 GIC_SPI 2 IRQ_TYPE_LEVEL_HIGH>, // INTD
  117. * <0x1800 0 0 1 &gic 0 0 GIC_SPI 4 IRQ_TYPE_LEVEL_HIGH>, // INTA SOLT 3
  118. * <0x1800 0 0 2 &gic 0 0 GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>, // INTB
  119. * <0x1800 0 0 3 &gic 0 0 GIC_SPI 2 IRQ_TYPE_LEVEL_HIGH>, // INTC
  120. * <0x1800 0 0 4 &gic 0 0 GIC_SPI 3 IRQ_TYPE_LEVEL_HIGH>; // INTD
  121. * };
  122. *
  123. * In fact, basically no SoC will be use multi ic to implemented INTx.
  124. * before call ofw_parse_irq_map(np, &args):
  125. *
  126. * args.data = addr;
  127. * args.args_count = 2 or 3;
  128. * args.args[0] = (addr cells);
  129. * args.args[1] = (pin cells);
  130. * args.args[2] = (icaddr cells);
  131. *
  132. * if call with `pcie` in ofw_parse_irq_map(np, &args):
  133. *
  134. * np = &pcie;
  135. * args.data = addr = fdt32_t({ (bus << 16) | (device << 11) | (function << 8), 0, 0, pin });
  136. * args.args_count = 2;
  137. * args.args[0] = 3;
  138. * args.args[1] = 1;
  139. *
  140. * To perform a lookup of the gic interrupt source number for INTB for IDSEL
  141. * 0x12 (slot 2), function 0x3, the following steps would be performed:
  142. *
  143. * 1.The user addr is value <0x9300 0 0 2>.
  144. *
  145. * 2.The encoding of the address includes the bus number (0x0 << 16),
  146. * device number (0x12 << 11), and function number (0x3 << 8).
  147. *
  148. * 3.The interrupt specifier is 2, which is the encoding for INTB as per
  149. * the PCI binding.
  150. *
  151. * 4.The interrupt-map-mask value <0x1800 0 0 7> is applied, giving a
  152. * result of <0x1000 0 0 2>.
  153. *
  154. * 5.That result is looked up in the interrupt-map table, which maps to the
  155. * parent interrupt specifier <GIC_SPI 4 IRQ_TYPE_LEVEL_HIGH>.
  156. */
  157. do {
  158. err = -RT_EEMPTY;
  159. if ((child_address_cells = rt_ofw_bus_addr_cells(np)) < 0)
  160. {
  161. LOG_D("%s property %s is undefined", np->full_name, "#address-cells");
  162. break;
  163. }
  164. if ((child_interrupt_cells = ofw_interrupt_cells(np)) < 0)
  165. {
  166. LOG_D("%s property %s is undefined", np->full_name, "#interrupt-cells");
  167. break;
  168. }
  169. if (!(map = rt_ofw_prop_read_raw(np, "interrupt-map", &map_len)))
  170. {
  171. LOG_D("%s property %s is undefined", np->full_name, "interrupt-map");
  172. break;
  173. }
  174. if (!(map_mask = rt_ofw_prop_read_raw(np, "interrupt-map-mask", &map_mask_len)))
  175. {
  176. LOG_D("%s property %s is undefined", np->full_name, "interrupt-map-mask");
  177. break;
  178. }
  179. err = -RT_EINVAL;
  180. addr = irq_args->data;
  181. addr_cells = irq_args->args[0];
  182. pin_cells = irq_args->args[1];
  183. icaddr_cells = irq_args->args_count == 3 ? irq_args->args[2] : 0;
  184. if (addr_cells > child_address_cells)
  185. {
  186. LOG_D("%s(%d) > %s(%d)", "addr_cells", addr_cells, "child_address_cells", child_address_cells);
  187. break;
  188. }
  189. if (pin_cells > child_interrupt_cells)
  190. {
  191. LOG_D("%s(%d) > %s(%d)", "pin_cells", pin_cells, "child_interrupt_cells", child_interrupt_cells);
  192. break;
  193. }
  194. err = -RT_ENOENT;
  195. #define _map_walk_range(_idx, _idx2, _count, ...) \
  196. for (idx1 = _idx, idx2 = _idx2, limit = idx1 + _count; idx1 < limit __VA_ARGS__; ++idx1, ++idx2)
  197. _map_walk_range(0, 0, addr_cells)
  198. {
  199. /* Applied addr mask */
  200. ((fdt32_t *)addr)[idx1] &= map_mask[idx2];
  201. }
  202. _map_walk_range(addr_cells, child_address_cells, pin_cells)
  203. {
  204. /* Applied pin mask */
  205. ((fdt32_t *)addr)[idx1] &= map_mask[idx2];
  206. }
  207. while (map_len > 0)
  208. {
  209. rt_bool_t match = RT_TRUE;
  210. _map_walk_range(0, 0, addr_cells)
  211. {
  212. /* Applied mask */
  213. if (addr[idx1] != map[idx2])
  214. {
  215. match = RT_FALSE;
  216. break;
  217. }
  218. }
  219. _map_walk_range(addr_cells, child_address_cells, pin_cells, && match)
  220. {
  221. /* Applied mask */
  222. if (addr[idx1] != map[idx2])
  223. {
  224. match = RT_FALSE;
  225. break;
  226. }
  227. }
  228. /* Skip addr, pin */
  229. map += map_mask_len;
  230. /* IC is different? */
  231. if (ic_phandle != fdt32_to_cpu(*map))
  232. {
  233. rt_ofw_node_put(ic_np);
  234. ic_phandle = fdt32_to_cpu(*map);
  235. ic_np = rt_ofw_find_node_by_phandle(ic_phandle);
  236. if (!ic_np)
  237. {
  238. LOG_D("%s irq parent phandle = %d is not found", np->full_name, ic_phandle);
  239. break;
  240. }
  241. if ((parent_address_cells = rt_ofw_bus_addr_cells(ic_np)) < 0)
  242. {
  243. LOG_D("%s property %s is undefined", ic_np->full_name, "#address-cells");
  244. break;
  245. }
  246. if (icaddr_cells > parent_address_cells)
  247. {
  248. LOG_D("%s(%d) > %s(%d)", "icaddr_cells", icaddr_cells, "parent_address_cells", parent_address_cells);
  249. break;
  250. }
  251. if ((parent_interrupt_cells = ofw_interrupt_cells(ic_np)) < 0)
  252. {
  253. LOG_D("%s property %s is undefined", ic_np->full_name, "#interrupt-cells");
  254. break;
  255. }
  256. RT_ASSERT(parent_interrupt_cells <= RT_OFW_MAX_CELL_ARGS);
  257. }
  258. /* Skip ic phandle */
  259. ++map;
  260. _map_walk_range(addr_cells + pin_cells, 0, icaddr_cells, && match)
  261. {
  262. /* Applied ic_addr mask */
  263. if (addr[idx1] != map[idx2])
  264. {
  265. match = RT_FALSE;
  266. break;
  267. }
  268. }
  269. /* Skip icaddr */
  270. map += parent_address_cells;
  271. if (match)
  272. {
  273. irq_args->data = ic_np;
  274. irq_args->args_count = parent_interrupt_cells;
  275. for (int i = 0; i < irq_args->args_count; ++i)
  276. {
  277. irq_args->args[i] = fdt32_to_cpu(*map++);
  278. }
  279. err = RT_EOK;
  280. break;
  281. }
  282. /* Skip icintr */
  283. map += parent_interrupt_cells;
  284. map_len -= map_mask_len + 1 + parent_address_cells + parent_interrupt_cells;
  285. }
  286. #undef _map_walk_range
  287. } while (0);
  288. return err;
  289. }
  290. rt_err_t rt_ofw_parse_irq_map(struct rt_ofw_node *np, struct rt_ofw_cell_args *irq_args)
  291. {
  292. rt_err_t err;
  293. if (np && irq_args && irq_args->data)
  294. {
  295. err = ofw_parse_irq_map(np, irq_args);
  296. }
  297. else
  298. {
  299. err = -RT_EINVAL;
  300. }
  301. return err;
  302. }
  303. static rt_err_t ofw_parse_irq_cells(struct rt_ofw_node *np, int index, struct rt_ofw_cell_args *out_irq_args)
  304. {
  305. rt_err_t err;
  306. /*
  307. * interrupts-extended:
  308. *
  309. * The interrupts-extended property lists the interrupt(s) generated by a
  310. * device. interrupts-extended should be used instead of interrupts when a
  311. * device is connected to multiple interrupt controllers as it encodes a
  312. * parent phandle with each interrupt specifier. Example:
  313. *
  314. * pic: interrupt-controller@0 {
  315. * interrupt-controller;
  316. * #interrupt-cells = <1>;
  317. * };
  318. *
  319. * gic: interrupt-controller@1 {
  320. * interrupt-controller;
  321. * #interrupt-cells = <3>;
  322. * };
  323. *
  324. * node: node {
  325. * interrupts-extended = <&pic 9>, <&gic GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>;
  326. * };
  327. *
  328. * call `rt_ofw_parse_phandle_cells` to get irq info;
  329. */
  330. err = rt_ofw_parse_phandle_cells(np, "interrupts-extended", "#interrupt-cells", index, out_irq_args);
  331. do {
  332. int interrupt_cells;
  333. const fdt32_t *cell;
  334. rt_ssize_t interrupt_len;
  335. struct rt_ofw_node *ic_np;
  336. if (!err)
  337. {
  338. break;
  339. }
  340. /*
  341. * interrupts (old style):
  342. *
  343. * The interrupts property of a device node defines the interrupt or
  344. * interrupts that are generated by the device. The value of the
  345. * interrupts property consists of an arbitrary number of interrupt
  346. * specifiers. The format of an interrupt specifier is defined by the
  347. * binding of the interrupt domain root.
  348. * interrupts is overridden by the interrupts-extended property and
  349. * normally only one or the other should be used. Example:
  350. *
  351. * pic: interrupt-controller@0 {
  352. * interrupt-controller;
  353. * #interrupt-cells = <1>;
  354. * };
  355. *
  356. * gic: interrupt-controller@1 {
  357. * interrupt-controller;
  358. * #interrupt-cells = <3>;
  359. * };
  360. *
  361. * node0: node0 {
  362. * interrupt-parent = <&pic>;
  363. * interrupts = <9>;
  364. * };
  365. *
  366. * node1: node1 {
  367. * interrupt-parent = <&gic>;
  368. * interrupts = <GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>;
  369. * };
  370. */
  371. cell = rt_ofw_prop_read_raw(np, "interrupts", &interrupt_len);
  372. if (!cell)
  373. {
  374. err = -RT_ERROR;
  375. break;
  376. }
  377. ic_np = rt_ofw_find_irq_parent(np, &interrupt_cells);
  378. if (!ic_np)
  379. {
  380. err = -RT_ERROR;
  381. break;
  382. }
  383. RT_ASSERT(interrupt_cells <= RT_OFW_MAX_CELL_ARGS);
  384. if (index >= interrupt_len / (interrupt_cells * sizeof(*cell)))
  385. {
  386. err = -RT_EINVAL;
  387. break;
  388. }
  389. cell += index * interrupt_cells;
  390. out_irq_args->data = ic_np;
  391. out_irq_args->args_count = interrupt_cells;
  392. for (int idx = 0; idx < interrupt_cells; ++idx, ++cell)
  393. {
  394. out_irq_args->args[idx] = fdt32_to_cpu(*cell);
  395. }
  396. err = RT_EOK;
  397. } while (0);
  398. return err;
  399. }
  400. rt_err_t rt_ofw_parse_irq_cells(struct rt_ofw_node *np, int index, struct rt_ofw_cell_args *out_irq_args)
  401. {
  402. rt_err_t err;
  403. if (np && index >= 0 && out_irq_args)
  404. {
  405. err = ofw_parse_irq_cells(np, index, out_irq_args);
  406. }
  407. else
  408. {
  409. err = -RT_EINVAL;
  410. }
  411. return err;
  412. }
  413. struct rt_ofw_node *rt_ofw_find_irq_parent(struct rt_ofw_node *np, int *out_interrupt_cells)
  414. {
  415. rt_ofw_foreach_parent_node(np)
  416. {
  417. rt_phandle ic_phandle;
  418. if (!rt_ofw_prop_read_u32(np, "interrupt-parent", (rt_uint32_t *)&ic_phandle))
  419. {
  420. int interrupt_cells;
  421. struct rt_ofw_node *ic_np = rt_ofw_find_node_by_phandle(ic_phandle);
  422. if (ic_np && (interrupt_cells = ofw_interrupt_cells(ic_np)) >= 0)
  423. {
  424. np = ic_np;
  425. if (out_interrupt_cells)
  426. {
  427. *out_interrupt_cells = interrupt_cells;
  428. }
  429. break;
  430. }
  431. rt_ofw_node_put(ic_np);
  432. }
  433. }
  434. return np;
  435. }
  436. static int ofw_map_irq(struct rt_ofw_cell_args *irq_args)
  437. {
  438. int irq;
  439. struct rt_ofw_node *ic_np = irq_args->data;
  440. struct rt_pic *pic = rt_ofw_data(ic_np);
  441. /* args.data is "interrupt-controller" */
  442. if (pic)
  443. {
  444. struct rt_pic_irq pirq;
  445. if (!pic->ops->irq_parse)
  446. {
  447. LOG_E("Master pic MUST implemented irq_parse");
  448. RT_ASSERT(0);
  449. }
  450. if (!pic->ops->irq_map)
  451. {
  452. LOG_E("Master pic MUST implemented irq_map");
  453. RT_ASSERT(0);
  454. }
  455. irq = pic->ops->irq_parse(pic, irq_args, &pirq);
  456. if (!irq)
  457. {
  458. irq = pic->ops->irq_map(pic, pirq.hwirq, pirq.mode);
  459. }
  460. }
  461. else
  462. {
  463. LOG_E("Master pic %s not support", ic_np->full_name);
  464. irq = -RT_EIO;
  465. }
  466. rt_ofw_node_put(ic_np);
  467. return irq;
  468. }
  469. int rt_ofw_map_irq(struct rt_ofw_cell_args *irq_args)
  470. {
  471. int irq;
  472. if (irq_args && irq_args->data && irq_args->args_count > 0)
  473. {
  474. irq = ofw_map_irq(irq_args);
  475. }
  476. else
  477. {
  478. irq = -RT_EINVAL;
  479. }
  480. return irq;
  481. }
  482. int rt_ofw_get_irq_count(struct rt_ofw_node *np)
  483. {
  484. int count;
  485. if (np)
  486. {
  487. struct rt_ofw_cell_args irq_args;
  488. count = 0;
  489. while (!ofw_parse_irq_cells(np, count, &irq_args))
  490. {
  491. ++count;
  492. }
  493. }
  494. else
  495. {
  496. count = -RT_EINVAL;
  497. }
  498. return count;
  499. }
  500. int rt_ofw_get_irq(struct rt_ofw_node *np, int index)
  501. {
  502. int irq;
  503. if (np && index >= 0)
  504. {
  505. struct rt_ofw_cell_args irq_args;
  506. irq = ofw_parse_irq_cells(np, index, &irq_args);
  507. if (irq >= 0)
  508. {
  509. irq = ofw_map_irq(&irq_args);
  510. }
  511. }
  512. else
  513. {
  514. irq = -RT_EINVAL;
  515. }
  516. return irq;
  517. }
  518. int rt_ofw_get_irq_by_name(struct rt_ofw_node *np, const char *name)
  519. {
  520. int irq;
  521. if (np && name)
  522. {
  523. int index = rt_ofw_prop_index_of_string(np, "interrupt-names", name);
  524. if (index >= 0)
  525. {
  526. irq = rt_ofw_get_irq(np, index);
  527. }
  528. else
  529. {
  530. irq = -1;
  531. }
  532. }
  533. else
  534. {
  535. irq = -RT_EINVAL;
  536. }
  537. return irq;
  538. }