of_property.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
  4. */
  5. #include <linux/pci.h>
  6. #include <linux/of.h>
  7. #include <linux/of_irq.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/bits.h>
  10. #include "pci.h"
  11. #define OF_PCI_ADDRESS_CELLS 3
  12. #define OF_PCI_SIZE_CELLS 2
  13. #define OF_PCI_MAX_INT_PIN 4
  14. struct of_pci_addr_pair {
  15. u32 phys_addr[OF_PCI_ADDRESS_CELLS];
  16. u32 size[OF_PCI_SIZE_CELLS];
  17. };
  18. /*
  19. * Each entry in the ranges table is a tuple containing the child address,
  20. * the parent address, and the size of the region in the child address space.
  21. * Thus, for PCI, in each entry parent address is an address on the primary
  22. * side and the child address is the corresponding address on the secondary
  23. * side.
  24. */
  25. struct of_pci_range_entry {
  26. u32 child_addr[OF_PCI_ADDRESS_CELLS];
  27. u32 parent_addr[OF_PCI_ADDRESS_CELLS];
  28. u32 size[OF_PCI_SIZE_CELLS];
  29. };
  30. #define OF_PCI_ADDR_SPACE_IO 0x1
  31. #define OF_PCI_ADDR_SPACE_MEM32 0x2
  32. #define OF_PCI_ADDR_SPACE_MEM64 0x3
  33. #define OF_PCI_ADDR_FIELD_NONRELOC BIT(31)
  34. #define OF_PCI_ADDR_FIELD_SS GENMASK(25, 24)
  35. #define OF_PCI_ADDR_FIELD_PREFETCH BIT(30)
  36. #define OF_PCI_ADDR_FIELD_BUS GENMASK(23, 16)
  37. #define OF_PCI_ADDR_FIELD_DEV GENMASK(15, 11)
  38. #define OF_PCI_ADDR_FIELD_FUNC GENMASK(10, 8)
  39. #define OF_PCI_ADDR_FIELD_REG GENMASK(7, 0)
  40. enum of_pci_prop_compatible {
  41. PROP_COMPAT_PCI_VVVV_DDDD,
  42. PROP_COMPAT_PCICLASS_CCSSPP,
  43. PROP_COMPAT_PCICLASS_CCSS,
  44. PROP_COMPAT_NUM,
  45. };
  46. static void of_pci_set_address(struct pci_dev *pdev, u32 *prop, u64 addr,
  47. u32 reg_num, u32 flags, bool reloc)
  48. {
  49. if (pdev) {
  50. prop[0] = FIELD_PREP(OF_PCI_ADDR_FIELD_BUS, pdev->bus->number) |
  51. FIELD_PREP(OF_PCI_ADDR_FIELD_DEV, PCI_SLOT(pdev->devfn)) |
  52. FIELD_PREP(OF_PCI_ADDR_FIELD_FUNC, PCI_FUNC(pdev->devfn));
  53. } else
  54. prop[0] = 0;
  55. prop[0] |= flags | reg_num;
  56. if (!reloc) {
  57. prop[0] |= OF_PCI_ADDR_FIELD_NONRELOC;
  58. prop[1] = upper_32_bits(addr);
  59. prop[2] = lower_32_bits(addr);
  60. }
  61. }
  62. static int of_pci_get_addr_flags(const struct resource *res, u32 *flags)
  63. {
  64. u32 ss;
  65. if (res->flags & IORESOURCE_IO)
  66. ss = OF_PCI_ADDR_SPACE_IO;
  67. else if (res->flags & IORESOURCE_MEM_64)
  68. ss = OF_PCI_ADDR_SPACE_MEM64;
  69. else if (res->flags & IORESOURCE_MEM)
  70. ss = OF_PCI_ADDR_SPACE_MEM32;
  71. else
  72. return -EINVAL;
  73. *flags = 0;
  74. if (res->flags & IORESOURCE_PREFETCH)
  75. *flags |= OF_PCI_ADDR_FIELD_PREFETCH;
  76. *flags |= FIELD_PREP(OF_PCI_ADDR_FIELD_SS, ss);
  77. return 0;
  78. }
  79. static int of_pci_prop_bus_range(struct pci_dev *pdev,
  80. struct of_changeset *ocs,
  81. struct device_node *np)
  82. {
  83. u32 bus_range[] = { pdev->subordinate->busn_res.start,
  84. pdev->subordinate->busn_res.end };
  85. return of_changeset_add_prop_u32_array(ocs, np, "bus-range", bus_range,
  86. ARRAY_SIZE(bus_range));
  87. }
  88. static int of_pci_prop_ranges(struct pci_dev *pdev, struct of_changeset *ocs,
  89. struct device_node *np)
  90. {
  91. struct of_pci_range_entry *rp;
  92. struct resource *res;
  93. int i, j, ret;
  94. u32 flags, num;
  95. u64 val64;
  96. if (pci_is_bridge(pdev)) {
  97. num = PCI_BRIDGE_RESOURCE_NUM;
  98. res = &pdev->resource[PCI_BRIDGE_RESOURCES];
  99. } else {
  100. num = PCI_STD_NUM_BARS;
  101. res = &pdev->resource[PCI_STD_RESOURCES];
  102. }
  103. rp = kzalloc_objs(*rp, num);
  104. if (!rp)
  105. return -ENOMEM;
  106. for (i = 0, j = 0; j < num; j++) {
  107. if (!resource_size(&res[j]))
  108. continue;
  109. if (of_pci_get_addr_flags(&res[j], &flags))
  110. continue;
  111. val64 = pci_bus_address(pdev, &res[j] - pdev->resource);
  112. of_pci_set_address(pdev, rp[i].parent_addr, val64, 0, flags,
  113. false);
  114. if (pci_is_bridge(pdev)) {
  115. memcpy(rp[i].child_addr, rp[i].parent_addr,
  116. sizeof(rp[i].child_addr));
  117. } else {
  118. /*
  119. * For endpoint device, the lower 64-bits of child
  120. * address is always zero.
  121. */
  122. rp[i].child_addr[0] = j;
  123. }
  124. val64 = resource_size(&res[j]);
  125. rp[i].size[0] = upper_32_bits(val64);
  126. rp[i].size[1] = lower_32_bits(val64);
  127. i++;
  128. }
  129. ret = of_changeset_add_prop_u32_array(ocs, np, "ranges", (u32 *)rp,
  130. i * sizeof(*rp) / sizeof(u32));
  131. kfree(rp);
  132. return ret;
  133. }
  134. static int of_pci_prop_reg(struct pci_dev *pdev, struct of_changeset *ocs,
  135. struct device_node *np)
  136. {
  137. struct of_pci_addr_pair reg = { 0 };
  138. /* configuration space */
  139. of_pci_set_address(pdev, reg.phys_addr, 0, 0, 0, true);
  140. return of_changeset_add_prop_u32_array(ocs, np, "reg", (u32 *)&reg,
  141. sizeof(reg) / sizeof(u32));
  142. }
  143. static int of_pci_prop_interrupts(struct pci_dev *pdev,
  144. struct of_changeset *ocs,
  145. struct device_node *np)
  146. {
  147. int ret;
  148. u8 pin;
  149. ret = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
  150. if (ret != 0)
  151. return ret;
  152. if (!pin)
  153. return 0;
  154. return of_changeset_add_prop_u32(ocs, np, "interrupts", (u32)pin);
  155. }
  156. static int of_pci_prop_intr_ctrl(struct pci_dev *pdev, struct of_changeset *ocs,
  157. struct device_node *np)
  158. {
  159. int ret;
  160. u8 pin;
  161. ret = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
  162. if (ret != 0)
  163. return ret;
  164. if (!pin)
  165. return 0;
  166. ret = of_changeset_add_prop_u32(ocs, np, "#interrupt-cells", 1);
  167. if (ret)
  168. return ret;
  169. return of_changeset_add_prop_bool(ocs, np, "interrupt-controller");
  170. }
  171. static int of_pci_prop_intr_map(struct pci_dev *pdev, struct of_changeset *ocs,
  172. struct device_node *np)
  173. {
  174. u32 i, addr_sz[OF_PCI_MAX_INT_PIN] = { 0 }, map_sz = 0;
  175. struct of_phandle_args out_irq[OF_PCI_MAX_INT_PIN];
  176. __be32 laddr[OF_PCI_ADDRESS_CELLS] = { 0 };
  177. u32 int_map_mask[] = { 0xffff00, 0, 0, 7 };
  178. struct device_node *pnode;
  179. struct pci_dev *child;
  180. u32 *int_map, *mapp;
  181. int ret;
  182. u8 pin;
  183. pnode = pci_device_to_OF_node(pdev->bus->self);
  184. if (!pnode)
  185. pnode = pci_bus_to_OF_node(pdev->bus);
  186. if (!pnode) {
  187. pci_err(pdev, "failed to get parent device node");
  188. return -EINVAL;
  189. }
  190. laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8));
  191. for (pin = 1; pin <= OF_PCI_MAX_INT_PIN; pin++) {
  192. i = pin - 1;
  193. out_irq[i].np = pnode;
  194. out_irq[i].args_count = 1;
  195. out_irq[i].args[0] = pin;
  196. ret = of_irq_parse_raw(laddr, &out_irq[i]);
  197. if (ret) {
  198. out_irq[i].np = NULL;
  199. pci_dbg(pdev, "parse irq %d failed, ret %d", pin, ret);
  200. continue;
  201. }
  202. of_property_read_u32(out_irq[i].np, "#address-cells",
  203. &addr_sz[i]);
  204. }
  205. list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
  206. for (pin = 1; pin <= OF_PCI_MAX_INT_PIN; pin++) {
  207. i = pci_swizzle_interrupt_pin(child, pin) - 1;
  208. if (!out_irq[i].np)
  209. continue;
  210. map_sz += 5 + addr_sz[i] + out_irq[i].args_count;
  211. }
  212. }
  213. /*
  214. * Parsing interrupt failed for all pins. In this case, it does not
  215. * need to generate interrupt-map property.
  216. */
  217. if (!map_sz)
  218. return 0;
  219. int_map = kcalloc(map_sz, sizeof(u32), GFP_KERNEL);
  220. if (!int_map)
  221. return -ENOMEM;
  222. mapp = int_map;
  223. list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
  224. for (pin = 1; pin <= OF_PCI_MAX_INT_PIN; pin++) {
  225. i = pci_swizzle_interrupt_pin(child, pin) - 1;
  226. if (!out_irq[i].np)
  227. continue;
  228. *mapp = (child->bus->number << 16) |
  229. (child->devfn << 8);
  230. mapp += OF_PCI_ADDRESS_CELLS;
  231. *mapp = pin;
  232. mapp++;
  233. *mapp = out_irq[i].np->phandle;
  234. mapp++;
  235. /*
  236. * A device address does not affect the device <->
  237. * interrupt-controller HW connection for all
  238. * modern interrupt controllers; moreover, the
  239. * kernel (i.e., of_irq_parse_raw()) ignores the
  240. * values in the parent unit address cells while
  241. * parsing the interrupt-map property because they
  242. * are irrelevant for interrupt mapping in modern
  243. * systems.
  244. *
  245. * Leave the parent unit address initialized to 0 --
  246. * just take into account the #address-cells size
  247. * to build the property properly.
  248. */
  249. mapp += addr_sz[i];
  250. memcpy(mapp, out_irq[i].args,
  251. out_irq[i].args_count * sizeof(u32));
  252. mapp += out_irq[i].args_count;
  253. }
  254. }
  255. ret = of_changeset_add_prop_u32_array(ocs, np, "interrupt-map", int_map,
  256. map_sz);
  257. if (ret)
  258. goto failed;
  259. ret = of_changeset_add_prop_u32(ocs, np, "#interrupt-cells", 1);
  260. if (ret)
  261. goto failed;
  262. ret = of_changeset_add_prop_u32_array(ocs, np, "interrupt-map-mask",
  263. int_map_mask,
  264. ARRAY_SIZE(int_map_mask));
  265. if (ret)
  266. goto failed;
  267. kfree(int_map);
  268. return 0;
  269. failed:
  270. kfree(int_map);
  271. return ret;
  272. }
  273. static int of_pci_prop_compatible(struct pci_dev *pdev,
  274. struct of_changeset *ocs,
  275. struct device_node *np)
  276. {
  277. const char *compat_strs[PROP_COMPAT_NUM] = { 0 };
  278. int i, ret;
  279. compat_strs[PROP_COMPAT_PCI_VVVV_DDDD] =
  280. kasprintf(GFP_KERNEL, "pci%x,%x", pdev->vendor, pdev->device);
  281. compat_strs[PROP_COMPAT_PCICLASS_CCSSPP] =
  282. kasprintf(GFP_KERNEL, "pciclass,%06x", pdev->class);
  283. compat_strs[PROP_COMPAT_PCICLASS_CCSS] =
  284. kasprintf(GFP_KERNEL, "pciclass,%04x", pdev->class >> 8);
  285. ret = of_changeset_add_prop_string_array(ocs, np, "compatible",
  286. compat_strs, PROP_COMPAT_NUM);
  287. for (i = 0; i < PROP_COMPAT_NUM; i++)
  288. kfree(compat_strs[i]);
  289. return ret;
  290. }
  291. int of_pci_add_properties(struct pci_dev *pdev, struct of_changeset *ocs,
  292. struct device_node *np)
  293. {
  294. int ret;
  295. /*
  296. * The added properties will be released when the
  297. * changeset is destroyed.
  298. */
  299. if (pci_is_bridge(pdev)) {
  300. ret = of_changeset_add_prop_string(ocs, np, "device_type",
  301. "pci");
  302. if (ret)
  303. return ret;
  304. ret = of_pci_prop_bus_range(pdev, ocs, np);
  305. if (ret)
  306. return ret;
  307. ret = of_pci_prop_intr_map(pdev, ocs, np);
  308. if (ret)
  309. return ret;
  310. } else {
  311. ret = of_pci_prop_intr_ctrl(pdev, ocs, np);
  312. if (ret)
  313. return ret;
  314. }
  315. ret = of_pci_prop_ranges(pdev, ocs, np);
  316. if (ret)
  317. return ret;
  318. ret = of_changeset_add_prop_u32(ocs, np, "#address-cells",
  319. OF_PCI_ADDRESS_CELLS);
  320. if (ret)
  321. return ret;
  322. ret = of_changeset_add_prop_u32(ocs, np, "#size-cells",
  323. OF_PCI_SIZE_CELLS);
  324. if (ret)
  325. return ret;
  326. ret = of_pci_prop_reg(pdev, ocs, np);
  327. if (ret)
  328. return ret;
  329. ret = of_pci_prop_compatible(pdev, ocs, np);
  330. if (ret)
  331. return ret;
  332. ret = of_pci_prop_interrupts(pdev, ocs, np);
  333. if (ret)
  334. return ret;
  335. return 0;
  336. }
  337. static bool of_pci_is_range_resource(const struct resource *res, u32 *flags)
  338. {
  339. if (!(resource_type(res) & IORESOURCE_MEM) &&
  340. !(resource_type(res) & IORESOURCE_MEM_64))
  341. return false;
  342. if (of_pci_get_addr_flags(res, flags))
  343. return false;
  344. return true;
  345. }
  346. static int of_pci_host_bridge_prop_ranges(struct pci_host_bridge *bridge,
  347. struct of_changeset *ocs,
  348. struct device_node *np)
  349. {
  350. struct resource_entry *window;
  351. unsigned int ranges_sz = 0;
  352. unsigned int n_range = 0;
  353. struct resource *res;
  354. int n_addr_cells;
  355. u32 *ranges;
  356. u64 val64;
  357. u32 flags;
  358. int ret;
  359. n_addr_cells = of_n_addr_cells(np);
  360. if (n_addr_cells <= 0 || n_addr_cells > 2)
  361. return -EINVAL;
  362. resource_list_for_each_entry(window, &bridge->windows) {
  363. res = window->res;
  364. if (!of_pci_is_range_resource(res, &flags))
  365. continue;
  366. n_range++;
  367. }
  368. if (!n_range)
  369. return 0;
  370. ranges = kcalloc(n_range,
  371. (OF_PCI_ADDRESS_CELLS + OF_PCI_SIZE_CELLS +
  372. n_addr_cells) * sizeof(*ranges),
  373. GFP_KERNEL);
  374. if (!ranges)
  375. return -ENOMEM;
  376. resource_list_for_each_entry(window, &bridge->windows) {
  377. res = window->res;
  378. if (!of_pci_is_range_resource(res, &flags))
  379. continue;
  380. /* PCI bus address */
  381. val64 = res->start;
  382. of_pci_set_address(NULL, &ranges[ranges_sz],
  383. val64 - window->offset, 0, flags, false);
  384. ranges_sz += OF_PCI_ADDRESS_CELLS;
  385. /* Host bus address */
  386. if (n_addr_cells == 2)
  387. ranges[ranges_sz++] = upper_32_bits(val64);
  388. ranges[ranges_sz++] = lower_32_bits(val64);
  389. /* Size */
  390. val64 = resource_size(res);
  391. ranges[ranges_sz] = upper_32_bits(val64);
  392. ranges[ranges_sz + 1] = lower_32_bits(val64);
  393. ranges_sz += OF_PCI_SIZE_CELLS;
  394. }
  395. ret = of_changeset_add_prop_u32_array(ocs, np, "ranges", ranges,
  396. ranges_sz);
  397. kfree(ranges);
  398. return ret;
  399. }
  400. int of_pci_add_host_bridge_properties(struct pci_host_bridge *bridge,
  401. struct of_changeset *ocs,
  402. struct device_node *np)
  403. {
  404. int ret;
  405. ret = of_changeset_add_prop_string(ocs, np, "device_type", "pci");
  406. if (ret)
  407. return ret;
  408. ret = of_changeset_add_prop_u32(ocs, np, "#address-cells",
  409. OF_PCI_ADDRESS_CELLS);
  410. if (ret)
  411. return ret;
  412. ret = of_changeset_add_prop_u32(ocs, np, "#size-cells",
  413. OF_PCI_SIZE_CELLS);
  414. if (ret)
  415. return ret;
  416. ret = of_pci_host_bridge_prop_ranges(bridge, ocs, np);
  417. if (ret)
  418. return ret;
  419. return 0;
  420. }