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