Merge 4.3-rc5 into char-misc next
[firefly-linux-kernel-4.4.55.git] / drivers / misc / mic / cosm / cosm_debugfs.c
1 /*
2  * Intel MIC Platform Software Stack (MPSS)
3  *
4  * Copyright(c) 2015 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 2, as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * The full GNU General Public License is included in this distribution in
16  * the file called "COPYING".
17  *
18  * Intel MIC Coprocessor State Management (COSM) Driver
19  *
20  */
21
22 #include <linux/debugfs.h>
23 #include <linux/slab.h>
24 #include "cosm_main.h"
25
26 /* Debugfs parent dir */
27 static struct dentry *cosm_dbg;
28
29 /**
30  * cosm_log_buf_show - Display MIC kernel log buffer
31  *
32  * log_buf addr/len is read from System.map by user space
33  * and populated in sysfs entries.
34  */
35 static int cosm_log_buf_show(struct seq_file *s, void *unused)
36 {
37         void __iomem *log_buf_va;
38         int __iomem *log_buf_len_va;
39         struct cosm_device *cdev = s->private;
40         void *kva;
41         int size;
42         u64 aper_offset;
43
44         if (!cdev || !cdev->log_buf_addr || !cdev->log_buf_len)
45                 goto done;
46
47         mutex_lock(&cdev->cosm_mutex);
48         switch (cdev->state) {
49         case MIC_BOOTING:
50         case MIC_ONLINE:
51         case MIC_SHUTTING_DOWN:
52                 break;
53         default:
54                 goto unlock;
55         }
56
57         /*
58          * Card kernel will never be relocated and any kernel text/data mapping
59          * can be translated to phys address by subtracting __START_KERNEL_map.
60          */
61         aper_offset = (u64)cdev->log_buf_len - __START_KERNEL_map;
62         log_buf_len_va = cdev->hw_ops->aper(cdev)->va + aper_offset;
63         aper_offset = (u64)cdev->log_buf_addr - __START_KERNEL_map;
64         log_buf_va = cdev->hw_ops->aper(cdev)->va + aper_offset;
65
66         size = ioread32(log_buf_len_va);
67         kva = kmalloc(size, GFP_KERNEL);
68         if (!kva)
69                 goto unlock;
70
71         memcpy_fromio(kva, log_buf_va, size);
72         seq_write(s, kva, size);
73         kfree(kva);
74 unlock:
75         mutex_unlock(&cdev->cosm_mutex);
76 done:
77         return 0;
78 }
79
80 static int cosm_log_buf_open(struct inode *inode, struct file *file)
81 {
82         return single_open(file, cosm_log_buf_show, inode->i_private);
83 }
84
85 static const struct file_operations log_buf_ops = {
86         .owner   = THIS_MODULE,
87         .open    = cosm_log_buf_open,
88         .read    = seq_read,
89         .llseek  = seq_lseek,
90         .release = single_release
91 };
92
93 /**
94  * cosm_force_reset_show - Force MIC reset
95  *
96  * Invokes the force_reset COSM bus op instead of the standard reset
97  * op in case a force reset of the MIC device is required
98  */
99 static int cosm_force_reset_show(struct seq_file *s, void *pos)
100 {
101         struct cosm_device *cdev = s->private;
102
103         cosm_stop(cdev, true);
104         return 0;
105 }
106
107 static int cosm_force_reset_debug_open(struct inode *inode, struct file *file)
108 {
109         return single_open(file, cosm_force_reset_show, inode->i_private);
110 }
111
112 static const struct file_operations force_reset_ops = {
113         .owner   = THIS_MODULE,
114         .open    = cosm_force_reset_debug_open,
115         .read    = seq_read,
116         .llseek  = seq_lseek,
117         .release = single_release
118 };
119
120 void cosm_create_debug_dir(struct cosm_device *cdev)
121 {
122         char name[16];
123
124         if (!cosm_dbg)
125                 return;
126
127         scnprintf(name, sizeof(name), "mic%d", cdev->index);
128         cdev->dbg_dir = debugfs_create_dir(name, cosm_dbg);
129         if (!cdev->dbg_dir)
130                 return;
131
132         debugfs_create_file("log_buf", 0444, cdev->dbg_dir, cdev, &log_buf_ops);
133         debugfs_create_file("force_reset", 0444, cdev->dbg_dir, cdev,
134                             &force_reset_ops);
135 }
136
137 void cosm_delete_debug_dir(struct cosm_device *cdev)
138 {
139         if (!cdev->dbg_dir)
140                 return;
141
142         debugfs_remove_recursive(cdev->dbg_dir);
143 }
144
145 void cosm_init_debugfs(void)
146 {
147         cosm_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
148         if (!cosm_dbg)
149                 pr_err("can't create debugfs dir\n");
150 }
151
152 void cosm_exit_debugfs(void)
153 {
154         debugfs_remove(cosm_dbg);
155 }