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