xref: /openbmc/linux/drivers/cxl/cxlmem.h (revision f3956ebb)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright(c) 2020-2021 Intel Corporation. */
3 #ifndef __CXL_MEM_H__
4 #define __CXL_MEM_H__
5 #include <linux/cdev.h>
6 #include "cxl.h"
7 
8 /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */
9 #define CXLMDEV_STATUS_OFFSET 0x0
10 #define   CXLMDEV_DEV_FATAL BIT(0)
11 #define   CXLMDEV_FW_HALT BIT(1)
12 #define   CXLMDEV_STATUS_MEDIA_STATUS_MASK GENMASK(3, 2)
13 #define     CXLMDEV_MS_NOT_READY 0
14 #define     CXLMDEV_MS_READY 1
15 #define     CXLMDEV_MS_ERROR 2
16 #define     CXLMDEV_MS_DISABLED 3
17 #define CXLMDEV_READY(status)                                                  \
18 	(FIELD_GET(CXLMDEV_STATUS_MEDIA_STATUS_MASK, status) ==                \
19 	 CXLMDEV_MS_READY)
20 #define   CXLMDEV_MBOX_IF_READY BIT(4)
21 #define   CXLMDEV_RESET_NEEDED_MASK GENMASK(7, 5)
22 #define     CXLMDEV_RESET_NEEDED_NOT 0
23 #define     CXLMDEV_RESET_NEEDED_COLD 1
24 #define     CXLMDEV_RESET_NEEDED_WARM 2
25 #define     CXLMDEV_RESET_NEEDED_HOT 3
26 #define     CXLMDEV_RESET_NEEDED_CXL 4
27 #define CXLMDEV_RESET_NEEDED(status)                                           \
28 	(FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) !=                       \
29 	 CXLMDEV_RESET_NEEDED_NOT)
30 
31 /**
32  * struct cdevm_file_operations - devm coordinated cdev file operations
33  * @fops: file operations that are synchronized against @shutdown
34  * @shutdown: disconnect driver data
35  *
36  * @shutdown is invoked in the devres release path to disconnect any
37  * driver instance data from @dev. It assumes synchronization with any
38  * fops operation that requires driver data. After @shutdown an
39  * operation may only reference @device data.
40  */
41 struct cdevm_file_operations {
42 	struct file_operations fops;
43 	void (*shutdown)(struct device *dev);
44 };
45 
46 /**
47  * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
48  * @dev: driver core device object
49  * @cdev: char dev core object for ioctl operations
50  * @cxlm: pointer to the parent device driver data
51  * @id: id number of this memdev instance.
52  */
53 struct cxl_memdev {
54 	struct device dev;
55 	struct cdev cdev;
56 	struct cxl_mem *cxlm;
57 	int id;
58 };
59 
60 static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
61 {
62 	return container_of(dev, struct cxl_memdev, dev);
63 }
64 
65 struct cxl_memdev *
66 devm_cxl_add_memdev(struct device *host, struct cxl_mem *cxlm,
67 		    const struct cdevm_file_operations *cdevm_fops);
68 
69 /**
70  * struct cxl_mem - A CXL memory device
71  * @pdev: The PCI device associated with this CXL device.
72  * @cxlmd: Logical memory device chardev / interface
73  * @regs: Parsed register blocks
74  * @payload_size: Size of space for payload
75  *                (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register)
76  * @lsa_size: Size of Label Storage Area
77  *                (CXL 2.0 8.2.9.5.1.1 Identify Memory Device)
78  * @mbox_mutex: Mutex to synchronize mailbox access.
79  * @firmware_version: Firmware version for the memory device.
80  * @enabled_cmds: Hardware commands found enabled in CEL.
81  * @pmem_range: Persistent memory capacity information.
82  * @ram_range: Volatile memory capacity information.
83  */
84 struct cxl_mem {
85 	struct pci_dev *pdev;
86 	struct cxl_memdev *cxlmd;
87 
88 	struct cxl_regs regs;
89 
90 	size_t payload_size;
91 	size_t lsa_size;
92 	struct mutex mbox_mutex; /* Protects device mailbox and firmware */
93 	char firmware_version[0x10];
94 	unsigned long *enabled_cmds;
95 
96 	struct range pmem_range;
97 	struct range ram_range;
98 	u64 total_bytes;
99 	u64 volatile_only_bytes;
100 	u64 persistent_only_bytes;
101 	u64 partition_align_bytes;
102 
103 	u64 active_volatile_bytes;
104 	u64 active_persistent_bytes;
105 	u64 next_volatile_bytes;
106 	u64 next_persistent_bytes;
107 };
108 #endif /* __CXL_MEM_H__ */
109