pci.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright(c) 2021 Intel Corporation. All rights reserved. */
  3. #include <linux/units.h>
  4. #include <linux/io-64-nonatomic-lo-hi.h>
  5. #include <linux/device.h>
  6. #include <linux/delay.h>
  7. #include <linux/pci.h>
  8. #include <linux/pci-doe.h>
  9. #include <linux/aer.h>
  10. #include <cxlpci.h>
  11. #include <cxlmem.h>
  12. #include <cxl.h>
  13. #include "core.h"
  14. #include "trace.h"
  15. /**
  16. * DOC: cxl core pci
  17. *
  18. * Compute Express Link protocols are layered on top of PCIe. CXL core provides
  19. * a set of helpers for CXL interactions which occur via PCIe.
  20. */
  21. static unsigned short media_ready_timeout = 60;
  22. module_param(media_ready_timeout, ushort, 0644);
  23. MODULE_PARM_DESC(media_ready_timeout, "seconds to wait for media ready");
  24. struct cxl_walk_context {
  25. struct pci_bus *bus;
  26. struct cxl_port *port;
  27. int type;
  28. int error;
  29. int count;
  30. };
  31. static int match_add_dports(struct pci_dev *pdev, void *data)
  32. {
  33. struct cxl_walk_context *ctx = data;
  34. struct cxl_port *port = ctx->port;
  35. int type = pci_pcie_type(pdev);
  36. struct cxl_register_map map;
  37. struct cxl_dport *dport;
  38. u32 lnkcap, port_num;
  39. int rc;
  40. if (pdev->bus != ctx->bus)
  41. return 0;
  42. if (!pci_is_pcie(pdev))
  43. return 0;
  44. if (type != ctx->type)
  45. return 0;
  46. if (pci_read_config_dword(pdev, pci_pcie_cap(pdev) + PCI_EXP_LNKCAP,
  47. &lnkcap))
  48. return 0;
  49. rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
  50. if (rc)
  51. dev_dbg(&port->dev, "failed to find component registers\n");
  52. port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
  53. dport = devm_cxl_add_dport(port, &pdev->dev, port_num, map.resource);
  54. if (IS_ERR(dport)) {
  55. ctx->error = PTR_ERR(dport);
  56. return PTR_ERR(dport);
  57. }
  58. ctx->count++;
  59. return 0;
  60. }
  61. /**
  62. * devm_cxl_port_enumerate_dports - enumerate downstream ports of the upstream port
  63. * @port: cxl_port whose ->uport_dev is the upstream of dports to be enumerated
  64. *
  65. * Returns a positive number of dports enumerated or a negative error
  66. * code.
  67. */
  68. int devm_cxl_port_enumerate_dports(struct cxl_port *port)
  69. {
  70. struct pci_bus *bus = cxl_port_to_pci_bus(port);
  71. struct cxl_walk_context ctx;
  72. int type;
  73. if (!bus)
  74. return -ENXIO;
  75. if (pci_is_root_bus(bus))
  76. type = PCI_EXP_TYPE_ROOT_PORT;
  77. else
  78. type = PCI_EXP_TYPE_DOWNSTREAM;
  79. ctx = (struct cxl_walk_context) {
  80. .port = port,
  81. .bus = bus,
  82. .type = type,
  83. };
  84. pci_walk_bus(bus, match_add_dports, &ctx);
  85. if (ctx.count == 0)
  86. return -ENODEV;
  87. if (ctx.error)
  88. return ctx.error;
  89. return ctx.count;
  90. }
  91. EXPORT_SYMBOL_NS_GPL(devm_cxl_port_enumerate_dports, CXL);
  92. static int cxl_dvsec_mem_range_valid(struct cxl_dev_state *cxlds, int id)
  93. {
  94. struct pci_dev *pdev = to_pci_dev(cxlds->dev);
  95. int d = cxlds->cxl_dvsec;
  96. bool valid = false;
  97. int rc, i;
  98. u32 temp;
  99. if (id > CXL_DVSEC_RANGE_MAX)
  100. return -EINVAL;
  101. /* Check MEM INFO VALID bit first, give up after 1s */
  102. i = 1;
  103. do {
  104. rc = pci_read_config_dword(pdev,
  105. d + CXL_DVSEC_RANGE_SIZE_LOW(id),
  106. &temp);
  107. if (rc)
  108. return rc;
  109. valid = FIELD_GET(CXL_DVSEC_MEM_INFO_VALID, temp);
  110. if (valid)
  111. break;
  112. msleep(1000);
  113. } while (i--);
  114. if (!valid) {
  115. dev_err(&pdev->dev,
  116. "Timeout awaiting memory range %d valid after 1s.\n",
  117. id);
  118. return -ETIMEDOUT;
  119. }
  120. return 0;
  121. }
  122. static int cxl_dvsec_mem_range_active(struct cxl_dev_state *cxlds, int id)
  123. {
  124. struct pci_dev *pdev = to_pci_dev(cxlds->dev);
  125. int d = cxlds->cxl_dvsec;
  126. bool active = false;
  127. int rc, i;
  128. u32 temp;
  129. if (id > CXL_DVSEC_RANGE_MAX)
  130. return -EINVAL;
  131. /* Check MEM ACTIVE bit, up to 60s timeout by default */
  132. for (i = media_ready_timeout; i; i--) {
  133. rc = pci_read_config_dword(
  134. pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(id), &temp);
  135. if (rc)
  136. return rc;
  137. active = FIELD_GET(CXL_DVSEC_MEM_ACTIVE, temp);
  138. if (active)
  139. break;
  140. msleep(1000);
  141. }
  142. if (!active) {
  143. dev_err(&pdev->dev,
  144. "timeout awaiting memory active after %d seconds\n",
  145. media_ready_timeout);
  146. return -ETIMEDOUT;
  147. }
  148. return 0;
  149. }
  150. /*
  151. * Wait up to @media_ready_timeout for the device to report memory
  152. * active.
  153. */
  154. int cxl_await_media_ready(struct cxl_dev_state *cxlds)
  155. {
  156. struct pci_dev *pdev = to_pci_dev(cxlds->dev);
  157. int d = cxlds->cxl_dvsec;
  158. int rc, i, hdm_count;
  159. u64 md_status;
  160. u16 cap;
  161. rc = pci_read_config_word(pdev,
  162. d + CXL_DVSEC_CAP_OFFSET, &cap);
  163. if (rc)
  164. return rc;
  165. hdm_count = FIELD_GET(CXL_DVSEC_HDM_COUNT_MASK, cap);
  166. for (i = 0; i < hdm_count; i++) {
  167. rc = cxl_dvsec_mem_range_valid(cxlds, i);
  168. if (rc)
  169. return rc;
  170. }
  171. for (i = 0; i < hdm_count; i++) {
  172. rc = cxl_dvsec_mem_range_active(cxlds, i);
  173. if (rc)
  174. return rc;
  175. }
  176. md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
  177. if (!CXLMDEV_READY(md_status))
  178. return -EIO;
  179. return 0;
  180. }
  181. EXPORT_SYMBOL_NS_GPL(cxl_await_media_ready, CXL);
  182. static int cxl_set_mem_enable(struct cxl_dev_state *cxlds, u16 val)
  183. {
  184. struct pci_dev *pdev = to_pci_dev(cxlds->dev);
  185. int d = cxlds->cxl_dvsec;
  186. u16 ctrl;
  187. int rc;
  188. rc = pci_read_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, &ctrl);
  189. if (rc < 0)
  190. return rc;
  191. if ((ctrl & CXL_DVSEC_MEM_ENABLE) == val)
  192. return 1;
  193. ctrl &= ~CXL_DVSEC_MEM_ENABLE;
  194. ctrl |= val;
  195. rc = pci_write_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, ctrl);
  196. if (rc < 0)
  197. return rc;
  198. return 0;
  199. }
  200. static void clear_mem_enable(void *cxlds)
  201. {
  202. cxl_set_mem_enable(cxlds, 0);
  203. }
  204. static int devm_cxl_enable_mem(struct device *host, struct cxl_dev_state *cxlds)
  205. {
  206. int rc;
  207. rc = cxl_set_mem_enable(cxlds, CXL_DVSEC_MEM_ENABLE);
  208. if (rc < 0)
  209. return rc;
  210. if (rc > 0)
  211. return 0;
  212. return devm_add_action_or_reset(host, clear_mem_enable, cxlds);
  213. }
  214. /* require dvsec ranges to be covered by a locked platform window */
  215. static int dvsec_range_allowed(struct device *dev, void *arg)
  216. {
  217. struct range *dev_range = arg;
  218. struct cxl_decoder *cxld;
  219. if (!is_root_decoder(dev))
  220. return 0;
  221. cxld = to_cxl_decoder(dev);
  222. if (!(cxld->flags & CXL_DECODER_F_RAM))
  223. return 0;
  224. return range_contains(&cxld->hpa_range, dev_range);
  225. }
  226. static void disable_hdm(void *_cxlhdm)
  227. {
  228. u32 global_ctrl;
  229. struct cxl_hdm *cxlhdm = _cxlhdm;
  230. void __iomem *hdm = cxlhdm->regs.hdm_decoder;
  231. global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
  232. writel(global_ctrl & ~CXL_HDM_DECODER_ENABLE,
  233. hdm + CXL_HDM_DECODER_CTRL_OFFSET);
  234. }
  235. static int devm_cxl_enable_hdm(struct device *host, struct cxl_hdm *cxlhdm)
  236. {
  237. void __iomem *hdm = cxlhdm->regs.hdm_decoder;
  238. u32 global_ctrl;
  239. global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
  240. writel(global_ctrl | CXL_HDM_DECODER_ENABLE,
  241. hdm + CXL_HDM_DECODER_CTRL_OFFSET);
  242. return devm_add_action_or_reset(host, disable_hdm, cxlhdm);
  243. }
  244. int cxl_dvsec_rr_decode(struct device *dev, struct cxl_port *port,
  245. struct cxl_endpoint_dvsec_info *info)
  246. {
  247. struct pci_dev *pdev = to_pci_dev(dev);
  248. struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
  249. int hdm_count, rc, i, ranges = 0;
  250. int d = cxlds->cxl_dvsec;
  251. u16 cap, ctrl;
  252. if (!d) {
  253. dev_dbg(dev, "No DVSEC Capability\n");
  254. return -ENXIO;
  255. }
  256. rc = pci_read_config_word(pdev, d + CXL_DVSEC_CAP_OFFSET, &cap);
  257. if (rc)
  258. return rc;
  259. if (!(cap & CXL_DVSEC_MEM_CAPABLE)) {
  260. dev_dbg(dev, "Not MEM Capable\n");
  261. return -ENXIO;
  262. }
  263. /*
  264. * It is not allowed by spec for MEM.capable to be set and have 0 legacy
  265. * HDM decoders (values > 2 are also undefined as of CXL 2.0). As this
  266. * driver is for a spec defined class code which must be CXL.mem
  267. * capable, there is no point in continuing to enable CXL.mem.
  268. */
  269. hdm_count = FIELD_GET(CXL_DVSEC_HDM_COUNT_MASK, cap);
  270. if (!hdm_count || hdm_count > 2)
  271. return -EINVAL;
  272. /*
  273. * The current DVSEC values are moot if the memory capability is
  274. * disabled, and they will remain moot after the HDM Decoder
  275. * capability is enabled.
  276. */
  277. rc = pci_read_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, &ctrl);
  278. if (rc)
  279. return rc;
  280. info->mem_enabled = FIELD_GET(CXL_DVSEC_MEM_ENABLE, ctrl);
  281. if (!info->mem_enabled)
  282. return 0;
  283. for (i = 0; i < hdm_count; i++) {
  284. u64 base, size;
  285. u32 temp;
  286. rc = cxl_dvsec_mem_range_valid(cxlds, i);
  287. if (rc)
  288. return rc;
  289. rc = pci_read_config_dword(
  290. pdev, d + CXL_DVSEC_RANGE_SIZE_HIGH(i), &temp);
  291. if (rc)
  292. return rc;
  293. size = (u64)temp << 32;
  294. rc = pci_read_config_dword(
  295. pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(i), &temp);
  296. if (rc)
  297. return rc;
  298. size |= temp & CXL_DVSEC_MEM_SIZE_LOW_MASK;
  299. if (!size) {
  300. continue;
  301. }
  302. rc = pci_read_config_dword(
  303. pdev, d + CXL_DVSEC_RANGE_BASE_HIGH(i), &temp);
  304. if (rc)
  305. return rc;
  306. base = (u64)temp << 32;
  307. rc = pci_read_config_dword(
  308. pdev, d + CXL_DVSEC_RANGE_BASE_LOW(i), &temp);
  309. if (rc)
  310. return rc;
  311. base |= temp & CXL_DVSEC_MEM_BASE_LOW_MASK;
  312. info->dvsec_range[ranges++] = (struct range) {
  313. .start = base,
  314. .end = base + size - 1
  315. };
  316. }
  317. info->ranges = ranges;
  318. return 0;
  319. }
  320. EXPORT_SYMBOL_NS_GPL(cxl_dvsec_rr_decode, CXL);
  321. /**
  322. * cxl_hdm_decode_init() - Setup HDM decoding for the endpoint
  323. * @cxlds: Device state
  324. * @cxlhdm: Mapped HDM decoder Capability
  325. * @info: Cached DVSEC range registers info
  326. *
  327. * Try to enable the endpoint's HDM Decoder Capability
  328. */
  329. int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
  330. struct cxl_endpoint_dvsec_info *info)
  331. {
  332. void __iomem *hdm = cxlhdm->regs.hdm_decoder;
  333. struct cxl_port *port = cxlhdm->port;
  334. struct device *dev = cxlds->dev;
  335. struct cxl_port *root;
  336. int i, rc, allowed;
  337. u32 global_ctrl = 0;
  338. if (hdm)
  339. global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
  340. /*
  341. * If the HDM Decoder Capability is already enabled then assume
  342. * that some other agent like platform firmware set it up.
  343. */
  344. if (global_ctrl & CXL_HDM_DECODER_ENABLE || (!hdm && info->mem_enabled))
  345. return devm_cxl_enable_mem(&port->dev, cxlds);
  346. else if (!hdm)
  347. return -ENODEV;
  348. root = to_cxl_port(port->dev.parent);
  349. while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
  350. root = to_cxl_port(root->dev.parent);
  351. if (!is_cxl_root(root)) {
  352. dev_err(dev, "Failed to acquire root port for HDM enable\n");
  353. return -ENODEV;
  354. }
  355. if (!info->mem_enabled) {
  356. rc = devm_cxl_enable_hdm(&port->dev, cxlhdm);
  357. if (rc)
  358. return rc;
  359. return devm_cxl_enable_mem(&port->dev, cxlds);
  360. }
  361. for (i = 0, allowed = 0; i < info->ranges; i++) {
  362. struct device *cxld_dev;
  363. cxld_dev = device_find_child(&root->dev, &info->dvsec_range[i],
  364. dvsec_range_allowed);
  365. if (!cxld_dev) {
  366. dev_dbg(dev, "DVSEC Range%d denied by platform\n", i);
  367. continue;
  368. }
  369. dev_dbg(dev, "DVSEC Range%d allowed by platform\n", i);
  370. put_device(cxld_dev);
  371. allowed++;
  372. }
  373. if (!allowed) {
  374. dev_err(dev, "Range register decodes outside platform defined CXL ranges.\n");
  375. return -ENXIO;
  376. }
  377. /*
  378. * Per CXL 2.0 Section 8.1.3.8.3 and 8.1.3.8.4 DVSEC CXL Range 1 Base
  379. * [High,Low] when HDM operation is enabled the range register values
  380. * are ignored by the device, but the spec also recommends matching the
  381. * DVSEC Range 1,2 to HDM Decoder Range 0,1. So, non-zero info->ranges
  382. * are expected even though Linux does not require or maintain that
  383. * match. If at least one DVSEC range is enabled and allowed, skip HDM
  384. * Decoder Capability Enable.
  385. */
  386. return 0;
  387. }
  388. EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, CXL);
  389. #define CXL_DOE_TABLE_ACCESS_REQ_CODE 0x000000ff
  390. #define CXL_DOE_TABLE_ACCESS_REQ_CODE_READ 0
  391. #define CXL_DOE_TABLE_ACCESS_TABLE_TYPE 0x0000ff00
  392. #define CXL_DOE_TABLE_ACCESS_TABLE_TYPE_CDATA 0
  393. #define CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE 0xffff0000
  394. #define CXL_DOE_TABLE_ACCESS_LAST_ENTRY 0xffff
  395. #define CXL_DOE_PROTOCOL_TABLE_ACCESS 2
  396. #define CDAT_DOE_REQ(entry_handle) cpu_to_le32 \
  397. (FIELD_PREP(CXL_DOE_TABLE_ACCESS_REQ_CODE, \
  398. CXL_DOE_TABLE_ACCESS_REQ_CODE_READ) | \
  399. FIELD_PREP(CXL_DOE_TABLE_ACCESS_TABLE_TYPE, \
  400. CXL_DOE_TABLE_ACCESS_TABLE_TYPE_CDATA) | \
  401. FIELD_PREP(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, (entry_handle)))
  402. static int cxl_cdat_get_length(struct device *dev,
  403. struct pci_doe_mb *doe_mb,
  404. size_t *length)
  405. {
  406. __le32 request = CDAT_DOE_REQ(0);
  407. __le32 response[2];
  408. int rc;
  409. rc = pci_doe(doe_mb, PCI_VENDOR_ID_CXL,
  410. CXL_DOE_PROTOCOL_TABLE_ACCESS,
  411. &request, sizeof(request),
  412. &response, sizeof(response));
  413. if (rc < 0) {
  414. dev_err(dev, "DOE failed: %d", rc);
  415. return rc;
  416. }
  417. if (rc < sizeof(response))
  418. return -EIO;
  419. *length = le32_to_cpu(response[1]);
  420. dev_dbg(dev, "CDAT length %zu\n", *length);
  421. return 0;
  422. }
  423. static int cxl_cdat_read_table(struct device *dev,
  424. struct pci_doe_mb *doe_mb,
  425. struct cdat_doe_rsp *rsp, size_t *length)
  426. {
  427. size_t received, remaining = *length;
  428. unsigned int entry_handle = 0;
  429. union cdat_data *data;
  430. __le32 saved_dw = 0;
  431. do {
  432. __le32 request = CDAT_DOE_REQ(entry_handle);
  433. int rc;
  434. rc = pci_doe(doe_mb, PCI_VENDOR_ID_CXL,
  435. CXL_DOE_PROTOCOL_TABLE_ACCESS,
  436. &request, sizeof(request),
  437. rsp, sizeof(*rsp) + remaining);
  438. if (rc < 0) {
  439. dev_err(dev, "DOE failed: %d", rc);
  440. return rc;
  441. }
  442. if (rc < sizeof(*rsp))
  443. return -EIO;
  444. data = (union cdat_data *)rsp->data;
  445. received = rc - sizeof(*rsp);
  446. if (entry_handle == 0) {
  447. if (received != sizeof(data->header))
  448. return -EIO;
  449. } else {
  450. if (received < sizeof(data->entry) ||
  451. received != le16_to_cpu(data->entry.length))
  452. return -EIO;
  453. }
  454. /* Get the CXL table access header entry handle */
  455. entry_handle = FIELD_GET(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE,
  456. le32_to_cpu(rsp->doe_header));
  457. /*
  458. * Table Access Response Header overwrote the last DW of
  459. * previous entry, so restore that DW
  460. */
  461. rsp->doe_header = saved_dw;
  462. remaining -= received;
  463. rsp = (void *)rsp + received;
  464. saved_dw = rsp->doe_header;
  465. } while (entry_handle != CXL_DOE_TABLE_ACCESS_LAST_ENTRY);
  466. /* Length in CDAT header may exceed concatenation of CDAT entries */
  467. *length -= remaining;
  468. return 0;
  469. }
  470. static unsigned char cdat_checksum(void *buf, size_t size)
  471. {
  472. unsigned char sum, *data = buf;
  473. size_t i;
  474. for (sum = 0, i = 0; i < size; i++)
  475. sum += data[i];
  476. return sum;
  477. }
  478. /**
  479. * read_cdat_data - Read the CDAT data on this port
  480. * @port: Port to read data from
  481. *
  482. * This call will sleep waiting for responses from the DOE mailbox.
  483. */
  484. void read_cdat_data(struct cxl_port *port)
  485. {
  486. struct device *uport = port->uport_dev;
  487. struct device *dev = &port->dev;
  488. struct pci_doe_mb *doe_mb;
  489. struct pci_dev *pdev = NULL;
  490. struct cxl_memdev *cxlmd;
  491. struct cdat_doe_rsp *buf;
  492. size_t table_length, length;
  493. int rc;
  494. if (is_cxl_memdev(uport)) {
  495. struct device *host;
  496. cxlmd = to_cxl_memdev(uport);
  497. host = cxlmd->dev.parent;
  498. if (dev_is_pci(host))
  499. pdev = to_pci_dev(host);
  500. } else if (dev_is_pci(uport)) {
  501. pdev = to_pci_dev(uport);
  502. }
  503. if (!pdev)
  504. return;
  505. doe_mb = pci_find_doe_mailbox(pdev, PCI_VENDOR_ID_CXL,
  506. CXL_DOE_PROTOCOL_TABLE_ACCESS);
  507. if (!doe_mb) {
  508. dev_dbg(dev, "No CDAT mailbox\n");
  509. return;
  510. }
  511. port->cdat_available = true;
  512. if (cxl_cdat_get_length(dev, doe_mb, &length)) {
  513. dev_dbg(dev, "No CDAT length\n");
  514. return;
  515. }
  516. /*
  517. * The begin of the CDAT buffer needs space for additional 4
  518. * bytes for the DOE header. Table data starts afterwards.
  519. */
  520. buf = devm_kzalloc(dev, sizeof(*buf) + length, GFP_KERNEL);
  521. if (!buf)
  522. goto err;
  523. table_length = length;
  524. rc = cxl_cdat_read_table(dev, doe_mb, buf, &length);
  525. if (rc)
  526. goto err;
  527. if (table_length != length)
  528. dev_warn(dev, "Malformed CDAT table length (%zu:%zu), discarding trailing data\n",
  529. table_length, length);
  530. if (cdat_checksum(buf->data, length))
  531. goto err;
  532. port->cdat.table = buf->data;
  533. port->cdat.length = length;
  534. return;
  535. err:
  536. /* Don't leave table data allocated on error */
  537. devm_kfree(dev, buf);
  538. dev_err(dev, "Failed to read/validate CDAT.\n");
  539. }
  540. EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL);
  541. static void __cxl_handle_cor_ras(struct cxl_dev_state *cxlds,
  542. void __iomem *ras_base)
  543. {
  544. void __iomem *addr;
  545. u32 status;
  546. if (!ras_base)
  547. return;
  548. addr = ras_base + CXL_RAS_CORRECTABLE_STATUS_OFFSET;
  549. status = readl(addr);
  550. if (status & CXL_RAS_CORRECTABLE_STATUS_MASK) {
  551. writel(status & CXL_RAS_CORRECTABLE_STATUS_MASK, addr);
  552. trace_cxl_aer_correctable_error(cxlds->cxlmd, status);
  553. }
  554. }
  555. static void cxl_handle_endpoint_cor_ras(struct cxl_dev_state *cxlds)
  556. {
  557. return __cxl_handle_cor_ras(cxlds, cxlds->regs.ras);
  558. }
  559. /* CXL spec rev3.0 8.2.4.16.1 */
  560. static void header_log_copy(void __iomem *ras_base, u32 *log)
  561. {
  562. void __iomem *addr;
  563. u32 *log_addr;
  564. int i, log_u32_size = CXL_HEADERLOG_SIZE / sizeof(u32);
  565. addr = ras_base + CXL_RAS_HEADER_LOG_OFFSET;
  566. log_addr = log;
  567. for (i = 0; i < log_u32_size; i++) {
  568. *log_addr = readl(addr);
  569. log_addr++;
  570. addr += sizeof(u32);
  571. }
  572. }
  573. /*
  574. * Log the state of the RAS status registers and prepare them to log the
  575. * next error status. Return 1 if reset needed.
  576. */
  577. static bool __cxl_handle_ras(struct cxl_dev_state *cxlds,
  578. void __iomem *ras_base)
  579. {
  580. u32 hl[CXL_HEADERLOG_SIZE_U32];
  581. void __iomem *addr;
  582. u32 status;
  583. u32 fe;
  584. if (!ras_base)
  585. return false;
  586. addr = ras_base + CXL_RAS_UNCORRECTABLE_STATUS_OFFSET;
  587. status = readl(addr);
  588. if (!(status & CXL_RAS_UNCORRECTABLE_STATUS_MASK))
  589. return false;
  590. /* If multiple errors, log header points to first error from ctrl reg */
  591. if (hweight32(status) > 1) {
  592. void __iomem *rcc_addr =
  593. ras_base + CXL_RAS_CAP_CONTROL_OFFSET;
  594. fe = BIT(FIELD_GET(CXL_RAS_CAP_CONTROL_FE_MASK,
  595. readl(rcc_addr)));
  596. } else {
  597. fe = status;
  598. }
  599. header_log_copy(ras_base, hl);
  600. trace_cxl_aer_uncorrectable_error(cxlds->cxlmd, status, fe, hl);
  601. writel(status & CXL_RAS_UNCORRECTABLE_STATUS_MASK, addr);
  602. return true;
  603. }
  604. static bool cxl_handle_endpoint_ras(struct cxl_dev_state *cxlds)
  605. {
  606. return __cxl_handle_ras(cxlds, cxlds->regs.ras);
  607. }
  608. #ifdef CONFIG_PCIEAER_CXL
  609. static void cxl_dport_map_rch_aer(struct cxl_dport *dport)
  610. {
  611. resource_size_t aer_phys;
  612. struct device *host;
  613. u16 aer_cap;
  614. aer_cap = cxl_rcrb_to_aer(dport->dport_dev, dport->rcrb.base);
  615. if (aer_cap) {
  616. host = dport->reg_map.host;
  617. aer_phys = aer_cap + dport->rcrb.base;
  618. dport->regs.dport_aer = devm_cxl_iomap_block(host, aer_phys,
  619. sizeof(struct aer_capability_regs));
  620. }
  621. }
  622. static void cxl_dport_map_ras(struct cxl_dport *dport)
  623. {
  624. struct cxl_register_map *map = &dport->reg_map;
  625. struct device *dev = dport->dport_dev;
  626. if (!map->component_map.ras.valid)
  627. dev_dbg(dev, "RAS registers not found\n");
  628. else if (cxl_map_component_regs(map, &dport->regs.component,
  629. BIT(CXL_CM_CAP_CAP_ID_RAS)))
  630. dev_dbg(dev, "Failed to map RAS capability.\n");
  631. }
  632. static void cxl_disable_rch_root_ints(struct cxl_dport *dport)
  633. {
  634. void __iomem *aer_base = dport->regs.dport_aer;
  635. u32 aer_cmd_mask, aer_cmd;
  636. if (!aer_base)
  637. return;
  638. /*
  639. * Disable RCH root port command interrupts.
  640. * CXL 3.0 12.2.1.1 - RCH Downstream Port-detected Errors
  641. *
  642. * This sequence may not be necessary. CXL spec states disabling
  643. * the root cmd register's interrupts is required. But, PCI spec
  644. * shows these are disabled by default on reset.
  645. */
  646. aer_cmd_mask = (PCI_ERR_ROOT_CMD_COR_EN |
  647. PCI_ERR_ROOT_CMD_NONFATAL_EN |
  648. PCI_ERR_ROOT_CMD_FATAL_EN);
  649. aer_cmd = readl(aer_base + PCI_ERR_ROOT_COMMAND);
  650. aer_cmd &= ~aer_cmd_mask;
  651. writel(aer_cmd, aer_base + PCI_ERR_ROOT_COMMAND);
  652. }
  653. /**
  654. * cxl_dport_init_ras_reporting - Setup CXL RAS report on this dport
  655. * @dport: the cxl_dport that needs to be initialized
  656. * @host: host device for devm operations
  657. */
  658. void cxl_dport_init_ras_reporting(struct cxl_dport *dport, struct device *host)
  659. {
  660. dport->reg_map.host = host;
  661. cxl_dport_map_ras(dport);
  662. if (dport->rch) {
  663. struct pci_host_bridge *host_bridge = to_pci_host_bridge(dport->dport_dev);
  664. if (!host_bridge->native_aer)
  665. return;
  666. cxl_dport_map_rch_aer(dport);
  667. cxl_disable_rch_root_ints(dport);
  668. }
  669. }
  670. EXPORT_SYMBOL_NS_GPL(cxl_dport_init_ras_reporting, CXL);
  671. static void cxl_handle_rdport_cor_ras(struct cxl_dev_state *cxlds,
  672. struct cxl_dport *dport)
  673. {
  674. return __cxl_handle_cor_ras(cxlds, dport->regs.ras);
  675. }
  676. static bool cxl_handle_rdport_ras(struct cxl_dev_state *cxlds,
  677. struct cxl_dport *dport)
  678. {
  679. return __cxl_handle_ras(cxlds, dport->regs.ras);
  680. }
  681. /*
  682. * Copy the AER capability registers using 32 bit read accesses.
  683. * This is necessary because RCRB AER capability is MMIO mapped. Clear the
  684. * status after copying.
  685. *
  686. * @aer_base: base address of AER capability block in RCRB
  687. * @aer_regs: destination for copying AER capability
  688. */
  689. static bool cxl_rch_get_aer_info(void __iomem *aer_base,
  690. struct aer_capability_regs *aer_regs)
  691. {
  692. int read_cnt = sizeof(struct aer_capability_regs) / sizeof(u32);
  693. u32 *aer_regs_buf = (u32 *)aer_regs;
  694. int n;
  695. if (!aer_base)
  696. return false;
  697. /* Use readl() to guarantee 32-bit accesses */
  698. for (n = 0; n < read_cnt; n++)
  699. aer_regs_buf[n] = readl(aer_base + n * sizeof(u32));
  700. writel(aer_regs->uncor_status, aer_base + PCI_ERR_UNCOR_STATUS);
  701. writel(aer_regs->cor_status, aer_base + PCI_ERR_COR_STATUS);
  702. return true;
  703. }
  704. /* Get AER severity. Return false if there is no error. */
  705. static bool cxl_rch_get_aer_severity(struct aer_capability_regs *aer_regs,
  706. int *severity)
  707. {
  708. if (aer_regs->uncor_status & ~aer_regs->uncor_mask) {
  709. if (aer_regs->uncor_status & PCI_ERR_ROOT_FATAL_RCV)
  710. *severity = AER_FATAL;
  711. else
  712. *severity = AER_NONFATAL;
  713. return true;
  714. }
  715. if (aer_regs->cor_status & ~aer_regs->cor_mask) {
  716. *severity = AER_CORRECTABLE;
  717. return true;
  718. }
  719. return false;
  720. }
  721. static void cxl_handle_rdport_errors(struct cxl_dev_state *cxlds)
  722. {
  723. struct pci_dev *pdev = to_pci_dev(cxlds->dev);
  724. struct aer_capability_regs aer_regs;
  725. struct cxl_dport *dport;
  726. int severity;
  727. struct cxl_port *port __free(put_cxl_port) =
  728. cxl_pci_find_port(pdev, &dport);
  729. if (!port)
  730. return;
  731. if (!cxl_rch_get_aer_info(dport->regs.dport_aer, &aer_regs))
  732. return;
  733. if (!cxl_rch_get_aer_severity(&aer_regs, &severity))
  734. return;
  735. pci_print_aer(pdev, severity, &aer_regs);
  736. if (severity == AER_CORRECTABLE)
  737. cxl_handle_rdport_cor_ras(cxlds, dport);
  738. else
  739. cxl_handle_rdport_ras(cxlds, dport);
  740. }
  741. #else
  742. static void cxl_handle_rdport_errors(struct cxl_dev_state *cxlds) { }
  743. #endif
  744. void cxl_cor_error_detected(struct pci_dev *pdev)
  745. {
  746. struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
  747. struct device *dev = &cxlds->cxlmd->dev;
  748. scoped_guard(device, dev) {
  749. if (!dev->driver) {
  750. dev_warn(&pdev->dev,
  751. "%s: memdev disabled, abort error handling\n",
  752. dev_name(dev));
  753. return;
  754. }
  755. if (cxlds->rcd)
  756. cxl_handle_rdport_errors(cxlds);
  757. cxl_handle_endpoint_cor_ras(cxlds);
  758. }
  759. }
  760. EXPORT_SYMBOL_NS_GPL(cxl_cor_error_detected, CXL);
  761. pci_ers_result_t cxl_error_detected(struct pci_dev *pdev,
  762. pci_channel_state_t state)
  763. {
  764. struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
  765. struct cxl_memdev *cxlmd = cxlds->cxlmd;
  766. struct device *dev = &cxlmd->dev;
  767. bool ue;
  768. scoped_guard(device, dev) {
  769. if (!dev->driver) {
  770. dev_warn(&pdev->dev,
  771. "%s: memdev disabled, abort error handling\n",
  772. dev_name(dev));
  773. return PCI_ERS_RESULT_DISCONNECT;
  774. }
  775. if (cxlds->rcd)
  776. cxl_handle_rdport_errors(cxlds);
  777. /*
  778. * A frozen channel indicates an impending reset which is fatal to
  779. * CXL.mem operation, and will likely crash the system. On the off
  780. * chance the situation is recoverable dump the status of the RAS
  781. * capability registers and bounce the active state of the memdev.
  782. */
  783. ue = cxl_handle_endpoint_ras(cxlds);
  784. }
  785. switch (state) {
  786. case pci_channel_io_normal:
  787. if (ue) {
  788. device_release_driver(dev);
  789. return PCI_ERS_RESULT_NEED_RESET;
  790. }
  791. return PCI_ERS_RESULT_CAN_RECOVER;
  792. case pci_channel_io_frozen:
  793. dev_warn(&pdev->dev,
  794. "%s: frozen state error detected, disable CXL.mem\n",
  795. dev_name(dev));
  796. device_release_driver(dev);
  797. return PCI_ERS_RESULT_NEED_RESET;
  798. case pci_channel_io_perm_failure:
  799. dev_warn(&pdev->dev,
  800. "failure state error detected, request disconnect\n");
  801. return PCI_ERS_RESULT_DISCONNECT;
  802. }
  803. return PCI_ERS_RESULT_NEED_RESET;
  804. }
  805. EXPORT_SYMBOL_NS_GPL(cxl_error_detected, CXL);
  806. static int cxl_flit_size(struct pci_dev *pdev)
  807. {
  808. if (cxl_pci_flit_256(pdev))
  809. return 256;
  810. return 68;
  811. }
  812. /**
  813. * cxl_pci_get_latency - calculate the link latency for the PCIe link
  814. * @pdev: PCI device
  815. *
  816. * return: calculated latency or 0 for no latency
  817. *
  818. * CXL Memory Device SW Guide v1.0 2.11.4 Link latency calculation
  819. * Link latency = LinkPropagationLatency + FlitLatency + RetimerLatency
  820. * LinkProgationLatency is negligible, so 0 will be used
  821. * RetimerLatency is assumed to be negligible and 0 will be used
  822. * FlitLatency = FlitSize / LinkBandwidth
  823. * FlitSize is defined by spec. CXL rev3.0 4.2.1.
  824. * 68B flit is used up to 32GT/s. >32GT/s, 256B flit size is used.
  825. * The FlitLatency is converted to picoseconds.
  826. */
  827. long cxl_pci_get_latency(struct pci_dev *pdev)
  828. {
  829. long bw;
  830. bw = pcie_link_speed_mbps(pdev);
  831. if (bw < 0)
  832. return 0;
  833. bw /= BITS_PER_BYTE;
  834. return cxl_flit_size(pdev) * MEGA / bw;
  835. }
  836. static int __cxl_endpoint_decoder_reset_detected(struct device *dev, void *data)
  837. {
  838. struct cxl_port *port = data;
  839. struct cxl_decoder *cxld;
  840. struct cxl_hdm *cxlhdm;
  841. void __iomem *hdm;
  842. u32 ctrl;
  843. if (!is_endpoint_decoder(dev))
  844. return 0;
  845. cxld = to_cxl_decoder(dev);
  846. if ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)
  847. return 0;
  848. cxlhdm = dev_get_drvdata(&port->dev);
  849. hdm = cxlhdm->regs.hdm_decoder;
  850. ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
  851. return !FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl);
  852. }
  853. bool cxl_endpoint_decoder_reset_detected(struct cxl_port *port)
  854. {
  855. return device_for_each_child(&port->dev, port,
  856. __cxl_endpoint_decoder_reset_detected);
  857. }
  858. EXPORT_SYMBOL_NS_GPL(cxl_endpoint_decoder_reset_detected, CXL);
  859. int cxl_pci_get_bandwidth(struct pci_dev *pdev, struct access_coordinate *c)
  860. {
  861. int speed, bw;
  862. u16 lnksta;
  863. u32 width;
  864. speed = pcie_link_speed_mbps(pdev);
  865. if (speed < 0)
  866. return speed;
  867. speed /= BITS_PER_BYTE;
  868. pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnksta);
  869. width = FIELD_GET(PCI_EXP_LNKSTA_NLW, lnksta);
  870. bw = speed * width;
  871. for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
  872. c[i].read_bandwidth = bw;
  873. c[i].write_bandwidth = bw;
  874. }
  875. return 0;
  876. }