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