1 /* 2 * Virtual Processor Dispatch Trace Log 3 * 4 * (C) Copyright IBM Corporation 2009 5 * 6 * Author: Jeremy Kerr <jk@ozlabs.org> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2, or (at your option) 11 * any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 */ 22 23 #include <linux/init.h> 24 #include <linux/debugfs.h> 25 #include <asm/smp.h> 26 #include <asm/system.h> 27 #include <asm/uaccess.h> 28 29 #include "plpar_wrappers.h" 30 31 /* 32 * Layout of entries in the hypervisor's DTL buffer. Although we don't 33 * actually access the internals of an entry (we only need to know the size), 34 * we might as well define it here for reference. 35 */ 36 struct dtl_entry { 37 u8 dispatch_reason; 38 u8 preempt_reason; 39 u16 processor_id; 40 u32 enqueue_to_dispatch_time; 41 u32 ready_to_enqueue_time; 42 u32 waiting_to_ready_time; 43 u64 timebase; 44 u64 fault_addr; 45 u64 srr0; 46 u64 srr1; 47 }; 48 49 struct dtl { 50 struct dtl_entry *buf; 51 struct dentry *file; 52 int cpu; 53 int buf_entries; 54 u64 last_idx; 55 }; 56 static DEFINE_PER_CPU(struct dtl, dtl); 57 58 /* 59 * Dispatch trace log event mask: 60 * 0x7: 0x1: voluntary virtual processor waits 61 * 0x2: time-slice preempts 62 * 0x4: virtual partition memory page faults 63 */ 64 static u8 dtl_event_mask = 0x7; 65 66 67 /* 68 * Size of per-cpu log buffers. Default is just under 16 pages worth. 69 */ 70 static int dtl_buf_entries = (16 * 85); 71 72 73 static int dtl_enable(struct dtl *dtl) 74 { 75 unsigned long addr; 76 int ret, hwcpu; 77 78 /* only allow one reader */ 79 if (dtl->buf) 80 return -EBUSY; 81 82 /* we need to store the original allocation size for use during read */ 83 dtl->buf_entries = dtl_buf_entries; 84 85 dtl->buf = kmalloc_node(dtl->buf_entries * sizeof(struct dtl_entry), 86 GFP_KERNEL, cpu_to_node(dtl->cpu)); 87 if (!dtl->buf) { 88 printk(KERN_WARNING "%s: buffer alloc failed for cpu %d\n", 89 __func__, dtl->cpu); 90 return -ENOMEM; 91 } 92 93 /* Register our dtl buffer with the hypervisor. The HV expects the 94 * buffer size to be passed in the second word of the buffer */ 95 ((u32 *)dtl->buf)[1] = dtl->buf_entries * sizeof(struct dtl_entry); 96 97 hwcpu = get_hard_smp_processor_id(dtl->cpu); 98 addr = __pa(dtl->buf); 99 ret = register_dtl(hwcpu, addr); 100 if (ret) { 101 printk(KERN_WARNING "%s: DTL registration for cpu %d (hw %d) " 102 "failed with %d\n", __func__, dtl->cpu, hwcpu, ret); 103 kfree(dtl->buf); 104 return -EIO; 105 } 106 107 /* set our initial buffer indices */ 108 dtl->last_idx = lppaca[dtl->cpu].dtl_idx = 0; 109 110 /* ensure that our updates to the lppaca fields have occurred before 111 * we actually enable the logging */ 112 smp_wmb(); 113 114 /* enable event logging */ 115 lppaca[dtl->cpu].dtl_enable_mask = dtl_event_mask; 116 117 return 0; 118 } 119 120 static void dtl_disable(struct dtl *dtl) 121 { 122 int hwcpu = get_hard_smp_processor_id(dtl->cpu); 123 124 lppaca[dtl->cpu].dtl_enable_mask = 0x0; 125 126 unregister_dtl(hwcpu, __pa(dtl->buf)); 127 128 kfree(dtl->buf); 129 dtl->buf = NULL; 130 dtl->buf_entries = 0; 131 } 132 133 /* file interface */ 134 135 static int dtl_file_open(struct inode *inode, struct file *filp) 136 { 137 struct dtl *dtl = inode->i_private; 138 int rc; 139 140 rc = dtl_enable(dtl); 141 if (rc) 142 return rc; 143 144 filp->private_data = dtl; 145 return 0; 146 } 147 148 static int dtl_file_release(struct inode *inode, struct file *filp) 149 { 150 struct dtl *dtl = inode->i_private; 151 dtl_disable(dtl); 152 return 0; 153 } 154 155 static ssize_t dtl_file_read(struct file *filp, char __user *buf, size_t len, 156 loff_t *pos) 157 { 158 int rc, cur_idx, last_idx, n_read, n_req, read_size; 159 struct dtl *dtl; 160 161 if ((len % sizeof(struct dtl_entry)) != 0) 162 return -EINVAL; 163 164 dtl = filp->private_data; 165 166 /* requested number of entries to read */ 167 n_req = len / sizeof(struct dtl_entry); 168 169 /* actual number of entries read */ 170 n_read = 0; 171 172 cur_idx = lppaca[dtl->cpu].dtl_idx; 173 last_idx = dtl->last_idx; 174 175 if (cur_idx - last_idx > dtl->buf_entries) { 176 pr_debug("%s: hv buffer overflow for cpu %d, samples lost\n", 177 __func__, dtl->cpu); 178 } 179 180 cur_idx %= dtl->buf_entries; 181 last_idx %= dtl->buf_entries; 182 183 /* read the tail of the buffer if we've wrapped */ 184 if (last_idx > cur_idx) { 185 read_size = min(n_req, dtl->buf_entries - last_idx); 186 187 rc = copy_to_user(buf, &dtl->buf[last_idx], 188 read_size * sizeof(struct dtl_entry)); 189 if (rc) 190 return -EFAULT; 191 192 last_idx = 0; 193 n_req -= read_size; 194 n_read += read_size; 195 buf += read_size * sizeof(struct dtl_entry); 196 } 197 198 /* .. and now the head */ 199 read_size = min(n_req, cur_idx - last_idx); 200 rc = copy_to_user(buf, &dtl->buf[last_idx], 201 read_size * sizeof(struct dtl_entry)); 202 if (rc) 203 return -EFAULT; 204 205 n_read += read_size; 206 dtl->last_idx += n_read; 207 208 return n_read * sizeof(struct dtl_entry); 209 } 210 211 static struct file_operations dtl_fops = { 212 .open = dtl_file_open, 213 .release = dtl_file_release, 214 .read = dtl_file_read, 215 .llseek = no_llseek, 216 }; 217 218 static struct dentry *dtl_dir; 219 220 static int dtl_setup_file(struct dtl *dtl) 221 { 222 char name[10]; 223 224 sprintf(name, "cpu-%d", dtl->cpu); 225 226 dtl->file = debugfs_create_file(name, 0400, dtl_dir, dtl, &dtl_fops); 227 if (!dtl->file) 228 return -ENOMEM; 229 230 return 0; 231 } 232 233 static int dtl_init(void) 234 { 235 struct dentry *event_mask_file, *buf_entries_file; 236 int rc, i; 237 238 if (!firmware_has_feature(FW_FEATURE_SPLPAR)) 239 return -ENODEV; 240 241 /* set up common debugfs structure */ 242 243 rc = -ENOMEM; 244 dtl_dir = debugfs_create_dir("dtl", powerpc_debugfs_root); 245 if (!dtl_dir) { 246 printk(KERN_WARNING "%s: can't create dtl root dir\n", 247 __func__); 248 goto err; 249 } 250 251 event_mask_file = debugfs_create_x8("dtl_event_mask", 0600, 252 dtl_dir, &dtl_event_mask); 253 buf_entries_file = debugfs_create_u32("dtl_buf_entries", 0600, 254 dtl_dir, &dtl_buf_entries); 255 256 if (!event_mask_file || !buf_entries_file) { 257 printk(KERN_WARNING "%s: can't create dtl files\n", __func__); 258 goto err_remove_dir; 259 } 260 261 /* set up the per-cpu log structures */ 262 for_each_possible_cpu(i) { 263 struct dtl *dtl = &per_cpu(dtl, i); 264 dtl->cpu = i; 265 266 rc = dtl_setup_file(dtl); 267 if (rc) 268 goto err_remove_dir; 269 } 270 271 return 0; 272 273 err_remove_dir: 274 debugfs_remove_recursive(dtl_dir); 275 err: 276 return rc; 277 } 278 arch_initcall(dtl_init); 279