ipc4-telemetry.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2018-2023 Intel Corporation
  7. //
  8. #include <linux/debugfs.h>
  9. #include <linux/io.h>
  10. #include <linux/pm_runtime.h>
  11. #include <sound/sof/debug.h>
  12. #include <sound/sof/ipc4/header.h>
  13. #include "sof-priv.h"
  14. #include "ops.h"
  15. #include "ipc4-telemetry.h"
  16. #include "ipc4-priv.h"
  17. static void __iomem *sof_ipc4_query_exception_address(struct snd_sof_dev *sdev)
  18. {
  19. u32 type = SOF_IPC4_DEBUG_SLOT_TELEMETRY;
  20. size_t telemetry_slot_offset;
  21. u32 offset;
  22. telemetry_slot_offset = sof_ipc4_find_debug_slot_offset_by_type(sdev, type);
  23. if (!telemetry_slot_offset)
  24. return NULL;
  25. /* skip the first separator magic number */
  26. offset = telemetry_slot_offset + sizeof(u32);
  27. return sdev->bar[sdev->mailbox_bar] + offset;
  28. }
  29. static ssize_t sof_telemetry_entry_read(struct file *file, char __user *buffer,
  30. size_t count, loff_t *ppos)
  31. {
  32. struct snd_sof_dfsentry *dfse = file->private_data;
  33. struct snd_sof_dev *sdev = dfse->sdev;
  34. void __iomem *io_addr;
  35. loff_t pos = *ppos;
  36. size_t size_ret;
  37. u8 *buf;
  38. if (pos < 0)
  39. return -EINVAL;
  40. /* skip the first separator magic number */
  41. if (pos >= SOF_IPC4_DEBUG_SLOT_SIZE - 4 || !count)
  42. return 0;
  43. if (count > SOF_IPC4_DEBUG_SLOT_SIZE - 4 - pos)
  44. count = SOF_IPC4_DEBUG_SLOT_SIZE - 4 - pos;
  45. io_addr = sof_ipc4_query_exception_address(sdev);
  46. if (!io_addr)
  47. return -EFAULT;
  48. buf = kzalloc(SOF_IPC4_DEBUG_SLOT_SIZE - 4, GFP_KERNEL);
  49. if (!buf)
  50. return -ENOMEM;
  51. memcpy_fromio(buf, io_addr, SOF_IPC4_DEBUG_SLOT_SIZE - 4);
  52. size_ret = copy_to_user(buffer, buf + pos, count);
  53. if (size_ret) {
  54. kfree(buf);
  55. return -EFAULT;
  56. }
  57. *ppos = pos + count;
  58. kfree(buf);
  59. return count;
  60. }
  61. static const struct file_operations sof_telemetry_fops = {
  62. .open = simple_open,
  63. .read = sof_telemetry_entry_read,
  64. };
  65. void sof_ipc4_create_exception_debugfs_node(struct snd_sof_dev *sdev)
  66. {
  67. struct snd_sof_dfsentry *dfse;
  68. dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
  69. if (!dfse)
  70. return;
  71. dfse->type = SOF_DFSENTRY_TYPE_IOMEM;
  72. dfse->size = SOF_IPC4_DEBUG_SLOT_SIZE - 4;
  73. dfse->access_type = SOF_DEBUGFS_ACCESS_ALWAYS;
  74. dfse->sdev = sdev;
  75. list_add(&dfse->list, &sdev->dfsentry_list);
  76. debugfs_create_file("exception", 0444, sdev->debugfs_root, dfse, &sof_telemetry_fops);
  77. }