1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * CXL EDAC memory feature driver. 4 * 5 * Copyright (c) 2024-2025 HiSilicon Limited. 6 * 7 * - Supports functions to configure EDAC features of the 8 * CXL memory devices. 9 * - Registers with the EDAC device subsystem driver to expose 10 * the features sysfs attributes to the user for configuring 11 * CXL memory RAS feature. 12 */ 13 14 #include <linux/cleanup.h> 15 #include <linux/edac.h> 16 #include <linux/limits.h> 17 #include <linux/unaligned.h> 18 #include <linux/xarray.h> 19 #include <cxl/features.h> 20 #include <cxl.h> 21 #include <cxlmem.h> 22 #include "core.h" 23 #include "trace.h" 24 25 #define CXL_NR_EDAC_DEV_FEATURES 7 26 27 #define CXL_SCRUB_NO_REGION -1 28 29 struct cxl_patrol_scrub_context { 30 u8 instance; 31 u16 get_feat_size; 32 u16 set_feat_size; 33 u8 get_version; 34 u8 set_version; 35 u16 effects; 36 struct cxl_memdev *cxlmd; 37 struct cxl_region *cxlr; 38 }; 39 40 /* 41 * See CXL spec rev 3.2 @8.2.10.9.11.1 Table 8-222 Device Patrol Scrub Control 42 * Feature Readable Attributes. 43 */ 44 struct cxl_scrub_rd_attrbs { 45 u8 scrub_cycle_cap; 46 __le16 scrub_cycle_hours; 47 u8 scrub_flags; 48 } __packed; 49 50 /* 51 * See CXL spec rev 3.2 @8.2.10.9.11.1 Table 8-223 Device Patrol Scrub Control 52 * Feature Writable Attributes. 53 */ 54 struct cxl_scrub_wr_attrbs { 55 u8 scrub_cycle_hours; 56 u8 scrub_flags; 57 } __packed; 58 59 #define CXL_SCRUB_CONTROL_CHANGEABLE BIT(0) 60 #define CXL_SCRUB_CONTROL_REALTIME BIT(1) 61 #define CXL_SCRUB_CONTROL_CYCLE_MASK GENMASK(7, 0) 62 #define CXL_SCRUB_CONTROL_MIN_CYCLE_MASK GENMASK(15, 8) 63 #define CXL_SCRUB_CONTROL_ENABLE BIT(0) 64 65 #define CXL_GET_SCRUB_CYCLE_CHANGEABLE(cap) \ 66 FIELD_GET(CXL_SCRUB_CONTROL_CHANGEABLE, cap) 67 #define CXL_GET_SCRUB_CYCLE(cycle) \ 68 FIELD_GET(CXL_SCRUB_CONTROL_CYCLE_MASK, cycle) 69 #define CXL_GET_SCRUB_MIN_CYCLE(cycle) \ 70 FIELD_GET(CXL_SCRUB_CONTROL_MIN_CYCLE_MASK, cycle) 71 #define CXL_GET_SCRUB_EN_STS(flags) FIELD_GET(CXL_SCRUB_CONTROL_ENABLE, flags) 72 73 #define CXL_SET_SCRUB_CYCLE(cycle) \ 74 FIELD_PREP(CXL_SCRUB_CONTROL_CYCLE_MASK, cycle) 75 #define CXL_SET_SCRUB_EN(en) FIELD_PREP(CXL_SCRUB_CONTROL_ENABLE, en) 76 77 static int cxl_mem_scrub_get_attrbs(struct cxl_mailbox *cxl_mbox, u8 *cap, 78 u16 *cycle, u8 *flags, u8 *min_cycle) 79 { 80 size_t rd_data_size = sizeof(struct cxl_scrub_rd_attrbs); 81 ssize_t data_size; 82 struct cxl_scrub_rd_attrbs *rd_attrbs __free(kfree) = 83 kzalloc(rd_data_size, GFP_KERNEL); 84 if (!rd_attrbs) 85 return -ENOMEM; 86 87 data_size = cxl_get_feature(cxl_mbox, &CXL_FEAT_PATROL_SCRUB_UUID, 88 CXL_GET_FEAT_SEL_CURRENT_VALUE, rd_attrbs, 89 rd_data_size, 0, NULL); 90 if (data_size < 0) 91 return data_size; 92 if ((size_t)data_size != rd_data_size) 93 return -EIO; 94 95 *cap = rd_attrbs->scrub_cycle_cap; 96 *cycle = le16_to_cpu(rd_attrbs->scrub_cycle_hours); 97 *flags = rd_attrbs->scrub_flags; 98 if (min_cycle) 99 *min_cycle = CXL_GET_SCRUB_MIN_CYCLE(*cycle); 100 101 return 0; 102 } 103 104 static int cxl_scrub_get_attrbs(struct cxl_patrol_scrub_context *cxl_ps_ctx, 105 u8 *cap, u16 *cycle, u8 *flags, u8 *min_cycle) 106 { 107 struct cxl_mailbox *cxl_mbox; 108 struct cxl_region_params *p; 109 struct cxl_memdev *cxlmd; 110 struct cxl_region *cxlr; 111 u8 min_scrub_cycle = 0; 112 int i, ret; 113 114 if (!cxl_ps_ctx->cxlr) { 115 cxl_mbox = &cxl_ps_ctx->cxlmd->cxlds->cxl_mbox; 116 return cxl_mem_scrub_get_attrbs(cxl_mbox, cap, cycle, 117 flags, min_cycle); 118 } 119 120 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region); 121 if ((ret = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) 122 return ret; 123 124 cxlr = cxl_ps_ctx->cxlr; 125 p = &cxlr->params; 126 127 for (i = 0; i < p->nr_targets; i++) { 128 struct cxl_endpoint_decoder *cxled = p->targets[i]; 129 130 cxlmd = cxled_to_memdev(cxled); 131 cxl_mbox = &cxlmd->cxlds->cxl_mbox; 132 ret = cxl_mem_scrub_get_attrbs(cxl_mbox, cap, cycle, flags, 133 min_cycle); 134 if (ret) 135 return ret; 136 137 /* 138 * The min_scrub_cycle of a region is the max of minimum scrub 139 * cycles supported by memdevs that back the region. 140 */ 141 if (min_cycle) 142 min_scrub_cycle = max(*min_cycle, min_scrub_cycle); 143 } 144 145 if (min_cycle) 146 *min_cycle = min_scrub_cycle; 147 148 return 0; 149 } 150 151 static int cxl_scrub_set_attrbs_region(struct device *dev, 152 struct cxl_patrol_scrub_context *cxl_ps_ctx, 153 u8 cycle, u8 flags) 154 { 155 struct cxl_scrub_wr_attrbs wr_attrbs; 156 struct cxl_mailbox *cxl_mbox; 157 struct cxl_region_params *p; 158 struct cxl_memdev *cxlmd; 159 struct cxl_region *cxlr; 160 int ret, i; 161 162 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region); 163 if ((ret = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) 164 return ret; 165 166 cxlr = cxl_ps_ctx->cxlr; 167 p = &cxlr->params; 168 wr_attrbs.scrub_cycle_hours = cycle; 169 wr_attrbs.scrub_flags = flags; 170 171 for (i = 0; i < p->nr_targets; i++) { 172 struct cxl_endpoint_decoder *cxled = p->targets[i]; 173 174 cxlmd = cxled_to_memdev(cxled); 175 cxl_mbox = &cxlmd->cxlds->cxl_mbox; 176 ret = cxl_set_feature(cxl_mbox, &CXL_FEAT_PATROL_SCRUB_UUID, 177 cxl_ps_ctx->set_version, &wr_attrbs, 178 sizeof(wr_attrbs), 179 CXL_SET_FEAT_FLAG_DATA_SAVED_ACROSS_RESET, 180 0, NULL); 181 if (ret) 182 return ret; 183 184 if (cycle != cxlmd->scrub_cycle) { 185 if (cxlmd->scrub_region_id != CXL_SCRUB_NO_REGION) 186 dev_info(dev, 187 "Device scrub rate(%d hours) set by region%d rate overwritten by region%d scrub rate(%d hours)\n", 188 cxlmd->scrub_cycle, 189 cxlmd->scrub_region_id, cxlr->id, 190 cycle); 191 192 cxlmd->scrub_cycle = cycle; 193 cxlmd->scrub_region_id = cxlr->id; 194 } 195 } 196 197 return 0; 198 } 199 200 static int cxl_scrub_set_attrbs_device(struct device *dev, 201 struct cxl_patrol_scrub_context *cxl_ps_ctx, 202 u8 cycle, u8 flags) 203 { 204 struct cxl_scrub_wr_attrbs wr_attrbs; 205 struct cxl_mailbox *cxl_mbox; 206 struct cxl_memdev *cxlmd; 207 int ret; 208 209 wr_attrbs.scrub_cycle_hours = cycle; 210 wr_attrbs.scrub_flags = flags; 211 212 cxlmd = cxl_ps_ctx->cxlmd; 213 cxl_mbox = &cxlmd->cxlds->cxl_mbox; 214 ret = cxl_set_feature(cxl_mbox, &CXL_FEAT_PATROL_SCRUB_UUID, 215 cxl_ps_ctx->set_version, &wr_attrbs, 216 sizeof(wr_attrbs), 217 CXL_SET_FEAT_FLAG_DATA_SAVED_ACROSS_RESET, 0, 218 NULL); 219 if (ret) 220 return ret; 221 222 if (cycle != cxlmd->scrub_cycle) { 223 if (cxlmd->scrub_region_id != CXL_SCRUB_NO_REGION) 224 dev_info(dev, 225 "Device scrub rate(%d hours) set by region%d rate overwritten with device local scrub rate(%d hours)\n", 226 cxlmd->scrub_cycle, cxlmd->scrub_region_id, 227 cycle); 228 229 cxlmd->scrub_cycle = cycle; 230 cxlmd->scrub_region_id = CXL_SCRUB_NO_REGION; 231 } 232 233 return 0; 234 } 235 236 static int cxl_scrub_set_attrbs(struct device *dev, 237 struct cxl_patrol_scrub_context *cxl_ps_ctx, 238 u8 cycle, u8 flags) 239 { 240 if (cxl_ps_ctx->cxlr) 241 return cxl_scrub_set_attrbs_region(dev, cxl_ps_ctx, cycle, flags); 242 243 return cxl_scrub_set_attrbs_device(dev, cxl_ps_ctx, cycle, flags); 244 } 245 246 static int cxl_patrol_scrub_get_enabled_bg(struct device *dev, void *drv_data, 247 bool *enabled) 248 { 249 struct cxl_patrol_scrub_context *ctx = drv_data; 250 u8 cap, flags; 251 u16 cycle; 252 int ret; 253 254 ret = cxl_scrub_get_attrbs(ctx, &cap, &cycle, &flags, NULL); 255 if (ret) 256 return ret; 257 258 *enabled = CXL_GET_SCRUB_EN_STS(flags); 259 260 return 0; 261 } 262 263 static int cxl_patrol_scrub_set_enabled_bg(struct device *dev, void *drv_data, 264 bool enable) 265 { 266 struct cxl_patrol_scrub_context *ctx = drv_data; 267 u8 cap, flags, wr_cycle; 268 u16 rd_cycle; 269 int ret; 270 271 if (!capable(CAP_SYS_RAWIO)) 272 return -EPERM; 273 274 ret = cxl_scrub_get_attrbs(ctx, &cap, &rd_cycle, &flags, NULL); 275 if (ret) 276 return ret; 277 278 wr_cycle = CXL_GET_SCRUB_CYCLE(rd_cycle); 279 flags = CXL_SET_SCRUB_EN(enable); 280 281 return cxl_scrub_set_attrbs(dev, ctx, wr_cycle, flags); 282 } 283 284 static int cxl_patrol_scrub_get_min_scrub_cycle(struct device *dev, 285 void *drv_data, u32 *min) 286 { 287 struct cxl_patrol_scrub_context *ctx = drv_data; 288 u8 cap, flags, min_cycle; 289 u16 cycle; 290 int ret; 291 292 ret = cxl_scrub_get_attrbs(ctx, &cap, &cycle, &flags, &min_cycle); 293 if (ret) 294 return ret; 295 296 *min = min_cycle * 3600; 297 298 return 0; 299 } 300 301 static int cxl_patrol_scrub_get_max_scrub_cycle(struct device *dev, 302 void *drv_data, u32 *max) 303 { 304 *max = U8_MAX * 3600; /* Max set by register size */ 305 306 return 0; 307 } 308 309 static int cxl_patrol_scrub_get_scrub_cycle(struct device *dev, void *drv_data, 310 u32 *scrub_cycle_secs) 311 { 312 struct cxl_patrol_scrub_context *ctx = drv_data; 313 u8 cap, flags; 314 u16 cycle; 315 int ret; 316 317 ret = cxl_scrub_get_attrbs(ctx, &cap, &cycle, &flags, NULL); 318 if (ret) 319 return ret; 320 321 *scrub_cycle_secs = CXL_GET_SCRUB_CYCLE(cycle) * 3600; 322 323 return 0; 324 } 325 326 static int cxl_patrol_scrub_set_scrub_cycle(struct device *dev, void *drv_data, 327 u32 scrub_cycle_secs) 328 { 329 struct cxl_patrol_scrub_context *ctx = drv_data; 330 u8 scrub_cycle_hours = scrub_cycle_secs / 3600; 331 u8 cap, wr_cycle, flags, min_cycle; 332 u16 rd_cycle; 333 int ret; 334 335 if (!capable(CAP_SYS_RAWIO)) 336 return -EPERM; 337 338 ret = cxl_scrub_get_attrbs(ctx, &cap, &rd_cycle, &flags, &min_cycle); 339 if (ret) 340 return ret; 341 342 if (!CXL_GET_SCRUB_CYCLE_CHANGEABLE(cap)) 343 return -EOPNOTSUPP; 344 345 if (scrub_cycle_hours < min_cycle) { 346 dev_dbg(dev, "Invalid CXL patrol scrub cycle(%d) to set\n", 347 scrub_cycle_hours); 348 dev_dbg(dev, 349 "Minimum supported CXL patrol scrub cycle in hour %d\n", 350 min_cycle); 351 return -EINVAL; 352 } 353 wr_cycle = CXL_SET_SCRUB_CYCLE(scrub_cycle_hours); 354 355 return cxl_scrub_set_attrbs(dev, ctx, wr_cycle, flags); 356 } 357 358 static const struct edac_scrub_ops cxl_ps_scrub_ops = { 359 .get_enabled_bg = cxl_patrol_scrub_get_enabled_bg, 360 .set_enabled_bg = cxl_patrol_scrub_set_enabled_bg, 361 .get_min_cycle = cxl_patrol_scrub_get_min_scrub_cycle, 362 .get_max_cycle = cxl_patrol_scrub_get_max_scrub_cycle, 363 .get_cycle_duration = cxl_patrol_scrub_get_scrub_cycle, 364 .set_cycle_duration = cxl_patrol_scrub_set_scrub_cycle, 365 }; 366 367 static int cxl_memdev_scrub_init(struct cxl_memdev *cxlmd, 368 struct edac_dev_feature *ras_feature, 369 u8 scrub_inst) 370 { 371 struct cxl_patrol_scrub_context *cxl_ps_ctx; 372 struct cxl_feat_entry *feat_entry; 373 u8 cap, flags; 374 u16 cycle; 375 int rc; 376 377 feat_entry = cxl_feature_info(to_cxlfs(cxlmd->cxlds), 378 &CXL_FEAT_PATROL_SCRUB_UUID); 379 if (IS_ERR(feat_entry)) 380 return -EOPNOTSUPP; 381 382 if (!(le32_to_cpu(feat_entry->flags) & CXL_FEATURE_F_CHANGEABLE)) 383 return -EOPNOTSUPP; 384 385 cxl_ps_ctx = devm_kzalloc(&cxlmd->dev, sizeof(*cxl_ps_ctx), GFP_KERNEL); 386 if (!cxl_ps_ctx) 387 return -ENOMEM; 388 389 *cxl_ps_ctx = (struct cxl_patrol_scrub_context){ 390 .get_feat_size = le16_to_cpu(feat_entry->get_feat_size), 391 .set_feat_size = le16_to_cpu(feat_entry->set_feat_size), 392 .get_version = feat_entry->get_feat_ver, 393 .set_version = feat_entry->set_feat_ver, 394 .effects = le16_to_cpu(feat_entry->effects), 395 .instance = scrub_inst, 396 .cxlmd = cxlmd, 397 }; 398 399 rc = cxl_mem_scrub_get_attrbs(&cxlmd->cxlds->cxl_mbox, &cap, &cycle, 400 &flags, NULL); 401 if (rc) 402 return rc; 403 404 cxlmd->scrub_cycle = CXL_GET_SCRUB_CYCLE(cycle); 405 cxlmd->scrub_region_id = CXL_SCRUB_NO_REGION; 406 407 ras_feature->ft_type = RAS_FEAT_SCRUB; 408 ras_feature->instance = cxl_ps_ctx->instance; 409 ras_feature->scrub_ops = &cxl_ps_scrub_ops; 410 ras_feature->ctx = cxl_ps_ctx; 411 412 return 0; 413 } 414 415 static int cxl_region_scrub_init(struct cxl_region *cxlr, 416 struct edac_dev_feature *ras_feature, 417 u8 scrub_inst) 418 { 419 struct cxl_patrol_scrub_context *cxl_ps_ctx; 420 struct cxl_region_params *p = &cxlr->params; 421 struct cxl_feat_entry *feat_entry = NULL; 422 struct cxl_memdev *cxlmd; 423 u8 cap, flags; 424 u16 cycle; 425 int i, rc; 426 427 /* 428 * The cxl_region_rwsem must be held if the code below is used in a context 429 * other than when the region is in the probe state, as shown here. 430 */ 431 for (i = 0; i < p->nr_targets; i++) { 432 struct cxl_endpoint_decoder *cxled = p->targets[i]; 433 434 cxlmd = cxled_to_memdev(cxled); 435 feat_entry = cxl_feature_info(to_cxlfs(cxlmd->cxlds), 436 &CXL_FEAT_PATROL_SCRUB_UUID); 437 if (IS_ERR(feat_entry)) 438 return -EOPNOTSUPP; 439 440 if (!(le32_to_cpu(feat_entry->flags) & 441 CXL_FEATURE_F_CHANGEABLE)) 442 return -EOPNOTSUPP; 443 444 rc = cxl_mem_scrub_get_attrbs(&cxlmd->cxlds->cxl_mbox, &cap, 445 &cycle, &flags, NULL); 446 if (rc) 447 return rc; 448 449 cxlmd->scrub_cycle = CXL_GET_SCRUB_CYCLE(cycle); 450 cxlmd->scrub_region_id = CXL_SCRUB_NO_REGION; 451 } 452 453 cxl_ps_ctx = devm_kzalloc(&cxlr->dev, sizeof(*cxl_ps_ctx), GFP_KERNEL); 454 if (!cxl_ps_ctx) 455 return -ENOMEM; 456 457 *cxl_ps_ctx = (struct cxl_patrol_scrub_context){ 458 .get_feat_size = le16_to_cpu(feat_entry->get_feat_size), 459 .set_feat_size = le16_to_cpu(feat_entry->set_feat_size), 460 .get_version = feat_entry->get_feat_ver, 461 .set_version = feat_entry->set_feat_ver, 462 .effects = le16_to_cpu(feat_entry->effects), 463 .instance = scrub_inst, 464 .cxlr = cxlr, 465 }; 466 467 ras_feature->ft_type = RAS_FEAT_SCRUB; 468 ras_feature->instance = cxl_ps_ctx->instance; 469 ras_feature->scrub_ops = &cxl_ps_scrub_ops; 470 ras_feature->ctx = cxl_ps_ctx; 471 472 return 0; 473 } 474 475 struct cxl_ecs_context { 476 u16 num_media_frus; 477 u16 get_feat_size; 478 u16 set_feat_size; 479 u8 get_version; 480 u8 set_version; 481 u16 effects; 482 struct cxl_memdev *cxlmd; 483 }; 484 485 /* 486 * See CXL spec rev 3.2 @8.2.10.9.11.2 Table 8-225 DDR5 ECS Control Feature 487 * Readable Attributes. 488 */ 489 struct cxl_ecs_fru_rd_attrbs { 490 u8 ecs_cap; 491 __le16 ecs_config; 492 u8 ecs_flags; 493 } __packed; 494 495 struct cxl_ecs_rd_attrbs { 496 u8 ecs_log_cap; 497 struct cxl_ecs_fru_rd_attrbs fru_attrbs[]; 498 } __packed; 499 500 /* 501 * See CXL spec rev 3.2 @8.2.10.9.11.2 Table 8-226 DDR5 ECS Control Feature 502 * Writable Attributes. 503 */ 504 struct cxl_ecs_fru_wr_attrbs { 505 __le16 ecs_config; 506 } __packed; 507 508 struct cxl_ecs_wr_attrbs { 509 u8 ecs_log_cap; 510 struct cxl_ecs_fru_wr_attrbs fru_attrbs[]; 511 } __packed; 512 513 #define CXL_ECS_LOG_ENTRY_TYPE_MASK GENMASK(1, 0) 514 #define CXL_ECS_REALTIME_REPORT_CAP_MASK BIT(0) 515 #define CXL_ECS_THRESHOLD_COUNT_MASK GENMASK(2, 0) 516 #define CXL_ECS_COUNT_MODE_MASK BIT(3) 517 #define CXL_ECS_RESET_COUNTER_MASK BIT(4) 518 #define CXL_ECS_RESET_COUNTER 1 519 520 enum { 521 ECS_THRESHOLD_256 = 256, 522 ECS_THRESHOLD_1024 = 1024, 523 ECS_THRESHOLD_4096 = 4096, 524 }; 525 526 enum { 527 ECS_THRESHOLD_IDX_256 = 3, 528 ECS_THRESHOLD_IDX_1024 = 4, 529 ECS_THRESHOLD_IDX_4096 = 5, 530 }; 531 532 static const u16 ecs_supp_threshold[] = { 533 [ECS_THRESHOLD_IDX_256] = 256, 534 [ECS_THRESHOLD_IDX_1024] = 1024, 535 [ECS_THRESHOLD_IDX_4096] = 4096, 536 }; 537 538 enum { 539 ECS_LOG_ENTRY_TYPE_DRAM = 0x0, 540 ECS_LOG_ENTRY_TYPE_MEM_MEDIA_FRU = 0x1, 541 }; 542 543 enum cxl_ecs_count_mode { 544 ECS_MODE_COUNTS_ROWS = 0, 545 ECS_MODE_COUNTS_CODEWORDS = 1, 546 }; 547 548 static int cxl_mem_ecs_get_attrbs(struct device *dev, 549 struct cxl_ecs_context *cxl_ecs_ctx, 550 int fru_id, u8 *log_cap, u16 *config) 551 { 552 struct cxl_memdev *cxlmd = cxl_ecs_ctx->cxlmd; 553 struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox; 554 struct cxl_ecs_fru_rd_attrbs *fru_rd_attrbs; 555 size_t rd_data_size; 556 ssize_t data_size; 557 558 rd_data_size = cxl_ecs_ctx->get_feat_size; 559 560 struct cxl_ecs_rd_attrbs *rd_attrbs __free(kvfree) = 561 kvzalloc(rd_data_size, GFP_KERNEL); 562 if (!rd_attrbs) 563 return -ENOMEM; 564 565 data_size = cxl_get_feature(cxl_mbox, &CXL_FEAT_ECS_UUID, 566 CXL_GET_FEAT_SEL_CURRENT_VALUE, rd_attrbs, 567 rd_data_size, 0, NULL); 568 if (data_size < 0) 569 return data_size; 570 if ((size_t)data_size != rd_data_size) 571 return -EIO; 572 573 fru_rd_attrbs = rd_attrbs->fru_attrbs; 574 *log_cap = rd_attrbs->ecs_log_cap; 575 *config = le16_to_cpu(fru_rd_attrbs[fru_id].ecs_config); 576 577 return 0; 578 } 579 580 static int cxl_mem_ecs_set_attrbs(struct device *dev, 581 struct cxl_ecs_context *cxl_ecs_ctx, 582 int fru_id, u8 log_cap, u16 config) 583 { 584 struct cxl_memdev *cxlmd = cxl_ecs_ctx->cxlmd; 585 struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox; 586 struct cxl_ecs_fru_rd_attrbs *fru_rd_attrbs; 587 struct cxl_ecs_fru_wr_attrbs *fru_wr_attrbs; 588 size_t rd_data_size, wr_data_size; 589 u16 num_media_frus, count; 590 ssize_t data_size; 591 592 num_media_frus = cxl_ecs_ctx->num_media_frus; 593 rd_data_size = cxl_ecs_ctx->get_feat_size; 594 wr_data_size = cxl_ecs_ctx->set_feat_size; 595 struct cxl_ecs_rd_attrbs *rd_attrbs __free(kvfree) = 596 kvzalloc(rd_data_size, GFP_KERNEL); 597 if (!rd_attrbs) 598 return -ENOMEM; 599 600 data_size = cxl_get_feature(cxl_mbox, &CXL_FEAT_ECS_UUID, 601 CXL_GET_FEAT_SEL_CURRENT_VALUE, rd_attrbs, 602 rd_data_size, 0, NULL); 603 if (data_size < 0) 604 return data_size; 605 if ((size_t)data_size != rd_data_size) 606 return -EIO; 607 608 struct cxl_ecs_wr_attrbs *wr_attrbs __free(kvfree) = 609 kvzalloc(wr_data_size, GFP_KERNEL); 610 if (!wr_attrbs) 611 return -ENOMEM; 612 613 /* 614 * Fill writable attributes from the current attributes read 615 * for all the media FRUs. 616 */ 617 fru_rd_attrbs = rd_attrbs->fru_attrbs; 618 fru_wr_attrbs = wr_attrbs->fru_attrbs; 619 wr_attrbs->ecs_log_cap = log_cap; 620 for (count = 0; count < num_media_frus; count++) 621 fru_wr_attrbs[count].ecs_config = 622 fru_rd_attrbs[count].ecs_config; 623 624 fru_wr_attrbs[fru_id].ecs_config = cpu_to_le16(config); 625 626 return cxl_set_feature(cxl_mbox, &CXL_FEAT_ECS_UUID, 627 cxl_ecs_ctx->set_version, wr_attrbs, 628 wr_data_size, 629 CXL_SET_FEAT_FLAG_DATA_SAVED_ACROSS_RESET, 630 0, NULL); 631 } 632 633 static u8 cxl_get_ecs_log_entry_type(u8 log_cap, u16 config) 634 { 635 return FIELD_GET(CXL_ECS_LOG_ENTRY_TYPE_MASK, log_cap); 636 } 637 638 static u16 cxl_get_ecs_threshold(u8 log_cap, u16 config) 639 { 640 u8 index = FIELD_GET(CXL_ECS_THRESHOLD_COUNT_MASK, config); 641 642 return ecs_supp_threshold[index]; 643 } 644 645 static u8 cxl_get_ecs_count_mode(u8 log_cap, u16 config) 646 { 647 return FIELD_GET(CXL_ECS_COUNT_MODE_MASK, config); 648 } 649 650 #define CXL_ECS_GET_ATTR(attrb) \ 651 static int cxl_ecs_get_##attrb(struct device *dev, void *drv_data, \ 652 int fru_id, u32 *val) \ 653 { \ 654 struct cxl_ecs_context *ctx = drv_data; \ 655 u8 log_cap; \ 656 u16 config; \ 657 int ret; \ 658 \ 659 ret = cxl_mem_ecs_get_attrbs(dev, ctx, fru_id, &log_cap, \ 660 &config); \ 661 if (ret) \ 662 return ret; \ 663 \ 664 *val = cxl_get_ecs_##attrb(log_cap, config); \ 665 \ 666 return 0; \ 667 } 668 669 CXL_ECS_GET_ATTR(log_entry_type) 670 CXL_ECS_GET_ATTR(count_mode) 671 CXL_ECS_GET_ATTR(threshold) 672 673 static int cxl_set_ecs_log_entry_type(struct device *dev, u8 *log_cap, 674 u16 *config, u32 val) 675 { 676 if (val != ECS_LOG_ENTRY_TYPE_DRAM && 677 val != ECS_LOG_ENTRY_TYPE_MEM_MEDIA_FRU) 678 return -EINVAL; 679 680 *log_cap = FIELD_PREP(CXL_ECS_LOG_ENTRY_TYPE_MASK, val); 681 682 return 0; 683 } 684 685 static int cxl_set_ecs_threshold(struct device *dev, u8 *log_cap, u16 *config, 686 u32 val) 687 { 688 *config &= ~CXL_ECS_THRESHOLD_COUNT_MASK; 689 690 switch (val) { 691 case ECS_THRESHOLD_256: 692 *config |= FIELD_PREP(CXL_ECS_THRESHOLD_COUNT_MASK, 693 ECS_THRESHOLD_IDX_256); 694 break; 695 case ECS_THRESHOLD_1024: 696 *config |= FIELD_PREP(CXL_ECS_THRESHOLD_COUNT_MASK, 697 ECS_THRESHOLD_IDX_1024); 698 break; 699 case ECS_THRESHOLD_4096: 700 *config |= FIELD_PREP(CXL_ECS_THRESHOLD_COUNT_MASK, 701 ECS_THRESHOLD_IDX_4096); 702 break; 703 default: 704 dev_dbg(dev, "Invalid CXL ECS threshold count(%u) to set\n", 705 val); 706 dev_dbg(dev, "Supported ECS threshold counts: %u, %u, %u\n", 707 ECS_THRESHOLD_256, ECS_THRESHOLD_1024, 708 ECS_THRESHOLD_4096); 709 return -EINVAL; 710 } 711 712 return 0; 713 } 714 715 static int cxl_set_ecs_count_mode(struct device *dev, u8 *log_cap, u16 *config, 716 u32 val) 717 { 718 if (val != ECS_MODE_COUNTS_ROWS && val != ECS_MODE_COUNTS_CODEWORDS) { 719 dev_dbg(dev, "Invalid CXL ECS scrub mode(%d) to set\n", val); 720 dev_dbg(dev, 721 "Supported ECS Modes: 0: ECS counts rows with errors," 722 " 1: ECS counts codewords with errors\n"); 723 return -EINVAL; 724 } 725 726 *config &= ~CXL_ECS_COUNT_MODE_MASK; 727 *config |= FIELD_PREP(CXL_ECS_COUNT_MODE_MASK, val); 728 729 return 0; 730 } 731 732 static int cxl_set_ecs_reset_counter(struct device *dev, u8 *log_cap, 733 u16 *config, u32 val) 734 { 735 if (val != CXL_ECS_RESET_COUNTER) 736 return -EINVAL; 737 738 *config &= ~CXL_ECS_RESET_COUNTER_MASK; 739 *config |= FIELD_PREP(CXL_ECS_RESET_COUNTER_MASK, val); 740 741 return 0; 742 } 743 744 #define CXL_ECS_SET_ATTR(attrb) \ 745 static int cxl_ecs_set_##attrb(struct device *dev, void *drv_data, \ 746 int fru_id, u32 val) \ 747 { \ 748 struct cxl_ecs_context *ctx = drv_data; \ 749 u8 log_cap; \ 750 u16 config; \ 751 int ret; \ 752 \ 753 if (!capable(CAP_SYS_RAWIO)) \ 754 return -EPERM; \ 755 \ 756 ret = cxl_mem_ecs_get_attrbs(dev, ctx, fru_id, &log_cap, \ 757 &config); \ 758 if (ret) \ 759 return ret; \ 760 \ 761 ret = cxl_set_ecs_##attrb(dev, &log_cap, &config, val); \ 762 if (ret) \ 763 return ret; \ 764 \ 765 return cxl_mem_ecs_set_attrbs(dev, ctx, fru_id, log_cap, \ 766 config); \ 767 } 768 CXL_ECS_SET_ATTR(log_entry_type) 769 CXL_ECS_SET_ATTR(count_mode) 770 CXL_ECS_SET_ATTR(reset_counter) 771 CXL_ECS_SET_ATTR(threshold) 772 773 static const struct edac_ecs_ops cxl_ecs_ops = { 774 .get_log_entry_type = cxl_ecs_get_log_entry_type, 775 .set_log_entry_type = cxl_ecs_set_log_entry_type, 776 .get_mode = cxl_ecs_get_count_mode, 777 .set_mode = cxl_ecs_set_count_mode, 778 .reset = cxl_ecs_set_reset_counter, 779 .get_threshold = cxl_ecs_get_threshold, 780 .set_threshold = cxl_ecs_set_threshold, 781 }; 782 783 static int cxl_memdev_ecs_init(struct cxl_memdev *cxlmd, 784 struct edac_dev_feature *ras_feature) 785 { 786 struct cxl_ecs_context *cxl_ecs_ctx; 787 struct cxl_feat_entry *feat_entry; 788 int num_media_frus; 789 790 feat_entry = 791 cxl_feature_info(to_cxlfs(cxlmd->cxlds), &CXL_FEAT_ECS_UUID); 792 if (IS_ERR(feat_entry)) 793 return -EOPNOTSUPP; 794 795 if (!(le32_to_cpu(feat_entry->flags) & CXL_FEATURE_F_CHANGEABLE)) 796 return -EOPNOTSUPP; 797 798 num_media_frus = (le16_to_cpu(feat_entry->get_feat_size) - 799 sizeof(struct cxl_ecs_rd_attrbs)) / 800 sizeof(struct cxl_ecs_fru_rd_attrbs); 801 if (!num_media_frus) 802 return -EOPNOTSUPP; 803 804 cxl_ecs_ctx = 805 devm_kzalloc(&cxlmd->dev, sizeof(*cxl_ecs_ctx), GFP_KERNEL); 806 if (!cxl_ecs_ctx) 807 return -ENOMEM; 808 809 *cxl_ecs_ctx = (struct cxl_ecs_context){ 810 .get_feat_size = le16_to_cpu(feat_entry->get_feat_size), 811 .set_feat_size = le16_to_cpu(feat_entry->set_feat_size), 812 .get_version = feat_entry->get_feat_ver, 813 .set_version = feat_entry->set_feat_ver, 814 .effects = le16_to_cpu(feat_entry->effects), 815 .num_media_frus = num_media_frus, 816 .cxlmd = cxlmd, 817 }; 818 819 ras_feature->ft_type = RAS_FEAT_ECS; 820 ras_feature->ecs_ops = &cxl_ecs_ops; 821 ras_feature->ctx = cxl_ecs_ctx; 822 ras_feature->ecs_info.num_media_frus = num_media_frus; 823 824 return 0; 825 } 826 827 /* 828 * Perform Maintenance CXL 3.2 Spec 8.2.10.7.1 829 */ 830 831 /* 832 * Perform Maintenance input payload 833 * CXL rev 3.2 section 8.2.10.7.1 Table 8-117 834 */ 835 struct cxl_mbox_maintenance_hdr { 836 u8 op_class; 837 u8 op_subclass; 838 } __packed; 839 840 static int cxl_perform_maintenance(struct cxl_mailbox *cxl_mbox, u8 class, 841 u8 subclass, void *data_in, 842 size_t data_in_size) 843 { 844 struct cxl_memdev_maintenance_pi { 845 struct cxl_mbox_maintenance_hdr hdr; 846 u8 data[]; 847 } __packed; 848 struct cxl_mbox_cmd mbox_cmd; 849 size_t hdr_size; 850 851 struct cxl_memdev_maintenance_pi *pi __free(kvfree) = 852 kvzalloc(cxl_mbox->payload_size, GFP_KERNEL); 853 if (!pi) 854 return -ENOMEM; 855 856 pi->hdr.op_class = class; 857 pi->hdr.op_subclass = subclass; 858 hdr_size = sizeof(pi->hdr); 859 /* 860 * Check minimum mbox payload size is available for 861 * the maintenance data transfer. 862 */ 863 if (hdr_size + data_in_size > cxl_mbox->payload_size) 864 return -ENOMEM; 865 866 memcpy(pi->data, data_in, data_in_size); 867 mbox_cmd = (struct cxl_mbox_cmd){ 868 .opcode = CXL_MBOX_OP_DO_MAINTENANCE, 869 .size_in = hdr_size + data_in_size, 870 .payload_in = pi, 871 }; 872 873 return cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 874 } 875 876 /* 877 * Support for finding a memory operation attributes 878 * are from the current boot or not. 879 */ 880 881 struct cxl_mem_err_rec { 882 struct xarray rec_gen_media; 883 struct xarray rec_dram; 884 }; 885 886 enum cxl_mem_repair_type { 887 CXL_PPR, 888 CXL_CACHELINE_SPARING, 889 CXL_ROW_SPARING, 890 CXL_BANK_SPARING, 891 CXL_RANK_SPARING, 892 CXL_REPAIR_MAX, 893 }; 894 895 /** 896 * struct cxl_mem_repair_attrbs - CXL memory repair attributes 897 * @dpa: DPA of memory to repair 898 * @nibble_mask: nibble mask, identifies one or more nibbles on the memory bus 899 * @row: row of memory to repair 900 * @column: column of memory to repair 901 * @channel: channel of memory to repair 902 * @sub_channel: sub channel of memory to repair 903 * @rank: rank of memory to repair 904 * @bank_group: bank group of memory to repair 905 * @bank: bank of memory to repair 906 * @repair_type: repair type. For eg. PPR, memory sparing etc. 907 */ 908 struct cxl_mem_repair_attrbs { 909 u64 dpa; 910 u32 nibble_mask; 911 u32 row; 912 u16 column; 913 u8 channel; 914 u8 sub_channel; 915 u8 rank; 916 u8 bank_group; 917 u8 bank; 918 enum cxl_mem_repair_type repair_type; 919 }; 920 921 static struct cxl_event_gen_media * 922 cxl_find_rec_gen_media(struct cxl_memdev *cxlmd, 923 struct cxl_mem_repair_attrbs *attrbs) 924 { 925 struct cxl_mem_err_rec *array_rec = cxlmd->err_rec_array; 926 struct cxl_event_gen_media *rec; 927 928 if (!array_rec) 929 return NULL; 930 931 rec = xa_load(&array_rec->rec_gen_media, attrbs->dpa); 932 if (!rec) 933 return NULL; 934 935 if (attrbs->repair_type == CXL_PPR) 936 return rec; 937 938 return NULL; 939 } 940 941 static struct cxl_event_dram * 942 cxl_find_rec_dram(struct cxl_memdev *cxlmd, 943 struct cxl_mem_repair_attrbs *attrbs) 944 { 945 struct cxl_mem_err_rec *array_rec = cxlmd->err_rec_array; 946 struct cxl_event_dram *rec; 947 u16 validity_flags; 948 949 if (!array_rec) 950 return NULL; 951 952 rec = xa_load(&array_rec->rec_dram, attrbs->dpa); 953 if (!rec) 954 return NULL; 955 956 validity_flags = get_unaligned_le16(rec->media_hdr.validity_flags); 957 if (!(validity_flags & CXL_DER_VALID_CHANNEL) || 958 !(validity_flags & CXL_DER_VALID_RANK)) 959 return NULL; 960 961 switch (attrbs->repair_type) { 962 case CXL_PPR: 963 if (!(validity_flags & CXL_DER_VALID_NIBBLE) || 964 get_unaligned_le24(rec->nibble_mask) == attrbs->nibble_mask) 965 return rec; 966 break; 967 case CXL_CACHELINE_SPARING: 968 if (!(validity_flags & CXL_DER_VALID_BANK_GROUP) || 969 !(validity_flags & CXL_DER_VALID_BANK) || 970 !(validity_flags & CXL_DER_VALID_ROW) || 971 !(validity_flags & CXL_DER_VALID_COLUMN)) 972 return NULL; 973 974 if (rec->media_hdr.channel == attrbs->channel && 975 rec->media_hdr.rank == attrbs->rank && 976 rec->bank_group == attrbs->bank_group && 977 rec->bank == attrbs->bank && 978 get_unaligned_le24(rec->row) == attrbs->row && 979 get_unaligned_le16(rec->column) == attrbs->column && 980 (!(validity_flags & CXL_DER_VALID_NIBBLE) || 981 get_unaligned_le24(rec->nibble_mask) == 982 attrbs->nibble_mask) && 983 (!(validity_flags & CXL_DER_VALID_SUB_CHANNEL) || 984 rec->sub_channel == attrbs->sub_channel)) 985 return rec; 986 break; 987 case CXL_ROW_SPARING: 988 if (!(validity_flags & CXL_DER_VALID_BANK_GROUP) || 989 !(validity_flags & CXL_DER_VALID_BANK) || 990 !(validity_flags & CXL_DER_VALID_ROW)) 991 return NULL; 992 993 if (rec->media_hdr.channel == attrbs->channel && 994 rec->media_hdr.rank == attrbs->rank && 995 rec->bank_group == attrbs->bank_group && 996 rec->bank == attrbs->bank && 997 get_unaligned_le24(rec->row) == attrbs->row && 998 (!(validity_flags & CXL_DER_VALID_NIBBLE) || 999 get_unaligned_le24(rec->nibble_mask) == 1000 attrbs->nibble_mask)) 1001 return rec; 1002 break; 1003 case CXL_BANK_SPARING: 1004 if (!(validity_flags & CXL_DER_VALID_BANK_GROUP) || 1005 !(validity_flags & CXL_DER_VALID_BANK)) 1006 return NULL; 1007 1008 if (rec->media_hdr.channel == attrbs->channel && 1009 rec->media_hdr.rank == attrbs->rank && 1010 rec->bank_group == attrbs->bank_group && 1011 rec->bank == attrbs->bank && 1012 (!(validity_flags & CXL_DER_VALID_NIBBLE) || 1013 get_unaligned_le24(rec->nibble_mask) == 1014 attrbs->nibble_mask)) 1015 return rec; 1016 break; 1017 case CXL_RANK_SPARING: 1018 if (rec->media_hdr.channel == attrbs->channel && 1019 rec->media_hdr.rank == attrbs->rank && 1020 (!(validity_flags & CXL_DER_VALID_NIBBLE) || 1021 get_unaligned_le24(rec->nibble_mask) == 1022 attrbs->nibble_mask)) 1023 return rec; 1024 break; 1025 default: 1026 return NULL; 1027 } 1028 1029 return NULL; 1030 } 1031 1032 #define CXL_MAX_STORAGE_DAYS 10 1033 #define CXL_MAX_STORAGE_TIME_SECS (CXL_MAX_STORAGE_DAYS * 24 * 60 * 60) 1034 1035 static void cxl_del_expired_gmedia_recs(struct xarray *rec_xarray, 1036 struct cxl_event_gen_media *cur_rec) 1037 { 1038 u64 cur_ts = le64_to_cpu(cur_rec->media_hdr.hdr.timestamp); 1039 struct cxl_event_gen_media *rec; 1040 unsigned long index; 1041 u64 delta_ts_secs; 1042 1043 xa_for_each(rec_xarray, index, rec) { 1044 delta_ts_secs = (cur_ts - 1045 le64_to_cpu(rec->media_hdr.hdr.timestamp)) / 1000000000ULL; 1046 if (delta_ts_secs >= CXL_MAX_STORAGE_TIME_SECS) { 1047 xa_erase(rec_xarray, index); 1048 kfree(rec); 1049 } 1050 } 1051 } 1052 1053 static void cxl_del_expired_dram_recs(struct xarray *rec_xarray, 1054 struct cxl_event_dram *cur_rec) 1055 { 1056 u64 cur_ts = le64_to_cpu(cur_rec->media_hdr.hdr.timestamp); 1057 struct cxl_event_dram *rec; 1058 unsigned long index; 1059 u64 delta_secs; 1060 1061 xa_for_each(rec_xarray, index, rec) { 1062 delta_secs = (cur_ts - 1063 le64_to_cpu(rec->media_hdr.hdr.timestamp)) / 1000000000ULL; 1064 if (delta_secs >= CXL_MAX_STORAGE_TIME_SECS) { 1065 xa_erase(rec_xarray, index); 1066 kfree(rec); 1067 } 1068 } 1069 } 1070 1071 #define CXL_MAX_REC_STORAGE_COUNT 200 1072 1073 static void cxl_del_overflow_old_recs(struct xarray *rec_xarray) 1074 { 1075 void *err_rec; 1076 unsigned long index, count = 0; 1077 1078 xa_for_each(rec_xarray, index, err_rec) 1079 count++; 1080 1081 if (count <= CXL_MAX_REC_STORAGE_COUNT) 1082 return; 1083 1084 count -= CXL_MAX_REC_STORAGE_COUNT; 1085 xa_for_each(rec_xarray, index, err_rec) { 1086 xa_erase(rec_xarray, index); 1087 kfree(err_rec); 1088 count--; 1089 if (!count) 1090 break; 1091 } 1092 } 1093 1094 int cxl_store_rec_gen_media(struct cxl_memdev *cxlmd, union cxl_event *evt) 1095 { 1096 struct cxl_mem_err_rec *array_rec = cxlmd->err_rec_array; 1097 struct cxl_event_gen_media *rec; 1098 void *old_rec; 1099 1100 if (!IS_ENABLED(CONFIG_CXL_EDAC_MEM_REPAIR) || !array_rec) 1101 return 0; 1102 1103 rec = kmemdup(&evt->gen_media, sizeof(*rec), GFP_KERNEL); 1104 if (!rec) 1105 return -ENOMEM; 1106 1107 old_rec = xa_store(&array_rec->rec_gen_media, 1108 le64_to_cpu(rec->media_hdr.phys_addr), rec, 1109 GFP_KERNEL); 1110 if (xa_is_err(old_rec)) { 1111 kfree(rec); 1112 return xa_err(old_rec); 1113 } 1114 1115 kfree(old_rec); 1116 1117 cxl_del_expired_gmedia_recs(&array_rec->rec_gen_media, rec); 1118 cxl_del_overflow_old_recs(&array_rec->rec_gen_media); 1119 1120 return 0; 1121 } 1122 EXPORT_SYMBOL_NS_GPL(cxl_store_rec_gen_media, "CXL"); 1123 1124 int cxl_store_rec_dram(struct cxl_memdev *cxlmd, union cxl_event *evt) 1125 { 1126 struct cxl_mem_err_rec *array_rec = cxlmd->err_rec_array; 1127 struct cxl_event_dram *rec; 1128 void *old_rec; 1129 1130 if (!IS_ENABLED(CONFIG_CXL_EDAC_MEM_REPAIR) || !array_rec) 1131 return 0; 1132 1133 rec = kmemdup(&evt->dram, sizeof(*rec), GFP_KERNEL); 1134 if (!rec) 1135 return -ENOMEM; 1136 1137 old_rec = xa_store(&array_rec->rec_dram, 1138 le64_to_cpu(rec->media_hdr.phys_addr), rec, 1139 GFP_KERNEL); 1140 if (xa_is_err(old_rec)) { 1141 kfree(rec); 1142 return xa_err(old_rec); 1143 } 1144 1145 kfree(old_rec); 1146 1147 cxl_del_expired_dram_recs(&array_rec->rec_dram, rec); 1148 cxl_del_overflow_old_recs(&array_rec->rec_dram); 1149 1150 return 0; 1151 } 1152 EXPORT_SYMBOL_NS_GPL(cxl_store_rec_dram, "CXL"); 1153 1154 static bool cxl_is_memdev_memory_online(const struct cxl_memdev *cxlmd) 1155 { 1156 struct cxl_port *port = cxlmd->endpoint; 1157 1158 if (port && cxl_num_decoders_committed(port)) 1159 return true; 1160 1161 return false; 1162 } 1163 1164 /* 1165 * CXL memory sparing control 1166 */ 1167 enum cxl_mem_sparing_granularity { 1168 CXL_MEM_SPARING_CACHELINE, 1169 CXL_MEM_SPARING_ROW, 1170 CXL_MEM_SPARING_BANK, 1171 CXL_MEM_SPARING_RANK, 1172 CXL_MEM_SPARING_MAX 1173 }; 1174 1175 struct cxl_mem_sparing_context { 1176 struct cxl_memdev *cxlmd; 1177 uuid_t repair_uuid; 1178 u16 get_feat_size; 1179 u16 set_feat_size; 1180 u16 effects; 1181 u8 instance; 1182 u8 get_version; 1183 u8 set_version; 1184 u8 op_class; 1185 u8 op_subclass; 1186 bool cap_safe_when_in_use; 1187 bool cap_hard_sparing; 1188 bool cap_soft_sparing; 1189 u8 channel; 1190 u8 rank; 1191 u8 bank_group; 1192 u32 nibble_mask; 1193 u64 dpa; 1194 u32 row; 1195 u16 column; 1196 u8 bank; 1197 u8 sub_channel; 1198 enum edac_mem_repair_type repair_type; 1199 bool persist_mode; 1200 }; 1201 1202 #define CXL_SPARING_RD_CAP_SAFE_IN_USE_MASK BIT(0) 1203 #define CXL_SPARING_RD_CAP_HARD_SPARING_MASK BIT(1) 1204 #define CXL_SPARING_RD_CAP_SOFT_SPARING_MASK BIT(2) 1205 1206 #define CXL_SPARING_WR_DEVICE_INITIATED_MASK BIT(0) 1207 1208 #define CXL_SPARING_QUERY_RESOURCE_FLAG BIT(0) 1209 #define CXL_SET_HARD_SPARING_FLAG BIT(1) 1210 #define CXL_SPARING_SUB_CHNL_VALID_FLAG BIT(2) 1211 #define CXL_SPARING_NIB_MASK_VALID_FLAG BIT(3) 1212 1213 #define CXL_GET_SPARING_SAFE_IN_USE(flags) \ 1214 (FIELD_GET(CXL_SPARING_RD_CAP_SAFE_IN_USE_MASK, \ 1215 flags) ^ 1) 1216 #define CXL_GET_CAP_HARD_SPARING(flags) \ 1217 FIELD_GET(CXL_SPARING_RD_CAP_HARD_SPARING_MASK, \ 1218 flags) 1219 #define CXL_GET_CAP_SOFT_SPARING(flags) \ 1220 FIELD_GET(CXL_SPARING_RD_CAP_SOFT_SPARING_MASK, \ 1221 flags) 1222 1223 #define CXL_SET_SPARING_QUERY_RESOURCE(val) \ 1224 FIELD_PREP(CXL_SPARING_QUERY_RESOURCE_FLAG, val) 1225 #define CXL_SET_HARD_SPARING(val) \ 1226 FIELD_PREP(CXL_SET_HARD_SPARING_FLAG, val) 1227 #define CXL_SET_SPARING_SUB_CHNL_VALID(val) \ 1228 FIELD_PREP(CXL_SPARING_SUB_CHNL_VALID_FLAG, val) 1229 #define CXL_SET_SPARING_NIB_MASK_VALID(val) \ 1230 FIELD_PREP(CXL_SPARING_NIB_MASK_VALID_FLAG, val) 1231 1232 /* 1233 * See CXL spec rev 3.2 @8.2.10.7.2.3 Table 8-134 Memory Sparing Feature 1234 * Readable Attributes. 1235 */ 1236 struct cxl_memdev_repair_rd_attrbs_hdr { 1237 u8 max_op_latency; 1238 __le16 op_cap; 1239 __le16 op_mode; 1240 u8 op_class; 1241 u8 op_subclass; 1242 u8 rsvd[9]; 1243 } __packed; 1244 1245 struct cxl_memdev_sparing_rd_attrbs { 1246 struct cxl_memdev_repair_rd_attrbs_hdr hdr; 1247 u8 rsvd; 1248 __le16 restriction_flags; 1249 } __packed; 1250 1251 /* 1252 * See CXL spec rev 3.2 @8.2.10.7.1.4 Table 8-120 Memory Sparing Input Payload. 1253 */ 1254 struct cxl_memdev_sparing_in_payload { 1255 u8 flags; 1256 u8 channel; 1257 u8 rank; 1258 u8 nibble_mask[3]; 1259 u8 bank_group; 1260 u8 bank; 1261 u8 row[3]; 1262 __le16 column; 1263 u8 sub_channel; 1264 } __packed; 1265 1266 static int 1267 cxl_mem_sparing_get_attrbs(struct cxl_mem_sparing_context *cxl_sparing_ctx) 1268 { 1269 size_t rd_data_size = sizeof(struct cxl_memdev_sparing_rd_attrbs); 1270 struct cxl_memdev *cxlmd = cxl_sparing_ctx->cxlmd; 1271 struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox; 1272 u16 restriction_flags; 1273 ssize_t data_size; 1274 u16 return_code; 1275 struct cxl_memdev_sparing_rd_attrbs *rd_attrbs __free(kfree) = 1276 kzalloc(rd_data_size, GFP_KERNEL); 1277 if (!rd_attrbs) 1278 return -ENOMEM; 1279 1280 data_size = cxl_get_feature(cxl_mbox, &cxl_sparing_ctx->repair_uuid, 1281 CXL_GET_FEAT_SEL_CURRENT_VALUE, rd_attrbs, 1282 rd_data_size, 0, &return_code); 1283 if (data_size < 0) 1284 return data_size; 1285 if ((size_t)data_size != rd_data_size) 1286 return -EIO; 1287 1288 cxl_sparing_ctx->op_class = rd_attrbs->hdr.op_class; 1289 cxl_sparing_ctx->op_subclass = rd_attrbs->hdr.op_subclass; 1290 restriction_flags = le16_to_cpu(rd_attrbs->restriction_flags); 1291 cxl_sparing_ctx->cap_safe_when_in_use = 1292 CXL_GET_SPARING_SAFE_IN_USE(restriction_flags); 1293 cxl_sparing_ctx->cap_hard_sparing = 1294 CXL_GET_CAP_HARD_SPARING(restriction_flags); 1295 cxl_sparing_ctx->cap_soft_sparing = 1296 CXL_GET_CAP_SOFT_SPARING(restriction_flags); 1297 1298 return 0; 1299 } 1300 1301 static struct cxl_event_dram * 1302 cxl_mem_get_rec_dram(struct cxl_memdev *cxlmd, 1303 struct cxl_mem_sparing_context *ctx) 1304 { 1305 struct cxl_mem_repair_attrbs attrbs = { 0 }; 1306 1307 attrbs.dpa = ctx->dpa; 1308 attrbs.channel = ctx->channel; 1309 attrbs.rank = ctx->rank; 1310 attrbs.nibble_mask = ctx->nibble_mask; 1311 switch (ctx->repair_type) { 1312 case EDAC_REPAIR_CACHELINE_SPARING: 1313 attrbs.repair_type = CXL_CACHELINE_SPARING; 1314 attrbs.bank_group = ctx->bank_group; 1315 attrbs.bank = ctx->bank; 1316 attrbs.row = ctx->row; 1317 attrbs.column = ctx->column; 1318 attrbs.sub_channel = ctx->sub_channel; 1319 break; 1320 case EDAC_REPAIR_ROW_SPARING: 1321 attrbs.repair_type = CXL_ROW_SPARING; 1322 attrbs.bank_group = ctx->bank_group; 1323 attrbs.bank = ctx->bank; 1324 attrbs.row = ctx->row; 1325 break; 1326 case EDAC_REPAIR_BANK_SPARING: 1327 attrbs.repair_type = CXL_BANK_SPARING; 1328 attrbs.bank_group = ctx->bank_group; 1329 attrbs.bank = ctx->bank; 1330 break; 1331 case EDAC_REPAIR_RANK_SPARING: 1332 attrbs.repair_type = CXL_RANK_SPARING; 1333 break; 1334 default: 1335 return NULL; 1336 } 1337 1338 return cxl_find_rec_dram(cxlmd, &attrbs); 1339 } 1340 1341 static int 1342 cxl_mem_perform_sparing(struct device *dev, 1343 struct cxl_mem_sparing_context *cxl_sparing_ctx) 1344 { 1345 struct cxl_memdev *cxlmd = cxl_sparing_ctx->cxlmd; 1346 struct cxl_memdev_sparing_in_payload sparing_pi; 1347 struct cxl_event_dram *rec = NULL; 1348 u16 validity_flags = 0; 1349 int ret; 1350 1351 ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region); 1352 if ((ret = ACQUIRE_ERR(rwsem_read_intr, ®ion_rwsem))) 1353 return ret; 1354 1355 ACQUIRE(rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa); 1356 if ((ret = ACQUIRE_ERR(rwsem_read_intr, &dpa_rwsem))) 1357 return ret; 1358 1359 if (!cxl_sparing_ctx->cap_safe_when_in_use) { 1360 /* Memory to repair must be offline */ 1361 if (cxl_is_memdev_memory_online(cxlmd)) 1362 return -EBUSY; 1363 } else { 1364 if (cxl_is_memdev_memory_online(cxlmd)) { 1365 rec = cxl_mem_get_rec_dram(cxlmd, cxl_sparing_ctx); 1366 if (!rec) 1367 return -EINVAL; 1368 1369 if (!get_unaligned_le16(rec->media_hdr.validity_flags)) 1370 return -EINVAL; 1371 } 1372 } 1373 1374 memset(&sparing_pi, 0, sizeof(sparing_pi)); 1375 sparing_pi.flags = CXL_SET_SPARING_QUERY_RESOURCE(0); 1376 if (cxl_sparing_ctx->persist_mode) 1377 sparing_pi.flags |= CXL_SET_HARD_SPARING(1); 1378 1379 if (rec) 1380 validity_flags = get_unaligned_le16(rec->media_hdr.validity_flags); 1381 1382 switch (cxl_sparing_ctx->repair_type) { 1383 case EDAC_REPAIR_CACHELINE_SPARING: 1384 sparing_pi.column = cpu_to_le16(cxl_sparing_ctx->column); 1385 if (!rec || (validity_flags & CXL_DER_VALID_SUB_CHANNEL)) { 1386 sparing_pi.flags |= CXL_SET_SPARING_SUB_CHNL_VALID(1); 1387 sparing_pi.sub_channel = cxl_sparing_ctx->sub_channel; 1388 } 1389 fallthrough; 1390 case EDAC_REPAIR_ROW_SPARING: 1391 put_unaligned_le24(cxl_sparing_ctx->row, sparing_pi.row); 1392 fallthrough; 1393 case EDAC_REPAIR_BANK_SPARING: 1394 sparing_pi.bank_group = cxl_sparing_ctx->bank_group; 1395 sparing_pi.bank = cxl_sparing_ctx->bank; 1396 fallthrough; 1397 case EDAC_REPAIR_RANK_SPARING: 1398 sparing_pi.rank = cxl_sparing_ctx->rank; 1399 fallthrough; 1400 default: 1401 sparing_pi.channel = cxl_sparing_ctx->channel; 1402 if ((rec && (validity_flags & CXL_DER_VALID_NIBBLE)) || 1403 (!rec && (!cxl_sparing_ctx->nibble_mask || 1404 (cxl_sparing_ctx->nibble_mask & 0xFFFFFF)))) { 1405 sparing_pi.flags |= CXL_SET_SPARING_NIB_MASK_VALID(1); 1406 put_unaligned_le24(cxl_sparing_ctx->nibble_mask, 1407 sparing_pi.nibble_mask); 1408 } 1409 break; 1410 } 1411 1412 return cxl_perform_maintenance(&cxlmd->cxlds->cxl_mbox, 1413 cxl_sparing_ctx->op_class, 1414 cxl_sparing_ctx->op_subclass, 1415 &sparing_pi, sizeof(sparing_pi)); 1416 } 1417 1418 static int cxl_mem_sparing_get_repair_type(struct device *dev, void *drv_data, 1419 const char **repair_type) 1420 { 1421 struct cxl_mem_sparing_context *ctx = drv_data; 1422 1423 switch (ctx->repair_type) { 1424 case EDAC_REPAIR_CACHELINE_SPARING: 1425 case EDAC_REPAIR_ROW_SPARING: 1426 case EDAC_REPAIR_BANK_SPARING: 1427 case EDAC_REPAIR_RANK_SPARING: 1428 *repair_type = edac_repair_type[ctx->repair_type]; 1429 break; 1430 default: 1431 return -EINVAL; 1432 } 1433 1434 return 0; 1435 } 1436 1437 #define CXL_SPARING_GET_ATTR(attrb, data_type) \ 1438 static int cxl_mem_sparing_get_##attrb( \ 1439 struct device *dev, void *drv_data, data_type *val) \ 1440 { \ 1441 struct cxl_mem_sparing_context *ctx = drv_data; \ 1442 \ 1443 *val = ctx->attrb; \ 1444 \ 1445 return 0; \ 1446 } 1447 CXL_SPARING_GET_ATTR(persist_mode, bool) 1448 CXL_SPARING_GET_ATTR(dpa, u64) 1449 CXL_SPARING_GET_ATTR(nibble_mask, u32) 1450 CXL_SPARING_GET_ATTR(bank_group, u32) 1451 CXL_SPARING_GET_ATTR(bank, u32) 1452 CXL_SPARING_GET_ATTR(rank, u32) 1453 CXL_SPARING_GET_ATTR(row, u32) 1454 CXL_SPARING_GET_ATTR(column, u32) 1455 CXL_SPARING_GET_ATTR(channel, u32) 1456 CXL_SPARING_GET_ATTR(sub_channel, u32) 1457 1458 #define CXL_SPARING_SET_ATTR(attrb, data_type) \ 1459 static int cxl_mem_sparing_set_##attrb(struct device *dev, \ 1460 void *drv_data, data_type val) \ 1461 { \ 1462 struct cxl_mem_sparing_context *ctx = drv_data; \ 1463 \ 1464 ctx->attrb = val; \ 1465 \ 1466 return 0; \ 1467 } 1468 CXL_SPARING_SET_ATTR(nibble_mask, u32) 1469 CXL_SPARING_SET_ATTR(bank_group, u32) 1470 CXL_SPARING_SET_ATTR(bank, u32) 1471 CXL_SPARING_SET_ATTR(rank, u32) 1472 CXL_SPARING_SET_ATTR(row, u32) 1473 CXL_SPARING_SET_ATTR(column, u32) 1474 CXL_SPARING_SET_ATTR(channel, u32) 1475 CXL_SPARING_SET_ATTR(sub_channel, u32) 1476 1477 static int cxl_mem_sparing_set_persist_mode(struct device *dev, void *drv_data, 1478 bool persist_mode) 1479 { 1480 struct cxl_mem_sparing_context *ctx = drv_data; 1481 1482 if ((persist_mode && ctx->cap_hard_sparing) || 1483 (!persist_mode && ctx->cap_soft_sparing)) 1484 ctx->persist_mode = persist_mode; 1485 else 1486 return -EOPNOTSUPP; 1487 1488 return 0; 1489 } 1490 1491 static int cxl_get_mem_sparing_safe_when_in_use(struct device *dev, 1492 void *drv_data, bool *safe) 1493 { 1494 struct cxl_mem_sparing_context *ctx = drv_data; 1495 1496 *safe = ctx->cap_safe_when_in_use; 1497 1498 return 0; 1499 } 1500 1501 static int cxl_mem_sparing_get_min_dpa(struct device *dev, void *drv_data, 1502 u64 *min_dpa) 1503 { 1504 struct cxl_mem_sparing_context *ctx = drv_data; 1505 struct cxl_memdev *cxlmd = ctx->cxlmd; 1506 struct cxl_dev_state *cxlds = cxlmd->cxlds; 1507 1508 *min_dpa = cxlds->dpa_res.start; 1509 1510 return 0; 1511 } 1512 1513 static int cxl_mem_sparing_get_max_dpa(struct device *dev, void *drv_data, 1514 u64 *max_dpa) 1515 { 1516 struct cxl_mem_sparing_context *ctx = drv_data; 1517 struct cxl_memdev *cxlmd = ctx->cxlmd; 1518 struct cxl_dev_state *cxlds = cxlmd->cxlds; 1519 1520 *max_dpa = cxlds->dpa_res.end; 1521 1522 return 0; 1523 } 1524 1525 static int cxl_mem_sparing_set_dpa(struct device *dev, void *drv_data, u64 dpa) 1526 { 1527 struct cxl_mem_sparing_context *ctx = drv_data; 1528 struct cxl_memdev *cxlmd = ctx->cxlmd; 1529 struct cxl_dev_state *cxlds = cxlmd->cxlds; 1530 1531 if (!cxl_resource_contains_addr(&cxlds->dpa_res, dpa)) 1532 return -EINVAL; 1533 1534 ctx->dpa = dpa; 1535 1536 return 0; 1537 } 1538 1539 static int cxl_do_mem_sparing(struct device *dev, void *drv_data, u32 val) 1540 { 1541 struct cxl_mem_sparing_context *ctx = drv_data; 1542 1543 if (val != EDAC_DO_MEM_REPAIR) 1544 return -EINVAL; 1545 1546 return cxl_mem_perform_sparing(dev, ctx); 1547 } 1548 1549 #define RANK_OPS \ 1550 .get_repair_type = cxl_mem_sparing_get_repair_type, \ 1551 .get_persist_mode = cxl_mem_sparing_get_persist_mode, \ 1552 .set_persist_mode = cxl_mem_sparing_set_persist_mode, \ 1553 .get_repair_safe_when_in_use = cxl_get_mem_sparing_safe_when_in_use, \ 1554 .get_min_dpa = cxl_mem_sparing_get_min_dpa, \ 1555 .get_max_dpa = cxl_mem_sparing_get_max_dpa, \ 1556 .get_dpa = cxl_mem_sparing_get_dpa, \ 1557 .set_dpa = cxl_mem_sparing_set_dpa, \ 1558 .get_nibble_mask = cxl_mem_sparing_get_nibble_mask, \ 1559 .set_nibble_mask = cxl_mem_sparing_set_nibble_mask, \ 1560 .get_rank = cxl_mem_sparing_get_rank, \ 1561 .set_rank = cxl_mem_sparing_set_rank, \ 1562 .get_channel = cxl_mem_sparing_get_channel, \ 1563 .set_channel = cxl_mem_sparing_set_channel, \ 1564 .do_repair = cxl_do_mem_sparing 1565 1566 #define BANK_OPS \ 1567 RANK_OPS, .get_bank_group = cxl_mem_sparing_get_bank_group, \ 1568 .set_bank_group = cxl_mem_sparing_set_bank_group, \ 1569 .get_bank = cxl_mem_sparing_get_bank, \ 1570 .set_bank = cxl_mem_sparing_set_bank 1571 1572 #define ROW_OPS \ 1573 BANK_OPS, .get_row = cxl_mem_sparing_get_row, \ 1574 .set_row = cxl_mem_sparing_set_row 1575 1576 #define CACHELINE_OPS \ 1577 ROW_OPS, .get_column = cxl_mem_sparing_get_column, \ 1578 .set_column = cxl_mem_sparing_set_column, \ 1579 .get_sub_channel = cxl_mem_sparing_get_sub_channel, \ 1580 .set_sub_channel = cxl_mem_sparing_set_sub_channel 1581 1582 static const struct edac_mem_repair_ops cxl_rank_sparing_ops = { 1583 RANK_OPS, 1584 }; 1585 1586 static const struct edac_mem_repair_ops cxl_bank_sparing_ops = { 1587 BANK_OPS, 1588 }; 1589 1590 static const struct edac_mem_repair_ops cxl_row_sparing_ops = { 1591 ROW_OPS, 1592 }; 1593 1594 static const struct edac_mem_repair_ops cxl_cacheline_sparing_ops = { 1595 CACHELINE_OPS, 1596 }; 1597 1598 struct cxl_mem_sparing_desc { 1599 const uuid_t repair_uuid; 1600 enum edac_mem_repair_type repair_type; 1601 const struct edac_mem_repair_ops *repair_ops; 1602 }; 1603 1604 static const struct cxl_mem_sparing_desc mem_sparing_desc[] = { 1605 { 1606 .repair_uuid = CXL_FEAT_CACHELINE_SPARING_UUID, 1607 .repair_type = EDAC_REPAIR_CACHELINE_SPARING, 1608 .repair_ops = &cxl_cacheline_sparing_ops, 1609 }, 1610 { 1611 .repair_uuid = CXL_FEAT_ROW_SPARING_UUID, 1612 .repair_type = EDAC_REPAIR_ROW_SPARING, 1613 .repair_ops = &cxl_row_sparing_ops, 1614 }, 1615 { 1616 .repair_uuid = CXL_FEAT_BANK_SPARING_UUID, 1617 .repair_type = EDAC_REPAIR_BANK_SPARING, 1618 .repair_ops = &cxl_bank_sparing_ops, 1619 }, 1620 { 1621 .repair_uuid = CXL_FEAT_RANK_SPARING_UUID, 1622 .repair_type = EDAC_REPAIR_RANK_SPARING, 1623 .repair_ops = &cxl_rank_sparing_ops, 1624 }, 1625 }; 1626 1627 static int cxl_memdev_sparing_init(struct cxl_memdev *cxlmd, 1628 struct edac_dev_feature *ras_feature, 1629 const struct cxl_mem_sparing_desc *desc, 1630 u8 repair_inst) 1631 { 1632 struct cxl_mem_sparing_context *cxl_sparing_ctx; 1633 struct cxl_feat_entry *feat_entry; 1634 int ret; 1635 1636 feat_entry = cxl_feature_info(to_cxlfs(cxlmd->cxlds), 1637 &desc->repair_uuid); 1638 if (IS_ERR(feat_entry)) 1639 return -EOPNOTSUPP; 1640 1641 if (!(le32_to_cpu(feat_entry->flags) & CXL_FEATURE_F_CHANGEABLE)) 1642 return -EOPNOTSUPP; 1643 1644 cxl_sparing_ctx = devm_kzalloc(&cxlmd->dev, sizeof(*cxl_sparing_ctx), 1645 GFP_KERNEL); 1646 if (!cxl_sparing_ctx) 1647 return -ENOMEM; 1648 1649 *cxl_sparing_ctx = (struct cxl_mem_sparing_context){ 1650 .get_feat_size = le16_to_cpu(feat_entry->get_feat_size), 1651 .set_feat_size = le16_to_cpu(feat_entry->set_feat_size), 1652 .get_version = feat_entry->get_feat_ver, 1653 .set_version = feat_entry->set_feat_ver, 1654 .effects = le16_to_cpu(feat_entry->effects), 1655 .cxlmd = cxlmd, 1656 .repair_type = desc->repair_type, 1657 .instance = repair_inst++, 1658 }; 1659 uuid_copy(&cxl_sparing_ctx->repair_uuid, &desc->repair_uuid); 1660 1661 ret = cxl_mem_sparing_get_attrbs(cxl_sparing_ctx); 1662 if (ret) 1663 return ret; 1664 1665 if ((cxl_sparing_ctx->cap_soft_sparing && 1666 cxl_sparing_ctx->cap_hard_sparing) || 1667 cxl_sparing_ctx->cap_soft_sparing) 1668 cxl_sparing_ctx->persist_mode = 0; 1669 else if (cxl_sparing_ctx->cap_hard_sparing) 1670 cxl_sparing_ctx->persist_mode = 1; 1671 else 1672 return -EOPNOTSUPP; 1673 1674 ras_feature->ft_type = RAS_FEAT_MEM_REPAIR; 1675 ras_feature->instance = cxl_sparing_ctx->instance; 1676 ras_feature->mem_repair_ops = desc->repair_ops; 1677 ras_feature->ctx = cxl_sparing_ctx; 1678 1679 return 0; 1680 } 1681 1682 /* 1683 * CXL memory soft PPR & hard PPR control 1684 */ 1685 struct cxl_ppr_context { 1686 uuid_t repair_uuid; 1687 u8 instance; 1688 u16 get_feat_size; 1689 u16 set_feat_size; 1690 u8 get_version; 1691 u8 set_version; 1692 u16 effects; 1693 u8 op_class; 1694 u8 op_subclass; 1695 bool cap_dpa; 1696 bool cap_nib_mask; 1697 bool media_accessible; 1698 bool data_retained; 1699 struct cxl_memdev *cxlmd; 1700 enum edac_mem_repair_type repair_type; 1701 bool persist_mode; 1702 u64 dpa; 1703 u32 nibble_mask; 1704 }; 1705 1706 /* 1707 * See CXL rev 3.2 @8.2.10.7.2.1 Table 8-128 sPPR Feature Readable Attributes 1708 * 1709 * See CXL rev 3.2 @8.2.10.7.2.2 Table 8-131 hPPR Feature Readable Attributes 1710 */ 1711 1712 #define CXL_PPR_OP_CAP_DEVICE_INITIATED BIT(0) 1713 #define CXL_PPR_OP_MODE_DEV_INITIATED BIT(0) 1714 1715 #define CXL_PPR_FLAG_DPA_SUPPORT_MASK BIT(0) 1716 #define CXL_PPR_FLAG_NIB_SUPPORT_MASK BIT(1) 1717 #define CXL_PPR_FLAG_MEM_SPARING_EV_REC_SUPPORT_MASK BIT(2) 1718 #define CXL_PPR_FLAG_DEV_INITED_PPR_AT_BOOT_CAP_MASK BIT(3) 1719 1720 #define CXL_PPR_RESTRICTION_FLAG_MEDIA_ACCESSIBLE_MASK BIT(0) 1721 #define CXL_PPR_RESTRICTION_FLAG_DATA_RETAINED_MASK BIT(2) 1722 1723 #define CXL_PPR_SPARING_EV_REC_EN_MASK BIT(0) 1724 #define CXL_PPR_DEV_INITED_PPR_AT_BOOT_EN_MASK BIT(1) 1725 1726 #define CXL_PPR_GET_CAP_DPA(flags) \ 1727 FIELD_GET(CXL_PPR_FLAG_DPA_SUPPORT_MASK, flags) 1728 #define CXL_PPR_GET_CAP_NIB_MASK(flags) \ 1729 FIELD_GET(CXL_PPR_FLAG_NIB_SUPPORT_MASK, flags) 1730 #define CXL_PPR_GET_MEDIA_ACCESSIBLE(restriction_flags) \ 1731 (FIELD_GET(CXL_PPR_RESTRICTION_FLAG_MEDIA_ACCESSIBLE_MASK, \ 1732 restriction_flags) ^ 1) 1733 #define CXL_PPR_GET_DATA_RETAINED(restriction_flags) \ 1734 (FIELD_GET(CXL_PPR_RESTRICTION_FLAG_DATA_RETAINED_MASK, \ 1735 restriction_flags) ^ 1) 1736 1737 struct cxl_memdev_ppr_rd_attrbs { 1738 struct cxl_memdev_repair_rd_attrbs_hdr hdr; 1739 u8 ppr_flags; 1740 __le16 restriction_flags; 1741 u8 ppr_op_mode; 1742 } __packed; 1743 1744 /* 1745 * See CXL rev 3.2 @8.2.10.7.1.2 Table 8-118 sPPR Maintenance Input Payload 1746 * 1747 * See CXL rev 3.2 @8.2.10.7.1.3 Table 8-119 hPPR Maintenance Input Payload 1748 */ 1749 struct cxl_memdev_ppr_maintenance_attrbs { 1750 u8 flags; 1751 __le64 dpa; 1752 u8 nibble_mask[3]; 1753 } __packed; 1754 1755 static int cxl_mem_ppr_get_attrbs(struct cxl_ppr_context *cxl_ppr_ctx) 1756 { 1757 size_t rd_data_size = sizeof(struct cxl_memdev_ppr_rd_attrbs); 1758 struct cxl_memdev *cxlmd = cxl_ppr_ctx->cxlmd; 1759 struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox; 1760 u16 restriction_flags; 1761 ssize_t data_size; 1762 u16 return_code; 1763 1764 struct cxl_memdev_ppr_rd_attrbs *rd_attrbs __free(kfree) = 1765 kmalloc(rd_data_size, GFP_KERNEL); 1766 if (!rd_attrbs) 1767 return -ENOMEM; 1768 1769 data_size = cxl_get_feature(cxl_mbox, &cxl_ppr_ctx->repair_uuid, 1770 CXL_GET_FEAT_SEL_CURRENT_VALUE, rd_attrbs, 1771 rd_data_size, 0, &return_code); 1772 if (data_size < 0) 1773 return data_size; 1774 if ((size_t)data_size != rd_data_size) 1775 return -EIO; 1776 1777 cxl_ppr_ctx->op_class = rd_attrbs->hdr.op_class; 1778 cxl_ppr_ctx->op_subclass = rd_attrbs->hdr.op_subclass; 1779 cxl_ppr_ctx->cap_dpa = CXL_PPR_GET_CAP_DPA(rd_attrbs->ppr_flags); 1780 cxl_ppr_ctx->cap_nib_mask = 1781 CXL_PPR_GET_CAP_NIB_MASK(rd_attrbs->ppr_flags); 1782 1783 restriction_flags = le16_to_cpu(rd_attrbs->restriction_flags); 1784 cxl_ppr_ctx->media_accessible = 1785 CXL_PPR_GET_MEDIA_ACCESSIBLE(restriction_flags); 1786 cxl_ppr_ctx->data_retained = 1787 CXL_PPR_GET_DATA_RETAINED(restriction_flags); 1788 1789 return 0; 1790 } 1791 1792 static int cxl_mem_perform_ppr(struct cxl_ppr_context *cxl_ppr_ctx) 1793 { 1794 struct cxl_memdev_ppr_maintenance_attrbs maintenance_attrbs; 1795 struct cxl_memdev *cxlmd = cxl_ppr_ctx->cxlmd; 1796 struct cxl_mem_repair_attrbs attrbs = { 0 }; 1797 int ret; 1798 1799 ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region); 1800 if ((ret = ACQUIRE_ERR(rwsem_read_intr, ®ion_rwsem))) 1801 return ret; 1802 1803 ACQUIRE(rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa); 1804 if ((ret = ACQUIRE_ERR(rwsem_read_intr, &dpa_rwsem))) 1805 return ret; 1806 1807 if (!cxl_ppr_ctx->media_accessible || !cxl_ppr_ctx->data_retained) { 1808 /* Memory to repair must be offline */ 1809 if (cxl_is_memdev_memory_online(cxlmd)) 1810 return -EBUSY; 1811 } else { 1812 if (cxl_is_memdev_memory_online(cxlmd)) { 1813 /* Check memory to repair is from the current boot */ 1814 attrbs.repair_type = CXL_PPR; 1815 attrbs.dpa = cxl_ppr_ctx->dpa; 1816 attrbs.nibble_mask = cxl_ppr_ctx->nibble_mask; 1817 if (!cxl_find_rec_dram(cxlmd, &attrbs) && 1818 !cxl_find_rec_gen_media(cxlmd, &attrbs)) 1819 return -EINVAL; 1820 } 1821 } 1822 1823 memset(&maintenance_attrbs, 0, sizeof(maintenance_attrbs)); 1824 maintenance_attrbs.flags = 0; 1825 maintenance_attrbs.dpa = cpu_to_le64(cxl_ppr_ctx->dpa); 1826 put_unaligned_le24(cxl_ppr_ctx->nibble_mask, 1827 maintenance_attrbs.nibble_mask); 1828 1829 return cxl_perform_maintenance(&cxlmd->cxlds->cxl_mbox, 1830 cxl_ppr_ctx->op_class, 1831 cxl_ppr_ctx->op_subclass, 1832 &maintenance_attrbs, 1833 sizeof(maintenance_attrbs)); 1834 } 1835 1836 static int cxl_ppr_get_repair_type(struct device *dev, void *drv_data, 1837 const char **repair_type) 1838 { 1839 *repair_type = edac_repair_type[EDAC_REPAIR_PPR]; 1840 1841 return 0; 1842 } 1843 1844 static int cxl_ppr_get_persist_mode(struct device *dev, void *drv_data, 1845 bool *persist_mode) 1846 { 1847 struct cxl_ppr_context *cxl_ppr_ctx = drv_data; 1848 1849 *persist_mode = cxl_ppr_ctx->persist_mode; 1850 1851 return 0; 1852 } 1853 1854 static int cxl_get_ppr_safe_when_in_use(struct device *dev, void *drv_data, 1855 bool *safe) 1856 { 1857 struct cxl_ppr_context *cxl_ppr_ctx = drv_data; 1858 1859 *safe = cxl_ppr_ctx->media_accessible & cxl_ppr_ctx->data_retained; 1860 1861 return 0; 1862 } 1863 1864 static int cxl_ppr_get_min_dpa(struct device *dev, void *drv_data, u64 *min_dpa) 1865 { 1866 struct cxl_ppr_context *cxl_ppr_ctx = drv_data; 1867 struct cxl_memdev *cxlmd = cxl_ppr_ctx->cxlmd; 1868 struct cxl_dev_state *cxlds = cxlmd->cxlds; 1869 1870 *min_dpa = cxlds->dpa_res.start; 1871 1872 return 0; 1873 } 1874 1875 static int cxl_ppr_get_max_dpa(struct device *dev, void *drv_data, u64 *max_dpa) 1876 { 1877 struct cxl_ppr_context *cxl_ppr_ctx = drv_data; 1878 struct cxl_memdev *cxlmd = cxl_ppr_ctx->cxlmd; 1879 struct cxl_dev_state *cxlds = cxlmd->cxlds; 1880 1881 *max_dpa = cxlds->dpa_res.end; 1882 1883 return 0; 1884 } 1885 1886 static int cxl_ppr_get_dpa(struct device *dev, void *drv_data, u64 *dpa) 1887 { 1888 struct cxl_ppr_context *cxl_ppr_ctx = drv_data; 1889 1890 *dpa = cxl_ppr_ctx->dpa; 1891 1892 return 0; 1893 } 1894 1895 static int cxl_ppr_set_dpa(struct device *dev, void *drv_data, u64 dpa) 1896 { 1897 struct cxl_ppr_context *cxl_ppr_ctx = drv_data; 1898 struct cxl_memdev *cxlmd = cxl_ppr_ctx->cxlmd; 1899 struct cxl_dev_state *cxlds = cxlmd->cxlds; 1900 1901 if (!cxl_resource_contains_addr(&cxlds->dpa_res, dpa)) 1902 return -EINVAL; 1903 1904 cxl_ppr_ctx->dpa = dpa; 1905 1906 return 0; 1907 } 1908 1909 static int cxl_ppr_get_nibble_mask(struct device *dev, void *drv_data, 1910 u32 *nibble_mask) 1911 { 1912 struct cxl_ppr_context *cxl_ppr_ctx = drv_data; 1913 1914 *nibble_mask = cxl_ppr_ctx->nibble_mask; 1915 1916 return 0; 1917 } 1918 1919 static int cxl_ppr_set_nibble_mask(struct device *dev, void *drv_data, 1920 u32 nibble_mask) 1921 { 1922 struct cxl_ppr_context *cxl_ppr_ctx = drv_data; 1923 1924 cxl_ppr_ctx->nibble_mask = nibble_mask; 1925 1926 return 0; 1927 } 1928 1929 static int cxl_do_ppr(struct device *dev, void *drv_data, u32 val) 1930 { 1931 struct cxl_ppr_context *cxl_ppr_ctx = drv_data; 1932 struct cxl_memdev *cxlmd = cxl_ppr_ctx->cxlmd; 1933 struct cxl_dev_state *cxlds = cxlmd->cxlds; 1934 1935 if (val != EDAC_DO_MEM_REPAIR || 1936 !cxl_resource_contains_addr(&cxlds->dpa_res, cxl_ppr_ctx->dpa)) 1937 return -EINVAL; 1938 1939 return cxl_mem_perform_ppr(cxl_ppr_ctx); 1940 } 1941 1942 static const struct edac_mem_repair_ops cxl_sppr_ops = { 1943 .get_repair_type = cxl_ppr_get_repair_type, 1944 .get_persist_mode = cxl_ppr_get_persist_mode, 1945 .get_repair_safe_when_in_use = cxl_get_ppr_safe_when_in_use, 1946 .get_min_dpa = cxl_ppr_get_min_dpa, 1947 .get_max_dpa = cxl_ppr_get_max_dpa, 1948 .get_dpa = cxl_ppr_get_dpa, 1949 .set_dpa = cxl_ppr_set_dpa, 1950 .get_nibble_mask = cxl_ppr_get_nibble_mask, 1951 .set_nibble_mask = cxl_ppr_set_nibble_mask, 1952 .do_repair = cxl_do_ppr, 1953 }; 1954 1955 static int cxl_memdev_soft_ppr_init(struct cxl_memdev *cxlmd, 1956 struct edac_dev_feature *ras_feature, 1957 u8 repair_inst) 1958 { 1959 struct cxl_ppr_context *cxl_sppr_ctx; 1960 struct cxl_feat_entry *feat_entry; 1961 int ret; 1962 1963 feat_entry = cxl_feature_info(to_cxlfs(cxlmd->cxlds), 1964 &CXL_FEAT_SPPR_UUID); 1965 if (IS_ERR(feat_entry)) 1966 return -EOPNOTSUPP; 1967 1968 if (!(le32_to_cpu(feat_entry->flags) & CXL_FEATURE_F_CHANGEABLE)) 1969 return -EOPNOTSUPP; 1970 1971 cxl_sppr_ctx = 1972 devm_kzalloc(&cxlmd->dev, sizeof(*cxl_sppr_ctx), GFP_KERNEL); 1973 if (!cxl_sppr_ctx) 1974 return -ENOMEM; 1975 1976 *cxl_sppr_ctx = (struct cxl_ppr_context){ 1977 .get_feat_size = le16_to_cpu(feat_entry->get_feat_size), 1978 .set_feat_size = le16_to_cpu(feat_entry->set_feat_size), 1979 .get_version = feat_entry->get_feat_ver, 1980 .set_version = feat_entry->set_feat_ver, 1981 .effects = le16_to_cpu(feat_entry->effects), 1982 .cxlmd = cxlmd, 1983 .repair_type = EDAC_REPAIR_PPR, 1984 .persist_mode = 0, 1985 .instance = repair_inst, 1986 }; 1987 uuid_copy(&cxl_sppr_ctx->repair_uuid, &CXL_FEAT_SPPR_UUID); 1988 1989 ret = cxl_mem_ppr_get_attrbs(cxl_sppr_ctx); 1990 if (ret) 1991 return ret; 1992 1993 ras_feature->ft_type = RAS_FEAT_MEM_REPAIR; 1994 ras_feature->instance = cxl_sppr_ctx->instance; 1995 ras_feature->mem_repair_ops = &cxl_sppr_ops; 1996 ras_feature->ctx = cxl_sppr_ctx; 1997 1998 return 0; 1999 } 2000 2001 static void err_rec_free(void *_cxlmd) 2002 { 2003 struct cxl_memdev *cxlmd = _cxlmd; 2004 struct cxl_mem_err_rec *array_rec = cxlmd->err_rec_array; 2005 struct cxl_event_gen_media *rec_gen_media; 2006 struct cxl_event_dram *rec_dram; 2007 unsigned long index; 2008 2009 cxlmd->err_rec_array = NULL; 2010 xa_for_each(&array_rec->rec_dram, index, rec_dram) 2011 kfree(rec_dram); 2012 xa_destroy(&array_rec->rec_dram); 2013 2014 xa_for_each(&array_rec->rec_gen_media, index, rec_gen_media) 2015 kfree(rec_gen_media); 2016 xa_destroy(&array_rec->rec_gen_media); 2017 kfree(array_rec); 2018 } 2019 2020 static int devm_cxl_memdev_setup_err_rec(struct cxl_memdev *cxlmd) 2021 { 2022 struct cxl_mem_err_rec *array_rec = kzalloc_obj(*array_rec); 2023 2024 if (!array_rec) 2025 return -ENOMEM; 2026 2027 xa_init(&array_rec->rec_gen_media); 2028 xa_init(&array_rec->rec_dram); 2029 cxlmd->err_rec_array = array_rec; 2030 2031 return devm_add_action_or_reset(&cxlmd->dev, err_rec_free, cxlmd); 2032 } 2033 2034 int devm_cxl_memdev_edac_register(struct cxl_memdev *cxlmd) 2035 { 2036 struct edac_dev_feature ras_features[CXL_NR_EDAC_DEV_FEATURES]; 2037 int num_ras_features = 0; 2038 u8 repair_inst = 0; 2039 int rc; 2040 2041 if (IS_ENABLED(CONFIG_CXL_EDAC_SCRUB)) { 2042 rc = cxl_memdev_scrub_init(cxlmd, &ras_features[num_ras_features], 0); 2043 if (rc < 0 && rc != -EOPNOTSUPP) 2044 return rc; 2045 2046 if (rc != -EOPNOTSUPP) 2047 num_ras_features++; 2048 } 2049 2050 if (IS_ENABLED(CONFIG_CXL_EDAC_ECS)) { 2051 rc = cxl_memdev_ecs_init(cxlmd, &ras_features[num_ras_features]); 2052 if (rc < 0 && rc != -EOPNOTSUPP) 2053 return rc; 2054 2055 if (rc != -EOPNOTSUPP) 2056 num_ras_features++; 2057 } 2058 2059 if (IS_ENABLED(CONFIG_CXL_EDAC_MEM_REPAIR)) { 2060 for (int i = 0; i < CXL_MEM_SPARING_MAX; i++) { 2061 rc = cxl_memdev_sparing_init(cxlmd, 2062 &ras_features[num_ras_features], 2063 &mem_sparing_desc[i], repair_inst); 2064 if (rc == -EOPNOTSUPP) 2065 continue; 2066 if (rc < 0) 2067 return rc; 2068 2069 repair_inst++; 2070 num_ras_features++; 2071 } 2072 2073 rc = cxl_memdev_soft_ppr_init(cxlmd, &ras_features[num_ras_features], 2074 repair_inst); 2075 if (rc < 0 && rc != -EOPNOTSUPP) 2076 return rc; 2077 2078 if (rc != -EOPNOTSUPP) { 2079 repair_inst++; 2080 num_ras_features++; 2081 } 2082 2083 if (repair_inst) { 2084 rc = devm_cxl_memdev_setup_err_rec(cxlmd); 2085 if (rc) 2086 return rc; 2087 } 2088 } 2089 2090 if (!num_ras_features) 2091 return -EINVAL; 2092 2093 char *cxl_dev_name __free(kfree) = 2094 kasprintf(GFP_KERNEL, "cxl_%s", dev_name(&cxlmd->dev)); 2095 if (!cxl_dev_name) 2096 return -ENOMEM; 2097 2098 return edac_dev_register(&cxlmd->dev, cxl_dev_name, NULL, 2099 num_ras_features, ras_features); 2100 } 2101 EXPORT_SYMBOL_NS_GPL(devm_cxl_memdev_edac_register, "CXL"); 2102 2103 int devm_cxl_region_edac_register(struct cxl_region *cxlr) 2104 { 2105 struct edac_dev_feature ras_features[CXL_NR_EDAC_DEV_FEATURES]; 2106 int num_ras_features = 0; 2107 int rc; 2108 2109 if (!IS_ENABLED(CONFIG_CXL_EDAC_SCRUB)) 2110 return 0; 2111 2112 rc = cxl_region_scrub_init(cxlr, &ras_features[num_ras_features], 0); 2113 if (rc < 0) 2114 return rc; 2115 2116 num_ras_features++; 2117 2118 char *cxl_dev_name __free(kfree) = 2119 kasprintf(GFP_KERNEL, "cxl_%s", dev_name(&cxlr->dev)); 2120 if (!cxl_dev_name) 2121 return -ENOMEM; 2122 2123 return edac_dev_register(&cxlr->dev, cxl_dev_name, NULL, 2124 num_ras_features, ras_features); 2125 } 2126 EXPORT_SYMBOL_NS_GPL(devm_cxl_region_edac_register, "CXL"); 2127 2128 2129