pci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright(c) 2020 Intel Corporation. All rights reserved. */
  3. #include <linux/io-64-nonatomic-lo-hi.h>
  4. #include <linux/moduleparam.h>
  5. #include <linux/module.h>
  6. #include <linux/delay.h>
  7. #include <linux/sizes.h>
  8. #include <linux/mutex.h>
  9. #include <linux/list.h>
  10. #include <linux/pci.h>
  11. #include <linux/pci-doe.h>
  12. #include <linux/io.h>
  13. #include "cxlmem.h"
  14. #include "cxlpci.h"
  15. #include "cxl.h"
  16. /**
  17. * DOC: cxl pci
  18. *
  19. * This implements the PCI exclusive functionality for a CXL device as it is
  20. * defined by the Compute Express Link specification. CXL devices may surface
  21. * certain functionality even if it isn't CXL enabled. While this driver is
  22. * focused around the PCI specific aspects of a CXL device, it binds to the
  23. * specific CXL memory device class code, and therefore the implementation of
  24. * cxl_pci is focused around CXL memory devices.
  25. *
  26. * The driver has several responsibilities, mainly:
  27. * - Create the memX device and register on the CXL bus.
  28. * - Enumerate device's register interface and map them.
  29. * - Registers nvdimm bridge device with cxl_core.
  30. * - Registers a CXL mailbox with cxl_core.
  31. */
  32. #define cxl_doorbell_busy(cxlds) \
  33. (readl((cxlds)->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET) & \
  34. CXLDEV_MBOX_CTRL_DOORBELL)
  35. /* CXL 2.0 - 8.2.8.4 */
  36. #define CXL_MAILBOX_TIMEOUT_MS (2 * HZ)
  37. /*
  38. * CXL 2.0 ECN "Add Mailbox Ready Time" defines a capability field to
  39. * dictate how long to wait for the mailbox to become ready. The new
  40. * field allows the device to tell software the amount of time to wait
  41. * before mailbox ready. This field per the spec theoretically allows
  42. * for up to 255 seconds. 255 seconds is unreasonably long, its longer
  43. * than the maximum SATA port link recovery wait. Default to 60 seconds
  44. * until someone builds a CXL device that needs more time in practice.
  45. */
  46. static unsigned short mbox_ready_timeout = 60;
  47. module_param(mbox_ready_timeout, ushort, 0644);
  48. MODULE_PARM_DESC(mbox_ready_timeout, "seconds to wait for mailbox ready");
  49. static int cxl_pci_mbox_wait_for_doorbell(struct cxl_dev_state *cxlds)
  50. {
  51. const unsigned long start = jiffies;
  52. unsigned long end = start;
  53. while (cxl_doorbell_busy(cxlds)) {
  54. end = jiffies;
  55. if (time_after(end, start + CXL_MAILBOX_TIMEOUT_MS)) {
  56. /* Check again in case preempted before timeout test */
  57. if (!cxl_doorbell_busy(cxlds))
  58. break;
  59. return -ETIMEDOUT;
  60. }
  61. cpu_relax();
  62. }
  63. dev_dbg(cxlds->dev, "Doorbell wait took %dms",
  64. jiffies_to_msecs(end) - jiffies_to_msecs(start));
  65. return 0;
  66. }
  67. #define cxl_err(dev, status, msg) \
  68. dev_err_ratelimited(dev, msg ", device state %s%s\n", \
  69. status & CXLMDEV_DEV_FATAL ? " fatal" : "", \
  70. status & CXLMDEV_FW_HALT ? " firmware-halt" : "")
  71. #define cxl_cmd_err(dev, cmd, status, msg) \
  72. dev_err_ratelimited(dev, msg " (opcode: %#x), device state %s%s\n", \
  73. (cmd)->opcode, \
  74. status & CXLMDEV_DEV_FATAL ? " fatal" : "", \
  75. status & CXLMDEV_FW_HALT ? " firmware-halt" : "")
  76. /**
  77. * __cxl_pci_mbox_send_cmd() - Execute a mailbox command
  78. * @cxlds: The device state to communicate with.
  79. * @mbox_cmd: Command to send to the memory device.
  80. *
  81. * Context: Any context. Expects mbox_mutex to be held.
  82. * Return: -ETIMEDOUT if timeout occurred waiting for completion. 0 on success.
  83. * Caller should check the return code in @mbox_cmd to make sure it
  84. * succeeded.
  85. *
  86. * This is a generic form of the CXL mailbox send command thus only using the
  87. * registers defined by the mailbox capability ID - CXL 2.0 8.2.8.4. Memory
  88. * devices, and perhaps other types of CXL devices may have further information
  89. * available upon error conditions. Driver facilities wishing to send mailbox
  90. * commands should use the wrapper command.
  91. *
  92. * The CXL spec allows for up to two mailboxes. The intention is for the primary
  93. * mailbox to be OS controlled and the secondary mailbox to be used by system
  94. * firmware. This allows the OS and firmware to communicate with the device and
  95. * not need to coordinate with each other. The driver only uses the primary
  96. * mailbox.
  97. */
  98. static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,
  99. struct cxl_mbox_cmd *mbox_cmd)
  100. {
  101. void __iomem *payload = cxlds->regs.mbox + CXLDEV_MBOX_PAYLOAD_OFFSET;
  102. struct device *dev = cxlds->dev;
  103. u64 cmd_reg, status_reg;
  104. size_t out_len;
  105. int rc;
  106. lockdep_assert_held(&cxlds->mbox_mutex);
  107. /*
  108. * Here are the steps from 8.2.8.4 of the CXL 2.0 spec.
  109. * 1. Caller reads MB Control Register to verify doorbell is clear
  110. * 2. Caller writes Command Register
  111. * 3. Caller writes Command Payload Registers if input payload is non-empty
  112. * 4. Caller writes MB Control Register to set doorbell
  113. * 5. Caller either polls for doorbell to be clear or waits for interrupt if configured
  114. * 6. Caller reads MB Status Register to fetch Return code
  115. * 7. If command successful, Caller reads Command Register to get Payload Length
  116. * 8. If output payload is non-empty, host reads Command Payload Registers
  117. *
  118. * Hardware is free to do whatever it wants before the doorbell is rung,
  119. * and isn't allowed to change anything after it clears the doorbell. As
  120. * such, steps 2 and 3 can happen in any order, and steps 6, 7, 8 can
  121. * also happen in any order (though some orders might not make sense).
  122. */
  123. /* #1 */
  124. if (cxl_doorbell_busy(cxlds)) {
  125. u64 md_status =
  126. readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
  127. cxl_cmd_err(cxlds->dev, mbox_cmd, md_status,
  128. "mailbox queue busy");
  129. return -EBUSY;
  130. }
  131. cmd_reg = FIELD_PREP(CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK,
  132. mbox_cmd->opcode);
  133. if (mbox_cmd->size_in) {
  134. if (WARN_ON(!mbox_cmd->payload_in))
  135. return -EINVAL;
  136. cmd_reg |= FIELD_PREP(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK,
  137. mbox_cmd->size_in);
  138. memcpy_toio(payload, mbox_cmd->payload_in, mbox_cmd->size_in);
  139. }
  140. /* #2, #3 */
  141. writeq(cmd_reg, cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET);
  142. /* #4 */
  143. dev_dbg(dev, "Sending command\n");
  144. writel(CXLDEV_MBOX_CTRL_DOORBELL,
  145. cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);
  146. /* #5 */
  147. rc = cxl_pci_mbox_wait_for_doorbell(cxlds);
  148. if (rc == -ETIMEDOUT) {
  149. u64 md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
  150. cxl_cmd_err(cxlds->dev, mbox_cmd, md_status, "mailbox timeout");
  151. return rc;
  152. }
  153. /* #6 */
  154. status_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_STATUS_OFFSET);
  155. mbox_cmd->return_code =
  156. FIELD_GET(CXLDEV_MBOX_STATUS_RET_CODE_MASK, status_reg);
  157. if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS) {
  158. dev_dbg(dev, "Mailbox operation had an error: %s\n",
  159. cxl_mbox_cmd_rc2str(mbox_cmd));
  160. return 0; /* completed but caller must check return_code */
  161. }
  162. /* #7 */
  163. cmd_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET);
  164. out_len = FIELD_GET(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK, cmd_reg);
  165. /* #8 */
  166. if (out_len && mbox_cmd->payload_out) {
  167. /*
  168. * Sanitize the copy. If hardware misbehaves, out_len per the
  169. * spec can actually be greater than the max allowed size (21
  170. * bits available but spec defined 1M max). The caller also may
  171. * have requested less data than the hardware supplied even
  172. * within spec.
  173. */
  174. size_t n = min3(mbox_cmd->size_out, cxlds->payload_size, out_len);
  175. memcpy_fromio(mbox_cmd->payload_out, payload, n);
  176. mbox_cmd->size_out = n;
  177. } else {
  178. mbox_cmd->size_out = 0;
  179. }
  180. return 0;
  181. }
  182. static int cxl_pci_mbox_send(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd)
  183. {
  184. int rc;
  185. mutex_lock_io(&cxlds->mbox_mutex);
  186. rc = __cxl_pci_mbox_send_cmd(cxlds, cmd);
  187. mutex_unlock(&cxlds->mbox_mutex);
  188. return rc;
  189. }
  190. static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds)
  191. {
  192. const int cap = readl(cxlds->regs.mbox + CXLDEV_MBOX_CAPS_OFFSET);
  193. unsigned long timeout;
  194. u64 md_status;
  195. timeout = jiffies + mbox_ready_timeout * HZ;
  196. do {
  197. md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
  198. if (md_status & CXLMDEV_MBOX_IF_READY)
  199. break;
  200. if (msleep_interruptible(100))
  201. break;
  202. } while (!time_after(jiffies, timeout));
  203. if (!(md_status & CXLMDEV_MBOX_IF_READY)) {
  204. cxl_err(cxlds->dev, md_status,
  205. "timeout awaiting mailbox ready");
  206. return -ETIMEDOUT;
  207. }
  208. /*
  209. * A command may be in flight from a previous driver instance,
  210. * think kexec, do one doorbell wait so that
  211. * __cxl_pci_mbox_send_cmd() can assume that it is the only
  212. * source for future doorbell busy events.
  213. */
  214. if (cxl_pci_mbox_wait_for_doorbell(cxlds) != 0) {
  215. cxl_err(cxlds->dev, md_status, "timeout awaiting mailbox idle");
  216. return -ETIMEDOUT;
  217. }
  218. cxlds->mbox_send = cxl_pci_mbox_send;
  219. cxlds->payload_size =
  220. 1 << FIELD_GET(CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK, cap);
  221. /*
  222. * CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register
  223. *
  224. * If the size is too small, mandatory commands will not work and so
  225. * there's no point in going forward. If the size is too large, there's
  226. * no harm is soft limiting it.
  227. */
  228. cxlds->payload_size = min_t(size_t, cxlds->payload_size, SZ_1M);
  229. if (cxlds->payload_size < 256) {
  230. dev_err(cxlds->dev, "Mailbox is too small (%zub)",
  231. cxlds->payload_size);
  232. return -ENXIO;
  233. }
  234. dev_dbg(cxlds->dev, "Mailbox payload sized %zu",
  235. cxlds->payload_size);
  236. return 0;
  237. }
  238. static int cxl_map_regblock(struct pci_dev *pdev, struct cxl_register_map *map)
  239. {
  240. void __iomem *addr;
  241. int bar = map->barno;
  242. struct device *dev = &pdev->dev;
  243. resource_size_t offset = map->block_offset;
  244. /* Basic sanity check that BAR is big enough */
  245. if (pci_resource_len(pdev, bar) < offset) {
  246. dev_err(dev, "BAR%d: %pr: too small (offset: %pa)\n", bar,
  247. &pdev->resource[bar], &offset);
  248. return -ENXIO;
  249. }
  250. addr = pci_iomap(pdev, bar, 0);
  251. if (!addr) {
  252. dev_err(dev, "failed to map registers\n");
  253. return -ENOMEM;
  254. }
  255. dev_dbg(dev, "Mapped CXL Memory Device resource bar %u @ %pa\n",
  256. bar, &offset);
  257. map->base = addr + map->block_offset;
  258. return 0;
  259. }
  260. static void cxl_unmap_regblock(struct pci_dev *pdev,
  261. struct cxl_register_map *map)
  262. {
  263. pci_iounmap(pdev, map->base - map->block_offset);
  264. map->base = NULL;
  265. }
  266. static int cxl_probe_regs(struct pci_dev *pdev, struct cxl_register_map *map)
  267. {
  268. struct cxl_component_reg_map *comp_map;
  269. struct cxl_device_reg_map *dev_map;
  270. struct device *dev = &pdev->dev;
  271. void __iomem *base = map->base;
  272. switch (map->reg_type) {
  273. case CXL_REGLOC_RBI_COMPONENT:
  274. comp_map = &map->component_map;
  275. cxl_probe_component_regs(dev, base, comp_map);
  276. if (!comp_map->hdm_decoder.valid) {
  277. dev_err(dev, "HDM decoder registers not found\n");
  278. return -ENXIO;
  279. }
  280. dev_dbg(dev, "Set up component registers\n");
  281. break;
  282. case CXL_REGLOC_RBI_MEMDEV:
  283. dev_map = &map->device_map;
  284. cxl_probe_device_regs(dev, base, dev_map);
  285. if (!dev_map->status.valid || !dev_map->mbox.valid ||
  286. !dev_map->memdev.valid) {
  287. dev_err(dev, "registers not found: %s%s%s\n",
  288. !dev_map->status.valid ? "status " : "",
  289. !dev_map->mbox.valid ? "mbox " : "",
  290. !dev_map->memdev.valid ? "memdev " : "");
  291. return -ENXIO;
  292. }
  293. dev_dbg(dev, "Probing device registers...\n");
  294. break;
  295. default:
  296. break;
  297. }
  298. return 0;
  299. }
  300. static int cxl_map_regs(struct cxl_dev_state *cxlds, struct cxl_register_map *map)
  301. {
  302. struct device *dev = cxlds->dev;
  303. struct pci_dev *pdev = to_pci_dev(dev);
  304. switch (map->reg_type) {
  305. case CXL_REGLOC_RBI_COMPONENT:
  306. cxl_map_component_regs(pdev, &cxlds->regs.component, map);
  307. dev_dbg(dev, "Mapping component registers...\n");
  308. break;
  309. case CXL_REGLOC_RBI_MEMDEV:
  310. cxl_map_device_regs(pdev, &cxlds->regs.device_regs, map);
  311. dev_dbg(dev, "Probing device registers...\n");
  312. break;
  313. default:
  314. break;
  315. }
  316. return 0;
  317. }
  318. static int cxl_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,
  319. struct cxl_register_map *map)
  320. {
  321. int rc;
  322. rc = cxl_find_regblock(pdev, type, map);
  323. if (rc)
  324. return rc;
  325. rc = cxl_map_regblock(pdev, map);
  326. if (rc)
  327. return rc;
  328. rc = cxl_probe_regs(pdev, map);
  329. cxl_unmap_regblock(pdev, map);
  330. return rc;
  331. }
  332. static void cxl_pci_destroy_doe(void *mbs)
  333. {
  334. xa_destroy(mbs);
  335. }
  336. static void devm_cxl_pci_create_doe(struct cxl_dev_state *cxlds)
  337. {
  338. struct device *dev = cxlds->dev;
  339. struct pci_dev *pdev = to_pci_dev(dev);
  340. u16 off = 0;
  341. xa_init(&cxlds->doe_mbs);
  342. if (devm_add_action(&pdev->dev, cxl_pci_destroy_doe, &cxlds->doe_mbs)) {
  343. dev_err(dev, "Failed to create XArray for DOE's\n");
  344. return;
  345. }
  346. /*
  347. * Mailbox creation is best effort. Higher layers must determine if
  348. * the lack of a mailbox for their protocol is a device failure or not.
  349. */
  350. pci_doe_for_each_off(pdev, off) {
  351. struct pci_doe_mb *doe_mb;
  352. doe_mb = pcim_doe_create_mb(pdev, off);
  353. if (IS_ERR(doe_mb)) {
  354. dev_err(dev, "Failed to create MB object for MB @ %x\n",
  355. off);
  356. continue;
  357. }
  358. if (xa_insert(&cxlds->doe_mbs, off, doe_mb, GFP_KERNEL)) {
  359. dev_err(dev, "xa_insert failed to insert MB @ %x\n",
  360. off);
  361. continue;
  362. }
  363. dev_dbg(dev, "Created DOE mailbox @%x\n", off);
  364. }
  365. }
  366. static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  367. {
  368. struct cxl_register_map map;
  369. struct cxl_memdev *cxlmd;
  370. struct cxl_dev_state *cxlds;
  371. int rc;
  372. /*
  373. * Double check the anonymous union trickery in struct cxl_regs
  374. * FIXME switch to struct_group()
  375. */
  376. BUILD_BUG_ON(offsetof(struct cxl_regs, memdev) !=
  377. offsetof(struct cxl_regs, device_regs.memdev));
  378. rc = pcim_enable_device(pdev);
  379. if (rc)
  380. return rc;
  381. cxlds = cxl_dev_state_create(&pdev->dev);
  382. if (IS_ERR(cxlds))
  383. return PTR_ERR(cxlds);
  384. cxlds->serial = pci_get_dsn(pdev);
  385. cxlds->cxl_dvsec = pci_find_dvsec_capability(
  386. pdev, PCI_DVSEC_VENDOR_ID_CXL, CXL_DVSEC_PCIE_DEVICE);
  387. if (!cxlds->cxl_dvsec)
  388. dev_warn(&pdev->dev,
  389. "Device DVSEC not present, skip CXL.mem init\n");
  390. rc = cxl_setup_regs(pdev, CXL_REGLOC_RBI_MEMDEV, &map);
  391. if (rc)
  392. return rc;
  393. rc = cxl_map_regs(cxlds, &map);
  394. if (rc)
  395. return rc;
  396. /*
  397. * If the component registers can't be found, the cxl_pci driver may
  398. * still be useful for management functions so don't return an error.
  399. */
  400. cxlds->component_reg_phys = CXL_RESOURCE_NONE;
  401. rc = cxl_setup_regs(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
  402. if (rc)
  403. dev_warn(&pdev->dev, "No component registers (%d)\n", rc);
  404. cxlds->component_reg_phys = cxl_regmap_to_base(pdev, &map);
  405. devm_cxl_pci_create_doe(cxlds);
  406. rc = cxl_pci_setup_mailbox(cxlds);
  407. if (rc)
  408. return rc;
  409. rc = cxl_enumerate_cmds(cxlds);
  410. if (rc)
  411. return rc;
  412. rc = cxl_dev_state_identify(cxlds);
  413. if (rc)
  414. return rc;
  415. rc = cxl_mem_create_range_info(cxlds);
  416. if (rc)
  417. return rc;
  418. cxlmd = devm_cxl_add_memdev(cxlds);
  419. if (IS_ERR(cxlmd))
  420. return PTR_ERR(cxlmd);
  421. if (resource_size(&cxlds->pmem_res) && IS_ENABLED(CONFIG_CXL_PMEM))
  422. rc = devm_cxl_add_nvdimm(&pdev->dev, cxlmd);
  423. return rc;
  424. }
  425. static const struct pci_device_id cxl_mem_pci_tbl[] = {
  426. /* PCI class code for CXL.mem Type-3 Devices */
  427. { PCI_DEVICE_CLASS((PCI_CLASS_MEMORY_CXL << 8 | CXL_MEMORY_PROGIF), ~0)},
  428. { /* terminate list */ },
  429. };
  430. MODULE_DEVICE_TABLE(pci, cxl_mem_pci_tbl);
  431. static struct pci_driver cxl_pci_driver = {
  432. .name = KBUILD_MODNAME,
  433. .id_table = cxl_mem_pci_tbl,
  434. .probe = cxl_pci_probe,
  435. .driver = {
  436. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  437. },
  438. };
  439. MODULE_LICENSE("GPL v2");
  440. module_pci_driver(cxl_pci_driver);
  441. MODULE_IMPORT_NS(CXL);