irq.c 18 KB

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