irq.c 19 KB

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