region_devs.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/scatterlist.h>
  6. #include <linux/memregion.h>
  7. #include <linux/highmem.h>
  8. #include <linux/kstrtox.h>
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/hash.h>
  12. #include <linux/sort.h>
  13. #include <linux/io.h>
  14. #include <linux/nd.h>
  15. #include "nd-core.h"
  16. #include "nd.h"
  17. /*
  18. * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
  19. * irrelevant.
  20. */
  21. #include <linux/io-64-nonatomic-hi-lo.h>
  22. static DEFINE_PER_CPU(int, flush_idx);
  23. static int nvdimm_map_flush(struct device *dev, struct nvdimm *nvdimm, int dimm,
  24. struct nd_region_data *ndrd)
  25. {
  26. int i, j;
  27. dev_dbg(dev, "%s: map %d flush address%s\n", nvdimm_name(nvdimm),
  28. nvdimm->num_flush, nvdimm->num_flush == 1 ? "" : "es");
  29. for (i = 0; i < (1 << ndrd->hints_shift); i++) {
  30. struct resource *res = &nvdimm->flush_wpq[i];
  31. unsigned long pfn = PHYS_PFN(res->start);
  32. void __iomem *flush_page;
  33. /* check if flush hints share a page */
  34. for (j = 0; j < i; j++) {
  35. struct resource *res_j = &nvdimm->flush_wpq[j];
  36. unsigned long pfn_j = PHYS_PFN(res_j->start);
  37. if (pfn == pfn_j)
  38. break;
  39. }
  40. if (j < i)
  41. flush_page = (void __iomem *) ((unsigned long)
  42. ndrd_get_flush_wpq(ndrd, dimm, j)
  43. & PAGE_MASK);
  44. else
  45. flush_page = devm_nvdimm_ioremap(dev,
  46. PFN_PHYS(pfn), PAGE_SIZE);
  47. if (!flush_page)
  48. return -ENXIO;
  49. ndrd_set_flush_wpq(ndrd, dimm, i, flush_page
  50. + (res->start & ~PAGE_MASK));
  51. }
  52. return 0;
  53. }
  54. static int nd_region_invalidate_memregion(struct nd_region *nd_region)
  55. {
  56. int i, incoherent = 0;
  57. for (i = 0; i < nd_region->ndr_mappings; i++) {
  58. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  59. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  60. if (test_bit(NDD_INCOHERENT, &nvdimm->flags)) {
  61. incoherent++;
  62. break;
  63. }
  64. }
  65. if (!incoherent)
  66. return 0;
  67. if (!cpu_cache_has_invalidate_memregion()) {
  68. if (IS_ENABLED(CONFIG_NVDIMM_SECURITY_TEST)) {
  69. dev_warn(
  70. &nd_region->dev,
  71. "Bypassing cpu_cache_invalidate_memergion() for testing!\n");
  72. goto out;
  73. } else {
  74. dev_err(&nd_region->dev,
  75. "Failed to synchronize CPU cache state\n");
  76. return -ENXIO;
  77. }
  78. }
  79. cpu_cache_invalidate_all();
  80. out:
  81. for (i = 0; i < nd_region->ndr_mappings; i++) {
  82. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  83. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  84. clear_bit(NDD_INCOHERENT, &nvdimm->flags);
  85. }
  86. return 0;
  87. }
  88. static int get_flush_data(struct nd_region *nd_region, size_t *size, int *num_flush)
  89. {
  90. size_t flush_data_size = sizeof(void *);
  91. int _num_flush = 0;
  92. int i;
  93. guard(nvdimm_bus)(&nd_region->dev);
  94. for (i = 0; i < nd_region->ndr_mappings; i++) {
  95. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  96. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  97. if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags))
  98. return -EBUSY;
  99. /* at least one null hint slot per-dimm for the "no-hint" case */
  100. flush_data_size += sizeof(void *);
  101. _num_flush = min_not_zero(_num_flush, nvdimm->num_flush);
  102. if (!nvdimm->num_flush)
  103. continue;
  104. flush_data_size += nvdimm->num_flush * sizeof(void *);
  105. }
  106. *size = flush_data_size;
  107. *num_flush = _num_flush;
  108. return 0;
  109. }
  110. int nd_region_activate(struct nd_region *nd_region)
  111. {
  112. int i, j, rc, num_flush;
  113. struct nd_region_data *ndrd;
  114. struct device *dev = &nd_region->dev;
  115. size_t flush_data_size;
  116. rc = get_flush_data(nd_region, &flush_data_size, &num_flush);
  117. if (rc)
  118. return rc;
  119. rc = nd_region_invalidate_memregion(nd_region);
  120. if (rc)
  121. return rc;
  122. ndrd = devm_kzalloc(dev, sizeof(*ndrd) + flush_data_size, GFP_KERNEL);
  123. if (!ndrd)
  124. return -ENOMEM;
  125. dev_set_drvdata(dev, ndrd);
  126. if (!num_flush)
  127. return 0;
  128. ndrd->hints_shift = ilog2(num_flush);
  129. for (i = 0; i < nd_region->ndr_mappings; i++) {
  130. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  131. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  132. int rc = nvdimm_map_flush(&nd_region->dev, nvdimm, i, ndrd);
  133. if (rc)
  134. return rc;
  135. }
  136. /*
  137. * Clear out entries that are duplicates. This should prevent the
  138. * extra flushings.
  139. */
  140. for (i = 0; i < nd_region->ndr_mappings - 1; i++) {
  141. /* ignore if NULL already */
  142. if (!ndrd_get_flush_wpq(ndrd, i, 0))
  143. continue;
  144. for (j = i + 1; j < nd_region->ndr_mappings; j++)
  145. if (ndrd_get_flush_wpq(ndrd, i, 0) ==
  146. ndrd_get_flush_wpq(ndrd, j, 0))
  147. ndrd_set_flush_wpq(ndrd, j, 0, NULL);
  148. }
  149. return 0;
  150. }
  151. static void nd_region_release(struct device *dev)
  152. {
  153. struct nd_region *nd_region = to_nd_region(dev);
  154. u16 i;
  155. for (i = 0; i < nd_region->ndr_mappings; i++) {
  156. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  157. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  158. put_device(&nvdimm->dev);
  159. }
  160. free_percpu(nd_region->lane);
  161. if (!test_bit(ND_REGION_CXL, &nd_region->flags))
  162. memregion_free(nd_region->id);
  163. kfree(nd_region);
  164. }
  165. struct nd_region *to_nd_region(struct device *dev)
  166. {
  167. struct nd_region *nd_region = container_of(dev, struct nd_region, dev);
  168. WARN_ON(dev->type->release != nd_region_release);
  169. return nd_region;
  170. }
  171. EXPORT_SYMBOL_GPL(to_nd_region);
  172. struct device *nd_region_dev(struct nd_region *nd_region)
  173. {
  174. if (!nd_region)
  175. return NULL;
  176. return &nd_region->dev;
  177. }
  178. EXPORT_SYMBOL_GPL(nd_region_dev);
  179. void *nd_region_provider_data(struct nd_region *nd_region)
  180. {
  181. return nd_region->provider_data;
  182. }
  183. EXPORT_SYMBOL_GPL(nd_region_provider_data);
  184. /**
  185. * nd_region_to_nstype() - region to an integer namespace type
  186. * @nd_region: region-device to interrogate
  187. *
  188. * This is the 'nstype' attribute of a region as well, an input to the
  189. * MODALIAS for namespace devices, and bit number for a nvdimm_bus to match
  190. * namespace devices with namespace drivers.
  191. */
  192. int nd_region_to_nstype(struct nd_region *nd_region)
  193. {
  194. if (is_memory(&nd_region->dev)) {
  195. u16 i, label;
  196. for (i = 0, label = 0; i < nd_region->ndr_mappings; i++) {
  197. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  198. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  199. if (test_bit(NDD_LABELING, &nvdimm->flags))
  200. label++;
  201. }
  202. if (label)
  203. return ND_DEVICE_NAMESPACE_PMEM;
  204. else
  205. return ND_DEVICE_NAMESPACE_IO;
  206. }
  207. return 0;
  208. }
  209. EXPORT_SYMBOL(nd_region_to_nstype);
  210. static unsigned long long region_size(struct nd_region *nd_region)
  211. {
  212. if (is_memory(&nd_region->dev)) {
  213. return nd_region->ndr_size;
  214. } else if (nd_region->ndr_mappings == 1) {
  215. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  216. return nd_mapping->size;
  217. }
  218. return 0;
  219. }
  220. static ssize_t size_show(struct device *dev,
  221. struct device_attribute *attr, char *buf)
  222. {
  223. struct nd_region *nd_region = to_nd_region(dev);
  224. return sprintf(buf, "%llu\n", region_size(nd_region));
  225. }
  226. static DEVICE_ATTR_RO(size);
  227. static ssize_t deep_flush_show(struct device *dev,
  228. struct device_attribute *attr, char *buf)
  229. {
  230. struct nd_region *nd_region = to_nd_region(dev);
  231. /*
  232. * NOTE: in the nvdimm_has_flush() error case this attribute is
  233. * not visible.
  234. */
  235. return sprintf(buf, "%d\n", nvdimm_has_flush(nd_region));
  236. }
  237. static ssize_t deep_flush_store(struct device *dev, struct device_attribute *attr,
  238. const char *buf, size_t len)
  239. {
  240. bool flush;
  241. int rc = kstrtobool(buf, &flush);
  242. struct nd_region *nd_region = to_nd_region(dev);
  243. if (rc)
  244. return rc;
  245. if (!flush)
  246. return -EINVAL;
  247. rc = nvdimm_flush(nd_region, NULL);
  248. if (rc)
  249. return rc;
  250. return len;
  251. }
  252. static DEVICE_ATTR_RW(deep_flush);
  253. static ssize_t mappings_show(struct device *dev,
  254. struct device_attribute *attr, char *buf)
  255. {
  256. struct nd_region *nd_region = to_nd_region(dev);
  257. return sprintf(buf, "%d\n", nd_region->ndr_mappings);
  258. }
  259. static DEVICE_ATTR_RO(mappings);
  260. static ssize_t nstype_show(struct device *dev,
  261. struct device_attribute *attr, char *buf)
  262. {
  263. struct nd_region *nd_region = to_nd_region(dev);
  264. return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
  265. }
  266. static DEVICE_ATTR_RO(nstype);
  267. static ssize_t set_cookie_show(struct device *dev,
  268. struct device_attribute *attr, char *buf)
  269. {
  270. struct nd_region *nd_region = to_nd_region(dev);
  271. struct nd_interleave_set *nd_set = nd_region->nd_set;
  272. ssize_t rc = 0;
  273. if (is_memory(dev) && nd_set)
  274. /* pass, should be precluded by region_visible */;
  275. else
  276. return -ENXIO;
  277. /*
  278. * The cookie to show depends on which specification of the
  279. * labels we are using. If there are not labels then default to
  280. * the v1.1 namespace label cookie definition. To read all this
  281. * data we need to wait for probing to settle.
  282. */
  283. guard(device)(dev);
  284. guard(nvdimm_bus)(dev);
  285. wait_nvdimm_bus_probe_idle(dev);
  286. if (nd_region->ndr_mappings) {
  287. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  288. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  289. if (ndd) {
  290. struct nd_namespace_index *nsindex;
  291. nsindex = to_namespace_index(ndd, ndd->ns_current);
  292. rc = sprintf(buf, "%#llx\n",
  293. nd_region_interleave_set_cookie(nd_region,
  294. nsindex));
  295. }
  296. }
  297. if (rc)
  298. return rc;
  299. return sprintf(buf, "%#llx\n", nd_set->cookie1);
  300. }
  301. static DEVICE_ATTR_RO(set_cookie);
  302. resource_size_t nd_region_available_dpa(struct nd_region *nd_region)
  303. {
  304. resource_size_t available;
  305. int i;
  306. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  307. available = 0;
  308. for (i = 0; i < nd_region->ndr_mappings; i++) {
  309. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  310. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  311. /* if a dimm is disabled the available capacity is zero */
  312. if (!ndd)
  313. return 0;
  314. available += nd_pmem_available_dpa(nd_region, nd_mapping);
  315. }
  316. return available;
  317. }
  318. resource_size_t nd_region_allocatable_dpa(struct nd_region *nd_region)
  319. {
  320. resource_size_t avail = 0;
  321. int i;
  322. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  323. for (i = 0; i < nd_region->ndr_mappings; i++) {
  324. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  325. avail = min_not_zero(avail, nd_pmem_max_contiguous_dpa(
  326. nd_region, nd_mapping));
  327. }
  328. return avail * nd_region->ndr_mappings;
  329. }
  330. static ssize_t available_size_show(struct device *dev,
  331. struct device_attribute *attr, char *buf)
  332. {
  333. struct nd_region *nd_region = to_nd_region(dev);
  334. /*
  335. * Flush in-flight updates and grab a snapshot of the available
  336. * size. Of course, this value is potentially invalidated the
  337. * memory nvdimm_bus_lock() is dropped, but that's userspace's
  338. * problem to not race itself.
  339. */
  340. guard(device)(dev);
  341. guard(nvdimm_bus)(dev);
  342. wait_nvdimm_bus_probe_idle(dev);
  343. return sprintf(buf, "%llu\n", nd_region_available_dpa(nd_region));
  344. }
  345. static DEVICE_ATTR_RO(available_size);
  346. static ssize_t max_available_extent_show(struct device *dev,
  347. struct device_attribute *attr, char *buf)
  348. {
  349. struct nd_region *nd_region = to_nd_region(dev);
  350. guard(device)(dev);
  351. guard(nvdimm_bus)(dev);
  352. wait_nvdimm_bus_probe_idle(dev);
  353. return sprintf(buf, "%llu\n", nd_region_allocatable_dpa(nd_region));
  354. }
  355. static DEVICE_ATTR_RO(max_available_extent);
  356. static ssize_t init_namespaces_show(struct device *dev,
  357. struct device_attribute *attr, char *buf)
  358. {
  359. struct nd_region_data *ndrd = dev_get_drvdata(dev);
  360. guard(nvdimm_bus)(dev);
  361. if (!ndrd)
  362. return -ENXIO;
  363. return sprintf(buf, "%d/%d\n", ndrd->ns_active, ndrd->ns_count);
  364. }
  365. static DEVICE_ATTR_RO(init_namespaces);
  366. static ssize_t namespace_seed_show(struct device *dev,
  367. struct device_attribute *attr, char *buf)
  368. {
  369. struct nd_region *nd_region = to_nd_region(dev);
  370. guard(nvdimm_bus)(dev);
  371. if (nd_region->ns_seed)
  372. return sprintf(buf, "%s\n", dev_name(nd_region->ns_seed));
  373. return sprintf(buf, "\n");
  374. }
  375. static DEVICE_ATTR_RO(namespace_seed);
  376. static ssize_t btt_seed_show(struct device *dev,
  377. struct device_attribute *attr, char *buf)
  378. {
  379. struct nd_region *nd_region = to_nd_region(dev);
  380. guard(nvdimm_bus)(dev);
  381. if (nd_region->btt_seed)
  382. return sprintf(buf, "%s\n", dev_name(nd_region->btt_seed));
  383. return sprintf(buf, "\n");
  384. }
  385. static DEVICE_ATTR_RO(btt_seed);
  386. static ssize_t pfn_seed_show(struct device *dev,
  387. struct device_attribute *attr, char *buf)
  388. {
  389. struct nd_region *nd_region = to_nd_region(dev);
  390. guard(nvdimm_bus)(dev);
  391. if (nd_region->pfn_seed)
  392. return sprintf(buf, "%s\n", dev_name(nd_region->pfn_seed));
  393. return sprintf(buf, "\n");
  394. }
  395. static DEVICE_ATTR_RO(pfn_seed);
  396. static ssize_t dax_seed_show(struct device *dev,
  397. struct device_attribute *attr, char *buf)
  398. {
  399. struct nd_region *nd_region = to_nd_region(dev);
  400. guard(nvdimm_bus)(dev);
  401. if (nd_region->dax_seed)
  402. return sprintf(buf, "%s\n", dev_name(nd_region->dax_seed));
  403. return sprintf(buf, "\n");
  404. }
  405. static DEVICE_ATTR_RO(dax_seed);
  406. static ssize_t read_only_show(struct device *dev,
  407. struct device_attribute *attr, char *buf)
  408. {
  409. struct nd_region *nd_region = to_nd_region(dev);
  410. return sprintf(buf, "%d\n", nd_region->ro);
  411. }
  412. static int revalidate_read_only(struct device *dev, void *data)
  413. {
  414. nd_device_notify(dev, NVDIMM_REVALIDATE_REGION);
  415. return 0;
  416. }
  417. static ssize_t read_only_store(struct device *dev,
  418. struct device_attribute *attr, const char *buf, size_t len)
  419. {
  420. bool ro;
  421. int rc = kstrtobool(buf, &ro);
  422. struct nd_region *nd_region = to_nd_region(dev);
  423. if (rc)
  424. return rc;
  425. nd_region->ro = ro;
  426. device_for_each_child(dev, NULL, revalidate_read_only);
  427. return len;
  428. }
  429. static DEVICE_ATTR_RW(read_only);
  430. static ssize_t align_show(struct device *dev,
  431. struct device_attribute *attr, char *buf)
  432. {
  433. struct nd_region *nd_region = to_nd_region(dev);
  434. return sprintf(buf, "%#lx\n", nd_region->align);
  435. }
  436. static ssize_t align_store(struct device *dev,
  437. struct device_attribute *attr, const char *buf, size_t len)
  438. {
  439. struct nd_region *nd_region = to_nd_region(dev);
  440. unsigned long val, dpa;
  441. u32 mappings, remainder;
  442. int rc;
  443. rc = kstrtoul(buf, 0, &val);
  444. if (rc)
  445. return rc;
  446. /*
  447. * Ensure space-align is evenly divisible by the region
  448. * interleave-width because the kernel typically has no facility
  449. * to determine which DIMM(s), dimm-physical-addresses, would
  450. * contribute to the tail capacity in system-physical-address
  451. * space for the namespace.
  452. */
  453. mappings = max_t(u32, 1, nd_region->ndr_mappings);
  454. dpa = div_u64_rem(val, mappings, &remainder);
  455. if (!is_power_of_2(dpa) || dpa < PAGE_SIZE
  456. || val > region_size(nd_region) || remainder)
  457. return -EINVAL;
  458. /*
  459. * Given that space allocation consults this value multiple
  460. * times ensure it does not change for the duration of the
  461. * allocation.
  462. */
  463. guard(nvdimm_bus)(dev);
  464. nd_region->align = val;
  465. return len;
  466. }
  467. static DEVICE_ATTR_RW(align);
  468. static ssize_t region_badblocks_show(struct device *dev,
  469. struct device_attribute *attr, char *buf)
  470. {
  471. struct nd_region *nd_region = to_nd_region(dev);
  472. ssize_t rc;
  473. device_lock(dev);
  474. if (dev->driver)
  475. rc = badblocks_show(&nd_region->bb, buf, 0);
  476. else
  477. rc = -ENXIO;
  478. device_unlock(dev);
  479. return rc;
  480. }
  481. static DEVICE_ATTR(badblocks, 0444, region_badblocks_show, NULL);
  482. static ssize_t resource_show(struct device *dev,
  483. struct device_attribute *attr, char *buf)
  484. {
  485. struct nd_region *nd_region = to_nd_region(dev);
  486. return sprintf(buf, "%#llx\n", nd_region->ndr_start);
  487. }
  488. static DEVICE_ATTR_ADMIN_RO(resource);
  489. static ssize_t persistence_domain_show(struct device *dev,
  490. struct device_attribute *attr, char *buf)
  491. {
  492. struct nd_region *nd_region = to_nd_region(dev);
  493. if (test_bit(ND_REGION_PERSIST_CACHE, &nd_region->flags))
  494. return sprintf(buf, "cpu_cache\n");
  495. else if (test_bit(ND_REGION_PERSIST_MEMCTRL, &nd_region->flags))
  496. return sprintf(buf, "memory_controller\n");
  497. else
  498. return sprintf(buf, "\n");
  499. }
  500. static DEVICE_ATTR_RO(persistence_domain);
  501. static struct attribute *nd_region_attributes[] = {
  502. &dev_attr_size.attr,
  503. &dev_attr_align.attr,
  504. &dev_attr_nstype.attr,
  505. &dev_attr_mappings.attr,
  506. &dev_attr_btt_seed.attr,
  507. &dev_attr_pfn_seed.attr,
  508. &dev_attr_dax_seed.attr,
  509. &dev_attr_deep_flush.attr,
  510. &dev_attr_read_only.attr,
  511. &dev_attr_set_cookie.attr,
  512. &dev_attr_available_size.attr,
  513. &dev_attr_max_available_extent.attr,
  514. &dev_attr_namespace_seed.attr,
  515. &dev_attr_init_namespaces.attr,
  516. &dev_attr_badblocks.attr,
  517. &dev_attr_resource.attr,
  518. &dev_attr_persistence_domain.attr,
  519. NULL,
  520. };
  521. static umode_t region_visible(struct kobject *kobj, struct attribute *a, int n)
  522. {
  523. struct device *dev = container_of(kobj, typeof(*dev), kobj);
  524. struct nd_region *nd_region = to_nd_region(dev);
  525. struct nd_interleave_set *nd_set = nd_region->nd_set;
  526. int type = nd_region_to_nstype(nd_region);
  527. if (!is_memory(dev) && a == &dev_attr_pfn_seed.attr)
  528. return 0;
  529. if (!is_memory(dev) && a == &dev_attr_dax_seed.attr)
  530. return 0;
  531. if (!is_memory(dev) && a == &dev_attr_badblocks.attr)
  532. return 0;
  533. if (a == &dev_attr_resource.attr && !is_memory(dev))
  534. return 0;
  535. if (a == &dev_attr_deep_flush.attr) {
  536. int has_flush = nvdimm_has_flush(nd_region);
  537. if (has_flush == 1)
  538. return a->mode;
  539. else if (has_flush == 0)
  540. return 0444;
  541. else
  542. return 0;
  543. }
  544. if (a == &dev_attr_persistence_domain.attr) {
  545. if ((nd_region->flags & (BIT(ND_REGION_PERSIST_CACHE)
  546. | BIT(ND_REGION_PERSIST_MEMCTRL))) == 0)
  547. return 0;
  548. return a->mode;
  549. }
  550. if (a == &dev_attr_align.attr)
  551. return a->mode;
  552. if (a != &dev_attr_set_cookie.attr
  553. && a != &dev_attr_available_size.attr)
  554. return a->mode;
  555. if (type == ND_DEVICE_NAMESPACE_PMEM &&
  556. a == &dev_attr_available_size.attr)
  557. return a->mode;
  558. else if (is_memory(dev) && nd_set)
  559. return a->mode;
  560. return 0;
  561. }
  562. static ssize_t mappingN(struct device *dev, char *buf, int n)
  563. {
  564. struct nd_region *nd_region = to_nd_region(dev);
  565. struct nd_mapping *nd_mapping;
  566. struct nvdimm *nvdimm;
  567. if (n >= nd_region->ndr_mappings)
  568. return -ENXIO;
  569. nd_mapping = &nd_region->mapping[n];
  570. nvdimm = nd_mapping->nvdimm;
  571. return sprintf(buf, "%s,%llu,%llu,%d\n", dev_name(&nvdimm->dev),
  572. nd_mapping->start, nd_mapping->size,
  573. nd_mapping->position);
  574. }
  575. #define REGION_MAPPING(idx) \
  576. static ssize_t mapping##idx##_show(struct device *dev, \
  577. struct device_attribute *attr, char *buf) \
  578. { \
  579. return mappingN(dev, buf, idx); \
  580. } \
  581. static DEVICE_ATTR_RO(mapping##idx)
  582. /*
  583. * 32 should be enough for a while, even in the presence of socket
  584. * interleave a 32-way interleave set is a degenerate case.
  585. */
  586. REGION_MAPPING(0);
  587. REGION_MAPPING(1);
  588. REGION_MAPPING(2);
  589. REGION_MAPPING(3);
  590. REGION_MAPPING(4);
  591. REGION_MAPPING(5);
  592. REGION_MAPPING(6);
  593. REGION_MAPPING(7);
  594. REGION_MAPPING(8);
  595. REGION_MAPPING(9);
  596. REGION_MAPPING(10);
  597. REGION_MAPPING(11);
  598. REGION_MAPPING(12);
  599. REGION_MAPPING(13);
  600. REGION_MAPPING(14);
  601. REGION_MAPPING(15);
  602. REGION_MAPPING(16);
  603. REGION_MAPPING(17);
  604. REGION_MAPPING(18);
  605. REGION_MAPPING(19);
  606. REGION_MAPPING(20);
  607. REGION_MAPPING(21);
  608. REGION_MAPPING(22);
  609. REGION_MAPPING(23);
  610. REGION_MAPPING(24);
  611. REGION_MAPPING(25);
  612. REGION_MAPPING(26);
  613. REGION_MAPPING(27);
  614. REGION_MAPPING(28);
  615. REGION_MAPPING(29);
  616. REGION_MAPPING(30);
  617. REGION_MAPPING(31);
  618. static umode_t mapping_visible(struct kobject *kobj, struct attribute *a, int n)
  619. {
  620. struct device *dev = container_of(kobj, struct device, kobj);
  621. struct nd_region *nd_region = to_nd_region(dev);
  622. if (n < nd_region->ndr_mappings)
  623. return a->mode;
  624. return 0;
  625. }
  626. static struct attribute *mapping_attributes[] = {
  627. &dev_attr_mapping0.attr,
  628. &dev_attr_mapping1.attr,
  629. &dev_attr_mapping2.attr,
  630. &dev_attr_mapping3.attr,
  631. &dev_attr_mapping4.attr,
  632. &dev_attr_mapping5.attr,
  633. &dev_attr_mapping6.attr,
  634. &dev_attr_mapping7.attr,
  635. &dev_attr_mapping8.attr,
  636. &dev_attr_mapping9.attr,
  637. &dev_attr_mapping10.attr,
  638. &dev_attr_mapping11.attr,
  639. &dev_attr_mapping12.attr,
  640. &dev_attr_mapping13.attr,
  641. &dev_attr_mapping14.attr,
  642. &dev_attr_mapping15.attr,
  643. &dev_attr_mapping16.attr,
  644. &dev_attr_mapping17.attr,
  645. &dev_attr_mapping18.attr,
  646. &dev_attr_mapping19.attr,
  647. &dev_attr_mapping20.attr,
  648. &dev_attr_mapping21.attr,
  649. &dev_attr_mapping22.attr,
  650. &dev_attr_mapping23.attr,
  651. &dev_attr_mapping24.attr,
  652. &dev_attr_mapping25.attr,
  653. &dev_attr_mapping26.attr,
  654. &dev_attr_mapping27.attr,
  655. &dev_attr_mapping28.attr,
  656. &dev_attr_mapping29.attr,
  657. &dev_attr_mapping30.attr,
  658. &dev_attr_mapping31.attr,
  659. NULL,
  660. };
  661. static const struct attribute_group nd_mapping_attribute_group = {
  662. .is_visible = mapping_visible,
  663. .attrs = mapping_attributes,
  664. };
  665. static const struct attribute_group nd_region_attribute_group = {
  666. .attrs = nd_region_attributes,
  667. .is_visible = region_visible,
  668. };
  669. static const struct attribute_group *nd_region_attribute_groups[] = {
  670. &nd_device_attribute_group,
  671. &nd_region_attribute_group,
  672. &nd_numa_attribute_group,
  673. &nd_mapping_attribute_group,
  674. NULL,
  675. };
  676. static const struct device_type nd_pmem_device_type = {
  677. .name = "nd_pmem",
  678. .release = nd_region_release,
  679. .groups = nd_region_attribute_groups,
  680. };
  681. static const struct device_type nd_volatile_device_type = {
  682. .name = "nd_volatile",
  683. .release = nd_region_release,
  684. .groups = nd_region_attribute_groups,
  685. };
  686. bool is_nd_pmem(const struct device *dev)
  687. {
  688. return dev ? dev->type == &nd_pmem_device_type : false;
  689. }
  690. bool is_nd_volatile(const struct device *dev)
  691. {
  692. return dev ? dev->type == &nd_volatile_device_type : false;
  693. }
  694. u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,
  695. struct nd_namespace_index *nsindex)
  696. {
  697. struct nd_interleave_set *nd_set = nd_region->nd_set;
  698. if (!nd_set)
  699. return 0;
  700. if (nsindex && __le16_to_cpu(nsindex->major) == 1
  701. && __le16_to_cpu(nsindex->minor) == 1)
  702. return nd_set->cookie1;
  703. return nd_set->cookie2;
  704. }
  705. u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region)
  706. {
  707. struct nd_interleave_set *nd_set = nd_region->nd_set;
  708. if (nd_set)
  709. return nd_set->altcookie;
  710. return 0;
  711. }
  712. void nd_mapping_free_labels(struct nd_mapping *nd_mapping)
  713. {
  714. struct nd_label_ent *label_ent, *e;
  715. lockdep_assert_held(&nd_mapping->lock);
  716. list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
  717. list_del(&label_ent->list);
  718. kfree(label_ent);
  719. }
  720. }
  721. /*
  722. * When a namespace is activated create new seeds for the next
  723. * namespace, or namespace-personality to be configured.
  724. */
  725. void nd_region_advance_seeds(struct nd_region *nd_region, struct device *dev)
  726. {
  727. guard(nvdimm_bus)(dev);
  728. if (nd_region->ns_seed == dev) {
  729. nd_region_create_ns_seed(nd_region);
  730. } else if (is_nd_btt(dev)) {
  731. struct nd_btt *nd_btt = to_nd_btt(dev);
  732. if (nd_region->btt_seed == dev)
  733. nd_region_create_btt_seed(nd_region);
  734. if (nd_region->ns_seed == &nd_btt->ndns->dev)
  735. nd_region_create_ns_seed(nd_region);
  736. } else if (is_nd_pfn(dev)) {
  737. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  738. if (nd_region->pfn_seed == dev)
  739. nd_region_create_pfn_seed(nd_region);
  740. if (nd_region->ns_seed == &nd_pfn->ndns->dev)
  741. nd_region_create_ns_seed(nd_region);
  742. } else if (is_nd_dax(dev)) {
  743. struct nd_dax *nd_dax = to_nd_dax(dev);
  744. if (nd_region->dax_seed == dev)
  745. nd_region_create_dax_seed(nd_region);
  746. if (nd_region->ns_seed == &nd_dax->nd_pfn.ndns->dev)
  747. nd_region_create_ns_seed(nd_region);
  748. }
  749. }
  750. /**
  751. * nd_region_acquire_lane - allocate and lock a lane
  752. * @nd_region: region id and number of lanes possible
  753. *
  754. * A lane correlates to a BLK-data-window and/or a log slot in the BTT.
  755. * We optimize for the common case where there are 256 lanes, one
  756. * per-cpu. For larger systems we need to lock to share lanes. For now
  757. * this implementation assumes the cost of maintaining an allocator for
  758. * free lanes is on the order of the lock hold time, so it implements a
  759. * static lane = cpu % num_lanes mapping.
  760. *
  761. * In the case of a BTT instance on top of a BLK namespace a lane may be
  762. * acquired recursively. We lock on the first instance.
  763. *
  764. * In the case of a BTT instance on top of PMEM, we only acquire a lane
  765. * for the BTT metadata updates.
  766. */
  767. unsigned int nd_region_acquire_lane(struct nd_region *nd_region)
  768. {
  769. unsigned int cpu, lane;
  770. migrate_disable();
  771. cpu = smp_processor_id();
  772. if (nd_region->num_lanes < nr_cpu_ids) {
  773. struct nd_percpu_lane *ndl_lock, *ndl_count;
  774. lane = cpu % nd_region->num_lanes;
  775. ndl_count = per_cpu_ptr(nd_region->lane, cpu);
  776. ndl_lock = per_cpu_ptr(nd_region->lane, lane);
  777. if (ndl_count->count++ == 0)
  778. spin_lock(&ndl_lock->lock);
  779. } else
  780. lane = cpu;
  781. return lane;
  782. }
  783. EXPORT_SYMBOL(nd_region_acquire_lane);
  784. void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane)
  785. {
  786. if (nd_region->num_lanes < nr_cpu_ids) {
  787. unsigned int cpu = smp_processor_id();
  788. struct nd_percpu_lane *ndl_lock, *ndl_count;
  789. ndl_count = per_cpu_ptr(nd_region->lane, cpu);
  790. ndl_lock = per_cpu_ptr(nd_region->lane, lane);
  791. if (--ndl_count->count == 0)
  792. spin_unlock(&ndl_lock->lock);
  793. }
  794. migrate_enable();
  795. }
  796. EXPORT_SYMBOL(nd_region_release_lane);
  797. /*
  798. * PowerPC requires this alignment for memremap_pages(). All other archs
  799. * should be ok with SUBSECTION_SIZE (see memremap_compat_align()).
  800. */
  801. #define MEMREMAP_COMPAT_ALIGN_MAX SZ_16M
  802. static unsigned long default_align(struct nd_region *nd_region)
  803. {
  804. unsigned long align;
  805. u32 remainder;
  806. int mappings;
  807. align = MEMREMAP_COMPAT_ALIGN_MAX;
  808. if (nd_region->ndr_size < MEMREMAP_COMPAT_ALIGN_MAX)
  809. align = PAGE_SIZE;
  810. mappings = max_t(u16, 1, nd_region->ndr_mappings);
  811. div_u64_rem(align, mappings, &remainder);
  812. if (remainder)
  813. align *= mappings;
  814. return align;
  815. }
  816. static struct lock_class_key nvdimm_region_key;
  817. static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
  818. struct nd_region_desc *ndr_desc,
  819. const struct device_type *dev_type, const char *caller)
  820. {
  821. struct nd_region *nd_region;
  822. struct device *dev;
  823. unsigned int i;
  824. int ro = 0;
  825. for (i = 0; i < ndr_desc->num_mappings; i++) {
  826. struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
  827. struct nvdimm *nvdimm = mapping->nvdimm;
  828. if ((mapping->start | mapping->size) % PAGE_SIZE) {
  829. dev_err(&nvdimm_bus->dev,
  830. "%s: %s mapping%d is not %ld aligned\n",
  831. caller, dev_name(&nvdimm->dev), i, PAGE_SIZE);
  832. return NULL;
  833. }
  834. if (test_bit(NDD_UNARMED, &nvdimm->flags))
  835. ro = 1;
  836. }
  837. nd_region =
  838. kzalloc_flex(*nd_region, mapping, ndr_desc->num_mappings);
  839. if (!nd_region)
  840. return NULL;
  841. nd_region->ndr_mappings = ndr_desc->num_mappings;
  842. /* CXL pre-assigns memregion ids before creating nvdimm regions */
  843. if (test_bit(ND_REGION_CXL, &ndr_desc->flags)) {
  844. nd_region->id = ndr_desc->memregion;
  845. } else {
  846. nd_region->id = memregion_alloc(GFP_KERNEL);
  847. if (nd_region->id < 0)
  848. goto err_id;
  849. }
  850. nd_region->lane = alloc_percpu(struct nd_percpu_lane);
  851. if (!nd_region->lane)
  852. goto err_percpu;
  853. for (i = 0; i < nr_cpu_ids; i++) {
  854. struct nd_percpu_lane *ndl;
  855. ndl = per_cpu_ptr(nd_region->lane, i);
  856. spin_lock_init(&ndl->lock);
  857. ndl->count = 0;
  858. }
  859. for (i = 0; i < ndr_desc->num_mappings; i++) {
  860. struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
  861. struct nvdimm *nvdimm = mapping->nvdimm;
  862. nd_region->mapping[i].nvdimm = nvdimm;
  863. nd_region->mapping[i].start = mapping->start;
  864. nd_region->mapping[i].size = mapping->size;
  865. nd_region->mapping[i].position = mapping->position;
  866. INIT_LIST_HEAD(&nd_region->mapping[i].labels);
  867. mutex_init(&nd_region->mapping[i].lock);
  868. get_device(&nvdimm->dev);
  869. }
  870. nd_region->provider_data = ndr_desc->provider_data;
  871. nd_region->nd_set = ndr_desc->nd_set;
  872. nd_region->num_lanes = ndr_desc->num_lanes;
  873. nd_region->flags = ndr_desc->flags;
  874. nd_region->ro = ro;
  875. nd_region->numa_node = ndr_desc->numa_node;
  876. nd_region->target_node = ndr_desc->target_node;
  877. ida_init(&nd_region->ns_ida);
  878. ida_init(&nd_region->btt_ida);
  879. ida_init(&nd_region->pfn_ida);
  880. ida_init(&nd_region->dax_ida);
  881. dev = &nd_region->dev;
  882. dev_set_name(dev, "region%d", nd_region->id);
  883. dev->parent = &nvdimm_bus->dev;
  884. dev->type = dev_type;
  885. dev->groups = ndr_desc->attr_groups;
  886. dev->of_node = ndr_desc->of_node;
  887. nd_region->ndr_size = resource_size(ndr_desc->res);
  888. nd_region->ndr_start = ndr_desc->res->start;
  889. nd_region->align = default_align(nd_region);
  890. if (ndr_desc->flush)
  891. nd_region->flush = ndr_desc->flush;
  892. else
  893. nd_region->flush = NULL;
  894. device_initialize(dev);
  895. lockdep_set_class(&dev->mutex, &nvdimm_region_key);
  896. nd_device_register(dev);
  897. return nd_region;
  898. err_percpu:
  899. if (!test_bit(ND_REGION_CXL, &ndr_desc->flags))
  900. memregion_free(nd_region->id);
  901. err_id:
  902. kfree(nd_region);
  903. return NULL;
  904. }
  905. struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
  906. struct nd_region_desc *ndr_desc)
  907. {
  908. ndr_desc->num_lanes = ND_MAX_LANES;
  909. return nd_region_create(nvdimm_bus, ndr_desc, &nd_pmem_device_type,
  910. __func__);
  911. }
  912. EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create);
  913. struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
  914. struct nd_region_desc *ndr_desc)
  915. {
  916. ndr_desc->num_lanes = ND_MAX_LANES;
  917. return nd_region_create(nvdimm_bus, ndr_desc, &nd_volatile_device_type,
  918. __func__);
  919. }
  920. EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create);
  921. void nvdimm_region_delete(struct nd_region *nd_region)
  922. {
  923. if (nd_region)
  924. nd_device_unregister(&nd_region->dev, ND_SYNC);
  925. }
  926. EXPORT_SYMBOL_GPL(nvdimm_region_delete);
  927. int nvdimm_flush(struct nd_region *nd_region, struct bio *bio)
  928. {
  929. int rc = 0;
  930. if (!nd_region->flush)
  931. rc = generic_nvdimm_flush(nd_region);
  932. else {
  933. if (nd_region->flush(nd_region, bio))
  934. rc = -EIO;
  935. }
  936. return rc;
  937. }
  938. /**
  939. * generic_nvdimm_flush() - flush any posted write queues between the cpu and pmem media
  940. * @nd_region: interleaved pmem region
  941. */
  942. int generic_nvdimm_flush(struct nd_region *nd_region)
  943. {
  944. struct nd_region_data *ndrd = dev_get_drvdata(&nd_region->dev);
  945. int i, idx;
  946. /*
  947. * Try to encourage some diversity in flush hint addresses
  948. * across cpus assuming a limited number of flush hints.
  949. */
  950. idx = this_cpu_read(flush_idx);
  951. idx = this_cpu_add_return(flush_idx, hash_32(current->pid + idx, 8));
  952. /*
  953. * The pmem_wmb() is needed to 'sfence' all
  954. * previous writes such that they are architecturally visible for
  955. * the platform buffer flush. Note that we've already arranged for pmem
  956. * writes to avoid the cache via memcpy_flushcache(). The final
  957. * wmb() ensures ordering for the NVDIMM flush write.
  958. */
  959. pmem_wmb();
  960. for (i = 0; i < nd_region->ndr_mappings; i++)
  961. if (ndrd_get_flush_wpq(ndrd, i, 0))
  962. writeq(1, ndrd_get_flush_wpq(ndrd, i, idx));
  963. wmb();
  964. return 0;
  965. }
  966. EXPORT_SYMBOL_GPL(nvdimm_flush);
  967. /**
  968. * nvdimm_has_flush - determine write flushing requirements
  969. * @nd_region: interleaved pmem region
  970. *
  971. * Returns 1 if writes require flushing
  972. * Returns 0 if writes do not require flushing
  973. * Returns -ENXIO if flushing capability can not be determined
  974. */
  975. int nvdimm_has_flush(struct nd_region *nd_region)
  976. {
  977. int i;
  978. /* no nvdimm or pmem api == flushing capability unknown */
  979. if (nd_region->ndr_mappings == 0
  980. || !IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API))
  981. return -ENXIO;
  982. /* Test if an explicit flush function is defined */
  983. if (test_bit(ND_REGION_ASYNC, &nd_region->flags) && nd_region->flush)
  984. return 1;
  985. /* Test if any flush hints for the region are available */
  986. for (i = 0; i < nd_region->ndr_mappings; i++) {
  987. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  988. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  989. /* flush hints present / available */
  990. if (nvdimm->num_flush)
  991. return 1;
  992. }
  993. /*
  994. * The platform defines dimm devices without hints nor explicit flush,
  995. * assume platform persistence mechanism like ADR
  996. */
  997. return 0;
  998. }
  999. EXPORT_SYMBOL_GPL(nvdimm_has_flush);
  1000. int nvdimm_has_cache(struct nd_region *nd_region)
  1001. {
  1002. return is_nd_pmem(&nd_region->dev) &&
  1003. !test_bit(ND_REGION_PERSIST_CACHE, &nd_region->flags);
  1004. }
  1005. EXPORT_SYMBOL_GPL(nvdimm_has_cache);
  1006. bool is_nvdimm_sync(struct nd_region *nd_region)
  1007. {
  1008. if (is_nd_volatile(&nd_region->dev))
  1009. return true;
  1010. return is_nd_pmem(&nd_region->dev) &&
  1011. !test_bit(ND_REGION_ASYNC, &nd_region->flags);
  1012. }
  1013. EXPORT_SYMBOL_GPL(is_nvdimm_sync);
  1014. MODULE_IMPORT_NS("DEVMEM");