Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 //
0003 // Copyright(c) 2022 Intel Corporation. All rights reserved.
0004 //
0005 // Authors: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
0006 //      Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
0007 //
0008 
0009 #include <linux/auxiliary_bus.h>
0010 #include <linux/completion.h>
0011 #include <linux/debugfs.h>
0012 #include <linux/ktime.h>
0013 #include <linux/mod_devicetable.h>
0014 #include <linux/module.h>
0015 #include <linux/pm_runtime.h>
0016 #include <linux/slab.h>
0017 #include <linux/uaccess.h>
0018 #include <sound/sof/header.h>
0019 
0020 #include "sof-client.h"
0021 
0022 #define MAX_IPC_FLOOD_DURATION_MS   1000
0023 #define MAX_IPC_FLOOD_COUNT     10000
0024 #define IPC_FLOOD_TEST_RESULT_LEN   512
0025 #define SOF_IPC_CLIENT_SUSPEND_DELAY_MS 3000
0026 
0027 #define DEBUGFS_IPC_FLOOD_COUNT     "ipc_flood_count"
0028 #define DEBUGFS_IPC_FLOOD_DURATION  "ipc_flood_duration_ms"
0029 
0030 struct sof_ipc_flood_priv {
0031     struct dentry *dfs_root;
0032     struct dentry *dfs_link[2];
0033     char *buf;
0034 };
0035 
0036 static int sof_ipc_flood_dfs_open(struct inode *inode, struct file *file)
0037 {
0038     struct sof_client_dev *cdev = inode->i_private;
0039     int ret;
0040 
0041     if (sof_client_get_fw_state(cdev) == SOF_FW_CRASHED)
0042         return -ENODEV;
0043 
0044     ret = debugfs_file_get(file->f_path.dentry);
0045     if (unlikely(ret))
0046         return ret;
0047 
0048     ret = simple_open(inode, file);
0049     if (ret)
0050         debugfs_file_put(file->f_path.dentry);
0051 
0052     return ret;
0053 }
0054 
0055 /*
0056  * helper function to perform the flood test. Only one of the two params, ipc_duration_ms
0057  * or ipc_count, will be non-zero and will determine the type of test
0058  */
0059 static int sof_debug_ipc_flood_test(struct sof_client_dev *cdev,
0060                     bool flood_duration_test,
0061                     unsigned long ipc_duration_ms,
0062                     unsigned long ipc_count)
0063 {
0064     struct sof_ipc_flood_priv *priv = cdev->data;
0065     struct device *dev = &cdev->auxdev.dev;
0066     struct sof_ipc_cmd_hdr hdr;
0067     struct sof_ipc_reply reply;
0068     u64 min_response_time = U64_MAX;
0069     ktime_t start, end, test_end;
0070     u64 avg_response_time = 0;
0071     u64 max_response_time = 0;
0072     u64 ipc_response_time;
0073     int i = 0;
0074     int ret;
0075 
0076     /* configure test IPC */
0077     hdr.cmd = SOF_IPC_GLB_TEST_MSG | SOF_IPC_TEST_IPC_FLOOD;
0078     hdr.size = sizeof(hdr);
0079 
0080     /* set test end time for duration flood test */
0081     if (flood_duration_test)
0082         test_end = ktime_get_ns() + ipc_duration_ms * NSEC_PER_MSEC;
0083 
0084     /* send test IPC's */
0085     while (1) {
0086         start = ktime_get();
0087         ret = sof_client_ipc_tx_message(cdev, &hdr, &reply, sizeof(reply));
0088         end = ktime_get();
0089 
0090         if (ret < 0)
0091             break;
0092 
0093         /* compute min and max response times */
0094         ipc_response_time = ktime_to_ns(ktime_sub(end, start));
0095         min_response_time = min(min_response_time, ipc_response_time);
0096         max_response_time = max(max_response_time, ipc_response_time);
0097 
0098         /* sum up response times */
0099         avg_response_time += ipc_response_time;
0100         i++;
0101 
0102         /* test complete? */
0103         if (flood_duration_test) {
0104             if (ktime_to_ns(end) >= test_end)
0105                 break;
0106         } else {
0107             if (i == ipc_count)
0108                 break;
0109         }
0110     }
0111 
0112     if (ret < 0)
0113         dev_err(dev, "ipc flood test failed at %d iterations\n", i);
0114 
0115     /* return if the first IPC fails */
0116     if (!i)
0117         return ret;
0118 
0119     /* compute average response time */
0120     do_div(avg_response_time, i);
0121 
0122     /* clear previous test output */
0123     memset(priv->buf, 0, IPC_FLOOD_TEST_RESULT_LEN);
0124 
0125     if (!ipc_count) {
0126         dev_dbg(dev, "IPC Flood test duration: %lums\n", ipc_duration_ms);
0127         snprintf(priv->buf, IPC_FLOOD_TEST_RESULT_LEN,
0128              "IPC Flood test duration: %lums\n", ipc_duration_ms);
0129     }
0130 
0131     dev_dbg(dev, "IPC Flood count: %d, Avg response time: %lluns\n",
0132         i, avg_response_time);
0133     dev_dbg(dev, "Max response time: %lluns\n", max_response_time);
0134     dev_dbg(dev, "Min response time: %lluns\n", min_response_time);
0135 
0136     /* format output string and save test results */
0137     snprintf(priv->buf + strlen(priv->buf),
0138          IPC_FLOOD_TEST_RESULT_LEN - strlen(priv->buf),
0139          "IPC Flood count: %d\nAvg response time: %lluns\n",
0140          i, avg_response_time);
0141 
0142     snprintf(priv->buf + strlen(priv->buf),
0143          IPC_FLOOD_TEST_RESULT_LEN - strlen(priv->buf),
0144          "Max response time: %lluns\nMin response time: %lluns\n",
0145          max_response_time, min_response_time);
0146 
0147     return ret;
0148 }
0149 
0150 /*
0151  * Writing to the debugfs entry initiates the IPC flood test based on
0152  * the IPC count or the duration specified by the user.
0153  */
0154 static ssize_t sof_ipc_flood_dfs_write(struct file *file, const char __user *buffer,
0155                        size_t count, loff_t *ppos)
0156 {
0157     struct sof_client_dev *cdev = file->private_data;
0158     struct device *dev = &cdev->auxdev.dev;
0159     unsigned long ipc_duration_ms = 0;
0160     bool flood_duration_test = false;
0161     unsigned long ipc_count = 0;
0162     struct dentry *dentry;
0163     int err;
0164     size_t size;
0165     char *string;
0166     int ret;
0167 
0168     string = kzalloc(count + 1, GFP_KERNEL);
0169     if (!string)
0170         return -ENOMEM;
0171 
0172     size = simple_write_to_buffer(string, count, ppos, buffer, count);
0173 
0174     /*
0175      * write op is only supported for ipc_flood_count or
0176      * ipc_flood_duration_ms debugfs entries atm.
0177      * ipc_flood_count floods the DSP with the number of IPC's specified.
0178      * ipc_duration_ms test floods the DSP for the time specified
0179      * in the debugfs entry.
0180      */
0181     dentry = file->f_path.dentry;
0182     if (strcmp(dentry->d_name.name, DEBUGFS_IPC_FLOOD_COUNT) &&
0183         strcmp(dentry->d_name.name, DEBUGFS_IPC_FLOOD_DURATION)) {
0184         ret = -EINVAL;
0185         goto out;
0186     }
0187 
0188     if (!strcmp(dentry->d_name.name, DEBUGFS_IPC_FLOOD_DURATION))
0189         flood_duration_test = true;
0190 
0191     /* test completion criterion */
0192     if (flood_duration_test)
0193         ret = kstrtoul(string, 0, &ipc_duration_ms);
0194     else
0195         ret = kstrtoul(string, 0, &ipc_count);
0196     if (ret < 0)
0197         goto out;
0198 
0199     /* limit max duration/ipc count for flood test */
0200     if (flood_duration_test) {
0201         if (!ipc_duration_ms) {
0202             ret = size;
0203             goto out;
0204         }
0205 
0206         /* find the minimum. min() is not used to avoid warnings */
0207         if (ipc_duration_ms > MAX_IPC_FLOOD_DURATION_MS)
0208             ipc_duration_ms = MAX_IPC_FLOOD_DURATION_MS;
0209     } else {
0210         if (!ipc_count) {
0211             ret = size;
0212             goto out;
0213         }
0214 
0215         /* find the minimum. min() is not used to avoid warnings */
0216         if (ipc_count > MAX_IPC_FLOOD_COUNT)
0217             ipc_count = MAX_IPC_FLOOD_COUNT;
0218     }
0219 
0220     ret = pm_runtime_resume_and_get(dev);
0221     if (ret < 0 && ret != -EACCES) {
0222         dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
0223         goto out;
0224     }
0225 
0226     /* flood test */
0227     ret = sof_debug_ipc_flood_test(cdev, flood_duration_test,
0228                        ipc_duration_ms, ipc_count);
0229 
0230     pm_runtime_mark_last_busy(dev);
0231     err = pm_runtime_put_autosuspend(dev);
0232     if (err < 0)
0233         dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
0234 
0235     /* return size if test is successful */
0236     if (ret >= 0)
0237         ret = size;
0238 out:
0239     kfree(string);
0240     return ret;
0241 }
0242 
0243 /* return the result of the last IPC flood test */
0244 static ssize_t sof_ipc_flood_dfs_read(struct file *file, char __user *buffer,
0245                       size_t count, loff_t *ppos)
0246 {
0247     struct sof_client_dev *cdev = file->private_data;
0248     struct sof_ipc_flood_priv *priv = cdev->data;
0249     size_t size_ret;
0250 
0251     struct dentry *dentry;
0252 
0253     dentry = file->f_path.dentry;
0254     if (!strcmp(dentry->d_name.name, DEBUGFS_IPC_FLOOD_COUNT) ||
0255         !strcmp(dentry->d_name.name, DEBUGFS_IPC_FLOOD_DURATION)) {
0256         if (*ppos)
0257             return 0;
0258 
0259         count = min_t(size_t, count, strlen(priv->buf));
0260         size_ret = copy_to_user(buffer, priv->buf, count);
0261         if (size_ret)
0262             return -EFAULT;
0263 
0264         *ppos += count;
0265         return count;
0266     }
0267     return count;
0268 }
0269 
0270 static int sof_ipc_flood_dfs_release(struct inode *inode, struct file *file)
0271 {
0272     debugfs_file_put(file->f_path.dentry);
0273 
0274     return 0;
0275 }
0276 
0277 static const struct file_operations sof_ipc_flood_fops = {
0278     .open = sof_ipc_flood_dfs_open,
0279     .read = sof_ipc_flood_dfs_read,
0280     .llseek = default_llseek,
0281     .write = sof_ipc_flood_dfs_write,
0282     .release = sof_ipc_flood_dfs_release,
0283 
0284     .owner = THIS_MODULE,
0285 };
0286 
0287 /*
0288  * The IPC test client creates a couple of debugfs entries that will be used
0289  * flood tests. Users can write to these entries to execute the IPC flood test
0290  * by specifying either the number of IPCs to flood the DSP with or the duration
0291  * (in ms) for which the DSP should be flooded with test IPCs. At the
0292  * end of each test, the average, min and max response times are reported back.
0293  * The results of the last flood test can be accessed by reading the debugfs
0294  * entries.
0295  */
0296 static int sof_ipc_flood_probe(struct auxiliary_device *auxdev,
0297                    const struct auxiliary_device_id *id)
0298 {
0299     struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
0300     struct dentry *debugfs_root = sof_client_get_debugfs_root(cdev);
0301     struct device *dev = &auxdev->dev;
0302     struct sof_ipc_flood_priv *priv;
0303 
0304     /* allocate memory for client data */
0305     priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0306     if (!priv)
0307         return -ENOMEM;
0308 
0309     priv->buf = devm_kmalloc(dev, IPC_FLOOD_TEST_RESULT_LEN, GFP_KERNEL);
0310     if (!priv->buf)
0311         return -ENOMEM;
0312 
0313     cdev->data = priv;
0314 
0315     /* create debugfs root folder with device name under parent SOF dir */
0316     priv->dfs_root = debugfs_create_dir(dev_name(dev), debugfs_root);
0317     if (!IS_ERR_OR_NULL(priv->dfs_root)) {
0318         /* create read-write ipc_flood_count debugfs entry */
0319         debugfs_create_file(DEBUGFS_IPC_FLOOD_COUNT, 0644, priv->dfs_root,
0320                     cdev, &sof_ipc_flood_fops);
0321 
0322         /* create read-write ipc_flood_duration_ms debugfs entry */
0323         debugfs_create_file(DEBUGFS_IPC_FLOOD_DURATION, 0644,
0324                     priv->dfs_root, cdev, &sof_ipc_flood_fops);
0325 
0326         if (auxdev->id == 0) {
0327             /*
0328              * Create symlinks for backwards compatibility to the
0329              * first IPC flood test instance
0330              */
0331             char target[100];
0332 
0333             snprintf(target, 100, "%s/" DEBUGFS_IPC_FLOOD_COUNT,
0334                  dev_name(dev));
0335             priv->dfs_link[0] =
0336                 debugfs_create_symlink(DEBUGFS_IPC_FLOOD_COUNT,
0337                                debugfs_root, target);
0338 
0339             snprintf(target, 100, "%s/" DEBUGFS_IPC_FLOOD_DURATION,
0340                  dev_name(dev));
0341             priv->dfs_link[1] =
0342                 debugfs_create_symlink(DEBUGFS_IPC_FLOOD_DURATION,
0343                                debugfs_root, target);
0344         }
0345     }
0346 
0347     /* enable runtime PM */
0348     pm_runtime_set_autosuspend_delay(dev, SOF_IPC_CLIENT_SUSPEND_DELAY_MS);
0349     pm_runtime_use_autosuspend(dev);
0350     pm_runtime_enable(dev);
0351     pm_runtime_mark_last_busy(dev);
0352     pm_runtime_idle(dev);
0353 
0354     return 0;
0355 }
0356 
0357 static void sof_ipc_flood_remove(struct auxiliary_device *auxdev)
0358 {
0359     struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
0360     struct sof_ipc_flood_priv *priv = cdev->data;
0361 
0362     pm_runtime_disable(&auxdev->dev);
0363 
0364     if (auxdev->id == 0) {
0365         debugfs_remove(priv->dfs_link[0]);
0366         debugfs_remove(priv->dfs_link[1]);
0367     }
0368 
0369     debugfs_remove_recursive(priv->dfs_root);
0370 }
0371 
0372 static const struct auxiliary_device_id sof_ipc_flood_client_id_table[] = {
0373     { .name = "snd_sof.ipc_flood" },
0374     {},
0375 };
0376 MODULE_DEVICE_TABLE(auxiliary, sof_ipc_flood_client_id_table);
0377 
0378 /*
0379  * No need for driver pm_ops as the generic pm callbacks in the auxiliary bus
0380  * type are enough to ensure that the parent SOF device resumes to bring the DSP
0381  * back to D0.
0382  * Driver name will be set based on KBUILD_MODNAME.
0383  */
0384 static struct auxiliary_driver sof_ipc_flood_client_drv = {
0385     .probe = sof_ipc_flood_probe,
0386     .remove = sof_ipc_flood_remove,
0387 
0388     .id_table = sof_ipc_flood_client_id_table,
0389 };
0390 
0391 module_auxiliary_driver(sof_ipc_flood_client_drv);
0392 
0393 MODULE_DESCRIPTION("SOF IPC Flood Test Client Driver");
0394 MODULE_LICENSE("GPL");
0395 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);