adreno_snapshot.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/utsname.h>
  7. #include "adreno.h"
  8. #include "adreno_cp_parser.h"
  9. #include "adreno_pm4types.h"
  10. #include "adreno_snapshot.h"
  11. /* Maintain a list of the objects we see during parsing */
  12. #define SNAPSHOT_OBJ_BUFSIZE 64
  13. /* Used to print error message if an IB has too many objects in it */
  14. static int ib_max_objs;
  15. struct snapshot_rb_params {
  16. struct kgsl_snapshot *snapshot;
  17. struct adreno_ringbuffer *rb;
  18. };
  19. /* Keep track of how many bytes are frozen after a snapshot and tell the user */
  20. static size_t snapshot_frozen_objsize;
  21. static struct kgsl_snapshot_object objbuf[SNAPSHOT_OBJ_BUFSIZE];
  22. /* Pointer to the next open entry in the object list */
  23. static unsigned int objbufptr;
  24. static inline int adreno_rb_ctxtswitch(struct adreno_device *adreno_dev,
  25. unsigned int *cmd)
  26. {
  27. return cmd[0] == cp_packet(adreno_dev, CP_NOP, 1) &&
  28. cmd[1] == CONTEXT_TO_MEM_IDENTIFIER;
  29. }
  30. /* Push a new buffer object onto the list */
  31. void kgsl_snapshot_push_object(struct kgsl_device *device,
  32. struct kgsl_process_private *process,
  33. uint64_t gpuaddr, uint64_t dwords)
  34. {
  35. int index;
  36. struct kgsl_mem_entry *entry;
  37. if (process == NULL || gpuaddr == 0)
  38. return;
  39. /*
  40. * Sometimes IBs can be reused in the same dump. Because we parse from
  41. * oldest to newest, if we come across an IB that has already been used,
  42. * assume that it has been reused and update the list with the newest
  43. * size.
  44. */
  45. for (index = 0; index < objbufptr; index++) {
  46. if (objbuf[index].gpuaddr == gpuaddr &&
  47. objbuf[index].entry->priv == process) {
  48. /*
  49. * Check if newly requested size is within the
  50. * allocated range or not, otherwise continue
  51. * with previous size.
  52. */
  53. if (!kgsl_gpuaddr_in_memdesc(
  54. &objbuf[index].entry->memdesc,
  55. gpuaddr, dwords << 2)) {
  56. dev_err(device->dev,
  57. "snapshot: gpuaddr 0x%016llX size is less than requested\n",
  58. gpuaddr);
  59. return;
  60. }
  61. objbuf[index].size = max_t(uint64_t,
  62. objbuf[index].size,
  63. dwords << 2);
  64. return;
  65. }
  66. }
  67. if (objbufptr == SNAPSHOT_OBJ_BUFSIZE) {
  68. dev_err(device->dev, "snapshot: too many snapshot objects\n");
  69. return;
  70. }
  71. entry = kgsl_sharedmem_find(process, gpuaddr);
  72. if (entry == NULL) {
  73. dev_err(device->dev,
  74. "snapshot: Can't find entry for 0x%016llX\n", gpuaddr);
  75. return;
  76. }
  77. /*
  78. * In some gpu fault scenarios incorrect dword size resulting to return
  79. * without putting IB addrs into the list. Hence update IB dword size
  80. * within memdesc size to have IB dump in snapshot.
  81. */
  82. if ((gpuaddr + (dwords << 2)) > (entry->memdesc.gpuaddr + entry->memdesc.size))
  83. dwords = (entry->memdesc.size - (gpuaddr - entry->memdesc.gpuaddr)) >> 2;
  84. if (!kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, dwords << 2)) {
  85. dev_err(device->dev,
  86. "snapshot: Mem entry 0x%016llX is too small\n",
  87. gpuaddr);
  88. kgsl_mem_entry_put(entry);
  89. return;
  90. }
  91. /* Put it on the list of things to parse */
  92. objbuf[objbufptr].gpuaddr = gpuaddr;
  93. objbuf[objbufptr].size = dwords << 2;
  94. objbuf[objbufptr++].entry = entry;
  95. }
  96. /*
  97. * Returns index of the specified object is already on the list of buffers
  98. * to be dumped
  99. */
  100. static int find_object(uint64_t gpuaddr, struct kgsl_process_private *process)
  101. {
  102. int index;
  103. for (index = 0; index < objbufptr; index++) {
  104. if (objbuf[index].gpuaddr == gpuaddr &&
  105. objbuf[index].entry->priv == process)
  106. return index;
  107. }
  108. return -ENOENT;
  109. }
  110. /*
  111. * snapshot_freeze_obj_list() - Take a list of ib objects and freeze their
  112. * memory for snapshot
  113. * @snapshot: The snapshot data.
  114. * @process: The process to which the IB belongs
  115. * @ib_obj_list: List of the IB objects
  116. *
  117. * Returns 0 on success else error code
  118. */
  119. static int snapshot_freeze_obj_list(struct kgsl_snapshot *snapshot,
  120. struct kgsl_process_private *process,
  121. struct adreno_ib_object_list *ib_obj_list)
  122. {
  123. int ret = 0;
  124. struct adreno_ib_object *ib_objs;
  125. int i;
  126. for (i = 0; i < ib_obj_list->num_objs; i++) {
  127. int temp_ret;
  128. int index;
  129. int freeze = 1;
  130. ib_objs = &(ib_obj_list->obj_list[i]);
  131. /* Make sure this object is not going to be saved statically */
  132. for (index = 0; index < objbufptr; index++) {
  133. if ((objbuf[index].gpuaddr <= ib_objs->gpuaddr) &&
  134. ((objbuf[index].gpuaddr +
  135. (objbuf[index].size)) >=
  136. (ib_objs->gpuaddr + ib_objs->size)) &&
  137. (objbuf[index].entry->priv == process)) {
  138. freeze = 0;
  139. objbuf[index].entry->memdesc.priv &=
  140. ~KGSL_MEMDESC_SKIP_RECLAIM;
  141. break;
  142. }
  143. }
  144. if (freeze) {
  145. temp_ret = kgsl_snapshot_get_object(snapshot,
  146. process, ib_objs->gpuaddr,
  147. ib_objs->size,
  148. ib_objs->snapshot_obj_type);
  149. if (temp_ret < 0) {
  150. if (ret >= 0)
  151. ret = temp_ret;
  152. } else {
  153. snapshot_frozen_objsize += temp_ret;
  154. }
  155. }
  156. }
  157. return ret;
  158. }
  159. static inline bool adreno_ib_addr_overlap(u64 ibaddr, u64 gpuaddr, u64 dwords)
  160. {
  161. u64 size = dwords << 2;
  162. if (size > (U64_MAX - gpuaddr))
  163. return false;
  164. return (ibaddr >= gpuaddr && ibaddr < (gpuaddr + size));
  165. }
  166. void adreno_parse_ib(struct kgsl_device *device,
  167. struct kgsl_snapshot *snapshot,
  168. struct kgsl_process_private *process,
  169. uint64_t gpuaddr, uint64_t dwords)
  170. {
  171. struct adreno_ib_object_list *ib_obj_list;
  172. /*
  173. * Check the IB address - if it is either the last executed IB1
  174. * then push it into the static blob otherwise put it in the dynamic
  175. * list
  176. */
  177. if (adreno_ib_addr_overlap(snapshot->ib1base, gpuaddr, dwords)) {
  178. /*
  179. * During restore after preemption, ib1base in the register
  180. * can be updated by CP. In such scenarios, to dump complete
  181. * IB1 in snapshot, we should consider ib1base from ringbuffer.
  182. */
  183. snapshot->ib1base = gpuaddr;
  184. snapshot->ib1size = dwords;
  185. kgsl_snapshot_push_object(device, process, gpuaddr, dwords);
  186. return;
  187. }
  188. if (kgsl_snapshot_have_object(snapshot, process,
  189. gpuaddr, dwords << 2))
  190. return;
  191. if (-E2BIG == adreno_ib_create_object_list(device, process,
  192. gpuaddr, dwords, snapshot->ib2base,
  193. &ib_obj_list))
  194. ib_max_objs = 1;
  195. if (ib_obj_list)
  196. kgsl_snapshot_add_ib_obj_list(snapshot, ib_obj_list);
  197. }
  198. void adreno_parse_ib_lpac(struct kgsl_device *device,
  199. struct kgsl_snapshot *snapshot,
  200. struct kgsl_process_private *process,
  201. u64 gpuaddr, u64 dwords)
  202. {
  203. struct adreno_ib_object_list *ib_obj_list;
  204. /*
  205. * Check the IB address - if it is either the last executed IB1
  206. * then push it into the static blob otherwise put it in the dynamic
  207. * list
  208. */
  209. if (adreno_ib_addr_overlap(snapshot->ib1base_lpac, gpuaddr, dwords)) {
  210. /*
  211. * During restore after preemption, ib1base in the register
  212. * can be updated by CP. In such scenarios, to dump complete
  213. * IB1 in snapshot, we should consider ib1base from ringbuffer.
  214. */
  215. snapshot->ib1base_lpac = gpuaddr;
  216. snapshot->ib1size_lpac = dwords;
  217. kgsl_snapshot_push_object(device, process, gpuaddr, dwords);
  218. return;
  219. }
  220. if (kgsl_snapshot_have_object(snapshot, process,
  221. gpuaddr, dwords << 2))
  222. return;
  223. if (-E2BIG == adreno_ib_create_object_list(device, process,
  224. gpuaddr, dwords, snapshot->ib2base_lpac,
  225. &ib_obj_list))
  226. ib_max_objs = 1;
  227. if (ib_obj_list)
  228. kgsl_snapshot_add_ib_obj_list(snapshot, ib_obj_list);
  229. }
  230. void adreno_snapshot_dump_all_ibs(struct kgsl_device *device,
  231. unsigned int *rbptr, struct kgsl_snapshot *snapshot)
  232. {
  233. int index = 0;
  234. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  235. struct kgsl_iommu *iommu = KGSL_IOMMU(device);
  236. for (index = 0; index < KGSL_RB_DWORDS;) {
  237. if (adreno_cmd_is_ib(adreno_dev, rbptr[index])) {
  238. uint64_t ibaddr;
  239. uint64_t ibsize;
  240. if (ADRENO_LEGACY_PM4(adreno_dev)) {
  241. ibaddr = rbptr[index + 1];
  242. ibsize = rbptr[index + 2];
  243. index += 3;
  244. } else {
  245. ibaddr = rbptr[index + 2];
  246. ibaddr = ibaddr << 32 | rbptr[index + 1];
  247. ibsize = rbptr[index + 3];
  248. index += 4;
  249. }
  250. /* Don't parse known global IBs */
  251. if (kgsl_gpuaddr_in_memdesc(iommu->setstate,
  252. ibaddr, ibsize))
  253. continue;
  254. if (kgsl_gpuaddr_in_memdesc(adreno_dev->pwron_fixup,
  255. ibaddr, ibsize))
  256. continue;
  257. adreno_parse_ib(device, snapshot, snapshot->process,
  258. ibaddr, ibsize);
  259. } else
  260. index = index + 1;
  261. }
  262. }
  263. /**
  264. * snapshot_rb_ibs() - Dump rb data and capture the IB's in the RB as well
  265. * @device: Pointer to a KGSL device
  266. * @rb: The RB to dump
  267. * @data: Pointer to memory where the RB data is to be dumped
  268. * @snapshot: Pointer to information about the current snapshot being taken
  269. */
  270. static void snapshot_rb_ibs(struct kgsl_device *device,
  271. struct adreno_ringbuffer *rb,
  272. struct kgsl_snapshot *snapshot)
  273. {
  274. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  275. unsigned int *rbptr, rptr = adreno_get_rptr(rb);
  276. int index, i;
  277. int parse_ibs = 0, ib_parse_start;
  278. /*
  279. * During IB parse, vmalloc is called which can sleep and
  280. * should not be called from atomic context. Since IBs are not
  281. * dumped during atomic snapshot, there is no need to parse it.
  282. */
  283. if (device->snapshot_atomic)
  284. return;
  285. rbptr = rb->buffer_desc->hostptr;
  286. /*
  287. * KGSL tries to dump the active IB first. If it is not present, then
  288. * only it dumps all the IBs. In few cases, non-active IBs may help to
  289. * establish the flow and understand the hardware state better.
  290. */
  291. if (device->dump_all_ibs) {
  292. adreno_snapshot_dump_all_ibs(device, rbptr, snapshot);
  293. return;
  294. }
  295. /*
  296. * Figure out the window of ringbuffer data to dump. First we need to
  297. * find where the last processed IB ws submitted. Start walking back
  298. * from the rptr
  299. */
  300. index = rptr;
  301. do {
  302. index--;
  303. if (index < 0) {
  304. if (ADRENO_LEGACY_PM4(adreno_dev))
  305. index = KGSL_RB_DWORDS - 3;
  306. else
  307. index = KGSL_RB_DWORDS - 4;
  308. /* We wrapped without finding what we wanted */
  309. if (index < rb->wptr) {
  310. index = rb->wptr;
  311. break;
  312. }
  313. }
  314. if (adreno_cmd_is_ib(adreno_dev, rbptr[index])) {
  315. uint64_t ibaddr;
  316. uint64_t ibsize;
  317. if (ADRENO_LEGACY_PM4(adreno_dev)) {
  318. ibaddr = rbptr[index + 1];
  319. ibsize = rbptr[index + 2];
  320. } else {
  321. ibaddr = rbptr[index + 2];
  322. ibaddr = ibaddr << 32 | rbptr[index + 1];
  323. ibsize = rbptr[index + 3];
  324. }
  325. if (adreno_ib_addr_overlap(snapshot->ib1base,
  326. ibaddr, ibsize)) {
  327. /*
  328. * During restore after preemption, ib1base in
  329. * the register can be updated by CP. In such
  330. * scenario, to dump complete IB1 in snapshot,
  331. * we should consider ib1base from ringbuffer.
  332. */
  333. snapshot->ib1base = ibaddr;
  334. snapshot->ib1size = ibsize;
  335. break;
  336. }
  337. }
  338. } while (index != rb->wptr);
  339. /*
  340. * If the ib1 was not found, for example, if ib1base was restored
  341. * incorrectly after preemption, then simply dump the entire
  342. * ringbuffer along with all the IBs in the ringbuffer.
  343. */
  344. if (index == rb->wptr) {
  345. adreno_snapshot_dump_all_ibs(device, rb->buffer_desc->hostptr, snapshot);
  346. return;
  347. }
  348. /*
  349. * index points at the last submitted IB. We can only trust that the
  350. * memory between the context switch and the hanging IB is valid, so
  351. * the next step is to find the context switch before the submission
  352. */
  353. while (index != rb->wptr) {
  354. index--;
  355. if (index < 0) {
  356. index = KGSL_RB_DWORDS - 2;
  357. /*
  358. * Wrapped without finding the context switch. This is
  359. * harmless - we should still have enough data to dump a
  360. * valid state
  361. */
  362. if (index < rb->wptr) {
  363. index = rb->wptr;
  364. break;
  365. }
  366. }
  367. /* Break if the current packet is a context switch identifier */
  368. if ((rbptr[index] == cp_packet(adreno_dev, CP_NOP, 1)) &&
  369. (rbptr[index + 1] == CONTEXT_TO_MEM_IDENTIFIER))
  370. break;
  371. }
  372. /*
  373. * Index represents the start of the window of interest. We will try
  374. * to dump all buffers between here and the rptr
  375. */
  376. ib_parse_start = index;
  377. /*
  378. * Loop through the RB, looking for indirect buffers and MMU pagetable
  379. * changes
  380. */
  381. index = rb->wptr;
  382. for (i = 0; i < KGSL_RB_DWORDS; i++) {
  383. /*
  384. * Only parse IBs between the start and the rptr or the next
  385. * context switch, whichever comes first
  386. */
  387. if (parse_ibs == 0 && index == ib_parse_start)
  388. parse_ibs = 1;
  389. else if (index == rptr || adreno_rb_ctxtswitch(adreno_dev,
  390. &rbptr[index]))
  391. parse_ibs = 0;
  392. if (parse_ibs && adreno_cmd_is_ib(adreno_dev, rbptr[index])) {
  393. struct kgsl_iommu *iommu = KGSL_IOMMU(device);
  394. uint64_t ibaddr;
  395. uint64_t ibsize;
  396. if (ADRENO_LEGACY_PM4(adreno_dev)) {
  397. ibaddr = rbptr[index + 1];
  398. ibsize = rbptr[index + 2];
  399. } else {
  400. ibaddr = rbptr[index + 2];
  401. ibaddr = ibaddr << 32 | rbptr[index + 1];
  402. ibsize = rbptr[index + 3];
  403. }
  404. index = (index + 1) % KGSL_RB_DWORDS;
  405. /* Don't parse known global IBs */
  406. if (kgsl_gpuaddr_in_memdesc(iommu->setstate,
  407. ibaddr, ibsize))
  408. continue;
  409. if (kgsl_gpuaddr_in_memdesc(adreno_dev->pwron_fixup,
  410. ibaddr, ibsize))
  411. continue;
  412. adreno_parse_ib(device, snapshot, snapshot->process,
  413. ibaddr, ibsize);
  414. } else
  415. index = (index + 1) % KGSL_RB_DWORDS;
  416. }
  417. }
  418. /* Snapshot the ringbuffer memory */
  419. static size_t snapshot_rb(struct kgsl_device *device, u8 *buf,
  420. size_t remain, void *priv)
  421. {
  422. struct kgsl_snapshot_rb_v2 *header = (struct kgsl_snapshot_rb_v2 *)buf;
  423. unsigned int *data = (unsigned int *)(buf + sizeof(*header));
  424. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  425. struct snapshot_rb_params *snap_rb_params = priv;
  426. struct kgsl_snapshot *snapshot = snap_rb_params->snapshot;
  427. struct adreno_ringbuffer *rb = snap_rb_params->rb;
  428. /*
  429. * Dump the entire ringbuffer - the parser can choose how much of it to
  430. * process
  431. */
  432. if (remain < KGSL_RB_SIZE + sizeof(*header)) {
  433. dev_err(device->dev,
  434. "snapshot: Not enough memory for the rb section\n");
  435. return 0;
  436. }
  437. /* Write the sub-header for the section */
  438. header->start = 0;
  439. header->end = KGSL_RB_DWORDS;
  440. header->wptr = rb->wptr;
  441. header->rptr = adreno_get_rptr(rb);
  442. header->rbsize = KGSL_RB_DWORDS;
  443. header->count = KGSL_RB_DWORDS;
  444. adreno_rb_readtimestamp(adreno_dev, rb, KGSL_TIMESTAMP_QUEUED,
  445. &header->timestamp_queued);
  446. adreno_rb_readtimestamp(adreno_dev, rb, KGSL_TIMESTAMP_RETIRED,
  447. &header->timestamp_retired);
  448. header->gpuaddr = rb->buffer_desc->gpuaddr;
  449. header->id = rb->id;
  450. if (rb == adreno_dev->cur_rb)
  451. snapshot_rb_ibs(device, rb, snapshot);
  452. /* Just copy the ringbuffer, there are no active IBs */
  453. memcpy(data, rb->buffer_desc->hostptr, KGSL_RB_SIZE);
  454. /* Return the size of the section */
  455. return KGSL_RB_SIZE + sizeof(*header);
  456. }
  457. static int _count_mem_entries(int id, void *ptr, void *data)
  458. {
  459. int *count = data;
  460. *count = *count + 1;
  461. return 0;
  462. }
  463. struct mem_entry {
  464. uint64_t gpuaddr;
  465. uint64_t size;
  466. unsigned int type;
  467. } __packed;
  468. static size_t snapshot_capture_mem_list(struct kgsl_device *device,
  469. u8 *buf, size_t remain, void *priv)
  470. {
  471. struct kgsl_snapshot_mem_list_v2 *header =
  472. (struct kgsl_snapshot_mem_list_v2 *)buf;
  473. int id, index = 0, ret = 0, num_mem = 0;
  474. struct kgsl_process_private *process = priv;
  475. struct mem_entry *m = (struct mem_entry *)(buf + sizeof(*header));
  476. struct kgsl_mem_entry *entry;
  477. /* we need a process to search! */
  478. if (process == NULL)
  479. return 0;
  480. spin_lock(&process->mem_lock);
  481. /* We need to know the number of memory objects that the process has */
  482. idr_for_each(&process->mem_idr, _count_mem_entries, &num_mem);
  483. if (num_mem == 0)
  484. goto out;
  485. if (remain < ((num_mem * sizeof(struct mem_entry)) + sizeof(*header))) {
  486. dev_err(device->dev,
  487. "snapshot: Not enough memory for the mem list\n");
  488. goto out;
  489. }
  490. header->num_entries = num_mem;
  491. header->ptbase = kgsl_mmu_pagetable_get_ttbr0(process->pagetable);
  492. /*
  493. * Walk through the memory list and store the
  494. * tuples(gpuaddr, size, memtype) in snapshot
  495. */
  496. idr_for_each_entry(&process->mem_idr, entry, id) {
  497. m[index].gpuaddr = entry->memdesc.gpuaddr;
  498. m[index].size = entry->memdesc.size;
  499. m[index].type = kgsl_memdesc_get_memtype(&entry->memdesc);
  500. index++;
  501. }
  502. ret = sizeof(*header) + (num_mem * sizeof(struct mem_entry));
  503. out:
  504. spin_unlock(&process->mem_lock);
  505. return ret;
  506. }
  507. struct snapshot_ib_meta {
  508. struct kgsl_snapshot *snapshot;
  509. struct kgsl_snapshot_object *obj;
  510. uint64_t ib1base;
  511. uint64_t ib1size;
  512. uint64_t ib2base;
  513. uint64_t ib2size;
  514. u64 ib1base_lpac;
  515. u64 ib1size_lpac;
  516. u64 ib2base_lpac;
  517. u64 ib2size_lpac;
  518. };
  519. static void kgsl_snapshot_add_active_ib_obj_list(struct kgsl_device *device,
  520. struct kgsl_snapshot *snapshot)
  521. {
  522. struct adreno_ib_object_list *ib_obj_list;
  523. int index = -ENOENT;
  524. if (!snapshot->ib1dumped)
  525. index = find_object(snapshot->ib1base, snapshot->process);
  526. /* only do this for IB1 because the IB2's are part of IB1 objects */
  527. if ((index != -ENOENT) &&
  528. (snapshot->ib1base == objbuf[index].gpuaddr)) {
  529. if (-E2BIG == adreno_ib_create_object_list(device,
  530. objbuf[index].entry->priv,
  531. objbuf[index].gpuaddr,
  532. objbuf[index].size >> 2,
  533. snapshot->ib2base,
  534. &ib_obj_list))
  535. ib_max_objs = 1;
  536. if (ib_obj_list) {
  537. /* freeze the IB objects in the IB */
  538. snapshot_freeze_obj_list(snapshot,
  539. objbuf[index].entry->priv,
  540. ib_obj_list);
  541. adreno_ib_destroy_obj_list(ib_obj_list);
  542. }
  543. } else {
  544. /* Get the IB2 index from parsed object */
  545. index = find_object(snapshot->ib2base, snapshot->process);
  546. if (index != -ENOENT)
  547. adreno_parse_ib(device, snapshot, snapshot->process,
  548. snapshot->ib2base, objbuf[index].size >> 2);
  549. }
  550. }
  551. static void kgsl_snapshot_add_active_lpac_ib_obj_list(struct kgsl_device *device,
  552. struct kgsl_snapshot *snapshot)
  553. {
  554. struct adreno_ib_object_list *ib_obj_list;
  555. int index = -ENOENT;
  556. if (!snapshot->ib1dumped_lpac)
  557. index = find_object(snapshot->ib1base_lpac, snapshot->process_lpac);
  558. /* only do this for IB1 because the IB2's are part of IB1 objects */
  559. if ((index != -ENOENT) &&
  560. (snapshot->ib1base_lpac == objbuf[index].gpuaddr)) {
  561. if (-E2BIG == adreno_ib_create_object_list(device,
  562. objbuf[index].entry->priv,
  563. objbuf[index].gpuaddr,
  564. objbuf[index].size >> 2,
  565. snapshot->ib2base_lpac,
  566. &ib_obj_list))
  567. ib_max_objs = 1;
  568. if (ib_obj_list) {
  569. /* freeze the IB objects in the IB */
  570. snapshot_freeze_obj_list(snapshot,
  571. objbuf[index].entry->priv,
  572. ib_obj_list);
  573. adreno_ib_destroy_obj_list(ib_obj_list);
  574. }
  575. } else {
  576. /* Get the IB2 index from parsed object */
  577. index = find_object(snapshot->ib2base_lpac, snapshot->process_lpac);
  578. if (index != -ENOENT)
  579. adreno_parse_ib_lpac(device, snapshot, snapshot->process_lpac,
  580. snapshot->ib2base_lpac, objbuf[index].size >> 2);
  581. }
  582. }
  583. /*
  584. * active_ib_is_parsed() - Checks if active ib is already parsed
  585. * @gpuaddr: Active IB base address at the time of fault
  586. * @size: Active IB size
  587. * @process: The process to which the IB belongs
  588. *
  589. * Function returns true if the active is already is parsed
  590. * else false
  591. */
  592. static bool active_ib_is_parsed(uint64_t gpuaddr, uint64_t size,
  593. struct kgsl_process_private *process)
  594. {
  595. int index;
  596. /* go through the static list for gpuaddr is in list or not */
  597. for (index = 0; index < objbufptr; index++) {
  598. if ((objbuf[index].gpuaddr <= gpuaddr) &&
  599. ((objbuf[index].gpuaddr +
  600. (objbuf[index].size)) >=
  601. (gpuaddr + size)) &&
  602. (objbuf[index].entry->priv == process))
  603. return true;
  604. }
  605. return false;
  606. }
  607. /* Snapshot the memory for an indirect buffer */
  608. static size_t snapshot_ib(struct kgsl_device *device, u8 *buf,
  609. size_t remain, void *priv)
  610. {
  611. struct kgsl_snapshot_ib_v2 *header = (struct kgsl_snapshot_ib_v2 *)buf;
  612. struct snapshot_ib_meta *metadata = priv;
  613. unsigned int *src;
  614. unsigned int *dst = (unsigned int *)(buf + sizeof(*header));
  615. struct adreno_ib_object_list *ib_obj_list;
  616. struct kgsl_snapshot *snapshot;
  617. struct kgsl_snapshot_object *obj;
  618. struct kgsl_memdesc *memdesc;
  619. if (metadata == NULL || metadata->snapshot == NULL || metadata->obj == NULL) {
  620. dev_err(device->dev, "snapshot: bad metadata\n");
  621. return 0;
  622. }
  623. snapshot = metadata->snapshot;
  624. obj = metadata->obj;
  625. memdesc = &obj->entry->memdesc;
  626. /* If size is zero get it from the medesc size */
  627. if (!obj->size)
  628. obj->size = (memdesc->size - (obj->gpuaddr - memdesc->gpuaddr));
  629. if (remain < (obj->size + sizeof(*header))) {
  630. dev_err(device->dev, "snapshot: Not enough memory for the ib\n");
  631. return 0;
  632. }
  633. src = kgsl_gpuaddr_to_vaddr(memdesc, obj->gpuaddr);
  634. if (src == NULL) {
  635. dev_err(device->dev,
  636. "snapshot: Unable to map GPU memory object 0x%016llX into the kernel\n",
  637. obj->gpuaddr);
  638. return 0;
  639. }
  640. /* only do this for IB1 because the IB2's are part of IB1 objects */
  641. if (metadata->ib1base == obj->gpuaddr) {
  642. snapshot->ib1dumped = active_ib_is_parsed(obj->gpuaddr,
  643. obj->size, obj->entry->priv);
  644. if (-E2BIG == adreno_ib_create_object_list(device,
  645. obj->entry->priv,
  646. obj->gpuaddr, obj->size >> 2,
  647. snapshot->ib2base,
  648. &ib_obj_list))
  649. ib_max_objs = 1;
  650. if (ib_obj_list) {
  651. /* freeze the IB objects in the IB */
  652. snapshot_freeze_obj_list(snapshot,
  653. obj->entry->priv,
  654. ib_obj_list);
  655. adreno_ib_destroy_obj_list(ib_obj_list);
  656. }
  657. }
  658. if (metadata->ib2base == obj->gpuaddr)
  659. snapshot->ib2dumped = active_ib_is_parsed(obj->gpuaddr,
  660. obj->size, obj->entry->priv);
  661. if (metadata->ib1base_lpac == obj->gpuaddr) {
  662. snapshot->ib1dumped_lpac = active_ib_is_parsed(obj->gpuaddr,
  663. obj->size, obj->entry->priv);
  664. if (-E2BIG == adreno_ib_create_object_list(device,
  665. obj->entry->priv,
  666. obj->gpuaddr, obj->size >> 2,
  667. snapshot->ib2base_lpac,
  668. &ib_obj_list))
  669. ib_max_objs = 1;
  670. if (ib_obj_list) {
  671. /* freeze the IB objects in the IB */
  672. snapshot_freeze_obj_list(snapshot,
  673. obj->entry->priv,
  674. ib_obj_list);
  675. adreno_ib_destroy_obj_list(ib_obj_list);
  676. }
  677. }
  678. if (metadata->ib2base_lpac == obj->gpuaddr)
  679. snapshot->ib2dumped_lpac = active_ib_is_parsed(obj->gpuaddr,
  680. obj->size, obj->entry->priv);
  681. /* Write the sub-header for the section */
  682. header->gpuaddr = obj->gpuaddr;
  683. header->ptbase =
  684. kgsl_mmu_pagetable_get_ttbr0(obj->entry->priv->pagetable);
  685. header->size = obj->size >> 2;
  686. /* Write the contents of the ib */
  687. memcpy((void *)dst, (void *)src, (size_t) obj->size);
  688. /* Write the contents of the ib */
  689. return obj->size + sizeof(*header);
  690. }
  691. /* Dump another item on the current pending list */
  692. static void dump_object(struct kgsl_device *device, int obj,
  693. struct kgsl_snapshot *snapshot)
  694. {
  695. struct snapshot_ib_meta metadata;
  696. metadata.snapshot = snapshot;
  697. metadata.obj = &objbuf[obj];
  698. metadata.ib1base = snapshot->ib1base;
  699. metadata.ib1size = snapshot->ib1size;
  700. metadata.ib2base = snapshot->ib2base;
  701. metadata.ib2size = snapshot->ib2size;
  702. metadata.ib1base_lpac = snapshot->ib1base_lpac;
  703. metadata.ib1size_lpac = snapshot->ib1size_lpac;
  704. metadata.ib2base_lpac = snapshot->ib2base_lpac;
  705. metadata.ib2size_lpac = snapshot->ib2size_lpac;
  706. kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_IB_V2,
  707. snapshot, snapshot_ib, &metadata);
  708. if (objbuf[obj].entry) {
  709. kgsl_memdesc_unmap(&(objbuf[obj].entry->memdesc));
  710. kgsl_mem_entry_put(objbuf[obj].entry);
  711. }
  712. }
  713. /* setup_fault process - Find kgsl_process_private struct that caused the fault
  714. *
  715. * Find the faulting process based what the dispatcher thinks happened and
  716. * what the hardware is using for the current pagetable. The process struct
  717. * will be used to look up GPU addresses that are encountered while parsing
  718. * the GPU state.
  719. */
  720. static struct kgsl_process_private *setup_fault_process(struct kgsl_device *device,
  721. struct kgsl_snapshot *snapshot,
  722. struct kgsl_context *context)
  723. {
  724. u64 hw_ptbase, proc_ptbase;
  725. struct kgsl_process_private *process = context ? context->proc_priv : NULL;
  726. if (process != NULL && !kgsl_process_private_get(process))
  727. process = NULL;
  728. /* Get the physical address of the MMU pagetable */
  729. hw_ptbase = kgsl_mmu_get_current_ttbr0(&device->mmu, context);
  730. /* if we have an input process, make sure the ptbases match */
  731. if (process) {
  732. int asid = kgsl_mmu_pagetable_get_asid(process->pagetable, context);
  733. proc_ptbase = kgsl_mmu_pagetable_get_ttbr0(process->pagetable);
  734. if (asid >= 0)
  735. proc_ptbase |= FIELD_PREP(GENMASK_ULL(63, KGSL_IOMMU_ASID_START_BIT), asid);
  736. /* agreement! No need to check further */
  737. if (hw_ptbase == proc_ptbase)
  738. goto done;
  739. kgsl_process_private_put(process);
  740. process = NULL;
  741. dev_err(device->dev,
  742. "snapshot: ptbase mismatch hw %llx sw %llx\n",
  743. hw_ptbase, proc_ptbase);
  744. }
  745. /* try to find the right pagetable by walking the process list */
  746. if (kgsl_mmu_is_perprocess(&device->mmu)) {
  747. struct kgsl_process_private *tmp;
  748. read_lock(&kgsl_driver.proclist_lock);
  749. list_for_each_entry(tmp, &kgsl_driver.process_list, list) {
  750. u64 pt_ttbr0;
  751. pt_ttbr0 = kgsl_mmu_pagetable_get_ttbr0(tmp->pagetable);
  752. if ((pt_ttbr0 == MMU_SW_PT_BASE(hw_ptbase))
  753. && kgsl_process_private_get(tmp)) {
  754. process = tmp;
  755. break;
  756. }
  757. }
  758. read_unlock(&kgsl_driver.proclist_lock);
  759. }
  760. done:
  761. return process;
  762. }
  763. /* Snapshot a global memory buffer */
  764. size_t adreno_snapshot_global(struct kgsl_device *device, u8 *buf,
  765. size_t remain, void *priv)
  766. {
  767. struct kgsl_memdesc *memdesc = priv;
  768. struct kgsl_snapshot_gpu_object_v2 *header =
  769. (struct kgsl_snapshot_gpu_object_v2 *)buf;
  770. u8 *ptr = buf + sizeof(*header);
  771. if (IS_ERR_OR_NULL(memdesc) || memdesc->size == 0)
  772. return 0;
  773. if (remain < (memdesc->size + sizeof(*header))) {
  774. dev_err(device->dev,
  775. "snapshot: Not enough memory for the memdesc\n");
  776. return 0;
  777. }
  778. if (memdesc->hostptr == NULL) {
  779. dev_err(device->dev,
  780. "snapshot: no kernel mapping for global object 0x%016llX\n",
  781. memdesc->gpuaddr);
  782. return 0;
  783. }
  784. header->size = memdesc->size >> 2;
  785. header->gpuaddr = memdesc->gpuaddr;
  786. header->ptbase = MMU_DEFAULT_TTBR0(device);
  787. header->type = SNAPSHOT_GPU_OBJECT_GLOBAL;
  788. memcpy(ptr, memdesc->hostptr, memdesc->size);
  789. return memdesc->size + sizeof(*header);
  790. }
  791. /* Snapshot IOMMU specific buffers */
  792. static void adreno_snapshot_iommu(struct kgsl_device *device,
  793. struct kgsl_snapshot *snapshot)
  794. {
  795. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  796. struct kgsl_iommu *iommu = KGSL_IOMMU(device);
  797. kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,
  798. snapshot, adreno_snapshot_global, iommu->setstate);
  799. if (adreno_is_preemption_enabled(adreno_dev))
  800. kgsl_snapshot_add_section(device,
  801. KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,
  802. snapshot, adreno_snapshot_global, iommu->smmu_info);
  803. }
  804. static void adreno_snapshot_ringbuffer(struct kgsl_device *device,
  805. struct kgsl_snapshot *snapshot, struct adreno_ringbuffer *rb)
  806. {
  807. struct snapshot_rb_params params = {
  808. .snapshot = snapshot,
  809. .rb = rb,
  810. };
  811. if (rb == NULL)
  812. return;
  813. kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_RB_V2, snapshot,
  814. snapshot_rb, &params);
  815. }
  816. static void adreno_snapshot_os(struct kgsl_device *device,
  817. struct kgsl_snapshot *snapshot, struct kgsl_context *guilty,
  818. struct kgsl_context *guilty_lpac)
  819. {
  820. struct kgsl_snapshot_section_header *sect =
  821. (struct kgsl_snapshot_section_header *) snapshot->ptr;
  822. struct kgsl_snapshot_linux_v4 *header = (struct kgsl_snapshot_linux_v4 *)
  823. (snapshot->ptr + sizeof(*sect));
  824. struct kgsl_context *context;
  825. u32 remain;
  826. void *mem;
  827. int id;
  828. if (snapshot->remain < (sizeof(*sect) + sizeof(*header))) {
  829. SNAPSHOT_ERR_NOMEM(device, "OS");
  830. return;
  831. }
  832. header->osid = KGSL_SNAPSHOT_OS_LINUX_V4;
  833. strscpy(header->release, init_utsname()->release, sizeof(header->release));
  834. strscpy(header->version, init_utsname()->version, sizeof(header->version));
  835. header->seconds = ktime_get_real_seconds();
  836. header->power_flags = device->pwrctrl.power_flags;
  837. header->power_level = device->pwrctrl.active_pwrlevel;
  838. header->power_interval_timeout = device->pwrctrl.interval_timeout;
  839. header->grpclk = clk_get_rate(device->pwrctrl.grp_clks[0]);
  840. /* Get the current PT base */
  841. header->ptbase = kgsl_mmu_get_current_ttbr0(&device->mmu, guilty);
  842. header->ptbase_lpac = kgsl_mmu_get_current_ttbr0(&device->mmu, guilty_lpac);
  843. header->ctxtcount = 0;
  844. /* If we know the guilty context then dump it */
  845. if (guilty) {
  846. header->current_context = guilty->id;
  847. header->pid = guilty->tid;
  848. strscpy(header->comm, guilty->proc_priv->comm,
  849. sizeof(header->comm));
  850. }
  851. if (guilty_lpac) {
  852. header->current_context_lpac = guilty_lpac->id;
  853. header->pid_lpac = guilty_lpac->tid;
  854. strscpy(header->comm_lpac, guilty_lpac->proc_priv->comm,
  855. sizeof(header->comm_lpac));
  856. }
  857. remain = snapshot->remain - sizeof(*sect) + sizeof(*header);
  858. mem = snapshot->ptr + sizeof(*sect) + sizeof(*header);
  859. read_lock(&device->context_lock);
  860. idr_for_each_entry(&device->context_idr, context, id) {
  861. struct kgsl_snapshot_linux_context_v2 *c = mem;
  862. if (remain < sizeof(*c))
  863. break;
  864. kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_QUEUED,
  865. &c->timestamp_queued);
  866. kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_CONSUMED,
  867. &c->timestamp_consumed);
  868. kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_RETIRED,
  869. &c->timestamp_retired);
  870. header->ctxtcount++;
  871. mem += sizeof(*c);
  872. remain -= sizeof(*c);
  873. }
  874. read_unlock(&device->context_lock);
  875. sect->magic = SNAPSHOT_SECTION_MAGIC;
  876. sect->id = KGSL_SNAPSHOT_SECTION_OS;
  877. sect->size = sizeof(*sect) + sizeof(*header) +
  878. header->ctxtcount * sizeof(struct kgsl_snapshot_linux_context_v2);
  879. snapshot->ptr += sect->size;
  880. snapshot->remain -= sect->size;
  881. snapshot->size += sect->size;
  882. }
  883. static void adreno_static_ib_dump(struct kgsl_device *device,
  884. struct kgsl_process_private *process, u64 ib1base,
  885. u32 ib1size, u64 ib2base, u32 ib2size)
  886. {
  887. if (process == NULL)
  888. return;
  889. /* Make sure that the last IB1 that was being executed is dumped.
  890. * Since this was the last IB1 that was processed, we should have
  891. * already added it to the list during the ringbuffer parse but we
  892. * want to be double plus sure.
  893. * The problem is that IB size from the register is the unprocessed size
  894. * of the buffer not the original size, so if we didn't catch this
  895. * buffer being directly used in the RB, then we might not be able to
  896. * dump the whole thing. Try to dump the maximum possible size from the
  897. * IB1 base address till the end of memdesc size so that we dont miss
  898. * what we are interested in. Print a warning message so we can try to
  899. * figure how often this really happens.
  900. */
  901. if (ib1base && (-ENOENT == find_object(ib1base, process))) {
  902. struct kgsl_mem_entry *entry;
  903. u64 ibsize;
  904. entry = kgsl_sharedmem_find(process, ib1base);
  905. if (entry == NULL) {
  906. dev_err(device->dev,
  907. "Can't find a memory entry containing IB1BASE %16llx\n",
  908. ib1base);
  909. } else {
  910. ibsize = entry->memdesc.size -
  911. (ib1base - entry->memdesc.gpuaddr);
  912. kgsl_mem_entry_put(entry);
  913. kgsl_snapshot_push_object(device, process,
  914. ib1base, ibsize >> 2);
  915. dev_err(device->dev,
  916. "CP_IB1_BASE %16llx is not found in the ringbuffer. Dumping %llx dwords of the buffer\n",
  917. ib1base, ibsize >> 2);
  918. }
  919. }
  920. /*
  921. * Add the last parsed IB2 to the list. The IB2 should be found as we
  922. * parse the objects below, but we try to add it to the list first, so
  923. * it too can be parsed. Don't print an error message in this case - if
  924. * the IB2 is found during parsing, the list will be updated with the
  925. * correct size.
  926. */
  927. if (ib2base && (-ENOENT == find_object(ib2base, process)))
  928. kgsl_snapshot_push_object(device, process, ib2base, ib2size);
  929. }
  930. /* adreno_snapshot - Snapshot the Adreno GPU state
  931. * @device - KGSL device to snapshot
  932. * @snapshot - Pointer to the snapshot instance
  933. * @context - context that caused the fault, if known by the driver
  934. * This is a hook function called by kgsl_snapshot to snapshot the
  935. * Adreno specific information for the GPU snapshot. In turn, this function
  936. * calls the GPU specific snapshot function to get core specific information.
  937. */
  938. void adreno_snapshot(struct kgsl_device *device, struct kgsl_snapshot *snapshot,
  939. struct kgsl_context *context, struct kgsl_context *context_lpac)
  940. {
  941. unsigned int i;
  942. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  943. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  944. struct kgsl_snapshot_header *header = (struct kgsl_snapshot_header *)
  945. snapshot->ptr;
  946. /* Set up the master header */
  947. header->magic = SNAPSHOT_MAGIC;
  948. /* gpuid is deprecated so initialize it to an obviously wrong value */
  949. header->gpuid = UINT_MAX;
  950. header->chipid = adreno_dev->chipid;
  951. snapshot->ptr += sizeof(*header);
  952. snapshot->remain -= sizeof(*header);
  953. snapshot->size += sizeof(*header);
  954. /* Write the OS section */
  955. adreno_snapshot_os(device, snapshot, context, context_lpac);
  956. ib_max_objs = 0;
  957. /* Reset the list of objects */
  958. objbufptr = 0;
  959. snapshot_frozen_objsize = 0;
  960. snapshot->process = setup_fault_process(device, snapshot, context);
  961. snapshot->process_lpac = setup_fault_process(device, snapshot, context_lpac);
  962. /* Add GPU specific sections - registers mainly, but other stuff too */
  963. if (gpudev->snapshot)
  964. gpudev->snapshot(adreno_dev, snapshot);
  965. snapshot->ib1dumped = false;
  966. snapshot->ib2dumped = false;
  967. snapshot->ib1dumped_lpac = false;
  968. snapshot->ib2dumped_lpac = false;
  969. adreno_snapshot_ringbuffer(device, snapshot, adreno_dev->cur_rb);
  970. /* Dump the prev ringbuffer */
  971. if (adreno_dev->prev_rb != adreno_dev->cur_rb)
  972. adreno_snapshot_ringbuffer(device, snapshot,
  973. adreno_dev->prev_rb);
  974. if ((adreno_dev->next_rb != adreno_dev->prev_rb) &&
  975. (adreno_dev->next_rb != adreno_dev->cur_rb))
  976. adreno_snapshot_ringbuffer(device, snapshot,
  977. adreno_dev->next_rb);
  978. if (device->snapshot_atomic)
  979. return;
  980. /* Dump selected global buffers */
  981. kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,
  982. snapshot, adreno_snapshot_global, device->memstore);
  983. kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,
  984. snapshot, adreno_snapshot_global, device->scratch);
  985. kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,
  986. snapshot, adreno_snapshot_global,
  987. adreno_dev->pwron_fixup);
  988. kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_GPU_OBJECT_V2,
  989. snapshot, adreno_snapshot_global,
  990. adreno_dev->profile_buffer);
  991. if (kgsl_mmu_get_mmutype(device) == KGSL_MMU_TYPE_IOMMU)
  992. adreno_snapshot_iommu(device, snapshot);
  993. /*
  994. * Add a section that lists (gpuaddr, size, memtype) tuples of the
  995. * hanging process
  996. */
  997. kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_MEMLIST_V2,
  998. snapshot, snapshot_capture_mem_list, snapshot->process);
  999. kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_MEMLIST_V2,
  1000. snapshot, snapshot_capture_mem_list, snapshot->process_lpac);
  1001. adreno_static_ib_dump(device, snapshot->process,
  1002. snapshot->ib1base, snapshot->ib1size, snapshot->ib2base, snapshot->ib2size);
  1003. adreno_static_ib_dump(device, snapshot->process_lpac,
  1004. snapshot->ib1base_lpac, snapshot->ib1size_lpac,
  1005. snapshot->ib2base_lpac, snapshot->ib2size_lpac);
  1006. /*
  1007. * Go through the list of found objects and dump each one. As the IBs
  1008. * are parsed, more objects might be found, and objbufptr will increase
  1009. */
  1010. for (i = 0; i < objbufptr; i++)
  1011. dump_object(device, i, snapshot);
  1012. /*
  1013. * Incase snapshot static blob is running out of memory, Add Active IB1
  1014. * and IB2 entries to obj_list so that active ib's can be dumped to
  1015. * snapshot dynamic blob.
  1016. */
  1017. if (!snapshot->ib1dumped || !snapshot->ib2dumped)
  1018. kgsl_snapshot_add_active_ib_obj_list(device, snapshot);
  1019. if (ib_max_objs)
  1020. dev_err(device->dev, "Max objects found in GC IB\n");
  1021. if (!snapshot->ib1dumped_lpac || !snapshot->ib2dumped_lpac)
  1022. kgsl_snapshot_add_active_lpac_ib_obj_list(device, snapshot);
  1023. if (ib_max_objs)
  1024. dev_err(device->dev, "Max objects found in LPAC IB\n");
  1025. if (snapshot_frozen_objsize)
  1026. dev_err(device->dev,
  1027. "GPU snapshot froze %zdKb of GPU buffers\n",
  1028. snapshot_frozen_objsize / 1024);
  1029. }
  1030. void adreno_snapshot_registers(struct kgsl_device *device,
  1031. struct kgsl_snapshot *snapshot,
  1032. const unsigned int *regs, unsigned int count)
  1033. {
  1034. struct kgsl_snapshot_registers r;
  1035. r.regs = regs;
  1036. r.count = count;
  1037. kgsl_snapshot_add_section(device, KGSL_SNAPSHOT_SECTION_REGS, snapshot,
  1038. kgsl_snapshot_dump_registers, &r);
  1039. }
  1040. int adreno_snapshot_regs_count(const u32 *ptr)
  1041. {
  1042. unsigned int count = 0;
  1043. unsigned int group_count;
  1044. for ( ; ptr[0] != UINT_MAX; ptr += 2) {
  1045. group_count = REG_COUNT(ptr);
  1046. if (group_count == 1)
  1047. count += group_count + 1;
  1048. else
  1049. count += group_count + 2;
  1050. }
  1051. return count;
  1052. }
  1053. /*
  1054. * This is a new format for dumping the registers, where we dump just the first
  1055. * address of the register along with the count of the contiguous registers
  1056. * which we going to dump. This helps us save memory by not dumping the
  1057. * address for each register
  1058. */
  1059. size_t adreno_snapshot_registers_v2(struct kgsl_device *device, u8 *buf,
  1060. size_t remain, void *priv)
  1061. {
  1062. const u32 *ptr = (const u32 *)priv;
  1063. unsigned int *data = (unsigned int *)buf;
  1064. int count = 0, k;
  1065. /* Figure out how many registers we are going to dump */
  1066. count = adreno_snapshot_regs_count(ptr);
  1067. if (remain < (count * 4)) {
  1068. SNAPSHOT_ERR_NOMEM(device, "REGISTERS");
  1069. return 0;
  1070. }
  1071. for (ptr = (const u32 *)priv; ptr[0] != UINT_MAX; ptr += 2) {
  1072. int cnt = REG_COUNT(ptr);
  1073. if (cnt == 1)
  1074. *data++ = BIT(31) | ptr[0];
  1075. else {
  1076. *data++ = ptr[0];
  1077. *data++ = cnt;
  1078. }
  1079. for (k = ptr[0]; k <= ptr[1]; k++) {
  1080. kgsl_regread(device, k, data);
  1081. data++;
  1082. }
  1083. }
  1084. /* Return the size of the section */
  1085. return (count * 4);
  1086. }
  1087. size_t adreno_snapshot_cx_misc_registers(struct kgsl_device *device,
  1088. u8 *buf, size_t remain, void *priv)
  1089. {
  1090. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  1091. const u32 *ptr = (const u32 *)priv;
  1092. u32 *data = (unsigned int *)buf;
  1093. int count = 0, k;
  1094. /* Figure out how many registers we are going to dump */
  1095. count = adreno_snapshot_regs_count(ptr);
  1096. if (remain < (count * sizeof(u32))) {
  1097. SNAPSHOT_ERR_NOMEM(device, "CX_MISC REGISTERS");
  1098. return 0;
  1099. }
  1100. for (; ptr[0] != UINT_MAX; ptr += 2) {
  1101. int cnt = REG_COUNT(ptr);
  1102. if (cnt == 1)
  1103. *data++ = BIT(31) | ptr[0];
  1104. else {
  1105. *data++ = ptr[0];
  1106. *data++ = cnt;
  1107. }
  1108. for (k = ptr[0]; k <= ptr[1]; k++)
  1109. adreno_cx_misc_regread(adreno_dev,
  1110. k - adreno_dev->cx_misc_base, data++);
  1111. }
  1112. /* Return the size of the section */
  1113. return (count * sizeof(u32));
  1114. }