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