Debian Codegraph @

drivers/pci/controller/dwc/pcie-designware-debugfs.c

raw
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 <[email protected]>
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 ERR_INJ0_OFF 0x34
21 #define EINJ_VAL_DIFF GENMASK(28, 16)
22 #define EINJ_VC_NUM GENMASK(14, 12)
23 #define EINJ_TYPE_SHIFT 8
24 #define EINJ0_TYPE GENMASK(11, 8)
25 #define EINJ1_TYPE BIT(8)
26 #define EINJ2_TYPE GENMASK(9, 8)
27 #define EINJ3_TYPE GENMASK(10, 8)
28 #define EINJ4_TYPE GENMASK(10, 8)
29 #define EINJ5_TYPE BIT(8)
30 #define EINJ_COUNT GENMASK(7, 0)
31
32 #define ERR_INJ_ENABLE_REG 0x30
33
34 #define RAS_DES_EVENT_COUNTER_DATA_REG 0xc
35
36 #define RAS_DES_EVENT_COUNTER_CTRL_REG 0x8
37 #define EVENT_COUNTER_GROUP_SELECT GENMASK(27, 24)
38 #define EVENT_COUNTER_EVENT_SELECT GENMASK(23, 16)
39 #define EVENT_COUNTER_LANE_SELECT GENMASK(11, 8)
40 #define EVENT_COUNTER_STATUS BIT(7)
41 #define EVENT_COUNTER_ENABLE GENMASK(4, 2)
42 #define PER_EVENT_ON 0x3
43 #define PER_EVENT_OFF 0x1
44
45 #define DWC_DEBUGFS_BUF_MAX 128
46
47 /**
48 * struct dwc_pcie_rasdes_info - Stores controller common information
49 * @ras_cap_offset: RAS DES vendor specific extended capability offset
50 * @reg_event_lock: Mutex used for RAS DES shadow event registers
51 *
52 * Any parameter constant to all files of the debugfs hierarchy for a single
53 * controller will be stored in this struct. It is allocated and assigned to
54 * controller specific struct dw_pcie during initialization.
55 */
56 struct dwc_pcie_rasdes_info {
57 u32 ras_cap_offset;
58 struct mutex reg_event_lock;
59 };
60
61 /**
62 * struct dwc_pcie_rasdes_priv - Stores file specific private data information
63 * @pci: Reference to the dw_pcie structure
64 * @idx: Index of specific file related information in array of structs
65 *
66 * All debugfs files will have this struct as its private data.
67 */
68 struct dwc_pcie_rasdes_priv {
69 struct dw_pcie *pci;
70 int idx;
71 };
72
73 /**
74 * struct dwc_pcie_err_inj - Store details about each error injection
75 * supported by DWC RAS DES
76 * @name: Name of the error that can be injected
77 * @err_inj_group: Group number to which the error belongs. The value
78 * can range from 0 to 5
79 * @err_inj_type: Each group can have multiple types of error
80 */
81 struct dwc_pcie_err_inj {
82 const char *name;
83 u32 err_inj_group;
84 u32 err_inj_type;
85 };
86
87 static const struct dwc_pcie_err_inj err_inj_list[] = {
88 {"tx_lcrc", 0x0, 0x0},
89 {"b16_crc_dllp", 0x0, 0x1},
90 {"b16_crc_upd_fc", 0x0, 0x2},
91 {"tx_ecrc", 0x0, 0x3},
92 {"fcrc_tlp", 0x0, 0x4},
93 {"parity_tsos", 0x0, 0x5},
94 {"parity_skpos", 0x0, 0x6},
95 {"rx_lcrc", 0x0, 0x8},
96 {"rx_ecrc", 0x0, 0xb},
97 {"tlp_err_seq", 0x1, 0x0},
98 {"ack_nak_dllp_seq", 0x1, 0x1},
99 {"ack_nak_dllp", 0x2, 0x0},
100 {"upd_fc_dllp", 0x2, 0x1},
101 {"nak_dllp", 0x2, 0x2},
102 {"inv_sync_hdr_sym", 0x3, 0x0},
103 {"com_pad_ts1", 0x3, 0x1},
104 {"com_pad_ts2", 0x3, 0x2},
105 {"com_fts", 0x3, 0x3},
106 {"com_idl", 0x3, 0x4},
107 {"end_edb", 0x3, 0x5},
108 {"stp_sdp", 0x3, 0x6},
109 {"com_skp", 0x3, 0x7},
110 {"posted_tlp_hdr", 0x4, 0x0},
111 {"non_post_tlp_hdr", 0x4, 0x1},
112 {"cmpl_tlp_hdr", 0x4, 0x2},
113 {"posted_tlp_data", 0x4, 0x4},
114 {"non_post_tlp_data", 0x4, 0x5},
115 {"cmpl_tlp_data", 0x4, 0x6},
116 {"duplicate_tlp", 0x5, 0x0},
117 {"nullified_tlp", 0x5, 0x1},
118 };
119
120 static const u32 err_inj_type_mask[] = {
121 EINJ0_TYPE,
122 EINJ1_TYPE,
123 EINJ2_TYPE,
124 EINJ3_TYPE,
125 EINJ4_TYPE,
126 EINJ5_TYPE,
127 };
128
129 /**
130 * struct dwc_pcie_event_counter - Store details about each event counter
131 * supported in DWC RAS DES
132 * @name: Name of the error counter
133 * @group_no: Group number that the event belongs to. The value can range
134 * from 0 to 4
135 * @event_no: Event number of the particular event. The value ranges are:
136 * Group 0: 0 - 10
137 * Group 1: 5 - 13
138 * Group 2: 0 - 7
139 * Group 3: 0 - 5
140 * Group 4: 0 - 1
141 */
142 struct dwc_pcie_event_counter {
143 const char *name;
144 u32 group_no;
145 u32 event_no;
146 };
147
148 static const struct dwc_pcie_event_counter event_list[] = {
149 {"ebuf_overflow", 0x0, 0x0},
150 {"ebuf_underrun", 0x0, 0x1},
151 {"decode_err", 0x0, 0x2},
152 {"running_disparity_err", 0x0, 0x3},
153 {"skp_os_parity_err", 0x0, 0x4},
154 {"sync_header_err", 0x0, 0x5},
155 {"rx_valid_deassertion", 0x0, 0x6},
156 {"ctl_skp_os_parity_err", 0x0, 0x7},
157 {"retimer_parity_err_1st", 0x0, 0x8},
158 {"retimer_parity_err_2nd", 0x0, 0x9},
159 {"margin_crc_parity_err", 0x0, 0xA},
160 {"detect_ei_infer", 0x1, 0x5},
161 {"receiver_err", 0x1, 0x6},
162 {"rx_recovery_req", 0x1, 0x7},
163 {"n_fts_timeout", 0x1, 0x8},
164 {"framing_err", 0x1, 0x9},
165 {"deskew_err", 0x1, 0xa},
166 {"framing_err_in_l0", 0x1, 0xc},
167 {"deskew_uncompleted_err", 0x1, 0xd},
168 {"bad_tlp", 0x2, 0x0},
169 {"lcrc_err", 0x2, 0x1},
170 {"bad_dllp", 0x2, 0x2},
171 {"replay_num_rollover", 0x2, 0x3},
172 {"replay_timeout", 0x2, 0x4},
173 {"rx_nak_dllp", 0x2, 0x5},
174 {"tx_nak_dllp", 0x2, 0x6},
175 {"retry_tlp", 0x2, 0x7},
176 {"fc_timeout", 0x3, 0x0},
177 {"poisoned_tlp", 0x3, 0x1},
178 {"ecrc_error", 0x3, 0x2},
179 {"unsupported_request", 0x3, 0x3},
180 {"completer_abort", 0x3, 0x4},
181 {"completion_timeout", 0x3, 0x5},
182 {"ebuf_skp_add", 0x4, 0x0},
183 {"ebuf_skp_del", 0x4, 0x1},
184 };
185
186 static ssize_t lane_detect_read(struct file *file, char __user *buf,
187 size_t count, loff_t *ppos)
188 {
189 struct dw_pcie *pci = file->private_data;
190 struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info;
191 char debugfs_buf[DWC_DEBUGFS_BUF_MAX];
192 ssize_t pos;
193 u32 val;
194
195 val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + SD_STATUS_L1LANE_REG);
196 val = FIELD_GET(PIPE_DETECT_LANE, val);
197 if (val)
198 pos = scnprintf(debugfs_buf, DWC_DEBUGFS_BUF_MAX, "Lane Detected\n");
199 else
200 pos = scnprintf(debugfs_buf, DWC_DEBUGFS_BUF_MAX, "Lane Undetected\n");
201
202 return simple_read_from_buffer(buf, count, ppos, debugfs_buf, pos);
203 }
204
205 static ssize_t lane_detect_write(struct file *file, const char __user *buf,
206 size_t count, loff_t *ppos)
207 {
208 struct dw_pcie *pci = file->private_data;
209 struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info;
210 u32 lane, val;
211 int ret;
212
213 ret = kstrtou32_from_user(buf, count, 0, &lane);
214 if (ret)
215 return ret;
216
217 val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + SD_STATUS_L1LANE_REG);
218 val &= ~(LANE_SELECT);
219 val |= FIELD_PREP(LANE_SELECT, lane);
220 dw_pcie_writel_dbi(pci, rinfo->ras_cap_offset + SD_STATUS_L1LANE_REG, val);
221
222 return count;
223 }
224
225 static ssize_t rx_valid_read(struct file *file, char __user *buf,
226 size_t count, loff_t *ppos)
227 {
228 struct dw_pcie *pci = file->private_data;
229 struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info;
230 char debugfs_buf[DWC_DEBUGFS_BUF_MAX];
231 ssize_t pos;
232 u32 val;
233
234 val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + SD_STATUS_L1LANE_REG);
235 val = FIELD_GET(PIPE_RXVALID, val);
236 if (val)
237 pos = scnprintf(debugfs_buf, DWC_DEBUGFS_BUF_MAX, "RX Valid\n");
238 else
239 pos = scnprintf(debugfs_buf, DWC_DEBUGFS_BUF_MAX, "RX Invalid\n");
240
241 return simple_read_from_buffer(buf, count, ppos, debugfs_buf, pos);
242 }
243
244 static ssize_t rx_valid_write(struct file *file, const char __user *buf,
245 size_t count, loff_t *ppos)
246 {
247 return lane_detect_write(file, buf, count, ppos);
248 }
249
250 static ssize_t err_inj_write(struct file *file, const char __user *buf,
251 size_t count, loff_t *ppos)
252 {
253 struct dwc_pcie_rasdes_priv *pdata = file->private_data;
254 struct dw_pcie *pci = pdata->pci;
255 struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info;
256 u32 val, counter, vc_num, err_group, type_mask;
257 int val_diff = 0;
258 char *kern_buf;
259
260 err_group = err_inj_list[pdata->idx].err_inj_group;
261 type_mask = err_inj_type_mask[err_group];
262
263 kern_buf = memdup_user_nul(buf, count);
264 if (IS_ERR(kern_buf))
265 return PTR_ERR(kern_buf);
266
267 if (err_group == 4) {
268 val = sscanf(kern_buf, "%u %d %u", &counter, &val_diff, &vc_num);
269 if ((val != 3) || (val_diff < -4095 || val_diff > 4095)) {
270 kfree(kern_buf);
271 return -EINVAL;
272 }
273 } else if (err_group == 1) {
274 val = sscanf(kern_buf, "%u %d", &counter, &val_diff);
275 if ((val != 2) || (val_diff < -4095 || val_diff > 4095)) {
276 kfree(kern_buf);
277 return -EINVAL;
278 }
279 } else {
280 val = kstrtou32(kern_buf, 0, &counter);
281 if (val) {
282 kfree(kern_buf);
283 return val;
284 }
285 }
286
287 val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + ERR_INJ0_OFF + (0x4 * err_group));
288 val &= ~(type_mask | EINJ_COUNT);
289 val |= ((err_inj_list[pdata->idx].err_inj_type << EINJ_TYPE_SHIFT) & type_mask);
290 val |= FIELD_PREP(EINJ_COUNT, counter);
291
292 if (err_group == 1 || err_group == 4) {
293 val &= ~(EINJ_VAL_DIFF);
294 val |= FIELD_PREP(EINJ_VAL_DIFF, val_diff);
295 }
296 if (err_group == 4) {
297 val &= ~(EINJ_VC_NUM);
298 val |= FIELD_PREP(EINJ_VC_NUM, vc_num);
299 }
300
301 dw_pcie_writel_dbi(pci, rinfo->ras_cap_offset + ERR_INJ0_OFF + (0x4 * err_group), val);
302 dw_pcie_writel_dbi(pci, rinfo->ras_cap_offset + ERR_INJ_ENABLE_REG, (0x1 << err_group));
303
304 kfree(kern_buf);
305 return count;
306 }
307
308 static void set_event_number(struct dwc_pcie_rasdes_priv *pdata,
309 struct dw_pcie *pci, struct dwc_pcie_rasdes_info *rinfo)
310 {
311 u32 val;
312
313 val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + RAS_DES_EVENT_COUNTER_CTRL_REG);
314 val &= ~EVENT_COUNTER_ENABLE;
315 val &= ~(EVENT_COUNTER_GROUP_SELECT | EVENT_COUNTER_EVENT_SELECT);
316 val |= FIELD_PREP(EVENT_COUNTER_GROUP_SELECT, event_list[pdata->idx].group_no);
317 val |= FIELD_PREP(EVENT_COUNTER_EVENT_SELECT, event_list[pdata->idx].event_no);
318 dw_pcie_writel_dbi(pci, rinfo->ras_cap_offset + RAS_DES_EVENT_COUNTER_CTRL_REG, val);
319 }
320
321 static ssize_t counter_enable_read(struct file *file, char __user *buf,
322 size_t count, loff_t *ppos)
323 {
324 struct dwc_pcie_rasdes_priv *pdata = file->private_data;
325 struct dw_pcie *pci = pdata->pci;
326 struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info;
327 char debugfs_buf[DWC_DEBUGFS_BUF_MAX];
328 ssize_t pos;
329 u32 val;
330
331 mutex_lock(&rinfo->reg_event_lock);
332 set_event_number(pdata, pci, rinfo);
333 val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + RAS_DES_EVENT_COUNTER_CTRL_REG);
334 mutex_unlock(&rinfo->reg_event_lock);
335 val = FIELD_GET(EVENT_COUNTER_STATUS, val);
336 if (val)
337 pos = scnprintf(debugfs_buf, DWC_DEBUGFS_BUF_MAX, "Counter Enabled\n");
338 else
339 pos = scnprintf(debugfs_buf, DWC_DEBUGFS_BUF_MAX, "Counter Disabled\n");
340
341 return simple_read_from_buffer(buf, count, ppos, debugfs_buf, pos);
342 }
343
344 static ssize_t counter_enable_write(struct file *file, const char __user *buf,
345 size_t count, loff_t *ppos)
346 {
347 struct dwc_pcie_rasdes_priv *pdata = file->private_data;
348 struct dw_pcie *pci = pdata->pci;
349 struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info;
350 u32 val, enable;
351 int ret;
352
353 ret = kstrtou32_from_user(buf, count, 0, &enable);
354 if (ret)
355 return ret;
356
357 mutex_lock(&rinfo->reg_event_lock);
358 set_event_number(pdata, pci, rinfo);
359 val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + RAS_DES_EVENT_COUNTER_CTRL_REG);
360 if (enable)
361 val |= FIELD_PREP(EVENT_COUNTER_ENABLE, PER_EVENT_ON);
362 else
363 val |= FIELD_PREP(EVENT_COUNTER_ENABLE, PER_EVENT_OFF);
364
365 dw_pcie_writel_dbi(pci, rinfo->ras_cap_offset + RAS_DES_EVENT_COUNTER_CTRL_REG, val);
366
367 /*
368 * While enabling the counter, always read the status back to check if
369 * it is enabled or not. Return error if it is not enabled to let the
370 * users know that the counter is not supported on the platform.
371 */
372 if (enable) {
373 val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset +
374 RAS_DES_EVENT_COUNTER_CTRL_REG);
375 if (!FIELD_GET(EVENT_COUNTER_STATUS, val)) {
376 mutex_unlock(&rinfo->reg_event_lock);
377 return -EOPNOTSUPP;
378 }
379 }
380
381 mutex_unlock(&rinfo->reg_event_lock);
382
383 return count;
384 }
385
386 static ssize_t counter_lane_read(struct file *file, char __user *buf,
387 size_t count, loff_t *ppos)
388 {
389 struct dwc_pcie_rasdes_priv *pdata = file->private_data;
390 struct dw_pcie *pci = pdata->pci;
391 struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info;
392 char debugfs_buf[DWC_DEBUGFS_BUF_MAX];
393 ssize_t pos;
394 u32 val;
395
396 mutex_lock(&rinfo->reg_event_lock);
397 set_event_number(pdata, pci, rinfo);
398 val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + RAS_DES_EVENT_COUNTER_CTRL_REG);
399 mutex_unlock(&rinfo->reg_event_lock);
400 val = FIELD_GET(EVENT_COUNTER_LANE_SELECT, val);
401 pos = scnprintf(debugfs_buf, DWC_DEBUGFS_BUF_MAX, "Lane: %d\n", val);
402
403 return simple_read_from_buffer(buf, count, ppos, debugfs_buf, pos);
404 }
405
406 static ssize_t counter_lane_write(struct file *file, const char __user *buf,
407 size_t count, loff_t *ppos)
408 {
409 struct dwc_pcie_rasdes_priv *pdata = file->private_data;
410 struct dw_pcie *pci = pdata->pci;
411 struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info;
412 u32 val, lane;
413 int ret;
414
415 ret = kstrtou32_from_user(buf, count, 0, &lane);
416 if (ret)
417 return ret;
418
419 mutex_lock(&rinfo->reg_event_lock);
420 set_event_number(pdata, pci, rinfo);
421 val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + RAS_DES_EVENT_COUNTER_CTRL_REG);
422 val &= ~(EVENT_COUNTER_LANE_SELECT);
423 val |= FIELD_PREP(EVENT_COUNTER_LANE_SELECT, lane);
424 dw_pcie_writel_dbi(pci, rinfo->ras_cap_offset + RAS_DES_EVENT_COUNTER_CTRL_REG, val);
425 mutex_unlock(&rinfo->reg_event_lock);
426
427 return count;
428 }
429
430 static ssize_t counter_value_read(struct file *file, char __user *buf,
431 size_t count, loff_t *ppos)
432 {
433 struct dwc_pcie_rasdes_priv *pdata = file->private_data;
434 struct dw_pcie *pci = pdata->pci;
435 struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info;
436 char debugfs_buf[DWC_DEBUGFS_BUF_MAX];
437 ssize_t pos;
438 u32 val;
439
440 mutex_lock(&rinfo->reg_event_lock);
441 set_event_number(pdata, pci, rinfo);
442 val = dw_pcie_readl_dbi(pci, rinfo->ras_cap_offset + RAS_DES_EVENT_COUNTER_DATA_REG);
443 mutex_unlock(&rinfo->reg_event_lock);
444 pos = scnprintf(debugfs_buf, DWC_DEBUGFS_BUF_MAX, "Counter value: %d\n", val);
445
446 return simple_read_from_buffer(buf, count, ppos, debugfs_buf, pos);
447 }
448
449 static int ltssm_status_show(struct seq_file *s, void *v)
450 {
451 struct dw_pcie *pci = s->private;
452 enum dw_pcie_ltssm val;
453
454 val = dw_pcie_get_ltssm(pci);
455 seq_printf(s, "%s (0x%02x)\n", dw_pcie_ltssm_status_string(val), val);
456
457 return 0;
458 }
459
460 static int ltssm_status_open(struct inode *inode, struct file *file)
461 {
462 return single_open(file, ltssm_status_show, inode->i_private);
463 }
464
465 #define dwc_debugfs_create(name) \
466 debugfs_create_file(#name, 0644, rasdes_debug, pci, \
467 &dbg_ ## name ## _fops)
468
469 #define DWC_DEBUGFS_FOPS(name) \
470 static const struct file_operations dbg_ ## name ## _fops = { \
471 .open = simple_open, \
472 .read = name ## _read, \
473 .write = name ## _write \
474 }
475
476 DWC_DEBUGFS_FOPS(lane_detect);
477 DWC_DEBUGFS_FOPS(rx_valid);
478
479 static const struct file_operations dwc_pcie_err_inj_ops = {
480 .open = simple_open,
481 .write = err_inj_write,
482 };
483
484 static const struct file_operations dwc_pcie_counter_enable_ops = {
485 .open = simple_open,
486 .read = counter_enable_read,
487 .write = counter_enable_write,
488 };
489
490 static const struct file_operations dwc_pcie_counter_lane_ops = {
491 .open = simple_open,
492 .read = counter_lane_read,
493 .write = counter_lane_write,
494 };
495
496 static const struct file_operations dwc_pcie_counter_value_ops = {
497 .open = simple_open,
498 .read = counter_value_read,
499 };
500
501 static const struct file_operations dwc_pcie_ltssm_status_ops = {
502 .open = ltssm_status_open,
503 .read = seq_read,
504 };
505
506 static void dwc_pcie_rasdes_debugfs_deinit(struct dw_pcie *pci)
507 {
508 struct dwc_pcie_rasdes_info *rinfo = pci->debugfs->rasdes_info;
509
510 mutex_destroy(&rinfo->reg_event_lock);
511 }
512
513 static int dwc_pcie_rasdes_debugfs_init(struct dw_pcie *pci, struct dentry *dir)
514 {
515 struct dentry *rasdes_debug, *rasdes_err_inj;
516 struct dentry *rasdes_event_counter, *rasdes_events;
517 struct dwc_pcie_rasdes_info *rasdes_info;
518 struct dwc_pcie_rasdes_priv *priv_tmp;
519 struct device *dev = pci->dev;
520 int ras_cap, i, ret;
521
522 /*
523 * If a given SoC has no RAS DES capability, the following call is
524 * bound to return an error, breaking some existing platforms. So,
525 * return 0 here, as this is not necessarily an error.
526 */
527 ras_cap = dw_pcie_find_rasdes_capability(pci);
528 if (!ras_cap) {
529 dev_dbg(dev, "no RAS DES capability available\n");
530 return 0;
531 }
532
533 rasdes_info = devm_kzalloc(dev, sizeof(*rasdes_info), GFP_KERNEL);
534 if (!rasdes_info)
535 return -ENOMEM;
536
537 /* Create subdirectories for Debug, Error Injection, Statistics. */
538 rasdes_debug = debugfs_create_dir("rasdes_debug", dir);
539 rasdes_err_inj = debugfs_create_dir("rasdes_err_inj", dir);
540 rasdes_event_counter = debugfs_create_dir("rasdes_event_counter", dir);
541
542 mutex_init(&rasdes_info->reg_event_lock);
543 rasdes_info->ras_cap_offset = ras_cap;
544 pci->debugfs->rasdes_info = rasdes_info;
545
546 /* Create debugfs files for Debug subdirectory. */
547 dwc_debugfs_create(lane_detect);
548 dwc_debugfs_create(rx_valid);
549
550 /* Create debugfs files for Error Injection subdirectory. */
551 for (i = 0; i < ARRAY_SIZE(err_inj_list); i++) {
552 priv_tmp = devm_kzalloc(dev, sizeof(*priv_tmp), GFP_KERNEL);
553 if (!priv_tmp) {
554 ret = -ENOMEM;
555 goto err_deinit;
556 }
557
558 priv_tmp->idx = i;
559 priv_tmp->pci = pci;
560 debugfs_create_file(err_inj_list[i].name, 0200, rasdes_err_inj, priv_tmp,
561 &dwc_pcie_err_inj_ops);
562 }
563
564 /* Create debugfs files for Statistical Counter subdirectory. */
565 for (i = 0; i < ARRAY_SIZE(event_list); i++) {
566 priv_tmp = devm_kzalloc(dev, sizeof(*priv_tmp), GFP_KERNEL);
567 if (!priv_tmp) {
568 ret = -ENOMEM;
569 goto err_deinit;
570 }
571
572 priv_tmp->idx = i;
573 priv_tmp->pci = pci;
574 rasdes_events = debugfs_create_dir(event_list[i].name, rasdes_event_counter);
575 if (event_list[i].group_no == 0 || event_list[i].group_no == 4) {
576 debugfs_create_file("lane_select", 0644, rasdes_events,
577 priv_tmp, &dwc_pcie_counter_lane_ops);
578 }
579 debugfs_create_file("counter_value", 0444, rasdes_events, priv_tmp,
580 &dwc_pcie_counter_value_ops);
581 debugfs_create_file("counter_enable", 0644, rasdes_events, priv_tmp,
582 &dwc_pcie_counter_enable_ops);
583 }
584
585 return 0;
586
587 err_deinit:
588 dwc_pcie_rasdes_debugfs_deinit(pci);
589 return ret;
590 }
591
592 static void dwc_pcie_ltssm_debugfs_init(struct dw_pcie *pci, struct dentry *dir)
593 {
594 debugfs_create_file("ltssm_status", 0444, dir, pci,
595 &dwc_pcie_ltssm_status_ops);
596 }
597
598 static int dw_pcie_ptm_check_capability(void *drvdata)
599 {
600 struct dw_pcie *pci = drvdata;
601
602 pci->ptm_vsec_offset = dw_pcie_find_ptm_capability(pci);
603
604 return pci->ptm_vsec_offset;
605 }
606
607 static int dw_pcie_ptm_context_update_write(void *drvdata, u8 mode)
608 {
609 struct dw_pcie *pci = drvdata;
610 u32 val;
611
612 if (mode == PCIE_PTM_CONTEXT_UPDATE_AUTO) {
613 val = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_RES_REQ_CTRL);
614 val |= PTM_REQ_AUTO_UPDATE_ENABLED;
615 dw_pcie_writel_dbi(pci, pci->ptm_vsec_offset + PTM_RES_REQ_CTRL, val);
616 } else if (mode == PCIE_PTM_CONTEXT_UPDATE_MANUAL) {
617 val = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_RES_REQ_CTRL);
618 val &= ~PTM_REQ_AUTO_UPDATE_ENABLED;
619 val |= PTM_REQ_START_UPDATE;
620 dw_pcie_writel_dbi(pci, pci->ptm_vsec_offset + PTM_RES_REQ_CTRL, val);
621 } else {
622 return -EINVAL;
623 }
624
625 return 0;
626 }
627
628 static int dw_pcie_ptm_context_update_read(void *drvdata, u8 *mode)
629 {
630 struct dw_pcie *pci = drvdata;
631 u32 val;
632
633 val = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_RES_REQ_CTRL);
634 if (FIELD_GET(PTM_REQ_AUTO_UPDATE_ENABLED, val))
635 *mode = PCIE_PTM_CONTEXT_UPDATE_AUTO;
636 else
637 /*
638 * PTM_REQ_START_UPDATE is a self clearing register bit. So if
639 * PTM_REQ_AUTO_UPDATE_ENABLED is not set, then it implies that
640 * manual update is used.
641 */
642 *mode = PCIE_PTM_CONTEXT_UPDATE_MANUAL;
643
644 return 0;
645 }
646
647 static int dw_pcie_ptm_context_valid_write(void *drvdata, bool valid)
648 {
649 struct dw_pcie *pci = drvdata;
650 u32 val;
651
652 if (valid) {
653 val = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_RES_REQ_CTRL);
654 val |= PTM_RES_CCONTEXT_VALID;
655 dw_pcie_writel_dbi(pci, pci->ptm_vsec_offset + PTM_RES_REQ_CTRL, val);
656 } else {
657 val = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_RES_REQ_CTRL);
658 val &= ~PTM_RES_CCONTEXT_VALID;
659 dw_pcie_writel_dbi(pci, pci->ptm_vsec_offset + PTM_RES_REQ_CTRL, val);
660 }
661
662 return 0;
663 }
664
665 static int dw_pcie_ptm_context_valid_read(void *drvdata, bool *valid)
666 {
667 struct dw_pcie *pci = drvdata;
668 u32 val;
669
670 val = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_RES_REQ_CTRL);
671 *valid = !!FIELD_GET(PTM_RES_CCONTEXT_VALID, val);
672
673 return 0;
674 }
675
676 static int dw_pcie_ptm_local_clock_read(void *drvdata, u64 *clock)
677 {
678 struct dw_pcie *pci = drvdata;
679 u32 msb, lsb;
680
681 do {
682 msb = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_LOCAL_MSB);
683 lsb = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_LOCAL_LSB);
684 } while (msb != dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_LOCAL_MSB));
685
686 *clock = ((u64) msb) << 32 | lsb;
687
688 return 0;
689 }
690
691 static int dw_pcie_ptm_master_clock_read(void *drvdata, u64 *clock)
692 {
693 struct dw_pcie *pci = drvdata;
694 u32 msb, lsb;
695
696 do {
697 msb = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_MASTER_MSB);
698 lsb = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_MASTER_LSB);
699 } while (msb != dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_MASTER_MSB));
700
701 *clock = ((u64) msb) << 32 | lsb;
702
703 return 0;
704 }
705
706 static int dw_pcie_ptm_t1_read(void *drvdata, u64 *clock)
707 {
708 struct dw_pcie *pci = drvdata;
709 u32 msb, lsb;
710
711 do {
712 msb = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_T1_T2_MSB);
713 lsb = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_T1_T2_LSB);
714 } while (msb != dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_T1_T2_MSB));
715
716 *clock = ((u64) msb) << 32 | lsb;
717
718 return 0;
719 }
720
721 static int dw_pcie_ptm_t2_read(void *drvdata, u64 *clock)
722 {
723 struct dw_pcie *pci = drvdata;
724 u32 msb, lsb;
725
726 do {
727 msb = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_T1_T2_MSB);
728 lsb = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_T1_T2_LSB);
729 } while (msb != dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_T1_T2_MSB));
730
731 *clock = ((u64) msb) << 32 | lsb;
732
733 return 0;
734 }
735
736 static int dw_pcie_ptm_t3_read(void *drvdata, u64 *clock)
737 {
738 struct dw_pcie *pci = drvdata;
739 u32 msb, lsb;
740
741 do {
742 msb = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_T3_T4_MSB);
743 lsb = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_T3_T4_LSB);
744 } while (msb != dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_T3_T4_MSB));
745
746 *clock = ((u64) msb) << 32 | lsb;
747
748 return 0;
749 }
750
751 static int dw_pcie_ptm_t4_read(void *drvdata, u64 *clock)
752 {
753 struct dw_pcie *pci = drvdata;
754 u32 msb, lsb;
755
756 do {
757 msb = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_T3_T4_MSB);
758 lsb = dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_T3_T4_LSB);
759 } while (msb != dw_pcie_readl_dbi(pci, pci->ptm_vsec_offset + PTM_T3_T4_MSB));
760
761 *clock = ((u64) msb) << 32 | lsb;
762
763 return 0;
764 }
765
766 static bool dw_pcie_ptm_context_update_visible(void *drvdata)
767 {
768 struct dw_pcie *pci = drvdata;
769
770 return pci->mode == DW_PCIE_EP_TYPE;
771 }
772
773 static bool dw_pcie_ptm_context_valid_visible(void *drvdata)
774 {
775 struct dw_pcie *pci = drvdata;
776
777 return pci->mode == DW_PCIE_RC_TYPE;
778 }
779
780 static bool dw_pcie_ptm_local_clock_visible(void *drvdata)
781 {
782 /* PTM local clock is always visible */
783 return true;
784 }
785
786 static bool dw_pcie_ptm_master_clock_visible(void *drvdata)
787 {
788 struct dw_pcie *pci = drvdata;
789
790 return pci->mode == DW_PCIE_EP_TYPE;
791 }
792
793 static bool dw_pcie_ptm_t1_visible(void *drvdata)
794 {
795 struct dw_pcie *pci = drvdata;
796
797 return pci->mode == DW_PCIE_EP_TYPE;
798 }
799
800 static bool dw_pcie_ptm_t2_visible(void *drvdata)
801 {
802 struct dw_pcie *pci = drvdata;
803
804 return pci->mode == DW_PCIE_RC_TYPE;
805 }
806
807 static bool dw_pcie_ptm_t3_visible(void *drvdata)
808 {
809 struct dw_pcie *pci = drvdata;
810
811 return pci->mode == DW_PCIE_RC_TYPE;
812 }
813
814 static bool dw_pcie_ptm_t4_visible(void *drvdata)
815 {
816 struct dw_pcie *pci = drvdata;
817
818 return pci->mode == DW_PCIE_EP_TYPE;
819 }
820
821 static const struct pcie_ptm_ops dw_pcie_ptm_ops = {
822 .check_capability = dw_pcie_ptm_check_capability,
823 .context_update_write = dw_pcie_ptm_context_update_write,
824 .context_update_read = dw_pcie_ptm_context_update_read,
825 .context_valid_write = dw_pcie_ptm_context_valid_write,
826 .context_valid_read = dw_pcie_ptm_context_valid_read,
827 .local_clock_read = dw_pcie_ptm_local_clock_read,
828 .master_clock_read = dw_pcie_ptm_master_clock_read,
829 .t1_read = dw_pcie_ptm_t1_read,
830 .t2_read = dw_pcie_ptm_t2_read,
831 .t3_read = dw_pcie_ptm_t3_read,
832 .t4_read = dw_pcie_ptm_t4_read,
833 .context_update_visible = dw_pcie_ptm_context_update_visible,
834 .context_valid_visible = dw_pcie_ptm_context_valid_visible,
835 .local_clock_visible = dw_pcie_ptm_local_clock_visible,
836 .master_clock_visible = dw_pcie_ptm_master_clock_visible,
837 .t1_visible = dw_pcie_ptm_t1_visible,
838 .t2_visible = dw_pcie_ptm_t2_visible,
839 .t3_visible = dw_pcie_ptm_t3_visible,
840 .t4_visible = dw_pcie_ptm_t4_visible,
841 };
842
843 void dwc_pcie_debugfs_deinit(struct dw_pcie *pci)
844 {
845 if (!pci->debugfs)
846 return;
847
848 pcie_ptm_destroy_debugfs(pci->ptm_debugfs);
849 dwc_pcie_rasdes_debugfs_deinit(pci);
850 debugfs_remove_recursive(pci->debugfs->debug_dir);
851 }
852
853 void dwc_pcie_debugfs_init(struct dw_pcie *pci, enum dw_pcie_device_mode mode)
854 {
855 char dirname[DWC_DEBUGFS_BUF_MAX];
856 struct device *dev = pci->dev;
857 struct debugfs_info *debugfs;
858 struct dentry *dir;
859 int err;
860
861 /* Create main directory for each platform driver. */
862 snprintf(dirname, DWC_DEBUGFS_BUF_MAX, "dwc_pcie_%s", dev_name(dev));
863 dir = debugfs_create_dir(dirname, NULL);
864 debugfs = devm_kzalloc(dev, sizeof(*debugfs), GFP_KERNEL);
865 if (!debugfs)
866 return;
867
868 debugfs->debug_dir = dir;
869 pci->debugfs = debugfs;
870 err = dwc_pcie_rasdes_debugfs_init(pci, dir);
871 if (err)
872 dev_err(dev, "failed to initialize RAS DES debugfs, err=%d\n",
873 err);
874
875 dwc_pcie_ltssm_debugfs_init(pci, dir);
876
877 pci->mode = mode;
878 pci->ptm_debugfs = pcie_ptm_create_debugfs(pci->dev, pci,
879 &dw_pcie_ptm_ops);
880 }