kgsl_snapshot.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /* Copyright (c) 2012-2016, 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. #include <linux/export.h>
  13. #include <linux/time.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/utsname.h>
  16. #include <linux/sched.h>
  17. #include <linux/idr.h>
  18. #include "kgsl.h"
  19. #include "kgsl_log.h"
  20. #include "kgsl_device.h"
  21. #include "kgsl_sharedmem.h"
  22. #include "kgsl_snapshot.h"
  23. /* Placeholder for the list of memory objects frozen after a hang */
  24. struct kgsl_snapshot_object {
  25. unsigned int gpuaddr;
  26. phys_addr_t ptbase;
  27. unsigned int size;
  28. unsigned int offset;
  29. int type;
  30. struct kgsl_mem_entry *entry;
  31. struct list_head node;
  32. };
  33. struct snapshot_obj_itr {
  34. void *buf; /* Buffer pointer to write to */
  35. int pos; /* Current position in the sequence */
  36. loff_t offset; /* file offset to start writing from */
  37. size_t remain; /* Bytes remaining in buffer */
  38. size_t write; /* Bytes written so far */
  39. };
  40. static void obj_itr_init(struct snapshot_obj_itr *itr, void *buf,
  41. loff_t offset, size_t remain)
  42. {
  43. itr->buf = buf;
  44. itr->offset = offset;
  45. itr->remain = remain;
  46. itr->pos = 0;
  47. itr->write = 0;
  48. }
  49. static int obj_itr_out(struct snapshot_obj_itr *itr, void *src, int size)
  50. {
  51. if (itr->remain == 0)
  52. return 0;
  53. if ((itr->pos + size) <= itr->offset)
  54. goto done;
  55. /* Handle the case that offset is in the middle of the buffer */
  56. if (itr->offset > itr->pos) {
  57. src += (itr->offset - itr->pos);
  58. size -= (itr->offset - itr->pos);
  59. /* Advance pos to the offset start */
  60. itr->pos = itr->offset;
  61. }
  62. if (size > itr->remain)
  63. size = itr->remain;
  64. memcpy(itr->buf, src, size);
  65. itr->buf += size;
  66. itr->write += size;
  67. itr->remain -= size;
  68. done:
  69. itr->pos += size;
  70. return size;
  71. }
  72. /* idr_for_each function to count the number of contexts */
  73. static int snapshot_context_count(int id, void *ptr, void *data)
  74. {
  75. int *count = data;
  76. *count = *count + 1;
  77. return 0;
  78. }
  79. /*
  80. * To simplify the iterator loop use a global pointer instead of trying
  81. * to pass around double star references to the snapshot data
  82. */
  83. static void *_ctxtptr;
  84. static int snapshot_context_info(int id, void *ptr, void *data)
  85. {
  86. struct kgsl_snapshot_linux_context *header = _ctxtptr;
  87. struct kgsl_context *context = ptr;
  88. struct kgsl_device *device;
  89. if (context)
  90. device = context->device;
  91. else
  92. device = (struct kgsl_device *)data;
  93. header->id = id;
  94. /* Future-proof for per-context timestamps - for now, just
  95. * return the global timestamp for all contexts
  96. */
  97. header->timestamp_queued = kgsl_readtimestamp(device, context,
  98. KGSL_TIMESTAMP_QUEUED);
  99. header->timestamp_retired = kgsl_readtimestamp(device, context,
  100. KGSL_TIMESTAMP_RETIRED);
  101. _ctxtptr += sizeof(struct kgsl_snapshot_linux_context);
  102. return 0;
  103. }
  104. /* Snapshot the Linux specific information */
  105. static int snapshot_os(struct kgsl_device *device,
  106. void *snapshot, int remain, void *priv)
  107. {
  108. struct kgsl_snapshot_linux *header = snapshot;
  109. struct kgsl_pwrctrl *pwr = &device->pwrctrl;
  110. struct task_struct *task;
  111. pid_t pid;
  112. int hang = (int) priv;
  113. int ctxtcount = 0;
  114. int size = sizeof(*header);
  115. phys_addr_t temp_ptbase;
  116. /* Figure out how many active contexts there are - these will
  117. * be appended on the end of the structure */
  118. read_lock(&device->context_lock);
  119. idr_for_each(&device->context_idr, snapshot_context_count, &ctxtcount);
  120. read_unlock(&device->context_lock);
  121. /* Increment ctxcount for the global memstore */
  122. ctxtcount++;
  123. size += ctxtcount * sizeof(struct kgsl_snapshot_linux_context);
  124. /* Make sure there is enough room for the data */
  125. if (remain < size) {
  126. SNAPSHOT_ERR_NOMEM(device, "OS");
  127. return 0;
  128. }
  129. memset(header, 0, sizeof(*header));
  130. header->osid = KGSL_SNAPSHOT_OS_LINUX;
  131. header->state = hang ? SNAPSHOT_STATE_HUNG : SNAPSHOT_STATE_RUNNING;
  132. /* Get the kernel build information */
  133. strlcpy(header->release, utsname()->release, sizeof(header->release));
  134. strlcpy(header->version, utsname()->version, sizeof(header->version));
  135. /* Get the Unix time for the timestamp */
  136. header->seconds = get_seconds();
  137. /* Remember the power information */
  138. header->power_flags = pwr->power_flags;
  139. header->power_level = pwr->active_pwrlevel;
  140. header->power_interval_timeout = pwr->interval_timeout;
  141. header->grpclk = kgsl_get_clkrate(pwr->grp_clks[0]);
  142. header->busclk = kgsl_get_clkrate(pwr->ebi1_clk);
  143. /* Save the last active context */
  144. kgsl_sharedmem_readl(&device->memstore, &header->current_context,
  145. KGSL_MEMSTORE_OFFSET(KGSL_MEMSTORE_GLOBAL, current_context));
  146. /* Get the current PT base */
  147. temp_ptbase = kgsl_mmu_get_current_ptbase(&device->mmu);
  148. /* Truncate to 32 bits in case LPAE is used */
  149. header->ptbase = (__u32)temp_ptbase;
  150. /* And the PID for the task leader */
  151. pid = header->pid = kgsl_mmu_get_ptname_from_ptbase(&device->mmu,
  152. temp_ptbase);
  153. task = find_task_by_vpid(pid);
  154. if (task)
  155. get_task_comm(header->comm, task);
  156. header->ctxtcount = ctxtcount;
  157. _ctxtptr = snapshot + sizeof(*header);
  158. /* append information for the global context */
  159. snapshot_context_info(KGSL_MEMSTORE_GLOBAL, NULL, device);
  160. /* append information for each context */
  161. read_lock(&device->context_lock);
  162. idr_for_each(&device->context_idr, snapshot_context_info, NULL);
  163. read_unlock(&device->context_lock);
  164. /* Return the size of the data segment */
  165. return size;
  166. }
  167. /*
  168. * kgsl_snapshot_dump_indexed_regs - helper function to dump indexed registers
  169. * @device - the device to dump registers from
  170. * @snapshot - pointer to the start of the region of memory for the snapshot
  171. * @remain - a pointer to the number of bytes remaining in the snapshot
  172. * @priv - A pointer to the kgsl_snapshot_indexed_registers data
  173. *
  174. * Given a indexed register cmd/data pair and a count, dump each indexed
  175. * register
  176. */
  177. static int kgsl_snapshot_dump_indexed_regs(struct kgsl_device *device,
  178. void *snapshot, int remain, void *priv)
  179. {
  180. struct kgsl_snapshot_indexed_registers *iregs = priv;
  181. struct kgsl_snapshot_indexed_regs *header = snapshot;
  182. unsigned int *data = snapshot + sizeof(*header);
  183. int i;
  184. if (remain < (iregs->count * 4) + sizeof(*header)) {
  185. SNAPSHOT_ERR_NOMEM(device, "INDEXED REGS");
  186. return 0;
  187. }
  188. header->index_reg = iregs->index;
  189. header->data_reg = iregs->data;
  190. header->count = iregs->count;
  191. header->start = iregs->start;
  192. for (i = 0; i < iregs->count; i++) {
  193. kgsl_regwrite(device, iregs->index, iregs->start + i);
  194. kgsl_regread(device, iregs->data, &data[i]);
  195. }
  196. return (iregs->count * 4) + sizeof(*header);
  197. }
  198. #define GPU_OBJ_HEADER_SZ \
  199. (sizeof(struct kgsl_snapshot_section_header) + \
  200. sizeof(struct kgsl_snapshot_gpu_object))
  201. static int kgsl_snapshot_dump_object(struct kgsl_device *device,
  202. struct kgsl_snapshot_object *obj, struct snapshot_obj_itr *itr)
  203. {
  204. struct kgsl_snapshot_section_header sect;
  205. struct kgsl_snapshot_gpu_object header;
  206. int ret;
  207. sect.magic = SNAPSHOT_SECTION_MAGIC;
  208. sect.id = KGSL_SNAPSHOT_SECTION_GPU_OBJECT;
  209. /*
  210. * Header size is in dwords, object size is in bytes -
  211. * round up if the object size isn't dword aligned
  212. */
  213. sect.size = GPU_OBJ_HEADER_SZ + ALIGN(obj->size, 4);
  214. ret = obj_itr_out(itr, &sect, sizeof(sect));
  215. if (ret == 0)
  216. return 0;
  217. header.size = ALIGN(obj->size, 4) >> 2;
  218. header.gpuaddr = obj->gpuaddr;
  219. header.ptbase = (__u32)obj->ptbase;
  220. header.type = obj->type;
  221. ret = obj_itr_out(itr, &header, sizeof(header));
  222. if (ret == 0)
  223. return 0;
  224. ret = obj_itr_out(itr, obj->entry->memdesc.hostptr + obj->offset,
  225. obj->size);
  226. if (ret == 0)
  227. return 0;
  228. /* Pad the end to a dword boundary if we need to */
  229. if (obj->size % 4) {
  230. unsigned int dummy = 0;
  231. ret = obj_itr_out(itr, &dummy, obj->size % 4);
  232. }
  233. return ret;
  234. }
  235. static void kgsl_snapshot_put_object(struct kgsl_device *device,
  236. struct kgsl_snapshot_object *obj)
  237. {
  238. list_del(&obj->node);
  239. obj->entry->memdesc.priv &= ~KGSL_MEMDESC_FROZEN;
  240. kgsl_memdesc_unmap(&obj->entry->memdesc);
  241. kgsl_mem_entry_put(obj->entry);
  242. kfree(obj);
  243. }
  244. /* ksgl_snapshot_have_object - Return 1 if the object has been processed
  245. *@device - the device that is being snapshotted
  246. * @ptbase - the pagetable base of the object to freeze
  247. * @gpuaddr - The gpu address of the object to freeze
  248. * @size - the size of the object (may not always be the size of the region)
  249. *
  250. * Return 1 if the object is already in the list - this can save us from
  251. * having to parse the sme thing over again.
  252. */
  253. int kgsl_snapshot_have_object(struct kgsl_device *device, phys_addr_t ptbase,
  254. unsigned int gpuaddr, unsigned int size)
  255. {
  256. struct kgsl_snapshot_object *obj;
  257. list_for_each_entry(obj, &device->snapshot_obj_list, node) {
  258. if (obj->ptbase != ptbase)
  259. continue;
  260. if ((gpuaddr >= obj->gpuaddr) &&
  261. ((gpuaddr + size) <= (obj->gpuaddr + obj->size)))
  262. return 1;
  263. }
  264. return 0;
  265. }
  266. EXPORT_SYMBOL(kgsl_snapshot_have_object);
  267. /* kgsl_snapshot_get_object - Mark a GPU buffer to be frozen
  268. * @device - the device that is being snapshotted
  269. * @ptbase - the pagetable base of the object to freeze
  270. * @gpuaddr - The gpu address of the object to freeze
  271. * @size - the size of the object (may not always be the size of the region)
  272. * @type - the type of object being saved (shader, vbo, etc)
  273. *
  274. * Mark and freeze a GPU buffer object. This will prevent it from being
  275. * freed until it can be copied out as part of the snapshot dump. Returns the
  276. * size of the object being frozen
  277. */
  278. int kgsl_snapshot_get_object(struct kgsl_device *device, phys_addr_t ptbase,
  279. unsigned int gpuaddr, unsigned int size, unsigned int type)
  280. {
  281. struct kgsl_mem_entry *entry;
  282. struct kgsl_snapshot_object *obj;
  283. int offset;
  284. int ret = -EINVAL;
  285. if (!gpuaddr)
  286. return 0;
  287. entry = kgsl_get_mem_entry(device, ptbase, gpuaddr, size);
  288. if (entry == NULL) {
  289. KGSL_DRV_ERR(device, "Unable to find GPU buffer %8.8X\n",
  290. gpuaddr);
  291. return -EINVAL;
  292. }
  293. /* We can't freeze external memory, because we don't own it */
  294. if (entry->memtype != KGSL_MEM_ENTRY_KERNEL) {
  295. KGSL_DRV_ERR(device,
  296. "Only internal GPU buffers can be frozen\n");
  297. goto err_put;
  298. }
  299. /*
  300. * size indicates the number of bytes in the region to save. This might
  301. * not always be the entire size of the region because some buffers are
  302. * sub-allocated from a larger region. However, if size 0 was passed
  303. * thats a flag that the caller wants to capture the entire buffer
  304. */
  305. if (size == 0) {
  306. size = entry->memdesc.size;
  307. offset = 0;
  308. /* Adjust the gpuaddr to the start of the object */
  309. gpuaddr = entry->memdesc.gpuaddr;
  310. } else {
  311. offset = gpuaddr - entry->memdesc.gpuaddr;
  312. }
  313. if (size + offset > entry->memdesc.size) {
  314. KGSL_DRV_ERR(device, "Invalid size for GPU buffer %8.8X\n",
  315. gpuaddr);
  316. goto err_put;
  317. }
  318. /* If the buffer is already on the list, skip it */
  319. list_for_each_entry(obj, &device->snapshot_obj_list, node) {
  320. if (obj->gpuaddr == gpuaddr && obj->ptbase == ptbase) {
  321. /* If the size is different, use the bigger size */
  322. if (obj->size < size)
  323. obj->size = size;
  324. ret = 0;
  325. goto err_put;
  326. }
  327. }
  328. if (kgsl_memdesc_map(&entry->memdesc) == NULL) {
  329. KGSL_DRV_ERR(device, "Unable to map GPU buffer %X\n",
  330. gpuaddr);
  331. goto err_put;
  332. }
  333. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  334. if (obj == NULL) {
  335. KGSL_DRV_ERR(device, "Unable to allocate memory\n");
  336. goto err_unmap;
  337. }
  338. obj->type = type;
  339. obj->entry = entry;
  340. obj->gpuaddr = gpuaddr;
  341. obj->ptbase = ptbase;
  342. obj->size = size;
  343. obj->offset = offset;
  344. list_add(&obj->node, &device->snapshot_obj_list);
  345. /*
  346. * Return the size of the entire mem entry that was frozen - this gets
  347. * used for tracking how much memory is frozen for a hang. Also, mark
  348. * the memory entry as frozen. If the entry was already marked as
  349. * frozen, then another buffer already got to it. In that case, return
  350. * 0 so it doesn't get counted twice
  351. */
  352. ret = (entry->memdesc.priv & KGSL_MEMDESC_FROZEN) ? 0
  353. : entry->memdesc.size;
  354. entry->memdesc.priv |= KGSL_MEMDESC_FROZEN;
  355. return ret;
  356. err_unmap:
  357. kgsl_memdesc_unmap(&entry->memdesc);
  358. err_put:
  359. kgsl_mem_entry_put(entry);
  360. return ret;
  361. }
  362. EXPORT_SYMBOL(kgsl_snapshot_get_object);
  363. /*
  364. * kgsl_snapshot_dump_regs - helper function to dump device registers
  365. * @device - the device to dump registers from
  366. * @snapshot - pointer to the start of the region of memory for the snapshot
  367. * @remain - a pointer to the number of bytes remaining in the snapshot
  368. * @priv - A pointer to the kgsl_snapshot_registers data
  369. *
  370. * Given an array of register ranges pairs (start,end [inclusive]), dump the
  371. * registers into a snapshot register section. The snapshot region stores a
  372. * part of dwords for each register - the word address of the register, and
  373. * the value.
  374. */
  375. int kgsl_snapshot_dump_regs(struct kgsl_device *device, void *snapshot,
  376. int remain, void *priv)
  377. {
  378. struct kgsl_snapshot_registers_list *list = priv;
  379. struct kgsl_snapshot_regs *header = snapshot;
  380. struct kgsl_snapshot_registers *regs;
  381. unsigned int *data = snapshot + sizeof(*header);
  382. int count = 0, i, j, k;
  383. /* Figure out how many registers we are going to dump */
  384. for (i = 0; i < list->count; i++) {
  385. regs = &(list->registers[i]);
  386. for (j = 0; j < regs->count; j++) {
  387. int start = regs->regs[j * 2];
  388. int end = regs->regs[j * 2 + 1];
  389. count += (end - start + 1);
  390. }
  391. }
  392. if (remain < (count * 8) + sizeof(*header)) {
  393. SNAPSHOT_ERR_NOMEM(device, "REGISTERS");
  394. return 0;
  395. }
  396. for (i = 0; i < list->count; i++) {
  397. regs = &(list->registers[i]);
  398. for (j = 0; j < regs->count; j++) {
  399. unsigned int start = regs->regs[j * 2];
  400. unsigned int end = regs->regs[j * 2 + 1];
  401. for (k = start; k <= end; k++) {
  402. unsigned int val;
  403. kgsl_regread(device, k, &val);
  404. *data++ = k;
  405. *data++ = val;
  406. }
  407. }
  408. }
  409. header->count = count;
  410. /* Return the size of the section */
  411. return (count * 8) + sizeof(*header);
  412. }
  413. EXPORT_SYMBOL(kgsl_snapshot_dump_regs);
  414. void *kgsl_snapshot_indexed_registers(struct kgsl_device *device,
  415. void *snapshot, int *remain,
  416. unsigned int index, unsigned int data, unsigned int start,
  417. unsigned int count)
  418. {
  419. struct kgsl_snapshot_indexed_registers iregs;
  420. iregs.index = index;
  421. iregs.data = data;
  422. iregs.start = start;
  423. iregs.count = count;
  424. return kgsl_snapshot_add_section(device,
  425. KGSL_SNAPSHOT_SECTION_INDEXED_REGS, snapshot,
  426. remain, kgsl_snapshot_dump_indexed_regs, &iregs);
  427. }
  428. EXPORT_SYMBOL(kgsl_snapshot_indexed_registers);
  429. /*
  430. * kgsl_snapshot - construct a device snapshot
  431. * @device - device to snapshot
  432. * @hang - set to 1 if the snapshot was triggered following a hnag
  433. * Given a device, construct a binary snapshot dump of the current device state
  434. * and store it in the device snapshot memory.
  435. */
  436. int kgsl_device_snapshot(struct kgsl_device *device, int hang)
  437. {
  438. struct kgsl_snapshot_header *header = device->snapshot;
  439. int remain = device->snapshot_maxsize - sizeof(*header);
  440. void *snapshot;
  441. struct timespec boot;
  442. int ret = 0;
  443. /*
  444. * Bail if failed to get active count for GPU,
  445. * try again
  446. */
  447. if (kgsl_active_count_get(device)) {
  448. KGSL_DRV_ERR(device, "Failed to get GPU active count");
  449. return -EINVAL;
  450. }
  451. /* increment the hang count (on hang) for good book keeping */
  452. if (hang)
  453. device->snapshot_faultcount++;
  454. /*
  455. * The first hang is always the one we are interested in. To
  456. * avoid a subsequent hang blowing away the first, the snapshot
  457. * is frozen until it is dumped via sysfs.
  458. *
  459. * Note that triggered snapshots are always taken regardless
  460. * of the state and never frozen.
  461. */
  462. if (hang && device->snapshot_frozen == 1) {
  463. ret = 0;
  464. goto done;
  465. }
  466. if (device->snapshot == NULL) {
  467. KGSL_DRV_ERR(device,
  468. "snapshot: No snapshot memory available\n");
  469. ret = -ENOMEM;
  470. goto done;
  471. }
  472. if (remain < sizeof(*header)) {
  473. KGSL_DRV_ERR(device,
  474. "snapshot: Not enough memory for the header\n");
  475. ret = -ENOMEM;
  476. goto done;
  477. }
  478. header->magic = SNAPSHOT_MAGIC;
  479. header->gpuid = kgsl_gpuid(device, &header->chipid);
  480. /* Get a pointer to the first section (right after the header) */
  481. snapshot = ((void *) device->snapshot) + sizeof(*header);
  482. /* Build the Linux specific header */
  483. snapshot = kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_OS,
  484. snapshot, &remain, snapshot_os, (void *) hang);
  485. /* Get the device specific sections */
  486. if (device->ftbl->snapshot)
  487. snapshot = device->ftbl->snapshot(device, snapshot, &remain,
  488. hang);
  489. /*
  490. * The timestamp is the seconds since boot so it is easier to match to
  491. * the kernel log
  492. */
  493. getboottime(&boot);
  494. device->snapshot_timestamp = get_seconds() - boot.tv_sec;
  495. device->snapshot_size = (int) (snapshot - device->snapshot);
  496. /* Freeze the snapshot on a hang until it gets read */
  497. device->snapshot_frozen = (hang) ? 1 : 0;
  498. /* log buffer info to aid in ramdump fault tolerance */
  499. KGSL_DRV_ERR(device, "snapshot created at pa %lx size %d\n",
  500. __pa(device->snapshot), device->snapshot_size);
  501. if (hang)
  502. sysfs_notify(&device->snapshot_kobj, NULL, "timestamp");
  503. done:
  504. kgsl_active_count_put(device);
  505. return ret;
  506. }
  507. EXPORT_SYMBOL(kgsl_device_snapshot);
  508. /* An attribute for showing snapshot details */
  509. struct kgsl_snapshot_attribute {
  510. struct attribute attr;
  511. ssize_t (*show)(struct kgsl_device *device, char *buf);
  512. ssize_t (*store)(struct kgsl_device *device, const char *buf,
  513. size_t count);
  514. };
  515. #define to_snapshot_attr(a) \
  516. container_of(a, struct kgsl_snapshot_attribute, attr)
  517. #define kobj_to_device(a) \
  518. container_of(a, struct kgsl_device, snapshot_kobj)
  519. /* Dump the sysfs binary data to the user */
  520. static ssize_t snapshot_show(struct file *filep, struct kobject *kobj,
  521. struct bin_attribute *attr, char *buf, loff_t off,
  522. size_t count)
  523. {
  524. struct kgsl_device *device = kobj_to_device(kobj);
  525. struct kgsl_snapshot_object *obj, *tmp;
  526. struct kgsl_snapshot_section_header head;
  527. struct snapshot_obj_itr itr;
  528. int ret;
  529. if (device == NULL)
  530. return 0;
  531. /* Return nothing if we haven't taken a snapshot yet */
  532. if (device->snapshot_timestamp == 0)
  533. return 0;
  534. /* Get the mutex to keep things from changing while we are dumping */
  535. kgsl_mutex_lock(&device->mutex, &device->mutex_owner);
  536. obj_itr_init(&itr, buf, off, count);
  537. ret = obj_itr_out(&itr, device->snapshot, device->snapshot_size);
  538. if (ret == 0)
  539. goto done;
  540. list_for_each_entry(obj, &device->snapshot_obj_list, node)
  541. kgsl_snapshot_dump_object(device, obj, &itr);
  542. {
  543. head.magic = SNAPSHOT_SECTION_MAGIC;
  544. head.id = KGSL_SNAPSHOT_SECTION_END;
  545. head.size = sizeof(head);
  546. obj_itr_out(&itr, &head, sizeof(head));
  547. }
  548. /*
  549. * Make sure everything has been written out before destroying things.
  550. * The best way to confirm this is to go all the way through without
  551. * writing any bytes - so only release if we get this far and
  552. * itr->write is 0
  553. */
  554. if (itr.write == 0) {
  555. list_for_each_entry_safe(obj, tmp, &device->snapshot_obj_list,
  556. node)
  557. kgsl_snapshot_put_object(device, obj);
  558. if (device->snapshot_frozen)
  559. KGSL_DRV_ERR(device, "Snapshot objects released\n");
  560. device->snapshot_frozen = 0;
  561. }
  562. done:
  563. kgsl_mutex_unlock(&device->mutex, &device->mutex_owner);
  564. return itr.write;
  565. }
  566. /* Show the total number of hangs since device boot */
  567. static ssize_t faultcount_show(struct kgsl_device *device, char *buf)
  568. {
  569. return snprintf(buf, PAGE_SIZE, "%d\n", device->snapshot_faultcount);
  570. }
  571. /* Reset the total number of hangs since device boot */
  572. static ssize_t faultcount_store(struct kgsl_device *device, const char *buf,
  573. size_t count)
  574. {
  575. if (device && count > 0)
  576. device->snapshot_faultcount = 0;
  577. return count;
  578. }
  579. /* Show the timestamp of the last collected snapshot */
  580. static ssize_t timestamp_show(struct kgsl_device *device, char *buf)
  581. {
  582. return snprintf(buf, PAGE_SIZE, "%d\n", device->snapshot_timestamp);
  583. }
  584. /* manually trigger a new snapshot to be collected */
  585. static ssize_t trigger_store(struct kgsl_device *device, const char *buf,
  586. size_t count)
  587. {
  588. if (device && count > 0) {
  589. kgsl_mutex_lock(&device->mutex, &device->mutex_owner);
  590. if (!kgsl_active_count_get(device)) {
  591. kgsl_device_snapshot(device, 0);
  592. kgsl_active_count_put(device);
  593. }
  594. kgsl_mutex_unlock(&device->mutex, &device->mutex_owner);
  595. }
  596. return count;
  597. }
  598. static struct bin_attribute snapshot_attr = {
  599. .attr.name = "dump",
  600. .attr.mode = 0444,
  601. .size = 0,
  602. .read = snapshot_show
  603. };
  604. #define SNAPSHOT_ATTR(_name, _mode, _show, _store) \
  605. struct kgsl_snapshot_attribute attr_##_name = { \
  606. .attr = { .name = __stringify(_name), .mode = _mode }, \
  607. .show = _show, \
  608. .store = _store, \
  609. }
  610. SNAPSHOT_ATTR(trigger, 0600, NULL, trigger_store);
  611. SNAPSHOT_ATTR(timestamp, 0444, timestamp_show, NULL);
  612. SNAPSHOT_ATTR(faultcount, 0644, faultcount_show, faultcount_store);
  613. static void snapshot_sysfs_release(struct kobject *kobj)
  614. {
  615. }
  616. static ssize_t snapshot_sysfs_show(struct kobject *kobj,
  617. struct attribute *attr, char *buf)
  618. {
  619. struct kgsl_snapshot_attribute *pattr = to_snapshot_attr(attr);
  620. struct kgsl_device *device = kobj_to_device(kobj);
  621. ssize_t ret;
  622. if (device && pattr->show)
  623. ret = pattr->show(device, buf);
  624. else
  625. ret = -EIO;
  626. return ret;
  627. }
  628. static ssize_t snapshot_sysfs_store(struct kobject *kobj,
  629. struct attribute *attr, const char *buf, size_t count)
  630. {
  631. struct kgsl_snapshot_attribute *pattr = to_snapshot_attr(attr);
  632. struct kgsl_device *device = kobj_to_device(kobj);
  633. ssize_t ret;
  634. if (device && pattr->store)
  635. ret = pattr->store(device, buf, count);
  636. else
  637. ret = -EIO;
  638. return ret;
  639. }
  640. static const struct sysfs_ops snapshot_sysfs_ops = {
  641. .show = snapshot_sysfs_show,
  642. .store = snapshot_sysfs_store,
  643. };
  644. static struct kobj_type ktype_snapshot = {
  645. .sysfs_ops = &snapshot_sysfs_ops,
  646. .default_attrs = NULL,
  647. .release = snapshot_sysfs_release,
  648. };
  649. /* kgsl_device_snapshot_init - Add resources for the device GPU snapshot
  650. * @device - The device to initalize
  651. *
  652. * Allocate memory for a GPU snapshot for the specified device,
  653. * and create the sysfs files to manage it
  654. */
  655. int kgsl_device_snapshot_init(struct kgsl_device *device)
  656. {
  657. int ret;
  658. if (device->snapshot == NULL)
  659. device->snapshot = kzalloc(KGSL_SNAPSHOT_MEMSIZE, GFP_KERNEL);
  660. if (device->snapshot == NULL)
  661. return -ENOMEM;
  662. device->snapshot_maxsize = KGSL_SNAPSHOT_MEMSIZE;
  663. device->snapshot_timestamp = 0;
  664. device->snapshot_faultcount = 0;
  665. INIT_LIST_HEAD(&device->snapshot_obj_list);
  666. ret = kobject_init_and_add(&device->snapshot_kobj, &ktype_snapshot,
  667. &device->dev->kobj, "snapshot");
  668. if (ret)
  669. goto done;
  670. ret = sysfs_create_bin_file(&device->snapshot_kobj, &snapshot_attr);
  671. if (ret)
  672. goto done;
  673. ret = sysfs_create_file(&device->snapshot_kobj, &attr_trigger.attr);
  674. if (ret)
  675. goto done;
  676. ret = sysfs_create_file(&device->snapshot_kobj, &attr_timestamp.attr);
  677. if (ret)
  678. goto done;
  679. ret = sysfs_create_file(&device->snapshot_kobj, &attr_faultcount.attr);
  680. done:
  681. return ret;
  682. }
  683. EXPORT_SYMBOL(kgsl_device_snapshot_init);
  684. /* kgsl_device_snapshot_close - Take down snapshot memory for a device
  685. * @device - Pointer to the kgsl_device
  686. *
  687. * Remove the sysfs files and free the memory allocated for the GPU
  688. * snapshot
  689. */
  690. void kgsl_device_snapshot_close(struct kgsl_device *device)
  691. {
  692. sysfs_remove_bin_file(&device->snapshot_kobj, &snapshot_attr);
  693. sysfs_remove_file(&device->snapshot_kobj, &attr_trigger.attr);
  694. sysfs_remove_file(&device->snapshot_kobj, &attr_timestamp.attr);
  695. kobject_put(&device->snapshot_kobj);
  696. kfree(device->snapshot);
  697. device->snapshot = NULL;
  698. device->snapshot_maxsize = 0;
  699. device->snapshot_timestamp = 0;
  700. device->snapshot_faultcount = 0;
  701. }
  702. EXPORT_SYMBOL(kgsl_device_snapshot_close);