xref: /linux/drivers/cxl/cxlmem.h (revision 13e7749d06b335774bbb341c65a0232484beb457)
15161a55cSBen Widawsky /* SPDX-License-Identifier: GPL-2.0-only */
25161a55cSBen Widawsky /* Copyright(c) 2020-2021 Intel Corporation. */
35161a55cSBen Widawsky #ifndef __CXL_MEM_H__
45161a55cSBen Widawsky #define __CXL_MEM_H__
55161a55cSBen Widawsky #include <linux/cdev.h>
65161a55cSBen Widawsky #include "cxl.h"
75161a55cSBen Widawsky 
85161a55cSBen Widawsky /* CXL 2.0 8.2.8.5.1.1 Memory Device Status Register */
95161a55cSBen Widawsky #define CXLMDEV_STATUS_OFFSET 0x0
105161a55cSBen Widawsky #define   CXLMDEV_DEV_FATAL BIT(0)
115161a55cSBen Widawsky #define   CXLMDEV_FW_HALT BIT(1)
125161a55cSBen Widawsky #define   CXLMDEV_STATUS_MEDIA_STATUS_MASK GENMASK(3, 2)
135161a55cSBen Widawsky #define     CXLMDEV_MS_NOT_READY 0
145161a55cSBen Widawsky #define     CXLMDEV_MS_READY 1
155161a55cSBen Widawsky #define     CXLMDEV_MS_ERROR 2
165161a55cSBen Widawsky #define     CXLMDEV_MS_DISABLED 3
175161a55cSBen Widawsky #define CXLMDEV_READY(status)                                                  \
185161a55cSBen Widawsky 	(FIELD_GET(CXLMDEV_STATUS_MEDIA_STATUS_MASK, status) ==                \
195161a55cSBen Widawsky 	 CXLMDEV_MS_READY)
205161a55cSBen Widawsky #define   CXLMDEV_MBOX_IF_READY BIT(4)
215161a55cSBen Widawsky #define   CXLMDEV_RESET_NEEDED_MASK GENMASK(7, 5)
225161a55cSBen Widawsky #define     CXLMDEV_RESET_NEEDED_NOT 0
235161a55cSBen Widawsky #define     CXLMDEV_RESET_NEEDED_COLD 1
245161a55cSBen Widawsky #define     CXLMDEV_RESET_NEEDED_WARM 2
255161a55cSBen Widawsky #define     CXLMDEV_RESET_NEEDED_HOT 3
265161a55cSBen Widawsky #define     CXLMDEV_RESET_NEEDED_CXL 4
275161a55cSBen Widawsky #define CXLMDEV_RESET_NEEDED(status)                                           \
285161a55cSBen Widawsky 	(FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) !=                       \
295161a55cSBen Widawsky 	 CXLMDEV_RESET_NEEDED_NOT)
305161a55cSBen Widawsky 
315161a55cSBen Widawsky /**
329cc238c7SDan Williams  * struct cdevm_file_operations - devm coordinated cdev file operations
339cc238c7SDan Williams  * @fops: file operations that are synchronized against @shutdown
349cc238c7SDan Williams  * @shutdown: disconnect driver data
359cc238c7SDan Williams  *
369cc238c7SDan Williams  * @shutdown is invoked in the devres release path to disconnect any
379cc238c7SDan Williams  * driver instance data from @dev. It assumes synchronization with any
389cc238c7SDan Williams  * fops operation that requires driver data. After @shutdown an
399cc238c7SDan Williams  * operation may only reference @device data.
409cc238c7SDan Williams  */
419cc238c7SDan Williams struct cdevm_file_operations {
429cc238c7SDan Williams 	struct file_operations fops;
439cc238c7SDan Williams 	void (*shutdown)(struct device *dev);
449cc238c7SDan Williams };
459cc238c7SDan Williams 
469cc238c7SDan Williams /**
475161a55cSBen Widawsky  * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
485161a55cSBen Widawsky  * @dev: driver core device object
495161a55cSBen Widawsky  * @cdev: char dev core object for ioctl operations
505161a55cSBen Widawsky  * @cxlm: pointer to the parent device driver data
515161a55cSBen Widawsky  * @id: id number of this memdev instance.
525161a55cSBen Widawsky  */
535161a55cSBen Widawsky struct cxl_memdev {
545161a55cSBen Widawsky 	struct device dev;
555161a55cSBen Widawsky 	struct cdev cdev;
565161a55cSBen Widawsky 	struct cxl_mem *cxlm;
575161a55cSBen Widawsky 	int id;
585161a55cSBen Widawsky };
595161a55cSBen Widawsky 
603d135db5SBen Widawsky static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
613d135db5SBen Widawsky {
623d135db5SBen Widawsky 	return container_of(dev, struct cxl_memdev, dev);
633d135db5SBen Widawsky }
643d135db5SBen Widawsky 
653d135db5SBen Widawsky struct cxl_memdev *
6699e222a5SDan Williams devm_cxl_add_memdev(struct cxl_mem *cxlm,
673d135db5SBen Widawsky 		    const struct cdevm_file_operations *cdevm_fops);
683d135db5SBen Widawsky 
695161a55cSBen Widawsky /**
705161a55cSBen Widawsky  * struct cxl_mem - A CXL memory device
7199e222a5SDan Williams  * @dev: The device associated with this CXL device.
725161a55cSBen Widawsky  * @cxlmd: Logical memory device chardev / interface
735161a55cSBen Widawsky  * @regs: Parsed register blocks
745161a55cSBen Widawsky  * @payload_size: Size of space for payload
755161a55cSBen Widawsky  *                (CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register)
765161a55cSBen Widawsky  * @lsa_size: Size of Label Storage Area
775161a55cSBen Widawsky  *                (CXL 2.0 8.2.9.5.1.1 Identify Memory Device)
785161a55cSBen Widawsky  * @mbox_mutex: Mutex to synchronize mailbox access.
795161a55cSBen Widawsky  * @firmware_version: Firmware version for the memory device.
805161a55cSBen Widawsky  * @enabled_cmds: Hardware commands found enabled in CEL.
81*13e7749dSDan Williams  * @pmem_range: Active Persistent memory capacity configuration
82*13e7749dSDan Williams  * @ram_range: Active Volatile memory capacity configuration
83*13e7749dSDan Williams  * @total_bytes: sum of all possible capacities
84*13e7749dSDan Williams  * @volatile_only_bytes: hard volatile capacity
85*13e7749dSDan Williams  * @persistent_only_bytes: hard persistent capacity
86*13e7749dSDan Williams  * @partition_align_bytes: alignment size for partition-able capacity
87*13e7749dSDan Williams  * @active_volatile_bytes: sum of hard + soft volatile
88*13e7749dSDan Williams  * @active_persistent_bytes: sum of hard + soft persistent
89*13e7749dSDan Williams  * @next_volatile_bytes: volatile capacity change pending device reset
90*13e7749dSDan Williams  * @next_persistent_bytes: persistent capacity change pending device reset
91*13e7749dSDan Williams  *
92*13e7749dSDan Williams  * See section 8.2.9.5.2 Capacity Configuration and Label Storage for
93*13e7749dSDan Williams  * details on capacity parameters.
945161a55cSBen Widawsky  */
955161a55cSBen Widawsky struct cxl_mem {
9699e222a5SDan Williams 	struct device *dev;
975161a55cSBen Widawsky 	struct cxl_memdev *cxlmd;
985161a55cSBen Widawsky 
995161a55cSBen Widawsky 	struct cxl_regs regs;
1005161a55cSBen Widawsky 
1015161a55cSBen Widawsky 	size_t payload_size;
1025161a55cSBen Widawsky 	size_t lsa_size;
1035161a55cSBen Widawsky 	struct mutex mbox_mutex; /* Protects device mailbox and firmware */
1045161a55cSBen Widawsky 	char firmware_version[0x10];
1055161a55cSBen Widawsky 	unsigned long *enabled_cmds;
1065161a55cSBen Widawsky 
1075161a55cSBen Widawsky 	struct range pmem_range;
1085161a55cSBen Widawsky 	struct range ram_range;
1090b9159d0SIra Weiny 	u64 total_bytes;
1100b9159d0SIra Weiny 	u64 volatile_only_bytes;
1110b9159d0SIra Weiny 	u64 persistent_only_bytes;
1120b9159d0SIra Weiny 	u64 partition_align_bytes;
113f847502aSIra Weiny 
114f847502aSIra Weiny 	u64 active_volatile_bytes;
115f847502aSIra Weiny 	u64 active_persistent_bytes;
116f847502aSIra Weiny 	u64 next_volatile_bytes;
117f847502aSIra Weiny 	u64 next_persistent_bytes;
1185161a55cSBen Widawsky };
1195161a55cSBen Widawsky #endif /* __CXL_MEM_H__ */
120