msm_vidc_debug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include "msm_vidc_debug.h"
  6. #include "msm_vidc_driver.h"
  7. #include "msm_vidc_dt.h"
  8. #include "msm_vidc.h"
  9. #include "msm_vidc_core.h"
  10. #include "msm_vidc_inst.h"
  11. #include "msm_vidc_internal.h"
  12. #define MAX_SSR_STRING_LEN 64
  13. #define MAX_DEBUG_LEVEL_STRING_LEN 15
  14. int msm_vidc_debug = VIDC_ERR | VIDC_PRINTK | FW_ERROR | FW_FATAL;
  15. module_param(msm_vidc_debug, int, 0644);
  16. bool msm_vidc_lossless_encode = !true;
  17. EXPORT_SYMBOL(msm_vidc_lossless_encode);
  18. bool msm_vidc_syscache_disable = !true;
  19. EXPORT_SYMBOL(msm_vidc_syscache_disable);
  20. int msm_vidc_clock_voting = !1;
  21. #define MAX_DBG_BUF_SIZE 4096
  22. struct core_inst_pair {
  23. struct msm_vidc_core *core;
  24. struct msm_vidc_inst *inst;
  25. };
  26. /* debug fs support */
  27. static inline void tic(struct msm_vidc_inst *i, enum profiling_points p,
  28. char *b)
  29. {
  30. return;
  31. }
  32. static inline void toc(struct msm_vidc_inst *i, enum profiling_points p)
  33. {
  34. return;
  35. }
  36. static u32 write_str(char *buffer,
  37. size_t size, const char *fmt, ...)
  38. {
  39. va_list args;
  40. u32 len;
  41. va_start(args, fmt);
  42. len = vscnprintf(buffer, size, fmt, args);
  43. va_end(args);
  44. return len;
  45. }
  46. static ssize_t core_info_read(struct file* file, char __user* buf,
  47. size_t count, loff_t* ppos)
  48. {
  49. struct msm_vidc_core *core = file->private_data;
  50. char* dbuf, * cur, * end;
  51. ssize_t len = 0;
  52. if (!core || !core->dt) {
  53. d_vpr_e("%s: invalid params %pK\n", __func__, core);
  54. return 0;
  55. }
  56. dbuf = kzalloc(MAX_DBG_BUF_SIZE, GFP_KERNEL);
  57. if (!dbuf) {
  58. d_vpr_e("%s: Allocation failed!\n", __func__);
  59. return -ENOMEM;
  60. }
  61. cur = dbuf;
  62. end = cur + MAX_DBG_BUF_SIZE;
  63. cur += write_str(cur, end - cur, "Core state: %d\n", core->state);
  64. cur += write_str(cur, end - cur,
  65. "FW version : %s\n", core->fw_version);
  66. cur += write_str(cur, end - cur,
  67. "register_base: 0x%x\n", core->dt->register_base);
  68. cur += write_str(cur, end - cur,
  69. "register_size: %u\n", core->dt->register_size);
  70. cur += write_str(cur, end - cur, "irq: %u\n", core->dt->irq);
  71. len = simple_read_from_buffer(buf, count, ppos,
  72. dbuf, cur - dbuf);
  73. kfree(dbuf);
  74. return len;
  75. }
  76. static const struct file_operations core_info_fops = {
  77. .open = simple_open,
  78. .read = core_info_read,
  79. };
  80. static ssize_t trigger_ssr_write(struct file* filp, const char __user* buf,
  81. size_t count, loff_t* ppos)
  82. {
  83. unsigned long ssr_trigger_val = 0;
  84. int rc = 0;
  85. struct msm_vidc_core* core = filp->private_data;
  86. size_t size = MAX_SSR_STRING_LEN;
  87. char kbuf[MAX_SSR_STRING_LEN + 1] = { 0 };
  88. if (!buf)
  89. return -EINVAL;
  90. if (!count)
  91. goto exit;
  92. if (count < size)
  93. size = count;
  94. if (copy_from_user(kbuf, buf, size)) {
  95. d_vpr_e("%s: User memory fault\n", __func__);
  96. rc = -EFAULT;
  97. goto exit;
  98. }
  99. rc = kstrtoul(kbuf, 0, &ssr_trigger_val);
  100. if (rc) {
  101. d_vpr_e("returning error err %d\n", rc);
  102. rc = -EINVAL;
  103. }
  104. else {
  105. msm_vidc_trigger_ssr(core, ssr_trigger_val);
  106. rc = count;
  107. }
  108. exit:
  109. return rc;
  110. }
  111. static const struct file_operations ssr_fops = {
  112. .open = simple_open,
  113. .write = trigger_ssr_write,
  114. };
  115. struct dentry* msm_vidc_debugfs_init_drv()
  116. {
  117. struct dentry *dir = NULL;
  118. dir = debugfs_create_dir("msm_vidc", NULL);
  119. if (IS_ERR_OR_NULL(dir)) {
  120. dir = NULL;
  121. goto failed_create_dir;
  122. }
  123. debugfs_create_u32("core_clock_voting", 0644, dir,
  124. &msm_vidc_clock_voting);
  125. debugfs_create_bool("disable_video_syscache", 0644, dir,
  126. &msm_vidc_syscache_disable);
  127. debugfs_create_bool("lossless_encoding", 0644, dir,
  128. &msm_vidc_lossless_encode);
  129. return dir;
  130. failed_create_dir:
  131. if (dir)
  132. debugfs_remove_recursive(dir);
  133. return NULL;
  134. }
  135. struct dentry *msm_vidc_debugfs_init_core(void *core_in)
  136. {
  137. struct dentry *dir = NULL;
  138. char debugfs_name[MAX_DEBUGFS_NAME];
  139. struct msm_vidc_core *core = (struct msm_vidc_core *) core_in;
  140. struct dentry *parent;
  141. if (!core || !core->debugfs_parent) {
  142. d_vpr_e("%s: invalid params\n", __func__);
  143. goto failed_create_dir;
  144. }
  145. parent = core->debugfs_parent;
  146. snprintf(debugfs_name, MAX_DEBUGFS_NAME, "core");
  147. dir = debugfs_create_dir(debugfs_name, parent);
  148. if (IS_ERR_OR_NULL(dir)) {
  149. dir = NULL;
  150. d_vpr_e("Failed to create debugfs for msm_vidc\n");
  151. goto failed_create_dir;
  152. }
  153. if (!debugfs_create_file("info", 0444, dir, core, &core_info_fops)) {
  154. d_vpr_e("debugfs_create_file: fail\n");
  155. goto failed_create_dir;
  156. }
  157. if (!debugfs_create_file("trigger_ssr", 0200,
  158. dir, core, &ssr_fops)) {
  159. d_vpr_e("debugfs_create_file: fail\n");
  160. goto failed_create_dir;
  161. }
  162. failed_create_dir:
  163. return dir;
  164. }
  165. static int inst_info_open(struct inode *inode, struct file *file)
  166. {
  167. d_vpr_l("Open inode ptr: %pK\n", inode->i_private);
  168. file->private_data = inode->i_private;
  169. return 0;
  170. }
  171. static int publish_unreleased_reference(struct msm_vidc_inst *inst,
  172. char **dbuf, char *end)
  173. {
  174. return 0;
  175. }
  176. static ssize_t inst_info_read(struct file *file, char __user *buf,
  177. size_t count, loff_t *ppos)
  178. {
  179. struct core_inst_pair *idata = file->private_data;
  180. struct msm_vidc_core *core;
  181. struct msm_vidc_inst *inst;
  182. char *dbuf, *cur, *end;
  183. int i, j;
  184. ssize_t len = 0;
  185. struct v4l2_format *f;
  186. if (!idata || !idata->core || !idata->inst ||
  187. !idata->inst->capabilities) {
  188. d_vpr_e("%s: invalid params %pK\n", __func__, idata);
  189. return 0;
  190. }
  191. core = idata->core;
  192. inst = idata->inst;
  193. inst = get_inst(core, inst->session_id);
  194. if (!inst) {
  195. d_vpr_h("%s: instance has become obsolete", __func__);
  196. return 0;
  197. }
  198. dbuf = kzalloc(MAX_DBG_BUF_SIZE, GFP_KERNEL);
  199. if (!dbuf) {
  200. i_vpr_e(inst, "%s: Allocation failed!\n", __func__);
  201. len = -ENOMEM;
  202. goto failed_alloc;
  203. }
  204. cur = dbuf;
  205. end = cur + MAX_DBG_BUF_SIZE;
  206. f = &inst->fmts[OUTPUT_PORT];
  207. cur += write_str(cur, end - cur, "==============================\n");
  208. cur += write_str(cur, end - cur, "INSTANCE: %pK (%s)\n", inst,
  209. inst->domain == MSM_VIDC_ENCODER ? "Encoder" : "Decoder");
  210. cur += write_str(cur, end - cur, "==============================\n");
  211. cur += write_str(cur, end - cur, "core: %pK\n", inst->core);
  212. cur += write_str(cur, end - cur, "height: %d\n", f->fmt.pix_mp.height);
  213. cur += write_str(cur, end - cur, "width: %d\n", f->fmt.pix_mp.width);
  214. cur += write_str(cur, end - cur, "fps: %d\n",
  215. inst->capabilities->cap[FRAME_RATE].value >> 16);
  216. cur += write_str(cur, end - cur, "state: %d\n", inst->state);
  217. cur += write_str(cur, end - cur, "secure: %d\n",
  218. is_secure_session(inst));
  219. cur += write_str(cur, end - cur, "-----------Formats-------------\n");
  220. for (i = 0; i < MAX_PORT; i++) {
  221. if (i != INPUT_PORT && i != OUTPUT_PORT)
  222. continue;
  223. f = &inst->fmts[i];
  224. cur += write_str(cur, end - cur, "capability: %s\n",
  225. i == INPUT_PORT ? "Output" : "Capture");
  226. cur += write_str(cur, end - cur, "planes : %d\n",
  227. f->fmt.pix_mp.num_planes);
  228. cur += write_str(cur, end - cur,
  229. "type: %s\n", i == INPUT_PORT ?
  230. "Output" : "Capture");
  231. cur += write_str(cur, end - cur, "count: %u\n",
  232. inst->vb2q[i].num_buffers);
  233. for (j = 0; j < f->fmt.pix_mp.num_planes; j++)
  234. cur += write_str(cur, end - cur,
  235. "size for plane %d: %u\n",
  236. j, f->fmt.pix_mp.plane_fmt[j].sizeimage);
  237. cur += write_str(cur, end - cur, "\n");
  238. }
  239. cur += write_str(cur, end - cur, "-------------------------------\n");
  240. cur += write_str(cur, end - cur, "ETB Count: %d\n",
  241. inst->debug_count.etb);
  242. cur += write_str(cur, end - cur, "EBD Count: %d\n",
  243. inst->debug_count.ebd);
  244. cur += write_str(cur, end - cur, "FTB Count: %d\n",
  245. inst->debug_count.ftb);
  246. cur += write_str(cur, end - cur, "FBD Count: %d\n",
  247. inst->debug_count.fbd);
  248. publish_unreleased_reference(inst, &cur, end);
  249. len = simple_read_from_buffer(buf, count, ppos,
  250. dbuf, cur - dbuf);
  251. kfree(dbuf);
  252. failed_alloc:
  253. put_inst(inst);
  254. return len;
  255. }
  256. static int inst_info_release(struct inode *inode, struct file *file)
  257. {
  258. d_vpr_l("Release inode ptr: %pK\n", inode->i_private);
  259. file->private_data = NULL;
  260. return 0;
  261. }
  262. static const struct file_operations inst_info_fops = {
  263. .open = inst_info_open,
  264. .read = inst_info_read,
  265. .release = inst_info_release,
  266. };
  267. struct dentry *msm_vidc_debugfs_init_inst(void *instance, struct dentry *parent)
  268. {
  269. struct dentry *dir = NULL, *info = NULL;
  270. char debugfs_name[MAX_DEBUGFS_NAME];
  271. struct core_inst_pair *idata = NULL;
  272. struct msm_vidc_inst *inst = (struct msm_vidc_inst *) instance;
  273. if (!inst) {
  274. d_vpr_e("%s: invalid params\n", __func__);
  275. goto exit;
  276. }
  277. snprintf(debugfs_name, MAX_DEBUGFS_NAME, "inst_%d", inst->session_id);
  278. idata = kzalloc(sizeof(struct core_inst_pair), GFP_KERNEL);
  279. if (!idata) {
  280. i_vpr_e(inst, "%s: Allocation failed!\n", __func__);
  281. goto exit;
  282. }
  283. idata->core = inst->core;
  284. idata->inst = inst;
  285. dir = debugfs_create_dir(debugfs_name, parent);
  286. if (IS_ERR_OR_NULL(dir)) {
  287. dir = NULL;
  288. i_vpr_e(inst,
  289. "%s: Failed to create debugfs for msm_vidc\n",
  290. __func__);
  291. goto failed_create_dir;
  292. }
  293. info = debugfs_create_file("info", 0444, dir,
  294. idata, &inst_info_fops);
  295. if (IS_ERR_OR_NULL(info)) {
  296. i_vpr_e(inst, "%s: debugfs_create_file: fail\n",
  297. __func__);
  298. goto failed_create_file;
  299. }
  300. dir->d_inode->i_private = info->d_inode->i_private;
  301. inst->debug.pdata[FRAME_PROCESSING].sampling = true;
  302. return dir;
  303. failed_create_file:
  304. debugfs_remove_recursive(dir);
  305. dir = NULL;
  306. failed_create_dir:
  307. kfree(idata);
  308. exit:
  309. return dir;
  310. }
  311. void msm_vidc_debugfs_deinit_inst(void *instance)
  312. {
  313. struct dentry *dentry = NULL;
  314. struct msm_vidc_inst *inst = (struct msm_vidc_inst *) instance;
  315. if (!inst || !inst->debugfs_root)
  316. return;
  317. dentry = inst->debugfs_root;
  318. if (dentry->d_inode) {
  319. i_vpr_l(inst, "%s: Destroy %pK\n",
  320. __func__, dentry->d_inode->i_private);
  321. kfree(dentry->d_inode->i_private);
  322. dentry->d_inode->i_private = NULL;
  323. }
  324. debugfs_remove_recursive(dentry);
  325. inst->debugfs_root = NULL;
  326. }
  327. void msm_vidc_debugfs_update(void *instance,
  328. enum msm_vidc_debugfs_event e)
  329. {
  330. struct msm_vidc_inst *inst = (struct msm_vidc_inst *) instance;
  331. struct msm_vidc_debug *d;
  332. char a[64] = "Frame processing";
  333. if (!inst) {
  334. d_vpr_e("%s: invalid params\n", __func__);
  335. return;
  336. }
  337. d = &inst->debug;
  338. switch (e) {
  339. case MSM_VIDC_DEBUGFS_EVENT_ETB:
  340. inst->debug_count.etb++;
  341. if (inst->debug_count.ebd &&
  342. inst->debug_count.ftb > inst->debug_count.fbd) {
  343. d->pdata[FRAME_PROCESSING].name[0] = '\0';
  344. tic(inst, FRAME_PROCESSING, a);
  345. }
  346. break;
  347. case MSM_VIDC_DEBUGFS_EVENT_EBD:
  348. inst->debug_count.ebd++;
  349. if (inst->debug_count.ebd &&
  350. inst->debug_count.ebd == inst->debug_count.etb) {
  351. toc(inst, FRAME_PROCESSING);
  352. i_vpr_p(inst, "EBD: FW needs input buffers\n");
  353. }
  354. if (inst->debug_count.ftb == inst->debug_count.fbd)
  355. i_vpr_p(inst, "EBD: FW needs output buffers\n");
  356. break;
  357. case MSM_VIDC_DEBUGFS_EVENT_FTB:
  358. inst->debug_count.ftb++;
  359. if (inst->debug_count.ebd &&
  360. inst->debug_count.etb > inst->debug_count.ebd) {
  361. d->pdata[FRAME_PROCESSING].name[0] = '\0';
  362. tic(inst, FRAME_PROCESSING, a);
  363. }
  364. break;
  365. case MSM_VIDC_DEBUGFS_EVENT_FBD:
  366. inst->debug_count.fbd++;
  367. inst->debug.samples++;
  368. if (inst->debug_count.fbd &&
  369. inst->debug_count.fbd == inst->debug_count.ftb) {
  370. toc(inst, FRAME_PROCESSING);
  371. i_vpr_p(inst, "FBD: FW needs output buffers\n");
  372. }
  373. if (inst->debug_count.etb == inst->debug_count.ebd)
  374. i_vpr_p(inst, "FBD: FW needs input buffers\n");
  375. break;
  376. default:
  377. i_vpr_e(inst, "invalid event in debugfs: %d\n", e);
  378. break;
  379. }
  380. }
  381. int msm_vidc_check_ratelimit(void)
  382. {
  383. static DEFINE_RATELIMIT_STATE(_rs,
  384. VIDC_DBG_SESSION_RATELIMIT_INTERVAL,
  385. VIDC_DBG_SESSION_RATELIMIT_BURST);
  386. return __ratelimit(&_rs);
  387. }