|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Synopsys DesignWare PCIe controller debugfs driver |
| 4 | + * |
| 5 | + * Copyright (C) 2025 Samsung Electronics Co., Ltd. |
| 6 | + * http://www.samsung.com |
| 7 | + * |
| 8 | + * Author: Shradha Todi <shradha.t@samsung.com> |
| 9 | + */ |
| 10 | + |
| 11 | +#include <linux/debugfs.h> |
| 12 | + |
| 13 | +#include "pcie-designware.h" |
| 14 | + |
| 15 | +#define SD_STATUS_L1LANE_REG 0xb0 |
| 16 | +#define PIPE_RXVALID BIT(18) |
| 17 | +#define PIPE_DETECT_LANE BIT(17) |
| 18 | +#define LANE_SELECT GENMASK(3, 0) |
| 19 | + |
| 20 | +#define DWC_DEBUGFS_BUF_MAX 128 |
| 21 | + |
| 22 | +/** |
| 23 | + * struct dwc_pcie_rasdes_info - Stores controller common information |
| 24 | + * @ras_cap_offset: RAS DES vendor specific extended capability offset |
| 25 | + * @reg_event_lock: Mutex used for RAS DES shadow event registers |
| 26 | + * |
| 27 | + * Any parameter constant to all files of the debugfs hierarchy for a single |
| 28 | + * controller will be stored in this struct. It is allocated and assigned to |
| 29 | + * controller specific struct dw_pcie during initialization. |
| 30 | + */ |
| 31 | +struct dwc_pcie_rasdes_info { |
| 32 | + u32 ras_cap_offset; |
| 33 | + struct mutex reg_event_lock; |
| 34 | +}; |
| 35 | + |
| 36 | +static ssize_t lane_detect_read(struct file *file, char __user *buf, |
| 37 | + size_t count, loff_t *ppos) |
| 38 | +{ |
| 39 | + struct dw_pcie *pci = file->private_data; |
| 40 | + struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info; |
| 41 | + char debugfs_buf[DWC_DEBUGFS_BUF_MAX]; |
| 42 | + ssize_t pos; |
| 43 | + u32 val; |
| 44 | + |
| 45 | + val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + SD_STATUS_L1LANE_REG); |
| 46 | + val = FIELD_GET(PIPE_DETECT_LANE, val); |
| 47 | + if (val) |
| 48 | + pos = scnprintf(debugfs_buf, DWC_DEBUGFS_BUF_MAX, "Lane Detected\n"); |
| 49 | + else |
| 50 | + pos = scnprintf(debugfs_buf, DWC_DEBUGFS_BUF_MAX, "Lane Undetected\n"); |
| 51 | + |
| 52 | + return simple_read_from_buffer(buf, count, ppos, debugfs_buf, pos); |
| 53 | +} |
| 54 | + |
| 55 | +static ssize_t lane_detect_write(struct file *file, const char __user *buf, |
| 56 | + size_t count, loff_t *ppos) |
| 57 | +{ |
| 58 | + struct dw_pcie *pci = file->private_data; |
| 59 | + struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info; |
| 60 | + u32 lane, val; |
| 61 | + |
| 62 | + val = kstrtou32_from_user(buf, count, 0, &lane); |
| 63 | + if (val) |
| 64 | + return val; |
| 65 | + |
| 66 | + val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + SD_STATUS_L1LANE_REG); |
| 67 | + val &= ~(LANE_SELECT); |
| 68 | + val |= FIELD_PREP(LANE_SELECT, lane); |
| 69 | + dw_pcie_writel_dbi(pci, rinfo->ras_cap_offset + SD_STATUS_L1LANE_REG, val); |
| 70 | + |
| 71 | + return count; |
| 72 | +} |
| 73 | + |
| 74 | +static ssize_t rx_valid_read(struct file *file, char __user *buf, |
| 75 | + size_t count, loff_t *ppos) |
| 76 | +{ |
| 77 | + struct dw_pcie *pci = file->private_data; |
| 78 | + struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info; |
| 79 | + char debugfs_buf[DWC_DEBUGFS_BUF_MAX]; |
| 80 | + ssize_t pos; |
| 81 | + u32 val; |
| 82 | + |
| 83 | + val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + SD_STATUS_L1LANE_REG); |
| 84 | + val = FIELD_GET(PIPE_RXVALID, val); |
| 85 | + if (val) |
| 86 | + pos = scnprintf(debugfs_buf, DWC_DEBUGFS_BUF_MAX, "RX Valid\n"); |
| 87 | + else |
| 88 | + pos = scnprintf(debugfs_buf, DWC_DEBUGFS_BUF_MAX, "RX Invalid\n"); |
| 89 | + |
| 90 | + return simple_read_from_buffer(buf, count, ppos, debugfs_buf, pos); |
| 91 | +} |
| 92 | + |
| 93 | +static ssize_t rx_valid_write(struct file *file, const char __user *buf, |
| 94 | + size_t count, loff_t *ppos) |
| 95 | +{ |
| 96 | + return lane_detect_write(file, buf, count, ppos); |
| 97 | +} |
| 98 | + |
| 99 | +#define dwc_debugfs_create(name) \ |
| 100 | +debugfs_create_file(#name, 0644, rasdes_debug, pci, \ |
| 101 | + &dbg_ ## name ## _fops) |
| 102 | + |
| 103 | +#define DWC_DEBUGFS_FOPS(name) \ |
| 104 | +static const struct file_operations dbg_ ## name ## _fops = { \ |
| 105 | + .open = simple_open, \ |
| 106 | + .read = name ## _read, \ |
| 107 | + .write = name ## _write \ |
| 108 | +} |
| 109 | + |
| 110 | +DWC_DEBUGFS_FOPS(lane_detect); |
| 111 | +DWC_DEBUGFS_FOPS(rx_valid); |
| 112 | + |
| 113 | +static void dwc_pcie_rasdes_debugfs_deinit(struct dw_pcie *pci) |
| 114 | +{ |
| 115 | + struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info; |
| 116 | + |
| 117 | + mutex_destroy(&rinfo->reg_event_lock); |
| 118 | +} |
| 119 | + |
| 120 | +static int dwc_pcie_rasdes_debugfs_init(struct dw_pcie *pci, struct dentry *dir) |
| 121 | +{ |
| 122 | + struct dentry *rasdes_debug; |
| 123 | + struct dwc_pcie_rasdes_info *rasdes_info; |
| 124 | + struct device *dev = pci->dev; |
| 125 | + int ras_cap; |
| 126 | + |
| 127 | + /* |
| 128 | + * If a given SoC has no RAS DES capability, the following call is |
| 129 | + * bound to return an error, breaking some existing platforms. So, |
| 130 | + * return 0 here, as this is not necessarily an error. |
| 131 | + */ |
| 132 | + ras_cap = dw_pcie_find_rasdes_capability(pci); |
| 133 | + if (!ras_cap) { |
| 134 | + dev_dbg(dev, "no RAS DES capability available\n"); |
| 135 | + return 0; |
| 136 | + } |
| 137 | + |
| 138 | + rasdes_info = devm_kzalloc(dev, sizeof(*rasdes_info), GFP_KERNEL); |
| 139 | + if (!rasdes_info) |
| 140 | + return -ENOMEM; |
| 141 | + |
| 142 | + /* Create subdirectories for Debug, Error Injection, Statistics. */ |
| 143 | + rasdes_debug = debugfs_create_dir("rasdes_debug", dir); |
| 144 | + |
| 145 | + mutex_init(&rasdes_info->reg_event_lock); |
| 146 | + rasdes_info->ras_cap_offset = ras_cap; |
| 147 | + pci->debugfs->rasdes_info = rasdes_info; |
| 148 | + |
| 149 | + /* Create debugfs files for Debug subdirectory. */ |
| 150 | + dwc_debugfs_create(lane_detect); |
| 151 | + dwc_debugfs_create(rx_valid); |
| 152 | + |
| 153 | + return 0; |
| 154 | +} |
| 155 | + |
| 156 | +void dwc_pcie_debugfs_deinit(struct dw_pcie *pci) |
| 157 | +{ |
| 158 | + if (!pci->debugfs) |
| 159 | + return; |
| 160 | + |
| 161 | + dwc_pcie_rasdes_debugfs_deinit(pci); |
| 162 | + debugfs_remove_recursive(pci->debugfs->debug_dir); |
| 163 | +} |
| 164 | + |
| 165 | +void dwc_pcie_debugfs_init(struct dw_pcie *pci) |
| 166 | +{ |
| 167 | + char dirname[DWC_DEBUGFS_BUF_MAX]; |
| 168 | + struct device *dev = pci->dev; |
| 169 | + struct debugfs_info *debugfs; |
| 170 | + struct dentry *dir; |
| 171 | + int err; |
| 172 | + |
| 173 | + /* Create main directory for each platform driver. */ |
| 174 | + snprintf(dirname, DWC_DEBUGFS_BUF_MAX, "dwc_pcie_%s", dev_name(dev)); |
| 175 | + dir = debugfs_create_dir(dirname, NULL); |
| 176 | + debugfs = devm_kzalloc(dev, sizeof(*debugfs), GFP_KERNEL); |
| 177 | + if (!debugfs) |
| 178 | + return; |
| 179 | + |
| 180 | + debugfs->debug_dir = dir; |
| 181 | + pci->debugfs = debugfs; |
| 182 | + err = dwc_pcie_rasdes_debugfs_init(pci, dir); |
| 183 | + if (err) |
| 184 | + dev_err(dev, "failed to initialize RAS DES debugfs, err=%d\n", |
| 185 | + err); |
| 186 | +} |
0 commit comments