irq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 = 0, parent_interrupt_cells = 0;
  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, almost no SoC will be use multi IC to implement 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. map_len /= sizeof(fdt32_t);
  180. map_mask_len /= sizeof(fdt32_t);
  181. err = -RT_EINVAL;
  182. addr = irq_args->data;
  183. addr_cells = irq_args->args[0];
  184. pin_cells = irq_args->args[1];
  185. icaddr_cells = irq_args->args_count == 3 ? irq_args->args[2] : 0;
  186. if (addr_cells > child_address_cells)
  187. {
  188. LOG_D("%s(%d) > %s(%d)", "addr_cells", addr_cells, "child_address_cells", child_address_cells);
  189. break;
  190. }
  191. if (pin_cells > child_interrupt_cells)
  192. {
  193. LOG_D("%s(%d) > %s(%d)", "pin_cells", pin_cells, "child_interrupt_cells", child_interrupt_cells);
  194. break;
  195. }
  196. err = -RT_ENOENT;
  197. #define _map_walk_range(_idx, _idx2, _count, ...) \
  198. for (idx1 = _idx, idx2 = _idx2, limit = idx1 + _count; idx1 < limit __VA_ARGS__; ++idx1, ++idx2)
  199. _map_walk_range(0, 0, addr_cells)
  200. {
  201. /* Applied addr mask */
  202. ((fdt32_t *)addr)[idx1] &= map_mask[idx2];
  203. }
  204. _map_walk_range(addr_cells, child_address_cells, pin_cells)
  205. {
  206. /* Applied pin mask */
  207. ((fdt32_t *)addr)[idx1] &= map_mask[idx2];
  208. }
  209. while (map_len > 0)
  210. {
  211. rt_bool_t match = RT_TRUE;
  212. _map_walk_range(0, 0, addr_cells)
  213. {
  214. /* Applied mask */
  215. if (addr[idx1] != map[idx2])
  216. {
  217. match = RT_FALSE;
  218. break;
  219. }
  220. }
  221. _map_walk_range(addr_cells, child_address_cells, pin_cells, && match)
  222. {
  223. /* Applied mask */
  224. if (addr[idx1] != map[idx2])
  225. {
  226. match = RT_FALSE;
  227. break;
  228. }
  229. }
  230. /* Skip addr, pin */
  231. map += map_mask_len;
  232. /* IC is different? */
  233. if (ic_phandle != fdt32_to_cpu(*map))
  234. {
  235. rt_ofw_node_put(ic_np);
  236. ic_phandle = fdt32_to_cpu(*map);
  237. ic_np = rt_ofw_find_node_by_phandle(ic_phandle);
  238. if (!ic_np)
  239. {
  240. LOG_D("%s irq parent phandle = %d is not found", np->full_name, ic_phandle);
  241. break;
  242. }
  243. if ((parent_address_cells = rt_ofw_bus_addr_cells(ic_np)) < 0)
  244. {
  245. LOG_D("%s property %s is undefined", ic_np->full_name, "#address-cells");
  246. break;
  247. }
  248. if (icaddr_cells > parent_address_cells)
  249. {
  250. LOG_D("%s(%d) > %s(%d)", "icaddr_cells", icaddr_cells, "parent_address_cells", parent_address_cells);
  251. break;
  252. }
  253. if ((parent_interrupt_cells = ofw_interrupt_cells(ic_np)) < 0)
  254. {
  255. LOG_D("%s property %s is undefined", ic_np->full_name, "#interrupt-cells");
  256. break;
  257. }
  258. RT_ASSERT(parent_interrupt_cells <= RT_OFW_MAX_CELL_ARGS);
  259. }
  260. /* Skip ic phandle */
  261. ++map;
  262. _map_walk_range(addr_cells + pin_cells, 0, icaddr_cells, && match)
  263. {
  264. /* Applied ic_addr mask */
  265. if (addr[idx1] != map[idx2])
  266. {
  267. match = RT_FALSE;
  268. break;
  269. }
  270. }
  271. /* Skip icaddr */
  272. map += parent_address_cells;
  273. if (match)
  274. {
  275. irq_args->data = ic_np;
  276. irq_args->args_count = parent_interrupt_cells;
  277. for (int i = 0; i < irq_args->args_count; ++i)
  278. {
  279. irq_args->args[i] = fdt32_to_cpu(*map++);
  280. }
  281. err = RT_EOK;
  282. break;
  283. }
  284. /* Skip icintr */
  285. map += parent_interrupt_cells;
  286. map_len -= map_mask_len + 1 + parent_address_cells + parent_interrupt_cells;
  287. }
  288. #undef _map_walk_range
  289. } while (0);
  290. return err;
  291. }
  292. rt_err_t rt_ofw_parse_irq_map(struct rt_ofw_node *np, struct rt_ofw_cell_args *irq_args)
  293. {
  294. rt_err_t err;
  295. if (np && irq_args && irq_args->data)
  296. {
  297. err = ofw_parse_irq_map(np, irq_args);
  298. }
  299. else
  300. {
  301. err = -RT_EINVAL;
  302. }
  303. return err;
  304. }
  305. static rt_err_t ofw_parse_irq_cells(struct rt_ofw_node *np, int index, struct rt_ofw_cell_args *out_irq_args)
  306. {
  307. rt_err_t err;
  308. /*
  309. * interrupts-extended:
  310. *
  311. * The interrupts-extended property lists the interrupt(s) generated by a
  312. * device. interrupts-extended should be used instead of interrupts when a
  313. * device is connected to multiple interrupt controllers as it encodes a
  314. * parent phandle with each interrupt specifier. Example:
  315. *
  316. * pic: interrupt-controller@0 {
  317. * interrupt-controller;
  318. * #interrupt-cells = <1>;
  319. * };
  320. *
  321. * gic: interrupt-controller@1 {
  322. * interrupt-controller;
  323. * #interrupt-cells = <3>;
  324. * };
  325. *
  326. * node: node {
  327. * interrupts-extended = <&pic 9>, <&gic GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>;
  328. * };
  329. *
  330. * call `rt_ofw_parse_phandle_cells` to get irq info;
  331. */
  332. err = rt_ofw_parse_phandle_cells(np, "interrupts-extended", "#interrupt-cells", index, out_irq_args);
  333. do {
  334. int interrupt_cells;
  335. const fdt32_t *cell;
  336. rt_ssize_t interrupt_len;
  337. struct rt_ofw_node *ic_np;
  338. if (!err)
  339. {
  340. break;
  341. }
  342. /*
  343. * interrupts (old style):
  344. *
  345. * The interrupts property of a device node defines the interrupt or
  346. * interrupts that are generated by the device. The value of the
  347. * interrupts property consists of an arbitrary number of interrupt
  348. * specifiers. The format of an interrupt specifier is defined by the
  349. * binding of the interrupt domain root.
  350. * interrupts is overridden by the interrupts-extended property and
  351. * normally only one or the other should be used. Example:
  352. *
  353. * pic: interrupt-controller@0 {
  354. * interrupt-controller;
  355. * #interrupt-cells = <1>;
  356. * };
  357. *
  358. * gic: interrupt-controller@1 {
  359. * interrupt-controller;
  360. * #interrupt-cells = <3>;
  361. * };
  362. *
  363. * node0: node0 {
  364. * interrupt-parent = <&pic>;
  365. * interrupts = <9>;
  366. * };
  367. *
  368. * node1: node1 {
  369. * interrupt-parent = <&gic>;
  370. * interrupts = <GIC_SPI 1 IRQ_TYPE_LEVEL_HIGH>;
  371. * };
  372. */
  373. cell = rt_ofw_prop_read_raw(np, "interrupts", &interrupt_len);
  374. if (!cell)
  375. {
  376. err = -RT_ERROR;
  377. break;
  378. }
  379. ic_np = rt_ofw_find_irq_parent(np, &interrupt_cells);
  380. if (!ic_np)
  381. {
  382. err = -RT_ERROR;
  383. break;
  384. }
  385. RT_ASSERT(interrupt_cells <= RT_OFW_MAX_CELL_ARGS);
  386. if (index >= interrupt_len / (interrupt_cells * sizeof(*cell)))
  387. {
  388. err = -RT_EINVAL;
  389. break;
  390. }
  391. cell += index * interrupt_cells;
  392. out_irq_args->data = ic_np;
  393. out_irq_args->args_count = interrupt_cells;
  394. for (int idx = 0; idx < interrupt_cells; ++idx, ++cell)
  395. {
  396. out_irq_args->args[idx] = fdt32_to_cpu(*cell);
  397. }
  398. err = RT_EOK;
  399. } while (0);
  400. return err;
  401. }
  402. rt_err_t rt_ofw_parse_irq_cells(struct rt_ofw_node *np, int index, struct rt_ofw_cell_args *out_irq_args)
  403. {
  404. rt_err_t err;
  405. if (np && index >= 0 && out_irq_args)
  406. {
  407. err = ofw_parse_irq_cells(np, index, out_irq_args);
  408. }
  409. else
  410. {
  411. err = -RT_EINVAL;
  412. }
  413. return err;
  414. }
  415. struct rt_ofw_node *rt_ofw_find_irq_parent(struct rt_ofw_node *np, int *out_interrupt_cells)
  416. {
  417. for (np = rt_ofw_node_get(np); np; np = rt_ofw_get_next_parent(np))
  418. {
  419. rt_phandle ic_phandle;
  420. if (!rt_ofw_prop_read_u32(np, "interrupt-parent", (rt_uint32_t *)&ic_phandle))
  421. {
  422. int interrupt_cells;
  423. struct rt_ofw_node *ic_np = rt_ofw_find_node_by_phandle(ic_phandle);
  424. if (ic_np && (interrupt_cells = ofw_interrupt_cells(ic_np)) >= 0)
  425. {
  426. np = ic_np;
  427. if (out_interrupt_cells)
  428. {
  429. *out_interrupt_cells = interrupt_cells;
  430. }
  431. break;
  432. }
  433. rt_ofw_node_put(ic_np);
  434. }
  435. }
  436. return np;
  437. }
  438. static int ofw_map_irq(struct rt_ofw_cell_args *irq_args)
  439. {
  440. int irq;
  441. struct rt_ofw_node *ic_np = irq_args->data;
  442. struct rt_pic *pic = rt_pic_dynamic_cast(rt_ofw_data(ic_np));
  443. /* args.data is "interrupt-controller" */
  444. if (pic)
  445. {
  446. struct rt_pic_irq pirq;
  447. if (!pic->ops->irq_parse)
  448. {
  449. LOG_E("Master pic MUST implemented irq_parse");
  450. RT_ASSERT(0);
  451. }
  452. if (!pic->ops->irq_map)
  453. {
  454. LOG_E("Master pic MUST implemented irq_map");
  455. RT_ASSERT(0);
  456. }
  457. irq = pic->ops->irq_parse(pic, irq_args, &pirq);
  458. if (!irq)
  459. {
  460. irq = pic->ops->irq_map(pic, pirq.hwirq, pirq.mode);
  461. }
  462. }
  463. else
  464. {
  465. LOG_E("Master pic %s not support", ic_np->full_name);
  466. irq = -RT_EIO;
  467. }
  468. rt_ofw_node_put(ic_np);
  469. return irq;
  470. }
  471. int rt_ofw_map_irq(struct rt_ofw_cell_args *irq_args)
  472. {
  473. int irq;
  474. if (irq_args && irq_args->data && irq_args->args_count > 0)
  475. {
  476. irq = ofw_map_irq(irq_args);
  477. }
  478. else
  479. {
  480. irq = -RT_EINVAL;
  481. }
  482. return irq;
  483. }
  484. int rt_ofw_get_irq_count(struct rt_ofw_node *np)
  485. {
  486. int count;
  487. if (np)
  488. {
  489. struct rt_ofw_cell_args irq_args;
  490. count = 0;
  491. while (!ofw_parse_irq_cells(np, count, &irq_args))
  492. {
  493. ++count;
  494. }
  495. }
  496. else
  497. {
  498. count = -RT_EINVAL;
  499. }
  500. return count;
  501. }
  502. int rt_ofw_get_irq(struct rt_ofw_node *np, int index)
  503. {
  504. int irq;
  505. if (np && index >= 0)
  506. {
  507. struct rt_ofw_cell_args irq_args;
  508. irq = ofw_parse_irq_cells(np, index, &irq_args);
  509. if (irq >= 0)
  510. {
  511. rt_phandle cpu_phandle;
  512. irq = ofw_map_irq(&irq_args);
  513. if (irq >= 0 && !rt_ofw_prop_read_u32_index(np, "interrupt-affinity", index, &cpu_phandle))
  514. {
  515. rt_uint64_t cpuid = rt_ofw_get_cpu_id(rt_ofw_find_node_by_phandle(cpu_phandle));
  516. if ((rt_int64_t)cpuid >= 0)
  517. {
  518. RT_BITMAP_DECLARE(affinity, RT_CPUS_NR) = { 0 };
  519. rt_bitmap_set_bit(affinity, cpuid);
  520. if (rt_pic_irq_set_affinity(irq, affinity) == -RT_ENOSYS)
  521. {
  522. LOG_W("%s irq affinity init fail", np->full_name);
  523. }
  524. }
  525. }
  526. }
  527. }
  528. else
  529. {
  530. irq = -RT_EINVAL;
  531. }
  532. return irq;
  533. }
  534. int rt_ofw_get_irq_by_name(struct rt_ofw_node *np, const char *name)
  535. {
  536. int irq;
  537. if (np && name)
  538. {
  539. int index = rt_ofw_prop_index_of_string(np, "interrupt-names", name);
  540. if (index >= 0)
  541. {
  542. irq = rt_ofw_get_irq(np, index);
  543. }
  544. else
  545. {
  546. irq = -1;
  547. }
  548. }
  549. else
  550. {
  551. irq = -RT_EINVAL;
  552. }
  553. return irq;
  554. }