xref: /aosp_15_r20/external/coreboot/src/soc/amd/common/fsp/pci/pcie_clk_req.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include <amdblocks/pci_clk_req.h>
4 #include <console/console.h>
5 #include <device/device.h>
6 #include <device/pci.h>
7 #include <device/pci_ids.h>
8 #include <soc/platform_descriptors.h>
9 
10 /* Update gpp clk req config based on DXIO descriptors and enabled devices. */
pcie_gpp_dxio_update_clk_req_config(enum gpp_clk_req * gpp_clk_config,size_t gpp_clk_config_num)11 void pcie_gpp_dxio_update_clk_req_config(enum gpp_clk_req *gpp_clk_config,
12 					 size_t gpp_clk_config_num)
13 {
14 	const fsp_dxio_descriptor *dxio_descs = NULL;
15 	const fsp_ddi_descriptor *ddi_descs = NULL;
16 	size_t dxio_num = 0;
17 	size_t ddi_num = 0;
18 
19 	mainboard_get_dxio_ddi_descriptors(&dxio_descs, &dxio_num, &ddi_descs, &ddi_num);
20 	if (dxio_descs == NULL) {
21 		printk(BIOS_WARNING,
22 		       "No DXIO descriptors found, GPP clk req may not reflect enabled devices\n");
23 		return;
24 	}
25 
26 	for (int i = 0; i < dxio_num; i++) {
27 		const fsp_dxio_descriptor *dxio_desc = &dxio_descs[i];
28 
29 		/*  Only consider PCIe and unused engine types. */
30 		if (dxio_desc->engine_type != PCIE_ENGINE
31 		    && dxio_desc->engine_type != UNUSED_ENGINE)
32 			continue;
33 		enum cpm_clk_req dxio_clk_req = dxio_desc->clk_req;
34 
35 		/* CLK_DISABLE means there's no corresponding clk req line in use */
36 		if (dxio_clk_req == CLK_DISABLE)
37 			continue;
38 
39 		/*
40 		 * dxio_clk_req is only 4 bits so having CLK_ENABLE as a value for
41 		 * a descriptor should cause a compiler error. 0xF isn't a
42 		 * valid clk_req value according to AMD's internal code either.
43 		 * This is here to draw attention in case this code is ever used
44 		 * in a situation where this has changed.
45 		 */
46 		if (dxio_clk_req == (CLK_ENABLE & 0xF)) {
47 			printk(BIOS_WARNING,
48 			       "CLK_ENABLE is an invalid clk_req value for PCIe device %d.%d, DXIO descriptor %d\n",
49 			       dxio_desc->device_number, dxio_desc->function_number, i);
50 			continue;
51 		}
52 
53 		/* cpm_clk_req 0 is CLK_DISABLE */
54 		int gpp_req_index = dxio_clk_req - CLK_REQ0;
55 		/* Ensure that our index is valid */
56 		if (gpp_req_index < 0 || gpp_req_index >= gpp_clk_config_num) {
57 			printk(BIOS_ERR,
58 			       "Failed to convert DXIO clk req value %d to GPP clk req index for PCIe device %d.%d, DXIO descriptor %d, clk req settings may be incorrect\n",
59 			       dxio_clk_req, dxio_desc->device_number,
60 			       dxio_desc->function_number, i);
61 			continue;
62 		}
63 
64 		const struct device *pci_device = pcidev_path_on_root(
65 			PCI_DEVFN(dxio_desc->device_number, dxio_desc->function_number));
66 		if (pci_device == NULL) {
67 			gpp_clk_config[gpp_req_index] = GPP_CLK_OFF;
68 			printk(BIOS_WARNING,
69 			       "Cannot find PCIe device %d.%d, disabling GPP clk req %d, DXIO descriptor %d\n",
70 			       dxio_desc->device_number, dxio_desc->function_number, i,
71 			       gpp_req_index);
72 			continue;
73 		}
74 
75 		/* PCIe devices haven't been fully set up yet, so directly read the vendor id
76 		 * and device id to determine if a device is physically present. If a device
77 		 * is not present then the id should be 0xffffffff. 0x00000000, 0xffff0000,
78 		 * and 0x0000ffff are there to account for any odd failure cases. */
79 		u32 id = pci_read_config32(pci_device, PCI_VENDOR_ID);
80 		bool enabled = pci_device->enabled && (id != 0xffffffff) && (id != 0x00000000)
81 			       && (id != 0x0000ffff) && (id != 0xffff0000);
82 
83 		/* Inform of possible mismatches between devices and SoC gpp_clk_config. */
84 		if (!enabled && gpp_clk_config[gpp_req_index] != GPP_CLK_OFF) {
85 			gpp_clk_config[gpp_req_index] = GPP_CLK_OFF;
86 			printk(BIOS_INFO,
87 			       "PCIe device %d.%d disabled, disabling GPP clk req %d, DXIO descriptor %d\n",
88 			       dxio_desc->device_number, dxio_desc->function_number,
89 			       gpp_req_index, i);
90 		} else if (enabled && gpp_clk_config[gpp_req_index] == GPP_CLK_OFF) {
91 			printk(BIOS_INFO,
92 			       "PCIe device %d.%d enabled, GPP clk req is off, DXIO descriptor %d\n",
93 			       dxio_desc->device_number, dxio_desc->function_number, i);
94 		}
95 	}
96 }
97