1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel MIC Platform Software Stack (MPSS)
4  *
5  * Copyright(c) 2015 Intel Corporation.
6  *
7  * Intel MIC Coprocessor State Management (COSM) Driver
8  */
9 
10 #include <linux/debugfs.h>
11 #include <linux/slab.h>
12 #include <linux/io.h>
13 #include "cosm_main.h"
14 
15 /* Debugfs parent dir */
16 static struct dentry *cosm_dbg;
17 
18 /**
19  * log_buf_show - Display MIC kernel log buffer
20  *
21  * log_buf addr/len is read from System.map by user space
22  * and populated in sysfs entries.
23  */
log_buf_show(struct seq_file * s,void * unused)24 static int log_buf_show(struct seq_file *s, void *unused)
25 {
26 	void __iomem *log_buf_va;
27 	int __iomem *log_buf_len_va;
28 	struct cosm_device *cdev = s->private;
29 	void *kva;
30 	int size;
31 	u64 aper_offset;
32 
33 	if (!cdev || !cdev->log_buf_addr || !cdev->log_buf_len)
34 		goto done;
35 
36 	mutex_lock(&cdev->cosm_mutex);
37 	switch (cdev->state) {
38 	case MIC_BOOTING:
39 	case MIC_ONLINE:
40 	case MIC_SHUTTING_DOWN:
41 		break;
42 	default:
43 		goto unlock;
44 	}
45 
46 	/*
47 	 * Card kernel will never be relocated and any kernel text/data mapping
48 	 * can be translated to phys address by subtracting __START_KERNEL_map.
49 	 */
50 	aper_offset = (u64)cdev->log_buf_len - __START_KERNEL_map;
51 	log_buf_len_va = cdev->hw_ops->aper(cdev)->va + aper_offset;
52 	aper_offset = (u64)cdev->log_buf_addr - __START_KERNEL_map;
53 	log_buf_va = cdev->hw_ops->aper(cdev)->va + aper_offset;
54 
55 	size = ioread32(log_buf_len_va);
56 	kva = kmalloc(size, GFP_KERNEL);
57 	if (!kva)
58 		goto unlock;
59 
60 	memcpy_fromio(kva, log_buf_va, size);
61 	seq_write(s, kva, size);
62 	kfree(kva);
63 unlock:
64 	mutex_unlock(&cdev->cosm_mutex);
65 done:
66 	return 0;
67 }
68 
69 DEFINE_SHOW_ATTRIBUTE(log_buf);
70 
71 /**
72  * force_reset_show - Force MIC reset
73  *
74  * Invokes the force_reset COSM bus op instead of the standard reset
75  * op in case a force reset of the MIC device is required
76  */
force_reset_show(struct seq_file * s,void * pos)77 static int force_reset_show(struct seq_file *s, void *pos)
78 {
79 	struct cosm_device *cdev = s->private;
80 
81 	cosm_stop(cdev, true);
82 	return 0;
83 }
84 
85 DEFINE_SHOW_ATTRIBUTE(force_reset);
86 
cosm_create_debug_dir(struct cosm_device * cdev)87 void cosm_create_debug_dir(struct cosm_device *cdev)
88 {
89 	char name[16];
90 
91 	if (!cosm_dbg)
92 		return;
93 
94 	scnprintf(name, sizeof(name), "mic%d", cdev->index);
95 	cdev->dbg_dir = debugfs_create_dir(name, cosm_dbg);
96 
97 	debugfs_create_file("log_buf", 0444, cdev->dbg_dir, cdev,
98 			    &log_buf_fops);
99 	debugfs_create_file("force_reset", 0444, cdev->dbg_dir, cdev,
100 			    &force_reset_fops);
101 }
102 
cosm_delete_debug_dir(struct cosm_device * cdev)103 void cosm_delete_debug_dir(struct cosm_device *cdev)
104 {
105 	if (!cdev->dbg_dir)
106 		return;
107 
108 	debugfs_remove_recursive(cdev->dbg_dir);
109 }
110 
cosm_init_debugfs(void)111 void cosm_init_debugfs(void)
112 {
113 	cosm_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
114 }
115 
cosm_exit_debugfs(void)116 void cosm_exit_debugfs(void)
117 {
118 	debugfs_remove(cosm_dbg);
119 }
120