Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame^] | 1 | /* Copyright (c) 2002,2008-2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/debugfs.h> |
| 16 | |
| 17 | #include "kgsl.h" |
| 18 | #include "kgsl_device.h" |
| 19 | #include "kgsl_sharedmem.h" |
| 20 | #include "kgsl_debugfs.h" |
| 21 | |
| 22 | /*default log levels is error for everything*/ |
| 23 | #define KGSL_LOG_LEVEL_MAX 7 |
| 24 | |
| 25 | struct dentry *kgsl_debugfs_dir; |
| 26 | static struct dentry *proc_d_debugfs; |
| 27 | |
| 28 | static inline int kgsl_log_set(unsigned int *log_val, void *data, u64 val) |
| 29 | { |
| 30 | *log_val = min_t(unsigned int, val, KGSL_LOG_LEVEL_MAX); |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | #define KGSL_DEBUGFS_LOG(__log) \ |
| 35 | static int __log ## _set(void *data, u64 val) \ |
| 36 | { \ |
| 37 | struct kgsl_device *device = data; \ |
| 38 | return kgsl_log_set(&device->__log, data, val); \ |
| 39 | } \ |
| 40 | static int __log ## _get(void *data, u64 *val) \ |
| 41 | { \ |
| 42 | struct kgsl_device *device = data; \ |
| 43 | *val = device->__log; \ |
| 44 | return 0; \ |
| 45 | } \ |
| 46 | DEFINE_SIMPLE_ATTRIBUTE(__log ## _fops, \ |
| 47 | __log ## _get, __log ## _set, "%llu\n") \ |
| 48 | |
| 49 | KGSL_DEBUGFS_LOG(drv_log); |
| 50 | KGSL_DEBUGFS_LOG(cmd_log); |
| 51 | KGSL_DEBUGFS_LOG(ctxt_log); |
| 52 | KGSL_DEBUGFS_LOG(mem_log); |
| 53 | KGSL_DEBUGFS_LOG(pwr_log); |
| 54 | |
| 55 | static int _strict_set(void *data, u64 val) |
| 56 | { |
| 57 | kgsl_sharedmem_set_noretry(val ? true : false); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int _strict_get(void *data, u64 *val) |
| 62 | { |
| 63 | *val = kgsl_sharedmem_get_noretry(); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | DEFINE_SIMPLE_ATTRIBUTE(_strict_fops, _strict_get, _strict_set, "%llu\n"); |
| 68 | |
| 69 | void kgsl_device_debugfs_init(struct kgsl_device *device) |
| 70 | { |
| 71 | if (kgsl_debugfs_dir && !IS_ERR(kgsl_debugfs_dir)) |
| 72 | device->d_debugfs = debugfs_create_dir(device->name, |
| 73 | kgsl_debugfs_dir); |
| 74 | |
| 75 | if (!device->d_debugfs || IS_ERR(device->d_debugfs)) |
| 76 | return; |
| 77 | |
| 78 | debugfs_create_file("log_level_cmd", 0644, device->d_debugfs, device, |
| 79 | &cmd_log_fops); |
| 80 | debugfs_create_file("log_level_ctxt", 0644, device->d_debugfs, device, |
| 81 | &ctxt_log_fops); |
| 82 | debugfs_create_file("log_level_drv", 0644, device->d_debugfs, device, |
| 83 | &drv_log_fops); |
| 84 | debugfs_create_file("log_level_mem", 0644, device->d_debugfs, device, |
| 85 | &mem_log_fops); |
| 86 | debugfs_create_file("log_level_pwr", 0644, device->d_debugfs, device, |
| 87 | &pwr_log_fops); |
| 88 | } |
| 89 | |
| 90 | struct type_entry { |
| 91 | int type; |
| 92 | const char *str; |
| 93 | }; |
| 94 | |
| 95 | static const struct type_entry memtypes[] = { KGSL_MEM_TYPES }; |
| 96 | |
| 97 | static const char *memtype_str(int memtype) |
| 98 | { |
| 99 | int i; |
| 100 | |
| 101 | for (i = 0; i < ARRAY_SIZE(memtypes); i++) |
| 102 | if (memtypes[i].type == memtype) |
| 103 | return memtypes[i].str; |
| 104 | return "unknown"; |
| 105 | } |
| 106 | |
| 107 | static char get_alignflag(const struct kgsl_memdesc *m) |
| 108 | { |
| 109 | int align = kgsl_memdesc_get_align(m); |
| 110 | |
| 111 | if (align >= ilog2(SZ_1M)) |
| 112 | return 'L'; |
| 113 | else if (align >= ilog2(SZ_64K)) |
| 114 | return 'l'; |
| 115 | return '-'; |
| 116 | } |
| 117 | |
| 118 | static char get_cacheflag(const struct kgsl_memdesc *m) |
| 119 | { |
| 120 | static const char table[] = { |
| 121 | [KGSL_CACHEMODE_WRITECOMBINE] = '-', |
| 122 | [KGSL_CACHEMODE_UNCACHED] = 'u', |
| 123 | [KGSL_CACHEMODE_WRITEBACK] = 'b', |
| 124 | [KGSL_CACHEMODE_WRITETHROUGH] = 't', |
| 125 | }; |
| 126 | |
| 127 | return table[kgsl_memdesc_get_cachemode(m)]; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static int print_mem_entry(void *data, void *ptr) |
| 132 | { |
| 133 | struct seq_file *s = data; |
| 134 | struct kgsl_mem_entry *entry = ptr; |
| 135 | char flags[10]; |
| 136 | char usage[16]; |
| 137 | struct kgsl_memdesc *m = &entry->memdesc; |
| 138 | unsigned int usermem_type = kgsl_memdesc_usermem_type(m); |
| 139 | int egl_surface_count = 0, egl_image_count = 0; |
| 140 | |
| 141 | if (m->flags & KGSL_MEMFLAGS_SPARSE_VIRT) |
| 142 | return 0; |
| 143 | |
| 144 | flags[0] = kgsl_memdesc_is_global(m) ? 'g' : '-'; |
| 145 | flags[1] = '-'; |
| 146 | flags[2] = !(m->flags & KGSL_MEMFLAGS_GPUREADONLY) ? 'w' : '-'; |
| 147 | flags[3] = get_alignflag(m); |
| 148 | flags[4] = get_cacheflag(m); |
| 149 | flags[5] = kgsl_memdesc_use_cpu_map(m) ? 'p' : '-'; |
| 150 | flags[6] = (m->useraddr) ? 'Y' : 'N'; |
| 151 | flags[7] = kgsl_memdesc_is_secured(m) ? 's' : '-'; |
| 152 | flags[8] = m->flags & KGSL_MEMFLAGS_SPARSE_PHYS ? 'P' : '-'; |
| 153 | flags[9] = '\0'; |
| 154 | |
| 155 | kgsl_get_memory_usage(usage, sizeof(usage), m->flags); |
| 156 | |
| 157 | if (usermem_type == KGSL_MEM_ENTRY_ION) |
| 158 | kgsl_get_egl_counts(entry, &egl_surface_count, |
| 159 | &egl_image_count); |
| 160 | |
| 161 | seq_printf(s, "%pK %pK %16llu %5d %9s %10s %16s %5d %16llu %6d %6d", |
| 162 | (uint64_t *)(uintptr_t) m->gpuaddr, |
| 163 | (unsigned long *) m->useraddr, |
| 164 | m->size, entry->id, flags, |
| 165 | memtype_str(usermem_type), |
| 166 | usage, (m->sgt ? m->sgt->nents : 0), m->mapsize, |
| 167 | egl_surface_count, egl_image_count); |
| 168 | |
| 169 | if (entry->metadata[0] != 0) |
| 170 | seq_printf(s, " %s", entry->metadata); |
| 171 | |
| 172 | seq_putc(s, '\n'); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static struct kgsl_mem_entry *process_mem_seq_find(struct seq_file *s, |
| 178 | void *ptr, loff_t pos) |
| 179 | { |
| 180 | struct kgsl_mem_entry *entry = ptr; |
| 181 | struct kgsl_process_private *private = s->private; |
| 182 | int id = 0; |
| 183 | |
| 184 | loff_t temp_pos = 1; |
| 185 | |
| 186 | if (entry != SEQ_START_TOKEN) |
| 187 | id = entry->id + 1; |
| 188 | |
| 189 | spin_lock(&private->mem_lock); |
| 190 | for (entry = idr_get_next(&private->mem_idr, &id); entry; |
| 191 | id++, entry = idr_get_next(&private->mem_idr, &id), |
| 192 | temp_pos++) { |
| 193 | if (temp_pos == pos && kgsl_mem_entry_get(entry)) { |
| 194 | spin_unlock(&private->mem_lock); |
| 195 | goto found; |
| 196 | } |
| 197 | } |
| 198 | spin_unlock(&private->mem_lock); |
| 199 | |
| 200 | entry = NULL; |
| 201 | found: |
| 202 | if (ptr != SEQ_START_TOKEN) |
| 203 | kgsl_mem_entry_put(ptr); |
| 204 | |
| 205 | return entry; |
| 206 | } |
| 207 | |
| 208 | static void *process_mem_seq_start(struct seq_file *s, loff_t *pos) |
| 209 | { |
| 210 | loff_t seq_file_offset = *pos; |
| 211 | |
| 212 | if (seq_file_offset == 0) |
| 213 | return SEQ_START_TOKEN; |
| 214 | else |
| 215 | return process_mem_seq_find(s, SEQ_START_TOKEN, |
| 216 | seq_file_offset); |
| 217 | } |
| 218 | |
| 219 | static void process_mem_seq_stop(struct seq_file *s, void *ptr) |
| 220 | { |
| 221 | if (ptr && ptr != SEQ_START_TOKEN) |
| 222 | kgsl_mem_entry_put(ptr); |
| 223 | } |
| 224 | |
| 225 | static void *process_mem_seq_next(struct seq_file *s, void *ptr, |
| 226 | loff_t *pos) |
| 227 | { |
| 228 | ++*pos; |
| 229 | return process_mem_seq_find(s, ptr, 1); |
| 230 | } |
| 231 | |
| 232 | static int process_mem_seq_show(struct seq_file *s, void *ptr) |
| 233 | { |
| 234 | if (ptr == SEQ_START_TOKEN) { |
| 235 | seq_printf(s, "%16s %16s %16s %5s %9s %10s %16s %5s %16s %6s %6s\n", |
| 236 | "gpuaddr", "useraddr", "size", "id", "flags", "type", |
| 237 | "usage", "sglen", "mapsize", "eglsrf", "eglimg"); |
| 238 | return 0; |
| 239 | } else |
| 240 | return print_mem_entry(s, ptr); |
| 241 | } |
| 242 | |
| 243 | static const struct seq_operations process_mem_seq_fops = { |
| 244 | .start = process_mem_seq_start, |
| 245 | .stop = process_mem_seq_stop, |
| 246 | .next = process_mem_seq_next, |
| 247 | .show = process_mem_seq_show, |
| 248 | }; |
| 249 | |
| 250 | static int process_mem_open(struct inode *inode, struct file *file) |
| 251 | { |
| 252 | int ret; |
| 253 | pid_t pid = (pid_t) (unsigned long) inode->i_private; |
| 254 | struct seq_file *s = NULL; |
| 255 | struct kgsl_process_private *private = NULL; |
| 256 | |
| 257 | private = kgsl_process_private_find(pid); |
| 258 | |
| 259 | if (!private) |
| 260 | return -ENODEV; |
| 261 | |
| 262 | ret = seq_open(file, &process_mem_seq_fops); |
| 263 | if (ret) |
| 264 | kgsl_process_private_put(private); |
| 265 | else { |
| 266 | s = file->private_data; |
| 267 | s->private = private; |
| 268 | } |
| 269 | |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | static int process_mem_release(struct inode *inode, struct file *file) |
| 274 | { |
| 275 | struct kgsl_process_private *private = |
| 276 | ((struct seq_file *)file->private_data)->private; |
| 277 | |
| 278 | if (private) |
| 279 | kgsl_process_private_put(private); |
| 280 | |
| 281 | return seq_release(inode, file); |
| 282 | } |
| 283 | |
| 284 | static const struct file_operations process_mem_fops = { |
| 285 | .open = process_mem_open, |
| 286 | .read = seq_read, |
| 287 | .llseek = seq_lseek, |
| 288 | .release = process_mem_release, |
| 289 | }; |
| 290 | |
| 291 | static int print_sparse_mem_entry(int id, void *ptr, void *data) |
| 292 | { |
| 293 | struct seq_file *s = data; |
| 294 | struct kgsl_mem_entry *entry = ptr; |
| 295 | struct kgsl_memdesc *m = &entry->memdesc; |
| 296 | struct rb_node *node; |
| 297 | |
| 298 | if (!(m->flags & KGSL_MEMFLAGS_SPARSE_VIRT)) |
| 299 | return 0; |
| 300 | |
| 301 | node = rb_first(&entry->bind_tree); |
| 302 | |
| 303 | while (node != NULL) { |
| 304 | struct sparse_bind_object *obj = rb_entry(node, |
| 305 | struct sparse_bind_object, node); |
| 306 | seq_printf(s, "%5d %16llx %16llx %16llx %16llx\n", |
| 307 | entry->id, entry->memdesc.gpuaddr, |
| 308 | obj->v_off, obj->size, obj->p_off); |
| 309 | node = rb_next(node); |
| 310 | } |
| 311 | |
| 312 | seq_putc(s, '\n'); |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int process_sparse_mem_print(struct seq_file *s, void *unused) |
| 318 | { |
| 319 | struct kgsl_process_private *private = s->private; |
| 320 | |
| 321 | seq_printf(s, "%5s %16s %16s %16s %16s\n", |
| 322 | "v_id", "gpuaddr", "v_offset", "v_size", "p_offset"); |
| 323 | |
| 324 | spin_lock(&private->mem_lock); |
| 325 | idr_for_each(&private->mem_idr, print_sparse_mem_entry, s); |
| 326 | spin_unlock(&private->mem_lock); |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static int process_sparse_mem_open(struct inode *inode, struct file *file) |
| 332 | { |
| 333 | int ret; |
| 334 | pid_t pid = (pid_t) (unsigned long) inode->i_private; |
| 335 | struct kgsl_process_private *private = NULL; |
| 336 | |
| 337 | private = kgsl_process_private_find(pid); |
| 338 | |
| 339 | if (!private) |
| 340 | return -ENODEV; |
| 341 | |
| 342 | ret = single_open(file, process_sparse_mem_print, private); |
| 343 | if (ret) |
| 344 | kgsl_process_private_put(private); |
| 345 | |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | static const struct file_operations process_sparse_mem_fops = { |
| 350 | .open = process_sparse_mem_open, |
| 351 | .read = seq_read, |
| 352 | .llseek = seq_lseek, |
| 353 | .release = process_mem_release, |
| 354 | }; |
| 355 | |
| 356 | static int globals_print(struct seq_file *s, void *unused) |
| 357 | { |
| 358 | kgsl_print_global_pt_entries(s); |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static int globals_open(struct inode *inode, struct file *file) |
| 363 | { |
| 364 | return single_open(file, globals_print, NULL); |
| 365 | } |
| 366 | |
| 367 | static int globals_release(struct inode *inode, struct file *file) |
| 368 | { |
| 369 | return single_release(inode, file); |
| 370 | } |
| 371 | |
| 372 | static const struct file_operations global_fops = { |
| 373 | .open = globals_open, |
| 374 | .read = seq_read, |
| 375 | .llseek = seq_lseek, |
| 376 | .release = globals_release, |
| 377 | }; |
| 378 | |
| 379 | /** |
| 380 | * kgsl_process_init_debugfs() - Initialize debugfs for a process |
| 381 | * @private: Pointer to process private structure created for the process |
| 382 | * |
| 383 | * kgsl_process_init_debugfs() is called at the time of creating the |
| 384 | * process struct when a process opens kgsl device for the first time. |
| 385 | * This function is not fatal - all we do is print a warning message if |
| 386 | * the files can't be created |
| 387 | */ |
| 388 | void kgsl_process_init_debugfs(struct kgsl_process_private *private) |
| 389 | { |
| 390 | unsigned char name[16]; |
| 391 | struct dentry *dentry; |
| 392 | |
| 393 | snprintf(name, sizeof(name), "%d", private->pid); |
| 394 | |
| 395 | private->debug_root = debugfs_create_dir(name, proc_d_debugfs); |
| 396 | |
| 397 | /* |
| 398 | * Both debugfs_create_dir() and debugfs_create_file() return |
| 399 | * ERR_PTR(-ENODEV) if debugfs is disabled in the kernel but return |
| 400 | * NULL on error when it is enabled. For both usages we need to check |
| 401 | * for ERROR or NULL and only print a warning on an actual failure |
| 402 | * (i.e. - when the return value is NULL) |
| 403 | */ |
| 404 | |
| 405 | if (IS_ERR_OR_NULL(private->debug_root)) { |
| 406 | WARN((private->debug_root == NULL), |
| 407 | "Unable to create debugfs dir for %s\n", name); |
| 408 | private->debug_root = NULL; |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | dentry = debugfs_create_file("mem", 0444, private->debug_root, |
| 413 | (void *) ((unsigned long) private->pid), &process_mem_fops); |
| 414 | |
| 415 | if (IS_ERR_OR_NULL(dentry)) |
| 416 | WARN((dentry == NULL), |
| 417 | "Unable to create 'mem' file for %s\n", name); |
| 418 | |
| 419 | dentry = debugfs_create_file("sparse_mem", 0444, private->debug_root, |
| 420 | (void *) ((unsigned long) private->pid), |
| 421 | &process_sparse_mem_fops); |
| 422 | |
| 423 | if (IS_ERR_OR_NULL(dentry)) |
| 424 | WARN((dentry == NULL), |
| 425 | "Unable to create 'sparse_mem' file for %s\n", name); |
| 426 | |
| 427 | } |
| 428 | |
| 429 | void kgsl_core_debugfs_init(void) |
| 430 | { |
| 431 | struct dentry *debug_dir; |
| 432 | |
| 433 | kgsl_debugfs_dir = debugfs_create_dir("kgsl", NULL); |
| 434 | |
| 435 | debugfs_create_file("globals", 0444, kgsl_debugfs_dir, NULL, |
| 436 | &global_fops); |
| 437 | |
| 438 | debug_dir = debugfs_create_dir("debug", kgsl_debugfs_dir); |
| 439 | |
| 440 | debugfs_create_file("strict_memory", 0644, debug_dir, NULL, |
| 441 | &_strict_fops); |
| 442 | |
| 443 | proc_d_debugfs = debugfs_create_dir("proc", kgsl_debugfs_dir); |
| 444 | } |
| 445 | |
| 446 | void kgsl_core_debugfs_close(void) |
| 447 | { |
| 448 | debugfs_remove_recursive(kgsl_debugfs_dir); |
| 449 | } |