1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
8 #include <linux/ndctl.h>
9 #include <linux/device.h>
10 #include <linux/badblocks.h>
11 #include <linux/perf_event.h>
12 #include <linux/platform_device.h>
15 NVDIMM_REVALIDATE_POISON,
16 NVDIMM_REVALIDATE_REGION,
19 enum nvdimm_claim_class {
25 NVDIMM_CCLASS_UNKNOWN,
28 #define NVDIMM_EVENT_VAR(_id) event_attr_##_id
29 #define NVDIMM_EVENT_PTR(_id) (&event_attr_##_id.attr.attr)
31 #define NVDIMM_EVENT_ATTR(_name, _id) \
32 PMU_EVENT_ATTR(_name, NVDIMM_EVENT_VAR(_id), _id, \
33 nvdimm_events_sysfs_show)
35 /* Event attribute array index */
36 #define NVDIMM_PMU_FORMAT_ATTR 0
37 #define NVDIMM_PMU_EVENT_ATTR 1
38 #define NVDIMM_PMU_CPUMASK_ATTR 2
39 #define NVDIMM_PMU_NULL_ATTR 3
42 * struct nvdimm_pmu - data structure for nvdimm perf driver
43 * @pmu: pmu data structure for nvdimm performance stats.
44 * @dev: nvdimm device pointer.
45 * @cpu: designated cpu for counter access.
46 * @node: node for cpu hotplug notifier link.
47 * @cpuhp_state: state for cpu hotplug notification.
48 * @arch_cpumask: cpumask to get designated cpu for counter access.
54 struct hlist_node node;
55 enum cpuhp_state cpuhp_state;
56 /* cpumask provided by arch/platform specific code */
57 struct cpumask arch_cpumask;
60 extern ssize_t nvdimm_events_sysfs_show(struct device *dev,
61 struct device_attribute *attr,
64 int register_nvdimm_pmu(struct nvdimm_pmu *nvdimm, struct platform_device *pdev);
65 void unregister_nvdimm_pmu(struct nvdimm_pmu *nd_pmu);
66 void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu);
67 int perf_pmu_register(struct pmu *pmu, const char *name, int type);
68 void perf_pmu_unregister(struct pmu *pmu);
70 struct nd_device_driver {
71 struct device_driver drv;
73 int (*probe)(struct device *dev);
74 void (*remove)(struct device *dev);
75 void (*shutdown)(struct device *dev);
76 void (*notify)(struct device *dev, enum nvdimm_event event);
79 static inline struct nd_device_driver *to_nd_device_driver(
80 struct device_driver *drv)
82 return container_of(drv, struct nd_device_driver, drv);
86 * struct nd_namespace_common - core infrastructure of a namespace
87 * @force_raw: ignore other personalities for the namespace (e.g. btt)
88 * @dev: device model node
89 * @claim: when set a another personality has taken ownership of the namespace
90 * @claim_class: restrict claim type to a given class
91 * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
93 struct nd_namespace_common {
97 enum nvdimm_claim_class claim_class;
98 int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
99 void *buf, size_t size, int rw, unsigned long flags);
102 static inline struct nd_namespace_common *to_ndns(struct device *dev)
104 return container_of(dev, struct nd_namespace_common, dev);
108 * struct nd_namespace_io - device representation of a persistent memory range
109 * @dev: namespace device created by the nd region driver
110 * @res: struct resource conversion of a NFIT SPA table
111 * @size: cached resource_size(@res) for fast path size checks
112 * @addr: virtual address to access the namespace range
113 * @bb: badblocks list for the namespace range
115 struct nd_namespace_io {
116 struct nd_namespace_common common;
118 resource_size_t size;
124 * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
125 * @nsio: device and system physical address range to drive
126 * @lbasize: logical sector size for the namespace in block-device-mode
127 * @alt_name: namespace name supplied in the dimm label
128 * @uuid: namespace name supplied in the dimm label
129 * @id: ida allocated id
131 struct nd_namespace_pmem {
132 struct nd_namespace_io nsio;
133 unsigned long lbasize;
140 * struct nd_namespace_blk - namespace for dimm-bounded persistent memory
141 * @alt_name: namespace name supplied in the dimm label
142 * @uuid: namespace name supplied in the dimm label
143 * @id: ida allocated id
144 * @lbasize: blk namespaces have a native sector size when btt not present
145 * @size: sum of all the resource ranges allocated to this namespace
146 * @num_resources: number of dpa extents to claim
147 * @res: discontiguous dpa extents for given dimm
149 struct nd_namespace_blk {
150 struct nd_namespace_common common;
154 unsigned long lbasize;
155 resource_size_t size;
157 struct resource **res;
160 static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
162 return container_of(dev, struct nd_namespace_io, common.dev);
165 static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
167 struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
169 return container_of(nsio, struct nd_namespace_pmem, nsio);
172 static inline struct nd_namespace_blk *to_nd_namespace_blk(const struct device *dev)
174 return container_of(dev, struct nd_namespace_blk, common.dev);
178 * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
179 * @ndns: device to read
180 * @offset: namespace-relative starting offset
181 * @buf: buffer to fill
182 * @size: transfer length
184 * @buf is up-to-date upon return from this routine.
186 static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
187 resource_size_t offset, void *buf, size_t size,
190 return ndns->rw_bytes(ndns, offset, buf, size, READ, flags);
194 * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
195 * @ndns: device to write
196 * @offset: namespace-relative starting offset
197 * @buf: buffer to drain
198 * @size: transfer length
200 * NVDIMM Namepaces disks do not implement sectors internally. Depending on
201 * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
202 * or on backing memory media upon return from this routine. Flushing
203 * to media is handled internal to the @ndns driver, if at all.
205 static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
206 resource_size_t offset, void *buf, size_t size,
209 return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags);
212 #define MODULE_ALIAS_ND_DEVICE(type) \
213 MODULE_ALIAS("nd:t" __stringify(type) "*")
214 #define ND_DEVICE_MODALIAS_FMT "nd:t%d"
217 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
218 int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
219 struct module *module, const char *mod_name);
220 static inline void nd_driver_unregister(struct nd_device_driver *drv)
222 driver_unregister(&drv->drv);
224 #define nd_driver_register(driver) \
225 __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
226 #define module_nd_driver(driver) \
227 module_driver(driver, nd_driver_register, nd_driver_unregister)
228 #endif /* __LINUX_ND_H__ */