of_property.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 {
  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. prop[0] = FIELD_PREP(OF_PCI_ADDR_FIELD_BUS, pdev->bus->number) |
  50. FIELD_PREP(OF_PCI_ADDR_FIELD_DEV, PCI_SLOT(pdev->devfn)) |
  51. FIELD_PREP(OF_PCI_ADDR_FIELD_FUNC, PCI_FUNC(pdev->devfn));
  52. prop[0] |= flags | reg_num;
  53. if (!reloc) {
  54. prop[0] |= OF_PCI_ADDR_FIELD_NONRELOC;
  55. prop[1] = upper_32_bits(addr);
  56. prop[2] = lower_32_bits(addr);
  57. }
  58. }
  59. static int of_pci_get_addr_flags(struct resource *res, u32 *flags)
  60. {
  61. u32 ss;
  62. if (res->flags & IORESOURCE_IO)
  63. ss = OF_PCI_ADDR_SPACE_IO;
  64. else if (res->flags & IORESOURCE_MEM_64)
  65. ss = OF_PCI_ADDR_SPACE_MEM64;
  66. else if (res->flags & IORESOURCE_MEM)
  67. ss = OF_PCI_ADDR_SPACE_MEM32;
  68. else
  69. return -EINVAL;
  70. *flags = 0;
  71. if (res->flags & IORESOURCE_PREFETCH)
  72. *flags |= OF_PCI_ADDR_FIELD_PREFETCH;
  73. *flags |= FIELD_PREP(OF_PCI_ADDR_FIELD_SS, ss);
  74. return 0;
  75. }
  76. static int of_pci_prop_bus_range(struct pci_dev *pdev,
  77. struct of_changeset *ocs,
  78. struct device_node *np)
  79. {
  80. u32 bus_range[] = { pdev->subordinate->busn_res.start,
  81. pdev->subordinate->busn_res.end };
  82. return of_changeset_add_prop_u32_array(ocs, np, "bus-range", bus_range,
  83. ARRAY_SIZE(bus_range));
  84. }
  85. static int of_pci_prop_ranges(struct pci_dev *pdev, struct of_changeset *ocs,
  86. struct device_node *np)
  87. {
  88. struct of_pci_range *rp;
  89. struct resource *res;
  90. int i, j, ret;
  91. u32 flags, num;
  92. u64 val64;
  93. if (pci_is_bridge(pdev)) {
  94. num = PCI_BRIDGE_RESOURCE_NUM;
  95. res = &pdev->resource[PCI_BRIDGE_RESOURCES];
  96. } else {
  97. num = PCI_STD_NUM_BARS;
  98. res = &pdev->resource[PCI_STD_RESOURCES];
  99. }
  100. rp = kcalloc(num, sizeof(*rp), GFP_KERNEL);
  101. if (!rp)
  102. return -ENOMEM;
  103. for (i = 0, j = 0; j < num; j++) {
  104. if (!resource_size(&res[j]))
  105. continue;
  106. if (of_pci_get_addr_flags(&res[j], &flags))
  107. continue;
  108. val64 = pci_bus_address(pdev, &res[j] - pdev->resource);
  109. of_pci_set_address(pdev, rp[i].parent_addr, val64, 0, flags,
  110. false);
  111. if (pci_is_bridge(pdev)) {
  112. memcpy(rp[i].child_addr, rp[i].parent_addr,
  113. sizeof(rp[i].child_addr));
  114. } else {
  115. /*
  116. * For endpoint device, the lower 64-bits of child
  117. * address is always zero.
  118. */
  119. rp[i].child_addr[0] = j;
  120. }
  121. val64 = resource_size(&res[j]);
  122. rp[i].size[0] = upper_32_bits(val64);
  123. rp[i].size[1] = lower_32_bits(val64);
  124. i++;
  125. }
  126. ret = of_changeset_add_prop_u32_array(ocs, np, "ranges", (u32 *)rp,
  127. i * sizeof(*rp) / sizeof(u32));
  128. kfree(rp);
  129. return ret;
  130. }
  131. static int of_pci_prop_reg(struct pci_dev *pdev, struct of_changeset *ocs,
  132. struct device_node *np)
  133. {
  134. struct of_pci_addr_pair reg = { 0 };
  135. /* configuration space */
  136. of_pci_set_address(pdev, reg.phys_addr, 0, 0, 0, true);
  137. return of_changeset_add_prop_u32_array(ocs, np, "reg", (u32 *)&reg,
  138. sizeof(reg) / sizeof(u32));
  139. }
  140. static int of_pci_prop_interrupts(struct pci_dev *pdev,
  141. struct of_changeset *ocs,
  142. struct device_node *np)
  143. {
  144. int ret;
  145. u8 pin;
  146. ret = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
  147. if (ret != 0)
  148. return ret;
  149. if (!pin)
  150. return 0;
  151. return of_changeset_add_prop_u32(ocs, np, "interrupts", (u32)pin);
  152. }
  153. static int of_pci_prop_intr_ctrl(struct pci_dev *pdev, struct of_changeset *ocs,
  154. struct device_node *np)
  155. {
  156. int ret;
  157. u8 pin;
  158. ret = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
  159. if (ret != 0)
  160. return ret;
  161. if (!pin)
  162. return 0;
  163. ret = of_changeset_add_prop_u32(ocs, np, "#interrupt-cells", 1);
  164. if (ret)
  165. return ret;
  166. return of_changeset_add_prop_bool(ocs, np, "interrupt-controller");
  167. }
  168. static int of_pci_prop_intr_map(struct pci_dev *pdev, struct of_changeset *ocs,
  169. struct device_node *np)
  170. {
  171. u32 i, addr_sz[OF_PCI_MAX_INT_PIN] = { 0 }, map_sz = 0;
  172. struct of_phandle_args out_irq[OF_PCI_MAX_INT_PIN];
  173. __be32 laddr[OF_PCI_ADDRESS_CELLS] = { 0 };
  174. u32 int_map_mask[] = { 0xffff00, 0, 0, 7 };
  175. struct device_node *pnode;
  176. struct pci_dev *child;
  177. u32 *int_map, *mapp;
  178. int ret;
  179. u8 pin;
  180. pnode = pci_device_to_OF_node(pdev->bus->self);
  181. if (!pnode)
  182. pnode = pci_bus_to_OF_node(pdev->bus);
  183. if (!pnode) {
  184. pci_err(pdev, "failed to get parent device node");
  185. return -EINVAL;
  186. }
  187. laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8));
  188. for (pin = 1; pin <= OF_PCI_MAX_INT_PIN; pin++) {
  189. i = pin - 1;
  190. out_irq[i].np = pnode;
  191. out_irq[i].args_count = 1;
  192. out_irq[i].args[0] = pin;
  193. ret = of_irq_parse_raw(laddr, &out_irq[i]);
  194. if (ret) {
  195. out_irq[i].np = NULL;
  196. pci_dbg(pdev, "parse irq %d failed, ret %d", pin, ret);
  197. continue;
  198. }
  199. of_property_read_u32(out_irq[i].np, "#address-cells",
  200. &addr_sz[i]);
  201. }
  202. list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
  203. for (pin = 1; pin <= OF_PCI_MAX_INT_PIN; pin++) {
  204. i = pci_swizzle_interrupt_pin(child, pin) - 1;
  205. if (!out_irq[i].np)
  206. continue;
  207. map_sz += 5 + addr_sz[i] + out_irq[i].args_count;
  208. }
  209. }
  210. /*
  211. * Parsing interrupt failed for all pins. In this case, it does not
  212. * need to generate interrupt-map property.
  213. */
  214. if (!map_sz)
  215. return 0;
  216. int_map = kcalloc(map_sz, sizeof(u32), GFP_KERNEL);
  217. if (!int_map)
  218. return -ENOMEM;
  219. mapp = int_map;
  220. list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
  221. for (pin = 1; pin <= OF_PCI_MAX_INT_PIN; pin++) {
  222. i = pci_swizzle_interrupt_pin(child, pin) - 1;
  223. if (!out_irq[i].np)
  224. continue;
  225. *mapp = (child->bus->number << 16) |
  226. (child->devfn << 8);
  227. mapp += OF_PCI_ADDRESS_CELLS;
  228. *mapp = pin;
  229. mapp++;
  230. *mapp = out_irq[i].np->phandle;
  231. mapp++;
  232. if (addr_sz[i]) {
  233. ret = of_property_read_u32_array(out_irq[i].np,
  234. "reg", mapp,
  235. addr_sz[i]);
  236. if (ret)
  237. goto failed;
  238. }
  239. mapp += addr_sz[i];
  240. memcpy(mapp, out_irq[i].args,
  241. out_irq[i].args_count * sizeof(u32));
  242. mapp += out_irq[i].args_count;
  243. }
  244. }
  245. ret = of_changeset_add_prop_u32_array(ocs, np, "interrupt-map", int_map,
  246. map_sz);
  247. if (ret)
  248. goto failed;
  249. ret = of_changeset_add_prop_u32(ocs, np, "#interrupt-cells", 1);
  250. if (ret)
  251. goto failed;
  252. ret = of_changeset_add_prop_u32_array(ocs, np, "interrupt-map-mask",
  253. int_map_mask,
  254. ARRAY_SIZE(int_map_mask));
  255. if (ret)
  256. goto failed;
  257. kfree(int_map);
  258. return 0;
  259. failed:
  260. kfree(int_map);
  261. return ret;
  262. }
  263. static int of_pci_prop_compatible(struct pci_dev *pdev,
  264. struct of_changeset *ocs,
  265. struct device_node *np)
  266. {
  267. const char *compat_strs[PROP_COMPAT_NUM] = { 0 };
  268. int i, ret;
  269. compat_strs[PROP_COMPAT_PCI_VVVV_DDDD] =
  270. kasprintf(GFP_KERNEL, "pci%x,%x", pdev->vendor, pdev->device);
  271. compat_strs[PROP_COMPAT_PCICLASS_CCSSPP] =
  272. kasprintf(GFP_KERNEL, "pciclass,%06x", pdev->class);
  273. compat_strs[PROP_COMPAT_PCICLASS_CCSS] =
  274. kasprintf(GFP_KERNEL, "pciclass,%04x", pdev->class >> 8);
  275. ret = of_changeset_add_prop_string_array(ocs, np, "compatible",
  276. compat_strs, PROP_COMPAT_NUM);
  277. for (i = 0; i < PROP_COMPAT_NUM; i++)
  278. kfree(compat_strs[i]);
  279. return ret;
  280. }
  281. int of_pci_add_properties(struct pci_dev *pdev, struct of_changeset *ocs,
  282. struct device_node *np)
  283. {
  284. int ret;
  285. /*
  286. * The added properties will be released when the
  287. * changeset is destroyed.
  288. */
  289. if (pci_is_bridge(pdev)) {
  290. ret = of_changeset_add_prop_string(ocs, np, "device_type",
  291. "pci");
  292. if (ret)
  293. return ret;
  294. ret = of_pci_prop_bus_range(pdev, ocs, np);
  295. if (ret)
  296. return ret;
  297. ret = of_pci_prop_intr_map(pdev, ocs, np);
  298. if (ret)
  299. return ret;
  300. } else {
  301. ret = of_pci_prop_intr_ctrl(pdev, ocs, np);
  302. if (ret)
  303. return ret;
  304. }
  305. ret = of_pci_prop_ranges(pdev, ocs, np);
  306. if (ret)
  307. return ret;
  308. ret = of_changeset_add_prop_u32(ocs, np, "#address-cells",
  309. OF_PCI_ADDRESS_CELLS);
  310. if (ret)
  311. return ret;
  312. ret = of_changeset_add_prop_u32(ocs, np, "#size-cells",
  313. OF_PCI_SIZE_CELLS);
  314. if (ret)
  315. return ret;
  316. ret = of_pci_prop_reg(pdev, ocs, np);
  317. if (ret)
  318. return ret;
  319. ret = of_pci_prop_compatible(pdev, ocs, np);
  320. if (ret)
  321. return ret;
  322. ret = of_pci_prop_interrupts(pdev, ocs, np);
  323. if (ret)
  324. return ret;
  325. return 0;
  326. }