pcie-designware-debugfs.c 25 KB

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