1 // SPDX-License-Identifier: GPL-2.0 2 3 #define pr_fmt(fmt) "papr-scm: " fmt 4 5 #include <linux/of.h> 6 #include <linux/kernel.h> 7 #include <linux/module.h> 8 #include <linux/ioport.h> 9 #include <linux/slab.h> 10 #include <linux/ndctl.h> 11 #include <linux/sched.h> 12 #include <linux/libnvdimm.h> 13 #include <linux/platform_device.h> 14 #include <linux/delay.h> 15 16 #include <asm/plpar_wrappers.h> 17 18 #define BIND_ANY_ADDR (~0ul) 19 20 #define PAPR_SCM_DIMM_CMD_MASK \ 21 ((1ul << ND_CMD_GET_CONFIG_SIZE) | \ 22 (1ul << ND_CMD_GET_CONFIG_DATA) | \ 23 (1ul << ND_CMD_SET_CONFIG_DATA)) 24 25 struct papr_scm_priv { 26 struct platform_device *pdev; 27 struct device_node *dn; 28 uint32_t drc_index; 29 uint64_t blocks; 30 uint64_t block_size; 31 int metadata_size; 32 bool is_volatile; 33 34 uint64_t bound_addr; 35 36 struct nvdimm_bus_descriptor bus_desc; 37 struct nvdimm_bus *bus; 38 struct nvdimm *nvdimm; 39 struct resource res; 40 struct nd_region *region; 41 struct nd_interleave_set nd_set; 42 }; 43 44 static int drc_pmem_bind(struct papr_scm_priv *p) 45 { 46 unsigned long ret[PLPAR_HCALL_BUFSIZE]; 47 uint64_t saved = 0; 48 uint64_t token; 49 int64_t rc; 50 51 /* 52 * When the hypervisor cannot map all the requested memory in a single 53 * hcall it returns H_BUSY and we call again with the token until 54 * we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS 55 * leave the system in an undefined state, so we wait. 56 */ 57 token = 0; 58 59 do { 60 rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0, 61 p->blocks, BIND_ANY_ADDR, token); 62 token = ret[0]; 63 if (!saved) 64 saved = ret[1]; 65 cond_resched(); 66 } while (rc == H_BUSY); 67 68 if (rc) 69 return rc; 70 71 p->bound_addr = saved; 72 dev_dbg(&p->pdev->dev, "bound drc 0x%x to %pR\n", p->drc_index, &p->res); 73 return rc; 74 } 75 76 static void drc_pmem_unbind(struct papr_scm_priv *p) 77 { 78 unsigned long ret[PLPAR_HCALL_BUFSIZE]; 79 uint64_t token = 0; 80 int64_t rc; 81 82 dev_dbg(&p->pdev->dev, "unbind drc 0x%x\n", p->drc_index); 83 84 /* NB: unbind has the same retry requirements as drc_pmem_bind() */ 85 do { 86 87 /* Unbind of all SCM resources associated with drcIndex */ 88 rc = plpar_hcall(H_SCM_UNBIND_ALL, ret, H_UNBIND_SCOPE_DRC, 89 p->drc_index, token); 90 token = ret[0]; 91 92 /* Check if we are stalled for some time */ 93 if (H_IS_LONG_BUSY(rc)) { 94 msleep(get_longbusy_msecs(rc)); 95 rc = H_BUSY; 96 } else if (rc == H_BUSY) { 97 cond_resched(); 98 } 99 100 } while (rc == H_BUSY); 101 102 if (rc) 103 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc); 104 else 105 dev_dbg(&p->pdev->dev, "unbind drc 0x%x complete\n", 106 p->drc_index); 107 108 return; 109 } 110 111 static int drc_pmem_query_n_bind(struct papr_scm_priv *p) 112 { 113 unsigned long start_addr; 114 unsigned long end_addr; 115 unsigned long ret[PLPAR_HCALL_BUFSIZE]; 116 int64_t rc; 117 118 119 rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret, 120 p->drc_index, 0); 121 if (rc) 122 goto err_out; 123 start_addr = ret[0]; 124 125 /* Make sure the full region is bound. */ 126 rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret, 127 p->drc_index, p->blocks - 1); 128 if (rc) 129 goto err_out; 130 end_addr = ret[0]; 131 132 if ((end_addr - start_addr) != ((p->blocks - 1) * p->block_size)) 133 goto err_out; 134 135 p->bound_addr = start_addr; 136 dev_dbg(&p->pdev->dev, "bound drc 0x%x to %pR\n", p->drc_index, &p->res); 137 return rc; 138 139 err_out: 140 dev_info(&p->pdev->dev, 141 "Failed to query, trying an unbind followed by bind"); 142 drc_pmem_unbind(p); 143 return drc_pmem_bind(p); 144 } 145 146 147 static int papr_scm_meta_get(struct papr_scm_priv *p, 148 struct nd_cmd_get_config_data_hdr *hdr) 149 { 150 unsigned long data[PLPAR_HCALL_BUFSIZE]; 151 unsigned long offset, data_offset; 152 int len, read; 153 int64_t ret; 154 155 if ((hdr->in_offset + hdr->in_length) > p->metadata_size) 156 return -EINVAL; 157 158 for (len = hdr->in_length; len; len -= read) { 159 160 data_offset = hdr->in_length - len; 161 offset = hdr->in_offset + data_offset; 162 163 if (len >= 8) 164 read = 8; 165 else if (len >= 4) 166 read = 4; 167 else if (len >= 2) 168 read = 2; 169 else 170 read = 1; 171 172 ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index, 173 offset, read); 174 175 if (ret == H_PARAMETER) /* bad DRC index */ 176 return -ENODEV; 177 if (ret) 178 return -EINVAL; /* other invalid parameter */ 179 180 switch (read) { 181 case 8: 182 *(uint64_t *)(hdr->out_buf + data_offset) = be64_to_cpu(data[0]); 183 break; 184 case 4: 185 *(uint32_t *)(hdr->out_buf + data_offset) = be32_to_cpu(data[0] & 0xffffffff); 186 break; 187 188 case 2: 189 *(uint16_t *)(hdr->out_buf + data_offset) = be16_to_cpu(data[0] & 0xffff); 190 break; 191 192 case 1: 193 *(uint8_t *)(hdr->out_buf + data_offset) = (data[0] & 0xff); 194 break; 195 } 196 } 197 return 0; 198 } 199 200 static int papr_scm_meta_set(struct papr_scm_priv *p, 201 struct nd_cmd_set_config_hdr *hdr) 202 { 203 unsigned long offset, data_offset; 204 int len, wrote; 205 unsigned long data; 206 __be64 data_be; 207 int64_t ret; 208 209 if ((hdr->in_offset + hdr->in_length) > p->metadata_size) 210 return -EINVAL; 211 212 for (len = hdr->in_length; len; len -= wrote) { 213 214 data_offset = hdr->in_length - len; 215 offset = hdr->in_offset + data_offset; 216 217 if (len >= 8) { 218 data = *(uint64_t *)(hdr->in_buf + data_offset); 219 data_be = cpu_to_be64(data); 220 wrote = 8; 221 } else if (len >= 4) { 222 data = *(uint32_t *)(hdr->in_buf + data_offset); 223 data &= 0xffffffff; 224 data_be = cpu_to_be32(data); 225 wrote = 4; 226 } else if (len >= 2) { 227 data = *(uint16_t *)(hdr->in_buf + data_offset); 228 data &= 0xffff; 229 data_be = cpu_to_be16(data); 230 wrote = 2; 231 } else { 232 data_be = *(uint8_t *)(hdr->in_buf + data_offset); 233 data_be &= 0xff; 234 wrote = 1; 235 } 236 237 ret = plpar_hcall_norets(H_SCM_WRITE_METADATA, p->drc_index, 238 offset, data_be, wrote); 239 if (ret == H_PARAMETER) /* bad DRC index */ 240 return -ENODEV; 241 if (ret) 242 return -EINVAL; /* other invalid parameter */ 243 } 244 245 return 0; 246 } 247 248 int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm, 249 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc) 250 { 251 struct nd_cmd_get_config_size *get_size_hdr; 252 struct papr_scm_priv *p; 253 254 /* Only dimm-specific calls are supported atm */ 255 if (!nvdimm) 256 return -EINVAL; 257 258 p = nvdimm_provider_data(nvdimm); 259 260 switch (cmd) { 261 case ND_CMD_GET_CONFIG_SIZE: 262 get_size_hdr = buf; 263 264 get_size_hdr->status = 0; 265 get_size_hdr->max_xfer = 8; 266 get_size_hdr->config_size = p->metadata_size; 267 *cmd_rc = 0; 268 break; 269 270 case ND_CMD_GET_CONFIG_DATA: 271 *cmd_rc = papr_scm_meta_get(p, buf); 272 break; 273 274 case ND_CMD_SET_CONFIG_DATA: 275 *cmd_rc = papr_scm_meta_set(p, buf); 276 break; 277 278 default: 279 return -EINVAL; 280 } 281 282 dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc); 283 284 return 0; 285 } 286 287 static inline int papr_scm_node(int node) 288 { 289 int min_dist = INT_MAX, dist; 290 int nid, min_node; 291 292 if ((node == NUMA_NO_NODE) || node_online(node)) 293 return node; 294 295 min_node = first_online_node; 296 for_each_online_node(nid) { 297 dist = node_distance(node, nid); 298 if (dist < min_dist) { 299 min_dist = dist; 300 min_node = nid; 301 } 302 } 303 return min_node; 304 } 305 306 static int papr_scm_nvdimm_init(struct papr_scm_priv *p) 307 { 308 struct device *dev = &p->pdev->dev; 309 struct nd_mapping_desc mapping; 310 struct nd_region_desc ndr_desc; 311 unsigned long dimm_flags; 312 int target_nid, online_nid; 313 314 p->bus_desc.ndctl = papr_scm_ndctl; 315 p->bus_desc.module = THIS_MODULE; 316 p->bus_desc.of_node = p->pdev->dev.of_node; 317 p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL); 318 319 if (!p->bus_desc.provider_name) 320 return -ENOMEM; 321 322 p->bus = nvdimm_bus_register(NULL, &p->bus_desc); 323 if (!p->bus) { 324 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn); 325 return -ENXIO; 326 } 327 328 dimm_flags = 0; 329 set_bit(NDD_ALIASING, &dimm_flags); 330 331 p->nvdimm = nvdimm_create(p->bus, p, NULL, dimm_flags, 332 PAPR_SCM_DIMM_CMD_MASK, 0, NULL); 333 if (!p->nvdimm) { 334 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn); 335 goto err; 336 } 337 338 if (nvdimm_bus_check_dimm_count(p->bus, 1)) 339 goto err; 340 341 /* now add the region */ 342 343 memset(&mapping, 0, sizeof(mapping)); 344 mapping.nvdimm = p->nvdimm; 345 mapping.start = 0; 346 mapping.size = p->blocks * p->block_size; // XXX: potential overflow? 347 348 memset(&ndr_desc, 0, sizeof(ndr_desc)); 349 target_nid = dev_to_node(&p->pdev->dev); 350 online_nid = papr_scm_node(target_nid); 351 ndr_desc.numa_node = online_nid; 352 ndr_desc.target_node = target_nid; 353 ndr_desc.res = &p->res; 354 ndr_desc.of_node = p->dn; 355 ndr_desc.provider_data = p; 356 ndr_desc.mapping = &mapping; 357 ndr_desc.num_mappings = 1; 358 ndr_desc.nd_set = &p->nd_set; 359 set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags); 360 361 if (p->is_volatile) 362 p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc); 363 else 364 p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc); 365 if (!p->region) { 366 dev_err(dev, "Error registering region %pR from %pOF\n", 367 ndr_desc.res, p->dn); 368 goto err; 369 } 370 if (target_nid != online_nid) 371 dev_info(dev, "Region registered with target node %d and online node %d", 372 target_nid, online_nid); 373 374 return 0; 375 376 err: nvdimm_bus_unregister(p->bus); 377 kfree(p->bus_desc.provider_name); 378 return -ENXIO; 379 } 380 381 static int papr_scm_probe(struct platform_device *pdev) 382 { 383 struct device_node *dn = pdev->dev.of_node; 384 u32 drc_index, metadata_size; 385 u64 blocks, block_size; 386 struct papr_scm_priv *p; 387 const char *uuid_str; 388 u64 uuid[2]; 389 int rc; 390 391 /* check we have all the required DT properties */ 392 if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) { 393 dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn); 394 return -ENODEV; 395 } 396 397 if (of_property_read_u64(dn, "ibm,block-size", &block_size)) { 398 dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn); 399 return -ENODEV; 400 } 401 402 if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) { 403 dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn); 404 return -ENODEV; 405 } 406 407 if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) { 408 dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn); 409 return -ENODEV; 410 } 411 412 413 p = kzalloc(sizeof(*p), GFP_KERNEL); 414 if (!p) 415 return -ENOMEM; 416 417 /* optional DT properties */ 418 of_property_read_u32(dn, "ibm,metadata-size", &metadata_size); 419 420 p->dn = dn; 421 p->drc_index = drc_index; 422 p->block_size = block_size; 423 p->blocks = blocks; 424 p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required"); 425 426 /* We just need to ensure that set cookies are unique across */ 427 uuid_parse(uuid_str, (uuid_t *) uuid); 428 /* 429 * cookie1 and cookie2 are not really little endian 430 * we store a little endian representation of the 431 * uuid str so that we can compare this with the label 432 * area cookie irrespective of the endian config with which 433 * the kernel is built. 434 */ 435 p->nd_set.cookie1 = cpu_to_le64(uuid[0]); 436 p->nd_set.cookie2 = cpu_to_le64(uuid[1]); 437 438 /* might be zero */ 439 p->metadata_size = metadata_size; 440 p->pdev = pdev; 441 442 /* request the hypervisor to bind this region to somewhere in memory */ 443 rc = drc_pmem_bind(p); 444 445 /* If phyp says drc memory still bound then force unbound and retry */ 446 if (rc == H_OVERLAP) 447 rc = drc_pmem_query_n_bind(p); 448 449 if (rc != H_SUCCESS) { 450 dev_err(&p->pdev->dev, "bind err: %d\n", rc); 451 rc = -ENXIO; 452 goto err; 453 } 454 455 /* setup the resource for the newly bound range */ 456 p->res.start = p->bound_addr; 457 p->res.end = p->bound_addr + p->blocks * p->block_size - 1; 458 p->res.name = pdev->name; 459 p->res.flags = IORESOURCE_MEM; 460 461 rc = papr_scm_nvdimm_init(p); 462 if (rc) 463 goto err2; 464 465 platform_set_drvdata(pdev, p); 466 467 return 0; 468 469 err2: drc_pmem_unbind(p); 470 err: kfree(p); 471 return rc; 472 } 473 474 static int papr_scm_remove(struct platform_device *pdev) 475 { 476 struct papr_scm_priv *p = platform_get_drvdata(pdev); 477 478 nvdimm_bus_unregister(p->bus); 479 drc_pmem_unbind(p); 480 kfree(p); 481 482 return 0; 483 } 484 485 static const struct of_device_id papr_scm_match[] = { 486 { .compatible = "ibm,pmemory" }, 487 { }, 488 }; 489 490 static struct platform_driver papr_scm_driver = { 491 .probe = papr_scm_probe, 492 .remove = papr_scm_remove, 493 .driver = { 494 .name = "papr_scm", 495 .of_match_table = papr_scm_match, 496 }, 497 }; 498 499 module_platform_driver(papr_scm_driver); 500 MODULE_DEVICE_TABLE(of, papr_scm_match); 501 MODULE_LICENSE("GPL"); 502 MODULE_AUTHOR("IBM Corporation"); 503