drivers/nvdimm: Add nvdimm pmu structure
[linux-2.6-microblaze.git] / include / linux / nd.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
4  */
5 #ifndef __LINUX_ND_H__
6 #define __LINUX_ND_H__
7 #include <linux/fs.h>
8 #include <linux/ndctl.h>
9 #include <linux/device.h>
10 #include <linux/badblocks.h>
11 #include <linux/perf_event.h>
12
13 enum nvdimm_event {
14         NVDIMM_REVALIDATE_POISON,
15         NVDIMM_REVALIDATE_REGION,
16 };
17
18 enum nvdimm_claim_class {
19         NVDIMM_CCLASS_NONE,
20         NVDIMM_CCLASS_BTT,
21         NVDIMM_CCLASS_BTT2,
22         NVDIMM_CCLASS_PFN,
23         NVDIMM_CCLASS_DAX,
24         NVDIMM_CCLASS_UNKNOWN,
25 };
26
27 /**
28  * struct nvdimm_pmu - data structure for nvdimm perf driver
29  * @pmu: pmu data structure for nvdimm performance stats.
30  * @dev: nvdimm device pointer.
31  * @cpu: designated cpu for counter access.
32  * @node: node for cpu hotplug notifier link.
33  * @cpuhp_state: state for cpu hotplug notification.
34  * @arch_cpumask: cpumask to get designated cpu for counter access.
35  */
36 struct nvdimm_pmu {
37         struct pmu pmu;
38         struct device *dev;
39         int cpu;
40         struct hlist_node node;
41         enum cpuhp_state cpuhp_state;
42         /* cpumask provided by arch/platform specific code */
43         struct cpumask arch_cpumask;
44 };
45
46 struct nd_device_driver {
47         struct device_driver drv;
48         unsigned long type;
49         int (*probe)(struct device *dev);
50         void (*remove)(struct device *dev);
51         void (*shutdown)(struct device *dev);
52         void (*notify)(struct device *dev, enum nvdimm_event event);
53 };
54
55 static inline struct nd_device_driver *to_nd_device_driver(
56                 struct device_driver *drv)
57 {
58         return container_of(drv, struct nd_device_driver, drv);
59 };
60
61 /**
62  * struct nd_namespace_common - core infrastructure of a namespace
63  * @force_raw: ignore other personalities for the namespace (e.g. btt)
64  * @dev: device model node
65  * @claim: when set a another personality has taken ownership of the namespace
66  * @claim_class: restrict claim type to a given class
67  * @rw_bytes: access the raw namespace capacity with byte-aligned transfers
68  */
69 struct nd_namespace_common {
70         int force_raw;
71         struct device dev;
72         struct device *claim;
73         enum nvdimm_claim_class claim_class;
74         int (*rw_bytes)(struct nd_namespace_common *, resource_size_t offset,
75                         void *buf, size_t size, int rw, unsigned long flags);
76 };
77
78 static inline struct nd_namespace_common *to_ndns(struct device *dev)
79 {
80         return container_of(dev, struct nd_namespace_common, dev);
81 }
82
83 /**
84  * struct nd_namespace_io - device representation of a persistent memory range
85  * @dev: namespace device created by the nd region driver
86  * @res: struct resource conversion of a NFIT SPA table
87  * @size: cached resource_size(@res) for fast path size checks
88  * @addr: virtual address to access the namespace range
89  * @bb: badblocks list for the namespace range
90  */
91 struct nd_namespace_io {
92         struct nd_namespace_common common;
93         struct resource res;
94         resource_size_t size;
95         void *addr;
96         struct badblocks bb;
97 };
98
99 /**
100  * struct nd_namespace_pmem - namespace device for dimm-backed interleaved memory
101  * @nsio: device and system physical address range to drive
102  * @lbasize: logical sector size for the namespace in block-device-mode
103  * @alt_name: namespace name supplied in the dimm label
104  * @uuid: namespace name supplied in the dimm label
105  * @id: ida allocated id
106  */
107 struct nd_namespace_pmem {
108         struct nd_namespace_io nsio;
109         unsigned long lbasize;
110         char *alt_name;
111         uuid_t *uuid;
112         int id;
113 };
114
115 /**
116  * struct nd_namespace_blk - namespace for dimm-bounded persistent memory
117  * @alt_name: namespace name supplied in the dimm label
118  * @uuid: namespace name supplied in the dimm label
119  * @id: ida allocated id
120  * @lbasize: blk namespaces have a native sector size when btt not present
121  * @size: sum of all the resource ranges allocated to this namespace
122  * @num_resources: number of dpa extents to claim
123  * @res: discontiguous dpa extents for given dimm
124  */
125 struct nd_namespace_blk {
126         struct nd_namespace_common common;
127         char *alt_name;
128         uuid_t *uuid;
129         int id;
130         unsigned long lbasize;
131         resource_size_t size;
132         int num_resources;
133         struct resource **res;
134 };
135
136 static inline struct nd_namespace_io *to_nd_namespace_io(const struct device *dev)
137 {
138         return container_of(dev, struct nd_namespace_io, common.dev);
139 }
140
141 static inline struct nd_namespace_pmem *to_nd_namespace_pmem(const struct device *dev)
142 {
143         struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
144
145         return container_of(nsio, struct nd_namespace_pmem, nsio);
146 }
147
148 static inline struct nd_namespace_blk *to_nd_namespace_blk(const struct device *dev)
149 {
150         return container_of(dev, struct nd_namespace_blk, common.dev);
151 }
152
153 /**
154  * nvdimm_read_bytes() - synchronously read bytes from an nvdimm namespace
155  * @ndns: device to read
156  * @offset: namespace-relative starting offset
157  * @buf: buffer to fill
158  * @size: transfer length
159  *
160  * @buf is up-to-date upon return from this routine.
161  */
162 static inline int nvdimm_read_bytes(struct nd_namespace_common *ndns,
163                 resource_size_t offset, void *buf, size_t size,
164                 unsigned long flags)
165 {
166         return ndns->rw_bytes(ndns, offset, buf, size, READ, flags);
167 }
168
169 /**
170  * nvdimm_write_bytes() - synchronously write bytes to an nvdimm namespace
171  * @ndns: device to write
172  * @offset: namespace-relative starting offset
173  * @buf: buffer to drain
174  * @size: transfer length
175  *
176  * NVDIMM Namepaces disks do not implement sectors internally.  Depending on
177  * the @ndns, the contents of @buf may be in cpu cache, platform buffers,
178  * or on backing memory media upon return from this routine.  Flushing
179  * to media is handled internal to the @ndns driver, if at all.
180  */
181 static inline int nvdimm_write_bytes(struct nd_namespace_common *ndns,
182                 resource_size_t offset, void *buf, size_t size,
183                 unsigned long flags)
184 {
185         return ndns->rw_bytes(ndns, offset, buf, size, WRITE, flags);
186 }
187
188 #define MODULE_ALIAS_ND_DEVICE(type) \
189         MODULE_ALIAS("nd:t" __stringify(type) "*")
190 #define ND_DEVICE_MODALIAS_FMT "nd:t%d"
191
192 struct nd_region;
193 void nvdimm_region_notify(struct nd_region *nd_region, enum nvdimm_event event);
194 int __must_check __nd_driver_register(struct nd_device_driver *nd_drv,
195                 struct module *module, const char *mod_name);
196 static inline void nd_driver_unregister(struct nd_device_driver *drv)
197 {
198         driver_unregister(&drv->drv);
199 }
200 #define nd_driver_register(driver) \
201         __nd_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
202 #define module_nd_driver(driver) \
203         module_driver(driver, nd_driver_register, nd_driver_unregister)
204 #endif /* __LINUX_ND_H__ */