Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012-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 | #include <linux/export.h> |
| 14 | #include <linux/time.h> |
| 15 | #include <linux/sysfs.h> |
| 16 | #include <linux/utsname.h> |
| 17 | #include <linux/sched.h> |
| 18 | #include <linux/idr.h> |
| 19 | |
| 20 | #include "kgsl.h" |
| 21 | #include "kgsl_log.h" |
| 22 | #include "kgsl_device.h" |
| 23 | #include "kgsl_sharedmem.h" |
| 24 | #include "kgsl_snapshot.h" |
| 25 | #include "adreno_cp_parser.h" |
| 26 | |
Carter Cooper | b88b708 | 2017-09-14 09:03:26 -0600 | [diff] [blame^] | 27 | static void kgsl_snapshot_save_frozen_objs(struct work_struct *work); |
| 28 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 29 | /* Placeholder for list of ib objects that contain all objects in that IB */ |
| 30 | |
| 31 | struct kgsl_snapshot_cp_obj { |
| 32 | struct adreno_ib_object_list *ib_obj_list; |
| 33 | struct list_head node; |
| 34 | }; |
| 35 | |
| 36 | struct snapshot_obj_itr { |
| 37 | u8 *buf; /* Buffer pointer to write to */ |
| 38 | int pos; /* Current position in the sequence */ |
| 39 | loff_t offset; /* file offset to start writing from */ |
| 40 | size_t remain; /* Bytes remaining in buffer */ |
| 41 | size_t write; /* Bytes written so far */ |
| 42 | }; |
| 43 | |
| 44 | static void obj_itr_init(struct snapshot_obj_itr *itr, u8 *buf, |
| 45 | loff_t offset, size_t remain) |
| 46 | { |
| 47 | itr->buf = buf; |
| 48 | itr->offset = offset; |
| 49 | itr->remain = remain; |
| 50 | itr->pos = 0; |
| 51 | itr->write = 0; |
| 52 | } |
| 53 | |
| 54 | static int obj_itr_out(struct snapshot_obj_itr *itr, void *src, int size) |
| 55 | { |
| 56 | if (itr->remain == 0) |
| 57 | return 0; |
| 58 | |
| 59 | if ((itr->pos + size) <= itr->offset) |
| 60 | goto done; |
| 61 | |
| 62 | /* Handle the case that offset is in the middle of the buffer */ |
| 63 | |
| 64 | if (itr->offset > itr->pos) { |
| 65 | src += (itr->offset - itr->pos); |
| 66 | size -= (itr->offset - itr->pos); |
| 67 | |
| 68 | /* Advance pos to the offset start */ |
| 69 | itr->pos = itr->offset; |
| 70 | } |
| 71 | |
| 72 | if (size > itr->remain) |
| 73 | size = itr->remain; |
| 74 | |
| 75 | memcpy(itr->buf, src, size); |
| 76 | |
| 77 | itr->buf += size; |
| 78 | itr->write += size; |
| 79 | itr->remain -= size; |
| 80 | |
| 81 | done: |
| 82 | itr->pos += size; |
| 83 | return size; |
| 84 | } |
| 85 | |
| 86 | /* idr_for_each function to count the number of contexts */ |
| 87 | |
| 88 | static int snapshot_context_count(int id, void *ptr, void *data) |
| 89 | { |
| 90 | int *count = data; |
| 91 | *count = *count + 1; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * To simplify the iterator loop use a global pointer instead of trying |
| 98 | * to pass around double star references to the snapshot data |
| 99 | */ |
| 100 | |
| 101 | static u8 *_ctxtptr; |
| 102 | |
| 103 | static int snapshot_context_info(int id, void *ptr, void *data) |
| 104 | { |
| 105 | struct kgsl_snapshot_linux_context_v2 *header = |
| 106 | (struct kgsl_snapshot_linux_context_v2 *)_ctxtptr; |
| 107 | struct kgsl_context *context = ptr; |
| 108 | struct kgsl_device *device; |
| 109 | |
| 110 | device = context->device; |
| 111 | |
| 112 | header->id = id; |
| 113 | |
| 114 | /* Future-proof for per-context timestamps - for now, just |
| 115 | * return the global timestamp for all contexts |
| 116 | */ |
| 117 | |
| 118 | kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_QUEUED, |
| 119 | &header->timestamp_queued); |
| 120 | kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_CONSUMED, |
| 121 | &header->timestamp_consumed); |
| 122 | kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED, |
| 123 | &header->timestamp_retired); |
| 124 | |
| 125 | _ctxtptr += sizeof(struct kgsl_snapshot_linux_context_v2); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /* Snapshot the Linux specific information */ |
| 131 | static size_t snapshot_os(struct kgsl_device *device, |
| 132 | u8 *buf, size_t remain, void *priv) |
| 133 | { |
| 134 | struct kgsl_snapshot_linux_v2 *header = |
| 135 | (struct kgsl_snapshot_linux_v2 *)buf; |
| 136 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 137 | int ctxtcount = 0; |
| 138 | size_t size = sizeof(*header); |
| 139 | struct kgsl_context *context; |
| 140 | |
| 141 | /* |
| 142 | * Figure out how many active contexts there are - these will |
| 143 | * be appended on the end of the structure |
| 144 | */ |
| 145 | |
| 146 | read_lock(&device->context_lock); |
| 147 | idr_for_each(&device->context_idr, snapshot_context_count, &ctxtcount); |
| 148 | read_unlock(&device->context_lock); |
| 149 | |
| 150 | size += ctxtcount * sizeof(struct kgsl_snapshot_linux_context_v2); |
| 151 | |
| 152 | /* Make sure there is enough room for the data */ |
| 153 | if (remain < size) { |
| 154 | SNAPSHOT_ERR_NOMEM(device, "OS"); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | memset(header, 0, sizeof(*header)); |
| 159 | |
| 160 | header->osid = KGSL_SNAPSHOT_OS_LINUX_V3; |
| 161 | |
| 162 | /* Get the kernel build information */ |
Deepak Kumar | 86bc884 | 2017-02-23 17:49:37 +0530 | [diff] [blame] | 163 | strlcpy(header->release, init_utsname()->release, |
| 164 | sizeof(header->release)); |
| 165 | strlcpy(header->version, init_utsname()->version, |
| 166 | sizeof(header->version)); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 167 | |
| 168 | /* Get the Unix time for the timestamp */ |
| 169 | header->seconds = get_seconds(); |
| 170 | |
| 171 | /* Remember the power information */ |
| 172 | header->power_flags = pwr->power_flags; |
| 173 | header->power_level = pwr->active_pwrlevel; |
| 174 | header->power_interval_timeout = pwr->interval_timeout; |
| 175 | header->grpclk = kgsl_get_clkrate(pwr->grp_clks[0]); |
| 176 | |
| 177 | /* |
| 178 | * Save the last active context from global index since its more |
| 179 | * reliable than currrent RB index |
| 180 | */ |
| 181 | kgsl_sharedmem_readl(&device->memstore, &header->current_context, |
| 182 | KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL, current_context)); |
| 183 | |
| 184 | context = kgsl_context_get(device, header->current_context); |
| 185 | |
| 186 | /* Get the current PT base */ |
Carter Cooper | b88b708 | 2017-09-14 09:03:26 -0600 | [diff] [blame^] | 187 | header->ptbase = kgsl_mmu_get_current_ttbr0(&device->mmu); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 188 | |
| 189 | /* And the PID for the task leader */ |
| 190 | if (context) { |
| 191 | header->pid = context->tid; |
| 192 | strlcpy(header->comm, context->proc_priv->comm, |
| 193 | sizeof(header->comm)); |
| 194 | kgsl_context_put(context); |
| 195 | context = NULL; |
| 196 | } |
| 197 | |
| 198 | header->ctxtcount = ctxtcount; |
| 199 | |
| 200 | _ctxtptr = buf + sizeof(*header); |
| 201 | /* append information for each context */ |
| 202 | |
| 203 | read_lock(&device->context_lock); |
| 204 | idr_for_each(&device->context_idr, snapshot_context_info, NULL); |
| 205 | read_unlock(&device->context_lock); |
| 206 | |
| 207 | /* Return the size of the data segment */ |
| 208 | return size; |
| 209 | } |
| 210 | |
Carter Cooper | b88b708 | 2017-09-14 09:03:26 -0600 | [diff] [blame^] | 211 | /* Snapshot the Linux specific information */ |
| 212 | static size_t snapshot_os_no_ctxt(struct kgsl_device *device, |
| 213 | u8 *buf, size_t remain, void *priv) |
| 214 | { |
| 215 | struct kgsl_snapshot_linux_v2 *header = |
| 216 | (struct kgsl_snapshot_linux_v2 *)buf; |
| 217 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 218 | size_t size = sizeof(*header); |
| 219 | |
| 220 | /* Make sure there is enough room for the data */ |
| 221 | if (remain < size) { |
| 222 | SNAPSHOT_ERR_NOMEM(device, "OS"); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | memset(header, 0, sizeof(*header)); |
| 227 | |
| 228 | header->osid = KGSL_SNAPSHOT_OS_LINUX_V3; |
| 229 | |
| 230 | /* Get the kernel build information */ |
| 231 | strlcpy(header->release, init_utsname()->release, |
| 232 | sizeof(header->release)); |
| 233 | strlcpy(header->version, init_utsname()->version, |
| 234 | sizeof(header->version)); |
| 235 | |
| 236 | /* Get the Unix time for the timestamp */ |
| 237 | header->seconds = get_seconds(); |
| 238 | |
| 239 | /* Remember the power information */ |
| 240 | header->power_flags = pwr->power_flags; |
| 241 | header->power_level = pwr->active_pwrlevel; |
| 242 | header->power_interval_timeout = pwr->interval_timeout; |
| 243 | header->grpclk = kgsl_get_clkrate(pwr->grp_clks[0]); |
| 244 | |
| 245 | /* Return the size of the data segment */ |
| 246 | return size; |
| 247 | } |
| 248 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 249 | static void kgsl_snapshot_put_object(struct kgsl_snapshot_object *obj) |
| 250 | { |
| 251 | list_del(&obj->node); |
| 252 | |
| 253 | obj->entry->memdesc.priv &= ~KGSL_MEMDESC_FROZEN; |
| 254 | kgsl_mem_entry_put(obj->entry); |
| 255 | |
| 256 | kfree(obj); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * kgsl_snapshot_have_object() - return 1 if the object has been processed |
| 261 | * @snapshot: the snapshot data |
| 262 | * @process: The process that owns the the object to freeze |
| 263 | * @gpuaddr: The gpu address of the object to freeze |
| 264 | * @size: the size of the object (may not always be the size of the region) |
| 265 | * |
| 266 | * Return 1 if the object is already in the list - this can save us from |
| 267 | * having to parse the same thing over again. There are 2 lists that are |
| 268 | * tracking objects so check for the object in both lists |
| 269 | */ |
| 270 | int kgsl_snapshot_have_object(struct kgsl_snapshot *snapshot, |
| 271 | struct kgsl_process_private *process, |
| 272 | uint64_t gpuaddr, uint64_t size) |
| 273 | { |
| 274 | struct kgsl_snapshot_object *obj; |
| 275 | struct kgsl_snapshot_cp_obj *obj_cp; |
| 276 | struct adreno_ib_object *ib_obj; |
| 277 | int i; |
| 278 | |
| 279 | /* Check whether the object is tracked already in ib list */ |
| 280 | list_for_each_entry(obj_cp, &snapshot->cp_list, node) { |
| 281 | if (obj_cp->ib_obj_list == NULL |
| 282 | || obj_cp->ib_obj_list->num_objs == 0) |
| 283 | continue; |
| 284 | |
| 285 | ib_obj = &(obj_cp->ib_obj_list->obj_list[0]); |
| 286 | if (ib_obj->entry == NULL || ib_obj->entry->priv != process) |
| 287 | continue; |
| 288 | |
| 289 | for (i = 0; i < obj_cp->ib_obj_list->num_objs; i++) { |
| 290 | ib_obj = &(obj_cp->ib_obj_list->obj_list[i]); |
| 291 | if ((gpuaddr >= ib_obj->gpuaddr) && |
| 292 | ((gpuaddr + size) <= |
| 293 | (ib_obj->gpuaddr + ib_obj->size))) |
| 294 | return 1; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | list_for_each_entry(obj, &snapshot->obj_list, node) { |
| 299 | if (obj->entry == NULL || obj->entry->priv != process) |
| 300 | continue; |
| 301 | |
| 302 | if ((gpuaddr >= obj->gpuaddr) && |
| 303 | ((gpuaddr + size) <= (obj->gpuaddr + obj->size))) |
| 304 | return 1; |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | EXPORT_SYMBOL(kgsl_snapshot_have_object); |
| 310 | |
| 311 | /** |
| 312 | * kgsl_snapshot_get_object() - Mark a GPU buffer to be frozen |
| 313 | * @snapshot: The snapshot data |
| 314 | * @process: The process that owns the object we want to freeze |
| 315 | * @gpuaddr: The gpu address of the object to freeze |
| 316 | * @size: the size of the object (may not always be the size of the region) |
| 317 | * @type: the type of object being saved (shader, vbo, etc) |
| 318 | * |
| 319 | * Mark and freeze a GPU buffer object. This will prevent it from being |
| 320 | * freed until it can be copied out as part of the snapshot dump. Returns the |
| 321 | * size of the object being frozen |
| 322 | */ |
| 323 | int kgsl_snapshot_get_object(struct kgsl_snapshot *snapshot, |
| 324 | struct kgsl_process_private *process, uint64_t gpuaddr, |
| 325 | uint64_t size, unsigned int type) |
| 326 | { |
| 327 | struct kgsl_mem_entry *entry; |
| 328 | struct kgsl_snapshot_object *obj; |
| 329 | uint64_t offset; |
| 330 | int ret = -EINVAL; |
| 331 | unsigned int mem_type; |
| 332 | |
| 333 | if (!gpuaddr) |
| 334 | return 0; |
| 335 | |
| 336 | entry = kgsl_sharedmem_find(process, gpuaddr); |
| 337 | |
| 338 | if (entry == NULL) { |
| 339 | KGSL_CORE_ERR("Unable to find GPU buffer 0x%016llX\n", gpuaddr); |
| 340 | return -EINVAL; |
| 341 | } |
| 342 | |
| 343 | /* We can't freeze external memory, because we don't own it */ |
| 344 | if (entry->memdesc.flags & KGSL_MEMFLAGS_USERMEM_MASK) |
| 345 | goto err_put; |
| 346 | /* |
| 347 | * Do not save texture and render targets in snapshot, |
| 348 | * they can be just too big |
| 349 | */ |
| 350 | |
| 351 | mem_type = kgsl_memdesc_get_memtype(&entry->memdesc); |
| 352 | if (mem_type == KGSL_MEMTYPE_TEXTURE || |
| 353 | mem_type == KGSL_MEMTYPE_EGL_SURFACE || |
| 354 | mem_type == KGSL_MEMTYPE_EGL_IMAGE) { |
| 355 | ret = 0; |
| 356 | goto err_put; |
| 357 | } |
| 358 | |
| 359 | /* Do not save sparse memory */ |
| 360 | if (entry->memdesc.flags & KGSL_MEMFLAGS_SPARSE_VIRT || |
| 361 | entry->memdesc.flags & KGSL_MEMFLAGS_SPARSE_PHYS) { |
| 362 | ret = 0; |
| 363 | goto err_put; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * size indicates the number of bytes in the region to save. This might |
| 368 | * not always be the entire size of the region because some buffers are |
| 369 | * sub-allocated from a larger region. However, if size 0 was passed |
| 370 | * thats a flag that the caller wants to capture the entire buffer |
| 371 | */ |
| 372 | |
| 373 | if (size == 0) { |
| 374 | size = entry->memdesc.size; |
| 375 | offset = 0; |
| 376 | |
| 377 | /* Adjust the gpuaddr to the start of the object */ |
| 378 | gpuaddr = entry->memdesc.gpuaddr; |
| 379 | } else { |
| 380 | offset = gpuaddr - entry->memdesc.gpuaddr; |
| 381 | } |
| 382 | |
| 383 | if (size + offset > entry->memdesc.size) { |
| 384 | KGSL_CORE_ERR("Invalid size for GPU buffer 0x%016llX\n", |
| 385 | gpuaddr); |
| 386 | goto err_put; |
| 387 | } |
| 388 | |
| 389 | /* If the buffer is already on the list, skip it */ |
| 390 | list_for_each_entry(obj, &snapshot->obj_list, node) { |
| 391 | /* combine the range with existing object if they overlap */ |
| 392 | if (obj->entry->priv == process && obj->type == type && |
| 393 | kgsl_addr_range_overlap(obj->gpuaddr, obj->size, |
| 394 | gpuaddr, size)) { |
| 395 | uint64_t end1 = obj->gpuaddr + obj->size; |
| 396 | uint64_t end2 = gpuaddr + size; |
| 397 | |
| 398 | if (obj->gpuaddr > gpuaddr) |
| 399 | obj->gpuaddr = gpuaddr; |
| 400 | if (end1 > end2) |
| 401 | obj->size = end1 - obj->gpuaddr; |
| 402 | else |
| 403 | obj->size = end2 - obj->gpuaddr; |
| 404 | obj->offset = obj->gpuaddr - entry->memdesc.gpuaddr; |
| 405 | ret = 0; |
| 406 | goto err_put; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | obj = kzalloc(sizeof(*obj), GFP_KERNEL); |
| 411 | |
| 412 | if (obj == NULL) |
| 413 | goto err_put; |
| 414 | |
| 415 | obj->type = type; |
| 416 | obj->entry = entry; |
| 417 | obj->gpuaddr = gpuaddr; |
| 418 | obj->size = size; |
| 419 | obj->offset = offset; |
| 420 | |
| 421 | list_add(&obj->node, &snapshot->obj_list); |
| 422 | |
| 423 | /* |
| 424 | * Return the size of the entire mem entry that was frozen - this gets |
| 425 | * used for tracking how much memory is frozen for a hang. Also, mark |
| 426 | * the memory entry as frozen. If the entry was already marked as |
| 427 | * frozen, then another buffer already got to it. In that case, return |
| 428 | * 0 so it doesn't get counted twice |
| 429 | */ |
| 430 | |
| 431 | ret = (entry->memdesc.priv & KGSL_MEMDESC_FROZEN) ? 0 |
| 432 | : entry->memdesc.size; |
| 433 | |
| 434 | entry->memdesc.priv |= KGSL_MEMDESC_FROZEN; |
| 435 | |
| 436 | return ret; |
| 437 | err_put: |
| 438 | kgsl_mem_entry_put(entry); |
| 439 | return ret; |
| 440 | } |
| 441 | EXPORT_SYMBOL(kgsl_snapshot_get_object); |
| 442 | |
| 443 | /** |
| 444 | * kgsl_snapshot_dump_registers - helper function to dump device registers |
| 445 | * @device - the device to dump registers from |
| 446 | * @snapshot - pointer to the start of the region of memory for the snapshot |
| 447 | * @remain - a pointer to the number of bytes remaining in the snapshot |
| 448 | * @priv - A pointer to the kgsl_snapshot_registers data |
| 449 | * |
| 450 | * Given an array of register ranges pairs (start,end [inclusive]), dump the |
| 451 | * registers into a snapshot register section. The snapshot region stores a |
| 452 | * part of dwords for each register - the word address of the register, and |
| 453 | * the value. |
| 454 | */ |
| 455 | size_t kgsl_snapshot_dump_registers(struct kgsl_device *device, u8 *buf, |
| 456 | size_t remain, void *priv) |
| 457 | { |
| 458 | struct kgsl_snapshot_regs *header = (struct kgsl_snapshot_regs *)buf; |
| 459 | struct kgsl_snapshot_registers *regs = priv; |
| 460 | unsigned int *data = (unsigned int *)(buf + sizeof(*header)); |
| 461 | int count = 0, j, k; |
| 462 | |
| 463 | /* Figure out how many registers we are going to dump */ |
| 464 | |
| 465 | for (j = 0; j < regs->count; j++) { |
| 466 | int start = regs->regs[j * 2]; |
| 467 | int end = regs->regs[j * 2 + 1]; |
| 468 | |
| 469 | count += (end - start + 1); |
| 470 | } |
| 471 | |
| 472 | if (remain < (count * 8) + sizeof(*header)) { |
| 473 | SNAPSHOT_ERR_NOMEM(device, "REGISTERS"); |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | for (j = 0; j < regs->count; j++) { |
| 478 | unsigned int start = regs->regs[j * 2]; |
| 479 | unsigned int end = regs->regs[j * 2 + 1]; |
| 480 | |
| 481 | for (k = start; k <= end; k++) { |
| 482 | unsigned int val; |
| 483 | |
| 484 | kgsl_regread(device, k, &val); |
| 485 | *data++ = k; |
| 486 | *data++ = val; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | header->count = count; |
| 491 | |
| 492 | /* Return the size of the section */ |
| 493 | return (count * 8) + sizeof(*header); |
| 494 | } |
| 495 | EXPORT_SYMBOL(kgsl_snapshot_dump_registers); |
| 496 | |
| 497 | struct kgsl_snapshot_indexed_registers { |
| 498 | unsigned int index; |
| 499 | unsigned int data; |
| 500 | unsigned int start; |
| 501 | unsigned int count; |
| 502 | }; |
| 503 | |
| 504 | static size_t kgsl_snapshot_dump_indexed_regs(struct kgsl_device *device, |
| 505 | u8 *buf, size_t remain, void *priv) |
| 506 | { |
| 507 | struct kgsl_snapshot_indexed_registers *iregs = priv; |
| 508 | struct kgsl_snapshot_indexed_regs *header = |
| 509 | (struct kgsl_snapshot_indexed_regs *)buf; |
| 510 | unsigned int *data = (unsigned int *)(buf + sizeof(*header)); |
| 511 | int i; |
| 512 | |
| 513 | if (remain < (iregs->count * 4) + sizeof(*header)) { |
| 514 | SNAPSHOT_ERR_NOMEM(device, "INDEXED REGS"); |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | header->index_reg = iregs->index; |
| 519 | header->data_reg = iregs->data; |
| 520 | header->count = iregs->count; |
| 521 | header->start = iregs->start; |
| 522 | |
| 523 | for (i = 0; i < iregs->count; i++) { |
| 524 | kgsl_regwrite(device, iregs->index, iregs->start + i); |
| 525 | kgsl_regread(device, iregs->data, &data[i]); |
| 526 | } |
| 527 | |
| 528 | return (iregs->count * 4) + sizeof(*header); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * kgsl_snapshot_indexed_registers - Add a set of indexed registers to the |
| 533 | * snapshot |
| 534 | * @device: Pointer to the KGSL device being snapshotted |
| 535 | * @snapshot: Snapshot instance |
| 536 | * @index: Offset for the index register |
| 537 | * @data: Offset for the data register |
| 538 | * @start: Index to start reading |
| 539 | * @count: Number of entries to read |
| 540 | * |
| 541 | * Dump the values from an indexed register group into the snapshot |
| 542 | */ |
| 543 | void kgsl_snapshot_indexed_registers(struct kgsl_device *device, |
| 544 | struct kgsl_snapshot *snapshot, |
| 545 | unsigned int index, unsigned int data, |
| 546 | unsigned int start, |
| 547 | unsigned int count) |
| 548 | { |
| 549 | struct kgsl_snapshot_indexed_registers iregs; |
| 550 | |
| 551 | iregs.index = index; |
| 552 | iregs.data = data; |
| 553 | iregs.start = start; |
| 554 | iregs.count = count; |
| 555 | |
| 556 | kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_INDEXED_REGS, |
| 557 | snapshot, kgsl_snapshot_dump_indexed_regs, &iregs); |
| 558 | } |
| 559 | EXPORT_SYMBOL(kgsl_snapshot_indexed_registers); |
| 560 | |
| 561 | /** |
| 562 | * kgsl_snapshot_add_section() - Add a new section to the GPU snapshot |
| 563 | * @device: the KGSL device being snapshotted |
| 564 | * @id: the section id |
| 565 | * @snapshot: pointer to the snapshot instance |
| 566 | * @func: Function pointer to fill the section |
| 567 | * @priv: Private pointer to pass to the function |
| 568 | * |
| 569 | * Set up a KGSL snapshot header by filling the memory with the callback |
| 570 | * function and adding the standard section header |
| 571 | */ |
| 572 | void kgsl_snapshot_add_section(struct kgsl_device *device, u16 id, |
| 573 | struct kgsl_snapshot *snapshot, |
| 574 | size_t (*func)(struct kgsl_device *, u8 *, size_t, void *), |
| 575 | void *priv) |
| 576 | { |
| 577 | struct kgsl_snapshot_section_header *header = |
| 578 | (struct kgsl_snapshot_section_header *)snapshot->ptr; |
| 579 | u8 *data = snapshot->ptr + sizeof(*header); |
| 580 | size_t ret = 0; |
| 581 | |
| 582 | /* |
| 583 | * Sanity check to make sure there is enough for the header. The |
| 584 | * callback will check to make sure there is enough for the rest |
| 585 | * of the data. If there isn't enough room then don't advance the |
| 586 | * pointer. |
| 587 | */ |
| 588 | |
| 589 | if (snapshot->remain < sizeof(*header)) |
| 590 | return; |
| 591 | |
| 592 | /* It is legal to have no function (i.e. - make an empty section) */ |
| 593 | if (func) { |
| 594 | ret = func(device, data, snapshot->remain - sizeof(*header), |
| 595 | priv); |
| 596 | |
| 597 | /* |
| 598 | * If there wasn't enough room for the data then don't bother |
| 599 | * setting up the header. |
| 600 | */ |
| 601 | |
| 602 | if (ret == 0) |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | header->magic = SNAPSHOT_SECTION_MAGIC; |
| 607 | header->id = id; |
| 608 | header->size = ret + sizeof(*header); |
| 609 | |
| 610 | snapshot->ptr += header->size; |
| 611 | snapshot->remain -= header->size; |
| 612 | snapshot->size += header->size; |
| 613 | } |
| 614 | |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 615 | static void kgsl_free_snapshot(struct kgsl_snapshot *snapshot) |
| 616 | { |
| 617 | struct kgsl_snapshot_object *obj, *tmp; |
| 618 | |
| 619 | wait_for_completion(&snapshot->dump_gate); |
| 620 | |
| 621 | list_for_each_entry_safe(obj, tmp, |
| 622 | &snapshot->obj_list, node) |
| 623 | kgsl_snapshot_put_object(obj); |
| 624 | |
| 625 | if (snapshot->mempool) |
| 626 | vfree(snapshot->mempool); |
| 627 | |
| 628 | kfree(snapshot); |
| 629 | KGSL_CORE_ERR("snapshot: objects released\n"); |
| 630 | } |
| 631 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 632 | /** |
| 633 | * kgsl_snapshot() - construct a device snapshot |
| 634 | * @device: device to snapshot |
| 635 | * @context: the context that is hung, might be NULL if unknown. |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 636 | * @gmu_fault: whether this snapshot is triggered by a GMU fault. |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 637 | * |
| 638 | * Given a device, construct a binary snapshot dump of the current device state |
| 639 | * and store it in the device snapshot memory. |
| 640 | */ |
| 641 | void kgsl_device_snapshot(struct kgsl_device *device, |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 642 | struct kgsl_context *context, bool gmu_fault) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 643 | { |
| 644 | struct kgsl_snapshot_header *header = device->snapshot_memory.ptr; |
| 645 | struct kgsl_snapshot *snapshot; |
| 646 | struct timespec boot; |
| 647 | phys_addr_t pa; |
| 648 | |
| 649 | if (device->snapshot_memory.ptr == NULL) { |
| 650 | KGSL_DRV_ERR(device, |
| 651 | "snapshot: no snapshot memory available\n"); |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | if (WARN(!kgsl_state_is_awake(device), |
| 656 | "snapshot: device is powered off\n")) |
| 657 | return; |
| 658 | |
| 659 | /* increment the hang count for good book keeping */ |
| 660 | device->snapshot_faultcount++; |
| 661 | |
| 662 | /* |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 663 | * Overwrite a non-GMU fault snapshot if a GMU fault occurs. |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 664 | */ |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 665 | if (device->snapshot != NULL) { |
| 666 | if (!gmu_fault || device->snapshot->gmu_fault) |
| 667 | return; |
| 668 | |
| 669 | /* |
| 670 | * If another thread is currently reading it, that thread |
| 671 | * will free it, otherwise free it now. |
| 672 | */ |
| 673 | if (!device->snapshot->sysfs_read) |
| 674 | kgsl_free_snapshot(device->snapshot); |
| 675 | device->snapshot = NULL; |
| 676 | } |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 677 | |
| 678 | /* Allocate memory for the snapshot instance */ |
| 679 | snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL); |
| 680 | if (snapshot == NULL) |
| 681 | return; |
| 682 | |
| 683 | init_completion(&snapshot->dump_gate); |
| 684 | INIT_LIST_HEAD(&snapshot->obj_list); |
| 685 | INIT_LIST_HEAD(&snapshot->cp_list); |
| 686 | INIT_WORK(&snapshot->work, kgsl_snapshot_save_frozen_objs); |
| 687 | |
| 688 | snapshot->start = device->snapshot_memory.ptr; |
| 689 | snapshot->ptr = device->snapshot_memory.ptr; |
| 690 | snapshot->remain = device->snapshot_memory.size; |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 691 | snapshot->gmu_fault = gmu_fault; |
| 692 | snapshot->first_read = true; |
| 693 | snapshot->sysfs_read = 0; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 694 | |
| 695 | header = (struct kgsl_snapshot_header *) snapshot->ptr; |
| 696 | |
| 697 | header->magic = SNAPSHOT_MAGIC; |
| 698 | header->gpuid = kgsl_gpuid(device, &header->chipid); |
| 699 | |
| 700 | snapshot->ptr += sizeof(*header); |
| 701 | snapshot->remain -= sizeof(*header); |
| 702 | snapshot->size += sizeof(*header); |
| 703 | |
| 704 | /* Build the Linux specific header */ |
Carter Cooper | b88b708 | 2017-09-14 09:03:26 -0600 | [diff] [blame^] | 705 | /* We either want to only dump GMU, or we want to dump GPU and GMU */ |
| 706 | if (gmu_fault) { |
| 707 | /* Dump only the GMU */ |
| 708 | kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_OS, |
| 709 | snapshot, snapshot_os_no_ctxt, NULL); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 710 | |
Carter Cooper | b88b708 | 2017-09-14 09:03:26 -0600 | [diff] [blame^] | 711 | if (device->ftbl->snapshot_gmu) |
| 712 | device->ftbl->snapshot_gmu(device, snapshot); |
| 713 | } else { |
| 714 | /* Dump GPU and GMU */ |
| 715 | kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_OS, |
| 716 | snapshot, snapshot_os, NULL); |
| 717 | |
| 718 | if (device->ftbl->snapshot) |
| 719 | device->ftbl->snapshot(device, snapshot, context); |
| 720 | if (device->ftbl->snapshot_gmu) |
| 721 | device->ftbl->snapshot_gmu(device, snapshot); |
| 722 | } |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 723 | |
| 724 | /* |
| 725 | * The timestamp is the seconds since boot so it is easier to match to |
| 726 | * the kernel log |
| 727 | */ |
| 728 | |
| 729 | getboottime(&boot); |
| 730 | snapshot->timestamp = get_seconds() - boot.tv_sec; |
| 731 | |
| 732 | /* Store the instance in the device until it gets dumped */ |
| 733 | device->snapshot = snapshot; |
| 734 | |
| 735 | /* log buffer info to aid in ramdump fault tolerance */ |
| 736 | pa = __pa(device->snapshot_memory.ptr); |
| 737 | KGSL_DRV_ERR(device, "snapshot created at pa %pa size %zd\n", |
| 738 | &pa, snapshot->size); |
| 739 | |
| 740 | sysfs_notify(&device->snapshot_kobj, NULL, "timestamp"); |
| 741 | |
| 742 | /* |
| 743 | * Queue a work item that will save the IB data in snapshot into |
| 744 | * static memory to prevent loss of data due to overwriting of |
| 745 | * memory. |
| 746 | * |
| 747 | */ |
| 748 | kgsl_schedule_work(&snapshot->work); |
| 749 | } |
| 750 | EXPORT_SYMBOL(kgsl_device_snapshot); |
| 751 | |
| 752 | /* An attribute for showing snapshot details */ |
| 753 | struct kgsl_snapshot_attribute { |
| 754 | struct attribute attr; |
| 755 | ssize_t (*show)(struct kgsl_device *device, char *buf); |
| 756 | ssize_t (*store)(struct kgsl_device *device, const char *buf, |
| 757 | size_t count); |
| 758 | }; |
| 759 | |
| 760 | /** |
| 761 | * kgsl_snapshot_process_ib_obj_list() - Go through the list of IB's which need |
| 762 | * to be dumped for snapshot and move them to the global snapshot list so |
| 763 | * they will get dumped when the global list is dumped |
| 764 | * @device: device being snapshotted |
| 765 | */ |
| 766 | static void kgsl_snapshot_process_ib_obj_list(struct kgsl_snapshot *snapshot) |
| 767 | { |
| 768 | struct kgsl_snapshot_cp_obj *obj, *obj_temp; |
| 769 | struct adreno_ib_object *ib_obj; |
| 770 | int i; |
| 771 | |
| 772 | list_for_each_entry_safe(obj, obj_temp, &snapshot->cp_list, |
| 773 | node) { |
| 774 | for (i = 0; i < obj->ib_obj_list->num_objs; i++) { |
| 775 | ib_obj = &(obj->ib_obj_list->obj_list[i]); |
| 776 | kgsl_snapshot_get_object(snapshot, ib_obj->entry->priv, |
| 777 | ib_obj->gpuaddr, ib_obj->size, |
| 778 | ib_obj->snapshot_obj_type); |
| 779 | } |
| 780 | list_del(&obj->node); |
| 781 | adreno_ib_destroy_obj_list(obj->ib_obj_list); |
| 782 | kfree(obj); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | #define to_snapshot_attr(a) \ |
| 787 | container_of(a, struct kgsl_snapshot_attribute, attr) |
| 788 | |
| 789 | #define kobj_to_device(a) \ |
| 790 | container_of(a, struct kgsl_device, snapshot_kobj) |
| 791 | |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 792 | static int snapshot_release(struct kgsl_device *device, |
| 793 | struct kgsl_snapshot *snapshot) |
| 794 | { |
| 795 | bool snapshot_free = false; |
| 796 | int ret = 0; |
| 797 | |
| 798 | mutex_lock(&device->mutex); |
| 799 | snapshot->sysfs_read--; |
| 800 | |
| 801 | /* |
| 802 | * If someone's replaced the snapshot, return an error and free |
| 803 | * the snapshot if this is the last thread to read it. |
| 804 | */ |
| 805 | if (device->snapshot != snapshot) { |
| 806 | ret = -EIO; |
| 807 | if (!snapshot->sysfs_read) |
| 808 | snapshot_free = true; |
| 809 | } |
| 810 | mutex_unlock(&device->mutex); |
| 811 | if (snapshot_free) |
| 812 | kgsl_free_snapshot(snapshot); |
| 813 | return ret; |
| 814 | } |
| 815 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 816 | /* Dump the sysfs binary data to the user */ |
| 817 | static ssize_t snapshot_show(struct file *filep, struct kobject *kobj, |
| 818 | struct bin_attribute *attr, char *buf, loff_t off, |
| 819 | size_t count) |
| 820 | { |
| 821 | struct kgsl_device *device = kobj_to_device(kobj); |
| 822 | struct kgsl_snapshot *snapshot; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 823 | struct kgsl_snapshot_section_header head; |
| 824 | struct snapshot_obj_itr itr; |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 825 | int ret = 0; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 826 | |
| 827 | if (device == NULL) |
| 828 | return 0; |
| 829 | |
| 830 | mutex_lock(&device->mutex); |
| 831 | snapshot = device->snapshot; |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 832 | if (snapshot != NULL) { |
| 833 | /* |
| 834 | * If we're reading at a non-zero offset from a new snapshot, |
| 835 | * that means we want to read from the previous snapshot (which |
| 836 | * was overwritten), so return an error |
| 837 | */ |
| 838 | if (snapshot->first_read) { |
| 839 | if (off) |
| 840 | ret = -EIO; |
| 841 | else |
| 842 | snapshot->first_read = false; |
| 843 | } |
| 844 | if (!ret) |
| 845 | snapshot->sysfs_read++; |
| 846 | } |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 847 | mutex_unlock(&device->mutex); |
| 848 | |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 849 | if (ret) |
| 850 | return ret; |
| 851 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 852 | /* Return nothing if we haven't taken a snapshot yet */ |
| 853 | if (snapshot == NULL) |
| 854 | return 0; |
| 855 | |
| 856 | /* |
| 857 | * Wait for the dump worker to finish. This is interruptible |
| 858 | * to allow userspace to bail if things go horribly wrong. |
| 859 | */ |
| 860 | ret = wait_for_completion_interruptible(&snapshot->dump_gate); |
| 861 | if (ret) { |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 862 | snapshot_release(device, snapshot); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 863 | return ret; |
| 864 | } |
| 865 | |
| 866 | obj_itr_init(&itr, buf, off, count); |
| 867 | |
| 868 | ret = obj_itr_out(&itr, snapshot->start, snapshot->size); |
| 869 | if (ret == 0) |
| 870 | goto done; |
| 871 | |
| 872 | /* Dump the memory pool if it exists */ |
| 873 | if (snapshot->mempool) { |
| 874 | ret = obj_itr_out(&itr, snapshot->mempool, |
| 875 | snapshot->mempool_size); |
| 876 | if (ret == 0) |
| 877 | goto done; |
| 878 | } |
| 879 | |
| 880 | { |
| 881 | head.magic = SNAPSHOT_SECTION_MAGIC; |
| 882 | head.id = KGSL_SNAPSHOT_SECTION_END; |
| 883 | head.size = sizeof(head); |
| 884 | |
| 885 | obj_itr_out(&itr, &head, sizeof(head)); |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | * Make sure everything has been written out before destroying things. |
| 890 | * The best way to confirm this is to go all the way through without |
| 891 | * writing any bytes - so only release if we get this far and |
| 892 | * itr->write is 0 and there are no concurrent reads pending |
| 893 | */ |
| 894 | |
| 895 | if (itr.write == 0) { |
| 896 | bool snapshot_free = false; |
| 897 | |
| 898 | mutex_lock(&device->mutex); |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 899 | if (--snapshot->sysfs_read == 0) { |
| 900 | if (device->snapshot == snapshot) |
| 901 | device->snapshot = NULL; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 902 | snapshot_free = true; |
| 903 | } |
| 904 | mutex_unlock(&device->mutex); |
| 905 | |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 906 | if (snapshot_free) |
| 907 | kgsl_free_snapshot(snapshot); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | done: |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 912 | ret = snapshot_release(device, snapshot); |
| 913 | return (ret < 0) ? ret : itr.write; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /* Show the total number of hangs since device boot */ |
| 917 | static ssize_t faultcount_show(struct kgsl_device *device, char *buf) |
| 918 | { |
| 919 | return snprintf(buf, PAGE_SIZE, "%d\n", device->snapshot_faultcount); |
| 920 | } |
| 921 | |
| 922 | /* Reset the total number of hangs since device boot */ |
| 923 | static ssize_t faultcount_store(struct kgsl_device *device, const char *buf, |
| 924 | size_t count) |
| 925 | { |
| 926 | if (device && count > 0) |
| 927 | device->snapshot_faultcount = 0; |
| 928 | |
| 929 | return count; |
| 930 | } |
| 931 | |
| 932 | /* Show the force_panic request status */ |
| 933 | static ssize_t force_panic_show(struct kgsl_device *device, char *buf) |
| 934 | { |
| 935 | return snprintf(buf, PAGE_SIZE, "%d\n", device->force_panic); |
| 936 | } |
| 937 | |
| 938 | /* Store the panic request value to force_panic */ |
| 939 | static ssize_t force_panic_store(struct kgsl_device *device, const char *buf, |
| 940 | size_t count) |
| 941 | { |
| 942 | unsigned int val = 0; |
| 943 | int ret; |
| 944 | |
| 945 | if (device && count > 0) |
| 946 | device->force_panic = 0; |
| 947 | |
| 948 | ret = kgsl_sysfs_store(buf, &val); |
| 949 | |
| 950 | if (!ret && device) |
| 951 | device->force_panic = (bool)val; |
| 952 | |
| 953 | return (ssize_t) ret < 0 ? ret : count; |
| 954 | } |
| 955 | |
| 956 | /* Show the snapshot_crashdumper request status */ |
| 957 | static ssize_t snapshot_crashdumper_show(struct kgsl_device *device, char *buf) |
| 958 | { |
| 959 | return snprintf(buf, PAGE_SIZE, "%d\n", device->snapshot_crashdumper); |
| 960 | } |
| 961 | |
| 962 | |
| 963 | /* Store the value to snapshot_crashdumper*/ |
| 964 | static ssize_t snapshot_crashdumper_store(struct kgsl_device *device, |
| 965 | const char *buf, size_t count) |
| 966 | { |
| 967 | unsigned int val = 0; |
| 968 | int ret; |
| 969 | |
| 970 | if (device && count > 0) |
| 971 | device->snapshot_crashdumper = 1; |
| 972 | |
| 973 | ret = kgsl_sysfs_store(buf, &val); |
| 974 | |
| 975 | if (!ret && device) |
| 976 | device->snapshot_crashdumper = (bool)val; |
| 977 | |
| 978 | return (ssize_t) ret < 0 ? ret : count; |
| 979 | } |
| 980 | |
| 981 | /* Show the timestamp of the last collected snapshot */ |
| 982 | static ssize_t timestamp_show(struct kgsl_device *device, char *buf) |
| 983 | { |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 984 | unsigned long timestamp; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 985 | |
Lynus Vaz | 43695aa | 2017-09-01 21:55:23 +0530 | [diff] [blame] | 986 | mutex_lock(&device->mutex); |
| 987 | timestamp = device->snapshot ? device->snapshot->timestamp : 0; |
| 988 | mutex_unlock(&device->mutex); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 989 | return snprintf(buf, PAGE_SIZE, "%lu\n", timestamp); |
| 990 | } |
| 991 | |
Harshdeep Dhatt | 134f7af | 2017-05-17 13:54:41 -0600 | [diff] [blame] | 992 | static ssize_t snapshot_legacy_show(struct kgsl_device *device, char *buf) |
| 993 | { |
| 994 | return snprintf(buf, PAGE_SIZE, "%d\n", device->snapshot_legacy); |
| 995 | } |
| 996 | |
| 997 | static ssize_t snapshot_legacy_store(struct kgsl_device *device, |
| 998 | const char *buf, size_t count) |
| 999 | { |
| 1000 | unsigned int val = 0; |
| 1001 | int ret; |
| 1002 | |
| 1003 | ret = kgsl_sysfs_store(buf, &val); |
| 1004 | |
| 1005 | if (!ret && device) |
| 1006 | device->snapshot_legacy = (bool)val; |
| 1007 | |
| 1008 | return (ssize_t) ret < 0 ? ret : count; |
| 1009 | } |
| 1010 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1011 | static struct bin_attribute snapshot_attr = { |
| 1012 | .attr.name = "dump", |
| 1013 | .attr.mode = 0444, |
| 1014 | .size = 0, |
| 1015 | .read = snapshot_show |
| 1016 | }; |
| 1017 | |
| 1018 | #define SNAPSHOT_ATTR(_name, _mode, _show, _store) \ |
| 1019 | struct kgsl_snapshot_attribute attr_##_name = { \ |
| 1020 | .attr = { .name = __stringify(_name), .mode = _mode }, \ |
| 1021 | .show = _show, \ |
| 1022 | .store = _store, \ |
| 1023 | } |
| 1024 | |
| 1025 | static SNAPSHOT_ATTR(timestamp, 0444, timestamp_show, NULL); |
| 1026 | static SNAPSHOT_ATTR(faultcount, 0644, faultcount_show, faultcount_store); |
| 1027 | static SNAPSHOT_ATTR(force_panic, 0644, force_panic_show, force_panic_store); |
| 1028 | static SNAPSHOT_ATTR(snapshot_crashdumper, 0644, snapshot_crashdumper_show, |
| 1029 | snapshot_crashdumper_store); |
Harshdeep Dhatt | 134f7af | 2017-05-17 13:54:41 -0600 | [diff] [blame] | 1030 | static SNAPSHOT_ATTR(snapshot_legacy, 0644, snapshot_legacy_show, |
| 1031 | snapshot_legacy_store); |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1032 | |
| 1033 | static ssize_t snapshot_sysfs_show(struct kobject *kobj, |
| 1034 | struct attribute *attr, char *buf) |
| 1035 | { |
| 1036 | struct kgsl_snapshot_attribute *pattr = to_snapshot_attr(attr); |
| 1037 | struct kgsl_device *device = kobj_to_device(kobj); |
| 1038 | ssize_t ret; |
| 1039 | |
| 1040 | if (device && pattr->show) |
| 1041 | ret = pattr->show(device, buf); |
| 1042 | else |
| 1043 | ret = -EIO; |
| 1044 | |
| 1045 | return ret; |
| 1046 | } |
| 1047 | |
| 1048 | static ssize_t snapshot_sysfs_store(struct kobject *kobj, |
| 1049 | struct attribute *attr, const char *buf, size_t count) |
| 1050 | { |
| 1051 | struct kgsl_snapshot_attribute *pattr = to_snapshot_attr(attr); |
| 1052 | struct kgsl_device *device = kobj_to_device(kobj); |
| 1053 | ssize_t ret; |
| 1054 | |
| 1055 | if (device && pattr->store) |
| 1056 | ret = pattr->store(device, buf, count); |
| 1057 | else |
| 1058 | ret = -EIO; |
| 1059 | |
| 1060 | return ret; |
| 1061 | } |
| 1062 | |
| 1063 | static const struct sysfs_ops snapshot_sysfs_ops = { |
| 1064 | .show = snapshot_sysfs_show, |
| 1065 | .store = snapshot_sysfs_store, |
| 1066 | }; |
| 1067 | |
| 1068 | static struct kobj_type ktype_snapshot = { |
| 1069 | .sysfs_ops = &snapshot_sysfs_ops, |
| 1070 | }; |
| 1071 | |
| 1072 | /** |
| 1073 | * kgsl_device_snapshot_init() - add resources for the device GPU snapshot |
| 1074 | * @device: The device to initialize |
| 1075 | * |
| 1076 | * Allocate memory for a GPU snapshot for the specified device, |
| 1077 | * and create the sysfs files to manage it |
| 1078 | */ |
| 1079 | int kgsl_device_snapshot_init(struct kgsl_device *device) |
| 1080 | { |
| 1081 | int ret; |
| 1082 | |
| 1083 | if (kgsl_property_read_u32(device, "qcom,snapshot-size", |
| 1084 | (unsigned int *) &(device->snapshot_memory.size))) |
| 1085 | device->snapshot_memory.size = KGSL_SNAPSHOT_MEMSIZE; |
| 1086 | |
| 1087 | /* |
| 1088 | * Choosing a memory size of 0 is essentially the same as disabling |
| 1089 | * snapshotting |
| 1090 | */ |
| 1091 | if (device->snapshot_memory.size == 0) |
| 1092 | return 0; |
| 1093 | |
| 1094 | /* |
| 1095 | * I'm not sure why anybody would choose to do so but make sure |
| 1096 | * that we can at least fit the snapshot header in the requested |
| 1097 | * region |
| 1098 | */ |
| 1099 | if (device->snapshot_memory.size < sizeof(struct kgsl_snapshot_header)) |
| 1100 | device->snapshot_memory.size = |
| 1101 | sizeof(struct kgsl_snapshot_header); |
| 1102 | |
| 1103 | device->snapshot_memory.ptr = kzalloc(device->snapshot_memory.size, |
| 1104 | GFP_KERNEL); |
| 1105 | |
| 1106 | if (device->snapshot_memory.ptr == NULL) |
| 1107 | return -ENOMEM; |
| 1108 | |
| 1109 | device->snapshot = NULL; |
| 1110 | device->snapshot_faultcount = 0; |
| 1111 | device->force_panic = 0; |
| 1112 | device->snapshot_crashdumper = 1; |
Harshdeep Dhatt | 134f7af | 2017-05-17 13:54:41 -0600 | [diff] [blame] | 1113 | device->snapshot_legacy = 0; |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1114 | |
| 1115 | ret = kobject_init_and_add(&device->snapshot_kobj, &ktype_snapshot, |
| 1116 | &device->dev->kobj, "snapshot"); |
| 1117 | if (ret) |
| 1118 | goto done; |
| 1119 | |
| 1120 | ret = sysfs_create_bin_file(&device->snapshot_kobj, &snapshot_attr); |
| 1121 | if (ret) |
| 1122 | goto done; |
| 1123 | |
| 1124 | ret = sysfs_create_file(&device->snapshot_kobj, &attr_timestamp.attr); |
| 1125 | if (ret) |
| 1126 | goto done; |
| 1127 | |
| 1128 | ret = sysfs_create_file(&device->snapshot_kobj, &attr_faultcount.attr); |
| 1129 | if (ret) |
| 1130 | goto done; |
| 1131 | |
| 1132 | ret = sysfs_create_file(&device->snapshot_kobj, |
| 1133 | &attr_force_panic.attr); |
| 1134 | if (ret) |
| 1135 | goto done; |
| 1136 | |
| 1137 | ret = sysfs_create_file(&device->snapshot_kobj, |
| 1138 | &attr_snapshot_crashdumper.attr); |
Harshdeep Dhatt | 134f7af | 2017-05-17 13:54:41 -0600 | [diff] [blame] | 1139 | if (ret) |
| 1140 | goto done; |
| 1141 | |
| 1142 | ret = sysfs_create_file(&device->snapshot_kobj, |
| 1143 | &attr_snapshot_legacy.attr); |
| 1144 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1145 | done: |
| 1146 | return ret; |
| 1147 | } |
| 1148 | EXPORT_SYMBOL(kgsl_device_snapshot_init); |
| 1149 | |
| 1150 | /** |
| 1151 | * kgsl_device_snapshot_close() - take down snapshot memory for a device |
| 1152 | * @device: Pointer to the kgsl_device |
| 1153 | * |
| 1154 | * Remove the sysfs files and free the memory allocated for the GPU |
| 1155 | * snapshot |
| 1156 | */ |
| 1157 | void kgsl_device_snapshot_close(struct kgsl_device *device) |
| 1158 | { |
| 1159 | sysfs_remove_bin_file(&device->snapshot_kobj, &snapshot_attr); |
| 1160 | sysfs_remove_file(&device->snapshot_kobj, &attr_timestamp.attr); |
| 1161 | |
| 1162 | kobject_put(&device->snapshot_kobj); |
| 1163 | |
| 1164 | kfree(device->snapshot_memory.ptr); |
| 1165 | |
| 1166 | device->snapshot_memory.ptr = NULL; |
| 1167 | device->snapshot_memory.size = 0; |
| 1168 | device->snapshot_faultcount = 0; |
| 1169 | device->force_panic = 0; |
| 1170 | device->snapshot_crashdumper = 1; |
| 1171 | } |
| 1172 | EXPORT_SYMBOL(kgsl_device_snapshot_close); |
| 1173 | |
| 1174 | /** |
| 1175 | * kgsl_snapshot_add_ib_obj_list() - Add a IB object list to the snapshot |
| 1176 | * object list |
| 1177 | * @device: the device that is being snapshotted |
| 1178 | * @ib_obj_list: The IB list that has objects required to execute an IB |
| 1179 | * @num_objs: Number of IB objects |
| 1180 | * @ptbase: The pagetable base in which the IB is mapped |
| 1181 | * |
| 1182 | * Adds a new IB to the list of IB objects maintained when getting snapshot |
| 1183 | * Returns 0 on success else -ENOMEM on error |
| 1184 | */ |
| 1185 | int kgsl_snapshot_add_ib_obj_list(struct kgsl_snapshot *snapshot, |
| 1186 | struct adreno_ib_object_list *ib_obj_list) |
| 1187 | { |
| 1188 | struct kgsl_snapshot_cp_obj *obj; |
| 1189 | |
| 1190 | obj = kzalloc(sizeof(*obj), GFP_KERNEL); |
| 1191 | if (!obj) |
| 1192 | return -ENOMEM; |
| 1193 | obj->ib_obj_list = ib_obj_list; |
| 1194 | list_add(&obj->node, &snapshot->cp_list); |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | static size_t _mempool_add_object(struct kgsl_snapshot *snapshot, u8 *data, |
| 1199 | struct kgsl_snapshot_object *obj) |
| 1200 | { |
| 1201 | struct kgsl_snapshot_section_header *section = |
| 1202 | (struct kgsl_snapshot_section_header *)data; |
| 1203 | struct kgsl_snapshot_gpu_object_v2 *header = |
| 1204 | (struct kgsl_snapshot_gpu_object_v2 *)(data + sizeof(*section)); |
| 1205 | u8 *dest = data + sizeof(*section) + sizeof(*header); |
| 1206 | uint64_t size; |
| 1207 | |
| 1208 | size = obj->size; |
| 1209 | |
| 1210 | if (!kgsl_memdesc_map(&obj->entry->memdesc)) { |
| 1211 | KGSL_CORE_ERR("snapshot: failed to map GPU object\n"); |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | section->magic = SNAPSHOT_SECTION_MAGIC; |
| 1216 | section->id = KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2; |
| 1217 | section->size = size + sizeof(*header) + sizeof(*section); |
| 1218 | |
| 1219 | header->size = size >> 2; |
| 1220 | header->gpuaddr = obj->gpuaddr; |
| 1221 | header->ptbase = |
| 1222 | kgsl_mmu_pagetable_get_ttbr0(obj->entry->priv->pagetable); |
| 1223 | header->type = obj->type; |
| 1224 | |
| 1225 | if (kgsl_addr_range_overlap(obj->gpuaddr, obj->size, |
| 1226 | snapshot->ib1base, snapshot->ib1size)) |
| 1227 | snapshot->ib1dumped = true; |
| 1228 | |
| 1229 | if (kgsl_addr_range_overlap(obj->gpuaddr, obj->size, |
| 1230 | snapshot->ib2base, snapshot->ib2size)) |
| 1231 | snapshot->ib2dumped = true; |
| 1232 | |
| 1233 | memcpy(dest, obj->entry->memdesc.hostptr + obj->offset, size); |
| 1234 | kgsl_memdesc_unmap(&obj->entry->memdesc); |
| 1235 | |
| 1236 | return section->size; |
| 1237 | } |
| 1238 | |
| 1239 | /** |
| 1240 | * kgsl_snapshot_save_frozen_objs() - Save the objects frozen in snapshot into |
| 1241 | * memory so that the data reported in these objects is correct when snapshot |
| 1242 | * is taken |
| 1243 | * @work: The work item that scheduled this work |
| 1244 | */ |
Carter Cooper | b88b708 | 2017-09-14 09:03:26 -0600 | [diff] [blame^] | 1245 | static void kgsl_snapshot_save_frozen_objs(struct work_struct *work) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1246 | { |
| 1247 | struct kgsl_snapshot *snapshot = container_of(work, |
| 1248 | struct kgsl_snapshot, work); |
| 1249 | struct kgsl_device *device = kgsl_get_device(KGSL_DEVICE_3D0); |
| 1250 | struct kgsl_snapshot_object *obj, *tmp; |
| 1251 | size_t size = 0; |
| 1252 | void *ptr; |
| 1253 | |
| 1254 | if (IS_ERR_OR_NULL(device)) |
| 1255 | return; |
| 1256 | |
Carter Cooper | b88b708 | 2017-09-14 09:03:26 -0600 | [diff] [blame^] | 1257 | if (snapshot->gmu_fault) |
| 1258 | goto gmu_only; |
| 1259 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1260 | kgsl_snapshot_process_ib_obj_list(snapshot); |
| 1261 | |
| 1262 | list_for_each_entry(obj, &snapshot->obj_list, node) { |
| 1263 | obj->size = ALIGN(obj->size, 4); |
| 1264 | |
| 1265 | size += ((size_t) obj->size + |
| 1266 | sizeof(struct kgsl_snapshot_gpu_object_v2) + |
| 1267 | sizeof(struct kgsl_snapshot_section_header)); |
| 1268 | } |
| 1269 | |
| 1270 | if (size == 0) |
| 1271 | goto done; |
| 1272 | |
| 1273 | snapshot->mempool = vmalloc(size); |
| 1274 | |
| 1275 | ptr = snapshot->mempool; |
| 1276 | snapshot->mempool_size = 0; |
| 1277 | |
| 1278 | /* even if vmalloc fails, make sure we clean up the obj_list */ |
| 1279 | list_for_each_entry_safe(obj, tmp, &snapshot->obj_list, node) { |
| 1280 | if (snapshot->mempool) { |
| 1281 | size_t ret = _mempool_add_object(snapshot, ptr, obj); |
| 1282 | |
| 1283 | ptr += ret; |
| 1284 | snapshot->mempool_size += ret; |
| 1285 | } |
| 1286 | |
| 1287 | kgsl_snapshot_put_object(obj); |
| 1288 | } |
| 1289 | done: |
| 1290 | /* |
| 1291 | * Get rid of the process struct here, so that it doesn't sit |
| 1292 | * around until someone bothers to read the snapshot file. |
| 1293 | */ |
| 1294 | kgsl_process_private_put(snapshot->process); |
| 1295 | snapshot->process = NULL; |
| 1296 | |
| 1297 | if (snapshot->ib1base && !snapshot->ib1dumped) |
| 1298 | KGSL_DRV_ERR(device, |
| 1299 | "snapshot: Active IB1:%016llx not dumped\n", |
| 1300 | snapshot->ib1base); |
| 1301 | else if (snapshot->ib2base && !snapshot->ib2dumped) |
| 1302 | KGSL_DRV_ERR(device, |
| 1303 | "snapshot: Active IB2:%016llx not dumped\n", |
| 1304 | snapshot->ib2base); |
| 1305 | |
Carter Cooper | b88b708 | 2017-09-14 09:03:26 -0600 | [diff] [blame^] | 1306 | gmu_only: |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1307 | complete_all(&snapshot->dump_gate); |
| 1308 | BUG_ON(device->force_panic); |
| 1309 | } |