region_devs.c 31 KB

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