1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23cdbe346SGary R Hook /*
33cdbe346SGary R Hook * AMD Cryptographic Coprocessor (CCP) driver
43cdbe346SGary R Hook *
53cdbe346SGary R Hook * Copyright (C) 2017 Advanced Micro Devices, Inc.
63cdbe346SGary R Hook *
73cdbe346SGary R Hook * Author: Gary R Hook <gary.hook@amd.com>
83cdbe346SGary R Hook */
93cdbe346SGary R Hook
103cdbe346SGary R Hook #include <linux/debugfs.h>
113cdbe346SGary R Hook #include <linux/ccp.h>
123cdbe346SGary R Hook
133cdbe346SGary R Hook #include "ccp-dev.h"
143cdbe346SGary R Hook
153cdbe346SGary R Hook /* DebugFS helpers */
163cdbe346SGary R Hook #define OBUFP (obuf + oboff)
173cdbe346SGary R Hook #define OBUFLEN 512
183cdbe346SGary R Hook #define OBUFSPC (OBUFLEN - oboff)
193cdbe346SGary R Hook #define OSCNPRINTF(fmt, ...) \
203cdbe346SGary R Hook scnprintf(OBUFP, OBUFSPC, fmt, ## __VA_ARGS__)
213cdbe346SGary R Hook
223cdbe346SGary R Hook #define BUFLEN 63
233cdbe346SGary R Hook
243cdbe346SGary R Hook #define RI_VERSION_NUM 0x0000003F
253cdbe346SGary R Hook #define RI_AES_PRESENT 0x00000040
263cdbe346SGary R Hook #define RI_3DES_PRESENT 0x00000080
273cdbe346SGary R Hook #define RI_SHA_PRESENT 0x00000100
283cdbe346SGary R Hook #define RI_RSA_PRESENT 0x00000200
293cdbe346SGary R Hook #define RI_ECC_PRESENT 0x00000400
303cdbe346SGary R Hook #define RI_ZDE_PRESENT 0x00000800
313cdbe346SGary R Hook #define RI_ZCE_PRESENT 0x00001000
323cdbe346SGary R Hook #define RI_TRNG_PRESENT 0x00002000
333cdbe346SGary R Hook #define RI_ELFC_PRESENT 0x00004000
343cdbe346SGary R Hook #define RI_ELFC_SHIFT 14
353cdbe346SGary R Hook #define RI_NUM_VQM 0x00078000
363cdbe346SGary R Hook #define RI_NVQM_SHIFT 15
373cdbe346SGary R Hook #define RI_NVQM(r) (((r) * RI_NUM_VQM) >> RI_NVQM_SHIFT)
383cdbe346SGary R Hook #define RI_LSB_ENTRIES 0x0FF80000
393cdbe346SGary R Hook #define RI_NLSB_SHIFT 19
403cdbe346SGary R Hook #define RI_NLSB(r) (((r) * RI_LSB_ENTRIES) >> RI_NLSB_SHIFT)
413cdbe346SGary R Hook
ccp5_debugfs_info_read(struct file * filp,char __user * ubuf,size_t count,loff_t * offp)423cdbe346SGary R Hook static ssize_t ccp5_debugfs_info_read(struct file *filp, char __user *ubuf,
433cdbe346SGary R Hook size_t count, loff_t *offp)
443cdbe346SGary R Hook {
453cdbe346SGary R Hook struct ccp_device *ccp = filp->private_data;
463cdbe346SGary R Hook unsigned int oboff = 0;
473cdbe346SGary R Hook unsigned int regval;
483cdbe346SGary R Hook ssize_t ret;
493cdbe346SGary R Hook char *obuf;
503cdbe346SGary R Hook
513cdbe346SGary R Hook if (!ccp)
523cdbe346SGary R Hook return 0;
533cdbe346SGary R Hook
543cdbe346SGary R Hook obuf = kmalloc(OBUFLEN, GFP_KERNEL);
553cdbe346SGary R Hook if (!obuf)
563cdbe346SGary R Hook return -ENOMEM;
573cdbe346SGary R Hook
583cdbe346SGary R Hook oboff += OSCNPRINTF("Device name: %s\n", ccp->name);
593cdbe346SGary R Hook oboff += OSCNPRINTF(" RNG name: %s\n", ccp->rngname);
603cdbe346SGary R Hook oboff += OSCNPRINTF(" # Queues: %d\n", ccp->cmd_q_count);
613cdbe346SGary R Hook oboff += OSCNPRINTF(" # Cmds: %d\n", ccp->cmd_count);
623cdbe346SGary R Hook
633cdbe346SGary R Hook regval = ioread32(ccp->io_regs + CMD5_PSP_CCP_VERSION);
643cdbe346SGary R Hook oboff += OSCNPRINTF(" Version: %d\n", regval & RI_VERSION_NUM);
653cdbe346SGary R Hook oboff += OSCNPRINTF(" Engines:");
663cdbe346SGary R Hook if (regval & RI_AES_PRESENT)
673cdbe346SGary R Hook oboff += OSCNPRINTF(" AES");
683cdbe346SGary R Hook if (regval & RI_3DES_PRESENT)
693cdbe346SGary R Hook oboff += OSCNPRINTF(" 3DES");
703cdbe346SGary R Hook if (regval & RI_SHA_PRESENT)
713cdbe346SGary R Hook oboff += OSCNPRINTF(" SHA");
723cdbe346SGary R Hook if (regval & RI_RSA_PRESENT)
733cdbe346SGary R Hook oboff += OSCNPRINTF(" RSA");
743cdbe346SGary R Hook if (regval & RI_ECC_PRESENT)
753cdbe346SGary R Hook oboff += OSCNPRINTF(" ECC");
763cdbe346SGary R Hook if (regval & RI_ZDE_PRESENT)
773cdbe346SGary R Hook oboff += OSCNPRINTF(" ZDE");
783cdbe346SGary R Hook if (regval & RI_ZCE_PRESENT)
793cdbe346SGary R Hook oboff += OSCNPRINTF(" ZCE");
803cdbe346SGary R Hook if (regval & RI_TRNG_PRESENT)
813cdbe346SGary R Hook oboff += OSCNPRINTF(" TRNG");
823cdbe346SGary R Hook oboff += OSCNPRINTF("\n");
833cdbe346SGary R Hook oboff += OSCNPRINTF(" Queues: %d\n",
843cdbe346SGary R Hook (regval & RI_NUM_VQM) >> RI_NVQM_SHIFT);
853cdbe346SGary R Hook oboff += OSCNPRINTF("LSB Entries: %d\n",
863cdbe346SGary R Hook (regval & RI_LSB_ENTRIES) >> RI_NLSB_SHIFT);
873cdbe346SGary R Hook
883cdbe346SGary R Hook ret = simple_read_from_buffer(ubuf, count, offp, obuf, oboff);
893cdbe346SGary R Hook kfree(obuf);
903cdbe346SGary R Hook
913cdbe346SGary R Hook return ret;
923cdbe346SGary R Hook }
933cdbe346SGary R Hook
943cdbe346SGary R Hook /* Return a formatted buffer containing the current
953cdbe346SGary R Hook * statistics across all queues for a CCP.
963cdbe346SGary R Hook */
ccp5_debugfs_stats_read(struct file * filp,char __user * ubuf,size_t count,loff_t * offp)973cdbe346SGary R Hook static ssize_t ccp5_debugfs_stats_read(struct file *filp, char __user *ubuf,
983cdbe346SGary R Hook size_t count, loff_t *offp)
993cdbe346SGary R Hook {
1003cdbe346SGary R Hook struct ccp_device *ccp = filp->private_data;
1013cdbe346SGary R Hook unsigned long total_xts_aes_ops = 0;
1023cdbe346SGary R Hook unsigned long total_3des_ops = 0;
1033cdbe346SGary R Hook unsigned long total_aes_ops = 0;
1043cdbe346SGary R Hook unsigned long total_sha_ops = 0;
1053cdbe346SGary R Hook unsigned long total_rsa_ops = 0;
1063cdbe346SGary R Hook unsigned long total_ecc_ops = 0;
1073cdbe346SGary R Hook unsigned long total_pt_ops = 0;
1083cdbe346SGary R Hook unsigned long total_ops = 0;
1093cdbe346SGary R Hook unsigned int oboff = 0;
1103cdbe346SGary R Hook ssize_t ret = 0;
1113cdbe346SGary R Hook unsigned int i;
1123cdbe346SGary R Hook char *obuf;
1133cdbe346SGary R Hook
1143cdbe346SGary R Hook for (i = 0; i < ccp->cmd_q_count; i++) {
1153cdbe346SGary R Hook struct ccp_cmd_queue *cmd_q = &ccp->cmd_q[i];
1163cdbe346SGary R Hook
1173cdbe346SGary R Hook total_ops += cmd_q->total_ops;
1183cdbe346SGary R Hook total_aes_ops += cmd_q->total_aes_ops;
1193cdbe346SGary R Hook total_xts_aes_ops += cmd_q->total_xts_aes_ops;
1203cdbe346SGary R Hook total_3des_ops += cmd_q->total_3des_ops;
1213cdbe346SGary R Hook total_sha_ops += cmd_q->total_sha_ops;
1223cdbe346SGary R Hook total_rsa_ops += cmd_q->total_rsa_ops;
1233cdbe346SGary R Hook total_pt_ops += cmd_q->total_pt_ops;
1243cdbe346SGary R Hook total_ecc_ops += cmd_q->total_ecc_ops;
1253cdbe346SGary R Hook }
1263cdbe346SGary R Hook
1273cdbe346SGary R Hook obuf = kmalloc(OBUFLEN, GFP_KERNEL);
1283cdbe346SGary R Hook if (!obuf)
1293cdbe346SGary R Hook return -ENOMEM;
1303cdbe346SGary R Hook
1313cdbe346SGary R Hook oboff += OSCNPRINTF("Total Interrupts Handled: %ld\n",
1323cdbe346SGary R Hook ccp->total_interrupts);
1333cdbe346SGary R Hook oboff += OSCNPRINTF(" Total Operations: %ld\n",
1343cdbe346SGary R Hook total_ops);
1353cdbe346SGary R Hook oboff += OSCNPRINTF(" AES: %ld\n",
1363cdbe346SGary R Hook total_aes_ops);
1373cdbe346SGary R Hook oboff += OSCNPRINTF(" XTS AES: %ld\n",
1383cdbe346SGary R Hook total_xts_aes_ops);
1393cdbe346SGary R Hook oboff += OSCNPRINTF(" SHA: %ld\n",
1403cdbe346SGary R Hook total_3des_ops);
1413cdbe346SGary R Hook oboff += OSCNPRINTF(" SHA: %ld\n",
1423cdbe346SGary R Hook total_sha_ops);
1433cdbe346SGary R Hook oboff += OSCNPRINTF(" RSA: %ld\n",
1443cdbe346SGary R Hook total_rsa_ops);
1453cdbe346SGary R Hook oboff += OSCNPRINTF(" Pass-Thru: %ld\n",
1463cdbe346SGary R Hook total_pt_ops);
1473cdbe346SGary R Hook oboff += OSCNPRINTF(" ECC: %ld\n",
1483cdbe346SGary R Hook total_ecc_ops);
1493cdbe346SGary R Hook
1503cdbe346SGary R Hook ret = simple_read_from_buffer(ubuf, count, offp, obuf, oboff);
1513cdbe346SGary R Hook kfree(obuf);
1523cdbe346SGary R Hook
1533cdbe346SGary R Hook return ret;
1543cdbe346SGary R Hook }
1553cdbe346SGary R Hook
1563cdbe346SGary R Hook /* Reset the counters in a queue
1573cdbe346SGary R Hook */
ccp5_debugfs_reset_queue_stats(struct ccp_cmd_queue * cmd_q)1583cdbe346SGary R Hook static void ccp5_debugfs_reset_queue_stats(struct ccp_cmd_queue *cmd_q)
1593cdbe346SGary R Hook {
1603cdbe346SGary R Hook cmd_q->total_ops = 0L;
1613cdbe346SGary R Hook cmd_q->total_aes_ops = 0L;
1623cdbe346SGary R Hook cmd_q->total_xts_aes_ops = 0L;
1633cdbe346SGary R Hook cmd_q->total_3des_ops = 0L;
1643cdbe346SGary R Hook cmd_q->total_sha_ops = 0L;
1653cdbe346SGary R Hook cmd_q->total_rsa_ops = 0L;
1663cdbe346SGary R Hook cmd_q->total_pt_ops = 0L;
1673cdbe346SGary R Hook cmd_q->total_ecc_ops = 0L;
1683cdbe346SGary R Hook }
1693cdbe346SGary R Hook
1703cdbe346SGary R Hook /* A value was written to the stats variable, which
1713cdbe346SGary R Hook * should be used to reset the queue counters across
1723cdbe346SGary R Hook * that device.
1733cdbe346SGary R Hook */
ccp5_debugfs_stats_write(struct file * filp,const char __user * ubuf,size_t count,loff_t * offp)1743cdbe346SGary R Hook static ssize_t ccp5_debugfs_stats_write(struct file *filp,
1753cdbe346SGary R Hook const char __user *ubuf,
1763cdbe346SGary R Hook size_t count, loff_t *offp)
1773cdbe346SGary R Hook {
1783cdbe346SGary R Hook struct ccp_device *ccp = filp->private_data;
1793cdbe346SGary R Hook int i;
1803cdbe346SGary R Hook
1813cdbe346SGary R Hook for (i = 0; i < ccp->cmd_q_count; i++)
1823cdbe346SGary R Hook ccp5_debugfs_reset_queue_stats(&ccp->cmd_q[i]);
1833cdbe346SGary R Hook ccp->total_interrupts = 0L;
1843cdbe346SGary R Hook
1853cdbe346SGary R Hook return count;
1863cdbe346SGary R Hook }
1873cdbe346SGary R Hook
1883cdbe346SGary R Hook /* Return a formatted buffer containing the current information
1893cdbe346SGary R Hook * for that queue
1903cdbe346SGary R Hook */
ccp5_debugfs_queue_read(struct file * filp,char __user * ubuf,size_t count,loff_t * offp)1913cdbe346SGary R Hook static ssize_t ccp5_debugfs_queue_read(struct file *filp, char __user *ubuf,
1923cdbe346SGary R Hook size_t count, loff_t *offp)
1933cdbe346SGary R Hook {
1943cdbe346SGary R Hook struct ccp_cmd_queue *cmd_q = filp->private_data;
1953cdbe346SGary R Hook unsigned int oboff = 0;
1963cdbe346SGary R Hook unsigned int regval;
1973cdbe346SGary R Hook ssize_t ret;
1983cdbe346SGary R Hook char *obuf;
1993cdbe346SGary R Hook
2003cdbe346SGary R Hook if (!cmd_q)
2013cdbe346SGary R Hook return 0;
2023cdbe346SGary R Hook
2033cdbe346SGary R Hook obuf = kmalloc(OBUFLEN, GFP_KERNEL);
2043cdbe346SGary R Hook if (!obuf)
2053cdbe346SGary R Hook return -ENOMEM;
2063cdbe346SGary R Hook
2073cdbe346SGary R Hook oboff += OSCNPRINTF(" Total Queue Operations: %ld\n",
2083cdbe346SGary R Hook cmd_q->total_ops);
2093cdbe346SGary R Hook oboff += OSCNPRINTF(" AES: %ld\n",
2103cdbe346SGary R Hook cmd_q->total_aes_ops);
2113cdbe346SGary R Hook oboff += OSCNPRINTF(" XTS AES: %ld\n",
2123cdbe346SGary R Hook cmd_q->total_xts_aes_ops);
2133cdbe346SGary R Hook oboff += OSCNPRINTF(" SHA: %ld\n",
2143cdbe346SGary R Hook cmd_q->total_3des_ops);
2153cdbe346SGary R Hook oboff += OSCNPRINTF(" SHA: %ld\n",
2163cdbe346SGary R Hook cmd_q->total_sha_ops);
2173cdbe346SGary R Hook oboff += OSCNPRINTF(" RSA: %ld\n",
2183cdbe346SGary R Hook cmd_q->total_rsa_ops);
2193cdbe346SGary R Hook oboff += OSCNPRINTF(" Pass-Thru: %ld\n",
2203cdbe346SGary R Hook cmd_q->total_pt_ops);
2213cdbe346SGary R Hook oboff += OSCNPRINTF(" ECC: %ld\n",
2223cdbe346SGary R Hook cmd_q->total_ecc_ops);
2233cdbe346SGary R Hook
2243cdbe346SGary R Hook regval = ioread32(cmd_q->reg_int_enable);
2253cdbe346SGary R Hook oboff += OSCNPRINTF(" Enabled Interrupts:");
2263cdbe346SGary R Hook if (regval & INT_EMPTY_QUEUE)
2273cdbe346SGary R Hook oboff += OSCNPRINTF(" EMPTY");
2283cdbe346SGary R Hook if (regval & INT_QUEUE_STOPPED)
2293cdbe346SGary R Hook oboff += OSCNPRINTF(" STOPPED");
2303cdbe346SGary R Hook if (regval & INT_ERROR)
2313cdbe346SGary R Hook oboff += OSCNPRINTF(" ERROR");
2323cdbe346SGary R Hook if (regval & INT_COMPLETION)
2333cdbe346SGary R Hook oboff += OSCNPRINTF(" COMPLETION");
2343cdbe346SGary R Hook oboff += OSCNPRINTF("\n");
2353cdbe346SGary R Hook
2363cdbe346SGary R Hook ret = simple_read_from_buffer(ubuf, count, offp, obuf, oboff);
2373cdbe346SGary R Hook kfree(obuf);
2383cdbe346SGary R Hook
2393cdbe346SGary R Hook return ret;
2403cdbe346SGary R Hook }
2413cdbe346SGary R Hook
2423cdbe346SGary R Hook /* A value was written to the stats variable for a
2433cdbe346SGary R Hook * queue. Reset the queue counters to this value.
2443cdbe346SGary R Hook */
ccp5_debugfs_queue_write(struct file * filp,const char __user * ubuf,size_t count,loff_t * offp)2453cdbe346SGary R Hook static ssize_t ccp5_debugfs_queue_write(struct file *filp,
2463cdbe346SGary R Hook const char __user *ubuf,
2473cdbe346SGary R Hook size_t count, loff_t *offp)
2483cdbe346SGary R Hook {
2493cdbe346SGary R Hook struct ccp_cmd_queue *cmd_q = filp->private_data;
2503cdbe346SGary R Hook
2513cdbe346SGary R Hook ccp5_debugfs_reset_queue_stats(cmd_q);
2523cdbe346SGary R Hook
2533cdbe346SGary R Hook return count;
2543cdbe346SGary R Hook }
2553cdbe346SGary R Hook
2563cdbe346SGary R Hook static const struct file_operations ccp_debugfs_info_ops = {
2573cdbe346SGary R Hook .owner = THIS_MODULE,
2583cdbe346SGary R Hook .open = simple_open,
2593cdbe346SGary R Hook .read = ccp5_debugfs_info_read,
2603cdbe346SGary R Hook .write = NULL,
2613cdbe346SGary R Hook };
2623cdbe346SGary R Hook
2633cdbe346SGary R Hook static const struct file_operations ccp_debugfs_queue_ops = {
2643cdbe346SGary R Hook .owner = THIS_MODULE,
2653cdbe346SGary R Hook .open = simple_open,
2663cdbe346SGary R Hook .read = ccp5_debugfs_queue_read,
2673cdbe346SGary R Hook .write = ccp5_debugfs_queue_write,
2683cdbe346SGary R Hook };
2693cdbe346SGary R Hook
2703cdbe346SGary R Hook static const struct file_operations ccp_debugfs_stats_ops = {
2713cdbe346SGary R Hook .owner = THIS_MODULE,
2723cdbe346SGary R Hook .open = simple_open,
2733cdbe346SGary R Hook .read = ccp5_debugfs_stats_read,
2743cdbe346SGary R Hook .write = ccp5_debugfs_stats_write,
2753cdbe346SGary R Hook };
2763cdbe346SGary R Hook
2773cdbe346SGary R Hook static struct dentry *ccp_debugfs_dir;
27879eb382bSSebastian Andrzej Siewior static DEFINE_MUTEX(ccp_debugfs_lock);
2793cdbe346SGary R Hook
2803cdbe346SGary R Hook #define MAX_NAME_LEN 20
2813cdbe346SGary R Hook
ccp5_debugfs_setup(struct ccp_device * ccp)2823cdbe346SGary R Hook void ccp5_debugfs_setup(struct ccp_device *ccp)
2833cdbe346SGary R Hook {
2843cdbe346SGary R Hook struct ccp_cmd_queue *cmd_q;
2853cdbe346SGary R Hook char name[MAX_NAME_LEN + 1];
2863cdbe346SGary R Hook struct dentry *debugfs_q_instance;
2873cdbe346SGary R Hook int i;
2883cdbe346SGary R Hook
2893cdbe346SGary R Hook if (!debugfs_initialized())
2903cdbe346SGary R Hook return;
2913cdbe346SGary R Hook
29279eb382bSSebastian Andrzej Siewior mutex_lock(&ccp_debugfs_lock);
29330b4c54cSGary R Hook if (!ccp_debugfs_dir)
2943cdbe346SGary R Hook ccp_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
29579eb382bSSebastian Andrzej Siewior mutex_unlock(&ccp_debugfs_lock);
2963cdbe346SGary R Hook
2973cdbe346SGary R Hook ccp->debugfs_instance = debugfs_create_dir(ccp->name, ccp_debugfs_dir);
2983cdbe346SGary R Hook
29931e1d260SGreg Kroah-Hartman debugfs_create_file("info", 0400, ccp->debugfs_instance, ccp,
3003cdbe346SGary R Hook &ccp_debugfs_info_ops);
3013cdbe346SGary R Hook
30231e1d260SGreg Kroah-Hartman debugfs_create_file("stats", 0600, ccp->debugfs_instance, ccp,
3033cdbe346SGary R Hook &ccp_debugfs_stats_ops);
3043cdbe346SGary R Hook
3053cdbe346SGary R Hook for (i = 0; i < ccp->cmd_q_count; i++) {
3063cdbe346SGary R Hook cmd_q = &ccp->cmd_q[i];
3073cdbe346SGary R Hook
3083cdbe346SGary R Hook snprintf(name, MAX_NAME_LEN - 1, "q%d", cmd_q->id);
3093cdbe346SGary R Hook
3103cdbe346SGary R Hook debugfs_q_instance =
3113cdbe346SGary R Hook debugfs_create_dir(name, ccp->debugfs_instance);
3123cdbe346SGary R Hook
31331e1d260SGreg Kroah-Hartman debugfs_create_file("stats", 0600, debugfs_q_instance, cmd_q,
3143cdbe346SGary R Hook &ccp_debugfs_queue_ops);
3153cdbe346SGary R Hook }
31633460b2dSGary R Hook
31733460b2dSGary R Hook return;
3183cdbe346SGary R Hook }
3193cdbe346SGary R Hook
ccp5_debugfs_destroy(void)3203cdbe346SGary R Hook void ccp5_debugfs_destroy(void)
3213cdbe346SGary R Hook {
3223cdbe346SGary R Hook debugfs_remove_recursive(ccp_debugfs_dir);
3233cdbe346SGary R Hook }
324