libnvdimm.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * libnvdimm - Non-volatile-memory Devices Subsystem
  3. *
  4. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of version 2 of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. */
  15. #ifndef __LIBNVDIMM_H__
  16. #define __LIBNVDIMM_H__
  17. #include <linux/kernel.h>
  18. #include <linux/sizes.h>
  19. #include <linux/types.h>
  20. #include <linux/uuid.h>
  21. #include <linux/spinlock.h>
  22. struct badrange_entry {
  23. u64 start;
  24. u64 length;
  25. struct list_head list;
  26. };
  27. struct badrange {
  28. struct list_head list;
  29. spinlock_t lock;
  30. };
  31. enum {
  32. /* when a dimm supports both PMEM and BLK access a label is required */
  33. NDD_ALIASING = 0,
  34. /* unarmed memory devices may not persist writes */
  35. NDD_UNARMED = 1,
  36. /* locked memory devices should not be accessed */
  37. NDD_LOCKED = 2,
  38. /* need to set a limit somewhere, but yes, this is likely overkill */
  39. ND_IOCTL_MAX_BUFLEN = SZ_4M,
  40. ND_CMD_MAX_ELEM = 5,
  41. ND_CMD_MAX_ENVELOPE = 256,
  42. ND_MAX_MAPPINGS = 32,
  43. /* region flag indicating to direct-map persistent memory by default */
  44. ND_REGION_PAGEMAP = 0,
  45. /*
  46. * Platform ensures entire CPU store data path is flushed to pmem on
  47. * system power loss.
  48. */
  49. ND_REGION_PERSIST_CACHE = 1,
  50. /*
  51. * Platform provides mechanisms to automatically flush outstanding
  52. * write data from memory controler to pmem on system power loss.
  53. * (ADR)
  54. */
  55. ND_REGION_PERSIST_MEMCTRL = 2,
  56. /* mark newly adjusted resources as requiring a label update */
  57. DPA_RESOURCE_ADJUSTED = 1 << 0,
  58. };
  59. extern struct attribute_group nvdimm_bus_attribute_group;
  60. extern struct attribute_group nvdimm_attribute_group;
  61. extern struct attribute_group nd_device_attribute_group;
  62. extern struct attribute_group nd_numa_attribute_group;
  63. extern struct attribute_group nd_region_attribute_group;
  64. extern struct attribute_group nd_mapping_attribute_group;
  65. struct nvdimm;
  66. struct nvdimm_bus_descriptor;
  67. typedef int (*ndctl_fn)(struct nvdimm_bus_descriptor *nd_desc,
  68. struct nvdimm *nvdimm, unsigned int cmd, void *buf,
  69. unsigned int buf_len, int *cmd_rc);
  70. struct device_node;
  71. struct nvdimm_bus_descriptor {
  72. const struct attribute_group **attr_groups;
  73. unsigned long bus_dsm_mask;
  74. unsigned long cmd_mask;
  75. struct module *module;
  76. char *provider_name;
  77. struct device_node *of_node;
  78. ndctl_fn ndctl;
  79. int (*flush_probe)(struct nvdimm_bus_descriptor *nd_desc);
  80. int (*clear_to_send)(struct nvdimm_bus_descriptor *nd_desc,
  81. struct nvdimm *nvdimm, unsigned int cmd);
  82. };
  83. struct nd_cmd_desc {
  84. int in_num;
  85. int out_num;
  86. u32 in_sizes[ND_CMD_MAX_ELEM];
  87. int out_sizes[ND_CMD_MAX_ELEM];
  88. };
  89. struct nd_interleave_set {
  90. /* v1.1 definition of the interleave-set-cookie algorithm */
  91. u64 cookie1;
  92. /* v1.2 definition of the interleave-set-cookie algorithm */
  93. u64 cookie2;
  94. /* compatibility with initial buggy Linux implementation */
  95. u64 altcookie;
  96. guid_t type_guid;
  97. };
  98. struct nd_mapping_desc {
  99. struct nvdimm *nvdimm;
  100. u64 start;
  101. u64 size;
  102. int position;
  103. };
  104. struct nd_region_desc {
  105. struct resource *res;
  106. struct nd_mapping_desc *mapping;
  107. u16 num_mappings;
  108. const struct attribute_group **attr_groups;
  109. struct nd_interleave_set *nd_set;
  110. void *provider_data;
  111. int num_lanes;
  112. int numa_node;
  113. unsigned long flags;
  114. struct device_node *of_node;
  115. };
  116. struct device;
  117. void *devm_nvdimm_memremap(struct device *dev, resource_size_t offset,
  118. size_t size, unsigned long flags);
  119. static inline void __iomem *devm_nvdimm_ioremap(struct device *dev,
  120. resource_size_t offset, size_t size)
  121. {
  122. return (void __iomem *) devm_nvdimm_memremap(dev, offset, size, 0);
  123. }
  124. struct nvdimm_bus;
  125. struct module;
  126. struct device;
  127. struct nd_blk_region;
  128. struct nd_blk_region_desc {
  129. int (*enable)(struct nvdimm_bus *nvdimm_bus, struct device *dev);
  130. int (*do_io)(struct nd_blk_region *ndbr, resource_size_t dpa,
  131. void *iobuf, u64 len, int rw);
  132. struct nd_region_desc ndr_desc;
  133. };
  134. static inline struct nd_blk_region_desc *to_blk_region_desc(
  135. struct nd_region_desc *ndr_desc)
  136. {
  137. return container_of(ndr_desc, struct nd_blk_region_desc, ndr_desc);
  138. }
  139. void badrange_init(struct badrange *badrange);
  140. int badrange_add(struct badrange *badrange, u64 addr, u64 length);
  141. void badrange_forget(struct badrange *badrange, phys_addr_t start,
  142. unsigned int len);
  143. int nvdimm_bus_add_badrange(struct nvdimm_bus *nvdimm_bus, u64 addr,
  144. u64 length);
  145. struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
  146. struct nvdimm_bus_descriptor *nfit_desc);
  147. void nvdimm_bus_unregister(struct nvdimm_bus *nvdimm_bus);
  148. struct nvdimm_bus *to_nvdimm_bus(struct device *dev);
  149. struct nvdimm *to_nvdimm(struct device *dev);
  150. struct nd_region *to_nd_region(struct device *dev);
  151. struct device *nd_region_dev(struct nd_region *nd_region);
  152. struct nd_blk_region *to_nd_blk_region(struct device *dev);
  153. struct nvdimm_bus_descriptor *to_nd_desc(struct nvdimm_bus *nvdimm_bus);
  154. struct device *to_nvdimm_bus_dev(struct nvdimm_bus *nvdimm_bus);
  155. const char *nvdimm_name(struct nvdimm *nvdimm);
  156. struct kobject *nvdimm_kobj(struct nvdimm *nvdimm);
  157. unsigned long nvdimm_cmd_mask(struct nvdimm *nvdimm);
  158. void *nvdimm_provider_data(struct nvdimm *nvdimm);
  159. struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
  160. const struct attribute_group **groups, unsigned long flags,
  161. unsigned long cmd_mask, int num_flush,
  162. struct resource *flush_wpq);
  163. const struct nd_cmd_desc *nd_cmd_dimm_desc(int cmd);
  164. const struct nd_cmd_desc *nd_cmd_bus_desc(int cmd);
  165. u32 nd_cmd_in_size(struct nvdimm *nvdimm, int cmd,
  166. const struct nd_cmd_desc *desc, int idx, void *buf);
  167. u32 nd_cmd_out_size(struct nvdimm *nvdimm, int cmd,
  168. const struct nd_cmd_desc *desc, int idx, const u32 *in_field,
  169. const u32 *out_field, unsigned long remainder);
  170. int nvdimm_bus_check_dimm_count(struct nvdimm_bus *nvdimm_bus, int dimm_count);
  171. struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
  172. struct nd_region_desc *ndr_desc);
  173. struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
  174. struct nd_region_desc *ndr_desc);
  175. struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
  176. struct nd_region_desc *ndr_desc);
  177. void *nd_region_provider_data(struct nd_region *nd_region);
  178. void *nd_blk_region_provider_data(struct nd_blk_region *ndbr);
  179. void nd_blk_region_set_provider_data(struct nd_blk_region *ndbr, void *data);
  180. struct nvdimm *nd_blk_region_to_dimm(struct nd_blk_region *ndbr);
  181. unsigned long nd_blk_memremap_flags(struct nd_blk_region *ndbr);
  182. unsigned int nd_region_acquire_lane(struct nd_region *nd_region);
  183. void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane);
  184. u64 nd_fletcher64(void *addr, size_t len, bool le);
  185. void nvdimm_flush(struct nd_region *nd_region);
  186. int nvdimm_has_flush(struct nd_region *nd_region);
  187. int nvdimm_has_cache(struct nd_region *nd_region);
  188. #ifdef CONFIG_ARCH_HAS_PMEM_API
  189. #define ARCH_MEMREMAP_PMEM MEMREMAP_WB
  190. void arch_wb_cache_pmem(void *addr, size_t size);
  191. void arch_invalidate_pmem(void *addr, size_t size);
  192. #else
  193. #define ARCH_MEMREMAP_PMEM MEMREMAP_WT
  194. static inline void arch_wb_cache_pmem(void *addr, size_t size)
  195. {
  196. }
  197. static inline void arch_invalidate_pmem(void *addr, size_t size)
  198. {
  199. }
  200. #endif
  201. #endif /* __LIBNVDIMM_H__ */