1 // SPDX-License-Identifier: (GPL-2.0 OR MIT) 2 /* 3 * Siemens System Memory Buffer driver. 4 * Copyright(c) 2022, HiSilicon Limited. 5 */ 6 7 #include <linux/atomic.h> 8 #include <linux/acpi.h> 9 #include <linux/circ_buf.h> 10 #include <linux/err.h> 11 #include <linux/fs.h> 12 #include <linux/module.h> 13 #include <linux/mod_devicetable.h> 14 #include <linux/platform_device.h> 15 16 #include "coresight-etm-perf.h" 17 #include "coresight-priv.h" 18 #include "ultrasoc-smb.h" 19 20 DEFINE_CORESIGHT_DEVLIST(sink_devs, "ultra_smb"); 21 22 #define ULTRASOC_SMB_DSM_UUID "82ae1283-7f6a-4cbe-aa06-53e8fb24db18" 23 24 static bool smb_buffer_not_empty(struct smb_drv_data *drvdata) 25 { 26 u32 buf_status = readl(drvdata->base + SMB_LB_INT_STS_REG); 27 28 return FIELD_GET(SMB_LB_INT_STS_NOT_EMPTY_MSK, buf_status); 29 } 30 31 static void smb_update_data_size(struct smb_drv_data *drvdata) 32 { 33 struct smb_data_buffer *sdb = &drvdata->sdb; 34 u32 buf_wrptr; 35 36 buf_wrptr = readl(drvdata->base + SMB_LB_WR_ADDR_REG) - 37 sdb->buf_hw_base; 38 39 /* Buffer is full */ 40 if (buf_wrptr == sdb->buf_rdptr && smb_buffer_not_empty(drvdata)) { 41 sdb->data_size = sdb->buf_size; 42 return; 43 } 44 45 /* The buffer mode is circular buffer mode */ 46 sdb->data_size = CIRC_CNT(buf_wrptr, sdb->buf_rdptr, 47 sdb->buf_size); 48 } 49 50 /* 51 * The read pointer adds @nbytes bytes (may round up to the beginning) 52 * after the data is read or discarded, while needing to update the 53 * available data size. 54 */ 55 static void smb_update_read_ptr(struct smb_drv_data *drvdata, u32 nbytes) 56 { 57 struct smb_data_buffer *sdb = &drvdata->sdb; 58 59 sdb->buf_rdptr += nbytes; 60 sdb->buf_rdptr %= sdb->buf_size; 61 writel(sdb->buf_hw_base + sdb->buf_rdptr, 62 drvdata->base + SMB_LB_RD_ADDR_REG); 63 64 sdb->data_size -= nbytes; 65 } 66 67 static void smb_reset_buffer(struct smb_drv_data *drvdata) 68 { 69 struct smb_data_buffer *sdb = &drvdata->sdb; 70 u32 write_ptr; 71 72 /* 73 * We must flush and discard any data left in hardware path 74 * to avoid corrupting the next session. 75 * Note: The write pointer will never exceed the read pointer. 76 */ 77 writel(SMB_LB_PURGE_PURGED, drvdata->base + SMB_LB_PURGE_REG); 78 79 /* Reset SMB logical buffer status flags */ 80 writel(SMB_LB_INT_STS_RESET, drvdata->base + SMB_LB_INT_STS_REG); 81 82 write_ptr = readl(drvdata->base + SMB_LB_WR_ADDR_REG); 83 84 /* Do nothing, not data left in hardware path */ 85 if (!write_ptr || write_ptr == sdb->buf_rdptr + sdb->buf_hw_base) 86 return; 87 88 /* 89 * The SMB_LB_WR_ADDR_REG register is read-only, 90 * Synchronize the read pointer to write pointer. 91 */ 92 writel(write_ptr, drvdata->base + SMB_LB_RD_ADDR_REG); 93 sdb->buf_rdptr = write_ptr - sdb->buf_hw_base; 94 } 95 96 static int smb_open(struct inode *inode, struct file *file) 97 { 98 struct smb_drv_data *drvdata = container_of(file->private_data, 99 struct smb_drv_data, miscdev); 100 int ret = 0; 101 102 spin_lock(&drvdata->spinlock); 103 104 if (drvdata->reading) { 105 ret = -EBUSY; 106 goto out; 107 } 108 109 if (atomic_read(&drvdata->csdev->refcnt)) { 110 ret = -EBUSY; 111 goto out; 112 } 113 114 smb_update_data_size(drvdata); 115 116 drvdata->reading = true; 117 out: 118 spin_unlock(&drvdata->spinlock); 119 120 return ret; 121 } 122 123 static ssize_t smb_read(struct file *file, char __user *data, size_t len, 124 loff_t *ppos) 125 { 126 struct smb_drv_data *drvdata = container_of(file->private_data, 127 struct smb_drv_data, miscdev); 128 struct smb_data_buffer *sdb = &drvdata->sdb; 129 struct device *dev = &drvdata->csdev->dev; 130 ssize_t to_copy = 0; 131 132 if (!len) 133 return 0; 134 135 if (!sdb->data_size) 136 return 0; 137 138 to_copy = min(sdb->data_size, len); 139 140 /* Copy parts of trace data when read pointer wrap around SMB buffer */ 141 if (sdb->buf_rdptr + to_copy > sdb->buf_size) 142 to_copy = sdb->buf_size - sdb->buf_rdptr; 143 144 if (copy_to_user(data, sdb->buf_base + sdb->buf_rdptr, to_copy)) { 145 dev_dbg(dev, "Failed to copy data to user\n"); 146 return -EFAULT; 147 } 148 149 *ppos += to_copy; 150 smb_update_read_ptr(drvdata, to_copy); 151 if (!sdb->data_size) 152 smb_reset_buffer(drvdata); 153 154 dev_dbg(dev, "%zu bytes copied\n", to_copy); 155 return to_copy; 156 } 157 158 static int smb_release(struct inode *inode, struct file *file) 159 { 160 struct smb_drv_data *drvdata = container_of(file->private_data, 161 struct smb_drv_data, miscdev); 162 163 spin_lock(&drvdata->spinlock); 164 drvdata->reading = false; 165 spin_unlock(&drvdata->spinlock); 166 167 return 0; 168 } 169 170 static const struct file_operations smb_fops = { 171 .owner = THIS_MODULE, 172 .open = smb_open, 173 .read = smb_read, 174 .release = smb_release, 175 .llseek = no_llseek, 176 }; 177 178 static ssize_t buf_size_show(struct device *dev, struct device_attribute *attr, 179 char *buf) 180 { 181 struct smb_drv_data *drvdata = dev_get_drvdata(dev->parent); 182 183 return sysfs_emit(buf, "0x%lx\n", drvdata->sdb.buf_size); 184 } 185 static DEVICE_ATTR_RO(buf_size); 186 187 static struct attribute *smb_sink_attrs[] = { 188 coresight_simple_reg32(read_pos, SMB_LB_RD_ADDR_REG), 189 coresight_simple_reg32(write_pos, SMB_LB_WR_ADDR_REG), 190 coresight_simple_reg32(buf_status, SMB_LB_INT_STS_REG), 191 &dev_attr_buf_size.attr, 192 NULL 193 }; 194 195 static const struct attribute_group smb_sink_group = { 196 .attrs = smb_sink_attrs, 197 .name = "mgmt", 198 }; 199 200 static const struct attribute_group *smb_sink_groups[] = { 201 &smb_sink_group, 202 NULL 203 }; 204 205 static void smb_enable_hw(struct smb_drv_data *drvdata) 206 { 207 writel(SMB_GLB_EN_HW_ENABLE, drvdata->base + SMB_GLB_EN_REG); 208 } 209 210 static void smb_disable_hw(struct smb_drv_data *drvdata) 211 { 212 writel(0x0, drvdata->base + SMB_GLB_EN_REG); 213 } 214 215 static void smb_enable_sysfs(struct coresight_device *csdev) 216 { 217 struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent); 218 219 if (drvdata->mode != CS_MODE_DISABLED) 220 return; 221 222 smb_enable_hw(drvdata); 223 drvdata->mode = CS_MODE_SYSFS; 224 } 225 226 static int smb_enable_perf(struct coresight_device *csdev, void *data) 227 { 228 struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent); 229 struct perf_output_handle *handle = data; 230 struct cs_buffers *buf = etm_perf_sink_config(handle); 231 pid_t pid; 232 233 if (!buf) 234 return -EINVAL; 235 236 /* Get a handle on the pid of the target process */ 237 pid = buf->pid; 238 239 /* Device is already in used by other session */ 240 if (drvdata->pid != -1 && drvdata->pid != pid) 241 return -EBUSY; 242 243 if (drvdata->pid == -1) { 244 smb_enable_hw(drvdata); 245 drvdata->pid = pid; 246 drvdata->mode = CS_MODE_PERF; 247 } 248 249 return 0; 250 } 251 252 static int smb_enable(struct coresight_device *csdev, enum cs_mode mode, 253 void *data) 254 { 255 struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent); 256 int ret = 0; 257 258 spin_lock(&drvdata->spinlock); 259 260 /* Do nothing, the trace data is reading by other interface now */ 261 if (drvdata->reading) { 262 ret = -EBUSY; 263 goto out; 264 } 265 266 /* Do nothing, the SMB is already enabled as other mode */ 267 if (drvdata->mode != CS_MODE_DISABLED && drvdata->mode != mode) { 268 ret = -EBUSY; 269 goto out; 270 } 271 272 switch (mode) { 273 case CS_MODE_SYSFS: 274 smb_enable_sysfs(csdev); 275 break; 276 case CS_MODE_PERF: 277 ret = smb_enable_perf(csdev, data); 278 break; 279 default: 280 ret = -EINVAL; 281 } 282 283 if (ret) 284 goto out; 285 286 atomic_inc(&csdev->refcnt); 287 288 dev_dbg(&csdev->dev, "Ultrasoc SMB enabled\n"); 289 out: 290 spin_unlock(&drvdata->spinlock); 291 292 return ret; 293 } 294 295 static int smb_disable(struct coresight_device *csdev) 296 { 297 struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent); 298 int ret = 0; 299 300 spin_lock(&drvdata->spinlock); 301 302 if (drvdata->reading) { 303 ret = -EBUSY; 304 goto out; 305 } 306 307 if (atomic_dec_return(&csdev->refcnt)) { 308 ret = -EBUSY; 309 goto out; 310 } 311 312 /* Complain if we (somehow) got out of sync */ 313 WARN_ON_ONCE(drvdata->mode == CS_MODE_DISABLED); 314 315 smb_disable_hw(drvdata); 316 317 /* Dissociate from the target process. */ 318 drvdata->pid = -1; 319 drvdata->mode = CS_MODE_DISABLED; 320 321 dev_dbg(&csdev->dev, "Ultrasoc SMB disabled\n"); 322 out: 323 spin_unlock(&drvdata->spinlock); 324 325 return ret; 326 } 327 328 static void *smb_alloc_buffer(struct coresight_device *csdev, 329 struct perf_event *event, void **pages, 330 int nr_pages, bool overwrite) 331 { 332 struct cs_buffers *buf; 333 int node; 334 335 node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu); 336 buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node); 337 if (!buf) 338 return NULL; 339 340 buf->snapshot = overwrite; 341 buf->nr_pages = nr_pages; 342 buf->data_pages = pages; 343 buf->pid = task_pid_nr(event->owner); 344 345 return buf; 346 } 347 348 static void smb_free_buffer(void *config) 349 { 350 struct cs_buffers *buf = config; 351 352 kfree(buf); 353 } 354 355 static void smb_sync_perf_buffer(struct smb_drv_data *drvdata, 356 struct cs_buffers *buf, 357 unsigned long head) 358 { 359 struct smb_data_buffer *sdb = &drvdata->sdb; 360 char **dst_pages = (char **)buf->data_pages; 361 unsigned long to_copy; 362 long pg_idx, pg_offset; 363 364 pg_idx = head >> PAGE_SHIFT; 365 pg_offset = head & (PAGE_SIZE - 1); 366 367 while (sdb->data_size) { 368 unsigned long pg_space = PAGE_SIZE - pg_offset; 369 370 to_copy = min(sdb->data_size, pg_space); 371 372 /* Copy parts of trace data when read pointer wrap around */ 373 if (sdb->buf_rdptr + to_copy > sdb->buf_size) 374 to_copy = sdb->buf_size - sdb->buf_rdptr; 375 376 memcpy(dst_pages[pg_idx] + pg_offset, 377 sdb->buf_base + sdb->buf_rdptr, to_copy); 378 379 pg_offset += to_copy; 380 if (pg_offset >= PAGE_SIZE) { 381 pg_offset = 0; 382 pg_idx++; 383 pg_idx %= buf->nr_pages; 384 } 385 smb_update_read_ptr(drvdata, to_copy); 386 } 387 388 smb_reset_buffer(drvdata); 389 } 390 391 static unsigned long smb_update_buffer(struct coresight_device *csdev, 392 struct perf_output_handle *handle, 393 void *sink_config) 394 { 395 struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent); 396 struct smb_data_buffer *sdb = &drvdata->sdb; 397 struct cs_buffers *buf = sink_config; 398 unsigned long data_size = 0; 399 bool lost = false; 400 401 if (!buf) 402 return 0; 403 404 spin_lock(&drvdata->spinlock); 405 406 /* Don't do anything if another tracer is using this sink. */ 407 if (atomic_read(&csdev->refcnt) != 1) 408 goto out; 409 410 smb_disable_hw(drvdata); 411 smb_update_data_size(drvdata); 412 413 /* 414 * The SMB buffer may be bigger than the space available in the 415 * perf ring buffer (handle->size). If so advance the offset so 416 * that we get the latest trace data. 417 */ 418 if (sdb->data_size > handle->size) { 419 smb_update_read_ptr(drvdata, sdb->data_size - handle->size); 420 lost = true; 421 } 422 423 data_size = sdb->data_size; 424 smb_sync_perf_buffer(drvdata, buf, handle->head); 425 if (!buf->snapshot && lost) 426 perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED); 427 out: 428 spin_unlock(&drvdata->spinlock); 429 430 return data_size; 431 } 432 433 static const struct coresight_ops_sink smb_cs_ops = { 434 .enable = smb_enable, 435 .disable = smb_disable, 436 .alloc_buffer = smb_alloc_buffer, 437 .free_buffer = smb_free_buffer, 438 .update_buffer = smb_update_buffer, 439 }; 440 441 static const struct coresight_ops cs_ops = { 442 .sink_ops = &smb_cs_ops, 443 }; 444 445 static int smb_init_data_buffer(struct platform_device *pdev, 446 struct smb_data_buffer *sdb) 447 { 448 struct resource *res; 449 void *base; 450 451 res = platform_get_resource(pdev, IORESOURCE_MEM, SMB_BUF_ADDR_RES); 452 if (!res) { 453 dev_err(&pdev->dev, "SMB device failed to get resource\n"); 454 return -EINVAL; 455 } 456 457 sdb->buf_rdptr = 0; 458 sdb->buf_hw_base = FIELD_GET(SMB_BUF_ADDR_LO_MSK, res->start); 459 sdb->buf_size = resource_size(res); 460 if (sdb->buf_size == 0) 461 return -EINVAL; 462 463 /* 464 * This is a chunk of memory, use classic mapping with better 465 * performance. 466 */ 467 base = devm_memremap(&pdev->dev, sdb->buf_hw_base, sdb->buf_size, 468 MEMREMAP_WB); 469 if (IS_ERR(base)) 470 return PTR_ERR(base); 471 472 sdb->buf_base = base; 473 474 return 0; 475 } 476 477 static void smb_init_hw(struct smb_drv_data *drvdata) 478 { 479 smb_disable_hw(drvdata); 480 481 writel(SMB_LB_CFG_LO_DEFAULT, drvdata->base + SMB_LB_CFG_LO_REG); 482 writel(SMB_LB_CFG_HI_DEFAULT, drvdata->base + SMB_LB_CFG_HI_REG); 483 writel(SMB_GLB_CFG_DEFAULT, drvdata->base + SMB_GLB_CFG_REG); 484 writel(SMB_GLB_INT_CFG, drvdata->base + SMB_GLB_INT_REG); 485 writel(SMB_LB_INT_CTRL_CFG, drvdata->base + SMB_LB_INT_CTRL_REG); 486 } 487 488 static int smb_register_sink(struct platform_device *pdev, 489 struct smb_drv_data *drvdata) 490 { 491 struct coresight_platform_data *pdata = NULL; 492 struct coresight_desc desc = { 0 }; 493 int ret; 494 495 pdata = coresight_get_platform_data(&pdev->dev); 496 if (IS_ERR(pdata)) 497 return PTR_ERR(pdata); 498 499 desc.type = CORESIGHT_DEV_TYPE_SINK; 500 desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER; 501 desc.ops = &cs_ops; 502 desc.pdata = pdata; 503 desc.dev = &pdev->dev; 504 desc.groups = smb_sink_groups; 505 desc.name = coresight_alloc_device_name(&sink_devs, &pdev->dev); 506 if (!desc.name) { 507 dev_err(&pdev->dev, "Failed to alloc coresight device name"); 508 return -ENOMEM; 509 } 510 desc.access = CSDEV_ACCESS_IOMEM(drvdata->base); 511 512 drvdata->csdev = coresight_register(&desc); 513 if (IS_ERR(drvdata->csdev)) 514 return PTR_ERR(drvdata->csdev); 515 516 drvdata->miscdev.name = desc.name; 517 drvdata->miscdev.minor = MISC_DYNAMIC_MINOR; 518 drvdata->miscdev.fops = &smb_fops; 519 ret = misc_register(&drvdata->miscdev); 520 if (ret) { 521 coresight_unregister(drvdata->csdev); 522 dev_err(&pdev->dev, "Failed to register misc, ret=%d\n", ret); 523 } 524 525 return ret; 526 } 527 528 static void smb_unregister_sink(struct smb_drv_data *drvdata) 529 { 530 misc_deregister(&drvdata->miscdev); 531 coresight_unregister(drvdata->csdev); 532 } 533 534 static int smb_config_inport(struct device *dev, bool enable) 535 { 536 u64 func = enable ? 1 : 0; 537 union acpi_object *obj; 538 guid_t guid; 539 u64 rev = 0; 540 541 /* 542 * Using DSM calls to enable/disable ultrasoc hardwares on 543 * tracing path, to prevent ultrasoc packet format being exposed. 544 */ 545 if (guid_parse(ULTRASOC_SMB_DSM_UUID, &guid)) { 546 dev_err(dev, "Get GUID failed\n"); 547 return -EINVAL; 548 } 549 550 obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &guid, rev, func, NULL); 551 if (!obj) { 552 dev_err(dev, "ACPI handle failed\n"); 553 return -ENODEV; 554 } 555 556 ACPI_FREE(obj); 557 558 return 0; 559 } 560 561 static int smb_probe(struct platform_device *pdev) 562 { 563 struct device *dev = &pdev->dev; 564 struct smb_drv_data *drvdata; 565 int ret; 566 567 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); 568 if (!drvdata) 569 return -ENOMEM; 570 571 drvdata->base = devm_platform_ioremap_resource(pdev, SMB_REG_ADDR_RES); 572 if (IS_ERR(drvdata->base)) { 573 dev_err(dev, "Failed to ioremap resource\n"); 574 return PTR_ERR(drvdata->base); 575 } 576 577 smb_init_hw(drvdata); 578 579 ret = smb_init_data_buffer(pdev, &drvdata->sdb); 580 if (ret) { 581 dev_err(dev, "Failed to init buffer, ret = %d\n", ret); 582 return ret; 583 } 584 585 ret = smb_config_inport(dev, true); 586 if (ret) 587 return ret; 588 589 smb_reset_buffer(drvdata); 590 platform_set_drvdata(pdev, drvdata); 591 spin_lock_init(&drvdata->spinlock); 592 drvdata->pid = -1; 593 594 ret = smb_register_sink(pdev, drvdata); 595 if (ret) { 596 smb_config_inport(&pdev->dev, false); 597 dev_err(dev, "Failed to register SMB sink\n"); 598 return ret; 599 } 600 601 return 0; 602 } 603 604 static int smb_remove(struct platform_device *pdev) 605 { 606 struct smb_drv_data *drvdata = platform_get_drvdata(pdev); 607 608 smb_unregister_sink(drvdata); 609 610 smb_config_inport(&pdev->dev, false); 611 612 return 0; 613 } 614 615 #ifdef CONFIG_ACPI 616 static const struct acpi_device_id ultrasoc_smb_acpi_match[] = { 617 {"HISI03A1", 0}, 618 {} 619 }; 620 MODULE_DEVICE_TABLE(acpi, ultrasoc_smb_acpi_match); 621 #endif 622 623 static struct platform_driver smb_driver = { 624 .driver = { 625 .name = "ultrasoc-smb", 626 .acpi_match_table = ACPI_PTR(ultrasoc_smb_acpi_match), 627 .suppress_bind_attrs = true, 628 }, 629 .probe = smb_probe, 630 .remove = smb_remove, 631 }; 632 module_platform_driver(smb_driver); 633 634 MODULE_DESCRIPTION("UltraSoc SMB CoreSight driver"); 635 MODULE_LICENSE("Dual MIT/GPL"); 636 MODULE_AUTHOR("Jonathan Zhou <jonathan.zhouwen@huawei.com>"); 637 MODULE_AUTHOR("Qi Liu <liuqi115@huawei.com>"); 638