adreno_cp_parser.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/slab.h>
  6. #include "adreno.h"
  7. #include "adreno_cp_parser.h"
  8. #include "adreno_pm4types.h"
  9. #include "adreno_snapshot.h"
  10. #define MAX_IB_OBJS 1000
  11. #define NUM_SET_DRAW_GROUPS 32
  12. struct set_draw_state {
  13. uint64_t cmd_stream_addr;
  14. uint64_t cmd_stream_dwords;
  15. };
  16. /* List of variables used when parsing an IB */
  17. struct ib_parser_variables {
  18. /* List of registers containing addresses and their sizes */
  19. unsigned int cp_addr_regs[ADRENO_CP_ADDR_MAX];
  20. /* 32 groups of command streams in set draw state packets */
  21. struct set_draw_state set_draw_groups[NUM_SET_DRAW_GROUPS];
  22. };
  23. /*
  24. * Used for locating shader objects. This array holds the unit size of shader
  25. * objects based on type and block of shader. The type can be 0 or 1 hence there
  26. * are 2 columns and block can be 0-7 hence 7 rows.
  27. */
  28. static int load_state_unit_sizes[7][2] = {
  29. { 2, 4 },
  30. { 0, 1 },
  31. { 2, 4 },
  32. { 0, 1 },
  33. { 8, 2 },
  34. { 8, 2 },
  35. { 8, 2 },
  36. };
  37. static int adreno_ib_find_objs(struct kgsl_device *device,
  38. struct kgsl_process_private *process,
  39. uint64_t gpuaddr, uint64_t dwords,
  40. uint64_t ib2base, int obj_type,
  41. struct adreno_ib_object_list *ib_obj_list,
  42. int ib_level);
  43. static int ib_parse_set_draw_state(struct kgsl_device *device,
  44. unsigned int *ptr,
  45. struct kgsl_process_private *process,
  46. struct adreno_ib_object_list *ib_obj_list,
  47. struct ib_parser_variables *ib_parse_vars);
  48. static int ib_parse_type7_set_draw_state(struct kgsl_device *device,
  49. unsigned int *ptr,
  50. struct kgsl_process_private *process,
  51. struct adreno_ib_object_list *ib_obj_list);
  52. /*
  53. * adreno_ib_merge_range() - Increases the address range tracked by an ib
  54. * object
  55. * @ib_obj: The ib object
  56. * @gpuaddr: The start address which is to be merged
  57. * @size: Size of the merging address
  58. */
  59. static void adreno_ib_merge_range(struct adreno_ib_object *ib_obj,
  60. uint64_t gpuaddr, uint64_t size)
  61. {
  62. uint64_t addr_end1 = ib_obj->gpuaddr + ib_obj->size;
  63. uint64_t addr_end2 = gpuaddr + size;
  64. if (gpuaddr < ib_obj->gpuaddr)
  65. ib_obj->gpuaddr = gpuaddr;
  66. if (addr_end2 > addr_end1)
  67. ib_obj->size = addr_end2 - ib_obj->gpuaddr;
  68. else
  69. ib_obj->size = addr_end1 - ib_obj->gpuaddr;
  70. }
  71. /*
  72. * adreno_ib_check_overlap() - Checks if an address range overlap
  73. * @gpuaddr: The start address range to check for overlap
  74. * @size: Size of the address range
  75. * @type: The type of address range
  76. * @ib_obj_list: The list of address ranges to check for overlap
  77. *
  78. * Checks if an address range overlaps with a list of address ranges
  79. * Returns the entry from list which overlaps else NULL
  80. */
  81. static struct adreno_ib_object *adreno_ib_check_overlap(uint64_t gpuaddr,
  82. uint64_t size, int type,
  83. struct adreno_ib_object_list *ib_obj_list)
  84. {
  85. struct adreno_ib_object *ib_obj;
  86. int i;
  87. for (i = 0; i < ib_obj_list->num_objs; i++) {
  88. ib_obj = &(ib_obj_list->obj_list[i]);
  89. if ((type == ib_obj->snapshot_obj_type) &&
  90. kgsl_addr_range_overlap(ib_obj->gpuaddr, ib_obj->size,
  91. gpuaddr, size))
  92. /* regions overlap */
  93. return ib_obj;
  94. }
  95. return NULL;
  96. }
  97. /*
  98. * adreno_ib_add() - Add a gpuaddress range to list
  99. * @process: Process in which the gpuaddress is mapped
  100. * @type: The type of address range
  101. * @ib_obj_list: List of the address ranges in which the given range is to be
  102. * added
  103. *
  104. * Add a gpuaddress range as an ib object to a given list after checking if it
  105. * overlaps with another entry on the list. If it conflicts then change the
  106. * existing entry to incorporate this range
  107. *
  108. * Returns 0 on success else error code
  109. */
  110. static int adreno_ib_add(struct kgsl_process_private *process,
  111. uint64_t gpuaddr, int type,
  112. struct adreno_ib_object_list *ib_obj_list)
  113. {
  114. uint64_t size;
  115. struct adreno_ib_object *ib_obj;
  116. struct kgsl_mem_entry *entry;
  117. if (ib_obj_list->num_objs >= MAX_IB_OBJS)
  118. return -E2BIG;
  119. entry = kgsl_sharedmem_find(process, gpuaddr);
  120. if (!entry)
  121. /*
  122. * Do not fail if gpuaddr not found, we can continue
  123. * to search for other objects even if few objects are
  124. * not found
  125. */
  126. return 0;
  127. size = entry->memdesc.size;
  128. gpuaddr = entry->memdesc.gpuaddr;
  129. ib_obj = adreno_ib_check_overlap(gpuaddr, size, type, ib_obj_list);
  130. if (ib_obj) {
  131. adreno_ib_merge_range(ib_obj, gpuaddr, size);
  132. kgsl_mem_entry_put(entry);
  133. } else {
  134. adreno_ib_init_ib_obj(gpuaddr, size, type, entry,
  135. &(ib_obj_list->obj_list[ib_obj_list->num_objs]));
  136. ib_obj_list->num_objs++;
  137. /* Skip reclaim for the memdesc until it is dumped */
  138. entry->memdesc.priv |= KGSL_MEMDESC_SKIP_RECLAIM;
  139. }
  140. return 0;
  141. }
  142. /*
  143. * ib_save_mip_addresses() - Find mip addresses
  144. * @pkt: Pointer to the packet in IB
  145. * @process: The process in which IB is mapped
  146. * @ib_obj_list: List in which any objects found are added
  147. *
  148. * Returns 0 on success else error code
  149. */
  150. static int ib_save_mip_addresses(unsigned int *pkt,
  151. struct kgsl_process_private *process,
  152. struct adreno_ib_object_list *ib_obj_list)
  153. {
  154. int ret = 0;
  155. int num_levels = (pkt[1] >> 22) & 0x03FF;
  156. int i;
  157. unsigned int *hostptr;
  158. struct kgsl_mem_entry *ent;
  159. unsigned int block, type;
  160. int unitsize = 0;
  161. block = (pkt[1] >> 19) & 0x07;
  162. type = pkt[2] & 0x03;
  163. if (type == 0)
  164. unitsize = load_state_unit_sizes[block][0];
  165. else
  166. unitsize = load_state_unit_sizes[block][1];
  167. if (3 == block && 1 == type) {
  168. uint64_t gpuaddr = pkt[2] & 0xFFFFFFFC;
  169. uint64_t size = (num_levels * unitsize) << 2;
  170. ent = kgsl_sharedmem_find(process, gpuaddr);
  171. if (ent == NULL)
  172. return 0;
  173. if (!kgsl_gpuaddr_in_memdesc(&ent->memdesc,
  174. gpuaddr, size)) {
  175. kgsl_mem_entry_put(ent);
  176. return 0;
  177. }
  178. hostptr = kgsl_gpuaddr_to_vaddr(&ent->memdesc, gpuaddr);
  179. if (hostptr != NULL) {
  180. for (i = 0; i < num_levels; i++) {
  181. ret = adreno_ib_add(process, hostptr[i],
  182. SNAPSHOT_GPU_OBJECT_GENERIC,
  183. ib_obj_list);
  184. if (ret)
  185. break;
  186. }
  187. }
  188. kgsl_memdesc_unmap(&ent->memdesc);
  189. kgsl_mem_entry_put(ent);
  190. }
  191. return ret;
  192. }
  193. /*
  194. * ib_parse_load_state() - Parse load state packet
  195. * @pkt: Pointer to the packet in IB
  196. * @process: The pagetable in which the IB is mapped
  197. * @ib_obj_list: List in which any objects found are added
  198. * @ib_parse_vars: VAriable list that store temporary addressses
  199. *
  200. * Parse load state packet found in an IB and add any memory object found to
  201. * a list
  202. * Returns 0 on success else error code
  203. */
  204. static int ib_parse_load_state(unsigned int *pkt,
  205. struct kgsl_process_private *process,
  206. struct adreno_ib_object_list *ib_obj_list,
  207. struct ib_parser_variables *ib_parse_vars)
  208. {
  209. int ret = 0;
  210. int i;
  211. /*
  212. * The object here is to find indirect shaders i.e - shaders loaded from
  213. * GPU memory instead of directly in the command. These should be added
  214. * to the list of memory objects to dump. So look at the load state
  215. * if the block is indirect (source = 4). If so then add the memory
  216. * address to the list. The size of the object differs depending on the
  217. * type per the load_state_unit_sizes array above.
  218. */
  219. if (type3_pkt_size(pkt[0]) < 2)
  220. return 0;
  221. /*
  222. * Anything from 3rd ordinal onwards of packet can be a memory object,
  223. * no need to be fancy about parsing it, just save it if it looks
  224. * like memory
  225. */
  226. for (i = 0; i <= (type3_pkt_size(pkt[0]) - 2); i++) {
  227. ret |= adreno_ib_add(process, pkt[2 + i] & 0xFFFFFFFC,
  228. SNAPSHOT_GPU_OBJECT_GENERIC,
  229. ib_obj_list);
  230. if (ret)
  231. break;
  232. }
  233. /* get the mip addresses */
  234. if (!ret)
  235. ret = ib_save_mip_addresses(pkt, process, ib_obj_list);
  236. return ret;
  237. }
  238. /*
  239. * This opcode sets the base addresses for the visibilty stream buffer and the
  240. * visiblity stream size buffer.
  241. */
  242. static int ib_parse_set_bin_data(unsigned int *pkt,
  243. struct kgsl_process_private *process,
  244. struct adreno_ib_object_list *ib_obj_list,
  245. struct ib_parser_variables *ib_parse_vars)
  246. {
  247. int ret = 0;
  248. if (type3_pkt_size(pkt[0]) < 2)
  249. return 0;
  250. /* Visiblity stream buffer */
  251. ret = adreno_ib_add(process, pkt[1],
  252. SNAPSHOT_GPU_OBJECT_GENERIC, ib_obj_list);
  253. if (ret)
  254. return ret;
  255. /* visiblity stream size buffer (fixed size 8 dwords) */
  256. ret = adreno_ib_add(process, pkt[2],
  257. SNAPSHOT_GPU_OBJECT_GENERIC, ib_obj_list);
  258. return ret;
  259. }
  260. /*
  261. * This opcode writes to GPU memory - if the buffer is written to, there is a
  262. * good chance that it would be valuable to capture in the snapshot, so mark all
  263. * buffers that are written to as frozen
  264. */
  265. static int ib_parse_mem_write(unsigned int *pkt,
  266. struct kgsl_process_private *process,
  267. struct adreno_ib_object_list *ib_obj_list,
  268. struct ib_parser_variables *ib_parse_vars)
  269. {
  270. if (type3_pkt_size(pkt[0]) < 1)
  271. return 0;
  272. /*
  273. * The address is where the data in the rest of this packet is written
  274. * to, but since that might be an offset into the larger buffer we need
  275. * to get the whole thing. Pass a size of 0 tocapture the entire buffer.
  276. */
  277. return adreno_ib_add(process, pkt[1] & 0xFFFFFFFC,
  278. SNAPSHOT_GPU_OBJECT_GENERIC, ib_obj_list);
  279. }
  280. /*
  281. * ib_add_type0_entries() - Add memory objects to list
  282. * @device: The device on which the IB will execute
  283. * @process: The process in which IB is mapped
  284. * @ib_obj_list: The list of gpu objects
  285. * @ib_parse_vars: addresses ranges found in type0 packets
  286. *
  287. * Add memory objects to given list that are found in type0 packets
  288. * Returns 0 on success else 0
  289. */
  290. static int ib_add_type0_entries(struct kgsl_device *device,
  291. struct kgsl_process_private *process,
  292. struct adreno_ib_object_list *ib_obj_list,
  293. struct ib_parser_variables *ib_parse_vars)
  294. {
  295. int ret = 0;
  296. int i;
  297. int vfd_end;
  298. unsigned int mask;
  299. /* First up the visiblity stream buffer */
  300. mask = 0xFFFFFFFF;
  301. for (i = ADRENO_CP_ADDR_VSC_PIPE_DATA_ADDRESS_0;
  302. i < ADRENO_CP_ADDR_VSC_PIPE_DATA_LENGTH_7; i++) {
  303. if (ib_parse_vars->cp_addr_regs[i]) {
  304. ret = adreno_ib_add(process,
  305. ib_parse_vars->cp_addr_regs[i] & mask,
  306. SNAPSHOT_GPU_OBJECT_GENERIC,
  307. ib_obj_list);
  308. if (ret)
  309. return ret;
  310. ib_parse_vars->cp_addr_regs[i] = 0;
  311. ib_parse_vars->cp_addr_regs[i + 1] = 0;
  312. i++;
  313. }
  314. }
  315. vfd_end = ADRENO_CP_ADDR_VFD_FETCH_INSTR_1_15;
  316. for (i = ADRENO_CP_ADDR_VFD_FETCH_INSTR_1_0;
  317. i <= vfd_end; i++) {
  318. if (ib_parse_vars->cp_addr_regs[i]) {
  319. ret = adreno_ib_add(process,
  320. ib_parse_vars->cp_addr_regs[i],
  321. SNAPSHOT_GPU_OBJECT_GENERIC,
  322. ib_obj_list);
  323. if (ret)
  324. return ret;
  325. ib_parse_vars->cp_addr_regs[i] = 0;
  326. }
  327. }
  328. if (ib_parse_vars->cp_addr_regs[ADRENO_CP_ADDR_VSC_SIZE_ADDRESS]) {
  329. ret = adreno_ib_add(process,
  330. ib_parse_vars->cp_addr_regs[
  331. ADRENO_CP_ADDR_VSC_SIZE_ADDRESS] & mask,
  332. SNAPSHOT_GPU_OBJECT_GENERIC, ib_obj_list);
  333. if (ret)
  334. return ret;
  335. ib_parse_vars->cp_addr_regs[
  336. ADRENO_CP_ADDR_VSC_SIZE_ADDRESS] = 0;
  337. }
  338. mask = 0xFFFFFFE0;
  339. for (i = ADRENO_CP_ADDR_SP_VS_PVT_MEM_ADDR;
  340. i <= ADRENO_CP_ADDR_SP_FS_OBJ_START_REG; i++) {
  341. ret = adreno_ib_add(process,
  342. ib_parse_vars->cp_addr_regs[i] & mask,
  343. SNAPSHOT_GPU_OBJECT_GENERIC, ib_obj_list);
  344. if (ret)
  345. return ret;
  346. ib_parse_vars->cp_addr_regs[i] = 0;
  347. }
  348. return ret;
  349. }
  350. /*
  351. * The DRAW_INDX opcode sends a draw initator which starts a draw operation in
  352. * the GPU, so this is the point where all the registers and buffers become
  353. * "valid". The DRAW_INDX may also have an index buffer pointer that should be
  354. * frozen with the others
  355. */
  356. static int ib_parse_draw_indx(struct kgsl_device *device, unsigned int *pkt,
  357. struct kgsl_process_private *process,
  358. struct adreno_ib_object_list *ib_obj_list,
  359. struct ib_parser_variables *ib_parse_vars)
  360. {
  361. int ret = 0;
  362. int i;
  363. int opcode = cp_type3_opcode(pkt[0]);
  364. switch (opcode) {
  365. case CP_DRAW_INDX:
  366. if (type3_pkt_size(pkt[0]) > 3) {
  367. ret = adreno_ib_add(process,
  368. pkt[4], SNAPSHOT_GPU_OBJECT_GENERIC,
  369. ib_obj_list);
  370. }
  371. break;
  372. case CP_DRAW_INDX_OFFSET:
  373. if (type3_pkt_size(pkt[0]) == 6) {
  374. ret = adreno_ib_add(process,
  375. pkt[5], SNAPSHOT_GPU_OBJECT_GENERIC,
  376. ib_obj_list);
  377. }
  378. break;
  379. case CP_DRAW_INDIRECT:
  380. if (type3_pkt_size(pkt[0]) == 2) {
  381. ret = adreno_ib_add(process,
  382. pkt[2], SNAPSHOT_GPU_OBJECT_GENERIC,
  383. ib_obj_list);
  384. }
  385. break;
  386. case CP_DRAW_INDX_INDIRECT:
  387. if (type3_pkt_size(pkt[0]) == 4) {
  388. ret = adreno_ib_add(process,
  389. pkt[2], SNAPSHOT_GPU_OBJECT_GENERIC,
  390. ib_obj_list);
  391. if (ret)
  392. break;
  393. ret = adreno_ib_add(process,
  394. pkt[4], SNAPSHOT_GPU_OBJECT_GENERIC,
  395. ib_obj_list);
  396. }
  397. break;
  398. case CP_DRAW_AUTO:
  399. if (type3_pkt_size(pkt[0]) == 6) {
  400. ret = adreno_ib_add(process,
  401. pkt[3], SNAPSHOT_GPU_OBJECT_GENERIC,
  402. ib_obj_list);
  403. if (ret)
  404. break;
  405. ret = adreno_ib_add(process,
  406. pkt[4], SNAPSHOT_GPU_OBJECT_GENERIC,
  407. ib_obj_list);
  408. }
  409. break;
  410. }
  411. if (ret)
  412. return ret;
  413. /*
  414. * All of the type0 writes are valid at a draw initiator, so freeze
  415. * the various buffers that we are tracking
  416. */
  417. ret = ib_add_type0_entries(device, process, ib_obj_list,
  418. ib_parse_vars);
  419. if (ret)
  420. return ret;
  421. /* Process set draw state command streams if any */
  422. for (i = 0; i < NUM_SET_DRAW_GROUPS; i++) {
  423. if (!ib_parse_vars->set_draw_groups[i].cmd_stream_dwords)
  424. continue;
  425. ret = adreno_ib_find_objs(device, process,
  426. ib_parse_vars->set_draw_groups[i].cmd_stream_addr,
  427. ib_parse_vars->set_draw_groups[i].cmd_stream_dwords,
  428. 0, SNAPSHOT_GPU_OBJECT_DRAW,
  429. ib_obj_list, 2);
  430. if (ret)
  431. break;
  432. }
  433. return ret;
  434. }
  435. /*
  436. * Parse all the type7 opcode packets that may contain important information,
  437. * such as additional GPU buffers to grab or a draw initator
  438. */
  439. static int ib_parse_type7(struct kgsl_device *device, unsigned int *ptr,
  440. struct kgsl_process_private *process,
  441. struct adreno_ib_object_list *ib_obj_list,
  442. struct ib_parser_variables *ib_parse_vars)
  443. {
  444. int opcode = cp_type7_opcode(*ptr);
  445. switch (opcode) {
  446. case CP_SET_DRAW_STATE:
  447. return ib_parse_type7_set_draw_state(device, ptr, process,
  448. ib_obj_list);
  449. }
  450. return 0;
  451. }
  452. /*
  453. * Parse all the type3 opcode packets that may contain important information,
  454. * such as additional GPU buffers to grab or a draw initator
  455. */
  456. static int ib_parse_type3(struct kgsl_device *device, unsigned int *ptr,
  457. struct kgsl_process_private *process,
  458. struct adreno_ib_object_list *ib_obj_list,
  459. struct ib_parser_variables *ib_parse_vars)
  460. {
  461. int opcode = cp_type3_opcode(*ptr);
  462. switch (opcode) {
  463. case CP_LOAD_STATE:
  464. return ib_parse_load_state(ptr, process, ib_obj_list,
  465. ib_parse_vars);
  466. case CP_SET_BIN_DATA:
  467. return ib_parse_set_bin_data(ptr, process, ib_obj_list,
  468. ib_parse_vars);
  469. case CP_MEM_WRITE:
  470. return ib_parse_mem_write(ptr, process, ib_obj_list,
  471. ib_parse_vars);
  472. case CP_DRAW_INDX:
  473. case CP_DRAW_INDX_OFFSET:
  474. case CP_DRAW_INDIRECT:
  475. case CP_DRAW_INDX_INDIRECT:
  476. return ib_parse_draw_indx(device, ptr, process, ib_obj_list,
  477. ib_parse_vars);
  478. case CP_SET_DRAW_STATE:
  479. return ib_parse_set_draw_state(device, ptr, process,
  480. ib_obj_list, ib_parse_vars);
  481. }
  482. return 0;
  483. }
  484. /*
  485. * Parse type0 packets found in the stream. Some of the registers that are
  486. * written are clues for GPU buffers that we need to freeze. Register writes
  487. * are considred valid when a draw initator is called, so just cache the values
  488. * here and freeze them when a CP_DRAW_INDX is seen. This protects against
  489. * needlessly caching buffers that won't be used during a draw call
  490. */
  491. static int ib_parse_type0(struct kgsl_device *device, unsigned int *ptr,
  492. struct kgsl_process_private *process,
  493. struct adreno_ib_object_list *ib_obj_list,
  494. struct ib_parser_variables *ib_parse_vars)
  495. {
  496. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  497. int size = type0_pkt_size(*ptr);
  498. int offset = type0_pkt_offset(*ptr);
  499. int i;
  500. int reg_index;
  501. int ret = 0;
  502. for (i = 0; i < size; i++, offset++) {
  503. /* Visiblity stream buffer */
  504. if (offset >= adreno_cp_parser_getreg(adreno_dev,
  505. ADRENO_CP_ADDR_VSC_PIPE_DATA_ADDRESS_0) &&
  506. offset <= adreno_cp_parser_getreg(adreno_dev,
  507. ADRENO_CP_ADDR_VSC_PIPE_DATA_LENGTH_7)) {
  508. reg_index = adreno_cp_parser_regindex(
  509. adreno_dev, offset,
  510. ADRENO_CP_ADDR_VSC_PIPE_DATA_ADDRESS_0,
  511. ADRENO_CP_ADDR_VSC_PIPE_DATA_LENGTH_7);
  512. if (reg_index >= 0)
  513. ib_parse_vars->cp_addr_regs[reg_index] =
  514. ptr[i + 1];
  515. continue;
  516. } else if ((offset >= adreno_cp_parser_getreg(adreno_dev,
  517. ADRENO_CP_ADDR_VFD_FETCH_INSTR_1_0)) &&
  518. (offset <= adreno_cp_parser_getreg(adreno_dev,
  519. ADRENO_CP_ADDR_VFD_FETCH_INSTR_1_15))) {
  520. reg_index = adreno_cp_parser_regindex(adreno_dev,
  521. offset,
  522. ADRENO_CP_ADDR_VFD_FETCH_INSTR_1_0,
  523. ADRENO_CP_ADDR_VFD_FETCH_INSTR_1_15);
  524. if (reg_index >= 0)
  525. ib_parse_vars->cp_addr_regs[reg_index] =
  526. ptr[i + 1];
  527. continue;
  528. } else if ((offset >= adreno_cp_parser_getreg(adreno_dev,
  529. ADRENO_CP_ADDR_VFD_FETCH_INSTR_1_16)) &&
  530. (offset <= adreno_cp_parser_getreg(adreno_dev,
  531. ADRENO_CP_ADDR_VFD_FETCH_INSTR_1_31))) {
  532. reg_index = adreno_cp_parser_regindex(adreno_dev,
  533. offset,
  534. ADRENO_CP_ADDR_VFD_FETCH_INSTR_1_16,
  535. ADRENO_CP_ADDR_VFD_FETCH_INSTR_1_31);
  536. if (reg_index >= 0)
  537. ib_parse_vars->cp_addr_regs[reg_index] =
  538. ptr[i + 1];
  539. continue;
  540. } else {
  541. if (offset ==
  542. adreno_cp_parser_getreg(adreno_dev,
  543. ADRENO_CP_ADDR_VSC_SIZE_ADDRESS))
  544. ib_parse_vars->cp_addr_regs[
  545. ADRENO_CP_ADDR_VSC_SIZE_ADDRESS] =
  546. ptr[i + 1];
  547. else if (offset == adreno_cp_parser_getreg(adreno_dev,
  548. ADRENO_CP_ADDR_SP_VS_PVT_MEM_ADDR))
  549. ib_parse_vars->cp_addr_regs[
  550. ADRENO_CP_ADDR_SP_VS_PVT_MEM_ADDR] =
  551. ptr[i + 1];
  552. else if (offset == adreno_cp_parser_getreg(adreno_dev,
  553. ADRENO_CP_ADDR_SP_FS_PVT_MEM_ADDR))
  554. ib_parse_vars->cp_addr_regs[
  555. ADRENO_CP_ADDR_SP_FS_PVT_MEM_ADDR] =
  556. ptr[i + 1];
  557. else if (offset == adreno_cp_parser_getreg(adreno_dev,
  558. ADRENO_CP_ADDR_SP_VS_OBJ_START_REG))
  559. ib_parse_vars->cp_addr_regs[
  560. ADRENO_CP_ADDR_SP_VS_OBJ_START_REG] =
  561. ptr[i + 1];
  562. else if (offset == adreno_cp_parser_getreg(adreno_dev,
  563. ADRENO_CP_ADDR_SP_FS_OBJ_START_REG))
  564. ib_parse_vars->cp_addr_regs[
  565. ADRENO_CP_ADDR_SP_FS_OBJ_START_REG] =
  566. ptr[i + 1];
  567. else if ((offset == adreno_cp_parser_getreg(adreno_dev,
  568. ADRENO_CP_UCHE_INVALIDATE0)) ||
  569. (offset == adreno_cp_parser_getreg(adreno_dev,
  570. ADRENO_CP_UCHE_INVALIDATE1))) {
  571. ret = adreno_ib_add(process,
  572. ptr[i + 1] & 0xFFFFFFC0,
  573. SNAPSHOT_GPU_OBJECT_GENERIC,
  574. ib_obj_list);
  575. if (ret)
  576. break;
  577. }
  578. }
  579. }
  580. return ret;
  581. }
  582. static int ib_parse_type7_set_draw_state(struct kgsl_device *device,
  583. unsigned int *ptr,
  584. struct kgsl_process_private *process,
  585. struct adreno_ib_object_list *ib_obj_list)
  586. {
  587. int size = type7_pkt_size(*ptr);
  588. int i;
  589. int ret = 0;
  590. int flags;
  591. uint64_t cmd_stream_dwords;
  592. uint64_t cmd_stream_addr;
  593. /*
  594. * size is the size of the packet that does not include the DWORD
  595. * for the packet header, we only want to loop here through the
  596. * packet parameters from ptr[1] till ptr[size] where ptr[0] is the
  597. * packet header. In each loop we look at 3 DWORDS hence increment
  598. * loop counter by 3 always
  599. */
  600. for (i = 1; i <= size; i += 3) {
  601. /* take action based on flags */
  602. flags = (ptr[i] & 0x000F0000) >> 16;
  603. /*
  604. * dirty flag or no flags both mean we need to load it for
  605. * next draw. No flags is used when the group is activated
  606. * or initialized for the first time in the IB
  607. */
  608. if (flags & 0x1 || !flags) {
  609. cmd_stream_dwords = ptr[i] & 0x0000FFFF;
  610. cmd_stream_addr = ptr[i + 2];
  611. cmd_stream_addr = cmd_stream_addr << 32 | ptr[i + 1];
  612. if (cmd_stream_dwords)
  613. ret = adreno_ib_find_objs(device, process,
  614. cmd_stream_addr, cmd_stream_dwords,
  615. 0, SNAPSHOT_GPU_OBJECT_DRAW,
  616. ib_obj_list, 2);
  617. if (ret)
  618. break;
  619. continue;
  620. }
  621. /* load immediate */
  622. if (flags & 0x8) {
  623. uint64_t gpuaddr = ptr[i + 2];
  624. gpuaddr = gpuaddr << 32 | ptr[i + 1];
  625. ret = adreno_ib_find_objs(device, process,
  626. gpuaddr, (ptr[i] & 0x0000FFFF),
  627. 0, SNAPSHOT_GPU_OBJECT_IB,
  628. ib_obj_list, 2);
  629. if (ret)
  630. break;
  631. }
  632. }
  633. return ret;
  634. }
  635. static int ib_parse_set_draw_state(struct kgsl_device *device,
  636. unsigned int *ptr,
  637. struct kgsl_process_private *process,
  638. struct adreno_ib_object_list *ib_obj_list,
  639. struct ib_parser_variables *ib_parse_vars)
  640. {
  641. int size = type0_pkt_size(*ptr);
  642. int i;
  643. int grp_id;
  644. int ret = 0;
  645. int flags;
  646. struct set_draw_state *group;
  647. /*
  648. * size is the size of the packet that does not include the DWORD
  649. * for the packet header, we only want to loop here through the
  650. * packet parameters from ptr[1] till ptr[size] where ptr[0] is the
  651. * packet header. In each loop we look at 2 DWORDS hence increment
  652. * loop counter by 2 always
  653. */
  654. for (i = 1; i <= size; i += 2) {
  655. grp_id = (ptr[i] & 0x1F000000) >> 24;
  656. /* take action based on flags */
  657. flags = (ptr[i] & 0x000F0000) >> 16;
  658. /* Disable all groups */
  659. if (flags & 0x4) {
  660. int j;
  661. for (j = 0; j < NUM_SET_DRAW_GROUPS; j++) {
  662. group = &(ib_parse_vars->set_draw_groups[j]);
  663. group->cmd_stream_dwords = 0;
  664. }
  665. continue;
  666. }
  667. /* disable flag */
  668. if (flags & 0x2) {
  669. group = &(ib_parse_vars->set_draw_groups[grp_id]);
  670. group->cmd_stream_dwords = 0;
  671. continue;
  672. }
  673. /*
  674. * dirty flag or no flags both mean we need to load it for
  675. * next draw. No flags is used when the group is activated
  676. * or initialized for the first time in the IB
  677. */
  678. if (flags & 0x1 || !flags) {
  679. group = &(ib_parse_vars->set_draw_groups[grp_id]);
  680. group->cmd_stream_dwords = ptr[i] & 0x0000FFFF;
  681. group->cmd_stream_addr = ptr[i + 1];
  682. continue;
  683. }
  684. /* load immediate */
  685. if (flags & 0x8) {
  686. ret = adreno_ib_find_objs(device, process,
  687. ptr[i + 1], (ptr[i] & 0x0000FFFF),
  688. 0, SNAPSHOT_GPU_OBJECT_IB,
  689. ib_obj_list, 2);
  690. if (ret)
  691. break;
  692. }
  693. }
  694. return ret;
  695. }
  696. /*
  697. * adreno_cp_parse_ib2() - Wrapper function around IB2 parsing
  698. * @device: Device pointer
  699. * @process: Process in which the IB is allocated
  700. * @gpuaddr: IB2 gpuaddr
  701. * @dwords: IB2 size in dwords
  702. * @ib2base: Base address of active IB2
  703. * @ib_obj_list: List of objects found in IB
  704. * @ib_level: The level from which function is called, either from IB1 or IB2
  705. *
  706. * Function does some checks to ensure that IB2 parsing is called from IB1
  707. * and then calls the function to find objects in IB2.
  708. */
  709. static int adreno_cp_parse_ib2(struct kgsl_device *device,
  710. struct kgsl_process_private *process,
  711. uint64_t gpuaddr, uint64_t dwords, uint64_t ib2base,
  712. struct adreno_ib_object_list *ib_obj_list,
  713. int ib_level)
  714. {
  715. int i;
  716. /*
  717. * We can only expect an IB2 in IB1, if we are
  718. * already processing an IB2 then return error
  719. */
  720. if (ib_level == 2)
  721. return -EINVAL;
  722. /* Save current IB2 statically */
  723. if (ib2base == gpuaddr)
  724. kgsl_snapshot_push_object(device, process, gpuaddr, dwords);
  725. /*
  726. * only try to find sub objects iff this IB has
  727. * not been processed already
  728. */
  729. for (i = 0; i < ib_obj_list->num_objs; i++) {
  730. struct adreno_ib_object *ib_obj = &(ib_obj_list->obj_list[i]);
  731. if ((ib_obj->snapshot_obj_type == SNAPSHOT_GPU_OBJECT_IB) &&
  732. (gpuaddr >= ib_obj->gpuaddr) &&
  733. (gpuaddr + dwords * sizeof(unsigned int) <=
  734. ib_obj->gpuaddr + ib_obj->size))
  735. return 0;
  736. }
  737. return adreno_ib_find_objs(device, process, gpuaddr, dwords, ib2base,
  738. SNAPSHOT_GPU_OBJECT_IB, ib_obj_list, 2);
  739. }
  740. /*
  741. * adreno_ib_find_objs() - Find all IB objects in a given IB
  742. * @device: The device pointer on which the IB executes
  743. * @process: The process in which the IB and all contained objects are mapped.
  744. * @gpuaddr: The gpu address of the IB
  745. * @ib2base: IB2 base address
  746. * @dwords: Size of ib in dwords
  747. * @obj_type: The object type can be either an IB or a draw state sequence
  748. * @ib_obj_list: The list in which the IB and the objects in it are added.
  749. * @ib_level: Indicates if IB1 or IB2 is being processed
  750. *
  751. * Finds all IB objects in a given IB and puts then in a list. Can be called
  752. * recursively for the IB2's in the IB1's
  753. * Returns 0 on success else error code
  754. */
  755. static int adreno_ib_find_objs(struct kgsl_device *device,
  756. struct kgsl_process_private *process,
  757. uint64_t gpuaddr, uint64_t dwords,
  758. uint64_t ib2base, int obj_type,
  759. struct adreno_ib_object_list *ib_obj_list,
  760. int ib_level)
  761. {
  762. int ret = 0;
  763. uint64_t rem = dwords;
  764. int i;
  765. struct ib_parser_variables ib_parse_vars;
  766. unsigned int *src;
  767. struct adreno_ib_object *ib_obj;
  768. struct kgsl_mem_entry *entry;
  769. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  770. /* check that this IB is not already on list */
  771. for (i = 0; i < ib_obj_list->num_objs; i++) {
  772. ib_obj = &(ib_obj_list->obj_list[i]);
  773. if ((obj_type == ib_obj->snapshot_obj_type) &&
  774. (ib_obj->gpuaddr <= gpuaddr) &&
  775. ((ib_obj->gpuaddr + ib_obj->size) >=
  776. (gpuaddr + (dwords << 2))))
  777. return 0;
  778. }
  779. entry = kgsl_sharedmem_find(process, gpuaddr);
  780. if (!entry)
  781. return -EINVAL;
  782. if (!kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, (dwords << 2))) {
  783. kgsl_mem_entry_put(entry);
  784. return -EINVAL;
  785. }
  786. src = kgsl_gpuaddr_to_vaddr(&entry->memdesc, gpuaddr);
  787. if (!src) {
  788. kgsl_mem_entry_put(entry);
  789. return -EINVAL;
  790. }
  791. memset(&ib_parse_vars, 0, sizeof(struct ib_parser_variables));
  792. ret = adreno_ib_add(process, gpuaddr, obj_type, ib_obj_list);
  793. if (ret)
  794. goto done;
  795. for (i = 0; rem > 0; rem--, i++) {
  796. int pktsize;
  797. if (pkt_is_type0(src[i]))
  798. pktsize = type0_pkt_size(src[i]);
  799. else if (pkt_is_type3(src[i]))
  800. pktsize = type3_pkt_size(src[i]);
  801. else if (pkt_is_type4(src[i]))
  802. pktsize = type4_pkt_size(src[i]);
  803. else if (pkt_is_type7(src[i]))
  804. pktsize = type7_pkt_size(src[i]);
  805. /*
  806. * If the packet isn't a type 1, type 3, type 4 or type 7 then
  807. * don't bother parsing it - it is likely corrupted
  808. */
  809. else
  810. break;
  811. if (((pkt_is_type0(src[i]) || pkt_is_type3(src[i])) && !pktsize)
  812. || ((pktsize + 1) > rem))
  813. break;
  814. if (pkt_is_type3(src[i])) {
  815. if (adreno_cmd_is_ib(adreno_dev, src[i])) {
  816. uint64_t gpuaddrib2 = src[i + 1];
  817. uint64_t size = src[i + 2];
  818. ret = adreno_cp_parse_ib2(device, process,
  819. gpuaddrib2, size, ib2base,
  820. ib_obj_list, ib_level);
  821. if (ret)
  822. goto done;
  823. } else {
  824. ret = ib_parse_type3(device, &src[i], process,
  825. ib_obj_list,
  826. &ib_parse_vars);
  827. /*
  828. * If the parse function failed (probably
  829. * because of a bad decode) then bail out and
  830. * just capture the binary IB data
  831. */
  832. if (ret)
  833. goto done;
  834. }
  835. }
  836. else if (pkt_is_type7(src[i])) {
  837. if (adreno_cmd_is_ib(adreno_dev, src[i])) {
  838. uint64_t size = src[i + 3];
  839. uint64_t gpuaddrib2 = src[i + 2];
  840. gpuaddrib2 = gpuaddrib2 << 32 | src[i + 1];
  841. ret = adreno_cp_parse_ib2(device, process,
  842. gpuaddrib2, size, ib2base,
  843. ib_obj_list, ib_level);
  844. if (ret)
  845. goto done;
  846. } else {
  847. ret = ib_parse_type7(device, &src[i], process,
  848. ib_obj_list,
  849. &ib_parse_vars);
  850. /*
  851. * If the parse function failed (probably
  852. * because of a bad decode) then bail out and
  853. * just capture the binary IB data
  854. */
  855. if (ret)
  856. goto done;
  857. }
  858. }
  859. else if (pkt_is_type0(src[i])) {
  860. ret = ib_parse_type0(device, &src[i], process,
  861. ib_obj_list, &ib_parse_vars);
  862. if (ret)
  863. goto done;
  864. }
  865. i += pktsize;
  866. rem -= pktsize;
  867. }
  868. done:
  869. /*
  870. * For set draw objects there may not be a draw_indx packet at its end
  871. * to signal that we need to save the found objects in it, so just save
  872. * it here.
  873. */
  874. if (!ret && SNAPSHOT_GPU_OBJECT_DRAW == obj_type)
  875. ret = ib_add_type0_entries(device, process, ib_obj_list,
  876. &ib_parse_vars);
  877. kgsl_memdesc_unmap(&entry->memdesc);
  878. kgsl_mem_entry_put(entry);
  879. return ret;
  880. }
  881. /*
  882. * adreno_ib_create_object_list() - Find all the memory objects in IB
  883. * @device: The device pointer on which the IB executes
  884. * @process: The process in which the IB and all contained objects are mapped
  885. * @gpuaddr: The gpu address of the IB
  886. * @dwords: Size of ib in dwords
  887. * @ib2base: Base address of active IB2
  888. * @ib_obj_list: The list in which the IB and the objects in it are added.
  889. *
  890. * Find all the memory objects that an IB needs for execution and place
  891. * them in a list including the IB.
  892. * Returns the ib object list. On success 0 is returned, on failure error
  893. * code is returned along with number of objects that was saved before
  894. * error occurred. If no objects found then the list pointer is set to
  895. * NULL.
  896. */
  897. int adreno_ib_create_object_list(struct kgsl_device *device,
  898. struct kgsl_process_private *process,
  899. uint64_t gpuaddr, uint64_t dwords, uint64_t ib2base,
  900. struct adreno_ib_object_list **out_ib_obj_list)
  901. {
  902. int ret = 0;
  903. struct adreno_ib_object_list *ib_obj_list;
  904. if (!out_ib_obj_list)
  905. return -EINVAL;
  906. *out_ib_obj_list = NULL;
  907. ib_obj_list = kzalloc(sizeof(*ib_obj_list), GFP_KERNEL);
  908. if (!ib_obj_list)
  909. return -ENOMEM;
  910. ib_obj_list->obj_list = vmalloc(MAX_IB_OBJS *
  911. sizeof(struct adreno_ib_object));
  912. if (!ib_obj_list->obj_list) {
  913. kfree(ib_obj_list);
  914. return -ENOMEM;
  915. }
  916. ret = adreno_ib_find_objs(device, process, gpuaddr, dwords, ib2base,
  917. SNAPSHOT_GPU_OBJECT_IB, ib_obj_list, 1);
  918. /* Even if there was an error return the remaining objects found */
  919. if (ib_obj_list->num_objs)
  920. *out_ib_obj_list = ib_obj_list;
  921. return ret;
  922. }
  923. /*
  924. * adreno_ib_destroy_obj_list() - Destroy an ib object list
  925. * @ib_obj_list: List to destroy
  926. *
  927. * Free up all resources used by an ib_obj_list
  928. */
  929. void adreno_ib_destroy_obj_list(struct adreno_ib_object_list *ib_obj_list)
  930. {
  931. int i;
  932. if (!ib_obj_list)
  933. return;
  934. for (i = 0; i < ib_obj_list->num_objs; i++) {
  935. if (ib_obj_list->obj_list[i].entry)
  936. kgsl_mem_entry_put(ib_obj_list->obj_list[i].entry);
  937. }
  938. vfree(ib_obj_list->obj_list);
  939. kfree(ib_obj_list);
  940. }