adreno_profile.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/ctype.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/sched/signal.h>
  9. #include "adreno.h"
  10. #include "adreno_hwsched.h"
  11. #include "adreno_profile.h"
  12. #include "adreno_pm4types.h"
  13. #include "adreno_ringbuffer.h"
  14. #define ASSIGNS_STR_FORMAT "%.8s:%u "
  15. /*
  16. * Raw Data for processing later:
  17. * : 3 - timestamp, count, context id
  18. * [per counter] - data for each counter
  19. * : 1 - Register offset
  20. * : 2 - Pre IB register hi/lo value
  21. * : 2 - Post IB register hi/lo value
  22. * [per counter end]
  23. */
  24. #define SIZE_DATA(cnt) (6 + (cnt) * 5)
  25. /*
  26. * Pre-IB command size (in dwords):
  27. * : 2 - NOP start identifier
  28. * : 4 - timestamp
  29. * : 4 - count
  30. * : 4 - context id
  31. * : 4 - pid
  32. * : 4 - tid
  33. * : 4 - type
  34. * [loop count start] - for each counter to watch
  35. * : 4 - Register offset
  36. * : 4 - Register read lo
  37. * : 4 - Register read high
  38. * [loop end]
  39. * : 2 - NOP end identifier
  40. */
  41. #define SIZE_PREIB(cnt) (28 + (cnt) * 12)
  42. /*
  43. * Post-IB command size (in dwords):
  44. * : 2 - NOP start identifier
  45. * [loop count start] - for each counter to watch
  46. * : 4 - Register read lo
  47. * : 4 - Register read high
  48. * [loop end]
  49. * : 2 - NOP end identifier
  50. */
  51. #define SIZE_POSTIB(cnt) (4 + (cnt) * 8)
  52. /* Counter data + Pre size + post size = total size */
  53. #define SIZE_SHARED_ENTRY(cnt) (SIZE_DATA(cnt) + SIZE_PREIB(cnt) \
  54. + SIZE_POSTIB(cnt))
  55. /*
  56. * Space for following string :"%u %u %u %.5s %u "
  57. * [count iterations]: "%.8s:%u %llu %llu%c"
  58. */
  59. #define SIZE_PIPE_ENTRY(cnt) (50 + (cnt) * 62)
  60. #define SIZE_LOG_ENTRY(cnt) (6 + (cnt) * 5)
  61. static inline uint _ib_cmd_mem_write(struct adreno_device *adreno_dev,
  62. uint *cmds, uint64_t gpuaddr, uint val, uint *off)
  63. {
  64. unsigned int *start = cmds;
  65. *cmds++ = cp_mem_packet(adreno_dev, CP_MEM_WRITE, 2, 1);
  66. cmds += cp_gpuaddr(adreno_dev, cmds, gpuaddr);
  67. *cmds++ = val;
  68. *off += sizeof(unsigned int);
  69. return cmds - start;
  70. }
  71. static inline uint _ib_cmd_reg_to_mem(struct adreno_device *adreno_dev,
  72. uint *cmds, uint64_t gpuaddr, uint val, uint *off)
  73. {
  74. unsigned int *start = cmds;
  75. *cmds++ = cp_mem_packet(adreno_dev, CP_REG_TO_MEM, 2, 1);
  76. *cmds++ = val;
  77. cmds += cp_gpuaddr(adreno_dev, cmds, gpuaddr);
  78. *off += sizeof(unsigned int);
  79. return cmds - start;
  80. }
  81. static u64 _build_pre_ib_cmds(struct adreno_device *adreno_dev,
  82. struct adreno_profile *profile,
  83. unsigned int head, unsigned int timestamp,
  84. struct adreno_context *drawctxt,
  85. u32 *dwords)
  86. {
  87. struct adreno_profile_assigns_list *entry;
  88. unsigned int *start, *ibcmds;
  89. unsigned int count = profile->assignment_count;
  90. uint64_t gpuaddr = profile->shared_buffer->gpuaddr;
  91. unsigned int ib_offset = head + SIZE_DATA(count);
  92. unsigned int data_offset = head * sizeof(unsigned int);
  93. ibcmds = ib_offset + ((unsigned int *) profile->shared_buffer->hostptr);
  94. start = ibcmds;
  95. ibcmds += cp_identifier(adreno_dev, ibcmds, START_PROFILE_IDENTIFIER);
  96. /*
  97. * Write ringbuffer commands to save the following to memory:
  98. * timestamp, count, context_id, pid, tid, context type
  99. */
  100. ibcmds += _ib_cmd_mem_write(adreno_dev, ibcmds, gpuaddr + data_offset,
  101. timestamp, &data_offset);
  102. ibcmds += _ib_cmd_mem_write(adreno_dev, ibcmds, gpuaddr + data_offset,
  103. profile->assignment_count, &data_offset);
  104. ibcmds += _ib_cmd_mem_write(adreno_dev, ibcmds, gpuaddr + data_offset,
  105. drawctxt->base.id, &data_offset);
  106. ibcmds += _ib_cmd_mem_write(adreno_dev, ibcmds, gpuaddr + data_offset,
  107. pid_nr(drawctxt->base.proc_priv->pid), &data_offset);
  108. ibcmds += _ib_cmd_mem_write(adreno_dev, ibcmds, gpuaddr + data_offset,
  109. drawctxt->base.tid, &data_offset);
  110. ibcmds += _ib_cmd_mem_write(adreno_dev, ibcmds, gpuaddr + data_offset,
  111. drawctxt->type, &data_offset);
  112. /* loop for each countable assigned */
  113. list_for_each_entry(entry, &profile->assignments_list, list) {
  114. ibcmds += _ib_cmd_mem_write(adreno_dev, ibcmds,
  115. gpuaddr + data_offset, entry->offset,
  116. &data_offset);
  117. ibcmds += _ib_cmd_reg_to_mem(adreno_dev, ibcmds,
  118. gpuaddr + data_offset, entry->offset,
  119. &data_offset);
  120. ibcmds += _ib_cmd_reg_to_mem(adreno_dev, ibcmds,
  121. gpuaddr + data_offset, entry->offset_hi,
  122. &data_offset);
  123. /* skip over post_ib counter data */
  124. data_offset += sizeof(unsigned int) * 2;
  125. }
  126. ibcmds += cp_identifier(adreno_dev, ibcmds, END_PROFILE_IDENTIFIER);
  127. *dwords = (ibcmds - start);
  128. return profile->shared_buffer->gpuaddr + (ib_offset * sizeof(u32));
  129. }
  130. static u64 _build_post_ib_cmds(struct adreno_device *adreno_dev,
  131. struct adreno_profile *profile, unsigned int head,
  132. u32 *dwords)
  133. {
  134. struct adreno_profile_assigns_list *entry;
  135. unsigned int *start, *ibcmds;
  136. unsigned int count = profile->assignment_count;
  137. uint64_t gpuaddr = profile->shared_buffer->gpuaddr;
  138. unsigned int ib_offset = head + SIZE_DATA(count) + SIZE_PREIB(count);
  139. unsigned int data_offset = head * sizeof(unsigned int);
  140. ibcmds = ib_offset + ((unsigned int *) profile->shared_buffer->hostptr);
  141. start = ibcmds;
  142. /* start of profile identifier */
  143. ibcmds += cp_identifier(adreno_dev, ibcmds, START_PROFILE_IDENTIFIER);
  144. /* skip over pre_ib preamble */
  145. data_offset += sizeof(unsigned int) * 6;
  146. /* loop for each countable assigned */
  147. list_for_each_entry(entry, &profile->assignments_list, list) {
  148. /* skip over pre_ib counter data */
  149. data_offset += sizeof(unsigned int) * 3;
  150. ibcmds += _ib_cmd_reg_to_mem(adreno_dev, ibcmds,
  151. gpuaddr + data_offset, entry->offset,
  152. &data_offset);
  153. ibcmds += _ib_cmd_reg_to_mem(adreno_dev, ibcmds,
  154. gpuaddr + data_offset, entry->offset_hi,
  155. &data_offset);
  156. }
  157. /* end of profile identifier */
  158. ibcmds += cp_identifier(adreno_dev, ibcmds, END_PROFILE_IDENTIFIER);
  159. *dwords = (ibcmds - start);
  160. return profile->shared_buffer->gpuaddr + (ib_offset * sizeof(u32));
  161. }
  162. static bool shared_buf_empty(struct adreno_profile *profile)
  163. {
  164. if (profile->shared_buffer->hostptr == NULL ||
  165. profile->shared_buffer->size == 0)
  166. return true;
  167. if (profile->shared_head == profile->shared_tail)
  168. return true;
  169. return false;
  170. }
  171. static inline void shared_buf_inc(unsigned int max_size,
  172. unsigned int *offset, size_t inc)
  173. {
  174. *offset = (*offset + inc) % max_size;
  175. }
  176. static inline void log_buf_wrapcnt(unsigned int cnt, uintptr_t *off)
  177. {
  178. *off = (*off + cnt) % ADRENO_PROFILE_LOG_BUF_SIZE_DWORDS;
  179. }
  180. static inline void log_buf_wrapinc_len(unsigned int *profile_log_buffer,
  181. unsigned int **ptr, unsigned int len)
  182. {
  183. *ptr += len;
  184. if (*ptr >= (profile_log_buffer +
  185. ADRENO_PROFILE_LOG_BUF_SIZE_DWORDS))
  186. *ptr -= ADRENO_PROFILE_LOG_BUF_SIZE_DWORDS;
  187. }
  188. static inline void log_buf_wrapinc(unsigned int *profile_log_buffer,
  189. unsigned int **ptr)
  190. {
  191. log_buf_wrapinc_len(profile_log_buffer, ptr, 1);
  192. }
  193. static inline unsigned int log_buf_available(struct adreno_profile *profile,
  194. unsigned int *head_ptr)
  195. {
  196. uintptr_t tail, head;
  197. tail = (uintptr_t) profile->log_tail -
  198. (uintptr_t) profile->log_buffer;
  199. head = (uintptr_t)head_ptr - (uintptr_t) profile->log_buffer;
  200. if (tail > head)
  201. return (tail - head) / sizeof(uintptr_t);
  202. else
  203. return ADRENO_PROFILE_LOG_BUF_SIZE_DWORDS - ((head - tail) /
  204. sizeof(uintptr_t));
  205. }
  206. static inline unsigned int shared_buf_available(struct adreno_profile *profile)
  207. {
  208. if (profile->shared_tail > profile->shared_head)
  209. return profile->shared_tail - profile->shared_head;
  210. else
  211. return profile->shared_size -
  212. (profile->shared_head - profile->shared_tail);
  213. }
  214. static struct adreno_profile_assigns_list *_find_assignment_by_offset(
  215. struct adreno_profile *profile, unsigned int offset)
  216. {
  217. struct adreno_profile_assigns_list *entry;
  218. list_for_each_entry(entry, &profile->assignments_list, list) {
  219. if (entry->offset == offset)
  220. return entry;
  221. }
  222. return NULL;
  223. }
  224. static bool _in_assignments_list(struct adreno_profile *profile,
  225. unsigned int groupid, unsigned int countable)
  226. {
  227. struct adreno_profile_assigns_list *entry;
  228. list_for_each_entry(entry, &profile->assignments_list, list) {
  229. if (entry->groupid == groupid && entry->countable ==
  230. countable)
  231. return true;
  232. }
  233. return false;
  234. }
  235. static bool _add_to_assignments_list(struct adreno_profile *profile,
  236. const char *str, unsigned int groupid, unsigned int countable,
  237. unsigned int offset, unsigned int offset_hi)
  238. {
  239. struct adreno_profile_assigns_list *entry;
  240. /* first make sure we can alloc memory */
  241. entry = kmalloc(sizeof(struct adreno_profile_assigns_list), GFP_KERNEL);
  242. if (!entry)
  243. return false;
  244. list_add_tail(&entry->list, &profile->assignments_list);
  245. entry->countable = countable;
  246. entry->groupid = groupid;
  247. entry->offset = offset;
  248. entry->offset_hi = offset_hi;
  249. strscpy(entry->name, str, sizeof(entry->name));
  250. profile->assignment_count++;
  251. return true;
  252. }
  253. static bool results_available(struct adreno_device *adreno_dev,
  254. struct adreno_profile *profile, unsigned int *shared_buf_tail)
  255. {
  256. unsigned int global_eop;
  257. unsigned int off = profile->shared_tail;
  258. unsigned int *shared_ptr;
  259. unsigned int ts, cnt;
  260. int ts_cmp;
  261. if (IS_ERR(profile->shared_buffer))
  262. return false;
  263. /*
  264. * If shared_buffer empty or Memstore EOP timestamp is less than
  265. * outstanding counter buffer timestamps then no results available
  266. */
  267. if (shared_buf_empty(profile))
  268. return false;
  269. shared_ptr = (unsigned int *)profile->shared_buffer->hostptr;
  270. if (adreno_rb_readtimestamp(adreno_dev,
  271. adreno_dev->cur_rb,
  272. KGSL_TIMESTAMP_RETIRED, &global_eop))
  273. return false;
  274. do {
  275. cnt = *(shared_ptr + off + 1);
  276. if (cnt == 0)
  277. return false;
  278. ts = *(shared_ptr + off);
  279. ts_cmp = timestamp_cmp(ts, global_eop);
  280. if (ts_cmp >= 0) {
  281. *shared_buf_tail = off;
  282. if (off == profile->shared_tail)
  283. return false;
  284. else
  285. return true;
  286. }
  287. shared_buf_inc(profile->shared_size, &off,
  288. SIZE_SHARED_ENTRY(cnt));
  289. } while (off != profile->shared_head);
  290. *shared_buf_tail = profile->shared_head;
  291. return true;
  292. }
  293. static void transfer_results(struct adreno_profile *profile,
  294. unsigned int shared_buf_tail)
  295. {
  296. unsigned int buf_off;
  297. unsigned int ts, cnt, ctxt_id, pid, tid, client_type;
  298. unsigned int *ptr = (unsigned int *) profile->shared_buffer->hostptr;
  299. unsigned int *log_ptr, *log_base;
  300. struct adreno_profile_assigns_list *assigns_list;
  301. int i, tmp_tail;
  302. log_ptr = profile->log_head;
  303. log_base = profile->log_buffer;
  304. if (log_ptr == NULL)
  305. return;
  306. /*
  307. * go through counter buffers and format for write into log_buffer
  308. * if log buffer doesn't have space just overwrite it circularly
  309. * shared_buf is guaranteed to not wrap within an entry so can use
  310. * ptr increment
  311. */
  312. while (profile->shared_tail != shared_buf_tail) {
  313. buf_off = profile->shared_tail;
  314. /*
  315. * format: timestamp, count, context_id
  316. * count entries: pc_off, pc_start, pc_end
  317. */
  318. ts = *(ptr + buf_off++);
  319. cnt = *(ptr + buf_off++);
  320. ctxt_id = *(ptr + buf_off++);
  321. pid = *(ptr + buf_off++);
  322. tid = *(ptr + buf_off++);
  323. client_type = *(ptr + buf_off++);
  324. /*
  325. * if entry overwrites the tail of log_buffer then adjust tail
  326. * ptr to make room for the new entry, discarding old entry
  327. */
  328. while (log_buf_available(profile, log_ptr) <=
  329. SIZE_LOG_ENTRY(cnt)) {
  330. unsigned int size_tail;
  331. uintptr_t boff;
  332. size_tail = SIZE_LOG_ENTRY(0xffff &
  333. *(profile->log_tail));
  334. boff = ((uintptr_t) profile->log_tail -
  335. (uintptr_t) log_base) / sizeof(uintptr_t);
  336. log_buf_wrapcnt(size_tail, &boff);
  337. profile->log_tail = log_base + boff;
  338. }
  339. *log_ptr = cnt;
  340. log_buf_wrapinc(log_base, &log_ptr);
  341. *log_ptr = client_type;
  342. log_buf_wrapinc(log_base, &log_ptr);
  343. *log_ptr = pid;
  344. log_buf_wrapinc(log_base, &log_ptr);
  345. *log_ptr = tid;
  346. log_buf_wrapinc(log_base, &log_ptr);
  347. *log_ptr = ctxt_id;
  348. log_buf_wrapinc(log_base, &log_ptr);
  349. *log_ptr = ts;
  350. log_buf_wrapinc(log_base, &log_ptr);
  351. for (i = 0; i < cnt; i++) {
  352. assigns_list = _find_assignment_by_offset(
  353. profile, *(ptr + buf_off++));
  354. if (assigns_list == NULL) {
  355. *log_ptr = (unsigned int) -1;
  356. shared_buf_inc(profile->shared_size,
  357. &profile->shared_tail,
  358. SIZE_SHARED_ENTRY(cnt));
  359. goto err;
  360. } else {
  361. *log_ptr = assigns_list->groupid << 16 |
  362. (assigns_list->countable & 0xffff);
  363. }
  364. log_buf_wrapinc(log_base, &log_ptr);
  365. *log_ptr = *(ptr + buf_off++); /* perf cntr start hi */
  366. log_buf_wrapinc(log_base, &log_ptr);
  367. *log_ptr = *(ptr + buf_off++); /* perf cntr start lo */
  368. log_buf_wrapinc(log_base, &log_ptr);
  369. *log_ptr = *(ptr + buf_off++); /* perf cntr end hi */
  370. log_buf_wrapinc(log_base, &log_ptr);
  371. *log_ptr = *(ptr + buf_off++); /* perf cntr end lo */
  372. log_buf_wrapinc(log_base, &log_ptr);
  373. }
  374. tmp_tail = profile->shared_tail;
  375. shared_buf_inc(profile->shared_size,
  376. &profile->shared_tail,
  377. SIZE_SHARED_ENTRY(cnt));
  378. /*
  379. * Possibly lost some room as we cycled around, so it's safe to
  380. * reset the max size
  381. */
  382. if (profile->shared_tail < tmp_tail)
  383. profile->shared_size =
  384. ADRENO_PROFILE_SHARED_BUF_SIZE_DWORDS;
  385. }
  386. profile->log_head = log_ptr;
  387. return;
  388. err:
  389. /* reset head/tail to same on error in hopes we work correctly later */
  390. profile->log_head = profile->log_tail;
  391. }
  392. static int profile_enable_get(void *data, u64 *val)
  393. {
  394. struct kgsl_device *device = data;
  395. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  396. mutex_lock(&device->mutex);
  397. *val = adreno_profile_enabled(&adreno_dev->profile);
  398. mutex_unlock(&device->mutex);
  399. return 0;
  400. }
  401. static int profile_enable_set(void *data, u64 val)
  402. {
  403. struct kgsl_device *device = data;
  404. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  405. struct adreno_profile *profile = &adreno_dev->profile;
  406. mutex_lock(&device->mutex);
  407. if (val && profile->log_buffer == NULL) {
  408. /* allocate profile_log_buffer the first time enabled */
  409. profile->log_buffer = vmalloc(ADRENO_PROFILE_LOG_BUF_SIZE);
  410. if (profile->log_buffer == NULL) {
  411. mutex_unlock(&device->mutex);
  412. return -ENOMEM;
  413. }
  414. profile->log_tail = profile->log_buffer;
  415. profile->log_head = profile->log_buffer;
  416. }
  417. profile->enabled = val;
  418. mutex_unlock(&device->mutex);
  419. return 0;
  420. }
  421. static ssize_t profile_assignments_read(struct file *filep,
  422. char __user *ubuf, size_t max, loff_t *ppos)
  423. {
  424. struct kgsl_device *device = (struct kgsl_device *) filep->private_data;
  425. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  426. struct adreno_profile *profile = &adreno_dev->profile;
  427. struct adreno_profile_assigns_list *entry;
  428. int len = 0, max_size = PAGE_SIZE;
  429. char *buf, *pos;
  430. ssize_t size = 0;
  431. mutex_lock(&device->mutex);
  432. if (profile->assignment_count == 0) {
  433. mutex_unlock(&device->mutex);
  434. return 0;
  435. }
  436. buf = kzalloc(max_size, GFP_KERNEL);
  437. if (!buf) {
  438. mutex_unlock(&device->mutex);
  439. return -ENOMEM;
  440. }
  441. pos = buf;
  442. /* copy all assingments from list to str */
  443. list_for_each_entry(entry, &profile->assignments_list, list) {
  444. len = scnprintf(pos, max_size, ASSIGNS_STR_FORMAT,
  445. entry->name, entry->countable);
  446. max_size -= len;
  447. pos += len;
  448. }
  449. size = simple_read_from_buffer(ubuf, max, ppos, buf,
  450. pos - buf);
  451. kfree(buf);
  452. mutex_unlock(&device->mutex);
  453. return size;
  454. }
  455. static void _remove_assignment(struct adreno_device *adreno_dev,
  456. unsigned int groupid, unsigned int countable)
  457. {
  458. struct adreno_profile *profile = &adreno_dev->profile;
  459. struct adreno_profile_assigns_list *entry, *tmp;
  460. list_for_each_entry_safe(entry, tmp, &profile->assignments_list, list) {
  461. if (entry->groupid == groupid &&
  462. entry->countable == countable) {
  463. list_del(&entry->list);
  464. profile->assignment_count--;
  465. kfree(entry);
  466. /* remove from perf counter allocation */
  467. adreno_perfcounter_put(adreno_dev, groupid, countable,
  468. PERFCOUNTER_FLAG_KERNEL);
  469. }
  470. }
  471. }
  472. static void _add_assignment(struct adreno_device *adreno_dev,
  473. unsigned int groupid, unsigned int countable)
  474. {
  475. struct adreno_profile *profile = &adreno_dev->profile;
  476. unsigned int offset, offset_hi;
  477. const char *name = NULL;
  478. name = adreno_perfcounter_get_name(adreno_dev, groupid);
  479. if (!name)
  480. return;
  481. /* if already in assigned list skip it */
  482. if (_in_assignments_list(profile, groupid, countable))
  483. return;
  484. /* add to perf counter allocation, if fail skip it */
  485. if (adreno_perfcounter_get(adreno_dev, groupid, countable,
  486. &offset, &offset_hi, PERFCOUNTER_FLAG_NONE))
  487. return;
  488. /* add to assignments list, put counter back if error */
  489. if (!_add_to_assignments_list(profile, name, groupid,
  490. countable, offset, offset_hi))
  491. adreno_perfcounter_put(adreno_dev, groupid,
  492. countable, PERFCOUNTER_FLAG_KERNEL);
  493. }
  494. static char *_parse_next_assignment(struct adreno_device *adreno_dev,
  495. char *str, int *groupid, int *countable, bool *remove)
  496. {
  497. char *groupid_str, *countable_str, *next_str = NULL;
  498. int ret;
  499. *groupid = -EINVAL;
  500. *countable = -EINVAL;
  501. *remove = false;
  502. /* remove spaces */
  503. while (*str == ' ')
  504. str++;
  505. /* check if it's a remove assignment */
  506. if (*str == '-') {
  507. *remove = true;
  508. str++;
  509. }
  510. /* get the groupid string */
  511. groupid_str = str;
  512. while (*str != ':') {
  513. if (*str == '\0')
  514. return NULL;
  515. *str = tolower(*str);
  516. str++;
  517. }
  518. if (groupid_str == str)
  519. return NULL;
  520. *str = '\0';
  521. str++;
  522. /* get the countable string */
  523. countable_str = str;
  524. while (*str != ' ' && *str != '\0')
  525. str++;
  526. if (countable_str == str)
  527. return NULL;
  528. /*
  529. * If we have reached the end of the original string then make sure we
  530. * return NULL from this function or we could accidently overrun
  531. */
  532. if (*str != '\0') {
  533. *str = '\0';
  534. next_str = str + 1;
  535. }
  536. /* set results */
  537. *groupid = adreno_perfcounter_get_groupid(adreno_dev,
  538. groupid_str);
  539. if (*groupid < 0)
  540. return NULL;
  541. ret = kstrtou32(countable_str, 10, countable);
  542. if (ret)
  543. return NULL;
  544. return next_str;
  545. }
  546. static ssize_t profile_assignments_write(struct file *filep,
  547. const char __user *user_buf, size_t len, loff_t *off)
  548. {
  549. struct kgsl_device *device = (struct kgsl_device *) filep->private_data;
  550. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  551. struct adreno_profile *profile = &adreno_dev->profile;
  552. size_t size = 0;
  553. char *buf, *pbuf;
  554. bool remove_assignment = false;
  555. int groupid, countable, ret;
  556. if (len >= PAGE_SIZE || len == 0)
  557. return -EINVAL;
  558. buf = kmalloc(len + 1, GFP_KERNEL);
  559. if (buf == NULL)
  560. return -ENOMEM;
  561. if (copy_from_user(buf, user_buf, len)) {
  562. size = -EFAULT;
  563. goto error_free;
  564. }
  565. mutex_lock(&device->mutex);
  566. if (adreno_profile_enabled(profile)) {
  567. size = -EINVAL;
  568. goto error_unlock;
  569. }
  570. ret = adreno_perfcntr_active_oob_get(adreno_dev);
  571. if (ret) {
  572. size = ret;
  573. goto error_unlock;
  574. }
  575. /*
  576. * When adding/removing assignments, ensure that the GPU is done with
  577. * all it's work. This helps to synchronize the work flow to the
  578. * GPU and avoid racey conditions.
  579. */
  580. ret = adreno_idle(device);
  581. if (ret) {
  582. size = -ETIMEDOUT;
  583. goto error_put;
  584. }
  585. /* clear all shared buffer results */
  586. adreno_profile_process_results(adreno_dev);
  587. pbuf = buf;
  588. /* clear the log buffer */
  589. if (profile->log_buffer != NULL) {
  590. profile->log_head = profile->log_buffer;
  591. profile->log_tail = profile->log_buffer;
  592. }
  593. /* for sanity and parsing, ensure it is null terminated */
  594. buf[len] = '\0';
  595. /* parse file buf and add(remove) to(from) appropriate lists */
  596. while (pbuf) {
  597. pbuf = _parse_next_assignment(adreno_dev, pbuf, &groupid,
  598. &countable, &remove_assignment);
  599. if (groupid < 0 || countable < 0)
  600. break;
  601. if (remove_assignment)
  602. _remove_assignment(adreno_dev, groupid, countable);
  603. else
  604. _add_assignment(adreno_dev, groupid, countable);
  605. }
  606. size = len;
  607. error_put:
  608. adreno_perfcntr_active_oob_put(adreno_dev);
  609. error_unlock:
  610. mutex_unlock(&device->mutex);
  611. error_free:
  612. kfree(buf);
  613. return size;
  614. }
  615. static int _pipe_print_pending(char __user *ubuf, size_t max)
  616. {
  617. loff_t unused = 0;
  618. char str[] = "Operation Would Block!";
  619. return simple_read_from_buffer(ubuf, max,
  620. &unused, str, strlen(str));
  621. }
  622. static int _pipe_print_results(struct adreno_device *adreno_dev,
  623. char __user *ubuf, size_t max)
  624. {
  625. struct adreno_profile *profile = &adreno_dev->profile;
  626. const char *grp_name;
  627. char __user *usr_buf = ubuf;
  628. unsigned int *log_ptr = NULL, *tmp_log_ptr = NULL;
  629. int len, i;
  630. int status = 0;
  631. ssize_t size, total_size = 0;
  632. unsigned int cnt, api_type, ctxt_id, pid, tid, ts, cnt_reg;
  633. unsigned long long pc_start, pc_end;
  634. const char *api_str;
  635. char format_space;
  636. loff_t unused = 0;
  637. char pipe_hdr_buf[51]; /* 4 uint32 + 5 space + 5 API type + '\0' */
  638. char pipe_cntr_buf[63]; /* 2 uint64 + 1 uint32 + 4 spaces + 8 group */
  639. /* convert unread entries to ASCII, copy to user-space */
  640. log_ptr = profile->log_tail;
  641. do {
  642. /* store the tmp var for error cases so we can skip */
  643. tmp_log_ptr = log_ptr;
  644. /* Too many to output to pipe, so skip this data */
  645. cnt = *log_ptr;
  646. log_buf_wrapinc(profile->log_buffer, &log_ptr);
  647. if (SIZE_PIPE_ENTRY(cnt) > max) {
  648. log_buf_wrapinc_len(profile->log_buffer,
  649. &tmp_log_ptr, SIZE_PIPE_ENTRY(cnt));
  650. log_ptr = tmp_log_ptr;
  651. goto done;
  652. }
  653. /*
  654. * Not enough space left in pipe, return without doing
  655. * anything
  656. */
  657. if ((max - (usr_buf - ubuf)) < SIZE_PIPE_ENTRY(cnt)) {
  658. log_ptr = tmp_log_ptr;
  659. goto done;
  660. }
  661. api_type = *log_ptr;
  662. api_str = kgsl_context_type(api_type);
  663. log_buf_wrapinc(profile->log_buffer, &log_ptr);
  664. pid = *log_ptr;
  665. log_buf_wrapinc(profile->log_buffer, &log_ptr);
  666. tid = *log_ptr;
  667. log_buf_wrapinc(profile->log_buffer, &log_ptr);
  668. ctxt_id = *log_ptr;
  669. log_buf_wrapinc(profile->log_buffer, &log_ptr);
  670. ts = *log_ptr;
  671. log_buf_wrapinc(profile->log_buffer, &log_ptr);
  672. len = scnprintf(pipe_hdr_buf, sizeof(pipe_hdr_buf) - 1,
  673. "%u %u %u %.5s %u ",
  674. pid, tid, ctxt_id, api_str, ts);
  675. size = simple_read_from_buffer(usr_buf,
  676. max - (usr_buf - ubuf),
  677. &unused, pipe_hdr_buf, len);
  678. /* non-fatal error, so skip rest of entry and return */
  679. if (size < 0) {
  680. log_buf_wrapinc_len(profile->log_buffer,
  681. &tmp_log_ptr, SIZE_PIPE_ENTRY(cnt));
  682. log_ptr = tmp_log_ptr;
  683. goto done;
  684. }
  685. unused = 0;
  686. usr_buf += size;
  687. total_size += size;
  688. for (i = 0; i < cnt; i++) {
  689. unsigned int start_lo, start_hi;
  690. unsigned int end_lo, end_hi;
  691. grp_name = adreno_perfcounter_get_name(
  692. adreno_dev, (*log_ptr >> 16) & 0xffff);
  693. /* non-fatal error, so skip rest of entry and return */
  694. if (grp_name == NULL) {
  695. log_buf_wrapinc_len(profile->log_buffer,
  696. &tmp_log_ptr, SIZE_PIPE_ENTRY(cnt));
  697. log_ptr = tmp_log_ptr;
  698. goto done;
  699. }
  700. if (i == cnt - 1)
  701. format_space = '\n';
  702. else
  703. format_space = ' ';
  704. cnt_reg = *log_ptr & 0xffff;
  705. log_buf_wrapinc(profile->log_buffer, &log_ptr);
  706. start_lo = *log_ptr;
  707. log_buf_wrapinc(profile->log_buffer, &log_ptr);
  708. start_hi = *log_ptr;
  709. log_buf_wrapinc(profile->log_buffer, &log_ptr);
  710. end_lo = *log_ptr;
  711. log_buf_wrapinc(profile->log_buffer, &log_ptr);
  712. end_hi = *log_ptr;
  713. log_buf_wrapinc(profile->log_buffer, &log_ptr);
  714. pc_start = (((uint64_t) start_hi) << 32) | start_lo;
  715. pc_end = (((uint64_t) end_hi) << 32) | end_lo;
  716. len = scnprintf(pipe_cntr_buf,
  717. sizeof(pipe_cntr_buf) - 1,
  718. "%.8s:%u %llu %llu%c",
  719. grp_name, cnt_reg, pc_start,
  720. pc_end, format_space);
  721. size = simple_read_from_buffer(usr_buf,
  722. max - (usr_buf - ubuf),
  723. &unused, pipe_cntr_buf, len);
  724. /* non-fatal error, so skip rest of entry and return */
  725. if (size < 0) {
  726. log_buf_wrapinc_len(profile->log_buffer,
  727. &tmp_log_ptr, SIZE_PIPE_ENTRY(cnt));
  728. log_ptr = tmp_log_ptr;
  729. goto done;
  730. }
  731. unused = 0;
  732. usr_buf += size;
  733. total_size += size;
  734. }
  735. } while (log_ptr != profile->log_head);
  736. done:
  737. status = total_size;
  738. profile->log_tail = log_ptr;
  739. return status;
  740. }
  741. static ssize_t profile_pipe_print(struct file *filep, char __user *ubuf,
  742. size_t max, loff_t *ppos)
  743. {
  744. struct kgsl_device *device = (struct kgsl_device *) filep->private_data;
  745. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  746. struct adreno_profile *profile = &adreno_dev->profile;
  747. char __user *usr_buf = ubuf;
  748. int status = 0;
  749. /*
  750. * this file not seekable since it only supports streaming, ignore
  751. * ppos <> 0
  752. */
  753. /*
  754. * format <pid> <tid> <context id> <cnt<<16 | client type> <timestamp>
  755. * for each perf counter <cntr_reg_off> <start hi & lo> <end hi & low>
  756. */
  757. mutex_lock(&device->mutex);
  758. while (1) {
  759. /* process any results that are available into the log_buffer */
  760. status = adreno_profile_process_results(adreno_dev);
  761. if (status > 0) {
  762. /* if we have results, print them and exit */
  763. status = _pipe_print_results(adreno_dev, usr_buf, max);
  764. break;
  765. }
  766. /* there are no unread results, act accordingly */
  767. if (filep->f_flags & O_NONBLOCK) {
  768. if (profile->shared_tail != profile->shared_head) {
  769. status = _pipe_print_pending(usr_buf, max);
  770. break;
  771. }
  772. status = 0;
  773. break;
  774. }
  775. mutex_unlock(&device->mutex);
  776. set_current_state(TASK_INTERRUPTIBLE);
  777. schedule_timeout(msecs_to_jiffies(100));
  778. mutex_lock(&device->mutex);
  779. if (signal_pending(current)) {
  780. status = 0;
  781. break;
  782. }
  783. }
  784. mutex_unlock(&device->mutex);
  785. return status;
  786. }
  787. static int profile_groups_show(struct seq_file *s, void *unused)
  788. {
  789. struct kgsl_device *device = (struct kgsl_device *) s->private;
  790. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  791. const struct adreno_perfcounters *counters =
  792. ADRENO_PERFCOUNTERS(adreno_dev);
  793. const struct adreno_perfcount_group *group;
  794. int i, j, used;
  795. mutex_lock(&device->mutex);
  796. for (i = 0; i < counters->group_count; ++i) {
  797. group = &(counters->groups[i]);
  798. /* get number of counters used for this group */
  799. used = 0;
  800. for (j = 0; j < group->reg_count; j++) {
  801. if (group->regs[j].countable !=
  802. KGSL_PERFCOUNTER_NOT_USED)
  803. used++;
  804. }
  805. seq_printf(s, "%s %d %d\n", group->name,
  806. group->reg_count, used);
  807. }
  808. mutex_unlock(&device->mutex);
  809. return 0;
  810. }
  811. DEFINE_SHOW_ATTRIBUTE(profile_groups);
  812. static const struct file_operations profile_pipe_fops = {
  813. .owner = THIS_MODULE,
  814. .open = simple_open,
  815. .read = profile_pipe_print,
  816. .llseek = noop_llseek,
  817. };
  818. static const struct file_operations profile_assignments_fops = {
  819. .owner = THIS_MODULE,
  820. .open = simple_open,
  821. .read = profile_assignments_read,
  822. .write = profile_assignments_write,
  823. .llseek = noop_llseek,
  824. };
  825. DEFINE_DEBUGFS_ATTRIBUTE(profile_enable_fops,
  826. profile_enable_get,
  827. profile_enable_set, "%llu\n");
  828. void adreno_profile_init(struct adreno_device *adreno_dev)
  829. {
  830. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  831. struct adreno_profile *profile = &adreno_dev->profile;
  832. struct dentry *profile_dir;
  833. profile->enabled = false;
  834. if (adreno_dev->hwsched_enabled)
  835. return;
  836. /* allocate shared_buffer, which includes pre_ib and post_ib */
  837. profile->shared_size = ADRENO_PROFILE_SHARED_BUF_SIZE_DWORDS;
  838. profile->shared_buffer = kgsl_allocate_global(device,
  839. profile->shared_size * sizeof(unsigned int),
  840. 0, 0, 0, "profile");
  841. if (IS_ERR(profile->shared_buffer)) {
  842. profile->shared_size = 0;
  843. return;
  844. }
  845. INIT_LIST_HEAD(&profile->assignments_list);
  846. /* Create perf counter debugfs */
  847. profile_dir = debugfs_create_dir("profiling", device->d_debugfs);
  848. if (IS_ERR(profile_dir))
  849. return;
  850. debugfs_create_file("enable", 0644, profile_dir, device,
  851. &profile_enable_fops);
  852. debugfs_create_file("blocks", 0444, profile_dir, device,
  853. &profile_groups_fops);
  854. debugfs_create_file("pipe", 0444, profile_dir, device,
  855. &profile_pipe_fops);
  856. debugfs_create_file("assignments", 0644, profile_dir, device,
  857. &profile_assignments_fops);
  858. }
  859. void adreno_profile_close(struct adreno_device *adreno_dev)
  860. {
  861. struct adreno_profile *profile = &adreno_dev->profile;
  862. struct adreno_profile_assigns_list *entry, *tmp;
  863. if (adreno_dev->hwsched_enabled)
  864. return;
  865. profile->enabled = false;
  866. vfree(profile->log_buffer);
  867. profile->log_buffer = NULL;
  868. profile->log_head = NULL;
  869. profile->log_tail = NULL;
  870. profile->shared_head = 0;
  871. profile->shared_tail = 0;
  872. profile->shared_size = 0;
  873. profile->assignment_count = 0;
  874. /* Return if list is not initialized */
  875. if (!profile->assignments_list.next)
  876. return;
  877. list_for_each_entry_safe(entry, tmp, &profile->assignments_list, list) {
  878. list_del(&entry->list);
  879. kfree(entry);
  880. }
  881. }
  882. int adreno_profile_process_results(struct adreno_device *adreno_dev)
  883. {
  884. struct adreno_profile *profile = &adreno_dev->profile;
  885. unsigned int shared_buf_tail = profile->shared_tail;
  886. if (adreno_dev->hwsched_enabled)
  887. return 0;
  888. if (!results_available(adreno_dev, profile, &shared_buf_tail))
  889. return 0;
  890. /*
  891. * transfer retired results to log_buffer
  892. * update shared_buffer tail ptr
  893. */
  894. transfer_results(profile, shared_buf_tail);
  895. return 1;
  896. }
  897. u64 adreno_profile_preib_processing(struct adreno_device *adreno_dev,
  898. struct adreno_context *drawctxt, u32 *dwords)
  899. {
  900. struct adreno_profile *profile = &adreno_dev->profile;
  901. int count = profile->assignment_count;
  902. unsigned int entry_head = profile->shared_head;
  903. unsigned int *shared_ptr;
  904. struct adreno_ringbuffer *rb = ADRENO_CURRENT_RINGBUFFER(adreno_dev);
  905. if (adreno_dev->hwsched_enabled)
  906. return 0;
  907. if (!drawctxt || !adreno_profile_assignments_ready(profile))
  908. return 0;
  909. /*
  910. * check if space available, include the post_ib in space available
  911. * check so don't have to handle trying to undo the pre_ib insertion in
  912. * ringbuffer in the case where only the post_ib fails enough space
  913. */
  914. if (SIZE_SHARED_ENTRY(count) >= shared_buf_available(profile))
  915. return 0;
  916. if (entry_head + SIZE_SHARED_ENTRY(count) >= profile->shared_size) {
  917. /* entry_head would wrap, start entry_head at 0 in buffer */
  918. entry_head = 0;
  919. profile->shared_size = profile->shared_head;
  920. profile->shared_head = 0;
  921. /* recheck space available */
  922. if (SIZE_SHARED_ENTRY(count) >= shared_buf_available(profile))
  923. return 0;
  924. }
  925. /* zero out the counter area of shared_buffer entry_head */
  926. shared_ptr = entry_head + ((unsigned int *)
  927. profile->shared_buffer->hostptr);
  928. memset(shared_ptr, 0, SIZE_SHARED_ENTRY(count) * sizeof(unsigned int));
  929. /* reserve space for the pre ib shared buffer */
  930. shared_buf_inc(profile->shared_size, &profile->shared_head,
  931. SIZE_SHARED_ENTRY(count));
  932. /* create the shared ibdesc */
  933. return _build_pre_ib_cmds(adreno_dev, profile, entry_head,
  934. rb->timestamp + 1, drawctxt, dwords);
  935. }
  936. u64 adreno_profile_postib_processing(struct adreno_device *adreno_dev,
  937. struct adreno_context *drawctxt, u32 *dwords)
  938. {
  939. struct adreno_profile *profile = &adreno_dev->profile;
  940. int count = profile->assignment_count;
  941. unsigned int entry_head = profile->shared_head -
  942. SIZE_SHARED_ENTRY(count);
  943. if (adreno_dev->hwsched_enabled)
  944. return 0;
  945. if (!drawctxt || !adreno_profile_assignments_ready(profile))
  946. return 0;
  947. /* create the shared ibdesc */
  948. return _build_post_ib_cmds(adreno_dev, profile, entry_head, dwords);
  949. }