|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | #include <linux/bitfield.h>
|
| 8 | +#include <linux/debugfs.h> |
8 | 9 | #include <linux/module.h>
|
9 | 10 | #include <linux/init.h>
|
10 | 11 | #include <linux/pci.h>
|
@@ -252,3 +253,302 @@ bool pcie_ptm_enabled(struct pci_dev *dev)
|
252 | 253 | return dev->ptm_enabled;
|
253 | 254 | }
|
254 | 255 | EXPORT_SYMBOL(pcie_ptm_enabled);
|
| 256 | + |
| 257 | +static ssize_t context_update_write(struct file *file, const char __user *ubuf, |
| 258 | + size_t count, loff_t *ppos) |
| 259 | +{ |
| 260 | + struct pci_ptm_debugfs *ptm_debugfs = file->private_data; |
| 261 | + char buf[7]; |
| 262 | + int ret; |
| 263 | + u8 mode; |
| 264 | + |
| 265 | + if (!ptm_debugfs->ops->context_update_write) |
| 266 | + return -EOPNOTSUPP; |
| 267 | + |
| 268 | + if (count < 1 || count >= sizeof(buf)) |
| 269 | + return -EINVAL; |
| 270 | + |
| 271 | + ret = copy_from_user(buf, ubuf, count); |
| 272 | + if (ret) |
| 273 | + return -EFAULT; |
| 274 | + |
| 275 | + buf[count] = '\0'; |
| 276 | + |
| 277 | + if (sysfs_streq(buf, "auto")) |
| 278 | + mode = PCIE_PTM_CONTEXT_UPDATE_AUTO; |
| 279 | + else if (sysfs_streq(buf, "manual")) |
| 280 | + mode = PCIE_PTM_CONTEXT_UPDATE_MANUAL; |
| 281 | + else |
| 282 | + return -EINVAL; |
| 283 | + |
| 284 | + mutex_lock(&ptm_debugfs->lock); |
| 285 | + ret = ptm_debugfs->ops->context_update_write(ptm_debugfs->pdata, mode); |
| 286 | + mutex_unlock(&ptm_debugfs->lock); |
| 287 | + if (ret) |
| 288 | + return ret; |
| 289 | + |
| 290 | + return count; |
| 291 | +} |
| 292 | + |
| 293 | +static ssize_t context_update_read(struct file *file, char __user *ubuf, |
| 294 | + size_t count, loff_t *ppos) |
| 295 | +{ |
| 296 | + struct pci_ptm_debugfs *ptm_debugfs = file->private_data; |
| 297 | + char buf[8]; /* Extra space for NULL termination at the end */ |
| 298 | + ssize_t pos; |
| 299 | + u8 mode; |
| 300 | + |
| 301 | + if (!ptm_debugfs->ops->context_update_read) |
| 302 | + return -EOPNOTSUPP; |
| 303 | + |
| 304 | + mutex_lock(&ptm_debugfs->lock); |
| 305 | + ptm_debugfs->ops->context_update_read(ptm_debugfs->pdata, &mode); |
| 306 | + mutex_unlock(&ptm_debugfs->lock); |
| 307 | + |
| 308 | + if (mode == PCIE_PTM_CONTEXT_UPDATE_AUTO) |
| 309 | + pos = scnprintf(buf, sizeof(buf), "auto\n"); |
| 310 | + else |
| 311 | + pos = scnprintf(buf, sizeof(buf), "manual\n"); |
| 312 | + |
| 313 | + return simple_read_from_buffer(ubuf, count, ppos, buf, pos); |
| 314 | +} |
| 315 | + |
| 316 | +static const struct file_operations context_update_fops = { |
| 317 | + .open = simple_open, |
| 318 | + .read = context_update_read, |
| 319 | + .write = context_update_write, |
| 320 | +}; |
| 321 | + |
| 322 | +static int context_valid_get(void *data, u64 *val) |
| 323 | +{ |
| 324 | + struct pci_ptm_debugfs *ptm_debugfs = data; |
| 325 | + bool valid; |
| 326 | + int ret; |
| 327 | + |
| 328 | + if (!ptm_debugfs->ops->context_valid_read) |
| 329 | + return -EOPNOTSUPP; |
| 330 | + |
| 331 | + mutex_lock(&ptm_debugfs->lock); |
| 332 | + ret = ptm_debugfs->ops->context_valid_read(ptm_debugfs->pdata, &valid); |
| 333 | + mutex_unlock(&ptm_debugfs->lock); |
| 334 | + if (ret) |
| 335 | + return ret; |
| 336 | + |
| 337 | + *val = valid; |
| 338 | + |
| 339 | + return 0; |
| 340 | +} |
| 341 | + |
| 342 | +static int context_valid_set(void *data, u64 val) |
| 343 | +{ |
| 344 | + struct pci_ptm_debugfs *ptm_debugfs = data; |
| 345 | + int ret; |
| 346 | + |
| 347 | + if (!ptm_debugfs->ops->context_valid_write) |
| 348 | + return -EOPNOTSUPP; |
| 349 | + |
| 350 | + mutex_lock(&ptm_debugfs->lock); |
| 351 | + ret = ptm_debugfs->ops->context_valid_write(ptm_debugfs->pdata, !!val); |
| 352 | + mutex_unlock(&ptm_debugfs->lock); |
| 353 | + |
| 354 | + return ret; |
| 355 | +} |
| 356 | + |
| 357 | +DEFINE_DEBUGFS_ATTRIBUTE(context_valid_fops, context_valid_get, |
| 358 | + context_valid_set, "%llu\n"); |
| 359 | + |
| 360 | +static int local_clock_get(void *data, u64 *val) |
| 361 | +{ |
| 362 | + struct pci_ptm_debugfs *ptm_debugfs = data; |
| 363 | + u64 clock; |
| 364 | + int ret; |
| 365 | + |
| 366 | + if (!ptm_debugfs->ops->local_clock_read) |
| 367 | + return -EOPNOTSUPP; |
| 368 | + |
| 369 | + ret = ptm_debugfs->ops->local_clock_read(ptm_debugfs->pdata, &clock); |
| 370 | + if (ret) |
| 371 | + return ret; |
| 372 | + |
| 373 | + *val = clock; |
| 374 | + |
| 375 | + return 0; |
| 376 | +} |
| 377 | + |
| 378 | +DEFINE_DEBUGFS_ATTRIBUTE(local_clock_fops, local_clock_get, NULL, "%llu\n"); |
| 379 | + |
| 380 | +static int master_clock_get(void *data, u64 *val) |
| 381 | +{ |
| 382 | + struct pci_ptm_debugfs *ptm_debugfs = data; |
| 383 | + u64 clock; |
| 384 | + int ret; |
| 385 | + |
| 386 | + if (!ptm_debugfs->ops->master_clock_read) |
| 387 | + return -EOPNOTSUPP; |
| 388 | + |
| 389 | + ret = ptm_debugfs->ops->master_clock_read(ptm_debugfs->pdata, &clock); |
| 390 | + if (ret) |
| 391 | + return ret; |
| 392 | + |
| 393 | + *val = clock; |
| 394 | + |
| 395 | + return 0; |
| 396 | +} |
| 397 | + |
| 398 | +DEFINE_DEBUGFS_ATTRIBUTE(master_clock_fops, master_clock_get, NULL, "%llu\n"); |
| 399 | + |
| 400 | +static int t1_get(void *data, u64 *val) |
| 401 | +{ |
| 402 | + struct pci_ptm_debugfs *ptm_debugfs = data; |
| 403 | + u64 clock; |
| 404 | + int ret; |
| 405 | + |
| 406 | + if (!ptm_debugfs->ops->t1_read) |
| 407 | + return -EOPNOTSUPP; |
| 408 | + |
| 409 | + ret = ptm_debugfs->ops->t1_read(ptm_debugfs->pdata, &clock); |
| 410 | + if (ret) |
| 411 | + return ret; |
| 412 | + |
| 413 | + *val = clock; |
| 414 | + |
| 415 | + return 0; |
| 416 | +} |
| 417 | + |
| 418 | +DEFINE_DEBUGFS_ATTRIBUTE(t1_fops, t1_get, NULL, "%llu\n"); |
| 419 | + |
| 420 | +static int t2_get(void *data, u64 *val) |
| 421 | +{ |
| 422 | + struct pci_ptm_debugfs *ptm_debugfs = data; |
| 423 | + u64 clock; |
| 424 | + int ret; |
| 425 | + |
| 426 | + if (!ptm_debugfs->ops->t2_read) |
| 427 | + return -EOPNOTSUPP; |
| 428 | + |
| 429 | + ret = ptm_debugfs->ops->t2_read(ptm_debugfs->pdata, &clock); |
| 430 | + if (ret) |
| 431 | + return ret; |
| 432 | + |
| 433 | + *val = clock; |
| 434 | + |
| 435 | + return 0; |
| 436 | +} |
| 437 | + |
| 438 | +DEFINE_DEBUGFS_ATTRIBUTE(t2_fops, t2_get, NULL, "%llu\n"); |
| 439 | + |
| 440 | +static int t3_get(void *data, u64 *val) |
| 441 | +{ |
| 442 | + struct pci_ptm_debugfs *ptm_debugfs = data; |
| 443 | + u64 clock; |
| 444 | + int ret; |
| 445 | + |
| 446 | + if (!ptm_debugfs->ops->t3_read) |
| 447 | + return -EOPNOTSUPP; |
| 448 | + |
| 449 | + ret = ptm_debugfs->ops->t3_read(ptm_debugfs->pdata, &clock); |
| 450 | + if (ret) |
| 451 | + return ret; |
| 452 | + |
| 453 | + *val = clock; |
| 454 | + |
| 455 | + return 0; |
| 456 | +} |
| 457 | + |
| 458 | +DEFINE_DEBUGFS_ATTRIBUTE(t3_fops, t3_get, NULL, "%llu\n"); |
| 459 | + |
| 460 | +static int t4_get(void *data, u64 *val) |
| 461 | +{ |
| 462 | + struct pci_ptm_debugfs *ptm_debugfs = data; |
| 463 | + u64 clock; |
| 464 | + int ret; |
| 465 | + |
| 466 | + if (!ptm_debugfs->ops->t4_read) |
| 467 | + return -EOPNOTSUPP; |
| 468 | + |
| 469 | + ret = ptm_debugfs->ops->t4_read(ptm_debugfs->pdata, &clock); |
| 470 | + if (ret) |
| 471 | + return ret; |
| 472 | + |
| 473 | + *val = clock; |
| 474 | + |
| 475 | + return 0; |
| 476 | +} |
| 477 | + |
| 478 | +DEFINE_DEBUGFS_ATTRIBUTE(t4_fops, t4_get, NULL, "%llu\n"); |
| 479 | + |
| 480 | +#define pcie_ptm_create_debugfs_file(pdata, mode, attr) \ |
| 481 | + do { \ |
| 482 | + if (ops->attr##_visible && ops->attr##_visible(pdata)) \ |
| 483 | + debugfs_create_file(#attr, mode, ptm_debugfs->debugfs, \ |
| 484 | + ptm_debugfs, &attr##_fops); \ |
| 485 | + } while (0) |
| 486 | + |
| 487 | +/* |
| 488 | + * pcie_ptm_create_debugfs() - Create debugfs entries for the PTM context |
| 489 | + * @dev: PTM capable component device |
| 490 | + * @pdata: Private data of the PTM capable component device |
| 491 | + * @ops: PTM callback structure |
| 492 | + * |
| 493 | + * Create debugfs entries for exposing the PTM context of the PTM capable |
| 494 | + * components such as Root Complex and Endpoint controllers. |
| 495 | + * |
| 496 | + * Return: Pointer to 'struct pci_ptm_debugfs' if success, NULL otherwise. |
| 497 | + */ |
| 498 | +struct pci_ptm_debugfs *pcie_ptm_create_debugfs(struct device *dev, void *pdata, |
| 499 | + const struct pcie_ptm_ops *ops) |
| 500 | +{ |
| 501 | + struct pci_ptm_debugfs *ptm_debugfs; |
| 502 | + char *dirname; |
| 503 | + int ret; |
| 504 | + |
| 505 | + /* Caller must provide check_capability() callback */ |
| 506 | + if (!ops->check_capability) |
| 507 | + return NULL; |
| 508 | + |
| 509 | + /* Check for PTM capability before creating debugfs attrbutes */ |
| 510 | + ret = ops->check_capability(pdata); |
| 511 | + if (!ret) { |
| 512 | + dev_dbg(dev, "PTM capability not present\n"); |
| 513 | + return NULL; |
| 514 | + } |
| 515 | + |
| 516 | + ptm_debugfs = kzalloc(sizeof(*ptm_debugfs), GFP_KERNEL); |
| 517 | + if (!ptm_debugfs) |
| 518 | + return NULL; |
| 519 | + |
| 520 | + dirname = devm_kasprintf(dev, GFP_KERNEL, "pcie_ptm_%s", dev_name(dev)); |
| 521 | + if (!dirname) |
| 522 | + return NULL; |
| 523 | + |
| 524 | + ptm_debugfs->debugfs = debugfs_create_dir(dirname, NULL); |
| 525 | + ptm_debugfs->pdata = pdata; |
| 526 | + ptm_debugfs->ops = ops; |
| 527 | + mutex_init(&ptm_debugfs->lock); |
| 528 | + |
| 529 | + pcie_ptm_create_debugfs_file(pdata, 0644, context_update); |
| 530 | + pcie_ptm_create_debugfs_file(pdata, 0644, context_valid); |
| 531 | + pcie_ptm_create_debugfs_file(pdata, 0444, local_clock); |
| 532 | + pcie_ptm_create_debugfs_file(pdata, 0444, master_clock); |
| 533 | + pcie_ptm_create_debugfs_file(pdata, 0444, t1); |
| 534 | + pcie_ptm_create_debugfs_file(pdata, 0444, t2); |
| 535 | + pcie_ptm_create_debugfs_file(pdata, 0444, t3); |
| 536 | + pcie_ptm_create_debugfs_file(pdata, 0444, t4); |
| 537 | + |
| 538 | + return ptm_debugfs; |
| 539 | +} |
| 540 | +EXPORT_SYMBOL_GPL(pcie_ptm_create_debugfs); |
| 541 | + |
| 542 | +/* |
| 543 | + * pcie_ptm_destroy_debugfs() - Destroy debugfs entries for the PTM context |
| 544 | + * @ptm_debugfs: Pointer to the PTM debugfs struct |
| 545 | + */ |
| 546 | +void pcie_ptm_destroy_debugfs(struct pci_ptm_debugfs *ptm_debugfs) |
| 547 | +{ |
| 548 | + if (!ptm_debugfs) |
| 549 | + return; |
| 550 | + |
| 551 | + mutex_destroy(&ptm_debugfs->lock); |
| 552 | + debugfs_remove_recursive(ptm_debugfs->debugfs); |
| 553 | +} |
| 554 | +EXPORT_SYMBOL_GPL(pcie_ptm_destroy_debugfs); |
0 commit comments